Merge branch 'perf-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.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  *      This program is free software; you can redistribute it and/or
9  *      modify it under the terms of the GNU General Public License
10  *      as published by the Free Software Foundation; either version
11  *      2 of the License, or (at your option) any later version.
12  */
13
14 #include <linux/capability.h>
15 #include <linux/errno.h>
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/interrupt.h>
19 #include <linux/socket.h>
20 #include <linux/sockios.h>
21 #include <linux/in6.h>
22 #include <linux/ipv6.h>
23 #include <linux/route.h>
24 #include <linux/slab.h>
25
26 #include <net/ipv6.h>
27 #include <net/ndisc.h>
28 #include <net/addrconf.h>
29 #include <net/transp_v6.h>
30 #include <net/ip6_route.h>
31 #include <net/tcp_states.h>
32
33 #include <linux/errqueue.h>
34 #include <asm/uaccess.h>
35
36 int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
37 {
38         struct sockaddr_in6     *usin = (struct sockaddr_in6 *) uaddr;
39         struct inet_sock        *inet = inet_sk(sk);
40         struct ipv6_pinfo       *np = inet6_sk(sk);
41         struct in6_addr         *daddr, *final_p, final;
42         struct dst_entry        *dst;
43         struct flowi            fl;
44         struct ip6_flowlabel    *flowlabel = NULL;
45         struct ipv6_txoptions   *opt;
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_v4mapped(inet->inet_daddr, &np->daddr);
104
105                 if (ipv6_addr_any(&np->saddr))
106                         ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
107
108                 if (ipv6_addr_any(&np->rcv_saddr))
109                         ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
110                                                &np->rcv_saddr);
111
112                 goto out;
113         }
114
115         if (addr_type&IPV6_ADDR_LINKLOCAL) {
116                 if (addr_len >= sizeof(struct sockaddr_in6) &&
117                     usin->sin6_scope_id) {
118                         if (sk->sk_bound_dev_if &&
119                             sk->sk_bound_dev_if != usin->sin6_scope_id) {
120                                 err = -EINVAL;
121                                 goto out;
122                         }
123                         sk->sk_bound_dev_if = usin->sin6_scope_id;
124                 }
125
126                 if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST))
127                         sk->sk_bound_dev_if = np->mcast_oif;
128
129                 /* Connect to link-local address requires an interface */
130                 if (!sk->sk_bound_dev_if) {
131                         err = -EINVAL;
132                         goto out;
133                 }
134         }
135
136         ipv6_addr_copy(&np->daddr, daddr);
137         np->flow_label = fl.fl6_flowlabel;
138
139         inet->inet_dport = usin->sin6_port;
140
141         /*
142          *      Check for a route to destination an obtain the
143          *      destination cache for it.
144          */
145
146         fl.proto = sk->sk_protocol;
147         ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
148         ipv6_addr_copy(&fl.fl6_src, &np->saddr);
149         fl.oif = sk->sk_bound_dev_if;
150         fl.mark = sk->sk_mark;
151         fl.fl_ip_dport = inet->inet_dport;
152         fl.fl_ip_sport = inet->inet_sport;
153
154         if (!fl.oif && (addr_type&IPV6_ADDR_MULTICAST))
155                 fl.oif = np->mcast_oif;
156
157         security_sk_classify_flow(sk, &fl);
158
159         opt = flowlabel ? flowlabel->opt : np->opt;
160         final_p = fl6_update_dst(&fl, opt, &final);
161
162         err = ip6_dst_lookup(sk, &dst, &fl);
163         if (err)
164                 goto out;
165         if (final_p)
166                 ipv6_addr_copy(&fl.fl6_dst, final_p);
167
168         err = __xfrm_lookup(sock_net(sk), &dst, &fl, sk, XFRM_LOOKUP_WAIT);
169         if (err < 0) {
170                 if (err == -EREMOTE)
171                         err = ip6_dst_blackhole(sk, &dst, &fl);
172                 if (err < 0)
173                         goto out;
174         }
175
176         /* source address lookup done in ip6_dst_lookup */
177
178         if (ipv6_addr_any(&np->saddr))
179                 ipv6_addr_copy(&np->saddr, &fl.fl6_src);
180
181         if (ipv6_addr_any(&np->rcv_saddr)) {
182                 ipv6_addr_copy(&np->rcv_saddr, &fl.fl6_src);
183                 inet->inet_rcv_saddr = LOOPBACK4_IPV6;
184         }
185
186         ip6_dst_store(sk, dst,
187                       ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ?
188                       &np->daddr : NULL,
189 #ifdef CONFIG_IPV6_SUBTREES
190                       ipv6_addr_equal(&fl.fl6_src, &np->saddr) ?
191                       &np->saddr :
192 #endif
193                       NULL);
194
195         sk->sk_state = TCP_ESTABLISHED;
196 out:
197         fl6_sock_release(flowlabel);
198         return err;
199 }
200
201 void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
202                      __be16 port, u32 info, u8 *payload)
203 {
204         struct ipv6_pinfo *np  = inet6_sk(sk);
205         struct icmp6hdr *icmph = icmp6_hdr(skb);
206         struct sock_exterr_skb *serr;
207
208         if (!np->recverr)
209                 return;
210
211         skb = skb_clone(skb, GFP_ATOMIC);
212         if (!skb)
213                 return;
214
215         skb->protocol = htons(ETH_P_IPV6);
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) -
226                                   skb_network_header(skb);
227         serr->port = port;
228
229         __skb_pull(skb, payload - skb->data);
230         skb_reset_transport_header(skb);
231
232         if (sock_queue_err_skb(sk, skb))
233                 kfree_skb(skb);
234 }
235
236 void ipv6_local_error(struct sock *sk, int err, struct flowi *fl, u32 info)
237 {
238         struct ipv6_pinfo *np = inet6_sk(sk);
239         struct sock_exterr_skb *serr;
240         struct ipv6hdr *iph;
241         struct sk_buff *skb;
242
243         if (!np->recverr)
244                 return;
245
246         skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
247         if (!skb)
248                 return;
249
250         skb->protocol = htons(ETH_P_IPV6);
251
252         skb_put(skb, sizeof(struct ipv6hdr));
253         skb_reset_network_header(skb);
254         iph = ipv6_hdr(skb);
255         ipv6_addr_copy(&iph->daddr, &fl->fl6_dst);
256
257         serr = SKB_EXT_ERR(skb);
258         serr->ee.ee_errno = err;
259         serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
260         serr->ee.ee_type = 0;
261         serr->ee.ee_code = 0;
262         serr->ee.ee_pad = 0;
263         serr->ee.ee_info = info;
264         serr->ee.ee_data = 0;
265         serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
266         serr->port = fl->fl_ip_dport;
267
268         __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
269         skb_reset_transport_header(skb);
270
271         if (sock_queue_err_skb(sk, skb))
272                 kfree_skb(skb);
273 }
274
275 void ipv6_local_rxpmtu(struct sock *sk, struct flowi *fl, u32 mtu)
276 {
277         struct ipv6_pinfo *np = inet6_sk(sk);
278         struct ipv6hdr *iph;
279         struct sk_buff *skb;
280         struct ip6_mtuinfo *mtu_info;
281
282         if (!np->rxopt.bits.rxpmtu)
283                 return;
284
285         skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
286         if (!skb)
287                 return;
288
289         skb_put(skb, sizeof(struct ipv6hdr));
290         skb_reset_network_header(skb);
291         iph = ipv6_hdr(skb);
292         ipv6_addr_copy(&iph->daddr, &fl->fl6_dst);
293
294         mtu_info = IP6CBMTU(skb);
295         if (!mtu_info) {
296                 kfree_skb(skb);
297                 return;
298         }
299
300         mtu_info->ip6m_mtu = mtu;
301         mtu_info->ip6m_addr.sin6_family = AF_INET6;
302         mtu_info->ip6m_addr.sin6_port = 0;
303         mtu_info->ip6m_addr.sin6_flowinfo = 0;
304         mtu_info->ip6m_addr.sin6_scope_id = fl->oif;
305         ipv6_addr_copy(&mtu_info->ip6m_addr.sin6_addr, &ipv6_hdr(skb)->daddr);
306
307         __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
308         skb_reset_transport_header(skb);
309
310         skb = xchg(&np->rxpmtu, skb);
311         kfree_skb(skb);
312 }
313
314 /*
315  *      Handle MSG_ERRQUEUE
316  */
317 int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len)
318 {
319         struct ipv6_pinfo *np = inet6_sk(sk);
320         struct sock_exterr_skb *serr;
321         struct sk_buff *skb, *skb2;
322         struct sockaddr_in6 *sin;
323         struct {
324                 struct sock_extended_err ee;
325                 struct sockaddr_in6      offender;
326         } errhdr;
327         int err;
328         int copied;
329
330         err = -EAGAIN;
331         skb = skb_dequeue(&sk->sk_error_queue);
332         if (skb == NULL)
333                 goto out;
334
335         copied = skb->len;
336         if (copied > len) {
337                 msg->msg_flags |= MSG_TRUNC;
338                 copied = len;
339         }
340         err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
341         if (err)
342                 goto out_free_skb;
343
344         sock_recv_timestamp(msg, sk, skb);
345
346         serr = SKB_EXT_ERR(skb);
347
348         sin = (struct sockaddr_in6 *)msg->msg_name;
349         if (sin) {
350                 const unsigned char *nh = skb_network_header(skb);
351                 sin->sin6_family = AF_INET6;
352                 sin->sin6_flowinfo = 0;
353                 sin->sin6_port = serr->port;
354                 sin->sin6_scope_id = 0;
355                 if (skb->protocol == htons(ETH_P_IPV6)) {
356                         ipv6_addr_copy(&sin->sin6_addr,
357                                   (struct in6_addr *)(nh + serr->addr_offset));
358                         if (np->sndflow)
359                                 sin->sin6_flowinfo =
360                                         (*(__be32 *)(nh + serr->addr_offset - 24) &
361                                          IPV6_FLOWINFO_MASK);
362                         if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
363                                 sin->sin6_scope_id = IP6CB(skb)->iif;
364                 } else {
365                         ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset),
366                                                &sin->sin6_addr);
367                 }
368         }
369
370         memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
371         sin = &errhdr.offender;
372         sin->sin6_family = AF_UNSPEC;
373         if (serr->ee.ee_origin != SO_EE_ORIGIN_LOCAL) {
374                 sin->sin6_family = AF_INET6;
375                 sin->sin6_flowinfo = 0;
376                 sin->sin6_scope_id = 0;
377                 if (skb->protocol == htons(ETH_P_IPV6)) {
378                         ipv6_addr_copy(&sin->sin6_addr, &ipv6_hdr(skb)->saddr);
379                         if (np->rxopt.all)
380                                 datagram_recv_ctl(sk, msg, skb);
381                         if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
382                                 sin->sin6_scope_id = IP6CB(skb)->iif;
383                 } else {
384                         struct inet_sock *inet = inet_sk(sk);
385
386                         ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
387                                                &sin->sin6_addr);
388                         if (inet->cmsg_flags)
389                                 ip_cmsg_recv(msg, skb);
390                 }
391         }
392
393         put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr);
394
395         /* Now we could try to dump offended packet options */
396
397         msg->msg_flags |= MSG_ERRQUEUE;
398         err = copied;
399
400         /* Reset and regenerate socket error */
401         spin_lock_bh(&sk->sk_error_queue.lock);
402         sk->sk_err = 0;
403         if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
404                 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
405                 spin_unlock_bh(&sk->sk_error_queue.lock);
406                 sk->sk_error_report(sk);
407         } else {
408                 spin_unlock_bh(&sk->sk_error_queue.lock);
409         }
410
411 out_free_skb:
412         kfree_skb(skb);
413 out:
414         return err;
415 }
416
417 /*
418  *      Handle IPV6_RECVPATHMTU
419  */
420 int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len)
421 {
422         struct ipv6_pinfo *np = inet6_sk(sk);
423         struct sk_buff *skb;
424         struct sockaddr_in6 *sin;
425         struct ip6_mtuinfo mtu_info;
426         int err;
427         int copied;
428
429         err = -EAGAIN;
430         skb = xchg(&np->rxpmtu, NULL);
431         if (skb == NULL)
432                 goto out;
433
434         copied = skb->len;
435         if (copied > len) {
436                 msg->msg_flags |= MSG_TRUNC;
437                 copied = len;
438         }
439         err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
440         if (err)
441                 goto out_free_skb;
442
443         sock_recv_timestamp(msg, sk, skb);
444
445         memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info));
446
447         sin = (struct sockaddr_in6 *)msg->msg_name;
448         if (sin) {
449                 sin->sin6_family = AF_INET6;
450                 sin->sin6_flowinfo = 0;
451                 sin->sin6_port = 0;
452                 sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id;
453                 ipv6_addr_copy(&sin->sin6_addr, &mtu_info.ip6m_addr.sin6_addr);
454         }
455
456         put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info);
457
458         err = copied;
459
460 out_free_skb:
461         kfree_skb(skb);
462 out:
463         return err;
464 }
465
466
467 int datagram_recv_ctl(struct sock *sk, struct msghdr *msg, struct sk_buff *skb)
468 {
469         struct ipv6_pinfo *np = inet6_sk(sk);
470         struct inet6_skb_parm *opt = IP6CB(skb);
471         unsigned char *nh = skb_network_header(skb);
472
473         if (np->rxopt.bits.rxinfo) {
474                 struct in6_pktinfo src_info;
475
476                 src_info.ipi6_ifindex = opt->iif;
477                 ipv6_addr_copy(&src_info.ipi6_addr, &ipv6_hdr(skb)->daddr);
478                 put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
479         }
480
481         if (np->rxopt.bits.rxhlim) {
482                 int hlim = ipv6_hdr(skb)->hop_limit;
483                 put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
484         }
485
486         if (np->rxopt.bits.rxtclass) {
487                 int tclass = (ntohl(*(__be32 *)ipv6_hdr(skb)) >> 20) & 0xff;
488                 put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
489         }
490
491         if (np->rxopt.bits.rxflow && (*(__be32 *)nh & IPV6_FLOWINFO_MASK)) {
492                 __be32 flowinfo = *(__be32 *)nh & IPV6_FLOWINFO_MASK;
493                 put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
494         }
495
496         /* HbH is allowed only once */
497         if (np->rxopt.bits.hopopts && opt->hop) {
498                 u8 *ptr = nh + opt->hop;
499                 put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
500         }
501
502         if (opt->lastopt &&
503             (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
504                 /*
505                  * Silly enough, but we need to reparse in order to
506                  * report extension headers (except for HbH)
507                  * in order.
508                  *
509                  * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
510                  * (and WILL NOT be) defined because
511                  * IPV6_RECVDSTOPTS is more generic. --yoshfuji
512                  */
513                 unsigned int off = sizeof(struct ipv6hdr);
514                 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
515
516                 while (off <= opt->lastopt) {
517                         unsigned len;
518                         u8 *ptr = nh + off;
519
520                         switch(nexthdr) {
521                         case IPPROTO_DSTOPTS:
522                                 nexthdr = ptr[0];
523                                 len = (ptr[1] + 1) << 3;
524                                 if (np->rxopt.bits.dstopts)
525                                         put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
526                                 break;
527                         case IPPROTO_ROUTING:
528                                 nexthdr = ptr[0];
529                                 len = (ptr[1] + 1) << 3;
530                                 if (np->rxopt.bits.srcrt)
531                                         put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
532                                 break;
533                         case IPPROTO_AH:
534                                 nexthdr = ptr[0];
535                                 len = (ptr[1] + 2) << 2;
536                                 break;
537                         default:
538                                 nexthdr = ptr[0];
539                                 len = (ptr[1] + 1) << 3;
540                                 break;
541                         }
542
543                         off += len;
544                 }
545         }
546
547         /* socket options in old style */
548         if (np->rxopt.bits.rxoinfo) {
549                 struct in6_pktinfo src_info;
550
551                 src_info.ipi6_ifindex = opt->iif;
552                 ipv6_addr_copy(&src_info.ipi6_addr, &ipv6_hdr(skb)->daddr);
553                 put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
554         }
555         if (np->rxopt.bits.rxohlim) {
556                 int hlim = ipv6_hdr(skb)->hop_limit;
557                 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
558         }
559         if (np->rxopt.bits.ohopopts && opt->hop) {
560                 u8 *ptr = nh + opt->hop;
561                 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr);
562         }
563         if (np->rxopt.bits.odstopts && opt->dst0) {
564                 u8 *ptr = nh + opt->dst0;
565                 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
566         }
567         if (np->rxopt.bits.osrcrt && opt->srcrt) {
568                 struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
569                 put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
570         }
571         if (np->rxopt.bits.odstopts && opt->dst1) {
572                 u8 *ptr = nh + opt->dst1;
573                 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
574         }
575         return 0;
576 }
577
578 int datagram_send_ctl(struct net *net,
579                       struct msghdr *msg, struct flowi *fl,
580                       struct ipv6_txoptions *opt,
581                       int *hlimit, int *tclass, int *dontfrag)
582 {
583         struct in6_pktinfo *src_info;
584         struct cmsghdr *cmsg;
585         struct ipv6_rt_hdr *rthdr;
586         struct ipv6_opt_hdr *hdr;
587         int len;
588         int err = 0;
589
590         for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
591                 int addr_type;
592
593                 if (!CMSG_OK(msg, cmsg)) {
594                         err = -EINVAL;
595                         goto exit_f;
596                 }
597
598                 if (cmsg->cmsg_level != SOL_IPV6)
599                         continue;
600
601                 switch (cmsg->cmsg_type) {
602                 case IPV6_PKTINFO:
603                 case IPV6_2292PKTINFO:
604                     {
605                         struct net_device *dev = NULL;
606
607                         if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
608                                 err = -EINVAL;
609                                 goto exit_f;
610                         }
611
612                         src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
613
614                         if (src_info->ipi6_ifindex) {
615                                 if (fl->oif && src_info->ipi6_ifindex != fl->oif)
616                                         return -EINVAL;
617                                 fl->oif = src_info->ipi6_ifindex;
618                         }
619
620                         addr_type = __ipv6_addr_type(&src_info->ipi6_addr);
621
622                         rcu_read_lock();
623                         if (fl->oif) {
624                                 dev = dev_get_by_index_rcu(net, fl->oif);
625                                 if (!dev) {
626                                         rcu_read_unlock();
627                                         return -ENODEV;
628                                 }
629                         } else if (addr_type & IPV6_ADDR_LINKLOCAL) {
630                                 rcu_read_unlock();
631                                 return -EINVAL;
632                         }
633
634                         if (addr_type != IPV6_ADDR_ANY) {
635                                 int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL;
636                                 if (!ipv6_chk_addr(net, &src_info->ipi6_addr,
637                                                    strict ? dev : NULL, 0))
638                                         err = -EINVAL;
639                                 else
640                                         ipv6_addr_copy(&fl->fl6_src, &src_info->ipi6_addr);
641                         }
642
643                         rcu_read_unlock();
644
645                         if (err)
646                                 goto exit_f;
647
648                         break;
649                     }
650
651                 case IPV6_FLOWINFO:
652                         if (cmsg->cmsg_len < CMSG_LEN(4)) {
653                                 err = -EINVAL;
654                                 goto exit_f;
655                         }
656
657                         if (fl->fl6_flowlabel&IPV6_FLOWINFO_MASK) {
658                                 if ((fl->fl6_flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
659                                         err = -EINVAL;
660                                         goto exit_f;
661                                 }
662                         }
663                         fl->fl6_flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
664                         break;
665
666                 case IPV6_2292HOPOPTS:
667                 case IPV6_HOPOPTS:
668                         if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
669                                 err = -EINVAL;
670                                 goto exit_f;
671                         }
672
673                         hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
674                         len = ((hdr->hdrlen + 1) << 3);
675                         if (cmsg->cmsg_len < CMSG_LEN(len)) {
676                                 err = -EINVAL;
677                                 goto exit_f;
678                         }
679                         if (!capable(CAP_NET_RAW)) {
680                                 err = -EPERM;
681                                 goto exit_f;
682                         }
683                         opt->opt_nflen += len;
684                         opt->hopopt = hdr;
685                         break;
686
687                 case IPV6_2292DSTOPTS:
688                         if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
689                                 err = -EINVAL;
690                                 goto exit_f;
691                         }
692
693                         hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
694                         len = ((hdr->hdrlen + 1) << 3);
695                         if (cmsg->cmsg_len < CMSG_LEN(len)) {
696                                 err = -EINVAL;
697                                 goto exit_f;
698                         }
699                         if (!capable(CAP_NET_RAW)) {
700                                 err = -EPERM;
701                                 goto exit_f;
702                         }
703                         if (opt->dst1opt) {
704                                 err = -EINVAL;
705                                 goto exit_f;
706                         }
707                         opt->opt_flen += len;
708                         opt->dst1opt = hdr;
709                         break;
710
711                 case IPV6_DSTOPTS:
712                 case IPV6_RTHDRDSTOPTS:
713                         if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
714                                 err = -EINVAL;
715                                 goto exit_f;
716                         }
717
718                         hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
719                         len = ((hdr->hdrlen + 1) << 3);
720                         if (cmsg->cmsg_len < CMSG_LEN(len)) {
721                                 err = -EINVAL;
722                                 goto exit_f;
723                         }
724                         if (!capable(CAP_NET_RAW)) {
725                                 err = -EPERM;
726                                 goto exit_f;
727                         }
728                         if (cmsg->cmsg_type == IPV6_DSTOPTS) {
729                                 opt->opt_flen += len;
730                                 opt->dst1opt = hdr;
731                         } else {
732                                 opt->opt_nflen += len;
733                                 opt->dst0opt = hdr;
734                         }
735                         break;
736
737                 case IPV6_2292RTHDR:
738                 case IPV6_RTHDR:
739                         if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
740                                 err = -EINVAL;
741                                 goto exit_f;
742                         }
743
744                         rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
745
746                         switch (rthdr->type) {
747 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
748                         case IPV6_SRCRT_TYPE_2:
749                                 if (rthdr->hdrlen != 2 ||
750                                     rthdr->segments_left != 1) {
751                                         err = -EINVAL;
752                                         goto exit_f;
753                                 }
754                                 break;
755 #endif
756                         default:
757                                 err = -EINVAL;
758                                 goto exit_f;
759                         }
760
761                         len = ((rthdr->hdrlen + 1) << 3);
762
763                         if (cmsg->cmsg_len < CMSG_LEN(len)) {
764                                 err = -EINVAL;
765                                 goto exit_f;
766                         }
767
768                         /* segments left must also match */
769                         if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
770                                 err = -EINVAL;
771                                 goto exit_f;
772                         }
773
774                         opt->opt_nflen += len;
775                         opt->srcrt = rthdr;
776
777                         if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
778                                 int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
779
780                                 opt->opt_nflen += dsthdrlen;
781                                 opt->dst0opt = opt->dst1opt;
782                                 opt->dst1opt = NULL;
783                                 opt->opt_flen -= dsthdrlen;
784                         }
785
786                         break;
787
788                 case IPV6_2292HOPLIMIT:
789                 case IPV6_HOPLIMIT:
790                         if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
791                                 err = -EINVAL;
792                                 goto exit_f;
793                         }
794
795                         *hlimit = *(int *)CMSG_DATA(cmsg);
796                         if (*hlimit < -1 || *hlimit > 0xff) {
797                                 err = -EINVAL;
798                                 goto exit_f;
799                         }
800
801                         break;
802
803                 case IPV6_TCLASS:
804                     {
805                         int tc;
806
807                         err = -EINVAL;
808                         if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
809                                 goto exit_f;
810                         }
811
812                         tc = *(int *)CMSG_DATA(cmsg);
813                         if (tc < -1 || tc > 0xff)
814                                 goto exit_f;
815
816                         err = 0;
817                         *tclass = tc;
818
819                         break;
820                     }
821
822                 case IPV6_DONTFRAG:
823                     {
824                         int df;
825
826                         err = -EINVAL;
827                         if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
828                                 goto exit_f;
829                         }
830
831                         df = *(int *)CMSG_DATA(cmsg);
832                         if (df < 0 || df > 1)
833                                 goto exit_f;
834
835                         err = 0;
836                         *dontfrag = df;
837
838                         break;
839                     }
840                 default:
841                         LIMIT_NETDEBUG(KERN_DEBUG "invalid cmsg type: %d\n",
842                                        cmsg->cmsg_type);
843                         err = -EINVAL;
844                         goto exit_f;
845                 }
846         }
847
848 exit_f:
849         return err;
850 }