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