netlink/addr_linux.go

129 lines
3.0 KiB
Go
Raw Normal View History

2014-09-01 03:27:34 +00:00
package netlink
import (
"fmt"
"net"
"strings"
"syscall"
2014-09-24 05:21:11 +00:00
"github.com/vishvananda/netlink/nl"
2014-09-01 03:27:34 +00:00
)
// AddrAdd will add an IP address to a link device.
2014-12-01 03:31:00 +00:00
// Equivalent to: `ip addr add $addr dev $link`
2014-09-24 05:21:11 +00:00
func AddrAdd(link Link, addr *Addr) error {
2014-09-01 03:27:34 +00:00
req := nl.NewNetlinkRequest(syscall.RTM_NEWADDR, syscall.NLM_F_CREATE|syscall.NLM_F_EXCL|syscall.NLM_F_ACK)
2014-09-01 03:27:34 +00:00
return addrHandle(link, addr, req)
}
// AddrDel will delete an IP address from a link device.
// Equivalent to: `ip addr del $addr dev $link`
2014-09-24 05:21:11 +00:00
func AddrDel(link Link, addr *Addr) error {
req := nl.NewNetlinkRequest(syscall.RTM_DELADDR, syscall.NLM_F_ACK)
2014-09-01 03:27:34 +00:00
return addrHandle(link, addr, req)
}
2014-09-24 05:21:11 +00:00
func addrHandle(link Link, addr *Addr, req *nl.NetlinkRequest) error {
base := link.Attrs()
if addr.Label != "" && !strings.HasPrefix(addr.Label, base.Name) {
2014-09-01 03:27:34 +00:00
return fmt.Errorf("label must begin with interface name")
}
2014-09-24 05:21:11 +00:00
ensureIndex(base)
2014-09-01 03:27:34 +00:00
family := nl.GetIPFamily(addr.IP)
2014-09-01 03:27:34 +00:00
msg := nl.NewIfAddrmsg(family)
2014-09-24 05:21:11 +00:00
msg.Index = uint32(base.Index)
2014-09-01 03:27:34 +00:00
prefixlen, _ := addr.Mask.Size()
msg.Prefixlen = uint8(prefixlen)
req.AddData(msg)
var addrData []byte
if family == FAMILY_V4 {
addrData = addr.IP.To4()
} else {
addrData = addr.IP.To16()
}
localData := nl.NewRtAttr(syscall.IFA_LOCAL, addrData)
2014-09-01 03:27:34 +00:00
req.AddData(localData)
addressData := nl.NewRtAttr(syscall.IFA_ADDRESS, addrData)
2014-09-01 03:27:34 +00:00
req.AddData(addressData)
if addr.Label != "" {
labelData := nl.NewRtAttr(syscall.IFA_LABEL, nl.ZeroTerminated(addr.Label))
2014-09-01 03:27:34 +00:00
req.AddData(labelData)
}
_, err := req.Execute(syscall.NETLINK_ROUTE, 0)
return err
}
// AddrList gets a list of IP addresses in the system.
// Equivalent to: `ip addr show`.
// The list can be filtered by link and ip family.
2014-09-24 05:21:11 +00:00
func AddrList(link Link, family int) ([]Addr, error) {
req := nl.NewNetlinkRequest(syscall.RTM_GETADDR, syscall.NLM_F_DUMP)
msg := nl.NewIfInfomsg(family)
2014-09-01 03:27:34 +00:00
req.AddData(msg)
msgs, err := req.Execute(syscall.NETLINK_ROUTE, syscall.RTM_NEWADDR)
if err != nil {
return nil, err
}
2014-09-16 00:06:08 +00:00
2014-09-24 05:21:11 +00:00
index := 0
if link != nil {
base := link.Attrs()
ensureIndex(base)
index = base.Index
}
2014-09-01 03:27:34 +00:00
res := make([]Addr, 0)
for _, m := range msgs {
msg := nl.DeserializeIfAddrmsg(m)
2014-09-01 03:27:34 +00:00
2014-09-24 05:21:11 +00:00
if link != nil && msg.Index != uint32(index) {
2014-09-01 03:27:34 +00:00
// Ignore messages from other interfaces
continue
}
attrs, err := nl.ParseRouteAttr(m[msg.Len():])
2014-09-01 03:27:34 +00:00
if err != nil {
return nil, err
}
var local, dst *net.IPNet
2014-09-01 03:27:34 +00:00
var addr Addr
for _, attr := range attrs {
switch attr.Attr.Type {
case syscall.IFA_ADDRESS:
dst = &net.IPNet{
IP: attr.Value,
Mask: net.CIDRMask(int(msg.Prefixlen), 8*len(attr.Value)),
}
case syscall.IFA_LOCAL:
local = &net.IPNet{
2014-09-01 03:27:34 +00:00
IP: attr.Value,
Mask: net.CIDRMask(int(msg.Prefixlen), 8*len(attr.Value)),
}
case syscall.IFA_LABEL:
addr.Label = string(attr.Value[:len(attr.Value)-1])
}
}
// IFA_LOCAL should be there but if not, fall back to IFA_ADDRESS
if local != nil {
addr.IPNet = local
} else {
addr.IPNet = dst
}
2014-09-01 03:27:34 +00:00
res = append(res, addr)
}
return res, nil
}