Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * RAW sockets for IPv6 |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3 | * Linux INET6 implementation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * Authors: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 6 | * Pedro Roque <roque@di.fc.ul.pt> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
| 8 | * Adapted from linux/net/ipv4/raw.c |
| 9 | * |
| 10 | * $Id: raw.c,v 1.51 2002/02/01 22:01:04 davem Exp $ |
| 11 | * |
| 12 | * Fixes: |
| 13 | * Hideaki YOSHIFUJI : sin6_scope_id support |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 14 | * YOSHIFUJI,H.@USAGI : raw checksum (RFC2292(bis) compliance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data |
| 16 | * |
| 17 | * This program is free software; you can redistribute it and/or |
| 18 | * modify it under the terms of the GNU General Public License |
| 19 | * as published by the Free Software Foundation; either version |
| 20 | * 2 of the License, or (at your option) any later version. |
| 21 | */ |
| 22 | |
| 23 | #include <linux/errno.h> |
| 24 | #include <linux/types.h> |
| 25 | #include <linux/socket.h> |
| 26 | #include <linux/sockios.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/net.h> |
| 28 | #include <linux/in6.h> |
| 29 | #include <linux/netdevice.h> |
| 30 | #include <linux/if_arp.h> |
| 31 | #include <linux/icmpv6.h> |
| 32 | #include <linux/netfilter.h> |
| 33 | #include <linux/netfilter_ipv6.h> |
Herbert Xu | 3305b80 | 2005-12-13 23:16:37 -0800 | [diff] [blame] | 34 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <asm/uaccess.h> |
| 36 | #include <asm/ioctls.h> |
| 37 | |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 38 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <net/ip.h> |
| 40 | #include <net/sock.h> |
| 41 | #include <net/snmp.h> |
| 42 | |
| 43 | #include <net/ipv6.h> |
| 44 | #include <net/ndisc.h> |
| 45 | #include <net/protocol.h> |
| 46 | #include <net/ip6_route.h> |
| 47 | #include <net/ip6_checksum.h> |
| 48 | #include <net/addrconf.h> |
| 49 | #include <net/transp_v6.h> |
| 50 | #include <net/udp.h> |
| 51 | #include <net/inet_common.h> |
Arnaldo Carvalho de Melo | c752f07 | 2005-08-09 20:08:28 -0700 | [diff] [blame] | 52 | #include <net/tcp_states.h> |
Masahide NAKAMURA | 59fbb3a | 2007-06-26 23:56:32 -0700 | [diff] [blame] | 53 | #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) |
Masahide NAKAMURA | 7be96f7 | 2006-08-23 20:35:31 -0700 | [diff] [blame] | 54 | #include <net/mip6.h> |
| 55 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Pavel Emelyanov | b673e4d | 2007-11-19 22:36:45 -0800 | [diff] [blame] | 57 | #include <net/raw.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | #include <net/rawv6.h> |
| 59 | #include <net/xfrm.h> |
| 60 | |
| 61 | #include <linux/proc_fs.h> |
| 62 | #include <linux/seq_file.h> |
| 63 | |
Pavel Emelyanov | b673e4d | 2007-11-19 22:36:45 -0800 | [diff] [blame] | 64 | static struct raw_hashinfo raw_v6_hashinfo = { |
| 65 | .lock = __RW_LOCK_UNLOCKED(), |
| 66 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | static void raw_v6_hash(struct sock *sk) |
| 69 | { |
Pavel Emelyanov | 65b4c50 | 2007-11-19 22:37:24 -0800 | [diff] [blame] | 70 | raw_hash_sk(sk, &raw_v6_hashinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static void raw_v6_unhash(struct sock *sk) |
| 74 | { |
Pavel Emelyanov | ab70768 | 2007-11-19 22:37:58 -0800 | [diff] [blame] | 75 | raw_unhash_sk(sk, &raw_v6_hashinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | |
Pavel Emelyanov | 69d6da0 | 2007-11-19 22:35:57 -0800 | [diff] [blame] | 79 | static struct sock *__raw_v6_lookup(struct sock *sk, unsigned short num, |
| 80 | struct in6_addr *loc_addr, struct in6_addr *rmt_addr, int dif) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { |
| 82 | struct hlist_node *node; |
| 83 | int is_multicast = ipv6_addr_is_multicast(loc_addr); |
| 84 | |
| 85 | sk_for_each_from(sk, node) |
| 86 | if (inet_sk(sk)->num == num) { |
| 87 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 88 | |
| 89 | if (!ipv6_addr_any(&np->daddr) && |
| 90 | !ipv6_addr_equal(&np->daddr, rmt_addr)) |
| 91 | continue; |
| 92 | |
Andrew McDonald | 0bd1b59 | 2005-08-09 19:44:42 -0700 | [diff] [blame] | 93 | if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif) |
| 94 | continue; |
| 95 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | if (!ipv6_addr_any(&np->rcv_saddr)) { |
| 97 | if (ipv6_addr_equal(&np->rcv_saddr, loc_addr)) |
| 98 | goto found; |
| 99 | if (is_multicast && |
| 100 | inet6_mc_check(sk, loc_addr, rmt_addr)) |
| 101 | goto found; |
| 102 | continue; |
| 103 | } |
| 104 | goto found; |
| 105 | } |
| 106 | sk = NULL; |
| 107 | found: |
| 108 | return sk; |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * 0 - deliver |
| 113 | * 1 - block |
| 114 | */ |
| 115 | static __inline__ int icmpv6_filter(struct sock *sk, struct sk_buff *skb) |
| 116 | { |
| 117 | struct icmp6hdr *icmph; |
| 118 | struct raw6_sock *rp = raw6_sk(sk); |
| 119 | |
| 120 | if (pskb_may_pull(skb, sizeof(struct icmp6hdr))) { |
| 121 | __u32 *data = &rp->filter.data[0]; |
| 122 | int bit_nr; |
| 123 | |
| 124 | icmph = (struct icmp6hdr *) skb->data; |
| 125 | bit_nr = icmph->icmp6_type; |
| 126 | |
| 127 | return (data[bit_nr >> 5] & (1 << (bit_nr & 31))) != 0; |
| 128 | } |
| 129 | return 0; |
| 130 | } |
| 131 | |
Masahide NAKAMURA | 59fbb3a | 2007-06-26 23:56:32 -0700 | [diff] [blame] | 132 | #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) |
| 133 | static int (*mh_filter)(struct sock *sock, struct sk_buff *skb); |
| 134 | |
| 135 | int rawv6_mh_filter_register(int (*filter)(struct sock *sock, |
| 136 | struct sk_buff *skb)) |
| 137 | { |
| 138 | rcu_assign_pointer(mh_filter, filter); |
| 139 | return 0; |
| 140 | } |
| 141 | EXPORT_SYMBOL(rawv6_mh_filter_register); |
| 142 | |
| 143 | int rawv6_mh_filter_unregister(int (*filter)(struct sock *sock, |
| 144 | struct sk_buff *skb)) |
| 145 | { |
| 146 | rcu_assign_pointer(mh_filter, NULL); |
| 147 | synchronize_rcu(); |
| 148 | return 0; |
| 149 | } |
| 150 | EXPORT_SYMBOL(rawv6_mh_filter_unregister); |
| 151 | |
| 152 | #endif |
| 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | /* |
| 155 | * demultiplex raw sockets. |
| 156 | * (should consider queueing the skb in the sock receive_queue |
| 157 | * without calling rawv6.c) |
| 158 | * |
| 159 | * Caller owns SKB so we must make clones. |
| 160 | */ |
Pavel Emelyanov | 69d6da0 | 2007-11-19 22:35:57 -0800 | [diff] [blame] | 161 | static int ipv6_raw_deliver(struct sk_buff *skb, int nexthdr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | { |
| 163 | struct in6_addr *saddr; |
| 164 | struct in6_addr *daddr; |
| 165 | struct sock *sk; |
Patrick McHardy | d13964f | 2005-08-09 19:45:02 -0700 | [diff] [blame] | 166 | int delivered = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | __u8 hash; |
| 168 | |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 169 | saddr = &ipv6_hdr(skb)->saddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | daddr = saddr + 1; |
| 171 | |
| 172 | hash = nexthdr & (MAX_INET_PROTOS - 1); |
| 173 | |
Pavel Emelyanov | b673e4d | 2007-11-19 22:36:45 -0800 | [diff] [blame] | 174 | read_lock(&raw_v6_hashinfo.lock); |
| 175 | sk = sk_head(&raw_v6_hashinfo.ht[hash]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
| 177 | /* |
| 178 | * The first socket found will be delivered after |
| 179 | * delivery to transport protocols. |
| 180 | */ |
| 181 | |
| 182 | if (sk == NULL) |
| 183 | goto out; |
| 184 | |
YOSHIFUJI Hideaki | 2dac4b9 | 2005-09-01 17:44:49 -0700 | [diff] [blame] | 185 | sk = __raw_v6_lookup(sk, nexthdr, daddr, saddr, IP6CB(skb)->iif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
| 187 | while (sk) { |
Masahide NAKAMURA | 7be96f7 | 2006-08-23 20:35:31 -0700 | [diff] [blame] | 188 | int filtered; |
| 189 | |
Patrick McHardy | d13964f | 2005-08-09 19:45:02 -0700 | [diff] [blame] | 190 | delivered = 1; |
Masahide NAKAMURA | 7be96f7 | 2006-08-23 20:35:31 -0700 | [diff] [blame] | 191 | switch (nexthdr) { |
| 192 | case IPPROTO_ICMPV6: |
| 193 | filtered = icmpv6_filter(sk, skb); |
| 194 | break; |
Masahide NAKAMURA | 59fbb3a | 2007-06-26 23:56:32 -0700 | [diff] [blame] | 195 | |
| 196 | #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) |
Masahide NAKAMURA | 7be96f7 | 2006-08-23 20:35:31 -0700 | [diff] [blame] | 197 | case IPPROTO_MH: |
Masahide NAKAMURA | 59fbb3a | 2007-06-26 23:56:32 -0700 | [diff] [blame] | 198 | { |
Masahide NAKAMURA | 7be96f7 | 2006-08-23 20:35:31 -0700 | [diff] [blame] | 199 | /* XXX: To validate MH only once for each packet, |
| 200 | * this is placed here. It should be after checking |
| 201 | * xfrm policy, however it doesn't. The checking xfrm |
| 202 | * policy is placed in rawv6_rcv() because it is |
| 203 | * required for each socket. |
| 204 | */ |
Masahide NAKAMURA | 59fbb3a | 2007-06-26 23:56:32 -0700 | [diff] [blame] | 205 | int (*filter)(struct sock *sock, struct sk_buff *skb); |
| 206 | |
| 207 | filter = rcu_dereference(mh_filter); |
| 208 | filtered = filter ? filter(sk, skb) : 0; |
Masahide NAKAMURA | 7be96f7 | 2006-08-23 20:35:31 -0700 | [diff] [blame] | 209 | break; |
Masahide NAKAMURA | 59fbb3a | 2007-06-26 23:56:32 -0700 | [diff] [blame] | 210 | } |
Masahide NAKAMURA | 7be96f7 | 2006-08-23 20:35:31 -0700 | [diff] [blame] | 211 | #endif |
| 212 | default: |
| 213 | filtered = 0; |
| 214 | break; |
| 215 | } |
| 216 | |
| 217 | if (filtered < 0) |
| 218 | break; |
| 219 | if (filtered == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC); |
| 221 | |
| 222 | /* Not releasing hash table! */ |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 223 | if (clone) { |
| 224 | nf_reset(clone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | rawv6_rcv(sk, clone); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 226 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | } |
Andrew McDonald | 0bd1b59 | 2005-08-09 19:44:42 -0700 | [diff] [blame] | 228 | sk = __raw_v6_lookup(sk_next(sk), nexthdr, daddr, saddr, |
YOSHIFUJI Hideaki | 2dac4b9 | 2005-09-01 17:44:49 -0700 | [diff] [blame] | 229 | IP6CB(skb)->iif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | } |
| 231 | out: |
Pavel Emelyanov | b673e4d | 2007-11-19 22:36:45 -0800 | [diff] [blame] | 232 | read_unlock(&raw_v6_hashinfo.lock); |
Patrick McHardy | d13964f | 2005-08-09 19:45:02 -0700 | [diff] [blame] | 233 | return delivered; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Pavel Emelyanov | 69d6da0 | 2007-11-19 22:35:57 -0800 | [diff] [blame] | 236 | int raw6_local_deliver(struct sk_buff *skb, int nexthdr) |
| 237 | { |
| 238 | struct sock *raw_sk; |
| 239 | |
Pavel Emelyanov | b673e4d | 2007-11-19 22:36:45 -0800 | [diff] [blame] | 240 | raw_sk = sk_head(&raw_v6_hashinfo.ht[nexthdr & (MAX_INET_PROTOS - 1)]); |
Pavel Emelyanov | 69d6da0 | 2007-11-19 22:35:57 -0800 | [diff] [blame] | 241 | if (raw_sk && !ipv6_raw_deliver(skb, nexthdr)) |
| 242 | raw_sk = NULL; |
| 243 | |
| 244 | return raw_sk != NULL; |
| 245 | } |
| 246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | /* This cleans up af_inet6 a bit. -DaveM */ |
| 248 | static int rawv6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len) |
| 249 | { |
| 250 | struct inet_sock *inet = inet_sk(sk); |
| 251 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 252 | struct sockaddr_in6 *addr = (struct sockaddr_in6 *) uaddr; |
Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 253 | __be32 v4addr = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | int addr_type; |
| 255 | int err; |
| 256 | |
| 257 | if (addr_len < SIN6_LEN_RFC2133) |
| 258 | return -EINVAL; |
| 259 | addr_type = ipv6_addr_type(&addr->sin6_addr); |
| 260 | |
| 261 | /* Raw sockets are IPv6 only */ |
| 262 | if (addr_type == IPV6_ADDR_MAPPED) |
| 263 | return(-EADDRNOTAVAIL); |
| 264 | |
| 265 | lock_sock(sk); |
| 266 | |
| 267 | err = -EINVAL; |
| 268 | if (sk->sk_state != TCP_CLOSE) |
| 269 | goto out; |
| 270 | |
| 271 | /* Check if the address belongs to the host. */ |
| 272 | if (addr_type != IPV6_ADDR_ANY) { |
| 273 | struct net_device *dev = NULL; |
| 274 | |
| 275 | if (addr_type & IPV6_ADDR_LINKLOCAL) { |
| 276 | if (addr_len >= sizeof(struct sockaddr_in6) && |
| 277 | addr->sin6_scope_id) { |
| 278 | /* Override any existing binding, if another |
| 279 | * one is supplied by user. |
| 280 | */ |
| 281 | sk->sk_bound_dev_if = addr->sin6_scope_id; |
| 282 | } |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 283 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | /* Binding to link-local address requires an interface */ |
| 285 | if (!sk->sk_bound_dev_if) |
| 286 | goto out; |
| 287 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 288 | dev = dev_get_by_index(&init_net, sk->sk_bound_dev_if); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | if (!dev) { |
| 290 | err = -ENODEV; |
| 291 | goto out; |
| 292 | } |
| 293 | } |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | /* ipv4 addr of the socket is invalid. Only the |
| 296 | * unspecified and mapped address have a v4 equivalent. |
| 297 | */ |
| 298 | v4addr = LOOPBACK4_IPV6; |
| 299 | if (!(addr_type & IPV6_ADDR_MULTICAST)) { |
| 300 | err = -EADDRNOTAVAIL; |
| 301 | if (!ipv6_chk_addr(&addr->sin6_addr, dev, 0)) { |
| 302 | if (dev) |
| 303 | dev_put(dev); |
| 304 | goto out; |
| 305 | } |
| 306 | } |
| 307 | if (dev) |
| 308 | dev_put(dev); |
| 309 | } |
| 310 | |
| 311 | inet->rcv_saddr = inet->saddr = v4addr; |
| 312 | ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr); |
| 313 | if (!(addr_type & IPV6_ADDR_MULTICAST)) |
| 314 | ipv6_addr_copy(&np->saddr, &addr->sin6_addr); |
| 315 | err = 0; |
| 316 | out: |
| 317 | release_sock(sk); |
| 318 | return err; |
| 319 | } |
| 320 | |
Pavel Emelyanov | 69d6da0 | 2007-11-19 22:35:57 -0800 | [diff] [blame] | 321 | static void rawv6_err(struct sock *sk, struct sk_buff *skb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | struct inet6_skb_parm *opt, |
Al Viro | 04ce690 | 2006-11-08 00:21:01 -0800 | [diff] [blame] | 323 | int type, int code, int offset, __be32 info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | { |
| 325 | struct inet_sock *inet = inet_sk(sk); |
| 326 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 327 | int err; |
| 328 | int harderr; |
| 329 | |
| 330 | /* Report error on raw socket, if: |
| 331 | 1. User requested recverr. |
| 332 | 2. Socket is connected (otherwise the error indication |
| 333 | is useless without recverr and error is hard. |
| 334 | */ |
| 335 | if (!np->recverr && sk->sk_state != TCP_ESTABLISHED) |
| 336 | return; |
| 337 | |
| 338 | harderr = icmpv6_err_convert(type, code, &err); |
| 339 | if (type == ICMPV6_PKT_TOOBIG) |
| 340 | harderr = (np->pmtudisc == IPV6_PMTUDISC_DO); |
| 341 | |
| 342 | if (np->recverr) { |
| 343 | u8 *payload = skb->data; |
| 344 | if (!inet->hdrincl) |
| 345 | payload += offset; |
| 346 | ipv6_icmp_error(sk, skb, err, 0, ntohl(info), payload); |
| 347 | } |
| 348 | |
| 349 | if (np->recverr || harderr) { |
| 350 | sk->sk_err = err; |
| 351 | sk->sk_error_report(sk); |
| 352 | } |
| 353 | } |
| 354 | |
Pavel Emelyanov | 69d6da0 | 2007-11-19 22:35:57 -0800 | [diff] [blame] | 355 | void raw6_icmp_error(struct sk_buff *skb, int nexthdr, |
| 356 | int type, int code, int inner_offset, __be32 info) |
| 357 | { |
| 358 | struct sock *sk; |
| 359 | int hash; |
| 360 | struct in6_addr *saddr, *daddr; |
| 361 | |
Pavel Emelyanov | b673e4d | 2007-11-19 22:36:45 -0800 | [diff] [blame] | 362 | hash = nexthdr & (RAW_HTABLE_SIZE - 1); |
Pavel Emelyanov | 69d6da0 | 2007-11-19 22:35:57 -0800 | [diff] [blame] | 363 | |
Pavel Emelyanov | b673e4d | 2007-11-19 22:36:45 -0800 | [diff] [blame] | 364 | read_lock(&raw_v6_hashinfo.lock); |
| 365 | sk = sk_head(&raw_v6_hashinfo.ht[hash]); |
Pavel Emelyanov | 69d6da0 | 2007-11-19 22:35:57 -0800 | [diff] [blame] | 366 | if (sk != NULL) { |
| 367 | saddr = &ipv6_hdr(skb)->saddr; |
| 368 | daddr = &ipv6_hdr(skb)->daddr; |
| 369 | |
| 370 | while ((sk = __raw_v6_lookup(sk, nexthdr, saddr, daddr, |
| 371 | IP6CB(skb)->iif))) { |
| 372 | rawv6_err(sk, skb, NULL, type, code, |
| 373 | inner_offset, info); |
| 374 | sk = sk_next(sk); |
| 375 | } |
| 376 | } |
Pavel Emelyanov | b673e4d | 2007-11-19 22:36:45 -0800 | [diff] [blame] | 377 | read_unlock(&raw_v6_hashinfo.lock); |
Pavel Emelyanov | 69d6da0 | 2007-11-19 22:35:57 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | static inline int rawv6_rcv_skb(struct sock * sk, struct sk_buff * skb) |
| 381 | { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 382 | if ((raw6_sk(sk)->checksum || sk->sk_filter) && |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 383 | skb_checksum_complete(skb)) { |
Wang Chen | a92aa31 | 2007-11-13 20:31:14 -0800 | [diff] [blame] | 384 | atomic_inc(&sk->sk_drops); |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 385 | kfree_skb(skb); |
| 386 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | /* Charge it to the socket. */ |
| 390 | if (sock_queue_rcv_skb(sk,skb)<0) { |
Wang Chen | a92aa31 | 2007-11-13 20:31:14 -0800 | [diff] [blame] | 391 | atomic_inc(&sk->sk_drops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | kfree_skb(skb); |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | /* |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 400 | * This is next to useless... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | * if we demultiplex in network layer we don't need the extra call |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 402 | * just to queue the skb... |
| 403 | * maybe we could have the network decide upon a hint if it |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | * should call raw_rcv for demultiplexing |
| 405 | */ |
| 406 | int rawv6_rcv(struct sock *sk, struct sk_buff *skb) |
| 407 | { |
| 408 | struct inet_sock *inet = inet_sk(sk); |
| 409 | struct raw6_sock *rp = raw6_sk(sk); |
| 410 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 411 | if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) { |
Wang Chen | a92aa31 | 2007-11-13 20:31:14 -0800 | [diff] [blame] | 412 | atomic_inc(&sk->sk_drops); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 413 | kfree_skb(skb); |
| 414 | return NET_RX_DROP; |
| 415 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
| 417 | if (!rp->checksum) |
| 418 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 419 | |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 420 | if (skb->ip_summed == CHECKSUM_COMPLETE) { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 421 | skb_postpull_rcsum(skb, skb_network_header(skb), |
Arnaldo Carvalho de Melo | cfe1fc7 | 2007-03-16 17:26:39 -0300 | [diff] [blame] | 422 | skb_network_header_len(skb)); |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 423 | if (!csum_ipv6_magic(&ipv6_hdr(skb)->saddr, |
| 424 | &ipv6_hdr(skb)->daddr, |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 425 | skb->len, inet->num, skb->csum)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | } |
Herbert Xu | 6047637 | 2007-04-09 11:59:39 -0700 | [diff] [blame] | 428 | if (!skb_csum_unnecessary(skb)) |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 429 | skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr, |
| 430 | &ipv6_hdr(skb)->daddr, |
| 431 | skb->len, |
| 432 | inet->num, 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
| 434 | if (inet->hdrincl) { |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 435 | if (skb_checksum_complete(skb)) { |
Wang Chen | a92aa31 | 2007-11-13 20:31:14 -0800 | [diff] [blame] | 436 | atomic_inc(&sk->sk_drops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | kfree_skb(skb); |
| 438 | return 0; |
| 439 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | rawv6_rcv_skb(sk, skb); |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | |
| 447 | /* |
| 448 | * This should be easy, if there is something there |
| 449 | * we return it, otherwise we block. |
| 450 | */ |
| 451 | |
| 452 | static int rawv6_recvmsg(struct kiocb *iocb, struct sock *sk, |
| 453 | struct msghdr *msg, size_t len, |
| 454 | int noblock, int flags, int *addr_len) |
| 455 | { |
| 456 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 457 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)msg->msg_name; |
| 458 | struct sk_buff *skb; |
| 459 | size_t copied; |
| 460 | int err; |
| 461 | |
| 462 | if (flags & MSG_OOB) |
| 463 | return -EOPNOTSUPP; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 464 | |
| 465 | if (addr_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | *addr_len=sizeof(*sin6); |
| 467 | |
| 468 | if (flags & MSG_ERRQUEUE) |
| 469 | return ipv6_recv_error(sk, msg, len); |
| 470 | |
| 471 | skb = skb_recv_datagram(sk, flags, noblock, &err); |
| 472 | if (!skb) |
| 473 | goto out; |
| 474 | |
| 475 | copied = skb->len; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 476 | if (copied > len) { |
| 477 | copied = len; |
| 478 | msg->msg_flags |= MSG_TRUNC; |
| 479 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
Herbert Xu | 6047637 | 2007-04-09 11:59:39 -0700 | [diff] [blame] | 481 | if (skb_csum_unnecessary(skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); |
| 483 | } else if (msg->msg_flags&MSG_TRUNC) { |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 484 | if (__skb_checksum_complete(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | goto csum_copy_err; |
| 486 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); |
| 487 | } else { |
| 488 | err = skb_copy_and_csum_datagram_iovec(skb, 0, msg->msg_iov); |
| 489 | if (err == -EINVAL) |
| 490 | goto csum_copy_err; |
| 491 | } |
| 492 | if (err) |
| 493 | goto out_free; |
| 494 | |
| 495 | /* Copy the address. */ |
| 496 | if (sin6) { |
| 497 | sin6->sin6_family = AF_INET6; |
Tetsuo Handa | f59fc7f | 2006-07-25 17:05:35 -0700 | [diff] [blame] | 498 | sin6->sin6_port = 0; |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 499 | ipv6_addr_copy(&sin6->sin6_addr, &ipv6_hdr(skb)->saddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | sin6->sin6_flowinfo = 0; |
| 501 | sin6->sin6_scope_id = 0; |
| 502 | if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) |
| 503 | sin6->sin6_scope_id = IP6CB(skb)->iif; |
| 504 | } |
| 505 | |
| 506 | sock_recv_timestamp(msg, sk, skb); |
| 507 | |
| 508 | if (np->rxopt.all) |
| 509 | datagram_recv_ctl(sk, msg, skb); |
| 510 | |
| 511 | err = copied; |
| 512 | if (flags & MSG_TRUNC) |
| 513 | err = skb->len; |
| 514 | |
| 515 | out_free: |
| 516 | skb_free_datagram(sk, skb); |
| 517 | out: |
| 518 | return err; |
| 519 | |
| 520 | csum_copy_err: |
Herbert Xu | 3305b80 | 2005-12-13 23:16:37 -0800 | [diff] [blame] | 521 | skb_kill_datagram(sk, skb, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
| 523 | /* Error for blocking case is chosen to masquerade |
| 524 | as some normal condition. |
| 525 | */ |
| 526 | err = (flags&MSG_DONTWAIT) ? -EAGAIN : -EHOSTUNREACH; |
Wang Chen | a92aa31 | 2007-11-13 20:31:14 -0800 | [diff] [blame] | 527 | atomic_inc(&sk->sk_drops); |
Herbert Xu | 3305b80 | 2005-12-13 23:16:37 -0800 | [diff] [blame] | 528 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | static int rawv6_push_pending_frames(struct sock *sk, struct flowi *fl, |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 532 | struct raw6_sock *rp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | { |
| 534 | struct sk_buff *skb; |
| 535 | int err = 0; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 536 | int offset; |
| 537 | int len; |
Herbert Xu | 679a873 | 2005-05-03 14:24:36 -0700 | [diff] [blame] | 538 | int total_len; |
Al Viro | 868c86b | 2006-11-14 21:35:48 -0800 | [diff] [blame] | 539 | __wsum tmp_csum; |
| 540 | __sum16 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | |
| 542 | if (!rp->checksum) |
| 543 | goto send; |
| 544 | |
| 545 | if ((skb = skb_peek(&sk->sk_write_queue)) == NULL) |
| 546 | goto out; |
| 547 | |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 548 | offset = rp->offset; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 549 | total_len = inet_sk(sk)->cork.length - (skb_network_header(skb) - |
| 550 | skb->data); |
Herbert Xu | 679a873 | 2005-05-03 14:24:36 -0700 | [diff] [blame] | 551 | if (offset >= total_len - 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | err = -EINVAL; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 553 | ip6_flush_pending_frames(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | goto out; |
| 555 | } |
| 556 | |
| 557 | /* should be check HW csum miyazawa */ |
| 558 | if (skb_queue_len(&sk->sk_write_queue) == 1) { |
| 559 | /* |
| 560 | * Only one fragment on the socket. |
| 561 | */ |
| 562 | tmp_csum = skb->csum; |
| 563 | } else { |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 564 | struct sk_buff *csum_skb = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | tmp_csum = 0; |
| 566 | |
| 567 | skb_queue_walk(&sk->sk_write_queue, skb) { |
| 568 | tmp_csum = csum_add(tmp_csum, skb->csum); |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 569 | |
| 570 | if (csum_skb) |
| 571 | continue; |
| 572 | |
Arnaldo Carvalho de Melo | ea2ae17 | 2007-04-25 17:55:53 -0700 | [diff] [blame] | 573 | len = skb->len - skb_transport_offset(skb); |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 574 | if (offset >= len) { |
| 575 | offset -= len; |
| 576 | continue; |
| 577 | } |
| 578 | |
| 579 | csum_skb = skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | } |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 581 | |
| 582 | skb = csum_skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Arnaldo Carvalho de Melo | ea2ae17 | 2007-04-25 17:55:53 -0700 | [diff] [blame] | 585 | offset += skb_transport_offset(skb); |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 586 | if (skb_copy_bits(skb, offset, &csum, 2)) |
| 587 | BUG(); |
| 588 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | /* in case cksum was not initialized */ |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 590 | if (unlikely(csum)) |
Al Viro | 5f92a73 | 2006-11-14 21:36:54 -0800 | [diff] [blame] | 591 | tmp_csum = csum_sub(tmp_csum, csum_unfold(csum)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | |
Al Viro | 868c86b | 2006-11-14 21:35:48 -0800 | [diff] [blame] | 593 | csum = csum_ipv6_magic(&fl->fl6_src, |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 594 | &fl->fl6_dst, |
Herbert Xu | 679a873 | 2005-05-03 14:24:36 -0700 | [diff] [blame] | 595 | total_len, fl->proto, tmp_csum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
Al Viro | f6ab028 | 2006-11-16 02:36:50 -0800 | [diff] [blame] | 597 | if (csum == 0 && fl->proto == IPPROTO_UDP) |
| 598 | csum = CSUM_MANGLED_0; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 599 | |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 600 | if (skb_store_bits(skb, offset, &csum, 2)) |
| 601 | BUG(); |
| 602 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | send: |
| 604 | err = ip6_push_pending_frames(sk); |
| 605 | out: |
| 606 | return err; |
| 607 | } |
| 608 | |
| 609 | static int rawv6_send_hdrinc(struct sock *sk, void *from, int length, |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 610 | struct flowi *fl, struct rt6_info *rt, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | unsigned int flags) |
| 612 | { |
Herbert Xu | 3320da8 | 2005-04-19 22:32:22 -0700 | [diff] [blame] | 613 | struct ipv6_pinfo *np = inet6_sk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | struct ipv6hdr *iph; |
| 615 | struct sk_buff *skb; |
| 616 | unsigned int hh_len; |
| 617 | int err; |
| 618 | |
| 619 | if (length > rt->u.dst.dev->mtu) { |
| 620 | ipv6_local_error(sk, EMSGSIZE, fl, rt->u.dst.dev->mtu); |
| 621 | return -EMSGSIZE; |
| 622 | } |
| 623 | if (flags&MSG_PROBE) |
| 624 | goto out; |
| 625 | |
| 626 | hh_len = LL_RESERVED_SPACE(rt->u.dst.dev); |
| 627 | |
| 628 | skb = sock_alloc_send_skb(sk, length+hh_len+15, |
| 629 | flags&MSG_DONTWAIT, &err); |
| 630 | if (skb == NULL) |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 631 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | skb_reserve(skb, hh_len); |
| 633 | |
| 634 | skb->priority = sk->sk_priority; |
| 635 | skb->dst = dst_clone(&rt->u.dst); |
| 636 | |
Arnaldo Carvalho de Melo | 1ced98e | 2007-03-10 19:57:15 -0300 | [diff] [blame] | 637 | skb_put(skb, length); |
| 638 | skb_reset_network_header(skb); |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 639 | iph = ipv6_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | |
| 641 | skb->ip_summed = CHECKSUM_NONE; |
| 642 | |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 643 | skb->transport_header = skb->network_header; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | err = memcpy_fromiovecend((void *)iph, from, 0, length); |
| 645 | if (err) |
| 646 | goto error_fault; |
| 647 | |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 648 | IP6_INC_STATS(rt->rt6i_idev, IPSTATS_MIB_OUTREQUESTS); |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 649 | err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, rt->u.dst.dev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | dst_output); |
| 651 | if (err > 0) |
Herbert Xu | 3320da8 | 2005-04-19 22:32:22 -0700 | [diff] [blame] | 652 | err = np->recverr ? net_xmit_errno(err) : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | if (err) |
| 654 | goto error; |
| 655 | out: |
| 656 | return 0; |
| 657 | |
| 658 | error_fault: |
| 659 | err = -EFAULT; |
| 660 | kfree_skb(skb); |
| 661 | error: |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 662 | IP6_INC_STATS(rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 663 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | } |
| 665 | |
Heiko Carstens | a27b58f | 2006-10-30 15:06:12 -0800 | [diff] [blame] | 666 | static int rawv6_probe_proto_opt(struct flowi *fl, struct msghdr *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | { |
| 668 | struct iovec *iov; |
| 669 | u8 __user *type = NULL; |
| 670 | u8 __user *code = NULL; |
Masahide NAKAMURA | 6e8f4d4 | 2006-08-23 20:36:47 -0700 | [diff] [blame] | 671 | u8 len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | int probed = 0; |
| 673 | int i; |
| 674 | |
| 675 | if (!msg->msg_iov) |
Heiko Carstens | a27b58f | 2006-10-30 15:06:12 -0800 | [diff] [blame] | 676 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | |
| 678 | for (i = 0; i < msg->msg_iovlen; i++) { |
| 679 | iov = &msg->msg_iov[i]; |
| 680 | if (!iov) |
| 681 | continue; |
| 682 | |
| 683 | switch (fl->proto) { |
| 684 | case IPPROTO_ICMPV6: |
| 685 | /* check if one-byte field is readable or not. */ |
| 686 | if (iov->iov_base && iov->iov_len < 1) |
| 687 | break; |
| 688 | |
| 689 | if (!type) { |
| 690 | type = iov->iov_base; |
| 691 | /* check if code field is readable or not. */ |
| 692 | if (iov->iov_len > 1) |
| 693 | code = type + 1; |
| 694 | } else if (!code) |
| 695 | code = iov->iov_base; |
| 696 | |
| 697 | if (type && code) { |
Heiko Carstens | a27b58f | 2006-10-30 15:06:12 -0800 | [diff] [blame] | 698 | if (get_user(fl->fl_icmp_type, type) || |
| 699 | get_user(fl->fl_icmp_code, code)) |
| 700 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | probed = 1; |
| 702 | } |
| 703 | break; |
Masahide NAKAMURA | 6e8f4d4 | 2006-08-23 20:36:47 -0700 | [diff] [blame] | 704 | case IPPROTO_MH: |
| 705 | if (iov->iov_base && iov->iov_len < 1) |
| 706 | break; |
| 707 | /* check if type field is readable or not. */ |
| 708 | if (iov->iov_len > 2 - len) { |
| 709 | u8 __user *p = iov->iov_base; |
Heiko Carstens | a27b58f | 2006-10-30 15:06:12 -0800 | [diff] [blame] | 710 | if (get_user(fl->fl_mh_type, &p[2 - len])) |
| 711 | return -EFAULT; |
Masahide NAKAMURA | 6e8f4d4 | 2006-08-23 20:36:47 -0700 | [diff] [blame] | 712 | probed = 1; |
| 713 | } else |
| 714 | len += iov->iov_len; |
| 715 | |
| 716 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | default: |
| 718 | probed = 1; |
| 719 | break; |
| 720 | } |
| 721 | if (probed) |
| 722 | break; |
| 723 | } |
Heiko Carstens | a27b58f | 2006-10-30 15:06:12 -0800 | [diff] [blame] | 724 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | static int rawv6_sendmsg(struct kiocb *iocb, struct sock *sk, |
| 728 | struct msghdr *msg, size_t len) |
| 729 | { |
| 730 | struct ipv6_txoptions opt_space; |
| 731 | struct sockaddr_in6 * sin6 = (struct sockaddr_in6 *) msg->msg_name; |
| 732 | struct in6_addr *daddr, *final_p = NULL, final; |
| 733 | struct inet_sock *inet = inet_sk(sk); |
| 734 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 735 | struct raw6_sock *rp = raw6_sk(sk); |
| 736 | struct ipv6_txoptions *opt = NULL; |
| 737 | struct ip6_flowlabel *flowlabel = NULL; |
| 738 | struct dst_entry *dst = NULL; |
| 739 | struct flowi fl; |
| 740 | int addr_len = msg->msg_namelen; |
| 741 | int hlimit = -1; |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 742 | int tclass = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | u16 proto; |
| 744 | int err; |
| 745 | |
| 746 | /* Rough check on arithmetic overflow, |
YOSHIFUJI Hideaki | b59e139 | 2007-03-30 14:45:35 -0700 | [diff] [blame] | 747 | better check is made in ip6_append_data(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | */ |
YOSHIFUJI Hideaki | b59e139 | 2007-03-30 14:45:35 -0700 | [diff] [blame] | 749 | if (len > INT_MAX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | return -EMSGSIZE; |
| 751 | |
| 752 | /* Mirror BSD error message compatibility */ |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 753 | if (msg->msg_flags & MSG_OOB) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | return -EOPNOTSUPP; |
| 755 | |
| 756 | /* |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 757 | * Get and verify the address. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | */ |
| 759 | memset(&fl, 0, sizeof(fl)); |
| 760 | |
| 761 | if (sin6) { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 762 | if (addr_len < SIN6_LEN_RFC2133) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | return -EINVAL; |
| 764 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 765 | if (sin6->sin6_family && sin6->sin6_family != AF_INET6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | return(-EAFNOSUPPORT); |
| 767 | |
| 768 | /* port is the proto value [0..255] carried in nexthdr */ |
| 769 | proto = ntohs(sin6->sin6_port); |
| 770 | |
| 771 | if (!proto) |
| 772 | proto = inet->num; |
| 773 | else if (proto != inet->num) |
| 774 | return(-EINVAL); |
| 775 | |
| 776 | if (proto > 255) |
| 777 | return(-EINVAL); |
| 778 | |
| 779 | daddr = &sin6->sin6_addr; |
| 780 | if (np->sndflow) { |
| 781 | fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK; |
| 782 | if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) { |
| 783 | flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel); |
| 784 | if (flowlabel == NULL) |
| 785 | return -EINVAL; |
| 786 | daddr = &flowlabel->dst; |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | /* |
| 791 | * Otherwise it will be difficult to maintain |
| 792 | * sk->sk_dst_cache. |
| 793 | */ |
| 794 | if (sk->sk_state == TCP_ESTABLISHED && |
| 795 | ipv6_addr_equal(daddr, &np->daddr)) |
| 796 | daddr = &np->daddr; |
| 797 | |
| 798 | if (addr_len >= sizeof(struct sockaddr_in6) && |
| 799 | sin6->sin6_scope_id && |
| 800 | ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL) |
| 801 | fl.oif = sin6->sin6_scope_id; |
| 802 | } else { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 803 | if (sk->sk_state != TCP_ESTABLISHED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | return -EDESTADDRREQ; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 805 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | proto = inet->num; |
| 807 | daddr = &np->daddr; |
| 808 | fl.fl6_flowlabel = np->flow_label; |
| 809 | } |
| 810 | |
| 811 | if (ipv6_addr_any(daddr)) { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 812 | /* |
| 813 | * unspecified destination address |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | * treated as error... is this correct ? |
| 815 | */ |
| 816 | fl6_sock_release(flowlabel); |
| 817 | return(-EINVAL); |
| 818 | } |
| 819 | |
| 820 | if (fl.oif == 0) |
| 821 | fl.oif = sk->sk_bound_dev_if; |
| 822 | |
| 823 | if (msg->msg_controllen) { |
| 824 | opt = &opt_space; |
| 825 | memset(opt, 0, sizeof(struct ipv6_txoptions)); |
| 826 | opt->tot_len = sizeof(struct ipv6_txoptions); |
| 827 | |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 828 | err = datagram_send_ctl(msg, &fl, opt, &hlimit, &tclass); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | if (err < 0) { |
| 830 | fl6_sock_release(flowlabel); |
| 831 | return err; |
| 832 | } |
| 833 | if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) { |
| 834 | flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel); |
| 835 | if (flowlabel == NULL) |
| 836 | return -EINVAL; |
| 837 | } |
| 838 | if (!(opt->opt_nflen|opt->opt_flen)) |
| 839 | opt = NULL; |
| 840 | } |
| 841 | if (opt == NULL) |
| 842 | opt = np->opt; |
YOSHIFUJI Hideaki | df9890c | 2005-11-20 12:23:18 +0900 | [diff] [blame] | 843 | if (flowlabel) |
| 844 | opt = fl6_merge_options(&opt_space, flowlabel, opt); |
| 845 | opt = ipv6_fixup_options(&opt_space, opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | |
| 847 | fl.proto = proto; |
Heiko Carstens | a27b58f | 2006-10-30 15:06:12 -0800 | [diff] [blame] | 848 | err = rawv6_probe_proto_opt(&fl, msg); |
| 849 | if (err) |
| 850 | goto out; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 851 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | ipv6_addr_copy(&fl.fl6_dst, daddr); |
| 853 | if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr)) |
| 854 | ipv6_addr_copy(&fl.fl6_src, &np->saddr); |
| 855 | |
| 856 | /* merge ip6_build_xmit from ip6_output */ |
| 857 | if (opt && opt->srcrt) { |
| 858 | struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt; |
| 859 | ipv6_addr_copy(&final, &fl.fl6_dst); |
| 860 | ipv6_addr_copy(&fl.fl6_dst, rt0->addr); |
| 861 | final_p = &final; |
| 862 | } |
| 863 | |
| 864 | if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst)) |
| 865 | fl.oif = np->mcast_oif; |
Venkat Yekkirala | beb8d13 | 2006-08-04 23:12:42 -0700 | [diff] [blame] | 866 | security_sk_classify_flow(sk, &fl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | |
| 868 | err = ip6_dst_lookup(sk, &dst, &fl); |
| 869 | if (err) |
| 870 | goto out; |
| 871 | if (final_p) |
| 872 | ipv6_addr_copy(&fl.fl6_dst, final_p); |
| 873 | |
Herbert Xu | bb72845 | 2007-12-12 18:48:58 -0800 | [diff] [blame] | 874 | if ((err = __xfrm_lookup(&dst, &fl, sk, XFRM_LOOKUP_WAIT)) < 0) { |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 875 | if (err == -EREMOTE) |
| 876 | err = ip6_dst_blackhole(sk, &dst, &fl); |
| 877 | if (err < 0) |
| 878 | goto out; |
| 879 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | |
| 881 | if (hlimit < 0) { |
| 882 | if (ipv6_addr_is_multicast(&fl.fl6_dst)) |
| 883 | hlimit = np->mcast_hops; |
| 884 | else |
| 885 | hlimit = np->hop_limit; |
| 886 | if (hlimit < 0) |
| 887 | hlimit = dst_metric(dst, RTAX_HOPLIMIT); |
| 888 | if (hlimit < 0) |
| 889 | hlimit = ipv6_get_hoplimit(dst->dev); |
| 890 | } |
| 891 | |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 892 | if (tclass < 0) { |
YOSHIFUJI Hideaki | e012d51 | 2006-09-13 20:01:28 -0700 | [diff] [blame] | 893 | tclass = np->tclass; |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 894 | if (tclass < 0) |
| 895 | tclass = 0; |
| 896 | } |
| 897 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | if (msg->msg_flags&MSG_CONFIRM) |
| 899 | goto do_confirm; |
| 900 | |
| 901 | back_from_confirm: |
| 902 | if (inet->hdrincl) { |
| 903 | err = rawv6_send_hdrinc(sk, msg->msg_iov, len, &fl, (struct rt6_info*)dst, msg->msg_flags); |
| 904 | } else { |
| 905 | lock_sock(sk); |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 906 | err = ip6_append_data(sk, ip_generic_getfrag, msg->msg_iov, |
| 907 | len, 0, hlimit, tclass, opt, &fl, (struct rt6_info*)dst, |
| 908 | msg->msg_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | |
| 910 | if (err) |
| 911 | ip6_flush_pending_frames(sk); |
| 912 | else if (!(msg->msg_flags & MSG_MORE)) |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 913 | err = rawv6_push_pending_frames(sk, &fl, rp); |
YOSHIFUJI Hideaki | 3ef9d94 | 2007-09-14 16:45:40 -0700 | [diff] [blame] | 914 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | } |
| 916 | done: |
Nicolas DICHTEL | 6d3e85e | 2006-02-13 15:56:13 -0800 | [diff] [blame] | 917 | dst_release(dst); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 918 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | fl6_sock_release(flowlabel); |
| 920 | return err<0?err:len; |
| 921 | do_confirm: |
| 922 | dst_confirm(dst); |
| 923 | if (!(msg->msg_flags & MSG_PROBE) || len) |
| 924 | goto back_from_confirm; |
| 925 | err = 0; |
| 926 | goto done; |
| 927 | } |
| 928 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 929 | static int rawv6_seticmpfilter(struct sock *sk, int level, int optname, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | char __user *optval, int optlen) |
| 931 | { |
| 932 | switch (optname) { |
| 933 | case ICMPV6_FILTER: |
| 934 | if (optlen > sizeof(struct icmp6_filter)) |
| 935 | optlen = sizeof(struct icmp6_filter); |
| 936 | if (copy_from_user(&raw6_sk(sk)->filter, optval, optlen)) |
| 937 | return -EFAULT; |
| 938 | return 0; |
| 939 | default: |
| 940 | return -ENOPROTOOPT; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 941 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | |
| 943 | return 0; |
| 944 | } |
| 945 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 946 | static int rawv6_geticmpfilter(struct sock *sk, int level, int optname, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | char __user *optval, int __user *optlen) |
| 948 | { |
| 949 | int len; |
| 950 | |
| 951 | switch (optname) { |
| 952 | case ICMPV6_FILTER: |
| 953 | if (get_user(len, optlen)) |
| 954 | return -EFAULT; |
| 955 | if (len < 0) |
| 956 | return -EINVAL; |
| 957 | if (len > sizeof(struct icmp6_filter)) |
| 958 | len = sizeof(struct icmp6_filter); |
| 959 | if (put_user(len, optlen)) |
| 960 | return -EFAULT; |
| 961 | if (copy_to_user(optval, &raw6_sk(sk)->filter, len)) |
| 962 | return -EFAULT; |
| 963 | return 0; |
| 964 | default: |
| 965 | return -ENOPROTOOPT; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 966 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | |
| 968 | return 0; |
| 969 | } |
| 970 | |
| 971 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 972 | static int do_rawv6_setsockopt(struct sock *sk, int level, int optname, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | char __user *optval, int optlen) |
| 974 | { |
| 975 | struct raw6_sock *rp = raw6_sk(sk); |
| 976 | int val; |
| 977 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 978 | if (get_user(val, (int __user *)optval)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | return -EFAULT; |
| 980 | |
| 981 | switch (optname) { |
| 982 | case IPV6_CHECKSUM: |
| 983 | /* You may get strange result with a positive odd offset; |
| 984 | RFC2292bis agrees with me. */ |
| 985 | if (val > 0 && (val&1)) |
| 986 | return(-EINVAL); |
| 987 | if (val < 0) { |
| 988 | rp->checksum = 0; |
| 989 | } else { |
| 990 | rp->checksum = 1; |
| 991 | rp->offset = val; |
| 992 | } |
| 993 | |
| 994 | return 0; |
| 995 | break; |
| 996 | |
| 997 | default: |
| 998 | return(-ENOPROTOOPT); |
| 999 | } |
| 1000 | } |
| 1001 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1002 | static int rawv6_setsockopt(struct sock *sk, int level, int optname, |
| 1003 | char __user *optval, int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | switch(level) { |
| 1006 | case SOL_RAW: |
| 1007 | break; |
| 1008 | |
| 1009 | case SOL_ICMPV6: |
| 1010 | if (inet_sk(sk)->num != IPPROTO_ICMPV6) |
| 1011 | return -EOPNOTSUPP; |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1012 | return rawv6_seticmpfilter(sk, level, optname, optval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | optlen); |
| 1014 | case SOL_IPV6: |
| 1015 | if (optname == IPV6_CHECKSUM) |
| 1016 | break; |
| 1017 | default: |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1018 | return ipv6_setsockopt(sk, level, optname, optval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | optlen); |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1022 | return do_rawv6_setsockopt(sk, level, optname, optval, optlen); |
| 1023 | } |
| 1024 | |
| 1025 | #ifdef CONFIG_COMPAT |
| 1026 | static int compat_rawv6_setsockopt(struct sock *sk, int level, int optname, |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1027 | char __user *optval, int optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1028 | { |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1029 | switch (level) { |
| 1030 | case SOL_RAW: |
| 1031 | break; |
| 1032 | case SOL_ICMPV6: |
| 1033 | if (inet_sk(sk)->num != IPPROTO_ICMPV6) |
| 1034 | return -EOPNOTSUPP; |
| 1035 | return rawv6_seticmpfilter(sk, level, optname, optval, optlen); |
| 1036 | case SOL_IPV6: |
| 1037 | if (optname == IPV6_CHECKSUM) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1038 | break; |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1039 | default: |
| 1040 | return compat_ipv6_setsockopt(sk, level, optname, |
| 1041 | optval, optlen); |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 1042 | } |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1043 | return do_rawv6_setsockopt(sk, level, optname, optval, optlen); |
| 1044 | } |
| 1045 | #endif |
| 1046 | |
| 1047 | static int do_rawv6_getsockopt(struct sock *sk, int level, int optname, |
| 1048 | char __user *optval, int __user *optlen) |
| 1049 | { |
| 1050 | struct raw6_sock *rp = raw6_sk(sk); |
| 1051 | int val, len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | |
| 1053 | if (get_user(len,optlen)) |
| 1054 | return -EFAULT; |
| 1055 | |
| 1056 | switch (optname) { |
| 1057 | case IPV6_CHECKSUM: |
| 1058 | if (rp->checksum == 0) |
| 1059 | val = -1; |
| 1060 | else |
| 1061 | val = rp->offset; |
| 1062 | break; |
| 1063 | |
| 1064 | default: |
| 1065 | return -ENOPROTOOPT; |
| 1066 | } |
| 1067 | |
| 1068 | len = min_t(unsigned int, sizeof(int), len); |
| 1069 | |
| 1070 | if (put_user(len, optlen)) |
| 1071 | return -EFAULT; |
| 1072 | if (copy_to_user(optval,&val,len)) |
| 1073 | return -EFAULT; |
| 1074 | return 0; |
| 1075 | } |
| 1076 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1077 | static int rawv6_getsockopt(struct sock *sk, int level, int optname, |
| 1078 | char __user *optval, int __user *optlen) |
| 1079 | { |
| 1080 | switch(level) { |
| 1081 | case SOL_RAW: |
| 1082 | break; |
| 1083 | |
| 1084 | case SOL_ICMPV6: |
| 1085 | if (inet_sk(sk)->num != IPPROTO_ICMPV6) |
| 1086 | return -EOPNOTSUPP; |
| 1087 | return rawv6_geticmpfilter(sk, level, optname, optval, |
| 1088 | optlen); |
| 1089 | case SOL_IPV6: |
| 1090 | if (optname == IPV6_CHECKSUM) |
| 1091 | break; |
| 1092 | default: |
| 1093 | return ipv6_getsockopt(sk, level, optname, optval, |
| 1094 | optlen); |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 1095 | } |
| 1096 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1097 | return do_rawv6_getsockopt(sk, level, optname, optval, optlen); |
| 1098 | } |
| 1099 | |
| 1100 | #ifdef CONFIG_COMPAT |
| 1101 | static int compat_rawv6_getsockopt(struct sock *sk, int level, int optname, |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1102 | char __user *optval, int __user *optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1103 | { |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1104 | switch (level) { |
| 1105 | case SOL_RAW: |
| 1106 | break; |
| 1107 | case SOL_ICMPV6: |
| 1108 | if (inet_sk(sk)->num != IPPROTO_ICMPV6) |
| 1109 | return -EOPNOTSUPP; |
| 1110 | return rawv6_geticmpfilter(sk, level, optname, optval, optlen); |
| 1111 | case SOL_IPV6: |
| 1112 | if (optname == IPV6_CHECKSUM) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1113 | break; |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1114 | default: |
| 1115 | return compat_ipv6_getsockopt(sk, level, optname, |
| 1116 | optval, optlen); |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 1117 | } |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1118 | return do_rawv6_getsockopt(sk, level, optname, optval, optlen); |
| 1119 | } |
| 1120 | #endif |
| 1121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | static int rawv6_ioctl(struct sock *sk, int cmd, unsigned long arg) |
| 1123 | { |
| 1124 | switch(cmd) { |
| 1125 | case SIOCOUTQ: |
| 1126 | { |
| 1127 | int amount = atomic_read(&sk->sk_wmem_alloc); |
| 1128 | return put_user(amount, (int __user *)arg); |
| 1129 | } |
| 1130 | case SIOCINQ: |
| 1131 | { |
| 1132 | struct sk_buff *skb; |
| 1133 | int amount = 0; |
| 1134 | |
Herbert Xu | e0f9f85 | 2005-06-18 22:56:18 -0700 | [diff] [blame] | 1135 | spin_lock_bh(&sk->sk_receive_queue.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | skb = skb_peek(&sk->sk_receive_queue); |
| 1137 | if (skb != NULL) |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1138 | amount = skb->tail - skb->transport_header; |
Herbert Xu | e0f9f85 | 2005-06-18 22:56:18 -0700 | [diff] [blame] | 1139 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | return put_user(amount, (int __user *)arg); |
| 1141 | } |
| 1142 | |
| 1143 | default: |
| 1144 | return -ENOIOCTLCMD; |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | static void rawv6_close(struct sock *sk, long timeout) |
| 1149 | { |
| 1150 | if (inet_sk(sk)->num == IPPROTO_RAW) |
| 1151 | ip6_ra_control(sk, -1, NULL); |
| 1152 | |
| 1153 | sk_common_release(sk); |
| 1154 | } |
| 1155 | |
| 1156 | static int rawv6_init_sk(struct sock *sk) |
| 1157 | { |
Masahide NAKAMURA | f48d5ff | 2007-02-07 00:07:39 -0800 | [diff] [blame] | 1158 | struct raw6_sock *rp = raw6_sk(sk); |
| 1159 | |
| 1160 | switch (inet_sk(sk)->num) { |
| 1161 | case IPPROTO_ICMPV6: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | rp->checksum = 1; |
| 1163 | rp->offset = 2; |
Masahide NAKAMURA | f48d5ff | 2007-02-07 00:07:39 -0800 | [diff] [blame] | 1164 | break; |
| 1165 | case IPPROTO_MH: |
| 1166 | rp->checksum = 1; |
| 1167 | rp->offset = 4; |
| 1168 | break; |
| 1169 | default: |
| 1170 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | } |
| 1172 | return(0); |
| 1173 | } |
| 1174 | |
Eric Dumazet | c5a432f | 2007-11-05 23:39:51 -0800 | [diff] [blame] | 1175 | DEFINE_PROTO_INUSE(rawv6) |
| 1176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | struct proto rawv6_prot = { |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1178 | .name = "RAWv6", |
| 1179 | .owner = THIS_MODULE, |
| 1180 | .close = rawv6_close, |
| 1181 | .connect = ip6_datagram_connect, |
| 1182 | .disconnect = udp_disconnect, |
| 1183 | .ioctl = rawv6_ioctl, |
| 1184 | .init = rawv6_init_sk, |
| 1185 | .destroy = inet6_destroy_sock, |
| 1186 | .setsockopt = rawv6_setsockopt, |
| 1187 | .getsockopt = rawv6_getsockopt, |
| 1188 | .sendmsg = rawv6_sendmsg, |
| 1189 | .recvmsg = rawv6_recvmsg, |
| 1190 | .bind = rawv6_bind, |
| 1191 | .backlog_rcv = rawv6_rcv_skb, |
| 1192 | .hash = raw_v6_hash, |
| 1193 | .unhash = raw_v6_unhash, |
| 1194 | .obj_size = sizeof(struct raw6_sock), |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1195 | #ifdef CONFIG_COMPAT |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1196 | .compat_setsockopt = compat_rawv6_setsockopt, |
| 1197 | .compat_getsockopt = compat_rawv6_getsockopt, |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1198 | #endif |
Eric Dumazet | c5a432f | 2007-11-05 23:39:51 -0800 | [diff] [blame] | 1199 | REF_PROTO_INUSE(rawv6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | }; |
| 1201 | |
| 1202 | #ifdef CONFIG_PROC_FS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | static void raw6_sock_seq_show(struct seq_file *seq, struct sock *sp, int i) |
| 1204 | { |
| 1205 | struct ipv6_pinfo *np = inet6_sk(sp); |
| 1206 | struct in6_addr *dest, *src; |
| 1207 | __u16 destp, srcp; |
| 1208 | |
| 1209 | dest = &np->daddr; |
| 1210 | src = &np->rcv_saddr; |
| 1211 | destp = 0; |
| 1212 | srcp = inet_sk(sp)->num; |
| 1213 | seq_printf(seq, |
| 1214 | "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X " |
Wang Chen | a92aa31 | 2007-11-13 20:31:14 -0800 | [diff] [blame] | 1215 | "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | i, |
| 1217 | src->s6_addr32[0], src->s6_addr32[1], |
| 1218 | src->s6_addr32[2], src->s6_addr32[3], srcp, |
| 1219 | dest->s6_addr32[0], dest->s6_addr32[1], |
| 1220 | dest->s6_addr32[2], dest->s6_addr32[3], destp, |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1221 | sp->sk_state, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | atomic_read(&sp->sk_wmem_alloc), |
| 1223 | atomic_read(&sp->sk_rmem_alloc), |
| 1224 | 0, 0L, 0, |
| 1225 | sock_i_uid(sp), 0, |
| 1226 | sock_i_ino(sp), |
Wang Chen | a92aa31 | 2007-11-13 20:31:14 -0800 | [diff] [blame] | 1227 | atomic_read(&sp->sk_refcnt), sp, atomic_read(&sp->sk_drops)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | static int raw6_seq_show(struct seq_file *seq, void *v) |
| 1231 | { |
| 1232 | if (v == SEQ_START_TOKEN) |
| 1233 | seq_printf(seq, |
| 1234 | " sl " |
| 1235 | "local_address " |
| 1236 | "remote_address " |
| 1237 | "st tx_queue rx_queue tr tm->when retrnsmt" |
Wang Chen | a92aa31 | 2007-11-13 20:31:14 -0800 | [diff] [blame] | 1238 | " uid timeout inode drops\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | else |
Pavel Emelyanov | 42a7380 | 2007-11-19 22:38:33 -0800 | [diff] [blame] | 1240 | raw6_sock_seq_show(seq, v, raw_seq_private(seq)->bucket); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | return 0; |
| 1242 | } |
| 1243 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 1244 | static const struct seq_operations raw6_seq_ops = { |
Pavel Emelyanov | 42a7380 | 2007-11-19 22:38:33 -0800 | [diff] [blame] | 1245 | .start = raw_seq_start, |
| 1246 | .next = raw_seq_next, |
| 1247 | .stop = raw_seq_stop, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | .show = raw6_seq_show, |
| 1249 | }; |
| 1250 | |
| 1251 | static int raw6_seq_open(struct inode *inode, struct file *file) |
| 1252 | { |
Pavel Emelyanov | 42a7380 | 2007-11-19 22:38:33 -0800 | [diff] [blame] | 1253 | return raw_seq_open(file, &raw_v6_hashinfo, PF_INET6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | } |
| 1255 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 1256 | static const struct file_operations raw6_seq_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | .owner = THIS_MODULE, |
| 1258 | .open = raw6_seq_open, |
| 1259 | .read = seq_read, |
| 1260 | .llseek = seq_lseek, |
| 1261 | .release = seq_release_private, |
| 1262 | }; |
| 1263 | |
| 1264 | int __init raw6_proc_init(void) |
| 1265 | { |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 1266 | if (!proc_net_fops_create(&init_net, "raw6", S_IRUGO, &raw6_seq_fops)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | return -ENOMEM; |
| 1268 | return 0; |
| 1269 | } |
| 1270 | |
| 1271 | void raw6_proc_exit(void) |
| 1272 | { |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 1273 | proc_net_remove(&init_net, "raw6"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | } |
| 1275 | #endif /* CONFIG_PROC_FS */ |
Daniel Lezcano | 7f4e486 | 2007-12-11 02:25:35 -0800 | [diff] [blame] | 1276 | |
| 1277 | /* Same as inet6_dgram_ops, sans udp_poll. */ |
| 1278 | static const struct proto_ops inet6_sockraw_ops = { |
| 1279 | .family = PF_INET6, |
| 1280 | .owner = THIS_MODULE, |
| 1281 | .release = inet6_release, |
| 1282 | .bind = inet6_bind, |
| 1283 | .connect = inet_dgram_connect, /* ok */ |
| 1284 | .socketpair = sock_no_socketpair, /* a do nothing */ |
| 1285 | .accept = sock_no_accept, /* a do nothing */ |
| 1286 | .getname = inet6_getname, |
| 1287 | .poll = datagram_poll, /* ok */ |
| 1288 | .ioctl = inet6_ioctl, /* must change */ |
| 1289 | .listen = sock_no_listen, /* ok */ |
| 1290 | .shutdown = inet_shutdown, /* ok */ |
| 1291 | .setsockopt = sock_common_setsockopt, /* ok */ |
| 1292 | .getsockopt = sock_common_getsockopt, /* ok */ |
| 1293 | .sendmsg = inet_sendmsg, /* ok */ |
| 1294 | .recvmsg = sock_common_recvmsg, /* ok */ |
| 1295 | .mmap = sock_no_mmap, |
| 1296 | .sendpage = sock_no_sendpage, |
| 1297 | #ifdef CONFIG_COMPAT |
| 1298 | .compat_setsockopt = compat_sock_common_setsockopt, |
| 1299 | .compat_getsockopt = compat_sock_common_getsockopt, |
| 1300 | #endif |
| 1301 | }; |
| 1302 | |
| 1303 | static struct inet_protosw rawv6_protosw = { |
| 1304 | .type = SOCK_RAW, |
| 1305 | .protocol = IPPROTO_IP, /* wild card */ |
| 1306 | .prot = &rawv6_prot, |
| 1307 | .ops = &inet6_sockraw_ops, |
| 1308 | .capability = CAP_NET_RAW, |
| 1309 | .no_check = UDP_CSUM_DEFAULT, |
| 1310 | .flags = INET_PROTOSW_REUSE, |
| 1311 | }; |
| 1312 | |
| 1313 | int __init rawv6_init(void) |
| 1314 | { |
| 1315 | int ret; |
| 1316 | |
| 1317 | ret = inet6_register_protosw(&rawv6_protosw); |
| 1318 | if (ret) |
| 1319 | goto out; |
| 1320 | out: |
| 1321 | return ret; |
| 1322 | } |
| 1323 | |
| 1324 | void __exit rawv6_exit(void) |
| 1325 | { |
| 1326 | inet6_unregister_protosw(&rawv6_protosw); |
| 1327 | } |