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