ipv6: use newly introduced __ipv6_addr_needs_scope_id and ipv6_iface_scope_id
[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  *      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 #include <linux/export.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 #include <net/dsfield.h>
34
35 #include <linux/errqueue.h>
36 #include <asm/uaccess.h>
37
38 static bool ipv6_mapped_addr_any(const struct in6_addr *a)
39 {
40         return ipv6_addr_v4mapped(a) && (a->s6_addr32[3] == 0);
41 }
42
43 int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
44 {
45         struct sockaddr_in6     *usin = (struct sockaddr_in6 *) uaddr;
46         struct inet_sock        *inet = inet_sk(sk);
47         struct ipv6_pinfo       *np = inet6_sk(sk);
48         struct in6_addr         *daddr, *final_p, final;
49         struct dst_entry        *dst;
50         struct flowi6           fl6;
51         struct ip6_flowlabel    *flowlabel = NULL;
52         struct ipv6_txoptions   *opt;
53         int                     addr_type;
54         int                     err;
55
56         if (usin->sin6_family == AF_INET) {
57                 if (__ipv6_only_sock(sk))
58                         return -EAFNOSUPPORT;
59                 err = ip4_datagram_connect(sk, uaddr, addr_len);
60                 goto ipv4_connected;
61         }
62
63         if (addr_len < SIN6_LEN_RFC2133)
64                 return -EINVAL;
65
66         if (usin->sin6_family != AF_INET6)
67                 return -EAFNOSUPPORT;
68
69         memset(&fl6, 0, sizeof(fl6));
70         if (np->sndflow) {
71                 fl6.flowlabel = usin->sin6_flowinfo&IPV6_FLOWINFO_MASK;
72                 if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
73                         flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
74                         if (flowlabel == NULL)
75                                 return -EINVAL;
76                         usin->sin6_addr = flowlabel->dst;
77                 }
78         }
79
80         addr_type = ipv6_addr_type(&usin->sin6_addr);
81
82         if (addr_type == IPV6_ADDR_ANY) {
83                 /*
84                  *      connect to self
85                  */
86                 usin->sin6_addr.s6_addr[15] = 0x01;
87         }
88
89         daddr = &usin->sin6_addr;
90
91         if (addr_type == IPV6_ADDR_MAPPED) {
92                 struct sockaddr_in sin;
93
94                 if (__ipv6_only_sock(sk)) {
95                         err = -ENETUNREACH;
96                         goto out;
97                 }
98                 sin.sin_family = AF_INET;
99                 sin.sin_addr.s_addr = daddr->s6_addr32[3];
100                 sin.sin_port = usin->sin6_port;
101
102                 err = ip4_datagram_connect(sk,
103                                            (struct sockaddr *) &sin,
104                                            sizeof(sin));
105
106 ipv4_connected:
107                 if (err)
108                         goto out;
109
110                 ipv6_addr_set_v4mapped(inet->inet_daddr, &np->daddr);
111
112                 if (ipv6_addr_any(&np->saddr) ||
113                     ipv6_mapped_addr_any(&np->saddr))
114                         ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
115
116                 if (ipv6_addr_any(&np->rcv_saddr) ||
117                     ipv6_mapped_addr_any(&np->rcv_saddr)) {
118                         ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
119                                                &np->rcv_saddr);
120                         if (sk->sk_prot->rehash)
121                                 sk->sk_prot->rehash(sk);
122                 }
123
124                 goto out;
125         }
126
127         if (__ipv6_addr_needs_scope_id(addr_type)) {
128                 if (addr_len >= sizeof(struct sockaddr_in6) &&
129                     usin->sin6_scope_id) {
130                         if (sk->sk_bound_dev_if &&
131                             sk->sk_bound_dev_if != usin->sin6_scope_id) {
132                                 err = -EINVAL;
133                                 goto out;
134                         }
135                         sk->sk_bound_dev_if = usin->sin6_scope_id;
136                 }
137
138                 if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST))
139                         sk->sk_bound_dev_if = np->mcast_oif;
140
141                 /* Connect to link-local address requires an interface */
142                 if (!sk->sk_bound_dev_if) {
143                         err = -EINVAL;
144                         goto out;
145                 }
146         }
147
148         np->daddr = *daddr;
149         np->flow_label = fl6.flowlabel;
150
151         inet->inet_dport = usin->sin6_port;
152
153         /*
154          *      Check for a route to destination an obtain the
155          *      destination cache for it.
156          */
157
158         fl6.flowi6_proto = sk->sk_protocol;
159         fl6.daddr = np->daddr;
160         fl6.saddr = np->saddr;
161         fl6.flowi6_oif = sk->sk_bound_dev_if;
162         fl6.flowi6_mark = sk->sk_mark;
163         fl6.fl6_dport = inet->inet_dport;
164         fl6.fl6_sport = inet->inet_sport;
165
166         if (!fl6.flowi6_oif && (addr_type&IPV6_ADDR_MULTICAST))
167                 fl6.flowi6_oif = np->mcast_oif;
168
169         security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
170
171         opt = flowlabel ? flowlabel->opt : np->opt;
172         final_p = fl6_update_dst(&fl6, opt, &final);
173
174         dst = ip6_dst_lookup_flow(sk, &fl6, final_p, true);
175         err = 0;
176         if (IS_ERR(dst)) {
177                 err = PTR_ERR(dst);
178                 goto out;
179         }
180
181         /* source address lookup done in ip6_dst_lookup */
182
183         if (ipv6_addr_any(&np->saddr))
184                 np->saddr = fl6.saddr;
185
186         if (ipv6_addr_any(&np->rcv_saddr)) {
187                 np->rcv_saddr = fl6.saddr;
188                 inet->inet_rcv_saddr = LOOPBACK4_IPV6;
189                 if (sk->sk_prot->rehash)
190                         sk->sk_prot->rehash(sk);
191         }
192
193         ip6_dst_store(sk, dst,
194                       ipv6_addr_equal(&fl6.daddr, &np->daddr) ?
195                       &np->daddr : NULL,
196 #ifdef CONFIG_IPV6_SUBTREES
197                       ipv6_addr_equal(&fl6.saddr, &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 EXPORT_SYMBOL_GPL(ip6_datagram_connect);
208
209 void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
210                      __be16 port, u32 info, u8 *payload)
211 {
212         struct ipv6_pinfo *np  = inet6_sk(sk);
213         struct icmp6hdr *icmph = icmp6_hdr(skb);
214         struct sock_exterr_skb *serr;
215
216         if (!np->recverr)
217                 return;
218
219         skb = skb_clone(skb, GFP_ATOMIC);
220         if (!skb)
221                 return;
222
223         skb->protocol = htons(ETH_P_IPV6);
224
225         serr = SKB_EXT_ERR(skb);
226         serr->ee.ee_errno = err;
227         serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6;
228         serr->ee.ee_type = icmph->icmp6_type;
229         serr->ee.ee_code = icmph->icmp6_code;
230         serr->ee.ee_pad = 0;
231         serr->ee.ee_info = info;
232         serr->ee.ee_data = 0;
233         serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
234                                   skb_network_header(skb);
235         serr->port = port;
236
237         __skb_pull(skb, payload - skb->data);
238         skb_reset_transport_header(skb);
239
240         if (sock_queue_err_skb(sk, skb))
241                 kfree_skb(skb);
242 }
243
244 void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info)
245 {
246         struct ipv6_pinfo *np = inet6_sk(sk);
247         struct sock_exterr_skb *serr;
248         struct ipv6hdr *iph;
249         struct sk_buff *skb;
250
251         if (!np->recverr)
252                 return;
253
254         skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
255         if (!skb)
256                 return;
257
258         skb->protocol = htons(ETH_P_IPV6);
259
260         skb_put(skb, sizeof(struct ipv6hdr));
261         skb_reset_network_header(skb);
262         iph = ipv6_hdr(skb);
263         iph->daddr = fl6->daddr;
264
265         serr = SKB_EXT_ERR(skb);
266         serr->ee.ee_errno = err;
267         serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
268         serr->ee.ee_type = 0;
269         serr->ee.ee_code = 0;
270         serr->ee.ee_pad = 0;
271         serr->ee.ee_info = info;
272         serr->ee.ee_data = 0;
273         serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
274         serr->port = fl6->fl6_dport;
275
276         __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
277         skb_reset_transport_header(skb);
278
279         if (sock_queue_err_skb(sk, skb))
280                 kfree_skb(skb);
281 }
282
283 void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu)
284 {
285         struct ipv6_pinfo *np = inet6_sk(sk);
286         struct ipv6hdr *iph;
287         struct sk_buff *skb;
288         struct ip6_mtuinfo *mtu_info;
289
290         if (!np->rxopt.bits.rxpmtu)
291                 return;
292
293         skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
294         if (!skb)
295                 return;
296
297         skb_put(skb, sizeof(struct ipv6hdr));
298         skb_reset_network_header(skb);
299         iph = ipv6_hdr(skb);
300         iph->daddr = fl6->daddr;
301
302         mtu_info = IP6CBMTU(skb);
303
304         mtu_info->ip6m_mtu = mtu;
305         mtu_info->ip6m_addr.sin6_family = AF_INET6;
306         mtu_info->ip6m_addr.sin6_port = 0;
307         mtu_info->ip6m_addr.sin6_flowinfo = 0;
308         mtu_info->ip6m_addr.sin6_scope_id = fl6->flowi6_oif;
309         mtu_info->ip6m_addr.sin6_addr = ipv6_hdr(skb)->daddr;
310
311         __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
312         skb_reset_transport_header(skb);
313
314         skb = xchg(&np->rxpmtu, skb);
315         kfree_skb(skb);
316 }
317
318 /*
319  *      Handle MSG_ERRQUEUE
320  */
321 int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len)
322 {
323         struct ipv6_pinfo *np = inet6_sk(sk);
324         struct sock_exterr_skb *serr;
325         struct sk_buff *skb, *skb2;
326         struct sockaddr_in6 *sin;
327         struct {
328                 struct sock_extended_err ee;
329                 struct sockaddr_in6      offender;
330         } errhdr;
331         int err;
332         int copied;
333
334         err = -EAGAIN;
335         skb = skb_dequeue(&sk->sk_error_queue);
336         if (skb == NULL)
337                 goto out;
338
339         copied = skb->len;
340         if (copied > len) {
341                 msg->msg_flags |= MSG_TRUNC;
342                 copied = len;
343         }
344         err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
345         if (err)
346                 goto out_free_skb;
347
348         sock_recv_timestamp(msg, sk, skb);
349
350         serr = SKB_EXT_ERR(skb);
351
352         sin = (struct sockaddr_in6 *)msg->msg_name;
353         if (sin) {
354                 const unsigned char *nh = skb_network_header(skb);
355                 sin->sin6_family = AF_INET6;
356                 sin->sin6_flowinfo = 0;
357                 sin->sin6_port = serr->port;
358                 if (skb->protocol == htons(ETH_P_IPV6)) {
359                         const struct ipv6hdr *ip6h = container_of((struct in6_addr *)(nh + serr->addr_offset),
360                                                                   struct ipv6hdr, daddr);
361                         sin->sin6_addr = ip6h->daddr;
362                         if (np->sndflow)
363                                 sin->sin6_flowinfo = ip6_flowinfo(ip6h);
364                         sin->sin6_scope_id =
365                                 ipv6_iface_scope_id(&sin->sin6_addr,
366                                                     IP6CB(skb)->iif);
367                 } else {
368                         ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset),
369                                                &sin->sin6_addr);
370                         sin->sin6_scope_id = 0;
371                 }
372         }
373
374         memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
375         sin = &errhdr.offender;
376         sin->sin6_family = AF_UNSPEC;
377         if (serr->ee.ee_origin != SO_EE_ORIGIN_LOCAL) {
378                 sin->sin6_family = AF_INET6;
379                 sin->sin6_flowinfo = 0;
380                 if (skb->protocol == htons(ETH_P_IPV6)) {
381                         sin->sin6_addr = ipv6_hdr(skb)->saddr;
382                         if (np->rxopt.all)
383                                 ip6_datagram_recv_ctl(sk, msg, skb);
384                         sin->sin6_scope_id =
385                                 ipv6_iface_scope_id(&sin->sin6_addr,
386                                                     IP6CB(skb)->iif);
387                 } else {
388                         struct inet_sock *inet = inet_sk(sk);
389
390                         ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
391                                                &sin->sin6_addr);
392                         sin->sin6_scope_id = 0;
393                         if (inet->cmsg_flags)
394                                 ip_cmsg_recv(msg, skb);
395                 }
396         }
397
398         put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr);
399
400         /* Now we could try to dump offended packet options */
401
402         msg->msg_flags |= MSG_ERRQUEUE;
403         err = copied;
404
405         /* Reset and regenerate socket error */
406         spin_lock_bh(&sk->sk_error_queue.lock);
407         sk->sk_err = 0;
408         if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
409                 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
410                 spin_unlock_bh(&sk->sk_error_queue.lock);
411                 sk->sk_error_report(sk);
412         } else {
413                 spin_unlock_bh(&sk->sk_error_queue.lock);
414         }
415
416 out_free_skb:
417         kfree_skb(skb);
418 out:
419         return err;
420 }
421 EXPORT_SYMBOL_GPL(ipv6_recv_error);
422
423 /*
424  *      Handle IPV6_RECVPATHMTU
425  */
426 int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len)
427 {
428         struct ipv6_pinfo *np = inet6_sk(sk);
429         struct sk_buff *skb;
430         struct sockaddr_in6 *sin;
431         struct ip6_mtuinfo mtu_info;
432         int err;
433         int copied;
434
435         err = -EAGAIN;
436         skb = xchg(&np->rxpmtu, NULL);
437         if (skb == NULL)
438                 goto out;
439
440         copied = skb->len;
441         if (copied > len) {
442                 msg->msg_flags |= MSG_TRUNC;
443                 copied = len;
444         }
445         err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
446         if (err)
447                 goto out_free_skb;
448
449         sock_recv_timestamp(msg, sk, skb);
450
451         memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info));
452
453         sin = (struct sockaddr_in6 *)msg->msg_name;
454         if (sin) {
455                 sin->sin6_family = AF_INET6;
456                 sin->sin6_flowinfo = 0;
457                 sin->sin6_port = 0;
458                 sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id;
459                 sin->sin6_addr = mtu_info.ip6m_addr.sin6_addr;
460         }
461
462         put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info);
463
464         err = copied;
465
466 out_free_skb:
467         kfree_skb(skb);
468 out:
469         return err;
470 }
471
472
473 int ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
474                           struct sk_buff *skb)
475 {
476         struct ipv6_pinfo *np = inet6_sk(sk);
477         struct inet6_skb_parm *opt = IP6CB(skb);
478         unsigned char *nh = skb_network_header(skb);
479
480         if (np->rxopt.bits.rxinfo) {
481                 struct in6_pktinfo src_info;
482
483                 src_info.ipi6_ifindex = opt->iif;
484                 src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
485                 put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
486         }
487
488         if (np->rxopt.bits.rxhlim) {
489                 int hlim = ipv6_hdr(skb)->hop_limit;
490                 put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
491         }
492
493         if (np->rxopt.bits.rxtclass) {
494                 int tclass = ipv6_get_dsfield(ipv6_hdr(skb));
495                 put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
496         }
497
498         if (np->rxopt.bits.rxflow) {
499                 __be32 flowinfo = ip6_flowinfo((struct ipv6hdr *)nh);
500                 if (flowinfo)
501                         put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
502         }
503
504         /* HbH is allowed only once */
505         if (np->rxopt.bits.hopopts && opt->hop) {
506                 u8 *ptr = nh + opt->hop;
507                 put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
508         }
509
510         if (opt->lastopt &&
511             (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
512                 /*
513                  * Silly enough, but we need to reparse in order to
514                  * report extension headers (except for HbH)
515                  * in order.
516                  *
517                  * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
518                  * (and WILL NOT be) defined because
519                  * IPV6_RECVDSTOPTS is more generic. --yoshfuji
520                  */
521                 unsigned int off = sizeof(struct ipv6hdr);
522                 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
523
524                 while (off <= opt->lastopt) {
525                         unsigned int len;
526                         u8 *ptr = nh + off;
527
528                         switch (nexthdr) {
529                         case IPPROTO_DSTOPTS:
530                                 nexthdr = ptr[0];
531                                 len = (ptr[1] + 1) << 3;
532                                 if (np->rxopt.bits.dstopts)
533                                         put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
534                                 break;
535                         case IPPROTO_ROUTING:
536                                 nexthdr = ptr[0];
537                                 len = (ptr[1] + 1) << 3;
538                                 if (np->rxopt.bits.srcrt)
539                                         put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
540                                 break;
541                         case IPPROTO_AH:
542                                 nexthdr = ptr[0];
543                                 len = (ptr[1] + 2) << 2;
544                                 break;
545                         default:
546                                 nexthdr = ptr[0];
547                                 len = (ptr[1] + 1) << 3;
548                                 break;
549                         }
550
551                         off += len;
552                 }
553         }
554
555         /* socket options in old style */
556         if (np->rxopt.bits.rxoinfo) {
557                 struct in6_pktinfo src_info;
558
559                 src_info.ipi6_ifindex = opt->iif;
560                 src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
561                 put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
562         }
563         if (np->rxopt.bits.rxohlim) {
564                 int hlim = ipv6_hdr(skb)->hop_limit;
565                 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
566         }
567         if (np->rxopt.bits.ohopopts && opt->hop) {
568                 u8 *ptr = nh + opt->hop;
569                 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr);
570         }
571         if (np->rxopt.bits.odstopts && opt->dst0) {
572                 u8 *ptr = nh + opt->dst0;
573                 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
574         }
575         if (np->rxopt.bits.osrcrt && opt->srcrt) {
576                 struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
577                 put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
578         }
579         if (np->rxopt.bits.odstopts && opt->dst1) {
580                 u8 *ptr = nh + opt->dst1;
581                 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
582         }
583         if (np->rxopt.bits.rxorigdstaddr) {
584                 struct sockaddr_in6 sin6;
585                 __be16 *ports = (__be16 *) skb_transport_header(skb);
586
587                 if (skb_transport_offset(skb) + 4 <= skb->len) {
588                         /* All current transport protocols have the port numbers in the
589                          * first four bytes of the transport header and this function is
590                          * written with this assumption in mind.
591                          */
592
593                         sin6.sin6_family = AF_INET6;
594                         sin6.sin6_addr = ipv6_hdr(skb)->daddr;
595                         sin6.sin6_port = ports[1];
596                         sin6.sin6_flowinfo = 0;
597                         sin6.sin6_scope_id = 0;
598
599                         put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6);
600                 }
601         }
602         return 0;
603 }
604 EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl);
605
606 int ip6_datagram_send_ctl(struct net *net, struct sock *sk,
607                           struct msghdr *msg, struct flowi6 *fl6,
608                           struct ipv6_txoptions *opt,
609                           int *hlimit, int *tclass, int *dontfrag)
610 {
611         struct in6_pktinfo *src_info;
612         struct cmsghdr *cmsg;
613         struct ipv6_rt_hdr *rthdr;
614         struct ipv6_opt_hdr *hdr;
615         int len;
616         int err = 0;
617
618         for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
619                 int addr_type;
620
621                 if (!CMSG_OK(msg, cmsg)) {
622                         err = -EINVAL;
623                         goto exit_f;
624                 }
625
626                 if (cmsg->cmsg_level != SOL_IPV6)
627                         continue;
628
629                 switch (cmsg->cmsg_type) {
630                 case IPV6_PKTINFO:
631                 case IPV6_2292PKTINFO:
632                     {
633                         struct net_device *dev = NULL;
634
635                         if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
636                                 err = -EINVAL;
637                                 goto exit_f;
638                         }
639
640                         src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
641
642                         if (src_info->ipi6_ifindex) {
643                                 if (fl6->flowi6_oif &&
644                                     src_info->ipi6_ifindex != fl6->flowi6_oif)
645                                         return -EINVAL;
646                                 fl6->flowi6_oif = src_info->ipi6_ifindex;
647                         }
648
649                         addr_type = __ipv6_addr_type(&src_info->ipi6_addr);
650
651                         rcu_read_lock();
652                         if (fl6->flowi6_oif) {
653                                 dev = dev_get_by_index_rcu(net, fl6->flowi6_oif);
654                                 if (!dev) {
655                                         rcu_read_unlock();
656                                         return -ENODEV;
657                                 }
658                         } else if (addr_type & IPV6_ADDR_LINKLOCAL) {
659                                 rcu_read_unlock();
660                                 return -EINVAL;
661                         }
662
663                         if (addr_type != IPV6_ADDR_ANY) {
664                                 int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL;
665                                 if (!(inet_sk(sk)->freebind || inet_sk(sk)->transparent) &&
666                                     !ipv6_chk_addr(net, &src_info->ipi6_addr,
667                                                    strict ? dev : NULL, 0))
668                                         err = -EINVAL;
669                                 else
670                                         fl6->saddr = src_info->ipi6_addr;
671                         }
672
673                         rcu_read_unlock();
674
675                         if (err)
676                                 goto exit_f;
677
678                         break;
679                     }
680
681                 case IPV6_FLOWINFO:
682                         if (cmsg->cmsg_len < CMSG_LEN(4)) {
683                                 err = -EINVAL;
684                                 goto exit_f;
685                         }
686
687                         if (fl6->flowlabel&IPV6_FLOWINFO_MASK) {
688                                 if ((fl6->flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
689                                         err = -EINVAL;
690                                         goto exit_f;
691                                 }
692                         }
693                         fl6->flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
694                         break;
695
696                 case IPV6_2292HOPOPTS:
697                 case IPV6_HOPOPTS:
698                         if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
699                                 err = -EINVAL;
700                                 goto exit_f;
701                         }
702
703                         hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
704                         len = ((hdr->hdrlen + 1) << 3);
705                         if (cmsg->cmsg_len < CMSG_LEN(len)) {
706                                 err = -EINVAL;
707                                 goto exit_f;
708                         }
709                         if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
710                                 err = -EPERM;
711                                 goto exit_f;
712                         }
713                         opt->opt_nflen += len;
714                         opt->hopopt = hdr;
715                         break;
716
717                 case IPV6_2292DSTOPTS:
718                         if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
719                                 err = -EINVAL;
720                                 goto exit_f;
721                         }
722
723                         hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
724                         len = ((hdr->hdrlen + 1) << 3);
725                         if (cmsg->cmsg_len < CMSG_LEN(len)) {
726                                 err = -EINVAL;
727                                 goto exit_f;
728                         }
729                         if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
730                                 err = -EPERM;
731                                 goto exit_f;
732                         }
733                         if (opt->dst1opt) {
734                                 err = -EINVAL;
735                                 goto exit_f;
736                         }
737                         opt->opt_flen += len;
738                         opt->dst1opt = hdr;
739                         break;
740
741                 case IPV6_DSTOPTS:
742                 case IPV6_RTHDRDSTOPTS:
743                         if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
744                                 err = -EINVAL;
745                                 goto exit_f;
746                         }
747
748                         hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
749                         len = ((hdr->hdrlen + 1) << 3);
750                         if (cmsg->cmsg_len < CMSG_LEN(len)) {
751                                 err = -EINVAL;
752                                 goto exit_f;
753                         }
754                         if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
755                                 err = -EPERM;
756                                 goto exit_f;
757                         }
758                         if (cmsg->cmsg_type == IPV6_DSTOPTS) {
759                                 opt->opt_flen += len;
760                                 opt->dst1opt = hdr;
761                         } else {
762                                 opt->opt_nflen += len;
763                                 opt->dst0opt = hdr;
764                         }
765                         break;
766
767                 case IPV6_2292RTHDR:
768                 case IPV6_RTHDR:
769                         if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
770                                 err = -EINVAL;
771                                 goto exit_f;
772                         }
773
774                         rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
775
776                         switch (rthdr->type) {
777 #if IS_ENABLED(CONFIG_IPV6_MIP6)
778                         case IPV6_SRCRT_TYPE_2:
779                                 if (rthdr->hdrlen != 2 ||
780                                     rthdr->segments_left != 1) {
781                                         err = -EINVAL;
782                                         goto exit_f;
783                                 }
784                                 break;
785 #endif
786                         default:
787                                 err = -EINVAL;
788                                 goto exit_f;
789                         }
790
791                         len = ((rthdr->hdrlen + 1) << 3);
792
793                         if (cmsg->cmsg_len < CMSG_LEN(len)) {
794                                 err = -EINVAL;
795                                 goto exit_f;
796                         }
797
798                         /* segments left must also match */
799                         if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
800                                 err = -EINVAL;
801                                 goto exit_f;
802                         }
803
804                         opt->opt_nflen += len;
805                         opt->srcrt = rthdr;
806
807                         if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
808                                 int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
809
810                                 opt->opt_nflen += dsthdrlen;
811                                 opt->dst0opt = opt->dst1opt;
812                                 opt->dst1opt = NULL;
813                                 opt->opt_flen -= dsthdrlen;
814                         }
815
816                         break;
817
818                 case IPV6_2292HOPLIMIT:
819                 case IPV6_HOPLIMIT:
820                         if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
821                                 err = -EINVAL;
822                                 goto exit_f;
823                         }
824
825                         *hlimit = *(int *)CMSG_DATA(cmsg);
826                         if (*hlimit < -1 || *hlimit > 0xff) {
827                                 err = -EINVAL;
828                                 goto exit_f;
829                         }
830
831                         break;
832
833                 case IPV6_TCLASS:
834                     {
835                         int tc;
836
837                         err = -EINVAL;
838                         if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
839                                 goto exit_f;
840
841                         tc = *(int *)CMSG_DATA(cmsg);
842                         if (tc < -1 || tc > 0xff)
843                                 goto exit_f;
844
845                         err = 0;
846                         *tclass = tc;
847
848                         break;
849                     }
850
851                 case IPV6_DONTFRAG:
852                     {
853                         int df;
854
855                         err = -EINVAL;
856                         if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
857                                 goto exit_f;
858
859                         df = *(int *)CMSG_DATA(cmsg);
860                         if (df < 0 || df > 1)
861                                 goto exit_f;
862
863                         err = 0;
864                         *dontfrag = df;
865
866                         break;
867                     }
868                 default:
869                         LIMIT_NETDEBUG(KERN_DEBUG "invalid cmsg type: %d\n",
870                                        cmsg->cmsg_type);
871                         err = -EINVAL;
872                         goto exit_f;
873                 }
874         }
875
876 exit_f:
877         return err;
878 }
879 EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl);