xref: /linux/drivers/net/bareudp.c (revision 5832c4a7)
1571912c6SMartin Varghese // SPDX-License-Identifier: GPL-2.0
2571912c6SMartin Varghese /* Bareudp: UDP  tunnel encasulation for different Payload types like
3571912c6SMartin Varghese  * MPLS, NSH, IP, etc.
4571912c6SMartin Varghese  * Copyright (c) 2019 Nokia, Inc.
5571912c6SMartin Varghese  * Authors:  Martin Varghese, <martin.varghese@nokia.com>
6571912c6SMartin Varghese  */
7571912c6SMartin Varghese 
8571912c6SMartin Varghese #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9571912c6SMartin Varghese 
10571912c6SMartin Varghese #include <linux/kernel.h>
11571912c6SMartin Varghese #include <linux/module.h>
12571912c6SMartin Varghese #include <linux/etherdevice.h>
13571912c6SMartin Varghese #include <linux/hash.h>
14571912c6SMartin Varghese #include <net/dst_metadata.h>
15571912c6SMartin Varghese #include <net/gro_cells.h>
16571912c6SMartin Varghese #include <net/rtnetlink.h>
17571912c6SMartin Varghese #include <net/protocol.h>
18571912c6SMartin Varghese #include <net/ip6_tunnel.h>
19571912c6SMartin Varghese #include <net/ip_tunnels.h>
20571912c6SMartin Varghese #include <net/udp_tunnel.h>
21571912c6SMartin Varghese #include <net/bareudp.h>
22571912c6SMartin Varghese 
23571912c6SMartin Varghese #define BAREUDP_BASE_HLEN sizeof(struct udphdr)
24571912c6SMartin Varghese #define BAREUDP_IPV4_HLEN (sizeof(struct iphdr) + \
25571912c6SMartin Varghese 			   sizeof(struct udphdr))
26571912c6SMartin Varghese #define BAREUDP_IPV6_HLEN (sizeof(struct ipv6hdr) + \
27571912c6SMartin Varghese 			   sizeof(struct udphdr))
28571912c6SMartin Varghese 
29571912c6SMartin Varghese static bool log_ecn_error = true;
30571912c6SMartin Varghese module_param(log_ecn_error, bool, 0644);
31571912c6SMartin Varghese MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
32571912c6SMartin Varghese 
33571912c6SMartin Varghese /* per-network namespace private data for this module */
34571912c6SMartin Varghese 
35571912c6SMartin Varghese static unsigned int bareudp_net_id;
36571912c6SMartin Varghese 
37571912c6SMartin Varghese struct bareudp_net {
38571912c6SMartin Varghese 	struct list_head        bareudp_list;
39571912c6SMartin Varghese };
40571912c6SMartin Varghese 
41dcdd77eeSGuillaume Nault struct bareudp_conf {
42dcdd77eeSGuillaume Nault 	__be16 ethertype;
43dcdd77eeSGuillaume Nault 	__be16 port;
44dcdd77eeSGuillaume Nault 	u16 sport_min;
45dcdd77eeSGuillaume Nault 	bool multi_proto_mode;
46dcdd77eeSGuillaume Nault };
47dcdd77eeSGuillaume Nault 
48571912c6SMartin Varghese /* Pseudo network device */
49571912c6SMartin Varghese struct bareudp_dev {
50571912c6SMartin Varghese 	struct net         *net;        /* netns for packet i/o */
51571912c6SMartin Varghese 	struct net_device  *dev;        /* netdev for bareudp tunnel */
52571912c6SMartin Varghese 	__be16		   ethertype;
53571912c6SMartin Varghese 	__be16             port;
54571912c6SMartin Varghese 	u16	           sport_min;
554b5f6723SMartin Varghese 	bool               multi_proto_mode;
56571912c6SMartin Varghese 	struct socket      __rcu *sock;
57571912c6SMartin Varghese 	struct list_head   next;        /* bareudp node  on namespace list */
58571912c6SMartin Varghese 	struct gro_cells   gro_cells;
59571912c6SMartin Varghese };
60571912c6SMartin Varghese 
bareudp_udp_encap_recv(struct sock * sk,struct sk_buff * skb)61571912c6SMartin Varghese static int bareudp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
62571912c6SMartin Varghese {
63571912c6SMartin Varghese 	struct metadata_dst *tun_dst = NULL;
64*5832c4a7SAlexander Lobakin 	IP_TUNNEL_DECLARE_FLAGS(key) = { };
65571912c6SMartin Varghese 	struct bareudp_dev *bareudp;
66571912c6SMartin Varghese 	unsigned short family;
67571912c6SMartin Varghese 	unsigned int len;
68571912c6SMartin Varghese 	__be16 proto;
69571912c6SMartin Varghese 	void *oiph;
70571912c6SMartin Varghese 	int err;
71571912c6SMartin Varghese 
72571912c6SMartin Varghese 	bareudp = rcu_dereference_sk_user_data(sk);
73571912c6SMartin Varghese 	if (!bareudp)
74571912c6SMartin Varghese 		goto drop;
75571912c6SMartin Varghese 
76571912c6SMartin Varghese 	if (skb->protocol ==  htons(ETH_P_IP))
77571912c6SMartin Varghese 		family = AF_INET;
78571912c6SMartin Varghese 	else
79571912c6SMartin Varghese 		family = AF_INET6;
80571912c6SMartin Varghese 
814b5f6723SMartin Varghese 	if (bareudp->ethertype == htons(ETH_P_IP)) {
82143a8526SGuillaume Nault 		__u8 ipversion;
834b5f6723SMartin Varghese 
84143a8526SGuillaume Nault 		if (skb_copy_bits(skb, BAREUDP_BASE_HLEN, &ipversion,
85143a8526SGuillaume Nault 				  sizeof(ipversion))) {
86143a8526SGuillaume Nault 			bareudp->dev->stats.rx_dropped++;
87143a8526SGuillaume Nault 			goto drop;
88143a8526SGuillaume Nault 		}
89143a8526SGuillaume Nault 		ipversion >>= 4;
90143a8526SGuillaume Nault 
91143a8526SGuillaume Nault 		if (ipversion == 4) {
92143a8526SGuillaume Nault 			proto = htons(ETH_P_IP);
93143a8526SGuillaume Nault 		} else if (ipversion == 6 && bareudp->multi_proto_mode) {
944b5f6723SMartin Varghese 			proto = htons(ETH_P_IPV6);
954b5f6723SMartin Varghese 		} else {
964b5f6723SMartin Varghese 			bareudp->dev->stats.rx_dropped++;
974b5f6723SMartin Varghese 			goto drop;
984b5f6723SMartin Varghese 		}
994b5f6723SMartin Varghese 	} else if (bareudp->ethertype == htons(ETH_P_MPLS_UC)) {
1004b5f6723SMartin Varghese 		struct iphdr *tunnel_hdr;
1014b5f6723SMartin Varghese 
1024b5f6723SMartin Varghese 		tunnel_hdr = (struct iphdr *)skb_network_header(skb);
1034b5f6723SMartin Varghese 		if (tunnel_hdr->version == 4) {
1044b5f6723SMartin Varghese 			if (!ipv4_is_multicast(tunnel_hdr->daddr)) {
1054b5f6723SMartin Varghese 				proto = bareudp->ethertype;
1064b5f6723SMartin Varghese 			} else if (bareudp->multi_proto_mode &&
1074b5f6723SMartin Varghese 				   ipv4_is_multicast(tunnel_hdr->daddr)) {
1084b5f6723SMartin Varghese 				proto = htons(ETH_P_MPLS_MC);
1094b5f6723SMartin Varghese 			} else {
1104b5f6723SMartin Varghese 				bareudp->dev->stats.rx_dropped++;
1114b5f6723SMartin Varghese 				goto drop;
1124b5f6723SMartin Varghese 			}
1134b5f6723SMartin Varghese 		} else {
1144b5f6723SMartin Varghese 			int addr_type;
1154b5f6723SMartin Varghese 			struct ipv6hdr *tunnel_hdr_v6;
1164b5f6723SMartin Varghese 
1174b5f6723SMartin Varghese 			tunnel_hdr_v6 = (struct ipv6hdr *)skb_network_header(skb);
1184b5f6723SMartin Varghese 			addr_type =
1194b5f6723SMartin Varghese 			ipv6_addr_type((struct in6_addr *)&tunnel_hdr_v6->daddr);
1204b5f6723SMartin Varghese 			if (!(addr_type & IPV6_ADDR_MULTICAST)) {
1214b5f6723SMartin Varghese 				proto = bareudp->ethertype;
1224b5f6723SMartin Varghese 			} else if (bareudp->multi_proto_mode &&
1234b5f6723SMartin Varghese 				   (addr_type & IPV6_ADDR_MULTICAST)) {
1244b5f6723SMartin Varghese 				proto = htons(ETH_P_MPLS_MC);
1254b5f6723SMartin Varghese 			} else {
1264b5f6723SMartin Varghese 				bareudp->dev->stats.rx_dropped++;
1274b5f6723SMartin Varghese 				goto drop;
1284b5f6723SMartin Varghese 			}
1294b5f6723SMartin Varghese 		}
1304b5f6723SMartin Varghese 	} else {
1314b5f6723SMartin Varghese 		proto = bareudp->ethertype;
1324b5f6723SMartin Varghese 	}
133571912c6SMartin Varghese 
134571912c6SMartin Varghese 	if (iptunnel_pull_header(skb, BAREUDP_BASE_HLEN,
135571912c6SMartin Varghese 				 proto,
136571912c6SMartin Varghese 				 !net_eq(bareudp->net,
137571912c6SMartin Varghese 				 dev_net(bareudp->dev)))) {
138571912c6SMartin Varghese 		bareudp->dev->stats.rx_dropped++;
139571912c6SMartin Varghese 		goto drop;
140571912c6SMartin Varghese 	}
141*5832c4a7SAlexander Lobakin 
142*5832c4a7SAlexander Lobakin 	__set_bit(IP_TUNNEL_KEY_BIT, key);
143*5832c4a7SAlexander Lobakin 
144*5832c4a7SAlexander Lobakin 	tun_dst = udp_tun_rx_dst(skb, family, key, 0, 0);
145571912c6SMartin Varghese 	if (!tun_dst) {
146571912c6SMartin Varghese 		bareudp->dev->stats.rx_dropped++;
147571912c6SMartin Varghese 		goto drop;
148571912c6SMartin Varghese 	}
149571912c6SMartin Varghese 	skb_dst_set(skb, &tun_dst->dst);
150571912c6SMartin Varghese 	skb->dev = bareudp->dev;
151571912c6SMartin Varghese 	oiph = skb_network_header(skb);
152571912c6SMartin Varghese 	skb_reset_network_header(skb);
15399c8719bSGuillaume Nault 	skb_reset_mac_header(skb);
154571912c6SMartin Varghese 
155e077ed58SHangbin Liu 	if (!ipv6_mod_enabled() || family == AF_INET)
156571912c6SMartin Varghese 		err = IP_ECN_decapsulate(oiph, skb);
157571912c6SMartin Varghese 	else
158571912c6SMartin Varghese 		err = IP6_ECN_decapsulate(oiph, skb);
159571912c6SMartin Varghese 
160571912c6SMartin Varghese 	if (unlikely(err)) {
161571912c6SMartin Varghese 		if (log_ecn_error) {
162e077ed58SHangbin Liu 			if  (!ipv6_mod_enabled() || family == AF_INET)
163571912c6SMartin Varghese 				net_info_ratelimited("non-ECT from %pI4 "
164571912c6SMartin Varghese 						     "with TOS=%#x\n",
165571912c6SMartin Varghese 						     &((struct iphdr *)oiph)->saddr,
166571912c6SMartin Varghese 						     ((struct iphdr *)oiph)->tos);
167571912c6SMartin Varghese 			else
168571912c6SMartin Varghese 				net_info_ratelimited("non-ECT from %pI6\n",
169571912c6SMartin Varghese 						     &((struct ipv6hdr *)oiph)->saddr);
170571912c6SMartin Varghese 		}
171571912c6SMartin Varghese 		if (err > 1) {
172571912c6SMartin Varghese 			++bareudp->dev->stats.rx_frame_errors;
173571912c6SMartin Varghese 			++bareudp->dev->stats.rx_errors;
174571912c6SMartin Varghese 			goto drop;
175571912c6SMartin Varghese 		}
176571912c6SMartin Varghese 	}
177571912c6SMartin Varghese 
178571912c6SMartin Varghese 	len = skb->len;
179571912c6SMartin Varghese 	err = gro_cells_receive(&bareudp->gro_cells, skb);
1808fdfffd0SFabian Frederick 	if (likely(err == NET_RX_SUCCESS))
1818fdfffd0SFabian Frederick 		dev_sw_netstats_rx_add(bareudp->dev, len);
1828fdfffd0SFabian Frederick 
183571912c6SMartin Varghese 	return 0;
184571912c6SMartin Varghese drop:
185571912c6SMartin Varghese 	/* Consume bad packet */
186571912c6SMartin Varghese 	kfree_skb(skb);
187571912c6SMartin Varghese 
188571912c6SMartin Varghese 	return 0;
189571912c6SMartin Varghese }
190571912c6SMartin Varghese 
bareudp_err_lookup(struct sock * sk,struct sk_buff * skb)191571912c6SMartin Varghese static int bareudp_err_lookup(struct sock *sk, struct sk_buff *skb)
192571912c6SMartin Varghese {
193571912c6SMartin Varghese 	return 0;
194571912c6SMartin Varghese }
195571912c6SMartin Varghese 
bareudp_init(struct net_device * dev)196571912c6SMartin Varghese static int bareudp_init(struct net_device *dev)
197571912c6SMartin Varghese {
198571912c6SMartin Varghese 	struct bareudp_dev *bareudp = netdev_priv(dev);
199571912c6SMartin Varghese 	int err;
200571912c6SMartin Varghese 
201571912c6SMartin Varghese 	err = gro_cells_init(&bareudp->gro_cells, dev);
2026f355bbbSBreno Leitao 	if (err)
203571912c6SMartin Varghese 		return err;
2046f355bbbSBreno Leitao 
205571912c6SMartin Varghese 	return 0;
206571912c6SMartin Varghese }
207571912c6SMartin Varghese 
bareudp_uninit(struct net_device * dev)208571912c6SMartin Varghese static void bareudp_uninit(struct net_device *dev)
209571912c6SMartin Varghese {
210571912c6SMartin Varghese 	struct bareudp_dev *bareudp = netdev_priv(dev);
211571912c6SMartin Varghese 
212571912c6SMartin Varghese 	gro_cells_destroy(&bareudp->gro_cells);
213571912c6SMartin Varghese }
214571912c6SMartin Varghese 
bareudp_create_sock(struct net * net,__be16 port)215571912c6SMartin Varghese static struct socket *bareudp_create_sock(struct net *net, __be16 port)
216571912c6SMartin Varghese {
217571912c6SMartin Varghese 	struct udp_port_cfg udp_conf;
218571912c6SMartin Varghese 	struct socket *sock;
219571912c6SMartin Varghese 	int err;
220571912c6SMartin Varghese 
221571912c6SMartin Varghese 	memset(&udp_conf, 0, sizeof(udp_conf));
222e077ed58SHangbin Liu 
223e077ed58SHangbin Liu 	if (ipv6_mod_enabled())
224571912c6SMartin Varghese 		udp_conf.family = AF_INET6;
225e077ed58SHangbin Liu 	else
226571912c6SMartin Varghese 		udp_conf.family = AF_INET;
227e077ed58SHangbin Liu 
228571912c6SMartin Varghese 	udp_conf.local_udp_port = port;
229571912c6SMartin Varghese 	/* Open UDP socket */
230571912c6SMartin Varghese 	err = udp_sock_create(net, &udp_conf, &sock);
231571912c6SMartin Varghese 	if (err < 0)
232571912c6SMartin Varghese 		return ERR_PTR(err);
233571912c6SMartin Varghese 
234b03ef676SPaolo Abeni 	udp_allow_gso(sock->sk);
235571912c6SMartin Varghese 	return sock;
236571912c6SMartin Varghese }
237571912c6SMartin Varghese 
238571912c6SMartin Varghese /* Create new listen socket if needed */
bareudp_socket_create(struct bareudp_dev * bareudp,__be16 port)239571912c6SMartin Varghese static int bareudp_socket_create(struct bareudp_dev *bareudp, __be16 port)
240571912c6SMartin Varghese {
241571912c6SMartin Varghese 	struct udp_tunnel_sock_cfg tunnel_cfg;
242571912c6SMartin Varghese 	struct socket *sock;
243571912c6SMartin Varghese 
244571912c6SMartin Varghese 	sock = bareudp_create_sock(bareudp->net, port);
245571912c6SMartin Varghese 	if (IS_ERR(sock))
246571912c6SMartin Varghese 		return PTR_ERR(sock);
247571912c6SMartin Varghese 
248571912c6SMartin Varghese 	/* Mark socket as an encapsulation socket */
249571912c6SMartin Varghese 	memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
250571912c6SMartin Varghese 	tunnel_cfg.sk_user_data = bareudp;
251571912c6SMartin Varghese 	tunnel_cfg.encap_type = 1;
252571912c6SMartin Varghese 	tunnel_cfg.encap_rcv = bareudp_udp_encap_recv;
253571912c6SMartin Varghese 	tunnel_cfg.encap_err_lookup = bareudp_err_lookup;
254571912c6SMartin Varghese 	tunnel_cfg.encap_destroy = NULL;
255571912c6SMartin Varghese 	setup_udp_tunnel_sock(bareudp->net, sock, &tunnel_cfg);
256571912c6SMartin Varghese 
257571912c6SMartin Varghese 	rcu_assign_pointer(bareudp->sock, sock);
258571912c6SMartin Varghese 	return 0;
259571912c6SMartin Varghese }
260571912c6SMartin Varghese 
bareudp_open(struct net_device * dev)261571912c6SMartin Varghese static int bareudp_open(struct net_device *dev)
262571912c6SMartin Varghese {
263571912c6SMartin Varghese 	struct bareudp_dev *bareudp = netdev_priv(dev);
264571912c6SMartin Varghese 	int ret = 0;
265571912c6SMartin Varghese 
266571912c6SMartin Varghese 	ret =  bareudp_socket_create(bareudp, bareudp->port);
267571912c6SMartin Varghese 	return ret;
268571912c6SMartin Varghese }
269571912c6SMartin Varghese 
bareudp_sock_release(struct bareudp_dev * bareudp)270571912c6SMartin Varghese static void bareudp_sock_release(struct bareudp_dev *bareudp)
271571912c6SMartin Varghese {
272571912c6SMartin Varghese 	struct socket *sock;
273571912c6SMartin Varghese 
274571912c6SMartin Varghese 	sock = bareudp->sock;
275571912c6SMartin Varghese 	rcu_assign_pointer(bareudp->sock, NULL);
276571912c6SMartin Varghese 	synchronize_net();
277571912c6SMartin Varghese 	udp_tunnel_sock_release(sock);
278571912c6SMartin Varghese }
279571912c6SMartin Varghese 
bareudp_stop(struct net_device * dev)280571912c6SMartin Varghese static int bareudp_stop(struct net_device *dev)
281571912c6SMartin Varghese {
282571912c6SMartin Varghese 	struct bareudp_dev *bareudp = netdev_priv(dev);
283571912c6SMartin Varghese 
284571912c6SMartin Varghese 	bareudp_sock_release(bareudp);
285571912c6SMartin Varghese 	return 0;
286571912c6SMartin Varghese }
287571912c6SMartin Varghese 
bareudp_xmit_skb(struct sk_buff * skb,struct net_device * dev,struct bareudp_dev * bareudp,const struct ip_tunnel_info * info)288571912c6SMartin Varghese static int bareudp_xmit_skb(struct sk_buff *skb, struct net_device *dev,
289571912c6SMartin Varghese 			    struct bareudp_dev *bareudp,
290571912c6SMartin Varghese 			    const struct ip_tunnel_info *info)
291571912c6SMartin Varghese {
292*5832c4a7SAlexander Lobakin 	bool udp_sum = test_bit(IP_TUNNEL_CSUM_BIT, info->key.tun_flags);
293571912c6SMartin Varghese 	bool xnet = !net_eq(bareudp->net, dev_net(bareudp->dev));
294571912c6SMartin Varghese 	bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
295571912c6SMartin Varghese 	struct socket *sock = rcu_dereference(bareudp->sock);
296571912c6SMartin Varghese 	const struct ip_tunnel_key *key = &info->key;
297571912c6SMartin Varghese 	struct rtable *rt;
298571912c6SMartin Varghese 	__be16 sport, df;
299571912c6SMartin Varghese 	int min_headroom;
300571912c6SMartin Varghese 	__u8 tos, ttl;
301571912c6SMartin Varghese 	__be32 saddr;
302571912c6SMartin Varghese 	int err;
303571912c6SMartin Varghese 
304571912c6SMartin Varghese 	if (!sock)
305571912c6SMartin Varghese 		return -ESHUTDOWN;
306571912c6SMartin Varghese 
307ef113733SBeniamino Galvani 	sport = udp_flow_src_port(bareudp->net, skb,
308ef113733SBeniamino Galvani 				  bareudp->sport_min, USHRT_MAX,
309ef113733SBeniamino Galvani 				  true);
31072fc68c6SBeniamino Galvani 	rt = udp_tunnel_dst_lookup(skb, dev, bareudp->net, 0, &saddr, &info->key,
311ef113733SBeniamino Galvani 				   sport, bareudp->port, key->tos,
31272fc68c6SBeniamino Galvani 				   use_cache ?
31372fc68c6SBeniamino Galvani 				   (struct dst_cache *)&info->dst_cache : NULL);
314571912c6SMartin Varghese 
315571912c6SMartin Varghese 	if (IS_ERR(rt))
316571912c6SMartin Varghese 		return PTR_ERR(rt);
317571912c6SMartin Varghese 
318571912c6SMartin Varghese 	skb_tunnel_check_pmtu(skb, &rt->dst,
3194cb47a86SStefano Brivio 			      BAREUDP_IPV4_HLEN + info->options_len, false);
320571912c6SMartin Varghese 
321571912c6SMartin Varghese 	tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
322571912c6SMartin Varghese 	ttl = key->ttl;
323*5832c4a7SAlexander Lobakin 	df = test_bit(IP_TUNNEL_DONT_FRAGMENT_BIT, key->tun_flags) ?
324*5832c4a7SAlexander Lobakin 	     htons(IP_DF) : 0;
325571912c6SMartin Varghese 	skb_scrub_packet(skb, xnet);
326571912c6SMartin Varghese 
327c102b6fdSDavid S. Miller 	err = -ENOSPC;
328571912c6SMartin Varghese 	if (!skb_pull(skb, skb_network_offset(skb)))
329571912c6SMartin Varghese 		goto free_dst;
330571912c6SMartin Varghese 
331571912c6SMartin Varghese 	min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len +
332571912c6SMartin Varghese 		BAREUDP_BASE_HLEN + info->options_len + sizeof(struct iphdr);
333571912c6SMartin Varghese 
334571912c6SMartin Varghese 	err = skb_cow_head(skb, min_headroom);
335571912c6SMartin Varghese 	if (unlikely(err))
336571912c6SMartin Varghese 		goto free_dst;
337571912c6SMartin Varghese 
338571912c6SMartin Varghese 	err = udp_tunnel_handle_offloads(skb, udp_sum);
339571912c6SMartin Varghese 	if (err)
340571912c6SMartin Varghese 		goto free_dst;
341571912c6SMartin Varghese 
342571912c6SMartin Varghese 	skb_set_inner_protocol(skb, bareudp->ethertype);
343571912c6SMartin Varghese 	udp_tunnel_xmit_skb(rt, sock->sk, skb, saddr, info->key.u.ipv4.dst,
344571912c6SMartin Varghese 			    tos, ttl, df, sport, bareudp->port,
345571912c6SMartin Varghese 			    !net_eq(bareudp->net, dev_net(bareudp->dev)),
346*5832c4a7SAlexander Lobakin 			    !test_bit(IP_TUNNEL_CSUM_BIT,
347*5832c4a7SAlexander Lobakin 				      info->key.tun_flags));
348571912c6SMartin Varghese 	return 0;
349571912c6SMartin Varghese 
350571912c6SMartin Varghese free_dst:
351571912c6SMartin Varghese 	dst_release(&rt->dst);
352571912c6SMartin Varghese 	return err;
353571912c6SMartin Varghese }
354571912c6SMartin Varghese 
bareudp6_xmit_skb(struct sk_buff * skb,struct net_device * dev,struct bareudp_dev * bareudp,const struct ip_tunnel_info * info)355571912c6SMartin Varghese static int bareudp6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
356571912c6SMartin Varghese 			     struct bareudp_dev *bareudp,
357571912c6SMartin Varghese 			     const struct ip_tunnel_info *info)
358571912c6SMartin Varghese {
359*5832c4a7SAlexander Lobakin 	bool udp_sum = test_bit(IP_TUNNEL_CSUM_BIT, info->key.tun_flags);
360571912c6SMartin Varghese 	bool xnet = !net_eq(bareudp->net, dev_net(bareudp->dev));
361571912c6SMartin Varghese 	bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
362571912c6SMartin Varghese 	struct socket *sock  = rcu_dereference(bareudp->sock);
363571912c6SMartin Varghese 	const struct ip_tunnel_key *key = &info->key;
364571912c6SMartin Varghese 	struct dst_entry *dst = NULL;
365571912c6SMartin Varghese 	struct in6_addr saddr, daddr;
366571912c6SMartin Varghese 	int min_headroom;
367571912c6SMartin Varghese 	__u8 prio, ttl;
368571912c6SMartin Varghese 	__be16 sport;
369571912c6SMartin Varghese 	int err;
370571912c6SMartin Varghese 
371571912c6SMartin Varghese 	if (!sock)
372571912c6SMartin Varghese 		return -ESHUTDOWN;
373571912c6SMartin Varghese 
374ef113733SBeniamino Galvani 	sport = udp_flow_src_port(bareudp->net, skb,
375ef113733SBeniamino Galvani 				  bareudp->sport_min, USHRT_MAX,
376ef113733SBeniamino Galvani 				  true);
377946fcfdbSBeniamino Galvani 	dst = udp_tunnel6_dst_lookup(skb, dev, bareudp->net, sock, 0, &saddr,
378ef113733SBeniamino Galvani 				     key, sport, bareudp->port, key->tos,
379946fcfdbSBeniamino Galvani 				     use_cache ?
380946fcfdbSBeniamino Galvani 				     (struct dst_cache *) &info->dst_cache : NULL);
381571912c6SMartin Varghese 	if (IS_ERR(dst))
382571912c6SMartin Varghese 		return PTR_ERR(dst);
383571912c6SMartin Varghese 
3844cb47a86SStefano Brivio 	skb_tunnel_check_pmtu(skb, dst, BAREUDP_IPV6_HLEN + info->options_len,
3854cb47a86SStefano Brivio 			      false);
386571912c6SMartin Varghese 
387571912c6SMartin Varghese 	prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
388571912c6SMartin Varghese 	ttl = key->ttl;
389571912c6SMartin Varghese 
390571912c6SMartin Varghese 	skb_scrub_packet(skb, xnet);
391571912c6SMartin Varghese 
392c102b6fdSDavid S. Miller 	err = -ENOSPC;
393571912c6SMartin Varghese 	if (!skb_pull(skb, skb_network_offset(skb)))
394571912c6SMartin Varghese 		goto free_dst;
395571912c6SMartin Varghese 
396571912c6SMartin Varghese 	min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len +
39710ad3e99STaehee Yoo 		BAREUDP_BASE_HLEN + info->options_len + sizeof(struct ipv6hdr);
398571912c6SMartin Varghese 
399571912c6SMartin Varghese 	err = skb_cow_head(skb, min_headroom);
400571912c6SMartin Varghese 	if (unlikely(err))
401571912c6SMartin Varghese 		goto free_dst;
402571912c6SMartin Varghese 
403571912c6SMartin Varghese 	err = udp_tunnel_handle_offloads(skb, udp_sum);
404571912c6SMartin Varghese 	if (err)
405571912c6SMartin Varghese 		goto free_dst;
406571912c6SMartin Varghese 
407571912c6SMartin Varghese 	daddr = info->key.u.ipv6.dst;
408571912c6SMartin Varghese 	udp_tunnel6_xmit_skb(dst, sock->sk, skb, dev,
409571912c6SMartin Varghese 			     &saddr, &daddr, prio, ttl,
410571912c6SMartin Varghese 			     info->key.label, sport, bareudp->port,
411*5832c4a7SAlexander Lobakin 			     !test_bit(IP_TUNNEL_CSUM_BIT,
412*5832c4a7SAlexander Lobakin 				       info->key.tun_flags));
413571912c6SMartin Varghese 	return 0;
414571912c6SMartin Varghese 
415571912c6SMartin Varghese free_dst:
416571912c6SMartin Varghese 	dst_release(dst);
417571912c6SMartin Varghese 	return err;
418571912c6SMartin Varghese }
419571912c6SMartin Varghese 
bareudp_proto_valid(struct bareudp_dev * bareudp,__be16 proto)420302d201bSGuillaume Nault static bool bareudp_proto_valid(struct bareudp_dev *bareudp, __be16 proto)
421302d201bSGuillaume Nault {
422302d201bSGuillaume Nault 	if (bareudp->ethertype == proto)
423302d201bSGuillaume Nault 		return true;
424302d201bSGuillaume Nault 
425302d201bSGuillaume Nault 	if (!bareudp->multi_proto_mode)
426302d201bSGuillaume Nault 		return false;
427302d201bSGuillaume Nault 
428302d201bSGuillaume Nault 	if (bareudp->ethertype == htons(ETH_P_MPLS_UC) &&
429302d201bSGuillaume Nault 	    proto == htons(ETH_P_MPLS_MC))
430302d201bSGuillaume Nault 		return true;
431302d201bSGuillaume Nault 
432302d201bSGuillaume Nault 	if (bareudp->ethertype == htons(ETH_P_IP) &&
433302d201bSGuillaume Nault 	    proto == htons(ETH_P_IPV6))
434302d201bSGuillaume Nault 		return true;
435302d201bSGuillaume Nault 
436302d201bSGuillaume Nault 	return false;
437302d201bSGuillaume Nault }
438302d201bSGuillaume Nault 
bareudp_xmit(struct sk_buff * skb,struct net_device * dev)439571912c6SMartin Varghese static netdev_tx_t bareudp_xmit(struct sk_buff *skb, struct net_device *dev)
440571912c6SMartin Varghese {
441571912c6SMartin Varghese 	struct bareudp_dev *bareudp = netdev_priv(dev);
442571912c6SMartin Varghese 	struct ip_tunnel_info *info = NULL;
443571912c6SMartin Varghese 	int err;
444571912c6SMartin Varghese 
445302d201bSGuillaume Nault 	if (!bareudp_proto_valid(bareudp, skb->protocol)) {
446571912c6SMartin Varghese 		err = -EINVAL;
447571912c6SMartin Varghese 		goto tx_error;
448571912c6SMartin Varghese 	}
449571912c6SMartin Varghese 
450571912c6SMartin Varghese 	info = skb_tunnel_info(skb);
451571912c6SMartin Varghese 	if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
452571912c6SMartin Varghese 		err = -EINVAL;
453571912c6SMartin Varghese 		goto tx_error;
454571912c6SMartin Varghese 	}
455571912c6SMartin Varghese 
456571912c6SMartin Varghese 	rcu_read_lock();
457e077ed58SHangbin Liu 	if (ipv6_mod_enabled() && info->mode & IP_TUNNEL_INFO_IPV6)
458571912c6SMartin Varghese 		err = bareudp6_xmit_skb(skb, dev, bareudp, info);
459571912c6SMartin Varghese 	else
460571912c6SMartin Varghese 		err = bareudp_xmit_skb(skb, dev, bareudp, info);
461571912c6SMartin Varghese 
462571912c6SMartin Varghese 	rcu_read_unlock();
463571912c6SMartin Varghese 
464571912c6SMartin Varghese 	if (likely(!err))
465571912c6SMartin Varghese 		return NETDEV_TX_OK;
466571912c6SMartin Varghese tx_error:
467571912c6SMartin Varghese 	dev_kfree_skb(skb);
468571912c6SMartin Varghese 
469571912c6SMartin Varghese 	if (err == -ELOOP)
470571912c6SMartin Varghese 		dev->stats.collisions++;
471571912c6SMartin Varghese 	else if (err == -ENETUNREACH)
472571912c6SMartin Varghese 		dev->stats.tx_carrier_errors++;
473571912c6SMartin Varghese 
474571912c6SMartin Varghese 	dev->stats.tx_errors++;
475571912c6SMartin Varghese 	return NETDEV_TX_OK;
476571912c6SMartin Varghese }
477571912c6SMartin Varghese 
bareudp_fill_metadata_dst(struct net_device * dev,struct sk_buff * skb)478571912c6SMartin Varghese static int bareudp_fill_metadata_dst(struct net_device *dev,
479571912c6SMartin Varghese 				     struct sk_buff *skb)
480571912c6SMartin Varghese {
481571912c6SMartin Varghese 	struct ip_tunnel_info *info = skb_tunnel_info(skb);
482571912c6SMartin Varghese 	struct bareudp_dev *bareudp = netdev_priv(dev);
483571912c6SMartin Varghese 	bool use_cache;
484ef113733SBeniamino Galvani 	__be16 sport;
485571912c6SMartin Varghese 
486571912c6SMartin Varghese 	use_cache = ip_tunnel_dst_cache_usable(skb, info);
487ef113733SBeniamino Galvani 	sport = udp_flow_src_port(bareudp->net, skb,
488ef113733SBeniamino Galvani 				  bareudp->sport_min, USHRT_MAX,
489ef113733SBeniamino Galvani 				  true);
490571912c6SMartin Varghese 
491e077ed58SHangbin Liu 	if (!ipv6_mod_enabled() || ip_tunnel_info_af(info) == AF_INET) {
492571912c6SMartin Varghese 		struct rtable *rt;
493571912c6SMartin Varghese 		__be32 saddr;
494571912c6SMartin Varghese 
49572fc68c6SBeniamino Galvani 		rt = udp_tunnel_dst_lookup(skb, dev, bareudp->net, 0, &saddr,
496ef113733SBeniamino Galvani 					   &info->key, sport, bareudp->port,
497ef113733SBeniamino Galvani 					   info->key.tos,
49872fc68c6SBeniamino Galvani 					   use_cache ? &info->dst_cache : NULL);
499571912c6SMartin Varghese 		if (IS_ERR(rt))
500571912c6SMartin Varghese 			return PTR_ERR(rt);
501571912c6SMartin Varghese 
502571912c6SMartin Varghese 		ip_rt_put(rt);
503571912c6SMartin Varghese 		info->key.u.ipv4.src = saddr;
504571912c6SMartin Varghese 	} else if (ip_tunnel_info_af(info) == AF_INET6) {
505571912c6SMartin Varghese 		struct dst_entry *dst;
506571912c6SMartin Varghese 		struct in6_addr saddr;
507571912c6SMartin Varghese 		struct socket *sock = rcu_dereference(bareudp->sock);
508571912c6SMartin Varghese 
509fc47e86dSBeniamino Galvani 		dst = udp_tunnel6_dst_lookup(skb, dev, bareudp->net, sock,
510946fcfdbSBeniamino Galvani 					     0, &saddr, &info->key,
511ef113733SBeniamino Galvani 					     sport, bareudp->port, info->key.tos,
512946fcfdbSBeniamino Galvani 					     use_cache ? &info->dst_cache : NULL);
513571912c6SMartin Varghese 		if (IS_ERR(dst))
514571912c6SMartin Varghese 			return PTR_ERR(dst);
515571912c6SMartin Varghese 
516571912c6SMartin Varghese 		dst_release(dst);
517571912c6SMartin Varghese 		info->key.u.ipv6.src = saddr;
518571912c6SMartin Varghese 	} else {
519571912c6SMartin Varghese 		return -EINVAL;
520571912c6SMartin Varghese 	}
521571912c6SMartin Varghese 
522ef113733SBeniamino Galvani 	info->key.tp_src = sport;
523571912c6SMartin Varghese 	info->key.tp_dst = bareudp->port;
524571912c6SMartin Varghese 	return 0;
525571912c6SMartin Varghese }
526571912c6SMartin Varghese 
527571912c6SMartin Varghese static const struct net_device_ops bareudp_netdev_ops = {
528571912c6SMartin Varghese 	.ndo_init               = bareudp_init,
529571912c6SMartin Varghese 	.ndo_uninit             = bareudp_uninit,
530571912c6SMartin Varghese 	.ndo_open               = bareudp_open,
531571912c6SMartin Varghese 	.ndo_stop               = bareudp_stop,
532571912c6SMartin Varghese 	.ndo_start_xmit         = bareudp_xmit,
533571912c6SMartin Varghese 	.ndo_fill_metadata_dst  = bareudp_fill_metadata_dst,
534571912c6SMartin Varghese };
535571912c6SMartin Varghese 
536571912c6SMartin Varghese static const struct nla_policy bareudp_policy[IFLA_BAREUDP_MAX + 1] = {
537571912c6SMartin Varghese 	[IFLA_BAREUDP_PORT]                = { .type = NLA_U16 },
538571912c6SMartin Varghese 	[IFLA_BAREUDP_ETHERTYPE]	   = { .type = NLA_U16 },
539571912c6SMartin Varghese 	[IFLA_BAREUDP_SRCPORT_MIN]         = { .type = NLA_U16 },
5404b5f6723SMartin Varghese 	[IFLA_BAREUDP_MULTIPROTO_MODE]     = { .type = NLA_FLAG },
541571912c6SMartin Varghese };
542571912c6SMartin Varghese 
543571912c6SMartin Varghese /* Info for udev, that this is a virtual tunnel endpoint */
544cec85994SJonas Bonn static const struct device_type bareudp_type = {
545571912c6SMartin Varghese 	.name = "bareudp",
546571912c6SMartin Varghese };
547571912c6SMartin Varghese 
548571912c6SMartin Varghese /* Initialize the device structure. */
bareudp_setup(struct net_device * dev)549571912c6SMartin Varghese static void bareudp_setup(struct net_device *dev)
550571912c6SMartin Varghese {
551571912c6SMartin Varghese 	dev->netdev_ops = &bareudp_netdev_ops;
552571912c6SMartin Varghese 	dev->needs_free_netdev = true;
553571912c6SMartin Varghese 	SET_NETDEV_DEVTYPE(dev, &bareudp_type);
5543224dcfdSXin Long 	dev->features    |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST;
555571912c6SMartin Varghese 	dev->features    |= NETIF_F_RXCSUM;
556d9e44981STaehee Yoo 	dev->features    |= NETIF_F_LLTX;
557571912c6SMartin Varghese 	dev->features    |= NETIF_F_GSO_SOFTWARE;
5583224dcfdSXin Long 	dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST;
5593224dcfdSXin Long 	dev->hw_features |= NETIF_F_RXCSUM;
560571912c6SMartin Varghese 	dev->hw_features |= NETIF_F_GSO_SOFTWARE;
561571912c6SMartin Varghese 	dev->hard_header_len = 0;
562571912c6SMartin Varghese 	dev->addr_len = 0;
563571912c6SMartin Varghese 	dev->mtu = ETH_DATA_LEN;
564571912c6SMartin Varghese 	dev->min_mtu = IPV4_MIN_MTU;
565571912c6SMartin Varghese 	dev->max_mtu = IP_MAX_MTU - BAREUDP_BASE_HLEN;
566571912c6SMartin Varghese 	dev->type = ARPHRD_NONE;
567571912c6SMartin Varghese 	netif_keep_dst(dev);
568571912c6SMartin Varghese 	dev->priv_flags |= IFF_NO_QUEUE;
569571912c6SMartin Varghese 	dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
5706f355bbbSBreno Leitao 	dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
571571912c6SMartin Varghese }
572571912c6SMartin Varghese 
bareudp_validate(struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)573571912c6SMartin Varghese static int bareudp_validate(struct nlattr *tb[], struct nlattr *data[],
574571912c6SMartin Varghese 			    struct netlink_ext_ack *extack)
575571912c6SMartin Varghese {
576571912c6SMartin Varghese 	if (!data) {
577571912c6SMartin Varghese 		NL_SET_ERR_MSG(extack,
578571912c6SMartin Varghese 			       "Not enough attributes provided to perform the operation");
579571912c6SMartin Varghese 		return -EINVAL;
580571912c6SMartin Varghese 	}
581571912c6SMartin Varghese 	return 0;
582571912c6SMartin Varghese }
583571912c6SMartin Varghese 
bareudp2info(struct nlattr * data[],struct bareudp_conf * conf,struct netlink_ext_ack * extack)584c46a49a4STaehee Yoo static int bareudp2info(struct nlattr *data[], struct bareudp_conf *conf,
585c46a49a4STaehee Yoo 			struct netlink_ext_ack *extack)
586571912c6SMartin Varghese {
587b15bb881SMartin 	memset(conf, 0, sizeof(*conf));
588b15bb881SMartin 
589c46a49a4STaehee Yoo 	if (!data[IFLA_BAREUDP_PORT]) {
590c46a49a4STaehee Yoo 		NL_SET_ERR_MSG(extack, "port not specified");
591571912c6SMartin Varghese 		return -EINVAL;
592c46a49a4STaehee Yoo 	}
593c46a49a4STaehee Yoo 	if (!data[IFLA_BAREUDP_ETHERTYPE]) {
594c46a49a4STaehee Yoo 		NL_SET_ERR_MSG(extack, "ethertype not specified");
595c46a49a4STaehee Yoo 		return -EINVAL;
596c46a49a4STaehee Yoo 	}
597571912c6SMartin Varghese 
598571912c6SMartin Varghese 	conf->port = nla_get_u16(data[IFLA_BAREUDP_PORT]);
599571912c6SMartin Varghese 	conf->ethertype = nla_get_u16(data[IFLA_BAREUDP_ETHERTYPE]);
600571912c6SMartin Varghese 
601571912c6SMartin Varghese 	if (data[IFLA_BAREUDP_SRCPORT_MIN])
602571912c6SMartin Varghese 		conf->sport_min =  nla_get_u16(data[IFLA_BAREUDP_SRCPORT_MIN]);
603571912c6SMartin Varghese 
6044c98045cSMartin 	if (data[IFLA_BAREUDP_MULTIPROTO_MODE])
6054c98045cSMartin 		conf->multi_proto_mode = true;
6064c98045cSMartin 
607571912c6SMartin Varghese 	return 0;
608571912c6SMartin Varghese }
609571912c6SMartin Varghese 
bareudp_find_dev(struct bareudp_net * bn,const struct bareudp_conf * conf)610571912c6SMartin Varghese static struct bareudp_dev *bareudp_find_dev(struct bareudp_net *bn,
611571912c6SMartin Varghese 					    const struct bareudp_conf *conf)
612571912c6SMartin Varghese {
613571912c6SMartin Varghese 	struct bareudp_dev *bareudp, *t = NULL;
614571912c6SMartin Varghese 
615571912c6SMartin Varghese 	list_for_each_entry(bareudp, &bn->bareudp_list, next) {
616571912c6SMartin Varghese 		if (conf->port == bareudp->port)
617571912c6SMartin Varghese 			t = bareudp;
618571912c6SMartin Varghese 	}
619571912c6SMartin Varghese 	return t;
620571912c6SMartin Varghese }
621571912c6SMartin Varghese 
bareudp_configure(struct net * net,struct net_device * dev,struct bareudp_conf * conf,struct netlink_ext_ack * extack)622571912c6SMartin Varghese static int bareudp_configure(struct net *net, struct net_device *dev,
623b4bffa4cSGuillaume Nault 			     struct bareudp_conf *conf,
624b4bffa4cSGuillaume Nault 			     struct netlink_ext_ack *extack)
625571912c6SMartin Varghese {
626571912c6SMartin Varghese 	struct bareudp_net *bn = net_generic(net, bareudp_net_id);
627571912c6SMartin Varghese 	struct bareudp_dev *t, *bareudp = netdev_priv(dev);
628571912c6SMartin Varghese 	int err;
629571912c6SMartin Varghese 
630571912c6SMartin Varghese 	bareudp->net = net;
631571912c6SMartin Varghese 	bareudp->dev = dev;
632571912c6SMartin Varghese 	t = bareudp_find_dev(bn, conf);
633b4bffa4cSGuillaume Nault 	if (t) {
634b4bffa4cSGuillaume Nault 		NL_SET_ERR_MSG(extack, "Another bareudp device using the same port already exists");
635571912c6SMartin Varghese 		return -EBUSY;
636b4bffa4cSGuillaume Nault 	}
637571912c6SMartin Varghese 
6384b5f6723SMartin Varghese 	if (conf->multi_proto_mode &&
6394b5f6723SMartin Varghese 	    (conf->ethertype != htons(ETH_P_MPLS_UC) &&
640b4bffa4cSGuillaume Nault 	     conf->ethertype != htons(ETH_P_IP))) {
641b4bffa4cSGuillaume Nault 		NL_SET_ERR_MSG(extack, "Cannot set multiproto mode for this ethertype (only IPv4 and unicast MPLS are supported)");
6424b5f6723SMartin Varghese 		return -EINVAL;
643b4bffa4cSGuillaume Nault 	}
6444b5f6723SMartin Varghese 
645571912c6SMartin Varghese 	bareudp->port = conf->port;
646571912c6SMartin Varghese 	bareudp->ethertype = conf->ethertype;
647571912c6SMartin Varghese 	bareudp->sport_min = conf->sport_min;
6484b5f6723SMartin Varghese 	bareudp->multi_proto_mode = conf->multi_proto_mode;
649fe80536aSMartin 
650571912c6SMartin Varghese 	err = register_netdevice(dev);
651571912c6SMartin Varghese 	if (err)
652571912c6SMartin Varghese 		return err;
653571912c6SMartin Varghese 
654571912c6SMartin Varghese 	list_add(&bareudp->next, &bn->bareudp_list);
655571912c6SMartin Varghese 	return 0;
656571912c6SMartin Varghese }
657571912c6SMartin Varghese 
bareudp_link_config(struct net_device * dev,struct nlattr * tb[])658571912c6SMartin Varghese static int bareudp_link_config(struct net_device *dev,
659571912c6SMartin Varghese 			       struct nlattr *tb[])
660571912c6SMartin Varghese {
661571912c6SMartin Varghese 	int err;
662571912c6SMartin Varghese 
663571912c6SMartin Varghese 	if (tb[IFLA_MTU]) {
664571912c6SMartin Varghese 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
665571912c6SMartin Varghese 		if (err)
666571912c6SMartin Varghese 			return err;
667571912c6SMartin Varghese 	}
668571912c6SMartin Varghese 	return 0;
669571912c6SMartin Varghese }
670571912c6SMartin Varghese 
bareudp_dellink(struct net_device * dev,struct list_head * head)67194bcfdbfSJakub Kicinski static void bareudp_dellink(struct net_device *dev, struct list_head *head)
67294bcfdbfSJakub Kicinski {
67394bcfdbfSJakub Kicinski 	struct bareudp_dev *bareudp = netdev_priv(dev);
67494bcfdbfSJakub Kicinski 
67594bcfdbfSJakub Kicinski 	list_del(&bareudp->next);
67694bcfdbfSJakub Kicinski 	unregister_netdevice_queue(dev, head);
67794bcfdbfSJakub Kicinski }
67894bcfdbfSJakub Kicinski 
bareudp_newlink(struct net * net,struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)679571912c6SMartin Varghese static int bareudp_newlink(struct net *net, struct net_device *dev,
680571912c6SMartin Varghese 			   struct nlattr *tb[], struct nlattr *data[],
681571912c6SMartin Varghese 			   struct netlink_ext_ack *extack)
682571912c6SMartin Varghese {
683571912c6SMartin Varghese 	struct bareudp_conf conf;
684571912c6SMartin Varghese 	int err;
685571912c6SMartin Varghese 
686c46a49a4STaehee Yoo 	err = bareudp2info(data, &conf, extack);
687571912c6SMartin Varghese 	if (err)
688571912c6SMartin Varghese 		return err;
689571912c6SMartin Varghese 
690b4bffa4cSGuillaume Nault 	err = bareudp_configure(net, dev, &conf, extack);
691571912c6SMartin Varghese 	if (err)
692571912c6SMartin Varghese 		return err;
693571912c6SMartin Varghese 
694571912c6SMartin Varghese 	err = bareudp_link_config(dev, tb);
695571912c6SMartin Varghese 	if (err)
69694bcfdbfSJakub Kicinski 		goto err_unconfig;
697571912c6SMartin Varghese 
698571912c6SMartin Varghese 	return 0;
699571912c6SMartin Varghese 
70094bcfdbfSJakub Kicinski err_unconfig:
7011d04ccb9SJakub Kicinski 	bareudp_dellink(dev, NULL);
70294bcfdbfSJakub Kicinski 	return err;
703571912c6SMartin Varghese }
704571912c6SMartin Varghese 
bareudp_get_size(const struct net_device * dev)705571912c6SMartin Varghese static size_t bareudp_get_size(const struct net_device *dev)
706571912c6SMartin Varghese {
707571912c6SMartin Varghese 	return  nla_total_size(sizeof(__be16)) +  /* IFLA_BAREUDP_PORT */
708571912c6SMartin Varghese 		nla_total_size(sizeof(__be16)) +  /* IFLA_BAREUDP_ETHERTYPE */
709571912c6SMartin Varghese 		nla_total_size(sizeof(__u16))  +  /* IFLA_BAREUDP_SRCPORT_MIN */
7104b5f6723SMartin Varghese 		nla_total_size(0)              +  /* IFLA_BAREUDP_MULTIPROTO_MODE */
711571912c6SMartin Varghese 		0;
712571912c6SMartin Varghese }
713571912c6SMartin Varghese 
bareudp_fill_info(struct sk_buff * skb,const struct net_device * dev)714571912c6SMartin Varghese static int bareudp_fill_info(struct sk_buff *skb, const struct net_device *dev)
715571912c6SMartin Varghese {
716571912c6SMartin Varghese 	struct bareudp_dev *bareudp = netdev_priv(dev);
717571912c6SMartin Varghese 
718571912c6SMartin Varghese 	if (nla_put_be16(skb, IFLA_BAREUDP_PORT, bareudp->port))
719571912c6SMartin Varghese 		goto nla_put_failure;
720571912c6SMartin Varghese 	if (nla_put_be16(skb, IFLA_BAREUDP_ETHERTYPE, bareudp->ethertype))
721571912c6SMartin Varghese 		goto nla_put_failure;
722571912c6SMartin Varghese 	if (nla_put_u16(skb, IFLA_BAREUDP_SRCPORT_MIN, bareudp->sport_min))
723571912c6SMartin Varghese 		goto nla_put_failure;
7244b5f6723SMartin Varghese 	if (bareudp->multi_proto_mode &&
7254b5f6723SMartin Varghese 	    nla_put_flag(skb, IFLA_BAREUDP_MULTIPROTO_MODE))
7264b5f6723SMartin Varghese 		goto nla_put_failure;
727571912c6SMartin Varghese 
728571912c6SMartin Varghese 	return 0;
729571912c6SMartin Varghese 
730571912c6SMartin Varghese nla_put_failure:
731571912c6SMartin Varghese 	return -EMSGSIZE;
732571912c6SMartin Varghese }
733571912c6SMartin Varghese 
734571912c6SMartin Varghese static struct rtnl_link_ops bareudp_link_ops __read_mostly = {
735571912c6SMartin Varghese 	.kind           = "bareudp",
736571912c6SMartin Varghese 	.maxtype        = IFLA_BAREUDP_MAX,
737571912c6SMartin Varghese 	.policy         = bareudp_policy,
738571912c6SMartin Varghese 	.priv_size      = sizeof(struct bareudp_dev),
739571912c6SMartin Varghese 	.setup          = bareudp_setup,
740571912c6SMartin Varghese 	.validate       = bareudp_validate,
741571912c6SMartin Varghese 	.newlink        = bareudp_newlink,
742571912c6SMartin Varghese 	.dellink        = bareudp_dellink,
743571912c6SMartin Varghese 	.get_size       = bareudp_get_size,
744571912c6SMartin Varghese 	.fill_info      = bareudp_fill_info,
745571912c6SMartin Varghese };
746571912c6SMartin Varghese 
bareudp_init_net(struct net * net)747571912c6SMartin Varghese static __net_init int bareudp_init_net(struct net *net)
748571912c6SMartin Varghese {
749571912c6SMartin Varghese 	struct bareudp_net *bn = net_generic(net, bareudp_net_id);
750571912c6SMartin Varghese 
751571912c6SMartin Varghese 	INIT_LIST_HEAD(&bn->bareudp_list);
752571912c6SMartin Varghese 	return 0;
753571912c6SMartin Varghese }
754571912c6SMartin Varghese 
bareudp_destroy_tunnels(struct net * net,struct list_head * head)755571912c6SMartin Varghese static void bareudp_destroy_tunnels(struct net *net, struct list_head *head)
756571912c6SMartin Varghese {
757571912c6SMartin Varghese 	struct bareudp_net *bn = net_generic(net, bareudp_net_id);
758571912c6SMartin Varghese 	struct bareudp_dev *bareudp, *next;
759571912c6SMartin Varghese 
760571912c6SMartin Varghese 	list_for_each_entry_safe(bareudp, next, &bn->bareudp_list, next)
761571912c6SMartin Varghese 		unregister_netdevice_queue(bareudp->dev, head);
762571912c6SMartin Varghese }
763571912c6SMartin Varghese 
bareudp_exit_batch_rtnl(struct list_head * net_list,struct list_head * dev_kill_list)764422b5ae9SEric Dumazet static void __net_exit bareudp_exit_batch_rtnl(struct list_head *net_list,
765422b5ae9SEric Dumazet 					       struct list_head *dev_kill_list)
766571912c6SMartin Varghese {
767571912c6SMartin Varghese 	struct net *net;
768571912c6SMartin Varghese 
769571912c6SMartin Varghese 	list_for_each_entry(net, net_list, exit_list)
770422b5ae9SEric Dumazet 		bareudp_destroy_tunnels(net, dev_kill_list);
771571912c6SMartin Varghese }
772571912c6SMartin Varghese 
773571912c6SMartin Varghese static struct pernet_operations bareudp_net_ops = {
774571912c6SMartin Varghese 	.init = bareudp_init_net,
775422b5ae9SEric Dumazet 	.exit_batch_rtnl = bareudp_exit_batch_rtnl,
776571912c6SMartin Varghese 	.id   = &bareudp_net_id,
777571912c6SMartin Varghese 	.size = sizeof(struct bareudp_net),
778571912c6SMartin Varghese };
779571912c6SMartin Varghese 
bareudp_init_module(void)780571912c6SMartin Varghese static int __init bareudp_init_module(void)
781571912c6SMartin Varghese {
782571912c6SMartin Varghese 	int rc;
783571912c6SMartin Varghese 
784571912c6SMartin Varghese 	rc = register_pernet_subsys(&bareudp_net_ops);
785571912c6SMartin Varghese 	if (rc)
786571912c6SMartin Varghese 		goto out1;
787571912c6SMartin Varghese 
788571912c6SMartin Varghese 	rc = rtnl_link_register(&bareudp_link_ops);
789571912c6SMartin Varghese 	if (rc)
790571912c6SMartin Varghese 		goto out2;
791571912c6SMartin Varghese 
792571912c6SMartin Varghese 	return 0;
793571912c6SMartin Varghese out2:
794571912c6SMartin Varghese 	unregister_pernet_subsys(&bareudp_net_ops);
795571912c6SMartin Varghese out1:
796571912c6SMartin Varghese 	return rc;
797571912c6SMartin Varghese }
798571912c6SMartin Varghese late_initcall(bareudp_init_module);
799571912c6SMartin Varghese 
bareudp_cleanup_module(void)800571912c6SMartin Varghese static void __exit bareudp_cleanup_module(void)
801571912c6SMartin Varghese {
802571912c6SMartin Varghese 	rtnl_link_unregister(&bareudp_link_ops);
803571912c6SMartin Varghese 	unregister_pernet_subsys(&bareudp_net_ops);
804571912c6SMartin Varghese }
805571912c6SMartin Varghese module_exit(bareudp_cleanup_module);
806571912c6SMartin Varghese 
807eea45da4STaehee Yoo MODULE_ALIAS_RTNL_LINK("bareudp");
808571912c6SMartin Varghese MODULE_LICENSE("GPL");
809571912c6SMartin Varghese MODULE_AUTHOR("Martin Varghese <martin.varghese@nokia.com>");
810571912c6SMartin Varghese MODULE_DESCRIPTION("Interface driver for UDP encapsulated traffic");
811