ip6_forward.c revision 1.2.2.3 1 /* $NetBSD: ip6_forward.c,v 1.2.2.3 1999/08/02 22:36:05 thorpej Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/malloc.h>
35 #include <sys/mbuf.h>
36 #include <sys/domain.h>
37 #include <sys/protosw.h>
38 #include <sys/socket.h>
39 #include <sys/errno.h>
40 #include <sys/time.h>
41 #include <sys/kernel.h>
42 #include <sys/syslog.h>
43
44 #include <net/if.h>
45 #include <net/route.h>
46
47 #include <netinet/in.h>
48 #include <netinet/in_var.h>
49 #include <netinet6/ip6.h>
50 #if !defined(__FreeBSD__) || __FreeBSD__ < 3
51 #include <netinet6/in6_pcb.h>
52 #endif
53 #include <netinet6/ip6_var.h>
54 #include <netinet6/icmp6.h>
55
56 struct route_in6 ip6_forward_rt;
57
58 /*
59 * Forward a packet. If some error occurs return the sender
60 * an icmp packet. Note we can't always generate a meaningful
61 * icmp message because icmp doesn't have a large enough repertoire
62 * of codes and types.
63 *
64 * If not forwarding, just drop the packet. This could be confusing
65 * if ipforwarding was zero but some routing protocol was advancing
66 * us as a gateway to somewhere. However, we must let the routing
67 * protocol deal with that.
68 *
69 */
70
71 void
72 ip6_forward(m, srcrt)
73 struct mbuf *m;
74 int srcrt;
75 {
76 register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
77 register struct sockaddr_in6 *dst;
78 register struct rtentry *rt;
79 int error, type = 0, code = 0;
80 struct mbuf *mcopy;
81
82 if (m->m_flags & (M_BCAST|M_MCAST) ||
83 in6_canforward(&ip6->ip6_src, &ip6->ip6_dst) == 0) {
84 ip6stat.ip6s_cantforward++;
85 ip6stat.ip6s_badscope++;
86 #if !defined(__FreeBSD__) || __FreeBSD__ < 3
87 if (ip6_log_time + ip6_log_interval < time.tv_sec) {
88 ip6_log_time = time.tv_sec;
89 #else
90 if (ip6_log_time + ip6_log_interval < time_second) {
91 ip6_log_time = time_second;
92 #endif
93 log(LOG_DEBUG,
94 "cannot forward "
95 "from %s to %s nxt %d received on %s\n",
96 ip6_sprintf(&ip6->ip6_src), /* XXX meaningless */
97 ip6_sprintf(&ip6->ip6_dst),
98 ip6->ip6_nxt,
99 m->m_pkthdr.rcvif->if_xname);
100 }
101 m_freem(m);
102 return;
103 }
104
105 if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
106 icmp6_error(m, ICMP6_TIME_EXCEEDED,
107 ICMP6_TIME_EXCEED_TRANSIT, 0);
108 return;
109 }
110 ip6->ip6_hlim -= IPV6_HLIMDEC;
111
112 dst = &ip6_forward_rt.ro_dst;
113 if (!srcrt) {
114 /*
115 * ip6_forward_rt.ro_dst.sin6_addr is equal to ip6->ip6_dst
116 */
117 if (ip6_forward_rt.ro_rt == 0 ||
118 (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) == 0) {
119 if (ip6_forward_rt.ro_rt) {
120 RTFREE(ip6_forward_rt.ro_rt);
121 ip6_forward_rt.ro_rt = 0;
122 }
123 /* this probably fails but give it a try again */
124 #ifdef __NetBSD__
125 rtalloc((struct route *)&ip6_forward_rt);
126 #else
127 rtalloc_ign((struct route *)&ip6_forward_rt,
128 RTF_PRCLONING);
129 #endif
130 }
131
132 if (ip6_forward_rt.ro_rt == 0) {
133 ip6stat.ip6s_noroute++;
134 icmp6_error(m, ICMP6_DST_UNREACH,
135 ICMP6_DST_UNREACH_NOROUTE, 0);
136 return;
137 }
138 } else if ((rt = ip6_forward_rt.ro_rt) == 0 ||
139 !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst->sin6_addr)) {
140 if (ip6_forward_rt.ro_rt) {
141 RTFREE(ip6_forward_rt.ro_rt);
142 ip6_forward_rt.ro_rt = 0;
143 }
144 bzero(dst, sizeof(*dst));
145 dst->sin6_len = sizeof(struct sockaddr_in6);
146 dst->sin6_family = AF_INET6;
147 dst->sin6_addr = ip6->ip6_dst;
148
149 #ifdef __NetBSD__
150 rtalloc((struct route *)&ip6_forward_rt);
151 #else
152 rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING);
153 #endif
154 if (ip6_forward_rt.ro_rt == 0) {
155 ip6stat.ip6s_noroute++;
156 icmp6_error(m, ICMP6_DST_UNREACH,
157 ICMP6_DST_UNREACH_NOROUTE, 0);
158 return;
159 }
160 }
161 rt = ip6_forward_rt.ro_rt;
162 if (m->m_pkthdr.len > rt->rt_ifp->if_mtu){
163 icmp6_error(m, ICMP6_PACKET_TOO_BIG, 0, rt->rt_ifp->if_mtu);
164 return;
165 }
166
167 if (rt->rt_flags & RTF_GATEWAY)
168 dst = (struct sockaddr_in6 *)rt->rt_gateway;
169 /*
170 * Save at most 528 bytes of the packet in case
171 * we need to generate an ICMP6 message to the src.
172 * Thanks to M_EXT, in most cases copy will not occur.
173 */
174 mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN));
175
176 /*
177 * If we are to forward the packet using the same interface
178 * as one we got the packet from, perhaps we should send a redirect
179 * to sender to shortcut a hop.
180 * Only send redirect if source is sending directly to us,
181 * and if packet was not source routed (or has any options).
182 * Also, don't send redirect if forwarding using a route
183 * modified by a redirect.
184 */
185 if (rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt &&
186 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0)
187 type = ND_REDIRECT;
188
189 error = (*rt->rt_ifp->if_output)(rt->rt_ifp, m,
190 (struct sockaddr *)dst,
191 ip6_forward_rt.ro_rt);
192 if (error)
193 ip6stat.ip6s_cantforward++;
194 else {
195 ip6stat.ip6s_forward++;
196 if (type)
197 ip6stat.ip6s_redirectsent++;
198 else {
199 if (mcopy)
200 goto freecopy;
201 }
202 }
203 if (mcopy == NULL)
204 return;
205
206 switch (error) {
207 case 0:
208 #if 1
209 if (type == ND_REDIRECT) {
210 icmp6_redirect_output(mcopy, rt);
211 return;
212 }
213 #endif
214 goto freecopy;
215
216 case EMSGSIZE:
217 /* xxx MTU is constant in PPP? */
218 goto freecopy;
219
220 case ENOBUFS:
221 /* Tell source to slow down like source quench in IP? */
222 goto freecopy;
223
224 case ENETUNREACH: /* shouldn't happen, checked above */
225 case EHOSTUNREACH:
226 case ENETDOWN:
227 case EHOSTDOWN:
228 default:
229 type = ICMP6_DST_UNREACH;
230 code = ICMP6_DST_UNREACH_ADDR;
231 break;
232 }
233 icmp6_error(mcopy, type, code, 0);
234 return;
235
236 freecopy:
237 m_freem(mcopy);
238 }
239