No empty .Rs/.Re
[netbsd-mini2440.git] / sys / compat / svr4 / svr4_sockio.c
blobaca657cd44b2fcdd8ac0f6afebb0e61cc6e635f7
1 /* $NetBSD: svr4_sockio.c,v 1.34 2008/03/21 21:54:59 ad Exp $ */
3 /*-
4 * Copyright (c) 1995, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: svr4_sockio.c,v 1.34 2008/03/21 21:54:59 ad Exp $");
35 #include <sys/param.h>
36 #include <sys/proc.h>
37 #include <sys/systm.h>
38 #include <sys/file.h>
39 #include <sys/filedesc.h>
40 #include <sys/ioctl.h>
41 #include <sys/termios.h>
42 #include <sys/tty.h>
43 #include <sys/socket.h>
44 #include <sys/mount.h>
45 #include <net/if.h>
46 #include <sys/malloc.h>
48 #include <sys/syscallargs.h>
50 #include <compat/sys/socket.h>
51 #include <compat/sys/sockio.h>
53 #include <compat/svr4/svr4_types.h>
54 #include <compat/svr4/svr4_util.h>
55 #include <compat/svr4/svr4_signal.h>
56 #include <compat/svr4/svr4_lwp.h>
57 #include <compat/svr4/svr4_ucontext.h>
58 #include <compat/svr4/svr4_syscallargs.h>
59 #include <compat/svr4/svr4_stropts.h>
60 #include <compat/svr4/svr4_ioctl.h>
61 #include <compat/svr4/svr4_sockio.h>
63 static int bsd_to_svr4_flags(int);
65 #define bsd_to_svr4_flag(a) \
66 if (bf & __CONCAT(I,a)) sf |= __CONCAT(SVR4_I,a)
68 static int
69 bsd_to_svr4_flags(int bf)
71 int sf = 0;
72 bsd_to_svr4_flag(FF_UP);
73 bsd_to_svr4_flag(FF_BROADCAST);
74 bsd_to_svr4_flag(FF_DEBUG);
75 bsd_to_svr4_flag(FF_LOOPBACK);
76 bsd_to_svr4_flag(FF_POINTOPOINT);
77 bsd_to_svr4_flag(FF_NOTRAILERS);
78 bsd_to_svr4_flag(FF_RUNNING);
79 bsd_to_svr4_flag(FF_NOARP);
80 bsd_to_svr4_flag(FF_PROMISC);
81 bsd_to_svr4_flag(FF_ALLMULTI);
82 bsd_to_svr4_flag(FF_MULTICAST);
83 return sf;
86 int
87 svr4_count_ifnum(struct ifnet *ifp)
89 struct ifaddr *ifa;
90 int ifnum = 0;
92 IFADDR_FOREACH(ifa, ifp)
93 ifnum++;
95 return MAX(1, ifnum);
98 int
99 svr4_sock_ioctl(file_t *fp, struct lwp *l, register_t *retval,
100 int fd, u_long cmd, void *data)
102 int error;
103 int (*ctl)(file_t *, u_long, void *) = fp->f_ops->fo_ioctl;
105 *retval = 0;
107 switch (cmd) {
108 case SVR4_SIOCGLIFNUM:
110 struct ifnet *ifp;
111 struct svr4_lifnum lifnum;
113 error = copyin(data, &lifnum, sizeof(lifnum));
114 if (error)
115 return error;
117 lifnum.lifn_count = 0;
118 /* XXX: We don't pay attention to family or flags */
119 IFNET_FOREACH(ifp)
120 lifnum.lifn_count += svr4_count_ifnum(ifp);
122 DPRINTF(("SIOCGLIFNUM [family=%d,flags=%d,count=%d]\n",
123 lifnum.lifn_family, lifnum.lifn_flags,
124 lifnum.lifn_count));
125 return copyout(&lifnum, data, sizeof(lifnum));
128 case SVR4_SIOCGIFNUM:
130 struct ifnet *ifp;
131 int ifnum = 0;
134 * This does not return the number of physical
135 * interfaces (if_index), but the number of interfaces
136 * + addresses like ifconf() does, because this number
137 * is used by code that will call SVR4_SIOCGIFCONF to
138 * find the space needed for SVR4_SIOCGIFCONF. So we
139 * count the number of ifreq entries that the next
140 * SVR4_SIOCGIFCONF will return. Maybe a more correct
141 * fix is to make SVR4_SIOCGIFCONF return only one
142 * entry per physical interface?
145 IFNET_FOREACH(ifp)
146 ifnum += svr4_count_ifnum(ifp);
148 DPRINTF(("SIOCGIFNUM %d\n", ifnum));
149 return copyout(&ifnum, data, sizeof(ifnum));
152 case SVR4_SIOCGIFFLAGS:
154 struct oifreq br;
155 struct svr4_ifreq sr;
157 if ((error = copyin(data, &sr, sizeof(sr))) != 0)
158 return error;
160 (void) strncpy(br.ifr_name, sr.svr4_ifr_name,
161 sizeof(br.ifr_name));
163 if ((error = (*ctl)(fp, SIOCGIFFLAGS, &br)) != 0) {
164 DPRINTF(("SIOCGIFFLAGS %s: error %d\n",
165 sr.svr4_ifr_name, error));
166 return error;
169 sr.svr4_ifr_flags = bsd_to_svr4_flags(br.ifr_flags);
170 DPRINTF(("SIOCGIFFLAGS %s = %x\n",
171 sr.svr4_ifr_name, sr.svr4_ifr_flags));
172 return copyout(&sr, data, sizeof(sr));
175 case SVR4_SIOCGIFCONF:
177 struct svr4_ifconf sc;
179 if ((error = copyin(data, &sc, sizeof(sc))) != 0)
180 return error;
182 DPRINTF(("ifreq %ld svr4_ifreq %ld ifc_len %d\n",
183 (unsigned long)sizeof(struct oifreq),
184 (unsigned long)sizeof(struct svr4_ifreq),
185 sc.svr4_ifc_len));
187 if ((error = (*ctl)(fp, OOSIOCGIFCONF, &sc)) != 0)
188 return error;
190 DPRINTF(("SIOCGIFCONF\n"));
191 return 0;
195 default:
196 DPRINTF(("Unknown svr4 sockio %lx\n", cmd));
197 return 0; /* ENOSYS really */