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