+- add patches.fixes/linux-post-2.6.3-20040220
[linux-flexiantxendom0-3.2.10.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 (sk->sk_filter && skb->ip_summed != CHECKSUM_UNNECESSARY) {
287                 if ((unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum))) {
288                         /* FIXME: increment a raw6 drops counter here */
289                         kfree_skb(skb);
290                         return 0;
291                 }
292                 skb->ip_summed = CHECKSUM_UNNECESSARY;
293         }
294
295         /* Charge it to the socket. */
296         if (sock_queue_rcv_skb(sk,skb)<0) {
297                 /* FIXME: increment a raw6 drops counter here */
298                 kfree_skb(skb);
299                 return 0;
300         }
301
302         return 0;
303 }
304
305 /*
306  *      This is next to useless... 
307  *      if we demultiplex in network layer we don't need the extra call
308  *      just to queue the skb... 
309  *      maybe we could have the network decide upon a hint if it 
310  *      should call raw_rcv for demultiplexing
311  */
312 int rawv6_rcv(struct sock *sk, struct sk_buff *skb)
313 {
314         struct inet_opt *inet = inet_sk(sk);
315         struct raw6_opt *raw_opt = raw6_sk(sk);
316
317         if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
318                 kfree_skb(skb);
319                 return NET_RX_DROP;
320         }
321
322         if (!raw_opt->checksum)
323                 skb->ip_summed = CHECKSUM_UNNECESSARY;
324
325         if (skb->ip_summed != CHECKSUM_UNNECESSARY) {
326                 if (skb->ip_summed == CHECKSUM_HW) {
327                         skb->ip_summed = CHECKSUM_UNNECESSARY;
328                         if (csum_ipv6_magic(&skb->nh.ipv6h->saddr,
329                                             &skb->nh.ipv6h->daddr,
330                                             skb->len, inet->num, skb->csum)) {
331                                 NETDEBUG(if (net_ratelimit()) printk(KERN_DEBUG "raw v6 hw csum failure.\n"));
332                                 skb->ip_summed = CHECKSUM_NONE;
333                         }
334                 }
335                 if (skb->ip_summed == CHECKSUM_NONE)
336                         skb->csum = ~csum_ipv6_magic(&skb->nh.ipv6h->saddr,
337                                                      &skb->nh.ipv6h->daddr,
338                                                      skb->len, inet->num, 0);
339         }
340
341         if (inet->hdrincl) {
342                 if (skb->ip_summed != CHECKSUM_UNNECESSARY &&
343                     (unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum))) {
344                         /* FIXME: increment a raw6 drops counter here */
345                         kfree_skb(skb);
346                         return 0;
347                 }
348                 skb->ip_summed = CHECKSUM_UNNECESSARY;
349         }
350
351         rawv6_rcv_skb(sk, skb);
352         return 0;
353 }
354
355
356 /*
357  *      This should be easy, if there is something there
358  *      we return it, otherwise we block.
359  */
360
361 static int rawv6_recvmsg(struct kiocb *iocb, struct sock *sk,
362                   struct msghdr *msg, size_t len,
363                   int noblock, int flags, int *addr_len)
364 {
365         struct ipv6_pinfo *np = inet6_sk(sk);
366         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)msg->msg_name;
367         struct sk_buff *skb;
368         size_t copied;
369         int err;
370
371         if (flags & MSG_OOB)
372                 return -EOPNOTSUPP;
373                 
374         if (addr_len) 
375                 *addr_len=sizeof(*sin6);
376
377         if (flags & MSG_ERRQUEUE)
378                 return ipv6_recv_error(sk, msg, len);
379
380         skb = skb_recv_datagram(sk, flags, noblock, &err);
381         if (!skb)
382                 goto out;
383
384         copied = skb->len;
385         if (copied > len) {
386                 copied = len;
387                 msg->msg_flags |= MSG_TRUNC;
388         }
389
390         if (skb->ip_summed==CHECKSUM_UNNECESSARY) {
391                 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
392         } else if (msg->msg_flags&MSG_TRUNC) {
393                 if ((unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum)))
394                         goto csum_copy_err;
395                 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
396         } else {
397                 err = skb_copy_and_csum_datagram_iovec(skb, 0, msg->msg_iov);
398                 if (err == -EINVAL)
399                         goto csum_copy_err;
400         }
401         if (err)
402                 goto out_free;
403
404         /* Copy the address. */
405         if (sin6) {
406                 sin6->sin6_family = AF_INET6;
407                 ipv6_addr_copy(&sin6->sin6_addr, &skb->nh.ipv6h->saddr);
408                 sin6->sin6_flowinfo = 0;
409                 sin6->sin6_scope_id = 0;
410                 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) {
411                         struct inet6_skb_parm *opt = (struct inet6_skb_parm *) skb->cb;
412                         sin6->sin6_scope_id = opt->iif;
413                 }
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
455         if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
456                 goto out;
457
458         if (opt->offset + 1 < len)
459                 csum = (u16 *)(skb->h.raw + opt->offset);
460         else {
461                 err = -EINVAL;
462                 goto out;
463         }
464
465         if (skb_queue_len(&sk->sk_write_queue) == 1) {
466                 /*
467                  * Only one fragment on the socket.
468                  */
469                 /* should be check HW csum miyazawa */
470                 *csum = csum_ipv6_magic(&fl->fl6_src,
471                                         &fl->fl6_dst,
472                                         len, fl->proto, skb->csum);
473         } else {
474                 u32 tmp_csum = 0;
475
476                 skb_queue_walk(&sk->sk_write_queue, skb) {
477                         tmp_csum = csum_add(tmp_csum, skb->csum);
478                 }
479
480                 tmp_csum = csum_ipv6_magic(&fl->fl6_src,
481                                            &fl->fl6_dst,
482                                            len, fl->proto, tmp_csum);
483                 *csum = tmp_csum;
484         }
485         if (*csum == 0)
486                 *csum = -1;
487         ip6_push_pending_frames(sk);
488 out:
489         return err;
490 }
491
492 static int rawv6_send_hdrinc(struct sock *sk, void *from, int length,
493                         struct flowi *fl, struct rt6_info *rt, 
494                         unsigned int flags)
495 {
496         struct inet_opt *inet = inet_sk(sk);
497         struct ipv6hdr *iph;
498         struct sk_buff *skb;
499         unsigned int hh_len;
500         int err;
501
502         if (length > rt->u.dst.dev->mtu) {
503                 ipv6_local_error(sk, EMSGSIZE, fl, rt->u.dst.dev->mtu);
504                 return -EMSGSIZE;
505         }
506         if (flags&MSG_PROBE)
507                 goto out;
508
509         hh_len = LL_RESERVED_SPACE(rt->u.dst.dev);
510
511         skb = sock_alloc_send_skb(sk, length+hh_len+15,
512                                   flags&MSG_DONTWAIT, &err);
513         if (skb == NULL)
514                 goto error; 
515         skb_reserve(skb, hh_len);
516
517         skb->priority = sk->sk_priority;
518         skb->dst = dst_clone(&rt->u.dst);
519
520         skb->nh.ipv6h = iph = (struct ipv6hdr *)skb_put(skb, length);
521
522         skb->ip_summed = CHECKSUM_NONE;
523
524         skb->h.raw = skb->nh.raw;
525         err = memcpy_fromiovecend((void *)iph, from, 0, length);
526         if (err)
527                 goto error_fault;
528
529         err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
530                       dst_output);
531         if (err > 0)
532                 err = inet->recverr ? net_xmit_errno(err) : 0;
533         if (err)
534                 goto error;
535 out:
536         return 0;
537
538 error_fault:
539         err = -EFAULT;
540         kfree_skb(skb);
541 error:
542         IP6_INC_STATS(Ip6OutDiscards);
543         return err; 
544 }
545 static int rawv6_sendmsg(struct kiocb *iocb, struct sock *sk,
546                    struct msghdr *msg, size_t len)
547 {
548         struct ipv6_txoptions opt_space;
549         struct sockaddr_in6 * sin6 = (struct sockaddr_in6 *) msg->msg_name;
550         struct in6_addr *daddr;
551         struct inet_opt *inet = inet_sk(sk);
552         struct ipv6_pinfo *np = inet6_sk(sk);
553         struct raw6_opt *raw_opt = raw6_sk(sk);
554         struct ipv6_txoptions *opt = NULL;
555         struct ip6_flowlabel *flowlabel = NULL;
556         struct dst_entry *dst = NULL;
557         struct flowi fl;
558         int addr_len = msg->msg_namelen;
559         int hlimit = -1;
560         u16 proto;
561         int err;
562
563         /* Rough check on arithmetic overflow,
564            better check is made in ip6_build_xmit
565          */
566         if (len < 0)
567                 return -EMSGSIZE;
568
569         /* Mirror BSD error message compatibility */
570         if (msg->msg_flags & MSG_OOB)           
571                 return -EOPNOTSUPP;
572
573         /*
574          *      Get and verify the address. 
575          */
576         memset(&fl, 0, sizeof(fl));
577
578         if (sin6) {
579                 if (addr_len < SIN6_LEN_RFC2133) 
580                         return -EINVAL;
581
582                 if (sin6->sin6_family && sin6->sin6_family != AF_INET6) 
583                         return(-EINVAL);
584
585                 /* port is the proto value [0..255] carried in nexthdr */
586                 proto = ntohs(sin6->sin6_port);
587
588                 if (!proto)
589                         proto = inet->num;
590                 else if (proto != inet->num)
591                         return(-EINVAL);
592
593                 if (proto > 255)
594                         return(-EINVAL);
595
596                 daddr = &sin6->sin6_addr;
597                 if (np->sndflow) {
598                         fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
599                         if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
600                                 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
601                                 if (flowlabel == NULL)
602                                         return -EINVAL;
603                                 daddr = &flowlabel->dst;
604                         }
605                 }
606
607                 /*
608                  * Otherwise it will be difficult to maintain
609                  * sk->sk_dst_cache.
610                  */
611                 if (sk->sk_state == TCP_ESTABLISHED &&
612                     !ipv6_addr_cmp(daddr, &np->daddr))
613                         daddr = &np->daddr;
614
615                 if (addr_len >= sizeof(struct sockaddr_in6) &&
616                     sin6->sin6_scope_id &&
617                     ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
618                         fl.oif = sin6->sin6_scope_id;
619         } else {
620                 if (sk->sk_state != TCP_ESTABLISHED) 
621                         return -EDESTADDRREQ;
622                 
623                 proto = inet->num;
624                 daddr = &np->daddr;
625                 fl.fl6_flowlabel = np->flow_label;
626         }
627
628         if (ipv6_addr_any(daddr)) {
629                 /* 
630                  * unspecified destination address 
631                  * treated as error... is this correct ?
632                  */
633                 fl6_sock_release(flowlabel);
634                 return(-EINVAL);
635         }
636
637         if (fl.oif == 0)
638                 fl.oif = sk->sk_bound_dev_if;
639
640         if (msg->msg_controllen) {
641                 opt = &opt_space;
642                 memset(opt, 0, sizeof(struct ipv6_txoptions));
643                 opt->tot_len = sizeof(struct ipv6_txoptions);
644
645                 err = datagram_send_ctl(msg, &fl, opt, &hlimit);
646                 if (err < 0) {
647                         fl6_sock_release(flowlabel);
648                         return err;
649                 }
650                 if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
651                         flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
652                         if (flowlabel == NULL)
653                                 return -EINVAL;
654                 }
655                 if (!(opt->opt_nflen|opt->opt_flen))
656                         opt = NULL;
657         }
658         if (opt == NULL)
659                 opt = np->opt;
660         if (flowlabel)
661                 opt = fl6_merge_options(&opt_space, flowlabel, opt);
662
663         fl.proto = proto;
664         ipv6_addr_copy(&fl.fl6_dst, daddr);
665         if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr))
666                 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
667
668         /* merge ip6_build_xmit from ip6_output */
669         if (opt && opt->srcrt) {
670                 struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
671                 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
672         }
673
674         if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst))
675                 fl.oif = np->mcast_oif;
676
677         err = ip6_dst_lookup(sk, &dst, &fl);
678         if (err)
679                 goto out;
680
681         if (hlimit < 0) {
682                 if (ipv6_addr_is_multicast(&fl.fl6_dst))
683                         hlimit = np->mcast_hops;
684                 else
685                         hlimit = np->hop_limit;
686                 if (hlimit < 0)
687                         hlimit = dst_metric(dst, RTAX_HOPLIMIT);
688         }
689
690         if (msg->msg_flags&MSG_CONFIRM)
691                 goto do_confirm;
692
693 back_from_confirm:
694         if (inet->hdrincl) {
695                 err = rawv6_send_hdrinc(sk, msg->msg_iov, len, &fl, (struct rt6_info*)dst, msg->msg_flags);
696         } else {
697                 lock_sock(sk);
698                 err = ip6_append_data(sk, ip_generic_getfrag, msg->msg_iov, len, 0,
699                                         hlimit, opt, &fl, (struct rt6_info*)dst, msg->msg_flags);
700
701                 if (err)
702                         ip6_flush_pending_frames(sk);
703                 else if (!(msg->msg_flags & MSG_MORE)) {
704                         if (raw_opt->checksum) {
705                                 err = rawv6_push_pending_frames(sk, &fl, raw_opt, len);
706                         } else {
707                                 err = ip6_push_pending_frames(sk);
708                         }
709                 }
710         }
711 done:
712         ip6_dst_store(sk, dst,
713                       !ipv6_addr_cmp(&fl.fl6_dst, &np->daddr) ?
714                       &np->daddr : NULL);
715         if (err > 0)
716                 err = np->recverr ? net_xmit_errno(err) : 0;
717
718         release_sock(sk);
719 out:    
720         fl6_sock_release(flowlabel);
721         return err<0?err:len;
722 do_confirm:
723         dst_confirm(dst);
724         if (!(msg->msg_flags & MSG_PROBE) || len)
725                 goto back_from_confirm;
726         err = 0;
727         goto done;
728 }
729
730 static int rawv6_seticmpfilter(struct sock *sk, int level, int optname, 
731                                char *optval, int optlen)
732 {
733         switch (optname) {
734         case ICMPV6_FILTER:
735                 if (optlen > sizeof(struct icmp6_filter))
736                         optlen = sizeof(struct icmp6_filter);
737                 if (copy_from_user(&raw6_sk(sk)->filter, optval, optlen))
738                         return -EFAULT;
739                 return 0;
740         default:
741                 return -ENOPROTOOPT;
742         };
743
744         return 0;
745 }
746
747 static int rawv6_geticmpfilter(struct sock *sk, int level, int optname, 
748                                char *optval, int *optlen)
749 {
750         int len;
751
752         switch (optname) {
753         case ICMPV6_FILTER:
754                 if (get_user(len, optlen))
755                         return -EFAULT;
756                 if (len < 0)
757                         return -EINVAL;
758                 if (len > sizeof(struct icmp6_filter))
759                         len = sizeof(struct icmp6_filter);
760                 if (put_user(len, optlen))
761                         return -EFAULT;
762                 if (copy_to_user(optval, &raw6_sk(sk)->filter, len))
763                         return -EFAULT;
764                 return 0;
765         default:
766                 return -ENOPROTOOPT;
767         };
768
769         return 0;
770 }
771
772
773 static int rawv6_setsockopt(struct sock *sk, int level, int optname, 
774                             char *optval, int optlen)
775 {
776         struct raw6_opt *opt = raw6_sk(sk);
777         int val;
778
779         switch(level) {
780                 case SOL_RAW:
781                         break;
782
783                 case SOL_ICMPV6:
784                         if (inet_sk(sk)->num != IPPROTO_ICMPV6)
785                                 return -EOPNOTSUPP;
786                         return rawv6_seticmpfilter(sk, level, optname, optval,
787                                                    optlen);
788                 case SOL_IPV6:
789                         if (optname == IPV6_CHECKSUM)
790                                 break;
791                 default:
792                         return ipv6_setsockopt(sk, level, optname, optval,
793                                                optlen);
794         };
795
796         if (get_user(val, (int *)optval))
797                 return -EFAULT;
798
799         switch (optname) {
800                 case IPV6_CHECKSUM:
801                         /* You may get strange result with a positive odd offset;
802                            RFC2292bis agrees with me. */
803                         if (val > 0 && (val&1))
804                                 return(-EINVAL);
805                         if (val < 0) {
806                                 opt->checksum = 0;
807                         } else {
808                                 opt->checksum = 1;
809                                 opt->offset = val;
810                         }
811
812                         return 0;
813                         break;
814
815                 default:
816                         return(-ENOPROTOOPT);
817         }
818 }
819
820 static int rawv6_getsockopt(struct sock *sk, int level, int optname, 
821                             char *optval, int *optlen)
822 {
823         struct raw6_opt *opt = raw6_sk(sk);
824         int val, len;
825
826         switch(level) {
827                 case SOL_RAW:
828                         break;
829
830                 case SOL_ICMPV6:
831                         if (inet_sk(sk)->num != IPPROTO_ICMPV6)
832                                 return -EOPNOTSUPP;
833                         return rawv6_geticmpfilter(sk, level, optname, optval,
834                                                    optlen);
835                 case SOL_IPV6:
836                         if (optname == IPV6_CHECKSUM)
837                                 break;
838                 default:
839                         return ipv6_getsockopt(sk, level, optname, optval,
840                                                optlen);
841         };
842
843         if (get_user(len,optlen))
844                 return -EFAULT;
845
846         switch (optname) {
847         case IPV6_CHECKSUM:
848                 if (opt->checksum == 0)
849                         val = -1;
850                 else
851                         val = opt->offset;
852                 break;
853
854         default:
855                 return -ENOPROTOOPT;
856         }
857
858         len = min_t(unsigned int, sizeof(int), len);
859
860         if (put_user(len, optlen))
861                 return -EFAULT;
862         if (copy_to_user(optval,&val,len))
863                 return -EFAULT;
864         return 0;
865 }
866
867 static int rawv6_ioctl(struct sock *sk, int cmd, unsigned long arg)
868 {
869         switch(cmd) {
870                 case SIOCOUTQ:
871                 {
872                         int amount = atomic_read(&sk->sk_wmem_alloc);
873                         return put_user(amount, (int *)arg);
874                 }
875                 case SIOCINQ:
876                 {
877                         struct sk_buff *skb;
878                         int amount = 0;
879
880                         spin_lock_irq(&sk->sk_receive_queue.lock);
881                         skb = skb_peek(&sk->sk_receive_queue);
882                         if (skb != NULL)
883                                 amount = skb->tail - skb->h.raw;
884                         spin_unlock_irq(&sk->sk_receive_queue.lock);
885                         return put_user(amount, (int *)arg);
886                 }
887
888                 default:
889                         return -ENOIOCTLCMD;
890         }
891 }
892
893 static void rawv6_close(struct sock *sk, long timeout)
894 {
895         if (inet_sk(sk)->num == IPPROTO_RAW)
896                 ip6_ra_control(sk, -1, NULL);
897
898         inet_sock_release(sk);
899 }
900
901 static int rawv6_init_sk(struct sock *sk)
902 {
903         if (inet_sk(sk)->num == IPPROTO_ICMPV6) {
904                 struct raw6_opt *opt = raw6_sk(sk);
905                 opt->checksum = 1;
906                 opt->offset = 2;
907         }
908         return(0);
909 }
910
911 struct proto rawv6_prot = {
912         .name =         "RAW",
913         .close =        rawv6_close,
914         .connect =      udpv6_connect,
915         .disconnect =   udp_disconnect,
916         .ioctl =        rawv6_ioctl,
917         .init =         rawv6_init_sk,
918         .destroy =      inet6_destroy_sock,
919         .setsockopt =   rawv6_setsockopt,
920         .getsockopt =   rawv6_getsockopt,
921         .sendmsg =      rawv6_sendmsg,
922         .recvmsg =      rawv6_recvmsg,
923         .bind =         rawv6_bind,
924         .backlog_rcv =  rawv6_rcv_skb,
925         .hash =         raw_v6_hash,
926         .unhash =       raw_v6_unhash,
927 };
928
929 #ifdef CONFIG_PROC_FS
930 struct raw6_iter_state {
931         int bucket;
932 };
933
934 #define raw6_seq_private(seq) ((struct raw6_iter_state *)(seq)->private)
935
936 static struct sock *raw6_get_first(struct seq_file *seq)
937 {
938         struct sock *sk;
939         struct hlist_node *node;
940         struct raw6_iter_state* state = raw6_seq_private(seq);
941
942         for (state->bucket = 0; state->bucket < RAWV6_HTABLE_SIZE; ++state->bucket)
943                 sk_for_each(sk, node, &raw_v6_htable[state->bucket])
944                         if (sk->sk_family == PF_INET6)
945                                 goto out;
946         sk = NULL;
947 out:
948         return sk;
949 }
950
951 static struct sock *raw6_get_next(struct seq_file *seq, struct sock *sk)
952 {
953         struct raw6_iter_state* state = raw6_seq_private(seq);
954
955         do {
956                 sk = sk_next(sk);
957 try_again:
958                 ;
959         } while (sk && sk->sk_family != PF_INET6);
960
961         if (!sk && ++state->bucket < RAWV6_HTABLE_SIZE) {
962                 sk = sk_head(&raw_v6_htable[state->bucket]);
963                 goto try_again;
964         }
965         return sk;
966 }
967
968 static struct sock *raw6_get_idx(struct seq_file *seq, loff_t pos)
969 {
970         struct sock *sk = raw6_get_first(seq);
971         if (sk)
972                 while (pos && (sk = raw6_get_next(seq, sk)) != NULL)
973                         --pos;
974         return pos ? NULL : sk;
975 }
976
977 static void *raw6_seq_start(struct seq_file *seq, loff_t *pos)
978 {
979         read_lock(&raw_v6_lock);
980         return *pos ? raw6_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
981 }
982
983 static void *raw6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
984 {
985         struct sock *sk;
986
987         if (v == SEQ_START_TOKEN)
988                 sk = raw6_get_first(seq);
989         else
990                 sk = raw6_get_next(seq, v);
991         ++*pos;
992         return sk;
993 }
994
995 static void raw6_seq_stop(struct seq_file *seq, void *v)
996 {
997         read_unlock(&raw_v6_lock);
998 }
999
1000 static void raw6_sock_seq_show(struct seq_file *seq, struct sock *sp, int i)
1001 {
1002         struct ipv6_pinfo *np = inet6_sk(sp);
1003         struct in6_addr *dest, *src;
1004         __u16 destp, srcp;
1005
1006         dest  = &np->daddr;
1007         src   = &np->rcv_saddr;
1008         destp = 0;
1009         srcp  = inet_sk(sp)->num;
1010         seq_printf(seq,
1011                    "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1012                    "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p\n",
1013                    i,
1014                    src->s6_addr32[0], src->s6_addr32[1],
1015                    src->s6_addr32[2], src->s6_addr32[3], srcp,
1016                    dest->s6_addr32[0], dest->s6_addr32[1],
1017                    dest->s6_addr32[2], dest->s6_addr32[3], destp,
1018                    sp->sk_state, 
1019                    atomic_read(&sp->sk_wmem_alloc),
1020                    atomic_read(&sp->sk_rmem_alloc),
1021                    0, 0L, 0,
1022                    sock_i_uid(sp), 0,
1023                    sock_i_ino(sp),
1024                    atomic_read(&sp->sk_refcnt), sp);
1025 }
1026
1027 static int raw6_seq_show(struct seq_file *seq, void *v)
1028 {
1029         if (v == SEQ_START_TOKEN)
1030                 seq_printf(seq,
1031                            "  sl  "
1032                            "local_address                         "
1033                            "remote_address                        "
1034                            "st tx_queue rx_queue tr tm->when retrnsmt"
1035                            "   uid  timeout inode\n");
1036         else
1037                 raw6_sock_seq_show(seq, v, raw6_seq_private(seq)->bucket);
1038         return 0;
1039 }
1040
1041 static struct seq_operations raw6_seq_ops = {
1042         .start =        raw6_seq_start,
1043         .next =         raw6_seq_next,
1044         .stop =         raw6_seq_stop,
1045         .show =         raw6_seq_show,
1046 };
1047
1048 static int raw6_seq_open(struct inode *inode, struct file *file)
1049 {
1050         struct seq_file *seq;
1051         int rc = -ENOMEM;
1052         struct raw6_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
1053         if (!s)
1054                 goto out;
1055         rc = seq_open(file, &raw6_seq_ops);
1056         if (rc)
1057                 goto out_kfree;
1058         seq = file->private_data;
1059         seq->private = s;
1060         memset(s, 0, sizeof(*s));
1061 out:
1062         return rc;
1063 out_kfree:
1064         kfree(s);
1065         goto out;
1066 }
1067
1068 static struct file_operations raw6_seq_fops = {
1069         .owner =        THIS_MODULE,
1070         .open =         raw6_seq_open,
1071         .read =         seq_read,
1072         .llseek =       seq_lseek,
1073         .release =      seq_release_private,
1074 };
1075
1076 int __init raw6_proc_init(void)
1077 {
1078         if (!proc_net_fops_create("raw6", S_IRUGO, &raw6_seq_fops))
1079                 return -ENOMEM;
1080         return 0;
1081 }
1082
1083 void raw6_proc_exit(void)
1084 {
1085         proc_net_remove("raw6");
1086 }
1087 #endif  /* CONFIG_PROC_FS */