link_linux.go 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  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. )
  12. var native = nl.NativeEndian()
  13. var lookupByDump = false
  14. var macvlanModes = [...]uint32{
  15. 0,
  16. nl.MACVLAN_MODE_PRIVATE,
  17. nl.MACVLAN_MODE_VEPA,
  18. nl.MACVLAN_MODE_BRIDGE,
  19. nl.MACVLAN_MODE_PASSTHRU,
  20. nl.MACVLAN_MODE_SOURCE,
  21. }
  22. func ensureIndex(link *LinkAttrs) {
  23. if link != nil && link.Index == 0 {
  24. newlink, _ := LinkByName(link.Name)
  25. if newlink != nil {
  26. link.Index = newlink.Attrs().Index
  27. }
  28. }
  29. }
  30. // LinkSetUp enables the link device.
  31. // Equivalent to: `ip link set $link up`
  32. func LinkSetUp(link Link) error {
  33. base := link.Attrs()
  34. ensureIndex(base)
  35. req := nl.NewNetlinkRequest(syscall.RTM_NEWLINK, syscall.NLM_F_ACK)
  36. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  37. msg.Change = syscall.IFF_UP
  38. msg.Flags = syscall.IFF_UP
  39. msg.Index = int32(base.Index)
  40. req.AddData(msg)
  41. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  42. return err
  43. }
  44. // LinkSetDown disables link device.
  45. // Equivalent to: `ip link set $link down`
  46. func LinkSetDown(link Link) error {
  47. base := link.Attrs()
  48. ensureIndex(base)
  49. req := nl.NewNetlinkRequest(syscall.RTM_NEWLINK, syscall.NLM_F_ACK)
  50. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  51. msg.Change = syscall.IFF_UP
  52. msg.Flags = 0 & ^syscall.IFF_UP
  53. msg.Index = int32(base.Index)
  54. req.AddData(msg)
  55. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  56. return err
  57. }
  58. // LinkSetMTU sets the mtu of the link device.
  59. // Equivalent to: `ip link set $link mtu $mtu`
  60. func LinkSetMTU(link Link, mtu int) error {
  61. base := link.Attrs()
  62. ensureIndex(base)
  63. req := nl.NewNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
  64. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  65. msg.Index = int32(base.Index)
  66. req.AddData(msg)
  67. b := make([]byte, 4)
  68. native.PutUint32(b, uint32(mtu))
  69. data := nl.NewRtAttr(syscall.IFLA_MTU, b)
  70. req.AddData(data)
  71. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  72. return err
  73. }
  74. // LinkSetName sets the name of the link device.
  75. // Equivalent to: `ip link set $link name $name`
  76. func LinkSetName(link Link, name string) error {
  77. base := link.Attrs()
  78. ensureIndex(base)
  79. req := nl.NewNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
  80. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  81. msg.Index = int32(base.Index)
  82. req.AddData(msg)
  83. data := nl.NewRtAttr(syscall.IFLA_IFNAME, []byte(name))
  84. req.AddData(data)
  85. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  86. return err
  87. }
  88. // LinkSetAlias sets the alias of the link device.
  89. // Equivalent to: `ip link set dev $link alias $name`
  90. func LinkSetAlias(link Link, name string) error {
  91. base := link.Attrs()
  92. ensureIndex(base)
  93. req := nl.NewNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
  94. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  95. msg.Index = int32(base.Index)
  96. req.AddData(msg)
  97. data := nl.NewRtAttr(syscall.IFLA_IFALIAS, []byte(name))
  98. req.AddData(data)
  99. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  100. return err
  101. }
  102. // LinkSetHardwareAddr sets the hardware address of the link device.
  103. // Equivalent to: `ip link set $link address $hwaddr`
  104. func LinkSetHardwareAddr(link Link, hwaddr net.HardwareAddr) error {
  105. base := link.Attrs()
  106. ensureIndex(base)
  107. req := nl.NewNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
  108. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  109. msg.Index = int32(base.Index)
  110. req.AddData(msg)
  111. data := nl.NewRtAttr(syscall.IFLA_ADDRESS, []byte(hwaddr))
  112. req.AddData(data)
  113. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  114. return err
  115. }
  116. // LinkSetVfHardwareAddr sets the hardware address of a vf for the link.
  117. // Equivalent to: `ip link set $link vf $vf mac $hwaddr`
  118. func LinkSetVfHardwareAddr(link Link, vf int, hwaddr net.HardwareAddr) error {
  119. base := link.Attrs()
  120. ensureIndex(base)
  121. req := nl.NewNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
  122. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  123. msg.Index = int32(base.Index)
  124. req.AddData(msg)
  125. data := nl.NewRtAttr(nl.IFLA_VFINFO_LIST, nil)
  126. info := nl.NewRtAttrChild(data, nl.IFLA_VF_INFO, nil)
  127. vfmsg := nl.VfMac{
  128. Vf: uint32(vf),
  129. }
  130. copy(vfmsg.Mac[:], []byte(hwaddr))
  131. nl.NewRtAttrChild(info, nl.IFLA_VF_MAC, vfmsg.Serialize())
  132. req.AddData(data)
  133. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  134. return err
  135. }
  136. // LinkSetVfVlan sets the vlan of a vf for the link.
  137. // Equivalent to: `ip link set $link vf $vf vlan $vlan`
  138. func LinkSetVfVlan(link Link, vf, vlan int) error {
  139. base := link.Attrs()
  140. ensureIndex(base)
  141. req := nl.NewNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
  142. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  143. msg.Index = int32(base.Index)
  144. req.AddData(msg)
  145. data := nl.NewRtAttr(nl.IFLA_VFINFO_LIST, nil)
  146. info := nl.NewRtAttrChild(data, nl.IFLA_VF_INFO, nil)
  147. vfmsg := nl.VfVlan{
  148. Vf: uint32(vf),
  149. Vlan: uint32(vlan),
  150. }
  151. nl.NewRtAttrChild(info, nl.IFLA_VF_VLAN, vfmsg.Serialize())
  152. req.AddData(data)
  153. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  154. return err
  155. }
  156. // LinkSetMaster sets the master of the link device.
  157. // Equivalent to: `ip link set $link master $master`
  158. func LinkSetMaster(link Link, master *Bridge) error {
  159. index := 0
  160. if master != nil {
  161. masterBase := master.Attrs()
  162. ensureIndex(masterBase)
  163. index = masterBase.Index
  164. }
  165. if index <= 0 {
  166. return fmt.Errorf("Device does not exist")
  167. }
  168. return LinkSetMasterByIndex(link, index)
  169. }
  170. // LinkSetNoMaster removes the master of the link device.
  171. // Equivalent to: `ip link set $link nomaster`
  172. func LinkSetNoMaster(link Link) error {
  173. return LinkSetMasterByIndex(link, 0)
  174. }
  175. // LinkSetMasterByIndex sets the master of the link device.
  176. // Equivalent to: `ip link set $link master $master`
  177. func LinkSetMasterByIndex(link Link, masterIndex int) error {
  178. base := link.Attrs()
  179. ensureIndex(base)
  180. req := nl.NewNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
  181. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  182. msg.Index = int32(base.Index)
  183. req.AddData(msg)
  184. b := make([]byte, 4)
  185. native.PutUint32(b, uint32(masterIndex))
  186. data := nl.NewRtAttr(syscall.IFLA_MASTER, b)
  187. req.AddData(data)
  188. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  189. return err
  190. }
  191. // LinkSetNsPid puts the device into a new network namespace. The
  192. // pid must be a pid of a running process.
  193. // Equivalent to: `ip link set $link netns $pid`
  194. func LinkSetNsPid(link Link, nspid int) error {
  195. base := link.Attrs()
  196. ensureIndex(base)
  197. req := nl.NewNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
  198. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  199. msg.Index = int32(base.Index)
  200. req.AddData(msg)
  201. b := make([]byte, 4)
  202. native.PutUint32(b, uint32(nspid))
  203. data := nl.NewRtAttr(syscall.IFLA_NET_NS_PID, b)
  204. req.AddData(data)
  205. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  206. return err
  207. }
  208. // LinkSetNsFd puts the device into a new network namespace. The
  209. // fd must be an open file descriptor to a network namespace.
  210. // Similar to: `ip link set $link netns $ns`
  211. func LinkSetNsFd(link Link, fd int) error {
  212. base := link.Attrs()
  213. ensureIndex(base)
  214. req := nl.NewNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
  215. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  216. msg.Index = int32(base.Index)
  217. req.AddData(msg)
  218. b := make([]byte, 4)
  219. native.PutUint32(b, uint32(fd))
  220. data := nl.NewRtAttr(nl.IFLA_NET_NS_FD, b)
  221. req.AddData(data)
  222. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  223. return err
  224. }
  225. func boolAttr(val bool) []byte {
  226. var v uint8
  227. if val {
  228. v = 1
  229. }
  230. return nl.Uint8Attr(v)
  231. }
  232. type vxlanPortRange struct {
  233. Lo, Hi uint16
  234. }
  235. func addVxlanAttrs(vxlan *Vxlan, linkInfo *nl.RtAttr) {
  236. data := nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_DATA, nil)
  237. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_ID, nl.Uint32Attr(uint32(vxlan.VxlanId)))
  238. if vxlan.VtepDevIndex != 0 {
  239. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_LINK, nl.Uint32Attr(uint32(vxlan.VtepDevIndex)))
  240. }
  241. if vxlan.SrcAddr != nil {
  242. ip := vxlan.SrcAddr.To4()
  243. if ip != nil {
  244. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_LOCAL, []byte(ip))
  245. } else {
  246. ip = vxlan.SrcAddr.To16()
  247. if ip != nil {
  248. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_LOCAL6, []byte(ip))
  249. }
  250. }
  251. }
  252. if vxlan.Group != nil {
  253. group := vxlan.Group.To4()
  254. if group != nil {
  255. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_GROUP, []byte(group))
  256. } else {
  257. group = vxlan.Group.To16()
  258. if group != nil {
  259. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_GROUP6, []byte(group))
  260. }
  261. }
  262. }
  263. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_TTL, nl.Uint8Attr(uint8(vxlan.TTL)))
  264. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_TOS, nl.Uint8Attr(uint8(vxlan.TOS)))
  265. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_LEARNING, boolAttr(vxlan.Learning))
  266. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_PROXY, boolAttr(vxlan.Proxy))
  267. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_RSC, boolAttr(vxlan.RSC))
  268. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_L2MISS, boolAttr(vxlan.L2miss))
  269. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_L3MISS, boolAttr(vxlan.L3miss))
  270. if vxlan.UDPCSum {
  271. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_UDP_CSUM, boolAttr(vxlan.UDPCSum))
  272. }
  273. if vxlan.GBP {
  274. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_GBP, boolAttr(vxlan.GBP))
  275. }
  276. if vxlan.NoAge {
  277. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_AGEING, nl.Uint32Attr(0))
  278. } else if vxlan.Age > 0 {
  279. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_AGEING, nl.Uint32Attr(uint32(vxlan.Age)))
  280. }
  281. if vxlan.Limit > 0 {
  282. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_LIMIT, nl.Uint32Attr(uint32(vxlan.Limit)))
  283. }
  284. if vxlan.Port > 0 {
  285. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_PORT, nl.Uint16Attr(uint16(vxlan.Port)))
  286. }
  287. if vxlan.PortLow > 0 || vxlan.PortHigh > 0 {
  288. pr := vxlanPortRange{uint16(vxlan.PortLow), uint16(vxlan.PortHigh)}
  289. buf := new(bytes.Buffer)
  290. binary.Write(buf, binary.BigEndian, &pr)
  291. nl.NewRtAttrChild(data, nl.IFLA_VXLAN_PORT_RANGE, buf.Bytes())
  292. }
  293. }
  294. func addBondAttrs(bond *Bond, linkInfo *nl.RtAttr) {
  295. data := nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_DATA, nil)
  296. if bond.Mode >= 0 {
  297. nl.NewRtAttrChild(data, nl.IFLA_BOND_MODE, nl.Uint8Attr(uint8(bond.Mode)))
  298. }
  299. if bond.ActiveSlave >= 0 {
  300. nl.NewRtAttrChild(data, nl.IFLA_BOND_ACTIVE_SLAVE, nl.Uint32Attr(uint32(bond.ActiveSlave)))
  301. }
  302. if bond.Miimon >= 0 {
  303. nl.NewRtAttrChild(data, nl.IFLA_BOND_MIIMON, nl.Uint32Attr(uint32(bond.Miimon)))
  304. }
  305. if bond.UpDelay >= 0 {
  306. nl.NewRtAttrChild(data, nl.IFLA_BOND_UPDELAY, nl.Uint32Attr(uint32(bond.UpDelay)))
  307. }
  308. if bond.DownDelay >= 0 {
  309. nl.NewRtAttrChild(data, nl.IFLA_BOND_DOWNDELAY, nl.Uint32Attr(uint32(bond.DownDelay)))
  310. }
  311. if bond.UseCarrier >= 0 {
  312. nl.NewRtAttrChild(data, nl.IFLA_BOND_USE_CARRIER, nl.Uint8Attr(uint8(bond.UseCarrier)))
  313. }
  314. if bond.ArpInterval >= 0 {
  315. nl.NewRtAttrChild(data, nl.IFLA_BOND_ARP_INTERVAL, nl.Uint32Attr(uint32(bond.ArpInterval)))
  316. }
  317. if bond.ArpIpTargets != nil {
  318. msg := nl.NewRtAttrChild(data, nl.IFLA_BOND_ARP_IP_TARGET, nil)
  319. for i := range bond.ArpIpTargets {
  320. ip := bond.ArpIpTargets[i].To4()
  321. if ip != nil {
  322. nl.NewRtAttrChild(msg, i, []byte(ip))
  323. continue
  324. }
  325. ip = bond.ArpIpTargets[i].To16()
  326. if ip != nil {
  327. nl.NewRtAttrChild(msg, i, []byte(ip))
  328. }
  329. }
  330. }
  331. if bond.ArpValidate >= 0 {
  332. nl.NewRtAttrChild(data, nl.IFLA_BOND_ARP_VALIDATE, nl.Uint32Attr(uint32(bond.ArpValidate)))
  333. }
  334. if bond.ArpAllTargets >= 0 {
  335. nl.NewRtAttrChild(data, nl.IFLA_BOND_ARP_ALL_TARGETS, nl.Uint32Attr(uint32(bond.ArpAllTargets)))
  336. }
  337. if bond.Primary >= 0 {
  338. nl.NewRtAttrChild(data, nl.IFLA_BOND_PRIMARY, nl.Uint32Attr(uint32(bond.Primary)))
  339. }
  340. if bond.PrimaryReselect >= 0 {
  341. nl.NewRtAttrChild(data, nl.IFLA_BOND_PRIMARY_RESELECT, nl.Uint8Attr(uint8(bond.PrimaryReselect)))
  342. }
  343. if bond.FailOverMac >= 0 {
  344. nl.NewRtAttrChild(data, nl.IFLA_BOND_FAIL_OVER_MAC, nl.Uint8Attr(uint8(bond.FailOverMac)))
  345. }
  346. if bond.XmitHashPolicy >= 0 {
  347. nl.NewRtAttrChild(data, nl.IFLA_BOND_XMIT_HASH_POLICY, nl.Uint8Attr(uint8(bond.XmitHashPolicy)))
  348. }
  349. if bond.ResendIgmp >= 0 {
  350. nl.NewRtAttrChild(data, nl.IFLA_BOND_RESEND_IGMP, nl.Uint32Attr(uint32(bond.ResendIgmp)))
  351. }
  352. if bond.NumPeerNotif >= 0 {
  353. nl.NewRtAttrChild(data, nl.IFLA_BOND_NUM_PEER_NOTIF, nl.Uint8Attr(uint8(bond.NumPeerNotif)))
  354. }
  355. if bond.AllSlavesActive >= 0 {
  356. nl.NewRtAttrChild(data, nl.IFLA_BOND_ALL_SLAVES_ACTIVE, nl.Uint8Attr(uint8(bond.AllSlavesActive)))
  357. }
  358. if bond.MinLinks >= 0 {
  359. nl.NewRtAttrChild(data, nl.IFLA_BOND_MIN_LINKS, nl.Uint32Attr(uint32(bond.MinLinks)))
  360. }
  361. if bond.LpInterval >= 0 {
  362. nl.NewRtAttrChild(data, nl.IFLA_BOND_LP_INTERVAL, nl.Uint32Attr(uint32(bond.LpInterval)))
  363. }
  364. if bond.PackersPerSlave >= 0 {
  365. nl.NewRtAttrChild(data, nl.IFLA_BOND_PACKETS_PER_SLAVE, nl.Uint32Attr(uint32(bond.PackersPerSlave)))
  366. }
  367. if bond.LacpRate >= 0 {
  368. nl.NewRtAttrChild(data, nl.IFLA_BOND_AD_LACP_RATE, nl.Uint8Attr(uint8(bond.LacpRate)))
  369. }
  370. if bond.AdSelect >= 0 {
  371. nl.NewRtAttrChild(data, nl.IFLA_BOND_AD_SELECT, nl.Uint8Attr(uint8(bond.AdSelect)))
  372. }
  373. }
  374. // LinkAdd adds a new link device. The type and features of the device
  375. // are taken fromt the parameters in the link object.
  376. // Equivalent to: `ip link add $link`
  377. func LinkAdd(link Link) error {
  378. // TODO: set mtu and hardware address
  379. // TODO: support extra data for macvlan
  380. base := link.Attrs()
  381. if base.Name == "" {
  382. return fmt.Errorf("LinkAttrs.Name cannot be empty!")
  383. }
  384. if tuntap, ok := link.(*Tuntap); ok {
  385. // TODO: support user
  386. // TODO: support group
  387. // TODO: support non- one_queue
  388. // TODO: support pi | vnet_hdr | multi_queue
  389. // TODO: support non- exclusive
  390. // TODO: support non- persistent
  391. if tuntap.Mode < syscall.IFF_TUN || tuntap.Mode > syscall.IFF_TAP {
  392. return fmt.Errorf("Tuntap.Mode %v unknown!", tuntap.Mode)
  393. }
  394. file, err := os.OpenFile("/dev/net/tun", os.O_RDWR, 0)
  395. if err != nil {
  396. return err
  397. }
  398. defer file.Close()
  399. var req ifReq
  400. req.Flags |= syscall.IFF_ONE_QUEUE
  401. req.Flags |= syscall.IFF_TUN_EXCL
  402. copy(req.Name[:15], base.Name)
  403. req.Flags |= uint16(tuntap.Mode)
  404. _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, file.Fd(), uintptr(syscall.TUNSETIFF), uintptr(unsafe.Pointer(&req)))
  405. if errno != 0 {
  406. return fmt.Errorf("Tuntap IOCTL TUNSETIFF failed, errno %v", errno)
  407. }
  408. _, _, errno = syscall.Syscall(syscall.SYS_IOCTL, file.Fd(), uintptr(syscall.TUNSETPERSIST), 1)
  409. if errno != 0 {
  410. return fmt.Errorf("Tuntap IOCTL TUNSETPERSIST failed, errno %v", errno)
  411. }
  412. ensureIndex(base)
  413. // can't set master during create, so set it afterwards
  414. if base.MasterIndex != 0 {
  415. // TODO: verify MasterIndex is actually a bridge?
  416. return LinkSetMasterByIndex(link, base.MasterIndex)
  417. }
  418. return nil
  419. }
  420. req := nl.NewNetlinkRequest(syscall.RTM_NEWLINK, syscall.NLM_F_CREATE|syscall.NLM_F_EXCL|syscall.NLM_F_ACK)
  421. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  422. // TODO: make it shorter
  423. if base.Flags&net.FlagUp != 0 {
  424. msg.Change = syscall.IFF_UP
  425. msg.Flags = syscall.IFF_UP
  426. }
  427. if base.Flags&net.FlagBroadcast != 0 {
  428. msg.Change |= syscall.IFF_BROADCAST
  429. msg.Flags |= syscall.IFF_BROADCAST
  430. }
  431. if base.Flags&net.FlagLoopback != 0 {
  432. msg.Change |= syscall.IFF_LOOPBACK
  433. msg.Flags |= syscall.IFF_LOOPBACK
  434. }
  435. if base.Flags&net.FlagPointToPoint != 0 {
  436. msg.Change |= syscall.IFF_POINTOPOINT
  437. msg.Flags |= syscall.IFF_POINTOPOINT
  438. }
  439. if base.Flags&net.FlagMulticast != 0 {
  440. msg.Change |= syscall.IFF_MULTICAST
  441. msg.Flags |= syscall.IFF_MULTICAST
  442. }
  443. req.AddData(msg)
  444. if base.ParentIndex != 0 {
  445. b := make([]byte, 4)
  446. native.PutUint32(b, uint32(base.ParentIndex))
  447. data := nl.NewRtAttr(syscall.IFLA_LINK, b)
  448. req.AddData(data)
  449. } else if link.Type() == "ipvlan" {
  450. return fmt.Errorf("Can't create ipvlan link without ParentIndex")
  451. }
  452. nameData := nl.NewRtAttr(syscall.IFLA_IFNAME, nl.ZeroTerminated(base.Name))
  453. req.AddData(nameData)
  454. if base.MTU > 0 {
  455. mtu := nl.NewRtAttr(syscall.IFLA_MTU, nl.Uint32Attr(uint32(base.MTU)))
  456. req.AddData(mtu)
  457. }
  458. if base.TxQLen >= 0 {
  459. qlen := nl.NewRtAttr(syscall.IFLA_TXQLEN, nl.Uint32Attr(uint32(base.TxQLen)))
  460. req.AddData(qlen)
  461. }
  462. if base.Namespace != nil {
  463. var attr *nl.RtAttr
  464. switch base.Namespace.(type) {
  465. case NsPid:
  466. val := nl.Uint32Attr(uint32(base.Namespace.(NsPid)))
  467. attr = nl.NewRtAttr(syscall.IFLA_NET_NS_PID, val)
  468. case NsFd:
  469. val := nl.Uint32Attr(uint32(base.Namespace.(NsFd)))
  470. attr = nl.NewRtAttr(nl.IFLA_NET_NS_FD, val)
  471. }
  472. req.AddData(attr)
  473. }
  474. linkInfo := nl.NewRtAttr(syscall.IFLA_LINKINFO, nil)
  475. nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_KIND, nl.NonZeroTerminated(link.Type()))
  476. if vlan, ok := link.(*Vlan); ok {
  477. b := make([]byte, 2)
  478. native.PutUint16(b, uint16(vlan.VlanId))
  479. data := nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_DATA, nil)
  480. nl.NewRtAttrChild(data, nl.IFLA_VLAN_ID, b)
  481. } else if veth, ok := link.(*Veth); ok {
  482. data := nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_DATA, nil)
  483. peer := nl.NewRtAttrChild(data, nl.VETH_INFO_PEER, nil)
  484. nl.NewIfInfomsgChild(peer, syscall.AF_UNSPEC)
  485. nl.NewRtAttrChild(peer, syscall.IFLA_IFNAME, nl.ZeroTerminated(veth.PeerName))
  486. if base.TxQLen >= 0 {
  487. nl.NewRtAttrChild(peer, syscall.IFLA_TXQLEN, nl.Uint32Attr(uint32(base.TxQLen)))
  488. }
  489. if base.MTU > 0 {
  490. nl.NewRtAttrChild(peer, syscall.IFLA_MTU, nl.Uint32Attr(uint32(base.MTU)))
  491. }
  492. } else if vxlan, ok := link.(*Vxlan); ok {
  493. addVxlanAttrs(vxlan, linkInfo)
  494. } else if bond, ok := link.(*Bond); ok {
  495. addBondAttrs(bond, linkInfo)
  496. } else if ipv, ok := link.(*IPVlan); ok {
  497. data := nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_DATA, nil)
  498. nl.NewRtAttrChild(data, nl.IFLA_IPVLAN_MODE, nl.Uint16Attr(uint16(ipv.Mode)))
  499. } else if macv, ok := link.(*Macvlan); ok {
  500. if macv.Mode != MACVLAN_MODE_DEFAULT {
  501. data := nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_DATA, nil)
  502. nl.NewRtAttrChild(data, nl.IFLA_MACVLAN_MODE, nl.Uint32Attr(macvlanModes[macv.Mode]))
  503. }
  504. } else if gretap, ok := link.(*Gretap); ok {
  505. addGretapAttrs(gretap, linkInfo)
  506. }
  507. req.AddData(linkInfo)
  508. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  509. if err != nil {
  510. return err
  511. }
  512. ensureIndex(base)
  513. // can't set master during create, so set it afterwards
  514. if base.MasterIndex != 0 {
  515. // TODO: verify MasterIndex is actually a bridge?
  516. return LinkSetMasterByIndex(link, base.MasterIndex)
  517. }
  518. return nil
  519. }
  520. // LinkDel deletes link device. Either Index or Name must be set in
  521. // the link object for it to be deleted. The other values are ignored.
  522. // Equivalent to: `ip link del $link`
  523. func LinkDel(link Link) error {
  524. base := link.Attrs()
  525. ensureIndex(base)
  526. req := nl.NewNetlinkRequest(syscall.RTM_DELLINK, syscall.NLM_F_ACK)
  527. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  528. msg.Index = int32(base.Index)
  529. req.AddData(msg)
  530. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  531. return err
  532. }
  533. func linkByNameDump(name string) (Link, error) {
  534. links, err := LinkList()
  535. if err != nil {
  536. return nil, err
  537. }
  538. for _, link := range links {
  539. if link.Attrs().Name == name {
  540. return link, nil
  541. }
  542. }
  543. return nil, fmt.Errorf("Link %s not found", name)
  544. }
  545. func linkByAliasDump(alias string) (Link, error) {
  546. links, err := LinkList()
  547. if err != nil {
  548. return nil, err
  549. }
  550. for _, link := range links {
  551. if link.Attrs().Alias == alias {
  552. return link, nil
  553. }
  554. }
  555. return nil, fmt.Errorf("Link alias %s not found", alias)
  556. }
  557. // LinkByName finds a link by name and returns a pointer to the object.
  558. func LinkByName(name string) (Link, error) {
  559. if lookupByDump {
  560. return linkByNameDump(name)
  561. }
  562. req := nl.NewNetlinkRequest(syscall.RTM_GETLINK, syscall.NLM_F_ACK)
  563. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  564. req.AddData(msg)
  565. nameData := nl.NewRtAttr(syscall.IFLA_IFNAME, nl.ZeroTerminated(name))
  566. req.AddData(nameData)
  567. link, err := execGetLink(req)
  568. if err == syscall.EINVAL {
  569. // older kernels don't support looking up via IFLA_IFNAME
  570. // so fall back to dumping all links
  571. lookupByDump = true
  572. return linkByNameDump(name)
  573. }
  574. return link, err
  575. }
  576. // LinkByAlias finds a link by its alias and returns a pointer to the object.
  577. // If there are multiple links with the alias it returns the first one
  578. func LinkByAlias(alias string) (Link, error) {
  579. if lookupByDump {
  580. return linkByAliasDump(alias)
  581. }
  582. req := nl.NewNetlinkRequest(syscall.RTM_GETLINK, syscall.NLM_F_ACK)
  583. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  584. req.AddData(msg)
  585. nameData := nl.NewRtAttr(syscall.IFLA_IFALIAS, nl.ZeroTerminated(alias))
  586. req.AddData(nameData)
  587. link, err := execGetLink(req)
  588. if err == syscall.EINVAL {
  589. // older kernels don't support looking up via IFLA_IFALIAS
  590. // so fall back to dumping all links
  591. lookupByDump = true
  592. return linkByAliasDump(alias)
  593. }
  594. return link, err
  595. }
  596. // LinkByIndex finds a link by index and returns a pointer to the object.
  597. func LinkByIndex(index int) (Link, error) {
  598. req := nl.NewNetlinkRequest(syscall.RTM_GETLINK, syscall.NLM_F_ACK)
  599. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  600. msg.Index = int32(index)
  601. req.AddData(msg)
  602. return execGetLink(req)
  603. }
  604. func execGetLink(req *nl.NetlinkRequest) (Link, error) {
  605. msgs, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  606. if err != nil {
  607. if errno, ok := err.(syscall.Errno); ok {
  608. if errno == syscall.ENODEV {
  609. return nil, fmt.Errorf("Link not found")
  610. }
  611. }
  612. return nil, err
  613. }
  614. switch {
  615. case len(msgs) == 0:
  616. return nil, fmt.Errorf("Link not found")
  617. case len(msgs) == 1:
  618. return linkDeserialize(msgs[0])
  619. default:
  620. return nil, fmt.Errorf("More than one link found")
  621. }
  622. }
  623. // linkDeserialize deserializes a raw message received from netlink into
  624. // a link object.
  625. func linkDeserialize(m []byte) (Link, error) {
  626. msg := nl.DeserializeIfInfomsg(m)
  627. attrs, err := nl.ParseRouteAttr(m[msg.Len():])
  628. if err != nil {
  629. return nil, err
  630. }
  631. base := LinkAttrs{Index: int(msg.Index), Flags: linkFlags(msg.Flags)}
  632. var link Link
  633. linkType := ""
  634. for _, attr := range attrs {
  635. switch attr.Attr.Type {
  636. case syscall.IFLA_LINKINFO:
  637. infos, err := nl.ParseRouteAttr(attr.Value)
  638. if err != nil {
  639. return nil, err
  640. }
  641. for _, info := range infos {
  642. switch info.Attr.Type {
  643. case nl.IFLA_INFO_KIND:
  644. linkType = string(info.Value[:len(info.Value)-1])
  645. switch linkType {
  646. case "dummy":
  647. link = &Dummy{}
  648. case "ifb":
  649. link = &Ifb{}
  650. case "bridge":
  651. link = &Bridge{}
  652. case "vlan":
  653. link = &Vlan{}
  654. case "veth":
  655. link = &Veth{}
  656. case "vxlan":
  657. link = &Vxlan{}
  658. case "bond":
  659. link = &Bond{}
  660. case "ipvlan":
  661. link = &IPVlan{}
  662. case "macvlan":
  663. link = &Macvlan{}
  664. case "macvtap":
  665. link = &Macvtap{}
  666. case "gretap":
  667. link = &Gretap{}
  668. default:
  669. link = &GenericLink{LinkType: linkType}
  670. }
  671. case nl.IFLA_INFO_DATA:
  672. data, err := nl.ParseRouteAttr(info.Value)
  673. if err != nil {
  674. return nil, err
  675. }
  676. switch linkType {
  677. case "vlan":
  678. parseVlanData(link, data)
  679. case "vxlan":
  680. parseVxlanData(link, data)
  681. case "bond":
  682. parseBondData(link, data)
  683. case "ipvlan":
  684. parseIPVlanData(link, data)
  685. case "macvlan":
  686. parseMacvlanData(link, data)
  687. case "macvtap":
  688. parseMacvtapData(link, data)
  689. case "gretap":
  690. parseGretapData(link, data)
  691. }
  692. }
  693. }
  694. case syscall.IFLA_ADDRESS:
  695. var nonzero bool
  696. for _, b := range attr.Value {
  697. if b != 0 {
  698. nonzero = true
  699. }
  700. }
  701. if nonzero {
  702. base.HardwareAddr = attr.Value[:]
  703. }
  704. case syscall.IFLA_IFNAME:
  705. base.Name = string(attr.Value[:len(attr.Value)-1])
  706. case syscall.IFLA_MTU:
  707. base.MTU = int(native.Uint32(attr.Value[0:4]))
  708. case syscall.IFLA_LINK:
  709. base.ParentIndex = int(native.Uint32(attr.Value[0:4]))
  710. case syscall.IFLA_MASTER:
  711. base.MasterIndex = int(native.Uint32(attr.Value[0:4]))
  712. case syscall.IFLA_TXQLEN:
  713. base.TxQLen = int(native.Uint32(attr.Value[0:4]))
  714. case syscall.IFLA_IFALIAS:
  715. base.Alias = string(attr.Value[:len(attr.Value)-1])
  716. }
  717. }
  718. // Links that don't have IFLA_INFO_KIND are hardware devices
  719. if link == nil {
  720. link = &Device{}
  721. }
  722. *link.Attrs() = base
  723. return link, nil
  724. }
  725. // LinkList gets a list of link devices.
  726. // Equivalent to: `ip link show`
  727. func LinkList() ([]Link, error) {
  728. // NOTE(vish): This duplicates functionality in net/iface_linux.go, but we need
  729. // to get the message ourselves to parse link type.
  730. req := nl.NewNetlinkRequest(syscall.RTM_GETLINK, syscall.NLM_F_DUMP)
  731. msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
  732. req.AddData(msg)
  733. msgs, err := req.Execute(syscall.NETLINK_ROUTE, syscall.RTM_NEWLINK)
  734. if err != nil {
  735. return nil, err
  736. }
  737. var res []Link
  738. for _, m := range msgs {
  739. link, err := linkDeserialize(m)
  740. if err != nil {
  741. return nil, err
  742. }
  743. res = append(res, link)
  744. }
  745. return res, nil
  746. }
  747. // LinkUpdate is used to pass information back from LinkSubscribe()
  748. type LinkUpdate struct {
  749. nl.IfInfomsg
  750. Header syscall.NlMsghdr
  751. Link
  752. }
  753. // LinkSubscribe takes a chan down which notifications will be sent
  754. // when links change. Close the 'done' chan to stop subscription.
  755. func LinkSubscribe(ch chan<- LinkUpdate, done <-chan struct{}) error {
  756. s, err := nl.Subscribe(syscall.NETLINK_ROUTE, syscall.RTNLGRP_LINK)
  757. if err != nil {
  758. return err
  759. }
  760. if done != nil {
  761. go func() {
  762. <-done
  763. s.Close()
  764. }()
  765. }
  766. go func() {
  767. defer close(ch)
  768. for {
  769. msgs, err := s.Receive()
  770. if err != nil {
  771. return
  772. }
  773. for _, m := range msgs {
  774. ifmsg := nl.DeserializeIfInfomsg(m.Data)
  775. link, err := linkDeserialize(m.Data)
  776. if err != nil {
  777. return
  778. }
  779. ch <- LinkUpdate{IfInfomsg: *ifmsg, Header: m.Header, Link: link}
  780. }
  781. }
  782. }()
  783. return nil
  784. }
  785. func LinkSetHairpin(link Link, mode bool) error {
  786. return setProtinfoAttr(link, mode, nl.IFLA_BRPORT_MODE)
  787. }
  788. func LinkSetGuard(link Link, mode bool) error {
  789. return setProtinfoAttr(link, mode, nl.IFLA_BRPORT_GUARD)
  790. }
  791. func LinkSetFastLeave(link Link, mode bool) error {
  792. return setProtinfoAttr(link, mode, nl.IFLA_BRPORT_FAST_LEAVE)
  793. }
  794. func LinkSetLearning(link Link, mode bool) error {
  795. return setProtinfoAttr(link, mode, nl.IFLA_BRPORT_LEARNING)
  796. }
  797. func LinkSetRootBlock(link Link, mode bool) error {
  798. return setProtinfoAttr(link, mode, nl.IFLA_BRPORT_PROTECT)
  799. }
  800. func LinkSetFlood(link Link, mode bool) error {
  801. return setProtinfoAttr(link, mode, nl.IFLA_BRPORT_UNICAST_FLOOD)
  802. }
  803. func setProtinfoAttr(link Link, mode bool, attr int) error {
  804. base := link.Attrs()
  805. ensureIndex(base)
  806. req := nl.NewNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
  807. msg := nl.NewIfInfomsg(syscall.AF_BRIDGE)
  808. msg.Index = int32(base.Index)
  809. req.AddData(msg)
  810. br := nl.NewRtAttr(syscall.IFLA_PROTINFO|syscall.NLA_F_NESTED, nil)
  811. nl.NewRtAttrChild(br, attr, boolToByte(mode))
  812. req.AddData(br)
  813. _, err := req.Execute(syscall.NETLINK_ROUTE, 0)
  814. if err != nil {
  815. return err
  816. }
  817. return nil
  818. }
  819. func parseVlanData(link Link, data []syscall.NetlinkRouteAttr) {
  820. vlan := link.(*Vlan)
  821. for _, datum := range data {
  822. switch datum.Attr.Type {
  823. case nl.IFLA_VLAN_ID:
  824. vlan.VlanId = int(native.Uint16(datum.Value[0:2]))
  825. }
  826. }
  827. }
  828. func parseVxlanData(link Link, data []syscall.NetlinkRouteAttr) {
  829. vxlan := link.(*Vxlan)
  830. for _, datum := range data {
  831. switch datum.Attr.Type {
  832. case nl.IFLA_VXLAN_ID:
  833. vxlan.VxlanId = int(native.Uint32(datum.Value[0:4]))
  834. case nl.IFLA_VXLAN_LINK:
  835. vxlan.VtepDevIndex = int(native.Uint32(datum.Value[0:4]))
  836. case nl.IFLA_VXLAN_LOCAL:
  837. vxlan.SrcAddr = net.IP(datum.Value[0:4])
  838. case nl.IFLA_VXLAN_LOCAL6:
  839. vxlan.SrcAddr = net.IP(datum.Value[0:16])
  840. case nl.IFLA_VXLAN_GROUP:
  841. vxlan.Group = net.IP(datum.Value[0:4])
  842. case nl.IFLA_VXLAN_GROUP6:
  843. vxlan.Group = net.IP(datum.Value[0:16])
  844. case nl.IFLA_VXLAN_TTL:
  845. vxlan.TTL = int(datum.Value[0])
  846. case nl.IFLA_VXLAN_TOS:
  847. vxlan.TOS = int(datum.Value[0])
  848. case nl.IFLA_VXLAN_LEARNING:
  849. vxlan.Learning = int8(datum.Value[0]) != 0
  850. case nl.IFLA_VXLAN_PROXY:
  851. vxlan.Proxy = int8(datum.Value[0]) != 0
  852. case nl.IFLA_VXLAN_RSC:
  853. vxlan.RSC = int8(datum.Value[0]) != 0
  854. case nl.IFLA_VXLAN_L2MISS:
  855. vxlan.L2miss = int8(datum.Value[0]) != 0
  856. case nl.IFLA_VXLAN_L3MISS:
  857. vxlan.L3miss = int8(datum.Value[0]) != 0
  858. case nl.IFLA_VXLAN_UDP_CSUM:
  859. vxlan.UDPCSum = int8(datum.Value[0]) != 0
  860. case nl.IFLA_VXLAN_GBP:
  861. vxlan.GBP = int8(datum.Value[0]) != 0
  862. case nl.IFLA_VXLAN_AGEING:
  863. vxlan.Age = int(native.Uint32(datum.Value[0:4]))
  864. vxlan.NoAge = vxlan.Age == 0
  865. case nl.IFLA_VXLAN_LIMIT:
  866. vxlan.Limit = int(native.Uint32(datum.Value[0:4]))
  867. case nl.IFLA_VXLAN_PORT:
  868. vxlan.Port = int(native.Uint16(datum.Value[0:2]))
  869. case nl.IFLA_VXLAN_PORT_RANGE:
  870. buf := bytes.NewBuffer(datum.Value[0:4])
  871. var pr vxlanPortRange
  872. if binary.Read(buf, binary.BigEndian, &pr) != nil {
  873. vxlan.PortLow = int(pr.Lo)
  874. vxlan.PortHigh = int(pr.Hi)
  875. }
  876. }
  877. }
  878. }
  879. func parseBondData(link Link, data []syscall.NetlinkRouteAttr) {
  880. bond := NewLinkBond(NewLinkAttrs())
  881. for i := range data {
  882. switch data[i].Attr.Type {
  883. case nl.IFLA_BOND_MODE:
  884. bond.Mode = BondMode(data[i].Value[0])
  885. case nl.IFLA_BOND_ACTIVE_SLAVE:
  886. bond.ActiveSlave = int(native.Uint32(data[i].Value[0:4]))
  887. case nl.IFLA_BOND_MIIMON:
  888. bond.Miimon = int(native.Uint32(data[i].Value[0:4]))
  889. case nl.IFLA_BOND_UPDELAY:
  890. bond.UpDelay = int(native.Uint32(data[i].Value[0:4]))
  891. case nl.IFLA_BOND_DOWNDELAY:
  892. bond.DownDelay = int(native.Uint32(data[i].Value[0:4]))
  893. case nl.IFLA_BOND_USE_CARRIER:
  894. bond.UseCarrier = int(data[i].Value[0])
  895. case nl.IFLA_BOND_ARP_INTERVAL:
  896. bond.ArpInterval = int(native.Uint32(data[i].Value[0:4]))
  897. case nl.IFLA_BOND_ARP_IP_TARGET:
  898. // TODO: implement
  899. case nl.IFLA_BOND_ARP_VALIDATE:
  900. bond.ArpValidate = BondArpValidate(native.Uint32(data[i].Value[0:4]))
  901. case nl.IFLA_BOND_ARP_ALL_TARGETS:
  902. bond.ArpAllTargets = BondArpAllTargets(native.Uint32(data[i].Value[0:4]))
  903. case nl.IFLA_BOND_PRIMARY:
  904. bond.Primary = int(native.Uint32(data[i].Value[0:4]))
  905. case nl.IFLA_BOND_PRIMARY_RESELECT:
  906. bond.PrimaryReselect = BondPrimaryReselect(data[i].Value[0])
  907. case nl.IFLA_BOND_FAIL_OVER_MAC:
  908. bond.FailOverMac = BondFailOverMac(data[i].Value[0])
  909. case nl.IFLA_BOND_XMIT_HASH_POLICY:
  910. bond.XmitHashPolicy = BondXmitHashPolicy(data[i].Value[0])
  911. case nl.IFLA_BOND_RESEND_IGMP:
  912. bond.ResendIgmp = int(native.Uint32(data[i].Value[0:4]))
  913. case nl.IFLA_BOND_NUM_PEER_NOTIF:
  914. bond.NumPeerNotif = int(data[i].Value[0])
  915. case nl.IFLA_BOND_ALL_SLAVES_ACTIVE:
  916. bond.AllSlavesActive = int(data[i].Value[0])
  917. case nl.IFLA_BOND_MIN_LINKS:
  918. bond.MinLinks = int(native.Uint32(data[i].Value[0:4]))
  919. case nl.IFLA_BOND_LP_INTERVAL:
  920. bond.LpInterval = int(native.Uint32(data[i].Value[0:4]))
  921. case nl.IFLA_BOND_PACKETS_PER_SLAVE:
  922. bond.PackersPerSlave = int(native.Uint32(data[i].Value[0:4]))
  923. case nl.IFLA_BOND_AD_LACP_RATE:
  924. bond.LacpRate = BondLacpRate(data[i].Value[0])
  925. case nl.IFLA_BOND_AD_SELECT:
  926. bond.AdSelect = BondAdSelect(data[i].Value[0])
  927. case nl.IFLA_BOND_AD_INFO:
  928. // TODO: implement
  929. }
  930. }
  931. }
  932. func parseIPVlanData(link Link, data []syscall.NetlinkRouteAttr) {
  933. ipv := link.(*IPVlan)
  934. for _, datum := range data {
  935. if datum.Attr.Type == nl.IFLA_IPVLAN_MODE {
  936. ipv.Mode = IPVlanMode(native.Uint32(datum.Value[0:4]))
  937. return
  938. }
  939. }
  940. }
  941. func parseMacvtapData(link Link, data []syscall.NetlinkRouteAttr) {
  942. macv := link.(*Macvtap)
  943. parseMacvlanData(&macv.Macvlan, data)
  944. }
  945. func parseMacvlanData(link Link, data []syscall.NetlinkRouteAttr) {
  946. macv := link.(*Macvlan)
  947. for _, datum := range data {
  948. if datum.Attr.Type == nl.IFLA_MACVLAN_MODE {
  949. switch native.Uint32(datum.Value[0:4]) {
  950. case nl.MACVLAN_MODE_PRIVATE:
  951. macv.Mode = MACVLAN_MODE_PRIVATE
  952. case nl.MACVLAN_MODE_VEPA:
  953. macv.Mode = MACVLAN_MODE_VEPA
  954. case nl.MACVLAN_MODE_BRIDGE:
  955. macv.Mode = MACVLAN_MODE_BRIDGE
  956. case nl.MACVLAN_MODE_PASSTHRU:
  957. macv.Mode = MACVLAN_MODE_PASSTHRU
  958. case nl.MACVLAN_MODE_SOURCE:
  959. macv.Mode = MACVLAN_MODE_SOURCE
  960. }
  961. return
  962. }
  963. }
  964. }
  965. // copied from pkg/net_linux.go
  966. func linkFlags(rawFlags uint32) net.Flags {
  967. var f net.Flags
  968. if rawFlags&syscall.IFF_UP != 0 {
  969. f |= net.FlagUp
  970. }
  971. if rawFlags&syscall.IFF_BROADCAST != 0 {
  972. f |= net.FlagBroadcast
  973. }
  974. if rawFlags&syscall.IFF_LOOPBACK != 0 {
  975. f |= net.FlagLoopback
  976. }
  977. if rawFlags&syscall.IFF_POINTOPOINT != 0 {
  978. f |= net.FlagPointToPoint
  979. }
  980. if rawFlags&syscall.IFF_MULTICAST != 0 {
  981. f |= net.FlagMulticast
  982. }
  983. return f
  984. }
  985. func htonl(val uint32) []byte {
  986. bytes := make([]byte, 4)
  987. binary.BigEndian.PutUint32(bytes, val)
  988. return bytes
  989. }
  990. func htons(val uint16) []byte {
  991. bytes := make([]byte, 2)
  992. binary.BigEndian.PutUint16(bytes, val)
  993. return bytes
  994. }
  995. func ntohl(buf []byte) uint32 {
  996. return binary.BigEndian.Uint32(buf)
  997. }
  998. func ntohs(buf []byte) uint16 {
  999. return binary.BigEndian.Uint16(buf)
  1000. }
  1001. func addGretapAttrs(gretap *Gretap, linkInfo *nl.RtAttr) {
  1002. data := nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_DATA, nil)
  1003. ip := gretap.Local.To4()
  1004. if ip != nil {
  1005. nl.NewRtAttrChild(data, nl.IFLA_GRE_LOCAL, []byte(ip))
  1006. }
  1007. ip = gretap.Remote.To4()
  1008. if ip != nil {
  1009. nl.NewRtAttrChild(data, nl.IFLA_GRE_REMOTE, []byte(ip))
  1010. }
  1011. if gretap.IKey != 0 {
  1012. nl.NewRtAttrChild(data, nl.IFLA_GRE_IKEY, htonl(gretap.IKey))
  1013. gretap.IFlags |= uint16(nl.GRE_KEY)
  1014. }
  1015. if gretap.OKey != 0 {
  1016. nl.NewRtAttrChild(data, nl.IFLA_GRE_OKEY, htonl(gretap.OKey))
  1017. gretap.OFlags |= uint16(nl.GRE_KEY)
  1018. }
  1019. nl.NewRtAttrChild(data, nl.IFLA_GRE_IFLAGS, htons(gretap.IFlags))
  1020. nl.NewRtAttrChild(data, nl.IFLA_GRE_OFLAGS, htons(gretap.OFlags))
  1021. if gretap.Link != 0 {
  1022. nl.NewRtAttrChild(data, nl.IFLA_GRE_LINK, nl.Uint32Attr(gretap.Link))
  1023. }
  1024. nl.NewRtAttrChild(data, nl.IFLA_GRE_PMTUDISC, nl.Uint8Attr(gretap.PMtuDisc))
  1025. nl.NewRtAttrChild(data, nl.IFLA_GRE_TTL, nl.Uint8Attr(gretap.Ttl))
  1026. nl.NewRtAttrChild(data, nl.IFLA_GRE_TOS, nl.Uint8Attr(gretap.Tos))
  1027. nl.NewRtAttrChild(data, nl.IFLA_GRE_ENCAP_TYPE, nl.Uint16Attr(gretap.EncapType))
  1028. nl.NewRtAttrChild(data, nl.IFLA_GRE_ENCAP_FLAGS, nl.Uint16Attr(gretap.EncapFlags))
  1029. nl.NewRtAttrChild(data, nl.IFLA_GRE_ENCAP_SPORT, htons(gretap.EncapSport))
  1030. nl.NewRtAttrChild(data, nl.IFLA_GRE_ENCAP_DPORT, htons(gretap.EncapDport))
  1031. }
  1032. func parseGretapData(link Link, data []syscall.NetlinkRouteAttr) {
  1033. gre := link.(*Gretap)
  1034. for _, datum := range data {
  1035. switch datum.Attr.Type {
  1036. case nl.IFLA_GRE_OKEY:
  1037. gre.IKey = ntohl(datum.Value[0:4])
  1038. case nl.IFLA_GRE_IKEY:
  1039. gre.OKey = ntohl(datum.Value[0:4])
  1040. case nl.IFLA_GRE_LOCAL:
  1041. gre.Local = net.IP(datum.Value[0:4])
  1042. case nl.IFLA_GRE_REMOTE:
  1043. gre.Remote = net.IP(datum.Value[0:4])
  1044. case nl.IFLA_GRE_ENCAP_SPORT:
  1045. gre.EncapSport = ntohs(datum.Value[0:2])
  1046. case nl.IFLA_GRE_ENCAP_DPORT:
  1047. gre.EncapDport = ntohs(datum.Value[0:2])
  1048. case nl.IFLA_GRE_IFLAGS:
  1049. gre.IFlags = ntohs(datum.Value[0:2])
  1050. case nl.IFLA_GRE_OFLAGS:
  1051. gre.OFlags = ntohs(datum.Value[0:2])
  1052. case nl.IFLA_GRE_TTL:
  1053. gre.Ttl = uint8(datum.Value[0])
  1054. case nl.IFLA_GRE_TOS:
  1055. gre.Tos = uint8(datum.Value[0])
  1056. case nl.IFLA_GRE_PMTUDISC:
  1057. gre.PMtuDisc = uint8(datum.Value[0])
  1058. case nl.IFLA_GRE_ENCAP_TYPE:
  1059. gre.EncapType = native.Uint16(datum.Value[0:2])
  1060. case nl.IFLA_GRE_ENCAP_FLAGS:
  1061. gre.EncapFlags = native.Uint16(datum.Value[0:2])
  1062. }
  1063. }
  1064. }