udp.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * UDP over IPv6
  4. * Linux INET6 implementation
  5. *
  6. * Authors:
  7. * Pedro Roque <roque@di.fc.ul.pt>
  8. *
  9. * Based on linux/ipv4/udp.c
  10. *
  11. * Fixes:
  12. * Hideaki YOSHIFUJI : sin6_scope_id support
  13. * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
  14. * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind
  15. * a single port at the same time.
  16. * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data
  17. * YOSHIFUJI Hideaki @USAGI: convert /proc/net/udp6 to seq_file.
  18. */
  19. #include <linux/bpf-cgroup.h>
  20. #include <linux/errno.h>
  21. #include <linux/types.h>
  22. #include <linux/socket.h>
  23. #include <linux/sockios.h>
  24. #include <linux/net.h>
  25. #include <linux/in6.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/if_arp.h>
  28. #include <linux/ipv6.h>
  29. #include <linux/icmpv6.h>
  30. #include <linux/init.h>
  31. #include <linux/module.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/slab.h>
  34. #include <linux/uaccess.h>
  35. #include <linux/indirect_call_wrapper.h>
  36. #include <trace/events/udp.h>
  37. #include <net/addrconf.h>
  38. #include <net/ndisc.h>
  39. #include <net/protocol.h>
  40. #include <net/transp_v6.h>
  41. #include <net/ip6_route.h>
  42. #include <net/raw.h>
  43. #include <net/seg6.h>
  44. #include <net/tcp_states.h>
  45. #include <net/ip6_checksum.h>
  46. #include <net/ip6_tunnel.h>
  47. #include <net/udp_tunnel.h>
  48. #include <net/xfrm.h>
  49. #include <net/inet_hashtables.h>
  50. #include <net/inet6_hashtables.h>
  51. #include <net/busy_poll.h>
  52. #include <net/sock_reuseport.h>
  53. #include <net/gro.h>
  54. #include <linux/proc_fs.h>
  55. #include <linux/seq_file.h>
  56. #include <trace/events/skb.h>
  57. #include "udp_impl.h"
  58. static void udpv6_destruct_sock(struct sock *sk)
  59. {
  60. udp_destruct_common(sk);
  61. inet6_sock_destruct(sk);
  62. }
  63. int udpv6_init_sock(struct sock *sk)
  64. {
  65. int res = udp_lib_init_sock(sk);
  66. sk->sk_destruct = udpv6_destruct_sock;
  67. set_bit(SOCK_SUPPORT_ZC, &sk->sk_socket->flags);
  68. return res;
  69. }
  70. INDIRECT_CALLABLE_SCOPE
  71. u32 udp6_ehashfn(const struct net *net,
  72. const struct in6_addr *laddr,
  73. const u16 lport,
  74. const struct in6_addr *faddr,
  75. const __be16 fport)
  76. {
  77. u32 lhash, fhash;
  78. net_get_random_once(&udp6_ehash_secret,
  79. sizeof(udp6_ehash_secret));
  80. net_get_random_once(&udp_ipv6_hash_secret,
  81. sizeof(udp_ipv6_hash_secret));
  82. lhash = (__force u32)laddr->s6_addr32[3];
  83. fhash = __ipv6_addr_jhash(faddr, udp_ipv6_hash_secret);
  84. return __inet6_ehashfn(lhash, lport, fhash, fport,
  85. udp6_ehash_secret + net_hash_mix(net));
  86. }
  87. int udp_v6_get_port(struct sock *sk, unsigned short snum)
  88. {
  89. unsigned int hash2_nulladdr =
  90. ipv6_portaddr_hash(sock_net(sk), &in6addr_any, snum);
  91. unsigned int hash2_partial =
  92. ipv6_portaddr_hash(sock_net(sk), &sk->sk_v6_rcv_saddr, 0);
  93. /* precompute partial secondary hash */
  94. udp_sk(sk)->udp_portaddr_hash = hash2_partial;
  95. return udp_lib_get_port(sk, snum, hash2_nulladdr);
  96. }
  97. void udp_v6_rehash(struct sock *sk)
  98. {
  99. u16 new_hash = ipv6_portaddr_hash(sock_net(sk),
  100. &sk->sk_v6_rcv_saddr,
  101. inet_sk(sk)->inet_num);
  102. u16 new_hash4;
  103. if (ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr)) {
  104. new_hash4 = udp_ehashfn(sock_net(sk),
  105. sk->sk_rcv_saddr, sk->sk_num,
  106. sk->sk_daddr, sk->sk_dport);
  107. } else {
  108. new_hash4 = udp6_ehashfn(sock_net(sk),
  109. &sk->sk_v6_rcv_saddr, sk->sk_num,
  110. &sk->sk_v6_daddr, sk->sk_dport);
  111. }
  112. udp_lib_rehash(sk, new_hash, new_hash4);
  113. }
  114. static int compute_score(struct sock *sk, const struct net *net,
  115. const struct in6_addr *saddr, __be16 sport,
  116. const struct in6_addr *daddr, unsigned short hnum,
  117. int dif, int sdif)
  118. {
  119. int bound_dev_if, score;
  120. struct inet_sock *inet;
  121. bool dev_match;
  122. if (!net_eq(sock_net(sk), net) ||
  123. udp_sk(sk)->udp_port_hash != hnum ||
  124. sk->sk_family != PF_INET6)
  125. return -1;
  126. if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
  127. return -1;
  128. score = 0;
  129. inet = inet_sk(sk);
  130. if (inet->inet_dport) {
  131. if (inet->inet_dport != sport)
  132. return -1;
  133. score++;
  134. }
  135. if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
  136. if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
  137. return -1;
  138. score++;
  139. }
  140. bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
  141. dev_match = udp_sk_bound_dev_eq(net, bound_dev_if, dif, sdif);
  142. if (!dev_match)
  143. return -1;
  144. if (bound_dev_if)
  145. score++;
  146. if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
  147. score++;
  148. return score;
  149. }
  150. /**
  151. * udp6_lib_lookup1() - Simplified lookup using primary hash (destination port)
  152. * @net: Network namespace
  153. * @saddr: Source address, network order
  154. * @sport: Source port, network order
  155. * @daddr: Destination address, network order
  156. * @hnum: Destination port, host order
  157. * @dif: Destination interface index
  158. * @sdif: Destination bridge port index, if relevant
  159. * @udptable: Set of UDP hash tables
  160. *
  161. * Simplified lookup to be used as fallback if no sockets are found due to a
  162. * potential race between (receive) address change, and lookup happening before
  163. * the rehash operation. This function ignores SO_REUSEPORT groups while scoring
  164. * result sockets, because if we have one, we don't need the fallback at all.
  165. *
  166. * Called under rcu_read_lock().
  167. *
  168. * Return: socket with highest matching score if any, NULL if none
  169. */
  170. static struct sock *udp6_lib_lookup1(const struct net *net,
  171. const struct in6_addr *saddr, __be16 sport,
  172. const struct in6_addr *daddr,
  173. unsigned int hnum, int dif, int sdif,
  174. const struct udp_table *udptable)
  175. {
  176. unsigned int slot = udp_hashfn(net, hnum, udptable->mask);
  177. struct udp_hslot *hslot = &udptable->hash[slot];
  178. struct sock *sk, *result = NULL;
  179. int score, badness = 0;
  180. sk_for_each_rcu(sk, &hslot->head) {
  181. score = compute_score(sk, net,
  182. saddr, sport, daddr, hnum, dif, sdif);
  183. if (score > badness) {
  184. result = sk;
  185. badness = score;
  186. }
  187. }
  188. return result;
  189. }
  190. /* called with rcu_read_lock() */
  191. static struct sock *udp6_lib_lookup2(const struct net *net,
  192. const struct in6_addr *saddr, __be16 sport,
  193. const struct in6_addr *daddr, unsigned int hnum,
  194. int dif, int sdif, struct udp_hslot *hslot2,
  195. struct sk_buff *skb)
  196. {
  197. struct sock *sk, *result;
  198. int score, badness;
  199. bool need_rescore;
  200. result = NULL;
  201. badness = -1;
  202. udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
  203. need_rescore = false;
  204. rescore:
  205. score = compute_score(need_rescore ? result : sk, net, saddr,
  206. sport, daddr, hnum, dif, sdif);
  207. if (score > badness) {
  208. badness = score;
  209. if (need_rescore)
  210. continue;
  211. if (sk->sk_state == TCP_ESTABLISHED) {
  212. result = sk;
  213. continue;
  214. }
  215. result = inet6_lookup_reuseport(net, sk, skb, sizeof(struct udphdr),
  216. saddr, sport, daddr, hnum, udp6_ehashfn);
  217. if (!result) {
  218. result = sk;
  219. continue;
  220. }
  221. /* Fall back to scoring if group has connections */
  222. if (!reuseport_has_conns(sk))
  223. return result;
  224. /* Reuseport logic returned an error, keep original score. */
  225. if (IS_ERR(result))
  226. continue;
  227. /* compute_score is too long of a function to be
  228. * inlined, and calling it again here yields
  229. * measurable overhead for some
  230. * workloads. Work around it by jumping
  231. * backwards to rescore 'result'.
  232. */
  233. need_rescore = true;
  234. goto rescore;
  235. }
  236. }
  237. return result;
  238. }
  239. #if IS_ENABLED(CONFIG_BASE_SMALL)
  240. static struct sock *udp6_lib_lookup4(const struct net *net,
  241. const struct in6_addr *saddr, __be16 sport,
  242. const struct in6_addr *daddr,
  243. unsigned int hnum, int dif, int sdif,
  244. struct udp_table *udptable)
  245. {
  246. return NULL;
  247. }
  248. static void udp6_hash4(struct sock *sk)
  249. {
  250. }
  251. #else /* !CONFIG_BASE_SMALL */
  252. static struct sock *udp6_lib_lookup4(const struct net *net,
  253. const struct in6_addr *saddr, __be16 sport,
  254. const struct in6_addr *daddr,
  255. unsigned int hnum, int dif, int sdif,
  256. struct udp_table *udptable)
  257. {
  258. const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
  259. const struct hlist_nulls_node *node;
  260. struct udp_hslot *hslot4;
  261. unsigned int hash4, slot;
  262. struct udp_sock *up;
  263. struct sock *sk;
  264. hash4 = udp6_ehashfn(net, daddr, hnum, saddr, sport);
  265. slot = hash4 & udptable->mask;
  266. hslot4 = &udptable->hash4[slot];
  267. begin:
  268. udp_lrpa_for_each_entry_rcu(up, node, &hslot4->nulls_head) {
  269. sk = (struct sock *)up;
  270. if (inet6_match(net, sk, saddr, daddr, ports, dif, sdif))
  271. return sk;
  272. }
  273. /* if the nulls value we got at the end of this lookup is not the
  274. * expected one, we must restart lookup. We probably met an item that
  275. * was moved to another chain due to rehash.
  276. */
  277. if (get_nulls_value(node) != slot)
  278. goto begin;
  279. return NULL;
  280. }
  281. static void udp6_hash4(struct sock *sk)
  282. {
  283. struct net *net = sock_net(sk);
  284. unsigned int hash;
  285. if (ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr)) {
  286. udp4_hash4(sk);
  287. return;
  288. }
  289. if (sk_unhashed(sk) || ipv6_addr_any(&sk->sk_v6_rcv_saddr))
  290. return;
  291. hash = udp6_ehashfn(net, &sk->sk_v6_rcv_saddr, sk->sk_num,
  292. &sk->sk_v6_daddr, sk->sk_dport);
  293. udp_lib_hash4(sk, hash);
  294. }
  295. #endif /* CONFIG_BASE_SMALL */
  296. /* rcu_read_lock() must be held */
  297. struct sock *__udp6_lib_lookup(const struct net *net,
  298. const struct in6_addr *saddr, __be16 sport,
  299. const struct in6_addr *daddr, __be16 dport,
  300. int dif, int sdif, struct udp_table *udptable,
  301. struct sk_buff *skb)
  302. {
  303. unsigned short hnum = ntohs(dport);
  304. struct udp_hslot *hslot2;
  305. struct sock *result, *sk;
  306. unsigned int hash2;
  307. hash2 = ipv6_portaddr_hash(net, daddr, hnum);
  308. hslot2 = udp_hashslot2(udptable, hash2);
  309. if (udp_has_hash4(hslot2)) {
  310. result = udp6_lib_lookup4(net, saddr, sport, daddr, hnum,
  311. dif, sdif, udptable);
  312. if (result) /* udp6_lib_lookup4 return sk or NULL */
  313. return result;
  314. }
  315. /* Lookup connected or non-wildcard sockets */
  316. result = udp6_lib_lookup2(net, saddr, sport,
  317. daddr, hnum, dif, sdif,
  318. hslot2, skb);
  319. if (!IS_ERR_OR_NULL(result) && result->sk_state == TCP_ESTABLISHED)
  320. goto done;
  321. /* Lookup redirect from BPF */
  322. if (static_branch_unlikely(&bpf_sk_lookup_enabled) &&
  323. udptable == net->ipv4.udp_table) {
  324. sk = inet6_lookup_run_sk_lookup(net, IPPROTO_UDP, skb, sizeof(struct udphdr),
  325. saddr, sport, daddr, hnum, dif,
  326. udp6_ehashfn);
  327. if (sk) {
  328. result = sk;
  329. goto done;
  330. }
  331. }
  332. /* Got non-wildcard socket or error on first lookup */
  333. if (result)
  334. goto done;
  335. /* Lookup wildcard sockets */
  336. hash2 = ipv6_portaddr_hash(net, &in6addr_any, hnum);
  337. hslot2 = udp_hashslot2(udptable, hash2);
  338. result = udp6_lib_lookup2(net, saddr, sport,
  339. &in6addr_any, hnum, dif, sdif,
  340. hslot2, skb);
  341. if (!IS_ERR_OR_NULL(result))
  342. goto done;
  343. /* Cover address change/lookup/rehash race: see __udp4_lib_lookup() */
  344. result = udp6_lib_lookup1(net, saddr, sport, daddr, hnum, dif, sdif,
  345. udptable);
  346. done:
  347. if (IS_ERR(result))
  348. return NULL;
  349. return result;
  350. }
  351. EXPORT_SYMBOL_GPL(__udp6_lib_lookup);
  352. static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
  353. __be16 sport, __be16 dport,
  354. struct udp_table *udptable)
  355. {
  356. const struct ipv6hdr *iph = ipv6_hdr(skb);
  357. return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
  358. &iph->daddr, dport, inet6_iif(skb),
  359. inet6_sdif(skb), udptable, skb);
  360. }
  361. struct sock *udp6_lib_lookup_skb(const struct sk_buff *skb,
  362. __be16 sport, __be16 dport)
  363. {
  364. const u16 offset = NAPI_GRO_CB(skb)->network_offsets[skb->encapsulation];
  365. const struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + offset);
  366. struct net *net = dev_net(skb->dev);
  367. int iif, sdif;
  368. inet6_get_iif_sdif(skb, &iif, &sdif);
  369. return __udp6_lib_lookup(net, &iph->saddr, sport,
  370. &iph->daddr, dport, iif,
  371. sdif, net->ipv4.udp_table, NULL);
  372. }
  373. /* Must be called under rcu_read_lock().
  374. * Does increment socket refcount.
  375. */
  376. #if IS_ENABLED(CONFIG_NF_TPROXY_IPV6) || IS_ENABLED(CONFIG_NF_SOCKET_IPV6)
  377. struct sock *udp6_lib_lookup(const struct net *net, const struct in6_addr *saddr, __be16 sport,
  378. const struct in6_addr *daddr, __be16 dport, int dif)
  379. {
  380. struct sock *sk;
  381. sk = __udp6_lib_lookup(net, saddr, sport, daddr, dport,
  382. dif, 0, net->ipv4.udp_table, NULL);
  383. if (sk && !refcount_inc_not_zero(&sk->sk_refcnt))
  384. sk = NULL;
  385. return sk;
  386. }
  387. EXPORT_SYMBOL_GPL(udp6_lib_lookup);
  388. #endif
  389. /* do not use the scratch area len for jumbogram: their length exceeds the
  390. * scratch area space; note that the IP6CB flags is still in the first
  391. * cacheline, so checking for jumbograms is cheap
  392. */
  393. static int udp6_skb_len(struct sk_buff *skb)
  394. {
  395. return unlikely(inet6_is_jumbogram(skb)) ? skb->len : udp_skb_len(skb);
  396. }
  397. /*
  398. * This should be easy, if there is something there we
  399. * return it, otherwise we block.
  400. */
  401. int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
  402. int flags, int *addr_len)
  403. {
  404. struct ipv6_pinfo *np = inet6_sk(sk);
  405. struct inet_sock *inet = inet_sk(sk);
  406. struct sk_buff *skb;
  407. unsigned int ulen, copied;
  408. int off, err, peeking = flags & MSG_PEEK;
  409. int is_udplite = IS_UDPLITE(sk);
  410. struct udp_mib __percpu *mib;
  411. bool checksum_valid = false;
  412. int is_udp4;
  413. if (flags & MSG_ERRQUEUE)
  414. return ipv6_recv_error(sk, msg, len, addr_len);
  415. if (np->rxopt.bits.rxpmtu && READ_ONCE(np->rxpmtu))
  416. return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
  417. try_again:
  418. off = sk_peek_offset(sk, flags);
  419. skb = __skb_recv_udp(sk, flags, &off, &err);
  420. if (!skb)
  421. return err;
  422. ulen = udp6_skb_len(skb);
  423. copied = len;
  424. if (copied > ulen - off)
  425. copied = ulen - off;
  426. else if (copied < ulen)
  427. msg->msg_flags |= MSG_TRUNC;
  428. is_udp4 = (skb->protocol == htons(ETH_P_IP));
  429. mib = __UDPX_MIB(sk, is_udp4);
  430. /*
  431. * If checksum is needed at all, try to do it while copying the
  432. * data. If the data is truncated, or if we only want a partial
  433. * coverage checksum (UDP-Lite), do it before the copy.
  434. */
  435. if (copied < ulen || peeking ||
  436. (is_udplite && UDP_SKB_CB(skb)->partial_cov)) {
  437. checksum_valid = udp_skb_csum_unnecessary(skb) ||
  438. !__udp_lib_checksum_complete(skb);
  439. if (!checksum_valid)
  440. goto csum_copy_err;
  441. }
  442. if (checksum_valid || udp_skb_csum_unnecessary(skb)) {
  443. if (udp_skb_is_linear(skb))
  444. err = copy_linear_skb(skb, copied, off, &msg->msg_iter);
  445. else
  446. err = skb_copy_datagram_msg(skb, off, msg, copied);
  447. } else {
  448. err = skb_copy_and_csum_datagram_msg(skb, off, msg);
  449. if (err == -EINVAL)
  450. goto csum_copy_err;
  451. }
  452. if (unlikely(err)) {
  453. if (!peeking) {
  454. udp_drops_inc(sk);
  455. SNMP_INC_STATS(mib, UDP_MIB_INERRORS);
  456. }
  457. kfree_skb(skb);
  458. return err;
  459. }
  460. if (!peeking)
  461. SNMP_INC_STATS(mib, UDP_MIB_INDATAGRAMS);
  462. sock_recv_cmsgs(msg, sk, skb);
  463. /* Copy the address. */
  464. if (msg->msg_name) {
  465. DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
  466. sin6->sin6_family = AF_INET6;
  467. sin6->sin6_port = udp_hdr(skb)->source;
  468. sin6->sin6_flowinfo = 0;
  469. if (is_udp4) {
  470. ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
  471. &sin6->sin6_addr);
  472. sin6->sin6_scope_id = 0;
  473. } else {
  474. sin6->sin6_addr = ipv6_hdr(skb)->saddr;
  475. sin6->sin6_scope_id =
  476. ipv6_iface_scope_id(&sin6->sin6_addr,
  477. inet6_iif(skb));
  478. }
  479. *addr_len = sizeof(*sin6);
  480. BPF_CGROUP_RUN_PROG_UDP6_RECVMSG_LOCK(sk,
  481. (struct sockaddr *)sin6,
  482. addr_len);
  483. }
  484. if (udp_test_bit(GRO_ENABLED, sk))
  485. udp_cmsg_recv(msg, sk, skb);
  486. if (np->rxopt.all)
  487. ip6_datagram_recv_common_ctl(sk, msg, skb);
  488. if (is_udp4) {
  489. if (inet_cmsg_flags(inet))
  490. ip_cmsg_recv_offset(msg, sk, skb,
  491. sizeof(struct udphdr), off);
  492. } else {
  493. if (np->rxopt.all)
  494. ip6_datagram_recv_specific_ctl(sk, msg, skb);
  495. }
  496. err = copied;
  497. if (flags & MSG_TRUNC)
  498. err = ulen;
  499. skb_consume_udp(sk, skb, peeking ? -err : err);
  500. return err;
  501. csum_copy_err:
  502. if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags,
  503. udp_skb_destructor)) {
  504. SNMP_INC_STATS(mib, UDP_MIB_CSUMERRORS);
  505. SNMP_INC_STATS(mib, UDP_MIB_INERRORS);
  506. }
  507. kfree_skb_reason(skb, SKB_DROP_REASON_UDP_CSUM);
  508. /* starting over for a new packet, but check if we need to yield */
  509. cond_resched();
  510. msg->msg_flags &= ~MSG_TRUNC;
  511. goto try_again;
  512. }
  513. DECLARE_STATIC_KEY_FALSE(udpv6_encap_needed_key);
  514. void udpv6_encap_enable(void)
  515. {
  516. static_branch_inc(&udpv6_encap_needed_key);
  517. }
  518. EXPORT_SYMBOL(udpv6_encap_enable);
  519. /* Handler for tunnels with arbitrary destination ports: no socket lookup, go
  520. * through error handlers in encapsulations looking for a match.
  521. */
  522. static int __udp6_lib_err_encap_no_sk(struct sk_buff *skb,
  523. struct inet6_skb_parm *opt,
  524. u8 type, u8 code, int offset, __be32 info)
  525. {
  526. int i;
  527. for (i = 0; i < MAX_IPTUN_ENCAP_OPS; i++) {
  528. int (*handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
  529. u8 type, u8 code, int offset, __be32 info);
  530. const struct ip6_tnl_encap_ops *encap;
  531. encap = rcu_dereference(ip6tun_encaps[i]);
  532. if (!encap)
  533. continue;
  534. handler = encap->err_handler;
  535. if (handler && !handler(skb, opt, type, code, offset, info))
  536. return 0;
  537. }
  538. return -ENOENT;
  539. }
  540. /* Try to match ICMP errors to UDP tunnels by looking up a socket without
  541. * reversing source and destination port: this will match tunnels that force the
  542. * same destination port on both endpoints (e.g. VXLAN, GENEVE). Note that
  543. * lwtunnels might actually break this assumption by being configured with
  544. * different destination ports on endpoints, in this case we won't be able to
  545. * trace ICMP messages back to them.
  546. *
  547. * If this doesn't match any socket, probe tunnels with arbitrary destination
  548. * ports (e.g. FoU, GUE): there, the receiving socket is useless, as the port
  549. * we've sent packets to won't necessarily match the local destination port.
  550. *
  551. * Then ask the tunnel implementation to match the error against a valid
  552. * association.
  553. *
  554. * Return an error if we can't find a match, the socket if we need further
  555. * processing, zero otherwise.
  556. */
  557. static struct sock *__udp6_lib_err_encap(struct net *net,
  558. const struct ipv6hdr *hdr, int offset,
  559. struct udphdr *uh,
  560. struct udp_table *udptable,
  561. struct sock *sk,
  562. struct sk_buff *skb,
  563. struct inet6_skb_parm *opt,
  564. u8 type, u8 code, __be32 info)
  565. {
  566. int (*lookup)(struct sock *sk, struct sk_buff *skb);
  567. int network_offset, transport_offset;
  568. struct udp_sock *up;
  569. network_offset = skb_network_offset(skb);
  570. transport_offset = skb_transport_offset(skb);
  571. /* Network header needs to point to the outer IPv6 header inside ICMP */
  572. skb_reset_network_header(skb);
  573. /* Transport header needs to point to the UDP header */
  574. skb_set_transport_header(skb, offset);
  575. if (sk) {
  576. up = udp_sk(sk);
  577. lookup = READ_ONCE(up->encap_err_lookup);
  578. if (lookup && lookup(sk, skb))
  579. sk = NULL;
  580. goto out;
  581. }
  582. sk = __udp6_lib_lookup(net, &hdr->daddr, uh->source,
  583. &hdr->saddr, uh->dest,
  584. inet6_iif(skb), 0, udptable, skb);
  585. if (sk) {
  586. up = udp_sk(sk);
  587. lookup = READ_ONCE(up->encap_err_lookup);
  588. if (!lookup || lookup(sk, skb))
  589. sk = NULL;
  590. }
  591. out:
  592. if (!sk) {
  593. sk = ERR_PTR(__udp6_lib_err_encap_no_sk(skb, opt, type, code,
  594. offset, info));
  595. }
  596. skb_set_transport_header(skb, transport_offset);
  597. skb_set_network_header(skb, network_offset);
  598. return sk;
  599. }
  600. int __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  601. u8 type, u8 code, int offset, __be32 info,
  602. struct udp_table *udptable)
  603. {
  604. struct ipv6_pinfo *np;
  605. const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
  606. const struct in6_addr *saddr = &hdr->saddr;
  607. const struct in6_addr *daddr = seg6_get_daddr(skb, opt) ? : &hdr->daddr;
  608. struct udphdr *uh = (struct udphdr *)(skb->data+offset);
  609. bool tunnel = false;
  610. struct sock *sk;
  611. int harderr;
  612. int err;
  613. struct net *net = dev_net(skb->dev);
  614. sk = __udp6_lib_lookup(net, daddr, uh->dest, saddr, uh->source,
  615. inet6_iif(skb), inet6_sdif(skb), udptable, NULL);
  616. if (!sk || READ_ONCE(udp_sk(sk)->encap_type)) {
  617. /* No socket for error: try tunnels before discarding */
  618. if (static_branch_unlikely(&udpv6_encap_needed_key)) {
  619. sk = __udp6_lib_err_encap(net, hdr, offset, uh,
  620. udptable, sk, skb,
  621. opt, type, code, info);
  622. if (!sk)
  623. return 0;
  624. } else
  625. sk = ERR_PTR(-ENOENT);
  626. if (IS_ERR(sk)) {
  627. __ICMP6_INC_STATS(net, __in6_dev_get(skb->dev),
  628. ICMP6_MIB_INERRORS);
  629. return PTR_ERR(sk);
  630. }
  631. tunnel = true;
  632. }
  633. harderr = icmpv6_err_convert(type, code, &err);
  634. np = inet6_sk(sk);
  635. if (type == ICMPV6_PKT_TOOBIG) {
  636. if (!ip6_sk_accept_pmtu(sk))
  637. goto out;
  638. ip6_sk_update_pmtu(skb, sk, info);
  639. if (READ_ONCE(np->pmtudisc) != IPV6_PMTUDISC_DONT)
  640. harderr = 1;
  641. }
  642. if (type == NDISC_REDIRECT) {
  643. if (tunnel) {
  644. ip6_redirect(skb, sock_net(sk), inet6_iif(skb),
  645. READ_ONCE(sk->sk_mark),
  646. sk_uid(sk));
  647. } else {
  648. ip6_sk_redirect(skb, sk);
  649. }
  650. goto out;
  651. }
  652. /* Tunnels don't have an application socket: don't pass errors back */
  653. if (tunnel) {
  654. if (udp_sk(sk)->encap_err_rcv)
  655. udp_sk(sk)->encap_err_rcv(sk, skb, err, uh->dest,
  656. ntohl(info), (u8 *)(uh+1));
  657. goto out;
  658. }
  659. if (!inet6_test_bit(RECVERR6, sk)) {
  660. if (!harderr || sk->sk_state != TCP_ESTABLISHED)
  661. goto out;
  662. } else {
  663. ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
  664. }
  665. sk->sk_err = err;
  666. sk_error_report(sk);
  667. out:
  668. return 0;
  669. }
  670. static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  671. {
  672. int rc;
  673. if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
  674. sock_rps_save_rxhash(sk, skb);
  675. sk_mark_napi_id(sk, skb);
  676. sk_incoming_cpu_update(sk);
  677. } else {
  678. sk_mark_napi_id_once(sk, skb);
  679. }
  680. rc = __udp_enqueue_schedule_skb(sk, skb);
  681. if (rc < 0) {
  682. int is_udplite = IS_UDPLITE(sk);
  683. enum skb_drop_reason drop_reason;
  684. /* Note that an ENOMEM error is charged twice */
  685. if (rc == -ENOMEM) {
  686. UDP6_INC_STATS(sock_net(sk),
  687. UDP_MIB_RCVBUFERRORS, is_udplite);
  688. drop_reason = SKB_DROP_REASON_SOCKET_RCVBUFF;
  689. } else {
  690. UDP6_INC_STATS(sock_net(sk),
  691. UDP_MIB_MEMERRORS, is_udplite);
  692. drop_reason = SKB_DROP_REASON_PROTO_MEM;
  693. }
  694. UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  695. trace_udp_fail_queue_rcv_skb(rc, sk, skb);
  696. sk_skb_reason_drop(sk, skb, drop_reason);
  697. return -1;
  698. }
  699. return 0;
  700. }
  701. static __inline__ int udpv6_err(struct sk_buff *skb,
  702. struct inet6_skb_parm *opt, u8 type,
  703. u8 code, int offset, __be32 info)
  704. {
  705. return __udp6_lib_err(skb, opt, type, code, offset, info,
  706. dev_net(skb->dev)->ipv4.udp_table);
  707. }
  708. static int udpv6_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb)
  709. {
  710. enum skb_drop_reason drop_reason = SKB_DROP_REASON_NOT_SPECIFIED;
  711. struct udp_sock *up = udp_sk(sk);
  712. int is_udplite = IS_UDPLITE(sk);
  713. if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
  714. drop_reason = SKB_DROP_REASON_XFRM_POLICY;
  715. goto drop;
  716. }
  717. nf_reset_ct(skb);
  718. if (static_branch_unlikely(&udpv6_encap_needed_key) &&
  719. READ_ONCE(up->encap_type)) {
  720. int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
  721. /*
  722. * This is an encapsulation socket so pass the skb to
  723. * the socket's udp_encap_rcv() hook. Otherwise, just
  724. * fall through and pass this up the UDP socket.
  725. * up->encap_rcv() returns the following value:
  726. * =0 if skb was successfully passed to the encap
  727. * handler or was discarded by it.
  728. * >0 if skb should be passed on to UDP.
  729. * <0 if skb should be resubmitted as proto -N
  730. */
  731. /* if we're overly short, let UDP handle it */
  732. encap_rcv = READ_ONCE(up->encap_rcv);
  733. if (encap_rcv) {
  734. int ret;
  735. /* Verify checksum before giving to encap */
  736. if (udp_lib_checksum_complete(skb))
  737. goto csum_error;
  738. ret = encap_rcv(sk, skb);
  739. if (ret <= 0) {
  740. __UDP6_INC_STATS(sock_net(sk),
  741. UDP_MIB_INDATAGRAMS,
  742. is_udplite);
  743. return -ret;
  744. }
  745. }
  746. /* FALLTHROUGH -- it's a UDP Packet */
  747. }
  748. /*
  749. * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
  750. */
  751. if (unlikely(udp_test_bit(UDPLITE_RECV_CC, sk) &&
  752. UDP_SKB_CB(skb)->partial_cov)) {
  753. u16 pcrlen = READ_ONCE(up->pcrlen);
  754. if (pcrlen == 0) { /* full coverage was set */
  755. net_dbg_ratelimited("UDPLITE6: partial coverage %d while full coverage %d requested\n",
  756. UDP_SKB_CB(skb)->cscov, skb->len);
  757. goto drop;
  758. }
  759. if (UDP_SKB_CB(skb)->cscov < pcrlen) {
  760. net_dbg_ratelimited("UDPLITE6: coverage %d too small, need min %d\n",
  761. UDP_SKB_CB(skb)->cscov, pcrlen);
  762. goto drop;
  763. }
  764. }
  765. prefetch(&sk->sk_rmem_alloc);
  766. if (rcu_access_pointer(sk->sk_filter) &&
  767. udp_lib_checksum_complete(skb))
  768. goto csum_error;
  769. if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr), &drop_reason))
  770. goto drop;
  771. udp_csum_pull_header(skb);
  772. skb_dst_drop(skb);
  773. return __udpv6_queue_rcv_skb(sk, skb);
  774. csum_error:
  775. drop_reason = SKB_DROP_REASON_UDP_CSUM;
  776. __UDP6_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
  777. drop:
  778. __UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  779. udp_drops_inc(sk);
  780. sk_skb_reason_drop(sk, skb, drop_reason);
  781. return -1;
  782. }
  783. static int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  784. {
  785. struct sk_buff *next, *segs;
  786. int ret;
  787. if (likely(!udp_unexpected_gso(sk, skb)))
  788. return udpv6_queue_rcv_one_skb(sk, skb);
  789. __skb_push(skb, -skb_mac_offset(skb));
  790. segs = udp_rcv_segment(sk, skb, false);
  791. skb_list_walk_safe(segs, skb, next) {
  792. __skb_pull(skb, skb_transport_offset(skb));
  793. udp_post_segment_fix_csum(skb);
  794. ret = udpv6_queue_rcv_one_skb(sk, skb);
  795. if (ret > 0)
  796. ip6_protocol_deliver_rcu(dev_net(skb->dev), skb, ret,
  797. true);
  798. }
  799. return 0;
  800. }
  801. static bool __udp_v6_is_mcast_sock(struct net *net, const struct sock *sk,
  802. __be16 loc_port, const struct in6_addr *loc_addr,
  803. __be16 rmt_port, const struct in6_addr *rmt_addr,
  804. int dif, int sdif, unsigned short hnum)
  805. {
  806. const struct inet_sock *inet = inet_sk(sk);
  807. if (!net_eq(sock_net(sk), net))
  808. return false;
  809. if (udp_sk(sk)->udp_port_hash != hnum ||
  810. sk->sk_family != PF_INET6 ||
  811. (inet->inet_dport && inet->inet_dport != rmt_port) ||
  812. (!ipv6_addr_any(&sk->sk_v6_daddr) &&
  813. !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) ||
  814. !udp_sk_bound_dev_eq(net, READ_ONCE(sk->sk_bound_dev_if), dif, sdif) ||
  815. (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) &&
  816. !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr)))
  817. return false;
  818. if (!inet6_mc_check(sk, loc_addr, rmt_addr))
  819. return false;
  820. return true;
  821. }
  822. static void udp6_csum_zero_error(struct sk_buff *skb)
  823. {
  824. /* RFC 2460 section 8.1 says that we SHOULD log
  825. * this error. Well, it is reasonable.
  826. */
  827. net_dbg_ratelimited("IPv6: udp checksum is 0 for [%pI6c]:%u->[%pI6c]:%u\n",
  828. &ipv6_hdr(skb)->saddr, ntohs(udp_hdr(skb)->source),
  829. &ipv6_hdr(skb)->daddr, ntohs(udp_hdr(skb)->dest));
  830. }
  831. /*
  832. * Note: called only from the BH handler context,
  833. * so we don't need to lock the hashes.
  834. */
  835. static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
  836. const struct in6_addr *saddr, const struct in6_addr *daddr,
  837. struct udp_table *udptable, int proto)
  838. {
  839. struct sock *sk, *first = NULL;
  840. const struct udphdr *uh = udp_hdr(skb);
  841. unsigned short hnum = ntohs(uh->dest);
  842. struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum);
  843. unsigned int offset = offsetof(typeof(*sk), sk_node);
  844. unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10);
  845. int dif = inet6_iif(skb);
  846. int sdif = inet6_sdif(skb);
  847. struct hlist_node *node;
  848. struct sk_buff *nskb;
  849. if (use_hash2) {
  850. hash2_any = ipv6_portaddr_hash(net, &in6addr_any, hnum) &
  851. udptable->mask;
  852. hash2 = ipv6_portaddr_hash(net, daddr, hnum) & udptable->mask;
  853. start_lookup:
  854. hslot = &udptable->hash2[hash2].hslot;
  855. offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
  856. }
  857. sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) {
  858. if (!__udp_v6_is_mcast_sock(net, sk, uh->dest, daddr,
  859. uh->source, saddr, dif, sdif,
  860. hnum))
  861. continue;
  862. /* If zero checksum and no_check is not on for
  863. * the socket then skip it.
  864. */
  865. if (!uh->check && !udp_get_no_check6_rx(sk))
  866. continue;
  867. if (!first) {
  868. first = sk;
  869. continue;
  870. }
  871. nskb = skb_clone(skb, GFP_ATOMIC);
  872. if (unlikely(!nskb)) {
  873. udp_drops_inc(sk);
  874. __UDP6_INC_STATS(net, UDP_MIB_RCVBUFERRORS,
  875. IS_UDPLITE(sk));
  876. __UDP6_INC_STATS(net, UDP_MIB_INERRORS,
  877. IS_UDPLITE(sk));
  878. continue;
  879. }
  880. if (udpv6_queue_rcv_skb(sk, nskb) > 0)
  881. consume_skb(nskb);
  882. }
  883. /* Also lookup *:port if we are using hash2 and haven't done so yet. */
  884. if (use_hash2 && hash2 != hash2_any) {
  885. hash2 = hash2_any;
  886. goto start_lookup;
  887. }
  888. if (first) {
  889. if (udpv6_queue_rcv_skb(first, skb) > 0)
  890. consume_skb(skb);
  891. } else {
  892. kfree_skb(skb);
  893. __UDP6_INC_STATS(net, UDP_MIB_IGNOREDMULTI,
  894. proto == IPPROTO_UDPLITE);
  895. }
  896. return 0;
  897. }
  898. static void udp6_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst)
  899. {
  900. if (udp_sk_rx_dst_set(sk, dst))
  901. sk->sk_rx_dst_cookie = rt6_get_cookie(dst_rt6_info(dst));
  902. }
  903. /* wrapper for udp_queue_rcv_skb taking care of csum conversion and
  904. * return code conversion for ip layer consumption
  905. */
  906. static int udp6_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb,
  907. struct udphdr *uh)
  908. {
  909. int ret;
  910. if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
  911. skb_checksum_try_convert(skb, IPPROTO_UDP, ip6_compute_pseudo);
  912. ret = udpv6_queue_rcv_skb(sk, skb);
  913. /* a return value > 0 means to resubmit the input */
  914. if (ret > 0)
  915. return ret;
  916. return 0;
  917. }
  918. int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
  919. int proto)
  920. {
  921. enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED;
  922. const struct in6_addr *saddr, *daddr;
  923. struct net *net = dev_net(skb->dev);
  924. struct sock *sk = NULL;
  925. struct udphdr *uh;
  926. bool refcounted;
  927. u32 ulen = 0;
  928. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  929. goto discard;
  930. saddr = &ipv6_hdr(skb)->saddr;
  931. daddr = &ipv6_hdr(skb)->daddr;
  932. uh = udp_hdr(skb);
  933. ulen = ntohs(uh->len);
  934. if (ulen > skb->len)
  935. goto short_packet;
  936. if (proto == IPPROTO_UDP) {
  937. /* UDP validates ulen. */
  938. /* Check for jumbo payload */
  939. if (ulen == 0)
  940. ulen = skb->len;
  941. if (ulen < sizeof(*uh))
  942. goto short_packet;
  943. if (ulen < skb->len) {
  944. if (pskb_trim_rcsum(skb, ulen))
  945. goto short_packet;
  946. saddr = &ipv6_hdr(skb)->saddr;
  947. daddr = &ipv6_hdr(skb)->daddr;
  948. uh = udp_hdr(skb);
  949. }
  950. }
  951. if (udp6_csum_init(skb, uh, proto))
  952. goto csum_error;
  953. /* Check if the socket is already available, e.g. due to early demux */
  954. sk = inet6_steal_sock(net, skb, sizeof(struct udphdr), saddr, uh->source, daddr, uh->dest,
  955. &refcounted, udp6_ehashfn);
  956. if (IS_ERR(sk))
  957. goto no_sk;
  958. if (sk) {
  959. struct dst_entry *dst = skb_dst(skb);
  960. int ret;
  961. if (unlikely(rcu_dereference(sk->sk_rx_dst) != dst))
  962. udp6_sk_rx_dst_set(sk, dst);
  963. if (!uh->check && !udp_get_no_check6_rx(sk)) {
  964. if (refcounted)
  965. sock_put(sk);
  966. goto report_csum_error;
  967. }
  968. ret = udp6_unicast_rcv_skb(sk, skb, uh);
  969. if (refcounted)
  970. sock_put(sk);
  971. return ret;
  972. }
  973. /*
  974. * Multicast receive code
  975. */
  976. if (ipv6_addr_is_multicast(daddr))
  977. return __udp6_lib_mcast_deliver(net, skb,
  978. saddr, daddr, udptable, proto);
  979. /* Unicast */
  980. sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
  981. if (sk) {
  982. if (!uh->check && !udp_get_no_check6_rx(sk))
  983. goto report_csum_error;
  984. return udp6_unicast_rcv_skb(sk, skb, uh);
  985. }
  986. no_sk:
  987. reason = SKB_DROP_REASON_NO_SOCKET;
  988. if (!uh->check)
  989. goto report_csum_error;
  990. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  991. goto discard;
  992. nf_reset_ct(skb);
  993. if (udp_lib_checksum_complete(skb))
  994. goto csum_error;
  995. __UDP6_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
  996. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  997. sk_skb_reason_drop(sk, skb, reason);
  998. return 0;
  999. short_packet:
  1000. if (reason == SKB_DROP_REASON_NOT_SPECIFIED)
  1001. reason = SKB_DROP_REASON_PKT_TOO_SMALL;
  1002. net_dbg_ratelimited("UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
  1003. proto == IPPROTO_UDPLITE ? "-Lite" : "",
  1004. saddr, ntohs(uh->source),
  1005. ulen, skb->len,
  1006. daddr, ntohs(uh->dest));
  1007. goto discard;
  1008. report_csum_error:
  1009. udp6_csum_zero_error(skb);
  1010. csum_error:
  1011. if (reason == SKB_DROP_REASON_NOT_SPECIFIED)
  1012. reason = SKB_DROP_REASON_UDP_CSUM;
  1013. __UDP6_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
  1014. discard:
  1015. __UDP6_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
  1016. sk_skb_reason_drop(sk, skb, reason);
  1017. return 0;
  1018. }
  1019. static struct sock *__udp6_lib_demux_lookup(struct net *net,
  1020. __be16 loc_port, const struct in6_addr *loc_addr,
  1021. __be16 rmt_port, const struct in6_addr *rmt_addr,
  1022. int dif, int sdif)
  1023. {
  1024. struct udp_table *udptable = net->ipv4.udp_table;
  1025. unsigned short hnum = ntohs(loc_port);
  1026. struct udp_hslot *hslot2;
  1027. unsigned int hash2;
  1028. __portpair ports;
  1029. struct sock *sk;
  1030. hash2 = ipv6_portaddr_hash(net, loc_addr, hnum);
  1031. hslot2 = udp_hashslot2(udptable, hash2);
  1032. ports = INET_COMBINED_PORTS(rmt_port, hnum);
  1033. udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
  1034. if (sk->sk_state == TCP_ESTABLISHED &&
  1035. inet6_match(net, sk, rmt_addr, loc_addr, ports, dif, sdif))
  1036. return sk;
  1037. /* Only check first socket in chain */
  1038. break;
  1039. }
  1040. return NULL;
  1041. }
  1042. void udp_v6_early_demux(struct sk_buff *skb)
  1043. {
  1044. struct net *net = dev_net(skb->dev);
  1045. const struct udphdr *uh;
  1046. struct sock *sk;
  1047. struct dst_entry *dst;
  1048. int dif = skb->dev->ifindex;
  1049. int sdif = inet6_sdif(skb);
  1050. if (!pskb_may_pull(skb, skb_transport_offset(skb) +
  1051. sizeof(struct udphdr)))
  1052. return;
  1053. uh = udp_hdr(skb);
  1054. if (skb->pkt_type == PACKET_HOST)
  1055. sk = __udp6_lib_demux_lookup(net, uh->dest,
  1056. &ipv6_hdr(skb)->daddr,
  1057. uh->source, &ipv6_hdr(skb)->saddr,
  1058. dif, sdif);
  1059. else
  1060. return;
  1061. if (!sk)
  1062. return;
  1063. skb->sk = sk;
  1064. DEBUG_NET_WARN_ON_ONCE(sk_is_refcounted(sk));
  1065. skb->destructor = sock_pfree;
  1066. dst = rcu_dereference(sk->sk_rx_dst);
  1067. if (dst)
  1068. dst = dst_check(dst, sk->sk_rx_dst_cookie);
  1069. if (dst) {
  1070. /* set noref for now.
  1071. * any place which wants to hold dst has to call
  1072. * dst_hold_safe()
  1073. */
  1074. skb_dst_set_noref(skb, dst);
  1075. }
  1076. }
  1077. INDIRECT_CALLABLE_SCOPE int udpv6_rcv(struct sk_buff *skb)
  1078. {
  1079. return __udp6_lib_rcv(skb, dev_net(skb->dev)->ipv4.udp_table, IPPROTO_UDP);
  1080. }
  1081. /*
  1082. * Throw away all pending data and cancel the corking. Socket is locked.
  1083. */
  1084. static void udp_v6_flush_pending_frames(struct sock *sk)
  1085. {
  1086. struct udp_sock *up = udp_sk(sk);
  1087. if (up->pending == AF_INET)
  1088. udp_flush_pending_frames(sk);
  1089. else if (up->pending) {
  1090. up->len = 0;
  1091. WRITE_ONCE(up->pending, 0);
  1092. ip6_flush_pending_frames(sk);
  1093. }
  1094. }
  1095. static int udpv6_pre_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
  1096. int addr_len)
  1097. {
  1098. if (addr_len < offsetofend(struct sockaddr, sa_family))
  1099. return -EINVAL;
  1100. /* The following checks are replicated from __ip6_datagram_connect()
  1101. * and intended to prevent BPF program called below from accessing
  1102. * bytes that are out of the bound specified by user in addr_len.
  1103. */
  1104. if (uaddr->sa_family == AF_INET) {
  1105. if (ipv6_only_sock(sk))
  1106. return -EAFNOSUPPORT;
  1107. return udp_pre_connect(sk, uaddr, addr_len);
  1108. }
  1109. if (addr_len < SIN6_LEN_RFC2133)
  1110. return -EINVAL;
  1111. return BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr, &addr_len);
  1112. }
  1113. static int udpv6_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
  1114. int addr_len)
  1115. {
  1116. int res;
  1117. lock_sock(sk);
  1118. res = __ip6_datagram_connect(sk, uaddr, addr_len);
  1119. if (!res)
  1120. udp6_hash4(sk);
  1121. release_sock(sk);
  1122. return res;
  1123. }
  1124. /**
  1125. * udp6_hwcsum_outgoing - handle outgoing HW checksumming
  1126. * @sk: socket we are sending on
  1127. * @skb: sk_buff containing the filled-in UDP header
  1128. * (checksum field must be zeroed out)
  1129. * @saddr: source address
  1130. * @daddr: destination address
  1131. * @len: length of packet
  1132. */
  1133. static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
  1134. const struct in6_addr *saddr,
  1135. const struct in6_addr *daddr, int len)
  1136. {
  1137. unsigned int offset;
  1138. struct udphdr *uh = udp_hdr(skb);
  1139. struct sk_buff *frags = skb_shinfo(skb)->frag_list;
  1140. __wsum csum = 0;
  1141. if (!frags) {
  1142. /* Only one fragment on the socket. */
  1143. skb->csum_start = skb_transport_header(skb) - skb->head;
  1144. skb->csum_offset = offsetof(struct udphdr, check);
  1145. uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
  1146. } else {
  1147. /*
  1148. * HW-checksum won't work as there are two or more
  1149. * fragments on the socket so that all csums of sk_buffs
  1150. * should be together
  1151. */
  1152. offset = skb_transport_offset(skb);
  1153. skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
  1154. csum = skb->csum;
  1155. skb->ip_summed = CHECKSUM_NONE;
  1156. do {
  1157. csum = csum_add(csum, frags->csum);
  1158. } while ((frags = frags->next));
  1159. uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
  1160. csum);
  1161. if (uh->check == 0)
  1162. uh->check = CSUM_MANGLED_0;
  1163. }
  1164. }
  1165. /*
  1166. * Sending
  1167. */
  1168. static int udp_v6_send_skb(struct sk_buff *skb, struct flowi6 *fl6,
  1169. struct inet_cork *cork)
  1170. {
  1171. struct sock *sk = skb->sk;
  1172. struct udphdr *uh;
  1173. int err = 0;
  1174. int is_udplite = IS_UDPLITE(sk);
  1175. __wsum csum = 0;
  1176. int offset = skb_transport_offset(skb);
  1177. int len = skb->len - offset;
  1178. int datalen = len - sizeof(*uh);
  1179. /*
  1180. * Create a UDP header
  1181. */
  1182. uh = udp_hdr(skb);
  1183. uh->source = fl6->fl6_sport;
  1184. uh->dest = fl6->fl6_dport;
  1185. uh->len = htons(len);
  1186. uh->check = 0;
  1187. if (cork->gso_size) {
  1188. const int hlen = skb_network_header_len(skb) +
  1189. sizeof(struct udphdr);
  1190. if (hlen + min(datalen, cork->gso_size) > cork->fragsize) {
  1191. kfree_skb(skb);
  1192. return -EMSGSIZE;
  1193. }
  1194. if (datalen > cork->gso_size * UDP_MAX_SEGMENTS) {
  1195. kfree_skb(skb);
  1196. return -EINVAL;
  1197. }
  1198. if (udp_get_no_check6_tx(sk)) {
  1199. kfree_skb(skb);
  1200. return -EINVAL;
  1201. }
  1202. if (is_udplite || dst_xfrm(skb_dst(skb))) {
  1203. kfree_skb(skb);
  1204. return -EIO;
  1205. }
  1206. if (datalen > cork->gso_size) {
  1207. skb_shinfo(skb)->gso_size = cork->gso_size;
  1208. skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4;
  1209. skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(datalen,
  1210. cork->gso_size);
  1211. /* Don't checksum the payload, skb will get segmented */
  1212. goto csum_partial;
  1213. }
  1214. }
  1215. if (is_udplite)
  1216. csum = udplite_csum(skb);
  1217. else if (udp_get_no_check6_tx(sk)) { /* UDP csum disabled */
  1218. skb->ip_summed = CHECKSUM_NONE;
  1219. goto send;
  1220. } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
  1221. csum_partial:
  1222. udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, len);
  1223. goto send;
  1224. } else
  1225. csum = udp_csum(skb);
  1226. /* add protocol-dependent pseudo-header */
  1227. uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
  1228. len, fl6->flowi6_proto, csum);
  1229. if (uh->check == 0)
  1230. uh->check = CSUM_MANGLED_0;
  1231. send:
  1232. err = ip6_send_skb(skb);
  1233. if (unlikely(err)) {
  1234. if (err == -ENOBUFS && !inet6_test_bit(RECVERR6, sk)) {
  1235. UDP6_INC_STATS(sock_net(sk),
  1236. UDP_MIB_SNDBUFERRORS, is_udplite);
  1237. err = 0;
  1238. }
  1239. } else {
  1240. UDP6_INC_STATS(sock_net(sk),
  1241. UDP_MIB_OUTDATAGRAMS, is_udplite);
  1242. }
  1243. return err;
  1244. }
  1245. static int udp_v6_push_pending_frames(struct sock *sk)
  1246. {
  1247. struct sk_buff *skb;
  1248. struct udp_sock *up = udp_sk(sk);
  1249. int err = 0;
  1250. if (up->pending == AF_INET)
  1251. return udp_push_pending_frames(sk);
  1252. skb = ip6_finish_skb(sk);
  1253. if (!skb)
  1254. goto out;
  1255. err = udp_v6_send_skb(skb, &inet_sk(sk)->cork.fl.u.ip6,
  1256. &inet_sk(sk)->cork.base);
  1257. out:
  1258. up->len = 0;
  1259. WRITE_ONCE(up->pending, 0);
  1260. return err;
  1261. }
  1262. int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
  1263. {
  1264. struct ipv6_txoptions opt_space;
  1265. struct udp_sock *up = udp_sk(sk);
  1266. struct inet_sock *inet = inet_sk(sk);
  1267. struct ipv6_pinfo *np = inet6_sk(sk);
  1268. DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
  1269. struct in6_addr *daddr, *final_p, final;
  1270. struct ipv6_txoptions *opt = NULL;
  1271. struct ipv6_txoptions *opt_to_free = NULL;
  1272. struct ip6_flowlabel *flowlabel = NULL;
  1273. struct inet_cork_full cork;
  1274. struct flowi6 *fl6 = &cork.fl.u.ip6;
  1275. struct dst_entry *dst;
  1276. struct ipcm6_cookie ipc6;
  1277. int addr_len = msg->msg_namelen;
  1278. bool connected = false;
  1279. int ulen = len;
  1280. int corkreq = udp_test_bit(CORK, sk) || msg->msg_flags & MSG_MORE;
  1281. int err;
  1282. int is_udplite = IS_UDPLITE(sk);
  1283. int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
  1284. ipcm6_init_sk(&ipc6, sk);
  1285. ipc6.gso_size = READ_ONCE(up->gso_size);
  1286. /* destination address check */
  1287. if (sin6) {
  1288. if (addr_len < offsetof(struct sockaddr, sa_data))
  1289. return -EINVAL;
  1290. switch (sin6->sin6_family) {
  1291. case AF_INET6:
  1292. if (addr_len < SIN6_LEN_RFC2133)
  1293. return -EINVAL;
  1294. daddr = &sin6->sin6_addr;
  1295. if (ipv6_addr_any(daddr) &&
  1296. ipv6_addr_v4mapped(&np->saddr))
  1297. ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK),
  1298. daddr);
  1299. break;
  1300. case AF_INET:
  1301. goto do_udp_sendmsg;
  1302. case AF_UNSPEC:
  1303. msg->msg_name = sin6 = NULL;
  1304. msg->msg_namelen = addr_len = 0;
  1305. daddr = NULL;
  1306. break;
  1307. default:
  1308. return -EINVAL;
  1309. }
  1310. } else if (!READ_ONCE(up->pending)) {
  1311. if (sk->sk_state != TCP_ESTABLISHED)
  1312. return -EDESTADDRREQ;
  1313. daddr = &sk->sk_v6_daddr;
  1314. } else
  1315. daddr = NULL;
  1316. if (daddr) {
  1317. if (ipv6_addr_v4mapped(daddr)) {
  1318. struct sockaddr_in sin;
  1319. sin.sin_family = AF_INET;
  1320. sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
  1321. sin.sin_addr.s_addr = daddr->s6_addr32[3];
  1322. msg->msg_name = &sin;
  1323. msg->msg_namelen = sizeof(sin);
  1324. do_udp_sendmsg:
  1325. err = ipv6_only_sock(sk) ?
  1326. -ENETUNREACH : udp_sendmsg(sk, msg, len);
  1327. msg->msg_name = sin6;
  1328. msg->msg_namelen = addr_len;
  1329. return err;
  1330. }
  1331. }
  1332. /* Rough check on arithmetic overflow,
  1333. better check is made in ip6_append_data().
  1334. */
  1335. if (len > INT_MAX - sizeof(struct udphdr))
  1336. return -EMSGSIZE;
  1337. getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
  1338. if (READ_ONCE(up->pending)) {
  1339. if (READ_ONCE(up->pending) == AF_INET)
  1340. return udp_sendmsg(sk, msg, len);
  1341. /*
  1342. * There are pending frames.
  1343. * The socket lock must be held while it's corked.
  1344. */
  1345. lock_sock(sk);
  1346. if (likely(up->pending)) {
  1347. if (unlikely(up->pending != AF_INET6)) {
  1348. release_sock(sk);
  1349. return -EAFNOSUPPORT;
  1350. }
  1351. dst = NULL;
  1352. goto do_append_data;
  1353. }
  1354. release_sock(sk);
  1355. }
  1356. ulen += sizeof(struct udphdr);
  1357. memset(fl6, 0, sizeof(*fl6));
  1358. if (sin6) {
  1359. if (sin6->sin6_port == 0)
  1360. return -EINVAL;
  1361. fl6->fl6_dport = sin6->sin6_port;
  1362. daddr = &sin6->sin6_addr;
  1363. if (inet6_test_bit(SNDFLOW, sk)) {
  1364. fl6->flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
  1365. if (fl6->flowlabel & IPV6_FLOWLABEL_MASK) {
  1366. flowlabel = fl6_sock_lookup(sk, fl6->flowlabel);
  1367. if (IS_ERR(flowlabel))
  1368. return -EINVAL;
  1369. }
  1370. }
  1371. /*
  1372. * Otherwise it will be difficult to maintain
  1373. * sk->sk_dst_cache.
  1374. */
  1375. if (sk->sk_state == TCP_ESTABLISHED &&
  1376. ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
  1377. daddr = &sk->sk_v6_daddr;
  1378. if (addr_len >= sizeof(struct sockaddr_in6) &&
  1379. sin6->sin6_scope_id &&
  1380. __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
  1381. fl6->flowi6_oif = sin6->sin6_scope_id;
  1382. } else {
  1383. if (sk->sk_state != TCP_ESTABLISHED)
  1384. return -EDESTADDRREQ;
  1385. fl6->fl6_dport = inet->inet_dport;
  1386. daddr = &sk->sk_v6_daddr;
  1387. fl6->flowlabel = np->flow_label;
  1388. connected = true;
  1389. }
  1390. if (!fl6->flowi6_oif)
  1391. fl6->flowi6_oif = READ_ONCE(sk->sk_bound_dev_if);
  1392. if (!fl6->flowi6_oif)
  1393. fl6->flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
  1394. fl6->flowi6_uid = sk_uid(sk);
  1395. if (msg->msg_controllen) {
  1396. opt = &opt_space;
  1397. memset(opt, 0, sizeof(struct ipv6_txoptions));
  1398. opt->tot_len = sizeof(*opt);
  1399. ipc6.opt = opt;
  1400. err = udp_cmsg_send(sk, msg, &ipc6.gso_size);
  1401. if (err > 0) {
  1402. err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, fl6,
  1403. &ipc6);
  1404. connected = false;
  1405. }
  1406. if (err < 0) {
  1407. fl6_sock_release(flowlabel);
  1408. return err;
  1409. }
  1410. if ((fl6->flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
  1411. flowlabel = fl6_sock_lookup(sk, fl6->flowlabel);
  1412. if (IS_ERR(flowlabel))
  1413. return -EINVAL;
  1414. }
  1415. if (!(opt->opt_nflen|opt->opt_flen))
  1416. opt = NULL;
  1417. }
  1418. if (!opt) {
  1419. opt = txopt_get(np);
  1420. opt_to_free = opt;
  1421. }
  1422. if (flowlabel)
  1423. opt = fl6_merge_options(&opt_space, flowlabel, opt);
  1424. opt = ipv6_fixup_options(&opt_space, opt);
  1425. ipc6.opt = opt;
  1426. fl6->flowi6_proto = sk->sk_protocol;
  1427. fl6->flowi6_mark = ipc6.sockc.mark;
  1428. fl6->daddr = *daddr;
  1429. if (ipv6_addr_any(&fl6->saddr) && !ipv6_addr_any(&np->saddr))
  1430. fl6->saddr = np->saddr;
  1431. fl6->fl6_sport = inet->inet_sport;
  1432. if (cgroup_bpf_enabled(CGROUP_UDP6_SENDMSG) && !connected) {
  1433. err = BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk,
  1434. (struct sockaddr *)sin6,
  1435. &addr_len,
  1436. &fl6->saddr);
  1437. if (err)
  1438. goto out_no_dst;
  1439. if (sin6) {
  1440. if (ipv6_addr_v4mapped(&sin6->sin6_addr)) {
  1441. /* BPF program rewrote IPv6-only by IPv4-mapped
  1442. * IPv6. It's currently unsupported.
  1443. */
  1444. err = -ENOTSUPP;
  1445. goto out_no_dst;
  1446. }
  1447. if (sin6->sin6_port == 0) {
  1448. /* BPF program set invalid port. Reject it. */
  1449. err = -EINVAL;
  1450. goto out_no_dst;
  1451. }
  1452. fl6->fl6_dport = sin6->sin6_port;
  1453. fl6->daddr = sin6->sin6_addr;
  1454. }
  1455. }
  1456. if (ipv6_addr_any(&fl6->daddr))
  1457. fl6->daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
  1458. final_p = fl6_update_dst(fl6, opt, &final);
  1459. if (final_p)
  1460. connected = false;
  1461. if (!fl6->flowi6_oif && ipv6_addr_is_multicast(&fl6->daddr)) {
  1462. fl6->flowi6_oif = READ_ONCE(np->mcast_oif);
  1463. connected = false;
  1464. } else if (!fl6->flowi6_oif)
  1465. fl6->flowi6_oif = READ_ONCE(np->ucast_oif);
  1466. security_sk_classify_flow(sk, flowi6_to_flowi_common(fl6));
  1467. fl6->flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6->flowlabel);
  1468. dst = ip6_sk_dst_lookup_flow(sk, fl6, final_p, connected);
  1469. if (IS_ERR(dst)) {
  1470. err = PTR_ERR(dst);
  1471. dst = NULL;
  1472. goto out;
  1473. }
  1474. if (ipc6.hlimit < 0)
  1475. ipc6.hlimit = ip6_sk_dst_hoplimit(np, fl6, dst);
  1476. if (msg->msg_flags&MSG_CONFIRM)
  1477. goto do_confirm;
  1478. back_from_confirm:
  1479. /* Lockless fast path for the non-corking case */
  1480. if (!corkreq) {
  1481. struct sk_buff *skb;
  1482. skb = ip6_make_skb(sk, getfrag, msg, ulen,
  1483. sizeof(struct udphdr), &ipc6,
  1484. dst_rt6_info(dst),
  1485. msg->msg_flags, &cork);
  1486. err = PTR_ERR(skb);
  1487. if (!IS_ERR_OR_NULL(skb))
  1488. err = udp_v6_send_skb(skb, fl6, &cork.base);
  1489. /* ip6_make_skb steals dst reference */
  1490. goto out_no_dst;
  1491. }
  1492. lock_sock(sk);
  1493. if (unlikely(up->pending)) {
  1494. /* The socket is already corked while preparing it. */
  1495. /* ... which is an evident application bug. --ANK */
  1496. release_sock(sk);
  1497. net_dbg_ratelimited("udp cork app bug 2\n");
  1498. err = -EINVAL;
  1499. goto out;
  1500. }
  1501. WRITE_ONCE(up->pending, AF_INET6);
  1502. do_append_data:
  1503. up->len += ulen;
  1504. err = ip6_append_data(sk, getfrag, msg, ulen, sizeof(struct udphdr),
  1505. &ipc6, fl6, dst_rt6_info(dst),
  1506. corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
  1507. if (err)
  1508. udp_v6_flush_pending_frames(sk);
  1509. else if (!corkreq)
  1510. err = udp_v6_push_pending_frames(sk);
  1511. else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
  1512. WRITE_ONCE(up->pending, 0);
  1513. if (err > 0)
  1514. err = inet6_test_bit(RECVERR6, sk) ? net_xmit_errno(err) : 0;
  1515. release_sock(sk);
  1516. out:
  1517. dst_release(dst);
  1518. out_no_dst:
  1519. fl6_sock_release(flowlabel);
  1520. txopt_put(opt_to_free);
  1521. if (!err)
  1522. return len;
  1523. /*
  1524. * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
  1525. * ENOBUFS might not be good (it's not tunable per se), but otherwise
  1526. * we don't have a good statistic (IpOutDiscards but it can be too many
  1527. * things). We could add another new stat but at least for now that
  1528. * seems like overkill.
  1529. */
  1530. if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
  1531. UDP6_INC_STATS(sock_net(sk),
  1532. UDP_MIB_SNDBUFERRORS, is_udplite);
  1533. }
  1534. return err;
  1535. do_confirm:
  1536. if (msg->msg_flags & MSG_PROBE)
  1537. dst_confirm_neigh(dst, &fl6->daddr);
  1538. if (!(msg->msg_flags&MSG_PROBE) || len)
  1539. goto back_from_confirm;
  1540. err = 0;
  1541. goto out;
  1542. }
  1543. EXPORT_SYMBOL(udpv6_sendmsg);
  1544. static void udpv6_splice_eof(struct socket *sock)
  1545. {
  1546. struct sock *sk = sock->sk;
  1547. struct udp_sock *up = udp_sk(sk);
  1548. if (!READ_ONCE(up->pending) || udp_test_bit(CORK, sk))
  1549. return;
  1550. lock_sock(sk);
  1551. if (up->pending && !udp_test_bit(CORK, sk))
  1552. udp_v6_push_pending_frames(sk);
  1553. release_sock(sk);
  1554. }
  1555. void udpv6_destroy_sock(struct sock *sk)
  1556. {
  1557. struct udp_sock *up = udp_sk(sk);
  1558. lock_sock(sk);
  1559. /* protects from races with udp_abort() */
  1560. sock_set_flag(sk, SOCK_DEAD);
  1561. udp_v6_flush_pending_frames(sk);
  1562. release_sock(sk);
  1563. if (static_branch_unlikely(&udpv6_encap_needed_key)) {
  1564. if (up->encap_type) {
  1565. void (*encap_destroy)(struct sock *sk);
  1566. encap_destroy = READ_ONCE(up->encap_destroy);
  1567. if (encap_destroy)
  1568. encap_destroy(sk);
  1569. }
  1570. if (udp_test_bit(ENCAP_ENABLED, sk)) {
  1571. static_branch_dec(&udpv6_encap_needed_key);
  1572. udp_encap_disable();
  1573. udp_tunnel_cleanup_gro(sk);
  1574. }
  1575. }
  1576. }
  1577. /*
  1578. * Socket option code for UDP
  1579. */
  1580. int udpv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
  1581. unsigned int optlen)
  1582. {
  1583. if (level == SOL_UDP || level == SOL_UDPLITE || level == SOL_SOCKET)
  1584. return udp_lib_setsockopt(sk, level, optname,
  1585. optval, optlen,
  1586. udp_v6_push_pending_frames);
  1587. return ipv6_setsockopt(sk, level, optname, optval, optlen);
  1588. }
  1589. int udpv6_getsockopt(struct sock *sk, int level, int optname,
  1590. char __user *optval, int __user *optlen)
  1591. {
  1592. if (level == SOL_UDP || level == SOL_UDPLITE)
  1593. return udp_lib_getsockopt(sk, level, optname, optval, optlen);
  1594. return ipv6_getsockopt(sk, level, optname, optval, optlen);
  1595. }
  1596. /* ------------------------------------------------------------------------ */
  1597. #ifdef CONFIG_PROC_FS
  1598. int udp6_seq_show(struct seq_file *seq, void *v)
  1599. {
  1600. if (v == SEQ_START_TOKEN) {
  1601. seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
  1602. } else {
  1603. int bucket = ((struct udp_iter_state *)seq->private)->bucket;
  1604. const struct inet_sock *inet = inet_sk((const struct sock *)v);
  1605. __u16 srcp = ntohs(inet->inet_sport);
  1606. __u16 destp = ntohs(inet->inet_dport);
  1607. __ip6_dgram_sock_seq_show(seq, v, srcp, destp,
  1608. udp_rqueue_get(v), bucket);
  1609. }
  1610. return 0;
  1611. }
  1612. const struct seq_operations udp6_seq_ops = {
  1613. .start = udp_seq_start,
  1614. .next = udp_seq_next,
  1615. .stop = udp_seq_stop,
  1616. .show = udp6_seq_show,
  1617. };
  1618. EXPORT_SYMBOL(udp6_seq_ops);
  1619. static struct udp_seq_afinfo udp6_seq_afinfo = {
  1620. .family = AF_INET6,
  1621. .udp_table = NULL,
  1622. };
  1623. int __net_init udp6_proc_init(struct net *net)
  1624. {
  1625. if (!proc_create_net_data("udp6", 0444, net->proc_net, &udp6_seq_ops,
  1626. sizeof(struct udp_iter_state), &udp6_seq_afinfo))
  1627. return -ENOMEM;
  1628. return 0;
  1629. }
  1630. void udp6_proc_exit(struct net *net)
  1631. {
  1632. remove_proc_entry("udp6", net->proc_net);
  1633. }
  1634. #endif /* CONFIG_PROC_FS */
  1635. /* ------------------------------------------------------------------------ */
  1636. struct proto udpv6_prot = {
  1637. .name = "UDPv6",
  1638. .owner = THIS_MODULE,
  1639. .close = udp_lib_close,
  1640. .pre_connect = udpv6_pre_connect,
  1641. .connect = udpv6_connect,
  1642. .disconnect = udp_disconnect,
  1643. .ioctl = udp_ioctl,
  1644. .init = udpv6_init_sock,
  1645. .destroy = udpv6_destroy_sock,
  1646. .setsockopt = udpv6_setsockopt,
  1647. .getsockopt = udpv6_getsockopt,
  1648. .sendmsg = udpv6_sendmsg,
  1649. .recvmsg = udpv6_recvmsg,
  1650. .splice_eof = udpv6_splice_eof,
  1651. .release_cb = ip6_datagram_release_cb,
  1652. .hash = udp_lib_hash,
  1653. .unhash = udp_lib_unhash,
  1654. .rehash = udp_v6_rehash,
  1655. .get_port = udp_v6_get_port,
  1656. .put_port = udp_lib_unhash,
  1657. #ifdef CONFIG_BPF_SYSCALL
  1658. .psock_update_sk_prot = udp_bpf_update_proto,
  1659. #endif
  1660. .memory_allocated = &net_aligned_data.udp_memory_allocated,
  1661. .per_cpu_fw_alloc = &udp_memory_per_cpu_fw_alloc,
  1662. .sysctl_mem = sysctl_udp_mem,
  1663. .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_udp_wmem_min),
  1664. .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_udp_rmem_min),
  1665. .obj_size = sizeof(struct udp6_sock),
  1666. .ipv6_pinfo_offset = offsetof(struct udp6_sock, inet6),
  1667. .h.udp_table = NULL,
  1668. .diag_destroy = udp_abort,
  1669. };
  1670. static struct inet_protosw udpv6_protosw = {
  1671. .type = SOCK_DGRAM,
  1672. .protocol = IPPROTO_UDP,
  1673. .prot = &udpv6_prot,
  1674. .ops = &inet6_dgram_ops,
  1675. .flags = INET_PROTOSW_PERMANENT,
  1676. };
  1677. int __init udpv6_init(void)
  1678. {
  1679. int ret;
  1680. net_hotdata.udpv6_protocol = (struct inet6_protocol) {
  1681. .handler = udpv6_rcv,
  1682. .err_handler = udpv6_err,
  1683. .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
  1684. };
  1685. ret = inet6_add_protocol(&net_hotdata.udpv6_protocol, IPPROTO_UDP);
  1686. if (ret)
  1687. goto out;
  1688. ret = inet6_register_protosw(&udpv6_protosw);
  1689. if (ret)
  1690. goto out_udpv6_protocol;
  1691. out:
  1692. return ret;
  1693. out_udpv6_protocol:
  1694. inet6_del_protocol(&net_hotdata.udpv6_protocol, IPPROTO_UDP);
  1695. goto out;
  1696. }
  1697. void udpv6_exit(void)
  1698. {
  1699. inet6_unregister_protosw(&udpv6_protosw);
  1700. inet6_del_protocol(&net_hotdata.udpv6_protocol, IPPROTO_UDP);
  1701. }