2014-09-01 03:27:34 +00:00
|
|
|
package netlink
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"syscall"
|
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
"github.com/vishvananda/netlink/nl"
|
2014-09-01 03:27:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func writeStateAlgo(a *XfrmStateAlgo) []byte {
|
2014-09-19 02:04:48 +00:00
|
|
|
algo := nl.XfrmAlgo{
|
2014-09-01 03:27:34 +00:00
|
|
|
AlgKeyLen: uint32(len(a.Key) * 8),
|
|
|
|
AlgKey: a.Key,
|
|
|
|
}
|
|
|
|
end := len(a.Name)
|
|
|
|
if end > 64 {
|
|
|
|
end = 64
|
|
|
|
}
|
|
|
|
copy(algo.AlgName[:end], a.Name)
|
|
|
|
return algo.Serialize()
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeStateAlgoAuth(a *XfrmStateAlgo) []byte {
|
2014-09-19 02:04:48 +00:00
|
|
|
algo := nl.XfrmAlgoAuth{
|
2014-09-01 03:27:34 +00:00
|
|
|
AlgKeyLen: uint32(len(a.Key) * 8),
|
|
|
|
AlgTruncLen: uint32(a.TruncateLen),
|
|
|
|
AlgKey: a.Key,
|
|
|
|
}
|
|
|
|
end := len(a.Name)
|
|
|
|
if end > 64 {
|
|
|
|
end = 64
|
|
|
|
}
|
|
|
|
copy(algo.AlgName[:end], a.Name)
|
|
|
|
return algo.Serialize()
|
|
|
|
}
|
|
|
|
|
|
|
|
// XfrmStateAdd will add an xfrm state to the system.
|
|
|
|
// Equivalent to: `ip xfrm state add $state`
|
|
|
|
func XfrmStateAdd(state *XfrmState) error {
|
|
|
|
// A state with spi 0 can't be deleted so don't allow it to be set
|
|
|
|
if state.Spi == 0 {
|
|
|
|
return fmt.Errorf("Spi must be set when adding xfrm state.")
|
|
|
|
}
|
2014-09-19 02:04:48 +00:00
|
|
|
req := nl.NewNetlinkRequest(nl.XFRM_MSG_NEWSA, syscall.NLM_F_CREATE|syscall.NLM_F_EXCL|syscall.NLM_F_ACK)
|
2014-09-01 03:27:34 +00:00
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
msg := &nl.XfrmUsersaInfo{}
|
|
|
|
msg.Family = uint16(nl.GetIPFamily(state.Dst))
|
2014-09-01 03:27:34 +00:00
|
|
|
msg.Id.Daddr.FromIP(state.Dst)
|
|
|
|
msg.Saddr.FromIP(state.Src)
|
|
|
|
msg.Id.Proto = uint8(state.Proto)
|
|
|
|
msg.Mode = uint8(state.Mode)
|
2014-09-19 02:04:48 +00:00
|
|
|
msg.Id.Spi = nl.Swap32(uint32(state.Spi))
|
2014-09-01 03:27:34 +00:00
|
|
|
msg.Reqid = uint32(state.Reqid)
|
2014-09-15 23:13:06 +00:00
|
|
|
msg.ReplayWindow = uint8(state.ReplayWindow)
|
2014-09-19 02:04:48 +00:00
|
|
|
msg.Lft.SoftByteLimit = nl.XFRM_INF
|
|
|
|
msg.Lft.HardByteLimit = nl.XFRM_INF
|
|
|
|
msg.Lft.SoftPacketLimit = nl.XFRM_INF
|
|
|
|
msg.Lft.HardPacketLimit = nl.XFRM_INF
|
2014-09-01 03:27:34 +00:00
|
|
|
req.AddData(msg)
|
|
|
|
|
|
|
|
if state.Auth != nil {
|
2014-09-19 02:04:48 +00:00
|
|
|
out := nl.NewRtAttr(nl.XFRMA_ALG_AUTH_TRUNC, writeStateAlgoAuth(state.Auth))
|
2014-09-01 03:27:34 +00:00
|
|
|
req.AddData(out)
|
|
|
|
}
|
|
|
|
if state.Crypt != nil {
|
2014-09-19 02:04:48 +00:00
|
|
|
out := nl.NewRtAttr(nl.XFRMA_ALG_CRYPT, writeStateAlgo(state.Crypt))
|
2014-09-01 03:27:34 +00:00
|
|
|
req.AddData(out)
|
|
|
|
}
|
2014-09-16 00:04:48 +00:00
|
|
|
if state.Encap != nil {
|
2014-09-19 02:04:48 +00:00
|
|
|
encapData := make([]byte, nl.SizeofXfrmEncapTmpl)
|
|
|
|
encap := nl.DeserializeXfrmEncapTmpl(encapData)
|
2014-09-16 00:04:48 +00:00
|
|
|
encap.EncapType = uint16(state.Encap.Type)
|
2014-09-19 02:04:48 +00:00
|
|
|
encap.EncapSport = nl.Swap16(uint16(state.Encap.SrcPort))
|
|
|
|
encap.EncapDport = nl.Swap16(uint16(state.Encap.DstPort))
|
2014-09-16 00:04:48 +00:00
|
|
|
encap.EncapOa.FromIP(state.Encap.OriginalAddress)
|
2014-09-19 02:04:48 +00:00
|
|
|
out := nl.NewRtAttr(nl.XFRMA_ENCAP, encapData)
|
2014-09-16 00:04:48 +00:00
|
|
|
req.AddData(out)
|
|
|
|
}
|
2014-09-01 03:27:34 +00:00
|
|
|
|
|
|
|
_, err := req.Execute(syscall.NETLINK_XFRM, 0)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// XfrmStateDel will delete an xfrm state from the system. Note that
|
|
|
|
// the Algos are ignored when matching the state to delete.
|
|
|
|
// Equivalent to: `ip xfrm state del $state`
|
|
|
|
func XfrmStateDel(state *XfrmState) error {
|
2014-09-19 02:04:48 +00:00
|
|
|
req := nl.NewNetlinkRequest(nl.XFRM_MSG_DELSA, syscall.NLM_F_ACK)
|
2014-09-01 03:27:34 +00:00
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
msg := &nl.XfrmUsersaId{}
|
2014-09-01 03:27:34 +00:00
|
|
|
msg.Daddr.FromIP(state.Dst)
|
2014-09-19 02:04:48 +00:00
|
|
|
msg.Family = uint16(nl.GetIPFamily(state.Dst))
|
2014-09-01 03:27:34 +00:00
|
|
|
msg.Proto = uint8(state.Proto)
|
2014-09-19 02:04:48 +00:00
|
|
|
msg.Spi = nl.Swap32(uint32(state.Spi))
|
2014-09-01 03:27:34 +00:00
|
|
|
req.AddData(msg)
|
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
saddr := nl.XfrmAddress{}
|
2014-09-01 03:27:34 +00:00
|
|
|
saddr.FromIP(state.Src)
|
2014-09-19 02:04:48 +00:00
|
|
|
srcdata := nl.NewRtAttr(nl.XFRMA_SRCADDR, saddr.Serialize())
|
2014-09-01 03:27:34 +00:00
|
|
|
|
|
|
|
req.AddData(srcdata)
|
|
|
|
|
|
|
|
_, err := req.Execute(syscall.NETLINK_XFRM, 0)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// XfrmStateList gets a list of xfrm states in the system.
|
|
|
|
// Equivalent to: `ip xfrm state show`.
|
|
|
|
// The list can be filtered by ip family.
|
|
|
|
func XfrmStateList(family int) ([]XfrmState, error) {
|
2014-09-19 02:04:48 +00:00
|
|
|
req := nl.NewNetlinkRequest(nl.XFRM_MSG_GETSA, syscall.NLM_F_DUMP)
|
2014-09-01 03:27:34 +00:00
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
msg := nl.NewIfInfomsg(family)
|
2014-09-01 03:27:34 +00:00
|
|
|
req.AddData(msg)
|
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
msgs, err := req.Execute(syscall.NETLINK_XFRM, nl.XFRM_MSG_NEWSA)
|
2014-09-01 03:27:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-06-19 00:41:46 +00:00
|
|
|
var res []XfrmState
|
2014-09-01 03:27:34 +00:00
|
|
|
for _, m := range msgs {
|
2014-09-19 02:04:48 +00:00
|
|
|
msg := nl.DeserializeXfrmUsersaInfo(m)
|
2014-09-01 03:27:34 +00:00
|
|
|
|
|
|
|
if family != FAMILY_ALL && family != int(msg.Family) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var state XfrmState
|
|
|
|
|
|
|
|
state.Dst = msg.Id.Daddr.ToIP()
|
|
|
|
state.Src = msg.Saddr.ToIP()
|
|
|
|
state.Proto = Proto(msg.Id.Proto)
|
|
|
|
state.Mode = Mode(msg.Mode)
|
2014-09-19 02:04:48 +00:00
|
|
|
state.Spi = int(nl.Swap32(msg.Id.Spi))
|
2014-09-01 03:27:34 +00:00
|
|
|
state.Reqid = int(msg.Reqid)
|
2014-09-15 23:13:06 +00:00
|
|
|
state.ReplayWindow = int(msg.ReplayWindow)
|
2014-09-01 03:27:34 +00:00
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
attrs, err := nl.ParseRouteAttr(m[msg.Len():])
|
2014-09-01 03:27:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, attr := range attrs {
|
|
|
|
switch attr.Attr.Type {
|
2014-09-19 02:04:48 +00:00
|
|
|
case nl.XFRMA_ALG_AUTH, nl.XFRMA_ALG_CRYPT:
|
2014-09-01 03:27:34 +00:00
|
|
|
var resAlgo *XfrmStateAlgo
|
2014-09-19 02:04:48 +00:00
|
|
|
if attr.Attr.Type == nl.XFRMA_ALG_AUTH {
|
2014-09-01 03:27:34 +00:00
|
|
|
if state.Auth == nil {
|
|
|
|
state.Auth = new(XfrmStateAlgo)
|
|
|
|
}
|
|
|
|
resAlgo = state.Auth
|
|
|
|
} else {
|
|
|
|
state.Crypt = new(XfrmStateAlgo)
|
|
|
|
resAlgo = state.Crypt
|
|
|
|
}
|
2014-09-19 02:04:48 +00:00
|
|
|
algo := nl.DeserializeXfrmAlgo(attr.Value[:])
|
|
|
|
(*resAlgo).Name = nl.BytesToString(algo.AlgName[:])
|
2014-09-01 03:27:34 +00:00
|
|
|
(*resAlgo).Key = algo.AlgKey
|
2014-09-19 02:04:48 +00:00
|
|
|
case nl.XFRMA_ALG_AUTH_TRUNC:
|
2014-09-01 03:27:34 +00:00
|
|
|
if state.Auth == nil {
|
|
|
|
state.Auth = new(XfrmStateAlgo)
|
|
|
|
}
|
2014-09-19 02:04:48 +00:00
|
|
|
algo := nl.DeserializeXfrmAlgoAuth(attr.Value[:])
|
|
|
|
state.Auth.Name = nl.BytesToString(algo.AlgName[:])
|
2014-09-01 03:27:34 +00:00
|
|
|
state.Auth.Key = algo.AlgKey
|
|
|
|
state.Auth.TruncateLen = int(algo.AlgTruncLen)
|
2014-09-19 02:04:48 +00:00
|
|
|
case nl.XFRMA_ENCAP:
|
|
|
|
encap := nl.DeserializeXfrmEncapTmpl(attr.Value[:])
|
2014-09-16 00:04:48 +00:00
|
|
|
state.Encap = new(XfrmStateEncap)
|
|
|
|
state.Encap.Type = EncapType(encap.EncapType)
|
2014-09-19 02:04:48 +00:00
|
|
|
state.Encap.SrcPort = int(nl.Swap16(encap.EncapSport))
|
|
|
|
state.Encap.DstPort = int(nl.Swap16(encap.EncapDport))
|
2014-09-16 00:04:48 +00:00
|
|
|
state.Encap.OriginalAddress = encap.EncapOa.ToIP()
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
res = append(res, state)
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|