OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [bsd_tcpip/] [current/] [src/] [sys/] [netinet/] [ip_input.c] - Blame information for rev 786

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.