udp.c 40 KB

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