route_linux.go 51 KB

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