Linux Audio

Check our new training course

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