1 | /*
|
---|
2 | * Copyright (c) 1982, 1986, 1988, 1990, 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_output.c 8.3 (Berkeley) 1/21/94
|
---|
34 | * ip_output.c,v 1.9 1994/11/16 10:17:10 jkh Exp
|
---|
35 | */
|
---|
36 |
|
---|
37 | /*
|
---|
38 | * Changes and additions relating to SLiRP are
|
---|
39 | * Copyright (c) 1995 Danny Gasparovski.
|
---|
40 | *
|
---|
41 | * Please read the file COPYRIGHT for the
|
---|
42 | * terms and conditions of the copyright.
|
---|
43 | */
|
---|
44 |
|
---|
45 | #include <slirp.h>
|
---|
46 |
|
---|
47 |
|
---|
48 | /*
|
---|
49 | * IP output. The packet in mbuf chain m contains a skeletal IP
|
---|
50 | * header (with len, off, ttl, proto, tos, src, dst).
|
---|
51 | * The mbuf chain containing the packet will be freed.
|
---|
52 | * The mbuf opt, if present, will not be freed.
|
---|
53 | */
|
---|
54 | int
|
---|
55 | ip_output(PNATState pData, struct socket *so, struct mbuf *m0)
|
---|
56 | {
|
---|
57 | register struct ip *ip;
|
---|
58 | register struct mbuf *m = m0;
|
---|
59 | register int hlen = sizeof(struct ip );
|
---|
60 | int len, off, error = 0;
|
---|
61 |
|
---|
62 | DEBUG_CALL("ip_output");
|
---|
63 | DEBUG_ARG("so = %lx", (long)so);
|
---|
64 | DEBUG_ARG("m0 = %lx", (long)m0);
|
---|
65 |
|
---|
66 | #if 0 /* We do no options */
|
---|
67 | if (opt)
|
---|
68 | {
|
---|
69 | m = ip_insertoptions(m, opt, &len);
|
---|
70 | hlen = len;
|
---|
71 | }
|
---|
72 | #endif
|
---|
73 | ip = mtod(m, struct ip *);
|
---|
74 | /*
|
---|
75 | * Fill in IP header.
|
---|
76 | */
|
---|
77 | ip->ip_v = IPVERSION;
|
---|
78 | ip->ip_off &= IP_DF;
|
---|
79 | ip->ip_id = htons(ip_currid++);
|
---|
80 | ip->ip_hl = hlen >> 2;
|
---|
81 | ipstat.ips_localout++;
|
---|
82 |
|
---|
83 | /*
|
---|
84 | * Verify that we have any chance at all of being able to queue
|
---|
85 | * the packet or packet fragments
|
---|
86 | */
|
---|
87 | #if 0 /* XXX Hmmm... */
|
---|
88 | if (if_queued > if_thresh && towrite <= 0)
|
---|
89 | {
|
---|
90 | error = ENOBUFS;
|
---|
91 | goto bad;
|
---|
92 | }
|
---|
93 | #endif
|
---|
94 |
|
---|
95 | /*
|
---|
96 | * If small enough for interface, can just send directly.
|
---|
97 | */
|
---|
98 | if ((u_int16_t)ip->ip_len <= if_mtu)
|
---|
99 | {
|
---|
100 | ip->ip_len = htons((u_int16_t)ip->ip_len);
|
---|
101 | ip->ip_off = htons((u_int16_t)ip->ip_off);
|
---|
102 | ip->ip_sum = 0;
|
---|
103 | ip->ip_sum = cksum(m, hlen);
|
---|
104 |
|
---|
105 | if_output(pData, so, m);
|
---|
106 | goto done;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /*
|
---|
110 | * Too large for interface; fragment if possible.
|
---|
111 | * Must be able to put at least 8 bytes per fragment.
|
---|
112 | */
|
---|
113 | if (ip->ip_off & IP_DF)
|
---|
114 | {
|
---|
115 | error = -1;
|
---|
116 | ipstat.ips_cantfrag++;
|
---|
117 | goto bad;
|
---|
118 | }
|
---|
119 |
|
---|
120 | len = (if_mtu - hlen) &~ 7; /* ip databytes per packet */
|
---|
121 | if (len < 8)
|
---|
122 | {
|
---|
123 | error = -1;
|
---|
124 | goto bad;
|
---|
125 | }
|
---|
126 |
|
---|
127 | {
|
---|
128 | int mhlen, firstlen = len;
|
---|
129 | struct mbuf **mnext = &m->m_nextpkt;
|
---|
130 |
|
---|
131 | /*
|
---|
132 | * Loop through length of segment after first fragment,
|
---|
133 | * make new header and copy data of each part and link onto chain.
|
---|
134 | */
|
---|
135 | m0 = m;
|
---|
136 | mhlen = sizeof (struct ip);
|
---|
137 | for (off = hlen + len; off < (u_int16_t)ip->ip_len; off += len)
|
---|
138 | {
|
---|
139 | register struct ip *mhip;
|
---|
140 | m = m_get(pData);
|
---|
141 | if (m == 0)
|
---|
142 | {
|
---|
143 | error = -1;
|
---|
144 | ipstat.ips_odropped++;
|
---|
145 | goto sendorfree;
|
---|
146 | }
|
---|
147 | m->m_data += if_maxlinkhdr;
|
---|
148 | mhip = mtod(m, struct ip *);
|
---|
149 | *mhip = *ip;
|
---|
150 |
|
---|
151 | #if 0 /* No options */
|
---|
152 | if (hlen > sizeof (struct ip))
|
---|
153 | {
|
---|
154 | mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
|
---|
155 | mhip->ip_hl = mhlen >> 2;
|
---|
156 | }
|
---|
157 | #endif
|
---|
158 | m->m_len = mhlen;
|
---|
159 | mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
|
---|
160 | if (ip->ip_off & IP_MF)
|
---|
161 | mhip->ip_off |= IP_MF;
|
---|
162 | if (off + len >= (u_int16_t)ip->ip_len)
|
---|
163 | len = (u_int16_t)ip->ip_len - off;
|
---|
164 | else
|
---|
165 | mhip->ip_off |= IP_MF;
|
---|
166 | mhip->ip_len = htons((u_int16_t)(len + mhlen));
|
---|
167 |
|
---|
168 | if (m_copy(m, m0, off, len) < 0)
|
---|
169 | {
|
---|
170 | error = -1;
|
---|
171 | goto sendorfree;
|
---|
172 | }
|
---|
173 |
|
---|
174 | mhip->ip_off = htons((u_int16_t)mhip->ip_off);
|
---|
175 | mhip->ip_sum = 0;
|
---|
176 | mhip->ip_sum = cksum(m, mhlen);
|
---|
177 | *mnext = m;
|
---|
178 | mnext = &m->m_nextpkt;
|
---|
179 | ipstat.ips_ofragments++;
|
---|
180 | }
|
---|
181 | /*
|
---|
182 | * Update first fragment by trimming what's been copied out
|
---|
183 | * and updating header, then send each fragment (in order).
|
---|
184 | */
|
---|
185 | m = m0;
|
---|
186 | m_adj(m, hlen + firstlen - (u_int16_t)ip->ip_len);
|
---|
187 | ip->ip_len = htons((u_int16_t)m->m_len);
|
---|
188 | ip->ip_off = htons((u_int16_t)(ip->ip_off | IP_MF));
|
---|
189 | ip->ip_sum = 0;
|
---|
190 | ip->ip_sum = cksum(m, hlen);
|
---|
191 |
|
---|
192 | sendorfree:
|
---|
193 | for (m = m0; m; m = m0)
|
---|
194 | {
|
---|
195 | m0 = m->m_nextpkt;
|
---|
196 | m->m_nextpkt = 0;
|
---|
197 | if (error == 0)
|
---|
198 | if_output(pData, so, m);
|
---|
199 | else
|
---|
200 | m_freem(pData, m);
|
---|
201 | }
|
---|
202 |
|
---|
203 | if (error == 0)
|
---|
204 | ipstat.ips_fragmented++;
|
---|
205 | }
|
---|
206 |
|
---|
207 | done:
|
---|
208 | return (error);
|
---|
209 |
|
---|
210 | bad:
|
---|
211 | m_freem(pData, m0);
|
---|
212 | goto done;
|
---|
213 | }
|
---|