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