udp.c 48 KB

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