VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/ip_output.c@ 28449

最後變更 在這個檔案從28449是 28449,由 vboxsync 提交於 15 年 前

NAT: slirp file headers

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 13.3 KB
 
1/* $Id: ip_output.c 28449 2010-04-19 09:52:59Z vboxsync $ */
2/** @file
3 * NAT - IP output.
4 */
5
6/*
7 * Copyright (C) 2006-2010 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22/*
23 * This code is based on:
24 *
25 * Copyright (c) 1982, 1986, 1988, 1990, 1993
26 * The Regents of the University of California. All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 *
56 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
57 * ip_output.c,v 1.9 1994/11/16 10:17:10 jkh Exp
58 */
59
60/*
61 * Changes and additions relating to SLiRP are
62 * Copyright (c) 1995 Danny Gasparovski.
63 *
64 * Please read the file COPYRIGHT for the
65 * terms and conditions of the copyright.
66 */
67
68#include <slirp.h>
69#include "alias.h"
70
71static const uint8_t broadcast_ethaddr[6] =
72{
73 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
74};
75
76static int rt_lookup_in_cache(PNATState pData, uint32_t dst, uint8_t *ether)
77{
78 int rc;
79 if (dst == INADDR_BROADCAST)
80 {
81 memcpy(ether, broadcast_ethaddr, ETH_ALEN);
82 return VINF_SUCCESS;
83 }
84
85 rc = slirp_arp_lookup_ether_by_ip(pData, dst, ether);
86 if (RT_SUCCESS(rc))
87 return rc;
88
89 rc = bootp_cache_lookup_ether_by_ip(pData, dst, ether);
90 if (RT_SUCCESS(rc))
91 return rc;
92 /*
93 * no chance to send this packet, sorry, we will request ether address via ARP
94 */
95 slirp_arp_who_has(pData, dst);
96 return VERR_NOT_FOUND;
97}
98
99/*
100 * IP output. The packet in mbuf chain m contains a skeletal IP
101 * header (with len, off, ttl, proto, tos, src, dst).
102 * The mbuf chain containing the packet will be freed.
103 * The mbuf opt, if present, will not be freed.
104 */
105int
106ip_output(PNATState pData, struct socket *so, struct mbuf *m0)
107{
108 return ip_output0(pData, so, m0, 0);
109}
110
111int
112ip_output0(PNATState pData, struct socket *so, struct mbuf *m0, int urg)
113{
114 register struct ip *ip;
115 register struct mbuf *m = m0;
116 register int hlen = sizeof(struct ip );
117 int len, off, error = 0;
118 extern uint8_t zerro_ethaddr[ETH_ALEN];
119 struct ethhdr *eh = NULL;
120 uint8_t eth_dst[ETH_ALEN];
121 int rc = 1;
122
123 STAM_PROFILE_START(&pData->StatIP_output, a);
124
125 DEBUG_CALL("ip_output");
126 DEBUG_ARG("so = %lx", (long)so);
127 DEBUG_ARG("m0 = %lx", (long)m0);
128
129#ifndef VBOX_WITH_SLIRP_BSD_MBUF
130 if(m->m_data != (MBUF_HEAD(m) + if_maxlinkhdr))
131 {
132 LogRel(("NAT: ethernet detects corruption of the packet"));
133 AssertMsgFailed(("!!Ethernet frame corrupted!!"));
134 }
135#else
136 M_ASSERTPKTHDR(m);
137 Assert(m->m_pkthdr.header);
138#endif
139
140#if 0 /* We do no options */
141 if (opt)
142 {
143 m = ip_insertoptions(m, opt, &len);
144 hlen = len;
145 }
146#endif
147 ip = mtod(m, struct ip *);
148 /*
149 * Fill in IP header.
150 */
151 ip->ip_v = IPVERSION;
152 ip->ip_off &= IP_DF;
153 ip->ip_id = RT_H2N_U16(ip_currid++);
154 ip->ip_hl = hlen >> 2;
155 ipstat.ips_localout++;
156
157 /*
158 * Verify that we have any chance at all of being able to queue
159 * the packet or packet fragments
160 */
161#if 0 /* XXX Hmmm... */
162 if (if_queued > if_thresh && towrite <= 0)
163 {
164 error = ENOBUFS;
165 goto exit_drop_package;
166 }
167#endif
168 /* Current TCP/IP stack hasn't routing information at
169 * all so we need to calculate destination ethernet address
170 */
171#ifndef VBOX_WITH_SLIRP_BSD_MBUF
172 eh = (struct ethhdr *)MBUF_HEAD(m);
173 if (memcmp(eh->h_source, zerro_ethaddr, ETH_ALEN) == 0)
174 {
175 rc = rt_lookup_in_cache(pData, ip->ip_dst.s_addr, eth_dst);
176 if (RT_FAILURE(rc))
177 goto exit_drop_package;
178 }
179 else
180 {
181 memcpy(eth_dst, eh->h_source, ETH_ALEN);
182 rc = 0; /*some times we've already know where to send packet*/
183 }
184#else
185 /*
186 * (vvl) Assumption is that m_data points at the IP header and only
187 * in case of dhcp we know and have header before IP.
188 */
189 rc = rt_lookup_in_cache(pData, ip->ip_dst.s_addr, eth_dst);
190 if (RT_FAILURE(rc))
191 goto exit_drop_package;
192
193 eh = (struct ethhdr *)(m->m_data - ETH_HLEN);
194#endif
195 /*
196 * If small enough for interface, can just send directly.
197 */
198 if ((u_int16_t)ip->ip_len <= if_mtu)
199 {
200 ip->ip_len = RT_H2N_U16((u_int16_t)ip->ip_len);
201 ip->ip_off = RT_H2N_U16((u_int16_t)ip->ip_off);
202 ip->ip_sum = 0;
203 ip->ip_sum = cksum(m, hlen);
204
205 {
206#ifndef VBOX_WITH_SLIRP_BSD_MBUF
207 STAM_PROFILE_START(&pData->StatALIAS_output, b);
208 rc = LibAliasOut((m->m_la ? m->m_la : pData->proxy_alias),
209 mtod(m, char *), m->m_len);
210 Log2(("NAT: LibAlias return %d\n", rc));
211#else
212 struct m_tag *t;
213 STAM_PROFILE_START(&pData->StatALIAS_output, b);
214 if ((t = m_tag_find(m, PACKET_TAG_ALIAS, NULL)) != 0)
215 rc = LibAliasOut((struct libalias *)&t[1], mtod(m, char *),
216 m_length(m, NULL));
217 else
218 rc = LibAliasOut(pData->proxy_alias, mtod(m, char *),
219 m_length(m, NULL));
220
221 if (rc == PKT_ALIAS_IGNORED)
222 {
223 Log(("NAT: packet was droppped\n"));
224 goto exit_drop_package;
225 }
226#endif
227 STAM_PROFILE_STOP(&pData->StatALIAS_output, b);
228 }
229
230 memcpy(eh->h_source, eth_dst, ETH_ALEN);
231
232 if_encap(pData, ETH_P_IP, m, urg? ETH_ENCAP_URG : 0);
233 goto done;
234 }
235
236 /*
237 * Too large for interface; fragment if possible.
238 * Must be able to put at least 8 bytes per fragment.
239 */
240 if (ip->ip_off & IP_DF)
241 {
242 error = -1;
243 ipstat.ips_cantfrag++;
244 goto exit_drop_package;
245 }
246
247 len = (if_mtu - hlen) &~ 7; /* ip databytes per packet */
248 if (len < 8)
249 {
250 error = -1;
251 goto exit_drop_package;
252 }
253
254 {
255 int mhlen, firstlen = len;
256 struct mbuf **mnext = &m->m_nextpkt;
257#ifdef VBOX_WITH_SLIRP_BSD_MBUF
258 char *buf; /* intermediate buffer we'll use for copy from orriginal packet */
259#endif
260 {
261#ifdef VBOX_WITH_SLIRP_BSD_MBUF
262 struct m_tag *t;
263 char *tmpbuf = NULL;
264 int tmplen = 0;
265#endif
266 int rcLa;
267 HTONS(ip->ip_len);
268 HTONS(ip->ip_off);
269 ip->ip_sum = 0;
270 ip->ip_sum = cksum(m, hlen);
271#ifndef VBOX_WITH_SLIRP_BSD_MBUF
272 rcLa = LibAliasOut((m->m_la ? m->m_la : pData->proxy_alias),
273 mtod(m, char *), m->m_len);
274#else
275 if (m->m_next != NULL)
276 {
277 /*we've receives packet in fragments*/
278 tmplen = m_length(m, NULL);
279 tmpbuf = RTMemAlloc(tmplen);
280 Assert(tmpbuf);
281 m_copydata(m, 0, tmplen, tmpbuf);
282 }
283 else
284 {
285 tmpbuf = mtod(m, char *);
286 tmplen = m_length(m, NULL);
287
288 }
289
290 if ((t = m_tag_find(m, PACKET_TAG_ALIAS, NULL)) != 0)
291 rcLa = LibAliasOut((struct libalias *)&t[1], tmpbuf, tmplen);
292 else
293 rcLa = LibAliasOut(pData->proxy_alias, tmpbuf, tmplen);
294
295 if (m->m_next != NULL)
296 {
297 if (rcLa != PKT_ALIAS_IGNORED)
298 {
299 struct ip *tmpip = (struct ip *)tmpbuf;
300 m_copyback(pData, m, 0, RT_N2H_U16(tmpip->ip_len) + (tmpip->ip_hl << 2), tmpbuf);
301 }
302 if (tmpbuf != NULL)
303 RTMemFree(tmpbuf);
304 }
305 if (rcLa == PKT_ALIAS_IGNORED)
306 {
307 Log(("NAT: packet was droppped\n"));
308 goto exit_drop_package;
309 }
310#endif
311 NTOHS(ip->ip_len);
312 NTOHS(ip->ip_off);
313 Log2(("NAT: LibAlias return %d\n", rcLa));
314 }
315
316 /*
317 * Loop through length of segment after first fragment,
318 * make new header and copy data of each part and link onto chain.
319 */
320 m0 = m;
321 mhlen = sizeof (struct ip);
322 for (off = hlen + len; off < (u_int16_t)ip->ip_len; off += len)
323 {
324 register struct ip *mhip;
325#ifndef VBOX_WITH_SLIRP_BSD_MBUF
326 m = m_get(pData);
327#else
328 m = m_getcl(pData, M_NOWAIT, MT_HEADER, M_PKTHDR);
329#endif
330 if (m == 0)
331 {
332 error = -1;
333 ipstat.ips_odropped++;
334 goto sendorfree;
335 }
336 m->m_data += if_maxlinkhdr;
337 mhip = mtod(m, struct ip *);
338 *mhip = *ip;
339 m->m_len += ip->ip_hl << 2;
340#ifdef VBOX_WITH_SLIRP_BSD_MBUF
341 m->m_pkthdr.header = mtod(m, void *);
342#endif
343 /* we've calculated eth_dst for first packet */
344#if 0 /* No options */
345 if (hlen > sizeof (struct ip))
346 {
347 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
348 mhip->ip_hl = mhlen >> 2;
349 }
350#endif
351 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
352 if (ip->ip_off & IP_MF)
353 mhip->ip_off |= IP_MF;
354 if (off + len >= (u_int16_t)ip->ip_len)
355 len = (u_int16_t)ip->ip_len - off;
356 else
357 mhip->ip_off |= IP_MF;
358 mhip->ip_len = RT_H2N_U16((u_int16_t)(len + mhlen));
359
360#ifndef VBOX_WITH_SLIRP_BSD_MBUF
361 if (m_copy(m, m0, off, len) < 0)
362 {
363 error = -1;
364 goto sendorfree;
365 }
366#else
367 buf = RTMemAlloc(len);
368 m_copydata(m0, off, len, buf); /* copy to buffer */
369 m->m_data += mhlen;
370 m_copyback(pData, m, 0, len, buf); /* copy from buffer */
371 m->m_data -= mhlen;
372 m->m_len += mhlen;
373 RTMemFree(buf);
374 m->m_len += RT_N2H_U16(mhip->ip_len);
375#endif
376
377 mhip->ip_off = RT_H2N_U16((u_int16_t)mhip->ip_off);
378 mhip->ip_sum = 0;
379 mhip->ip_sum = cksum(m, mhlen);
380 *mnext = m;
381 mnext = &m->m_nextpkt;
382 ipstat.ips_ofragments++;
383 }
384 /*
385 * Update first fragment by trimming what's been copied out
386 * and updating header, then send each fragment (in order).
387 */
388 m = m0;
389 m_adj(m, hlen + firstlen - (u_int16_t)ip->ip_len);
390 ip->ip_len = RT_H2N_U16((u_int16_t)m->m_len);
391 ip->ip_off = RT_H2N_U16((u_int16_t)(ip->ip_off | IP_MF));
392 ip->ip_sum = 0;
393 ip->ip_sum = cksum(m, hlen);
394
395sendorfree:
396 for (m = m0; m; m = m0)
397 {
398 m0 = m->m_nextpkt;
399 m->m_nextpkt = 0;
400 if (error == 0)
401 {
402#ifndef VBOX_WITH_SLIRP_BSD_MBUF
403 eh = (struct ethhdr *)MBUF_HEAD(m);
404#else
405 m->m_data -= ETH_HLEN;
406 eh = mtod(m, struct ethhdr *);
407 m->m_data += ETH_HLEN;
408#endif
409 memcpy(eh->h_source, eth_dst, ETH_ALEN);
410
411 if_encap(pData, ETH_P_IP, m, 0);
412 }
413 else
414 {
415 m_freem(pData, m);
416 }
417 }
418
419 if (error == 0)
420 ipstat.ips_fragmented++;
421 }
422
423done:
424 STAM_PROFILE_STOP(&pData->StatIP_output, a);
425 return error;
426
427exit_drop_package:
428 m_freem(pData, m0);
429 STAM_PROFILE_STOP(&pData->StatIP_output, a);
430 return error;
431}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette