udp.c 39 KB

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