VirtualBox

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

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

NAT: warnings and style

  • Property svn:eol-style set to native
File size: 8.0 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
47#ifdef VBOX_WITHOUT_SLIRP_CLIENT_ETHER
48static const uint8_t* rt_lookup_in_cache(PNATState pData, uint32_t dst)
49{
50 int i;
51 /* @todo (r - 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 if (dst != 0)
64 return pData->slirp_ethaddr;
65
66 return NULL;
67}
68#endif
69
70/*
71 * IP output. The packet in mbuf chain m contains a skeletal IP
72 * header (with len, off, ttl, proto, tos, src, dst).
73 * The mbuf chain containing the packet will be freed.
74 * The mbuf opt, if present, will not be freed.
75 */
76int
77ip_output(PNATState pData, struct socket *so, struct mbuf *m0)
78{
79 register struct ip *ip;
80 register struct mbuf *m = m0;
81 register int hlen = sizeof(struct ip );
82 int len, off, error = 0;
83#ifdef VBOX_WITHOUT_SLIRP_CLIENT_ETHER
84 extern uint8_t zerro_ethaddr[ETH_ALEN];
85 struct ethhdr *eh = NULL;
86 const uint8_t *eth_dst = NULL;
87#endif
88
89 DEBUG_CALL("ip_output");
90 DEBUG_ARG("so = %lx", (long)so);
91 DEBUG_ARG("m0 = %lx", (long)m0);
92 if(m->m_data != (MBUF_HEAD(m) + if_maxlinkhdr))
93 {
94 LogRel(("NAT: ethernet detects corruption of the packet"));
95 AssertMsgFailed(("!!Ethernet frame corrupted!!"));
96 }
97
98#if 0 /* We do no options */
99 if (opt)
100 {
101 m = ip_insertoptions(m, opt, &len);
102 hlen = len;
103 }
104#endif
105 ip = mtod(m, struct ip *);
106 /*
107 * Fill in IP header.
108 */
109 ip->ip_v = IPVERSION;
110 ip->ip_off &= IP_DF;
111 ip->ip_id = htons(ip_currid++);
112 ip->ip_hl = hlen >> 2;
113 ipstat.ips_localout++;
114
115 /*
116 * Verify that we have any chance at all of being able to queue
117 * the packet or packet fragments
118 */
119#if 0 /* XXX Hmmm... */
120 if (if_queued > if_thresh && towrite <= 0)
121 {
122 error = ENOBUFS;
123 goto bad;
124 }
125#endif
126#ifdef VBOX_WITHOUT_SLIRP_CLIENT_ETHER
127 /* Current TCP/IP stack hasn't routing information at
128 * all so we need to calculate destination ethernet address
129 */
130 eh = (struct ethhdr *)MBUF_HEAD(m);
131 if (memcmp(eh->h_source, zerro_ethaddr, ETH_ALEN) == 0)
132 eth_dst = rt_lookup_in_cache(pData, ip->ip_dst.s_addr);
133#endif
134
135 /*
136 * If small enough for interface, can just send directly.
137 */
138 if ((u_int16_t)ip->ip_len <= if_mtu)
139 {
140 ip->ip_len = htons((u_int16_t)ip->ip_len);
141 ip->ip_off = htons((u_int16_t)ip->ip_off);
142 ip->ip_sum = 0;
143 ip->ip_sum = cksum(m, hlen);
144#ifdef VBOX_WITHOUT_SLIRP_CLIENT_ETHER
145 if (eth_dst != NULL) {
146 memcpy(eh->h_source, eth_dst, ETH_ALEN);
147 }
148#endif
149
150 if_output(pData, so, m);
151 goto done;
152 }
153
154 /*
155 * Too large for interface; fragment if possible.
156 * Must be able to put at least 8 bytes per fragment.
157 */
158 if (ip->ip_off & IP_DF)
159 {
160 error = -1;
161 ipstat.ips_cantfrag++;
162 goto bad;
163 }
164
165 len = (if_mtu - hlen) &~ 7; /* ip databytes per packet */
166 if (len < 8)
167 {
168 error = -1;
169 goto bad;
170 }
171
172 {
173 int mhlen, firstlen = len;
174 struct mbuf **mnext = &m->m_nextpkt;
175
176 /*
177 * Loop through length of segment after first fragment,
178 * make new header and copy data of each part and link onto chain.
179 */
180 m0 = m;
181 mhlen = sizeof (struct ip);
182 for (off = hlen + len; off < (u_int16_t)ip->ip_len; off += len)
183 {
184 register struct ip *mhip;
185 m = m_get(pData);
186 if (m == 0)
187 {
188 error = -1;
189 ipstat.ips_odropped++;
190 goto sendorfree;
191 }
192 m_adj(m, if_maxlinkhdr);
193 mhip = mtod(m, struct ip *);
194 *mhip = *ip;
195#ifdef VBOX_WITHOUT_SLIRP_CLIENT_ETHER
196 /* we've calculated eth_dst for first packet */
197 eh = (struct ethhdr *)MBUF_HEAD(m);
198 if (eth_dst != NULL) {
199 memcpy(eh->h_source, eth_dst, ETH_ALEN);
200 }
201#endif
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
244sendorfree:
245 for (m = m0; m; m = m0)
246 {
247 m0 = m->m_nextpkt;
248 m->m_nextpkt = 0;
249 if (error == 0)
250 if_output(pData, so, m);
251 else
252 m_freem(pData, m);
253 }
254
255 if (error == 0)
256 ipstat.ips_fragmented++;
257 }
258
259done:
260 return (error);
261
262bad:
263 m_freem(pData, m0);
264 goto done;
265}
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