udp.c 42 KB

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