VirtualBox

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

Last change on this file since 20376 was 20164, checked in by vboxsync, 16 years ago

NAT/libalias: port-forwarding

  • Property svn:eol-style set to native
File size: 8.9 KB
Line 
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((m->m_la ? m->m_la : pData->proxy_alias),
156 mtod(m, char *), m->m_len);
157 Log2(("NAT: LibAlias return %d\n", rc));
158 }
159#endif
160
161 if_output(pData, so, m);
162 goto done;
163 }
164
165 /*
166 * Too large for interface; fragment if possible.
167 * Must be able to put at least 8 bytes per fragment.
168 */
169 if (ip->ip_off & IP_DF)
170 {
171 error = -1;
172 ipstat.ips_cantfrag++;
173 goto bad;
174 }
175
176 len = (if_mtu - hlen) &~ 7; /* ip databytes per packet */
177 if (len < 8)
178 {
179 error = -1;
180 goto bad;
181 }
182
183 {
184 int mhlen, firstlen = len;
185 struct mbuf **mnext = &m->m_nextpkt;
186
187 /*
188 * Loop through length of segment after first fragment,
189 * make new header and copy data of each part and link onto chain.
190 */
191 m0 = m;
192 mhlen = sizeof (struct ip);
193 for (off = hlen + len; off < (u_int16_t)ip->ip_len; off += len)
194 {
195 register struct ip *mhip;
196 m = m_get(pData);
197 if (m == 0)
198 {
199 error = -1;
200 ipstat.ips_odropped++;
201 goto sendorfree;
202 }
203 m_adj(m, if_maxlinkhdr);
204 mhip = mtod(m, struct ip *);
205 *mhip = *ip;
206#ifdef VBOX_WITHOUT_SLIRP_CLIENT_ETHER
207 /* we've calculated eth_dst for first packet */
208 eh = (struct ethhdr *)MBUF_HEAD(m);
209 if (eth_dst != NULL) {
210 memcpy(eh->h_source, eth_dst, ETH_ALEN);
211 }
212#endif
213
214#if 0 /* No options */
215 if (hlen > sizeof (struct ip))
216 {
217 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
218 mhip->ip_hl = mhlen >> 2;
219 }
220#endif
221 m->m_len = mhlen;
222 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
223 if (ip->ip_off & IP_MF)
224 mhip->ip_off |= IP_MF;
225 if (off + len >= (u_int16_t)ip->ip_len)
226 len = (u_int16_t)ip->ip_len - off;
227 else
228 mhip->ip_off |= IP_MF;
229 mhip->ip_len = htons((u_int16_t)(len + mhlen));
230
231 if (m_copy(m, m0, off, len) < 0)
232 {
233 error = -1;
234 goto sendorfree;
235 }
236
237 mhip->ip_off = htons((u_int16_t)mhip->ip_off);
238 mhip->ip_sum = 0;
239 mhip->ip_sum = cksum(m, mhlen);
240 *mnext = m;
241 mnext = &m->m_nextpkt;
242 ipstat.ips_ofragments++;
243 }
244 /*
245 * Update first fragment by trimming what's been copied out
246 * and updating header, then send each fragment (in order).
247 */
248 m = m0;
249 m_adj(m, hlen + firstlen - (u_int16_t)ip->ip_len);
250 ip->ip_len = htons((u_int16_t)m->m_len);
251 ip->ip_off = htons((u_int16_t)(ip->ip_off | IP_MF));
252 ip->ip_sum = 0;
253 ip->ip_sum = cksum(m, hlen);
254#ifdef VBOX_WITH_SLIRP_ALIAS
255 {
256 int rc;
257 rc = LibAliasOut((m->m_la ? m->m_la : pData->proxy_alias),
258 mtod(m, char *), m->m_len);
259 Log2(("NAT: LibAlias return %d\n", rc));
260 }
261#endif
262
263sendorfree:
264 for (m = m0; m; m = m0)
265 {
266 m0 = m->m_nextpkt;
267 m->m_nextpkt = 0;
268 if (error == 0)
269 {
270#ifdef VBOX_WITH_SLIRP_ALIAS
271 {
272 int rc;
273 rc = LibAliasOut((m->m_la ? m->m_la : pData->proxy_alias),
274 mtod(m, char *), m->m_len);
275 Log2(("NAT: LibAlias return %d\n", rc));
276 }
277#endif
278 if_output(pData, so, m);
279 }
280 else
281 {
282 m_freem(pData, m);
283 }
284 }
285
286 if (error == 0)
287 ipstat.ips_fragmented++;
288 }
289
290done:
291 return (error);
292
293bad:
294 m_freem(pData, m0);
295 goto done;
296}
Note: See TracBrowser for help on using the repository browser.

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