155714Skris/*	$OpenBSD: dispatch.c,v 1.45 2023/09/02 10:18:45 kn Exp $ */
255714Skris
355714Skris/*
455714Skris * Copyright (c) 1995, 1996, 1997, 1998, 1999
555714Skris * The Internet Software Consortium.   All rights reserved.
655714Skris *
755714Skris * Redistribution and use in source and binary forms, with or without
8280304Sjkim * modification, are permitted provided that the following conditions
955714Skris * are met:
1055714Skris *
1155714Skris * 1. Redistributions of source code must retain the above copyright
1255714Skris *    notice, this list of conditions and the following disclaimer.
1355714Skris * 2. Redistributions in binary form must reproduce the above copyright
1455714Skris *    notice, this list of conditions and the following disclaimer in the
15280304Sjkim *    documentation and/or other materials provided with the distribution.
1655714Skris * 3. Neither the name of The Internet Software Consortium nor the names
1755714Skris *    of its contributors may be used to endorse or promote products derived
1855714Skris *    from this software without specific prior written permission.
1955714Skris *
2055714Skris * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
2155714Skris * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22280304Sjkim * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
2355714Skris * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2455714Skris * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
2555714Skris * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2655714Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2755714Skris * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
2855714Skris * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
2955714Skris * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
3055714Skris * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
3155714Skris * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3255714Skris * SUCH DAMAGE.
3355714Skris *
3455714Skris * This software has been written for the Internet Software Consortium
3555714Skris * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
3655714Skris * Enterprises.  To learn more about the Internet Software Consortium,
37280304Sjkim * see ``http://www.vix.com/isc''.  To learn more about Vixie
3855714Skris * Enterprises, see ``http://www.vix.com''.
3955714Skris */
40280304Sjkim
4155714Skris#include <sys/types.h>
4255714Skris#include <sys/ioctl.h>
4355714Skris#include <sys/socket.h>
4455714Skris
4555714Skris#include <arpa/inet.h>
4655714Skris
4755714Skris#include <net/if.h>
4855714Skris#include <net/if_dl.h>
4955714Skris#include <net/if_media.h>
5055714Skris
5155714Skris#include <netinet/in.h>
52280304Sjkim
5355714Skris#include <errno.h>
5455714Skris#include <ifaddrs.h>
5555714Skris#include <limits.h>
5655714Skris#include <poll.h>
5755714Skris#include <stdio.h>
5855714Skris#include <stdlib.h>
59109998Smarkm#include <string.h>
60109998Smarkm#include <syslog.h>
61109998Smarkm#include <time.h>
62280304Sjkim#include <unistd.h>
6355714Skris
6455714Skris#include "dhcp.h"
6555714Skris#include "tree.h"
66280304Sjkim#include "dhcpd.h"
67280304Sjkim#include "log.h"
68280304Sjkim#include "sync.h"
69280304Sjkim
7055714Skrisextern int syncfd;
7155714Skris
7255714Skrisstruct interface_info *interfaces;
73280304Sjkimstruct protocol *protocols;
74280304Sjkimstruct dhcpd_timeout *timeouts;
75280304Sjkimstatic struct dhcpd_timeout *free_timeouts;
76280304Sjkimstatic int interfaces_invalidated;
77280304Sjkim
78280304Sjkimstatic int interface_status(struct interface_info *ifinfo);
79280304Sjkimint get_rdomain(char *);
80280304Sjkim
81280304Sjkim/* Use getifaddrs() to get a list of all the attached interfaces.
82280304Sjkim   For each interface that's of type INET and not the loopback interface,
83280304Sjkim   register that interface with the network I/O software, figure out what
84280304Sjkim   subnet it's on, and add it to the list of interfaces. */
85280304Sjkim
86280304Sjkimvoid
8755714Skrisdiscover_interfaces(int *rdomain)
8855714Skris{
89280304Sjkim	struct interface_info *tmp;
90280304Sjkim	struct interface_info *last, *next;
91280304Sjkim	struct subnet *subnet;
92280304Sjkim	struct shared_network *share;
9355714Skris	struct sockaddr_in foo;
9455714Skris	int ir = 0, ird;
9555714Skris	struct ifreq *tif;
96280304Sjkim	struct ifaddrs *ifap, *ifa;
97280304Sjkim
98280304Sjkim	if (getifaddrs(&ifap) != 0)
99280304Sjkim		fatalx("getifaddrs failed");
100280304Sjkim
101280304Sjkim	/*
102280304Sjkim	 * If we already have a list of interfaces, the interfaces were
103280304Sjkim	 * requested.
104280304Sjkim	 */
105280304Sjkim	if (interfaces != NULL)
106280304Sjkim		ir = 1;
107280304Sjkim	else
108280304Sjkim		/* must specify an interface when rdomains are used */
10955714Skris		*rdomain = 0;
11055714Skris
111280304Sjkim	/* Cycle through the list of interfaces looking for IP addresses. */
112280304Sjkim	for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
113280304Sjkim		/*
114280304Sjkim		 * See if this is the sort of interface we want to
115280304Sjkim		 * deal with.
116280304Sjkim		 */
117280304Sjkim		if ((ifa->ifa_flags & IFF_LOOPBACK) ||
118280304Sjkim		    (ifa->ifa_flags & IFF_POINTOPOINT) ||
119280304Sjkim		    (!(ifa->ifa_flags & IFF_BROADCAST)))
120280304Sjkim			continue;
121280304Sjkim
122280304Sjkim		/* See if we've seen an interface that matches this one. */
123280304Sjkim		for (tmp = interfaces; tmp; tmp = tmp->next)
124280304Sjkim			if (!strcmp(tmp->name, ifa->ifa_name))
12555714Skris				break;
12655714Skris
127280304Sjkim		/* If we are looking for specific interfaces, ignore others. */
128280304Sjkim		if (tmp == NULL && ir)
129280304Sjkim			continue;
130280304Sjkim
131280304Sjkim		ird = get_rdomain(ifa->ifa_name);
132280304Sjkim		if (*rdomain == -1)
133280304Sjkim			*rdomain = ird;
134280304Sjkim		else if (*rdomain != ird && ir)
135280304Sjkim			fatalx("Interface %s is not in rdomain %d",
136280304Sjkim			    tmp->name, *rdomain);
137280304Sjkim		else if (*rdomain != ird && !ir)
138280304Sjkim			continue;
139280304Sjkim
14055714Skris		/* If there isn't already an interface by this name,
14155714Skris		   allocate one. */
14255714Skris		if (tmp == NULL) {
14355714Skris			tmp = calloc(1, sizeof *tmp);
14455714Skris			if (!tmp)
14555714Skris				fatalx("Insufficient memory to %s %s",
14655714Skris				    "record interface", ifa->ifa_name);
14755714Skris			strlcpy(tmp->name, ifa->ifa_name, sizeof(tmp->name));
14855714Skris			tmp->next = interfaces;
14955714Skris			tmp->noifmedia = tmp->dead = tmp->errors = 0;
15055714Skris			interfaces = tmp;
15155714Skris		}
15255714Skris
153109998Smarkm		/* If we have the capability, extract link information
154280304Sjkim		   and record it in a linked list. */
15555714Skris		if (ifa->ifa_addr->sa_family == AF_LINK) {
156280304Sjkim			struct sockaddr_dl *sdl =
15755714Skris			    ((struct sockaddr_dl *)(ifa->ifa_addr));
15855714Skris			tmp->index = sdl->sdl_index;
15955714Skris			tmp->hw_address.hlen = sdl->sdl_alen;
16055714Skris			tmp->hw_address.htype = HTYPE_ETHER; /* XXX */
16155714Skris			memcpy(tmp->hw_address.haddr,
16255714Skris			    LLADDR(sdl), sdl->sdl_alen);
163280304Sjkim		} else if (ifa->ifa_addr->sa_family == AF_INET) {
16455714Skris			struct iaddr addr;
16555714Skris
16655714Skris			/* Get a pointer to the address... */
167280304Sjkim			memcpy(&foo, ifa->ifa_addr, sizeof(foo));
168280304Sjkim
169280304Sjkim			/* We don't want the loopback interface. */
170280304Sjkim			if (foo.sin_addr.s_addr == htonl (INADDR_LOOPBACK))
171280304Sjkim				continue;
172280304Sjkim
173280304Sjkim			/* If this is the first real IP address we've
174280304Sjkim			   found, keep a pointer to ifreq structure in
175280304Sjkim			   which we found it. */
176280304Sjkim			if (!tmp->ifp) {
177280304Sjkim				int len = (IFNAMSIZ + ifa->ifa_addr->sa_len);
178280304Sjkim				tif = malloc(len);
179280304Sjkim				if (!tif)
180280304Sjkim					fatalx("no space to remember ifp.");
181280304Sjkim				strlcpy(tif->ifr_name, ifa->ifa_name,
18255714Skris				    IFNAMSIZ);
183280304Sjkim				memcpy(&tif->ifr_addr, ifa->ifa_addr,
184280304Sjkim				    ifa->ifa_addr->sa_len);
185280304Sjkim				tmp->ifp = tif;
186280304Sjkim				tmp->primary_address = foo.sin_addr;
187280304Sjkim			}
188280304Sjkim
189280304Sjkim			/* Grab the address... */
190280304Sjkim			addr.len = 4;
191280304Sjkim			memcpy(addr.iabuf, &foo.sin_addr.s_addr, addr.len);
192280304Sjkim
193280304Sjkim			/* If there's a registered subnet for this address,
194280304Sjkim			   connect it together... */
195280304Sjkim			if ((subnet = find_subnet(addr))) {
196280304Sjkim				/* If this interface has multiple aliases
197280304Sjkim				   on the same subnet, ignore all but the
198280304Sjkim				   first we encounter. */
199280304Sjkim				if (!subnet->interface) {
200280304Sjkim					subnet->interface = tmp;
201280304Sjkim					subnet->interface_address = addr;
202280304Sjkim				} else if (subnet->interface != tmp) {
203280304Sjkim					log_warnx("Multiple %s %s: %s %s",
20455714Skris					    "interfaces match the",
205280304Sjkim					    "same subnet",
206280304Sjkim					    subnet->interface->name,
207280304Sjkim					    tmp->name);
208280304Sjkim				}
209280304Sjkim				share = subnet->shared_network;
210280304Sjkim				if (tmp->shared_network &&
211280304Sjkim				    tmp->shared_network != share) {
212280304Sjkim					log_warnx("Interface %s matches %s",
213280304Sjkim					    tmp->name,
214280304Sjkim					    "multiple shared networks");
215280304Sjkim				} else {
21655714Skris					tmp->shared_network = share;
21755714Skris				}
218160814Ssimon
219160814Ssimon				if (!share->interface) {
220160814Ssimon					share->interface = tmp;
221160814Ssimon				} else if (share->interface != tmp) {
222160814Ssimon					log_warnx("Multiple %s %s: %s %s",
223160814Ssimon					    "interfaces match the",
224160814Ssimon					    "same shared network",
225160814Ssimon					    share->interface->name,
226					    tmp->name);
227				}
228			}
229		}
230	}
231
232	/* Discard interfaces we can't listen on. */
233	last = NULL;
234	for (tmp = interfaces; tmp; tmp = next) {
235		next = tmp->next;
236
237		if (!tmp->ifp) {
238			log_warnx("Can't listen on %s - it has no IP address.",
239			    tmp->name);
240			/* Remove tmp from the list of interfaces. */
241			if (!last)
242				interfaces = interfaces->next;
243			else
244				last->next = tmp->next;
245			continue;
246		}
247
248		memcpy(&foo, &tmp->ifp->ifr_addr, sizeof tmp->ifp->ifr_addr);
249
250		if (!tmp->shared_network) {
251			log_warnx("Can't listen on %s - dhcpd.conf has no "
252			    "subnet declaration for %s.", tmp->name,
253			    inet_ntoa(foo.sin_addr));
254			/* Remove tmp from the list of interfaces. */
255			if (!last)
256				interfaces = interfaces->next;
257			else
258				last->next = tmp->next;
259			continue;
260		}
261
262		last = tmp;
263
264		/* Find subnets that don't have valid interface addresses. */
265		for (subnet = (tmp->shared_network ?
266		    tmp->shared_network->subnets : NULL); subnet;
267		    subnet = subnet->next_sibling) {
268			if (!subnet->interface_address.len) {
269				/*
270				 * Set the interface address for this subnet
271				 * to the first address we found.
272				 */
273				subnet->interface_address.len = 4;
274				memcpy(subnet->interface_address.iabuf,
275				    &foo.sin_addr.s_addr, 4);
276			}
277		}
278
279		/* Register the interface... */
280		if_register_receive(tmp);
281		if_register_send(tmp);
282		log_info("Listening on %s (%s).", tmp->name,
283		    inet_ntoa(foo.sin_addr));
284	}
285
286	if (interfaces == NULL)
287		fatalx("No interfaces to listen on.");
288
289	/* Now register all the remaining interfaces as protocols. */
290	for (tmp = interfaces; tmp; tmp = tmp->next)
291		add_protocol(tmp->name, tmp->rfdesc, got_one, tmp);
292
293	freeifaddrs(ifap);
294}
295
296/*
297 * Wait for packets to come in using poll().  When a packet comes in,
298 * call receive_packet to receive the packet and possibly strip hardware
299 * addressing information from it, and then process it in do_packet.
300 */
301void
302dispatch(void)
303{
304	int nfds, i, to_msec;
305	struct protocol *l;
306	static struct pollfd *fds;
307	static int nfds_max;
308	time_t howlong;
309
310	for (nfds = 0, l = protocols; l; l = l->next)
311		nfds++;
312	if (syncfd != -1)
313		nfds++;
314	if (nfds > nfds_max) {
315		fds = reallocarray(fds, nfds, sizeof(struct pollfd));
316		if (fds == NULL)
317			fatalx("Can't allocate poll structures.");
318		nfds_max = nfds;
319	}
320
321	for (;;) {
322		/*
323		 * Call any expired timeouts, and then if there's
324		 * still a timeout registered, time out the poll
325		 * call then.
326		 */
327		time(&cur_time);
328another:
329		if (timeouts) {
330			if (timeouts->when <= cur_time) {
331				struct dhcpd_timeout *t = timeouts;
332				timeouts = timeouts->next;
333				(*(t->func))(t->what);
334				t->next = free_timeouts;
335				free_timeouts = t;
336				goto another;
337			}
338
339			/*
340			 * Figure timeout in milliseconds, and check for
341			 * potential overflow, so we can cram into an int
342			 * for poll, while not polling with a negative
343			 * timeout and blocking indefinitely.
344			 */
345			howlong = timeouts->when - cur_time;
346			if (howlong > INT_MAX / 1000)
347				howlong = INT_MAX / 1000;
348			to_msec = howlong * 1000;
349		} else
350			to_msec = -1;
351
352		/* Set up the descriptors to be polled. */
353		for (i = 0, l = protocols; l; l = l->next) {
354			struct interface_info *ip = l->local;
355
356			if (ip && (l->handler != got_one || !ip->dead)) {
357				fds[i].fd = l->fd;
358				fds[i].events = POLLIN;
359				++i;
360			}
361		}
362
363		if (i == 0)
364			fatalx("No live interfaces to poll on - exiting.");
365
366		if (syncfd != -1) {
367			/* add syncer */
368			fds[i].fd = syncfd;
369			fds[i].events = POLLIN;
370		}
371
372		/* Wait for a packet or a timeout... */
373		switch (poll(fds, nfds, to_msec)) {
374		case -1:
375			if (errno != EAGAIN && errno != EINTR)
376				fatal("poll");
377			/* FALLTHROUGH */
378		case 0:
379			continue;	/* no packets */
380		}
381		time(&cur_time);
382
383		for (i = 0, l = protocols; l; l = l->next) {
384			struct interface_info *ip = l->local;
385
386			if ((fds[i].revents & (POLLIN | POLLHUP))) {
387				if (ip && (l->handler != got_one ||
388				    !ip->dead))
389					(*(l->handler))(l);
390				if (interfaces_invalidated)
391					break;
392			}
393			++i;
394		}
395		if ((syncfd != -1) && (fds[i].revents & (POLLIN | POLLHUP)))
396			sync_recv();
397		interfaces_invalidated = 0;
398	}
399}
400
401
402void
403got_one(struct protocol *l)
404{
405	struct sockaddr_in from;
406	struct hardware hfrom;
407	struct iaddr ifrom;
408	ssize_t result;
409	union {
410		unsigned char packbuf[4095];
411		struct dhcp_packet packet;
412	} u;
413	struct interface_info *ip = l->local;
414
415	memset(&u, 0, sizeof(u));
416
417	if ((result = receive_packet(ip, u.packbuf, sizeof u,
418	    &from, &hfrom)) == -1) {
419		log_warn("receive_packet failed on %s", ip->name);
420		ip->errors++;
421		if ((!interface_status(ip)) ||
422		    (ip->noifmedia && ip->errors > 20)) {
423			/* our interface has gone away. */
424			log_warnx("Interface %s no longer appears valid.",
425			    ip->name);
426			ip->dead = 1;
427			interfaces_invalidated = 1;
428			close(l->fd);
429			remove_protocol(l);
430			free(ip);
431		}
432		return;
433	}
434	if (result == 0)
435		return;
436
437	ifrom.len = 4;
438	memcpy(ifrom.iabuf, &from.sin_addr, ifrom.len);
439
440	do_packet(ip, &u.packet, result, from.sin_port, ifrom, &hfrom);
441}
442
443int
444interface_status(struct interface_info *ifinfo)
445{
446	char * ifname = ifinfo->name;
447	int ifsock = ifinfo->rfdesc;
448	struct ifreq ifr;
449	struct ifmediareq ifmr;
450
451	/* get interface flags */
452	memset(&ifr, 0, sizeof(ifr));
453	strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
454	if (ioctl(ifsock, SIOCGIFFLAGS, &ifr) == -1) {
455		log_warn("ioctl(SIOCGIFFLAGS) on %s", ifname);
456		goto inactive;
457	}
458	/*
459	 * if one of UP and RUNNING flags is dropped,
460	 * the interface is not active.
461	 */
462	if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
463		goto inactive;
464
465	/* Next, check carrier on the interface, if possible */
466	if (ifinfo->noifmedia)
467		goto active;
468	memset(&ifmr, 0, sizeof(ifmr));
469	strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
470	if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) == -1) {
471		if (errno != EINVAL) {
472			log_debug("ioctl(SIOCGIFMEDIA) on %s", ifname);
473			ifinfo->noifmedia = 1;
474			goto active;
475		}
476		/*
477		 * EINVAL (or ENOTTY) simply means that the interface
478		 * does not support the SIOCGIFMEDIA ioctl. We regard it alive.
479		 */
480		ifinfo->noifmedia = 1;
481		goto active;
482	}
483	if (ifmr.ifm_status & IFM_AVALID) {
484		switch (ifmr.ifm_active & IFM_NMASK) {
485		case IFM_ETHER:
486			if (ifmr.ifm_status & IFM_ACTIVE)
487				goto active;
488			else
489				goto inactive;
490			break;
491		default:
492			goto inactive;
493		}
494	}
495 inactive:
496	return (0);
497 active:
498	return (1);
499}
500
501int
502locate_network(struct packet *packet)
503{
504	struct iaddr ia;
505
506	/* If this came through a gateway, find the corresponding subnet... */
507	if (packet->raw->giaddr.s_addr) {
508		struct subnet *subnet;
509
510		ia.len = 4;
511		memcpy(ia.iabuf, &packet->raw->giaddr, 4);
512		subnet = find_subnet(ia);
513		if (subnet)
514			packet->shared_network = subnet->shared_network;
515		else
516			packet->shared_network = NULL;
517	} else {
518		packet->shared_network = packet->interface->shared_network;
519	}
520	if (packet->shared_network)
521		return 1;
522	return 0;
523}
524
525void
526add_timeout(time_t when, void (*where)(void *), void *what)
527{
528	struct dhcpd_timeout *t, *q;
529
530	/* See if this timeout supersedes an existing timeout. */
531	t = NULL;
532	for (q = timeouts; q; q = q->next) {
533		if (q->func == where && q->what == what) {
534			if (t)
535				t->next = q->next;
536			else
537				timeouts = q->next;
538			break;
539		}
540		t = q;
541	}
542
543	/* If we didn't supersede a timeout, allocate a timeout
544	   structure now. */
545	if (!q) {
546		if (free_timeouts) {
547			q = free_timeouts;
548			free_timeouts = q->next;
549			q->func = where;
550			q->what = what;
551		} else {
552			q = malloc(sizeof (struct dhcpd_timeout));
553			if (!q)
554				fatalx("Can't allocate timeout structure!");
555			q->func = where;
556			q->what = what;
557		}
558	}
559
560	q->when = when;
561
562	/* Now sort this timeout into the timeout list. */
563
564	/* Beginning of list? */
565	if (!timeouts || timeouts->when > q->when) {
566		q->next = timeouts;
567		timeouts = q;
568		return;
569	}
570
571	/* Middle of list? */
572	for (t = timeouts; t->next; t = t->next) {
573		if (t->next->when > q->when) {
574			q->next = t->next;
575			t->next = q;
576			return;
577		}
578	}
579
580	/* End of list. */
581	t->next = q;
582	q->next = NULL;
583}
584
585void
586cancel_timeout(void (*where)(void *), void *what)
587{
588	struct dhcpd_timeout *t, *q;
589
590	/* Look for this timeout on the list, and unlink it if we find it. */
591	t = NULL;
592	for (q = timeouts; q; q = q->next) {
593		if (q->func == where && q->what == what) {
594			if (t)
595				t->next = q->next;
596			else
597				timeouts = q->next;
598			break;
599		}
600		t = q;
601	}
602
603	/* If we found the timeout, put it on the free list. */
604	if (q) {
605		q->next = free_timeouts;
606		free_timeouts = q;
607	}
608}
609
610/* Add a protocol to the list of protocols... */
611void
612add_protocol(char *name, int fd, void (*handler)(struct protocol *),
613    void *local)
614{
615	struct protocol *p;
616
617	p = malloc(sizeof *p);
618	if (!p)
619		fatalx("can't allocate protocol struct for %s", name);
620	p->fd = fd;
621	p->handler = handler;
622	p->local = local;
623	p->next = protocols;
624	protocols = p;
625}
626
627void
628remove_protocol(struct protocol *proto)
629{
630	struct protocol *p, *next, *prev = NULL;
631
632	for (p = protocols; p; p = next) {
633		next = p->next;
634		if (p == proto) {
635			if (prev)
636				prev->next = p->next;
637			else
638				protocols = p->next;
639			free(p);
640		}
641	}
642}
643
644int
645get_rdomain(char *name)
646{
647	int rv = 0, s;
648	struct  ifreq ifr;
649
650	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
651		fatal("get_rdomain socket");
652
653	memset(&ifr, 0, sizeof(ifr));
654	strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
655	if (ioctl(s, SIOCGIFRDOMAIN, (caddr_t)&ifr) != -1)
656		rv = ifr.ifr_rdomainid;
657
658	close(s);
659	return rv;
660}
661