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