]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/net/bareudp.c
bareudp: use ipv6_mod_enabled to check if IPv6 enabled
[mirror_ubuntu-jammy-kernel.git] / drivers / net / bareudp.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Bareudp: UDP tunnel encasulation for different Payload types like
3 * MPLS, NSH, IP, etc.
4 * Copyright (c) 2019 Nokia, Inc.
5 * Authors: Martin Varghese, <martin.varghese@nokia.com>
6 */
7
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/etherdevice.h>
13 #include <linux/hash.h>
14 #include <net/dst_metadata.h>
15 #include <net/gro_cells.h>
16 #include <net/rtnetlink.h>
17 #include <net/protocol.h>
18 #include <net/ip6_tunnel.h>
19 #include <net/ip_tunnels.h>
20 #include <net/udp_tunnel.h>
21 #include <net/bareudp.h>
22
23 #define BAREUDP_BASE_HLEN sizeof(struct udphdr)
24 #define BAREUDP_IPV4_HLEN (sizeof(struct iphdr) + \
25 sizeof(struct udphdr))
26 #define BAREUDP_IPV6_HLEN (sizeof(struct ipv6hdr) + \
27 sizeof(struct udphdr))
28
29 static bool log_ecn_error = true;
30 module_param(log_ecn_error, bool, 0644);
31 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
32
33 /* per-network namespace private data for this module */
34
35 static unsigned int bareudp_net_id;
36
37 struct bareudp_net {
38 struct list_head bareudp_list;
39 };
40
41 /* Pseudo network device */
42 struct bareudp_dev {
43 struct net *net; /* netns for packet i/o */
44 struct net_device *dev; /* netdev for bareudp tunnel */
45 __be16 ethertype;
46 __be16 port;
47 u16 sport_min;
48 bool multi_proto_mode;
49 struct socket __rcu *sock;
50 struct list_head next; /* bareudp node on namespace list */
51 struct gro_cells gro_cells;
52 };
53
54 static int bareudp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
55 {
56 struct metadata_dst *tun_dst = NULL;
57 struct bareudp_dev *bareudp;
58 unsigned short family;
59 unsigned int len;
60 __be16 proto;
61 void *oiph;
62 int err;
63
64 bareudp = rcu_dereference_sk_user_data(sk);
65 if (!bareudp)
66 goto drop;
67
68 if (skb->protocol == htons(ETH_P_IP))
69 family = AF_INET;
70 else
71 family = AF_INET6;
72
73 if (bareudp->ethertype == htons(ETH_P_IP)) {
74 __u8 ipversion;
75
76 if (skb_copy_bits(skb, BAREUDP_BASE_HLEN, &ipversion,
77 sizeof(ipversion))) {
78 bareudp->dev->stats.rx_dropped++;
79 goto drop;
80 }
81 ipversion >>= 4;
82
83 if (ipversion == 4) {
84 proto = htons(ETH_P_IP);
85 } else if (ipversion == 6 && bareudp->multi_proto_mode) {
86 proto = htons(ETH_P_IPV6);
87 } else {
88 bareudp->dev->stats.rx_dropped++;
89 goto drop;
90 }
91 } else if (bareudp->ethertype == htons(ETH_P_MPLS_UC)) {
92 struct iphdr *tunnel_hdr;
93
94 tunnel_hdr = (struct iphdr *)skb_network_header(skb);
95 if (tunnel_hdr->version == 4) {
96 if (!ipv4_is_multicast(tunnel_hdr->daddr)) {
97 proto = bareudp->ethertype;
98 } else if (bareudp->multi_proto_mode &&
99 ipv4_is_multicast(tunnel_hdr->daddr)) {
100 proto = htons(ETH_P_MPLS_MC);
101 } else {
102 bareudp->dev->stats.rx_dropped++;
103 goto drop;
104 }
105 } else {
106 int addr_type;
107 struct ipv6hdr *tunnel_hdr_v6;
108
109 tunnel_hdr_v6 = (struct ipv6hdr *)skb_network_header(skb);
110 addr_type =
111 ipv6_addr_type((struct in6_addr *)&tunnel_hdr_v6->daddr);
112 if (!(addr_type & IPV6_ADDR_MULTICAST)) {
113 proto = bareudp->ethertype;
114 } else if (bareudp->multi_proto_mode &&
115 (addr_type & IPV6_ADDR_MULTICAST)) {
116 proto = htons(ETH_P_MPLS_MC);
117 } else {
118 bareudp->dev->stats.rx_dropped++;
119 goto drop;
120 }
121 }
122 } else {
123 proto = bareudp->ethertype;
124 }
125
126 if (iptunnel_pull_header(skb, BAREUDP_BASE_HLEN,
127 proto,
128 !net_eq(bareudp->net,
129 dev_net(bareudp->dev)))) {
130 bareudp->dev->stats.rx_dropped++;
131 goto drop;
132 }
133 tun_dst = udp_tun_rx_dst(skb, family, TUNNEL_KEY, 0, 0);
134 if (!tun_dst) {
135 bareudp->dev->stats.rx_dropped++;
136 goto drop;
137 }
138 skb_dst_set(skb, &tun_dst->dst);
139 skb->dev = bareudp->dev;
140 oiph = skb_network_header(skb);
141 skb_reset_network_header(skb);
142 skb_reset_mac_header(skb);
143
144 if (!ipv6_mod_enabled() || family == AF_INET)
145 err = IP_ECN_decapsulate(oiph, skb);
146 else
147 err = IP6_ECN_decapsulate(oiph, skb);
148
149 if (unlikely(err)) {
150 if (log_ecn_error) {
151 if (!ipv6_mod_enabled() || family == AF_INET)
152 net_info_ratelimited("non-ECT from %pI4 "
153 "with TOS=%#x\n",
154 &((struct iphdr *)oiph)->saddr,
155 ((struct iphdr *)oiph)->tos);
156 else
157 net_info_ratelimited("non-ECT from %pI6\n",
158 &((struct ipv6hdr *)oiph)->saddr);
159 }
160 if (err > 1) {
161 ++bareudp->dev->stats.rx_frame_errors;
162 ++bareudp->dev->stats.rx_errors;
163 goto drop;
164 }
165 }
166
167 len = skb->len;
168 err = gro_cells_receive(&bareudp->gro_cells, skb);
169 if (likely(err == NET_RX_SUCCESS))
170 dev_sw_netstats_rx_add(bareudp->dev, len);
171
172 return 0;
173 drop:
174 /* Consume bad packet */
175 kfree_skb(skb);
176
177 return 0;
178 }
179
180 static int bareudp_err_lookup(struct sock *sk, struct sk_buff *skb)
181 {
182 return 0;
183 }
184
185 static int bareudp_init(struct net_device *dev)
186 {
187 struct bareudp_dev *bareudp = netdev_priv(dev);
188 int err;
189
190 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
191 if (!dev->tstats)
192 return -ENOMEM;
193
194 err = gro_cells_init(&bareudp->gro_cells, dev);
195 if (err) {
196 free_percpu(dev->tstats);
197 return err;
198 }
199 return 0;
200 }
201
202 static void bareudp_uninit(struct net_device *dev)
203 {
204 struct bareudp_dev *bareudp = netdev_priv(dev);
205
206 gro_cells_destroy(&bareudp->gro_cells);
207 free_percpu(dev->tstats);
208 }
209
210 static struct socket *bareudp_create_sock(struct net *net, __be16 port)
211 {
212 struct udp_port_cfg udp_conf;
213 struct socket *sock;
214 int err;
215
216 memset(&udp_conf, 0, sizeof(udp_conf));
217
218 if (ipv6_mod_enabled())
219 udp_conf.family = AF_INET6;
220 else
221 udp_conf.family = AF_INET;
222
223 udp_conf.local_udp_port = port;
224 /* Open UDP socket */
225 err = udp_sock_create(net, &udp_conf, &sock);
226 if (err < 0)
227 return ERR_PTR(err);
228
229 udp_allow_gso(sock->sk);
230 return sock;
231 }
232
233 /* Create new listen socket if needed */
234 static int bareudp_socket_create(struct bareudp_dev *bareudp, __be16 port)
235 {
236 struct udp_tunnel_sock_cfg tunnel_cfg;
237 struct socket *sock;
238
239 sock = bareudp_create_sock(bareudp->net, port);
240 if (IS_ERR(sock))
241 return PTR_ERR(sock);
242
243 /* Mark socket as an encapsulation socket */
244 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
245 tunnel_cfg.sk_user_data = bareudp;
246 tunnel_cfg.encap_type = 1;
247 tunnel_cfg.encap_rcv = bareudp_udp_encap_recv;
248 tunnel_cfg.encap_err_lookup = bareudp_err_lookup;
249 tunnel_cfg.encap_destroy = NULL;
250 setup_udp_tunnel_sock(bareudp->net, sock, &tunnel_cfg);
251
252 rcu_assign_pointer(bareudp->sock, sock);
253 return 0;
254 }
255
256 static int bareudp_open(struct net_device *dev)
257 {
258 struct bareudp_dev *bareudp = netdev_priv(dev);
259 int ret = 0;
260
261 ret = bareudp_socket_create(bareudp, bareudp->port);
262 return ret;
263 }
264
265 static void bareudp_sock_release(struct bareudp_dev *bareudp)
266 {
267 struct socket *sock;
268
269 sock = bareudp->sock;
270 rcu_assign_pointer(bareudp->sock, NULL);
271 synchronize_net();
272 udp_tunnel_sock_release(sock);
273 }
274
275 static int bareudp_stop(struct net_device *dev)
276 {
277 struct bareudp_dev *bareudp = netdev_priv(dev);
278
279 bareudp_sock_release(bareudp);
280 return 0;
281 }
282
283 static int bareudp_xmit_skb(struct sk_buff *skb, struct net_device *dev,
284 struct bareudp_dev *bareudp,
285 const struct ip_tunnel_info *info)
286 {
287 bool xnet = !net_eq(bareudp->net, dev_net(bareudp->dev));
288 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
289 struct socket *sock = rcu_dereference(bareudp->sock);
290 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
291 const struct ip_tunnel_key *key = &info->key;
292 struct rtable *rt;
293 __be16 sport, df;
294 int min_headroom;
295 __u8 tos, ttl;
296 __be32 saddr;
297 int err;
298
299 if (!sock)
300 return -ESHUTDOWN;
301
302 rt = ip_route_output_tunnel(skb, dev, bareudp->net, &saddr, info,
303 IPPROTO_UDP, use_cache);
304
305 if (IS_ERR(rt))
306 return PTR_ERR(rt);
307
308 skb_tunnel_check_pmtu(skb, &rt->dst,
309 BAREUDP_IPV4_HLEN + info->options_len, false);
310
311 sport = udp_flow_src_port(bareudp->net, skb,
312 bareudp->sport_min, USHRT_MAX,
313 true);
314 tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
315 ttl = key->ttl;
316 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
317 skb_scrub_packet(skb, xnet);
318
319 err = -ENOSPC;
320 if (!skb_pull(skb, skb_network_offset(skb)))
321 goto free_dst;
322
323 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len +
324 BAREUDP_BASE_HLEN + info->options_len + sizeof(struct iphdr);
325
326 err = skb_cow_head(skb, min_headroom);
327 if (unlikely(err))
328 goto free_dst;
329
330 err = udp_tunnel_handle_offloads(skb, udp_sum);
331 if (err)
332 goto free_dst;
333
334 skb_set_inner_protocol(skb, bareudp->ethertype);
335 udp_tunnel_xmit_skb(rt, sock->sk, skb, saddr, info->key.u.ipv4.dst,
336 tos, ttl, df, sport, bareudp->port,
337 !net_eq(bareudp->net, dev_net(bareudp->dev)),
338 !(info->key.tun_flags & TUNNEL_CSUM));
339 return 0;
340
341 free_dst:
342 dst_release(&rt->dst);
343 return err;
344 }
345
346 static int bareudp6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
347 struct bareudp_dev *bareudp,
348 const struct ip_tunnel_info *info)
349 {
350 bool xnet = !net_eq(bareudp->net, dev_net(bareudp->dev));
351 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
352 struct socket *sock = rcu_dereference(bareudp->sock);
353 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
354 const struct ip_tunnel_key *key = &info->key;
355 struct dst_entry *dst = NULL;
356 struct in6_addr saddr, daddr;
357 int min_headroom;
358 __u8 prio, ttl;
359 __be16 sport;
360 int err;
361
362 if (!sock)
363 return -ESHUTDOWN;
364
365 dst = ip6_dst_lookup_tunnel(skb, dev, bareudp->net, sock, &saddr, info,
366 IPPROTO_UDP, use_cache);
367 if (IS_ERR(dst))
368 return PTR_ERR(dst);
369
370 skb_tunnel_check_pmtu(skb, dst, BAREUDP_IPV6_HLEN + info->options_len,
371 false);
372
373 sport = udp_flow_src_port(bareudp->net, skb,
374 bareudp->sport_min, USHRT_MAX,
375 true);
376 prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
377 ttl = key->ttl;
378
379 skb_scrub_packet(skb, xnet);
380
381 err = -ENOSPC;
382 if (!skb_pull(skb, skb_network_offset(skb)))
383 goto free_dst;
384
385 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len +
386 BAREUDP_BASE_HLEN + info->options_len + sizeof(struct ipv6hdr);
387
388 err = skb_cow_head(skb, min_headroom);
389 if (unlikely(err))
390 goto free_dst;
391
392 err = udp_tunnel_handle_offloads(skb, udp_sum);
393 if (err)
394 goto free_dst;
395
396 daddr = info->key.u.ipv6.dst;
397 udp_tunnel6_xmit_skb(dst, sock->sk, skb, dev,
398 &saddr, &daddr, prio, ttl,
399 info->key.label, sport, bareudp->port,
400 !(info->key.tun_flags & TUNNEL_CSUM));
401 return 0;
402
403 free_dst:
404 dst_release(dst);
405 return err;
406 }
407
408 static bool bareudp_proto_valid(struct bareudp_dev *bareudp, __be16 proto)
409 {
410 if (bareudp->ethertype == proto)
411 return true;
412
413 if (!bareudp->multi_proto_mode)
414 return false;
415
416 if (bareudp->ethertype == htons(ETH_P_MPLS_UC) &&
417 proto == htons(ETH_P_MPLS_MC))
418 return true;
419
420 if (bareudp->ethertype == htons(ETH_P_IP) &&
421 proto == htons(ETH_P_IPV6))
422 return true;
423
424 return false;
425 }
426
427 static netdev_tx_t bareudp_xmit(struct sk_buff *skb, struct net_device *dev)
428 {
429 struct bareudp_dev *bareudp = netdev_priv(dev);
430 struct ip_tunnel_info *info = NULL;
431 int err;
432
433 if (!bareudp_proto_valid(bareudp, skb->protocol)) {
434 err = -EINVAL;
435 goto tx_error;
436 }
437
438 info = skb_tunnel_info(skb);
439 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
440 err = -EINVAL;
441 goto tx_error;
442 }
443
444 rcu_read_lock();
445 if (ipv6_mod_enabled() && info->mode & IP_TUNNEL_INFO_IPV6)
446 err = bareudp6_xmit_skb(skb, dev, bareudp, info);
447 else
448 err = bareudp_xmit_skb(skb, dev, bareudp, info);
449
450 rcu_read_unlock();
451
452 if (likely(!err))
453 return NETDEV_TX_OK;
454 tx_error:
455 dev_kfree_skb(skb);
456
457 if (err == -ELOOP)
458 dev->stats.collisions++;
459 else if (err == -ENETUNREACH)
460 dev->stats.tx_carrier_errors++;
461
462 dev->stats.tx_errors++;
463 return NETDEV_TX_OK;
464 }
465
466 static int bareudp_fill_metadata_dst(struct net_device *dev,
467 struct sk_buff *skb)
468 {
469 struct ip_tunnel_info *info = skb_tunnel_info(skb);
470 struct bareudp_dev *bareudp = netdev_priv(dev);
471 bool use_cache;
472
473 use_cache = ip_tunnel_dst_cache_usable(skb, info);
474
475 if (!ipv6_mod_enabled() || ip_tunnel_info_af(info) == AF_INET) {
476 struct rtable *rt;
477 __be32 saddr;
478
479 rt = ip_route_output_tunnel(skb, dev, bareudp->net, &saddr,
480 info, IPPROTO_UDP, use_cache);
481 if (IS_ERR(rt))
482 return PTR_ERR(rt);
483
484 ip_rt_put(rt);
485 info->key.u.ipv4.src = saddr;
486 } else if (ip_tunnel_info_af(info) == AF_INET6) {
487 struct dst_entry *dst;
488 struct in6_addr saddr;
489 struct socket *sock = rcu_dereference(bareudp->sock);
490
491 dst = ip6_dst_lookup_tunnel(skb, dev, bareudp->net, sock,
492 &saddr, info, IPPROTO_UDP,
493 use_cache);
494 if (IS_ERR(dst))
495 return PTR_ERR(dst);
496
497 dst_release(dst);
498 info->key.u.ipv6.src = saddr;
499 } else {
500 return -EINVAL;
501 }
502
503 info->key.tp_src = udp_flow_src_port(bareudp->net, skb,
504 bareudp->sport_min,
505 USHRT_MAX, true);
506 info->key.tp_dst = bareudp->port;
507 return 0;
508 }
509
510 static const struct net_device_ops bareudp_netdev_ops = {
511 .ndo_init = bareudp_init,
512 .ndo_uninit = bareudp_uninit,
513 .ndo_open = bareudp_open,
514 .ndo_stop = bareudp_stop,
515 .ndo_start_xmit = bareudp_xmit,
516 .ndo_get_stats64 = dev_get_tstats64,
517 .ndo_fill_metadata_dst = bareudp_fill_metadata_dst,
518 };
519
520 static const struct nla_policy bareudp_policy[IFLA_BAREUDP_MAX + 1] = {
521 [IFLA_BAREUDP_PORT] = { .type = NLA_U16 },
522 [IFLA_BAREUDP_ETHERTYPE] = { .type = NLA_U16 },
523 [IFLA_BAREUDP_SRCPORT_MIN] = { .type = NLA_U16 },
524 [IFLA_BAREUDP_MULTIPROTO_MODE] = { .type = NLA_FLAG },
525 };
526
527 /* Info for udev, that this is a virtual tunnel endpoint */
528 static const struct device_type bareudp_type = {
529 .name = "bareudp",
530 };
531
532 /* Initialize the device structure. */
533 static void bareudp_setup(struct net_device *dev)
534 {
535 dev->netdev_ops = &bareudp_netdev_ops;
536 dev->needs_free_netdev = true;
537 SET_NETDEV_DEVTYPE(dev, &bareudp_type);
538 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST;
539 dev->features |= NETIF_F_RXCSUM;
540 dev->features |= NETIF_F_LLTX;
541 dev->features |= NETIF_F_GSO_SOFTWARE;
542 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST;
543 dev->hw_features |= NETIF_F_RXCSUM;
544 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
545 dev->hard_header_len = 0;
546 dev->addr_len = 0;
547 dev->mtu = ETH_DATA_LEN;
548 dev->min_mtu = IPV4_MIN_MTU;
549 dev->max_mtu = IP_MAX_MTU - BAREUDP_BASE_HLEN;
550 dev->type = ARPHRD_NONE;
551 netif_keep_dst(dev);
552 dev->priv_flags |= IFF_NO_QUEUE;
553 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
554 }
555
556 static int bareudp_validate(struct nlattr *tb[], struct nlattr *data[],
557 struct netlink_ext_ack *extack)
558 {
559 if (!data) {
560 NL_SET_ERR_MSG(extack,
561 "Not enough attributes provided to perform the operation");
562 return -EINVAL;
563 }
564 return 0;
565 }
566
567 static int bareudp2info(struct nlattr *data[], struct bareudp_conf *conf,
568 struct netlink_ext_ack *extack)
569 {
570 memset(conf, 0, sizeof(*conf));
571
572 if (!data[IFLA_BAREUDP_PORT]) {
573 NL_SET_ERR_MSG(extack, "port not specified");
574 return -EINVAL;
575 }
576 if (!data[IFLA_BAREUDP_ETHERTYPE]) {
577 NL_SET_ERR_MSG(extack, "ethertype not specified");
578 return -EINVAL;
579 }
580
581 if (data[IFLA_BAREUDP_PORT])
582 conf->port = nla_get_u16(data[IFLA_BAREUDP_PORT]);
583
584 if (data[IFLA_BAREUDP_ETHERTYPE])
585 conf->ethertype = nla_get_u16(data[IFLA_BAREUDP_ETHERTYPE]);
586
587 if (data[IFLA_BAREUDP_SRCPORT_MIN])
588 conf->sport_min = nla_get_u16(data[IFLA_BAREUDP_SRCPORT_MIN]);
589
590 if (data[IFLA_BAREUDP_MULTIPROTO_MODE])
591 conf->multi_proto_mode = true;
592
593 return 0;
594 }
595
596 static struct bareudp_dev *bareudp_find_dev(struct bareudp_net *bn,
597 const struct bareudp_conf *conf)
598 {
599 struct bareudp_dev *bareudp, *t = NULL;
600
601 list_for_each_entry(bareudp, &bn->bareudp_list, next) {
602 if (conf->port == bareudp->port)
603 t = bareudp;
604 }
605 return t;
606 }
607
608 static int bareudp_configure(struct net *net, struct net_device *dev,
609 struct bareudp_conf *conf)
610 {
611 struct bareudp_net *bn = net_generic(net, bareudp_net_id);
612 struct bareudp_dev *t, *bareudp = netdev_priv(dev);
613 int err;
614
615 bareudp->net = net;
616 bareudp->dev = dev;
617 t = bareudp_find_dev(bn, conf);
618 if (t)
619 return -EBUSY;
620
621 if (conf->multi_proto_mode &&
622 (conf->ethertype != htons(ETH_P_MPLS_UC) &&
623 conf->ethertype != htons(ETH_P_IP)))
624 return -EINVAL;
625
626 bareudp->port = conf->port;
627 bareudp->ethertype = conf->ethertype;
628 bareudp->sport_min = conf->sport_min;
629 bareudp->multi_proto_mode = conf->multi_proto_mode;
630
631 err = register_netdevice(dev);
632 if (err)
633 return err;
634
635 list_add(&bareudp->next, &bn->bareudp_list);
636 return 0;
637 }
638
639 static int bareudp_link_config(struct net_device *dev,
640 struct nlattr *tb[])
641 {
642 int err;
643
644 if (tb[IFLA_MTU]) {
645 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
646 if (err)
647 return err;
648 }
649 return 0;
650 }
651
652 static void bareudp_dellink(struct net_device *dev, struct list_head *head)
653 {
654 struct bareudp_dev *bareudp = netdev_priv(dev);
655
656 list_del(&bareudp->next);
657 unregister_netdevice_queue(dev, head);
658 }
659
660 static int bareudp_newlink(struct net *net, struct net_device *dev,
661 struct nlattr *tb[], struct nlattr *data[],
662 struct netlink_ext_ack *extack)
663 {
664 struct bareudp_conf conf;
665 int err;
666
667 err = bareudp2info(data, &conf, extack);
668 if (err)
669 return err;
670
671 err = bareudp_configure(net, dev, &conf);
672 if (err)
673 return err;
674
675 err = bareudp_link_config(dev, tb);
676 if (err)
677 goto err_unconfig;
678
679 return 0;
680
681 err_unconfig:
682 bareudp_dellink(dev, NULL);
683 return err;
684 }
685
686 static size_t bareudp_get_size(const struct net_device *dev)
687 {
688 return nla_total_size(sizeof(__be16)) + /* IFLA_BAREUDP_PORT */
689 nla_total_size(sizeof(__be16)) + /* IFLA_BAREUDP_ETHERTYPE */
690 nla_total_size(sizeof(__u16)) + /* IFLA_BAREUDP_SRCPORT_MIN */
691 nla_total_size(0) + /* IFLA_BAREUDP_MULTIPROTO_MODE */
692 0;
693 }
694
695 static int bareudp_fill_info(struct sk_buff *skb, const struct net_device *dev)
696 {
697 struct bareudp_dev *bareudp = netdev_priv(dev);
698
699 if (nla_put_be16(skb, IFLA_BAREUDP_PORT, bareudp->port))
700 goto nla_put_failure;
701 if (nla_put_be16(skb, IFLA_BAREUDP_ETHERTYPE, bareudp->ethertype))
702 goto nla_put_failure;
703 if (nla_put_u16(skb, IFLA_BAREUDP_SRCPORT_MIN, bareudp->sport_min))
704 goto nla_put_failure;
705 if (bareudp->multi_proto_mode &&
706 nla_put_flag(skb, IFLA_BAREUDP_MULTIPROTO_MODE))
707 goto nla_put_failure;
708
709 return 0;
710
711 nla_put_failure:
712 return -EMSGSIZE;
713 }
714
715 static struct rtnl_link_ops bareudp_link_ops __read_mostly = {
716 .kind = "bareudp",
717 .maxtype = IFLA_BAREUDP_MAX,
718 .policy = bareudp_policy,
719 .priv_size = sizeof(struct bareudp_dev),
720 .setup = bareudp_setup,
721 .validate = bareudp_validate,
722 .newlink = bareudp_newlink,
723 .dellink = bareudp_dellink,
724 .get_size = bareudp_get_size,
725 .fill_info = bareudp_fill_info,
726 };
727
728 struct net_device *bareudp_dev_create(struct net *net, const char *name,
729 u8 name_assign_type,
730 struct bareudp_conf *conf)
731 {
732 struct nlattr *tb[IFLA_MAX + 1];
733 struct net_device *dev;
734 int err;
735
736 memset(tb, 0, sizeof(tb));
737 dev = rtnl_create_link(net, name, name_assign_type,
738 &bareudp_link_ops, tb, NULL);
739 if (IS_ERR(dev))
740 return dev;
741
742 err = bareudp_configure(net, dev, conf);
743 if (err) {
744 free_netdev(dev);
745 return ERR_PTR(err);
746 }
747 err = dev_set_mtu(dev, IP_MAX_MTU - BAREUDP_BASE_HLEN);
748 if (err)
749 goto err;
750
751 err = rtnl_configure_link(dev, NULL);
752 if (err < 0)
753 goto err;
754
755 return dev;
756 err:
757 bareudp_dellink(dev, NULL);
758 return ERR_PTR(err);
759 }
760 EXPORT_SYMBOL_GPL(bareudp_dev_create);
761
762 static __net_init int bareudp_init_net(struct net *net)
763 {
764 struct bareudp_net *bn = net_generic(net, bareudp_net_id);
765
766 INIT_LIST_HEAD(&bn->bareudp_list);
767 return 0;
768 }
769
770 static void bareudp_destroy_tunnels(struct net *net, struct list_head *head)
771 {
772 struct bareudp_net *bn = net_generic(net, bareudp_net_id);
773 struct bareudp_dev *bareudp, *next;
774
775 list_for_each_entry_safe(bareudp, next, &bn->bareudp_list, next)
776 unregister_netdevice_queue(bareudp->dev, head);
777 }
778
779 static void __net_exit bareudp_exit_batch_net(struct list_head *net_list)
780 {
781 struct net *net;
782 LIST_HEAD(list);
783
784 rtnl_lock();
785 list_for_each_entry(net, net_list, exit_list)
786 bareudp_destroy_tunnels(net, &list);
787
788 /* unregister the devices gathered above */
789 unregister_netdevice_many(&list);
790 rtnl_unlock();
791 }
792
793 static struct pernet_operations bareudp_net_ops = {
794 .init = bareudp_init_net,
795 .exit_batch = bareudp_exit_batch_net,
796 .id = &bareudp_net_id,
797 .size = sizeof(struct bareudp_net),
798 };
799
800 static int __init bareudp_init_module(void)
801 {
802 int rc;
803
804 rc = register_pernet_subsys(&bareudp_net_ops);
805 if (rc)
806 goto out1;
807
808 rc = rtnl_link_register(&bareudp_link_ops);
809 if (rc)
810 goto out2;
811
812 return 0;
813 out2:
814 unregister_pernet_subsys(&bareudp_net_ops);
815 out1:
816 return rc;
817 }
818 late_initcall(bareudp_init_module);
819
820 static void __exit bareudp_cleanup_module(void)
821 {
822 rtnl_link_unregister(&bareudp_link_ops);
823 unregister_pernet_subsys(&bareudp_net_ops);
824 }
825 module_exit(bareudp_cleanup_module);
826
827 MODULE_ALIAS_RTNL_LINK("bareudp");
828 MODULE_LICENSE("GPL");
829 MODULE_AUTHOR("Martin Varghese <martin.varghese@nokia.com>");
830 MODULE_DESCRIPTION("Interface driver for UDP encapsulated traffic");