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