svr4_sockio.c revision 52333
16872Siklam/*
212953Scoleenp * Copyright (c) 1998 Mark Newton
36872Siklam * Copyright (c) 1995 Christos Zoulas
46872Siklam * All rights reserved.
56872Siklam *
66872Siklam * Redistribution and use in source and binary forms, with or without
76872Siklam * modification, are permitted provided that the following conditions
86872Siklam * are met:
96872Siklam * 1. Redistributions of source code must retain the above copyright
106872Siklam *    notice, this list of conditions and the following disclaimer.
116872Siklam * 2. Redistributions in binary form must reproduce the above copyright
126872Siklam *    notice, this list of conditions and the following disclaimer in the
136872Siklam *    documentation and/or other materials provided with the distribution.
146872Siklam * 3. The name of the author may not be used to endorse or promote products
156872Siklam *    derived from this software without specific prior written permission
166872Siklam *
176872Siklam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
186872Siklam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
196872Siklam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
206872Siklam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
216872Siklam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
226872Siklam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236872Siklam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246872Siklam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256872Siklam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
266872Siklam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276872Siklam *
286872Siklam * $FreeBSD: head/sys/compat/svr4/svr4_sockio.c 52333 1999-10-17 14:44:48Z newton $
2913541Sccheung */
307896Sstefank
317896Sstefank#include <sys/param.h>
326872Siklam#include <sys/proc.h>
339545Siklam#include <sys/systm.h>
349545Siklam#include <sys/file.h>
356872Siklam#include <sys/filedesc.h>
366872Siklam#include <sys/sockio.h>
376872Siklam#include <sys/termios.h>
386872Siklam#include <sys/socket.h>
396872Siklam#include <net/if.h>
406872Siklam
416872Siklam
426872Siklam#include <svr4/svr4.h>
436872Siklam#include <svr4/svr4_types.h>
446872Siklam#include <svr4/svr4_util.h>
459684Smgronlun#include <svr4/svr4_signal.h>
466872Siklam#include <svr4/svr4_proto.h>
476872Siklam#include <svr4/svr4_stropts.h>
486872Siklam#include <svr4/svr4_ioctl.h>
496872Siklam#include <svr4/svr4_sockio.h>
506872Siklam
516872Siklamstatic int bsd_to_svr4_flags __P((int));
526872Siklam
5312953Scoleenp#define bsd_to_svr4_flag(a) \
5412953Scoleenp	if (bf & __CONCAT(I,a))	sf |= __CONCAT(SVR4_I,a)
5512953Scoleenp
5612953Scoleenpstatic int
576872Siklambsd_to_svr4_flags(bf)
5811410Sjiangli	int bf;
596872Siklam{
6013541Sccheung	int sf = 0;
6113541Sccheung	bsd_to_svr4_flag(FF_UP);
6213541Sccheung	bsd_to_svr4_flag(FF_BROADCAST);
6313541Sccheung	bsd_to_svr4_flag(FF_DEBUG);
6413541Sccheung	bsd_to_svr4_flag(FF_LOOPBACK);
6513541Sccheung	bsd_to_svr4_flag(FF_POINTOPOINT);
6613541Sccheung#if defined(IFF_NOTRAILERS)
6713541Sccheung	bsd_to_svr4_flag(FF_NOTRAILERS);
6813541Sccheung#endif
696872Siklam	bsd_to_svr4_flag(FF_RUNNING);
7010420Salanb	bsd_to_svr4_flag(FF_NOARP);
716872Siklam	bsd_to_svr4_flag(FF_PROMISC);
7211410Sjiangli	bsd_to_svr4_flag(FF_ALLMULTI);
736872Siklam	bsd_to_svr4_flag(FF_MULTICAST);
746872Siklam	return sf;
7512953Scoleenp}
766872Siklam
776872Siklamint
786872Siklamsvr4_sock_ioctl(fp, p, retval, fd, cmd, data)
796872Siklam	struct file *fp;
806872Siklam	struct proc *p;
816982Siklam	register_t *retval;
8211661Slfoltan	int fd;
8311661Slfoltan	u_long cmd;
846872Siklam	caddr_t data;
857413Siklam{
867413Siklam	int error;
877413Siklam
886872Siklam	*retval = 0;
8910420Salanb
9010420Salanb	switch (cmd) {
9110420Salanb	case SVR4_SIOCGIFNUM:
929545Siklam		{
9313541Sccheung			struct ifnet *ifp;
9413541Sccheung			struct ifaddr *ifa;
9513541Sccheung			int ifnum = 0;
9613541Sccheung
9713541Sccheung			/*
9813541Sccheung			 * This does not return the number of physical
9913541Sccheung			 * interfaces (if_index), but the number of interfaces
1006872Siklam			 * + addresses like ifconf() does, because this number
1016872Siklam			 * is used by code that will call SVR4_SIOCGIFCONF to
1026872Siklam			 * find the space needed for SVR4_SIOCGIFCONF. So we
103			 * count the number of ifreq entries that the next
104			 * SVR4_SIOCGIFCONF will return. Maybe a more correct
105			 * fix is to make SVR4_SIOCGIFCONF return only one
106			 * entry per physical interface?
107			 */
108
109			for (ifp = ifnet.tqh_first;
110			     ifp != 0; ifp = ifp->if_link.tqe_next)
111				if ((ifa = ifp->if_addrhead.tqh_first) == NULL)
112					ifnum++;
113				else
114					for (;ifa != NULL;
115					    ifa = ifa->ifa_link.tqe_next)
116						ifnum++;
117
118
119			DPRINTF(("SIOCGIFNUM %d\n", ifnum));
120			return copyout(&ifnum, data, sizeof(ifnum));
121		}
122
123	case SVR4_SIOCGIFFLAGS:
124		{
125			struct ifreq br;
126			struct svr4_ifreq sr;
127
128			if ((error = copyin(data, &sr, sizeof(sr))) != 0)
129				return error;
130
131			(void) strncpy(br.ifr_name, sr.svr4_ifr_name,
132			    sizeof(br.ifr_name));
133			if ((error = fo_ioctl(fp, SIOCGIFFLAGS,
134					    (caddr_t) &br, p)) != 0) {
135				DPRINTF(("SIOCGIFFLAGS (%s) %s: error %d\n",
136					 br.ifr_name, sr.svr4_ifr_name, error));
137				return error;
138			}
139
140			sr.svr4_ifr_flags = bsd_to_svr4_flags(br.ifr_flags);
141			DPRINTF(("SIOCGIFFLAGS %s = %x\n",
142				sr.svr4_ifr_name, sr.svr4_ifr_flags));
143			return copyout(&sr, data, sizeof(sr));
144		}
145
146	case SVR4_SIOCGIFCONF:
147		{
148			struct svr4_ifconf sc;
149
150			if ((error = copyin(data, &sc, sizeof(sc))) != 0)
151				return error;
152
153			DPRINTF(("ifreq %d svr4_ifreq %d ifc_len %d\n",
154				sizeof(struct ifreq), sizeof(struct svr4_ifreq),
155				sc.svr4_ifc_len));
156
157			if ((error = fo_ioctl(fp, OSIOCGIFCONF,
158					    (caddr_t) &sc, p)) != 0)
159				return error;
160
161			DPRINTF(("SIOCGIFCONF\n"));
162			return 0;
163		}
164
165
166	default:
167		DPRINTF(("Unknown svr4 sockio %lx\n", cmd));
168		return 0;	/* ENOSYS really */
169	}
170}
171