blob: 850b83e430bc5482bd2f66609f8c06c3e08fa773 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * RAW sockets for IPv6
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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 Hideaki1ab14572007-02-09 23:24:49 +090014 * YOSHIFUJI,H.@USAGI : raw checksum (RFC2292(bis) compliance)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * 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 Torvalds1da177e2005-04-16 15:20:36 -070027#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 Xu3305b802005-12-13 23:16:37 -080034#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/uaccess.h>
36#include <asm/ioctls.h>
37
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020038#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#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 Meloc752f072005-08-09 20:08:28 -070052#include <net/tcp_states.h>
Masahide NAKAMURA59fbb3a2007-06-26 23:56:32 -070053#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURA7be96f72006-08-23 20:35:31 -070054#include <net/mip6.h>
55#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Pavel Emelyanovb673e4d2007-11-19 22:36:45 -080057#include <net/raw.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <net/rawv6.h>
59#include <net/xfrm.h>
60
61#include <linux/proc_fs.h>
62#include <linux/seq_file.h>
63
Pavel Emelyanovb673e4d2007-11-19 22:36:45 -080064static struct raw_hashinfo raw_v6_hashinfo = {
65 .lock = __RW_LOCK_UNLOCKED(),
66};
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68static void raw_v6_hash(struct sock *sk)
69{
Pavel Emelyanov65b4c502007-11-19 22:37:24 -080070 raw_hash_sk(sk, &raw_v6_hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
73static void raw_v6_unhash(struct sock *sk)
74{
Pavel Emelyanovab707682007-11-19 22:37:58 -080075 raw_unhash_sk(sk, &raw_v6_hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77
78
Pavel Emelyanov69d6da02007-11-19 22:35:57 -080079static 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 Torvalds1da177e2005-04-16 15:20:36 -070081{
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 McDonald0bd1b592005-08-09 19:44:42 -070093 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif)
94 continue;
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 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;
107found:
108 return sk;
109}
110
111/*
112 * 0 - deliver
113 * 1 - block
114 */
115static __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 NAKAMURA59fbb3a2007-06-26 23:56:32 -0700132#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
133static int (*mh_filter)(struct sock *sock, struct sk_buff *skb);
134
135int 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}
141EXPORT_SYMBOL(rawv6_mh_filter_register);
142
143int 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}
150EXPORT_SYMBOL(rawv6_mh_filter_unregister);
151
152#endif
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/*
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 Emelyanov69d6da02007-11-19 22:35:57 -0800161static int ipv6_raw_deliver(struct sk_buff *skb, int nexthdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 struct in6_addr *saddr;
164 struct in6_addr *daddr;
165 struct sock *sk;
Patrick McHardyd13964f2005-08-09 19:45:02 -0700166 int delivered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 __u8 hash;
168
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700169 saddr = &ipv6_hdr(skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 daddr = saddr + 1;
171
172 hash = nexthdr & (MAX_INET_PROTOS - 1);
173
Pavel Emelyanovb673e4d2007-11-19 22:36:45 -0800174 read_lock(&raw_v6_hashinfo.lock);
175 sk = sk_head(&raw_v6_hashinfo.ht[hash]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
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 Hideaki2dac4b92005-09-01 17:44:49 -0700185 sk = __raw_v6_lookup(sk, nexthdr, daddr, saddr, IP6CB(skb)->iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 while (sk) {
Masahide NAKAMURA7be96f72006-08-23 20:35:31 -0700188 int filtered;
189
Patrick McHardyd13964f2005-08-09 19:45:02 -0700190 delivered = 1;
Masahide NAKAMURA7be96f72006-08-23 20:35:31 -0700191 switch (nexthdr) {
192 case IPPROTO_ICMPV6:
193 filtered = icmpv6_filter(sk, skb);
194 break;
Masahide NAKAMURA59fbb3a2007-06-26 23:56:32 -0700195
196#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURA7be96f72006-08-23 20:35:31 -0700197 case IPPROTO_MH:
Masahide NAKAMURA59fbb3a2007-06-26 23:56:32 -0700198 {
Masahide NAKAMURA7be96f72006-08-23 20:35:31 -0700199 /* 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 NAKAMURA59fbb3a2007-06-26 23:56:32 -0700205 int (*filter)(struct sock *sock, struct sk_buff *skb);
206
207 filter = rcu_dereference(mh_filter);
208 filtered = filter ? filter(sk, skb) : 0;
Masahide NAKAMURA7be96f72006-08-23 20:35:31 -0700209 break;
Masahide NAKAMURA59fbb3a2007-06-26 23:56:32 -0700210 }
Masahide NAKAMURA7be96f72006-08-23 20:35:31 -0700211#endif
212 default:
213 filtered = 0;
214 break;
215 }
216
217 if (filtered < 0)
218 break;
219 if (filtered == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
221
222 /* Not releasing hash table! */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800223 if (clone) {
224 nf_reset(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 rawv6_rcv(sk, clone);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
Andrew McDonald0bd1b592005-08-09 19:44:42 -0700228 sk = __raw_v6_lookup(sk_next(sk), nexthdr, daddr, saddr,
YOSHIFUJI Hideaki2dac4b92005-09-01 17:44:49 -0700229 IP6CB(skb)->iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231out:
Pavel Emelyanovb673e4d2007-11-19 22:36:45 -0800232 read_unlock(&raw_v6_hashinfo.lock);
Patrick McHardyd13964f2005-08-09 19:45:02 -0700233 return delivered;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
Pavel Emelyanov69d6da02007-11-19 22:35:57 -0800236int raw6_local_deliver(struct sk_buff *skb, int nexthdr)
237{
238 struct sock *raw_sk;
239
Pavel Emelyanovb673e4d2007-11-19 22:36:45 -0800240 raw_sk = sk_head(&raw_v6_hashinfo.ht[nexthdr & (MAX_INET_PROTOS - 1)]);
Pavel Emelyanov69d6da02007-11-19 22:35:57 -0800241 if (raw_sk && !ipv6_raw_deliver(skb, nexthdr))
242 raw_sk = NULL;
243
244 return raw_sk != NULL;
245}
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247/* This cleans up af_inet6 a bit. -DaveM */
248static 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 Viroe69a4ad2006-11-14 20:56:00 -0800253 __be32 v4addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 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 Hideaki1ab14572007-02-09 23:24:49 +0900283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 /* Binding to link-local address requires an interface */
285 if (!sk->sk_bound_dev_if)
286 goto out;
287
Eric W. Biederman881d9662007-09-17 11:56:21 -0700288 dev = dev_get_by_index(&init_net, sk->sk_bound_dev_if);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (!dev) {
290 err = -ENODEV;
291 goto out;
292 }
293 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 /* 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;
316out:
317 release_sock(sk);
318 return err;
319}
320
Pavel Emelyanov69d6da02007-11-19 22:35:57 -0800321static void rawv6_err(struct sock *sk, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 struct inet6_skb_parm *opt,
Al Viro04ce6902006-11-08 00:21:01 -0800323 int type, int code, int offset, __be32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
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 Emelyanov69d6da02007-11-19 22:35:57 -0800355void 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 Emelyanovb673e4d2007-11-19 22:36:45 -0800362 hash = nexthdr & (RAW_HTABLE_SIZE - 1);
Pavel Emelyanov69d6da02007-11-19 22:35:57 -0800363
Pavel Emelyanovb673e4d2007-11-19 22:36:45 -0800364 read_lock(&raw_v6_hashinfo.lock);
365 sk = sk_head(&raw_v6_hashinfo.ht[hash]);
Pavel Emelyanov69d6da02007-11-19 22:35:57 -0800366 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 Emelyanovb673e4d2007-11-19 22:36:45 -0800377 read_unlock(&raw_v6_hashinfo.lock);
Pavel Emelyanov69d6da02007-11-19 22:35:57 -0800378}
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380static inline int rawv6_rcv_skb(struct sock * sk, struct sk_buff * skb)
381{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900382 if ((raw6_sk(sk)->checksum || sk->sk_filter) &&
Herbert Xufb286bb2005-11-10 13:01:24 -0800383 skb_checksum_complete(skb)) {
Wang Chena92aa312007-11-13 20:31:14 -0800384 atomic_inc(&sk->sk_drops);
Herbert Xufb286bb2005-11-10 13:01:24 -0800385 kfree_skb(skb);
386 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388
389 /* Charge it to the socket. */
390 if (sock_queue_rcv_skb(sk,skb)<0) {
Wang Chena92aa312007-11-13 20:31:14 -0800391 atomic_inc(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 kfree_skb(skb);
393 return 0;
394 }
395
396 return 0;
397}
398
399/*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900400 * This is next to useless...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 * if we demultiplex in network layer we don't need the extra call
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900402 * just to queue the skb...
403 * maybe we could have the network decide upon a hint if it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 * should call raw_rcv for demultiplexing
405 */
406int 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 Hideaki1ab14572007-02-09 23:24:49 +0900411 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
Wang Chena92aa312007-11-13 20:31:14 -0800412 atomic_inc(&sk->sk_drops);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900413 kfree_skb(skb);
414 return NET_RX_DROP;
415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 if (!rp->checksum)
418 skb->ip_summed = CHECKSUM_UNNECESSARY;
419
Patrick McHardy84fa7932006-08-29 16:44:56 -0700420 if (skb->ip_summed == CHECKSUM_COMPLETE) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700421 skb_postpull_rcsum(skb, skb_network_header(skb),
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300422 skb_network_header_len(skb));
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700423 if (!csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
424 &ipv6_hdr(skb)->daddr,
Herbert Xufb286bb2005-11-10 13:01:24 -0800425 skb->len, inet->num, skb->csum))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 skb->ip_summed = CHECKSUM_UNNECESSARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
Herbert Xu60476372007-04-09 11:59:39 -0700428 if (!skb_csum_unnecessary(skb))
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700429 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
430 &ipv6_hdr(skb)->daddr,
431 skb->len,
432 inet->num, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 if (inet->hdrincl) {
Herbert Xufb286bb2005-11-10 13:01:24 -0800435 if (skb_checksum_complete(skb)) {
Wang Chena92aa312007-11-13 20:31:14 -0800436 atomic_inc(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 kfree_skb(skb);
438 return 0;
439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
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
452static 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 Hideaki1ab14572007-02-09 23:24:49 +0900464
465 if (addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 *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 Hideaki1ab14572007-02-09 23:24:49 +0900476 if (copied > len) {
477 copied = len;
478 msg->msg_flags |= MSG_TRUNC;
479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Herbert Xu60476372007-04-09 11:59:39 -0700481 if (skb_csum_unnecessary(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
483 } else if (msg->msg_flags&MSG_TRUNC) {
Herbert Xufb286bb2005-11-10 13:01:24 -0800484 if (__skb_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 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 Handaf59fc7f2006-07-25 17:05:35 -0700498 sin6->sin6_port = 0;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700499 ipv6_addr_copy(&sin6->sin6_addr, &ipv6_hdr(skb)->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 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
515out_free:
516 skb_free_datagram(sk, skb);
517out:
518 return err;
519
520csum_copy_err:
Herbert Xu3305b802005-12-13 23:16:37 -0800521 skb_kill_datagram(sk, skb, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 /* Error for blocking case is chosen to masquerade
524 as some normal condition.
525 */
526 err = (flags&MSG_DONTWAIT) ? -EAGAIN : -EHOSTUNREACH;
Wang Chena92aa312007-11-13 20:31:14 -0800527 atomic_inc(&sk->sk_drops);
Herbert Xu3305b802005-12-13 23:16:37 -0800528 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
531static int rawv6_push_pending_frames(struct sock *sk, struct flowi *fl,
Herbert Xu357b40a2005-04-19 22:30:14 -0700532 struct raw6_sock *rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 struct sk_buff *skb;
535 int err = 0;
Herbert Xu357b40a2005-04-19 22:30:14 -0700536 int offset;
537 int len;
Herbert Xu679a8732005-05-03 14:24:36 -0700538 int total_len;
Al Viro868c86b2006-11-14 21:35:48 -0800539 __wsum tmp_csum;
540 __sum16 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 if (!rp->checksum)
543 goto send;
544
545 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
546 goto out;
547
Herbert Xu357b40a2005-04-19 22:30:14 -0700548 offset = rp->offset;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700549 total_len = inet_sk(sk)->cork.length - (skb_network_header(skb) -
550 skb->data);
Herbert Xu679a8732005-05-03 14:24:36 -0700551 if (offset >= total_len - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 err = -EINVAL;
Herbert Xu357b40a2005-04-19 22:30:14 -0700553 ip6_flush_pending_frames(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 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 Xu357b40a2005-04-19 22:30:14 -0700564 struct sk_buff *csum_skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 tmp_csum = 0;
566
567 skb_queue_walk(&sk->sk_write_queue, skb) {
568 tmp_csum = csum_add(tmp_csum, skb->csum);
Herbert Xu357b40a2005-04-19 22:30:14 -0700569
570 if (csum_skb)
571 continue;
572
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700573 len = skb->len - skb_transport_offset(skb);
Herbert Xu357b40a2005-04-19 22:30:14 -0700574 if (offset >= len) {
575 offset -= len;
576 continue;
577 }
578
579 csum_skb = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
Herbert Xu357b40a2005-04-19 22:30:14 -0700581
582 skb = csum_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700585 offset += skb_transport_offset(skb);
Herbert Xu357b40a2005-04-19 22:30:14 -0700586 if (skb_copy_bits(skb, offset, &csum, 2))
587 BUG();
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /* in case cksum was not initialized */
Herbert Xu357b40a2005-04-19 22:30:14 -0700590 if (unlikely(csum))
Al Viro5f92a732006-11-14 21:36:54 -0800591 tmp_csum = csum_sub(tmp_csum, csum_unfold(csum));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Al Viro868c86b2006-11-14 21:35:48 -0800593 csum = csum_ipv6_magic(&fl->fl6_src,
Herbert Xu357b40a2005-04-19 22:30:14 -0700594 &fl->fl6_dst,
Herbert Xu679a8732005-05-03 14:24:36 -0700595 total_len, fl->proto, tmp_csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Al Virof6ab0282006-11-16 02:36:50 -0800597 if (csum == 0 && fl->proto == IPPROTO_UDP)
598 csum = CSUM_MANGLED_0;
Herbert Xu357b40a2005-04-19 22:30:14 -0700599
Herbert Xu357b40a2005-04-19 22:30:14 -0700600 if (skb_store_bits(skb, offset, &csum, 2))
601 BUG();
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603send:
604 err = ip6_push_pending_frames(sk);
605out:
606 return err;
607}
608
609static int rawv6_send_hdrinc(struct sock *sk, void *from, int length,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900610 struct flowi *fl, struct rt6_info *rt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 unsigned int flags)
612{
Herbert Xu3320da82005-04-19 22:32:22 -0700613 struct ipv6_pinfo *np = inet6_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 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 Hideaki1ab14572007-02-09 23:24:49 +0900631 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 skb_reserve(skb, hh_len);
633
634 skb->priority = sk->sk_priority;
635 skb->dst = dst_clone(&rt->u.dst);
636
Arnaldo Carvalho de Melo1ced98e2007-03-10 19:57:15 -0300637 skb_put(skb, length);
638 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700639 iph = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 skb->ip_summed = CHECKSUM_NONE;
642
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700643 skb->transport_header = skb->network_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 err = memcpy_fromiovecend((void *)iph, from, 0, length);
645 if (err)
646 goto error_fault;
647
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900648 IP6_INC_STATS(rt->rt6i_idev, IPSTATS_MIB_OUTREQUESTS);
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800649 err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 dst_output);
651 if (err > 0)
Herbert Xu3320da82005-04-19 22:32:22 -0700652 err = np->recverr ? net_xmit_errno(err) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (err)
654 goto error;
655out:
656 return 0;
657
658error_fault:
659 err = -EFAULT;
660 kfree_skb(skb);
661error:
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900662 IP6_INC_STATS(rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900663 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
Heiko Carstensa27b58f2006-10-30 15:06:12 -0800666static int rawv6_probe_proto_opt(struct flowi *fl, struct msghdr *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 struct iovec *iov;
669 u8 __user *type = NULL;
670 u8 __user *code = NULL;
Masahide NAKAMURA6e8f4d42006-08-23 20:36:47 -0700671 u8 len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 int probed = 0;
673 int i;
674
675 if (!msg->msg_iov)
Heiko Carstensa27b58f2006-10-30 15:06:12 -0800676 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
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 Carstensa27b58f2006-10-30 15:06:12 -0800698 if (get_user(fl->fl_icmp_type, type) ||
699 get_user(fl->fl_icmp_code, code))
700 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 probed = 1;
702 }
703 break;
Masahide NAKAMURA6e8f4d42006-08-23 20:36:47 -0700704 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 Carstensa27b58f2006-10-30 15:06:12 -0800710 if (get_user(fl->fl_mh_type, &p[2 - len]))
711 return -EFAULT;
Masahide NAKAMURA6e8f4d42006-08-23 20:36:47 -0700712 probed = 1;
713 } else
714 len += iov->iov_len;
715
716 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 default:
718 probed = 1;
719 break;
720 }
721 if (probed)
722 break;
723 }
Heiko Carstensa27b58f2006-10-30 15:06:12 -0800724 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
727static 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 Hideaki41a1f8e2005-09-08 10:19:03 +0900742 int tclass = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 u16 proto;
744 int err;
745
746 /* Rough check on arithmetic overflow,
YOSHIFUJI Hideakib59e1392007-03-30 14:45:35 -0700747 better check is made in ip6_append_data().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 */
YOSHIFUJI Hideakib59e1392007-03-30 14:45:35 -0700749 if (len > INT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return -EMSGSIZE;
751
752 /* Mirror BSD error message compatibility */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900753 if (msg->msg_flags & MSG_OOB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return -EOPNOTSUPP;
755
756 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900757 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 */
759 memset(&fl, 0, sizeof(fl));
760
761 if (sin6) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900762 if (addr_len < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return -EINVAL;
764
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900765 if (sin6->sin6_family && sin6->sin6_family != AF_INET6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 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 Hideaki1ab14572007-02-09 23:24:49 +0900803 if (sk->sk_state != TCP_ESTABLISHED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return -EDESTADDRREQ;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 proto = inet->num;
807 daddr = &np->daddr;
808 fl.fl6_flowlabel = np->flow_label;
809 }
810
811 if (ipv6_addr_any(daddr)) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900812 /*
813 * unspecified destination address
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 * 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 Hideaki41a1f8e2005-09-08 10:19:03 +0900828 err = datagram_send_ctl(msg, &fl, opt, &hlimit, &tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 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 Hideakidf9890c2005-11-20 12:23:18 +0900843 if (flowlabel)
844 opt = fl6_merge_options(&opt_space, flowlabel, opt);
845 opt = ipv6_fixup_options(&opt_space, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 fl.proto = proto;
Heiko Carstensa27b58f2006-10-30 15:06:12 -0800848 err = rawv6_probe_proto_opt(&fl, msg);
849 if (err)
850 goto out;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 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 Yekkiralabeb8d132006-08-04 23:12:42 -0700866 security_sk_classify_flow(sk, &fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
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
David S. Miller14e50e52007-05-24 18:17:54 -0700874 if ((err = __xfrm_lookup(&dst, &fl, sk, 1)) < 0) {
875 if (err == -EREMOTE)
876 err = ip6_dst_blackhole(sk, &dst, &fl);
877 if (err < 0)
878 goto out;
879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
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 Hideaki41a1f8e2005-09-08 10:19:03 +0900892 if (tclass < 0) {
YOSHIFUJI Hideakie012d512006-09-13 20:01:28 -0700893 tclass = np->tclass;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900894 if (tclass < 0)
895 tclass = 0;
896 }
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (msg->msg_flags&MSG_CONFIRM)
899 goto do_confirm;
900
901back_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 Hideaki41a1f8e2005-09-08 10:19:03 +0900906 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 Torvalds1da177e2005-04-16 15:20:36 -0700909
910 if (err)
911 ip6_flush_pending_frames(sk);
912 else if (!(msg->msg_flags & MSG_MORE))
Herbert Xu357b40a2005-04-19 22:30:14 -0700913 err = rawv6_push_pending_frames(sk, &fl, rp);
YOSHIFUJI Hideaki3ef9d942007-09-14 16:45:40 -0700914 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
916done:
Nicolas DICHTEL6d3e85e2006-02-13 15:56:13 -0800917 dst_release(dst);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900918out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 fl6_sock_release(flowlabel);
920 return err<0?err:len;
921do_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 Hideaki1ab14572007-02-09 23:24:49 +0900929static int rawv6_seticmpfilter(struct sock *sk, int level, int optname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 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 Hemminger3ff50b72007-04-20 17:09:22 -0700941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 return 0;
944}
945
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900946static int rawv6_geticmpfilter(struct sock *sk, int level, int optname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 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 Hemminger3ff50b72007-04-20 17:09:22 -0700966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 return 0;
969}
970
971
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800972static int do_rawv6_setsockopt(struct sock *sk, int level, int optname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 char __user *optval, int optlen)
974{
975 struct raw6_sock *rp = raw6_sk(sk);
976 int val;
977
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900978 if (get_user(val, (int __user *)optval))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 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 Mishin3fdadf72006-03-20 22:45:21 -08001002static int rawv6_setsockopt(struct sock *sk, int level, int optname,
1003 char __user *optval, int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 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 Mishin3fdadf72006-03-20 22:45:21 -08001012 return rawv6_seticmpfilter(sk, level, optname, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 optlen);
1014 case SOL_IPV6:
1015 if (optname == IPV6_CHECKSUM)
1016 break;
1017 default:
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001018 return ipv6_setsockopt(sk, level, optname, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 optlen);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001020 }
1021
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001022 return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
1023}
1024
1025#ifdef CONFIG_COMPAT
1026static int compat_rawv6_setsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001027 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001028{
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001029 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 Mishin3fdadf72006-03-20 22:45:21 -08001038 break;
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001039 default:
1040 return compat_ipv6_setsockopt(sk, level, optname,
1041 optval, optlen);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001042 }
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001043 return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
1044}
1045#endif
1046
1047static 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 Torvalds1da177e2005-04-16 15:20:36 -07001052
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 Mishin3fdadf72006-03-20 22:45:21 -08001077static 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 Hemminger3ff50b72007-04-20 17:09:22 -07001095 }
1096
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001097 return do_rawv6_getsockopt(sk, level, optname, optval, optlen);
1098}
1099
1100#ifdef CONFIG_COMPAT
1101static int compat_rawv6_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001102 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001103{
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001104 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 Mishin3fdadf72006-03-20 22:45:21 -08001113 break;
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001114 default:
1115 return compat_ipv6_getsockopt(sk, level, optname,
1116 optval, optlen);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001117 }
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001118 return do_rawv6_getsockopt(sk, level, optname, optval, optlen);
1119}
1120#endif
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122static 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 Xue0f9f852005-06-18 22:56:18 -07001135 spin_lock_bh(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 skb = skb_peek(&sk->sk_receive_queue);
1137 if (skb != NULL)
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001138 amount = skb->tail - skb->transport_header;
Herbert Xue0f9f852005-06-18 22:56:18 -07001139 spin_unlock_bh(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 return put_user(amount, (int __user *)arg);
1141 }
1142
1143 default:
1144 return -ENOIOCTLCMD;
1145 }
1146}
1147
1148static 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
1156static int rawv6_init_sk(struct sock *sk)
1157{
Masahide NAKAMURAf48d5ff2007-02-07 00:07:39 -08001158 struct raw6_sock *rp = raw6_sk(sk);
1159
1160 switch (inet_sk(sk)->num) {
1161 case IPPROTO_ICMPV6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 rp->checksum = 1;
1163 rp->offset = 2;
Masahide NAKAMURAf48d5ff2007-02-07 00:07:39 -08001164 break;
1165 case IPPROTO_MH:
1166 rp->checksum = 1;
1167 rp->offset = 4;
1168 break;
1169 default:
1170 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172 return(0);
1173}
1174
Eric Dumazetc5a432f2007-11-05 23:39:51 -08001175DEFINE_PROTO_INUSE(rawv6)
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177struct proto rawv6_prot = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001178 .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 Mishin3fdadf72006-03-20 22:45:21 -08001195#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001196 .compat_setsockopt = compat_rawv6_setsockopt,
1197 .compat_getsockopt = compat_rawv6_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001198#endif
Eric Dumazetc5a432f2007-11-05 23:39:51 -08001199 REF_PROTO_INUSE(rawv6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200};
1201
1202#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203static 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 Chena92aa312007-11-13 20:31:14 -08001215 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 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 Hideaki1ab14572007-02-09 23:24:49 +09001221 sp->sk_state,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 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 Chena92aa312007-11-13 20:31:14 -08001227 atomic_read(&sp->sk_refcnt), sp, atomic_read(&sp->sk_drops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
1230static 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 Chena92aa312007-11-13 20:31:14 -08001238 " uid timeout inode drops\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 else
Pavel Emelyanov42a73802007-11-19 22:38:33 -08001240 raw6_sock_seq_show(seq, v, raw_seq_private(seq)->bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return 0;
1242}
1243
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001244static const struct seq_operations raw6_seq_ops = {
Pavel Emelyanov42a73802007-11-19 22:38:33 -08001245 .start = raw_seq_start,
1246 .next = raw_seq_next,
1247 .stop = raw_seq_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 .show = raw6_seq_show,
1249};
1250
1251static int raw6_seq_open(struct inode *inode, struct file *file)
1252{
Pavel Emelyanov42a73802007-11-19 22:38:33 -08001253 return raw_seq_open(file, &raw_v6_hashinfo, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254}
1255
Arjan van de Ven9a321442007-02-12 00:55:35 -08001256static const struct file_operations raw6_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 .owner = THIS_MODULE,
1258 .open = raw6_seq_open,
1259 .read = seq_read,
1260 .llseek = seq_lseek,
1261 .release = seq_release_private,
1262};
1263
1264int __init raw6_proc_init(void)
1265{
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001266 if (!proc_net_fops_create(&init_net, "raw6", S_IRUGO, &raw6_seq_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return -ENOMEM;
1268 return 0;
1269}
1270
1271void raw6_proc_exit(void)
1272{
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001273 proc_net_remove(&init_net, "raw6");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275#endif /* CONFIG_PROC_FS */
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001276
1277/* Same as inet6_dgram_ops, sans udp_poll. */
1278static 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
1303static 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
1313int __init rawv6_init(void)
1314{
1315 int ret;
1316
1317 ret = inet6_register_protosw(&rawv6_protosw);
1318 if (ret)
1319 goto out;
1320out:
1321 return ret;
1322}
1323
1324void __exit rawv6_exit(void)
1325{
1326 inet6_unregister_protosw(&rawv6_protosw);
1327}