manager.go 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. package routing
  2. import (
  3. "bufio"
  4. "encoding/json"
  5. "fmt"
  6. "log"
  7. "net"
  8. "os"
  9. "os/exec"
  10. "path/filepath"
  11. "sort"
  12. "strconv"
  13. "strings"
  14. "sync"
  15. "time"
  16. "github.com/vishvananda/netlink"
  17. )
  18. const (
  19. StaticRoutesStorePath = "/etc/debtik/routing/static_routes.json"
  20. PolicyRulesStorePath = "/etc/debtik/routing/policy_rules.json"
  21. CustomTablesStorePath = "/etc/debtik/routing/custom_tables.json"
  22. RTTableFilePath = "/etc/iproute2/rt_tables"
  23. )
  24. type RoutingManager struct {
  25. mu sync.Mutex
  26. staticRoutes []RouteItem
  27. policyRules []PolicyRuleItem
  28. customTables []RoutingTableItem
  29. stopChan chan struct{}
  30. }
  31. func NewRoutingManager() *RoutingManager {
  32. m := &RoutingManager{
  33. staticRoutes: make([]RouteItem, 0),
  34. policyRules: make([]PolicyRuleItem, 0),
  35. customTables: make([]RoutingTableItem, 0),
  36. stopChan: make(chan struct{}),
  37. }
  38. m.initRTTableFile()
  39. m.load()
  40. m.syncCustomTablesToSystem()
  41. m.applyAll()
  42. m.startHealthCheckWorker()
  43. m.startRouteSyncWorker()
  44. return m
  45. }
  46. func (m *RoutingManager) initRTTableFile() {
  47. _ = os.MkdirAll(filepath.Dir(RTTableFilePath), 0755)
  48. if _, err := os.Stat(RTTableFilePath); os.IsNotExist(err) {
  49. // Copy from /usr/share/iproute2/rt_tables or seed default
  50. content, err := os.ReadFile("/usr/share/iproute2/rt_tables")
  51. if err == nil {
  52. _ = os.WriteFile(RTTableFilePath, content, 0644)
  53. } else {
  54. defaultContent := "# reserved values\n255\tlocal\n254\tmain\n253\tdefault\n0\tunspec\n"
  55. _ = os.WriteFile(RTTableFilePath, []byte(defaultContent), 0644)
  56. }
  57. }
  58. }
  59. func (m *RoutingManager) load() {
  60. if data, err := os.ReadFile(StaticRoutesStorePath); err == nil {
  61. _ = json.Unmarshal(data, &m.staticRoutes)
  62. }
  63. if data, err := os.ReadFile(PolicyRulesStorePath); err == nil {
  64. _ = json.Unmarshal(data, &m.policyRules)
  65. }
  66. if data, err := os.ReadFile(CustomTablesStorePath); err == nil {
  67. _ = json.Unmarshal(data, &m.customTables)
  68. }
  69. for i := range m.staticRoutes {
  70. if !m.staticRoutes[i].Disabled && m.staticRoutes[i].HealthStatus != "offline" {
  71. m.staticRoutes[i].Active = true
  72. }
  73. }
  74. }
  75. func (m *RoutingManager) save() error {
  76. _ = os.MkdirAll("/etc/debtik/routing", 0755)
  77. if data, err := json.MarshalIndent(m.staticRoutes, "", " "); err != nil {
  78. return err
  79. } else if err = os.WriteFile(StaticRoutesStorePath, data, 0600); err != nil {
  80. return err
  81. }
  82. if data, err := json.MarshalIndent(m.policyRules, "", " "); err != nil {
  83. return err
  84. } else if err = os.WriteFile(PolicyRulesStorePath, data, 0600); err != nil {
  85. return err
  86. }
  87. if data, err := json.MarshalIndent(m.customTables, "", " "); err != nil {
  88. return err
  89. } else if err = os.WriteFile(CustomTablesStorePath, data, 0600); err != nil {
  90. return err
  91. }
  92. return nil
  93. }
  94. func (m *RoutingManager) syncCustomTablesToSystem() {
  95. content, _ := os.ReadFile(RTTableFilePath)
  96. str := string(content)
  97. var sb strings.Builder
  98. sb.WriteString(str)
  99. for _, t := range m.customTables {
  100. check := fmt.Sprintf("\t%s", t.Name)
  101. if !strings.Contains(str, check) {
  102. sb.WriteString(fmt.Sprintf("%d\t%s\n", t.TableID, t.Name))
  103. }
  104. }
  105. _ = os.WriteFile(RTTableFilePath, []byte(sb.String()), 0644)
  106. }
  107. func (m *RoutingManager) cleanRogueDefaultRoutes() {
  108. hasManagedDefault := false
  109. for _, r := range m.staticRoutes {
  110. if normalizeDst(r.DstAddress) == "default" && !r.Disabled {
  111. hasManagedDefault = true
  112. break
  113. }
  114. }
  115. if !hasManagedDefault {
  116. return
  117. }
  118. routes, err := netlink.RouteList(nil, netlink.FAMILY_V4)
  119. if err != nil {
  120. return
  121. }
  122. links, _ := netlink.LinkList()
  123. linkMap := make(map[int]string)
  124. for _, l := range links {
  125. linkMap[l.Attrs().Index] = l.Attrs().Name
  126. }
  127. for _, r := range routes {
  128. if (r.Table == 254 || r.Table == 0) && r.Dst == nil && len(r.MultiPath) == 0 {
  129. gw := ""
  130. if r.Gw != nil {
  131. gw = r.Gw.String()
  132. }
  133. ifName := linkMap[r.LinkIndex]
  134. // If priority is 0 (unmetered route from dhclient or boot) and protocol != RTPROT_STATIC:
  135. if r.Priority == 0 && int(r.Protocol) != 4 {
  136. log.Printf("[RouteManager] Removing rogue unmanaged default route: via %s dev %s proto %d", gw, ifName, r.Protocol)
  137. _ = netlink.RouteDel(&r)
  138. }
  139. }
  140. }
  141. }
  142. func (m *RoutingManager) getTableIDByName(name string) int {
  143. name = strings.TrimSpace(name)
  144. if name == "" || name == "main" {
  145. return 254
  146. }
  147. if name == "default" {
  148. return 253
  149. }
  150. if name == "local" {
  151. return 255
  152. }
  153. for id, n := range m.getTableMap() {
  154. if strings.EqualFold(n, name) {
  155. return id
  156. }
  157. }
  158. if id, err := strconv.Atoi(name); err == nil {
  159. return id
  160. }
  161. return 254
  162. }
  163. func (m *RoutingManager) startRouteSyncWorker() {
  164. ticker := time.NewTicker(3 * time.Second)
  165. go func() {
  166. for {
  167. select {
  168. case <-m.stopChan:
  169. ticker.Stop()
  170. return
  171. case <-ticker.C:
  172. m.syncAllRoutesWorker()
  173. }
  174. }
  175. }()
  176. }
  177. func (m *RoutingManager) syncAllRoutesWorker() {
  178. m.mu.Lock()
  179. defer m.mu.Unlock()
  180. // Unmanaged routes belong to DHCP, the host or other routing daemons.
  181. liveRoutes, err := netlink.RouteList(nil, netlink.FAMILY_V4)
  182. if err != nil {
  183. return
  184. }
  185. type groupKey struct {
  186. dst string
  187. table int
  188. dist int
  189. }
  190. kernelRouteGroups := make(map[groupKey]bool)
  191. for _, lr := range liveRoutes {
  192. dst := "default"
  193. if lr.Dst != nil {
  194. dst = lr.Dst.String()
  195. }
  196. tbl := lr.Table
  197. if tbl == 0 {
  198. tbl = 254
  199. }
  200. k := groupKey{dst: normalizeDst(dst), table: tbl, dist: lr.Priority}
  201. kernelRouteGroups[k] = true
  202. }
  203. seen := make(map[groupKey]bool)
  204. for _, r := range m.staticRoutes {
  205. if r.Disabled || r.HealthStatus == "offline" {
  206. continue
  207. }
  208. normDst := normalizeDst(r.DstAddress)
  209. tableID := m.getTableIDByName(r.RoutingTable)
  210. k := groupKey{dst: normDst, table: tableID, dist: r.Distance}
  211. if !seen[k] {
  212. seen[k] = true
  213. if !kernelRouteGroups[k] {
  214. log.Printf("[RouteSync] Missing kernel route %s table %s (id %d) metric %d -> Restoring", normDst, r.RoutingTable, tableID, r.Distance)
  215. if err := m.syncRouteGroupToKernel(r.DstAddress, r.RoutingTable, r.Distance); err != nil {
  216. log.Printf("route synchronization failed: %v", err)
  217. }
  218. }
  219. }
  220. }
  221. }
  222. func (m *RoutingManager) applyAll() {
  223. // Unmanaged routes belong to DHCP, the host or other routing daemons.
  224. // Apply all static routes grouped by (dst, table, distance) for ECMP
  225. type groupKey struct {
  226. dst string
  227. table string
  228. dist int
  229. }
  230. seen := make(map[groupKey]bool)
  231. for _, r := range m.staticRoutes {
  232. normDst := normalizeDst(r.DstAddress)
  233. tbl := strings.TrimSpace(r.RoutingTable)
  234. if tbl == "" {
  235. tbl = "main"
  236. }
  237. k := groupKey{dst: normDst, table: tbl, dist: r.Distance}
  238. if !seen[k] {
  239. seen[k] = true
  240. _ = m.syncRouteGroupToKernel(r.DstAddress, r.RoutingTable, r.Distance)
  241. }
  242. }
  243. // Apply policy rules
  244. for _, pr := range m.policyRules {
  245. _ = m.applyRuleToKernel(pr)
  246. }
  247. }
  248. // ==================== Routes (/ip route) ====================
  249. func isDynamicProtocol(proto netlink.RouteProtocol) bool {
  250. switch int(proto) {
  251. case 11, // RTPROT_ZEBRA
  252. 12, // RTPROT_BIRD
  253. 16, // RTPROT_DHCP
  254. 42, // RTPROT_BABEL
  255. 186, // RTPROT_BGP
  256. 187, // RTPROT_ISIS
  257. 188, // RTPROT_OSPF
  258. 189, // RTPROT_RIP
  259. 192: // RTPROT_EIGRP
  260. return true
  261. default:
  262. return false
  263. }
  264. }
  265. func getRouteProtocolName(proto netlink.RouteProtocol, priority int, isDirect bool, isMulticast bool) string {
  266. if isMulticast {
  267. return "multicast"
  268. }
  269. switch int(proto) {
  270. case 16:
  271. return "dhcp"
  272. case 4:
  273. return "static"
  274. case 186:
  275. return "bgp"
  276. case 188:
  277. return "ospf"
  278. case 189:
  279. return "rip"
  280. case 11:
  281. return "zebra"
  282. case 42:
  283. return "babel"
  284. case 12: // RTPROT_BIRD
  285. if priority == 20 {
  286. return "bgp"
  287. } else if priority == 200 {
  288. return "ibgp"
  289. } else if priority == 110 {
  290. return "ospf"
  291. } else if priority == 120 {
  292. return "rip"
  293. }
  294. return "bgp"
  295. case 2:
  296. return "connected"
  297. case 3:
  298. return "boot"
  299. default:
  300. if isDirect {
  301. return "connected"
  302. }
  303. if priority == 20 {
  304. return "bgp"
  305. } else if priority == 200 {
  306. return "ibgp"
  307. } else if priority == 110 {
  308. return "ospf"
  309. } else if priority == 120 {
  310. return "rip"
  311. }
  312. return "static"
  313. }
  314. }
  315. func (m *RoutingManager) listLiveRoutesLocked() []RouteItem {
  316. tableNames := m.getTableMap()
  317. links, _ := netlink.LinkList()
  318. linkMap := make(map[int]string)
  319. for _, l := range links {
  320. linkMap[l.Attrs().Index] = l.Attrs().Name
  321. }
  322. var liveRoutes []RouteItem
  323. routes, err := netlink.RouteList(nil, netlink.FAMILY_V4)
  324. if err == nil {
  325. for _, r := range routes {
  326. dst := "0.0.0.0/0"
  327. if r.Dst != nil {
  328. dst = r.Dst.String()
  329. }
  330. gw := ""
  331. if r.Gw != nil {
  332. gw = r.Gw.String()
  333. }
  334. tableName := "main"
  335. if name, ok := tableNames[r.Table]; ok {
  336. tableName = name
  337. } else if r.Table != 254 && r.Table != 0 {
  338. tableName = strconv.Itoa(r.Table)
  339. }
  340. isDyn := isDynamicProtocol(r.Protocol)
  341. isDirect := (r.Scope == netlink.SCOPE_LINK || r.Protocol == netlink.RouteProtocol(2)) && !isDyn && r.Protocol != 4
  342. isStatic := !isDirect && !isDyn
  343. isMulticast := r.Type == 5 || strings.HasPrefix(dst, "224.") || strings.HasPrefix(dst, "ff") || dst == "224.0.0.0/4"
  344. protoName := getRouteProtocolName(r.Protocol, r.Priority, isDirect, isMulticast)
  345. if len(r.MultiPath) > 0 {
  346. for _, nh := range r.MultiPath {
  347. nhGw := ""
  348. if nh.Gw != nil {
  349. nhGw = nh.Gw.String()
  350. }
  351. nhIfName := linkMap[nh.LinkIndex]
  352. cleanDst := strings.ReplaceAll(dst, "/", "_")
  353. cleanGw := nhGw
  354. if cleanGw == "" {
  355. cleanGw = nhIfName
  356. }
  357. routeID := fmt.Sprintf("kr-%d-%s-%s", r.Table, cleanDst, cleanGw)
  358. liveRoutes = append(liveRoutes, RouteItem{
  359. ID: routeID,
  360. DstAddress: dst,
  361. Gateway: nhGw,
  362. Interface: nhIfName,
  363. Distance: r.Priority,
  364. Protocol: protoName,
  365. RoutingTable: tableName,
  366. TableID: r.Table,
  367. Active: true,
  368. Connect: isDirect,
  369. Dynamic: isDyn,
  370. Static: isStatic,
  371. ECMP: true,
  372. IsMultiPath: true,
  373. Multicast: isMulticast,
  374. Disabled: false,
  375. })
  376. }
  377. continue
  378. }
  379. ifName := linkMap[r.LinkIndex]
  380. act := "lookup"
  381. if r.Type == 6 { // RTN_BLACKHOLE
  382. gw = "blackhole"
  383. act = "blackhole"
  384. }
  385. cleanDst := strings.ReplaceAll(dst, "/", "_")
  386. cleanGw := gw
  387. if cleanGw == "" {
  388. cleanGw = ifName
  389. }
  390. routeID := fmt.Sprintf("kr-%d-%s-%s", r.Table, cleanDst, cleanGw)
  391. liveRoutes = append(liveRoutes, RouteItem{
  392. ID: routeID,
  393. DstAddress: dst,
  394. Gateway: gw,
  395. Action: act,
  396. Interface: ifName,
  397. Distance: r.Priority,
  398. Protocol: protoName,
  399. RoutingTable: tableName,
  400. TableID: r.Table,
  401. Active: true,
  402. Connect: isDirect,
  403. Dynamic: isDyn,
  404. Static: isStatic,
  405. Multicast: isMulticast,
  406. Disabled: false,
  407. })
  408. }
  409. }
  410. return liveRoutes
  411. }
  412. func (m *RoutingManager) ListRoutes() []RouteItem {
  413. m.mu.Lock()
  414. defer m.mu.Unlock()
  415. liveRoutes := m.listLiveRoutesLocked()
  416. // Merge static routes that might be disabled or have custom metadata
  417. for _, sr := range m.staticRoutes {
  418. found := false
  419. for i, lr := range liveRoutes {
  420. if lr.DstAddress == sr.DstAddress && lr.RoutingTable == sr.RoutingTable && (sr.Gateway == "" || lr.Gateway == sr.Gateway) {
  421. liveRoutes[i].Comment = sr.Comment
  422. liveRoutes[i].Static = true
  423. liveRoutes[i].Dynamic = false
  424. liveRoutes[i].Protocol = "static"
  425. liveRoutes[i].ID = sr.ID
  426. if sr.Distance > 0 && liveRoutes[i].Distance == 0 {
  427. liveRoutes[i].Distance = sr.Distance
  428. }
  429. liveRoutes[i].CheckGateway = sr.CheckGateway
  430. liveRoutes[i].CheckTarget = sr.CheckTarget
  431. liveRoutes[i].CheckInterval = sr.CheckInterval
  432. liveRoutes[i].CheckThreshold = sr.CheckThreshold
  433. liveRoutes[i].HealthStatus = sr.HealthStatus
  434. liveRoutes[i].HealthRTT = sr.HealthRTT
  435. liveRoutes[i].HealthLossPct = sr.HealthLossPct
  436. liveRoutes[i].HealthFailCount = sr.HealthFailCount
  437. liveRoutes[i].HealthLastCheck = sr.HealthLastCheck
  438. found = true
  439. break
  440. }
  441. }
  442. if !found {
  443. // Inactive or disabled static route
  444. srCopy := sr
  445. srCopy.Active = false
  446. srCopy.Static = true
  447. srCopy.Dynamic = false
  448. srCopy.Protocol = "static"
  449. liveRoutes = append(liveRoutes, srCopy)
  450. }
  451. }
  452. // Detect Equal-Cost Multi-Path (ECMP) routes
  453. groupCounts := make(map[string]int)
  454. for _, r := range liveRoutes {
  455. if r.Active && !r.Disabled {
  456. key := fmt.Sprintf("%s|%s|%d", r.DstAddress, r.RoutingTable, r.Distance)
  457. groupCounts[key]++
  458. }
  459. }
  460. for i := range liveRoutes {
  461. key := fmt.Sprintf("%s|%s|%d", liveRoutes[i].DstAddress, liveRoutes[i].RoutingTable, liveRoutes[i].Distance)
  462. if liveRoutes[i].IsMultiPath || (liveRoutes[i].Active && !liveRoutes[i].Disabled && groupCounts[key] > 1) {
  463. liveRoutes[i].ECMP = true
  464. }
  465. if liveRoutes[i].DstAddress == "224.0.0.0/4" || strings.HasPrefix(liveRoutes[i].DstAddress, "224.") || strings.HasPrefix(liveRoutes[i].DstAddress, "ff") {
  466. liveRoutes[i].Multicast = true
  467. }
  468. }
  469. sort.Slice(liveRoutes, func(i, j int) bool {
  470. if liveRoutes[i].RoutingTable != liveRoutes[j].RoutingTable {
  471. return liveRoutes[i].RoutingTable < liveRoutes[j].RoutingTable
  472. }
  473. if liveRoutes[i].DstAddress != liveRoutes[j].DstAddress {
  474. return liveRoutes[i].DstAddress < liveRoutes[j].DstAddress
  475. }
  476. return liveRoutes[i].Gateway < liveRoutes[j].Gateway
  477. })
  478. return liveRoutes
  479. }
  480. func (m *RoutingManager) SaveRoute(r RouteItem) (err error) {
  481. m.mu.Lock()
  482. defer m.mu.Unlock()
  483. before := append([]RouteItem(nil), m.staticRoutes...)
  484. dirty, applied := false, false
  485. defer func() {
  486. if err != nil && dirty {
  487. m.staticRoutes = before
  488. if applied {
  489. _ = m.syncRouteGroupToKernel(r.DstAddress, r.RoutingTable, r.Distance)
  490. }
  491. m.applyAll()
  492. _ = m.save()
  493. }
  494. }()
  495. if r.DstAddress == "" {
  496. return fmt.Errorf("destination address is required")
  497. }
  498. if r.DstAddress == "default" {
  499. r.DstAddress = "0.0.0.0/0"
  500. }
  501. if r.RoutingTable == "" {
  502. r.RoutingTable = "main"
  503. }
  504. if _, _, parseErr := net.ParseCIDR(r.DstAddress); parseErr != nil && net.ParseIP(r.DstAddress) == nil {
  505. return fmt.Errorf("invalid destination: %s", r.DstAddress)
  506. }
  507. dirty = true
  508. r.Static = true
  509. // 1. If old route details are provided from edit modal, remove old route first
  510. if r.OldDstAddress != "" {
  511. oldDist := 0
  512. if r.OldDistance != nil {
  513. oldDist = *r.OldDistance
  514. }
  515. oldRoute := RouteItem{
  516. DstAddress: r.OldDstAddress,
  517. Gateway: r.OldGateway,
  518. RoutingTable: r.OldRoutingTable,
  519. Distance: oldDist,
  520. Interface: r.OldInterface,
  521. }
  522. _ = m.removeRouteFromKernel(oldRoute)
  523. }
  524. // 2. If existing entry in m.staticRoutes exists, remove its old route from kernel
  525. foundIndex := -1
  526. for i, existing := range m.staticRoutes {
  527. matchByID := r.ID != "" && existing.ID == r.ID
  528. matchByExactParams := r.ID == "" &&
  529. normalizeDst(existing.DstAddress) == normalizeDst(r.DstAddress) &&
  530. existing.RoutingTable == r.RoutingTable &&
  531. existing.Gateway == r.Gateway &&
  532. existing.Interface == r.Interface &&
  533. existing.Distance == r.Distance
  534. if matchByID || matchByExactParams {
  535. foundIndex = i
  536. _ = m.removeRouteFromKernel(existing)
  537. break
  538. }
  539. }
  540. // 3. If r.ID starts with kr- (kernel static route being edited/adopted), remove it from kernel
  541. if strings.HasPrefix(r.ID, "kr-") {
  542. for _, lr := range m.listLiveRoutesLocked() {
  543. if lr.ID == r.ID {
  544. _ = m.removeRouteFromKernel(lr)
  545. break
  546. }
  547. }
  548. r.ID = fmt.Sprintf("route-%d", time.Now().UnixNano())
  549. }
  550. if r.ID == "" {
  551. r.ID = fmt.Sprintf("route-%d", time.Now().UnixNano())
  552. }
  553. if r.CheckInterval <= 0 {
  554. r.CheckInterval = 5
  555. }
  556. if r.CheckThreshold <= 0 {
  557. r.CheckThreshold = 3
  558. }
  559. if r.CheckGateway == "internet" && strings.TrimSpace(r.CheckTarget) == "" {
  560. r.CheckTarget = "1.1.1.1"
  561. }
  562. if r.CheckGateway != "" && r.CheckGateway != "none" {
  563. if r.HealthStatus == "" {
  564. r.HealthStatus = "checking"
  565. }
  566. } else {
  567. r.CheckGateway = "none"
  568. r.HealthStatus = "none"
  569. }
  570. if foundIndex >= 0 {
  571. if r.HealthStatus == "checking" && m.staticRoutes[foundIndex].HealthStatus != "" && m.staticRoutes[foundIndex].CheckGateway == r.CheckGateway {
  572. r.HealthStatus = m.staticRoutes[foundIndex].HealthStatus
  573. r.HealthRTT = m.staticRoutes[foundIndex].HealthRTT
  574. r.HealthLossPct = m.staticRoutes[foundIndex].HealthLossPct
  575. r.HealthLastCheck = m.staticRoutes[foundIndex].HealthLastCheck
  576. }
  577. m.staticRoutes[foundIndex] = r
  578. } else {
  579. m.staticRoutes = append(m.staticRoutes, r)
  580. }
  581. for i := range m.staticRoutes {
  582. if m.staticRoutes[i].ID == r.ID {
  583. if !m.staticRoutes[i].Disabled && m.staticRoutes[i].HealthStatus != "offline" {
  584. m.staticRoutes[i].Active = true
  585. } else {
  586. m.staticRoutes[i].Active = false
  587. }
  588. break
  589. }
  590. }
  591. if saveErr := m.save(); saveErr != nil {
  592. return saveErr
  593. }
  594. if err := m.syncRouteGroupToKernel(r.DstAddress, r.RoutingTable, r.Distance); err != nil {
  595. return err
  596. }
  597. applied = true
  598. if r.OldDstAddress != "" {
  599. oldDist := 0
  600. if r.OldDistance != nil {
  601. oldDist = *r.OldDistance
  602. }
  603. if r.OldDstAddress != r.DstAddress || r.OldRoutingTable != r.RoutingTable || oldDist != r.Distance {
  604. if err := m.syncRouteGroupToKernel(r.OldDstAddress, r.OldRoutingTable, oldDist); err != nil {
  605. return err
  606. }
  607. }
  608. }
  609. return nil
  610. }
  611. func (m *RoutingManager) DeleteRoute(id string) error {
  612. m.mu.Lock()
  613. defer m.mu.Unlock()
  614. var updated []RouteItem
  615. var target *RouteItem
  616. for _, r := range m.staticRoutes {
  617. if r.ID == id {
  618. target = &r
  619. } else {
  620. updated = append(updated, r)
  621. }
  622. }
  623. if target != nil {
  624. m.staticRoutes = updated
  625. _ = m.save()
  626. _ = m.syncRouteGroupToKernel(target.DstAddress, target.RoutingTable, target.Distance)
  627. return nil
  628. }
  629. // If not in staticRoutes, could be a live route (e.g. kr-...)
  630. for _, lr := range m.listLiveRoutesLocked() {
  631. if lr.ID == id {
  632. _ = m.removeRouteFromKernel(lr)
  633. return nil
  634. }
  635. }
  636. return fmt.Errorf("route not found")
  637. }
  638. func (m *RoutingManager) SetRouteState(id string, disabled bool) error {
  639. m.mu.Lock()
  640. defer m.mu.Unlock()
  641. for i := range m.staticRoutes {
  642. if m.staticRoutes[i].ID == id {
  643. m.staticRoutes[i].Disabled = disabled
  644. if !disabled && m.staticRoutes[i].HealthStatus != "offline" {
  645. m.staticRoutes[i].Active = true
  646. } else if disabled {
  647. m.staticRoutes[i].Active = false
  648. }
  649. _ = m.save()
  650. _ = m.syncRouteGroupToKernel(m.staticRoutes[i].DstAddress, m.staticRoutes[i].RoutingTable, m.staticRoutes[i].Distance)
  651. return nil
  652. }
  653. }
  654. // If not in staticRoutes, could be a kernel static route to disable
  655. if disabled {
  656. for _, lr := range m.listLiveRoutesLocked() {
  657. if lr.ID == id {
  658. lr.Disabled = true
  659. lr.Static = true
  660. lr.Active = false
  661. lr.ID = fmt.Sprintf("route-%d", time.Now().UnixNano())
  662. m.staticRoutes = append(m.staticRoutes, lr)
  663. _ = m.save()
  664. _ = m.syncRouteGroupToKernel(lr.DstAddress, lr.RoutingTable, lr.Distance)
  665. return nil
  666. }
  667. }
  668. }
  669. return fmt.Errorf("route not found")
  670. }
  671. func normalizeDst(dst string) string {
  672. dst = strings.TrimSpace(dst)
  673. if dst == "" || dst == "default" || dst == "0.0.0.0/0" {
  674. return "default"
  675. }
  676. if !strings.Contains(dst, "/") && net.ParseIP(dst) != nil {
  677. return dst + "/32"
  678. }
  679. return dst
  680. }
  681. func (m *RoutingManager) syncRouteGroupToKernel(dstAddress, routingTable string, distance int) error {
  682. normDst := normalizeDst(dstAddress)
  683. tbl := strings.TrimSpace(routingTable)
  684. if tbl == "" {
  685. tbl = "main"
  686. }
  687. var activeRoutes []RouteItem
  688. for _, r := range m.staticRoutes {
  689. rDst := normalizeDst(r.DstAddress)
  690. rTbl := strings.TrimSpace(r.RoutingTable)
  691. if rTbl == "" {
  692. rTbl = "main"
  693. }
  694. if rDst == normDst && rTbl == tbl && r.Distance == distance {
  695. if !r.Disabled && r.Active && r.HealthStatus != "offline" {
  696. activeRoutes = append(activeRoutes, r)
  697. }
  698. }
  699. }
  700. if len(activeRoutes) == 0 {
  701. // Safety guard: if all default routes are marked offline, do NOT delete the default route into a blackout!
  702. // Fallback to keeping all enabled static routes in the group active so recovery traffic and emergency pings can flow.
  703. if normDst == "default" {
  704. var fallbackRoutes []RouteItem
  705. for _, r := range m.staticRoutes {
  706. rDst := normalizeDst(r.DstAddress)
  707. rTbl := strings.TrimSpace(r.RoutingTable)
  708. if rTbl == "" {
  709. rTbl = "main"
  710. }
  711. if rDst == normDst && rTbl == tbl && r.Distance == distance && !r.Disabled {
  712. fallbackRoutes = append(fallbackRoutes, r)
  713. }
  714. }
  715. if len(fallbackRoutes) > 0 {
  716. log.Printf("[RouteManager] All default routes offline in group! Keeping fallback routes active to prevent blackout")
  717. activeRoutes = fallbackRoutes
  718. }
  719. }
  720. }
  721. if len(activeRoutes) == 0 {
  722. args := []string{"route", "del", normDst}
  723. if distance > 0 {
  724. args = append(args, "metric", strconv.Itoa(distance))
  725. }
  726. if tbl != "main" {
  727. args = append(args, "table", tbl)
  728. }
  729. args = append(args, "proto", "static")
  730. _ = exec.Command("ip", args...).Run()
  731. // Also delete blackhole route variant if it existed
  732. bhArgs := []string{"route", "del", "blackhole", normDst}
  733. if distance > 0 {
  734. bhArgs = append(bhArgs, "metric", strconv.Itoa(distance))
  735. }
  736. if tbl != "main" {
  737. bhArgs = append(bhArgs, "table", tbl)
  738. }
  739. _ = exec.Command("ip", bhArgs...).Run()
  740. return nil
  741. }
  742. if len(activeRoutes) == 1 {
  743. r := activeRoutes[0]
  744. gw := strings.TrimSpace(r.Gateway)
  745. isBlackhole := strings.EqualFold(gw, "blackhole") || strings.EqualFold(r.Action, "blackhole")
  746. var args []string
  747. if isBlackhole {
  748. args = []string{"route", "replace", "blackhole", normDst}
  749. } else {
  750. args = []string{"route", "replace", normDst}
  751. if gw != "" {
  752. if net.ParseIP(gw) != nil {
  753. args = append(args, "via", gw)
  754. } else {
  755. args = append(args, "dev", gw)
  756. }
  757. }
  758. iface := strings.TrimSpace(r.Interface)
  759. if iface != "" && gw != iface && (gw == "" || net.ParseIP(gw) != nil) {
  760. args = append(args, "dev", iface)
  761. }
  762. if gw != "" && iface != "" && net.ParseIP(gw) != nil {
  763. args = append(args, "onlink")
  764. }
  765. }
  766. if distance > 0 {
  767. args = append(args, "metric", strconv.Itoa(distance))
  768. }
  769. if tbl != "main" {
  770. args = append(args, "table", tbl)
  771. }
  772. args = append(args, "proto", "static")
  773. cmd := exec.Command("ip", args...)
  774. if out, err := cmd.CombinedOutput(); err != nil {
  775. return fmt.Errorf("failed to apply route (%s): %s: %w", strings.Join(args, " "), string(out), err)
  776. }
  777. return nil
  778. }
  779. // Multiple routes: ECMP multipath route
  780. args := []string{"route", "replace", normDst, "scope", "global"}
  781. if distance > 0 {
  782. args = append(args, "metric", strconv.Itoa(distance))
  783. }
  784. if tbl != "main" {
  785. args = append(args, "table", tbl)
  786. }
  787. args = append(args, "proto", "static")
  788. for _, r := range activeRoutes {
  789. args = append(args, "nexthop")
  790. gw := strings.TrimSpace(r.Gateway)
  791. if gw != "" && net.ParseIP(gw) != nil {
  792. args = append(args, "via", gw)
  793. }
  794. iface := strings.TrimSpace(r.Interface)
  795. if iface == "" && gw != "" && net.ParseIP(gw) == nil {
  796. iface = gw
  797. }
  798. if iface != "" {
  799. args = append(args, "dev", iface)
  800. }
  801. if gw != "" && iface != "" && net.ParseIP(gw) != nil {
  802. args = append(args, "onlink")
  803. }
  804. args = append(args, "weight", "1")
  805. }
  806. cmd := exec.Command("ip", args...)
  807. if out, err := cmd.CombinedOutput(); err != nil {
  808. return fmt.Errorf("failed to apply multipath route (%s): %s: %w", strings.Join(args, " "), string(out), err)
  809. }
  810. return nil
  811. }
  812. func (m *RoutingManager) applyRouteToKernel(r RouteItem) error {
  813. return m.syncRouteGroupToKernel(r.DstAddress, r.RoutingTable, r.Distance)
  814. }
  815. func (m *RoutingManager) removeRouteFromKernel(r RouteItem) error {
  816. return m.syncRouteGroupToKernel(r.DstAddress, r.RoutingTable, r.Distance)
  817. }
  818. // ==================== Policy Rules (/ip route rule) ====================
  819. func (m *RoutingManager) ListPolicyRules() []PolicyRuleItem {
  820. m.mu.Lock()
  821. defer m.mu.Unlock()
  822. // Query live rules from kernel
  823. rules, err := netlink.RuleList(netlink.FAMILY_V4)
  824. if err != nil {
  825. return m.policyRules
  826. }
  827. tableNames := m.getTableMap()
  828. var res []PolicyRuleItem
  829. userRulesByPrio := make(map[int]PolicyRuleItem)
  830. for _, pr := range m.policyRules {
  831. userRulesByPrio[pr.Priority] = pr
  832. }
  833. for _, r := range rules {
  834. tableName := "main"
  835. if name, ok := tableNames[r.Table]; ok {
  836. tableName = name
  837. } else if r.Table != 254 && r.Table != 0 {
  838. tableName = strconv.Itoa(r.Table)
  839. }
  840. src := ""
  841. if r.Src != nil {
  842. src = r.Src.String()
  843. }
  844. dst := ""
  845. if r.Dst != nil {
  846. dst = r.Dst.String()
  847. }
  848. markStr := ""
  849. if r.Mark > 0 {
  850. markStr = fmt.Sprintf("0x%x (%d)", r.Mark, r.Mark)
  851. }
  852. id := fmt.Sprintf("rule-%d-%d", r.Priority, r.Table)
  853. comment := ""
  854. isDefault := false
  855. isSystem := false
  856. if r.Priority == 0 || r.Priority == 32766 || r.Priority == 32767 {
  857. isDefault = true
  858. isSystem = true
  859. } else if userRule, ok := userRulesByPrio[r.Priority]; ok {
  860. if userRule.ID != "" {
  861. id = userRule.ID
  862. }
  863. comment = userRule.Comment
  864. isDefault = false
  865. isSystem = false
  866. } else {
  867. // System/daemon rule not added by user in debtik (e.g. WireGuard, IPsec)
  868. isSystem = true
  869. }
  870. res = append(res, PolicyRuleItem{
  871. ID: id,
  872. Priority: r.Priority,
  873. SrcAddress: src,
  874. DstAddress: dst,
  875. RoutingMark: markStr,
  876. Table: tableName,
  877. TableID: r.Table,
  878. Action: "lookup",
  879. Comment: comment,
  880. IsDefault: isDefault,
  881. IsSystem: isSystem,
  882. })
  883. }
  884. return res
  885. }
  886. func (m *RoutingManager) SavePolicyRule(pr PolicyRuleItem) error {
  887. m.mu.Lock()
  888. defer m.mu.Unlock()
  889. if pr.Table == "" {
  890. pr.Table = "main"
  891. }
  892. if pr.ID == "" {
  893. pr.ID = fmt.Sprintf("prule-%d", time.Now().UnixNano())
  894. }
  895. if pr.Priority == 0 {
  896. pr.Priority = 1000 + len(m.policyRules)*10
  897. }
  898. if err := m.applyRuleToKernel(pr); err != nil {
  899. return err
  900. }
  901. m.policyRules = append(m.policyRules, pr)
  902. return m.save()
  903. }
  904. func (m *RoutingManager) DeletePolicyRule(id string) error {
  905. m.mu.Lock()
  906. defer m.mu.Unlock()
  907. var updated []PolicyRuleItem
  908. var target *PolicyRuleItem
  909. for _, pr := range m.policyRules {
  910. if pr.ID == id || fmt.Sprintf("rule-%d-%d", pr.Priority, pr.TableID) == id {
  911. target = &pr
  912. } else {
  913. updated = append(updated, pr)
  914. }
  915. }
  916. if target != nil {
  917. _ = m.removeRuleFromKernel(*target)
  918. } else if strings.HasPrefix(id, "rule-") {
  919. parts := strings.Split(id, "-")
  920. if len(parts) >= 2 {
  921. if prio, err := strconv.Atoi(parts[1]); err == nil && prio > 0 {
  922. _ = exec.Command("ip", "rule", "del", "pref", strconv.Itoa(prio)).Run()
  923. }
  924. }
  925. }
  926. m.policyRules = updated
  927. return m.save()
  928. }
  929. func (m *RoutingManager) applyRuleToKernel(pr PolicyRuleItem) error {
  930. args := []string{"rule", "add"}
  931. if pr.Priority > 0 {
  932. args = append(args, "pref", strconv.Itoa(pr.Priority))
  933. }
  934. if pr.SrcAddress != "" {
  935. args = append(args, "from", pr.SrcAddress)
  936. }
  937. if pr.DstAddress != "" {
  938. args = append(args, "to", pr.DstAddress)
  939. }
  940. if pr.RoutingMark != "" {
  941. // Clean mark string if it has (hex)
  942. markClean := pr.RoutingMark
  943. if strings.Contains(markClean, "(") {
  944. markClean = strings.TrimSpace(markClean[:strings.Index(markClean, "(")])
  945. }
  946. args = append(args, "fwmark", markClean)
  947. }
  948. if pr.Table != "" {
  949. args = append(args, "lookup", pr.Table)
  950. }
  951. cmd := exec.Command("ip", args...)
  952. out, err := cmd.CombinedOutput()
  953. if err != nil {
  954. return fmt.Errorf("failed to add rule (%s): %s: %w", strings.Join(args, " "), string(out), err)
  955. }
  956. return nil
  957. }
  958. func (m *RoutingManager) removeRuleFromKernel(pr PolicyRuleItem) error {
  959. if pr.Priority > 0 {
  960. _ = exec.Command("ip", "rule", "del", "pref", strconv.Itoa(pr.Priority)).Run()
  961. return nil
  962. }
  963. args := []string{"rule", "del"}
  964. if pr.RoutingMark != "" {
  965. markClean := pr.RoutingMark
  966. if strings.Contains(markClean, "(") {
  967. markClean = strings.TrimSpace(markClean[:strings.Index(markClean, "(")])
  968. }
  969. args = append(args, "fwmark", markClean)
  970. }
  971. if pr.Table != "" {
  972. args = append(args, "lookup", pr.Table)
  973. }
  974. _ = exec.Command("ip", args...).Run()
  975. return nil
  976. }
  977. // ==================== Routing Tables ====================
  978. func (m *RoutingManager) ListTables() []RoutingTableItem {
  979. m.mu.Lock()
  980. defer m.mu.Unlock()
  981. tables := []RoutingTableItem{
  982. {Name: "main", TableID: 254, Comment: "Default system routing table"},
  983. {Name: "default", TableID: 253, Comment: "Default fallback table"},
  984. {Name: "local", TableID: 255, Comment: "Local kernel device table"},
  985. }
  986. tables = append(tables, m.customTables...)
  987. return tables
  988. }
  989. func (m *RoutingManager) SaveTable(t RoutingTableItem) error {
  990. m.mu.Lock()
  991. defer m.mu.Unlock()
  992. if t.Name == "" {
  993. return fmt.Errorf("table name is required")
  994. }
  995. if t.TableID == 0 {
  996. // Auto assign FIB ID starting at 100
  997. t.TableID = 100 + len(m.customTables)
  998. }
  999. for _, existing := range m.customTables {
  1000. if existing.Name == t.Name || existing.TableID == t.TableID {
  1001. return fmt.Errorf("table with name %q or ID %d already exists", t.Name, t.TableID)
  1002. }
  1003. }
  1004. m.customTables = append(m.customTables, t)
  1005. _ = m.save()
  1006. m.syncCustomTablesToSystem()
  1007. return nil
  1008. }
  1009. func (m *RoutingManager) getTableMap() map[int]string {
  1010. tableMap := map[int]string{
  1011. 254: "main",
  1012. 253: "default",
  1013. 255: "local",
  1014. }
  1015. // Read /etc/iproute2/rt_tables
  1016. if f, err := os.Open(RTTableFilePath); err == nil {
  1017. defer f.Close()
  1018. scanner := bufio.NewScanner(f)
  1019. for scanner.Scan() {
  1020. line := strings.TrimSpace(scanner.Text())
  1021. if line == "" || strings.HasPrefix(line, "#") {
  1022. continue
  1023. }
  1024. parts := strings.Fields(line)
  1025. if len(parts) >= 2 {
  1026. if id, err := strconv.Atoi(parts[0]); err == nil {
  1027. tableMap[id] = parts[1]
  1028. }
  1029. }
  1030. }
  1031. }
  1032. for _, t := range m.customTables {
  1033. tableMap[t.TableID] = t.Name
  1034. }
  1035. return tableMap
  1036. }
  1037. func (m *RoutingManager) Stop() {
  1038. if m.stopChan != nil {
  1039. select {
  1040. case <-m.stopChan:
  1041. default:
  1042. close(m.stopChan)
  1043. }
  1044. }
  1045. }
  1046. // RenameInterface updates static routes and policy rules when an interface is renamed.
  1047. func (m *RoutingManager) RenameInterface(oldName, newName string) {
  1048. m.mu.Lock()
  1049. defer m.mu.Unlock()
  1050. changed := false
  1051. for i := range m.staticRoutes {
  1052. if m.staticRoutes[i].Interface == oldName {
  1053. m.staticRoutes[i].Interface = newName
  1054. changed = true
  1055. }
  1056. if m.staticRoutes[i].Gateway == oldName {
  1057. m.staticRoutes[i].Gateway = newName
  1058. changed = true
  1059. }
  1060. }
  1061. if changed {
  1062. _ = m.save()
  1063. }
  1064. }