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