cb0b110a2ac8c2a85b153fbe7a5b254620f4310d
[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(&init_net, &addr->sin6_addr,
302                                            dev, 0)) {
303                                 if (dev)
304                                         dev_put(dev);
305                                 goto out;
306                         }
307                 }
308                 if (dev)
309                         dev_put(dev);
310         }
311
312         inet->rcv_saddr = inet->saddr = v4addr;
313         ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
314         if (!(addr_type & IPV6_ADDR_MULTICAST))
315                 ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
316         err = 0;
317 out:
318         release_sock(sk);
319         return err;
320 }
321
322 static void rawv6_err(struct sock *sk, struct sk_buff *skb,
323                struct inet6_skb_parm *opt,
324                int type, int code, int offset, __be32 info)
325 {
326         struct inet_sock *inet = inet_sk(sk);
327         struct ipv6_pinfo *np = inet6_sk(sk);
328         int err;
329         int harderr;
330
331         /* Report error on raw socket, if:
332            1. User requested recverr.
333            2. Socket is connected (otherwise the error indication
334               is useless without recverr and error is hard.
335          */
336         if (!np->recverr && sk->sk_state != TCP_ESTABLISHED)
337                 return;
338
339         harderr = icmpv6_err_convert(type, code, &err);
340         if (type == ICMPV6_PKT_TOOBIG)
341                 harderr = (np->pmtudisc == IPV6_PMTUDISC_DO);
342
343         if (np->recverr) {
344                 u8 *payload = skb->data;
345                 if (!inet->hdrincl)
346                         payload += offset;
347                 ipv6_icmp_error(sk, skb, err, 0, ntohl(info), payload);
348         }
349
350         if (np->recverr || harderr) {
351                 sk->sk_err = err;
352                 sk->sk_error_report(sk);
353         }
354 }
355
356 void raw6_icmp_error(struct sk_buff *skb, int nexthdr,
357                 int type, int code, int inner_offset, __be32 info)
358 {
359         struct sock *sk;
360         int hash;
361         struct in6_addr *saddr, *daddr;
362
363         hash = nexthdr & (RAW_HTABLE_SIZE - 1);
364
365         read_lock(&raw_v6_hashinfo.lock);
366         sk = sk_head(&raw_v6_hashinfo.ht[hash]);
367         if (sk != NULL) {
368                 saddr = &ipv6_hdr(skb)->saddr;
369                 daddr = &ipv6_hdr(skb)->daddr;
370
371                 while ((sk = __raw_v6_lookup(sk, nexthdr, saddr, daddr,
372                                                 IP6CB(skb)->iif))) {
373                         rawv6_err(sk, skb, NULL, type, code,
374                                         inner_offset, info);
375                         sk = sk_next(sk);
376                 }
377         }
378         read_unlock(&raw_v6_hashinfo.lock);
379 }
380
381 static inline int rawv6_rcv_skb(struct sock * sk, struct sk_buff * skb)
382 {
383         if ((raw6_sk(sk)->checksum || sk->sk_filter) &&
384             skb_checksum_complete(skb)) {
385                 atomic_inc(&sk->sk_drops);
386                 kfree_skb(skb);
387                 return 0;
388         }
389
390         /* Charge it to the socket. */
391         if (sock_queue_rcv_skb(sk,skb)<0) {
392                 atomic_inc(&sk->sk_drops);
393                 kfree_skb(skb);
394                 return 0;
395         }
396
397         return 0;
398 }
399
400 /*
401  *      This is next to useless...
402  *      if we demultiplex in network layer we don't need the extra call
403  *      just to queue the skb...
404  *      maybe we could have the network decide upon a hint if it
405  *      should call raw_rcv for demultiplexing
406  */
407 int rawv6_rcv(struct sock *sk, struct sk_buff *skb)
408 {
409         struct inet_sock *inet = inet_sk(sk);
410         struct raw6_sock *rp = raw6_sk(sk);
411
412         if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
413                 atomic_inc(&sk->sk_drops);
414                 kfree_skb(skb);
415                 return NET_RX_DROP;
416         }
417
418         if (!rp->checksum)
419                 skb->ip_summed = CHECKSUM_UNNECESSARY;
420
421         if (skb->ip_summed == CHECKSUM_COMPLETE) {
422                 skb_postpull_rcsum(skb, skb_network_header(skb),
423                                    skb_network_header_len(skb));
424                 if (!csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
425                                      &ipv6_hdr(skb)->daddr,
426                                      skb->len, inet->num, skb->csum))
427                         skb->ip_summed = CHECKSUM_UNNECESSARY;
428         }
429         if (!skb_csum_unnecessary(skb))
430                 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
431                                                          &ipv6_hdr(skb)->daddr,
432                                                          skb->len,
433                                                          inet->num, 0));
434
435         if (inet->hdrincl) {
436                 if (skb_checksum_complete(skb)) {
437                         atomic_inc(&sk->sk_drops);
438                         kfree_skb(skb);
439                         return 0;
440                 }
441         }
442
443         rawv6_rcv_skb(sk, skb);
444         return 0;
445 }
446
447
448 /*
449  *      This should be easy, if there is something there
450  *      we return it, otherwise we block.
451  */
452
453 static int rawv6_recvmsg(struct kiocb *iocb, struct sock *sk,
454                   struct msghdr *msg, size_t len,
455                   int noblock, int flags, int *addr_len)
456 {
457         struct ipv6_pinfo *np = inet6_sk(sk);
458         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)msg->msg_name;
459         struct sk_buff *skb;
460         size_t copied;
461         int err;
462
463         if (flags & MSG_OOB)
464                 return -EOPNOTSUPP;
465
466         if (addr_len)
467                 *addr_len=sizeof(*sin6);
468
469         if (flags & MSG_ERRQUEUE)
470                 return ipv6_recv_error(sk, msg, len);
471
472         skb = skb_recv_datagram(sk, flags, noblock, &err);
473         if (!skb)
474                 goto out;
475
476         copied = skb->len;
477         if (copied > len) {
478                 copied = len;
479                 msg->msg_flags |= MSG_TRUNC;
480         }
481
482         if (skb_csum_unnecessary(skb)) {
483                 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
484         } else if (msg->msg_flags&MSG_TRUNC) {
485                 if (__skb_checksum_complete(skb))
486                         goto csum_copy_err;
487                 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
488         } else {
489                 err = skb_copy_and_csum_datagram_iovec(skb, 0, msg->msg_iov);
490                 if (err == -EINVAL)
491                         goto csum_copy_err;
492         }
493         if (err)
494                 goto out_free;
495
496         /* Copy the address. */
497         if (sin6) {
498                 sin6->sin6_family = AF_INET6;
499                 sin6->sin6_port = 0;
500                 ipv6_addr_copy(&sin6->sin6_addr, &ipv6_hdr(skb)->saddr);
501                 sin6->sin6_flowinfo = 0;
502                 sin6->sin6_scope_id = 0;
503                 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
504                         sin6->sin6_scope_id = IP6CB(skb)->iif;
505         }
506
507         sock_recv_timestamp(msg, sk, skb);
508
509         if (np->rxopt.all)
510                 datagram_recv_ctl(sk, msg, skb);
511
512         err = copied;
513         if (flags & MSG_TRUNC)
514                 err = skb->len;
515
516 out_free:
517         skb_free_datagram(sk, skb);
518 out:
519         return err;
520
521 csum_copy_err:
522         skb_kill_datagram(sk, skb, flags);
523
524         /* Error for blocking case is chosen to masquerade
525            as some normal condition.
526          */
527         err = (flags&MSG_DONTWAIT) ? -EAGAIN : -EHOSTUNREACH;
528         atomic_inc(&sk->sk_drops);
529         goto out;
530 }
531
532 static int rawv6_push_pending_frames(struct sock *sk, struct flowi *fl,
533                                      struct raw6_sock *rp)
534 {
535         struct sk_buff *skb;
536         int err = 0;
537         int offset;
538         int len;
539         int total_len;
540         __wsum tmp_csum;
541         __sum16 csum;
542
543         if (!rp->checksum)
544                 goto send;
545
546         if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
547                 goto out;
548
549         offset = rp->offset;
550         total_len = inet_sk(sk)->cork.length - (skb_network_header(skb) -
551                                                 skb->data);
552         if (offset >= total_len - 1) {
553                 err = -EINVAL;
554                 ip6_flush_pending_frames(sk);
555                 goto out;
556         }
557
558         /* should be check HW csum miyazawa */
559         if (skb_queue_len(&sk->sk_write_queue) == 1) {
560                 /*
561                  * Only one fragment on the socket.
562                  */
563                 tmp_csum = skb->csum;
564         } else {
565                 struct sk_buff *csum_skb = NULL;
566                 tmp_csum = 0;
567
568                 skb_queue_walk(&sk->sk_write_queue, skb) {
569                         tmp_csum = csum_add(tmp_csum, skb->csum);
570
571                         if (csum_skb)
572                                 continue;
573
574                         len = skb->len - skb_transport_offset(skb);
575                         if (offset >= len) {
576                                 offset -= len;
577                                 continue;
578                         }
579
580                         csum_skb = skb;
581                 }
582
583                 skb = csum_skb;
584         }
585
586         offset += skb_transport_offset(skb);
587         if (skb_copy_bits(skb, offset, &csum, 2))
588                 BUG();
589
590         /* in case cksum was not initialized */
591         if (unlikely(csum))
592                 tmp_csum = csum_sub(tmp_csum, csum_unfold(csum));
593
594         csum = csum_ipv6_magic(&fl->fl6_src,
595                                    &fl->fl6_dst,
596                                    total_len, fl->proto, tmp_csum);
597
598         if (csum == 0 && fl->proto == IPPROTO_UDP)
599                 csum = CSUM_MANGLED_0;
600
601         if (skb_store_bits(skb, offset, &csum, 2))
602                 BUG();
603
604 send:
605         err = ip6_push_pending_frames(sk);
606 out:
607         return err;
608 }
609
610 static int rawv6_send_hdrinc(struct sock *sk, void *from, int length,
611                         struct flowi *fl, struct rt6_info *rt,
612                         unsigned int flags)
613 {
614         struct ipv6_pinfo *np = inet6_sk(sk);
615         struct ipv6hdr *iph;
616         struct sk_buff *skb;
617         unsigned int hh_len;
618         int err;
619
620         if (length > rt->u.dst.dev->mtu) {
621                 ipv6_local_error(sk, EMSGSIZE, fl, rt->u.dst.dev->mtu);
622                 return -EMSGSIZE;
623         }
624         if (flags&MSG_PROBE)
625                 goto out;
626
627         hh_len = LL_RESERVED_SPACE(rt->u.dst.dev);
628
629         skb = sock_alloc_send_skb(sk, length+hh_len+15,
630                                   flags&MSG_DONTWAIT, &err);
631         if (skb == NULL)
632                 goto error;
633         skb_reserve(skb, hh_len);
634
635         skb->priority = sk->sk_priority;
636         skb->dst = dst_clone(&rt->u.dst);
637
638         skb_put(skb, length);
639         skb_reset_network_header(skb);
640         iph = ipv6_hdr(skb);
641
642         skb->ip_summed = CHECKSUM_NONE;
643
644         skb->transport_header = skb->network_header;
645         err = memcpy_fromiovecend((void *)iph, from, 0, length);
646         if (err)
647                 goto error_fault;
648
649         IP6_INC_STATS(rt->rt6i_idev, IPSTATS_MIB_OUTREQUESTS);
650         err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
651                       dst_output);
652         if (err > 0)
653                 err = np->recverr ? net_xmit_errno(err) : 0;
654         if (err)
655                 goto error;
656 out:
657         return 0;
658
659 error_fault:
660         err = -EFAULT;
661         kfree_skb(skb);
662 error:
663         IP6_INC_STATS(rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
664         return err;
665 }
666
667 static int rawv6_probe_proto_opt(struct flowi *fl, struct msghdr *msg)
668 {
669         struct iovec *iov;
670         u8 __user *type = NULL;
671         u8 __user *code = NULL;
672         u8 len = 0;
673         int probed = 0;
674         int i;
675
676         if (!msg->msg_iov)
677                 return 0;
678
679         for (i = 0; i < msg->msg_iovlen; i++) {
680                 iov = &msg->msg_iov[i];
681                 if (!iov)
682                         continue;
683
684                 switch (fl->proto) {
685                 case IPPROTO_ICMPV6:
686                         /* check if one-byte field is readable or not. */
687                         if (iov->iov_base && iov->iov_len < 1)
688                                 break;
689
690                         if (!type) {
691                                 type = iov->iov_base;
692                                 /* check if code field is readable or not. */
693                                 if (iov->iov_len > 1)
694                                         code = type + 1;
695                         } else if (!code)
696                                 code = iov->iov_base;
697
698                         if (type && code) {
699                                 if (get_user(fl->fl_icmp_type, type) ||
700                                     get_user(fl->fl_icmp_code, code))
701                                         return -EFAULT;
702                                 probed = 1;
703                         }
704                         break;
705                 case IPPROTO_MH:
706                         if (iov->iov_base && iov->iov_len < 1)
707                                 break;
708                         /* check if type field is readable or not. */
709                         if (iov->iov_len > 2 - len) {
710                                 u8 __user *p = iov->iov_base;
711                                 if (get_user(fl->fl_mh_type, &p[2 - len]))
712                                         return -EFAULT;
713                                 probed = 1;
714                         } else
715                                 len += iov->iov_len;
716
717                         break;
718                 default:
719                         probed = 1;
720                         break;
721                 }
722                 if (probed)
723                         break;
724         }
725         return 0;
726 }
727
728 static int rawv6_sendmsg(struct kiocb *iocb, struct sock *sk,
729                    struct msghdr *msg, size_t len)
730 {
731         struct ipv6_txoptions opt_space;
732         struct sockaddr_in6 * sin6 = (struct sockaddr_in6 *) msg->msg_name;
733         struct in6_addr *daddr, *final_p = NULL, final;
734         struct inet_sock *inet = inet_sk(sk);
735         struct ipv6_pinfo *np = inet6_sk(sk);
736         struct raw6_sock *rp = raw6_sk(sk);
737         struct ipv6_txoptions *opt = NULL;
738         struct ip6_flowlabel *flowlabel = NULL;
739         struct dst_entry *dst = NULL;
740         struct flowi fl;
741         int addr_len = msg->msg_namelen;
742         int hlimit = -1;
743         int tclass = -1;
744         u16 proto;
745         int err;
746
747         /* Rough check on arithmetic overflow,
748            better check is made in ip6_append_data().
749          */
750         if (len > INT_MAX)
751                 return -EMSGSIZE;
752
753         /* Mirror BSD error message compatibility */
754         if (msg->msg_flags & MSG_OOB)
755                 return -EOPNOTSUPP;
756
757         /*
758          *      Get and verify the address.
759          */
760         memset(&fl, 0, sizeof(fl));
761
762         if (sin6) {
763                 if (addr_len < SIN6_LEN_RFC2133)
764                         return -EINVAL;
765
766                 if (sin6->sin6_family && sin6->sin6_family != AF_INET6)
767                         return(-EAFNOSUPPORT);
768
769                 /* port is the proto value [0..255] carried in nexthdr */
770                 proto = ntohs(sin6->sin6_port);
771
772                 if (!proto)
773                         proto = inet->num;
774                 else if (proto != inet->num)
775                         return(-EINVAL);
776
777                 if (proto > 255)
778                         return(-EINVAL);
779
780                 daddr = &sin6->sin6_addr;
781                 if (np->sndflow) {
782                         fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
783                         if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
784                                 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
785                                 if (flowlabel == NULL)
786                                         return -EINVAL;
787                                 daddr = &flowlabel->dst;
788                         }
789                 }
790
791                 /*
792                  * Otherwise it will be difficult to maintain
793                  * sk->sk_dst_cache.
794                  */
795                 if (sk->sk_state == TCP_ESTABLISHED &&
796                     ipv6_addr_equal(daddr, &np->daddr))
797                         daddr = &np->daddr;
798
799                 if (addr_len >= sizeof(struct sockaddr_in6) &&
800                     sin6->sin6_scope_id &&
801                     ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
802                         fl.oif = sin6->sin6_scope_id;
803         } else {
804                 if (sk->sk_state != TCP_ESTABLISHED)
805                         return -EDESTADDRREQ;
806
807                 proto = inet->num;
808                 daddr = &np->daddr;
809                 fl.fl6_flowlabel = np->flow_label;
810         }
811
812         if (ipv6_addr_any(daddr)) {
813                 /*
814                  * unspecified destination address
815                  * treated as error... is this correct ?
816                  */
817                 fl6_sock_release(flowlabel);
818                 return(-EINVAL);
819         }
820
821         if (fl.oif == 0)
822                 fl.oif = sk->sk_bound_dev_if;
823
824         if (msg->msg_controllen) {
825                 opt = &opt_space;
826                 memset(opt, 0, sizeof(struct ipv6_txoptions));
827                 opt->tot_len = sizeof(struct ipv6_txoptions);
828
829                 err = datagram_send_ctl(msg, &fl, opt, &hlimit, &tclass);
830                 if (err < 0) {
831                         fl6_sock_release(flowlabel);
832                         return err;
833                 }
834                 if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
835                         flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
836                         if (flowlabel == NULL)
837                                 return -EINVAL;
838                 }
839                 if (!(opt->opt_nflen|opt->opt_flen))
840                         opt = NULL;
841         }
842         if (opt == NULL)
843                 opt = np->opt;
844         if (flowlabel)
845                 opt = fl6_merge_options(&opt_space, flowlabel, opt);
846         opt = ipv6_fixup_options(&opt_space, opt);
847
848         fl.proto = proto;
849         err = rawv6_probe_proto_opt(&fl, msg);
850         if (err)
851                 goto out;
852
853         ipv6_addr_copy(&fl.fl6_dst, daddr);
854         if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr))
855                 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
856
857         /* merge ip6_build_xmit from ip6_output */
858         if (opt && opt->srcrt) {
859                 struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
860                 ipv6_addr_copy(&final, &fl.fl6_dst);
861                 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
862                 final_p = &final;
863         }
864
865         if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst))
866                 fl.oif = np->mcast_oif;
867         security_sk_classify_flow(sk, &fl);
868
869         err = ip6_dst_lookup(sk, &dst, &fl);
870         if (err)
871                 goto out;
872         if (final_p)
873                 ipv6_addr_copy(&fl.fl6_dst, final_p);
874
875         if ((err = __xfrm_lookup(&dst, &fl, sk, XFRM_LOOKUP_WAIT)) < 0) {
876                 if (err == -EREMOTE)
877                         err = ip6_dst_blackhole(sk, &dst, &fl);
878                 if (err < 0)
879                         goto out;
880         }
881
882         if (hlimit < 0) {
883                 if (ipv6_addr_is_multicast(&fl.fl6_dst))
884                         hlimit = np->mcast_hops;
885                 else
886                         hlimit = np->hop_limit;
887                 if (hlimit < 0)
888                         hlimit = dst_metric(dst, RTAX_HOPLIMIT);
889                 if (hlimit < 0)
890                         hlimit = ipv6_get_hoplimit(dst->dev);
891         }
892
893         if (tclass < 0) {
894                 tclass = np->tclass;
895                 if (tclass < 0)
896                         tclass = 0;
897         }
898
899         if (msg->msg_flags&MSG_CONFIRM)
900                 goto do_confirm;
901
902 back_from_confirm:
903         if (inet->hdrincl) {
904                 err = rawv6_send_hdrinc(sk, msg->msg_iov, len, &fl, (struct rt6_info*)dst, msg->msg_flags);
905         } else {
906                 lock_sock(sk);
907                 err = ip6_append_data(sk, ip_generic_getfrag, msg->msg_iov,
908                         len, 0, hlimit, tclass, opt, &fl, (struct rt6_info*)dst,
909                         msg->msg_flags);
910
911                 if (err)
912                         ip6_flush_pending_frames(sk);
913                 else if (!(msg->msg_flags & MSG_MORE))
914                         err = rawv6_push_pending_frames(sk, &fl, rp);
915                 release_sock(sk);
916         }
917 done:
918         dst_release(dst);
919 out:
920         fl6_sock_release(flowlabel);
921         return err<0?err:len;
922 do_confirm:
923         dst_confirm(dst);
924         if (!(msg->msg_flags & MSG_PROBE) || len)
925                 goto back_from_confirm;
926         err = 0;
927         goto done;
928 }
929
930 static int rawv6_seticmpfilter(struct sock *sk, int level, int optname,
931                                char __user *optval, int optlen)
932 {
933         switch (optname) {
934         case ICMPV6_FILTER:
935                 if (optlen > sizeof(struct icmp6_filter))
936                         optlen = sizeof(struct icmp6_filter);
937                 if (copy_from_user(&raw6_sk(sk)->filter, optval, optlen))
938                         return -EFAULT;
939                 return 0;
940         default:
941                 return -ENOPROTOOPT;
942         }
943
944         return 0;
945 }
946
947 static int rawv6_geticmpfilter(struct sock *sk, int level, int optname,
948                                char __user *optval, int __user *optlen)
949 {
950         int len;
951
952         switch (optname) {
953         case ICMPV6_FILTER:
954                 if (get_user(len, optlen))
955                         return -EFAULT;
956                 if (len < 0)
957                         return -EINVAL;
958                 if (len > sizeof(struct icmp6_filter))
959                         len = sizeof(struct icmp6_filter);
960                 if (put_user(len, optlen))
961                         return -EFAULT;
962                 if (copy_to_user(optval, &raw6_sk(sk)->filter, len))
963                         return -EFAULT;
964                 return 0;
965         default:
966                 return -ENOPROTOOPT;
967         }
968
969         return 0;
970 }
971
972
973 static int do_rawv6_setsockopt(struct sock *sk, int level, int optname,
974                             char __user *optval, int optlen)
975 {
976         struct raw6_sock *rp = raw6_sk(sk);
977         int val;
978
979         if (get_user(val, (int __user *)optval))
980                 return -EFAULT;
981
982         switch (optname) {
983                 case IPV6_CHECKSUM:
984                         /* You may get strange result with a positive odd offset;
985                            RFC2292bis agrees with me. */
986                         if (val > 0 && (val&1))
987                                 return(-EINVAL);
988                         if (val < 0) {
989                                 rp->checksum = 0;
990                         } else {
991                                 rp->checksum = 1;
992                                 rp->offset = val;
993                         }
994
995                         return 0;
996                         break;
997
998                 default:
999                         return(-ENOPROTOOPT);
1000         }
1001 }
1002
1003 static int rawv6_setsockopt(struct sock *sk, int level, int optname,
1004                           char __user *optval, int optlen)
1005 {
1006         switch(level) {
1007                 case SOL_RAW:
1008                         break;
1009
1010                 case SOL_ICMPV6:
1011                         if (inet_sk(sk)->num != IPPROTO_ICMPV6)
1012                                 return -EOPNOTSUPP;
1013                         return rawv6_seticmpfilter(sk, level, optname, optval,
1014                                                    optlen);
1015                 case SOL_IPV6:
1016                         if (optname == IPV6_CHECKSUM)
1017                                 break;
1018                 default:
1019                         return ipv6_setsockopt(sk, level, optname, optval,
1020                                                optlen);
1021         }
1022
1023         return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
1024 }
1025
1026 #ifdef CONFIG_COMPAT
1027 static int compat_rawv6_setsockopt(struct sock *sk, int level, int optname,
1028                                    char __user *optval, int optlen)
1029 {
1030         switch (level) {
1031         case SOL_RAW:
1032                 break;
1033         case SOL_ICMPV6:
1034                 if (inet_sk(sk)->num != IPPROTO_ICMPV6)
1035                         return -EOPNOTSUPP;
1036                 return rawv6_seticmpfilter(sk, level, optname, optval, optlen);
1037         case SOL_IPV6:
1038                 if (optname == IPV6_CHECKSUM)
1039                         break;
1040         default:
1041                 return compat_ipv6_setsockopt(sk, level, optname,
1042                                               optval, optlen);
1043         }
1044         return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
1045 }
1046 #endif
1047
1048 static int do_rawv6_getsockopt(struct sock *sk, int level, int optname,
1049                             char __user *optval, int __user *optlen)
1050 {
1051         struct raw6_sock *rp = raw6_sk(sk);
1052         int val, len;
1053
1054         if (get_user(len,optlen))
1055                 return -EFAULT;
1056
1057         switch (optname) {
1058         case IPV6_CHECKSUM:
1059                 if (rp->checksum == 0)
1060                         val = -1;
1061                 else
1062                         val = rp->offset;
1063                 break;
1064
1065         default:
1066                 return -ENOPROTOOPT;
1067         }
1068
1069         len = min_t(unsigned int, sizeof(int), len);
1070
1071         if (put_user(len, optlen))
1072                 return -EFAULT;
1073         if (copy_to_user(optval,&val,len))
1074                 return -EFAULT;
1075         return 0;
1076 }
1077
1078 static int rawv6_getsockopt(struct sock *sk, int level, int optname,
1079                           char __user *optval, int __user *optlen)
1080 {
1081         switch(level) {
1082                 case SOL_RAW:
1083                         break;
1084
1085                 case SOL_ICMPV6:
1086                         if (inet_sk(sk)->num != IPPROTO_ICMPV6)
1087                                 return -EOPNOTSUPP;
1088                         return rawv6_geticmpfilter(sk, level, optname, optval,
1089                                                    optlen);
1090                 case SOL_IPV6:
1091                         if (optname == IPV6_CHECKSUM)
1092                                 break;
1093                 default:
1094                         return ipv6_getsockopt(sk, level, optname, optval,
1095                                                optlen);
1096         }
1097
1098         return do_rawv6_getsockopt(sk, level, optname, optval, optlen);
1099 }
1100
1101 #ifdef CONFIG_COMPAT
1102 static int compat_rawv6_getsockopt(struct sock *sk, int level, int optname,
1103                                    char __user *optval, int __user *optlen)
1104 {
1105         switch (level) {
1106         case SOL_RAW:
1107                 break;
1108         case SOL_ICMPV6:
1109                 if (inet_sk(sk)->num != IPPROTO_ICMPV6)
1110                         return -EOPNOTSUPP;
1111                 return rawv6_geticmpfilter(sk, level, optname, optval, optlen);
1112         case SOL_IPV6:
1113                 if (optname == IPV6_CHECKSUM)
1114                         break;
1115         default:
1116                 return compat_ipv6_getsockopt(sk, level, optname,
1117                                               optval, optlen);
1118         }
1119         return do_rawv6_getsockopt(sk, level, optname, optval, optlen);
1120 }
1121 #endif
1122
1123 static int rawv6_ioctl(struct sock *sk, int cmd, unsigned long arg)
1124 {
1125         switch(cmd) {
1126                 case SIOCOUTQ:
1127                 {
1128                         int amount = atomic_read(&sk->sk_wmem_alloc);
1129                         return put_user(amount, (int __user *)arg);
1130                 }
1131                 case SIOCINQ:
1132                 {
1133                         struct sk_buff *skb;
1134                         int amount = 0;
1135
1136                         spin_lock_bh(&sk->sk_receive_queue.lock);
1137                         skb = skb_peek(&sk->sk_receive_queue);
1138                         if (skb != NULL)
1139                                 amount = skb->tail - skb->transport_header;
1140                         spin_unlock_bh(&sk->sk_receive_queue.lock);
1141                         return put_user(amount, (int __user *)arg);
1142                 }
1143
1144                 default:
1145                         return -ENOIOCTLCMD;
1146         }
1147 }
1148
1149 static void rawv6_close(struct sock *sk, long timeout)
1150 {
1151         if (inet_sk(sk)->num == IPPROTO_RAW)
1152                 ip6_ra_control(sk, -1, NULL);
1153
1154         sk_common_release(sk);
1155 }
1156
1157 static int rawv6_init_sk(struct sock *sk)
1158 {
1159         struct raw6_sock *rp = raw6_sk(sk);
1160
1161         switch (inet_sk(sk)->num) {
1162         case IPPROTO_ICMPV6:
1163                 rp->checksum = 1;
1164                 rp->offset   = 2;
1165                 break;
1166         case IPPROTO_MH:
1167                 rp->checksum = 1;
1168                 rp->offset   = 4;
1169                 break;
1170         default:
1171                 break;
1172         }
1173         return(0);
1174 }
1175
1176 DEFINE_PROTO_INUSE(rawv6)
1177
1178 struct proto rawv6_prot = {
1179         .name              = "RAWv6",
1180         .owner             = THIS_MODULE,
1181         .close             = rawv6_close,
1182         .connect           = ip6_datagram_connect,
1183         .disconnect        = udp_disconnect,
1184         .ioctl             = rawv6_ioctl,
1185         .init              = rawv6_init_sk,
1186         .destroy           = inet6_destroy_sock,
1187         .setsockopt        = rawv6_setsockopt,
1188         .getsockopt        = rawv6_getsockopt,
1189         .sendmsg           = rawv6_sendmsg,
1190         .recvmsg           = rawv6_recvmsg,
1191         .bind              = rawv6_bind,
1192         .backlog_rcv       = rawv6_rcv_skb,
1193         .hash              = raw_v6_hash,
1194         .unhash            = raw_v6_unhash,
1195         .obj_size          = sizeof(struct raw6_sock),
1196 #ifdef CONFIG_COMPAT
1197         .compat_setsockopt = compat_rawv6_setsockopt,
1198         .compat_getsockopt = compat_rawv6_getsockopt,
1199 #endif
1200         REF_PROTO_INUSE(rawv6)
1201 };
1202
1203 #ifdef CONFIG_PROC_FS
1204 static void raw6_sock_seq_show(struct seq_file *seq, struct sock *sp, int i)
1205 {
1206         struct ipv6_pinfo *np = inet6_sk(sp);
1207         struct in6_addr *dest, *src;
1208         __u16 destp, srcp;
1209
1210         dest  = &np->daddr;
1211         src   = &np->rcv_saddr;
1212         destp = 0;
1213         srcp  = inet_sk(sp)->num;
1214         seq_printf(seq,
1215                    "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1216                    "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d\n",
1217                    i,
1218                    src->s6_addr32[0], src->s6_addr32[1],
1219                    src->s6_addr32[2], src->s6_addr32[3], srcp,
1220                    dest->s6_addr32[0], dest->s6_addr32[1],
1221                    dest->s6_addr32[2], dest->s6_addr32[3], destp,
1222                    sp->sk_state,
1223                    atomic_read(&sp->sk_wmem_alloc),
1224                    atomic_read(&sp->sk_rmem_alloc),
1225                    0, 0L, 0,
1226                    sock_i_uid(sp), 0,
1227                    sock_i_ino(sp),
1228                    atomic_read(&sp->sk_refcnt), sp, atomic_read(&sp->sk_drops));
1229 }
1230
1231 static int raw6_seq_show(struct seq_file *seq, void *v)
1232 {
1233         if (v == SEQ_START_TOKEN)
1234                 seq_printf(seq,
1235                            "  sl  "
1236                            "local_address                         "
1237                            "remote_address                        "
1238                            "st tx_queue rx_queue tr tm->when retrnsmt"
1239                            "   uid  timeout inode  drops\n");
1240         else
1241                 raw6_sock_seq_show(seq, v, raw_seq_private(seq)->bucket);
1242         return 0;
1243 }
1244
1245 static const struct seq_operations raw6_seq_ops = {
1246         .start =        raw_seq_start,
1247         .next =         raw_seq_next,
1248         .stop =         raw_seq_stop,
1249         .show =         raw6_seq_show,
1250 };
1251
1252 static int raw6_seq_open(struct inode *inode, struct file *file)
1253 {
1254         return raw_seq_open(file, &raw_v6_hashinfo, PF_INET6);
1255 }
1256
1257 static const struct file_operations raw6_seq_fops = {
1258         .owner =        THIS_MODULE,
1259         .open =         raw6_seq_open,
1260         .read =         seq_read,
1261         .llseek =       seq_lseek,
1262         .release =      seq_release_private,
1263 };
1264
1265 int __init raw6_proc_init(void)
1266 {
1267         if (!proc_net_fops_create(&init_net, "raw6", S_IRUGO, &raw6_seq_fops))
1268                 return -ENOMEM;
1269         return 0;
1270 }
1271
1272 void raw6_proc_exit(void)
1273 {
1274         proc_net_remove(&init_net, "raw6");
1275 }
1276 #endif  /* CONFIG_PROC_FS */
1277
1278 /* Same as inet6_dgram_ops, sans udp_poll.  */
1279 static const struct proto_ops inet6_sockraw_ops = {
1280         .family            = PF_INET6,
1281         .owner             = THIS_MODULE,
1282         .release           = inet6_release,
1283         .bind              = inet6_bind,
1284         .connect           = inet_dgram_connect,        /* ok           */
1285         .socketpair        = sock_no_socketpair,        /* a do nothing */
1286         .accept            = sock_no_accept,            /* a do nothing */
1287         .getname           = inet6_getname,
1288         .poll              = datagram_poll,             /* ok           */
1289         .ioctl             = inet6_ioctl,               /* must change  */
1290         .listen            = sock_no_listen,            /* ok           */
1291         .shutdown          = inet_shutdown,             /* ok           */
1292         .setsockopt        = sock_common_setsockopt,    /* ok           */
1293         .getsockopt        = sock_common_getsockopt,    /* ok           */
1294         .sendmsg           = inet_sendmsg,              /* ok           */
1295         .recvmsg           = sock_common_recvmsg,       /* ok           */
1296         .mmap              = sock_no_mmap,
1297         .sendpage          = sock_no_sendpage,
1298 #ifdef CONFIG_COMPAT
1299         .compat_setsockopt = compat_sock_common_setsockopt,
1300         .compat_getsockopt = compat_sock_common_getsockopt,
1301 #endif
1302 };
1303
1304 static struct inet_protosw rawv6_protosw = {
1305         .type           = SOCK_RAW,
1306         .protocol       = IPPROTO_IP,   /* wild card */
1307         .prot           = &rawv6_prot,
1308         .ops            = &inet6_sockraw_ops,
1309         .capability     = CAP_NET_RAW,
1310         .no_check       = UDP_CSUM_DEFAULT,
1311         .flags          = INET_PROTOSW_REUSE,
1312 };
1313
1314 int __init rawv6_init(void)
1315 {
1316         int ret;
1317
1318         ret = inet6_register_protosw(&rawv6_protosw);
1319         if (ret)
1320                 goto out;
1321 out:
1322         return ret;
1323 }
1324
1325 void rawv6_exit(void)
1326 {
1327         inet6_unregister_protosw(&rawv6_protosw);
1328 }