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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [rtems/] [c/] [src/] [libnetworking/] [netinet/] [ip_input.c] - Blame information for rev 173

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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