source: rtems/cpukit/libnetworking/netinet/ip_input.c @ 0e16fa45

5
Last change on this file since 0e16fa45 was cb68253, checked in by Sebastian Huber <sebastian.huber@…>, on 09/07/18 at 04:19:02

network: Use kernel/user space header files

Add and use <machine/rtems-bsd-kernel-space.h> and
<machine/rtems-bsd-user-space.h> similar to the libbsd to avoid command
line defines and defines scattered throught the code base.

Simplify cpukit/libnetworking/Makefile.am.

Update #3375.

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