udp.c 39 KB

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