source: rtems/cpukit/libnetworking/netinet/ip_input.c @ dd967330

4.104.114.95
Last change on this file since dd967330 was dd967330, checked in by Ralf Corsepius <ralf.corsepius@…>, on 09/01/08 at 06:36:17

Stop using old-style function definitions.

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