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