link_linux.go 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643
  1. package netlink
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "fmt"
  6. "net"
  7. "os"
  8. "syscall"
  9. "unsafe"
  10. "github.com/vishvananda/netlink/nl"
  11. "github.com/vishvananda/netns"
  12. )
  13. const (
  14. SizeofLinkStats32 = 0x5c
  15. SizeofLinkStats64 = 0xd8
  16. IFLA_STATS64 = 0x17 // syscall pkg does not contain this one
  17. )
  18. const (
  19. TUNTAP_MODE_TUN TuntapMode = syscall.IFF_TUN
  20. TUNTAP_MODE_TAP TuntapMode = syscall.IFF_TAP
  21. TUNTAP_DEFAULTS TuntapFlag = syscall.IFF_TUN_EXCL | syscall.IFF_ONE_QUEUE
  22. TUNTAP_VNET_HDR TuntapFlag = syscall.IFF_VNET_HDR
  23. TUNTAP_TUN_EXCL TuntapFlag = syscall.IFF_TUN_EXCL
  24. TUNTAP_NO_PI TuntapFlag = syscall.IFF_NO_PI
  25. TUNTAP_ONE_QUEUE TuntapFlag = syscall.IFF_ONE_QUEUE
  26. )
  27. var lookupByDump = false
  28. var macvlanModes = [...]uint32{
  29. 0,
  30. nl.MACVLAN_MODE_PRIVATE,
  31. nl.MACVLAN_MODE_VEPA,
  32. nl.MACVLAN_MODE_BRIDGE,
  33. nl.MACVLAN_MODE_PASSTHRU,
  34. nl.MACVLAN_MODE_SOURCE,
  35. }
  36. func ensureIndex(link *LinkAttrs) {
  37. if link != nil && link.Index == 0 {
  38. newlink, _ := LinkByName(link.Name)
  39. if newlink != nil {
  40. link.Index = newlink.Attrs().Index
  41. }
  42. }
  43. }
  44. func (h *Handle) ensureIndex(link *LinkAttrs) {
  45. if link != nil && link.Index == 0 {
  46. newlink, _ := h.LinkByName(link.Name)
  47. if newlink != nil {
  48. link.Index = newlink.Attrs().Index
  49. }
  50. }
  51. }
  52. func (h *Handle) SetPromiscOn(link Link) error {
  53. base := link.Attrs()
  54. h.ensureIndex(base)
  55. req := h.newNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
  56. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  57. msg.Change = syscall.IFF_PROMISC
  58. msg.Flags = syscall.IFF_UP
  59. msg.Index = int32(base.Index)
  60. req.AddData(msg)
  61. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  62. return err
  63. }
  64. func SetPromiscOn(link Link) error {
  65. return pkgHandle.SetPromiscOn(link)
  66. }
  67. func (h *Handle) SetPromiscOff(link Link) error {
  68. base := link.Attrs()
  69. h.ensureIndex(base)
  70. req := h.newNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
  71. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  72. msg.Change = syscall.IFF_PROMISC
  73. msg.Flags = 0 & ^syscall.IFF_UP
  74. msg.Index = int32(base.Index)
  75. req.AddData(msg)
  76. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  77. return err
  78. }
  79. func SetPromiscOff(link Link) error {
  80. return pkgHandle.SetPromiscOff(link)
  81. }
  82. // LinkSetUp enables the link device.
  83. // Equivalent to: `ip link set $link up`
  84. func LinkSetUp(link Link) error {
  85. return pkgHandle.LinkSetUp(link)
  86. }
  87. // LinkSetUp enables the link device.
  88. // Equivalent to: `ip link set $link up`
  89. func (h *Handle) LinkSetUp(link Link) error {
  90. base := link.Attrs()
  91. h.ensureIndex(base)
  92. req := h.newNetlinkRequest(syscall.RTM_NEWLINK, syscall.NLM_F_ACK)
  93. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  94. msg.Change = syscall.IFF_UP
  95. msg.Flags = syscall.IFF_UP
  96. msg.Index = int32(base.Index)
  97. req.AddData(msg)
  98. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  99. return err
  100. }
  101. // LinkSetDown disables link device.
  102. // Equivalent to: `ip link set $link down`
  103. func LinkSetDown(link Link) error {
  104. return pkgHandle.LinkSetDown(link)
  105. }
  106. // LinkSetDown disables link device.
  107. // Equivalent to: `ip link set $link down`
  108. func (h *Handle) LinkSetDown(link Link) error {
  109. base := link.Attrs()
  110. h.ensureIndex(base)
  111. req := h.newNetlinkRequest(syscall.RTM_NEWLINK, syscall.NLM_F_ACK)
  112. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  113. msg.Change = syscall.IFF_UP
  114. msg.Flags = 0 & ^syscall.IFF_UP
  115. msg.Index = int32(base.Index)
  116. req.AddData(msg)
  117. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  118. return err
  119. }
  120. // LinkSetMTU sets the mtu of the link device.
  121. // Equivalent to: `ip link set $link mtu $mtu`
  122. func LinkSetMTU(link Link, mtu int) error {
  123. return pkgHandle.LinkSetMTU(link, mtu)
  124. }
  125. // LinkSetMTU sets the mtu of the link device.
  126. // Equivalent to: `ip link set $link mtu $mtu`
  127. func (h *Handle) LinkSetMTU(link Link, mtu int) error {
  128. base := link.Attrs()
  129. h.ensureIndex(base)
  130. req := h.newNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
  131. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  132. msg.Index = int32(base.Index)
  133. req.AddData(msg)
  134. b := make([]byte, 4)
  135. native.PutUint32(b, uint32(mtu))
  136. data := nl.NewRtAttr(syscall.IFLA_MTU, b)
  137. req.AddData(data)
  138. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  139. return err
  140. }
  141. // LinkSetName sets the name of the link device.
  142. // Equivalent to: `ip link set $link name $name`
  143. func LinkSetName(link Link, name string) error {
  144. return pkgHandle.LinkSetName(link, name)
  145. }
  146. // LinkSetName sets the name of the link device.
  147. // Equivalent to: `ip link set $link name $name`
  148. func (h *Handle) LinkSetName(link Link, name string) error {
  149. base := link.Attrs()
  150. h.ensureIndex(base)
  151. req := h.newNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
  152. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  153. msg.Index = int32(base.Index)
  154. req.AddData(msg)
  155. data := nl.NewRtAttr(syscall.IFLA_IFNAME, []byte(name))
  156. req.AddData(data)
  157. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  158. return err
  159. }
  160. // LinkSetAlias sets the alias of the link device.
  161. // Equivalent to: `ip link set dev $link alias $name`
  162. func LinkSetAlias(link Link, name string) error {
  163. return pkgHandle.LinkSetAlias(link, name)
  164. }
  165. // LinkSetAlias sets the alias of the link device.
  166. // Equivalent to: `ip link set dev $link alias $name`
  167. func (h *Handle) LinkSetAlias(link Link, name string) error {
  168. base := link.Attrs()
  169. h.ensureIndex(base)
  170. req := h.newNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
  171. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  172. msg.Index = int32(base.Index)
  173. req.AddData(msg)
  174. data := nl.NewRtAttr(syscall.IFLA_IFALIAS, []byte(name))
  175. req.AddData(data)
  176. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  177. return err
  178. }
  179. // LinkSetHardwareAddr sets the hardware address of the link device.
  180. // Equivalent to: `ip link set $link address $hwaddr`
  181. func LinkSetHardwareAddr(link Link, hwaddr net.HardwareAddr) error {
  182. return pkgHandle.LinkSetHardwareAddr(link, hwaddr)
  183. }
  184. // LinkSetHardwareAddr sets the hardware address of the link device.
  185. // Equivalent to: `ip link set $link address $hwaddr`
  186. func (h *Handle) LinkSetHardwareAddr(link Link, hwaddr net.HardwareAddr) error {
  187. base := link.Attrs()
  188. h.ensureIndex(base)
  189. req := h.newNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
  190. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  191. msg.Index = int32(base.Index)
  192. req.AddData(msg)
  193. data := nl.NewRtAttr(syscall.IFLA_ADDRESS, []byte(hwaddr))
  194. req.AddData(data)
  195. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  196. return err
  197. }
  198. // LinkSetVfHardwareAddr sets the hardware address of a vf for the link.
  199. // Equivalent to: `ip link set $link vf $vf mac $hwaddr`
  200. func LinkSetVfHardwareAddr(link Link, vf int, hwaddr net.HardwareAddr) error {
  201. return pkgHandle.LinkSetVfHardwareAddr(link, vf, hwaddr)
  202. }
  203. // LinkSetVfHardwareAddr sets the hardware address of a vf for the link.
  204. // Equivalent to: `ip link set $link vf $vf mac $hwaddr`
  205. func (h *Handle) LinkSetVfHardwareAddr(link Link, vf int, hwaddr net.HardwareAddr) error {
  206. base := link.Attrs()
  207. h.ensureIndex(base)
  208. req := h.newNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
  209. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  210. msg.Index = int32(base.Index)
  211. req.AddData(msg)
  212. data := nl.NewRtAttr(nl.IFLA_VFINFO_LIST, nil)
  213. info := nl.NewRtAttrChild(data, nl.IFLA_VF_INFO, nil)
  214. vfmsg := nl.VfMac{
  215. Vf: uint32(vf),
  216. }
  217. copy(vfmsg.Mac[:], []byte(hwaddr))
  218. nl.NewRtAttrChild(info, nl.IFLA_VF_MAC, vfmsg.Serialize())
  219. req.AddData(data)
  220. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  221. return err
  222. }
  223. // LinkSetVfVlan sets the vlan of a vf for the link.
  224. // Equivalent to: `ip link set $link vf $vf vlan $vlan`
  225. func LinkSetVfVlan(link Link, vf, vlan int) error {
  226. return pkgHandle.LinkSetVfVlan(link, vf, vlan)
  227. }
  228. // LinkSetVfVlan sets the vlan of a vf for the link.
  229. // Equivalent to: `ip link set $link vf $vf vlan $vlan`
  230. func (h *Handle) LinkSetVfVlan(link Link, vf, vlan int) error {
  231. base := link.Attrs()
  232. h.ensureIndex(base)
  233. req := h.newNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
  234. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  235. msg.Index = int32(base.Index)
  236. req.AddData(msg)
  237. data := nl.NewRtAttr(nl.IFLA_VFINFO_LIST, nil)
  238. info := nl.NewRtAttrChild(data, nl.IFLA_VF_INFO, nil)
  239. vfmsg := nl.VfVlan{
  240. Vf: uint32(vf),
  241. Vlan: uint32(vlan),
  242. }
  243. nl.NewRtAttrChild(info, nl.IFLA_VF_VLAN, vfmsg.Serialize())
  244. req.AddData(data)
  245. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  246. return err
  247. }
  248. // LinkSetVfTxRate sets the tx rate of a vf for the link.
  249. // Equivalent to: `ip link set $link vf $vf rate $rate`
  250. func LinkSetVfTxRate(link Link, vf, rate int) error {
  251. return pkgHandle.LinkSetVfTxRate(link, vf, rate)
  252. }
  253. // LinkSetVfTxRate sets the tx rate of a vf for the link.
  254. // Equivalent to: `ip link set $link vf $vf rate $rate`
  255. func (h *Handle) LinkSetVfTxRate(link Link, vf, rate int) error {
  256. base := link.Attrs()
  257. h.ensureIndex(base)
  258. req := h.newNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
  259. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  260. msg.Index = int32(base.Index)
  261. req.AddData(msg)
  262. data := nl.NewRtAttr(nl.IFLA_VFINFO_LIST, nil)
  263. info := nl.NewRtAttrChild(data, nl.IFLA_VF_INFO, nil)
  264. vfmsg := nl.VfTxRate{
  265. Vf: uint32(vf),
  266. Rate: uint32(rate),
  267. }
  268. nl.NewRtAttrChild(info, nl.IFLA_VF_TX_RATE, vfmsg.Serialize())
  269. req.AddData(data)
  270. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  271. return err
  272. }
  273. // LinkSetMaster sets the master of the link device.
  274. // Equivalent to: `ip link set $link master $master`
  275. func LinkSetMaster(link Link, master *Bridge) error {
  276. return pkgHandle.LinkSetMaster(link, master)
  277. }
  278. // LinkSetMaster sets the master of the link device.
  279. // Equivalent to: `ip link set $link master $master`
  280. func (h *Handle) LinkSetMaster(link Link, master *Bridge) error {
  281. index := 0
  282. if master != nil {
  283. masterBase := master.Attrs()
  284. h.ensureIndex(masterBase)
  285. index = masterBase.Index
  286. }
  287. if index <= 0 {
  288. return fmt.Errorf("Device does not exist")
  289. }
  290. return h.LinkSetMasterByIndex(link, index)
  291. }
  292. // LinkSetNoMaster removes the master of the link device.
  293. // Equivalent to: `ip link set $link nomaster`
  294. func LinkSetNoMaster(link Link) error {
  295. return pkgHandle.LinkSetNoMaster(link)
  296. }
  297. // LinkSetNoMaster removes the master of the link device.
  298. // Equivalent to: `ip link set $link nomaster`
  299. func (h *Handle) LinkSetNoMaster(link Link) error {
  300. return h.LinkSetMasterByIndex(link, 0)
  301. }
  302. // LinkSetMasterByIndex sets the master of the link device.
  303. // Equivalent to: `ip link set $link master $master`
  304. func LinkSetMasterByIndex(link Link, masterIndex int) error {
  305. return pkgHandle.LinkSetMasterByIndex(link, masterIndex)
  306. }
  307. // LinkSetMasterByIndex sets the master of the link device.
  308. // Equivalent to: `ip link set $link master $master`
  309. func (h *Handle) LinkSetMasterByIndex(link Link, masterIndex int) error {
  310. base := link.Attrs()
  311. h.ensureIndex(base)
  312. req := h.newNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
  313. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  314. msg.Index = int32(base.Index)
  315. req.AddData(msg)
  316. b := make([]byte, 4)
  317. native.PutUint32(b, uint32(masterIndex))
  318. data := nl.NewRtAttr(syscall.IFLA_MASTER, b)
  319. req.AddData(data)
  320. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  321. return err
  322. }
  323. // LinkSetNsPid puts the device into a new network namespace. The
  324. // pid must be a pid of a running process.
  325. // Equivalent to: `ip link set $link netns $pid`
  326. func LinkSetNsPid(link Link, nspid int) error {
  327. return pkgHandle.LinkSetNsPid(link, nspid)
  328. }
  329. // LinkSetNsPid puts the device into a new network namespace. The
  330. // pid must be a pid of a running process.
  331. // Equivalent to: `ip link set $link netns $pid`
  332. func (h *Handle) LinkSetNsPid(link Link, nspid int) error {
  333. base := link.Attrs()
  334. h.ensureIndex(base)
  335. req := h.newNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
  336. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  337. msg.Index = int32(base.Index)
  338. req.AddData(msg)
  339. b := make([]byte, 4)
  340. native.PutUint32(b, uint32(nspid))
  341. data := nl.NewRtAttr(syscall.IFLA_NET_NS_PID, b)
  342. req.AddData(data)
  343. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  344. return err
  345. }
  346. // LinkSetNsFd puts the device into a new network namespace. The
  347. // fd must be an open file descriptor to a network namespace.
  348. // Similar to: `ip link set $link netns $ns`
  349. func LinkSetNsFd(link Link, fd int) error {
  350. return pkgHandle.LinkSetNsFd(link, fd)
  351. }
  352. // LinkSetNsFd puts the device into a new network namespace. The
  353. // fd must be an open file descriptor to a network namespace.
  354. // Similar to: `ip link set $link netns $ns`
  355. func (h *Handle) LinkSetNsFd(link Link, fd int) error {
  356. base := link.Attrs()
  357. h.ensureIndex(base)
  358. req := h.newNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
  359. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  360. msg.Index = int32(base.Index)
  361. req.AddData(msg)
  362. b := make([]byte, 4)
  363. native.PutUint32(b, uint32(fd))
  364. data := nl.NewRtAttr(nl.IFLA_NET_NS_FD, b)
  365. req.AddData(data)
  366. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  367. return err
  368. }
  369. // LinkSetXdpFd adds a bpf function to the driver. The fd must be a bpf
  370. // program loaded with bpf(type=BPF_PROG_TYPE_XDP)
  371. func LinkSetXdpFd(link Link, fd int) error {
  372. base := link.Attrs()
  373. ensureIndex(base)
  374. req := nl.NewNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
  375. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  376. msg.Index = int32(base.Index)
  377. req.AddData(msg)
  378. addXdpAttrs(&LinkXdp{Fd: fd}, req)
  379. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  380. return err
  381. }
  382. func boolAttr(val bool) []byte {
  383. var v uint8
  384. if val {
  385. v = 1
  386. }
  387. return nl.Uint8Attr(v)
  388. }
  389. type vxlanPortRange struct {
  390. Lo, Hi uint16
  391. }
  392. func addVxlanAttrs(vxlan *Vxlan, linkInfo *nl.RtAttr) {
  393. data := nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_DATA, nil)
  394. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_ID, nl.Uint32Attr(uint32(vxlan.VxlanId)))
  395. if vxlan.VtepDevIndex != 0 {
  396. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_LINK, nl.Uint32Attr(uint32(vxlan.VtepDevIndex)))
  397. }
  398. if vxlan.SrcAddr != nil {
  399. ip := vxlan.SrcAddr.To4()
  400. if ip != nil {
  401. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_LOCAL, []byte(ip))
  402. } else {
  403. ip = vxlan.SrcAddr.To16()
  404. if ip != nil {
  405. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_LOCAL6, []byte(ip))
  406. }
  407. }
  408. }
  409. if vxlan.Group != nil {
  410. group := vxlan.Group.To4()
  411. if group != nil {
  412. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_GROUP, []byte(group))
  413. } else {
  414. group = vxlan.Group.To16()
  415. if group != nil {
  416. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_GROUP6, []byte(group))
  417. }
  418. }
  419. }
  420. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_TTL, nl.Uint8Attr(uint8(vxlan.TTL)))
  421. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_TOS, nl.Uint8Attr(uint8(vxlan.TOS)))
  422. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_LEARNING, boolAttr(vxlan.Learning))
  423. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_PROXY, boolAttr(vxlan.Proxy))
  424. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_RSC, boolAttr(vxlan.RSC))
  425. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_L2MISS, boolAttr(vxlan.L2miss))
  426. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_L3MISS, boolAttr(vxlan.L3miss))
  427. if vxlan.UDPCSum {
  428. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_UDP_CSUM, boolAttr(vxlan.UDPCSum))
  429. }
  430. if vxlan.GBP {
  431. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_GBP, []byte{})
  432. }
  433. if vxlan.NoAge {
  434. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_AGEING, nl.Uint32Attr(0))
  435. } else if vxlan.Age > 0 {
  436. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_AGEING, nl.Uint32Attr(uint32(vxlan.Age)))
  437. }
  438. if vxlan.Limit > 0 {
  439. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_LIMIT, nl.Uint32Attr(uint32(vxlan.Limit)))
  440. }
  441. if vxlan.Port > 0 {
  442. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_PORT, htons(uint16(vxlan.Port)))
  443. }
  444. if vxlan.PortLow > 0 || vxlan.PortHigh > 0 {
  445. pr := vxlanPortRange{uint16(vxlan.PortLow), uint16(vxlan.PortHigh)}
  446. buf := new(bytes.Buffer)
  447. binary.Write(buf, binary.BigEndian, &pr)
  448. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_PORT_RANGE, buf.Bytes())
  449. }
  450. }
  451. func addBondAttrs(bond *Bond, linkInfo *nl.RtAttr) {
  452. data := nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_DATA, nil)
  453. if bond.Mode >= 0 {
  454. nl.NewRtAttrChild(data, nl.IFLA_BOND_MODE, nl.Uint8Attr(uint8(bond.Mode)))
  455. }
  456. if bond.ActiveSlave >= 0 {
  457. nl.NewRtAttrChild(data, nl.IFLA_BOND_ACTIVE_SLAVE, nl.Uint32Attr(uint32(bond.ActiveSlave)))
  458. }
  459. if bond.Miimon >= 0 {
  460. nl.NewRtAttrChild(data, nl.IFLA_BOND_MIIMON, nl.Uint32Attr(uint32(bond.Miimon)))
  461. }
  462. if bond.UpDelay >= 0 {
  463. nl.NewRtAttrChild(data, nl.IFLA_BOND_UPDELAY, nl.Uint32Attr(uint32(bond.UpDelay)))
  464. }
  465. if bond.DownDelay >= 0 {
  466. nl.NewRtAttrChild(data, nl.IFLA_BOND_DOWNDELAY, nl.Uint32Attr(uint32(bond.DownDelay)))
  467. }
  468. if bond.UseCarrier >= 0 {
  469. nl.NewRtAttrChild(data, nl.IFLA_BOND_USE_CARRIER, nl.Uint8Attr(uint8(bond.UseCarrier)))
  470. }
  471. if bond.ArpInterval >= 0 {
  472. nl.NewRtAttrChild(data, nl.IFLA_BOND_ARP_INTERVAL, nl.Uint32Attr(uint32(bond.ArpInterval)))
  473. }
  474. if bond.ArpIpTargets != nil {
  475. msg := nl.NewRtAttrChild(data, nl.IFLA_BOND_ARP_IP_TARGET, nil)
  476. for i := range bond.ArpIpTargets {
  477. ip := bond.ArpIpTargets[i].To4()
  478. if ip != nil {
  479. nl.NewRtAttrChild(msg, i, []byte(ip))
  480. continue
  481. }
  482. ip = bond.ArpIpTargets[i].To16()
  483. if ip != nil {
  484. nl.NewRtAttrChild(msg, i, []byte(ip))
  485. }
  486. }
  487. }
  488. if bond.ArpValidate >= 0 {
  489. nl.NewRtAttrChild(data, nl.IFLA_BOND_ARP_VALIDATE, nl.Uint32Attr(uint32(bond.ArpValidate)))
  490. }
  491. if bond.ArpAllTargets >= 0 {
  492. nl.NewRtAttrChild(data, nl.IFLA_BOND_ARP_ALL_TARGETS, nl.Uint32Attr(uint32(bond.ArpAllTargets)))
  493. }
  494. if bond.Primary >= 0 {
  495. nl.NewRtAttrChild(data, nl.IFLA_BOND_PRIMARY, nl.Uint32Attr(uint32(bond.Primary)))
  496. }
  497. if bond.PrimaryReselect >= 0 {
  498. nl.NewRtAttrChild(data, nl.IFLA_BOND_PRIMARY_RESELECT, nl.Uint8Attr(uint8(bond.PrimaryReselect)))
  499. }
  500. if bond.FailOverMac >= 0 {
  501. nl.NewRtAttrChild(data, nl.IFLA_BOND_FAIL_OVER_MAC, nl.Uint8Attr(uint8(bond.FailOverMac)))
  502. }
  503. if bond.XmitHashPolicy >= 0 {
  504. nl.NewRtAttrChild(data, nl.IFLA_BOND_XMIT_HASH_POLICY, nl.Uint8Attr(uint8(bond.XmitHashPolicy)))
  505. }
  506. if bond.ResendIgmp >= 0 {
  507. nl.NewRtAttrChild(data, nl.IFLA_BOND_RESEND_IGMP, nl.Uint32Attr(uint32(bond.ResendIgmp)))
  508. }
  509. if bond.NumPeerNotif >= 0 {
  510. nl.NewRtAttrChild(data, nl.IFLA_BOND_NUM_PEER_NOTIF, nl.Uint8Attr(uint8(bond.NumPeerNotif)))
  511. }
  512. if bond.AllSlavesActive >= 0 {
  513. nl.NewRtAttrChild(data, nl.IFLA_BOND_ALL_SLAVES_ACTIVE, nl.Uint8Attr(uint8(bond.AllSlavesActive)))
  514. }
  515. if bond.MinLinks >= 0 {
  516. nl.NewRtAttrChild(data, nl.IFLA_BOND_MIN_LINKS, nl.Uint32Attr(uint32(bond.MinLinks)))
  517. }
  518. if bond.LpInterval >= 0 {
  519. nl.NewRtAttrChild(data, nl.IFLA_BOND_LP_INTERVAL, nl.Uint32Attr(uint32(bond.LpInterval)))
  520. }
  521. if bond.PackersPerSlave >= 0 {
  522. nl.NewRtAttrChild(data, nl.IFLA_BOND_PACKETS_PER_SLAVE, nl.Uint32Attr(uint32(bond.PackersPerSlave)))
  523. }
  524. if bond.LacpRate >= 0 {
  525. nl.NewRtAttrChild(data, nl.IFLA_BOND_AD_LACP_RATE, nl.Uint8Attr(uint8(bond.LacpRate)))
  526. }
  527. if bond.AdSelect >= 0 {
  528. nl.NewRtAttrChild(data, nl.IFLA_BOND_AD_SELECT, nl.Uint8Attr(uint8(bond.AdSelect)))
  529. }
  530. }
  531. // LinkAdd adds a new link device. The type and features of the device
  532. // are taken from the parameters in the link object.
  533. // Equivalent to: `ip link add $link`
  534. func LinkAdd(link Link) error {
  535. return pkgHandle.LinkAdd(link)
  536. }
  537. // LinkAdd adds a new link device. The type and features of the device
  538. // are taken fromt the parameters in the link object.
  539. // Equivalent to: `ip link add $link`
  540. func (h *Handle) LinkAdd(link Link) error {
  541. // TODO: set mtu and hardware address
  542. // TODO: support extra data for macvlan
  543. base := link.Attrs()
  544. if base.Name == "" {
  545. return fmt.Errorf("LinkAttrs.Name cannot be empty!")
  546. }
  547. if tuntap, ok := link.(*Tuntap); ok {
  548. // TODO: support user
  549. // TODO: support group
  550. // TODO: multi_queue
  551. // TODO: support non- persistent
  552. if tuntap.Mode < syscall.IFF_TUN || tuntap.Mode > syscall.IFF_TAP {
  553. return fmt.Errorf("Tuntap.Mode %v unknown!", tuntap.Mode)
  554. }
  555. file, err := os.OpenFile("/dev/net/tun", os.O_RDWR, 0)
  556. if err != nil {
  557. return err
  558. }
  559. defer file.Close()
  560. var req ifReq
  561. if tuntap.Flags == 0 {
  562. req.Flags = uint16(TUNTAP_DEFAULTS)
  563. } else {
  564. req.Flags = uint16(tuntap.Flags)
  565. }
  566. req.Flags |= uint16(tuntap.Mode)
  567. copy(req.Name[:15], base.Name)
  568. _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, file.Fd(), uintptr(syscall.TUNSETIFF), uintptr(unsafe.Pointer(&req)))
  569. if errno != 0 {
  570. return fmt.Errorf("Tuntap IOCTL TUNSETIFF failed, errno %v", errno)
  571. }
  572. _, _, errno = syscall.Syscall(syscall.SYS_IOCTL, file.Fd(), uintptr(syscall.TUNSETPERSIST), 1)
  573. if errno != 0 {
  574. return fmt.Errorf("Tuntap IOCTL TUNSETPERSIST failed, errno %v", errno)
  575. }
  576. h.ensureIndex(base)
  577. // can't set master during create, so set it afterwards
  578. if base.MasterIndex != 0 {
  579. // TODO: verify MasterIndex is actually a bridge?
  580. return h.LinkSetMasterByIndex(link, base.MasterIndex)
  581. }
  582. return nil
  583. }
  584. req := h.newNetlinkRequest(syscall.RTM_NEWLINK, syscall.NLM_F_CREATE|syscall.NLM_F_EXCL|syscall.NLM_F_ACK)
  585. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  586. // TODO: make it shorter
  587. if base.Flags&net.FlagUp != 0 {
  588. msg.Change = syscall.IFF_UP
  589. msg.Flags = syscall.IFF_UP
  590. }
  591. if base.Flags&net.FlagBroadcast != 0 {
  592. msg.Change |= syscall.IFF_BROADCAST
  593. msg.Flags |= syscall.IFF_BROADCAST
  594. }
  595. if base.Flags&net.FlagLoopback != 0 {
  596. msg.Change |= syscall.IFF_LOOPBACK
  597. msg.Flags |= syscall.IFF_LOOPBACK
  598. }
  599. if base.Flags&net.FlagPointToPoint != 0 {
  600. msg.Change |= syscall.IFF_POINTOPOINT
  601. msg.Flags |= syscall.IFF_POINTOPOINT
  602. }
  603. if base.Flags&net.FlagMulticast != 0 {
  604. msg.Change |= syscall.IFF_MULTICAST
  605. msg.Flags |= syscall.IFF_MULTICAST
  606. }
  607. req.AddData(msg)
  608. if base.ParentIndex != 0 {
  609. b := make([]byte, 4)
  610. native.PutUint32(b, uint32(base.ParentIndex))
  611. data := nl.NewRtAttr(syscall.IFLA_LINK, b)
  612. req.AddData(data)
  613. } else if link.Type() == "ipvlan" {
  614. return fmt.Errorf("Can't create ipvlan link without ParentIndex")
  615. }
  616. nameData := nl.NewRtAttr(syscall.IFLA_IFNAME, nl.ZeroTerminated(base.Name))
  617. req.AddData(nameData)
  618. if base.MTU > 0 {
  619. mtu := nl.NewRtAttr(syscall.IFLA_MTU, nl.Uint32Attr(uint32(base.MTU)))
  620. req.AddData(mtu)
  621. }
  622. if base.TxQLen >= 0 {
  623. qlen := nl.NewRtAttr(syscall.IFLA_TXQLEN, nl.Uint32Attr(uint32(base.TxQLen)))
  624. req.AddData(qlen)
  625. }
  626. if base.Namespace != nil {
  627. var attr *nl.RtAttr
  628. switch base.Namespace.(type) {
  629. case NsPid:
  630. val := nl.Uint32Attr(uint32(base.Namespace.(NsPid)))
  631. attr = nl.NewRtAttr(syscall.IFLA_NET_NS_PID, val)
  632. case NsFd:
  633. val := nl.Uint32Attr(uint32(base.Namespace.(NsFd)))
  634. attr = nl.NewRtAttr(nl.IFLA_NET_NS_FD, val)
  635. }
  636. req.AddData(attr)
  637. }
  638. if base.Xdp != nil {
  639. addXdpAttrs(base.Xdp, req)
  640. }
  641. linkInfo := nl.NewRtAttr(syscall.IFLA_LINKINFO, nil)
  642. nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_KIND, nl.NonZeroTerminated(link.Type()))
  643. if vlan, ok := link.(*Vlan); ok {
  644. b := make([]byte, 2)
  645. native.PutUint16(b, uint16(vlan.VlanId))
  646. data := nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_DATA, nil)
  647. nl.NewRtAttrChild(data, nl.IFLA_VLAN_ID, b)
  648. } else if veth, ok := link.(*Veth); ok {
  649. data := nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_DATA, nil)
  650. peer := nl.NewRtAttrChild(data, nl.VETH_INFO_PEER, nil)
  651. nl.NewIfInfomsgChild(peer, syscall.AF_UNSPEC)
  652. nl.NewRtAttrChild(peer, syscall.IFLA_IFNAME, nl.ZeroTerminated(veth.PeerName))
  653. if base.TxQLen >= 0 {
  654. nl.NewRtAttrChild(peer, syscall.IFLA_TXQLEN, nl.Uint32Attr(uint32(base.TxQLen)))
  655. }
  656. if base.MTU > 0 {
  657. nl.NewRtAttrChild(peer, syscall.IFLA_MTU, nl.Uint32Attr(uint32(base.MTU)))
  658. }
  659. } else if vxlan, ok := link.(*Vxlan); ok {
  660. addVxlanAttrs(vxlan, linkInfo)
  661. } else if bond, ok := link.(*Bond); ok {
  662. addBondAttrs(bond, linkInfo)
  663. } else if ipv, ok := link.(*IPVlan); ok {
  664. data := nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_DATA, nil)
  665. nl.NewRtAttrChild(data, nl.IFLA_IPVLAN_MODE, nl.Uint16Attr(uint16(ipv.Mode)))
  666. } else if macv, ok := link.(*Macvlan); ok {
  667. if macv.Mode != MACVLAN_MODE_DEFAULT {
  668. data := nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_DATA, nil)
  669. nl.NewRtAttrChild(data, nl.IFLA_MACVLAN_MODE, nl.Uint32Attr(macvlanModes[macv.Mode]))
  670. }
  671. } else if macv, ok := link.(*Macvtap); ok {
  672. if macv.Mode != MACVLAN_MODE_DEFAULT {
  673. data := nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_DATA, nil)
  674. nl.NewRtAttrChild(data, nl.IFLA_MACVLAN_MODE, nl.Uint32Attr(macvlanModes[macv.Mode]))
  675. }
  676. } else if gretap, ok := link.(*Gretap); ok {
  677. addGretapAttrs(gretap, linkInfo)
  678. } else if iptun, ok := link.(*Iptun); ok {
  679. addIptunAttrs(iptun, linkInfo)
  680. } else if vti, ok := link.(*Vti); ok {
  681. addVtiAttrs(vti, linkInfo)
  682. } else if vrf, ok := link.(*Vrf); ok {
  683. addVrfAttrs(vrf, linkInfo)
  684. }
  685. req.AddData(linkInfo)
  686. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  687. if err != nil {
  688. return err
  689. }
  690. h.ensureIndex(base)
  691. // can't set master during create, so set it afterwards
  692. if base.MasterIndex != 0 {
  693. // TODO: verify MasterIndex is actually a bridge?
  694. return h.LinkSetMasterByIndex(link, base.MasterIndex)
  695. }
  696. return nil
  697. }
  698. // LinkDel deletes link device. Either Index or Name must be set in
  699. // the link object for it to be deleted. The other values are ignored.
  700. // Equivalent to: `ip link del $link`
  701. func LinkDel(link Link) error {
  702. return pkgHandle.LinkDel(link)
  703. }
  704. // LinkDel deletes link device. Either Index or Name must be set in
  705. // the link object for it to be deleted. The other values are ignored.
  706. // Equivalent to: `ip link del $link`
  707. func (h *Handle) LinkDel(link Link) error {
  708. base := link.Attrs()
  709. h.ensureIndex(base)
  710. req := h.newNetlinkRequest(syscall.RTM_DELLINK, syscall.NLM_F_ACK)
  711. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  712. msg.Index = int32(base.Index)
  713. req.AddData(msg)
  714. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  715. return err
  716. }
  717. func (h *Handle) linkByNameDump(name string) (Link, error) {
  718. links, err := h.LinkList()
  719. if err != nil {
  720. return nil, err
  721. }
  722. for _, link := range links {
  723. if link.Attrs().Name == name {
  724. return link, nil
  725. }
  726. }
  727. return nil, fmt.Errorf("Link %s not found", name)
  728. }
  729. func (h *Handle) linkByAliasDump(alias string) (Link, error) {
  730. links, err := h.LinkList()
  731. if err != nil {
  732. return nil, err
  733. }
  734. for _, link := range links {
  735. if link.Attrs().Alias == alias {
  736. return link, nil
  737. }
  738. }
  739. return nil, fmt.Errorf("Link alias %s not found", alias)
  740. }
  741. // LinkByName finds a link by name and returns a pointer to the object.
  742. func LinkByName(name string) (Link, error) {
  743. return pkgHandle.LinkByName(name)
  744. }
  745. // LinkByName finds a link by name and returns a pointer to the object.
  746. func (h *Handle) LinkByName(name string) (Link, error) {
  747. if h.lookupByDump {
  748. return h.linkByNameDump(name)
  749. }
  750. req := h.newNetlinkRequest(syscall.RTM_GETLINK, syscall.NLM_F_ACK)
  751. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  752. req.AddData(msg)
  753. nameData := nl.NewRtAttr(syscall.IFLA_IFNAME, nl.ZeroTerminated(name))
  754. req.AddData(nameData)
  755. link, err := execGetLink(req)
  756. if err == syscall.EINVAL {
  757. // older kernels don't support looking up via IFLA_IFNAME
  758. // so fall back to dumping all links
  759. h.lookupByDump = true
  760. return h.linkByNameDump(name)
  761. }
  762. return link, err
  763. }
  764. // LinkByAlias finds a link by its alias and returns a pointer to the object.
  765. // If there are multiple links with the alias it returns the first one
  766. func LinkByAlias(alias string) (Link, error) {
  767. return pkgHandle.LinkByAlias(alias)
  768. }
  769. // LinkByAlias finds a link by its alias and returns a pointer to the object.
  770. // If there are multiple links with the alias it returns the first one
  771. func (h *Handle) LinkByAlias(alias string) (Link, error) {
  772. if h.lookupByDump {
  773. return h.linkByAliasDump(alias)
  774. }
  775. req := h.newNetlinkRequest(syscall.RTM_GETLINK, syscall.NLM_F_ACK)
  776. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  777. req.AddData(msg)
  778. nameData := nl.NewRtAttr(syscall.IFLA_IFALIAS, nl.ZeroTerminated(alias))
  779. req.AddData(nameData)
  780. link, err := execGetLink(req)
  781. if err == syscall.EINVAL {
  782. // older kernels don't support looking up via IFLA_IFALIAS
  783. // so fall back to dumping all links
  784. h.lookupByDump = true
  785. return h.linkByAliasDump(alias)
  786. }
  787. return link, err
  788. }
  789. // LinkByIndex finds a link by index and returns a pointer to the object.
  790. func LinkByIndex(index int) (Link, error) {
  791. return pkgHandle.LinkByIndex(index)
  792. }
  793. // LinkByIndex finds a link by index and returns a pointer to the object.
  794. func (h *Handle) LinkByIndex(index int) (Link, error) {
  795. req := h.newNetlinkRequest(syscall.RTM_GETLINK, syscall.NLM_F_ACK)
  796. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  797. msg.Index = int32(index)
  798. req.AddData(msg)
  799. return execGetLink(req)
  800. }
  801. func execGetLink(req *nl.NetlinkRequest) (Link, error) {
  802. msgs, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  803. if err != nil {
  804. if errno, ok := err.(syscall.Errno); ok {
  805. if errno == syscall.ENODEV {
  806. return nil, fmt.Errorf("Link not found")
  807. }
  808. }
  809. return nil, err
  810. }
  811. switch {
  812. case len(msgs) == 0:
  813. return nil, fmt.Errorf("Link not found")
  814. case len(msgs) == 1:
  815. return LinkDeserialize(nil, msgs[0])
  816. default:
  817. return nil, fmt.Errorf("More than one link found")
  818. }
  819. }
  820. // linkDeserialize deserializes a raw message received from netlink into
  821. // a link object.
  822. func LinkDeserialize(hdr *syscall.NlMsghdr, m []byte) (Link, error) {
  823. msg := nl.DeserializeIfInfomsg(m)
  824. attrs, err := nl.ParseRouteAttr(m[msg.Len():])
  825. if err != nil {
  826. return nil, err
  827. }
  828. base := LinkAttrs{Index: int(msg.Index), RawFlags: msg.Flags, Flags: linkFlags(msg.Flags), EncapType: msg.EncapType()}
  829. if msg.Flags&syscall.IFF_PROMISC != 0 {
  830. base.Promisc = 1
  831. }
  832. var (
  833. link Link
  834. stats32 []byte
  835. stats64 []byte
  836. linkType string
  837. )
  838. for _, attr := range attrs {
  839. switch attr.Attr.Type {
  840. case syscall.IFLA_LINKINFO:
  841. infos, err := nl.ParseRouteAttr(attr.Value)
  842. if err != nil {
  843. return nil, err
  844. }
  845. for _, info := range infos {
  846. switch info.Attr.Type {
  847. case nl.IFLA_INFO_KIND:
  848. linkType = string(info.Value[:len(info.Value)-1])
  849. switch linkType {
  850. case "dummy":
  851. link = &Dummy{}
  852. case "ifb":
  853. link = &Ifb{}
  854. case "bridge":
  855. link = &Bridge{}
  856. case "vlan":
  857. link = &Vlan{}
  858. case "veth":
  859. link = &Veth{}
  860. case "vxlan":
  861. link = &Vxlan{}
  862. case "bond":
  863. link = &Bond{}
  864. case "ipvlan":
  865. link = &IPVlan{}
  866. case "macvlan":
  867. link = &Macvlan{}
  868. case "macvtap":
  869. link = &Macvtap{}
  870. case "gretap":
  871. link = &Gretap{}
  872. case "ipip":
  873. link = &Iptun{}
  874. case "vti":
  875. link = &Vti{}
  876. case "vrf":
  877. link = &Vrf{}
  878. default:
  879. link = &GenericLink{LinkType: linkType}
  880. }
  881. case nl.IFLA_INFO_DATA:
  882. data, err := nl.ParseRouteAttr(info.Value)
  883. if err != nil {
  884. return nil, err
  885. }
  886. switch linkType {
  887. case "vlan":
  888. parseVlanData(link, data)
  889. case "vxlan":
  890. parseVxlanData(link, data)
  891. case "bond":
  892. parseBondData(link, data)
  893. case "ipvlan":
  894. parseIPVlanData(link, data)
  895. case "macvlan":
  896. parseMacvlanData(link, data)
  897. case "macvtap":
  898. parseMacvtapData(link, data)
  899. case "gretap":
  900. parseGretapData(link, data)
  901. case "ipip":
  902. parseIptunData(link, data)
  903. case "vti":
  904. parseVtiData(link, data)
  905. case "vrf":
  906. parseVrfData(link, data)
  907. }
  908. }
  909. }
  910. case syscall.IFLA_ADDRESS:
  911. var nonzero bool
  912. for _, b := range attr.Value {
  913. if b != 0 {
  914. nonzero = true
  915. }
  916. }
  917. if nonzero {
  918. base.HardwareAddr = attr.Value[:]
  919. }
  920. case syscall.IFLA_IFNAME:
  921. base.Name = string(attr.Value[:len(attr.Value)-1])
  922. case syscall.IFLA_MTU:
  923. base.MTU = int(native.Uint32(attr.Value[0:4]))
  924. case syscall.IFLA_LINK:
  925. base.ParentIndex = int(native.Uint32(attr.Value[0:4]))
  926. case syscall.IFLA_MASTER:
  927. base.MasterIndex = int(native.Uint32(attr.Value[0:4]))
  928. case syscall.IFLA_TXQLEN:
  929. base.TxQLen = int(native.Uint32(attr.Value[0:4]))
  930. case syscall.IFLA_IFALIAS:
  931. base.Alias = string(attr.Value[:len(attr.Value)-1])
  932. case syscall.IFLA_STATS:
  933. stats32 = attr.Value[:]
  934. case IFLA_STATS64:
  935. stats64 = attr.Value[:]
  936. case nl.IFLA_XDP:
  937. xdp, err := parseLinkXdp(attr.Value[:])
  938. if err != nil {
  939. return nil, err
  940. }
  941. base.Xdp = xdp
  942. case syscall.IFLA_PROTINFO | syscall.NLA_F_NESTED:
  943. if hdr != nil && hdr.Type == syscall.RTM_NEWLINK &&
  944. msg.Family == syscall.AF_BRIDGE {
  945. attrs, err := nl.ParseRouteAttr(attr.Value[:])
  946. if err != nil {
  947. return nil, err
  948. }
  949. base.Protinfo = parseProtinfo(attrs)
  950. }
  951. case syscall.IFLA_OPERSTATE:
  952. base.OperState = LinkOperState(uint8(attr.Value[0]))
  953. }
  954. }
  955. if stats64 != nil {
  956. base.Statistics = parseLinkStats64(stats64)
  957. } else if stats32 != nil {
  958. base.Statistics = parseLinkStats32(stats32)
  959. }
  960. // Links that don't have IFLA_INFO_KIND are hardware devices
  961. if link == nil {
  962. link = &Device{}
  963. }
  964. *link.Attrs() = base
  965. return link, nil
  966. }
  967. // LinkList gets a list of link devices.
  968. // Equivalent to: `ip link show`
  969. func LinkList() ([]Link, error) {
  970. return pkgHandle.LinkList()
  971. }
  972. // LinkList gets a list of link devices.
  973. // Equivalent to: `ip link show`
  974. func (h *Handle) LinkList() ([]Link, error) {
  975. // NOTE(vish): This duplicates functionality in net/iface_linux.go, but we need
  976. // to get the message ourselves to parse link type.
  977. req := h.newNetlinkRequest(syscall.RTM_GETLINK, syscall.NLM_F_DUMP)
  978. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  979. req.AddData(msg)
  980. msgs, err := req.Execute(syscall.NETLINK_ROUTE, syscall.RTM_NEWLINK)
  981. if err != nil {
  982. return nil, err
  983. }
  984. var res []Link
  985. for _, m := range msgs {
  986. link, err := LinkDeserialize(nil, m)
  987. if err != nil {
  988. return nil, err
  989. }
  990. res = append(res, link)
  991. }
  992. return res, nil
  993. }
  994. // LinkUpdate is used to pass information back from LinkSubscribe()
  995. type LinkUpdate struct {
  996. nl.IfInfomsg
  997. Header syscall.NlMsghdr
  998. Link
  999. }
  1000. // LinkSubscribe takes a chan down which notifications will be sent
  1001. // when links change. Close the 'done' chan to stop subscription.
  1002. func LinkSubscribe(ch chan<- LinkUpdate, done <-chan struct{}) error {
  1003. return linkSubscribe(netns.None(), netns.None(), ch, done)
  1004. }
  1005. // LinkSubscribeAt works like LinkSubscribe plus it allows the caller
  1006. // to choose the network namespace in which to subscribe (ns).
  1007. func LinkSubscribeAt(ns netns.NsHandle, ch chan<- LinkUpdate, done <-chan struct{}) error {
  1008. return linkSubscribe(ns, netns.None(), ch, done)
  1009. }
  1010. func linkSubscribe(newNs, curNs netns.NsHandle, ch chan<- LinkUpdate, done <-chan struct{}) error {
  1011. s, err := nl.SubscribeAt(newNs, curNs, syscall.NETLINK_ROUTE, syscall.RTNLGRP_LINK)
  1012. if err != nil {
  1013. return err
  1014. }
  1015. if done != nil {
  1016. go func() {
  1017. <-done
  1018. s.Close()
  1019. }()
  1020. }
  1021. go func() {
  1022. defer close(ch)
  1023. for {
  1024. msgs, err := s.Receive()
  1025. if err != nil {
  1026. return
  1027. }
  1028. for _, m := range msgs {
  1029. ifmsg := nl.DeserializeIfInfomsg(m.Data)
  1030. link, err := LinkDeserialize(&m.Header, m.Data)
  1031. if err != nil {
  1032. return
  1033. }
  1034. ch <- LinkUpdate{IfInfomsg: *ifmsg, Header: m.Header, Link: link}
  1035. }
  1036. }
  1037. }()
  1038. return nil
  1039. }
  1040. func LinkSetHairpin(link Link, mode bool) error {
  1041. return pkgHandle.LinkSetHairpin(link, mode)
  1042. }
  1043. func (h *Handle) LinkSetHairpin(link Link, mode bool) error {
  1044. return h.setProtinfoAttr(link, mode, nl.IFLA_BRPORT_MODE)
  1045. }
  1046. func LinkSetGuard(link Link, mode bool) error {
  1047. return pkgHandle.LinkSetGuard(link, mode)
  1048. }
  1049. func (h *Handle) LinkSetGuard(link Link, mode bool) error {
  1050. return h.setProtinfoAttr(link, mode, nl.IFLA_BRPORT_GUARD)
  1051. }
  1052. func LinkSetFastLeave(link Link, mode bool) error {
  1053. return pkgHandle.LinkSetFastLeave(link, mode)
  1054. }
  1055. func (h *Handle) LinkSetFastLeave(link Link, mode bool) error {
  1056. return h.setProtinfoAttr(link, mode, nl.IFLA_BRPORT_FAST_LEAVE)
  1057. }
  1058. func LinkSetLearning(link Link, mode bool) error {
  1059. return pkgHandle.LinkSetLearning(link, mode)
  1060. }
  1061. func (h *Handle) LinkSetLearning(link Link, mode bool) error {
  1062. return h.setProtinfoAttr(link, mode, nl.IFLA_BRPORT_LEARNING)
  1063. }
  1064. func LinkSetRootBlock(link Link, mode bool) error {
  1065. return pkgHandle.LinkSetRootBlock(link, mode)
  1066. }
  1067. func (h *Handle) LinkSetRootBlock(link Link, mode bool) error {
  1068. return h.setProtinfoAttr(link, mode, nl.IFLA_BRPORT_PROTECT)
  1069. }
  1070. func LinkSetFlood(link Link, mode bool) error {
  1071. return pkgHandle.LinkSetFlood(link, mode)
  1072. }
  1073. func (h *Handle) LinkSetFlood(link Link, mode bool) error {
  1074. return h.setProtinfoAttr(link, mode, nl.IFLA_BRPORT_UNICAST_FLOOD)
  1075. }
  1076. func (h *Handle) setProtinfoAttr(link Link, mode bool, attr int) error {
  1077. base := link.Attrs()
  1078. h.ensureIndex(base)
  1079. req := h.newNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
  1080. msg := nl.NewIfInfomsg(syscall.AF_BRIDGE)
  1081. msg.Index = int32(base.Index)
  1082. req.AddData(msg)
  1083. br := nl.NewRtAttr(syscall.IFLA_PROTINFO|syscall.NLA_F_NESTED, nil)
  1084. nl.NewRtAttrChild(br, attr, boolToByte(mode))
  1085. req.AddData(br)
  1086. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  1087. if err != nil {
  1088. return err
  1089. }
  1090. return nil
  1091. }
  1092. func parseVlanData(link Link, data []syscall.NetlinkRouteAttr) {
  1093. vlan := link.(*Vlan)
  1094. for _, datum := range data {
  1095. switch datum.Attr.Type {
  1096. case nl.IFLA_VLAN_ID:
  1097. vlan.VlanId = int(native.Uint16(datum.Value[0:2]))
  1098. }
  1099. }
  1100. }
  1101. func parseVxlanData(link Link, data []syscall.NetlinkRouteAttr) {
  1102. vxlan := link.(*Vxlan)
  1103. for _, datum := range data {
  1104. switch datum.Attr.Type {
  1105. case nl.IFLA_VXLAN_ID:
  1106. vxlan.VxlanId = int(native.Uint32(datum.Value[0:4]))
  1107. case nl.IFLA_VXLAN_LINK:
  1108. vxlan.VtepDevIndex = int(native.Uint32(datum.Value[0:4]))
  1109. case nl.IFLA_VXLAN_LOCAL:
  1110. vxlan.SrcAddr = net.IP(datum.Value[0:4])
  1111. case nl.IFLA_VXLAN_LOCAL6:
  1112. vxlan.SrcAddr = net.IP(datum.Value[0:16])
  1113. case nl.IFLA_VXLAN_GROUP:
  1114. vxlan.Group = net.IP(datum.Value[0:4])
  1115. case nl.IFLA_VXLAN_GROUP6:
  1116. vxlan.Group = net.IP(datum.Value[0:16])
  1117. case nl.IFLA_VXLAN_TTL:
  1118. vxlan.TTL = int(datum.Value[0])
  1119. case nl.IFLA_VXLAN_TOS:
  1120. vxlan.TOS = int(datum.Value[0])
  1121. case nl.IFLA_VXLAN_LEARNING:
  1122. vxlan.Learning = int8(datum.Value[0]) != 0
  1123. case nl.IFLA_VXLAN_PROXY:
  1124. vxlan.Proxy = int8(datum.Value[0]) != 0
  1125. case nl.IFLA_VXLAN_RSC:
  1126. vxlan.RSC = int8(datum.Value[0]) != 0
  1127. case nl.IFLA_VXLAN_L2MISS:
  1128. vxlan.L2miss = int8(datum.Value[0]) != 0
  1129. case nl.IFLA_VXLAN_L3MISS:
  1130. vxlan.L3miss = int8(datum.Value[0]) != 0
  1131. case nl.IFLA_VXLAN_UDP_CSUM:
  1132. vxlan.UDPCSum = int8(datum.Value[0]) != 0
  1133. case nl.IFLA_VXLAN_GBP:
  1134. vxlan.GBP = true
  1135. case nl.IFLA_VXLAN_AGEING:
  1136. vxlan.Age = int(native.Uint32(datum.Value[0:4]))
  1137. vxlan.NoAge = vxlan.Age == 0
  1138. case nl.IFLA_VXLAN_LIMIT:
  1139. vxlan.Limit = int(native.Uint32(datum.Value[0:4]))
  1140. case nl.IFLA_VXLAN_PORT:
  1141. vxlan.Port = int(ntohs(datum.Value[0:2]))
  1142. case nl.IFLA_VXLAN_PORT_RANGE:
  1143. buf := bytes.NewBuffer(datum.Value[0:4])
  1144. var pr vxlanPortRange
  1145. if binary.Read(buf, binary.BigEndian, &pr) != nil {
  1146. vxlan.PortLow = int(pr.Lo)
  1147. vxlan.PortHigh = int(pr.Hi)
  1148. }
  1149. }
  1150. }
  1151. }
  1152. func parseBondData(link Link, data []syscall.NetlinkRouteAttr) {
  1153. bond := NewLinkBond(NewLinkAttrs())
  1154. for i := range data {
  1155. switch data[i].Attr.Type {
  1156. case nl.IFLA_BOND_MODE:
  1157. bond.Mode = BondMode(data[i].Value[0])
  1158. case nl.IFLA_BOND_ACTIVE_SLAVE:
  1159. bond.ActiveSlave = int(native.Uint32(data[i].Value[0:4]))
  1160. case nl.IFLA_BOND_MIIMON:
  1161. bond.Miimon = int(native.Uint32(data[i].Value[0:4]))
  1162. case nl.IFLA_BOND_UPDELAY:
  1163. bond.UpDelay = int(native.Uint32(data[i].Value[0:4]))
  1164. case nl.IFLA_BOND_DOWNDELAY:
  1165. bond.DownDelay = int(native.Uint32(data[i].Value[0:4]))
  1166. case nl.IFLA_BOND_USE_CARRIER:
  1167. bond.UseCarrier = int(data[i].Value[0])
  1168. case nl.IFLA_BOND_ARP_INTERVAL:
  1169. bond.ArpInterval = int(native.Uint32(data[i].Value[0:4]))
  1170. case nl.IFLA_BOND_ARP_IP_TARGET:
  1171. // TODO: implement
  1172. case nl.IFLA_BOND_ARP_VALIDATE:
  1173. bond.ArpValidate = BondArpValidate(native.Uint32(data[i].Value[0:4]))
  1174. case nl.IFLA_BOND_ARP_ALL_TARGETS:
  1175. bond.ArpAllTargets = BondArpAllTargets(native.Uint32(data[i].Value[0:4]))
  1176. case nl.IFLA_BOND_PRIMARY:
  1177. bond.Primary = int(native.Uint32(data[i].Value[0:4]))
  1178. case nl.IFLA_BOND_PRIMARY_RESELECT:
  1179. bond.PrimaryReselect = BondPrimaryReselect(data[i].Value[0])
  1180. case nl.IFLA_BOND_FAIL_OVER_MAC:
  1181. bond.FailOverMac = BondFailOverMac(data[i].Value[0])
  1182. case nl.IFLA_BOND_XMIT_HASH_POLICY:
  1183. bond.XmitHashPolicy = BondXmitHashPolicy(data[i].Value[0])
  1184. case nl.IFLA_BOND_RESEND_IGMP:
  1185. bond.ResendIgmp = int(native.Uint32(data[i].Value[0:4]))
  1186. case nl.IFLA_BOND_NUM_PEER_NOTIF:
  1187. bond.NumPeerNotif = int(data[i].Value[0])
  1188. case nl.IFLA_BOND_ALL_SLAVES_ACTIVE:
  1189. bond.AllSlavesActive = int(data[i].Value[0])
  1190. case nl.IFLA_BOND_MIN_LINKS:
  1191. bond.MinLinks = int(native.Uint32(data[i].Value[0:4]))
  1192. case nl.IFLA_BOND_LP_INTERVAL:
  1193. bond.LpInterval = int(native.Uint32(data[i].Value[0:4]))
  1194. case nl.IFLA_BOND_PACKETS_PER_SLAVE:
  1195. bond.PackersPerSlave = int(native.Uint32(data[i].Value[0:4]))
  1196. case nl.IFLA_BOND_AD_LACP_RATE:
  1197. bond.LacpRate = BondLacpRate(data[i].Value[0])
  1198. case nl.IFLA_BOND_AD_SELECT:
  1199. bond.AdSelect = BondAdSelect(data[i].Value[0])
  1200. case nl.IFLA_BOND_AD_INFO:
  1201. // TODO: implement
  1202. }
  1203. }
  1204. }
  1205. func parseIPVlanData(link Link, data []syscall.NetlinkRouteAttr) {
  1206. ipv := link.(*IPVlan)
  1207. for _, datum := range data {
  1208. if datum.Attr.Type == nl.IFLA_IPVLAN_MODE {
  1209. ipv.Mode = IPVlanMode(native.Uint32(datum.Value[0:4]))
  1210. return
  1211. }
  1212. }
  1213. }
  1214. func parseMacvtapData(link Link, data []syscall.NetlinkRouteAttr) {
  1215. macv := link.(*Macvtap)
  1216. parseMacvlanData(&macv.Macvlan, data)
  1217. }
  1218. func parseMacvlanData(link Link, data []syscall.NetlinkRouteAttr) {
  1219. macv := link.(*Macvlan)
  1220. for _, datum := range data {
  1221. if datum.Attr.Type == nl.IFLA_MACVLAN_MODE {
  1222. switch native.Uint32(datum.Value[0:4]) {
  1223. case nl.MACVLAN_MODE_PRIVATE:
  1224. macv.Mode = MACVLAN_MODE_PRIVATE
  1225. case nl.MACVLAN_MODE_VEPA:
  1226. macv.Mode = MACVLAN_MODE_VEPA
  1227. case nl.MACVLAN_MODE_BRIDGE:
  1228. macv.Mode = MACVLAN_MODE_BRIDGE
  1229. case nl.MACVLAN_MODE_PASSTHRU:
  1230. macv.Mode = MACVLAN_MODE_PASSTHRU
  1231. case nl.MACVLAN_MODE_SOURCE:
  1232. macv.Mode = MACVLAN_MODE_SOURCE
  1233. }
  1234. return
  1235. }
  1236. }
  1237. }
  1238. // copied from pkg/net_linux.go
  1239. func linkFlags(rawFlags uint32) net.Flags {
  1240. var f net.Flags
  1241. if rawFlags&syscall.IFF_UP != 0 {
  1242. f |= net.FlagUp
  1243. }
  1244. if rawFlags&syscall.IFF_BROADCAST != 0 {
  1245. f |= net.FlagBroadcast
  1246. }
  1247. if rawFlags&syscall.IFF_LOOPBACK != 0 {
  1248. f |= net.FlagLoopback
  1249. }
  1250. if rawFlags&syscall.IFF_POINTOPOINT != 0 {
  1251. f |= net.FlagPointToPoint
  1252. }
  1253. if rawFlags&syscall.IFF_MULTICAST != 0 {
  1254. f |= net.FlagMulticast
  1255. }
  1256. return f
  1257. }
  1258. func addGretapAttrs(gretap *Gretap, linkInfo *nl.RtAttr) {
  1259. data := nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_DATA, nil)
  1260. ip := gretap.Local.To4()
  1261. if ip != nil {
  1262. nl.NewRtAttrChild(data, nl.IFLA_GRE_LOCAL, []byte(ip))
  1263. }
  1264. ip = gretap.Remote.To4()
  1265. if ip != nil {
  1266. nl.NewRtAttrChild(data, nl.IFLA_GRE_REMOTE, []byte(ip))
  1267. }
  1268. if gretap.IKey != 0 {
  1269. nl.NewRtAttrChild(data, nl.IFLA_GRE_IKEY, htonl(gretap.IKey))
  1270. gretap.IFlags |= uint16(nl.GRE_KEY)
  1271. }
  1272. if gretap.OKey != 0 {
  1273. nl.NewRtAttrChild(data, nl.IFLA_GRE_OKEY, htonl(gretap.OKey))
  1274. gretap.OFlags |= uint16(nl.GRE_KEY)
  1275. }
  1276. nl.NewRtAttrChild(data, nl.IFLA_GRE_IFLAGS, htons(gretap.IFlags))
  1277. nl.NewRtAttrChild(data, nl.IFLA_GRE_OFLAGS, htons(gretap.OFlags))
  1278. if gretap.Link != 0 {
  1279. nl.NewRtAttrChild(data, nl.IFLA_GRE_LINK, nl.Uint32Attr(gretap.Link))
  1280. }
  1281. nl.NewRtAttrChild(data, nl.IFLA_GRE_PMTUDISC, nl.Uint8Attr(gretap.PMtuDisc))
  1282. nl.NewRtAttrChild(data, nl.IFLA_GRE_TTL, nl.Uint8Attr(gretap.Ttl))
  1283. nl.NewRtAttrChild(data, nl.IFLA_GRE_TOS, nl.Uint8Attr(gretap.Tos))
  1284. nl.NewRtAttrChild(data, nl.IFLA_GRE_ENCAP_TYPE, nl.Uint16Attr(gretap.EncapType))
  1285. nl.NewRtAttrChild(data, nl.IFLA_GRE_ENCAP_FLAGS, nl.Uint16Attr(gretap.EncapFlags))
  1286. nl.NewRtAttrChild(data, nl.IFLA_GRE_ENCAP_SPORT, htons(gretap.EncapSport))
  1287. nl.NewRtAttrChild(data, nl.IFLA_GRE_ENCAP_DPORT, htons(gretap.EncapDport))
  1288. }
  1289. func parseGretapData(link Link, data []syscall.NetlinkRouteAttr) {
  1290. gre := link.(*Gretap)
  1291. for _, datum := range data {
  1292. switch datum.Attr.Type {
  1293. case nl.IFLA_GRE_OKEY:
  1294. gre.IKey = ntohl(datum.Value[0:4])
  1295. case nl.IFLA_GRE_IKEY:
  1296. gre.OKey = ntohl(datum.Value[0:4])
  1297. case nl.IFLA_GRE_LOCAL:
  1298. gre.Local = net.IP(datum.Value[0:4])
  1299. case nl.IFLA_GRE_REMOTE:
  1300. gre.Remote = net.IP(datum.Value[0:4])
  1301. case nl.IFLA_GRE_ENCAP_SPORT:
  1302. gre.EncapSport = ntohs(datum.Value[0:2])
  1303. case nl.IFLA_GRE_ENCAP_DPORT:
  1304. gre.EncapDport = ntohs(datum.Value[0:2])
  1305. case nl.IFLA_GRE_IFLAGS:
  1306. gre.IFlags = ntohs(datum.Value[0:2])
  1307. case nl.IFLA_GRE_OFLAGS:
  1308. gre.OFlags = ntohs(datum.Value[0:2])
  1309. case nl.IFLA_GRE_TTL:
  1310. gre.Ttl = uint8(datum.Value[0])
  1311. case nl.IFLA_GRE_TOS:
  1312. gre.Tos = uint8(datum.Value[0])
  1313. case nl.IFLA_GRE_PMTUDISC:
  1314. gre.PMtuDisc = uint8(datum.Value[0])
  1315. case nl.IFLA_GRE_ENCAP_TYPE:
  1316. gre.EncapType = native.Uint16(datum.Value[0:2])
  1317. case nl.IFLA_GRE_ENCAP_FLAGS:
  1318. gre.EncapFlags = native.Uint16(datum.Value[0:2])
  1319. }
  1320. }
  1321. }
  1322. func parseLinkStats32(data []byte) *LinkStatistics {
  1323. return (*LinkStatistics)((*LinkStatistics32)(unsafe.Pointer(&data[0:SizeofLinkStats32][0])).to64())
  1324. }
  1325. func parseLinkStats64(data []byte) *LinkStatistics {
  1326. return (*LinkStatistics)((*LinkStatistics64)(unsafe.Pointer(&data[0:SizeofLinkStats64][0])))
  1327. }
  1328. func addXdpAttrs(xdp *LinkXdp, req *nl.NetlinkRequest) {
  1329. attrs := nl.NewRtAttr(nl.IFLA_XDP|syscall.NLA_F_NESTED, nil)
  1330. b := make([]byte, 4)
  1331. native.PutUint32(b, uint32(xdp.Fd))
  1332. nl.NewRtAttrChild(attrs, nl.IFLA_XDP_FD, b)
  1333. req.AddData(attrs)
  1334. }
  1335. func parseLinkXdp(data []byte) (*LinkXdp, error) {
  1336. attrs, err := nl.ParseRouteAttr(data)
  1337. if err != nil {
  1338. return nil, err
  1339. }
  1340. xdp := &LinkXdp{}
  1341. for _, attr := range attrs {
  1342. switch attr.Attr.Type {
  1343. case nl.IFLA_XDP_FD:
  1344. xdp.Fd = int(native.Uint32(attr.Value[0:4]))
  1345. case nl.IFLA_XDP_ATTACHED:
  1346. xdp.Attached = attr.Value[0] != 0
  1347. }
  1348. }
  1349. return xdp, nil
  1350. }
  1351. func addIptunAttrs(iptun *Iptun, linkInfo *nl.RtAttr) {
  1352. data := nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_DATA, nil)
  1353. ip := iptun.Local.To4()
  1354. if ip != nil {
  1355. nl.NewRtAttrChild(data, nl.IFLA_IPTUN_LOCAL, []byte(ip))
  1356. }
  1357. ip = iptun.Remote.To4()
  1358. if ip != nil {
  1359. nl.NewRtAttrChild(data, nl.IFLA_IPTUN_REMOTE, []byte(ip))
  1360. }
  1361. if iptun.Link != 0 {
  1362. nl.NewRtAttrChild(data, nl.IFLA_IPTUN_LINK, nl.Uint32Attr(iptun.Link))
  1363. }
  1364. nl.NewRtAttrChild(data, nl.IFLA_IPTUN_PMTUDISC, nl.Uint8Attr(iptun.PMtuDisc))
  1365. nl.NewRtAttrChild(data, nl.IFLA_IPTUN_TTL, nl.Uint8Attr(iptun.Ttl))
  1366. nl.NewRtAttrChild(data, nl.IFLA_IPTUN_TOS, nl.Uint8Attr(iptun.Tos))
  1367. }
  1368. func parseIptunData(link Link, data []syscall.NetlinkRouteAttr) {
  1369. iptun := link.(*Iptun)
  1370. for _, datum := range data {
  1371. switch datum.Attr.Type {
  1372. case nl.IFLA_IPTUN_LOCAL:
  1373. iptun.Local = net.IP(datum.Value[0:4])
  1374. case nl.IFLA_IPTUN_REMOTE:
  1375. iptun.Remote = net.IP(datum.Value[0:4])
  1376. case nl.IFLA_IPTUN_TTL:
  1377. iptun.Ttl = uint8(datum.Value[0])
  1378. case nl.IFLA_IPTUN_TOS:
  1379. iptun.Tos = uint8(datum.Value[0])
  1380. case nl.IFLA_IPTUN_PMTUDISC:
  1381. iptun.PMtuDisc = uint8(datum.Value[0])
  1382. }
  1383. }
  1384. }
  1385. func addVtiAttrs(vti *Vti, linkInfo *nl.RtAttr) {
  1386. data := nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_DATA, nil)
  1387. ip := vti.Local.To4()
  1388. if ip != nil {
  1389. nl.NewRtAttrChild(data, nl.IFLA_VTI_LOCAL, []byte(ip))
  1390. }
  1391. ip = vti.Remote.To4()
  1392. if ip != nil {
  1393. nl.NewRtAttrChild(data, nl.IFLA_VTI_REMOTE, []byte(ip))
  1394. }
  1395. if vti.Link != 0 {
  1396. nl.NewRtAttrChild(data, nl.IFLA_VTI_LINK, nl.Uint32Attr(vti.Link))
  1397. }
  1398. nl.NewRtAttrChild(data, nl.IFLA_VTI_IKEY, htonl(vti.IKey))
  1399. nl.NewRtAttrChild(data, nl.IFLA_VTI_OKEY, htonl(vti.OKey))
  1400. }
  1401. func parseVtiData(link Link, data []syscall.NetlinkRouteAttr) {
  1402. vti := link.(*Vti)
  1403. for _, datum := range data {
  1404. switch datum.Attr.Type {
  1405. case nl.IFLA_VTI_LOCAL:
  1406. vti.Local = net.IP(datum.Value[0:4])
  1407. case nl.IFLA_VTI_REMOTE:
  1408. vti.Remote = net.IP(datum.Value[0:4])
  1409. case nl.IFLA_VTI_IKEY:
  1410. vti.IKey = ntohl(datum.Value[0:4])
  1411. case nl.IFLA_VTI_OKEY:
  1412. vti.OKey = ntohl(datum.Value[0:4])
  1413. }
  1414. }
  1415. }
  1416. func addVrfAttrs(vrf *Vrf, linkInfo *nl.RtAttr) {
  1417. data := nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_DATA, nil)
  1418. b := make([]byte, 4)
  1419. native.PutUint32(b, uint32(vrf.Table))
  1420. nl.NewRtAttrChild(data, nl.IFLA_VRF_TABLE, b)
  1421. }
  1422. func parseVrfData(link Link, data []syscall.NetlinkRouteAttr) {
  1423. vrf := link.(*Vrf)
  1424. for _, datum := range data {
  1425. switch datum.Attr.Type {
  1426. case nl.IFLA_VRF_TABLE:
  1427. vrf.Table = native.Uint32(datum.Value[0:4])
  1428. }
  1429. }
  1430. }