types.go 33 KB

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