xref: /linux/drivers/net/gtp.c (revision ef6af7bdb9e6c14eae8dc5fe852aefe1e089c85c)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* GTP according to GSM TS 09.60 / 3GPP TS 29.060
3  *
4  * (C) 2012-2014 by sysmocom - s.f.m.c. GmbH
5  * (C) 2016 by Pablo Neira Ayuso <pablo@netfilter.org>
6  *
7  * Author: Harald Welte <hwelte@sysmocom.de>
8  *	   Pablo Neira Ayuso <pablo@netfilter.org>
9  *	   Andreas Schultz <aschultz@travelping.com>
10  */
11 
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 
14 #include <linux/module.h>
15 #include <linux/skbuff.h>
16 #include <linux/udp.h>
17 #include <linux/rculist.h>
18 #include <linux/jhash.h>
19 #include <linux/if_tunnel.h>
20 #include <linux/net.h>
21 #include <linux/file.h>
22 #include <linux/gtp.h>
23 
24 #include <net/dst_metadata.h>
25 #include <net/net_namespace.h>
26 #include <net/protocol.h>
27 #include <net/ip.h>
28 #include <net/udp.h>
29 #include <net/udp_tunnel.h>
30 #include <net/icmp.h>
31 #include <net/xfrm.h>
32 #include <net/genetlink.h>
33 #include <net/netns/generic.h>
34 #include <net/gtp.h>
35 
36 /* An active session for the subscriber. */
37 struct pdp_ctx {
38 	struct hlist_node	hlist_tid;
39 	struct hlist_node	hlist_addr;
40 
41 	union {
42 		struct {
43 			u64	tid;
44 			u16	flow;
45 		} v0;
46 		struct {
47 			u32	i_tei;
48 			u32	o_tei;
49 		} v1;
50 	} u;
51 	u8			gtp_version;
52 	u16			af;
53 
54 	struct in_addr		ms_addr_ip4;
55 	struct in_addr		peer_addr_ip4;
56 
57 	struct sock		*sk;
58 	struct net_device       *dev;
59 
60 	atomic_t		tx_seq;
61 	struct rcu_head		rcu_head;
62 };
63 
64 /* One instance of the GTP device. */
65 struct gtp_dev {
66 	struct list_head	list;
67 
68 	struct sock		*sk0;
69 	struct sock		*sk1u;
70 
71 	struct net_device	*dev;
72 
73 	unsigned int		role;
74 	unsigned int		hash_size;
75 	struct hlist_head	*tid_hash;
76 	struct hlist_head	*addr_hash;
77 	/* Used by LWT tunnel. */
78 	bool			collect_md;
79 	struct socket		*collect_md_sock;
80 };
81 
82 static unsigned int gtp_net_id __read_mostly;
83 
84 struct gtp_net {
85 	struct list_head gtp_dev_list;
86 };
87 
88 static u32 gtp_h_initval;
89 
90 static void pdp_context_delete(struct pdp_ctx *pctx);
91 
92 static inline u32 gtp0_hashfn(u64 tid)
93 {
94 	u32 *tid32 = (u32 *) &tid;
95 	return jhash_2words(tid32[0], tid32[1], gtp_h_initval);
96 }
97 
98 static inline u32 gtp1u_hashfn(u32 tid)
99 {
100 	return jhash_1word(tid, gtp_h_initval);
101 }
102 
103 static inline u32 ipv4_hashfn(__be32 ip)
104 {
105 	return jhash_1word((__force u32)ip, gtp_h_initval);
106 }
107 
108 /* Resolve a PDP context structure based on the 64bit TID. */
109 static struct pdp_ctx *gtp0_pdp_find(struct gtp_dev *gtp, u64 tid)
110 {
111 	struct hlist_head *head;
112 	struct pdp_ctx *pdp;
113 
114 	head = &gtp->tid_hash[gtp0_hashfn(tid) % gtp->hash_size];
115 
116 	hlist_for_each_entry_rcu(pdp, head, hlist_tid) {
117 		if (pdp->gtp_version == GTP_V0 &&
118 		    pdp->u.v0.tid == tid)
119 			return pdp;
120 	}
121 	return NULL;
122 }
123 
124 /* Resolve a PDP context structure based on the 32bit TEI. */
125 static struct pdp_ctx *gtp1_pdp_find(struct gtp_dev *gtp, u32 tid)
126 {
127 	struct hlist_head *head;
128 	struct pdp_ctx *pdp;
129 
130 	head = &gtp->tid_hash[gtp1u_hashfn(tid) % gtp->hash_size];
131 
132 	hlist_for_each_entry_rcu(pdp, head, hlist_tid) {
133 		if (pdp->gtp_version == GTP_V1 &&
134 		    pdp->u.v1.i_tei == tid)
135 			return pdp;
136 	}
137 	return NULL;
138 }
139 
140 /* Resolve a PDP context based on IPv4 address of MS. */
141 static struct pdp_ctx *ipv4_pdp_find(struct gtp_dev *gtp, __be32 ms_addr)
142 {
143 	struct hlist_head *head;
144 	struct pdp_ctx *pdp;
145 
146 	head = &gtp->addr_hash[ipv4_hashfn(ms_addr) % gtp->hash_size];
147 
148 	hlist_for_each_entry_rcu(pdp, head, hlist_addr) {
149 		if (pdp->af == AF_INET &&
150 		    pdp->ms_addr_ip4.s_addr == ms_addr)
151 			return pdp;
152 	}
153 
154 	return NULL;
155 }
156 
157 static bool gtp_check_ms_ipv4(struct sk_buff *skb, struct pdp_ctx *pctx,
158 				  unsigned int hdrlen, unsigned int role)
159 {
160 	struct iphdr *iph;
161 
162 	if (!pskb_may_pull(skb, hdrlen + sizeof(struct iphdr)))
163 		return false;
164 
165 	iph = (struct iphdr *)(skb->data + hdrlen);
166 
167 	if (role == GTP_ROLE_SGSN)
168 		return iph->daddr == pctx->ms_addr_ip4.s_addr;
169 	else
170 		return iph->saddr == pctx->ms_addr_ip4.s_addr;
171 }
172 
173 /* Check if the inner IP address in this packet is assigned to any
174  * existing mobile subscriber.
175  */
176 static bool gtp_check_ms(struct sk_buff *skb, struct pdp_ctx *pctx,
177 			     unsigned int hdrlen, unsigned int role)
178 {
179 	switch (ntohs(skb->protocol)) {
180 	case ETH_P_IP:
181 		return gtp_check_ms_ipv4(skb, pctx, hdrlen, role);
182 	}
183 	return false;
184 }
185 
186 static int gtp_set_tun_dst(struct gtp_dev *gtp, struct sk_buff *skb,
187 			   unsigned int hdrlen, u8 gtp_version,
188 			   __be64 tid, u8 flags)
189 {
190 	struct metadata_dst *tun_dst;
191 	int opts_len = 0;
192 
193 	if (unlikely(flags & GTP1_F_MASK))
194 		opts_len = sizeof(struct gtpu_metadata);
195 
196 	tun_dst = udp_tun_rx_dst(skb, gtp->sk1u->sk_family, TUNNEL_KEY, tid, opts_len);
197 	if (!tun_dst) {
198 		netdev_dbg(gtp->dev, "Failed to allocate tun_dst");
199 		goto err;
200 	}
201 
202 	netdev_dbg(gtp->dev, "attaching metadata_dst to skb, gtp ver %d hdrlen %d\n",
203 		   gtp_version, hdrlen);
204 	if (unlikely(opts_len)) {
205 		struct gtpu_metadata *opts;
206 		struct gtp1_header *gtp1;
207 
208 		opts = ip_tunnel_info_opts(&tun_dst->u.tun_info);
209 		gtp1 = (struct gtp1_header *)(skb->data + sizeof(struct udphdr));
210 		opts->ver = GTP_METADATA_V1;
211 		opts->flags = gtp1->flags;
212 		opts->type = gtp1->type;
213 		netdev_dbg(gtp->dev, "recved control pkt: flag %x type: %d\n",
214 			   opts->flags, opts->type);
215 		tun_dst->u.tun_info.key.tun_flags |= TUNNEL_GTPU_OPT;
216 		tun_dst->u.tun_info.options_len = opts_len;
217 		skb->protocol = htons(0xffff);         /* Unknown */
218 	}
219 	/* Get rid of the GTP + UDP headers. */
220 	if (iptunnel_pull_header(skb, hdrlen, skb->protocol,
221 				 !net_eq(sock_net(gtp->sk1u), dev_net(gtp->dev)))) {
222 		gtp->dev->stats.rx_length_errors++;
223 		goto err;
224 	}
225 
226 	skb_dst_set(skb, &tun_dst->dst);
227 	return 0;
228 err:
229 	return -1;
230 }
231 
232 static int gtp_rx(struct gtp_dev *gtp, struct sk_buff *skb,
233 		  unsigned int hdrlen, u8 gtp_version, unsigned int role,
234 		  __be64 tid, u8 flags, u8 type)
235 {
236 	if (ip_tunnel_collect_metadata() || gtp->collect_md) {
237 		int err;
238 
239 		err = gtp_set_tun_dst(gtp, skb, hdrlen, gtp_version, tid, flags);
240 		if (err)
241 			goto err;
242 	} else {
243 		struct pdp_ctx *pctx;
244 
245 		if (flags & GTP1_F_MASK)
246 			hdrlen += 4;
247 
248 		if (type != GTP_TPDU)
249 			return 1;
250 
251 		if (gtp_version == GTP_V0)
252 			pctx = gtp0_pdp_find(gtp, be64_to_cpu(tid));
253 		else
254 			pctx = gtp1_pdp_find(gtp, be64_to_cpu(tid));
255 		if (!pctx) {
256 			netdev_dbg(gtp->dev, "No PDP ctx to decap skb=%p\n", skb);
257 			return 1;
258 		}
259 
260 		if (!gtp_check_ms(skb, pctx, hdrlen, role)) {
261 			netdev_dbg(pctx->dev, "No PDP ctx for this MS\n");
262 			return 1;
263 		}
264 		/* Get rid of the GTP + UDP headers. */
265 		if (iptunnel_pull_header(skb, hdrlen, skb->protocol,
266 					 !net_eq(sock_net(pctx->sk), dev_net(gtp->dev)))) {
267 			gtp->dev->stats.rx_length_errors++;
268 			goto err;
269 		}
270 	}
271 	netdev_dbg(gtp->dev, "forwarding packet from GGSN to uplink\n");
272 
273 	/* Now that the UDP and the GTP header have been removed, set up the
274 	 * new network header. This is required by the upper layer to
275 	 * calculate the transport header.
276 	 */
277 	skb_reset_network_header(skb);
278 	if (pskb_may_pull(skb, sizeof(struct iphdr))) {
279 		struct iphdr *iph;
280 
281 		iph = ip_hdr(skb);
282 		if (iph->version == 4) {
283 			netdev_dbg(gtp->dev, "inner pkt: ipv4");
284 			skb->protocol = htons(ETH_P_IP);
285 		} else if (iph->version == 6) {
286 			netdev_dbg(gtp->dev, "inner pkt: ipv6");
287 			skb->protocol = htons(ETH_P_IPV6);
288 		} else {
289 			netdev_dbg(gtp->dev, "inner pkt error: Unknown type");
290 		}
291 	}
292 
293 	skb->dev = gtp->dev;
294 	dev_sw_netstats_rx_add(gtp->dev, skb->len);
295 	netif_rx(skb);
296 	return 0;
297 
298 err:
299 	gtp->dev->stats.rx_dropped++;
300 	return -1;
301 }
302 
303 /* 1 means pass up to the stack, -1 means drop and 0 means decapsulated. */
304 static int gtp0_udp_encap_recv(struct gtp_dev *gtp, struct sk_buff *skb)
305 {
306 	unsigned int hdrlen = sizeof(struct udphdr) +
307 			      sizeof(struct gtp0_header);
308 	struct gtp0_header *gtp0;
309 
310 	if (!pskb_may_pull(skb, hdrlen))
311 		return -1;
312 
313 	gtp0 = (struct gtp0_header *)(skb->data + sizeof(struct udphdr));
314 
315 	if ((gtp0->flags >> 5) != GTP_V0)
316 		return 1;
317 
318 	return gtp_rx(gtp, skb, hdrlen, GTP_V0, gtp->role, gtp0->tid, gtp0->flags, gtp0->type);
319 }
320 
321 static int gtp1u_udp_encap_recv(struct gtp_dev *gtp, struct sk_buff *skb)
322 {
323 	unsigned int hdrlen = sizeof(struct udphdr) +
324 			      sizeof(struct gtp1_header);
325 	struct gtp1_header *gtp1;
326 
327 	if (!pskb_may_pull(skb, hdrlen))
328 		return -1;
329 
330 	gtp1 = (struct gtp1_header *)(skb->data + sizeof(struct udphdr));
331 
332 	netdev_dbg(gtp->dev, "GTPv1 recv: flags %x\n", gtp1->flags);
333 	if ((gtp1->flags >> 5) != GTP_V1)
334 		return 1;
335 
336 	/* From 29.060: "This field shall be present if and only if any one or
337 	 * more of the S, PN and E flags are set.".
338 	 *
339 	 * If any of the bit is set, then the remaining ones also have to be
340 	 * set.
341 	 */
342 	/* Make sure the header is larger enough, including extensions. */
343 	if (!pskb_may_pull(skb, hdrlen))
344 		return -1;
345 
346 	gtp1 = (struct gtp1_header *)(skb->data + sizeof(struct udphdr));
347 
348 	return gtp_rx(gtp, skb, hdrlen, GTP_V1, gtp->role,
349 		      key32_to_tunnel_id(gtp1->tid), gtp1->flags, gtp1->type);
350 }
351 
352 static void __gtp_encap_destroy(struct sock *sk)
353 {
354 	struct gtp_dev *gtp;
355 
356 	lock_sock(sk);
357 	gtp = sk->sk_user_data;
358 	if (gtp) {
359 		if (gtp->sk0 == sk)
360 			gtp->sk0 = NULL;
361 		else
362 			gtp->sk1u = NULL;
363 		udp_sk(sk)->encap_type = 0;
364 		rcu_assign_sk_user_data(sk, NULL);
365 		sock_put(sk);
366 	}
367 	release_sock(sk);
368 }
369 
370 static void gtp_encap_destroy(struct sock *sk)
371 {
372 	rtnl_lock();
373 	__gtp_encap_destroy(sk);
374 	rtnl_unlock();
375 }
376 
377 static void gtp_encap_disable_sock(struct sock *sk)
378 {
379 	if (!sk)
380 		return;
381 
382 	__gtp_encap_destroy(sk);
383 }
384 
385 static void gtp_encap_disable(struct gtp_dev *gtp)
386 {
387 	gtp_encap_disable_sock(gtp->sk0);
388 	gtp_encap_disable_sock(gtp->sk1u);
389 	if (gtp->collect_md_sock) {
390 		udp_tunnel_sock_release(gtp->collect_md_sock);
391 		gtp->collect_md_sock = NULL;
392 		netdev_dbg(gtp->dev, "GTP socket released.\n");
393 	}
394 }
395 
396 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
397  * Return codes: 0: success, <0: error, >0: pass up to userspace UDP socket.
398  */
399 static int gtp_encap_recv(struct sock *sk, struct sk_buff *skb)
400 {
401 	struct gtp_dev *gtp;
402 	int ret = 0;
403 
404 	gtp = rcu_dereference_sk_user_data(sk);
405 	if (!gtp)
406 		return 1;
407 
408 	netdev_dbg(gtp->dev, "encap_recv sk=%p type %d\n",
409 		   sk, udp_sk(sk)->encap_type);
410 
411 	switch (udp_sk(sk)->encap_type) {
412 	case UDP_ENCAP_GTP0:
413 		netdev_dbg(gtp->dev, "received GTP0 packet\n");
414 		ret = gtp0_udp_encap_recv(gtp, skb);
415 		break;
416 	case UDP_ENCAP_GTP1U:
417 		netdev_dbg(gtp->dev, "received GTP1U packet\n");
418 		ret = gtp1u_udp_encap_recv(gtp, skb);
419 		break;
420 	default:
421 		ret = -1; /* Shouldn't happen. */
422 	}
423 
424 	switch (ret) {
425 	case 1:
426 		netdev_dbg(gtp->dev, "pass up to the process\n");
427 		break;
428 	case 0:
429 		break;
430 	case -1:
431 		netdev_dbg(gtp->dev, "GTP packet has been dropped\n");
432 		kfree_skb(skb);
433 		ret = 0;
434 		break;
435 	}
436 
437 	return ret;
438 }
439 
440 static int gtp_dev_init(struct net_device *dev)
441 {
442 	struct gtp_dev *gtp = netdev_priv(dev);
443 
444 	gtp->dev = dev;
445 
446 	dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
447 	if (!dev->tstats)
448 		return -ENOMEM;
449 
450 	return 0;
451 }
452 
453 static void gtp_dev_uninit(struct net_device *dev)
454 {
455 	struct gtp_dev *gtp = netdev_priv(dev);
456 
457 	gtp_encap_disable(gtp);
458 	free_percpu(dev->tstats);
459 }
460 
461 static struct rtable *ip4_route_output_gtp(struct flowi4 *fl4,
462 					   const struct sock *sk,
463 					   __be32 daddr,
464 					   __be32 saddr)
465 {
466 	memset(fl4, 0, sizeof(*fl4));
467 	fl4->flowi4_oif		= sk->sk_bound_dev_if;
468 	fl4->daddr		= daddr;
469 	fl4->saddr		= saddr;
470 	fl4->flowi4_tos		= RT_CONN_FLAGS(sk);
471 	fl4->flowi4_proto	= sk->sk_protocol;
472 
473 	return ip_route_output_key(sock_net(sk), fl4);
474 }
475 
476 static inline void gtp0_push_header(struct sk_buff *skb, struct pdp_ctx *pctx)
477 {
478 	int payload_len = skb->len;
479 	struct gtp0_header *gtp0;
480 
481 	gtp0 = skb_push(skb, sizeof(*gtp0));
482 
483 	gtp0->flags	= 0x1e; /* v0, GTP-non-prime. */
484 	gtp0->type	= GTP_TPDU;
485 	gtp0->length	= htons(payload_len);
486 	gtp0->seq	= htons((atomic_inc_return(&pctx->tx_seq) - 1) % 0xffff);
487 	gtp0->flow	= htons(pctx->u.v0.flow);
488 	gtp0->number	= 0xff;
489 	gtp0->spare[0]	= gtp0->spare[1] = gtp0->spare[2] = 0xff;
490 	gtp0->tid	= cpu_to_be64(pctx->u.v0.tid);
491 }
492 
493 static inline void gtp1_push_header(struct sk_buff *skb, __be32 tid)
494 {
495 	int payload_len = skb->len;
496 	struct gtp1_header *gtp1;
497 
498 	gtp1 = skb_push(skb, sizeof(*gtp1));
499 
500 	/* Bits    8  7  6  5  4  3  2	1
501 	 *	  +--+--+--+--+--+--+--+--+
502 	 *	  |version |PT| 0| E| S|PN|
503 	 *	  +--+--+--+--+--+--+--+--+
504 	 *	    0  0  1  1	1  0  0  0
505 	 */
506 	gtp1->flags	= 0x30; /* v1, GTP-non-prime. */
507 	gtp1->type	= GTP_TPDU;
508 	gtp1->length	= htons(payload_len);
509 	gtp1->tid	= tid;
510 
511 	/* TODO: Suppport for extension header, sequence number and N-PDU.
512 	 *	 Update the length field if any of them is available.
513 	 */
514 }
515 
516 static inline int gtp1_push_control_header(struct sk_buff *skb,
517 					   __be32 tid,
518 					   struct gtpu_metadata *opts,
519 					   struct net_device *dev)
520 {
521 	struct gtp1_header *gtp1c;
522 	int payload_len;
523 
524 	if (opts->ver != GTP_METADATA_V1)
525 		return -ENOENT;
526 
527 	if (opts->type == 0xFE) {
528 		/* for end marker ignore skb data. */
529 		netdev_dbg(dev, "xmit pkt with null data");
530 		pskb_trim(skb, 0);
531 	}
532 	if (skb_cow_head(skb, sizeof(*gtp1c)) < 0)
533 		return -ENOMEM;
534 
535 	payload_len = skb->len;
536 
537 	gtp1c = skb_push(skb, sizeof(*gtp1c));
538 
539 	gtp1c->flags	= opts->flags;
540 	gtp1c->type	= opts->type;
541 	gtp1c->length	= htons(payload_len);
542 	gtp1c->tid	= tid;
543 	netdev_dbg(dev, "xmit control pkt: ver %d flags %x type %x pkt len %d tid %x",
544 		   opts->ver, opts->flags, opts->type, skb->len, tid);
545 	return 0;
546 }
547 
548 struct gtp_pktinfo {
549 	struct sock             *sk;
550 	__u8                    tos;
551 	struct flowi4           fl4;
552 	struct rtable           *rt;
553 	struct net_device       *dev;
554 	__be16                  gtph_port;
555 };
556 
557 static inline void gtp_set_pktinfo_ipv4(struct gtp_pktinfo *pktinfo,
558 					struct sock *sk,
559 					__u8 tos,
560 					struct rtable *rt,
561 					struct flowi4 *fl4,
562 					struct net_device *dev)
563 {
564 	pktinfo->sk	= sk;
565 	pktinfo->tos    = tos;
566 	pktinfo->rt	= rt;
567 	pktinfo->fl4	= *fl4;
568 	pktinfo->dev	= dev;
569 }
570 
571 static int gtp_build_skb_ip4(struct sk_buff *skb, struct net_device *dev,
572 			     struct gtp_pktinfo *pktinfo)
573 {
574 	struct gtp_dev *gtp = netdev_priv(dev);
575 	struct gtpu_metadata *opts = NULL;
576 	struct sock *sk = NULL;
577 	struct pdp_ctx *pctx;
578 	struct rtable *rt;
579 	struct flowi4 fl4;
580 	u8 gtp_version;
581 	__be16 df = 0;
582 	__be32 tun_id;
583 	__be32 daddr;
584 	__be32 saddr;
585 	__u8 tos;
586 	int mtu;
587 
588 	if (gtp->collect_md) {
589 		/* LWT GTP1U encap */
590 		struct ip_tunnel_info *info = NULL;
591 
592 		info = skb_tunnel_info(skb);
593 		if (!info) {
594 			netdev_dbg(dev, "missing tunnel info");
595 			return -ENOENT;
596 		}
597 		if (info->key.tp_dst && ntohs(info->key.tp_dst) != GTP1U_PORT) {
598 			netdev_dbg(dev, "unexpected GTP dst port: %d", ntohs(info->key.tp_dst));
599 			return -EOPNOTSUPP;
600 		}
601 		pctx = NULL;
602 		gtp_version = GTP_V1;
603 		tun_id = tunnel_id_to_key32(info->key.tun_id);
604 		daddr = info->key.u.ipv4.dst;
605 		saddr = info->key.u.ipv4.src;
606 		sk = gtp->sk1u;
607 		if (!sk) {
608 			netdev_dbg(dev, "missing tunnel sock");
609 			return -EOPNOTSUPP;
610 		}
611 		tos = info->key.tos;
612 		if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT)
613 			df = htons(IP_DF);
614 
615 		if (info->options_len != 0) {
616 			if (info->key.tun_flags & TUNNEL_GTPU_OPT) {
617 				opts = ip_tunnel_info_opts(info);
618 			} else {
619 				netdev_dbg(dev, "missing tunnel metadata for control pkt");
620 				return -EOPNOTSUPP;
621 			}
622 		}
623 		netdev_dbg(dev, "flow-based GTP1U encap: tunnel id %d\n",
624 			   be32_to_cpu(tun_id));
625 	} else {
626 		struct iphdr *iph;
627 
628 		if (ntohs(skb->protocol) != ETH_P_IP)
629 			return -EOPNOTSUPP;
630 
631 		iph = ip_hdr(skb);
632 
633 		/* Read the IP destination address and resolve the PDP context.
634 		 * Prepend PDP header with TEI/TID from PDP ctx.
635 		 */
636 		if (gtp->role == GTP_ROLE_SGSN)
637 			pctx = ipv4_pdp_find(gtp, iph->saddr);
638 		else
639 			pctx = ipv4_pdp_find(gtp, iph->daddr);
640 
641 		if (!pctx) {
642 			netdev_dbg(dev, "no PDP ctx found for %pI4, skip\n",
643 				   &iph->daddr);
644 			return -ENOENT;
645 		}
646 		sk = pctx->sk;
647 		netdev_dbg(dev, "found PDP context %p\n", pctx);
648 
649 		gtp_version = pctx->gtp_version;
650 		tun_id  = htonl(pctx->u.v1.o_tei);
651 		daddr = pctx->peer_addr_ip4.s_addr;
652 		saddr = inet_sk(sk)->inet_saddr;
653 		tos = iph->tos;
654 		df = iph->frag_off;
655 		netdev_dbg(dev, "gtp -> IP src: %pI4 dst: %pI4\n",
656 			   &iph->saddr, &iph->daddr);
657 	}
658 
659 	rt = ip4_route_output_gtp(&fl4, sk, daddr, saddr);
660 	if (IS_ERR(rt)) {
661 		netdev_dbg(dev, "no route to SSGN %pI4\n", &daddr);
662 		dev->stats.tx_carrier_errors++;
663 		goto err;
664 	}
665 
666 	if (rt->dst.dev == dev) {
667 		netdev_dbg(dev, "circular route to SSGN %pI4\n", &daddr);
668 		dev->stats.collisions++;
669 		goto err_rt;
670 	}
671 
672 	skb_dst_drop(skb);
673 
674 	/* This is similar to tnl_update_pmtu(). */
675 	if (df) {
676 		mtu = dst_mtu(&rt->dst) - dev->hard_header_len -
677 			sizeof(struct iphdr) - sizeof(struct udphdr);
678 		switch (gtp_version) {
679 		case GTP_V0:
680 			mtu -= sizeof(struct gtp0_header);
681 			break;
682 		case GTP_V1:
683 			mtu -= sizeof(struct gtp1_header);
684 			break;
685 		}
686 	} else {
687 		mtu = dst_mtu(&rt->dst);
688 	}
689 
690 	rt->dst.ops->update_pmtu(&rt->dst, NULL, skb, mtu, false);
691 
692 	if (!skb_is_gso(skb) && (df & htons(IP_DF)) && mtu < skb->len) {
693 		netdev_dbg(dev, "packet too big, fragmentation needed");
694 		memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
695 		icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
696 			      htonl(mtu));
697 		goto err_rt;
698 	}
699 
700 	gtp_set_pktinfo_ipv4(pktinfo, sk, tos, rt, &fl4, dev);
701 
702 	if (unlikely(opts)) {
703 		int err;
704 
705 		pktinfo->gtph_port = htons(GTP1U_PORT);
706 		err = gtp1_push_control_header(skb, tun_id, opts, dev);
707 		if (err) {
708 			netdev_info(dev, "cntr pkt error %d", err);
709 			goto err_rt;
710 		}
711 		return 0;
712 	}
713 
714 	switch (gtp_version) {
715 	case GTP_V0:
716 		pktinfo->gtph_port = htons(GTP0_PORT);
717 		gtp0_push_header(skb, pctx);
718 		break;
719 	case GTP_V1:
720 		pktinfo->gtph_port = htons(GTP1U_PORT);
721 		gtp1_push_header(skb, tun_id);
722 		break;
723 	}
724 
725 	return 0;
726 err_rt:
727 	ip_rt_put(rt);
728 err:
729 	return -EBADMSG;
730 }
731 
732 static netdev_tx_t gtp_dev_xmit(struct sk_buff *skb, struct net_device *dev)
733 {
734 	struct gtp_pktinfo pktinfo;
735 	int err;
736 
737 	/* Ensure there is sufficient headroom. */
738 	if (skb_cow_head(skb, dev->needed_headroom))
739 		goto tx_err;
740 
741 	skb_reset_inner_headers(skb);
742 
743 	/* PDP context lookups in gtp_build_skb_*() need rcu read-side lock. */
744 	rcu_read_lock();
745 	err = gtp_build_skb_ip4(skb, dev, &pktinfo);
746 	rcu_read_unlock();
747 
748 	if (err < 0)
749 		goto tx_err;
750 
751 	udp_tunnel_xmit_skb(pktinfo.rt, pktinfo.sk, skb,
752 			    pktinfo.fl4.saddr,
753 			    pktinfo.fl4.daddr,
754 			    pktinfo.tos,
755 			    ip4_dst_hoplimit(&pktinfo.rt->dst),
756 			    0,
757 			    pktinfo.gtph_port,
758 			    pktinfo.gtph_port,
759 			    true,
760 			    false);
761 
762 	return NETDEV_TX_OK;
763 tx_err:
764 	dev->stats.tx_errors++;
765 	dev_kfree_skb(skb);
766 	return NETDEV_TX_OK;
767 }
768 
769 static const struct net_device_ops gtp_netdev_ops = {
770 	.ndo_init		= gtp_dev_init,
771 	.ndo_uninit		= gtp_dev_uninit,
772 	.ndo_start_xmit		= gtp_dev_xmit,
773 	.ndo_get_stats64	= dev_get_tstats64,
774 };
775 
776 static struct gtp_dev *gtp_find_flow_based_dev(struct net *net)
777 {
778 	struct gtp_net *gn = net_generic(net, gtp_net_id);
779 	struct gtp_dev *gtp;
780 
781 	list_for_each_entry(gtp, &gn->gtp_dev_list, list) {
782 		if (gtp->collect_md)
783 			return gtp;
784 	}
785 
786 	return NULL;
787 }
788 
789 static void gtp_link_setup(struct net_device *dev)
790 {
791 	dev->netdev_ops		= &gtp_netdev_ops;
792 	dev->needs_free_netdev	= true;
793 
794 	dev->hard_header_len = 0;
795 	dev->addr_len = 0;
796 
797 	/* Zero header length. */
798 	dev->type = ARPHRD_NONE;
799 	dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
800 
801 	dev->priv_flags	|= IFF_NO_QUEUE;
802 	dev->features	|= NETIF_F_LLTX;
803 	netif_keep_dst(dev);
804 
805 	/* Assume largest header, ie. GTPv0. */
806 	dev->needed_headroom	= LL_MAX_HEADER +
807 				  sizeof(struct iphdr) +
808 				  sizeof(struct udphdr) +
809 				  sizeof(struct gtp0_header);
810 }
811 
812 static int gtp_hashtable_new(struct gtp_dev *gtp, int hsize);
813 static int gtp_encap_enable(struct gtp_dev *gtp, struct net_device *dev, struct nlattr *data[]);
814 
815 static void gtp_destructor(struct net_device *dev)
816 {
817 	struct gtp_dev *gtp = netdev_priv(dev);
818 
819 	kfree(gtp->addr_hash);
820 	kfree(gtp->tid_hash);
821 }
822 
823 static int gtp_newlink(struct net *src_net, struct net_device *dev,
824 		       struct nlattr *tb[], struct nlattr *data[],
825 		       struct netlink_ext_ack *extack)
826 {
827 	struct gtp_dev *gtp;
828 	struct gtp_net *gn;
829 	int hashsize, err;
830 
831 	if (!data[IFLA_GTP_FD0] && !data[IFLA_GTP_FD1] &&
832 	    !data[IFLA_GTP_COLLECT_METADATA])
833 		return -EINVAL;
834 
835 	gtp = netdev_priv(dev);
836 
837 	if (data[IFLA_GTP_COLLECT_METADATA]) {
838 		if (data[IFLA_GTP_FD0]) {
839 			netdev_dbg(dev, "LWT device does not support setting v0 socket");
840 			return -EINVAL;
841 		}
842 		if (gtp_find_flow_based_dev(src_net)) {
843 			netdev_dbg(dev, "LWT device already exist");
844 			return -EBUSY;
845 		}
846 		gtp->collect_md = true;
847 	}
848 
849 	if (!data[IFLA_GTP_PDP_HASHSIZE]) {
850 		hashsize = 1024;
851 	} else {
852 		hashsize = nla_get_u32(data[IFLA_GTP_PDP_HASHSIZE]);
853 		if (!hashsize)
854 			hashsize = 1024;
855 	}
856 
857 	err = gtp_hashtable_new(gtp, hashsize);
858 	if (err < 0)
859 		return err;
860 
861 	err = gtp_encap_enable(gtp, dev, data);
862 	if (err < 0)
863 		goto out_hashtable;
864 
865 	err = register_netdevice(dev);
866 	if (err < 0) {
867 		netdev_dbg(dev, "failed to register new netdev %d\n", err);
868 		goto out_encap;
869 	}
870 
871 	gn = net_generic(dev_net(dev), gtp_net_id);
872 	list_add_rcu(&gtp->list, &gn->gtp_dev_list);
873 	dev->priv_destructor = gtp_destructor;
874 
875 	netdev_dbg(dev, "registered new GTP interface %s\n", dev->name);
876 
877 	return 0;
878 
879 out_encap:
880 	gtp_encap_disable(gtp);
881 out_hashtable:
882 	kfree(gtp->addr_hash);
883 	kfree(gtp->tid_hash);
884 	return err;
885 }
886 
887 static void gtp_dellink(struct net_device *dev, struct list_head *head)
888 {
889 	struct gtp_dev *gtp = netdev_priv(dev);
890 	struct pdp_ctx *pctx;
891 	int i;
892 
893 	for (i = 0; i < gtp->hash_size; i++)
894 		hlist_for_each_entry_rcu(pctx, &gtp->tid_hash[i], hlist_tid)
895 			pdp_context_delete(pctx);
896 
897 	list_del_rcu(&gtp->list);
898 	unregister_netdevice_queue(dev, head);
899 }
900 
901 static const struct nla_policy gtp_policy[IFLA_GTP_MAX + 1] = {
902 	[IFLA_GTP_FD0]			= { .type = NLA_U32 },
903 	[IFLA_GTP_FD1]			= { .type = NLA_U32 },
904 	[IFLA_GTP_PDP_HASHSIZE]		= { .type = NLA_U32 },
905 	[IFLA_GTP_ROLE]			= { .type = NLA_U32 },
906 	[IFLA_GTP_COLLECT_METADATA]	= { .type = NLA_FLAG },
907 };
908 
909 static int gtp_validate(struct nlattr *tb[], struct nlattr *data[],
910 			struct netlink_ext_ack *extack)
911 {
912 	if (!data)
913 		return -EINVAL;
914 
915 	return 0;
916 }
917 
918 static size_t gtp_get_size(const struct net_device *dev)
919 {
920 	return nla_total_size(sizeof(__u32));	/* IFLA_GTP_PDP_HASHSIZE */
921 }
922 
923 static int gtp_fill_info(struct sk_buff *skb, const struct net_device *dev)
924 {
925 	struct gtp_dev *gtp = netdev_priv(dev);
926 
927 	if (nla_put_u32(skb, IFLA_GTP_PDP_HASHSIZE, gtp->hash_size))
928 		goto nla_put_failure;
929 
930 	if (gtp->collect_md && nla_put_flag(skb, IFLA_GTP_COLLECT_METADATA))
931 		goto nla_put_failure;
932 
933 	return 0;
934 
935 nla_put_failure:
936 	return -EMSGSIZE;
937 }
938 
939 static struct rtnl_link_ops gtp_link_ops __read_mostly = {
940 	.kind		= "gtp",
941 	.maxtype	= IFLA_GTP_MAX,
942 	.policy		= gtp_policy,
943 	.priv_size	= sizeof(struct gtp_dev),
944 	.setup		= gtp_link_setup,
945 	.validate	= gtp_validate,
946 	.newlink	= gtp_newlink,
947 	.dellink	= gtp_dellink,
948 	.get_size	= gtp_get_size,
949 	.fill_info	= gtp_fill_info,
950 };
951 
952 static int gtp_hashtable_new(struct gtp_dev *gtp, int hsize)
953 {
954 	int i;
955 
956 	gtp->addr_hash = kmalloc_array(hsize, sizeof(struct hlist_head),
957 				       GFP_KERNEL | __GFP_NOWARN);
958 	if (gtp->addr_hash == NULL)
959 		return -ENOMEM;
960 
961 	gtp->tid_hash = kmalloc_array(hsize, sizeof(struct hlist_head),
962 				      GFP_KERNEL | __GFP_NOWARN);
963 	if (gtp->tid_hash == NULL)
964 		goto err1;
965 
966 	gtp->hash_size = hsize;
967 
968 	for (i = 0; i < hsize; i++) {
969 		INIT_HLIST_HEAD(&gtp->addr_hash[i]);
970 		INIT_HLIST_HEAD(&gtp->tid_hash[i]);
971 	}
972 	return 0;
973 err1:
974 	kfree(gtp->addr_hash);
975 	return -ENOMEM;
976 }
977 
978 static int __gtp_encap_enable_socket(struct socket *sock, int type,
979 				     struct gtp_dev *gtp)
980 {
981 	struct udp_tunnel_sock_cfg tuncfg = {NULL};
982 	struct sock *sk;
983 
984 	sk = sock->sk;
985 	if (sk->sk_protocol != IPPROTO_UDP ||
986 	    sk->sk_type != SOCK_DGRAM ||
987 	    (sk->sk_family != AF_INET && sk->sk_family != AF_INET6)) {
988 		pr_debug("socket not UDP\n");
989 		return -EINVAL;
990 	}
991 
992 	lock_sock(sk);
993 	if (sk->sk_user_data) {
994 		release_sock(sock->sk);
995 		return -EBUSY;
996 	}
997 
998 	sock_hold(sk);
999 
1000 	tuncfg.sk_user_data = gtp;
1001 	tuncfg.encap_type = type;
1002 	tuncfg.encap_rcv = gtp_encap_recv;
1003 	tuncfg.encap_destroy = gtp_encap_destroy;
1004 
1005 	setup_udp_tunnel_sock(sock_net(sock->sk), sock, &tuncfg);
1006 	release_sock(sock->sk);
1007 	return 0;
1008 }
1009 
1010 static struct sock *gtp_encap_enable_socket(int fd, int type,
1011 					    struct gtp_dev *gtp)
1012 {
1013 	struct socket *sock;
1014 	int err;
1015 
1016 	pr_debug("enable gtp on %d, %d\n", fd, type);
1017 
1018 	sock = sockfd_lookup(fd, &err);
1019 	if (!sock) {
1020 		pr_debug("gtp socket fd=%d not found\n", fd);
1021 		return NULL;
1022 	}
1023 	err =  __gtp_encap_enable_socket(sock, type, gtp);
1024 	sockfd_put(sock);
1025 	if (err)
1026 		return ERR_PTR(err);
1027 
1028 	return sock->sk;
1029 }
1030 
1031 static struct socket *gtp_create_gtp_socket(struct gtp_dev *gtp, struct net_device *dev)
1032 {
1033 	struct udp_port_cfg udp_conf;
1034 	struct socket *sock;
1035 	int err;
1036 
1037 	memset(&udp_conf, 0, sizeof(udp_conf));
1038 	udp_conf.family = AF_INET;
1039 	udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
1040 	udp_conf.local_udp_port = htons(GTP1U_PORT);
1041 
1042 	err = udp_sock_create(dev_net(dev), &udp_conf, &sock);
1043 	if (err < 0) {
1044 		pr_debug("create gtp sock failed: %d\n", err);
1045 		return ERR_PTR(err);
1046 	}
1047 	err = __gtp_encap_enable_socket(sock, UDP_ENCAP_GTP1U, gtp);
1048 	if (err) {
1049 		pr_debug("enable gtp sock encap failed: %d\n", err);
1050 		udp_tunnel_sock_release(sock);
1051 		return ERR_PTR(err);
1052 	}
1053 	pr_debug("create gtp sock done\n");
1054 	return sock;
1055 }
1056 
1057 static int gtp_encap_enable(struct gtp_dev *gtp, struct net_device *dev, struct nlattr *data[])
1058 {
1059 	struct sock *sk1u = NULL;
1060 	struct sock *sk0 = NULL;
1061 	unsigned int role = GTP_ROLE_GGSN;
1062 
1063 	if (data[IFLA_GTP_FD0]) {
1064 		u32 fd0 = nla_get_u32(data[IFLA_GTP_FD0]);
1065 
1066 		sk0 = gtp_encap_enable_socket(fd0, UDP_ENCAP_GTP0, gtp);
1067 		if (IS_ERR(sk0))
1068 			return PTR_ERR(sk0);
1069 	}
1070 
1071 	if (data[IFLA_GTP_FD1]) {
1072 		u32 fd1 = nla_get_u32(data[IFLA_GTP_FD1]);
1073 
1074 		sk1u = gtp_encap_enable_socket(fd1, UDP_ENCAP_GTP1U, gtp);
1075 		if (IS_ERR(sk1u)) {
1076 			gtp_encap_disable_sock(sk0);
1077 			return PTR_ERR(sk1u);
1078 		}
1079 	}
1080 
1081 	if (data[IFLA_GTP_COLLECT_METADATA]) {
1082 		struct socket *sock;
1083 
1084 		if (!sk1u) {
1085 			sock = gtp_create_gtp_socket(gtp, dev);
1086 			if (IS_ERR(sock))
1087 				return PTR_ERR(sock);
1088 
1089 			gtp->collect_md_sock = sock;
1090 			sk1u = sock->sk;
1091 		} else {
1092 			gtp->collect_md_sock = NULL;
1093 		}
1094 	}
1095 
1096 	if (data[IFLA_GTP_ROLE]) {
1097 		role = nla_get_u32(data[IFLA_GTP_ROLE]);
1098 		if (role > GTP_ROLE_SGSN) {
1099 			gtp_encap_disable(gtp);
1100 			return -EINVAL;
1101 		}
1102 	}
1103 
1104 	gtp->sk0 = sk0;
1105 	gtp->sk1u = sk1u;
1106 	gtp->role = role;
1107 
1108 	return 0;
1109 }
1110 
1111 static struct gtp_dev *gtp_find_dev(struct net *src_net, struct nlattr *nla[])
1112 {
1113 	struct gtp_dev *gtp = NULL;
1114 	struct net_device *dev;
1115 	struct net *net;
1116 
1117 	/* Examine the link attributes and figure out which network namespace
1118 	 * we are talking about.
1119 	 */
1120 	if (nla[GTPA_NET_NS_FD])
1121 		net = get_net_ns_by_fd(nla_get_u32(nla[GTPA_NET_NS_FD]));
1122 	else
1123 		net = get_net(src_net);
1124 
1125 	if (IS_ERR(net))
1126 		return NULL;
1127 
1128 	/* Check if there's an existing gtpX device to configure */
1129 	dev = dev_get_by_index_rcu(net, nla_get_u32(nla[GTPA_LINK]));
1130 	if (dev && dev->netdev_ops == &gtp_netdev_ops)
1131 		gtp = netdev_priv(dev);
1132 
1133 	put_net(net);
1134 	return gtp;
1135 }
1136 
1137 static void ipv4_pdp_fill(struct pdp_ctx *pctx, struct genl_info *info)
1138 {
1139 	pctx->gtp_version = nla_get_u32(info->attrs[GTPA_VERSION]);
1140 	pctx->af = AF_INET;
1141 	pctx->peer_addr_ip4.s_addr =
1142 		nla_get_be32(info->attrs[GTPA_PEER_ADDRESS]);
1143 	pctx->ms_addr_ip4.s_addr =
1144 		nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
1145 
1146 	switch (pctx->gtp_version) {
1147 	case GTP_V0:
1148 		/* According to TS 09.60, sections 7.5.1 and 7.5.2, the flow
1149 		 * label needs to be the same for uplink and downlink packets,
1150 		 * so let's annotate this.
1151 		 */
1152 		pctx->u.v0.tid = nla_get_u64(info->attrs[GTPA_TID]);
1153 		pctx->u.v0.flow = nla_get_u16(info->attrs[GTPA_FLOW]);
1154 		break;
1155 	case GTP_V1:
1156 		pctx->u.v1.i_tei = nla_get_u32(info->attrs[GTPA_I_TEI]);
1157 		pctx->u.v1.o_tei = nla_get_u32(info->attrs[GTPA_O_TEI]);
1158 		break;
1159 	default:
1160 		break;
1161 	}
1162 }
1163 
1164 static struct pdp_ctx *gtp_pdp_add(struct gtp_dev *gtp, struct sock *sk,
1165 				   struct genl_info *info)
1166 {
1167 	struct pdp_ctx *pctx, *pctx_tid = NULL;
1168 	struct net_device *dev = gtp->dev;
1169 	u32 hash_ms, hash_tid = 0;
1170 	unsigned int version;
1171 	bool found = false;
1172 	__be32 ms_addr;
1173 
1174 	ms_addr = nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
1175 	hash_ms = ipv4_hashfn(ms_addr) % gtp->hash_size;
1176 	version = nla_get_u32(info->attrs[GTPA_VERSION]);
1177 
1178 	pctx = ipv4_pdp_find(gtp, ms_addr);
1179 	if (pctx)
1180 		found = true;
1181 	if (version == GTP_V0)
1182 		pctx_tid = gtp0_pdp_find(gtp,
1183 					 nla_get_u64(info->attrs[GTPA_TID]));
1184 	else if (version == GTP_V1)
1185 		pctx_tid = gtp1_pdp_find(gtp,
1186 					 nla_get_u32(info->attrs[GTPA_I_TEI]));
1187 	if (pctx_tid)
1188 		found = true;
1189 
1190 	if (found) {
1191 		if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
1192 			return ERR_PTR(-EEXIST);
1193 		if (info->nlhdr->nlmsg_flags & NLM_F_REPLACE)
1194 			return ERR_PTR(-EOPNOTSUPP);
1195 
1196 		if (pctx && pctx_tid)
1197 			return ERR_PTR(-EEXIST);
1198 		if (!pctx)
1199 			pctx = pctx_tid;
1200 
1201 		ipv4_pdp_fill(pctx, info);
1202 
1203 		if (pctx->gtp_version == GTP_V0)
1204 			netdev_dbg(dev, "GTPv0-U: update tunnel id = %llx (pdp %p)\n",
1205 				   pctx->u.v0.tid, pctx);
1206 		else if (pctx->gtp_version == GTP_V1)
1207 			netdev_dbg(dev, "GTPv1-U: update tunnel id = %x/%x (pdp %p)\n",
1208 				   pctx->u.v1.i_tei, pctx->u.v1.o_tei, pctx);
1209 
1210 		return pctx;
1211 
1212 	}
1213 
1214 	pctx = kmalloc(sizeof(*pctx), GFP_ATOMIC);
1215 	if (pctx == NULL)
1216 		return ERR_PTR(-ENOMEM);
1217 
1218 	sock_hold(sk);
1219 	pctx->sk = sk;
1220 	pctx->dev = gtp->dev;
1221 	ipv4_pdp_fill(pctx, info);
1222 	atomic_set(&pctx->tx_seq, 0);
1223 
1224 	switch (pctx->gtp_version) {
1225 	case GTP_V0:
1226 		/* TS 09.60: "The flow label identifies unambiguously a GTP
1227 		 * flow.". We use the tid for this instead, I cannot find a
1228 		 * situation in which this doesn't unambiguosly identify the
1229 		 * PDP context.
1230 		 */
1231 		hash_tid = gtp0_hashfn(pctx->u.v0.tid) % gtp->hash_size;
1232 		break;
1233 	case GTP_V1:
1234 		hash_tid = gtp1u_hashfn(pctx->u.v1.i_tei) % gtp->hash_size;
1235 		break;
1236 	}
1237 
1238 	hlist_add_head_rcu(&pctx->hlist_addr, &gtp->addr_hash[hash_ms]);
1239 	hlist_add_head_rcu(&pctx->hlist_tid, &gtp->tid_hash[hash_tid]);
1240 
1241 	switch (pctx->gtp_version) {
1242 	case GTP_V0:
1243 		netdev_dbg(dev, "GTPv0-U: new PDP ctx id=%llx ssgn=%pI4 ms=%pI4 (pdp=%p)\n",
1244 			   pctx->u.v0.tid, &pctx->peer_addr_ip4,
1245 			   &pctx->ms_addr_ip4, pctx);
1246 		break;
1247 	case GTP_V1:
1248 		netdev_dbg(dev, "GTPv1-U: new PDP ctx id=%x/%x ssgn=%pI4 ms=%pI4 (pdp=%p)\n",
1249 			   pctx->u.v1.i_tei, pctx->u.v1.o_tei,
1250 			   &pctx->peer_addr_ip4, &pctx->ms_addr_ip4, pctx);
1251 		break;
1252 	}
1253 
1254 	return pctx;
1255 }
1256 
1257 static void pdp_context_free(struct rcu_head *head)
1258 {
1259 	struct pdp_ctx *pctx = container_of(head, struct pdp_ctx, rcu_head);
1260 
1261 	sock_put(pctx->sk);
1262 	kfree(pctx);
1263 }
1264 
1265 static void pdp_context_delete(struct pdp_ctx *pctx)
1266 {
1267 	hlist_del_rcu(&pctx->hlist_tid);
1268 	hlist_del_rcu(&pctx->hlist_addr);
1269 	call_rcu(&pctx->rcu_head, pdp_context_free);
1270 }
1271 
1272 static int gtp_tunnel_notify(struct pdp_ctx *pctx, u8 cmd, gfp_t allocation);
1273 
1274 static int gtp_genl_new_pdp(struct sk_buff *skb, struct genl_info *info)
1275 {
1276 	unsigned int version;
1277 	struct pdp_ctx *pctx;
1278 	struct gtp_dev *gtp;
1279 	struct sock *sk;
1280 	int err;
1281 
1282 	if (!info->attrs[GTPA_VERSION] ||
1283 	    !info->attrs[GTPA_LINK] ||
1284 	    !info->attrs[GTPA_PEER_ADDRESS] ||
1285 	    !info->attrs[GTPA_MS_ADDRESS])
1286 		return -EINVAL;
1287 
1288 	version = nla_get_u32(info->attrs[GTPA_VERSION]);
1289 
1290 	switch (version) {
1291 	case GTP_V0:
1292 		if (!info->attrs[GTPA_TID] ||
1293 		    !info->attrs[GTPA_FLOW])
1294 			return -EINVAL;
1295 		break;
1296 	case GTP_V1:
1297 		if (!info->attrs[GTPA_I_TEI] ||
1298 		    !info->attrs[GTPA_O_TEI])
1299 			return -EINVAL;
1300 		break;
1301 
1302 	default:
1303 		return -EINVAL;
1304 	}
1305 
1306 	rtnl_lock();
1307 
1308 	gtp = gtp_find_dev(sock_net(skb->sk), info->attrs);
1309 	if (!gtp) {
1310 		err = -ENODEV;
1311 		goto out_unlock;
1312 	}
1313 
1314 	if (version == GTP_V0)
1315 		sk = gtp->sk0;
1316 	else if (version == GTP_V1)
1317 		sk = gtp->sk1u;
1318 	else
1319 		sk = NULL;
1320 
1321 	if (!sk) {
1322 		err = -ENODEV;
1323 		goto out_unlock;
1324 	}
1325 
1326 	pctx = gtp_pdp_add(gtp, sk, info);
1327 	if (IS_ERR(pctx)) {
1328 		err = PTR_ERR(pctx);
1329 	} else {
1330 		gtp_tunnel_notify(pctx, GTP_CMD_NEWPDP, GFP_KERNEL);
1331 		err = 0;
1332 	}
1333 
1334 out_unlock:
1335 	rtnl_unlock();
1336 	return err;
1337 }
1338 
1339 static struct pdp_ctx *gtp_find_pdp_by_link(struct net *net,
1340 					    struct nlattr *nla[])
1341 {
1342 	struct gtp_dev *gtp;
1343 
1344 	gtp = gtp_find_dev(net, nla);
1345 	if (!gtp)
1346 		return ERR_PTR(-ENODEV);
1347 
1348 	if (nla[GTPA_MS_ADDRESS]) {
1349 		__be32 ip = nla_get_be32(nla[GTPA_MS_ADDRESS]);
1350 
1351 		return ipv4_pdp_find(gtp, ip);
1352 	} else if (nla[GTPA_VERSION]) {
1353 		u32 gtp_version = nla_get_u32(nla[GTPA_VERSION]);
1354 
1355 		if (gtp_version == GTP_V0 && nla[GTPA_TID])
1356 			return gtp0_pdp_find(gtp, nla_get_u64(nla[GTPA_TID]));
1357 		else if (gtp_version == GTP_V1 && nla[GTPA_I_TEI])
1358 			return gtp1_pdp_find(gtp, nla_get_u32(nla[GTPA_I_TEI]));
1359 	}
1360 
1361 	return ERR_PTR(-EINVAL);
1362 }
1363 
1364 static struct pdp_ctx *gtp_find_pdp(struct net *net, struct nlattr *nla[])
1365 {
1366 	struct pdp_ctx *pctx;
1367 
1368 	if (nla[GTPA_LINK])
1369 		pctx = gtp_find_pdp_by_link(net, nla);
1370 	else
1371 		pctx = ERR_PTR(-EINVAL);
1372 
1373 	if (!pctx)
1374 		pctx = ERR_PTR(-ENOENT);
1375 
1376 	return pctx;
1377 }
1378 
1379 static int gtp_genl_del_pdp(struct sk_buff *skb, struct genl_info *info)
1380 {
1381 	struct pdp_ctx *pctx;
1382 	int err = 0;
1383 
1384 	if (!info->attrs[GTPA_VERSION])
1385 		return -EINVAL;
1386 
1387 	rcu_read_lock();
1388 
1389 	pctx = gtp_find_pdp(sock_net(skb->sk), info->attrs);
1390 	if (IS_ERR(pctx)) {
1391 		err = PTR_ERR(pctx);
1392 		goto out_unlock;
1393 	}
1394 
1395 	if (pctx->gtp_version == GTP_V0)
1396 		netdev_dbg(pctx->dev, "GTPv0-U: deleting tunnel id = %llx (pdp %p)\n",
1397 			   pctx->u.v0.tid, pctx);
1398 	else if (pctx->gtp_version == GTP_V1)
1399 		netdev_dbg(pctx->dev, "GTPv1-U: deleting tunnel id = %x/%x (pdp %p)\n",
1400 			   pctx->u.v1.i_tei, pctx->u.v1.o_tei, pctx);
1401 
1402 	gtp_tunnel_notify(pctx, GTP_CMD_DELPDP, GFP_ATOMIC);
1403 	pdp_context_delete(pctx);
1404 
1405 out_unlock:
1406 	rcu_read_unlock();
1407 	return err;
1408 }
1409 
1410 static struct genl_family gtp_genl_family;
1411 
1412 enum gtp_multicast_groups {
1413 	GTP_GENL_MCGRP,
1414 };
1415 
1416 static const struct genl_multicast_group gtp_genl_mcgrps[] = {
1417 	[GTP_GENL_MCGRP] = { .name = GTP_GENL_MCGRP_NAME },
1418 };
1419 
1420 static int gtp_genl_fill_info(struct sk_buff *skb, u32 snd_portid, u32 snd_seq,
1421 			      int flags, u32 type, struct pdp_ctx *pctx)
1422 {
1423 	void *genlh;
1424 
1425 	genlh = genlmsg_put(skb, snd_portid, snd_seq, &gtp_genl_family, flags,
1426 			    type);
1427 	if (genlh == NULL)
1428 		goto nlmsg_failure;
1429 
1430 	if (nla_put_u32(skb, GTPA_VERSION, pctx->gtp_version) ||
1431 	    nla_put_u32(skb, GTPA_LINK, pctx->dev->ifindex) ||
1432 	    nla_put_be32(skb, GTPA_PEER_ADDRESS, pctx->peer_addr_ip4.s_addr) ||
1433 	    nla_put_be32(skb, GTPA_MS_ADDRESS, pctx->ms_addr_ip4.s_addr))
1434 		goto nla_put_failure;
1435 
1436 	switch (pctx->gtp_version) {
1437 	case GTP_V0:
1438 		if (nla_put_u64_64bit(skb, GTPA_TID, pctx->u.v0.tid, GTPA_PAD) ||
1439 		    nla_put_u16(skb, GTPA_FLOW, pctx->u.v0.flow))
1440 			goto nla_put_failure;
1441 		break;
1442 	case GTP_V1:
1443 		if (nla_put_u32(skb, GTPA_I_TEI, pctx->u.v1.i_tei) ||
1444 		    nla_put_u32(skb, GTPA_O_TEI, pctx->u.v1.o_tei))
1445 			goto nla_put_failure;
1446 		break;
1447 	}
1448 	genlmsg_end(skb, genlh);
1449 	return 0;
1450 
1451 nlmsg_failure:
1452 nla_put_failure:
1453 	genlmsg_cancel(skb, genlh);
1454 	return -EMSGSIZE;
1455 }
1456 
1457 static int gtp_tunnel_notify(struct pdp_ctx *pctx, u8 cmd, gfp_t allocation)
1458 {
1459 	struct sk_buff *msg;
1460 	int ret;
1461 
1462 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, allocation);
1463 	if (!msg)
1464 		return -ENOMEM;
1465 
1466 	ret = gtp_genl_fill_info(msg, 0, 0, 0, cmd, pctx);
1467 	if (ret < 0) {
1468 		nlmsg_free(msg);
1469 		return ret;
1470 	}
1471 
1472 	ret = genlmsg_multicast_netns(&gtp_genl_family, dev_net(pctx->dev), msg,
1473 				      0, GTP_GENL_MCGRP, GFP_ATOMIC);
1474 	return ret;
1475 }
1476 
1477 static int gtp_genl_get_pdp(struct sk_buff *skb, struct genl_info *info)
1478 {
1479 	struct pdp_ctx *pctx = NULL;
1480 	struct sk_buff *skb2;
1481 	int err;
1482 
1483 	if (!info->attrs[GTPA_VERSION])
1484 		return -EINVAL;
1485 
1486 	rcu_read_lock();
1487 
1488 	pctx = gtp_find_pdp(sock_net(skb->sk), info->attrs);
1489 	if (IS_ERR(pctx)) {
1490 		err = PTR_ERR(pctx);
1491 		goto err_unlock;
1492 	}
1493 
1494 	skb2 = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
1495 	if (skb2 == NULL) {
1496 		err = -ENOMEM;
1497 		goto err_unlock;
1498 	}
1499 
1500 	err = gtp_genl_fill_info(skb2, NETLINK_CB(skb).portid, info->snd_seq,
1501 				 0, info->nlhdr->nlmsg_type, pctx);
1502 	if (err < 0)
1503 		goto err_unlock_free;
1504 
1505 	rcu_read_unlock();
1506 	return genlmsg_unicast(genl_info_net(info), skb2, info->snd_portid);
1507 
1508 err_unlock_free:
1509 	kfree_skb(skb2);
1510 err_unlock:
1511 	rcu_read_unlock();
1512 	return err;
1513 }
1514 
1515 static int gtp_genl_dump_pdp(struct sk_buff *skb,
1516 				struct netlink_callback *cb)
1517 {
1518 	struct gtp_dev *last_gtp = (struct gtp_dev *)cb->args[2], *gtp;
1519 	int i, j, bucket = cb->args[0], skip = cb->args[1];
1520 	struct net *net = sock_net(skb->sk);
1521 	struct pdp_ctx *pctx;
1522 	struct gtp_net *gn;
1523 
1524 	gn = net_generic(net, gtp_net_id);
1525 
1526 	if (cb->args[4])
1527 		return 0;
1528 
1529 	rcu_read_lock();
1530 	list_for_each_entry_rcu(gtp, &gn->gtp_dev_list, list) {
1531 		if (last_gtp && last_gtp != gtp)
1532 			continue;
1533 		else
1534 			last_gtp = NULL;
1535 
1536 		for (i = bucket; i < gtp->hash_size; i++) {
1537 			j = 0;
1538 			hlist_for_each_entry_rcu(pctx, &gtp->tid_hash[i],
1539 						 hlist_tid) {
1540 				if (j >= skip &&
1541 				    gtp_genl_fill_info(skb,
1542 					    NETLINK_CB(cb->skb).portid,
1543 					    cb->nlh->nlmsg_seq,
1544 					    NLM_F_MULTI,
1545 					    cb->nlh->nlmsg_type, pctx)) {
1546 					cb->args[0] = i;
1547 					cb->args[1] = j;
1548 					cb->args[2] = (unsigned long)gtp;
1549 					goto out;
1550 				}
1551 				j++;
1552 			}
1553 			skip = 0;
1554 		}
1555 		bucket = 0;
1556 	}
1557 	cb->args[4] = 1;
1558 out:
1559 	rcu_read_unlock();
1560 	return skb->len;
1561 }
1562 
1563 static const struct nla_policy gtp_genl_policy[GTPA_MAX + 1] = {
1564 	[GTPA_LINK]		= { .type = NLA_U32, },
1565 	[GTPA_VERSION]		= { .type = NLA_U32, },
1566 	[GTPA_TID]		= { .type = NLA_U64, },
1567 	[GTPA_PEER_ADDRESS]	= { .type = NLA_U32, },
1568 	[GTPA_MS_ADDRESS]	= { .type = NLA_U32, },
1569 	[GTPA_FLOW]		= { .type = NLA_U16, },
1570 	[GTPA_NET_NS_FD]	= { .type = NLA_U32, },
1571 	[GTPA_I_TEI]		= { .type = NLA_U32, },
1572 	[GTPA_O_TEI]		= { .type = NLA_U32, },
1573 };
1574 
1575 static const struct genl_small_ops gtp_genl_ops[] = {
1576 	{
1577 		.cmd = GTP_CMD_NEWPDP,
1578 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1579 		.doit = gtp_genl_new_pdp,
1580 		.flags = GENL_ADMIN_PERM,
1581 	},
1582 	{
1583 		.cmd = GTP_CMD_DELPDP,
1584 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1585 		.doit = gtp_genl_del_pdp,
1586 		.flags = GENL_ADMIN_PERM,
1587 	},
1588 	{
1589 		.cmd = GTP_CMD_GETPDP,
1590 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1591 		.doit = gtp_genl_get_pdp,
1592 		.dumpit = gtp_genl_dump_pdp,
1593 		.flags = GENL_ADMIN_PERM,
1594 	},
1595 };
1596 
1597 static struct genl_family gtp_genl_family __ro_after_init = {
1598 	.name		= "gtp",
1599 	.version	= 0,
1600 	.hdrsize	= 0,
1601 	.maxattr	= GTPA_MAX,
1602 	.policy = gtp_genl_policy,
1603 	.netnsok	= true,
1604 	.module		= THIS_MODULE,
1605 	.small_ops	= gtp_genl_ops,
1606 	.n_small_ops	= ARRAY_SIZE(gtp_genl_ops),
1607 	.mcgrps		= gtp_genl_mcgrps,
1608 	.n_mcgrps	= ARRAY_SIZE(gtp_genl_mcgrps),
1609 };
1610 
1611 static int __net_init gtp_net_init(struct net *net)
1612 {
1613 	struct gtp_net *gn = net_generic(net, gtp_net_id);
1614 
1615 	INIT_LIST_HEAD(&gn->gtp_dev_list);
1616 	return 0;
1617 }
1618 
1619 static void __net_exit gtp_net_exit(struct net *net)
1620 {
1621 	struct gtp_net *gn = net_generic(net, gtp_net_id);
1622 	struct gtp_dev *gtp;
1623 	LIST_HEAD(list);
1624 
1625 	rtnl_lock();
1626 	list_for_each_entry(gtp, &gn->gtp_dev_list, list)
1627 		gtp_dellink(gtp->dev, &list);
1628 
1629 	unregister_netdevice_many(&list);
1630 	rtnl_unlock();
1631 }
1632 
1633 static struct pernet_operations gtp_net_ops = {
1634 	.init	= gtp_net_init,
1635 	.exit	= gtp_net_exit,
1636 	.id	= &gtp_net_id,
1637 	.size	= sizeof(struct gtp_net),
1638 };
1639 
1640 static int __init gtp_init(void)
1641 {
1642 	int err;
1643 
1644 	get_random_bytes(&gtp_h_initval, sizeof(gtp_h_initval));
1645 
1646 	err = rtnl_link_register(&gtp_link_ops);
1647 	if (err < 0)
1648 		goto error_out;
1649 
1650 	err = genl_register_family(&gtp_genl_family);
1651 	if (err < 0)
1652 		goto unreg_rtnl_link;
1653 
1654 	err = register_pernet_subsys(&gtp_net_ops);
1655 	if (err < 0)
1656 		goto unreg_genl_family;
1657 
1658 	pr_info("GTP module loaded (pdp ctx size %zd bytes) with tnl-md support\n",
1659 		sizeof(struct pdp_ctx));
1660 	return 0;
1661 
1662 unreg_genl_family:
1663 	genl_unregister_family(&gtp_genl_family);
1664 unreg_rtnl_link:
1665 	rtnl_link_unregister(&gtp_link_ops);
1666 error_out:
1667 	pr_err("error loading GTP module loaded\n");
1668 	return err;
1669 }
1670 late_initcall(gtp_init);
1671 
1672 static void __exit gtp_fini(void)
1673 {
1674 	genl_unregister_family(&gtp_genl_family);
1675 	rtnl_link_unregister(&gtp_link_ops);
1676 	unregister_pernet_subsys(&gtp_net_ops);
1677 
1678 	pr_info("GTP module unloaded\n");
1679 }
1680 module_exit(gtp_fini);
1681 
1682 MODULE_LICENSE("GPL");
1683 MODULE_AUTHOR("Harald Welte <hwelte@sysmocom.de>");
1684 MODULE_DESCRIPTION("Interface driver for GTP encapsulated traffic");
1685 MODULE_ALIAS_RTNL_LINK("gtp");
1686 MODULE_ALIAS_GENL_FAMILY("gtp");
1687