[IPV6]: Support IPV6_{RECV,}TCLASS socket options / ancillary data.
[linux-3.10.git] / 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/errno.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.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         if (flowlabel) {
159                 if (flowlabel->opt && flowlabel->opt->srcrt) {
160                         struct rt0_hdr *rt0 = (struct rt0_hdr *) flowlabel->opt->srcrt;
161                         ipv6_addr_copy(&final, &fl.fl6_dst);
162                         ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
163                         final_p = &final;
164                 }
165         } else if (np->opt && np->opt->srcrt) {
166                 struct rt0_hdr *rt0 = (struct rt0_hdr *)np->opt->srcrt;
167                 ipv6_addr_copy(&final, &fl.fl6_dst);
168                 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
169                 final_p = &final;
170         }
171
172         err = ip6_dst_lookup(sk, &dst, &fl);
173         if (err)
174                 goto out;
175         if (final_p)
176                 ipv6_addr_copy(&fl.fl6_dst, final_p);
177
178         if ((err = xfrm_lookup(&dst, &fl, sk, 0)) < 0) {
179                 dst_release(dst);
180                 goto out;
181         }
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
197         sk->sk_state = TCP_ESTABLISHED;
198 out:
199         fl6_sock_release(flowlabel);
200         return err;
201 }
202
203 void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err, 
204                      u16 port, u32 info, u8 *payload)
205 {
206         struct ipv6_pinfo *np  = inet6_sk(sk);
207         struct icmp6hdr *icmph = (struct icmp6hdr *)skb->h.raw;
208         struct sock_exterr_skb *serr;
209
210         if (!np->recverr)
211                 return;
212
213         skb = skb_clone(skb, GFP_ATOMIC);
214         if (!skb)
215                 return;
216
217         serr = SKB_EXT_ERR(skb);
218         serr->ee.ee_errno = err;
219         serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6;
220         serr->ee.ee_type = icmph->icmp6_type; 
221         serr->ee.ee_code = icmph->icmp6_code;
222         serr->ee.ee_pad = 0;
223         serr->ee.ee_info = info;
224         serr->ee.ee_data = 0;
225         serr->addr_offset = (u8*)&(((struct ipv6hdr*)(icmph+1))->daddr) - skb->nh.raw;
226         serr->port = port;
227
228         skb->h.raw = payload;
229         __skb_pull(skb, payload - skb->data);
230
231         if (sock_queue_err_skb(sk, skb))
232                 kfree_skb(skb);
233 }
234
235 void ipv6_local_error(struct sock *sk, int err, struct flowi *fl, u32 info)
236 {
237         struct ipv6_pinfo *np = inet6_sk(sk);
238         struct sock_exterr_skb *serr;
239         struct ipv6hdr *iph;
240         struct sk_buff *skb;
241
242         if (!np->recverr)
243                 return;
244
245         skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
246         if (!skb)
247                 return;
248
249         iph = (struct ipv6hdr*)skb_put(skb, sizeof(struct ipv6hdr));
250         skb->nh.ipv6h = iph;
251         ipv6_addr_copy(&iph->daddr, &fl->fl6_dst);
252
253         serr = SKB_EXT_ERR(skb);
254         serr->ee.ee_errno = err;
255         serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
256         serr->ee.ee_type = 0; 
257         serr->ee.ee_code = 0;
258         serr->ee.ee_pad = 0;
259         serr->ee.ee_info = info;
260         serr->ee.ee_data = 0;
261         serr->addr_offset = (u8*)&iph->daddr - skb->nh.raw;
262         serr->port = fl->fl_ip_dport;
263
264         skb->h.raw = skb->tail;
265         __skb_pull(skb, skb->tail - skb->data);
266
267         if (sock_queue_err_skb(sk, skb))
268                 kfree_skb(skb);
269 }
270
271 /* 
272  *      Handle MSG_ERRQUEUE
273  */
274 int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len)
275 {
276         struct ipv6_pinfo *np = inet6_sk(sk);
277         struct sock_exterr_skb *serr;
278         struct sk_buff *skb, *skb2;
279         struct sockaddr_in6 *sin;
280         struct {
281                 struct sock_extended_err ee;
282                 struct sockaddr_in6      offender;
283         } errhdr;
284         int err;
285         int copied;
286
287         err = -EAGAIN;
288         skb = skb_dequeue(&sk->sk_error_queue);
289         if (skb == NULL)
290                 goto out;
291
292         copied = skb->len;
293         if (copied > len) {
294                 msg->msg_flags |= MSG_TRUNC;
295                 copied = len;
296         }
297         err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
298         if (err)
299                 goto out_free_skb;
300
301         sock_recv_timestamp(msg, sk, skb);
302
303         serr = SKB_EXT_ERR(skb);
304
305         sin = (struct sockaddr_in6 *)msg->msg_name;
306         if (sin) {
307                 sin->sin6_family = AF_INET6;
308                 sin->sin6_flowinfo = 0;
309                 sin->sin6_port = serr->port; 
310                 sin->sin6_scope_id = 0;
311                 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6) {
312                         ipv6_addr_copy(&sin->sin6_addr,
313                           (struct in6_addr *)(skb->nh.raw + serr->addr_offset));
314                         if (np->sndflow)
315                                 sin->sin6_flowinfo = *(u32*)(skb->nh.raw + serr->addr_offset - 24) & IPV6_FLOWINFO_MASK;
316                         if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
317                                 sin->sin6_scope_id = IP6CB(skb)->iif;
318                 } else {
319                         ipv6_addr_set(&sin->sin6_addr, 0, 0,
320                                       htonl(0xffff),
321                                       *(u32*)(skb->nh.raw + serr->addr_offset));
322                 }
323         }
324
325         memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
326         sin = &errhdr.offender;
327         sin->sin6_family = AF_UNSPEC;
328         if (serr->ee.ee_origin != SO_EE_ORIGIN_LOCAL) {
329                 sin->sin6_family = AF_INET6;
330                 sin->sin6_flowinfo = 0;
331                 sin->sin6_scope_id = 0;
332                 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6) {
333                         ipv6_addr_copy(&sin->sin6_addr, &skb->nh.ipv6h->saddr);
334                         if (np->rxopt.all)
335                                 datagram_recv_ctl(sk, msg, skb);
336                         if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
337                                 sin->sin6_scope_id = IP6CB(skb)->iif;
338                 } else {
339                         struct inet_sock *inet = inet_sk(sk);
340
341                         ipv6_addr_set(&sin->sin6_addr, 0, 0,
342                                       htonl(0xffff),
343                                       skb->nh.iph->saddr);
344                         if (inet->cmsg_flags)
345                                 ip_cmsg_recv(msg, skb);
346                 }
347         }
348
349         put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr);
350
351         /* Now we could try to dump offended packet options */
352
353         msg->msg_flags |= MSG_ERRQUEUE;
354         err = copied;
355
356         /* Reset and regenerate socket error */
357         spin_lock_bh(&sk->sk_error_queue.lock);
358         sk->sk_err = 0;
359         if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
360                 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
361                 spin_unlock_bh(&sk->sk_error_queue.lock);
362                 sk->sk_error_report(sk);
363         } else {
364                 spin_unlock_bh(&sk->sk_error_queue.lock);
365         }
366
367 out_free_skb:   
368         kfree_skb(skb);
369 out:
370         return err;
371 }
372
373
374
375 int datagram_recv_ctl(struct sock *sk, struct msghdr *msg, struct sk_buff *skb)
376 {
377         struct ipv6_pinfo *np = inet6_sk(sk);
378         struct inet6_skb_parm *opt = IP6CB(skb);
379
380         if (np->rxopt.bits.rxinfo) {
381                 struct in6_pktinfo src_info;
382
383                 src_info.ipi6_ifindex = opt->iif;
384                 ipv6_addr_copy(&src_info.ipi6_addr, &skb->nh.ipv6h->daddr);
385                 put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
386         }
387
388         if (np->rxopt.bits.rxhlim) {
389                 int hlim = skb->nh.ipv6h->hop_limit;
390                 put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
391         }
392
393         if (np->rxopt.bits.rxtclass) {
394                 int tclass = (ntohl(*(u32 *)skb->nh.ipv6h) >> 20) & 0xff;
395                 put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
396         }
397
398         if (np->rxopt.bits.rxflow && (*(u32*)skb->nh.raw & IPV6_FLOWINFO_MASK)) {
399                 u32 flowinfo = *(u32*)skb->nh.raw & IPV6_FLOWINFO_MASK;
400                 put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
401         }
402
403         /* HbH is allowed only once */
404         if (np->rxopt.bits.hopopts && opt->hop) {
405                 u8 *ptr = skb->nh.raw + opt->hop;
406                 put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
407         }
408
409         if (opt->lastopt &&
410             (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
411                 /*
412                  * Silly enough, but we need to reparse in order to
413                  * report extension headers (except for HbH)
414                  * in order.
415                  *
416                  * Also note that IPV6_RECVRTHDRDSTOPTS is NOT 
417                  * (and WILL NOT be) defined because
418                  * IPV6_RECVDSTOPTS is more generic. --yoshfuji
419                  */
420                 unsigned int off = sizeof(struct ipv6hdr);
421                 u8 nexthdr = skb->nh.ipv6h->nexthdr;
422
423                 while (off <= opt->lastopt) {
424                         unsigned len;
425                         u8 *ptr = skb->nh.raw + off;
426
427                         switch(nexthdr) {
428                         case IPPROTO_DSTOPTS:
429                                 nexthdr = ptr[0];
430                                 len = (ptr[1] + 1) << 3;
431                                 if (np->rxopt.bits.dstopts)
432                                         put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
433                                 break;
434                         case IPPROTO_ROUTING:
435                                 nexthdr = ptr[0];
436                                 len = (ptr[1] + 1) << 3;
437                                 if (np->rxopt.bits.srcrt)
438                                         put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
439                                 break;
440                         case IPPROTO_AH:
441                                 nexthdr = ptr[0];
442                                 len = (ptr[1] + 1) << 2;
443                                 break;
444                         default:
445                                 nexthdr = ptr[0];
446                                 len = (ptr[1] + 1) << 3;
447                                 break;
448                         }
449
450                         off += len;
451                 }
452         }
453
454         /* socket options in old style */
455         if (np->rxopt.bits.rxoinfo) {
456                 struct in6_pktinfo src_info;
457
458                 src_info.ipi6_ifindex = opt->iif;
459                 ipv6_addr_copy(&src_info.ipi6_addr, &skb->nh.ipv6h->daddr);
460                 put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
461         }
462         if (np->rxopt.bits.rxohlim) {
463                 int hlim = skb->nh.ipv6h->hop_limit;
464                 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
465         }
466         if (np->rxopt.bits.ohopopts && opt->hop) {
467                 u8 *ptr = skb->nh.raw + opt->hop;
468                 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr);
469         }
470         if (np->rxopt.bits.odstopts && opt->dst0) {
471                 u8 *ptr = skb->nh.raw + opt->dst0;
472                 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
473         }
474         if (np->rxopt.bits.osrcrt && opt->srcrt) {
475                 struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(skb->nh.raw + opt->srcrt);
476                 put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
477         }
478         if (np->rxopt.bits.odstopts && opt->dst1) {
479                 u8 *ptr = skb->nh.raw + opt->dst1;
480                 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
481         }
482         return 0;
483 }
484
485 int datagram_send_ctl(struct msghdr *msg, struct flowi *fl,
486                       struct ipv6_txoptions *opt,
487                       int *hlimit, int *tclass)
488 {
489         struct in6_pktinfo *src_info;
490         struct cmsghdr *cmsg;
491         struct ipv6_rt_hdr *rthdr;
492         struct ipv6_opt_hdr *hdr;
493         int len;
494         int err = 0;
495
496         for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
497                 int addr_type;
498                 struct net_device *dev = NULL;
499
500                 if (!CMSG_OK(msg, cmsg)) {
501                         err = -EINVAL;
502                         goto exit_f;
503                 }
504
505                 if (cmsg->cmsg_level != SOL_IPV6)
506                         continue;
507
508                 switch (cmsg->cmsg_type) {
509                 case IPV6_PKTINFO:
510                 case IPV6_2292PKTINFO:
511                         if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
512                                 err = -EINVAL;
513                                 goto exit_f;
514                         }
515
516                         src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
517                         
518                         if (src_info->ipi6_ifindex) {
519                                 if (fl->oif && src_info->ipi6_ifindex != fl->oif)
520                                         return -EINVAL;
521                                 fl->oif = src_info->ipi6_ifindex;
522                         }
523
524                         addr_type = ipv6_addr_type(&src_info->ipi6_addr);
525
526                         if (addr_type == IPV6_ADDR_ANY)
527                                 break;
528                         
529                         if (addr_type & IPV6_ADDR_LINKLOCAL) {
530                                 if (!src_info->ipi6_ifindex)
531                                         return -EINVAL;
532                                 else {
533                                         dev = dev_get_by_index(src_info->ipi6_ifindex);
534                                         if (!dev)
535                                                 return -ENODEV;
536                                 }
537                         }
538                         if (!ipv6_chk_addr(&src_info->ipi6_addr, dev, 0)) {
539                                 if (dev)
540                                         dev_put(dev);
541                                 err = -EINVAL;
542                                 goto exit_f;
543                         }
544                         if (dev)
545                                 dev_put(dev);
546
547                         ipv6_addr_copy(&fl->fl6_src, &src_info->ipi6_addr);
548                         break;
549
550                 case IPV6_FLOWINFO:
551                         if (cmsg->cmsg_len < CMSG_LEN(4)) {
552                                 err = -EINVAL;
553                                 goto exit_f;
554                         }
555
556                         if (fl->fl6_flowlabel&IPV6_FLOWINFO_MASK) {
557                                 if ((fl->fl6_flowlabel^*(u32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
558                                         err = -EINVAL;
559                                         goto exit_f;
560                                 }
561                         }
562                         fl->fl6_flowlabel = IPV6_FLOWINFO_MASK & *(u32 *)CMSG_DATA(cmsg);
563                         break;
564
565                 case IPV6_2292HOPOPTS:
566                 case IPV6_HOPOPTS:
567                         if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
568                                 err = -EINVAL;
569                                 goto exit_f;
570                         }
571
572                         hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
573                         len = ((hdr->hdrlen + 1) << 3);
574                         if (cmsg->cmsg_len < CMSG_LEN(len)) {
575                                 err = -EINVAL;
576                                 goto exit_f;
577                         }
578                         if (!capable(CAP_NET_RAW)) {
579                                 err = -EPERM;
580                                 goto exit_f;
581                         }
582                         opt->opt_nflen += len;
583                         opt->hopopt = hdr;
584                         break;
585
586                 case IPV6_2292DSTOPTS:
587                         if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
588                                 err = -EINVAL;
589                                 goto exit_f;
590                         }
591
592                         hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
593                         len = ((hdr->hdrlen + 1) << 3);
594                         if (cmsg->cmsg_len < CMSG_LEN(len)) {
595                                 err = -EINVAL;
596                                 goto exit_f;
597                         }
598                         if (!capable(CAP_NET_RAW)) {
599                                 err = -EPERM;
600                                 goto exit_f;
601                         }
602                         if (opt->dst1opt) {
603                                 err = -EINVAL;
604                                 goto exit_f;
605                         }
606                         opt->opt_flen += len;
607                         opt->dst1opt = hdr;
608                         break;
609
610                 case IPV6_DSTOPTS:
611                 case IPV6_RTHDRDSTOPTS:
612                         if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
613                                 err = -EINVAL;
614                                 goto exit_f;
615                         }
616
617                         hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
618                         len = ((hdr->hdrlen + 1) << 3);
619                         if (cmsg->cmsg_len < CMSG_LEN(len)) {
620                                 err = -EINVAL;
621                                 goto exit_f;
622                         }
623                         if (!capable(CAP_NET_RAW)) {
624                                 err = -EPERM;
625                                 goto exit_f;
626                         }
627                         if (cmsg->cmsg_type == IPV6_DSTOPTS) {
628                                 opt->opt_flen += len;
629                                 opt->dst1opt = hdr;
630                         } else {
631                                 opt->opt_nflen += len;
632                                 opt->dst0opt = hdr;
633                         }
634                         break;
635
636                 case IPV6_2292RTHDR:
637                 case IPV6_RTHDR:
638                         if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
639                                 err = -EINVAL;
640                                 goto exit_f;
641                         }
642
643                         rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
644
645                         /*
646                          *      TYPE 0
647                          */
648                         if (rthdr->type) {
649                                 err = -EINVAL;
650                                 goto exit_f;
651                         }
652
653                         len = ((rthdr->hdrlen + 1) << 3);
654
655                         if (cmsg->cmsg_len < CMSG_LEN(len)) {
656                                 err = -EINVAL;
657                                 goto exit_f;
658                         }
659
660                         /* segments left must also match */
661                         if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
662                                 err = -EINVAL;
663                                 goto exit_f;
664                         }
665
666                         opt->opt_nflen += len;
667                         opt->srcrt = rthdr;
668
669                         if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
670                                 int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
671
672                                 opt->opt_nflen += dsthdrlen;
673                                 opt->dst0opt = opt->dst1opt;
674                                 opt->dst1opt = NULL;
675                                 opt->opt_flen -= dsthdrlen;
676                         }
677
678                         break;
679
680                 case IPV6_2292HOPLIMIT:
681                 case IPV6_HOPLIMIT:
682                         if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
683                                 err = -EINVAL;
684                                 goto exit_f;
685                         }
686
687                         *hlimit = *(int *)CMSG_DATA(cmsg);
688                         break;
689
690                 case IPV6_TCLASS:
691                     {
692                         int tc;
693
694                         err = -EINVAL;
695                         if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
696                                 goto exit_f;
697                         }
698
699                         tc = *(int *)CMSG_DATA(cmsg);
700                         if (tc < 0 || tc > 0xff)
701                                 goto exit_f;
702
703                         err = 0;
704                         *tclass = tc;
705
706                         break;
707                     }
708                 default:
709                         LIMIT_NETDEBUG(KERN_DEBUG "invalid cmsg type: %d\n",
710                                        cmsg->cmsg_type);
711                         err = -EINVAL;
712                         break;
713                 };
714         }
715
716 exit_f:
717         return err;
718 }