netlink_linux.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  1. package netlink
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. "io"
  6. "math/rand"
  7. "net"
  8. "sync/atomic"
  9. "syscall"
  10. "unsafe"
  11. )
  12. const (
  13. IFNAMSIZ = 16
  14. DEFAULT_CHANGE = 0xFFFFFFFF
  15. IFLA_INFO_KIND = 1
  16. IFLA_INFO_DATA = 2
  17. VETH_INFO_PEER = 1
  18. IFLA_MACVLAN_MODE = 1
  19. IFLA_VLAN_ID = 1
  20. IFLA_NET_NS_FD = 28
  21. IFLA_ADDRESS = 1
  22. SIOC_BRADDBR = 0x89a0
  23. SIOC_BRDELBR = 0x89a1
  24. SIOC_BRADDIF = 0x89a2
  25. )
  26. const (
  27. MACVLAN_MODE_PRIVATE = 1 << iota
  28. MACVLAN_MODE_VEPA
  29. MACVLAN_MODE_BRIDGE
  30. MACVLAN_MODE_PASSTHRU
  31. )
  32. var nextSeqNr uint32
  33. type ifreqHwaddr struct {
  34. IfrnName [IFNAMSIZ]byte
  35. IfruHwaddr syscall.RawSockaddr
  36. }
  37. type ifreqIndex struct {
  38. IfrnName [IFNAMSIZ]byte
  39. IfruIndex int32
  40. }
  41. type ifreqFlags struct {
  42. IfrnName [IFNAMSIZ]byte
  43. Ifruflags uint16
  44. }
  45. var native binary.ByteOrder
  46. func init() {
  47. var x uint32 = 0x01020304
  48. if *(*byte)(unsafe.Pointer(&x)) == 0x01 {
  49. native = binary.BigEndian
  50. } else {
  51. native = binary.LittleEndian
  52. }
  53. }
  54. func getIpFamily(ip net.IP) int {
  55. if len(ip) <= net.IPv4len {
  56. return syscall.AF_INET
  57. }
  58. if ip.To4() != nil {
  59. return syscall.AF_INET
  60. }
  61. return syscall.AF_INET6
  62. }
  63. type NetlinkRequestData interface {
  64. Len() int
  65. ToWireFormat() []byte
  66. }
  67. type IfInfomsg struct {
  68. syscall.IfInfomsg
  69. }
  70. func newIfInfomsg(family int) *IfInfomsg {
  71. return &IfInfomsg{
  72. IfInfomsg: syscall.IfInfomsg{
  73. Family: uint8(family),
  74. },
  75. }
  76. }
  77. func newIfInfomsgChild(parent *RtAttr, family int) *IfInfomsg {
  78. msg := newIfInfomsg(family)
  79. parent.children = append(parent.children, msg)
  80. return msg
  81. }
  82. func (msg *IfInfomsg) ToWireFormat() []byte {
  83. length := syscall.SizeofIfInfomsg
  84. b := make([]byte, length)
  85. b[0] = msg.Family
  86. b[1] = 0
  87. native.PutUint16(b[2:4], msg.Type)
  88. native.PutUint32(b[4:8], uint32(msg.Index))
  89. native.PutUint32(b[8:12], msg.Flags)
  90. native.PutUint32(b[12:16], msg.Change)
  91. return b
  92. }
  93. func (msg *IfInfomsg) Len() int {
  94. return syscall.SizeofIfInfomsg
  95. }
  96. type IfAddrmsg struct {
  97. syscall.IfAddrmsg
  98. }
  99. func newIfAddrmsg(family int) *IfAddrmsg {
  100. return &IfAddrmsg{
  101. IfAddrmsg: syscall.IfAddrmsg{
  102. Family: uint8(family),
  103. },
  104. }
  105. }
  106. func (msg *IfAddrmsg) ToWireFormat() []byte {
  107. length := syscall.SizeofIfAddrmsg
  108. b := make([]byte, length)
  109. b[0] = msg.Family
  110. b[1] = msg.Prefixlen
  111. b[2] = msg.Flags
  112. b[3] = msg.Scope
  113. native.PutUint32(b[4:8], msg.Index)
  114. return b
  115. }
  116. func (msg *IfAddrmsg) Len() int {
  117. return syscall.SizeofIfAddrmsg
  118. }
  119. type RtMsg struct {
  120. syscall.RtMsg
  121. }
  122. func newRtMsg() *RtMsg {
  123. return &RtMsg{
  124. RtMsg: syscall.RtMsg{
  125. Table: syscall.RT_TABLE_MAIN,
  126. Scope: syscall.RT_SCOPE_UNIVERSE,
  127. Protocol: syscall.RTPROT_BOOT,
  128. Type: syscall.RTN_UNICAST,
  129. },
  130. }
  131. }
  132. func (msg *RtMsg) ToWireFormat() []byte {
  133. length := syscall.SizeofRtMsg
  134. b := make([]byte, length)
  135. b[0] = msg.Family
  136. b[1] = msg.Dst_len
  137. b[2] = msg.Src_len
  138. b[3] = msg.Tos
  139. b[4] = msg.Table
  140. b[5] = msg.Protocol
  141. b[6] = msg.Scope
  142. b[7] = msg.Type
  143. native.PutUint32(b[8:12], msg.Flags)
  144. return b
  145. }
  146. func (msg *RtMsg) Len() int {
  147. return syscall.SizeofRtMsg
  148. }
  149. func rtaAlignOf(attrlen int) int {
  150. return (attrlen + syscall.RTA_ALIGNTO - 1) & ^(syscall.RTA_ALIGNTO - 1)
  151. }
  152. type RtAttr struct {
  153. syscall.RtAttr
  154. Data []byte
  155. children []NetlinkRequestData
  156. }
  157. func newRtAttr(attrType int, data []byte) *RtAttr {
  158. return &RtAttr{
  159. RtAttr: syscall.RtAttr{
  160. Type: uint16(attrType),
  161. },
  162. children: []NetlinkRequestData{},
  163. Data: data,
  164. }
  165. }
  166. func newRtAttrChild(parent *RtAttr, attrType int, data []byte) *RtAttr {
  167. attr := newRtAttr(attrType, data)
  168. parent.children = append(parent.children, attr)
  169. return attr
  170. }
  171. func (a *RtAttr) Len() int {
  172. if len(a.children) == 0 {
  173. return (syscall.SizeofRtAttr + len(a.Data))
  174. }
  175. l := 0
  176. for _, child := range a.children {
  177. l += child.Len()
  178. }
  179. l += syscall.SizeofRtAttr
  180. return rtaAlignOf(l + len(a.Data))
  181. }
  182. func (a *RtAttr) ToWireFormat() []byte {
  183. length := a.Len()
  184. buf := make([]byte, rtaAlignOf(length))
  185. if a.Data != nil {
  186. copy(buf[4:], a.Data)
  187. } else {
  188. next := 4
  189. for _, child := range a.children {
  190. childBuf := child.ToWireFormat()
  191. copy(buf[next:], childBuf)
  192. next += rtaAlignOf(len(childBuf))
  193. }
  194. }
  195. if l := uint16(length); l != 0 {
  196. native.PutUint16(buf[0:2], l)
  197. }
  198. native.PutUint16(buf[2:4], a.Type)
  199. return buf
  200. }
  201. func uint32Attr(t int, n uint32) *RtAttr {
  202. buf := make([]byte, 4)
  203. native.PutUint32(buf, n)
  204. return newRtAttr(t, buf)
  205. }
  206. type NetlinkRequest struct {
  207. syscall.NlMsghdr
  208. Data []NetlinkRequestData
  209. }
  210. func (rr *NetlinkRequest) ToWireFormat() []byte {
  211. length := rr.Len
  212. dataBytes := make([][]byte, len(rr.Data))
  213. for i, data := range rr.Data {
  214. dataBytes[i] = data.ToWireFormat()
  215. length += uint32(len(dataBytes[i]))
  216. }
  217. b := make([]byte, length)
  218. native.PutUint32(b[0:4], length)
  219. native.PutUint16(b[4:6], rr.Type)
  220. native.PutUint16(b[6:8], rr.Flags)
  221. native.PutUint32(b[8:12], rr.Seq)
  222. native.PutUint32(b[12:16], rr.Pid)
  223. next := 16
  224. for _, data := range dataBytes {
  225. copy(b[next:], data)
  226. next += len(data)
  227. }
  228. return b
  229. }
  230. func (rr *NetlinkRequest) AddData(data NetlinkRequestData) {
  231. if data != nil {
  232. rr.Data = append(rr.Data, data)
  233. }
  234. }
  235. func newNetlinkRequest(proto, flags int) *NetlinkRequest {
  236. return &NetlinkRequest{
  237. NlMsghdr: syscall.NlMsghdr{
  238. Len: uint32(syscall.NLMSG_HDRLEN),
  239. Type: uint16(proto),
  240. Flags: syscall.NLM_F_REQUEST | uint16(flags),
  241. Seq: atomic.AddUint32(&nextSeqNr, 1),
  242. },
  243. }
  244. }
  245. type NetlinkSocket struct {
  246. fd int
  247. lsa syscall.SockaddrNetlink
  248. }
  249. func getNetlinkSocket() (*NetlinkSocket, error) {
  250. fd, err := syscall.Socket(syscall.AF_NETLINK, syscall.SOCK_RAW, syscall.NETLINK_ROUTE)
  251. if err != nil {
  252. return nil, err
  253. }
  254. s := &NetlinkSocket{
  255. fd: fd,
  256. }
  257. s.lsa.Family = syscall.AF_NETLINK
  258. if err := syscall.Bind(fd, &s.lsa); err != nil {
  259. syscall.Close(fd)
  260. return nil, err
  261. }
  262. return s, nil
  263. }
  264. func (s *NetlinkSocket) Close() {
  265. syscall.Close(s.fd)
  266. }
  267. func (s *NetlinkSocket) Send(request *NetlinkRequest) error {
  268. if err := syscall.Sendto(s.fd, request.ToWireFormat(), 0, &s.lsa); err != nil {
  269. return err
  270. }
  271. return nil
  272. }
  273. func (s *NetlinkSocket) Receive() ([]syscall.NetlinkMessage, error) {
  274. rb := make([]byte, syscall.Getpagesize())
  275. nr, _, err := syscall.Recvfrom(s.fd, rb, 0)
  276. if err != nil {
  277. return nil, err
  278. }
  279. if nr < syscall.NLMSG_HDRLEN {
  280. return nil, ErrShortResponse
  281. }
  282. rb = rb[:nr]
  283. return syscall.ParseNetlinkMessage(rb)
  284. }
  285. func (s *NetlinkSocket) GetPid() (uint32, error) {
  286. lsa, err := syscall.Getsockname(s.fd)
  287. if err != nil {
  288. return 0, err
  289. }
  290. switch v := lsa.(type) {
  291. case *syscall.SockaddrNetlink:
  292. return v.Pid, nil
  293. }
  294. return 0, ErrWrongSockType
  295. }
  296. func (s *NetlinkSocket) CheckMessage(m syscall.NetlinkMessage, seq, pid uint32) error {
  297. if m.Header.Seq != seq {
  298. return fmt.Errorf("netlink: invalid seq %d, expected %d", m.Header.Seq, seq)
  299. }
  300. if m.Header.Pid != pid {
  301. return fmt.Errorf("netlink: wrong pid %d, expected %d", m.Header.Pid, pid)
  302. }
  303. if m.Header.Type == syscall.NLMSG_DONE {
  304. return io.EOF
  305. }
  306. if m.Header.Type == syscall.NLMSG_ERROR {
  307. e := int32(native.Uint32(m.Data[0:4]))
  308. if e == 0 {
  309. return io.EOF
  310. }
  311. return syscall.Errno(-e)
  312. }
  313. return nil
  314. }
  315. func (s *NetlinkSocket) HandleAck(seq uint32) error {
  316. pid, err := s.GetPid()
  317. if err != nil {
  318. return err
  319. }
  320. outer:
  321. for {
  322. msgs, err := s.Receive()
  323. if err != nil {
  324. return err
  325. }
  326. for _, m := range msgs {
  327. if err := s.CheckMessage(m, seq, pid); err != nil {
  328. if err == io.EOF {
  329. break outer
  330. }
  331. return err
  332. }
  333. }
  334. }
  335. return nil
  336. }
  337. func zeroTerminated(s string) []byte {
  338. return []byte(s + "\000")
  339. }
  340. func nonZeroTerminated(s string) []byte {
  341. return []byte(s)
  342. }
  343. // Add a new network link of a specified type.
  344. // This is identical to running: ip link add $name type $linkType
  345. func NetworkLinkAdd(name string, linkType string) error {
  346. if name == "" || linkType == "" {
  347. return fmt.Errorf("Neither link name nor link type can be empty!")
  348. }
  349. s, err := getNetlinkSocket()
  350. if err != nil {
  351. return err
  352. }
  353. defer s.Close()
  354. wb := newNetlinkRequest(syscall.RTM_NEWLINK, syscall.NLM_F_CREATE|syscall.NLM_F_EXCL|syscall.NLM_F_ACK)
  355. msg := newIfInfomsg(syscall.AF_UNSPEC)
  356. wb.AddData(msg)
  357. linkInfo := newRtAttr(syscall.IFLA_LINKINFO, nil)
  358. newRtAttrChild(linkInfo, IFLA_INFO_KIND, nonZeroTerminated(linkType))
  359. wb.AddData(linkInfo)
  360. nameData := newRtAttr(syscall.IFLA_IFNAME, zeroTerminated(name))
  361. wb.AddData(nameData)
  362. if err := s.Send(wb); err != nil {
  363. return err
  364. }
  365. return s.HandleAck(wb.Seq)
  366. }
  367. // Delete a network link.
  368. // This is identical to running: ip link del $name
  369. func NetworkLinkDel(name string) error {
  370. if name == "" {
  371. return fmt.Errorf("Network link name can not be empty!")
  372. }
  373. s, err := getNetlinkSocket()
  374. if err != nil {
  375. return err
  376. }
  377. defer s.Close()
  378. iface, err := net.InterfaceByName(name)
  379. if err != nil {
  380. return err
  381. }
  382. wb := newNetlinkRequest(syscall.RTM_DELLINK, syscall.NLM_F_ACK)
  383. msg := newIfInfomsg(syscall.AF_UNSPEC)
  384. msg.Index = int32(iface.Index)
  385. wb.AddData(msg)
  386. if err := s.Send(wb); err != nil {
  387. return err
  388. }
  389. return s.HandleAck(wb.Seq)
  390. }
  391. // Bring up a particular network interface.
  392. // This is identical to running: ip link set dev $name up
  393. func NetworkLinkUp(iface *net.Interface) error {
  394. s, err := getNetlinkSocket()
  395. if err != nil {
  396. return err
  397. }
  398. defer s.Close()
  399. wb := newNetlinkRequest(syscall.RTM_NEWLINK, syscall.NLM_F_ACK)
  400. msg := newIfInfomsg(syscall.AF_UNSPEC)
  401. msg.Index = int32(iface.Index)
  402. msg.Flags = syscall.IFF_UP
  403. msg.Change = syscall.IFF_UP
  404. wb.AddData(msg)
  405. if err := s.Send(wb); err != nil {
  406. return err
  407. }
  408. return s.HandleAck(wb.Seq)
  409. }
  410. // Bring down a particular network interface.
  411. // This is identical to running: ip link set $name down
  412. func NetworkLinkDown(iface *net.Interface) error {
  413. s, err := getNetlinkSocket()
  414. if err != nil {
  415. return err
  416. }
  417. defer s.Close()
  418. wb := newNetlinkRequest(syscall.RTM_NEWLINK, syscall.NLM_F_ACK)
  419. msg := newIfInfomsg(syscall.AF_UNSPEC)
  420. msg.Index = int32(iface.Index)
  421. msg.Flags = 0 & ^syscall.IFF_UP
  422. msg.Change = DEFAULT_CHANGE
  423. wb.AddData(msg)
  424. if err := s.Send(wb); err != nil {
  425. return err
  426. }
  427. return s.HandleAck(wb.Seq)
  428. }
  429. // Set link layer address ie. MAC Address.
  430. // This is identical to running: ip link set dev $name address $macaddress
  431. func NetworkSetMacAddress(iface *net.Interface, macaddr string) error {
  432. s, err := getNetlinkSocket()
  433. if err != nil {
  434. return err
  435. }
  436. defer s.Close()
  437. hwaddr, err := net.ParseMAC(macaddr)
  438. if err != nil {
  439. return err
  440. }
  441. var (
  442. MULTICAST byte = 0x1
  443. LOCALOUI byte = 0x2
  444. )
  445. if hwaddr[0]&0x1 == MULTICAST || hwaddr[0]&0x2 != LOCALOUI {
  446. return fmt.Errorf("Incorrect Local MAC Address specified: %s", macaddr)
  447. }
  448. wb := newNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
  449. msg := newIfInfomsg(syscall.AF_UNSPEC)
  450. msg.Index = int32(iface.Index)
  451. msg.Change = DEFAULT_CHANGE
  452. wb.AddData(msg)
  453. macdata := make([]byte, 6)
  454. copy(macdata, hwaddr)
  455. data := newRtAttr(IFLA_ADDRESS, macdata)
  456. wb.AddData(data)
  457. if err := s.Send(wb); err != nil {
  458. return err
  459. }
  460. return s.HandleAck(wb.Seq)
  461. }
  462. // Set link Maximum Transmission Unit
  463. // This is identical to running: ip link set dev $name mtu $MTU
  464. // bridge is a bitch here https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=292088
  465. // https://bugzilla.redhat.com/show_bug.cgi?id=697021
  466. // There is a discussion about how to deal with ifcs joining bridge with MTU > 1500
  467. // Regular network nterfaces do seem to work though!
  468. func NetworkSetMTU(iface *net.Interface, mtu int) error {
  469. s, err := getNetlinkSocket()
  470. if err != nil {
  471. return err
  472. }
  473. defer s.Close()
  474. wb := newNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
  475. msg := newIfInfomsg(syscall.AF_UNSPEC)
  476. msg.Type = syscall.RTM_SETLINK
  477. msg.Flags = syscall.NLM_F_REQUEST
  478. msg.Index = int32(iface.Index)
  479. msg.Change = DEFAULT_CHANGE
  480. wb.AddData(msg)
  481. wb.AddData(uint32Attr(syscall.IFLA_MTU, uint32(mtu)))
  482. if err := s.Send(wb); err != nil {
  483. return err
  484. }
  485. return s.HandleAck(wb.Seq)
  486. }
  487. func networkMasterAction(iface *net.Interface, rtattr *RtAttr) error {
  488. s, err := getNetlinkSocket()
  489. if err != nil {
  490. return err
  491. }
  492. defer s.Close()
  493. wb := newNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
  494. msg := newIfInfomsg(syscall.AF_UNSPEC)
  495. msg.Type = syscall.RTM_SETLINK
  496. msg.Flags = syscall.NLM_F_REQUEST
  497. msg.Index = int32(iface.Index)
  498. msg.Change = DEFAULT_CHANGE
  499. wb.AddData(msg)
  500. wb.AddData(rtattr)
  501. if err := s.Send(wb); err != nil {
  502. return err
  503. }
  504. return s.HandleAck(wb.Seq)
  505. }
  506. // Add an interface to bridge.
  507. // This is identical to running: ip link set $name master $master
  508. func NetworkSetMaster(iface, master *net.Interface) error {
  509. data := uint32Attr(syscall.IFLA_MASTER, uint32(master.Index))
  510. return networkMasterAction(iface, data)
  511. }
  512. // Remove an interface from the bridge
  513. // This is is identical to to running: ip link $name set nomaster
  514. func NetworkSetNoMaster(iface *net.Interface) error {
  515. data := uint32Attr(syscall.IFLA_MASTER, 0)
  516. return networkMasterAction(iface, data)
  517. }
  518. func networkSetNsAction(iface *net.Interface, rtattr *RtAttr) error {
  519. s, err := getNetlinkSocket()
  520. if err != nil {
  521. return err
  522. }
  523. defer s.Close()
  524. wb := newNetlinkRequest(syscall.RTM_NEWLINK, syscall.NLM_F_ACK)
  525. msg := newIfInfomsg(syscall.AF_UNSPEC)
  526. msg.Index = int32(iface.Index)
  527. wb.AddData(msg)
  528. wb.AddData(rtattr)
  529. if err := s.Send(wb); err != nil {
  530. return err
  531. }
  532. return s.HandleAck(wb.Seq)
  533. }
  534. // Move a particular network interface to a particular network namespace
  535. // specified by PID. This is idential to running: ip link set dev $name netns $pid
  536. func NetworkSetNsPid(iface *net.Interface, nspid int) error {
  537. data := uint32Attr(syscall.IFLA_NET_NS_PID, uint32(nspid))
  538. return networkSetNsAction(iface, data)
  539. }
  540. // Move a particular network interface to a particular mounted
  541. // network namespace specified by file descriptor.
  542. // This is idential to running: ip link set dev $name netns $fd
  543. func NetworkSetNsFd(iface *net.Interface, fd int) error {
  544. data := uint32Attr(IFLA_NET_NS_FD, uint32(fd))
  545. return networkSetNsAction(iface, data)
  546. }
  547. // Rname a particular interface to a different name
  548. // !!! Note that you can't rename an active interface. You need to bring it down before renaming it.
  549. // This is identical to running: ip link set dev ${oldName} name ${newName}
  550. func NetworkChangeName(iface *net.Interface, newName string) error {
  551. if len(newName) >= IFNAMSIZ {
  552. return fmt.Errorf("Interface name %s too long", newName)
  553. }
  554. s, err := getNetlinkSocket()
  555. if err != nil {
  556. return err
  557. }
  558. defer s.Close()
  559. wb := newNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
  560. msg := newIfInfomsg(syscall.AF_UNSPEC)
  561. msg.Index = int32(iface.Index)
  562. msg.Change = DEFAULT_CHANGE
  563. wb.AddData(msg)
  564. nameData := newRtAttr(syscall.IFLA_IFNAME, zeroTerminated(newName))
  565. wb.AddData(nameData)
  566. if err := s.Send(wb); err != nil {
  567. return err
  568. }
  569. return s.HandleAck(wb.Seq)
  570. }
  571. // Add a new VETH pair link on the host
  572. // This is identical to running: ip link add name $name type veth peer name $peername
  573. func NetworkCreateVethPair(name1, name2 string) error {
  574. s, err := getNetlinkSocket()
  575. if err != nil {
  576. return err
  577. }
  578. defer s.Close()
  579. wb := newNetlinkRequest(syscall.RTM_NEWLINK, syscall.NLM_F_CREATE|syscall.NLM_F_EXCL|syscall.NLM_F_ACK)
  580. msg := newIfInfomsg(syscall.AF_UNSPEC)
  581. wb.AddData(msg)
  582. nameData := newRtAttr(syscall.IFLA_IFNAME, zeroTerminated(name1))
  583. wb.AddData(nameData)
  584. nest1 := newRtAttr(syscall.IFLA_LINKINFO, nil)
  585. newRtAttrChild(nest1, IFLA_INFO_KIND, zeroTerminated("veth"))
  586. nest2 := newRtAttrChild(nest1, IFLA_INFO_DATA, nil)
  587. nest3 := newRtAttrChild(nest2, VETH_INFO_PEER, nil)
  588. newIfInfomsgChild(nest3, syscall.AF_UNSPEC)
  589. newRtAttrChild(nest3, syscall.IFLA_IFNAME, zeroTerminated(name2))
  590. wb.AddData(nest1)
  591. if err := s.Send(wb); err != nil {
  592. return err
  593. }
  594. return s.HandleAck(wb.Seq)
  595. }
  596. // Add a new VLAN interface with masterDev as its upper device
  597. // This is identical to running:
  598. // ip link add name $name link $masterdev type vlan id $id
  599. func NetworkLinkAddVlan(masterDev, vlanDev string, vlanId uint16) error {
  600. s, err := getNetlinkSocket()
  601. if err != nil {
  602. return err
  603. }
  604. defer s.Close()
  605. wb := newNetlinkRequest(syscall.RTM_NEWLINK, syscall.NLM_F_CREATE|syscall.NLM_F_EXCL|syscall.NLM_F_ACK)
  606. masterDevIfc, err := net.InterfaceByName(masterDev)
  607. if err != nil {
  608. return err
  609. }
  610. msg := newIfInfomsg(syscall.AF_UNSPEC)
  611. wb.AddData(msg)
  612. nest1 := newRtAttr(syscall.IFLA_LINKINFO, nil)
  613. newRtAttrChild(nest1, IFLA_INFO_KIND, nonZeroTerminated("vlan"))
  614. nest2 := newRtAttrChild(nest1, IFLA_INFO_DATA, nil)
  615. vlanData := make([]byte, 2)
  616. native.PutUint16(vlanData, vlanId)
  617. newRtAttrChild(nest2, IFLA_VLAN_ID, vlanData)
  618. wb.AddData(nest1)
  619. wb.AddData(uint32Attr(syscall.IFLA_LINK, uint32(masterDevIfc.Index)))
  620. wb.AddData(newRtAttr(syscall.IFLA_IFNAME, zeroTerminated(vlanDev)))
  621. if err := s.Send(wb); err != nil {
  622. return err
  623. }
  624. return s.HandleAck(wb.Seq)
  625. }
  626. // Add MAC VLAN network interface with masterDev as its upper device
  627. // This is identical to running:
  628. // ip link add name $name link $masterdev type macvlan mode $mode
  629. func NetworkLinkAddMacVlan(masterDev, macVlanDev string, mode string) error {
  630. s, err := getNetlinkSocket()
  631. if err != nil {
  632. return err
  633. }
  634. defer s.Close()
  635. macVlan := map[string]uint32{
  636. "private": MACVLAN_MODE_PRIVATE,
  637. "vepa": MACVLAN_MODE_VEPA,
  638. "bridge": MACVLAN_MODE_BRIDGE,
  639. "passthru": MACVLAN_MODE_PASSTHRU,
  640. }
  641. wb := newNetlinkRequest(syscall.RTM_NEWLINK, syscall.NLM_F_CREATE|syscall.NLM_F_EXCL|syscall.NLM_F_ACK)
  642. masterDevIfc, err := net.InterfaceByName(masterDev)
  643. if err != nil {
  644. return err
  645. }
  646. msg := newIfInfomsg(syscall.AF_UNSPEC)
  647. wb.AddData(msg)
  648. nest1 := newRtAttr(syscall.IFLA_LINKINFO, nil)
  649. newRtAttrChild(nest1, IFLA_INFO_KIND, nonZeroTerminated("macvlan"))
  650. nest2 := newRtAttrChild(nest1, IFLA_INFO_DATA, nil)
  651. macVlanData := make([]byte, 4)
  652. native.PutUint32(macVlanData, macVlan[mode])
  653. newRtAttrChild(nest2, IFLA_MACVLAN_MODE, macVlanData)
  654. wb.AddData(nest1)
  655. wb.AddData(uint32Attr(syscall.IFLA_LINK, uint32(masterDevIfc.Index)))
  656. wb.AddData(newRtAttr(syscall.IFLA_IFNAME, zeroTerminated(macVlanDev)))
  657. if err := s.Send(wb); err != nil {
  658. return err
  659. }
  660. return s.HandleAck(wb.Seq)
  661. }
  662. func networkLinkIpAction(action, flags int, ifa IfAddr) error {
  663. s, err := getNetlinkSocket()
  664. if err != nil {
  665. return err
  666. }
  667. defer s.Close()
  668. family := getIpFamily(ifa.IP)
  669. wb := newNetlinkRequest(action, flags)
  670. msg := newIfAddrmsg(family)
  671. msg.Index = uint32(ifa.Iface.Index)
  672. prefixLen, _ := ifa.IPNet.Mask.Size()
  673. msg.Prefixlen = uint8(prefixLen)
  674. wb.AddData(msg)
  675. var ipData []byte
  676. if family == syscall.AF_INET {
  677. ipData = ifa.IP.To4()
  678. } else {
  679. ipData = ifa.IP.To16()
  680. }
  681. localData := newRtAttr(syscall.IFA_LOCAL, ipData)
  682. wb.AddData(localData)
  683. addrData := newRtAttr(syscall.IFA_ADDRESS, ipData)
  684. wb.AddData(addrData)
  685. if err := s.Send(wb); err != nil {
  686. return err
  687. }
  688. return s.HandleAck(wb.Seq)
  689. }
  690. // Delete an IP address from an interface. This is identical to:
  691. // ip addr del $ip/$ipNet dev $iface
  692. func NetworkLinkDelIp(iface *net.Interface, ip net.IP, ipNet *net.IPNet) error {
  693. return networkLinkIpAction(
  694. syscall.RTM_DELADDR,
  695. syscall.NLM_F_ACK,
  696. IfAddr{iface, ip, ipNet},
  697. )
  698. }
  699. // Add an Ip address to an interface. This is identical to:
  700. // ip addr add $ip/$ipNet dev $iface
  701. func NetworkLinkAddIp(iface *net.Interface, ip net.IP, ipNet *net.IPNet) error {
  702. return networkLinkIpAction(
  703. syscall.RTM_NEWADDR,
  704. syscall.NLM_F_CREATE|syscall.NLM_F_EXCL|syscall.NLM_F_ACK,
  705. IfAddr{iface, ip, ipNet},
  706. )
  707. }
  708. // Returns an array of IPNet for all the currently routed subnets on ipv4
  709. // This is similar to the first column of "ip route" output
  710. func NetworkGetRoutes() ([]Route, error) {
  711. s, err := getNetlinkSocket()
  712. if err != nil {
  713. return nil, err
  714. }
  715. defer s.Close()
  716. wb := newNetlinkRequest(syscall.RTM_GETROUTE, syscall.NLM_F_DUMP)
  717. msg := newIfInfomsg(syscall.AF_UNSPEC)
  718. wb.AddData(msg)
  719. if err := s.Send(wb); err != nil {
  720. return nil, err
  721. }
  722. pid, err := s.GetPid()
  723. if err != nil {
  724. return nil, err
  725. }
  726. res := make([]Route, 0)
  727. outer:
  728. for {
  729. msgs, err := s.Receive()
  730. if err != nil {
  731. return nil, err
  732. }
  733. for _, m := range msgs {
  734. if err := s.CheckMessage(m, wb.Seq, pid); err != nil {
  735. if err == io.EOF {
  736. break outer
  737. }
  738. return nil, err
  739. }
  740. if m.Header.Type != syscall.RTM_NEWROUTE {
  741. continue
  742. }
  743. var r Route
  744. msg := (*RtMsg)(unsafe.Pointer(&m.Data[0:syscall.SizeofRtMsg][0]))
  745. if msg.Flags&syscall.RTM_F_CLONED != 0 {
  746. // Ignore cloned routes
  747. continue
  748. }
  749. if msg.Table != syscall.RT_TABLE_MAIN {
  750. // Ignore non-main tables
  751. continue
  752. }
  753. if msg.Family != syscall.AF_INET {
  754. // Ignore non-ipv4 routes
  755. continue
  756. }
  757. if msg.Dst_len == 0 {
  758. // Default routes
  759. r.Default = true
  760. }
  761. attrs, err := syscall.ParseNetlinkRouteAttr(&m)
  762. if err != nil {
  763. return nil, err
  764. }
  765. for _, attr := range attrs {
  766. switch attr.Attr.Type {
  767. case syscall.RTA_DST:
  768. ip := attr.Value
  769. r.IPNet = &net.IPNet{
  770. IP: ip,
  771. Mask: net.CIDRMask(int(msg.Dst_len), 8*len(ip)),
  772. }
  773. case syscall.RTA_OIF:
  774. index := int(native.Uint32(attr.Value[0:4]))
  775. r.Iface, _ = net.InterfaceByIndex(index)
  776. }
  777. }
  778. if r.Default || r.IPNet != nil {
  779. res = append(res, r)
  780. }
  781. }
  782. }
  783. return res, nil
  784. }
  785. // Add a new route table entry.
  786. func AddRoute(destination, source, gateway, device string) error {
  787. if destination == "" && source == "" && gateway == "" {
  788. return fmt.Errorf("one of destination, source or gateway must not be blank")
  789. }
  790. s, err := getNetlinkSocket()
  791. if err != nil {
  792. return err
  793. }
  794. defer s.Close()
  795. wb := newNetlinkRequest(syscall.RTM_NEWROUTE, syscall.NLM_F_CREATE|syscall.NLM_F_EXCL|syscall.NLM_F_ACK)
  796. msg := newRtMsg()
  797. currentFamily := -1
  798. var rtAttrs []*RtAttr
  799. if destination != "" {
  800. destIP, destNet, err := net.ParseCIDR(destination)
  801. if err != nil {
  802. return fmt.Errorf("destination CIDR %s couldn't be parsed", destination)
  803. }
  804. destFamily := getIpFamily(destIP)
  805. currentFamily = destFamily
  806. destLen, bits := destNet.Mask.Size()
  807. if destLen == 0 && bits == 0 {
  808. return fmt.Errorf("destination CIDR %s generated a non-canonical Mask", destination)
  809. }
  810. msg.Family = uint8(destFamily)
  811. msg.Dst_len = uint8(destLen)
  812. var destData []byte
  813. if destFamily == syscall.AF_INET {
  814. destData = destIP.To4()
  815. } else {
  816. destData = destIP.To16()
  817. }
  818. rtAttrs = append(rtAttrs, newRtAttr(syscall.RTA_DST, destData))
  819. }
  820. if source != "" {
  821. srcIP, srcNet, err := net.ParseCIDR(source)
  822. if err != nil {
  823. return fmt.Errorf("source CIDR %s couldn't be parsed", source)
  824. }
  825. srcFamily := getIpFamily(srcIP)
  826. if currentFamily != -1 && currentFamily != srcFamily {
  827. return fmt.Errorf("source and destination ip were not the same IP family")
  828. }
  829. currentFamily = srcFamily
  830. srcLen, bits := srcNet.Mask.Size()
  831. if srcLen == 0 && bits == 0 {
  832. return fmt.Errorf("source CIDR %s generated a non-canonical Mask", source)
  833. }
  834. msg.Family = uint8(srcFamily)
  835. msg.Src_len = uint8(srcLen)
  836. var srcData []byte
  837. if srcFamily == syscall.AF_INET {
  838. srcData = srcIP.To4()
  839. } else {
  840. srcData = srcIP.To16()
  841. }
  842. rtAttrs = append(rtAttrs, newRtAttr(syscall.RTA_SRC, srcData))
  843. }
  844. if gateway != "" {
  845. gwIP := net.ParseIP(gateway)
  846. if gwIP == nil {
  847. return fmt.Errorf("gateway IP %s couldn't be parsed", gateway)
  848. }
  849. gwFamily := getIpFamily(gwIP)
  850. if currentFamily != -1 && currentFamily != gwFamily {
  851. return fmt.Errorf("gateway, source, and destination ip were not the same IP family")
  852. }
  853. msg.Family = uint8(gwFamily)
  854. var gwData []byte
  855. if gwFamily == syscall.AF_INET {
  856. gwData = gwIP.To4()
  857. } else {
  858. gwData = gwIP.To16()
  859. }
  860. rtAttrs = append(rtAttrs, newRtAttr(syscall.RTA_GATEWAY, gwData))
  861. }
  862. wb.AddData(msg)
  863. for _, attr := range rtAttrs {
  864. wb.AddData(attr)
  865. }
  866. iface, err := net.InterfaceByName(device)
  867. if err != nil {
  868. return err
  869. }
  870. wb.AddData(uint32Attr(syscall.RTA_OIF, uint32(iface.Index)))
  871. if err := s.Send(wb); err != nil {
  872. return err
  873. }
  874. return s.HandleAck(wb.Seq)
  875. }
  876. // Add a new default gateway. Identical to:
  877. // ip route add default via $ip
  878. func AddDefaultGw(ip, device string) error {
  879. return AddRoute("", "", ip, device)
  880. }
  881. // THIS CODE DOES NOT COMMUNICATE WITH KERNEL VIA RTNETLINK INTERFACE
  882. // IT IS HERE FOR BACKWARDS COMPATIBILITY WITH OLDER LINUX KERNELS
  883. // WHICH SHIP WITH OLDER NOT ENTIRELY FUNCTIONAL VERSION OF NETLINK
  884. func getIfSocket() (fd int, err error) {
  885. for _, socket := range []int{
  886. syscall.AF_INET,
  887. syscall.AF_PACKET,
  888. syscall.AF_INET6,
  889. } {
  890. if fd, err = syscall.Socket(socket, syscall.SOCK_DGRAM, 0); err == nil {
  891. break
  892. }
  893. }
  894. if err == nil {
  895. return fd, nil
  896. }
  897. return -1, err
  898. }
  899. // Create the actual bridge device. This is more backward-compatible than
  900. // netlink.NetworkLinkAdd and works on RHEL 6.
  901. func CreateBridge(name string, setMacAddr bool) error {
  902. if len(name) >= IFNAMSIZ {
  903. return fmt.Errorf("Interface name %s too long", name)
  904. }
  905. s, err := getIfSocket()
  906. if err != nil {
  907. return err
  908. }
  909. defer syscall.Close(s)
  910. nameBytePtr, err := syscall.BytePtrFromString(name)
  911. if err != nil {
  912. return err
  913. }
  914. if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(s), SIOC_BRADDBR, uintptr(unsafe.Pointer(nameBytePtr))); err != 0 {
  915. return err
  916. }
  917. if setMacAddr {
  918. return SetMacAddress(name, randMacAddr())
  919. }
  920. return nil
  921. }
  922. // Delete the actual bridge device.
  923. func DeleteBridge(name string) error {
  924. s, err := getIfSocket()
  925. if err != nil {
  926. return err
  927. }
  928. defer syscall.Close(s)
  929. nameBytePtr, err := syscall.BytePtrFromString(name)
  930. if err != nil {
  931. return err
  932. }
  933. var ifr ifreqFlags
  934. copy(ifr.IfrnName[:len(ifr.IfrnName)-1], []byte(name))
  935. if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(s),
  936. syscall.SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifr))); err != 0 {
  937. return err
  938. }
  939. if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(s),
  940. SIOC_BRDELBR, uintptr(unsafe.Pointer(nameBytePtr))); err != 0 {
  941. return err
  942. }
  943. return nil
  944. }
  945. // Add a slave to abridge device. This is more backward-compatible than
  946. // netlink.NetworkSetMaster and works on RHEL 6.
  947. func AddToBridge(iface, master *net.Interface) error {
  948. if len(master.Name) >= IFNAMSIZ {
  949. return fmt.Errorf("Interface name %s too long", master.Name)
  950. }
  951. s, err := getIfSocket()
  952. if err != nil {
  953. return err
  954. }
  955. defer syscall.Close(s)
  956. ifr := ifreqIndex{}
  957. copy(ifr.IfrnName[:len(ifr.IfrnName)-1], master.Name)
  958. ifr.IfruIndex = int32(iface.Index)
  959. if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(s), SIOC_BRADDIF, uintptr(unsafe.Pointer(&ifr))); err != 0 {
  960. return err
  961. }
  962. return nil
  963. }
  964. func randMacAddr() string {
  965. hw := make(net.HardwareAddr, 6)
  966. for i := 0; i < 6; i++ {
  967. hw[i] = byte(rand.Intn(255))
  968. }
  969. hw[0] &^= 0x1 // clear multicast bit
  970. hw[0] |= 0x2 // set local assignment bit (IEEE802)
  971. return hw.String()
  972. }
  973. func SetMacAddress(name, addr string) error {
  974. if len(name) >= IFNAMSIZ {
  975. return fmt.Errorf("Interface name %s too long", name)
  976. }
  977. hw, err := net.ParseMAC(addr)
  978. if err != nil {
  979. return err
  980. }
  981. s, err := getIfSocket()
  982. if err != nil {
  983. return err
  984. }
  985. defer syscall.Close(s)
  986. ifr := ifreqHwaddr{}
  987. ifr.IfruHwaddr.Family = syscall.ARPHRD_ETHER
  988. copy(ifr.IfrnName[:len(ifr.IfrnName)-1], name)
  989. for i := 0; i < 6; i++ {
  990. ifr.IfruHwaddr.Data[i] = ifrDataByte(hw[i])
  991. }
  992. if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(s), syscall.SIOCSIFHWADDR, uintptr(unsafe.Pointer(&ifr))); err != 0 {
  993. return err
  994. }
  995. return nil
  996. }
  997. func ChangeName(iface *net.Interface, newName string) error {
  998. if len(newName) >= IFNAMSIZ {
  999. return fmt.Errorf("Interface name %s too long", newName)
  1000. }
  1001. fd, err := getIfSocket()
  1002. if err != nil {
  1003. return err
  1004. }
  1005. defer syscall.Close(fd)
  1006. data := [IFNAMSIZ * 2]byte{}
  1007. // the "-1"s here are very important for ensuring we get proper null
  1008. // termination of our new C strings
  1009. copy(data[:IFNAMSIZ-1], iface.Name)
  1010. copy(data[IFNAMSIZ:IFNAMSIZ*2-1], newName)
  1011. if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), syscall.SIOCSIFNAME, uintptr(unsafe.Pointer(&data[0]))); errno != 0 {
  1012. return errno
  1013. }
  1014. return nil
  1015. }