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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
33 #include "opt_bootp.h"
35 #include "opt_ipstealth.h"
36 #include "opt_ipsec.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/callout.h>
45 #include <sys/malloc.h>
46 #include <sys/domain.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
50 #include <sys/kernel.h>
51 #include <sys/syslog.h>
52 #include <sys/sysctl.h>
56 #include <net/if_types.h>
57 #include <net/if_var.h>
58 #include <net/if_dl.h>
59 #include <net/route.h>
60 #include <net/netisr.h>
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/in_var.h>
65 #include <netinet/ip.h>
66 #include <netinet/in_pcb.h>
67 #include <netinet/ip_var.h>
68 #include <netinet/ip_icmp.h>
69 #include <machine/in_cksum.h>
71 #include <netinet/ip_carp.h>
74 #include <sys/socketvar.h>
76 /* XXX: Temporary until ipfw_ether and ipfw_bridge are converted. */
77 #include <netinet/ip_fw.h>
78 #include <netinet/ip_dummynet.h>
81 #include <netinet6/ipsec.h>
82 #include <netkey/key.h>
86 #include <netipsec/ipsec.h>
87 #include <netipsec/key.h>
93 SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW,
94 &ipforwarding, 0, "Enable IP forwarding between interfaces");
96 static int ipsendredirects = 1; /* XXX */
97 SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW,
98 &ipsendredirects, 0, "Enable sending IP redirects");
100 int ip_defttl = IPDEFTTL;
101 SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW,
102 &ip_defttl, 0, "Maximum TTL on IP packets");
104 static int ip_dosourceroute = 0;
105 SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW,
106 &ip_dosourceroute, 0, "Enable forwarding source routed IP packets");
108 static int ip_acceptsourceroute = 0;
109 SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute,
110 CTLFLAG_RW, &ip_acceptsourceroute, 0,
111 "Enable accepting source routed IP packets");
113 int ip_doopts = 1; /* 0 = ignore, 1 = process, 2 = reject */
114 SYSCTL_INT(_net_inet_ip, OID_AUTO, process_options, CTLFLAG_RW,
115 &ip_doopts, 0, "Enable IP options processing ([LS]SRR, RR, TS)");
117 static int ip_keepfaith = 0;
118 SYSCTL_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW,
120 "Enable packet capture for FAITH IPv4->IPv6 translater daemon");
122 static int nipq = 0; /* total # of reass queues */
124 SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragpackets, CTLFLAG_RW,
126 "Maximum number of IPv4 fragment reassembly queue entries");
128 static int maxfragsperpacket;
129 SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragsperpacket, CTLFLAG_RW,
130 &maxfragsperpacket, 0,
131 "Maximum number of IPv4 fragments allowed per packet");
133 static int ip_sendsourcequench = 0;
134 SYSCTL_INT(_net_inet_ip, OID_AUTO, sendsourcequench, CTLFLAG_RW,
135 &ip_sendsourcequench, 0,
136 "Enable the transmission of source quench packets");
138 int ip_do_randomid = 0;
139 SYSCTL_INT(_net_inet_ip, OID_AUTO, random_id, CTLFLAG_RW,
141 "Assign random ip_id values");
144 * XXX - Setting ip_checkinterface mostly implements the receive side of
145 * the Strong ES model described in RFC 1122, but since the routing table
146 * and transmit implementation do not implement the Strong ES model,
147 * setting this to 1 results in an odd hybrid.
149 * XXX - ip_checkinterface currently must be disabled if you use ipnat
150 * to translate the destination address to another local interface.
152 * XXX - ip_checkinterface must be disabled if you add IP aliases
153 * to the loopback interface instead of the interface where the
154 * packets for those addresses are received.
156 static int ip_checkinterface = 0;
157 SYSCTL_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW,
158 &ip_checkinterface, 0, "Verify packet arrives on correct interface");
161 static int ipprintfs = 0;
164 struct pfil_head inet_pfil_hook; /* Packet filter hooks */
166 static struct ifqueue ipintrq;
167 static int ipqmaxlen = IFQ_MAXLEN;
169 extern struct domain inetdomain;
170 extern struct protosw inetsw[];
171 u_char ip_protox[IPPROTO_MAX];
172 struct in_ifaddrhead in_ifaddrhead; /* first inet address */
173 struct in_ifaddrhashhead *in_ifaddrhashtbl; /* inet addr hash table */
174 u_long in_ifaddrhmask; /* mask for hash table */
176 SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RW,
177 &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue");
178 SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD,
179 &ipintrq.ifq_drops, 0, "Number of packets dropped from the IP input queue");
181 struct ipstat ipstat;
182 SYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RW,
183 &ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)");
185 /* Packet reassembly stuff */
186 #define IPREASS_NHASH_LOG2 6
187 #define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2)
188 #define IPREASS_HMASK (IPREASS_NHASH - 1)
189 #define IPREASS_HASH(x,y) \
190 (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
192 static TAILQ_HEAD(ipqhead, ipq) ipq[IPREASS_NHASH];
194 struct callout ipport_tick_callout;
196 #define IPQ_LOCK() mtx_lock(&ipqlock)
197 #define IPQ_UNLOCK() mtx_unlock(&ipqlock)
198 #define IPQ_LOCK_INIT() mtx_init(&ipqlock, "ipqlock", NULL, MTX_DEF)
199 #define IPQ_LOCK_ASSERT() mtx_assert(&ipqlock, MA_OWNED)
202 SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
203 &ip_mtu, 0, "Default MTU");
208 SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW,
213 * ipfw_ether and ipfw_bridge hooks.
214 * XXX: Temporary until those are converted to pfil_hooks as well.
216 ip_fw_chk_t *ip_fw_chk_ptr = NULL;
217 ip_dn_io_t *ip_dn_io_ptr = NULL;
222 * XXX this is ugly. IP options source routing magic.
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)];
234 struct ipoptrt ip_srcrt;
237 static void save_rte(struct mbuf *, u_char *, struct in_addr);
238 static int ip_dooptions(struct mbuf *m, int);
239 static void ip_forward(struct mbuf *m, int srcrt);
240 static void ip_freef(struct ipqhead *, struct ipq *);
243 * IP initialization: fill in IP protocol switch table.
244 * All protocols not implemented in kernel go to raw IP protocol handler.
249 register struct protosw *pr;
252 TAILQ_INIT(&in_ifaddrhead);
253 in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &in_ifaddrhmask);
254 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
256 panic("ip_init: PF_INET not found");
258 /* Initialize the entire ip_protox[] array to IPPROTO_RAW. */
259 for (i = 0; i < IPPROTO_MAX; i++)
260 ip_protox[i] = pr - inetsw;
262 * Cycle through IP protocols and put them into the appropriate place
265 for (pr = inetdomain.dom_protosw;
266 pr < inetdomain.dom_protoswNPROTOSW; pr++)
267 if (pr->pr_domain->dom_family == PF_INET &&
268 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
269 /* Be careful to only index valid IP protocols. */
270 if (pr->pr_protocol < IPPROTO_MAX)
271 ip_protox[pr->pr_protocol] = pr - inetsw;
274 /* Initialize packet filter hooks. */
275 inet_pfil_hook.ph_type = PFIL_TYPE_AF;
276 inet_pfil_hook.ph_af = AF_INET;
277 if ((i = pfil_head_register(&inet_pfil_hook)) != 0)
278 printf("%s: WARNING: unable to register pfil hook, "
279 "error %d\n", __func__, i);
281 /* Initialize IP reassembly queue. */
283 for (i = 0; i < IPREASS_NHASH; i++)
285 maxnipq = nmbclusters / 32;
286 maxfragsperpacket = 16;
288 /* Start ipport_tick. */
289 callout_init(&ipport_tick_callout, CALLOUT_MPSAFE);
291 EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
292 SHUTDOWN_PRI_DEFAULT);
294 /* Initialize various other remaining things. */
295 ip_id = time_second & 0xffff;
296 ipintrq.ifq_maxlen = ipqmaxlen;
297 mtx_init(&ipintrq.ifq_mtx, "ip_inq", NULL, MTX_DEF);
298 netisr_register(NETISR_IP, ip_input, &ipintrq, NETISR_MPSAFE);
304 callout_stop(&ipport_tick_callout);
308 * Ip input routine. Checksum and byte swap header. If fragmented
309 * try to reassemble. Process options. Pass to next level.
312 ip_input(struct mbuf *m)
314 struct ip *ip = NULL;
315 struct in_ifaddr *ia = NULL;
317 int checkif, hlen = 0;
319 int dchg = 0; /* dest changed after fw */
320 struct in_addr odst; /* original dst address */
323 struct tdb_ident *tdbi;
324 struct secpolicy *sp;
326 #endif /* FAST_IPSEC */
330 if (m->m_flags & M_FASTFWD_OURS) {
332 * Firewall or NAT changed destination to local.
333 * We expect ip_len and ip_off to be in host byte order.
335 m->m_flags &= ~M_FASTFWD_OURS;
336 /* Set up some basics that will be used later. */
337 ip = mtod(m, struct ip *);
338 hlen = ip->ip_hl << 2;
344 if (m->m_pkthdr.len < sizeof(struct ip))
347 if (m->m_len < sizeof (struct ip) &&
348 (m = m_pullup(m, sizeof (struct ip))) == NULL) {
349 ipstat.ips_toosmall++;
352 ip = mtod(m, struct ip *);
354 if (ip->ip_v != IPVERSION) {
355 ipstat.ips_badvers++;
359 hlen = ip->ip_hl << 2;
360 if (hlen < sizeof(struct ip)) { /* minimum header length */
361 ipstat.ips_badhlen++;
364 if (hlen > m->m_len) {
365 if ((m = m_pullup(m, hlen)) == NULL) {
366 ipstat.ips_badhlen++;
369 ip = mtod(m, struct ip *);
372 /* 127/8 must not appear on wire - RFC1122 */
373 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
374 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
375 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
376 ipstat.ips_badaddr++;
381 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
382 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
384 if (hlen == sizeof(struct ip)) {
385 sum = in_cksum_hdr(ip);
387 sum = in_cksum(m, hlen);
396 if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
397 /* packet is dropped by traffic conditioner */
402 * Convert fields to host representation.
404 ip->ip_len = ntohs(ip->ip_len);
405 if (ip->ip_len < hlen) {
409 ip->ip_off = ntohs(ip->ip_off);
412 * Check that the amount of data in the buffers
413 * is as at least much as the IP header would have us expect.
414 * Trim mbufs if longer than we expect.
415 * Drop packet if shorter than we expect.
417 if (m->m_pkthdr.len < ip->ip_len) {
419 ipstat.ips_tooshort++;
422 if (m->m_pkthdr.len > ip->ip_len) {
423 if (m->m_len == m->m_pkthdr.len) {
424 m->m_len = ip->ip_len;
425 m->m_pkthdr.len = ip->ip_len;
427 m_adj(m, ip->ip_len - m->m_pkthdr.len);
429 #if defined(IPSEC) && !defined(IPSEC_FILTERGIF)
431 * Bypass packet filtering for packets from a tunnel (gif).
433 if (ipsec_getnhist(m))
436 #if defined(FAST_IPSEC) && !defined(IPSEC_FILTERGIF)
438 * Bypass packet filtering for packets from a tunnel (gif).
440 if (m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL)
445 * Run through list of hooks for input packets.
447 * NB: Beware of the destination address changing (e.g.
448 * by NAT rewriting). When this happens, tell
449 * ip_forward to do the right thing.
452 /* Jump over all PFIL processing if hooks are not active. */
453 if (inet_pfil_hook.ph_busy_count == -1)
457 if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif,
460 if (m == NULL) /* consumed by filter */
463 ip = mtod(m, struct ip *);
464 dchg = (odst.s_addr != ip->ip_dst.s_addr);
466 #ifdef IPFIREWALL_FORWARD
467 if (m->m_flags & M_FASTFWD_OURS) {
468 m->m_flags &= ~M_FASTFWD_OURS;
471 #ifndef IPFIREWALL_FORWARD_EXTENDED
472 dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL);
474 if ((dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL)) != 0) {
476 * Directly ship on the packet. This allows to forward packets
477 * that were destined for us to some other directly connected
483 #endif /* IPFIREWALL_FORWARD_EXTENDED */
484 #endif /* IPFIREWALL_FORWARD */
488 * Process options and, if not destined for us,
489 * ship it on. ip_dooptions returns 1 when an
490 * error was detected (causing an icmp message
491 * to be sent and the original packet to be freed).
493 if (hlen > sizeof (struct ip) && ip_dooptions(m, 0))
496 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
497 * matter if it is destined to another node, or whether it is
498 * a multicast one, RSVP wants it! and prevents it from being forwarded
499 * anywhere else. Also checks if the rsvp daemon is running before
500 * grabbing the packet.
502 if (rsvp_on && ip->ip_p==IPPROTO_RSVP)
506 * Check our list of addresses, to see if the packet is for us.
507 * If we don't have any addresses, assume any unicast packet
508 * we receive might be for us (and let the upper layers deal
511 if (TAILQ_EMPTY(&in_ifaddrhead) &&
512 (m->m_flags & (M_MCAST|M_BCAST)) == 0)
516 * Enable a consistency check between the destination address
517 * and the arrival interface for a unicast packet (the RFC 1122
518 * strong ES model) if IP forwarding is disabled and the packet
519 * is not locally generated and the packet is not subject to
522 * XXX - Checking also should be disabled if the destination
523 * address is ipnat'ed to a different interface.
525 * XXX - Checking is incompatible with IP aliases added
526 * to the loopback interface instead of the interface where
527 * the packets are received.
529 * XXX - This is the case for carp vhost IPs as well so we
530 * insert a workaround. If the packet got here, we already
531 * checked with carp_iamatch() and carp_forus().
533 checkif = ip_checkinterface && (ipforwarding == 0) &&
534 m->m_pkthdr.rcvif != NULL &&
535 ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) &&
537 !m->m_pkthdr.rcvif->if_carp &&
542 * Check for exact addresses in the hash bucket.
544 LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
546 * If the address matches, verify that the packet
547 * arrived via the correct interface if checking is
550 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr &&
551 (!checkif || ia->ia_ifp == m->m_pkthdr.rcvif))
555 * Check for broadcast addresses.
557 * Only accept broadcast packets that arrive via the matching
558 * interface. Reception of forwarded directed broadcasts would
559 * be handled via ip_forward() and ether_output() with the loopback
560 * into the stack for SIMPLEX interfaces handled by ether_output().
562 if (m->m_pkthdr.rcvif != NULL &&
563 m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
564 TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) {
565 if (ifa->ifa_addr->sa_family != AF_INET)
568 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
571 if (ia->ia_netbroadcast.s_addr == ip->ip_dst.s_addr)
574 if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY)
579 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
580 struct in_multi *inm;
583 * If we are acting as a multicast router, all
584 * incoming multicast packets are passed to the
585 * kernel-level multicast forwarding function.
586 * The packet is returned (relatively) intact; if
587 * ip_mforward() returns a non-zero value, the packet
588 * must be discarded, else it may be accepted below.
591 ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) {
592 ipstat.ips_cantforward++;
598 * The process-level routing daemon needs to receive
599 * all multicast IGMP packets, whether or not this
600 * host belongs to their destination groups.
602 if (ip->ip_p == IPPROTO_IGMP)
604 ipstat.ips_forward++;
607 * See if we belong to the destination multicast group on the
611 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
614 ipstat.ips_notmember++;
620 if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
622 if (ip->ip_dst.s_addr == INADDR_ANY)
626 * FAITH(Firewall Aided Internet Translator)
628 if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
630 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP)
638 * Not for us; forward if possible and desirable.
640 if (ipforwarding == 0) {
641 ipstat.ips_cantforward++;
646 * Enforce inbound IPsec SPD.
648 if (ipsec4_in_reject(m, NULL)) {
649 ipsecstat.in_polvio++;
654 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
657 tdbi = (struct tdb_ident *)(mtag + 1);
658 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
660 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
661 IP_FORWARDING, &error);
663 if (sp == NULL) { /* NB: can happen if error */
665 /*XXX error stat???*/
666 DPRINTF(("ip_input: no SP for forwarding\n")); /*XXX*/
671 * Check security policy against packet attributes.
673 error = ipsec_in_reject(sp, m);
677 ipstat.ips_cantforward++;
680 #endif /* FAST_IPSEC */
688 * IPSTEALTH: Process non-routing options only
689 * if the packet is destined for us.
691 if (ipstealth && hlen > sizeof (struct ip) &&
694 #endif /* IPSTEALTH */
696 /* Count the packet in the ip address stats */
698 ia->ia_ifa.if_ipackets++;
699 ia->ia_ifa.if_ibytes += m->m_pkthdr.len;
703 * Attempt reassembly; if it succeeds, proceed.
704 * ip_reass() will return a different mbuf.
706 if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
710 ip = mtod(m, struct ip *);
711 /* Get the header length of the reassembled packet */
712 hlen = ip->ip_hl << 2;
716 * Further protocols expect the packet length to be w/o the
723 * enforce IPsec policy checking if we are seeing last header.
724 * note that we do not visit this with protocols with pcb layer
725 * code - like udp/tcp/raw ip.
727 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0 &&
728 ipsec4_in_reject(m, NULL)) {
729 ipsecstat.in_polvio++;
735 * enforce IPsec policy checking if we are seeing last header.
736 * note that we do not visit this with protocols with pcb layer
737 * code - like udp/tcp/raw ip.
739 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0) {
741 * Check if the packet has already had IPsec processing
742 * done. If so, then just pass it along. This tag gets
743 * set during AH, ESP, etc. input handling, before the
744 * packet is returned to the ip input queue for delivery.
746 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
749 tdbi = (struct tdb_ident *)(mtag + 1);
750 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
752 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
753 IP_FORWARDING, &error);
757 * Check security policy against packet attributes.
759 error = ipsec_in_reject(sp, m);
762 /* XXX error stat??? */
764 DPRINTF(("ip_input: no SP, packet discarded\n"));/*XXX*/
771 #endif /* FAST_IPSEC */
774 * Switch out to protocol's input routine.
776 ipstat.ips_delivered++;
778 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
785 * Take incoming datagram fragment and try to reassemble it into
786 * whole datagram. If the argument is the first fragment or one
787 * in between the function will return NULL and store the mbuf
788 * in the fragment chain. If the argument is the last fragment
789 * the packet will be reassembled and the pointer to the new
790 * mbuf returned for further processing. Only m_tags attached
791 * to the first packet/fragment are preserved.
792 * The IP header is *NOT* adjusted out of iplen.
796 ip_reass(struct mbuf *m)
799 struct mbuf *p, *q, *nq, *t;
800 struct ipq *fp = NULL;
801 struct ipqhead *head;
806 /* If maxnipq or maxfragsperpacket are 0, never accept fragments. */
807 if (maxnipq == 0 || maxfragsperpacket == 0) {
808 ipstat.ips_fragments++;
809 ipstat.ips_fragdropped++;
814 ip = mtod(m, struct ip *);
815 hlen = ip->ip_hl << 2;
817 hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
822 * Look for queue of fragments
825 TAILQ_FOREACH(fp, head, ipq_list)
826 if (ip->ip_id == fp->ipq_id &&
827 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
828 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
830 mac_fragment_match(m, fp) &&
832 ip->ip_p == fp->ipq_p)
838 * Enforce upper bound on number of fragmented packets
839 * for which we attempt reassembly;
840 * If maxnipq is -1, accept all fragments without limitation.
842 if ((nipq > maxnipq) && (maxnipq > 0)) {
844 * drop something from the tail of the current queue
845 * before proceeding further
847 struct ipq *q = TAILQ_LAST(head, ipqhead);
848 if (q == NULL) { /* gak */
849 for (i = 0; i < IPREASS_NHASH; i++) {
850 struct ipq *r = TAILQ_LAST(&ipq[i], ipqhead);
852 ipstat.ips_fragtimeout += r->ipq_nfrags;
853 ip_freef(&ipq[i], r);
858 ipstat.ips_fragtimeout += q->ipq_nfrags;
865 * Adjust ip_len to not reflect header,
866 * convert offset of this to bytes.
869 if (ip->ip_off & IP_MF) {
871 * Make sure that fragments have a data length
872 * that's a non-zero multiple of 8 bytes.
874 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
875 ipstat.ips_toosmall++; /* XXX */
878 m->m_flags |= M_FRAG;
880 m->m_flags &= ~M_FRAG;
885 * Attempt reassembly; if it succeeds, proceed.
886 * ip_reass() will return a different mbuf.
888 ipstat.ips_fragments++;
889 m->m_pkthdr.header = ip;
891 /* Previous ip_reass() started here. */
893 * Presence of header sizes in mbufs
894 * would confuse code below.
900 * If first fragment to arrive, create a reassembly queue.
903 if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)
905 fp = mtod(t, struct ipq *);
907 if (mac_init_ipq(fp, M_NOWAIT) != 0) {
911 mac_create_ipq(m, fp);
913 TAILQ_INSERT_HEAD(head, fp, ipq_list);
916 fp->ipq_ttl = IPFRAGTTL;
917 fp->ipq_p = ip->ip_p;
918 fp->ipq_id = ip->ip_id;
919 fp->ipq_src = ip->ip_src;
920 fp->ipq_dst = ip->ip_dst;
927 mac_update_ipq(m, fp);
931 #define GETIP(m) ((struct ip*)((m)->m_pkthdr.header))
934 * Handle ECN by comparing this segment with the first one;
935 * if CE is set, do not lose CE.
936 * drop if CE and not-ECT are mixed for the same packet.
938 ecn = ip->ip_tos & IPTOS_ECN_MASK;
939 ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK;
940 if (ecn == IPTOS_ECN_CE) {
941 if (ecn0 == IPTOS_ECN_NOTECT)
943 if (ecn0 != IPTOS_ECN_CE)
944 GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE;
946 if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT)
950 * Find a segment which begins after this one does.
952 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
953 if (GETIP(q)->ip_off > ip->ip_off)
957 * If there is a preceding segment, it may provide some of
958 * our data already. If so, drop the data from the incoming
959 * segment. If it provides all of our data, drop us, otherwise
960 * stick new segment in the proper place.
962 * If some of the data is dropped from the the preceding
963 * segment, then it's checksum is invalidated.
966 i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
971 m->m_pkthdr.csum_flags = 0;
975 m->m_nextpkt = p->m_nextpkt;
978 m->m_nextpkt = fp->ipq_frags;
983 * While we overlap succeeding segments trim them or,
984 * if they are completely covered, dequeue them.
986 for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
988 i = (ip->ip_off + ip->ip_len) - GETIP(q)->ip_off;
989 if (i < GETIP(q)->ip_len) {
990 GETIP(q)->ip_len -= i;
991 GETIP(q)->ip_off += i;
993 q->m_pkthdr.csum_flags = 0;
998 ipstat.ips_fragdropped++;
1004 * Check for complete reassembly and perform frag per packet
1007 * Frag limiting is performed here so that the nth frag has
1008 * a chance to complete the packet before we drop the packet.
1009 * As a result, n+1 frags are actually allowed per packet, but
1010 * only n will ever be stored. (n = maxfragsperpacket.)
1014 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
1015 if (GETIP(q)->ip_off != next) {
1016 if (fp->ipq_nfrags > maxfragsperpacket) {
1017 ipstat.ips_fragdropped += fp->ipq_nfrags;
1022 next += GETIP(q)->ip_len;
1024 /* Make sure the last packet didn't have the IP_MF flag */
1025 if (p->m_flags & M_FRAG) {
1026 if (fp->ipq_nfrags > maxfragsperpacket) {
1027 ipstat.ips_fragdropped += fp->ipq_nfrags;
1034 * Reassembly is complete. Make sure the packet is a sane size.
1038 if (next + (ip->ip_hl << 2) > IP_MAXPACKET) {
1039 ipstat.ips_toolong++;
1040 ipstat.ips_fragdropped += fp->ipq_nfrags;
1046 * Concatenate fragments.
1053 q->m_nextpkt = NULL;
1054 for (q = nq; q != NULL; q = nq) {
1056 q->m_nextpkt = NULL;
1057 m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
1058 m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
1062 mac_create_datagram_from_ipq(fp, m);
1063 mac_destroy_ipq(fp);
1067 * Create header for new ip packet by modifying header of first
1068 * packet; dequeue and discard fragment reassembly header.
1069 * Make header visible.
1071 ip->ip_len = (ip->ip_hl << 2) + next;
1072 ip->ip_src = fp->ipq_src;
1073 ip->ip_dst = fp->ipq_dst;
1074 TAILQ_REMOVE(head, fp, ipq_list);
1076 (void) m_free(dtom(fp));
1077 m->m_len += (ip->ip_hl << 2);
1078 m->m_data -= (ip->ip_hl << 2);
1079 /* some debugging cruft by sklower, below, will go away soon */
1080 if (m->m_flags & M_PKTHDR) /* XXX this should be done elsewhere */
1082 ipstat.ips_reassembled++;
1087 ipstat.ips_fragdropped++;
1099 * Free a fragment reassembly header and all
1100 * associated datagrams.
1104 struct ipqhead *fhp;
1107 register struct mbuf *q;
1111 while (fp->ipq_frags) {
1113 fp->ipq_frags = q->m_nextpkt;
1116 TAILQ_REMOVE(fhp, fp, ipq_list);
1117 (void) m_free(dtom(fp));
1122 * IP timer processing;
1123 * if a timer expires on a reassembly
1124 * queue, discard it.
1129 register struct ipq *fp;
1133 for (i = 0; i < IPREASS_NHASH; i++) {
1134 for(fp = TAILQ_FIRST(&ipq[i]); fp;) {
1138 fp = TAILQ_NEXT(fp, ipq_list);
1139 if(--fpp->ipq_ttl == 0) {
1140 ipstat.ips_fragtimeout += fpp->ipq_nfrags;
1141 ip_freef(&ipq[i], fpp);
1146 * If we are over the maximum number of fragments
1147 * (due to the limit being lowered), drain off
1148 * enough to get down to the new limit.
1150 if (maxnipq >= 0 && nipq > maxnipq) {
1151 for (i = 0; i < IPREASS_NHASH; i++) {
1152 while (nipq > maxnipq && !TAILQ_EMPTY(&ipq[i])) {
1153 ipstat.ips_fragdropped +=
1154 TAILQ_FIRST(&ipq[i])->ipq_nfrags;
1155 ip_freef(&ipq[i], TAILQ_FIRST(&ipq[i]));
1163 * Drain off all datagram fragments.
1171 for (i = 0; i < IPREASS_NHASH; i++) {
1172 while(!TAILQ_EMPTY(&ipq[i])) {
1173 ipstat.ips_fragdropped +=
1174 TAILQ_FIRST(&ipq[i])->ipq_nfrags;
1175 ip_freef(&ipq[i], TAILQ_FIRST(&ipq[i]));
1183 * The protocol to be inserted into ip_protox[] must be already registered
1184 * in inetsw[], either statically or through pf_proto_register().
1187 ipproto_register(u_char ipproto)
1191 /* Sanity checks. */
1193 return (EPROTONOSUPPORT);
1196 * The protocol slot must not be occupied by another protocol
1197 * already. An index pointing to IPPROTO_RAW is unused.
1199 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1201 return (EPFNOSUPPORT);
1202 if (ip_protox[ipproto] != pr - inetsw) /* IPPROTO_RAW */
1205 /* Find the protocol position in inetsw[] and set the index. */
1206 for (pr = inetdomain.dom_protosw;
1207 pr < inetdomain.dom_protoswNPROTOSW; pr++) {
1208 if (pr->pr_domain->dom_family == PF_INET &&
1209 pr->pr_protocol && pr->pr_protocol == ipproto) {
1210 /* Be careful to only index valid IP protocols. */
1211 if (pr->pr_protocol < IPPROTO_MAX) {
1212 ip_protox[pr->pr_protocol] = pr - inetsw;
1218 return (EPROTONOSUPPORT);
1222 ipproto_unregister(u_char ipproto)
1226 /* Sanity checks. */
1228 return (EPROTONOSUPPORT);
1230 /* Check if the protocol was indeed registered. */
1231 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1233 return (EPFNOSUPPORT);
1234 if (ip_protox[ipproto] == pr - inetsw) /* IPPROTO_RAW */
1237 /* Reset the protocol slot to IPPROTO_RAW. */
1238 ip_protox[ipproto] = pr - inetsw;
1244 * Do option processing on a datagram,
1245 * possibly discarding it if bad options are encountered,
1246 * or forwarding it if source-routed.
1247 * The pass argument is used when operating in the IPSTEALTH
1248 * mode to tell what options to process:
1249 * [LS]SRR (pass 0) or the others (pass 1).
1250 * The reason for as many as two passes is that when doing IPSTEALTH,
1251 * non-routing options should be processed only if the packet is for us.
1252 * Returns 1 if packet has been forwarded/freed,
1253 * 0 if the packet should be processed further.
1256 ip_dooptions(struct mbuf *m, int pass)
1258 struct ip *ip = mtod(m, struct ip *);
1260 struct in_ifaddr *ia;
1261 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
1262 struct in_addr *sin, dst;
1264 struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
1266 /* ignore or reject packets with IP options */
1269 else if (ip_doopts == 2) {
1270 type = ICMP_UNREACH;
1271 code = ICMP_UNREACH_FILTER_PROHIB;
1276 cp = (u_char *)(ip + 1);
1277 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
1278 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1279 opt = cp[IPOPT_OPTVAL];
1280 if (opt == IPOPT_EOL)
1282 if (opt == IPOPT_NOP)
1285 if (cnt < IPOPT_OLEN + sizeof(*cp)) {
1286 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1289 optlen = cp[IPOPT_OLEN];
1290 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
1291 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1301 * Source routing with record.
1302 * Find interface with current destination address.
1303 * If none on this machine then drop if strictly routed,
1304 * or do nothing if loosely routed.
1305 * Record interface address and bring up next address
1306 * component. If strictly routed make sure next
1307 * address is on directly accessible net.
1312 if (ipstealth && pass > 0)
1315 if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1316 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1319 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1320 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1323 ipaddr.sin_addr = ip->ip_dst;
1324 ia = (struct in_ifaddr *)
1325 ifa_ifwithaddr((struct sockaddr *)&ipaddr);
1327 if (opt == IPOPT_SSRR) {
1328 type = ICMP_UNREACH;
1329 code = ICMP_UNREACH_SRCFAIL;
1332 if (!ip_dosourceroute)
1333 goto nosourcerouting;
1335 * Loose routing, and not at next destination
1336 * yet; nothing to do except forward.
1340 off--; /* 0 origin */
1341 if (off > optlen - (int)sizeof(struct in_addr)) {
1343 * End of source route. Should be for us.
1345 if (!ip_acceptsourceroute)
1346 goto nosourcerouting;
1347 save_rte(m, cp, ip->ip_src);
1354 if (!ip_dosourceroute) {
1356 char buf[16]; /* aaa.bbb.ccc.ddd\0 */
1358 * Acting as a router, so generate ICMP
1361 strcpy(buf, inet_ntoa(ip->ip_dst));
1363 "attempted source route from %s to %s\n",
1364 inet_ntoa(ip->ip_src), buf);
1365 type = ICMP_UNREACH;
1366 code = ICMP_UNREACH_SRCFAIL;
1370 * Not acting as a router, so silently drop.
1375 ipstat.ips_cantforward++;
1382 * locate outgoing interface
1384 (void)memcpy(&ipaddr.sin_addr, cp + off,
1385 sizeof(ipaddr.sin_addr));
1387 if (opt == IPOPT_SSRR) {
1388 #define INA struct in_ifaddr *
1389 #define SA struct sockaddr *
1390 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == NULL)
1391 ia = (INA)ifa_ifwithnet((SA)&ipaddr);
1393 ia = ip_rtaddr(ipaddr.sin_addr);
1395 type = ICMP_UNREACH;
1396 code = ICMP_UNREACH_SRCFAIL;
1399 ip->ip_dst = ipaddr.sin_addr;
1400 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
1401 sizeof(struct in_addr));
1402 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1404 * Let ip_intr's mcast routing check handle mcast pkts
1406 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
1411 if (ipstealth && pass == 0)
1414 if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1415 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1418 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1419 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1423 * If no space remains, ignore.
1425 off--; /* 0 origin */
1426 if (off > optlen - (int)sizeof(struct in_addr))
1428 (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst,
1429 sizeof(ipaddr.sin_addr));
1431 * locate outgoing interface; if we're the destination,
1432 * use the incoming interface (should be same).
1434 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == NULL &&
1435 (ia = ip_rtaddr(ipaddr.sin_addr)) == NULL) {
1436 type = ICMP_UNREACH;
1437 code = ICMP_UNREACH_HOST;
1440 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
1441 sizeof(struct in_addr));
1442 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1447 if (ipstealth && pass == 0)
1450 code = cp - (u_char *)ip;
1451 if (optlen < 4 || optlen > 40) {
1452 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1455 if ((off = cp[IPOPT_OFFSET]) < 5) {
1456 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1459 if (off > optlen - (int)sizeof(int32_t)) {
1460 cp[IPOPT_OFFSET + 1] += (1 << 4);
1461 if ((cp[IPOPT_OFFSET + 1] & 0xf0) == 0) {
1462 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1467 off--; /* 0 origin */
1468 sin = (struct in_addr *)(cp + off);
1469 switch (cp[IPOPT_OFFSET + 1] & 0x0f) {
1471 case IPOPT_TS_TSONLY:
1474 case IPOPT_TS_TSANDADDR:
1475 if (off + sizeof(n_time) +
1476 sizeof(struct in_addr) > optlen) {
1477 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1480 ipaddr.sin_addr = dst;
1481 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
1485 (void)memcpy(sin, &IA_SIN(ia)->sin_addr,
1486 sizeof(struct in_addr));
1487 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1488 off += sizeof(struct in_addr);
1491 case IPOPT_TS_PRESPEC:
1492 if (off + sizeof(n_time) +
1493 sizeof(struct in_addr) > optlen) {
1494 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1497 (void)memcpy(&ipaddr.sin_addr, sin,
1498 sizeof(struct in_addr));
1499 if (ifa_ifwithaddr((SA)&ipaddr) == NULL)
1501 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1502 off += sizeof(struct in_addr);
1506 code = &cp[IPOPT_OFFSET + 1] - (u_char *)ip;
1510 (void)memcpy(cp + off, &ntime, sizeof(n_time));
1511 cp[IPOPT_OFFSET] += sizeof(n_time);
1514 if (forward && ipforwarding) {
1520 icmp_error(m, type, code, 0, 0);
1521 ipstat.ips_badoptions++;
1526 * Given address of next destination (final or next hop),
1527 * return internet address info of interface to be used to get there.
1534 struct sockaddr_in *sin;
1535 struct in_ifaddr *ifa;
1537 bzero(&sro, sizeof(sro));
1538 sin = (struct sockaddr_in *)&sro.ro_dst;
1539 sin->sin_family = AF_INET;
1540 sin->sin_len = sizeof(*sin);
1541 sin->sin_addr = dst;
1542 rtalloc_ign(&sro, RTF_CLONING);
1544 if (sro.ro_rt == NULL)
1547 ifa = ifatoia(sro.ro_rt->rt_ifa);
1553 * Save incoming source route for use in replies,
1554 * to be picked up later by ip_srcroute if the receiver is interested.
1557 save_rte(m, option, dst)
1563 struct ipopt_tag *opts;
1565 opts = (struct ipopt_tag *)m_tag_get(PACKET_TAG_IPOPTIONS,
1566 sizeof(struct ipopt_tag), M_NOWAIT);
1570 olen = option[IPOPT_OLEN];
1573 printf("save_rte: olen %d\n", olen);
1575 if (olen > sizeof(opts->ip_srcrt) - (1 + sizeof(dst))) {
1576 m_tag_free((struct m_tag *)opts);
1579 bcopy(option, opts->ip_srcrt.srcopt, olen);
1580 opts->ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1581 opts->ip_srcrt.dst = dst;
1582 m_tag_prepend(m, (struct m_tag *)opts);
1586 * Retrieve incoming source route for use in replies,
1587 * in the same form used by setsockopt.
1588 * The first hop is placed before the options, will be removed later.
1594 register struct in_addr *p, *q;
1595 register struct mbuf *m;
1596 struct ipopt_tag *opts;
1598 opts = (struct ipopt_tag *)m_tag_find(m0, PACKET_TAG_IPOPTIONS, NULL);
1602 if (opts->ip_nhops == 0)
1604 m = m_get(M_DONTWAIT, MT_DATA);
1608 #define OPTSIZ (sizeof(opts->ip_srcrt.nop) + sizeof(opts->ip_srcrt.srcopt))
1610 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1611 m->m_len = opts->ip_nhops * sizeof(struct in_addr) +
1612 sizeof(struct in_addr) + OPTSIZ;
1615 printf("ip_srcroute: nhops %d mlen %d", opts->ip_nhops, m->m_len);
1619 * First save first hop for return route
1621 p = &(opts->ip_srcrt.route[opts->ip_nhops - 1]);
1622 *(mtod(m, struct in_addr *)) = *p--;
1625 printf(" hops %lx", (u_long)ntohl(mtod(m, struct in_addr *)->s_addr));
1629 * Copy option fields and padding (nop) to mbuf.
1631 opts->ip_srcrt.nop = IPOPT_NOP;
1632 opts->ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
1633 (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr),
1634 &(opts->ip_srcrt.nop), OPTSIZ);
1635 q = (struct in_addr *)(mtod(m, caddr_t) +
1636 sizeof(struct in_addr) + OPTSIZ);
1639 * Record return path as an IP source route,
1640 * reversing the path (pointers are now aligned).
1642 while (p >= opts->ip_srcrt.route) {
1645 printf(" %lx", (u_long)ntohl(q->s_addr));
1650 * Last hop goes to final destination.
1652 *q = opts->ip_srcrt.dst;
1655 printf(" %lx\n", (u_long)ntohl(q->s_addr));
1657 m_tag_delete(m0, (struct m_tag *)opts);
1662 * Strip out IP options, at higher
1663 * level protocol in the kernel.
1664 * Second argument is buffer to which options
1665 * will be moved, and return value is their length.
1666 * XXX should be deleted; last arg currently ignored.
1669 ip_stripoptions(m, mopt)
1670 register struct mbuf *m;
1674 struct ip *ip = mtod(m, struct ip *);
1675 register caddr_t opts;
1678 olen = (ip->ip_hl << 2) - sizeof (struct ip);
1679 opts = (caddr_t)(ip + 1);
1680 i = m->m_len - (sizeof (struct ip) + olen);
1681 bcopy(opts + olen, opts, (unsigned)i);
1683 if (m->m_flags & M_PKTHDR)
1684 m->m_pkthdr.len -= olen;
1685 ip->ip_v = IPVERSION;
1686 ip->ip_hl = sizeof(struct ip) >> 2;
1689 u_char inetctlerrmap[PRC_NCMDS] = {
1691 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1692 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1693 EMSGSIZE, EHOSTUNREACH, 0, 0,
1694 0, 0, EHOSTUNREACH, 0,
1695 ENOPROTOOPT, ECONNREFUSED
1699 * Forward a packet. If some error occurs return the sender
1700 * an icmp packet. Note we can't always generate a meaningful
1701 * icmp message because icmp doesn't have a large enough repertoire
1702 * of codes and types.
1704 * If not forwarding, just drop the packet. This could be confusing
1705 * if ipforwarding was zero but some routing protocol was advancing
1706 * us as a gateway to somewhere. However, we must let the routing
1707 * protocol deal with that.
1709 * The srcrt parameter indicates whether the packet is being forwarded
1710 * via a source route.
1713 ip_forward(struct mbuf *m, int srcrt)
1715 struct ip *ip = mtod(m, struct ip *);
1716 struct in_ifaddr *ia = NULL;
1718 struct in_addr dest;
1719 int error, type = 0, code = 0, mtu = 0;
1723 printf("forward: src %lx dst %lx ttl %x\n",
1724 (u_long)ip->ip_src.s_addr, (u_long)ip->ip_dst.s_addr,
1729 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1730 ipstat.ips_cantforward++;
1737 if (ip->ip_ttl <= IPTTLDEC) {
1738 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
1746 if (!srcrt && (ia = ip_rtaddr(ip->ip_dst)) == NULL) {
1747 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
1752 * Save the IP header and at most 8 bytes of the payload,
1753 * in case we need to generate an ICMP message to the src.
1755 * XXX this can be optimized a lot by saving the data in a local
1756 * buffer on the stack (72 bytes at most), and only allocating the
1757 * mbuf if really necessary. The vast majority of the packets
1758 * are forwarded without having to send an ICMP back (either
1759 * because unnecessary, or because rate limited), so we are
1760 * really we are wasting a lot of work here.
1762 * We don't use m_copy() because it might return a reference
1763 * to a shared cluster. Both this function and ip_output()
1764 * assume exclusive access to the IP header in `m', so any
1765 * data in a cluster may change before we reach icmp_error().
1767 MGET(mcopy, M_DONTWAIT, m->m_type);
1768 if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_DONTWAIT)) {
1770 * It's probably ok if the pkthdr dup fails (because
1771 * the deep copy of the tag chain failed), but for now
1772 * be conservative and just discard the copy since
1773 * code below may some day want the tags.
1778 if (mcopy != NULL) {
1779 mcopy->m_len = imin((ip->ip_hl << 2) + 8,
1781 mcopy->m_pkthdr.len = mcopy->m_len;
1782 m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1788 ip->ip_ttl -= IPTTLDEC;
1794 * If forwarding packet using same interface that it came in on,
1795 * perhaps should send a redirect to sender to shortcut a hop.
1796 * Only send redirect if source is sending directly to us,
1797 * and if packet was not source routed (or has any options).
1798 * Also, don't send redirect if forwarding using a default route
1799 * or a route modified by a redirect.
1802 if (!srcrt && ipsendredirects && ia->ia_ifp == m->m_pkthdr.rcvif) {
1803 struct sockaddr_in *sin;
1807 bzero(&ro, sizeof(ro));
1808 sin = (struct sockaddr_in *)&ro.ro_dst;
1809 sin->sin_family = AF_INET;
1810 sin->sin_len = sizeof(*sin);
1811 sin->sin_addr = ip->ip_dst;
1812 rtalloc_ign(&ro, RTF_CLONING);
1816 if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1817 satosin(rt_key(rt))->sin_addr.s_addr != 0) {
1818 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa))
1819 u_long src = ntohl(ip->ip_src.s_addr);
1822 (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1823 if (rt->rt_flags & RTF_GATEWAY)
1824 dest.s_addr = satosin(rt->rt_gateway)->sin_addr.s_addr;
1826 dest.s_addr = ip->ip_dst.s_addr;
1827 /* Router requirements says to only send host redirects */
1828 type = ICMP_REDIRECT;
1829 code = ICMP_REDIRECT_HOST;
1832 printf("redirect (%d) to %lx\n", code, (u_long)dest.s_addr);
1840 error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
1842 ipstat.ips_cantforward++;
1844 ipstat.ips_forward++;
1846 ipstat.ips_redirectsent++;
1858 case 0: /* forwarded, but need redirect */
1859 /* type, code set above */
1862 case ENETUNREACH: /* shouldn't happen, checked above */
1867 type = ICMP_UNREACH;
1868 code = ICMP_UNREACH_HOST;
1872 type = ICMP_UNREACH;
1873 code = ICMP_UNREACH_NEEDFRAG;
1874 #if defined(IPSEC) || defined(FAST_IPSEC)
1876 * If the packet is routed over IPsec tunnel, tell the
1877 * originator the tunnel MTU.
1878 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
1882 struct secpolicy *sp = NULL;
1888 sp = ipsec4_getpolicybyaddr(mcopy,
1892 #else /* FAST_IPSEC */
1893 sp = ipsec_getpolicybyaddr(mcopy,
1899 /* count IPsec header size */
1900 ipsechdr = ipsec4_hdrsiz(mcopy,
1905 * find the correct route for outer IPv4
1906 * header, compute tunnel MTU.
1909 && sp->req->sav != NULL
1910 && sp->req->sav->sah != NULL) {
1911 ro = &sp->req->sav->sah->sa_route;
1912 if (ro->ro_rt && ro->ro_rt->rt_ifp) {
1914 ro->ro_rt->rt_rmx.rmx_mtu ?
1915 ro->ro_rt->rt_rmx.rmx_mtu :
1916 ro->ro_rt->rt_ifp->if_mtu;
1923 #else /* FAST_IPSEC */
1926 ipstat.ips_cantfrag++;
1929 #endif /*IPSEC || FAST_IPSEC*/
1931 * When doing source routing 'ia' can be NULL. Fall back
1932 * to the minimum guaranteed routeable packet size and use
1933 * the same hack as IPSEC to setup a dummyifp for icmp.
1938 mtu = ia->ia_ifp->if_mtu;
1939 #if defined(IPSEC) || defined(FAST_IPSEC)
1941 #endif /*IPSEC || FAST_IPSEC*/
1942 ipstat.ips_cantfrag++;
1947 * A router should not generate ICMP_SOURCEQUENCH as
1948 * required in RFC1812 Requirements for IP Version 4 Routers.
1949 * Source quench could be a big problem under DoS attacks,
1950 * or if the underlying interface is rate-limited.
1951 * Those who need source quench packets may re-enable them
1952 * via the net.inet.ip.sendsourcequench sysctl.
1954 if (ip_sendsourcequench == 0) {
1958 type = ICMP_SOURCEQUENCH;
1963 case EACCES: /* ipfw denied packet */
1967 icmp_error(mcopy, type, code, dest.s_addr, mtu);
1971 ip_savecontrol(inp, mp, ip, m)
1972 register struct inpcb *inp;
1973 register struct mbuf **mp;
1974 register struct ip *ip;
1975 register struct mbuf *m;
1977 if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) {
1981 if (inp->inp_socket->so_options & SO_BINTIME) {
1982 *mp = sbcreatecontrol((caddr_t) &bt, sizeof(bt),
1983 SCM_BINTIME, SOL_SOCKET);
1985 mp = &(*mp)->m_next;
1987 if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1990 bintime2timeval(&bt, &tv);
1991 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1992 SCM_TIMESTAMP, SOL_SOCKET);
1994 mp = &(*mp)->m_next;
1997 if (inp->inp_flags & INP_RECVDSTADDR) {
1998 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
1999 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
2001 mp = &(*mp)->m_next;
2003 if (inp->inp_flags & INP_RECVTTL) {
2004 *mp = sbcreatecontrol((caddr_t) &ip->ip_ttl,
2005 sizeof(u_char), IP_RECVTTL, IPPROTO_IP);
2007 mp = &(*mp)->m_next;
2011 * Moving these out of udp_input() made them even more broken
2012 * than they already were.
2014 /* options were tossed already */
2015 if (inp->inp_flags & INP_RECVOPTS) {
2016 *mp = sbcreatecontrol((caddr_t) opts_deleted_above,
2017 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
2019 mp = &(*mp)->m_next;
2021 /* ip_srcroute doesn't do what we want here, need to fix */
2022 if (inp->inp_flags & INP_RECVRETOPTS) {
2023 *mp = sbcreatecontrol((caddr_t) ip_srcroute(m),
2024 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
2026 mp = &(*mp)->m_next;
2029 if (inp->inp_flags & INP_RECVIF) {
2032 struct sockaddr_dl sdl;
2035 struct sockaddr_dl *sdp;
2036 struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
2038 if (((ifp = m->m_pkthdr.rcvif))
2039 && ( ifp->if_index && (ifp->if_index <= if_index))) {
2040 sdp = (struct sockaddr_dl *)
2041 (ifaddr_byindex(ifp->if_index)->ifa_addr);
2043 * Change our mind and don't try copy.
2045 if ((sdp->sdl_family != AF_LINK)
2046 || (sdp->sdl_len > sizeof(sdlbuf))) {
2049 bcopy(sdp, sdl2, sdp->sdl_len);
2053 = offsetof(struct sockaddr_dl, sdl_data[0]);
2054 sdl2->sdl_family = AF_LINK;
2055 sdl2->sdl_index = 0;
2056 sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
2058 *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len,
2059 IP_RECVIF, IPPROTO_IP);
2061 mp = &(*mp)->m_next;
2066 * XXX these routines are called from the upper part of the kernel.
2067 * They need to be locked when we remove Giant.
2069 * They could also be moved to ip_mroute.c, since all the RSVP
2070 * handling is done there already.
2072 static int ip_rsvp_on;
2073 struct socket *ip_rsvpd;
2075 ip_rsvp_init(struct socket *so)
2077 if (so->so_type != SOCK_RAW ||
2078 so->so_proto->pr_protocol != IPPROTO_RSVP)
2081 if (ip_rsvpd != NULL)
2086 * This may seem silly, but we need to be sure we don't over-increment
2087 * the RSVP counter, in case something slips up.
2102 * This may seem silly, but we need to be sure we don't over-decrement
2103 * the RSVP counter, in case something slips up.
2113 rsvp_input(struct mbuf *m, int off) /* XXX must fixup manually */
2115 if (rsvp_input_p) { /* call the real one if loaded */
2116 rsvp_input_p(m, off);
2120 /* Can still get packets with rsvp_on = 0 if there is a local member
2121 * of the group to which the RSVP packet is addressed. But in this
2122 * case we want to throw the packet away.
2130 if (ip_rsvpd != NULL) {
2134 /* Drop the packet */