| 1 |
27 |
unneback |
//==========================================================================
|
| 2 |
|
|
//
|
| 3 |
|
|
// src/sys/netinet/ip_input.c
|
| 4 |
|
|
//
|
| 5 |
|
|
//==========================================================================
|
| 6 |
|
|
//####BSDCOPYRIGHTBEGIN####
|
| 7 |
|
|
//
|
| 8 |
|
|
// -------------------------------------------
|
| 9 |
|
|
//
|
| 10 |
|
|
// Portions of this software may have been derived from OpenBSD,
|
| 11 |
|
|
// FreeBSD or other sources, and are covered by the appropriate
|
| 12 |
|
|
// copyright disclaimers included herein.
|
| 13 |
|
|
//
|
| 14 |
|
|
// Portions created by Red Hat are
|
| 15 |
|
|
// Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
|
| 16 |
|
|
//
|
| 17 |
|
|
// -------------------------------------------
|
| 18 |
|
|
//
|
| 19 |
|
|
//####BSDCOPYRIGHTEND####
|
| 20 |
|
|
//==========================================================================
|
| 21 |
|
|
|
| 22 |
|
|
/*
|
| 23 |
|
|
* Copyright (c) 1982, 1986, 1988, 1993
|
| 24 |
|
|
* The Regents of the University of California. All rights reserved.
|
| 25 |
|
|
*
|
| 26 |
|
|
* Redistribution and use in source and binary forms, with or without
|
| 27 |
|
|
* modification, are permitted provided that the following conditions
|
| 28 |
|
|
* are met:
|
| 29 |
|
|
* 1. Redistributions of source code must retain the above copyright
|
| 30 |
|
|
* notice, this list of conditions and the following disclaimer.
|
| 31 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
| 32 |
|
|
* notice, this list of conditions and the following disclaimer in the
|
| 33 |
|
|
* documentation and/or other materials provided with the distribution.
|
| 34 |
|
|
* 3. All advertising materials mentioning features or use of this software
|
| 35 |
|
|
* must display the following acknowledgement:
|
| 36 |
|
|
* This product includes software developed by the University of
|
| 37 |
|
|
* California, Berkeley and its contributors.
|
| 38 |
|
|
* 4. Neither the name of the University nor the names of its contributors
|
| 39 |
|
|
* may be used to endorse or promote products derived from this software
|
| 40 |
|
|
* without specific prior written permission.
|
| 41 |
|
|
*
|
| 42 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
| 43 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
| 44 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
| 45 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
| 46 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
| 47 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
| 48 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
| 49 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
| 50 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
| 51 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
| 52 |
|
|
* SUCH DAMAGE.
|
| 53 |
|
|
*
|
| 54 |
|
|
* @(#)ip_input.c 8.2 (Berkeley) 1/4/94
|
| 55 |
|
|
* $FreeBSD: src/sys/netinet/ip_input.c,v 1.130.2.25 2001/08/29 21:41:37 jesper Exp $
|
| 56 |
|
|
*/
|
| 57 |
|
|
|
| 58 |
|
|
#define _IP_VHL
|
| 59 |
|
|
|
| 60 |
|
|
#include <sys/param.h>
|
| 61 |
|
|
#include <sys/mbuf.h>
|
| 62 |
|
|
#include <sys/malloc.h>
|
| 63 |
|
|
#include <sys/domain.h>
|
| 64 |
|
|
#include <sys/protosw.h>
|
| 65 |
|
|
#include <sys/socket.h>
|
| 66 |
|
|
|
| 67 |
|
|
#include <net/if.h>
|
| 68 |
|
|
#include <net/if_var.h>
|
| 69 |
|
|
#include <net/if_dl.h>
|
| 70 |
|
|
#include <net/route.h>
|
| 71 |
|
|
#include <net/netisr.h>
|
| 72 |
|
|
#include <net/intrq.h>
|
| 73 |
|
|
|
| 74 |
|
|
#include <netinet/in.h>
|
| 75 |
|
|
#include <netinet/in_systm.h>
|
| 76 |
|
|
#include <netinet/in_var.h>
|
| 77 |
|
|
#include <netinet/ip.h>
|
| 78 |
|
|
#include <netinet/in_pcb.h>
|
| 79 |
|
|
#include <netinet/ip_var.h>
|
| 80 |
|
|
#include <netinet/ip_icmp.h>
|
| 81 |
|
|
|
| 82 |
|
|
#include <sys/socketvar.h>
|
| 83 |
|
|
|
| 84 |
|
|
#include <netinet/ip_fw.h>
|
| 85 |
|
|
|
| 86 |
|
|
#ifdef IPSEC
|
| 87 |
|
|
#include <netinet6/ipsec.h>
|
| 88 |
|
|
#include <netkey/key.h>
|
| 89 |
|
|
#endif
|
| 90 |
|
|
|
| 91 |
|
|
#ifdef DUMMYNET
|
| 92 |
|
|
#include <netinet/ip_dummynet.h>
|
| 93 |
|
|
#endif
|
| 94 |
|
|
|
| 95 |
|
|
int rsvp_on = 0;
|
| 96 |
|
|
static int ip_rsvp_on;
|
| 97 |
|
|
struct socket *ip_rsvpd;
|
| 98 |
|
|
|
| 99 |
|
|
int ipforwarding = 0;
|
| 100 |
|
|
static int ipsendredirects = 1; /* XXX */
|
| 101 |
|
|
int ip_defttl = IPDEFTTL;
|
| 102 |
|
|
static int ip_dosourceroute = 0;
|
| 103 |
|
|
static int ip_acceptsourceroute = 0;
|
| 104 |
|
|
static int ip_keepfaith = 0;
|
| 105 |
|
|
static int ip_nfragpackets = 0;
|
| 106 |
|
|
static int ip_maxfragpackets; /* initialized in ip_init() */
|
| 107 |
|
|
|
| 108 |
|
|
/*
|
| 109 |
|
|
* XXX - Setting ip_checkinterface mostly implements the receive side of
|
| 110 |
|
|
* the Strong ES model described in RFC 1122, but since the routing table
|
| 111 |
|
|
* and transmit implementation do not implement the Strong ES model,
|
| 112 |
|
|
* setting this to 1 results in an odd hybrid.
|
| 113 |
|
|
*
|
| 114 |
|
|
* XXX - ip_checkinterface currently must be disabled if you use ipnat
|
| 115 |
|
|
* to translate the destination address to another local interface.
|
| 116 |
|
|
*
|
| 117 |
|
|
* XXX - ip_checkinterface must be disabled if you add IP aliases
|
| 118 |
|
|
* to the loopback interface instead of the interface where the
|
| 119 |
|
|
* packets for those addresses are received.
|
| 120 |
|
|
*/
|
| 121 |
|
|
static int ip_checkinterface = 0;
|
| 122 |
|
|
|
| 123 |
|
|
#ifdef DIAGNOSTIC
|
| 124 |
|
|
static int ipprintfs = 0;
|
| 125 |
|
|
#endif
|
| 126 |
|
|
|
| 127 |
|
|
extern struct domain inetdomain;
|
| 128 |
|
|
extern struct protosw inetsw[];
|
| 129 |
|
|
u_char ip_protox[IPPROTO_MAX];
|
| 130 |
|
|
static int ipqmaxlen = IFQ_MAXLEN;
|
| 131 |
|
|
struct in_ifaddrhead in_ifaddrhead; /* first inet address */
|
| 132 |
|
|
struct ipstat ipstat;
|
| 133 |
|
|
|
| 134 |
|
|
/* Packet reassembly stuff */
|
| 135 |
|
|
#define IPREASS_NHASH_LOG2 6
|
| 136 |
|
|
#define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2)
|
| 137 |
|
|
#define IPREASS_HMASK (IPREASS_NHASH - 1)
|
| 138 |
|
|
#define IPREASS_HASH(x,y) \
|
| 139 |
|
|
(((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
|
| 140 |
|
|
|
| 141 |
|
|
static struct ipq ipq[IPREASS_NHASH];
|
| 142 |
|
|
static int nipq = 0; /* total # of reass queues */
|
| 143 |
|
|
static int maxnipq;
|
| 144 |
|
|
const int ipintrq_present = 1;
|
| 145 |
|
|
|
| 146 |
|
|
#ifdef IPSTEALTH
|
| 147 |
|
|
static int ipstealth = 0;
|
| 148 |
|
|
#endif
|
| 149 |
|
|
|
| 150 |
|
|
|
| 151 |
|
|
/* Firewall hooks */
|
| 152 |
|
|
ip_fw_chk_t *ip_fw_chk_ptr;
|
| 153 |
|
|
ip_fw_ctl_t *ip_fw_ctl_ptr;
|
| 154 |
|
|
int fw_enable = 1 ;
|
| 155 |
|
|
|
| 156 |
|
|
#ifdef DUMMYNET
|
| 157 |
|
|
ip_dn_ctl_t *ip_dn_ctl_ptr;
|
| 158 |
|
|
#endif
|
| 159 |
|
|
|
| 160 |
|
|
int (*fr_checkp) __P((struct ip *, int, struct ifnet *, int, struct mbuf **)) = NULL;
|
| 161 |
|
|
|
| 162 |
|
|
|
| 163 |
|
|
/*
|
| 164 |
|
|
* We need to save the IP options in case a protocol wants to respond
|
| 165 |
|
|
* to an incoming packet over the same route if the packet got here
|
| 166 |
|
|
* using IP source routing. This allows connection establishment and
|
| 167 |
|
|
* maintenance when the remote end is on a network that is not known
|
| 168 |
|
|
* to us.
|
| 169 |
|
|
*/
|
| 170 |
|
|
static int ip_nhops = 0;
|
| 171 |
|
|
static struct ip_srcrt {
|
| 172 |
|
|
struct in_addr dst; /* final destination */
|
| 173 |
|
|
char nop; /* one NOP to align */
|
| 174 |
|
|
char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */
|
| 175 |
|
|
struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
|
| 176 |
|
|
} ip_srcrt;
|
| 177 |
|
|
|
| 178 |
|
|
struct sockaddr_in *ip_fw_fwd_addr;
|
| 179 |
|
|
|
| 180 |
|
|
static void save_rte __P((u_char *, struct in_addr));
|
| 181 |
|
|
static int ip_dooptions __P((struct mbuf *));
|
| 182 |
|
|
#ifdef NATPT
|
| 183 |
|
|
void ip_forward __P((struct mbuf *, int));
|
| 184 |
|
|
#else
|
| 185 |
|
|
static void ip_forward __P((struct mbuf *, int));
|
| 186 |
|
|
#endif
|
| 187 |
|
|
static void ip_freef __P((struct ipq *));
|
| 188 |
|
|
#ifdef IPDIVERT
|
| 189 |
|
|
static struct mbuf *ip_reass __P((struct mbuf *,
|
| 190 |
|
|
struct ipq *, struct ipq *, u_int32_t *, u_int16_t *));
|
| 191 |
|
|
#else
|
| 192 |
|
|
static struct mbuf *ip_reass __P((struct mbuf *, struct ipq *, struct ipq *));
|
| 193 |
|
|
#endif
|
| 194 |
|
|
static struct in_ifaddr *ip_rtaddr __P((struct in_addr));
|
| 195 |
|
|
static void ipintr __P((void));
|
| 196 |
|
|
|
| 197 |
|
|
#ifdef NATPT
|
| 198 |
|
|
extern int ip6_protocol_tr;
|
| 199 |
|
|
int natpt_in4 __P((struct mbuf *, struct mbuf **));
|
| 200 |
|
|
extern void ip6_forward __P((struct mbuf *, int));
|
| 201 |
|
|
#endif /* NATPT */
|
| 202 |
|
|
|
| 203 |
|
|
/*
|
| 204 |
|
|
* IP initialization: fill in IP protocol switch table.
|
| 205 |
|
|
* All protocols not implemented in kernel go to raw IP protocol handler.
|
| 206 |
|
|
*/
|
| 207 |
|
|
void
|
| 208 |
|
|
ip_init()
|
| 209 |
|
|
{
|
| 210 |
|
|
register struct protosw *pr;
|
| 211 |
|
|
register int i;
|
| 212 |
|
|
|
| 213 |
|
|
TAILQ_INIT(&in_ifaddrhead);
|
| 214 |
|
|
pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
|
| 215 |
|
|
if (pr == 0)
|
| 216 |
|
|
panic("ip_init");
|
| 217 |
|
|
for (i = 0; i < IPPROTO_MAX; i++)
|
| 218 |
|
|
ip_protox[i] = pr - inetsw;
|
| 219 |
|
|
for (pr = inetdomain.dom_protosw;
|
| 220 |
|
|
pr < inetdomain.dom_protoswNPROTOSW; pr++)
|
| 221 |
|
|
if (pr->pr_domain->dom_family == PF_INET &&
|
| 222 |
|
|
pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
|
| 223 |
|
|
ip_protox[pr->pr_protocol] = pr - inetsw;
|
| 224 |
|
|
|
| 225 |
|
|
for (i = 0; i < IPREASS_NHASH; i++)
|
| 226 |
|
|
ipq[i].next = ipq[i].prev = &ipq[i];
|
| 227 |
|
|
|
| 228 |
|
|
maxnipq = nmbclusters / 4;
|
| 229 |
|
|
ip_maxfragpackets = nmbclusters / 4;
|
| 230 |
|
|
|
| 231 |
|
|
#ifndef RANDOM_IP_ID
|
| 232 |
|
|
ip_id = time_second & 0xffff;
|
| 233 |
|
|
#endif
|
| 234 |
|
|
ipintrq.ifq_maxlen = ipqmaxlen;
|
| 235 |
|
|
|
| 236 |
|
|
register_netisr(NETISR_IP, ipintr);
|
| 237 |
|
|
}
|
| 238 |
|
|
|
| 239 |
|
|
static struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
|
| 240 |
|
|
static struct route ipforward_rt;
|
| 241 |
|
|
|
| 242 |
|
|
/*
|
| 243 |
|
|
* Ip input routine. Checksum and byte swap header. If fragmented
|
| 244 |
|
|
* try to reassemble. Process options. Pass to next level.
|
| 245 |
|
|
*/
|
| 246 |
|
|
void
|
| 247 |
|
|
ip_input(struct mbuf *m)
|
| 248 |
|
|
{
|
| 249 |
|
|
struct ip *ip;
|
| 250 |
|
|
struct ipq *fp;
|
| 251 |
|
|
struct in_ifaddr *ia = NULL;
|
| 252 |
|
|
int i, hlen, mff, checkif;
|
| 253 |
|
|
u_short sum;
|
| 254 |
|
|
u_int16_t divert_cookie; /* firewall cookie */
|
| 255 |
|
|
struct in_addr pkt_dst;
|
| 256 |
|
|
#ifdef IPDIVERT
|
| 257 |
|
|
u_int32_t divert_info = 0; /* packet divert/tee info */
|
| 258 |
|
|
#endif
|
| 259 |
|
|
struct ip_fw_chain *rule = NULL;
|
| 260 |
|
|
|
| 261 |
|
|
#ifdef IPDIVERT
|
| 262 |
|
|
/* Get and reset firewall cookie */
|
| 263 |
|
|
divert_cookie = ip_divert_cookie;
|
| 264 |
|
|
ip_divert_cookie = 0;
|
| 265 |
|
|
#else
|
| 266 |
|
|
divert_cookie = 0;
|
| 267 |
|
|
#endif
|
| 268 |
|
|
|
| 269 |
|
|
#if defined(IPFIREWALL) && defined(DUMMYNET)
|
| 270 |
|
|
/*
|
| 271 |
|
|
* dummynet packet are prepended a vestigial mbuf with
|
| 272 |
|
|
* m_type = MT_DUMMYNET and m_data pointing to the matching
|
| 273 |
|
|
* rule.
|
| 274 |
|
|
*/
|
| 275 |
|
|
if (m->m_type == MT_DUMMYNET) {
|
| 276 |
|
|
rule = (struct ip_fw_chain *)(m->m_data) ;
|
| 277 |
|
|
m = m->m_next ;
|
| 278 |
|
|
ip = mtod(m, struct ip *);
|
| 279 |
|
|
hlen = IP_VHL_HL(ip->ip_vhl) << 2;
|
| 280 |
|
|
goto iphack ;
|
| 281 |
|
|
} else
|
| 282 |
|
|
rule = NULL ;
|
| 283 |
|
|
#endif
|
| 284 |
|
|
|
| 285 |
|
|
#ifdef DIAGNOSTIC
|
| 286 |
|
|
if (m == NULL || (m->m_flags & M_PKTHDR) == 0)
|
| 287 |
|
|
panic("ip_input no HDR");
|
| 288 |
|
|
#endif
|
| 289 |
|
|
ipstat.ips_total++;
|
| 290 |
|
|
|
| 291 |
|
|
if (m->m_pkthdr.len < sizeof(struct ip))
|
| 292 |
|
|
goto tooshort;
|
| 293 |
|
|
|
| 294 |
|
|
if (m->m_len < sizeof (struct ip) &&
|
| 295 |
|
|
(m = m_pullup(m, sizeof (struct ip))) == 0) {
|
| 296 |
|
|
ipstat.ips_toosmall++;
|
| 297 |
|
|
return;
|
| 298 |
|
|
}
|
| 299 |
|
|
ip = mtod(m, struct ip *);
|
| 300 |
|
|
|
| 301 |
|
|
if (IP_VHL_V(ip->ip_vhl) != IPVERSION) {
|
| 302 |
|
|
ipstat.ips_badvers++;
|
| 303 |
|
|
goto bad;
|
| 304 |
|
|
}
|
| 305 |
|
|
|
| 306 |
|
|
hlen = IP_VHL_HL(ip->ip_vhl) << 2;
|
| 307 |
|
|
if (hlen < sizeof(struct ip)) { /* minimum header length */
|
| 308 |
|
|
ipstat.ips_badhlen++;
|
| 309 |
|
|
goto bad;
|
| 310 |
|
|
}
|
| 311 |
|
|
if (hlen > m->m_len) {
|
| 312 |
|
|
if ((m = m_pullup(m, hlen)) == 0) {
|
| 313 |
|
|
ipstat.ips_badhlen++;
|
| 314 |
|
|
return;
|
| 315 |
|
|
}
|
| 316 |
|
|
ip = mtod(m, struct ip *);
|
| 317 |
|
|
}
|
| 318 |
|
|
|
| 319 |
|
|
/* 127/8 must not appear on wire - RFC1122 */
|
| 320 |
|
|
if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
|
| 321 |
|
|
(ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
|
| 322 |
|
|
if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
|
| 323 |
|
|
ipstat.ips_badaddr++;
|
| 324 |
|
|
goto bad;
|
| 325 |
|
|
}
|
| 326 |
|
|
}
|
| 327 |
|
|
|
| 328 |
|
|
if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
|
| 329 |
|
|
sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
|
| 330 |
|
|
} else {
|
| 331 |
|
|
if (hlen == sizeof(struct ip)) {
|
| 332 |
|
|
sum = in_cksum_hdr(ip);
|
| 333 |
|
|
} else {
|
| 334 |
|
|
sum = in_cksum(m, hlen);
|
| 335 |
|
|
}
|
| 336 |
|
|
}
|
| 337 |
|
|
if (sum) {
|
| 338 |
|
|
ipstat.ips_badsum++;
|
| 339 |
|
|
goto bad;
|
| 340 |
|
|
}
|
| 341 |
|
|
|
| 342 |
|
|
#ifdef ALTQ
|
| 343 |
|
|
if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
|
| 344 |
|
|
/* packet is dropped by traffic conditioner */
|
| 345 |
|
|
return;
|
| 346 |
|
|
#endif
|
| 347 |
|
|
/*
|
| 348 |
|
|
* Convert fields to host representation.
|
| 349 |
|
|
*/
|
| 350 |
|
|
NTOHS(ip->ip_len);
|
| 351 |
|
|
if (ip->ip_len < hlen) {
|
| 352 |
|
|
ipstat.ips_badlen++;
|
| 353 |
|
|
goto bad;
|
| 354 |
|
|
}
|
| 355 |
|
|
NTOHS(ip->ip_off);
|
| 356 |
|
|
|
| 357 |
|
|
/*
|
| 358 |
|
|
* Check that the amount of data in the buffers
|
| 359 |
|
|
* is as at least much as the IP header would have us expect.
|
| 360 |
|
|
* Trim mbufs if longer than we expect.
|
| 361 |
|
|
* Drop packet if shorter than we expect.
|
| 362 |
|
|
*/
|
| 363 |
|
|
if (m->m_pkthdr.len < ip->ip_len) {
|
| 364 |
|
|
tooshort:
|
| 365 |
|
|
ipstat.ips_tooshort++;
|
| 366 |
|
|
goto bad;
|
| 367 |
|
|
}
|
| 368 |
|
|
if (m->m_pkthdr.len > ip->ip_len) {
|
| 369 |
|
|
if (m->m_len == m->m_pkthdr.len) {
|
| 370 |
|
|
m->m_len = ip->ip_len;
|
| 371 |
|
|
m->m_pkthdr.len = ip->ip_len;
|
| 372 |
|
|
} else
|
| 373 |
|
|
m_adj(m, ip->ip_len - m->m_pkthdr.len);
|
| 374 |
|
|
}
|
| 375 |
|
|
|
| 376 |
|
|
#ifdef IPSEC
|
| 377 |
|
|
if (ipsec_getnhist(m))
|
| 378 |
|
|
goto pass;
|
| 379 |
|
|
#endif
|
| 380 |
|
|
|
| 381 |
|
|
/*
|
| 382 |
|
|
* IpHack's section.
|
| 383 |
|
|
* Right now when no processing on packet has done
|
| 384 |
|
|
* and it is still fresh out of network we do our black
|
| 385 |
|
|
* deals with it.
|
| 386 |
|
|
* - Firewall: deny/allow/divert
|
| 387 |
|
|
* - Xlate: translate packet's addr/port (NAT).
|
| 388 |
|
|
* - Pipe: pass pkt through dummynet.
|
| 389 |
|
|
* - Wrap: fake packet's addr/port <unimpl.>
|
| 390 |
|
|
* - Encapsulate: put it in another IP and send out. <unimp.>
|
| 391 |
|
|
*/
|
| 392 |
|
|
|
| 393 |
|
|
#if defined(IPFIREWALL) && defined(DUMMYNET)
|
| 394 |
|
|
iphack:
|
| 395 |
|
|
#endif
|
| 396 |
|
|
/*
|
| 397 |
|
|
* Check if we want to allow this packet to be processed.
|
| 398 |
|
|
* Consider it to be bad if not.
|
| 399 |
|
|
*/
|
| 400 |
|
|
if (fr_checkp) {
|
| 401 |
|
|
struct mbuf *m1 = m;
|
| 402 |
|
|
|
| 403 |
|
|
if ((*fr_checkp)(ip, hlen, m->m_pkthdr.rcvif, 0, &m1) || !m1)
|
| 404 |
|
|
return;
|
| 405 |
|
|
ip = mtod(m = m1, struct ip *);
|
| 406 |
|
|
}
|
| 407 |
|
|
if (fw_enable && ip_fw_chk_ptr) {
|
| 408 |
|
|
#ifdef IPFIREWALL_FORWARD
|
| 409 |
|
|
/*
|
| 410 |
|
|
* If we've been forwarded from the output side, then
|
| 411 |
|
|
* skip the firewall a second time
|
| 412 |
|
|
*/
|
| 413 |
|
|
if (ip_fw_fwd_addr)
|
| 414 |
|
|
goto ours;
|
| 415 |
|
|
#endif /* IPFIREWALL_FORWARD */
|
| 416 |
|
|
/*
|
| 417 |
|
|
* See the comment in ip_output for the return values
|
| 418 |
|
|
* produced by the firewall.
|
| 419 |
|
|
*/
|
| 420 |
|
|
i = (*ip_fw_chk_ptr)(&ip,
|
| 421 |
|
|
hlen, NULL, &divert_cookie, &m, &rule, &ip_fw_fwd_addr);
|
| 422 |
|
|
if ( (i & IP_FW_PORT_DENY_FLAG) || m == NULL) { /* drop */
|
| 423 |
|
|
if (m)
|
| 424 |
|
|
m_freem(m);
|
| 425 |
|
|
return ;
|
| 426 |
|
|
}
|
| 427 |
|
|
ip = mtod(m, struct ip *); /* just in case m changed */
|
| 428 |
|
|
if (i == 0 && ip_fw_fwd_addr == NULL) /* common case */
|
| 429 |
|
|
goto pass;
|
| 430 |
|
|
#ifdef DUMMYNET
|
| 431 |
|
|
if ((i & IP_FW_PORT_DYNT_FLAG) != 0) {
|
| 432 |
|
|
/* Send packet to the appropriate pipe */
|
| 433 |
|
|
dummynet_io(i&0xffff,DN_TO_IP_IN,m,NULL,NULL,0, rule,
|
| 434 |
|
|
0);
|
| 435 |
|
|
return;
|
| 436 |
|
|
}
|
| 437 |
|
|
#endif
|
| 438 |
|
|
#ifdef IPDIVERT
|
| 439 |
|
|
if (i != 0 && (i & IP_FW_PORT_DYNT_FLAG) == 0) {
|
| 440 |
|
|
/* Divert or tee packet */
|
| 441 |
|
|
divert_info = i;
|
| 442 |
|
|
goto ours;
|
| 443 |
|
|
}
|
| 444 |
|
|
#endif
|
| 445 |
|
|
#ifdef IPFIREWALL_FORWARD
|
| 446 |
|
|
if (i == 0 && ip_fw_fwd_addr != NULL)
|
| 447 |
|
|
goto pass;
|
| 448 |
|
|
#endif
|
| 449 |
|
|
/*
|
| 450 |
|
|
* if we get here, the packet must be dropped
|
| 451 |
|
|
*/
|
| 452 |
|
|
m_freem(m);
|
| 453 |
|
|
return;
|
| 454 |
|
|
}
|
| 455 |
|
|
pass:
|
| 456 |
|
|
|
| 457 |
|
|
/*
|
| 458 |
|
|
* Process options and, if not destined for us,
|
| 459 |
|
|
* ship it on. ip_dooptions returns 1 when an
|
| 460 |
|
|
* error was detected (causing an icmp message
|
| 461 |
|
|
* to be sent and the original packet to be freed).
|
| 462 |
|
|
*/
|
| 463 |
|
|
ip_nhops = 0; /* for source routed packets */
|
| 464 |
|
|
if (hlen > sizeof (struct ip) && ip_dooptions(m)) {
|
| 465 |
|
|
#ifdef IPFIREWALL_FORWARD
|
| 466 |
|
|
ip_fw_fwd_addr = NULL;
|
| 467 |
|
|
#endif
|
| 468 |
|
|
return;
|
| 469 |
|
|
}
|
| 470 |
|
|
|
| 471 |
|
|
/* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
|
| 472 |
|
|
* matter if it is destined to another node, or whether it is
|
| 473 |
|
|
* a multicast one, RSVP wants it! and prevents it from being forwarded
|
| 474 |
|
|
* anywhere else. Also checks if the rsvp daemon is running before
|
| 475 |
|
|
* grabbing the packet.
|
| 476 |
|
|
*/
|
| 477 |
|
|
if (rsvp_on && ip->ip_p==IPPROTO_RSVP)
|
| 478 |
|
|
goto ours;
|
| 479 |
|
|
|
| 480 |
|
|
#ifdef NATPT
|
| 481 |
|
|
/*
|
| 482 |
|
|
* NATPT (Network Address Translation - Protocol Translation)
|
| 483 |
|
|
*/
|
| 484 |
|
|
if (ip6_protocol_tr) {
|
| 485 |
|
|
struct mbuf *m1 = NULL;
|
| 486 |
|
|
|
| 487 |
|
|
switch (natpt_in4(m, &m1)) {
|
| 488 |
|
|
case IPPROTO_IP: /* this packet is not changed */
|
| 489 |
|
|
goto checkaddresses;
|
| 490 |
|
|
|
| 491 |
|
|
case IPPROTO_IPV4:
|
| 492 |
|
|
ip_forward(m1, 0);
|
| 493 |
|
|
break;
|
| 494 |
|
|
|
| 495 |
|
|
case IPPROTO_IPV6:
|
| 496 |
|
|
ip6_forward(m1, 1);
|
| 497 |
|
|
break;
|
| 498 |
|
|
|
| 499 |
|
|
case IPPROTO_DONE: /* discard without free */
|
| 500 |
|
|
return;
|
| 501 |
|
|
|
| 502 |
|
|
case IPPROTO_MAX: /* discard this packet */
|
| 503 |
|
|
default:
|
| 504 |
|
|
break;
|
| 505 |
|
|
}
|
| 506 |
|
|
|
| 507 |
|
|
if (m != m1)
|
| 508 |
|
|
m_freem(m);
|
| 509 |
|
|
|
| 510 |
|
|
return;
|
| 511 |
|
|
}
|
| 512 |
|
|
checkaddresses:;
|
| 513 |
|
|
#endif
|
| 514 |
|
|
|
| 515 |
|
|
/*
|
| 516 |
|
|
* Check our list of addresses, to see if the packet is for us.
|
| 517 |
|
|
* If we don't have any addresses, assume any unicast packet
|
| 518 |
|
|
* we receive might be for us (and let the upper layers deal
|
| 519 |
|
|
* with it).
|
| 520 |
|
|
*/
|
| 521 |
|
|
if (TAILQ_EMPTY(&in_ifaddrhead) &&
|
| 522 |
|
|
(m->m_flags & (M_MCAST|M_BCAST)) == 0)
|
| 523 |
|
|
goto ours;
|
| 524 |
|
|
|
| 525 |
|
|
/*
|
| 526 |
|
|
* Cache the destination address of the packet; this may be
|
| 527 |
|
|
* changed by use of 'ipfw fwd'.
|
| 528 |
|
|
*/
|
| 529 |
|
|
pkt_dst = ip_fw_fwd_addr == NULL ?
|
| 530 |
|
|
ip->ip_dst : ip_fw_fwd_addr->sin_addr;
|
| 531 |
|
|
|
| 532 |
|
|
/*
|
| 533 |
|
|
* Enable a consistency check between the destination address
|
| 534 |
|
|
* and the arrival interface for a unicast packet (the RFC 1122
|
| 535 |
|
|
* strong ES model) if IP forwarding is disabled and the packet
|
| 536 |
|
|
* is not locally generated and the packet is not subject to
|
| 537 |
|
|
* 'ipfw fwd'.
|
| 538 |
|
|
*
|
| 539 |
|
|
* XXX - Checking also should be disabled if the destination
|
| 540 |
|
|
* address is ipnat'ed to a different interface.
|
| 541 |
|
|
*
|
| 542 |
|
|
* XXX - Checking is incompatible with IP aliases added
|
| 543 |
|
|
* to the loopback interface instead of the interface where
|
| 544 |
|
|
* the packets are received.
|
| 545 |
|
|
*/
|
| 546 |
|
|
checkif = ip_checkinterface && (ipforwarding == 0) &&
|
| 547 |
|
|
((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) &&
|
| 548 |
|
|
(ip_fw_fwd_addr == NULL);
|
| 549 |
|
|
|
| 550 |
|
|
TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
|
| 551 |
|
|
#define satosin(sa) ((struct sockaddr_in *)(sa))
|
| 552 |
|
|
|
| 553 |
|
|
#ifdef BOOTP_COMPAT
|
| 554 |
|
|
if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY)
|
| 555 |
|
|
goto ours;
|
| 556 |
|
|
#endif
|
| 557 |
|
|
/*
|
| 558 |
|
|
* If the address matches, verify that the packet
|
| 559 |
|
|
* arrived via the correct interface if checking is
|
| 560 |
|
|
* enabled.
|
| 561 |
|
|
*/
|
| 562 |
|
|
if (IA_SIN(ia)->sin_addr.s_addr == pkt_dst.s_addr &&
|
| 563 |
|
|
(!checkif || ia->ia_ifp == m->m_pkthdr.rcvif))
|
| 564 |
|
|
goto ours;
|
| 565 |
|
|
/*
|
| 566 |
|
|
* Only accept broadcast packets that arrive via the
|
| 567 |
|
|
* matching interface. Reception of forwarded directed
|
| 568 |
|
|
* broadcasts would be handled via ip_forward() and
|
| 569 |
|
|
* ether_output() with the loopback into the stack for
|
| 570 |
|
|
* SIMPLEX interfaces handled by ether_output().
|
| 571 |
|
|
*/
|
| 572 |
|
|
if (ia->ia_ifp == m->m_pkthdr.rcvif &&
|
| 573 |
|
|
ia->ia_ifp && ia->ia_ifp->if_flags & IFF_BROADCAST) {
|
| 574 |
|
|
if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
|
| 575 |
|
|
pkt_dst.s_addr)
|
| 576 |
|
|
goto ours;
|
| 577 |
|
|
if (ia->ia_netbroadcast.s_addr == pkt_dst.s_addr)
|
| 578 |
|
|
goto ours;
|
| 579 |
|
|
}
|
| 580 |
|
|
}
|
| 581 |
|
|
if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
|
| 582 |
|
|
struct in_multi *inm;
|
| 583 |
|
|
if (ip_mrouter) {
|
| 584 |
|
|
/*
|
| 585 |
|
|
* If we are acting as a multicast router, all
|
| 586 |
|
|
* incoming multicast packets are passed to the
|
| 587 |
|
|
* kernel-level multicast forwarding function.
|
| 588 |
|
|
* The packet is returned (relatively) intact; if
|
| 589 |
|
|
* ip_mforward() returns a non-zero value, the packet
|
| 590 |
|
|
* must be discarded, else it may be accepted below.
|
| 591 |
|
|
*/
|
| 592 |
|
|
if (ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) {
|
| 593 |
|
|
ipstat.ips_cantforward++;
|
| 594 |
|
|
m_freem(m);
|
| 595 |
|
|
return;
|
| 596 |
|
|
}
|
| 597 |
|
|
|
| 598 |
|
|
/*
|
| 599 |
|
|
* The process-level routing demon needs to receive
|
| 600 |
|
|
* all multicast IGMP packets, whether or not this
|
| 601 |
|
|
* host belongs to their destination groups.
|
| 602 |
|
|
*/
|
| 603 |
|
|
if (ip->ip_p == IPPROTO_IGMP)
|
| 604 |
|
|
goto ours;
|
| 605 |
|
|
ipstat.ips_forward++;
|
| 606 |
|
|
}
|
| 607 |
|
|
/*
|
| 608 |
|
|
* See if we belong to the destination multicast group on the
|
| 609 |
|
|
* arrival interface.
|
| 610 |
|
|
*/
|
| 611 |
|
|
IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
|
| 612 |
|
|
if (inm == NULL) {
|
| 613 |
|
|
ipstat.ips_notmember++;
|
| 614 |
|
|
m_freem(m);
|
| 615 |
|
|
return;
|
| 616 |
|
|
}
|
| 617 |
|
|
goto ours;
|
| 618 |
|
|
}
|
| 619 |
|
|
if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
|
| 620 |
|
|
goto ours;
|
| 621 |
|
|
if (ip->ip_dst.s_addr == INADDR_ANY)
|
| 622 |
|
|
goto ours;
|
| 623 |
|
|
|
| 624 |
|
|
#if defined(NFAITH) && 0 < NFAITH
|
| 625 |
|
|
/*
|
| 626 |
|
|
* FAITH(Firewall Aided Internet Translator)
|
| 627 |
|
|
*/
|
| 628 |
|
|
if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
|
| 629 |
|
|
if (ip_keepfaith) {
|
| 630 |
|
|
if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP)
|
| 631 |
|
|
goto ours;
|
| 632 |
|
|
}
|
| 633 |
|
|
m_freem(m);
|
| 634 |
|
|
return;
|
| 635 |
|
|
}
|
| 636 |
|
|
#endif
|
| 637 |
|
|
/*
|
| 638 |
|
|
* Not for us; forward if possible and desirable.
|
| 639 |
|
|
*/
|
| 640 |
|
|
if (ipforwarding == 0) {
|
| 641 |
|
|
ipstat.ips_cantforward++;
|
| 642 |
|
|
m_freem(m);
|
| 643 |
|
|
} else
|
| 644 |
|
|
ip_forward(m, 0);
|
| 645 |
|
|
#ifdef IPFIREWALL_FORWARD
|
| 646 |
|
|
ip_fw_fwd_addr = NULL;
|
| 647 |
|
|
#endif
|
| 648 |
|
|
return;
|
| 649 |
|
|
|
| 650 |
|
|
ours:
|
| 651 |
|
|
/* Count the packet in the ip address stats */
|
| 652 |
|
|
if (ia != NULL) {
|
| 653 |
|
|
ia->ia_ifa.if_ipackets++;
|
| 654 |
|
|
ia->ia_ifa.if_ibytes += m->m_pkthdr.len;
|
| 655 |
|
|
}
|
| 656 |
|
|
|
| 657 |
|
|
/*
|
| 658 |
|
|
* If offset or IP_MF are set, must reassemble.
|
| 659 |
|
|
* Otherwise, nothing need be done.
|
| 660 |
|
|
* (We could look in the reassembly queue to see
|
| 661 |
|
|
* if the packet was previously fragmented,
|
| 662 |
|
|
* but it's not worth the time; just let them time out.)
|
| 663 |
|
|
*/
|
| 664 |
|
|
if (ip->ip_off & (IP_MF | IP_OFFMASK | IP_RF)) {
|
| 665 |
|
|
|
| 666 |
|
|
#if 0 /*
|
| 667 |
|
|
* Reassembly should be able to treat a mbuf cluster, for later
|
| 668 |
|
|
* operation of contiguous protocol headers on the cluster. (KAME)
|
| 669 |
|
|
*/
|
| 670 |
|
|
if (m->m_flags & M_EXT) { /* XXX */
|
| 671 |
|
|
if ((m = m_pullup(m, hlen)) == 0) {
|
| 672 |
|
|
ipstat.ips_toosmall++;
|
| 673 |
|
|
#ifdef IPFIREWALL_FORWARD
|
| 674 |
|
|
ip_fw_fwd_addr = NULL;
|
| 675 |
|
|
#endif
|
| 676 |
|
|
return;
|
| 677 |
|
|
}
|
| 678 |
|
|
ip = mtod(m, struct ip *);
|
| 679 |
|
|
}
|
| 680 |
|
|
#endif
|
| 681 |
|
|
sum = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
|
| 682 |
|
|
/*
|
| 683 |
|
|
* Look for queue of fragments
|
| 684 |
|
|
* of this datagram.
|
| 685 |
|
|
*/
|
| 686 |
|
|
for (fp = ipq[sum].next; fp != &ipq[sum]; fp = fp->next)
|
| 687 |
|
|
if (ip->ip_id == fp->ipq_id &&
|
| 688 |
|
|
ip->ip_src.s_addr == fp->ipq_src.s_addr &&
|
| 689 |
|
|
ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
|
| 690 |
|
|
ip->ip_p == fp->ipq_p)
|
| 691 |
|
|
goto found;
|
| 692 |
|
|
|
| 693 |
|
|
fp = 0;
|
| 694 |
|
|
|
| 695 |
|
|
/* check if there's a place for the new queue */
|
| 696 |
|
|
if (nipq > maxnipq) {
|
| 697 |
|
|
/*
|
| 698 |
|
|
* drop something from the tail of the current queue
|
| 699 |
|
|
* before proceeding further
|
| 700 |
|
|
*/
|
| 701 |
|
|
if (ipq[sum].prev == &ipq[sum]) { /* gak */
|
| 702 |
|
|
for (i = 0; i < IPREASS_NHASH; i++) {
|
| 703 |
|
|
if (ipq[i].prev != &ipq[i]) {
|
| 704 |
|
|
ip_freef(ipq[i].prev);
|
| 705 |
|
|
break;
|
| 706 |
|
|
}
|
| 707 |
|
|
}
|
| 708 |
|
|
} else
|
| 709 |
|
|
ip_freef(ipq[sum].prev);
|
| 710 |
|
|
}
|
| 711 |
|
|
found:
|
| 712 |
|
|
/*
|
| 713 |
|
|
* Adjust ip_len to not reflect header,
|
| 714 |
|
|
* set ip_mff if more fragments are expected,
|
| 715 |
|
|
* convert offset of this to bytes.
|
| 716 |
|
|
*/
|
| 717 |
|
|
ip->ip_len -= hlen;
|
| 718 |
|
|
mff = (ip->ip_off & IP_MF) != 0;
|
| 719 |
|
|
if (mff) {
|
| 720 |
|
|
/*
|
| 721 |
|
|
* Make sure that fragments have a data length
|
| 722 |
|
|
* that's a non-zero multiple of 8 bytes.
|
| 723 |
|
|
*/
|
| 724 |
|
|
if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
|
| 725 |
|
|
ipstat.ips_toosmall++; /* XXX */
|
| 726 |
|
|
goto bad;
|
| 727 |
|
|
}
|
| 728 |
|
|
m->m_flags |= M_FRAG;
|
| 729 |
|
|
}
|
| 730 |
|
|
ip->ip_off <<= 3;
|
| 731 |
|
|
|
| 732 |
|
|
/*
|
| 733 |
|
|
* If datagram marked as having more fragments
|
| 734 |
|
|
* or if this is not the first fragment,
|
| 735 |
|
|
* attempt reassembly; if it succeeds, proceed.
|
| 736 |
|
|
*/
|
| 737 |
|
|
if (mff || ip->ip_off) {
|
| 738 |
|
|
ipstat.ips_fragments++;
|
| 739 |
|
|
m->m_pkthdr.header = ip;
|
| 740 |
|
|
#ifdef IPDIVERT
|
| 741 |
|
|
m = ip_reass(m,
|
| 742 |
|
|
fp, &ipq[sum], &divert_info, &divert_cookie);
|
| 743 |
|
|
#else
|
| 744 |
|
|
m = ip_reass(m, fp, &ipq[sum]);
|
| 745 |
|
|
#endif
|
| 746 |
|
|
if (m == 0) {
|
| 747 |
|
|
#ifdef IPFIREWALL_FORWARD
|
| 748 |
|
|
ip_fw_fwd_addr = NULL;
|
| 749 |
|
|
#endif
|
| 750 |
|
|
return;
|
| 751 |
|
|
}
|
| 752 |
|
|
ipstat.ips_reassembled++;
|
| 753 |
|
|
ip = mtod(m, struct ip *);
|
| 754 |
|
|
/* Get the header length of the reassembled packet */
|
| 755 |
|
|
hlen = IP_VHL_HL(ip->ip_vhl) << 2;
|
| 756 |
|
|
#ifdef IPDIVERT
|
| 757 |
|
|
/* Restore original checksum before diverting packet */
|
| 758 |
|
|
if (divert_info != 0) {
|
| 759 |
|
|
ip->ip_len += hlen;
|
| 760 |
|
|
HTONS(ip->ip_len);
|
| 761 |
|
|
HTONS(ip->ip_off);
|
| 762 |
|
|
ip->ip_sum = 0;
|
| 763 |
|
|
if (hlen == sizeof(struct ip))
|
| 764 |
|
|
ip->ip_sum = in_cksum_hdr(ip);
|
| 765 |
|
|
else
|
| 766 |
|
|
ip->ip_sum = in_cksum(m, hlen);
|
| 767 |
|
|
NTOHS(ip->ip_off);
|
| 768 |
|
|
NTOHS(ip->ip_len);
|
| 769 |
|
|
ip->ip_len -= hlen;
|
| 770 |
|
|
}
|
| 771 |
|
|
#endif
|
| 772 |
|
|
} else
|
| 773 |
|
|
if (fp)
|
| 774 |
|
|
ip_freef(fp);
|
| 775 |
|
|
} else
|
| 776 |
|
|
ip->ip_len -= hlen;
|
| 777 |
|
|
|
| 778 |
|
|
#ifdef IPDIVERT
|
| 779 |
|
|
/*
|
| 780 |
|
|
* Divert or tee packet to the divert protocol if required.
|
| 781 |
|
|
*
|
| 782 |
|
|
* If divert_info is zero then cookie should be too, so we shouldn't
|
| 783 |
|
|
* need to clear them here. Assume divert_packet() does so also.
|
| 784 |
|
|
*/
|
| 785 |
|
|
if (divert_info != 0) {
|
| 786 |
|
|
struct mbuf *clone = NULL;
|
| 787 |
|
|
|
| 788 |
|
|
/* Clone packet if we're doing a 'tee' */
|
| 789 |
|
|
if ((divert_info & IP_FW_PORT_TEE_FLAG) != 0)
|
| 790 |
|
|
clone = m_dup(m, M_DONTWAIT);
|
| 791 |
|
|
|
| 792 |
|
|
/* Restore packet header fields to original values */
|
| 793 |
|
|
ip->ip_len += hlen;
|
| 794 |
|
|
HTONS(ip->ip_len);
|
| 795 |
|
|
HTONS(ip->ip_off);
|
| 796 |
|
|
|
| 797 |
|
|
/* Deliver packet to divert input routine */
|
| 798 |
|
|
ip_divert_cookie = divert_cookie;
|
| 799 |
|
|
divert_packet(m, 1, divert_info & 0xffff);
|
| 800 |
|
|
ipstat.ips_delivered++;
|
| 801 |
|
|
|
| 802 |
|
|
/* If 'tee', continue with original packet */
|
| 803 |
|
|
if (clone == NULL)
|
| 804 |
|
|
return;
|
| 805 |
|
|
m = clone;
|
| 806 |
|
|
ip = mtod(m, struct ip *);
|
| 807 |
|
|
}
|
| 808 |
|
|
#endif
|
| 809 |
|
|
|
| 810 |
|
|
#ifdef IPSEC
|
| 811 |
|
|
/*
|
| 812 |
|
|
* enforce IPsec policy checking if we are seeing last header.
|
| 813 |
|
|
* note that we do not visit this with protocols with pcb layer
|
| 814 |
|
|
* code - like udp/tcp/raw ip.
|
| 815 |
|
|
*/
|
| 816 |
|
|
if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0 &&
|
| 817 |
|
|
ipsec4_in_reject(m, NULL)) {
|
| 818 |
|
|
ipsecstat.in_polvio++;
|
| 819 |
|
|
goto bad;
|
| 820 |
|
|
}
|
| 821 |
|
|
#endif
|
| 822 |
|
|
|
| 823 |
|
|
/*
|
| 824 |
|
|
* Switch out to protocol's input routine.
|
| 825 |
|
|
*/
|
| 826 |
|
|
ipstat.ips_delivered++;
|
| 827 |
|
|
{
|
| 828 |
|
|
int off = hlen;
|
| 829 |
|
|
|
| 830 |
|
|
(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, off);
|
| 831 |
|
|
#ifdef IPFIREWALL_FORWARD
|
| 832 |
|
|
ip_fw_fwd_addr = NULL; /* tcp needed it */
|
| 833 |
|
|
#endif
|
| 834 |
|
|
return;
|
| 835 |
|
|
}
|
| 836 |
|
|
bad:
|
| 837 |
|
|
#ifdef IPFIREWALL_FORWARD
|
| 838 |
|
|
ip_fw_fwd_addr = NULL;
|
| 839 |
|
|
#endif
|
| 840 |
|
|
m_freem(m);
|
| 841 |
|
|
}
|
| 842 |
|
|
|
| 843 |
|
|
/*
|
| 844 |
|
|
* IP software interrupt routine - to go away sometime soon
|
| 845 |
|
|
*/
|
| 846 |
|
|
static void
|
| 847 |
|
|
ipintr(void)
|
| 848 |
|
|
{
|
| 849 |
|
|
int s;
|
| 850 |
|
|
struct mbuf *m;
|
| 851 |
|
|
|
| 852 |
|
|
while(1) {
|
| 853 |
|
|
s = splimp();
|
| 854 |
|
|
IF_DEQUEUE(&ipintrq, m);
|
| 855 |
|
|
splx(s);
|
| 856 |
|
|
if (m == 0)
|
| 857 |
|
|
return;
|
| 858 |
|
|
ip_input(m);
|
| 859 |
|
|
}
|
| 860 |
|
|
}
|
| 861 |
|
|
|
| 862 |
|
|
/*
|
| 863 |
|
|
* Take incoming datagram fragment and try to reassemble it into
|
| 864 |
|
|
* whole datagram. If a chain for reassembly of this datagram already
|
| 865 |
|
|
* exists, then it is given as fp; otherwise have to make a chain.
|
| 866 |
|
|
*
|
| 867 |
|
|
* When IPDIVERT enabled, keep additional state with each packet that
|
| 868 |
|
|
* tells us if we need to divert or tee the packet we're building.
|
| 869 |
|
|
*/
|
| 870 |
|
|
|
| 871 |
|
|
static struct mbuf *
|
| 872 |
|
|
#ifdef IPDIVERT
|
| 873 |
|
|
ip_reass(m, fp, where, divinfo, divcookie)
|
| 874 |
|
|
#else
|
| 875 |
|
|
ip_reass(m, fp, where)
|
| 876 |
|
|
#endif
|
| 877 |
|
|
register struct mbuf *m;
|
| 878 |
|
|
register struct ipq *fp;
|
| 879 |
|
|
struct ipq *where;
|
| 880 |
|
|
#ifdef IPDIVERT
|
| 881 |
|
|
u_int32_t *divinfo;
|
| 882 |
|
|
u_int16_t *divcookie;
|
| 883 |
|
|
#endif
|
| 884 |
|
|
{
|
| 885 |
|
|
struct ip *ip = mtod(m, struct ip *);
|
| 886 |
|
|
register struct mbuf *p = 0, *q, *nq;
|
| 887 |
|
|
struct mbuf *t;
|
| 888 |
|
|
int hlen = IP_VHL_HL(ip->ip_vhl) << 2;
|
| 889 |
|
|
int i, next;
|
| 890 |
|
|
|
| 891 |
|
|
/*
|
| 892 |
|
|
* Presence of header sizes in mbufs
|
| 893 |
|
|
* would confuse code below.
|
| 894 |
|
|
*/
|
| 895 |
|
|
m->m_data += hlen;
|
| 896 |
|
|
m->m_len -= hlen;
|
| 897 |
|
|
|
| 898 |
|
|
/*
|
| 899 |
|
|
* If first fragment to arrive, create a reassembly queue.
|
| 900 |
|
|
*/
|
| 901 |
|
|
if (fp == 0) {
|
| 902 |
|
|
/*
|
| 903 |
|
|
* Enforce upper bound on number of fragmented packets
|
| 904 |
|
|
* for which we attempt reassembly;
|
| 905 |
|
|
* If maxfrag is 0, never accept fragments.
|
| 906 |
|
|
* If maxfrag is -1, accept all fragments without limitation.
|
| 907 |
|
|
*/
|
| 908 |
|
|
if ((ip_maxfragpackets >= 0) && (ip_nfragpackets >= ip_maxfragpackets))
|
| 909 |
|
|
goto dropfrag;
|
| 910 |
|
|
ip_nfragpackets++;
|
| 911 |
|
|
if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)
|
| 912 |
|
|
goto dropfrag;
|
| 913 |
|
|
fp = mtod(t, struct ipq *);
|
| 914 |
|
|
insque(fp, where);
|
| 915 |
|
|
nipq++;
|
| 916 |
|
|
fp->ipq_ttl = IPFRAGTTL;
|
| 917 |
|
|
fp->ipq_p = ip->ip_p;
|
| 918 |
|
|
fp->ipq_id = ip->ip_id;
|
| 919 |
|
|
fp->ipq_src = ip->ip_src;
|
| 920 |
|
|
fp->ipq_dst = ip->ip_dst;
|
| 921 |
|
|
fp->ipq_frags = m;
|
| 922 |
|
|
m->m_nextpkt = NULL;
|
| 923 |
|
|
#ifdef IPDIVERT
|
| 924 |
|
|
fp->ipq_div_info = 0;
|
| 925 |
|
|
fp->ipq_div_cookie = 0;
|
| 926 |
|
|
#endif
|
| 927 |
|
|
goto inserted;
|
| 928 |
|
|
}
|
| 929 |
|
|
|
| 930 |
|
|
#define GETIP(m) ((struct ip*)((m)->m_pkthdr.header))
|
| 931 |
|
|
|
| 932 |
|
|
/*
|
| 933 |
|
|
* Find a segment which begins after this one does.
|
| 934 |
|
|
*/
|
| 935 |
|
|
for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
|
| 936 |
|
|
if (GETIP(q)->ip_off > ip->ip_off)
|
| 937 |
|
|
break;
|
| 938 |
|
|
|
| 939 |
|
|
/*
|
| 940 |
|
|
* If there is a preceding segment, it may provide some of
|
| 941 |
|
|
* our data already. If so, drop the data from the incoming
|
| 942 |
|
|
* segment. If it provides all of our data, drop us, otherwise
|
| 943 |
|
|
* stick new segment in the proper place.
|
| 944 |
|
|
*
|
| 945 |
|
|
* If some of the data is dropped from the the preceding
|
| 946 |
|
|
* segment, then it's checksum is invalidated.
|
| 947 |
|
|
*/
|
| 948 |
|
|
if (p) {
|
| 949 |
|
|
i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
|
| 950 |
|
|
if (i > 0) {
|
| 951 |
|
|
if (i >= ip->ip_len)
|
| 952 |
|
|
goto dropfrag;
|
| 953 |
|
|
m_adj(m, i);
|
| 954 |
|
|
m->m_pkthdr.csum_flags = 0;
|
| 955 |
|
|
ip->ip_off += i;
|
| 956 |
|
|
ip->ip_len -= i;
|
| 957 |
|
|
}
|
| 958 |
|
|
m->m_nextpkt = p->m_nextpkt;
|
| 959 |
|
|
p->m_nextpkt = m;
|
| 960 |
|
|
} else {
|
| 961 |
|
|
m->m_nextpkt = fp->ipq_frags;
|
| 962 |
|
|
fp->ipq_frags = m;
|
| 963 |
|
|
}
|
| 964 |
|
|
|
| 965 |
|
|
/*
|
| 966 |
|
|
* While we overlap succeeding segments trim them or,
|
| 967 |
|
|
* if they are completely covered, dequeue them.
|
| 968 |
|
|
*/
|
| 969 |
|
|
for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
|
| 970 |
|
|
q = nq) {
|
| 971 |
|
|
i = (ip->ip_off + ip->ip_len) -
|
| 972 |
|
|
GETIP(q)->ip_off;
|
| 973 |
|
|
if (i < GETIP(q)->ip_len) {
|
| 974 |
|
|
GETIP(q)->ip_len -= i;
|
| 975 |
|
|
GETIP(q)->ip_off += i;
|
| 976 |
|
|
m_adj(q, i);
|
| 977 |
|
|
q->m_pkthdr.csum_flags = 0;
|
| 978 |
|
|
break;
|
| 979 |
|
|
}
|
| 980 |
|
|
nq = q->m_nextpkt;
|
| 981 |
|
|
m->m_nextpkt = nq;
|
| 982 |
|
|
m_freem(q);
|
| 983 |
|
|
}
|
| 984 |
|
|
|
| 985 |
|
|
inserted:
|
| 986 |
|
|
|
| 987 |
|
|
#ifdef IPDIVERT
|
| 988 |
|
|
/*
|
| 989 |
|
|
* Transfer firewall instructions to the fragment structure.
|
| 990 |
|
|
* Any fragment diverting causes the whole packet to divert.
|
| 991 |
|
|
*/
|
| 992 |
|
|
fp->ipq_div_info = *divinfo;
|
| 993 |
|
|
fp->ipq_div_cookie = *divcookie;
|
| 994 |
|
|
*divinfo = 0;
|
| 995 |
|
|
*divcookie = 0;
|
| 996 |
|
|
#endif
|
| 997 |
|
|
|
| 998 |
|
|
/*
|
| 999 |
|
|
* Check for complete reassembly.
|
| 1000 |
|
|
*/
|
| 1001 |
|
|
next = 0;
|
| 1002 |
|
|
for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
|
| 1003 |
|
|
if (GETIP(q)->ip_off != next)
|
| 1004 |
|
|
return (0);
|
| 1005 |
|
|
next += GETIP(q)->ip_len;
|
| 1006 |
|
|
}
|
| 1007 |
|
|
/* Make sure the last packet didn't have the IP_MF flag */
|
| 1008 |
|
|
if (p->m_flags & M_FRAG)
|
| 1009 |
|
|
return (0);
|
| 1010 |
|
|
|
| 1011 |
|
|
/*
|
| 1012 |
|
|
* Reassembly is complete. Make sure the packet is a sane size.
|
| 1013 |
|
|
*/
|
| 1014 |
|
|
q = fp->ipq_frags;
|
| 1015 |
|
|
ip = GETIP(q);
|
| 1016 |
|
|
if (next + (IP_VHL_HL(ip->ip_vhl) << 2) > IP_MAXPACKET) {
|
| 1017 |
|
|
ipstat.ips_toolong++;
|
| 1018 |
|
|
ip_freef(fp);
|
| 1019 |
|
|
return (0);
|
| 1020 |
|
|
}
|
| 1021 |
|
|
|
| 1022 |
|
|
/*
|
| 1023 |
|
|
* Concatenate fragments.
|
| 1024 |
|
|
*/
|
| 1025 |
|
|
m = q;
|
| 1026 |
|
|
t = m->m_next;
|
| 1027 |
|
|
m->m_next = 0;
|
| 1028 |
|
|
m_cat(m, t);
|
| 1029 |
|
|
nq = q->m_nextpkt;
|
| 1030 |
|
|
q->m_nextpkt = 0;
|
| 1031 |
|
|
for (q = nq; q != NULL; q = nq) {
|
| 1032 |
|
|
nq = q->m_nextpkt;
|
| 1033 |
|
|
q->m_nextpkt = NULL;
|
| 1034 |
|
|
m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
|
| 1035 |
|
|
m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
|
| 1036 |
|
|
m_cat(m, q);
|
| 1037 |
|
|
}
|
| 1038 |
|
|
|
| 1039 |
|
|
#ifdef IPDIVERT
|
| 1040 |
|
|
/*
|
| 1041 |
|
|
* Extract firewall instructions from the fragment structure.
|
| 1042 |
|
|
*/
|
| 1043 |
|
|
*divinfo = fp->ipq_div_info;
|
| 1044 |
|
|
*divcookie = fp->ipq_div_cookie;
|
| 1045 |
|
|
#endif
|
| 1046 |
|
|
|
| 1047 |
|
|
/*
|
| 1048 |
|
|
* Create header for new ip packet by
|
| 1049 |
|
|
* modifying header of first packet;
|
| 1050 |
|
|
* dequeue and discard fragment reassembly header.
|
| 1051 |
|
|
* Make header visible.
|
| 1052 |
|
|
*/
|
| 1053 |
|
|
ip->ip_len = next;
|
| 1054 |
|
|
ip->ip_src = fp->ipq_src;
|
| 1055 |
|
|
ip->ip_dst = fp->ipq_dst;
|
| 1056 |
|
|
remque(fp);
|
| 1057 |
|
|
nipq--;
|
| 1058 |
|
|
(void) m_free(dtom(fp));
|
| 1059 |
|
|
ip_nfragpackets--;
|
| 1060 |
|
|
m->m_len += (IP_VHL_HL(ip->ip_vhl) << 2);
|
| 1061 |
|
|
m->m_data -= (IP_VHL_HL(ip->ip_vhl) << 2);
|
| 1062 |
|
|
/* some debugging cruft by sklower, below, will go away soon */
|
| 1063 |
|
|
if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
|
| 1064 |
|
|
register int plen = 0;
|
| 1065 |
|
|
for (t = m; t; t = t->m_next)
|
| 1066 |
|
|
plen += t->m_len;
|
| 1067 |
|
|
m->m_pkthdr.len = plen;
|
| 1068 |
|
|
}
|
| 1069 |
|
|
return (m);
|
| 1070 |
|
|
|
| 1071 |
|
|
dropfrag:
|
| 1072 |
|
|
#ifdef IPDIVERT
|
| 1073 |
|
|
*divinfo = 0;
|
| 1074 |
|
|
*divcookie = 0;
|
| 1075 |
|
|
#endif
|
| 1076 |
|
|
ipstat.ips_fragdropped++;
|
| 1077 |
|
|
m_freem(m);
|
| 1078 |
|
|
return (0);
|
| 1079 |
|
|
|
| 1080 |
|
|
#undef GETIP
|
| 1081 |
|
|
}
|
| 1082 |
|
|
|
| 1083 |
|
|
/*
|
| 1084 |
|
|
* Free a fragment reassembly header and all
|
| 1085 |
|
|
* associated datagrams.
|
| 1086 |
|
|
*/
|
| 1087 |
|
|
static void
|
| 1088 |
|
|
ip_freef(fp)
|
| 1089 |
|
|
struct ipq *fp;
|
| 1090 |
|
|
{
|
| 1091 |
|
|
register struct mbuf *q;
|
| 1092 |
|
|
|
| 1093 |
|
|
while (fp->ipq_frags) {
|
| 1094 |
|
|
q = fp->ipq_frags;
|
| 1095 |
|
|
fp->ipq_frags = q->m_nextpkt;
|
| 1096 |
|
|
m_freem(q);
|
| 1097 |
|
|
}
|
| 1098 |
|
|
remque(fp);
|
| 1099 |
|
|
(void) m_free(dtom(fp));
|
| 1100 |
|
|
ip_nfragpackets--;
|
| 1101 |
|
|
nipq--;
|
| 1102 |
|
|
}
|
| 1103 |
|
|
|
| 1104 |
|
|
/*
|
| 1105 |
|
|
* IP timer processing;
|
| 1106 |
|
|
* if a timer expires on a reassembly
|
| 1107 |
|
|
* queue, discard it.
|
| 1108 |
|
|
*/
|
| 1109 |
|
|
void
|
| 1110 |
|
|
ip_slowtimo()
|
| 1111 |
|
|
{
|
| 1112 |
|
|
register struct ipq *fp;
|
| 1113 |
|
|
int s = splnet();
|
| 1114 |
|
|
int i;
|
| 1115 |
|
|
|
| 1116 |
|
|
for (i = 0; i < IPREASS_NHASH; i++) {
|
| 1117 |
|
|
fp = ipq[i].next;
|
| 1118 |
|
|
if (fp == 0)
|
| 1119 |
|
|
continue;
|
| 1120 |
|
|
while (fp != &ipq[i]) {
|
| 1121 |
|
|
--fp->ipq_ttl;
|
| 1122 |
|
|
fp = fp->next;
|
| 1123 |
|
|
if (fp->prev->ipq_ttl == 0) {
|
| 1124 |
|
|
ipstat.ips_fragtimeout++;
|
| 1125 |
|
|
ip_freef(fp->prev);
|
| 1126 |
|
|
}
|
| 1127 |
|
|
}
|
| 1128 |
|
|
}
|
| 1129 |
|
|
/*
|
| 1130 |
|
|
* If we are over the maximum number of fragments
|
| 1131 |
|
|
* (due to the limit being lowered), drain off
|
| 1132 |
|
|
* enough to get down to the new limit.
|
| 1133 |
|
|
*/
|
| 1134 |
|
|
for (i = 0; i < IPREASS_NHASH; i++) {
|
| 1135 |
|
|
if (ip_maxfragpackets >= 0) {
|
| 1136 |
|
|
while ((ip_nfragpackets > ip_maxfragpackets) &&
|
| 1137 |
|
|
(ipq[i].next != &ipq[i])) {
|
| 1138 |
|
|
ipstat.ips_fragdropped++;
|
| 1139 |
|
|
ip_freef(ipq[i].next);
|
| 1140 |
|
|
}
|
| 1141 |
|
|
}
|
| 1142 |
|
|
}
|
| 1143 |
|
|
ipflow_slowtimo();
|
| 1144 |
|
|
splx(s);
|
| 1145 |
|
|
}
|
| 1146 |
|
|
|
| 1147 |
|
|
/*
|
| 1148 |
|
|
* Drain off all datagram fragments.
|
| 1149 |
|
|
*/
|
| 1150 |
|
|
void
|
| 1151 |
|
|
ip_drain()
|
| 1152 |
|
|
{
|
| 1153 |
|
|
int i;
|
| 1154 |
|
|
|
| 1155 |
|
|
for (i = 0; i < IPREASS_NHASH; i++) {
|
| 1156 |
|
|
while (ipq[i].next != &ipq[i]) {
|
| 1157 |
|
|
ipstat.ips_fragdropped++;
|
| 1158 |
|
|
ip_freef(ipq[i].next);
|
| 1159 |
|
|
}
|
| 1160 |
|
|
}
|
| 1161 |
|
|
in_rtqdrain();
|
| 1162 |
|
|
}
|
| 1163 |
|
|
|
| 1164 |
|
|
/*
|
| 1165 |
|
|
* Do option processing on a datagram,
|
| 1166 |
|
|
* possibly discarding it if bad options are encountered,
|
| 1167 |
|
|
* or forwarding it if source-routed.
|
| 1168 |
|
|
* Returns 1 if packet has been forwarded/freed,
|
| 1169 |
|
|
* 0 if the packet should be processed further.
|
| 1170 |
|
|
*/
|
| 1171 |
|
|
static int
|
| 1172 |
|
|
ip_dooptions(m)
|
| 1173 |
|
|
struct mbuf *m;
|
| 1174 |
|
|
{
|
| 1175 |
|
|
register struct ip *ip = mtod(m, struct ip *);
|
| 1176 |
|
|
register u_char *cp;
|
| 1177 |
|
|
register struct ip_timestamp *ipt;
|
| 1178 |
|
|
register struct in_ifaddr *ia;
|
| 1179 |
|
|
int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
|
| 1180 |
|
|
struct in_addr *sin, dst;
|
| 1181 |
|
|
n_time ntime;
|
| 1182 |
|
|
|
| 1183 |
|
|
dst = ip->ip_dst;
|
| 1184 |
|
|
cp = (u_char *)(ip + 1);
|
| 1185 |
|
|
cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
|
| 1186 |
|
|
for (; cnt > 0; cnt -= optlen, cp += optlen) {
|
| 1187 |
|
|
opt = cp[IPOPT_OPTVAL];
|
| 1188 |
|
|
if (opt == IPOPT_EOL)
|
| 1189 |
|
|
break;
|
| 1190 |
|
|
if (opt == IPOPT_NOP)
|
| 1191 |
|
|
optlen = 1;
|
| 1192 |
|
|
else {
|
| 1193 |
|
|
if (cnt < IPOPT_OLEN + sizeof(*cp)) {
|
| 1194 |
|
|
code = &cp[IPOPT_OLEN] - (u_char *)ip;
|
| 1195 |
|
|
goto bad;
|
| 1196 |
|
|
}
|
| 1197 |
|
|
optlen = cp[IPOPT_OLEN];
|
| 1198 |
|
|
if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
|
| 1199 |
|
|
code = &cp[IPOPT_OLEN] - (u_char *)ip;
|
| 1200 |
|
|
goto bad;
|
| 1201 |
|
|
}
|
| 1202 |
|
|
}
|
| 1203 |
|
|
switch (opt) {
|
| 1204 |
|
|
|
| 1205 |
|
|
default:
|
| 1206 |
|
|
break;
|
| 1207 |
|
|
|
| 1208 |
|
|
/*
|
| 1209 |
|
|
* Source routing with record.
|
| 1210 |
|
|
* Find interface with current destination address.
|
| 1211 |
|
|
* If none on this machine then drop if strictly routed,
|
| 1212 |
|
|
* or do nothing if loosely routed.
|
| 1213 |
|
|
* Record interface address and bring up next address
|
| 1214 |
|
|
* component. If strictly routed make sure next
|
| 1215 |
|
|
* address is on directly accessible net.
|
| 1216 |
|
|
*/
|
| 1217 |
|
|
case IPOPT_LSRR:
|
| 1218 |
|
|
case IPOPT_SSRR:
|
| 1219 |
|
|
if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
|
| 1220 |
|
|
code = &cp[IPOPT_OLEN] - (u_char *)ip;
|
| 1221 |
|
|
goto bad;
|
| 1222 |
|
|
}
|
| 1223 |
|
|
if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
|
| 1224 |
|
|
code = &cp[IPOPT_OFFSET] - (u_char *)ip;
|
| 1225 |
|
|
goto bad;
|
| 1226 |
|
|
}
|
| 1227 |
|
|
ipaddr.sin_addr = ip->ip_dst;
|
| 1228 |
|
|
ia = (struct in_ifaddr *)
|
| 1229 |
|
|
ifa_ifwithaddr((struct sockaddr *)&ipaddr);
|
| 1230 |
|
|
if (ia == 0) {
|
| 1231 |
|
|
if (opt == IPOPT_SSRR) {
|
| 1232 |
|
|
type = ICMP_UNREACH;
|
| 1233 |
|
|
code = ICMP_UNREACH_SRCFAIL;
|
| 1234 |
|
|
goto bad;
|
| 1235 |
|
|
}
|
| 1236 |
|
|
if (!ip_dosourceroute)
|
| 1237 |
|
|
goto nosourcerouting;
|
| 1238 |
|
|
/*
|
| 1239 |
|
|
* Loose routing, and not at next destination
|
| 1240 |
|
|
* yet; nothing to do except forward.
|
| 1241 |
|
|
*/
|
| 1242 |
|
|
break;
|
| 1243 |
|
|
}
|
| 1244 |
|
|
off--; /* 0 origin */
|
| 1245 |
|
|
if (off > optlen - (int)sizeof(struct in_addr)) {
|
| 1246 |
|
|
/*
|
| 1247 |
|
|
* End of source route. Should be for us.
|
| 1248 |
|
|
*/
|
| 1249 |
|
|
if (!ip_acceptsourceroute)
|
| 1250 |
|
|
goto nosourcerouting;
|
| 1251 |
|
|
save_rte(cp, ip->ip_src);
|
| 1252 |
|
|
break;
|
| 1253 |
|
|
}
|
| 1254 |
|
|
|
| 1255 |
|
|
if (!ip_dosourceroute) {
|
| 1256 |
|
|
if (ipforwarding) {
|
| 1257 |
|
|
char buf[16]; /* aaa.bbb.ccc.ddd\0 */
|
| 1258 |
|
|
/*
|
| 1259 |
|
|
* Acting as a router, so generate ICMP
|
| 1260 |
|
|
*/
|
| 1261 |
|
|
nosourcerouting:
|
| 1262 |
|
|
strcpy(buf, inet_ntoa(ip->ip_dst));
|
| 1263 |
|
|
log(LOG_WARNING,
|
| 1264 |
|
|
"attempted source route from %s to %s\n",
|
| 1265 |
|
|
inet_ntoa(ip->ip_src), buf);
|
| 1266 |
|
|
type = ICMP_UNREACH;
|
| 1267 |
|
|
code = ICMP_UNREACH_SRCFAIL;
|
| 1268 |
|
|
goto bad;
|
| 1269 |
|
|
} else {
|
| 1270 |
|
|
/*
|
| 1271 |
|
|
* Not acting as a router, so silently drop.
|
| 1272 |
|
|
*/
|
| 1273 |
|
|
ipstat.ips_cantforward++;
|
| 1274 |
|
|
m_freem(m);
|
| 1275 |
|
|
return (1);
|
| 1276 |
|
|
}
|
| 1277 |
|
|
}
|
| 1278 |
|
|
|
| 1279 |
|
|
/*
|
| 1280 |
|
|
* locate outgoing interface
|
| 1281 |
|
|
*/
|
| 1282 |
|
|
(void)memcpy(&ipaddr.sin_addr, cp + off,
|
| 1283 |
|
|
sizeof(ipaddr.sin_addr));
|
| 1284 |
|
|
|
| 1285 |
|
|
if (opt == IPOPT_SSRR) {
|
| 1286 |
|
|
#define INA struct in_ifaddr *
|
| 1287 |
|
|
#define SA struct sockaddr *
|
| 1288 |
|
|
if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
|
| 1289 |
|
|
ia = (INA)ifa_ifwithnet((SA)&ipaddr);
|
| 1290 |
|
|
} else
|
| 1291 |
|
|
ia = ip_rtaddr(ipaddr.sin_addr);
|
| 1292 |
|
|
if (ia == 0) {
|
| 1293 |
|
|
type = ICMP_UNREACH;
|
| 1294 |
|
|
code = ICMP_UNREACH_SRCFAIL;
|
| 1295 |
|
|
goto bad;
|
| 1296 |
|
|
}
|
| 1297 |
|
|
ip->ip_dst = ipaddr.sin_addr;
|
| 1298 |
|
|
(void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
|
| 1299 |
|
|
sizeof(struct in_addr));
|
| 1300 |
|
|
cp[IPOPT_OFFSET] += sizeof(struct in_addr);
|
| 1301 |
|
|
/*
|
| 1302 |
|
|
* Let ip_intr's mcast routing check handle mcast pkts
|
| 1303 |
|
|
*/
|
| 1304 |
|
|
forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
|
| 1305 |
|
|
break;
|
| 1306 |
|
|
|
| 1307 |
|
|
case IPOPT_RR:
|
| 1308 |
|
|
if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
|
| 1309 |
|
|
code = &cp[IPOPT_OFFSET] - (u_char *)ip;
|
| 1310 |
|
|
goto bad;
|
| 1311 |
|
|
}
|
| 1312 |
|
|
if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
|
| 1313 |
|
|
code = &cp[IPOPT_OFFSET] - (u_char *)ip;
|
| 1314 |
|
|
goto bad;
|
| 1315 |
|
|
}
|
| 1316 |
|
|
/*
|
| 1317 |
|
|
* If no space remains, ignore.
|
| 1318 |
|
|
*/
|
| 1319 |
|
|
off--; /* 0 origin */
|
| 1320 |
|
|
if (off > optlen - (int)sizeof(struct in_addr))
|
| 1321 |
|
|
break;
|
| 1322 |
|
|
(void)memcpy(&ipaddr.sin_addr, &ip->ip_dst,
|
| 1323 |
|
|
sizeof(ipaddr.sin_addr));
|
| 1324 |
|
|
/*
|
| 1325 |
|
|
* locate outgoing interface; if we're the destination,
|
| 1326 |
|
|
* use the incoming interface (should be same).
|
| 1327 |
|
|
*/
|
| 1328 |
|
|
if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
|
| 1329 |
|
|
(ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
|
| 1330 |
|
|
type = ICMP_UNREACH;
|
| 1331 |
|
|
code = ICMP_UNREACH_HOST;
|
| 1332 |
|
|
goto bad;
|
| 1333 |
|
|
}
|
| 1334 |
|
|
(void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
|
| 1335 |
|
|
sizeof(struct in_addr));
|
| 1336 |
|
|
cp[IPOPT_OFFSET] += sizeof(struct in_addr);
|
| 1337 |
|
|
break;
|
| 1338 |
|
|
|
| 1339 |
|
|
case IPOPT_TS:
|
| 1340 |
|
|
code = cp - (u_char *)ip;
|
| 1341 |
|
|
ipt = (struct ip_timestamp *)cp;
|
| 1342 |
|
|
if (ipt->ipt_len < 4 || ipt->ipt_len > 40) {
|
| 1343 |
|
|
code = (u_char *)&ipt->ipt_len - (u_char *)ip;
|
| 1344 |
|
|
goto bad;
|
| 1345 |
|
|
}
|
| 1346 |
|
|
if (ipt->ipt_ptr < 5) {
|
| 1347 |
|
|
code = (u_char *)&ipt->ipt_ptr - (u_char *)ip;
|
| 1348 |
|
|
goto bad;
|
| 1349 |
|
|
}
|
| 1350 |
|
|
if (ipt->ipt_ptr >
|
| 1351 |
|
|
ipt->ipt_len - (int)sizeof(int32_t)) {
|
| 1352 |
|
|
if (++ipt->ipt_oflw == 0) {
|
| 1353 |
|
|
code = (u_char *)&ipt->ipt_ptr -
|
| 1354 |
|
|
(u_char *)ip;
|
| 1355 |
|
|
goto bad;
|
| 1356 |
|
|
}
|
| 1357 |
|
|
break;
|
| 1358 |
|
|
}
|
| 1359 |
|
|
sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
|
| 1360 |
|
|
switch (ipt->ipt_flg) {
|
| 1361 |
|
|
|
| 1362 |
|
|
case IPOPT_TS_TSONLY:
|
| 1363 |
|
|
break;
|
| 1364 |
|
|
|
| 1365 |
|
|
case IPOPT_TS_TSANDADDR:
|
| 1366 |
|
|
if (ipt->ipt_ptr - 1 + sizeof(n_time) +
|
| 1367 |
|
|
sizeof(struct in_addr) > ipt->ipt_len) {
|
| 1368 |
|
|
code = (u_char *)&ipt->ipt_ptr -
|
| 1369 |
|
|
(u_char *)ip;
|
| 1370 |
|
|
goto bad;
|
| 1371 |
|
|
}
|
| 1372 |
|
|
ipaddr.sin_addr = dst;
|
| 1373 |
|
|
ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
|
| 1374 |
|
|
m->m_pkthdr.rcvif);
|
| 1375 |
|
|
if (ia == 0)
|
| 1376 |
|
|
continue;
|
| 1377 |
|
|
(void)memcpy(sin, &IA_SIN(ia)->sin_addr,
|
| 1378 |
|
|
sizeof(struct in_addr));
|
| 1379 |
|
|
ipt->ipt_ptr += sizeof(struct in_addr);
|
| 1380 |
|
|
break;
|
| 1381 |
|
|
|
| 1382 |
|
|
case IPOPT_TS_PRESPEC:
|
| 1383 |
|
|
if (ipt->ipt_ptr - 1 + sizeof(n_time) +
|
| 1384 |
|
|
sizeof(struct in_addr) > ipt->ipt_len) {
|
| 1385 |
|
|
code = (u_char *)&ipt->ipt_ptr -
|
| 1386 |
|
|
(u_char *)ip;
|
| 1387 |
|
|
goto bad;
|
| 1388 |
|
|
}
|
| 1389 |
|
|
(void)memcpy(&ipaddr.sin_addr, sin,
|
| 1390 |
|
|
sizeof(struct in_addr));
|
| 1391 |
|
|
if (ifa_ifwithaddr((SA)&ipaddr) == 0)
|
| 1392 |
|
|
continue;
|
| 1393 |
|
|
ipt->ipt_ptr += sizeof(struct in_addr);
|
| 1394 |
|
|
break;
|
| 1395 |
|
|
|
| 1396 |
|
|
default:
|
| 1397 |
|
|
/* XXX can't take &ipt->ipt_flg */
|
| 1398 |
|
|
code = (u_char *)&ipt->ipt_ptr -
|
| 1399 |
|
|
(u_char *)ip + 1;
|
| 1400 |
|
|
goto bad;
|
| 1401 |
|
|
}
|
| 1402 |
|
|
ntime = iptime();
|
| 1403 |
|
|
(void)memcpy(cp + ipt->ipt_ptr - 1, &ntime,
|
| 1404 |
|
|
sizeof(n_time));
|
| 1405 |
|
|
ipt->ipt_ptr += sizeof(n_time);
|
| 1406 |
|
|
}
|
| 1407 |
|
|
}
|
| 1408 |
|
|
if (forward && ipforwarding) {
|
| 1409 |
|
|
ip_forward(m, 1);
|
| 1410 |
|
|
return (1);
|
| 1411 |
|
|
}
|
| 1412 |
|
|
return (0);
|
| 1413 |
|
|
bad:
|
| 1414 |
|
|
icmp_error(m, type, code, 0, 0);
|
| 1415 |
|
|
ipstat.ips_badoptions++;
|
| 1416 |
|
|
return (1);
|
| 1417 |
|
|
}
|
| 1418 |
|
|
|
| 1419 |
|
|
/*
|
| 1420 |
|
|
* Given address of next destination (final or next hop),
|
| 1421 |
|
|
* return internet address info of interface to be used to get there.
|
| 1422 |
|
|
*/
|
| 1423 |
|
|
static struct in_ifaddr *
|
| 1424 |
|
|
ip_rtaddr(dst)
|
| 1425 |
|
|
struct in_addr dst;
|
| 1426 |
|
|
{
|
| 1427 |
|
|
register struct sockaddr_in *sin;
|
| 1428 |
|
|
|
| 1429 |
|
|
sin = (struct sockaddr_in *) &ipforward_rt.ro_dst;
|
| 1430 |
|
|
|
| 1431 |
|
|
if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) {
|
| 1432 |
|
|
if (ipforward_rt.ro_rt) {
|
| 1433 |
|
|
RTFREE(ipforward_rt.ro_rt);
|
| 1434 |
|
|
ipforward_rt.ro_rt = 0;
|
| 1435 |
|
|
}
|
| 1436 |
|
|
sin->sin_family = AF_INET;
|
| 1437 |
|
|
sin->sin_len = sizeof(*sin);
|
| 1438 |
|
|
sin->sin_addr = dst;
|
| 1439 |
|
|
|
| 1440 |
|
|
rtalloc_ign(&ipforward_rt, RTF_PRCLONING);
|
| 1441 |
|
|
}
|
| 1442 |
|
|
if (ipforward_rt.ro_rt == 0)
|
| 1443 |
|
|
return ((struct in_ifaddr *)0);
|
| 1444 |
|
|
return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa);
|
| 1445 |
|
|
}
|
| 1446 |
|
|
|
| 1447 |
|
|
/*
|
| 1448 |
|
|
* Save incoming source route for use in replies,
|
| 1449 |
|
|
* to be picked up later by ip_srcroute if the receiver is interested.
|
| 1450 |
|
|
*/
|
| 1451 |
|
|
void
|
| 1452 |
|
|
save_rte(option, dst)
|
| 1453 |
|
|
u_char *option;
|
| 1454 |
|
|
struct in_addr dst;
|
| 1455 |
|
|
{
|
| 1456 |
|
|
unsigned olen;
|
| 1457 |
|
|
|
| 1458 |
|
|
olen = option[IPOPT_OLEN];
|
| 1459 |
|
|
#ifdef DIAGNOSTIC
|
| 1460 |
|
|
if (ipprintfs)
|
| 1461 |
|
|
printf("save_rte: olen %d\n", olen);
|
| 1462 |
|
|
#endif
|
| 1463 |
|
|
if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
|
| 1464 |
|
|
return;
|
| 1465 |
|
|
bcopy(option, ip_srcrt.srcopt, olen);
|
| 1466 |
|
|
ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
|
| 1467 |
|
|
ip_srcrt.dst = dst;
|
| 1468 |
|
|
}
|
| 1469 |
|
|
|
| 1470 |
|
|
/*
|
| 1471 |
|
|
* Retrieve incoming source route for use in replies,
|
| 1472 |
|
|
* in the same form used by setsockopt.
|
| 1473 |
|
|
* The first hop is placed before the options, will be removed later.
|
| 1474 |
|
|
*/
|
| 1475 |
|
|
struct mbuf *
|
| 1476 |
|
|
ip_srcroute()
|
| 1477 |
|
|
{
|
| 1478 |
|
|
register struct in_addr *p, *q;
|
| 1479 |
|
|
register struct mbuf *m;
|
| 1480 |
|
|
|
| 1481 |
|
|
if (ip_nhops == 0)
|
| 1482 |
|
|
return ((struct mbuf *)0);
|
| 1483 |
|
|
m = m_get(M_DONTWAIT, MT_HEADER);
|
| 1484 |
|
|
if (m == 0)
|
| 1485 |
|
|
return ((struct mbuf *)0);
|
| 1486 |
|
|
|
| 1487 |
|
|
#define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
|
| 1488 |
|
|
|
| 1489 |
|
|
/* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
|
| 1490 |
|
|
m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
|
| 1491 |
|
|
OPTSIZ;
|
| 1492 |
|
|
#ifdef DIAGNOSTIC
|
| 1493 |
|
|
if (ipprintfs)
|
| 1494 |
|
|
printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
|
| 1495 |
|
|
#endif
|
| 1496 |
|
|
|
| 1497 |
|
|
/*
|
| 1498 |
|
|
* First save first hop for return route
|
| 1499 |
|
|
*/
|
| 1500 |
|
|
p = &ip_srcrt.route[ip_nhops - 1];
|
| 1501 |
|
|
*(mtod(m, struct in_addr *)) = *p--;
|
| 1502 |
|
|
#ifdef DIAGNOSTIC
|
| 1503 |
|
|
if (ipprintfs)
|
| 1504 |
|
|
printf(" hops %lx", (u_long)ntohl(mtod(m, struct in_addr *)->s_addr));
|
| 1505 |
|
|
#endif
|
| 1506 |
|
|
|
| 1507 |
|
|
/*
|
| 1508 |
|
|
* Copy option fields and padding (nop) to mbuf.
|
| 1509 |
|
|
*/
|
| 1510 |
|
|
ip_srcrt.nop = IPOPT_NOP;
|
| 1511 |
|
|
ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
|
| 1512 |
|
|
(void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr),
|
| 1513 |
|
|
&ip_srcrt.nop, OPTSIZ);
|
| 1514 |
|
|
q = (struct in_addr *)(mtod(m, caddr_t) +
|
| 1515 |
|
|
sizeof(struct in_addr) + OPTSIZ);
|
| 1516 |
|
|
#undef OPTSIZ
|
| 1517 |
|
|
/*
|
| 1518 |
|
|
* Record return path as an IP source route,
|
| 1519 |
|
|
* reversing the path (pointers are now aligned).
|
| 1520 |
|
|
*/
|
| 1521 |
|
|
while (p >= ip_srcrt.route) {
|
| 1522 |
|
|
#ifdef DIAGNOSTIC
|
| 1523 |
|
|
if (ipprintfs)
|
| 1524 |
|
|
printf(" %lx", (u_long)ntohl(q->s_addr));
|
| 1525 |
|
|
#endif
|
| 1526 |
|
|
*q++ = *p--;
|
| 1527 |
|
|
}
|
| 1528 |
|
|
/*
|
| 1529 |
|
|
* Last hop goes to final destination.
|
| 1530 |
|
|
*/
|
| 1531 |
|
|
*q = ip_srcrt.dst;
|
| 1532 |
|
|
#ifdef DIAGNOSTIC
|
| 1533 |
|
|
if (ipprintfs)
|
| 1534 |
|
|
printf(" %lx\n", (u_long)ntohl(q->s_addr));
|
| 1535 |
|
|
#endif
|
| 1536 |
|
|
return (m);
|
| 1537 |
|
|
}
|
| 1538 |
|
|
|
| 1539 |
|
|
/*
|
| 1540 |
|
|
* Strip out IP options, at higher
|
| 1541 |
|
|
* level protocol in the kernel.
|
| 1542 |
|
|
* Second argument is buffer to which options
|
| 1543 |
|
|
* will be moved, and return value is their length.
|
| 1544 |
|
|
* XXX should be deleted; last arg currently ignored.
|
| 1545 |
|
|
*/
|
| 1546 |
|
|
void
|
| 1547 |
|
|
ip_stripoptions(m, mopt)
|
| 1548 |
|
|
register struct mbuf *m;
|
| 1549 |
|
|
struct mbuf *mopt;
|
| 1550 |
|
|
{
|
| 1551 |
|
|
register int i;
|
| 1552 |
|
|
struct ip *ip = mtod(m, struct ip *);
|
| 1553 |
|
|
register caddr_t opts;
|
| 1554 |
|
|
int olen;
|
| 1555 |
|
|
|
| 1556 |
|
|
olen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
|
| 1557 |
|
|
opts = (caddr_t)(ip + 1);
|
| 1558 |
|
|
i = m->m_len - (sizeof (struct ip) + olen);
|
| 1559 |
|
|
bcopy(opts + olen, opts, (unsigned)i);
|
| 1560 |
|
|
m->m_len -= olen;
|
| 1561 |
|
|
if (m->m_flags & M_PKTHDR)
|
| 1562 |
|
|
m->m_pkthdr.len -= olen;
|
| 1563 |
|
|
ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2);
|
| 1564 |
|
|
}
|
| 1565 |
|
|
|
| 1566 |
|
|
int inetctlerrmap[PRC_NCMDS] = {
|
| 1567 |
|
|
0, 0, 0, 0,
|
| 1568 |
|
|
0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
|
| 1569 |
|
|
EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
|
| 1570 |
|
|
EMSGSIZE, EHOSTUNREACH, 0, 0,
|
| 1571 |
|
|
0, 0, 0, 0,
|
| 1572 |
|
|
ENOPROTOOPT, ECONNREFUSED
|
| 1573 |
|
|
};
|
| 1574 |
|
|
|
| 1575 |
|
|
/*
|
| 1576 |
|
|
* Forward a packet. If some error occurs return the sender
|
| 1577 |
|
|
* an icmp packet. Note we can't always generate a meaningful
|
| 1578 |
|
|
* icmp message because icmp doesn't have a large enough repertoire
|
| 1579 |
|
|
* of codes and types.
|
| 1580 |
|
|
*
|
| 1581 |
|
|
* If not forwarding, just drop the packet. This could be confusing
|
| 1582 |
|
|
* if ipforwarding was zero but some routing protocol was advancing
|
| 1583 |
|
|
* us as a gateway to somewhere. However, we must let the routing
|
| 1584 |
|
|
* protocol deal with that.
|
| 1585 |
|
|
*
|
| 1586 |
|
|
* The srcrt parameter indicates whether the packet is being forwarded
|
| 1587 |
|
|
* via a source route.
|
| 1588 |
|
|
*/
|
| 1589 |
|
|
#ifdef NATPT
|
| 1590 |
|
|
void
|
| 1591 |
|
|
#else
|
| 1592 |
|
|
static void
|
| 1593 |
|
|
#endif
|
| 1594 |
|
|
ip_forward(m, srcrt)
|
| 1595 |
|
|
struct mbuf *m;
|
| 1596 |
|
|
int srcrt;
|
| 1597 |
|
|
{
|
| 1598 |
|
|
register struct ip *ip = mtod(m, struct ip *);
|
| 1599 |
|
|
register struct sockaddr_in *sin;
|
| 1600 |
|
|
register struct rtentry *rt;
|
| 1601 |
|
|
int error, type = 0, code = 0;
|
| 1602 |
|
|
struct mbuf *mcopy;
|
| 1603 |
|
|
n_long dest;
|
| 1604 |
|
|
struct ifnet *destifp;
|
| 1605 |
|
|
#ifdef IPSEC
|
| 1606 |
|
|
struct ifnet dummyifp;
|
| 1607 |
|
|
#endif
|
| 1608 |
|
|
|
| 1609 |
|
|
dest = 0;
|
| 1610 |
|
|
#ifdef DIAGNOSTIC
|
| 1611 |
|
|
if (ipprintfs)
|
| 1612 |
|
|
printf("forward: src %lx dst %lx ttl %x\n",
|
| 1613 |
|
|
(u_long)ip->ip_src.s_addr, (u_long)ip->ip_dst.s_addr,
|
| 1614 |
|
|
ip->ip_ttl);
|
| 1615 |
|
|
#endif
|
| 1616 |
|
|
|
| 1617 |
|
|
|
| 1618 |
|
|
if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
|
| 1619 |
|
|
ipstat.ips_cantforward++;
|
| 1620 |
|
|
m_freem(m);
|
| 1621 |
|
|
return;
|
| 1622 |
|
|
}
|
| 1623 |
|
|
#ifdef IPSTEALTH
|
| 1624 |
|
|
if (!ipstealth) {
|
| 1625 |
|
|
#endif
|
| 1626 |
|
|
if (ip->ip_ttl <= IPTTLDEC) {
|
| 1627 |
|
|
icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
|
| 1628 |
|
|
dest, 0);
|
| 1629 |
|
|
return;
|
| 1630 |
|
|
}
|
| 1631 |
|
|
#ifdef IPSTEALTH
|
| 1632 |
|
|
}
|
| 1633 |
|
|
#endif
|
| 1634 |
|
|
|
| 1635 |
|
|
sin = (struct sockaddr_in *)&ipforward_rt.ro_dst;
|
| 1636 |
|
|
if ((rt = ipforward_rt.ro_rt) == 0 ||
|
| 1637 |
|
|
ip->ip_dst.s_addr != sin->sin_addr.s_addr) {
|
| 1638 |
|
|
if (ipforward_rt.ro_rt) {
|
| 1639 |
|
|
RTFREE(ipforward_rt.ro_rt);
|
| 1640 |
|
|
ipforward_rt.ro_rt = 0;
|
| 1641 |
|
|
}
|
| 1642 |
|
|
sin->sin_family = AF_INET;
|
| 1643 |
|
|
sin->sin_len = sizeof(*sin);
|
| 1644 |
|
|
sin->sin_addr = ip->ip_dst;
|
| 1645 |
|
|
|
| 1646 |
|
|
rtalloc_ign(&ipforward_rt, RTF_PRCLONING);
|
| 1647 |
|
|
if (ipforward_rt.ro_rt == 0) {
|
| 1648 |
|
|
icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
|
| 1649 |
|
|
return;
|
| 1650 |
|
|
}
|
| 1651 |
|
|
rt = ipforward_rt.ro_rt;
|
| 1652 |
|
|
}
|
| 1653 |
|
|
|
| 1654 |
|
|
/*
|
| 1655 |
|
|
* Save the IP header and at most 8 bytes of the payload,
|
| 1656 |
|
|
* in case we need to generate an ICMP message to the src.
|
| 1657 |
|
|
*
|
| 1658 |
|
|
* We don't use m_copy() because it might return a reference
|
| 1659 |
|
|
* to a shared cluster. Both this function and ip_output()
|
| 1660 |
|
|
* assume exclusive access to the IP header in `m', so any
|
| 1661 |
|
|
* data in a cluster may change before we reach icmp_error().
|
| 1662 |
|
|
*/
|
| 1663 |
|
|
MGET(mcopy, M_DONTWAIT, m->m_type);
|
| 1664 |
|
|
if (mcopy != NULL) {
|
| 1665 |
|
|
M_COPY_PKTHDR(mcopy, m);
|
| 1666 |
|
|
mcopy->m_len = imin((IP_VHL_HL(ip->ip_vhl) << 2) + 8,
|
| 1667 |
|
|
(int)ip->ip_len);
|
| 1668 |
|
|
m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
|
| 1669 |
|
|
}
|
| 1670 |
|
|
|
| 1671 |
|
|
#ifdef IPSTEALTH
|
| 1672 |
|
|
if (!ipstealth) {
|
| 1673 |
|
|
#endif
|
| 1674 |
|
|
ip->ip_ttl -= IPTTLDEC;
|
| 1675 |
|
|
#ifdef IPSTEALTH
|
| 1676 |
|
|
}
|
| 1677 |
|
|
#endif
|
| 1678 |
|
|
|
| 1679 |
|
|
/*
|
| 1680 |
|
|
* If forwarding packet using same interface that it came in on,
|
| 1681 |
|
|
* perhaps should send a redirect to sender to shortcut a hop.
|
| 1682 |
|
|
* Only send redirect if source is sending directly to us,
|
| 1683 |
|
|
* and if packet was not source routed (or has any options).
|
| 1684 |
|
|
* Also, don't send redirect if forwarding using a default route
|
| 1685 |
|
|
* or a route modified by a redirect.
|
| 1686 |
|
|
*/
|
| 1687 |
|
|
#define satosin(sa) ((struct sockaddr_in *)(sa))
|
| 1688 |
|
|
if (rt->rt_ifp == m->m_pkthdr.rcvif &&
|
| 1689 |
|
|
(rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
|
| 1690 |
|
|
satosin(rt_key(rt))->sin_addr.s_addr != 0 &&
|
| 1691 |
|
|
ipsendredirects && !srcrt) {
|
| 1692 |
|
|
#define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa))
|
| 1693 |
|
|
u_long src = ntohl(ip->ip_src.s_addr);
|
| 1694 |
|
|
|
| 1695 |
|
|
if (RTA(rt) &&
|
| 1696 |
|
|
(src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
|
| 1697 |
|
|
if (rt->rt_flags & RTF_GATEWAY)
|
| 1698 |
|
|
dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
|
| 1699 |
|
|
else
|
| 1700 |
|
|
dest = ip->ip_dst.s_addr;
|
| 1701 |
|
|
/* Router requirements says to only send host redirects */
|
| 1702 |
|
|
type = ICMP_REDIRECT;
|
| 1703 |
|
|
code = ICMP_REDIRECT_HOST;
|
| 1704 |
|
|
#ifdef DIAGNOSTIC
|
| 1705 |
|
|
if (ipprintfs)
|
| 1706 |
|
|
printf("redirect (%d) to %lx\n", code, (u_long)dest);
|
| 1707 |
|
|
#endif
|
| 1708 |
|
|
}
|
| 1709 |
|
|
}
|
| 1710 |
|
|
|
| 1711 |
|
|
error = ip_output(m, (struct mbuf *)0, &ipforward_rt,
|
| 1712 |
|
|
IP_FORWARDING, 0);
|
| 1713 |
|
|
if (error)
|
| 1714 |
|
|
ipstat.ips_cantforward++;
|
| 1715 |
|
|
else {
|
| 1716 |
|
|
ipstat.ips_forward++;
|
| 1717 |
|
|
if (type)
|
| 1718 |
|
|
ipstat.ips_redirectsent++;
|
| 1719 |
|
|
else {
|
| 1720 |
|
|
if (mcopy) {
|
| 1721 |
|
|
ipflow_create(&ipforward_rt, mcopy);
|
| 1722 |
|
|
m_freem(mcopy);
|
| 1723 |
|
|
}
|
| 1724 |
|
|
return;
|
| 1725 |
|
|
}
|
| 1726 |
|
|
}
|
| 1727 |
|
|
if (mcopy == NULL)
|
| 1728 |
|
|
return;
|
| 1729 |
|
|
destifp = NULL;
|
| 1730 |
|
|
|
| 1731 |
|
|
switch (error) {
|
| 1732 |
|
|
|
| 1733 |
|
|
case 0: /* forwarded, but need redirect */
|
| 1734 |
|
|
/* type, code set above */
|
| 1735 |
|
|
break;
|
| 1736 |
|
|
|
| 1737 |
|
|
case ENETUNREACH: /* shouldn't happen, checked above */
|
| 1738 |
|
|
case EHOSTUNREACH:
|
| 1739 |
|
|
case ENETDOWN:
|
| 1740 |
|
|
case EHOSTDOWN:
|
| 1741 |
|
|
default:
|
| 1742 |
|
|
type = ICMP_UNREACH;
|
| 1743 |
|
|
code = ICMP_UNREACH_HOST;
|
| 1744 |
|
|
break;
|
| 1745 |
|
|
|
| 1746 |
|
|
case EMSGSIZE:
|
| 1747 |
|
|
type = ICMP_UNREACH;
|
| 1748 |
|
|
code = ICMP_UNREACH_NEEDFRAG;
|
| 1749 |
|
|
#ifndef IPSEC
|
| 1750 |
|
|
if (ipforward_rt.ro_rt)
|
| 1751 |
|
|
destifp = ipforward_rt.ro_rt->rt_ifp;
|
| 1752 |
|
|
#else
|
| 1753 |
|
|
/*
|
| 1754 |
|
|
* If the packet is routed over IPsec tunnel, tell the
|
| 1755 |
|
|
* originator the tunnel MTU.
|
| 1756 |
|
|
* tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
|
| 1757 |
|
|
* XXX quickhack!!!
|
| 1758 |
|
|
*/
|
| 1759 |
|
|
if (ipforward_rt.ro_rt) {
|
| 1760 |
|
|
struct secpolicy *sp = NULL;
|
| 1761 |
|
|
int ipsecerror;
|
| 1762 |
|
|
int ipsechdr;
|
| 1763 |
|
|
struct route *ro;
|
| 1764 |
|
|
|
| 1765 |
|
|
sp = ipsec4_getpolicybyaddr(mcopy,
|
| 1766 |
|
|
IPSEC_DIR_OUTBOUND,
|
| 1767 |
|
|
IP_FORWARDING,
|
| 1768 |
|
|
&ipsecerror);
|
| 1769 |
|
|
|
| 1770 |
|
|
if (sp == NULL)
|
| 1771 |
|
|
destifp = ipforward_rt.ro_rt->rt_ifp;
|
| 1772 |
|
|
else {
|
| 1773 |
|
|
/* count IPsec header size */
|
| 1774 |
|
|
ipsechdr = ipsec4_hdrsiz(mcopy,
|
| 1775 |
|
|
IPSEC_DIR_OUTBOUND,
|
| 1776 |
|
|
NULL);
|
| 1777 |
|
|
|
| 1778 |
|
|
/*
|
| 1779 |
|
|
* find the correct route for outer IPv4
|
| 1780 |
|
|
* header, compute tunnel MTU.
|
| 1781 |
|
|
*
|
| 1782 |
|
|
* XXX BUG ALERT
|
| 1783 |
|
|
* The "dummyifp" code relies upon the fact
|
| 1784 |
|
|
* that icmp_error() touches only ifp->if_mtu.
|
| 1785 |
|
|
*/
|
| 1786 |
|
|
/*XXX*/
|
| 1787 |
|
|
destifp = NULL;
|
| 1788 |
|
|
if (sp->req != NULL
|
| 1789 |
|
|
&& sp->req->sav != NULL
|
| 1790 |
|
|
&& sp->req->sav->sah != NULL) {
|
| 1791 |
|
|
ro = &sp->req->sav->sah->sa_route;
|
| 1792 |
|
|
if (ro->ro_rt && ro->ro_rt->rt_ifp) {
|
| 1793 |
|
|
dummyifp.if_mtu =
|
| 1794 |
|
|
ro->ro_rt->rt_ifp->if_mtu;
|
| 1795 |
|
|
dummyifp.if_mtu -= ipsechdr;
|
| 1796 |
|
|
destifp = &dummyifp;
|
| 1797 |
|
|
}
|
| 1798 |
|
|
}
|
| 1799 |
|
|
|
| 1800 |
|
|
key_freesp(sp);
|
| 1801 |
|
|
}
|
| 1802 |
|
|
}
|
| 1803 |
|
|
#endif /*IPSEC*/
|
| 1804 |
|
|
ipstat.ips_cantfrag++;
|
| 1805 |
|
|
break;
|
| 1806 |
|
|
|
| 1807 |
|
|
case ENOBUFS:
|
| 1808 |
|
|
#ifdef ALTQ
|
| 1809 |
|
|
/*
|
| 1810 |
|
|
* don't generate ICMP_SOURCEQUENCH
|
| 1811 |
|
|
* (RFC1812 Requirements for IP Version 4 Routers)
|
| 1812 |
|
|
*/
|
| 1813 |
|
|
if (mcopy)
|
| 1814 |
|
|
m_freem(mcopy);
|
| 1815 |
|
|
return;
|
| 1816 |
|
|
#else
|
| 1817 |
|
|
type = ICMP_SOURCEQUENCH;
|
| 1818 |
|
|
code = 0;
|
| 1819 |
|
|
break;
|
| 1820 |
|
|
#endif
|
| 1821 |
|
|
|
| 1822 |
|
|
case EACCES: /* ipfw denied packet */
|
| 1823 |
|
|
m_freem(mcopy);
|
| 1824 |
|
|
return;
|
| 1825 |
|
|
}
|
| 1826 |
|
|
icmp_error(mcopy, type, code, dest, destifp);
|
| 1827 |
|
|
}
|
| 1828 |
|
|
|
| 1829 |
|
|
void
|
| 1830 |
|
|
ip_savecontrol(inp, mp, ip, m)
|
| 1831 |
|
|
register struct inpcb *inp;
|
| 1832 |
|
|
register struct mbuf **mp;
|
| 1833 |
|
|
register struct ip *ip;
|
| 1834 |
|
|
register struct mbuf *m;
|
| 1835 |
|
|
{
|
| 1836 |
|
|
if (inp->inp_socket->so_options & SO_TIMESTAMP) {
|
| 1837 |
|
|
struct timeval tv;
|
| 1838 |
|
|
|
| 1839 |
|
|
microtime(&tv);
|
| 1840 |
|
|
*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
|
| 1841 |
|
|
SCM_TIMESTAMP, SOL_SOCKET);
|
| 1842 |
|
|
if (*mp)
|
| 1843 |
|
|
mp = &(*mp)->m_next;
|
| 1844 |
|
|
}
|
| 1845 |
|
|
if (inp->inp_flags & INP_RECVDSTADDR) {
|
| 1846 |
|
|
*mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
|
| 1847 |
|
|
sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
|
| 1848 |
|
|
if (*mp)
|
| 1849 |
|
|
mp = &(*mp)->m_next;
|
| 1850 |
|
|
}
|
| 1851 |
|
|
#ifdef notyet
|
| 1852 |
|
|
/* XXX
|
| 1853 |
|
|
* Moving these out of udp_input() made them even more broken
|
| 1854 |
|
|
* than they already were.
|
| 1855 |
|
|
*/
|
| 1856 |
|
|
/* options were tossed already */
|
| 1857 |
|
|
if (inp->inp_flags & INP_RECVOPTS) {
|
| 1858 |
|
|
*mp = sbcreatecontrol((caddr_t) opts_deleted_above,
|
| 1859 |
|
|
sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
|
| 1860 |
|
|
if (*mp)
|
| 1861 |
|
|
mp = &(*mp)->m_next;
|
| 1862 |
|
|
}
|
| 1863 |
|
|
/* ip_srcroute doesn't do what we want here, need to fix */
|
| 1864 |
|
|
if (inp->inp_flags & INP_RECVRETOPTS) {
|
| 1865 |
|
|
*mp = sbcreatecontrol((caddr_t) ip_srcroute(),
|
| 1866 |
|
|
sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
|
| 1867 |
|
|
if (*mp)
|
| 1868 |
|
|
mp = &(*mp)->m_next;
|
| 1869 |
|
|
}
|
| 1870 |
|
|
#endif
|
| 1871 |
|
|
if (inp->inp_flags & INP_RECVIF) {
|
| 1872 |
|
|
struct ifnet *ifp;
|
| 1873 |
|
|
struct sdlbuf {
|
| 1874 |
|
|
struct sockaddr_dl sdl;
|
| 1875 |
|
|
u_char pad[32];
|
| 1876 |
|
|
} sdlbuf;
|
| 1877 |
|
|
struct sockaddr_dl *sdp;
|
| 1878 |
|
|
struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
|
| 1879 |
|
|
|
| 1880 |
|
|
if (((ifp = m->m_pkthdr.rcvif))
|
| 1881 |
|
|
&& ( ifp->if_index && (ifp->if_index <= if_index))) {
|
| 1882 |
|
|
sdp = (struct sockaddr_dl *)(ifnet_addrs
|
| 1883 |
|
|
[ifp->if_index - 1]->ifa_addr);
|
| 1884 |
|
|
/*
|
| 1885 |
|
|
* Change our mind and don't try copy.
|
| 1886 |
|
|
*/
|
| 1887 |
|
|
if ((sdp->sdl_family != AF_LINK)
|
| 1888 |
|
|
|| (sdp->sdl_len > sizeof(sdlbuf))) {
|
| 1889 |
|
|
goto makedummy;
|
| 1890 |
|
|
}
|
| 1891 |
|
|
bcopy(sdp, sdl2, sdp->sdl_len);
|
| 1892 |
|
|
} else {
|
| 1893 |
|
|
makedummy:
|
| 1894 |
|
|
sdl2->sdl_len
|
| 1895 |
|
|
= offsetof(struct sockaddr_dl, sdl_data[0]);
|
| 1896 |
|
|
sdl2->sdl_family = AF_LINK;
|
| 1897 |
|
|
sdl2->sdl_index = 0;
|
| 1898 |
|
|
sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
|
| 1899 |
|
|
}
|
| 1900 |
|
|
*mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len,
|
| 1901 |
|
|
IP_RECVIF, IPPROTO_IP);
|
| 1902 |
|
|
if (*mp)
|
| 1903 |
|
|
mp = &(*mp)->m_next;
|
| 1904 |
|
|
}
|
| 1905 |
|
|
}
|
| 1906 |
|
|
|
| 1907 |
|
|
int
|
| 1908 |
|
|
ip_rsvp_init(struct socket *so)
|
| 1909 |
|
|
{
|
| 1910 |
|
|
if (so->so_type != SOCK_RAW ||
|
| 1911 |
|
|
so->so_proto->pr_protocol != IPPROTO_RSVP)
|
| 1912 |
|
|
return EOPNOTSUPP;
|
| 1913 |
|
|
|
| 1914 |
|
|
if (ip_rsvpd != NULL)
|
| 1915 |
|
|
return EADDRINUSE;
|
| 1916 |
|
|
|
| 1917 |
|
|
ip_rsvpd = so;
|
| 1918 |
|
|
/*
|
| 1919 |
|
|
* This may seem silly, but we need to be sure we don't over-increment
|
| 1920 |
|
|
* the RSVP counter, in case something slips up.
|
| 1921 |
|
|
*/
|
| 1922 |
|
|
if (!ip_rsvp_on) {
|
| 1923 |
|
|
ip_rsvp_on = 1;
|
| 1924 |
|
|
rsvp_on++;
|
| 1925 |
|
|
}
|
| 1926 |
|
|
|
| 1927 |
|
|
return 0;
|
| 1928 |
|
|
}
|
| 1929 |
|
|
|
| 1930 |
|
|
int
|
| 1931 |
|
|
ip_rsvp_done(void)
|
| 1932 |
|
|
{
|
| 1933 |
|
|
ip_rsvpd = NULL;
|
| 1934 |
|
|
/*
|
| 1935 |
|
|
* This may seem silly, but we need to be sure we don't over-decrement
|
| 1936 |
|
|
* the RSVP counter, in case something slips up.
|
| 1937 |
|
|
*/
|
| 1938 |
|
|
if (ip_rsvp_on) {
|
| 1939 |
|
|
ip_rsvp_on = 0;
|
| 1940 |
|
|
rsvp_on--;
|
| 1941 |
|
|
}
|
| 1942 |
|
|
return 0;
|
| 1943 |
|
|
}
|