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