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