Deleted Added
sdiff udiff text old ( 83130 ) new ( 83934 )
full compact
1/*
2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
34 * $FreeBSD: head/sys/netinet/ip_input.c 83130 2001-09-06 02:40:43Z jlemon $
35 */
36
37#define _IP_VHL
38
39#include "opt_bootp.h"
40#include "opt_ipfw.h"
41#include "opt_ipdn.h"
42#include "opt_ipdivert.h"
43#include "opt_ipfilter.h"
44#include "opt_ipstealth.h"
45#include "opt_ipsec.h"
46#include "opt_pfil_hooks.h"
47#include "opt_random_ip_id.h"
48
49#include <sys/param.h>
50#include <sys/systm.h>
51#include <sys/mbuf.h>
52#include <sys/malloc.h>
53#include <sys/domain.h>
54#include <sys/protosw.h>
55#include <sys/socket.h>
56#include <sys/time.h>
57#include <sys/kernel.h>
58#include <sys/syslog.h>
59#include <sys/sysctl.h>
60
61#include <net/pfil.h>
62#include <net/if.h>
63#include <net/if_var.h>
64#include <net/if_dl.h>
65#include <net/route.h>
66#include <net/netisr.h>
67#include <net/intrq.h>
68
69#include <netinet/in.h>
70#include <netinet/in_systm.h>
71#include <netinet/in_var.h>
72#include <netinet/ip.h>
73#include <netinet/in_pcb.h>
74#include <netinet/ip_var.h>
75#include <netinet/ip_icmp.h>
76#include <machine/in_cksum.h>
77
78#include <sys/socketvar.h>
79
80#include <netinet/ip_fw.h>
81
82#ifdef IPSEC
83#include <netinet6/ipsec.h>
84#include <netkey/key.h>
85#endif
86
87#include "faith.h"
88#if defined(NFAITH) && NFAITH > 0
89#include <net/if_types.h>
90#endif
91
92#ifdef DUMMYNET
93#include <netinet/ip_dummynet.h>
94#endif
95
96int rsvp_on = 0;
97static int ip_rsvp_on;
98struct socket *ip_rsvpd;
99
100int ipforwarding = 0;
101SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW,
102 &ipforwarding, 0, "Enable IP forwarding between interfaces");
103
104static int ipsendredirects = 1; /* XXX */
105SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW,
106 &ipsendredirects, 0, "Enable sending IP redirects");
107
108int ip_defttl = IPDEFTTL;
109SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW,
110 &ip_defttl, 0, "Maximum TTL on IP packets");
111
112static int ip_dosourceroute = 0;
113SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW,
114 &ip_dosourceroute, 0, "Enable forwarding source routed IP packets");
115
116static int ip_acceptsourceroute = 0;
117SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute,
118 CTLFLAG_RW, &ip_acceptsourceroute, 0,
119 "Enable accepting source routed IP packets");
120
121static int ip_keepfaith = 0;
122SYSCTL_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW,
123 &ip_keepfaith, 0,
124 "Enable packet capture for FAITH IPv4->IPv6 translater daemon");
125
126static int ip_nfragpackets = 0;
127static int ip_maxfragpackets; /* initialized in ip_init() */
128SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragpackets, CTLFLAG_RW,
129 &ip_maxfragpackets, 0,
130 "Maximum number of IPv4 fragment reassembly queue entries");
131
132/*
133 * XXX - Setting ip_checkinterface mostly implements the receive side of
134 * the Strong ES model described in RFC 1122, but since the routing table
135 * and transmit implementation do not implement the Strong ES model,
136 * setting this to 1 results in an odd hybrid.
137 *
138 * XXX - ip_checkinterface currently must be disabled if you use ipnat
139 * to translate the destination address to another local interface.
140 *
141 * XXX - ip_checkinterface must be disabled if you add IP aliases
142 * to the loopback interface instead of the interface where the
143 * packets for those addresses are received.
144 */
145static int ip_checkinterface = 1;
146SYSCTL_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW,
147 &ip_checkinterface, 0, "Verify packet arrives on correct interface");
148
149#ifdef DIAGNOSTIC
150static int ipprintfs = 0;
151#endif
152
153extern struct domain inetdomain;
154extern struct protosw inetsw[];
155u_char ip_protox[IPPROTO_MAX];
156static int ipqmaxlen = IFQ_MAXLEN;
157struct in_ifaddrhead in_ifaddrhead; /* first inet address */
158SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RW,
159 &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue");
160SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD,
161 &ipintrq.ifq_drops, 0, "Number of packets dropped from the IP input queue");
162
163struct ipstat ipstat;
164SYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RW,
165 &ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)");
166
167/* Packet reassembly stuff */
168#define IPREASS_NHASH_LOG2 6
169#define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2)
170#define IPREASS_HMASK (IPREASS_NHASH - 1)
171#define IPREASS_HASH(x,y) \
172 (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
173
174static TAILQ_HEAD(ipqhead, ipq) ipq[IPREASS_NHASH];
175static int nipq = 0; /* total # of reass queues */
176static int maxnipq;
177const int ipintrq_present = 1;
178
179#ifdef IPCTL_DEFMTU
180SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
181 &ip_mtu, 0, "Default MTU");
182#endif
183
184#ifdef IPSTEALTH
185static int ipstealth = 0;
186SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW,
187 &ipstealth, 0, "");
188#endif
189
190
191/* Firewall hooks */
192ip_fw_chk_t *ip_fw_chk_ptr;
193ip_fw_ctl_t *ip_fw_ctl_ptr;
194int fw_enable = 1 ;
195
196#ifdef DUMMYNET
197ip_dn_ctl_t *ip_dn_ctl_ptr;
198#endif
199
200
201/*
202 * We need to save the IP options in case a protocol wants to respond
203 * to an incoming packet over the same route if the packet got here
204 * using IP source routing. This allows connection establishment and
205 * maintenance when the remote end is on a network that is not known
206 * to us.
207 */
208static int ip_nhops = 0;
209static struct ip_srcrt {
210 struct in_addr dst; /* final destination */
211 char nop; /* one NOP to align */
212 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */
213 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
214} ip_srcrt;
215
216struct sockaddr_in *ip_fw_fwd_addr;
217
218static void save_rte __P((u_char *, struct in_addr));
219static int ip_dooptions __P((struct mbuf *));
220static void ip_forward __P((struct mbuf *, int));
221static void ip_freef __P((struct ipqhead *, struct ipq *));
222#ifdef IPDIVERT
223static struct mbuf *ip_reass __P((struct mbuf *, struct ipqhead *, struct ipq *, u_int32_t *, u_int16_t *));
224#else
225static struct mbuf *ip_reass __P((struct mbuf *, struct ipqhead *, struct ipq *));
226#endif
227static struct in_ifaddr *ip_rtaddr __P((struct in_addr));
228static void ipintr __P((void));
229
230/*
231 * IP initialization: fill in IP protocol switch table.
232 * All protocols not implemented in kernel go to raw IP protocol handler.
233 */
234void
235ip_init()
236{
237 register struct protosw *pr;
238 register int i;
239
240 TAILQ_INIT(&in_ifaddrhead);
241 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
242 if (pr == 0)
243 panic("ip_init");
244 for (i = 0; i < IPPROTO_MAX; i++)
245 ip_protox[i] = pr - inetsw;
246 for (pr = inetdomain.dom_protosw;
247 pr < inetdomain.dom_protoswNPROTOSW; pr++)
248 if (pr->pr_domain->dom_family == PF_INET &&
249 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
250 ip_protox[pr->pr_protocol] = pr - inetsw;
251
252 for (i = 0; i < IPREASS_NHASH; i++)
253 TAILQ_INIT(&ipq[i]);
254
255 maxnipq = nmbclusters / 4;
256 ip_maxfragpackets = nmbclusters / 4;
257
258#ifndef RANDOM_IP_ID
259 ip_id = time_second & 0xffff;
260#endif
261 ipintrq.ifq_maxlen = ipqmaxlen;
262 mtx_init(&ipintrq.ifq_mtx, "ip_inq", MTX_DEF);
263
264 register_netisr(NETISR_IP, ipintr);
265}
266
267static struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
268struct route ipforward_rt;
269
270/*
271 * Ip input routine. Checksum and byte swap header. If fragmented
272 * try to reassemble. Process options. Pass to next level.
273 */
274void
275ip_input(struct mbuf *m)
276{
277 struct ip *ip;
278 struct ipq *fp;
279 struct in_ifaddr *ia = NULL;
280 int i, hlen, checkif;
281 u_short sum;
282 u_int16_t divert_cookie; /* firewall cookie */
283 struct in_addr pkt_dst;
284#ifdef IPDIVERT
285 u_int32_t divert_info = 0; /* packet divert/tee info */
286#endif
287 struct ip_fw_chain *rule = NULL;
288#ifdef PFIL_HOOKS
289 struct packet_filter_hook *pfh;
290 struct mbuf *m0;
291 int rv;
292#endif /* PFIL_HOOKS */
293
294#ifdef IPDIVERT
295 /* Get and reset firewall cookie */
296 divert_cookie = ip_divert_cookie;
297 ip_divert_cookie = 0;
298#else
299 divert_cookie = 0;
300#endif
301
302#if defined(IPFIREWALL) && defined(DUMMYNET)
303 /*
304 * dummynet packet are prepended a vestigial mbuf with
305 * m_type = MT_DUMMYNET and m_data pointing to the matching
306 * rule.
307 */
308 if (m->m_type == MT_DUMMYNET) {
309 rule = (struct ip_fw_chain *)(m->m_data) ;
310 m = m->m_next ;
311 ip = mtod(m, struct ip *);
312 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
313 goto iphack ;
314 } else
315 rule = NULL ;
316#endif
317
318#ifdef DIAGNOSTIC
319 if (m == NULL || (m->m_flags & M_PKTHDR) == 0)
320 panic("ip_input no HDR");
321#endif
322 ipstat.ips_total++;
323
324 if (m->m_pkthdr.len < sizeof(struct ip))
325 goto tooshort;
326
327 if (m->m_len < sizeof (struct ip) &&
328 (m = m_pullup(m, sizeof (struct ip))) == 0) {
329 ipstat.ips_toosmall++;
330 return;
331 }
332 ip = mtod(m, struct ip *);
333
334 if (IP_VHL_V(ip->ip_vhl) != IPVERSION) {
335 ipstat.ips_badvers++;
336 goto bad;
337 }
338
339 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
340 if (hlen < sizeof(struct ip)) { /* minimum header length */
341 ipstat.ips_badhlen++;
342 goto bad;
343 }
344 if (hlen > m->m_len) {
345 if ((m = m_pullup(m, hlen)) == 0) {
346 ipstat.ips_badhlen++;
347 return;
348 }
349 ip = mtod(m, struct ip *);
350 }
351
352 /* 127/8 must not appear on wire - RFC1122 */
353 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
354 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
355 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
356 ipstat.ips_badaddr++;
357 goto bad;
358 }
359 }
360
361 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
362 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
363 } else {
364 if (hlen == sizeof(struct ip)) {
365 sum = in_cksum_hdr(ip);
366 } else {
367 sum = in_cksum(m, hlen);
368 }
369 }
370 if (sum) {
371 ipstat.ips_badsum++;
372 goto bad;
373 }
374
375 /*
376 * Convert fields to host representation.
377 */
378 NTOHS(ip->ip_len);
379 if (ip->ip_len < hlen) {
380 ipstat.ips_badlen++;
381 goto bad;
382 }
383 NTOHS(ip->ip_off);
384
385 /*
386 * Check that the amount of data in the buffers
387 * is as at least much as the IP header would have us expect.
388 * Trim mbufs if longer than we expect.
389 * Drop packet if shorter than we expect.
390 */
391 if (m->m_pkthdr.len < ip->ip_len) {
392tooshort:
393 ipstat.ips_tooshort++;
394 goto bad;
395 }
396 if (m->m_pkthdr.len > ip->ip_len) {
397 if (m->m_len == m->m_pkthdr.len) {
398 m->m_len = ip->ip_len;
399 m->m_pkthdr.len = ip->ip_len;
400 } else
401 m_adj(m, ip->ip_len - m->m_pkthdr.len);
402 }
403
404#ifdef IPSEC
405 if (ipsec_gethist(m, NULL))
406 goto pass;
407#endif
408
409 /*
410 * IpHack's section.
411 * Right now when no processing on packet has done
412 * and it is still fresh out of network we do our black
413 * deals with it.
414 * - Firewall: deny/allow/divert
415 * - Xlate: translate packet's addr/port (NAT).
416 * - Pipe: pass pkt through dummynet.
417 * - Wrap: fake packet's addr/port <unimpl.>
418 * - Encapsulate: put it in another IP and send out. <unimp.>
419 */
420
421#if defined(IPFIREWALL) && defined(DUMMYNET)
422iphack:
423#endif
424
425#ifdef PFIL_HOOKS
426 /*
427 * Run through list of hooks for input packets. If there are any
428 * filters which require that additional packets in the flow are
429 * not fast-forwarded, they must clear the M_CANFASTFWD flag.
430 * Note that filters must _never_ set this flag, as another filter
431 * in the list may have previously cleared it.
432 */
433 m0 = m;
434 pfh = pfil_hook_get(PFIL_IN, &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
435 for (; pfh; pfh = TAILQ_NEXT(pfh, pfil_link))
436 if (pfh->pfil_func) {
437 rv = pfh->pfil_func(ip, hlen,
438 m->m_pkthdr.rcvif, 0, &m0);
439 if (rv)
440 return;
441 m = m0;
442 if (m == NULL)
443 return;
444 ip = mtod(m, struct ip *);
445 }
446#endif /* PFIL_HOOKS */
447
448 if (fw_enable && ip_fw_chk_ptr) {
449#ifdef IPFIREWALL_FORWARD
450 /*
451 * If we've been forwarded from the output side, then
452 * skip the firewall a second time
453 */
454 if (ip_fw_fwd_addr)
455 goto ours;
456#endif /* IPFIREWALL_FORWARD */
457 /*
458 * See the comment in ip_output for the return values
459 * produced by the firewall.
460 */
461 i = (*ip_fw_chk_ptr)(&ip,
462 hlen, NULL, &divert_cookie, &m, &rule, &ip_fw_fwd_addr);
463 if (i & IP_FW_PORT_DENY_FLAG) { /* XXX new interface-denied */
464 if (m)
465 m_freem(m);
466 return ;
467 }
468 if (m == NULL) { /* Packet discarded by firewall */
469 static int __debug=10;
470 if (__debug >0) {
471 printf("firewall returns NULL, please update!\n");
472 __debug-- ;
473 }
474 return;
475 }
476 if (i == 0 && ip_fw_fwd_addr == NULL) /* common case */
477 goto pass;
478#ifdef DUMMYNET
479 if ((i & IP_FW_PORT_DYNT_FLAG) != 0) {
480 /* Send packet to the appropriate pipe */
481 dummynet_io(i&0xffff,DN_TO_IP_IN,m,NULL,NULL,0, rule,
482 0);
483 return;
484 }
485#endif
486#ifdef IPDIVERT
487 if (i != 0 && (i & IP_FW_PORT_DYNT_FLAG) == 0) {
488 /* Divert or tee packet */
489 divert_info = i;
490 goto ours;
491 }
492#endif
493#ifdef IPFIREWALL_FORWARD
494 if (i == 0 && ip_fw_fwd_addr != NULL)
495 goto pass;
496#endif
497 /*
498 * if we get here, the packet must be dropped
499 */
500 m_freem(m);
501 return;
502 }
503pass:
504
505 /*
506 * Process options and, if not destined for us,
507 * ship it on. ip_dooptions returns 1 when an
508 * error was detected (causing an icmp message
509 * to be sent and the original packet to be freed).
510 */
511 ip_nhops = 0; /* for source routed packets */
512 if (hlen > sizeof (struct ip) && ip_dooptions(m)) {
513#ifdef IPFIREWALL_FORWARD
514 ip_fw_fwd_addr = NULL;
515#endif
516 return;
517 }
518
519 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
520 * matter if it is destined to another node, or whether it is
521 * a multicast one, RSVP wants it! and prevents it from being forwarded
522 * anywhere else. Also checks if the rsvp daemon is running before
523 * grabbing the packet.
524 */
525 if (rsvp_on && ip->ip_p==IPPROTO_RSVP)
526 goto ours;
527
528 /*
529 * Check our list of addresses, to see if the packet is for us.
530 * If we don't have any addresses, assume any unicast packet
531 * we receive might be for us (and let the upper layers deal
532 * with it).
533 */
534 if (TAILQ_EMPTY(&in_ifaddrhead) &&
535 (m->m_flags & (M_MCAST|M_BCAST)) == 0)
536 goto ours;
537
538 /*
539 * Cache the destination address of the packet; this may be
540 * changed by use of 'ipfw fwd'.
541 */
542 pkt_dst = ip_fw_fwd_addr == NULL ?
543 ip->ip_dst : ip_fw_fwd_addr->sin_addr;
544
545 /*
546 * Enable a consistency check between the destination address
547 * and the arrival interface for a unicast packet (the RFC 1122
548 * strong ES model) if IP forwarding is disabled and the packet
549 * is not locally generated and the packet is not subject to
550 * 'ipfw fwd'.
551 *
552 * XXX - Checking also should be disabled if the destination
553 * address is ipnat'ed to a different interface.
554 *
555 * XXX - Checking is incompatible with IP aliases added
556 * to the loopback interface instead of the interface where
557 * the packets are received.
558 */
559 checkif = ip_checkinterface && (ipforwarding == 0) &&
560 ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) &&
561 (ip_fw_fwd_addr == NULL);
562
563 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
564#define satosin(sa) ((struct sockaddr_in *)(sa))
565
566#ifdef BOOTP_COMPAT
567 if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY)
568 goto ours;
569#endif
570 /*
571 * If the address matches, verify that the packet
572 * arrived via the correct interface if checking is
573 * enabled.
574 */
575 if (IA_SIN(ia)->sin_addr.s_addr == pkt_dst.s_addr &&
576 (!checkif || ia->ia_ifp == m->m_pkthdr.rcvif))
577 goto ours;
578 /*
579 * Only accept broadcast packets that arrive via the
580 * matching interface. Reception of forwarded directed
581 * broadcasts would be handled via ip_forward() and
582 * ether_output() with the loopback into the stack for
583 * SIMPLEX interfaces handled by ether_output().
584 */
585 if (ia->ia_ifp == m->m_pkthdr.rcvif &&
586 ia->ia_ifp && ia->ia_ifp->if_flags & IFF_BROADCAST) {
587 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
588 pkt_dst.s_addr)
589 goto ours;
590 if (ia->ia_netbroadcast.s_addr == pkt_dst.s_addr)
591 goto ours;
592 }
593 }
594 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
595 struct in_multi *inm;
596 if (ip_mrouter) {
597 /*
598 * If we are acting as a multicast router, all
599 * incoming multicast packets are passed to the
600 * kernel-level multicast forwarding function.
601 * The packet is returned (relatively) intact; if
602 * ip_mforward() returns a non-zero value, the packet
603 * must be discarded, else it may be accepted below.
604 */
605 if (ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) {
606 ipstat.ips_cantforward++;
607 m_freem(m);
608 return;
609 }
610
611 /*
612 * The process-level routing demon needs to receive
613 * all multicast IGMP packets, whether or not this
614 * host belongs to their destination groups.
615 */
616 if (ip->ip_p == IPPROTO_IGMP)
617 goto ours;
618 ipstat.ips_forward++;
619 }
620 /*
621 * See if we belong to the destination multicast group on the
622 * arrival interface.
623 */
624 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
625 if (inm == NULL) {
626 ipstat.ips_notmember++;
627 m_freem(m);
628 return;
629 }
630 goto ours;
631 }
632 if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
633 goto ours;
634 if (ip->ip_dst.s_addr == INADDR_ANY)
635 goto ours;
636
637#if defined(NFAITH) && 0 < NFAITH
638 /*
639 * FAITH(Firewall Aided Internet Translator)
640 */
641 if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
642 if (ip_keepfaith) {
643 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP)
644 goto ours;
645 }
646 m_freem(m);
647 return;
648 }
649#endif
650 /*
651 * Not for us; forward if possible and desirable.
652 */
653 if (ipforwarding == 0) {
654 ipstat.ips_cantforward++;
655 m_freem(m);
656 } else
657 ip_forward(m, 0);
658#ifdef IPFIREWALL_FORWARD
659 ip_fw_fwd_addr = NULL;
660#endif
661 return;
662
663ours:
664 /* Count the packet in the ip address stats */
665 if (ia != NULL) {
666 ia->ia_ifa.if_ipackets++;
667 ia->ia_ifa.if_ibytes += m->m_pkthdr.len;
668 }
669
670 /*
671 * If offset or IP_MF are set, must reassemble.
672 * Otherwise, nothing need be done.
673 * (We could look in the reassembly queue to see
674 * if the packet was previously fragmented,
675 * but it's not worth the time; just let them time out.)
676 */
677 if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
678
679 sum = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
680 /*
681 * Look for queue of fragments
682 * of this datagram.
683 */
684 TAILQ_FOREACH(fp, &ipq[sum], ipq_list)
685 if (ip->ip_id == fp->ipq_id &&
686 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
687 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
688 ip->ip_p == fp->ipq_p)
689 goto found;
690
691 fp = 0;
692
693 /* check if there's a place for the new queue */
694 if (nipq > maxnipq) {
695 /*
696 * drop something from the tail of the current queue
697 * before proceeding further
698 */
699 struct ipq *q = TAILQ_LAST(&ipq[sum], ipqhead);
700 if (q == NULL) { /* gak */
701 for (i = 0; i < IPREASS_NHASH; i++) {
702 struct ipq *r = TAILQ_LAST(&ipq[i], ipqhead);
703 if (r) {
704 ip_freef(&ipq[i], r);
705 break;
706 }
707 }
708 } else
709 ip_freef(&ipq[sum], q);
710 }
711found:
712 /*
713 * Adjust ip_len to not reflect header,
714 * convert offset of this to bytes.
715 */
716 ip->ip_len -= hlen;
717 if (ip->ip_off & IP_MF) {
718 /*
719 * Make sure that fragments have a data length
720 * that's a non-zero multiple of 8 bytes.
721 */
722 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
723 ipstat.ips_toosmall++; /* XXX */
724 goto bad;
725 }
726 m->m_flags |= M_FRAG;
727 }
728 ip->ip_off <<= 3;
729
730 /*
731 * Attempt reassembly; if it succeeds, proceed.
732 */
733 ipstat.ips_fragments++;
734 m->m_pkthdr.header = ip;
735#ifdef IPDIVERT
736 m = ip_reass(m,
737 &ipq[sum], fp, &divert_info, &divert_cookie);
738#else
739 m = ip_reass(m, &ipq[sum], fp);
740#endif
741 if (m == 0) {
742#ifdef IPFIREWALL_FORWARD
743 ip_fw_fwd_addr = NULL;
744#endif
745 return;
746 }
747 ipstat.ips_reassembled++;
748 ip = mtod(m, struct ip *);
749 /* Get the header length of the reassembled packet */
750 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
751#ifdef IPDIVERT
752 /* Restore original checksum before diverting packet */
753 if (divert_info != 0) {
754 ip->ip_len += hlen;
755 HTONS(ip->ip_len);
756 HTONS(ip->ip_off);
757 ip->ip_sum = 0;
758 if (hlen == sizeof(struct ip))
759 ip->ip_sum = in_cksum_hdr(ip);
760 else
761 ip->ip_sum = in_cksum(m, hlen);
762 NTOHS(ip->ip_off);
763 NTOHS(ip->ip_len);
764 ip->ip_len -= hlen;
765 }
766#endif
767 } else
768 ip->ip_len -= hlen;
769
770#ifdef IPDIVERT
771 /*
772 * Divert or tee packet to the divert protocol if required.
773 *
774 * If divert_info is zero then cookie should be too, so we shouldn't
775 * need to clear them here. Assume divert_packet() does so also.
776 */
777 if (divert_info != 0) {
778 struct mbuf *clone = NULL;
779
780 /* Clone packet if we're doing a 'tee' */
781 if ((divert_info & IP_FW_PORT_TEE_FLAG) != 0)
782 clone = m_dup(m, M_DONTWAIT);
783
784 /* Restore packet header fields to original values */
785 ip->ip_len += hlen;
786 HTONS(ip->ip_len);
787 HTONS(ip->ip_off);
788
789 /* Deliver packet to divert input routine */
790 ip_divert_cookie = divert_cookie;
791 divert_packet(m, 1, divert_info & 0xffff);
792 ipstat.ips_delivered++;
793
794 /* If 'tee', continue with original packet */
795 if (clone == NULL)
796 return;
797 m = clone;
798 ip = mtod(m, struct ip *);
799 }
800#endif
801
802#ifdef IPSEC
803 /*
804 * enforce IPsec policy checking if we are seeing last header.
805 * note that we do not visit this with protocols with pcb layer
806 * code - like udp/tcp/raw ip.
807 */
808 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0 &&
809 ipsec4_in_reject(m, NULL)) {
810 ipsecstat.in_polvio++;
811 goto bad;
812 }
813#endif
814
815 /*
816 * Switch out to protocol's input routine.
817 */
818 ipstat.ips_delivered++;
819 {
820 int off = hlen;
821
822 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, off);
823#ifdef IPFIREWALL_FORWARD
824 ip_fw_fwd_addr = NULL; /* tcp needed it */
825#endif
826 return;
827 }
828bad:
829#ifdef IPFIREWALL_FORWARD
830 ip_fw_fwd_addr = NULL;
831#endif
832 m_freem(m);
833}
834
835/*
836 * IP software interrupt routine - to go away sometime soon
837 */
838static void
839ipintr(void)
840{
841 struct mbuf *m;
842
843 while (1) {
844 IF_DEQUEUE(&ipintrq, m);
845 if (m == 0)
846 return;
847 ip_input(m);
848 }
849}
850
851/*
852 * Take incoming datagram fragment and try to reassemble it into
853 * whole datagram. If a chain for reassembly of this datagram already
854 * exists, then it is given as fp; otherwise have to make a chain.
855 *
856 * When IPDIVERT enabled, keep additional state with each packet that
857 * tells us if we need to divert or tee the packet we're building.
858 */
859
860static struct mbuf *
861#ifdef IPDIVERT
862ip_reass(m, head, fp, divinfo, divcookie)
863#else
864ip_reass(m, head, fp)
865#endif
866 struct mbuf *m;
867 struct ipqhead *head;
868 struct ipq *fp;
869#ifdef IPDIVERT
870 u_int32_t *divinfo;
871 u_int16_t *divcookie;
872#endif
873{
874 struct ip *ip = mtod(m, struct ip *);
875 register struct mbuf *p, *q, *nq;
876 struct mbuf *t;
877 int hlen = IP_VHL_HL(ip->ip_vhl) << 2;
878 int i, next;
879
880 /*
881 * Presence of header sizes in mbufs
882 * would confuse code below.
883 */
884 m->m_data += hlen;
885 m->m_len -= hlen;
886
887 /*
888 * If first fragment to arrive, create a reassembly queue.
889 */
890 if (fp == 0) {
891 /*
892 * Enforce upper bound on number of fragmented packets
893 * for which we attempt reassembly;
894 * If maxfrag is 0, never accept fragments.
895 * If maxfrag is -1, accept all fragments without limitation.
896 */
897 if ((ip_maxfragpackets >= 0) && (ip_nfragpackets >= ip_maxfragpackets))
898 goto dropfrag;
899 ip_nfragpackets++;
900 if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)
901 goto dropfrag;
902 fp = mtod(t, struct ipq *);
903 TAILQ_INSERT_HEAD(head, fp, ipq_list);
904 nipq++;
905 fp->ipq_ttl = IPFRAGTTL;
906 fp->ipq_p = ip->ip_p;
907 fp->ipq_id = ip->ip_id;
908 fp->ipq_src = ip->ip_src;
909 fp->ipq_dst = ip->ip_dst;
910 fp->ipq_frags = m;
911 m->m_nextpkt = NULL;
912#ifdef IPDIVERT
913 fp->ipq_div_info = 0;
914 fp->ipq_div_cookie = 0;
915#endif
916 goto inserted;
917 }
918
919#define GETIP(m) ((struct ip*)((m)->m_pkthdr.header))
920
921 /*
922 * Find a segment which begins after this one does.
923 */
924 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
925 if (GETIP(q)->ip_off > ip->ip_off)
926 break;
927
928 /*
929 * If there is a preceding segment, it may provide some of
930 * our data already. If so, drop the data from the incoming
931 * segment. If it provides all of our data, drop us, otherwise
932 * stick new segment in the proper place.
933 *
934 * If some of the data is dropped from the the preceding
935 * segment, then it's checksum is invalidated.
936 */
937 if (p) {
938 i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
939 if (i > 0) {
940 if (i >= ip->ip_len)
941 goto dropfrag;
942 m_adj(m, i);
943 m->m_pkthdr.csum_flags = 0;
944 ip->ip_off += i;
945 ip->ip_len -= i;
946 }
947 m->m_nextpkt = p->m_nextpkt;
948 p->m_nextpkt = m;
949 } else {
950 m->m_nextpkt = fp->ipq_frags;
951 fp->ipq_frags = m;
952 }
953
954 /*
955 * While we overlap succeeding segments trim them or,
956 * if they are completely covered, dequeue them.
957 */
958 for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
959 q = nq) {
960 i = (ip->ip_off + ip->ip_len) -
961 GETIP(q)->ip_off;
962 if (i < GETIP(q)->ip_len) {
963 GETIP(q)->ip_len -= i;
964 GETIP(q)->ip_off += i;
965 m_adj(q, i);
966 q->m_pkthdr.csum_flags = 0;
967 break;
968 }
969 nq = q->m_nextpkt;
970 m->m_nextpkt = nq;
971 m_freem(q);
972 }
973
974inserted:
975
976#ifdef IPDIVERT
977 /*
978 * Transfer firewall instructions to the fragment structure.
979 * Any fragment diverting causes the whole packet to divert.
980 */
981 fp->ipq_div_info = *divinfo;
982 fp->ipq_div_cookie = *divcookie;
983 *divinfo = 0;
984 *divcookie = 0;
985#endif
986
987 /*
988 * Check for complete reassembly.
989 */
990 next = 0;
991 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
992 if (GETIP(q)->ip_off != next)
993 return (0);
994 next += GETIP(q)->ip_len;
995 }
996 /* Make sure the last packet didn't have the IP_MF flag */
997 if (p->m_flags & M_FRAG)
998 return (0);
999
1000 /*
1001 * Reassembly is complete. Make sure the packet is a sane size.
1002 */
1003 q = fp->ipq_frags;
1004 ip = GETIP(q);
1005 if (next + (IP_VHL_HL(ip->ip_vhl) << 2) > IP_MAXPACKET) {
1006 ipstat.ips_toolong++;
1007 ip_freef(head, fp);
1008 return (0);
1009 }
1010
1011 /*
1012 * Concatenate fragments.
1013 */
1014 m = q;
1015 t = m->m_next;
1016 m->m_next = 0;
1017 m_cat(m, t);
1018 nq = q->m_nextpkt;
1019 q->m_nextpkt = 0;
1020 for (q = nq; q != NULL; q = nq) {
1021 nq = q->m_nextpkt;
1022 q->m_nextpkt = NULL;
1023 m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
1024 m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
1025 m_cat(m, q);
1026 }
1027
1028#ifdef IPDIVERT
1029 /*
1030 * Extract firewall instructions from the fragment structure.
1031 */
1032 *divinfo = fp->ipq_div_info;
1033 *divcookie = fp->ipq_div_cookie;
1034#endif
1035
1036 /*
1037 * Create header for new ip packet by
1038 * modifying header of first packet;
1039 * dequeue and discard fragment reassembly header.
1040 * Make header visible.
1041 */
1042 ip->ip_len = next;
1043 ip->ip_src = fp->ipq_src;
1044 ip->ip_dst = fp->ipq_dst;
1045 TAILQ_REMOVE(head, fp, ipq_list);
1046 nipq--;
1047 (void) m_free(dtom(fp));
1048 ip_nfragpackets--;
1049 m->m_len += (IP_VHL_HL(ip->ip_vhl) << 2);
1050 m->m_data -= (IP_VHL_HL(ip->ip_vhl) << 2);
1051 /* some debugging cruft by sklower, below, will go away soon */
1052 if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
1053 register int plen = 0;
1054 for (t = m; t; t = t->m_next)
1055 plen += t->m_len;
1056 m->m_pkthdr.len = plen;
1057 }
1058 return (m);
1059
1060dropfrag:
1061#ifdef IPDIVERT
1062 *divinfo = 0;
1063 *divcookie = 0;
1064#endif
1065 ipstat.ips_fragdropped++;
1066 m_freem(m);
1067 return (0);
1068
1069#undef GETIP
1070}
1071
1072/*
1073 * Free a fragment reassembly header and all
1074 * associated datagrams.
1075 */
1076static void
1077ip_freef(fhp, fp)
1078 struct ipqhead *fhp;
1079 struct ipq *fp;
1080{
1081 register struct mbuf *q;
1082
1083 while (fp->ipq_frags) {
1084 q = fp->ipq_frags;
1085 fp->ipq_frags = q->m_nextpkt;
1086 m_freem(q);
1087 }
1088 TAILQ_REMOVE(fhp, fp, ipq_list);
1089 (void) m_free(dtom(fp));
1090 ip_nfragpackets--;
1091 nipq--;
1092}
1093
1094/*
1095 * IP timer processing;
1096 * if a timer expires on a reassembly
1097 * queue, discard it.
1098 */
1099void
1100ip_slowtimo()
1101{
1102 register struct ipq *fp;
1103 int s = splnet();
1104 int i;
1105
1106 for (i = 0; i < IPREASS_NHASH; i++) {
1107 for(fp = TAILQ_FIRST(&ipq[i]); fp;) {
1108 struct ipq *fpp;
1109
1110 fpp = fp;
1111 fp = TAILQ_NEXT(fp, ipq_list);
1112 if(--fpp->ipq_ttl == 0) {
1113 ipstat.ips_fragtimeout++;
1114 ip_freef(&ipq[i], fpp);
1115 }
1116 }
1117 }
1118 /*
1119 * If we are over the maximum number of fragments
1120 * (due to the limit being lowered), drain off
1121 * enough to get down to the new limit.
1122 */
1123 for (i = 0; i < IPREASS_NHASH; i++) {
1124 if (ip_maxfragpackets >= 0) {
1125 while (ip_nfragpackets > ip_maxfragpackets &&
1126 !TAILQ_EMPTY(&ipq[i])) {
1127 ipstat.ips_fragdropped++;
1128 ip_freef(&ipq[i], TAILQ_FIRST(&ipq[i]));
1129 }
1130 }
1131 }
1132 ipflow_slowtimo();
1133 splx(s);
1134}
1135
1136/*
1137 * Drain off all datagram fragments.
1138 */
1139void
1140ip_drain()
1141{
1142 int i;
1143
1144 for (i = 0; i < IPREASS_NHASH; i++) {
1145 while(!TAILQ_EMPTY(&ipq[i])) {
1146 ipstat.ips_fragdropped++;
1147 ip_freef(&ipq[i], TAILQ_FIRST(&ipq[i]));
1148 }
1149 }
1150 in_rtqdrain();
1151}
1152
1153/*
1154 * Do option processing on a datagram,
1155 * possibly discarding it if bad options are encountered,
1156 * or forwarding it if source-routed.
1157 * Returns 1 if packet has been forwarded/freed,
1158 * 0 if the packet should be processed further.
1159 */
1160static int
1161ip_dooptions(m)
1162 struct mbuf *m;
1163{
1164 register struct ip *ip = mtod(m, struct ip *);
1165 register u_char *cp;
1166 register struct ip_timestamp *ipt;
1167 register struct in_ifaddr *ia;
1168 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
1169 struct in_addr *sin, dst;
1170 n_time ntime;
1171
1172 dst = ip->ip_dst;
1173 cp = (u_char *)(ip + 1);
1174 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1175 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1176 opt = cp[IPOPT_OPTVAL];
1177 if (opt == IPOPT_EOL)
1178 break;
1179 if (opt == IPOPT_NOP)
1180 optlen = 1;
1181 else {
1182 if (cnt < IPOPT_OLEN + sizeof(*cp)) {
1183 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1184 goto bad;
1185 }
1186 optlen = cp[IPOPT_OLEN];
1187 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
1188 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1189 goto bad;
1190 }
1191 }
1192 switch (opt) {
1193
1194 default:
1195 break;
1196
1197 /*
1198 * Source routing with record.
1199 * Find interface with current destination address.
1200 * If none on this machine then drop if strictly routed,
1201 * or do nothing if loosely routed.
1202 * Record interface address and bring up next address
1203 * component. If strictly routed make sure next
1204 * address is on directly accessible net.
1205 */
1206 case IPOPT_LSRR:
1207 case IPOPT_SSRR:
1208 if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1209 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1210 goto bad;
1211 }
1212 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1213 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1214 goto bad;
1215 }
1216 ipaddr.sin_addr = ip->ip_dst;
1217 ia = (struct in_ifaddr *)
1218 ifa_ifwithaddr((struct sockaddr *)&ipaddr);
1219 if (ia == 0) {
1220 if (opt == IPOPT_SSRR) {
1221 type = ICMP_UNREACH;
1222 code = ICMP_UNREACH_SRCFAIL;
1223 goto bad;
1224 }
1225 if (!ip_dosourceroute)
1226 goto nosourcerouting;
1227 /*
1228 * Loose routing, and not at next destination
1229 * yet; nothing to do except forward.
1230 */
1231 break;
1232 }
1233 off--; /* 0 origin */
1234 if (off > optlen - (int)sizeof(struct in_addr)) {
1235 /*
1236 * End of source route. Should be for us.
1237 */
1238 if (!ip_acceptsourceroute)
1239 goto nosourcerouting;
1240 save_rte(cp, ip->ip_src);
1241 break;
1242 }
1243
1244 if (!ip_dosourceroute) {
1245 if (ipforwarding) {
1246 char buf[16]; /* aaa.bbb.ccc.ddd\0 */
1247 /*
1248 * Acting as a router, so generate ICMP
1249 */
1250nosourcerouting:
1251 strcpy(buf, inet_ntoa(ip->ip_dst));
1252 log(LOG_WARNING,
1253 "attempted source route from %s to %s\n",
1254 inet_ntoa(ip->ip_src), buf);
1255 type = ICMP_UNREACH;
1256 code = ICMP_UNREACH_SRCFAIL;
1257 goto bad;
1258 } else {
1259 /*
1260 * Not acting as a router, so silently drop.
1261 */
1262 ipstat.ips_cantforward++;
1263 m_freem(m);
1264 return (1);
1265 }
1266 }
1267
1268 /*
1269 * locate outgoing interface
1270 */
1271 (void)memcpy(&ipaddr.sin_addr, cp + off,
1272 sizeof(ipaddr.sin_addr));
1273
1274 if (opt == IPOPT_SSRR) {
1275#define INA struct in_ifaddr *
1276#define SA struct sockaddr *
1277 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
1278 ia = (INA)ifa_ifwithnet((SA)&ipaddr);
1279 } else
1280 ia = ip_rtaddr(ipaddr.sin_addr);
1281 if (ia == 0) {
1282 type = ICMP_UNREACH;
1283 code = ICMP_UNREACH_SRCFAIL;
1284 goto bad;
1285 }
1286 ip->ip_dst = ipaddr.sin_addr;
1287 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
1288 sizeof(struct in_addr));
1289 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1290 /*
1291 * Let ip_intr's mcast routing check handle mcast pkts
1292 */
1293 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
1294 break;
1295
1296 case IPOPT_RR:
1297 if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1298 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1299 goto bad;
1300 }
1301 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1302 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1303 goto bad;
1304 }
1305 /*
1306 * If no space remains, ignore.
1307 */
1308 off--; /* 0 origin */
1309 if (off > optlen - (int)sizeof(struct in_addr))
1310 break;
1311 (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst,
1312 sizeof(ipaddr.sin_addr));
1313 /*
1314 * locate outgoing interface; if we're the destination,
1315 * use the incoming interface (should be same).
1316 */
1317 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
1318 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
1319 type = ICMP_UNREACH;
1320 code = ICMP_UNREACH_HOST;
1321 goto bad;
1322 }
1323 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
1324 sizeof(struct in_addr));
1325 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1326 break;
1327
1328 case IPOPT_TS:
1329 code = cp - (u_char *)ip;
1330 ipt = (struct ip_timestamp *)cp;
1331 if (ipt->ipt_len < 4 || ipt->ipt_len > 40) {
1332 code = (u_char *)&ipt->ipt_len - (u_char *)ip;
1333 goto bad;
1334 }
1335 if (ipt->ipt_ptr < 5) {
1336 code = (u_char *)&ipt->ipt_ptr - (u_char *)ip;
1337 goto bad;
1338 }
1339 if (ipt->ipt_ptr >
1340 ipt->ipt_len - (int)sizeof(int32_t)) {
1341 if (++ipt->ipt_oflw == 0) {
1342 code = (u_char *)&ipt->ipt_ptr -
1343 (u_char *)ip;
1344 goto bad;
1345 }
1346 break;
1347 }
1348 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
1349 switch (ipt->ipt_flg) {
1350
1351 case IPOPT_TS_TSONLY:
1352 break;
1353
1354 case IPOPT_TS_TSANDADDR:
1355 if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1356 sizeof(struct in_addr) > ipt->ipt_len) {
1357 code = (u_char *)&ipt->ipt_ptr -
1358 (u_char *)ip;
1359 goto bad;
1360 }
1361 ipaddr.sin_addr = dst;
1362 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
1363 m->m_pkthdr.rcvif);
1364 if (ia == 0)
1365 continue;
1366 (void)memcpy(sin, &IA_SIN(ia)->sin_addr,
1367 sizeof(struct in_addr));
1368 ipt->ipt_ptr += sizeof(struct in_addr);
1369 break;
1370
1371 case IPOPT_TS_PRESPEC:
1372 if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1373 sizeof(struct in_addr) > ipt->ipt_len) {
1374 code = (u_char *)&ipt->ipt_ptr -
1375 (u_char *)ip;
1376 goto bad;
1377 }
1378 (void)memcpy(&ipaddr.sin_addr, sin,
1379 sizeof(struct in_addr));
1380 if (ifa_ifwithaddr((SA)&ipaddr) == 0)
1381 continue;
1382 ipt->ipt_ptr += sizeof(struct in_addr);
1383 break;
1384
1385 default:
1386 /* XXX can't take &ipt->ipt_flg */
1387 code = (u_char *)&ipt->ipt_ptr -
1388 (u_char *)ip + 1;
1389 goto bad;
1390 }
1391 ntime = iptime();
1392 (void)memcpy(cp + ipt->ipt_ptr - 1, &ntime,
1393 sizeof(n_time));
1394 ipt->ipt_ptr += sizeof(n_time);
1395 }
1396 }
1397 if (forward && ipforwarding) {
1398 ip_forward(m, 1);
1399 return (1);
1400 }
1401 return (0);
1402bad:
1403 icmp_error(m, type, code, 0, 0);
1404 ipstat.ips_badoptions++;
1405 return (1);
1406}
1407
1408/*
1409 * Given address of next destination (final or next hop),
1410 * return internet address info of interface to be used to get there.
1411 */
1412static struct in_ifaddr *
1413ip_rtaddr(dst)
1414 struct in_addr dst;
1415{
1416 register struct sockaddr_in *sin;
1417
1418 sin = (struct sockaddr_in *) &ipforward_rt.ro_dst;
1419
1420 if (ipforward_rt.ro_rt == 0 ||
1421 !(ipforward_rt.ro_rt->rt_flags & RTF_UP) ||
1422 dst.s_addr != sin->sin_addr.s_addr) {
1423 if (ipforward_rt.ro_rt) {
1424 RTFREE(ipforward_rt.ro_rt);
1425 ipforward_rt.ro_rt = 0;
1426 }
1427 sin->sin_family = AF_INET;
1428 sin->sin_len = sizeof(*sin);
1429 sin->sin_addr = dst;
1430
1431 rtalloc_ign(&ipforward_rt, RTF_PRCLONING);
1432 }
1433 if (ipforward_rt.ro_rt == 0)
1434 return ((struct in_ifaddr *)0);
1435 return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa);
1436}
1437
1438/*
1439 * Save incoming source route for use in replies,
1440 * to be picked up later by ip_srcroute if the receiver is interested.
1441 */
1442void
1443save_rte(option, dst)
1444 u_char *option;
1445 struct in_addr dst;
1446{
1447 unsigned olen;
1448
1449 olen = option[IPOPT_OLEN];
1450#ifdef DIAGNOSTIC
1451 if (ipprintfs)
1452 printf("save_rte: olen %d\n", olen);
1453#endif
1454 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
1455 return;
1456 bcopy(option, ip_srcrt.srcopt, olen);
1457 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1458 ip_srcrt.dst = dst;
1459}
1460
1461/*
1462 * Retrieve incoming source route for use in replies,
1463 * in the same form used by setsockopt.
1464 * The first hop is placed before the options, will be removed later.
1465 */
1466struct mbuf *
1467ip_srcroute()
1468{
1469 register struct in_addr *p, *q;
1470 register struct mbuf *m;
1471
1472 if (ip_nhops == 0)
1473 return ((struct mbuf *)0);
1474 m = m_get(M_DONTWAIT, MT_HEADER);
1475 if (m == 0)
1476 return ((struct mbuf *)0);
1477
1478#define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1479
1480 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1481 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
1482 OPTSIZ;
1483#ifdef DIAGNOSTIC
1484 if (ipprintfs)
1485 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
1486#endif
1487
1488 /*
1489 * First save first hop for return route
1490 */
1491 p = &ip_srcrt.route[ip_nhops - 1];
1492 *(mtod(m, struct in_addr *)) = *p--;
1493#ifdef DIAGNOSTIC
1494 if (ipprintfs)
1495 printf(" hops %lx", (u_long)ntohl(mtod(m, struct in_addr *)->s_addr));
1496#endif
1497
1498 /*
1499 * Copy option fields and padding (nop) to mbuf.
1500 */
1501 ip_srcrt.nop = IPOPT_NOP;
1502 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
1503 (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr),
1504 &ip_srcrt.nop, OPTSIZ);
1505 q = (struct in_addr *)(mtod(m, caddr_t) +
1506 sizeof(struct in_addr) + OPTSIZ);
1507#undef OPTSIZ
1508 /*
1509 * Record return path as an IP source route,
1510 * reversing the path (pointers are now aligned).
1511 */
1512 while (p >= ip_srcrt.route) {
1513#ifdef DIAGNOSTIC
1514 if (ipprintfs)
1515 printf(" %lx", (u_long)ntohl(q->s_addr));
1516#endif
1517 *q++ = *p--;
1518 }
1519 /*
1520 * Last hop goes to final destination.
1521 */
1522 *q = ip_srcrt.dst;
1523#ifdef DIAGNOSTIC
1524 if (ipprintfs)
1525 printf(" %lx\n", (u_long)ntohl(q->s_addr));
1526#endif
1527 return (m);
1528}
1529
1530/*
1531 * Strip out IP options, at higher
1532 * level protocol in the kernel.
1533 * Second argument is buffer to which options
1534 * will be moved, and return value is their length.
1535 * XXX should be deleted; last arg currently ignored.
1536 */
1537void
1538ip_stripoptions(m, mopt)
1539 register struct mbuf *m;
1540 struct mbuf *mopt;
1541{
1542 register int i;
1543 struct ip *ip = mtod(m, struct ip *);
1544 register caddr_t opts;
1545 int olen;
1546
1547 olen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1548 opts = (caddr_t)(ip + 1);
1549 i = m->m_len - (sizeof (struct ip) + olen);
1550 bcopy(opts + olen, opts, (unsigned)i);
1551 m->m_len -= olen;
1552 if (m->m_flags & M_PKTHDR)
1553 m->m_pkthdr.len -= olen;
1554 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2);
1555}
1556
1557u_char inetctlerrmap[PRC_NCMDS] = {
1558 0, 0, 0, 0,
1559 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1560 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1561 EMSGSIZE, EHOSTUNREACH, 0, 0,
1562 0, 0, 0, 0,
1563 ENOPROTOOPT, ECONNREFUSED
1564};
1565
1566/*
1567 * Forward a packet. If some error occurs return the sender
1568 * an icmp packet. Note we can't always generate a meaningful
1569 * icmp message because icmp doesn't have a large enough repertoire
1570 * of codes and types.
1571 *
1572 * If not forwarding, just drop the packet. This could be confusing
1573 * if ipforwarding was zero but some routing protocol was advancing
1574 * us as a gateway to somewhere. However, we must let the routing
1575 * protocol deal with that.
1576 *
1577 * The srcrt parameter indicates whether the packet is being forwarded
1578 * via a source route.
1579 */
1580static void
1581ip_forward(m, srcrt)
1582 struct mbuf *m;
1583 int srcrt;
1584{
1585 register struct ip *ip = mtod(m, struct ip *);
1586 register struct rtentry *rt;
1587 int error, type = 0, code = 0;
1588 struct mbuf *mcopy;
1589 n_long dest;
1590 struct ifnet *destifp;
1591#ifdef IPSEC
1592 struct ifnet dummyifp;
1593#endif
1594
1595 dest = 0;
1596#ifdef DIAGNOSTIC
1597 if (ipprintfs)
1598 printf("forward: src %lx dst %lx ttl %x\n",
1599 (u_long)ip->ip_src.s_addr, (u_long)ip->ip_dst.s_addr,
1600 ip->ip_ttl);
1601#endif
1602
1603
1604 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1605 ipstat.ips_cantforward++;
1606 m_freem(m);
1607 return;
1608 }
1609#ifdef IPSTEALTH
1610 if (!ipstealth) {
1611#endif
1612 if (ip->ip_ttl <= IPTTLDEC) {
1613 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
1614 dest, 0);
1615 return;
1616 }
1617#ifdef IPSTEALTH
1618 }
1619#endif
1620
1621 if (ip_rtaddr(ip->ip_dst) == 0) {
1622 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
1623 return;
1624 } else
1625 rt = ipforward_rt.ro_rt;
1626
1627 /*
1628 * Save the IP header and at most 8 bytes of the payload,
1629 * in case we need to generate an ICMP message to the src.
1630 *
1631 * We don't use m_copy() because it might return a reference
1632 * to a shared cluster. Both this function and ip_output()
1633 * assume exclusive access to the IP header in `m', so any
1634 * data in a cluster may change before we reach icmp_error().
1635 */
1636 MGET(mcopy, M_DONTWAIT, m->m_type);
1637 if (mcopy != NULL) {
1638 M_COPY_PKTHDR(mcopy, m);
1639 mcopy->m_len = imin((IP_VHL_HL(ip->ip_vhl) << 2) + 8,
1640 (int)ip->ip_len);
1641 m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1642 }
1643
1644#ifdef IPSTEALTH
1645 if (!ipstealth) {
1646#endif
1647 ip->ip_ttl -= IPTTLDEC;
1648#ifdef IPSTEALTH
1649 }
1650#endif
1651
1652 /*
1653 * If forwarding packet using same interface that it came in on,
1654 * perhaps should send a redirect to sender to shortcut a hop.
1655 * Only send redirect if source is sending directly to us,
1656 * and if packet was not source routed (or has any options).
1657 * Also, don't send redirect if forwarding using a default route
1658 * or a route modified by a redirect.
1659 */
1660#define satosin(sa) ((struct sockaddr_in *)(sa))
1661 if (rt->rt_ifp == m->m_pkthdr.rcvif &&
1662 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1663 satosin(rt_key(rt))->sin_addr.s_addr != 0 &&
1664 ipsendredirects && !srcrt) {
1665#define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa))
1666 u_long src = ntohl(ip->ip_src.s_addr);
1667
1668 if (RTA(rt) &&
1669 (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1670 if (rt->rt_flags & RTF_GATEWAY)
1671 dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1672 else
1673 dest = ip->ip_dst.s_addr;
1674 /* Router requirements says to only send host redirects */
1675 type = ICMP_REDIRECT;
1676 code = ICMP_REDIRECT_HOST;
1677#ifdef DIAGNOSTIC
1678 if (ipprintfs)
1679 printf("redirect (%d) to %lx\n", code, (u_long)dest);
1680#endif
1681 }
1682 }
1683
1684 error = ip_output(m, (struct mbuf *)0, &ipforward_rt,
1685 IP_FORWARDING, 0);
1686 if (error)
1687 ipstat.ips_cantforward++;
1688 else {
1689 ipstat.ips_forward++;
1690 if (type)
1691 ipstat.ips_redirectsent++;
1692 else {
1693 if (mcopy) {
1694 ipflow_create(&ipforward_rt, mcopy);
1695 m_freem(mcopy);
1696 }
1697 return;
1698 }
1699 }
1700 if (mcopy == NULL)
1701 return;
1702 destifp = NULL;
1703
1704 switch (error) {
1705
1706 case 0: /* forwarded, but need redirect */
1707 /* type, code set above */
1708 break;
1709
1710 case ENETUNREACH: /* shouldn't happen, checked above */
1711 case EHOSTUNREACH:
1712 case ENETDOWN:
1713 case EHOSTDOWN:
1714 default:
1715 type = ICMP_UNREACH;
1716 code = ICMP_UNREACH_HOST;
1717 break;
1718
1719 case EMSGSIZE:
1720 type = ICMP_UNREACH;
1721 code = ICMP_UNREACH_NEEDFRAG;
1722#ifndef IPSEC
1723 if (ipforward_rt.ro_rt)
1724 destifp = ipforward_rt.ro_rt->rt_ifp;
1725#else
1726 /*
1727 * If the packet is routed over IPsec tunnel, tell the
1728 * originator the tunnel MTU.
1729 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
1730 * XXX quickhack!!!
1731 */
1732 if (ipforward_rt.ro_rt) {
1733 struct secpolicy *sp = NULL;
1734 int ipsecerror;
1735 int ipsechdr;
1736 struct route *ro;
1737
1738 sp = ipsec4_getpolicybyaddr(mcopy,
1739 IPSEC_DIR_OUTBOUND,
1740 IP_FORWARDING,
1741 &ipsecerror);
1742
1743 if (sp == NULL)
1744 destifp = ipforward_rt.ro_rt->rt_ifp;
1745 else {
1746 /* count IPsec header size */
1747 ipsechdr = ipsec4_hdrsiz(mcopy,
1748 IPSEC_DIR_OUTBOUND,
1749 NULL);
1750
1751 /*
1752 * find the correct route for outer IPv4
1753 * header, compute tunnel MTU.
1754 *
1755 * XXX BUG ALERT
1756 * The "dummyifp" code relies upon the fact
1757 * that icmp_error() touches only ifp->if_mtu.
1758 */
1759 /*XXX*/
1760 destifp = NULL;
1761 if (sp->req != NULL
1762 && sp->req->sav != NULL
1763 && sp->req->sav->sah != NULL) {
1764 ro = &sp->req->sav->sah->sa_route;
1765 if (ro->ro_rt && ro->ro_rt->rt_ifp) {
1766 dummyifp.if_mtu =
1767 ro->ro_rt->rt_ifp->if_mtu;
1768 dummyifp.if_mtu -= ipsechdr;
1769 destifp = &dummyifp;
1770 }
1771 }
1772
1773 key_freesp(sp);
1774 }
1775 }
1776#endif /*IPSEC*/
1777 ipstat.ips_cantfrag++;
1778 break;
1779
1780 case ENOBUFS:
1781 type = ICMP_SOURCEQUENCH;
1782 code = 0;
1783 break;
1784
1785 case EACCES: /* ipfw denied packet */
1786 m_freem(mcopy);
1787 return;
1788 }
1789 icmp_error(mcopy, type, code, dest, destifp);
1790}
1791
1792void
1793ip_savecontrol(inp, mp, ip, m)
1794 register struct inpcb *inp;
1795 register struct mbuf **mp;
1796 register struct ip *ip;
1797 register struct mbuf *m;
1798{
1799 if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1800 struct timeval tv;
1801
1802 microtime(&tv);
1803 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1804 SCM_TIMESTAMP, SOL_SOCKET);
1805 if (*mp)
1806 mp = &(*mp)->m_next;
1807 }
1808 if (inp->inp_flags & INP_RECVDSTADDR) {
1809 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
1810 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1811 if (*mp)
1812 mp = &(*mp)->m_next;
1813 }
1814#ifdef notyet
1815 /* XXX
1816 * Moving these out of udp_input() made them even more broken
1817 * than they already were.
1818 */
1819 /* options were tossed already */
1820 if (inp->inp_flags & INP_RECVOPTS) {
1821 *mp = sbcreatecontrol((caddr_t) opts_deleted_above,
1822 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
1823 if (*mp)
1824 mp = &(*mp)->m_next;
1825 }
1826 /* ip_srcroute doesn't do what we want here, need to fix */
1827 if (inp->inp_flags & INP_RECVRETOPTS) {
1828 *mp = sbcreatecontrol((caddr_t) ip_srcroute(),
1829 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
1830 if (*mp)
1831 mp = &(*mp)->m_next;
1832 }
1833#endif
1834 if (inp->inp_flags & INP_RECVIF) {
1835 struct ifnet *ifp;
1836 struct sdlbuf {
1837 struct sockaddr_dl sdl;
1838 u_char pad[32];
1839 } sdlbuf;
1840 struct sockaddr_dl *sdp;
1841 struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
1842
1843 if (((ifp = m->m_pkthdr.rcvif))
1844 && ( ifp->if_index && (ifp->if_index <= if_index))) {
1845 sdp = (struct sockaddr_dl *)
1846 (ifaddr_byindex(ifp->if_index)->ifa_addr);
1847 /*
1848 * Change our mind and don't try copy.
1849 */
1850 if ((sdp->sdl_family != AF_LINK)
1851 || (sdp->sdl_len > sizeof(sdlbuf))) {
1852 goto makedummy;
1853 }
1854 bcopy(sdp, sdl2, sdp->sdl_len);
1855 } else {
1856makedummy:
1857 sdl2->sdl_len
1858 = offsetof(struct sockaddr_dl, sdl_data[0]);
1859 sdl2->sdl_family = AF_LINK;
1860 sdl2->sdl_index = 0;
1861 sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1862 }
1863 *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len,
1864 IP_RECVIF, IPPROTO_IP);
1865 if (*mp)
1866 mp = &(*mp)->m_next;
1867 }
1868}
1869
1870int
1871ip_rsvp_init(struct socket *so)
1872{
1873 if (so->so_type != SOCK_RAW ||
1874 so->so_proto->pr_protocol != IPPROTO_RSVP)
1875 return EOPNOTSUPP;
1876
1877 if (ip_rsvpd != NULL)
1878 return EADDRINUSE;
1879
1880 ip_rsvpd = so;
1881 /*
1882 * This may seem silly, but we need to be sure we don't over-increment
1883 * the RSVP counter, in case something slips up.
1884 */
1885 if (!ip_rsvp_on) {
1886 ip_rsvp_on = 1;
1887 rsvp_on++;
1888 }
1889
1890 return 0;
1891}
1892
1893int
1894ip_rsvp_done(void)
1895{
1896 ip_rsvpd = NULL;
1897 /*
1898 * This may seem silly, but we need to be sure we don't over-decrement
1899 * the RSVP counter, in case something slips up.
1900 */
1901 if (ip_rsvp_on) {
1902 ip_rsvp_on = 0;
1903 rsvp_on--;
1904 }
1905 return 0;
1906}