Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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