VirtualBox

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

Last change on this file since 39347 was 39259, checked in by vboxsync, 13 years ago

NAT: firewall emulation hack allowing NETBIOS resolving mechanism live in peace with Sun RPC.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.0 KB
Line 
1/* $Id: ip_output.c 39259 2011-11-10 08:29:39Z vboxsync $ */
2/** @file
3 * NAT - IP output.
4 */
5
6/*
7 * Copyright (C) 2006-2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/*
19 * This code is based on:
20 *
21 * Copyright (c) 1982, 1986, 1988, 1990, 1993
22 * The Regents of the University of California. All rights reserved.
23 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 * must display the following acknowledgement:
34 * This product includes software developed by the University of
35 * California, Berkeley and its contributors.
36 * 4. Neither the name of the University nor the names of its contributors
37 * may be used to endorse or promote products derived from this software
38 * without specific prior written permission.
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
53 * ip_output.c,v 1.9 1994/11/16 10:17:10 jkh Exp
54 */
55
56/*
57 * Changes and additions relating to SLiRP are
58 * Copyright (c) 1995 Danny Gasparovski.
59 *
60 * Please read the file COPYRIGHT for the
61 * terms and conditions of the copyright.
62 */
63
64#include <slirp.h>
65#include "alias.h"
66
67static const uint8_t broadcast_ethaddr[6] =
68{
69 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
70};
71
72static int rt_lookup_in_cache(PNATState pData, uint32_t dst, uint8_t *ether)
73{
74 int rc;
75 LogFlowFunc(("ENTER: dst:%RTnaipv4, ether:%p\n", dst, ether));
76 if (dst == INADDR_BROADCAST)
77 {
78 memcpy(ether, broadcast_ethaddr, ETH_ALEN);
79 LogFlowFunc(("LEAVE: VINF_SUCCESS\n"));
80 return VINF_SUCCESS;
81 }
82
83 rc = slirp_arp_lookup_ether_by_ip(pData, dst, ether);
84 if (RT_SUCCESS(rc))
85 {
86 LogFlowFunc(("LEAVE: %Rrc\n", rc));
87 return rc;
88 }
89
90 rc = bootp_cache_lookup_ether_by_ip(pData, dst, ether);
91 if (RT_SUCCESS(rc))
92 {
93 LogFlowFunc(("LEAVE: %Rrc\n", rc));
94 return rc;
95 }
96 /*
97 * no chance to send this packet, sorry, we will request ether address via ARP
98 */
99 slirp_arp_who_has(pData, dst);
100 LogFlowFunc(("LEAVE: VERR_NOT_FOUND\n"));
101 return VERR_NOT_FOUND;
102}
103
104/*
105 * IP output. The packet in mbuf chain m contains a skeletal IP
106 * header (with len, off, ttl, proto, tos, src, dst).
107 * The mbuf chain containing the packet will be freed.
108 * The mbuf opt, if present, will not be freed.
109 */
110int
111ip_output(PNATState pData, struct socket *so, struct mbuf *m0)
112{
113 return ip_output0(pData, so, m0, 0);
114}
115
116/* This function will free m0! */
117int
118ip_output0(PNATState pData, struct socket *so, struct mbuf *m0, int urg)
119{
120 register struct ip *ip;
121 register struct mbuf *m = m0;
122 register int hlen = sizeof(struct ip);
123 int len, off, error = 0;
124 struct ethhdr *eh = NULL;
125 uint8_t eth_dst[ETH_ALEN];
126 int rc = 1;
127
128 STAM_PROFILE_START(&pData->StatIP_output, a);
129
130 LogFlowFunc(("ip_output: so = %R[natsock], m0 = %lx\n", so, (long)m0));
131
132 M_ASSERTPKTHDR(m);
133 Assert(m->m_pkthdr.header);
134
135#if 0 /* We do no options */
136 if (opt)
137 {
138 m = ip_insertoptions(m, opt, &len);
139 hlen = len;
140 }
141#endif
142 ip = mtod(m, struct ip *);
143 LogFunc(("ip(src:%RTnaipv4, dst:%RTnaipv4)\n", ip->ip_src, ip->ip_dst));
144 /*
145 * Fill in IP header.
146 */
147 ip->ip_v = IPVERSION;
148 ip->ip_off &= IP_DF;
149 ip->ip_id = RT_H2N_U16(ip_currid++);
150 ip->ip_hl = hlen >> 2;
151 ipstat.ips_localout++;
152
153 /* Current TCP/IP stack hasn't routing information at
154 * all so we need to calculate destination ethernet address
155 */
156 rc = rt_lookup_in_cache(pData, ip->ip_dst.s_addr, eth_dst);
157 if (RT_FAILURE(rc))
158 goto exit_drop_package;
159
160 eh = (struct ethhdr *)(m->m_data - ETH_HLEN);
161 /*
162 * If small enough for interface, can just send directly.
163 */
164 if ((u_int16_t)ip->ip_len <= if_mtu)
165 {
166 ip->ip_len = RT_H2N_U16((u_int16_t)ip->ip_len);
167 ip->ip_off = RT_H2N_U16((u_int16_t)ip->ip_off);
168 ip->ip_sum = 0;
169 ip->ip_sum = cksum(m, hlen);
170
171 if (!(m->m_flags & M_SKIP_FIREWALL)){
172 struct m_tag *t;
173 STAM_PROFILE_START(&pData->StatALIAS_output, b);
174 if ((t = m_tag_find(m, PACKET_TAG_ALIAS, NULL)) != 0)
175 rc = LibAliasOut((struct libalias *)&t[1], mtod(m, char *),
176 m_length(m, NULL));
177 else
178 rc = LibAliasOut(pData->proxy_alias, mtod(m, char *),
179 m_length(m, NULL));
180
181 if (rc == PKT_ALIAS_IGNORED)
182 {
183 Log(("NAT: packet was droppped\n"));
184 goto exit_drop_package;
185 }
186 STAM_PROFILE_STOP(&pData->StatALIAS_output, b);
187 }
188 else
189 m->m_flags &= ~M_SKIP_FIREWALL;
190
191 memcpy(eh->h_source, eth_dst, ETH_ALEN);
192
193 LogFlowFunc(("ip(ip_src:%RTnaipv4, ip_dst:%RTnaipv4)\n",
194 ip->ip_src, ip->ip_dst));
195 if_encap(pData, ETH_P_IP, m, urg? ETH_ENCAP_URG : 0);
196 goto done;
197 }
198
199 /*
200 * Too large for interface; fragment if possible.
201 * Must be able to put at least 8 bytes per fragment.
202 */
203 if (ip->ip_off & IP_DF)
204 {
205 error = -1;
206 ipstat.ips_cantfrag++;
207 goto exit_drop_package;
208 }
209
210 len = (if_mtu - hlen) &~ 7; /* ip databytes per packet */
211 if (len < 8)
212 {
213 error = -1;
214 goto exit_drop_package;
215 }
216
217 {
218 int mhlen, firstlen = len;
219 struct mbuf **mnext = &m->m_nextpkt;
220 char *buf; /* intermediate buffer we'll use for a copy of the original packet */
221 /*
222 * Loop through length of segment after first fragment,
223 * make new header and copy data of each part and link onto chain.
224 */
225 m0 = m;
226 mhlen = ip->ip_hl << 2;
227 Log(("NAT:ip:frag: mhlen = %d\n", mhlen));
228 for (off = hlen + len; off < (u_int16_t)ip->ip_len; off += len)
229 {
230 register struct ip *mhip;
231 m = m_getjcl(pData, M_NOWAIT, MT_HEADER , M_PKTHDR, slirp_size(pData));
232 if (m == 0)
233 {
234 error = -1;
235 ipstat.ips_odropped++;
236 goto send_or_free;
237 }
238 m->m_data += if_maxlinkhdr;
239 mhip = mtod(m, struct ip *);
240 *mhip = *ip;
241 m->m_pkthdr.header = mtod(m, void *);
242 /* we've calculated eth_dst for first packet */
243#if 0 /* No options */
244 if (hlen > sizeof (struct ip))
245 {
246 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
247 mhip->ip_hl = mhlen >> 2;
248 }
249#endif
250 m->m_len = mhlen;
251 mhip->ip_off = ((off - mhlen) >> 3) + (ip->ip_off & ~IP_MF);
252 if (ip->ip_off & IP_MF)
253 mhip->ip_off |= IP_MF;
254 if (off + len >= (u_int16_t)ip->ip_len)
255 len = (u_int16_t)ip->ip_len - off;
256 else
257 mhip->ip_off |= IP_MF;
258 mhip->ip_len = RT_H2N_U16((u_int16_t)(len + mhlen));
259
260 buf = RTMemAlloc(len);
261 Log(("NAT:ip:frag: alloc = %d\n", len));
262 m_copydata(m0, off, len, buf); /* copy to buffer */
263 Log(("NAT:ip:frag: m_copydata(m0 = %p,off = %d, len = %d,)\n", m0, off, len));
264
265 m->m_data += mhlen;
266 m->m_len -= mhlen;
267 m_copyback(pData, m, 0, len, buf); /* copy from buffer */
268 Log(("NAT:ip:frag: m_copyback(m = %p,, len = %d,)\n", m, len));
269 m->m_data -= mhlen;
270 m->m_len += mhlen;
271 RTMemFree(buf);
272 Assert((m->m_len == (mhlen + len)));
273
274 mhip->ip_off = RT_H2N_U16((u_int16_t)(mhip->ip_off));
275 mhip->ip_sum = 0;
276 mhip->ip_sum = cksum(m, mhlen);
277 *mnext = m;
278 mnext = &m->m_nextpkt;
279 ipstat.ips_ofragments++;
280 }
281 /*
282 * Update first fragment by trimming what's been copied out
283 * and updating header, then send each fragment (in order).
284 *
285 * note: m_adj do all required releases for chained mbufs.
286 */
287 m = m0;
288 m_adj(m, mhlen + firstlen - (u_int16_t)ip->ip_len);
289 Log(("NAT:ip:frag: m_adj(m(m_len:%d) = %p, len = %d)\n", m->m_len, m, mhlen + firstlen - (u_int16_t)ip->ip_len));
290 ip->ip_len = RT_H2N_U16((u_int16_t)mhlen + firstlen);
291 ip->ip_off = RT_H2N_U16((u_int16_t)(ip->ip_off | IP_MF));
292 ip->ip_sum = 0;
293 ip->ip_sum = cksum(m, mhlen);
294
295send_or_free:
296 if (!(m->m_flags & M_SKIP_FIREWALL)){
297 /* @todo: We can't alias all fragments because the way libalias processing
298 * the fragments brake the sequence. libalias put alias_address to the source
299 * address of IP header of fragment, while IP header of the first packet is
300 * is unmodified. That confuses guest's TCP/IP stack and guest drop the sequence.
301 * Here we're letting libalias to process the first packet and send the rest as is,
302 * it's exactly the way in of packet are processing in proxyonly way.
303 * Here we need investigate what should be done to avoid such behavior and find right
304 * solution.
305 */
306 struct m_tag *t;
307 int rcLa;
308 if ((t = m_tag_find(m, PACKET_TAG_ALIAS, NULL)) != 0)
309 rcLa = LibAliasOut((struct libalias *)&t[1], mtod(m, char *), m->m_len);
310 else
311 rcLa = LibAliasOut(pData->proxy_alias, mtod(m, char *), m->m_len);
312
313 if (rcLa == PKT_ALIAS_IGNORED)
314 {
315 Log(("NAT: packet was droppped\n"));
316 goto exit_drop_package;
317 }
318 Log2(("NAT: LibAlias return %d\n", rcLa));
319 }
320 else
321 m->m_flags &= ~M_SKIP_FIREWALL;
322 for (m = m0; m; m = m0)
323 {
324 m0 = m->m_nextpkt;
325 m->m_nextpkt = 0;
326 if (error == 0)
327 {
328 m->m_data -= ETH_HLEN;
329 eh = mtod(m, struct ethhdr *);
330 m->m_data += ETH_HLEN;
331 memcpy(eh->h_source, eth_dst, ETH_ALEN);
332
333 Log(("NAT:ip:frag: if_encap(,,m(m_len = %d) = %p,0)\n", m->m_len, m));
334 if_encap(pData, ETH_P_IP, m, 0);
335 }
336 else
337 m_freem(pData, m);
338 }
339
340 if (error == 0)
341 ipstat.ips_fragmented++;
342 }
343
344done:
345 STAM_PROFILE_STOP(&pData->StatIP_output, a);
346 LogFlowFunc(("LEAVE: %d\n", error));
347 return error;
348
349exit_drop_package:
350 m_freem(pData, m0);
351 STAM_PROFILE_STOP(&pData->StatIP_output, a);
352 LogFlowFunc(("LEAVE: %d\n", error));
353 return error;
354}
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