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