udp.c 40 KB

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