patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / net / ipv6 / raw.c
1 /*
2  *      RAW sockets for IPv6
3  *      Linux INET6 implementation 
4  *
5  *      Authors:
6  *      Pedro Roque             <roque@di.fc.ul.pt>     
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
14  *      YOSHIFUJI,H.@USAGI      :       raw checksum (RFC2292(bis) compliance) 
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>
27 #include <linux/sched.h>
28 #include <linux/net.h>
29 #include <linux/in6.h>
30 #include <linux/netdevice.h>
31 #include <linux/if_arp.h>
32 #include <linux/icmpv6.h>
33 #include <linux/netfilter.h>
34 #include <linux/netfilter_ipv6.h>
35 #include <asm/uaccess.h>
36 #include <asm/ioctls.h>
37
38 #include <net/sock.h>
39 #include <net/snmp.h>
40
41 #include <net/ipv6.h>
42 #include <net/ndisc.h>
43 #include <net/protocol.h>
44 #include <net/ip6_route.h>
45 #include <net/addrconf.h>
46 #include <net/transp_v6.h>
47 #include <net/udp.h>
48 #include <net/inet_common.h>
49
50 #include <net/rawv6.h>
51 #include <net/xfrm.h>
52
53 #include <linux/proc_fs.h>
54 #include <linux/seq_file.h>
55
56 struct hlist_head raw_v6_htable[RAWV6_HTABLE_SIZE];
57 rwlock_t raw_v6_lock = RW_LOCK_UNLOCKED;
58
59 static void raw_v6_hash(struct sock *sk)
60 {
61         struct hlist_head *list = &raw_v6_htable[inet_sk(sk)->num &
62                                                  (RAWV6_HTABLE_SIZE - 1)];
63
64         write_lock_bh(&raw_v6_lock);
65         sk_add_node(sk, list);
66         sock_prot_inc_use(sk->sk_prot);
67         write_unlock_bh(&raw_v6_lock);
68 }
69
70 static void raw_v6_unhash(struct sock *sk)
71 {
72         write_lock_bh(&raw_v6_lock);
73         if (sk_del_node_init(sk))
74                 sock_prot_dec_use(sk->sk_prot);
75         write_unlock_bh(&raw_v6_lock);
76 }
77
78
79 /* Grumble... icmp and ip_input want to get at this... */
80 struct sock *__raw_v6_lookup(struct sock *sk, unsigned short num,
81                              struct in6_addr *loc_addr, struct in6_addr *rmt_addr)
82 {
83         struct hlist_node *node;
84         int is_multicast = ipv6_addr_is_multicast(loc_addr);
85
86         sk_for_each_from(sk, node)
87                 if (inet_sk(sk)->num == num) {
88                         struct ipv6_pinfo *np = inet6_sk(sk);
89
90                         if (!ipv6_addr_any(&np->daddr) &&
91                             ipv6_addr_cmp(&np->daddr, rmt_addr))
92                                 continue;
93
94                         if (!ipv6_addr_any(&np->rcv_saddr)) {
95                                 if (!ipv6_addr_cmp(&np->rcv_saddr, loc_addr))
96                                         goto found;
97                                 if (is_multicast &&
98                                     inet6_mc_check(sk, loc_addr, rmt_addr))
99                                         goto found;
100                                 continue;
101                         }
102                         goto found;
103                 }
104         sk = NULL;
105 found:
106         return sk;
107 }
108
109 /*
110  *      0 - deliver
111  *      1 - block
112  */
113 static __inline__ int icmpv6_filter(struct sock *sk, struct sk_buff *skb)
114 {
115         struct icmp6hdr *icmph;
116         struct raw6_opt *opt = raw6_sk(sk);
117
118         if (pskb_may_pull(skb, sizeof(struct icmp6hdr))) {
119                 __u32 *data = &opt->filter.data[0];
120                 int bit_nr;
121
122                 icmph = (struct icmp6hdr *) skb->data;
123                 bit_nr = icmph->icmp6_type;
124
125                 return (data[bit_nr >> 5] & (1 << (bit_nr & 31))) != 0;
126         }
127         return 0;
128 }
129
130 /*
131  *      demultiplex raw sockets.
132  *      (should consider queueing the skb in the sock receive_queue
133  *      without calling rawv6.c)
134  *
135  *      Caller owns SKB so we must make clones.
136  */
137 void ipv6_raw_deliver(struct sk_buff *skb, int nexthdr)
138 {
139         struct in6_addr *saddr;
140         struct in6_addr *daddr;
141         struct sock *sk;
142         __u8 hash;
143
144         saddr = &skb->nh.ipv6h->saddr;
145         daddr = saddr + 1;
146
147         hash = nexthdr & (MAX_INET_PROTOS - 1);
148
149         read_lock(&raw_v6_lock);
150         sk = sk_head(&raw_v6_htable[hash]);
151
152         /*
153          *      The first socket found will be delivered after
154          *      delivery to transport protocols.
155          */
156
157         if (sk == NULL)
158                 goto out;
159
160         sk = __raw_v6_lookup(sk, nexthdr, daddr, saddr);
161
162         while (sk) {
163                 if (nexthdr != IPPROTO_ICMPV6 || !icmpv6_filter(sk, skb)) {
164                         struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
165
166                         /* Not releasing hash table! */
167                         if (clone)
168                                 rawv6_rcv(sk, clone);
169                 }
170                 sk = __raw_v6_lookup(sk_next(sk), nexthdr, daddr, saddr);
171         }
172 out:
173         read_unlock(&raw_v6_lock);
174 }
175
176 /* This cleans up af_inet6 a bit. -DaveM */
177 static int rawv6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
178 {
179         struct inet_opt *inet = inet_sk(sk);
180         struct ipv6_pinfo *np = inet6_sk(sk);
181         struct sockaddr_in6 *addr = (struct sockaddr_in6 *) uaddr;
182         __u32 v4addr = 0;
183         int addr_type;
184         int err;
185
186         if (addr_len < SIN6_LEN_RFC2133)
187                 return -EINVAL;
188         addr_type = ipv6_addr_type(&addr->sin6_addr);
189
190         /* Raw sockets are IPv6 only */
191         if (addr_type == IPV6_ADDR_MAPPED)
192                 return(-EADDRNOTAVAIL);
193
194         lock_sock(sk);
195
196         err = -EINVAL;
197         if (sk->sk_state != TCP_CLOSE)
198                 goto out;
199
200         /* Check if the address belongs to the host. */
201         if (addr_type != IPV6_ADDR_ANY) {
202                 struct net_device *dev = NULL;
203
204                 if (addr_type & IPV6_ADDR_LINKLOCAL) {
205                         if (addr_len >= sizeof(struct sockaddr_in6) &&
206                             addr->sin6_scope_id) {
207                                 /* Override any existing binding, if another
208                                  * one is supplied by user.
209                                  */
210                                 sk->sk_bound_dev_if = addr->sin6_scope_id;
211                         }
212                         
213                         /* Binding to link-local address requires an interface */
214                         if (!sk->sk_bound_dev_if)
215                                 goto out;
216
217                         dev = dev_get_by_index(sk->sk_bound_dev_if);
218                         if (!dev) {
219                                 err = -ENODEV;
220                                 goto out;
221                         }
222                 }
223                 
224                 /* ipv4 addr of the socket is invalid.  Only the
225                  * unspecified and mapped address have a v4 equivalent.
226                  */
227                 v4addr = LOOPBACK4_IPV6;
228                 if (!(addr_type & IPV6_ADDR_MULTICAST)) {
229                         err = -EADDRNOTAVAIL;
230                         if (!ipv6_chk_addr(&addr->sin6_addr, dev, 0)) {
231                                 if (dev)
232                                         dev_put(dev);
233                                 goto out;
234                         }
235                 }
236                 if (dev)
237                         dev_put(dev);
238         }
239
240         inet->rcv_saddr = inet->saddr = v4addr;
241         ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
242         if (!(addr_type & IPV6_ADDR_MULTICAST))
243                 ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
244         err = 0;
245 out:
246         release_sock(sk);
247         return err;
248 }
249
250 void rawv6_err(struct sock *sk, struct sk_buff *skb,
251                struct inet6_skb_parm *opt,
252                int type, int code, int offset, u32 info)
253 {
254         struct inet_opt *inet = inet_sk(sk);
255         struct ipv6_pinfo *np = inet6_sk(sk);
256         int err;
257         int harderr;
258
259         /* Report error on raw socket, if:
260            1. User requested recverr.
261            2. Socket is connected (otherwise the error indication
262               is useless without recverr and error is hard.
263          */
264         if (!np->recverr && sk->sk_state != TCP_ESTABLISHED)
265                 return;
266
267         harderr = icmpv6_err_convert(type, code, &err);
268         if (type == ICMPV6_PKT_TOOBIG)
269                 harderr = (np->pmtudisc == IPV6_PMTUDISC_DO);
270
271         if (np->recverr) {
272                 u8 *payload = skb->data;
273                 if (!inet->hdrincl)
274                         payload += offset;
275                 ipv6_icmp_error(sk, skb, err, 0, ntohl(info), payload);
276         }
277
278         if (np->recverr || harderr) {
279                 sk->sk_err = err;
280                 sk->sk_error_report(sk);
281         }
282 }
283
284 static inline int rawv6_rcv_skb(struct sock * sk, struct sk_buff * skb)
285 {
286         if ((raw6_sk(sk)->checksum || sk->sk_filter) && 
287             skb->ip_summed != CHECKSUM_UNNECESSARY) {
288                 if ((unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum))) {
289                         /* FIXME: increment a raw6 drops counter here */
290                         kfree_skb(skb);
291                         return 0;
292                 }
293                 skb->ip_summed = CHECKSUM_UNNECESSARY;
294         }
295
296         /* Charge it to the socket. */
297         if (sock_queue_rcv_skb(sk,skb)<0) {
298                 /* FIXME: increment a raw6 drops counter here */
299                 kfree_skb(skb);
300                 return 0;
301         }
302
303         return 0;
304 }
305
306 /*
307  *      This is next to useless... 
308  *      if we demultiplex in network layer we don't need the extra call
309  *      just to queue the skb... 
310  *      maybe we could have the network decide upon a hint if it 
311  *      should call raw_rcv for demultiplexing
312  */
313 int rawv6_rcv(struct sock *sk, struct sk_buff *skb)
314 {
315         struct inet_opt *inet = inet_sk(sk);
316         struct raw6_opt *raw_opt = raw6_sk(sk);
317
318         if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
319                 kfree_skb(skb);
320                 return NET_RX_DROP;
321         }
322
323         if (!raw_opt->checksum)
324                 skb->ip_summed = CHECKSUM_UNNECESSARY;
325
326         if (skb->ip_summed != CHECKSUM_UNNECESSARY) {
327                 if (skb->ip_summed == CHECKSUM_HW) {
328                         skb->ip_summed = CHECKSUM_UNNECESSARY;
329                         if (csum_ipv6_magic(&skb->nh.ipv6h->saddr,
330                                             &skb->nh.ipv6h->daddr,
331                                             skb->len, inet->num, skb->csum)) {
332                                 LIMIT_NETDEBUG(
333                                 printk(KERN_DEBUG "raw v6 hw csum failure.\n"));
334                                 skb->ip_summed = CHECKSUM_NONE;
335                         }
336                 }
337                 if (skb->ip_summed == CHECKSUM_NONE)
338                         skb->csum = ~csum_ipv6_magic(&skb->nh.ipv6h->saddr,
339                                                      &skb->nh.ipv6h->daddr,
340                                                      skb->len, inet->num, 0);
341         }
342
343         if (inet->hdrincl) {
344                 if (skb->ip_summed != CHECKSUM_UNNECESSARY &&
345                     (unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum))) {
346                         /* FIXME: increment a raw6 drops counter here */
347                         kfree_skb(skb);
348                         return 0;
349                 }
350                 skb->ip_summed = CHECKSUM_UNNECESSARY;
351         }
352
353         rawv6_rcv_skb(sk, skb);
354         return 0;
355 }
356
357
358 /*
359  *      This should be easy, if there is something there
360  *      we return it, otherwise we block.
361  */
362
363 static int rawv6_recvmsg(struct kiocb *iocb, struct sock *sk,
364                   struct msghdr *msg, size_t len,
365                   int noblock, int flags, int *addr_len)
366 {
367         struct ipv6_pinfo *np = inet6_sk(sk);
368         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)msg->msg_name;
369         struct sk_buff *skb;
370         size_t copied;
371         int err;
372
373         if (flags & MSG_OOB)
374                 return -EOPNOTSUPP;
375                 
376         if (addr_len) 
377                 *addr_len=sizeof(*sin6);
378
379         if (flags & MSG_ERRQUEUE)
380                 return ipv6_recv_error(sk, msg, len);
381
382         skb = skb_recv_datagram(sk, flags, noblock, &err);
383         if (!skb)
384                 goto out;
385
386         copied = skb->len;
387         if (copied > len) {
388                 copied = len;
389                 msg->msg_flags |= MSG_TRUNC;
390         }
391
392         if (skb->ip_summed==CHECKSUM_UNNECESSARY) {
393                 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
394         } else if (msg->msg_flags&MSG_TRUNC) {
395                 if ((unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum)))
396                         goto csum_copy_err;
397                 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
398         } else {
399                 err = skb_copy_and_csum_datagram_iovec(skb, 0, msg->msg_iov);
400                 if (err == -EINVAL)
401                         goto csum_copy_err;
402         }
403         if (err)
404                 goto out_free;
405
406         /* Copy the address. */
407         if (sin6) {
408                 sin6->sin6_family = AF_INET6;
409                 ipv6_addr_copy(&sin6->sin6_addr, &skb->nh.ipv6h->saddr);
410                 sin6->sin6_flowinfo = 0;
411                 sin6->sin6_scope_id = 0;
412                 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
413                         sin6->sin6_scope_id = IP6CB(skb)->iif;
414         }
415
416         sock_recv_timestamp(msg, sk, skb);
417
418         if (np->rxopt.all)
419                 datagram_recv_ctl(sk, msg, skb);
420         err = copied;
421
422 out_free:
423         skb_free_datagram(sk, skb);
424 out:
425         return err;
426
427 csum_copy_err:
428         /* Clear queue. */
429         if (flags&MSG_PEEK) {
430                 int clear = 0;
431                 spin_lock_irq(&sk->sk_receive_queue.lock);
432                 if (skb == skb_peek(&sk->sk_receive_queue)) {
433                         __skb_unlink(skb, &sk->sk_receive_queue);
434                         clear = 1;
435                 }
436                 spin_unlock_irq(&sk->sk_receive_queue.lock);
437                 if (clear)
438                         kfree_skb(skb);
439         }
440
441         /* Error for blocking case is chosen to masquerade
442            as some normal condition.
443          */
444         err = (flags&MSG_DONTWAIT) ? -EAGAIN : -EHOSTUNREACH;
445         /* FIXME: increment a raw6 drops counter here */
446         goto out_free;
447 }
448
449 static int rawv6_push_pending_frames(struct sock *sk, struct flowi *fl, struct raw6_opt *opt, int len)
450 {
451         struct sk_buff *skb;
452         int err = 0;
453         u16 *csum;
454         u32 tmp_csum;
455
456         if (!opt->checksum)
457                 goto send;
458
459         if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
460                 goto out;
461
462         if (opt->offset + 1 < len)
463                 csum = (u16 *)(skb->h.raw + opt->offset);
464         else {
465                 err = -EINVAL;
466                 goto out;
467         }
468
469         /* should be check HW csum miyazawa */
470         if (skb_queue_len(&sk->sk_write_queue) == 1) {
471                 /*
472                  * Only one fragment on the socket.
473                  */
474                 tmp_csum = skb->csum;
475         } else {
476                 tmp_csum = 0;
477
478                 skb_queue_walk(&sk->sk_write_queue, skb) {
479                         tmp_csum = csum_add(tmp_csum, skb->csum);
480                 }
481         }
482
483         /* in case cksum was not initialized */
484         if (unlikely(*csum))
485                 tmp_csum = csum_sub(tmp_csum, *csum);
486
487         *csum = csum_ipv6_magic(&fl->fl6_src,
488                                 &fl->fl6_dst,
489                                 len, fl->proto, tmp_csum);
490
491         if (*csum == 0)
492                 *csum = -1;
493 send:
494         err = ip6_push_pending_frames(sk);
495 out:
496         return err;
497 }
498
499 static int rawv6_send_hdrinc(struct sock *sk, void *from, int length,
500                         struct flowi *fl, struct rt6_info *rt, 
501                         unsigned int flags)
502 {
503         struct inet_opt *inet = inet_sk(sk);
504         struct ipv6hdr *iph;
505         struct sk_buff *skb;
506         unsigned int hh_len;
507         int err;
508
509         if (length > rt->u.dst.dev->mtu) {
510                 ipv6_local_error(sk, EMSGSIZE, fl, rt->u.dst.dev->mtu);
511                 return -EMSGSIZE;
512         }
513         if (flags&MSG_PROBE)
514                 goto out;
515
516         hh_len = LL_RESERVED_SPACE(rt->u.dst.dev);
517
518         skb = sock_alloc_send_skb(sk, length+hh_len+15,
519                                   flags&MSG_DONTWAIT, &err);
520         if (skb == NULL)
521                 goto error; 
522         skb_reserve(skb, hh_len);
523
524         skb->priority = sk->sk_priority;
525         skb->dst = dst_clone(&rt->u.dst);
526
527         skb->nh.ipv6h = iph = (struct ipv6hdr *)skb_put(skb, length);
528
529         skb->ip_summed = CHECKSUM_NONE;
530
531         skb->h.raw = skb->nh.raw;
532         err = memcpy_fromiovecend((void *)iph, from, 0, length);
533         if (err)
534                 goto error_fault;
535
536         IP6_INC_STATS(OutRequests);             
537         err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
538                       dst_output);
539         if (err > 0)
540                 err = inet->recverr ? net_xmit_errno(err) : 0;
541         if (err)
542                 goto error;
543 out:
544         return 0;
545
546 error_fault:
547         err = -EFAULT;
548         kfree_skb(skb);
549 error:
550         IP6_INC_STATS(OutDiscards);
551         return err; 
552 }
553 static int rawv6_sendmsg(struct kiocb *iocb, struct sock *sk,
554                    struct msghdr *msg, size_t len)
555 {
556         struct ipv6_txoptions opt_space;
557         struct sockaddr_in6 * sin6 = (struct sockaddr_in6 *) msg->msg_name;
558         struct in6_addr *daddr;
559         struct inet_opt *inet = inet_sk(sk);
560         struct ipv6_pinfo *np = inet6_sk(sk);
561         struct raw6_opt *raw_opt = raw6_sk(sk);
562         struct ipv6_txoptions *opt = NULL;
563         struct ip6_flowlabel *flowlabel = NULL;
564         struct dst_entry *dst = NULL;
565         struct flowi fl;
566         int addr_len = msg->msg_namelen;
567         int hlimit = -1;
568         u16 proto;
569         int err;
570
571         /* Rough check on arithmetic overflow,
572            better check is made in ip6_build_xmit
573          */
574         if (len < 0)
575                 return -EMSGSIZE;
576
577         /* Mirror BSD error message compatibility */
578         if (msg->msg_flags & MSG_OOB)           
579                 return -EOPNOTSUPP;
580
581         /*
582          *      Get and verify the address. 
583          */
584         memset(&fl, 0, sizeof(fl));
585
586         if (sin6) {
587                 if (addr_len < SIN6_LEN_RFC2133) 
588                         return -EINVAL;
589
590                 if (sin6->sin6_family && sin6->sin6_family != AF_INET6) 
591                         return(-EINVAL);
592
593                 /* port is the proto value [0..255] carried in nexthdr */
594                 proto = ntohs(sin6->sin6_port);
595
596                 if (!proto)
597                         proto = inet->num;
598                 else if (proto != inet->num)
599                         return(-EINVAL);
600
601                 if (proto > 255)
602                         return(-EINVAL);
603
604                 daddr = &sin6->sin6_addr;
605                 if (np->sndflow) {
606                         fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
607                         if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
608                                 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
609                                 if (flowlabel == NULL)
610                                         return -EINVAL;
611                                 daddr = &flowlabel->dst;
612                         }
613                 }
614
615                 /*
616                  * Otherwise it will be difficult to maintain
617                  * sk->sk_dst_cache.
618                  */
619                 if (sk->sk_state == TCP_ESTABLISHED &&
620                     !ipv6_addr_cmp(daddr, &np->daddr))
621                         daddr = &np->daddr;
622
623                 if (addr_len >= sizeof(struct sockaddr_in6) &&
624                     sin6->sin6_scope_id &&
625                     ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
626                         fl.oif = sin6->sin6_scope_id;
627         } else {
628                 if (sk->sk_state != TCP_ESTABLISHED) 
629                         return -EDESTADDRREQ;
630                 
631                 proto = inet->num;
632                 daddr = &np->daddr;
633                 fl.fl6_flowlabel = np->flow_label;
634         }
635
636         if (ipv6_addr_any(daddr)) {
637                 /* 
638                  * unspecified destination address 
639                  * treated as error... is this correct ?
640                  */
641                 fl6_sock_release(flowlabel);
642                 return(-EINVAL);
643         }
644
645         if (fl.oif == 0)
646                 fl.oif = sk->sk_bound_dev_if;
647
648         if (msg->msg_controllen) {
649                 opt = &opt_space;
650                 memset(opt, 0, sizeof(struct ipv6_txoptions));
651                 opt->tot_len = sizeof(struct ipv6_txoptions);
652
653                 err = datagram_send_ctl(msg, &fl, opt, &hlimit);
654                 if (err < 0) {
655                         fl6_sock_release(flowlabel);
656                         return err;
657                 }
658                 if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
659                         flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
660                         if (flowlabel == NULL)
661                                 return -EINVAL;
662                 }
663                 if (!(opt->opt_nflen|opt->opt_flen))
664                         opt = NULL;
665         }
666         if (opt == NULL)
667                 opt = np->opt;
668         if (flowlabel)
669                 opt = fl6_merge_options(&opt_space, flowlabel, opt);
670
671         fl.proto = proto;
672         ipv6_addr_copy(&fl.fl6_dst, daddr);
673         if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr))
674                 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
675
676         /* merge ip6_build_xmit from ip6_output */
677         if (opt && opt->srcrt) {
678                 struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
679                 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
680         }
681
682         if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst))
683                 fl.oif = np->mcast_oif;
684
685         err = ip6_dst_lookup(sk, &dst, &fl);
686         if (err)
687                 goto out;
688
689         if (hlimit < 0) {
690                 if (ipv6_addr_is_multicast(&fl.fl6_dst))
691                         hlimit = np->mcast_hops;
692                 else
693                         hlimit = np->hop_limit;
694                 if (hlimit < 0)
695                         hlimit = dst_metric(dst, RTAX_HOPLIMIT);
696         }
697
698         if (msg->msg_flags&MSG_CONFIRM)
699                 goto do_confirm;
700
701 back_from_confirm:
702         if (inet->hdrincl) {
703                 err = rawv6_send_hdrinc(sk, msg->msg_iov, len, &fl, (struct rt6_info*)dst, msg->msg_flags);
704         } else {
705                 lock_sock(sk);
706                 err = ip6_append_data(sk, ip_generic_getfrag, msg->msg_iov, len, 0,
707                                         hlimit, opt, &fl, (struct rt6_info*)dst, msg->msg_flags);
708
709                 if (err)
710                         ip6_flush_pending_frames(sk);
711                 else if (!(msg->msg_flags & MSG_MORE))
712                         err = rawv6_push_pending_frames(sk, &fl, raw_opt, len);
713         }
714 done:
715         ip6_dst_store(sk, dst,
716                       !ipv6_addr_cmp(&fl.fl6_dst, &np->daddr) ?
717                       &np->daddr : NULL);
718         if (err > 0)
719                 err = np->recverr ? net_xmit_errno(err) : 0;
720
721         release_sock(sk);
722 out:    
723         fl6_sock_release(flowlabel);
724         return err<0?err:len;
725 do_confirm:
726         dst_confirm(dst);
727         if (!(msg->msg_flags & MSG_PROBE) || len)
728                 goto back_from_confirm;
729         err = 0;
730         goto done;
731 }
732
733 static int rawv6_seticmpfilter(struct sock *sk, int level, int optname, 
734                                char __user *optval, int optlen)
735 {
736         switch (optname) {
737         case ICMPV6_FILTER:
738                 if (optlen > sizeof(struct icmp6_filter))
739                         optlen = sizeof(struct icmp6_filter);
740                 if (copy_from_user(&raw6_sk(sk)->filter, optval, optlen))
741                         return -EFAULT;
742                 return 0;
743         default:
744                 return -ENOPROTOOPT;
745         };
746
747         return 0;
748 }
749
750 static int rawv6_geticmpfilter(struct sock *sk, int level, int optname, 
751                                char __user *optval, int __user *optlen)
752 {
753         int len;
754
755         switch (optname) {
756         case ICMPV6_FILTER:
757                 if (get_user(len, optlen))
758                         return -EFAULT;
759                 if (len < 0)
760                         return -EINVAL;
761                 if (len > sizeof(struct icmp6_filter))
762                         len = sizeof(struct icmp6_filter);
763                 if (put_user(len, optlen))
764                         return -EFAULT;
765                 if (copy_to_user(optval, &raw6_sk(sk)->filter, len))
766                         return -EFAULT;
767                 return 0;
768         default:
769                 return -ENOPROTOOPT;
770         };
771
772         return 0;
773 }
774
775
776 static int rawv6_setsockopt(struct sock *sk, int level, int optname, 
777                             char __user *optval, int optlen)
778 {
779         struct raw6_opt *opt = raw6_sk(sk);
780         int val;
781
782         switch(level) {
783                 case SOL_RAW:
784                         break;
785
786                 case SOL_ICMPV6:
787                         if (inet_sk(sk)->num != IPPROTO_ICMPV6)
788                                 return -EOPNOTSUPP;
789                         return rawv6_seticmpfilter(sk, level, optname, optval,
790                                                    optlen);
791                 case SOL_IPV6:
792                         if (optname == IPV6_CHECKSUM)
793                                 break;
794                 default:
795                         return ipv6_setsockopt(sk, level, optname, optval,
796                                                optlen);
797         };
798
799         if (get_user(val, (int __user *)optval))
800                 return -EFAULT;
801
802         switch (optname) {
803                 case IPV6_CHECKSUM:
804                         /* You may get strange result with a positive odd offset;
805                            RFC2292bis agrees with me. */
806                         if (val > 0 && (val&1))
807                                 return(-EINVAL);
808                         if (val < 0) {
809                                 opt->checksum = 0;
810                         } else {
811                                 opt->checksum = 1;
812                                 opt->offset = val;
813                         }
814
815                         return 0;
816                         break;
817
818                 default:
819                         return(-ENOPROTOOPT);
820         }
821 }
822
823 static int rawv6_getsockopt(struct sock *sk, int level, int optname, 
824                             char __user *optval, int __user *optlen)
825 {
826         struct raw6_opt *opt = raw6_sk(sk);
827         int val, len;
828
829         switch(level) {
830                 case SOL_RAW:
831                         break;
832
833                 case SOL_ICMPV6:
834                         if (inet_sk(sk)->num != IPPROTO_ICMPV6)
835                                 return -EOPNOTSUPP;
836                         return rawv6_geticmpfilter(sk, level, optname, optval,
837                                                    optlen);
838                 case SOL_IPV6:
839                         if (optname == IPV6_CHECKSUM)
840                                 break;
841                 default:
842                         return ipv6_getsockopt(sk, level, optname, optval,
843                                                optlen);
844         };
845
846         if (get_user(len,optlen))
847                 return -EFAULT;
848
849         switch (optname) {
850         case IPV6_CHECKSUM:
851                 if (opt->checksum == 0)
852                         val = -1;
853                 else
854                         val = opt->offset;
855                 break;
856
857         default:
858                 return -ENOPROTOOPT;
859         }
860
861         len = min_t(unsigned int, sizeof(int), len);
862
863         if (put_user(len, optlen))
864                 return -EFAULT;
865         if (copy_to_user(optval,&val,len))
866                 return -EFAULT;
867         return 0;
868 }
869
870 static int rawv6_ioctl(struct sock *sk, int cmd, unsigned long arg)
871 {
872         switch(cmd) {
873                 case SIOCOUTQ:
874                 {
875                         int amount = atomic_read(&sk->sk_wmem_alloc);
876                         return put_user(amount, (int __user *)arg);
877                 }
878                 case SIOCINQ:
879                 {
880                         struct sk_buff *skb;
881                         int amount = 0;
882
883                         spin_lock_irq(&sk->sk_receive_queue.lock);
884                         skb = skb_peek(&sk->sk_receive_queue);
885                         if (skb != NULL)
886                                 amount = skb->tail - skb->h.raw;
887                         spin_unlock_irq(&sk->sk_receive_queue.lock);
888                         return put_user(amount, (int __user *)arg);
889                 }
890
891                 default:
892                         return -ENOIOCTLCMD;
893         }
894 }
895
896 static void rawv6_close(struct sock *sk, long timeout)
897 {
898         if (inet_sk(sk)->num == IPPROTO_RAW)
899                 ip6_ra_control(sk, -1, NULL);
900
901         inet_sock_release(sk);
902 }
903
904 static int rawv6_init_sk(struct sock *sk)
905 {
906         if (inet_sk(sk)->num == IPPROTO_ICMPV6) {
907                 struct raw6_opt *opt = raw6_sk(sk);
908                 opt->checksum = 1;
909                 opt->offset = 2;
910         }
911         return(0);
912 }
913
914 struct proto rawv6_prot = {
915         .name =         "RAW",
916         .close =        rawv6_close,
917         .connect =      udpv6_connect,
918         .disconnect =   udp_disconnect,
919         .ioctl =        rawv6_ioctl,
920         .init =         rawv6_init_sk,
921         .destroy =      inet6_destroy_sock,
922         .setsockopt =   rawv6_setsockopt,
923         .getsockopt =   rawv6_getsockopt,
924         .sendmsg =      rawv6_sendmsg,
925         .recvmsg =      rawv6_recvmsg,
926         .bind =         rawv6_bind,
927         .backlog_rcv =  rawv6_rcv_skb,
928         .hash =         raw_v6_hash,
929         .unhash =       raw_v6_unhash,
930 };
931
932 #ifdef CONFIG_PROC_FS
933 struct raw6_iter_state {
934         int bucket;
935 };
936
937 #define raw6_seq_private(seq) ((struct raw6_iter_state *)(seq)->private)
938
939 static struct sock *raw6_get_first(struct seq_file *seq)
940 {
941         struct sock *sk;
942         struct hlist_node *node;
943         struct raw6_iter_state* state = raw6_seq_private(seq);
944
945         for (state->bucket = 0; state->bucket < RAWV6_HTABLE_SIZE; ++state->bucket)
946                 sk_for_each(sk, node, &raw_v6_htable[state->bucket])
947                         if (sk->sk_family == PF_INET6)
948                                 goto out;
949         sk = NULL;
950 out:
951         return sk;
952 }
953
954 static struct sock *raw6_get_next(struct seq_file *seq, struct sock *sk)
955 {
956         struct raw6_iter_state* state = raw6_seq_private(seq);
957
958         do {
959                 sk = sk_next(sk);
960 try_again:
961                 ;
962         } while (sk && sk->sk_family != PF_INET6);
963
964         if (!sk && ++state->bucket < RAWV6_HTABLE_SIZE) {
965                 sk = sk_head(&raw_v6_htable[state->bucket]);
966                 goto try_again;
967         }
968         return sk;
969 }
970
971 static struct sock *raw6_get_idx(struct seq_file *seq, loff_t pos)
972 {
973         struct sock *sk = raw6_get_first(seq);
974         if (sk)
975                 while (pos && (sk = raw6_get_next(seq, sk)) != NULL)
976                         --pos;
977         return pos ? NULL : sk;
978 }
979
980 static void *raw6_seq_start(struct seq_file *seq, loff_t *pos)
981 {
982         read_lock(&raw_v6_lock);
983         return *pos ? raw6_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
984 }
985
986 static void *raw6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
987 {
988         struct sock *sk;
989
990         if (v == SEQ_START_TOKEN)
991                 sk = raw6_get_first(seq);
992         else
993                 sk = raw6_get_next(seq, v);
994         ++*pos;
995         return sk;
996 }
997
998 static void raw6_seq_stop(struct seq_file *seq, void *v)
999 {
1000         read_unlock(&raw_v6_lock);
1001 }
1002
1003 static void raw6_sock_seq_show(struct seq_file *seq, struct sock *sp, int i)
1004 {
1005         struct ipv6_pinfo *np = inet6_sk(sp);
1006         struct in6_addr *dest, *src;
1007         __u16 destp, srcp;
1008
1009         dest  = &np->daddr;
1010         src   = &np->rcv_saddr;
1011         destp = 0;
1012         srcp  = inet_sk(sp)->num;
1013         seq_printf(seq,
1014                    "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1015                    "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p\n",
1016                    i,
1017                    src->s6_addr32[0], src->s6_addr32[1],
1018                    src->s6_addr32[2], src->s6_addr32[3], srcp,
1019                    dest->s6_addr32[0], dest->s6_addr32[1],
1020                    dest->s6_addr32[2], dest->s6_addr32[3], destp,
1021                    sp->sk_state, 
1022                    atomic_read(&sp->sk_wmem_alloc),
1023                    atomic_read(&sp->sk_rmem_alloc),
1024                    0, 0L, 0,
1025                    sock_i_uid(sp), 0,
1026                    sock_i_ino(sp),
1027                    atomic_read(&sp->sk_refcnt), sp);
1028 }
1029
1030 static int raw6_seq_show(struct seq_file *seq, void *v)
1031 {
1032         if (v == SEQ_START_TOKEN)
1033                 seq_printf(seq,
1034                            "  sl  "
1035                            "local_address                         "
1036                            "remote_address                        "
1037                            "st tx_queue rx_queue tr tm->when retrnsmt"
1038                            "   uid  timeout inode\n");
1039         else
1040                 raw6_sock_seq_show(seq, v, raw6_seq_private(seq)->bucket);
1041         return 0;
1042 }
1043
1044 static struct seq_operations raw6_seq_ops = {
1045         .start =        raw6_seq_start,
1046         .next =         raw6_seq_next,
1047         .stop =         raw6_seq_stop,
1048         .show =         raw6_seq_show,
1049 };
1050
1051 static int raw6_seq_open(struct inode *inode, struct file *file)
1052 {
1053         struct seq_file *seq;
1054         int rc = -ENOMEM;
1055         struct raw6_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
1056         if (!s)
1057                 goto out;
1058         rc = seq_open(file, &raw6_seq_ops);
1059         if (rc)
1060                 goto out_kfree;
1061         seq = file->private_data;
1062         seq->private = s;
1063         memset(s, 0, sizeof(*s));
1064 out:
1065         return rc;
1066 out_kfree:
1067         kfree(s);
1068         goto out;
1069 }
1070
1071 static struct file_operations raw6_seq_fops = {
1072         .owner =        THIS_MODULE,
1073         .open =         raw6_seq_open,
1074         .read =         seq_read,
1075         .llseek =       seq_lseek,
1076         .release =      seq_release_private,
1077 };
1078
1079 int __init raw6_proc_init(void)
1080 {
1081         if (!proc_net_fops_create("raw6", S_IRUGO, &raw6_seq_fops))
1082                 return -ENOMEM;
1083         return 0;
1084 }
1085
1086 void raw6_proc_exit(void)
1087 {
1088         proc_net_remove("raw6");
1089 }
1090 #endif  /* CONFIG_PROC_FS */