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