svr4_sockio.c revision 108172
143412Snewton/* 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. 2749267Snewton * 2850477Speter * $FreeBSD: head/sys/compat/svr4/svr4_sockio.c 108172 2002-12-22 05:35:03Z hsu $ 2943412Snewton */ 3043412Snewton 3143412Snewton#include <sys/param.h> 3243412Snewton#include <sys/proc.h> 3343412Snewton#include <sys/systm.h> 3443412Snewton#include <sys/file.h> 3543412Snewton#include <sys/filedesc.h> 3643412Snewton#include <sys/sockio.h> 3743412Snewton#include <sys/socket.h> 3843412Snewton#include <net/if.h> 3943412Snewton 4043412Snewton 4165302Sobrien#include <compat/svr4/svr4.h> 4265302Sobrien#include <compat/svr4/svr4_util.h> 4365302Sobrien#include <compat/svr4/svr4_ioctl.h> 4465302Sobrien#include <compat/svr4/svr4_sockio.h> 4543412Snewton 4692761Salfredstatic int bsd_to_svr4_flags(int); 4743412Snewton 4843412Snewton#define bsd_to_svr4_flag(a) \ 4943412Snewton if (bf & __CONCAT(I,a)) sf |= __CONCAT(SVR4_I,a) 5043412Snewton 5143412Snewtonstatic int 5243412Snewtonbsd_to_svr4_flags(bf) 5343412Snewton int bf; 5443412Snewton{ 5543412Snewton int sf = 0; 5643412Snewton bsd_to_svr4_flag(FF_UP); 5743412Snewton bsd_to_svr4_flag(FF_BROADCAST); 5843412Snewton bsd_to_svr4_flag(FF_DEBUG); 5943412Snewton bsd_to_svr4_flag(FF_LOOPBACK); 6043412Snewton bsd_to_svr4_flag(FF_POINTOPOINT); 6143412Snewton#if defined(IFF_NOTRAILERS) 6243412Snewton bsd_to_svr4_flag(FF_NOTRAILERS); 6343412Snewton#endif 6443412Snewton bsd_to_svr4_flag(FF_RUNNING); 6543412Snewton bsd_to_svr4_flag(FF_NOARP); 6643412Snewton bsd_to_svr4_flag(FF_PROMISC); 6743412Snewton bsd_to_svr4_flag(FF_ALLMULTI); 6843412Snewton bsd_to_svr4_flag(FF_MULTICAST); 6943412Snewton return sf; 7043412Snewton} 7143412Snewton 7243412Snewtonint 7383366Sjuliansvr4_sock_ioctl(fp, td, retval, fd, cmd, data) 7443412Snewton struct file *fp; 7583366Sjulian struct thread *td; 7643412Snewton register_t *retval; 7743412Snewton int fd; 7843412Snewton u_long cmd; 7943412Snewton caddr_t data; 8043412Snewton{ 8143412Snewton int error; 8243412Snewton 8343412Snewton *retval = 0; 8443412Snewton 8543412Snewton switch (cmd) { 8643412Snewton case SVR4_SIOCGIFNUM: 8743412Snewton { 8843412Snewton struct ifnet *ifp; 8943412Snewton struct ifaddr *ifa; 9043412Snewton int ifnum = 0; 9143412Snewton 9243412Snewton /* 9343412Snewton * This does not return the number of physical 9443412Snewton * interfaces (if_index), but the number of interfaces 9543412Snewton * + addresses like ifconf() does, because this number 9643412Snewton * is used by code that will call SVR4_SIOCGIFCONF to 9743412Snewton * find the space needed for SVR4_SIOCGIFCONF. So we 9843412Snewton * count the number of ifreq entries that the next 9943412Snewton * SVR4_SIOCGIFCONF will return. Maybe a more correct 10043412Snewton * fix is to make SVR4_SIOCGIFCONF return only one 10143412Snewton * entry per physical interface? 10243412Snewton */ 103108172Shsu IFNET_RLOCK(); 10471453Sjhb TAILQ_FOREACH(ifp, &ifnet, if_link) 10571453Sjhb if (TAILQ_FIRST(&ifp->if_addrhead) == NULL) 10643412Snewton ifnum++; 10743412Snewton else 10871453Sjhb TAILQ_FOREACH(ifa, &ifp->if_addrhead, 10971453Sjhb ifa_link) 11043412Snewton ifnum++; 111108172Shsu IFNET_RUNLOCK(); 11243412Snewton DPRINTF(("SIOCGIFNUM %d\n", ifnum)); 11343412Snewton return copyout(&ifnum, data, sizeof(ifnum)); 11443412Snewton } 11543412Snewton 11643412Snewton case SVR4_SIOCGIFFLAGS: 11743412Snewton { 11843412Snewton struct ifreq br; 11943412Snewton struct svr4_ifreq sr; 12043412Snewton 12143412Snewton if ((error = copyin(data, &sr, sizeof(sr))) != 0) 12243412Snewton return error; 12343412Snewton 124105360Srobert (void) strlcpy(br.ifr_name, sr.svr4_ifr_name, 12543412Snewton sizeof(br.ifr_name)); 12651418Sgreen if ((error = fo_ioctl(fp, SIOCGIFFLAGS, 127102003Srwatson (caddr_t) &br, td->td_ucred, 128102003Srwatson td)) != 0) { 12943412Snewton DPRINTF(("SIOCGIFFLAGS (%s) %s: error %d\n", 13043412Snewton br.ifr_name, sr.svr4_ifr_name, error)); 13143412Snewton return error; 13243412Snewton } 13343412Snewton 13443412Snewton sr.svr4_ifr_flags = bsd_to_svr4_flags(br.ifr_flags); 13543412Snewton DPRINTF(("SIOCGIFFLAGS %s = %x\n", 13643412Snewton sr.svr4_ifr_name, sr.svr4_ifr_flags)); 13743412Snewton return copyout(&sr, data, sizeof(sr)); 13843412Snewton } 13943412Snewton 14043412Snewton case SVR4_SIOCGIFCONF: 14143412Snewton { 14243412Snewton struct svr4_ifconf sc; 14343412Snewton 14443412Snewton if ((error = copyin(data, &sc, sizeof(sc))) != 0) 14543412Snewton return error; 14643412Snewton 14743412Snewton DPRINTF(("ifreq %d svr4_ifreq %d ifc_len %d\n", 14843412Snewton sizeof(struct ifreq), sizeof(struct svr4_ifreq), 14943412Snewton sc.svr4_ifc_len)); 15043412Snewton 15151418Sgreen if ((error = fo_ioctl(fp, OSIOCGIFCONF, 152102003Srwatson (caddr_t) &sc, td->td_ucred, 153102003Srwatson td)) != 0) 15443412Snewton return error; 15543412Snewton 15643412Snewton DPRINTF(("SIOCGIFCONF\n")); 15743412Snewton return 0; 15843412Snewton } 15943412Snewton 16043412Snewton 16143412Snewton default: 16243412Snewton DPRINTF(("Unknown svr4 sockio %lx\n", cmd)); 16343412Snewton return 0; /* ENOSYS really */ 16443412Snewton } 16543412Snewton} 166