udp.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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 <asm/uaccess.h>
  37. #include <net/ndisc.h>
  38. #include <net/protocol.h>
  39. #include <net/transp_v6.h>
  40. #include <net/ip6_route.h>
  41. #include <net/raw.h>
  42. #include <net/tcp_states.h>
  43. #include <net/ip6_checksum.h>
  44. #include <net/xfrm.h>
  45. #include <linux/proc_fs.h>
  46. #include <linux/seq_file.h>
  47. #include "udp_impl.h"
  48. int udp_v6_get_port(struct sock *sk, unsigned short snum)
  49. {
  50. return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal);
  51. }
  52. static struct sock *__udp6_lib_lookup(struct net *net,
  53. struct in6_addr *saddr, __be16 sport,
  54. struct in6_addr *daddr, __be16 dport,
  55. int dif, struct hlist_head udptable[])
  56. {
  57. struct sock *sk, *result = NULL;
  58. struct hlist_node *node;
  59. unsigned short hnum = ntohs(dport);
  60. int badness = -1;
  61. read_lock(&udp_hash_lock);
  62. sk_for_each(sk, node, &udptable[udp_hashfn(net, hnum)]) {
  63. struct inet_sock *inet = inet_sk(sk);
  64. if (net_eq(sock_net(sk), net) && sk->sk_hash == hnum &&
  65. sk->sk_family == PF_INET6) {
  66. struct ipv6_pinfo *np = inet6_sk(sk);
  67. int score = 0;
  68. if (inet->dport) {
  69. if (inet->dport != sport)
  70. continue;
  71. score++;
  72. }
  73. if (!ipv6_addr_any(&np->rcv_saddr)) {
  74. if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
  75. continue;
  76. score++;
  77. }
  78. if (!ipv6_addr_any(&np->daddr)) {
  79. if (!ipv6_addr_equal(&np->daddr, saddr))
  80. continue;
  81. score++;
  82. }
  83. if (sk->sk_bound_dev_if) {
  84. if (sk->sk_bound_dev_if != dif)
  85. continue;
  86. score++;
  87. }
  88. if (score == 4) {
  89. result = sk;
  90. break;
  91. } else if (score > badness) {
  92. result = sk;
  93. badness = score;
  94. }
  95. }
  96. }
  97. if (result)
  98. sock_hold(result);
  99. read_unlock(&udp_hash_lock);
  100. return result;
  101. }
  102. static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
  103. __be16 sport, __be16 dport,
  104. struct hlist_head udptable[])
  105. {
  106. struct sock *sk;
  107. struct ipv6hdr *iph = ipv6_hdr(skb);
  108. if (unlikely(sk = skb_steal_sock(skb)))
  109. return sk;
  110. else
  111. return __udp6_lib_lookup(dev_net(skb->dst->dev), &iph->saddr, sport,
  112. &iph->daddr, dport, inet6_iif(skb),
  113. udptable);
  114. }
  115. /*
  116. * This should be easy, if there is something there we
  117. * return it, otherwise we block.
  118. */
  119. int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
  120. struct msghdr *msg, size_t len,
  121. int noblock, int flags, int *addr_len)
  122. {
  123. struct ipv6_pinfo *np = inet6_sk(sk);
  124. struct inet_sock *inet = inet_sk(sk);
  125. struct sk_buff *skb;
  126. unsigned int ulen, copied;
  127. int peeked;
  128. int err;
  129. int is_udplite = IS_UDPLITE(sk);
  130. int is_udp4;
  131. if (addr_len)
  132. *addr_len=sizeof(struct sockaddr_in6);
  133. if (flags & MSG_ERRQUEUE)
  134. return ipv6_recv_error(sk, msg, len);
  135. try_again:
  136. skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
  137. &peeked, &err);
  138. if (!skb)
  139. goto out;
  140. ulen = skb->len - sizeof(struct udphdr);
  141. copied = len;
  142. if (copied > ulen)
  143. copied = ulen;
  144. else if (copied < ulen)
  145. msg->msg_flags |= MSG_TRUNC;
  146. is_udp4 = (skb->protocol == htons(ETH_P_IP));
  147. /*
  148. * If checksum is needed at all, try to do it while copying the
  149. * data. If the data is truncated, or if we only want a partial
  150. * coverage checksum (UDP-Lite), do it before the copy.
  151. */
  152. if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
  153. if (udp_lib_checksum_complete(skb))
  154. goto csum_copy_err;
  155. }
  156. if (skb_csum_unnecessary(skb))
  157. err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
  158. msg->msg_iov, copied );
  159. else {
  160. err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
  161. if (err == -EINVAL)
  162. goto csum_copy_err;
  163. }
  164. if (err)
  165. goto out_free;
  166. if (!peeked) {
  167. if (is_udp4)
  168. UDP_INC_STATS_USER(sock_net(sk),
  169. UDP_MIB_INDATAGRAMS, is_udplite);
  170. else
  171. UDP6_INC_STATS_USER(sock_net(sk),
  172. UDP_MIB_INDATAGRAMS, is_udplite);
  173. }
  174. sock_recv_timestamp(msg, sk, skb);
  175. /* Copy the address. */
  176. if (msg->msg_name) {
  177. struct sockaddr_in6 *sin6;
  178. sin6 = (struct sockaddr_in6 *) msg->msg_name;
  179. sin6->sin6_family = AF_INET6;
  180. sin6->sin6_port = udp_hdr(skb)->source;
  181. sin6->sin6_flowinfo = 0;
  182. sin6->sin6_scope_id = 0;
  183. if (is_udp4)
  184. ipv6_addr_set(&sin6->sin6_addr, 0, 0,
  185. htonl(0xffff), ip_hdr(skb)->saddr);
  186. else {
  187. ipv6_addr_copy(&sin6->sin6_addr,
  188. &ipv6_hdr(skb)->saddr);
  189. if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
  190. sin6->sin6_scope_id = IP6CB(skb)->iif;
  191. }
  192. }
  193. if (is_udp4) {
  194. if (inet->cmsg_flags)
  195. ip_cmsg_recv(msg, skb);
  196. } else {
  197. if (np->rxopt.all)
  198. datagram_recv_ctl(sk, msg, skb);
  199. }
  200. err = copied;
  201. if (flags & MSG_TRUNC)
  202. err = ulen;
  203. out_free:
  204. lock_sock(sk);
  205. skb_free_datagram(sk, skb);
  206. release_sock(sk);
  207. out:
  208. return err;
  209. csum_copy_err:
  210. lock_sock(sk);
  211. if (!skb_kill_datagram(sk, skb, flags)) {
  212. if (is_udp4)
  213. UDP_INC_STATS_USER(sock_net(sk),
  214. UDP_MIB_INERRORS, is_udplite);
  215. else
  216. UDP6_INC_STATS_USER(sock_net(sk),
  217. UDP_MIB_INERRORS, is_udplite);
  218. }
  219. release_sock(sk);
  220. if (flags & MSG_DONTWAIT)
  221. return -EAGAIN;
  222. goto try_again;
  223. }
  224. void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  225. int type, int code, int offset, __be32 info,
  226. struct hlist_head udptable[] )
  227. {
  228. struct ipv6_pinfo *np;
  229. struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data;
  230. struct in6_addr *saddr = &hdr->saddr;
  231. struct in6_addr *daddr = &hdr->daddr;
  232. struct udphdr *uh = (struct udphdr*)(skb->data+offset);
  233. struct sock *sk;
  234. int err;
  235. sk = __udp6_lib_lookup(dev_net(skb->dev), daddr, uh->dest,
  236. saddr, uh->source, inet6_iif(skb), udptable);
  237. if (sk == NULL)
  238. return;
  239. np = inet6_sk(sk);
  240. if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
  241. goto out;
  242. if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
  243. goto out;
  244. if (np->recverr)
  245. ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
  246. sk->sk_err = err;
  247. sk->sk_error_report(sk);
  248. out:
  249. sock_put(sk);
  250. }
  251. static __inline__ void udpv6_err(struct sk_buff *skb,
  252. struct inet6_skb_parm *opt, int type,
  253. int code, int offset, __be32 info )
  254. {
  255. __udp6_lib_err(skb, opt, type, code, offset, info, udp_hash);
  256. }
  257. int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
  258. {
  259. struct udp_sock *up = udp_sk(sk);
  260. int rc;
  261. int is_udplite = IS_UDPLITE(sk);
  262. if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
  263. goto drop;
  264. /*
  265. * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
  266. */
  267. if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
  268. if (up->pcrlen == 0) { /* full coverage was set */
  269. LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: partial coverage"
  270. " %d while full coverage %d requested\n",
  271. UDP_SKB_CB(skb)->cscov, skb->len);
  272. goto drop;
  273. }
  274. if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
  275. LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: coverage %d "
  276. "too small, need min %d\n",
  277. UDP_SKB_CB(skb)->cscov, up->pcrlen);
  278. goto drop;
  279. }
  280. }
  281. if (sk->sk_filter) {
  282. if (udp_lib_checksum_complete(skb))
  283. goto drop;
  284. }
  285. if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) {
  286. /* Note that an ENOMEM error is charged twice */
  287. if (rc == -ENOMEM) {
  288. UDP6_INC_STATS_BH(sock_net(sk),
  289. UDP_MIB_RCVBUFERRORS, is_udplite);
  290. atomic_inc(&sk->sk_drops);
  291. }
  292. goto drop;
  293. }
  294. return 0;
  295. drop:
  296. UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  297. kfree_skb(skb);
  298. return -1;
  299. }
  300. static struct sock *udp_v6_mcast_next(struct net *net, struct sock *sk,
  301. __be16 loc_port, struct in6_addr *loc_addr,
  302. __be16 rmt_port, struct in6_addr *rmt_addr,
  303. int dif)
  304. {
  305. struct hlist_node *node;
  306. struct sock *s = sk;
  307. unsigned short num = ntohs(loc_port);
  308. sk_for_each_from(s, node) {
  309. struct inet_sock *inet = inet_sk(s);
  310. if (!net_eq(sock_net(s), net))
  311. continue;
  312. if (s->sk_hash == num && s->sk_family == PF_INET6) {
  313. struct ipv6_pinfo *np = inet6_sk(s);
  314. if (inet->dport) {
  315. if (inet->dport != rmt_port)
  316. continue;
  317. }
  318. if (!ipv6_addr_any(&np->daddr) &&
  319. !ipv6_addr_equal(&np->daddr, rmt_addr))
  320. continue;
  321. if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
  322. continue;
  323. if (!ipv6_addr_any(&np->rcv_saddr)) {
  324. if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr))
  325. continue;
  326. }
  327. if (!inet6_mc_check(s, loc_addr, rmt_addr))
  328. continue;
  329. return s;
  330. }
  331. }
  332. return NULL;
  333. }
  334. /*
  335. * Note: called only from the BH handler context,
  336. * so we don't need to lock the hashes.
  337. */
  338. static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
  339. struct in6_addr *saddr, struct in6_addr *daddr,
  340. struct hlist_head udptable[])
  341. {
  342. struct sock *sk, *sk2;
  343. const struct udphdr *uh = udp_hdr(skb);
  344. int dif;
  345. read_lock(&udp_hash_lock);
  346. sk = sk_head(&udptable[udp_hashfn(net, ntohs(uh->dest))]);
  347. dif = inet6_iif(skb);
  348. sk = udp_v6_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif);
  349. if (!sk) {
  350. kfree_skb(skb);
  351. goto out;
  352. }
  353. sk2 = sk;
  354. while ((sk2 = udp_v6_mcast_next(net, sk_next(sk2), uh->dest, daddr,
  355. uh->source, saddr, dif))) {
  356. struct sk_buff *buff = skb_clone(skb, GFP_ATOMIC);
  357. if (buff) {
  358. bh_lock_sock(sk2);
  359. if (!sock_owned_by_user(sk2))
  360. udpv6_queue_rcv_skb(sk2, buff);
  361. else
  362. sk_add_backlog(sk2, buff);
  363. bh_unlock_sock(sk2);
  364. }
  365. }
  366. bh_lock_sock(sk);
  367. if (!sock_owned_by_user(sk))
  368. udpv6_queue_rcv_skb(sk, skb);
  369. else
  370. sk_add_backlog(sk, skb);
  371. bh_unlock_sock(sk);
  372. out:
  373. read_unlock(&udp_hash_lock);
  374. return 0;
  375. }
  376. static inline int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh,
  377. int proto)
  378. {
  379. int err;
  380. UDP_SKB_CB(skb)->partial_cov = 0;
  381. UDP_SKB_CB(skb)->cscov = skb->len;
  382. if (proto == IPPROTO_UDPLITE) {
  383. err = udplite_checksum_init(skb, uh);
  384. if (err)
  385. return err;
  386. }
  387. if (uh->check == 0) {
  388. /* RFC 2460 section 8.1 says that we SHOULD log
  389. this error. Well, it is reasonable.
  390. */
  391. LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n");
  392. return 1;
  393. }
  394. if (skb->ip_summed == CHECKSUM_COMPLETE &&
  395. !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
  396. skb->len, proto, skb->csum))
  397. skb->ip_summed = CHECKSUM_UNNECESSARY;
  398. if (!skb_csum_unnecessary(skb))
  399. skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  400. &ipv6_hdr(skb)->daddr,
  401. skb->len, proto, 0));
  402. return 0;
  403. }
  404. int __udp6_lib_rcv(struct sk_buff *skb, struct hlist_head udptable[],
  405. int proto)
  406. {
  407. struct sock *sk;
  408. struct udphdr *uh;
  409. struct net_device *dev = skb->dev;
  410. struct in6_addr *saddr, *daddr;
  411. u32 ulen = 0;
  412. struct net *net = dev_net(skb->dev);
  413. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  414. goto short_packet;
  415. saddr = &ipv6_hdr(skb)->saddr;
  416. daddr = &ipv6_hdr(skb)->daddr;
  417. uh = udp_hdr(skb);
  418. ulen = ntohs(uh->len);
  419. if (ulen > skb->len)
  420. goto short_packet;
  421. if (proto == IPPROTO_UDP) {
  422. /* UDP validates ulen. */
  423. /* Check for jumbo payload */
  424. if (ulen == 0)
  425. ulen = skb->len;
  426. if (ulen < sizeof(*uh))
  427. goto short_packet;
  428. if (ulen < skb->len) {
  429. if (pskb_trim_rcsum(skb, ulen))
  430. goto short_packet;
  431. saddr = &ipv6_hdr(skb)->saddr;
  432. daddr = &ipv6_hdr(skb)->daddr;
  433. uh = udp_hdr(skb);
  434. }
  435. }
  436. if (udp6_csum_init(skb, uh, proto))
  437. goto discard;
  438. /*
  439. * Multicast receive code
  440. */
  441. if (ipv6_addr_is_multicast(daddr))
  442. return __udp6_lib_mcast_deliver(net, skb,
  443. saddr, daddr, udptable);
  444. /* Unicast */
  445. /*
  446. * check socket cache ... must talk to Alan about his plans
  447. * for sock caches... i'll skip this for now.
  448. */
  449. sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
  450. if (sk == NULL) {
  451. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  452. goto discard;
  453. if (udp_lib_checksum_complete(skb))
  454. goto discard;
  455. UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS,
  456. proto == IPPROTO_UDPLITE);
  457. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, dev);
  458. kfree_skb(skb);
  459. return 0;
  460. }
  461. /* deliver */
  462. bh_lock_sock(sk);
  463. if (!sock_owned_by_user(sk))
  464. udpv6_queue_rcv_skb(sk, skb);
  465. else
  466. sk_add_backlog(sk, skb);
  467. bh_unlock_sock(sk);
  468. sock_put(sk);
  469. return 0;
  470. short_packet:
  471. LIMIT_NETDEBUG(KERN_DEBUG "UDP%sv6: short packet: %d/%u\n",
  472. proto == IPPROTO_UDPLITE ? "-Lite" : "",
  473. ulen, skb->len);
  474. discard:
  475. UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
  476. kfree_skb(skb);
  477. return 0;
  478. }
  479. static __inline__ int udpv6_rcv(struct sk_buff *skb)
  480. {
  481. return __udp6_lib_rcv(skb, udp_hash, IPPROTO_UDP);
  482. }
  483. /*
  484. * Throw away all pending data and cancel the corking. Socket is locked.
  485. */
  486. static void udp_v6_flush_pending_frames(struct sock *sk)
  487. {
  488. struct udp_sock *up = udp_sk(sk);
  489. if (up->pending == AF_INET)
  490. udp_flush_pending_frames(sk);
  491. else if (up->pending) {
  492. up->len = 0;
  493. up->pending = 0;
  494. ip6_flush_pending_frames(sk);
  495. }
  496. }
  497. /*
  498. * Sending
  499. */
  500. static int udp_v6_push_pending_frames(struct sock *sk)
  501. {
  502. struct sk_buff *skb;
  503. struct udphdr *uh;
  504. struct udp_sock *up = udp_sk(sk);
  505. struct inet_sock *inet = inet_sk(sk);
  506. struct flowi *fl = &inet->cork.fl;
  507. int err = 0;
  508. int is_udplite = IS_UDPLITE(sk);
  509. __wsum csum = 0;
  510. /* Grab the skbuff where UDP header space exists. */
  511. if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
  512. goto out;
  513. /*
  514. * Create a UDP header
  515. */
  516. uh = udp_hdr(skb);
  517. uh->source = fl->fl_ip_sport;
  518. uh->dest = fl->fl_ip_dport;
  519. uh->len = htons(up->len);
  520. uh->check = 0;
  521. if (is_udplite)
  522. csum = udplite_csum_outgoing(sk, skb);
  523. else
  524. csum = udp_csum_outgoing(sk, skb);
  525. /* add protocol-dependent pseudo-header */
  526. uh->check = csum_ipv6_magic(&fl->fl6_src, &fl->fl6_dst,
  527. up->len, fl->proto, csum );
  528. if (uh->check == 0)
  529. uh->check = CSUM_MANGLED_0;
  530. err = ip6_push_pending_frames(sk);
  531. out:
  532. up->len = 0;
  533. up->pending = 0;
  534. if (!err)
  535. UDP6_INC_STATS_USER(sock_net(sk),
  536. UDP_MIB_OUTDATAGRAMS, is_udplite);
  537. return err;
  538. }
  539. int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
  540. struct msghdr *msg, size_t len)
  541. {
  542. struct ipv6_txoptions opt_space;
  543. struct udp_sock *up = udp_sk(sk);
  544. struct inet_sock *inet = inet_sk(sk);
  545. struct ipv6_pinfo *np = inet6_sk(sk);
  546. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
  547. struct in6_addr *daddr, *final_p = NULL, final;
  548. struct ipv6_txoptions *opt = NULL;
  549. struct ip6_flowlabel *flowlabel = NULL;
  550. struct flowi fl;
  551. struct dst_entry *dst;
  552. int addr_len = msg->msg_namelen;
  553. int ulen = len;
  554. int hlimit = -1;
  555. int tclass = -1;
  556. int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
  557. int err;
  558. int connected = 0;
  559. int is_udplite = IS_UDPLITE(sk);
  560. int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
  561. /* destination address check */
  562. if (sin6) {
  563. if (addr_len < offsetof(struct sockaddr, sa_data))
  564. return -EINVAL;
  565. switch (sin6->sin6_family) {
  566. case AF_INET6:
  567. if (addr_len < SIN6_LEN_RFC2133)
  568. return -EINVAL;
  569. daddr = &sin6->sin6_addr;
  570. break;
  571. case AF_INET:
  572. goto do_udp_sendmsg;
  573. case AF_UNSPEC:
  574. msg->msg_name = sin6 = NULL;
  575. msg->msg_namelen = addr_len = 0;
  576. daddr = NULL;
  577. break;
  578. default:
  579. return -EINVAL;
  580. }
  581. } else if (!up->pending) {
  582. if (sk->sk_state != TCP_ESTABLISHED)
  583. return -EDESTADDRREQ;
  584. daddr = &np->daddr;
  585. } else
  586. daddr = NULL;
  587. if (daddr) {
  588. if (ipv6_addr_v4mapped(daddr)) {
  589. struct sockaddr_in sin;
  590. sin.sin_family = AF_INET;
  591. sin.sin_port = sin6 ? sin6->sin6_port : inet->dport;
  592. sin.sin_addr.s_addr = daddr->s6_addr32[3];
  593. msg->msg_name = &sin;
  594. msg->msg_namelen = sizeof(sin);
  595. do_udp_sendmsg:
  596. if (__ipv6_only_sock(sk))
  597. return -ENETUNREACH;
  598. return udp_sendmsg(iocb, sk, msg, len);
  599. }
  600. }
  601. if (up->pending == AF_INET)
  602. return udp_sendmsg(iocb, sk, msg, len);
  603. /* Rough check on arithmetic overflow,
  604. better check is made in ip6_append_data().
  605. */
  606. if (len > INT_MAX - sizeof(struct udphdr))
  607. return -EMSGSIZE;
  608. if (up->pending) {
  609. /*
  610. * There are pending frames.
  611. * The socket lock must be held while it's corked.
  612. */
  613. lock_sock(sk);
  614. if (likely(up->pending)) {
  615. if (unlikely(up->pending != AF_INET6)) {
  616. release_sock(sk);
  617. return -EAFNOSUPPORT;
  618. }
  619. dst = NULL;
  620. goto do_append_data;
  621. }
  622. release_sock(sk);
  623. }
  624. ulen += sizeof(struct udphdr);
  625. memset(&fl, 0, sizeof(fl));
  626. if (sin6) {
  627. if (sin6->sin6_port == 0)
  628. return -EINVAL;
  629. fl.fl_ip_dport = sin6->sin6_port;
  630. daddr = &sin6->sin6_addr;
  631. if (np->sndflow) {
  632. fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
  633. if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
  634. flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
  635. if (flowlabel == NULL)
  636. return -EINVAL;
  637. daddr = &flowlabel->dst;
  638. }
  639. }
  640. /*
  641. * Otherwise it will be difficult to maintain
  642. * sk->sk_dst_cache.
  643. */
  644. if (sk->sk_state == TCP_ESTABLISHED &&
  645. ipv6_addr_equal(daddr, &np->daddr))
  646. daddr = &np->daddr;
  647. if (addr_len >= sizeof(struct sockaddr_in6) &&
  648. sin6->sin6_scope_id &&
  649. ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
  650. fl.oif = sin6->sin6_scope_id;
  651. } else {
  652. if (sk->sk_state != TCP_ESTABLISHED)
  653. return -EDESTADDRREQ;
  654. fl.fl_ip_dport = inet->dport;
  655. daddr = &np->daddr;
  656. fl.fl6_flowlabel = np->flow_label;
  657. connected = 1;
  658. }
  659. if (!fl.oif)
  660. fl.oif = sk->sk_bound_dev_if;
  661. if (msg->msg_controllen) {
  662. opt = &opt_space;
  663. memset(opt, 0, sizeof(struct ipv6_txoptions));
  664. opt->tot_len = sizeof(*opt);
  665. err = datagram_send_ctl(sock_net(sk), msg, &fl, opt, &hlimit, &tclass);
  666. if (err < 0) {
  667. fl6_sock_release(flowlabel);
  668. return err;
  669. }
  670. if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
  671. flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
  672. if (flowlabel == NULL)
  673. return -EINVAL;
  674. }
  675. if (!(opt->opt_nflen|opt->opt_flen))
  676. opt = NULL;
  677. connected = 0;
  678. }
  679. if (opt == NULL)
  680. opt = np->opt;
  681. if (flowlabel)
  682. opt = fl6_merge_options(&opt_space, flowlabel, opt);
  683. opt = ipv6_fixup_options(&opt_space, opt);
  684. fl.proto = sk->sk_protocol;
  685. if (!ipv6_addr_any(daddr))
  686. ipv6_addr_copy(&fl.fl6_dst, daddr);
  687. else
  688. fl.fl6_dst.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
  689. if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr))
  690. ipv6_addr_copy(&fl.fl6_src, &np->saddr);
  691. fl.fl_ip_sport = inet->sport;
  692. /* merge ip6_build_xmit from ip6_output */
  693. if (opt && opt->srcrt) {
  694. struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
  695. ipv6_addr_copy(&final, &fl.fl6_dst);
  696. ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
  697. final_p = &final;
  698. connected = 0;
  699. }
  700. if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst)) {
  701. fl.oif = np->mcast_oif;
  702. connected = 0;
  703. }
  704. security_sk_classify_flow(sk, &fl);
  705. err = ip6_sk_dst_lookup(sk, &dst, &fl);
  706. if (err)
  707. goto out;
  708. if (final_p)
  709. ipv6_addr_copy(&fl.fl6_dst, final_p);
  710. if ((err = __xfrm_lookup(&dst, &fl, sk, XFRM_LOOKUP_WAIT)) < 0) {
  711. if (err == -EREMOTE)
  712. err = ip6_dst_blackhole(sk, &dst, &fl);
  713. if (err < 0)
  714. goto out;
  715. }
  716. if (hlimit < 0) {
  717. if (ipv6_addr_is_multicast(&fl.fl6_dst))
  718. hlimit = np->mcast_hops;
  719. else
  720. hlimit = np->hop_limit;
  721. if (hlimit < 0)
  722. hlimit = ip6_dst_hoplimit(dst);
  723. }
  724. if (tclass < 0) {
  725. tclass = np->tclass;
  726. if (tclass < 0)
  727. tclass = 0;
  728. }
  729. if (msg->msg_flags&MSG_CONFIRM)
  730. goto do_confirm;
  731. back_from_confirm:
  732. lock_sock(sk);
  733. if (unlikely(up->pending)) {
  734. /* The socket is already corked while preparing it. */
  735. /* ... which is an evident application bug. --ANK */
  736. release_sock(sk);
  737. LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
  738. err = -EINVAL;
  739. goto out;
  740. }
  741. up->pending = AF_INET6;
  742. do_append_data:
  743. up->len += ulen;
  744. getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
  745. err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen,
  746. sizeof(struct udphdr), hlimit, tclass, opt, &fl,
  747. (struct rt6_info*)dst,
  748. corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
  749. if (err)
  750. udp_v6_flush_pending_frames(sk);
  751. else if (!corkreq)
  752. err = udp_v6_push_pending_frames(sk);
  753. else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
  754. up->pending = 0;
  755. if (dst) {
  756. if (connected) {
  757. ip6_dst_store(sk, dst,
  758. ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ?
  759. &np->daddr : NULL,
  760. #ifdef CONFIG_IPV6_SUBTREES
  761. ipv6_addr_equal(&fl.fl6_src, &np->saddr) ?
  762. &np->saddr :
  763. #endif
  764. NULL);
  765. } else {
  766. dst_release(dst);
  767. }
  768. dst = NULL;
  769. }
  770. if (err > 0)
  771. err = np->recverr ? net_xmit_errno(err) : 0;
  772. release_sock(sk);
  773. out:
  774. dst_release(dst);
  775. fl6_sock_release(flowlabel);
  776. if (!err)
  777. return len;
  778. /*
  779. * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
  780. * ENOBUFS might not be good (it's not tunable per se), but otherwise
  781. * we don't have a good statistic (IpOutDiscards but it can be too many
  782. * things). We could add another new stat but at least for now that
  783. * seems like overkill.
  784. */
  785. if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
  786. UDP6_INC_STATS_USER(sock_net(sk),
  787. UDP_MIB_SNDBUFERRORS, is_udplite);
  788. }
  789. return err;
  790. do_confirm:
  791. dst_confirm(dst);
  792. if (!(msg->msg_flags&MSG_PROBE) || len)
  793. goto back_from_confirm;
  794. err = 0;
  795. goto out;
  796. }
  797. void udpv6_destroy_sock(struct sock *sk)
  798. {
  799. lock_sock(sk);
  800. udp_v6_flush_pending_frames(sk);
  801. release_sock(sk);
  802. inet6_destroy_sock(sk);
  803. }
  804. /*
  805. * Socket option code for UDP
  806. */
  807. int udpv6_setsockopt(struct sock *sk, int level, int optname,
  808. char __user *optval, int optlen)
  809. {
  810. if (level == SOL_UDP || level == SOL_UDPLITE)
  811. return udp_lib_setsockopt(sk, level, optname, optval, optlen,
  812. udp_v6_push_pending_frames);
  813. return ipv6_setsockopt(sk, level, optname, optval, optlen);
  814. }
  815. #ifdef CONFIG_COMPAT
  816. int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
  817. char __user *optval, int optlen)
  818. {
  819. if (level == SOL_UDP || level == SOL_UDPLITE)
  820. return udp_lib_setsockopt(sk, level, optname, optval, optlen,
  821. udp_v6_push_pending_frames);
  822. return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
  823. }
  824. #endif
  825. int udpv6_getsockopt(struct sock *sk, int level, int optname,
  826. char __user *optval, int __user *optlen)
  827. {
  828. if (level == SOL_UDP || level == SOL_UDPLITE)
  829. return udp_lib_getsockopt(sk, level, optname, optval, optlen);
  830. return ipv6_getsockopt(sk, level, optname, optval, optlen);
  831. }
  832. #ifdef CONFIG_COMPAT
  833. int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
  834. char __user *optval, int __user *optlen)
  835. {
  836. if (level == SOL_UDP || level == SOL_UDPLITE)
  837. return udp_lib_getsockopt(sk, level, optname, optval, optlen);
  838. return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
  839. }
  840. #endif
  841. static struct inet6_protocol udpv6_protocol = {
  842. .handler = udpv6_rcv,
  843. .err_handler = udpv6_err,
  844. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  845. };
  846. /* ------------------------------------------------------------------------ */
  847. #ifdef CONFIG_PROC_FS
  848. static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket)
  849. {
  850. struct inet_sock *inet = inet_sk(sp);
  851. struct ipv6_pinfo *np = inet6_sk(sp);
  852. struct in6_addr *dest, *src;
  853. __u16 destp, srcp;
  854. dest = &np->daddr;
  855. src = &np->rcv_saddr;
  856. destp = ntohs(inet->dport);
  857. srcp = ntohs(inet->sport);
  858. seq_printf(seq,
  859. "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
  860. "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d\n",
  861. bucket,
  862. src->s6_addr32[0], src->s6_addr32[1],
  863. src->s6_addr32[2], src->s6_addr32[3], srcp,
  864. dest->s6_addr32[0], dest->s6_addr32[1],
  865. dest->s6_addr32[2], dest->s6_addr32[3], destp,
  866. sp->sk_state,
  867. atomic_read(&sp->sk_wmem_alloc),
  868. atomic_read(&sp->sk_rmem_alloc),
  869. 0, 0L, 0,
  870. sock_i_uid(sp), 0,
  871. sock_i_ino(sp),
  872. atomic_read(&sp->sk_refcnt), sp,
  873. atomic_read(&sp->sk_drops));
  874. }
  875. int udp6_seq_show(struct seq_file *seq, void *v)
  876. {
  877. if (v == SEQ_START_TOKEN)
  878. seq_printf(seq,
  879. " sl "
  880. "local_address "
  881. "remote_address "
  882. "st tx_queue rx_queue tr tm->when retrnsmt"
  883. " uid timeout inode ref pointer drops\n");
  884. else
  885. udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
  886. return 0;
  887. }
  888. static struct udp_seq_afinfo udp6_seq_afinfo = {
  889. .name = "udp6",
  890. .family = AF_INET6,
  891. .hashtable = udp_hash,
  892. .seq_fops = {
  893. .owner = THIS_MODULE,
  894. },
  895. .seq_ops = {
  896. .show = udp6_seq_show,
  897. },
  898. };
  899. int udp6_proc_init(struct net *net)
  900. {
  901. return udp_proc_register(net, &udp6_seq_afinfo);
  902. }
  903. void udp6_proc_exit(struct net *net) {
  904. udp_proc_unregister(net, &udp6_seq_afinfo);
  905. }
  906. #endif /* CONFIG_PROC_FS */
  907. /* ------------------------------------------------------------------------ */
  908. struct proto udpv6_prot = {
  909. .name = "UDPv6",
  910. .owner = THIS_MODULE,
  911. .close = udp_lib_close,
  912. .connect = ip6_datagram_connect,
  913. .disconnect = udp_disconnect,
  914. .ioctl = udp_ioctl,
  915. .destroy = udpv6_destroy_sock,
  916. .setsockopt = udpv6_setsockopt,
  917. .getsockopt = udpv6_getsockopt,
  918. .sendmsg = udpv6_sendmsg,
  919. .recvmsg = udpv6_recvmsg,
  920. .backlog_rcv = udpv6_queue_rcv_skb,
  921. .hash = udp_lib_hash,
  922. .unhash = udp_lib_unhash,
  923. .get_port = udp_v6_get_port,
  924. .memory_allocated = &udp_memory_allocated,
  925. .sysctl_mem = sysctl_udp_mem,
  926. .sysctl_wmem = &sysctl_udp_wmem_min,
  927. .sysctl_rmem = &sysctl_udp_rmem_min,
  928. .obj_size = sizeof(struct udp6_sock),
  929. .h.udp_hash = udp_hash,
  930. #ifdef CONFIG_COMPAT
  931. .compat_setsockopt = compat_udpv6_setsockopt,
  932. .compat_getsockopt = compat_udpv6_getsockopt,
  933. #endif
  934. };
  935. static struct inet_protosw udpv6_protosw = {
  936. .type = SOCK_DGRAM,
  937. .protocol = IPPROTO_UDP,
  938. .prot = &udpv6_prot,
  939. .ops = &inet6_dgram_ops,
  940. .capability =-1,
  941. .no_check = UDP_CSUM_DEFAULT,
  942. .flags = INET_PROTOSW_PERMANENT,
  943. };
  944. int __init udpv6_init(void)
  945. {
  946. int ret;
  947. ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
  948. if (ret)
  949. goto out;
  950. ret = inet6_register_protosw(&udpv6_protosw);
  951. if (ret)
  952. goto out_udpv6_protocol;
  953. out:
  954. return ret;
  955. out_udpv6_protocol:
  956. inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
  957. goto out;
  958. }
  959. void udpv6_exit(void)
  960. {
  961. inet6_unregister_protosw(&udpv6_protosw);
  962. inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
  963. }