[IPV6] SIT: Unify code path to get hash array index.
[linux-2.6] / net / ipv6 / datagram.c
1 /*
2  *      common UDP/RAW code
3  *      Linux INET6 implementation
4  *
5  *      Authors:
6  *      Pedro Roque             <roque@di.fc.ul.pt>
7  *
8  *      $Id: datagram.c,v 1.24 2002/02/01 22:01:04 davem Exp $
9  *
10  *      This program is free software; you can redistribute it and/or
11  *      modify it under the terms of the GNU General Public License
12  *      as published by the Free Software Foundation; either version
13  *      2 of the License, or (at your option) any later version.
14  */
15
16 #include <linux/capability.h>
17 #include <linux/errno.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/interrupt.h>
21 #include <linux/socket.h>
22 #include <linux/sockios.h>
23 #include <linux/in6.h>
24 #include <linux/ipv6.h>
25 #include <linux/route.h>
26
27 #include <net/ipv6.h>
28 #include <net/ndisc.h>
29 #include <net/addrconf.h>
30 #include <net/transp_v6.h>
31 #include <net/ip6_route.h>
32 #include <net/tcp_states.h>
33
34 #include <linux/errqueue.h>
35 #include <asm/uaccess.h>
36
37 int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
38 {
39         struct sockaddr_in6     *usin = (struct sockaddr_in6 *) uaddr;
40         struct inet_sock        *inet = inet_sk(sk);
41         struct ipv6_pinfo       *np = inet6_sk(sk);
42         struct in6_addr         *daddr, *final_p = NULL, final;
43         struct dst_entry        *dst;
44         struct flowi            fl;
45         struct ip6_flowlabel    *flowlabel = NULL;
46         int                     addr_type;
47         int                     err;
48
49         if (usin->sin6_family == AF_INET) {
50                 if (__ipv6_only_sock(sk))
51                         return -EAFNOSUPPORT;
52                 err = ip4_datagram_connect(sk, uaddr, addr_len);
53                 goto ipv4_connected;
54         }
55
56         if (addr_len < SIN6_LEN_RFC2133)
57                 return -EINVAL;
58
59         if (usin->sin6_family != AF_INET6)
60                 return -EAFNOSUPPORT;
61
62         memset(&fl, 0, sizeof(fl));
63         if (np->sndflow) {
64                 fl.fl6_flowlabel = usin->sin6_flowinfo&IPV6_FLOWINFO_MASK;
65                 if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
66                         flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
67                         if (flowlabel == NULL)
68                                 return -EINVAL;
69                         ipv6_addr_copy(&usin->sin6_addr, &flowlabel->dst);
70                 }
71         }
72
73         addr_type = ipv6_addr_type(&usin->sin6_addr);
74
75         if (addr_type == IPV6_ADDR_ANY) {
76                 /*
77                  *      connect to self
78                  */
79                 usin->sin6_addr.s6_addr[15] = 0x01;
80         }
81
82         daddr = &usin->sin6_addr;
83
84         if (addr_type == IPV6_ADDR_MAPPED) {
85                 struct sockaddr_in sin;
86
87                 if (__ipv6_only_sock(sk)) {
88                         err = -ENETUNREACH;
89                         goto out;
90                 }
91                 sin.sin_family = AF_INET;
92                 sin.sin_addr.s_addr = daddr->s6_addr32[3];
93                 sin.sin_port = usin->sin6_port;
94
95                 err = ip4_datagram_connect(sk,
96                                            (struct sockaddr*) &sin,
97                                            sizeof(sin));
98
99 ipv4_connected:
100                 if (err)
101                         goto out;
102
103                 ipv6_addr_set(&np->daddr, 0, 0, htonl(0x0000ffff), inet->daddr);
104
105                 if (ipv6_addr_any(&np->saddr)) {
106                         ipv6_addr_set(&np->saddr, 0, 0, htonl(0x0000ffff),
107                                       inet->saddr);
108                 }
109
110                 if (ipv6_addr_any(&np->rcv_saddr)) {
111                         ipv6_addr_set(&np->rcv_saddr, 0, 0, htonl(0x0000ffff),
112                                       inet->rcv_saddr);
113                 }
114                 goto out;
115         }
116
117         if (addr_type&IPV6_ADDR_LINKLOCAL) {
118                 if (addr_len >= sizeof(struct sockaddr_in6) &&
119                     usin->sin6_scope_id) {
120                         if (sk->sk_bound_dev_if &&
121                             sk->sk_bound_dev_if != usin->sin6_scope_id) {
122                                 err = -EINVAL;
123                                 goto out;
124                         }
125                         sk->sk_bound_dev_if = usin->sin6_scope_id;
126                         if (!sk->sk_bound_dev_if &&
127                             (addr_type & IPV6_ADDR_MULTICAST))
128                                 fl.oif = np->mcast_oif;
129                 }
130
131                 /* Connect to link-local address requires an interface */
132                 if (!sk->sk_bound_dev_if) {
133                         err = -EINVAL;
134                         goto out;
135                 }
136         }
137
138         ipv6_addr_copy(&np->daddr, daddr);
139         np->flow_label = fl.fl6_flowlabel;
140
141         inet->dport = usin->sin6_port;
142
143         /*
144          *      Check for a route to destination an obtain the
145          *      destination cache for it.
146          */
147
148         fl.proto = sk->sk_protocol;
149         ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
150         ipv6_addr_copy(&fl.fl6_src, &np->saddr);
151         fl.oif = sk->sk_bound_dev_if;
152         fl.fl_ip_dport = inet->dport;
153         fl.fl_ip_sport = inet->sport;
154
155         if (!fl.oif && (addr_type&IPV6_ADDR_MULTICAST))
156                 fl.oif = np->mcast_oif;
157
158         security_sk_classify_flow(sk, &fl);
159
160         if (flowlabel) {
161                 if (flowlabel->opt && flowlabel->opt->srcrt) {
162                         struct rt0_hdr *rt0 = (struct rt0_hdr *) flowlabel->opt->srcrt;
163                         ipv6_addr_copy(&final, &fl.fl6_dst);
164                         ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
165                         final_p = &final;
166                 }
167         } else if (np->opt && np->opt->srcrt) {
168                 struct rt0_hdr *rt0 = (struct rt0_hdr *)np->opt->srcrt;
169                 ipv6_addr_copy(&final, &fl.fl6_dst);
170                 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
171                 final_p = &final;
172         }
173
174         err = ip6_dst_lookup(sk, &dst, &fl);
175         if (err)
176                 goto out;
177         if (final_p)
178                 ipv6_addr_copy(&fl.fl6_dst, final_p);
179
180         if ((err = xfrm_lookup(&dst, &fl, sk, 1)) < 0)
181                 goto out;
182
183         /* source address lookup done in ip6_dst_lookup */
184
185         if (ipv6_addr_any(&np->saddr))
186                 ipv6_addr_copy(&np->saddr, &fl.fl6_src);
187
188         if (ipv6_addr_any(&np->rcv_saddr)) {
189                 ipv6_addr_copy(&np->rcv_saddr, &fl.fl6_src);
190                 inet->rcv_saddr = LOOPBACK4_IPV6;
191         }
192
193         ip6_dst_store(sk, dst,
194                       ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ?
195                       &np->daddr : NULL,
196 #ifdef CONFIG_IPV6_SUBTREES
197                       ipv6_addr_equal(&fl.fl6_src, &np->saddr) ?
198                       &np->saddr :
199 #endif
200                       NULL);
201
202         sk->sk_state = TCP_ESTABLISHED;
203 out:
204         fl6_sock_release(flowlabel);
205         return err;
206 }
207
208 void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
209                      __be16 port, u32 info, u8 *payload)
210 {
211         struct ipv6_pinfo *np  = inet6_sk(sk);
212         struct icmp6hdr *icmph = icmp6_hdr(skb);
213         struct sock_exterr_skb *serr;
214
215         if (!np->recverr)
216                 return;
217
218         skb = skb_clone(skb, GFP_ATOMIC);
219         if (!skb)
220                 return;
221
222         serr = SKB_EXT_ERR(skb);
223         serr->ee.ee_errno = err;
224         serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6;
225         serr->ee.ee_type = icmph->icmp6_type;
226         serr->ee.ee_code = icmph->icmp6_code;
227         serr->ee.ee_pad = 0;
228         serr->ee.ee_info = info;
229         serr->ee.ee_data = 0;
230         serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
231                                   skb_network_header(skb);
232         serr->port = port;
233
234         __skb_pull(skb, payload - skb->data);
235         skb_reset_transport_header(skb);
236
237         if (sock_queue_err_skb(sk, skb))
238                 kfree_skb(skb);
239 }
240
241 void ipv6_local_error(struct sock *sk, int err, struct flowi *fl, u32 info)
242 {
243         struct ipv6_pinfo *np = inet6_sk(sk);
244         struct sock_exterr_skb *serr;
245         struct ipv6hdr *iph;
246         struct sk_buff *skb;
247
248         if (!np->recverr)
249                 return;
250
251         skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
252         if (!skb)
253                 return;
254
255         skb_put(skb, sizeof(struct ipv6hdr));
256         skb_reset_network_header(skb);
257         iph = ipv6_hdr(skb);
258         ipv6_addr_copy(&iph->daddr, &fl->fl6_dst);
259
260         serr = SKB_EXT_ERR(skb);
261         serr->ee.ee_errno = err;
262         serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
263         serr->ee.ee_type = 0;
264         serr->ee.ee_code = 0;
265         serr->ee.ee_pad = 0;
266         serr->ee.ee_info = info;
267         serr->ee.ee_data = 0;
268         serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
269         serr->port = fl->fl_ip_dport;
270
271         __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
272         skb_reset_transport_header(skb);
273
274         if (sock_queue_err_skb(sk, skb))
275                 kfree_skb(skb);
276 }
277
278 /*
279  *      Handle MSG_ERRQUEUE
280  */
281 int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len)
282 {
283         struct ipv6_pinfo *np = inet6_sk(sk);
284         struct sock_exterr_skb *serr;
285         struct sk_buff *skb, *skb2;
286         struct sockaddr_in6 *sin;
287         struct {
288                 struct sock_extended_err ee;
289                 struct sockaddr_in6      offender;
290         } errhdr;
291         int err;
292         int copied;
293
294         err = -EAGAIN;
295         skb = skb_dequeue(&sk->sk_error_queue);
296         if (skb == NULL)
297                 goto out;
298
299         copied = skb->len;
300         if (copied > len) {
301                 msg->msg_flags |= MSG_TRUNC;
302                 copied = len;
303         }
304         err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
305         if (err)
306                 goto out_free_skb;
307
308         sock_recv_timestamp(msg, sk, skb);
309
310         serr = SKB_EXT_ERR(skb);
311
312         sin = (struct sockaddr_in6 *)msg->msg_name;
313         if (sin) {
314                 const unsigned char *nh = skb_network_header(skb);
315                 sin->sin6_family = AF_INET6;
316                 sin->sin6_flowinfo = 0;
317                 sin->sin6_port = serr->port;
318                 sin->sin6_scope_id = 0;
319                 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6) {
320                         ipv6_addr_copy(&sin->sin6_addr,
321                                   (struct in6_addr *)(nh + serr->addr_offset));
322                         if (np->sndflow)
323                                 sin->sin6_flowinfo =
324                                         (*(__be32 *)(nh + serr->addr_offset - 24) &
325                                          IPV6_FLOWINFO_MASK);
326                         if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
327                                 sin->sin6_scope_id = IP6CB(skb)->iif;
328                 } else {
329                         ipv6_addr_set(&sin->sin6_addr, 0, 0,
330                                       htonl(0xffff),
331                                       *(__be32 *)(nh + serr->addr_offset));
332                 }
333         }
334
335         memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
336         sin = &errhdr.offender;
337         sin->sin6_family = AF_UNSPEC;
338         if (serr->ee.ee_origin != SO_EE_ORIGIN_LOCAL) {
339                 sin->sin6_family = AF_INET6;
340                 sin->sin6_flowinfo = 0;
341                 sin->sin6_scope_id = 0;
342                 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6) {
343                         ipv6_addr_copy(&sin->sin6_addr, &ipv6_hdr(skb)->saddr);
344                         if (np->rxopt.all)
345                                 datagram_recv_ctl(sk, msg, skb);
346                         if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
347                                 sin->sin6_scope_id = IP6CB(skb)->iif;
348                 } else {
349                         struct inet_sock *inet = inet_sk(sk);
350
351                         ipv6_addr_set(&sin->sin6_addr, 0, 0,
352                                       htonl(0xffff), ip_hdr(skb)->saddr);
353                         if (inet->cmsg_flags)
354                                 ip_cmsg_recv(msg, skb);
355                 }
356         }
357
358         put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr);
359
360         /* Now we could try to dump offended packet options */
361
362         msg->msg_flags |= MSG_ERRQUEUE;
363         err = copied;
364
365         /* Reset and regenerate socket error */
366         spin_lock_bh(&sk->sk_error_queue.lock);
367         sk->sk_err = 0;
368         if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
369                 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
370                 spin_unlock_bh(&sk->sk_error_queue.lock);
371                 sk->sk_error_report(sk);
372         } else {
373                 spin_unlock_bh(&sk->sk_error_queue.lock);
374         }
375
376 out_free_skb:
377         kfree_skb(skb);
378 out:
379         return err;
380 }
381
382
383
384 int datagram_recv_ctl(struct sock *sk, struct msghdr *msg, struct sk_buff *skb)
385 {
386         struct ipv6_pinfo *np = inet6_sk(sk);
387         struct inet6_skb_parm *opt = IP6CB(skb);
388         unsigned char *nh = skb_network_header(skb);
389
390         if (np->rxopt.bits.rxinfo) {
391                 struct in6_pktinfo src_info;
392
393                 src_info.ipi6_ifindex = opt->iif;
394                 ipv6_addr_copy(&src_info.ipi6_addr, &ipv6_hdr(skb)->daddr);
395                 put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
396         }
397
398         if (np->rxopt.bits.rxhlim) {
399                 int hlim = ipv6_hdr(skb)->hop_limit;
400                 put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
401         }
402
403         if (np->rxopt.bits.rxtclass) {
404                 int tclass = (ntohl(*(__be32 *)ipv6_hdr(skb)) >> 20) & 0xff;
405                 put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
406         }
407
408         if (np->rxopt.bits.rxflow && (*(__be32 *)nh & IPV6_FLOWINFO_MASK)) {
409                 __be32 flowinfo = *(__be32 *)nh & IPV6_FLOWINFO_MASK;
410                 put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
411         }
412
413         /* HbH is allowed only once */
414         if (np->rxopt.bits.hopopts && opt->hop) {
415                 u8 *ptr = nh + opt->hop;
416                 put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
417         }
418
419         if (opt->lastopt &&
420             (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
421                 /*
422                  * Silly enough, but we need to reparse in order to
423                  * report extension headers (except for HbH)
424                  * in order.
425                  *
426                  * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
427                  * (and WILL NOT be) defined because
428                  * IPV6_RECVDSTOPTS is more generic. --yoshfuji
429                  */
430                 unsigned int off = sizeof(struct ipv6hdr);
431                 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
432
433                 while (off <= opt->lastopt) {
434                         unsigned len;
435                         u8 *ptr = nh + off;
436
437                         switch(nexthdr) {
438                         case IPPROTO_DSTOPTS:
439                                 nexthdr = ptr[0];
440                                 len = (ptr[1] + 1) << 3;
441                                 if (np->rxopt.bits.dstopts)
442                                         put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
443                                 break;
444                         case IPPROTO_ROUTING:
445                                 nexthdr = ptr[0];
446                                 len = (ptr[1] + 1) << 3;
447                                 if (np->rxopt.bits.srcrt)
448                                         put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
449                                 break;
450                         case IPPROTO_AH:
451                                 nexthdr = ptr[0];
452                                 len = (ptr[1] + 2) << 2;
453                                 break;
454                         default:
455                                 nexthdr = ptr[0];
456                                 len = (ptr[1] + 1) << 3;
457                                 break;
458                         }
459
460                         off += len;
461                 }
462         }
463
464         /* socket options in old style */
465         if (np->rxopt.bits.rxoinfo) {
466                 struct in6_pktinfo src_info;
467
468                 src_info.ipi6_ifindex = opt->iif;
469                 ipv6_addr_copy(&src_info.ipi6_addr, &ipv6_hdr(skb)->daddr);
470                 put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
471         }
472         if (np->rxopt.bits.rxohlim) {
473                 int hlim = ipv6_hdr(skb)->hop_limit;
474                 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
475         }
476         if (np->rxopt.bits.ohopopts && opt->hop) {
477                 u8 *ptr = nh + opt->hop;
478                 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr);
479         }
480         if (np->rxopt.bits.odstopts && opt->dst0) {
481                 u8 *ptr = nh + opt->dst0;
482                 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
483         }
484         if (np->rxopt.bits.osrcrt && opt->srcrt) {
485                 struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
486                 put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
487         }
488         if (np->rxopt.bits.odstopts && opt->dst1) {
489                 u8 *ptr = nh + opt->dst1;
490                 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
491         }
492         return 0;
493 }
494
495 int datagram_send_ctl(struct msghdr *msg, struct flowi *fl,
496                       struct ipv6_txoptions *opt,
497                       int *hlimit, int *tclass)
498 {
499         struct in6_pktinfo *src_info;
500         struct cmsghdr *cmsg;
501         struct ipv6_rt_hdr *rthdr;
502         struct ipv6_opt_hdr *hdr;
503         int len;
504         int err = 0;
505
506         for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
507                 int addr_type;
508                 struct net_device *dev = NULL;
509
510                 if (!CMSG_OK(msg, cmsg)) {
511                         err = -EINVAL;
512                         goto exit_f;
513                 }
514
515                 if (cmsg->cmsg_level != SOL_IPV6)
516                         continue;
517
518                 switch (cmsg->cmsg_type) {
519                 case IPV6_PKTINFO:
520                 case IPV6_2292PKTINFO:
521                         if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
522                                 err = -EINVAL;
523                                 goto exit_f;
524                         }
525
526                         src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
527
528                         if (src_info->ipi6_ifindex) {
529                                 if (fl->oif && src_info->ipi6_ifindex != fl->oif)
530                                         return -EINVAL;
531                                 fl->oif = src_info->ipi6_ifindex;
532                         }
533
534                         addr_type = ipv6_addr_type(&src_info->ipi6_addr);
535
536                         if (addr_type == IPV6_ADDR_ANY)
537                                 break;
538
539                         if (addr_type & IPV6_ADDR_LINKLOCAL) {
540                                 if (!src_info->ipi6_ifindex)
541                                         return -EINVAL;
542                                 else {
543                                         dev = dev_get_by_index(src_info->ipi6_ifindex);
544                                         if (!dev)
545                                                 return -ENODEV;
546                                 }
547                         }
548                         if (!ipv6_chk_addr(&src_info->ipi6_addr, dev, 0)) {
549                                 if (dev)
550                                         dev_put(dev);
551                                 err = -EINVAL;
552                                 goto exit_f;
553                         }
554                         if (dev)
555                                 dev_put(dev);
556
557                         ipv6_addr_copy(&fl->fl6_src, &src_info->ipi6_addr);
558                         break;
559
560                 case IPV6_FLOWINFO:
561                         if (cmsg->cmsg_len < CMSG_LEN(4)) {
562                                 err = -EINVAL;
563                                 goto exit_f;
564                         }
565
566                         if (fl->fl6_flowlabel&IPV6_FLOWINFO_MASK) {
567                                 if ((fl->fl6_flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
568                                         err = -EINVAL;
569                                         goto exit_f;
570                                 }
571                         }
572                         fl->fl6_flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
573                         break;
574
575                 case IPV6_2292HOPOPTS:
576                 case IPV6_HOPOPTS:
577                         if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
578                                 err = -EINVAL;
579                                 goto exit_f;
580                         }
581
582                         hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
583                         len = ((hdr->hdrlen + 1) << 3);
584                         if (cmsg->cmsg_len < CMSG_LEN(len)) {
585                                 err = -EINVAL;
586                                 goto exit_f;
587                         }
588                         if (!capable(CAP_NET_RAW)) {
589                                 err = -EPERM;
590                                 goto exit_f;
591                         }
592                         opt->opt_nflen += len;
593                         opt->hopopt = hdr;
594                         break;
595
596                 case IPV6_2292DSTOPTS:
597                         if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
598                                 err = -EINVAL;
599                                 goto exit_f;
600                         }
601
602                         hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
603                         len = ((hdr->hdrlen + 1) << 3);
604                         if (cmsg->cmsg_len < CMSG_LEN(len)) {
605                                 err = -EINVAL;
606                                 goto exit_f;
607                         }
608                         if (!capable(CAP_NET_RAW)) {
609                                 err = -EPERM;
610                                 goto exit_f;
611                         }
612                         if (opt->dst1opt) {
613                                 err = -EINVAL;
614                                 goto exit_f;
615                         }
616                         opt->opt_flen += len;
617                         opt->dst1opt = hdr;
618                         break;
619
620                 case IPV6_DSTOPTS:
621                 case IPV6_RTHDRDSTOPTS:
622                         if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
623                                 err = -EINVAL;
624                                 goto exit_f;
625                         }
626
627                         hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
628                         len = ((hdr->hdrlen + 1) << 3);
629                         if (cmsg->cmsg_len < CMSG_LEN(len)) {
630                                 err = -EINVAL;
631                                 goto exit_f;
632                         }
633                         if (!capable(CAP_NET_RAW)) {
634                                 err = -EPERM;
635                                 goto exit_f;
636                         }
637                         if (cmsg->cmsg_type == IPV6_DSTOPTS) {
638                                 opt->opt_flen += len;
639                                 opt->dst1opt = hdr;
640                         } else {
641                                 opt->opt_nflen += len;
642                                 opt->dst0opt = hdr;
643                         }
644                         break;
645
646                 case IPV6_2292RTHDR:
647                 case IPV6_RTHDR:
648                         if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
649                                 err = -EINVAL;
650                                 goto exit_f;
651                         }
652
653                         rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
654
655                         switch (rthdr->type) {
656                         case IPV6_SRCRT_TYPE_0:
657 #ifdef CONFIG_IPV6_MIP6
658                         case IPV6_SRCRT_TYPE_2:
659 #endif
660                                 break;
661                         default:
662                                 err = -EINVAL;
663                                 goto exit_f;
664                         }
665
666                         len = ((rthdr->hdrlen + 1) << 3);
667
668                         if (cmsg->cmsg_len < CMSG_LEN(len)) {
669                                 err = -EINVAL;
670                                 goto exit_f;
671                         }
672
673                         /* segments left must also match */
674                         if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
675                                 err = -EINVAL;
676                                 goto exit_f;
677                         }
678
679                         opt->opt_nflen += len;
680                         opt->srcrt = rthdr;
681
682                         if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
683                                 int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
684
685                                 opt->opt_nflen += dsthdrlen;
686                                 opt->dst0opt = opt->dst1opt;
687                                 opt->dst1opt = NULL;
688                                 opt->opt_flen -= dsthdrlen;
689                         }
690
691                         break;
692
693                 case IPV6_2292HOPLIMIT:
694                 case IPV6_HOPLIMIT:
695                         if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
696                                 err = -EINVAL;
697                                 goto exit_f;
698                         }
699
700                         *hlimit = *(int *)CMSG_DATA(cmsg);
701                         break;
702
703                 case IPV6_TCLASS:
704                     {
705                         int tc;
706
707                         err = -EINVAL;
708                         if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
709                                 goto exit_f;
710                         }
711
712                         tc = *(int *)CMSG_DATA(cmsg);
713                         if (tc < -1 || tc > 0xff)
714                                 goto exit_f;
715
716                         err = 0;
717                         *tclass = tc;
718
719                         break;
720                     }
721                 default:
722                         LIMIT_NETDEBUG(KERN_DEBUG "invalid cmsg type: %d\n",
723                                        cmsg->cmsg_type);
724                         err = -EINVAL;
725                         break;
726                 }
727         }
728
729 exit_f:
730         return err;
731 }