blob: b9ef496d3d7cd650f80eaa4e11fd0386b502dbc1 [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>
40#include <net/addrconf.h>
41#include <net/xfrm.h>
42#include <net/net_namespace.h>
43#include <net/netns/generic.h>
44#include <linux/etherdevice.h>
45
46static int xfrmi_dev_init(struct net_device *dev);
47static void xfrmi_dev_setup(struct net_device *dev);
48static struct rtnl_link_ops xfrmi_link_ops __read_mostly;
49static unsigned int xfrmi_net_id __read_mostly;
50
51struct xfrmi_net {
52 /* lists for storing interfaces in use */
53 struct xfrm_if __rcu *xfrmi[1];
54};
55
56#define for_each_xfrmi_rcu(start, xi) \
57 for (xi = rcu_dereference(start); xi; xi = rcu_dereference(xi->next))
58
59static struct xfrm_if *xfrmi_lookup(struct net *net, struct xfrm_state *x)
60{
61 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
62 struct xfrm_if *xi;
63
64 for_each_xfrmi_rcu(xfrmn->xfrmi[0], xi) {
65 if (x->if_id == xi->p.if_id &&
66 (xi->dev->flags & IFF_UP))
67 return xi;
68 }
69
70 return NULL;
71}
72
Martin Willi025c65e2019-03-26 13:20:43 +010073static struct xfrm_if *xfrmi_decode_session(struct sk_buff *skb,
74 unsigned short family)
Steffen Klassertf203b762018-06-12 14:07:12 +020075{
76 struct xfrmi_net *xfrmn;
Steffen Klassertf203b762018-06-12 14:07:12 +020077 struct xfrm_if *xi;
Martin Willi025c65e2019-03-26 13:20:43 +010078 int ifindex = 0;
Steffen Klassertf203b762018-06-12 14:07:12 +020079
Tobias Brunner660899d2019-02-18 10:49:39 +010080 if (!secpath_exists(skb) || !skb->dev)
Steffen Klassertf203b762018-06-12 14:07:12 +020081 return NULL;
82
Martin Willi025c65e2019-03-26 13:20:43 +010083 switch (family) {
84 case AF_INET6:
85 ifindex = inet6_sdif(skb);
86 break;
87 case AF_INET:
88 ifindex = inet_sdif(skb);
89 break;
90 }
91 if (!ifindex)
92 ifindex = skb->dev->ifindex;
93
Tobias Brunner660899d2019-02-18 10:49:39 +010094 xfrmn = net_generic(xs_net(xfrm_input_state(skb)), xfrmi_net_id);
Steffen Klassertf203b762018-06-12 14:07:12 +020095
96 for_each_xfrmi_rcu(xfrmn->xfrmi[0], xi) {
97 if (ifindex == xi->dev->ifindex &&
98 (xi->dev->flags & IFF_UP))
99 return xi;
100 }
101
102 return NULL;
103}
104
105static void xfrmi_link(struct xfrmi_net *xfrmn, struct xfrm_if *xi)
106{
107 struct xfrm_if __rcu **xip = &xfrmn->xfrmi[0];
108
109 rcu_assign_pointer(xi->next , rtnl_dereference(*xip));
110 rcu_assign_pointer(*xip, xi);
111}
112
113static void xfrmi_unlink(struct xfrmi_net *xfrmn, struct xfrm_if *xi)
114{
115 struct xfrm_if __rcu **xip;
116 struct xfrm_if *iter;
117
118 for (xip = &xfrmn->xfrmi[0];
119 (iter = rtnl_dereference(*xip)) != NULL;
120 xip = &iter->next) {
121 if (xi == iter) {
122 rcu_assign_pointer(*xip, xi->next);
123 break;
124 }
125 }
126}
127
128static void xfrmi_dev_free(struct net_device *dev)
129{
Li RongQing4da40252018-09-30 15:06:06 +0800130 struct xfrm_if *xi = netdev_priv(dev);
131
132 gro_cells_destroy(&xi->gro_cells);
Steffen Klassertf203b762018-06-12 14:07:12 +0200133 free_percpu(dev->tstats);
134}
135
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200136static int xfrmi_create(struct net_device *dev)
Steffen Klassertf203b762018-06-12 14:07:12 +0200137{
138 struct xfrm_if *xi = netdev_priv(dev);
139 struct net *net = dev_net(dev);
140 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
141 int err;
142
143 dev->rtnl_link_ops = &xfrmi_link_ops;
144 err = register_netdevice(dev);
145 if (err < 0)
146 goto out;
147
Steffen Klassertf203b762018-06-12 14:07:12 +0200148 xfrmi_link(xfrmn, xi);
149
150 return 0;
151
152out:
153 return err;
154}
155
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200156static struct xfrm_if *xfrmi_locate(struct net *net, struct xfrm_if_parms *p)
Steffen Klassertf203b762018-06-12 14:07:12 +0200157{
158 struct xfrm_if __rcu **xip;
159 struct xfrm_if *xi;
160 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
161
162 for (xip = &xfrmn->xfrmi[0];
163 (xi = rtnl_dereference(*xip)) != NULL;
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200164 xip = &xi->next)
165 if (xi->p.if_id == p->if_id)
Steffen Klassertf203b762018-06-12 14:07:12 +0200166 return xi;
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200167
168 return NULL;
Steffen Klassertf203b762018-06-12 14:07:12 +0200169}
170
171static void xfrmi_dev_uninit(struct net_device *dev)
172{
173 struct xfrm_if *xi = netdev_priv(dev);
174 struct xfrmi_net *xfrmn = net_generic(xi->net, xfrmi_net_id);
175
176 xfrmi_unlink(xfrmn, xi);
Steffen Klassertf203b762018-06-12 14:07:12 +0200177}
178
179static void xfrmi_scrub_packet(struct sk_buff *skb, bool xnet)
180{
181 skb->tstamp = 0;
182 skb->pkt_type = PACKET_HOST;
183 skb->skb_iif = 0;
184 skb->ignore_df = 0;
185 skb_dst_drop(skb);
Florian Westphal895b5c92019-09-29 20:54:03 +0200186 nf_reset_ct(skb);
Steffen Klassertf203b762018-06-12 14:07:12 +0200187 nf_reset_trace(skb);
188
189 if (!xnet)
190 return;
191
192 ipvs_reset(skb);
193 secpath_reset(skb);
194 skb_orphan(skb);
195 skb->mark = 0;
196}
197
198static int xfrmi_rcv_cb(struct sk_buff *skb, int err)
199{
Florian Westphal4c145dc2019-03-29 21:16:31 +0100200 const struct xfrm_mode *inner_mode;
Steffen Klassertf203b762018-06-12 14:07:12 +0200201 struct pcpu_sw_netstats *tstats;
Steffen Klassertf203b762018-06-12 14:07:12 +0200202 struct net_device *dev;
203 struct xfrm_state *x;
204 struct xfrm_if *xi;
205 bool xnet;
206
Florian Westphal26912e32018-12-18 17:15:24 +0100207 if (err && !secpath_exists(skb))
Steffen Klassertf203b762018-06-12 14:07:12 +0200208 return 0;
209
210 x = xfrm_input_state(skb);
211
212 xi = xfrmi_lookup(xs_net(x), x);
213 if (!xi)
214 return 1;
215
216 dev = xi->dev;
217 skb->dev = dev;
218
219 if (err) {
220 dev->stats.rx_errors++;
221 dev->stats.rx_dropped++;
222
223 return 0;
224 }
225
226 xnet = !net_eq(xi->net, dev_net(skb->dev));
227
228 if (xnet) {
Florian Westphalc9500d72019-03-29 21:16:32 +0100229 inner_mode = &x->inner_mode;
Steffen Klassertf203b762018-06-12 14:07:12 +0200230
231 if (x->sel.family == AF_UNSPEC) {
232 inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
233 if (inner_mode == NULL) {
234 XFRM_INC_STATS(dev_net(skb->dev),
235 LINUX_MIB_XFRMINSTATEMODEERROR);
236 return -EINVAL;
237 }
238 }
239
240 if (!xfrm_policy_check(NULL, XFRM_POLICY_IN, skb,
Florian Westphalb45714b2019-03-29 21:16:23 +0100241 inner_mode->family))
Steffen Klassertf203b762018-06-12 14:07:12 +0200242 return -EPERM;
243 }
244
245 xfrmi_scrub_packet(skb, xnet);
246
247 tstats = this_cpu_ptr(dev->tstats);
248
249 u64_stats_update_begin(&tstats->syncp);
250 tstats->rx_packets++;
251 tstats->rx_bytes += skb->len;
252 u64_stats_update_end(&tstats->syncp);
253
254 return 0;
255}
256
257static int
258xfrmi_xmit2(struct sk_buff *skb, struct net_device *dev, struct flowi *fl)
259{
260 struct xfrm_if *xi = netdev_priv(dev);
261 struct net_device_stats *stats = &xi->dev->stats;
262 struct dst_entry *dst = skb_dst(skb);
263 unsigned int length = skb->len;
264 struct net_device *tdev;
265 struct xfrm_state *x;
266 int err = -1;
267 int mtu;
268
Steffen Klassertf203b762018-06-12 14:07:12 +0200269 dst_hold(dst);
Benedict Wongbc56b332018-07-19 10:50:44 -0700270 dst = xfrm_lookup_with_ifid(xi->net, dst, fl, NULL, 0, xi->p.if_id);
Steffen Klassertf203b762018-06-12 14:07:12 +0200271 if (IS_ERR(dst)) {
272 err = PTR_ERR(dst);
273 dst = NULL;
274 goto tx_err_link_failure;
275 }
276
277 x = dst->xfrm;
278 if (!x)
279 goto tx_err_link_failure;
280
281 if (x->if_id != xi->p.if_id)
282 goto tx_err_link_failure;
283
284 tdev = dst->dev;
285
286 if (tdev == dev) {
287 stats->collisions++;
288 net_warn_ratelimited("%s: Local routing loop detected!\n",
Nicolas Dichtele0aaa332019-07-15 12:00:21 +0200289 dev->name);
Steffen Klassertf203b762018-06-12 14:07:12 +0200290 goto tx_err_dst_release;
291 }
292
293 mtu = dst_mtu(dst);
294 if (!skb->ignore_df && skb->len > mtu) {
Xu Wang8aaea2b2020-01-13 09:00:36 +0000295 skb_dst_update_pmtu_no_confirm(skb, mtu);
Steffen Klassertf203b762018-06-12 14:07:12 +0200296
297 if (skb->protocol == htons(ETH_P_IPV6)) {
298 if (mtu < IPV6_MIN_MTU)
299 mtu = IPV6_MIN_MTU;
300
Jason A. Donenfeld45942ba2020-02-11 20:47:09 +0100301 icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Steffen Klassertf203b762018-06-12 14:07:12 +0200302 } else {
Jason A. Donenfeld45942ba2020-02-11 20:47:09 +0100303 icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
304 htonl(mtu));
Steffen Klassertf203b762018-06-12 14:07:12 +0200305 }
306
307 dst_release(dst);
308 return -EMSGSIZE;
309 }
310
311 xfrmi_scrub_packet(skb, !net_eq(xi->net, dev_net(dev)));
312 skb_dst_set(skb, dst);
313 skb->dev = tdev;
314
315 err = dst_output(xi->net, skb->sk, skb);
316 if (net_xmit_eval(err) == 0) {
317 struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
318
319 u64_stats_update_begin(&tstats->syncp);
320 tstats->tx_bytes += length;
321 tstats->tx_packets++;
322 u64_stats_update_end(&tstats->syncp);
323 } else {
324 stats->tx_errors++;
325 stats->tx_aborted_errors++;
326 }
327
328 return 0;
329tx_err_link_failure:
330 stats->tx_carrier_errors++;
331 dst_link_failure(skb);
332tx_err_dst_release:
333 dst_release(dst);
334 return err;
335}
336
337static netdev_tx_t xfrmi_xmit(struct sk_buff *skb, struct net_device *dev)
338{
339 struct xfrm_if *xi = netdev_priv(dev);
340 struct net_device_stats *stats = &xi->dev->stats;
Nicolas Dichtelf0423652020-01-13 09:32:47 +0100341 struct dst_entry *dst = skb_dst(skb);
Steffen Klassertf203b762018-06-12 14:07:12 +0200342 struct flowi fl;
343 int ret;
344
345 memset(&fl, 0, sizeof(fl));
346
347 switch (skb->protocol) {
348 case htons(ETH_P_IPV6):
349 xfrm_decode_session(skb, &fl, AF_INET6);
350 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
Nicolas Dichtelf0423652020-01-13 09:32:47 +0100351 if (!dst) {
352 fl.u.ip6.flowi6_oif = dev->ifindex;
353 fl.u.ip6.flowi6_flags |= FLOWI_FLAG_ANYSRC;
354 dst = ip6_route_output(dev_net(dev), NULL, &fl.u.ip6);
355 if (dst->error) {
356 dst_release(dst);
357 stats->tx_carrier_errors++;
358 goto tx_err;
359 }
360 skb_dst_set(skb, dst);
361 }
Steffen Klassertf203b762018-06-12 14:07:12 +0200362 break;
363 case htons(ETH_P_IP):
364 xfrm_decode_session(skb, &fl, AF_INET);
365 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
Nicolas Dichtelf0423652020-01-13 09:32:47 +0100366 if (!dst) {
367 struct rtable *rt;
368
369 fl.u.ip4.flowi4_oif = dev->ifindex;
370 fl.u.ip4.flowi4_flags |= FLOWI_FLAG_ANYSRC;
371 rt = __ip_route_output_key(dev_net(dev), &fl.u.ip4);
372 if (IS_ERR(rt)) {
373 stats->tx_carrier_errors++;
374 goto tx_err;
375 }
376 skb_dst_set(skb, &rt->dst);
377 }
Steffen Klassertf203b762018-06-12 14:07:12 +0200378 break;
379 default:
380 goto tx_err;
381 }
382
Nicolas Dichtel22d65522019-07-15 12:00:23 +0200383 fl.flowi_oif = xi->p.link;
Steffen Klassertf203b762018-06-12 14:07:12 +0200384
385 ret = xfrmi_xmit2(skb, dev, &fl);
386 if (ret < 0)
387 goto tx_err;
388
389 return NETDEV_TX_OK;
390
391tx_err:
392 stats->tx_errors++;
393 stats->tx_dropped++;
394 kfree_skb(skb);
395 return NETDEV_TX_OK;
396}
397
398static int xfrmi4_err(struct sk_buff *skb, u32 info)
399{
400 const struct iphdr *iph = (const struct iphdr *)skb->data;
401 struct net *net = dev_net(skb->dev);
402 int protocol = iph->protocol;
403 struct ip_comp_hdr *ipch;
404 struct ip_esp_hdr *esph;
405 struct ip_auth_hdr *ah ;
406 struct xfrm_state *x;
407 struct xfrm_if *xi;
408 __be32 spi;
409
410 switch (protocol) {
411 case IPPROTO_ESP:
412 esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
413 spi = esph->spi;
414 break;
415 case IPPROTO_AH:
416 ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2));
417 spi = ah->spi;
418 break;
419 case IPPROTO_COMP:
420 ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
421 spi = htonl(ntohs(ipch->cpi));
422 break;
423 default:
424 return 0;
425 }
426
427 switch (icmp_hdr(skb)->type) {
428 case ICMP_DEST_UNREACH:
429 if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
430 return 0;
431 case ICMP_REDIRECT:
432 break;
433 default:
434 return 0;
435 }
436
437 x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
438 spi, protocol, AF_INET);
439 if (!x)
440 return 0;
441
442 xi = xfrmi_lookup(net, x);
443 if (!xi) {
444 xfrm_state_put(x);
445 return -1;
446 }
447
448 if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
Maciej Żenczykowskid888f392018-09-25 20:56:26 -0700449 ipv4_update_pmtu(skb, net, info, 0, protocol);
Steffen Klassertf203b762018-06-12 14:07:12 +0200450 else
Maciej Żenczykowski1042caa2018-09-25 20:56:27 -0700451 ipv4_redirect(skb, net, 0, protocol);
Steffen Klassertf203b762018-06-12 14:07:12 +0200452 xfrm_state_put(x);
453
454 return 0;
455}
456
457static int xfrmi6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
458 u8 type, u8 code, int offset, __be32 info)
459{
460 const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
461 struct net *net = dev_net(skb->dev);
462 int protocol = iph->nexthdr;
463 struct ip_comp_hdr *ipch;
464 struct ip_esp_hdr *esph;
465 struct ip_auth_hdr *ah;
466 struct xfrm_state *x;
467 struct xfrm_if *xi;
468 __be32 spi;
469
470 switch (protocol) {
471 case IPPROTO_ESP:
472 esph = (struct ip_esp_hdr *)(skb->data + offset);
473 spi = esph->spi;
474 break;
475 case IPPROTO_AH:
476 ah = (struct ip_auth_hdr *)(skb->data + offset);
477 spi = ah->spi;
478 break;
479 case IPPROTO_COMP:
480 ipch = (struct ip_comp_hdr *)(skb->data + offset);
481 spi = htonl(ntohs(ipch->cpi));
482 break;
483 default:
484 return 0;
485 }
486
487 if (type != ICMPV6_PKT_TOOBIG &&
488 type != NDISC_REDIRECT)
489 return 0;
490
491 x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
492 spi, protocol, AF_INET6);
493 if (!x)
494 return 0;
495
496 xi = xfrmi_lookup(net, x);
497 if (!xi) {
498 xfrm_state_put(x);
499 return -1;
500 }
501
502 if (type == NDISC_REDIRECT)
503 ip6_redirect(skb, net, skb->dev->ifindex, 0,
504 sock_net_uid(net, NULL));
505 else
506 ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
507 xfrm_state_put(x);
508
509 return 0;
510}
511
512static int xfrmi_change(struct xfrm_if *xi, const struct xfrm_if_parms *p)
513{
514 if (xi->p.link != p->link)
515 return -EINVAL;
516
517 xi->p.if_id = p->if_id;
518
519 return 0;
520}
521
522static int xfrmi_update(struct xfrm_if *xi, struct xfrm_if_parms *p)
523{
Nicolas Dichtelc5d10302019-07-15 12:00:22 +0200524 struct net *net = xi->net;
Steffen Klassertf203b762018-06-12 14:07:12 +0200525 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
526 int err;
527
528 xfrmi_unlink(xfrmn, xi);
529 synchronize_net();
530 err = xfrmi_change(xi, p);
531 xfrmi_link(xfrmn, xi);
532 netdev_state_change(xi->dev);
533 return err;
534}
535
536static void xfrmi_get_stats64(struct net_device *dev,
537 struct rtnl_link_stats64 *s)
538{
539 int cpu;
540
Steffen Klassertf203b762018-06-12 14:07:12 +0200541 for_each_possible_cpu(cpu) {
542 struct pcpu_sw_netstats *stats;
543 struct pcpu_sw_netstats tmp;
544 int start;
545
546 stats = per_cpu_ptr(dev->tstats, cpu);
547 do {
548 start = u64_stats_fetch_begin_irq(&stats->syncp);
549 tmp.rx_packets = stats->rx_packets;
550 tmp.rx_bytes = stats->rx_bytes;
551 tmp.tx_packets = stats->tx_packets;
552 tmp.tx_bytes = stats->tx_bytes;
553 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
554
555 s->rx_packets += tmp.rx_packets;
556 s->rx_bytes += tmp.rx_bytes;
557 s->tx_packets += tmp.tx_packets;
558 s->tx_bytes += tmp.tx_bytes;
559 }
560
561 s->rx_dropped = dev->stats.rx_dropped;
562 s->tx_dropped = dev->stats.tx_dropped;
563}
564
565static int xfrmi_get_iflink(const struct net_device *dev)
566{
567 struct xfrm_if *xi = netdev_priv(dev);
568
Nicolas Dichtel22d65522019-07-15 12:00:23 +0200569 return xi->p.link;
Steffen Klassertf203b762018-06-12 14:07:12 +0200570}
571
572
573static const struct net_device_ops xfrmi_netdev_ops = {
574 .ndo_init = xfrmi_dev_init,
575 .ndo_uninit = xfrmi_dev_uninit,
576 .ndo_start_xmit = xfrmi_xmit,
577 .ndo_get_stats64 = xfrmi_get_stats64,
578 .ndo_get_iflink = xfrmi_get_iflink,
579};
580
581static void xfrmi_dev_setup(struct net_device *dev)
582{
583 dev->netdev_ops = &xfrmi_netdev_ops;
584 dev->type = ARPHRD_NONE;
Steffen Klassertf203b762018-06-12 14:07:12 +0200585 dev->mtu = ETH_DATA_LEN;
586 dev->min_mtu = ETH_MIN_MTU;
Nicolas Dichtelf0423652020-01-13 09:32:47 +0100587 dev->max_mtu = IP_MAX_MTU;
Steffen Klassertf203b762018-06-12 14:07:12 +0200588 dev->flags = IFF_NOARP;
589 dev->needs_free_netdev = true;
590 dev->priv_destructor = xfrmi_dev_free;
591 netif_keep_dst(dev);
Nicolas Dichtel22d65522019-07-15 12:00:23 +0200592
593 eth_broadcast_addr(dev->broadcast);
Steffen Klassertf203b762018-06-12 14:07:12 +0200594}
595
596static int xfrmi_dev_init(struct net_device *dev)
597{
598 struct xfrm_if *xi = netdev_priv(dev);
Nicolas Dichtel22d65522019-07-15 12:00:23 +0200599 struct net_device *phydev = __dev_get_by_index(xi->net, xi->p.link);
Steffen Klassertf203b762018-06-12 14:07:12 +0200600 int err;
601
602 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
603 if (!dev->tstats)
604 return -ENOMEM;
605
606 err = gro_cells_init(&xi->gro_cells, dev);
607 if (err) {
608 free_percpu(dev->tstats);
609 return err;
610 }
611
612 dev->features |= NETIF_F_LLTX;
613
Nicolas Dichtel22d65522019-07-15 12:00:23 +0200614 if (phydev) {
615 dev->needed_headroom = phydev->needed_headroom;
616 dev->needed_tailroom = phydev->needed_tailroom;
Steffen Klassertf203b762018-06-12 14:07:12 +0200617
Nicolas Dichtel22d65522019-07-15 12:00:23 +0200618 if (is_zero_ether_addr(dev->dev_addr))
619 eth_hw_addr_inherit(dev, phydev);
620 if (is_zero_ether_addr(dev->broadcast))
621 memcpy(dev->broadcast, phydev->broadcast,
622 dev->addr_len);
623 } else {
624 eth_hw_addr_random(dev);
625 eth_broadcast_addr(dev->broadcast);
626 }
Steffen Klassertf203b762018-06-12 14:07:12 +0200627
628 return 0;
629}
630
631static int xfrmi_validate(struct nlattr *tb[], struct nlattr *data[],
632 struct netlink_ext_ack *extack)
633{
634 return 0;
635}
636
637static void xfrmi_netlink_parms(struct nlattr *data[],
638 struct xfrm_if_parms *parms)
639{
640 memset(parms, 0, sizeof(*parms));
641
642 if (!data)
643 return;
644
645 if (data[IFLA_XFRM_LINK])
646 parms->link = nla_get_u32(data[IFLA_XFRM_LINK]);
647
648 if (data[IFLA_XFRM_IF_ID])
649 parms->if_id = nla_get_u32(data[IFLA_XFRM_IF_ID]);
650}
651
652static int xfrmi_newlink(struct net *src_net, struct net_device *dev,
653 struct nlattr *tb[], struct nlattr *data[],
654 struct netlink_ext_ack *extack)
655{
656 struct net *net = dev_net(dev);
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200657 struct xfrm_if_parms p;
Steffen Klassertf203b762018-06-12 14:07:12 +0200658 struct xfrm_if *xi;
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200659 int err;
Steffen Klassertf203b762018-06-12 14:07:12 +0200660
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200661 xfrmi_netlink_parms(data, &p);
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200662 xi = xfrmi_locate(net, &p);
663 if (xi)
664 return -EEXIST;
665
666 xi = netdev_priv(dev);
667 xi->p = p;
668 xi->net = net;
669 xi->dev = dev;
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200670
671 err = xfrmi_create(dev);
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200672 return err;
Steffen Klassertf203b762018-06-12 14:07:12 +0200673}
674
675static void xfrmi_dellink(struct net_device *dev, struct list_head *head)
676{
677 unregister_netdevice_queue(dev, head);
678}
679
680static int xfrmi_changelink(struct net_device *dev, struct nlattr *tb[],
681 struct nlattr *data[],
682 struct netlink_ext_ack *extack)
683{
Nicolas Dichtelc5d10302019-07-15 12:00:22 +0200684 struct xfrm_if *xi = netdev_priv(dev);
685 struct net *net = xi->net;
Nicolas Dichtele9e7e852019-07-15 12:00:20 +0200686 struct xfrm_if_parms p;
Steffen Klassertf203b762018-06-12 14:07:12 +0200687
Nicolas Dichtele9e7e852019-07-15 12:00:20 +0200688 xfrmi_netlink_parms(data, &p);
689 xi = xfrmi_locate(net, &p);
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200690 if (!xi) {
Benedict Wong44e2b832018-07-25 13:45:29 -0700691 xi = netdev_priv(dev);
692 } else {
Steffen Klassertf203b762018-06-12 14:07:12 +0200693 if (xi->dev != dev)
694 return -EEXIST;
Benedict Wong44e2b832018-07-25 13:45:29 -0700695 }
Steffen Klassertf203b762018-06-12 14:07:12 +0200696
Nicolas Dichtele9e7e852019-07-15 12:00:20 +0200697 return xfrmi_update(xi, &p);
Steffen Klassertf203b762018-06-12 14:07:12 +0200698}
699
700static size_t xfrmi_get_size(const struct net_device *dev)
701{
702 return
703 /* IFLA_XFRM_LINK */
704 nla_total_size(4) +
705 /* IFLA_XFRM_IF_ID */
706 nla_total_size(4) +
707 0;
708}
709
710static int xfrmi_fill_info(struct sk_buff *skb, const struct net_device *dev)
711{
712 struct xfrm_if *xi = netdev_priv(dev);
713 struct xfrm_if_parms *parm = &xi->p;
714
715 if (nla_put_u32(skb, IFLA_XFRM_LINK, parm->link) ||
716 nla_put_u32(skb, IFLA_XFRM_IF_ID, parm->if_id))
717 goto nla_put_failure;
718 return 0;
719
720nla_put_failure:
721 return -EMSGSIZE;
722}
723
Wei Yongjun211d6f22018-07-28 06:49:48 +0000724static struct net *xfrmi_get_link_net(const struct net_device *dev)
Steffen Klassertf203b762018-06-12 14:07:12 +0200725{
726 struct xfrm_if *xi = netdev_priv(dev);
727
Nicolas Dichtelc5d10302019-07-15 12:00:22 +0200728 return xi->net;
Steffen Klassertf203b762018-06-12 14:07:12 +0200729}
730
731static const struct nla_policy xfrmi_policy[IFLA_XFRM_MAX + 1] = {
732 [IFLA_XFRM_LINK] = { .type = NLA_U32 },
733 [IFLA_XFRM_IF_ID] = { .type = NLA_U32 },
734};
735
736static struct rtnl_link_ops xfrmi_link_ops __read_mostly = {
737 .kind = "xfrm",
738 .maxtype = IFLA_XFRM_MAX,
739 .policy = xfrmi_policy,
740 .priv_size = sizeof(struct xfrm_if),
741 .setup = xfrmi_dev_setup,
742 .validate = xfrmi_validate,
743 .newlink = xfrmi_newlink,
744 .dellink = xfrmi_dellink,
745 .changelink = xfrmi_changelink,
746 .get_size = xfrmi_get_size,
747 .fill_info = xfrmi_fill_info,
748 .get_link_net = xfrmi_get_link_net,
749};
750
Nicolas Dichtelc95c5f52020-04-23 00:06:45 +0200751static void __net_exit xfrmi_exit_batch_net(struct list_head *net_exit_list)
752{
753 struct net *net;
754 LIST_HEAD(list);
755
756 rtnl_lock();
757 list_for_each_entry(net, net_exit_list, exit_list) {
758 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
759 struct xfrm_if __rcu **xip;
760 struct xfrm_if *xi;
761
762 for (xip = &xfrmn->xfrmi[0];
763 (xi = rtnl_dereference(*xip)) != NULL;
764 xip = &xi->next)
765 unregister_netdevice_queue(xi->dev, &list);
766 }
767 unregister_netdevice_many(&list);
768 rtnl_unlock();
769}
770
Steffen Klassertf203b762018-06-12 14:07:12 +0200771static struct pernet_operations xfrmi_net_ops = {
Nicolas Dichtelc95c5f52020-04-23 00:06:45 +0200772 .exit_batch = xfrmi_exit_batch_net,
Steffen Klassertf203b762018-06-12 14:07:12 +0200773 .id = &xfrmi_net_id,
774 .size = sizeof(struct xfrmi_net),
775};
776
777static struct xfrm6_protocol xfrmi_esp6_protocol __read_mostly = {
778 .handler = xfrm6_rcv,
Sabrina Dubroca0146dca2020-04-27 17:59:34 +0200779 .input_handler = xfrm_input,
Steffen Klassertf203b762018-06-12 14:07:12 +0200780 .cb_handler = xfrmi_rcv_cb,
781 .err_handler = xfrmi6_err,
782 .priority = 10,
783};
784
785static struct xfrm6_protocol xfrmi_ah6_protocol __read_mostly = {
786 .handler = xfrm6_rcv,
Sabrina Dubroca0146dca2020-04-27 17:59:34 +0200787 .input_handler = xfrm_input,
Steffen Klassertf203b762018-06-12 14:07:12 +0200788 .cb_handler = xfrmi_rcv_cb,
789 .err_handler = xfrmi6_err,
790 .priority = 10,
791};
792
793static struct xfrm6_protocol xfrmi_ipcomp6_protocol __read_mostly = {
794 .handler = xfrm6_rcv,
Sabrina Dubroca0146dca2020-04-27 17:59:34 +0200795 .input_handler = xfrm_input,
Steffen Klassertf203b762018-06-12 14:07:12 +0200796 .cb_handler = xfrmi_rcv_cb,
797 .err_handler = xfrmi6_err,
798 .priority = 10,
799};
800
Xin Longd7b360c2020-07-06 20:01:37 +0800801#if IS_ENABLED(CONFIG_INET6_XFRM_TUNNEL)
802static int xfrmi6_rcv_tunnel(struct sk_buff *skb)
803{
804 const xfrm_address_t *saddr;
805 __be32 spi;
806
807 saddr = (const xfrm_address_t *)&ipv6_hdr(skb)->saddr;
808 spi = xfrm6_tunnel_spi_lookup(dev_net(skb->dev), saddr);
809
810 return xfrm6_rcv_spi(skb, IPPROTO_IPV6, spi, NULL);
811}
812
813static struct xfrm6_tunnel xfrmi_ipv6_handler __read_mostly = {
814 .handler = xfrmi6_rcv_tunnel,
815 .cb_handler = xfrmi_rcv_cb,
816 .err_handler = xfrmi6_err,
817 .priority = -1,
818};
819#endif
820
Steffen Klassertf203b762018-06-12 14:07:12 +0200821static struct xfrm4_protocol xfrmi_esp4_protocol __read_mostly = {
822 .handler = xfrm4_rcv,
823 .input_handler = xfrm_input,
824 .cb_handler = xfrmi_rcv_cb,
825 .err_handler = xfrmi4_err,
826 .priority = 10,
827};
828
829static struct xfrm4_protocol xfrmi_ah4_protocol __read_mostly = {
830 .handler = xfrm4_rcv,
831 .input_handler = xfrm_input,
832 .cb_handler = xfrmi_rcv_cb,
833 .err_handler = xfrmi4_err,
834 .priority = 10,
835};
836
837static struct xfrm4_protocol xfrmi_ipcomp4_protocol __read_mostly = {
838 .handler = xfrm4_rcv,
839 .input_handler = xfrm_input,
840 .cb_handler = xfrmi_rcv_cb,
841 .err_handler = xfrmi4_err,
842 .priority = 10,
843};
844
845static int __init xfrmi4_init(void)
846{
847 int err;
848
849 err = xfrm4_protocol_register(&xfrmi_esp4_protocol, IPPROTO_ESP);
850 if (err < 0)
851 goto xfrm_proto_esp_failed;
852 err = xfrm4_protocol_register(&xfrmi_ah4_protocol, IPPROTO_AH);
853 if (err < 0)
854 goto xfrm_proto_ah_failed;
855 err = xfrm4_protocol_register(&xfrmi_ipcomp4_protocol, IPPROTO_COMP);
856 if (err < 0)
857 goto xfrm_proto_comp_failed;
858
859 return 0;
860
861xfrm_proto_comp_failed:
862 xfrm4_protocol_deregister(&xfrmi_ah4_protocol, IPPROTO_AH);
863xfrm_proto_ah_failed:
864 xfrm4_protocol_deregister(&xfrmi_esp4_protocol, IPPROTO_ESP);
865xfrm_proto_esp_failed:
866 return err;
867}
868
869static void xfrmi4_fini(void)
870{
871 xfrm4_protocol_deregister(&xfrmi_ipcomp4_protocol, IPPROTO_COMP);
872 xfrm4_protocol_deregister(&xfrmi_ah4_protocol, IPPROTO_AH);
873 xfrm4_protocol_deregister(&xfrmi_esp4_protocol, IPPROTO_ESP);
874}
875
876static int __init xfrmi6_init(void)
877{
878 int err;
879
880 err = xfrm6_protocol_register(&xfrmi_esp6_protocol, IPPROTO_ESP);
881 if (err < 0)
882 goto xfrm_proto_esp_failed;
883 err = xfrm6_protocol_register(&xfrmi_ah6_protocol, IPPROTO_AH);
884 if (err < 0)
885 goto xfrm_proto_ah_failed;
886 err = xfrm6_protocol_register(&xfrmi_ipcomp6_protocol, IPPROTO_COMP);
887 if (err < 0)
888 goto xfrm_proto_comp_failed;
Xin Longd7b360c2020-07-06 20:01:37 +0800889#if IS_ENABLED(CONFIG_INET6_XFRM_TUNNEL)
890 err = xfrm6_tunnel_register(&xfrmi_ipv6_handler, AF_INET6);
891 if (err < 0)
892 goto xfrm_tunnel_ipv6_failed;
893 err = xfrm6_tunnel_register(&xfrmi_ipv6_handler, AF_INET);
894 if (err < 0)
895 goto xfrm_tunnel_ip6ip_failed;
896#endif
Steffen Klassertf203b762018-06-12 14:07:12 +0200897
898 return 0;
899
Xin Longd7b360c2020-07-06 20:01:37 +0800900#if IS_ENABLED(CONFIG_INET6_XFRM_TUNNEL)
901xfrm_tunnel_ip6ip_failed:
902 xfrm6_tunnel_deregister(&xfrmi_ipv6_handler, AF_INET6);
903xfrm_tunnel_ipv6_failed:
904 xfrm6_protocol_deregister(&xfrmi_ipcomp6_protocol, IPPROTO_COMP);
905#endif
Steffen Klassertf203b762018-06-12 14:07:12 +0200906xfrm_proto_comp_failed:
907 xfrm6_protocol_deregister(&xfrmi_ah6_protocol, IPPROTO_AH);
908xfrm_proto_ah_failed:
909 xfrm6_protocol_deregister(&xfrmi_esp6_protocol, IPPROTO_ESP);
910xfrm_proto_esp_failed:
911 return err;
912}
913
914static void xfrmi6_fini(void)
915{
Xin Longd7b360c2020-07-06 20:01:37 +0800916#if IS_ENABLED(CONFIG_INET6_XFRM_TUNNEL)
917 xfrm6_tunnel_deregister(&xfrmi_ipv6_handler, AF_INET);
918 xfrm6_tunnel_deregister(&xfrmi_ipv6_handler, AF_INET6);
919#endif
Steffen Klassertf203b762018-06-12 14:07:12 +0200920 xfrm6_protocol_deregister(&xfrmi_ipcomp6_protocol, IPPROTO_COMP);
921 xfrm6_protocol_deregister(&xfrmi_ah6_protocol, IPPROTO_AH);
922 xfrm6_protocol_deregister(&xfrmi_esp6_protocol, IPPROTO_ESP);
923}
924
925static const struct xfrm_if_cb xfrm_if_cb = {
926 .decode_session = xfrmi_decode_session,
927};
928
929static int __init xfrmi_init(void)
930{
931 const char *msg;
932 int err;
933
934 pr_info("IPsec XFRM device driver\n");
935
936 msg = "tunnel device";
937 err = register_pernet_device(&xfrmi_net_ops);
938 if (err < 0)
939 goto pernet_dev_failed;
940
941 msg = "xfrm4 protocols";
942 err = xfrmi4_init();
943 if (err < 0)
944 goto xfrmi4_failed;
945
946 msg = "xfrm6 protocols";
947 err = xfrmi6_init();
948 if (err < 0)
949 goto xfrmi6_failed;
950
951
952 msg = "netlink interface";
953 err = rtnl_link_register(&xfrmi_link_ops);
954 if (err < 0)
955 goto rtnl_link_failed;
956
957 xfrm_if_register_cb(&xfrm_if_cb);
958
959 return err;
960
961rtnl_link_failed:
962 xfrmi6_fini();
963xfrmi6_failed:
964 xfrmi4_fini();
965xfrmi4_failed:
966 unregister_pernet_device(&xfrmi_net_ops);
967pernet_dev_failed:
968 pr_err("xfrmi init: failed to register %s\n", msg);
969 return err;
970}
971
972static void __exit xfrmi_fini(void)
973{
974 xfrm_if_unregister_cb();
975 rtnl_link_unregister(&xfrmi_link_ops);
976 xfrmi4_fini();
977 xfrmi6_fini();
978 unregister_pernet_device(&xfrmi_net_ops);
979}
980
981module_init(xfrmi_init);
982module_exit(xfrmi_fini);
983MODULE_LICENSE("GPL");
984MODULE_ALIAS_RTNL_LINK("xfrm");
985MODULE_ALIAS_NETDEV("xfrm0");
986MODULE_AUTHOR("Steffen Klassert");
987MODULE_DESCRIPTION("XFRM virtual interface");