udp.c 39 KB

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