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