udp.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * UDP over IPv6
  4. * Linux INET6 implementation
  5. *
  6. * Authors:
  7. * Pedro Roque <roque@di.fc.ul.pt>
  8. *
  9. * Based on linux/ipv4/udp.c
  10. *
  11. * Fixes:
  12. * Hideaki YOSHIFUJI : sin6_scope_id support
  13. * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
  14. * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind
  15. * a single port at the same time.
  16. * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data
  17. * YOSHIFUJI Hideaki @USAGI: convert /proc/net/udp6 to seq_file.
  18. */
  19. #include <linux/errno.h>
  20. #include <linux/types.h>
  21. #include <linux/socket.h>
  22. #include <linux/sockios.h>
  23. #include <linux/net.h>
  24. #include <linux/in6.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/if_arp.h>
  27. #include <linux/ipv6.h>
  28. #include <linux/icmpv6.h>
  29. #include <linux/init.h>
  30. #include <linux/module.h>
  31. #include <linux/skbuff.h>
  32. #include <linux/slab.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/indirect_call_wrapper.h>
  35. #include <net/addrconf.h>
  36. #include <net/ndisc.h>
  37. #include <net/protocol.h>
  38. #include <net/transp_v6.h>
  39. #include <net/ip6_route.h>
  40. #include <net/raw.h>
  41. #include <net/tcp_states.h>
  42. #include <net/ip6_checksum.h>
  43. #include <net/ip6_tunnel.h>
  44. #include <net/xfrm.h>
  45. #include <net/inet_hashtables.h>
  46. #include <net/inet6_hashtables.h>
  47. #include <net/busy_poll.h>
  48. #include <net/sock_reuseport.h>
  49. #include <linux/proc_fs.h>
  50. #include <linux/seq_file.h>
  51. #include <trace/events/skb.h>
  52. #include "udp_impl.h"
  53. static u32 udp6_ehashfn(const struct net *net,
  54. const struct in6_addr *laddr,
  55. const u16 lport,
  56. const struct in6_addr *faddr,
  57. const __be16 fport)
  58. {
  59. static u32 udp6_ehash_secret __read_mostly;
  60. static u32 udp_ipv6_hash_secret __read_mostly;
  61. u32 lhash, fhash;
  62. net_get_random_once(&udp6_ehash_secret,
  63. sizeof(udp6_ehash_secret));
  64. net_get_random_once(&udp_ipv6_hash_secret,
  65. sizeof(udp_ipv6_hash_secret));
  66. lhash = (__force u32)laddr->s6_addr32[3];
  67. fhash = __ipv6_addr_jhash(faddr, udp_ipv6_hash_secret);
  68. return __inet6_ehashfn(lhash, lport, fhash, fport,
  69. udp_ipv6_hash_secret + net_hash_mix(net));
  70. }
  71. int udp_v6_get_port(struct sock *sk, unsigned short snum)
  72. {
  73. unsigned int hash2_nulladdr =
  74. ipv6_portaddr_hash(sock_net(sk), &in6addr_any, snum);
  75. unsigned int hash2_partial =
  76. ipv6_portaddr_hash(sock_net(sk), &sk->sk_v6_rcv_saddr, 0);
  77. /* precompute partial secondary hash */
  78. udp_sk(sk)->udp_portaddr_hash = hash2_partial;
  79. return udp_lib_get_port(sk, snum, hash2_nulladdr);
  80. }
  81. void udp_v6_rehash(struct sock *sk)
  82. {
  83. u16 new_hash = ipv6_portaddr_hash(sock_net(sk),
  84. &sk->sk_v6_rcv_saddr,
  85. inet_sk(sk)->inet_num);
  86. udp_lib_rehash(sk, new_hash);
  87. }
  88. static int compute_score(struct sock *sk, struct net *net,
  89. const struct in6_addr *saddr, __be16 sport,
  90. const struct in6_addr *daddr, unsigned short hnum,
  91. int dif, int sdif)
  92. {
  93. int score;
  94. struct inet_sock *inet;
  95. bool dev_match;
  96. if (!net_eq(sock_net(sk), net) ||
  97. udp_sk(sk)->udp_port_hash != hnum ||
  98. sk->sk_family != PF_INET6)
  99. return -1;
  100. if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
  101. return -1;
  102. score = 0;
  103. inet = inet_sk(sk);
  104. if (inet->inet_dport) {
  105. if (inet->inet_dport != sport)
  106. return -1;
  107. score++;
  108. }
  109. if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
  110. if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
  111. return -1;
  112. score++;
  113. }
  114. dev_match = udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif);
  115. if (!dev_match)
  116. return -1;
  117. if (sk->sk_bound_dev_if)
  118. score++;
  119. if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
  120. score++;
  121. return score;
  122. }
  123. static struct sock *lookup_reuseport(struct net *net, struct sock *sk,
  124. struct sk_buff *skb,
  125. const struct in6_addr *saddr,
  126. __be16 sport,
  127. const struct in6_addr *daddr,
  128. unsigned int hnum)
  129. {
  130. struct sock *reuse_sk = NULL;
  131. u32 hash;
  132. if (sk->sk_reuseport && sk->sk_state != TCP_ESTABLISHED) {
  133. hash = udp6_ehashfn(net, daddr, hnum, saddr, sport);
  134. reuse_sk = reuseport_select_sock(sk, hash, skb,
  135. sizeof(struct udphdr));
  136. }
  137. return reuse_sk;
  138. }
  139. /* called with rcu_read_lock() */
  140. static struct sock *udp6_lib_lookup2(struct net *net,
  141. const struct in6_addr *saddr, __be16 sport,
  142. const struct in6_addr *daddr, unsigned int hnum,
  143. int dif, int sdif, struct udp_hslot *hslot2,
  144. struct sk_buff *skb)
  145. {
  146. struct sock *sk, *result;
  147. int score, badness;
  148. result = NULL;
  149. badness = -1;
  150. udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
  151. score = compute_score(sk, net, saddr, sport,
  152. daddr, hnum, dif, sdif);
  153. if (score > badness) {
  154. result = lookup_reuseport(net, sk, skb,
  155. saddr, sport, daddr, hnum);
  156. /* Fall back to scoring if group has connections */
  157. if (result && !reuseport_has_conns(sk, false))
  158. return result;
  159. result = result ? : sk;
  160. badness = score;
  161. }
  162. }
  163. return result;
  164. }
  165. static inline struct sock *udp6_lookup_run_bpf(struct net *net,
  166. struct udp_table *udptable,
  167. struct sk_buff *skb,
  168. const struct in6_addr *saddr,
  169. __be16 sport,
  170. const struct in6_addr *daddr,
  171. u16 hnum)
  172. {
  173. struct sock *sk, *reuse_sk;
  174. bool no_reuseport;
  175. if (udptable != &udp_table)
  176. return NULL; /* only UDP is supported */
  177. no_reuseport = bpf_sk_lookup_run_v6(net, IPPROTO_UDP,
  178. saddr, sport, daddr, hnum, &sk);
  179. if (no_reuseport || IS_ERR_OR_NULL(sk))
  180. return sk;
  181. reuse_sk = lookup_reuseport(net, sk, skb, saddr, sport, daddr, hnum);
  182. if (reuse_sk)
  183. sk = reuse_sk;
  184. return sk;
  185. }
  186. /* rcu_read_lock() must be held */
  187. struct sock *__udp6_lib_lookup(struct net *net,
  188. const struct in6_addr *saddr, __be16 sport,
  189. const struct in6_addr *daddr, __be16 dport,
  190. int dif, int sdif, struct udp_table *udptable,
  191. struct sk_buff *skb)
  192. {
  193. unsigned short hnum = ntohs(dport);
  194. unsigned int hash2, slot2;
  195. struct udp_hslot *hslot2;
  196. struct sock *result, *sk;
  197. hash2 = ipv6_portaddr_hash(net, daddr, hnum);
  198. slot2 = hash2 & udptable->mask;
  199. hslot2 = &udptable->hash2[slot2];
  200. /* Lookup connected or non-wildcard sockets */
  201. result = udp6_lib_lookup2(net, saddr, sport,
  202. daddr, hnum, dif, sdif,
  203. hslot2, skb);
  204. if (!IS_ERR_OR_NULL(result) && result->sk_state == TCP_ESTABLISHED)
  205. goto done;
  206. /* Lookup redirect from BPF */
  207. if (static_branch_unlikely(&bpf_sk_lookup_enabled)) {
  208. sk = udp6_lookup_run_bpf(net, udptable, skb,
  209. saddr, sport, daddr, hnum);
  210. if (sk) {
  211. result = sk;
  212. goto done;
  213. }
  214. }
  215. /* Got non-wildcard socket or error on first lookup */
  216. if (result)
  217. goto done;
  218. /* Lookup wildcard sockets */
  219. hash2 = ipv6_portaddr_hash(net, &in6addr_any, hnum);
  220. slot2 = hash2 & udptable->mask;
  221. hslot2 = &udptable->hash2[slot2];
  222. result = udp6_lib_lookup2(net, saddr, sport,
  223. &in6addr_any, hnum, dif, sdif,
  224. hslot2, skb);
  225. done:
  226. if (IS_ERR(result))
  227. return NULL;
  228. return result;
  229. }
  230. EXPORT_SYMBOL_GPL(__udp6_lib_lookup);
  231. static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
  232. __be16 sport, __be16 dport,
  233. struct udp_table *udptable)
  234. {
  235. const struct ipv6hdr *iph = ipv6_hdr(skb);
  236. return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
  237. &iph->daddr, dport, inet6_iif(skb),
  238. inet6_sdif(skb), udptable, skb);
  239. }
  240. struct sock *udp6_lib_lookup_skb(struct sk_buff *skb,
  241. __be16 sport, __be16 dport)
  242. {
  243. const struct ipv6hdr *iph = ipv6_hdr(skb);
  244. return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
  245. &iph->daddr, dport, inet6_iif(skb),
  246. inet6_sdif(skb), &udp_table, NULL);
  247. }
  248. EXPORT_SYMBOL_GPL(udp6_lib_lookup_skb);
  249. /* Must be called under rcu_read_lock().
  250. * Does increment socket refcount.
  251. */
  252. #if IS_ENABLED(CONFIG_NF_TPROXY_IPV6) || IS_ENABLED(CONFIG_NF_SOCKET_IPV6)
  253. struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport,
  254. const struct in6_addr *daddr, __be16 dport, int dif)
  255. {
  256. struct sock *sk;
  257. sk = __udp6_lib_lookup(net, saddr, sport, daddr, dport,
  258. dif, 0, &udp_table, NULL);
  259. if (sk && !refcount_inc_not_zero(&sk->sk_refcnt))
  260. sk = NULL;
  261. return sk;
  262. }
  263. EXPORT_SYMBOL_GPL(udp6_lib_lookup);
  264. #endif
  265. /* do not use the scratch area len for jumbogram: their length execeeds the
  266. * scratch area space; note that the IP6CB flags is still in the first
  267. * cacheline, so checking for jumbograms is cheap
  268. */
  269. static int udp6_skb_len(struct sk_buff *skb)
  270. {
  271. return unlikely(inet6_is_jumbogram(skb)) ? skb->len : udp_skb_len(skb);
  272. }
  273. /*
  274. * This should be easy, if there is something there we
  275. * return it, otherwise we block.
  276. */
  277. int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
  278. int noblock, int flags, int *addr_len)
  279. {
  280. struct ipv6_pinfo *np = inet6_sk(sk);
  281. struct inet_sock *inet = inet_sk(sk);
  282. struct sk_buff *skb;
  283. unsigned int ulen, copied;
  284. int off, err, peeking = flags & MSG_PEEK;
  285. int is_udplite = IS_UDPLITE(sk);
  286. struct udp_mib __percpu *mib;
  287. bool checksum_valid = false;
  288. int is_udp4;
  289. if (flags & MSG_ERRQUEUE)
  290. return ipv6_recv_error(sk, msg, len, addr_len);
  291. if (np->rxpmtu && np->rxopt.bits.rxpmtu)
  292. return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
  293. try_again:
  294. off = sk_peek_offset(sk, flags);
  295. skb = __skb_recv_udp(sk, flags, noblock, &off, &err);
  296. if (!skb)
  297. return err;
  298. ulen = udp6_skb_len(skb);
  299. copied = len;
  300. if (copied > ulen - off)
  301. copied = ulen - off;
  302. else if (copied < ulen)
  303. msg->msg_flags |= MSG_TRUNC;
  304. is_udp4 = (skb->protocol == htons(ETH_P_IP));
  305. mib = __UDPX_MIB(sk, is_udp4);
  306. /*
  307. * If checksum is needed at all, try to do it while copying the
  308. * data. If the data is truncated, or if we only want a partial
  309. * coverage checksum (UDP-Lite), do it before the copy.
  310. */
  311. if (copied < ulen || peeking ||
  312. (is_udplite && UDP_SKB_CB(skb)->partial_cov)) {
  313. checksum_valid = udp_skb_csum_unnecessary(skb) ||
  314. !__udp_lib_checksum_complete(skb);
  315. if (!checksum_valid)
  316. goto csum_copy_err;
  317. }
  318. if (checksum_valid || udp_skb_csum_unnecessary(skb)) {
  319. if (udp_skb_is_linear(skb))
  320. err = copy_linear_skb(skb, copied, off, &msg->msg_iter);
  321. else
  322. err = skb_copy_datagram_msg(skb, off, msg, copied);
  323. } else {
  324. err = skb_copy_and_csum_datagram_msg(skb, off, msg);
  325. if (err == -EINVAL)
  326. goto csum_copy_err;
  327. }
  328. if (unlikely(err)) {
  329. if (!peeking) {
  330. atomic_inc(&sk->sk_drops);
  331. SNMP_INC_STATS(mib, UDP_MIB_INERRORS);
  332. }
  333. kfree_skb(skb);
  334. return err;
  335. }
  336. if (!peeking)
  337. SNMP_INC_STATS(mib, UDP_MIB_INDATAGRAMS);
  338. sock_recv_ts_and_drops(msg, sk, skb);
  339. /* Copy the address. */
  340. if (msg->msg_name) {
  341. DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
  342. sin6->sin6_family = AF_INET6;
  343. sin6->sin6_port = udp_hdr(skb)->source;
  344. sin6->sin6_flowinfo = 0;
  345. if (is_udp4) {
  346. ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
  347. &sin6->sin6_addr);
  348. sin6->sin6_scope_id = 0;
  349. } else {
  350. sin6->sin6_addr = ipv6_hdr(skb)->saddr;
  351. sin6->sin6_scope_id =
  352. ipv6_iface_scope_id(&sin6->sin6_addr,
  353. inet6_iif(skb));
  354. }
  355. *addr_len = sizeof(*sin6);
  356. if (cgroup_bpf_enabled)
  357. BPF_CGROUP_RUN_PROG_UDP6_RECVMSG_LOCK(sk,
  358. (struct sockaddr *)sin6);
  359. }
  360. if (udp_sk(sk)->gro_enabled)
  361. udp_cmsg_recv(msg, sk, skb);
  362. if (np->rxopt.all)
  363. ip6_datagram_recv_common_ctl(sk, msg, skb);
  364. if (is_udp4) {
  365. if (inet->cmsg_flags)
  366. ip_cmsg_recv_offset(msg, sk, skb,
  367. sizeof(struct udphdr), off);
  368. } else {
  369. if (np->rxopt.all)
  370. ip6_datagram_recv_specific_ctl(sk, msg, skb);
  371. }
  372. err = copied;
  373. if (flags & MSG_TRUNC)
  374. err = ulen;
  375. skb_consume_udp(sk, skb, peeking ? -err : err);
  376. return err;
  377. csum_copy_err:
  378. if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags,
  379. udp_skb_destructor)) {
  380. SNMP_INC_STATS(mib, UDP_MIB_CSUMERRORS);
  381. SNMP_INC_STATS(mib, UDP_MIB_INERRORS);
  382. }
  383. kfree_skb(skb);
  384. /* starting over for a new packet, but check if we need to yield */
  385. cond_resched();
  386. msg->msg_flags &= ~MSG_TRUNC;
  387. goto try_again;
  388. }
  389. DEFINE_STATIC_KEY_FALSE(udpv6_encap_needed_key);
  390. void udpv6_encap_enable(void)
  391. {
  392. static_branch_inc(&udpv6_encap_needed_key);
  393. }
  394. EXPORT_SYMBOL(udpv6_encap_enable);
  395. /* Handler for tunnels with arbitrary destination ports: no socket lookup, go
  396. * through error handlers in encapsulations looking for a match.
  397. */
  398. static int __udp6_lib_err_encap_no_sk(struct sk_buff *skb,
  399. struct inet6_skb_parm *opt,
  400. u8 type, u8 code, int offset, __be32 info)
  401. {
  402. int i;
  403. for (i = 0; i < MAX_IPTUN_ENCAP_OPS; i++) {
  404. int (*handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
  405. u8 type, u8 code, int offset, __be32 info);
  406. const struct ip6_tnl_encap_ops *encap;
  407. encap = rcu_dereference(ip6tun_encaps[i]);
  408. if (!encap)
  409. continue;
  410. handler = encap->err_handler;
  411. if (handler && !handler(skb, opt, type, code, offset, info))
  412. return 0;
  413. }
  414. return -ENOENT;
  415. }
  416. /* Try to match ICMP errors to UDP tunnels by looking up a socket without
  417. * reversing source and destination port: this will match tunnels that force the
  418. * same destination port on both endpoints (e.g. VXLAN, GENEVE). Note that
  419. * lwtunnels might actually break this assumption by being configured with
  420. * different destination ports on endpoints, in this case we won't be able to
  421. * trace ICMP messages back to them.
  422. *
  423. * If this doesn't match any socket, probe tunnels with arbitrary destination
  424. * ports (e.g. FoU, GUE): there, the receiving socket is useless, as the port
  425. * we've sent packets to won't necessarily match the local destination port.
  426. *
  427. * Then ask the tunnel implementation to match the error against a valid
  428. * association.
  429. *
  430. * Return an error if we can't find a match, the socket if we need further
  431. * processing, zero otherwise.
  432. */
  433. static struct sock *__udp6_lib_err_encap(struct net *net,
  434. const struct ipv6hdr *hdr, int offset,
  435. struct udphdr *uh,
  436. struct udp_table *udptable,
  437. struct sk_buff *skb,
  438. struct inet6_skb_parm *opt,
  439. u8 type, u8 code, __be32 info)
  440. {
  441. int network_offset, transport_offset;
  442. struct sock *sk;
  443. network_offset = skb_network_offset(skb);
  444. transport_offset = skb_transport_offset(skb);
  445. /* Network header needs to point to the outer IPv6 header inside ICMP */
  446. skb_reset_network_header(skb);
  447. /* Transport header needs to point to the UDP header */
  448. skb_set_transport_header(skb, offset);
  449. sk = __udp6_lib_lookup(net, &hdr->daddr, uh->source,
  450. &hdr->saddr, uh->dest,
  451. inet6_iif(skb), 0, udptable, skb);
  452. if (sk) {
  453. int (*lookup)(struct sock *sk, struct sk_buff *skb);
  454. struct udp_sock *up = udp_sk(sk);
  455. lookup = READ_ONCE(up->encap_err_lookup);
  456. if (!lookup || lookup(sk, skb))
  457. sk = NULL;
  458. }
  459. if (!sk) {
  460. sk = ERR_PTR(__udp6_lib_err_encap_no_sk(skb, opt, type, code,
  461. offset, info));
  462. }
  463. skb_set_transport_header(skb, transport_offset);
  464. skb_set_network_header(skb, network_offset);
  465. return sk;
  466. }
  467. int __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  468. u8 type, u8 code, int offset, __be32 info,
  469. struct udp_table *udptable)
  470. {
  471. struct ipv6_pinfo *np;
  472. const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
  473. const struct in6_addr *saddr = &hdr->saddr;
  474. const struct in6_addr *daddr = &hdr->daddr;
  475. struct udphdr *uh = (struct udphdr *)(skb->data+offset);
  476. bool tunnel = false;
  477. struct sock *sk;
  478. int harderr;
  479. int err;
  480. struct net *net = dev_net(skb->dev);
  481. sk = __udp6_lib_lookup(net, daddr, uh->dest, saddr, uh->source,
  482. inet6_iif(skb), inet6_sdif(skb), udptable, NULL);
  483. if (!sk) {
  484. /* No socket for error: try tunnels before discarding */
  485. sk = ERR_PTR(-ENOENT);
  486. if (static_branch_unlikely(&udpv6_encap_needed_key)) {
  487. sk = __udp6_lib_err_encap(net, hdr, offset, uh,
  488. udptable, skb,
  489. opt, type, code, info);
  490. if (!sk)
  491. return 0;
  492. }
  493. if (IS_ERR(sk)) {
  494. __ICMP6_INC_STATS(net, __in6_dev_get(skb->dev),
  495. ICMP6_MIB_INERRORS);
  496. return PTR_ERR(sk);
  497. }
  498. tunnel = true;
  499. }
  500. harderr = icmpv6_err_convert(type, code, &err);
  501. np = inet6_sk(sk);
  502. if (type == ICMPV6_PKT_TOOBIG) {
  503. if (!ip6_sk_accept_pmtu(sk))
  504. goto out;
  505. ip6_sk_update_pmtu(skb, sk, info);
  506. if (np->pmtudisc != IPV6_PMTUDISC_DONT)
  507. harderr = 1;
  508. }
  509. if (type == NDISC_REDIRECT) {
  510. if (tunnel) {
  511. ip6_redirect(skb, sock_net(sk), inet6_iif(skb),
  512. sk->sk_mark, sk->sk_uid);
  513. } else {
  514. ip6_sk_redirect(skb, sk);
  515. }
  516. goto out;
  517. }
  518. /* Tunnels don't have an application socket: don't pass errors back */
  519. if (tunnel)
  520. goto out;
  521. if (!np->recverr) {
  522. if (!harderr || sk->sk_state != TCP_ESTABLISHED)
  523. goto out;
  524. } else {
  525. ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
  526. }
  527. sk->sk_err = err;
  528. sk->sk_error_report(sk);
  529. out:
  530. return 0;
  531. }
  532. static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  533. {
  534. int rc;
  535. if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
  536. sock_rps_save_rxhash(sk, skb);
  537. sk_mark_napi_id(sk, skb);
  538. sk_incoming_cpu_update(sk);
  539. } else {
  540. sk_mark_napi_id_once(sk, skb);
  541. }
  542. rc = __udp_enqueue_schedule_skb(sk, skb);
  543. if (rc < 0) {
  544. int is_udplite = IS_UDPLITE(sk);
  545. /* Note that an ENOMEM error is charged twice */
  546. if (rc == -ENOMEM)
  547. UDP6_INC_STATS(sock_net(sk),
  548. UDP_MIB_RCVBUFERRORS, is_udplite);
  549. UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  550. kfree_skb(skb);
  551. return -1;
  552. }
  553. return 0;
  554. }
  555. static __inline__ int udpv6_err(struct sk_buff *skb,
  556. struct inet6_skb_parm *opt, u8 type,
  557. u8 code, int offset, __be32 info)
  558. {
  559. return __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
  560. }
  561. static int udpv6_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb)
  562. {
  563. struct udp_sock *up = udp_sk(sk);
  564. int is_udplite = IS_UDPLITE(sk);
  565. if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
  566. goto drop;
  567. if (static_branch_unlikely(&udpv6_encap_needed_key) && up->encap_type) {
  568. int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
  569. /*
  570. * This is an encapsulation socket so pass the skb to
  571. * the socket's udp_encap_rcv() hook. Otherwise, just
  572. * fall through and pass this up the UDP socket.
  573. * up->encap_rcv() returns the following value:
  574. * =0 if skb was successfully passed to the encap
  575. * handler or was discarded by it.
  576. * >0 if skb should be passed on to UDP.
  577. * <0 if skb should be resubmitted as proto -N
  578. */
  579. /* if we're overly short, let UDP handle it */
  580. encap_rcv = READ_ONCE(up->encap_rcv);
  581. if (encap_rcv) {
  582. int ret;
  583. /* Verify checksum before giving to encap */
  584. if (udp_lib_checksum_complete(skb))
  585. goto csum_error;
  586. ret = encap_rcv(sk, skb);
  587. if (ret <= 0) {
  588. __UDP_INC_STATS(sock_net(sk),
  589. UDP_MIB_INDATAGRAMS,
  590. is_udplite);
  591. return -ret;
  592. }
  593. }
  594. /* FALLTHROUGH -- it's a UDP Packet */
  595. }
  596. /*
  597. * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
  598. */
  599. if ((up->pcflag & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
  600. if (up->pcrlen == 0) { /* full coverage was set */
  601. net_dbg_ratelimited("UDPLITE6: partial coverage %d while full coverage %d requested\n",
  602. UDP_SKB_CB(skb)->cscov, skb->len);
  603. goto drop;
  604. }
  605. if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
  606. net_dbg_ratelimited("UDPLITE6: coverage %d too small, need min %d\n",
  607. UDP_SKB_CB(skb)->cscov, up->pcrlen);
  608. goto drop;
  609. }
  610. }
  611. prefetch(&sk->sk_rmem_alloc);
  612. if (rcu_access_pointer(sk->sk_filter) &&
  613. udp_lib_checksum_complete(skb))
  614. goto csum_error;
  615. if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr)))
  616. goto drop;
  617. udp_csum_pull_header(skb);
  618. skb_dst_drop(skb);
  619. return __udpv6_queue_rcv_skb(sk, skb);
  620. csum_error:
  621. __UDP6_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
  622. drop:
  623. __UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  624. atomic_inc(&sk->sk_drops);
  625. kfree_skb(skb);
  626. return -1;
  627. }
  628. static int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  629. {
  630. struct sk_buff *next, *segs;
  631. int ret;
  632. if (likely(!udp_unexpected_gso(sk, skb)))
  633. return udpv6_queue_rcv_one_skb(sk, skb);
  634. __skb_push(skb, -skb_mac_offset(skb));
  635. segs = udp_rcv_segment(sk, skb, false);
  636. skb_list_walk_safe(segs, skb, next) {
  637. __skb_pull(skb, skb_transport_offset(skb));
  638. ret = udpv6_queue_rcv_one_skb(sk, skb);
  639. if (ret > 0)
  640. ip6_protocol_deliver_rcu(dev_net(skb->dev), skb, ret,
  641. true);
  642. }
  643. return 0;
  644. }
  645. static bool __udp_v6_is_mcast_sock(struct net *net, struct sock *sk,
  646. __be16 loc_port, const struct in6_addr *loc_addr,
  647. __be16 rmt_port, const struct in6_addr *rmt_addr,
  648. int dif, int sdif, unsigned short hnum)
  649. {
  650. struct inet_sock *inet = inet_sk(sk);
  651. if (!net_eq(sock_net(sk), net))
  652. return false;
  653. if (udp_sk(sk)->udp_port_hash != hnum ||
  654. sk->sk_family != PF_INET6 ||
  655. (inet->inet_dport && inet->inet_dport != rmt_port) ||
  656. (!ipv6_addr_any(&sk->sk_v6_daddr) &&
  657. !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) ||
  658. !udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif) ||
  659. (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) &&
  660. !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr)))
  661. return false;
  662. if (!inet6_mc_check(sk, loc_addr, rmt_addr))
  663. return false;
  664. return true;
  665. }
  666. static void udp6_csum_zero_error(struct sk_buff *skb)
  667. {
  668. /* RFC 2460 section 8.1 says that we SHOULD log
  669. * this error. Well, it is reasonable.
  670. */
  671. net_dbg_ratelimited("IPv6: udp checksum is 0 for [%pI6c]:%u->[%pI6c]:%u\n",
  672. &ipv6_hdr(skb)->saddr, ntohs(udp_hdr(skb)->source),
  673. &ipv6_hdr(skb)->daddr, ntohs(udp_hdr(skb)->dest));
  674. }
  675. /*
  676. * Note: called only from the BH handler context,
  677. * so we don't need to lock the hashes.
  678. */
  679. static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
  680. const struct in6_addr *saddr, const struct in6_addr *daddr,
  681. struct udp_table *udptable, int proto)
  682. {
  683. struct sock *sk, *first = NULL;
  684. const struct udphdr *uh = udp_hdr(skb);
  685. unsigned short hnum = ntohs(uh->dest);
  686. struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum);
  687. unsigned int offset = offsetof(typeof(*sk), sk_node);
  688. unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10);
  689. int dif = inet6_iif(skb);
  690. int sdif = inet6_sdif(skb);
  691. struct hlist_node *node;
  692. struct sk_buff *nskb;
  693. if (use_hash2) {
  694. hash2_any = ipv6_portaddr_hash(net, &in6addr_any, hnum) &
  695. udptable->mask;
  696. hash2 = ipv6_portaddr_hash(net, daddr, hnum) & udptable->mask;
  697. start_lookup:
  698. hslot = &udptable->hash2[hash2];
  699. offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
  700. }
  701. sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) {
  702. if (!__udp_v6_is_mcast_sock(net, sk, uh->dest, daddr,
  703. uh->source, saddr, dif, sdif,
  704. hnum))
  705. continue;
  706. /* If zero checksum and no_check is not on for
  707. * the socket then skip it.
  708. */
  709. if (!uh->check && !udp_sk(sk)->no_check6_rx)
  710. continue;
  711. if (!first) {
  712. first = sk;
  713. continue;
  714. }
  715. nskb = skb_clone(skb, GFP_ATOMIC);
  716. if (unlikely(!nskb)) {
  717. atomic_inc(&sk->sk_drops);
  718. __UDP6_INC_STATS(net, UDP_MIB_RCVBUFERRORS,
  719. IS_UDPLITE(sk));
  720. __UDP6_INC_STATS(net, UDP_MIB_INERRORS,
  721. IS_UDPLITE(sk));
  722. continue;
  723. }
  724. if (udpv6_queue_rcv_skb(sk, nskb) > 0)
  725. consume_skb(nskb);
  726. }
  727. /* Also lookup *:port if we are using hash2 and haven't done so yet. */
  728. if (use_hash2 && hash2 != hash2_any) {
  729. hash2 = hash2_any;
  730. goto start_lookup;
  731. }
  732. if (first) {
  733. if (udpv6_queue_rcv_skb(first, skb) > 0)
  734. consume_skb(skb);
  735. } else {
  736. kfree_skb(skb);
  737. __UDP6_INC_STATS(net, UDP_MIB_IGNOREDMULTI,
  738. proto == IPPROTO_UDPLITE);
  739. }
  740. return 0;
  741. }
  742. static void udp6_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst)
  743. {
  744. if (udp_sk_rx_dst_set(sk, dst)) {
  745. const struct rt6_info *rt = (const struct rt6_info *)dst;
  746. inet6_sk(sk)->rx_dst_cookie = rt6_get_cookie(rt);
  747. }
  748. }
  749. /* wrapper for udp_queue_rcv_skb tacking care of csum conversion and
  750. * return code conversion for ip layer consumption
  751. */
  752. static int udp6_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb,
  753. struct udphdr *uh)
  754. {
  755. int ret;
  756. if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
  757. skb_checksum_try_convert(skb, IPPROTO_UDP, ip6_compute_pseudo);
  758. ret = udpv6_queue_rcv_skb(sk, skb);
  759. /* a return value > 0 means to resubmit the input */
  760. if (ret > 0)
  761. return ret;
  762. return 0;
  763. }
  764. int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
  765. int proto)
  766. {
  767. const struct in6_addr *saddr, *daddr;
  768. struct net *net = dev_net(skb->dev);
  769. struct udphdr *uh;
  770. struct sock *sk;
  771. bool refcounted;
  772. u32 ulen = 0;
  773. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  774. goto discard;
  775. saddr = &ipv6_hdr(skb)->saddr;
  776. daddr = &ipv6_hdr(skb)->daddr;
  777. uh = udp_hdr(skb);
  778. ulen = ntohs(uh->len);
  779. if (ulen > skb->len)
  780. goto short_packet;
  781. if (proto == IPPROTO_UDP) {
  782. /* UDP validates ulen. */
  783. /* Check for jumbo payload */
  784. if (ulen == 0)
  785. ulen = skb->len;
  786. if (ulen < sizeof(*uh))
  787. goto short_packet;
  788. if (ulen < skb->len) {
  789. if (pskb_trim_rcsum(skb, ulen))
  790. goto short_packet;
  791. saddr = &ipv6_hdr(skb)->saddr;
  792. daddr = &ipv6_hdr(skb)->daddr;
  793. uh = udp_hdr(skb);
  794. }
  795. }
  796. if (udp6_csum_init(skb, uh, proto))
  797. goto csum_error;
  798. /* Check if the socket is already available, e.g. due to early demux */
  799. sk = skb_steal_sock(skb, &refcounted);
  800. if (sk) {
  801. struct dst_entry *dst = skb_dst(skb);
  802. int ret;
  803. if (unlikely(sk->sk_rx_dst != dst))
  804. udp6_sk_rx_dst_set(sk, dst);
  805. if (!uh->check && !udp_sk(sk)->no_check6_rx) {
  806. if (refcounted)
  807. sock_put(sk);
  808. goto report_csum_error;
  809. }
  810. ret = udp6_unicast_rcv_skb(sk, skb, uh);
  811. if (refcounted)
  812. sock_put(sk);
  813. return ret;
  814. }
  815. /*
  816. * Multicast receive code
  817. */
  818. if (ipv6_addr_is_multicast(daddr))
  819. return __udp6_lib_mcast_deliver(net, skb,
  820. saddr, daddr, udptable, proto);
  821. /* Unicast */
  822. sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
  823. if (sk) {
  824. if (!uh->check && !udp_sk(sk)->no_check6_rx)
  825. goto report_csum_error;
  826. return udp6_unicast_rcv_skb(sk, skb, uh);
  827. }
  828. if (!uh->check)
  829. goto report_csum_error;
  830. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  831. goto discard;
  832. if (udp_lib_checksum_complete(skb))
  833. goto csum_error;
  834. __UDP6_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
  835. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  836. kfree_skb(skb);
  837. return 0;
  838. short_packet:
  839. net_dbg_ratelimited("UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
  840. proto == IPPROTO_UDPLITE ? "-Lite" : "",
  841. saddr, ntohs(uh->source),
  842. ulen, skb->len,
  843. daddr, ntohs(uh->dest));
  844. goto discard;
  845. report_csum_error:
  846. udp6_csum_zero_error(skb);
  847. csum_error:
  848. __UDP6_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
  849. discard:
  850. __UDP6_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
  851. kfree_skb(skb);
  852. return 0;
  853. }
  854. static struct sock *__udp6_lib_demux_lookup(struct net *net,
  855. __be16 loc_port, const struct in6_addr *loc_addr,
  856. __be16 rmt_port, const struct in6_addr *rmt_addr,
  857. int dif, int sdif)
  858. {
  859. unsigned short hnum = ntohs(loc_port);
  860. unsigned int hash2 = ipv6_portaddr_hash(net, loc_addr, hnum);
  861. unsigned int slot2 = hash2 & udp_table.mask;
  862. struct udp_hslot *hslot2 = &udp_table.hash2[slot2];
  863. const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum);
  864. struct sock *sk;
  865. udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
  866. if (sk->sk_state == TCP_ESTABLISHED &&
  867. INET6_MATCH(sk, net, rmt_addr, loc_addr, ports, dif, sdif))
  868. return sk;
  869. /* Only check first socket in chain */
  870. break;
  871. }
  872. return NULL;
  873. }
  874. INDIRECT_CALLABLE_SCOPE void udp_v6_early_demux(struct sk_buff *skb)
  875. {
  876. struct net *net = dev_net(skb->dev);
  877. const struct udphdr *uh;
  878. struct sock *sk;
  879. struct dst_entry *dst;
  880. int dif = skb->dev->ifindex;
  881. int sdif = inet6_sdif(skb);
  882. if (!pskb_may_pull(skb, skb_transport_offset(skb) +
  883. sizeof(struct udphdr)))
  884. return;
  885. uh = udp_hdr(skb);
  886. if (skb->pkt_type == PACKET_HOST)
  887. sk = __udp6_lib_demux_lookup(net, uh->dest,
  888. &ipv6_hdr(skb)->daddr,
  889. uh->source, &ipv6_hdr(skb)->saddr,
  890. dif, sdif);
  891. else
  892. return;
  893. if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
  894. return;
  895. skb->sk = sk;
  896. skb->destructor = sock_efree;
  897. dst = READ_ONCE(sk->sk_rx_dst);
  898. if (dst)
  899. dst = dst_check(dst, inet6_sk(sk)->rx_dst_cookie);
  900. if (dst) {
  901. /* set noref for now.
  902. * any place which wants to hold dst has to call
  903. * dst_hold_safe()
  904. */
  905. skb_dst_set_noref(skb, dst);
  906. }
  907. }
  908. INDIRECT_CALLABLE_SCOPE int udpv6_rcv(struct sk_buff *skb)
  909. {
  910. return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
  911. }
  912. /*
  913. * Throw away all pending data and cancel the corking. Socket is locked.
  914. */
  915. static void udp_v6_flush_pending_frames(struct sock *sk)
  916. {
  917. struct udp_sock *up = udp_sk(sk);
  918. if (up->pending == AF_INET)
  919. udp_flush_pending_frames(sk);
  920. else if (up->pending) {
  921. up->len = 0;
  922. up->pending = 0;
  923. ip6_flush_pending_frames(sk);
  924. }
  925. }
  926. static int udpv6_pre_connect(struct sock *sk, struct sockaddr *uaddr,
  927. int addr_len)
  928. {
  929. if (addr_len < offsetofend(struct sockaddr, sa_family))
  930. return -EINVAL;
  931. /* The following checks are replicated from __ip6_datagram_connect()
  932. * and intended to prevent BPF program called below from accessing
  933. * bytes that are out of the bound specified by user in addr_len.
  934. */
  935. if (uaddr->sa_family == AF_INET) {
  936. if (__ipv6_only_sock(sk))
  937. return -EAFNOSUPPORT;
  938. return udp_pre_connect(sk, uaddr, addr_len);
  939. }
  940. if (addr_len < SIN6_LEN_RFC2133)
  941. return -EINVAL;
  942. return BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr);
  943. }
  944. /**
  945. * udp6_hwcsum_outgoing - handle outgoing HW checksumming
  946. * @sk: socket we are sending on
  947. * @skb: sk_buff containing the filled-in UDP header
  948. * (checksum field must be zeroed out)
  949. * @saddr: source address
  950. * @daddr: destination address
  951. * @len: length of packet
  952. */
  953. static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
  954. const struct in6_addr *saddr,
  955. const struct in6_addr *daddr, int len)
  956. {
  957. unsigned int offset;
  958. struct udphdr *uh = udp_hdr(skb);
  959. struct sk_buff *frags = skb_shinfo(skb)->frag_list;
  960. __wsum csum = 0;
  961. if (!frags) {
  962. /* Only one fragment on the socket. */
  963. skb->csum_start = skb_transport_header(skb) - skb->head;
  964. skb->csum_offset = offsetof(struct udphdr, check);
  965. uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
  966. } else {
  967. /*
  968. * HW-checksum won't work as there are two or more
  969. * fragments on the socket so that all csums of sk_buffs
  970. * should be together
  971. */
  972. offset = skb_transport_offset(skb);
  973. skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
  974. csum = skb->csum;
  975. skb->ip_summed = CHECKSUM_NONE;
  976. do {
  977. csum = csum_add(csum, frags->csum);
  978. } while ((frags = frags->next));
  979. uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
  980. csum);
  981. if (uh->check == 0)
  982. uh->check = CSUM_MANGLED_0;
  983. }
  984. }
  985. /*
  986. * Sending
  987. */
  988. static int udp_v6_send_skb(struct sk_buff *skb, struct flowi6 *fl6,
  989. struct inet_cork *cork)
  990. {
  991. struct sock *sk = skb->sk;
  992. struct udphdr *uh;
  993. int err = 0;
  994. int is_udplite = IS_UDPLITE(sk);
  995. __wsum csum = 0;
  996. int offset = skb_transport_offset(skb);
  997. int len = skb->len - offset;
  998. int datalen = len - sizeof(*uh);
  999. /*
  1000. * Create a UDP header
  1001. */
  1002. uh = udp_hdr(skb);
  1003. uh->source = fl6->fl6_sport;
  1004. uh->dest = fl6->fl6_dport;
  1005. uh->len = htons(len);
  1006. uh->check = 0;
  1007. if (cork->gso_size) {
  1008. const int hlen = skb_network_header_len(skb) +
  1009. sizeof(struct udphdr);
  1010. if (hlen + cork->gso_size > cork->fragsize) {
  1011. kfree_skb(skb);
  1012. return -EINVAL;
  1013. }
  1014. if (datalen > cork->gso_size * UDP_MAX_SEGMENTS) {
  1015. kfree_skb(skb);
  1016. return -EINVAL;
  1017. }
  1018. if (udp_sk(sk)->no_check6_tx) {
  1019. kfree_skb(skb);
  1020. return -EINVAL;
  1021. }
  1022. if (skb->ip_summed != CHECKSUM_PARTIAL || is_udplite ||
  1023. dst_xfrm(skb_dst(skb))) {
  1024. kfree_skb(skb);
  1025. return -EIO;
  1026. }
  1027. if (datalen > cork->gso_size) {
  1028. skb_shinfo(skb)->gso_size = cork->gso_size;
  1029. skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4;
  1030. skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(datalen,
  1031. cork->gso_size);
  1032. }
  1033. goto csum_partial;
  1034. }
  1035. if (is_udplite)
  1036. csum = udplite_csum(skb);
  1037. else if (udp_sk(sk)->no_check6_tx) { /* UDP csum disabled */
  1038. skb->ip_summed = CHECKSUM_NONE;
  1039. goto send;
  1040. } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
  1041. csum_partial:
  1042. udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, len);
  1043. goto send;
  1044. } else
  1045. csum = udp_csum(skb);
  1046. /* add protocol-dependent pseudo-header */
  1047. uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
  1048. len, fl6->flowi6_proto, csum);
  1049. if (uh->check == 0)
  1050. uh->check = CSUM_MANGLED_0;
  1051. send:
  1052. err = ip6_send_skb(skb);
  1053. if (err) {
  1054. if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
  1055. UDP6_INC_STATS(sock_net(sk),
  1056. UDP_MIB_SNDBUFERRORS, is_udplite);
  1057. err = 0;
  1058. }
  1059. } else {
  1060. UDP6_INC_STATS(sock_net(sk),
  1061. UDP_MIB_OUTDATAGRAMS, is_udplite);
  1062. }
  1063. return err;
  1064. }
  1065. static int udp_v6_push_pending_frames(struct sock *sk)
  1066. {
  1067. struct sk_buff *skb;
  1068. struct udp_sock *up = udp_sk(sk);
  1069. struct flowi6 fl6;
  1070. int err = 0;
  1071. if (up->pending == AF_INET)
  1072. return udp_push_pending_frames(sk);
  1073. /* ip6_finish_skb will release the cork, so make a copy of
  1074. * fl6 here.
  1075. */
  1076. fl6 = inet_sk(sk)->cork.fl.u.ip6;
  1077. skb = ip6_finish_skb(sk);
  1078. if (!skb)
  1079. goto out;
  1080. err = udp_v6_send_skb(skb, &fl6, &inet_sk(sk)->cork.base);
  1081. out:
  1082. up->len = 0;
  1083. up->pending = 0;
  1084. return err;
  1085. }
  1086. int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
  1087. {
  1088. struct ipv6_txoptions opt_space;
  1089. struct udp_sock *up = udp_sk(sk);
  1090. struct inet_sock *inet = inet_sk(sk);
  1091. struct ipv6_pinfo *np = inet6_sk(sk);
  1092. DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
  1093. struct in6_addr *daddr, *final_p, final;
  1094. struct ipv6_txoptions *opt = NULL;
  1095. struct ipv6_txoptions *opt_to_free = NULL;
  1096. struct ip6_flowlabel *flowlabel = NULL;
  1097. struct flowi6 fl6;
  1098. struct dst_entry *dst;
  1099. struct ipcm6_cookie ipc6;
  1100. int addr_len = msg->msg_namelen;
  1101. bool connected = false;
  1102. int ulen = len;
  1103. int corkreq = READ_ONCE(up->corkflag) || msg->msg_flags&MSG_MORE;
  1104. int err;
  1105. int is_udplite = IS_UDPLITE(sk);
  1106. int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
  1107. ipcm6_init(&ipc6);
  1108. ipc6.gso_size = READ_ONCE(up->gso_size);
  1109. ipc6.sockc.tsflags = sk->sk_tsflags;
  1110. ipc6.sockc.mark = sk->sk_mark;
  1111. /* destination address check */
  1112. if (sin6) {
  1113. if (addr_len < offsetof(struct sockaddr, sa_data))
  1114. return -EINVAL;
  1115. switch (sin6->sin6_family) {
  1116. case AF_INET6:
  1117. if (addr_len < SIN6_LEN_RFC2133)
  1118. return -EINVAL;
  1119. daddr = &sin6->sin6_addr;
  1120. if (ipv6_addr_any(daddr) &&
  1121. ipv6_addr_v4mapped(&np->saddr))
  1122. ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK),
  1123. daddr);
  1124. break;
  1125. case AF_INET:
  1126. goto do_udp_sendmsg;
  1127. case AF_UNSPEC:
  1128. msg->msg_name = sin6 = NULL;
  1129. msg->msg_namelen = addr_len = 0;
  1130. daddr = NULL;
  1131. break;
  1132. default:
  1133. return -EINVAL;
  1134. }
  1135. } else if (!up->pending) {
  1136. if (sk->sk_state != TCP_ESTABLISHED)
  1137. return -EDESTADDRREQ;
  1138. daddr = &sk->sk_v6_daddr;
  1139. } else
  1140. daddr = NULL;
  1141. if (daddr) {
  1142. if (ipv6_addr_v4mapped(daddr)) {
  1143. struct sockaddr_in sin;
  1144. sin.sin_family = AF_INET;
  1145. sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
  1146. sin.sin_addr.s_addr = daddr->s6_addr32[3];
  1147. msg->msg_name = &sin;
  1148. msg->msg_namelen = sizeof(sin);
  1149. do_udp_sendmsg:
  1150. if (__ipv6_only_sock(sk))
  1151. return -ENETUNREACH;
  1152. return udp_sendmsg(sk, msg, len);
  1153. }
  1154. }
  1155. if (up->pending == AF_INET)
  1156. return udp_sendmsg(sk, msg, len);
  1157. /* Rough check on arithmetic overflow,
  1158. better check is made in ip6_append_data().
  1159. */
  1160. if (len > INT_MAX - sizeof(struct udphdr))
  1161. return -EMSGSIZE;
  1162. getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
  1163. if (up->pending) {
  1164. /*
  1165. * There are pending frames.
  1166. * The socket lock must be held while it's corked.
  1167. */
  1168. lock_sock(sk);
  1169. if (likely(up->pending)) {
  1170. if (unlikely(up->pending != AF_INET6)) {
  1171. release_sock(sk);
  1172. return -EAFNOSUPPORT;
  1173. }
  1174. dst = NULL;
  1175. goto do_append_data;
  1176. }
  1177. release_sock(sk);
  1178. }
  1179. ulen += sizeof(struct udphdr);
  1180. memset(&fl6, 0, sizeof(fl6));
  1181. if (sin6) {
  1182. if (sin6->sin6_port == 0)
  1183. return -EINVAL;
  1184. fl6.fl6_dport = sin6->sin6_port;
  1185. daddr = &sin6->sin6_addr;
  1186. if (np->sndflow) {
  1187. fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
  1188. if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
  1189. flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
  1190. if (IS_ERR(flowlabel))
  1191. return -EINVAL;
  1192. }
  1193. }
  1194. /*
  1195. * Otherwise it will be difficult to maintain
  1196. * sk->sk_dst_cache.
  1197. */
  1198. if (sk->sk_state == TCP_ESTABLISHED &&
  1199. ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
  1200. daddr = &sk->sk_v6_daddr;
  1201. if (addr_len >= sizeof(struct sockaddr_in6) &&
  1202. sin6->sin6_scope_id &&
  1203. __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
  1204. fl6.flowi6_oif = sin6->sin6_scope_id;
  1205. } else {
  1206. if (sk->sk_state != TCP_ESTABLISHED)
  1207. return -EDESTADDRREQ;
  1208. fl6.fl6_dport = inet->inet_dport;
  1209. daddr = &sk->sk_v6_daddr;
  1210. fl6.flowlabel = np->flow_label;
  1211. connected = true;
  1212. }
  1213. if (!fl6.flowi6_oif)
  1214. fl6.flowi6_oif = sk->sk_bound_dev_if;
  1215. if (!fl6.flowi6_oif)
  1216. fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
  1217. fl6.flowi6_uid = sk->sk_uid;
  1218. if (msg->msg_controllen) {
  1219. opt = &opt_space;
  1220. memset(opt, 0, sizeof(struct ipv6_txoptions));
  1221. opt->tot_len = sizeof(*opt);
  1222. ipc6.opt = opt;
  1223. err = udp_cmsg_send(sk, msg, &ipc6.gso_size);
  1224. if (err > 0)
  1225. err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6,
  1226. &ipc6);
  1227. if (err < 0) {
  1228. fl6_sock_release(flowlabel);
  1229. return err;
  1230. }
  1231. if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
  1232. flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
  1233. if (IS_ERR(flowlabel))
  1234. return -EINVAL;
  1235. }
  1236. if (!(opt->opt_nflen|opt->opt_flen))
  1237. opt = NULL;
  1238. connected = false;
  1239. }
  1240. if (!opt) {
  1241. opt = txopt_get(np);
  1242. opt_to_free = opt;
  1243. }
  1244. if (flowlabel)
  1245. opt = fl6_merge_options(&opt_space, flowlabel, opt);
  1246. opt = ipv6_fixup_options(&opt_space, opt);
  1247. ipc6.opt = opt;
  1248. fl6.flowi6_proto = sk->sk_protocol;
  1249. fl6.flowi6_mark = ipc6.sockc.mark;
  1250. fl6.daddr = *daddr;
  1251. if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
  1252. fl6.saddr = np->saddr;
  1253. fl6.fl6_sport = inet->inet_sport;
  1254. if (cgroup_bpf_enabled && !connected) {
  1255. err = BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk,
  1256. (struct sockaddr *)sin6, &fl6.saddr);
  1257. if (err)
  1258. goto out_no_dst;
  1259. if (sin6) {
  1260. if (ipv6_addr_v4mapped(&sin6->sin6_addr)) {
  1261. /* BPF program rewrote IPv6-only by IPv4-mapped
  1262. * IPv6. It's currently unsupported.
  1263. */
  1264. err = -ENOTSUPP;
  1265. goto out_no_dst;
  1266. }
  1267. if (sin6->sin6_port == 0) {
  1268. /* BPF program set invalid port. Reject it. */
  1269. err = -EINVAL;
  1270. goto out_no_dst;
  1271. }
  1272. fl6.fl6_dport = sin6->sin6_port;
  1273. fl6.daddr = sin6->sin6_addr;
  1274. }
  1275. }
  1276. if (ipv6_addr_any(&fl6.daddr))
  1277. fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
  1278. final_p = fl6_update_dst(&fl6, opt, &final);
  1279. if (final_p)
  1280. connected = false;
  1281. if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) {
  1282. fl6.flowi6_oif = np->mcast_oif;
  1283. connected = false;
  1284. } else if (!fl6.flowi6_oif)
  1285. fl6.flowi6_oif = np->ucast_oif;
  1286. security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
  1287. if (ipc6.tclass < 0)
  1288. ipc6.tclass = np->tclass;
  1289. fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
  1290. dst = ip6_sk_dst_lookup_flow(sk, &fl6, final_p, connected);
  1291. if (IS_ERR(dst)) {
  1292. err = PTR_ERR(dst);
  1293. dst = NULL;
  1294. goto out;
  1295. }
  1296. if (ipc6.hlimit < 0)
  1297. ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
  1298. if (msg->msg_flags&MSG_CONFIRM)
  1299. goto do_confirm;
  1300. back_from_confirm:
  1301. /* Lockless fast path for the non-corking case */
  1302. if (!corkreq) {
  1303. struct inet_cork_full cork;
  1304. struct sk_buff *skb;
  1305. skb = ip6_make_skb(sk, getfrag, msg, ulen,
  1306. sizeof(struct udphdr), &ipc6,
  1307. &fl6, (struct rt6_info *)dst,
  1308. msg->msg_flags, &cork);
  1309. err = PTR_ERR(skb);
  1310. if (!IS_ERR_OR_NULL(skb))
  1311. err = udp_v6_send_skb(skb, &fl6, &cork.base);
  1312. goto out;
  1313. }
  1314. lock_sock(sk);
  1315. if (unlikely(up->pending)) {
  1316. /* The socket is already corked while preparing it. */
  1317. /* ... which is an evident application bug. --ANK */
  1318. release_sock(sk);
  1319. net_dbg_ratelimited("udp cork app bug 2\n");
  1320. err = -EINVAL;
  1321. goto out;
  1322. }
  1323. up->pending = AF_INET6;
  1324. do_append_data:
  1325. if (ipc6.dontfrag < 0)
  1326. ipc6.dontfrag = np->dontfrag;
  1327. up->len += ulen;
  1328. err = ip6_append_data(sk, getfrag, msg, ulen, sizeof(struct udphdr),
  1329. &ipc6, &fl6, (struct rt6_info *)dst,
  1330. corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
  1331. if (err)
  1332. udp_v6_flush_pending_frames(sk);
  1333. else if (!corkreq)
  1334. err = udp_v6_push_pending_frames(sk);
  1335. else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
  1336. up->pending = 0;
  1337. if (err > 0)
  1338. err = np->recverr ? net_xmit_errno(err) : 0;
  1339. release_sock(sk);
  1340. out:
  1341. dst_release(dst);
  1342. out_no_dst:
  1343. fl6_sock_release(flowlabel);
  1344. txopt_put(opt_to_free);
  1345. if (!err)
  1346. return len;
  1347. /*
  1348. * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
  1349. * ENOBUFS might not be good (it's not tunable per se), but otherwise
  1350. * we don't have a good statistic (IpOutDiscards but it can be too many
  1351. * things). We could add another new stat but at least for now that
  1352. * seems like overkill.
  1353. */
  1354. if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
  1355. UDP6_INC_STATS(sock_net(sk),
  1356. UDP_MIB_SNDBUFERRORS, is_udplite);
  1357. }
  1358. return err;
  1359. do_confirm:
  1360. if (msg->msg_flags & MSG_PROBE)
  1361. dst_confirm_neigh(dst, &fl6.daddr);
  1362. if (!(msg->msg_flags&MSG_PROBE) || len)
  1363. goto back_from_confirm;
  1364. err = 0;
  1365. goto out;
  1366. }
  1367. void udpv6_destroy_sock(struct sock *sk)
  1368. {
  1369. struct udp_sock *up = udp_sk(sk);
  1370. lock_sock(sk);
  1371. /* protects from races with udp_abort() */
  1372. sock_set_flag(sk, SOCK_DEAD);
  1373. udp_v6_flush_pending_frames(sk);
  1374. release_sock(sk);
  1375. if (static_branch_unlikely(&udpv6_encap_needed_key)) {
  1376. if (up->encap_type) {
  1377. void (*encap_destroy)(struct sock *sk);
  1378. encap_destroy = READ_ONCE(up->encap_destroy);
  1379. if (encap_destroy)
  1380. encap_destroy(sk);
  1381. }
  1382. if (up->encap_enabled) {
  1383. static_branch_dec(&udpv6_encap_needed_key);
  1384. udp_encap_disable();
  1385. }
  1386. }
  1387. inet6_destroy_sock(sk);
  1388. }
  1389. /*
  1390. * Socket option code for UDP
  1391. */
  1392. int udpv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
  1393. unsigned int optlen)
  1394. {
  1395. if (level == SOL_UDP || level == SOL_UDPLITE)
  1396. return udp_lib_setsockopt(sk, level, optname,
  1397. optval, optlen,
  1398. udp_v6_push_pending_frames);
  1399. return ipv6_setsockopt(sk, level, optname, optval, optlen);
  1400. }
  1401. int udpv6_getsockopt(struct sock *sk, int level, int optname,
  1402. char __user *optval, int __user *optlen)
  1403. {
  1404. if (level == SOL_UDP || level == SOL_UDPLITE)
  1405. return udp_lib_getsockopt(sk, level, optname, optval, optlen);
  1406. return ipv6_getsockopt(sk, level, optname, optval, optlen);
  1407. }
  1408. /* thinking of making this const? Don't.
  1409. * early_demux can change based on sysctl.
  1410. */
  1411. static struct inet6_protocol udpv6_protocol = {
  1412. .early_demux = udp_v6_early_demux,
  1413. .early_demux_handler = udp_v6_early_demux,
  1414. .handler = udpv6_rcv,
  1415. .err_handler = udpv6_err,
  1416. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  1417. };
  1418. /* ------------------------------------------------------------------------ */
  1419. #ifdef CONFIG_PROC_FS
  1420. int udp6_seq_show(struct seq_file *seq, void *v)
  1421. {
  1422. if (v == SEQ_START_TOKEN) {
  1423. seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
  1424. } else {
  1425. int bucket = ((struct udp_iter_state *)seq->private)->bucket;
  1426. struct inet_sock *inet = inet_sk(v);
  1427. __u16 srcp = ntohs(inet->inet_sport);
  1428. __u16 destp = ntohs(inet->inet_dport);
  1429. __ip6_dgram_sock_seq_show(seq, v, srcp, destp,
  1430. udp_rqueue_get(v), bucket);
  1431. }
  1432. return 0;
  1433. }
  1434. const struct seq_operations udp6_seq_ops = {
  1435. .start = udp_seq_start,
  1436. .next = udp_seq_next,
  1437. .stop = udp_seq_stop,
  1438. .show = udp6_seq_show,
  1439. };
  1440. EXPORT_SYMBOL(udp6_seq_ops);
  1441. static struct udp_seq_afinfo udp6_seq_afinfo = {
  1442. .family = AF_INET6,
  1443. .udp_table = &udp_table,
  1444. };
  1445. int __net_init udp6_proc_init(struct net *net)
  1446. {
  1447. if (!proc_create_net_data("udp6", 0444, net->proc_net, &udp6_seq_ops,
  1448. sizeof(struct udp_iter_state), &udp6_seq_afinfo))
  1449. return -ENOMEM;
  1450. return 0;
  1451. }
  1452. void udp6_proc_exit(struct net *net)
  1453. {
  1454. remove_proc_entry("udp6", net->proc_net);
  1455. }
  1456. #endif /* CONFIG_PROC_FS */
  1457. /* ------------------------------------------------------------------------ */
  1458. struct proto udpv6_prot = {
  1459. .name = "UDPv6",
  1460. .owner = THIS_MODULE,
  1461. .close = udp_lib_close,
  1462. .pre_connect = udpv6_pre_connect,
  1463. .connect = ip6_datagram_connect,
  1464. .disconnect = udp_disconnect,
  1465. .ioctl = udp_ioctl,
  1466. .init = udp_init_sock,
  1467. .destroy = udpv6_destroy_sock,
  1468. .setsockopt = udpv6_setsockopt,
  1469. .getsockopt = udpv6_getsockopt,
  1470. .sendmsg = udpv6_sendmsg,
  1471. .recvmsg = udpv6_recvmsg,
  1472. .release_cb = ip6_datagram_release_cb,
  1473. .hash = udp_lib_hash,
  1474. .unhash = udp_lib_unhash,
  1475. .rehash = udp_v6_rehash,
  1476. .get_port = udp_v6_get_port,
  1477. .memory_allocated = &udp_memory_allocated,
  1478. .sysctl_mem = sysctl_udp_mem,
  1479. .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_udp_wmem_min),
  1480. .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_udp_rmem_min),
  1481. .obj_size = sizeof(struct udp6_sock),
  1482. .h.udp_table = &udp_table,
  1483. .diag_destroy = udp_abort,
  1484. };
  1485. static struct inet_protosw udpv6_protosw = {
  1486. .type = SOCK_DGRAM,
  1487. .protocol = IPPROTO_UDP,
  1488. .prot = &udpv6_prot,
  1489. .ops = &inet6_dgram_ops,
  1490. .flags = INET_PROTOSW_PERMANENT,
  1491. };
  1492. int __init udpv6_init(void)
  1493. {
  1494. int ret;
  1495. ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
  1496. if (ret)
  1497. goto out;
  1498. ret = inet6_register_protosw(&udpv6_protosw);
  1499. if (ret)
  1500. goto out_udpv6_protocol;
  1501. out:
  1502. return ret;
  1503. out_udpv6_protocol:
  1504. inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
  1505. goto out;
  1506. }
  1507. void udpv6_exit(void)
  1508. {
  1509. inet6_unregister_protosw(&udpv6_protosw);
  1510. inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
  1511. }