1139823Simp/*-
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$
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>
40185571Sbz#include <net/vnet.h>
4117352Swollman
4217352Swollman/*
4317352Swollman * A sysctl(3) MIB for generic interface information.  This information
4417352Swollman * is exported in the net.link.generic branch, which has the following
4517352Swollman * structure:
4617352Swollman *
4717352Swollman * net.link.generic	.system			- system-wide control variables
4817352Swollman *						  and statistics (node)
4917352Swollman *			.ifdata.<ifindex>.general
5017352Swollman *						- what's in `struct ifdata'
5117352Swollman *						  plus some other info
5217352Swollman *			.ifdata.<ifindex>.linkspecific
5317352Swollman *						- a link-type-specific data
5417352Swollman *						  structure (as might be used
5517352Swollman *						  by an SNMP agent
5617352Swollman *
5717352Swollman * Perhaps someday we will make addresses accessible via this interface
5817352Swollman * as well (then there will be four such...).  The reason that the
5917352Swollman * index comes before the last element in the name is because it
6017352Swollman * seems more orthogonal that way, particularly with the possibility
6117352Swollman * of other per-interface data living down here as well (e.g., integrated
6217352Swollman * services stuff).
6317352Swollman */
6417352Swollman
6544078SdfrSYSCTL_DECL(_net_link_generic);
66248085Smariusstatic SYSCTL_NODE(_net_link_generic, IFMIB_SYSTEM, system, CTLFLAG_RW, 0,
6717352Swollman	    "Variables global to all interfaces");
6817352Swollman
69195699SrwatsonSYSCTL_VNET_INT(_net_link_generic_system, IFMIB_IFCOUNT, ifcount, CTLFLAG_RD,
70195699Srwatson	    &VNET_NAME(if_index), 0,
71183550Szec	     "Number of configured interfaces");
72183550Szec
7317352Swollmanstatic int
7462573Sphksysctl_ifdata(SYSCTL_HANDLER_ARGS) /* XXX bad syntax! */
7517352Swollman{
7617352Swollman	int *name = (int *)arg1;
77121816Sbrooks	int error;
7817352Swollman	u_int namelen = arg2;
7917352Swollman	struct ifnet *ifp;
8017352Swollman	struct ifmibdata ifmd;
81154023Sharti	size_t dlen;
82154023Sharti	char *dbuf;
8317352Swollman
8417352Swollman	if (namelen != 2)
8517352Swollman		return EINVAL;
86191367Srwatson	if (name[0] <= 0)
87191367Srwatson		return (ENOENT);
88191367Srwatson	ifp = ifnet_byindex_ref(name[0]);
89191367Srwatson	if (ifp == NULL)
90191367Srwatson		return (ENOENT);
9117352Swollman
9217352Swollman	switch(name[1]) {
9317352Swollman	default:
94191367Srwatson		error = ENOENT;
95191367Srwatson		goto out;
9617352Swollman
9717352Swollman	case IFDATA_GENERAL:
98145953Scperciva		bzero(&ifmd, sizeof(ifmd));
99121816Sbrooks		strlcpy(ifmd.ifmd_name, ifp->if_xname, sizeof(ifmd.ifmd_name));
10017352Swollman
10117352Swollman#define COPY(fld) ifmd.ifmd_##fld = ifp->if_##fld
10217352Swollman		COPY(pcount);
10317352Swollman		COPY(data);
10417352Swollman#undef COPY
105151569Scsjp		ifmd.ifmd_flags = ifp->if_flags | ifp->if_drv_flags;
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)
112191367Srwatson			goto out;
11317352Swollman
11417352Swollman		error = SYSCTL_IN(req, &ifmd, sizeof ifmd);
11517352Swollman		if (error)
116191367Srwatson			goto out;
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)
137191367Srwatson			goto out;
13817352Swollman
13917352Swollman		error = SYSCTL_IN(req, ifp->if_linkmib, ifp->if_linkmiblen);
14017352Swollman		if (error)
141191367Srwatson			goto out;
142182904Sbms		break;
143154023Sharti
144154023Sharti	case IFDATA_DRIVERNAME:
145154023Sharti		/* 20 is enough for 64bit ints */
146154023Sharti		dlen = strlen(ifp->if_dname) + 20 + 1;
147191367Srwatson		if ((dbuf = malloc(dlen, M_TEMP, M_NOWAIT)) == NULL) {
148191367Srwatson			error = ENOMEM;
149191367Srwatson			goto out;
150191367Srwatson		}
151154023Sharti		if (ifp->if_dunit == IF_DUNIT_NONE)
152154023Sharti			strcpy(dbuf, ifp->if_dname);
153154023Sharti		else
154154023Sharti			sprintf(dbuf, "%s%d", ifp->if_dname, ifp->if_dunit);
155154023Sharti
156154023Sharti		error = SYSCTL_OUT(req, dbuf, strlen(dbuf) + 1);
157154023Sharti		if (error == 0 && req->newptr != NULL)
158154023Sharti			error = EPERM;
159154023Sharti		free(dbuf, M_TEMP);
160191367Srwatson		goto out;
16117352Swollman	}
162191367Srwatsonout:
163191367Srwatson	if_rele(ifp);
164191367Srwatson	return error;
16517352Swollman}
16617352Swollman
167248085Smariusstatic SYSCTL_NODE(_net_link_generic, IFMIB_IFDATA, ifdata, CTLFLAG_RW,
16817352Swollman	    sysctl_ifdata, "Interface table");
16917352Swollman
170