udp.c 39 KB

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