udp.c 40 KB

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