link_linux.go 54 KB

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