Skip to content

Commit 286446c

Browse files
mmellisonshemminger
authored andcommitted
ip: support for xfrm interfaces
Interfaces take a 'if_id' which is an interface id which can be set on an xfrm policy as its interface lookup key (XFRMA_IF_ID). Signed-off-by: Matt Ellison <matt@arroyo.io> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
1 parent d5d27f2 commit 286446c

File tree

5 files changed

+138
-3
lines changed

5 files changed

+138
-3
lines changed

ip/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ IPOBJ=ip.o ipaddress.o ipaddrlabel.o iproute.o iprule.o ipnetns.o \
55
ipxfrm.o xfrm_state.o xfrm_policy.o xfrm_monitor.o iplink_dummy.o \
66
iplink_ifb.o iplink_nlmon.o iplink_team.o iplink_vcan.o iplink_vxcan.o \
77
iplink_vlan.o link_veth.o link_gre.o iplink_can.o iplink_xdp.o \
8-
iplink_macvlan.o ipl2tp.o link_vti.o link_vti6.o \
8+
iplink_macvlan.o ipl2tp.o link_vti.o link_vti6.o link_xfrm.o \
99
iplink_vxlan.o tcp_metrics.o iplink_ipoib.o ipnetconf.o link_ip6tnl.o \
1010
link_iptnl.o link_gre6.o iplink_bond.o iplink_bond_slave.o iplink_hsr.o \
1111
iplink_bridge.o iplink_bridge_slave.o ipfou.o iplink_ipvlan.o \

ip/iplink.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ void iplink_usage(void)
121121
" bridge | bond | team | ipoib | ip6tnl | ipip | sit | vxlan |\n"
122122
" gre | gretap | erspan | ip6gre | ip6gretap | ip6erspan |\n"
123123
" vti | nlmon | team_slave | bond_slave | bridge_slave |\n"
124-
" ipvlan | ipvtap | geneve | vrf | macsec | netdevsim | rmnet }\n");
124+
" ipvlan | ipvtap | geneve | vrf | macsec | netdevsim | rmnet |\n"
125+
" xfrm }\n");
125126
}
126127
exit(-1);
127128
}

ip/link_xfrm.c

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* link_xfrm.c Virtual XFRM Interface driver module
4+
*
5+
* Authors: Matt Ellison <matt@arroyo.io>
6+
*/
7+
8+
#include <string.h>
9+
#include <linux/if_link.h>
10+
11+
#include "rt_names.h"
12+
#include "utils.h"
13+
#include "ip_common.h"
14+
#include "tunnel.h"
15+
16+
static void xfrm_print_help(struct link_util *lu, int argc, char **argv,
17+
FILE *f)
18+
{
19+
fprintf(f, "Usage: ... %-4s dev PHYS_DEV [ if_id IF-ID ]\n", lu->id);
20+
fprintf(f, "\nWhere: IF-ID := { 0x0..0xffffffff }\n");
21+
}
22+
23+
static int xfrm_parse_opt(struct link_util *lu, int argc, char **argv,
24+
struct nlmsghdr *n)
25+
{
26+
unsigned int link = 0;
27+
__u32 if_id = 0;
28+
29+
while (argc > 0) {
30+
if (!matches(*argv, "dev")) {
31+
NEXT_ARG();
32+
link = ll_name_to_index(*argv);
33+
if (!link)
34+
exit(nodev(*argv));
35+
} else if (!matches(*argv, "if_id")) {
36+
NEXT_ARG();
37+
if (!get_u32(&if_id, *argv, 0))
38+
addattr32(n, 1024, IFLA_XFRM_IF_ID, if_id);
39+
} else {
40+
xfrm_print_help(lu, argc, argv, stderr);
41+
return -1;
42+
}
43+
argc--; argv++;
44+
}
45+
46+
if (link) {
47+
addattr32(n, 1024, IFLA_XFRM_LINK, link);
48+
} else {
49+
fprintf(stderr, "must specify physical device\n");
50+
return -1;
51+
}
52+
53+
return 0;
54+
}
55+
56+
static void xfrm_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
57+
{
58+
59+
if (!tb)
60+
return;
61+
62+
if (tb[IFLA_XFRM_IF_ID]) {
63+
__u32 id = rta_getattr_u32(tb[IFLA_XFRM_IF_ID]);
64+
65+
print_0xhex(PRINT_ANY, "if_id", "if_id %#llx ", id);
66+
67+
}
68+
69+
}
70+
71+
struct link_util xfrm_link_util = {
72+
.id = "xfrm",
73+
.maxattr = IFLA_XFRM_MAX,
74+
.parse_opt = xfrm_parse_opt,
75+
.print_opt = xfrm_print_opt,
76+
.print_help = xfrm_print_help,
77+
};

man/man8/ip-link.8.in

+26-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,8 @@ ip-link \- network device configuration
221221
.BR vrf " |"
222222
.BR macsec " |"
223223
.BR netdevsim " |"
224-
.BR rmnet " ]"
224+
.BR rmnet " |"
225+
.BR xfrm " ]"
225226

226227
.ti -8
227228
.IR ETYPE " := [ " TYPE " |"
@@ -350,6 +351,9 @@ Link types:
350351
.sp
351352
.BR rmnet
352353
- Qualcomm rmnet device
354+
.sp
355+
.BR xfrm
356+
- Virtual xfrm interface
353357
.in -8
354358

355359
.TP
@@ -1741,6 +1745,27 @@ the following additional arguments are supported:
17411745

17421746
.in -8
17431747

1748+
.TP
1749+
XFRM Type Support
1750+
For a link of type
1751+
.I XFRM
1752+
the following additional arguments are supported:
1753+
1754+
.BI "ip link add " DEVICE " type xfrm dev " PHYS_DEV " [ if_id " IF_ID " ]"
1755+
1756+
.in +8
1757+
.sp
1758+
.BI dev " PHYS_DEV "
1759+
- specifies the underlying physical interface from which transform traffic is sent and received.
1760+
1761+
.sp
1762+
.BI if_id " IF-ID "
1763+
- specifies the hexadecimal lookup key used to send traffic to and from specific xfrm
1764+
policies. Policies must be configured with the same key. If not set, the key defaults to
1765+
0 and will match any policies which similarly do not have a lookup key configuration.
1766+
1767+
.in -8
1768+
17441769
.SS ip link delete - delete virtual link
17451770

17461771
.TP
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/sh
2+
3+
. lib/generic.sh
4+
5+
ts_log "[Testing Add XFRM Interface, With IF-ID]"
6+
7+
PHYS_DEV="lo"
8+
NEW_DEV="$(rand_dev)"
9+
IF_ID="0xf"
10+
11+
ts_ip "$0" "Add $NEW_DEV xfrm interface" link add dev $NEW_DEV type xfrm dev $PHYS_DEV if_id $IF_ID
12+
13+
ts_ip "$0" "Show $NEW_DEV xfrm interface" -d link show dev $NEW_DEV
14+
test_on "$NEW_DEV"
15+
test_on "if_id $IF_ID"
16+
17+
ts_ip "$0" "Del $NEW_DEV xfrm interface" link del dev $NEW_DEV
18+
19+
20+
ts_log "[Testing Add XFRM Interface, No IF-ID]"
21+
22+
PHYS_DEV="lo"
23+
NEW_DEV="$(rand_dev)"
24+
IF_ID="0xf"
25+
26+
ts_ip "$0" "Add $NEW_DEV xfrm interface" link add dev $NEW_DEV type xfrm dev $PHYS_DEV
27+
28+
ts_ip "$0" "Show $NEW_DEV xfrm interface" -d link show dev $NEW_DEV
29+
test_on "$NEW_DEV"
30+
test_on_not "if_id $IF_ID"
31+
32+
ts_ip "$0" "Del $NEW_DEV xfrm interface" link del dev $NEW_DEV

0 commit comments

Comments
 (0)