udp.c 37 KB

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