11
11
#include <sys/socket.h>
12
12
#include <sys/wait.h>
13
13
#include <linux/tcp.h>
14
+ #include <linux/udp.h>
14
15
#include <arpa/inet.h>
15
16
#include <net/if.h>
16
17
#include <netinet/in.h>
18
+ #include <netinet/ip.h>
17
19
#include <netdb.h>
18
20
#include <fcntl.h>
19
21
#include <libgen.h>
27
29
#include <time.h>
28
30
#include <errno.h>
29
31
32
+ #include <linux/xfrm.h>
33
+ #include <linux/ipsec.h>
34
+ #include <linux/pfkeyv2.h>
35
+
30
36
#ifndef IPV6_UNICAST_IF
31
37
#define IPV6_UNICAST_IF 76
32
38
#endif
@@ -114,6 +120,9 @@ struct sock_args {
114
120
struct in_addr in ;
115
121
struct in6_addr in6 ;
116
122
} expected_raddr ;
123
+
124
+ /* ESP in UDP encap test */
125
+ int use_xfrm ;
117
126
};
118
127
119
128
static int server_mode ;
@@ -1346,6 +1355,41 @@ static int bind_socket(int sd, struct sock_args *args)
1346
1355
return 0 ;
1347
1356
}
1348
1357
1358
+ static int config_xfrm_policy (int sd , struct sock_args * args )
1359
+ {
1360
+ struct xfrm_userpolicy_info policy = {};
1361
+ int type = UDP_ENCAP_ESPINUDP ;
1362
+ int xfrm_af = IP_XFRM_POLICY ;
1363
+ int level = SOL_IP ;
1364
+
1365
+ if (args -> type != SOCK_DGRAM ) {
1366
+ log_error ("Invalid socket type. Only DGRAM could be used for XFRM\n" );
1367
+ return 1 ;
1368
+ }
1369
+
1370
+ policy .action = XFRM_POLICY_ALLOW ;
1371
+ policy .sel .family = args -> version ;
1372
+ if (args -> version == AF_INET6 ) {
1373
+ xfrm_af = IPV6_XFRM_POLICY ;
1374
+ level = SOL_IPV6 ;
1375
+ }
1376
+
1377
+ policy .dir = XFRM_POLICY_OUT ;
1378
+ if (setsockopt (sd , level , xfrm_af , & policy , sizeof (policy )) < 0 )
1379
+ return 1 ;
1380
+
1381
+ policy .dir = XFRM_POLICY_IN ;
1382
+ if (setsockopt (sd , level , xfrm_af , & policy , sizeof (policy )) < 0 )
1383
+ return 1 ;
1384
+
1385
+ if (setsockopt (sd , IPPROTO_UDP , UDP_ENCAP , & type , sizeof (type )) < 0 ) {
1386
+ log_err_errno ("Failed to set xfrm encap" );
1387
+ return 1 ;
1388
+ }
1389
+
1390
+ return 0 ;
1391
+ }
1392
+
1349
1393
static int lsock_init (struct sock_args * args )
1350
1394
{
1351
1395
long flags ;
@@ -1389,6 +1433,11 @@ static int lsock_init(struct sock_args *args)
1389
1433
if (fcntl (sd , F_SETFD , FD_CLOEXEC ) < 0 )
1390
1434
log_err_errno ("Failed to set close-on-exec flag" );
1391
1435
1436
+ if (args -> use_xfrm && config_xfrm_policy (sd , args )) {
1437
+ log_err_errno ("Failed to set xfrm policy" );
1438
+ goto err ;
1439
+ }
1440
+
1392
1441
out :
1393
1442
return sd ;
1394
1443
@@ -1772,7 +1821,7 @@ static int ipc_parent(int cpid, int fd, struct sock_args *args)
1772
1821
return client_status ;
1773
1822
}
1774
1823
1775
- #define GETOPT_STR "sr:l:c:p:t:g:P:DRn:M:X:m:d:I:BN:O:SCi6L :0:1:2:3:Fbq"
1824
+ #define GETOPT_STR "sr:l:c:p:t:g:P:DRn:M:X:m:d:I:BN:O:SCi6xL :0:1:2:3:Fbq"
1776
1825
1777
1826
static void print_usage (char * prog )
1778
1827
{
@@ -1795,6 +1844,7 @@ static void print_usage(char *prog)
1795
1844
" -D|R datagram (D) / raw (R) socket (default stream)\n"
1796
1845
" -l addr local address to bind to in server mode\n"
1797
1846
" -c addr local address to bind to in client mode\n"
1847
+ " -x configure XFRM policy on socket\n"
1798
1848
"\n"
1799
1849
" -d dev bind socket to given device name\n"
1800
1850
" -I dev bind socket to given device name - server mode\n"
@@ -1966,6 +2016,9 @@ int main(int argc, char *argv[])
1966
2016
case 'q' :
1967
2017
quiet = 1 ;
1968
2018
break ;
2019
+ case 'x' :
2020
+ args .use_xfrm = 1 ;
2021
+ break ;
1969
2022
default :
1970
2023
print_usage (argv [0 ]);
1971
2024
return 1 ;
0 commit comments