xref: /openbmc/linux/drivers/net/bareudp.c (revision 8fc4e4aa)
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 /* Pseudo network device */
42 struct 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;
48 	bool               multi_proto_mode;
49 	struct socket      __rcu *sock;
50 	struct list_head   next;        /* bareudp node  on namespace list */
51 	struct gro_cells   gro_cells;
52 };
53 
54 static int bareudp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
55 {
56 	struct metadata_dst *tun_dst = NULL;
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 
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 	}
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 	}
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;
131 	}
132 	skb_dst_set(skb, &tun_dst->dst);
133 	skb->dev = bareudp->dev;
134 	oiph = skb_network_header(skb);
135 	skb_reset_network_header(skb);
136 
137 	if (!IS_ENABLED(CONFIG_IPV6) || family == AF_INET)
138 		err = IP_ECN_decapsulate(oiph, skb);
139 	else
140 		err = IP6_ECN_decapsulate(oiph, skb);
141 
142 	if (unlikely(err)) {
143 		if (log_ecn_error) {
144 			if  (!IS_ENABLED(CONFIG_IPV6) || family == AF_INET)
145 				net_info_ratelimited("non-ECT from %pI4 "
146 						     "with TOS=%#x\n",
147 						     &((struct iphdr *)oiph)->saddr,
148 						     ((struct iphdr *)oiph)->tos);
149 			else
150 				net_info_ratelimited("non-ECT from %pI6\n",
151 						     &((struct ipv6hdr *)oiph)->saddr);
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);
162 	if (likely(err == NET_RX_SUCCESS))
163 		dev_sw_netstats_rx_add(bareudp->dev, len);
164 
165 	return 0;
166 drop:
167 	/* Consume bad packet */
168 	kfree_skb(skb);
169 
170 	return 0;
171 }
172 
173 static int bareudp_err_lookup(struct sock *sk, struct sk_buff *skb)
174 {
175 	return 0;
176 }
177 
178 static 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 
195 static 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 
203 static 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 
221 	udp_allow_gso(sock->sk);
222 	return sock;
223 }
224 
225 /* Create new listen socket if needed */
226 static 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 
244 	rcu_assign_pointer(bareudp->sock, sock);
245 	return 0;
246 }
247 
248 static 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 
257 static 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 
267 static 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 
275 static 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,
301 			      BAREUDP_IPV4_HLEN + info->options_len, false);
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 
311 	err = -ENOSPC;
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 
333 free_dst:
334 	dst_release(&rt->dst);
335 	return err;
336 }
337 
338 static 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 
362 	skb_tunnel_check_pmtu(skb, dst, BAREUDP_IPV6_HLEN + info->options_len,
363 			      false);
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 
373 	err = -ENOSPC;
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 +
378 		BAREUDP_BASE_HLEN + info->options_len + sizeof(struct ipv6hdr);
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 
395 free_dst:
396 	dst_release(dst);
397 	return err;
398 }
399 
400 static 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 
419 static 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 
425 	if (!bareudp_proto_valid(bareudp, skb->protocol)) {
426 		err = -EINVAL;
427 		goto tx_error;
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();
437 	if (IS_ENABLED(CONFIG_IPV6) && info->mode & IP_TUNNEL_INFO_IPV6)
438 		err = bareudp6_xmit_skb(skb, dev, bareudp, info);
439 	else
440 		err = bareudp_xmit_skb(skb, dev, bareudp, info);
441 
442 	rcu_read_unlock();
443 
444 	if (likely(!err))
445 		return NETDEV_TX_OK;
446 tx_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 
458 static 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 
467 	if (!IS_ENABLED(CONFIG_IPV6) || ip_tunnel_info_af(info) == AF_INET) {
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;
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;
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 
502 static 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,
508 	.ndo_get_stats64        = dev_get_tstats64,
509 	.ndo_fill_metadata_dst  = bareudp_fill_metadata_dst,
510 };
511 
512 static 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 },
516 	[IFLA_BAREUDP_MULTIPROTO_MODE]     = { .type = NLA_FLAG },
517 };
518 
519 /* Info for udev, that this is a virtual tunnel endpoint */
520 static const struct device_type bareudp_type = {
521 	.name = "bareudp",
522 };
523 
524 /* Initialize the device structure. */
525 static 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);
530 	dev->features    |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST;
531 	dev->features    |= NETIF_F_RXCSUM;
532 	dev->features    |= NETIF_F_LLTX;
533 	dev->features    |= NETIF_F_GSO_SOFTWARE;
534 	dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST;
535 	dev->hw_features |= NETIF_F_RXCSUM;
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 
548 static 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 
559 static int bareudp2info(struct nlattr *data[], struct bareudp_conf *conf,
560 			struct netlink_ext_ack *extack)
561 {
562 	memset(conf, 0, sizeof(*conf));
563 
564 	if (!data[IFLA_BAREUDP_PORT]) {
565 		NL_SET_ERR_MSG(extack, "port not specified");
566 		return -EINVAL;
567 	}
568 	if (!data[IFLA_BAREUDP_ETHERTYPE]) {
569 		NL_SET_ERR_MSG(extack, "ethertype not specified");
570 		return -EINVAL;
571 	}
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 
582 	if (data[IFLA_BAREUDP_MULTIPROTO_MODE])
583 		conf->multi_proto_mode = true;
584 
585 	return 0;
586 }
587 
588 static 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 
600 static 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 
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 
618 	bareudp->port = conf->port;
619 	bareudp->ethertype = conf->ethertype;
620 	bareudp->sport_min = conf->sport_min;
621 	bareudp->multi_proto_mode = conf->multi_proto_mode;
622 
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 
631 static 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 
644 static 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 
652 static 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 
659 	err = bareudp2info(data, &conf, extack);
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)
669 		goto err_unconfig;
670 
671 	return 0;
672 
673 err_unconfig:
674 	bareudp_dellink(dev, NULL);
675 	return err;
676 }
677 
678 static 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 */
683 		nla_total_size(0)              +  /* IFLA_BAREUDP_MULTIPROTO_MODE */
684 		0;
685 }
686 
687 static 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;
697 	if (bareudp->multi_proto_mode &&
698 	    nla_put_flag(skb, IFLA_BAREUDP_MULTIPROTO_MODE))
699 		goto nla_put_failure;
700 
701 	return 0;
702 
703 nla_put_failure:
704 	return -EMSGSIZE;
705 }
706 
707 static 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 
720 struct 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;
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;
748 err:
749 	bareudp_dellink(dev, NULL);
750 	return ERR_PTR(err);
751 }
752 EXPORT_SYMBOL_GPL(bareudp_dev_create);
753 
754 static __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 
762 static 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 
771 static 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 
785 static 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 
792 static 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;
805 out2:
806 	unregister_pernet_subsys(&bareudp_net_ops);
807 out1:
808 	return rc;
809 }
810 late_initcall(bareudp_init_module);
811 
812 static void __exit bareudp_cleanup_module(void)
813 {
814 	rtnl_link_unregister(&bareudp_link_ops);
815 	unregister_pernet_subsys(&bareudp_net_ops);
816 }
817 module_exit(bareudp_cleanup_module);
818 
819 MODULE_ALIAS_RTNL_LINK("bareudp");
820 MODULE_LICENSE("GPL");
821 MODULE_AUTHOR("Martin Varghese <martin.varghese@nokia.com>");
822 MODULE_DESCRIPTION("Interface driver for UDP encapsulated traffic");
823