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

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

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

~ [ 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