gtp.c 32 KB

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