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