udp.c 39 KB

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