VirtualBox

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

最後變更 在這個檔案從20106是 20053,由 vboxsync 提交於 16 年 前

NAT: LibAlias enabling + tcp_emu replaced with ftp_module

  • 屬性 svn:eol-style 設為 native
檔案大小: 8.8 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#ifdef VBOX_WITH_SLIRP_ALIAS
47# include "alias.h"
48#endif
49
50#ifdef VBOX_WITHOUT_SLIRP_CLIENT_ETHER
51static const uint8_t* rt_lookup_in_cache(PNATState pData, uint32_t dst)
52{
53 int i;
54 /* @todo (r - vasily) to quick ramp up on routing rails
55 * we use information from DHCP server leasings, this
56 * code couldn't detect any changes in network topology
57 * and should be borrowed from other places
58 */
59 for (i = 0; i < NB_ADDR; i++)
60 {
61 if ( bootp_clients[i].allocated
62 && bootp_clients[i].addr.s_addr == dst)
63 return &bootp_clients[i].macaddr[0];
64 }
65
66 if (dst != 0)
67 return pData->slirp_ethaddr;
68
69 return NULL;
70}
71#endif
72
73/*
74 * IP output. The packet in mbuf chain m contains a skeletal IP
75 * header (with len, off, ttl, proto, tos, src, dst).
76 * The mbuf chain containing the packet will be freed.
77 * The mbuf opt, if present, will not be freed.
78 */
79int
80ip_output(PNATState pData, struct socket *so, struct mbuf *m0)
81{
82 register struct ip *ip;
83 register struct mbuf *m = m0;
84 register int hlen = sizeof(struct ip );
85 int len, off, error = 0;
86#ifdef VBOX_WITHOUT_SLIRP_CLIENT_ETHER
87 extern uint8_t zerro_ethaddr[ETH_ALEN];
88 struct ethhdr *eh = NULL;
89 const uint8_t *eth_dst = NULL;
90#endif
91
92 DEBUG_CALL("ip_output");
93 DEBUG_ARG("so = %lx", (long)so);
94 DEBUG_ARG("m0 = %lx", (long)m0);
95 if(m->m_data != (MBUF_HEAD(m) + if_maxlinkhdr))
96 {
97 LogRel(("NAT: ethernet detects corruption of the packet"));
98 AssertMsgFailed(("!!Ethernet frame corrupted!!"));
99 }
100
101#if 0 /* We do no options */
102 if (opt)
103 {
104 m = ip_insertoptions(m, opt, &len);
105 hlen = len;
106 }
107#endif
108 ip = mtod(m, struct ip *);
109 /*
110 * Fill in IP header.
111 */
112 ip->ip_v = IPVERSION;
113 ip->ip_off &= IP_DF;
114 ip->ip_id = htons(ip_currid++);
115 ip->ip_hl = hlen >> 2;
116 ipstat.ips_localout++;
117
118 /*
119 * Verify that we have any chance at all of being able to queue
120 * the packet or packet fragments
121 */
122#if 0 /* XXX Hmmm... */
123 if (if_queued > if_thresh && towrite <= 0)
124 {
125 error = ENOBUFS;
126 goto bad;
127 }
128#endif
129#ifdef VBOX_WITHOUT_SLIRP_CLIENT_ETHER
130 /* Current TCP/IP stack hasn't routing information at
131 * all so we need to calculate destination ethernet address
132 */
133 eh = (struct ethhdr *)MBUF_HEAD(m);
134 if (memcmp(eh->h_source, zerro_ethaddr, ETH_ALEN) == 0)
135 eth_dst = rt_lookup_in_cache(pData, ip->ip_dst.s_addr);
136#endif
137
138 /*
139 * If small enough for interface, can just send directly.
140 */
141 if ((u_int16_t)ip->ip_len <= if_mtu)
142 {
143 ip->ip_len = htons((u_int16_t)ip->ip_len);
144 ip->ip_off = htons((u_int16_t)ip->ip_off);
145 ip->ip_sum = 0;
146 ip->ip_sum = cksum(m, hlen);
147#ifdef VBOX_WITHOUT_SLIRP_CLIENT_ETHER
148 if (eth_dst != NULL) {
149 memcpy(eh->h_source, eth_dst, ETH_ALEN);
150 }
151#endif
152#ifdef VBOX_WITH_SLIRP_ALIAS
153 {
154 int rc;
155 rc = LibAliasOut(LIST_FIRST(&instancehead), mtod(m, char *), m->m_len);
156 Log2(("NAT: LibAlias return %d\n", rc));
157 }
158#endif
159
160 if_output(pData, so, m);
161 goto done;
162 }
163
164 /*
165 * Too large for interface; fragment if possible.
166 * Must be able to put at least 8 bytes per fragment.
167 */
168 if (ip->ip_off & IP_DF)
169 {
170 error = -1;
171 ipstat.ips_cantfrag++;
172 goto bad;
173 }
174
175 len = (if_mtu - hlen) &~ 7; /* ip databytes per packet */
176 if (len < 8)
177 {
178 error = -1;
179 goto bad;
180 }
181
182 {
183 int mhlen, firstlen = len;
184 struct mbuf **mnext = &m->m_nextpkt;
185
186 /*
187 * Loop through length of segment after first fragment,
188 * make new header and copy data of each part and link onto chain.
189 */
190 m0 = m;
191 mhlen = sizeof (struct ip);
192 for (off = hlen + len; off < (u_int16_t)ip->ip_len; off += len)
193 {
194 register struct ip *mhip;
195 m = m_get(pData);
196 if (m == 0)
197 {
198 error = -1;
199 ipstat.ips_odropped++;
200 goto sendorfree;
201 }
202 m_adj(m, if_maxlinkhdr);
203 mhip = mtod(m, struct ip *);
204 *mhip = *ip;
205#ifdef VBOX_WITHOUT_SLIRP_CLIENT_ETHER
206 /* we've calculated eth_dst for first packet */
207 eh = (struct ethhdr *)MBUF_HEAD(m);
208 if (eth_dst != NULL) {
209 memcpy(eh->h_source, eth_dst, ETH_ALEN);
210 }
211#endif
212
213#if 0 /* No options */
214 if (hlen > sizeof (struct ip))
215 {
216 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
217 mhip->ip_hl = mhlen >> 2;
218 }
219#endif
220 m->m_len = mhlen;
221 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
222 if (ip->ip_off & IP_MF)
223 mhip->ip_off |= IP_MF;
224 if (off + len >= (u_int16_t)ip->ip_len)
225 len = (u_int16_t)ip->ip_len - off;
226 else
227 mhip->ip_off |= IP_MF;
228 mhip->ip_len = htons((u_int16_t)(len + mhlen));
229
230 if (m_copy(m, m0, off, len) < 0)
231 {
232 error = -1;
233 goto sendorfree;
234 }
235
236 mhip->ip_off = htons((u_int16_t)mhip->ip_off);
237 mhip->ip_sum = 0;
238 mhip->ip_sum = cksum(m, mhlen);
239 *mnext = m;
240 mnext = &m->m_nextpkt;
241 ipstat.ips_ofragments++;
242 }
243 /*
244 * Update first fragment by trimming what's been copied out
245 * and updating header, then send each fragment (in order).
246 */
247 m = m0;
248 m_adj(m, hlen + firstlen - (u_int16_t)ip->ip_len);
249 ip->ip_len = htons((u_int16_t)m->m_len);
250 ip->ip_off = htons((u_int16_t)(ip->ip_off | IP_MF));
251 ip->ip_sum = 0;
252 ip->ip_sum = cksum(m, hlen);
253#ifdef VBOX_WITH_SLIRP_ALIAS
254 {
255 int rc;
256 rc = LibAliasOut(LIST_FIRST(&instancehead), mtod(m, char *), m->m_len);
257 Log2(("NAT: LibAlias return %d\n", rc));
258 }
259#endif
260
261sendorfree:
262 for (m = m0; m; m = m0)
263 {
264 m0 = m->m_nextpkt;
265 m->m_nextpkt = 0;
266 if (error == 0)
267 {
268#ifdef VBOX_WITH_SLIRP_ALIAS
269 {
270 int rc;
271 rc = LibAliasOut(LIST_FIRST(&instancehead), mtod(m, char *), m->m_len);
272 Log2(("NAT: LibAlias return %d\n", rc));
273 }
274#endif
275 if_output(pData, so, m);
276 }
277 else
278 {
279 m_freem(pData, m);
280 }
281 }
282
283 if (error == 0)
284 ipstat.ips_fragmented++;
285 }
286
287done:
288 return (error);
289
290bad:
291 m_freem(pData, m0);
292 goto done;
293}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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