xref: /illumos-gate/usr/src/cmd/ipf/tools/ip_fil.c (revision 7ddc9b1a)
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  *
6d6c23f6fSyx  * Copyright 2008 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 
176*7ddc9b1aSDarren 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 {
258f4b3ec61Sdh 				error = fr_auth_ioctl(data, mode, cmd, uid, NULL, ifs);
2597c478bd9Sstevel@tonic-gate 			}
2607c478bd9Sstevel@tonic-gate 		} else
2617c478bd9Sstevel@tonic-gate 			error = EIO;
2627c478bd9Sstevel@tonic-gate 		SPL_X(s);
2637c478bd9Sstevel@tonic-gate 		return error;
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 	if (unit == IPL_LOGSYNC) {
2667c478bd9Sstevel@tonic-gate #ifdef	IPFILTER_SYNC
267f4b3ec61Sdh 		if (ifs->ifs_fr_running > 0)
2687c478bd9Sstevel@tonic-gate 			error = fr_sync_ioctl(data, cmd, mode);
2697c478bd9Sstevel@tonic-gate 		else
2707c478bd9Sstevel@tonic-gate #endif
2717c478bd9Sstevel@tonic-gate 			error = EIO;
2727c478bd9Sstevel@tonic-gate 		SPL_X(s);
2737c478bd9Sstevel@tonic-gate 		return error;
2747c478bd9Sstevel@tonic-gate 	}
2757c478bd9Sstevel@tonic-gate 	if (unit == IPL_LOGSCAN) {
2767c478bd9Sstevel@tonic-gate #ifdef	IPFILTER_SCAN
277f4b3ec61Sdh 		if (ifs->ifs_fr_running > 0)
2787c478bd9Sstevel@tonic-gate 			error = fr_scan_ioctl(data, cmd, mode);
2797c478bd9Sstevel@tonic-gate 		else
2807c478bd9Sstevel@tonic-gate #endif
2817c478bd9Sstevel@tonic-gate 			error = EIO;
2827c478bd9Sstevel@tonic-gate 		SPL_X(s);
2837c478bd9Sstevel@tonic-gate 		return error;
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 	if (unit == IPL_LOGLOOKUP) {
286f4b3ec61Sdh 		if (ifs->ifs_fr_running > 0)
287f4b3ec61Sdh 			error = ip_lookup_ioctl(data, cmd, mode, uid,
288f4b3ec61Sdh 			    NULL, ifs);
2897c478bd9Sstevel@tonic-gate 		else
2907c478bd9Sstevel@tonic-gate 			error = EIO;
2917c478bd9Sstevel@tonic-gate 		SPL_X(s);
2927c478bd9Sstevel@tonic-gate 		return error;
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	switch (cmd)
2967c478bd9Sstevel@tonic-gate 	{
2977c478bd9Sstevel@tonic-gate 	case FIONREAD :
2987c478bd9Sstevel@tonic-gate #ifdef IPFILTER_LOG
299f4b3ec61Sdh 		error = COPYOUT(&ifs->ifs_iplused[IPL_LOGIPF], (caddr_t)data,
300f4b3ec61Sdh 			       sizeof(ifs->ifs_iplused[IPL_LOGIPF]));
3017c478bd9Sstevel@tonic-gate #endif
3027c478bd9Sstevel@tonic-gate 		break;
3037c478bd9Sstevel@tonic-gate 	case SIOCFRENB :
3047c478bd9Sstevel@tonic-gate 		if (!(mode & FWRITE))
3057c478bd9Sstevel@tonic-gate 			error = EPERM;
3067c478bd9Sstevel@tonic-gate 		else {
3077c478bd9Sstevel@tonic-gate 			error = COPYIN(data, &tmp, sizeof(tmp));
3087c478bd9Sstevel@tonic-gate 			if (error)
3097c478bd9Sstevel@tonic-gate 				break;
3107c478bd9Sstevel@tonic-gate 			if (tmp)
311*7ddc9b1aSDarren Reed 				error = iplattach(ifs);
3127c478bd9Sstevel@tonic-gate 			else
313f4b3ec61Sdh 				error = ipldetach(ifs);
3147c478bd9Sstevel@tonic-gate 		}
3157c478bd9Sstevel@tonic-gate 		break;
316ab25eeb5Syz 	case SIOCIPFSET :
317ab25eeb5Syz 		if (!(mode & FWRITE)) {
318ab25eeb5Syz 			error = EPERM;
319ab25eeb5Syz 			break;
320ab25eeb5Syz 		}
321ab25eeb5Syz 	case SIOCIPFGETNEXT :
322ab25eeb5Syz 	case SIOCIPFGET :
323f4b3ec61Sdh 		error = fr_ipftune(cmd, (void *)data, ifs);
324ab25eeb5Syz 		break;
3257c478bd9Sstevel@tonic-gate 	case SIOCSETFF :
3267c478bd9Sstevel@tonic-gate 		if (!(mode & FWRITE))
3277c478bd9Sstevel@tonic-gate 			error = EPERM;
3287c478bd9Sstevel@tonic-gate 		else
329f4b3ec61Sdh 			error = COPYIN(data, &ifs->ifs_fr_flags,
330f4b3ec61Sdh 			    sizeof(ifs->ifs_fr_flags));
3317c478bd9Sstevel@tonic-gate 		break;
3327c478bd9Sstevel@tonic-gate 	case SIOCGETFF :
333f4b3ec61Sdh 		error = COPYOUT(&ifs->ifs_fr_flags, data,
334f4b3ec61Sdh 		    sizeof(ifs->ifs_fr_flags));
3357c478bd9Sstevel@tonic-gate 		break;
3367c478bd9Sstevel@tonic-gate 	case SIOCFUNCL :
3377c478bd9Sstevel@tonic-gate 		error = fr_resolvefunc(data);
3387c478bd9Sstevel@tonic-gate 		break;
3397c478bd9Sstevel@tonic-gate 	case SIOCINAFR :
3407c478bd9Sstevel@tonic-gate 	case SIOCRMAFR :
3417c478bd9Sstevel@tonic-gate 	case SIOCADAFR :
3427c478bd9Sstevel@tonic-gate 	case SIOCZRLST :
3437c478bd9Sstevel@tonic-gate 		if (!(mode & FWRITE))
3447c478bd9Sstevel@tonic-gate 			error = EPERM;
3457c478bd9Sstevel@tonic-gate 		else
346f4b3ec61Sdh 			error = frrequest(unit, cmd, data,
347f4b3ec61Sdh 			    ifs->ifs_fr_active, 1, ifs);
3487c478bd9Sstevel@tonic-gate 		break;
3497c478bd9Sstevel@tonic-gate 	case SIOCINIFR :
3507c478bd9Sstevel@tonic-gate 	case SIOCRMIFR :
3517c478bd9Sstevel@tonic-gate 	case SIOCADIFR :
3527c478bd9Sstevel@tonic-gate 		if (!(mode & FWRITE))
3537c478bd9Sstevel@tonic-gate 			error = EPERM;
3547c478bd9Sstevel@tonic-gate 		else
355f4b3ec61Sdh 			error = frrequest(unit, cmd, data,
356f4b3ec61Sdh 			    1 - ifs->ifs_fr_active, 1, ifs);
3577c478bd9Sstevel@tonic-gate 		break;
3587c478bd9Sstevel@tonic-gate 	case SIOCSWAPA :
3597c478bd9Sstevel@tonic-gate 		if (!(mode & FWRITE))
3607c478bd9Sstevel@tonic-gate 			error = EPERM;
3617c478bd9Sstevel@tonic-gate 		else {
362f4b3ec61Sdh 			*(u_int *)data = ifs->ifs_fr_active;
363f4b3ec61Sdh 			ifs->ifs_fr_active = 1 - ifs->ifs_fr_active;
3647c478bd9Sstevel@tonic-gate 		}
3657c478bd9Sstevel@tonic-gate 		break;
3667c478bd9Sstevel@tonic-gate 	case SIOCGETFS :
367f4b3ec61Sdh 		fr_getstat(&fio, ifs);
3687c478bd9Sstevel@tonic-gate 		error = fr_outobj(data, &fio, IPFOBJ_IPFSTAT);
3697c478bd9Sstevel@tonic-gate 		break;
3707c478bd9Sstevel@tonic-gate 	case	SIOCFRZST :
3717c478bd9Sstevel@tonic-gate 		if (!(mode & FWRITE))
3727c478bd9Sstevel@tonic-gate 			error = EPERM;
3737c478bd9Sstevel@tonic-gate 		else
374f4b3ec61Sdh 			error = frzerostats(data, ifs);
3757c478bd9Sstevel@tonic-gate 		break;
3767c478bd9Sstevel@tonic-gate 	case	SIOCIPFFL :
3777c478bd9Sstevel@tonic-gate 		if (!(mode & FWRITE))
3787c478bd9Sstevel@tonic-gate 			error = EPERM;
3797c478bd9Sstevel@tonic-gate 		else {
3807c478bd9Sstevel@tonic-gate 			error = COPYIN(data, &tmp, sizeof(tmp));
3817c478bd9Sstevel@tonic-gate 			if (!error) {
382f4b3ec61Sdh 				tmp = frflush(unit, 4, tmp, ifs);
3837c478bd9Sstevel@tonic-gate 				error = COPYOUT(&tmp, data, sizeof(tmp));
3847c478bd9Sstevel@tonic-gate 			}
3857c478bd9Sstevel@tonic-gate 		}
3867c478bd9Sstevel@tonic-gate 		break;
3877663b816Sml #ifdef	USE_INET6
3887663b816Sml 	case	SIOCIPFL6 :
3897663b816Sml 		if (!(mode & FWRITE))
3907663b816Sml 			error = EPERM;
3917663b816Sml 		else {
3927663b816Sml 			error = COPYIN(data, &tmp, sizeof(tmp));
3937663b816Sml 			if (!error) {
394f4b3ec61Sdh 				tmp = frflush(unit, 6, tmp, ifs);
3957663b816Sml 				error = COPYOUT(&tmp, data, sizeof(tmp));
3967663b816Sml 			}
3977663b816Sml 		}
3987663b816Sml 		break;
3997663b816Sml #endif
4007c478bd9Sstevel@tonic-gate 	case SIOCSTLCK :
4017c478bd9Sstevel@tonic-gate 		error = COPYIN(data, &tmp, sizeof(tmp));
4027c478bd9Sstevel@tonic-gate 		if (error == 0) {
403f4b3ec61Sdh 			ifs->ifs_fr_state_lock = tmp;
404f4b3ec61Sdh 			ifs->ifs_fr_nat_lock = tmp;
405f4b3ec61Sdh 			ifs->ifs_fr_frag_lock = tmp;
406f4b3ec61Sdh 			ifs->ifs_fr_auth_lock = tmp;
4077c478bd9Sstevel@tonic-gate 		} else
4087c478bd9Sstevel@tonic-gate 			error = EFAULT;
4097c478bd9Sstevel@tonic-gate 		break;
4107c478bd9Sstevel@tonic-gate #ifdef	IPFILTER_LOG
4117c478bd9Sstevel@tonic-gate 	case	SIOCIPFFB :
4127c478bd9Sstevel@tonic-gate 		if (!(mode & FWRITE))
4137c478bd9Sstevel@tonic-gate 			error = EPERM;
4147c478bd9Sstevel@tonic-gate 		else
415f4b3ec61Sdh 			*(int *)data = ipflog_clear(unit, ifs);
4167c478bd9Sstevel@tonic-gate 		break;
4177c478bd9Sstevel@tonic-gate #endif /* IPFILTER_LOG */
4187c478bd9Sstevel@tonic-gate 	case SIOCGFRST :
419f4b3ec61Sdh 		error = fr_outobj(data, fr_fragstats(ifs), IPFOBJ_FRAGSTAT);
4207c478bd9Sstevel@tonic-gate 		break;
4217c478bd9Sstevel@tonic-gate 	case SIOCFRSYN :
4227c478bd9Sstevel@tonic-gate 		if (!(mode & FWRITE))
4237c478bd9Sstevel@tonic-gate 			error = EPERM;
4247c478bd9Sstevel@tonic-gate 		else {
425f4b3ec61Sdh 			frsync(IPFSYNC_RESYNC, IPFSYNC_RESYNC, NULL, NULL, ifs);
4267c478bd9Sstevel@tonic-gate 		}
4277c478bd9Sstevel@tonic-gate 		break;
4287c478bd9Sstevel@tonic-gate 	default :
4297c478bd9Sstevel@tonic-gate 		error = EINVAL;
4307c478bd9Sstevel@tonic-gate 		break;
4317c478bd9Sstevel@tonic-gate 	}
4327c478bd9Sstevel@tonic-gate 	SPL_X(s);
4337c478bd9Sstevel@tonic-gate 	return error;
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 
437f4b3ec61Sdh void fr_forgetifp(ifp, ifs)
4387c478bd9Sstevel@tonic-gate void *ifp;
439f4b3ec61Sdh ipf_stack_t *ifs;
4407c478bd9Sstevel@tonic-gate {
4417c478bd9Sstevel@tonic-gate 	register frentry_t *f;
4427c478bd9Sstevel@tonic-gate 
443f4b3ec61Sdh 	WRITE_ENTER(&ifs->ifs_ipf_mutex);
444f4b3ec61Sdh 	for (f = ifs->ifs_ipacct[0][ifs->ifs_fr_active]; (f != NULL);
445f4b3ec61Sdh 	    f = f->fr_next)
4467c478bd9Sstevel@tonic-gate 		if (f->fr_ifa == ifp)
4477c478bd9Sstevel@tonic-gate 			f->fr_ifa = (void *)-1;
448f4b3ec61Sdh 	for (f = ifs->ifs_ipacct[1][ifs->ifs_fr_active]; (f != NULL);
449f4b3ec61Sdh 	    f = f->fr_next)
4507c478bd9Sstevel@tonic-gate 		if (f->fr_ifa == ifp)
4517c478bd9Sstevel@tonic-gate 			f->fr_ifa = (void *)-1;
452f4b3ec61Sdh 	for (f = ifs->ifs_ipfilter[0][ifs->ifs_fr_active]; (f != NULL);
453f4b3ec61Sdh 	    f = f->fr_next)
4547c478bd9Sstevel@tonic-gate 		if (f->fr_ifa == ifp)
4557c478bd9Sstevel@tonic-gate 			f->fr_ifa = (void *)-1;
456f4b3ec61Sdh 	for (f = ifs->ifs_ipfilter[1][ifs->ifs_fr_active]; (f != NULL);
457f4b3ec61Sdh 	    f = f->fr_next)
4587c478bd9Sstevel@tonic-gate 		if (f->fr_ifa == ifp)
4597c478bd9Sstevel@tonic-gate 			f->fr_ifa = (void *)-1;
4607c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
461f4b3ec61Sdh 	for (f = ifs->ifs_ipacct6[0][ifs->ifs_fr_active]; (f != NULL);
462f4b3ec61Sdh 	    f = f->fr_next)
4637c478bd9Sstevel@tonic-gate 		if (f->fr_ifa == ifp)
4647c478bd9Sstevel@tonic-gate 			f->fr_ifa = (void *)-1;
465f4b3ec61Sdh 	for (f = ifs->ifs_ipacct6[1][ifs->ifs_fr_active]; (f != NULL);
466f4b3ec61Sdh 	    f = f->fr_next)
4677c478bd9Sstevel@tonic-gate 		if (f->fr_ifa == ifp)
4687c478bd9Sstevel@tonic-gate 			f->fr_ifa = (void *)-1;
469f4b3ec61Sdh 	for (f = ifs->ifs_ipfilter6[0][ifs->ifs_fr_active]; (f != NULL);
470f4b3ec61Sdh 	    f = f->fr_next)
4717c478bd9Sstevel@tonic-gate 		if (f->fr_ifa == ifp)
4727c478bd9Sstevel@tonic-gate 			f->fr_ifa = (void *)-1;
473f4b3ec61Sdh 	for (f = ifs->ifs_ipfilter6[1][ifs->ifs_fr_active]; (f != NULL);
474f4b3ec61Sdh 	    f = f->fr_next)
4757c478bd9Sstevel@tonic-gate 		if (f->fr_ifa == ifp)
4767c478bd9Sstevel@tonic-gate 			f->fr_ifa = (void *)-1;
4777c478bd9Sstevel@tonic-gate #endif
478f4b3ec61Sdh 	RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
479d6c23f6fSyx 	fr_natifpsync(IPFSYNC_OLDIFP, 4, ifp, NULL, ifs);
480d6c23f6fSyx 	fr_natifpsync(IPFSYNC_OLDIFP, 6, ifp, NULL, ifs);
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 
484f4b3ec61Sdh void fr_resolvedest(fdp, v, ifs)
4857c478bd9Sstevel@tonic-gate frdest_t *fdp;
4867c478bd9Sstevel@tonic-gate int v;
487f4b3ec61Sdh ipf_stack_t *ifs;
4887c478bd9Sstevel@tonic-gate {
4897c478bd9Sstevel@tonic-gate 	fdp->fd_ifp = NULL;
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	if (*fdp->fd_ifname) {
492f4b3ec61Sdh 		fdp->fd_ifp = GETIFP(fdp->fd_ifname, v, ifs);
4937c478bd9Sstevel@tonic-gate 		if (!fdp->fd_ifp)
4947c478bd9Sstevel@tonic-gate 			fdp->fd_ifp = (struct ifnet *)-1;
4957c478bd9Sstevel@tonic-gate 	}
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 
499ab25eeb5Syz #if defined(__sgi) && (IRIX < 60500)
5007c478bd9Sstevel@tonic-gate static int no_output(ifp, m, s)
5017c478bd9Sstevel@tonic-gate #else
5027c478bd9Sstevel@tonic-gate # if TRU64 >= 1885
5037c478bd9Sstevel@tonic-gate static int no_output (ifp, m, s, rt, cp)
5047c478bd9Sstevel@tonic-gate char *cp;
5057c478bd9Sstevel@tonic-gate # else
5067c478bd9Sstevel@tonic-gate static int no_output(ifp, m, s, rt)
5077c478bd9Sstevel@tonic-gate # endif
5087c478bd9Sstevel@tonic-gate struct rtentry *rt;
5097c478bd9Sstevel@tonic-gate #endif
5107c478bd9Sstevel@tonic-gate struct ifnet *ifp;
5117c478bd9Sstevel@tonic-gate struct mbuf *m;
5127c478bd9Sstevel@tonic-gate struct sockaddr *s;
5137c478bd9Sstevel@tonic-gate {
5147c478bd9Sstevel@tonic-gate 	return 0;
5157c478bd9Sstevel@tonic-gate }
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 
518ab25eeb5Syz #if defined(__sgi) && (IRIX < 60500)
5197c478bd9Sstevel@tonic-gate static int write_output(ifp, m, s)
5207c478bd9Sstevel@tonic-gate #else
5217c478bd9Sstevel@tonic-gate # if TRU64 >= 1885
5227c478bd9Sstevel@tonic-gate static int write_output (ifp, m, s, rt, cp)
5237c478bd9Sstevel@tonic-gate char *cp;
5247c478bd9Sstevel@tonic-gate # else
5257c478bd9Sstevel@tonic-gate static int write_output(ifp, m, s, rt)
5267c478bd9Sstevel@tonic-gate # endif
5277c478bd9Sstevel@tonic-gate struct rtentry *rt;
5287c478bd9Sstevel@tonic-gate #endif
5297c478bd9Sstevel@tonic-gate struct ifnet *ifp;
5307c478bd9Sstevel@tonic-gate struct mbuf *m;
5317c478bd9Sstevel@tonic-gate struct sockaddr *s;
5327c478bd9Sstevel@tonic-gate {
5337c478bd9Sstevel@tonic-gate 	char fname[32];
5347c478bd9Sstevel@tonic-gate 	mb_t *mb;
5357c478bd9Sstevel@tonic-gate 	ip_t *ip;
5367c478bd9Sstevel@tonic-gate 	int fd;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	mb = (mb_t *)m;
5397c478bd9Sstevel@tonic-gate 	ip = MTOD(mb, ip_t *);
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate #if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
542ab25eeb5Syz     (defined(OpenBSD) && (OpenBSD >= 199603)) || defined(linux) || \
543ab25eeb5Syz     (defined(__FreeBSD__) && (__FreeBSD_version >= 501113))
5447c478bd9Sstevel@tonic-gate 	sprintf(fname, "/tmp/%s", ifp->if_xname);
5457c478bd9Sstevel@tonic-gate #else
5467c478bd9Sstevel@tonic-gate 	sprintf(fname, "/tmp/%s%d", ifp->if_name, ifp->if_unit);
5477c478bd9Sstevel@tonic-gate #endif
5487c478bd9Sstevel@tonic-gate 	fd = open(fname, O_WRONLY|O_APPEND);
5497c478bd9Sstevel@tonic-gate 	if (fd == -1) {
5507c478bd9Sstevel@tonic-gate 		perror("open");
5517c478bd9Sstevel@tonic-gate 		return -1;
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 	write(fd, (char *)ip, ntohs(ip->ip_len));
5547c478bd9Sstevel@tonic-gate 	close(fd);
5557c478bd9Sstevel@tonic-gate 	return 0;
5567c478bd9Sstevel@tonic-gate }
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 
559ab25eeb5Syz static void fr_setifpaddr(ifp, addr)
560ab25eeb5Syz struct ifnet *ifp;
561ab25eeb5Syz char *addr;
562ab25eeb5Syz {
563ab25eeb5Syz #ifdef __sgi
564ab25eeb5Syz 	struct in_ifaddr *ifa;
565ab25eeb5Syz #else
566ab25eeb5Syz 	struct ifaddr *ifa;
567ab25eeb5Syz #endif
568ab25eeb5Syz 
569ab25eeb5Syz #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
570ab25eeb5Syz 	if (ifp->if_addrlist.tqh_first != NULL)
571ab25eeb5Syz #else
572ab25eeb5Syz # ifdef __sgi
573ab25eeb5Syz 	if (ifp->in_ifaddr != NULL)
574ab25eeb5Syz # else
575ab25eeb5Syz 	if (ifp->if_addrlist != NULL)
576ab25eeb5Syz # endif
577ab25eeb5Syz #endif
578ab25eeb5Syz 		return;
579ab25eeb5Syz 
580ab25eeb5Syz 	ifa = (struct ifaddr *)malloc(sizeof(*ifa));
581ab25eeb5Syz #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
582ab25eeb5Syz 	ifp->if_addrlist.tqh_first = ifa;
583ab25eeb5Syz #else
584ab25eeb5Syz # ifdef __sgi
585ab25eeb5Syz 	ifp->in_ifaddr = ifa;
586ab25eeb5Syz # else
587ab25eeb5Syz 	ifp->if_addrlist = ifa;
588ab25eeb5Syz # endif
589ab25eeb5Syz #endif
590ab25eeb5Syz 
591ab25eeb5Syz 	if (ifa != NULL) {
592ab25eeb5Syz 		struct sockaddr_in *sin;
593ab25eeb5Syz 
594ab25eeb5Syz #ifdef __sgi
595ab25eeb5Syz 		sin = (struct sockaddr_in *)&ifa->ia_addr;
596ab25eeb5Syz #else
597ab25eeb5Syz 		sin = (struct sockaddr_in *)&ifa->ifa_addr;
598ab25eeb5Syz #endif
599ab25eeb5Syz 		sin->sin_addr.s_addr = inet_addr(addr);
600ab25eeb5Syz 		if (sin->sin_addr.s_addr == 0)
601ab25eeb5Syz 			abort();
602ab25eeb5Syz 	}
603ab25eeb5Syz }
604ab25eeb5Syz 
605f4b3ec61Sdh /*ARGSUSED*/
606f4b3ec61Sdh struct ifnet *get_unit(name, v, ifs)
6077c478bd9Sstevel@tonic-gate char *name;
6087c478bd9Sstevel@tonic-gate int v;
609f4b3ec61Sdh ipf_stack_t *ifs;
6107c478bd9Sstevel@tonic-gate {
611ab25eeb5Syz 	struct ifnet *ifp, **ifpp, **old_ifneta;
612ab25eeb5Syz 	char *addr;
6137c478bd9Sstevel@tonic-gate #if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
614ab25eeb5Syz     (defined(OpenBSD) && (OpenBSD >= 199603)) || defined(linux) || \
615ab25eeb5Syz     (defined(__FreeBSD__) && (__FreeBSD_version >= 501113))
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	if (name == NULL)
6187c478bd9Sstevel@tonic-gate 		name = "anon0";
6197c478bd9Sstevel@tonic-gate 
620ab25eeb5Syz 	addr = strchr(name, '=');
621ab25eeb5Syz 	if (addr != NULL)
622ab25eeb5Syz 		*addr++ = '\0';
623ab25eeb5Syz 
624ab25eeb5Syz 	for (ifpp = ifneta; ifpp && (ifp = *ifpp); ifpp++) {
625ab25eeb5Syz 		if (!strcmp(name, ifp->if_xname)) {
626ab25eeb5Syz 			if (addr != NULL)
627ab25eeb5Syz 				fr_setifpaddr(ifp, addr);
6287c478bd9Sstevel@tonic-gate 			return ifp;
629ab25eeb5Syz 		}
6307c478bd9Sstevel@tonic-gate 	}
6317c478bd9Sstevel@tonic-gate #else
6327c478bd9Sstevel@tonic-gate 	char *s, ifname[LIFNAMSIZ+1];
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	if (name == NULL)
6357c478bd9Sstevel@tonic-gate 		name = "anon0";
6367c478bd9Sstevel@tonic-gate 
637ab25eeb5Syz 	addr = strchr(name, '=');
638ab25eeb5Syz 	if (addr != NULL)
639ab25eeb5Syz 		*addr++ = '\0';
640ab25eeb5Syz 
641ab25eeb5Syz 	for (ifpp = ifneta; ifpp && (ifp = *ifpp); ifpp++) {
642381a2a9aSdr 		COPYIFNAME(ifp, ifname, 0);
643ab25eeb5Syz 		if (!strcmp(name, ifname)) {
644ab25eeb5Syz 			if (addr != NULL)
645ab25eeb5Syz 				fr_setifpaddr(ifp, addr);
6467c478bd9Sstevel@tonic-gate 			return ifp;
647ab25eeb5Syz 		}
6487c478bd9Sstevel@tonic-gate 	}
6497c478bd9Sstevel@tonic-gate #endif
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	if (!ifneta) {
6527c478bd9Sstevel@tonic-gate 		ifneta = (struct ifnet **)malloc(sizeof(ifp) * 2);
6537c478bd9Sstevel@tonic-gate 		if (!ifneta)
6547c478bd9Sstevel@tonic-gate 			return NULL;
6557c478bd9Sstevel@tonic-gate 		ifneta[1] = NULL;
6567c478bd9Sstevel@tonic-gate 		ifneta[0] = (struct ifnet *)calloc(1, sizeof(*ifp));
6577c478bd9Sstevel@tonic-gate 		if (!ifneta[0]) {
6587c478bd9Sstevel@tonic-gate 			free(ifneta);
6597c478bd9Sstevel@tonic-gate 			return NULL;
6607c478bd9Sstevel@tonic-gate 		}
6617c478bd9Sstevel@tonic-gate 		nifs = 1;
6627c478bd9Sstevel@tonic-gate 	} else {
6637c478bd9Sstevel@tonic-gate 		old_ifneta = ifneta;
6647c478bd9Sstevel@tonic-gate 		nifs++;
6657c478bd9Sstevel@tonic-gate 		ifneta = (struct ifnet **)realloc(ifneta,
666ab25eeb5Syz 						  (nifs + 1) * sizeof(ifp));
6677c478bd9Sstevel@tonic-gate 		if (!ifneta) {
6687c478bd9Sstevel@tonic-gate 			free(old_ifneta);
6697c478bd9Sstevel@tonic-gate 			nifs = 0;
6707c478bd9Sstevel@tonic-gate 			return NULL;
6717c478bd9Sstevel@tonic-gate 		}
6727c478bd9Sstevel@tonic-gate 		ifneta[nifs] = NULL;
6737c478bd9Sstevel@tonic-gate 		ifneta[nifs - 1] = (struct ifnet *)malloc(sizeof(*ifp));
6747c478bd9Sstevel@tonic-gate 		if (!ifneta[nifs - 1]) {
6757c478bd9Sstevel@tonic-gate 			nifs--;
6767c478bd9Sstevel@tonic-gate 			return NULL;
6777c478bd9Sstevel@tonic-gate 		}
6787c478bd9Sstevel@tonic-gate 	}
6797c478bd9Sstevel@tonic-gate 	ifp = ifneta[nifs - 1];
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate #if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
682ab25eeb5Syz     (defined(OpenBSD) && (OpenBSD >= 199603)) || defined(linux) || \
683ab25eeb5Syz     (defined(__FreeBSD__) && (__FreeBSD_version >= 501113))
684ab25eeb5Syz 	(void) strncpy(ifp->if_xname, name, sizeof(ifp->if_xname));
6857c478bd9Sstevel@tonic-gate #else
686ab25eeb5Syz 	for (s = name; *s && !ISDIGIT(*s); s++)
6877c478bd9Sstevel@tonic-gate 		;
688ab25eeb5Syz 	if (*s && ISDIGIT(*s)) {
6897c478bd9Sstevel@tonic-gate 		ifp->if_unit = atoi(s);
6907c478bd9Sstevel@tonic-gate 		ifp->if_name = (char *)malloc(s - name + 1);
6915e985db5Sschuster 		if (ifp->if_name == NULL) {
6925e985db5Sschuster 			/*
6935e985db5Sschuster 			 * XXX do it more elegantly: free up mem,
6945e985db5Sschuster 			 * return NULL
6955e985db5Sschuster 			 */
6965e985db5Sschuster 			perror("malloc");
6975e985db5Sschuster 			exit(1);
6985e985db5Sschuster 		}
699ab25eeb5Syz 		(void) strncpy(ifp->if_name, name, s - name);
7007c478bd9Sstevel@tonic-gate 		ifp->if_name[s - name] = '\0';
7017c478bd9Sstevel@tonic-gate 	} else {
7027c478bd9Sstevel@tonic-gate 		ifp->if_name = strdup(name);
7037c478bd9Sstevel@tonic-gate 		ifp->if_unit = -1;
7047c478bd9Sstevel@tonic-gate 	}
7057c478bd9Sstevel@tonic-gate #endif
7067c478bd9Sstevel@tonic-gate 	ifp->if_output = no_output;
707ab25eeb5Syz 
708ab25eeb5Syz 	if (addr != NULL) {
709ab25eeb5Syz 		fr_setifpaddr(ifp, addr);
710ab25eeb5Syz 	}
711ab25eeb5Syz 
7127c478bd9Sstevel@tonic-gate 	return ifp;
7137c478bd9Sstevel@tonic-gate }
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate char *get_ifname(ifp)
7177c478bd9Sstevel@tonic-gate struct ifnet *ifp;
7187c478bd9Sstevel@tonic-gate {
7197c478bd9Sstevel@tonic-gate 	static char ifname[LIFNAMSIZ];
7207c478bd9Sstevel@tonic-gate 
721ab25eeb5Syz #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(linux) || \
722ab25eeb5Syz     (defined(__FreeBSD__) && (__FreeBSD_version >= 501113))
7237c478bd9Sstevel@tonic-gate 	sprintf(ifname, "%s", ifp->if_xname);
7247c478bd9Sstevel@tonic-gate #else
7257c478bd9Sstevel@tonic-gate 	sprintf(ifname, "%s%d", ifp->if_name, ifp->if_unit);
7267c478bd9Sstevel@tonic-gate #endif
7277c478bd9Sstevel@tonic-gate 	return ifname;
7287c478bd9Sstevel@tonic-gate }
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate void init_ifp()
7337c478bd9Sstevel@tonic-gate {
734ab25eeb5Syz 	struct ifnet *ifp, **ifpp;
7357c478bd9Sstevel@tonic-gate 	char fname[32];
7367c478bd9Sstevel@tonic-gate 	int fd;
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate #if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
739ab25eeb5Syz     (defined(OpenBSD) && (OpenBSD >= 199603)) || defined(linux) || \
740ab25eeb5Syz     (defined(__FreeBSD__) && (__FreeBSD_version >= 501113))
741ab25eeb5Syz 	for (ifpp = ifneta; ifpp && (ifp = *ifpp); ifpp++) {
7427c478bd9Sstevel@tonic-gate 		ifp->if_output = write_output;
7437c478bd9Sstevel@tonic-gate 		sprintf(fname, "/tmp/%s", ifp->if_xname);
7447c478bd9Sstevel@tonic-gate 		fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
7457c478bd9Sstevel@tonic-gate 		if (fd == -1)
7467c478bd9Sstevel@tonic-gate 			perror("open");
7477c478bd9Sstevel@tonic-gate 		else
7487c478bd9Sstevel@tonic-gate 			close(fd);
7497c478bd9Sstevel@tonic-gate 	}
7507c478bd9Sstevel@tonic-gate #else
7517c478bd9Sstevel@tonic-gate 
752ab25eeb5Syz 	for (ifpp = ifneta; ifpp && (ifp = *ifpp); ifpp++) {
7537c478bd9Sstevel@tonic-gate 		ifp->if_output = write_output;
7547c478bd9Sstevel@tonic-gate 		sprintf(fname, "/tmp/%s%d", ifp->if_name, ifp->if_unit);
7557c478bd9Sstevel@tonic-gate 		fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
7567c478bd9Sstevel@tonic-gate 		if (fd == -1)
7577c478bd9Sstevel@tonic-gate 			perror("open");
7587c478bd9Sstevel@tonic-gate 		else
7597c478bd9Sstevel@tonic-gate 			close(fd);
7607c478bd9Sstevel@tonic-gate 	}
7617c478bd9Sstevel@tonic-gate #endif
7627c478bd9Sstevel@tonic-gate }
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate int fr_fastroute(m, mpp, fin, fdp)
7667c478bd9Sstevel@tonic-gate mb_t *m, **mpp;
7677c478bd9Sstevel@tonic-gate fr_info_t *fin;
7687c478bd9Sstevel@tonic-gate frdest_t *fdp;
7697c478bd9Sstevel@tonic-gate {
7707c478bd9Sstevel@tonic-gate 	struct ifnet *ifp = fdp->fd_ifp;
7717c478bd9Sstevel@tonic-gate 	ip_t *ip = fin->fin_ip;
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	if (!ifp)
7747c478bd9Sstevel@tonic-gate 		return 0;	/* no routing table out here */
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	ip->ip_len = htons((u_short)ip->ip_len);
7777c478bd9Sstevel@tonic-gate 	ip->ip_off = htons((u_short)(ip->ip_off | IP_MF));
7787c478bd9Sstevel@tonic-gate 	ip->ip_sum = 0;
779ab25eeb5Syz #if defined(__sgi) && (IRIX < 60500)
7807c478bd9Sstevel@tonic-gate 	(*ifp->if_output)(ifp, (void *)ip, NULL);
7817c478bd9Sstevel@tonic-gate # if TRU64 >= 1885
7827c478bd9Sstevel@tonic-gate 	(*ifp->if_output)(ifp, (void *)m, NULL, 0, 0);
7837c478bd9Sstevel@tonic-gate # else
7847c478bd9Sstevel@tonic-gate 	(*ifp->if_output)(ifp, (void *)m, NULL, 0);
7857c478bd9Sstevel@tonic-gate # endif
7867c478bd9Sstevel@tonic-gate #endif
7877c478bd9Sstevel@tonic-gate 	return 0;
7887c478bd9Sstevel@tonic-gate }
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate int fr_send_reset(fin)
7927c478bd9Sstevel@tonic-gate fr_info_t *fin;
7937c478bd9Sstevel@tonic-gate {
7947c478bd9Sstevel@tonic-gate 	verbose("- TCP RST sent\n");
7957c478bd9Sstevel@tonic-gate 	return 0;
7967c478bd9Sstevel@tonic-gate }
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate int fr_send_icmp_err(type, fin, dst)
8007c478bd9Sstevel@tonic-gate int type;
8017c478bd9Sstevel@tonic-gate fr_info_t *fin;
8027c478bd9Sstevel@tonic-gate int dst;
8037c478bd9Sstevel@tonic-gate {
804ab25eeb5Syz 	verbose("- ICMP unreachable sent\n");
8057c478bd9Sstevel@tonic-gate 	return 0;
8067c478bd9Sstevel@tonic-gate }
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 
809f4b3ec61Sdh void frsync(command, version, nic, data, ifs)
810381a2a9aSdr int command, version;
811381a2a9aSdr void *nic;
812381a2a9aSdr char *data;
813f4b3ec61Sdh ipf_stack_t *ifs;
8147c478bd9Sstevel@tonic-gate {
8157c478bd9Sstevel@tonic-gate 	return;
8167c478bd9Sstevel@tonic-gate }
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate void m_freem(m)
8207c478bd9Sstevel@tonic-gate mb_t *m;
8217c478bd9Sstevel@tonic-gate {
8227c478bd9Sstevel@tonic-gate 	return;
8237c478bd9Sstevel@tonic-gate }
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate void m_copydata(m, off, len, cp)
8277c478bd9Sstevel@tonic-gate mb_t *m;
8287c478bd9Sstevel@tonic-gate int off, len;
8297c478bd9Sstevel@tonic-gate caddr_t cp;
8307c478bd9Sstevel@tonic-gate {
8317c478bd9Sstevel@tonic-gate 	bcopy((char *)m + off, cp, len);
8327c478bd9Sstevel@tonic-gate }
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate int ipfuiomove(buf, len, rwflag, uio)
8367c478bd9Sstevel@tonic-gate caddr_t buf;
8377c478bd9Sstevel@tonic-gate int len, rwflag;
8387c478bd9Sstevel@tonic-gate struct uio *uio;
8397c478bd9Sstevel@tonic-gate {
8407c478bd9Sstevel@tonic-gate 	int left, ioc, num, offset;
8417c478bd9Sstevel@tonic-gate 	struct iovec *io;
8427c478bd9Sstevel@tonic-gate 	char *start;
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	if (rwflag == UIO_READ) {
8457c478bd9Sstevel@tonic-gate 		left = len;
8467c478bd9Sstevel@tonic-gate 		ioc = 0;
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 		offset = uio->uio_offset;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 		while ((left > 0) && (ioc < uio->uio_iovcnt)) {
8517c478bd9Sstevel@tonic-gate 			io = uio->uio_iov + ioc;
8527c478bd9Sstevel@tonic-gate 			num = io->iov_len;
8537c478bd9Sstevel@tonic-gate 			if (num > left)
8547c478bd9Sstevel@tonic-gate 				num = left;
8557c478bd9Sstevel@tonic-gate 			start = (char *)io->iov_base + offset;
8567c478bd9Sstevel@tonic-gate 			if (start > (char *)io->iov_base + io->iov_len) {
8577c478bd9Sstevel@tonic-gate 				offset -= io->iov_len;
8587c478bd9Sstevel@tonic-gate 				ioc++;
8597c478bd9Sstevel@tonic-gate 				continue;
8607c478bd9Sstevel@tonic-gate 			}
8617c478bd9Sstevel@tonic-gate 			bcopy(buf, start, num);
8627c478bd9Sstevel@tonic-gate 			uio->uio_resid -= num;
8637c478bd9Sstevel@tonic-gate 			uio->uio_offset += num;
8647c478bd9Sstevel@tonic-gate 			left -= num;
8657c478bd9Sstevel@tonic-gate 			if (left > 0)
8667c478bd9Sstevel@tonic-gate 				ioc++;
8677c478bd9Sstevel@tonic-gate 		}
8687c478bd9Sstevel@tonic-gate 		if (left > 0)
8697c478bd9Sstevel@tonic-gate 			return EFAULT;
8707c478bd9Sstevel@tonic-gate 	}
8717c478bd9Sstevel@tonic-gate 	return 0;
8727c478bd9Sstevel@tonic-gate }
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate u_32_t fr_newisn(fin)
8767c478bd9Sstevel@tonic-gate fr_info_t *fin;
8777c478bd9Sstevel@tonic-gate {
8787c478bd9Sstevel@tonic-gate 	static int iss_seq_off = 0;
8797c478bd9Sstevel@tonic-gate 	u_char hash[16];
8807c478bd9Sstevel@tonic-gate 	u_32_t newiss;
8817c478bd9Sstevel@tonic-gate 	MD5_CTX ctx;
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	/*
8847c478bd9Sstevel@tonic-gate 	 * Compute the base value of the ISS.  It is a hash
8857c478bd9Sstevel@tonic-gate 	 * of (saddr, sport, daddr, dport, secret).
8867c478bd9Sstevel@tonic-gate 	 */
8877c478bd9Sstevel@tonic-gate 	MD5Init(&ctx);
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	MD5Update(&ctx, (u_char *) &fin->fin_fi.fi_src,
8907c478bd9Sstevel@tonic-gate 		  sizeof(fin->fin_fi.fi_src));
8917c478bd9Sstevel@tonic-gate 	MD5Update(&ctx, (u_char *) &fin->fin_fi.fi_dst,
8927c478bd9Sstevel@tonic-gate 		  sizeof(fin->fin_fi.fi_dst));
8937c478bd9Sstevel@tonic-gate 	MD5Update(&ctx, (u_char *) &fin->fin_dat, sizeof(fin->fin_dat));
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 	/* MD5Update(&ctx, ipf_iss_secret, sizeof(ipf_iss_secret)); */
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 	MD5Final(hash, &ctx);
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	memcpy(&newiss, hash, sizeof(newiss));
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	/*
9027c478bd9Sstevel@tonic-gate 	 * Now increment our "timer", and add it in to
9037c478bd9Sstevel@tonic-gate 	 * the computed value.
9047c478bd9Sstevel@tonic-gate 	 *
9057c478bd9Sstevel@tonic-gate 	 * XXX Use `addin'?
9067c478bd9Sstevel@tonic-gate 	 * XXX TCP_ISSINCR too large to use?
9077c478bd9Sstevel@tonic-gate 	 */
9087c478bd9Sstevel@tonic-gate 	iss_seq_off += 0x00010000;
9097c478bd9Sstevel@tonic-gate 	newiss += iss_seq_off;
9107c478bd9Sstevel@tonic-gate 	return newiss;
9117c478bd9Sstevel@tonic-gate }
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
9157c478bd9Sstevel@tonic-gate /* Function:    fr_nextipid                                                 */
9167c478bd9Sstevel@tonic-gate /* Returns:     int - 0 == success, -1 == error (packet should be droppped) */
9177c478bd9Sstevel@tonic-gate /* Parameters:  fin(I) - pointer to packet information                      */
9187c478bd9Sstevel@tonic-gate /*                                                                          */
9197c478bd9Sstevel@tonic-gate /* Returns the next IPv4 ID to use for this packet.                         */
9207c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
9217c478bd9Sstevel@tonic-gate INLINE u_short fr_nextipid(fin)
9227c478bd9Sstevel@tonic-gate fr_info_t *fin;
9237c478bd9Sstevel@tonic-gate {
9247c478bd9Sstevel@tonic-gate 	static u_short ipid = 0;
9257c478bd9Sstevel@tonic-gate 	u_short id;
926f4b3ec61Sdh 	ipf_stack_t *ifs = fin->fin_ifs;
9277c478bd9Sstevel@tonic-gate 
928f4b3ec61Sdh 	MUTEX_ENTER(&ifs->ifs_ipf_rw);
9297c478bd9Sstevel@tonic-gate 	id = ipid++;
930f4b3ec61Sdh 	MUTEX_EXIT(&ifs->ifs_ipf_rw);
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 	return id;
9337c478bd9Sstevel@tonic-gate }
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate INLINE void fr_checkv4sum(fin)
9377c478bd9Sstevel@tonic-gate fr_info_t *fin;
9387c478bd9Sstevel@tonic-gate {
9397c478bd9Sstevel@tonic-gate 	if (fr_checkl4sum(fin) == -1)
9407c478bd9Sstevel@tonic-gate 		fin->fin_flx |= FI_BAD;
9417c478bd9Sstevel@tonic-gate }
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
9457c478bd9Sstevel@tonic-gate INLINE void fr_checkv6sum(fin)
9467c478bd9Sstevel@tonic-gate fr_info_t *fin;
9477c478bd9Sstevel@tonic-gate {
9487c478bd9Sstevel@tonic-gate 	if (fr_checkl4sum(fin) == -1)
9497c478bd9Sstevel@tonic-gate 		fin->fin_flx |= FI_BAD;
9507c478bd9Sstevel@tonic-gate }
9517c478bd9Sstevel@tonic-gate #endif
952ab25eeb5Syz 
953ab25eeb5Syz 
954ab25eeb5Syz /*
955ab25eeb5Syz  * See above for description, except that all addressing is in user space.
956ab25eeb5Syz  */
957ab25eeb5Syz int copyoutptr(src, dst, size)
958ab25eeb5Syz void *src, *dst;
959ab25eeb5Syz size_t size;
960ab25eeb5Syz {
961ab25eeb5Syz 	caddr_t ca;
962ab25eeb5Syz 
963ab25eeb5Syz 	bcopy(dst, (char *)&ca, sizeof(ca));
964ab25eeb5Syz 	bcopy(src, ca, size);
965ab25eeb5Syz 	return 0;
966ab25eeb5Syz }
967ab25eeb5Syz 
968ab25eeb5Syz 
969ab25eeb5Syz /*
970ab25eeb5Syz  * See above for description, except that all addressing is in user space.
971ab25eeb5Syz  */
972ab25eeb5Syz int copyinptr(src, dst, size)
973ab25eeb5Syz void *src, *dst;
974ab25eeb5Syz size_t size;
975ab25eeb5Syz {
976ab25eeb5Syz 	caddr_t ca;
977ab25eeb5Syz 
978ab25eeb5Syz 	bcopy(src, (char *)&ca, sizeof(ca));
979ab25eeb5Syz 	bcopy(ca, dst, size);
980ab25eeb5Syz 	return 0;
981ab25eeb5Syz }
982ab25eeb5Syz 
983ab25eeb5Syz 
984ab25eeb5Syz /*
985ab25eeb5Syz  * return the first IP Address associated with an interface
986ab25eeb5Syz  */
987f4b3ec61Sdh int fr_ifpaddr(v, atype, ifptr, inp, inpmask, ifs)
988ab25eeb5Syz int v, atype;
989ab25eeb5Syz void *ifptr;
990ab25eeb5Syz struct in_addr *inp, *inpmask;
991f4b3ec61Sdh ipf_stack_t *ifs;
992ab25eeb5Syz {
993ab25eeb5Syz 	struct ifnet *ifp = ifptr;
994ab25eeb5Syz #ifdef __sgi
995ab25eeb5Syz 	struct in_ifaddr *ifa;
996ab25eeb5Syz #else
997ab25eeb5Syz 	struct ifaddr *ifa;
998ab25eeb5Syz #endif
999ab25eeb5Syz 
1000ab25eeb5Syz #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
1001ab25eeb5Syz 	ifa = ifp->if_addrlist.tqh_first;
1002ab25eeb5Syz #else
1003ab25eeb5Syz # ifdef __sgi
1004ab25eeb5Syz 	ifa = (struct in_ifaddr *)ifp->in_ifaddr;
1005ab25eeb5Syz # else
1006ab25eeb5Syz 	ifa = ifp->if_addrlist;
1007ab25eeb5Syz # endif
1008ab25eeb5Syz #endif
1009ab25eeb5Syz 	if (ifa != NULL) {
1010ab25eeb5Syz 		struct sockaddr_in *sin, mask;
1011ab25eeb5Syz 
1012ab25eeb5Syz 		mask.sin_addr.s_addr = 0xffffffff;
1013ab25eeb5Syz 
1014ab25eeb5Syz #ifdef __sgi
1015ab25eeb5Syz 		sin = (struct sockaddr_in *)&ifa->ia_addr;
1016ab25eeb5Syz #else
1017ab25eeb5Syz 		sin = (struct sockaddr_in *)&ifa->ifa_addr;
1018ab25eeb5Syz #endif
1019ab25eeb5Syz 
1020ab25eeb5Syz 		return fr_ifpfillv4addr(atype, sin, &mask, inp, inpmask);
1021ab25eeb5Syz 	}
1022ab25eeb5Syz 	return 0;
1023ab25eeb5Syz }
1024ab073b32Sdr 
1025ab073b32Sdr 
1026ab073b32Sdr /*
1027ab073b32Sdr  * This function is not meant to be random, rather just produce a
1028ab073b32Sdr  * sequence of numbers that isn't linear to show "randomness".
1029ab073b32Sdr  */
1030ab073b32Sdr u_32_t ipf_random()
1031ab073b32Sdr {
1032ab073b32Sdr 	static u_int last = 0xa5a5a5a5;
1033ab073b32Sdr 	static int calls = 0;
1034ab073b32Sdr 	int number;
1035ab073b32Sdr 
1036ab073b32Sdr 	calls++;
1037ab073b32Sdr 
1038ab073b32Sdr 	/*
1039ab073b32Sdr 	 * These are deliberately chosen to ensure that there is some
1040ab073b32Sdr 	 * attempt to test whether the output covers the range in test n18.
1041ab073b32Sdr 	 */
1042ab073b32Sdr 	switch (calls)
1043ab073b32Sdr 	{
1044ab073b32Sdr 	case 1 :
1045ab073b32Sdr 		number = 0;
1046ab073b32Sdr 		break;
1047ab073b32Sdr 	case 2 :
1048ab073b32Sdr 		number = 4;
1049ab073b32Sdr 		break;
1050ab073b32Sdr 	case 3 :
1051ab073b32Sdr 		number = 3999;
1052ab073b32Sdr 		break;
1053ab073b32Sdr 	case 4 :
1054ab073b32Sdr 		number = 4000;
1055ab073b32Sdr 		break;
1056ab073b32Sdr 	case 5 :
1057ab073b32Sdr 		number = 48999;
1058ab073b32Sdr 		break;
1059ab073b32Sdr 	case 6 :
1060ab073b32Sdr 		number = 49000;
1061ab073b32Sdr 		break;
1062ab073b32Sdr 	default :
1063ab073b32Sdr 		number = last;
1064ab073b32Sdr 		last *= calls;
1065ab073b32Sdr 		last++;
1066ab073b32Sdr 		number ^= last;
1067ab073b32Sdr 		break;
1068ab073b32Sdr 	}
1069ab073b32Sdr 	return number;
1070ab073b32Sdr }
1071