udp.c 39 KB

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