gtp.c 32 KB

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