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