xref: /illumos-gate/usr/src/cmd/ipf/lib/common/getifname.c (revision af5f29dd)
1ab25eeb5Syz /*
2ab25eeb5Syz  * Copyright (C) 2003 by Darren Reed.
3ab25eeb5Syz  *
4ab25eeb5Syz  * See the IPFILTER.LICENCE file for details on licencing.
5ab25eeb5Syz  *
6f4b3ec61Sdh  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
7ab25eeb5Syz  * Use is subject to license terms.
8ab25eeb5Syz  */
9ab25eeb5Syz 
107c478bd9Sstevel@tonic-gate #include "ipf.h"
117c478bd9Sstevel@tonic-gate #include "kmem.h"
127c478bd9Sstevel@tonic-gate 
137c478bd9Sstevel@tonic-gate /*
147c478bd9Sstevel@tonic-gate  * Given a pointer to an interface in the kernel, return a pointer to a
157c478bd9Sstevel@tonic-gate  * string which is the interface name.
16381a2a9aSdr  *
17381a2a9aSdr  * The same code is used to run in two different environments: in ipfstat
18381a2a9aSdr  * and in ipftest.  In ipftest, kmemcpy is wrapper for bcopy but in ipfstat,
19381a2a9aSdr  * it is used as an interface to libkvm.
207c478bd9Sstevel@tonic-gate  */
getifname(ptr)217c478bd9Sstevel@tonic-gate char *getifname(ptr)
227c478bd9Sstevel@tonic-gate struct ifnet *ptr;
237c478bd9Sstevel@tonic-gate {
24*af5f29ddSToomas Soome # ifdef SOLARIS
25ab25eeb5Syz #  include <sys/mutex.h>
26ab25eeb5Syz #  include <sys/condvar.h>
27ab25eeb5Syz # endif
28ab25eeb5Syz # ifdef __hpux
29ab25eeb5Syz #  include "compat.h"
30ab25eeb5Syz # endif
317c478bd9Sstevel@tonic-gate # if defined(NetBSD) && (NetBSD >= 199905) && (NetBSD < 1991011) || \
32ab25eeb5Syz     defined(__OpenBSD__) || \
33ab25eeb5Syz     (defined(__FreeBSD__) && (__FreeBSD_version >= 501113))
347c478bd9Sstevel@tonic-gate #else
357c478bd9Sstevel@tonic-gate 	char buf[32];
367c478bd9Sstevel@tonic-gate 	int len;
377c478bd9Sstevel@tonic-gate # endif
387c478bd9Sstevel@tonic-gate 	struct ifnet netif;
39f4b3ec61Sdh #define SOLARIS_PFHOOKS	1
40381a2a9aSdr # ifdef SOLARIS_PFHOOKS
41381a2a9aSdr 	if ((opts & OPT_DONOTHING) == 0)
42381a2a9aSdr 		return "@";
43381a2a9aSdr # endif
44381a2a9aSdr 
457c478bd9Sstevel@tonic-gate 	if ((void *)ptr == (void *)-1)
467c478bd9Sstevel@tonic-gate 		return "!";
477c478bd9Sstevel@tonic-gate 	if (ptr == NULL)
487c478bd9Sstevel@tonic-gate 		return "-";
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	if (kmemcpy((char *)&netif, (u_long)ptr, sizeof(netif)) == -1)
517c478bd9Sstevel@tonic-gate 		return "X";
527c478bd9Sstevel@tonic-gate # if defined(NetBSD) && (NetBSD >= 199905) && (NetBSD < 1991011) || \
53ab25eeb5Syz     defined(__OpenBSD__) || defined(linux) || \
54ab25eeb5Syz     (defined(__FreeBSD__) && (__FreeBSD_version >= 501113))
557c478bd9Sstevel@tonic-gate 	return strdup(netif.if_xname);
567c478bd9Sstevel@tonic-gate # else
577c478bd9Sstevel@tonic-gate 	if (kstrncpy(buf, (u_long)netif.if_name, sizeof(buf)) == -1)
587c478bd9Sstevel@tonic-gate 		return "X";
597c478bd9Sstevel@tonic-gate 	if (netif.if_unit < 10)
607c478bd9Sstevel@tonic-gate 		len = 2;
617c478bd9Sstevel@tonic-gate 	else if (netif.if_unit < 1000)
627c478bd9Sstevel@tonic-gate 		len = 3;
637c478bd9Sstevel@tonic-gate 	else if (netif.if_unit < 10000)
647c478bd9Sstevel@tonic-gate 		len = 4;
657c478bd9Sstevel@tonic-gate 	else
667c478bd9Sstevel@tonic-gate 		len = 5;
677c478bd9Sstevel@tonic-gate 	buf[sizeof(buf) - len] = '\0';
687c478bd9Sstevel@tonic-gate 	sprintf(buf + strlen(buf), "%d", netif.if_unit % 10000);
697c478bd9Sstevel@tonic-gate 	return strdup(buf);
707c478bd9Sstevel@tonic-gate # endif
717c478bd9Sstevel@tonic-gate }
72