udp.c 39 KB

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