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