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