udp.c 37 KB

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