GNU Linux-libre 6.1.90-gnu
[releases.git] / net / ipv6 / datagram.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *      common UDP/RAW code
4  *      Linux INET6 implementation
5  *
6  *      Authors:
7  *      Pedro Roque             <roque@di.fc.ul.pt>
8  */
9
10 #include <linux/capability.h>
11 #include <linux/errno.h>
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/interrupt.h>
15 #include <linux/socket.h>
16 #include <linux/sockios.h>
17 #include <linux/in6.h>
18 #include <linux/ipv6.h>
19 #include <linux/route.h>
20 #include <linux/slab.h>
21 #include <linux/export.h>
22 #include <linux/icmp.h>
23
24 #include <net/ipv6.h>
25 #include <net/ndisc.h>
26 #include <net/addrconf.h>
27 #include <net/transp_v6.h>
28 #include <net/ip6_route.h>
29 #include <net/tcp_states.h>
30 #include <net/dsfield.h>
31 #include <net/sock_reuseport.h>
32
33 #include <linux/errqueue.h>
34 #include <linux/uaccess.h>
35
36 static bool ipv6_mapped_addr_any(const struct in6_addr *a)
37 {
38         return ipv6_addr_v4mapped(a) && (a->s6_addr32[3] == 0);
39 }
40
41 static void ip6_datagram_flow_key_init(struct flowi6 *fl6, struct sock *sk)
42 {
43         struct inet_sock *inet = inet_sk(sk);
44         struct ipv6_pinfo *np = inet6_sk(sk);
45         int oif = sk->sk_bound_dev_if;
46
47         memset(fl6, 0, sizeof(*fl6));
48         fl6->flowi6_proto = sk->sk_protocol;
49         fl6->daddr = sk->sk_v6_daddr;
50         fl6->saddr = np->saddr;
51         fl6->flowi6_mark = sk->sk_mark;
52         fl6->fl6_dport = inet->inet_dport;
53         fl6->fl6_sport = inet->inet_sport;
54         fl6->flowlabel = ip6_make_flowinfo(np->tclass, np->flow_label);
55         fl6->flowi6_uid = sk->sk_uid;
56
57         if (!oif)
58                 oif = np->sticky_pktinfo.ipi6_ifindex;
59
60         if (!oif) {
61                 if (ipv6_addr_is_multicast(&fl6->daddr))
62                         oif = np->mcast_oif;
63                 else
64                         oif = np->ucast_oif;
65         }
66
67         fl6->flowi6_oif = oif;
68         security_sk_classify_flow(sk, flowi6_to_flowi_common(fl6));
69 }
70
71 int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr)
72 {
73         struct ip6_flowlabel *flowlabel = NULL;
74         struct in6_addr *final_p, final;
75         struct ipv6_txoptions *opt;
76         struct dst_entry *dst;
77         struct inet_sock *inet = inet_sk(sk);
78         struct ipv6_pinfo *np = inet6_sk(sk);
79         struct flowi6 fl6;
80         int err = 0;
81
82         if (np->sndflow && (np->flow_label & IPV6_FLOWLABEL_MASK)) {
83                 flowlabel = fl6_sock_lookup(sk, np->flow_label);
84                 if (IS_ERR(flowlabel))
85                         return -EINVAL;
86         }
87         ip6_datagram_flow_key_init(&fl6, sk);
88
89         rcu_read_lock();
90         opt = flowlabel ? flowlabel->opt : rcu_dereference(np->opt);
91         final_p = fl6_update_dst(&fl6, opt, &final);
92         rcu_read_unlock();
93
94         dst = ip6_dst_lookup_flow(sock_net(sk), sk, &fl6, final_p);
95         if (IS_ERR(dst)) {
96                 err = PTR_ERR(dst);
97                 goto out;
98         }
99
100         if (fix_sk_saddr) {
101                 if (ipv6_addr_any(&np->saddr))
102                         np->saddr = fl6.saddr;
103
104                 if (ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
105                         sk->sk_v6_rcv_saddr = fl6.saddr;
106                         inet->inet_rcv_saddr = LOOPBACK4_IPV6;
107                         if (sk->sk_prot->rehash)
108                                 sk->sk_prot->rehash(sk);
109                 }
110         }
111
112         ip6_sk_dst_store_flow(sk, dst, &fl6);
113
114 out:
115         fl6_sock_release(flowlabel);
116         return err;
117 }
118
119 void ip6_datagram_release_cb(struct sock *sk)
120 {
121         struct dst_entry *dst;
122
123         if (ipv6_addr_v4mapped(&sk->sk_v6_daddr))
124                 return;
125
126         rcu_read_lock();
127         dst = __sk_dst_get(sk);
128         if (!dst || !dst->obsolete ||
129             dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) {
130                 rcu_read_unlock();
131                 return;
132         }
133         rcu_read_unlock();
134
135         ip6_datagram_dst_update(sk, false);
136 }
137 EXPORT_SYMBOL_GPL(ip6_datagram_release_cb);
138
139 int __ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr,
140                            int addr_len)
141 {
142         struct sockaddr_in6     *usin = (struct sockaddr_in6 *) uaddr;
143         struct inet_sock        *inet = inet_sk(sk);
144         struct ipv6_pinfo       *np = inet6_sk(sk);
145         struct in6_addr         *daddr, old_daddr;
146         __be32                  fl6_flowlabel = 0;
147         __be32                  old_fl6_flowlabel;
148         __be16                  old_dport;
149         int                     addr_type;
150         int                     err;
151
152         if (usin->sin6_family == AF_INET) {
153                 if (ipv6_only_sock(sk))
154                         return -EAFNOSUPPORT;
155                 err = __ip4_datagram_connect(sk, uaddr, addr_len);
156                 goto ipv4_connected;
157         }
158
159         if (addr_len < SIN6_LEN_RFC2133)
160                 return -EINVAL;
161
162         if (usin->sin6_family != AF_INET6)
163                 return -EAFNOSUPPORT;
164
165         if (np->sndflow)
166                 fl6_flowlabel = usin->sin6_flowinfo & IPV6_FLOWINFO_MASK;
167
168         if (ipv6_addr_any(&usin->sin6_addr)) {
169                 /*
170                  *      connect to self
171                  */
172                 if (ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr))
173                         ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK),
174                                                &usin->sin6_addr);
175                 else
176                         usin->sin6_addr = in6addr_loopback;
177         }
178
179         addr_type = ipv6_addr_type(&usin->sin6_addr);
180
181         daddr = &usin->sin6_addr;
182
183         if (addr_type & IPV6_ADDR_MAPPED) {
184                 struct sockaddr_in sin;
185
186                 if (ipv6_only_sock(sk)) {
187                         err = -ENETUNREACH;
188                         goto out;
189                 }
190                 sin.sin_family = AF_INET;
191                 sin.sin_addr.s_addr = daddr->s6_addr32[3];
192                 sin.sin_port = usin->sin6_port;
193
194                 err = __ip4_datagram_connect(sk,
195                                              (struct sockaddr *) &sin,
196                                              sizeof(sin));
197
198 ipv4_connected:
199                 if (err)
200                         goto out;
201
202                 ipv6_addr_set_v4mapped(inet->inet_daddr, &sk->sk_v6_daddr);
203
204                 if (ipv6_addr_any(&np->saddr) ||
205                     ipv6_mapped_addr_any(&np->saddr))
206                         ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
207
208                 if (ipv6_addr_any(&sk->sk_v6_rcv_saddr) ||
209                     ipv6_mapped_addr_any(&sk->sk_v6_rcv_saddr)) {
210                         ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
211                                                &sk->sk_v6_rcv_saddr);
212                         if (sk->sk_prot->rehash)
213                                 sk->sk_prot->rehash(sk);
214                 }
215
216                 goto out;
217         }
218
219         if (__ipv6_addr_needs_scope_id(addr_type)) {
220                 if (addr_len >= sizeof(struct sockaddr_in6) &&
221                     usin->sin6_scope_id) {
222                         if (!sk_dev_equal_l3scope(sk, usin->sin6_scope_id)) {
223                                 err = -EINVAL;
224                                 goto out;
225                         }
226                         WRITE_ONCE(sk->sk_bound_dev_if, usin->sin6_scope_id);
227                 }
228
229                 if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST))
230                         WRITE_ONCE(sk->sk_bound_dev_if, np->mcast_oif);
231
232                 /* Connect to link-local address requires an interface */
233                 if (!sk->sk_bound_dev_if) {
234                         err = -EINVAL;
235                         goto out;
236                 }
237         }
238
239         /* save the current peer information before updating it */
240         old_daddr = sk->sk_v6_daddr;
241         old_fl6_flowlabel = np->flow_label;
242         old_dport = inet->inet_dport;
243
244         sk->sk_v6_daddr = *daddr;
245         np->flow_label = fl6_flowlabel;
246         inet->inet_dport = usin->sin6_port;
247
248         /*
249          *      Check for a route to destination an obtain the
250          *      destination cache for it.
251          */
252
253         err = ip6_datagram_dst_update(sk, true);
254         if (err) {
255                 /* Restore the socket peer info, to keep it consistent with
256                  * the old socket state
257                  */
258                 sk->sk_v6_daddr = old_daddr;
259                 np->flow_label = old_fl6_flowlabel;
260                 inet->inet_dport = old_dport;
261                 goto out;
262         }
263
264         reuseport_has_conns_set(sk);
265         sk->sk_state = TCP_ESTABLISHED;
266         sk_set_txhash(sk);
267 out:
268         return err;
269 }
270 EXPORT_SYMBOL_GPL(__ip6_datagram_connect);
271
272 int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
273 {
274         int res;
275
276         lock_sock(sk);
277         res = __ip6_datagram_connect(sk, uaddr, addr_len);
278         release_sock(sk);
279         return res;
280 }
281 EXPORT_SYMBOL_GPL(ip6_datagram_connect);
282
283 int ip6_datagram_connect_v6_only(struct sock *sk, struct sockaddr *uaddr,
284                                  int addr_len)
285 {
286         DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, uaddr);
287         if (sin6->sin6_family != AF_INET6)
288                 return -EAFNOSUPPORT;
289         return ip6_datagram_connect(sk, uaddr, addr_len);
290 }
291 EXPORT_SYMBOL_GPL(ip6_datagram_connect_v6_only);
292
293 static void ipv6_icmp_error_rfc4884(const struct sk_buff *skb,
294                                     struct sock_ee_data_rfc4884 *out)
295 {
296         switch (icmp6_hdr(skb)->icmp6_type) {
297         case ICMPV6_TIME_EXCEED:
298         case ICMPV6_DEST_UNREACH:
299                 ip_icmp_error_rfc4884(skb, out, sizeof(struct icmp6hdr),
300                                       icmp6_hdr(skb)->icmp6_datagram_len * 8);
301         }
302 }
303
304 void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
305                      __be16 port, u32 info, u8 *payload)
306 {
307         struct ipv6_pinfo *np  = inet6_sk(sk);
308         struct icmp6hdr *icmph = icmp6_hdr(skb);
309         struct sock_exterr_skb *serr;
310
311         if (!np->recverr)
312                 return;
313
314         skb = skb_clone(skb, GFP_ATOMIC);
315         if (!skb)
316                 return;
317
318         skb->protocol = htons(ETH_P_IPV6);
319
320         serr = SKB_EXT_ERR(skb);
321         serr->ee.ee_errno = err;
322         serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6;
323         serr->ee.ee_type = icmph->icmp6_type;
324         serr->ee.ee_code = icmph->icmp6_code;
325         serr->ee.ee_pad = 0;
326         serr->ee.ee_info = info;
327         serr->ee.ee_data = 0;
328         serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
329                                   skb_network_header(skb);
330         serr->port = port;
331
332         __skb_pull(skb, payload - skb->data);
333
334         if (inet6_sk(sk)->recverr_rfc4884)
335                 ipv6_icmp_error_rfc4884(skb, &serr->ee.ee_rfc4884);
336
337         skb_reset_transport_header(skb);
338
339         if (sock_queue_err_skb(sk, skb))
340                 kfree_skb(skb);
341 }
342
343 void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info)
344 {
345         const struct ipv6_pinfo *np = inet6_sk(sk);
346         struct sock_exterr_skb *serr;
347         struct ipv6hdr *iph;
348         struct sk_buff *skb;
349
350         if (!np->recverr)
351                 return;
352
353         skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
354         if (!skb)
355                 return;
356
357         skb->protocol = htons(ETH_P_IPV6);
358
359         skb_put(skb, sizeof(struct ipv6hdr));
360         skb_reset_network_header(skb);
361         iph = ipv6_hdr(skb);
362         iph->daddr = fl6->daddr;
363         ip6_flow_hdr(iph, 0, 0);
364
365         serr = SKB_EXT_ERR(skb);
366         serr->ee.ee_errno = err;
367         serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
368         serr->ee.ee_type = 0;
369         serr->ee.ee_code = 0;
370         serr->ee.ee_pad = 0;
371         serr->ee.ee_info = info;
372         serr->ee.ee_data = 0;
373         serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
374         serr->port = fl6->fl6_dport;
375
376         __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
377         skb_reset_transport_header(skb);
378
379         if (sock_queue_err_skb(sk, skb))
380                 kfree_skb(skb);
381 }
382
383 void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu)
384 {
385         struct ipv6_pinfo *np = inet6_sk(sk);
386         struct ipv6hdr *iph;
387         struct sk_buff *skb;
388         struct ip6_mtuinfo *mtu_info;
389
390         if (!np->rxopt.bits.rxpmtu)
391                 return;
392
393         skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
394         if (!skb)
395                 return;
396
397         skb_put(skb, sizeof(struct ipv6hdr));
398         skb_reset_network_header(skb);
399         iph = ipv6_hdr(skb);
400         iph->daddr = fl6->daddr;
401
402         mtu_info = IP6CBMTU(skb);
403
404         mtu_info->ip6m_mtu = mtu;
405         mtu_info->ip6m_addr.sin6_family = AF_INET6;
406         mtu_info->ip6m_addr.sin6_port = 0;
407         mtu_info->ip6m_addr.sin6_flowinfo = 0;
408         mtu_info->ip6m_addr.sin6_scope_id = fl6->flowi6_oif;
409         mtu_info->ip6m_addr.sin6_addr = ipv6_hdr(skb)->daddr;
410
411         __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
412         skb_reset_transport_header(skb);
413
414         skb = xchg(&np->rxpmtu, skb);
415         kfree_skb(skb);
416 }
417
418 /* For some errors we have valid addr_offset even with zero payload and
419  * zero port. Also, addr_offset should be supported if port is set.
420  */
421 static inline bool ipv6_datagram_support_addr(struct sock_exterr_skb *serr)
422 {
423         return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6 ||
424                serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
425                serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
426 }
427
428 /* IPv6 supports cmsg on all origins aside from SO_EE_ORIGIN_LOCAL.
429  *
430  * At one point, excluding local errors was a quick test to identify icmp/icmp6
431  * errors. This is no longer true, but the test remained, so the v6 stack,
432  * unlike v4, also honors cmsg requests on all wifi and timestamp errors.
433  */
434 static bool ip6_datagram_support_cmsg(struct sk_buff *skb,
435                                       struct sock_exterr_skb *serr)
436 {
437         if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
438             serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6)
439                 return true;
440
441         if (serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL)
442                 return false;
443
444         if (!IP6CB(skb)->iif)
445                 return false;
446
447         return true;
448 }
449
450 /*
451  *      Handle MSG_ERRQUEUE
452  */
453 int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
454 {
455         struct ipv6_pinfo *np = inet6_sk(sk);
456         struct sock_exterr_skb *serr;
457         struct sk_buff *skb;
458         DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
459         struct {
460                 struct sock_extended_err ee;
461                 struct sockaddr_in6      offender;
462         } errhdr;
463         int err;
464         int copied;
465
466         err = -EAGAIN;
467         skb = sock_dequeue_err_skb(sk);
468         if (!skb)
469                 goto out;
470
471         copied = skb->len;
472         if (copied > len) {
473                 msg->msg_flags |= MSG_TRUNC;
474                 copied = len;
475         }
476         err = skb_copy_datagram_msg(skb, 0, msg, copied);
477         if (unlikely(err)) {
478                 kfree_skb(skb);
479                 return err;
480         }
481         sock_recv_timestamp(msg, sk, skb);
482
483         serr = SKB_EXT_ERR(skb);
484
485         if (sin && ipv6_datagram_support_addr(serr)) {
486                 const unsigned char *nh = skb_network_header(skb);
487                 sin->sin6_family = AF_INET6;
488                 sin->sin6_flowinfo = 0;
489                 sin->sin6_port = serr->port;
490                 if (skb->protocol == htons(ETH_P_IPV6)) {
491                         const struct ipv6hdr *ip6h = container_of((struct in6_addr *)(nh + serr->addr_offset),
492                                                                   struct ipv6hdr, daddr);
493                         sin->sin6_addr = ip6h->daddr;
494                         if (np->sndflow)
495                                 sin->sin6_flowinfo = ip6_flowinfo(ip6h);
496                         sin->sin6_scope_id =
497                                 ipv6_iface_scope_id(&sin->sin6_addr,
498                                                     IP6CB(skb)->iif);
499                 } else {
500                         ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset),
501                                                &sin->sin6_addr);
502                         sin->sin6_scope_id = 0;
503                 }
504                 *addr_len = sizeof(*sin);
505         }
506
507         memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
508         sin = &errhdr.offender;
509         memset(sin, 0, sizeof(*sin));
510
511         if (ip6_datagram_support_cmsg(skb, serr)) {
512                 sin->sin6_family = AF_INET6;
513                 if (np->rxopt.all)
514                         ip6_datagram_recv_common_ctl(sk, msg, skb);
515                 if (skb->protocol == htons(ETH_P_IPV6)) {
516                         sin->sin6_addr = ipv6_hdr(skb)->saddr;
517                         if (np->rxopt.all)
518                                 ip6_datagram_recv_specific_ctl(sk, msg, skb);
519                         sin->sin6_scope_id =
520                                 ipv6_iface_scope_id(&sin->sin6_addr,
521                                                     IP6CB(skb)->iif);
522                 } else {
523                         ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
524                                                &sin->sin6_addr);
525                         if (inet_sk(sk)->cmsg_flags)
526                                 ip_cmsg_recv(msg, skb);
527                 }
528         }
529
530         put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr);
531
532         /* Now we could try to dump offended packet options */
533
534         msg->msg_flags |= MSG_ERRQUEUE;
535         err = copied;
536
537         consume_skb(skb);
538 out:
539         return err;
540 }
541 EXPORT_SYMBOL_GPL(ipv6_recv_error);
542
543 /*
544  *      Handle IPV6_RECVPATHMTU
545  */
546 int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len,
547                      int *addr_len)
548 {
549         struct ipv6_pinfo *np = inet6_sk(sk);
550         struct sk_buff *skb;
551         struct ip6_mtuinfo mtu_info;
552         DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
553         int err;
554         int copied;
555
556         err = -EAGAIN;
557         skb = xchg(&np->rxpmtu, NULL);
558         if (!skb)
559                 goto out;
560
561         copied = skb->len;
562         if (copied > len) {
563                 msg->msg_flags |= MSG_TRUNC;
564                 copied = len;
565         }
566         err = skb_copy_datagram_msg(skb, 0, msg, copied);
567         if (err)
568                 goto out_free_skb;
569
570         sock_recv_timestamp(msg, sk, skb);
571
572         memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info));
573
574         if (sin) {
575                 sin->sin6_family = AF_INET6;
576                 sin->sin6_flowinfo = 0;
577                 sin->sin6_port = 0;
578                 sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id;
579                 sin->sin6_addr = mtu_info.ip6m_addr.sin6_addr;
580                 *addr_len = sizeof(*sin);
581         }
582
583         put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info);
584
585         err = copied;
586
587 out_free_skb:
588         kfree_skb(skb);
589 out:
590         return err;
591 }
592
593
594 void ip6_datagram_recv_common_ctl(struct sock *sk, struct msghdr *msg,
595                                  struct sk_buff *skb)
596 {
597         struct ipv6_pinfo *np = inet6_sk(sk);
598         bool is_ipv6 = skb->protocol == htons(ETH_P_IPV6);
599
600         if (np->rxopt.bits.rxinfo) {
601                 struct in6_pktinfo src_info;
602
603                 if (is_ipv6) {
604                         src_info.ipi6_ifindex = IP6CB(skb)->iif;
605                         src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
606                 } else {
607                         src_info.ipi6_ifindex =
608                                 PKTINFO_SKB_CB(skb)->ipi_ifindex;
609                         ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr,
610                                                &src_info.ipi6_addr);
611                 }
612
613                 if (src_info.ipi6_ifindex >= 0)
614                         put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO,
615                                  sizeof(src_info), &src_info);
616         }
617 }
618
619 void ip6_datagram_recv_specific_ctl(struct sock *sk, struct msghdr *msg,
620                                     struct sk_buff *skb)
621 {
622         struct ipv6_pinfo *np = inet6_sk(sk);
623         struct inet6_skb_parm *opt = IP6CB(skb);
624         unsigned char *nh = skb_network_header(skb);
625
626         if (np->rxopt.bits.rxhlim) {
627                 int hlim = ipv6_hdr(skb)->hop_limit;
628                 put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
629         }
630
631         if (np->rxopt.bits.rxtclass) {
632                 int tclass = ipv6_get_dsfield(ipv6_hdr(skb));
633                 put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
634         }
635
636         if (np->rxopt.bits.rxflow) {
637                 __be32 flowinfo = ip6_flowinfo((struct ipv6hdr *)nh);
638                 if (flowinfo)
639                         put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
640         }
641
642         /* HbH is allowed only once */
643         if (np->rxopt.bits.hopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
644                 u8 *ptr = nh + sizeof(struct ipv6hdr);
645                 put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
646         }
647
648         if (opt->lastopt &&
649             (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
650                 /*
651                  * Silly enough, but we need to reparse in order to
652                  * report extension headers (except for HbH)
653                  * in order.
654                  *
655                  * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
656                  * (and WILL NOT be) defined because
657                  * IPV6_RECVDSTOPTS is more generic. --yoshfuji
658                  */
659                 unsigned int off = sizeof(struct ipv6hdr);
660                 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
661
662                 while (off <= opt->lastopt) {
663                         unsigned int len;
664                         u8 *ptr = nh + off;
665
666                         switch (nexthdr) {
667                         case IPPROTO_DSTOPTS:
668                                 nexthdr = ptr[0];
669                                 len = (ptr[1] + 1) << 3;
670                                 if (np->rxopt.bits.dstopts)
671                                         put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
672                                 break;
673                         case IPPROTO_ROUTING:
674                                 nexthdr = ptr[0];
675                                 len = (ptr[1] + 1) << 3;
676                                 if (np->rxopt.bits.srcrt)
677                                         put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
678                                 break;
679                         case IPPROTO_AH:
680                                 nexthdr = ptr[0];
681                                 len = (ptr[1] + 2) << 2;
682                                 break;
683                         default:
684                                 nexthdr = ptr[0];
685                                 len = (ptr[1] + 1) << 3;
686                                 break;
687                         }
688
689                         off += len;
690                 }
691         }
692
693         /* socket options in old style */
694         if (np->rxopt.bits.rxoinfo) {
695                 struct in6_pktinfo src_info;
696
697                 src_info.ipi6_ifindex = opt->iif;
698                 src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
699                 put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
700         }
701         if (np->rxopt.bits.rxohlim) {
702                 int hlim = ipv6_hdr(skb)->hop_limit;
703                 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
704         }
705         if (np->rxopt.bits.ohopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
706                 u8 *ptr = nh + sizeof(struct ipv6hdr);
707                 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr);
708         }
709         if (np->rxopt.bits.odstopts && opt->dst0) {
710                 u8 *ptr = nh + opt->dst0;
711                 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
712         }
713         if (np->rxopt.bits.osrcrt && opt->srcrt) {
714                 struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
715                 put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
716         }
717         if (np->rxopt.bits.odstopts && opt->dst1) {
718                 u8 *ptr = nh + opt->dst1;
719                 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
720         }
721         if (np->rxopt.bits.rxorigdstaddr) {
722                 struct sockaddr_in6 sin6;
723                 __be16 _ports[2], *ports;
724
725                 ports = skb_header_pointer(skb, skb_transport_offset(skb),
726                                            sizeof(_ports), &_ports);
727                 if (ports) {
728                         /* All current transport protocols have the port numbers in the
729                          * first four bytes of the transport header and this function is
730                          * written with this assumption in mind.
731                          */
732                         sin6.sin6_family = AF_INET6;
733                         sin6.sin6_addr = ipv6_hdr(skb)->daddr;
734                         sin6.sin6_port = ports[1];
735                         sin6.sin6_flowinfo = 0;
736                         sin6.sin6_scope_id =
737                                 ipv6_iface_scope_id(&ipv6_hdr(skb)->daddr,
738                                                     opt->iif);
739
740                         put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6);
741                 }
742         }
743         if (np->rxopt.bits.recvfragsize && opt->frag_max_size) {
744                 int val = opt->frag_max_size;
745
746                 put_cmsg(msg, SOL_IPV6, IPV6_RECVFRAGSIZE, sizeof(val), &val);
747         }
748 }
749
750 void ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
751                           struct sk_buff *skb)
752 {
753         ip6_datagram_recv_common_ctl(sk, msg, skb);
754         ip6_datagram_recv_specific_ctl(sk, msg, skb);
755 }
756 EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl);
757
758 int ip6_datagram_send_ctl(struct net *net, struct sock *sk,
759                           struct msghdr *msg, struct flowi6 *fl6,
760                           struct ipcm6_cookie *ipc6)
761 {
762         struct in6_pktinfo *src_info;
763         struct cmsghdr *cmsg;
764         struct ipv6_rt_hdr *rthdr;
765         struct ipv6_opt_hdr *hdr;
766         struct ipv6_txoptions *opt = ipc6->opt;
767         int len;
768         int err = 0;
769
770         for_each_cmsghdr(cmsg, msg) {
771                 int addr_type;
772
773                 if (!CMSG_OK(msg, cmsg)) {
774                         err = -EINVAL;
775                         goto exit_f;
776                 }
777
778                 if (cmsg->cmsg_level == SOL_SOCKET) {
779                         err = __sock_cmsg_send(sk, msg, cmsg, &ipc6->sockc);
780                         if (err)
781                                 return err;
782                         continue;
783                 }
784
785                 if (cmsg->cmsg_level != SOL_IPV6)
786                         continue;
787
788                 switch (cmsg->cmsg_type) {
789                 case IPV6_PKTINFO:
790                 case IPV6_2292PKTINFO:
791                     {
792                         struct net_device *dev = NULL;
793                         int src_idx;
794
795                         if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
796                                 err = -EINVAL;
797                                 goto exit_f;
798                         }
799
800                         src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
801                         src_idx = src_info->ipi6_ifindex;
802
803                         if (src_idx) {
804                                 if (fl6->flowi6_oif &&
805                                     src_idx != fl6->flowi6_oif &&
806                                     (READ_ONCE(sk->sk_bound_dev_if) != fl6->flowi6_oif ||
807                                      !sk_dev_equal_l3scope(sk, src_idx)))
808                                         return -EINVAL;
809                                 fl6->flowi6_oif = src_idx;
810                         }
811
812                         addr_type = __ipv6_addr_type(&src_info->ipi6_addr);
813
814                         rcu_read_lock();
815                         if (fl6->flowi6_oif) {
816                                 dev = dev_get_by_index_rcu(net, fl6->flowi6_oif);
817                                 if (!dev) {
818                                         rcu_read_unlock();
819                                         return -ENODEV;
820                                 }
821                         } else if (addr_type & IPV6_ADDR_LINKLOCAL) {
822                                 rcu_read_unlock();
823                                 return -EINVAL;
824                         }
825
826                         if (addr_type != IPV6_ADDR_ANY) {
827                                 int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL;
828                                 if (!ipv6_can_nonlocal_bind(net, inet_sk(sk)) &&
829                                     !ipv6_chk_addr_and_flags(net, &src_info->ipi6_addr,
830                                                              dev, !strict, 0,
831                                                              IFA_F_TENTATIVE) &&
832                                     !ipv6_chk_acast_addr_src(net, dev,
833                                                              &src_info->ipi6_addr))
834                                         err = -EINVAL;
835                                 else
836                                         fl6->saddr = src_info->ipi6_addr;
837                         }
838
839                         rcu_read_unlock();
840
841                         if (err)
842                                 goto exit_f;
843
844                         break;
845                     }
846
847                 case IPV6_FLOWINFO:
848                         if (cmsg->cmsg_len < CMSG_LEN(4)) {
849                                 err = -EINVAL;
850                                 goto exit_f;
851                         }
852
853                         if (fl6->flowlabel&IPV6_FLOWINFO_MASK) {
854                                 if ((fl6->flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
855                                         err = -EINVAL;
856                                         goto exit_f;
857                                 }
858                         }
859                         fl6->flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
860                         break;
861
862                 case IPV6_2292HOPOPTS:
863                 case IPV6_HOPOPTS:
864                         if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
865                                 err = -EINVAL;
866                                 goto exit_f;
867                         }
868
869                         hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
870                         len = ((hdr->hdrlen + 1) << 3);
871                         if (cmsg->cmsg_len < CMSG_LEN(len)) {
872                                 err = -EINVAL;
873                                 goto exit_f;
874                         }
875                         if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
876                                 err = -EPERM;
877                                 goto exit_f;
878                         }
879                         opt->opt_nflen += len;
880                         opt->hopopt = hdr;
881                         break;
882
883                 case IPV6_2292DSTOPTS:
884                         if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
885                                 err = -EINVAL;
886                                 goto exit_f;
887                         }
888
889                         hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
890                         len = ((hdr->hdrlen + 1) << 3);
891                         if (cmsg->cmsg_len < CMSG_LEN(len)) {
892                                 err = -EINVAL;
893                                 goto exit_f;
894                         }
895                         if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
896                                 err = -EPERM;
897                                 goto exit_f;
898                         }
899                         if (opt->dst1opt) {
900                                 err = -EINVAL;
901                                 goto exit_f;
902                         }
903                         opt->opt_flen += len;
904                         opt->dst1opt = hdr;
905                         break;
906
907                 case IPV6_DSTOPTS:
908                 case IPV6_RTHDRDSTOPTS:
909                         if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
910                                 err = -EINVAL;
911                                 goto exit_f;
912                         }
913
914                         hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
915                         len = ((hdr->hdrlen + 1) << 3);
916                         if (cmsg->cmsg_len < CMSG_LEN(len)) {
917                                 err = -EINVAL;
918                                 goto exit_f;
919                         }
920                         if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
921                                 err = -EPERM;
922                                 goto exit_f;
923                         }
924                         if (cmsg->cmsg_type == IPV6_DSTOPTS) {
925                                 opt->opt_flen += len;
926                                 opt->dst1opt = hdr;
927                         } else {
928                                 opt->opt_nflen += len;
929                                 opt->dst0opt = hdr;
930                         }
931                         break;
932
933                 case IPV6_2292RTHDR:
934                 case IPV6_RTHDR:
935                         if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
936                                 err = -EINVAL;
937                                 goto exit_f;
938                         }
939
940                         rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
941
942                         switch (rthdr->type) {
943 #if IS_ENABLED(CONFIG_IPV6_MIP6)
944                         case IPV6_SRCRT_TYPE_2:
945                                 if (rthdr->hdrlen != 2 ||
946                                     rthdr->segments_left != 1) {
947                                         err = -EINVAL;
948                                         goto exit_f;
949                                 }
950                                 break;
951 #endif
952                         default:
953                                 err = -EINVAL;
954                                 goto exit_f;
955                         }
956
957                         len = ((rthdr->hdrlen + 1) << 3);
958
959                         if (cmsg->cmsg_len < CMSG_LEN(len)) {
960                                 err = -EINVAL;
961                                 goto exit_f;
962                         }
963
964                         /* segments left must also match */
965                         if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
966                                 err = -EINVAL;
967                                 goto exit_f;
968                         }
969
970                         opt->opt_nflen += len;
971                         opt->srcrt = rthdr;
972
973                         if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
974                                 int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
975
976                                 opt->opt_nflen += dsthdrlen;
977                                 opt->dst0opt = opt->dst1opt;
978                                 opt->dst1opt = NULL;
979                                 opt->opt_flen -= dsthdrlen;
980                         }
981
982                         break;
983
984                 case IPV6_2292HOPLIMIT:
985                 case IPV6_HOPLIMIT:
986                         if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
987                                 err = -EINVAL;
988                                 goto exit_f;
989                         }
990
991                         ipc6->hlimit = *(int *)CMSG_DATA(cmsg);
992                         if (ipc6->hlimit < -1 || ipc6->hlimit > 0xff) {
993                                 err = -EINVAL;
994                                 goto exit_f;
995                         }
996
997                         break;
998
999                 case IPV6_TCLASS:
1000                     {
1001                         int tc;
1002
1003                         err = -EINVAL;
1004                         if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
1005                                 goto exit_f;
1006
1007                         tc = *(int *)CMSG_DATA(cmsg);
1008                         if (tc < -1 || tc > 0xff)
1009                                 goto exit_f;
1010
1011                         err = 0;
1012                         ipc6->tclass = tc;
1013
1014                         break;
1015                     }
1016
1017                 case IPV6_DONTFRAG:
1018                     {
1019                         int df;
1020
1021                         err = -EINVAL;
1022                         if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
1023                                 goto exit_f;
1024
1025                         df = *(int *)CMSG_DATA(cmsg);
1026                         if (df < 0 || df > 1)
1027                                 goto exit_f;
1028
1029                         err = 0;
1030                         ipc6->dontfrag = df;
1031
1032                         break;
1033                     }
1034                 default:
1035                         net_dbg_ratelimited("invalid cmsg type: %d\n",
1036                                             cmsg->cmsg_type);
1037                         err = -EINVAL;
1038                         goto exit_f;
1039                 }
1040         }
1041
1042 exit_f:
1043         return err;
1044 }
1045 EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl);
1046
1047 void __ip6_dgram_sock_seq_show(struct seq_file *seq, struct sock *sp,
1048                                __u16 srcp, __u16 destp, int rqueue, int bucket)
1049 {
1050         const struct in6_addr *dest, *src;
1051
1052         dest  = &sp->sk_v6_daddr;
1053         src   = &sp->sk_v6_rcv_saddr;
1054         seq_printf(seq,
1055                    "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1056                    "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %u\n",
1057                    bucket,
1058                    src->s6_addr32[0], src->s6_addr32[1],
1059                    src->s6_addr32[2], src->s6_addr32[3], srcp,
1060                    dest->s6_addr32[0], dest->s6_addr32[1],
1061                    dest->s6_addr32[2], dest->s6_addr32[3], destp,
1062                    sp->sk_state,
1063                    sk_wmem_alloc_get(sp),
1064                    rqueue,
1065                    0, 0L, 0,
1066                    from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)),
1067                    0,
1068                    sock_i_ino(sp),
1069                    refcount_read(&sp->sk_refcnt), sp,
1070                    atomic_read(&sp->sk_drops));
1071 }