~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ipv6/datagram.c

Version: ~ [ linux-5.11 ] ~ [ linux-5.10.17 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.99 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.176 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.221 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.257 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.257 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp