uipc_syscalls_40.c revision 1.15.2.11 1 /* $NetBSD: uipc_syscalls_40.c,v 1.15.2.11 2018/09/22 04:56:28 pgoyette Exp $ */
2
3 /* written by Pavel Cahyna, 2006. Public domain. */
4
5 #include <sys/cdefs.h>
6 __KERNEL_RCSID(0, "$NetBSD: uipc_syscalls_40.c,v 1.15.2.11 2018/09/22 04:56:28 pgoyette Exp $");
7
8 #if defined(_KERNEL_OPT)
9 #include "opt_compat_netbsd.h"
10 #endif
11
12 /*
13 * System call interface to the socket abstraction.
14 */
15
16 #include <sys/param.h>
17 #include <sys/kernel.h>
18 #include <sys/msg.h>
19 #include <sys/sysctl.h>
20 #include <sys/syscallargs.h>
21 #include <sys/errno.h>
22 #include <sys/compat_stub.h>
23
24 #include <net/if.h>
25
26 #include <compat/sys/if.h>
27 #include <compat/sys/socket.h>
28 #include <compat/sys/sockio.h>
29
30 #include <compat/common/if_40.h>
31 #include <compat/common/compat_mod.h>
32
33 /*
34 * Return interface configuration
35 * of system. List may be used
36 * in later ioctl's (above) to get
37 * other information.
38 */
39 /*ARGSUSED*/
40 static int
41 compat_ifconf(u_long cmd, void *data)
42 {
43 struct oifconf *ifc = data;
44 struct ifnet *ifp;
45 struct oifreq ifr, *ifrp = NULL;
46 int space = 0, error = 0;
47 const int sz = (int)sizeof(ifr);
48 const bool docopy = ifc->ifc_req != NULL;
49 int s;
50 int bound;
51 struct psref psref;
52
53 switch (cmd) {
54 case OSIOCGIFCONF:
55 case OOSIOCGIFCONF:
56 break;
57 default:
58 return ENOSYS;
59 }
60
61 if (docopy) {
62 space = ifc->ifc_len;
63 ifrp = ifc->ifc_req;
64 }
65
66 bound = curlwp_bind();
67 s = pserialize_read_enter();
68 IFNET_READER_FOREACH(ifp) {
69 struct ifaddr *ifa;
70
71 if_acquire(ifp, &psref);
72 pserialize_read_exit(s);
73
74 (void)strncpy(ifr.ifr_name, ifp->if_xname,
75 sizeof(ifr.ifr_name));
76 if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0') {
77 error = ENAMETOOLONG;
78 goto release_exit;
79 }
80 if (IFADDR_READER_EMPTY(ifp)) {
81 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
82 if (space >= sz) {
83 error = copyout(&ifr, ifrp, sz);
84 if (error != 0)
85 goto release_exit;
86 ifrp++;
87 }
88 space -= sizeof(ifr);
89 goto next;
90 }
91
92 s = pserialize_read_enter();
93 IFADDR_READER_FOREACH(ifa, ifp) {
94 struct sockaddr *sa = ifa->ifa_addr;
95 struct psref psref_ifa;
96
97 ifa_acquire(ifa, &psref_ifa);
98 pserialize_read_exit(s);
99 #ifdef COMPAT_OSOCK
100 if (cmd == OOSIOCGIFCONF) {
101 struct osockaddr *osa =
102 (struct osockaddr *)&ifr.ifr_addr;
103 /*
104 * If it does not fit, we don't bother with it
105 */
106 if (sa->sa_len > sizeof(*osa))
107 goto next_ifa;
108 memcpy(&ifr.ifr_addr, sa, sa->sa_len);
109 osa->sa_family = sa->sa_family;
110 if (space >= sz) {
111 error = copyout(&ifr, ifrp, sz);
112 ifrp++;
113 }
114 } else
115 #endif
116 if (sa->sa_len <= sizeof(*sa)) {
117 memcpy(&ifr.ifr_addr, sa, sa->sa_len);
118 if (space >= sz) {
119 error = copyout(&ifr, ifrp, sz);
120 ifrp++;
121 }
122 } else {
123 space -= sa->sa_len - sizeof(*sa);
124 if (space >= sz) {
125 error = copyout(&ifr, ifrp,
126 sizeof(ifr.ifr_name));
127 if (error == 0) {
128 error = copyout(sa,
129 &ifrp->ifr_addr,
130 sa->sa_len);
131 }
132 ifrp = (struct oifreq *)
133 (sa->sa_len +
134 (char *)&ifrp->ifr_addr);
135 }
136 }
137 if (error != 0) {
138 ifa_release(ifa, &psref_ifa);
139 goto release_exit;
140 }
141 space -= sz;
142
143 #ifdef COMPAT_OSOCK
144 next_ifa:
145 #endif
146 s = pserialize_read_enter();
147 ifa_release(ifa, &psref_ifa);
148 }
149 pserialize_read_exit(s);
150
151 next:
152 s = pserialize_read_enter();
153 if_release(ifp, &psref);
154 }
155 pserialize_read_exit(s);
156 curlwp_bindx(bound);
157
158 if (docopy)
159 ifc->ifc_len -= space;
160 else
161 ifc->ifc_len = -space;
162 return (0);
163
164 release_exit:
165 if_release(ifp, &psref);
166 curlwp_bindx(bound);
167 return error;
168 }
169
170 MODULE_SET_HOOK(uipc_syscalls_40_hook, "uipc40", compat_ifconf);
171 MODULE_UNSET_HOOK(uipc_syscalls_40_hook);
172
173 void
174 uipc_syscalls_40_init(void)
175 {
176
177 uipc_syscalls_40_hook_set();
178 }
179
180 void
181 uipc_syscalls_40_fini(void)
182 {
183
184 uipc_syscalls_40_hook_unset();
185 }
186