udp.c 37 KB

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