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 "opt_ipsec.h" 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/mbuf.h> 37 #include <sys/domain.h> 38 #include <sys/protosw.h> 39 #include <sys/socket.h> 40 #include <sys/errno.h> 41 #include <sys/time.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 #include <netinet6/ip6_var.h> 51 #include <netinet6/icmp6.h> 52 #include <netinet6/nd6.h> 53 54 #ifdef IPSEC_IPV6FWD 55 #include <netinet6/ipsec.h> 56 #include <netinet6/ipsec6.h> 57 #include <netkey/key.h> 58 #ifdef IPSEC_DEBUG 59 #include <netkey/key_debug.h> 60 #else 61 #define KEYDEBUG(lev,arg) 62 #endif 63 #endif /* IPSEC_IPV6FWD */ 64 65 #ifdef IPV6FIREWALL 66 #include <netinet6/ip6_fw.h> 67 #endif 68 69 #include <net/net_osdep.h> 70 71 struct route_in6 ip6_forward_rt; 72 73 /* 74 * Forward a packet. If some error occurs return the sender 75 * an icmp packet. Note we can't always generate a meaningful 76 * icmp message because icmp doesn't have a large enough repertoire 77 * of codes and types. 78 * 79 * If not forwarding, just drop the packet. This could be confusing 80 * if ipforwarding was zero but some routing protocol was advancing 81 * us as a gateway to somewhere. However, we must let the routing 82 * protocol deal with that. 83 * 84 */ 85 86 void 87 ip6_forward(m, srcrt) 88 struct mbuf *m; 89 int srcrt; 90 { 91 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 92 register struct sockaddr_in6 *dst; 93 register struct rtentry *rt; 94 int error, type = 0, code = 0; 95 struct mbuf *mcopy; 96 #ifdef IPSEC_IPV6FWD 97 struct secpolicy *sp = NULL; 98 #endif 99 100 #ifdef IPSEC_IPV6FWD 101 /* 102 * Check AH/ESP integrity. 103 */ 104 /* 105 * Don't increment ip6s_cantforward because this is the check 106 * before forwarding packet actually. 107 */ 108 if (ipsec6_in_reject(m, NULL)) { 109 ipsec6stat.in_polvio++; 110 m_freem(m); 111 return; 112 } 113 #endif /*IPSEC_IPV6FWD*/ 114 115 if (m->m_flags & (M_BCAST|M_MCAST) || 116 in6_canforward(&ip6->ip6_src, &ip6->ip6_dst) == 0) { 117 ip6stat.ip6s_cantforward++; 118 ip6stat.ip6s_badscope++; 119 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */ 120 if (ip6_log_time + ip6_log_interval < time_second) { 121 char addr[INET6_ADDRSTRLEN]; 122 ip6_log_time = time_second; 123 strncpy(addr, ip6_sprintf(&ip6->ip6_src), sizeof(addr)); 124 log(LOG_DEBUG, 125 "cannot forward " 126 "from %s to %s nxt %d received on %s\n", 127 addr, ip6_sprintf(&ip6->ip6_dst), 128 ip6->ip6_nxt, 129 if_name(m->m_pkthdr.rcvif)); 130 } 131 m_freem(m); 132 return; 133 } 134 135 if (ip6->ip6_hlim <= IPV6_HLIMDEC) { 136 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */ 137 icmp6_error(m, ICMP6_TIME_EXCEEDED, 138 ICMP6_TIME_EXCEED_TRANSIT, 0); 139 return; 140 } 141 ip6->ip6_hlim -= IPV6_HLIMDEC; 142 143 #ifdef IPSEC_IPV6FWD 144 /* get a security policy for this packet */ 145 sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error); 146 if (sp == NULL) { 147 ipsec6stat.out_inval++; 148 ip6stat.ip6s_cantforward++; 149 /* XXX: any icmp ? */ 150 m_freem(m); 151 return; 152 } 153 154 error = 0; 155 156 /* check policy */ 157 switch (sp->policy) { 158 case IPSEC_POLICY_DISCARD: 159 /* 160 * This packet is just discarded. 161 */ 162 ipsec6stat.out_polvio++; 163 ip6stat.ip6s_cantforward++; 164 key_freesp(sp); 165 /* XXX: any icmp ? */ 166 m_freem(m); 167 return; 168 169 case IPSEC_POLICY_BYPASS: 170 case IPSEC_POLICY_NONE: 171 /* no need to do IPsec. */ 172 key_freesp(sp); 173 goto skip_ipsec; 174 175 case IPSEC_POLICY_IPSEC: 176 if (sp->req == NULL) { 177 /* XXX should be panic ? */ 178 printf("ip6_forward: No IPsec request specified.\n"); 179 ip6stat.ip6s_cantforward++; 180 key_freesp(sp); 181 /* XXX: any icmp ? */ 182 m_freem(m); 183 return; 184 } 185 /* do IPsec */ 186 break; 187 188 case IPSEC_POLICY_ENTRUST: 189 default: 190 /* should be panic ?? */ 191 printf("ip6_forward: Invalid policy found. %d\n", sp->policy); 192 key_freesp(sp); 193 goto skip_ipsec; 194 } 195 196 { 197 struct ipsec_output_state state; 198 199 /* 200 * All the extension headers will become inaccessible 201 * (since they can be encrypted). 202 * Don't panic, we need no more updates to extension headers 203 * on inner IPv6 packet (since they are now encapsulated). 204 * 205 * IPv6 [ESP|AH] IPv6 [extension headers] payload 206 */ 207 bzero(&state, sizeof(state)); 208 state.m = m; 209 state.ro = NULL; /* update at ipsec6_output_tunnel() */ 210 state.dst = NULL; /* update at ipsec6_output_tunnel() */ 211 212 error = ipsec6_output_tunnel(&state, sp, 0); 213 214 m = state.m; 215 /* XXX allocate a route (ro, dst) again later */ 216 key_freesp(sp); 217 218 if (error) { 219 /* mbuf is already reclaimed in ipsec6_output_tunnel. */ 220 switch (error) { 221 case EHOSTUNREACH: 222 case ENETUNREACH: 223 case EMSGSIZE: 224 case ENOBUFS: 225 case ENOMEM: 226 break; 227 default: 228 printf("ip6_output (ipsec): error code %d\n", error); 229 /*fall through*/ 230 case ENOENT: 231 /* don't show these error codes to the user */ 232 break; 233 } 234 ip6stat.ip6s_cantforward++; 235 /* XXX: any icmp ? */ 236 m_freem(m); 237 return; 238 } 239 } 240 skip_ipsec: 241 #endif /* IPSEC_IPV6FWD */ 242 243 dst = &ip6_forward_rt.ro_dst; 244 if (!srcrt) { 245 /* 246 * ip6_forward_rt.ro_dst.sin6_addr is equal to ip6->ip6_dst 247 */ 248 if (ip6_forward_rt.ro_rt == 0 || 249 (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) == 0) { 250 if (ip6_forward_rt.ro_rt) { 251 RTFREE(ip6_forward_rt.ro_rt); 252 ip6_forward_rt.ro_rt = 0; 253 } 254 /* this probably fails but give it a try again */ 255 rtalloc_ign((struct route *)&ip6_forward_rt, 256 RTF_PRCLONING); 257 } 258 259 if (ip6_forward_rt.ro_rt == 0) { 260 ip6stat.ip6s_noroute++; 261 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */ 262 icmp6_error(m, ICMP6_DST_UNREACH, 263 ICMP6_DST_UNREACH_NOROUTE, 0); 264 return; 265 } 266 } else if ((rt = ip6_forward_rt.ro_rt) == 0 || 267 !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst->sin6_addr)) { 268 if (ip6_forward_rt.ro_rt) { 269 RTFREE(ip6_forward_rt.ro_rt); 270 ip6_forward_rt.ro_rt = 0; 271 } 272 bzero(dst, sizeof(*dst)); 273 dst->sin6_len = sizeof(struct sockaddr_in6); 274 dst->sin6_family = AF_INET6; 275 dst->sin6_addr = ip6->ip6_dst; 276 277 rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING); 278 if (ip6_forward_rt.ro_rt == 0) { 279 ip6stat.ip6s_noroute++; 280 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */ 281 icmp6_error(m, ICMP6_DST_UNREACH, 282 ICMP6_DST_UNREACH_NOROUTE, 0); 283 return; 284 } 285 } 286 rt = ip6_forward_rt.ro_rt; 287 if (m->m_pkthdr.len > rt->rt_ifp->if_mtu){ 288 in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig); 289 icmp6_error(m, ICMP6_PACKET_TOO_BIG, 0, rt->rt_ifp->if_mtu); 290 return; 291 } 292 293 if (rt->rt_flags & RTF_GATEWAY) 294 dst = (struct sockaddr_in6 *)rt->rt_gateway; 295 /* 296 * Save at most 528 bytes of the packet in case 297 * we need to generate an ICMP6 message to the src. 298 * Thanks to M_EXT, in most cases copy will not occur. 299 */ 300 mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN)); 301 302 /* 303 * If we are to forward the packet using the same interface 304 * as one we got the packet from, perhaps we should send a redirect 305 * to sender to shortcut a hop. 306 * Only send redirect if source is sending directly to us, 307 * and if packet was not source routed (or has any options). 308 * Also, don't send redirect if forwarding using a route 309 * modified by a redirect. 310 */ 311 if (rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt && 312 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) 313 type = ND_REDIRECT; 314 315 #ifdef IPV6FIREWALL 316 /* 317 * Check with the firewall... 318 */ 319 if (ip6_fw_chk_ptr) { 320 u_short port = 0; 321 /* If ipfw says divert, we have to just drop packet */ 322 if ((*ip6_fw_chk_ptr)(&ip6, rt->rt_ifp, &port, &m)) { 323 m_freem(m); 324 goto freecopy; 325 } 326 if (!m) 327 goto freecopy; 328 } 329 #endif 330 331 error = nd6_output(rt->rt_ifp, m, dst, rt); 332 if (error) { 333 in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard); 334 ip6stat.ip6s_cantforward++; 335 } else { 336 ip6stat.ip6s_forward++; 337 in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward); 338 if (type) 339 ip6stat.ip6s_redirectsent++; 340 else { 341 if (mcopy) 342 goto freecopy; 343 } 344 } 345 if (mcopy == NULL) 346 return; 347 348 switch (error) { 349 case 0: 350 if (type == ND_REDIRECT) { 351 icmp6_redirect_output(mcopy, rt); 352 return; 353 } 354 goto freecopy; 355 356 case EMSGSIZE: 357 /* xxx MTU is constant in PPP? */ 358 goto freecopy; 359 360 case ENOBUFS: 361 /* Tell source to slow down like source quench in IP? */ 362 goto freecopy; 363 364 case ENETUNREACH: /* shouldn't happen, checked above */ 365 case EHOSTUNREACH: 366 case ENETDOWN: 367 case EHOSTDOWN: 368 default: 369 type = ICMP6_DST_UNREACH; 370 code = ICMP6_DST_UNREACH_ADDR; 371 break; 372 } 373 icmp6_error(mcopy, type, code, 0); 374 return; 375 376 freecopy: 377 m_freem(mcopy); 378 return; 379 } 380