]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/net/bareudp.c
Merge tag 'for-linus-5.7-rc1-tag' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-jammy-kernel.git] / drivers / net / bareudp.c
CommitLineData
571912c6
MV
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
29static bool log_ecn_error = true;
30module_param(log_ecn_error, bool, 0644);
31MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
32
33/* per-network namespace private data for this module */
34
35static unsigned int bareudp_net_id;
36
37struct bareudp_net {
38 struct list_head bareudp_list;
39};
40
41/* Pseudo network device */
42struct 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;
4b5f6723 48 bool multi_proto_mode;
571912c6
MV
49 struct socket __rcu *sock;
50 struct list_head next; /* bareudp node on namespace list */
51 struct gro_cells gro_cells;
52};
53
54static int bareudp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
55{
56 struct metadata_dst *tun_dst = NULL;
57 struct pcpu_sw_netstats *stats;
58 struct bareudp_dev *bareudp;
59 unsigned short family;
60 unsigned int len;
61 __be16 proto;
62 void *oiph;
63 int err;
64
65 bareudp = rcu_dereference_sk_user_data(sk);
66 if (!bareudp)
67 goto drop;
68
69 if (skb->protocol == htons(ETH_P_IP))
70 family = AF_INET;
71 else
72 family = AF_INET6;
73
4b5f6723
MV
74 if (bareudp->ethertype == htons(ETH_P_IP)) {
75 struct iphdr *iphdr;
76
77 iphdr = (struct iphdr *)(skb->data + BAREUDP_BASE_HLEN);
78 if (iphdr->version == 4) {
79 proto = bareudp->ethertype;
80 } else if (bareudp->multi_proto_mode && (iphdr->version == 6)) {
81 proto = htons(ETH_P_IPV6);
82 } else {
83 bareudp->dev->stats.rx_dropped++;
84 goto drop;
85 }
86 } else if (bareudp->ethertype == htons(ETH_P_MPLS_UC)) {
87 struct iphdr *tunnel_hdr;
88
89 tunnel_hdr = (struct iphdr *)skb_network_header(skb);
90 if (tunnel_hdr->version == 4) {
91 if (!ipv4_is_multicast(tunnel_hdr->daddr)) {
92 proto = bareudp->ethertype;
93 } else if (bareudp->multi_proto_mode &&
94 ipv4_is_multicast(tunnel_hdr->daddr)) {
95 proto = htons(ETH_P_MPLS_MC);
96 } else {
97 bareudp->dev->stats.rx_dropped++;
98 goto drop;
99 }
100 } else {
101 int addr_type;
102 struct ipv6hdr *tunnel_hdr_v6;
103
104 tunnel_hdr_v6 = (struct ipv6hdr *)skb_network_header(skb);
105 addr_type =
106 ipv6_addr_type((struct in6_addr *)&tunnel_hdr_v6->daddr);
107 if (!(addr_type & IPV6_ADDR_MULTICAST)) {
108 proto = bareudp->ethertype;
109 } else if (bareudp->multi_proto_mode &&
110 (addr_type & IPV6_ADDR_MULTICAST)) {
111 proto = htons(ETH_P_MPLS_MC);
112 } else {
113 bareudp->dev->stats.rx_dropped++;
114 goto drop;
115 }
116 }
117 } else {
118 proto = bareudp->ethertype;
119 }
571912c6
MV
120
121 if (iptunnel_pull_header(skb, BAREUDP_BASE_HLEN,
122 proto,
123 !net_eq(bareudp->net,
124 dev_net(bareudp->dev)))) {
125 bareudp->dev->stats.rx_dropped++;
126 goto drop;
127 }
128
129 tun_dst = udp_tun_rx_dst(skb, family, TUNNEL_KEY, 0, 0);
130 if (!tun_dst) {
131 bareudp->dev->stats.rx_dropped++;
132 goto drop;
133 }
134 skb_dst_set(skb, &tun_dst->dst);
135 skb->dev = bareudp->dev;
136 oiph = skb_network_header(skb);
137 skb_reset_network_header(skb);
138
139 if (family == AF_INET)
140 err = IP_ECN_decapsulate(oiph, skb);
141#if IS_ENABLED(CONFIG_IPV6)
142 else
143 err = IP6_ECN_decapsulate(oiph, skb);
144#endif
145
146 if (unlikely(err)) {
147 if (log_ecn_error) {
148 if (family == AF_INET)
149 net_info_ratelimited("non-ECT from %pI4 "
150 "with TOS=%#x\n",
151 &((struct iphdr *)oiph)->saddr,
152 ((struct iphdr *)oiph)->tos);
153#if IS_ENABLED(CONFIG_IPV6)
154 else
155 net_info_ratelimited("non-ECT from %pI6\n",
156 &((struct ipv6hdr *)oiph)->saddr);
157#endif
158 }
159 if (err > 1) {
160 ++bareudp->dev->stats.rx_frame_errors;
161 ++bareudp->dev->stats.rx_errors;
162 goto drop;
163 }
164 }
165
166 len = skb->len;
167 err = gro_cells_receive(&bareudp->gro_cells, skb);
168 if (likely(err == NET_RX_SUCCESS)) {
169 stats = this_cpu_ptr(bareudp->dev->tstats);
170 u64_stats_update_begin(&stats->syncp);
171 stats->rx_packets++;
172 stats->rx_bytes += len;
173 u64_stats_update_end(&stats->syncp);
174 }
175 return 0;
176drop:
177 /* Consume bad packet */
178 kfree_skb(skb);
179
180 return 0;
181}
182
183static int bareudp_err_lookup(struct sock *sk, struct sk_buff *skb)
184{
185 return 0;
186}
187
188static int bareudp_init(struct net_device *dev)
189{
190 struct bareudp_dev *bareudp = netdev_priv(dev);
191 int err;
192
193 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
194 if (!dev->tstats)
195 return -ENOMEM;
196
197 err = gro_cells_init(&bareudp->gro_cells, dev);
198 if (err) {
199 free_percpu(dev->tstats);
200 return err;
201 }
202 return 0;
203}
204
205static void bareudp_uninit(struct net_device *dev)
206{
207 struct bareudp_dev *bareudp = netdev_priv(dev);
208
209 gro_cells_destroy(&bareudp->gro_cells);
210 free_percpu(dev->tstats);
211}
212
213static struct socket *bareudp_create_sock(struct net *net, __be16 port)
214{
215 struct udp_port_cfg udp_conf;
216 struct socket *sock;
217 int err;
218
219 memset(&udp_conf, 0, sizeof(udp_conf));
220#if IS_ENABLED(CONFIG_IPV6)
221 udp_conf.family = AF_INET6;
222#else
223 udp_conf.family = AF_INET;
224#endif
225 udp_conf.local_udp_port = port;
226 /* Open UDP socket */
227 err = udp_sock_create(net, &udp_conf, &sock);
228 if (err < 0)
229 return ERR_PTR(err);
230
231 return sock;
232}
233
234/* Create new listen socket if needed */
235static int bareudp_socket_create(struct bareudp_dev *bareudp, __be16 port)
236{
237 struct udp_tunnel_sock_cfg tunnel_cfg;
238 struct socket *sock;
239
240 sock = bareudp_create_sock(bareudp->net, port);
241 if (IS_ERR(sock))
242 return PTR_ERR(sock);
243
244 /* Mark socket as an encapsulation socket */
245 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
246 tunnel_cfg.sk_user_data = bareudp;
247 tunnel_cfg.encap_type = 1;
248 tunnel_cfg.encap_rcv = bareudp_udp_encap_recv;
249 tunnel_cfg.encap_err_lookup = bareudp_err_lookup;
250 tunnel_cfg.encap_destroy = NULL;
251 setup_udp_tunnel_sock(bareudp->net, sock, &tunnel_cfg);
252
81f954a4
MV
253 /* As the setup_udp_tunnel_sock does not call udp_encap_enable if the
254 * socket type is v6 an explicit call to udp_encap_enable is needed.
255 */
256 if (sock->sk->sk_family == AF_INET6)
257 udp_encap_enable();
258
571912c6
MV
259 rcu_assign_pointer(bareudp->sock, sock);
260 return 0;
261}
262
263static 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
272static 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
282static 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
290static 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);
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
c102b6fd 326 err = -ENOSPC;
571912c6
MV
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
348free_dst:
349 dst_release(&rt->dst);
350 return err;
351}
352
353#if IS_ENABLED(CONFIG_IPV6)
354static int bareudp6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
355 struct bareudp_dev *bareudp,
356 const struct ip_tunnel_info *info)
357{
358 bool xnet = !net_eq(bareudp->net, dev_net(bareudp->dev));
359 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
360 struct socket *sock = rcu_dereference(bareudp->sock);
361 bool udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
362 const struct ip_tunnel_key *key = &info->key;
363 struct dst_entry *dst = NULL;
364 struct in6_addr saddr, daddr;
365 int min_headroom;
366 __u8 prio, ttl;
367 __be16 sport;
368 int err;
369
370 if (!sock)
371 return -ESHUTDOWN;
372
373 dst = ip6_dst_lookup_tunnel(skb, dev, bareudp->net, sock, &saddr, info,
374 IPPROTO_UDP, use_cache);
375 if (IS_ERR(dst))
376 return PTR_ERR(dst);
377
378 skb_tunnel_check_pmtu(skb, dst, BAREUDP_IPV6_HLEN + info->options_len);
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
c102b6fd 388 err = -ENOSPC;
571912c6
MV
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 iphdr);
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
410free_dst:
411 dst_release(dst);
412 return err;
413}
414#endif
415
416static netdev_tx_t bareudp_xmit(struct sk_buff *skb, struct net_device *dev)
417{
418 struct bareudp_dev *bareudp = netdev_priv(dev);
419 struct ip_tunnel_info *info = NULL;
420 int err;
421
422 if (skb->protocol != bareudp->ethertype) {
4b5f6723
MV
423 if (!bareudp->multi_proto_mode ||
424 (skb->protocol != htons(ETH_P_MPLS_MC) &&
425 skb->protocol != htons(ETH_P_IPV6))) {
426 err = -EINVAL;
427 goto tx_error;
428 }
571912c6
MV
429 }
430
431 info = skb_tunnel_info(skb);
432 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
433 err = -EINVAL;
434 goto tx_error;
435 }
436
437 rcu_read_lock();
438#if IS_ENABLED(CONFIG_IPV6)
439 if (info->mode & IP_TUNNEL_INFO_IPV6)
440 err = bareudp6_xmit_skb(skb, dev, bareudp, info);
441 else
442#endif
443 err = bareudp_xmit_skb(skb, dev, bareudp, info);
444
445 rcu_read_unlock();
446
447 if (likely(!err))
448 return NETDEV_TX_OK;
449tx_error:
450 dev_kfree_skb(skb);
451
452 if (err == -ELOOP)
453 dev->stats.collisions++;
454 else if (err == -ENETUNREACH)
455 dev->stats.tx_carrier_errors++;
456
457 dev->stats.tx_errors++;
458 return NETDEV_TX_OK;
459}
460
461static int bareudp_fill_metadata_dst(struct net_device *dev,
462 struct sk_buff *skb)
463{
464 struct ip_tunnel_info *info = skb_tunnel_info(skb);
465 struct bareudp_dev *bareudp = netdev_priv(dev);
466 bool use_cache;
467
468 use_cache = ip_tunnel_dst_cache_usable(skb, info);
469
470 if (ip_tunnel_info_af(info) == AF_INET) {
471 struct rtable *rt;
472 __be32 saddr;
473
474 rt = ip_route_output_tunnel(skb, dev, bareudp->net, &saddr,
475 info, IPPROTO_UDP, use_cache);
476 if (IS_ERR(rt))
477 return PTR_ERR(rt);
478
479 ip_rt_put(rt);
480 info->key.u.ipv4.src = saddr;
481#if IS_ENABLED(CONFIG_IPV6)
482 } else if (ip_tunnel_info_af(info) == AF_INET6) {
483 struct dst_entry *dst;
484 struct in6_addr saddr;
485 struct socket *sock = rcu_dereference(bareudp->sock);
486
487 dst = ip6_dst_lookup_tunnel(skb, dev, bareudp->net, sock,
488 &saddr, info, IPPROTO_UDP,
489 use_cache);
490 if (IS_ERR(dst))
491 return PTR_ERR(dst);
492
493 dst_release(dst);
494 info->key.u.ipv6.src = saddr;
495#endif
496 } else {
497 return -EINVAL;
498 }
499
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;
505}
506
507static 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 = ip_tunnel_get_stats64,
514 .ndo_fill_metadata_dst = bareudp_fill_metadata_dst,
515};
516
517static 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 },
4b5f6723 521 [IFLA_BAREUDP_MULTIPROTO_MODE] = { .type = NLA_FLAG },
571912c6
MV
522};
523
524/* Info for udev, that this is a virtual tunnel endpoint */
525static struct device_type bareudp_type = {
526 .name = "bareudp",
527};
528
529/* Initialize the device structure. */
530static void bareudp_setup(struct net_device *dev)
531{
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_GSO_SOFTWARE;
538 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
539 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
540 dev->hard_header_len = 0;
541 dev->addr_len = 0;
542 dev->mtu = ETH_DATA_LEN;
543 dev->min_mtu = IPV4_MIN_MTU;
544 dev->max_mtu = IP_MAX_MTU - BAREUDP_BASE_HLEN;
545 dev->type = ARPHRD_NONE;
546 netif_keep_dst(dev);
547 dev->priv_flags |= IFF_NO_QUEUE;
548 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
549}
550
551static int bareudp_validate(struct nlattr *tb[], struct nlattr *data[],
552 struct netlink_ext_ack *extack)
553{
554 if (!data) {
555 NL_SET_ERR_MSG(extack,
556 "Not enough attributes provided to perform the operation");
557 return -EINVAL;
558 }
559 return 0;
560}
561
c46a49a4
TY
562static int bareudp2info(struct nlattr *data[], struct bareudp_conf *conf,
563 struct netlink_ext_ack *extack)
571912c6 564{
c46a49a4
TY
565 if (!data[IFLA_BAREUDP_PORT]) {
566 NL_SET_ERR_MSG(extack, "port not specified");
571912c6 567 return -EINVAL;
c46a49a4
TY
568 }
569 if (!data[IFLA_BAREUDP_ETHERTYPE]) {
570 NL_SET_ERR_MSG(extack, "ethertype not specified");
571 return -EINVAL;
572 }
571912c6
MV
573
574 if (data[IFLA_BAREUDP_PORT])
575 conf->port = nla_get_u16(data[IFLA_BAREUDP_PORT]);
576
577 if (data[IFLA_BAREUDP_ETHERTYPE])
578 conf->ethertype = nla_get_u16(data[IFLA_BAREUDP_ETHERTYPE]);
579
580 if (data[IFLA_BAREUDP_SRCPORT_MIN])
581 conf->sport_min = nla_get_u16(data[IFLA_BAREUDP_SRCPORT_MIN]);
582
583 return 0;
584}
585
586static struct bareudp_dev *bareudp_find_dev(struct bareudp_net *bn,
587 const struct bareudp_conf *conf)
588{
589 struct bareudp_dev *bareudp, *t = NULL;
590
591 list_for_each_entry(bareudp, &bn->bareudp_list, next) {
592 if (conf->port == bareudp->port)
593 t = bareudp;
594 }
595 return t;
596}
597
598static int bareudp_configure(struct net *net, struct net_device *dev,
599 struct bareudp_conf *conf)
600{
601 struct bareudp_net *bn = net_generic(net, bareudp_net_id);
602 struct bareudp_dev *t, *bareudp = netdev_priv(dev);
603 int err;
604
605 bareudp->net = net;
606 bareudp->dev = dev;
607 t = bareudp_find_dev(bn, conf);
608 if (t)
609 return -EBUSY;
610
4b5f6723
MV
611 if (conf->multi_proto_mode &&
612 (conf->ethertype != htons(ETH_P_MPLS_UC) &&
613 conf->ethertype != htons(ETH_P_IP)))
614 return -EINVAL;
615
571912c6
MV
616 bareudp->port = conf->port;
617 bareudp->ethertype = conf->ethertype;
618 bareudp->sport_min = conf->sport_min;
4b5f6723 619 bareudp->multi_proto_mode = conf->multi_proto_mode;
571912c6
MV
620 err = register_netdevice(dev);
621 if (err)
622 return err;
623
624 list_add(&bareudp->next, &bn->bareudp_list);
625 return 0;
626}
627
628static int bareudp_link_config(struct net_device *dev,
629 struct nlattr *tb[])
630{
631 int err;
632
633 if (tb[IFLA_MTU]) {
634 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
635 if (err)
636 return err;
637 }
638 return 0;
639}
640
641static int bareudp_newlink(struct net *net, struct net_device *dev,
642 struct nlattr *tb[], struct nlattr *data[],
643 struct netlink_ext_ack *extack)
644{
645 struct bareudp_conf conf;
646 int err;
647
c46a49a4 648 err = bareudp2info(data, &conf, extack);
571912c6
MV
649 if (err)
650 return err;
651
652 err = bareudp_configure(net, dev, &conf);
653 if (err)
654 return err;
655
656 err = bareudp_link_config(dev, tb);
657 if (err)
658 return err;
659
660 return 0;
661}
662
663static void bareudp_dellink(struct net_device *dev, struct list_head *head)
664{
665 struct bareudp_dev *bareudp = netdev_priv(dev);
666
667 list_del(&bareudp->next);
668 unregister_netdevice_queue(dev, head);
669}
670
671static size_t bareudp_get_size(const struct net_device *dev)
672{
673 return nla_total_size(sizeof(__be16)) + /* IFLA_BAREUDP_PORT */
674 nla_total_size(sizeof(__be16)) + /* IFLA_BAREUDP_ETHERTYPE */
675 nla_total_size(sizeof(__u16)) + /* IFLA_BAREUDP_SRCPORT_MIN */
4b5f6723 676 nla_total_size(0) + /* IFLA_BAREUDP_MULTIPROTO_MODE */
571912c6
MV
677 0;
678}
679
680static int bareudp_fill_info(struct sk_buff *skb, const struct net_device *dev)
681{
682 struct bareudp_dev *bareudp = netdev_priv(dev);
683
684 if (nla_put_be16(skb, IFLA_BAREUDP_PORT, bareudp->port))
685 goto nla_put_failure;
686 if (nla_put_be16(skb, IFLA_BAREUDP_ETHERTYPE, bareudp->ethertype))
687 goto nla_put_failure;
688 if (nla_put_u16(skb, IFLA_BAREUDP_SRCPORT_MIN, bareudp->sport_min))
689 goto nla_put_failure;
4b5f6723
MV
690 if (bareudp->multi_proto_mode &&
691 nla_put_flag(skb, IFLA_BAREUDP_MULTIPROTO_MODE))
692 goto nla_put_failure;
571912c6
MV
693
694 return 0;
695
696nla_put_failure:
697 return -EMSGSIZE;
698}
699
700static struct rtnl_link_ops bareudp_link_ops __read_mostly = {
701 .kind = "bareudp",
702 .maxtype = IFLA_BAREUDP_MAX,
703 .policy = bareudp_policy,
704 .priv_size = sizeof(struct bareudp_dev),
705 .setup = bareudp_setup,
706 .validate = bareudp_validate,
707 .newlink = bareudp_newlink,
708 .dellink = bareudp_dellink,
709 .get_size = bareudp_get_size,
710 .fill_info = bareudp_fill_info,
711};
712
713struct net_device *bareudp_dev_create(struct net *net, const char *name,
714 u8 name_assign_type,
715 struct bareudp_conf *conf)
716{
717 struct nlattr *tb[IFLA_MAX + 1];
718 struct net_device *dev;
719 LIST_HEAD(list_kill);
720 int err;
721
722 memset(tb, 0, sizeof(tb));
723 dev = rtnl_create_link(net, name, name_assign_type,
724 &bareudp_link_ops, tb, NULL);
725 if (IS_ERR(dev))
726 return dev;
727
728 err = bareudp_configure(net, dev, conf);
729 if (err) {
730 free_netdev(dev);
731 return ERR_PTR(err);
732 }
733 err = dev_set_mtu(dev, IP_MAX_MTU - BAREUDP_BASE_HLEN);
734 if (err)
735 goto err;
736
737 err = rtnl_configure_link(dev, NULL);
738 if (err < 0)
739 goto err;
740
741 return dev;
742err:
743 bareudp_dellink(dev, &list_kill);
744 unregister_netdevice_many(&list_kill);
745 return ERR_PTR(err);
746}
747EXPORT_SYMBOL_GPL(bareudp_dev_create);
748
749static __net_init int bareudp_init_net(struct net *net)
750{
751 struct bareudp_net *bn = net_generic(net, bareudp_net_id);
752
753 INIT_LIST_HEAD(&bn->bareudp_list);
754 return 0;
755}
756
757static void bareudp_destroy_tunnels(struct net *net, struct list_head *head)
758{
759 struct bareudp_net *bn = net_generic(net, bareudp_net_id);
760 struct bareudp_dev *bareudp, *next;
761
762 list_for_each_entry_safe(bareudp, next, &bn->bareudp_list, next)
763 unregister_netdevice_queue(bareudp->dev, head);
764}
765
766static void __net_exit bareudp_exit_batch_net(struct list_head *net_list)
767{
768 struct net *net;
769 LIST_HEAD(list);
770
771 rtnl_lock();
772 list_for_each_entry(net, net_list, exit_list)
773 bareudp_destroy_tunnels(net, &list);
774
775 /* unregister the devices gathered above */
776 unregister_netdevice_many(&list);
777 rtnl_unlock();
778}
779
780static struct pernet_operations bareudp_net_ops = {
781 .init = bareudp_init_net,
782 .exit_batch = bareudp_exit_batch_net,
783 .id = &bareudp_net_id,
784 .size = sizeof(struct bareudp_net),
785};
786
787static int __init bareudp_init_module(void)
788{
789 int rc;
790
791 rc = register_pernet_subsys(&bareudp_net_ops);
792 if (rc)
793 goto out1;
794
795 rc = rtnl_link_register(&bareudp_link_ops);
796 if (rc)
797 goto out2;
798
799 return 0;
800out2:
801 unregister_pernet_subsys(&bareudp_net_ops);
802out1:
803 return rc;
804}
805late_initcall(bareudp_init_module);
806
807static void __exit bareudp_cleanup_module(void)
808{
809 rtnl_link_unregister(&bareudp_link_ops);
810 unregister_pernet_subsys(&bareudp_net_ops);
811}
812module_exit(bareudp_cleanup_module);
813
eea45da4 814MODULE_ALIAS_RTNL_LINK("bareudp");
571912c6
MV
815MODULE_LICENSE("GPL");
816MODULE_AUTHOR("Martin Varghese <martin.varghese@nokia.com>");
817MODULE_DESCRIPTION("Interface driver for UDP encapsulated traffic");