route_linux.go 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  1. package netlink
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "fmt"
  6. "net"
  7. "strconv"
  8. "strings"
  9. "syscall"
  10. "github.com/vishvananda/netlink/nl"
  11. "github.com/vishvananda/netns"
  12. "golang.org/x/sys/unix"
  13. )
  14. // RtAttr is shared so it is in netlink_linux.go
  15. const (
  16. SCOPE_UNIVERSE Scope = unix.RT_SCOPE_UNIVERSE
  17. SCOPE_SITE Scope = unix.RT_SCOPE_SITE
  18. SCOPE_LINK Scope = unix.RT_SCOPE_LINK
  19. SCOPE_HOST Scope = unix.RT_SCOPE_HOST
  20. SCOPE_NOWHERE Scope = unix.RT_SCOPE_NOWHERE
  21. )
  22. func (s Scope) String() string {
  23. switch s {
  24. case SCOPE_UNIVERSE:
  25. return "universe"
  26. case SCOPE_SITE:
  27. return "site"
  28. case SCOPE_LINK:
  29. return "link"
  30. case SCOPE_HOST:
  31. return "host"
  32. case SCOPE_NOWHERE:
  33. return "nowhere"
  34. default:
  35. return "unknown"
  36. }
  37. }
  38. const (
  39. RT_FILTER_PROTOCOL uint64 = 1 << (1 + iota)
  40. RT_FILTER_SCOPE
  41. RT_FILTER_TYPE
  42. RT_FILTER_TOS
  43. RT_FILTER_IIF
  44. RT_FILTER_OIF
  45. RT_FILTER_DST
  46. RT_FILTER_SRC
  47. RT_FILTER_GW
  48. RT_FILTER_TABLE
  49. RT_FILTER_HOPLIMIT
  50. RT_FILTER_PRIORITY
  51. RT_FILTER_MARK
  52. RT_FILTER_MASK
  53. )
  54. const (
  55. FLAG_ONLINK NextHopFlag = unix.RTNH_F_ONLINK
  56. FLAG_PERVASIVE NextHopFlag = unix.RTNH_F_PERVASIVE
  57. )
  58. var testFlags = []flagString{
  59. {f: FLAG_ONLINK, s: "onlink"},
  60. {f: FLAG_PERVASIVE, s: "pervasive"},
  61. }
  62. func listFlags(flag int) []string {
  63. var flags []string
  64. for _, tf := range testFlags {
  65. if flag&int(tf.f) != 0 {
  66. flags = append(flags, tf.s)
  67. }
  68. }
  69. return flags
  70. }
  71. func (r *Route) ListFlags() []string {
  72. return listFlags(r.Flags)
  73. }
  74. func (n *NexthopInfo) ListFlags() []string {
  75. return listFlags(n.Flags)
  76. }
  77. type MPLSDestination struct {
  78. Labels []int
  79. }
  80. func (d *MPLSDestination) Family() int {
  81. return nl.FAMILY_MPLS
  82. }
  83. func (d *MPLSDestination) Decode(buf []byte) error {
  84. d.Labels = nl.DecodeMPLSStack(buf)
  85. return nil
  86. }
  87. func (d *MPLSDestination) Encode() ([]byte, error) {
  88. return nl.EncodeMPLSStack(d.Labels...), nil
  89. }
  90. func (d *MPLSDestination) String() string {
  91. s := make([]string, 0, len(d.Labels))
  92. for _, l := range d.Labels {
  93. s = append(s, fmt.Sprintf("%d", l))
  94. }
  95. return strings.Join(s, "/")
  96. }
  97. func (d *MPLSDestination) Equal(x Destination) bool {
  98. o, ok := x.(*MPLSDestination)
  99. if !ok {
  100. return false
  101. }
  102. if d == nil && o == nil {
  103. return true
  104. }
  105. if d == nil || o == nil {
  106. return false
  107. }
  108. if d.Labels == nil && o.Labels == nil {
  109. return true
  110. }
  111. if d.Labels == nil || o.Labels == nil {
  112. return false
  113. }
  114. if len(d.Labels) != len(o.Labels) {
  115. return false
  116. }
  117. for i := range d.Labels {
  118. if d.Labels[i] != o.Labels[i] {
  119. return false
  120. }
  121. }
  122. return true
  123. }
  124. type MPLSEncap struct {
  125. Labels []int
  126. }
  127. func (e *MPLSEncap) Type() int {
  128. return nl.LWTUNNEL_ENCAP_MPLS
  129. }
  130. func (e *MPLSEncap) Decode(buf []byte) error {
  131. if len(buf) < 4 {
  132. return fmt.Errorf("lack of bytes")
  133. }
  134. native := nl.NativeEndian()
  135. l := native.Uint16(buf)
  136. if len(buf) < int(l) {
  137. return fmt.Errorf("lack of bytes")
  138. }
  139. buf = buf[:l]
  140. typ := native.Uint16(buf[2:])
  141. if typ != nl.MPLS_IPTUNNEL_DST {
  142. return fmt.Errorf("unknown MPLS Encap Type: %d", typ)
  143. }
  144. e.Labels = nl.DecodeMPLSStack(buf[4:])
  145. return nil
  146. }
  147. func (e *MPLSEncap) Encode() ([]byte, error) {
  148. s := nl.EncodeMPLSStack(e.Labels...)
  149. native := nl.NativeEndian()
  150. hdr := make([]byte, 4)
  151. native.PutUint16(hdr, uint16(len(s)+4))
  152. native.PutUint16(hdr[2:], nl.MPLS_IPTUNNEL_DST)
  153. return append(hdr, s...), nil
  154. }
  155. func (e *MPLSEncap) String() string {
  156. s := make([]string, 0, len(e.Labels))
  157. for _, l := range e.Labels {
  158. s = append(s, fmt.Sprintf("%d", l))
  159. }
  160. return strings.Join(s, "/")
  161. }
  162. func (e *MPLSEncap) Equal(x Encap) bool {
  163. o, ok := x.(*MPLSEncap)
  164. if !ok {
  165. return false
  166. }
  167. if e == nil && o == nil {
  168. return true
  169. }
  170. if e == nil || o == nil {
  171. return false
  172. }
  173. if e.Labels == nil && o.Labels == nil {
  174. return true
  175. }
  176. if e.Labels == nil || o.Labels == nil {
  177. return false
  178. }
  179. if len(e.Labels) != len(o.Labels) {
  180. return false
  181. }
  182. for i := range e.Labels {
  183. if e.Labels[i] != o.Labels[i] {
  184. return false
  185. }
  186. }
  187. return true
  188. }
  189. // SEG6 definitions
  190. type SEG6Encap struct {
  191. Mode int
  192. Segments []net.IP
  193. }
  194. func (e *SEG6Encap) Type() int {
  195. return nl.LWTUNNEL_ENCAP_SEG6
  196. }
  197. func (e *SEG6Encap) Decode(buf []byte) error {
  198. if len(buf) < 4 {
  199. return fmt.Errorf("lack of bytes")
  200. }
  201. native := nl.NativeEndian()
  202. // Get Length(l) & Type(typ) : 2 + 2 bytes
  203. l := native.Uint16(buf)
  204. if len(buf) < int(l) {
  205. return fmt.Errorf("lack of bytes")
  206. }
  207. buf = buf[:l] // make sure buf size upper limit is Length
  208. typ := native.Uint16(buf[2:])
  209. // LWTUNNEL_ENCAP_SEG6 has only one attr type SEG6_IPTUNNEL_SRH
  210. if typ != nl.SEG6_IPTUNNEL_SRH {
  211. return fmt.Errorf("unknown SEG6 Type: %d", typ)
  212. }
  213. var err error
  214. e.Mode, e.Segments, err = nl.DecodeSEG6Encap(buf[4:])
  215. return err
  216. }
  217. func (e *SEG6Encap) Encode() ([]byte, error) {
  218. s, err := nl.EncodeSEG6Encap(e.Mode, e.Segments)
  219. native := nl.NativeEndian()
  220. hdr := make([]byte, 4)
  221. native.PutUint16(hdr, uint16(len(s)+4))
  222. native.PutUint16(hdr[2:], nl.SEG6_IPTUNNEL_SRH)
  223. return append(hdr, s...), err
  224. }
  225. func (e *SEG6Encap) String() string {
  226. segs := make([]string, 0, len(e.Segments))
  227. // append segment backwards (from n to 0) since seg#0 is the last segment.
  228. for i := len(e.Segments); i > 0; i-- {
  229. segs = append(segs, fmt.Sprintf("%s", e.Segments[i-1]))
  230. }
  231. str := fmt.Sprintf("mode %s segs %d [ %s ]", nl.SEG6EncapModeString(e.Mode),
  232. len(e.Segments), strings.Join(segs, " "))
  233. return str
  234. }
  235. func (e *SEG6Encap) Equal(x Encap) bool {
  236. o, ok := x.(*SEG6Encap)
  237. if !ok {
  238. return false
  239. }
  240. if e == o {
  241. return true
  242. }
  243. if e == nil || o == nil {
  244. return false
  245. }
  246. if e.Mode != o.Mode {
  247. return false
  248. }
  249. if len(e.Segments) != len(o.Segments) {
  250. return false
  251. }
  252. for i := range e.Segments {
  253. if !e.Segments[i].Equal(o.Segments[i]) {
  254. return false
  255. }
  256. }
  257. return true
  258. }
  259. // SEG6LocalEncap definitions
  260. type SEG6LocalEncap struct {
  261. Flags [nl.SEG6_LOCAL_MAX]bool
  262. Action int
  263. Segments []net.IP // from SRH in seg6_local_lwt
  264. Table int // table id for End.T and End.DT6
  265. InAddr net.IP
  266. In6Addr net.IP
  267. Iif int
  268. Oif int
  269. }
  270. func (e *SEG6LocalEncap) Type() int {
  271. return nl.LWTUNNEL_ENCAP_SEG6_LOCAL
  272. }
  273. func (e *SEG6LocalEncap) Decode(buf []byte) error {
  274. attrs, err := nl.ParseRouteAttr(buf)
  275. if err != nil {
  276. return err
  277. }
  278. native := nl.NativeEndian()
  279. for _, attr := range attrs {
  280. switch attr.Attr.Type {
  281. case nl.SEG6_LOCAL_ACTION:
  282. e.Action = int(native.Uint32(attr.Value[0:4]))
  283. e.Flags[nl.SEG6_LOCAL_ACTION] = true
  284. case nl.SEG6_LOCAL_SRH:
  285. e.Segments, err = nl.DecodeSEG6Srh(attr.Value[:])
  286. e.Flags[nl.SEG6_LOCAL_SRH] = true
  287. case nl.SEG6_LOCAL_TABLE:
  288. e.Table = int(native.Uint32(attr.Value[0:4]))
  289. e.Flags[nl.SEG6_LOCAL_TABLE] = true
  290. case nl.SEG6_LOCAL_NH4:
  291. e.InAddr = net.IP(attr.Value[0:4])
  292. e.Flags[nl.SEG6_LOCAL_NH4] = true
  293. case nl.SEG6_LOCAL_NH6:
  294. e.In6Addr = net.IP(attr.Value[0:16])
  295. e.Flags[nl.SEG6_LOCAL_NH6] = true
  296. case nl.SEG6_LOCAL_IIF:
  297. e.Iif = int(native.Uint32(attr.Value[0:4]))
  298. e.Flags[nl.SEG6_LOCAL_IIF] = true
  299. case nl.SEG6_LOCAL_OIF:
  300. e.Oif = int(native.Uint32(attr.Value[0:4]))
  301. e.Flags[nl.SEG6_LOCAL_OIF] = true
  302. }
  303. }
  304. return err
  305. }
  306. func (e *SEG6LocalEncap) Encode() ([]byte, error) {
  307. var err error
  308. native := nl.NativeEndian()
  309. res := make([]byte, 8)
  310. native.PutUint16(res, 8) // length
  311. native.PutUint16(res[2:], nl.SEG6_LOCAL_ACTION)
  312. native.PutUint32(res[4:], uint32(e.Action))
  313. if e.Flags[nl.SEG6_LOCAL_SRH] {
  314. srh, err := nl.EncodeSEG6Srh(e.Segments)
  315. if err != nil {
  316. return nil, err
  317. }
  318. attr := make([]byte, 4)
  319. native.PutUint16(attr, uint16(len(srh)+4))
  320. native.PutUint16(attr[2:], nl.SEG6_LOCAL_SRH)
  321. attr = append(attr, srh...)
  322. res = append(res, attr...)
  323. }
  324. if e.Flags[nl.SEG6_LOCAL_TABLE] {
  325. attr := make([]byte, 8)
  326. native.PutUint16(attr, 8)
  327. native.PutUint16(attr[2:], nl.SEG6_LOCAL_TABLE)
  328. native.PutUint32(attr[4:], uint32(e.Table))
  329. res = append(res, attr...)
  330. }
  331. if e.Flags[nl.SEG6_LOCAL_NH4] {
  332. attr := make([]byte, 4)
  333. native.PutUint16(attr, 8)
  334. native.PutUint16(attr[2:], nl.SEG6_LOCAL_NH4)
  335. ipv4 := e.InAddr.To4()
  336. if ipv4 == nil {
  337. err = fmt.Errorf("SEG6_LOCAL_NH4 has invalid IPv4 address")
  338. return nil, err
  339. }
  340. attr = append(attr, ipv4...)
  341. res = append(res, attr...)
  342. }
  343. if e.Flags[nl.SEG6_LOCAL_NH6] {
  344. attr := make([]byte, 4)
  345. native.PutUint16(attr, 20)
  346. native.PutUint16(attr[2:], nl.SEG6_LOCAL_NH6)
  347. attr = append(attr, e.In6Addr...)
  348. res = append(res, attr...)
  349. }
  350. if e.Flags[nl.SEG6_LOCAL_IIF] {
  351. attr := make([]byte, 8)
  352. native.PutUint16(attr, 8)
  353. native.PutUint16(attr[2:], nl.SEG6_LOCAL_IIF)
  354. native.PutUint32(attr[4:], uint32(e.Iif))
  355. res = append(res, attr...)
  356. }
  357. if e.Flags[nl.SEG6_LOCAL_OIF] {
  358. attr := make([]byte, 8)
  359. native.PutUint16(attr, 8)
  360. native.PutUint16(attr[2:], nl.SEG6_LOCAL_OIF)
  361. native.PutUint32(attr[4:], uint32(e.Oif))
  362. res = append(res, attr...)
  363. }
  364. return res, err
  365. }
  366. func (e *SEG6LocalEncap) String() string {
  367. strs := make([]string, 0, nl.SEG6_LOCAL_MAX)
  368. strs = append(strs, fmt.Sprintf("action %s", nl.SEG6LocalActionString(e.Action)))
  369. if e.Flags[nl.SEG6_LOCAL_TABLE] {
  370. strs = append(strs, fmt.Sprintf("table %d", e.Table))
  371. }
  372. if e.Flags[nl.SEG6_LOCAL_NH4] {
  373. strs = append(strs, fmt.Sprintf("nh4 %s", e.InAddr))
  374. }
  375. if e.Flags[nl.SEG6_LOCAL_NH6] {
  376. strs = append(strs, fmt.Sprintf("nh6 %s", e.In6Addr))
  377. }
  378. if e.Flags[nl.SEG6_LOCAL_IIF] {
  379. link, err := LinkByIndex(e.Iif)
  380. if err != nil {
  381. strs = append(strs, fmt.Sprintf("iif %d", e.Iif))
  382. } else {
  383. strs = append(strs, fmt.Sprintf("iif %s", link.Attrs().Name))
  384. }
  385. }
  386. if e.Flags[nl.SEG6_LOCAL_OIF] {
  387. link, err := LinkByIndex(e.Oif)
  388. if err != nil {
  389. strs = append(strs, fmt.Sprintf("oif %d", e.Oif))
  390. } else {
  391. strs = append(strs, fmt.Sprintf("oif %s", link.Attrs().Name))
  392. }
  393. }
  394. if e.Flags[nl.SEG6_LOCAL_SRH] {
  395. segs := make([]string, 0, len(e.Segments))
  396. //append segment backwards (from n to 0) since seg#0 is the last segment.
  397. for i := len(e.Segments); i > 0; i-- {
  398. segs = append(segs, fmt.Sprintf("%s", e.Segments[i-1]))
  399. }
  400. strs = append(strs, fmt.Sprintf("segs %d [ %s ]", len(e.Segments), strings.Join(segs, " ")))
  401. }
  402. return strings.Join(strs, " ")
  403. }
  404. func (e *SEG6LocalEncap) Equal(x Encap) bool {
  405. o, ok := x.(*SEG6LocalEncap)
  406. if !ok {
  407. return false
  408. }
  409. if e == o {
  410. return true
  411. }
  412. if e == nil || o == nil {
  413. return false
  414. }
  415. // compare all arrays first
  416. for i := range e.Flags {
  417. if e.Flags[i] != o.Flags[i] {
  418. return false
  419. }
  420. }
  421. if len(e.Segments) != len(o.Segments) {
  422. return false
  423. }
  424. for i := range e.Segments {
  425. if !e.Segments[i].Equal(o.Segments[i]) {
  426. return false
  427. }
  428. }
  429. // compare values
  430. if !e.InAddr.Equal(o.InAddr) || !e.In6Addr.Equal(o.In6Addr) {
  431. return false
  432. }
  433. if e.Action != o.Action || e.Table != o.Table || e.Iif != o.Iif || e.Oif != o.Oif {
  434. return false
  435. }
  436. return true
  437. }
  438. type Via struct {
  439. AddrFamily int
  440. Addr net.IP
  441. }
  442. func (v *Via) Equal(x Destination) bool {
  443. o, ok := x.(*Via)
  444. if !ok {
  445. return false
  446. }
  447. if v.AddrFamily == x.Family() && v.Addr.Equal(o.Addr) {
  448. return true
  449. }
  450. return false
  451. }
  452. func (v *Via) String() string {
  453. return fmt.Sprintf("Family: %d, Address: %s", v.AddrFamily, v.Addr.String())
  454. }
  455. func (v *Via) Family() int {
  456. return v.AddrFamily
  457. }
  458. func (v *Via) Encode() ([]byte, error) {
  459. buf := &bytes.Buffer{}
  460. err := binary.Write(buf, native, uint16(v.AddrFamily))
  461. if err != nil {
  462. return nil, err
  463. }
  464. err = binary.Write(buf, native, v.Addr)
  465. if err != nil {
  466. return nil, err
  467. }
  468. return buf.Bytes(), nil
  469. }
  470. func (v *Via) Decode(b []byte) error {
  471. native := nl.NativeEndian()
  472. if len(b) < 6 {
  473. return fmt.Errorf("decoding failed: buffer too small (%d bytes)", len(b))
  474. }
  475. v.AddrFamily = int(native.Uint16(b[0:2]))
  476. if v.AddrFamily == nl.FAMILY_V4 {
  477. v.Addr = net.IP(b[2:6])
  478. return nil
  479. } else if v.AddrFamily == nl.FAMILY_V6 {
  480. if len(b) < 18 {
  481. return fmt.Errorf("decoding failed: buffer too small (%d bytes)", len(b))
  482. }
  483. v.Addr = net.IP(b[2:])
  484. return nil
  485. }
  486. return fmt.Errorf("decoding failed: address family %d unknown", v.AddrFamily)
  487. }
  488. // RouteAdd will add a route to the system.
  489. // Equivalent to: `ip route add $route`
  490. func RouteAdd(route *Route) error {
  491. return pkgHandle.RouteAdd(route)
  492. }
  493. // RouteAdd will add a route to the system.
  494. // Equivalent to: `ip route add $route`
  495. func (h *Handle) RouteAdd(route *Route) error {
  496. flags := unix.NLM_F_CREATE | unix.NLM_F_EXCL | unix.NLM_F_ACK
  497. req := h.newNetlinkRequest(unix.RTM_NEWROUTE, flags)
  498. return h.routeHandle(route, req, nl.NewRtMsg())
  499. }
  500. // RouteAppend will append a route to the system.
  501. // Equivalent to: `ip route append $route`
  502. func RouteAppend(route *Route) error {
  503. return pkgHandle.RouteAppend(route)
  504. }
  505. // RouteAppend will append a route to the system.
  506. // Equivalent to: `ip route append $route`
  507. func (h *Handle) RouteAppend(route *Route) error {
  508. flags := unix.NLM_F_CREATE | unix.NLM_F_APPEND | unix.NLM_F_ACK
  509. req := h.newNetlinkRequest(unix.RTM_NEWROUTE, flags)
  510. return h.routeHandle(route, req, nl.NewRtMsg())
  511. }
  512. // RouteAddEcmp will add a route to the system.
  513. func RouteAddEcmp(route *Route) error {
  514. return pkgHandle.RouteAddEcmp(route)
  515. }
  516. // RouteAddEcmp will add a route to the system.
  517. func (h *Handle) RouteAddEcmp(route *Route) error {
  518. flags := unix.NLM_F_CREATE | unix.NLM_F_ACK
  519. req := h.newNetlinkRequest(unix.RTM_NEWROUTE, flags)
  520. return h.routeHandle(route, req, nl.NewRtMsg())
  521. }
  522. // RouteReplace will add a route to the system.
  523. // Equivalent to: `ip route replace $route`
  524. func RouteReplace(route *Route) error {
  525. return pkgHandle.RouteReplace(route)
  526. }
  527. // RouteReplace will add a route to the system.
  528. // Equivalent to: `ip route replace $route`
  529. func (h *Handle) RouteReplace(route *Route) error {
  530. flags := unix.NLM_F_CREATE | unix.NLM_F_REPLACE | unix.NLM_F_ACK
  531. req := h.newNetlinkRequest(unix.RTM_NEWROUTE, flags)
  532. return h.routeHandle(route, req, nl.NewRtMsg())
  533. }
  534. // RouteDel will delete a route from the system.
  535. // Equivalent to: `ip route del $route`
  536. func RouteDel(route *Route) error {
  537. return pkgHandle.RouteDel(route)
  538. }
  539. // RouteDel will delete a route from the system.
  540. // Equivalent to: `ip route del $route`
  541. func (h *Handle) RouteDel(route *Route) error {
  542. req := h.newNetlinkRequest(unix.RTM_DELROUTE, unix.NLM_F_ACK)
  543. return h.routeHandle(route, req, nl.NewRtDelMsg())
  544. }
  545. func (h *Handle) routeHandle(route *Route, req *nl.NetlinkRequest, msg *nl.RtMsg) error {
  546. if (route.Dst == nil || route.Dst.IP == nil) && route.Src == nil && route.Gw == nil && route.MPLSDst == nil {
  547. return fmt.Errorf("one of Dst.IP, Src, or Gw must not be nil")
  548. }
  549. family := -1
  550. var rtAttrs []*nl.RtAttr
  551. if route.Dst != nil && route.Dst.IP != nil {
  552. dstLen, _ := route.Dst.Mask.Size()
  553. msg.Dst_len = uint8(dstLen)
  554. dstFamily := nl.GetIPFamily(route.Dst.IP)
  555. family = dstFamily
  556. var dstData []byte
  557. if dstFamily == FAMILY_V4 {
  558. dstData = route.Dst.IP.To4()
  559. } else {
  560. dstData = route.Dst.IP.To16()
  561. }
  562. rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_DST, dstData))
  563. } else if route.MPLSDst != nil {
  564. family = nl.FAMILY_MPLS
  565. msg.Dst_len = uint8(20)
  566. msg.Type = unix.RTN_UNICAST
  567. rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_DST, nl.EncodeMPLSStack(*route.MPLSDst)))
  568. }
  569. if route.NewDst != nil {
  570. if family != -1 && family != route.NewDst.Family() {
  571. return fmt.Errorf("new destination and destination are not the same address family")
  572. }
  573. buf, err := route.NewDst.Encode()
  574. if err != nil {
  575. return err
  576. }
  577. rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_NEWDST, buf))
  578. }
  579. if route.Encap != nil {
  580. buf := make([]byte, 2)
  581. native.PutUint16(buf, uint16(route.Encap.Type()))
  582. rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_ENCAP_TYPE, buf))
  583. buf, err := route.Encap.Encode()
  584. if err != nil {
  585. return err
  586. }
  587. rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_ENCAP, buf))
  588. }
  589. if route.Src != nil {
  590. srcFamily := nl.GetIPFamily(route.Src)
  591. if family != -1 && family != srcFamily {
  592. return fmt.Errorf("source and destination ip are not the same IP family")
  593. }
  594. family = srcFamily
  595. var srcData []byte
  596. if srcFamily == FAMILY_V4 {
  597. srcData = route.Src.To4()
  598. } else {
  599. srcData = route.Src.To16()
  600. }
  601. // The commonly used src ip for routes is actually PREFSRC
  602. rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_PREFSRC, srcData))
  603. }
  604. if route.Gw != nil {
  605. gwFamily := nl.GetIPFamily(route.Gw)
  606. if family != -1 && family != gwFamily {
  607. return fmt.Errorf("gateway, source, and destination ip are not the same IP family")
  608. }
  609. family = gwFamily
  610. var gwData []byte
  611. if gwFamily == FAMILY_V4 {
  612. gwData = route.Gw.To4()
  613. } else {
  614. gwData = route.Gw.To16()
  615. }
  616. rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_GATEWAY, gwData))
  617. }
  618. if route.Via != nil {
  619. buf, err := route.Via.Encode()
  620. if err != nil {
  621. return fmt.Errorf("failed to encode RTA_VIA: %v", err)
  622. }
  623. rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_VIA, buf))
  624. }
  625. if len(route.MultiPath) > 0 {
  626. buf := []byte{}
  627. for _, nh := range route.MultiPath {
  628. rtnh := &nl.RtNexthop{
  629. RtNexthop: unix.RtNexthop{
  630. Hops: uint8(nh.Hops),
  631. Ifindex: int32(nh.LinkIndex),
  632. Flags: uint8(nh.Flags),
  633. },
  634. }
  635. children := []nl.NetlinkRequestData{}
  636. if nh.Gw != nil {
  637. gwFamily := nl.GetIPFamily(nh.Gw)
  638. if family != -1 && family != gwFamily {
  639. return fmt.Errorf("gateway, source, and destination ip are not the same IP family")
  640. }
  641. if gwFamily == FAMILY_V4 {
  642. children = append(children, nl.NewRtAttr(unix.RTA_GATEWAY, []byte(nh.Gw.To4())))
  643. } else {
  644. children = append(children, nl.NewRtAttr(unix.RTA_GATEWAY, []byte(nh.Gw.To16())))
  645. }
  646. }
  647. if nh.NewDst != nil {
  648. if family != -1 && family != nh.NewDst.Family() {
  649. return fmt.Errorf("new destination and destination are not the same address family")
  650. }
  651. buf, err := nh.NewDst.Encode()
  652. if err != nil {
  653. return err
  654. }
  655. children = append(children, nl.NewRtAttr(unix.RTA_NEWDST, buf))
  656. }
  657. if nh.Encap != nil {
  658. buf := make([]byte, 2)
  659. native.PutUint16(buf, uint16(nh.Encap.Type()))
  660. children = append(children, nl.NewRtAttr(unix.RTA_ENCAP_TYPE, buf))
  661. buf, err := nh.Encap.Encode()
  662. if err != nil {
  663. return err
  664. }
  665. children = append(children, nl.NewRtAttr(unix.RTA_ENCAP, buf))
  666. }
  667. if nh.Via != nil {
  668. buf, err := nh.Via.Encode()
  669. if err != nil {
  670. return err
  671. }
  672. children = append(children, nl.NewRtAttr(unix.RTA_VIA, buf))
  673. }
  674. rtnh.Children = children
  675. buf = append(buf, rtnh.Serialize()...)
  676. }
  677. rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_MULTIPATH, buf))
  678. }
  679. if route.Table > 0 {
  680. if route.Table >= 256 {
  681. msg.Table = unix.RT_TABLE_UNSPEC
  682. b := make([]byte, 4)
  683. native.PutUint32(b, uint32(route.Table))
  684. rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_TABLE, b))
  685. } else {
  686. msg.Table = uint8(route.Table)
  687. }
  688. }
  689. if route.Priority > 0 {
  690. b := make([]byte, 4)
  691. native.PutUint32(b, uint32(route.Priority))
  692. rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_PRIORITY, b))
  693. }
  694. if route.Tos > 0 {
  695. msg.Tos = uint8(route.Tos)
  696. }
  697. if route.Protocol > 0 {
  698. msg.Protocol = uint8(route.Protocol)
  699. }
  700. if route.Type > 0 {
  701. msg.Type = uint8(route.Type)
  702. }
  703. var metrics []*nl.RtAttr
  704. if route.MTU > 0 {
  705. b := nl.Uint32Attr(uint32(route.MTU))
  706. metrics = append(metrics, nl.NewRtAttr(unix.RTAX_MTU, b))
  707. }
  708. if route.Window > 0 {
  709. b := nl.Uint32Attr(uint32(route.Window))
  710. metrics = append(metrics, nl.NewRtAttr(unix.RTAX_WINDOW, b))
  711. }
  712. if route.Rtt > 0 {
  713. b := nl.Uint32Attr(uint32(route.Rtt))
  714. metrics = append(metrics, nl.NewRtAttr(unix.RTAX_RTT, b))
  715. }
  716. if route.RttVar > 0 {
  717. b := nl.Uint32Attr(uint32(route.RttVar))
  718. metrics = append(metrics, nl.NewRtAttr(unix.RTAX_RTTVAR, b))
  719. }
  720. if route.Ssthresh > 0 {
  721. b := nl.Uint32Attr(uint32(route.Ssthresh))
  722. metrics = append(metrics, nl.NewRtAttr(unix.RTAX_SSTHRESH, b))
  723. }
  724. if route.Cwnd > 0 {
  725. b := nl.Uint32Attr(uint32(route.Cwnd))
  726. metrics = append(metrics, nl.NewRtAttr(unix.RTAX_CWND, b))
  727. }
  728. if route.AdvMSS > 0 {
  729. b := nl.Uint32Attr(uint32(route.AdvMSS))
  730. metrics = append(metrics, nl.NewRtAttr(unix.RTAX_ADVMSS, b))
  731. }
  732. if route.Reordering > 0 {
  733. b := nl.Uint32Attr(uint32(route.Reordering))
  734. metrics = append(metrics, nl.NewRtAttr(unix.RTAX_REORDERING, b))
  735. }
  736. if route.Hoplimit > 0 {
  737. b := nl.Uint32Attr(uint32(route.Hoplimit))
  738. metrics = append(metrics, nl.NewRtAttr(unix.RTAX_HOPLIMIT, b))
  739. }
  740. if route.InitCwnd > 0 {
  741. b := nl.Uint32Attr(uint32(route.InitCwnd))
  742. metrics = append(metrics, nl.NewRtAttr(unix.RTAX_INITCWND, b))
  743. }
  744. if route.Features > 0 {
  745. b := nl.Uint32Attr(uint32(route.Features))
  746. metrics = append(metrics, nl.NewRtAttr(unix.RTAX_FEATURES, b))
  747. }
  748. if route.RtoMin > 0 {
  749. b := nl.Uint32Attr(uint32(route.RtoMin))
  750. metrics = append(metrics, nl.NewRtAttr(unix.RTAX_RTO_MIN, b))
  751. }
  752. if route.InitRwnd > 0 {
  753. b := nl.Uint32Attr(uint32(route.InitRwnd))
  754. metrics = append(metrics, nl.NewRtAttr(unix.RTAX_INITRWND, b))
  755. }
  756. if route.QuickACK > 0 {
  757. b := nl.Uint32Attr(uint32(route.QuickACK))
  758. metrics = append(metrics, nl.NewRtAttr(unix.RTAX_QUICKACK, b))
  759. }
  760. if route.Congctl != "" {
  761. b := nl.ZeroTerminated(route.Congctl)
  762. metrics = append(metrics, nl.NewRtAttr(unix.RTAX_CC_ALGO, b))
  763. }
  764. if route.FastOpenNoCookie > 0 {
  765. b := nl.Uint32Attr(uint32(route.FastOpenNoCookie))
  766. metrics = append(metrics, nl.NewRtAttr(unix.RTAX_FASTOPEN_NO_COOKIE, b))
  767. }
  768. if metrics != nil {
  769. attr := nl.NewRtAttr(unix.RTA_METRICS, nil)
  770. for _, metric := range metrics {
  771. attr.AddChild(metric)
  772. }
  773. rtAttrs = append(rtAttrs, attr)
  774. }
  775. msg.Flags = uint32(route.Flags)
  776. msg.Scope = uint8(route.Scope)
  777. msg.Family = uint8(family)
  778. req.AddData(msg)
  779. for _, attr := range rtAttrs {
  780. req.AddData(attr)
  781. }
  782. var (
  783. b = make([]byte, 4)
  784. native = nl.NativeEndian()
  785. )
  786. native.PutUint32(b, uint32(route.LinkIndex))
  787. req.AddData(nl.NewRtAttr(unix.RTA_OIF, b))
  788. _, err := req.Execute(unix.NETLINK_ROUTE, 0)
  789. return err
  790. }
  791. // RouteList gets a list of routes in the system.
  792. // Equivalent to: `ip route show`.
  793. // The list can be filtered by link and ip family.
  794. func RouteList(link Link, family int) ([]Route, error) {
  795. return pkgHandle.RouteList(link, family)
  796. }
  797. // RouteList gets a list of routes in the system.
  798. // Equivalent to: `ip route show`.
  799. // The list can be filtered by link and ip family.
  800. func (h *Handle) RouteList(link Link, family int) ([]Route, error) {
  801. var routeFilter *Route
  802. if link != nil {
  803. routeFilter = &Route{
  804. LinkIndex: link.Attrs().Index,
  805. }
  806. }
  807. return h.RouteListFiltered(family, routeFilter, RT_FILTER_OIF)
  808. }
  809. // RouteListFiltered gets a list of routes in the system filtered with specified rules.
  810. // All rules must be defined in RouteFilter struct
  811. func RouteListFiltered(family int, filter *Route, filterMask uint64) ([]Route, error) {
  812. return pkgHandle.RouteListFiltered(family, filter, filterMask)
  813. }
  814. // RouteListFiltered gets a list of routes in the system filtered with specified rules.
  815. // All rules must be defined in RouteFilter struct
  816. func (h *Handle) RouteListFiltered(family int, filter *Route, filterMask uint64) ([]Route, error) {
  817. req := h.newNetlinkRequest(unix.RTM_GETROUTE, unix.NLM_F_DUMP)
  818. infmsg := nl.NewIfInfomsg(family)
  819. req.AddData(infmsg)
  820. msgs, err := req.Execute(unix.NETLINK_ROUTE, unix.RTM_NEWROUTE)
  821. if err != nil {
  822. return nil, err
  823. }
  824. var res []Route
  825. for _, m := range msgs {
  826. msg := nl.DeserializeRtMsg(m)
  827. if msg.Flags&unix.RTM_F_CLONED != 0 {
  828. // Ignore cloned routes
  829. continue
  830. }
  831. if msg.Table != unix.RT_TABLE_MAIN {
  832. if filter == nil || filter != nil && filterMask&RT_FILTER_TABLE == 0 {
  833. // Ignore non-main tables
  834. continue
  835. }
  836. }
  837. route, err := deserializeRoute(m)
  838. if err != nil {
  839. return nil, err
  840. }
  841. if filter != nil {
  842. switch {
  843. case filterMask&RT_FILTER_TABLE != 0 && filter.Table != unix.RT_TABLE_UNSPEC && route.Table != filter.Table:
  844. continue
  845. case filterMask&RT_FILTER_PROTOCOL != 0 && route.Protocol != filter.Protocol:
  846. continue
  847. case filterMask&RT_FILTER_SCOPE != 0 && route.Scope != filter.Scope:
  848. continue
  849. case filterMask&RT_FILTER_TYPE != 0 && route.Type != filter.Type:
  850. continue
  851. case filterMask&RT_FILTER_TOS != 0 && route.Tos != filter.Tos:
  852. continue
  853. case filterMask&RT_FILTER_OIF != 0 && route.LinkIndex != filter.LinkIndex:
  854. continue
  855. case filterMask&RT_FILTER_IIF != 0 && route.ILinkIndex != filter.ILinkIndex:
  856. continue
  857. case filterMask&RT_FILTER_GW != 0 && !route.Gw.Equal(filter.Gw):
  858. continue
  859. case filterMask&RT_FILTER_SRC != 0 && !route.Src.Equal(filter.Src):
  860. continue
  861. case filterMask&RT_FILTER_DST != 0:
  862. if filter.MPLSDst == nil || route.MPLSDst == nil || (*filter.MPLSDst) != (*route.MPLSDst) {
  863. if !ipNetEqual(route.Dst, filter.Dst) {
  864. continue
  865. }
  866. }
  867. case filterMask&RT_FILTER_HOPLIMIT != 0 && route.Hoplimit != filter.Hoplimit:
  868. continue
  869. }
  870. }
  871. res = append(res, route)
  872. }
  873. return res, nil
  874. }
  875. // deserializeRoute decodes a binary netlink message into a Route struct
  876. func deserializeRoute(m []byte) (Route, error) {
  877. msg := nl.DeserializeRtMsg(m)
  878. attrs, err := nl.ParseRouteAttr(m[msg.Len():])
  879. if err != nil {
  880. return Route{}, err
  881. }
  882. route := Route{
  883. Scope: Scope(msg.Scope),
  884. Protocol: RouteProtocol(int(msg.Protocol)),
  885. Table: int(msg.Table),
  886. Type: int(msg.Type),
  887. Tos: int(msg.Tos),
  888. Flags: int(msg.Flags),
  889. }
  890. native := nl.NativeEndian()
  891. var encap, encapType syscall.NetlinkRouteAttr
  892. for _, attr := range attrs {
  893. switch attr.Attr.Type {
  894. case unix.RTA_GATEWAY:
  895. route.Gw = net.IP(attr.Value)
  896. case unix.RTA_PREFSRC:
  897. route.Src = net.IP(attr.Value)
  898. case unix.RTA_DST:
  899. if msg.Family == nl.FAMILY_MPLS {
  900. stack := nl.DecodeMPLSStack(attr.Value)
  901. if len(stack) == 0 || len(stack) > 1 {
  902. return route, fmt.Errorf("invalid MPLS RTA_DST")
  903. }
  904. route.MPLSDst = &stack[0]
  905. } else {
  906. route.Dst = &net.IPNet{
  907. IP: attr.Value,
  908. Mask: net.CIDRMask(int(msg.Dst_len), 8*len(attr.Value)),
  909. }
  910. }
  911. case unix.RTA_OIF:
  912. route.LinkIndex = int(native.Uint32(attr.Value[0:4]))
  913. case unix.RTA_IIF:
  914. route.ILinkIndex = int(native.Uint32(attr.Value[0:4]))
  915. case unix.RTA_PRIORITY:
  916. route.Priority = int(native.Uint32(attr.Value[0:4]))
  917. case unix.RTA_TABLE:
  918. route.Table = int(native.Uint32(attr.Value[0:4]))
  919. case unix.RTA_MULTIPATH:
  920. parseRtNexthop := func(value []byte) (*NexthopInfo, []byte, error) {
  921. if len(value) < unix.SizeofRtNexthop {
  922. return nil, nil, fmt.Errorf("lack of bytes")
  923. }
  924. nh := nl.DeserializeRtNexthop(value)
  925. if len(value) < int(nh.RtNexthop.Len) {
  926. return nil, nil, fmt.Errorf("lack of bytes")
  927. }
  928. info := &NexthopInfo{
  929. LinkIndex: int(nh.RtNexthop.Ifindex),
  930. Hops: int(nh.RtNexthop.Hops),
  931. Flags: int(nh.RtNexthop.Flags),
  932. }
  933. attrs, err := nl.ParseRouteAttr(value[unix.SizeofRtNexthop:int(nh.RtNexthop.Len)])
  934. if err != nil {
  935. return nil, nil, err
  936. }
  937. var encap, encapType syscall.NetlinkRouteAttr
  938. for _, attr := range attrs {
  939. switch attr.Attr.Type {
  940. case unix.RTA_GATEWAY:
  941. info.Gw = net.IP(attr.Value)
  942. case unix.RTA_NEWDST:
  943. var d Destination
  944. switch msg.Family {
  945. case nl.FAMILY_MPLS:
  946. d = &MPLSDestination{}
  947. }
  948. if err := d.Decode(attr.Value); err != nil {
  949. return nil, nil, err
  950. }
  951. info.NewDst = d
  952. case unix.RTA_ENCAP_TYPE:
  953. encapType = attr
  954. case unix.RTA_ENCAP:
  955. encap = attr
  956. case unix.RTA_VIA:
  957. d := &Via{}
  958. if err := d.Decode(attr.Value); err != nil {
  959. return nil, nil, err
  960. }
  961. info.Via = d
  962. }
  963. }
  964. if len(encap.Value) != 0 && len(encapType.Value) != 0 {
  965. typ := int(native.Uint16(encapType.Value[0:2]))
  966. var e Encap
  967. switch typ {
  968. case nl.LWTUNNEL_ENCAP_MPLS:
  969. e = &MPLSEncap{}
  970. if err := e.Decode(encap.Value); err != nil {
  971. return nil, nil, err
  972. }
  973. }
  974. info.Encap = e
  975. }
  976. return info, value[int(nh.RtNexthop.Len):], nil
  977. }
  978. rest := attr.Value
  979. for len(rest) > 0 {
  980. info, buf, err := parseRtNexthop(rest)
  981. if err != nil {
  982. return route, err
  983. }
  984. route.MultiPath = append(route.MultiPath, info)
  985. rest = buf
  986. }
  987. case unix.RTA_NEWDST:
  988. var d Destination
  989. switch msg.Family {
  990. case nl.FAMILY_MPLS:
  991. d = &MPLSDestination{}
  992. }
  993. if err := d.Decode(attr.Value); err != nil {
  994. return route, err
  995. }
  996. route.NewDst = d
  997. case unix.RTA_VIA:
  998. v := &Via{}
  999. if err := v.Decode(attr.Value); err != nil {
  1000. return route, err
  1001. }
  1002. route.Via = v
  1003. case unix.RTA_ENCAP_TYPE:
  1004. encapType = attr
  1005. case unix.RTA_ENCAP:
  1006. encap = attr
  1007. case unix.RTA_METRICS:
  1008. metrics, err := nl.ParseRouteAttr(attr.Value)
  1009. if err != nil {
  1010. return route, err
  1011. }
  1012. for _, metric := range metrics {
  1013. switch metric.Attr.Type {
  1014. case unix.RTAX_MTU:
  1015. route.MTU = int(native.Uint32(metric.Value[0:4]))
  1016. case unix.RTAX_WINDOW:
  1017. route.Window = int(native.Uint32(metric.Value[0:4]))
  1018. case unix.RTAX_RTT:
  1019. route.Rtt = int(native.Uint32(metric.Value[0:4]))
  1020. case unix.RTAX_RTTVAR:
  1021. route.RttVar = int(native.Uint32(metric.Value[0:4]))
  1022. case unix.RTAX_SSTHRESH:
  1023. route.Ssthresh = int(native.Uint32(metric.Value[0:4]))
  1024. case unix.RTAX_CWND:
  1025. route.Cwnd = int(native.Uint32(metric.Value[0:4]))
  1026. case unix.RTAX_ADVMSS:
  1027. route.AdvMSS = int(native.Uint32(metric.Value[0:4]))
  1028. case unix.RTAX_REORDERING:
  1029. route.Reordering = int(native.Uint32(metric.Value[0:4]))
  1030. case unix.RTAX_HOPLIMIT:
  1031. route.Hoplimit = int(native.Uint32(metric.Value[0:4]))
  1032. case unix.RTAX_INITCWND:
  1033. route.InitCwnd = int(native.Uint32(metric.Value[0:4]))
  1034. case unix.RTAX_FEATURES:
  1035. route.Features = int(native.Uint32(metric.Value[0:4]))
  1036. case unix.RTAX_RTO_MIN:
  1037. route.RtoMin = int(native.Uint32(metric.Value[0:4]))
  1038. case unix.RTAX_INITRWND:
  1039. route.InitRwnd = int(native.Uint32(metric.Value[0:4]))
  1040. case unix.RTAX_QUICKACK:
  1041. route.QuickACK = int(native.Uint32(metric.Value[0:4]))
  1042. case unix.RTAX_CC_ALGO:
  1043. route.Congctl = nl.BytesToString(metric.Value)
  1044. case unix.RTAX_FASTOPEN_NO_COOKIE:
  1045. route.FastOpenNoCookie = int(native.Uint32(metric.Value[0:4]))
  1046. }
  1047. }
  1048. }
  1049. }
  1050. if len(encap.Value) != 0 && len(encapType.Value) != 0 {
  1051. typ := int(native.Uint16(encapType.Value[0:2]))
  1052. var e Encap
  1053. switch typ {
  1054. case nl.LWTUNNEL_ENCAP_MPLS:
  1055. e = &MPLSEncap{}
  1056. if err := e.Decode(encap.Value); err != nil {
  1057. return route, err
  1058. }
  1059. case nl.LWTUNNEL_ENCAP_SEG6:
  1060. e = &SEG6Encap{}
  1061. if err := e.Decode(encap.Value); err != nil {
  1062. return route, err
  1063. }
  1064. case nl.LWTUNNEL_ENCAP_SEG6_LOCAL:
  1065. e = &SEG6LocalEncap{}
  1066. if err := e.Decode(encap.Value); err != nil {
  1067. return route, err
  1068. }
  1069. }
  1070. route.Encap = e
  1071. }
  1072. return route, nil
  1073. }
  1074. // RouteGetOptions contains a set of options to use with
  1075. // RouteGetWithOptions
  1076. type RouteGetOptions struct {
  1077. VrfName string
  1078. SrcAddr net.IP
  1079. }
  1080. // RouteGetWithOptions gets a route to a specific destination from the host system.
  1081. // Equivalent to: 'ip route get <> vrf <VrfName>'.
  1082. func RouteGetWithOptions(destination net.IP, options *RouteGetOptions) ([]Route, error) {
  1083. return pkgHandle.RouteGetWithOptions(destination, options)
  1084. }
  1085. // RouteGet gets a route to a specific destination from the host system.
  1086. // Equivalent to: 'ip route get'.
  1087. func RouteGet(destination net.IP) ([]Route, error) {
  1088. return pkgHandle.RouteGet(destination)
  1089. }
  1090. // RouteGetWithOptions gets a route to a specific destination from the host system.
  1091. // Equivalent to: 'ip route get <> vrf <VrfName>'.
  1092. func (h *Handle) RouteGetWithOptions(destination net.IP, options *RouteGetOptions) ([]Route, error) {
  1093. req := h.newNetlinkRequest(unix.RTM_GETROUTE, unix.NLM_F_REQUEST)
  1094. family := nl.GetIPFamily(destination)
  1095. var destinationData []byte
  1096. var bitlen uint8
  1097. if family == FAMILY_V4 {
  1098. destinationData = destination.To4()
  1099. bitlen = 32
  1100. } else {
  1101. destinationData = destination.To16()
  1102. bitlen = 128
  1103. }
  1104. msg := &nl.RtMsg{}
  1105. msg.Family = uint8(family)
  1106. msg.Dst_len = bitlen
  1107. if options != nil && options.SrcAddr != nil {
  1108. msg.Src_len = bitlen
  1109. }
  1110. msg.Flags = unix.RTM_F_LOOKUP_TABLE
  1111. req.AddData(msg)
  1112. rtaDst := nl.NewRtAttr(unix.RTA_DST, destinationData)
  1113. req.AddData(rtaDst)
  1114. if options != nil {
  1115. if options.VrfName != "" {
  1116. link, err := LinkByName(options.VrfName)
  1117. if err != nil {
  1118. return nil, err
  1119. }
  1120. var (
  1121. b = make([]byte, 4)
  1122. native = nl.NativeEndian()
  1123. )
  1124. native.PutUint32(b, uint32(link.Attrs().Index))
  1125. req.AddData(nl.NewRtAttr(unix.RTA_OIF, b))
  1126. }
  1127. if options.SrcAddr != nil {
  1128. var srcAddr []byte
  1129. if family == FAMILY_V4 {
  1130. srcAddr = options.SrcAddr.To4()
  1131. } else {
  1132. srcAddr = options.SrcAddr.To16()
  1133. }
  1134. req.AddData(nl.NewRtAttr(unix.RTA_SRC, srcAddr))
  1135. }
  1136. }
  1137. msgs, err := req.Execute(unix.NETLINK_ROUTE, unix.RTM_NEWROUTE)
  1138. if err != nil {
  1139. return nil, err
  1140. }
  1141. var res []Route
  1142. for _, m := range msgs {
  1143. route, err := deserializeRoute(m)
  1144. if err != nil {
  1145. return nil, err
  1146. }
  1147. res = append(res, route)
  1148. }
  1149. return res, nil
  1150. }
  1151. // RouteGet gets a route to a specific destination from the host system.
  1152. // Equivalent to: 'ip route get'.
  1153. func (h *Handle) RouteGet(destination net.IP) ([]Route, error) {
  1154. return h.RouteGetWithOptions(destination, nil)
  1155. }
  1156. // RouteSubscribe takes a chan down which notifications will be sent
  1157. // when routes are added or deleted. Close the 'done' chan to stop subscription.
  1158. func RouteSubscribe(ch chan<- RouteUpdate, done <-chan struct{}) error {
  1159. return routeSubscribeAt(netns.None(), netns.None(), ch, done, nil, false)
  1160. }
  1161. // RouteSubscribeAt works like RouteSubscribe plus it allows the caller
  1162. // to choose the network namespace in which to subscribe (ns).
  1163. func RouteSubscribeAt(ns netns.NsHandle, ch chan<- RouteUpdate, done <-chan struct{}) error {
  1164. return routeSubscribeAt(ns, netns.None(), ch, done, nil, false)
  1165. }
  1166. // RouteSubscribeOptions contains a set of options to use with
  1167. // RouteSubscribeWithOptions.
  1168. type RouteSubscribeOptions struct {
  1169. Namespace *netns.NsHandle
  1170. ErrorCallback func(error)
  1171. ListExisting bool
  1172. }
  1173. // RouteSubscribeWithOptions work like RouteSubscribe but enable to
  1174. // provide additional options to modify the behavior. Currently, the
  1175. // namespace can be provided as well as an error callback.
  1176. func RouteSubscribeWithOptions(ch chan<- RouteUpdate, done <-chan struct{}, options RouteSubscribeOptions) error {
  1177. if options.Namespace == nil {
  1178. none := netns.None()
  1179. options.Namespace = &none
  1180. }
  1181. return routeSubscribeAt(*options.Namespace, netns.None(), ch, done, options.ErrorCallback, options.ListExisting)
  1182. }
  1183. func routeSubscribeAt(newNs, curNs netns.NsHandle, ch chan<- RouteUpdate, done <-chan struct{}, cberr func(error), listExisting bool) error {
  1184. s, err := nl.SubscribeAt(newNs, curNs, unix.NETLINK_ROUTE, unix.RTNLGRP_IPV4_ROUTE, unix.RTNLGRP_IPV6_ROUTE)
  1185. if err != nil {
  1186. return err
  1187. }
  1188. if done != nil {
  1189. go func() {
  1190. <-done
  1191. s.Close()
  1192. }()
  1193. }
  1194. if listExisting {
  1195. req := pkgHandle.newNetlinkRequest(unix.RTM_GETROUTE,
  1196. unix.NLM_F_DUMP)
  1197. infmsg := nl.NewIfInfomsg(unix.AF_UNSPEC)
  1198. req.AddData(infmsg)
  1199. if err := s.Send(req); err != nil {
  1200. return err
  1201. }
  1202. }
  1203. go func() {
  1204. defer close(ch)
  1205. for {
  1206. msgs, from, err := s.Receive()
  1207. if err != nil {
  1208. if cberr != nil {
  1209. cberr(err)
  1210. }
  1211. return
  1212. }
  1213. if from.Pid != nl.PidKernel {
  1214. if cberr != nil {
  1215. cberr(fmt.Errorf("Wrong sender portid %d, expected %d", from.Pid, nl.PidKernel))
  1216. }
  1217. continue
  1218. }
  1219. for _, m := range msgs {
  1220. if m.Header.Type == unix.NLMSG_DONE {
  1221. continue
  1222. }
  1223. if m.Header.Type == unix.NLMSG_ERROR {
  1224. native := nl.NativeEndian()
  1225. error := int32(native.Uint32(m.Data[0:4]))
  1226. if error == 0 {
  1227. continue
  1228. }
  1229. if cberr != nil {
  1230. cberr(syscall.Errno(-error))
  1231. }
  1232. return
  1233. }
  1234. route, err := deserializeRoute(m.Data)
  1235. if err != nil {
  1236. if cberr != nil {
  1237. cberr(err)
  1238. }
  1239. return
  1240. }
  1241. ch <- RouteUpdate{Type: m.Header.Type, Route: route}
  1242. }
  1243. }
  1244. }()
  1245. return nil
  1246. }
  1247. func (p RouteProtocol) String() string {
  1248. switch int(p) {
  1249. case unix.RTPROT_BABEL:
  1250. return "babel"
  1251. case unix.RTPROT_BGP:
  1252. return "bgp"
  1253. case unix.RTPROT_BIRD:
  1254. return "bird"
  1255. case unix.RTPROT_BOOT:
  1256. return "boot"
  1257. case unix.RTPROT_DHCP:
  1258. return "dhcp"
  1259. case unix.RTPROT_DNROUTED:
  1260. return "dnrouted"
  1261. case unix.RTPROT_EIGRP:
  1262. return "eigrp"
  1263. case unix.RTPROT_GATED:
  1264. return "gated"
  1265. case unix.RTPROT_ISIS:
  1266. return "isis"
  1267. //case unix.RTPROT_KEEPALIVED:
  1268. // return "keepalived"
  1269. case unix.RTPROT_KERNEL:
  1270. return "kernel"
  1271. case unix.RTPROT_MROUTED:
  1272. return "mrouted"
  1273. case unix.RTPROT_MRT:
  1274. return "mrt"
  1275. case unix.RTPROT_NTK:
  1276. return "ntk"
  1277. case unix.RTPROT_OSPF:
  1278. return "ospf"
  1279. case unix.RTPROT_RA:
  1280. return "ra"
  1281. case unix.RTPROT_REDIRECT:
  1282. return "redirect"
  1283. case unix.RTPROT_RIP:
  1284. return "rip"
  1285. case unix.RTPROT_STATIC:
  1286. return "static"
  1287. case unix.RTPROT_UNSPEC:
  1288. return "unspec"
  1289. case unix.RTPROT_XORP:
  1290. return "xorp"
  1291. case unix.RTPROT_ZEBRA:
  1292. return "zebra"
  1293. default:
  1294. return strconv.Itoa(int(p))
  1295. }
  1296. }