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