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