1139743Simp/*-
243412Snewton * Copyright (c) 1998 Mark Newton
343412Snewton * Copyright (c) 1995 Christos Zoulas
443412Snewton * All rights reserved.
543412Snewton *
643412Snewton * Redistribution and use in source and binary forms, with or without
743412Snewton * modification, are permitted provided that the following conditions
843412Snewton * are met:
943412Snewton * 1. Redistributions of source code must retain the above copyright
1043412Snewton *    notice, this list of conditions and the following disclaimer.
1143412Snewton * 2. Redistributions in binary form must reproduce the above copyright
1243412Snewton *    notice, this list of conditions and the following disclaimer in the
1343412Snewton *    documentation and/or other materials provided with the distribution.
1443412Snewton * 3. The name of the author may not be used to endorse or promote products
1543412Snewton *    derived from this software without specific prior written permission
1643412Snewton *
1743412Snewton * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1843412Snewton * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1943412Snewton * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2043412Snewton * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2143412Snewton * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2243412Snewton * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2343412Snewton * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2443412Snewton * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2543412Snewton * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2643412Snewton * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2743412Snewton */
2843412Snewton
29116174Sobrien#include <sys/cdefs.h>
30116174Sobrien__FBSDID("$FreeBSD$");
31116174Sobrien
3243412Snewton#include <sys/param.h>
3343412Snewton#include <sys/proc.h>
3443412Snewton#include <sys/systm.h>
3543412Snewton#include <sys/file.h>
3643412Snewton#include <sys/filedesc.h>
3743412Snewton#include <sys/sockio.h>
3843412Snewton#include <sys/socket.h>
39182145Sjulian
4043412Snewton#include <net/if.h>
41185571Sbz#include <net/vnet.h>
4243412Snewton
4365302Sobrien#include <compat/svr4/svr4.h>
4465302Sobrien#include <compat/svr4/svr4_util.h>
4565302Sobrien#include <compat/svr4/svr4_ioctl.h>
4665302Sobrien#include <compat/svr4/svr4_sockio.h>
4743412Snewton
4892761Salfredstatic int bsd_to_svr4_flags(int);
4943412Snewton
5043412Snewton#define bsd_to_svr4_flag(a) \
5143412Snewton	if (bf & __CONCAT(I,a))	sf |= __CONCAT(SVR4_I,a)
5243412Snewton
5343412Snewtonstatic int
5443412Snewtonbsd_to_svr4_flags(bf)
5543412Snewton	int bf;
5643412Snewton{
5743412Snewton	int sf = 0;
5843412Snewton	bsd_to_svr4_flag(FF_UP);
5943412Snewton	bsd_to_svr4_flag(FF_BROADCAST);
6043412Snewton	bsd_to_svr4_flag(FF_DEBUG);
6143412Snewton	bsd_to_svr4_flag(FF_LOOPBACK);
6243412Snewton	bsd_to_svr4_flag(FF_POINTOPOINT);
6343412Snewton#if defined(IFF_NOTRAILERS)
6443412Snewton	bsd_to_svr4_flag(FF_NOTRAILERS);
6543412Snewton#endif
66148887Srwatson	if (bf & IFF_DRV_RUNNING)
67148887Srwatson		sf |= SVR4_IFF_RUNNING;
6843412Snewton	bsd_to_svr4_flag(FF_NOARP);
6943412Snewton	bsd_to_svr4_flag(FF_PROMISC);
7043412Snewton	bsd_to_svr4_flag(FF_ALLMULTI);
7143412Snewton	bsd_to_svr4_flag(FF_MULTICAST);
7243412Snewton	return sf;
7343412Snewton}
7443412Snewton
7543412Snewtonint
7683366Sjuliansvr4_sock_ioctl(fp, td, retval, fd, cmd, data)
7743412Snewton	struct file *fp;
7883366Sjulian	struct thread *td;
7943412Snewton	register_t *retval;
8043412Snewton	int fd;
8143412Snewton	u_long cmd;
8243412Snewton	caddr_t data;
8343412Snewton{
8443412Snewton	int error;
8543412Snewton
8643412Snewton	*retval = 0;
8743412Snewton
8843412Snewton	switch (cmd) {
8943412Snewton	case SVR4_SIOCGIFNUM:
9043412Snewton		{
9143412Snewton			struct ifnet *ifp;
9243412Snewton			struct ifaddr *ifa;
9343412Snewton			int ifnum = 0;
9443412Snewton
9543412Snewton			/*
9643412Snewton			 * This does not return the number of physical
9743412Snewton			 * interfaces (if_index), but the number of interfaces
9843412Snewton			 * + addresses like ifconf() does, because this number
9943412Snewton			 * is used by code that will call SVR4_SIOCGIFCONF to
10043412Snewton			 * find the space needed for SVR4_SIOCGIFCONF. So we
10143412Snewton			 * count the number of ifreq entries that the next
10243412Snewton			 * SVR4_SIOCGIFCONF will return. Maybe a more correct
10343412Snewton			 * fix is to make SVR4_SIOCGIFCONF return only one
10443412Snewton			 * entry per physical interface?
10543412Snewton			 */
106108172Shsu			IFNET_RLOCK();
107181803Sbz			TAILQ_FOREACH(ifp, &V_ifnet, if_link)
108160980Sbrooks				if (TAILQ_EMPTY(&ifp->if_addrhead))
10943412Snewton					ifnum++;
11043412Snewton				else
11171453Sjhb					TAILQ_FOREACH(ifa, &ifp->if_addrhead,
11271453Sjhb					    ifa_link)
11343412Snewton						ifnum++;
114108172Shsu			IFNET_RUNLOCK();
11543412Snewton			DPRINTF(("SIOCGIFNUM %d\n", ifnum));
11643412Snewton			return copyout(&ifnum, data, sizeof(ifnum));
11743412Snewton		}
11843412Snewton
11943412Snewton	case SVR4_SIOCGIFFLAGS:
12043412Snewton		{
12143412Snewton			struct ifreq br;
12243412Snewton			struct svr4_ifreq sr;
12343412Snewton
12443412Snewton			if ((error = copyin(data, &sr, sizeof(sr))) != 0)
12543412Snewton				return error;
12643412Snewton
127105360Srobert			(void) strlcpy(br.ifr_name, sr.svr4_ifr_name,
12843412Snewton			    sizeof(br.ifr_name));
12951418Sgreen			if ((error = fo_ioctl(fp, SIOCGIFFLAGS,
130102003Srwatson					    (caddr_t) &br, td->td_ucred,
131102003Srwatson					    td)) != 0) {
13243412Snewton				DPRINTF(("SIOCGIFFLAGS (%s) %s: error %d\n",
13343412Snewton					 br.ifr_name, sr.svr4_ifr_name, error));
13443412Snewton				return error;
13543412Snewton			}
13643412Snewton
13743412Snewton			sr.svr4_ifr_flags = bsd_to_svr4_flags(br.ifr_flags);
13843412Snewton			DPRINTF(("SIOCGIFFLAGS %s = %x\n",
13943412Snewton				sr.svr4_ifr_name, sr.svr4_ifr_flags));
14043412Snewton			return copyout(&sr, data, sizeof(sr));
14143412Snewton		}
14243412Snewton
14343412Snewton	case SVR4_SIOCGIFCONF:
14443412Snewton		{
14543412Snewton			struct svr4_ifconf sc;
14643412Snewton
14743412Snewton			if ((error = copyin(data, &sc, sizeof(sc))) != 0)
14843412Snewton				return error;
14943412Snewton
15043412Snewton			DPRINTF(("ifreq %d svr4_ifreq %d ifc_len %d\n",
15143412Snewton				sizeof(struct ifreq), sizeof(struct svr4_ifreq),
15243412Snewton				sc.svr4_ifc_len));
15343412Snewton
15451418Sgreen			if ((error = fo_ioctl(fp, OSIOCGIFCONF,
155102003Srwatson					    (caddr_t) &sc, td->td_ucred,
156102003Srwatson					    td)) != 0)
15743412Snewton				return error;
15843412Snewton
15943412Snewton			DPRINTF(("SIOCGIFCONF\n"));
16043412Snewton			return 0;
16143412Snewton		}
16243412Snewton
16343412Snewton
16443412Snewton	default:
16543412Snewton		DPRINTF(("Unknown svr4 sockio %lx\n", cmd));
16643412Snewton		return 0;	/* ENOSYS really */
16743412Snewton	}
16843412Snewton}
169