Add if_broadcastaddr to struct ifnet to hold the link layer broadcast address.
[dragonfly.git] / sys / netinet6 / route6.c
1 /*      $FreeBSD: src/sys/netinet6/route6.c,v 1.1.2.5 2003/01/23 21:06:47 sam Exp $     */
2 /*      $DragonFly: src/sys/netinet6/route6.c,v 1.4 2004/05/20 18:30:36 cpressey Exp $  */
3 /*      $KAME: route6.c,v 1.24 2001/03/14 03:07:05 itojun Exp $ */
4
5 /*
6  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the project nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #include "opt_inet.h"
35 #include "opt_inet6.h"
36
37 #include <sys/param.h>
38 #include <sys/mbuf.h>
39 #include <sys/socket.h>
40 #include <sys/systm.h>
41 #include <sys/queue.h>
42
43 #include <net/if.h>
44
45 #include <netinet/in.h>
46 #include <netinet6/in6_var.h>
47 #include <netinet/ip6.h>
48 #include <netinet6/ip6_var.h>
49
50 #include <netinet/icmp6.h>
51
52 static int ip6_rthdr0 (struct mbuf *, struct ip6_hdr *,
53     struct ip6_rthdr0 *);
54
55 int
56 route6_input(struct mbuf **mp, int *offp, int proto) /* proto is unused */
57 {
58         struct ip6_hdr *ip6;
59         struct mbuf *m = *mp;
60         struct ip6_rthdr *rh;
61         int off = *offp, rhlen;
62         struct ip6aux *ip6a;
63
64         ip6a = ip6_findaux(m);
65         if (ip6a) {
66                 /* XXX reject home-address option before rthdr */
67                 if (ip6a->ip6a_flags & IP6A_SWAP) {
68                         ip6stat.ip6s_badoptions++;
69                         m_freem(m);
70                         return IPPROTO_DONE;
71                 }
72         }
73
74 #ifndef PULLDOWN_TEST
75         IP6_EXTHDR_CHECK(m, off, sizeof(*rh), IPPROTO_DONE);
76         ip6 = mtod(m, struct ip6_hdr *);
77         rh = (struct ip6_rthdr *)((caddr_t)ip6 + off);
78 #else
79         ip6 = mtod(m, struct ip6_hdr *);
80         IP6_EXTHDR_GET(rh, struct ip6_rthdr *, m, off, sizeof(*rh));
81         if (rh == NULL) {
82                 ip6stat.ip6s_tooshort++;
83                 return IPPROTO_DONE;
84         }
85 #endif
86
87         switch (rh->ip6r_type) {
88         case IPV6_RTHDR_TYPE_0:
89                 rhlen = (rh->ip6r_len + 1) << 3;
90 #ifndef PULLDOWN_TEST
91                 /*
92                  * note on option length:
93                  * due to IP6_EXTHDR_CHECK assumption, we cannot handle
94                  * very big routing header (max rhlen == 2048).
95                  */
96                 IP6_EXTHDR_CHECK(m, off, rhlen, IPPROTO_DONE);
97 #else
98                 /*
99                  * note on option length:
100                  * maximum rhlen: 2048
101                  * max mbuf m_pulldown can handle: MCLBYTES == usually 2048
102                  * so, here we are assuming that m_pulldown can handle
103                  * rhlen == 2048 case.  this may not be a good thing to
104                  * assume - we may want to avoid pulling it up altogether.
105                  */
106                 IP6_EXTHDR_GET(rh, struct ip6_rthdr *, m, off, rhlen);
107                 if (rh == NULL) {
108                         ip6stat.ip6s_tooshort++;
109                         return IPPROTO_DONE;
110                 }
111 #endif
112                 if (ip6_rthdr0(m, ip6, (struct ip6_rthdr0 *)rh))
113                         return(IPPROTO_DONE);
114                 break;
115         default:
116                 /* unknown routing type */
117                 if (rh->ip6r_segleft == 0) {
118                         rhlen = (rh->ip6r_len + 1) << 3;
119                         break;  /* Final dst. Just ignore the header. */
120                 }
121                 ip6stat.ip6s_badoptions++;
122                 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
123                             (caddr_t)&rh->ip6r_type - (caddr_t)ip6);
124                 return(IPPROTO_DONE);
125         }
126
127         *offp += rhlen;
128         return(rh->ip6r_nxt);
129 }
130
131 /*
132  * Type0 routing header processing
133  *
134  * RFC2292 backward compatibility warning: no support for strict/loose bitmap,
135  * as it was dropped between RFC1883 and RFC2460.
136  */
137 static int
138 ip6_rthdr0(struct mbuf *m, struct ip6_hdr *ip6, struct ip6_rthdr0 *rh0)
139 {
140         int addrs, index;
141         struct in6_addr *nextaddr, tmpaddr;
142
143         if (rh0->ip6r0_segleft == 0)
144                 return(0);
145
146         if (rh0->ip6r0_len % 2
147 #ifdef COMPAT_RFC1883
148             || rh0->ip6r0_len > 46
149 #endif
150                 ) {
151                 /*
152                  * Type 0 routing header can't contain more than 23 addresses.
153                  * RFC 2462: this limitation was removed since strict/loose
154                  * bitmap field was deleted.
155                  */
156                 ip6stat.ip6s_badoptions++;
157                 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
158                             (caddr_t)&rh0->ip6r0_len - (caddr_t)ip6);
159                 return(-1);
160         }
161
162         if ((addrs = rh0->ip6r0_len / 2) < rh0->ip6r0_segleft) {
163                 ip6stat.ip6s_badoptions++;
164                 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
165                             (caddr_t)&rh0->ip6r0_segleft - (caddr_t)ip6);
166                 return(-1);
167         }
168
169         index = addrs - rh0->ip6r0_segleft;
170         rh0->ip6r0_segleft--;
171         /* note that ip6r0_addr does not exist in RFC2292bis */
172         nextaddr = rh0->ip6r0_addr + index;
173
174         /*
175          * reject invalid addresses.  be proactive about malicious use of
176          * IPv4 mapped/compat address.
177          * XXX need more checks?
178          */
179         if (IN6_IS_ADDR_MULTICAST(nextaddr) ||
180             IN6_IS_ADDR_UNSPECIFIED(nextaddr) ||
181             IN6_IS_ADDR_V4MAPPED(nextaddr) ||
182             IN6_IS_ADDR_V4COMPAT(nextaddr)) {
183                 ip6stat.ip6s_badoptions++;
184                 m_freem(m);
185                 return(-1);
186         }
187         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
188             IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst) ||
189             IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst) ||
190             IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
191                 ip6stat.ip6s_badoptions++;
192                 m_freem(m);
193                 return(-1);
194         }
195
196         /*
197          * Swap the IPv6 destination address and nextaddr. Forward the packet.
198          */
199         tmpaddr = *nextaddr;
200         *nextaddr = ip6->ip6_dst;
201         if (IN6_IS_ADDR_LINKLOCAL(nextaddr))
202                 nextaddr->s6_addr16[1] = 0;
203         ip6->ip6_dst = tmpaddr;
204         if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst))
205                 ip6->ip6_dst.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
206
207 #ifdef COMPAT_RFC1883
208         if (rh0->ip6r0_slmap[index / 8] & (1 << (7 - (index % 8))))
209                 ip6_forward(m, IPV6_SRCRT_NEIGHBOR);
210         else
211                 ip6_forward(m, IPV6_SRCRT_NOTNEIGHBOR);
212 #else
213         ip6_forward(m, 1);
214 #endif
215
216         return(-1);                     /* m would be freed in ip6_forward() */
217 }