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