blob: aa4cdcf69d471e63ec712810edaa78008572a9e9 [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. Donenfeld8f9a1fa2020-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);
Sabrina Dubroca45a36a12020-08-04 11:37:29 +0200299 if (skb->len > mtu) {
Xu Wang8aaea2b2020-01-13 09:00:36 +0000300 skb_dst_update_pmtu_no_confirm(skb, mtu);
Steffen Klassertf203b762018-06-12 14:07:12 +0200301
302 if (skb->protocol == htons(ETH_P_IPV6)) {
303 if (mtu < IPV6_MIN_MTU)
304 mtu = IPV6_MIN_MTU;
305
Jason A. Donenfeld45942ba2020-02-11 20:47:09 +0100306 icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Steffen Klassertf203b762018-06-12 14:07:12 +0200307 } else {
Jason A. Donenfeld45942ba2020-02-11 20:47:09 +0100308 icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
309 htonl(mtu));
Steffen Klassertf203b762018-06-12 14:07:12 +0200310 }
311
312 dst_release(dst);
313 return -EMSGSIZE;
314 }
315
316 xfrmi_scrub_packet(skb, !net_eq(xi->net, dev_net(dev)));
317 skb_dst_set(skb, dst);
318 skb->dev = tdev;
319
320 err = dst_output(xi->net, skb->sk, skb);
321 if (net_xmit_eval(err) == 0) {
322 struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
323
324 u64_stats_update_begin(&tstats->syncp);
325 tstats->tx_bytes += length;
326 tstats->tx_packets++;
327 u64_stats_update_end(&tstats->syncp);
328 } else {
329 stats->tx_errors++;
330 stats->tx_aborted_errors++;
331 }
332
333 return 0;
334tx_err_link_failure:
335 stats->tx_carrier_errors++;
336 dst_link_failure(skb);
337tx_err_dst_release:
338 dst_release(dst);
339 return err;
340}
341
342static netdev_tx_t xfrmi_xmit(struct sk_buff *skb, struct net_device *dev)
343{
344 struct xfrm_if *xi = netdev_priv(dev);
345 struct net_device_stats *stats = &xi->dev->stats;
Nicolas Dichtelf0423652020-01-13 09:32:47 +0100346 struct dst_entry *dst = skb_dst(skb);
Steffen Klassertf203b762018-06-12 14:07:12 +0200347 struct flowi fl;
348 int ret;
349
350 memset(&fl, 0, sizeof(fl));
351
352 switch (skb->protocol) {
353 case htons(ETH_P_IPV6):
354 xfrm_decode_session(skb, &fl, AF_INET6);
355 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
Nicolas Dichtelf0423652020-01-13 09:32:47 +0100356 if (!dst) {
357 fl.u.ip6.flowi6_oif = dev->ifindex;
358 fl.u.ip6.flowi6_flags |= FLOWI_FLAG_ANYSRC;
359 dst = ip6_route_output(dev_net(dev), NULL, &fl.u.ip6);
360 if (dst->error) {
361 dst_release(dst);
362 stats->tx_carrier_errors++;
363 goto tx_err;
364 }
365 skb_dst_set(skb, dst);
366 }
Steffen Klassertf203b762018-06-12 14:07:12 +0200367 break;
368 case htons(ETH_P_IP):
369 xfrm_decode_session(skb, &fl, AF_INET);
370 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
Nicolas Dichtelf0423652020-01-13 09:32:47 +0100371 if (!dst) {
372 struct rtable *rt;
373
374 fl.u.ip4.flowi4_oif = dev->ifindex;
375 fl.u.ip4.flowi4_flags |= FLOWI_FLAG_ANYSRC;
376 rt = __ip_route_output_key(dev_net(dev), &fl.u.ip4);
377 if (IS_ERR(rt)) {
378 stats->tx_carrier_errors++;
379 goto tx_err;
380 }
381 skb_dst_set(skb, &rt->dst);
382 }
Steffen Klassertf203b762018-06-12 14:07:12 +0200383 break;
384 default:
385 goto tx_err;
386 }
387
Nicolas Dichtel22d65522019-07-15 12:00:23 +0200388 fl.flowi_oif = xi->p.link;
Steffen Klassertf203b762018-06-12 14:07:12 +0200389
390 ret = xfrmi_xmit2(skb, dev, &fl);
391 if (ret < 0)
392 goto tx_err;
393
394 return NETDEV_TX_OK;
395
396tx_err:
397 stats->tx_errors++;
398 stats->tx_dropped++;
399 kfree_skb(skb);
400 return NETDEV_TX_OK;
401}
402
403static int xfrmi4_err(struct sk_buff *skb, u32 info)
404{
405 const struct iphdr *iph = (const struct iphdr *)skb->data;
406 struct net *net = dev_net(skb->dev);
407 int protocol = iph->protocol;
408 struct ip_comp_hdr *ipch;
409 struct ip_esp_hdr *esph;
410 struct ip_auth_hdr *ah ;
411 struct xfrm_state *x;
412 struct xfrm_if *xi;
413 __be32 spi;
414
415 switch (protocol) {
416 case IPPROTO_ESP:
417 esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
418 spi = esph->spi;
419 break;
420 case IPPROTO_AH:
421 ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2));
422 spi = ah->spi;
423 break;
424 case IPPROTO_COMP:
425 ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
426 spi = htonl(ntohs(ipch->cpi));
427 break;
428 default:
429 return 0;
430 }
431
432 switch (icmp_hdr(skb)->type) {
433 case ICMP_DEST_UNREACH:
434 if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
435 return 0;
436 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
541static void xfrmi_get_stats64(struct net_device *dev,
542 struct rtnl_link_stats64 *s)
543{
Heiner Kallweit5fc35942020-10-12 10:19:59 +0200544 dev_fetch_sw_netstats(s, dev->tstats);
Steffen Klassertf203b762018-06-12 14:07:12 +0200545
546 s->rx_dropped = dev->stats.rx_dropped;
547 s->tx_dropped = dev->stats.tx_dropped;
548}
549
550static int xfrmi_get_iflink(const struct net_device *dev)
551{
552 struct xfrm_if *xi = netdev_priv(dev);
553
Nicolas Dichtel22d65522019-07-15 12:00:23 +0200554 return xi->p.link;
Steffen Klassertf203b762018-06-12 14:07:12 +0200555}
556
557
558static const struct net_device_ops xfrmi_netdev_ops = {
559 .ndo_init = xfrmi_dev_init,
560 .ndo_uninit = xfrmi_dev_uninit,
561 .ndo_start_xmit = xfrmi_xmit,
562 .ndo_get_stats64 = xfrmi_get_stats64,
563 .ndo_get_iflink = xfrmi_get_iflink,
564};
565
566static void xfrmi_dev_setup(struct net_device *dev)
567{
568 dev->netdev_ops = &xfrmi_netdev_ops;
Jason A. Donenfeld8f9a1fa2020-06-29 19:06:25 -0600569 dev->header_ops = &ip_tunnel_header_ops;
Steffen Klassertf203b762018-06-12 14:07:12 +0200570 dev->type = ARPHRD_NONE;
Steffen Klassertf203b762018-06-12 14:07:12 +0200571 dev->mtu = ETH_DATA_LEN;
572 dev->min_mtu = ETH_MIN_MTU;
Nicolas Dichtelf0423652020-01-13 09:32:47 +0100573 dev->max_mtu = IP_MAX_MTU;
Steffen Klassertf203b762018-06-12 14:07:12 +0200574 dev->flags = IFF_NOARP;
575 dev->needs_free_netdev = true;
576 dev->priv_destructor = xfrmi_dev_free;
577 netif_keep_dst(dev);
Nicolas Dichtel22d65522019-07-15 12:00:23 +0200578
579 eth_broadcast_addr(dev->broadcast);
Steffen Klassertf203b762018-06-12 14:07:12 +0200580}
581
582static int xfrmi_dev_init(struct net_device *dev)
583{
584 struct xfrm_if *xi = netdev_priv(dev);
Nicolas Dichtel22d65522019-07-15 12:00:23 +0200585 struct net_device *phydev = __dev_get_by_index(xi->net, xi->p.link);
Steffen Klassertf203b762018-06-12 14:07:12 +0200586 int err;
587
588 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
589 if (!dev->tstats)
590 return -ENOMEM;
591
592 err = gro_cells_init(&xi->gro_cells, dev);
593 if (err) {
594 free_percpu(dev->tstats);
595 return err;
596 }
597
598 dev->features |= NETIF_F_LLTX;
599
Nicolas Dichtel22d65522019-07-15 12:00:23 +0200600 if (phydev) {
601 dev->needed_headroom = phydev->needed_headroom;
602 dev->needed_tailroom = phydev->needed_tailroom;
Steffen Klassertf203b762018-06-12 14:07:12 +0200603
Nicolas Dichtel22d65522019-07-15 12:00:23 +0200604 if (is_zero_ether_addr(dev->dev_addr))
605 eth_hw_addr_inherit(dev, phydev);
606 if (is_zero_ether_addr(dev->broadcast))
607 memcpy(dev->broadcast, phydev->broadcast,
608 dev->addr_len);
609 } else {
610 eth_hw_addr_random(dev);
611 eth_broadcast_addr(dev->broadcast);
612 }
Steffen Klassertf203b762018-06-12 14:07:12 +0200613
614 return 0;
615}
616
617static int xfrmi_validate(struct nlattr *tb[], struct nlattr *data[],
618 struct netlink_ext_ack *extack)
619{
620 return 0;
621}
622
623static void xfrmi_netlink_parms(struct nlattr *data[],
624 struct xfrm_if_parms *parms)
625{
626 memset(parms, 0, sizeof(*parms));
627
628 if (!data)
629 return;
630
631 if (data[IFLA_XFRM_LINK])
632 parms->link = nla_get_u32(data[IFLA_XFRM_LINK]);
633
634 if (data[IFLA_XFRM_IF_ID])
635 parms->if_id = nla_get_u32(data[IFLA_XFRM_IF_ID]);
636}
637
638static int xfrmi_newlink(struct net *src_net, struct net_device *dev,
639 struct nlattr *tb[], struct nlattr *data[],
640 struct netlink_ext_ack *extack)
641{
642 struct net *net = dev_net(dev);
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200643 struct xfrm_if_parms p;
Steffen Klassertf203b762018-06-12 14:07:12 +0200644 struct xfrm_if *xi;
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200645 int err;
Steffen Klassertf203b762018-06-12 14:07:12 +0200646
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200647 xfrmi_netlink_parms(data, &p);
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200648 xi = xfrmi_locate(net, &p);
649 if (xi)
650 return -EEXIST;
651
652 xi = netdev_priv(dev);
653 xi->p = p;
654 xi->net = net;
655 xi->dev = dev;
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200656
657 err = xfrmi_create(dev);
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200658 return err;
Steffen Klassertf203b762018-06-12 14:07:12 +0200659}
660
661static void xfrmi_dellink(struct net_device *dev, struct list_head *head)
662{
663 unregister_netdevice_queue(dev, head);
664}
665
666static int xfrmi_changelink(struct net_device *dev, struct nlattr *tb[],
667 struct nlattr *data[],
668 struct netlink_ext_ack *extack)
669{
Nicolas Dichtelc5d10302019-07-15 12:00:22 +0200670 struct xfrm_if *xi = netdev_priv(dev);
671 struct net *net = xi->net;
Nicolas Dichtele9e7e852019-07-15 12:00:20 +0200672 struct xfrm_if_parms p;
Steffen Klassertf203b762018-06-12 14:07:12 +0200673
Nicolas Dichtele9e7e852019-07-15 12:00:20 +0200674 xfrmi_netlink_parms(data, &p);
675 xi = xfrmi_locate(net, &p);
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200676 if (!xi) {
Benedict Wong44e2b832018-07-25 13:45:29 -0700677 xi = netdev_priv(dev);
678 } else {
Steffen Klassertf203b762018-06-12 14:07:12 +0200679 if (xi->dev != dev)
680 return -EEXIST;
Benedict Wong44e2b832018-07-25 13:45:29 -0700681 }
Steffen Klassertf203b762018-06-12 14:07:12 +0200682
Nicolas Dichtele9e7e852019-07-15 12:00:20 +0200683 return xfrmi_update(xi, &p);
Steffen Klassertf203b762018-06-12 14:07:12 +0200684}
685
686static size_t xfrmi_get_size(const struct net_device *dev)
687{
688 return
689 /* IFLA_XFRM_LINK */
690 nla_total_size(4) +
691 /* IFLA_XFRM_IF_ID */
692 nla_total_size(4) +
693 0;
694}
695
696static int xfrmi_fill_info(struct sk_buff *skb, const struct net_device *dev)
697{
698 struct xfrm_if *xi = netdev_priv(dev);
699 struct xfrm_if_parms *parm = &xi->p;
700
701 if (nla_put_u32(skb, IFLA_XFRM_LINK, parm->link) ||
702 nla_put_u32(skb, IFLA_XFRM_IF_ID, parm->if_id))
703 goto nla_put_failure;
704 return 0;
705
706nla_put_failure:
707 return -EMSGSIZE;
708}
709
Wei Yongjun211d6f22018-07-28 06:49:48 +0000710static struct net *xfrmi_get_link_net(const struct net_device *dev)
Steffen Klassertf203b762018-06-12 14:07:12 +0200711{
712 struct xfrm_if *xi = netdev_priv(dev);
713
Nicolas Dichtelc5d10302019-07-15 12:00:22 +0200714 return xi->net;
Steffen Klassertf203b762018-06-12 14:07:12 +0200715}
716
717static const struct nla_policy xfrmi_policy[IFLA_XFRM_MAX + 1] = {
718 [IFLA_XFRM_LINK] = { .type = NLA_U32 },
719 [IFLA_XFRM_IF_ID] = { .type = NLA_U32 },
720};
721
722static struct rtnl_link_ops xfrmi_link_ops __read_mostly = {
723 .kind = "xfrm",
724 .maxtype = IFLA_XFRM_MAX,
725 .policy = xfrmi_policy,
726 .priv_size = sizeof(struct xfrm_if),
727 .setup = xfrmi_dev_setup,
728 .validate = xfrmi_validate,
729 .newlink = xfrmi_newlink,
730 .dellink = xfrmi_dellink,
731 .changelink = xfrmi_changelink,
732 .get_size = xfrmi_get_size,
733 .fill_info = xfrmi_fill_info,
734 .get_link_net = xfrmi_get_link_net,
735};
736
Nicolas Dichtelc95c5f52020-04-23 00:06:45 +0200737static void __net_exit xfrmi_exit_batch_net(struct list_head *net_exit_list)
738{
739 struct net *net;
740 LIST_HEAD(list);
741
742 rtnl_lock();
743 list_for_each_entry(net, net_exit_list, exit_list) {
744 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
745 struct xfrm_if __rcu **xip;
746 struct xfrm_if *xi;
Eyal Birgere98e4452020-07-09 13:16:52 +0300747 int i;
Nicolas Dichtelc95c5f52020-04-23 00:06:45 +0200748
Eyal Birgere98e4452020-07-09 13:16:52 +0300749 for (i = 0; i < XFRMI_HASH_SIZE; i++) {
750 for (xip = &xfrmn->xfrmi[i];
751 (xi = rtnl_dereference(*xip)) != NULL;
752 xip = &xi->next)
753 unregister_netdevice_queue(xi->dev, &list);
754 }
Nicolas Dichtelc95c5f52020-04-23 00:06:45 +0200755 }
756 unregister_netdevice_many(&list);
757 rtnl_unlock();
758}
759
Steffen Klassertf203b762018-06-12 14:07:12 +0200760static struct pernet_operations xfrmi_net_ops = {
Nicolas Dichtelc95c5f52020-04-23 00:06:45 +0200761 .exit_batch = xfrmi_exit_batch_net,
Steffen Klassertf203b762018-06-12 14:07:12 +0200762 .id = &xfrmi_net_id,
763 .size = sizeof(struct xfrmi_net),
764};
765
766static struct xfrm6_protocol xfrmi_esp6_protocol __read_mostly = {
767 .handler = xfrm6_rcv,
Sabrina Dubroca0146dca2020-04-27 17:59:34 +0200768 .input_handler = xfrm_input,
Steffen Klassertf203b762018-06-12 14:07:12 +0200769 .cb_handler = xfrmi_rcv_cb,
770 .err_handler = xfrmi6_err,
771 .priority = 10,
772};
773
774static struct xfrm6_protocol xfrmi_ah6_protocol __read_mostly = {
775 .handler = xfrm6_rcv,
Sabrina Dubroca0146dca2020-04-27 17:59:34 +0200776 .input_handler = xfrm_input,
Steffen Klassertf203b762018-06-12 14:07:12 +0200777 .cb_handler = xfrmi_rcv_cb,
778 .err_handler = xfrmi6_err,
779 .priority = 10,
780};
781
782static struct xfrm6_protocol xfrmi_ipcomp6_protocol __read_mostly = {
783 .handler = xfrm6_rcv,
Sabrina Dubroca0146dca2020-04-27 17:59:34 +0200784 .input_handler = xfrm_input,
Steffen Klassertf203b762018-06-12 14:07:12 +0200785 .cb_handler = xfrmi_rcv_cb,
786 .err_handler = xfrmi6_err,
787 .priority = 10,
788};
789
Xin Long0a0d93b2020-07-17 15:02:30 +0800790#if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
Xin Longd7b360c2020-07-06 20:01:37 +0800791static int xfrmi6_rcv_tunnel(struct sk_buff *skb)
792{
793 const xfrm_address_t *saddr;
794 __be32 spi;
795
796 saddr = (const xfrm_address_t *)&ipv6_hdr(skb)->saddr;
797 spi = xfrm6_tunnel_spi_lookup(dev_net(skb->dev), saddr);
798
799 return xfrm6_rcv_spi(skb, IPPROTO_IPV6, spi, NULL);
800}
801
802static struct xfrm6_tunnel xfrmi_ipv6_handler __read_mostly = {
803 .handler = xfrmi6_rcv_tunnel,
804 .cb_handler = xfrmi_rcv_cb,
805 .err_handler = xfrmi6_err,
806 .priority = -1,
807};
Xin Long8b404f42020-07-13 15:42:38 +0800808
809static struct xfrm6_tunnel xfrmi_ip6ip_handler __read_mostly = {
810 .handler = xfrmi6_rcv_tunnel,
811 .cb_handler = xfrmi_rcv_cb,
812 .err_handler = xfrmi6_err,
813 .priority = -1,
814};
Xin Longd7b360c2020-07-06 20:01:37 +0800815#endif
816
Steffen Klassertf203b762018-06-12 14:07:12 +0200817static struct xfrm4_protocol xfrmi_esp4_protocol __read_mostly = {
818 .handler = xfrm4_rcv,
819 .input_handler = xfrm_input,
820 .cb_handler = xfrmi_rcv_cb,
821 .err_handler = xfrmi4_err,
822 .priority = 10,
823};
824
825static struct xfrm4_protocol xfrmi_ah4_protocol __read_mostly = {
826 .handler = xfrm4_rcv,
827 .input_handler = xfrm_input,
828 .cb_handler = xfrmi_rcv_cb,
829 .err_handler = xfrmi4_err,
830 .priority = 10,
831};
832
833static struct xfrm4_protocol xfrmi_ipcomp4_protocol __read_mostly = {
834 .handler = xfrm4_rcv,
835 .input_handler = xfrm_input,
836 .cb_handler = xfrmi_rcv_cb,
837 .err_handler = xfrmi4_err,
838 .priority = 10,
839};
840
Xin Long0a0d93b2020-07-17 15:02:30 +0800841#if IS_REACHABLE(CONFIG_INET_XFRM_TUNNEL)
Xin Longda9bbf02020-07-06 20:01:38 +0800842static int xfrmi4_rcv_tunnel(struct sk_buff *skb)
843{
844 return xfrm4_rcv_spi(skb, IPPROTO_IPIP, ip_hdr(skb)->saddr);
845}
846
847static struct xfrm_tunnel xfrmi_ipip_handler __read_mostly = {
848 .handler = xfrmi4_rcv_tunnel,
849 .cb_handler = xfrmi_rcv_cb,
850 .err_handler = xfrmi4_err,
851 .priority = -1,
852};
Xin Long8b404f42020-07-13 15:42:38 +0800853
854static struct xfrm_tunnel xfrmi_ipip6_handler __read_mostly = {
855 .handler = xfrmi4_rcv_tunnel,
856 .cb_handler = xfrmi_rcv_cb,
857 .err_handler = xfrmi4_err,
858 .priority = -1,
859};
Xin Longda9bbf02020-07-06 20:01:38 +0800860#endif
861
Steffen Klassertf203b762018-06-12 14:07:12 +0200862static int __init xfrmi4_init(void)
863{
864 int err;
865
866 err = xfrm4_protocol_register(&xfrmi_esp4_protocol, IPPROTO_ESP);
867 if (err < 0)
868 goto xfrm_proto_esp_failed;
869 err = xfrm4_protocol_register(&xfrmi_ah4_protocol, IPPROTO_AH);
870 if (err < 0)
871 goto xfrm_proto_ah_failed;
872 err = xfrm4_protocol_register(&xfrmi_ipcomp4_protocol, IPPROTO_COMP);
873 if (err < 0)
874 goto xfrm_proto_comp_failed;
Xin Long0a0d93b2020-07-17 15:02:30 +0800875#if IS_REACHABLE(CONFIG_INET_XFRM_TUNNEL)
Xin Longda9bbf02020-07-06 20:01:38 +0800876 err = xfrm4_tunnel_register(&xfrmi_ipip_handler, AF_INET);
877 if (err < 0)
878 goto xfrm_tunnel_ipip_failed;
Xin Long8b404f42020-07-13 15:42:38 +0800879 err = xfrm4_tunnel_register(&xfrmi_ipip6_handler, AF_INET6);
Xin Longda9bbf02020-07-06 20:01:38 +0800880 if (err < 0)
881 goto xfrm_tunnel_ipip6_failed;
882#endif
Steffen Klassertf203b762018-06-12 14:07:12 +0200883
884 return 0;
885
Xin Long0a0d93b2020-07-17 15:02:30 +0800886#if IS_REACHABLE(CONFIG_INET_XFRM_TUNNEL)
Xin Longda9bbf02020-07-06 20:01:38 +0800887xfrm_tunnel_ipip6_failed:
888 xfrm4_tunnel_deregister(&xfrmi_ipip_handler, AF_INET);
889xfrm_tunnel_ipip_failed:
890 xfrm4_protocol_deregister(&xfrmi_ipcomp4_protocol, IPPROTO_COMP);
891#endif
Steffen Klassertf203b762018-06-12 14:07:12 +0200892xfrm_proto_comp_failed:
893 xfrm4_protocol_deregister(&xfrmi_ah4_protocol, IPPROTO_AH);
894xfrm_proto_ah_failed:
895 xfrm4_protocol_deregister(&xfrmi_esp4_protocol, IPPROTO_ESP);
896xfrm_proto_esp_failed:
897 return err;
898}
899
900static void xfrmi4_fini(void)
901{
Xin Long0a0d93b2020-07-17 15:02:30 +0800902#if IS_REACHABLE(CONFIG_INET_XFRM_TUNNEL)
Xin Long8b404f42020-07-13 15:42:38 +0800903 xfrm4_tunnel_deregister(&xfrmi_ipip6_handler, AF_INET6);
Xin Longda9bbf02020-07-06 20:01:38 +0800904 xfrm4_tunnel_deregister(&xfrmi_ipip_handler, AF_INET);
905#endif
Steffen Klassertf203b762018-06-12 14:07:12 +0200906 xfrm4_protocol_deregister(&xfrmi_ipcomp4_protocol, IPPROTO_COMP);
907 xfrm4_protocol_deregister(&xfrmi_ah4_protocol, IPPROTO_AH);
908 xfrm4_protocol_deregister(&xfrmi_esp4_protocol, IPPROTO_ESP);
909}
910
911static int __init xfrmi6_init(void)
912{
913 int err;
914
915 err = xfrm6_protocol_register(&xfrmi_esp6_protocol, IPPROTO_ESP);
916 if (err < 0)
917 goto xfrm_proto_esp_failed;
918 err = xfrm6_protocol_register(&xfrmi_ah6_protocol, IPPROTO_AH);
919 if (err < 0)
920 goto xfrm_proto_ah_failed;
921 err = xfrm6_protocol_register(&xfrmi_ipcomp6_protocol, IPPROTO_COMP);
922 if (err < 0)
923 goto xfrm_proto_comp_failed;
Xin Long0a0d93b2020-07-17 15:02:30 +0800924#if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
Xin Longd7b360c2020-07-06 20:01:37 +0800925 err = xfrm6_tunnel_register(&xfrmi_ipv6_handler, AF_INET6);
926 if (err < 0)
927 goto xfrm_tunnel_ipv6_failed;
Xin Long8b404f42020-07-13 15:42:38 +0800928 err = xfrm6_tunnel_register(&xfrmi_ip6ip_handler, AF_INET);
Xin Longd7b360c2020-07-06 20:01:37 +0800929 if (err < 0)
930 goto xfrm_tunnel_ip6ip_failed;
931#endif
Steffen Klassertf203b762018-06-12 14:07:12 +0200932
933 return 0;
934
Xin Long0a0d93b2020-07-17 15:02:30 +0800935#if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
Xin Longd7b360c2020-07-06 20:01:37 +0800936xfrm_tunnel_ip6ip_failed:
937 xfrm6_tunnel_deregister(&xfrmi_ipv6_handler, AF_INET6);
938xfrm_tunnel_ipv6_failed:
939 xfrm6_protocol_deregister(&xfrmi_ipcomp6_protocol, IPPROTO_COMP);
940#endif
Steffen Klassertf203b762018-06-12 14:07:12 +0200941xfrm_proto_comp_failed:
942 xfrm6_protocol_deregister(&xfrmi_ah6_protocol, IPPROTO_AH);
943xfrm_proto_ah_failed:
944 xfrm6_protocol_deregister(&xfrmi_esp6_protocol, IPPROTO_ESP);
945xfrm_proto_esp_failed:
946 return err;
947}
948
949static void xfrmi6_fini(void)
950{
Xin Long0a0d93b2020-07-17 15:02:30 +0800951#if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
Xin Long8b404f42020-07-13 15:42:38 +0800952 xfrm6_tunnel_deregister(&xfrmi_ip6ip_handler, AF_INET);
Xin Longd7b360c2020-07-06 20:01:37 +0800953 xfrm6_tunnel_deregister(&xfrmi_ipv6_handler, AF_INET6);
954#endif
Steffen Klassertf203b762018-06-12 14:07:12 +0200955 xfrm6_protocol_deregister(&xfrmi_ipcomp6_protocol, IPPROTO_COMP);
956 xfrm6_protocol_deregister(&xfrmi_ah6_protocol, IPPROTO_AH);
957 xfrm6_protocol_deregister(&xfrmi_esp6_protocol, IPPROTO_ESP);
958}
959
960static const struct xfrm_if_cb xfrm_if_cb = {
961 .decode_session = xfrmi_decode_session,
962};
963
964static int __init xfrmi_init(void)
965{
966 const char *msg;
967 int err;
968
969 pr_info("IPsec XFRM device driver\n");
970
971 msg = "tunnel device";
972 err = register_pernet_device(&xfrmi_net_ops);
973 if (err < 0)
974 goto pernet_dev_failed;
975
976 msg = "xfrm4 protocols";
977 err = xfrmi4_init();
978 if (err < 0)
979 goto xfrmi4_failed;
980
981 msg = "xfrm6 protocols";
982 err = xfrmi6_init();
983 if (err < 0)
984 goto xfrmi6_failed;
985
986
987 msg = "netlink interface";
988 err = rtnl_link_register(&xfrmi_link_ops);
989 if (err < 0)
990 goto rtnl_link_failed;
991
992 xfrm_if_register_cb(&xfrm_if_cb);
993
994 return err;
995
996rtnl_link_failed:
997 xfrmi6_fini();
998xfrmi6_failed:
999 xfrmi4_fini();
1000xfrmi4_failed:
1001 unregister_pernet_device(&xfrmi_net_ops);
1002pernet_dev_failed:
1003 pr_err("xfrmi init: failed to register %s\n", msg);
1004 return err;
1005}
1006
1007static void __exit xfrmi_fini(void)
1008{
1009 xfrm_if_unregister_cb();
1010 rtnl_link_unregister(&xfrmi_link_ops);
1011 xfrmi4_fini();
1012 xfrmi6_fini();
1013 unregister_pernet_device(&xfrmi_net_ops);
1014}
1015
1016module_init(xfrmi_init);
1017module_exit(xfrmi_fini);
1018MODULE_LICENSE("GPL");
1019MODULE_ALIAS_RTNL_LINK("xfrm");
1020MODULE_ALIAS_NETDEV("xfrm0");
1021MODULE_AUTHOR("Steffen Klassert");
1022MODULE_DESCRIPTION("XFRM virtual interface");