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