udp.c 40 KB

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