VirtualBox

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

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

NAT: Big changeset:

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

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