WIP FPC-III support
[linux/fpc-iii.git] / drivers / net / bareudp.c
blob85de5f96c02bbca4d5d95dc1e285ae739f13bbd8
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 */
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
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))
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");
33 /* per-network namespace private data for this module */
35 static unsigned int bareudp_net_id;
37 struct bareudp_net {
38 struct list_head bareudp_list;
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;
54 static int bareudp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
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;
64 bareudp = rcu_dereference_sk_user_data(sk);
65 if (!bareudp)
66 goto drop;
68 if (skb->protocol == htons(ETH_P_IP))
69 family = AF_INET;
70 else
71 family = AF_INET6;
73 if (bareudp->ethertype == htons(ETH_P_IP)) {
74 struct iphdr *iphdr;
76 iphdr = (struct iphdr *)(skb->data + BAREUDP_BASE_HLEN);
77 if (iphdr->version == 4) {
78 proto = bareudp->ethertype;
79 } else if (bareudp->multi_proto_mode && (iphdr->version == 6)) {
80 proto = htons(ETH_P_IPV6);
81 } else {
82 bareudp->dev->stats.rx_dropped++;
83 goto drop;
85 } else if (bareudp->ethertype == htons(ETH_P_MPLS_UC)) {
86 struct iphdr *tunnel_hdr;
88 tunnel_hdr = (struct iphdr *)skb_network_header(skb);
89 if (tunnel_hdr->version == 4) {
90 if (!ipv4_is_multicast(tunnel_hdr->daddr)) {
91 proto = bareudp->ethertype;
92 } else if (bareudp->multi_proto_mode &&
93 ipv4_is_multicast(tunnel_hdr->daddr)) {
94 proto = htons(ETH_P_MPLS_MC);
95 } else {
96 bareudp->dev->stats.rx_dropped++;
97 goto drop;
99 } else {
100 int addr_type;
101 struct ipv6hdr *tunnel_hdr_v6;
103 tunnel_hdr_v6 = (struct ipv6hdr *)skb_network_header(skb);
104 addr_type =
105 ipv6_addr_type((struct in6_addr *)&tunnel_hdr_v6->daddr);
106 if (!(addr_type & IPV6_ADDR_MULTICAST)) {
107 proto = bareudp->ethertype;
108 } else if (bareudp->multi_proto_mode &&
109 (addr_type & IPV6_ADDR_MULTICAST)) {
110 proto = htons(ETH_P_MPLS_MC);
111 } else {
112 bareudp->dev->stats.rx_dropped++;
113 goto drop;
116 } else {
117 proto = bareudp->ethertype;
120 if (iptunnel_pull_header(skb, BAREUDP_BASE_HLEN,
121 proto,
122 !net_eq(bareudp->net,
123 dev_net(bareudp->dev)))) {
124 bareudp->dev->stats.rx_dropped++;
125 goto drop;
127 tun_dst = udp_tun_rx_dst(skb, family, TUNNEL_KEY, 0, 0);
128 if (!tun_dst) {
129 bareudp->dev->stats.rx_dropped++;
130 goto drop;
132 skb_dst_set(skb, &tun_dst->dst);
133 skb->dev = bareudp->dev;
134 oiph = skb_network_header(skb);
135 skb_reset_network_header(skb);
137 if (!IS_ENABLED(CONFIG_IPV6) || family == AF_INET)
138 err = IP_ECN_decapsulate(oiph, skb);
139 else
140 err = IP6_ECN_decapsulate(oiph, skb);
142 if (unlikely(err)) {
143 if (log_ecn_error) {
144 if (!IS_ENABLED(CONFIG_IPV6) || family == AF_INET)
145 net_info_ratelimited("non-ECT from %pI4 "
146 "with TOS=%#x\n",
147 &((struct iphdr *)oiph)->saddr,
148 ((struct iphdr *)oiph)->tos);
149 else
150 net_info_ratelimited("non-ECT from %pI6\n",
151 &((struct ipv6hdr *)oiph)->saddr);
153 if (err > 1) {
154 ++bareudp->dev->stats.rx_frame_errors;
155 ++bareudp->dev->stats.rx_errors;
156 goto drop;
160 len = skb->len;
161 err = gro_cells_receive(&bareudp->gro_cells, skb);
162 if (likely(err == NET_RX_SUCCESS))
163 dev_sw_netstats_rx_add(bareudp->dev, len);
165 return 0;
166 drop:
167 /* Consume bad packet */
168 kfree_skb(skb);
170 return 0;
173 static int bareudp_err_lookup(struct sock *sk, struct sk_buff *skb)
175 return 0;
178 static int bareudp_init(struct net_device *dev)
180 struct bareudp_dev *bareudp = netdev_priv(dev);
181 int err;
183 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
184 if (!dev->tstats)
185 return -ENOMEM;
187 err = gro_cells_init(&bareudp->gro_cells, dev);
188 if (err) {
189 free_percpu(dev->tstats);
190 return err;
192 return 0;
195 static void bareudp_uninit(struct net_device *dev)
197 struct bareudp_dev *bareudp = netdev_priv(dev);
199 gro_cells_destroy(&bareudp->gro_cells);
200 free_percpu(dev->tstats);
203 static struct socket *bareudp_create_sock(struct net *net, __be16 port)
205 struct udp_port_cfg udp_conf;
206 struct socket *sock;
207 int err;
209 memset(&udp_conf, 0, sizeof(udp_conf));
210 #if IS_ENABLED(CONFIG_IPV6)
211 udp_conf.family = AF_INET6;
212 #else
213 udp_conf.family = AF_INET;
214 #endif
215 udp_conf.local_udp_port = port;
216 /* Open UDP socket */
217 err = udp_sock_create(net, &udp_conf, &sock);
218 if (err < 0)
219 return ERR_PTR(err);
221 return sock;
224 /* Create new listen socket if needed */
225 static int bareudp_socket_create(struct bareudp_dev *bareudp, __be16 port)
227 struct udp_tunnel_sock_cfg tunnel_cfg;
228 struct socket *sock;
230 sock = bareudp_create_sock(bareudp->net, port);
231 if (IS_ERR(sock))
232 return PTR_ERR(sock);
234 /* Mark socket as an encapsulation socket */
235 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
236 tunnel_cfg.sk_user_data = bareudp;
237 tunnel_cfg.encap_type = 1;
238 tunnel_cfg.encap_rcv = bareudp_udp_encap_recv;
239 tunnel_cfg.encap_err_lookup = bareudp_err_lookup;
240 tunnel_cfg.encap_destroy = NULL;
241 setup_udp_tunnel_sock(bareudp->net, sock, &tunnel_cfg);
243 /* As the setup_udp_tunnel_sock does not call udp_encap_enable if the
244 * socket type is v6 an explicit call to udp_encap_enable is needed.
246 if (sock->sk->sk_family == AF_INET6)
247 udp_encap_enable();
249 rcu_assign_pointer(bareudp->sock, sock);
250 return 0;
253 static int bareudp_open(struct net_device *dev)
255 struct bareudp_dev *bareudp = netdev_priv(dev);
256 int ret = 0;
258 ret = bareudp_socket_create(bareudp, bareudp->port);
259 return ret;
262 static void bareudp_sock_release(struct bareudp_dev *bareudp)
264 struct socket *sock;
266 sock = bareudp->sock;
267 rcu_assign_pointer(bareudp->sock, NULL);
268 synchronize_net();
269 udp_tunnel_sock_release(sock);
272 static int bareudp_stop(struct net_device *dev)
274 struct bareudp_dev *bareudp = netdev_priv(dev);
276 bareudp_sock_release(bareudp);
277 return 0;
280 static int bareudp_xmit_skb(struct sk_buff *skb, struct net_device *dev,
281 struct bareudp_dev *bareudp,
282 const struct ip_tunnel_info *info)
284 bool xnet = !net_eq(bareudp->net, dev_net(bareudp->dev));
285 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
286 struct socket *sock = rcu_dereference(bareudp->sock);
287 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
288 const struct ip_tunnel_key *key = &info->key;
289 struct rtable *rt;
290 __be16 sport, df;
291 int min_headroom;
292 __u8 tos, ttl;
293 __be32 saddr;
294 int err;
296 if (!sock)
297 return -ESHUTDOWN;
299 rt = ip_route_output_tunnel(skb, dev, bareudp->net, &saddr, info,
300 IPPROTO_UDP, use_cache);
302 if (IS_ERR(rt))
303 return PTR_ERR(rt);
305 skb_tunnel_check_pmtu(skb, &rt->dst,
306 BAREUDP_IPV4_HLEN + info->options_len, false);
308 sport = udp_flow_src_port(bareudp->net, skb,
309 bareudp->sport_min, USHRT_MAX,
310 true);
311 tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
312 ttl = key->ttl;
313 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
314 skb_scrub_packet(skb, xnet);
316 err = -ENOSPC;
317 if (!skb_pull(skb, skb_network_offset(skb)))
318 goto free_dst;
320 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len +
321 BAREUDP_BASE_HLEN + info->options_len + sizeof(struct iphdr);
323 err = skb_cow_head(skb, min_headroom);
324 if (unlikely(err))
325 goto free_dst;
327 err = udp_tunnel_handle_offloads(skb, udp_sum);
328 if (err)
329 goto free_dst;
331 skb_set_inner_protocol(skb, bareudp->ethertype);
332 udp_tunnel_xmit_skb(rt, sock->sk, skb, saddr, info->key.u.ipv4.dst,
333 tos, ttl, df, sport, bareudp->port,
334 !net_eq(bareudp->net, dev_net(bareudp->dev)),
335 !(info->key.tun_flags & TUNNEL_CSUM));
336 return 0;
338 free_dst:
339 dst_release(&rt->dst);
340 return err;
343 static int bareudp6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
344 struct bareudp_dev *bareudp,
345 const struct ip_tunnel_info *info)
347 bool xnet = !net_eq(bareudp->net, dev_net(bareudp->dev));
348 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
349 struct socket *sock = rcu_dereference(bareudp->sock);
350 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
351 const struct ip_tunnel_key *key = &info->key;
352 struct dst_entry *dst = NULL;
353 struct in6_addr saddr, daddr;
354 int min_headroom;
355 __u8 prio, ttl;
356 __be16 sport;
357 int err;
359 if (!sock)
360 return -ESHUTDOWN;
362 dst = ip6_dst_lookup_tunnel(skb, dev, bareudp->net, sock, &saddr, info,
363 IPPROTO_UDP, use_cache);
364 if (IS_ERR(dst))
365 return PTR_ERR(dst);
367 skb_tunnel_check_pmtu(skb, dst, BAREUDP_IPV6_HLEN + info->options_len,
368 false);
370 sport = udp_flow_src_port(bareudp->net, skb,
371 bareudp->sport_min, USHRT_MAX,
372 true);
373 prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
374 ttl = key->ttl;
376 skb_scrub_packet(skb, xnet);
378 err = -ENOSPC;
379 if (!skb_pull(skb, skb_network_offset(skb)))
380 goto free_dst;
382 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len +
383 BAREUDP_BASE_HLEN + info->options_len + sizeof(struct ipv6hdr);
385 err = skb_cow_head(skb, min_headroom);
386 if (unlikely(err))
387 goto free_dst;
389 err = udp_tunnel_handle_offloads(skb, udp_sum);
390 if (err)
391 goto free_dst;
393 daddr = info->key.u.ipv6.dst;
394 udp_tunnel6_xmit_skb(dst, sock->sk, skb, dev,
395 &saddr, &daddr, prio, ttl,
396 info->key.label, sport, bareudp->port,
397 !(info->key.tun_flags & TUNNEL_CSUM));
398 return 0;
400 free_dst:
401 dst_release(dst);
402 return err;
405 static bool bareudp_proto_valid(struct bareudp_dev *bareudp, __be16 proto)
407 if (bareudp->ethertype == proto)
408 return true;
410 if (!bareudp->multi_proto_mode)
411 return false;
413 if (bareudp->ethertype == htons(ETH_P_MPLS_UC) &&
414 proto == htons(ETH_P_MPLS_MC))
415 return true;
417 if (bareudp->ethertype == htons(ETH_P_IP) &&
418 proto == htons(ETH_P_IPV6))
419 return true;
421 return false;
424 static netdev_tx_t bareudp_xmit(struct sk_buff *skb, struct net_device *dev)
426 struct bareudp_dev *bareudp = netdev_priv(dev);
427 struct ip_tunnel_info *info = NULL;
428 int err;
430 if (!bareudp_proto_valid(bareudp, skb->protocol)) {
431 err = -EINVAL;
432 goto tx_error;
435 info = skb_tunnel_info(skb);
436 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
437 err = -EINVAL;
438 goto tx_error;
441 rcu_read_lock();
442 if (IS_ENABLED(CONFIG_IPV6) && info->mode & IP_TUNNEL_INFO_IPV6)
443 err = bareudp6_xmit_skb(skb, dev, bareudp, info);
444 else
445 err = bareudp_xmit_skb(skb, dev, bareudp, info);
447 rcu_read_unlock();
449 if (likely(!err))
450 return NETDEV_TX_OK;
451 tx_error:
452 dev_kfree_skb(skb);
454 if (err == -ELOOP)
455 dev->stats.collisions++;
456 else if (err == -ENETUNREACH)
457 dev->stats.tx_carrier_errors++;
459 dev->stats.tx_errors++;
460 return NETDEV_TX_OK;
463 static int bareudp_fill_metadata_dst(struct net_device *dev,
464 struct sk_buff *skb)
466 struct ip_tunnel_info *info = skb_tunnel_info(skb);
467 struct bareudp_dev *bareudp = netdev_priv(dev);
468 bool use_cache;
470 use_cache = ip_tunnel_dst_cache_usable(skb, info);
472 if (!IS_ENABLED(CONFIG_IPV6) || ip_tunnel_info_af(info) == AF_INET) {
473 struct rtable *rt;
474 __be32 saddr;
476 rt = ip_route_output_tunnel(skb, dev, bareudp->net, &saddr,
477 info, IPPROTO_UDP, use_cache);
478 if (IS_ERR(rt))
479 return PTR_ERR(rt);
481 ip_rt_put(rt);
482 info->key.u.ipv4.src = saddr;
483 } else if (ip_tunnel_info_af(info) == AF_INET6) {
484 struct dst_entry *dst;
485 struct in6_addr saddr;
486 struct socket *sock = rcu_dereference(bareudp->sock);
488 dst = ip6_dst_lookup_tunnel(skb, dev, bareudp->net, sock,
489 &saddr, info, IPPROTO_UDP,
490 use_cache);
491 if (IS_ERR(dst))
492 return PTR_ERR(dst);
494 dst_release(dst);
495 info->key.u.ipv6.src = saddr;
496 } else {
497 return -EINVAL;
500 info->key.tp_src = udp_flow_src_port(bareudp->net, skb,
501 bareudp->sport_min,
502 USHRT_MAX, true);
503 info->key.tp_dst = bareudp->port;
504 return 0;
507 static const struct net_device_ops bareudp_netdev_ops = {
508 .ndo_init = bareudp_init,
509 .ndo_uninit = bareudp_uninit,
510 .ndo_open = bareudp_open,
511 .ndo_stop = bareudp_stop,
512 .ndo_start_xmit = bareudp_xmit,
513 .ndo_get_stats64 = dev_get_tstats64,
514 .ndo_fill_metadata_dst = bareudp_fill_metadata_dst,
517 static const struct nla_policy bareudp_policy[IFLA_BAREUDP_MAX + 1] = {
518 [IFLA_BAREUDP_PORT] = { .type = NLA_U16 },
519 [IFLA_BAREUDP_ETHERTYPE] = { .type = NLA_U16 },
520 [IFLA_BAREUDP_SRCPORT_MIN] = { .type = NLA_U16 },
521 [IFLA_BAREUDP_MULTIPROTO_MODE] = { .type = NLA_FLAG },
524 /* Info for udev, that this is a virtual tunnel endpoint */
525 static const struct device_type bareudp_type = {
526 .name = "bareudp",
529 /* Initialize the device structure. */
530 static void bareudp_setup(struct net_device *dev)
532 dev->netdev_ops = &bareudp_netdev_ops;
533 dev->needs_free_netdev = true;
534 SET_NETDEV_DEVTYPE(dev, &bareudp_type);
535 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
536 dev->features |= NETIF_F_RXCSUM;
537 dev->features |= NETIF_F_LLTX;
538 dev->features |= NETIF_F_GSO_SOFTWARE;
539 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
540 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
541 dev->hard_header_len = 0;
542 dev->addr_len = 0;
543 dev->mtu = ETH_DATA_LEN;
544 dev->min_mtu = IPV4_MIN_MTU;
545 dev->max_mtu = IP_MAX_MTU - BAREUDP_BASE_HLEN;
546 dev->type = ARPHRD_NONE;
547 netif_keep_dst(dev);
548 dev->priv_flags |= IFF_NO_QUEUE;
549 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
552 static int bareudp_validate(struct nlattr *tb[], struct nlattr *data[],
553 struct netlink_ext_ack *extack)
555 if (!data) {
556 NL_SET_ERR_MSG(extack,
557 "Not enough attributes provided to perform the operation");
558 return -EINVAL;
560 return 0;
563 static int bareudp2info(struct nlattr *data[], struct bareudp_conf *conf,
564 struct netlink_ext_ack *extack)
566 memset(conf, 0, sizeof(*conf));
568 if (!data[IFLA_BAREUDP_PORT]) {
569 NL_SET_ERR_MSG(extack, "port not specified");
570 return -EINVAL;
572 if (!data[IFLA_BAREUDP_ETHERTYPE]) {
573 NL_SET_ERR_MSG(extack, "ethertype not specified");
574 return -EINVAL;
577 if (data[IFLA_BAREUDP_PORT])
578 conf->port = nla_get_u16(data[IFLA_BAREUDP_PORT]);
580 if (data[IFLA_BAREUDP_ETHERTYPE])
581 conf->ethertype = nla_get_u16(data[IFLA_BAREUDP_ETHERTYPE]);
583 if (data[IFLA_BAREUDP_SRCPORT_MIN])
584 conf->sport_min = nla_get_u16(data[IFLA_BAREUDP_SRCPORT_MIN]);
586 if (data[IFLA_BAREUDP_MULTIPROTO_MODE])
587 conf->multi_proto_mode = true;
589 return 0;
592 static struct bareudp_dev *bareudp_find_dev(struct bareudp_net *bn,
593 const struct bareudp_conf *conf)
595 struct bareudp_dev *bareudp, *t = NULL;
597 list_for_each_entry(bareudp, &bn->bareudp_list, next) {
598 if (conf->port == bareudp->port)
599 t = bareudp;
601 return t;
604 static int bareudp_configure(struct net *net, struct net_device *dev,
605 struct bareudp_conf *conf)
607 struct bareudp_net *bn = net_generic(net, bareudp_net_id);
608 struct bareudp_dev *t, *bareudp = netdev_priv(dev);
609 int err;
611 bareudp->net = net;
612 bareudp->dev = dev;
613 t = bareudp_find_dev(bn, conf);
614 if (t)
615 return -EBUSY;
617 if (conf->multi_proto_mode &&
618 (conf->ethertype != htons(ETH_P_MPLS_UC) &&
619 conf->ethertype != htons(ETH_P_IP)))
620 return -EINVAL;
622 bareudp->port = conf->port;
623 bareudp->ethertype = conf->ethertype;
624 bareudp->sport_min = conf->sport_min;
625 bareudp->multi_proto_mode = conf->multi_proto_mode;
627 err = register_netdevice(dev);
628 if (err)
629 return err;
631 list_add(&bareudp->next, &bn->bareudp_list);
632 return 0;
635 static int bareudp_link_config(struct net_device *dev,
636 struct nlattr *tb[])
638 int err;
640 if (tb[IFLA_MTU]) {
641 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
642 if (err)
643 return err;
645 return 0;
648 static void bareudp_dellink(struct net_device *dev, struct list_head *head)
650 struct bareudp_dev *bareudp = netdev_priv(dev);
652 list_del(&bareudp->next);
653 unregister_netdevice_queue(dev, head);
656 static int bareudp_newlink(struct net *net, struct net_device *dev,
657 struct nlattr *tb[], struct nlattr *data[],
658 struct netlink_ext_ack *extack)
660 struct bareudp_conf conf;
661 LIST_HEAD(list_kill);
662 int err;
664 err = bareudp2info(data, &conf, extack);
665 if (err)
666 return err;
668 err = bareudp_configure(net, dev, &conf);
669 if (err)
670 return err;
672 err = bareudp_link_config(dev, tb);
673 if (err)
674 goto err_unconfig;
676 return 0;
678 err_unconfig:
679 bareudp_dellink(dev, &list_kill);
680 unregister_netdevice_many(&list_kill);
681 return err;
684 static size_t bareudp_get_size(const struct net_device *dev)
686 return nla_total_size(sizeof(__be16)) + /* IFLA_BAREUDP_PORT */
687 nla_total_size(sizeof(__be16)) + /* IFLA_BAREUDP_ETHERTYPE */
688 nla_total_size(sizeof(__u16)) + /* IFLA_BAREUDP_SRCPORT_MIN */
689 nla_total_size(0) + /* IFLA_BAREUDP_MULTIPROTO_MODE */
693 static int bareudp_fill_info(struct sk_buff *skb, const struct net_device *dev)
695 struct bareudp_dev *bareudp = netdev_priv(dev);
697 if (nla_put_be16(skb, IFLA_BAREUDP_PORT, bareudp->port))
698 goto nla_put_failure;
699 if (nla_put_be16(skb, IFLA_BAREUDP_ETHERTYPE, bareudp->ethertype))
700 goto nla_put_failure;
701 if (nla_put_u16(skb, IFLA_BAREUDP_SRCPORT_MIN, bareudp->sport_min))
702 goto nla_put_failure;
703 if (bareudp->multi_proto_mode &&
704 nla_put_flag(skb, IFLA_BAREUDP_MULTIPROTO_MODE))
705 goto nla_put_failure;
707 return 0;
709 nla_put_failure:
710 return -EMSGSIZE;
713 static struct rtnl_link_ops bareudp_link_ops __read_mostly = {
714 .kind = "bareudp",
715 .maxtype = IFLA_BAREUDP_MAX,
716 .policy = bareudp_policy,
717 .priv_size = sizeof(struct bareudp_dev),
718 .setup = bareudp_setup,
719 .validate = bareudp_validate,
720 .newlink = bareudp_newlink,
721 .dellink = bareudp_dellink,
722 .get_size = bareudp_get_size,
723 .fill_info = bareudp_fill_info,
726 struct net_device *bareudp_dev_create(struct net *net, const char *name,
727 u8 name_assign_type,
728 struct bareudp_conf *conf)
730 struct nlattr *tb[IFLA_MAX + 1];
731 struct net_device *dev;
732 LIST_HEAD(list_kill);
733 int err;
735 memset(tb, 0, sizeof(tb));
736 dev = rtnl_create_link(net, name, name_assign_type,
737 &bareudp_link_ops, tb, NULL);
738 if (IS_ERR(dev))
739 return dev;
741 err = bareudp_configure(net, dev, conf);
742 if (err) {
743 free_netdev(dev);
744 return ERR_PTR(err);
746 err = dev_set_mtu(dev, IP_MAX_MTU - BAREUDP_BASE_HLEN);
747 if (err)
748 goto err;
750 err = rtnl_configure_link(dev, NULL);
751 if (err < 0)
752 goto err;
754 return dev;
755 err:
756 bareudp_dellink(dev, &list_kill);
757 unregister_netdevice_many(&list_kill);
758 return ERR_PTR(err);
760 EXPORT_SYMBOL_GPL(bareudp_dev_create);
762 static __net_init int bareudp_init_net(struct net *net)
764 struct bareudp_net *bn = net_generic(net, bareudp_net_id);
766 INIT_LIST_HEAD(&bn->bareudp_list);
767 return 0;
770 static void bareudp_destroy_tunnels(struct net *net, struct list_head *head)
772 struct bareudp_net *bn = net_generic(net, bareudp_net_id);
773 struct bareudp_dev *bareudp, *next;
775 list_for_each_entry_safe(bareudp, next, &bn->bareudp_list, next)
776 unregister_netdevice_queue(bareudp->dev, head);
779 static void __net_exit bareudp_exit_batch_net(struct list_head *net_list)
781 struct net *net;
782 LIST_HEAD(list);
784 rtnl_lock();
785 list_for_each_entry(net, net_list, exit_list)
786 bareudp_destroy_tunnels(net, &list);
788 /* unregister the devices gathered above */
789 unregister_netdevice_many(&list);
790 rtnl_unlock();
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),
800 static int __init bareudp_init_module(void)
802 int rc;
804 rc = register_pernet_subsys(&bareudp_net_ops);
805 if (rc)
806 goto out1;
808 rc = rtnl_link_register(&bareudp_link_ops);
809 if (rc)
810 goto out2;
812 return 0;
813 out2:
814 unregister_pernet_subsys(&bareudp_net_ops);
815 out1:
816 return rc;
818 late_initcall(bareudp_init_module);
820 static void __exit bareudp_cleanup_module(void)
822 rtnl_link_unregister(&bareudp_link_ops);
823 unregister_pernet_subsys(&bareudp_net_ops);
825 module_exit(bareudp_cleanup_module);
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");