VirtualBox

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

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

NAT: applied patch from xtracker 3993 (use BSD mbufs)

  • 屬性 svn:eol-style 設為 native
檔案大小: 12.5 KB
 
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
48static const uint8_t broadcast_ethaddr[6] =
49{
50 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
51};
52
53static 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 */
82int
83ip_output(PNATState pData, struct socket *so, struct mbuf *m0)
84{
85 return ip_output0(pData, so, m0, 0);
86}
87
88int
89ip_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 = RT_H2N_U16(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 = RT_H2N_U16((u_int16_t)ip->ip_len);
178 ip->ip_off = RT_H2N_U16((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, b);
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, b);
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, b);
205 }
206
207 memcpy(eh->h_source, eth_dst, ETH_ALEN);
208
209 if_encap(pData, ETH_P_IP, m, urg? ETH_ENCAP_URG : 0);
210 goto done;
211 }
212
213 /*
214 * Too large for interface; fragment if possible.
215 * Must be able to put at least 8 bytes per fragment.
216 */
217 if (ip->ip_off & IP_DF)
218 {
219 error = -1;
220 ipstat.ips_cantfrag++;
221 goto exit_drop_package;
222 }
223
224 len = (if_mtu - hlen) &~ 7; /* ip databytes per packet */
225 if (len < 8)
226 {
227 error = -1;
228 goto exit_drop_package;
229 }
230
231 {
232 int mhlen, firstlen = len;
233 struct mbuf **mnext = &m->m_nextpkt;
234#ifdef VBOX_WITH_SLIRP_BSD_MBUF
235 char *buf; /* intermediate buffer we'll use for copy from orriginal packet */
236#endif
237 {
238#ifdef VBOX_WITH_SLIRP_BSD_MBUF
239 struct m_tag *t;
240 char *tmpbuf = NULL;
241 int tmplen = 0;
242#endif
243 int rcLa;
244 HTONS(ip->ip_len);
245 HTONS(ip->ip_off);
246 ip->ip_sum = 0;
247 ip->ip_sum = cksum(m, hlen);
248#ifndef VBOX_WITH_SLIRP_BSD_MBUF
249 rcLa = LibAliasOut((m->m_la ? m->m_la : pData->proxy_alias),
250 mtod(m, char *), m->m_len);
251#else
252 if (m->m_next != NULL)
253 {
254 /*we've receives packet in fragments*/
255 tmplen = m_length(m, NULL);
256 tmpbuf = RTMemAlloc(tmplen);
257 Assert(tmpbuf);
258 m_copydata(m, 0, tmplen, tmpbuf);
259 }
260 else
261 {
262 tmpbuf = mtod(m, char *);
263 tmplen = m_length(m, NULL);
264
265 }
266
267 if ((t = m_tag_find(m, PACKET_TAG_ALIAS, NULL)) != 0)
268 rcLa = LibAliasOut((struct libalias *)&t[1], tmpbuf, tmplen);
269 else
270 rcLa = LibAliasOut(pData->proxy_alias, tmpbuf, tmplen);
271
272 if (m->m_next != NULL)
273 {
274 if (rcLa != PKT_ALIAS_IGNORED)
275 {
276 struct ip *tmpip = (struct ip *)tmpbuf;
277 m_copyback(pData, m, 0, RT_N2H_U16(tmpip->ip_len) + (tmpip->ip_hl << 2), tmpbuf);
278 }
279 if (tmpbuf != NULL)
280 RTMemFree(tmpbuf);
281 }
282 if (rcLa == PKT_ALIAS_IGNORED)
283 {
284 Log(("NAT: packet was droppped\n"));
285 goto exit_drop_package;
286 }
287#endif
288 NTOHS(ip->ip_len);
289 NTOHS(ip->ip_off);
290 Log2(("NAT: LibAlias return %d\n", rcLa));
291 }
292
293 /*
294 * Loop through length of segment after first fragment,
295 * make new header and copy data of each part and link onto chain.
296 */
297 m0 = m;
298 mhlen = sizeof (struct ip);
299 for (off = hlen + len; off < (u_int16_t)ip->ip_len; off += len)
300 {
301 register struct ip *mhip;
302#ifndef VBOX_WITH_SLIRP_BSD_MBUF
303 m = m_get(pData);
304#else
305 m = m_getcl(pData, M_NOWAIT, MT_HEADER, M_PKTHDR);
306#endif
307 if (m == 0)
308 {
309 error = -1;
310 ipstat.ips_odropped++;
311 goto sendorfree;
312 }
313 m->m_data += if_maxlinkhdr;
314 mhip = mtod(m, struct ip *);
315 *mhip = *ip;
316 m->m_len += ip->ip_hl << 2;
317#ifdef VBOX_WITH_SLIRP_BSD_MBUF
318 m->m_pkthdr.header = mtod(m, void *);
319#endif
320 /* we've calculated eth_dst for first packet */
321#if 0 /* No options */
322 if (hlen > sizeof (struct ip))
323 {
324 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
325 mhip->ip_hl = mhlen >> 2;
326 }
327#endif
328 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
329 if (ip->ip_off & IP_MF)
330 mhip->ip_off |= IP_MF;
331 if (off + len >= (u_int16_t)ip->ip_len)
332 len = (u_int16_t)ip->ip_len - off;
333 else
334 mhip->ip_off |= IP_MF;
335 mhip->ip_len = RT_H2N_U16((u_int16_t)(len + mhlen));
336
337#ifndef VBOX_WITH_SLIRP_BSD_MBUF
338 if (m_copy(m, m0, off, len) < 0)
339 {
340 error = -1;
341 goto sendorfree;
342 }
343#else
344 buf = RTMemAlloc(len);
345 m_copydata(m0, off, len, buf); /* copy to buffer */
346 m->m_data += mhlen;
347 m_copyback(pData, m, 0, len, buf); /* copy from buffer */
348 m->m_data -= mhlen;
349 m->m_len += mhlen;
350 RTMemFree(buf);
351 m->m_len += RT_N2H_U16(mhip->ip_len);
352#endif
353
354 mhip->ip_off = RT_H2N_U16((u_int16_t)mhip->ip_off);
355 mhip->ip_sum = 0;
356 mhip->ip_sum = cksum(m, mhlen);
357 *mnext = m;
358 mnext = &m->m_nextpkt;
359 ipstat.ips_ofragments++;
360 }
361 /*
362 * Update first fragment by trimming what's been copied out
363 * and updating header, then send each fragment (in order).
364 */
365 m = m0;
366 m_adj(m, hlen + firstlen - (u_int16_t)ip->ip_len);
367 ip->ip_len = RT_H2N_U16((u_int16_t)m->m_len);
368 ip->ip_off = RT_H2N_U16((u_int16_t)(ip->ip_off | IP_MF));
369 ip->ip_sum = 0;
370 ip->ip_sum = cksum(m, hlen);
371
372sendorfree:
373 for (m = m0; m; m = m0)
374 {
375 m0 = m->m_nextpkt;
376 m->m_nextpkt = 0;
377 if (error == 0)
378 {
379#ifndef VBOX_WITH_SLIRP_BSD_MBUF
380 eh = (struct ethhdr *)MBUF_HEAD(m);
381#else
382 m->m_data -= ETH_HLEN;
383 eh = mtod(m, struct ethhdr *);
384 m->m_data += ETH_HLEN;
385#endif
386 memcpy(eh->h_source, eth_dst, ETH_ALEN);
387
388 if_encap(pData, ETH_P_IP, m, 0);
389 }
390 else
391 {
392 m_freem(pData, m);
393 }
394 }
395
396 if (error == 0)
397 ipstat.ips_fragmented++;
398 }
399
400done:
401 STAM_PROFILE_STOP(&pData->StatIP_output, a);
402 return error;
403
404exit_drop_package:
405 m_freem(pData, m0);
406 STAM_PROFILE_STOP(&pData->StatIP_output, a);
407 return error;
408}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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