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 * $Id: ip_input.c,v 1.117 1999/04/20 13:32:05 peter Exp $
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
45#include <stddef.h>
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/mbuf.h>
50#include <sys/malloc.h>
51#include <sys/domain.h>
52#include <sys/protosw.h>
53#include <sys/socket.h>
54#include <sys/time.h>
55#include <sys/kernel.h>
56#include <sys/syslog.h>
57#include <sys/sysctl.h>
58
59#include <net/if.h>
60#include <net/if_var.h>
61#include <net/if_dl.h>
62#include <net/route.h>
63#include <net/netisr.h>
64
65#include <netinet/in.h>
66#include <netinet/in_systm.h>
67#include <netinet/in_var.h>
68#include <netinet/ip.h>
69#include <netinet/in_pcb.h>
70#include <netinet/ip_var.h>
71#include <netinet/ip_icmp.h>
72#include <machine/in_cksum.h>
73
74#include <sys/socketvar.h>
75
76#include <netinet/ip_fw.h>
77
78#ifdef DUMMYNET
79#include <netinet/ip_dummynet.h>
80#endif
81
82int rsvp_on = 0;
83static int ip_rsvp_on;
84struct socket *ip_rsvpd;
85
86int ipforwarding = 0;
87SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW,
88 &ipforwarding, 0, "Enable IP forwarding between interfaces");
89
90static int ipsendredirects = 1; /* XXX */
91SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW,
92 &ipsendredirects, 0, "Enable sending IP redirects");
93
94int ip_defttl = IPDEFTTL;
95SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW,
96 &ip_defttl, 0, "Maximum TTL on IP packets");
97
98static int ip_dosourceroute = 0;
99SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW,
100 &ip_dosourceroute, 0, "Enable forwarding source routed IP packets");
101
102static int ip_acceptsourceroute = 0;
103SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute,
104 CTLFLAG_RW, &ip_acceptsourceroute, 0,
105 "Enable accepting source routed IP packets");
106#ifdef DIAGNOSTIC
107static int ipprintfs = 0;
108#endif
109
110extern struct domain inetdomain;
111extern struct protosw inetsw[];
112u_char ip_protox[IPPROTO_MAX];
113static int ipqmaxlen = IFQ_MAXLEN;
114struct in_ifaddrhead in_ifaddrhead; /* first inet address */
115struct ifqueue ipintrq;
116SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RD,
117 &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue");
118SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD,
119 &ipintrq.ifq_drops, 0, "Number of packets dropped from the IP input queue");
120
121struct ipstat ipstat;
122SYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RD,
123 &ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)");
124
125/* Packet reassembly stuff */
126#define IPREASS_NHASH_LOG2 6
127#define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2)
128#define IPREASS_HMASK (IPREASS_NHASH - 1)
129#define IPREASS_HASH(x,y) \
130 (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
131
132static struct ipq ipq[IPREASS_NHASH];
133static int nipq = 0; /* total # of reass queues */
134static int maxnipq;
135
136#ifdef IPCTL_DEFMTU
137SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
138 &ip_mtu, 0, "Default MTU");
139#endif
140
141#ifdef IPSTEALTH
142static int ipstealth = 0;
143SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW,
144 &ipstealth, 0, "");
145#endif
146
147
148/* Firewall hooks */
149ip_fw_chk_t *ip_fw_chk_ptr;
150ip_fw_ctl_t *ip_fw_ctl_ptr;
151
152#ifdef DUMMYNET
153ip_dn_ctl_t *ip_dn_ctl_ptr;
154#endif
155
156#if defined(IPFILTER_LKM) || defined(IPFILTER)
157int iplattach __P((void));
158int (*fr_checkp) __P((struct ip *, int, struct ifnet *, int, struct mbuf **)) = NULL;
159#endif
160
161
162/*
163 * We need to save the IP options in case a protocol wants to respond
164 * to an incoming packet over the same route if the packet got here
165 * using IP source routing. This allows connection establishment and
166 * maintenance when the remote end is on a network that is not known
167 * to us.
168 */
169static int ip_nhops = 0;
170static struct ip_srcrt {
171 struct in_addr dst; /* final destination */
172 char nop; /* one NOP to align */
173 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */
174 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
175} ip_srcrt;
176
177#ifdef IPDIVERT
178/*
179 * Shared variable between ip_input() and ip_reass() to communicate
180 * about which packets, once assembled from fragments, get diverted,
181 * and to which port.
182 */
183static u_short frag_divert_port;
184#endif
185
186struct sockaddr_in *ip_fw_fwd_addr;
187
188static void save_rte __P((u_char *, struct in_addr));
189static int ip_dooptions __P((struct mbuf *));
190static void ip_forward __P((struct mbuf *, int));
191static void ip_freef __P((struct ipq *));
192static struct ip *
193 ip_reass __P((struct mbuf *, struct ipq *, struct ipq *));
194static struct in_ifaddr *
195 ip_rtaddr __P((struct in_addr));
196static void ipintr __P((void));
197/*
198 * IP initialization: fill in IP protocol switch table.
199 * All protocols not implemented in kernel go to raw IP protocol handler.
200 */
201void
202ip_init()
203{
204 register struct protosw *pr;
205 register int i;
206
207 TAILQ_INIT(&in_ifaddrhead);
208 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
209 if (pr == 0)
210 panic("ip_init");
211 for (i = 0; i < IPPROTO_MAX; i++)
212 ip_protox[i] = pr - inetsw;
213 for (pr = inetdomain.dom_protosw;
214 pr < inetdomain.dom_protoswNPROTOSW; pr++)
215 if (pr->pr_domain->dom_family == PF_INET &&
216 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
217 ip_protox[pr->pr_protocol] = pr - inetsw;
218
219 for (i = 0; i < IPREASS_NHASH; i++)
220 ipq[i].next = ipq[i].prev = &ipq[i];
221
222 maxnipq = nmbclusters/4;
223
224 ip_id = time_second & 0xffff;
225 ipintrq.ifq_maxlen = ipqmaxlen;
226#ifdef IPFILTER
227 iplattach();
228#endif
229
230}
231
232static struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
233static struct route ipforward_rt;
234
235/*
236 * Ip input routine. Checksum and byte swap header. If fragmented
237 * try to reassemble. Process options. Pass to next level.
238 */
239void
240ip_input(struct mbuf *m)
241{
242 struct ip *ip;
243 struct ipq *fp;
244 struct in_ifaddr *ia;
245 int i, hlen, mff;
246 u_short sum;
247#ifndef IPDIVERT /* dummy variable for the firewall code to play with */
248 u_short ip_divert_cookie = 0 ;
249#endif
250 struct ip_fw_chain *rule = NULL ;
251
252#if defined(IPFIREWALL) && defined(DUMMYNET)
253 /*
254 * dummynet packet are prepended a vestigial mbuf with
255 * m_type = MT_DUMMYNET and m_data pointing to the matching
256 * rule.
257 */
258 if (m->m_type == MT_DUMMYNET) {
259 struct mbuf *m0 = m ;
260 rule = (struct ip_fw_chain *)(m->m_data) ;
261 m = m->m_next ;
262 free(m0, M_IPFW);
263 ip = mtod(m, struct ip *);
264 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
265 goto iphack ;
266 } else
267 rule = NULL ;
268#endif
269
270#ifdef DIAGNOSTIC
271 if (m == NULL || (m->m_flags & M_PKTHDR) == 0)
272 panic("ip_input no HDR");
273#endif
274 ipstat.ips_total++;
275
276 if (m->m_pkthdr.len < sizeof(struct ip))
277 goto tooshort;
278
279 if (m->m_len < sizeof (struct ip) &&
280 (m = m_pullup(m, sizeof (struct ip))) == 0) {
281 ipstat.ips_toosmall++;
282 return;
283 }
284 ip = mtod(m, struct ip *);
285
286 if (IP_VHL_V(ip->ip_vhl) != IPVERSION) {
287 ipstat.ips_badvers++;
288 goto bad;
289 }
290
291 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
292 if (hlen < sizeof(struct ip)) { /* minimum header length */
293 ipstat.ips_badhlen++;
294 goto bad;
295 }
296 if (hlen > m->m_len) {
297 if ((m = m_pullup(m, hlen)) == 0) {
298 ipstat.ips_badhlen++;
299 return;
300 }
301 ip = mtod(m, struct ip *);
302 }
303 if (hlen == sizeof(struct ip)) {
304 sum = in_cksum_hdr(ip);
305 } else {
306 sum = in_cksum(m, hlen);
307 }
308 if (sum) {
309 ipstat.ips_badsum++;
310 goto bad;
311 }
312
313 /*
314 * Convert fields to host representation.
315 */
316 NTOHS(ip->ip_len);
317 if (ip->ip_len < hlen) {
318 ipstat.ips_badlen++;
319 goto bad;
320 }
321 NTOHS(ip->ip_id);
322 NTOHS(ip->ip_off);
323
324 /*
325 * Check that the amount of data in the buffers
326 * is as at least much as the IP header would have us expect.
327 * Trim mbufs if longer than we expect.
328 * Drop packet if shorter than we expect.
329 */
330 if (m->m_pkthdr.len < ip->ip_len) {
331tooshort:
332 ipstat.ips_tooshort++;
333 goto bad;
334 }
335 if (m->m_pkthdr.len > ip->ip_len) {
336 if (m->m_len == m->m_pkthdr.len) {
337 m->m_len = ip->ip_len;
338 m->m_pkthdr.len = ip->ip_len;
339 } else
340 m_adj(m, ip->ip_len - m->m_pkthdr.len);
341 }
342 /*
343 * IpHack's section.
344 * Right now when no processing on packet has done
345 * and it is still fresh out of network we do our black
346 * deals with it.
347 * - Firewall: deny/allow/divert
348 * - Xlate: translate packet's addr/port (NAT).
349 * - Pipe: pass pkt through dummynet.
350 * - Wrap: fake packet's addr/port <unimpl.>
351 * - Encapsulate: put it in another IP and send out. <unimp.>
352 */
353
354#if defined(IPFIREWALL) && defined(DUMMYNET)
355iphack:
356#endif
357#if defined(IPFILTER) || defined(IPFILTER_LKM)
358 /*
359 * Check if we want to allow this packet to be processed.
360 * Consider it to be bad if not.
361 */
362 if (fr_checkp) {
363 struct mbuf *m1 = m;
364
365 if ((*fr_checkp)(ip, hlen, m->m_pkthdr.rcvif, 0, &m1) || !m1)
366 return;
367 ip = mtod(m = m1, struct ip *);
368 }
369#endif
370 if (ip_fw_chk_ptr) {
371#ifdef IPFIREWALL_FORWARD
372 /*
373 * If we've been forwarded from the output side, then
374 * skip the firewall a second time
375 */
376 if (ip_fw_fwd_addr)
377 goto ours;
378#endif /* IPFIREWALL_FORWARD */
379 i = (*ip_fw_chk_ptr)(&ip, hlen, NULL, &ip_divert_cookie,
380 &m, &rule, &ip_fw_fwd_addr);
381 /*
382 * see the comment in ip_output for the return values
383 * produced by the firewall.
384 */
385 if (!m) /* packet discarded by firewall */
386 return ;
387 if (i == 0 && ip_fw_fwd_addr == NULL) /* common case */
388 goto pass ;
389#ifdef DUMMYNET
390 if (i & 0x10000) {
391 /* send packet to the appropriate pipe */
392 dummynet_io(i&0xffff,DN_TO_IP_IN,m,NULL,NULL,0, rule);
393 return ;
394 }
395#endif
396#ifdef IPDIVERT
397 if (i > 0 && i < 0x10000) {
398 /* Divert packet */
399 frag_divert_port = i & 0xffff ;
400 goto ours;
401 }
402#endif
403#ifdef IPFIREWALL_FORWARD
404 if (i == 0 && ip_fw_fwd_addr != NULL)
405 goto pass ;
406#endif
407 /*
408 * if we get here, the packet must be dropped
409 */
410 m_freem(m);
411 return;
412 }
413pass:
414
415 /*
416 * Process options and, if not destined for us,
417 * ship it on. ip_dooptions returns 1 when an
418 * error was detected (causing an icmp message
419 * to be sent and the original packet to be freed).
420 */
421 ip_nhops = 0; /* for source routed packets */
422 if (hlen > sizeof (struct ip) && ip_dooptions(m)) {
423#ifdef IPFIREWALL_FORWARD
424 ip_fw_fwd_addr = NULL;
425#endif
426 return;
427 }
428
429 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
430 * matter if it is destined to another node, or whether it is
431 * a multicast one, RSVP wants it! and prevents it from being forwarded
432 * anywhere else. Also checks if the rsvp daemon is running before
433 * grabbing the packet.
434 */
435 if (rsvp_on && ip->ip_p==IPPROTO_RSVP)
436 goto ours;
437
438 /*
439 * Check our list of addresses, to see if the packet is for us.
440 * If we don't have any addresses, assume any unicast packet
441 * we receive might be for us (and let the upper layers deal
442 * with it).
443 */
444 if (TAILQ_EMPTY(&in_ifaddrhead) &&
445 (m->m_flags & (M_MCAST|M_BCAST)) == 0)
446 goto ours;
447
448 for (ia = TAILQ_FIRST(&in_ifaddrhead); ia;
449 ia = TAILQ_NEXT(ia, ia_link)) {
450#define satosin(sa) ((struct sockaddr_in *)(sa))
451
452#ifdef BOOTP_COMPAT
453 if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY)
454 goto ours;
455#endif
456#ifdef IPFIREWALL_FORWARD
457 /*
458 * If the addr to forward to is one of ours, we pretend to
459 * be the destination for this packet.
460 */
461 if (ip_fw_fwd_addr == NULL) {
462 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr)
463 goto ours;
464 } else if (IA_SIN(ia)->sin_addr.s_addr ==
465 ip_fw_fwd_addr->sin_addr.s_addr)
466 goto ours;
467#else
468 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr)
469 goto ours;
470#endif
471 if (ia->ia_ifp && ia->ia_ifp->if_flags & IFF_BROADCAST) {
472 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
473 ip->ip_dst.s_addr)
474 goto ours;
475 if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr)
476 goto ours;
477 }
478 }
479 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
480 struct in_multi *inm;
481 if (ip_mrouter) {
482 /*
483 * If we are acting as a multicast router, all
484 * incoming multicast packets are passed to the
485 * kernel-level multicast forwarding function.
486 * The packet is returned (relatively) intact; if
487 * ip_mforward() returns a non-zero value, the packet
488 * must be discarded, else it may be accepted below.
489 *
490 * (The IP ident field is put in the same byte order
491 * as expected when ip_mforward() is called from
492 * ip_output().)
493 */
494 ip->ip_id = htons(ip->ip_id);
495 if (ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) {
496 ipstat.ips_cantforward++;
497 m_freem(m);
498 return;
499 }
500 ip->ip_id = ntohs(ip->ip_id);
501
502 /*
503 * The process-level routing demon needs to receive
504 * all multicast IGMP packets, whether or not this
505 * host belongs to their destination groups.
506 */
507 if (ip->ip_p == IPPROTO_IGMP)
508 goto ours;
509 ipstat.ips_forward++;
510 }
511 /*
512 * See if we belong to the destination multicast group on the
513 * arrival interface.
514 */
515 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
516 if (inm == NULL) {
517 ipstat.ips_notmember++;
518 m_freem(m);
519 return;
520 }
521 goto ours;
522 }
523 if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
524 goto ours;
525 if (ip->ip_dst.s_addr == INADDR_ANY)
526 goto ours;
527
528 /*
529 * Not for us; forward if possible and desirable.
530 */
531 if (ipforwarding == 0) {
532 ipstat.ips_cantforward++;
533 m_freem(m);
534 } else
535 ip_forward(m, 0);
536#ifdef IPFIREWALL_FORWARD
537 ip_fw_fwd_addr = NULL;
538#endif
539 return;
540
541ours:
542
543 /*
544 * If offset or IP_MF are set, must reassemble.
545 * Otherwise, nothing need be done.
546 * (We could look in the reassembly queue to see
547 * if the packet was previously fragmented,
548 * but it's not worth the time; just let them time out.)
549 */
550 if (ip->ip_off & (IP_MF | IP_OFFMASK | IP_RF)) {
551 if (m->m_flags & M_EXT) { /* XXX */
552 if ((m = m_pullup(m, hlen)) == 0) {
553 ipstat.ips_toosmall++;
554#ifdef IPDIVERT
555 frag_divert_port = 0;
556 ip_divert_cookie = 0;
557#endif
558#ifdef IPFIREWALL_FORWARD
559 ip_fw_fwd_addr = NULL;
560#endif
561 return;
562 }
563 ip = mtod(m, struct ip *);
564 }
565 sum = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
566 /*
567 * Look for queue of fragments
568 * of this datagram.
569 */
570 for (fp = ipq[sum].next; fp != &ipq[sum]; fp = fp->next)
571 if (ip->ip_id == fp->ipq_id &&
572 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
573 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
574 ip->ip_p == fp->ipq_p)
575 goto found;
576
577 fp = 0;
578
579 /* check if there's a place for the new queue */
580 if (nipq > maxnipq) {
581 /*
582 * drop something from the tail of the current queue
583 * before proceeding further
584 */
585 if (ipq[sum].prev == &ipq[sum]) { /* gak */
586 for (i = 0; i < IPREASS_NHASH; i++) {
587 if (ipq[i].prev != &ipq[i]) {
588 ip_freef(ipq[i].prev);
589 break;
590 }
591 }
592 } else
593 ip_freef(ipq[sum].prev);
594 }
595found:
596 /*
597 * Adjust ip_len to not reflect header,
598 * set ip_mff if more fragments are expected,
599 * convert offset of this to bytes.
600 */
601 ip->ip_len -= hlen;
602 mff = (ip->ip_off & IP_MF) != 0;
603 if (mff) {
604 /*
605 * Make sure that fragments have a data length
606 * that's a non-zero multiple of 8 bytes.
607 */
608 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
609 ipstat.ips_toosmall++; /* XXX */
610 goto bad;
611 }
612 m->m_flags |= M_FRAG;
613 }
614 ip->ip_off <<= 3;
615
616 /*
617 * If datagram marked as having more fragments
618 * or if this is not the first fragment,
619 * attempt reassembly; if it succeeds, proceed.
620 */
621 if (mff || ip->ip_off) {
622 ipstat.ips_fragments++;
623 m->m_pkthdr.header = ip;
624 ip = ip_reass(m, fp, &ipq[sum]);
625 if (ip == 0) {
626#ifdef IPFIREWALL_FORWARD
627 ip_fw_fwd_addr = NULL;
628#endif
629 return;
630 }
631 /* Get the length of the reassembled packets header */
632 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
633 ipstat.ips_reassembled++;
634 m = dtom(ip);
635#ifdef IPDIVERT
636 if (frag_divert_port) {
637 ip->ip_len += hlen;
638 HTONS(ip->ip_len);
639 HTONS(ip->ip_off);
640 HTONS(ip->ip_id);
641 ip->ip_sum = 0;
642 ip->ip_sum = in_cksum_hdr(ip);
643 NTOHS(ip->ip_id);
644 NTOHS(ip->ip_off);
645 NTOHS(ip->ip_len);
646 ip->ip_len -= hlen;
647 }
648#endif
649 } else
650 if (fp)
651 ip_freef(fp);
652 } else
653 ip->ip_len -= hlen;
654
655#ifdef IPDIVERT
656 /*
657 * Divert reassembled packets to the divert protocol if required
658 * If divert port is null then cookie should be too,
659 * so we shouldn't need to clear them here. Assume ip_divert does so.
660 */
661 if (frag_divert_port) {
662 ipstat.ips_delivered++;
663 ip_divert_port = frag_divert_port;
664 frag_divert_port = 0;
665 (*inetsw[ip_protox[IPPROTO_DIVERT]].pr_input)(m, hlen);
666 return;
667 }
668
669 /* Don't let packets divert themselves */
670 if (ip->ip_p == IPPROTO_DIVERT) {
671 ipstat.ips_noproto++;
672 goto bad;
673 }
674
675#endif
676
677 /*
678 * Switch out to protocol's input routine.
679 */
680 ipstat.ips_delivered++;
681 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
682#ifdef IPFIREWALL_FORWARD
683 ip_fw_fwd_addr = NULL; /* tcp needed it */
684#endif
685 return;
686bad:
687#ifdef IPFIREWALL_FORWARD
688 ip_fw_fwd_addr = NULL;
689#endif
690 m_freem(m);
691}
692
693/*
694 * IP software interrupt routine - to go away sometime soon
695 */
696static void
697ipintr(void)
698{
699 int s;
700 struct mbuf *m;
701
702 while(1) {
703 s = splimp();
704 IF_DEQUEUE(&ipintrq, m);
705 splx(s);
706 if (m == 0)
707 return;
708 ip_input(m);
709 }
710}
711
712NETISR_SET(NETISR_IP, ipintr);
713
714/*
715 * Take incoming datagram fragment and try to
716 * reassemble it into whole datagram. If a chain for
717 * reassembly of this datagram already exists, then it
718 * is given as fp; otherwise have to make a chain.
719 */
720static struct ip *
721ip_reass(m, fp, where)
722 register struct mbuf *m;
723 register struct ipq *fp;
724 struct ipq *where;
725{
726 struct ip *ip = mtod(m, struct ip *);
727 register struct mbuf *p = 0, *q, *nq;
728 struct mbuf *t;
729 int hlen = IP_VHL_HL(ip->ip_vhl) << 2;
730 int i, next;
731
732 /*
733 * Presence of header sizes in mbufs
734 * would confuse code below.
735 */
736 m->m_data += hlen;
737 m->m_len -= hlen;
738
739 /*
740 * If first fragment to arrive, create a reassembly queue.
741 */
742 if (fp == 0) {
743 if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)
744 goto dropfrag;
745 fp = mtod(t, struct ipq *);
746 insque(fp, where);
747 nipq++;
748 fp->ipq_ttl = IPFRAGTTL;
749 fp->ipq_p = ip->ip_p;
750 fp->ipq_id = ip->ip_id;
751 fp->ipq_src = ip->ip_src;
752 fp->ipq_dst = ip->ip_dst;
753 fp->ipq_frags = m;
754 m->m_nextpkt = NULL;
755#ifdef IPDIVERT
756 fp->ipq_divert = 0;
757 fp->ipq_div_cookie = 0;
758#endif
759 goto inserted;
760 }
761
762#define GETIP(m) ((struct ip*)((m)->m_pkthdr.header))
763
764 /*
765 * Find a segment which begins after this one does.
766 */
767 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
768 if (GETIP(q)->ip_off > ip->ip_off)
769 break;
770
771 /*
772 * If there is a preceding segment, it may provide some of
773 * our data already. If so, drop the data from the incoming
774 * segment. If it provides all of our data, drop us, otherwise
775 * stick new segment in the proper place.
776 */
777 if (p) {
778 i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
779 if (i > 0) {
780 if (i >= ip->ip_len)
781 goto dropfrag;
782 m_adj(dtom(ip), i);
783 ip->ip_off += i;
784 ip->ip_len -= i;
785 }
786 m->m_nextpkt = p->m_nextpkt;
787 p->m_nextpkt = m;
788 } else {
789 m->m_nextpkt = fp->ipq_frags;
790 fp->ipq_frags = m;
791 }
792
793 /*
794 * While we overlap succeeding segments trim them or,
795 * if they are completely covered, dequeue them.
796 */
797 for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
798 q = nq) {
799 i = (ip->ip_off + ip->ip_len) -
800 GETIP(q)->ip_off;
801 if (i < GETIP(q)->ip_len) {
802 GETIP(q)->ip_len -= i;
803 GETIP(q)->ip_off += i;
804 m_adj(q, i);
805 break;
806 }
807 nq = q->m_nextpkt;
808 m->m_nextpkt = nq;
809 m_freem(q);
810 }
811
812inserted:
813
814#ifdef IPDIVERT
815 /*
816 * Any fragment diverting causes the whole packet to divert
817 */
818 if (frag_divert_port) {
819 fp->ipq_divert = frag_divert_port;
820 fp->ipq_div_cookie = ip_divert_cookie;
821 }
822 frag_divert_port = 0;
823 ip_divert_cookie = 0;
824#endif
825
826 /*
827 * Check for complete reassembly.
828 */
829 next = 0;
830 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
831 if (GETIP(q)->ip_off != next)
832 return (0);
833 next += GETIP(q)->ip_len;
834 }
835 /* Make sure the last packet didn't have the IP_MF flag */
836 if (p->m_flags & M_FRAG)
837 return (0);
838
839 /*
840 * Reassembly is complete. Make sure the packet is a sane size.
841 */
842 q = fp->ipq_frags;
843 ip = GETIP(q);
844 if (next + (IP_VHL_HL(ip->ip_vhl) << 2) > IP_MAXPACKET) {
845 ipstat.ips_toolong++;
846 ip_freef(fp);
847 return (0);
848 }
849
850 /*
851 * Concatenate fragments.
852 */
853 m = q;
854 t = m->m_next;
855 m->m_next = 0;
856 m_cat(m, t);
857 nq = q->m_nextpkt;
858 q->m_nextpkt = 0;
859 for (q = nq; q != NULL; q = nq) {
860 nq = q->m_nextpkt;
861 q->m_nextpkt = NULL;
862 m_cat(m, q);
863 }
864
865#ifdef IPDIVERT
866 /*
867 * extract divert port for packet, if any
868 */
869 frag_divert_port = fp->ipq_divert;
870 ip_divert_cookie = fp->ipq_div_cookie;
871#endif
872
873 /*
874 * Create header for new ip packet by
875 * modifying header of first packet;
876 * dequeue and discard fragment reassembly header.
877 * Make header visible.
878 */
879 ip->ip_len = next;
880 ip->ip_src = fp->ipq_src;
881 ip->ip_dst = fp->ipq_dst;
882 remque(fp);
883 nipq--;
884 (void) m_free(dtom(fp));
885 m->m_len += (IP_VHL_HL(ip->ip_vhl) << 2);
886 m->m_data -= (IP_VHL_HL(ip->ip_vhl) << 2);
887 /* some debugging cruft by sklower, below, will go away soon */
888 if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
889 register int plen = 0;
890 for (t = m; m; m = m->m_next)
891 plen += m->m_len;
892 t->m_pkthdr.len = plen;
893 }
894 return (ip);
895
896dropfrag:
897#ifdef IPDIVERT
898 frag_divert_port = 0;
899 ip_divert_cookie = 0;
900#endif
901 ipstat.ips_fragdropped++;
902 m_freem(m);
903 return (0);
904
905#undef GETIP
906}
907
908/*
909 * Free a fragment reassembly header and all
910 * associated datagrams.
911 */
912static void
913ip_freef(fp)
914 struct ipq *fp;
915{
916 register struct mbuf *q;
917
918 while (fp->ipq_frags) {
919 q = fp->ipq_frags;
920 fp->ipq_frags = q->m_nextpkt;
921 m_freem(q);
922 }
923 remque(fp);
924 (void) m_free(dtom(fp));
925 nipq--;
926}
927
928/*
929 * IP timer processing;
930 * if a timer expires on a reassembly
931 * queue, discard it.
932 */
933void
934ip_slowtimo()
935{
936 register struct ipq *fp;
937 int s = splnet();
938 int i;
939
940 for (i = 0; i < IPREASS_NHASH; i++) {
941 fp = ipq[i].next;
942 if (fp == 0)
943 continue;
944 while (fp != &ipq[i]) {
945 --fp->ipq_ttl;
946 fp = fp->next;
947 if (fp->prev->ipq_ttl == 0) {
948 ipstat.ips_fragtimeout++;
949 ip_freef(fp->prev);
950 }
951 }
952 }
953 ipflow_slowtimo();
954 splx(s);
955}
956
957/*
958 * Drain off all datagram fragments.
959 */
960void
961ip_drain()
962{
963 int i;
964
965 for (i = 0; i < IPREASS_NHASH; i++) {
966 while (ipq[i].next != &ipq[i]) {
967 ipstat.ips_fragdropped++;
968 ip_freef(ipq[i].next);
969 }
970 }
971 in_rtqdrain();
972}
973
974/*
975 * Do option processing on a datagram,
976 * possibly discarding it if bad options are encountered,
977 * or forwarding it if source-routed.
978 * Returns 1 if packet has been forwarded/freed,
979 * 0 if the packet should be processed further.
980 */
981static int
982ip_dooptions(m)
983 struct mbuf *m;
984{
985 register struct ip *ip = mtod(m, struct ip *);
986 register u_char *cp;
987 register struct ip_timestamp *ipt;
988 register struct in_ifaddr *ia;
989 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
990 struct in_addr *sin, dst;
991 n_time ntime;
992
993 dst = ip->ip_dst;
994 cp = (u_char *)(ip + 1);
995 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
996 for (; cnt > 0; cnt -= optlen, cp += optlen) {
997 opt = cp[IPOPT_OPTVAL];
998 if (opt == IPOPT_EOL)
999 break;
1000 if (opt == IPOPT_NOP)
1001 optlen = 1;
1002 else {
1003 optlen = cp[IPOPT_OLEN];
1004 if (optlen <= 0 || optlen > cnt) {
1005 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1006 goto bad;
1007 }
1008 }
1009 switch (opt) {
1010
1011 default:
1012 break;
1013
1014 /*
1015 * Source routing with record.
1016 * Find interface with current destination address.
1017 * If none on this machine then drop if strictly routed,
1018 * or do nothing if loosely routed.
1019 * Record interface address and bring up next address
1020 * component. If strictly routed make sure next
1021 * address is on directly accessible net.
1022 */
1023 case IPOPT_LSRR:
1024 case IPOPT_SSRR:
1025 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1026 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1027 goto bad;
1028 }
1029 ipaddr.sin_addr = ip->ip_dst;
1030 ia = (struct in_ifaddr *)
1031 ifa_ifwithaddr((struct sockaddr *)&ipaddr);
1032 if (ia == 0) {
1033 if (opt == IPOPT_SSRR) {
1034 type = ICMP_UNREACH;
1035 code = ICMP_UNREACH_SRCFAIL;
1036 goto bad;
1037 }
1038 if (!ip_dosourceroute)
1039 goto nosourcerouting;
1040 /*
1041 * Loose routing, and not at next destination
1042 * yet; nothing to do except forward.
1043 */
1044 break;
1045 }
1046 off--; /* 0 origin */
1047 if (off > optlen - sizeof(struct in_addr)) {
1048 /*
1049 * End of source route. Should be for us.
1050 */
1051 if (!ip_acceptsourceroute)
1052 goto nosourcerouting;
1053 save_rte(cp, ip->ip_src);
1054 break;
1055 }
1056
1057 if (!ip_dosourceroute) {
1058 if (ipforwarding) {
1059 char buf[16]; /* aaa.bbb.ccc.ddd\0 */
1060 /*
1061 * Acting as a router, so generate ICMP
1062 */
1063nosourcerouting:
1064 strcpy(buf, inet_ntoa(ip->ip_dst));
1065 log(LOG_WARNING,
1066 "attempted source route from %s to %s\n",
1067 inet_ntoa(ip->ip_src), buf);
1068 type = ICMP_UNREACH;
1069 code = ICMP_UNREACH_SRCFAIL;
1070 goto bad;
1071 } else {
1072 /*
1073 * Not acting as a router, so silently drop.
1074 */
1075 ipstat.ips_cantforward++;
1076 m_freem(m);
1077 return (1);
1078 }
1079 }
1080
1081 /*
1082 * locate outgoing interface
1083 */
1084 (void)memcpy(&ipaddr.sin_addr, cp + off,
1085 sizeof(ipaddr.sin_addr));
1086
1087 if (opt == IPOPT_SSRR) {
1088#define INA struct in_ifaddr *
1089#define SA struct sockaddr *
1090 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
1091 ia = (INA)ifa_ifwithnet((SA)&ipaddr);
1092 } else
1093 ia = ip_rtaddr(ipaddr.sin_addr);
1094 if (ia == 0) {
1095 type = ICMP_UNREACH;
1096 code = ICMP_UNREACH_SRCFAIL;
1097 goto bad;
1098 }
1099 ip->ip_dst = ipaddr.sin_addr;
1100 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
1101 sizeof(struct in_addr));
1102 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1103 /*
1104 * Let ip_intr's mcast routing check handle mcast pkts
1105 */
1106 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
1107 break;
1108
1109 case IPOPT_RR:
1110 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1111 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1112 goto bad;
1113 }
1114 /*
1115 * If no space remains, ignore.
1116 */
1117 off--; /* 0 origin */
1118 if (off > optlen - sizeof(struct in_addr))
1119 break;
1120 (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst,
1121 sizeof(ipaddr.sin_addr));
1122 /*
1123 * locate outgoing interface; if we're the destination,
1124 * use the incoming interface (should be same).
1125 */
1126 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
1127 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
1128 type = ICMP_UNREACH;
1129 code = ICMP_UNREACH_HOST;
1130 goto bad;
1131 }
1132 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
1133 sizeof(struct in_addr));
1134 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1135 break;
1136
1137 case IPOPT_TS:
1138 code = cp - (u_char *)ip;
1139 ipt = (struct ip_timestamp *)cp;
1140 if (ipt->ipt_len < 5)
1141 goto bad;
1142 if (ipt->ipt_ptr > ipt->ipt_len - sizeof(int32_t)) {
1143 if (++ipt->ipt_oflw == 0)
1144 goto bad;
1145 break;
1146 }
1147 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
1148 switch (ipt->ipt_flg) {
1149
1150 case IPOPT_TS_TSONLY:
1151 break;
1152
1153 case IPOPT_TS_TSANDADDR:
1154 if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1155 sizeof(struct in_addr) > ipt->ipt_len)
1156 goto bad;
1157 ipaddr.sin_addr = dst;
1158 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
1159 m->m_pkthdr.rcvif);
1160 if (ia == 0)
1161 continue;
1162 (void)memcpy(sin, &IA_SIN(ia)->sin_addr,
1163 sizeof(struct in_addr));
1164 ipt->ipt_ptr += sizeof(struct in_addr);
1165 break;
1166
1167 case IPOPT_TS_PRESPEC:
1168 if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1169 sizeof(struct in_addr) > ipt->ipt_len)
1170 goto bad;
1171 (void)memcpy(&ipaddr.sin_addr, sin,
1172 sizeof(struct in_addr));
1173 if (ifa_ifwithaddr((SA)&ipaddr) == 0)
1174 continue;
1175 ipt->ipt_ptr += sizeof(struct in_addr);
1176 break;
1177
1178 default:
1179 goto bad;
1180 }
1181 ntime = iptime();
1182 (void)memcpy(cp + ipt->ipt_ptr - 1, &ntime,
1183 sizeof(n_time));
1184 ipt->ipt_ptr += sizeof(n_time);
1185 }
1186 }
1187 if (forward && ipforwarding) {
1188 ip_forward(m, 1);
1189 return (1);
1190 }
1191 return (0);
1192bad:
1193 ip->ip_len -= IP_VHL_HL(ip->ip_vhl) << 2; /* XXX icmp_error adds in hdr length */
1194 icmp_error(m, type, code, 0, 0);
1195 ipstat.ips_badoptions++;
1196 return (1);
1197}
1198
1199/*
1200 * Given address of next destination (final or next hop),
1201 * return internet address info of interface to be used to get there.
1202 */
1203static struct in_ifaddr *
1204ip_rtaddr(dst)
1205 struct in_addr dst;
1206{
1207 register struct sockaddr_in *sin;
1208
1209 sin = (struct sockaddr_in *) &ipforward_rt.ro_dst;
1210
1211 if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) {
1212 if (ipforward_rt.ro_rt) {
1213 RTFREE(ipforward_rt.ro_rt);
1214 ipforward_rt.ro_rt = 0;
1215 }
1216 sin->sin_family = AF_INET;
1217 sin->sin_len = sizeof(*sin);
1218 sin->sin_addr = dst;
1219
1220 rtalloc_ign(&ipforward_rt, RTF_PRCLONING);
1221 }
1222 if (ipforward_rt.ro_rt == 0)
1223 return ((struct in_ifaddr *)0);
1224 return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa);
1225}
1226
1227/*
1228 * Save incoming source route for use in replies,
1229 * to be picked up later by ip_srcroute if the receiver is interested.
1230 */
1231void
1232save_rte(option, dst)
1233 u_char *option;
1234 struct in_addr dst;
1235{
1236 unsigned olen;
1237
1238 olen = option[IPOPT_OLEN];
1239#ifdef DIAGNOSTIC
1240 if (ipprintfs)
1241 printf("save_rte: olen %d\n", olen);
1242#endif
1243 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
1244 return;
1245 bcopy(option, ip_srcrt.srcopt, olen);
1246 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1247 ip_srcrt.dst = dst;
1248}
1249
1250/*
1251 * Retrieve incoming source route for use in replies,
1252 * in the same form used by setsockopt.
1253 * The first hop is placed before the options, will be removed later.
1254 */
1255struct mbuf *
1256ip_srcroute()
1257{
1258 register struct in_addr *p, *q;
1259 register struct mbuf *m;
1260
1261 if (ip_nhops == 0)
1262 return ((struct mbuf *)0);
1263 m = m_get(M_DONTWAIT, MT_HEADER);
1264 if (m == 0)
1265 return ((struct mbuf *)0);
1266
1267#define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1268
1269 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1270 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
1271 OPTSIZ;
1272#ifdef DIAGNOSTIC
1273 if (ipprintfs)
1274 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
1275#endif
1276
1277 /*
1278 * First save first hop for return route
1279 */
1280 p = &ip_srcrt.route[ip_nhops - 1];
1281 *(mtod(m, struct in_addr *)) = *p--;
1282#ifdef DIAGNOSTIC
1283 if (ipprintfs)
1284 printf(" hops %lx", (u_long)ntohl(mtod(m, struct in_addr *)->s_addr));
1285#endif
1286
1287 /*
1288 * Copy option fields and padding (nop) to mbuf.
1289 */
1290 ip_srcrt.nop = IPOPT_NOP;
1291 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
1292 (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr),
1293 &ip_srcrt.nop, OPTSIZ);
1294 q = (struct in_addr *)(mtod(m, caddr_t) +
1295 sizeof(struct in_addr) + OPTSIZ);
1296#undef OPTSIZ
1297 /*
1298 * Record return path as an IP source route,
1299 * reversing the path (pointers are now aligned).
1300 */
1301 while (p >= ip_srcrt.route) {
1302#ifdef DIAGNOSTIC
1303 if (ipprintfs)
1304 printf(" %lx", (u_long)ntohl(q->s_addr));
1305#endif
1306 *q++ = *p--;
1307 }
1308 /*
1309 * Last hop goes to final destination.
1310 */
1311 *q = ip_srcrt.dst;
1312#ifdef DIAGNOSTIC
1313 if (ipprintfs)
1314 printf(" %lx\n", (u_long)ntohl(q->s_addr));
1315#endif
1316 return (m);
1317}
1318
1319/*
1320 * Strip out IP options, at higher
1321 * level protocol in the kernel.
1322 * Second argument is buffer to which options
1323 * will be moved, and return value is their length.
1324 * XXX should be deleted; last arg currently ignored.
1325 */
1326void
1327ip_stripoptions(m, mopt)
1328 register struct mbuf *m;
1329 struct mbuf *mopt;
1330{
1331 register int i;
1332 struct ip *ip = mtod(m, struct ip *);
1333 register caddr_t opts;
1334 int olen;
1335
1336 olen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1337 opts = (caddr_t)(ip + 1);
1338 i = m->m_len - (sizeof (struct ip) + olen);
1339 bcopy(opts + olen, opts, (unsigned)i);
1340 m->m_len -= olen;
1341 if (m->m_flags & M_PKTHDR)
1342 m->m_pkthdr.len -= olen;
1343 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2);
1344}
1345
1346u_char inetctlerrmap[PRC_NCMDS] = {
1347 0, 0, 0, 0,
1348 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1349 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1350 EMSGSIZE, EHOSTUNREACH, 0, 0,
1351 0, 0, 0, 0,
1352 ENOPROTOOPT
1353};
1354
1355/*
1356 * Forward a packet. If some error occurs return the sender
1357 * an icmp packet. Note we can't always generate a meaningful
1358 * icmp message because icmp doesn't have a large enough repertoire
1359 * of codes and types.
1360 *
1361 * If not forwarding, just drop the packet. This could be confusing
1362 * if ipforwarding was zero but some routing protocol was advancing
1363 * us as a gateway to somewhere. However, we must let the routing
1364 * protocol deal with that.
1365 *
1366 * The srcrt parameter indicates whether the packet is being forwarded
1367 * via a source route.
1368 */
1369static void
1370ip_forward(m, srcrt)
1371 struct mbuf *m;
1372 int srcrt;
1373{
1374 register struct ip *ip = mtod(m, struct ip *);
1375 register struct sockaddr_in *sin;
1376 register struct rtentry *rt;
1377 int error, type = 0, code = 0;
1378 struct mbuf *mcopy;
1379 n_long dest;
1380 struct ifnet *destifp;
1381
1382 dest = 0;
1383#ifdef DIAGNOSTIC
1384 if (ipprintfs)
1385 printf("forward: src %lx dst %lx ttl %x\n",
1386 (u_long)ip->ip_src.s_addr, (u_long)ip->ip_dst.s_addr,
1387 ip->ip_ttl);
1388#endif
1389
1390
1391 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1392 ipstat.ips_cantforward++;
1393 m_freem(m);
1394 return;
1395 }
1396 HTONS(ip->ip_id);
1397#ifdef IPSTEALTH
1398 if (!ipstealth) {
1399#endif
1400 if (ip->ip_ttl <= IPTTLDEC) {
1401 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
1402 dest, 0);
1403 return;
1404 }
1405 ip->ip_ttl -= IPTTLDEC;
1406#ifdef IPSTEALTH
1407 }
1408#endif
1409
1410 sin = (struct sockaddr_in *)&ipforward_rt.ro_dst;
1411 if ((rt = ipforward_rt.ro_rt) == 0 ||
1412 ip->ip_dst.s_addr != sin->sin_addr.s_addr) {
1413 if (ipforward_rt.ro_rt) {
1414 RTFREE(ipforward_rt.ro_rt);
1415 ipforward_rt.ro_rt = 0;
1416 }
1417 sin->sin_family = AF_INET;
1418 sin->sin_len = sizeof(*sin);
1419 sin->sin_addr = ip->ip_dst;
1420
1421 rtalloc_ign(&ipforward_rt, RTF_PRCLONING);
1422 if (ipforward_rt.ro_rt == 0) {
1423 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
1424 return;
1425 }
1426 rt = ipforward_rt.ro_rt;
1427 }
1428
1429 /*
1430 * Save at most 64 bytes of the packet in case
1431 * we need to generate an ICMP message to the src.
1432 */
1433 mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64));
1434
1435 /*
1436 * If forwarding packet using same interface that it came in on,
1437 * perhaps should send a redirect to sender to shortcut a hop.
1438 * Only send redirect if source is sending directly to us,
1439 * and if packet was not source routed (or has any options).
1440 * Also, don't send redirect if forwarding using a default route
1441 * or a route modified by a redirect.
1442 */
1443#define satosin(sa) ((struct sockaddr_in *)(sa))
1444 if (rt->rt_ifp == m->m_pkthdr.rcvif &&
1445 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1446 satosin(rt_key(rt))->sin_addr.s_addr != 0 &&
1447 ipsendredirects && !srcrt) {
1448#define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa))
1449 u_long src = ntohl(ip->ip_src.s_addr);
1450
1451 if (RTA(rt) &&
1452 (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1453 if (rt->rt_flags & RTF_GATEWAY)
1454 dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1455 else
1456 dest = ip->ip_dst.s_addr;
1457 /* Router requirements says to only send host redirects */
1458 type = ICMP_REDIRECT;
1459 code = ICMP_REDIRECT_HOST;
1460#ifdef DIAGNOSTIC
1461 if (ipprintfs)
1462 printf("redirect (%d) to %lx\n", code, (u_long)dest);
1463#endif
1464 }
1465 }
1466
1467 error = ip_output(m, (struct mbuf *)0, &ipforward_rt,
1468 IP_FORWARDING, 0);
1469 if (error)
1470 ipstat.ips_cantforward++;
1471 else {
1472 ipstat.ips_forward++;
1473 if (type)
1474 ipstat.ips_redirectsent++;
1475 else {
1476 if (mcopy) {
1477 ipflow_create(&ipforward_rt, mcopy);
1478 m_freem(mcopy);
1479 }
1480 return;
1481 }
1482 }
1483 if (mcopy == NULL)
1484 return;
1485 destifp = NULL;
1486
1487 switch (error) {
1488
1489 case 0: /* forwarded, but need redirect */
1490 /* type, code set above */
1491 break;
1492
1493 case ENETUNREACH: /* shouldn't happen, checked above */
1494 case EHOSTUNREACH:
1495 case ENETDOWN:
1496 case EHOSTDOWN:
1497 default:
1498 type = ICMP_UNREACH;
1499 code = ICMP_UNREACH_HOST;
1500 break;
1501
1502 case EMSGSIZE:
1503 type = ICMP_UNREACH;
1504 code = ICMP_UNREACH_NEEDFRAG;
1505 if (ipforward_rt.ro_rt)
1506 destifp = ipforward_rt.ro_rt->rt_ifp;
1507 ipstat.ips_cantfrag++;
1508 break;
1509
1510 case ENOBUFS:
1511 type = ICMP_SOURCEQUENCH;
1512 code = 0;
1513 break;
1514 }
1515 icmp_error(mcopy, type, code, dest, destifp);
1516}
1517
1518void
1519ip_savecontrol(inp, mp, ip, m)
1520 register struct inpcb *inp;
1521 register struct mbuf **mp;
1522 register struct ip *ip;
1523 register struct mbuf *m;
1524{
1525 if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1526 struct timeval tv;
1527
1528 microtime(&tv);
1529 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1530 SCM_TIMESTAMP, SOL_SOCKET);
1531 if (*mp)
1532 mp = &(*mp)->m_next;
1533 }
1534 if (inp->inp_flags & INP_RECVDSTADDR) {
1535 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
1536 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1537 if (*mp)
1538 mp = &(*mp)->m_next;
1539 }
1540#ifdef notyet
1541 /* XXX
1542 * Moving these out of udp_input() made them even more broken
1543 * than they already were.
1544 */
1545 /* options were tossed already */
1546 if (inp->inp_flags & INP_RECVOPTS) {
1547 *mp = sbcreatecontrol((caddr_t) opts_deleted_above,
1548 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
1549 if (*mp)
1550 mp = &(*mp)->m_next;
1551 }
1552 /* ip_srcroute doesn't do what we want here, need to fix */
1553 if (inp->inp_flags & INP_RECVRETOPTS) {
1554 *mp = sbcreatecontrol((caddr_t) ip_srcroute(),
1555 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
1556 if (*mp)
1557 mp = &(*mp)->m_next;
1558 }
1559#endif
1560 if (inp->inp_flags & INP_RECVIF) {
1561 struct ifnet *ifp;
1562 struct sdlbuf {
1563 struct sockaddr_dl sdl;
1564 u_char pad[32];
1565 } sdlbuf;
1566 struct sockaddr_dl *sdp;
1567 struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
1568
1569 if (((ifp = m->m_pkthdr.rcvif))
1570 && ( ifp->if_index && (ifp->if_index <= if_index))) {
1571 sdp = (struct sockaddr_dl *)(ifnet_addrs
1572 [ifp->if_index - 1]->ifa_addr);
1573 /*
1574 * Change our mind and don't try copy.
1575 */
1576 if ((sdp->sdl_family != AF_LINK)
1577 || (sdp->sdl_len > sizeof(sdlbuf))) {
1578 goto makedummy;
1579 }
1580 bcopy(sdp, sdl2, sdp->sdl_len);
1581 } else {
1582makedummy:
1583 sdl2->sdl_len
1584 = offsetof(struct sockaddr_dl, sdl_data[0]);
1585 sdl2->sdl_family = AF_LINK;
1586 sdl2->sdl_index = 0;
1587 sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1588 }
1589 *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len,
1590 IP_RECVIF, IPPROTO_IP);
1591 if (*mp)
1592 mp = &(*mp)->m_next;
1593 }
1594}
1595
1596int
1597ip_rsvp_init(struct socket *so)
1598{
1599 if (so->so_type != SOCK_RAW ||
1600 so->so_proto->pr_protocol != IPPROTO_RSVP)
1601 return EOPNOTSUPP;
1602
1603 if (ip_rsvpd != NULL)
1604 return EADDRINUSE;
1605
1606 ip_rsvpd = so;
1607 /*
1608 * This may seem silly, but we need to be sure we don't over-increment
1609 * the RSVP counter, in case something slips up.
1610 */
1611 if (!ip_rsvp_on) {
1612 ip_rsvp_on = 1;
1613 rsvp_on++;
1614 }
1615
1616 return 0;
1617}
1618
1619int
1620ip_rsvp_done(void)
1621{
1622 ip_rsvpd = NULL;
1623 /*
1624 * This may seem silly, but we need to be sure we don't over-decrement
1625 * the RSVP counter, in case something slips up.
1626 */
1627 if (ip_rsvp_on) {
1628 ip_rsvp_on = 0;
1629 rsvp_on--;
1630 }
1631 return 0;
1632}
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 * $Id: ip_input.c,v 1.117 1999/04/20 13:32:05 peter Exp $
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
45#include <stddef.h>
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/mbuf.h>
50#include <sys/malloc.h>
51#include <sys/domain.h>
52#include <sys/protosw.h>
53#include <sys/socket.h>
54#include <sys/time.h>
55#include <sys/kernel.h>
56#include <sys/syslog.h>
57#include <sys/sysctl.h>
58
59#include <net/if.h>
60#include <net/if_var.h>
61#include <net/if_dl.h>
62#include <net/route.h>
63#include <net/netisr.h>
64
65#include <netinet/in.h>
66#include <netinet/in_systm.h>
67#include <netinet/in_var.h>
68#include <netinet/ip.h>
69#include <netinet/in_pcb.h>
70#include <netinet/ip_var.h>
71#include <netinet/ip_icmp.h>
72#include <machine/in_cksum.h>
73
74#include <sys/socketvar.h>
75
76#include <netinet/ip_fw.h>
77
78#ifdef DUMMYNET
79#include <netinet/ip_dummynet.h>
80#endif
81
82int rsvp_on = 0;
83static int ip_rsvp_on;
84struct socket *ip_rsvpd;
85
86int ipforwarding = 0;
87SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW,
88 &ipforwarding, 0, "Enable IP forwarding between interfaces");
89
90static int ipsendredirects = 1; /* XXX */
91SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW,
92 &ipsendredirects, 0, "Enable sending IP redirects");
93
94int ip_defttl = IPDEFTTL;
95SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW,
96 &ip_defttl, 0, "Maximum TTL on IP packets");
97
98static int ip_dosourceroute = 0;
99SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW,
100 &ip_dosourceroute, 0, "Enable forwarding source routed IP packets");
101
102static int ip_acceptsourceroute = 0;
103SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute,
104 CTLFLAG_RW, &ip_acceptsourceroute, 0,
105 "Enable accepting source routed IP packets");
106#ifdef DIAGNOSTIC
107static int ipprintfs = 0;
108#endif
109
110extern struct domain inetdomain;
111extern struct protosw inetsw[];
112u_char ip_protox[IPPROTO_MAX];
113static int ipqmaxlen = IFQ_MAXLEN;
114struct in_ifaddrhead in_ifaddrhead; /* first inet address */
115struct ifqueue ipintrq;
116SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RD,
117 &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue");
118SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD,
119 &ipintrq.ifq_drops, 0, "Number of packets dropped from the IP input queue");
120
121struct ipstat ipstat;
122SYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RD,
123 &ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)");
124
125/* Packet reassembly stuff */
126#define IPREASS_NHASH_LOG2 6
127#define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2)
128#define IPREASS_HMASK (IPREASS_NHASH - 1)
129#define IPREASS_HASH(x,y) \
130 (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
131
132static struct ipq ipq[IPREASS_NHASH];
133static int nipq = 0; /* total # of reass queues */
134static int maxnipq;
135
136#ifdef IPCTL_DEFMTU
137SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
138 &ip_mtu, 0, "Default MTU");
139#endif
140
141#ifdef IPSTEALTH
142static int ipstealth = 0;
143SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW,
144 &ipstealth, 0, "");
145#endif
146
147
148/* Firewall hooks */
149ip_fw_chk_t *ip_fw_chk_ptr;
150ip_fw_ctl_t *ip_fw_ctl_ptr;
151
152#ifdef DUMMYNET
153ip_dn_ctl_t *ip_dn_ctl_ptr;
154#endif
155
156#if defined(IPFILTER_LKM) || defined(IPFILTER)
157int iplattach __P((void));
158int (*fr_checkp) __P((struct ip *, int, struct ifnet *, int, struct mbuf **)) = NULL;
159#endif
160
161
162/*
163 * We need to save the IP options in case a protocol wants to respond
164 * to an incoming packet over the same route if the packet got here
165 * using IP source routing. This allows connection establishment and
166 * maintenance when the remote end is on a network that is not known
167 * to us.
168 */
169static int ip_nhops = 0;
170static struct ip_srcrt {
171 struct in_addr dst; /* final destination */
172 char nop; /* one NOP to align */
173 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */
174 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
175} ip_srcrt;
176
177#ifdef IPDIVERT
178/*
179 * Shared variable between ip_input() and ip_reass() to communicate
180 * about which packets, once assembled from fragments, get diverted,
181 * and to which port.
182 */
183static u_short frag_divert_port;
184#endif
185
186struct sockaddr_in *ip_fw_fwd_addr;
187
188static void save_rte __P((u_char *, struct in_addr));
189static int ip_dooptions __P((struct mbuf *));
190static void ip_forward __P((struct mbuf *, int));
191static void ip_freef __P((struct ipq *));
192static struct ip *
193 ip_reass __P((struct mbuf *, struct ipq *, struct ipq *));
194static struct in_ifaddr *
195 ip_rtaddr __P((struct in_addr));
196static void ipintr __P((void));
197/*
198 * IP initialization: fill in IP protocol switch table.
199 * All protocols not implemented in kernel go to raw IP protocol handler.
200 */
201void
202ip_init()
203{
204 register struct protosw *pr;
205 register int i;
206
207 TAILQ_INIT(&in_ifaddrhead);
208 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
209 if (pr == 0)
210 panic("ip_init");
211 for (i = 0; i < IPPROTO_MAX; i++)
212 ip_protox[i] = pr - inetsw;
213 for (pr = inetdomain.dom_protosw;
214 pr < inetdomain.dom_protoswNPROTOSW; pr++)
215 if (pr->pr_domain->dom_family == PF_INET &&
216 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
217 ip_protox[pr->pr_protocol] = pr - inetsw;
218
219 for (i = 0; i < IPREASS_NHASH; i++)
220 ipq[i].next = ipq[i].prev = &ipq[i];
221
222 maxnipq = nmbclusters/4;
223
224 ip_id = time_second & 0xffff;
225 ipintrq.ifq_maxlen = ipqmaxlen;
226#ifdef IPFILTER
227 iplattach();
228#endif
229
230}
231
232static struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
233static struct route ipforward_rt;
234
235/*
236 * Ip input routine. Checksum and byte swap header. If fragmented
237 * try to reassemble. Process options. Pass to next level.
238 */
239void
240ip_input(struct mbuf *m)
241{
242 struct ip *ip;
243 struct ipq *fp;
244 struct in_ifaddr *ia;
245 int i, hlen, mff;
246 u_short sum;
247#ifndef IPDIVERT /* dummy variable for the firewall code to play with */
248 u_short ip_divert_cookie = 0 ;
249#endif
250 struct ip_fw_chain *rule = NULL ;
251
252#if defined(IPFIREWALL) && defined(DUMMYNET)
253 /*
254 * dummynet packet are prepended a vestigial mbuf with
255 * m_type = MT_DUMMYNET and m_data pointing to the matching
256 * rule.
257 */
258 if (m->m_type == MT_DUMMYNET) {
259 struct mbuf *m0 = m ;
260 rule = (struct ip_fw_chain *)(m->m_data) ;
261 m = m->m_next ;
262 free(m0, M_IPFW);
263 ip = mtod(m, struct ip *);
264 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
265 goto iphack ;
266 } else
267 rule = NULL ;
268#endif
269
270#ifdef DIAGNOSTIC
271 if (m == NULL || (m->m_flags & M_PKTHDR) == 0)
272 panic("ip_input no HDR");
273#endif
274 ipstat.ips_total++;
275
276 if (m->m_pkthdr.len < sizeof(struct ip))
277 goto tooshort;
278
279 if (m->m_len < sizeof (struct ip) &&
280 (m = m_pullup(m, sizeof (struct ip))) == 0) {
281 ipstat.ips_toosmall++;
282 return;
283 }
284 ip = mtod(m, struct ip *);
285
286 if (IP_VHL_V(ip->ip_vhl) != IPVERSION) {
287 ipstat.ips_badvers++;
288 goto bad;
289 }
290
291 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
292 if (hlen < sizeof(struct ip)) { /* minimum header length */
293 ipstat.ips_badhlen++;
294 goto bad;
295 }
296 if (hlen > m->m_len) {
297 if ((m = m_pullup(m, hlen)) == 0) {
298 ipstat.ips_badhlen++;
299 return;
300 }
301 ip = mtod(m, struct ip *);
302 }
303 if (hlen == sizeof(struct ip)) {
304 sum = in_cksum_hdr(ip);
305 } else {
306 sum = in_cksum(m, hlen);
307 }
308 if (sum) {
309 ipstat.ips_badsum++;
310 goto bad;
311 }
312
313 /*
314 * Convert fields to host representation.
315 */
316 NTOHS(ip->ip_len);
317 if (ip->ip_len < hlen) {
318 ipstat.ips_badlen++;
319 goto bad;
320 }
321 NTOHS(ip->ip_id);
322 NTOHS(ip->ip_off);
323
324 /*
325 * Check that the amount of data in the buffers
326 * is as at least much as the IP header would have us expect.
327 * Trim mbufs if longer than we expect.
328 * Drop packet if shorter than we expect.
329 */
330 if (m->m_pkthdr.len < ip->ip_len) {
331tooshort:
332 ipstat.ips_tooshort++;
333 goto bad;
334 }
335 if (m->m_pkthdr.len > ip->ip_len) {
336 if (m->m_len == m->m_pkthdr.len) {
337 m->m_len = ip->ip_len;
338 m->m_pkthdr.len = ip->ip_len;
339 } else
340 m_adj(m, ip->ip_len - m->m_pkthdr.len);
341 }
342 /*
343 * IpHack's section.
344 * Right now when no processing on packet has done
345 * and it is still fresh out of network we do our black
346 * deals with it.
347 * - Firewall: deny/allow/divert
348 * - Xlate: translate packet's addr/port (NAT).
349 * - Pipe: pass pkt through dummynet.
350 * - Wrap: fake packet's addr/port <unimpl.>
351 * - Encapsulate: put it in another IP and send out. <unimp.>
352 */
353
354#if defined(IPFIREWALL) && defined(DUMMYNET)
355iphack:
356#endif
357#if defined(IPFILTER) || defined(IPFILTER_LKM)
358 /*
359 * Check if we want to allow this packet to be processed.
360 * Consider it to be bad if not.
361 */
362 if (fr_checkp) {
363 struct mbuf *m1 = m;
364
365 if ((*fr_checkp)(ip, hlen, m->m_pkthdr.rcvif, 0, &m1) || !m1)
366 return;
367 ip = mtod(m = m1, struct ip *);
368 }
369#endif
370 if (ip_fw_chk_ptr) {
371#ifdef IPFIREWALL_FORWARD
372 /*
373 * If we've been forwarded from the output side, then
374 * skip the firewall a second time
375 */
376 if (ip_fw_fwd_addr)
377 goto ours;
378#endif /* IPFIREWALL_FORWARD */
379 i = (*ip_fw_chk_ptr)(&ip, hlen, NULL, &ip_divert_cookie,
380 &m, &rule, &ip_fw_fwd_addr);
381 /*
382 * see the comment in ip_output for the return values
383 * produced by the firewall.
384 */
385 if (!m) /* packet discarded by firewall */
386 return ;
387 if (i == 0 && ip_fw_fwd_addr == NULL) /* common case */
388 goto pass ;
389#ifdef DUMMYNET
390 if (i & 0x10000) {
391 /* send packet to the appropriate pipe */
392 dummynet_io(i&0xffff,DN_TO_IP_IN,m,NULL,NULL,0, rule);
393 return ;
394 }
395#endif
396#ifdef IPDIVERT
397 if (i > 0 && i < 0x10000) {
398 /* Divert packet */
399 frag_divert_port = i & 0xffff ;
400 goto ours;
401 }
402#endif
403#ifdef IPFIREWALL_FORWARD
404 if (i == 0 && ip_fw_fwd_addr != NULL)
405 goto pass ;
406#endif
407 /*
408 * if we get here, the packet must be dropped
409 */
410 m_freem(m);
411 return;
412 }
413pass:
414
415 /*
416 * Process options and, if not destined for us,
417 * ship it on. ip_dooptions returns 1 when an
418 * error was detected (causing an icmp message
419 * to be sent and the original packet to be freed).
420 */
421 ip_nhops = 0; /* for source routed packets */
422 if (hlen > sizeof (struct ip) && ip_dooptions(m)) {
423#ifdef IPFIREWALL_FORWARD
424 ip_fw_fwd_addr = NULL;
425#endif
426 return;
427 }
428
429 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
430 * matter if it is destined to another node, or whether it is
431 * a multicast one, RSVP wants it! and prevents it from being forwarded
432 * anywhere else. Also checks if the rsvp daemon is running before
433 * grabbing the packet.
434 */
435 if (rsvp_on && ip->ip_p==IPPROTO_RSVP)
436 goto ours;
437
438 /*
439 * Check our list of addresses, to see if the packet is for us.
440 * If we don't have any addresses, assume any unicast packet
441 * we receive might be for us (and let the upper layers deal
442 * with it).
443 */
444 if (TAILQ_EMPTY(&in_ifaddrhead) &&
445 (m->m_flags & (M_MCAST|M_BCAST)) == 0)
446 goto ours;
447
448 for (ia = TAILQ_FIRST(&in_ifaddrhead); ia;
449 ia = TAILQ_NEXT(ia, ia_link)) {
450#define satosin(sa) ((struct sockaddr_in *)(sa))
451
452#ifdef BOOTP_COMPAT
453 if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY)
454 goto ours;
455#endif
456#ifdef IPFIREWALL_FORWARD
457 /*
458 * If the addr to forward to is one of ours, we pretend to
459 * be the destination for this packet.
460 */
461 if (ip_fw_fwd_addr == NULL) {
462 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr)
463 goto ours;
464 } else if (IA_SIN(ia)->sin_addr.s_addr ==
465 ip_fw_fwd_addr->sin_addr.s_addr)
466 goto ours;
467#else
468 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr)
469 goto ours;
470#endif
471 if (ia->ia_ifp && ia->ia_ifp->if_flags & IFF_BROADCAST) {
472 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
473 ip->ip_dst.s_addr)
474 goto ours;
475 if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr)
476 goto ours;
477 }
478 }
479 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
480 struct in_multi *inm;
481 if (ip_mrouter) {
482 /*
483 * If we are acting as a multicast router, all
484 * incoming multicast packets are passed to the
485 * kernel-level multicast forwarding function.
486 * The packet is returned (relatively) intact; if
487 * ip_mforward() returns a non-zero value, the packet
488 * must be discarded, else it may be accepted below.
489 *
490 * (The IP ident field is put in the same byte order
491 * as expected when ip_mforward() is called from
492 * ip_output().)
493 */
494 ip->ip_id = htons(ip->ip_id);
495 if (ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) {
496 ipstat.ips_cantforward++;
497 m_freem(m);
498 return;
499 }
500 ip->ip_id = ntohs(ip->ip_id);
501
502 /*
503 * The process-level routing demon needs to receive
504 * all multicast IGMP packets, whether or not this
505 * host belongs to their destination groups.
506 */
507 if (ip->ip_p == IPPROTO_IGMP)
508 goto ours;
509 ipstat.ips_forward++;
510 }
511 /*
512 * See if we belong to the destination multicast group on the
513 * arrival interface.
514 */
515 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
516 if (inm == NULL) {
517 ipstat.ips_notmember++;
518 m_freem(m);
519 return;
520 }
521 goto ours;
522 }
523 if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
524 goto ours;
525 if (ip->ip_dst.s_addr == INADDR_ANY)
526 goto ours;
527
528 /*
529 * Not for us; forward if possible and desirable.
530 */
531 if (ipforwarding == 0) {
532 ipstat.ips_cantforward++;
533 m_freem(m);
534 } else
535 ip_forward(m, 0);
536#ifdef IPFIREWALL_FORWARD
537 ip_fw_fwd_addr = NULL;
538#endif
539 return;
540
541ours:
542
543 /*
544 * If offset or IP_MF are set, must reassemble.
545 * Otherwise, nothing need be done.
546 * (We could look in the reassembly queue to see
547 * if the packet was previously fragmented,
548 * but it's not worth the time; just let them time out.)
549 */
550 if (ip->ip_off & (IP_MF | IP_OFFMASK | IP_RF)) {
551 if (m->m_flags & M_EXT) { /* XXX */
552 if ((m = m_pullup(m, hlen)) == 0) {
553 ipstat.ips_toosmall++;
554#ifdef IPDIVERT
555 frag_divert_port = 0;
556 ip_divert_cookie = 0;
557#endif
558#ifdef IPFIREWALL_FORWARD
559 ip_fw_fwd_addr = NULL;
560#endif
561 return;
562 }
563 ip = mtod(m, struct ip *);
564 }
565 sum = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
566 /*
567 * Look for queue of fragments
568 * of this datagram.
569 */
570 for (fp = ipq[sum].next; fp != &ipq[sum]; fp = fp->next)
571 if (ip->ip_id == fp->ipq_id &&
572 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
573 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
574 ip->ip_p == fp->ipq_p)
575 goto found;
576
577 fp = 0;
578
579 /* check if there's a place for the new queue */
580 if (nipq > maxnipq) {
581 /*
582 * drop something from the tail of the current queue
583 * before proceeding further
584 */
585 if (ipq[sum].prev == &ipq[sum]) { /* gak */
586 for (i = 0; i < IPREASS_NHASH; i++) {
587 if (ipq[i].prev != &ipq[i]) {
588 ip_freef(ipq[i].prev);
589 break;
590 }
591 }
592 } else
593 ip_freef(ipq[sum].prev);
594 }
595found:
596 /*
597 * Adjust ip_len to not reflect header,
598 * set ip_mff if more fragments are expected,
599 * convert offset of this to bytes.
600 */
601 ip->ip_len -= hlen;
602 mff = (ip->ip_off & IP_MF) != 0;
603 if (mff) {
604 /*
605 * Make sure that fragments have a data length
606 * that's a non-zero multiple of 8 bytes.
607 */
608 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
609 ipstat.ips_toosmall++; /* XXX */
610 goto bad;
611 }
612 m->m_flags |= M_FRAG;
613 }
614 ip->ip_off <<= 3;
615
616 /*
617 * If datagram marked as having more fragments
618 * or if this is not the first fragment,
619 * attempt reassembly; if it succeeds, proceed.
620 */
621 if (mff || ip->ip_off) {
622 ipstat.ips_fragments++;
623 m->m_pkthdr.header = ip;
624 ip = ip_reass(m, fp, &ipq[sum]);
625 if (ip == 0) {
626#ifdef IPFIREWALL_FORWARD
627 ip_fw_fwd_addr = NULL;
628#endif
629 return;
630 }
631 /* Get the length of the reassembled packets header */
632 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
633 ipstat.ips_reassembled++;
634 m = dtom(ip);
635#ifdef IPDIVERT
636 if (frag_divert_port) {
637 ip->ip_len += hlen;
638 HTONS(ip->ip_len);
639 HTONS(ip->ip_off);
640 HTONS(ip->ip_id);
641 ip->ip_sum = 0;
642 ip->ip_sum = in_cksum_hdr(ip);
643 NTOHS(ip->ip_id);
644 NTOHS(ip->ip_off);
645 NTOHS(ip->ip_len);
646 ip->ip_len -= hlen;
647 }
648#endif
649 } else
650 if (fp)
651 ip_freef(fp);
652 } else
653 ip->ip_len -= hlen;
654
655#ifdef IPDIVERT
656 /*
657 * Divert reassembled packets to the divert protocol if required
658 * If divert port is null then cookie should be too,
659 * so we shouldn't need to clear them here. Assume ip_divert does so.
660 */
661 if (frag_divert_port) {
662 ipstat.ips_delivered++;
663 ip_divert_port = frag_divert_port;
664 frag_divert_port = 0;
665 (*inetsw[ip_protox[IPPROTO_DIVERT]].pr_input)(m, hlen);
666 return;
667 }
668
669 /* Don't let packets divert themselves */
670 if (ip->ip_p == IPPROTO_DIVERT) {
671 ipstat.ips_noproto++;
672 goto bad;
673 }
674
675#endif
676
677 /*
678 * Switch out to protocol's input routine.
679 */
680 ipstat.ips_delivered++;
681 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
682#ifdef IPFIREWALL_FORWARD
683 ip_fw_fwd_addr = NULL; /* tcp needed it */
684#endif
685 return;
686bad:
687#ifdef IPFIREWALL_FORWARD
688 ip_fw_fwd_addr = NULL;
689#endif
690 m_freem(m);
691}
692
693/*
694 * IP software interrupt routine - to go away sometime soon
695 */
696static void
697ipintr(void)
698{
699 int s;
700 struct mbuf *m;
701
702 while(1) {
703 s = splimp();
704 IF_DEQUEUE(&ipintrq, m);
705 splx(s);
706 if (m == 0)
707 return;
708 ip_input(m);
709 }
710}
711
712NETISR_SET(NETISR_IP, ipintr);
713
714/*
715 * Take incoming datagram fragment and try to
716 * reassemble it into whole datagram. If a chain for
717 * reassembly of this datagram already exists, then it
718 * is given as fp; otherwise have to make a chain.
719 */
720static struct ip *
721ip_reass(m, fp, where)
722 register struct mbuf *m;
723 register struct ipq *fp;
724 struct ipq *where;
725{
726 struct ip *ip = mtod(m, struct ip *);
727 register struct mbuf *p = 0, *q, *nq;
728 struct mbuf *t;
729 int hlen = IP_VHL_HL(ip->ip_vhl) << 2;
730 int i, next;
731
732 /*
733 * Presence of header sizes in mbufs
734 * would confuse code below.
735 */
736 m->m_data += hlen;
737 m->m_len -= hlen;
738
739 /*
740 * If first fragment to arrive, create a reassembly queue.
741 */
742 if (fp == 0) {
743 if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)
744 goto dropfrag;
745 fp = mtod(t, struct ipq *);
746 insque(fp, where);
747 nipq++;
748 fp->ipq_ttl = IPFRAGTTL;
749 fp->ipq_p = ip->ip_p;
750 fp->ipq_id = ip->ip_id;
751 fp->ipq_src = ip->ip_src;
752 fp->ipq_dst = ip->ip_dst;
753 fp->ipq_frags = m;
754 m->m_nextpkt = NULL;
755#ifdef IPDIVERT
756 fp->ipq_divert = 0;
757 fp->ipq_div_cookie = 0;
758#endif
759 goto inserted;
760 }
761
762#define GETIP(m) ((struct ip*)((m)->m_pkthdr.header))
763
764 /*
765 * Find a segment which begins after this one does.
766 */
767 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
768 if (GETIP(q)->ip_off > ip->ip_off)
769 break;
770
771 /*
772 * If there is a preceding segment, it may provide some of
773 * our data already. If so, drop the data from the incoming
774 * segment. If it provides all of our data, drop us, otherwise
775 * stick new segment in the proper place.
776 */
777 if (p) {
778 i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
779 if (i > 0) {
780 if (i >= ip->ip_len)
781 goto dropfrag;
782 m_adj(dtom(ip), i);
783 ip->ip_off += i;
784 ip->ip_len -= i;
785 }
786 m->m_nextpkt = p->m_nextpkt;
787 p->m_nextpkt = m;
788 } else {
789 m->m_nextpkt = fp->ipq_frags;
790 fp->ipq_frags = m;
791 }
792
793 /*
794 * While we overlap succeeding segments trim them or,
795 * if they are completely covered, dequeue them.
796 */
797 for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
798 q = nq) {
799 i = (ip->ip_off + ip->ip_len) -
800 GETIP(q)->ip_off;
801 if (i < GETIP(q)->ip_len) {
802 GETIP(q)->ip_len -= i;
803 GETIP(q)->ip_off += i;
804 m_adj(q, i);
805 break;
806 }
807 nq = q->m_nextpkt;
808 m->m_nextpkt = nq;
809 m_freem(q);
810 }
811
812inserted:
813
814#ifdef IPDIVERT
815 /*
816 * Any fragment diverting causes the whole packet to divert
817 */
818 if (frag_divert_port) {
819 fp->ipq_divert = frag_divert_port;
820 fp->ipq_div_cookie = ip_divert_cookie;
821 }
822 frag_divert_port = 0;
823 ip_divert_cookie = 0;
824#endif
825
826 /*
827 * Check for complete reassembly.
828 */
829 next = 0;
830 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
831 if (GETIP(q)->ip_off != next)
832 return (0);
833 next += GETIP(q)->ip_len;
834 }
835 /* Make sure the last packet didn't have the IP_MF flag */
836 if (p->m_flags & M_FRAG)
837 return (0);
838
839 /*
840 * Reassembly is complete. Make sure the packet is a sane size.
841 */
842 q = fp->ipq_frags;
843 ip = GETIP(q);
844 if (next + (IP_VHL_HL(ip->ip_vhl) << 2) > IP_MAXPACKET) {
845 ipstat.ips_toolong++;
846 ip_freef(fp);
847 return (0);
848 }
849
850 /*
851 * Concatenate fragments.
852 */
853 m = q;
854 t = m->m_next;
855 m->m_next = 0;
856 m_cat(m, t);
857 nq = q->m_nextpkt;
858 q->m_nextpkt = 0;
859 for (q = nq; q != NULL; q = nq) {
860 nq = q->m_nextpkt;
861 q->m_nextpkt = NULL;
862 m_cat(m, q);
863 }
864
865#ifdef IPDIVERT
866 /*
867 * extract divert port for packet, if any
868 */
869 frag_divert_port = fp->ipq_divert;
870 ip_divert_cookie = fp->ipq_div_cookie;
871#endif
872
873 /*
874 * Create header for new ip packet by
875 * modifying header of first packet;
876 * dequeue and discard fragment reassembly header.
877 * Make header visible.
878 */
879 ip->ip_len = next;
880 ip->ip_src = fp->ipq_src;
881 ip->ip_dst = fp->ipq_dst;
882 remque(fp);
883 nipq--;
884 (void) m_free(dtom(fp));
885 m->m_len += (IP_VHL_HL(ip->ip_vhl) << 2);
886 m->m_data -= (IP_VHL_HL(ip->ip_vhl) << 2);
887 /* some debugging cruft by sklower, below, will go away soon */
888 if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
889 register int plen = 0;
890 for (t = m; m; m = m->m_next)
891 plen += m->m_len;
892 t->m_pkthdr.len = plen;
893 }
894 return (ip);
895
896dropfrag:
897#ifdef IPDIVERT
898 frag_divert_port = 0;
899 ip_divert_cookie = 0;
900#endif
901 ipstat.ips_fragdropped++;
902 m_freem(m);
903 return (0);
904
905#undef GETIP
906}
907
908/*
909 * Free a fragment reassembly header and all
910 * associated datagrams.
911 */
912static void
913ip_freef(fp)
914 struct ipq *fp;
915{
916 register struct mbuf *q;
917
918 while (fp->ipq_frags) {
919 q = fp->ipq_frags;
920 fp->ipq_frags = q->m_nextpkt;
921 m_freem(q);
922 }
923 remque(fp);
924 (void) m_free(dtom(fp));
925 nipq--;
926}
927
928/*
929 * IP timer processing;
930 * if a timer expires on a reassembly
931 * queue, discard it.
932 */
933void
934ip_slowtimo()
935{
936 register struct ipq *fp;
937 int s = splnet();
938 int i;
939
940 for (i = 0; i < IPREASS_NHASH; i++) {
941 fp = ipq[i].next;
942 if (fp == 0)
943 continue;
944 while (fp != &ipq[i]) {
945 --fp->ipq_ttl;
946 fp = fp->next;
947 if (fp->prev->ipq_ttl == 0) {
948 ipstat.ips_fragtimeout++;
949 ip_freef(fp->prev);
950 }
951 }
952 }
953 ipflow_slowtimo();
954 splx(s);
955}
956
957/*
958 * Drain off all datagram fragments.
959 */
960void
961ip_drain()
962{
963 int i;
964
965 for (i = 0; i < IPREASS_NHASH; i++) {
966 while (ipq[i].next != &ipq[i]) {
967 ipstat.ips_fragdropped++;
968 ip_freef(ipq[i].next);
969 }
970 }
971 in_rtqdrain();
972}
973
974/*
975 * Do option processing on a datagram,
976 * possibly discarding it if bad options are encountered,
977 * or forwarding it if source-routed.
978 * Returns 1 if packet has been forwarded/freed,
979 * 0 if the packet should be processed further.
980 */
981static int
982ip_dooptions(m)
983 struct mbuf *m;
984{
985 register struct ip *ip = mtod(m, struct ip *);
986 register u_char *cp;
987 register struct ip_timestamp *ipt;
988 register struct in_ifaddr *ia;
989 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
990 struct in_addr *sin, dst;
991 n_time ntime;
992
993 dst = ip->ip_dst;
994 cp = (u_char *)(ip + 1);
995 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
996 for (; cnt > 0; cnt -= optlen, cp += optlen) {
997 opt = cp[IPOPT_OPTVAL];
998 if (opt == IPOPT_EOL)
999 break;
1000 if (opt == IPOPT_NOP)
1001 optlen = 1;
1002 else {
1003 optlen = cp[IPOPT_OLEN];
1004 if (optlen <= 0 || optlen > cnt) {
1005 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1006 goto bad;
1007 }
1008 }
1009 switch (opt) {
1010
1011 default:
1012 break;
1013
1014 /*
1015 * Source routing with record.
1016 * Find interface with current destination address.
1017 * If none on this machine then drop if strictly routed,
1018 * or do nothing if loosely routed.
1019 * Record interface address and bring up next address
1020 * component. If strictly routed make sure next
1021 * address is on directly accessible net.
1022 */
1023 case IPOPT_LSRR:
1024 case IPOPT_SSRR:
1025 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1026 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1027 goto bad;
1028 }
1029 ipaddr.sin_addr = ip->ip_dst;
1030 ia = (struct in_ifaddr *)
1031 ifa_ifwithaddr((struct sockaddr *)&ipaddr);
1032 if (ia == 0) {
1033 if (opt == IPOPT_SSRR) {
1034 type = ICMP_UNREACH;
1035 code = ICMP_UNREACH_SRCFAIL;
1036 goto bad;
1037 }
1038 if (!ip_dosourceroute)
1039 goto nosourcerouting;
1040 /*
1041 * Loose routing, and not at next destination
1042 * yet; nothing to do except forward.
1043 */
1044 break;
1045 }
1046 off--; /* 0 origin */
1047 if (off > optlen - sizeof(struct in_addr)) {
1048 /*
1049 * End of source route. Should be for us.
1050 */
1051 if (!ip_acceptsourceroute)
1052 goto nosourcerouting;
1053 save_rte(cp, ip->ip_src);
1054 break;
1055 }
1056
1057 if (!ip_dosourceroute) {
1058 if (ipforwarding) {
1059 char buf[16]; /* aaa.bbb.ccc.ddd\0 */
1060 /*
1061 * Acting as a router, so generate ICMP
1062 */
1063nosourcerouting:
1064 strcpy(buf, inet_ntoa(ip->ip_dst));
1065 log(LOG_WARNING,
1066 "attempted source route from %s to %s\n",
1067 inet_ntoa(ip->ip_src), buf);
1068 type = ICMP_UNREACH;
1069 code = ICMP_UNREACH_SRCFAIL;
1070 goto bad;
1071 } else {
1072 /*
1073 * Not acting as a router, so silently drop.
1074 */
1075 ipstat.ips_cantforward++;
1076 m_freem(m);
1077 return (1);
1078 }
1079 }
1080
1081 /*
1082 * locate outgoing interface
1083 */
1084 (void)memcpy(&ipaddr.sin_addr, cp + off,
1085 sizeof(ipaddr.sin_addr));
1086
1087 if (opt == IPOPT_SSRR) {
1088#define INA struct in_ifaddr *
1089#define SA struct sockaddr *
1090 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
1091 ia = (INA)ifa_ifwithnet((SA)&ipaddr);
1092 } else
1093 ia = ip_rtaddr(ipaddr.sin_addr);
1094 if (ia == 0) {
1095 type = ICMP_UNREACH;
1096 code = ICMP_UNREACH_SRCFAIL;
1097 goto bad;
1098 }
1099 ip->ip_dst = ipaddr.sin_addr;
1100 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
1101 sizeof(struct in_addr));
1102 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1103 /*
1104 * Let ip_intr's mcast routing check handle mcast pkts
1105 */
1106 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
1107 break;
1108
1109 case IPOPT_RR:
1110 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1111 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1112 goto bad;
1113 }
1114 /*
1115 * If no space remains, ignore.
1116 */
1117 off--; /* 0 origin */
1118 if (off > optlen - sizeof(struct in_addr))
1119 break;
1120 (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst,
1121 sizeof(ipaddr.sin_addr));
1122 /*
1123 * locate outgoing interface; if we're the destination,
1124 * use the incoming interface (should be same).
1125 */
1126 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
1127 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
1128 type = ICMP_UNREACH;
1129 code = ICMP_UNREACH_HOST;
1130 goto bad;
1131 }
1132 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
1133 sizeof(struct in_addr));
1134 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1135 break;
1136
1137 case IPOPT_TS:
1138 code = cp - (u_char *)ip;
1139 ipt = (struct ip_timestamp *)cp;
1140 if (ipt->ipt_len < 5)
1141 goto bad;
1142 if (ipt->ipt_ptr > ipt->ipt_len - sizeof(int32_t)) {
1143 if (++ipt->ipt_oflw == 0)
1144 goto bad;
1145 break;
1146 }
1147 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
1148 switch (ipt->ipt_flg) {
1149
1150 case IPOPT_TS_TSONLY:
1151 break;
1152
1153 case IPOPT_TS_TSANDADDR:
1154 if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1155 sizeof(struct in_addr) > ipt->ipt_len)
1156 goto bad;
1157 ipaddr.sin_addr = dst;
1158 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
1159 m->m_pkthdr.rcvif);
1160 if (ia == 0)
1161 continue;
1162 (void)memcpy(sin, &IA_SIN(ia)->sin_addr,
1163 sizeof(struct in_addr));
1164 ipt->ipt_ptr += sizeof(struct in_addr);
1165 break;
1166
1167 case IPOPT_TS_PRESPEC:
1168 if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1169 sizeof(struct in_addr) > ipt->ipt_len)
1170 goto bad;
1171 (void)memcpy(&ipaddr.sin_addr, sin,
1172 sizeof(struct in_addr));
1173 if (ifa_ifwithaddr((SA)&ipaddr) == 0)
1174 continue;
1175 ipt->ipt_ptr += sizeof(struct in_addr);
1176 break;
1177
1178 default:
1179 goto bad;
1180 }
1181 ntime = iptime();
1182 (void)memcpy(cp + ipt->ipt_ptr - 1, &ntime,
1183 sizeof(n_time));
1184 ipt->ipt_ptr += sizeof(n_time);
1185 }
1186 }
1187 if (forward && ipforwarding) {
1188 ip_forward(m, 1);
1189 return (1);
1190 }
1191 return (0);
1192bad:
1193 ip->ip_len -= IP_VHL_HL(ip->ip_vhl) << 2; /* XXX icmp_error adds in hdr length */
1194 icmp_error(m, type, code, 0, 0);
1195 ipstat.ips_badoptions++;
1196 return (1);
1197}
1198
1199/*
1200 * Given address of next destination (final or next hop),
1201 * return internet address info of interface to be used to get there.
1202 */
1203static struct in_ifaddr *
1204ip_rtaddr(dst)
1205 struct in_addr dst;
1206{
1207 register struct sockaddr_in *sin;
1208
1209 sin = (struct sockaddr_in *) &ipforward_rt.ro_dst;
1210
1211 if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) {
1212 if (ipforward_rt.ro_rt) {
1213 RTFREE(ipforward_rt.ro_rt);
1214 ipforward_rt.ro_rt = 0;
1215 }
1216 sin->sin_family = AF_INET;
1217 sin->sin_len = sizeof(*sin);
1218 sin->sin_addr = dst;
1219
1220 rtalloc_ign(&ipforward_rt, RTF_PRCLONING);
1221 }
1222 if (ipforward_rt.ro_rt == 0)
1223 return ((struct in_ifaddr *)0);
1224 return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa);
1225}
1226
1227/*
1228 * Save incoming source route for use in replies,
1229 * to be picked up later by ip_srcroute if the receiver is interested.
1230 */
1231void
1232save_rte(option, dst)
1233 u_char *option;
1234 struct in_addr dst;
1235{
1236 unsigned olen;
1237
1238 olen = option[IPOPT_OLEN];
1239#ifdef DIAGNOSTIC
1240 if (ipprintfs)
1241 printf("save_rte: olen %d\n", olen);
1242#endif
1243 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
1244 return;
1245 bcopy(option, ip_srcrt.srcopt, olen);
1246 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1247 ip_srcrt.dst = dst;
1248}
1249
1250/*
1251 * Retrieve incoming source route for use in replies,
1252 * in the same form used by setsockopt.
1253 * The first hop is placed before the options, will be removed later.
1254 */
1255struct mbuf *
1256ip_srcroute()
1257{
1258 register struct in_addr *p, *q;
1259 register struct mbuf *m;
1260
1261 if (ip_nhops == 0)
1262 return ((struct mbuf *)0);
1263 m = m_get(M_DONTWAIT, MT_HEADER);
1264 if (m == 0)
1265 return ((struct mbuf *)0);
1266
1267#define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1268
1269 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1270 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
1271 OPTSIZ;
1272#ifdef DIAGNOSTIC
1273 if (ipprintfs)
1274 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
1275#endif
1276
1277 /*
1278 * First save first hop for return route
1279 */
1280 p = &ip_srcrt.route[ip_nhops - 1];
1281 *(mtod(m, struct in_addr *)) = *p--;
1282#ifdef DIAGNOSTIC
1283 if (ipprintfs)
1284 printf(" hops %lx", (u_long)ntohl(mtod(m, struct in_addr *)->s_addr));
1285#endif
1286
1287 /*
1288 * Copy option fields and padding (nop) to mbuf.
1289 */
1290 ip_srcrt.nop = IPOPT_NOP;
1291 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
1292 (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr),
1293 &ip_srcrt.nop, OPTSIZ);
1294 q = (struct in_addr *)(mtod(m, caddr_t) +
1295 sizeof(struct in_addr) + OPTSIZ);
1296#undef OPTSIZ
1297 /*
1298 * Record return path as an IP source route,
1299 * reversing the path (pointers are now aligned).
1300 */
1301 while (p >= ip_srcrt.route) {
1302#ifdef DIAGNOSTIC
1303 if (ipprintfs)
1304 printf(" %lx", (u_long)ntohl(q->s_addr));
1305#endif
1306 *q++ = *p--;
1307 }
1308 /*
1309 * Last hop goes to final destination.
1310 */
1311 *q = ip_srcrt.dst;
1312#ifdef DIAGNOSTIC
1313 if (ipprintfs)
1314 printf(" %lx\n", (u_long)ntohl(q->s_addr));
1315#endif
1316 return (m);
1317}
1318
1319/*
1320 * Strip out IP options, at higher
1321 * level protocol in the kernel.
1322 * Second argument is buffer to which options
1323 * will be moved, and return value is their length.
1324 * XXX should be deleted; last arg currently ignored.
1325 */
1326void
1327ip_stripoptions(m, mopt)
1328 register struct mbuf *m;
1329 struct mbuf *mopt;
1330{
1331 register int i;
1332 struct ip *ip = mtod(m, struct ip *);
1333 register caddr_t opts;
1334 int olen;
1335
1336 olen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1337 opts = (caddr_t)(ip + 1);
1338 i = m->m_len - (sizeof (struct ip) + olen);
1339 bcopy(opts + olen, opts, (unsigned)i);
1340 m->m_len -= olen;
1341 if (m->m_flags & M_PKTHDR)
1342 m->m_pkthdr.len -= olen;
1343 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2);
1344}
1345
1346u_char inetctlerrmap[PRC_NCMDS] = {
1347 0, 0, 0, 0,
1348 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1349 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1350 EMSGSIZE, EHOSTUNREACH, 0, 0,
1351 0, 0, 0, 0,
1352 ENOPROTOOPT
1353};
1354
1355/*
1356 * Forward a packet. If some error occurs return the sender
1357 * an icmp packet. Note we can't always generate a meaningful
1358 * icmp message because icmp doesn't have a large enough repertoire
1359 * of codes and types.
1360 *
1361 * If not forwarding, just drop the packet. This could be confusing
1362 * if ipforwarding was zero but some routing protocol was advancing
1363 * us as a gateway to somewhere. However, we must let the routing
1364 * protocol deal with that.
1365 *
1366 * The srcrt parameter indicates whether the packet is being forwarded
1367 * via a source route.
1368 */
1369static void
1370ip_forward(m, srcrt)
1371 struct mbuf *m;
1372 int srcrt;
1373{
1374 register struct ip *ip = mtod(m, struct ip *);
1375 register struct sockaddr_in *sin;
1376 register struct rtentry *rt;
1377 int error, type = 0, code = 0;
1378 struct mbuf *mcopy;
1379 n_long dest;
1380 struct ifnet *destifp;
1381
1382 dest = 0;
1383#ifdef DIAGNOSTIC
1384 if (ipprintfs)
1385 printf("forward: src %lx dst %lx ttl %x\n",
1386 (u_long)ip->ip_src.s_addr, (u_long)ip->ip_dst.s_addr,
1387 ip->ip_ttl);
1388#endif
1389
1390
1391 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1392 ipstat.ips_cantforward++;
1393 m_freem(m);
1394 return;
1395 }
1396 HTONS(ip->ip_id);
1397#ifdef IPSTEALTH
1398 if (!ipstealth) {
1399#endif
1400 if (ip->ip_ttl <= IPTTLDEC) {
1401 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
1402 dest, 0);
1403 return;
1404 }
1405 ip->ip_ttl -= IPTTLDEC;
1406#ifdef IPSTEALTH
1407 }
1408#endif
1409
1410 sin = (struct sockaddr_in *)&ipforward_rt.ro_dst;
1411 if ((rt = ipforward_rt.ro_rt) == 0 ||
1412 ip->ip_dst.s_addr != sin->sin_addr.s_addr) {
1413 if (ipforward_rt.ro_rt) {
1414 RTFREE(ipforward_rt.ro_rt);
1415 ipforward_rt.ro_rt = 0;
1416 }
1417 sin->sin_family = AF_INET;
1418 sin->sin_len = sizeof(*sin);
1419 sin->sin_addr = ip->ip_dst;
1420
1421 rtalloc_ign(&ipforward_rt, RTF_PRCLONING);
1422 if (ipforward_rt.ro_rt == 0) {
1423 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
1424 return;
1425 }
1426 rt = ipforward_rt.ro_rt;
1427 }
1428
1429 /*
1430 * Save at most 64 bytes of the packet in case
1431 * we need to generate an ICMP message to the src.
1432 */
1433 mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64));
1434
1435 /*
1436 * If forwarding packet using same interface that it came in on,
1437 * perhaps should send a redirect to sender to shortcut a hop.
1438 * Only send redirect if source is sending directly to us,
1439 * and if packet was not source routed (or has any options).
1440 * Also, don't send redirect if forwarding using a default route
1441 * or a route modified by a redirect.
1442 */
1443#define satosin(sa) ((struct sockaddr_in *)(sa))
1444 if (rt->rt_ifp == m->m_pkthdr.rcvif &&
1445 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1446 satosin(rt_key(rt))->sin_addr.s_addr != 0 &&
1447 ipsendredirects && !srcrt) {
1448#define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa))
1449 u_long src = ntohl(ip->ip_src.s_addr);
1450
1451 if (RTA(rt) &&
1452 (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1453 if (rt->rt_flags & RTF_GATEWAY)
1454 dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1455 else
1456 dest = ip->ip_dst.s_addr;
1457 /* Router requirements says to only send host redirects */
1458 type = ICMP_REDIRECT;
1459 code = ICMP_REDIRECT_HOST;
1460#ifdef DIAGNOSTIC
1461 if (ipprintfs)
1462 printf("redirect (%d) to %lx\n", code, (u_long)dest);
1463#endif
1464 }
1465 }
1466
1467 error = ip_output(m, (struct mbuf *)0, &ipforward_rt,
1468 IP_FORWARDING, 0);
1469 if (error)
1470 ipstat.ips_cantforward++;
1471 else {
1472 ipstat.ips_forward++;
1473 if (type)
1474 ipstat.ips_redirectsent++;
1475 else {
1476 if (mcopy) {
1477 ipflow_create(&ipforward_rt, mcopy);
1478 m_freem(mcopy);
1479 }
1480 return;
1481 }
1482 }
1483 if (mcopy == NULL)
1484 return;
1485 destifp = NULL;
1486
1487 switch (error) {
1488
1489 case 0: /* forwarded, but need redirect */
1490 /* type, code set above */
1491 break;
1492
1493 case ENETUNREACH: /* shouldn't happen, checked above */
1494 case EHOSTUNREACH:
1495 case ENETDOWN:
1496 case EHOSTDOWN:
1497 default:
1498 type = ICMP_UNREACH;
1499 code = ICMP_UNREACH_HOST;
1500 break;
1501
1502 case EMSGSIZE:
1503 type = ICMP_UNREACH;
1504 code = ICMP_UNREACH_NEEDFRAG;
1505 if (ipforward_rt.ro_rt)
1506 destifp = ipforward_rt.ro_rt->rt_ifp;
1507 ipstat.ips_cantfrag++;
1508 break;
1509
1510 case ENOBUFS:
1511 type = ICMP_SOURCEQUENCH;
1512 code = 0;
1513 break;
1514 }
1515 icmp_error(mcopy, type, code, dest, destifp);
1516}
1517
1518void
1519ip_savecontrol(inp, mp, ip, m)
1520 register struct inpcb *inp;
1521 register struct mbuf **mp;
1522 register struct ip *ip;
1523 register struct mbuf *m;
1524{
1525 if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1526 struct timeval tv;
1527
1528 microtime(&tv);
1529 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1530 SCM_TIMESTAMP, SOL_SOCKET);
1531 if (*mp)
1532 mp = &(*mp)->m_next;
1533 }
1534 if (inp->inp_flags & INP_RECVDSTADDR) {
1535 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
1536 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1537 if (*mp)
1538 mp = &(*mp)->m_next;
1539 }
1540#ifdef notyet
1541 /* XXX
1542 * Moving these out of udp_input() made them even more broken
1543 * than they already were.
1544 */
1545 /* options were tossed already */
1546 if (inp->inp_flags & INP_RECVOPTS) {
1547 *mp = sbcreatecontrol((caddr_t) opts_deleted_above,
1548 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
1549 if (*mp)
1550 mp = &(*mp)->m_next;
1551 }
1552 /* ip_srcroute doesn't do what we want here, need to fix */
1553 if (inp->inp_flags & INP_RECVRETOPTS) {
1554 *mp = sbcreatecontrol((caddr_t) ip_srcroute(),
1555 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
1556 if (*mp)
1557 mp = &(*mp)->m_next;
1558 }
1559#endif
1560 if (inp->inp_flags & INP_RECVIF) {
1561 struct ifnet *ifp;
1562 struct sdlbuf {
1563 struct sockaddr_dl sdl;
1564 u_char pad[32];
1565 } sdlbuf;
1566 struct sockaddr_dl *sdp;
1567 struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
1568
1569 if (((ifp = m->m_pkthdr.rcvif))
1570 && ( ifp->if_index && (ifp->if_index <= if_index))) {
1571 sdp = (struct sockaddr_dl *)(ifnet_addrs
1572 [ifp->if_index - 1]->ifa_addr);
1573 /*
1574 * Change our mind and don't try copy.
1575 */
1576 if ((sdp->sdl_family != AF_LINK)
1577 || (sdp->sdl_len > sizeof(sdlbuf))) {
1578 goto makedummy;
1579 }
1580 bcopy(sdp, sdl2, sdp->sdl_len);
1581 } else {
1582makedummy:
1583 sdl2->sdl_len
1584 = offsetof(struct sockaddr_dl, sdl_data[0]);
1585 sdl2->sdl_family = AF_LINK;
1586 sdl2->sdl_index = 0;
1587 sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1588 }
1589 *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len,
1590 IP_RECVIF, IPPROTO_IP);
1591 if (*mp)
1592 mp = &(*mp)->m_next;
1593 }
1594}
1595
1596int
1597ip_rsvp_init(struct socket *so)
1598{
1599 if (so->so_type != SOCK_RAW ||
1600 so->so_proto->pr_protocol != IPPROTO_RSVP)
1601 return EOPNOTSUPP;
1602
1603 if (ip_rsvpd != NULL)
1604 return EADDRINUSE;
1605
1606 ip_rsvpd = so;
1607 /*
1608 * This may seem silly, but we need to be sure we don't over-increment
1609 * the RSVP counter, in case something slips up.
1610 */
1611 if (!ip_rsvp_on) {
1612 ip_rsvp_on = 1;
1613 rsvp_on++;
1614 }
1615
1616 return 0;
1617}
1618
1619int
1620ip_rsvp_done(void)
1621{
1622 ip_rsvpd = NULL;
1623 /*
1624 * This may seem silly, but we need to be sure we don't over-decrement
1625 * the RSVP counter, in case something slips up.
1626 */
1627 if (ip_rsvp_on) {
1628 ip_rsvp_on = 0;
1629 rsvp_on--;
1630 }
1631 return 0;
1632}