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