]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/route6.c
This commit was generated by cvs2svn to compensate for changes in r57844,
[FreeBSD/FreeBSD.git] / sys / netinet6 / route6.c
1 /*
2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the project nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31
32 #include <sys/param.h>
33 #include <sys/mbuf.h>
34 #include <sys/socket.h>
35
36 #include <net/if.h>
37
38 #include <netinet/in.h>
39 #include <netinet6/in6.h>
40 #include <netinet6/in6_var.h>
41 #include <netinet6/ip6.h>
42 #include <netinet6/ip6_var.h>
43
44 #include <netinet/icmp6.h>
45
46 static int      ip6_rthdr0 __P((struct mbuf *, struct ip6_hdr *, struct ip6_rthdr0 *));
47
48 int
49 route6_input(mp, offp, proto)
50         struct mbuf **mp;
51         int *offp, proto;       /* proto is unused */
52 {
53         register struct ip6_hdr *ip6;
54         register struct mbuf *m = *mp;
55         register struct ip6_rthdr *rh;
56         int off = *offp, rhlen;
57
58         IP6_EXTHDR_CHECK(m, off, sizeof(*rh), IPPROTO_DONE);
59         ip6 = mtod(m, struct ip6_hdr *);
60         rh = (struct ip6_rthdr *)((caddr_t)ip6 + off);
61
62         switch(rh->ip6r_type) {
63          case IPV6_RTHDR_TYPE_0:
64                  rhlen = (rh->ip6r_len + 1) << 3;
65                  IP6_EXTHDR_CHECK(m, off, rhlen, IPPROTO_DONE);
66                  if (ip6_rthdr0(m, ip6, (struct ip6_rthdr0 *)rh))
67                          return(IPPROTO_DONE);
68                  break;
69          default:
70                  /* unknown routing type */
71                  if (rh->ip6r_segleft == 0) {
72                          rhlen = (rh->ip6r_len + 1) << 3;
73                          break; /* Final dst. Just ignore the header. */
74                  }
75                  ip6stat.ip6s_badoptions++;
76                  icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
77                              (caddr_t)&rh->ip6r_type - (caddr_t)ip6);
78                  return(IPPROTO_DONE);
79         }
80
81         *offp += rhlen;
82         return(rh->ip6r_nxt);
83 }
84
85 /*
86  * Type0 routing header processing
87  */
88 static int
89 ip6_rthdr0(m, ip6, rh0)
90         struct mbuf *m;
91         struct ip6_hdr *ip6;
92         struct ip6_rthdr0 *rh0;
93 {
94         int addrs, index;
95         struct in6_addr *nextaddr, tmpaddr;
96
97         if (rh0->ip6r0_segleft == 0)
98                 return(0);
99
100         if (rh0->ip6r0_len % 2
101 #ifdef COMPAT_RFC1883
102             || rh0->ip6r0_len > 46
103 #endif
104                 ) {
105                 /*
106                  * Type 0 routing header can't contain more than 23 addresses.
107                  * RFC 2462: this limitation was removed since stict/loose
108                  * bitmap field was deleted.
109                  */
110                 ip6stat.ip6s_badoptions++;
111                 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
112                             (caddr_t)&rh0->ip6r0_len - (caddr_t)ip6);
113                 return(-1);
114         }
115
116         if ((addrs = rh0->ip6r0_len / 2) < rh0->ip6r0_segleft) {
117                 ip6stat.ip6s_badoptions++;
118                 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
119                             (caddr_t)&rh0->ip6r0_segleft - (caddr_t)ip6);
120                 return(-1);
121         }
122
123         index = addrs - rh0->ip6r0_segleft;
124         rh0->ip6r0_segleft--;
125         nextaddr = rh0->ip6r0_addr + index;
126
127         if (IN6_IS_ADDR_MULTICAST(nextaddr) ||
128             IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
129                 ip6stat.ip6s_badoptions++;
130                 m_freem(m);
131                 return(-1);
132         }
133
134         /*
135          * Swap the IPv6 destination address and nextaddr. Forward the packet.
136          */
137         tmpaddr = *nextaddr;
138         *nextaddr = ip6->ip6_dst;
139         if (IN6_IS_ADDR_LINKLOCAL(nextaddr))
140                 nextaddr->s6_addr16[1] = 0;
141         ip6->ip6_dst = tmpaddr;
142         if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst))
143                 ip6->ip6_dst.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
144
145 #ifdef COMPAT_RFC1883
146         if (rh0->ip6r0_slmap[index / 8] & (1 << (7 - (index % 8))))
147                 ip6_forward(m, IPV6_SRCRT_NEIGHBOR);
148         else
149                 ip6_forward(m, IPV6_SRCRT_NOTNEIGHBOR);
150 #else
151         ip6_forward(m, 1);
152 #endif
153
154         return(-1);                     /* m would be freed in ip6_forward() */
155 }