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