gtp.c 31 KB

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