udp.c 41 KB

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