xref: /linux/drivers/net/gtp.c (revision 889dd44491b37c890b1b4ef40876cf19032ca897)
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/mutex.h>
16 #include <linux/skbuff.h>
17 #include <linux/udp.h>
18 #include <linux/rculist.h>
19 #include <linux/jhash.h>
20 #include <linux/if_tunnel.h>
21 #include <linux/net.h>
22 #include <linux/file.h>
23 #include <linux/gtp.h>
24 
25 #include <net/flow.h>
26 #include <net/inet_dscp.h>
27 #include <net/net_namespace.h>
28 #include <net/protocol.h>
29 #include <net/inet_sock.h>
30 #include <net/ip.h>
31 #include <net/ipv6.h>
32 #include <net/udp.h>
33 #include <net/udp_tunnel.h>
34 #include <net/icmp.h>
35 #include <net/xfrm.h>
36 #include <net/genetlink.h>
37 #include <net/netns/generic.h>
38 #include <net/gtp.h>
39 
40 /* An active session for the subscriber. */
41 struct pdp_ctx {
42 	struct hlist_node	hlist_tid;
43 	struct hlist_node	hlist_addr;
44 
45 	union {
46 		struct {
47 			u64	tid;
48 			u16	flow;
49 		} v0;
50 		struct {
51 			u32	i_tei;
52 			u32	o_tei;
53 		} v1;
54 	} u;
55 	u8			gtp_version;
56 	u16			af;
57 
58 	union {
59 		struct in_addr	addr;
60 		struct in6_addr	addr6;
61 	} ms;
62 	union {
63 		struct in_addr	addr;
64 		struct in6_addr	addr6;
65 	} peer;
66 
67 	struct sock		*sk;
68 	struct net_device       *dev;
69 
70 	atomic_t		tx_seq;
71 	struct rcu_head		rcu_head;
72 };
73 
74 /* One instance of the GTP device. */
75 struct gtp_dev {
76 	struct list_head	list;
77 
78 	struct sock		*sk0;
79 	struct sock		*sk1u;
80 	u8			sk_created;
81 
82 	struct net_device	*dev;
83 	struct net		*net;
84 
85 	unsigned int		role;
86 	unsigned int		hash_size;
87 	struct hlist_head	*tid_hash;
88 	struct hlist_head	*addr_hash;
89 
90 	u8			restart_count;
91 };
92 
93 struct echo_info {
94 	u16			af;
95 	u8			gtp_version;
96 
97 	union {
98 		struct in_addr	addr;
99 	} ms;
100 	union {
101 		struct in_addr	addr;
102 	} peer;
103 };
104 
105 static unsigned int gtp_net_id __read_mostly;
106 
107 struct gtp_net {
108 	struct list_head gtp_dev_list;
109 };
110 
111 static u32 gtp_h_initval;
112 static DEFINE_MUTEX(gtp_pdp_lock);
113 
114 static struct genl_family gtp_genl_family;
115 
116 enum gtp_multicast_groups {
117 	GTP_GENL_MCGRP,
118 };
119 
120 static const struct genl_multicast_group gtp_genl_mcgrps[] = {
121 	[GTP_GENL_MCGRP] = { .name = GTP_GENL_MCGRP_NAME,
122 			     .flags = GENL_MCAST_CAP_NET_ADMIN, },
123 };
124 
125 static void pdp_context_delete(struct pdp_ctx *pctx);
126 
127 static inline u32 gtp0_hashfn(u64 tid)
128 {
129 	u32 *tid32 = (u32 *) &tid;
130 	return jhash_2words(tid32[0], tid32[1], gtp_h_initval);
131 }
132 
133 static inline u32 gtp1u_hashfn(u32 tid)
134 {
135 	return jhash_1word(tid, gtp_h_initval);
136 }
137 
138 static inline u32 ipv4_hashfn(__be32 ip)
139 {
140 	return jhash_1word((__force u32)ip, gtp_h_initval);
141 }
142 
143 static u32 ipv6_hashfn(const struct in6_addr *ip6)
144 {
145 	return jhash_2words((__force u32)ip6->s6_addr32[0],
146 			    (__force u32)ip6->s6_addr32[1], gtp_h_initval);
147 }
148 
149 /* Resolve a PDP context structure based on the 64bit TID. */
150 static struct pdp_ctx *gtp0_pdp_find(struct gtp_dev *gtp, u64 tid, u16 family)
151 {
152 	struct hlist_head *head;
153 	struct pdp_ctx *pdp;
154 
155 	head = &gtp->tid_hash[gtp0_hashfn(tid) % gtp->hash_size];
156 
157 	hlist_for_each_entry_rcu(pdp, head, hlist_tid,
158 				 lockdep_is_held(&gtp_pdp_lock)) {
159 		if (pdp->af == family &&
160 		    pdp->gtp_version == GTP_V0 &&
161 		    pdp->u.v0.tid == tid)
162 			return pdp;
163 	}
164 	return NULL;
165 }
166 
167 /* Resolve a PDP context structure based on the 32bit TEI. */
168 static struct pdp_ctx *gtp1_pdp_find(struct gtp_dev *gtp, u32 tid, u16 family)
169 {
170 	struct hlist_head *head;
171 	struct pdp_ctx *pdp;
172 
173 	head = &gtp->tid_hash[gtp1u_hashfn(tid) % gtp->hash_size];
174 
175 	hlist_for_each_entry_rcu(pdp, head, hlist_tid,
176 				 lockdep_is_held(&gtp_pdp_lock)) {
177 		if (pdp->af == family &&
178 		    pdp->gtp_version == GTP_V1 &&
179 		    pdp->u.v1.i_tei == tid)
180 			return pdp;
181 	}
182 	return NULL;
183 }
184 
185 /* Resolve a PDP context based on IPv4 address of MS. */
186 static struct pdp_ctx *ipv4_pdp_find(struct gtp_dev *gtp, __be32 ms_addr)
187 {
188 	struct hlist_head *head;
189 	struct pdp_ctx *pdp;
190 
191 	head = &gtp->addr_hash[ipv4_hashfn(ms_addr) % gtp->hash_size];
192 
193 	hlist_for_each_entry_rcu(pdp, head, hlist_addr,
194 				 lockdep_is_held(&gtp_pdp_lock)) {
195 		if (pdp->af == AF_INET &&
196 		    pdp->ms.addr.s_addr == ms_addr)
197 			return pdp;
198 	}
199 
200 	return NULL;
201 }
202 
203 /* 3GPP TS 29.060: PDN Connection: the association between a MS represented by
204  * [...] one IPv6 *prefix* and a PDN represented by an APN.
205  *
206  * Then, 3GPP TS 29.061, Section 11.2.1.3 says: The size of the prefix shall be
207  * according to the maximum prefix length for a global IPv6 address as
208  * specified in the IPv6 Addressing Architecture, see RFC 4291.
209  *
210  * Finally, RFC 4291 section 2.5.4 states: All Global Unicast addresses other
211  * than those that start with binary 000 have a 64-bit interface ID field
212  * (i.e., n + m = 64).
213  */
214 static bool ipv6_pdp_addr_equal(const struct in6_addr *a,
215 				const struct in6_addr *b)
216 {
217 	return a->s6_addr32[0] == b->s6_addr32[0] &&
218 	       a->s6_addr32[1] == b->s6_addr32[1];
219 }
220 
221 static struct pdp_ctx *ipv6_pdp_find(struct gtp_dev *gtp,
222 				     const struct in6_addr *ms_addr)
223 {
224 	struct hlist_head *head;
225 	struct pdp_ctx *pdp;
226 
227 	head = &gtp->addr_hash[ipv6_hashfn(ms_addr) % gtp->hash_size];
228 
229 	hlist_for_each_entry_rcu(pdp, head, hlist_addr,
230 				 lockdep_is_held(&gtp_pdp_lock)) {
231 		if (pdp->af == AF_INET6 &&
232 		    ipv6_pdp_addr_equal(&pdp->ms.addr6, ms_addr))
233 			return pdp;
234 	}
235 
236 	return NULL;
237 }
238 
239 static bool gtp_check_ms_ipv4(struct sk_buff *skb, struct pdp_ctx *pctx,
240 				  unsigned int hdrlen, unsigned int role)
241 {
242 	struct iphdr *iph;
243 
244 	if (!pskb_may_pull(skb, hdrlen + sizeof(struct iphdr)))
245 		return false;
246 
247 	iph = (struct iphdr *)(skb->data + hdrlen);
248 
249 	if (role == GTP_ROLE_SGSN)
250 		return iph->daddr == pctx->ms.addr.s_addr;
251 	else
252 		return iph->saddr == pctx->ms.addr.s_addr;
253 }
254 
255 static bool gtp_check_ms_ipv6(struct sk_buff *skb, struct pdp_ctx *pctx,
256 			      unsigned int hdrlen, unsigned int role)
257 {
258 	struct ipv6hdr *ip6h;
259 	int ret;
260 
261 	if (!pskb_may_pull(skb, hdrlen + sizeof(struct ipv6hdr)))
262 		return false;
263 
264 	ip6h = (struct ipv6hdr *)(skb->data + hdrlen);
265 
266 	if ((ipv6_addr_type(&ip6h->saddr) & IPV6_ADDR_LINKLOCAL) ||
267 	    (ipv6_addr_type(&ip6h->daddr) & IPV6_ADDR_LINKLOCAL))
268 		return false;
269 
270 	if (role == GTP_ROLE_SGSN) {
271 		ret = ipv6_pdp_addr_equal(&ip6h->daddr, &pctx->ms.addr6);
272 	} else {
273 		ret = ipv6_pdp_addr_equal(&ip6h->saddr, &pctx->ms.addr6);
274 	}
275 
276 	return ret;
277 }
278 
279 /* Check if the inner IP address in this packet is assigned to any
280  * existing mobile subscriber.
281  */
282 static bool gtp_check_ms(struct sk_buff *skb, struct pdp_ctx *pctx,
283 			 unsigned int hdrlen, unsigned int role,
284 			 __u16 inner_proto)
285 {
286 	switch (inner_proto) {
287 	case ETH_P_IP:
288 		return gtp_check_ms_ipv4(skb, pctx, hdrlen, role);
289 	case ETH_P_IPV6:
290 		return gtp_check_ms_ipv6(skb, pctx, hdrlen, role);
291 	}
292 	return false;
293 }
294 
295 static int gtp_inner_proto(struct sk_buff *skb, unsigned int hdrlen,
296 			   __u16 *inner_proto)
297 {
298 	__u8 *ip_version, _ip_version;
299 
300 	ip_version = skb_header_pointer(skb, hdrlen, sizeof(*ip_version),
301 					&_ip_version);
302 	if (!ip_version)
303 		return -1;
304 
305 	switch (*ip_version & 0xf0) {
306 	case 0x40:
307 		*inner_proto = ETH_P_IP;
308 		break;
309 	case 0x60:
310 		*inner_proto = ETH_P_IPV6;
311 		break;
312 	default:
313 		return -1;
314 	}
315 
316 	return 0;
317 }
318 
319 static int gtp_rx(struct pdp_ctx *pctx, struct sk_buff *skb,
320 		  unsigned int hdrlen, unsigned int role, __u16 inner_proto)
321 {
322 	if (skb_is_gso(skb)) {
323 		netdev_dbg(pctx->dev, "GSO is not supported in GTP\n");
324 		goto err;
325 	}
326 
327 	if (!gtp_check_ms(skb, pctx, hdrlen, role, inner_proto)) {
328 		netdev_dbg(pctx->dev, "No PDP ctx for this MS\n");
329 		return 1;
330 	}
331 
332 	/* Get rid of the GTP + UDP headers. */
333 	if (iptunnel_pull_header(skb, hdrlen, htons(inner_proto),
334 			 !net_eq(sock_net(pctx->sk), dev_net(pctx->dev)))) {
335 		pctx->dev->stats.rx_length_errors++;
336 		goto err;
337 	}
338 
339 	netdev_dbg(pctx->dev, "forwarding packet from GGSN to uplink\n");
340 
341 	/* Now that the UDP and the GTP header have been removed, set up the
342 	 * new network header. This is required by the upper layer to
343 	 * calculate the transport header.
344 	 */
345 	skb_reset_network_header(skb);
346 	skb_reset_mac_header(skb);
347 
348 	skb->dev = pctx->dev;
349 
350 	dev_sw_netstats_rx_add(pctx->dev, skb->len);
351 
352 	__netif_rx(skb);
353 	return 0;
354 
355 err:
356 	pctx->dev->stats.rx_dropped++;
357 	return -1;
358 }
359 
360 static struct rtable *ip4_route_output_gtp(struct flowi4 *fl4,
361 					   const struct sock *sk,
362 					   __be32 daddr, __be32 saddr)
363 {
364 	memset(fl4, 0, sizeof(*fl4));
365 	fl4->flowi4_oif		= sk->sk_bound_dev_if;
366 	fl4->daddr		= daddr;
367 	fl4->saddr		= saddr;
368 	fl4->flowi4_dscp	= inet_sk_dscp(inet_sk(sk));
369 	fl4->flowi4_scope	= ip_sock_rt_scope(sk);
370 	fl4->flowi4_proto	= sk->sk_protocol;
371 
372 	return ip_route_output_key(sock_net(sk), fl4);
373 }
374 
375 static struct rt6_info *ip6_route_output_gtp(struct net *net,
376 					     struct flowi6 *fl6,
377 					     const struct sock *sk,
378 					     const struct in6_addr *daddr,
379 					     struct in6_addr *saddr)
380 {
381 	struct dst_entry *dst;
382 
383 	memset(fl6, 0, sizeof(*fl6));
384 	fl6->flowi6_oif		= sk->sk_bound_dev_if;
385 	fl6->daddr		= *daddr;
386 	fl6->saddr		= *saddr;
387 	fl6->flowi6_proto	= sk->sk_protocol;
388 
389 	dst = ip6_dst_lookup_flow(net, sk, fl6, NULL);
390 	if (IS_ERR(dst))
391 		return ERR_PTR(-ENETUNREACH);
392 
393 	return (struct rt6_info *)dst;
394 }
395 
396 /* GSM TS 09.60. 7.3
397  * In all Path Management messages:
398  * - TID: is not used and shall be set to 0.
399  * - Flow Label is not used and shall be set to 0
400  * In signalling messages:
401  * - number: this field is not yet used in signalling messages.
402  *   It shall be set to 255 by the sender and shall be ignored
403  *   by the receiver
404  * Returns true if the echo req was correct, false otherwise.
405  */
406 static bool gtp0_validate_echo_hdr(struct gtp0_header *gtp0)
407 {
408 	return !(gtp0->tid || (gtp0->flags ^ 0x1e) ||
409 		gtp0->number != 0xff || gtp0->flow);
410 }
411 
412 /* msg_type has to be GTP_ECHO_REQ or GTP_ECHO_RSP */
413 static void gtp0_build_echo_msg(struct gtp0_header *hdr, __u8 msg_type)
414 {
415 	int len_pkt, len_hdr;
416 
417 	hdr->flags = 0x1e; /* v0, GTP-non-prime. */
418 	hdr->type = msg_type;
419 	/* GSM TS 09.60. 7.3 In all Path Management Flow Label and TID
420 	 * are not used and shall be set to 0.
421 	 */
422 	hdr->flow = 0;
423 	hdr->tid = 0;
424 	hdr->number = 0xff;
425 	hdr->spare[0] = 0xff;
426 	hdr->spare[1] = 0xff;
427 	hdr->spare[2] = 0xff;
428 
429 	len_pkt = sizeof(struct gtp0_packet);
430 	len_hdr = sizeof(struct gtp0_header);
431 
432 	if (msg_type == GTP_ECHO_RSP)
433 		hdr->length = htons(len_pkt - len_hdr);
434 	else
435 		hdr->length = 0;
436 }
437 
438 static int gtp0_send_echo_resp_ip(struct gtp_dev *gtp, struct sk_buff *skb)
439 {
440 	struct iphdr *iph = ip_hdr(skb);
441 	struct flowi4 fl4;
442 	struct rtable *rt;
443 
444 	/* find route to the sender,
445 	 * src address becomes dst address and vice versa.
446 	 */
447 	rt = ip4_route_output_gtp(&fl4, gtp->sk0, iph->saddr, iph->daddr);
448 	if (IS_ERR(rt)) {
449 		netdev_dbg(gtp->dev, "no route for echo response from %pI4\n",
450 			   &iph->saddr);
451 		return -1;
452 	}
453 
454 	udp_tunnel_xmit_skb(rt, gtp->sk0, skb,
455 			    fl4.saddr, fl4.daddr,
456 			    iph->tos,
457 			    ip4_dst_hoplimit(&rt->dst),
458 			    0,
459 			    htons(GTP0_PORT), htons(GTP0_PORT),
460 			    !net_eq(sock_net(gtp->sk1u),
461 				    dev_net(gtp->dev)),
462 			    false,
463 			    0);
464 
465 	return 0;
466 }
467 
468 static int gtp0_send_echo_resp(struct gtp_dev *gtp, struct sk_buff *skb)
469 {
470 	struct gtp0_packet *gtp_pkt;
471 	struct gtp0_header *gtp0;
472 	__be16 seq;
473 
474 	gtp0 = (struct gtp0_header *)(skb->data + sizeof(struct udphdr));
475 
476 	if (!gtp0_validate_echo_hdr(gtp0))
477 		return -1;
478 
479 	seq = gtp0->seq;
480 
481 	/* pull GTP and UDP headers */
482 	skb_pull_data(skb, sizeof(struct gtp0_header) + sizeof(struct udphdr));
483 
484 	gtp_pkt = skb_push(skb, sizeof(struct gtp0_packet));
485 	memset(gtp_pkt, 0, sizeof(struct gtp0_packet));
486 
487 	gtp0_build_echo_msg(&gtp_pkt->gtp0_h, GTP_ECHO_RSP);
488 
489 	/* GSM TS 09.60. 7.3 The Sequence Number in a signalling response
490 	 * message shall be copied from the signalling request message
491 	 * that the GSN is replying to.
492 	 */
493 	gtp_pkt->gtp0_h.seq = seq;
494 
495 	gtp_pkt->ie.tag = GTPIE_RECOVERY;
496 	gtp_pkt->ie.val = gtp->restart_count;
497 
498 	switch (gtp->sk0->sk_family) {
499 	case AF_INET:
500 		if (gtp0_send_echo_resp_ip(gtp, skb) < 0)
501 			return -1;
502 		break;
503 	case AF_INET6:
504 		return -1;
505 	}
506 
507 	return 0;
508 }
509 
510 static int gtp_genl_fill_echo(struct sk_buff *skb, u32 snd_portid, u32 snd_seq,
511 			      int flags, u32 type, struct echo_info echo)
512 {
513 	void *genlh;
514 
515 	genlh = genlmsg_put(skb, snd_portid, snd_seq, &gtp_genl_family, flags,
516 			    type);
517 	if (!genlh)
518 		goto failure;
519 
520 	if (nla_put_u32(skb, GTPA_VERSION, echo.gtp_version) ||
521 	    nla_put_be32(skb, GTPA_PEER_ADDRESS, echo.peer.addr.s_addr) ||
522 	    nla_put_be32(skb, GTPA_MS_ADDRESS, echo.ms.addr.s_addr))
523 		goto failure;
524 
525 	genlmsg_end(skb, genlh);
526 	return 0;
527 
528 failure:
529 	genlmsg_cancel(skb, genlh);
530 	return -EMSGSIZE;
531 }
532 
533 static void gtp0_handle_echo_resp_ip(struct sk_buff *skb, struct echo_info *echo)
534 {
535 	struct iphdr *iph = ip_hdr(skb);
536 
537 	echo->ms.addr.s_addr = iph->daddr;
538 	echo->peer.addr.s_addr = iph->saddr;
539 	echo->gtp_version = GTP_V0;
540 }
541 
542 static int gtp0_handle_echo_resp(struct gtp_dev *gtp, struct sk_buff *skb)
543 {
544 	struct gtp0_header *gtp0;
545 	struct echo_info echo;
546 	struct sk_buff *msg;
547 	int ret;
548 
549 	gtp0 = (struct gtp0_header *)(skb->data + sizeof(struct udphdr));
550 
551 	if (!gtp0_validate_echo_hdr(gtp0))
552 		return -1;
553 
554 	switch (gtp->sk0->sk_family) {
555 	case AF_INET:
556 		gtp0_handle_echo_resp_ip(skb, &echo);
557 		break;
558 	case AF_INET6:
559 		return -1;
560 	}
561 
562 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
563 	if (!msg)
564 		return -1;
565 
566 	ret = gtp_genl_fill_echo(msg, 0, 0, 0, GTP_CMD_ECHOREQ, echo);
567 	if (ret < 0) {
568 		nlmsg_free(msg);
569 		return -1;
570 	}
571 
572 	genlmsg_multicast_netns(&gtp_genl_family, dev_net(gtp->dev),
573 				msg, 0, GTP_GENL_MCGRP, GFP_ATOMIC);
574 	consume_skb(skb);
575 
576 	return 0;
577 }
578 
579 static int gtp_proto_to_family(__u16 proto)
580 {
581 	switch (proto) {
582 	case ETH_P_IP:
583 		return AF_INET;
584 	case ETH_P_IPV6:
585 		return AF_INET6;
586 	default:
587 		WARN_ON_ONCE(1);
588 		break;
589 	}
590 
591 	return AF_UNSPEC;
592 }
593 
594 /* 1 means pass up to the stack, -1 means drop and 0 means decapsulated. */
595 static int gtp0_udp_encap_recv(struct gtp_dev *gtp, struct sk_buff *skb)
596 {
597 	unsigned int hdrlen = sizeof(struct udphdr) +
598 			      sizeof(struct gtp0_header);
599 	struct gtp0_header *gtp0;
600 	struct pdp_ctx *pctx;
601 	__u16 inner_proto;
602 
603 	if (!pskb_may_pull(skb, hdrlen))
604 		return -1;
605 
606 	gtp0 = (struct gtp0_header *)(skb->data + sizeof(struct udphdr));
607 
608 	if ((gtp0->flags >> 5) != GTP_V0)
609 		return 1;
610 
611 	/* If the sockets were created in kernel, it means that
612 	 * there is no daemon running in userspace which would
613 	 * handle echo request.
614 	 */
615 	if (gtp0->type == GTP_ECHO_REQ && gtp->sk_created)
616 		return gtp0_send_echo_resp(gtp, skb);
617 
618 	if (gtp0->type == GTP_ECHO_RSP && gtp->sk_created)
619 		return gtp0_handle_echo_resp(gtp, skb);
620 
621 	if (gtp0->type != GTP_TPDU)
622 		return 1;
623 
624 	if (gtp_inner_proto(skb, hdrlen, &inner_proto) < 0) {
625 		netdev_dbg(gtp->dev, "GTP packet does not encapsulate an IP packet\n");
626 		return -1;
627 	}
628 
629 	pctx = gtp0_pdp_find(gtp, be64_to_cpu(gtp0->tid),
630 			     gtp_proto_to_family(inner_proto));
631 	if (!pctx) {
632 		netdev_dbg(gtp->dev, "No PDP ctx to decap skb=%p\n", skb);
633 		return 1;
634 	}
635 
636 	return gtp_rx(pctx, skb, hdrlen, gtp->role, inner_proto);
637 }
638 
639 /* msg_type has to be GTP_ECHO_REQ or GTP_ECHO_RSP */
640 static void gtp1u_build_echo_msg(struct gtp1_header_long *hdr, __u8 msg_type)
641 {
642 	int len_pkt, len_hdr;
643 
644 	/* S flag must be set to 1 */
645 	hdr->flags = 0x32; /* v1, GTP-non-prime. */
646 	hdr->type = msg_type;
647 	/* 3GPP TS 29.281 5.1 - TEID has to be set to 0 */
648 	hdr->tid = 0;
649 
650 	/* seq, npdu and next should be counted to the length of the GTP packet
651 	 * that's why size of gtp1_header should be subtracted,
652 	 * not size of gtp1_header_long.
653 	 */
654 
655 	len_hdr = sizeof(struct gtp1_header);
656 
657 	if (msg_type == GTP_ECHO_RSP) {
658 		len_pkt = sizeof(struct gtp1u_packet);
659 		hdr->length = htons(len_pkt - len_hdr);
660 	} else {
661 		/* GTP_ECHO_REQ does not carry GTP Information Element,
662 		 * the why gtp1_header_long is used here.
663 		 */
664 		len_pkt = sizeof(struct gtp1_header_long);
665 		hdr->length = htons(len_pkt - len_hdr);
666 	}
667 }
668 
669 static int gtp1u_send_echo_resp(struct gtp_dev *gtp, struct sk_buff *skb)
670 {
671 	struct gtp1_header_long *gtp1u;
672 	struct gtp1u_packet *gtp_pkt;
673 	struct rtable *rt;
674 	struct flowi4 fl4;
675 	struct iphdr *iph;
676 
677 	gtp1u = (struct gtp1_header_long *)(skb->data + sizeof(struct udphdr));
678 
679 	/* 3GPP TS 29.281 5.1 - For the Echo Request, Echo Response,
680 	 * Error Indication and Supported Extension Headers Notification
681 	 * messages, the S flag shall be set to 1 and TEID shall be set to 0.
682 	 */
683 	if (!(gtp1u->flags & GTP1_F_SEQ) || gtp1u->tid)
684 		return -1;
685 
686 	/* pull GTP and UDP headers */
687 	if (!skb_pull_data(skb, sizeof(struct gtp1_header_long) +
688 				sizeof(struct udphdr)))
689 		return -1;
690 
691 	gtp_pkt = skb_push(skb, sizeof(struct gtp1u_packet));
692 	memset(gtp_pkt, 0, sizeof(struct gtp1u_packet));
693 
694 	gtp1u_build_echo_msg(&gtp_pkt->gtp1u_h, GTP_ECHO_RSP);
695 
696 	/* 3GPP TS 29.281 7.7.2 - The Restart Counter value in the
697 	 * Recovery information element shall not be used, i.e. it shall
698 	 * be set to zero by the sender and shall be ignored by the receiver.
699 	 * The Recovery information element is mandatory due to backwards
700 	 * compatibility reasons.
701 	 */
702 	gtp_pkt->ie.tag = GTPIE_RECOVERY;
703 	gtp_pkt->ie.val = 0;
704 
705 	iph = ip_hdr(skb);
706 
707 	/* find route to the sender,
708 	 * src address becomes dst address and vice versa.
709 	 */
710 	rt = ip4_route_output_gtp(&fl4, gtp->sk1u, iph->saddr, iph->daddr);
711 	if (IS_ERR(rt)) {
712 		netdev_dbg(gtp->dev, "no route for echo response from %pI4\n",
713 			   &iph->saddr);
714 		return -1;
715 	}
716 
717 	udp_tunnel_xmit_skb(rt, gtp->sk1u, skb,
718 			    fl4.saddr, fl4.daddr,
719 			    iph->tos,
720 			    ip4_dst_hoplimit(&rt->dst),
721 			    0,
722 			    htons(GTP1U_PORT), htons(GTP1U_PORT),
723 			    !net_eq(sock_net(gtp->sk1u),
724 				    dev_net(gtp->dev)),
725 			    false,
726 			    0);
727 	return 0;
728 }
729 
730 static int gtp1u_handle_echo_resp(struct gtp_dev *gtp, struct sk_buff *skb)
731 {
732 	struct gtp1_header_long *gtp1u;
733 	struct echo_info echo;
734 	struct sk_buff *msg;
735 	struct iphdr *iph;
736 	int ret;
737 
738 	gtp1u = (struct gtp1_header_long *)(skb->data + sizeof(struct udphdr));
739 
740 	/* 3GPP TS 29.281 5.1 - For the Echo Request, Echo Response,
741 	 * Error Indication and Supported Extension Headers Notification
742 	 * messages, the S flag shall be set to 1 and TEID shall be set to 0.
743 	 */
744 	if (!(gtp1u->flags & GTP1_F_SEQ) || gtp1u->tid)
745 		return -1;
746 
747 	iph = ip_hdr(skb);
748 	echo.ms.addr.s_addr = iph->daddr;
749 	echo.peer.addr.s_addr = iph->saddr;
750 	echo.gtp_version = GTP_V1;
751 
752 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
753 	if (!msg)
754 		return -1;
755 
756 	ret = gtp_genl_fill_echo(msg, 0, 0, 0, GTP_CMD_ECHOREQ, echo);
757 	if (ret < 0) {
758 		nlmsg_free(msg);
759 		return -1;
760 	}
761 
762 	genlmsg_multicast_netns(&gtp_genl_family, dev_net(gtp->dev),
763 				msg, 0, GTP_GENL_MCGRP, GFP_ATOMIC);
764 	consume_skb(skb);
765 
766 	return 0;
767 }
768 
769 static int gtp_parse_exthdrs(struct sk_buff *skb, unsigned int *hdrlen)
770 {
771 	struct gtp_ext_hdr *gtp_exthdr, _gtp_exthdr;
772 	unsigned int offset = *hdrlen;
773 	__u8 *next_type, _next_type;
774 
775 	/* From 29.060: "The Extension Header Length field specifies the length
776 	 * of the particular Extension header in 4 octets units."
777 	 *
778 	 * This length field includes length field size itself (1 byte),
779 	 * payload (variable length) and next type (1 byte). The extension
780 	 * header is aligned to 4 bytes.
781 	 */
782 
783 	do {
784 		gtp_exthdr = skb_header_pointer(skb, offset, sizeof(*gtp_exthdr),
785 						&_gtp_exthdr);
786 		if (!gtp_exthdr || !gtp_exthdr->len)
787 			return -1;
788 
789 		offset += gtp_exthdr->len * 4;
790 
791 		/* From 29.060: "If no such Header follows, then the value of
792 		 * the Next Extension Header Type shall be 0."
793 		 */
794 		next_type = skb_header_pointer(skb, offset - 1,
795 					       sizeof(_next_type), &_next_type);
796 		if (!next_type)
797 			return -1;
798 
799 	} while (*next_type != 0);
800 
801 	*hdrlen = offset;
802 
803 	return 0;
804 }
805 
806 static int gtp1u_udp_encap_recv(struct gtp_dev *gtp, struct sk_buff *skb)
807 {
808 	unsigned int hdrlen = sizeof(struct udphdr) +
809 			      sizeof(struct gtp1_header);
810 	struct gtp1_header *gtp1;
811 	struct pdp_ctx *pctx;
812 	__u16 inner_proto;
813 
814 	if (!pskb_may_pull(skb, hdrlen))
815 		return -1;
816 
817 	gtp1 = (struct gtp1_header *)(skb->data + sizeof(struct udphdr));
818 
819 	if ((gtp1->flags >> 5) != GTP_V1)
820 		return 1;
821 
822 	/* If the sockets were created in kernel, it means that
823 	 * there is no daemon running in userspace which would
824 	 * handle echo request.
825 	 */
826 	if (gtp1->type == GTP_ECHO_REQ && gtp->sk_created)
827 		return gtp1u_send_echo_resp(gtp, skb);
828 
829 	if (gtp1->type == GTP_ECHO_RSP && gtp->sk_created)
830 		return gtp1u_handle_echo_resp(gtp, skb);
831 
832 	if (gtp1->type != GTP_TPDU)
833 		return 1;
834 
835 	/* From 29.060: "This field shall be present if and only if any one or
836 	 * more of the S, PN and E flags are set.".
837 	 *
838 	 * If any of the bit is set, then the remaining ones also have to be
839 	 * set.
840 	 */
841 	if (gtp1->flags & GTP1_F_MASK)
842 		hdrlen += 4;
843 
844 	/* Make sure the header is larger enough, including extensions. */
845 	if (!pskb_may_pull(skb, hdrlen))
846 		return -1;
847 
848 	gtp1 = (struct gtp1_header *)(skb->data + sizeof(struct udphdr));
849 
850 	if (gtp1->flags & GTP1_F_EXTHDR &&
851 	    gtp_parse_exthdrs(skb, &hdrlen) < 0)
852 		return -1;
853 
854 	if (gtp_inner_proto(skb, hdrlen, &inner_proto) < 0) {
855 		netdev_dbg(gtp->dev, "GTP packet does not encapsulate an IP packet\n");
856 		return -1;
857 	}
858 
859 	pctx = gtp1_pdp_find(gtp, ntohl(gtp1->tid),
860 			     gtp_proto_to_family(inner_proto));
861 	if (!pctx) {
862 		netdev_dbg(gtp->dev, "No PDP ctx to decap skb=%p\n", skb);
863 		return 1;
864 	}
865 
866 	return gtp_rx(pctx, skb, hdrlen, gtp->role, inner_proto);
867 }
868 
869 static void __gtp_encap_destroy(struct sock *sk)
870 {
871 	struct gtp_dev *gtp;
872 
873 	lock_sock(sk);
874 	gtp = sk->sk_user_data;
875 	if (gtp) {
876 		if (gtp->sk0 == sk)
877 			gtp->sk0 = NULL;
878 		else
879 			gtp->sk1u = NULL;
880 		WRITE_ONCE(udp_sk(sk)->encap_type, 0);
881 		rcu_assign_sk_user_data(sk, NULL);
882 		release_sock(sk);
883 		sock_put(sk);
884 		return;
885 	}
886 	release_sock(sk);
887 }
888 
889 static void gtp_encap_destroy(struct sock *sk)
890 {
891 	rtnl_lock();
892 	__gtp_encap_destroy(sk);
893 	rtnl_unlock();
894 }
895 
896 static void gtp_encap_disable_sock(struct sock *sk)
897 {
898 	if (!sk)
899 		return;
900 
901 	__gtp_encap_destroy(sk);
902 }
903 
904 static void gtp_encap_disable(struct gtp_dev *gtp)
905 {
906 	if (gtp->sk_created) {
907 		udp_tunnel_sock_release(gtp->sk0);
908 		udp_tunnel_sock_release(gtp->sk1u);
909 		gtp->sk_created = false;
910 		gtp->sk0 = NULL;
911 		gtp->sk1u = NULL;
912 	} else {
913 		gtp_encap_disable_sock(gtp->sk0);
914 		gtp_encap_disable_sock(gtp->sk1u);
915 	}
916 }
917 
918 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
919  * Return codes: 0: success, <0: error, >0: pass up to userspace UDP socket.
920  */
921 static int gtp_encap_recv(struct sock *sk, struct sk_buff *skb)
922 {
923 	struct gtp_dev *gtp;
924 	int ret = 0;
925 
926 	gtp = rcu_dereference_sk_user_data(sk);
927 	if (!gtp)
928 		return 1;
929 
930 	netdev_dbg(gtp->dev, "encap_recv sk=%p\n", sk);
931 
932 	switch (READ_ONCE(udp_sk(sk)->encap_type)) {
933 	case UDP_ENCAP_GTP0:
934 		netdev_dbg(gtp->dev, "received GTP0 packet\n");
935 		ret = gtp0_udp_encap_recv(gtp, skb);
936 		break;
937 	case UDP_ENCAP_GTP1U:
938 		netdev_dbg(gtp->dev, "received GTP1U packet\n");
939 		ret = gtp1u_udp_encap_recv(gtp, skb);
940 		break;
941 	default:
942 		ret = -1; /* Shouldn't happen. */
943 	}
944 
945 	switch (ret) {
946 	case 1:
947 		netdev_dbg(gtp->dev, "pass up to the process\n");
948 		break;
949 	case 0:
950 		break;
951 	case -1:
952 		netdev_dbg(gtp->dev, "GTP packet has been dropped\n");
953 		kfree_skb(skb);
954 		ret = 0;
955 		break;
956 	}
957 
958 	return ret;
959 }
960 
961 static void gtp_dev_uninit(struct net_device *dev)
962 {
963 	struct gtp_dev *gtp = netdev_priv(dev);
964 
965 	gtp_encap_disable(gtp);
966 }
967 
968 static inline void gtp0_push_header(struct sk_buff *skb, struct pdp_ctx *pctx)
969 {
970 	int payload_len = skb->len;
971 	struct gtp0_header *gtp0;
972 
973 	gtp0 = skb_push(skb, sizeof(*gtp0));
974 
975 	gtp0->flags	= 0x1e; /* v0, GTP-non-prime. */
976 	gtp0->type	= GTP_TPDU;
977 	gtp0->length	= htons(payload_len);
978 	gtp0->seq	= htons((atomic_inc_return(&pctx->tx_seq) - 1) % 0xffff);
979 	gtp0->flow	= htons(pctx->u.v0.flow);
980 	gtp0->number	= 0xff;
981 	gtp0->spare[0]	= gtp0->spare[1] = gtp0->spare[2] = 0xff;
982 	gtp0->tid	= cpu_to_be64(pctx->u.v0.tid);
983 }
984 
985 static inline void gtp1_push_header(struct sk_buff *skb, struct pdp_ctx *pctx)
986 {
987 	int payload_len = skb->len;
988 	struct gtp1_header *gtp1;
989 
990 	gtp1 = skb_push(skb, sizeof(*gtp1));
991 
992 	/* Bits    8  7  6  5  4  3  2	1
993 	 *	  +--+--+--+--+--+--+--+--+
994 	 *	  |version |PT| 0| E| S|PN|
995 	 *	  +--+--+--+--+--+--+--+--+
996 	 *	    0  0  1  1	1  0  0  0
997 	 */
998 	gtp1->flags	= 0x30; /* v1, GTP-non-prime. */
999 	gtp1->type	= GTP_TPDU;
1000 	gtp1->length	= htons(payload_len);
1001 	gtp1->tid	= htonl(pctx->u.v1.o_tei);
1002 
1003 	/* TODO: Support for extension header, sequence number and N-PDU.
1004 	 *	 Update the length field if any of them is available.
1005 	 */
1006 }
1007 
1008 struct gtp_pktinfo {
1009 	struct sock		*sk;
1010 	union {
1011 		struct flowi4	fl4;
1012 		struct flowi6	fl6;
1013 	};
1014 	union {
1015 		struct rtable	*rt;
1016 		struct rt6_info	*rt6;
1017 	};
1018 	struct pdp_ctx		*pctx;
1019 	struct net_device	*dev;
1020 	__u8			tos;
1021 	__be16			gtph_port;
1022 };
1023 
1024 static void gtp_push_header(struct sk_buff *skb, struct gtp_pktinfo *pktinfo)
1025 {
1026 	switch (pktinfo->pctx->gtp_version) {
1027 	case GTP_V0:
1028 		pktinfo->gtph_port = htons(GTP0_PORT);
1029 		gtp0_push_header(skb, pktinfo->pctx);
1030 		break;
1031 	case GTP_V1:
1032 		pktinfo->gtph_port = htons(GTP1U_PORT);
1033 		gtp1_push_header(skb, pktinfo->pctx);
1034 		break;
1035 	}
1036 }
1037 
1038 static inline void gtp_set_pktinfo_ipv4(struct gtp_pktinfo *pktinfo,
1039 					struct sock *sk, __u8 tos,
1040 					struct pdp_ctx *pctx, struct rtable *rt,
1041 					struct flowi4 *fl4,
1042 					struct net_device *dev)
1043 {
1044 	pktinfo->sk	= sk;
1045 	pktinfo->tos	= tos;
1046 	pktinfo->pctx	= pctx;
1047 	pktinfo->rt	= rt;
1048 	pktinfo->fl4	= *fl4;
1049 	pktinfo->dev	= dev;
1050 }
1051 
1052 static void gtp_set_pktinfo_ipv6(struct gtp_pktinfo *pktinfo,
1053 				 struct sock *sk, __u8 tos,
1054 				 struct pdp_ctx *pctx, struct rt6_info *rt6,
1055 				 struct flowi6 *fl6,
1056 				 struct net_device *dev)
1057 {
1058 	pktinfo->sk	= sk;
1059 	pktinfo->tos	= tos;
1060 	pktinfo->pctx	= pctx;
1061 	pktinfo->rt6	= rt6;
1062 	pktinfo->fl6	= *fl6;
1063 	pktinfo->dev	= dev;
1064 }
1065 
1066 static int gtp_build_skb_outer_ip4(struct sk_buff *skb, struct net_device *dev,
1067 				   struct gtp_pktinfo *pktinfo,
1068 				   struct pdp_ctx *pctx, __u8 tos,
1069 				   __be16 frag_off)
1070 {
1071 	struct rtable *rt;
1072 	struct flowi4 fl4;
1073 	__be16 df;
1074 	int mtu;
1075 
1076 	rt = ip4_route_output_gtp(&fl4, pctx->sk, pctx->peer.addr.s_addr,
1077 				  inet_sk(pctx->sk)->inet_saddr);
1078 	if (IS_ERR(rt)) {
1079 		netdev_dbg(dev, "no route to SSGN %pI4\n",
1080 			   &pctx->peer.addr.s_addr);
1081 		dev->stats.tx_carrier_errors++;
1082 		goto err;
1083 	}
1084 
1085 	if (rt->dst.dev == dev) {
1086 		netdev_dbg(dev, "circular route to SSGN %pI4\n",
1087 			   &pctx->peer.addr.s_addr);
1088 		dev->stats.collisions++;
1089 		goto err_rt;
1090 	}
1091 
1092 	/* This is similar to tnl_update_pmtu(). */
1093 	df = frag_off;
1094 	if (df) {
1095 		mtu = dst_mtu(&rt->dst) - dev->hard_header_len -
1096 			sizeof(struct iphdr) - sizeof(struct udphdr);
1097 		switch (pctx->gtp_version) {
1098 		case GTP_V0:
1099 			mtu -= sizeof(struct gtp0_header);
1100 			break;
1101 		case GTP_V1:
1102 			mtu -= sizeof(struct gtp1_header);
1103 			break;
1104 		}
1105 	} else {
1106 		mtu = dst_mtu(&rt->dst);
1107 	}
1108 
1109 	skb_dst_update_pmtu_no_confirm(skb, mtu);
1110 
1111 	if (frag_off & htons(IP_DF) &&
1112 	    ((!skb_is_gso(skb) && skb->len > mtu) ||
1113 	     (skb_is_gso(skb) && !skb_gso_validate_network_len(skb, mtu)))) {
1114 		netdev_dbg(dev, "packet too big, fragmentation needed\n");
1115 		icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
1116 			      htonl(mtu));
1117 		goto err_rt;
1118 	}
1119 
1120 	gtp_set_pktinfo_ipv4(pktinfo, pctx->sk, tos, pctx, rt, &fl4, dev);
1121 	gtp_push_header(skb, pktinfo);
1122 
1123 	return 0;
1124 err_rt:
1125 	ip_rt_put(rt);
1126 err:
1127 	return -EBADMSG;
1128 }
1129 
1130 static int gtp_build_skb_outer_ip6(struct net *net, struct sk_buff *skb,
1131 				   struct net_device *dev,
1132 				   struct gtp_pktinfo *pktinfo,
1133 				   struct pdp_ctx *pctx, __u8 tos)
1134 {
1135 	struct dst_entry *dst;
1136 	struct rt6_info *rt;
1137 	struct flowi6 fl6;
1138 	int mtu;
1139 
1140 	rt = ip6_route_output_gtp(net, &fl6, pctx->sk, &pctx->peer.addr6,
1141 				  &inet6_sk(pctx->sk)->saddr);
1142 	if (IS_ERR(rt)) {
1143 		netdev_dbg(dev, "no route to SSGN %pI6\n",
1144 			   &pctx->peer.addr6);
1145 		dev->stats.tx_carrier_errors++;
1146 		goto err;
1147 	}
1148 	dst = &rt->dst;
1149 
1150 	if (rt->dst.dev == dev) {
1151 		netdev_dbg(dev, "circular route to SSGN %pI6\n",
1152 			   &pctx->peer.addr6);
1153 		dev->stats.collisions++;
1154 		goto err_rt;
1155 	}
1156 
1157 	mtu = dst_mtu(&rt->dst) - dev->hard_header_len -
1158 		sizeof(struct ipv6hdr) - sizeof(struct udphdr);
1159 	switch (pctx->gtp_version) {
1160 	case GTP_V0:
1161 		mtu -= sizeof(struct gtp0_header);
1162 		break;
1163 	case GTP_V1:
1164 		mtu -= sizeof(struct gtp1_header);
1165 		break;
1166 	}
1167 
1168 	skb_dst_update_pmtu_no_confirm(skb, mtu);
1169 
1170 	if ((!skb_is_gso(skb) && skb->len > mtu) ||
1171 	    (skb_is_gso(skb) && !skb_gso_validate_network_len(skb, mtu))) {
1172 		netdev_dbg(dev, "packet too big, fragmentation needed\n");
1173 		icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1174 		goto err_rt;
1175 	}
1176 
1177 	gtp_set_pktinfo_ipv6(pktinfo, pctx->sk, tos, pctx, rt, &fl6, dev);
1178 	gtp_push_header(skb, pktinfo);
1179 
1180 	return 0;
1181 err_rt:
1182 	dst_release(dst);
1183 err:
1184 	return -EBADMSG;
1185 }
1186 
1187 static int gtp_build_skb_ip4(struct sk_buff *skb, struct net_device *dev,
1188 			     struct gtp_pktinfo *pktinfo)
1189 {
1190 	struct gtp_dev *gtp = netdev_priv(dev);
1191 	struct net *net = gtp->net;
1192 	struct pdp_ctx *pctx;
1193 	struct iphdr *iph;
1194 	int ret;
1195 
1196 	/* Read the IP destination address and resolve the PDP context.
1197 	 * Prepend PDP header with TEI/TID from PDP ctx.
1198 	 */
1199 	iph = ip_hdr(skb);
1200 	if (gtp->role == GTP_ROLE_SGSN)
1201 		pctx = ipv4_pdp_find(gtp, iph->saddr);
1202 	else
1203 		pctx = ipv4_pdp_find(gtp, iph->daddr);
1204 
1205 	if (!pctx) {
1206 		netdev_dbg(dev, "no PDP ctx found for %pI4, skip\n",
1207 			   &iph->daddr);
1208 		return -ENOENT;
1209 	}
1210 	netdev_dbg(dev, "found PDP context %p\n", pctx);
1211 
1212 	switch (pctx->sk->sk_family) {
1213 	case AF_INET:
1214 		ret = gtp_build_skb_outer_ip4(skb, dev, pktinfo, pctx,
1215 					      iph->tos, iph->frag_off);
1216 		break;
1217 	case AF_INET6:
1218 		ret = gtp_build_skb_outer_ip6(net, skb, dev, pktinfo, pctx,
1219 					      iph->tos);
1220 		break;
1221 	default:
1222 		ret = -1;
1223 		WARN_ON_ONCE(1);
1224 		break;
1225 	}
1226 
1227 	if (ret < 0)
1228 		return ret;
1229 
1230 	netdev_dbg(dev, "gtp -> IP src: %pI4 dst: %pI4\n",
1231 		   &iph->saddr, &iph->daddr);
1232 
1233 	return 0;
1234 }
1235 
1236 static int gtp_build_skb_ip6(struct sk_buff *skb, struct net_device *dev,
1237 			     struct gtp_pktinfo *pktinfo)
1238 {
1239 	struct gtp_dev *gtp = netdev_priv(dev);
1240 	struct net *net = gtp->net;
1241 	struct pdp_ctx *pctx;
1242 	struct ipv6hdr *ip6h;
1243 	__u8 tos;
1244 	int ret;
1245 
1246 	/* Read the IP destination address and resolve the PDP context.
1247 	 * Prepend PDP header with TEI/TID from PDP ctx.
1248 	 */
1249 	ip6h = ipv6_hdr(skb);
1250 	if (gtp->role == GTP_ROLE_SGSN)
1251 		pctx = ipv6_pdp_find(gtp, &ip6h->saddr);
1252 	else
1253 		pctx = ipv6_pdp_find(gtp, &ip6h->daddr);
1254 
1255 	if (!pctx) {
1256 		netdev_dbg(dev, "no PDP ctx found for %pI6, skip\n",
1257 			   &ip6h->daddr);
1258 		return -ENOENT;
1259 	}
1260 	netdev_dbg(dev, "found PDP context %p\n", pctx);
1261 
1262 	tos = ipv6_get_dsfield(ip6h);
1263 
1264 	switch (pctx->sk->sk_family) {
1265 	case AF_INET:
1266 		ret = gtp_build_skb_outer_ip4(skb, dev, pktinfo, pctx, tos, 0);
1267 		break;
1268 	case AF_INET6:
1269 		ret = gtp_build_skb_outer_ip6(net, skb, dev, pktinfo, pctx, tos);
1270 		break;
1271 	default:
1272 		ret = -1;
1273 		WARN_ON_ONCE(1);
1274 		break;
1275 	}
1276 
1277 	if (ret < 0)
1278 		return ret;
1279 
1280 	netdev_dbg(dev, "gtp -> IP src: %pI6 dst: %pI6\n",
1281 		   &ip6h->saddr, &ip6h->daddr);
1282 
1283 	return 0;
1284 }
1285 
1286 static netdev_tx_t gtp_dev_xmit(struct sk_buff *skb, struct net_device *dev)
1287 {
1288 	unsigned int proto = ntohs(skb->protocol);
1289 	struct gtp_pktinfo pktinfo;
1290 	int err;
1291 
1292 	/* Ensure there is sufficient headroom. */
1293 	if (skb_cow_head(skb, dev->needed_headroom))
1294 		goto tx_err;
1295 
1296 	if (!pskb_inet_may_pull(skb))
1297 		goto tx_err;
1298 
1299 	skb_reset_inner_headers(skb);
1300 
1301 	/* PDP context lookups in gtp_build_skb_*() need rcu read-side lock. */
1302 	rcu_read_lock();
1303 	switch (proto) {
1304 	case ETH_P_IP:
1305 		err = gtp_build_skb_ip4(skb, dev, &pktinfo);
1306 		break;
1307 	case ETH_P_IPV6:
1308 		err = gtp_build_skb_ip6(skb, dev, &pktinfo);
1309 		break;
1310 	default:
1311 		err = -EOPNOTSUPP;
1312 		break;
1313 	}
1314 	rcu_read_unlock();
1315 
1316 	if (err < 0)
1317 		goto tx_err;
1318 
1319 	switch (pktinfo.pctx->sk->sk_family) {
1320 	case AF_INET:
1321 		udp_tunnel_xmit_skb(pktinfo.rt, pktinfo.sk, skb,
1322 				    pktinfo.fl4.saddr, pktinfo.fl4.daddr,
1323 				    pktinfo.tos,
1324 				    ip4_dst_hoplimit(&pktinfo.rt->dst),
1325 				    0,
1326 				    pktinfo.gtph_port, pktinfo.gtph_port,
1327 				    !net_eq(sock_net(pktinfo.pctx->sk),
1328 					    dev_net(dev)),
1329 				    false, 0);
1330 		break;
1331 	case AF_INET6:
1332 #if IS_ENABLED(CONFIG_IPV6)
1333 		udp_tunnel6_xmit_skb(&pktinfo.rt6->dst, pktinfo.sk, skb, dev,
1334 				     &pktinfo.fl6.saddr, &pktinfo.fl6.daddr,
1335 				     pktinfo.tos,
1336 				     ip6_dst_hoplimit(&pktinfo.rt->dst),
1337 				     0,
1338 				     pktinfo.gtph_port, pktinfo.gtph_port,
1339 				     false, 0);
1340 #else
1341 		goto tx_err;
1342 #endif
1343 		break;
1344 	}
1345 
1346 	return NETDEV_TX_OK;
1347 tx_err:
1348 	dev->stats.tx_errors++;
1349 	dev_kfree_skb(skb);
1350 	return NETDEV_TX_OK;
1351 }
1352 
1353 static const struct net_device_ops gtp_netdev_ops = {
1354 	.ndo_uninit		= gtp_dev_uninit,
1355 	.ndo_start_xmit		= gtp_dev_xmit,
1356 };
1357 
1358 static const struct device_type gtp_type = {
1359 	.name = "gtp",
1360 };
1361 
1362 #define GTP_TH_MAXLEN	(sizeof(struct udphdr) + sizeof(struct gtp0_header))
1363 #define GTP_IPV4_MAXLEN	(sizeof(struct iphdr) + GTP_TH_MAXLEN)
1364 
1365 static void gtp_link_setup(struct net_device *dev)
1366 {
1367 	struct gtp_dev *gtp = netdev_priv(dev);
1368 
1369 	dev->netdev_ops		= &gtp_netdev_ops;
1370 	dev->needs_free_netdev	= true;
1371 	SET_NETDEV_DEVTYPE(dev, &gtp_type);
1372 
1373 	dev->hard_header_len = 0;
1374 	dev->addr_len = 0;
1375 	dev->mtu = ETH_DATA_LEN - GTP_IPV4_MAXLEN;
1376 
1377 	/* Zero header length. */
1378 	dev->type = ARPHRD_NONE;
1379 	dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
1380 
1381 	dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
1382 	dev->priv_flags	|= IFF_NO_QUEUE;
1383 	dev->lltx = true;
1384 	netif_keep_dst(dev);
1385 
1386 	dev->needed_headroom	= LL_MAX_HEADER + GTP_IPV4_MAXLEN;
1387 	gtp->dev = dev;
1388 }
1389 
1390 static int gtp_hashtable_new(struct gtp_dev *gtp, int hsize);
1391 static int gtp_encap_enable(struct gtp_dev *gtp, struct nlattr *data[]);
1392 
1393 static void gtp_destructor(struct net_device *dev)
1394 {
1395 	struct gtp_dev *gtp = netdev_priv(dev);
1396 
1397 	kfree(gtp->addr_hash);
1398 	kfree(gtp->tid_hash);
1399 }
1400 
1401 static int gtp_sock_udp_config(struct udp_port_cfg *udp_conf,
1402 			       const struct nlattr *nla, int family)
1403 {
1404 	udp_conf->family = family;
1405 
1406 	switch (udp_conf->family) {
1407 	case AF_INET:
1408 		udp_conf->local_ip.s_addr = nla_get_be32(nla);
1409 		break;
1410 #if IS_ENABLED(CONFIG_IPV6)
1411 	case AF_INET6:
1412 		udp_conf->local_ip6 = nla_get_in6_addr(nla);
1413 		break;
1414 #endif
1415 	default:
1416 		return -EOPNOTSUPP;
1417 	}
1418 
1419 	return 0;
1420 }
1421 
1422 static struct sock *gtp_create_sock(int type, struct gtp_dev *gtp,
1423 				    const struct nlattr *nla, int family)
1424 {
1425 	struct udp_tunnel_sock_cfg tuncfg = {};
1426 	struct udp_port_cfg udp_conf = {};
1427 	struct net *net = gtp->net;
1428 	struct socket *sock;
1429 	int err;
1430 
1431 	if (nla) {
1432 		err = gtp_sock_udp_config(&udp_conf, nla, family);
1433 		if (err < 0)
1434 			return ERR_PTR(err);
1435 	} else {
1436 		udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
1437 		udp_conf.family = AF_INET;
1438 	}
1439 
1440 	if (type == UDP_ENCAP_GTP0)
1441 		udp_conf.local_udp_port = htons(GTP0_PORT);
1442 	else if (type == UDP_ENCAP_GTP1U)
1443 		udp_conf.local_udp_port = htons(GTP1U_PORT);
1444 	else
1445 		return ERR_PTR(-EINVAL);
1446 
1447 	err = udp_sock_create(net, &udp_conf, &sock);
1448 	if (err)
1449 		return ERR_PTR(err);
1450 
1451 	tuncfg.sk_user_data = gtp;
1452 	tuncfg.encap_type = type;
1453 	tuncfg.encap_rcv = gtp_encap_recv;
1454 	tuncfg.encap_destroy = NULL;
1455 
1456 	setup_udp_tunnel_sock(net, sock->sk, &tuncfg);
1457 
1458 	return sock->sk;
1459 }
1460 
1461 static int gtp_create_sockets(struct gtp_dev *gtp, const struct nlattr *nla,
1462 			      int family)
1463 {
1464 	struct sock *sk1u;
1465 	struct sock *sk0;
1466 
1467 	sk0 = gtp_create_sock(UDP_ENCAP_GTP0, gtp, nla, family);
1468 	if (IS_ERR(sk0))
1469 		return PTR_ERR(sk0);
1470 
1471 	sk1u = gtp_create_sock(UDP_ENCAP_GTP1U, gtp, nla, family);
1472 	if (IS_ERR(sk1u)) {
1473 		udp_tunnel_sock_release(sk0);
1474 		return PTR_ERR(sk1u);
1475 	}
1476 
1477 	gtp->sk_created = true;
1478 	gtp->sk0 = sk0;
1479 	gtp->sk1u = sk1u;
1480 
1481 	return 0;
1482 }
1483 
1484 #define GTP_TH_MAXLEN	(sizeof(struct udphdr) + sizeof(struct gtp0_header))
1485 #define GTP_IPV6_MAXLEN	(sizeof(struct ipv6hdr) + GTP_TH_MAXLEN)
1486 
1487 static int gtp_newlink(struct net_device *dev,
1488 		       struct rtnl_newlink_params *params,
1489 		       struct netlink_ext_ack *extack)
1490 {
1491 	struct net *link_net = rtnl_newlink_link_net(params);
1492 	struct nlattr **data = params->data;
1493 	unsigned int role = GTP_ROLE_GGSN;
1494 	struct gtp_dev *gtp;
1495 	struct gtp_net *gn;
1496 	int hashsize, err;
1497 
1498 #if !IS_ENABLED(CONFIG_IPV6)
1499 	if (data[IFLA_GTP_LOCAL6])
1500 		return -EAFNOSUPPORT;
1501 #endif
1502 
1503 	gtp = netdev_priv(dev);
1504 
1505 	if (!data[IFLA_GTP_PDP_HASHSIZE]) {
1506 		hashsize = 1024;
1507 	} else {
1508 		hashsize = nla_get_u32(data[IFLA_GTP_PDP_HASHSIZE]);
1509 		if (!hashsize)
1510 			hashsize = 1024;
1511 	}
1512 
1513 	if (data[IFLA_GTP_ROLE]) {
1514 		role = nla_get_u32(data[IFLA_GTP_ROLE]);
1515 		if (role > GTP_ROLE_SGSN)
1516 			return -EINVAL;
1517 	}
1518 	gtp->role = role;
1519 
1520 	gtp->restart_count = nla_get_u8_default(data[IFLA_GTP_RESTART_COUNT],
1521 						0);
1522 
1523 	gtp->net = link_net;
1524 
1525 	err = gtp_hashtable_new(gtp, hashsize);
1526 	if (err < 0)
1527 		return err;
1528 
1529 	if (data[IFLA_GTP_CREATE_SOCKETS]) {
1530 		if (data[IFLA_GTP_LOCAL6])
1531 			err = gtp_create_sockets(gtp, data[IFLA_GTP_LOCAL6], AF_INET6);
1532 		else
1533 			err = gtp_create_sockets(gtp, data[IFLA_GTP_LOCAL], AF_INET);
1534 	} else {
1535 		err = gtp_encap_enable(gtp, data);
1536 	}
1537 
1538 	if (err < 0)
1539 		goto out_hashtable;
1540 
1541 	if ((gtp->sk0 && gtp->sk0->sk_family == AF_INET6) ||
1542 	    (gtp->sk1u && gtp->sk1u->sk_family == AF_INET6)) {
1543 		dev->mtu = ETH_DATA_LEN - GTP_IPV6_MAXLEN;
1544 		dev->needed_headroom = LL_MAX_HEADER + GTP_IPV6_MAXLEN;
1545 	}
1546 
1547 	err = register_netdevice(dev);
1548 	if (err < 0) {
1549 		netdev_dbg(dev, "failed to register new netdev %d\n", err);
1550 		goto out_encap;
1551 	}
1552 
1553 	gn = net_generic(link_net, gtp_net_id);
1554 	list_add(&gtp->list, &gn->gtp_dev_list);
1555 	dev->priv_destructor = gtp_destructor;
1556 
1557 	netdev_dbg(dev, "registered new GTP interface\n");
1558 
1559 	return 0;
1560 
1561 out_encap:
1562 	gtp_encap_disable(gtp);
1563 out_hashtable:
1564 	/* Wait for RCU readers that may still reference this gtp_dev. */
1565 	synchronize_net();
1566 	kfree(gtp->addr_hash);
1567 	kfree(gtp->tid_hash);
1568 	return err;
1569 }
1570 
1571 static void gtp_dellink(struct net_device *dev, struct list_head *head)
1572 {
1573 	struct gtp_dev *gtp = netdev_priv(dev);
1574 	struct hlist_node *next;
1575 	struct pdp_ctx *pctx;
1576 	int i;
1577 
1578 	mutex_lock(&gtp_pdp_lock);
1579 	for (i = 0; i < gtp->hash_size; i++)
1580 		hlist_for_each_entry_safe(pctx, next, &gtp->tid_hash[i], hlist_tid)
1581 			pdp_context_delete(pctx);
1582 	mutex_unlock(&gtp_pdp_lock);
1583 
1584 	list_del(&gtp->list);
1585 	unregister_netdevice_queue(dev, head);
1586 }
1587 
1588 static const struct nla_policy gtp_policy[IFLA_GTP_MAX + 1] = {
1589 	[IFLA_GTP_FD0]			= { .type = NLA_U32 },
1590 	[IFLA_GTP_FD1]			= { .type = NLA_U32 },
1591 	[IFLA_GTP_PDP_HASHSIZE]		= { .type = NLA_U32 },
1592 	[IFLA_GTP_ROLE]			= { .type = NLA_U32 },
1593 	[IFLA_GTP_CREATE_SOCKETS]	= { .type = NLA_U8 },
1594 	[IFLA_GTP_RESTART_COUNT]	= { .type = NLA_U8 },
1595 	[IFLA_GTP_LOCAL]		= { .type = NLA_U32 },
1596 	[IFLA_GTP_LOCAL6]		= { .len = sizeof(struct in6_addr) },
1597 };
1598 
1599 static int gtp_validate(struct nlattr *tb[], struct nlattr *data[],
1600 			struct netlink_ext_ack *extack)
1601 {
1602 	if (!data)
1603 		return -EINVAL;
1604 
1605 	return 0;
1606 }
1607 
1608 static size_t gtp_get_size(const struct net_device *dev)
1609 {
1610 	return nla_total_size(sizeof(__u32)) + /* IFLA_GTP_PDP_HASHSIZE */
1611 		nla_total_size(sizeof(__u32)) + /* IFLA_GTP_ROLE */
1612 		nla_total_size(sizeof(__u8)); /* IFLA_GTP_RESTART_COUNT */
1613 }
1614 
1615 static int gtp_fill_info(struct sk_buff *skb, const struct net_device *dev)
1616 {
1617 	struct gtp_dev *gtp = netdev_priv(dev);
1618 
1619 	if (nla_put_u32(skb, IFLA_GTP_PDP_HASHSIZE, gtp->hash_size))
1620 		goto nla_put_failure;
1621 	if (nla_put_u32(skb, IFLA_GTP_ROLE, gtp->role))
1622 		goto nla_put_failure;
1623 	if (nla_put_u8(skb, IFLA_GTP_RESTART_COUNT, gtp->restart_count))
1624 		goto nla_put_failure;
1625 
1626 	return 0;
1627 
1628 nla_put_failure:
1629 	return -EMSGSIZE;
1630 }
1631 
1632 static struct rtnl_link_ops gtp_link_ops __read_mostly = {
1633 	.kind		= "gtp",
1634 	.maxtype	= IFLA_GTP_MAX,
1635 	.policy		= gtp_policy,
1636 	.priv_size	= sizeof(struct gtp_dev),
1637 	.setup		= gtp_link_setup,
1638 	.validate	= gtp_validate,
1639 	.newlink	= gtp_newlink,
1640 	.dellink	= gtp_dellink,
1641 	.get_size	= gtp_get_size,
1642 	.fill_info	= gtp_fill_info,
1643 };
1644 
1645 static int gtp_hashtable_new(struct gtp_dev *gtp, int hsize)
1646 {
1647 	int i;
1648 
1649 	gtp->addr_hash = kmalloc_objs(struct hlist_head, hsize,
1650 				      GFP_KERNEL | __GFP_NOWARN);
1651 	if (gtp->addr_hash == NULL)
1652 		return -ENOMEM;
1653 
1654 	gtp->tid_hash = kmalloc_objs(struct hlist_head, hsize,
1655 				     GFP_KERNEL | __GFP_NOWARN);
1656 	if (gtp->tid_hash == NULL)
1657 		goto err1;
1658 
1659 	gtp->hash_size = hsize;
1660 
1661 	for (i = 0; i < hsize; i++) {
1662 		INIT_HLIST_HEAD(&gtp->addr_hash[i]);
1663 		INIT_HLIST_HEAD(&gtp->tid_hash[i]);
1664 	}
1665 	return 0;
1666 err1:
1667 	kfree(gtp->addr_hash);
1668 	return -ENOMEM;
1669 }
1670 
1671 static struct sock *gtp_encap_enable_socket(int fd, int type,
1672 					    struct gtp_dev *gtp)
1673 {
1674 	struct udp_tunnel_sock_cfg tuncfg = {NULL};
1675 	struct socket *sock;
1676 	struct sock *sk;
1677 	int err;
1678 
1679 	pr_debug("enable gtp on %d, %d\n", fd, type);
1680 
1681 	sock = sockfd_lookup(fd, &err);
1682 	if (!sock) {
1683 		pr_debug("gtp socket fd=%d not found\n", fd);
1684 		return ERR_PTR(err);
1685 	}
1686 
1687 	sk = sock->sk;
1688 	if (sk->sk_protocol != IPPROTO_UDP ||
1689 	    sk->sk_type != SOCK_DGRAM ||
1690 	    (sk->sk_family != AF_INET && sk->sk_family != AF_INET6)) {
1691 		pr_debug("socket fd=%d not UDP\n", fd);
1692 		sk = ERR_PTR(-EINVAL);
1693 		goto out_sock;
1694 	}
1695 
1696 	if (sk->sk_family == AF_INET6 &&
1697 	    !sk->sk_ipv6only) {
1698 		sk = ERR_PTR(-EADDRNOTAVAIL);
1699 		goto out_sock;
1700 	}
1701 
1702 	lock_sock(sk);
1703 	if (sk->sk_user_data) {
1704 		sk = ERR_PTR(-EBUSY);
1705 		goto out_rel_sock;
1706 	}
1707 
1708 	sock_hold(sk);
1709 
1710 	tuncfg.sk_user_data = gtp;
1711 	tuncfg.encap_type = type;
1712 	tuncfg.encap_rcv = gtp_encap_recv;
1713 	tuncfg.encap_destroy = gtp_encap_destroy;
1714 
1715 	setup_udp_tunnel_sock(sock_net(sock->sk), sk, &tuncfg);
1716 
1717 out_rel_sock:
1718 	release_sock(sock->sk);
1719 out_sock:
1720 	sockfd_put(sock);
1721 	return sk;
1722 }
1723 
1724 static int gtp_encap_enable(struct gtp_dev *gtp, struct nlattr *data[])
1725 {
1726 	struct sock *sk1u = NULL;
1727 	struct sock *sk0 = NULL;
1728 
1729 	if (!data[IFLA_GTP_FD0] && !data[IFLA_GTP_FD1])
1730 		return -EINVAL;
1731 
1732 	if (data[IFLA_GTP_FD0]) {
1733 		int fd0 = nla_get_u32(data[IFLA_GTP_FD0]);
1734 
1735 		if (fd0 >= 0) {
1736 			sk0 = gtp_encap_enable_socket(fd0, UDP_ENCAP_GTP0, gtp);
1737 			if (IS_ERR(sk0))
1738 				return PTR_ERR(sk0);
1739 		}
1740 	}
1741 
1742 	if (data[IFLA_GTP_FD1]) {
1743 		int fd1 = nla_get_u32(data[IFLA_GTP_FD1]);
1744 
1745 		if (fd1 >= 0) {
1746 			sk1u = gtp_encap_enable_socket(fd1, UDP_ENCAP_GTP1U, gtp);
1747 			if (IS_ERR(sk1u)) {
1748 				gtp_encap_disable_sock(sk0);
1749 				return PTR_ERR(sk1u);
1750 			}
1751 		}
1752 	}
1753 
1754 	gtp->sk0 = sk0;
1755 	gtp->sk1u = sk1u;
1756 
1757 	if (sk0 && sk1u &&
1758 	    sk0->sk_family != sk1u->sk_family) {
1759 		gtp_encap_disable_sock(sk0);
1760 		gtp_encap_disable_sock(sk1u);
1761 		return -EINVAL;
1762 	}
1763 
1764 	return 0;
1765 }
1766 
1767 static struct gtp_dev *gtp_find_dev(struct net *src_net, struct nlattr *nla[])
1768 {
1769 	struct gtp_dev *gtp = NULL;
1770 	struct net_device *dev;
1771 	struct net *net;
1772 
1773 	/* Examine the link attributes and figure out which network namespace
1774 	 * we are talking about.
1775 	 */
1776 	if (nla[GTPA_NET_NS_FD])
1777 		net = get_net_ns_by_fd(nla_get_u32(nla[GTPA_NET_NS_FD]));
1778 	else
1779 		net = get_net(src_net);
1780 
1781 	if (IS_ERR(net))
1782 		return NULL;
1783 
1784 	/* Check if there's an existing gtpX device to configure */
1785 	dev = dev_get_by_index_rcu(net, nla_get_u32(nla[GTPA_LINK]));
1786 	if (dev && dev->netdev_ops == &gtp_netdev_ops)
1787 		gtp = netdev_priv(dev);
1788 
1789 	put_net(net);
1790 	return gtp;
1791 }
1792 
1793 static void gtp_pdp_fill(struct pdp_ctx *pctx, struct genl_info *info)
1794 {
1795 	pctx->gtp_version = nla_get_u32(info->attrs[GTPA_VERSION]);
1796 
1797 	switch (pctx->gtp_version) {
1798 	case GTP_V0:
1799 		/* According to TS 09.60, sections 7.5.1 and 7.5.2, the flow
1800 		 * label needs to be the same for uplink and downlink packets,
1801 		 * so let's annotate this.
1802 		 */
1803 		pctx->u.v0.tid = nla_get_u64(info->attrs[GTPA_TID]);
1804 		pctx->u.v0.flow = nla_get_u16(info->attrs[GTPA_FLOW]);
1805 		break;
1806 	case GTP_V1:
1807 		pctx->u.v1.i_tei = nla_get_u32(info->attrs[GTPA_I_TEI]);
1808 		pctx->u.v1.o_tei = nla_get_u32(info->attrs[GTPA_O_TEI]);
1809 		break;
1810 	default:
1811 		break;
1812 	}
1813 }
1814 
1815 static void ip_pdp_peer_fill(struct pdp_ctx *pctx, struct genl_info *info)
1816 {
1817 	if (info->attrs[GTPA_PEER_ADDRESS]) {
1818 		pctx->peer.addr.s_addr =
1819 			nla_get_be32(info->attrs[GTPA_PEER_ADDRESS]);
1820 	} else if (info->attrs[GTPA_PEER_ADDR6]) {
1821 		pctx->peer.addr6 = nla_get_in6_addr(info->attrs[GTPA_PEER_ADDR6]);
1822 	}
1823 }
1824 
1825 static void ipv4_pdp_fill(struct pdp_ctx *pctx, struct genl_info *info)
1826 {
1827 	ip_pdp_peer_fill(pctx, info);
1828 	pctx->ms.addr.s_addr =
1829 		nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
1830 	gtp_pdp_fill(pctx, info);
1831 }
1832 
1833 static bool ipv6_pdp_fill(struct pdp_ctx *pctx, struct genl_info *info)
1834 {
1835 	ip_pdp_peer_fill(pctx, info);
1836 	pctx->ms.addr6 = nla_get_in6_addr(info->attrs[GTPA_MS_ADDR6]);
1837 	if (pctx->ms.addr6.s6_addr32[2] ||
1838 	    pctx->ms.addr6.s6_addr32[3])
1839 		return false;
1840 
1841 	gtp_pdp_fill(pctx, info);
1842 
1843 	return true;
1844 }
1845 
1846 static struct pdp_ctx *gtp_pdp_add(struct gtp_dev *gtp, struct sock *sk,
1847 				   struct genl_info *info)
1848 {
1849 	struct pdp_ctx *pctx, *pctx_tid = NULL;
1850 	struct net_device *dev = gtp->dev;
1851 	u32 hash_ms, hash_tid = 0;
1852 	struct in6_addr ms_addr6;
1853 	unsigned int version;
1854 	bool found = false;
1855 	__be32 ms_addr;
1856 	int family;
1857 
1858 	version = nla_get_u32(info->attrs[GTPA_VERSION]);
1859 
1860 	family = nla_get_u8_default(info->attrs[GTPA_FAMILY], AF_INET);
1861 
1862 #if !IS_ENABLED(CONFIG_IPV6)
1863 	if (family == AF_INET6)
1864 		return ERR_PTR(-EAFNOSUPPORT);
1865 #endif
1866 	if (!info->attrs[GTPA_PEER_ADDRESS] &&
1867 	    !info->attrs[GTPA_PEER_ADDR6])
1868 		return ERR_PTR(-EINVAL);
1869 
1870 	if ((info->attrs[GTPA_PEER_ADDRESS] &&
1871 	     sk->sk_family == AF_INET6) ||
1872 	    (info->attrs[GTPA_PEER_ADDR6] &&
1873 	     sk->sk_family == AF_INET))
1874 		return ERR_PTR(-EAFNOSUPPORT);
1875 
1876 	switch (family) {
1877 	case AF_INET:
1878 		if (!info->attrs[GTPA_MS_ADDRESS] ||
1879 		    info->attrs[GTPA_MS_ADDR6])
1880 			return ERR_PTR(-EINVAL);
1881 
1882 		ms_addr = nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
1883 		hash_ms = ipv4_hashfn(ms_addr) % gtp->hash_size;
1884 		pctx = ipv4_pdp_find(gtp, ms_addr);
1885 		break;
1886 	case AF_INET6:
1887 		if (!info->attrs[GTPA_MS_ADDR6] ||
1888 		    info->attrs[GTPA_MS_ADDRESS])
1889 			return ERR_PTR(-EINVAL);
1890 
1891 		ms_addr6 = nla_get_in6_addr(info->attrs[GTPA_MS_ADDR6]);
1892 		hash_ms = ipv6_hashfn(&ms_addr6) % gtp->hash_size;
1893 		pctx = ipv6_pdp_find(gtp, &ms_addr6);
1894 		break;
1895 	default:
1896 		return ERR_PTR(-EAFNOSUPPORT);
1897 	}
1898 	if (pctx)
1899 		found = true;
1900 	if (version == GTP_V0)
1901 		pctx_tid = gtp0_pdp_find(gtp,
1902 					 nla_get_u64(info->attrs[GTPA_TID]),
1903 					 family);
1904 	else if (version == GTP_V1)
1905 		pctx_tid = gtp1_pdp_find(gtp,
1906 					 nla_get_u32(info->attrs[GTPA_I_TEI]),
1907 					 family);
1908 	if (pctx_tid)
1909 		found = true;
1910 
1911 	if (found) {
1912 		if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
1913 			return ERR_PTR(-EEXIST);
1914 		if (info->nlhdr->nlmsg_flags & NLM_F_REPLACE)
1915 			return ERR_PTR(-EOPNOTSUPP);
1916 
1917 		if (pctx && pctx_tid)
1918 			return ERR_PTR(-EEXIST);
1919 		if (!pctx)
1920 			pctx = pctx_tid;
1921 
1922 		switch (pctx->af) {
1923 		case AF_INET:
1924 			ipv4_pdp_fill(pctx, info);
1925 			break;
1926 		case AF_INET6:
1927 			if (!ipv6_pdp_fill(pctx, info))
1928 				return ERR_PTR(-EADDRNOTAVAIL);
1929 			break;
1930 		}
1931 
1932 		if (pctx->gtp_version == GTP_V0)
1933 			netdev_dbg(dev, "GTPv0-U: update tunnel id = %llx (pdp %p)\n",
1934 				   pctx->u.v0.tid, pctx);
1935 		else if (pctx->gtp_version == GTP_V1)
1936 			netdev_dbg(dev, "GTPv1-U: update tunnel id = %x/%x (pdp %p)\n",
1937 				   pctx->u.v1.i_tei, pctx->u.v1.o_tei, pctx);
1938 
1939 		return pctx;
1940 
1941 	}
1942 
1943 	pctx = kmalloc_obj(*pctx, GFP_ATOMIC);
1944 	if (pctx == NULL)
1945 		return ERR_PTR(-ENOMEM);
1946 
1947 	sock_hold(sk);
1948 	pctx->sk = sk;
1949 	pctx->dev = gtp->dev;
1950 	pctx->af = family;
1951 
1952 	switch (pctx->af) {
1953 	case AF_INET:
1954 		if (!info->attrs[GTPA_MS_ADDRESS]) {
1955 			sock_put(sk);
1956 			kfree(pctx);
1957 			return ERR_PTR(-EINVAL);
1958 		}
1959 
1960 		ipv4_pdp_fill(pctx, info);
1961 		break;
1962 	case AF_INET6:
1963 		if (!info->attrs[GTPA_MS_ADDR6]) {
1964 			sock_put(sk);
1965 			kfree(pctx);
1966 			return ERR_PTR(-EINVAL);
1967 		}
1968 
1969 		if (!ipv6_pdp_fill(pctx, info)) {
1970 			sock_put(sk);
1971 			kfree(pctx);
1972 			return ERR_PTR(-EADDRNOTAVAIL);
1973 		}
1974 		break;
1975 	}
1976 	atomic_set(&pctx->tx_seq, 0);
1977 
1978 	switch (pctx->gtp_version) {
1979 	case GTP_V0:
1980 		/* TS 09.60: "The flow label identifies unambiguously a GTP
1981 		 * flow.". We use the tid for this instead, I cannot find a
1982 		 * situation in which this doesn't unambiguosly identify the
1983 		 * PDP context.
1984 		 */
1985 		hash_tid = gtp0_hashfn(pctx->u.v0.tid) % gtp->hash_size;
1986 		break;
1987 	case GTP_V1:
1988 		hash_tid = gtp1u_hashfn(pctx->u.v1.i_tei) % gtp->hash_size;
1989 		break;
1990 	}
1991 
1992 	hlist_add_head_rcu(&pctx->hlist_addr, &gtp->addr_hash[hash_ms]);
1993 	hlist_add_head_rcu(&pctx->hlist_tid, &gtp->tid_hash[hash_tid]);
1994 
1995 	switch (pctx->gtp_version) {
1996 	case GTP_V0:
1997 		netdev_dbg(dev, "GTPv0-U: new PDP ctx id=%llx ssgn=%pI4 ms=%pI4 (pdp=%p)\n",
1998 			   pctx->u.v0.tid, &pctx->peer.addr,
1999 			   &pctx->ms.addr, pctx);
2000 		break;
2001 	case GTP_V1:
2002 		netdev_dbg(dev, "GTPv1-U: new PDP ctx id=%x/%x ssgn=%pI4 ms=%pI4 (pdp=%p)\n",
2003 			   pctx->u.v1.i_tei, pctx->u.v1.o_tei,
2004 			   &pctx->peer.addr, &pctx->ms.addr, pctx);
2005 		break;
2006 	}
2007 
2008 	return pctx;
2009 }
2010 
2011 static void pdp_context_free(struct rcu_head *head)
2012 {
2013 	struct pdp_ctx *pctx = container_of(head, struct pdp_ctx, rcu_head);
2014 
2015 	sock_put(pctx->sk);
2016 	kfree(pctx);
2017 }
2018 
2019 static void pdp_context_delete(struct pdp_ctx *pctx)
2020 {
2021 	hlist_del_rcu(&pctx->hlist_tid);
2022 	hlist_del_rcu(&pctx->hlist_addr);
2023 	call_rcu(&pctx->rcu_head, pdp_context_free);
2024 }
2025 
2026 static int gtp_tunnel_notify(struct pdp_ctx *pctx, u8 cmd, gfp_t allocation);
2027 
2028 static int gtp_genl_new_pdp(struct sk_buff *skb, struct genl_info *info)
2029 {
2030 	unsigned int version;
2031 	struct pdp_ctx *pctx;
2032 	struct gtp_dev *gtp;
2033 	struct sock *sk;
2034 	int err;
2035 
2036 	if (!info->attrs[GTPA_VERSION] ||
2037 	    !info->attrs[GTPA_LINK])
2038 		return -EINVAL;
2039 
2040 	version = nla_get_u32(info->attrs[GTPA_VERSION]);
2041 
2042 	switch (version) {
2043 	case GTP_V0:
2044 		if (!info->attrs[GTPA_TID] ||
2045 		    !info->attrs[GTPA_FLOW])
2046 			return -EINVAL;
2047 		break;
2048 	case GTP_V1:
2049 		if (!info->attrs[GTPA_I_TEI] ||
2050 		    !info->attrs[GTPA_O_TEI])
2051 			return -EINVAL;
2052 		break;
2053 
2054 	default:
2055 		return -EINVAL;
2056 	}
2057 
2058 	rtnl_lock();
2059 
2060 	/* the rtnl lock keeps the device alive once found */
2061 	rcu_read_lock();
2062 	gtp = gtp_find_dev(sock_net(skb->sk), info->attrs);
2063 	rcu_read_unlock();
2064 	if (!gtp) {
2065 		err = -ENODEV;
2066 		goto out_unlock;
2067 	}
2068 
2069 	if (version == GTP_V0)
2070 		sk = gtp->sk0;
2071 	else if (version == GTP_V1)
2072 		sk = gtp->sk1u;
2073 	else
2074 		sk = NULL;
2075 
2076 	if (!sk) {
2077 		err = -ENODEV;
2078 		goto out_unlock;
2079 	}
2080 
2081 	mutex_lock(&gtp_pdp_lock);
2082 	pctx = gtp_pdp_add(gtp, sk, info);
2083 	if (IS_ERR(pctx)) {
2084 		err = PTR_ERR(pctx);
2085 	} else {
2086 		gtp_tunnel_notify(pctx, GTP_CMD_NEWPDP, GFP_KERNEL);
2087 		err = 0;
2088 	}
2089 	mutex_unlock(&gtp_pdp_lock);
2090 
2091 out_unlock:
2092 	rtnl_unlock();
2093 	return err;
2094 }
2095 
2096 static struct pdp_ctx *gtp_find_pdp_by_link(struct net *net,
2097 					    struct nlattr *nla[])
2098 {
2099 	struct gtp_dev *gtp;
2100 	int family;
2101 
2102 	family = nla_get_u8_default(nla[GTPA_FAMILY], AF_INET);
2103 
2104 	gtp = gtp_find_dev(net, nla);
2105 	if (!gtp)
2106 		return ERR_PTR(-ENODEV);
2107 
2108 	if (nla[GTPA_MS_ADDRESS]) {
2109 		__be32 ip = nla_get_be32(nla[GTPA_MS_ADDRESS]);
2110 
2111 		if (family != AF_INET)
2112 			return ERR_PTR(-EINVAL);
2113 
2114 		return ipv4_pdp_find(gtp, ip);
2115 	} else if (nla[GTPA_MS_ADDR6]) {
2116 		struct in6_addr addr = nla_get_in6_addr(nla[GTPA_MS_ADDR6]);
2117 
2118 		if (family != AF_INET6)
2119 			return ERR_PTR(-EINVAL);
2120 
2121 		if (addr.s6_addr32[2] ||
2122 		    addr.s6_addr32[3])
2123 			return ERR_PTR(-EADDRNOTAVAIL);
2124 
2125 		return ipv6_pdp_find(gtp, &addr);
2126 	} else if (nla[GTPA_VERSION]) {
2127 		u32 gtp_version = nla_get_u32(nla[GTPA_VERSION]);
2128 
2129 		if (gtp_version == GTP_V0 && nla[GTPA_TID]) {
2130 			return gtp0_pdp_find(gtp, nla_get_u64(nla[GTPA_TID]),
2131 					     family);
2132 		} else if (gtp_version == GTP_V1 && nla[GTPA_I_TEI]) {
2133 			return gtp1_pdp_find(gtp, nla_get_u32(nla[GTPA_I_TEI]),
2134 					     family);
2135 		}
2136 	}
2137 
2138 	return ERR_PTR(-EINVAL);
2139 }
2140 
2141 static struct pdp_ctx *gtp_find_pdp(struct net *net, struct nlattr *nla[])
2142 {
2143 	struct pdp_ctx *pctx;
2144 
2145 	if (nla[GTPA_LINK])
2146 		pctx = gtp_find_pdp_by_link(net, nla);
2147 	else
2148 		pctx = ERR_PTR(-EINVAL);
2149 
2150 	if (!pctx)
2151 		pctx = ERR_PTR(-ENOENT);
2152 
2153 	return pctx;
2154 }
2155 
2156 static int gtp_genl_del_pdp(struct sk_buff *skb, struct genl_info *info)
2157 {
2158 	struct pdp_ctx *pctx;
2159 	int err = 0;
2160 
2161 	if (!info->attrs[GTPA_VERSION])
2162 		return -EINVAL;
2163 
2164 	mutex_lock(&gtp_pdp_lock);
2165 
2166 	rcu_read_lock();
2167 
2168 	pctx = gtp_find_pdp(sock_net(skb->sk), info->attrs);
2169 	if (IS_ERR(pctx)) {
2170 		err = PTR_ERR(pctx);
2171 		goto out_unlock;
2172 	}
2173 
2174 	if (pctx->gtp_version == GTP_V0)
2175 		netdev_dbg(pctx->dev, "GTPv0-U: deleting tunnel id = %llx (pdp %p)\n",
2176 			   pctx->u.v0.tid, pctx);
2177 	else if (pctx->gtp_version == GTP_V1)
2178 		netdev_dbg(pctx->dev, "GTPv1-U: deleting tunnel id = %x/%x (pdp %p)\n",
2179 			   pctx->u.v1.i_tei, pctx->u.v1.o_tei, pctx);
2180 
2181 	gtp_tunnel_notify(pctx, GTP_CMD_DELPDP, GFP_ATOMIC);
2182 	pdp_context_delete(pctx);
2183 
2184 out_unlock:
2185 	rcu_read_unlock();
2186 	mutex_unlock(&gtp_pdp_lock);
2187 	return err;
2188 }
2189 
2190 static int gtp_genl_fill_info(struct sk_buff *skb, u32 snd_portid, u32 snd_seq,
2191 			      int flags, u32 type, struct pdp_ctx *pctx)
2192 {
2193 	void *genlh;
2194 
2195 	genlh = genlmsg_put(skb, snd_portid, snd_seq, &gtp_genl_family, flags,
2196 			    type);
2197 	if (genlh == NULL)
2198 		goto nlmsg_failure;
2199 
2200 	if (nla_put_u32(skb, GTPA_VERSION, pctx->gtp_version) ||
2201 	    nla_put_u32(skb, GTPA_LINK, pctx->dev->ifindex) ||
2202 	    nla_put_u8(skb, GTPA_FAMILY, pctx->af))
2203 		goto nla_put_failure;
2204 
2205 	switch (pctx->af) {
2206 	case AF_INET:
2207 		if (nla_put_be32(skb, GTPA_MS_ADDRESS, pctx->ms.addr.s_addr))
2208 			goto nla_put_failure;
2209 		break;
2210 	case AF_INET6:
2211 		if (nla_put_in6_addr(skb, GTPA_MS_ADDR6, &pctx->ms.addr6))
2212 			goto nla_put_failure;
2213 		break;
2214 	}
2215 
2216 	switch (pctx->sk->sk_family) {
2217 	case AF_INET:
2218 		if (nla_put_be32(skb, GTPA_PEER_ADDRESS, pctx->peer.addr.s_addr))
2219 			goto nla_put_failure;
2220 		break;
2221 	case AF_INET6:
2222 		if (nla_put_in6_addr(skb, GTPA_PEER_ADDR6, &pctx->peer.addr6))
2223 			goto nla_put_failure;
2224 		break;
2225 	}
2226 
2227 	switch (pctx->gtp_version) {
2228 	case GTP_V0:
2229 		if (nla_put_u64_64bit(skb, GTPA_TID, pctx->u.v0.tid, GTPA_PAD) ||
2230 		    nla_put_u16(skb, GTPA_FLOW, pctx->u.v0.flow))
2231 			goto nla_put_failure;
2232 		break;
2233 	case GTP_V1:
2234 		if (nla_put_u32(skb, GTPA_I_TEI, pctx->u.v1.i_tei) ||
2235 		    nla_put_u32(skb, GTPA_O_TEI, pctx->u.v1.o_tei))
2236 			goto nla_put_failure;
2237 		break;
2238 	}
2239 	genlmsg_end(skb, genlh);
2240 	return 0;
2241 
2242 nlmsg_failure:
2243 nla_put_failure:
2244 	genlmsg_cancel(skb, genlh);
2245 	return -EMSGSIZE;
2246 }
2247 
2248 static int gtp_tunnel_notify(struct pdp_ctx *pctx, u8 cmd, gfp_t allocation)
2249 {
2250 	struct sk_buff *msg;
2251 	int ret;
2252 
2253 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, allocation);
2254 	if (!msg)
2255 		return -ENOMEM;
2256 
2257 	ret = gtp_genl_fill_info(msg, 0, 0, 0, cmd, pctx);
2258 	if (ret < 0) {
2259 		nlmsg_free(msg);
2260 		return ret;
2261 	}
2262 
2263 	ret = genlmsg_multicast_netns(&gtp_genl_family, dev_net(pctx->dev), msg,
2264 				      0, GTP_GENL_MCGRP, GFP_ATOMIC);
2265 	return ret;
2266 }
2267 
2268 static int gtp_genl_get_pdp(struct sk_buff *skb, struct genl_info *info)
2269 {
2270 	struct pdp_ctx *pctx = NULL;
2271 	struct sk_buff *skb2;
2272 	int err;
2273 
2274 	if (!info->attrs[GTPA_VERSION])
2275 		return -EINVAL;
2276 
2277 	rcu_read_lock();
2278 
2279 	pctx = gtp_find_pdp(sock_net(skb->sk), info->attrs);
2280 	if (IS_ERR(pctx)) {
2281 		err = PTR_ERR(pctx);
2282 		goto err_unlock;
2283 	}
2284 
2285 	skb2 = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
2286 	if (skb2 == NULL) {
2287 		err = -ENOMEM;
2288 		goto err_unlock;
2289 	}
2290 
2291 	err = gtp_genl_fill_info(skb2, NETLINK_CB(skb).portid, info->snd_seq,
2292 				 0, GTP_CMD_GETPDP, pctx);
2293 	if (err < 0)
2294 		goto err_unlock_free;
2295 
2296 	rcu_read_unlock();
2297 	return genlmsg_unicast(genl_info_net(info), skb2, info->snd_portid);
2298 
2299 err_unlock_free:
2300 	kfree_skb(skb2);
2301 err_unlock:
2302 	rcu_read_unlock();
2303 	return err;
2304 }
2305 
2306 static int gtp_genl_dump_pdp(struct sk_buff *skb,
2307 				struct netlink_callback *cb)
2308 {
2309 	struct gtp_dev *last_gtp = (struct gtp_dev *)cb->args[2], *gtp;
2310 	int i, j, bucket = cb->args[0], skip = cb->args[1];
2311 	struct net *net = sock_net(skb->sk);
2312 	struct net_device *dev;
2313 	struct pdp_ctx *pctx;
2314 
2315 	if (cb->args[4])
2316 		return 0;
2317 
2318 	rcu_read_lock();
2319 	for_each_netdev_rcu(net, dev) {
2320 		if (dev->rtnl_link_ops != &gtp_link_ops)
2321 			continue;
2322 
2323 		gtp = netdev_priv(dev);
2324 
2325 		if (last_gtp && last_gtp != gtp)
2326 			continue;
2327 		else
2328 			last_gtp = NULL;
2329 
2330 		for (i = bucket; i < gtp->hash_size; i++) {
2331 			j = 0;
2332 			hlist_for_each_entry_rcu(pctx, &gtp->tid_hash[i],
2333 						 hlist_tid) {
2334 				if (j >= skip &&
2335 				    gtp_genl_fill_info(skb,
2336 					    NETLINK_CB(cb->skb).portid,
2337 					    cb->nlh->nlmsg_seq,
2338 					    NLM_F_MULTI,
2339 					    GTP_CMD_GETPDP, pctx)) {
2340 					cb->args[0] = i;
2341 					cb->args[1] = j;
2342 					cb->args[2] = (unsigned long)gtp;
2343 					goto out;
2344 				}
2345 				j++;
2346 			}
2347 			skip = 0;
2348 		}
2349 		bucket = 0;
2350 	}
2351 	cb->args[4] = 1;
2352 out:
2353 	rcu_read_unlock();
2354 	return skb->len;
2355 }
2356 
2357 static int gtp_genl_send_echo_req(struct sk_buff *skb, struct genl_info *info)
2358 {
2359 	struct sk_buff *skb_to_send;
2360 	__be32 src_ip, dst_ip;
2361 	unsigned int version;
2362 	struct gtp_dev *gtp;
2363 	struct flowi4 fl4;
2364 	struct rtable *rt;
2365 	int len, ret = 0;
2366 	struct sock *sk;
2367 	__be16 port;
2368 
2369 	if (!info->attrs[GTPA_VERSION] ||
2370 	    !info->attrs[GTPA_LINK] ||
2371 	    !info->attrs[GTPA_PEER_ADDRESS] ||
2372 	    !info->attrs[GTPA_MS_ADDRESS])
2373 		return -EINVAL;
2374 
2375 	version = nla_get_u32(info->attrs[GTPA_VERSION]);
2376 	dst_ip = nla_get_be32(info->attrs[GTPA_PEER_ADDRESS]);
2377 	src_ip = nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
2378 
2379 	rcu_read_lock();
2380 	gtp = gtp_find_dev(sock_net(skb->sk), info->attrs);
2381 	if (!gtp) {
2382 		ret = -ENODEV;
2383 		goto unlock;
2384 	}
2385 
2386 	if (!gtp->sk_created) {
2387 		ret = -EOPNOTSUPP;
2388 		goto unlock;
2389 	}
2390 	if (!(gtp->dev->flags & IFF_UP)) {
2391 		ret = -ENETDOWN;
2392 		goto unlock;
2393 	}
2394 
2395 	if (version == GTP_V0) {
2396 		struct gtp0_header *gtp0_h;
2397 
2398 		len = LL_RESERVED_SPACE(gtp->dev) + sizeof(struct gtp0_header) +
2399 			sizeof(struct iphdr) + sizeof(struct udphdr);
2400 
2401 		skb_to_send = netdev_alloc_skb_ip_align(gtp->dev, len);
2402 		if (!skb_to_send) {
2403 			ret = -ENOMEM;
2404 			goto unlock;
2405 		}
2406 
2407 		sk = gtp->sk0;
2408 		port = htons(GTP0_PORT);
2409 
2410 		gtp0_h = skb_push(skb_to_send, sizeof(struct gtp0_header));
2411 		memset(gtp0_h, 0, sizeof(struct gtp0_header));
2412 		gtp0_build_echo_msg(gtp0_h, GTP_ECHO_REQ);
2413 	} else if (version == GTP_V1) {
2414 		struct gtp1_header_long *gtp1u_h;
2415 
2416 		len = LL_RESERVED_SPACE(gtp->dev) +
2417 			sizeof(struct gtp1_header_long) +
2418 			sizeof(struct iphdr) + sizeof(struct udphdr);
2419 
2420 		skb_to_send = netdev_alloc_skb_ip_align(gtp->dev, len);
2421 		if (!skb_to_send) {
2422 			ret = -ENOMEM;
2423 			goto unlock;
2424 		}
2425 
2426 		sk = gtp->sk1u;
2427 		port = htons(GTP1U_PORT);
2428 
2429 		gtp1u_h = skb_push(skb_to_send,
2430 				   sizeof(struct gtp1_header_long));
2431 		memset(gtp1u_h, 0, sizeof(struct gtp1_header_long));
2432 		gtp1u_build_echo_msg(gtp1u_h, GTP_ECHO_REQ);
2433 	} else {
2434 		ret = -ENODEV;
2435 		goto unlock;
2436 	}
2437 
2438 	rt = ip4_route_output_gtp(&fl4, sk, dst_ip, src_ip);
2439 	if (IS_ERR(rt)) {
2440 		netdev_dbg(gtp->dev, "no route for echo request to %pI4\n",
2441 			   &dst_ip);
2442 		kfree_skb(skb_to_send);
2443 		ret = -ENODEV;
2444 		goto unlock;
2445 	}
2446 
2447 	local_bh_disable();
2448 	udp_tunnel_xmit_skb(rt, sk, skb_to_send,
2449 			    fl4.saddr, fl4.daddr,
2450 			    inet_dscp_to_dsfield(fl4.flowi4_dscp),
2451 			    ip4_dst_hoplimit(&rt->dst),
2452 			    0,
2453 			    port, port,
2454 			    !net_eq(sock_net(sk),
2455 				    dev_net(gtp->dev)),
2456 			    false, 0);
2457 	local_bh_enable();
2458 
2459 unlock:
2460 	rcu_read_unlock();
2461 	return ret;
2462 }
2463 
2464 static const struct nla_policy gtp_genl_policy[GTPA_MAX + 1] = {
2465 	[GTPA_LINK]		= { .type = NLA_U32, },
2466 	[GTPA_VERSION]		= { .type = NLA_U32, },
2467 	[GTPA_TID]		= { .type = NLA_U64, },
2468 	[GTPA_PEER_ADDRESS]	= { .type = NLA_U32, },
2469 	[GTPA_MS_ADDRESS]	= { .type = NLA_U32, },
2470 	[GTPA_FLOW]		= { .type = NLA_U16, },
2471 	[GTPA_NET_NS_FD]	= { .type = NLA_U32, },
2472 	[GTPA_I_TEI]		= { .type = NLA_U32, },
2473 	[GTPA_O_TEI]		= { .type = NLA_U32, },
2474 	[GTPA_PEER_ADDR6]	= { .len = sizeof(struct in6_addr), },
2475 	[GTPA_MS_ADDR6]		= { .len = sizeof(struct in6_addr), },
2476 	[GTPA_FAMILY]		= { .type = NLA_U8, },
2477 };
2478 
2479 static const struct genl_small_ops gtp_genl_ops[] = {
2480 	{
2481 		.cmd = GTP_CMD_NEWPDP,
2482 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2483 		.doit = gtp_genl_new_pdp,
2484 		.flags = GENL_ADMIN_PERM,
2485 	},
2486 	{
2487 		.cmd = GTP_CMD_DELPDP,
2488 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2489 		.doit = gtp_genl_del_pdp,
2490 		.flags = GENL_ADMIN_PERM,
2491 	},
2492 	{
2493 		.cmd = GTP_CMD_GETPDP,
2494 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2495 		.doit = gtp_genl_get_pdp,
2496 		.dumpit = gtp_genl_dump_pdp,
2497 		.flags = GENL_ADMIN_PERM,
2498 	},
2499 	{
2500 		.cmd = GTP_CMD_ECHOREQ,
2501 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2502 		.doit = gtp_genl_send_echo_req,
2503 		.flags = GENL_ADMIN_PERM,
2504 	},
2505 };
2506 
2507 static struct genl_family gtp_genl_family __ro_after_init = {
2508 	.name		= "gtp",
2509 	.version	= 0,
2510 	.hdrsize	= 0,
2511 	.maxattr	= GTPA_MAX,
2512 	.policy = gtp_genl_policy,
2513 	.netnsok	= true,
2514 	.module		= THIS_MODULE,
2515 	.small_ops	= gtp_genl_ops,
2516 	.n_small_ops	= ARRAY_SIZE(gtp_genl_ops),
2517 	.resv_start_op	= GTP_CMD_ECHOREQ + 1,
2518 	.mcgrps		= gtp_genl_mcgrps,
2519 	.n_mcgrps	= ARRAY_SIZE(gtp_genl_mcgrps),
2520 };
2521 
2522 static int __net_init gtp_net_init(struct net *net)
2523 {
2524 	struct gtp_net *gn = net_generic(net, gtp_net_id);
2525 
2526 	INIT_LIST_HEAD(&gn->gtp_dev_list);
2527 	return 0;
2528 }
2529 
2530 static void __net_exit gtp_net_exit_rtnl(struct net *net,
2531 					 struct list_head *dev_to_kill)
2532 {
2533 	struct gtp_net *gn = net_generic(net, gtp_net_id);
2534 	struct gtp_dev *gtp, *gtp_next;
2535 
2536 	list_for_each_entry_safe(gtp, gtp_next, &gn->gtp_dev_list, list)
2537 		gtp_dellink(gtp->dev, dev_to_kill);
2538 }
2539 
2540 static struct pernet_operations gtp_net_ops = {
2541 	.init	= gtp_net_init,
2542 	.exit_rtnl = gtp_net_exit_rtnl,
2543 	.id	= &gtp_net_id,
2544 	.size	= sizeof(struct gtp_net),
2545 };
2546 
2547 static int __init gtp_init(void)
2548 {
2549 	int err;
2550 
2551 	get_random_bytes(&gtp_h_initval, sizeof(gtp_h_initval));
2552 
2553 	err = register_pernet_subsys(&gtp_net_ops);
2554 	if (err < 0)
2555 		goto error_out;
2556 
2557 	err = rtnl_link_register(&gtp_link_ops);
2558 	if (err < 0)
2559 		goto unreg_pernet_subsys;
2560 
2561 	err = genl_register_family(&gtp_genl_family);
2562 	if (err < 0)
2563 		goto unreg_rtnl_link;
2564 
2565 	pr_info("GTP module loaded (pdp ctx size %zd bytes)\n",
2566 		sizeof(struct pdp_ctx));
2567 	return 0;
2568 
2569 unreg_rtnl_link:
2570 	rtnl_link_unregister(&gtp_link_ops);
2571 unreg_pernet_subsys:
2572 	unregister_pernet_subsys(&gtp_net_ops);
2573 error_out:
2574 	pr_err("error loading GTP module loaded\n");
2575 	return err;
2576 }
2577 late_initcall(gtp_init);
2578 
2579 static void __exit gtp_fini(void)
2580 {
2581 	genl_unregister_family(&gtp_genl_family);
2582 	rtnl_link_unregister(&gtp_link_ops);
2583 	unregister_pernet_subsys(&gtp_net_ops);
2584 
2585 	pr_info("GTP module unloaded\n");
2586 }
2587 module_exit(gtp_fini);
2588 
2589 MODULE_LICENSE("GPL");
2590 MODULE_AUTHOR("Harald Welte <hwelte@sysmocom.de>");
2591 MODULE_DESCRIPTION("Interface driver for GTP encapsulated traffic");
2592 MODULE_ALIAS_RTNL_LINK("gtp");
2593 MODULE_ALIAS_GENL_FAMILY("gtp");
2594