gtp.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945
  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 <[email protected]>
  6. *
  7. * Author: Harald Welte <[email protected]>
  8. * Pablo Neira Ayuso <[email protected]>
  9. * Andreas Schultz <[email protected]>
  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. u8 sk_created;
  60. struct net_device *dev;
  61. struct net *net;
  62. unsigned int role;
  63. unsigned int hash_size;
  64. struct hlist_head *tid_hash;
  65. struct hlist_head *addr_hash;
  66. u8 restart_count;
  67. };
  68. struct echo_info {
  69. struct in_addr ms_addr_ip4;
  70. struct in_addr peer_addr_ip4;
  71. u8 gtp_version;
  72. };
  73. static unsigned int gtp_net_id __read_mostly;
  74. struct gtp_net {
  75. struct list_head gtp_dev_list;
  76. };
  77. static u32 gtp_h_initval;
  78. static struct genl_family gtp_genl_family;
  79. enum gtp_multicast_groups {
  80. GTP_GENL_MCGRP,
  81. };
  82. static const struct genl_multicast_group gtp_genl_mcgrps[] = {
  83. [GTP_GENL_MCGRP] = { .name = GTP_GENL_MCGRP_NAME },
  84. };
  85. static void pdp_context_delete(struct pdp_ctx *pctx);
  86. static inline u32 gtp0_hashfn(u64 tid)
  87. {
  88. u32 *tid32 = (u32 *) &tid;
  89. return jhash_2words(tid32[0], tid32[1], gtp_h_initval);
  90. }
  91. static inline u32 gtp1u_hashfn(u32 tid)
  92. {
  93. return jhash_1word(tid, gtp_h_initval);
  94. }
  95. static inline u32 ipv4_hashfn(__be32 ip)
  96. {
  97. return jhash_1word((__force u32)ip, gtp_h_initval);
  98. }
  99. /* Resolve a PDP context structure based on the 64bit TID. */
  100. static struct pdp_ctx *gtp0_pdp_find(struct gtp_dev *gtp, u64 tid)
  101. {
  102. struct hlist_head *head;
  103. struct pdp_ctx *pdp;
  104. head = &gtp->tid_hash[gtp0_hashfn(tid) % gtp->hash_size];
  105. hlist_for_each_entry_rcu(pdp, head, hlist_tid) {
  106. if (pdp->gtp_version == GTP_V0 &&
  107. pdp->u.v0.tid == tid)
  108. return pdp;
  109. }
  110. return NULL;
  111. }
  112. /* Resolve a PDP context structure based on the 32bit TEI. */
  113. static struct pdp_ctx *gtp1_pdp_find(struct gtp_dev *gtp, u32 tid)
  114. {
  115. struct hlist_head *head;
  116. struct pdp_ctx *pdp;
  117. head = &gtp->tid_hash[gtp1u_hashfn(tid) % gtp->hash_size];
  118. hlist_for_each_entry_rcu(pdp, head, hlist_tid) {
  119. if (pdp->gtp_version == GTP_V1 &&
  120. pdp->u.v1.i_tei == tid)
  121. return pdp;
  122. }
  123. return NULL;
  124. }
  125. /* Resolve a PDP context based on IPv4 address of MS. */
  126. static struct pdp_ctx *ipv4_pdp_find(struct gtp_dev *gtp, __be32 ms_addr)
  127. {
  128. struct hlist_head *head;
  129. struct pdp_ctx *pdp;
  130. head = &gtp->addr_hash[ipv4_hashfn(ms_addr) % gtp->hash_size];
  131. hlist_for_each_entry_rcu(pdp, head, hlist_addr) {
  132. if (pdp->af == AF_INET &&
  133. pdp->ms_addr_ip4.s_addr == ms_addr)
  134. return pdp;
  135. }
  136. return NULL;
  137. }
  138. static bool gtp_check_ms_ipv4(struct sk_buff *skb, struct pdp_ctx *pctx,
  139. unsigned int hdrlen, unsigned int role)
  140. {
  141. struct iphdr *iph;
  142. if (!pskb_may_pull(skb, hdrlen + sizeof(struct iphdr)))
  143. return false;
  144. iph = (struct iphdr *)(skb->data + hdrlen);
  145. if (role == GTP_ROLE_SGSN)
  146. return iph->daddr == pctx->ms_addr_ip4.s_addr;
  147. else
  148. return iph->saddr == pctx->ms_addr_ip4.s_addr;
  149. }
  150. /* Check if the inner IP address in this packet is assigned to any
  151. * existing mobile subscriber.
  152. */
  153. static bool gtp_check_ms(struct sk_buff *skb, struct pdp_ctx *pctx,
  154. unsigned int hdrlen, unsigned int role)
  155. {
  156. switch (ntohs(skb->protocol)) {
  157. case ETH_P_IP:
  158. return gtp_check_ms_ipv4(skb, pctx, hdrlen, role);
  159. }
  160. return false;
  161. }
  162. static int gtp_rx(struct pdp_ctx *pctx, struct sk_buff *skb,
  163. unsigned int hdrlen, unsigned int role)
  164. {
  165. if (!gtp_check_ms(skb, pctx, hdrlen, role)) {
  166. netdev_dbg(pctx->dev, "No PDP ctx for this MS\n");
  167. return 1;
  168. }
  169. /* Get rid of the GTP + UDP headers. */
  170. if (iptunnel_pull_header(skb, hdrlen, skb->protocol,
  171. !net_eq(sock_net(pctx->sk), dev_net(pctx->dev)))) {
  172. pctx->dev->stats.rx_length_errors++;
  173. goto err;
  174. }
  175. netdev_dbg(pctx->dev, "forwarding packet from GGSN to uplink\n");
  176. /* Now that the UDP and the GTP header have been removed, set up the
  177. * new network header. This is required by the upper layer to
  178. * calculate the transport header.
  179. */
  180. skb_reset_network_header(skb);
  181. skb_reset_mac_header(skb);
  182. skb->dev = pctx->dev;
  183. dev_sw_netstats_rx_add(pctx->dev, skb->len);
  184. __netif_rx(skb);
  185. return 0;
  186. err:
  187. pctx->dev->stats.rx_dropped++;
  188. return -1;
  189. }
  190. static struct rtable *ip4_route_output_gtp(struct flowi4 *fl4,
  191. const struct sock *sk,
  192. __be32 daddr, __be32 saddr)
  193. {
  194. memset(fl4, 0, sizeof(*fl4));
  195. fl4->flowi4_oif = sk->sk_bound_dev_if;
  196. fl4->daddr = daddr;
  197. fl4->saddr = saddr;
  198. fl4->flowi4_tos = RT_CONN_FLAGS(sk);
  199. fl4->flowi4_proto = sk->sk_protocol;
  200. return ip_route_output_key(sock_net(sk), fl4);
  201. }
  202. /* GSM TS 09.60. 7.3
  203. * In all Path Management messages:
  204. * - TID: is not used and shall be set to 0.
  205. * - Flow Label is not used and shall be set to 0
  206. * In signalling messages:
  207. * - number: this field is not yet used in signalling messages.
  208. * It shall be set to 255 by the sender and shall be ignored
  209. * by the receiver
  210. * Returns true if the echo req was correct, false otherwise.
  211. */
  212. static bool gtp0_validate_echo_hdr(struct gtp0_header *gtp0)
  213. {
  214. return !(gtp0->tid || (gtp0->flags ^ 0x1e) ||
  215. gtp0->number != 0xff || gtp0->flow);
  216. }
  217. /* msg_type has to be GTP_ECHO_REQ or GTP_ECHO_RSP */
  218. static void gtp0_build_echo_msg(struct gtp0_header *hdr, __u8 msg_type)
  219. {
  220. int len_pkt, len_hdr;
  221. hdr->flags = 0x1e; /* v0, GTP-non-prime. */
  222. hdr->type = msg_type;
  223. /* GSM TS 09.60. 7.3 In all Path Management Flow Label and TID
  224. * are not used and shall be set to 0.
  225. */
  226. hdr->flow = 0;
  227. hdr->tid = 0;
  228. hdr->number = 0xff;
  229. hdr->spare[0] = 0xff;
  230. hdr->spare[1] = 0xff;
  231. hdr->spare[2] = 0xff;
  232. len_pkt = sizeof(struct gtp0_packet);
  233. len_hdr = sizeof(struct gtp0_header);
  234. if (msg_type == GTP_ECHO_RSP)
  235. hdr->length = htons(len_pkt - len_hdr);
  236. else
  237. hdr->length = 0;
  238. }
  239. static int gtp0_send_echo_resp(struct gtp_dev *gtp, struct sk_buff *skb)
  240. {
  241. struct gtp0_packet *gtp_pkt;
  242. struct gtp0_header *gtp0;
  243. struct rtable *rt;
  244. struct flowi4 fl4;
  245. struct iphdr *iph;
  246. __be16 seq;
  247. gtp0 = (struct gtp0_header *)(skb->data + sizeof(struct udphdr));
  248. if (!gtp0_validate_echo_hdr(gtp0))
  249. return -1;
  250. seq = gtp0->seq;
  251. /* pull GTP and UDP headers */
  252. skb_pull_data(skb, sizeof(struct gtp0_header) + sizeof(struct udphdr));
  253. gtp_pkt = skb_push(skb, sizeof(struct gtp0_packet));
  254. memset(gtp_pkt, 0, sizeof(struct gtp0_packet));
  255. gtp0_build_echo_msg(&gtp_pkt->gtp0_h, GTP_ECHO_RSP);
  256. /* GSM TS 09.60. 7.3 The Sequence Number in a signalling response
  257. * message shall be copied from the signalling request message
  258. * that the GSN is replying to.
  259. */
  260. gtp_pkt->gtp0_h.seq = seq;
  261. gtp_pkt->ie.tag = GTPIE_RECOVERY;
  262. gtp_pkt->ie.val = gtp->restart_count;
  263. iph = ip_hdr(skb);
  264. /* find route to the sender,
  265. * src address becomes dst address and vice versa.
  266. */
  267. rt = ip4_route_output_gtp(&fl4, gtp->sk0, iph->saddr, iph->daddr);
  268. if (IS_ERR(rt)) {
  269. netdev_dbg(gtp->dev, "no route for echo response from %pI4\n",
  270. &iph->saddr);
  271. return -1;
  272. }
  273. udp_tunnel_xmit_skb(rt, gtp->sk0, skb,
  274. fl4.saddr, fl4.daddr,
  275. iph->tos,
  276. ip4_dst_hoplimit(&rt->dst),
  277. 0,
  278. htons(GTP0_PORT), htons(GTP0_PORT),
  279. !net_eq(sock_net(gtp->sk1u),
  280. dev_net(gtp->dev)),
  281. false);
  282. return 0;
  283. }
  284. static int gtp_genl_fill_echo(struct sk_buff *skb, u32 snd_portid, u32 snd_seq,
  285. int flags, u32 type, struct echo_info echo)
  286. {
  287. void *genlh;
  288. genlh = genlmsg_put(skb, snd_portid, snd_seq, &gtp_genl_family, flags,
  289. type);
  290. if (!genlh)
  291. goto failure;
  292. if (nla_put_u32(skb, GTPA_VERSION, echo.gtp_version) ||
  293. nla_put_be32(skb, GTPA_PEER_ADDRESS, echo.peer_addr_ip4.s_addr) ||
  294. nla_put_be32(skb, GTPA_MS_ADDRESS, echo.ms_addr_ip4.s_addr))
  295. goto failure;
  296. genlmsg_end(skb, genlh);
  297. return 0;
  298. failure:
  299. genlmsg_cancel(skb, genlh);
  300. return -EMSGSIZE;
  301. }
  302. static int gtp0_handle_echo_resp(struct gtp_dev *gtp, struct sk_buff *skb)
  303. {
  304. struct gtp0_header *gtp0;
  305. struct echo_info echo;
  306. struct sk_buff *msg;
  307. struct iphdr *iph;
  308. int ret;
  309. gtp0 = (struct gtp0_header *)(skb->data + sizeof(struct udphdr));
  310. if (!gtp0_validate_echo_hdr(gtp0))
  311. return -1;
  312. iph = ip_hdr(skb);
  313. echo.ms_addr_ip4.s_addr = iph->daddr;
  314. echo.peer_addr_ip4.s_addr = iph->saddr;
  315. echo.gtp_version = GTP_V0;
  316. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  317. if (!msg)
  318. return -ENOMEM;
  319. ret = gtp_genl_fill_echo(msg, 0, 0, 0, GTP_CMD_ECHOREQ, echo);
  320. if (ret < 0) {
  321. nlmsg_free(msg);
  322. return ret;
  323. }
  324. return genlmsg_multicast_netns(&gtp_genl_family, dev_net(gtp->dev),
  325. msg, 0, GTP_GENL_MCGRP, GFP_ATOMIC);
  326. }
  327. /* 1 means pass up to the stack, -1 means drop and 0 means decapsulated. */
  328. static int gtp0_udp_encap_recv(struct gtp_dev *gtp, struct sk_buff *skb)
  329. {
  330. unsigned int hdrlen = sizeof(struct udphdr) +
  331. sizeof(struct gtp0_header);
  332. struct gtp0_header *gtp0;
  333. struct pdp_ctx *pctx;
  334. if (!pskb_may_pull(skb, hdrlen))
  335. return -1;
  336. gtp0 = (struct gtp0_header *)(skb->data + sizeof(struct udphdr));
  337. if ((gtp0->flags >> 5) != GTP_V0)
  338. return 1;
  339. /* If the sockets were created in kernel, it means that
  340. * there is no daemon running in userspace which would
  341. * handle echo request.
  342. */
  343. if (gtp0->type == GTP_ECHO_REQ && gtp->sk_created)
  344. return gtp0_send_echo_resp(gtp, skb);
  345. if (gtp0->type == GTP_ECHO_RSP && gtp->sk_created)
  346. return gtp0_handle_echo_resp(gtp, skb);
  347. if (gtp0->type != GTP_TPDU)
  348. return 1;
  349. pctx = gtp0_pdp_find(gtp, be64_to_cpu(gtp0->tid));
  350. if (!pctx) {
  351. netdev_dbg(gtp->dev, "No PDP ctx to decap skb=%p\n", skb);
  352. return 1;
  353. }
  354. return gtp_rx(pctx, skb, hdrlen, gtp->role);
  355. }
  356. /* msg_type has to be GTP_ECHO_REQ or GTP_ECHO_RSP */
  357. static void gtp1u_build_echo_msg(struct gtp1_header_long *hdr, __u8 msg_type)
  358. {
  359. int len_pkt, len_hdr;
  360. /* S flag must be set to 1 */
  361. hdr->flags = 0x32; /* v1, GTP-non-prime. */
  362. hdr->type = msg_type;
  363. /* 3GPP TS 29.281 5.1 - TEID has to be set to 0 */
  364. hdr->tid = 0;
  365. /* seq, npdu and next should be counted to the length of the GTP packet
  366. * that's why szie of gtp1_header should be subtracted,
  367. * not size of gtp1_header_long.
  368. */
  369. len_hdr = sizeof(struct gtp1_header);
  370. if (msg_type == GTP_ECHO_RSP) {
  371. len_pkt = sizeof(struct gtp1u_packet);
  372. hdr->length = htons(len_pkt - len_hdr);
  373. } else {
  374. /* GTP_ECHO_REQ does not carry GTP Information Element,
  375. * the why gtp1_header_long is used here.
  376. */
  377. len_pkt = sizeof(struct gtp1_header_long);
  378. hdr->length = htons(len_pkt - len_hdr);
  379. }
  380. }
  381. static int gtp1u_send_echo_resp(struct gtp_dev *gtp, struct sk_buff *skb)
  382. {
  383. struct gtp1_header_long *gtp1u;
  384. struct gtp1u_packet *gtp_pkt;
  385. struct rtable *rt;
  386. struct flowi4 fl4;
  387. struct iphdr *iph;
  388. gtp1u = (struct gtp1_header_long *)(skb->data + sizeof(struct udphdr));
  389. /* 3GPP TS 29.281 5.1 - For the Echo Request, Echo Response,
  390. * Error Indication and Supported Extension Headers Notification
  391. * messages, the S flag shall be set to 1 and TEID shall be set to 0.
  392. */
  393. if (!(gtp1u->flags & GTP1_F_SEQ) || gtp1u->tid)
  394. return -1;
  395. /* pull GTP and UDP headers */
  396. skb_pull_data(skb,
  397. sizeof(struct gtp1_header_long) + sizeof(struct udphdr));
  398. gtp_pkt = skb_push(skb, sizeof(struct gtp1u_packet));
  399. memset(gtp_pkt, 0, sizeof(struct gtp1u_packet));
  400. gtp1u_build_echo_msg(&gtp_pkt->gtp1u_h, GTP_ECHO_RSP);
  401. /* 3GPP TS 29.281 7.7.2 - The Restart Counter value in the
  402. * Recovery information element shall not be used, i.e. it shall
  403. * be set to zero by the sender and shall be ignored by the receiver.
  404. * The Recovery information element is mandatory due to backwards
  405. * compatibility reasons.
  406. */
  407. gtp_pkt->ie.tag = GTPIE_RECOVERY;
  408. gtp_pkt->ie.val = 0;
  409. iph = ip_hdr(skb);
  410. /* find route to the sender,
  411. * src address becomes dst address and vice versa.
  412. */
  413. rt = ip4_route_output_gtp(&fl4, gtp->sk1u, iph->saddr, iph->daddr);
  414. if (IS_ERR(rt)) {
  415. netdev_dbg(gtp->dev, "no route for echo response from %pI4\n",
  416. &iph->saddr);
  417. return -1;
  418. }
  419. udp_tunnel_xmit_skb(rt, gtp->sk1u, skb,
  420. fl4.saddr, fl4.daddr,
  421. iph->tos,
  422. ip4_dst_hoplimit(&rt->dst),
  423. 0,
  424. htons(GTP1U_PORT), htons(GTP1U_PORT),
  425. !net_eq(sock_net(gtp->sk1u),
  426. dev_net(gtp->dev)),
  427. false);
  428. return 0;
  429. }
  430. static int gtp1u_handle_echo_resp(struct gtp_dev *gtp, struct sk_buff *skb)
  431. {
  432. struct gtp1_header_long *gtp1u;
  433. struct echo_info echo;
  434. struct sk_buff *msg;
  435. struct iphdr *iph;
  436. int ret;
  437. gtp1u = (struct gtp1_header_long *)(skb->data + sizeof(struct udphdr));
  438. /* 3GPP TS 29.281 5.1 - For the Echo Request, Echo Response,
  439. * Error Indication and Supported Extension Headers Notification
  440. * messages, the S flag shall be set to 1 and TEID shall be set to 0.
  441. */
  442. if (!(gtp1u->flags & GTP1_F_SEQ) || gtp1u->tid)
  443. return -1;
  444. iph = ip_hdr(skb);
  445. echo.ms_addr_ip4.s_addr = iph->daddr;
  446. echo.peer_addr_ip4.s_addr = iph->saddr;
  447. echo.gtp_version = GTP_V1;
  448. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  449. if (!msg)
  450. return -ENOMEM;
  451. ret = gtp_genl_fill_echo(msg, 0, 0, 0, GTP_CMD_ECHOREQ, echo);
  452. if (ret < 0) {
  453. nlmsg_free(msg);
  454. return ret;
  455. }
  456. return genlmsg_multicast_netns(&gtp_genl_family, dev_net(gtp->dev),
  457. msg, 0, GTP_GENL_MCGRP, GFP_ATOMIC);
  458. }
  459. static int gtp1u_udp_encap_recv(struct gtp_dev *gtp, struct sk_buff *skb)
  460. {
  461. unsigned int hdrlen = sizeof(struct udphdr) +
  462. sizeof(struct gtp1_header);
  463. struct gtp1_header *gtp1;
  464. struct pdp_ctx *pctx;
  465. if (!pskb_may_pull(skb, hdrlen))
  466. return -1;
  467. gtp1 = (struct gtp1_header *)(skb->data + sizeof(struct udphdr));
  468. if ((gtp1->flags >> 5) != GTP_V1)
  469. return 1;
  470. /* If the sockets were created in kernel, it means that
  471. * there is no daemon running in userspace which would
  472. * handle echo request.
  473. */
  474. if (gtp1->type == GTP_ECHO_REQ && gtp->sk_created)
  475. return gtp1u_send_echo_resp(gtp, skb);
  476. if (gtp1->type == GTP_ECHO_RSP && gtp->sk_created)
  477. return gtp1u_handle_echo_resp(gtp, skb);
  478. if (gtp1->type != GTP_TPDU)
  479. return 1;
  480. /* From 29.060: "This field shall be present if and only if any one or
  481. * more of the S, PN and E flags are set.".
  482. *
  483. * If any of the bit is set, then the remaining ones also have to be
  484. * set.
  485. */
  486. if (gtp1->flags & GTP1_F_MASK)
  487. hdrlen += 4;
  488. /* Make sure the header is larger enough, including extensions. */
  489. if (!pskb_may_pull(skb, hdrlen))
  490. return -1;
  491. gtp1 = (struct gtp1_header *)(skb->data + sizeof(struct udphdr));
  492. pctx = gtp1_pdp_find(gtp, ntohl(gtp1->tid));
  493. if (!pctx) {
  494. netdev_dbg(gtp->dev, "No PDP ctx to decap skb=%p\n", skb);
  495. return 1;
  496. }
  497. return gtp_rx(pctx, skb, hdrlen, gtp->role);
  498. }
  499. static void __gtp_encap_destroy(struct sock *sk)
  500. {
  501. struct gtp_dev *gtp;
  502. lock_sock(sk);
  503. gtp = sk->sk_user_data;
  504. if (gtp) {
  505. if (gtp->sk0 == sk)
  506. gtp->sk0 = NULL;
  507. else
  508. gtp->sk1u = NULL;
  509. udp_sk(sk)->encap_type = 0;
  510. rcu_assign_sk_user_data(sk, NULL);
  511. release_sock(sk);
  512. sock_put(sk);
  513. return;
  514. }
  515. release_sock(sk);
  516. }
  517. static void gtp_encap_destroy(struct sock *sk)
  518. {
  519. rtnl_lock();
  520. __gtp_encap_destroy(sk);
  521. rtnl_unlock();
  522. }
  523. static void gtp_encap_disable_sock(struct sock *sk)
  524. {
  525. if (!sk)
  526. return;
  527. __gtp_encap_destroy(sk);
  528. }
  529. static void gtp_encap_disable(struct gtp_dev *gtp)
  530. {
  531. if (gtp->sk_created) {
  532. udp_tunnel_sock_release(gtp->sk0->sk_socket);
  533. udp_tunnel_sock_release(gtp->sk1u->sk_socket);
  534. gtp->sk_created = false;
  535. gtp->sk0 = NULL;
  536. gtp->sk1u = NULL;
  537. } else {
  538. gtp_encap_disable_sock(gtp->sk0);
  539. gtp_encap_disable_sock(gtp->sk1u);
  540. }
  541. }
  542. /* UDP encapsulation receive handler. See net/ipv4/udp.c.
  543. * Return codes: 0: success, <0: error, >0: pass up to userspace UDP socket.
  544. */
  545. static int gtp_encap_recv(struct sock *sk, struct sk_buff *skb)
  546. {
  547. struct gtp_dev *gtp;
  548. int ret = 0;
  549. gtp = rcu_dereference_sk_user_data(sk);
  550. if (!gtp)
  551. return 1;
  552. netdev_dbg(gtp->dev, "encap_recv sk=%p\n", sk);
  553. switch (udp_sk(sk)->encap_type) {
  554. case UDP_ENCAP_GTP0:
  555. netdev_dbg(gtp->dev, "received GTP0 packet\n");
  556. ret = gtp0_udp_encap_recv(gtp, skb);
  557. break;
  558. case UDP_ENCAP_GTP1U:
  559. netdev_dbg(gtp->dev, "received GTP1U packet\n");
  560. ret = gtp1u_udp_encap_recv(gtp, skb);
  561. break;
  562. default:
  563. ret = -1; /* Shouldn't happen. */
  564. }
  565. switch (ret) {
  566. case 1:
  567. netdev_dbg(gtp->dev, "pass up to the process\n");
  568. break;
  569. case 0:
  570. break;
  571. case -1:
  572. netdev_dbg(gtp->dev, "GTP packet has been dropped\n");
  573. kfree_skb(skb);
  574. ret = 0;
  575. break;
  576. }
  577. return ret;
  578. }
  579. static int gtp_dev_init(struct net_device *dev)
  580. {
  581. struct gtp_dev *gtp = netdev_priv(dev);
  582. gtp->dev = dev;
  583. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  584. if (!dev->tstats)
  585. return -ENOMEM;
  586. return 0;
  587. }
  588. static void gtp_dev_uninit(struct net_device *dev)
  589. {
  590. struct gtp_dev *gtp = netdev_priv(dev);
  591. gtp_encap_disable(gtp);
  592. free_percpu(dev->tstats);
  593. }
  594. static inline void gtp0_push_header(struct sk_buff *skb, struct pdp_ctx *pctx)
  595. {
  596. int payload_len = skb->len;
  597. struct gtp0_header *gtp0;
  598. gtp0 = skb_push(skb, sizeof(*gtp0));
  599. gtp0->flags = 0x1e; /* v0, GTP-non-prime. */
  600. gtp0->type = GTP_TPDU;
  601. gtp0->length = htons(payload_len);
  602. gtp0->seq = htons((atomic_inc_return(&pctx->tx_seq) - 1) % 0xffff);
  603. gtp0->flow = htons(pctx->u.v0.flow);
  604. gtp0->number = 0xff;
  605. gtp0->spare[0] = gtp0->spare[1] = gtp0->spare[2] = 0xff;
  606. gtp0->tid = cpu_to_be64(pctx->u.v0.tid);
  607. }
  608. static inline void gtp1_push_header(struct sk_buff *skb, struct pdp_ctx *pctx)
  609. {
  610. int payload_len = skb->len;
  611. struct gtp1_header *gtp1;
  612. gtp1 = skb_push(skb, sizeof(*gtp1));
  613. /* Bits 8 7 6 5 4 3 2 1
  614. * +--+--+--+--+--+--+--+--+
  615. * |version |PT| 0| E| S|PN|
  616. * +--+--+--+--+--+--+--+--+
  617. * 0 0 1 1 1 0 0 0
  618. */
  619. gtp1->flags = 0x30; /* v1, GTP-non-prime. */
  620. gtp1->type = GTP_TPDU;
  621. gtp1->length = htons(payload_len);
  622. gtp1->tid = htonl(pctx->u.v1.o_tei);
  623. /* TODO: Support for extension header, sequence number and N-PDU.
  624. * Update the length field if any of them is available.
  625. */
  626. }
  627. struct gtp_pktinfo {
  628. struct sock *sk;
  629. struct iphdr *iph;
  630. struct flowi4 fl4;
  631. struct rtable *rt;
  632. struct pdp_ctx *pctx;
  633. struct net_device *dev;
  634. __be16 gtph_port;
  635. };
  636. static void gtp_push_header(struct sk_buff *skb, struct gtp_pktinfo *pktinfo)
  637. {
  638. switch (pktinfo->pctx->gtp_version) {
  639. case GTP_V0:
  640. pktinfo->gtph_port = htons(GTP0_PORT);
  641. gtp0_push_header(skb, pktinfo->pctx);
  642. break;
  643. case GTP_V1:
  644. pktinfo->gtph_port = htons(GTP1U_PORT);
  645. gtp1_push_header(skb, pktinfo->pctx);
  646. break;
  647. }
  648. }
  649. static inline void gtp_set_pktinfo_ipv4(struct gtp_pktinfo *pktinfo,
  650. struct sock *sk, struct iphdr *iph,
  651. struct pdp_ctx *pctx, struct rtable *rt,
  652. struct flowi4 *fl4,
  653. struct net_device *dev)
  654. {
  655. pktinfo->sk = sk;
  656. pktinfo->iph = iph;
  657. pktinfo->pctx = pctx;
  658. pktinfo->rt = rt;
  659. pktinfo->fl4 = *fl4;
  660. pktinfo->dev = dev;
  661. }
  662. static int gtp_build_skb_ip4(struct sk_buff *skb, struct net_device *dev,
  663. struct gtp_pktinfo *pktinfo)
  664. {
  665. struct gtp_dev *gtp = netdev_priv(dev);
  666. struct pdp_ctx *pctx;
  667. struct rtable *rt;
  668. struct flowi4 fl4;
  669. struct iphdr *iph;
  670. __be16 df;
  671. int mtu;
  672. /* Read the IP destination address and resolve the PDP context.
  673. * Prepend PDP header with TEI/TID from PDP ctx.
  674. */
  675. iph = ip_hdr(skb);
  676. if (gtp->role == GTP_ROLE_SGSN)
  677. pctx = ipv4_pdp_find(gtp, iph->saddr);
  678. else
  679. pctx = ipv4_pdp_find(gtp, iph->daddr);
  680. if (!pctx) {
  681. netdev_dbg(dev, "no PDP ctx found for %pI4, skip\n",
  682. &iph->daddr);
  683. return -ENOENT;
  684. }
  685. netdev_dbg(dev, "found PDP context %p\n", pctx);
  686. rt = ip4_route_output_gtp(&fl4, pctx->sk, pctx->peer_addr_ip4.s_addr,
  687. inet_sk(pctx->sk)->inet_saddr);
  688. if (IS_ERR(rt)) {
  689. netdev_dbg(dev, "no route to SSGN %pI4\n",
  690. &pctx->peer_addr_ip4.s_addr);
  691. dev->stats.tx_carrier_errors++;
  692. goto err;
  693. }
  694. if (rt->dst.dev == dev) {
  695. netdev_dbg(dev, "circular route to SSGN %pI4\n",
  696. &pctx->peer_addr_ip4.s_addr);
  697. dev->stats.collisions++;
  698. goto err_rt;
  699. }
  700. /* This is similar to tnl_update_pmtu(). */
  701. df = iph->frag_off;
  702. if (df) {
  703. mtu = dst_mtu(&rt->dst) - dev->hard_header_len -
  704. sizeof(struct iphdr) - sizeof(struct udphdr);
  705. switch (pctx->gtp_version) {
  706. case GTP_V0:
  707. mtu -= sizeof(struct gtp0_header);
  708. break;
  709. case GTP_V1:
  710. mtu -= sizeof(struct gtp1_header);
  711. break;
  712. }
  713. } else {
  714. mtu = dst_mtu(&rt->dst);
  715. }
  716. skb_dst_update_pmtu_no_confirm(skb, mtu);
  717. if (iph->frag_off & htons(IP_DF) &&
  718. ((!skb_is_gso(skb) && skb->len > mtu) ||
  719. (skb_is_gso(skb) && !skb_gso_validate_network_len(skb, mtu)))) {
  720. netdev_dbg(dev, "packet too big, fragmentation needed\n");
  721. icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  722. htonl(mtu));
  723. goto err_rt;
  724. }
  725. gtp_set_pktinfo_ipv4(pktinfo, pctx->sk, iph, pctx, rt, &fl4, dev);
  726. gtp_push_header(skb, pktinfo);
  727. return 0;
  728. err_rt:
  729. ip_rt_put(rt);
  730. err:
  731. return -EBADMSG;
  732. }
  733. static netdev_tx_t gtp_dev_xmit(struct sk_buff *skb, struct net_device *dev)
  734. {
  735. unsigned int proto = ntohs(skb->protocol);
  736. struct gtp_pktinfo pktinfo;
  737. int err;
  738. /* Ensure there is sufficient headroom. */
  739. if (skb_cow_head(skb, dev->needed_headroom))
  740. goto tx_err;
  741. skb_reset_inner_headers(skb);
  742. /* PDP context lookups in gtp_build_skb_*() need rcu read-side lock. */
  743. rcu_read_lock();
  744. switch (proto) {
  745. case ETH_P_IP:
  746. err = gtp_build_skb_ip4(skb, dev, &pktinfo);
  747. break;
  748. default:
  749. err = -EOPNOTSUPP;
  750. break;
  751. }
  752. rcu_read_unlock();
  753. if (err < 0)
  754. goto tx_err;
  755. switch (proto) {
  756. case ETH_P_IP:
  757. netdev_dbg(pktinfo.dev, "gtp -> IP src: %pI4 dst: %pI4\n",
  758. &pktinfo.iph->saddr, &pktinfo.iph->daddr);
  759. udp_tunnel_xmit_skb(pktinfo.rt, pktinfo.sk, skb,
  760. pktinfo.fl4.saddr, pktinfo.fl4.daddr,
  761. pktinfo.iph->tos,
  762. ip4_dst_hoplimit(&pktinfo.rt->dst),
  763. 0,
  764. pktinfo.gtph_port, pktinfo.gtph_port,
  765. !net_eq(sock_net(pktinfo.pctx->sk),
  766. dev_net(dev)),
  767. false);
  768. break;
  769. }
  770. return NETDEV_TX_OK;
  771. tx_err:
  772. dev->stats.tx_errors++;
  773. dev_kfree_skb(skb);
  774. return NETDEV_TX_OK;
  775. }
  776. static const struct net_device_ops gtp_netdev_ops = {
  777. .ndo_init = gtp_dev_init,
  778. .ndo_uninit = gtp_dev_uninit,
  779. .ndo_start_xmit = gtp_dev_xmit,
  780. .ndo_get_stats64 = dev_get_tstats64,
  781. };
  782. static const struct device_type gtp_type = {
  783. .name = "gtp",
  784. };
  785. static void gtp_link_setup(struct net_device *dev)
  786. {
  787. unsigned int max_gtp_header_len = sizeof(struct iphdr) +
  788. sizeof(struct udphdr) +
  789. sizeof(struct gtp0_header);
  790. dev->netdev_ops = &gtp_netdev_ops;
  791. dev->needs_free_netdev = true;
  792. SET_NETDEV_DEVTYPE(dev, &gtp_type);
  793. dev->hard_header_len = 0;
  794. dev->addr_len = 0;
  795. dev->mtu = ETH_DATA_LEN - max_gtp_header_len;
  796. /* Zero header length. */
  797. dev->type = ARPHRD_NONE;
  798. dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  799. dev->priv_flags |= IFF_NO_QUEUE;
  800. dev->features |= NETIF_F_LLTX;
  801. netif_keep_dst(dev);
  802. dev->needed_headroom = LL_MAX_HEADER + max_gtp_header_len;
  803. }
  804. static int gtp_hashtable_new(struct gtp_dev *gtp, int hsize);
  805. static int gtp_encap_enable(struct gtp_dev *gtp, struct nlattr *data[]);
  806. static void gtp_destructor(struct net_device *dev)
  807. {
  808. struct gtp_dev *gtp = netdev_priv(dev);
  809. kfree(gtp->addr_hash);
  810. kfree(gtp->tid_hash);
  811. }
  812. static struct sock *gtp_create_sock(int type, struct gtp_dev *gtp)
  813. {
  814. struct udp_tunnel_sock_cfg tuncfg = {};
  815. struct udp_port_cfg udp_conf = {
  816. .local_ip.s_addr = htonl(INADDR_ANY),
  817. .family = AF_INET,
  818. };
  819. struct net *net = gtp->net;
  820. struct socket *sock;
  821. int err;
  822. if (type == UDP_ENCAP_GTP0)
  823. udp_conf.local_udp_port = htons(GTP0_PORT);
  824. else if (type == UDP_ENCAP_GTP1U)
  825. udp_conf.local_udp_port = htons(GTP1U_PORT);
  826. else
  827. return ERR_PTR(-EINVAL);
  828. err = udp_sock_create(net, &udp_conf, &sock);
  829. if (err)
  830. return ERR_PTR(err);
  831. tuncfg.sk_user_data = gtp;
  832. tuncfg.encap_type = type;
  833. tuncfg.encap_rcv = gtp_encap_recv;
  834. tuncfg.encap_destroy = NULL;
  835. setup_udp_tunnel_sock(net, sock, &tuncfg);
  836. return sock->sk;
  837. }
  838. static int gtp_create_sockets(struct gtp_dev *gtp, struct nlattr *data[])
  839. {
  840. struct sock *sk1u = NULL;
  841. struct sock *sk0 = NULL;
  842. sk0 = gtp_create_sock(UDP_ENCAP_GTP0, gtp);
  843. if (IS_ERR(sk0))
  844. return PTR_ERR(sk0);
  845. sk1u = gtp_create_sock(UDP_ENCAP_GTP1U, gtp);
  846. if (IS_ERR(sk1u)) {
  847. udp_tunnel_sock_release(sk0->sk_socket);
  848. return PTR_ERR(sk1u);
  849. }
  850. gtp->sk_created = true;
  851. gtp->sk0 = sk0;
  852. gtp->sk1u = sk1u;
  853. return 0;
  854. }
  855. static int gtp_newlink(struct net *src_net, struct net_device *dev,
  856. struct nlattr *tb[], struct nlattr *data[],
  857. struct netlink_ext_ack *extack)
  858. {
  859. unsigned int role = GTP_ROLE_GGSN;
  860. struct gtp_dev *gtp;
  861. struct gtp_net *gn;
  862. int hashsize, err;
  863. gtp = netdev_priv(dev);
  864. if (!data[IFLA_GTP_PDP_HASHSIZE]) {
  865. hashsize = 1024;
  866. } else {
  867. hashsize = nla_get_u32(data[IFLA_GTP_PDP_HASHSIZE]);
  868. if (!hashsize)
  869. hashsize = 1024;
  870. }
  871. if (data[IFLA_GTP_ROLE]) {
  872. role = nla_get_u32(data[IFLA_GTP_ROLE]);
  873. if (role > GTP_ROLE_SGSN)
  874. return -EINVAL;
  875. }
  876. gtp->role = role;
  877. if (!data[IFLA_GTP_RESTART_COUNT])
  878. gtp->restart_count = 0;
  879. else
  880. gtp->restart_count = nla_get_u8(data[IFLA_GTP_RESTART_COUNT]);
  881. gtp->net = src_net;
  882. err = gtp_hashtable_new(gtp, hashsize);
  883. if (err < 0)
  884. return err;
  885. if (data[IFLA_GTP_CREATE_SOCKETS])
  886. err = gtp_create_sockets(gtp, data);
  887. else
  888. err = gtp_encap_enable(gtp, data);
  889. if (err < 0)
  890. goto out_hashtable;
  891. err = register_netdevice(dev);
  892. if (err < 0) {
  893. netdev_dbg(dev, "failed to register new netdev %d\n", err);
  894. goto out_encap;
  895. }
  896. gn = net_generic(dev_net(dev), gtp_net_id);
  897. list_add_rcu(&gtp->list, &gn->gtp_dev_list);
  898. dev->priv_destructor = gtp_destructor;
  899. netdev_dbg(dev, "registered new GTP interface\n");
  900. return 0;
  901. out_encap:
  902. gtp_encap_disable(gtp);
  903. out_hashtable:
  904. kfree(gtp->addr_hash);
  905. kfree(gtp->tid_hash);
  906. return err;
  907. }
  908. static void gtp_dellink(struct net_device *dev, struct list_head *head)
  909. {
  910. struct gtp_dev *gtp = netdev_priv(dev);
  911. struct pdp_ctx *pctx;
  912. int i;
  913. for (i = 0; i < gtp->hash_size; i++)
  914. hlist_for_each_entry_rcu(pctx, &gtp->tid_hash[i], hlist_tid)
  915. pdp_context_delete(pctx);
  916. list_del_rcu(&gtp->list);
  917. unregister_netdevice_queue(dev, head);
  918. }
  919. static const struct nla_policy gtp_policy[IFLA_GTP_MAX + 1] = {
  920. [IFLA_GTP_FD0] = { .type = NLA_U32 },
  921. [IFLA_GTP_FD1] = { .type = NLA_U32 },
  922. [IFLA_GTP_PDP_HASHSIZE] = { .type = NLA_U32 },
  923. [IFLA_GTP_ROLE] = { .type = NLA_U32 },
  924. [IFLA_GTP_CREATE_SOCKETS] = { .type = NLA_U8 },
  925. [IFLA_GTP_RESTART_COUNT] = { .type = NLA_U8 },
  926. };
  927. static int gtp_validate(struct nlattr *tb[], struct nlattr *data[],
  928. struct netlink_ext_ack *extack)
  929. {
  930. if (!data)
  931. return -EINVAL;
  932. return 0;
  933. }
  934. static size_t gtp_get_size(const struct net_device *dev)
  935. {
  936. return nla_total_size(sizeof(__u32)) + /* IFLA_GTP_PDP_HASHSIZE */
  937. nla_total_size(sizeof(__u32)) + /* IFLA_GTP_ROLE */
  938. nla_total_size(sizeof(__u8)); /* IFLA_GTP_RESTART_COUNT */
  939. }
  940. static int gtp_fill_info(struct sk_buff *skb, const struct net_device *dev)
  941. {
  942. struct gtp_dev *gtp = netdev_priv(dev);
  943. if (nla_put_u32(skb, IFLA_GTP_PDP_HASHSIZE, gtp->hash_size))
  944. goto nla_put_failure;
  945. if (nla_put_u32(skb, IFLA_GTP_ROLE, gtp->role))
  946. goto nla_put_failure;
  947. if (nla_put_u8(skb, IFLA_GTP_RESTART_COUNT, gtp->restart_count))
  948. goto nla_put_failure;
  949. return 0;
  950. nla_put_failure:
  951. return -EMSGSIZE;
  952. }
  953. static struct rtnl_link_ops gtp_link_ops __read_mostly = {
  954. .kind = "gtp",
  955. .maxtype = IFLA_GTP_MAX,
  956. .policy = gtp_policy,
  957. .priv_size = sizeof(struct gtp_dev),
  958. .setup = gtp_link_setup,
  959. .validate = gtp_validate,
  960. .newlink = gtp_newlink,
  961. .dellink = gtp_dellink,
  962. .get_size = gtp_get_size,
  963. .fill_info = gtp_fill_info,
  964. };
  965. static int gtp_hashtable_new(struct gtp_dev *gtp, int hsize)
  966. {
  967. int i;
  968. gtp->addr_hash = kmalloc_array(hsize, sizeof(struct hlist_head),
  969. GFP_KERNEL | __GFP_NOWARN);
  970. if (gtp->addr_hash == NULL)
  971. return -ENOMEM;
  972. gtp->tid_hash = kmalloc_array(hsize, sizeof(struct hlist_head),
  973. GFP_KERNEL | __GFP_NOWARN);
  974. if (gtp->tid_hash == NULL)
  975. goto err1;
  976. gtp->hash_size = hsize;
  977. for (i = 0; i < hsize; i++) {
  978. INIT_HLIST_HEAD(&gtp->addr_hash[i]);
  979. INIT_HLIST_HEAD(&gtp->tid_hash[i]);
  980. }
  981. return 0;
  982. err1:
  983. kfree(gtp->addr_hash);
  984. return -ENOMEM;
  985. }
  986. static struct sock *gtp_encap_enable_socket(int fd, int type,
  987. struct gtp_dev *gtp)
  988. {
  989. struct udp_tunnel_sock_cfg tuncfg = {NULL};
  990. struct socket *sock;
  991. struct sock *sk;
  992. int err;
  993. pr_debug("enable gtp on %d, %d\n", fd, type);
  994. sock = sockfd_lookup(fd, &err);
  995. if (!sock) {
  996. pr_debug("gtp socket fd=%d not found\n", fd);
  997. return NULL;
  998. }
  999. sk = sock->sk;
  1000. if (sk->sk_protocol != IPPROTO_UDP ||
  1001. sk->sk_type != SOCK_DGRAM ||
  1002. (sk->sk_family != AF_INET && sk->sk_family != AF_INET6)) {
  1003. pr_debug("socket fd=%d not UDP\n", fd);
  1004. sk = ERR_PTR(-EINVAL);
  1005. goto out_sock;
  1006. }
  1007. lock_sock(sk);
  1008. if (sk->sk_user_data) {
  1009. sk = ERR_PTR(-EBUSY);
  1010. goto out_rel_sock;
  1011. }
  1012. sock_hold(sk);
  1013. tuncfg.sk_user_data = gtp;
  1014. tuncfg.encap_type = type;
  1015. tuncfg.encap_rcv = gtp_encap_recv;
  1016. tuncfg.encap_destroy = gtp_encap_destroy;
  1017. setup_udp_tunnel_sock(sock_net(sock->sk), sock, &tuncfg);
  1018. out_rel_sock:
  1019. release_sock(sock->sk);
  1020. out_sock:
  1021. sockfd_put(sock);
  1022. return sk;
  1023. }
  1024. static int gtp_encap_enable(struct gtp_dev *gtp, struct nlattr *data[])
  1025. {
  1026. struct sock *sk1u = NULL;
  1027. struct sock *sk0 = NULL;
  1028. if (!data[IFLA_GTP_FD0] && !data[IFLA_GTP_FD1])
  1029. return -EINVAL;
  1030. if (data[IFLA_GTP_FD0]) {
  1031. u32 fd0 = nla_get_u32(data[IFLA_GTP_FD0]);
  1032. sk0 = gtp_encap_enable_socket(fd0, UDP_ENCAP_GTP0, gtp);
  1033. if (IS_ERR(sk0))
  1034. return PTR_ERR(sk0);
  1035. }
  1036. if (data[IFLA_GTP_FD1]) {
  1037. u32 fd1 = nla_get_u32(data[IFLA_GTP_FD1]);
  1038. sk1u = gtp_encap_enable_socket(fd1, UDP_ENCAP_GTP1U, gtp);
  1039. if (IS_ERR(sk1u)) {
  1040. gtp_encap_disable_sock(sk0);
  1041. return PTR_ERR(sk1u);
  1042. }
  1043. }
  1044. gtp->sk0 = sk0;
  1045. gtp->sk1u = sk1u;
  1046. return 0;
  1047. }
  1048. static struct gtp_dev *gtp_find_dev(struct net *src_net, struct nlattr *nla[])
  1049. {
  1050. struct gtp_dev *gtp = NULL;
  1051. struct net_device *dev;
  1052. struct net *net;
  1053. /* Examine the link attributes and figure out which network namespace
  1054. * we are talking about.
  1055. */
  1056. if (nla[GTPA_NET_NS_FD])
  1057. net = get_net_ns_by_fd(nla_get_u32(nla[GTPA_NET_NS_FD]));
  1058. else
  1059. net = get_net(src_net);
  1060. if (IS_ERR(net))
  1061. return NULL;
  1062. /* Check if there's an existing gtpX device to configure */
  1063. dev = dev_get_by_index_rcu(net, nla_get_u32(nla[GTPA_LINK]));
  1064. if (dev && dev->netdev_ops == &gtp_netdev_ops)
  1065. gtp = netdev_priv(dev);
  1066. put_net(net);
  1067. return gtp;
  1068. }
  1069. static void ipv4_pdp_fill(struct pdp_ctx *pctx, struct genl_info *info)
  1070. {
  1071. pctx->gtp_version = nla_get_u32(info->attrs[GTPA_VERSION]);
  1072. pctx->af = AF_INET;
  1073. pctx->peer_addr_ip4.s_addr =
  1074. nla_get_be32(info->attrs[GTPA_PEER_ADDRESS]);
  1075. pctx->ms_addr_ip4.s_addr =
  1076. nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
  1077. switch (pctx->gtp_version) {
  1078. case GTP_V0:
  1079. /* According to TS 09.60, sections 7.5.1 and 7.5.2, the flow
  1080. * label needs to be the same for uplink and downlink packets,
  1081. * so let's annotate this.
  1082. */
  1083. pctx->u.v0.tid = nla_get_u64(info->attrs[GTPA_TID]);
  1084. pctx->u.v0.flow = nla_get_u16(info->attrs[GTPA_FLOW]);
  1085. break;
  1086. case GTP_V1:
  1087. pctx->u.v1.i_tei = nla_get_u32(info->attrs[GTPA_I_TEI]);
  1088. pctx->u.v1.o_tei = nla_get_u32(info->attrs[GTPA_O_TEI]);
  1089. break;
  1090. default:
  1091. break;
  1092. }
  1093. }
  1094. static struct pdp_ctx *gtp_pdp_add(struct gtp_dev *gtp, struct sock *sk,
  1095. struct genl_info *info)
  1096. {
  1097. struct pdp_ctx *pctx, *pctx_tid = NULL;
  1098. struct net_device *dev = gtp->dev;
  1099. u32 hash_ms, hash_tid = 0;
  1100. unsigned int version;
  1101. bool found = false;
  1102. __be32 ms_addr;
  1103. ms_addr = nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
  1104. hash_ms = ipv4_hashfn(ms_addr) % gtp->hash_size;
  1105. version = nla_get_u32(info->attrs[GTPA_VERSION]);
  1106. pctx = ipv4_pdp_find(gtp, ms_addr);
  1107. if (pctx)
  1108. found = true;
  1109. if (version == GTP_V0)
  1110. pctx_tid = gtp0_pdp_find(gtp,
  1111. nla_get_u64(info->attrs[GTPA_TID]));
  1112. else if (version == GTP_V1)
  1113. pctx_tid = gtp1_pdp_find(gtp,
  1114. nla_get_u32(info->attrs[GTPA_I_TEI]));
  1115. if (pctx_tid)
  1116. found = true;
  1117. if (found) {
  1118. if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
  1119. return ERR_PTR(-EEXIST);
  1120. if (info->nlhdr->nlmsg_flags & NLM_F_REPLACE)
  1121. return ERR_PTR(-EOPNOTSUPP);
  1122. if (pctx && pctx_tid)
  1123. return ERR_PTR(-EEXIST);
  1124. if (!pctx)
  1125. pctx = pctx_tid;
  1126. ipv4_pdp_fill(pctx, info);
  1127. if (pctx->gtp_version == GTP_V0)
  1128. netdev_dbg(dev, "GTPv0-U: update tunnel id = %llx (pdp %p)\n",
  1129. pctx->u.v0.tid, pctx);
  1130. else if (pctx->gtp_version == GTP_V1)
  1131. netdev_dbg(dev, "GTPv1-U: update tunnel id = %x/%x (pdp %p)\n",
  1132. pctx->u.v1.i_tei, pctx->u.v1.o_tei, pctx);
  1133. return pctx;
  1134. }
  1135. pctx = kmalloc(sizeof(*pctx), GFP_ATOMIC);
  1136. if (pctx == NULL)
  1137. return ERR_PTR(-ENOMEM);
  1138. sock_hold(sk);
  1139. pctx->sk = sk;
  1140. pctx->dev = gtp->dev;
  1141. ipv4_pdp_fill(pctx, info);
  1142. atomic_set(&pctx->tx_seq, 0);
  1143. switch (pctx->gtp_version) {
  1144. case GTP_V0:
  1145. /* TS 09.60: "The flow label identifies unambiguously a GTP
  1146. * flow.". We use the tid for this instead, I cannot find a
  1147. * situation in which this doesn't unambiguosly identify the
  1148. * PDP context.
  1149. */
  1150. hash_tid = gtp0_hashfn(pctx->u.v0.tid) % gtp->hash_size;
  1151. break;
  1152. case GTP_V1:
  1153. hash_tid = gtp1u_hashfn(pctx->u.v1.i_tei) % gtp->hash_size;
  1154. break;
  1155. }
  1156. hlist_add_head_rcu(&pctx->hlist_addr, &gtp->addr_hash[hash_ms]);
  1157. hlist_add_head_rcu(&pctx->hlist_tid, &gtp->tid_hash[hash_tid]);
  1158. switch (pctx->gtp_version) {
  1159. case GTP_V0:
  1160. netdev_dbg(dev, "GTPv0-U: new PDP ctx id=%llx ssgn=%pI4 ms=%pI4 (pdp=%p)\n",
  1161. pctx->u.v0.tid, &pctx->peer_addr_ip4,
  1162. &pctx->ms_addr_ip4, pctx);
  1163. break;
  1164. case GTP_V1:
  1165. netdev_dbg(dev, "GTPv1-U: new PDP ctx id=%x/%x ssgn=%pI4 ms=%pI4 (pdp=%p)\n",
  1166. pctx->u.v1.i_tei, pctx->u.v1.o_tei,
  1167. &pctx->peer_addr_ip4, &pctx->ms_addr_ip4, pctx);
  1168. break;
  1169. }
  1170. return pctx;
  1171. }
  1172. static void pdp_context_free(struct rcu_head *head)
  1173. {
  1174. struct pdp_ctx *pctx = container_of(head, struct pdp_ctx, rcu_head);
  1175. sock_put(pctx->sk);
  1176. kfree(pctx);
  1177. }
  1178. static void pdp_context_delete(struct pdp_ctx *pctx)
  1179. {
  1180. hlist_del_rcu(&pctx->hlist_tid);
  1181. hlist_del_rcu(&pctx->hlist_addr);
  1182. call_rcu(&pctx->rcu_head, pdp_context_free);
  1183. }
  1184. static int gtp_tunnel_notify(struct pdp_ctx *pctx, u8 cmd, gfp_t allocation);
  1185. static int gtp_genl_new_pdp(struct sk_buff *skb, struct genl_info *info)
  1186. {
  1187. unsigned int version;
  1188. struct pdp_ctx *pctx;
  1189. struct gtp_dev *gtp;
  1190. struct sock *sk;
  1191. int err;
  1192. if (!info->attrs[GTPA_VERSION] ||
  1193. !info->attrs[GTPA_LINK] ||
  1194. !info->attrs[GTPA_PEER_ADDRESS] ||
  1195. !info->attrs[GTPA_MS_ADDRESS])
  1196. return -EINVAL;
  1197. version = nla_get_u32(info->attrs[GTPA_VERSION]);
  1198. switch (version) {
  1199. case GTP_V0:
  1200. if (!info->attrs[GTPA_TID] ||
  1201. !info->attrs[GTPA_FLOW])
  1202. return -EINVAL;
  1203. break;
  1204. case GTP_V1:
  1205. if (!info->attrs[GTPA_I_TEI] ||
  1206. !info->attrs[GTPA_O_TEI])
  1207. return -EINVAL;
  1208. break;
  1209. default:
  1210. return -EINVAL;
  1211. }
  1212. rtnl_lock();
  1213. gtp = gtp_find_dev(sock_net(skb->sk), info->attrs);
  1214. if (!gtp) {
  1215. err = -ENODEV;
  1216. goto out_unlock;
  1217. }
  1218. if (version == GTP_V0)
  1219. sk = gtp->sk0;
  1220. else if (version == GTP_V1)
  1221. sk = gtp->sk1u;
  1222. else
  1223. sk = NULL;
  1224. if (!sk) {
  1225. err = -ENODEV;
  1226. goto out_unlock;
  1227. }
  1228. pctx = gtp_pdp_add(gtp, sk, info);
  1229. if (IS_ERR(pctx)) {
  1230. err = PTR_ERR(pctx);
  1231. } else {
  1232. gtp_tunnel_notify(pctx, GTP_CMD_NEWPDP, GFP_KERNEL);
  1233. err = 0;
  1234. }
  1235. out_unlock:
  1236. rtnl_unlock();
  1237. return err;
  1238. }
  1239. static struct pdp_ctx *gtp_find_pdp_by_link(struct net *net,
  1240. struct nlattr *nla[])
  1241. {
  1242. struct gtp_dev *gtp;
  1243. gtp = gtp_find_dev(net, nla);
  1244. if (!gtp)
  1245. return ERR_PTR(-ENODEV);
  1246. if (nla[GTPA_MS_ADDRESS]) {
  1247. __be32 ip = nla_get_be32(nla[GTPA_MS_ADDRESS]);
  1248. return ipv4_pdp_find(gtp, ip);
  1249. } else if (nla[GTPA_VERSION]) {
  1250. u32 gtp_version = nla_get_u32(nla[GTPA_VERSION]);
  1251. if (gtp_version == GTP_V0 && nla[GTPA_TID])
  1252. return gtp0_pdp_find(gtp, nla_get_u64(nla[GTPA_TID]));
  1253. else if (gtp_version == GTP_V1 && nla[GTPA_I_TEI])
  1254. return gtp1_pdp_find(gtp, nla_get_u32(nla[GTPA_I_TEI]));
  1255. }
  1256. return ERR_PTR(-EINVAL);
  1257. }
  1258. static struct pdp_ctx *gtp_find_pdp(struct net *net, struct nlattr *nla[])
  1259. {
  1260. struct pdp_ctx *pctx;
  1261. if (nla[GTPA_LINK])
  1262. pctx = gtp_find_pdp_by_link(net, nla);
  1263. else
  1264. pctx = ERR_PTR(-EINVAL);
  1265. if (!pctx)
  1266. pctx = ERR_PTR(-ENOENT);
  1267. return pctx;
  1268. }
  1269. static int gtp_genl_del_pdp(struct sk_buff *skb, struct genl_info *info)
  1270. {
  1271. struct pdp_ctx *pctx;
  1272. int err = 0;
  1273. if (!info->attrs[GTPA_VERSION])
  1274. return -EINVAL;
  1275. rcu_read_lock();
  1276. pctx = gtp_find_pdp(sock_net(skb->sk), info->attrs);
  1277. if (IS_ERR(pctx)) {
  1278. err = PTR_ERR(pctx);
  1279. goto out_unlock;
  1280. }
  1281. if (pctx->gtp_version == GTP_V0)
  1282. netdev_dbg(pctx->dev, "GTPv0-U: deleting tunnel id = %llx (pdp %p)\n",
  1283. pctx->u.v0.tid, pctx);
  1284. else if (pctx->gtp_version == GTP_V1)
  1285. netdev_dbg(pctx->dev, "GTPv1-U: deleting tunnel id = %x/%x (pdp %p)\n",
  1286. pctx->u.v1.i_tei, pctx->u.v1.o_tei, pctx);
  1287. gtp_tunnel_notify(pctx, GTP_CMD_DELPDP, GFP_ATOMIC);
  1288. pdp_context_delete(pctx);
  1289. out_unlock:
  1290. rcu_read_unlock();
  1291. return err;
  1292. }
  1293. static int gtp_genl_fill_info(struct sk_buff *skb, u32 snd_portid, u32 snd_seq,
  1294. int flags, u32 type, struct pdp_ctx *pctx)
  1295. {
  1296. void *genlh;
  1297. genlh = genlmsg_put(skb, snd_portid, snd_seq, &gtp_genl_family, flags,
  1298. type);
  1299. if (genlh == NULL)
  1300. goto nlmsg_failure;
  1301. if (nla_put_u32(skb, GTPA_VERSION, pctx->gtp_version) ||
  1302. nla_put_u32(skb, GTPA_LINK, pctx->dev->ifindex) ||
  1303. nla_put_be32(skb, GTPA_PEER_ADDRESS, pctx->peer_addr_ip4.s_addr) ||
  1304. nla_put_be32(skb, GTPA_MS_ADDRESS, pctx->ms_addr_ip4.s_addr))
  1305. goto nla_put_failure;
  1306. switch (pctx->gtp_version) {
  1307. case GTP_V0:
  1308. if (nla_put_u64_64bit(skb, GTPA_TID, pctx->u.v0.tid, GTPA_PAD) ||
  1309. nla_put_u16(skb, GTPA_FLOW, pctx->u.v0.flow))
  1310. goto nla_put_failure;
  1311. break;
  1312. case GTP_V1:
  1313. if (nla_put_u32(skb, GTPA_I_TEI, pctx->u.v1.i_tei) ||
  1314. nla_put_u32(skb, GTPA_O_TEI, pctx->u.v1.o_tei))
  1315. goto nla_put_failure;
  1316. break;
  1317. }
  1318. genlmsg_end(skb, genlh);
  1319. return 0;
  1320. nlmsg_failure:
  1321. nla_put_failure:
  1322. genlmsg_cancel(skb, genlh);
  1323. return -EMSGSIZE;
  1324. }
  1325. static int gtp_tunnel_notify(struct pdp_ctx *pctx, u8 cmd, gfp_t allocation)
  1326. {
  1327. struct sk_buff *msg;
  1328. int ret;
  1329. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, allocation);
  1330. if (!msg)
  1331. return -ENOMEM;
  1332. ret = gtp_genl_fill_info(msg, 0, 0, 0, cmd, pctx);
  1333. if (ret < 0) {
  1334. nlmsg_free(msg);
  1335. return ret;
  1336. }
  1337. ret = genlmsg_multicast_netns(&gtp_genl_family, dev_net(pctx->dev), msg,
  1338. 0, GTP_GENL_MCGRP, GFP_ATOMIC);
  1339. return ret;
  1340. }
  1341. static int gtp_genl_get_pdp(struct sk_buff *skb, struct genl_info *info)
  1342. {
  1343. struct pdp_ctx *pctx = NULL;
  1344. struct sk_buff *skb2;
  1345. int err;
  1346. if (!info->attrs[GTPA_VERSION])
  1347. return -EINVAL;
  1348. rcu_read_lock();
  1349. pctx = gtp_find_pdp(sock_net(skb->sk), info->attrs);
  1350. if (IS_ERR(pctx)) {
  1351. err = PTR_ERR(pctx);
  1352. goto err_unlock;
  1353. }
  1354. skb2 = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
  1355. if (skb2 == NULL) {
  1356. err = -ENOMEM;
  1357. goto err_unlock;
  1358. }
  1359. err = gtp_genl_fill_info(skb2, NETLINK_CB(skb).portid, info->snd_seq,
  1360. 0, info->nlhdr->nlmsg_type, pctx);
  1361. if (err < 0)
  1362. goto err_unlock_free;
  1363. rcu_read_unlock();
  1364. return genlmsg_unicast(genl_info_net(info), skb2, info->snd_portid);
  1365. err_unlock_free:
  1366. kfree_skb(skb2);
  1367. err_unlock:
  1368. rcu_read_unlock();
  1369. return err;
  1370. }
  1371. static int gtp_genl_dump_pdp(struct sk_buff *skb,
  1372. struct netlink_callback *cb)
  1373. {
  1374. struct gtp_dev *last_gtp = (struct gtp_dev *)cb->args[2], *gtp;
  1375. int i, j, bucket = cb->args[0], skip = cb->args[1];
  1376. struct net *net = sock_net(skb->sk);
  1377. struct pdp_ctx *pctx;
  1378. struct gtp_net *gn;
  1379. gn = net_generic(net, gtp_net_id);
  1380. if (cb->args[4])
  1381. return 0;
  1382. rcu_read_lock();
  1383. list_for_each_entry_rcu(gtp, &gn->gtp_dev_list, list) {
  1384. if (last_gtp && last_gtp != gtp)
  1385. continue;
  1386. else
  1387. last_gtp = NULL;
  1388. for (i = bucket; i < gtp->hash_size; i++) {
  1389. j = 0;
  1390. hlist_for_each_entry_rcu(pctx, &gtp->tid_hash[i],
  1391. hlist_tid) {
  1392. if (j >= skip &&
  1393. gtp_genl_fill_info(skb,
  1394. NETLINK_CB(cb->skb).portid,
  1395. cb->nlh->nlmsg_seq,
  1396. NLM_F_MULTI,
  1397. cb->nlh->nlmsg_type, pctx)) {
  1398. cb->args[0] = i;
  1399. cb->args[1] = j;
  1400. cb->args[2] = (unsigned long)gtp;
  1401. goto out;
  1402. }
  1403. j++;
  1404. }
  1405. skip = 0;
  1406. }
  1407. bucket = 0;
  1408. }
  1409. cb->args[4] = 1;
  1410. out:
  1411. rcu_read_unlock();
  1412. return skb->len;
  1413. }
  1414. static int gtp_genl_send_echo_req(struct sk_buff *skb, struct genl_info *info)
  1415. {
  1416. struct sk_buff *skb_to_send;
  1417. __be32 src_ip, dst_ip;
  1418. unsigned int version;
  1419. struct gtp_dev *gtp;
  1420. struct flowi4 fl4;
  1421. struct rtable *rt;
  1422. struct sock *sk;
  1423. __be16 port;
  1424. int len;
  1425. if (!info->attrs[GTPA_VERSION] ||
  1426. !info->attrs[GTPA_LINK] ||
  1427. !info->attrs[GTPA_PEER_ADDRESS] ||
  1428. !info->attrs[GTPA_MS_ADDRESS])
  1429. return -EINVAL;
  1430. version = nla_get_u32(info->attrs[GTPA_VERSION]);
  1431. dst_ip = nla_get_be32(info->attrs[GTPA_PEER_ADDRESS]);
  1432. src_ip = nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
  1433. gtp = gtp_find_dev(sock_net(skb->sk), info->attrs);
  1434. if (!gtp)
  1435. return -ENODEV;
  1436. if (!gtp->sk_created)
  1437. return -EOPNOTSUPP;
  1438. if (!(gtp->dev->flags & IFF_UP))
  1439. return -ENETDOWN;
  1440. if (version == GTP_V0) {
  1441. struct gtp0_header *gtp0_h;
  1442. len = LL_RESERVED_SPACE(gtp->dev) + sizeof(struct gtp0_header) +
  1443. sizeof(struct iphdr) + sizeof(struct udphdr);
  1444. skb_to_send = netdev_alloc_skb_ip_align(gtp->dev, len);
  1445. if (!skb_to_send)
  1446. return -ENOMEM;
  1447. sk = gtp->sk0;
  1448. port = htons(GTP0_PORT);
  1449. gtp0_h = skb_push(skb_to_send, sizeof(struct gtp0_header));
  1450. memset(gtp0_h, 0, sizeof(struct gtp0_header));
  1451. gtp0_build_echo_msg(gtp0_h, GTP_ECHO_REQ);
  1452. } else if (version == GTP_V1) {
  1453. struct gtp1_header_long *gtp1u_h;
  1454. len = LL_RESERVED_SPACE(gtp->dev) +
  1455. sizeof(struct gtp1_header_long) +
  1456. sizeof(struct iphdr) + sizeof(struct udphdr);
  1457. skb_to_send = netdev_alloc_skb_ip_align(gtp->dev, len);
  1458. if (!skb_to_send)
  1459. return -ENOMEM;
  1460. sk = gtp->sk1u;
  1461. port = htons(GTP1U_PORT);
  1462. gtp1u_h = skb_push(skb_to_send,
  1463. sizeof(struct gtp1_header_long));
  1464. memset(gtp1u_h, 0, sizeof(struct gtp1_header_long));
  1465. gtp1u_build_echo_msg(gtp1u_h, GTP_ECHO_REQ);
  1466. } else {
  1467. return -ENODEV;
  1468. }
  1469. rt = ip4_route_output_gtp(&fl4, sk, dst_ip, src_ip);
  1470. if (IS_ERR(rt)) {
  1471. netdev_dbg(gtp->dev, "no route for echo request to %pI4\n",
  1472. &dst_ip);
  1473. kfree_skb(skb_to_send);
  1474. return -ENODEV;
  1475. }
  1476. udp_tunnel_xmit_skb(rt, sk, skb_to_send,
  1477. fl4.saddr, fl4.daddr,
  1478. fl4.flowi4_tos,
  1479. ip4_dst_hoplimit(&rt->dst),
  1480. 0,
  1481. port, port,
  1482. !net_eq(sock_net(sk),
  1483. dev_net(gtp->dev)),
  1484. false);
  1485. return 0;
  1486. }
  1487. static const struct nla_policy gtp_genl_policy[GTPA_MAX + 1] = {
  1488. [GTPA_LINK] = { .type = NLA_U32, },
  1489. [GTPA_VERSION] = { .type = NLA_U32, },
  1490. [GTPA_TID] = { .type = NLA_U64, },
  1491. [GTPA_PEER_ADDRESS] = { .type = NLA_U32, },
  1492. [GTPA_MS_ADDRESS] = { .type = NLA_U32, },
  1493. [GTPA_FLOW] = { .type = NLA_U16, },
  1494. [GTPA_NET_NS_FD] = { .type = NLA_U32, },
  1495. [GTPA_I_TEI] = { .type = NLA_U32, },
  1496. [GTPA_O_TEI] = { .type = NLA_U32, },
  1497. };
  1498. static const struct genl_small_ops gtp_genl_ops[] = {
  1499. {
  1500. .cmd = GTP_CMD_NEWPDP,
  1501. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  1502. .doit = gtp_genl_new_pdp,
  1503. .flags = GENL_ADMIN_PERM,
  1504. },
  1505. {
  1506. .cmd = GTP_CMD_DELPDP,
  1507. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  1508. .doit = gtp_genl_del_pdp,
  1509. .flags = GENL_ADMIN_PERM,
  1510. },
  1511. {
  1512. .cmd = GTP_CMD_GETPDP,
  1513. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  1514. .doit = gtp_genl_get_pdp,
  1515. .dumpit = gtp_genl_dump_pdp,
  1516. .flags = GENL_ADMIN_PERM,
  1517. },
  1518. {
  1519. .cmd = GTP_CMD_ECHOREQ,
  1520. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  1521. .doit = gtp_genl_send_echo_req,
  1522. .flags = GENL_ADMIN_PERM,
  1523. },
  1524. };
  1525. static struct genl_family gtp_genl_family __ro_after_init = {
  1526. .name = "gtp",
  1527. .version = 0,
  1528. .hdrsize = 0,
  1529. .maxattr = GTPA_MAX,
  1530. .policy = gtp_genl_policy,
  1531. .netnsok = true,
  1532. .module = THIS_MODULE,
  1533. .small_ops = gtp_genl_ops,
  1534. .n_small_ops = ARRAY_SIZE(gtp_genl_ops),
  1535. .resv_start_op = GTP_CMD_ECHOREQ + 1,
  1536. .mcgrps = gtp_genl_mcgrps,
  1537. .n_mcgrps = ARRAY_SIZE(gtp_genl_mcgrps),
  1538. };
  1539. static int __net_init gtp_net_init(struct net *net)
  1540. {
  1541. struct gtp_net *gn = net_generic(net, gtp_net_id);
  1542. INIT_LIST_HEAD(&gn->gtp_dev_list);
  1543. return 0;
  1544. }
  1545. static void __net_exit gtp_net_exit(struct net *net)
  1546. {
  1547. struct gtp_net *gn = net_generic(net, gtp_net_id);
  1548. struct gtp_dev *gtp;
  1549. LIST_HEAD(list);
  1550. rtnl_lock();
  1551. list_for_each_entry(gtp, &gn->gtp_dev_list, list)
  1552. gtp_dellink(gtp->dev, &list);
  1553. unregister_netdevice_many(&list);
  1554. rtnl_unlock();
  1555. }
  1556. static struct pernet_operations gtp_net_ops = {
  1557. .init = gtp_net_init,
  1558. .exit = gtp_net_exit,
  1559. .id = &gtp_net_id,
  1560. .size = sizeof(struct gtp_net),
  1561. };
  1562. static int __init gtp_init(void)
  1563. {
  1564. int err;
  1565. get_random_bytes(&gtp_h_initval, sizeof(gtp_h_initval));
  1566. err = rtnl_link_register(&gtp_link_ops);
  1567. if (err < 0)
  1568. goto error_out;
  1569. err = genl_register_family(&gtp_genl_family);
  1570. if (err < 0)
  1571. goto unreg_rtnl_link;
  1572. err = register_pernet_subsys(&gtp_net_ops);
  1573. if (err < 0)
  1574. goto unreg_genl_family;
  1575. pr_info("GTP module loaded (pdp ctx size %zd bytes)\n",
  1576. sizeof(struct pdp_ctx));
  1577. return 0;
  1578. unreg_genl_family:
  1579. genl_unregister_family(&gtp_genl_family);
  1580. unreg_rtnl_link:
  1581. rtnl_link_unregister(&gtp_link_ops);
  1582. error_out:
  1583. pr_err("error loading GTP module loaded\n");
  1584. return err;
  1585. }
  1586. late_initcall(gtp_init);
  1587. static void __exit gtp_fini(void)
  1588. {
  1589. genl_unregister_family(&gtp_genl_family);
  1590. rtnl_link_unregister(&gtp_link_ops);
  1591. unregister_pernet_subsys(&gtp_net_ops);
  1592. pr_info("GTP module unloaded\n");
  1593. }
  1594. module_exit(gtp_fini);
  1595. MODULE_LICENSE("GPL");
  1596. MODULE_AUTHOR("Harald Welte <[email protected]>");
  1597. MODULE_DESCRIPTION("Interface driver for GTP encapsulated traffic");
  1598. MODULE_ALIAS_RTNL_LINK("gtp");
  1599. MODULE_ALIAS_GENL_FAMILY("gtp");