xref: /illumos-gate/usr/src/cmd/ipf/tools/ip_fil.c (revision 33f2fefd)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (C) 1993-2001, 2003 by Darren Reed.
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * See the IPFILTER.LICENCE file for details on licencing.
57c478bd9Sstevel@tonic-gate  *
6*33f2fefdSDarren Reed  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
77c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
87c478bd9Sstevel@tonic-gate  */
97c478bd9Sstevel@tonic-gate 
107c478bd9Sstevel@tonic-gate #if !defined(lint)
11ab25eeb5Syz static const char sccsid[] = "@(#)ip_fil.c	2.41 6/5/96 (C) 1993-2000 Darren Reed";
12ab25eeb5Syz static const char rcsid[] = "@(#)$Id: ip_fil.c,v 2.133.2.9 2005/01/08 14:22:18 darrenr Exp $";
137c478bd9Sstevel@tonic-gate #endif
147c478bd9Sstevel@tonic-gate 
157c478bd9Sstevel@tonic-gate #ifndef	SOLARIS
167c478bd9Sstevel@tonic-gate #define	SOLARIS	(defined(sun) && (defined(__svr4__) || defined(__SVR4)))
177c478bd9Sstevel@tonic-gate #endif
187c478bd9Sstevel@tonic-gate 
197c478bd9Sstevel@tonic-gate #include <sys/param.h>
207c478bd9Sstevel@tonic-gate #if defined(__FreeBSD__) && !defined(__FreeBSD_version)
217c478bd9Sstevel@tonic-gate # if defined(IPFILTER_LKM)
227c478bd9Sstevel@tonic-gate #  ifndef __FreeBSD_cc_version
237c478bd9Sstevel@tonic-gate #   include <osreldate.h>
247c478bd9Sstevel@tonic-gate #  else
257c478bd9Sstevel@tonic-gate #   if __FreeBSD_cc_version < 430000
267c478bd9Sstevel@tonic-gate #    include <osreldate.h>
277c478bd9Sstevel@tonic-gate #   endif
287c478bd9Sstevel@tonic-gate #  endif
297c478bd9Sstevel@tonic-gate # endif
307c478bd9Sstevel@tonic-gate #endif
317c478bd9Sstevel@tonic-gate #include <sys/errno.h>
32ab25eeb5Syz #if defined(__hpux) && (HPUXREV >= 1111) && !defined(_KERNEL)
33ab25eeb5Syz # include <sys/kern_svcs.h>
34ab25eeb5Syz #endif
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
36ab25eeb5Syz #define _KERNEL
37ab25eeb5Syz #define KERNEL
38ab25eeb5Syz #ifdef __OpenBSD__
397c478bd9Sstevel@tonic-gate struct file;
407c478bd9Sstevel@tonic-gate #endif
41ab25eeb5Syz #include <sys/uio.h>
42ab25eeb5Syz #undef _KERNEL
43ab25eeb5Syz #undef KERNEL
447c478bd9Sstevel@tonic-gate #include <sys/file.h>
457c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
467c478bd9Sstevel@tonic-gate #ifdef __sgi
477c478bd9Sstevel@tonic-gate # include <sys/ptimers.h>
487c478bd9Sstevel@tonic-gate #endif
497c478bd9Sstevel@tonic-gate #include <sys/time.h>
507c478bd9Sstevel@tonic-gate #if !SOLARIS
517c478bd9Sstevel@tonic-gate # if (NetBSD > 199609) || (OpenBSD > 199603) || (__FreeBSD_version >= 300000)
527c478bd9Sstevel@tonic-gate #  include <sys/dirent.h>
537c478bd9Sstevel@tonic-gate # else
547c478bd9Sstevel@tonic-gate #  include <sys/dir.h>
557c478bd9Sstevel@tonic-gate # endif
567c478bd9Sstevel@tonic-gate #else
577c478bd9Sstevel@tonic-gate # include <sys/filio.h>
587c478bd9Sstevel@tonic-gate #endif
59ab25eeb5Syz #ifndef linux
60ab25eeb5Syz # include <sys/protosw.h>
61ab25eeb5Syz #endif
627c478bd9Sstevel@tonic-gate #include <sys/socket.h>
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #include <stdio.h>
657c478bd9Sstevel@tonic-gate #include <string.h>
667c478bd9Sstevel@tonic-gate #include <stdlib.h>
677c478bd9Sstevel@tonic-gate #include <ctype.h>
687c478bd9Sstevel@tonic-gate #include <fcntl.h>
69f4b3ec61Sdh #include <sys/zone.h>
70ab25eeb5Syz #include <arpa/inet.h>
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #ifdef __hpux
737c478bd9Sstevel@tonic-gate # define _NET_ROUTE_INCLUDED
747c478bd9Sstevel@tonic-gate #endif
757c478bd9Sstevel@tonic-gate #include <net/if.h>
767c478bd9Sstevel@tonic-gate #ifdef sun
777c478bd9Sstevel@tonic-gate # include <net/af.h>
787c478bd9Sstevel@tonic-gate #endif
797c478bd9Sstevel@tonic-gate #if __FreeBSD_version >= 300000
807c478bd9Sstevel@tonic-gate # include <net/if_var.h>
817c478bd9Sstevel@tonic-gate #endif
827c478bd9Sstevel@tonic-gate #ifdef __sgi
837c478bd9Sstevel@tonic-gate #include <sys/debug.h>
847c478bd9Sstevel@tonic-gate # ifdef IFF_DRVRLOCK /* IRIX6 */
857c478bd9Sstevel@tonic-gate #include <sys/hashing.h>
867c478bd9Sstevel@tonic-gate # endif
877c478bd9Sstevel@tonic-gate #endif
88ab25eeb5Syz #if defined(__FreeBSD__)
89ab25eeb5Syz # include "radix_ipf.h"
90ab25eeb5Syz #endif
917c478bd9Sstevel@tonic-gate #include <net/route.h>
927c478bd9Sstevel@tonic-gate #include <netinet/in.h>
937c478bd9Sstevel@tonic-gate #if !(defined(__sgi) && !defined(IFF_DRVRLOCK)) /* IRIX < 6 */ && \
94ab25eeb5Syz     !defined(__hpux) && !defined(linux)
957c478bd9Sstevel@tonic-gate # include <netinet/in_var.h>
967c478bd9Sstevel@tonic-gate #endif
977c478bd9Sstevel@tonic-gate #include <netinet/in_systm.h>
987c478bd9Sstevel@tonic-gate #include <netinet/ip.h>
99ab25eeb5Syz #if !defined(linux)
100ab25eeb5Syz # include <netinet/ip_var.h>
101ab25eeb5Syz #endif
1027c478bd9Sstevel@tonic-gate #include <netinet/tcp.h>
1037c478bd9Sstevel@tonic-gate #if defined(__osf__)
1047c478bd9Sstevel@tonic-gate # include <netinet/tcp_timer.h>
1057c478bd9Sstevel@tonic-gate #endif
106ab25eeb5Syz #if defined(__osf__) || defined(__hpux) || defined(__sgi)
107ab25eeb5Syz # include "radix_ipf_local.h"
108ab25eeb5Syz # define _RADIX_H_
109ab25eeb5Syz #endif
1107c478bd9Sstevel@tonic-gate #include <netinet/udp.h>
1117c478bd9Sstevel@tonic-gate #include <netinet/tcpip.h>
1127c478bd9Sstevel@tonic-gate #include <netinet/ip_icmp.h>
1137c478bd9Sstevel@tonic-gate #include <unistd.h>
1147c478bd9Sstevel@tonic-gate #include <syslog.h>
1157c478bd9Sstevel@tonic-gate #ifdef __hpux
1167c478bd9Sstevel@tonic-gate # undef _NET_ROUTE_INCLUDED
1177c478bd9Sstevel@tonic-gate #endif
1187c478bd9Sstevel@tonic-gate #include "netinet/ip_compat.h"
1197c478bd9Sstevel@tonic-gate #include "netinet/ip_fil.h"
1207c478bd9Sstevel@tonic-gate #include "netinet/ip_nat.h"
1217c478bd9Sstevel@tonic-gate #include "netinet/ip_frag.h"
1227c478bd9Sstevel@tonic-gate #include "netinet/ip_state.h"
1237c478bd9Sstevel@tonic-gate #include "netinet/ip_proxy.h"
1247c478bd9Sstevel@tonic-gate #include "netinet/ip_auth.h"
1257c478bd9Sstevel@tonic-gate #ifdef	IPFILTER_SYNC
1267c478bd9Sstevel@tonic-gate #include "netinet/ip_sync.h"
1277c478bd9Sstevel@tonic-gate #endif
1287c478bd9Sstevel@tonic-gate #ifdef	IPFILTER_SCAN
1297c478bd9Sstevel@tonic-gate #include "netinet/ip_scan.h"
1307c478bd9Sstevel@tonic-gate #endif
1317c478bd9Sstevel@tonic-gate #include "netinet/ip_pool.h"
1327c478bd9Sstevel@tonic-gate #ifdef IPFILTER_COMPILED
1337c478bd9Sstevel@tonic-gate # include "netinet/ip_rules.h"
1347c478bd9Sstevel@tonic-gate #endif
135f4b3ec61Sdh #include "netinet/ipf_stack.h"
1367c478bd9Sstevel@tonic-gate #if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000)
1377c478bd9Sstevel@tonic-gate # include <sys/malloc.h>
1387c478bd9Sstevel@tonic-gate #endif
1397c478bd9Sstevel@tonic-gate #ifdef __hpux
1407c478bd9Sstevel@tonic-gate struct rtentry;
1417c478bd9Sstevel@tonic-gate #endif
142ab25eeb5Syz #include "md5.h"
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate #if !defined(__osf__)
1467c478bd9Sstevel@tonic-gate extern	struct	protosw	inetsw[];
1477c478bd9Sstevel@tonic-gate #endif
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate #include "ipt.h"
1507c478bd9Sstevel@tonic-gate static	struct	ifnet **ifneta = NULL;
1517c478bd9Sstevel@tonic-gate static	int	nifs = 0;
1527c478bd9Sstevel@tonic-gate 
153f4b3ec61Sdh static	int	frzerostats __P((caddr_t, ipf_stack_t *ifs));
154ab25eeb5Syz static	void	fr_setifpaddr __P((struct ifnet *, char *));
1557c478bd9Sstevel@tonic-gate void	init_ifp __P((void));
156ab25eeb5Syz #if defined(__sgi) && (IRIX < 60500)
1577c478bd9Sstevel@tonic-gate static int 	no_output __P((struct ifnet *, struct mbuf *,
1587c478bd9Sstevel@tonic-gate 			       struct sockaddr *));
1597c478bd9Sstevel@tonic-gate static int	write_output __P((struct ifnet *, struct mbuf *,
1607c478bd9Sstevel@tonic-gate 				  struct sockaddr *));
1617c478bd9Sstevel@tonic-gate #else
1627c478bd9Sstevel@tonic-gate # if TRU64 >= 1885
1637c478bd9Sstevel@tonic-gate static int 	no_output __P((struct ifnet *, struct mbuf *,
1647c478bd9Sstevel@tonic-gate 			       struct sockaddr *, struct rtentry *, char *));
1657c478bd9Sstevel@tonic-gate static int	write_output __P((struct ifnet *, struct mbuf *,
1667c478bd9Sstevel@tonic-gate 				  struct sockaddr *, struct rtentry *, char *));
1677c478bd9Sstevel@tonic-gate # else
1687c478bd9Sstevel@tonic-gate static int 	no_output __P((struct ifnet *, struct mbuf *,
1697c478bd9Sstevel@tonic-gate 			       struct sockaddr *, struct rtentry *));
1707c478bd9Sstevel@tonic-gate static int	write_output __P((struct ifnet *, struct mbuf *,
1717c478bd9Sstevel@tonic-gate 				  struct sockaddr *, struct rtentry *));
1727c478bd9Sstevel@tonic-gate # endif
1737c478bd9Sstevel@tonic-gate #endif
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 
1767ddc9b1aSDarren Reed int iplattach(ifs)
177f4b3ec61Sdh ipf_stack_t *ifs;
1787c478bd9Sstevel@tonic-gate {
179f4b3ec61Sdh 	ifs->ifs_fr_running = 1;
1807c478bd9Sstevel@tonic-gate 	return 0;
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 
184f4b3ec61Sdh int ipldetach(ifs)
185f4b3ec61Sdh ipf_stack_t *ifs;
1867c478bd9Sstevel@tonic-gate {
187f4b3ec61Sdh 	ifs->ifs_fr_running = -1;
1887c478bd9Sstevel@tonic-gate 	return 0;
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 
192f4b3ec61Sdh static	int	frzerostats(data, ifs)
1937c478bd9Sstevel@tonic-gate caddr_t	data;
194f4b3ec61Sdh ipf_stack_t *ifs;
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	friostat_t fio;
1977c478bd9Sstevel@tonic-gate 	int error;
1987c478bd9Sstevel@tonic-gate 
199f4b3ec61Sdh 	fr_getstat(&fio, ifs);
2007c478bd9Sstevel@tonic-gate 	error = copyoutptr(&fio, data, sizeof(fio));
2017c478bd9Sstevel@tonic-gate 	if (error)
2027c478bd9Sstevel@tonic-gate 		return EFAULT;
2037c478bd9Sstevel@tonic-gate 
204f4b3ec61Sdh 	bzero((char *)ifs->ifs_frstats, sizeof(*ifs->ifs_frstats) * 2);
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	return 0;
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate /*
2117c478bd9Sstevel@tonic-gate  * Filter ioctl interface.
2127c478bd9Sstevel@tonic-gate  */
2137c478bd9Sstevel@tonic-gate int iplioctl(dev, cmd, data, mode)
2147c478bd9Sstevel@tonic-gate int dev;
215ab25eeb5Syz ioctlcmd_t cmd;
2167c478bd9Sstevel@tonic-gate caddr_t data;
2177c478bd9Sstevel@tonic-gate int mode;
2187c478bd9Sstevel@tonic-gate {
219f4b3ec61Sdh 	int error = 0, unit = 0, tmp, uid;
2207c478bd9Sstevel@tonic-gate 	friostat_t fio;
221f4b3ec61Sdh 	ipf_stack_t *ifs;
222f4b3ec61Sdh 	extern ipf_stack_t *get_ifs();
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	unit = dev;
225f4b3ec61Sdh 	uid = getuid();
226f4b3ec61Sdh 
227f4b3ec61Sdh 	ifs = get_ifs();
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	SPL_NET(s);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	if (unit == IPL_LOGNAT) {
232f4b3ec61Sdh 		if (ifs->ifs_fr_running > 0)
233f4b3ec61Sdh 			error = fr_nat_ioctl(data, cmd, mode, uid, NULL, ifs);
2347c478bd9Sstevel@tonic-gate 		else
2357c478bd9Sstevel@tonic-gate 			error = EIO;
2367c478bd9Sstevel@tonic-gate 		SPL_X(s);
2377c478bd9Sstevel@tonic-gate 		return error;
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate 	if (unit == IPL_LOGSTATE) {
240f4b3ec61Sdh 		if (ifs->ifs_fr_running > 0)
241f4b3ec61Sdh 			error = fr_state_ioctl(data, cmd, mode, uid, NULL, ifs);
2427c478bd9Sstevel@tonic-gate 		else
2437c478bd9Sstevel@tonic-gate 			error = EIO;
2447c478bd9Sstevel@tonic-gate 		SPL_X(s);
2457c478bd9Sstevel@tonic-gate 		return error;
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate 	if (unit == IPL_LOGAUTH) {
248f4b3ec61Sdh 		if (ifs->ifs_fr_running > 0) {
249ab25eeb5Syz 			if ((cmd == (ioctlcmd_t)SIOCADAFR) ||
250ab25eeb5Syz 			    (cmd == (ioctlcmd_t)SIOCRMAFR)) {
2517c478bd9Sstevel@tonic-gate 				if (!(mode & FWRITE)) {
2527c478bd9Sstevel@tonic-gate 					error = EPERM;
2537c478bd9Sstevel@tonic-gate 				} else {
2547c478bd9Sstevel@tonic-gate 					error = frrequest(unit, cmd, data,
255f4b3ec61Sdh 					    ifs->ifs_fr_active, 1, ifs);
2567c478bd9Sstevel@tonic-gate 				}
2577c478bd9Sstevel@tonic-gate 			} else {
258*33f2fefdSDarren Reed 				error = fr_auth_ioctl(data, cmd, mode, uid,
259*33f2fefdSDarren Reed 						      NULL, ifs);
2607c478bd9Sstevel@tonic-gate 			}
2617c478bd9Sstevel@tonic-gate 		} else
2627c478bd9Sstevel@tonic-gate 			error = EIO;
2637c478bd9Sstevel@tonic-gate 		SPL_X(s);
2647c478bd9Sstevel@tonic-gate 		return error;
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 	if (unit == IPL_LOGSYNC) {
2677c478bd9Sstevel@tonic-gate #ifdef	IPFILTER_SYNC
268f4b3ec61Sdh 		if (ifs->ifs_fr_running > 0)
2697c478bd9Sstevel@tonic-gate 			error = fr_sync_ioctl(data, cmd, mode);
2707c478bd9Sstevel@tonic-gate 		else
2717c478bd9Sstevel@tonic-gate #endif
2727c478bd9Sstevel@tonic-gate 			error = EIO;
2737c478bd9Sstevel@tonic-gate 		SPL_X(s);
2747c478bd9Sstevel@tonic-gate 		return error;
2757c478bd9Sstevel@tonic-gate 	}
2767c478bd9Sstevel@tonic-gate 	if (unit == IPL_LOGSCAN) {
2777c478bd9Sstevel@tonic-gate #ifdef	IPFILTER_SCAN
278f4b3ec61Sdh 		if (ifs->ifs_fr_running > 0)
2797c478bd9Sstevel@tonic-gate 			error = fr_scan_ioctl(data, cmd, mode);
2807c478bd9Sstevel@tonic-gate 		else
2817c478bd9Sstevel@tonic-gate #endif
2827c478bd9Sstevel@tonic-gate 			error = EIO;
2837c478bd9Sstevel@tonic-gate 		SPL_X(s);
2847c478bd9Sstevel@tonic-gate 		return error;
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 	if (unit == IPL_LOGLOOKUP) {
287f4b3ec61Sdh 		if (ifs->ifs_fr_running > 0)
288f4b3ec61Sdh 			error = ip_lookup_ioctl(data, cmd, mode, uid,
289f4b3ec61Sdh 			    NULL, ifs);
2907c478bd9Sstevel@tonic-gate 		else
2917c478bd9Sstevel@tonic-gate 			error = EIO;
2927c478bd9Sstevel@tonic-gate 		SPL_X(s);
2937c478bd9Sstevel@tonic-gate 		return error;
2947c478bd9Sstevel@tonic-gate 	}
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	switch (cmd)
2977c478bd9Sstevel@tonic-gate 	{
2987c478bd9Sstevel@tonic-gate 	case FIONREAD :
2997c478bd9Sstevel@tonic-gate #ifdef IPFILTER_LOG
300f4b3ec61Sdh 		error = COPYOUT(&ifs->ifs_iplused[IPL_LOGIPF], (caddr_t)data,
301f4b3ec61Sdh 			       sizeof(ifs->ifs_iplused[IPL_LOGIPF]));
3027c478bd9Sstevel@tonic-gate #endif
3037c478bd9Sstevel@tonic-gate 		break;
3047c478bd9Sstevel@tonic-gate 	case SIOCFRENB :
3057c478bd9Sstevel@tonic-gate 		if (!(mode & FWRITE))
3067c478bd9Sstevel@tonic-gate 			error = EPERM;
3077c478bd9Sstevel@tonic-gate 		else {
3087c478bd9Sstevel@tonic-gate 			error = COPYIN(data, &tmp, sizeof(tmp));
3097c478bd9Sstevel@tonic-gate 			if (error)
3107c478bd9Sstevel@tonic-gate 				break;
3117c478bd9Sstevel@tonic-gate 			if (tmp)
3127ddc9b1aSDarren Reed 				error = iplattach(ifs);
3137c478bd9Sstevel@tonic-gate 			else
314f4b3ec61Sdh 				error = ipldetach(ifs);
3157c478bd9Sstevel@tonic-gate 		}
3167c478bd9Sstevel@tonic-gate 		break;
317ab25eeb5Syz 	case SIOCIPFSET :
318ab25eeb5Syz 		if (!(mode & FWRITE)) {
319ab25eeb5Syz 			error = EPERM;
320ab25eeb5Syz 			break;
321ab25eeb5Syz 		}
322ab25eeb5Syz 	case SIOCIPFGETNEXT :
323ab25eeb5Syz 	case SIOCIPFGET :
324f4b3ec61Sdh 		error = fr_ipftune(cmd, (void *)data, ifs);
325ab25eeb5Syz 		break;
3267c478bd9Sstevel@tonic-gate 	case SIOCSETFF :
3277c478bd9Sstevel@tonic-gate 		if (!(mode & FWRITE))
3287c478bd9Sstevel@tonic-gate 			error = EPERM;
3297c478bd9Sstevel@tonic-gate 		else
330f4b3ec61Sdh 			error = COPYIN(data, &ifs->ifs_fr_flags,
331f4b3ec61Sdh 			    sizeof(ifs->ifs_fr_flags));
3327c478bd9Sstevel@tonic-gate 		break;
3337c478bd9Sstevel@tonic-gate 	case SIOCGETFF :
334f4b3ec61Sdh 		error = COPYOUT(&ifs->ifs_fr_flags, data,
335f4b3ec61Sdh 		    sizeof(ifs->ifs_fr_flags));
3367c478bd9Sstevel@tonic-gate 		break;
3377c478bd9Sstevel@tonic-gate 	case SIOCFUNCL :
3387c478bd9Sstevel@tonic-gate 		error = fr_resolvefunc(data);
3397c478bd9Sstevel@tonic-gate 		break;
3407c478bd9Sstevel@tonic-gate 	case SIOCINAFR :
3417c478bd9Sstevel@tonic-gate 	case SIOCRMAFR :
3427c478bd9Sstevel@tonic-gate 	case SIOCADAFR :
3437c478bd9Sstevel@tonic-gate 	case SIOCZRLST :
3447c478bd9Sstevel@tonic-gate 		if (!(mode & FWRITE))
3457c478bd9Sstevel@tonic-gate 			error = EPERM;
3467c478bd9Sstevel@tonic-gate 		else
347f4b3ec61Sdh 			error = frrequest(unit, cmd, data,
348f4b3ec61Sdh 			    ifs->ifs_fr_active, 1, ifs);
3497c478bd9Sstevel@tonic-gate 		break;
3507c478bd9Sstevel@tonic-gate 	case SIOCINIFR :
3517c478bd9Sstevel@tonic-gate 	case SIOCRMIFR :
3527c478bd9Sstevel@tonic-gate 	case SIOCADIFR :
3537c478bd9Sstevel@tonic-gate 		if (!(mode & FWRITE))
3547c478bd9Sstevel@tonic-gate 			error = EPERM;
3557c478bd9Sstevel@tonic-gate 		else
356f4b3ec61Sdh 			error = frrequest(unit, cmd, data,
357f4b3ec61Sdh 			    1 - ifs->ifs_fr_active, 1, ifs);
3587c478bd9Sstevel@tonic-gate 		break;
3597c478bd9Sstevel@tonic-gate 	case SIOCSWAPA :
3607c478bd9Sstevel@tonic-gate 		if (!(mode & FWRITE))
3617c478bd9Sstevel@tonic-gate 			error = EPERM;
3627c478bd9Sstevel@tonic-gate 		else {
363f4b3ec61Sdh 			*(u_int *)data = ifs->ifs_fr_active;
364f4b3ec61Sdh 			ifs->ifs_fr_active = 1 - ifs->ifs_fr_active;
3657c478bd9Sstevel@tonic-gate 		}
3667c478bd9Sstevel@tonic-gate 		break;
3677c478bd9Sstevel@tonic-gate 	case SIOCGETFS :
368f4b3ec61Sdh 		fr_getstat(&fio, ifs);
3697c478bd9Sstevel@tonic-gate 		error = fr_outobj(data, &fio, IPFOBJ_IPFSTAT);
3707c478bd9Sstevel@tonic-gate 		break;
3717c478bd9Sstevel@tonic-gate 	case	SIOCFRZST :
3727c478bd9Sstevel@tonic-gate 		if (!(mode & FWRITE))
3737c478bd9Sstevel@tonic-gate 			error = EPERM;
3747c478bd9Sstevel@tonic-gate 		else
375f4b3ec61Sdh 			error = frzerostats(data, ifs);
3767c478bd9Sstevel@tonic-gate 		break;
3777c478bd9Sstevel@tonic-gate 	case	SIOCIPFFL :
3787c478bd9Sstevel@tonic-gate 		if (!(mode & FWRITE))
3797c478bd9Sstevel@tonic-gate 			error = EPERM;
3807c478bd9Sstevel@tonic-gate 		else {
3817c478bd9Sstevel@tonic-gate 			error = COPYIN(data, &tmp, sizeof(tmp));
3827c478bd9Sstevel@tonic-gate 			if (!error) {
383f4b3ec61Sdh 				tmp = frflush(unit, 4, tmp, ifs);
3847c478bd9Sstevel@tonic-gate 				error = COPYOUT(&tmp, data, sizeof(tmp));
3857c478bd9Sstevel@tonic-gate 			}
3867c478bd9Sstevel@tonic-gate 		}
3877c478bd9Sstevel@tonic-gate 		break;
3887663b816Sml #ifdef	USE_INET6
3897663b816Sml 	case	SIOCIPFL6 :
3907663b816Sml 		if (!(mode & FWRITE))
3917663b816Sml 			error = EPERM;
3927663b816Sml 		else {
3937663b816Sml 			error = COPYIN(data, &tmp, sizeof(tmp));
3947663b816Sml 			if (!error) {
395f4b3ec61Sdh 				tmp = frflush(unit, 6, tmp, ifs);
3967663b816Sml 				error = COPYOUT(&tmp, data, sizeof(tmp));
3977663b816Sml 			}
3987663b816Sml 		}
3997663b816Sml 		break;
4007663b816Sml #endif
4017c478bd9Sstevel@tonic-gate 	case SIOCSTLCK :
4027c478bd9Sstevel@tonic-gate 		error = COPYIN(data, &tmp, sizeof(tmp));
4037c478bd9Sstevel@tonic-gate 		if (error == 0) {
404f4b3ec61Sdh 			ifs->ifs_fr_state_lock = tmp;
405f4b3ec61Sdh 			ifs->ifs_fr_nat_lock = tmp;
406f4b3ec61Sdh 			ifs->ifs_fr_frag_lock = tmp;
407f4b3ec61Sdh 			ifs->ifs_fr_auth_lock = tmp;
4087c478bd9Sstevel@tonic-gate 		} else
4097c478bd9Sstevel@tonic-gate 			error = EFAULT;
4107c478bd9Sstevel@tonic-gate 		break;
4117c478bd9Sstevel@tonic-gate #ifdef	IPFILTER_LOG
4127c478bd9Sstevel@tonic-gate 	case	SIOCIPFFB :
4137c478bd9Sstevel@tonic-gate 		if (!(mode & FWRITE))
4147c478bd9Sstevel@tonic-gate 			error = EPERM;
4157c478bd9Sstevel@tonic-gate 		else
416f4b3ec61Sdh 			*(int *)data = ipflog_clear(unit, ifs);
4177c478bd9Sstevel@tonic-gate 		break;
4187c478bd9Sstevel@tonic-gate #endif /* IPFILTER_LOG */
4197c478bd9Sstevel@tonic-gate 	case SIOCGFRST :
420f4b3ec61Sdh 		error = fr_outobj(data, fr_fragstats(ifs), IPFOBJ_FRAGSTAT);
4217c478bd9Sstevel@tonic-gate 		break;
4227c478bd9Sstevel@tonic-gate 	case SIOCFRSYN :
4237c478bd9Sstevel@tonic-gate 		if (!(mode & FWRITE))
4247c478bd9Sstevel@tonic-gate 			error = EPERM;
4257c478bd9Sstevel@tonic-gate 		else {
426f4b3ec61Sdh 			frsync(IPFSYNC_RESYNC, IPFSYNC_RESYNC, NULL, NULL, ifs);
4277c478bd9Sstevel@tonic-gate 		}
4287c478bd9Sstevel@tonic-gate 		break;
4297c478bd9Sstevel@tonic-gate 	default :
4307c478bd9Sstevel@tonic-gate 		error = EINVAL;
4317c478bd9Sstevel@tonic-gate 		break;
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 	SPL_X(s);
4347c478bd9Sstevel@tonic-gate 	return error;
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 
438f4b3ec61Sdh void fr_forgetifp(ifp, ifs)
4397c478bd9Sstevel@tonic-gate void *ifp;
440f4b3ec61Sdh ipf_stack_t *ifs;
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate 	register frentry_t *f;
4437c478bd9Sstevel@tonic-gate 
444f4b3ec61Sdh 	WRITE_ENTER(&ifs->ifs_ipf_mutex);
445f4b3ec61Sdh 	for (f = ifs->ifs_ipacct[0][ifs->ifs_fr_active]; (f != NULL);
446f4b3ec61Sdh 	    f = f->fr_next)
4477c478bd9Sstevel@tonic-gate 		if (f->fr_ifa == ifp)
4487c478bd9Sstevel@tonic-gate 			f->fr_ifa = (void *)-1;
449f4b3ec61Sdh 	for (f = ifs->ifs_ipacct[1][ifs->ifs_fr_active]; (f != NULL);
450f4b3ec61Sdh 	    f = f->fr_next)
4517c478bd9Sstevel@tonic-gate 		if (f->fr_ifa == ifp)
4527c478bd9Sstevel@tonic-gate 			f->fr_ifa = (void *)-1;
453f4b3ec61Sdh 	for (f = ifs->ifs_ipfilter[0][ifs->ifs_fr_active]; (f != NULL);
454f4b3ec61Sdh 	    f = f->fr_next)
4557c478bd9Sstevel@tonic-gate 		if (f->fr_ifa == ifp)
4567c478bd9Sstevel@tonic-gate 			f->fr_ifa = (void *)-1;
457f4b3ec61Sdh 	for (f = ifs->ifs_ipfilter[1][ifs->ifs_fr_active]; (f != NULL);
458f4b3ec61Sdh 	    f = f->fr_next)
4597c478bd9Sstevel@tonic-gate 		if (f->fr_ifa == ifp)
4607c478bd9Sstevel@tonic-gate 			f->fr_ifa = (void *)-1;
4617c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
462f4b3ec61Sdh 	for (f = ifs->ifs_ipacct6[0][ifs->ifs_fr_active]; (f != NULL);
463f4b3ec61Sdh 	    f = f->fr_next)
4647c478bd9Sstevel@tonic-gate 		if (f->fr_ifa == ifp)
4657c478bd9Sstevel@tonic-gate 			f->fr_ifa = (void *)-1;
466f4b3ec61Sdh 	for (f = ifs->ifs_ipacct6[1][ifs->ifs_fr_active]; (f != NULL);
467f4b3ec61Sdh 	    f = f->fr_next)
4687c478bd9Sstevel@tonic-gate 		if (f->fr_ifa == ifp)
4697c478bd9Sstevel@tonic-gate 			f->fr_ifa = (void *)-1;
470f4b3ec61Sdh 	for (f = ifs->ifs_ipfilter6[0][ifs->ifs_fr_active]; (f != NULL);
471f4b3ec61Sdh 	    f = f->fr_next)
4727c478bd9Sstevel@tonic-gate 		if (f->fr_ifa == ifp)
4737c478bd9Sstevel@tonic-gate 			f->fr_ifa = (void *)-1;
474f4b3ec61Sdh 	for (f = ifs->ifs_ipfilter6[1][ifs->ifs_fr_active]; (f != NULL);
475f4b3ec61Sdh 	    f = f->fr_next)
4767c478bd9Sstevel@tonic-gate 		if (f->fr_ifa == ifp)
4777c478bd9Sstevel@tonic-gate 			f->fr_ifa = (void *)-1;
4787c478bd9Sstevel@tonic-gate #endif
479f4b3ec61Sdh 	RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
480d6c23f6fSyx 	fr_natifpsync(IPFSYNC_OLDIFP, 4, ifp, NULL, ifs);
481d6c23f6fSyx 	fr_natifpsync(IPFSYNC_OLDIFP, 6, ifp, NULL, ifs);
4827c478bd9Sstevel@tonic-gate }
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 
485f4b3ec61Sdh void fr_resolvedest(fdp, v, ifs)
4867c478bd9Sstevel@tonic-gate frdest_t *fdp;
4877c478bd9Sstevel@tonic-gate int v;
488f4b3ec61Sdh ipf_stack_t *ifs;
4897c478bd9Sstevel@tonic-gate {
4907c478bd9Sstevel@tonic-gate 	fdp->fd_ifp = NULL;
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	if (*fdp->fd_ifname) {
493f4b3ec61Sdh 		fdp->fd_ifp = GETIFP(fdp->fd_ifname, v, ifs);
4947c478bd9Sstevel@tonic-gate 		if (!fdp->fd_ifp)
4957c478bd9Sstevel@tonic-gate 			fdp->fd_ifp = (struct ifnet *)-1;
4967c478bd9Sstevel@tonic-gate 	}
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 
500ab25eeb5Syz #if defined(__sgi) && (IRIX < 60500)
5017c478bd9Sstevel@tonic-gate static int no_output(ifp, m, s)
5027c478bd9Sstevel@tonic-gate #else
5037c478bd9Sstevel@tonic-gate # if TRU64 >= 1885
5047c478bd9Sstevel@tonic-gate static int no_output (ifp, m, s, rt, cp)
5057c478bd9Sstevel@tonic-gate char *cp;
5067c478bd9Sstevel@tonic-gate # else
5077c478bd9Sstevel@tonic-gate static int no_output(ifp, m, s, rt)
5087c478bd9Sstevel@tonic-gate # endif
5097c478bd9Sstevel@tonic-gate struct rtentry *rt;
5107c478bd9Sstevel@tonic-gate #endif
5117c478bd9Sstevel@tonic-gate struct ifnet *ifp;
5127c478bd9Sstevel@tonic-gate struct mbuf *m;
5137c478bd9Sstevel@tonic-gate struct sockaddr *s;
5147c478bd9Sstevel@tonic-gate {
5157c478bd9Sstevel@tonic-gate 	return 0;
5167c478bd9Sstevel@tonic-gate }
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 
519ab25eeb5Syz #if defined(__sgi) && (IRIX < 60500)
5207c478bd9Sstevel@tonic-gate static int write_output(ifp, m, s)
5217c478bd9Sstevel@tonic-gate #else
5227c478bd9Sstevel@tonic-gate # if TRU64 >= 1885
5237c478bd9Sstevel@tonic-gate static int write_output (ifp, m, s, rt, cp)
5247c478bd9Sstevel@tonic-gate char *cp;
5257c478bd9Sstevel@tonic-gate # else
5267c478bd9Sstevel@tonic-gate static int write_output(ifp, m, s, rt)
5277c478bd9Sstevel@tonic-gate # endif
5287c478bd9Sstevel@tonic-gate struct rtentry *rt;
5297c478bd9Sstevel@tonic-gate #endif
5307c478bd9Sstevel@tonic-gate struct ifnet *ifp;
5317c478bd9Sstevel@tonic-gate struct mbuf *m;
5327c478bd9Sstevel@tonic-gate struct sockaddr *s;
5337c478bd9Sstevel@tonic-gate {
5347c478bd9Sstevel@tonic-gate 	char fname[32];
5357c478bd9Sstevel@tonic-gate 	mb_t *mb;
5367c478bd9Sstevel@tonic-gate 	ip_t *ip;
5377c478bd9Sstevel@tonic-gate 	int fd;
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	mb = (mb_t *)m;
5407c478bd9Sstevel@tonic-gate 	ip = MTOD(mb, ip_t *);
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate #if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
543ab25eeb5Syz     (defined(OpenBSD) && (OpenBSD >= 199603)) || defined(linux) || \
544ab25eeb5Syz     (defined(__FreeBSD__) && (__FreeBSD_version >= 501113))
5457c478bd9Sstevel@tonic-gate 	sprintf(fname, "/tmp/%s", ifp->if_xname);
5467c478bd9Sstevel@tonic-gate #else
5477c478bd9Sstevel@tonic-gate 	sprintf(fname, "/tmp/%s%d", ifp->if_name, ifp->if_unit);
5487c478bd9Sstevel@tonic-gate #endif
5497c478bd9Sstevel@tonic-gate 	fd = open(fname, O_WRONLY|O_APPEND);
5507c478bd9Sstevel@tonic-gate 	if (fd == -1) {
5517c478bd9Sstevel@tonic-gate 		perror("open");
5527c478bd9Sstevel@tonic-gate 		return -1;
5537c478bd9Sstevel@tonic-gate 	}
5547c478bd9Sstevel@tonic-gate 	write(fd, (char *)ip, ntohs(ip->ip_len));
5557c478bd9Sstevel@tonic-gate 	close(fd);
5567c478bd9Sstevel@tonic-gate 	return 0;
5577c478bd9Sstevel@tonic-gate }
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 
560ab25eeb5Syz static void fr_setifpaddr(ifp, addr)
561ab25eeb5Syz struct ifnet *ifp;
562ab25eeb5Syz char *addr;
563ab25eeb5Syz {
564ab25eeb5Syz #ifdef __sgi
565ab25eeb5Syz 	struct in_ifaddr *ifa;
566ab25eeb5Syz #else
567ab25eeb5Syz 	struct ifaddr *ifa;
568ab25eeb5Syz #endif
569ab25eeb5Syz 
570ab25eeb5Syz #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
571ab25eeb5Syz 	if (ifp->if_addrlist.tqh_first != NULL)
572ab25eeb5Syz #else
573ab25eeb5Syz # ifdef __sgi
574ab25eeb5Syz 	if (ifp->in_ifaddr != NULL)
575ab25eeb5Syz # else
576ab25eeb5Syz 	if (ifp->if_addrlist != NULL)
577ab25eeb5Syz # endif
578ab25eeb5Syz #endif
579ab25eeb5Syz 		return;
580ab25eeb5Syz 
581ab25eeb5Syz 	ifa = (struct ifaddr *)malloc(sizeof(*ifa));
582ab25eeb5Syz #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
583ab25eeb5Syz 	ifp->if_addrlist.tqh_first = ifa;
584ab25eeb5Syz #else
585ab25eeb5Syz # ifdef __sgi
586ab25eeb5Syz 	ifp->in_ifaddr = ifa;
587ab25eeb5Syz # else
588ab25eeb5Syz 	ifp->if_addrlist = ifa;
589ab25eeb5Syz # endif
590ab25eeb5Syz #endif
591ab25eeb5Syz 
592ab25eeb5Syz 	if (ifa != NULL) {
593ab25eeb5Syz 		struct sockaddr_in *sin;
594ab25eeb5Syz 
595ab25eeb5Syz #ifdef __sgi
596ab25eeb5Syz 		sin = (struct sockaddr_in *)&ifa->ia_addr;
597ab25eeb5Syz #else
598ab25eeb5Syz 		sin = (struct sockaddr_in *)&ifa->ifa_addr;
599ab25eeb5Syz #endif
600ab25eeb5Syz 		sin->sin_addr.s_addr = inet_addr(addr);
601ab25eeb5Syz 		if (sin->sin_addr.s_addr == 0)
602ab25eeb5Syz 			abort();
603ab25eeb5Syz 	}
604ab25eeb5Syz }
605ab25eeb5Syz 
606f4b3ec61Sdh /*ARGSUSED*/
607f4b3ec61Sdh struct ifnet *get_unit(name, v, ifs)
6087c478bd9Sstevel@tonic-gate char *name;
6097c478bd9Sstevel@tonic-gate int v;
610f4b3ec61Sdh ipf_stack_t *ifs;
6117c478bd9Sstevel@tonic-gate {
612ab25eeb5Syz 	struct ifnet *ifp, **ifpp, **old_ifneta;
613ab25eeb5Syz 	char *addr;
6147c478bd9Sstevel@tonic-gate #if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
615ab25eeb5Syz     (defined(OpenBSD) && (OpenBSD >= 199603)) || defined(linux) || \
616ab25eeb5Syz     (defined(__FreeBSD__) && (__FreeBSD_version >= 501113))
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	if (name == NULL)
6197c478bd9Sstevel@tonic-gate 		name = "anon0";
6207c478bd9Sstevel@tonic-gate 
621ab25eeb5Syz 	addr = strchr(name, '=');
622ab25eeb5Syz 	if (addr != NULL)
623ab25eeb5Syz 		*addr++ = '\0';
624ab25eeb5Syz 
625ab25eeb5Syz 	for (ifpp = ifneta; ifpp && (ifp = *ifpp); ifpp++) {
626ab25eeb5Syz 		if (!strcmp(name, ifp->if_xname)) {
627ab25eeb5Syz 			if (addr != NULL)
628ab25eeb5Syz 				fr_setifpaddr(ifp, addr);
6297c478bd9Sstevel@tonic-gate 			return ifp;
630ab25eeb5Syz 		}
6317c478bd9Sstevel@tonic-gate 	}
6327c478bd9Sstevel@tonic-gate #else
6337c478bd9Sstevel@tonic-gate 	char *s, ifname[LIFNAMSIZ+1];
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	if (name == NULL)
6367c478bd9Sstevel@tonic-gate 		name = "anon0";
6377c478bd9Sstevel@tonic-gate 
638ab25eeb5Syz 	addr = strchr(name, '=');
639ab25eeb5Syz 	if (addr != NULL)
640ab25eeb5Syz 		*addr++ = '\0';
641ab25eeb5Syz 
642ab25eeb5Syz 	for (ifpp = ifneta; ifpp && (ifp = *ifpp); ifpp++) {
643381a2a9aSdr 		COPYIFNAME(ifp, ifname, 0);
644ab25eeb5Syz 		if (!strcmp(name, ifname)) {
645ab25eeb5Syz 			if (addr != NULL)
646ab25eeb5Syz 				fr_setifpaddr(ifp, addr);
6477c478bd9Sstevel@tonic-gate 			return ifp;
648ab25eeb5Syz 		}
6497c478bd9Sstevel@tonic-gate 	}
6507c478bd9Sstevel@tonic-gate #endif
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	if (!ifneta) {
6537c478bd9Sstevel@tonic-gate 		ifneta = (struct ifnet **)malloc(sizeof(ifp) * 2);
6547c478bd9Sstevel@tonic-gate 		if (!ifneta)
6557c478bd9Sstevel@tonic-gate 			return NULL;
6567c478bd9Sstevel@tonic-gate 		ifneta[1] = NULL;
6577c478bd9Sstevel@tonic-gate 		ifneta[0] = (struct ifnet *)calloc(1, sizeof(*ifp));
6587c478bd9Sstevel@tonic-gate 		if (!ifneta[0]) {
6597c478bd9Sstevel@tonic-gate 			free(ifneta);
6607c478bd9Sstevel@tonic-gate 			return NULL;
6617c478bd9Sstevel@tonic-gate 		}
6627c478bd9Sstevel@tonic-gate 		nifs = 1;
6637c478bd9Sstevel@tonic-gate 	} else {
6647c478bd9Sstevel@tonic-gate 		old_ifneta = ifneta;
6657c478bd9Sstevel@tonic-gate 		nifs++;
6667c478bd9Sstevel@tonic-gate 		ifneta = (struct ifnet **)realloc(ifneta,
667ab25eeb5Syz 						  (nifs + 1) * sizeof(ifp));
6687c478bd9Sstevel@tonic-gate 		if (!ifneta) {
6697c478bd9Sstevel@tonic-gate 			free(old_ifneta);
6707c478bd9Sstevel@tonic-gate 			nifs = 0;
6717c478bd9Sstevel@tonic-gate 			return NULL;
6727c478bd9Sstevel@tonic-gate 		}
6737c478bd9Sstevel@tonic-gate 		ifneta[nifs] = NULL;
6747c478bd9Sstevel@tonic-gate 		ifneta[nifs - 1] = (struct ifnet *)malloc(sizeof(*ifp));
6757c478bd9Sstevel@tonic-gate 		if (!ifneta[nifs - 1]) {
6767c478bd9Sstevel@tonic-gate 			nifs--;
6777c478bd9Sstevel@tonic-gate 			return NULL;
6787c478bd9Sstevel@tonic-gate 		}
6797c478bd9Sstevel@tonic-gate 	}
6807c478bd9Sstevel@tonic-gate 	ifp = ifneta[nifs - 1];
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate #if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
683ab25eeb5Syz     (defined(OpenBSD) && (OpenBSD >= 199603)) || defined(linux) || \
684ab25eeb5Syz     (defined(__FreeBSD__) && (__FreeBSD_version >= 501113))
685ab25eeb5Syz 	(void) strncpy(ifp->if_xname, name, sizeof(ifp->if_xname));
6867c478bd9Sstevel@tonic-gate #else
687ab25eeb5Syz 	for (s = name; *s && !ISDIGIT(*s); s++)
6887c478bd9Sstevel@tonic-gate 		;
689ab25eeb5Syz 	if (*s && ISDIGIT(*s)) {
6907c478bd9Sstevel@tonic-gate 		ifp->if_unit = atoi(s);
6917c478bd9Sstevel@tonic-gate 		ifp->if_name = (char *)malloc(s - name + 1);
6925e985db5Sschuster 		if (ifp->if_name == NULL) {
6935e985db5Sschuster 			/*
6945e985db5Sschuster 			 * XXX do it more elegantly: free up mem,
6955e985db5Sschuster 			 * return NULL
6965e985db5Sschuster 			 */
6975e985db5Sschuster 			perror("malloc");
6985e985db5Sschuster 			exit(1);
6995e985db5Sschuster 		}
700ab25eeb5Syz 		(void) strncpy(ifp->if_name, name, s - name);
7017c478bd9Sstevel@tonic-gate 		ifp->if_name[s - name] = '\0';
7027c478bd9Sstevel@tonic-gate 	} else {
7037c478bd9Sstevel@tonic-gate 		ifp->if_name = strdup(name);
7047c478bd9Sstevel@tonic-gate 		ifp->if_unit = -1;
7057c478bd9Sstevel@tonic-gate 	}
7067c478bd9Sstevel@tonic-gate #endif
7077c478bd9Sstevel@tonic-gate 	ifp->if_output = no_output;
708ab25eeb5Syz 
709ab25eeb5Syz 	if (addr != NULL) {
710ab25eeb5Syz 		fr_setifpaddr(ifp, addr);
711ab25eeb5Syz 	}
712ab25eeb5Syz 
7137c478bd9Sstevel@tonic-gate 	return ifp;
7147c478bd9Sstevel@tonic-gate }
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate char *get_ifname(ifp)
7187c478bd9Sstevel@tonic-gate struct ifnet *ifp;
7197c478bd9Sstevel@tonic-gate {
7207c478bd9Sstevel@tonic-gate 	static char ifname[LIFNAMSIZ];
7217c478bd9Sstevel@tonic-gate 
722ab25eeb5Syz #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(linux) || \
723ab25eeb5Syz     (defined(__FreeBSD__) && (__FreeBSD_version >= 501113))
7247c478bd9Sstevel@tonic-gate 	sprintf(ifname, "%s", ifp->if_xname);
7257c478bd9Sstevel@tonic-gate #else
7267c478bd9Sstevel@tonic-gate 	sprintf(ifname, "%s%d", ifp->if_name, ifp->if_unit);
7277c478bd9Sstevel@tonic-gate #endif
7287c478bd9Sstevel@tonic-gate 	return ifname;
7297c478bd9Sstevel@tonic-gate }
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate void init_ifp()
7347c478bd9Sstevel@tonic-gate {
735ab25eeb5Syz 	struct ifnet *ifp, **ifpp;
7367c478bd9Sstevel@tonic-gate 	char fname[32];
7377c478bd9Sstevel@tonic-gate 	int fd;
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate #if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
740ab25eeb5Syz     (defined(OpenBSD) && (OpenBSD >= 199603)) || defined(linux) || \
741ab25eeb5Syz     (defined(__FreeBSD__) && (__FreeBSD_version >= 501113))
742ab25eeb5Syz 	for (ifpp = ifneta; ifpp && (ifp = *ifpp); ifpp++) {
7437c478bd9Sstevel@tonic-gate 		ifp->if_output = write_output;
7447c478bd9Sstevel@tonic-gate 		sprintf(fname, "/tmp/%s", ifp->if_xname);
7457c478bd9Sstevel@tonic-gate 		fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
7467c478bd9Sstevel@tonic-gate 		if (fd == -1)
7477c478bd9Sstevel@tonic-gate 			perror("open");
7487c478bd9Sstevel@tonic-gate 		else
7497c478bd9Sstevel@tonic-gate 			close(fd);
7507c478bd9Sstevel@tonic-gate 	}
7517c478bd9Sstevel@tonic-gate #else
7527c478bd9Sstevel@tonic-gate 
753ab25eeb5Syz 	for (ifpp = ifneta; ifpp && (ifp = *ifpp); ifpp++) {
7547c478bd9Sstevel@tonic-gate 		ifp->if_output = write_output;
7557c478bd9Sstevel@tonic-gate 		sprintf(fname, "/tmp/%s%d", ifp->if_name, ifp->if_unit);
7567c478bd9Sstevel@tonic-gate 		fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
7577c478bd9Sstevel@tonic-gate 		if (fd == -1)
7587c478bd9Sstevel@tonic-gate 			perror("open");
7597c478bd9Sstevel@tonic-gate 		else
7607c478bd9Sstevel@tonic-gate 			close(fd);
7617c478bd9Sstevel@tonic-gate 	}
7627c478bd9Sstevel@tonic-gate #endif
7637c478bd9Sstevel@tonic-gate }
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate int fr_fastroute(m, mpp, fin, fdp)
7677c478bd9Sstevel@tonic-gate mb_t *m, **mpp;
7687c478bd9Sstevel@tonic-gate fr_info_t *fin;
7697c478bd9Sstevel@tonic-gate frdest_t *fdp;
7707c478bd9Sstevel@tonic-gate {
7717c478bd9Sstevel@tonic-gate 	struct ifnet *ifp = fdp->fd_ifp;
7727c478bd9Sstevel@tonic-gate 	ip_t *ip = fin->fin_ip;
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	if (!ifp)
7757c478bd9Sstevel@tonic-gate 		return 0;	/* no routing table out here */
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 	ip->ip_len = htons((u_short)ip->ip_len);
7787c478bd9Sstevel@tonic-gate 	ip->ip_off = htons((u_short)(ip->ip_off | IP_MF));
7797c478bd9Sstevel@tonic-gate 	ip->ip_sum = 0;
780ab25eeb5Syz #if defined(__sgi) && (IRIX < 60500)
7817c478bd9Sstevel@tonic-gate 	(*ifp->if_output)(ifp, (void *)ip, NULL);
7827c478bd9Sstevel@tonic-gate # if TRU64 >= 1885
7837c478bd9Sstevel@tonic-gate 	(*ifp->if_output)(ifp, (void *)m, NULL, 0, 0);
7847c478bd9Sstevel@tonic-gate # else
7857c478bd9Sstevel@tonic-gate 	(*ifp->if_output)(ifp, (void *)m, NULL, 0);
7867c478bd9Sstevel@tonic-gate # endif
7877c478bd9Sstevel@tonic-gate #endif
7887c478bd9Sstevel@tonic-gate 	return 0;
7897c478bd9Sstevel@tonic-gate }
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate int fr_send_reset(fin)
7937c478bd9Sstevel@tonic-gate fr_info_t *fin;
7947c478bd9Sstevel@tonic-gate {
7957c478bd9Sstevel@tonic-gate 	verbose("- TCP RST sent\n");
7967c478bd9Sstevel@tonic-gate 	return 0;
7977c478bd9Sstevel@tonic-gate }
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate int fr_send_icmp_err(type, fin, dst)
8017c478bd9Sstevel@tonic-gate int type;
8027c478bd9Sstevel@tonic-gate fr_info_t *fin;
8037c478bd9Sstevel@tonic-gate int dst;
8047c478bd9Sstevel@tonic-gate {
805ab25eeb5Syz 	verbose("- ICMP unreachable sent\n");
8067c478bd9Sstevel@tonic-gate 	return 0;
8077c478bd9Sstevel@tonic-gate }
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 
810f4b3ec61Sdh void frsync(command, version, nic, data, ifs)
811381a2a9aSdr int command, version;
812381a2a9aSdr void *nic;
813381a2a9aSdr char *data;
814f4b3ec61Sdh ipf_stack_t *ifs;
8157c478bd9Sstevel@tonic-gate {
8167c478bd9Sstevel@tonic-gate 	return;
8177c478bd9Sstevel@tonic-gate }
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate void m_freem(m)
8217c478bd9Sstevel@tonic-gate mb_t *m;
8227c478bd9Sstevel@tonic-gate {
8237c478bd9Sstevel@tonic-gate 	return;
8247c478bd9Sstevel@tonic-gate }
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate void m_copydata(m, off, len, cp)
8287c478bd9Sstevel@tonic-gate mb_t *m;
8297c478bd9Sstevel@tonic-gate int off, len;
8307c478bd9Sstevel@tonic-gate caddr_t cp;
8317c478bd9Sstevel@tonic-gate {
8327c478bd9Sstevel@tonic-gate 	bcopy((char *)m + off, cp, len);
8337c478bd9Sstevel@tonic-gate }
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate int ipfuiomove(buf, len, rwflag, uio)
8377c478bd9Sstevel@tonic-gate caddr_t buf;
8387c478bd9Sstevel@tonic-gate int len, rwflag;
8397c478bd9Sstevel@tonic-gate struct uio *uio;
8407c478bd9Sstevel@tonic-gate {
8417c478bd9Sstevel@tonic-gate 	int left, ioc, num, offset;
8427c478bd9Sstevel@tonic-gate 	struct iovec *io;
8437c478bd9Sstevel@tonic-gate 	char *start;
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 	if (rwflag == UIO_READ) {
8467c478bd9Sstevel@tonic-gate 		left = len;
8477c478bd9Sstevel@tonic-gate 		ioc = 0;
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 		offset = uio->uio_offset;
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 		while ((left > 0) && (ioc < uio->uio_iovcnt)) {
8527c478bd9Sstevel@tonic-gate 			io = uio->uio_iov + ioc;
8537c478bd9Sstevel@tonic-gate 			num = io->iov_len;
8547c478bd9Sstevel@tonic-gate 			if (num > left)
8557c478bd9Sstevel@tonic-gate 				num = left;
8567c478bd9Sstevel@tonic-gate 			start = (char *)io->iov_base + offset;
8577c478bd9Sstevel@tonic-gate 			if (start > (char *)io->iov_base + io->iov_len) {
8587c478bd9Sstevel@tonic-gate 				offset -= io->iov_len;
8597c478bd9Sstevel@tonic-gate 				ioc++;
8607c478bd9Sstevel@tonic-gate 				continue;
8617c478bd9Sstevel@tonic-gate 			}
8627c478bd9Sstevel@tonic-gate 			bcopy(buf, start, num);
8637c478bd9Sstevel@tonic-gate 			uio->uio_resid -= num;
8647c478bd9Sstevel@tonic-gate 			uio->uio_offset += num;
8657c478bd9Sstevel@tonic-gate 			left -= num;
8667c478bd9Sstevel@tonic-gate 			if (left > 0)
8677c478bd9Sstevel@tonic-gate 				ioc++;
8687c478bd9Sstevel@tonic-gate 		}
8697c478bd9Sstevel@tonic-gate 		if (left > 0)
8707c478bd9Sstevel@tonic-gate 			return EFAULT;
8717c478bd9Sstevel@tonic-gate 	}
8727c478bd9Sstevel@tonic-gate 	return 0;
8737c478bd9Sstevel@tonic-gate }
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate u_32_t fr_newisn(fin)
8777c478bd9Sstevel@tonic-gate fr_info_t *fin;
8787c478bd9Sstevel@tonic-gate {
8797c478bd9Sstevel@tonic-gate 	static int iss_seq_off = 0;
8807c478bd9Sstevel@tonic-gate 	u_char hash[16];
8817c478bd9Sstevel@tonic-gate 	u_32_t newiss;
8827c478bd9Sstevel@tonic-gate 	MD5_CTX ctx;
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	/*
8857c478bd9Sstevel@tonic-gate 	 * Compute the base value of the ISS.  It is a hash
8867c478bd9Sstevel@tonic-gate 	 * of (saddr, sport, daddr, dport, secret).
8877c478bd9Sstevel@tonic-gate 	 */
8887c478bd9Sstevel@tonic-gate 	MD5Init(&ctx);
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	MD5Update(&ctx, (u_char *) &fin->fin_fi.fi_src,
8917c478bd9Sstevel@tonic-gate 		  sizeof(fin->fin_fi.fi_src));
8927c478bd9Sstevel@tonic-gate 	MD5Update(&ctx, (u_char *) &fin->fin_fi.fi_dst,
8937c478bd9Sstevel@tonic-gate 		  sizeof(fin->fin_fi.fi_dst));
8947c478bd9Sstevel@tonic-gate 	MD5Update(&ctx, (u_char *) &fin->fin_dat, sizeof(fin->fin_dat));
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	/* MD5Update(&ctx, ipf_iss_secret, sizeof(ipf_iss_secret)); */
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	MD5Final(hash, &ctx);
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 	memcpy(&newiss, hash, sizeof(newiss));
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 	/*
9037c478bd9Sstevel@tonic-gate 	 * Now increment our "timer", and add it in to
9047c478bd9Sstevel@tonic-gate 	 * the computed value.
9057c478bd9Sstevel@tonic-gate 	 *
9067c478bd9Sstevel@tonic-gate 	 * XXX Use `addin'?
9077c478bd9Sstevel@tonic-gate 	 * XXX TCP_ISSINCR too large to use?
9087c478bd9Sstevel@tonic-gate 	 */
9097c478bd9Sstevel@tonic-gate 	iss_seq_off += 0x00010000;
9107c478bd9Sstevel@tonic-gate 	newiss += iss_seq_off;
9117c478bd9Sstevel@tonic-gate 	return newiss;
9127c478bd9Sstevel@tonic-gate }
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
9167c478bd9Sstevel@tonic-gate /* Function:    fr_nextipid                                                 */
9177c478bd9Sstevel@tonic-gate /* Returns:     int - 0 == success, -1 == error (packet should be droppped) */
9187c478bd9Sstevel@tonic-gate /* Parameters:  fin(I) - pointer to packet information                      */
9197c478bd9Sstevel@tonic-gate /*                                                                          */
9207c478bd9Sstevel@tonic-gate /* Returns the next IPv4 ID to use for this packet.                         */
9217c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
9227c478bd9Sstevel@tonic-gate INLINE u_short fr_nextipid(fin)
9237c478bd9Sstevel@tonic-gate fr_info_t *fin;
9247c478bd9Sstevel@tonic-gate {
9257c478bd9Sstevel@tonic-gate 	static u_short ipid = 0;
9267c478bd9Sstevel@tonic-gate 	u_short id;
927f4b3ec61Sdh 	ipf_stack_t *ifs = fin->fin_ifs;
9287c478bd9Sstevel@tonic-gate 
929f4b3ec61Sdh 	MUTEX_ENTER(&ifs->ifs_ipf_rw);
9307c478bd9Sstevel@tonic-gate 	id = ipid++;
931f4b3ec61Sdh 	MUTEX_EXIT(&ifs->ifs_ipf_rw);
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 	return id;
9347c478bd9Sstevel@tonic-gate }
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate INLINE void fr_checkv4sum(fin)
9387c478bd9Sstevel@tonic-gate fr_info_t *fin;
9397c478bd9Sstevel@tonic-gate {
9407c478bd9Sstevel@tonic-gate 	if (fr_checkl4sum(fin) == -1)
9417c478bd9Sstevel@tonic-gate 		fin->fin_flx |= FI_BAD;
9427c478bd9Sstevel@tonic-gate }
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
9467c478bd9Sstevel@tonic-gate INLINE void fr_checkv6sum(fin)
9477c478bd9Sstevel@tonic-gate fr_info_t *fin;
9487c478bd9Sstevel@tonic-gate {
9497c478bd9Sstevel@tonic-gate 	if (fr_checkl4sum(fin) == -1)
9507c478bd9Sstevel@tonic-gate 		fin->fin_flx |= FI_BAD;
9517c478bd9Sstevel@tonic-gate }
9527c478bd9Sstevel@tonic-gate #endif
953ab25eeb5Syz 
954ab25eeb5Syz 
955ab25eeb5Syz /*
956ab25eeb5Syz  * See above for description, except that all addressing is in user space.
957ab25eeb5Syz  */
958ab25eeb5Syz int copyoutptr(src, dst, size)
959ab25eeb5Syz void *src, *dst;
960ab25eeb5Syz size_t size;
961ab25eeb5Syz {
962ab25eeb5Syz 	caddr_t ca;
963ab25eeb5Syz 
964ab25eeb5Syz 	bcopy(dst, (char *)&ca, sizeof(ca));
965ab25eeb5Syz 	bcopy(src, ca, size);
966ab25eeb5Syz 	return 0;
967ab25eeb5Syz }
968ab25eeb5Syz 
969ab25eeb5Syz 
970ab25eeb5Syz /*
971ab25eeb5Syz  * See above for description, except that all addressing is in user space.
972ab25eeb5Syz  */
973ab25eeb5Syz int copyinptr(src, dst, size)
974ab25eeb5Syz void *src, *dst;
975ab25eeb5Syz size_t size;
976ab25eeb5Syz {
977ab25eeb5Syz 	caddr_t ca;
978ab25eeb5Syz 
979ab25eeb5Syz 	bcopy(src, (char *)&ca, sizeof(ca));
980ab25eeb5Syz 	bcopy(ca, dst, size);
981ab25eeb5Syz 	return 0;
982ab25eeb5Syz }
983ab25eeb5Syz 
984ab25eeb5Syz 
985ab25eeb5Syz /*
986ab25eeb5Syz  * return the first IP Address associated with an interface
987ab25eeb5Syz  */
988f4b3ec61Sdh int fr_ifpaddr(v, atype, ifptr, inp, inpmask, ifs)
989ab25eeb5Syz int v, atype;
990ab25eeb5Syz void *ifptr;
991ab25eeb5Syz struct in_addr *inp, *inpmask;
992f4b3ec61Sdh ipf_stack_t *ifs;
993ab25eeb5Syz {
994ab25eeb5Syz 	struct ifnet *ifp = ifptr;
995ab25eeb5Syz #ifdef __sgi
996ab25eeb5Syz 	struct in_ifaddr *ifa;
997ab25eeb5Syz #else
998ab25eeb5Syz 	struct ifaddr *ifa;
999ab25eeb5Syz #endif
1000ab25eeb5Syz 
1001ab25eeb5Syz #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
1002ab25eeb5Syz 	ifa = ifp->if_addrlist.tqh_first;
1003ab25eeb5Syz #else
1004ab25eeb5Syz # ifdef __sgi
1005ab25eeb5Syz 	ifa = (struct in_ifaddr *)ifp->in_ifaddr;
1006ab25eeb5Syz # else
1007ab25eeb5Syz 	ifa = ifp->if_addrlist;
1008ab25eeb5Syz # endif
1009ab25eeb5Syz #endif
1010ab25eeb5Syz 	if (ifa != NULL) {
1011ab25eeb5Syz 		struct sockaddr_in *sin, mask;
1012ab25eeb5Syz 
1013ab25eeb5Syz 		mask.sin_addr.s_addr = 0xffffffff;
1014ab25eeb5Syz 
1015ab25eeb5Syz #ifdef __sgi
1016ab25eeb5Syz 		sin = (struct sockaddr_in *)&ifa->ia_addr;
1017ab25eeb5Syz #else
1018ab25eeb5Syz 		sin = (struct sockaddr_in *)&ifa->ifa_addr;
1019ab25eeb5Syz #endif
1020ab25eeb5Syz 
1021ab25eeb5Syz 		return fr_ifpfillv4addr(atype, sin, &mask, inp, inpmask);
1022ab25eeb5Syz 	}
1023ab25eeb5Syz 	return 0;
1024ab25eeb5Syz }
1025ab073b32Sdr 
1026ab073b32Sdr 
1027ab073b32Sdr /*
1028ab073b32Sdr  * This function is not meant to be random, rather just produce a
1029ab073b32Sdr  * sequence of numbers that isn't linear to show "randomness".
1030ab073b32Sdr  */
1031ab073b32Sdr u_32_t ipf_random()
1032ab073b32Sdr {
1033ab073b32Sdr 	static u_int last = 0xa5a5a5a5;
1034ab073b32Sdr 	static int calls = 0;
1035ab073b32Sdr 	int number;
1036ab073b32Sdr 
1037ab073b32Sdr 	calls++;
1038ab073b32Sdr 
1039ab073b32Sdr 	/*
1040ab073b32Sdr 	 * These are deliberately chosen to ensure that there is some
1041ab073b32Sdr 	 * attempt to test whether the output covers the range in test n18.
1042ab073b32Sdr 	 */
1043ab073b32Sdr 	switch (calls)
1044ab073b32Sdr 	{
1045ab073b32Sdr 	case 1 :
1046ab073b32Sdr 		number = 0;
1047ab073b32Sdr 		break;
1048ab073b32Sdr 	case 2 :
1049ab073b32Sdr 		number = 4;
1050ab073b32Sdr 		break;
1051ab073b32Sdr 	case 3 :
1052ab073b32Sdr 		number = 3999;
1053ab073b32Sdr 		break;
1054ab073b32Sdr 	case 4 :
1055ab073b32Sdr 		number = 4000;
1056ab073b32Sdr 		break;
1057ab073b32Sdr 	case 5 :
1058ab073b32Sdr 		number = 48999;
1059ab073b32Sdr 		break;
1060ab073b32Sdr 	case 6 :
1061ab073b32Sdr 		number = 49000;
1062ab073b32Sdr 		break;
1063ab073b32Sdr 	default :
1064ab073b32Sdr 		number = last;
1065ab073b32Sdr 		last *= calls;
1066ab073b32Sdr 		last++;
1067ab073b32Sdr 		number ^= last;
1068ab073b32Sdr 		break;
1069ab073b32Sdr 	}
1070ab073b32Sdr 	return number;
1071ab073b32Sdr }
1072