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