udp.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568
  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. np = inet6_sk(sk);
  422. if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
  423. goto out;
  424. if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
  425. goto out;
  426. if (np->recverr)
  427. ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
  428. sk->sk_err = err;
  429. sk->sk_error_report(sk);
  430. out:
  431. sock_put(sk);
  432. }
  433. static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  434. {
  435. int rc;
  436. if (!ipv6_addr_any(&inet6_sk(sk)->daddr))
  437. sock_rps_save_rxhash(sk, skb);
  438. rc = sock_queue_rcv_skb(sk, skb);
  439. if (rc < 0) {
  440. int is_udplite = IS_UDPLITE(sk);
  441. /* Note that an ENOMEM error is charged twice */
  442. if (rc == -ENOMEM)
  443. UDP6_INC_STATS_BH(sock_net(sk),
  444. UDP_MIB_RCVBUFERRORS, is_udplite);
  445. UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  446. kfree_skb(skb);
  447. return -1;
  448. }
  449. return 0;
  450. }
  451. static __inline__ void udpv6_err(struct sk_buff *skb,
  452. struct inet6_skb_parm *opt, u8 type,
  453. u8 code, int offset, __be32 info )
  454. {
  455. __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
  456. }
  457. static struct static_key udpv6_encap_needed __read_mostly;
  458. void udpv6_encap_enable(void)
  459. {
  460. if (!static_key_enabled(&udpv6_encap_needed))
  461. static_key_slow_inc(&udpv6_encap_needed);
  462. }
  463. EXPORT_SYMBOL(udpv6_encap_enable);
  464. int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  465. {
  466. struct udp_sock *up = udp_sk(sk);
  467. int rc;
  468. int is_udplite = IS_UDPLITE(sk);
  469. if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
  470. goto drop;
  471. if (static_key_false(&udpv6_encap_needed) && up->encap_type) {
  472. int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
  473. /*
  474. * This is an encapsulation socket so pass the skb to
  475. * the socket's udp_encap_rcv() hook. Otherwise, just
  476. * fall through and pass this up the UDP socket.
  477. * up->encap_rcv() returns the following value:
  478. * =0 if skb was successfully passed to the encap
  479. * handler or was discarded by it.
  480. * >0 if skb should be passed on to UDP.
  481. * <0 if skb should be resubmitted as proto -N
  482. */
  483. /* if we're overly short, let UDP handle it */
  484. encap_rcv = ACCESS_ONCE(up->encap_rcv);
  485. if (skb->len > sizeof(struct udphdr) && encap_rcv != NULL) {
  486. int ret;
  487. ret = encap_rcv(sk, skb);
  488. if (ret <= 0) {
  489. UDP_INC_STATS_BH(sock_net(sk),
  490. UDP_MIB_INDATAGRAMS,
  491. is_udplite);
  492. return -ret;
  493. }
  494. }
  495. /* FALLTHROUGH -- it's a UDP Packet */
  496. }
  497. /*
  498. * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
  499. */
  500. if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
  501. if (up->pcrlen == 0) { /* full coverage was set */
  502. LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: partial coverage"
  503. " %d while full coverage %d requested\n",
  504. UDP_SKB_CB(skb)->cscov, skb->len);
  505. goto drop;
  506. }
  507. if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
  508. LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: coverage %d "
  509. "too small, need min %d\n",
  510. UDP_SKB_CB(skb)->cscov, up->pcrlen);
  511. goto drop;
  512. }
  513. }
  514. if (rcu_access_pointer(sk->sk_filter)) {
  515. if (udp_lib_checksum_complete(skb))
  516. goto drop;
  517. }
  518. if (sk_rcvqueues_full(sk, skb, sk->sk_rcvbuf))
  519. goto drop;
  520. skb_dst_drop(skb);
  521. bh_lock_sock(sk);
  522. rc = 0;
  523. if (!sock_owned_by_user(sk))
  524. rc = __udpv6_queue_rcv_skb(sk, skb);
  525. else if (sk_add_backlog(sk, skb, sk->sk_rcvbuf)) {
  526. bh_unlock_sock(sk);
  527. goto drop;
  528. }
  529. bh_unlock_sock(sk);
  530. return rc;
  531. drop:
  532. UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  533. atomic_inc(&sk->sk_drops);
  534. kfree_skb(skb);
  535. return -1;
  536. }
  537. static struct sock *udp_v6_mcast_next(struct net *net, struct sock *sk,
  538. __be16 loc_port, const struct in6_addr *loc_addr,
  539. __be16 rmt_port, const struct in6_addr *rmt_addr,
  540. int dif)
  541. {
  542. struct hlist_nulls_node *node;
  543. struct sock *s = sk;
  544. unsigned short num = ntohs(loc_port);
  545. sk_nulls_for_each_from(s, node) {
  546. struct inet_sock *inet = inet_sk(s);
  547. if (!net_eq(sock_net(s), net))
  548. continue;
  549. if (udp_sk(s)->udp_port_hash == num &&
  550. s->sk_family == PF_INET6) {
  551. struct ipv6_pinfo *np = inet6_sk(s);
  552. if (inet->inet_dport) {
  553. if (inet->inet_dport != rmt_port)
  554. continue;
  555. }
  556. if (!ipv6_addr_any(&np->daddr) &&
  557. !ipv6_addr_equal(&np->daddr, rmt_addr))
  558. continue;
  559. if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
  560. continue;
  561. if (!ipv6_addr_any(&np->rcv_saddr)) {
  562. if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr))
  563. continue;
  564. }
  565. if (!inet6_mc_check(s, loc_addr, rmt_addr))
  566. continue;
  567. return s;
  568. }
  569. }
  570. return NULL;
  571. }
  572. static void flush_stack(struct sock **stack, unsigned int count,
  573. struct sk_buff *skb, unsigned int final)
  574. {
  575. struct sk_buff *skb1 = NULL;
  576. struct sock *sk;
  577. unsigned int i;
  578. for (i = 0; i < count; i++) {
  579. sk = stack[i];
  580. if (likely(skb1 == NULL))
  581. skb1 = (i == final) ? skb : skb_clone(skb, GFP_ATOMIC);
  582. if (!skb1) {
  583. atomic_inc(&sk->sk_drops);
  584. UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
  585. IS_UDPLITE(sk));
  586. UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS,
  587. IS_UDPLITE(sk));
  588. }
  589. if (skb1 && udpv6_queue_rcv_skb(sk, skb1) <= 0)
  590. skb1 = NULL;
  591. }
  592. if (unlikely(skb1))
  593. kfree_skb(skb1);
  594. }
  595. /*
  596. * Note: called only from the BH handler context,
  597. * so we don't need to lock the hashes.
  598. */
  599. static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
  600. const struct in6_addr *saddr, const struct in6_addr *daddr,
  601. struct udp_table *udptable)
  602. {
  603. struct sock *sk, *stack[256 / sizeof(struct sock *)];
  604. const struct udphdr *uh = udp_hdr(skb);
  605. struct udp_hslot *hslot = udp_hashslot(udptable, net, ntohs(uh->dest));
  606. int dif;
  607. unsigned int i, count = 0;
  608. spin_lock(&hslot->lock);
  609. sk = sk_nulls_head(&hslot->head);
  610. dif = inet6_iif(skb);
  611. sk = udp_v6_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif);
  612. while (sk) {
  613. stack[count++] = sk;
  614. sk = udp_v6_mcast_next(net, sk_nulls_next(sk), uh->dest, daddr,
  615. uh->source, saddr, dif);
  616. if (unlikely(count == ARRAY_SIZE(stack))) {
  617. if (!sk)
  618. break;
  619. flush_stack(stack, count, skb, ~0);
  620. count = 0;
  621. }
  622. }
  623. /*
  624. * before releasing the lock, we must take reference on sockets
  625. */
  626. for (i = 0; i < count; i++)
  627. sock_hold(stack[i]);
  628. spin_unlock(&hslot->lock);
  629. if (count) {
  630. flush_stack(stack, count, skb, count - 1);
  631. for (i = 0; i < count; i++)
  632. sock_put(stack[i]);
  633. } else {
  634. kfree_skb(skb);
  635. }
  636. return 0;
  637. }
  638. static inline int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh,
  639. int proto)
  640. {
  641. int err;
  642. UDP_SKB_CB(skb)->partial_cov = 0;
  643. UDP_SKB_CB(skb)->cscov = skb->len;
  644. if (proto == IPPROTO_UDPLITE) {
  645. err = udplite_checksum_init(skb, uh);
  646. if (err)
  647. return err;
  648. }
  649. if (uh->check == 0) {
  650. /* RFC 2460 section 8.1 says that we SHOULD log
  651. this error. Well, it is reasonable.
  652. */
  653. LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n");
  654. return 1;
  655. }
  656. if (skb->ip_summed == CHECKSUM_COMPLETE &&
  657. !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
  658. skb->len, proto, skb->csum))
  659. skb->ip_summed = CHECKSUM_UNNECESSARY;
  660. if (!skb_csum_unnecessary(skb))
  661. skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  662. &ipv6_hdr(skb)->daddr,
  663. skb->len, proto, 0));
  664. return 0;
  665. }
  666. int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
  667. int proto)
  668. {
  669. struct net *net = dev_net(skb->dev);
  670. struct sock *sk;
  671. struct udphdr *uh;
  672. const struct in6_addr *saddr, *daddr;
  673. u32 ulen = 0;
  674. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  675. goto discard;
  676. saddr = &ipv6_hdr(skb)->saddr;
  677. daddr = &ipv6_hdr(skb)->daddr;
  678. uh = udp_hdr(skb);
  679. ulen = ntohs(uh->len);
  680. if (ulen > skb->len)
  681. goto short_packet;
  682. if (proto == IPPROTO_UDP) {
  683. /* UDP validates ulen. */
  684. /* Check for jumbo payload */
  685. if (ulen == 0)
  686. ulen = skb->len;
  687. if (ulen < sizeof(*uh))
  688. goto short_packet;
  689. if (ulen < skb->len) {
  690. if (pskb_trim_rcsum(skb, ulen))
  691. goto short_packet;
  692. saddr = &ipv6_hdr(skb)->saddr;
  693. daddr = &ipv6_hdr(skb)->daddr;
  694. uh = udp_hdr(skb);
  695. }
  696. }
  697. if (udp6_csum_init(skb, uh, proto))
  698. goto discard;
  699. /*
  700. * Multicast receive code
  701. */
  702. if (ipv6_addr_is_multicast(daddr))
  703. return __udp6_lib_mcast_deliver(net, skb,
  704. saddr, daddr, udptable);
  705. /* Unicast */
  706. /*
  707. * check socket cache ... must talk to Alan about his plans
  708. * for sock caches... i'll skip this for now.
  709. */
  710. sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
  711. if (sk != NULL) {
  712. int ret = udpv6_queue_rcv_skb(sk, skb);
  713. sock_put(sk);
  714. /* a return value > 0 means to resubmit the input, but
  715. * it wants the return to be -protocol, or 0
  716. */
  717. if (ret > 0)
  718. return -ret;
  719. return 0;
  720. }
  721. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  722. goto discard;
  723. if (udp_lib_checksum_complete(skb))
  724. goto discard;
  725. UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
  726. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  727. kfree_skb(skb);
  728. return 0;
  729. short_packet:
  730. LIMIT_NETDEBUG(KERN_DEBUG "UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
  731. proto == IPPROTO_UDPLITE ? "-Lite" : "",
  732. saddr,
  733. ntohs(uh->source),
  734. ulen,
  735. skb->len,
  736. daddr,
  737. ntohs(uh->dest));
  738. discard:
  739. UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
  740. kfree_skb(skb);
  741. return 0;
  742. }
  743. static __inline__ int udpv6_rcv(struct sk_buff *skb)
  744. {
  745. return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
  746. }
  747. /*
  748. * Throw away all pending data and cancel the corking. Socket is locked.
  749. */
  750. static void udp_v6_flush_pending_frames(struct sock *sk)
  751. {
  752. struct udp_sock *up = udp_sk(sk);
  753. if (up->pending == AF_INET)
  754. udp_flush_pending_frames(sk);
  755. else if (up->pending) {
  756. up->len = 0;
  757. up->pending = 0;
  758. ip6_flush_pending_frames(sk);
  759. }
  760. }
  761. /**
  762. * udp6_hwcsum_outgoing - handle outgoing HW checksumming
  763. * @sk: socket we are sending on
  764. * @skb: sk_buff containing the filled-in UDP header
  765. * (checksum field must be zeroed out)
  766. */
  767. static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
  768. const struct in6_addr *saddr,
  769. const struct in6_addr *daddr, int len)
  770. {
  771. unsigned int offset;
  772. struct udphdr *uh = udp_hdr(skb);
  773. __wsum csum = 0;
  774. if (skb_queue_len(&sk->sk_write_queue) == 1) {
  775. /* Only one fragment on the socket. */
  776. skb->csum_start = skb_transport_header(skb) - skb->head;
  777. skb->csum_offset = offsetof(struct udphdr, check);
  778. uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
  779. } else {
  780. /*
  781. * HW-checksum won't work as there are two or more
  782. * fragments on the socket so that all csums of sk_buffs
  783. * should be together
  784. */
  785. offset = skb_transport_offset(skb);
  786. skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
  787. skb->ip_summed = CHECKSUM_NONE;
  788. skb_queue_walk(&sk->sk_write_queue, skb) {
  789. csum = csum_add(csum, skb->csum);
  790. }
  791. uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
  792. csum);
  793. if (uh->check == 0)
  794. uh->check = CSUM_MANGLED_0;
  795. }
  796. }
  797. /*
  798. * Sending
  799. */
  800. static int udp_v6_push_pending_frames(struct sock *sk)
  801. {
  802. struct sk_buff *skb;
  803. struct udphdr *uh;
  804. struct udp_sock *up = udp_sk(sk);
  805. struct inet_sock *inet = inet_sk(sk);
  806. struct flowi6 *fl6 = &inet->cork.fl.u.ip6;
  807. int err = 0;
  808. int is_udplite = IS_UDPLITE(sk);
  809. __wsum csum = 0;
  810. /* Grab the skbuff where UDP header space exists. */
  811. if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
  812. goto out;
  813. /*
  814. * Create a UDP header
  815. */
  816. uh = udp_hdr(skb);
  817. uh->source = fl6->fl6_sport;
  818. uh->dest = fl6->fl6_dport;
  819. uh->len = htons(up->len);
  820. uh->check = 0;
  821. if (is_udplite)
  822. csum = udplite_csum_outgoing(sk, skb);
  823. else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
  824. udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr,
  825. up->len);
  826. goto send;
  827. } else
  828. csum = udp_csum_outgoing(sk, skb);
  829. /* add protocol-dependent pseudo-header */
  830. uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
  831. up->len, fl6->flowi6_proto, csum);
  832. if (uh->check == 0)
  833. uh->check = CSUM_MANGLED_0;
  834. send:
  835. err = ip6_push_pending_frames(sk);
  836. if (err) {
  837. if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
  838. UDP6_INC_STATS_USER(sock_net(sk),
  839. UDP_MIB_SNDBUFERRORS, is_udplite);
  840. err = 0;
  841. }
  842. } else
  843. UDP6_INC_STATS_USER(sock_net(sk),
  844. UDP_MIB_OUTDATAGRAMS, is_udplite);
  845. out:
  846. up->len = 0;
  847. up->pending = 0;
  848. return err;
  849. }
  850. int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
  851. struct msghdr *msg, size_t len)
  852. {
  853. struct ipv6_txoptions opt_space;
  854. struct udp_sock *up = udp_sk(sk);
  855. struct inet_sock *inet = inet_sk(sk);
  856. struct ipv6_pinfo *np = inet6_sk(sk);
  857. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
  858. struct in6_addr *daddr, *final_p, final;
  859. struct ipv6_txoptions *opt = NULL;
  860. struct ip6_flowlabel *flowlabel = NULL;
  861. struct flowi6 fl6;
  862. struct dst_entry *dst;
  863. int addr_len = msg->msg_namelen;
  864. int ulen = len;
  865. int hlimit = -1;
  866. int tclass = -1;
  867. int dontfrag = -1;
  868. int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
  869. int err;
  870. int connected = 0;
  871. int is_udplite = IS_UDPLITE(sk);
  872. int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
  873. /* destination address check */
  874. if (sin6) {
  875. if (addr_len < offsetof(struct sockaddr, sa_data))
  876. return -EINVAL;
  877. switch (sin6->sin6_family) {
  878. case AF_INET6:
  879. if (addr_len < SIN6_LEN_RFC2133)
  880. return -EINVAL;
  881. daddr = &sin6->sin6_addr;
  882. break;
  883. case AF_INET:
  884. goto do_udp_sendmsg;
  885. case AF_UNSPEC:
  886. msg->msg_name = sin6 = NULL;
  887. msg->msg_namelen = addr_len = 0;
  888. daddr = NULL;
  889. break;
  890. default:
  891. return -EINVAL;
  892. }
  893. } else if (!up->pending) {
  894. if (sk->sk_state != TCP_ESTABLISHED)
  895. return -EDESTADDRREQ;
  896. daddr = &np->daddr;
  897. } else
  898. daddr = NULL;
  899. if (daddr) {
  900. if (ipv6_addr_v4mapped(daddr)) {
  901. struct sockaddr_in sin;
  902. sin.sin_family = AF_INET;
  903. sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
  904. sin.sin_addr.s_addr = daddr->s6_addr32[3];
  905. msg->msg_name = &sin;
  906. msg->msg_namelen = sizeof(sin);
  907. do_udp_sendmsg:
  908. if (__ipv6_only_sock(sk))
  909. return -ENETUNREACH;
  910. return udp_sendmsg(iocb, sk, msg, len);
  911. }
  912. }
  913. if (up->pending == AF_INET)
  914. return udp_sendmsg(iocb, sk, msg, len);
  915. /* Rough check on arithmetic overflow,
  916. better check is made in ip6_append_data().
  917. */
  918. if (len > INT_MAX - sizeof(struct udphdr))
  919. return -EMSGSIZE;
  920. if (up->pending) {
  921. /*
  922. * There are pending frames.
  923. * The socket lock must be held while it's corked.
  924. */
  925. lock_sock(sk);
  926. if (likely(up->pending)) {
  927. if (unlikely(up->pending != AF_INET6)) {
  928. release_sock(sk);
  929. return -EAFNOSUPPORT;
  930. }
  931. dst = NULL;
  932. goto do_append_data;
  933. }
  934. release_sock(sk);
  935. }
  936. ulen += sizeof(struct udphdr);
  937. memset(&fl6, 0, sizeof(fl6));
  938. if (sin6) {
  939. if (sin6->sin6_port == 0)
  940. return -EINVAL;
  941. fl6.fl6_dport = sin6->sin6_port;
  942. daddr = &sin6->sin6_addr;
  943. if (np->sndflow) {
  944. fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
  945. if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
  946. flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
  947. if (flowlabel == NULL)
  948. return -EINVAL;
  949. daddr = &flowlabel->dst;
  950. }
  951. }
  952. /*
  953. * Otherwise it will be difficult to maintain
  954. * sk->sk_dst_cache.
  955. */
  956. if (sk->sk_state == TCP_ESTABLISHED &&
  957. ipv6_addr_equal(daddr, &np->daddr))
  958. daddr = &np->daddr;
  959. if (addr_len >= sizeof(struct sockaddr_in6) &&
  960. sin6->sin6_scope_id &&
  961. ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
  962. fl6.flowi6_oif = sin6->sin6_scope_id;
  963. } else {
  964. if (sk->sk_state != TCP_ESTABLISHED)
  965. return -EDESTADDRREQ;
  966. fl6.fl6_dport = inet->inet_dport;
  967. daddr = &np->daddr;
  968. fl6.flowlabel = np->flow_label;
  969. connected = 1;
  970. }
  971. if (!fl6.flowi6_oif)
  972. fl6.flowi6_oif = sk->sk_bound_dev_if;
  973. if (!fl6.flowi6_oif)
  974. fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
  975. fl6.flowi6_mark = sk->sk_mark;
  976. if (msg->msg_controllen) {
  977. opt = &opt_space;
  978. memset(opt, 0, sizeof(struct ipv6_txoptions));
  979. opt->tot_len = sizeof(*opt);
  980. err = datagram_send_ctl(sock_net(sk), sk, msg, &fl6, opt,
  981. &hlimit, &tclass, &dontfrag);
  982. if (err < 0) {
  983. fl6_sock_release(flowlabel);
  984. return err;
  985. }
  986. if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
  987. flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
  988. if (flowlabel == NULL)
  989. return -EINVAL;
  990. }
  991. if (!(opt->opt_nflen|opt->opt_flen))
  992. opt = NULL;
  993. connected = 0;
  994. }
  995. if (opt == NULL)
  996. opt = np->opt;
  997. if (flowlabel)
  998. opt = fl6_merge_options(&opt_space, flowlabel, opt);
  999. opt = ipv6_fixup_options(&opt_space, opt);
  1000. fl6.flowi6_proto = sk->sk_protocol;
  1001. if (!ipv6_addr_any(daddr))
  1002. fl6.daddr = *daddr;
  1003. else
  1004. fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
  1005. if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
  1006. fl6.saddr = np->saddr;
  1007. fl6.fl6_sport = inet->inet_sport;
  1008. final_p = fl6_update_dst(&fl6, opt, &final);
  1009. if (final_p)
  1010. connected = 0;
  1011. if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) {
  1012. fl6.flowi6_oif = np->mcast_oif;
  1013. connected = 0;
  1014. } else if (!fl6.flowi6_oif)
  1015. fl6.flowi6_oif = np->ucast_oif;
  1016. security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
  1017. dst = ip6_sk_dst_lookup_flow(sk, &fl6, final_p, true);
  1018. if (IS_ERR(dst)) {
  1019. err = PTR_ERR(dst);
  1020. dst = NULL;
  1021. goto out;
  1022. }
  1023. if (hlimit < 0) {
  1024. if (ipv6_addr_is_multicast(&fl6.daddr))
  1025. hlimit = np->mcast_hops;
  1026. else
  1027. hlimit = np->hop_limit;
  1028. if (hlimit < 0)
  1029. hlimit = ip6_dst_hoplimit(dst);
  1030. }
  1031. if (tclass < 0)
  1032. tclass = np->tclass;
  1033. if (dontfrag < 0)
  1034. dontfrag = np->dontfrag;
  1035. if (msg->msg_flags&MSG_CONFIRM)
  1036. goto do_confirm;
  1037. back_from_confirm:
  1038. lock_sock(sk);
  1039. if (unlikely(up->pending)) {
  1040. /* The socket is already corked while preparing it. */
  1041. /* ... which is an evident application bug. --ANK */
  1042. release_sock(sk);
  1043. LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
  1044. err = -EINVAL;
  1045. goto out;
  1046. }
  1047. up->pending = AF_INET6;
  1048. do_append_data:
  1049. up->len += ulen;
  1050. getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
  1051. err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen,
  1052. sizeof(struct udphdr), hlimit, tclass, opt, &fl6,
  1053. (struct rt6_info*)dst,
  1054. corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags, dontfrag);
  1055. if (err)
  1056. udp_v6_flush_pending_frames(sk);
  1057. else if (!corkreq)
  1058. err = udp_v6_push_pending_frames(sk);
  1059. else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
  1060. up->pending = 0;
  1061. if (dst) {
  1062. if (connected) {
  1063. ip6_dst_store(sk, dst,
  1064. ipv6_addr_equal(&fl6.daddr, &np->daddr) ?
  1065. &np->daddr : NULL,
  1066. #ifdef CONFIG_IPV6_SUBTREES
  1067. ipv6_addr_equal(&fl6.saddr, &np->saddr) ?
  1068. &np->saddr :
  1069. #endif
  1070. NULL);
  1071. } else {
  1072. dst_release(dst);
  1073. }
  1074. dst = NULL;
  1075. }
  1076. if (err > 0)
  1077. err = np->recverr ? net_xmit_errno(err) : 0;
  1078. release_sock(sk);
  1079. out:
  1080. dst_release(dst);
  1081. fl6_sock_release(flowlabel);
  1082. if (!err)
  1083. return len;
  1084. /*
  1085. * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
  1086. * ENOBUFS might not be good (it's not tunable per se), but otherwise
  1087. * we don't have a good statistic (IpOutDiscards but it can be too many
  1088. * things). We could add another new stat but at least for now that
  1089. * seems like overkill.
  1090. */
  1091. if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
  1092. UDP6_INC_STATS_USER(sock_net(sk),
  1093. UDP_MIB_SNDBUFERRORS, is_udplite);
  1094. }
  1095. return err;
  1096. do_confirm:
  1097. dst_confirm(dst);
  1098. if (!(msg->msg_flags&MSG_PROBE) || len)
  1099. goto back_from_confirm;
  1100. err = 0;
  1101. goto out;
  1102. }
  1103. void udpv6_destroy_sock(struct sock *sk)
  1104. {
  1105. lock_sock(sk);
  1106. udp_v6_flush_pending_frames(sk);
  1107. release_sock(sk);
  1108. inet6_destroy_sock(sk);
  1109. }
  1110. /*
  1111. * Socket option code for UDP
  1112. */
  1113. int udpv6_setsockopt(struct sock *sk, int level, int optname,
  1114. char __user *optval, unsigned int optlen)
  1115. {
  1116. if (level == SOL_UDP || level == SOL_UDPLITE)
  1117. return udp_lib_setsockopt(sk, level, optname, optval, optlen,
  1118. udp_v6_push_pending_frames);
  1119. return ipv6_setsockopt(sk, level, optname, optval, optlen);
  1120. }
  1121. #ifdef CONFIG_COMPAT
  1122. int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
  1123. char __user *optval, unsigned int optlen)
  1124. {
  1125. if (level == SOL_UDP || level == SOL_UDPLITE)
  1126. return udp_lib_setsockopt(sk, level, optname, optval, optlen,
  1127. udp_v6_push_pending_frames);
  1128. return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
  1129. }
  1130. #endif
  1131. int udpv6_getsockopt(struct sock *sk, int level, int optname,
  1132. char __user *optval, int __user *optlen)
  1133. {
  1134. if (level == SOL_UDP || level == SOL_UDPLITE)
  1135. return udp_lib_getsockopt(sk, level, optname, optval, optlen);
  1136. return ipv6_getsockopt(sk, level, optname, optval, optlen);
  1137. }
  1138. #ifdef CONFIG_COMPAT
  1139. int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
  1140. char __user *optval, int __user *optlen)
  1141. {
  1142. if (level == SOL_UDP || level == SOL_UDPLITE)
  1143. return udp_lib_getsockopt(sk, level, optname, optval, optlen);
  1144. return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
  1145. }
  1146. #endif
  1147. static int udp6_ufo_send_check(struct sk_buff *skb)
  1148. {
  1149. const struct ipv6hdr *ipv6h;
  1150. struct udphdr *uh;
  1151. if (!pskb_may_pull(skb, sizeof(*uh)))
  1152. return -EINVAL;
  1153. ipv6h = ipv6_hdr(skb);
  1154. uh = udp_hdr(skb);
  1155. uh->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, skb->len,
  1156. IPPROTO_UDP, 0);
  1157. skb->csum_start = skb_transport_header(skb) - skb->head;
  1158. skb->csum_offset = offsetof(struct udphdr, check);
  1159. skb->ip_summed = CHECKSUM_PARTIAL;
  1160. return 0;
  1161. }
  1162. static struct sk_buff *udp6_ufo_fragment(struct sk_buff *skb,
  1163. netdev_features_t features)
  1164. {
  1165. struct sk_buff *segs = ERR_PTR(-EINVAL);
  1166. unsigned int mss;
  1167. unsigned int unfrag_ip6hlen, unfrag_len;
  1168. struct frag_hdr *fptr;
  1169. u8 *mac_start, *prevhdr;
  1170. u8 nexthdr;
  1171. u8 frag_hdr_sz = sizeof(struct frag_hdr);
  1172. int offset;
  1173. __wsum csum;
  1174. mss = skb_shinfo(skb)->gso_size;
  1175. if (unlikely(skb->len <= mss))
  1176. goto out;
  1177. if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
  1178. /* Packet is from an untrusted source, reset gso_segs. */
  1179. int type = skb_shinfo(skb)->gso_type;
  1180. if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY) ||
  1181. !(type & (SKB_GSO_UDP))))
  1182. goto out;
  1183. skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
  1184. segs = NULL;
  1185. goto out;
  1186. }
  1187. /* Do software UFO. Complete and fill in the UDP checksum as HW cannot
  1188. * do checksum of UDP packets sent as multiple IP fragments.
  1189. */
  1190. offset = skb_checksum_start_offset(skb);
  1191. csum = skb_checksum(skb, offset, skb->len - offset, 0);
  1192. offset += skb->csum_offset;
  1193. *(__sum16 *)(skb->data + offset) = csum_fold(csum);
  1194. skb->ip_summed = CHECKSUM_NONE;
  1195. /* Check if there is enough headroom to insert fragment header. */
  1196. if ((skb_mac_header(skb) < skb->head + frag_hdr_sz) &&
  1197. pskb_expand_head(skb, frag_hdr_sz, 0, GFP_ATOMIC))
  1198. goto out;
  1199. /* Find the unfragmentable header and shift it left by frag_hdr_sz
  1200. * bytes to insert fragment header.
  1201. */
  1202. unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr);
  1203. nexthdr = *prevhdr;
  1204. *prevhdr = NEXTHDR_FRAGMENT;
  1205. unfrag_len = skb_network_header(skb) - skb_mac_header(skb) +
  1206. unfrag_ip6hlen;
  1207. mac_start = skb_mac_header(skb);
  1208. memmove(mac_start-frag_hdr_sz, mac_start, unfrag_len);
  1209. skb->mac_header -= frag_hdr_sz;
  1210. skb->network_header -= frag_hdr_sz;
  1211. fptr = (struct frag_hdr *)(skb_network_header(skb) + unfrag_ip6hlen);
  1212. fptr->nexthdr = nexthdr;
  1213. fptr->reserved = 0;
  1214. ipv6_select_ident(fptr, (struct rt6_info *)skb_dst(skb));
  1215. /* Fragment the skb. ipv6 header and the remaining fields of the
  1216. * fragment header are updated in ipv6_gso_segment()
  1217. */
  1218. segs = skb_segment(skb, features);
  1219. out:
  1220. return segs;
  1221. }
  1222. static const struct inet6_protocol udpv6_protocol = {
  1223. .handler = udpv6_rcv,
  1224. .err_handler = udpv6_err,
  1225. .gso_send_check = udp6_ufo_send_check,
  1226. .gso_segment = udp6_ufo_fragment,
  1227. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  1228. };
  1229. /* ------------------------------------------------------------------------ */
  1230. #ifdef CONFIG_PROC_FS
  1231. static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket)
  1232. {
  1233. struct inet_sock *inet = inet_sk(sp);
  1234. struct ipv6_pinfo *np = inet6_sk(sp);
  1235. const struct in6_addr *dest, *src;
  1236. __u16 destp, srcp;
  1237. dest = &np->daddr;
  1238. src = &np->rcv_saddr;
  1239. destp = ntohs(inet->inet_dport);
  1240. srcp = ntohs(inet->inet_sport);
  1241. seq_printf(seq,
  1242. "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
  1243. "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %d\n",
  1244. bucket,
  1245. src->s6_addr32[0], src->s6_addr32[1],
  1246. src->s6_addr32[2], src->s6_addr32[3], srcp,
  1247. dest->s6_addr32[0], dest->s6_addr32[1],
  1248. dest->s6_addr32[2], dest->s6_addr32[3], destp,
  1249. sp->sk_state,
  1250. sk_wmem_alloc_get(sp),
  1251. sk_rmem_alloc_get(sp),
  1252. 0, 0L, 0,
  1253. sock_i_uid(sp), 0,
  1254. sock_i_ino(sp),
  1255. atomic_read(&sp->sk_refcnt), sp,
  1256. atomic_read(&sp->sk_drops));
  1257. }
  1258. int udp6_seq_show(struct seq_file *seq, void *v)
  1259. {
  1260. if (v == SEQ_START_TOKEN)
  1261. seq_printf(seq,
  1262. " sl "
  1263. "local_address "
  1264. "remote_address "
  1265. "st tx_queue rx_queue tr tm->when retrnsmt"
  1266. " uid timeout inode ref pointer drops\n");
  1267. else
  1268. udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
  1269. return 0;
  1270. }
  1271. static const struct file_operations udp6_afinfo_seq_fops = {
  1272. .owner = THIS_MODULE,
  1273. .open = udp_seq_open,
  1274. .read = seq_read,
  1275. .llseek = seq_lseek,
  1276. .release = seq_release_net
  1277. };
  1278. static struct udp_seq_afinfo udp6_seq_afinfo = {
  1279. .name = "udp6",
  1280. .family = AF_INET6,
  1281. .udp_table = &udp_table,
  1282. .seq_fops = &udp6_afinfo_seq_fops,
  1283. .seq_ops = {
  1284. .show = udp6_seq_show,
  1285. },
  1286. };
  1287. int __net_init udp6_proc_init(struct net *net)
  1288. {
  1289. return udp_proc_register(net, &udp6_seq_afinfo);
  1290. }
  1291. void udp6_proc_exit(struct net *net) {
  1292. udp_proc_unregister(net, &udp6_seq_afinfo);
  1293. }
  1294. #endif /* CONFIG_PROC_FS */
  1295. /* ------------------------------------------------------------------------ */
  1296. struct proto udpv6_prot = {
  1297. .name = "UDPv6",
  1298. .owner = THIS_MODULE,
  1299. .close = udp_lib_close,
  1300. .connect = ip6_datagram_connect,
  1301. .disconnect = udp_disconnect,
  1302. .ioctl = udp_ioctl,
  1303. .destroy = udpv6_destroy_sock,
  1304. .setsockopt = udpv6_setsockopt,
  1305. .getsockopt = udpv6_getsockopt,
  1306. .sendmsg = udpv6_sendmsg,
  1307. .recvmsg = udpv6_recvmsg,
  1308. .backlog_rcv = __udpv6_queue_rcv_skb,
  1309. .hash = udp_lib_hash,
  1310. .unhash = udp_lib_unhash,
  1311. .rehash = udp_v6_rehash,
  1312. .get_port = udp_v6_get_port,
  1313. .memory_allocated = &udp_memory_allocated,
  1314. .sysctl_mem = sysctl_udp_mem,
  1315. .sysctl_wmem = &sysctl_udp_wmem_min,
  1316. .sysctl_rmem = &sysctl_udp_rmem_min,
  1317. .obj_size = sizeof(struct udp6_sock),
  1318. .slab_flags = SLAB_DESTROY_BY_RCU,
  1319. .h.udp_table = &udp_table,
  1320. #ifdef CONFIG_COMPAT
  1321. .compat_setsockopt = compat_udpv6_setsockopt,
  1322. .compat_getsockopt = compat_udpv6_getsockopt,
  1323. #endif
  1324. .clear_sk = sk_prot_clear_portaddr_nulls,
  1325. };
  1326. static struct inet_protosw udpv6_protosw = {
  1327. .type = SOCK_DGRAM,
  1328. .protocol = IPPROTO_UDP,
  1329. .prot = &udpv6_prot,
  1330. .ops = &inet6_dgram_ops,
  1331. .no_check = UDP_CSUM_DEFAULT,
  1332. .flags = INET_PROTOSW_PERMANENT,
  1333. };
  1334. int __init udpv6_init(void)
  1335. {
  1336. int ret;
  1337. ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
  1338. if (ret)
  1339. goto out;
  1340. ret = inet6_register_protosw(&udpv6_protosw);
  1341. if (ret)
  1342. goto out_udpv6_protocol;
  1343. out:
  1344. return ret;
  1345. out_udpv6_protocol:
  1346. inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
  1347. goto out;
  1348. }
  1349. void udpv6_exit(void)
  1350. {
  1351. inet6_unregister_protosw(&udpv6_protosw);
  1352. inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
  1353. }