2014-09-01 03:27:34 +00:00
|
|
|
package netlink
|
|
|
|
|
|
|
|
import (
|
2016-05-09 23:53:16 +00:00
|
|
|
"fmt"
|
2014-09-01 03:27:34 +00:00
|
|
|
"net"
|
2017-10-27 18:58:20 +00:00
|
|
|
"time"
|
2014-09-01 03:27:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// XfrmStateAlgo represents the algorithm to use for the ipsec encryption.
|
|
|
|
type XfrmStateAlgo struct {
|
|
|
|
Name string
|
|
|
|
Key []byte
|
|
|
|
TruncateLen int // Auth only
|
2016-06-29 16:10:41 +00:00
|
|
|
ICVLen int // AEAD only
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 23:53:16 +00:00
|
|
|
func (a XfrmStateAlgo) String() string {
|
2016-06-29 16:10:41 +00:00
|
|
|
base := fmt.Sprintf("{Name: %s, Key: 0x%x", a.Name, a.Key)
|
|
|
|
if a.TruncateLen != 0 {
|
|
|
|
base = fmt.Sprintf("%s, Truncate length: %d", base, a.TruncateLen)
|
|
|
|
}
|
|
|
|
if a.ICVLen != 0 {
|
|
|
|
base = fmt.Sprintf("%s, ICV length: %d", base, a.ICVLen)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s}", base)
|
2016-05-09 23:53:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-13 23:42:24 +00:00
|
|
|
// EncapType is an enum representing the optional packet encapsulation.
|
2014-09-16 00:04:48 +00:00
|
|
|
type EncapType uint8
|
|
|
|
|
|
|
|
const (
|
2015-02-12 19:16:19 +00:00
|
|
|
XFRM_ENCAP_ESPINUDP_NONIKE EncapType = iota + 1
|
|
|
|
XFRM_ENCAP_ESPINUDP
|
2014-09-16 00:04:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (e EncapType) String() string {
|
|
|
|
switch e {
|
|
|
|
case XFRM_ENCAP_ESPINUDP_NONIKE:
|
2016-05-13 23:42:24 +00:00
|
|
|
return "espinudp-non-ike"
|
2014-09-16 00:04:48 +00:00
|
|
|
case XFRM_ENCAP_ESPINUDP:
|
|
|
|
return "espinudp"
|
|
|
|
}
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:12:26 +00:00
|
|
|
// XfrmStateEncap represents the encapsulation to use for the ipsec encryption.
|
2014-09-16 00:04:48 +00:00
|
|
|
type XfrmStateEncap struct {
|
|
|
|
Type EncapType
|
|
|
|
SrcPort int
|
|
|
|
DstPort int
|
|
|
|
OriginalAddress net.IP
|
|
|
|
}
|
|
|
|
|
2016-05-09 23:53:16 +00:00
|
|
|
func (e XfrmStateEncap) String() string {
|
|
|
|
return fmt.Sprintf("{Type: %s, Srcport: %d, DstPort: %d, OriginalAddress: %v}",
|
|
|
|
e.Type, e.SrcPort, e.DstPort, e.OriginalAddress)
|
|
|
|
}
|
|
|
|
|
2016-05-18 17:20:27 +00:00
|
|
|
// XfrmStateLimits represents the configured limits for the state.
|
|
|
|
type XfrmStateLimits struct {
|
|
|
|
ByteSoft uint64
|
|
|
|
ByteHard uint64
|
|
|
|
PacketSoft uint64
|
|
|
|
PacketHard uint64
|
|
|
|
TimeSoft uint64
|
|
|
|
TimeHard uint64
|
|
|
|
TimeUseSoft uint64
|
|
|
|
TimeUseHard uint64
|
|
|
|
}
|
|
|
|
|
2017-10-27 18:58:20 +00:00
|
|
|
// XfrmStateStats represents the current number of bytes/packets
|
|
|
|
// processed by this State, the State's installation and first use
|
|
|
|
// time and the replay window counters.
|
|
|
|
type XfrmStateStats struct {
|
|
|
|
ReplayWindow uint32
|
|
|
|
Replay uint32
|
|
|
|
Failed uint32
|
|
|
|
Bytes uint64
|
|
|
|
Packets uint64
|
|
|
|
AddTime uint64
|
|
|
|
UseTime uint64
|
|
|
|
}
|
|
|
|
|
2014-09-01 03:27:34 +00:00
|
|
|
// XfrmState represents the state of an ipsec policy. It optionally
|
|
|
|
// contains an XfrmStateAlgo for encryption and one for authentication.
|
|
|
|
type XfrmState struct {
|
2014-09-16 00:04:48 +00:00
|
|
|
Dst net.IP
|
|
|
|
Src net.IP
|
|
|
|
Proto Proto
|
|
|
|
Mode Mode
|
|
|
|
Spi int
|
|
|
|
Reqid int
|
2014-09-15 23:13:06 +00:00
|
|
|
ReplayWindow int
|
2016-05-18 17:20:27 +00:00
|
|
|
Limits XfrmStateLimits
|
2017-10-27 18:58:20 +00:00
|
|
|
Statistics XfrmStateStats
|
2016-05-01 03:31:59 +00:00
|
|
|
Mark *XfrmMark
|
2020-12-11 21:20:40 +00:00
|
|
|
OutputMark *XfrmMark
|
2019-01-02 22:56:57 +00:00
|
|
|
Ifid int
|
2014-09-16 00:04:48 +00:00
|
|
|
Auth *XfrmStateAlgo
|
|
|
|
Crypt *XfrmStateAlgo
|
2016-06-29 16:10:41 +00:00
|
|
|
Aead *XfrmStateAlgo
|
2014-09-16 00:04:48 +00:00
|
|
|
Encap *XfrmStateEncap
|
2017-01-30 11:36:23 +00:00
|
|
|
ESN bool
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
2016-05-09 23:53:16 +00:00
|
|
|
|
|
|
|
func (sa XfrmState) String() string {
|
2020-12-11 21:20:40 +00:00
|
|
|
return fmt.Sprintf("Dst: %v, Src: %v, Proto: %s, Mode: %s, SPI: 0x%x, ReqID: 0x%x, ReplayWindow: %d, Mark: %v, OutputMark: %v, Ifid: %d, Auth: %v, Crypt: %v, Aead: %v, Encap: %v, ESN: %t",
|
2019-09-10 13:56:44 +00:00
|
|
|
sa.Dst, sa.Src, sa.Proto, sa.Mode, sa.Spi, sa.Reqid, sa.ReplayWindow, sa.Mark, sa.OutputMark, sa.Ifid, sa.Auth, sa.Crypt, sa.Aead, sa.Encap, sa.ESN)
|
2016-05-09 23:53:16 +00:00
|
|
|
}
|
2016-05-18 17:20:27 +00:00
|
|
|
func (sa XfrmState) Print(stats bool) string {
|
|
|
|
if !stats {
|
|
|
|
return sa.String()
|
|
|
|
}
|
2017-10-27 18:58:20 +00:00
|
|
|
at := time.Unix(int64(sa.Statistics.AddTime), 0).Format(time.UnixDate)
|
|
|
|
ut := "-"
|
|
|
|
if sa.Statistics.UseTime > 0 {
|
|
|
|
ut = time.Unix(int64(sa.Statistics.UseTime), 0).Format(time.UnixDate)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s, ByteSoft: %s, ByteHard: %s, PacketSoft: %s, PacketHard: %s, TimeSoft: %d, TimeHard: %d, TimeUseSoft: %d, TimeUseHard: %d, Bytes: %d, Packets: %d, "+
|
|
|
|
"AddTime: %s, UseTime: %s, ReplayWindow: %d, Replay: %d, Failed: %d",
|
2016-05-18 17:20:27 +00:00
|
|
|
sa.String(), printLimit(sa.Limits.ByteSoft), printLimit(sa.Limits.ByteHard), printLimit(sa.Limits.PacketSoft), printLimit(sa.Limits.PacketHard),
|
2017-10-27 18:58:20 +00:00
|
|
|
sa.Limits.TimeSoft, sa.Limits.TimeHard, sa.Limits.TimeUseSoft, sa.Limits.TimeUseHard, sa.Statistics.Bytes, sa.Statistics.Packets, at, ut,
|
|
|
|
sa.Statistics.ReplayWindow, sa.Statistics.Replay, sa.Statistics.Failed)
|
2016-05-18 17:20:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func printLimit(lmt uint64) string {
|
2016-06-15 15:44:14 +00:00
|
|
|
if lmt == ^uint64(0) {
|
2016-05-18 17:20:27 +00:00
|
|
|
return "(INF)"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%d", lmt)
|
|
|
|
}
|