types.go 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. package dynamicrouting
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. "strings"
  6. )
  7. // BGPInstance represents global BGP settings (/routing bgp instance)
  8. type BGPInstance struct {
  9. Enabled bool `json:"enabled"`
  10. AS uint32 `json:"as"`
  11. RouterID string `json:"router_id"`
  12. HoldTime int `json:"hold_time"` // Default 180
  13. KeepaliveTime int `json:"keepalive_time"` // Default 60
  14. RedistributeConnected bool `json:"redistribute_connected"`
  15. RedistributeStatic bool `json:"redistribute_static"`
  16. RedistributeOSPF bool `json:"redistribute_ospf"`
  17. Running bool `json:"running"`
  18. }
  19. // BGPPeer represents a BGP peer connection (/routing bgp connection / peer)
  20. type BGPPeer struct {
  21. ID string `json:"id"`
  22. Name string `json:"name"`
  23. Enabled bool `json:"enabled"`
  24. Comment string `json:"comment,omitempty"`
  25. Instance string `json:"instance,omitempty"`
  26. Template string `json:"template,omitempty"`
  27. LocalAS uint32 `json:"local_as,omitempty"` // Multi-Instance Local AS (if 0, uses global AS)
  28. RouterID string `json:"router_id,omitempty"`
  29. AFI string `json:"afi,omitempty"` // "ip", "ipv6"
  30. RemoteAddress string `json:"remote_address"`
  31. RemotePort int `json:"remote_port,omitempty"`
  32. RemoteAS uint32 `json:"remote_as"`
  33. RemoteAllowAS string `json:"remote_allow_as,omitempty"`
  34. LocalAddress string `json:"local_address,omitempty"`
  35. LocalPort int `json:"local_port,omitempty"`
  36. LocalRole string `json:"local_role,omitempty"` // ebgp, ibgp
  37. Password string `json:"password,omitempty"` // TCP MD5 Key
  38. Multihop bool `json:"multihop"`
  39. TxTTL int `json:"tx_ttl,omitempty"`
  40. RxMinTTL int `json:"rx_min_ttl,omitempty"`
  41. Connect bool `json:"connect"`
  42. Listen bool `json:"listen"`
  43. HoldTime int `json:"hold_time,omitempty"`
  44. KeepaliveTime int `json:"keepalive_time,omitempty"`
  45. UseBFD bool `json:"use_bfd,omitempty"`
  46. RoutingTable string `json:"routing_table,omitempty"`
  47. VRF string `json:"vrf,omitempty"`
  48. NoClientReflect bool `json:"no_client_reflect,omitempty"`
  49. RedistConnected bool `json:"redist_connected"`
  50. RedistStatic bool `json:"redist_static"`
  51. RedistOSPF bool `json:"redist_ospf"`
  52. RedistBGP bool `json:"redist_bgp"`
  53. RedistRIP bool `json:"redist_rip,omitempty"`
  54. RedistVPN bool `json:"redist_vpn,omitempty"`
  55. RedistDHCP bool `json:"redist_dhcp,omitempty"`
  56. RedistFantasy bool `json:"redist_fantasy,omitempty"`
  57. RedistModem bool `json:"redist_modem,omitempty"`
  58. RedistBgpMplsVpn bool `json:"redist_bgp_mpls_vpn,omitempty"`
  59. RedistSlaac bool `json:"redist_slaac,omitempty"`
  60. DefaultOriginate bool `json:"default_originate"`
  61. NetworkBlackhole bool `json:"network_blackhole,omitempty"`
  62. NoEarlyCut bool `json:"no_early_cut,omitempty"`
  63. KeepSentAttrs bool `json:"keep_sent_attrs,omitempty"`
  64. InputAffinity string `json:"input_affinity,omitempty"`
  65. OutputAffinity string `json:"output_affinity,omitempty"`
  66. NexthopChoice string `json:"nexthop_choice,omitempty"` // default, force-self
  67. NexthopSelf bool `json:"nexthop_self"`
  68. DefaultPrepend int `json:"default_prepend,omitempty"`
  69. AllowASIn int `json:"allow_as_in,omitempty"`
  70. RemovePrivateAS bool `json:"remove_private_as"`
  71. ASOverride bool `json:"as_override,omitempty"`
  72. IgnoreASPathLen bool `json:"ignore_as_path_len,omitempty"`
  73. InFilter string `json:"in_filter,omitempty"` // Routing Filter chain for incoming routes
  74. InAcceptNLRI string `json:"in_accept_nlri,omitempty"`
  75. InAcceptCommunities string `json:"in_accept_communities,omitempty"`
  76. InAcceptExtCommunities string `json:"in_accept_ext_communities,omitempty"`
  77. InAcceptLargeCommunities string `json:"in_accept_large_communities,omitempty"`
  78. InFilterCommunities string `json:"in_filter_communities,omitempty"`
  79. InFilterExtCommunities string `json:"in_filter_ext_communities,omitempty"`
  80. InFilterLargeCommunities string `json:"in_filter_large_communities,omitempty"`
  81. InFilterUnknown bool `json:"in_filter_unknown,omitempty"`
  82. OutFilter string `json:"out_filter,omitempty"` // Routing Filter chain for outgoing routes
  83. OutSelectionPolicy string `json:"out_selection_policy,omitempty"`
  84. OutputNetwork string `json:"output_network,omitempty"`
  85. OutputNetworks []string `json:"output_networks,omitempty"` // Dynamic [+] / [-] output networks
  86. Disabled bool `json:"disabled"`
  87. Passive bool `json:"passive"`
  88. // Runtime telemetry
  89. State string `json:"state,omitempty"` // Established, Active, Connect, Idle
  90. Uptime string `json:"uptime,omitempty"`
  91. PrefixReceived int `json:"prefix_received"`
  92. PrefixSent int `json:"prefix_sent"`
  93. }
  94. // BGPNetwork represents an advertised network (/routing bgp network)
  95. type BGPNetwork struct {
  96. ID string `json:"id"`
  97. Network string `json:"network"` // CIDR, e.g. 10.200.0.0/24
  98. Disabled bool `json:"disabled"`
  99. Comment string `json:"comment,omitempty"`
  100. }
  101. // BGPRoute represents an active BGP route
  102. type BGPRoute struct {
  103. Prefix string `json:"prefix"`
  104. NextHop string `json:"next_hop"`
  105. Metric int `json:"metric"`
  106. Weight int `json:"weight"`
  107. Path string `json:"path"`
  108. Origin string `json:"origin"`
  109. Best bool `json:"best"`
  110. }
  111. // OSPFInstance represents an OSPF instance (/routing ospf instance)
  112. type OSPFInstance struct {
  113. ID string `json:"id"`
  114. Name string `json:"name"` // Default "Default"
  115. Version int `json:"version"` // 2 or 3 (default 2)
  116. VRF string `json:"vrf"` // Default "main"
  117. RouterID string `json:"router_id"` // e.g. "10.42.62.254"
  118. RoutingTable string `json:"routing_table,omitempty"`// Default "main"
  119. Enabled bool `json:"enabled"`
  120. Disabled bool `json:"disabled"`
  121. Comment string `json:"comment,omitempty"`
  122. InFilter string `json:"in_filter,omitempty"`
  123. OutFilter string `json:"out_filter,omitempty"`
  124. RedistributeConnected bool `json:"redistribute_connected"`
  125. RedistributeStatic bool `json:"redistribute_static"`
  126. RedistributeBGP bool `json:"redistribute_bgp"`
  127. RedistributeDefault bool `json:"redistribute_default"` // MikroTik redistribute-default
  128. OriginateDefault string `json:"originate_default,omitempty"` // "never", "if-installed", "always"
  129. DefaultMetric int `json:"default_metric"`
  130. Running bool `json:"running"`
  131. }
  132. // UnmarshalJSON supports both int (2, 3) and string ("2", "3", "ospfv2", "ospfv3") for Version.
  133. func (o *OSPFInstance) UnmarshalJSON(data []byte) error {
  134. type Alias OSPFInstance
  135. aux := struct {
  136. RawVersion interface{} `json:"version"`
  137. *Alias
  138. }{
  139. Alias: (*Alias)(o),
  140. }
  141. if err := json.Unmarshal(data, &aux); err != nil {
  142. return err
  143. }
  144. switch v := aux.RawVersion.(type) {
  145. case float64:
  146. o.Version = int(v)
  147. case int:
  148. o.Version = v
  149. case string:
  150. vStr := strings.ToLower(strings.TrimSpace(v))
  151. if strings.Contains(vStr, "3") || strings.Contains(vStr, "v3") || vStr == "ipv6" {
  152. o.Version = 3
  153. } else if strings.Contains(vStr, "2") || strings.Contains(vStr, "v2") || vStr == "ipv4" {
  154. o.Version = 2
  155. } else if parsed, err := strconv.Atoi(vStr); err == nil {
  156. o.Version = parsed
  157. }
  158. }
  159. if o.Version <= 0 {
  160. o.Version = 2
  161. }
  162. return nil
  163. }
  164. // OSPFArea represents an OSPF area (/routing ospf area)
  165. type OSPFArea struct {
  166. ID string `json:"id"`
  167. Name string `json:"name"` // Default "Default"
  168. Instance string `json:"instance,omitempty"` // Default "Default"
  169. AreaID string `json:"area_id"` // e.g. "0.0.0.0"
  170. AreaType string `json:"area_type"` // default, stub, nssa
  171. DefaultCost int `json:"default_cost,omitempty"`
  172. NoSummaries bool `json:"no_summaries,omitempty"`
  173. Disabled bool `json:"disabled"`
  174. Comment string `json:"comment,omitempty"`
  175. }
  176. // OSPFAreaRange represents an area range (/routing ospf area range)
  177. type OSPFAreaRange struct {
  178. ID string `json:"id"`
  179. Area string `json:"area"` // Area name or ID
  180. Prefix string `json:"prefix"` // CIDR e.g. "10.42.0.0/16"
  181. Cost int `json:"cost"`
  182. Advertise bool `json:"advertise"` // default true
  183. Disabled bool `json:"disabled"`
  184. Comment string `json:"comment,omitempty"`
  185. }
  186. // OSPFInterface represents an OSPF interface template (/routing ospf interface-template)
  187. type OSPFInterface struct {
  188. ID string `json:"id"`
  189. Interfaces string `json:"interfaces,omitempty"` // ether1, wlan1, all...
  190. Area string `json:"area"` // Area ID or name e.g. "Default" / "0.0.0.0"
  191. Networks string `json:"networks,omitempty"` // CIDR e.g. "10.42.36.244/30"
  192. NetworkType string `json:"network_type"` // broadcast, ptp, nbma, point-to-multipoint
  193. Cost int `json:"cost"` // Metric, default 1
  194. Priority int `json:"priority"` // Default 128
  195. HelloInterval int `json:"hello_interval"` // Default 10
  196. DeadInterval int `json:"dead_interval"` // Default 40
  197. Passive bool `json:"passive"`
  198. Auth string `json:"auth,omitempty"` // none, simple, md5
  199. AuthKey string `json:"auth_key,omitempty"`
  200. UseBFD bool `json:"use_bfd,omitempty"`
  201. Disabled bool `json:"disabled"`
  202. Comment string `json:"comment,omitempty"`
  203. // Backward compatibility aliases
  204. Network string `json:"network,omitempty"`
  205. Interface string `json:"interface,omitempty"`
  206. AuthType string `json:"auth_type,omitempty"`
  207. }
  208. // OSPFInterfaceLive represents dynamic live OSPF interface state (/routing ospf interface)
  209. type OSPFInterfaceLive struct {
  210. Flags string `json:"flags"` // "D" (dynamic)
  211. Interface string `json:"interface"` // e.g. "eth0"
  212. Address string `json:"address"` // e.g. "192.168.131.0/24"
  213. Area string `json:"area"` // e.g. "Default" or "0.0.0.0"
  214. InstanceID int `json:"instance_id"` // 0
  215. State string `json:"state"` // dr, backup, waiting, passive, ptp
  216. Cost int `json:"cost"`
  217. Priority int `json:"priority"`
  218. }
  219. // OSPFStaticNeighbor represents a static OSPF neighbor (/routing ospf static-neighbor)
  220. type OSPFStaticNeighbor struct {
  221. ID string `json:"id"`
  222. Address string `json:"address"`
  223. Area string `json:"area"`
  224. PollInterval int `json:"poll_interval"`
  225. Cost int `json:"cost"`
  226. Disabled bool `json:"disabled"`
  227. Comment string `json:"comment,omitempty"`
  228. }
  229. // OSPFNeighbor represents a live OSPF neighbor (/routing ospf neighbor)
  230. type OSPFNeighbor struct {
  231. Flags string `json:"flags"` // "D" (dynamic)
  232. Comment string `json:"comment,omitempty"`
  233. Instance string `json:"instance"` // "Default"
  234. Area string `json:"area"` // "Default"
  235. Address string `json:"address"` // "10.42.62.249"
  236. State string `json:"state"` // "Full"
  237. StateChanges int `json:"state_changes"` // e.g. 6
  238. NeighborID string `json:"neighbor_id,omitempty"`
  239. Interface string `json:"interface,omitempty"`
  240. DeadTimer string `json:"dead_timer,omitempty"`
  241. Priority int `json:"priority,omitempty"`
  242. }
  243. // OSPFLSA represents an LSA record in the Link-State Database (/routing ospf lsa)
  244. type OSPFLSA struct {
  245. Flags string `json:"flags"` // "D", "SD"
  246. Instance string `json:"instance"` // "Default"
  247. Area string `json:"area"` // "Default"
  248. Type string `json:"type"` // "router", "network", "as-external", "summary-net", "summary-asbr"
  249. Originator string `json:"originator"` // Router ID
  250. ID string `json:"id"` // LS ID
  251. Link string `json:"link"`
  252. LinkIndex int `json:"link_index"`
  253. Sequence string `json:"sequence"` // e.g. "8000006e"
  254. Age int `json:"age"` // seconds
  255. }
  256. // OSPFRoute represents an active OSPF route
  257. type OSPFRoute struct {
  258. Prefix string `json:"prefix"`
  259. RouteType string `json:"route_type"` // Intra-Area, Inter-Area, External
  260. Cost int `json:"cost"`
  261. Area string `json:"area"`
  262. NextHop string `json:"next_hop"`
  263. Interface string `json:"interface"`
  264. }
  265. // RoutingFilterRule represents a route filter rule (/routing filter rule)
  266. type RoutingFilterRule struct {
  267. ID string `json:"id"`
  268. Chain string `json:"chain"` // e.g. "bgp-in", "bgp-out", "isp-in"
  269. RuleOrder int `json:"rule_order"` // e.g. 10, 20, 30... Sequence number
  270. Rule string `json:"rule"` // RouterOS v7 rule script e.g. "if (dst in 10.0.0.0/8) { set bgp-local-pref 150; accept; }"
  271. Action string `json:"action"` // "accept" (permit), "discard" (deny), "reject", "passthrough"
  272. Prefix string `json:"prefix,omitempty"` // CIDR e.g. "10.0.0.0/8" or "0.0.0.0/0"
  273. PrefixLen string `json:"prefix_len,omitempty"` // e.g. "24-32", "ge 24 le 32", "24"
  274. MatchProtocol string `json:"match_protocol,omitempty"` // "any", "bgp", "ospf", "connected", "static"
  275. MatchASPath string `json:"match_as_path,omitempty"` // regex e.g. "^65000_"
  276. MatchCommunity string `json:"match_community,omitempty"`// e.g. "65001:100"
  277. SetLocalPref int `json:"set_local_pref,omitempty"` // BGP Local Preference
  278. SetMetric int `json:"set_metric,omitempty"` // BGP MED / OSPF Metric
  279. SetASPrepend int `json:"set_as_prepend,omitempty"` // AS Path prepend count
  280. SetCommunity string `json:"set_community,omitempty"` // Set BGP community
  281. SetNextHop string `json:"set_next_hop,omitempty"` // Custom Next Hop IP
  282. SetDistance int `json:"set_distance,omitempty"` // Administrative Distance
  283. Disabled bool `json:"disabled"`
  284. Comment string `json:"comment,omitempty"`
  285. }
  286. // RoutingFilterSelectRule represents a deprecated route selection rule (/routing filter select-rule)
  287. type RoutingFilterSelectRule struct {
  288. ID string `json:"id"`
  289. Chain string `json:"chain"`
  290. RuleOrder int `json:"rule_order"`
  291. Rule string `json:"rule"`
  292. Disabled bool `json:"disabled"`
  293. Comment string `json:"comment,omitempty"`
  294. }
  295. // RoutingFilterCommunityList represents a named community set (/routing filter community-list)
  296. type RoutingFilterCommunityList struct {
  297. ID string `json:"id"`
  298. Name string `json:"name"`
  299. Communities []string `json:"communities"`
  300. Regexp string `json:"regexp,omitempty"`
  301. Disabled bool `json:"disabled"`
  302. Comment string `json:"comment,omitempty"`
  303. }
  304. // RoutingFilterASList represents a named AS number list (/routing filter num-list)
  305. type RoutingFilterASList struct {
  306. ID string `json:"id"`
  307. Name string `json:"name"`
  308. Range string `json:"range"`
  309. Disabled bool `json:"disabled"`
  310. Comment string `json:"comment,omitempty"`
  311. }
  312. // RIPInstance represents a RIP routing instance (/routing rip instance)
  313. type RIPInstance struct {
  314. ID string `json:"id"`
  315. Name string `json:"name"` // e.g. "default"
  316. Version int `json:"version"` // 2 for IPv4, 1 for RIPng IPv6 (default 2)
  317. VRF string `json:"vrf,omitempty"` // default "main"
  318. RoutingTable string `json:"routing_table,omitempty"`// default "main"
  319. Enabled bool `json:"enabled"`
  320. Disabled bool `json:"disabled"`
  321. Comment string `json:"comment,omitempty"`
  322. InFilter string `json:"in_filter,omitempty"`
  323. OutFilter string `json:"out_filter,omitempty"`
  324. RedistributeConnected bool `json:"redistribute_connected"`
  325. RedistributeStatic bool `json:"redistribute_static"`
  326. RedistributeOSPF bool `json:"redistribute_ospf"`
  327. RedistributeBGP bool `json:"redistribute_bgp"`
  328. RedistributeDefault bool `json:"redistribute_default"`
  329. DefaultMetric int `json:"default_metric"` // default 1
  330. UpdateTime int `json:"update_time"` // default 30
  331. TimeoutTime int `json:"timeout_time"` // default 180
  332. GarbageTime int `json:"garbage_time"` // default 120
  333. Running bool `json:"running"`
  334. }
  335. // UnmarshalJSON supports both int (2, 1) and string ("2", "1", "rip2", "ripng") for Version.
  336. func (r *RIPInstance) UnmarshalJSON(data []byte) error {
  337. type Alias RIPInstance
  338. aux := struct {
  339. RawVersion interface{} `json:"version"`
  340. *Alias
  341. }{
  342. Alias: (*Alias)(r),
  343. }
  344. if err := json.Unmarshal(data, &aux); err != nil {
  345. return err
  346. }
  347. switch v := aux.RawVersion.(type) {
  348. case float64:
  349. r.Version = int(v)
  350. case int:
  351. r.Version = v
  352. case string:
  353. vStr := strings.ToLower(strings.TrimSpace(v))
  354. if strings.Contains(vStr, "ng") || strings.Contains(vStr, "v1") || vStr == "1" || vStr == "ipv6" {
  355. r.Version = 1
  356. } else if strings.Contains(vStr, "2") || vStr == "ipv4" {
  357. r.Version = 2
  358. } else if parsed, err := strconv.Atoi(vStr); err == nil {
  359. r.Version = parsed
  360. }
  361. }
  362. if r.Version == 0 {
  363. r.Version = 2
  364. }
  365. return nil
  366. }
  367. // RIPInterface represents a RIP interface template (/routing rip interface)
  368. type RIPInterface struct {
  369. ID string `json:"id"`
  370. Instance string `json:"instance,omitempty"`
  371. Interface string `json:"interface"` // e.g. "eth0" or "*"
  372. Metric int `json:"metric"` // Metric/cost added (default 1)
  373. Mode string `json:"mode,omitempty"` // "multicast", "broadcast" (default multicast)
  374. Passive bool `json:"passive"`
  375. UseBFD bool `json:"use_bfd,omitempty"`
  376. Auth string `json:"auth,omitempty"` // "none", "simple", "md5"
  377. AuthKey string `json:"auth_key,omitempty"`
  378. Disabled bool `json:"disabled"`
  379. Comment string `json:"comment,omitempty"`
  380. }
  381. // RIPNeighbor represents an explicit RIP neighbor (/routing rip neighbor)
  382. type RIPNeighbor struct {
  383. ID string `json:"id"`
  384. Instance string `json:"instance,omitempty"`
  385. Address string `json:"address"` // Neighbor IP
  386. Interface string `json:"interface,omitempty"`
  387. Disabled bool `json:"disabled"`
  388. Comment string `json:"comment,omitempty"`
  389. }
  390. // RIPNeighborLive represents a live RIP neighbor detected by BIRD
  391. type RIPNeighborLive struct {
  392. Address string `json:"address"`
  393. Interface string `json:"interface"`
  394. Uptime string `json:"uptime"`
  395. LastSeen string `json:"last_seen"`
  396. }
  397. // RIPRoute represents an active RIP route learned or advertised
  398. type RIPRoute struct {
  399. Prefix string `json:"prefix"`
  400. NextHop string `json:"next_hop"`
  401. Metric int `json:"metric"`
  402. Interface string `json:"interface"`
  403. Uptime string `json:"uptime"`
  404. }
  405. // BFDInstance represents global BFD settings (/routing bfd instance)
  406. type BFDInstance struct {
  407. Enabled bool `json:"enabled"`
  408. Disabled bool `json:"disabled"`
  409. Comment string `json:"comment,omitempty"`
  410. Running bool `json:"running"`
  411. }
  412. // BFDInterface represents a BFD interface template (/routing bfd interface)
  413. type BFDInterface struct {
  414. ID string `json:"id"`
  415. Interface string `json:"interface"` // e.g. "eth0" or "*"
  416. MinRxInterval int `json:"min_rx_interval"` // ms, default 100
  417. MinTxInterval int `json:"min_tx_interval"` // ms, default 100
  418. IdleTxInterval int `json:"idle_tx_interval,omitempty"`// ms, default 1000
  419. Multiplier int `json:"multiplier"` // default 3
  420. Passive bool `json:"passive"`
  421. Auth string `json:"auth,omitempty"` // "none", "simple", "meticulous keyed sha1"
  422. AuthKey string `json:"auth_key,omitempty"`
  423. Disabled bool `json:"disabled"`
  424. Comment string `json:"comment,omitempty"`
  425. }
  426. // BFDNeighbor represents an explicit BFD neighbor / peer (/routing bfd peer)
  427. type BFDNeighbor struct {
  428. ID string `json:"id"`
  429. Address string `json:"address"`
  430. Interface string `json:"interface,omitempty"`
  431. LocalAddress string `json:"local_address,omitempty"`
  432. Multihop bool `json:"multihop"`
  433. Disabled bool `json:"disabled"`
  434. Comment string `json:"comment,omitempty"`
  435. }
  436. // BFDSession represents a live BFD session (/routing bfd session)
  437. type BFDSession struct {
  438. IP string `json:"ip"`
  439. Interface string `json:"interface"`
  440. State string `json:"state"` // "Up", "Down", "Init", "AdminDown"
  441. Since string `json:"since"`
  442. Interval string `json:"interval"`
  443. Timeout string `json:"timeout"`
  444. }
  445. // BabelInstance represents a Babel instance (/routing babel instance)
  446. type BabelInstance struct {
  447. ID string `json:"id"`
  448. Name string `json:"name"` // "default"
  449. RouterID string `json:"router_id,omitempty"`
  450. VRF string `json:"vrf,omitempty"`
  451. RoutingTable string `json:"routing_table,omitempty"`
  452. Enabled bool `json:"enabled"`
  453. Disabled bool `json:"disabled"`
  454. Comment string `json:"comment,omitempty"`
  455. InFilter string `json:"in_filter,omitempty"`
  456. OutFilter string `json:"out_filter,omitempty"`
  457. RedistributeConnected bool `json:"redistribute_connected"`
  458. RedistributeStatic bool `json:"redistribute_static"`
  459. RedistributeOSPF bool `json:"redistribute_ospf"`
  460. RedistributeBGP bool `json:"redistribute_bgp"`
  461. RedistributeDefault bool `json:"redistribute_default"`
  462. DefaultMetric int `json:"default_metric"`
  463. Running bool `json:"running"`
  464. }
  465. // BabelInterface represents a Babel interface template (/routing babel interface)
  466. type BabelInterface struct {
  467. ID string `json:"id"`
  468. Instance string `json:"instance,omitempty"`
  469. Interface string `json:"interface"` // "eth0" or "*"
  470. Type string `json:"type,omitempty"` // "wired", "wireless" (default "wired")
  471. RxCost int `json:"rxcost,omitempty"` // default 10 or 96
  472. HelloInterval int `json:"hello_interval,omitempty"` // default 4 s
  473. UpdateInterval int `json:"update_interval,omitempty"` // default 16 s
  474. Auth string `json:"auth,omitempty"` // "none", "mac"
  475. AuthKey string `json:"auth_key,omitempty"`
  476. Disabled bool `json:"disabled"`
  477. Comment string `json:"comment,omitempty"`
  478. }
  479. // BabelNeighbor represents a live Babel neighbor (/routing babel neighbor)
  480. type BabelNeighbor struct {
  481. IP string `json:"ip"`
  482. Interface string `json:"interface"`
  483. Metric int `json:"metric"`
  484. Cost int `json:"cost,omitempty"`
  485. Routes int `json:"routes,omitempty"`
  486. Hellos int `json:"hellos,omitempty"`
  487. Uptime string `json:"uptime,omitempty"`
  488. Expires string `json:"expires,omitempty"`
  489. Auth string `json:"auth,omitempty"`
  490. RTT float64 `json:"rtt,omitempty"`
  491. }
  492. // BabelRoute represents an active Babel route (/routing babel route)
  493. type BabelRoute struct {
  494. Prefix string `json:"prefix"`
  495. NextHop string `json:"next_hop"`
  496. Metric int `json:"metric"`
  497. Interface string `json:"interface"`
  498. Flag string `json:"flag,omitempty"`
  499. Seqno int `json:"seqno,omitempty"`
  500. Expires string `json:"expires,omitempty"`
  501. }
  502. // BabelInterfaceLive represents a running Babel interface reported by BIRD (/routing babel interface)
  503. type BabelInterfaceLive struct {
  504. Instance string `json:"instance"`
  505. Interface string `json:"interface"`
  506. State string `json:"state"`
  507. Auth string `json:"auth"`
  508. RxCost int `json:"rxcost"`
  509. Neighbors int `json:"neighbors"`
  510. Timer string `json:"timer"`
  511. NextHopV4 string `json:"next_hop_v4"`
  512. NextHopV6 string `json:"next_hop_v6"`
  513. }
  514. // RAdvInstance represents an IPv6 Router Advertisement instance (/routing radv instance)
  515. type RAdvInstance struct {
  516. ID string `json:"id"`
  517. Name string `json:"name"` // "default"
  518. Enabled bool `json:"enabled"`
  519. Disabled bool `json:"disabled"`
  520. Comment string `json:"comment,omitempty"`
  521. Running bool `json:"running"`
  522. }
  523. // RAdvInterface represents a RAdv interface (/routing radv interface)
  524. type RAdvInterface struct {
  525. ID string `json:"id"`
  526. Instance string `json:"instance,omitempty"`
  527. Interface string `json:"interface"`
  528. MinRAInterval int `json:"min_ra_interval,omitempty"` // default 200 s
  529. MaxRAInterval int `json:"max_ra_interval,omitempty"` // default 600 s
  530. DefaultLifetime int `json:"default_lifetime,omitempty"` // default 1800 s
  531. HopLimit int `json:"hop_limit,omitempty"` // default 64
  532. Prefixes []string `json:"prefixes,omitempty"` // IPv6 prefixes e.g. ["2001:db8::/64"]
  533. RDNSS []string `json:"rdnss,omitempty"` // IPv6 DNS servers e.g. ["2001:4860:4860::8888"]
  534. Disabled bool `json:"disabled"`
  535. Comment string `json:"comment,omitempty"`
  536. }
  537. // EIGRPInstance represents an EIGRP routing instance (/routing eigrp instance)
  538. type EIGRPInstance struct {
  539. ID string `json:"id"`
  540. Name string `json:"name"` // e.g. "default", "eigrp100"
  541. AS int `json:"as"` // Autonomous System number, default 100
  542. RouterID string `json:"router_id,omitempty"` // e.g. "192.168.131.56"
  543. VRF string `json:"vrf,omitempty"` // default "main"
  544. Enabled bool `json:"enabled"`
  545. Disabled bool `json:"disabled"`
  546. Running bool `json:"running"`
  547. Comment string `json:"comment,omitempty"`
  548. InFilter string `json:"in_filter,omitempty"`
  549. OutFilter string `json:"out_filter,omitempty"`
  550. RedistributeConnected bool `json:"redistribute_connected"`
  551. RedistributeStatic bool `json:"redistribute_static"`
  552. RedistributeOSPF bool `json:"redistribute_ospf"`
  553. RedistributeBGP bool `json:"redistribute_bgp"`
  554. RedistributeDefault bool `json:"redistribute_default"`
  555. Variance int `json:"variance,omitempty"` // default 1
  556. MaximumPaths int `json:"maximum_paths,omitempty"` // default 4
  557. DefaultMetric int `json:"default_metric,omitempty"`
  558. K1 int `json:"k1"` // default 1
  559. K2 int `json:"k2"` // default 0
  560. K3 int `json:"k3"` // default 1
  561. K4 int `json:"k4"` // default 0
  562. K5 int `json:"k5"` // default 0
  563. }
  564. // UnmarshalJSON supports both int and string for AS and numeric fields.
  565. func (e *EIGRPInstance) UnmarshalJSON(data []byte) error {
  566. type Alias EIGRPInstance
  567. aux := struct {
  568. RawAS interface{} `json:"as"`
  569. *Alias
  570. }{
  571. Alias: (*Alias)(e),
  572. }
  573. if err := json.Unmarshal(data, &aux); err != nil {
  574. return err
  575. }
  576. switch v := aux.RawAS.(type) {
  577. case float64:
  578. e.AS = int(v)
  579. case int:
  580. e.AS = v
  581. case string:
  582. if parsed, err := strconv.Atoi(strings.TrimSpace(v)); err == nil {
  583. e.AS = parsed
  584. }
  585. }
  586. if e.AS == 0 {
  587. e.AS = 100
  588. }
  589. if e.Variance == 0 {
  590. e.Variance = 1
  591. }
  592. if e.MaximumPaths == 0 {
  593. e.MaximumPaths = 4
  594. }
  595. return nil
  596. }
  597. // EIGRPInterface represents an EIGRP interface template (/routing eigrp interface)
  598. type EIGRPInterface struct {
  599. ID string `json:"id"`
  600. Instance string `json:"instance,omitempty"` // Instance Name or AS
  601. Interface string `json:"interface"` // e.g. "eth0" or "*"
  602. HelloInterval int `json:"hello_interval,omitempty"`// Default 5s
  603. HoldTime int `json:"hold_time,omitempty"` // Default 15s
  604. Passive bool `json:"passive"`
  605. SplitHorizon bool `json:"split_horizon"` // Default true
  606. AuthMode string `json:"auth_mode,omitempty"` // "none", "md5"
  607. AuthKey string `json:"auth_key,omitempty"`
  608. Disabled bool `json:"disabled"`
  609. Comment string `json:"comment,omitempty"`
  610. }
  611. // EIGRPNetwork represents a network network command under EIGRP (/routing eigrp network)
  612. type EIGRPNetwork struct {
  613. ID string `json:"id"`
  614. Instance string `json:"instance,omitempty"`
  615. Network string `json:"network"` // CIDR, e.g. "192.168.131.0/24"
  616. Disabled bool `json:"disabled"`
  617. Comment string `json:"comment,omitempty"`
  618. }
  619. // EIGRPNeighborLive represents a live EIGRP neighbor reported by FRR (/routing eigrp neighbor)
  620. type EIGRPNeighborLive struct {
  621. AS int `json:"as"`
  622. Address string `json:"address"`
  623. Interface string `json:"interface"`
  624. HoldTime int `json:"hold_time"`
  625. Uptime string `json:"uptime"`
  626. SRTT int `json:"srtt"`
  627. RTO int `json:"rto"`
  628. SeqNum int `json:"seq_num"`
  629. }
  630. // EIGRPRouteLive represents a route in the EIGRP topology table (/routing eigrp topology)
  631. type EIGRPRouteLive struct {
  632. AS int `json:"as"`
  633. Prefix string `json:"prefix"`
  634. Successors int `json:"successors"`
  635. Distance int `json:"distance"` // Feasible Distance (FD)
  636. NextHop string `json:"next_hop"`
  637. Interface string `json:"interface"`
  638. State string `json:"state"` // "P" (Passive), "A" (Active)
  639. }
  640. // DynamicRoutingSummary aggregates status for dashboard & badges
  641. type DynamicRoutingSummary struct {
  642. BGPEnabled bool `json:"bgp_enabled"`
  643. BGPRunning bool `json:"bgp_running"`
  644. BGPPeerCount int `json:"bgp_peer_count"`
  645. BGPEstablishedCount int `json:"bgp_established_count"`
  646. OSPFEnabled bool `json:"ospf_enabled"`
  647. OSPFRunning bool `json:"ospf_running"`
  648. OSPFNeighborCount int `json:"ospf_neighbor_count"`
  649. OSPFAdjacentCount int `json:"ospf_adjacent_count"`
  650. RIPEnabled bool `json:"rip_enabled"`
  651. RIPRunning bool `json:"rip_running"`
  652. RIPNeighborCount int `json:"rip_neighbor_count"`
  653. BFDEnabled bool `json:"bfd_enabled"`
  654. BFDRunning bool `json:"bfd_running"`
  655. BFDSessionCount int `json:"bfd_session_count"`
  656. BabelEnabled bool `json:"babel_enabled"`
  657. BabelRunning bool `json:"babel_running"`
  658. BabelNeighborCount int `json:"babel_neighbor_count"`
  659. RAdvEnabled bool `json:"radv_enabled"`
  660. RAdvRunning bool `json:"radv_running"`
  661. EIGRPEnabled bool `json:"eigrp_enabled"`
  662. EIGRPRunning bool `json:"eigrp_running"`
  663. EIGRPNeighborCount int `json:"eigrp_neighbor_count"`
  664. RoutingFilterCount int `json:"routing_filter_count"`
  665. }
  666. // DynamicConfig stores all dynamic routing data on disk
  667. type DynamicConfig struct {
  668. BGPInstance BGPInstance `json:"bgp_instance"`
  669. BGPPeers []BGPPeer `json:"bgp_peers"`
  670. BGPNetworks []BGPNetwork `json:"bgp_networks"`
  671. OSPFInstances []OSPFInstance `json:"ospf_instances"`
  672. OSPFInstance OSPFInstance `json:"ospf_instance"` // Backward compatibility
  673. OSPFAreas []OSPFArea `json:"ospf_areas"`
  674. OSPFAreaRanges []OSPFAreaRange `json:"ospf_area_ranges"`
  675. OSPFInterfaces []OSPFInterface `json:"ospf_interfaces"`
  676. OSPFStaticNbrs []OSPFStaticNeighbor `json:"ospf_static_nbrs"`
  677. RIPInstances []RIPInstance `json:"rip_instances"`
  678. RIPInterfaces []RIPInterface `json:"rip_interfaces"`
  679. RIPNeighbors []RIPNeighbor `json:"rip_neighbors"`
  680. BFDInstance BFDInstance `json:"bfd_instance"`
  681. BFDInterfaces []BFDInterface `json:"bfd_interfaces"`
  682. BFDNeighbors []BFDNeighbor `json:"bfd_neighbors"`
  683. BabelInstances []BabelInstance `json:"babel_instances"`
  684. BabelInterfaces []BabelInterface `json:"babel_interfaces"`
  685. RAdvInstances []RAdvInstance `json:"radv_instances"`
  686. RAdvInterfaces []RAdvInterface `json:"radv_interfaces"`
  687. EIGRPInstances []EIGRPInstance `json:"eigrp_instances"`
  688. EIGRPInterfaces []EIGRPInterface `json:"eigrp_interfaces"`
  689. EIGRPNetworks []EIGRPNetwork `json:"eigrp_networks"`
  690. RoutingFilters []RoutingFilterRule `json:"routing_filters"`
  691. RoutingSelectRules []RoutingFilterSelectRule `json:"routing_select_rules,omitempty"` // Deprecated: unused in BIRD 2
  692. RoutingCommunityLists []RoutingFilterCommunityList `json:"routing_community_lists"`
  693. RoutingASLists []RoutingFilterASList `json:"routing_as_lists"`
  694. }