VirtualBox

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

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

NAT: small cosmetics, trailing spaces

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

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