route_linux.go 41 KB

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