gtp.c 31 KB

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