if_mib.c revision 50477
117352Swollman/*
217352Swollman * Copyright 1996 Massachusetts Institute of Technology
317352Swollman *
417352Swollman * Permission to use, copy, modify, and distribute this software and
517352Swollman * its documentation for any purpose and without fee is hereby
617352Swollman * granted, provided that both the above copyright notice and this
717352Swollman * permission notice appear in all copies, that both the above
817352Swollman * copyright notice and this permission notice appear in all
917352Swollman * supporting documentation, and that the name of M.I.T. not be used
1017352Swollman * in advertising or publicity pertaining to distribution of the
1117352Swollman * software without specific, written prior permission.  M.I.T. makes
1217352Swollman * no representations about the suitability of this software for any
1317352Swollman * purpose.  It is provided "as is" without express or implied
1417352Swollman * warranty.
1517352Swollman *
1617352Swollman * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
1717352Swollman * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
1817352Swollman * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1917352Swollman * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
2017352Swollman * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2117352Swollman * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2217352Swollman * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
2317352Swollman * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
2417352Swollman * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2517352Swollman * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
2617352Swollman * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2717352Swollman * SUCH DAMAGE.
2817352Swollman *
2950477Speter * $FreeBSD: head/sys/net/if_mib.c 50477 1999-08-28 01:08:13Z peter $
3017352Swollman */
3117352Swollman
3217352Swollman#include <sys/param.h>
3317352Swollman#include <sys/systm.h>
3417352Swollman#include <sys/kernel.h>
3518892Sbde#include <sys/socket.h>
3617352Swollman#include <sys/sysctl.h>
3717352Swollman
3817352Swollman#include <net/if.h>
3917352Swollman#include <net/if_mib.h>
4017352Swollman
4117352Swollman/*
4217352Swollman * A sysctl(3) MIB for generic interface information.  This information
4317352Swollman * is exported in the net.link.generic branch, which has the following
4417352Swollman * structure:
4517352Swollman *
4617352Swollman * net.link.generic	.system			- system-wide control variables
4717352Swollman *						  and statistics (node)
4817352Swollman *			.ifdata.<ifindex>.general
4917352Swollman *						- what's in `struct ifdata'
5017352Swollman *						  plus some other info
5117352Swollman *			.ifdata.<ifindex>.linkspecific
5217352Swollman *						- a link-type-specific data
5317352Swollman *						  structure (as might be used
5417352Swollman *						  by an SNMP agent
5517352Swollman *
5617352Swollman * Perhaps someday we will make addresses accessible via this interface
5717352Swollman * as well (then there will be four such...).  The reason that the
5817352Swollman * index comes before the last element in the name is because it
5917352Swollman * seems more orthogonal that way, particularly with the possibility
6017352Swollman * of other per-interface data living down here as well (e.g., integrated
6117352Swollman * services stuff).
6217352Swollman */
6317352Swollman
6444078SdfrSYSCTL_DECL(_net_link_generic);
6517352SwollmanSYSCTL_NODE(_net_link_generic, IFMIB_SYSTEM, system, CTLFLAG_RW, 0,
6617352Swollman	    "Variables global to all interfaces");
6717352SwollmanSYSCTL_INT(_net_link_generic_system, IFMIB_IFCOUNT, ifcount, CTLFLAG_RD,
6817352Swollman	   &if_index, 0, "Number of configured interfaces");
6917352Swollman
7017352Swollmanstatic int
7117352Swollmansysctl_ifdata SYSCTL_HANDLER_ARGS /* XXX bad syntax! */
7217352Swollman{
7317352Swollman	int *name = (int *)arg1;
7417352Swollman	int error, ifnlen;
7517352Swollman	u_int namelen = arg2;
7617352Swollman	struct ifnet *ifp;
7717352Swollman	char workbuf[64];
7817352Swollman	struct ifmibdata ifmd;
7917352Swollman
8017352Swollman	if (namelen != 2)
8117352Swollman		return EINVAL;
8217352Swollman
8317352Swollman	if (name[0] <= 0 || name[0] > if_index)
8417352Swollman		return ENOENT;
8517352Swollman
8617352Swollman	ifp = ifnet_addrs[name[0] - 1]->ifa_ifp;
8717352Swollman
8817352Swollman	switch(name[1]) {
8917352Swollman	default:
9017352Swollman		return ENOENT;
9117352Swollman
9217352Swollman	case IFDATA_GENERAL:
9341514Sarchie		ifnlen = snprintf(workbuf, sizeof(workbuf),
9441514Sarchie		    "%s%d", ifp->if_name, ifp->if_unit);
9517352Swollman		if(ifnlen + 1 > sizeof ifmd.ifmd_name) {
9617352Swollman			return ENAMETOOLONG;
9717352Swollman		} else {
9817352Swollman			strcpy(ifmd.ifmd_name, workbuf);
9917352Swollman		}
10017352Swollman
10117352Swollman#define COPY(fld) ifmd.ifmd_##fld = ifp->if_##fld
10217352Swollman		COPY(pcount);
10317352Swollman		COPY(flags);
10417352Swollman		COPY(data);
10517352Swollman#undef COPY
10617352Swollman		ifmd.ifmd_snd_len = ifp->if_snd.ifq_len;
10717352Swollman		ifmd.ifmd_snd_maxlen = ifp->if_snd.ifq_maxlen;
10817352Swollman		ifmd.ifmd_snd_drops = ifp->if_snd.ifq_drops;
10917352Swollman
11017352Swollman		error = SYSCTL_OUT(req, &ifmd, sizeof ifmd);
11117352Swollman		if (error || !req->newptr)
11217352Swollman			return error;
11317352Swollman
11417352Swollman		error = SYSCTL_IN(req, &ifmd, sizeof ifmd);
11517352Swollman		if (error)
11617352Swollman			return error;
11717352Swollman
11817352Swollman#define DONTCOPY(fld) ifmd.ifmd_data.ifi_##fld = ifp->if_data.ifi_##fld
11917352Swollman		DONTCOPY(type);
12017352Swollman		DONTCOPY(physical);
12117352Swollman		DONTCOPY(addrlen);
12217352Swollman		DONTCOPY(hdrlen);
12317352Swollman		DONTCOPY(mtu);
12417352Swollman		DONTCOPY(metric);
12517352Swollman		DONTCOPY(baudrate);
12617352Swollman#undef DONTCOPY
12717352Swollman#define COPY(fld) ifp->if_##fld = ifmd.ifmd_##fld
12817352Swollman		COPY(data);
12917352Swollman		ifp->if_snd.ifq_maxlen = ifmd.ifmd_snd_maxlen;
13017352Swollman		ifp->if_snd.ifq_drops = ifmd.ifmd_snd_drops;
13117352Swollman#undef COPY
13217352Swollman		break;
13317352Swollman
13417352Swollman	case IFDATA_LINKSPECIFIC:
13517352Swollman		error = SYSCTL_OUT(req, ifp->if_linkmib, ifp->if_linkmiblen);
13617352Swollman		if (error || !req->newptr)
13717352Swollman			return error;
13817352Swollman
13917352Swollman		error = SYSCTL_IN(req, ifp->if_linkmib, ifp->if_linkmiblen);
14017352Swollman		if (error)
14117352Swollman			return error;
14217352Swollman
14317352Swollman	}
14417352Swollman	return 0;
14517352Swollman}
14617352Swollman
14717352SwollmanSYSCTL_NODE(_net_link_generic, IFMIB_IFDATA, ifdata, CTLFLAG_RW,
14817352Swollman	    sysctl_ifdata, "Interface table");
14917352Swollman
150