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