Staticize save_rte()
[dragonfly.git] / sys / netinet / ip_input.c
CommitLineData
984263bc 1/*
66d6c637
JH
2 * Copyright (c) 2003, 2004 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 2003, 2004 The DragonFly Project. All rights reserved.
f23061d4 4 *
66d6c637
JH
5 * This code is derived from software contributed to The DragonFly Project
6 * by Jeffrey M. Hsu.
f23061d4 7 *
66d6c637
JH
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of The DragonFly Project nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific, prior written permission.
f23061d4 19 *
66d6c637
JH
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
66d6c637 34/*
984263bc
MD
35 * Copyright (c) 1982, 1986, 1988, 1993
36 * The Regents of the University of California. All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
67 * $FreeBSD: src/sys/netinet/ip_input.c,v 1.130.2.52 2003/03/07 07:01:28 silby Exp $
102cfd56 68 * $DragonFly: src/sys/netinet/ip_input.c,v 1.91 2008/08/23 04:50:29 sephe Exp $
984263bc
MD
69 */
70
71#define _IP_VHL
72
73#include "opt_bootp.h"
74#include "opt_ipfw.h"
75#include "opt_ipdn.h"
76#include "opt_ipdivert.h"
77#include "opt_ipfilter.h"
78#include "opt_ipstealth.h"
79#include "opt_ipsec.h"
984263bc
MD
80
81#include <sys/param.h>
82#include <sys/systm.h>
83#include <sys/mbuf.h>
84#include <sys/malloc.h>
2b89bacf 85#include <sys/mpipe.h>
984263bc
MD
86#include <sys/domain.h>
87#include <sys/protosw.h>
88#include <sys/socket.h>
89#include <sys/time.h>
fc9c7b4f
HP
90#include <sys/globaldata.h>
91#include <sys/thread.h>
984263bc
MD
92#include <sys/kernel.h>
93#include <sys/syslog.h>
94#include <sys/sysctl.h>
3f9db7f8 95#include <sys/in_cksum.h>
984263bc 96
a00138cb
JS
97#include <machine/stdarg.h>
98
984263bc
MD
99#include <net/if.h>
100#include <net/if_types.h>
101#include <net/if_var.h>
102#include <net/if_dl.h>
e7e55f42 103#include <net/pfil.h>
984263bc
MD
104#include <net/route.h>
105#include <net/netisr.h>
984263bc
MD
106
107#include <netinet/in.h>
108#include <netinet/in_systm.h>
109#include <netinet/in_var.h>
110#include <netinet/ip.h>
111#include <netinet/in_pcb.h>
112#include <netinet/ip_var.h>
113#include <netinet/ip_icmp.h>
984263bc 114
4599cf19
MD
115#include <sys/thread2.h>
116#include <sys/msgport2.h>
117#include <net/netmsg2.h>
984263bc
MD
118
119#include <sys/socketvar.h>
120
1f2de5d4
MD
121#include <net/ipfw/ip_fw.h>
122#include <net/dummynet/ip_dummynet.h>
984263bc
MD
123
124#ifdef IPSEC
125#include <netinet6/ipsec.h>
d2438d69 126#include <netproto/key/key.h>
984263bc
MD
127#endif
128
129#ifdef FAST_IPSEC
bf844ffa
JH
130#include <netproto/ipsec/ipsec.h>
131#include <netproto/ipsec/key.h>
984263bc
MD
132#endif
133
134int rsvp_on = 0;
135static int ip_rsvp_on;
136struct socket *ip_rsvpd;
137
dffa46cd 138int ipforwarding = 0;
984263bc
MD
139SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW,
140 &ipforwarding, 0, "Enable IP forwarding between interfaces");
141
dffa46cd 142static int ipsendredirects = 1; /* XXX */
984263bc
MD
143SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW,
144 &ipsendredirects, 0, "Enable sending IP redirects");
145
dffa46cd 146int ip_defttl = IPDEFTTL;
984263bc
MD
147SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW,
148 &ip_defttl, 0, "Maximum TTL on IP packets");
149
dffa46cd 150static int ip_dosourceroute = 0;
984263bc
MD
151SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW,
152 &ip_dosourceroute, 0, "Enable forwarding source routed IP packets");
153
dffa46cd
JH
154static int ip_acceptsourceroute = 0;
155SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute,
156 CTLFLAG_RW, &ip_acceptsourceroute, 0,
984263bc
MD
157 "Enable accepting source routed IP packets");
158
dffa46cd 159static int ip_keepfaith = 0;
984263bc 160SYSCTL_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW,
dffa46cd 161 &ip_keepfaith, 0,
0ca0cd25 162 "Enable packet capture for FAITH IPv4->IPv6 translator daemon");
984263bc 163
dffa46cd
JH
164static int nipq = 0; /* total # of reass queues */
165static int maxnipq;
984263bc 166SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragpackets, CTLFLAG_RW,
dffa46cd
JH
167 &maxnipq, 0,
168 "Maximum number of IPv4 fragment reassembly queue entries");
984263bc 169
dffa46cd 170static int maxfragsperpacket;
984263bc 171SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragsperpacket, CTLFLAG_RW,
dffa46cd
JH
172 &maxfragsperpacket, 0,
173 "Maximum number of IPv4 fragments allowed per packet");
984263bc 174
dffa46cd 175static int ip_sendsourcequench = 0;
984263bc 176SYSCTL_INT(_net_inet_ip, OID_AUTO, sendsourcequench, CTLFLAG_RW,
dffa46cd
JH
177 &ip_sendsourcequench, 0,
178 "Enable the transmission of source quench packets");
984263bc 179
b6afe32a 180int ip_do_randomid = 1;
6277137d
MD
181SYSCTL_INT(_net_inet_ip, OID_AUTO, random_id, CTLFLAG_RW,
182 &ip_do_randomid, 0,
183 "Assign random ip_id values");
984263bc
MD
184/*
185 * XXX - Setting ip_checkinterface mostly implements the receive side of
186 * the Strong ES model described in RFC 1122, but since the routing table
187 * and transmit implementation do not implement the Strong ES model,
188 * setting this to 1 results in an odd hybrid.
189 *
190 * XXX - ip_checkinterface currently must be disabled if you use ipnat
191 * to translate the destination address to another local interface.
192 *
193 * XXX - ip_checkinterface must be disabled if you add IP aliases
194 * to the loopback interface instead of the interface where the
195 * packets for those addresses are received.
196 */
dffa46cd 197static int ip_checkinterface = 0;
984263bc
MD
198SYSCTL_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW,
199 &ip_checkinterface, 0, "Verify packet arrives on correct interface");
200
201#ifdef DIAGNOSTIC
dffa46cd 202static int ipprintfs = 0;
984263bc
MD
203#endif
204
984263bc 205extern struct domain inetdomain;
4468b0b4 206extern struct protosw inetsw[];
984263bc 207u_char ip_protox[IPPROTO_MAX];
1b562c24 208struct in_ifaddrhead in_ifaddrheads[MAXCPU]; /* first inet address */
f8983475
SZ
209struct in_ifaddrhashhead *in_ifaddrhashtbls[MAXCPU];
210 /* inet addr hash table */
984263bc 211u_long in_ifaddrhmask; /* mask for hash table */
dffa46cd 212
ae1d3076 213struct ip_stats ipstats_percpu[MAXCPU];
fc9c7b4f
HP
214#ifdef SMP
215static int
216sysctl_ipstats(SYSCTL_HANDLER_ARGS)
217{
218 int cpu, error = 0;
219
220 for (cpu = 0; cpu < ncpus; ++cpu) {
ae1d3076 221 if ((error = SYSCTL_OUT(req, &ipstats_percpu[cpu],
fc9c7b4f
HP
222 sizeof(struct ip_stats))))
223 break;
ae1d3076 224 if ((error = SYSCTL_IN(req, &ipstats_percpu[cpu],
fc9c7b4f
HP
225 sizeof(struct ip_stats))))
226 break;
227 }
228
229 return (error);
230}
231SYSCTL_PROC(_net_inet_ip, IPCTL_STATS, stats, (CTLTYPE_OPAQUE | CTLFLAG_RW),
232 0, 0, sysctl_ipstats, "S,ip_stats", "IP statistics");
233#else
984263bc 234SYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RW,
fc9c7b4f
HP
235 &ipstat, ip_stats, "IP statistics");
236#endif
984263bc
MD
237
238/* Packet reassembly stuff */
dffa46cd
JH
239#define IPREASS_NHASH_LOG2 6
240#define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2)
241#define IPREASS_HMASK (IPREASS_NHASH - 1)
242#define IPREASS_HASH(x,y) \
243 (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
984263bc
MD
244
245static struct ipq ipq[IPREASS_NHASH];
984263bc
MD
246
247#ifdef IPCTL_DEFMTU
248SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
249 &ip_mtu, 0, "Default MTU");
250#endif
251
252#ifdef IPSTEALTH
dffa46cd
JH
253static int ipstealth = 0;
254SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW, &ipstealth, 0, "");
255#else
256static const int ipstealth = 0;
984263bc
MD
257#endif
258
259
260/* Firewall hooks */
261ip_fw_chk_t *ip_fw_chk_ptr;
84a3e25a 262ip_fw_dn_io_t *ip_fw_dn_io_ptr;
2930a997 263int ip_fw_loaded;
dffa46cd 264int fw_enable = 1;
984263bc
MD
265int fw_one_pass = 1;
266
e7e55f42 267struct pfil_head inet_pfil_hook;
984263bc
MD
268
269/*
270 * XXX this is ugly -- the following two global variables are
271 * used to store packet state while it travels through the stack.
272 * Note that the code even makes assumptions on the size and
273 * alignment of fields inside struct ip_srcrt so e.g. adding some
274 * fields will break the code. This needs to be fixed.
275 *
276 * We need to save the IP options in case a protocol wants to respond
277 * to an incoming packet over the same route if the packet got here
278 * using IP source routing. This allows connection establishment and
279 * maintenance when the remote end is on a network that is not known
280 * to us.
281 */
dffa46cd
JH
282static int ip_nhops = 0;
283
984263bc
MD
284static struct ip_srcrt {
285 struct in_addr dst; /* final destination */
286 char nop; /* one NOP to align */
287 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */
288 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
289} ip_srcrt;
290
2b89bacf
MD
291static MALLOC_DEFINE(M_IPQ, "ipq", "IP Fragment Management");
292static struct malloc_pipe ipq_mpipe;
293
bb3b17df
SZ
294static void save_rte(u_char *, struct in_addr);
295static int ip_dooptions(struct mbuf *m, int, struct sockaddr_in *);
296static void ip_freef(struct ipq *);
297static void ip_input_handler(struct netmsg *);
9691feb6 298static struct mbuf *ip_reass(struct mbuf *, u_int32_t *);
984263bc
MD
299
300/*
301 * IP initialization: fill in IP protocol switch table.
302 * All protocols not implemented in kernel go to raw IP protocol handler.
303 */
304void
8a3125c6 305ip_init(void)
984263bc 306{
4468b0b4 307 struct protosw *pr;
2256ba69 308 int i;
fc9c7b4f
HP
309#ifdef SMP
310 int cpu;
311#endif
984263bc 312
2b89bacf
MD
313 /*
314 * Make sure we can handle a reasonable number of fragments but
315 * cap it at 4000 (XXX).
316 */
317 mpipe_init(&ipq_mpipe, M_IPQ, sizeof(struct ipq),
318 IFQ_MAXLEN, 4000, 0, NULL);
f8983475 319 for (i = 0; i < ncpus; ++i) {
1b562c24 320 TAILQ_INIT(&in_ifaddrheads[i]);
f8983475
SZ
321 in_ifaddrhashtbls[i] =
322 hashinit(INADDR_NHASH, M_IFADDR, &in_ifaddrhmask);
323 }
4468b0b4 324 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
dffa46cd 325 if (pr == NULL)
984263bc
MD
326 panic("ip_init");
327 for (i = 0; i < IPPROTO_MAX; i++)
328 ip_protox[i] = pr - inetsw;
4468b0b4
JS
329 for (pr = inetdomain.dom_protosw;
330 pr < inetdomain.dom_protoswNPROTOSW; pr++)
984263bc
MD
331 if (pr->pr_domain->dom_family == PF_INET &&
332 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
333 ip_protox[pr->pr_protocol] = pr - inetsw;
334
e7e55f42
JR
335 inet_pfil_hook.ph_type = PFIL_TYPE_AF;
336 inet_pfil_hook.ph_af = AF_INET;
5e3f3b7a 337 if ((i = pfil_head_register(&inet_pfil_hook)) != 0) {
a6ec04bc 338 kprintf("%s: WARNING: unable to register pfil hook, "
e7e55f42 339 "error %d\n", __func__, i);
5e3f3b7a 340 }
e7e55f42 341
984263bc
MD
342 for (i = 0; i < IPREASS_NHASH; i++)
343 ipq[i].next = ipq[i].prev = &ipq[i];
344
345 maxnipq = nmbclusters / 32;
346 maxfragsperpacket = 16;
347
984263bc 348 ip_id = time_second & 0xffff;
984263bc 349
fc9c7b4f 350 /*
ae1d3076 351 * Initialize IP statistics counters for each CPU.
fc9c7b4f 352 *
fc9c7b4f
HP
353 */
354#ifdef SMP
355 for (cpu = 0; cpu < ncpus; ++cpu) {
ae1d3076 356 bzero(&ipstats_percpu[cpu], sizeof(struct ip_stats));
fc9c7b4f
HP
357 }
358#else
359 bzero(&ipstat, sizeof(struct ip_stats));
360#endif
361
934c6849 362 netisr_register(NETISR_IP, ip_mport_in, ip_input_handler);
984263bc
MD
363}
364
365/*
366 * XXX watch out this one. It is perhaps used as a cache for
367 * the most recently used route ? it is cleared in in_addroute()
368 * when a new route is successfully created.
369 */
ecdefdda 370struct route ipforward_rt[MAXCPU];
984263bc 371
0c7ac0cd
JH
372/* Do transport protocol processing. */
373static void
5de23090 374transport_processing_oncpu(struct mbuf *m, int hlen, struct ip *ip)
0c7ac0cd
JH
375{
376 /*
377 * Switch out to protocol's input routine.
378 */
5de23090 379 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen, ip->ip_p);
0c7ac0cd
JH
380}
381
4599cf19
MD
382static void
383transport_processing_handler(netmsg_t netmsg)
0c7ac0cd 384{
3d6057fe 385 struct netmsg_packet *pmsg = (struct netmsg_packet *)netmsg;
0c7ac0cd 386 struct ip *ip;
3d6057fe
SZ
387 int hlen;
388
389 ip = mtod(pmsg->nm_packet, struct ip *);
390 hlen = pmsg->nm_netmsg.nm_lmsg.u.ms_result;
0c7ac0cd 391
3d6057fe
SZ
392 transport_processing_oncpu(pmsg->nm_packet, hlen, ip);
393 /* netmsg was embedded in the mbuf, do not reply! */
0c7ac0cd
JH
394}
395
4599cf19 396static void
4f277347
JH
397ip_input_handler(struct netmsg *msg0)
398{
399 struct mbuf *m = ((struct netmsg_packet *)msg0)->nm_packet;
400
401 ip_input(m);
6aad077d 402 /* msg0 was embedded in the mbuf, do not reply! */
4f277347
JH
403}
404
984263bc 405/*
590b8cd4 406 * IP input routine. Checksum and byte swap header. If fragmented
984263bc
MD
407 * try to reassemble. Process options. Pass to next level.
408 */
409void
4f277347 410ip_input(struct mbuf *m)
984263bc
MD
411{
412 struct ip *ip;
984263bc 413 struct in_ifaddr *ia = NULL;
f8983475 414 struct in_ifaddr_container *iac;
dffa46cd 415 int i, hlen, checkif;
984263bc
MD
416 u_short sum;
417 struct in_addr pkt_dst;
418 u_int32_t divert_info = 0; /* packet divert/tee info */
419 struct ip_fw_args args;
dffa46cd 420 boolean_t using_srcrt = FALSE; /* forward (by PFIL_HOOKS) */
0c7ac0cd 421 boolean_t needredispatch = FALSE;
e7e55f42 422 struct in_addr odst; /* original dst address(NAT) */
984263bc 423 struct m_tag *mtag;
5de23090 424 struct sockaddr_in *next_hop = NULL;
e5ecc832 425#ifdef FAST_IPSEC
984263bc
MD
426 struct tdb_ident *tdbi;
427 struct secpolicy *sp;
1cae611f 428 int error;
dffa46cd 429#endif
984263bc
MD
430
431 args.eh = NULL;
432 args.oif = NULL;
433 args.rule = NULL;
984263bc 434
814907cb 435 M_ASSERTPKTHDR(m);
984263bc 436
5de23090
SZ
437 if (m->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
438 /* Next hop */
439 mtag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
440 KKASSERT(mtag != NULL);
441 next_hop = m_tag_data(mtag);
442 }
443
eb241549
SZ
444 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
445 /* Extract info from dummynet tag */
446 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
447 KKASSERT(mtag != NULL);
84a3e25a 448 args.rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
eb241549
SZ
449 KKASSERT(args.rule != NULL);
450
4c7020ad 451 m_tag_delete(m, mtag);
eb241549 452 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
4c7020ad 453
eb241549 454 /* dummynet already filtered us */
984263bc
MD
455 ip = mtod(m, struct ip *);
456 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
dffa46cd 457 goto iphack;
984263bc
MD
458 }
459
460 ipstat.ips_total++;
461
55d829f8
JH
462 /* length checks already done in ip_demux() */
463 KASSERT(m->m_len >= sizeof(ip), ("IP header not in one mbuf"));
984263bc 464
984263bc
MD
465 ip = mtod(m, struct ip *);
466
467 if (IP_VHL_V(ip->ip_vhl) != IPVERSION) {
468 ipstat.ips_badvers++;
469 goto bad;
470 }
471
472 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
9babcab8
JH
473 /* length checks already done in ip_demux() */
474 KASSERT(hlen >= sizeof(struct ip), ("IP header len too small"));
475 KASSERT(m->m_len >= hlen, ("packet shorter than IP header length"));
984263bc
MD
476
477 /* 127/8 must not appear on wire - RFC1122 */
478 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
479 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
dffa46cd 480 if (!(m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK)) {
984263bc
MD
481 ipstat.ips_badaddr++;
482 goto bad;
483 }
484 }
485
486 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
487 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
488 } else {
489 if (hlen == sizeof(struct ip)) {
490 sum = in_cksum_hdr(ip);
491 } else {
492 sum = in_cksum(m, hlen);
493 }
494 }
5fe66e68 495 if (sum != 0) {
984263bc
MD
496 ipstat.ips_badsum++;
497 goto bad;
498 }
499
4d723e5a
JS
500#ifdef ALTQ
501 if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) {
502 /* packet is dropped by traffic conditioner */
503 return;
504 }
505#endif
984263bc
MD
506 /*
507 * Convert fields to host representation.
508 */
509 ip->ip_len = ntohs(ip->ip_len);
510 if (ip->ip_len < hlen) {
511 ipstat.ips_badlen++;
512 goto bad;
513 }
514 ip->ip_off = ntohs(ip->ip_off);
515
516 /*
517 * Check that the amount of data in the buffers
518 * is as at least much as the IP header would have us expect.
519 * Trim mbufs if longer than we expect.
520 * Drop packet if shorter than we expect.
521 */
522 if (m->m_pkthdr.len < ip->ip_len) {
984263bc
MD
523 ipstat.ips_tooshort++;
524 goto bad;
525 }
526 if (m->m_pkthdr.len > ip->ip_len) {
527 if (m->m_len == m->m_pkthdr.len) {
528 m->m_len = ip->ip_len;
529 m->m_pkthdr.len = ip->ip_len;
530 } else
531 m_adj(m, ip->ip_len - m->m_pkthdr.len);
532 }
533#if defined(IPSEC) && !defined(IPSEC_FILTERGIF)
534 /*
535 * Bypass packet filtering for packets from a tunnel (gif).
536 */
537 if (ipsec_gethist(m, NULL))
538 goto pass;
539#endif
540
541 /*
542 * IpHack's section.
543 * Right now when no processing on packet has done
544 * and it is still fresh out of network we do our black
545 * deals with it.
546 * - Firewall: deny/allow/divert
547 * - Xlate: translate packet's addr/port (NAT).
548 * - Pipe: pass pkt through dummynet.
549 * - Wrap: fake packet's addr/port <unimpl.>
550 * - Encapsulate: put it in another IP and send out. <unimp.>
dffa46cd 551 */
984263bc
MD
552
553iphack:
dffa46cd 554
984263bc 555 /*
e7e55f42
JR
556 * Run through list of hooks for input packets.
557 *
558 * NB: Beware of the destination address changing (e.g.
559 * by NAT rewriting). When this happens, tell
560 * ip_forward to do the right thing.
984263bc 561 */
afabe90c
MD
562 if (pfil_has_hooks(&inet_pfil_hook)) {
563 odst = ip->ip_dst;
f23061d4 564 if (pfil_run_hooks(&inet_pfil_hook, &m,
afabe90c
MD
565 m->m_pkthdr.rcvif, PFIL_IN)) {
566 return;
567 }
568 if (m == NULL) /* consumed by filter */
569 return;
570 ip = mtod(m, struct ip *);
571 using_srcrt = (odst.s_addr != ip->ip_dst.s_addr);
572 }
dffa46cd 573
984263bc
MD
574 if (fw_enable && IPFW_LOADED) {
575 /*
576 * If we've been forwarded from the output side, then
577 * skip the firewall a second time
578 */
5de23090 579 if (next_hop != NULL)
984263bc
MD
580 goto ours;
581
582 args.m = m;
583 i = ip_fw_chk_ptr(&args);
584 m = args.m;
585
5fe66e68
JH
586 if ((i & IP_FW_PORT_DENY_FLAG) || m == NULL) { /* drop */
587 if (m != NULL)
984263bc
MD
588 m_freem(m);
589 return;
590 }
5fe66e68 591 ip = mtod(m, struct ip *); /* just in case m changed */
5de23090
SZ
592
593 if (m->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
594 mtag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
595 KKASSERT(mtag != NULL);
596 next_hop = m_tag_data(mtag);
597 }
598
599 if (i == 0 && next_hop == NULL) /* common case */
984263bc 600 goto pass;
84a3e25a 601 if (i & IP_FW_PORT_DYNT_FLAG) {
984263bc 602 /* Send packet to the appropriate pipe */
84a3e25a 603 ip_fw_dn_io_ptr(m, i&0xffff, DN_TO_IP_IN, &args);
984263bc
MD
604 return;
605 }
606#ifdef IPDIVERT
dffa46cd 607 if (i != 0 && !(i & IP_FW_PORT_DYNT_FLAG)) {
984263bc
MD
608 /* Divert or tee packet */
609 divert_info = i;
610 goto ours;
611 }
612#endif
5de23090 613 if (i == 0 && next_hop != NULL)
984263bc
MD
614 goto pass;
615 /*
616 * if we get here, the packet must be dropped
617 */
618 m_freem(m);
619 return;
620 }
621pass:
622
623 /*
624 * Process options and, if not destined for us,
625 * ship it on. ip_dooptions returns 1 when an
626 * error was detected (causing an icmp message
627 * to be sent and the original packet to be freed).
628 */
629 ip_nhops = 0; /* for source routed packets */
5de23090 630 if (hlen > sizeof(struct ip) && ip_dooptions(m, 0, next_hop))
984263bc
MD
631 return;
632
dffa46cd
JH
633 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
634 * matter if it is destined to another node, or whether it is
635 * a multicast one, RSVP wants it! and prevents it from being forwarded
636 * anywhere else. Also checks if the rsvp daemon is running before
984263bc 637 * grabbing the packet.
dffa46cd
JH
638 */
639 if (rsvp_on && ip->ip_p == IPPROTO_RSVP)
984263bc
MD
640 goto ours;
641
642 /*
643 * Check our list of addresses, to see if the packet is for us.
644 * If we don't have any addresses, assume any unicast packet
645 * we receive might be for us (and let the upper layers deal
646 * with it).
647 */
1b562c24
SZ
648 if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid]) &&
649 !(m->m_flags & (M_MCAST | M_BCAST)))
984263bc
MD
650 goto ours;
651
652 /*
653 * Cache the destination address of the packet; this may be
654 * changed by use of 'ipfw fwd'.
655 */
5de23090 656 pkt_dst = next_hop ? next_hop->sin_addr : ip->ip_dst;
984263bc
MD
657
658 /*
659 * Enable a consistency check between the destination address
660 * and the arrival interface for a unicast packet (the RFC 1122
661 * strong ES model) if IP forwarding is disabled and the packet
662 * is not locally generated and the packet is not subject to
663 * 'ipfw fwd'.
664 *
665 * XXX - Checking also should be disabled if the destination
666 * address is ipnat'ed to a different interface.
667 *
668 * XXX - Checking is incompatible with IP aliases added
669 * to the loopback interface instead of the interface where
670 * the packets are received.
671 */
dffa46cd
JH
672 checkif = ip_checkinterface &&
673 !ipforwarding &&
674 m->m_pkthdr.rcvif != NULL &&
675 !(m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) &&
5de23090 676 next_hop == NULL;
984263bc
MD
677
678 /*
679 * Check for exact addresses in the hash bucket.
680 */
f8983475
SZ
681 LIST_FOREACH(iac, INADDR_HASH(pkt_dst.s_addr), ia_hash) {
682 ia = iac->ia;
683
984263bc
MD
684 /*
685 * If the address matches, verify that the packet
686 * arrived via the correct interface if checking is
687 * enabled.
688 */
dffa46cd 689 if (IA_SIN(ia)->sin_addr.s_addr == pkt_dst.s_addr &&
984263bc
MD
690 (!checkif || ia->ia_ifp == m->m_pkthdr.rcvif))
691 goto ours;
692 }
f8983475
SZ
693 ia = NULL;
694
984263bc
MD
695 /*
696 * Check for broadcast addresses.
697 *
698 * Only accept broadcast packets that arrive via the matching
699 * interface. Reception of forwarded directed broadcasts would
700 * be handled via ip_forward() and ether_output() with the loopback
701 * into the stack for SIMPLEX interfaces handled by ether_output().
702 */
703 if (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
b2632176
SZ
704 struct ifaddr_container *ifac;
705
706 TAILQ_FOREACH(ifac, &m->m_pkthdr.rcvif->if_addrheads[mycpuid],
707 ifa_link) {
708 struct ifaddr *ifa = ifac->ifa;
709
83a415f4
MD
710 if (ifa->ifa_addr == NULL) /* shutdown/startup race */
711 continue;
984263bc
MD
712 if (ifa->ifa_addr->sa_family != AF_INET)
713 continue;
714 ia = ifatoia(ifa);
715 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
dffa46cd 716 pkt_dst.s_addr)
984263bc
MD
717 goto ours;
718 if (ia->ia_netbroadcast.s_addr == pkt_dst.s_addr)
719 goto ours;
720#ifdef BOOTP_COMPAT
721 if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY)
722 goto ours;
723#endif
724 }
725 }
726 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
727 struct in_multi *inm;
5fe66e68
JH
728
729 if (ip_mrouter != NULL) {
984263bc
MD
730 /*
731 * If we are acting as a multicast router, all
732 * incoming multicast packets are passed to the
733 * kernel-level multicast forwarding function.
734 * The packet is returned (relatively) intact; if
735 * ip_mforward() returns a non-zero value, the packet
736 * must be discarded, else it may be accepted below.
737 */
5fe66e68 738 if (ip_mforward != NULL &&
dffa46cd 739 ip_mforward(ip, m->m_pkthdr.rcvif, m, NULL) != 0) {
984263bc
MD
740 ipstat.ips_cantforward++;
741 m_freem(m);
742 return;
743 }
744
745 /*
746 * The process-level routing daemon needs to receive
747 * all multicast IGMP packets, whether or not this
748 * host belongs to their destination groups.
749 */
750 if (ip->ip_p == IPPROTO_IGMP)
751 goto ours;
752 ipstat.ips_forward++;
753 }
754 /*
755 * See if we belong to the destination multicast group on the
756 * arrival interface.
757 */
758 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
759 if (inm == NULL) {
760 ipstat.ips_notmember++;
761 m_freem(m);
762 return;
763 }
764 goto ours;
765 }
dffa46cd 766 if (ip->ip_dst.s_addr == INADDR_BROADCAST)
984263bc
MD
767 goto ours;
768 if (ip->ip_dst.s_addr == INADDR_ANY)
769 goto ours;
770
771 /*
772 * FAITH(Firewall Aided Internet Translator)
773 */
774 if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
775 if (ip_keepfaith) {
dffa46cd 776 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP)
984263bc
MD
777 goto ours;
778 }
779 m_freem(m);
780 return;
781 }
782
783 /*
784 * Not for us; forward if possible and desirable.
785 */
dffa46cd 786 if (!ipforwarding) {
984263bc
MD
787 ipstat.ips_cantforward++;
788 m_freem(m);
789 } else {
790#ifdef IPSEC
791 /*
792 * Enforce inbound IPsec SPD.
793 */
794 if (ipsec4_in_reject(m, NULL)) {
795 ipsecstat.in_polvio++;
796 goto bad;
797 }
dffa46cd 798#endif
984263bc
MD
799#ifdef FAST_IPSEC
800 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
1cae611f 801 crit_enter();
984263bc 802 if (mtag != NULL) {
d031aa80 803 tdbi = (struct tdb_ident *)m_tag_data(mtag);
984263bc
MD
804 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
805 } else {
806 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
dffa46cd 807 IP_FORWARDING, &error);
984263bc
MD
808 }
809 if (sp == NULL) { /* NB: can happen if error */
1cae611f 810 crit_exit();
984263bc
MD
811 /*XXX error stat???*/
812 DPRINTF(("ip_input: no SP for forwarding\n")); /*XXX*/
813 goto bad;
814 }
815
816 /*
817 * Check security policy against packet attributes.
818 */
819 error = ipsec_in_reject(sp, m);
820 KEY_FREESP(&sp);
1cae611f 821 crit_exit();
984263bc
MD
822 if (error) {
823 ipstat.ips_cantforward++;
824 goto bad;
825 }
dffa46cd 826#endif
5de23090 827 ip_forward(m, using_srcrt, next_hop);
984263bc
MD
828 }
829 return;
830
831ours:
dffa46cd 832
984263bc
MD
833 /*
834 * IPSTEALTH: Process non-routing options only
835 * if the packet is destined for us.
836 */
dffa46cd
JH
837 if (ipstealth &&
838 hlen > sizeof(struct ip) &&
5de23090 839 ip_dooptions(m, 1, next_hop))
984263bc 840 return;
984263bc
MD
841
842 /* Count the packet in the ip address stats */
843 if (ia != NULL) {
844 ia->ia_ifa.if_ipackets++;
845 ia->ia_ifa.if_ibytes += m->m_pkthdr.len;
846 }
847
848 /*
849 * If offset or IP_MF are set, must reassemble.
850 * Otherwise, nothing need be done.
851 * (We could look in the reassembly queue to see
852 * if the packet was previously fragmented,
853 * but it's not worth the time; just let them time out.)
854 */
855 if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
984263bc
MD
856 /*
857 * Attempt reassembly; if it succeeds, proceed.
858 * ip_reass() will return a different mbuf, and update
e5ecc832 859 * the divert info in divert_info.
984263bc 860 */
9691feb6 861 m = ip_reass(m, &divert_info);
dffa46cd 862 if (m == NULL)
984263bc 863 return;
d2f6f5b2 864
0c7ac0cd 865 needredispatch = TRUE;
984263bc
MD
866 ip = mtod(m, struct ip *);
867 /* Get the header length of the reassembled packet */
868 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
869#ifdef IPDIVERT
870 /* Restore original checksum before diverting packet */
871 if (divert_info != 0) {
872 ip->ip_len += hlen;
873 ip->ip_len = htons(ip->ip_len);
874 ip->ip_off = htons(ip->ip_off);
875 ip->ip_sum = 0;
876 if (hlen == sizeof(struct ip))
877 ip->ip_sum = in_cksum_hdr(ip);
878 else
879 ip->ip_sum = in_cksum(m, hlen);
880 ip->ip_off = ntohs(ip->ip_off);
881 ip->ip_len = ntohs(ip->ip_len);
882 ip->ip_len -= hlen;
883 }
884#endif
bf82f9b7 885 } else {
984263bc 886 ip->ip_len -= hlen;
bf82f9b7 887 }
984263bc
MD
888
889#ifdef IPDIVERT
890 /*
891 * Divert or tee packet to the divert protocol if required.
892 */
893 if (divert_info != 0) {
894 struct mbuf *clone = NULL;
895
896 /* Clone packet if we're doing a 'tee' */
897 if ((divert_info & IP_FW_PORT_TEE_FLAG) != 0)
74f1caca 898 clone = m_dup(m, MB_DONTWAIT);
984263bc
MD
899
900 /* Restore packet header fields to original values */
901 ip->ip_len += hlen;
902 ip->ip_len = htons(ip->ip_len);
903 ip->ip_off = htons(ip->ip_off);
904
905 /* Deliver packet to divert input routine */
e5ecc832 906 divert_packet(m, 1, divert_info & 0xffff);
984263bc
MD
907 ipstat.ips_delivered++;
908
909 /* If 'tee', continue with original packet */
910 if (clone == NULL)
911 return;
912 m = clone;
913 ip = mtod(m, struct ip *);
914 ip->ip_len += hlen;
915 /*
916 * Jump backwards to complete processing of the
917 * packet. But first clear divert_info to avoid
918 * entering this block again.
5de23090
SZ
919 * We do not need to clear args.divert_rule as
920 * it will not be used.
e5ecc832
JS
921 *
922 * XXX Better safe than sorry, remove the DIVERT tag.
984263bc 923 */
e5ecc832
JS
924 mtag = m_tag_find(m, PACKET_TAG_IPFW_DIVERT, NULL);
925 if (mtag != NULL)
926 m_tag_delete(m, mtag);
927
984263bc
MD
928 divert_info = 0;
929 goto pass;
930 }
931#endif
932
933#ifdef IPSEC
934 /*
935 * enforce IPsec policy checking if we are seeing last header.
936 * note that we do not visit this with protocols with pcb layer
937 * code - like udp/tcp/raw ip.
938 */
dffa46cd 939 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) &&
984263bc
MD
940 ipsec4_in_reject(m, NULL)) {
941 ipsecstat.in_polvio++;
942 goto bad;
943 }
944#endif
945#if FAST_IPSEC
946 /*
947 * enforce IPsec policy checking if we are seeing last header.
948 * note that we do not visit this with protocols with pcb layer
949 * code - like udp/tcp/raw ip.
950 */
dffa46cd 951 if (inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) {
984263bc
MD
952 /*
953 * Check if the packet has already had IPsec processing
954 * done. If so, then just pass it along. This tag gets
955 * set during AH, ESP, etc. input handling, before the
956 * packet is returned to the ip input queue for delivery.
dffa46cd 957 */
984263bc 958 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
1cae611f 959 crit_enter();
984263bc 960 if (mtag != NULL) {
d031aa80 961 tdbi = (struct tdb_ident *)m_tag_data(mtag);
984263bc
MD
962 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
963 } else {
964 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
dffa46cd 965 IP_FORWARDING, &error);
984263bc
MD
966 }
967 if (sp != NULL) {
968 /*
969 * Check security policy against packet attributes.
970 */
971 error = ipsec_in_reject(sp, m);
972 KEY_FREESP(&sp);
973 } else {
974 /* XXX error stat??? */
975 error = EINVAL;
976DPRINTF(("ip_input: no SP, packet discarded\n"));/*XXX*/
977 goto bad;
978 }
1cae611f 979 crit_exit();
984263bc
MD
980 if (error)
981 goto bad;
982 }
983#endif /* FAST_IPSEC */
984
984263bc 985 ipstat.ips_delivered++;
0c7ac0cd 986 if (needredispatch) {
3d6057fe 987 struct netmsg_packet *pmsg;
0c7ac0cd 988 lwkt_port_t port;
984263bc 989
35658e20
JH
990 ip->ip_off = htons(ip->ip_off);
991 ip->ip_len = htons(ip->ip_len);
934c6849 992 port = ip_mport_in(&m);
35658e20
JH
993 if (port == NULL)
994 return;
995
3d6057fe
SZ
996 pmsg = &m->m_hdr.mh_netmsg;
997 netmsg_init(&pmsg->nm_netmsg, &netisr_apanic_rport, 0,
4599cf19 998 transport_processing_handler);
3d6057fe
SZ
999 pmsg->nm_packet = m;
1000 pmsg->nm_netmsg.nm_lmsg.u.ms_result = hlen;
984263bc 1001
3f030272
JH
1002 ip = mtod(m, struct ip *);
1003 ip->ip_len = ntohs(ip->ip_len);
1004 ip->ip_off = ntohs(ip->ip_off);
3d6057fe 1005 lwkt_sendmsg(port, &pmsg->nm_netmsg.nm_lmsg);
0c7ac0cd 1006 } else {
5de23090 1007 transport_processing_oncpu(m, hlen, ip);
bf82f9b7 1008 }
984263bc 1009 return;
a80cf23b 1010
984263bc
MD
1011bad:
1012 m_freem(m);
1013}
1014
984263bc
MD
1015/*
1016 * Take incoming datagram fragment and try to reassemble it into
1017 * whole datagram. If a chain for reassembly of this datagram already
1018 * exists, then it is given as fp; otherwise have to make a chain.
1019 *
1020 * When IPDIVERT enabled, keep additional state with each packet that
1021 * tells us if we need to divert or tee the packet we're building.
e5ecc832 1022 * In particular, *divinfo includes the port and TEE flag.
984263bc
MD
1023 */
1024
1025static struct mbuf *
9691feb6 1026ip_reass(struct mbuf *m, u_int32_t *divinfo)
984263bc
MD
1027{
1028 struct ip *ip = mtod(m, struct ip *);
dffa46cd 1029 struct mbuf *p = NULL, *q, *nq;
2b89bacf 1030 struct mbuf *n;
9691feb6 1031 struct ipq *fp = NULL;
984263bc
MD
1032 int hlen = IP_VHL_HL(ip->ip_vhl) << 2;
1033 int i, next;
9691feb6 1034 u_short sum;
e5ecc832
JS
1035#ifdef IPDIVERT
1036 struct m_tag *mtag;
1037#endif
984263bc 1038
9691feb6
SZ
1039 /* If maxnipq is 0, never accept fragments. */
1040 if (maxnipq == 0) {
1041 ipstat.ips_fragments++;
1042 ipstat.ips_fragdropped++;
1043 m_freem(m);
1044 return NULL;
1045 }
1046
1047 sum = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
1048 /*
1049 * Look for queue of fragments of this datagram.
1050 */
1051 for (fp = ipq[sum].next; fp != &ipq[sum]; fp = fp->next)
1052 if (ip->ip_id == fp->ipq_id &&
1053 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
1054 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
1055 ip->ip_p == fp->ipq_p)
1056 goto found;
1057
1058 fp = NULL;
1059
1060 /*
1061 * Enforce upper bound on number of fragmented packets
1062 * for which we attempt reassembly;
1063 * If maxnipq is -1, accept all fragments without limitation.
1064 */
1065 if (nipq > maxnipq && maxnipq > 0) {
1066 /*
1067 * drop something from the tail of the current queue
1068 * before proceeding further
1069 */
1070 if (ipq[sum].prev == &ipq[sum]) { /* gak */
1071 for (i = 0; i < IPREASS_NHASH; i++) {
1072 if (ipq[i].prev != &ipq[i]) {
1073 ipstat.ips_fragtimeout +=
1074 ipq[i].prev->ipq_nfrags;
1075 ip_freef(ipq[i].prev);
1076 break;
1077 }
1078 }
1079 } else {
1080 ipstat.ips_fragtimeout +=
1081 ipq[sum].prev->ipq_nfrags;
1082 ip_freef(ipq[sum].prev);
1083 }
1084 }
1085found:
1086 /*
1087 * Adjust ip_len to not reflect header,
1088 * convert offset of this to bytes.
1089 */
1090 ip->ip_len -= hlen;
1091 if (ip->ip_off & IP_MF) {
1092 /*
1093 * Make sure that fragments have a data length
1094 * that's a non-zero multiple of 8 bytes.
1095 */
1096 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
1097 ipstat.ips_toosmall++; /* XXX */
1098 m_freem(m);
1099 return NULL;
1100 }
1101 m->m_flags |= M_FRAG;
1102 } else
1103 m->m_flags &= ~M_FRAG;
1104 ip->ip_off <<= 3;
1105
1106 ipstat.ips_fragments++;
1107 m->m_pkthdr.header = ip;
1108
fbb35ef0
SZ
1109 /*
1110 * If the hardware has not done csum over this fragment
1111 * then csum_data is not valid at all.
1112 */
1113 if ((m->m_pkthdr.csum_flags & (CSUM_FRAG_NOT_CHECKED | CSUM_DATA_VALID))
1114 == (CSUM_FRAG_NOT_CHECKED | CSUM_DATA_VALID)) {
1115 m->m_pkthdr.csum_data = 0;
1116 m->m_pkthdr.csum_flags &= ~(CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1117 }
1118
984263bc
MD
1119 /*
1120 * Presence of header sizes in mbufs
1121 * would confuse code below.
1122 */
1123 m->m_data += hlen;
1124 m->m_len -= hlen;
1125
1126 /*
1127 * If first fragment to arrive, create a reassembly queue.
1128 */
dffa46cd 1129 if (fp == NULL) {
2b89bacf 1130 if ((fp = mpipe_alloc_nowait(&ipq_mpipe)) == NULL)
984263bc 1131 goto dropfrag;
9691feb6 1132 insque(fp, &ipq[sum]);
984263bc
MD
1133 nipq++;
1134 fp->ipq_nfrags = 1;
1135 fp->ipq_ttl = IPFRAGTTL;
1136 fp->ipq_p = ip->ip_p;
1137 fp->ipq_id = ip->ip_id;
1138 fp->ipq_src = ip->ip_src;
1139 fp->ipq_dst = ip->ip_dst;
1140 fp->ipq_frags = m;
1141 m->m_nextpkt = NULL;
1142#ifdef IPDIVERT
1143 fp->ipq_div_info = 0;
984263bc
MD
1144#endif
1145 goto inserted;
1146 } else {
1147 fp->ipq_nfrags++;
1148 }
1149
dffa46cd 1150#define GETIP(m) ((struct ip*)((m)->m_pkthdr.header))
984263bc
MD
1151
1152 /*
1153 * Find a segment which begins after this one does.
1154 */
1155 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
1156 if (GETIP(q)->ip_off > ip->ip_off)
1157 break;
1158
1159 /*
1160 * If there is a preceding segment, it may provide some of
1161 * our data already. If so, drop the data from the incoming
1162 * segment. If it provides all of our data, drop us, otherwise
1163 * stick new segment in the proper place.
1164 *
1165 * If some of the data is dropped from the the preceding
1166 * segment, then it's checksum is invalidated.
1167 */
1168 if (p) {
1169 i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
1170 if (i > 0) {
1171 if (i >= ip->ip_len)
1172 goto dropfrag;
1173 m_adj(m, i);
1174 m->m_pkthdr.csum_flags = 0;
1175 ip->ip_off += i;
1176 ip->ip_len -= i;
1177 }
1178 m->m_nextpkt = p->m_nextpkt;
1179 p->m_nextpkt = m;
1180 } else {
1181 m->m_nextpkt = fp->ipq_frags;
1182 fp->ipq_frags = m;
1183 }
1184
1185 /*
1186 * While we overlap succeeding segments trim them or,
1187 * if they are completely covered, dequeue them.
1188 */
1189 for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
1190 q = nq) {
dffa46cd 1191 i = (ip->ip_off + ip->ip_len) - GETIP(q)->ip_off;
984263bc
MD
1192 if (i < GETIP(q)->ip_len) {
1193 GETIP(q)->ip_len -= i;
1194 GETIP(q)->ip_off += i;
1195 m_adj(q, i);
1196 q->m_pkthdr.csum_flags = 0;
1197 break;
1198 }
1199 nq = q->m_nextpkt;
1200 m->m_nextpkt = nq;
1201 ipstat.ips_fragdropped++;
1202 fp->ipq_nfrags--;
c4933956 1203 q->m_nextpkt = NULL;
984263bc
MD
1204 m_freem(q);
1205 }
1206
1207inserted:
1208
1209#ifdef IPDIVERT
1210 /*
1211 * Transfer firewall instructions to the fragment structure.
1212 * Only trust info in the fragment at offset 0.
1213 */
1214 if (ip->ip_off == 0) {
1215 fp->ipq_div_info = *divinfo;
e5ecc832
JS
1216 } else {
1217 mtag = m_tag_find(m, PACKET_TAG_IPFW_DIVERT, NULL);
1218 if (mtag != NULL)
1219 m_tag_delete(m, mtag);
984263bc
MD
1220 }
1221 *divinfo = 0;
984263bc
MD
1222#endif
1223
1224 /*
1225 * Check for complete reassembly and perform frag per packet
1226 * limiting.
1227 *
1228 * Frag limiting is performed here so that the nth frag has
1229 * a chance to complete the packet before we drop the packet.
1230 * As a result, n+1 frags are actually allowed per packet, but
1231 * only n will ever be stored. (n = maxfragsperpacket.)
1232 *
1233 */
1234 next = 0;
1235 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
1236 if (GETIP(q)->ip_off != next) {
1237 if (fp->ipq_nfrags > maxfragsperpacket) {
1238 ipstat.ips_fragdropped += fp->ipq_nfrags;
1239 ip_freef(fp);
1240 }
dffa46cd 1241 return (NULL);
984263bc
MD
1242 }
1243 next += GETIP(q)->ip_len;
1244 }
1245 /* Make sure the last packet didn't have the IP_MF flag */
1246 if (p->m_flags & M_FRAG) {
1247 if (fp->ipq_nfrags > maxfragsperpacket) {
1248 ipstat.ips_fragdropped += fp->ipq_nfrags;
1249 ip_freef(fp);
1250 }
dffa46cd 1251 return (NULL);
984263bc
MD
1252 }
1253
1254 /*
1255 * Reassembly is complete. Make sure the packet is a sane size.
1256 */
1257 q = fp->ipq_frags;
1258 ip = GETIP(q);
1259 if (next + (IP_VHL_HL(ip->ip_vhl) << 2) > IP_MAXPACKET) {
1260 ipstat.ips_toolong++;
1261 ipstat.ips_fragdropped += fp->ipq_nfrags;
1262 ip_freef(fp);
dffa46cd 1263 return (NULL);
984263bc
MD
1264 }
1265
1266 /*
1267 * Concatenate fragments.
1268 */
1269 m = q;
2b89bacf 1270 n = m->m_next;
dffa46cd 1271 m->m_next = NULL;
2b89bacf 1272 m_cat(m, n);
984263bc 1273 nq = q->m_nextpkt;
dffa46cd 1274 q->m_nextpkt = NULL;
984263bc
MD
1275 for (q = nq; q != NULL; q = nq) {
1276 nq = q->m_nextpkt;
1277 q->m_nextpkt = NULL;
1278 m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
1279 m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
1280 m_cat(m, q);
1281 }
1282
1e7aea7f
MD
1283 /*
1284 * Clean up the 1's complement checksum. Carry over 16 bits must
1285 * be added back. This assumes no more then 65535 packet fragments
37ed010a 1286 * were reassembled. A second carry can also occur (but not a third).
1e7aea7f
MD
1287 */
1288 m->m_pkthdr.csum_data = (m->m_pkthdr.csum_data & 0xffff) +
1289 (m->m_pkthdr.csum_data >> 16);
37ed010a
MD
1290 if (m->m_pkthdr.csum_data > 0xFFFF)
1291 m->m_pkthdr.csum_data -= 0xFFFF;
1e7aea7f
MD
1292
1293
984263bc
MD
1294#ifdef IPDIVERT
1295 /*
1296 * Extract firewall instructions from the fragment structure.
1297 */
1298 *divinfo = fp->ipq_div_info;
984263bc
MD
1299#endif
1300
1301 /*
1302 * Create header for new ip packet by
1303 * modifying header of first packet;
1304 * dequeue and discard fragment reassembly header.
1305 * Make header visible.
1306 */
1307 ip->ip_len = next;
1308 ip->ip_src = fp->ipq_src;
1309 ip->ip_dst = fp->ipq_dst;
1310 remque(fp);
1311 nipq--;
2b89bacf 1312 mpipe_free(&ipq_mpipe, fp);
984263bc
MD
1313 m->m_len += (IP_VHL_HL(ip->ip_vhl) << 2);
1314 m->m_data -= (IP_VHL_HL(ip->ip_vhl) << 2);
1315 /* some debugging cruft by sklower, below, will go away soon */
1316 if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
2256ba69 1317 int plen = 0;
dffa46cd 1318
2b89bacf
MD
1319 for (n = m; n; n = n->m_next)
1320 plen += n->m_len;
984263bc
MD
1321 m->m_pkthdr.len = plen;
1322 }
d2f6f5b2
SZ
1323
1324 ipstat.ips_reassembled++;
984263bc
MD
1325 return (m);
1326
1327dropfrag:
1328#ifdef IPDIVERT
1329 *divinfo = 0;
984263bc
MD
1330#endif
1331 ipstat.ips_fragdropped++;
dffa46cd 1332 if (fp != NULL)
984263bc
MD
1333 fp->ipq_nfrags--;
1334 m_freem(m);
dffa46cd 1335 return (NULL);
984263bc
MD
1336
1337#undef GETIP
1338}
1339
1340/*
1341 * Free a fragment reassembly header and all
1342 * associated datagrams.
1343 */
1344static void
dffa46cd 1345ip_freef(struct ipq *fp)
984263bc 1346{
2256ba69 1347 struct mbuf *q;
984263bc
MD
1348
1349 while (fp->ipq_frags) {
1350 q = fp->ipq_frags;
1351 fp->ipq_frags = q->m_nextpkt;
c4933956 1352 q->m_nextpkt = NULL;
984263bc
MD
1353 m_freem(q);
1354 }
1355 remque(fp);
2b89bacf 1356 mpipe_free(&ipq_mpipe, fp);
984263bc
MD
1357 nipq--;
1358}
1359
1360/*
1361 * IP timer processing;
1362 * if a timer expires on a reassembly
1363 * queue, discard it.
1364 */
1365void
8a3125c6 1366ip_slowtimo(void)
984263bc 1367{
2256ba69 1368 struct ipq *fp;
984263bc
MD
1369 int i;
1370
1cae611f 1371 crit_enter();
984263bc
MD
1372 for (i = 0; i < IPREASS_NHASH; i++) {
1373 fp = ipq[i].next;
dffa46cd 1374 if (fp == NULL)
984263bc
MD
1375 continue;
1376 while (fp != &ipq[i]) {
1377 --fp->ipq_ttl;
1378 fp = fp->next;
1379 if (fp->prev->ipq_ttl == 0) {
1380 ipstat.ips_fragtimeout += fp->prev->ipq_nfrags;
1381 ip_freef(fp->prev);
1382 }
1383 }
1384 }
1385 /*
1386 * If we are over the maximum number of fragments
1387 * (due to the limit being lowered), drain off
1388 * enough to get down to the new limit.
1389 */
1390 if (maxnipq >= 0 && nipq > maxnipq) {
1391 for (i = 0; i < IPREASS_NHASH; i++) {
1392 while (nipq > maxnipq &&
1393 (ipq[i].next != &ipq[i])) {
1394 ipstat.ips_fragdropped +=
1395 ipq[i].next->ipq_nfrags;
1396 ip_freef(ipq[i].next);
1397 }
1398 }
1399 }
1400 ipflow_slowtimo();
1cae611f 1401 crit_exit();
984263bc
MD
1402}
1403
1404/*
1405 * Drain off all datagram fragments.
1406 */
1407void
8a3125c6 1408ip_drain(void)
984263bc 1409{
dffa46cd 1410 int i;
984263bc
MD
1411
1412 for (i = 0; i < IPREASS_NHASH; i++) {
1413 while (ipq[i].next != &ipq[i]) {
1414 ipstat.ips_fragdropped += ipq[i].next->ipq_nfrags;
1415 ip_freef(ipq[i].next);
1416 }
1417 }
1418 in_rtqdrain();
1419}
1420
1421/*
1422 * Do option processing on a datagram,
1423 * possibly discarding it if bad options are encountered,
1424 * or forwarding it if source-routed.
1425 * The pass argument is used when operating in the IPSTEALTH
1426 * mode to tell what options to process:
1427 * [LS]SRR (pass 0) or the others (pass 1).
1428 * The reason for as many as two passes is that when doing IPSTEALTH,
1429 * non-routing options should be processed only if the packet is for us.
1430 * Returns 1 if packet has been forwarded/freed,
1431 * 0 if the packet should be processed further.
1432 */
1433static int
1434ip_dooptions(struct mbuf *m, int pass, struct sockaddr_in *next_hop)
1435{
407e896e 1436 struct sockaddr_in ipaddr = { sizeof ipaddr, AF_INET };
984263bc
MD
1437 struct ip *ip = mtod(m, struct ip *);
1438 u_char *cp;
1439 struct in_ifaddr *ia;
dffa46cd
JH
1440 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB;
1441 boolean_t forward = FALSE;
984263bc
MD
1442 struct in_addr *sin, dst;
1443 n_time ntime;
1444
1445 dst = ip->ip_dst;
1446 cp = (u_char *)(ip + 1);
dffa46cd 1447 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof(struct ip);
984263bc
MD
1448 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1449 opt = cp[IPOPT_OPTVAL];
1450 if (opt == IPOPT_EOL)
1451 break;
1452 if (opt == IPOPT_NOP)
1453 optlen = 1;
1454 else {
1455 if (cnt < IPOPT_OLEN + sizeof(*cp)) {
1456 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1457 goto bad;
1458 }
1459 optlen = cp[IPOPT_OLEN];
1460 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
1461 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1462 goto bad;
1463 }
1464 }
1465 switch (opt) {
1466
1467 default:
1468 break;
1469
1470 /*
1471 * Source routing with record.
1472 * Find interface with current destination address.
1473 * If none on this machine then drop if strictly routed,
1474 * or do nothing if loosely routed.
1475 * Record interface address and bring up next address
1476 * component. If strictly routed make sure next
1477 * address is on directly accessible net.
1478 */
1479 case IPOPT_LSRR:
1480 case IPOPT_SSRR:
984263bc
MD
1481 if (ipstealth && pass > 0)
1482 break;
984263bc
MD
1483 if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1484 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1485 goto bad;
1486 }
1487 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1488 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1489 goto bad;
1490 }
1491 ipaddr.sin_addr = ip->ip_dst;
1492 ia = (struct in_ifaddr *)
1493 ifa_ifwithaddr((struct sockaddr *)&ipaddr);
dffa46cd 1494 if (ia == NULL) {
984263bc
MD
1495 if (opt == IPOPT_SSRR) {
1496 type = ICMP_UNREACH;
1497 code = ICMP_UNREACH_SRCFAIL;
1498 goto bad;
1499 }
1500 if (!ip_dosourceroute)
1501 goto nosourcerouting;
1502 /*
1503 * Loose routing, and not at next destination
1504 * yet; nothing to do except forward.
1505 */
1506 break;
1507 }
1508 off--; /* 0 origin */
1509 if (off > optlen - (int)sizeof(struct in_addr)) {
1510 /*
1511 * End of source route. Should be for us.
1512 */
1513 if (!ip_acceptsourceroute)
1514 goto nosourcerouting;
1515 save_rte(cp, ip->ip_src);
1516 break;
1517 }
984263bc
MD
1518 if (ipstealth)
1519 goto dropit;
984263bc
MD
1520 if (!ip_dosourceroute) {
1521 if (ipforwarding) {
1141eb20
JH
1522 char buf[sizeof "aaa.bbb.ccc.ddd"];
1523
984263bc
MD
1524 /*
1525 * Acting as a router, so generate ICMP
1526 */
1527nosourcerouting:
1528 strcpy(buf, inet_ntoa(ip->ip_dst));
dffa46cd 1529 log(LOG_WARNING,
984263bc
MD
1530 "attempted source route from %s to %s\n",
1531 inet_ntoa(ip->ip_src), buf);
1532 type = ICMP_UNREACH;
1533 code = ICMP_UNREACH_SRCFAIL;
1534 goto bad;
1535 } else {
1536 /*
dffa46cd
JH
1537 * Not acting as a router,
1538 * so silently drop.
984263bc 1539 */
984263bc 1540dropit:
984263bc
MD
1541 ipstat.ips_cantforward++;
1542 m_freem(m);
1543 return (1);
1544 }
1545 }
1546
1547 /*
1548 * locate outgoing interface
1549 */
f23061d4
JH
1550 memcpy(&ipaddr.sin_addr, cp + off,
1551 sizeof ipaddr.sin_addr);
984263bc
MD
1552
1553 if (opt == IPOPT_SSRR) {
1554#define INA struct in_ifaddr *
1555#define SA struct sockaddr *
dffa46cd
JH
1556 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr))
1557 == NULL)
1558 ia = (INA)ifa_ifwithnet((SA)&ipaddr);
984263bc 1559 } else
ecdefdda
MD
1560 ia = ip_rtaddr(ipaddr.sin_addr,
1561 &ipforward_rt[mycpuid]);
dffa46cd 1562 if (ia == NULL) {
984263bc
MD
1563 type = ICMP_UNREACH;
1564 code = ICMP_UNREACH_SRCFAIL;
1565 goto bad;
1566 }
1567 ip->ip_dst = ipaddr.sin_addr;
f23061d4 1568 memcpy(cp + off, &IA_SIN(ia)->sin_addr,
984263bc
MD
1569 sizeof(struct in_addr));
1570 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1571 /*
1572 * Let ip_intr's mcast routing check handle mcast pkts
1573 */
1574 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
1575 break;
1576
1577 case IPOPT_RR:
984263bc
MD
1578 if (ipstealth && pass == 0)
1579 break;
984263bc
MD
1580 if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1581 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1582 goto bad;
1583 }
1584 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1585 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1586 goto bad;
1587 }
1588 /*
1589 * If no space remains, ignore.
1590 */
1591 off--; /* 0 origin */
1592 if (off > optlen - (int)sizeof(struct in_addr))
1593 break;
f23061d4
JH
1594 memcpy(&ipaddr.sin_addr, &ip->ip_dst,
1595 sizeof ipaddr.sin_addr);
984263bc
MD
1596 /*
1597 * locate outgoing interface; if we're the destination,
1598 * use the incoming interface (should be same).
1599 */
dffa46cd 1600 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == NULL &&
ecdefdda
MD
1601 (ia = ip_rtaddr(ipaddr.sin_addr,
1602 &ipforward_rt[mycpuid]))
dffa46cd 1603 == NULL) {
984263bc
MD
1604 type = ICMP_UNREACH;
1605 code = ICMP_UNREACH_HOST;
1606 goto bad;
1607 }
f23061d4 1608 memcpy(cp + off, &IA_SIN(ia)->sin_addr,
984263bc
MD
1609 sizeof(struct in_addr));
1610 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1611 break;
1612
1613 case IPOPT_TS:
984263bc
MD
1614 if (ipstealth && pass == 0)
1615 break;
984263bc
MD
1616 code = cp - (u_char *)ip;
1617 if (optlen < 4 || optlen > 40) {
1618 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1619 goto bad;
1620 }
1621 if ((off = cp[IPOPT_OFFSET]) < 5) {
1622 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1623 goto bad;
1624 }
1625 if (off > optlen - (int)sizeof(int32_t)) {
1626 cp[IPOPT_OFFSET + 1] += (1 << 4);
1627 if ((cp[IPOPT_OFFSET + 1] & 0xf0) == 0) {
1628 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1629 goto bad;
1630 }
1631 break;
1632 }
1633 off--; /* 0 origin */
1634 sin = (struct in_addr *)(cp + off);
1635 switch (cp[IPOPT_OFFSET + 1] & 0x0f) {
1636
1637 case IPOPT_TS_TSONLY:
1638 break;
1639
1640 case IPOPT_TS_TSANDADDR:
1641 if (off + sizeof(n_time) +
1642 sizeof(struct in_addr) > optlen) {
1643 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1644 goto bad;
1645 }
1646 ipaddr.sin_addr = dst;
1647 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
1648 m->m_pkthdr.rcvif);
dffa46cd 1649 if (ia == NULL)
984263bc 1650 continue;
f23061d4 1651 memcpy(sin, &IA_SIN(ia)->sin_addr,
984263bc
MD
1652 sizeof(struct in_addr));
1653 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1654 off += sizeof(struct in_addr);
1655 break;
1656
1657 case IPOPT_TS_PRESPEC:
1658 if (off + sizeof(n_time) +
1659 sizeof(struct in_addr) > optlen) {
1660 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1661 goto bad;
1662 }
f23061d4 1663 memcpy(&ipaddr.sin_addr, sin,
984263bc 1664 sizeof(struct in_addr));
dffa46cd 1665 if (ifa_ifwithaddr((SA)&ipaddr) == NULL)
984263bc
MD
1666 continue;
1667 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1668 off += sizeof(struct in_addr);
1669 break;
1670
1671 default:
1672 code = &cp[IPOPT_OFFSET + 1] - (u_char *)ip;
1673 goto bad;
1674 }
1675 ntime = iptime();
f23061d4 1676 memcpy(cp + off, &ntime, sizeof(n_time));
984263bc
MD
1677 cp[IPOPT_OFFSET] += sizeof(n_time);
1678 }
1679 }
1680 if (forward && ipforwarding) {
41332872 1681 ip_forward(m, TRUE, next_hop);
984263bc
MD
1682 return (1);
1683 }
1684 return (0);
1685bad:
981773b8 1686 icmp_error(m, type, code, 0, 0);
984263bc
MD
1687 ipstat.ips_badoptions++;
1688 return (1);
1689}
1690
1691/*
1692 * Given address of next destination (final or next hop),
1693 * return internet address info of interface to be used to get there.
1694 */
1695struct in_ifaddr *
41332872 1696ip_rtaddr(struct in_addr dst, struct route *ro)
984263bc 1697{
2256ba69 1698 struct sockaddr_in *sin;
984263bc 1699
41332872 1700 sin = (struct sockaddr_in *)&ro->ro_dst;
984263bc 1701
41332872
JH
1702 if (ro->ro_rt == NULL || dst.s_addr != sin->sin_addr.s_addr) {
1703 if (ro->ro_rt != NULL) {
1704 RTFREE(ro->ro_rt);
1705 ro->ro_rt = NULL;
984263bc
MD
1706 }
1707 sin->sin_family = AF_INET;
5fe66e68 1708 sin->sin_len = sizeof *sin;
984263bc 1709 sin->sin_addr = dst;
41332872 1710 rtalloc_ign(ro, RTF_PRCLONING);
984263bc 1711 }
dffa46cd 1712
41332872 1713 if (ro->ro_rt == NULL)
dffa46cd
JH
1714 return (NULL);
1715
41332872 1716 return (ifatoia(ro->ro_rt->rt_ifa));
984263bc
MD
1717}
1718
1719/*
1720 * Save incoming source route for use in replies,
1721 * to be picked up later by ip_srcroute if the receiver is interested.
1722 */
102cfd56 1723static void
dffa46cd 1724save_rte(u_char *option, struct in_addr dst)
984263bc
MD
1725{
1726 unsigned olen;
1727
1728 olen = option[IPOPT_OLEN];
1729#ifdef DIAGNOSTIC
1730 if (ipprintfs)
a6ec04bc 1731 kprintf("save_rte: olen %d\n", olen);
984263bc
MD
1732#endif
1733 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
1734 return;
1735 bcopy(option, ip_srcrt.srcopt, olen);
1736 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1737 ip_srcrt.dst = dst;
1738}
1739
1740/*
1741 * Retrieve incoming source route for use in replies,
1742 * in the same form used by setsockopt.
1743 * The first hop is placed before the options, will be removed later.
1744 */
1745struct mbuf *
8a3125c6 1746ip_srcroute(void)
984263bc 1747{
2256ba69
RG
1748 struct in_addr *p, *q;
1749 struct mbuf *m;
984263bc
MD
1750
1751 if (ip_nhops == 0)
dffa46cd 1752 return (NULL);
74f1caca 1753 m = m_get(MB_DONTWAIT, MT_HEADER);
dffa46cd
JH
1754 if (m == NULL)
1755 return (NULL);
984263bc 1756
dffa46cd 1757#define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
984263bc
MD
1758
1759 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1760 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
1761 OPTSIZ;
1762#ifdef DIAGNOSTIC
1763 if (ipprintfs)
a6ec04bc 1764 kprintf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
984263bc
MD
1765#endif
1766
1767 /*
1768 * First save first hop for return route
1769 */
1770 p = &ip_srcrt.route[ip_nhops - 1];
1771 *(mtod(m, struct in_addr *)) = *p--;
1772#ifdef DIAGNOSTIC
1773 if (ipprintfs)
a6ec04bc 1774 kprintf(" hops %x", ntohl(mtod(m, struct in_addr *)->s_addr));
984263bc
MD
1775#endif
1776
1777 /*
1778 * Copy option fields and padding (nop) to mbuf.
1779 */
1780 ip_srcrt.nop = IPOPT_NOP;
1781 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
f23061d4 1782 memcpy(mtod(m, caddr_t) + sizeof(struct in_addr), &ip_srcrt.nop,
dffa46cd 1783 OPTSIZ);
984263bc
MD
1784 q = (struct in_addr *)(mtod(m, caddr_t) +
1785 sizeof(struct in_addr) + OPTSIZ);
1786#undef OPTSIZ
1787 /*
1788 * Record return path as an IP source route,
1789 * reversing the path (pointers are now aligned).
1790 */
1791 while (p >= ip_srcrt.route) {
1792#ifdef DIAGNOSTIC
1793 if (ipprintfs)
a6ec04bc 1794 kprintf(" %x", ntohl(q->s_addr));
984263bc
MD
1795#endif
1796 *q++ = *p--;
1797 }
1798 /*
1799 * Last hop goes to final destination.
1800 */
1801 *q = ip_srcrt.dst;
1802#ifdef DIAGNOSTIC
1803 if (ipprintfs)
a6ec04bc 1804 kprintf(" %x\n", ntohl(q->s_addr));
984263bc
MD
1805#endif
1806 return (m);
1807}
1808
1809/*
bddf0751 1810 * Strip out IP options.
984263bc
MD
1811 */
1812void
bddf0751 1813ip_stripoptions(struct mbuf *m)
984263bc 1814{
bddf0751 1815 int datalen;
984263bc 1816 struct ip *ip = mtod(m, struct ip *);
2256ba69 1817 caddr_t opts;
bddf0751 1818 int optlen;
984263bc 1819
bddf0751 1820 optlen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof(struct ip);
984263bc 1821 opts = (caddr_t)(ip + 1);
bddf0751
JH
1822 datalen = m->m_len - (sizeof(struct ip) + optlen);
1823 bcopy(opts + optlen, opts, datalen);
1824 m->m_len -= optlen;
984263bc 1825 if (m->m_flags & M_PKTHDR)
bddf0751 1826 m->m_pkthdr.len -= optlen;
984263bc
MD
1827 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2);
1828}
1829
1830u_char inetctlerrmap[PRC_NCMDS] = {
1831 0, 0, 0, 0,
1832 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1833 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1834 EMSGSIZE, EHOSTUNREACH, 0, 0,
1835 0, 0, 0, 0,
1836 ENOPROTOOPT, ECONNREFUSED
1837};
1838
1839/*
1840 * Forward a packet. If some error occurs return the sender
1841 * an icmp packet. Note we can't always generate a meaningful
1842 * icmp message because icmp doesn't have a large enough repertoire
1843 * of codes and types.
1844 *
1845 * If not forwarding, just drop the packet. This could be confusing
1846 * if ipforwarding was zero but some routing protocol was advancing
1847 * us as a gateway to somewhere. However, we must let the routing
1848 * protocol deal with that.
1849 *
dffa46cd 1850 * The using_srcrt parameter indicates whether the packet is being forwarded
984263bc
MD
1851 * via a source route.
1852 */
33cc7bb1 1853void
41332872 1854ip_forward(struct mbuf *m, boolean_t using_srcrt, struct sockaddr_in *next_hop)
984263bc
MD
1855{
1856 struct ip *ip = mtod(m, struct ip *);
00392387 1857 struct sockaddr_in *ipforward_rtaddr;
984263bc 1858 struct rtentry *rt;
981773b8 1859 int error, type = 0, code = 0, destmtu = 0;
984263bc
MD
1860 struct mbuf *mcopy;
1861 n_long dest;
1862 struct in_addr pkt_dst;
ecdefdda 1863 struct route *cache_rt = &ipforward_rt[mycpuid];
984263bc 1864
be23faf1 1865 dest = INADDR_ANY;
984263bc
MD
1866 /*
1867 * Cache the destination address of the packet; this may be
1868 * changed by use of 'ipfw fwd'.
1869 */
5fe66e68 1870 pkt_dst = (next_hop != NULL) ? next_hop->sin_addr : ip->ip_dst;
984263bc
MD
1871
1872#ifdef DIAGNOSTIC
1873 if (ipprintfs)
a6ec04bc 1874 kprintf("forward: src %x dst %x ttl %x\n",
dffa46cd 1875 ip->ip_src.s_addr, pkt_dst.s_addr, ip->ip_ttl);
984263bc
MD
1876#endif
1877
dffa46cd 1878 if (m->m_flags & (M_BCAST | M_MCAST) || !in_canforward(pkt_dst)) {
984263bc
MD
1879 ipstat.ips_cantforward++;
1880 m_freem(m);
1881 return;
1882 }
dffa46cd 1883 if (!ipstealth && ip->ip_ttl <= IPTTLDEC) {
981773b8 1884 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0);
dffa46cd 1885 return;
984263bc 1886 }
984263bc 1887
ecdefdda
MD
1888 ipforward_rtaddr = (struct sockaddr_in *) &cache_rt->ro_dst;
1889 if (cache_rt->ro_rt == NULL ||
00392387 1890 ipforward_rtaddr->sin_addr.s_addr != pkt_dst.s_addr) {
ecdefdda
MD
1891 if (cache_rt->ro_rt != NULL) {
1892 RTFREE(cache_rt->ro_rt);
1893 cache_rt->ro_rt = NULL;
984263bc 1894 }
00392387
JH
1895 ipforward_rtaddr->sin_family = AF_INET;
1896 ipforward_rtaddr->sin_len = sizeof(struct sockaddr_in);
1897 ipforward_rtaddr->sin_addr = pkt_dst;
ecdefdda
MD
1898 rtalloc_ign(cache_rt, RTF_PRCLONING);
1899 if (cache_rt->ro_rt == NULL) {
981773b8 1900 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
984263bc
MD
1901 return;
1902 }
984263bc 1903 }
ecdefdda 1904 rt = cache_rt->ro_rt;
984263bc
MD
1905
1906 /*
1907 * Save the IP header and at most 8 bytes of the payload,
1908 * in case we need to generate an ICMP message to the src.
1909 *
1910 * XXX this can be optimized a lot by saving the data in a local
1911 * buffer on the stack (72 bytes at most), and only allocating the
1912 * mbuf if really necessary. The vast majority of the packets
1913 * are forwarded without having to send an ICMP back (either
1914 * because unnecessary, or because rate limited), so we are
1915 * really we are wasting a lot of work here.
1916 *
1917 * We don't use m_copy() because it might return a reference
1918 * to a shared cluster. Both this function and ip_output()
1919 * assume exclusive access to the IP header in `m', so any
1920 * data in a cluster may change before we reach icmp_error().
1921 */
85c8d4e6 1922 MGETHDR(mcopy, MB_DONTWAIT, m->m_type);
74f1caca 1923 if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, MB_DONTWAIT)) {
984263bc
MD
1924 /*
1925 * It's probably ok if the pkthdr dup fails (because
1926 * the deep copy of the tag chain failed), but for now
1927 * be conservative and just discard the copy since
1928 * code below may some day want the tags.
1929 */
1930 m_free(mcopy);
1931 mcopy = NULL;
1932 }
1933 if (mcopy != NULL) {
1934 mcopy->m_len = imin((IP_VHL_HL(ip->ip_vhl) << 2) + 8,
1935 (int)ip->ip_len);
52560a13 1936 mcopy->m_pkthdr.len = mcopy->m_len;
984263bc
MD
1937 m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1938 }
1939
dffa46cd 1940 if (!ipstealth)
984263bc 1941 ip->ip_ttl -= IPTTLDEC;
984263bc
MD
1942
1943 /*
1944 * If forwarding packet using same interface that it came in on,
1945 * perhaps should send a redirect to sender to shortcut a hop.
1946 * Only send redirect if source is sending directly to us,
1947 * and if packet was not source routed (or has any options).
1948 * Also, don't send redirect if forwarding using a default route
1949 * or a route modified by a redirect.
1950 */
1951 if (rt->rt_ifp == m->m_pkthdr.rcvif &&
dffa46cd
JH
1952 !(rt->rt_flags & (RTF_DYNAMIC | RTF_MODIFIED)) &&
1953 satosin(rt_key(rt))->sin_addr.s_addr != INADDR_ANY &&
983015c8 1954 ipsendredirects && !using_srcrt && next_hop == NULL) {
984263bc 1955 u_long src = ntohl(ip->ip_src.s_addr);
41332872 1956 struct in_ifaddr *rt_ifa = (struct in_ifaddr *)rt->rt_ifa;
984263bc 1957
41332872
JH
1958 if (rt_ifa != NULL &&
1959 (src & rt_ifa->ia_subnetmask) == rt_ifa->ia_subnet) {
dffa46cd
JH
1960 if (rt->rt_flags & RTF_GATEWAY)
1961 dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1962 else
1963 dest = pkt_dst.s_addr;
1964 /*
1965 * Router requirements says to only send
1966 * host redirects.
1967 */
1968 type = ICMP_REDIRECT;
1969 code = ICMP_REDIRECT_HOST;
984263bc 1970#ifdef DIAGNOSTIC
dffa46cd 1971 if (ipprintfs)
a6ec04bc 1972 kprintf("redirect (%d) to %x\n", code, dest);
984263bc
MD
1973#endif
1974 }
1975 }
1976
5de23090 1977 error = ip_output(m, NULL, cache_rt, IP_FORWARDING, NULL, NULL);
41332872 1978 if (error == 0) {
984263bc 1979 ipstat.ips_forward++;
41332872 1980 if (type == 0) {
984263bc 1981 if (mcopy) {
ecdefdda 1982 ipflow_create(cache_rt, mcopy);
297c8124 1983 m_freem(mcopy);
984263bc 1984 }
41332872
JH
1985 return; /* most common case */
1986 } else {
1987 ipstat.ips_redirectsent++;
984263bc 1988 }
41332872
JH
1989 } else {
1990 ipstat.ips_cantforward++;
984263bc 1991 }
41332872 1992
984263bc
MD
1993 if (mcopy == NULL)
1994 return;
41332872
JH
1995
1996 /*
1997 * Send ICMP message.
1998 */
984263bc
MD
1999
2000 switch (error) {
2001
2002 case 0: /* forwarded, but need redirect */
2003 /* type, code set above */
2004 break;
2005
2006 case ENETUNREACH: /* shouldn't happen, checked above */
2007 case EHOSTUNREACH:
2008 case ENETDOWN:
2009 case EHOSTDOWN:
2010 default:
2011 type = ICMP_UNREACH;
2012 code = ICMP_UNREACH_HOST;
2013 break;
2014
2015 case EMSGSIZE:
2016 type = ICMP_UNREACH;
2017 code = ICMP_UNREACH_NEEDFRAG;
2018#ifdef IPSEC
2019 /*
2020 * If the packet is routed over IPsec tunnel, tell the
2021 * originator the tunnel MTU.
2022 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
2023 * XXX quickhack!!!
2024 */
ecdefdda 2025 if (cache_rt->ro_rt != NULL) {
984263bc
MD
2026 struct secpolicy *sp = NULL;
2027 int ipsecerror;
2028 int ipsechdr;
2029 struct route *ro;
2030
2031 sp = ipsec4_getpolicybyaddr(mcopy,
2032 IPSEC_DIR_OUTBOUND,
dffa46cd
JH
2033 IP_FORWARDING,
2034 &ipsecerror);
984263bc
MD
2035
2036 if (sp == NULL)
ecdefdda 2037 destmtu = cache_rt->ro_rt->rt_ifp->if_mtu;
984263bc
MD
2038 else {
2039 /* count IPsec header size */
2040 ipsechdr = ipsec4_hdrsiz(mcopy,
2041 IPSEC_DIR_OUTBOUND,
2042 NULL);
2043
2044 /*
2045 * find the correct route for outer IPv4
2046 * header, compute tunnel MTU.
2047 *
984263bc 2048 */
012d77ea
JH
2049 if (sp->req != NULL && sp->req->sav != NULL &&
2050 sp->req->sav->sah != NULL) {
984263bc 2051 ro = &sp->req->sav->sah->sa_route;
012d77ea
JH
2052 if (ro->ro_rt != NULL &&
2053 ro->ro_rt->rt_ifp != NULL) {
981773b8 2054 destmtu =
984263bc 2055 ro->ro_rt->rt_ifp->if_mtu;
981773b8 2056 destmtu -= ipsechdr;
984263bc
MD
2057 }
2058 }
2059
2060 key_freesp(sp);
2061 }
2062 }
2063#elif FAST_IPSEC
2064 /*
2065 * If the packet is routed over IPsec tunnel, tell the
2066 * originator the tunnel MTU.
2067 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
2068 * XXX quickhack!!!
2069 */
ecdefdda 2070 if (cache_rt->ro_rt != NULL) {
984263bc
MD
2071 struct secpolicy *sp = NULL;
2072 int ipsecerror;
2073 int ipsechdr;
2074 struct route *ro;
2075
2076 sp = ipsec_getpolicybyaddr(mcopy,
2077 IPSEC_DIR_OUTBOUND,
dffa46cd
JH
2078 IP_FORWARDING,
2079 &ipsecerror);
984263bc
MD
2080
2081 if (sp == NULL)
ecdefdda 2082 destmtu = cache_rt->ro_rt->rt_ifp->if_mtu;
984263bc
MD
2083 else {
2084 /* count IPsec header size */
2085 ipsechdr = ipsec4_hdrsiz(mcopy,
2086 IPSEC_DIR_OUTBOUND,
2087 NULL);
2088
2089 /*
2090 * find the correct route for outer IPv4
2091 * header, compute tunnel MTU.
984263bc 2092 */
981773b8 2093
012d77ea
JH
2094 if (sp->req != NULL &&
2095 sp->req->sav != NULL &&
2096 sp->req->sav->sah != NULL) {
984263bc 2097 ro = &sp->req->sav->sah->sa_route;
012d77ea
JH
2098 if (ro->ro_rt != NULL &&
2099 ro->ro_rt->rt_ifp != NULL) {
981773b8 2100 destmtu =
984263bc 2101 ro->ro_rt->rt_ifp->if_mtu;
981773b8 2102 destmtu -= ipsechdr;
984263bc
MD
2103 }
2104 }
2105
2106 KEY_FREESP(&sp);
2107 }
2108 }
2109#else /* !IPSEC && !FAST_IPSEC */
ecdefdda
MD
2110 if (cache_rt->ro_rt != NULL)
2111 destmtu = cache_rt->ro_rt->rt_ifp->if_mtu;
984263bc
MD
2112#endif /*IPSEC*/
2113 ipstat.ips_cantfrag++;
2114 break;
2115
2116 case ENOBUFS:
2117 /*
2118 * A router should not generate ICMP_SOURCEQUENCH as
2119 * required in RFC1812 Requirements for IP Version 4 Routers.
2120 * Source quench could be a big problem under DoS attacks,
2121 * or if the underlying interface is rate-limited.
2122 * Those who need source quench packets may re-enable them
2123 * via the net.inet.ip.sendsourcequench sysctl.
2124 */
dffa46cd 2125 if (!ip_sendsourcequench) {
984263bc
MD
2126 m_freem(mcopy);
2127 return;
2128 } else {
2129 type = ICMP_SOURCEQUENCH;
2130 code = 0;
2131 }
2132 break;
2133
2134 case EACCES: /* ipfw denied packet */
2135 m_freem(mcopy);
2136 return;
2137 }
981773b8 2138 icmp_error(mcopy, type, code, dest, destmtu);
984263bc
MD
2139}
2140
2141void
dffa46cd
JH
2142ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
2143 struct mbuf *m)
984263bc
MD
2144{
2145 if (inp->inp_socket->so_options & SO_TIMESTAMP) {
2146 struct timeval tv;
2147
2148 microtime(&tv);
2149 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
dffa46cd 2150 SCM_TIMESTAMP, SOL_SOCKET);
984263bc
MD
2151 if (*mp)
2152 mp = &(*mp)->m_next;
2153 }
2154 if (inp->inp_flags & INP_RECVDSTADDR) {
2155 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
2156 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
2157 if (*mp)
2158 mp = &(*mp)->m_next;
2159 }
95926362
MD
2160 if (inp->inp_flags & INP_RECVTTL) {
2161 *mp = sbcreatecontrol((caddr_t) &ip->ip_ttl,
2162 sizeof(u_char), IP_RECVTTL, IPPROTO_IP);
2163 if (*mp)
2164 mp = &(*mp)->m_next;
2165 }
984263bc
MD
2166#ifdef notyet
2167 /* XXX
2168 * Moving these out of udp_input() made them even more broken
2169 * than they already were.
2170 */
2171 /* options were tossed already */
2172 if (inp->inp_flags & INP_RECVOPTS) {
2173 *mp = sbcreatecontrol((caddr_t) opts_deleted_above,
2174 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
2175 if (*mp)
2176 mp = &(*mp)->m_next;
2177 }
2178 /* ip_srcroute doesn't do what we want here, need to fix */
2179 if (inp->inp_flags & INP_RECVRETOPTS) {
2180 *mp = sbcreatecontrol((caddr_t) ip_srcroute(),
2181 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
2182 if (*mp)
2183 mp = &(*mp)->m_next;
2184 }
2185#endif
2186 if (inp->inp_flags & INP_RECVIF) {
2187 struct ifnet *ifp;
2188 struct sdlbuf {
2189 struct sockaddr_dl sdl;
2190 u_char pad[32];
2191 } sdlbuf;
2192 struct sockaddr_dl *sdp;
2193 struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
2194
dffa46cd
JH
2195 if (((ifp = m->m_pkthdr.rcvif)) &&
2196 ((ifp->if_index != 0) && (ifp->if_index <= if_index))) {
f2682cb9 2197 sdp = IF_LLSOCKADDR(ifp);
984263bc
MD
2198 /*
2199 * Change our mind and don't try copy.
2200 */
dffa46cd
JH
2201 if ((sdp->sdl_family != AF_LINK) ||
2202 (sdp->sdl_len > sizeof(sdlbuf))) {
984263bc
MD
2203 goto makedummy;
2204 }
2205 bcopy(sdp, sdl2, sdp->sdl_len);
2206 } else {
dffa46cd
JH
2207makedummy:
2208 sdl2->sdl_len =
2209 offsetof(struct sockaddr_dl, sdl_data[0]);
984263bc
MD
2210 sdl2->sdl_family = AF_LINK;
2211 sdl2->sdl_index = 0;
2212 sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
2213 }
2214 *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len,
2215 IP_RECVIF, IPPROTO_IP);
2216 if (*mp)
2217 mp = &(*mp)->m_next;
2218 }
2219}
2220
2221/*
2222 * XXX these routines are called from the upper part of the kernel.
2223 *
2224 * They could also be moved to ip_mroute.c, since all the RSVP
2225 * handling is done there already.
2226 */
2227int
2228ip_rsvp_init(struct socket *so)
2229{
2230 if (so->so_type != SOCK_RAW ||
2231 so->so_proto->pr_protocol != IPPROTO_RSVP)
2232 return EOPNOTSUPP;
2233
2234 if (ip_rsvpd != NULL)
2235 return EADDRINUSE;
2236
2237 ip_rsvpd = so;
2238 /*
2239 * This may seem silly, but we need to be sure we don't over-increment
2240 * the RSVP counter, in case something slips up.
2241 */
2242 if (!ip_rsvp_on) {
2243 ip_rsvp_on = 1;
2244 rsvp_on++;
2245 }
2246
2247 return 0;
2248}
2249
2250int
2251ip_rsvp_done(void)
2252{
2253 ip_rsvpd = NULL;
2254 /*
2255 * This may seem silly, but we need to be sure we don't over-decrement
2256 * the RSVP counter, in case something slips up.
2257 */
2258 if (ip_rsvp_on) {
2259 ip_rsvp_on = 0;
2260 rsvp_on--;
2261 }
2262 return 0;
2263}
2264
2265void
a00138cb 2266rsvp_input(struct mbuf *m, ...) /* XXX must fixup manually */
984263bc 2267{
a00138cb
JS
2268 int off, proto;
2269 __va_list ap;
2270
2271 __va_start(ap, m);
2272 off = __va_arg(ap, int);
2273 proto = __va_arg(ap, int);
2274 __va_end(ap);
2275
984263bc
MD
2276 if (rsvp_input_p) { /* call the real one if loaded */
2277 rsvp_input_p(m, off, proto);
2278 return;
2279 }
2280
2281 /* Can still get packets with rsvp_on = 0 if there is a local member
2282 * of the group to which the RSVP packet is addressed. But in this
2283 * case we want to throw the packet away.
2284 */
2285
2286 if (!rsvp_on) {
2287 m_freem(m);
2288 return;
2289 }
2290
dffa46cd 2291 if (ip_rsvpd != NULL) {
984263bc
MD
2292 rip_input(m, off, proto);
2293 return;
2294 }
2295 /* Drop the packet */
2296 m_freem(m);
2297}