VirtualBox

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

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

NAT: BSD mbuf related changeset + (slirp.c, ip_output).

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