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