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 | #include "alias.h"
|
---|
47 |
|
---|
48 | static int rt_lookup_in_cache(PNATState pData, uint32_t dst, uint8_t *ether)
|
---|
49 | {
|
---|
50 | int rc = 1;
|
---|
51 | rc = slirp_arp_lookup_ether_by_ip(pData, dst, ether);
|
---|
52 | if (rc == 0)
|
---|
53 | return rc;
|
---|
54 | rc = bootp_cache_lookup_ether_by_ip(pData, dst, ether);
|
---|
55 | if (rc == 0)
|
---|
56 | return rc;
|
---|
57 | /*
|
---|
58 | * no chance to send this packet, sorry, we will request ether address via ARP
|
---|
59 | */
|
---|
60 | slirp_arp_who_has(pData, dst);
|
---|
61 | return rc;
|
---|
62 | }
|
---|
63 |
|
---|
64 | /*
|
---|
65 | * IP output. The packet in mbuf chain m contains a skeletal IP
|
---|
66 | * header (with len, off, ttl, proto, tos, src, dst).
|
---|
67 | * The mbuf chain containing the packet will be freed.
|
---|
68 | * The mbuf opt, if present, will not be freed.
|
---|
69 | */
|
---|
70 | int
|
---|
71 | ip_output(PNATState pData, struct socket *so, struct mbuf *m0)
|
---|
72 | {
|
---|
73 | register struct ip *ip;
|
---|
74 | register struct mbuf *m = m0;
|
---|
75 | register int hlen = sizeof(struct ip );
|
---|
76 | int len, off, error = 0;
|
---|
77 | extern uint8_t zerro_ethaddr[ETH_ALEN];
|
---|
78 | struct ethhdr *eh = NULL;
|
---|
79 | uint8_t eth_dst[ETH_ALEN];
|
---|
80 | int rc = 1;
|
---|
81 |
|
---|
82 | STAM_PROFILE_START(&pData->StatIP_output, a);
|
---|
83 |
|
---|
84 | DEBUG_CALL("ip_output");
|
---|
85 | DEBUG_ARG("so = %lx", (long)so);
|
---|
86 | DEBUG_ARG("m0 = %lx", (long)m0);
|
---|
87 | if(m->m_data != (MBUF_HEAD(m) + if_maxlinkhdr))
|
---|
88 | {
|
---|
89 | LogRel(("NAT: ethernet detects corruption of the packet"));
|
---|
90 | AssertMsgFailed(("!!Ethernet frame corrupted!!"));
|
---|
91 | }
|
---|
92 |
|
---|
93 | #if 0 /* We do no options */
|
---|
94 | if (opt)
|
---|
95 | {
|
---|
96 | m = ip_insertoptions(m, opt, &len);
|
---|
97 | hlen = len;
|
---|
98 | }
|
---|
99 | #endif
|
---|
100 | ip = mtod(m, struct ip *);
|
---|
101 | /*
|
---|
102 | * Fill in IP header.
|
---|
103 | */
|
---|
104 | ip->ip_v = IPVERSION;
|
---|
105 | ip->ip_off &= IP_DF;
|
---|
106 | ip->ip_id = htons(ip_currid++);
|
---|
107 | ip->ip_hl = hlen >> 2;
|
---|
108 | ipstat.ips_localout++;
|
---|
109 |
|
---|
110 | /*
|
---|
111 | * Verify that we have any chance at all of being able to queue
|
---|
112 | * the packet or packet fragments
|
---|
113 | */
|
---|
114 | #if 0 /* XXX Hmmm... */
|
---|
115 | if (if_queued > if_thresh && towrite <= 0)
|
---|
116 | {
|
---|
117 | error = ENOBUFS;
|
---|
118 | goto bad;
|
---|
119 | }
|
---|
120 | #endif
|
---|
121 | /* Current TCP/IP stack hasn't routing information at
|
---|
122 | * all so we need to calculate destination ethernet address
|
---|
123 | */
|
---|
124 | eh = (struct ethhdr *)MBUF_HEAD(m);
|
---|
125 | if (memcmp(eh->h_source, zerro_ethaddr, ETH_ALEN) == 0)
|
---|
126 | {
|
---|
127 | rc = rt_lookup_in_cache(pData, ip->ip_dst.s_addr, eth_dst);
|
---|
128 | if (rc != 0)
|
---|
129 | goto bad;
|
---|
130 | }
|
---|
131 | else
|
---|
132 | {
|
---|
133 | memcpy(eth_dst, eh->h_source, ETH_ALEN);
|
---|
134 | rc = 0; /*some times we've already know where to send packet*/
|
---|
135 | }
|
---|
136 |
|
---|
137 | /*
|
---|
138 | * If small enough for interface, can just send directly.
|
---|
139 | */
|
---|
140 | if ((u_int16_t)ip->ip_len <= if_mtu)
|
---|
141 | {
|
---|
142 | ip->ip_len = htons((u_int16_t)ip->ip_len);
|
---|
143 | ip->ip_off = htons((u_int16_t)ip->ip_off);
|
---|
144 | ip->ip_sum = 0;
|
---|
145 | ip->ip_sum = cksum(m, hlen);
|
---|
146 |
|
---|
147 | Assert((rc == 0));
|
---|
148 | memcpy(eh->h_source, eth_dst, ETH_ALEN);
|
---|
149 |
|
---|
150 | STAM_PROFILE_START(&pData->StatALIAS_output, a);
|
---|
151 | rc = LibAliasOut((m->m_la ? m->m_la : pData->proxy_alias),
|
---|
152 | mtod(m, char *), m->m_len);
|
---|
153 | Log2(("NAT: LibAlias return %d\n", rc));
|
---|
154 | STAM_PROFILE_STOP(&pData->StatALIAS_output, a);
|
---|
155 |
|
---|
156 | if_output(pData, so, m);
|
---|
157 | goto done;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /*
|
---|
161 | * Too large for interface; fragment if possible.
|
---|
162 | * Must be able to put at least 8 bytes per fragment.
|
---|
163 | */
|
---|
164 | if (ip->ip_off & IP_DF)
|
---|
165 | {
|
---|
166 | error = -1;
|
---|
167 | ipstat.ips_cantfrag++;
|
---|
168 | goto bad;
|
---|
169 | }
|
---|
170 |
|
---|
171 | len = (if_mtu - hlen) &~ 7; /* ip databytes per packet */
|
---|
172 | if (len < 8)
|
---|
173 | {
|
---|
174 | error = -1;
|
---|
175 | goto bad;
|
---|
176 | }
|
---|
177 |
|
---|
178 | {
|
---|
179 | int mhlen, firstlen = len;
|
---|
180 | struct mbuf **mnext = &m->m_nextpkt;
|
---|
181 |
|
---|
182 | /*
|
---|
183 | * Loop through length of segment after first fragment,
|
---|
184 | * make new header and copy data of each part and link onto chain.
|
---|
185 | */
|
---|
186 | m0 = m;
|
---|
187 | mhlen = sizeof (struct ip);
|
---|
188 | for (off = hlen + len; off < (u_int16_t)ip->ip_len; off += len)
|
---|
189 | {
|
---|
190 | register struct ip *mhip;
|
---|
191 | m = m_get(pData);
|
---|
192 | if (m == 0)
|
---|
193 | {
|
---|
194 | error = -1;
|
---|
195 | ipstat.ips_odropped++;
|
---|
196 | goto sendorfree;
|
---|
197 | }
|
---|
198 | m->m_data += if_maxlinkhdr;
|
---|
199 | mhip = mtod(m, struct ip *);
|
---|
200 | *mhip = *ip;
|
---|
201 | /* we've calculated eth_dst for first packet */
|
---|
202 | eh = (struct ethhdr *)MBUF_HEAD(m);
|
---|
203 | Assert((rc == 0));
|
---|
204 |
|
---|
205 | memcpy(eh->h_source, eth_dst, ETH_ALEN);
|
---|
206 |
|
---|
207 | #if 0 /* No options */
|
---|
208 | if (hlen > sizeof (struct ip))
|
---|
209 | {
|
---|
210 | mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
|
---|
211 | mhip->ip_hl = mhlen >> 2;
|
---|
212 | }
|
---|
213 | #endif
|
---|
214 | m->m_len = mhlen;
|
---|
215 | mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
|
---|
216 | if (ip->ip_off & IP_MF)
|
---|
217 | mhip->ip_off |= IP_MF;
|
---|
218 | if (off + len >= (u_int16_t)ip->ip_len)
|
---|
219 | len = (u_int16_t)ip->ip_len - off;
|
---|
220 | else
|
---|
221 | mhip->ip_off |= IP_MF;
|
---|
222 | mhip->ip_len = htons((u_int16_t)(len + mhlen));
|
---|
223 |
|
---|
224 | if (m_copy(m, m0, off, len) < 0)
|
---|
225 | {
|
---|
226 | error = -1;
|
---|
227 | goto sendorfree;
|
---|
228 | }
|
---|
229 |
|
---|
230 | mhip->ip_off = htons((u_int16_t)mhip->ip_off);
|
---|
231 | mhip->ip_sum = 0;
|
---|
232 | mhip->ip_sum = cksum(m, mhlen);
|
---|
233 | *mnext = m;
|
---|
234 | mnext = &m->m_nextpkt;
|
---|
235 | ipstat.ips_ofragments++;
|
---|
236 | }
|
---|
237 | /*
|
---|
238 | * Update first fragment by trimming what's been copied out
|
---|
239 | * and updating header, then send each fragment (in order).
|
---|
240 | */
|
---|
241 | m = m0;
|
---|
242 | m_adj(m, hlen + firstlen - (u_int16_t)ip->ip_len);
|
---|
243 | ip->ip_len = htons((u_int16_t)m->m_len);
|
---|
244 | ip->ip_off = htons((u_int16_t)(ip->ip_off | IP_MF));
|
---|
245 | ip->ip_sum = 0;
|
---|
246 | ip->ip_sum = cksum(m, hlen);
|
---|
247 |
|
---|
248 | {
|
---|
249 | STAM_PROFILE_START(&pData->StatALIAS_output, a);
|
---|
250 | rc = LibAliasOut((m->m_la ? m->m_la : pData->proxy_alias),
|
---|
251 | mtod(m, char *), m->m_len);
|
---|
252 | Log2(("NAT: LibAlias return %d\n", rc));
|
---|
253 | STAM_PROFILE_STOP(&pData->StatALIAS_output, a);
|
---|
254 | }
|
---|
255 |
|
---|
256 | sendorfree:
|
---|
257 | for (m = m0; m; m = m0)
|
---|
258 | {
|
---|
259 | m0 = m->m_nextpkt;
|
---|
260 | m->m_nextpkt = 0;
|
---|
261 | if (error == 0)
|
---|
262 | {
|
---|
263 | int rc;
|
---|
264 | rc = LibAliasOut((m->m_la ? m->m_la : pData->proxy_alias),
|
---|
265 | mtod(m, char *), m->m_len);
|
---|
266 | Log2(("NAT: LibAlias return %d\n", rc));
|
---|
267 | if_output(pData, so, m);
|
---|
268 | }
|
---|
269 | else
|
---|
270 | {
|
---|
271 | m_freem(pData, m);
|
---|
272 | }
|
---|
273 | }
|
---|
274 |
|
---|
275 | if (error == 0)
|
---|
276 | ipstat.ips_fragmented++;
|
---|
277 | }
|
---|
278 |
|
---|
279 | done:
|
---|
280 | STAM_PROFILE_STOP(&pData->StatIP_output, a);
|
---|
281 | return (error);
|
---|
282 |
|
---|
283 | bad:
|
---|
284 | m_freem(pData, m0);
|
---|
285 | STAM_PROFILE_STOP(&pData->StatIP_output, a);
|
---|
286 | goto done;
|
---|
287 | }
|
---|