| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |  | 
|  | 26 | #include <linux/config.h> | 
|  | 27 | #include <linux/errno.h> | 
|  | 28 | #include <linux/types.h> | 
|  | 29 | #include <linux/socket.h> | 
|  | 30 | #include <linux/sockios.h> | 
|  | 31 | #include <linux/sched.h> | 
|  | 32 | #include <linux/net.h> | 
|  | 33 | #include <linux/in6.h> | 
|  | 34 | #include <linux/netdevice.h> | 
|  | 35 | #include <linux/if_arp.h> | 
|  | 36 | #include <linux/ipv6.h> | 
|  | 37 | #include <linux/icmpv6.h> | 
|  | 38 | #include <linux/init.h> | 
|  | 39 | #include <asm/uaccess.h> | 
|  | 40 |  | 
|  | 41 | #include <net/sock.h> | 
|  | 42 | #include <net/snmp.h> | 
|  | 43 |  | 
|  | 44 | #include <net/ipv6.h> | 
|  | 45 | #include <net/ndisc.h> | 
|  | 46 | #include <net/protocol.h> | 
|  | 47 | #include <net/transp_v6.h> | 
|  | 48 | #include <net/ip6_route.h> | 
|  | 49 | #include <net/addrconf.h> | 
|  | 50 | #include <net/ip.h> | 
|  | 51 | #include <net/udp.h> | 
|  | 52 | #include <net/raw.h> | 
|  | 53 | #include <net/inet_common.h> | 
| Arnaldo Carvalho de Melo | c752f07 | 2005-08-09 20:08:28 -0700 | [diff] [blame] | 54 | #include <net/tcp_states.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 |  | 
|  | 56 | #include <net/ip6_checksum.h> | 
|  | 57 | #include <net/xfrm.h> | 
|  | 58 |  | 
|  | 59 | #include <linux/proc_fs.h> | 
|  | 60 | #include <linux/seq_file.h> | 
|  | 61 |  | 
| Eric Dumazet | ba89966 | 2005-08-26 12:05:31 -0700 | [diff] [blame] | 62 | DEFINE_SNMP_STAT(struct udp_mib, udp_stats_in6) __read_mostly; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 |  | 
|  | 64 | /* Grrr, addr_type already calculated by caller, but I don't want | 
|  | 65 | * to add some silly "cookie" argument to this method just for that. | 
|  | 66 | */ | 
|  | 67 | static int udp_v6_get_port(struct sock *sk, unsigned short snum) | 
|  | 68 | { | 
|  | 69 | struct sock *sk2; | 
|  | 70 | struct hlist_node *node; | 
|  | 71 |  | 
|  | 72 | write_lock_bh(&udp_hash_lock); | 
|  | 73 | if (snum == 0) { | 
|  | 74 | int best_size_so_far, best, result, i; | 
|  | 75 |  | 
|  | 76 | if (udp_port_rover > sysctl_local_port_range[1] || | 
|  | 77 | udp_port_rover < sysctl_local_port_range[0]) | 
|  | 78 | udp_port_rover = sysctl_local_port_range[0]; | 
|  | 79 | best_size_so_far = 32767; | 
|  | 80 | best = result = udp_port_rover; | 
|  | 81 | for (i = 0; i < UDP_HTABLE_SIZE; i++, result++) { | 
|  | 82 | int size; | 
|  | 83 | struct hlist_head *list; | 
|  | 84 |  | 
|  | 85 | list = &udp_hash[result & (UDP_HTABLE_SIZE - 1)]; | 
|  | 86 | if (hlist_empty(list)) { | 
|  | 87 | if (result > sysctl_local_port_range[1]) | 
|  | 88 | result = sysctl_local_port_range[0] + | 
|  | 89 | ((result - sysctl_local_port_range[0]) & | 
|  | 90 | (UDP_HTABLE_SIZE - 1)); | 
|  | 91 | goto gotit; | 
|  | 92 | } | 
|  | 93 | size = 0; | 
|  | 94 | sk_for_each(sk2, node, list) | 
|  | 95 | if (++size >= best_size_so_far) | 
|  | 96 | goto next; | 
|  | 97 | best_size_so_far = size; | 
|  | 98 | best = result; | 
|  | 99 | next:; | 
|  | 100 | } | 
|  | 101 | result = best; | 
|  | 102 | for(;; result += UDP_HTABLE_SIZE) { | 
|  | 103 | if (result > sysctl_local_port_range[1]) | 
|  | 104 | result = sysctl_local_port_range[0] | 
|  | 105 | + ((result - sysctl_local_port_range[0]) & | 
|  | 106 | (UDP_HTABLE_SIZE - 1)); | 
|  | 107 | if (!udp_lport_inuse(result)) | 
|  | 108 | break; | 
|  | 109 | } | 
|  | 110 | gotit: | 
|  | 111 | udp_port_rover = snum = result; | 
|  | 112 | } else { | 
|  | 113 | sk_for_each(sk2, node, | 
|  | 114 | &udp_hash[snum & (UDP_HTABLE_SIZE - 1)]) { | 
|  | 115 | if (inet_sk(sk2)->num == snum && | 
|  | 116 | sk2 != sk && | 
|  | 117 | (!sk2->sk_bound_dev_if || | 
|  | 118 | !sk->sk_bound_dev_if || | 
|  | 119 | sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && | 
|  | 120 | (!sk2->sk_reuse || !sk->sk_reuse) && | 
|  | 121 | ipv6_rcv_saddr_equal(sk, sk2)) | 
|  | 122 | goto fail; | 
|  | 123 | } | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | inet_sk(sk)->num = snum; | 
|  | 127 | if (sk_unhashed(sk)) { | 
|  | 128 | sk_add_node(sk, &udp_hash[snum & (UDP_HTABLE_SIZE - 1)]); | 
|  | 129 | sock_prot_inc_use(sk->sk_prot); | 
|  | 130 | } | 
|  | 131 | write_unlock_bh(&udp_hash_lock); | 
|  | 132 | return 0; | 
|  | 133 |  | 
|  | 134 | fail: | 
|  | 135 | write_unlock_bh(&udp_hash_lock); | 
|  | 136 | return 1; | 
|  | 137 | } | 
|  | 138 |  | 
|  | 139 | static void udp_v6_hash(struct sock *sk) | 
|  | 140 | { | 
|  | 141 | BUG(); | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 | static void udp_v6_unhash(struct sock *sk) | 
|  | 145 | { | 
|  | 146 | write_lock_bh(&udp_hash_lock); | 
|  | 147 | if (sk_del_node_init(sk)) { | 
|  | 148 | inet_sk(sk)->num = 0; | 
|  | 149 | sock_prot_dec_use(sk->sk_prot); | 
|  | 150 | } | 
|  | 151 | write_unlock_bh(&udp_hash_lock); | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | static struct sock *udp_v6_lookup(struct in6_addr *saddr, u16 sport, | 
|  | 155 | struct in6_addr *daddr, u16 dport, int dif) | 
|  | 156 | { | 
|  | 157 | struct sock *sk, *result = NULL; | 
|  | 158 | struct hlist_node *node; | 
|  | 159 | unsigned short hnum = ntohs(dport); | 
|  | 160 | int badness = -1; | 
|  | 161 |  | 
|  | 162 | read_lock(&udp_hash_lock); | 
|  | 163 | sk_for_each(sk, node, &udp_hash[hnum & (UDP_HTABLE_SIZE - 1)]) { | 
|  | 164 | struct inet_sock *inet = inet_sk(sk); | 
|  | 165 |  | 
|  | 166 | if (inet->num == hnum && sk->sk_family == PF_INET6) { | 
|  | 167 | struct ipv6_pinfo *np = inet6_sk(sk); | 
|  | 168 | int score = 0; | 
|  | 169 | if (inet->dport) { | 
|  | 170 | if (inet->dport != sport) | 
|  | 171 | continue; | 
|  | 172 | score++; | 
|  | 173 | } | 
|  | 174 | if (!ipv6_addr_any(&np->rcv_saddr)) { | 
|  | 175 | if (!ipv6_addr_equal(&np->rcv_saddr, daddr)) | 
|  | 176 | continue; | 
|  | 177 | score++; | 
|  | 178 | } | 
|  | 179 | if (!ipv6_addr_any(&np->daddr)) { | 
|  | 180 | if (!ipv6_addr_equal(&np->daddr, saddr)) | 
|  | 181 | continue; | 
|  | 182 | score++; | 
|  | 183 | } | 
|  | 184 | if (sk->sk_bound_dev_if) { | 
|  | 185 | if (sk->sk_bound_dev_if != dif) | 
|  | 186 | continue; | 
|  | 187 | score++; | 
|  | 188 | } | 
|  | 189 | if(score == 4) { | 
|  | 190 | result = sk; | 
|  | 191 | break; | 
|  | 192 | } else if(score > badness) { | 
|  | 193 | result = sk; | 
|  | 194 | badness = score; | 
|  | 195 | } | 
|  | 196 | } | 
|  | 197 | } | 
|  | 198 | if (result) | 
|  | 199 | sock_hold(result); | 
|  | 200 | read_unlock(&udp_hash_lock); | 
|  | 201 | return result; | 
|  | 202 | } | 
|  | 203 |  | 
|  | 204 | /* | 
|  | 205 | * | 
|  | 206 | */ | 
|  | 207 |  | 
|  | 208 | static void udpv6_close(struct sock *sk, long timeout) | 
|  | 209 | { | 
|  | 210 | sk_common_release(sk); | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | /* | 
|  | 214 | * 	This should be easy, if there is something there we | 
|  | 215 | * 	return it, otherwise we block. | 
|  | 216 | */ | 
|  | 217 |  | 
|  | 218 | static int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk, | 
|  | 219 | struct msghdr *msg, size_t len, | 
|  | 220 | int noblock, int flags, int *addr_len) | 
|  | 221 | { | 
|  | 222 | struct ipv6_pinfo *np = inet6_sk(sk); | 
|  | 223 | struct inet_sock *inet = inet_sk(sk); | 
|  | 224 | struct sk_buff *skb; | 
|  | 225 | size_t copied; | 
|  | 226 | int err; | 
|  | 227 |  | 
|  | 228 | if (addr_len) | 
|  | 229 | *addr_len=sizeof(struct sockaddr_in6); | 
|  | 230 |  | 
|  | 231 | if (flags & MSG_ERRQUEUE) | 
|  | 232 | return ipv6_recv_error(sk, msg, len); | 
|  | 233 |  | 
|  | 234 | try_again: | 
|  | 235 | skb = skb_recv_datagram(sk, flags, noblock, &err); | 
|  | 236 | if (!skb) | 
|  | 237 | goto out; | 
|  | 238 |  | 
|  | 239 | copied = skb->len - sizeof(struct udphdr); | 
|  | 240 | if (copied > len) { | 
|  | 241 | copied = len; | 
|  | 242 | msg->msg_flags |= MSG_TRUNC; | 
|  | 243 | } | 
|  | 244 |  | 
|  | 245 | if (skb->ip_summed==CHECKSUM_UNNECESSARY) { | 
|  | 246 | err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov, | 
|  | 247 | copied); | 
|  | 248 | } else if (msg->msg_flags&MSG_TRUNC) { | 
|  | 249 | if ((unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum))) | 
|  | 250 | goto csum_copy_err; | 
|  | 251 | err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov, | 
|  | 252 | copied); | 
|  | 253 | } else { | 
|  | 254 | err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov); | 
|  | 255 | if (err == -EINVAL) | 
|  | 256 | goto csum_copy_err; | 
|  | 257 | } | 
|  | 258 | if (err) | 
|  | 259 | goto out_free; | 
|  | 260 |  | 
|  | 261 | sock_recv_timestamp(msg, sk, skb); | 
|  | 262 |  | 
|  | 263 | /* Copy the address. */ | 
|  | 264 | if (msg->msg_name) { | 
|  | 265 | struct sockaddr_in6 *sin6; | 
|  | 266 |  | 
|  | 267 | sin6 = (struct sockaddr_in6 *) msg->msg_name; | 
|  | 268 | sin6->sin6_family = AF_INET6; | 
|  | 269 | sin6->sin6_port = skb->h.uh->source; | 
|  | 270 | sin6->sin6_flowinfo = 0; | 
|  | 271 | sin6->sin6_scope_id = 0; | 
|  | 272 |  | 
|  | 273 | if (skb->protocol == htons(ETH_P_IP)) | 
|  | 274 | ipv6_addr_set(&sin6->sin6_addr, 0, 0, | 
|  | 275 | htonl(0xffff), skb->nh.iph->saddr); | 
|  | 276 | else { | 
|  | 277 | ipv6_addr_copy(&sin6->sin6_addr, &skb->nh.ipv6h->saddr); | 
|  | 278 | if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) | 
|  | 279 | sin6->sin6_scope_id = IP6CB(skb)->iif; | 
|  | 280 | } | 
|  | 281 |  | 
|  | 282 | } | 
|  | 283 | if (skb->protocol == htons(ETH_P_IP)) { | 
|  | 284 | if (inet->cmsg_flags) | 
|  | 285 | ip_cmsg_recv(msg, skb); | 
|  | 286 | } else { | 
|  | 287 | if (np->rxopt.all) | 
|  | 288 | datagram_recv_ctl(sk, msg, skb); | 
|  | 289 | } | 
|  | 290 |  | 
|  | 291 | err = copied; | 
|  | 292 | if (flags & MSG_TRUNC) | 
|  | 293 | err = skb->len - sizeof(struct udphdr); | 
|  | 294 |  | 
|  | 295 | out_free: | 
|  | 296 | skb_free_datagram(sk, skb); | 
|  | 297 | out: | 
|  | 298 | return err; | 
|  | 299 |  | 
|  | 300 | csum_copy_err: | 
|  | 301 | /* Clear queue. */ | 
|  | 302 | if (flags&MSG_PEEK) { | 
|  | 303 | int clear = 0; | 
| Herbert Xu | e0f9f85 | 2005-06-18 22:56:18 -0700 | [diff] [blame] | 304 | spin_lock_bh(&sk->sk_receive_queue.lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | if (skb == skb_peek(&sk->sk_receive_queue)) { | 
|  | 306 | __skb_unlink(skb, &sk->sk_receive_queue); | 
|  | 307 | clear = 1; | 
|  | 308 | } | 
| Herbert Xu | e0f9f85 | 2005-06-18 22:56:18 -0700 | [diff] [blame] | 309 | spin_unlock_bh(&sk->sk_receive_queue.lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | if (clear) | 
|  | 311 | kfree_skb(skb); | 
|  | 312 | } | 
|  | 313 |  | 
|  | 314 | skb_free_datagram(sk, skb); | 
|  | 315 |  | 
|  | 316 | if (flags & MSG_DONTWAIT) { | 
|  | 317 | UDP6_INC_STATS_USER(UDP_MIB_INERRORS); | 
|  | 318 | return -EAGAIN; | 
|  | 319 | } | 
|  | 320 | goto try_again; | 
|  | 321 | } | 
|  | 322 |  | 
|  | 323 | static void udpv6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, | 
|  | 324 | int type, int code, int offset, __u32 info) | 
|  | 325 | { | 
|  | 326 | struct ipv6_pinfo *np; | 
|  | 327 | struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data; | 
|  | 328 | struct net_device *dev = skb->dev; | 
|  | 329 | struct in6_addr *saddr = &hdr->saddr; | 
|  | 330 | struct in6_addr *daddr = &hdr->daddr; | 
|  | 331 | struct udphdr *uh = (struct udphdr*)(skb->data+offset); | 
|  | 332 | struct sock *sk; | 
|  | 333 | int err; | 
|  | 334 |  | 
|  | 335 | sk = udp_v6_lookup(daddr, uh->dest, saddr, uh->source, dev->ifindex); | 
|  | 336 |  | 
|  | 337 | if (sk == NULL) | 
|  | 338 | return; | 
|  | 339 |  | 
|  | 340 | np = inet6_sk(sk); | 
|  | 341 |  | 
|  | 342 | if (!icmpv6_err_convert(type, code, &err) && !np->recverr) | 
|  | 343 | goto out; | 
|  | 344 |  | 
|  | 345 | if (sk->sk_state != TCP_ESTABLISHED && !np->recverr) | 
|  | 346 | goto out; | 
|  | 347 |  | 
|  | 348 | if (np->recverr) | 
|  | 349 | ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1)); | 
|  | 350 |  | 
|  | 351 | sk->sk_err = err; | 
|  | 352 | sk->sk_error_report(sk); | 
|  | 353 | out: | 
|  | 354 | sock_put(sk); | 
|  | 355 | } | 
|  | 356 |  | 
|  | 357 | static inline int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb) | 
|  | 358 | { | 
|  | 359 | if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) { | 
|  | 360 | kfree_skb(skb); | 
|  | 361 | return -1; | 
|  | 362 | } | 
|  | 363 |  | 
|  | 364 | if (skb->ip_summed != CHECKSUM_UNNECESSARY) { | 
|  | 365 | if ((unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum))) { | 
|  | 366 | UDP6_INC_STATS_BH(UDP_MIB_INERRORS); | 
|  | 367 | kfree_skb(skb); | 
|  | 368 | return 0; | 
|  | 369 | } | 
|  | 370 | skb->ip_summed = CHECKSUM_UNNECESSARY; | 
|  | 371 | } | 
|  | 372 |  | 
|  | 373 | if (sock_queue_rcv_skb(sk,skb)<0) { | 
|  | 374 | UDP6_INC_STATS_BH(UDP_MIB_INERRORS); | 
|  | 375 | kfree_skb(skb); | 
|  | 376 | return 0; | 
|  | 377 | } | 
|  | 378 | UDP6_INC_STATS_BH(UDP_MIB_INDATAGRAMS); | 
|  | 379 | return 0; | 
|  | 380 | } | 
|  | 381 |  | 
|  | 382 | static struct sock *udp_v6_mcast_next(struct sock *sk, | 
|  | 383 | u16 loc_port, struct in6_addr *loc_addr, | 
|  | 384 | u16 rmt_port, struct in6_addr *rmt_addr, | 
|  | 385 | int dif) | 
|  | 386 | { | 
|  | 387 | struct hlist_node *node; | 
|  | 388 | struct sock *s = sk; | 
|  | 389 | unsigned short num = ntohs(loc_port); | 
|  | 390 |  | 
|  | 391 | sk_for_each_from(s, node) { | 
|  | 392 | struct inet_sock *inet = inet_sk(s); | 
|  | 393 |  | 
|  | 394 | if (inet->num == num && s->sk_family == PF_INET6) { | 
|  | 395 | struct ipv6_pinfo *np = inet6_sk(s); | 
|  | 396 | if (inet->dport) { | 
|  | 397 | if (inet->dport != rmt_port) | 
|  | 398 | continue; | 
|  | 399 | } | 
|  | 400 | if (!ipv6_addr_any(&np->daddr) && | 
|  | 401 | !ipv6_addr_equal(&np->daddr, rmt_addr)) | 
|  | 402 | continue; | 
|  | 403 |  | 
|  | 404 | if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif) | 
|  | 405 | continue; | 
|  | 406 |  | 
|  | 407 | if (!ipv6_addr_any(&np->rcv_saddr)) { | 
| David L Stevens | 40796c5 | 2005-09-14 21:10:20 -0700 | [diff] [blame] | 408 | if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr)) | 
|  | 409 | continue; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | } | 
|  | 411 | if(!inet6_mc_check(s, loc_addr, rmt_addr)) | 
|  | 412 | continue; | 
|  | 413 | return s; | 
|  | 414 | } | 
|  | 415 | } | 
|  | 416 | return NULL; | 
|  | 417 | } | 
|  | 418 |  | 
|  | 419 | /* | 
|  | 420 | * Note: called only from the BH handler context, | 
|  | 421 | * so we don't need to lock the hashes. | 
|  | 422 | */ | 
|  | 423 | static void udpv6_mcast_deliver(struct udphdr *uh, | 
|  | 424 | struct in6_addr *saddr, struct in6_addr *daddr, | 
|  | 425 | struct sk_buff *skb) | 
|  | 426 | { | 
|  | 427 | struct sock *sk, *sk2; | 
|  | 428 | int dif; | 
|  | 429 |  | 
|  | 430 | read_lock(&udp_hash_lock); | 
|  | 431 | sk = sk_head(&udp_hash[ntohs(uh->dest) & (UDP_HTABLE_SIZE - 1)]); | 
|  | 432 | dif = skb->dev->ifindex; | 
|  | 433 | sk = udp_v6_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif); | 
|  | 434 | if (!sk) { | 
|  | 435 | kfree_skb(skb); | 
|  | 436 | goto out; | 
|  | 437 | } | 
|  | 438 |  | 
|  | 439 | sk2 = sk; | 
|  | 440 | while ((sk2 = udp_v6_mcast_next(sk_next(sk2), uh->dest, daddr, | 
|  | 441 | uh->source, saddr, dif))) { | 
|  | 442 | struct sk_buff *buff = skb_clone(skb, GFP_ATOMIC); | 
|  | 443 | if (buff) | 
|  | 444 | udpv6_queue_rcv_skb(sk2, buff); | 
|  | 445 | } | 
|  | 446 | udpv6_queue_rcv_skb(sk, skb); | 
|  | 447 | out: | 
|  | 448 | read_unlock(&udp_hash_lock); | 
|  | 449 | } | 
|  | 450 |  | 
|  | 451 | static int udpv6_rcv(struct sk_buff **pskb, unsigned int *nhoffp) | 
|  | 452 | { | 
|  | 453 | struct sk_buff *skb = *pskb; | 
|  | 454 | struct sock *sk; | 
|  | 455 | struct udphdr *uh; | 
|  | 456 | struct net_device *dev = skb->dev; | 
|  | 457 | struct in6_addr *saddr, *daddr; | 
|  | 458 | u32 ulen = 0; | 
|  | 459 |  | 
|  | 460 | if (!pskb_may_pull(skb, sizeof(struct udphdr))) | 
|  | 461 | goto short_packet; | 
|  | 462 |  | 
|  | 463 | saddr = &skb->nh.ipv6h->saddr; | 
|  | 464 | daddr = &skb->nh.ipv6h->daddr; | 
|  | 465 | uh = skb->h.uh; | 
|  | 466 |  | 
|  | 467 | ulen = ntohs(uh->len); | 
|  | 468 |  | 
|  | 469 | /* Check for jumbo payload */ | 
|  | 470 | if (ulen == 0) | 
|  | 471 | ulen = skb->len; | 
|  | 472 |  | 
|  | 473 | if (ulen > skb->len || ulen < sizeof(*uh)) | 
|  | 474 | goto short_packet; | 
|  | 475 |  | 
|  | 476 | if (uh->check == 0) { | 
|  | 477 | /* RFC 2460 section 8.1 says that we SHOULD log | 
|  | 478 | this error. Well, it is reasonable. | 
|  | 479 | */ | 
| Patrick McHardy | 64ce207 | 2005-08-09 20:50:53 -0700 | [diff] [blame] | 480 | LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | goto discard; | 
|  | 482 | } | 
|  | 483 |  | 
|  | 484 | if (ulen < skb->len) { | 
| Stephen Hemminger | 42ca89c | 2005-09-08 12:57:43 -0700 | [diff] [blame] | 485 | if (pskb_trim_rcsum(skb, ulen)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | goto discard; | 
|  | 487 | saddr = &skb->nh.ipv6h->saddr; | 
|  | 488 | daddr = &skb->nh.ipv6h->daddr; | 
|  | 489 | uh = skb->h.uh; | 
|  | 490 | } | 
|  | 491 |  | 
|  | 492 | if (skb->ip_summed==CHECKSUM_HW) { | 
|  | 493 | skb->ip_summed = CHECKSUM_UNNECESSARY; | 
|  | 494 | if (csum_ipv6_magic(saddr, daddr, ulen, IPPROTO_UDP, skb->csum)) { | 
| Patrick McHardy | 64ce207 | 2005-08-09 20:50:53 -0700 | [diff] [blame] | 495 | LIMIT_NETDEBUG(KERN_DEBUG "udp v6 hw csum failure.\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | skb->ip_summed = CHECKSUM_NONE; | 
|  | 497 | } | 
|  | 498 | } | 
|  | 499 | if (skb->ip_summed != CHECKSUM_UNNECESSARY) | 
|  | 500 | skb->csum = ~csum_ipv6_magic(saddr, daddr, ulen, IPPROTO_UDP, 0); | 
|  | 501 |  | 
|  | 502 | /* | 
|  | 503 | *	Multicast receive code | 
|  | 504 | */ | 
|  | 505 | if (ipv6_addr_is_multicast(daddr)) { | 
|  | 506 | udpv6_mcast_deliver(uh, saddr, daddr, skb); | 
|  | 507 | return 0; | 
|  | 508 | } | 
|  | 509 |  | 
|  | 510 | /* Unicast */ | 
|  | 511 |  | 
|  | 512 | /* | 
|  | 513 | * check socket cache ... must talk to Alan about his plans | 
|  | 514 | * for sock caches... i'll skip this for now. | 
|  | 515 | */ | 
|  | 516 | sk = udp_v6_lookup(saddr, uh->source, daddr, uh->dest, dev->ifindex); | 
|  | 517 |  | 
|  | 518 | if (sk == NULL) { | 
|  | 519 | if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) | 
|  | 520 | goto discard; | 
|  | 521 |  | 
|  | 522 | if (skb->ip_summed != CHECKSUM_UNNECESSARY && | 
|  | 523 | (unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum))) | 
|  | 524 | goto discard; | 
|  | 525 | UDP6_INC_STATS_BH(UDP_MIB_NOPORTS); | 
|  | 526 |  | 
|  | 527 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, dev); | 
|  | 528 |  | 
|  | 529 | kfree_skb(skb); | 
|  | 530 | return(0); | 
|  | 531 | } | 
|  | 532 |  | 
|  | 533 | /* deliver */ | 
|  | 534 |  | 
|  | 535 | udpv6_queue_rcv_skb(sk, skb); | 
|  | 536 | sock_put(sk); | 
|  | 537 | return(0); | 
|  | 538 |  | 
|  | 539 | short_packet: | 
|  | 540 | if (net_ratelimit()) | 
|  | 541 | printk(KERN_DEBUG "UDP: short packet: %d/%u\n", ulen, skb->len); | 
|  | 542 |  | 
|  | 543 | discard: | 
|  | 544 | UDP6_INC_STATS_BH(UDP_MIB_INERRORS); | 
|  | 545 | kfree_skb(skb); | 
|  | 546 | return(0); | 
|  | 547 | } | 
|  | 548 | /* | 
|  | 549 | * Throw away all pending data and cancel the corking. Socket is locked. | 
|  | 550 | */ | 
|  | 551 | static void udp_v6_flush_pending_frames(struct sock *sk) | 
|  | 552 | { | 
|  | 553 | struct udp_sock *up = udp_sk(sk); | 
|  | 554 |  | 
|  | 555 | if (up->pending) { | 
|  | 556 | up->len = 0; | 
|  | 557 | up->pending = 0; | 
|  | 558 | ip6_flush_pending_frames(sk); | 
|  | 559 | } | 
|  | 560 | } | 
|  | 561 |  | 
|  | 562 | /* | 
|  | 563 | *	Sending | 
|  | 564 | */ | 
|  | 565 |  | 
|  | 566 | static int udp_v6_push_pending_frames(struct sock *sk, struct udp_sock *up) | 
|  | 567 | { | 
|  | 568 | struct sk_buff *skb; | 
|  | 569 | struct udphdr *uh; | 
|  | 570 | struct inet_sock *inet = inet_sk(sk); | 
|  | 571 | struct flowi *fl = &inet->cork.fl; | 
|  | 572 | int err = 0; | 
|  | 573 |  | 
|  | 574 | /* Grab the skbuff where UDP header space exists. */ | 
|  | 575 | if ((skb = skb_peek(&sk->sk_write_queue)) == NULL) | 
|  | 576 | goto out; | 
|  | 577 |  | 
|  | 578 | /* | 
|  | 579 | * Create a UDP header | 
|  | 580 | */ | 
|  | 581 | uh = skb->h.uh; | 
|  | 582 | uh->source = fl->fl_ip_sport; | 
|  | 583 | uh->dest = fl->fl_ip_dport; | 
|  | 584 | uh->len = htons(up->len); | 
|  | 585 | uh->check = 0; | 
|  | 586 |  | 
|  | 587 | if (sk->sk_no_check == UDP_CSUM_NOXMIT) { | 
|  | 588 | skb->ip_summed = CHECKSUM_NONE; | 
|  | 589 | goto send; | 
|  | 590 | } | 
|  | 591 |  | 
|  | 592 | if (skb_queue_len(&sk->sk_write_queue) == 1) { | 
|  | 593 | skb->csum = csum_partial((char *)uh, | 
|  | 594 | sizeof(struct udphdr), skb->csum); | 
|  | 595 | uh->check = csum_ipv6_magic(&fl->fl6_src, | 
|  | 596 | &fl->fl6_dst, | 
|  | 597 | up->len, fl->proto, skb->csum); | 
|  | 598 | } else { | 
|  | 599 | u32 tmp_csum = 0; | 
|  | 600 |  | 
|  | 601 | skb_queue_walk(&sk->sk_write_queue, skb) { | 
|  | 602 | tmp_csum = csum_add(tmp_csum, skb->csum); | 
|  | 603 | } | 
|  | 604 | tmp_csum = csum_partial((char *)uh, | 
|  | 605 | sizeof(struct udphdr), tmp_csum); | 
|  | 606 | tmp_csum = csum_ipv6_magic(&fl->fl6_src, | 
|  | 607 | &fl->fl6_dst, | 
|  | 608 | up->len, fl->proto, tmp_csum); | 
|  | 609 | uh->check = tmp_csum; | 
|  | 610 |  | 
|  | 611 | } | 
|  | 612 | if (uh->check == 0) | 
|  | 613 | uh->check = -1; | 
|  | 614 |  | 
|  | 615 | send: | 
|  | 616 | err = ip6_push_pending_frames(sk); | 
|  | 617 | out: | 
|  | 618 | up->len = 0; | 
|  | 619 | up->pending = 0; | 
|  | 620 | return err; | 
|  | 621 | } | 
|  | 622 |  | 
|  | 623 | static int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk, | 
|  | 624 | struct msghdr *msg, size_t len) | 
|  | 625 | { | 
|  | 626 | struct ipv6_txoptions opt_space; | 
|  | 627 | struct udp_sock *up = udp_sk(sk); | 
|  | 628 | struct inet_sock *inet = inet_sk(sk); | 
|  | 629 | struct ipv6_pinfo *np = inet6_sk(sk); | 
|  | 630 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name; | 
|  | 631 | struct in6_addr *daddr, *final_p = NULL, final; | 
|  | 632 | struct ipv6_txoptions *opt = NULL; | 
|  | 633 | struct ip6_flowlabel *flowlabel = NULL; | 
|  | 634 | struct flowi *fl = &inet->cork.fl; | 
|  | 635 | struct dst_entry *dst; | 
|  | 636 | int addr_len = msg->msg_namelen; | 
|  | 637 | int ulen = len; | 
|  | 638 | int hlimit = -1; | 
| YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 639 | int tclass = -1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | int corkreq = up->corkflag || msg->msg_flags&MSG_MORE; | 
|  | 641 | int err; | 
| Mitsuru KANDA | 987905d | 2005-09-18 00:30:08 -0700 | [diff] [blame] | 642 | int connected = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 |  | 
|  | 644 | /* destination address check */ | 
|  | 645 | if (sin6) { | 
|  | 646 | if (addr_len < offsetof(struct sockaddr, sa_data)) | 
|  | 647 | return -EINVAL; | 
|  | 648 |  | 
|  | 649 | switch (sin6->sin6_family) { | 
|  | 650 | case AF_INET6: | 
|  | 651 | if (addr_len < SIN6_LEN_RFC2133) | 
|  | 652 | return -EINVAL; | 
|  | 653 | daddr = &sin6->sin6_addr; | 
|  | 654 | break; | 
|  | 655 | case AF_INET: | 
|  | 656 | goto do_udp_sendmsg; | 
|  | 657 | case AF_UNSPEC: | 
|  | 658 | msg->msg_name = sin6 = NULL; | 
|  | 659 | msg->msg_namelen = addr_len = 0; | 
|  | 660 | daddr = NULL; | 
|  | 661 | break; | 
|  | 662 | default: | 
|  | 663 | return -EINVAL; | 
|  | 664 | } | 
|  | 665 | } else if (!up->pending) { | 
|  | 666 | if (sk->sk_state != TCP_ESTABLISHED) | 
|  | 667 | return -EDESTADDRREQ; | 
|  | 668 | daddr = &np->daddr; | 
|  | 669 | } else | 
|  | 670 | daddr = NULL; | 
|  | 671 |  | 
|  | 672 | if (daddr) { | 
|  | 673 | if (ipv6_addr_type(daddr) == IPV6_ADDR_MAPPED) { | 
|  | 674 | struct sockaddr_in sin; | 
|  | 675 | sin.sin_family = AF_INET; | 
|  | 676 | sin.sin_port = sin6 ? sin6->sin6_port : inet->dport; | 
|  | 677 | sin.sin_addr.s_addr = daddr->s6_addr32[3]; | 
|  | 678 | msg->msg_name = &sin; | 
|  | 679 | msg->msg_namelen = sizeof(sin); | 
|  | 680 | do_udp_sendmsg: | 
|  | 681 | if (__ipv6_only_sock(sk)) | 
|  | 682 | return -ENETUNREACH; | 
|  | 683 | return udp_sendmsg(iocb, sk, msg, len); | 
|  | 684 | } | 
|  | 685 | } | 
|  | 686 |  | 
|  | 687 | if (up->pending == AF_INET) | 
|  | 688 | return udp_sendmsg(iocb, sk, msg, len); | 
|  | 689 |  | 
|  | 690 | /* Rough check on arithmetic overflow, | 
|  | 691 | better check is made in ip6_build_xmit | 
|  | 692 | */ | 
|  | 693 | if (len > INT_MAX - sizeof(struct udphdr)) | 
|  | 694 | return -EMSGSIZE; | 
|  | 695 |  | 
|  | 696 | if (up->pending) { | 
|  | 697 | /* | 
|  | 698 | * There are pending frames. | 
|  | 699 | * The socket lock must be held while it's corked. | 
|  | 700 | */ | 
|  | 701 | lock_sock(sk); | 
|  | 702 | if (likely(up->pending)) { | 
|  | 703 | if (unlikely(up->pending != AF_INET6)) { | 
|  | 704 | release_sock(sk); | 
|  | 705 | return -EAFNOSUPPORT; | 
|  | 706 | } | 
|  | 707 | dst = NULL; | 
|  | 708 | goto do_append_data; | 
|  | 709 | } | 
|  | 710 | release_sock(sk); | 
|  | 711 | } | 
|  | 712 | ulen += sizeof(struct udphdr); | 
|  | 713 |  | 
|  | 714 | memset(fl, 0, sizeof(*fl)); | 
|  | 715 |  | 
|  | 716 | if (sin6) { | 
|  | 717 | if (sin6->sin6_port == 0) | 
|  | 718 | return -EINVAL; | 
|  | 719 |  | 
|  | 720 | fl->fl_ip_dport = sin6->sin6_port; | 
|  | 721 | daddr = &sin6->sin6_addr; | 
|  | 722 |  | 
|  | 723 | if (np->sndflow) { | 
|  | 724 | fl->fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK; | 
|  | 725 | if (fl->fl6_flowlabel&IPV6_FLOWLABEL_MASK) { | 
|  | 726 | flowlabel = fl6_sock_lookup(sk, fl->fl6_flowlabel); | 
|  | 727 | if (flowlabel == NULL) | 
|  | 728 | return -EINVAL; | 
|  | 729 | daddr = &flowlabel->dst; | 
|  | 730 | } | 
|  | 731 | } | 
|  | 732 |  | 
|  | 733 | /* | 
|  | 734 | * Otherwise it will be difficult to maintain | 
|  | 735 | * sk->sk_dst_cache. | 
|  | 736 | */ | 
|  | 737 | if (sk->sk_state == TCP_ESTABLISHED && | 
|  | 738 | ipv6_addr_equal(daddr, &np->daddr)) | 
|  | 739 | daddr = &np->daddr; | 
|  | 740 |  | 
|  | 741 | if (addr_len >= sizeof(struct sockaddr_in6) && | 
|  | 742 | sin6->sin6_scope_id && | 
|  | 743 | ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL) | 
|  | 744 | fl->oif = sin6->sin6_scope_id; | 
|  | 745 | } else { | 
|  | 746 | if (sk->sk_state != TCP_ESTABLISHED) | 
|  | 747 | return -EDESTADDRREQ; | 
|  | 748 |  | 
|  | 749 | fl->fl_ip_dport = inet->dport; | 
|  | 750 | daddr = &np->daddr; | 
|  | 751 | fl->fl6_flowlabel = np->flow_label; | 
| Mitsuru KANDA | 987905d | 2005-09-18 00:30:08 -0700 | [diff] [blame] | 752 | connected = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | } | 
|  | 754 |  | 
|  | 755 | if (!fl->oif) | 
|  | 756 | fl->oif = sk->sk_bound_dev_if; | 
|  | 757 |  | 
|  | 758 | if (msg->msg_controllen) { | 
|  | 759 | opt = &opt_space; | 
|  | 760 | memset(opt, 0, sizeof(struct ipv6_txoptions)); | 
|  | 761 | opt->tot_len = sizeof(*opt); | 
|  | 762 |  | 
| YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 763 | err = datagram_send_ctl(msg, fl, opt, &hlimit, &tclass); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | if (err < 0) { | 
|  | 765 | fl6_sock_release(flowlabel); | 
|  | 766 | return err; | 
|  | 767 | } | 
|  | 768 | if ((fl->fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) { | 
|  | 769 | flowlabel = fl6_sock_lookup(sk, fl->fl6_flowlabel); | 
|  | 770 | if (flowlabel == NULL) | 
|  | 771 | return -EINVAL; | 
|  | 772 | } | 
|  | 773 | if (!(opt->opt_nflen|opt->opt_flen)) | 
|  | 774 | opt = NULL; | 
| Mitsuru KANDA | 987905d | 2005-09-18 00:30:08 -0700 | [diff] [blame] | 775 | connected = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | } | 
|  | 777 | if (opt == NULL) | 
|  | 778 | opt = np->opt; | 
| YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 779 | opt = fl6_merge_options(&opt_space, flowlabel, opt); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 |  | 
|  | 781 | fl->proto = IPPROTO_UDP; | 
|  | 782 | ipv6_addr_copy(&fl->fl6_dst, daddr); | 
|  | 783 | if (ipv6_addr_any(&fl->fl6_src) && !ipv6_addr_any(&np->saddr)) | 
|  | 784 | ipv6_addr_copy(&fl->fl6_src, &np->saddr); | 
|  | 785 | fl->fl_ip_sport = inet->sport; | 
|  | 786 |  | 
|  | 787 | /* merge ip6_build_xmit from ip6_output */ | 
|  | 788 | if (opt && opt->srcrt) { | 
|  | 789 | struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt; | 
|  | 790 | ipv6_addr_copy(&final, &fl->fl6_dst); | 
|  | 791 | ipv6_addr_copy(&fl->fl6_dst, rt0->addr); | 
|  | 792 | final_p = &final; | 
| Mitsuru KANDA | 987905d | 2005-09-18 00:30:08 -0700 | [diff] [blame] | 793 | connected = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | } | 
|  | 795 |  | 
| Mitsuru KANDA | 987905d | 2005-09-18 00:30:08 -0700 | [diff] [blame] | 796 | if (!fl->oif && ipv6_addr_is_multicast(&fl->fl6_dst)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | fl->oif = np->mcast_oif; | 
| Mitsuru KANDA | 987905d | 2005-09-18 00:30:08 -0700 | [diff] [blame] | 798 | connected = 0; | 
|  | 799 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 |  | 
|  | 801 | err = ip6_dst_lookup(sk, &dst, fl); | 
|  | 802 | if (err) | 
|  | 803 | goto out; | 
|  | 804 | if (final_p) | 
|  | 805 | ipv6_addr_copy(&fl->fl6_dst, final_p); | 
|  | 806 |  | 
| Patrick McHardy | e104411 | 2005-09-08 15:11:55 -0700 | [diff] [blame] | 807 | if ((err = xfrm_lookup(&dst, fl, sk, 0)) < 0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 |  | 
|  | 810 | if (hlimit < 0) { | 
|  | 811 | if (ipv6_addr_is_multicast(&fl->fl6_dst)) | 
|  | 812 | hlimit = np->mcast_hops; | 
|  | 813 | else | 
|  | 814 | hlimit = np->hop_limit; | 
|  | 815 | if (hlimit < 0) | 
|  | 816 | hlimit = dst_metric(dst, RTAX_HOPLIMIT); | 
|  | 817 | if (hlimit < 0) | 
|  | 818 | hlimit = ipv6_get_hoplimit(dst->dev); | 
|  | 819 | } | 
|  | 820 |  | 
| YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 821 | if (tclass < 0) { | 
|  | 822 | tclass = np->tclass; | 
|  | 823 | if (tclass < 0) | 
|  | 824 | tclass = 0; | 
|  | 825 | } | 
|  | 826 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | if (msg->msg_flags&MSG_CONFIRM) | 
|  | 828 | goto do_confirm; | 
|  | 829 | back_from_confirm: | 
|  | 830 |  | 
|  | 831 | lock_sock(sk); | 
|  | 832 | if (unlikely(up->pending)) { | 
|  | 833 | /* The socket is already corked while preparing it. */ | 
|  | 834 | /* ... which is an evident application bug. --ANK */ | 
|  | 835 | release_sock(sk); | 
|  | 836 |  | 
| Patrick McHardy | 64ce207 | 2005-08-09 20:50:53 -0700 | [diff] [blame] | 837 | LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | err = -EINVAL; | 
|  | 839 | goto out; | 
|  | 840 | } | 
|  | 841 |  | 
|  | 842 | up->pending = AF_INET6; | 
|  | 843 |  | 
|  | 844 | do_append_data: | 
|  | 845 | up->len += ulen; | 
| YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 846 | err = ip6_append_data(sk, ip_generic_getfrag, msg->msg_iov, ulen, | 
|  | 847 | sizeof(struct udphdr), hlimit, tclass, opt, fl, | 
|  | 848 | (struct rt6_info*)dst, | 
|  | 849 | corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | if (err) | 
|  | 851 | udp_v6_flush_pending_frames(sk); | 
|  | 852 | else if (!corkreq) | 
|  | 853 | err = udp_v6_push_pending_frames(sk, up); | 
|  | 854 |  | 
| David S. Miller | a5e7c21 | 2005-10-03 14:21:58 -0700 | [diff] [blame] | 855 | if (dst) { | 
|  | 856 | if (connected) { | 
|  | 857 | ip6_dst_store(sk, dst, | 
|  | 858 | ipv6_addr_equal(&fl->fl6_dst, &np->daddr) ? | 
|  | 859 | &np->daddr : NULL); | 
|  | 860 | } else { | 
|  | 861 | dst_release(dst); | 
|  | 862 | } | 
|  | 863 | } | 
|  | 864 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | if (err > 0) | 
|  | 866 | err = np->recverr ? net_xmit_errno(err) : 0; | 
|  | 867 | release_sock(sk); | 
|  | 868 | out: | 
|  | 869 | fl6_sock_release(flowlabel); | 
|  | 870 | if (!err) { | 
|  | 871 | UDP6_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS); | 
|  | 872 | return len; | 
|  | 873 | } | 
|  | 874 | return err; | 
|  | 875 |  | 
|  | 876 | do_confirm: | 
|  | 877 | dst_confirm(dst); | 
|  | 878 | if (!(msg->msg_flags&MSG_PROBE) || len) | 
|  | 879 | goto back_from_confirm; | 
|  | 880 | err = 0; | 
|  | 881 | goto out; | 
|  | 882 | } | 
|  | 883 |  | 
|  | 884 | static int udpv6_destroy_sock(struct sock *sk) | 
|  | 885 | { | 
|  | 886 | lock_sock(sk); | 
|  | 887 | udp_v6_flush_pending_frames(sk); | 
|  | 888 | release_sock(sk); | 
|  | 889 |  | 
|  | 890 | inet6_destroy_sock(sk); | 
|  | 891 |  | 
|  | 892 | return 0; | 
|  | 893 | } | 
|  | 894 |  | 
|  | 895 | /* | 
|  | 896 | *	Socket option code for UDP | 
|  | 897 | */ | 
|  | 898 | static int udpv6_setsockopt(struct sock *sk, int level, int optname, | 
|  | 899 | char __user *optval, int optlen) | 
|  | 900 | { | 
|  | 901 | struct udp_sock *up = udp_sk(sk); | 
|  | 902 | int val; | 
|  | 903 | int err = 0; | 
|  | 904 |  | 
|  | 905 | if (level != SOL_UDP) | 
|  | 906 | return ipv6_setsockopt(sk, level, optname, optval, optlen); | 
|  | 907 |  | 
|  | 908 | if(optlen<sizeof(int)) | 
|  | 909 | return -EINVAL; | 
|  | 910 |  | 
|  | 911 | if (get_user(val, (int __user *)optval)) | 
|  | 912 | return -EFAULT; | 
|  | 913 |  | 
|  | 914 | switch(optname) { | 
|  | 915 | case UDP_CORK: | 
|  | 916 | if (val != 0) { | 
|  | 917 | up->corkflag = 1; | 
|  | 918 | } else { | 
|  | 919 | up->corkflag = 0; | 
|  | 920 | lock_sock(sk); | 
|  | 921 | udp_v6_push_pending_frames(sk, up); | 
|  | 922 | release_sock(sk); | 
|  | 923 | } | 
|  | 924 | break; | 
|  | 925 |  | 
|  | 926 | case UDP_ENCAP: | 
|  | 927 | switch (val) { | 
|  | 928 | case 0: | 
|  | 929 | up->encap_type = val; | 
|  | 930 | break; | 
|  | 931 | default: | 
|  | 932 | err = -ENOPROTOOPT; | 
|  | 933 | break; | 
|  | 934 | } | 
|  | 935 | break; | 
|  | 936 |  | 
|  | 937 | default: | 
|  | 938 | err = -ENOPROTOOPT; | 
|  | 939 | break; | 
|  | 940 | }; | 
|  | 941 |  | 
|  | 942 | return err; | 
|  | 943 | } | 
|  | 944 |  | 
|  | 945 | static int udpv6_getsockopt(struct sock *sk, int level, int optname, | 
|  | 946 | char __user *optval, int __user *optlen) | 
|  | 947 | { | 
|  | 948 | struct udp_sock *up = udp_sk(sk); | 
|  | 949 | int val, len; | 
|  | 950 |  | 
|  | 951 | if (level != SOL_UDP) | 
|  | 952 | return ipv6_getsockopt(sk, level, optname, optval, optlen); | 
|  | 953 |  | 
|  | 954 | if(get_user(len,optlen)) | 
|  | 955 | return -EFAULT; | 
|  | 956 |  | 
|  | 957 | len = min_t(unsigned int, len, sizeof(int)); | 
|  | 958 |  | 
|  | 959 | if(len < 0) | 
|  | 960 | return -EINVAL; | 
|  | 961 |  | 
|  | 962 | switch(optname) { | 
|  | 963 | case UDP_CORK: | 
|  | 964 | val = up->corkflag; | 
|  | 965 | break; | 
|  | 966 |  | 
|  | 967 | case UDP_ENCAP: | 
|  | 968 | val = up->encap_type; | 
|  | 969 | break; | 
|  | 970 |  | 
|  | 971 | default: | 
|  | 972 | return -ENOPROTOOPT; | 
|  | 973 | }; | 
|  | 974 |  | 
|  | 975 | if(put_user(len, optlen)) | 
|  | 976 | return -EFAULT; | 
|  | 977 | if(copy_to_user(optval, &val,len)) | 
|  | 978 | return -EFAULT; | 
|  | 979 | return 0; | 
|  | 980 | } | 
|  | 981 |  | 
|  | 982 | static struct inet6_protocol udpv6_protocol = { | 
|  | 983 | .handler	=	udpv6_rcv, | 
|  | 984 | .err_handler	=	udpv6_err, | 
|  | 985 | .flags		=	INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, | 
|  | 986 | }; | 
|  | 987 |  | 
|  | 988 | /* ------------------------------------------------------------------------ */ | 
|  | 989 | #ifdef CONFIG_PROC_FS | 
|  | 990 |  | 
|  | 991 | static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket) | 
|  | 992 | { | 
|  | 993 | struct inet_sock *inet = inet_sk(sp); | 
|  | 994 | struct ipv6_pinfo *np = inet6_sk(sp); | 
|  | 995 | struct in6_addr *dest, *src; | 
|  | 996 | __u16 destp, srcp; | 
|  | 997 |  | 
|  | 998 | dest  = &np->daddr; | 
|  | 999 | src   = &np->rcv_saddr; | 
|  | 1000 | destp = ntohs(inet->dport); | 
|  | 1001 | srcp  = ntohs(inet->sport); | 
|  | 1002 | seq_printf(seq, | 
|  | 1003 | "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X " | 
|  | 1004 | "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p\n", | 
|  | 1005 | bucket, | 
|  | 1006 | src->s6_addr32[0], src->s6_addr32[1], | 
|  | 1007 | src->s6_addr32[2], src->s6_addr32[3], srcp, | 
|  | 1008 | dest->s6_addr32[0], dest->s6_addr32[1], | 
|  | 1009 | dest->s6_addr32[2], dest->s6_addr32[3], destp, | 
|  | 1010 | sp->sk_state, | 
|  | 1011 | atomic_read(&sp->sk_wmem_alloc), | 
|  | 1012 | atomic_read(&sp->sk_rmem_alloc), | 
|  | 1013 | 0, 0L, 0, | 
|  | 1014 | sock_i_uid(sp), 0, | 
|  | 1015 | sock_i_ino(sp), | 
|  | 1016 | atomic_read(&sp->sk_refcnt), sp); | 
|  | 1017 | } | 
|  | 1018 |  | 
|  | 1019 | static int udp6_seq_show(struct seq_file *seq, void *v) | 
|  | 1020 | { | 
|  | 1021 | if (v == SEQ_START_TOKEN) | 
|  | 1022 | seq_printf(seq, | 
|  | 1023 | "  sl  " | 
|  | 1024 | "local_address                         " | 
|  | 1025 | "remote_address                        " | 
|  | 1026 | "st tx_queue rx_queue tr tm->when retrnsmt" | 
|  | 1027 | "   uid  timeout inode\n"); | 
|  | 1028 | else | 
|  | 1029 | udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket); | 
|  | 1030 | return 0; | 
|  | 1031 | } | 
|  | 1032 |  | 
|  | 1033 | static struct file_operations udp6_seq_fops; | 
|  | 1034 | static struct udp_seq_afinfo udp6_seq_afinfo = { | 
|  | 1035 | .owner		= THIS_MODULE, | 
|  | 1036 | .name		= "udp6", | 
|  | 1037 | .family		= AF_INET6, | 
|  | 1038 | .seq_show	= udp6_seq_show, | 
|  | 1039 | .seq_fops	= &udp6_seq_fops, | 
|  | 1040 | }; | 
|  | 1041 |  | 
|  | 1042 | int __init udp6_proc_init(void) | 
|  | 1043 | { | 
|  | 1044 | return udp_proc_register(&udp6_seq_afinfo); | 
|  | 1045 | } | 
|  | 1046 |  | 
|  | 1047 | void udp6_proc_exit(void) { | 
|  | 1048 | udp_proc_unregister(&udp6_seq_afinfo); | 
|  | 1049 | } | 
|  | 1050 | #endif /* CONFIG_PROC_FS */ | 
|  | 1051 |  | 
|  | 1052 | /* ------------------------------------------------------------------------ */ | 
|  | 1053 |  | 
|  | 1054 | struct proto udpv6_prot = { | 
|  | 1055 | .name =		"UDPv6", | 
|  | 1056 | .owner =	THIS_MODULE, | 
|  | 1057 | .close =	udpv6_close, | 
|  | 1058 | .connect =	ip6_datagram_connect, | 
|  | 1059 | .disconnect =	udp_disconnect, | 
|  | 1060 | .ioctl =	udp_ioctl, | 
|  | 1061 | .destroy =	udpv6_destroy_sock, | 
|  | 1062 | .setsockopt =	udpv6_setsockopt, | 
|  | 1063 | .getsockopt =	udpv6_getsockopt, | 
|  | 1064 | .sendmsg =	udpv6_sendmsg, | 
|  | 1065 | .recvmsg =	udpv6_recvmsg, | 
|  | 1066 | .backlog_rcv =	udpv6_queue_rcv_skb, | 
|  | 1067 | .hash =		udp_v6_hash, | 
|  | 1068 | .unhash =	udp_v6_unhash, | 
|  | 1069 | .get_port =	udp_v6_get_port, | 
|  | 1070 | .obj_size =	sizeof(struct udp6_sock), | 
|  | 1071 | }; | 
|  | 1072 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | static struct inet_protosw udpv6_protosw = { | 
|  | 1074 | .type =      SOCK_DGRAM, | 
|  | 1075 | .protocol =  IPPROTO_UDP, | 
|  | 1076 | .prot =      &udpv6_prot, | 
|  | 1077 | .ops =       &inet6_dgram_ops, | 
|  | 1078 | .capability =-1, | 
|  | 1079 | .no_check =  UDP_CSUM_DEFAULT, | 
|  | 1080 | .flags =     INET_PROTOSW_PERMANENT, | 
|  | 1081 | }; | 
|  | 1082 |  | 
|  | 1083 |  | 
|  | 1084 | void __init udpv6_init(void) | 
|  | 1085 | { | 
|  | 1086 | if (inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP) < 0) | 
|  | 1087 | printk(KERN_ERR "udpv6_init: Could not register protocol\n"); | 
|  | 1088 | inet6_register_protosw(&udpv6_protosw); | 
|  | 1089 | } |