udp.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  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. sock_i_uid(sp), 0,
  1261. sock_i_ino(sp),
  1262. atomic_read(&sp->sk_refcnt), sp,
  1263. atomic_read(&sp->sk_drops));
  1264. }
  1265. int udp6_seq_show(struct seq_file *seq, void *v)
  1266. {
  1267. if (v == SEQ_START_TOKEN)
  1268. seq_printf(seq,
  1269. " sl "
  1270. "local_address "
  1271. "remote_address "
  1272. "st tx_queue rx_queue tr tm->when retrnsmt"
  1273. " uid timeout inode ref pointer drops\n");
  1274. else
  1275. udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
  1276. return 0;
  1277. }
  1278. static const struct file_operations udp6_afinfo_seq_fops = {
  1279. .owner = THIS_MODULE,
  1280. .open = udp_seq_open,
  1281. .read = seq_read,
  1282. .llseek = seq_lseek,
  1283. .release = seq_release_net
  1284. };
  1285. static struct udp_seq_afinfo udp6_seq_afinfo = {
  1286. .name = "udp6",
  1287. .family = AF_INET6,
  1288. .udp_table = &udp_table,
  1289. .seq_fops = &udp6_afinfo_seq_fops,
  1290. .seq_ops = {
  1291. .show = udp6_seq_show,
  1292. },
  1293. };
  1294. int __net_init udp6_proc_init(struct net *net)
  1295. {
  1296. return udp_proc_register(net, &udp6_seq_afinfo);
  1297. }
  1298. void udp6_proc_exit(struct net *net) {
  1299. udp_proc_unregister(net, &udp6_seq_afinfo);
  1300. }
  1301. #endif /* CONFIG_PROC_FS */
  1302. /* ------------------------------------------------------------------------ */
  1303. struct proto udpv6_prot = {
  1304. .name = "UDPv6",
  1305. .owner = THIS_MODULE,
  1306. .close = udp_lib_close,
  1307. .connect = ip6_datagram_connect,
  1308. .disconnect = udp_disconnect,
  1309. .ioctl = udp_ioctl,
  1310. .destroy = udpv6_destroy_sock,
  1311. .setsockopt = udpv6_setsockopt,
  1312. .getsockopt = udpv6_getsockopt,
  1313. .sendmsg = udpv6_sendmsg,
  1314. .recvmsg = udpv6_recvmsg,
  1315. .backlog_rcv = __udpv6_queue_rcv_skb,
  1316. .hash = udp_lib_hash,
  1317. .unhash = udp_lib_unhash,
  1318. .rehash = udp_v6_rehash,
  1319. .get_port = udp_v6_get_port,
  1320. .memory_allocated = &udp_memory_allocated,
  1321. .sysctl_mem = sysctl_udp_mem,
  1322. .sysctl_wmem = &sysctl_udp_wmem_min,
  1323. .sysctl_rmem = &sysctl_udp_rmem_min,
  1324. .obj_size = sizeof(struct udp6_sock),
  1325. .slab_flags = SLAB_DESTROY_BY_RCU,
  1326. .h.udp_table = &udp_table,
  1327. #ifdef CONFIG_COMPAT
  1328. .compat_setsockopt = compat_udpv6_setsockopt,
  1329. .compat_getsockopt = compat_udpv6_getsockopt,
  1330. #endif
  1331. .clear_sk = sk_prot_clear_portaddr_nulls,
  1332. };
  1333. static struct inet_protosw udpv6_protosw = {
  1334. .type = SOCK_DGRAM,
  1335. .protocol = IPPROTO_UDP,
  1336. .prot = &udpv6_prot,
  1337. .ops = &inet6_dgram_ops,
  1338. .no_check = UDP_CSUM_DEFAULT,
  1339. .flags = INET_PROTOSW_PERMANENT,
  1340. };
  1341. int __init udpv6_init(void)
  1342. {
  1343. int ret;
  1344. ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
  1345. if (ret)
  1346. goto out;
  1347. ret = inet6_register_protosw(&udpv6_protosw);
  1348. if (ret)
  1349. goto out_udpv6_protocol;
  1350. out:
  1351. return ret;
  1352. out_udpv6_protocol:
  1353. inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
  1354. goto out;
  1355. }
  1356. void udpv6_exit(void)
  1357. {
  1358. inet6_unregister_protosw(&udpv6_protosw);
  1359. inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
  1360. }