VirtualBox

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

Last change on this file since 86327 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.6 KB
Line 
1/* $Id: ip_output.c 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * NAT - IP output.
4 */
5
6/*
7 * Copyright (C) 2006-2020 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 <iprt/errcore.h>
66#include "alias.h"
67
68static const uint8_t broadcast_ethaddr[6] =
69{
70 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
71};
72
73static int rt_lookup_in_cache(PNATState pData, uint32_t dst, uint8_t *ether)
74{
75 int rc;
76 LogFlowFunc(("ENTER: dst:%RTnaipv4, ether:%RTmac\n", dst, ether));
77 if (dst == INADDR_BROADCAST)
78 {
79 memcpy(ether, broadcast_ethaddr, ETH_ALEN);
80 LogFlowFunc(("LEAVE: VINF_SUCCESS\n"));
81 return VINF_SUCCESS;
82 }
83
84 rc = slirp_arp_lookup_ether_by_ip(pData, dst, ether);
85 if (RT_SUCCESS(rc))
86 {
87 LogFlowFunc(("LEAVE: %Rrc\n", rc));
88 return rc;
89 }
90
91 rc = bootp_cache_lookup_ether_by_ip(pData, dst, ether);
92 if (RT_SUCCESS(rc))
93 {
94 LogFlowFunc(("LEAVE: %Rrc\n", rc));
95 return rc;
96 }
97 /*
98 * no chance to send this packet, sorry, we will request ether address via ARP
99 */
100 slirp_arp_who_has(pData, dst);
101 LogFlowFunc(("LEAVE: VERR_NOT_FOUND\n"));
102 return VERR_NOT_FOUND;
103}
104
105/*
106 * IP output. The packet in mbuf chain m contains a skeletal IP
107 * header (with len, off, ttl, proto, tos, src, dst).
108 * The mbuf chain containing the packet will be freed.
109 * The mbuf opt, if present, will not be freed.
110 */
111int
112ip_output(PNATState pData, struct socket *so, struct mbuf *m0)
113{
114 return ip_output0(pData, so, m0, 0);
115}
116
117/* This function will free m0! */
118int
119ip_output0(PNATState pData, struct socket *so, struct mbuf *m0, int urg)
120{
121 register struct ip *ip;
122 register struct mbuf *m = m0;
123 register int hlen = sizeof(struct ip);
124 int len, off, error = 0;
125 struct ethhdr *eh = NULL;
126 uint8_t eth_dst[ETH_ALEN];
127 int rc = 1;
128
129 STAM_PROFILE_START(&pData->StatIP_output, a);
130
131#ifdef LOG_ENABLED
132 LogFlowFunc(("ip_output: so = %R[natsock], m0 = %p\n", so, m0));
133#else
134 NOREF(so);
135#endif
136
137 M_ASSERTPKTHDR(m);
138 Assert(m->m_pkthdr.header);
139
140#if 0 /* We do no options */
141 if (opt)
142 {
143 m = ip_insertoptions(m, opt, &len);
144 hlen = len;
145 }
146#endif
147 ip = mtod(m, struct ip *);
148 LogFunc(("ip(src:%RTnaipv4, dst:%RTnaipv4)\n", ip->ip_src, ip->ip_dst));
149 /*
150 * Fill in IP header.
151 */
152 ip->ip_v = IPVERSION;
153 ip->ip_off &= IP_DF;
154 ip->ip_id = RT_H2N_U16(ip_currid);
155 ip->ip_hl = hlen >> 2;
156 ip_currid++;
157 ipstat.ips_localout++;
158
159 /* Current TCP/IP stack hasn't routing information at
160 * all so we need to calculate destination ethernet address
161 */
162 rc = rt_lookup_in_cache(pData, ip->ip_dst.s_addr, eth_dst);
163 if (RT_FAILURE(rc))
164 goto exit_drop_package;
165
166 eh = (struct ethhdr *)(m->m_data - ETH_HLEN);
167 /*
168 * If small enough for interface, can just send directly.
169 */
170 if ((u_int16_t)ip->ip_len <= if_mtu)
171 {
172 ip->ip_len = RT_H2N_U16((u_int16_t)ip->ip_len);
173 ip->ip_off = RT_H2N_U16((u_int16_t)ip->ip_off);
174 ip->ip_sum = 0;
175 ip->ip_sum = cksum(m, hlen);
176
177 if (!(m->m_flags & M_SKIP_FIREWALL)){
178 STAM_PROFILE_START(&pData->StatALIAS_output, b);
179 rc = LibAliasOut(pData->proxy_alias, mtod(m, char *), m_length(m, NULL));
180 if (rc == PKT_ALIAS_IGNORED)
181 {
182 Log(("NAT: packet was droppped\n"));
183 goto exit_drop_package;
184 }
185 STAM_PROFILE_STOP(&pData->StatALIAS_output, b);
186 }
187 else
188 m->m_flags &= ~M_SKIP_FIREWALL;
189
190 memcpy(eh->h_source, eth_dst, ETH_ALEN);
191
192 LogFlowFunc(("ip(ip_src:%RTnaipv4, ip_dst:%RTnaipv4)\n",
193 ip->ip_src, ip->ip_dst));
194 if_encap(pData, ETH_P_IP, m, urg? ETH_ENCAP_URG : 0);
195 goto done;
196 }
197
198 /*
199 * Too large for interface; fragment if possible.
200 * Must be able to put at least 8 bytes per fragment.
201 */
202 if (ip->ip_off & IP_DF)
203 {
204 error = -1;
205 ipstat.ips_cantfrag++;
206 goto exit_drop_package;
207 }
208
209 len = (if_mtu - hlen) &~ 7; /* ip databytes per packet */
210 if (len < 8)
211 {
212 error = -1;
213 goto exit_drop_package;
214 }
215
216 {
217 int mhlen, firstlen = len;
218 struct mbuf **mnext = &m->m_nextpkt;
219 char *buf; /* intermediate buffer we'll use for a copy of the original packet */
220 /*
221 * Loop through length of segment after first fragment,
222 * make new header and copy data of each part and link onto chain.
223 */
224 m0 = m;
225 mhlen = ip->ip_hl << 2;
226 Log(("NAT:ip:frag: mhlen = %d\n", mhlen));
227 for (off = hlen + len; off < (u_int16_t)ip->ip_len; off += len)
228 {
229 register struct ip *mhip;
230 m = m_getjcl(pData, M_NOWAIT, MT_HEADER , M_PKTHDR, slirp_size(pData));
231 if (m == 0)
232 {
233 error = -1;
234 ipstat.ips_odropped++;
235 goto exit_drop_package;
236 }
237 m->m_data += if_maxlinkhdr;
238 mhip = mtod(m, struct ip *);
239 *mhip = *ip;
240 m->m_pkthdr.header = mtod(m, void *);
241 /* we've calculated eth_dst for first packet */
242#if 0 /* No options */
243 if (hlen > sizeof (struct ip))
244 {
245 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
246 mhip->ip_hl = mhlen >> 2;
247 }
248#endif
249 m->m_len = mhlen;
250 mhip->ip_off = ((off - mhlen) >> 3) + (ip->ip_off & ~IP_MF);
251 if (ip->ip_off & IP_MF)
252 mhip->ip_off |= IP_MF;
253 if (off + len >= (u_int16_t)ip->ip_len)
254 len = (u_int16_t)ip->ip_len - off;
255 else
256 mhip->ip_off |= IP_MF;
257 mhip->ip_len = RT_H2N_U16((u_int16_t)(len + mhlen));
258
259 buf = RTMemAlloc(len);
260 Log(("NAT:ip:frag: alloc = %d\n", len));
261 m_copydata(m0, off, len, buf); /* copy to buffer */
262 Log(("NAT:ip:frag: m_copydata(m0 = %p,off = %d, len = %d,)\n", m0, off, len));
263
264 m->m_data += mhlen;
265 m->m_len -= mhlen;
266 m_copyback(pData, m, 0, len, buf); /* copy from buffer */
267 Log(("NAT:ip:frag: m_copyback(m = %p,, len = %d,)\n", m, len));
268 m->m_data -= mhlen;
269 m->m_len += mhlen;
270 RTMemFree(buf);
271 Assert((m->m_len == (mhlen + len)));
272
273 mhip->ip_off = RT_H2N_U16((u_int16_t)(mhip->ip_off));
274 mhip->ip_sum = 0;
275 mhip->ip_sum = cksum(m, mhlen);
276 *mnext = m;
277 mnext = &m->m_nextpkt;
278 ipstat.ips_ofragments++;
279 }
280 /*
281 * Update first fragment by trimming what's been copied out
282 * and updating header, then send each fragment (in order).
283 *
284 * note: m_adj do all required releases for chained mbufs.
285 */
286 m = m0;
287 m_adj(m, mhlen + firstlen - (u_int16_t)ip->ip_len);
288 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));
289 ip->ip_len = RT_H2N_U16((u_int16_t)mhlen + firstlen);
290 ip->ip_off = RT_H2N_U16((u_int16_t)(ip->ip_off | IP_MF));
291 ip->ip_sum = 0;
292 ip->ip_sum = cksum(m, mhlen);
293
294 if (!(m->m_flags & M_SKIP_FIREWALL)){
295 /** @todo We can't alias all fragments because the way libalias processing
296 * the fragments brake the sequence. libalias put alias_address to the source
297 * address of IP header of fragment, while IP header of the first packet is
298 * is unmodified. That confuses guest's TCP/IP stack and guest drop the sequence.
299 * Here we're letting libalias to process the first packet and send the rest as is,
300 * it's exactly the way in of packet are processing in proxyonly way.
301 * Here we need investigate what should be done to avoid such behavior and find right
302 * solution.
303 */
304 int rcLa;
305
306 rcLa = LibAliasOut(pData->proxy_alias, mtod(m, char *), m->m_len);
307 if (rcLa == PKT_ALIAS_IGNORED)
308 {
309 Log(("NAT: packet was droppped\n"));
310 goto exit_drop_package;
311 }
312 Log2(("NAT: LibAlias return %d\n", rcLa));
313 }
314 else
315 m->m_flags &= ~M_SKIP_FIREWALL;
316 for (m = m0; m; m = m0)
317 {
318 m0 = m->m_nextpkt;
319 m->m_nextpkt = 0;
320 if (error == 0)
321 {
322 m->m_data -= ETH_HLEN;
323 eh = mtod(m, struct ethhdr *);
324 m->m_data += ETH_HLEN;
325 memcpy(eh->h_source, eth_dst, ETH_ALEN);
326
327 Log(("NAT:ip:frag: if_encap(,,m(m_len = %d) = %p,0)\n", m->m_len, m));
328 if_encap(pData, ETH_P_IP, m, 0);
329 }
330 else
331 m_freem(pData, m);
332 }
333
334 if (error == 0)
335 ipstat.ips_fragmented++;
336 }
337
338done:
339 STAM_PROFILE_STOP(&pData->StatIP_output, a);
340 LogFlowFunc(("LEAVE: %d\n", error));
341 return error;
342
343exit_drop_package:
344 m_freem(pData, m0);
345 STAM_PROFILE_STOP(&pData->StatIP_output, a);
346 LogFlowFunc(("LEAVE: %d\n", error));
347 return error;
348}
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