libflute
IpSec.cpp
Go to the documentation of this file.
1 // libflute - FLUTE/ALC library
2 //
3 // Copyright (C) 2021 Klaus Kühnhammer (Österreichische Rundfunksender GmbH & Co KG)
4 //
5 // Licensed under the License terms and conditions for use, reproduction, and
6 // distribution of 5G-MAG software (the “License”). You may not use this file
7 // except in compliance with the License. You may obtain a copy of the License at
8 // https://www.5g-mag.com/reference-tools. Unless required by applicable law or
9 // agreed to in writing, software distributed under the License is distributed on
10 // an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 // or implied.
12 //
13 // See the License for the specific language governing permissions and limitations
14 // under the License.
15 //
16 #include <stdexcept>
17 #include <string>
18 #include <cstring>
19 #include <iostream>
20 #include "spdlog/spdlog.h"
21 #include <netlink/netlink.h>
22 #include <netlink/attr.h>
23 #include <netlink/msg.h>
24 #include <linux/rtnetlink.h>
25 #include <linux/in.h>
26 #include <linux/xfrm.h>
27 #include <linux/ipsec.h>
28 #include <arpa/inet.h>
29 #include "IpSec.h"
30 #include <boost/algorithm/hex.hpp>
31 
32 namespace LibFlute::IpSec {
33  void configure_policy(uint32_t spi, const std::string& dest_address, Direction direction)
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  }
71  void configure_state(uint32_t spi, const std::string& dest_address, Direction direction, const std::string& key)
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  }
121 
122  void enable_esp(uint32_t spi, const std::string& dest_address, Direction direction, const std::string& key)
123  {
124  configure_state(spi, dest_address, direction, key);
125  configure_policy(spi, dest_address, direction);
126  }
127 };
void configure_policy(uint32_t spi, const std::string &dest_address, Direction direction)
Definition: IpSec.cpp:33
void enable_esp(uint32_t spi, const std::string &dest_address, Direction direction, const std::string &key)
Definition: IpSec.cpp:122
void configure_state(uint32_t spi, const std::string &dest_address, Direction direction, const std::string &key)
Definition: IpSec.cpp:71