1 | /*
|
---|
2 | * Copyright (c) 1995 Danny Gasparovski.
|
---|
3 | *
|
---|
4 | * Please read the file COPYRIGHT for the
|
---|
5 | * terms and conditions of the copyright.
|
---|
6 | */
|
---|
7 |
|
---|
8 | #define WANT_SYS_IOCTL_H
|
---|
9 | #include <slirp.h>
|
---|
10 |
|
---|
11 | #ifndef HAVE_INET_ATON
|
---|
12 | int
|
---|
13 | inet_aton(const char *cp, struct in_addr *ia)
|
---|
14 | {
|
---|
15 | u_int32_t addr = inet_addr(cp);
|
---|
16 | if (addr == 0xffffffff)
|
---|
17 | return 0;
|
---|
18 | ia->s_addr = addr;
|
---|
19 | return 1;
|
---|
20 | }
|
---|
21 | #endif
|
---|
22 |
|
---|
23 | /*
|
---|
24 | * Get our IP address and put it in our_addr
|
---|
25 | */
|
---|
26 | void
|
---|
27 | getouraddr(PNATState pData)
|
---|
28 | {
|
---|
29 | our_addr.s_addr = loopback_addr.s_addr;
|
---|
30 | }
|
---|
31 |
|
---|
32 | struct quehead
|
---|
33 | {
|
---|
34 | struct quehead *qh_link;
|
---|
35 | struct quehead *qh_rlink;
|
---|
36 | };
|
---|
37 |
|
---|
38 | void
|
---|
39 | insque(PNATState pData, void *a, void *b)
|
---|
40 | {
|
---|
41 | register struct quehead *element = (struct quehead *) a;
|
---|
42 | register struct quehead *head = (struct quehead *) b;
|
---|
43 | element->qh_link = head->qh_link;
|
---|
44 | head->qh_link = (struct quehead *)element;
|
---|
45 | element->qh_rlink = (struct quehead *)head;
|
---|
46 | ((struct quehead *)(element->qh_link))->qh_rlink = (struct quehead *)element;
|
---|
47 | }
|
---|
48 |
|
---|
49 | void
|
---|
50 | remque(PNATState pData, void *a)
|
---|
51 | {
|
---|
52 | register struct quehead *element = (struct quehead *) a;
|
---|
53 | ((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
|
---|
54 | ((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
|
---|
55 | element->qh_rlink = NULL;
|
---|
56 | /* element->qh_link = NULL; TCP FIN1 crashes if you do this. Why ? */
|
---|
57 | }
|
---|
58 |
|
---|
59 | int
|
---|
60 | add_exec(struct ex_list **ex_ptr, int do_pty, char *exec, int addr, int port)
|
---|
61 | {
|
---|
62 | struct ex_list *tmp_ptr;
|
---|
63 |
|
---|
64 | /* First, check if the port is "bound" */
|
---|
65 | for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next)
|
---|
66 | {
|
---|
67 | if (port == tmp_ptr->ex_fport && addr == tmp_ptr->ex_addr)
|
---|
68 | return -1;
|
---|
69 | }
|
---|
70 |
|
---|
71 | tmp_ptr = *ex_ptr;
|
---|
72 | *ex_ptr = (struct ex_list *)RTMemAlloc(sizeof(struct ex_list));
|
---|
73 | (*ex_ptr)->ex_fport = port;
|
---|
74 | (*ex_ptr)->ex_addr = addr;
|
---|
75 | (*ex_ptr)->ex_pty = do_pty;
|
---|
76 | (*ex_ptr)->ex_exec = RTStrDup(exec);
|
---|
77 | (*ex_ptr)->ex_next = tmp_ptr;
|
---|
78 | return 0;
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | /*
|
---|
83 | * Set fd blocking and non-blocking
|
---|
84 | */
|
---|
85 | void
|
---|
86 | fd_nonblock(int fd)
|
---|
87 | {
|
---|
88 | #ifdef FIONBIO
|
---|
89 | int opt = 1;
|
---|
90 |
|
---|
91 | ioctlsocket(fd, FIONBIO, &opt);
|
---|
92 | #else
|
---|
93 | int opt;
|
---|
94 |
|
---|
95 | opt = fcntl(fd, F_GETFL, 0);
|
---|
96 | opt |= O_NONBLOCK;
|
---|
97 | fcntl(fd, F_SETFL, opt);
|
---|
98 | #endif
|
---|
99 | }
|
---|
100 |
|
---|
101 | void
|
---|
102 | fd_block(int fd)
|
---|
103 | {
|
---|
104 | #ifdef FIONBIO
|
---|
105 | int opt = 0;
|
---|
106 |
|
---|
107 | ioctlsocket(fd, FIONBIO, &opt);
|
---|
108 | #else
|
---|
109 | int opt;
|
---|
110 |
|
---|
111 | opt = fcntl(fd, F_GETFL, 0);
|
---|
112 | opt &= ~O_NONBLOCK;
|
---|
113 | fcntl(fd, F_SETFL, opt);
|
---|
114 | #endif
|
---|
115 | }
|
---|
116 |
|
---|
117 | #ifdef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
118 | #define ITEM_MAGIC 0xdead0001
|
---|
119 | struct item
|
---|
120 | {
|
---|
121 | uint32_t magic;
|
---|
122 | uma_zone_t zone;
|
---|
123 | uint32_t ref_count;
|
---|
124 | LIST_ENTRY(item) list;
|
---|
125 | };
|
---|
126 |
|
---|
127 | #define ZONE_MAGIC 0xdead0002
|
---|
128 | struct uma_zone
|
---|
129 | {
|
---|
130 | uint32_t magic;
|
---|
131 | PNATState pData; /* to minimize changes in the rest of UMA emulation code */
|
---|
132 | RTCRITSECT csZone;
|
---|
133 | const char *name;
|
---|
134 | size_t size; /* item size */
|
---|
135 | ctor_t pfCtor;
|
---|
136 | dtor_t pfDtor;
|
---|
137 | zinit_t pfInit;
|
---|
138 | zfini_t pfFini;
|
---|
139 | uma_alloc_t pfAlloc;
|
---|
140 | uma_free_t pfFree;
|
---|
141 | int max_items;
|
---|
142 | int cur_items;
|
---|
143 | LIST_HEAD(RT_NOTHING, item) used_items;
|
---|
144 | LIST_HEAD(RT_NOTHING, item) free_items;
|
---|
145 | uma_zone_t master_zone;
|
---|
146 | };
|
---|
147 |
|
---|
148 |
|
---|
149 | static void *slirp_uma_alloc(uma_zone_t zone,
|
---|
150 | int size, uint8_t *pflags, int wait)
|
---|
151 | {
|
---|
152 | struct item *it;
|
---|
153 | RTCritSectEnter(&zone->csZone);
|
---|
154 | if (!LIST_EMPTY(&zone->free_items))
|
---|
155 | {
|
---|
156 | /*
|
---|
157 | * @todo (r=vvl) here should be some
|
---|
158 | * accounting of extra items in case
|
---|
159 | * breakthrough barrier
|
---|
160 | */
|
---|
161 | if (LIST_EMPTY(&zone->free_items))
|
---|
162 | {
|
---|
163 | RTCritSectLeave(&zone->csZone);
|
---|
164 | return NULL;
|
---|
165 | }
|
---|
166 | it = LIST_FIRST(&zone->free_items);
|
---|
167 | LIST_REMOVE(it, list);
|
---|
168 | LIST_INSERT_HEAD(&zone->used_items, it, list);
|
---|
169 | goto allocated;
|
---|
170 | }
|
---|
171 |
|
---|
172 | /*@todo 'Z' should be depend on flag */
|
---|
173 | it = RTMemAllocZ(sizeof(struct item) + zone->size);
|
---|
174 | if (it == NULL)
|
---|
175 | {
|
---|
176 | Log(("NAT: uma no memory"));
|
---|
177 | RTCritSectLeave(&zone->csZone);
|
---|
178 | return NULL;
|
---|
179 | }
|
---|
180 | it->magic = ITEM_MAGIC;
|
---|
181 | LIST_INSERT_HEAD(&zone->used_items, it, list);
|
---|
182 | zone->cur_items++;
|
---|
183 | it->zone = zone;
|
---|
184 | if (zone->cur_items >= zone->max_items)
|
---|
185 | LogRel(("NAT: zone(%s) has reached it maximum\n", zone->name));
|
---|
186 |
|
---|
187 | allocated:
|
---|
188 | if (zone->pfInit)
|
---|
189 | zone->pfInit(zone->pData, (void *)&it[1], zone->size, M_DONTWAIT);
|
---|
190 | RTCritSectLeave(&zone->csZone);
|
---|
191 | return (void *)&it[1];
|
---|
192 | }
|
---|
193 |
|
---|
194 | static void slirp_uma_free(void *item, int size, uint8_t flags)
|
---|
195 | {
|
---|
196 | struct item *it;
|
---|
197 | uma_zone_t zone;
|
---|
198 | Assert(item);
|
---|
199 | it = &((struct item *)item)[-1];
|
---|
200 | Assert(it->magic == ITEM_MAGIC);
|
---|
201 | zone = it->zone;
|
---|
202 | RTCritSectEnter(&zone->csZone);
|
---|
203 | Assert(zone->magic == ZONE_MAGIC);
|
---|
204 | LIST_REMOVE(it, list);
|
---|
205 | LIST_INSERT_HEAD(&zone->free_items, it, list);
|
---|
206 | zone->cur_items--;
|
---|
207 | RTCritSectLeave(&zone->csZone);
|
---|
208 | }
|
---|
209 |
|
---|
210 | uma_zone_t uma_zcreate(PNATState pData, char *name, size_t size,
|
---|
211 | ctor_t ctor, dtor_t dtor, zinit_t init, zfini_t fini, int flags1, int flags2)
|
---|
212 | {
|
---|
213 | uma_zone_t zone = RTMemAllocZ(sizeof(struct uma_zone) + size);
|
---|
214 | Assert((pData));
|
---|
215 | zone->magic = ZONE_MAGIC;
|
---|
216 | zone->pData = pData;
|
---|
217 | zone->name = name;
|
---|
218 | zone->size = size;
|
---|
219 | zone->pfCtor = ctor;
|
---|
220 | zone->pfDtor = dtor;
|
---|
221 | zone->pfInit = init;
|
---|
222 | zone->pfFini = fini;
|
---|
223 | zone->pfAlloc = slirp_uma_alloc;
|
---|
224 | zone->pfFree = slirp_uma_free;
|
---|
225 | RTCritSectInit(&zone->csZone);
|
---|
226 | return zone;
|
---|
227 |
|
---|
228 | }
|
---|
229 | uma_zone_t uma_zsecond_create(char *name, ctor_t ctor,
|
---|
230 | dtor_t dtor, zinit_t init, zfini_t fini, uma_zone_t master)
|
---|
231 | {
|
---|
232 | uma_zone_t zone;
|
---|
233 | #if 0
|
---|
234 | if (master->pfAlloc != NULL)
|
---|
235 | zone = (uma_zone_t)master->pfAlloc(master, sizeof(struct uma_zone), NULL, 0);
|
---|
236 | #endif
|
---|
237 | zone = RTMemAllocZ(sizeof(struct uma_zone));
|
---|
238 | if (zone == NULL)
|
---|
239 | return NULL;
|
---|
240 |
|
---|
241 | Assert((master && master->pData));
|
---|
242 | zone->magic = ZONE_MAGIC;
|
---|
243 | zone->pData = master->pData;
|
---|
244 | zone->name = name;
|
---|
245 | zone->pfCtor = ctor;
|
---|
246 | zone->pfDtor = dtor;
|
---|
247 | zone->pfInit = init;
|
---|
248 | zone->pfFini = fini;
|
---|
249 | zone->pfAlloc = slirp_uma_alloc;
|
---|
250 | zone->pfFree = slirp_uma_free;
|
---|
251 | zone->size = master->size;
|
---|
252 | RTCritSectInit(&zone->csZone);
|
---|
253 | return zone;
|
---|
254 | }
|
---|
255 |
|
---|
256 | void uma_zone_set_max(uma_zone_t zone, int max)
|
---|
257 | {
|
---|
258 | zone->max_items = max;
|
---|
259 | }
|
---|
260 |
|
---|
261 | void uma_zone_set_allocf(uma_zone_t zone, uma_alloc_t pfAlloc)
|
---|
262 | {
|
---|
263 | zone->pfAlloc = pfAlloc;
|
---|
264 | }
|
---|
265 |
|
---|
266 | void uma_zone_set_freef(uma_zone_t zone, uma_free_t pfFree)
|
---|
267 | {
|
---|
268 | zone->pfFree = pfFree;
|
---|
269 | }
|
---|
270 |
|
---|
271 | uint32_t *uma_find_refcnt(uma_zone_t zone, void *mem)
|
---|
272 | {
|
---|
273 | /*@todo (vvl) this function supposed to work with special zone storing
|
---|
274 | reference counters */
|
---|
275 | struct item *it = (struct item *)mem; /* 1st element */
|
---|
276 | Assert(mem != NULL);
|
---|
277 | Assert(zone->magic == ZONE_MAGIC);
|
---|
278 | /* for returning pointer to counter we need get 0 elemnt */
|
---|
279 | Assert(it[-1].magic == ITEM_MAGIC);
|
---|
280 | return &it[-1].ref_count;
|
---|
281 | }
|
---|
282 |
|
---|
283 | void *uma_zalloc_arg(uma_zone_t zone, void *args, int how)
|
---|
284 | {
|
---|
285 | void *mem;
|
---|
286 | Assert(zone->magic == ZONE_MAGIC);
|
---|
287 | if (zone->pfAlloc == NULL)
|
---|
288 | return NULL;
|
---|
289 | RTCritSectEnter(&zone->csZone);
|
---|
290 | mem = zone->pfAlloc(zone, zone->size, NULL, 0);
|
---|
291 | if (zone->pfCtor)
|
---|
292 | zone->pfCtor(zone->pData, mem, zone->size, args, M_DONTWAIT);
|
---|
293 | RTCritSectLeave(&zone->csZone);
|
---|
294 | return mem;
|
---|
295 | }
|
---|
296 |
|
---|
297 | void uma_zfree(uma_zone_t zone, void *item)
|
---|
298 | {
|
---|
299 | uma_zfree_arg(zone, item, NULL);
|
---|
300 | }
|
---|
301 |
|
---|
302 | void uma_zfree_arg(uma_zone_t zone, void *mem, void *flags)
|
---|
303 | {
|
---|
304 | struct item *it;
|
---|
305 | Assert(zone->magic == ZONE_MAGIC);
|
---|
306 | if (zone->pfFree == NULL)
|
---|
307 | return;
|
---|
308 |
|
---|
309 | Assert((mem));
|
---|
310 | RTCritSectEnter(&zone->csZone);
|
---|
311 | it = &((struct item *)mem)[-1];
|
---|
312 | if (it->magic != ITEM_MAGIC)
|
---|
313 | {
|
---|
314 | Log(("NAT:UMA: %p seems to be allocated on heap ... freeing\n", mem));
|
---|
315 | RTMemFree(mem);
|
---|
316 | RTCritSectLeave(&zone->csZone);
|
---|
317 | return;
|
---|
318 | }
|
---|
319 | Assert((zone->magic == ZONE_MAGIC && zone == it->zone));
|
---|
320 |
|
---|
321 | if (zone->pfDtor)
|
---|
322 | zone->pfDtor(zone->pData, mem, zone->size, flags);
|
---|
323 | zone->pfFree(mem, 0, 0);
|
---|
324 | RTCritSectLeave(&zone->csZone);
|
---|
325 | }
|
---|
326 |
|
---|
327 | int uma_zone_exhausted_nolock(uma_zone_t zone)
|
---|
328 | {
|
---|
329 | return 0;
|
---|
330 | }
|
---|
331 |
|
---|
332 | void zone_drain(uma_zone_t zone)
|
---|
333 | {
|
---|
334 | }
|
---|
335 |
|
---|
336 | void slirp_null_arg_free(void *mem, void *arg)
|
---|
337 | {
|
---|
338 | /*@todo (r=vvl) make it wiser*/
|
---|
339 | Assert(mem);
|
---|
340 | RTMemFree(mem);
|
---|
341 | }
|
---|
342 |
|
---|
343 | void *uma_zalloc(uma_zone_t zone, int len)
|
---|
344 | {
|
---|
345 | return NULL;
|
---|
346 | }
|
---|
347 |
|
---|
348 | struct mbuf *slirp_ext_m_get(PNATState pData, size_t cbMin, void **ppvBuf, size_t *pcbBuf)
|
---|
349 | {
|
---|
350 | struct mbuf *m;
|
---|
351 | size_t size = MCLBYTES;
|
---|
352 | if (cbMin < MSIZE)
|
---|
353 | size = MCLBYTES;
|
---|
354 | else if (cbMin < MCLBYTES)
|
---|
355 | size = MCLBYTES;
|
---|
356 | else if (cbMin < MJUM9BYTES)
|
---|
357 | size = MJUM9BYTES;
|
---|
358 | else if (cbMin < MJUM16BYTES)
|
---|
359 | size = MJUM16BYTES;
|
---|
360 | else
|
---|
361 | AssertMsgFailed(("Unsupported size"));
|
---|
362 |
|
---|
363 | m = m_getjcl(pData, M_NOWAIT, MT_HEADER, M_PKTHDR, size);
|
---|
364 | m->m_len = size ;
|
---|
365 | *ppvBuf = mtod(m, void *);
|
---|
366 | *pcbBuf = size;
|
---|
367 | return m;
|
---|
368 | }
|
---|
369 |
|
---|
370 | void slirp_ext_m_free(PNATState pData, struct mbuf *m)
|
---|
371 | {
|
---|
372 | m_free(pData, m);
|
---|
373 | }
|
---|
374 |
|
---|
375 | static void zone_destroy(uma_zone_t zone)
|
---|
376 | {
|
---|
377 | struct item *it;
|
---|
378 | RTCritSectEnter(&zone->csZone);
|
---|
379 | LogRel(("NAT: zone(nm:%s, used:%d)\n", zone->name, zone->cur_items));
|
---|
380 | /* freeing */
|
---|
381 | while (!LIST_EMPTY(&zone->free_items))
|
---|
382 | {
|
---|
383 | it = LIST_FIRST(&zone->free_items);
|
---|
384 | LIST_REMOVE(it, list);
|
---|
385 | RTMemFree(it);
|
---|
386 | }
|
---|
387 | while (zone->master_zone != NULL && !LIST_EMPTY(&zone->used_items))
|
---|
388 | {
|
---|
389 | it = LIST_FIRST(&zone->used_items);
|
---|
390 | LIST_REMOVE(it, list);
|
---|
391 | RTMemFree(it);
|
---|
392 | }
|
---|
393 | RTCritSectLeave(&zone->csZone);
|
---|
394 | RTCritSectDelete(&zone->csZone);
|
---|
395 | RTMemFree(zone);
|
---|
396 | }
|
---|
397 |
|
---|
398 | void m_fini(PNATState pData)
|
---|
399 | {
|
---|
400 | zone_destroy(pData->zone_mbuf);
|
---|
401 | zone_destroy(pData->zone_clust);
|
---|
402 | zone_destroy(pData->zone_pack);
|
---|
403 | zone_destroy(pData->zone_jumbop);
|
---|
404 | zone_destroy(pData->zone_jumbo9);
|
---|
405 | zone_destroy(pData->zone_jumbo16);
|
---|
406 | /*@todo do finalize here.*/
|
---|
407 | }
|
---|
408 | #endif /* VBOX_WITH_SLIRP_BSD_MBUF */
|
---|