udp.c 39 KB

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