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