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