ip_input.c revision 1.266 1 /* $NetBSD: ip_input.c,v 1.266 2008/04/12 05:58:22 thorpej 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. Neither the name of the University nor the names of its contributors
82 * may be used to endorse or promote products derived from this software
83 * without specific prior written permission.
84 *
85 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
86 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
87 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
88 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
89 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
90 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
91 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
92 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
93 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
94 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
95 * SUCH DAMAGE.
96 *
97 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
98 */
99
100 #include <sys/cdefs.h>
101 __KERNEL_RCSID(0, "$NetBSD: ip_input.c,v 1.266 2008/04/12 05:58:22 thorpej Exp $");
102
103 #include "opt_inet.h"
104 #include "opt_gateway.h"
105 #include "opt_pfil_hooks.h"
106 #include "opt_ipsec.h"
107 #include "opt_mrouting.h"
108 #include "opt_mbuftrace.h"
109 #include "opt_inet_csum.h"
110
111 #include <sys/param.h>
112 #include <sys/systm.h>
113 #include <sys/malloc.h>
114 #include <sys/mbuf.h>
115 #include <sys/domain.h>
116 #include <sys/protosw.h>
117 #include <sys/socket.h>
118 #include <sys/socketvar.h>
119 #include <sys/errno.h>
120 #include <sys/time.h>
121 #include <sys/kernel.h>
122 #include <sys/pool.h>
123 #include <sys/sysctl.h>
124 #include <sys/kauth.h>
125
126 #include <net/if.h>
127 #include <net/if_dl.h>
128 #include <net/route.h>
129 #include <net/pfil.h>
130
131 #include <netinet/in.h>
132 #include <netinet/in_systm.h>
133 #include <netinet/ip.h>
134 #include <netinet/in_pcb.h>
135 #include <netinet/in_proto.h>
136 #include <netinet/in_var.h>
137 #include <netinet/ip_var.h>
138 #include <netinet/ip_private.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 #ifdef FAST_IPSEC
155 #include <netipsec/ipsec.h>
156 #include <netipsec/key.h>
157 #endif /* FAST_IPSEC*/
158
159 #ifndef IPFORWARDING
160 #ifdef GATEWAY
161 #define IPFORWARDING 1 /* forward IP packets not for us */
162 #else /* GATEWAY */
163 #define IPFORWARDING 0 /* don't forward IP packets not for us */
164 #endif /* GATEWAY */
165 #endif /* IPFORWARDING */
166 #ifndef IPSENDREDIRECTS
167 #define IPSENDREDIRECTS 1
168 #endif
169 #ifndef IPFORWSRCRT
170 #define IPFORWSRCRT 1 /* forward source-routed packets */
171 #endif
172 #ifndef IPALLOWSRCRT
173 #define IPALLOWSRCRT 1 /* allow source-routed packets */
174 #endif
175 #ifndef IPMTUDISC
176 #define IPMTUDISC 1
177 #endif
178 #ifndef IPMTUDISCTIMEOUT
179 #define IPMTUDISCTIMEOUT (10 * 60) /* as per RFC 1191 */
180 #endif
181
182 /*
183 * Note: DIRECTED_BROADCAST is handled this way so that previous
184 * configuration using this option will Just Work.
185 */
186 #ifndef IPDIRECTEDBCAST
187 #ifdef DIRECTED_BROADCAST
188 #define IPDIRECTEDBCAST 1
189 #else
190 #define IPDIRECTEDBCAST 0
191 #endif /* DIRECTED_BROADCAST */
192 #endif /* IPDIRECTEDBCAST */
193 int ipforwarding = IPFORWARDING;
194 int ipsendredirects = IPSENDREDIRECTS;
195 int ip_defttl = IPDEFTTL;
196 int ip_forwsrcrt = IPFORWSRCRT;
197 int ip_directedbcast = IPDIRECTEDBCAST;
198 int ip_allowsrcrt = IPALLOWSRCRT;
199 int ip_mtudisc = IPMTUDISC;
200 int ip_mtudisc_timeout = IPMTUDISCTIMEOUT;
201 #ifdef DIAGNOSTIC
202 int ipprintfs = 0;
203 #endif
204
205 int ip_do_randomid = 0;
206
207 /*
208 * XXX - Setting ip_checkinterface mostly implements the receive side of
209 * the Strong ES model described in RFC 1122, but since the routing table
210 * and transmit implementation do not implement the Strong ES model,
211 * setting this to 1 results in an odd hybrid.
212 *
213 * XXX - ip_checkinterface currently must be disabled if you use ipnat
214 * to translate the destination address to another local interface.
215 *
216 * XXX - ip_checkinterface must be disabled if you add IP aliases
217 * to the loopback interface instead of the interface where the
218 * packets for those addresses are received.
219 */
220 int ip_checkinterface = 0;
221
222
223 struct rttimer_queue *ip_mtudisc_timeout_q = NULL;
224
225 int ipqmaxlen = IFQ_MAXLEN;
226 u_long in_ifaddrhash; /* size of hash table - 1 */
227 int in_ifaddrentries; /* total number of addrs */
228 struct in_ifaddrhead in_ifaddrhead;
229 struct in_ifaddrhashhead *in_ifaddrhashtbl;
230 u_long in_multihash; /* size of hash table - 1 */
231 int in_multientries; /* total number of addrs */
232 struct in_multihashhead *in_multihashtbl;
233 struct ifqueue ipintrq;
234 uint16_t ip_id;
235
236 percpu_t *ipstat_percpu;
237
238 #ifdef PFIL_HOOKS
239 struct pfil_head inet_pfil_hook;
240 #endif
241
242 /*
243 * Cached copy of nmbclusters. If nbclusters is different,
244 * recalculate IP parameters derived from nmbclusters.
245 */
246 static int ip_nmbclusters; /* copy of nmbclusters */
247 static void ip_nmbclusters_changed(void); /* recalc limits */
248
249 #define CHECK_NMBCLUSTER_PARAMS() \
250 do { \
251 if (__predict_false(ip_nmbclusters != nmbclusters)) \
252 ip_nmbclusters_changed(); \
253 } while (/*CONSTCOND*/0)
254
255 /* IP datagram reassembly queues (hashed) */
256 #define IPREASS_NHASH_LOG2 6
257 #define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2)
258 #define IPREASS_HMASK (IPREASS_NHASH - 1)
259 #define IPREASS_HASH(x,y) \
260 (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
261 struct ipqhead ipq[IPREASS_NHASH];
262 int ipq_locked;
263 static int ip_nfragpackets; /* packets in reass queue */
264 static int ip_nfrags; /* total fragments in reass queues */
265
266 int ip_maxfragpackets = 200; /* limit on packets. XXX sysctl */
267 int ip_maxfrags; /* limit on fragments. XXX sysctl */
268
269
270 /*
271 * Additive-Increase/Multiplicative-Decrease (AIMD) strategy for
272 * IP reassembly queue buffer managment.
273 *
274 * We keep a count of total IP fragments (NB: not fragmented packets!)
275 * awaiting reassembly (ip_nfrags) and a limit (ip_maxfrags) on fragments.
276 * If ip_nfrags exceeds ip_maxfrags the limit, we drop half the
277 * total fragments in reassembly queues.This AIMD policy avoids
278 * repeatedly deleting single packets under heavy fragmentation load
279 * (e.g., from lossy NFS peers).
280 */
281 static u_int ip_reass_ttl_decr(u_int ticks);
282 static void ip_reass_drophalf(void);
283
284
285 static inline int ipq_lock_try(void);
286 static inline void ipq_unlock(void);
287
288 static inline int
289 ipq_lock_try(void)
290 {
291 int s;
292
293 /*
294 * Use splvm() -- we're blocking things that would cause
295 * mbuf allocation.
296 */
297 s = splvm();
298 if (ipq_locked) {
299 splx(s);
300 return (0);
301 }
302 ipq_locked = 1;
303 splx(s);
304 return (1);
305 }
306
307 static inline void
308 ipq_unlock(void)
309 {
310 int s;
311
312 s = splvm();
313 ipq_locked = 0;
314 splx(s);
315 }
316
317 #ifdef DIAGNOSTIC
318 #define IPQ_LOCK() \
319 do { \
320 if (ipq_lock_try() == 0) { \
321 printf("%s:%d: ipq already locked\n", __FILE__, __LINE__); \
322 panic("ipq_lock"); \
323 } \
324 } while (/*CONSTCOND*/ 0)
325 #define IPQ_LOCK_CHECK() \
326 do { \
327 if (ipq_locked == 0) { \
328 printf("%s:%d: ipq lock not held\n", __FILE__, __LINE__); \
329 panic("ipq lock check"); \
330 } \
331 } while (/*CONSTCOND*/ 0)
332 #else
333 #define IPQ_LOCK() (void) ipq_lock_try()
334 #define IPQ_LOCK_CHECK() /* nothing */
335 #endif
336
337 #define IPQ_UNLOCK() ipq_unlock()
338
339 POOL_INIT(inmulti_pool, sizeof(struct in_multi), 0, 0, 0, "inmltpl", NULL,
340 IPL_SOFTNET);
341 POOL_INIT(ipqent_pool, sizeof(struct ipqent), 0, 0, 0, "ipqepl", NULL,
342 IPL_VM);
343
344 #ifdef INET_CSUM_COUNTERS
345 #include <sys/device.h>
346
347 struct evcnt ip_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
348 NULL, "inet", "hwcsum bad");
349 struct evcnt ip_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
350 NULL, "inet", "hwcsum ok");
351 struct evcnt ip_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
352 NULL, "inet", "swcsum");
353
354 #define INET_CSUM_COUNTER_INCR(ev) (ev)->ev_count++
355
356 EVCNT_ATTACH_STATIC(ip_hwcsum_bad);
357 EVCNT_ATTACH_STATIC(ip_hwcsum_ok);
358 EVCNT_ATTACH_STATIC(ip_swcsum);
359
360 #else
361
362 #define INET_CSUM_COUNTER_INCR(ev) /* nothing */
363
364 #endif /* INET_CSUM_COUNTERS */
365
366 /*
367 * We need to save the IP options in case a protocol wants to respond
368 * to an incoming packet over the same route if the packet got here
369 * using IP source routing. This allows connection establishment and
370 * maintenance when the remote end is on a network that is not known
371 * to us.
372 */
373 int ip_nhops = 0;
374 static struct ip_srcrt {
375 struct in_addr dst; /* final destination */
376 char nop; /* one NOP to align */
377 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */
378 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
379 } ip_srcrt;
380
381 static void save_rte(u_char *, struct in_addr);
382
383 #ifdef MBUFTRACE
384 struct mowner ip_rx_mowner = MOWNER_INIT("internet", "rx");
385 struct mowner ip_tx_mowner = MOWNER_INIT("internet", "tx");
386 #endif
387
388 /*
389 * Compute IP limits derived from the value of nmbclusters.
390 */
391 static void
392 ip_nmbclusters_changed(void)
393 {
394 ip_maxfrags = nmbclusters / 4;
395 ip_nmbclusters = nmbclusters;
396 }
397
398 /*
399 * IP initialization: fill in IP protocol switch table.
400 * All protocols not implemented in kernel go to raw IP protocol handler.
401 */
402 void
403 ip_init(void)
404 {
405 const struct protosw *pr;
406 int i;
407
408 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
409 if (pr == 0)
410 panic("ip_init");
411 for (i = 0; i < IPPROTO_MAX; i++)
412 ip_protox[i] = pr - inetsw;
413 for (pr = inetdomain.dom_protosw;
414 pr < inetdomain.dom_protoswNPROTOSW; pr++)
415 if (pr->pr_domain->dom_family == PF_INET &&
416 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
417 ip_protox[pr->pr_protocol] = pr - inetsw;
418
419 for (i = 0; i < IPREASS_NHASH; i++)
420 LIST_INIT(&ipq[i]);
421
422 ip_initid();
423 ip_id = time_second & 0xfffff;
424
425 ipintrq.ifq_maxlen = ipqmaxlen;
426 ip_nmbclusters_changed();
427
428 TAILQ_INIT(&in_ifaddrhead);
429 in_ifaddrhashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, M_IFADDR,
430 M_WAITOK, &in_ifaddrhash);
431 in_multihashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, M_IPMADDR,
432 M_WAITOK, &in_multihash);
433 ip_mtudisc_timeout_q = rt_timer_queue_create(ip_mtudisc_timeout);
434 #ifdef GATEWAY
435 ipflow_init(ip_hashsize);
436 #endif
437
438 #ifdef PFIL_HOOKS
439 /* Register our Packet Filter hook. */
440 inet_pfil_hook.ph_type = PFIL_TYPE_AF;
441 inet_pfil_hook.ph_af = AF_INET;
442 i = pfil_head_register(&inet_pfil_hook);
443 if (i != 0)
444 printf("ip_init: WARNING: unable to register pfil hook, "
445 "error %d\n", i);
446 #endif /* PFIL_HOOKS */
447
448 #ifdef MBUFTRACE
449 MOWNER_ATTACH(&ip_tx_mowner);
450 MOWNER_ATTACH(&ip_rx_mowner);
451 #endif /* MBUFTRACE */
452
453 ipstat_percpu = percpu_alloc(sizeof(uint64_t) * IP_NSTATS);
454 }
455
456 struct sockaddr_in ipaddr = {
457 .sin_len = sizeof(ipaddr),
458 .sin_family = AF_INET,
459 };
460 struct route ipforward_rt;
461
462 /*
463 * IP software interrupt routine
464 */
465 void
466 ipintr(void)
467 {
468 int s;
469 struct mbuf *m;
470
471 while (!IF_IS_EMPTY(&ipintrq)) {
472 s = splnet();
473 IF_DEQUEUE(&ipintrq, m);
474 splx(s);
475 if (m == 0)
476 return;
477 ip_input(m);
478 }
479 }
480
481 /*
482 * Ip input routine. Checksum and byte swap header. If fragmented
483 * try to reassemble. Process options. Pass to next level.
484 */
485 void
486 ip_input(struct mbuf *m)
487 {
488 struct ip *ip = NULL;
489 struct ipq *fp;
490 struct in_ifaddr *ia;
491 struct ifaddr *ifa;
492 struct ipqent *ipqe;
493 int hlen = 0, mff, len;
494 int downmatch;
495 int checkif;
496 int srcrt = 0;
497 int s;
498 u_int hash;
499 #ifdef FAST_IPSEC
500 struct m_tag *mtag;
501 struct tdb_ident *tdbi;
502 struct secpolicy *sp;
503 int error;
504 #endif /* FAST_IPSEC */
505
506 MCLAIM(m, &ip_rx_mowner);
507 #ifdef DIAGNOSTIC
508 if ((m->m_flags & M_PKTHDR) == 0)
509 panic("ipintr no HDR");
510 #endif
511
512 /*
513 * If no IP addresses have been set yet but the interfaces
514 * are receiving, can't do anything with incoming packets yet.
515 */
516 if (TAILQ_FIRST(&in_ifaddrhead) == 0)
517 goto bad;
518 IP_STATINC(IP_STAT_TOTAL);
519 /*
520 * If the IP header is not aligned, slurp it up into a new
521 * mbuf with space for link headers, in the event we forward
522 * it. Otherwise, if it is aligned, make sure the entire
523 * base IP header is in the first mbuf of the chain.
524 */
525 if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
526 if ((m = m_copyup(m, sizeof(struct ip),
527 (max_linkhdr + 3) & ~3)) == NULL) {
528 /* XXXJRT new stat, please */
529 IP_STATINC(IP_STAT_TOOSMALL);
530 return;
531 }
532 } else if (__predict_false(m->m_len < sizeof (struct ip))) {
533 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
534 IP_STATINC(IP_STAT_TOOSMALL);
535 return;
536 }
537 }
538 ip = mtod(m, struct ip *);
539 if (ip->ip_v != IPVERSION) {
540 IP_STATINC(IP_STAT_BADVERS);
541 goto bad;
542 }
543 hlen = ip->ip_hl << 2;
544 if (hlen < sizeof(struct ip)) { /* minimum header length */
545 IP_STATINC(IP_STAT_BADHLEN);
546 goto bad;
547 }
548 if (hlen > m->m_len) {
549 if ((m = m_pullup(m, hlen)) == 0) {
550 IP_STATINC(IP_STAT_BADHLEN);
551 return;
552 }
553 ip = mtod(m, struct ip *);
554 }
555
556 /*
557 * RFC1122: packets with a multicast source address are
558 * not allowed.
559 */
560 if (IN_MULTICAST(ip->ip_src.s_addr)) {
561 IP_STATINC(IP_STAT_BADADDR);
562 goto bad;
563 }
564
565 /* 127/8 must not appear on wire - RFC1122 */
566 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
567 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
568 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
569 IP_STATINC(IP_STAT_BADADDR);
570 goto bad;
571 }
572 }
573
574 switch (m->m_pkthdr.csum_flags &
575 ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_IPv4) |
576 M_CSUM_IPv4_BAD)) {
577 case M_CSUM_IPv4|M_CSUM_IPv4_BAD:
578 INET_CSUM_COUNTER_INCR(&ip_hwcsum_bad);
579 goto badcsum;
580
581 case M_CSUM_IPv4:
582 /* Checksum was okay. */
583 INET_CSUM_COUNTER_INCR(&ip_hwcsum_ok);
584 break;
585
586 default:
587 /*
588 * Must compute it ourselves. Maybe skip checksum on
589 * loopback interfaces.
590 */
591 if (__predict_true(!(m->m_pkthdr.rcvif->if_flags &
592 IFF_LOOPBACK) || ip_do_loopback_cksum)) {
593 INET_CSUM_COUNTER_INCR(&ip_swcsum);
594 if (in_cksum(m, hlen) != 0)
595 goto badcsum;
596 }
597 break;
598 }
599
600 /* Retrieve the packet length. */
601 len = ntohs(ip->ip_len);
602
603 /*
604 * Check for additional length bogosity
605 */
606 if (len < hlen) {
607 IP_STATINC(IP_STAT_BADLEN);
608 goto bad;
609 }
610
611 /*
612 * Check that the amount of data in the buffers
613 * is as at least much as the IP header would have us expect.
614 * Trim mbufs if longer than we expect.
615 * Drop packet if shorter than we expect.
616 */
617 if (m->m_pkthdr.len < len) {
618 IP_STATINC(IP_STAT_TOOSHORT);
619 goto bad;
620 }
621 if (m->m_pkthdr.len > len) {
622 if (m->m_len == m->m_pkthdr.len) {
623 m->m_len = len;
624 m->m_pkthdr.len = len;
625 } else
626 m_adj(m, len - m->m_pkthdr.len);
627 }
628
629 #if defined(IPSEC)
630 /* ipflow (IP fast forwarding) is not compatible with IPsec. */
631 m->m_flags &= ~M_CANFASTFWD;
632 #else
633 /*
634 * Assume that we can create a fast-forward IP flow entry
635 * based on this packet.
636 */
637 m->m_flags |= M_CANFASTFWD;
638 #endif
639
640 #ifdef PFIL_HOOKS
641 /*
642 * Run through list of hooks for input packets. If there are any
643 * filters which require that additional packets in the flow are
644 * not fast-forwarded, they must clear the M_CANFASTFWD flag.
645 * Note that filters must _never_ set this flag, as another filter
646 * in the list may have previously cleared it.
647 */
648 /*
649 * let ipfilter look at packet on the wire,
650 * not the decapsulated packet.
651 */
652 #ifdef IPSEC
653 if (!ipsec_getnhist(m))
654 #elif defined(FAST_IPSEC)
655 if (!ipsec_indone(m))
656 #else
657 if (1)
658 #endif
659 {
660 struct in_addr odst;
661
662 odst = ip->ip_dst;
663 if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif,
664 PFIL_IN) != 0)
665 return;
666 if (m == NULL)
667 return;
668 ip = mtod(m, struct ip *);
669 hlen = ip->ip_hl << 2;
670 /*
671 * XXX The setting of "srcrt" here is to prevent ip_forward()
672 * from generating ICMP redirects for packets that have
673 * been redirected by a hook back out on to the same LAN that
674 * they came from and is not an indication that the packet
675 * is being inffluenced by source routing options. This
676 * allows things like
677 * "rdr tlp0 0/0 port 80 -> 1.1.1.200 3128 tcp"
678 * where tlp0 is both on the 1.1.1.0/24 network and is the
679 * default route for hosts on 1.1.1.0/24. Of course this
680 * also requires a "map tlp0 ..." to complete the story.
681 * One might argue whether or not this kind of network config.
682 * should be supported in this manner...
683 */
684 srcrt = (odst.s_addr != ip->ip_dst.s_addr);
685 }
686 #endif /* PFIL_HOOKS */
687
688 #ifdef ALTQ
689 /* XXX Temporary until ALTQ is changed to use a pfil hook */
690 if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) {
691 /* packet dropped by traffic conditioner */
692 return;
693 }
694 #endif
695
696 /*
697 * Process options and, if not destined for us,
698 * ship it on. ip_dooptions returns 1 when an
699 * error was detected (causing an icmp message
700 * to be sent and the original packet to be freed).
701 */
702 ip_nhops = 0; /* for source routed packets */
703 if (hlen > sizeof (struct ip) && ip_dooptions(m))
704 return;
705
706 /*
707 * Enable a consistency check between the destination address
708 * and the arrival interface for a unicast packet (the RFC 1122
709 * strong ES model) if IP forwarding is disabled and the packet
710 * is not locally generated.
711 *
712 * XXX - Checking also should be disabled if the destination
713 * address is ipnat'ed to a different interface.
714 *
715 * XXX - Checking is incompatible with IP aliases added
716 * to the loopback interface instead of the interface where
717 * the packets are received.
718 *
719 * XXX - We need to add a per ifaddr flag for this so that
720 * we get finer grain control.
721 */
722 checkif = ip_checkinterface && (ipforwarding == 0) &&
723 (m->m_pkthdr.rcvif != NULL) &&
724 ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0);
725
726 /*
727 * Check our list of addresses, to see if the packet is for us.
728 *
729 * Traditional 4.4BSD did not consult IFF_UP at all.
730 * The behavior here is to treat addresses on !IFF_UP interface
731 * as not mine.
732 */
733 downmatch = 0;
734 LIST_FOREACH(ia, &IN_IFADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
735 if (in_hosteq(ia->ia_addr.sin_addr, ip->ip_dst)) {
736 if (checkif && ia->ia_ifp != m->m_pkthdr.rcvif)
737 continue;
738 if ((ia->ia_ifp->if_flags & IFF_UP) != 0)
739 break;
740 else
741 downmatch++;
742 }
743 }
744 if (ia != NULL)
745 goto ours;
746 if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
747 IFADDR_FOREACH(ifa, m->m_pkthdr.rcvif) {
748 if (ifa->ifa_addr->sa_family != AF_INET)
749 continue;
750 ia = ifatoia(ifa);
751 if (in_hosteq(ip->ip_dst, ia->ia_broadaddr.sin_addr) ||
752 in_hosteq(ip->ip_dst, ia->ia_netbroadcast) ||
753 /*
754 * Look for all-0's host part (old broadcast addr),
755 * either for subnet or net.
756 */
757 ip->ip_dst.s_addr == ia->ia_subnet ||
758 ip->ip_dst.s_addr == ia->ia_net)
759 goto ours;
760 /*
761 * An interface with IP address zero accepts
762 * all packets that arrive on that interface.
763 */
764 if (in_nullhost(ia->ia_addr.sin_addr))
765 goto ours;
766 }
767 }
768 if (IN_MULTICAST(ip->ip_dst.s_addr)) {
769 struct in_multi *inm;
770 #ifdef MROUTING
771 extern struct socket *ip_mrouter;
772
773 if (ip_mrouter) {
774 /*
775 * If we are acting as a multicast router, all
776 * incoming multicast packets are passed to the
777 * kernel-level multicast forwarding function.
778 * The packet is returned (relatively) intact; if
779 * ip_mforward() returns a non-zero value, the packet
780 * must be discarded, else it may be accepted below.
781 *
782 * (The IP ident field is put in the same byte order
783 * as expected when ip_mforward() is called from
784 * ip_output().)
785 */
786 if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) {
787 IP_STATINC(IP_STAT_CANTFORWARD);
788 m_freem(m);
789 return;
790 }
791
792 /*
793 * The process-level routing demon needs to receive
794 * all multicast IGMP packets, whether or not this
795 * host belongs to their destination groups.
796 */
797 if (ip->ip_p == IPPROTO_IGMP)
798 goto ours;
799 IP_STATINC(IP_STAT_CANTFORWARD);
800 }
801 #endif
802 /*
803 * See if we belong to the destination multicast group on the
804 * arrival interface.
805 */
806 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
807 if (inm == NULL) {
808 IP_STATINC(IP_STAT_CANTFORWARD);
809 m_freem(m);
810 return;
811 }
812 goto ours;
813 }
814 if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
815 in_nullhost(ip->ip_dst))
816 goto ours;
817
818 /*
819 * Not for us; forward if possible and desirable.
820 */
821 if (ipforwarding == 0) {
822 IP_STATINC(IP_STAT_CANTFORWARD);
823 m_freem(m);
824 } else {
825 /*
826 * If ip_dst matched any of my address on !IFF_UP interface,
827 * and there's no IFF_UP interface that matches ip_dst,
828 * send icmp unreach. Forwarding it will result in in-kernel
829 * forwarding loop till TTL goes to 0.
830 */
831 if (downmatch) {
832 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
833 IP_STATINC(IP_STAT_CANTFORWARD);
834 return;
835 }
836 #ifdef IPSEC
837 if (ipsec4_in_reject(m, NULL)) {
838 ipsecstat.in_polvio++;
839 goto bad;
840 }
841 #endif
842 #ifdef FAST_IPSEC
843 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
844 s = splsoftnet();
845 if (mtag != NULL) {
846 tdbi = (struct tdb_ident *)(mtag + 1);
847 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
848 } else {
849 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
850 IP_FORWARDING, &error);
851 }
852 if (sp == NULL) { /* NB: can happen if error */
853 splx(s);
854 /*XXX error stat???*/
855 DPRINTF(("ip_input: no SP for forwarding\n")); /*XXX*/
856 goto bad;
857 }
858
859 /*
860 * Check security policy against packet attributes.
861 */
862 error = ipsec_in_reject(sp, m);
863 KEY_FREESP(&sp);
864 splx(s);
865 if (error) {
866 IP_STATINC(IP_STAT_CANTFORWARD);
867 goto bad;
868 }
869
870 /*
871 * Peek at the outbound SP for this packet to determine if
872 * it's a Fast Forward candidate.
873 */
874 mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
875 if (mtag != NULL)
876 m->m_flags &= ~M_CANFASTFWD;
877 else {
878 s = splsoftnet();
879 sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND,
880 (IP_FORWARDING |
881 (ip_directedbcast ? IP_ALLOWBROADCAST : 0)),
882 &error, NULL);
883 if (sp != NULL) {
884 m->m_flags &= ~M_CANFASTFWD;
885 KEY_FREESP(&sp);
886 }
887 splx(s);
888 }
889 #endif /* FAST_IPSEC */
890
891 ip_forward(m, srcrt);
892 }
893 return;
894
895 ours:
896 /*
897 * If offset or IP_MF are set, must reassemble.
898 * Otherwise, nothing need be done.
899 * (We could look in the reassembly queue to see
900 * if the packet was previously fragmented,
901 * but it's not worth the time; just let them time out.)
902 */
903 if (ip->ip_off & ~htons(IP_DF|IP_RF)) {
904 uint16_t off;
905 /*
906 * Prevent TCP blind data attacks by not allowing non-initial
907 * fragments to start at less than 68 bytes (minimal fragment
908 * size) and making sure the first fragment is at least 68
909 * bytes.
910 */
911 off = (ntohs(ip->ip_off) & IP_OFFMASK) << 3;
912 if ((off > 0 ? off + hlen : len) < IP_MINFRAGSIZE - 1) {
913 IP_STATINC(IP_STAT_BADFRAGS);
914 goto bad;
915 }
916 /*
917 * Look for queue of fragments
918 * of this datagram.
919 */
920 IPQ_LOCK();
921 hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
922 LIST_FOREACH(fp, &ipq[hash], ipq_q) {
923 if (ip->ip_id == fp->ipq_id &&
924 in_hosteq(ip->ip_src, fp->ipq_src) &&
925 in_hosteq(ip->ip_dst, fp->ipq_dst) &&
926 ip->ip_p == fp->ipq_p) {
927 /*
928 * Make sure the TOS is matches previous
929 * fragments.
930 */
931 if (ip->ip_tos != fp->ipq_tos) {
932 IP_STATINC(IP_STAT_BADFRAGS);
933 goto bad;
934 }
935 goto found;
936 }
937 }
938 fp = 0;
939 found:
940
941 /*
942 * Adjust ip_len to not reflect header,
943 * set ipqe_mff if more fragments are expected,
944 * convert offset of this to bytes.
945 */
946 ip->ip_len = htons(ntohs(ip->ip_len) - hlen);
947 mff = (ip->ip_off & htons(IP_MF)) != 0;
948 if (mff) {
949 /*
950 * Make sure that fragments have a data length
951 * that's a non-zero multiple of 8 bytes.
952 */
953 if (ntohs(ip->ip_len) == 0 ||
954 (ntohs(ip->ip_len) & 0x7) != 0) {
955 IP_STATINC(IP_STAT_BADFRAGS);
956 IPQ_UNLOCK();
957 goto bad;
958 }
959 }
960 ip->ip_off = htons((ntohs(ip->ip_off) & IP_OFFMASK) << 3);
961
962 /*
963 * If datagram marked as having more fragments
964 * or if this is not the first fragment,
965 * attempt reassembly; if it succeeds, proceed.
966 */
967 if (mff || ip->ip_off != htons(0)) {
968 IP_STATINC(IP_STAT_FRAGMENTS);
969 s = splvm();
970 ipqe = pool_get(&ipqent_pool, PR_NOWAIT);
971 splx(s);
972 if (ipqe == NULL) {
973 IP_STATINC(IP_STAT_RCVMEMDROP);
974 IPQ_UNLOCK();
975 goto bad;
976 }
977 ipqe->ipqe_mff = mff;
978 ipqe->ipqe_m = m;
979 ipqe->ipqe_ip = ip;
980 m = ip_reass(ipqe, fp, &ipq[hash]);
981 if (m == 0) {
982 IPQ_UNLOCK();
983 return;
984 }
985 IP_STATINC(IP_STAT_REASSEMBLED);
986 ip = mtod(m, struct ip *);
987 hlen = ip->ip_hl << 2;
988 ip->ip_len = htons(ntohs(ip->ip_len) + hlen);
989 } else
990 if (fp)
991 ip_freef(fp);
992 IPQ_UNLOCK();
993 }
994
995 #if defined(IPSEC)
996 /*
997 * enforce IPsec policy checking if we are seeing last header.
998 * note that we do not visit this with protocols with pcb layer
999 * code - like udp/tcp/raw ip.
1000 */
1001 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0 &&
1002 ipsec4_in_reject(m, NULL)) {
1003 ipsecstat.in_polvio++;
1004 goto bad;
1005 }
1006 #endif
1007 #ifdef FAST_IPSEC
1008 /*
1009 * enforce IPsec policy checking if we are seeing last header.
1010 * note that we do not visit this with protocols with pcb layer
1011 * code - like udp/tcp/raw ip.
1012 */
1013 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0) {
1014 /*
1015 * Check if the packet has already had IPsec processing
1016 * done. If so, then just pass it along. This tag gets
1017 * set during AH, ESP, etc. input handling, before the
1018 * packet is returned to the ip input queue for delivery.
1019 */
1020 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
1021 s = splsoftnet();
1022 if (mtag != NULL) {
1023 tdbi = (struct tdb_ident *)(mtag + 1);
1024 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
1025 } else {
1026 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
1027 IP_FORWARDING, &error);
1028 }
1029 if (sp != NULL) {
1030 /*
1031 * Check security policy against packet attributes.
1032 */
1033 error = ipsec_in_reject(sp, m);
1034 KEY_FREESP(&sp);
1035 } else {
1036 /* XXX error stat??? */
1037 error = EINVAL;
1038 DPRINTF(("ip_input: no SP, packet discarded\n"));/*XXX*/
1039 }
1040 splx(s);
1041 if (error)
1042 goto bad;
1043 }
1044 #endif /* FAST_IPSEC */
1045
1046 /*
1047 * Switch out to protocol's input routine.
1048 */
1049 #if IFA_STATS
1050 if (ia && ip)
1051 ia->ia_ifa.ifa_data.ifad_inbytes += ntohs(ip->ip_len);
1052 #endif
1053 IP_STATINC(IP_STAT_DELIVERED);
1054 {
1055 int off = hlen, nh = ip->ip_p;
1056
1057 (*inetsw[ip_protox[nh]].pr_input)(m, off, nh);
1058 return;
1059 }
1060 bad:
1061 m_freem(m);
1062 return;
1063
1064 badcsum:
1065 IP_STATINC(IP_STAT_BADSUM);
1066 m_freem(m);
1067 }
1068
1069 /*
1070 * Take incoming datagram fragment and try to
1071 * reassemble it into whole datagram. If a chain for
1072 * reassembly of this datagram already exists, then it
1073 * is given as fp; otherwise have to make a chain.
1074 */
1075 struct mbuf *
1076 ip_reass(struct ipqent *ipqe, struct ipq *fp, struct ipqhead *ipqhead)
1077 {
1078 struct mbuf *m = ipqe->ipqe_m;
1079 struct ipqent *nq, *p, *q;
1080 struct ip *ip;
1081 struct mbuf *t;
1082 int hlen = ipqe->ipqe_ip->ip_hl << 2;
1083 int i, next, s;
1084
1085 IPQ_LOCK_CHECK();
1086
1087 /*
1088 * Presence of header sizes in mbufs
1089 * would confuse code below.
1090 */
1091 m->m_data += hlen;
1092 m->m_len -= hlen;
1093
1094 #ifdef notyet
1095 /* make sure fragment limit is up-to-date */
1096 CHECK_NMBCLUSTER_PARAMS();
1097
1098 /* If we have too many fragments, drop the older half. */
1099 if (ip_nfrags >= ip_maxfrags)
1100 ip_reass_drophalf(void);
1101 #endif
1102
1103 /*
1104 * We are about to add a fragment; increment frag count.
1105 */
1106 ip_nfrags++;
1107
1108 /*
1109 * If first fragment to arrive, create a reassembly queue.
1110 */
1111 if (fp == 0) {
1112 /*
1113 * Enforce upper bound on number of fragmented packets
1114 * for which we attempt reassembly;
1115 * If maxfrag is 0, never accept fragments.
1116 * If maxfrag is -1, accept all fragments without limitation.
1117 */
1118 if (ip_maxfragpackets < 0)
1119 ;
1120 else if (ip_nfragpackets >= ip_maxfragpackets)
1121 goto dropfrag;
1122 ip_nfragpackets++;
1123 MALLOC(fp, struct ipq *, sizeof (struct ipq),
1124 M_FTABLE, M_NOWAIT);
1125 if (fp == NULL)
1126 goto dropfrag;
1127 LIST_INSERT_HEAD(ipqhead, fp, ipq_q);
1128 fp->ipq_nfrags = 1;
1129 fp->ipq_ttl = IPFRAGTTL;
1130 fp->ipq_p = ipqe->ipqe_ip->ip_p;
1131 fp->ipq_id = ipqe->ipqe_ip->ip_id;
1132 fp->ipq_tos = ipqe->ipqe_ip->ip_tos;
1133 TAILQ_INIT(&fp->ipq_fragq);
1134 fp->ipq_src = ipqe->ipqe_ip->ip_src;
1135 fp->ipq_dst = ipqe->ipqe_ip->ip_dst;
1136 p = NULL;
1137 goto insert;
1138 } else {
1139 fp->ipq_nfrags++;
1140 }
1141
1142 /*
1143 * Find a segment which begins after this one does.
1144 */
1145 for (p = NULL, q = TAILQ_FIRST(&fp->ipq_fragq); q != NULL;
1146 p = q, q = TAILQ_NEXT(q, ipqe_q))
1147 if (ntohs(q->ipqe_ip->ip_off) > ntohs(ipqe->ipqe_ip->ip_off))
1148 break;
1149
1150 /*
1151 * If there is a preceding segment, it may provide some of
1152 * our data already. If so, drop the data from the incoming
1153 * segment. If it provides all of our data, drop us.
1154 */
1155 if (p != NULL) {
1156 i = ntohs(p->ipqe_ip->ip_off) + ntohs(p->ipqe_ip->ip_len) -
1157 ntohs(ipqe->ipqe_ip->ip_off);
1158 if (i > 0) {
1159 if (i >= ntohs(ipqe->ipqe_ip->ip_len))
1160 goto dropfrag;
1161 m_adj(ipqe->ipqe_m, i);
1162 ipqe->ipqe_ip->ip_off =
1163 htons(ntohs(ipqe->ipqe_ip->ip_off) + i);
1164 ipqe->ipqe_ip->ip_len =
1165 htons(ntohs(ipqe->ipqe_ip->ip_len) - i);
1166 }
1167 }
1168
1169 /*
1170 * While we overlap succeeding segments trim them or,
1171 * if they are completely covered, dequeue them.
1172 */
1173 for (; q != NULL &&
1174 ntohs(ipqe->ipqe_ip->ip_off) + ntohs(ipqe->ipqe_ip->ip_len) >
1175 ntohs(q->ipqe_ip->ip_off); q = nq) {
1176 i = (ntohs(ipqe->ipqe_ip->ip_off) +
1177 ntohs(ipqe->ipqe_ip->ip_len)) - ntohs(q->ipqe_ip->ip_off);
1178 if (i < ntohs(q->ipqe_ip->ip_len)) {
1179 q->ipqe_ip->ip_len =
1180 htons(ntohs(q->ipqe_ip->ip_len) - i);
1181 q->ipqe_ip->ip_off =
1182 htons(ntohs(q->ipqe_ip->ip_off) + i);
1183 m_adj(q->ipqe_m, i);
1184 break;
1185 }
1186 nq = TAILQ_NEXT(q, ipqe_q);
1187 m_freem(q->ipqe_m);
1188 TAILQ_REMOVE(&fp->ipq_fragq, q, ipqe_q);
1189 s = splvm();
1190 pool_put(&ipqent_pool, q);
1191 splx(s);
1192 fp->ipq_nfrags--;
1193 ip_nfrags--;
1194 }
1195
1196 insert:
1197 /*
1198 * Stick new segment in its place;
1199 * check for complete reassembly.
1200 */
1201 if (p == NULL) {
1202 TAILQ_INSERT_HEAD(&fp->ipq_fragq, ipqe, ipqe_q);
1203 } else {
1204 TAILQ_INSERT_AFTER(&fp->ipq_fragq, p, ipqe, ipqe_q);
1205 }
1206 next = 0;
1207 for (p = NULL, q = TAILQ_FIRST(&fp->ipq_fragq); q != NULL;
1208 p = q, q = TAILQ_NEXT(q, ipqe_q)) {
1209 if (ntohs(q->ipqe_ip->ip_off) != next)
1210 return (0);
1211 next += ntohs(q->ipqe_ip->ip_len);
1212 }
1213 if (p->ipqe_mff)
1214 return (0);
1215
1216 /*
1217 * Reassembly is complete. Check for a bogus message size and
1218 * concatenate fragments.
1219 */
1220 q = TAILQ_FIRST(&fp->ipq_fragq);
1221 ip = q->ipqe_ip;
1222 if ((next + (ip->ip_hl << 2)) > IP_MAXPACKET) {
1223 IP_STATINC(IP_STAT_TOOLONG);
1224 ip_freef(fp);
1225 return (0);
1226 }
1227 m = q->ipqe_m;
1228 t = m->m_next;
1229 m->m_next = 0;
1230 m_cat(m, t);
1231 nq = TAILQ_NEXT(q, ipqe_q);
1232 s = splvm();
1233 pool_put(&ipqent_pool, q);
1234 splx(s);
1235 for (q = nq; q != NULL; q = nq) {
1236 t = q->ipqe_m;
1237 nq = TAILQ_NEXT(q, ipqe_q);
1238 s = splvm();
1239 pool_put(&ipqent_pool, q);
1240 splx(s);
1241 m_cat(m, t);
1242 }
1243 ip_nfrags -= fp->ipq_nfrags;
1244
1245 /*
1246 * Create header for new ip packet by
1247 * modifying header of first packet;
1248 * dequeue and discard fragment reassembly header.
1249 * Make header visible.
1250 */
1251 ip->ip_len = htons(next);
1252 ip->ip_src = fp->ipq_src;
1253 ip->ip_dst = fp->ipq_dst;
1254 LIST_REMOVE(fp, ipq_q);
1255 FREE(fp, M_FTABLE);
1256 ip_nfragpackets--;
1257 m->m_len += (ip->ip_hl << 2);
1258 m->m_data -= (ip->ip_hl << 2);
1259 /* some debugging cruft by sklower, below, will go away soon */
1260 if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
1261 int plen = 0;
1262 for (t = m; t; t = t->m_next)
1263 plen += t->m_len;
1264 m->m_pkthdr.len = plen;
1265 m->m_pkthdr.csum_flags = 0;
1266 }
1267 return (m);
1268
1269 dropfrag:
1270 if (fp != 0)
1271 fp->ipq_nfrags--;
1272 ip_nfrags--;
1273 IP_STATINC(IP_STAT_FRAGDROPPED);
1274 m_freem(m);
1275 s = splvm();
1276 pool_put(&ipqent_pool, ipqe);
1277 splx(s);
1278 return (0);
1279 }
1280
1281 /*
1282 * Free a fragment reassembly header and all
1283 * associated datagrams.
1284 */
1285 void
1286 ip_freef(struct ipq *fp)
1287 {
1288 struct ipqent *q, *p;
1289 u_int nfrags = 0;
1290 int s;
1291
1292 IPQ_LOCK_CHECK();
1293
1294 for (q = TAILQ_FIRST(&fp->ipq_fragq); q != NULL; q = p) {
1295 p = TAILQ_NEXT(q, ipqe_q);
1296 m_freem(q->ipqe_m);
1297 nfrags++;
1298 TAILQ_REMOVE(&fp->ipq_fragq, q, ipqe_q);
1299 s = splvm();
1300 pool_put(&ipqent_pool, q);
1301 splx(s);
1302 }
1303
1304 if (nfrags != fp->ipq_nfrags)
1305 printf("ip_freef: nfrags %d != %d\n", fp->ipq_nfrags, nfrags);
1306 ip_nfrags -= nfrags;
1307 LIST_REMOVE(fp, ipq_q);
1308 FREE(fp, M_FTABLE);
1309 ip_nfragpackets--;
1310 }
1311
1312 /*
1313 * IP reassembly TTL machinery for multiplicative drop.
1314 */
1315 static u_int fragttl_histo[(IPFRAGTTL+1)];
1316
1317
1318 /*
1319 * Decrement TTL of all reasembly queue entries by `ticks'.
1320 * Count number of distinct fragments (as opposed to partial, fragmented
1321 * datagrams) in the reassembly queue. While we traverse the entire
1322 * reassembly queue, compute and return the median TTL over all fragments.
1323 */
1324 static u_int
1325 ip_reass_ttl_decr(u_int ticks)
1326 {
1327 u_int nfrags, median, dropfraction, keepfraction;
1328 struct ipq *fp, *nfp;
1329 int i;
1330
1331 nfrags = 0;
1332 memset(fragttl_histo, 0, sizeof fragttl_histo);
1333
1334 for (i = 0; i < IPREASS_NHASH; i++) {
1335 for (fp = LIST_FIRST(&ipq[i]); fp != NULL; fp = nfp) {
1336 fp->ipq_ttl = ((fp->ipq_ttl <= ticks) ?
1337 0 : fp->ipq_ttl - ticks);
1338 nfp = LIST_NEXT(fp, ipq_q);
1339 if (fp->ipq_ttl == 0) {
1340 IP_STATINC(IP_STAT_FRAGTIMEOUT);
1341 ip_freef(fp);
1342 } else {
1343 nfrags += fp->ipq_nfrags;
1344 fragttl_histo[fp->ipq_ttl] += fp->ipq_nfrags;
1345 }
1346 }
1347 }
1348
1349 KASSERT(ip_nfrags == nfrags);
1350
1351 /* Find median (or other drop fraction) in histogram. */
1352 dropfraction = (ip_nfrags / 2);
1353 keepfraction = ip_nfrags - dropfraction;
1354 for (i = IPFRAGTTL, median = 0; i >= 0; i--) {
1355 median += fragttl_histo[i];
1356 if (median >= keepfraction)
1357 break;
1358 }
1359
1360 /* Return TTL of median (or other fraction). */
1361 return (u_int)i;
1362 }
1363
1364 void
1365 ip_reass_drophalf(void)
1366 {
1367
1368 u_int median_ticks;
1369 /*
1370 * Compute median TTL of all fragments, and count frags
1371 * with that TTL or lower (roughly half of all fragments).
1372 */
1373 median_ticks = ip_reass_ttl_decr(0);
1374
1375 /* Drop half. */
1376 median_ticks = ip_reass_ttl_decr(median_ticks);
1377
1378 }
1379
1380 /*
1381 * IP timer processing;
1382 * if a timer expires on a reassembly
1383 * queue, discard it.
1384 */
1385 void
1386 ip_slowtimo(void)
1387 {
1388 static u_int dropscanidx = 0;
1389 u_int i;
1390 u_int median_ttl;
1391 int s = splsoftnet();
1392
1393 IPQ_LOCK();
1394
1395 /* Age TTL of all fragments by 1 tick .*/
1396 median_ttl = ip_reass_ttl_decr(1);
1397
1398 /* make sure fragment limit is up-to-date */
1399 CHECK_NMBCLUSTER_PARAMS();
1400
1401 /* If we have too many fragments, drop the older half. */
1402 if (ip_nfrags > ip_maxfrags)
1403 ip_reass_ttl_decr(median_ttl);
1404
1405 /*
1406 * If we are over the maximum number of fragmented packets
1407 * (due to the limit being lowered), drain off
1408 * enough to get down to the new limit. Start draining
1409 * from the reassembly hashqueue most recently drained.
1410 */
1411 if (ip_maxfragpackets < 0)
1412 ;
1413 else {
1414 int wrapped = 0;
1415
1416 i = dropscanidx;
1417 while (ip_nfragpackets > ip_maxfragpackets && wrapped == 0) {
1418 while (LIST_FIRST(&ipq[i]) != NULL)
1419 ip_freef(LIST_FIRST(&ipq[i]));
1420 if (++i >= IPREASS_NHASH) {
1421 i = 0;
1422 }
1423 /*
1424 * Dont scan forever even if fragment counters are
1425 * wrong: stop after scanning entire reassembly queue.
1426 */
1427 if (i == dropscanidx)
1428 wrapped = 1;
1429 }
1430 dropscanidx = i;
1431 }
1432 IPQ_UNLOCK();
1433 splx(s);
1434 }
1435
1436 /*
1437 * Drain off all datagram fragments.
1438 */
1439 void
1440 ip_drain(void)
1441 {
1442
1443 /*
1444 * We may be called from a device's interrupt context. If
1445 * the ipq is already busy, just bail out now.
1446 */
1447 if (ipq_lock_try() == 0)
1448 return;
1449
1450 /*
1451 * Drop half the total fragments now. If more mbufs are needed,
1452 * we will be called again soon.
1453 */
1454 ip_reass_drophalf();
1455
1456 IPQ_UNLOCK();
1457 }
1458
1459 /*
1460 * Do option processing on a datagram,
1461 * possibly discarding it if bad options are encountered,
1462 * or forwarding it if source-routed.
1463 * Returns 1 if packet has been forwarded/freed,
1464 * 0 if the packet should be processed further.
1465 */
1466 int
1467 ip_dooptions(struct mbuf *m)
1468 {
1469 struct ip *ip = mtod(m, struct ip *);
1470 u_char *cp, *cp0;
1471 struct ip_timestamp *ipt;
1472 struct in_ifaddr *ia;
1473 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
1474 struct in_addr dst;
1475 n_time ntime;
1476
1477 dst = ip->ip_dst;
1478 cp = (u_char *)(ip + 1);
1479 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
1480 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1481 opt = cp[IPOPT_OPTVAL];
1482 if (opt == IPOPT_EOL)
1483 break;
1484 if (opt == IPOPT_NOP)
1485 optlen = 1;
1486 else {
1487 if (cnt < IPOPT_OLEN + sizeof(*cp)) {
1488 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1489 goto bad;
1490 }
1491 optlen = cp[IPOPT_OLEN];
1492 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
1493 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1494 goto bad;
1495 }
1496 }
1497 switch (opt) {
1498
1499 default:
1500 break;
1501
1502 /*
1503 * Source routing with record.
1504 * Find interface with current destination address.
1505 * If none on this machine then drop if strictly routed,
1506 * or do nothing if loosely routed.
1507 * Record interface address and bring up next address
1508 * component. If strictly routed make sure next
1509 * address is on directly accessible net.
1510 */
1511 case IPOPT_LSRR:
1512 case IPOPT_SSRR:
1513 if (ip_allowsrcrt == 0) {
1514 type = ICMP_UNREACH;
1515 code = ICMP_UNREACH_NET_PROHIB;
1516 goto bad;
1517 }
1518 if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1519 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1520 goto bad;
1521 }
1522 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1523 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1524 goto bad;
1525 }
1526 ipaddr.sin_addr = ip->ip_dst;
1527 ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)));
1528 if (ia == 0) {
1529 if (opt == IPOPT_SSRR) {
1530 type = ICMP_UNREACH;
1531 code = ICMP_UNREACH_SRCFAIL;
1532 goto bad;
1533 }
1534 /*
1535 * Loose routing, and not at next destination
1536 * yet; nothing to do except forward.
1537 */
1538 break;
1539 }
1540 off--; /* 0 origin */
1541 if ((off + sizeof(struct in_addr)) > optlen) {
1542 /*
1543 * End of source route. Should be for us.
1544 */
1545 save_rte(cp, ip->ip_src);
1546 break;
1547 }
1548 /*
1549 * locate outgoing interface
1550 */
1551 bcopy((void *)(cp + off), (void *)&ipaddr.sin_addr,
1552 sizeof(ipaddr.sin_addr));
1553 if (opt == IPOPT_SSRR)
1554 ia = ifatoia(ifa_ifwithladdr(sintosa(&ipaddr)));
1555 else
1556 ia = ip_rtaddr(ipaddr.sin_addr);
1557 if (ia == 0) {
1558 type = ICMP_UNREACH;
1559 code = ICMP_UNREACH_SRCFAIL;
1560 goto bad;
1561 }
1562 ip->ip_dst = ipaddr.sin_addr;
1563 bcopy((void *)&ia->ia_addr.sin_addr,
1564 (void *)(cp + off), sizeof(struct in_addr));
1565 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1566 /*
1567 * Let ip_intr's mcast routing check handle mcast pkts
1568 */
1569 forward = !IN_MULTICAST(ip->ip_dst.s_addr);
1570 break;
1571
1572 case IPOPT_RR:
1573 if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1574 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1575 goto bad;
1576 }
1577 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1578 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1579 goto bad;
1580 }
1581 /*
1582 * If no space remains, ignore.
1583 */
1584 off--; /* 0 origin */
1585 if ((off + sizeof(struct in_addr)) > optlen)
1586 break;
1587 bcopy((void *)(&ip->ip_dst), (void *)&ipaddr.sin_addr,
1588 sizeof(ipaddr.sin_addr));
1589 /*
1590 * locate outgoing interface; if we're the destination,
1591 * use the incoming interface (should be same).
1592 */
1593 if ((ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr))))
1594 == NULL &&
1595 (ia = ip_rtaddr(ipaddr.sin_addr)) == NULL) {
1596 type = ICMP_UNREACH;
1597 code = ICMP_UNREACH_HOST;
1598 goto bad;
1599 }
1600 bcopy((void *)&ia->ia_addr.sin_addr,
1601 (void *)(cp + off), sizeof(struct in_addr));
1602 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1603 break;
1604
1605 case IPOPT_TS:
1606 code = cp - (u_char *)ip;
1607 ipt = (struct ip_timestamp *)cp;
1608 if (ipt->ipt_len < 4 || ipt->ipt_len > 40) {
1609 code = (u_char *)&ipt->ipt_len - (u_char *)ip;
1610 goto bad;
1611 }
1612 if (ipt->ipt_ptr < 5) {
1613 code = (u_char *)&ipt->ipt_ptr - (u_char *)ip;
1614 goto bad;
1615 }
1616 if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
1617 if (++ipt->ipt_oflw == 0) {
1618 code = (u_char *)&ipt->ipt_ptr -
1619 (u_char *)ip;
1620 goto bad;
1621 }
1622 break;
1623 }
1624 cp0 = (cp + ipt->ipt_ptr - 1);
1625 switch (ipt->ipt_flg) {
1626
1627 case IPOPT_TS_TSONLY:
1628 break;
1629
1630 case IPOPT_TS_TSANDADDR:
1631 if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1632 sizeof(struct in_addr) > ipt->ipt_len) {
1633 code = (u_char *)&ipt->ipt_ptr -
1634 (u_char *)ip;
1635 goto bad;
1636 }
1637 ipaddr.sin_addr = dst;
1638 ia = ifatoia(ifaof_ifpforaddr(sintosa(&ipaddr),
1639 m->m_pkthdr.rcvif));
1640 if (ia == 0)
1641 continue;
1642 bcopy(&ia->ia_addr.sin_addr,
1643 cp0, sizeof(struct in_addr));
1644 ipt->ipt_ptr += sizeof(struct in_addr);
1645 break;
1646
1647 case IPOPT_TS_PRESPEC:
1648 if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1649 sizeof(struct in_addr) > ipt->ipt_len) {
1650 code = (u_char *)&ipt->ipt_ptr -
1651 (u_char *)ip;
1652 goto bad;
1653 }
1654 bcopy(cp0, &ipaddr.sin_addr,
1655 sizeof(struct in_addr));
1656 if (ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)))
1657 == NULL)
1658 continue;
1659 ipt->ipt_ptr += sizeof(struct in_addr);
1660 break;
1661
1662 default:
1663 /* XXX can't take &ipt->ipt_flg */
1664 code = (u_char *)&ipt->ipt_ptr -
1665 (u_char *)ip + 1;
1666 goto bad;
1667 }
1668 ntime = iptime();
1669 cp0 = (u_char *) &ntime; /* XXX grumble, GCC... */
1670 memmove((char *)cp + ipt->ipt_ptr - 1, cp0,
1671 sizeof(n_time));
1672 ipt->ipt_ptr += sizeof(n_time);
1673 }
1674 }
1675 if (forward) {
1676 if (ip_forwsrcrt == 0) {
1677 type = ICMP_UNREACH;
1678 code = ICMP_UNREACH_SRCFAIL;
1679 goto bad;
1680 }
1681 ip_forward(m, 1);
1682 return (1);
1683 }
1684 return (0);
1685 bad:
1686 icmp_error(m, type, code, 0, 0);
1687 IP_STATINC(IP_STAT_BADOPTIONS);
1688 return (1);
1689 }
1690
1691 /*
1692 * Given address of next destination (final or next hop),
1693 * return internet address info of interface to be used to get there.
1694 */
1695 struct in_ifaddr *
1696 ip_rtaddr(struct in_addr dst)
1697 {
1698 struct rtentry *rt;
1699 union {
1700 struct sockaddr dst;
1701 struct sockaddr_in dst4;
1702 } u;
1703
1704 sockaddr_in_init(&u.dst4, &dst, 0);
1705
1706 if ((rt = rtcache_lookup(&ipforward_rt, &u.dst)) == NULL)
1707 return NULL;
1708
1709 return ifatoia(rt->rt_ifa);
1710 }
1711
1712 /*
1713 * Save incoming source route for use in replies,
1714 * to be picked up later by ip_srcroute if the receiver is interested.
1715 */
1716 void
1717 save_rte(u_char *option, struct in_addr dst)
1718 {
1719 unsigned olen;
1720
1721 olen = option[IPOPT_OLEN];
1722 #ifdef DIAGNOSTIC
1723 if (ipprintfs)
1724 printf("save_rte: olen %d\n", olen);
1725 #endif /* 0 */
1726 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
1727 return;
1728 bcopy((void *)option, (void *)ip_srcrt.srcopt, olen);
1729 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1730 ip_srcrt.dst = dst;
1731 }
1732
1733 /*
1734 * Retrieve incoming source route for use in replies,
1735 * in the same form used by setsockopt.
1736 * The first hop is placed before the options, will be removed later.
1737 */
1738 struct mbuf *
1739 ip_srcroute(void)
1740 {
1741 struct in_addr *p, *q;
1742 struct mbuf *m;
1743
1744 if (ip_nhops == 0)
1745 return NULL;
1746 m = m_get(M_DONTWAIT, MT_SOOPTS);
1747 if (m == 0)
1748 return NULL;
1749
1750 MCLAIM(m, &inetdomain.dom_mowner);
1751 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1752
1753 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1754 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
1755 OPTSIZ;
1756 #ifdef DIAGNOSTIC
1757 if (ipprintfs)
1758 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
1759 #endif
1760
1761 /*
1762 * First save first hop for return route
1763 */
1764 p = &ip_srcrt.route[ip_nhops - 1];
1765 *(mtod(m, struct in_addr *)) = *p--;
1766 #ifdef DIAGNOSTIC
1767 if (ipprintfs)
1768 printf(" hops %x", ntohl(mtod(m, struct in_addr *)->s_addr));
1769 #endif
1770
1771 /*
1772 * Copy option fields and padding (nop) to mbuf.
1773 */
1774 ip_srcrt.nop = IPOPT_NOP;
1775 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
1776 memmove(mtod(m, char *) + sizeof(struct in_addr), &ip_srcrt.nop,
1777 OPTSIZ);
1778 q = (struct in_addr *)(mtod(m, char *) +
1779 sizeof(struct in_addr) + OPTSIZ);
1780 #undef OPTSIZ
1781 /*
1782 * Record return path as an IP source route,
1783 * reversing the path (pointers are now aligned).
1784 */
1785 while (p >= ip_srcrt.route) {
1786 #ifdef DIAGNOSTIC
1787 if (ipprintfs)
1788 printf(" %x", ntohl(q->s_addr));
1789 #endif
1790 *q++ = *p--;
1791 }
1792 /*
1793 * Last hop goes to final destination.
1794 */
1795 *q = ip_srcrt.dst;
1796 #ifdef DIAGNOSTIC
1797 if (ipprintfs)
1798 printf(" %x\n", ntohl(q->s_addr));
1799 #endif
1800 return (m);
1801 }
1802
1803 const int inetctlerrmap[PRC_NCMDS] = {
1804 [PRC_MSGSIZE] = EMSGSIZE,
1805 [PRC_HOSTDEAD] = EHOSTDOWN,
1806 [PRC_HOSTUNREACH] = EHOSTUNREACH,
1807 [PRC_UNREACH_NET] = EHOSTUNREACH,
1808 [PRC_UNREACH_HOST] = EHOSTUNREACH,
1809 [PRC_UNREACH_PROTOCOL] = ECONNREFUSED,
1810 [PRC_UNREACH_PORT] = ECONNREFUSED,
1811 [PRC_UNREACH_SRCFAIL] = EHOSTUNREACH,
1812 [PRC_PARAMPROB] = ENOPROTOOPT,
1813 };
1814
1815 /*
1816 * Forward a packet. If some error occurs return the sender
1817 * an icmp packet. Note we can't always generate a meaningful
1818 * icmp message because icmp doesn't have a large enough repertoire
1819 * of codes and types.
1820 *
1821 * If not forwarding, just drop the packet. This could be confusing
1822 * if ipforwarding was zero but some routing protocol was advancing
1823 * us as a gateway to somewhere. However, we must let the routing
1824 * protocol deal with that.
1825 *
1826 * The srcrt parameter indicates whether the packet is being forwarded
1827 * via a source route.
1828 */
1829 void
1830 ip_forward(struct mbuf *m, int srcrt)
1831 {
1832 struct ip *ip = mtod(m, struct ip *);
1833 struct rtentry *rt;
1834 int error, type = 0, code = 0, destmtu = 0;
1835 struct mbuf *mcopy;
1836 n_long dest;
1837 union {
1838 struct sockaddr dst;
1839 struct sockaddr_in dst4;
1840 } u;
1841
1842 /*
1843 * We are now in the output path.
1844 */
1845 MCLAIM(m, &ip_tx_mowner);
1846
1847 /*
1848 * Clear any in-bound checksum flags for this packet.
1849 */
1850 m->m_pkthdr.csum_flags = 0;
1851
1852 dest = 0;
1853 #ifdef DIAGNOSTIC
1854 if (ipprintfs) {
1855 printf("forward: src %s ", inet_ntoa(ip->ip_src));
1856 printf("dst %s ttl %x\n", inet_ntoa(ip->ip_dst), ip->ip_ttl);
1857 }
1858 #endif
1859 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1860 IP_STATINC(IP_STAT_CANTFORWARD);
1861 m_freem(m);
1862 return;
1863 }
1864 if (ip->ip_ttl <= IPTTLDEC) {
1865 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0);
1866 return;
1867 }
1868
1869 sockaddr_in_init(&u.dst4, &ip->ip_dst, 0);
1870 if ((rt = rtcache_lookup(&ipforward_rt, &u.dst)) == NULL) {
1871 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, dest, 0);
1872 return;
1873 }
1874
1875 /*
1876 * Save at most 68 bytes of the packet in case
1877 * we need to generate an ICMP message to the src.
1878 * Pullup to avoid sharing mbuf cluster between m and mcopy.
1879 */
1880 mcopy = m_copym(m, 0, imin(ntohs(ip->ip_len), 68), M_DONTWAIT);
1881 if (mcopy)
1882 mcopy = m_pullup(mcopy, ip->ip_hl << 2);
1883
1884 ip->ip_ttl -= IPTTLDEC;
1885
1886 /*
1887 * If forwarding packet using same interface that it came in on,
1888 * perhaps should send a redirect to sender to shortcut a hop.
1889 * Only send redirect if source is sending directly to us,
1890 * and if packet was not source routed (or has any options).
1891 * Also, don't send redirect if forwarding using a default route
1892 * or a route modified by a redirect.
1893 */
1894 if (rt->rt_ifp == m->m_pkthdr.rcvif &&
1895 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1896 !in_nullhost(satocsin(rt_getkey(rt))->sin_addr) &&
1897 ipsendredirects && !srcrt) {
1898 if (rt->rt_ifa &&
1899 (ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_subnetmask) ==
1900 ifatoia(rt->rt_ifa)->ia_subnet) {
1901 if (rt->rt_flags & RTF_GATEWAY)
1902 dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1903 else
1904 dest = ip->ip_dst.s_addr;
1905 /*
1906 * Router requirements says to only send host
1907 * redirects.
1908 */
1909 type = ICMP_REDIRECT;
1910 code = ICMP_REDIRECT_HOST;
1911 #ifdef DIAGNOSTIC
1912 if (ipprintfs)
1913 printf("redirect (%d) to %x\n", code,
1914 (u_int32_t)dest);
1915 #endif
1916 }
1917 }
1918
1919 error = ip_output(m, NULL, &ipforward_rt,
1920 (IP_FORWARDING | (ip_directedbcast ? IP_ALLOWBROADCAST : 0)),
1921 (struct ip_moptions *)NULL, (struct socket *)NULL);
1922
1923 if (error)
1924 IP_STATINC(IP_STAT_CANTFORWARD);
1925 else {
1926 uint64_t *ips = IP_STAT_GETREF();
1927 ips[IP_STAT_FORWARD]++;
1928 if (type) {
1929 ips[IP_STAT_REDIRECTSENT]++;
1930 IP_STAT_PUTREF();
1931 } else {
1932 IP_STAT_PUTREF();
1933 if (mcopy) {
1934 #ifdef GATEWAY
1935 if (mcopy->m_flags & M_CANFASTFWD)
1936 ipflow_create(&ipforward_rt, mcopy);
1937 #endif
1938 m_freem(mcopy);
1939 }
1940 return;
1941 }
1942 }
1943 if (mcopy == NULL)
1944 return;
1945
1946 switch (error) {
1947
1948 case 0: /* forwarded, but need redirect */
1949 /* type, code set above */
1950 break;
1951
1952 case ENETUNREACH: /* shouldn't happen, checked above */
1953 case EHOSTUNREACH:
1954 case ENETDOWN:
1955 case EHOSTDOWN:
1956 default:
1957 type = ICMP_UNREACH;
1958 code = ICMP_UNREACH_HOST;
1959 break;
1960
1961 case EMSGSIZE:
1962 type = ICMP_UNREACH;
1963 code = ICMP_UNREACH_NEEDFRAG;
1964
1965 if ((rt = rtcache_validate(&ipforward_rt)) != NULL) {
1966
1967 #if defined(IPSEC) || defined(FAST_IPSEC)
1968 /*
1969 * If the packet is routed over IPsec tunnel, tell the
1970 * originator the tunnel MTU.
1971 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
1972 * XXX quickhack!!!
1973 */
1974
1975 struct secpolicy *sp;
1976 int ipsecerror;
1977 size_t ipsechdr;
1978 struct route *ro;
1979
1980 sp = ipsec4_getpolicybyaddr(mcopy,
1981 IPSEC_DIR_OUTBOUND, IP_FORWARDING,
1982 &ipsecerror);
1983 #endif
1984
1985 destmtu = rt->rt_ifp->if_mtu;
1986 #if defined(IPSEC) || defined(FAST_IPSEC)
1987 if (sp != NULL) {
1988 /* count IPsec header size */
1989 ipsechdr = ipsec4_hdrsiz(mcopy,
1990 IPSEC_DIR_OUTBOUND, NULL);
1991
1992 /*
1993 * find the correct route for outer IPv4
1994 * header, compute tunnel MTU.
1995 */
1996
1997 if (sp->req != NULL
1998 && sp->req->sav != NULL
1999 && sp->req->sav->sah != NULL) {
2000 ro = &sp->req->sav->sah->sa_route;
2001 if (rt && rt->rt_ifp) {
2002 destmtu =
2003 rt->rt_rmx.rmx_mtu ?
2004 rt->rt_rmx.rmx_mtu :
2005 rt->rt_ifp->if_mtu;
2006 destmtu -= ipsechdr;
2007 }
2008 }
2009
2010 #ifdef IPSEC
2011 key_freesp(sp);
2012 #else
2013 KEY_FREESP(&sp);
2014 #endif
2015 }
2016 #endif /*defined(IPSEC) || defined(FAST_IPSEC)*/
2017 }
2018 IP_STATINC(IP_STAT_CANTFRAG);
2019 break;
2020
2021 case ENOBUFS:
2022 #if 1
2023 /*
2024 * a router should not generate ICMP_SOURCEQUENCH as
2025 * required in RFC1812 Requirements for IP Version 4 Routers.
2026 * source quench could be a big problem under DoS attacks,
2027 * or if the underlying interface is rate-limited.
2028 */
2029 if (mcopy)
2030 m_freem(mcopy);
2031 return;
2032 #else
2033 type = ICMP_SOURCEQUENCH;
2034 code = 0;
2035 break;
2036 #endif
2037 }
2038 icmp_error(mcopy, type, code, dest, destmtu);
2039 }
2040
2041 void
2042 ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
2043 struct mbuf *m)
2044 {
2045
2046 if (inp->inp_socket->so_options & SO_TIMESTAMP) {
2047 struct timeval tv;
2048
2049 microtime(&tv);
2050 *mp = sbcreatecontrol((void *) &tv, sizeof(tv),
2051 SCM_TIMESTAMP, SOL_SOCKET);
2052 if (*mp)
2053 mp = &(*mp)->m_next;
2054 }
2055 if (inp->inp_flags & INP_RECVDSTADDR) {
2056 *mp = sbcreatecontrol((void *) &ip->ip_dst,
2057 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
2058 if (*mp)
2059 mp = &(*mp)->m_next;
2060 }
2061 #ifdef notyet
2062 /*
2063 * XXX
2064 * Moving these out of udp_input() made them even more broken
2065 * than they already were.
2066 * - fenner (at) parc.xerox.com
2067 */
2068 /* options were tossed already */
2069 if (inp->inp_flags & INP_RECVOPTS) {
2070 *mp = sbcreatecontrol((void *) opts_deleted_above,
2071 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
2072 if (*mp)
2073 mp = &(*mp)->m_next;
2074 }
2075 /* ip_srcroute doesn't do what we want here, need to fix */
2076 if (inp->inp_flags & INP_RECVRETOPTS) {
2077 *mp = sbcreatecontrol((void *) ip_srcroute(),
2078 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
2079 if (*mp)
2080 mp = &(*mp)->m_next;
2081 }
2082 #endif
2083 if (inp->inp_flags & INP_RECVIF) {
2084 struct sockaddr_dl sdl;
2085
2086 sockaddr_dl_init(&sdl, sizeof(sdl),
2087 (m->m_pkthdr.rcvif != NULL)
2088 ? m->m_pkthdr.rcvif->if_index
2089 : 0,
2090 0, NULL, 0, NULL, 0);
2091 *mp = sbcreatecontrol(&sdl, sdl.sdl_len, IP_RECVIF, IPPROTO_IP);
2092 if (*mp)
2093 mp = &(*mp)->m_next;
2094 }
2095 }
2096
2097 /*
2098 * sysctl helper routine for net.inet.ip.forwsrcrt.
2099 */
2100 static int
2101 sysctl_net_inet_ip_forwsrcrt(SYSCTLFN_ARGS)
2102 {
2103 int error, tmp;
2104 struct sysctlnode node;
2105
2106 node = *rnode;
2107 tmp = ip_forwsrcrt;
2108 node.sysctl_data = &tmp;
2109 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2110 if (error || newp == NULL)
2111 return (error);
2112
2113 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_FORWSRCRT,
2114 0, NULL, NULL, NULL))
2115 return (EPERM);
2116
2117 ip_forwsrcrt = tmp;
2118
2119 return (0);
2120 }
2121
2122 /*
2123 * sysctl helper routine for net.inet.ip.mtudisctimeout. checks the
2124 * range of the new value and tweaks timers if it changes.
2125 */
2126 static int
2127 sysctl_net_inet_ip_pmtudto(SYSCTLFN_ARGS)
2128 {
2129 int error, tmp;
2130 struct sysctlnode node;
2131
2132 node = *rnode;
2133 tmp = ip_mtudisc_timeout;
2134 node.sysctl_data = &tmp;
2135 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2136 if (error || newp == NULL)
2137 return (error);
2138 if (tmp < 0)
2139 return (EINVAL);
2140
2141 ip_mtudisc_timeout = tmp;
2142 rt_timer_queue_change(ip_mtudisc_timeout_q, ip_mtudisc_timeout);
2143
2144 return (0);
2145 }
2146
2147 #ifdef GATEWAY
2148 /*
2149 * sysctl helper routine for net.inet.ip.maxflows.
2150 */
2151 static int
2152 sysctl_net_inet_ip_maxflows(SYSCTLFN_ARGS)
2153 {
2154 int s;
2155
2156 s = sysctl_lookup(SYSCTLFN_CALL(rnode));
2157 if (s || newp == NULL)
2158 return (s);
2159
2160 s = splsoftnet();
2161 ipflow_prune();
2162 splx(s);
2163
2164 return (0);
2165 }
2166
2167 static int
2168 sysctl_net_inet_ip_hashsize(SYSCTLFN_ARGS)
2169 {
2170 int error, tmp;
2171 struct sysctlnode node;
2172
2173 node = *rnode;
2174 tmp = ip_hashsize;
2175 node.sysctl_data = &tmp;
2176 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2177 if (error || newp == NULL)
2178 return (error);
2179
2180 if ((tmp & (tmp - 1)) == 0 && tmp != 0) {
2181 /*
2182 * Can only fail due to malloc()
2183 */
2184 if (ipflow_invalidate_all(tmp))
2185 return ENOMEM;
2186 } else {
2187 /*
2188 * EINVAL if not a power of 2
2189 */
2190 return EINVAL;
2191 }
2192
2193 return (0);
2194 }
2195 #endif /* GATEWAY */
2196
2197 static void
2198 ipstat_convert_to_user_cb(void *v1, void *v2, struct cpu_info *ci)
2199 {
2200 uint64_t *ipsc = v1;
2201 uint64_t *ips = v2;
2202 u_int i;
2203
2204 for (i = 0; i < IP_NSTATS; i++)
2205 ips[i] += ipsc[i];
2206 }
2207
2208 static void
2209 ipstat_convert_to_user(uint64_t *ips)
2210 {
2211
2212 memset(ips, 0, sizeof(uint64_t) * IP_NSTATS);
2213 percpu_foreach(ipstat_percpu, ipstat_convert_to_user_cb, ips);
2214 }
2215
2216 static int
2217 sysctl_net_inet_ip_stats(SYSCTLFN_ARGS)
2218 {
2219 struct sysctlnode node;
2220 uint64_t ips[IP_NSTATS];
2221
2222 ipstat_convert_to_user(ips);
2223 node = *rnode;
2224 node.sysctl_data = ips;
2225 node.sysctl_size = sizeof(ips);
2226 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
2227 }
2228
2229 SYSCTL_SETUP(sysctl_net_inet_ip_setup, "sysctl net.inet.ip subtree setup")
2230 {
2231 extern int subnetsarelocal, hostzeroisbroadcast;
2232
2233 sysctl_createv(clog, 0, NULL, NULL,
2234 CTLFLAG_PERMANENT,
2235 CTLTYPE_NODE, "net", NULL,
2236 NULL, 0, NULL, 0,
2237 CTL_NET, CTL_EOL);
2238 sysctl_createv(clog, 0, NULL, NULL,
2239 CTLFLAG_PERMANENT,
2240 CTLTYPE_NODE, "inet",
2241 SYSCTL_DESCR("PF_INET related settings"),
2242 NULL, 0, NULL, 0,
2243 CTL_NET, PF_INET, CTL_EOL);
2244 sysctl_createv(clog, 0, NULL, NULL,
2245 CTLFLAG_PERMANENT,
2246 CTLTYPE_NODE, "ip",
2247 SYSCTL_DESCR("IPv4 related settings"),
2248 NULL, 0, NULL, 0,
2249 CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL);
2250
2251 sysctl_createv(clog, 0, NULL, NULL,
2252 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2253 CTLTYPE_INT, "forwarding",
2254 SYSCTL_DESCR("Enable forwarding of INET datagrams"),
2255 NULL, 0, &ipforwarding, 0,
2256 CTL_NET, PF_INET, IPPROTO_IP,
2257 IPCTL_FORWARDING, CTL_EOL);
2258 sysctl_createv(clog, 0, NULL, NULL,
2259 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2260 CTLTYPE_INT, "redirect",
2261 SYSCTL_DESCR("Enable sending of ICMP redirect messages"),
2262 NULL, 0, &ipsendredirects, 0,
2263 CTL_NET, PF_INET, IPPROTO_IP,
2264 IPCTL_SENDREDIRECTS, CTL_EOL);
2265 sysctl_createv(clog, 0, NULL, NULL,
2266 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2267 CTLTYPE_INT, "ttl",
2268 SYSCTL_DESCR("Default TTL for an INET datagram"),
2269 NULL, 0, &ip_defttl, 0,
2270 CTL_NET, PF_INET, IPPROTO_IP,
2271 IPCTL_DEFTTL, CTL_EOL);
2272 #ifdef IPCTL_DEFMTU
2273 sysctl_createv(clog, 0, NULL, NULL,
2274 CTLFLAG_PERMANENT /* |CTLFLAG_READWRITE? */,
2275 CTLTYPE_INT, "mtu",
2276 SYSCTL_DESCR("Default MTA for an INET route"),
2277 NULL, 0, &ip_mtu, 0,
2278 CTL_NET, PF_INET, IPPROTO_IP,
2279 IPCTL_DEFMTU, CTL_EOL);
2280 #endif /* IPCTL_DEFMTU */
2281 sysctl_createv(clog, 0, NULL, NULL,
2282 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2283 CTLTYPE_INT, "forwsrcrt",
2284 SYSCTL_DESCR("Enable forwarding of source-routed "
2285 "datagrams"),
2286 sysctl_net_inet_ip_forwsrcrt, 0, &ip_forwsrcrt, 0,
2287 CTL_NET, PF_INET, IPPROTO_IP,
2288 IPCTL_FORWSRCRT, CTL_EOL);
2289 sysctl_createv(clog, 0, NULL, NULL,
2290 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2291 CTLTYPE_INT, "directed-broadcast",
2292 SYSCTL_DESCR("Enable forwarding of broadcast datagrams"),
2293 NULL, 0, &ip_directedbcast, 0,
2294 CTL_NET, PF_INET, IPPROTO_IP,
2295 IPCTL_DIRECTEDBCAST, CTL_EOL);
2296 sysctl_createv(clog, 0, NULL, NULL,
2297 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2298 CTLTYPE_INT, "allowsrcrt",
2299 SYSCTL_DESCR("Accept source-routed datagrams"),
2300 NULL, 0, &ip_allowsrcrt, 0,
2301 CTL_NET, PF_INET, IPPROTO_IP,
2302 IPCTL_ALLOWSRCRT, CTL_EOL);
2303 sysctl_createv(clog, 0, NULL, NULL,
2304 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2305 CTLTYPE_INT, "subnetsarelocal",
2306 SYSCTL_DESCR("Whether logical subnets are considered "
2307 "local"),
2308 NULL, 0, &subnetsarelocal, 0,
2309 CTL_NET, PF_INET, IPPROTO_IP,
2310 IPCTL_SUBNETSARELOCAL, CTL_EOL);
2311 sysctl_createv(clog, 0, NULL, NULL,
2312 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2313 CTLTYPE_INT, "mtudisc",
2314 SYSCTL_DESCR("Use RFC1191 Path MTU Discovery"),
2315 NULL, 0, &ip_mtudisc, 0,
2316 CTL_NET, PF_INET, IPPROTO_IP,
2317 IPCTL_MTUDISC, CTL_EOL);
2318 sysctl_createv(clog, 0, NULL, NULL,
2319 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2320 CTLTYPE_INT, "anonportmin",
2321 SYSCTL_DESCR("Lowest ephemeral port number to assign"),
2322 sysctl_net_inet_ip_ports, 0, &anonportmin, 0,
2323 CTL_NET, PF_INET, IPPROTO_IP,
2324 IPCTL_ANONPORTMIN, CTL_EOL);
2325 sysctl_createv(clog, 0, NULL, NULL,
2326 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2327 CTLTYPE_INT, "anonportmax",
2328 SYSCTL_DESCR("Highest ephemeral port number to assign"),
2329 sysctl_net_inet_ip_ports, 0, &anonportmax, 0,
2330 CTL_NET, PF_INET, IPPROTO_IP,
2331 IPCTL_ANONPORTMAX, CTL_EOL);
2332 sysctl_createv(clog, 0, NULL, NULL,
2333 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2334 CTLTYPE_INT, "mtudisctimeout",
2335 SYSCTL_DESCR("Lifetime of a Path MTU Discovered route"),
2336 sysctl_net_inet_ip_pmtudto, 0, &ip_mtudisc_timeout, 0,
2337 CTL_NET, PF_INET, IPPROTO_IP,
2338 IPCTL_MTUDISCTIMEOUT, CTL_EOL);
2339 #ifdef GATEWAY
2340 sysctl_createv(clog, 0, NULL, NULL,
2341 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2342 CTLTYPE_INT, "maxflows",
2343 SYSCTL_DESCR("Number of flows for fast forwarding"),
2344 sysctl_net_inet_ip_maxflows, 0, &ip_maxflows, 0,
2345 CTL_NET, PF_INET, IPPROTO_IP,
2346 IPCTL_MAXFLOWS, CTL_EOL);
2347 sysctl_createv(clog, 0, NULL, NULL,
2348 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2349 CTLTYPE_INT, "hashsize",
2350 SYSCTL_DESCR("Size of hash table for fast forwarding (IPv4)"),
2351 sysctl_net_inet_ip_hashsize, 0, &ip_hashsize, 0,
2352 CTL_NET, PF_INET, IPPROTO_IP,
2353 CTL_CREATE, CTL_EOL);
2354 #endif /* GATEWAY */
2355 sysctl_createv(clog, 0, NULL, NULL,
2356 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2357 CTLTYPE_INT, "hostzerobroadcast",
2358 SYSCTL_DESCR("All zeroes address is broadcast address"),
2359 NULL, 0, &hostzeroisbroadcast, 0,
2360 CTL_NET, PF_INET, IPPROTO_IP,
2361 IPCTL_HOSTZEROBROADCAST, CTL_EOL);
2362 #if NGIF > 0
2363 sysctl_createv(clog, 0, NULL, NULL,
2364 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2365 CTLTYPE_INT, "gifttl",
2366 SYSCTL_DESCR("Default TTL for a gif tunnel datagram"),
2367 NULL, 0, &ip_gif_ttl, 0,
2368 CTL_NET, PF_INET, IPPROTO_IP,
2369 IPCTL_GIF_TTL, CTL_EOL);
2370 #endif /* NGIF */
2371 #ifndef IPNOPRIVPORTS
2372 sysctl_createv(clog, 0, NULL, NULL,
2373 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2374 CTLTYPE_INT, "lowportmin",
2375 SYSCTL_DESCR("Lowest privileged ephemeral port number "
2376 "to assign"),
2377 sysctl_net_inet_ip_ports, 0, &lowportmin, 0,
2378 CTL_NET, PF_INET, IPPROTO_IP,
2379 IPCTL_LOWPORTMIN, CTL_EOL);
2380 sysctl_createv(clog, 0, NULL, NULL,
2381 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2382 CTLTYPE_INT, "lowportmax",
2383 SYSCTL_DESCR("Highest privileged ephemeral port number "
2384 "to assign"),
2385 sysctl_net_inet_ip_ports, 0, &lowportmax, 0,
2386 CTL_NET, PF_INET, IPPROTO_IP,
2387 IPCTL_LOWPORTMAX, CTL_EOL);
2388 #endif /* IPNOPRIVPORTS */
2389 sysctl_createv(clog, 0, NULL, NULL,
2390 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2391 CTLTYPE_INT, "maxfragpackets",
2392 SYSCTL_DESCR("Maximum number of fragments to retain for "
2393 "possible reassembly"),
2394 NULL, 0, &ip_maxfragpackets, 0,
2395 CTL_NET, PF_INET, IPPROTO_IP,
2396 IPCTL_MAXFRAGPACKETS, CTL_EOL);
2397 #if NGRE > 0
2398 sysctl_createv(clog, 0, NULL, NULL,
2399 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2400 CTLTYPE_INT, "grettl",
2401 SYSCTL_DESCR("Default TTL for a gre tunnel datagram"),
2402 NULL, 0, &ip_gre_ttl, 0,
2403 CTL_NET, PF_INET, IPPROTO_IP,
2404 IPCTL_GRE_TTL, CTL_EOL);
2405 #endif /* NGRE */
2406 sysctl_createv(clog, 0, NULL, NULL,
2407 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2408 CTLTYPE_INT, "checkinterface",
2409 SYSCTL_DESCR("Enable receive side of Strong ES model "
2410 "from RFC1122"),
2411 NULL, 0, &ip_checkinterface, 0,
2412 CTL_NET, PF_INET, IPPROTO_IP,
2413 IPCTL_CHECKINTERFACE, CTL_EOL);
2414 sysctl_createv(clog, 0, NULL, NULL,
2415 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2416 CTLTYPE_INT, "random_id",
2417 SYSCTL_DESCR("Assign random ip_id values"),
2418 NULL, 0, &ip_do_randomid, 0,
2419 CTL_NET, PF_INET, IPPROTO_IP,
2420 IPCTL_RANDOMID, CTL_EOL);
2421 sysctl_createv(clog, 0, NULL, NULL,
2422 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2423 CTLTYPE_INT, "do_loopback_cksum",
2424 SYSCTL_DESCR("Perform IP checksum on loopback"),
2425 NULL, 0, &ip_do_loopback_cksum, 0,
2426 CTL_NET, PF_INET, IPPROTO_IP,
2427 IPCTL_LOOPBACKCKSUM, CTL_EOL);
2428 sysctl_createv(clog, 0, NULL, NULL,
2429 CTLFLAG_PERMANENT,
2430 CTLTYPE_STRUCT, "stats",
2431 SYSCTL_DESCR("IP statistics"),
2432 sysctl_net_inet_ip_stats, 0, NULL, 0,
2433 CTL_NET, PF_INET, IPPROTO_IP, IPCTL_STATS,
2434 CTL_EOL);
2435 }
2436
2437 void
2438 ip_statinc(u_int stat)
2439 {
2440
2441 KASSERT(stat < IP_NSTATS);
2442 IP_STATINC(stat);
2443 }
2444