libflute
Enumerations | Functions
LibFlute::IpSec Namespace Reference

Enumerations

enum class  Direction { In , Out }
 

Functions

void configure_policy (uint32_t spi, const std::string &dest_address, Direction direction)
 
void configure_state (uint32_t spi, const std::string &dest_address, Direction direction, const std::string &key)
 
void enable_esp (uint32_t spi, const std::string &dest_address, Direction direction, const std::string &key)
 

Enumeration Type Documentation

◆ Direction

Enumerator
In 
Out 

Definition at line 20 of file IpSec.h.

Function Documentation

◆ configure_policy()

void LibFlute::IpSec::configure_policy ( uint32_t  spi,
const std::string &  dest_address,
Direction  direction 
)

Definition at line 33 of file IpSec.cpp.

34  {
35  struct nl_sock *sk;
36  struct nl_msg *msg;
37 
38  struct xfrm_userpolicy_info xpinfo = {};
39  xpinfo.lft.soft_byte_limit = XFRM_INF;
40  xpinfo.lft.hard_byte_limit = XFRM_INF;
41  xpinfo.lft.soft_packet_limit = XFRM_INF;
42  xpinfo.lft.hard_packet_limit = XFRM_INF;
43  xpinfo.dir = (direction == Direction::In) ? XFRM_POLICY_IN : XFRM_POLICY_OUT;
44 
45  xpinfo.sel.family = AF_INET;
46  xpinfo.sel.saddr.a4 = INADDR_ANY;
47  xpinfo.sel.daddr.a4 = inet_addr(dest_address.c_str());
48  xpinfo.sel.prefixlen_d = 32;
49 
50  struct xfrm_user_tmpl tmpl = {};
51  tmpl.id.daddr.a4 = inet_addr(dest_address.c_str());
52  tmpl.id.spi = htonl(spi);
53  tmpl.id.proto = IPPROTO_ESP;
54  tmpl.saddr.a4 = INADDR_ANY;
55  tmpl.reqid = spi;
56  tmpl.mode = XFRM_MODE_TRANSPORT;
57  tmpl.aalgos = (~(__u32)0);
58  tmpl.ealgos = (~(__u32)0);
59  tmpl.calgos = (~(__u32)0);
60  tmpl.family = AF_INET;
61 
62  msg = nlmsg_alloc_simple(XFRM_MSG_UPDPOLICY, 0);
63  nlmsg_append(msg, &xpinfo, sizeof(xpinfo), NLMSG_ALIGNTO);
64  nla_put(msg, XFRMA_TMPL, sizeof(tmpl), &tmpl);
65 
66  sk = nl_socket_alloc();
67  nl_connect(sk, NETLINK_XFRM);
68  nl_send_auto(sk, msg);
69  nlmsg_free(msg);
70  }

◆ configure_state()

void LibFlute::IpSec::configure_state ( uint32_t  spi,
const std::string &  dest_address,
Direction  direction,
const std::string &  key 
)

Definition at line 71 of file IpSec.cpp.

72  {
73  struct nl_sock *sk;
74  struct nl_msg *msg;
75 
76  struct xfrm_usersa_info xsinfo = {};
77 
78  xsinfo.sel.family = AF_INET;
79  xsinfo.sel.saddr.a4 = INADDR_ANY;
80  xsinfo.sel.daddr.a4 = inet_addr(dest_address.c_str());
81  xsinfo.sel.prefixlen_d = 32;
82 
83  xsinfo.id.daddr.a4 = inet_addr(dest_address.c_str());
84  xsinfo.id.spi = htonl(spi);
85  xsinfo.id.proto = IPPROTO_ESP;
86 
87  xsinfo.saddr.a4 = INADDR_ANY;
88 
89  xsinfo.lft.soft_byte_limit = XFRM_INF;
90  xsinfo.lft.hard_byte_limit = XFRM_INF;
91  xsinfo.lft.soft_packet_limit = XFRM_INF;
92  xsinfo.lft.hard_packet_limit = XFRM_INF;
93 
94  xsinfo.reqid = spi;
95  xsinfo.family = AF_INET;
96  xsinfo.mode = XFRM_MODE_TRANSPORT;
97 
98  std::vector<char> algo_buf(sizeof(struct xfrm_algo) + 512, 0);
99  auto* algo = reinterpret_cast<struct xfrm_algo*>(algo_buf.data());
100 
101  std::vector<char> binary_key;
102  for (unsigned int i = 0; i < key.length(); i += 2) {
103  binary_key.emplace_back((char)strtol(key.substr(i, 2).c_str(), nullptr, 16));
104  }
105  if (binary_key.size() > 512) {
106  throw std::runtime_error("Key is too long");
107  }
108  strcpy(algo->alg_name, "aes");
109  algo->alg_key_len = binary_key.size() * 8;
110  memcpy(algo->alg_key, &binary_key[0], binary_key.size());
111 
112  msg = nlmsg_alloc_simple(XFRM_MSG_NEWSA, 0);
113  nlmsg_append(msg, &xsinfo, sizeof(xsinfo), NLMSG_ALIGNTO);
114  nla_put(msg, XFRMA_ALG_CRYPT, algo_buf.size(), algo);
115 
116  sk = nl_socket_alloc();
117  nl_connect(sk, NETLINK_XFRM);
118  nl_send_auto(sk, msg);
119  nlmsg_free(msg);
120  }

◆ enable_esp()

void LibFlute::IpSec::enable_esp ( uint32_t  spi,
const std::string &  dest_address,
Direction  direction,
const std::string &  key 
)

Definition at line 122 of file IpSec.cpp.

123  {
124  configure_state(spi, dest_address, direction, key);
125  configure_policy(spi, dest_address, direction);
126  }
void configure_policy(uint32_t spi, const std::string &dest_address, Direction direction)
Definition: IpSec.cpp:33
void configure_state(uint32_t spi, const std::string &dest_address, Direction direction, const std::string &key)
Definition: IpSec.cpp:71