Statistics
| Branch: | Revision:

root / slirp / ip_output.c @ eb38c52c

History | View | Annotate | Download (5.6 kB)

1 f0cbd3ec bellard
/*
2 f0cbd3ec bellard
 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 f0cbd3ec bellard
 *        The Regents of the University of California.  All rights reserved.
4 f0cbd3ec bellard
 *
5 f0cbd3ec bellard
 * Redistribution and use in source and binary forms, with or without
6 f0cbd3ec bellard
 * modification, are permitted provided that the following conditions
7 f0cbd3ec bellard
 * are met:
8 f0cbd3ec bellard
 * 1. Redistributions of source code must retain the above copyright
9 f0cbd3ec bellard
 *    notice, this list of conditions and the following disclaimer.
10 f0cbd3ec bellard
 * 2. Redistributions in binary form must reproduce the above copyright
11 f0cbd3ec bellard
 *    notice, this list of conditions and the following disclaimer in the
12 f0cbd3ec bellard
 *    documentation and/or other materials provided with the distribution.
13 f0cbd3ec bellard
 * 3. All advertising materials mentioning features or use of this software
14 f0cbd3ec bellard
 *    must display the following acknowledgement:
15 f0cbd3ec bellard
 *        This product includes software developed by the University of
16 f0cbd3ec bellard
 *        California, Berkeley and its contributors.
17 f0cbd3ec bellard
 * 4. Neither the name of the University nor the names of its contributors
18 f0cbd3ec bellard
 *    may be used to endorse or promote products derived from this software
19 f0cbd3ec bellard
 *    without specific prior written permission.
20 f0cbd3ec bellard
 *
21 f0cbd3ec bellard
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 f0cbd3ec bellard
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 f0cbd3ec bellard
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 f0cbd3ec bellard
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 f0cbd3ec bellard
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 f0cbd3ec bellard
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 f0cbd3ec bellard
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 f0cbd3ec bellard
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 f0cbd3ec bellard
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 f0cbd3ec bellard
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 f0cbd3ec bellard
 * SUCH DAMAGE.
32 f0cbd3ec bellard
 *
33 f0cbd3ec bellard
 *        @(#)ip_output.c        8.3 (Berkeley) 1/21/94
34 f0cbd3ec bellard
 * ip_output.c,v 1.9 1994/11/16 10:17:10 jkh Exp
35 f0cbd3ec bellard
 */
36 f0cbd3ec bellard
37 f0cbd3ec bellard
/*
38 f0cbd3ec bellard
 * Changes and additions relating to SLiRP are
39 f0cbd3ec bellard
 * Copyright (c) 1995 Danny Gasparovski.
40 f0cbd3ec bellard
 *
41 f0cbd3ec bellard
 * Please read the file COPYRIGHT for the
42 f0cbd3ec bellard
 * terms and conditions of the copyright.
43 f0cbd3ec bellard
 */
44 f0cbd3ec bellard
45 f0cbd3ec bellard
#include <slirp.h>
46 f0cbd3ec bellard
47 f0cbd3ec bellard
u_int16_t ip_id;
48 f0cbd3ec bellard
49 7878ff6b blueswir1
/* Number of packets queued before we start sending
50 7878ff6b blueswir1
 * (to prevent allocing too many mbufs) */
51 7878ff6b blueswir1
#define IF_THRESH 10
52 7878ff6b blueswir1
53 f0cbd3ec bellard
/*
54 f0cbd3ec bellard
 * IP output.  The packet in mbuf chain m contains a skeletal IP
55 f0cbd3ec bellard
 * header (with len, off, ttl, proto, tos, src, dst).
56 f0cbd3ec bellard
 * The mbuf chain containing the packet will be freed.
57 f0cbd3ec bellard
 * The mbuf opt, if present, will not be freed.
58 f0cbd3ec bellard
 */
59 f0cbd3ec bellard
int
60 f0cbd3ec bellard
ip_output(so, m0)
61 f0cbd3ec bellard
        struct socket *so;
62 f0cbd3ec bellard
        struct mbuf *m0;
63 f0cbd3ec bellard
{
64 f0cbd3ec bellard
        register struct ip *ip;
65 f0cbd3ec bellard
        register struct mbuf *m = m0;
66 f0cbd3ec bellard
        register int hlen = sizeof(struct ip );
67 f0cbd3ec bellard
        int len, off, error = 0;
68 f0cbd3ec bellard
69 f0cbd3ec bellard
        DEBUG_CALL("ip_output");
70 f0cbd3ec bellard
        DEBUG_ARG("so = %lx", (long)so);
71 f0cbd3ec bellard
        DEBUG_ARG("m0 = %lx", (long)m0);
72 5fafdf24 ths
73 f0cbd3ec bellard
        /* We do no options */
74 f0cbd3ec bellard
/*        if (opt) {
75 f0cbd3ec bellard
 *                m = ip_insertoptions(m, opt, &len);
76 f0cbd3ec bellard
 *                hlen = len;
77 f0cbd3ec bellard
 *        }
78 f0cbd3ec bellard
 */
79 f0cbd3ec bellard
        ip = mtod(m, struct ip *);
80 f0cbd3ec bellard
        /*
81 f0cbd3ec bellard
         * Fill in IP header.
82 f0cbd3ec bellard
         */
83 f0cbd3ec bellard
        ip->ip_v = IPVERSION;
84 f0cbd3ec bellard
        ip->ip_off &= IP_DF;
85 f0cbd3ec bellard
        ip->ip_id = htons(ip_id++);
86 f0cbd3ec bellard
        ip->ip_hl = hlen >> 2;
87 31a60e22 blueswir1
        STAT(ipstat.ips_localout++);
88 f0cbd3ec bellard
89 f0cbd3ec bellard
        /*
90 f0cbd3ec bellard
         * Verify that we have any chance at all of being able to queue
91 f0cbd3ec bellard
         *      the packet or packet fragments
92 f0cbd3ec bellard
         */
93 f0cbd3ec bellard
        /* XXX Hmmm... */
94 7878ff6b blueswir1
/*        if (if_queued > IF_THRESH && towrite <= 0) {
95 f0cbd3ec bellard
 *                error = ENOBUFS;
96 f0cbd3ec bellard
 *                goto bad;
97 f0cbd3ec bellard
 *        }
98 f0cbd3ec bellard
 */
99 5fafdf24 ths
100 f0cbd3ec bellard
        /*
101 f0cbd3ec bellard
         * If small enough for interface, can just send directly.
102 f0cbd3ec bellard
         */
103 9634d903 blueswir1
        if ((u_int16_t)ip->ip_len <= IF_MTU) {
104 f0cbd3ec bellard
                ip->ip_len = htons((u_int16_t)ip->ip_len);
105 f0cbd3ec bellard
                ip->ip_off = htons((u_int16_t)ip->ip_off);
106 f0cbd3ec bellard
                ip->ip_sum = 0;
107 f0cbd3ec bellard
                ip->ip_sum = cksum(m, hlen);
108 f0cbd3ec bellard
109 f0cbd3ec bellard
                if_output(so, m);
110 f0cbd3ec bellard
                goto done;
111 f0cbd3ec bellard
        }
112 f0cbd3ec bellard
113 f0cbd3ec bellard
        /*
114 f0cbd3ec bellard
         * Too large for interface; fragment if possible.
115 f0cbd3ec bellard
         * Must be able to put at least 8 bytes per fragment.
116 f0cbd3ec bellard
         */
117 f0cbd3ec bellard
        if (ip->ip_off & IP_DF) {
118 f0cbd3ec bellard
                error = -1;
119 31a60e22 blueswir1
                STAT(ipstat.ips_cantfrag++);
120 f0cbd3ec bellard
                goto bad;
121 f0cbd3ec bellard
        }
122 5fafdf24 ths
123 9634d903 blueswir1
        len = (IF_MTU - hlen) &~ 7;       /* ip databytes per packet */
124 f0cbd3ec bellard
        if (len < 8) {
125 f0cbd3ec bellard
                error = -1;
126 f0cbd3ec bellard
                goto bad;
127 f0cbd3ec bellard
        }
128 f0cbd3ec bellard
129 f0cbd3ec bellard
    {
130 f0cbd3ec bellard
        int mhlen, firstlen = len;
131 f0cbd3ec bellard
        struct mbuf **mnext = &m->m_nextpkt;
132 f0cbd3ec bellard
133 f0cbd3ec bellard
        /*
134 f0cbd3ec bellard
         * Loop through length of segment after first fragment,
135 f0cbd3ec bellard
         * make new header and copy data of each part and link onto chain.
136 f0cbd3ec bellard
         */
137 f0cbd3ec bellard
        m0 = m;
138 f0cbd3ec bellard
        mhlen = sizeof (struct ip);
139 f0cbd3ec bellard
        for (off = hlen + len; off < (u_int16_t)ip->ip_len; off += len) {
140 f0cbd3ec bellard
          register struct ip *mhip;
141 f0cbd3ec bellard
          m = m_get();
142 f0cbd3ec bellard
          if (m == 0) {
143 f0cbd3ec bellard
            error = -1;
144 31a60e22 blueswir1
            STAT(ipstat.ips_odropped++);
145 f0cbd3ec bellard
            goto sendorfree;
146 f0cbd3ec bellard
          }
147 9634d903 blueswir1
          m->m_data += IF_MAXLINKHDR;
148 f0cbd3ec bellard
          mhip = mtod(m, struct ip *);
149 f0cbd3ec bellard
          *mhip = *ip;
150 3b46e624 ths
151 f0cbd3ec bellard
                /* No options */
152 f0cbd3ec bellard
/*                if (hlen > sizeof (struct ip)) {
153 f0cbd3ec bellard
 *                        mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
154 f0cbd3ec bellard
 *                        mhip->ip_hl = mhlen >> 2;
155 f0cbd3ec bellard
 *                }
156 f0cbd3ec bellard
 */
157 f0cbd3ec bellard
          m->m_len = mhlen;
158 f0cbd3ec bellard
          mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
159 f0cbd3ec bellard
          if (ip->ip_off & IP_MF)
160 f0cbd3ec bellard
            mhip->ip_off |= IP_MF;
161 f0cbd3ec bellard
          if (off + len >= (u_int16_t)ip->ip_len)
162 f0cbd3ec bellard
            len = (u_int16_t)ip->ip_len - off;
163 5fafdf24 ths
          else
164 f0cbd3ec bellard
            mhip->ip_off |= IP_MF;
165 f0cbd3ec bellard
          mhip->ip_len = htons((u_int16_t)(len + mhlen));
166 3b46e624 ths
167 f0cbd3ec bellard
          if (m_copy(m, m0, off, len) < 0) {
168 f0cbd3ec bellard
            error = -1;
169 f0cbd3ec bellard
            goto sendorfree;
170 f0cbd3ec bellard
          }
171 3b46e624 ths
172 f0cbd3ec bellard
          mhip->ip_off = htons((u_int16_t)mhip->ip_off);
173 f0cbd3ec bellard
          mhip->ip_sum = 0;
174 f0cbd3ec bellard
          mhip->ip_sum = cksum(m, mhlen);
175 f0cbd3ec bellard
          *mnext = m;
176 f0cbd3ec bellard
          mnext = &m->m_nextpkt;
177 31a60e22 blueswir1
          STAT(ipstat.ips_ofragments++);
178 f0cbd3ec bellard
        }
179 f0cbd3ec bellard
        /*
180 f0cbd3ec bellard
         * Update first fragment by trimming what's been copied out
181 f0cbd3ec bellard
         * and updating header, then send each fragment (in order).
182 f0cbd3ec bellard
         */
183 f0cbd3ec bellard
        m = m0;
184 f0cbd3ec bellard
        m_adj(m, hlen + firstlen - (u_int16_t)ip->ip_len);
185 f0cbd3ec bellard
        ip->ip_len = htons((u_int16_t)m->m_len);
186 f0cbd3ec bellard
        ip->ip_off = htons((u_int16_t)(ip->ip_off | IP_MF));
187 f0cbd3ec bellard
        ip->ip_sum = 0;
188 f0cbd3ec bellard
        ip->ip_sum = cksum(m, hlen);
189 f0cbd3ec bellard
sendorfree:
190 f0cbd3ec bellard
        for (m = m0; m; m = m0) {
191 f0cbd3ec bellard
                m0 = m->m_nextpkt;
192 f0cbd3ec bellard
                m->m_nextpkt = 0;
193 f0cbd3ec bellard
                if (error == 0)
194 f0cbd3ec bellard
                        if_output(so, m);
195 f0cbd3ec bellard
                else
196 f0cbd3ec bellard
                        m_freem(m);
197 f0cbd3ec bellard
        }
198 f0cbd3ec bellard
199 f0cbd3ec bellard
        if (error == 0)
200 31a60e22 blueswir1
                STAT(ipstat.ips_fragmented++);
201 f0cbd3ec bellard
    }
202 f0cbd3ec bellard
203 f0cbd3ec bellard
done:
204 f0cbd3ec bellard
        return (error);
205 f0cbd3ec bellard
206 f0cbd3ec bellard
bad:
207 f0cbd3ec bellard
        m_freem(m0);
208 f0cbd3ec bellard
        goto done;
209 f0cbd3ec bellard
}