udp.c 42 KB

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