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 const uint8_t broadcast_ethaddr[6] =
|
---|
49 | {
|
---|
50 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
|
---|
51 | };
|
---|
52 |
|
---|
53 | static int rt_lookup_in_cache(PNATState pData, uint32_t dst, uint8_t *ether)
|
---|
54 | {
|
---|
55 | int rc;
|
---|
56 | if (dst == INADDR_BROADCAST)
|
---|
57 | {
|
---|
58 | memcpy(ether, broadcast_ethaddr, ETH_ALEN);
|
---|
59 | return VINF_SUCCESS;
|
---|
60 | }
|
---|
61 |
|
---|
62 | rc = slirp_arp_lookup_ether_by_ip(pData, dst, ether);
|
---|
63 | if (RT_SUCCESS(rc))
|
---|
64 | return rc;
|
---|
65 |
|
---|
66 | rc = bootp_cache_lookup_ether_by_ip(pData, dst, ether);
|
---|
67 | if (RT_SUCCESS(rc))
|
---|
68 | return rc;
|
---|
69 | /*
|
---|
70 | * no chance to send this packet, sorry, we will request ether address via ARP
|
---|
71 | */
|
---|
72 | slirp_arp_who_has(pData, dst);
|
---|
73 | return VERR_NOT_FOUND;
|
---|
74 | }
|
---|
75 |
|
---|
76 | /*
|
---|
77 | * IP output. The packet in mbuf chain m contains a skeletal IP
|
---|
78 | * header (with len, off, ttl, proto, tos, src, dst).
|
---|
79 | * The mbuf chain containing the packet will be freed.
|
---|
80 | * The mbuf opt, if present, will not be freed.
|
---|
81 | */
|
---|
82 | int
|
---|
83 | ip_output(PNATState pData, struct socket *so, struct mbuf *m0)
|
---|
84 | {
|
---|
85 | return ip_output0(pData, so, m0, 0);
|
---|
86 | }
|
---|
87 |
|
---|
88 | int
|
---|
89 | ip_output0(PNATState pData, struct socket *so, struct mbuf *m0, int urg)
|
---|
90 | {
|
---|
91 | register struct ip *ip;
|
---|
92 | register struct mbuf *m = m0;
|
---|
93 | register int hlen = sizeof(struct ip );
|
---|
94 | int len, off, error = 0;
|
---|
95 | extern uint8_t zerro_ethaddr[ETH_ALEN];
|
---|
96 | struct ethhdr *eh = NULL;
|
---|
97 | uint8_t eth_dst[ETH_ALEN];
|
---|
98 | int rc = 1;
|
---|
99 |
|
---|
100 | STAM_PROFILE_START(&pData->StatIP_output, a);
|
---|
101 |
|
---|
102 | DEBUG_CALL("ip_output");
|
---|
103 | DEBUG_ARG("so = %lx", (long)so);
|
---|
104 | DEBUG_ARG("m0 = %lx", (long)m0);
|
---|
105 |
|
---|
106 | #ifndef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
107 | if(m->m_data != (MBUF_HEAD(m) + if_maxlinkhdr))
|
---|
108 | {
|
---|
109 | LogRel(("NAT: ethernet detects corruption of the packet"));
|
---|
110 | AssertMsgFailed(("!!Ethernet frame corrupted!!"));
|
---|
111 | }
|
---|
112 | #else
|
---|
113 | M_ASSERTPKTHDR(m);
|
---|
114 | Assert(m->m_pkthdr.header);
|
---|
115 | #endif
|
---|
116 |
|
---|
117 | #if 0 /* We do no options */
|
---|
118 | if (opt)
|
---|
119 | {
|
---|
120 | m = ip_insertoptions(m, opt, &len);
|
---|
121 | hlen = len;
|
---|
122 | }
|
---|
123 | #endif
|
---|
124 | ip = mtod(m, struct ip *);
|
---|
125 | /*
|
---|
126 | * Fill in IP header.
|
---|
127 | */
|
---|
128 | ip->ip_v = IPVERSION;
|
---|
129 | ip->ip_off &= IP_DF;
|
---|
130 | ip->ip_id = htons(ip_currid++);
|
---|
131 | ip->ip_hl = hlen >> 2;
|
---|
132 | ipstat.ips_localout++;
|
---|
133 |
|
---|
134 | /*
|
---|
135 | * Verify that we have any chance at all of being able to queue
|
---|
136 | * the packet or packet fragments
|
---|
137 | */
|
---|
138 | #if 0 /* XXX Hmmm... */
|
---|
139 | if (if_queued > if_thresh && towrite <= 0)
|
---|
140 | {
|
---|
141 | error = ENOBUFS;
|
---|
142 | goto exit_drop_package;
|
---|
143 | }
|
---|
144 | #endif
|
---|
145 | /* Current TCP/IP stack hasn't routing information at
|
---|
146 | * all so we need to calculate destination ethernet address
|
---|
147 | */
|
---|
148 | #ifndef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
149 | eh = (struct ethhdr *)MBUF_HEAD(m);
|
---|
150 | if (memcmp(eh->h_source, zerro_ethaddr, ETH_ALEN) == 0)
|
---|
151 | {
|
---|
152 | rc = rt_lookup_in_cache(pData, ip->ip_dst.s_addr, eth_dst);
|
---|
153 | if (RT_FAILURE(rc))
|
---|
154 | goto exit_drop_package;
|
---|
155 | }
|
---|
156 | else
|
---|
157 | {
|
---|
158 | memcpy(eth_dst, eh->h_source, ETH_ALEN);
|
---|
159 | rc = 0; /*some times we've already know where to send packet*/
|
---|
160 | }
|
---|
161 | #else
|
---|
162 | /*
|
---|
163 | * (vvl) Assumption is that m_data points at the IP header and only
|
---|
164 | * in case of dhcp we know and have header before IP.
|
---|
165 | */
|
---|
166 | rc = rt_lookup_in_cache(pData, ip->ip_dst.s_addr, eth_dst);
|
---|
167 | if (RT_FAILURE(rc))
|
---|
168 | goto exit_drop_package;
|
---|
169 |
|
---|
170 | eh = (struct ethhdr *)(m->m_data - ETH_HLEN);
|
---|
171 | #endif
|
---|
172 | /*
|
---|
173 | * If small enough for interface, can just send directly.
|
---|
174 | */
|
---|
175 | if ((u_int16_t)ip->ip_len <= if_mtu)
|
---|
176 | {
|
---|
177 | ip->ip_len = htons((u_int16_t)ip->ip_len);
|
---|
178 | ip->ip_off = htons((u_int16_t)ip->ip_off);
|
---|
179 | ip->ip_sum = 0;
|
---|
180 | ip->ip_sum = cksum(m, hlen);
|
---|
181 |
|
---|
182 | {
|
---|
183 | #ifndef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
184 | STAM_PROFILE_START(&pData->StatALIAS_output, a);
|
---|
185 | rc = LibAliasOut((m->m_la ? m->m_la : pData->proxy_alias),
|
---|
186 | mtod(m, char *), m->m_len);
|
---|
187 | Log2(("NAT: LibAlias return %d\n", rc));
|
---|
188 | #else
|
---|
189 | struct m_tag *t;
|
---|
190 | STAM_PROFILE_START(&pData->StatALIAS_output, a);
|
---|
191 | if (t = m_tag_find(m, PACKET_TAG_ALIAS, NULL) != 0)
|
---|
192 | rc = LibAliasOut((struct libalias *)&t[1], mtod(m, char *),
|
---|
193 | m_length(m, NULL));
|
---|
194 | else
|
---|
195 | rc = LibAliasOut(pData->proxy_alias, mtod(m, char *),
|
---|
196 | m_length(m, NULL));
|
---|
197 |
|
---|
198 | if (rc == PKT_ALIAS_IGNORED)
|
---|
199 | {
|
---|
200 | Log(("NAT: packet was droppped\n"));
|
---|
201 | goto exit_drop_package;
|
---|
202 | }
|
---|
203 | #endif
|
---|
204 | STAM_PROFILE_STOP(&pData->StatALIAS_output, a);
|
---|
205 | }
|
---|
206 |
|
---|
207 | memcpy(eh->h_source, eth_dst, ETH_ALEN);
|
---|
208 |
|
---|
209 | #ifdef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
210 | if_output(pData, so, m);
|
---|
211 | #else
|
---|
212 | if_encap(pData, ETH_P_IP, m, urg? ETH_ENCAP_URG : 0);
|
---|
213 | #endif
|
---|
214 | goto done;
|
---|
215 | }
|
---|
216 |
|
---|
217 | /*
|
---|
218 | * Too large for interface; fragment if possible.
|
---|
219 | * Must be able to put at least 8 bytes per fragment.
|
---|
220 | */
|
---|
221 | if (ip->ip_off & IP_DF)
|
---|
222 | {
|
---|
223 | error = -1;
|
---|
224 | ipstat.ips_cantfrag++;
|
---|
225 | goto exit_drop_package;
|
---|
226 | }
|
---|
227 |
|
---|
228 | len = (if_mtu - hlen) &~ 7; /* ip databytes per packet */
|
---|
229 | if (len < 8)
|
---|
230 | {
|
---|
231 | error = -1;
|
---|
232 | goto exit_drop_package;
|
---|
233 | }
|
---|
234 |
|
---|
235 | {
|
---|
236 | int mhlen, firstlen = len;
|
---|
237 | struct mbuf **mnext = &m->m_nextpkt;
|
---|
238 | #ifdef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
239 | uint8_t *buf; /* intermediate buffer we'll use for copy from orriginal packet*/
|
---|
240 | #endif
|
---|
241 | {
|
---|
242 | #ifdef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
243 | struct m_tag *t;
|
---|
244 | char *tmpbuf = NULL;
|
---|
245 | int tmplen = 0;
|
---|
246 | #endif
|
---|
247 | int rc;
|
---|
248 | HTONS(ip->ip_len);
|
---|
249 | HTONS(ip->ip_off);
|
---|
250 | ip->ip_sum = 0;
|
---|
251 | ip->ip_sum = cksum(m, hlen);
|
---|
252 | #ifndef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
253 | rc = LibAliasOut((m->m_la ? m->m_la : pData->proxy_alias),
|
---|
254 | mtod(m, char *), m->m_len);
|
---|
255 | #else
|
---|
256 | if (m->m_next != NULL)
|
---|
257 | {
|
---|
258 | /*we've receives packet in fragments*/
|
---|
259 | tmplen = m_length(m, NULL);
|
---|
260 | tmpbuf = RTMemAlloc(tmplen);
|
---|
261 | Assert(tmpbuf);
|
---|
262 | m_copydata(m, 0, tmplen, tmpbuf);
|
---|
263 | }
|
---|
264 | else
|
---|
265 | {
|
---|
266 | tmpbuf = mtod(m, char *);
|
---|
267 | tmplen = m_length(m, NULL);
|
---|
268 |
|
---|
269 | }
|
---|
270 |
|
---|
271 | if (t = m_tag_find(m, PACKET_TAG_ALIAS, NULL) != 0)
|
---|
272 | rc = LibAliasOut((struct libalias *)&t[1], tmpbuf, tmplen);
|
---|
273 | else
|
---|
274 | rc = LibAliasOut(pData->proxy_alias, tmpbuf, tmplen);
|
---|
275 |
|
---|
276 | if (m->m_next != NULL)
|
---|
277 | {
|
---|
278 | if (rc != PKT_ALIAS_IGNORED)
|
---|
279 | {
|
---|
280 | struct ip *tmpip = (struct ip *)tmpbuf;
|
---|
281 | m_copyback(pData, m, 0, ntohs(tmpip->ip_len) + (tmpip->ip_hl << 2), tmpbuf);
|
---|
282 | }
|
---|
283 | if (tmpbuf != NULL)
|
---|
284 | RTMemFree(tmpbuf);
|
---|
285 | }
|
---|
286 | if (rc == PKT_ALIAS_IGNORED)
|
---|
287 | {
|
---|
288 | Log(("NAT: packet was droppped\n"));
|
---|
289 | goto exit_drop_package;
|
---|
290 | }
|
---|
291 | #endif
|
---|
292 | NTOHS(ip->ip_len);
|
---|
293 | NTOHS(ip->ip_off);
|
---|
294 | Log2(("NAT: LibAlias return %d\n", rc));
|
---|
295 | }
|
---|
296 |
|
---|
297 | /*
|
---|
298 | * Loop through length of segment after first fragment,
|
---|
299 | * make new header and copy data of each part and link onto chain.
|
---|
300 | */
|
---|
301 | m0 = m;
|
---|
302 | mhlen = sizeof (struct ip);
|
---|
303 | for (off = hlen + len; off < (u_int16_t)ip->ip_len; off += len)
|
---|
304 | {
|
---|
305 | register struct ip *mhip;
|
---|
306 | #ifndef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
307 | m = m_get(pData);
|
---|
308 | #else
|
---|
309 | m = m_getcl(pData, M_NOWAIT, MT_HEADER, M_PKTHDR);
|
---|
310 | #endif
|
---|
311 | if (m == 0)
|
---|
312 | {
|
---|
313 | error = -1;
|
---|
314 | ipstat.ips_odropped++;
|
---|
315 | goto sendorfree;
|
---|
316 | }
|
---|
317 | m->m_data += if_maxlinkhdr;
|
---|
318 | mhip = mtod(m, struct ip *);
|
---|
319 | *mhip = *ip;
|
---|
320 | m->m_len += ip->ip_hl << 2;
|
---|
321 | #ifdef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
322 | m->m_pkthdr.header = mtod(m, void *);
|
---|
323 | #endif
|
---|
324 | /* we've calculated eth_dst for first packet */
|
---|
325 | #if 0 /* No options */
|
---|
326 | if (hlen > sizeof (struct ip))
|
---|
327 | {
|
---|
328 | mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
|
---|
329 | mhip->ip_hl = mhlen >> 2;
|
---|
330 | }
|
---|
331 | #endif
|
---|
332 | mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
|
---|
333 | if (ip->ip_off & IP_MF)
|
---|
334 | mhip->ip_off |= IP_MF;
|
---|
335 | if (off + len >= (u_int16_t)ip->ip_len)
|
---|
336 | len = (u_int16_t)ip->ip_len - off;
|
---|
337 | else
|
---|
338 | mhip->ip_off |= IP_MF;
|
---|
339 | mhip->ip_len = htons((u_int16_t)(len + mhlen));
|
---|
340 |
|
---|
341 | #ifndef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
342 | if (m_copy(m, m0, off, len) < 0)
|
---|
343 | {
|
---|
344 | error = -1;
|
---|
345 | goto sendorfree;
|
---|
346 | }
|
---|
347 | #else
|
---|
348 | buf = RTMemAlloc(len);
|
---|
349 | m_copydata(m0, off, len, buf); /* copy to buffer */
|
---|
350 | m->m_data += mhlen;
|
---|
351 | m_copyback(pData, m, 0, len, buf); /* copy from buffer */
|
---|
352 | m->m_data -= mhlen;
|
---|
353 | m->m_len += mhlen;
|
---|
354 | RTMemFree(buf);
|
---|
355 | m->m_len += ntohs(mhip->ip_len);
|
---|
356 | #endif
|
---|
357 |
|
---|
358 | mhip->ip_off = htons((u_int16_t)mhip->ip_off);
|
---|
359 | mhip->ip_sum = 0;
|
---|
360 | mhip->ip_sum = cksum(m, mhlen);
|
---|
361 | *mnext = m;
|
---|
362 | mnext = &m->m_nextpkt;
|
---|
363 | ipstat.ips_ofragments++;
|
---|
364 | }
|
---|
365 | /*
|
---|
366 | * Update first fragment by trimming what's been copied out
|
---|
367 | * and updating header, then send each fragment (in order).
|
---|
368 | */
|
---|
369 | m = m0;
|
---|
370 | m_adj(m, hlen + firstlen - (u_int16_t)ip->ip_len);
|
---|
371 | ip->ip_len = htons((u_int16_t)m->m_len);
|
---|
372 | ip->ip_off = htons((u_int16_t)(ip->ip_off | IP_MF));
|
---|
373 | ip->ip_sum = 0;
|
---|
374 | ip->ip_sum = cksum(m, hlen);
|
---|
375 |
|
---|
376 | sendorfree:
|
---|
377 | for (m = m0; m; m = m0)
|
---|
378 | {
|
---|
379 | m0 = m->m_nextpkt;
|
---|
380 | m->m_nextpkt = 0;
|
---|
381 | if (error == 0)
|
---|
382 | {
|
---|
383 | #ifndef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
384 | eh = (struct ethhdr *)MBUF_HEAD(m);
|
---|
385 | #else
|
---|
386 | m->m_data -= ETH_HLEN;
|
---|
387 | eh = mtod(m, struct ethhdr *);
|
---|
388 | m->m_data += ETH_HLEN;
|
---|
389 | #endif
|
---|
390 | memcpy(eh->h_source, eth_dst, ETH_ALEN);
|
---|
391 |
|
---|
392 | #ifdef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
393 | if_output(pData, so, m);
|
---|
394 | #else
|
---|
395 | if_encap(pData, ETH_P_IP, m, 0);
|
---|
396 | #endif
|
---|
397 | }
|
---|
398 | else
|
---|
399 | {
|
---|
400 | m_freem(pData, m);
|
---|
401 | }
|
---|
402 | }
|
---|
403 |
|
---|
404 | if (error == 0)
|
---|
405 | ipstat.ips_fragmented++;
|
---|
406 | }
|
---|
407 |
|
---|
408 | done:
|
---|
409 | STAM_PROFILE_STOP(&pData->StatIP_output, a);
|
---|
410 | return error;
|
---|
411 |
|
---|
412 | exit_drop_package:
|
---|
413 | m_freem(pData, m0);
|
---|
414 | STAM_PROFILE_STOP(&pData->StatIP_output, a);
|
---|
415 | return error;
|
---|
416 | }
|
---|