gtp.c 31 KB

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