blob: 57448fc519fcd9b6b09f66bcaf996ceb9b34f8d8 [file] [log] [blame]
Steffen Klassertf203b762018-06-12 14:07:12 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * XFRM virtual interface
4 *
5 * Copyright (C) 2018 secunet Security Networks AG
6 *
7 * Author:
8 * Steffen Klassert <steffen.klassert@secunet.com>
9 */
10
11#include <linux/module.h>
12#include <linux/capability.h>
13#include <linux/errno.h>
14#include <linux/types.h>
15#include <linux/sockios.h>
16#include <linux/icmp.h>
17#include <linux/if.h>
18#include <linux/in.h>
19#include <linux/ip.h>
20#include <linux/net.h>
21#include <linux/in6.h>
22#include <linux/netdevice.h>
23#include <linux/if_link.h>
24#include <linux/if_arp.h>
25#include <linux/icmpv6.h>
26#include <linux/init.h>
27#include <linux/route.h>
28#include <linux/rtnetlink.h>
29#include <linux/netfilter_ipv6.h>
30#include <linux/slab.h>
31#include <linux/hash.h>
32
33#include <linux/uaccess.h>
34#include <linux/atomic.h>
35
36#include <net/icmp.h>
37#include <net/ip.h>
38#include <net/ipv6.h>
39#include <net/ip6_route.h>
Jason A. Donenfeld8f9a1fa42020-06-29 19:06:25 -060040#include <net/ip_tunnels.h>
Steffen Klassertf203b762018-06-12 14:07:12 +020041#include <net/addrconf.h>
42#include <net/xfrm.h>
43#include <net/net_namespace.h>
44#include <net/netns/generic.h>
45#include <linux/etherdevice.h>
46
47static int xfrmi_dev_init(struct net_device *dev);
48static void xfrmi_dev_setup(struct net_device *dev);
49static struct rtnl_link_ops xfrmi_link_ops __read_mostly;
50static unsigned int xfrmi_net_id __read_mostly;
Eyal Birger2749c692020-07-09 13:16:51 +030051static const struct net_device_ops xfrmi_netdev_ops;
Steffen Klassertf203b762018-06-12 14:07:12 +020052
Eyal Birgere98e4452020-07-09 13:16:52 +030053#define XFRMI_HASH_BITS 8
54#define XFRMI_HASH_SIZE BIT(XFRMI_HASH_BITS)
Steffen Klassertf203b762018-06-12 14:07:12 +020055
56struct xfrmi_net {
57 /* lists for storing interfaces in use */
Eyal Birgere98e4452020-07-09 13:16:52 +030058 struct xfrm_if __rcu *xfrmi[XFRMI_HASH_SIZE];
Steffen Klassertf203b762018-06-12 14:07:12 +020059};
60
61#define for_each_xfrmi_rcu(start, xi) \
62 for (xi = rcu_dereference(start); xi; xi = rcu_dereference(xi->next))
63
Eyal Birgere98e4452020-07-09 13:16:52 +030064static u32 xfrmi_hash(u32 if_id)
65{
66 return hash_32(if_id, XFRMI_HASH_BITS);
67}
68
Steffen Klassertf203b762018-06-12 14:07:12 +020069static struct xfrm_if *xfrmi_lookup(struct net *net, struct xfrm_state *x)
70{
71 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
72 struct xfrm_if *xi;
73
Eyal Birgere98e4452020-07-09 13:16:52 +030074 for_each_xfrmi_rcu(xfrmn->xfrmi[xfrmi_hash(x->if_id)], xi) {
Steffen Klassertf203b762018-06-12 14:07:12 +020075 if (x->if_id == xi->p.if_id &&
76 (xi->dev->flags & IFF_UP))
77 return xi;
78 }
79
80 return NULL;
81}
82
Martin Willi025c65e2019-03-26 13:20:43 +010083static struct xfrm_if *xfrmi_decode_session(struct sk_buff *skb,
84 unsigned short family)
Steffen Klassertf203b762018-06-12 14:07:12 +020085{
Eyal Birger2749c692020-07-09 13:16:51 +030086 struct net_device *dev;
Martin Willi025c65e2019-03-26 13:20:43 +010087 int ifindex = 0;
Steffen Klassertf203b762018-06-12 14:07:12 +020088
Tobias Brunner660899d2019-02-18 10:49:39 +010089 if (!secpath_exists(skb) || !skb->dev)
Steffen Klassertf203b762018-06-12 14:07:12 +020090 return NULL;
91
Martin Willi025c65e2019-03-26 13:20:43 +010092 switch (family) {
93 case AF_INET6:
94 ifindex = inet6_sdif(skb);
95 break;
96 case AF_INET:
97 ifindex = inet_sdif(skb);
98 break;
99 }
Martin Willi025c65e2019-03-26 13:20:43 +0100100
Eyal Birger2749c692020-07-09 13:16:51 +0300101 if (ifindex) {
102 struct net *net = xs_net(xfrm_input_state(skb));
Steffen Klassertf203b762018-06-12 14:07:12 +0200103
Eyal Birger2749c692020-07-09 13:16:51 +0300104 dev = dev_get_by_index_rcu(net, ifindex);
105 } else {
106 dev = skb->dev;
Steffen Klassertf203b762018-06-12 14:07:12 +0200107 }
108
Eyal Birger2749c692020-07-09 13:16:51 +0300109 if (!dev || !(dev->flags & IFF_UP))
110 return NULL;
111 if (dev->netdev_ops != &xfrmi_netdev_ops)
112 return NULL;
113
114 return netdev_priv(dev);
Steffen Klassertf203b762018-06-12 14:07:12 +0200115}
116
117static void xfrmi_link(struct xfrmi_net *xfrmn, struct xfrm_if *xi)
118{
Eyal Birgere98e4452020-07-09 13:16:52 +0300119 struct xfrm_if __rcu **xip = &xfrmn->xfrmi[xfrmi_hash(xi->p.if_id)];
Steffen Klassertf203b762018-06-12 14:07:12 +0200120
121 rcu_assign_pointer(xi->next , rtnl_dereference(*xip));
122 rcu_assign_pointer(*xip, xi);
123}
124
125static void xfrmi_unlink(struct xfrmi_net *xfrmn, struct xfrm_if *xi)
126{
127 struct xfrm_if __rcu **xip;
128 struct xfrm_if *iter;
129
Eyal Birgere98e4452020-07-09 13:16:52 +0300130 for (xip = &xfrmn->xfrmi[xfrmi_hash(xi->p.if_id)];
Steffen Klassertf203b762018-06-12 14:07:12 +0200131 (iter = rtnl_dereference(*xip)) != NULL;
132 xip = &iter->next) {
133 if (xi == iter) {
134 rcu_assign_pointer(*xip, xi->next);
135 break;
136 }
137 }
138}
139
140static void xfrmi_dev_free(struct net_device *dev)
141{
Li RongQing4da40252018-09-30 15:06:06 +0800142 struct xfrm_if *xi = netdev_priv(dev);
143
144 gro_cells_destroy(&xi->gro_cells);
Steffen Klassertf203b762018-06-12 14:07:12 +0200145 free_percpu(dev->tstats);
146}
147
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200148static int xfrmi_create(struct net_device *dev)
Steffen Klassertf203b762018-06-12 14:07:12 +0200149{
150 struct xfrm_if *xi = netdev_priv(dev);
151 struct net *net = dev_net(dev);
152 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
153 int err;
154
155 dev->rtnl_link_ops = &xfrmi_link_ops;
156 err = register_netdevice(dev);
157 if (err < 0)
158 goto out;
159
Steffen Klassertf203b762018-06-12 14:07:12 +0200160 xfrmi_link(xfrmn, xi);
161
162 return 0;
163
164out:
165 return err;
166}
167
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200168static struct xfrm_if *xfrmi_locate(struct net *net, struct xfrm_if_parms *p)
Steffen Klassertf203b762018-06-12 14:07:12 +0200169{
170 struct xfrm_if __rcu **xip;
171 struct xfrm_if *xi;
172 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
173
Eyal Birgere98e4452020-07-09 13:16:52 +0300174 for (xip = &xfrmn->xfrmi[xfrmi_hash(p->if_id)];
Steffen Klassertf203b762018-06-12 14:07:12 +0200175 (xi = rtnl_dereference(*xip)) != NULL;
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200176 xip = &xi->next)
177 if (xi->p.if_id == p->if_id)
Steffen Klassertf203b762018-06-12 14:07:12 +0200178 return xi;
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200179
180 return NULL;
Steffen Klassertf203b762018-06-12 14:07:12 +0200181}
182
183static void xfrmi_dev_uninit(struct net_device *dev)
184{
185 struct xfrm_if *xi = netdev_priv(dev);
186 struct xfrmi_net *xfrmn = net_generic(xi->net, xfrmi_net_id);
187
188 xfrmi_unlink(xfrmn, xi);
Steffen Klassertf203b762018-06-12 14:07:12 +0200189}
190
191static void xfrmi_scrub_packet(struct sk_buff *skb, bool xnet)
192{
193 skb->tstamp = 0;
194 skb->pkt_type = PACKET_HOST;
195 skb->skb_iif = 0;
196 skb->ignore_df = 0;
197 skb_dst_drop(skb);
Florian Westphal895b5c92019-09-29 20:54:03 +0200198 nf_reset_ct(skb);
Steffen Klassertf203b762018-06-12 14:07:12 +0200199 nf_reset_trace(skb);
200
201 if (!xnet)
202 return;
203
204 ipvs_reset(skb);
205 secpath_reset(skb);
206 skb_orphan(skb);
207 skb->mark = 0;
208}
209
210static int xfrmi_rcv_cb(struct sk_buff *skb, int err)
211{
Florian Westphal4c145dc2019-03-29 21:16:31 +0100212 const struct xfrm_mode *inner_mode;
Steffen Klassertf203b762018-06-12 14:07:12 +0200213 struct net_device *dev;
214 struct xfrm_state *x;
215 struct xfrm_if *xi;
216 bool xnet;
217
Florian Westphal26912e32018-12-18 17:15:24 +0100218 if (err && !secpath_exists(skb))
Steffen Klassertf203b762018-06-12 14:07:12 +0200219 return 0;
220
221 x = xfrm_input_state(skb);
222
223 xi = xfrmi_lookup(xs_net(x), x);
224 if (!xi)
225 return 1;
226
227 dev = xi->dev;
228 skb->dev = dev;
229
230 if (err) {
231 dev->stats.rx_errors++;
232 dev->stats.rx_dropped++;
233
234 return 0;
235 }
236
237 xnet = !net_eq(xi->net, dev_net(skb->dev));
238
239 if (xnet) {
Florian Westphalc9500d72019-03-29 21:16:32 +0100240 inner_mode = &x->inner_mode;
Steffen Klassertf203b762018-06-12 14:07:12 +0200241
242 if (x->sel.family == AF_UNSPEC) {
243 inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
244 if (inner_mode == NULL) {
245 XFRM_INC_STATS(dev_net(skb->dev),
246 LINUX_MIB_XFRMINSTATEMODEERROR);
247 return -EINVAL;
248 }
249 }
250
251 if (!xfrm_policy_check(NULL, XFRM_POLICY_IN, skb,
Florian Westphalb45714b2019-03-29 21:16:23 +0100252 inner_mode->family))
Steffen Klassertf203b762018-06-12 14:07:12 +0200253 return -EPERM;
254 }
255
256 xfrmi_scrub_packet(skb, xnet);
Fabian Frederickc8521622020-10-05 22:36:34 +0200257 dev_sw_netstats_rx_add(dev, skb->len);
Steffen Klassertf203b762018-06-12 14:07:12 +0200258
259 return 0;
260}
261
262static int
263xfrmi_xmit2(struct sk_buff *skb, struct net_device *dev, struct flowi *fl)
264{
265 struct xfrm_if *xi = netdev_priv(dev);
266 struct net_device_stats *stats = &xi->dev->stats;
267 struct dst_entry *dst = skb_dst(skb);
268 unsigned int length = skb->len;
269 struct net_device *tdev;
270 struct xfrm_state *x;
271 int err = -1;
272 int mtu;
273
Steffen Klassertf203b762018-06-12 14:07:12 +0200274 dst_hold(dst);
Benedict Wongbc56b332018-07-19 10:50:44 -0700275 dst = xfrm_lookup_with_ifid(xi->net, dst, fl, NULL, 0, xi->p.if_id);
Steffen Klassertf203b762018-06-12 14:07:12 +0200276 if (IS_ERR(dst)) {
277 err = PTR_ERR(dst);
278 dst = NULL;
279 goto tx_err_link_failure;
280 }
281
282 x = dst->xfrm;
283 if (!x)
284 goto tx_err_link_failure;
285
286 if (x->if_id != xi->p.if_id)
287 goto tx_err_link_failure;
288
289 tdev = dst->dev;
290
291 if (tdev == dev) {
292 stats->collisions++;
293 net_warn_ratelimited("%s: Local routing loop detected!\n",
Nicolas Dichtele0aaa332019-07-15 12:00:21 +0200294 dev->name);
Steffen Klassertf203b762018-06-12 14:07:12 +0200295 goto tx_err_dst_release;
296 }
297
298 mtu = dst_mtu(dst);
Eyal Birger18f97692021-01-06 08:10:46 +0200299 if ((!skb_is_gso(skb) && skb->len > mtu) ||
300 (skb_is_gso(skb) && !skb_gso_validate_network_len(skb, mtu))) {
Xu Wang8aaea2b2020-01-13 09:00:36 +0000301 skb_dst_update_pmtu_no_confirm(skb, mtu);
Steffen Klassertf203b762018-06-12 14:07:12 +0200302
303 if (skb->protocol == htons(ETH_P_IPV6)) {
304 if (mtu < IPV6_MIN_MTU)
305 mtu = IPV6_MIN_MTU;
306
Jason A. Donenfeld45942ba2020-02-11 20:47:09 +0100307 icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Steffen Klassertf203b762018-06-12 14:07:12 +0200308 } else {
Eyal Birger8fc0e3b2021-02-20 15:01:15 +0200309 if (!(ip_hdr(skb)->frag_off & htons(IP_DF)))
310 goto xmit;
Jason A. Donenfeld45942ba2020-02-11 20:47:09 +0100311 icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
312 htonl(mtu));
Steffen Klassertf203b762018-06-12 14:07:12 +0200313 }
314
315 dst_release(dst);
316 return -EMSGSIZE;
317 }
318
Eyal Birger8fc0e3b2021-02-20 15:01:15 +0200319xmit:
Steffen Klassertf203b762018-06-12 14:07:12 +0200320 xfrmi_scrub_packet(skb, !net_eq(xi->net, dev_net(dev)));
321 skb_dst_set(skb, dst);
322 skb->dev = tdev;
323
324 err = dst_output(xi->net, skb->sk, skb);
325 if (net_xmit_eval(err) == 0) {
Lev Stipakov0064c5c2020-11-13 23:59:40 +0200326 dev_sw_netstats_tx_add(dev, 1, length);
Steffen Klassertf203b762018-06-12 14:07:12 +0200327 } else {
328 stats->tx_errors++;
329 stats->tx_aborted_errors++;
330 }
331
332 return 0;
333tx_err_link_failure:
334 stats->tx_carrier_errors++;
335 dst_link_failure(skb);
336tx_err_dst_release:
337 dst_release(dst);
338 return err;
339}
340
341static netdev_tx_t xfrmi_xmit(struct sk_buff *skb, struct net_device *dev)
342{
343 struct xfrm_if *xi = netdev_priv(dev);
344 struct net_device_stats *stats = &xi->dev->stats;
Nicolas Dichtelf0423652020-01-13 09:32:47 +0100345 struct dst_entry *dst = skb_dst(skb);
Steffen Klassertf203b762018-06-12 14:07:12 +0200346 struct flowi fl;
347 int ret;
348
349 memset(&fl, 0, sizeof(fl));
350
351 switch (skb->protocol) {
352 case htons(ETH_P_IPV6):
353 xfrm_decode_session(skb, &fl, AF_INET6);
354 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
Nicolas Dichtelf0423652020-01-13 09:32:47 +0100355 if (!dst) {
356 fl.u.ip6.flowi6_oif = dev->ifindex;
357 fl.u.ip6.flowi6_flags |= FLOWI_FLAG_ANYSRC;
358 dst = ip6_route_output(dev_net(dev), NULL, &fl.u.ip6);
359 if (dst->error) {
360 dst_release(dst);
361 stats->tx_carrier_errors++;
362 goto tx_err;
363 }
364 skb_dst_set(skb, dst);
365 }
Steffen Klassertf203b762018-06-12 14:07:12 +0200366 break;
367 case htons(ETH_P_IP):
368 xfrm_decode_session(skb, &fl, AF_INET);
369 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
Nicolas Dichtelf0423652020-01-13 09:32:47 +0100370 if (!dst) {
371 struct rtable *rt;
372
373 fl.u.ip4.flowi4_oif = dev->ifindex;
374 fl.u.ip4.flowi4_flags |= FLOWI_FLAG_ANYSRC;
375 rt = __ip_route_output_key(dev_net(dev), &fl.u.ip4);
376 if (IS_ERR(rt)) {
377 stats->tx_carrier_errors++;
378 goto tx_err;
379 }
380 skb_dst_set(skb, &rt->dst);
381 }
Steffen Klassertf203b762018-06-12 14:07:12 +0200382 break;
383 default:
384 goto tx_err;
385 }
386
Nicolas Dichtel22d65522019-07-15 12:00:23 +0200387 fl.flowi_oif = xi->p.link;
Steffen Klassertf203b762018-06-12 14:07:12 +0200388
389 ret = xfrmi_xmit2(skb, dev, &fl);
390 if (ret < 0)
391 goto tx_err;
392
393 return NETDEV_TX_OK;
394
395tx_err:
396 stats->tx_errors++;
397 stats->tx_dropped++;
398 kfree_skb(skb);
399 return NETDEV_TX_OK;
400}
401
402static int xfrmi4_err(struct sk_buff *skb, u32 info)
403{
404 const struct iphdr *iph = (const struct iphdr *)skb->data;
405 struct net *net = dev_net(skb->dev);
406 int protocol = iph->protocol;
407 struct ip_comp_hdr *ipch;
408 struct ip_esp_hdr *esph;
409 struct ip_auth_hdr *ah ;
410 struct xfrm_state *x;
411 struct xfrm_if *xi;
412 __be32 spi;
413
414 switch (protocol) {
415 case IPPROTO_ESP:
416 esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
417 spi = esph->spi;
418 break;
419 case IPPROTO_AH:
420 ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2));
421 spi = ah->spi;
422 break;
423 case IPPROTO_COMP:
424 ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
425 spi = htonl(ntohs(ipch->cpi));
426 break;
427 default:
428 return 0;
429 }
430
431 switch (icmp_hdr(skb)->type) {
432 case ICMP_DEST_UNREACH:
433 if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
434 return 0;
Gustavo A. R. Silva135436a2020-11-20 12:41:00 -0600435 break;
Steffen Klassertf203b762018-06-12 14:07:12 +0200436 case ICMP_REDIRECT:
437 break;
438 default:
439 return 0;
440 }
441
442 x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
443 spi, protocol, AF_INET);
444 if (!x)
445 return 0;
446
447 xi = xfrmi_lookup(net, x);
448 if (!xi) {
449 xfrm_state_put(x);
450 return -1;
451 }
452
453 if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
Maciej Żenczykowskid888f392018-09-25 20:56:26 -0700454 ipv4_update_pmtu(skb, net, info, 0, protocol);
Steffen Klassertf203b762018-06-12 14:07:12 +0200455 else
Maciej Żenczykowski1042caa2018-09-25 20:56:27 -0700456 ipv4_redirect(skb, net, 0, protocol);
Steffen Klassertf203b762018-06-12 14:07:12 +0200457 xfrm_state_put(x);
458
459 return 0;
460}
461
462static int xfrmi6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
463 u8 type, u8 code, int offset, __be32 info)
464{
465 const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
466 struct net *net = dev_net(skb->dev);
467 int protocol = iph->nexthdr;
468 struct ip_comp_hdr *ipch;
469 struct ip_esp_hdr *esph;
470 struct ip_auth_hdr *ah;
471 struct xfrm_state *x;
472 struct xfrm_if *xi;
473 __be32 spi;
474
475 switch (protocol) {
476 case IPPROTO_ESP:
477 esph = (struct ip_esp_hdr *)(skb->data + offset);
478 spi = esph->spi;
479 break;
480 case IPPROTO_AH:
481 ah = (struct ip_auth_hdr *)(skb->data + offset);
482 spi = ah->spi;
483 break;
484 case IPPROTO_COMP:
485 ipch = (struct ip_comp_hdr *)(skb->data + offset);
486 spi = htonl(ntohs(ipch->cpi));
487 break;
488 default:
489 return 0;
490 }
491
492 if (type != ICMPV6_PKT_TOOBIG &&
493 type != NDISC_REDIRECT)
494 return 0;
495
496 x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
497 spi, protocol, AF_INET6);
498 if (!x)
499 return 0;
500
501 xi = xfrmi_lookup(net, x);
502 if (!xi) {
503 xfrm_state_put(x);
504 return -1;
505 }
506
507 if (type == NDISC_REDIRECT)
508 ip6_redirect(skb, net, skb->dev->ifindex, 0,
509 sock_net_uid(net, NULL));
510 else
511 ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
512 xfrm_state_put(x);
513
514 return 0;
515}
516
517static int xfrmi_change(struct xfrm_if *xi, const struct xfrm_if_parms *p)
518{
519 if (xi->p.link != p->link)
520 return -EINVAL;
521
522 xi->p.if_id = p->if_id;
523
524 return 0;
525}
526
527static int xfrmi_update(struct xfrm_if *xi, struct xfrm_if_parms *p)
528{
Nicolas Dichtelc5d10302019-07-15 12:00:22 +0200529 struct net *net = xi->net;
Steffen Klassertf203b762018-06-12 14:07:12 +0200530 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
531 int err;
532
533 xfrmi_unlink(xfrmn, xi);
534 synchronize_net();
535 err = xfrmi_change(xi, p);
536 xfrmi_link(xfrmn, xi);
537 netdev_state_change(xi->dev);
538 return err;
539}
540
Steffen Klassertf203b762018-06-12 14:07:12 +0200541static int xfrmi_get_iflink(const struct net_device *dev)
542{
543 struct xfrm_if *xi = netdev_priv(dev);
544
Nicolas Dichtel22d65522019-07-15 12:00:23 +0200545 return xi->p.link;
Steffen Klassertf203b762018-06-12 14:07:12 +0200546}
547
Steffen Klassertf203b762018-06-12 14:07:12 +0200548static const struct net_device_ops xfrmi_netdev_ops = {
549 .ndo_init = xfrmi_dev_init,
550 .ndo_uninit = xfrmi_dev_uninit,
551 .ndo_start_xmit = xfrmi_xmit,
Lev Stipakov0064c5c2020-11-13 23:59:40 +0200552 .ndo_get_stats64 = dev_get_tstats64,
Steffen Klassertf203b762018-06-12 14:07:12 +0200553 .ndo_get_iflink = xfrmi_get_iflink,
554};
555
556static void xfrmi_dev_setup(struct net_device *dev)
557{
558 dev->netdev_ops = &xfrmi_netdev_ops;
Jason A. Donenfeld8f9a1fa42020-06-29 19:06:25 -0600559 dev->header_ops = &ip_tunnel_header_ops;
Steffen Klassertf203b762018-06-12 14:07:12 +0200560 dev->type = ARPHRD_NONE;
Steffen Klassertf203b762018-06-12 14:07:12 +0200561 dev->mtu = ETH_DATA_LEN;
562 dev->min_mtu = ETH_MIN_MTU;
Nicolas Dichtelf0423652020-01-13 09:32:47 +0100563 dev->max_mtu = IP_MAX_MTU;
Steffen Klassertf203b762018-06-12 14:07:12 +0200564 dev->flags = IFF_NOARP;
565 dev->needs_free_netdev = true;
566 dev->priv_destructor = xfrmi_dev_free;
567 netif_keep_dst(dev);
Nicolas Dichtel22d65522019-07-15 12:00:23 +0200568
569 eth_broadcast_addr(dev->broadcast);
Steffen Klassertf203b762018-06-12 14:07:12 +0200570}
571
Eyal Birger18f97692021-01-06 08:10:46 +0200572#define XFRMI_FEATURES (NETIF_F_SG | \
573 NETIF_F_FRAGLIST | \
574 NETIF_F_GSO_SOFTWARE | \
575 NETIF_F_HW_CSUM)
576
Steffen Klassertf203b762018-06-12 14:07:12 +0200577static int xfrmi_dev_init(struct net_device *dev)
578{
579 struct xfrm_if *xi = netdev_priv(dev);
Nicolas Dichtel22d65522019-07-15 12:00:23 +0200580 struct net_device *phydev = __dev_get_by_index(xi->net, xi->p.link);
Steffen Klassertf203b762018-06-12 14:07:12 +0200581 int err;
582
583 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
584 if (!dev->tstats)
585 return -ENOMEM;
586
587 err = gro_cells_init(&xi->gro_cells, dev);
588 if (err) {
589 free_percpu(dev->tstats);
590 return err;
591 }
592
593 dev->features |= NETIF_F_LLTX;
Eyal Birger18f97692021-01-06 08:10:46 +0200594 dev->features |= XFRMI_FEATURES;
595 dev->hw_features |= XFRMI_FEATURES;
Steffen Klassertf203b762018-06-12 14:07:12 +0200596
Nicolas Dichtel22d65522019-07-15 12:00:23 +0200597 if (phydev) {
598 dev->needed_headroom = phydev->needed_headroom;
599 dev->needed_tailroom = phydev->needed_tailroom;
Steffen Klassertf203b762018-06-12 14:07:12 +0200600
Nicolas Dichtel22d65522019-07-15 12:00:23 +0200601 if (is_zero_ether_addr(dev->dev_addr))
602 eth_hw_addr_inherit(dev, phydev);
603 if (is_zero_ether_addr(dev->broadcast))
604 memcpy(dev->broadcast, phydev->broadcast,
605 dev->addr_len);
606 } else {
607 eth_hw_addr_random(dev);
608 eth_broadcast_addr(dev->broadcast);
609 }
Steffen Klassertf203b762018-06-12 14:07:12 +0200610
611 return 0;
612}
613
614static int xfrmi_validate(struct nlattr *tb[], struct nlattr *data[],
615 struct netlink_ext_ack *extack)
616{
617 return 0;
618}
619
620static void xfrmi_netlink_parms(struct nlattr *data[],
621 struct xfrm_if_parms *parms)
622{
623 memset(parms, 0, sizeof(*parms));
624
625 if (!data)
626 return;
627
628 if (data[IFLA_XFRM_LINK])
629 parms->link = nla_get_u32(data[IFLA_XFRM_LINK]);
630
631 if (data[IFLA_XFRM_IF_ID])
632 parms->if_id = nla_get_u32(data[IFLA_XFRM_IF_ID]);
633}
634
635static int xfrmi_newlink(struct net *src_net, struct net_device *dev,
636 struct nlattr *tb[], struct nlattr *data[],
637 struct netlink_ext_ack *extack)
638{
639 struct net *net = dev_net(dev);
Antony Antony8dce4392021-12-12 11:34:30 +0100640 struct xfrm_if_parms p = {};
Steffen Klassertf203b762018-06-12 14:07:12 +0200641 struct xfrm_if *xi;
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200642 int err;
Steffen Klassertf203b762018-06-12 14:07:12 +0200643
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200644 xfrmi_netlink_parms(data, &p);
Antony Antony8dce4392021-12-12 11:34:30 +0100645 if (!p.if_id) {
646 NL_SET_ERR_MSG(extack, "if_id must be non zero");
647 return -EINVAL;
648 }
649
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200650 xi = xfrmi_locate(net, &p);
651 if (xi)
652 return -EEXIST;
653
654 xi = netdev_priv(dev);
655 xi->p = p;
656 xi->net = net;
657 xi->dev = dev;
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200658
659 err = xfrmi_create(dev);
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200660 return err;
Steffen Klassertf203b762018-06-12 14:07:12 +0200661}
662
663static void xfrmi_dellink(struct net_device *dev, struct list_head *head)
664{
665 unregister_netdevice_queue(dev, head);
666}
667
668static int xfrmi_changelink(struct net_device *dev, struct nlattr *tb[],
669 struct nlattr *data[],
670 struct netlink_ext_ack *extack)
671{
Nicolas Dichtelc5d10302019-07-15 12:00:22 +0200672 struct xfrm_if *xi = netdev_priv(dev);
673 struct net *net = xi->net;
Antony Antony8dce4392021-12-12 11:34:30 +0100674 struct xfrm_if_parms p = {};
675
676 if (!p.if_id) {
677 NL_SET_ERR_MSG(extack, "if_id must be non zero");
678 return -EINVAL;
679 }
Steffen Klassertf203b762018-06-12 14:07:12 +0200680
Nicolas Dichtele9e7e852019-07-15 12:00:20 +0200681 xfrmi_netlink_parms(data, &p);
682 xi = xfrmi_locate(net, &p);
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200683 if (!xi) {
Benedict Wong44e2b832018-07-25 13:45:29 -0700684 xi = netdev_priv(dev);
685 } else {
Steffen Klassertf203b762018-06-12 14:07:12 +0200686 if (xi->dev != dev)
687 return -EEXIST;
Benedict Wong44e2b832018-07-25 13:45:29 -0700688 }
Steffen Klassertf203b762018-06-12 14:07:12 +0200689
Nicolas Dichtele9e7e852019-07-15 12:00:20 +0200690 return xfrmi_update(xi, &p);
Steffen Klassertf203b762018-06-12 14:07:12 +0200691}
692
693static size_t xfrmi_get_size(const struct net_device *dev)
694{
695 return
696 /* IFLA_XFRM_LINK */
697 nla_total_size(4) +
698 /* IFLA_XFRM_IF_ID */
699 nla_total_size(4) +
700 0;
701}
702
703static int xfrmi_fill_info(struct sk_buff *skb, const struct net_device *dev)
704{
705 struct xfrm_if *xi = netdev_priv(dev);
706 struct xfrm_if_parms *parm = &xi->p;
707
708 if (nla_put_u32(skb, IFLA_XFRM_LINK, parm->link) ||
709 nla_put_u32(skb, IFLA_XFRM_IF_ID, parm->if_id))
710 goto nla_put_failure;
711 return 0;
712
713nla_put_failure:
714 return -EMSGSIZE;
715}
716
Wei Yongjun211d6f22018-07-28 06:49:48 +0000717static struct net *xfrmi_get_link_net(const struct net_device *dev)
Steffen Klassertf203b762018-06-12 14:07:12 +0200718{
719 struct xfrm_if *xi = netdev_priv(dev);
720
Nicolas Dichtelc5d10302019-07-15 12:00:22 +0200721 return xi->net;
Steffen Klassertf203b762018-06-12 14:07:12 +0200722}
723
724static const struct nla_policy xfrmi_policy[IFLA_XFRM_MAX + 1] = {
725 [IFLA_XFRM_LINK] = { .type = NLA_U32 },
726 [IFLA_XFRM_IF_ID] = { .type = NLA_U32 },
727};
728
729static struct rtnl_link_ops xfrmi_link_ops __read_mostly = {
730 .kind = "xfrm",
731 .maxtype = IFLA_XFRM_MAX,
732 .policy = xfrmi_policy,
733 .priv_size = sizeof(struct xfrm_if),
734 .setup = xfrmi_dev_setup,
735 .validate = xfrmi_validate,
736 .newlink = xfrmi_newlink,
737 .dellink = xfrmi_dellink,
738 .changelink = xfrmi_changelink,
739 .get_size = xfrmi_get_size,
740 .fill_info = xfrmi_fill_info,
741 .get_link_net = xfrmi_get_link_net,
742};
743
Nicolas Dichtelc95c5f52020-04-23 00:06:45 +0200744static void __net_exit xfrmi_exit_batch_net(struct list_head *net_exit_list)
745{
746 struct net *net;
747 LIST_HEAD(list);
748
749 rtnl_lock();
750 list_for_each_entry(net, net_exit_list, exit_list) {
751 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
752 struct xfrm_if __rcu **xip;
753 struct xfrm_if *xi;
Eyal Birgere98e4452020-07-09 13:16:52 +0300754 int i;
Nicolas Dichtelc95c5f52020-04-23 00:06:45 +0200755
Eyal Birgere98e4452020-07-09 13:16:52 +0300756 for (i = 0; i < XFRMI_HASH_SIZE; i++) {
757 for (xip = &xfrmn->xfrmi[i];
758 (xi = rtnl_dereference(*xip)) != NULL;
759 xip = &xi->next)
760 unregister_netdevice_queue(xi->dev, &list);
761 }
Nicolas Dichtelc95c5f52020-04-23 00:06:45 +0200762 }
763 unregister_netdevice_many(&list);
764 rtnl_unlock();
765}
766
Steffen Klassertf203b762018-06-12 14:07:12 +0200767static struct pernet_operations xfrmi_net_ops = {
Nicolas Dichtelc95c5f52020-04-23 00:06:45 +0200768 .exit_batch = xfrmi_exit_batch_net,
Steffen Klassertf203b762018-06-12 14:07:12 +0200769 .id = &xfrmi_net_id,
770 .size = sizeof(struct xfrmi_net),
771};
772
773static struct xfrm6_protocol xfrmi_esp6_protocol __read_mostly = {
774 .handler = xfrm6_rcv,
Sabrina Dubroca0146dca2020-04-27 17:59:34 +0200775 .input_handler = xfrm_input,
Steffen Klassertf203b762018-06-12 14:07:12 +0200776 .cb_handler = xfrmi_rcv_cb,
777 .err_handler = xfrmi6_err,
778 .priority = 10,
779};
780
781static struct xfrm6_protocol xfrmi_ah6_protocol __read_mostly = {
782 .handler = xfrm6_rcv,
Sabrina Dubroca0146dca2020-04-27 17:59:34 +0200783 .input_handler = xfrm_input,
Steffen Klassertf203b762018-06-12 14:07:12 +0200784 .cb_handler = xfrmi_rcv_cb,
785 .err_handler = xfrmi6_err,
786 .priority = 10,
787};
788
789static struct xfrm6_protocol xfrmi_ipcomp6_protocol __read_mostly = {
790 .handler = xfrm6_rcv,
Sabrina Dubroca0146dca2020-04-27 17:59:34 +0200791 .input_handler = xfrm_input,
Steffen Klassertf203b762018-06-12 14:07:12 +0200792 .cb_handler = xfrmi_rcv_cb,
793 .err_handler = xfrmi6_err,
794 .priority = 10,
795};
796
Xin Long0a0d93b2020-07-17 15:02:30 +0800797#if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
Xin Longd7b360c2020-07-06 20:01:37 +0800798static int xfrmi6_rcv_tunnel(struct sk_buff *skb)
799{
800 const xfrm_address_t *saddr;
801 __be32 spi;
802
803 saddr = (const xfrm_address_t *)&ipv6_hdr(skb)->saddr;
804 spi = xfrm6_tunnel_spi_lookup(dev_net(skb->dev), saddr);
805
806 return xfrm6_rcv_spi(skb, IPPROTO_IPV6, spi, NULL);
807}
808
809static struct xfrm6_tunnel xfrmi_ipv6_handler __read_mostly = {
810 .handler = xfrmi6_rcv_tunnel,
811 .cb_handler = xfrmi_rcv_cb,
812 .err_handler = xfrmi6_err,
Xin Long7fe94612020-10-08 16:13:24 +0800813 .priority = 2,
Xin Longd7b360c2020-07-06 20:01:37 +0800814};
Xin Long8b404f42020-07-13 15:42:38 +0800815
816static struct xfrm6_tunnel xfrmi_ip6ip_handler __read_mostly = {
817 .handler = xfrmi6_rcv_tunnel,
818 .cb_handler = xfrmi_rcv_cb,
819 .err_handler = xfrmi6_err,
Xin Long7fe94612020-10-08 16:13:24 +0800820 .priority = 2,
Xin Long8b404f42020-07-13 15:42:38 +0800821};
Xin Longd7b360c2020-07-06 20:01:37 +0800822#endif
823
Steffen Klassertf203b762018-06-12 14:07:12 +0200824static struct xfrm4_protocol xfrmi_esp4_protocol __read_mostly = {
825 .handler = xfrm4_rcv,
826 .input_handler = xfrm_input,
827 .cb_handler = xfrmi_rcv_cb,
828 .err_handler = xfrmi4_err,
829 .priority = 10,
830};
831
832static struct xfrm4_protocol xfrmi_ah4_protocol __read_mostly = {
833 .handler = xfrm4_rcv,
834 .input_handler = xfrm_input,
835 .cb_handler = xfrmi_rcv_cb,
836 .err_handler = xfrmi4_err,
837 .priority = 10,
838};
839
840static struct xfrm4_protocol xfrmi_ipcomp4_protocol __read_mostly = {
841 .handler = xfrm4_rcv,
842 .input_handler = xfrm_input,
843 .cb_handler = xfrmi_rcv_cb,
844 .err_handler = xfrmi4_err,
845 .priority = 10,
846};
847
Xin Long0a0d93b2020-07-17 15:02:30 +0800848#if IS_REACHABLE(CONFIG_INET_XFRM_TUNNEL)
Xin Longda9bbf02020-07-06 20:01:38 +0800849static int xfrmi4_rcv_tunnel(struct sk_buff *skb)
850{
851 return xfrm4_rcv_spi(skb, IPPROTO_IPIP, ip_hdr(skb)->saddr);
852}
853
854static struct xfrm_tunnel xfrmi_ipip_handler __read_mostly = {
855 .handler = xfrmi4_rcv_tunnel,
856 .cb_handler = xfrmi_rcv_cb,
857 .err_handler = xfrmi4_err,
Xin Long7fe94612020-10-08 16:13:24 +0800858 .priority = 3,
Xin Longda9bbf02020-07-06 20:01:38 +0800859};
Xin Long8b404f42020-07-13 15:42:38 +0800860
861static struct xfrm_tunnel xfrmi_ipip6_handler __read_mostly = {
862 .handler = xfrmi4_rcv_tunnel,
863 .cb_handler = xfrmi_rcv_cb,
864 .err_handler = xfrmi4_err,
Xin Long7fe94612020-10-08 16:13:24 +0800865 .priority = 2,
Xin Long8b404f42020-07-13 15:42:38 +0800866};
Xin Longda9bbf02020-07-06 20:01:38 +0800867#endif
868
Steffen Klassertf203b762018-06-12 14:07:12 +0200869static int __init xfrmi4_init(void)
870{
871 int err;
872
873 err = xfrm4_protocol_register(&xfrmi_esp4_protocol, IPPROTO_ESP);
874 if (err < 0)
875 goto xfrm_proto_esp_failed;
876 err = xfrm4_protocol_register(&xfrmi_ah4_protocol, IPPROTO_AH);
877 if (err < 0)
878 goto xfrm_proto_ah_failed;
879 err = xfrm4_protocol_register(&xfrmi_ipcomp4_protocol, IPPROTO_COMP);
880 if (err < 0)
881 goto xfrm_proto_comp_failed;
Xin Long0a0d93b2020-07-17 15:02:30 +0800882#if IS_REACHABLE(CONFIG_INET_XFRM_TUNNEL)
Xin Longda9bbf02020-07-06 20:01:38 +0800883 err = xfrm4_tunnel_register(&xfrmi_ipip_handler, AF_INET);
884 if (err < 0)
885 goto xfrm_tunnel_ipip_failed;
Xin Long8b404f42020-07-13 15:42:38 +0800886 err = xfrm4_tunnel_register(&xfrmi_ipip6_handler, AF_INET6);
Xin Longda9bbf02020-07-06 20:01:38 +0800887 if (err < 0)
888 goto xfrm_tunnel_ipip6_failed;
889#endif
Steffen Klassertf203b762018-06-12 14:07:12 +0200890
891 return 0;
892
Xin Long0a0d93b2020-07-17 15:02:30 +0800893#if IS_REACHABLE(CONFIG_INET_XFRM_TUNNEL)
Xin Longda9bbf02020-07-06 20:01:38 +0800894xfrm_tunnel_ipip6_failed:
895 xfrm4_tunnel_deregister(&xfrmi_ipip_handler, AF_INET);
896xfrm_tunnel_ipip_failed:
897 xfrm4_protocol_deregister(&xfrmi_ipcomp4_protocol, IPPROTO_COMP);
898#endif
Steffen Klassertf203b762018-06-12 14:07:12 +0200899xfrm_proto_comp_failed:
900 xfrm4_protocol_deregister(&xfrmi_ah4_protocol, IPPROTO_AH);
901xfrm_proto_ah_failed:
902 xfrm4_protocol_deregister(&xfrmi_esp4_protocol, IPPROTO_ESP);
903xfrm_proto_esp_failed:
904 return err;
905}
906
907static void xfrmi4_fini(void)
908{
Xin Long0a0d93b2020-07-17 15:02:30 +0800909#if IS_REACHABLE(CONFIG_INET_XFRM_TUNNEL)
Xin Long8b404f42020-07-13 15:42:38 +0800910 xfrm4_tunnel_deregister(&xfrmi_ipip6_handler, AF_INET6);
Xin Longda9bbf02020-07-06 20:01:38 +0800911 xfrm4_tunnel_deregister(&xfrmi_ipip_handler, AF_INET);
912#endif
Steffen Klassertf203b762018-06-12 14:07:12 +0200913 xfrm4_protocol_deregister(&xfrmi_ipcomp4_protocol, IPPROTO_COMP);
914 xfrm4_protocol_deregister(&xfrmi_ah4_protocol, IPPROTO_AH);
915 xfrm4_protocol_deregister(&xfrmi_esp4_protocol, IPPROTO_ESP);
916}
917
918static int __init xfrmi6_init(void)
919{
920 int err;
921
922 err = xfrm6_protocol_register(&xfrmi_esp6_protocol, IPPROTO_ESP);
923 if (err < 0)
924 goto xfrm_proto_esp_failed;
925 err = xfrm6_protocol_register(&xfrmi_ah6_protocol, IPPROTO_AH);
926 if (err < 0)
927 goto xfrm_proto_ah_failed;
928 err = xfrm6_protocol_register(&xfrmi_ipcomp6_protocol, IPPROTO_COMP);
929 if (err < 0)
930 goto xfrm_proto_comp_failed;
Xin Long0a0d93b2020-07-17 15:02:30 +0800931#if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
Xin Longd7b360c2020-07-06 20:01:37 +0800932 err = xfrm6_tunnel_register(&xfrmi_ipv6_handler, AF_INET6);
933 if (err < 0)
934 goto xfrm_tunnel_ipv6_failed;
Xin Long8b404f42020-07-13 15:42:38 +0800935 err = xfrm6_tunnel_register(&xfrmi_ip6ip_handler, AF_INET);
Xin Longd7b360c2020-07-06 20:01:37 +0800936 if (err < 0)
937 goto xfrm_tunnel_ip6ip_failed;
938#endif
Steffen Klassertf203b762018-06-12 14:07:12 +0200939
940 return 0;
941
Xin Long0a0d93b2020-07-17 15:02:30 +0800942#if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
Xin Longd7b360c2020-07-06 20:01:37 +0800943xfrm_tunnel_ip6ip_failed:
944 xfrm6_tunnel_deregister(&xfrmi_ipv6_handler, AF_INET6);
945xfrm_tunnel_ipv6_failed:
946 xfrm6_protocol_deregister(&xfrmi_ipcomp6_protocol, IPPROTO_COMP);
947#endif
Steffen Klassertf203b762018-06-12 14:07:12 +0200948xfrm_proto_comp_failed:
949 xfrm6_protocol_deregister(&xfrmi_ah6_protocol, IPPROTO_AH);
950xfrm_proto_ah_failed:
951 xfrm6_protocol_deregister(&xfrmi_esp6_protocol, IPPROTO_ESP);
952xfrm_proto_esp_failed:
953 return err;
954}
955
956static void xfrmi6_fini(void)
957{
Xin Long0a0d93b2020-07-17 15:02:30 +0800958#if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
Xin Long8b404f42020-07-13 15:42:38 +0800959 xfrm6_tunnel_deregister(&xfrmi_ip6ip_handler, AF_INET);
Xin Longd7b360c2020-07-06 20:01:37 +0800960 xfrm6_tunnel_deregister(&xfrmi_ipv6_handler, AF_INET6);
961#endif
Steffen Klassertf203b762018-06-12 14:07:12 +0200962 xfrm6_protocol_deregister(&xfrmi_ipcomp6_protocol, IPPROTO_COMP);
963 xfrm6_protocol_deregister(&xfrmi_ah6_protocol, IPPROTO_AH);
964 xfrm6_protocol_deregister(&xfrmi_esp6_protocol, IPPROTO_ESP);
965}
966
967static const struct xfrm_if_cb xfrm_if_cb = {
968 .decode_session = xfrmi_decode_session,
969};
970
971static int __init xfrmi_init(void)
972{
973 const char *msg;
974 int err;
975
976 pr_info("IPsec XFRM device driver\n");
977
978 msg = "tunnel device";
979 err = register_pernet_device(&xfrmi_net_ops);
980 if (err < 0)
981 goto pernet_dev_failed;
982
983 msg = "xfrm4 protocols";
984 err = xfrmi4_init();
985 if (err < 0)
986 goto xfrmi4_failed;
987
988 msg = "xfrm6 protocols";
989 err = xfrmi6_init();
990 if (err < 0)
991 goto xfrmi6_failed;
992
993
994 msg = "netlink interface";
995 err = rtnl_link_register(&xfrmi_link_ops);
996 if (err < 0)
997 goto rtnl_link_failed;
998
999 xfrm_if_register_cb(&xfrm_if_cb);
1000
1001 return err;
1002
1003rtnl_link_failed:
1004 xfrmi6_fini();
1005xfrmi6_failed:
1006 xfrmi4_fini();
1007xfrmi4_failed:
1008 unregister_pernet_device(&xfrmi_net_ops);
1009pernet_dev_failed:
1010 pr_err("xfrmi init: failed to register %s\n", msg);
1011 return err;
1012}
1013
1014static void __exit xfrmi_fini(void)
1015{
1016 xfrm_if_unregister_cb();
1017 rtnl_link_unregister(&xfrmi_link_ops);
1018 xfrmi4_fini();
1019 xfrmi6_fini();
1020 unregister_pernet_device(&xfrmi_net_ops);
1021}
1022
1023module_init(xfrmi_init);
1024module_exit(xfrmi_fini);
1025MODULE_LICENSE("GPL");
1026MODULE_ALIAS_RTNL_LINK("xfrm");
1027MODULE_ALIAS_NETDEV("xfrm0");
1028MODULE_AUTHOR("Steffen Klassert");
1029MODULE_DESCRIPTION("XFRM virtual interface");