gtp.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  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 = netdev_alloc_pcpu_stats(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 = 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 = 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->needs_free_netdev = true;
  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. struct netlink_ext_ack *extack)
  527. {
  528. struct gtp_dev *gtp;
  529. struct gtp_net *gn;
  530. int hashsize, err;
  531. if (!data[IFLA_GTP_FD0] && !data[IFLA_GTP_FD1])
  532. return -EINVAL;
  533. gtp = netdev_priv(dev);
  534. err = gtp_encap_enable(gtp, data);
  535. if (err < 0)
  536. return err;
  537. if (!data[IFLA_GTP_PDP_HASHSIZE])
  538. hashsize = 1024;
  539. else
  540. hashsize = nla_get_u32(data[IFLA_GTP_PDP_HASHSIZE]);
  541. err = gtp_hashtable_new(gtp, hashsize);
  542. if (err < 0)
  543. goto out_encap;
  544. err = register_netdevice(dev);
  545. if (err < 0) {
  546. netdev_dbg(dev, "failed to register new netdev %d\n", err);
  547. goto out_hashtable;
  548. }
  549. gn = net_generic(dev_net(dev), gtp_net_id);
  550. list_add_rcu(&gtp->list, &gn->gtp_dev_list);
  551. netdev_dbg(dev, "registered new GTP interface\n");
  552. return 0;
  553. out_hashtable:
  554. gtp_hashtable_free(gtp);
  555. out_encap:
  556. gtp_encap_disable(gtp);
  557. return err;
  558. }
  559. static void gtp_dellink(struct net_device *dev, struct list_head *head)
  560. {
  561. struct gtp_dev *gtp = netdev_priv(dev);
  562. gtp_encap_disable(gtp);
  563. gtp_hashtable_free(gtp);
  564. list_del_rcu(&gtp->list);
  565. unregister_netdevice_queue(dev, head);
  566. }
  567. static const struct nla_policy gtp_policy[IFLA_GTP_MAX + 1] = {
  568. [IFLA_GTP_FD0] = { .type = NLA_U32 },
  569. [IFLA_GTP_FD1] = { .type = NLA_U32 },
  570. [IFLA_GTP_PDP_HASHSIZE] = { .type = NLA_U32 },
  571. [IFLA_GTP_ROLE] = { .type = NLA_U32 },
  572. };
  573. static int gtp_validate(struct nlattr *tb[], struct nlattr *data[],
  574. struct netlink_ext_ack *extack)
  575. {
  576. if (!data)
  577. return -EINVAL;
  578. return 0;
  579. }
  580. static size_t gtp_get_size(const struct net_device *dev)
  581. {
  582. return nla_total_size(sizeof(__u32)); /* IFLA_GTP_PDP_HASHSIZE */
  583. }
  584. static int gtp_fill_info(struct sk_buff *skb, const struct net_device *dev)
  585. {
  586. struct gtp_dev *gtp = netdev_priv(dev);
  587. if (nla_put_u32(skb, IFLA_GTP_PDP_HASHSIZE, gtp->hash_size))
  588. goto nla_put_failure;
  589. return 0;
  590. nla_put_failure:
  591. return -EMSGSIZE;
  592. }
  593. static struct rtnl_link_ops gtp_link_ops __read_mostly = {
  594. .kind = "gtp",
  595. .maxtype = IFLA_GTP_MAX,
  596. .policy = gtp_policy,
  597. .priv_size = sizeof(struct gtp_dev),
  598. .setup = gtp_link_setup,
  599. .validate = gtp_validate,
  600. .newlink = gtp_newlink,
  601. .dellink = gtp_dellink,
  602. .get_size = gtp_get_size,
  603. .fill_info = gtp_fill_info,
  604. };
  605. static int gtp_hashtable_new(struct gtp_dev *gtp, int hsize)
  606. {
  607. int i;
  608. gtp->addr_hash = kmalloc_array(hsize, sizeof(struct hlist_head),
  609. GFP_KERNEL);
  610. if (gtp->addr_hash == NULL)
  611. return -ENOMEM;
  612. gtp->tid_hash = kmalloc_array(hsize, sizeof(struct hlist_head),
  613. GFP_KERNEL);
  614. if (gtp->tid_hash == NULL)
  615. goto err1;
  616. gtp->hash_size = hsize;
  617. for (i = 0; i < hsize; i++) {
  618. INIT_HLIST_HEAD(&gtp->addr_hash[i]);
  619. INIT_HLIST_HEAD(&gtp->tid_hash[i]);
  620. }
  621. return 0;
  622. err1:
  623. kfree(gtp->addr_hash);
  624. return -ENOMEM;
  625. }
  626. static void gtp_hashtable_free(struct gtp_dev *gtp)
  627. {
  628. struct pdp_ctx *pctx;
  629. int i;
  630. for (i = 0; i < gtp->hash_size; i++)
  631. hlist_for_each_entry_rcu(pctx, &gtp->tid_hash[i], hlist_tid)
  632. pdp_context_delete(pctx);
  633. synchronize_rcu();
  634. kfree(gtp->addr_hash);
  635. kfree(gtp->tid_hash);
  636. }
  637. static struct sock *gtp_encap_enable_socket(int fd, int type,
  638. struct gtp_dev *gtp)
  639. {
  640. struct udp_tunnel_sock_cfg tuncfg = {NULL};
  641. struct socket *sock;
  642. struct sock *sk;
  643. int err;
  644. pr_debug("enable gtp on %d, %d\n", fd, type);
  645. sock = sockfd_lookup(fd, &err);
  646. if (!sock) {
  647. pr_debug("gtp socket fd=%d not found\n", fd);
  648. return NULL;
  649. }
  650. if (sock->sk->sk_protocol != IPPROTO_UDP) {
  651. pr_debug("socket fd=%d not UDP\n", fd);
  652. sk = ERR_PTR(-EINVAL);
  653. goto out_sock;
  654. }
  655. if (rcu_dereference_sk_user_data(sock->sk)) {
  656. sk = ERR_PTR(-EBUSY);
  657. goto out_sock;
  658. }
  659. sk = sock->sk;
  660. sock_hold(sk);
  661. tuncfg.sk_user_data = gtp;
  662. tuncfg.encap_type = type;
  663. tuncfg.encap_rcv = gtp_encap_recv;
  664. tuncfg.encap_destroy = gtp_encap_destroy;
  665. setup_udp_tunnel_sock(sock_net(sock->sk), sock, &tuncfg);
  666. out_sock:
  667. sockfd_put(sock);
  668. return sk;
  669. }
  670. static int gtp_encap_enable(struct gtp_dev *gtp, struct nlattr *data[])
  671. {
  672. struct sock *sk1u = NULL;
  673. struct sock *sk0 = NULL;
  674. unsigned int role = GTP_ROLE_GGSN;
  675. if (data[IFLA_GTP_FD0]) {
  676. u32 fd0 = nla_get_u32(data[IFLA_GTP_FD0]);
  677. sk0 = gtp_encap_enable_socket(fd0, UDP_ENCAP_GTP0, gtp);
  678. if (IS_ERR(sk0))
  679. return PTR_ERR(sk0);
  680. }
  681. if (data[IFLA_GTP_FD1]) {
  682. u32 fd1 = nla_get_u32(data[IFLA_GTP_FD1]);
  683. sk1u = gtp_encap_enable_socket(fd1, UDP_ENCAP_GTP1U, gtp);
  684. if (IS_ERR(sk1u)) {
  685. if (sk0)
  686. gtp_encap_disable_sock(sk0);
  687. return PTR_ERR(sk1u);
  688. }
  689. }
  690. if (data[IFLA_GTP_ROLE]) {
  691. role = nla_get_u32(data[IFLA_GTP_ROLE]);
  692. if (role > GTP_ROLE_SGSN)
  693. return -EINVAL;
  694. }
  695. gtp->sk0 = sk0;
  696. gtp->sk1u = sk1u;
  697. gtp->role = role;
  698. return 0;
  699. }
  700. static struct gtp_dev *gtp_find_dev(struct net *src_net, struct nlattr *nla[])
  701. {
  702. struct gtp_dev *gtp = NULL;
  703. struct net_device *dev;
  704. struct net *net;
  705. /* Examine the link attributes and figure out which network namespace
  706. * we are talking about.
  707. */
  708. if (nla[GTPA_NET_NS_FD])
  709. net = get_net_ns_by_fd(nla_get_u32(nla[GTPA_NET_NS_FD]));
  710. else
  711. net = get_net(src_net);
  712. if (IS_ERR(net))
  713. return NULL;
  714. /* Check if there's an existing gtpX device to configure */
  715. dev = dev_get_by_index_rcu(net, nla_get_u32(nla[GTPA_LINK]));
  716. if (dev && dev->netdev_ops == &gtp_netdev_ops)
  717. gtp = netdev_priv(dev);
  718. put_net(net);
  719. return gtp;
  720. }
  721. static void ipv4_pdp_fill(struct pdp_ctx *pctx, struct genl_info *info)
  722. {
  723. pctx->gtp_version = nla_get_u32(info->attrs[GTPA_VERSION]);
  724. pctx->af = AF_INET;
  725. pctx->peer_addr_ip4.s_addr =
  726. nla_get_be32(info->attrs[GTPA_PEER_ADDRESS]);
  727. pctx->ms_addr_ip4.s_addr =
  728. nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
  729. switch (pctx->gtp_version) {
  730. case GTP_V0:
  731. /* According to TS 09.60, sections 7.5.1 and 7.5.2, the flow
  732. * label needs to be the same for uplink and downlink packets,
  733. * so let's annotate this.
  734. */
  735. pctx->u.v0.tid = nla_get_u64(info->attrs[GTPA_TID]);
  736. pctx->u.v0.flow = nla_get_u16(info->attrs[GTPA_FLOW]);
  737. break;
  738. case GTP_V1:
  739. pctx->u.v1.i_tei = nla_get_u32(info->attrs[GTPA_I_TEI]);
  740. pctx->u.v1.o_tei = nla_get_u32(info->attrs[GTPA_O_TEI]);
  741. break;
  742. default:
  743. break;
  744. }
  745. }
  746. static int ipv4_pdp_add(struct gtp_dev *gtp, struct sock *sk,
  747. struct genl_info *info)
  748. {
  749. struct net_device *dev = gtp->dev;
  750. u32 hash_ms, hash_tid = 0;
  751. struct pdp_ctx *pctx;
  752. bool found = false;
  753. __be32 ms_addr;
  754. ms_addr = nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
  755. hash_ms = ipv4_hashfn(ms_addr) % gtp->hash_size;
  756. hlist_for_each_entry_rcu(pctx, &gtp->addr_hash[hash_ms], hlist_addr) {
  757. if (pctx->ms_addr_ip4.s_addr == ms_addr) {
  758. found = true;
  759. break;
  760. }
  761. }
  762. if (found) {
  763. if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
  764. return -EEXIST;
  765. if (info->nlhdr->nlmsg_flags & NLM_F_REPLACE)
  766. return -EOPNOTSUPP;
  767. ipv4_pdp_fill(pctx, info);
  768. if (pctx->gtp_version == GTP_V0)
  769. netdev_dbg(dev, "GTPv0-U: update tunnel id = %llx (pdp %p)\n",
  770. pctx->u.v0.tid, pctx);
  771. else if (pctx->gtp_version == GTP_V1)
  772. netdev_dbg(dev, "GTPv1-U: update tunnel id = %x/%x (pdp %p)\n",
  773. pctx->u.v1.i_tei, pctx->u.v1.o_tei, pctx);
  774. return 0;
  775. }
  776. pctx = kmalloc(sizeof(struct pdp_ctx), GFP_KERNEL);
  777. if (pctx == NULL)
  778. return -ENOMEM;
  779. sock_hold(sk);
  780. pctx->sk = sk;
  781. pctx->dev = gtp->dev;
  782. ipv4_pdp_fill(pctx, info);
  783. atomic_set(&pctx->tx_seq, 0);
  784. switch (pctx->gtp_version) {
  785. case GTP_V0:
  786. /* TS 09.60: "The flow label identifies unambiguously a GTP
  787. * flow.". We use the tid for this instead, I cannot find a
  788. * situation in which this doesn't unambiguosly identify the
  789. * PDP context.
  790. */
  791. hash_tid = gtp0_hashfn(pctx->u.v0.tid) % gtp->hash_size;
  792. break;
  793. case GTP_V1:
  794. hash_tid = gtp1u_hashfn(pctx->u.v1.i_tei) % gtp->hash_size;
  795. break;
  796. }
  797. hlist_add_head_rcu(&pctx->hlist_addr, &gtp->addr_hash[hash_ms]);
  798. hlist_add_head_rcu(&pctx->hlist_tid, &gtp->tid_hash[hash_tid]);
  799. switch (pctx->gtp_version) {
  800. case GTP_V0:
  801. netdev_dbg(dev, "GTPv0-U: new PDP ctx id=%llx ssgn=%pI4 ms=%pI4 (pdp=%p)\n",
  802. pctx->u.v0.tid, &pctx->peer_addr_ip4,
  803. &pctx->ms_addr_ip4, pctx);
  804. break;
  805. case GTP_V1:
  806. netdev_dbg(dev, "GTPv1-U: new PDP ctx id=%x/%x ssgn=%pI4 ms=%pI4 (pdp=%p)\n",
  807. pctx->u.v1.i_tei, pctx->u.v1.o_tei,
  808. &pctx->peer_addr_ip4, &pctx->ms_addr_ip4, pctx);
  809. break;
  810. }
  811. return 0;
  812. }
  813. static void pdp_context_free(struct rcu_head *head)
  814. {
  815. struct pdp_ctx *pctx = container_of(head, struct pdp_ctx, rcu_head);
  816. sock_put(pctx->sk);
  817. kfree(pctx);
  818. }
  819. static void pdp_context_delete(struct pdp_ctx *pctx)
  820. {
  821. hlist_del_rcu(&pctx->hlist_tid);
  822. hlist_del_rcu(&pctx->hlist_addr);
  823. call_rcu(&pctx->rcu_head, pdp_context_free);
  824. }
  825. static int gtp_genl_new_pdp(struct sk_buff *skb, struct genl_info *info)
  826. {
  827. unsigned int version;
  828. struct gtp_dev *gtp;
  829. struct sock *sk;
  830. int err;
  831. if (!info->attrs[GTPA_VERSION] ||
  832. !info->attrs[GTPA_LINK] ||
  833. !info->attrs[GTPA_PEER_ADDRESS] ||
  834. !info->attrs[GTPA_MS_ADDRESS])
  835. return -EINVAL;
  836. version = nla_get_u32(info->attrs[GTPA_VERSION]);
  837. switch (version) {
  838. case GTP_V0:
  839. if (!info->attrs[GTPA_TID] ||
  840. !info->attrs[GTPA_FLOW])
  841. return -EINVAL;
  842. break;
  843. case GTP_V1:
  844. if (!info->attrs[GTPA_I_TEI] ||
  845. !info->attrs[GTPA_O_TEI])
  846. return -EINVAL;
  847. break;
  848. default:
  849. return -EINVAL;
  850. }
  851. rcu_read_lock();
  852. gtp = gtp_find_dev(sock_net(skb->sk), info->attrs);
  853. if (!gtp) {
  854. err = -ENODEV;
  855. goto out_unlock;
  856. }
  857. if (version == GTP_V0)
  858. sk = gtp->sk0;
  859. else if (version == GTP_V1)
  860. sk = gtp->sk1u;
  861. else
  862. sk = NULL;
  863. if (!sk) {
  864. err = -ENODEV;
  865. goto out_unlock;
  866. }
  867. err = ipv4_pdp_add(gtp, sk, info);
  868. out_unlock:
  869. rcu_read_unlock();
  870. return err;
  871. }
  872. static struct pdp_ctx *gtp_find_pdp_by_link(struct net *net,
  873. struct nlattr *nla[])
  874. {
  875. struct gtp_dev *gtp;
  876. gtp = gtp_find_dev(net, nla);
  877. if (!gtp)
  878. return ERR_PTR(-ENODEV);
  879. if (nla[GTPA_MS_ADDRESS]) {
  880. __be32 ip = nla_get_be32(nla[GTPA_MS_ADDRESS]);
  881. return ipv4_pdp_find(gtp, ip);
  882. } else if (nla[GTPA_VERSION]) {
  883. u32 gtp_version = nla_get_u32(nla[GTPA_VERSION]);
  884. if (gtp_version == GTP_V0 && nla[GTPA_TID])
  885. return gtp0_pdp_find(gtp, nla_get_u64(nla[GTPA_TID]));
  886. else if (gtp_version == GTP_V1 && nla[GTPA_I_TEI])
  887. return gtp1_pdp_find(gtp, nla_get_u32(nla[GTPA_I_TEI]));
  888. }
  889. return ERR_PTR(-EINVAL);
  890. }
  891. static struct pdp_ctx *gtp_find_pdp(struct net *net, struct nlattr *nla[])
  892. {
  893. struct pdp_ctx *pctx;
  894. if (nla[GTPA_LINK])
  895. pctx = gtp_find_pdp_by_link(net, nla);
  896. else
  897. pctx = ERR_PTR(-EINVAL);
  898. if (!pctx)
  899. pctx = ERR_PTR(-ENOENT);
  900. return pctx;
  901. }
  902. static int gtp_genl_del_pdp(struct sk_buff *skb, struct genl_info *info)
  903. {
  904. struct pdp_ctx *pctx;
  905. int err = 0;
  906. if (!info->attrs[GTPA_VERSION])
  907. return -EINVAL;
  908. rcu_read_lock();
  909. pctx = gtp_find_pdp(sock_net(skb->sk), info->attrs);
  910. if (IS_ERR(pctx)) {
  911. err = PTR_ERR(pctx);
  912. goto out_unlock;
  913. }
  914. if (pctx->gtp_version == GTP_V0)
  915. netdev_dbg(pctx->dev, "GTPv0-U: deleting tunnel id = %llx (pdp %p)\n",
  916. pctx->u.v0.tid, pctx);
  917. else if (pctx->gtp_version == GTP_V1)
  918. netdev_dbg(pctx->dev, "GTPv1-U: deleting tunnel id = %x/%x (pdp %p)\n",
  919. pctx->u.v1.i_tei, pctx->u.v1.o_tei, pctx);
  920. pdp_context_delete(pctx);
  921. out_unlock:
  922. rcu_read_unlock();
  923. return err;
  924. }
  925. static struct genl_family gtp_genl_family;
  926. static int gtp_genl_fill_info(struct sk_buff *skb, u32 snd_portid, u32 snd_seq,
  927. u32 type, struct pdp_ctx *pctx)
  928. {
  929. void *genlh;
  930. genlh = genlmsg_put(skb, snd_portid, snd_seq, &gtp_genl_family, 0,
  931. type);
  932. if (genlh == NULL)
  933. goto nlmsg_failure;
  934. if (nla_put_u32(skb, GTPA_VERSION, pctx->gtp_version) ||
  935. nla_put_be32(skb, GTPA_PEER_ADDRESS, pctx->peer_addr_ip4.s_addr) ||
  936. nla_put_be32(skb, GTPA_MS_ADDRESS, pctx->ms_addr_ip4.s_addr))
  937. goto nla_put_failure;
  938. switch (pctx->gtp_version) {
  939. case GTP_V0:
  940. if (nla_put_u64_64bit(skb, GTPA_TID, pctx->u.v0.tid, GTPA_PAD) ||
  941. nla_put_u16(skb, GTPA_FLOW, pctx->u.v0.flow))
  942. goto nla_put_failure;
  943. break;
  944. case GTP_V1:
  945. if (nla_put_u32(skb, GTPA_I_TEI, pctx->u.v1.i_tei) ||
  946. nla_put_u32(skb, GTPA_O_TEI, pctx->u.v1.o_tei))
  947. goto nla_put_failure;
  948. break;
  949. }
  950. genlmsg_end(skb, genlh);
  951. return 0;
  952. nlmsg_failure:
  953. nla_put_failure:
  954. genlmsg_cancel(skb, genlh);
  955. return -EMSGSIZE;
  956. }
  957. static int gtp_genl_get_pdp(struct sk_buff *skb, struct genl_info *info)
  958. {
  959. struct pdp_ctx *pctx = NULL;
  960. struct sk_buff *skb2;
  961. int err;
  962. if (!info->attrs[GTPA_VERSION])
  963. return -EINVAL;
  964. rcu_read_lock();
  965. pctx = gtp_find_pdp(sock_net(skb->sk), info->attrs);
  966. if (IS_ERR(pctx)) {
  967. err = PTR_ERR(pctx);
  968. goto err_unlock;
  969. }
  970. skb2 = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
  971. if (skb2 == NULL) {
  972. err = -ENOMEM;
  973. goto err_unlock;
  974. }
  975. err = gtp_genl_fill_info(skb2, NETLINK_CB(skb).portid,
  976. info->snd_seq, info->nlhdr->nlmsg_type, pctx);
  977. if (err < 0)
  978. goto err_unlock_free;
  979. rcu_read_unlock();
  980. return genlmsg_unicast(genl_info_net(info), skb2, info->snd_portid);
  981. err_unlock_free:
  982. kfree_skb(skb2);
  983. err_unlock:
  984. rcu_read_unlock();
  985. return err;
  986. }
  987. static int gtp_genl_dump_pdp(struct sk_buff *skb,
  988. struct netlink_callback *cb)
  989. {
  990. struct gtp_dev *last_gtp = (struct gtp_dev *)cb->args[2], *gtp;
  991. struct net *net = sock_net(skb->sk);
  992. struct gtp_net *gn = net_generic(net, gtp_net_id);
  993. unsigned long tid = cb->args[1];
  994. int i, k = cb->args[0], ret;
  995. struct pdp_ctx *pctx;
  996. if (cb->args[4])
  997. return 0;
  998. list_for_each_entry_rcu(gtp, &gn->gtp_dev_list, list) {
  999. if (last_gtp && last_gtp != gtp)
  1000. continue;
  1001. else
  1002. last_gtp = NULL;
  1003. for (i = k; i < gtp->hash_size; i++) {
  1004. hlist_for_each_entry_rcu(pctx, &gtp->tid_hash[i], hlist_tid) {
  1005. if (tid && tid != pctx->u.tid)
  1006. continue;
  1007. else
  1008. tid = 0;
  1009. ret = gtp_genl_fill_info(skb,
  1010. NETLINK_CB(cb->skb).portid,
  1011. cb->nlh->nlmsg_seq,
  1012. cb->nlh->nlmsg_type, pctx);
  1013. if (ret < 0) {
  1014. cb->args[0] = i;
  1015. cb->args[1] = pctx->u.tid;
  1016. cb->args[2] = (unsigned long)gtp;
  1017. goto out;
  1018. }
  1019. }
  1020. }
  1021. }
  1022. cb->args[4] = 1;
  1023. out:
  1024. return skb->len;
  1025. }
  1026. static struct nla_policy gtp_genl_policy[GTPA_MAX + 1] = {
  1027. [GTPA_LINK] = { .type = NLA_U32, },
  1028. [GTPA_VERSION] = { .type = NLA_U32, },
  1029. [GTPA_TID] = { .type = NLA_U64, },
  1030. [GTPA_PEER_ADDRESS] = { .type = NLA_U32, },
  1031. [GTPA_MS_ADDRESS] = { .type = NLA_U32, },
  1032. [GTPA_FLOW] = { .type = NLA_U16, },
  1033. [GTPA_NET_NS_FD] = { .type = NLA_U32, },
  1034. [GTPA_I_TEI] = { .type = NLA_U32, },
  1035. [GTPA_O_TEI] = { .type = NLA_U32, },
  1036. };
  1037. static const struct genl_ops gtp_genl_ops[] = {
  1038. {
  1039. .cmd = GTP_CMD_NEWPDP,
  1040. .doit = gtp_genl_new_pdp,
  1041. .policy = gtp_genl_policy,
  1042. .flags = GENL_ADMIN_PERM,
  1043. },
  1044. {
  1045. .cmd = GTP_CMD_DELPDP,
  1046. .doit = gtp_genl_del_pdp,
  1047. .policy = gtp_genl_policy,
  1048. .flags = GENL_ADMIN_PERM,
  1049. },
  1050. {
  1051. .cmd = GTP_CMD_GETPDP,
  1052. .doit = gtp_genl_get_pdp,
  1053. .dumpit = gtp_genl_dump_pdp,
  1054. .policy = gtp_genl_policy,
  1055. .flags = GENL_ADMIN_PERM,
  1056. },
  1057. };
  1058. static struct genl_family gtp_genl_family __ro_after_init = {
  1059. .name = "gtp",
  1060. .version = 0,
  1061. .hdrsize = 0,
  1062. .maxattr = GTPA_MAX,
  1063. .netnsok = true,
  1064. .module = THIS_MODULE,
  1065. .ops = gtp_genl_ops,
  1066. .n_ops = ARRAY_SIZE(gtp_genl_ops),
  1067. };
  1068. static int __net_init gtp_net_init(struct net *net)
  1069. {
  1070. struct gtp_net *gn = net_generic(net, gtp_net_id);
  1071. INIT_LIST_HEAD(&gn->gtp_dev_list);
  1072. return 0;
  1073. }
  1074. static void __net_exit gtp_net_exit(struct net *net)
  1075. {
  1076. struct gtp_net *gn = net_generic(net, gtp_net_id);
  1077. struct gtp_dev *gtp;
  1078. LIST_HEAD(list);
  1079. rtnl_lock();
  1080. list_for_each_entry(gtp, &gn->gtp_dev_list, list)
  1081. gtp_dellink(gtp->dev, &list);
  1082. unregister_netdevice_many(&list);
  1083. rtnl_unlock();
  1084. }
  1085. static struct pernet_operations gtp_net_ops = {
  1086. .init = gtp_net_init,
  1087. .exit = gtp_net_exit,
  1088. .id = &gtp_net_id,
  1089. .size = sizeof(struct gtp_net),
  1090. };
  1091. static int __init gtp_init(void)
  1092. {
  1093. int err;
  1094. get_random_bytes(&gtp_h_initval, sizeof(gtp_h_initval));
  1095. err = rtnl_link_register(&gtp_link_ops);
  1096. if (err < 0)
  1097. goto error_out;
  1098. err = genl_register_family(&gtp_genl_family);
  1099. if (err < 0)
  1100. goto unreg_rtnl_link;
  1101. err = register_pernet_subsys(&gtp_net_ops);
  1102. if (err < 0)
  1103. goto unreg_genl_family;
  1104. pr_info("GTP module loaded (pdp ctx size %zd bytes)\n",
  1105. sizeof(struct pdp_ctx));
  1106. return 0;
  1107. unreg_genl_family:
  1108. genl_unregister_family(&gtp_genl_family);
  1109. unreg_rtnl_link:
  1110. rtnl_link_unregister(&gtp_link_ops);
  1111. error_out:
  1112. pr_err("error loading GTP module loaded\n");
  1113. return err;
  1114. }
  1115. late_initcall(gtp_init);
  1116. static void __exit gtp_fini(void)
  1117. {
  1118. unregister_pernet_subsys(&gtp_net_ops);
  1119. genl_unregister_family(&gtp_genl_family);
  1120. rtnl_link_unregister(&gtp_link_ops);
  1121. pr_info("GTP module unloaded\n");
  1122. }
  1123. module_exit(gtp_fini);
  1124. MODULE_LICENSE("GPL");
  1125. MODULE_AUTHOR("Harald Welte <hwelte@sysmocom.de>");
  1126. MODULE_DESCRIPTION("Interface driver for GTP encapsulated traffic");
  1127. MODULE_ALIAS_RTNL_LINK("gtp");
  1128. MODULE_ALIAS_GENL_FAMILY("gtp");