udp.c 39 KB

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