VirtualBox

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

Last change on this file since 22478 was 22478, checked in by vboxsync, 15 years ago

NAT: fix of save/restore problem (some todo still required)

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