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