udp.c 26 KB

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