2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
39 #include "opt_bootp.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"
49 #include <sys/param.h>
50 #include <sys/systm.h>
52 #include <sys/malloc.h>
53 #include <sys/domain.h>
54 #include <sys/protosw.h>
55 #include <sys/socket.h>
57 #include <sys/kernel.h>
58 #include <sys/syslog.h>
59 #include <sys/sysctl.h>
63 #include <net/if_types.h>
64 #include <net/if_var.h>
65 #include <net/if_dl.h>
66 #include <net/route.h>
67 #include <net/netisr.h>
68 #include <net/intrq.h>
70 #include <netinet/in.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/in_var.h>
73 #include <netinet/ip.h>
74 #include <netinet/in_pcb.h>
75 #include <netinet/ip_var.h>
76 #include <netinet/ip_icmp.h>
77 #include <machine/in_cksum.h>
79 #include <sys/socketvar.h>
81 #include <netinet/ip_fw.h>
82 #include <netinet/ip_dummynet.h>
85 #include <netinet6/ipsec.h>
86 #include <netkey/key.h>
90 static int ip_rsvp_on;
91 struct socket *ip_rsvpd;
94 SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW,
95 &ipforwarding, 0, "Enable IP forwarding between interfaces");
97 static int ipsendredirects = 1; /* XXX */
98 SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW,
99 &ipsendredirects, 0, "Enable sending IP redirects");
101 int ip_defttl = IPDEFTTL;
102 SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW,
103 &ip_defttl, 0, "Maximum TTL on IP packets");
105 static int ip_dosourceroute = 0;
106 SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW,
107 &ip_dosourceroute, 0, "Enable forwarding source routed IP packets");
109 static int ip_acceptsourceroute = 0;
110 SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute,
111 CTLFLAG_RW, &ip_acceptsourceroute, 0,
112 "Enable accepting source routed IP packets");
114 static int ip_keepfaith = 0;
115 SYSCTL_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW,
117 "Enable packet capture for FAITH IPv4->IPv6 translater daemon");
119 static int ip_nfragpackets = 0;
120 static int ip_maxfragpackets; /* initialized in ip_init() */
121 SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragpackets, CTLFLAG_RW,
122 &ip_maxfragpackets, 0,
123 "Maximum number of IPv4 fragment reassembly queue entries");
126 * XXX - Setting ip_checkinterface mostly implements the receive side of
127 * the Strong ES model described in RFC 1122, but since the routing table
128 * and transmit implementation do not implement the Strong ES model,
129 * setting this to 1 results in an odd hybrid.
131 * XXX - ip_checkinterface currently must be disabled if you use ipnat
132 * to translate the destination address to another local interface.
134 * XXX - ip_checkinterface must be disabled if you add IP aliases
135 * to the loopback interface instead of the interface where the
136 * packets for those addresses are received.
138 static int ip_checkinterface = 1;
139 SYSCTL_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW,
140 &ip_checkinterface, 0, "Verify packet arrives on correct interface");
143 static int ipprintfs = 0;
146 static int ipqmaxlen = IFQ_MAXLEN;
148 extern struct domain inetdomain;
149 extern struct protosw inetsw[];
150 u_char ip_protox[IPPROTO_MAX];
151 struct in_ifaddrhead in_ifaddrhead; /* first inet address */
152 struct in_ifaddrhashhead *in_ifaddrhashtbl; /* inet addr hash table */
153 u_long in_ifaddrhmask; /* mask for hash table */
155 SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RW,
156 &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue");
157 SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD,
158 &ipintrq.ifq_drops, 0, "Number of packets dropped from the IP input queue");
160 struct ipstat ipstat;
161 SYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RW,
162 &ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)");
164 /* Packet reassembly stuff */
165 #define IPREASS_NHASH_LOG2 6
166 #define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2)
167 #define IPREASS_HMASK (IPREASS_NHASH - 1)
168 #define IPREASS_HASH(x,y) \
169 (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
171 static TAILQ_HEAD(ipqhead, ipq) ipq[IPREASS_NHASH];
172 static int nipq = 0; /* total # of reass queues */
176 SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
177 &ip_mtu, 0, "Default MTU");
181 static int ipstealth = 0;
182 SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW,
188 ip_fw_chk_t *ip_fw_chk_ptr;
192 ip_dn_io_t *ip_dn_io_ptr;
196 * We need to save the IP options in case a protocol wants to respond
197 * to an incoming packet over the same route if the packet got here
198 * using IP source routing. This allows connection establishment and
199 * maintenance when the remote end is on a network that is not known
202 static int ip_nhops = 0;
203 static struct ip_srcrt {
204 struct in_addr dst; /* final destination */
205 char nop; /* one NOP to align */
206 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */
207 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
210 struct sockaddr_in *ip_fw_fwd_addr;
212 static void save_rte(u_char *, struct in_addr);
213 static int ip_dooptions(struct mbuf *, int);
214 static void ip_forward(struct mbuf *, int);
215 static void ip_freef(struct ipqhead *, struct ipq *);
217 static struct mbuf *ip_reass(struct mbuf *, struct ipqhead *, struct ipq *, u_int32_t *, u_int16_t *);
219 static struct mbuf *ip_reass(struct mbuf *, struct ipqhead *, struct ipq *);
221 static void ipintr(void);
224 * IP initialization: fill in IP protocol switch table.
225 * All protocols not implemented in kernel go to raw IP protocol handler.
230 register struct protosw *pr;
233 TAILQ_INIT(&in_ifaddrhead);
234 in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &in_ifaddrhmask);
235 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
238 for (i = 0; i < IPPROTO_MAX; i++)
239 ip_protox[i] = pr - inetsw;
240 for (pr = inetdomain.dom_protosw;
241 pr < inetdomain.dom_protoswNPROTOSW; pr++)
242 if (pr->pr_domain->dom_family == PF_INET &&
243 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
244 ip_protox[pr->pr_protocol] = pr - inetsw;
246 for (i = 0; i < IPREASS_NHASH; i++)
249 maxnipq = nmbclusters / 4;
250 ip_maxfragpackets = nmbclusters / 4;
253 ip_id = time_second & 0xffff;
255 ipintrq.ifq_maxlen = ipqmaxlen;
256 mtx_init(&ipintrq.ifq_mtx, "ip_inq", NULL, MTX_DEF);
259 register_netisr(NETISR_IP, ipintr);
262 static struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
263 struct route ipforward_rt;
266 * Ip input routine. Checksum and byte swap header. If fragmented
267 * try to reassemble. Process options. Pass to next level.
270 ip_input(struct mbuf *m)
274 struct in_ifaddr *ia = NULL;
276 int i, hlen, checkif;
278 u_int16_t divert_cookie; /* firewall cookie */
279 struct in_addr pkt_dst;
281 u_int32_t divert_info = 0; /* packet divert/tee info */
283 struct ip_fw *rule = NULL;
285 struct packet_filter_hook *pfh;
288 #endif /* PFIL_HOOKS */
291 /* Get and reset firewall cookie */
292 divert_cookie = ip_divert_cookie;
293 ip_divert_cookie = 0;
299 * dummynet packet are prepended a vestigial mbuf with
300 * m_type = MT_DUMMYNET and m_data pointing to the matching
303 if (m->m_type == MT_DUMMYNET) {
304 rule = (struct ip_fw *)(m->m_data) ;
306 ip = mtod(m, struct ip *);
307 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
313 if (m == NULL || (m->m_flags & M_PKTHDR) == 0)
314 panic("ip_input no HDR");
318 if (m->m_pkthdr.len < sizeof(struct ip))
321 if (m->m_len < sizeof (struct ip) &&
322 (m = m_pullup(m, sizeof (struct ip))) == 0) {
323 ipstat.ips_toosmall++;
326 ip = mtod(m, struct ip *);
328 if (IP_VHL_V(ip->ip_vhl) != IPVERSION) {
329 ipstat.ips_badvers++;
333 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
334 if (hlen < sizeof(struct ip)) { /* minimum header length */
335 ipstat.ips_badhlen++;
338 if (hlen > m->m_len) {
339 if ((m = m_pullup(m, hlen)) == 0) {
340 ipstat.ips_badhlen++;
343 ip = mtod(m, struct ip *);
346 /* 127/8 must not appear on wire - RFC1122 */
347 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
348 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
349 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
350 ipstat.ips_badaddr++;
355 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
356 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
358 if (hlen == sizeof(struct ip)) {
359 sum = in_cksum_hdr(ip);
361 sum = in_cksum(m, hlen);
370 * Convert fields to host representation.
372 ip->ip_len = ntohs(ip->ip_len);
373 if (ip->ip_len < hlen) {
377 ip->ip_off = ntohs(ip->ip_off);
380 * Check that the amount of data in the buffers
381 * is as at least much as the IP header would have us expect.
382 * Trim mbufs if longer than we expect.
383 * Drop packet if shorter than we expect.
385 if (m->m_pkthdr.len < ip->ip_len) {
387 ipstat.ips_tooshort++;
390 if (m->m_pkthdr.len > ip->ip_len) {
391 if (m->m_len == m->m_pkthdr.len) {
392 m->m_len = ip->ip_len;
393 m->m_pkthdr.len = ip->ip_len;
395 m_adj(m, ip->ip_len - m->m_pkthdr.len);
399 if (ipsec_gethist(m, NULL))
405 * Right now when no processing on packet has done
406 * and it is still fresh out of network we do our black
408 * - Firewall: deny/allow/divert
409 * - Xlate: translate packet's addr/port (NAT).
410 * - Pipe: pass pkt through dummynet.
411 * - Wrap: fake packet's addr/port <unimpl.>
412 * - Encapsulate: put it in another IP and send out. <unimp.>
419 * Run through list of hooks for input packets. If there are any
420 * filters which require that additional packets in the flow are
421 * not fast-forwarded, they must clear the M_CANFASTFWD flag.
422 * Note that filters must _never_ set this flag, as another filter
423 * in the list may have previously cleared it.
426 pfh = pfil_hook_get(PFIL_IN, &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
427 for (; pfh; pfh = TAILQ_NEXT(pfh, pfil_link))
428 if (pfh->pfil_func) {
429 rv = pfh->pfil_func(ip, hlen,
430 m->m_pkthdr.rcvif, 0, &m0);
436 ip = mtod(m, struct ip *);
438 #endif /* PFIL_HOOKS */
440 if (fw_enable && IPFW_LOADED) {
441 #ifdef IPFIREWALL_FORWARD
443 * If we've been forwarded from the output side, then
444 * skip the firewall a second time
448 #endif /* IPFIREWALL_FORWARD */
450 * See the comment in ip_output for the return values
451 * produced by the firewall.
453 i = ip_fw_chk_ptr(&m, NULL /* oif */, &divert_cookie,
454 &rule, &ip_fw_fwd_addr);
455 if ( (i & IP_FW_PORT_DENY_FLAG) || m == NULL) { /* drop */
460 ip = mtod(m, struct ip *); /* just in case m changed */
461 if (i == 0 && ip_fw_fwd_addr == NULL) /* common case */
463 if (DUMMYNET_LOADED && (i & IP_FW_PORT_DYNT_FLAG) != 0) {
464 /* Send packet to the appropriate pipe */
465 ip_dn_io_ptr(i&0xffff,DN_TO_IP_IN,m,NULL,NULL,0, rule,
470 if (i != 0 && (i & IP_FW_PORT_DYNT_FLAG) == 0) {
471 /* Divert or tee packet */
476 #ifdef IPFIREWALL_FORWARD
477 if (i == 0 && ip_fw_fwd_addr != NULL)
481 * if we get here, the packet must be dropped
489 * Process options and, if not destined for us,
490 * ship it on. ip_dooptions returns 1 when an
491 * error was detected (causing an icmp message
492 * to be sent and the original packet to be freed).
494 ip_nhops = 0; /* for source routed packets */
495 if (hlen > sizeof (struct ip) && ip_dooptions(m, 0)) {
496 #ifdef IPFIREWALL_FORWARD
497 ip_fw_fwd_addr = NULL;
502 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
503 * matter if it is destined to another node, or whether it is
504 * a multicast one, RSVP wants it! and prevents it from being forwarded
505 * anywhere else. Also checks if the rsvp daemon is running before
506 * grabbing the packet.
508 if (rsvp_on && ip->ip_p==IPPROTO_RSVP)
512 * Check our list of addresses, to see if the packet is for us.
513 * If we don't have any addresses, assume any unicast packet
514 * we receive might be for us (and let the upper layers deal
517 if (TAILQ_EMPTY(&in_ifaddrhead) &&
518 (m->m_flags & (M_MCAST|M_BCAST)) == 0)
522 * Cache the destination address of the packet; this may be
523 * changed by use of 'ipfw fwd'.
525 pkt_dst = ip_fw_fwd_addr == NULL ?
526 ip->ip_dst : ip_fw_fwd_addr->sin_addr;
529 * Enable a consistency check between the destination address
530 * and the arrival interface for a unicast packet (the RFC 1122
531 * strong ES model) if IP forwarding is disabled and the packet
532 * is not locally generated and the packet is not subject to
535 * XXX - Checking also should be disabled if the destination
536 * address is ipnat'ed to a different interface.
538 * XXX - Checking is incompatible with IP aliases added
539 * to the loopback interface instead of the interface where
540 * the packets are received.
542 checkif = ip_checkinterface && (ipforwarding == 0) &&
543 m->m_pkthdr.rcvif != NULL &&
544 ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) &&
545 (ip_fw_fwd_addr == NULL);
548 * Check for exact addresses in the hash bucket.
550 LIST_FOREACH(ia, INADDR_HASH(pkt_dst.s_addr), ia_hash) {
552 * If the address matches, verify that the packet
553 * arrived via the correct interface if checking is
556 if (IA_SIN(ia)->sin_addr.s_addr == pkt_dst.s_addr &&
557 (!checkif || ia->ia_ifp == m->m_pkthdr.rcvif))
561 * Check for broadcast addresses.
563 * Only accept broadcast packets that arrive via the matching
564 * interface. Reception of forwarded directed broadcasts would
565 * be handled via ip_forward() and ether_output() with the loopback
566 * into the stack for SIMPLEX interfaces handled by ether_output().
568 if (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
569 TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) {
570 if (ifa->ifa_addr->sa_family != AF_INET)
573 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
576 if (ia->ia_netbroadcast.s_addr == pkt_dst.s_addr)
579 if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY)
584 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
585 struct in_multi *inm;
588 * If we are acting as a multicast router, all
589 * incoming multicast packets are passed to the
590 * kernel-level multicast forwarding function.
591 * The packet is returned (relatively) intact; if
592 * ip_mforward() returns a non-zero value, the packet
593 * must be discarded, else it may be accepted below.
595 if (ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) {
596 ipstat.ips_cantforward++;
602 * The process-level routing daemon needs to receive
603 * all multicast IGMP packets, whether or not this
604 * host belongs to their destination groups.
606 if (ip->ip_p == IPPROTO_IGMP)
608 ipstat.ips_forward++;
611 * See if we belong to the destination multicast group on the
614 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
616 ipstat.ips_notmember++;
622 if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
624 if (ip->ip_dst.s_addr == INADDR_ANY)
628 * FAITH(Firewall Aided Internet Translator)
630 if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
632 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP)
640 * Not for us; forward if possible and desirable.
642 if (ipforwarding == 0) {
643 ipstat.ips_cantforward++;
648 * Enforce inbound IPsec SPD.
650 if (ipsec4_in_reject(m, NULL)) {
651 ipsecstat.in_polvio++;
657 #ifdef IPFIREWALL_FORWARD
658 ip_fw_fwd_addr = NULL;
665 * IPSTEALTH: Process non-routing options only
666 * if the packet is destined for us.
668 if (ipstealth && hlen > sizeof (struct ip) && ip_dooptions(m, 1)) {
669 #ifdef IPFIREWALL_FORWARD
670 ip_fw_fwd_addr = NULL;
674 #endif /* IPSTEALTH */
676 /* Count the packet in the ip address stats */
678 ia->ia_ifa.if_ipackets++;
679 ia->ia_ifa.if_ibytes += m->m_pkthdr.len;
683 * If offset or IP_MF are set, must reassemble.
684 * Otherwise, nothing need be done.
685 * (We could look in the reassembly queue to see
686 * if the packet was previously fragmented,
687 * but it's not worth the time; just let them time out.)
689 if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
691 sum = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
693 * Look for queue of fragments
696 TAILQ_FOREACH(fp, &ipq[sum], ipq_list)
697 if (ip->ip_id == fp->ipq_id &&
698 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
699 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
700 ip->ip_p == fp->ipq_p)
705 /* check if there's a place for the new queue */
706 if (nipq > maxnipq) {
708 * drop something from the tail of the current queue
709 * before proceeding further
711 struct ipq *q = TAILQ_LAST(&ipq[sum], ipqhead);
712 if (q == NULL) { /* gak */
713 for (i = 0; i < IPREASS_NHASH; i++) {
714 struct ipq *r = TAILQ_LAST(&ipq[i], ipqhead);
716 ip_freef(&ipq[i], r);
721 ip_freef(&ipq[sum], q);
725 * Adjust ip_len to not reflect header,
726 * convert offset of this to bytes.
729 if (ip->ip_off & IP_MF) {
731 * Make sure that fragments have a data length
732 * that's a non-zero multiple of 8 bytes.
734 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
735 ipstat.ips_toosmall++; /* XXX */
738 m->m_flags |= M_FRAG;
743 * Attempt reassembly; if it succeeds, proceed.
745 ipstat.ips_fragments++;
746 m->m_pkthdr.header = ip;
749 &ipq[sum], fp, &divert_info, &divert_cookie);
751 m = ip_reass(m, &ipq[sum], fp);
754 #ifdef IPFIREWALL_FORWARD
755 ip_fw_fwd_addr = NULL;
759 ipstat.ips_reassembled++;
760 ip = mtod(m, struct ip *);
761 /* Get the header length of the reassembled packet */
762 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
764 /* Restore original checksum before diverting packet */
765 if (divert_info != 0) {
767 ip->ip_len = htons(ip->ip_len);
768 ip->ip_off = htons(ip->ip_off);
770 if (hlen == sizeof(struct ip))
771 ip->ip_sum = in_cksum_hdr(ip);
773 ip->ip_sum = in_cksum(m, hlen);
774 ip->ip_off = ntohs(ip->ip_off);
775 ip->ip_len = ntohs(ip->ip_len);
784 * Divert or tee packet to the divert protocol if required.
786 * If divert_info is zero then cookie should be too, so we shouldn't
787 * need to clear them here. Assume divert_packet() does so also.
789 if (divert_info != 0) {
790 struct mbuf *clone = NULL;
792 /* Clone packet if we're doing a 'tee' */
793 if ((divert_info & IP_FW_PORT_TEE_FLAG) != 0)
794 clone = m_dup(m, M_DONTWAIT);
796 /* Restore packet header fields to original values */
798 ip->ip_len = htons(ip->ip_len);
799 ip->ip_off = htons(ip->ip_off);
801 /* Deliver packet to divert input routine */
802 ip_divert_cookie = divert_cookie;
803 divert_packet(m, 1, divert_info & 0xffff);
804 ipstat.ips_delivered++;
806 /* If 'tee', continue with original packet */
810 ip = mtod(m, struct ip *);
819 * enforce IPsec policy checking if we are seeing last header.
820 * note that we do not visit this with protocols with pcb layer
821 * code - like udp/tcp/raw ip.
823 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0 &&
824 ipsec4_in_reject(m, NULL)) {
825 ipsecstat.in_polvio++;
831 * Switch out to protocol's input routine.
833 ipstat.ips_delivered++;
837 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, off);
838 #ifdef IPFIREWALL_FORWARD
839 ip_fw_fwd_addr = NULL; /* tcp needed it */
844 #ifdef IPFIREWALL_FORWARD
845 ip_fw_fwd_addr = NULL;
851 * IP software interrupt routine - to go away sometime soon
859 IF_DEQUEUE(&ipintrq, m);
867 * Take incoming datagram fragment and try to reassemble it into
868 * whole datagram. If a chain for reassembly of this datagram already
869 * exists, then it is given as fp; otherwise have to make a chain.
871 * When IPDIVERT enabled, keep additional state with each packet that
872 * tells us if we need to divert or tee the packet we're building.
877 ip_reass(m, head, fp, divinfo, divcookie)
879 ip_reass(m, head, fp)
882 struct ipqhead *head;
886 u_int16_t *divcookie;
889 struct ip *ip = mtod(m, struct ip *);
890 register struct mbuf *p, *q, *nq;
892 int hlen = IP_VHL_HL(ip->ip_vhl) << 2;
896 * Presence of header sizes in mbufs
897 * would confuse code below.
903 * If first fragment to arrive, create a reassembly queue.
907 * Enforce upper bound on number of fragmented packets
908 * for which we attempt reassembly;
909 * If maxfrag is 0, never accept fragments.
910 * If maxfrag is -1, accept all fragments without limitation.
912 if ((ip_maxfragpackets >= 0) && (ip_nfragpackets >= ip_maxfragpackets))
915 if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)
917 fp = mtod(t, struct ipq *);
918 TAILQ_INSERT_HEAD(head, fp, ipq_list);
920 fp->ipq_ttl = IPFRAGTTL;
921 fp->ipq_p = ip->ip_p;
922 fp->ipq_id = ip->ip_id;
923 fp->ipq_src = ip->ip_src;
924 fp->ipq_dst = ip->ip_dst;
928 fp->ipq_div_info = 0;
929 fp->ipq_div_cookie = 0;
934 #define GETIP(m) ((struct ip*)((m)->m_pkthdr.header))
937 * Find a segment which begins after this one does.
939 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
940 if (GETIP(q)->ip_off > ip->ip_off)
944 * If there is a preceding segment, it may provide some of
945 * our data already. If so, drop the data from the incoming
946 * segment. If it provides all of our data, drop us, otherwise
947 * stick new segment in the proper place.
949 * If some of the data is dropped from the the preceding
950 * segment, then it's checksum is invalidated.
953 i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
958 m->m_pkthdr.csum_flags = 0;
962 m->m_nextpkt = p->m_nextpkt;
965 m->m_nextpkt = fp->ipq_frags;
970 * While we overlap succeeding segments trim them or,
971 * if they are completely covered, dequeue them.
973 for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
975 i = (ip->ip_off + ip->ip_len) -
977 if (i < GETIP(q)->ip_len) {
978 GETIP(q)->ip_len -= i;
979 GETIP(q)->ip_off += i;
981 q->m_pkthdr.csum_flags = 0;
993 * Transfer firewall instructions to the fragment structure.
994 * Any fragment diverting causes the whole packet to divert.
996 fp->ipq_div_info = *divinfo;
997 fp->ipq_div_cookie = *divcookie;
1003 * Check for complete reassembly.
1006 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
1007 if (GETIP(q)->ip_off != next)
1009 next += GETIP(q)->ip_len;
1011 /* Make sure the last packet didn't have the IP_MF flag */
1012 if (p->m_flags & M_FRAG)
1016 * Reassembly is complete. Make sure the packet is a sane size.
1020 if (next + (IP_VHL_HL(ip->ip_vhl) << 2) > IP_MAXPACKET) {
1021 ipstat.ips_toolong++;
1027 * Concatenate fragments.
1035 for (q = nq; q != NULL; q = nq) {
1037 q->m_nextpkt = NULL;
1038 m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
1039 m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
1045 * Extract firewall instructions from the fragment structure.
1047 *divinfo = fp->ipq_div_info;
1048 *divcookie = fp->ipq_div_cookie;
1052 * Create header for new ip packet by
1053 * modifying header of first packet;
1054 * dequeue and discard fragment reassembly header.
1055 * Make header visible.
1058 ip->ip_src = fp->ipq_src;
1059 ip->ip_dst = fp->ipq_dst;
1060 TAILQ_REMOVE(head, fp, ipq_list);
1062 (void) m_free(dtom(fp));
1064 m->m_len += (IP_VHL_HL(ip->ip_vhl) << 2);
1065 m->m_data -= (IP_VHL_HL(ip->ip_vhl) << 2);
1066 /* some debugging cruft by sklower, below, will go away soon */
1067 if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
1068 register int plen = 0;
1069 for (t = m; t; t = t->m_next)
1071 m->m_pkthdr.len = plen;
1080 ipstat.ips_fragdropped++;
1088 * Free a fragment reassembly header and all
1089 * associated datagrams.
1093 struct ipqhead *fhp;
1096 register struct mbuf *q;
1098 while (fp->ipq_frags) {
1100 fp->ipq_frags = q->m_nextpkt;
1103 TAILQ_REMOVE(fhp, fp, ipq_list);
1104 (void) m_free(dtom(fp));
1110 * IP timer processing;
1111 * if a timer expires on a reassembly
1112 * queue, discard it.
1117 register struct ipq *fp;
1121 for (i = 0; i < IPREASS_NHASH; i++) {
1122 for(fp = TAILQ_FIRST(&ipq[i]); fp;) {
1126 fp = TAILQ_NEXT(fp, ipq_list);
1127 if(--fpp->ipq_ttl == 0) {
1128 ipstat.ips_fragtimeout++;
1129 ip_freef(&ipq[i], fpp);
1134 * If we are over the maximum number of fragments
1135 * (due to the limit being lowered), drain off
1136 * enough to get down to the new limit.
1138 for (i = 0; i < IPREASS_NHASH; i++) {
1139 if (ip_maxfragpackets >= 0) {
1140 while (ip_nfragpackets > ip_maxfragpackets &&
1141 !TAILQ_EMPTY(&ipq[i])) {
1142 ipstat.ips_fragdropped++;
1143 ip_freef(&ipq[i], TAILQ_FIRST(&ipq[i]));
1152 * Drain off all datagram fragments.
1159 for (i = 0; i < IPREASS_NHASH; i++) {
1160 while(!TAILQ_EMPTY(&ipq[i])) {
1161 ipstat.ips_fragdropped++;
1162 ip_freef(&ipq[i], TAILQ_FIRST(&ipq[i]));
1169 * Do option processing on a datagram,
1170 * possibly discarding it if bad options are encountered,
1171 * or forwarding it if source-routed.
1172 * The pass argument is used when operating in the IPSTEALTH
1173 * mode to tell what options to process:
1174 * [LS]SRR (pass 0) or the others (pass 1).
1175 * The reason for as many as two passes is that when doing IPSTEALTH,
1176 * non-routing options should be processed only if the packet is for us.
1177 * Returns 1 if packet has been forwarded/freed,
1178 * 0 if the packet should be processed further.
1181 ip_dooptions(m, pass)
1185 register struct ip *ip = mtod(m, struct ip *);
1186 register u_char *cp;
1187 register struct in_ifaddr *ia;
1188 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
1189 struct in_addr *sin, dst;
1193 cp = (u_char *)(ip + 1);
1194 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1195 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1196 opt = cp[IPOPT_OPTVAL];
1197 if (opt == IPOPT_EOL)
1199 if (opt == IPOPT_NOP)
1202 if (cnt < IPOPT_OLEN + sizeof(*cp)) {
1203 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1206 optlen = cp[IPOPT_OLEN];
1207 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
1208 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1218 * Source routing with record.
1219 * Find interface with current destination address.
1220 * If none on this machine then drop if strictly routed,
1221 * or do nothing if loosely routed.
1222 * Record interface address and bring up next address
1223 * component. If strictly routed make sure next
1224 * address is on directly accessible net.
1229 if (ipstealth && pass > 0)
1232 if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1233 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1236 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1237 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1240 ipaddr.sin_addr = ip->ip_dst;
1241 ia = (struct in_ifaddr *)
1242 ifa_ifwithaddr((struct sockaddr *)&ipaddr);
1244 if (opt == IPOPT_SSRR) {
1245 type = ICMP_UNREACH;
1246 code = ICMP_UNREACH_SRCFAIL;
1249 if (!ip_dosourceroute)
1250 goto nosourcerouting;
1252 * Loose routing, and not at next destination
1253 * yet; nothing to do except forward.
1257 off--; /* 0 origin */
1258 if (off > optlen - (int)sizeof(struct in_addr)) {
1260 * End of source route. Should be for us.
1262 if (!ip_acceptsourceroute)
1263 goto nosourcerouting;
1264 save_rte(cp, ip->ip_src);
1271 if (!ip_dosourceroute) {
1273 char buf[16]; /* aaa.bbb.ccc.ddd\0 */
1275 * Acting as a router, so generate ICMP
1278 strcpy(buf, inet_ntoa(ip->ip_dst));
1280 "attempted source route from %s to %s\n",
1281 inet_ntoa(ip->ip_src), buf);
1282 type = ICMP_UNREACH;
1283 code = ICMP_UNREACH_SRCFAIL;
1287 * Not acting as a router, so silently drop.
1292 ipstat.ips_cantforward++;
1299 * locate outgoing interface
1301 (void)memcpy(&ipaddr.sin_addr, cp + off,
1302 sizeof(ipaddr.sin_addr));
1304 if (opt == IPOPT_SSRR) {
1305 #define INA struct in_ifaddr *
1306 #define SA struct sockaddr *
1307 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
1308 ia = (INA)ifa_ifwithnet((SA)&ipaddr);
1310 ia = ip_rtaddr(ipaddr.sin_addr, &ipforward_rt);
1312 type = ICMP_UNREACH;
1313 code = ICMP_UNREACH_SRCFAIL;
1316 ip->ip_dst = ipaddr.sin_addr;
1317 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
1318 sizeof(struct in_addr));
1319 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1321 * Let ip_intr's mcast routing check handle mcast pkts
1323 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
1328 if (ipstealth && pass == 0)
1331 if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1332 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1335 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1336 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1340 * If no space remains, ignore.
1342 off--; /* 0 origin */
1343 if (off > optlen - (int)sizeof(struct in_addr))
1345 (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst,
1346 sizeof(ipaddr.sin_addr));
1348 * locate outgoing interface; if we're the destination,
1349 * use the incoming interface (should be same).
1351 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
1352 (ia = ip_rtaddr(ipaddr.sin_addr,
1353 &ipforward_rt)) == 0) {
1354 type = ICMP_UNREACH;
1355 code = ICMP_UNREACH_HOST;
1358 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
1359 sizeof(struct in_addr));
1360 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1365 if (ipstealth && pass == 0)
1368 code = cp - (u_char *)ip;
1369 if (optlen < 4 || optlen > 40) {
1370 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1373 if ((off = cp[IPOPT_OFFSET]) < 5) {
1374 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1377 if (off > optlen - (int)sizeof(int32_t)) {
1378 cp[IPOPT_OFFSET + 1] += (1 << 4);
1379 if ((cp[IPOPT_OFFSET + 1] & 0xf0) == 0) {
1380 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1385 off--; /* 0 origin */
1386 sin = (struct in_addr *)(cp + off);
1387 switch (cp[IPOPT_OFFSET + 1] & 0x0f) {
1389 case IPOPT_TS_TSONLY:
1392 case IPOPT_TS_TSANDADDR:
1393 if (off + sizeof(n_time) +
1394 sizeof(struct in_addr) > optlen) {
1395 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1398 ipaddr.sin_addr = dst;
1399 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
1403 (void)memcpy(sin, &IA_SIN(ia)->sin_addr,
1404 sizeof(struct in_addr));
1405 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1408 case IPOPT_TS_PRESPEC:
1409 if (off + sizeof(n_time) +
1410 sizeof(struct in_addr) > optlen) {
1411 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1414 (void)memcpy(&ipaddr.sin_addr, sin,
1415 sizeof(struct in_addr));
1416 if (ifa_ifwithaddr((SA)&ipaddr) == 0)
1418 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1422 code = &cp[IPOPT_OFFSET + 1] - (u_char *)ip;
1426 (void)memcpy(cp + off, &ntime, sizeof(n_time));
1427 cp[IPOPT_OFFSET] += sizeof(n_time);
1430 if (forward && ipforwarding) {
1436 icmp_error(m, type, code, 0, 0);
1437 ipstat.ips_badoptions++;
1442 * Given address of next destination (final or next hop),
1443 * return internet address info of interface to be used to get there.
1450 register struct sockaddr_in *sin;
1452 sin = (struct sockaddr_in *)&rt->ro_dst;
1454 if (rt->ro_rt == 0 ||
1455 !(rt->ro_rt->rt_flags & RTF_UP) ||
1456 dst.s_addr != sin->sin_addr.s_addr) {
1461 sin->sin_family = AF_INET;
1462 sin->sin_len = sizeof(*sin);
1463 sin->sin_addr = dst;
1465 rtalloc_ign(rt, RTF_PRCLONING);
1468 return ((struct in_ifaddr *)0);
1469 return (ifatoia(rt->ro_rt->rt_ifa));
1473 * Save incoming source route for use in replies,
1474 * to be picked up later by ip_srcroute if the receiver is interested.
1477 save_rte(option, dst)
1483 olen = option[IPOPT_OLEN];
1486 printf("save_rte: olen %d\n", olen);
1488 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
1490 bcopy(option, ip_srcrt.srcopt, olen);
1491 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1496 * Retrieve incoming source route for use in replies,
1497 * in the same form used by setsockopt.
1498 * The first hop is placed before the options, will be removed later.
1503 register struct in_addr *p, *q;
1504 register struct mbuf *m;
1507 return ((struct mbuf *)0);
1508 m = m_get(M_DONTWAIT, MT_HEADER);
1510 return ((struct mbuf *)0);
1512 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1514 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1515 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
1519 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
1523 * First save first hop for return route
1525 p = &ip_srcrt.route[ip_nhops - 1];
1526 *(mtod(m, struct in_addr *)) = *p--;
1529 printf(" hops %lx", (u_long)ntohl(mtod(m, struct in_addr *)->s_addr));
1533 * Copy option fields and padding (nop) to mbuf.
1535 ip_srcrt.nop = IPOPT_NOP;
1536 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
1537 (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr),
1538 &ip_srcrt.nop, OPTSIZ);
1539 q = (struct in_addr *)(mtod(m, caddr_t) +
1540 sizeof(struct in_addr) + OPTSIZ);
1543 * Record return path as an IP source route,
1544 * reversing the path (pointers are now aligned).
1546 while (p >= ip_srcrt.route) {
1549 printf(" %lx", (u_long)ntohl(q->s_addr));
1554 * Last hop goes to final destination.
1559 printf(" %lx\n", (u_long)ntohl(q->s_addr));
1565 * Strip out IP options, at higher
1566 * level protocol in the kernel.
1567 * Second argument is buffer to which options
1568 * will be moved, and return value is their length.
1569 * XXX should be deleted; last arg currently ignored.
1572 ip_stripoptions(m, mopt)
1573 register struct mbuf *m;
1577 struct ip *ip = mtod(m, struct ip *);
1578 register caddr_t opts;
1581 olen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1582 opts = (caddr_t)(ip + 1);
1583 i = m->m_len - (sizeof (struct ip) + olen);
1584 bcopy(opts + olen, opts, (unsigned)i);
1586 if (m->m_flags & M_PKTHDR)
1587 m->m_pkthdr.len -= olen;
1588 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2);
1591 u_char inetctlerrmap[PRC_NCMDS] = {
1593 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1594 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1595 EMSGSIZE, EHOSTUNREACH, 0, 0,
1597 ENOPROTOOPT, ECONNREFUSED
1601 * Forward a packet. If some error occurs return the sender
1602 * an icmp packet. Note we can't always generate a meaningful
1603 * icmp message because icmp doesn't have a large enough repertoire
1604 * of codes and types.
1606 * If not forwarding, just drop the packet. This could be confusing
1607 * if ipforwarding was zero but some routing protocol was advancing
1608 * us as a gateway to somewhere. However, we must let the routing
1609 * protocol deal with that.
1611 * The srcrt parameter indicates whether the packet is being forwarded
1612 * via a source route.
1615 ip_forward(m, srcrt)
1619 register struct ip *ip = mtod(m, struct ip *);
1620 register struct rtentry *rt;
1621 int error, type = 0, code = 0;
1624 struct in_addr pkt_dst;
1625 struct ifnet *destifp;
1627 struct ifnet dummyifp;
1632 * Cache the destination address of the packet; this may be
1633 * changed by use of 'ipfw fwd'.
1635 pkt_dst = ip_fw_fwd_addr == NULL ?
1636 ip->ip_dst : ip_fw_fwd_addr->sin_addr;
1640 printf("forward: src %lx dst %lx ttl %x\n",
1641 (u_long)ip->ip_src.s_addr, (u_long)pkt_dst.s_addr,
1646 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(pkt_dst) == 0) {
1647 ipstat.ips_cantforward++;
1654 if (ip->ip_ttl <= IPTTLDEC) {
1655 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
1663 if (ip_rtaddr(pkt_dst, &ipforward_rt) == 0) {
1664 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
1667 rt = ipforward_rt.ro_rt;
1670 * Save the IP header and at most 8 bytes of the payload,
1671 * in case we need to generate an ICMP message to the src.
1673 * We don't use m_copy() because it might return a reference
1674 * to a shared cluster. Both this function and ip_output()
1675 * assume exclusive access to the IP header in `m', so any
1676 * data in a cluster may change before we reach icmp_error().
1678 MGET(mcopy, M_DONTWAIT, m->m_type);
1679 if (mcopy != NULL) {
1680 M_COPY_PKTHDR(mcopy, m);
1681 mcopy->m_len = imin((IP_VHL_HL(ip->ip_vhl) << 2) + 8,
1683 m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1689 ip->ip_ttl -= IPTTLDEC;
1695 * If forwarding packet using same interface that it came in on,
1696 * perhaps should send a redirect to sender to shortcut a hop.
1697 * Only send redirect if source is sending directly to us,
1698 * and if packet was not source routed (or has any options).
1699 * Also, don't send redirect if forwarding using a default route
1700 * or a route modified by a redirect.
1702 if (rt->rt_ifp == m->m_pkthdr.rcvif &&
1703 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1704 satosin(rt_key(rt))->sin_addr.s_addr != 0 &&
1705 ipsendredirects && !srcrt && !ip_fw_fwd_addr) {
1706 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa))
1707 u_long src = ntohl(ip->ip_src.s_addr);
1710 (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1711 if (rt->rt_flags & RTF_GATEWAY)
1712 dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1714 dest = pkt_dst.s_addr;
1715 /* Router requirements says to only send host redirects */
1716 type = ICMP_REDIRECT;
1717 code = ICMP_REDIRECT_HOST;
1720 printf("redirect (%d) to %lx\n", code, (u_long)dest);
1725 error = ip_output(m, (struct mbuf *)0, &ipforward_rt,
1728 ipstat.ips_cantforward++;
1730 ipstat.ips_forward++;
1732 ipstat.ips_redirectsent++;
1735 ipflow_create(&ipforward_rt, mcopy);
1747 case 0: /* forwarded, but need redirect */
1748 /* type, code set above */
1751 case ENETUNREACH: /* shouldn't happen, checked above */
1756 type = ICMP_UNREACH;
1757 code = ICMP_UNREACH_HOST;
1761 type = ICMP_UNREACH;
1762 code = ICMP_UNREACH_NEEDFRAG;
1764 if (ipforward_rt.ro_rt)
1765 destifp = ipforward_rt.ro_rt->rt_ifp;
1768 * If the packet is routed over IPsec tunnel, tell the
1769 * originator the tunnel MTU.
1770 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
1773 if (ipforward_rt.ro_rt) {
1774 struct secpolicy *sp = NULL;
1779 sp = ipsec4_getpolicybyaddr(mcopy,
1785 destifp = ipforward_rt.ro_rt->rt_ifp;
1787 /* count IPsec header size */
1788 ipsechdr = ipsec4_hdrsiz(mcopy,
1793 * find the correct route for outer IPv4
1794 * header, compute tunnel MTU.
1797 * The "dummyifp" code relies upon the fact
1798 * that icmp_error() touches only ifp->if_mtu.
1803 && sp->req->sav != NULL
1804 && sp->req->sav->sah != NULL) {
1805 ro = &sp->req->sav->sah->sa_route;
1806 if (ro->ro_rt && ro->ro_rt->rt_ifp) {
1808 ro->ro_rt->rt_ifp->if_mtu;
1809 dummyifp.if_mtu -= ipsechdr;
1810 destifp = &dummyifp;
1818 ipstat.ips_cantfrag++;
1822 type = ICMP_SOURCEQUENCH;
1826 case EACCES: /* ipfw denied packet */
1830 icmp_error(mcopy, type, code, dest, destifp);
1834 ip_savecontrol(inp, mp, ip, m)
1835 register struct inpcb *inp;
1836 register struct mbuf **mp;
1837 register struct ip *ip;
1838 register struct mbuf *m;
1840 if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1844 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1845 SCM_TIMESTAMP, SOL_SOCKET);
1847 mp = &(*mp)->m_next;
1849 if (inp->inp_flags & INP_RECVDSTADDR) {
1850 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
1851 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1853 mp = &(*mp)->m_next;
1857 * Moving these out of udp_input() made them even more broken
1858 * than they already were.
1860 /* options were tossed already */
1861 if (inp->inp_flags & INP_RECVOPTS) {
1862 *mp = sbcreatecontrol((caddr_t) opts_deleted_above,
1863 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
1865 mp = &(*mp)->m_next;
1867 /* ip_srcroute doesn't do what we want here, need to fix */
1868 if (inp->inp_flags & INP_RECVRETOPTS) {
1869 *mp = sbcreatecontrol((caddr_t) ip_srcroute(),
1870 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
1872 mp = &(*mp)->m_next;
1875 if (inp->inp_flags & INP_RECVIF) {
1878 struct sockaddr_dl sdl;
1881 struct sockaddr_dl *sdp;
1882 struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
1884 if (((ifp = m->m_pkthdr.rcvif))
1885 && ( ifp->if_index && (ifp->if_index <= if_index))) {
1886 sdp = (struct sockaddr_dl *)
1887 (ifaddr_byindex(ifp->if_index)->ifa_addr);
1889 * Change our mind and don't try copy.
1891 if ((sdp->sdl_family != AF_LINK)
1892 || (sdp->sdl_len > sizeof(sdlbuf))) {
1895 bcopy(sdp, sdl2, sdp->sdl_len);
1899 = offsetof(struct sockaddr_dl, sdl_data[0]);
1900 sdl2->sdl_family = AF_LINK;
1901 sdl2->sdl_index = 0;
1902 sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1904 *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len,
1905 IP_RECVIF, IPPROTO_IP);
1907 mp = &(*mp)->m_next;
1912 ip_rsvp_init(struct socket *so)
1914 if (so->so_type != SOCK_RAW ||
1915 so->so_proto->pr_protocol != IPPROTO_RSVP)
1918 if (ip_rsvpd != NULL)
1923 * This may seem silly, but we need to be sure we don't over-increment
1924 * the RSVP counter, in case something slips up.
1939 * This may seem silly, but we need to be sure we don't over-decrement
1940 * the RSVP counter, in case something slips up.