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