3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * $Id: datagram.c,v 1.24 2002/02/01 22:01:04 davem Exp $
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
16 #include <linux/capability.h>
17 #include <linux/errno.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/interrupt.h>
21 #include <linux/socket.h>
22 #include <linux/sockios.h>
23 #include <linux/in6.h>
24 #include <linux/ipv6.h>
25 #include <linux/route.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>
34 #include <linux/errqueue.h>
35 #include <asm/uaccess.h>
37 int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
39 struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr;
40 struct inet_sock *inet = inet_sk(sk);
41 struct ipv6_pinfo *np = inet6_sk(sk);
42 struct in6_addr *daddr, *final_p = NULL, final;
43 struct dst_entry *dst;
45 struct ip6_flowlabel *flowlabel = NULL;
49 if (usin->sin6_family == AF_INET) {
50 if (__ipv6_only_sock(sk))
52 err = ip4_datagram_connect(sk, uaddr, addr_len);
56 if (addr_len < SIN6_LEN_RFC2133)
59 if (usin->sin6_family != AF_INET6)
62 memset(&fl, 0, sizeof(fl));
64 fl.fl6_flowlabel = usin->sin6_flowinfo&IPV6_FLOWINFO_MASK;
65 if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
66 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
67 if (flowlabel == NULL)
69 ipv6_addr_copy(&usin->sin6_addr, &flowlabel->dst);
73 addr_type = ipv6_addr_type(&usin->sin6_addr);
75 if (addr_type == IPV6_ADDR_ANY) {
79 usin->sin6_addr.s6_addr[15] = 0x01;
82 daddr = &usin->sin6_addr;
84 if (addr_type == IPV6_ADDR_MAPPED) {
85 struct sockaddr_in sin;
87 if (__ipv6_only_sock(sk)) {
91 sin.sin_family = AF_INET;
92 sin.sin_addr.s_addr = daddr->s6_addr32[3];
93 sin.sin_port = usin->sin6_port;
95 err = ip4_datagram_connect(sk,
96 (struct sockaddr*) &sin,
103 ipv6_addr_set(&np->daddr, 0, 0, htonl(0x0000ffff), inet->daddr);
105 if (ipv6_addr_any(&np->saddr)) {
106 ipv6_addr_set(&np->saddr, 0, 0, htonl(0x0000ffff),
110 if (ipv6_addr_any(&np->rcv_saddr)) {
111 ipv6_addr_set(&np->rcv_saddr, 0, 0, htonl(0x0000ffff),
117 if (addr_type&IPV6_ADDR_LINKLOCAL) {
118 if (addr_len >= sizeof(struct sockaddr_in6) &&
119 usin->sin6_scope_id) {
120 if (sk->sk_bound_dev_if &&
121 sk->sk_bound_dev_if != usin->sin6_scope_id) {
125 sk->sk_bound_dev_if = usin->sin6_scope_id;
128 if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST))
129 sk->sk_bound_dev_if = np->mcast_oif;
131 /* Connect to link-local address requires an interface */
132 if (!sk->sk_bound_dev_if) {
138 ipv6_addr_copy(&np->daddr, daddr);
139 np->flow_label = fl.fl6_flowlabel;
141 inet->dport = usin->sin6_port;
144 * Check for a route to destination an obtain the
145 * destination cache for it.
148 fl.proto = sk->sk_protocol;
149 ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
150 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
151 fl.oif = sk->sk_bound_dev_if;
152 fl.fl_ip_dport = inet->dport;
153 fl.fl_ip_sport = inet->sport;
155 if (!fl.oif && (addr_type&IPV6_ADDR_MULTICAST))
156 fl.oif = np->mcast_oif;
158 security_sk_classify_flow(sk, &fl);
161 if (flowlabel->opt && flowlabel->opt->srcrt) {
162 struct rt0_hdr *rt0 = (struct rt0_hdr *) flowlabel->opt->srcrt;
163 ipv6_addr_copy(&final, &fl.fl6_dst);
164 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
167 } else if (np->opt && np->opt->srcrt) {
168 struct rt0_hdr *rt0 = (struct rt0_hdr *)np->opt->srcrt;
169 ipv6_addr_copy(&final, &fl.fl6_dst);
170 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
174 err = ip6_dst_lookup(sk, &dst, &fl);
178 ipv6_addr_copy(&fl.fl6_dst, final_p);
180 if ((err = __xfrm_lookup(&dst, &fl, sk, XFRM_LOOKUP_WAIT)) < 0) {
182 err = ip6_dst_blackhole(sk, &dst, &fl);
187 /* source address lookup done in ip6_dst_lookup */
189 if (ipv6_addr_any(&np->saddr))
190 ipv6_addr_copy(&np->saddr, &fl.fl6_src);
192 if (ipv6_addr_any(&np->rcv_saddr)) {
193 ipv6_addr_copy(&np->rcv_saddr, &fl.fl6_src);
194 inet->rcv_saddr = LOOPBACK4_IPV6;
197 ip6_dst_store(sk, dst,
198 ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ?
200 #ifdef CONFIG_IPV6_SUBTREES
201 ipv6_addr_equal(&fl.fl6_src, &np->saddr) ?
206 sk->sk_state = TCP_ESTABLISHED;
208 fl6_sock_release(flowlabel);
212 void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
213 __be16 port, u32 info, u8 *payload)
215 struct ipv6_pinfo *np = inet6_sk(sk);
216 struct icmp6hdr *icmph = icmp6_hdr(skb);
217 struct sock_exterr_skb *serr;
222 skb = skb_clone(skb, GFP_ATOMIC);
226 serr = SKB_EXT_ERR(skb);
227 serr->ee.ee_errno = err;
228 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6;
229 serr->ee.ee_type = icmph->icmp6_type;
230 serr->ee.ee_code = icmph->icmp6_code;
232 serr->ee.ee_info = info;
233 serr->ee.ee_data = 0;
234 serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
235 skb_network_header(skb);
238 __skb_pull(skb, payload - skb->data);
239 skb_reset_transport_header(skb);
241 if (sock_queue_err_skb(sk, skb))
245 void ipv6_local_error(struct sock *sk, int err, struct flowi *fl, u32 info)
247 struct ipv6_pinfo *np = inet6_sk(sk);
248 struct sock_exterr_skb *serr;
255 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
259 skb_put(skb, sizeof(struct ipv6hdr));
260 skb_reset_network_header(skb);
262 ipv6_addr_copy(&iph->daddr, &fl->fl6_dst);
264 serr = SKB_EXT_ERR(skb);
265 serr->ee.ee_errno = err;
266 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
267 serr->ee.ee_type = 0;
268 serr->ee.ee_code = 0;
270 serr->ee.ee_info = info;
271 serr->ee.ee_data = 0;
272 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
273 serr->port = fl->fl_ip_dport;
275 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
276 skb_reset_transport_header(skb);
278 if (sock_queue_err_skb(sk, skb))
283 * Handle MSG_ERRQUEUE
285 int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len)
287 struct ipv6_pinfo *np = inet6_sk(sk);
288 struct sock_exterr_skb *serr;
289 struct sk_buff *skb, *skb2;
290 struct sockaddr_in6 *sin;
292 struct sock_extended_err ee;
293 struct sockaddr_in6 offender;
299 skb = skb_dequeue(&sk->sk_error_queue);
305 msg->msg_flags |= MSG_TRUNC;
308 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
312 sock_recv_timestamp(msg, sk, skb);
314 serr = SKB_EXT_ERR(skb);
316 sin = (struct sockaddr_in6 *)msg->msg_name;
318 const unsigned char *nh = skb_network_header(skb);
319 sin->sin6_family = AF_INET6;
320 sin->sin6_flowinfo = 0;
321 sin->sin6_port = serr->port;
322 sin->sin6_scope_id = 0;
323 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6) {
324 ipv6_addr_copy(&sin->sin6_addr,
325 (struct in6_addr *)(nh + serr->addr_offset));
328 (*(__be32 *)(nh + serr->addr_offset - 24) &
330 if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
331 sin->sin6_scope_id = IP6CB(skb)->iif;
333 ipv6_addr_set(&sin->sin6_addr, 0, 0,
335 *(__be32 *)(nh + serr->addr_offset));
339 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
340 sin = &errhdr.offender;
341 sin->sin6_family = AF_UNSPEC;
342 if (serr->ee.ee_origin != SO_EE_ORIGIN_LOCAL) {
343 sin->sin6_family = AF_INET6;
344 sin->sin6_flowinfo = 0;
345 sin->sin6_scope_id = 0;
346 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6) {
347 ipv6_addr_copy(&sin->sin6_addr, &ipv6_hdr(skb)->saddr);
349 datagram_recv_ctl(sk, msg, skb);
350 if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
351 sin->sin6_scope_id = IP6CB(skb)->iif;
353 struct inet_sock *inet = inet_sk(sk);
355 ipv6_addr_set(&sin->sin6_addr, 0, 0,
356 htonl(0xffff), ip_hdr(skb)->saddr);
357 if (inet->cmsg_flags)
358 ip_cmsg_recv(msg, skb);
362 put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr);
364 /* Now we could try to dump offended packet options */
366 msg->msg_flags |= MSG_ERRQUEUE;
369 /* Reset and regenerate socket error */
370 spin_lock_bh(&sk->sk_error_queue.lock);
372 if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
373 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
374 spin_unlock_bh(&sk->sk_error_queue.lock);
375 sk->sk_error_report(sk);
377 spin_unlock_bh(&sk->sk_error_queue.lock);
388 int datagram_recv_ctl(struct sock *sk, struct msghdr *msg, struct sk_buff *skb)
390 struct ipv6_pinfo *np = inet6_sk(sk);
391 struct inet6_skb_parm *opt = IP6CB(skb);
392 unsigned char *nh = skb_network_header(skb);
394 if (np->rxopt.bits.rxinfo) {
395 struct in6_pktinfo src_info;
397 src_info.ipi6_ifindex = opt->iif;
398 ipv6_addr_copy(&src_info.ipi6_addr, &ipv6_hdr(skb)->daddr);
399 put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
402 if (np->rxopt.bits.rxhlim) {
403 int hlim = ipv6_hdr(skb)->hop_limit;
404 put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
407 if (np->rxopt.bits.rxtclass) {
408 int tclass = (ntohl(*(__be32 *)ipv6_hdr(skb)) >> 20) & 0xff;
409 put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
412 if (np->rxopt.bits.rxflow && (*(__be32 *)nh & IPV6_FLOWINFO_MASK)) {
413 __be32 flowinfo = *(__be32 *)nh & IPV6_FLOWINFO_MASK;
414 put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
417 /* HbH is allowed only once */
418 if (np->rxopt.bits.hopopts && opt->hop) {
419 u8 *ptr = nh + opt->hop;
420 put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
424 (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
426 * Silly enough, but we need to reparse in order to
427 * report extension headers (except for HbH)
430 * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
431 * (and WILL NOT be) defined because
432 * IPV6_RECVDSTOPTS is more generic. --yoshfuji
434 unsigned int off = sizeof(struct ipv6hdr);
435 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
437 while (off <= opt->lastopt) {
442 case IPPROTO_DSTOPTS:
444 len = (ptr[1] + 1) << 3;
445 if (np->rxopt.bits.dstopts)
446 put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
448 case IPPROTO_ROUTING:
450 len = (ptr[1] + 1) << 3;
451 if (np->rxopt.bits.srcrt)
452 put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
456 len = (ptr[1] + 2) << 2;
460 len = (ptr[1] + 1) << 3;
468 /* socket options in old style */
469 if (np->rxopt.bits.rxoinfo) {
470 struct in6_pktinfo src_info;
472 src_info.ipi6_ifindex = opt->iif;
473 ipv6_addr_copy(&src_info.ipi6_addr, &ipv6_hdr(skb)->daddr);
474 put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
476 if (np->rxopt.bits.rxohlim) {
477 int hlim = ipv6_hdr(skb)->hop_limit;
478 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
480 if (np->rxopt.bits.ohopopts && opt->hop) {
481 u8 *ptr = nh + opt->hop;
482 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr);
484 if (np->rxopt.bits.odstopts && opt->dst0) {
485 u8 *ptr = nh + opt->dst0;
486 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
488 if (np->rxopt.bits.osrcrt && opt->srcrt) {
489 struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
490 put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
492 if (np->rxopt.bits.odstopts && opt->dst1) {
493 u8 *ptr = nh + opt->dst1;
494 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
499 int datagram_send_ctl(struct net *net,
500 struct msghdr *msg, struct flowi *fl,
501 struct ipv6_txoptions *opt,
502 int *hlimit, int *tclass)
504 struct in6_pktinfo *src_info;
505 struct cmsghdr *cmsg;
506 struct ipv6_rt_hdr *rthdr;
507 struct ipv6_opt_hdr *hdr;
511 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
514 if (!CMSG_OK(msg, cmsg)) {
519 if (cmsg->cmsg_level != SOL_IPV6)
522 switch (cmsg->cmsg_type) {
524 case IPV6_2292PKTINFO:
526 struct net_device *dev = NULL;
528 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
533 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
535 if (src_info->ipi6_ifindex) {
536 if (fl->oif && src_info->ipi6_ifindex != fl->oif)
538 fl->oif = src_info->ipi6_ifindex;
541 addr_type = __ipv6_addr_type(&src_info->ipi6_addr);
544 dev = dev_get_by_index(net, fl->oif);
547 } else if (addr_type & IPV6_ADDR_LINKLOCAL)
550 if (addr_type != IPV6_ADDR_ANY) {
551 int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL;
552 if (!ipv6_chk_addr(net, &src_info->ipi6_addr,
553 strict ? dev : NULL, 0))
556 ipv6_addr_copy(&fl->fl6_src, &src_info->ipi6_addr);
569 if (cmsg->cmsg_len < CMSG_LEN(4)) {
574 if (fl->fl6_flowlabel&IPV6_FLOWINFO_MASK) {
575 if ((fl->fl6_flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
580 fl->fl6_flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
583 case IPV6_2292HOPOPTS:
585 if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
590 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
591 len = ((hdr->hdrlen + 1) << 3);
592 if (cmsg->cmsg_len < CMSG_LEN(len)) {
596 if (!capable(CAP_NET_RAW)) {
600 opt->opt_nflen += len;
604 case IPV6_2292DSTOPTS:
605 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
610 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
611 len = ((hdr->hdrlen + 1) << 3);
612 if (cmsg->cmsg_len < CMSG_LEN(len)) {
616 if (!capable(CAP_NET_RAW)) {
624 opt->opt_flen += len;
629 case IPV6_RTHDRDSTOPTS:
630 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
635 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
636 len = ((hdr->hdrlen + 1) << 3);
637 if (cmsg->cmsg_len < CMSG_LEN(len)) {
641 if (!capable(CAP_NET_RAW)) {
645 if (cmsg->cmsg_type == IPV6_DSTOPTS) {
646 opt->opt_flen += len;
649 opt->opt_nflen += len;
656 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
661 rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
663 switch (rthdr->type) {
664 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
665 case IPV6_SRCRT_TYPE_2:
673 len = ((rthdr->hdrlen + 1) << 3);
675 if (cmsg->cmsg_len < CMSG_LEN(len)) {
680 /* segments left must also match */
681 if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
686 opt->opt_nflen += len;
689 if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
690 int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
692 opt->opt_nflen += dsthdrlen;
693 opt->dst0opt = opt->dst1opt;
695 opt->opt_flen -= dsthdrlen;
700 case IPV6_2292HOPLIMIT:
702 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
707 *hlimit = *(int *)CMSG_DATA(cmsg);
708 if (*hlimit < -1 || *hlimit > 0xff) {
720 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
724 tc = *(int *)CMSG_DATA(cmsg);
725 if (tc < -1 || tc > 0xff)
734 LIMIT_NETDEBUG(KERN_DEBUG "invalid cmsg type: %d\n",