route_linux.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. package netlink
  2. import (
  3. "fmt"
  4. "net"
  5. "strings"
  6. "syscall"
  7. "github.com/vishvananda/netlink/nl"
  8. "github.com/vishvananda/netns"
  9. )
  10. // RtAttr is shared so it is in netlink_linux.go
  11. const (
  12. SCOPE_UNIVERSE Scope = syscall.RT_SCOPE_UNIVERSE
  13. SCOPE_SITE Scope = syscall.RT_SCOPE_SITE
  14. SCOPE_LINK Scope = syscall.RT_SCOPE_LINK
  15. SCOPE_HOST Scope = syscall.RT_SCOPE_HOST
  16. SCOPE_NOWHERE Scope = syscall.RT_SCOPE_NOWHERE
  17. )
  18. const (
  19. RT_FILTER_PROTOCOL uint64 = 1 << (1 + iota)
  20. RT_FILTER_SCOPE
  21. RT_FILTER_TYPE
  22. RT_FILTER_TOS
  23. RT_FILTER_IIF
  24. RT_FILTER_OIF
  25. RT_FILTER_DST
  26. RT_FILTER_SRC
  27. RT_FILTER_GW
  28. RT_FILTER_TABLE
  29. )
  30. const (
  31. FLAG_ONLINK NextHopFlag = syscall.RTNH_F_ONLINK
  32. FLAG_PERVASIVE NextHopFlag = syscall.RTNH_F_PERVASIVE
  33. )
  34. var testFlags = []flagString{
  35. {f: FLAG_ONLINK, s: "onlink"},
  36. {f: FLAG_PERVASIVE, s: "pervasive"},
  37. }
  38. func listFlags(flag int) []string {
  39. var flags []string
  40. for _, tf := range testFlags {
  41. if flag&int(tf.f) != 0 {
  42. flags = append(flags, tf.s)
  43. }
  44. }
  45. return flags
  46. }
  47. func (r *Route) ListFlags() []string {
  48. return listFlags(r.Flags)
  49. }
  50. func (n *NexthopInfo) ListFlags() []string {
  51. return listFlags(n.Flags)
  52. }
  53. type MPLSDestination struct {
  54. Labels []int
  55. }
  56. func (d *MPLSDestination) Family() int {
  57. return nl.FAMILY_MPLS
  58. }
  59. func (d *MPLSDestination) Decode(buf []byte) error {
  60. d.Labels = nl.DecodeMPLSStack(buf)
  61. return nil
  62. }
  63. func (d *MPLSDestination) Encode() ([]byte, error) {
  64. return nl.EncodeMPLSStack(d.Labels...), nil
  65. }
  66. func (d *MPLSDestination) String() string {
  67. s := make([]string, 0, len(d.Labels))
  68. for _, l := range d.Labels {
  69. s = append(s, fmt.Sprintf("%d", l))
  70. }
  71. return strings.Join(s, "/")
  72. }
  73. type MPLSEncap struct {
  74. Labels []int
  75. }
  76. func (e *MPLSEncap) Type() int {
  77. return nl.LWTUNNEL_ENCAP_MPLS
  78. }
  79. func (e *MPLSEncap) Decode(buf []byte) error {
  80. if len(buf) < 4 {
  81. return fmt.Errorf("Lack of bytes")
  82. }
  83. native := nl.NativeEndian()
  84. l := native.Uint16(buf)
  85. if len(buf) < int(l) {
  86. return fmt.Errorf("Lack of bytes")
  87. }
  88. buf = buf[:l]
  89. typ := native.Uint16(buf[2:])
  90. if typ != nl.MPLS_IPTUNNEL_DST {
  91. return fmt.Errorf("Unknown MPLS Encap Type: %d", typ)
  92. }
  93. e.Labels = nl.DecodeMPLSStack(buf[4:])
  94. return nil
  95. }
  96. func (e *MPLSEncap) Encode() ([]byte, error) {
  97. s := nl.EncodeMPLSStack(e.Labels...)
  98. native := nl.NativeEndian()
  99. hdr := make([]byte, 4)
  100. native.PutUint16(hdr, uint16(len(s)+4))
  101. native.PutUint16(hdr[2:], nl.MPLS_IPTUNNEL_DST)
  102. return append(hdr, s...), nil
  103. }
  104. func (e *MPLSEncap) String() string {
  105. s := make([]string, 0, len(e.Labels))
  106. for _, l := range e.Labels {
  107. s = append(s, fmt.Sprintf("%d", l))
  108. }
  109. return strings.Join(s, "/")
  110. }
  111. // RouteAdd will add a route to the system.
  112. // Equivalent to: `ip route add $route`
  113. func RouteAdd(route *Route) error {
  114. return pkgHandle.RouteAdd(route)
  115. }
  116. // RouteAdd will add a route to the system.
  117. // Equivalent to: `ip route add $route`
  118. func (h *Handle) RouteAdd(route *Route) error {
  119. flags := syscall.NLM_F_CREATE | syscall.NLM_F_EXCL | syscall.NLM_F_ACK
  120. req := h.newNetlinkRequest(syscall.RTM_NEWROUTE, flags)
  121. return h.routeHandle(route, req, nl.NewRtMsg())
  122. }
  123. // RouteReplace will add a route to the system.
  124. // Equivalent to: `ip route replace $route`
  125. func RouteReplace(route *Route) error {
  126. return pkgHandle.RouteReplace(route)
  127. }
  128. // RouteReplace will add a route to the system.
  129. // Equivalent to: `ip route replace $route`
  130. func (h *Handle) RouteReplace(route *Route) error {
  131. flags := syscall.NLM_F_CREATE | syscall.NLM_F_REPLACE | syscall.NLM_F_ACK
  132. req := h.newNetlinkRequest(syscall.RTM_NEWROUTE, flags)
  133. return h.routeHandle(route, req, nl.NewRtMsg())
  134. }
  135. // RouteDel will delete a route from the system.
  136. // Equivalent to: `ip route del $route`
  137. func RouteDel(route *Route) error {
  138. return pkgHandle.RouteDel(route)
  139. }
  140. // RouteDel will delete a route from the system.
  141. // Equivalent to: `ip route del $route`
  142. func (h *Handle) RouteDel(route *Route) error {
  143. req := h.newNetlinkRequest(syscall.RTM_DELROUTE, syscall.NLM_F_ACK)
  144. return h.routeHandle(route, req, nl.NewRtDelMsg())
  145. }
  146. func (h *Handle) routeHandle(route *Route, req *nl.NetlinkRequest, msg *nl.RtMsg) error {
  147. if (route.Dst == nil || route.Dst.IP == nil) && route.Src == nil && route.Gw == nil && route.MPLSDst == nil {
  148. return fmt.Errorf("one of Dst.IP, Src, or Gw must not be nil")
  149. }
  150. family := -1
  151. var rtAttrs []*nl.RtAttr
  152. if route.Dst != nil && route.Dst.IP != nil {
  153. dstLen, _ := route.Dst.Mask.Size()
  154. msg.Dst_len = uint8(dstLen)
  155. dstFamily := nl.GetIPFamily(route.Dst.IP)
  156. family = dstFamily
  157. var dstData []byte
  158. if dstFamily == FAMILY_V4 {
  159. dstData = route.Dst.IP.To4()
  160. } else {
  161. dstData = route.Dst.IP.To16()
  162. }
  163. rtAttrs = append(rtAttrs, nl.NewRtAttr(syscall.RTA_DST, dstData))
  164. } else if route.MPLSDst != nil {
  165. family = nl.FAMILY_MPLS
  166. msg.Dst_len = uint8(20)
  167. msg.Type = syscall.RTN_UNICAST
  168. rtAttrs = append(rtAttrs, nl.NewRtAttr(syscall.RTA_DST, nl.EncodeMPLSStack(*route.MPLSDst)))
  169. }
  170. if route.NewDst != nil {
  171. if family != -1 && family != route.NewDst.Family() {
  172. return fmt.Errorf("new destination and destination are not the same address family")
  173. }
  174. buf, err := route.NewDst.Encode()
  175. if err != nil {
  176. return err
  177. }
  178. rtAttrs = append(rtAttrs, nl.NewRtAttr(nl.RTA_NEWDST, buf))
  179. }
  180. if route.Encap != nil {
  181. buf := make([]byte, 2)
  182. native.PutUint16(buf, uint16(route.Encap.Type()))
  183. rtAttrs = append(rtAttrs, nl.NewRtAttr(nl.RTA_ENCAP_TYPE, buf))
  184. buf, err := route.Encap.Encode()
  185. if err != nil {
  186. return err
  187. }
  188. rtAttrs = append(rtAttrs, nl.NewRtAttr(nl.RTA_ENCAP, buf))
  189. }
  190. if route.Src != nil {
  191. srcFamily := nl.GetIPFamily(route.Src)
  192. if family != -1 && family != srcFamily {
  193. return fmt.Errorf("source and destination ip are not the same IP family")
  194. }
  195. family = srcFamily
  196. var srcData []byte
  197. if srcFamily == FAMILY_V4 {
  198. srcData = route.Src.To4()
  199. } else {
  200. srcData = route.Src.To16()
  201. }
  202. // The commonly used src ip for routes is actually PREFSRC
  203. rtAttrs = append(rtAttrs, nl.NewRtAttr(syscall.RTA_PREFSRC, srcData))
  204. }
  205. if route.Gw != nil {
  206. gwFamily := nl.GetIPFamily(route.Gw)
  207. if family != -1 && family != gwFamily {
  208. return fmt.Errorf("gateway, source, and destination ip are not the same IP family")
  209. }
  210. family = gwFamily
  211. var gwData []byte
  212. if gwFamily == FAMILY_V4 {
  213. gwData = route.Gw.To4()
  214. } else {
  215. gwData = route.Gw.To16()
  216. }
  217. rtAttrs = append(rtAttrs, nl.NewRtAttr(syscall.RTA_GATEWAY, gwData))
  218. }
  219. if len(route.MultiPath) > 0 {
  220. buf := []byte{}
  221. for _, nh := range route.MultiPath {
  222. rtnh := &nl.RtNexthop{
  223. RtNexthop: syscall.RtNexthop{
  224. Hops: uint8(nh.Hops),
  225. Ifindex: int32(nh.LinkIndex),
  226. Flags: uint8(nh.Flags),
  227. },
  228. }
  229. children := []nl.NetlinkRequestData{}
  230. if nh.Gw != nil {
  231. gwFamily := nl.GetIPFamily(nh.Gw)
  232. if family != -1 && family != gwFamily {
  233. return fmt.Errorf("gateway, source, and destination ip are not the same IP family")
  234. }
  235. if gwFamily == FAMILY_V4 {
  236. children = append(children, nl.NewRtAttr(syscall.RTA_GATEWAY, []byte(nh.Gw.To4())))
  237. } else {
  238. children = append(children, nl.NewRtAttr(syscall.RTA_GATEWAY, []byte(nh.Gw.To16())))
  239. }
  240. }
  241. if nh.NewDst != nil {
  242. if family != -1 && family != nh.NewDst.Family() {
  243. return fmt.Errorf("new destination and destination are not the same address family")
  244. }
  245. buf, err := nh.NewDst.Encode()
  246. if err != nil {
  247. return err
  248. }
  249. children = append(children, nl.NewRtAttr(nl.RTA_NEWDST, buf))
  250. }
  251. if nh.Encap != nil {
  252. buf := make([]byte, 2)
  253. native.PutUint16(buf, uint16(nh.Encap.Type()))
  254. rtAttrs = append(rtAttrs, nl.NewRtAttr(nl.RTA_ENCAP_TYPE, buf))
  255. buf, err := nh.Encap.Encode()
  256. if err != nil {
  257. return err
  258. }
  259. children = append(children, nl.NewRtAttr(nl.RTA_ENCAP, buf))
  260. }
  261. rtnh.Children = children
  262. buf = append(buf, rtnh.Serialize()...)
  263. }
  264. rtAttrs = append(rtAttrs, nl.NewRtAttr(syscall.RTA_MULTIPATH, buf))
  265. }
  266. if route.Table > 0 {
  267. if route.Table >= 256 {
  268. msg.Table = syscall.RT_TABLE_UNSPEC
  269. b := make([]byte, 4)
  270. native.PutUint32(b, uint32(route.Table))
  271. rtAttrs = append(rtAttrs, nl.NewRtAttr(syscall.RTA_TABLE, b))
  272. } else {
  273. msg.Table = uint8(route.Table)
  274. }
  275. }
  276. if route.Priority > 0 {
  277. b := make([]byte, 4)
  278. native.PutUint32(b, uint32(route.Priority))
  279. rtAttrs = append(rtAttrs, nl.NewRtAttr(syscall.RTA_PRIORITY, b))
  280. }
  281. if route.Tos > 0 {
  282. msg.Tos = uint8(route.Tos)
  283. }
  284. if route.Protocol > 0 {
  285. msg.Protocol = uint8(route.Protocol)
  286. }
  287. if route.Type > 0 {
  288. msg.Type = uint8(route.Type)
  289. }
  290. msg.Flags = uint32(route.Flags)
  291. msg.Scope = uint8(route.Scope)
  292. msg.Family = uint8(family)
  293. req.AddData(msg)
  294. for _, attr := range rtAttrs {
  295. req.AddData(attr)
  296. }
  297. var (
  298. b = make([]byte, 4)
  299. native = nl.NativeEndian()
  300. )
  301. native.PutUint32(b, uint32(route.LinkIndex))
  302. req.AddData(nl.NewRtAttr(syscall.RTA_OIF, b))
  303. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  304. return err
  305. }
  306. // RouteList gets a list of routes in the system.
  307. // Equivalent to: `ip route show`.
  308. // The list can be filtered by link and ip family.
  309. func RouteList(link Link, family int) ([]Route, error) {
  310. return pkgHandle.RouteList(link, family)
  311. }
  312. // RouteList gets a list of routes in the system.
  313. // Equivalent to: `ip route show`.
  314. // The list can be filtered by link and ip family.
  315. func (h *Handle) RouteList(link Link, family int) ([]Route, error) {
  316. var routeFilter *Route
  317. if link != nil {
  318. routeFilter = &Route{
  319. LinkIndex: link.Attrs().Index,
  320. }
  321. }
  322. return h.RouteListFiltered(family, routeFilter, RT_FILTER_OIF)
  323. }
  324. // RouteListFiltered gets a list of routes in the system filtered with specified rules.
  325. // All rules must be defined in RouteFilter struct
  326. func RouteListFiltered(family int, filter *Route, filterMask uint64) ([]Route, error) {
  327. return pkgHandle.RouteListFiltered(family, filter, filterMask)
  328. }
  329. // RouteListFiltered gets a list of routes in the system filtered with specified rules.
  330. // All rules must be defined in RouteFilter struct
  331. func (h *Handle) RouteListFiltered(family int, filter *Route, filterMask uint64) ([]Route, error) {
  332. req := h.newNetlinkRequest(syscall.RTM_GETROUTE, syscall.NLM_F_DUMP)
  333. infmsg := nl.NewIfInfomsg(family)
  334. req.AddData(infmsg)
  335. msgs, err := req.Execute(syscall.NETLINK_ROUTE, syscall.RTM_NEWROUTE)
  336. if err != nil {
  337. return nil, err
  338. }
  339. var res []Route
  340. for _, m := range msgs {
  341. msg := nl.DeserializeRtMsg(m)
  342. if msg.Flags&syscall.RTM_F_CLONED != 0 {
  343. // Ignore cloned routes
  344. continue
  345. }
  346. if msg.Table != syscall.RT_TABLE_MAIN {
  347. if filter == nil || filter != nil && filterMask&RT_FILTER_TABLE == 0 {
  348. // Ignore non-main tables
  349. continue
  350. }
  351. }
  352. route, err := deserializeRoute(m)
  353. if err != nil {
  354. return nil, err
  355. }
  356. if filter != nil {
  357. switch {
  358. case filterMask&RT_FILTER_TABLE != 0 && route.Table != filter.Table:
  359. continue
  360. case filterMask&RT_FILTER_PROTOCOL != 0 && route.Protocol != filter.Protocol:
  361. continue
  362. case filterMask&RT_FILTER_SCOPE != 0 && route.Scope != filter.Scope:
  363. continue
  364. case filterMask&RT_FILTER_TYPE != 0 && route.Type != filter.Type:
  365. continue
  366. case filterMask&RT_FILTER_TOS != 0 && route.Tos != filter.Tos:
  367. continue
  368. case filterMask&RT_FILTER_OIF != 0 && route.LinkIndex != filter.LinkIndex:
  369. continue
  370. case filterMask&RT_FILTER_IIF != 0 && route.ILinkIndex != filter.ILinkIndex:
  371. continue
  372. case filterMask&RT_FILTER_GW != 0 && !route.Gw.Equal(filter.Gw):
  373. continue
  374. case filterMask&RT_FILTER_SRC != 0 && !route.Src.Equal(filter.Src):
  375. continue
  376. case filterMask&RT_FILTER_DST != 0:
  377. if filter.MPLSDst == nil || route.MPLSDst == nil || (*filter.MPLSDst) != (*route.MPLSDst) {
  378. if filter.Dst == nil {
  379. if route.Dst != nil {
  380. continue
  381. }
  382. } else {
  383. if route.Dst == nil {
  384. continue
  385. }
  386. aMaskLen, aMaskBits := route.Dst.Mask.Size()
  387. bMaskLen, bMaskBits := filter.Dst.Mask.Size()
  388. if !(route.Dst.IP.Equal(filter.Dst.IP) && aMaskLen == bMaskLen && aMaskBits == bMaskBits) {
  389. continue
  390. }
  391. }
  392. }
  393. }
  394. }
  395. res = append(res, route)
  396. }
  397. return res, nil
  398. }
  399. // deserializeRoute decodes a binary netlink message into a Route struct
  400. func deserializeRoute(m []byte) (Route, error) {
  401. msg := nl.DeserializeRtMsg(m)
  402. attrs, err := nl.ParseRouteAttr(m[msg.Len():])
  403. if err != nil {
  404. return Route{}, err
  405. }
  406. route := Route{
  407. Scope: Scope(msg.Scope),
  408. Protocol: int(msg.Protocol),
  409. Table: int(msg.Table),
  410. Type: int(msg.Type),
  411. Tos: int(msg.Tos),
  412. Flags: int(msg.Flags),
  413. }
  414. native := nl.NativeEndian()
  415. var encap, encapType syscall.NetlinkRouteAttr
  416. for _, attr := range attrs {
  417. switch attr.Attr.Type {
  418. case syscall.RTA_GATEWAY:
  419. route.Gw = net.IP(attr.Value)
  420. case syscall.RTA_PREFSRC:
  421. route.Src = net.IP(attr.Value)
  422. case syscall.RTA_DST:
  423. if msg.Family == nl.FAMILY_MPLS {
  424. stack := nl.DecodeMPLSStack(attr.Value)
  425. if len(stack) == 0 || len(stack) > 1 {
  426. return route, fmt.Errorf("invalid MPLS RTA_DST")
  427. }
  428. route.MPLSDst = &stack[0]
  429. } else {
  430. route.Dst = &net.IPNet{
  431. IP: attr.Value,
  432. Mask: net.CIDRMask(int(msg.Dst_len), 8*len(attr.Value)),
  433. }
  434. }
  435. case syscall.RTA_OIF:
  436. route.LinkIndex = int(native.Uint32(attr.Value[0:4]))
  437. case syscall.RTA_IIF:
  438. route.ILinkIndex = int(native.Uint32(attr.Value[0:4]))
  439. case syscall.RTA_PRIORITY:
  440. route.Priority = int(native.Uint32(attr.Value[0:4]))
  441. case syscall.RTA_TABLE:
  442. route.Table = int(native.Uint32(attr.Value[0:4]))
  443. case syscall.RTA_MULTIPATH:
  444. parseRtNexthop := func(value []byte) (*NexthopInfo, []byte, error) {
  445. if len(value) < syscall.SizeofRtNexthop {
  446. return nil, nil, fmt.Errorf("Lack of bytes")
  447. }
  448. nh := nl.DeserializeRtNexthop(value)
  449. if len(value) < int(nh.RtNexthop.Len) {
  450. return nil, nil, fmt.Errorf("Lack of bytes")
  451. }
  452. info := &NexthopInfo{
  453. LinkIndex: int(nh.RtNexthop.Ifindex),
  454. Hops: int(nh.RtNexthop.Hops),
  455. Flags: int(nh.RtNexthop.Flags),
  456. }
  457. attrs, err := nl.ParseRouteAttr(value[syscall.SizeofRtNexthop:int(nh.RtNexthop.Len)])
  458. if err != nil {
  459. return nil, nil, err
  460. }
  461. var encap, encapType syscall.NetlinkRouteAttr
  462. for _, attr := range attrs {
  463. switch attr.Attr.Type {
  464. case syscall.RTA_GATEWAY:
  465. info.Gw = net.IP(attr.Value)
  466. case nl.RTA_NEWDST:
  467. var d Destination
  468. switch msg.Family {
  469. case nl.FAMILY_MPLS:
  470. d = &MPLSDestination{}
  471. }
  472. if err := d.Decode(attr.Value); err != nil {
  473. return nil, nil, err
  474. }
  475. info.NewDst = d
  476. case nl.RTA_ENCAP_TYPE:
  477. encapType = attr
  478. case nl.RTA_ENCAP:
  479. encap = attr
  480. }
  481. }
  482. if len(encap.Value) != 0 && len(encapType.Value) != 0 {
  483. typ := int(native.Uint16(encapType.Value[0:2]))
  484. var e Encap
  485. switch typ {
  486. case nl.LWTUNNEL_ENCAP_MPLS:
  487. e = &MPLSEncap{}
  488. if err := e.Decode(encap.Value); err != nil {
  489. return nil, nil, err
  490. }
  491. }
  492. info.Encap = e
  493. }
  494. return info, value[int(nh.RtNexthop.Len):], nil
  495. }
  496. rest := attr.Value
  497. for len(rest) > 0 {
  498. info, buf, err := parseRtNexthop(rest)
  499. if err != nil {
  500. return route, err
  501. }
  502. route.MultiPath = append(route.MultiPath, info)
  503. rest = buf
  504. }
  505. case nl.RTA_NEWDST:
  506. var d Destination
  507. switch msg.Family {
  508. case nl.FAMILY_MPLS:
  509. d = &MPLSDestination{}
  510. }
  511. if err := d.Decode(attr.Value); err != nil {
  512. return route, err
  513. }
  514. route.NewDst = d
  515. case nl.RTA_ENCAP_TYPE:
  516. encapType = attr
  517. case nl.RTA_ENCAP:
  518. encap = attr
  519. }
  520. }
  521. if len(encap.Value) != 0 && len(encapType.Value) != 0 {
  522. typ := int(native.Uint16(encapType.Value[0:2]))
  523. var e Encap
  524. switch typ {
  525. case nl.LWTUNNEL_ENCAP_MPLS:
  526. e = &MPLSEncap{}
  527. if err := e.Decode(encap.Value); err != nil {
  528. return route, err
  529. }
  530. }
  531. route.Encap = e
  532. }
  533. return route, nil
  534. }
  535. // RouteGet gets a route to a specific destination from the host system.
  536. // Equivalent to: 'ip route get'.
  537. func RouteGet(destination net.IP) ([]Route, error) {
  538. return pkgHandle.RouteGet(destination)
  539. }
  540. // RouteGet gets a route to a specific destination from the host system.
  541. // Equivalent to: 'ip route get'.
  542. func (h *Handle) RouteGet(destination net.IP) ([]Route, error) {
  543. req := h.newNetlinkRequest(syscall.RTM_GETROUTE, syscall.NLM_F_REQUEST)
  544. family := nl.GetIPFamily(destination)
  545. var destinationData []byte
  546. var bitlen uint8
  547. if family == FAMILY_V4 {
  548. destinationData = destination.To4()
  549. bitlen = 32
  550. } else {
  551. destinationData = destination.To16()
  552. bitlen = 128
  553. }
  554. msg := &nl.RtMsg{}
  555. msg.Family = uint8(family)
  556. msg.Dst_len = bitlen
  557. req.AddData(msg)
  558. rtaDst := nl.NewRtAttr(syscall.RTA_DST, destinationData)
  559. req.AddData(rtaDst)
  560. msgs, err := req.Execute(syscall.NETLINK_ROUTE, syscall.RTM_NEWROUTE)
  561. if err != nil {
  562. return nil, err
  563. }
  564. var res []Route
  565. for _, m := range msgs {
  566. route, err := deserializeRoute(m)
  567. if err != nil {
  568. return nil, err
  569. }
  570. res = append(res, route)
  571. }
  572. return res, nil
  573. }
  574. // RouteSubscribe takes a chan down which notifications will be sent
  575. // when routes are added or deleted. Close the 'done' chan to stop subscription.
  576. func RouteSubscribe(ch chan<- RouteUpdate, done <-chan struct{}) error {
  577. return routeSubscribeAt(netns.None(), netns.None(), ch, done)
  578. }
  579. // RouteSubscribeAt works like RouteSubscribe plus it allows the caller
  580. // to choose the network namespace in which to subscribe (ns).
  581. func RouteSubscribeAt(ns netns.NsHandle, ch chan<- RouteUpdate, done <-chan struct{}) error {
  582. return routeSubscribeAt(ns, netns.None(), ch, done)
  583. }
  584. func routeSubscribeAt(newNs, curNs netns.NsHandle, ch chan<- RouteUpdate, done <-chan struct{}) error {
  585. s, err := nl.SubscribeAt(newNs, curNs, syscall.NETLINK_ROUTE, syscall.RTNLGRP_IPV4_ROUTE, syscall.RTNLGRP_IPV6_ROUTE)
  586. if err != nil {
  587. return err
  588. }
  589. if done != nil {
  590. go func() {
  591. <-done
  592. s.Close()
  593. }()
  594. }
  595. go func() {
  596. defer close(ch)
  597. for {
  598. msgs, err := s.Receive()
  599. if err != nil {
  600. return
  601. }
  602. for _, m := range msgs {
  603. route, err := deserializeRoute(m.Data)
  604. if err != nil {
  605. return
  606. }
  607. ch <- RouteUpdate{Type: m.Header.Type, Route: route}
  608. }
  609. }
  610. }()
  611. return nil
  612. }