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