route_linux.go 41 KB

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