2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2023 Dmitry Chagin <dchagin@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/eventhandler.h>
33 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/mutex.h>
38 #include <sys/socket.h>
41 #include <net/if_var.h>
44 #include <compat/linux/linux.h>
45 #include <compat/linux/linux_common.h>
46 #include <fs/pseudofs/pseudofs.h>
48 #include <compat/linsysfs/linsysfs.h>
51 static eventhandler_tag if_arrival_tag, if_departure_tag;
53 static uint32_t net_latch_count = 0;
54 static struct mtx net_latch_mtx;
55 MTX_SYSINIT(net_latch_mtx, &net_latch_mtx, "lsfnet", MTX_DEF);
57 struct ifp_nodes_queue {
58 TAILQ_ENTRY(ifp_nodes_queue) ifp_nodes_next;
63 TAILQ_HEAD(,ifp_nodes_queue) ifp_nodes_q;
66 linsysfs_net_latch_hold(void)
69 mtx_lock(&net_latch_mtx);
70 if (net_latch_count++ > 0)
71 mtx_sleep(&net_latch_count, &net_latch_mtx, PDROP, "lsfnet", 0);
73 mtx_unlock(&net_latch_mtx);
77 linsysfs_net_latch_rele(void)
80 mtx_lock(&net_latch_mtx);
81 if (--net_latch_count > 0)
82 wakeup_one(&net_latch_count);
83 mtx_unlock(&net_latch_mtx);
87 linsysfs_if_addr(PFS_FILL_ARGS)
89 struct epoch_tracker et;
90 struct l_sockaddr lsa;
94 CURVNET_SET(TD_TO_VNET(td));
96 ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name);
97 if (ifp != NULL && (error = linux_ifhwaddr(ifp, &lsa)) == 0)
98 error = sbuf_printf(sb, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n",
99 lsa.sa_data[0], lsa.sa_data[1], lsa.sa_data[2],
100 lsa.sa_data[3], lsa.sa_data[4], lsa.sa_data[5]);
105 return (error == -1 ? ERANGE : error);
109 linsysfs_if_addrlen(PFS_FILL_ARGS)
112 sbuf_printf(sb, "%d\n", LINUX_IFHWADDRLEN);
117 linsysfs_if_flags(PFS_FILL_ARGS)
119 struct epoch_tracker et;
123 CURVNET_SET(TD_TO_VNET(td));
125 ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name);
127 error = sbuf_printf(sb, "0x%x\n", linux_ifflags(ifp));
132 return (error == -1 ? ERANGE : error);
136 linsysfs_if_ifindex(PFS_FILL_ARGS)
138 struct epoch_tracker et;
142 CURVNET_SET(TD_TO_VNET(td));
144 ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name);
146 error = sbuf_printf(sb, "%u\n", if_getindex(ifp));
151 return (error == -1 ? ERANGE : error);
155 linsysfs_if_mtu(PFS_FILL_ARGS)
157 struct epoch_tracker et;
161 CURVNET_SET(TD_TO_VNET(td));
163 ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name);
165 error = sbuf_printf(sb, "%u\n", if_getmtu(ifp));
170 return (error == -1 ? ERANGE : error);
174 linsysfs_if_txq_len(PFS_FILL_ARGS)
178 sbuf_printf(sb, "1000\n");
183 linsysfs_if_type(PFS_FILL_ARGS)
185 struct epoch_tracker et;
186 struct l_sockaddr lsa;
190 CURVNET_SET(TD_TO_VNET(td));
192 ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name);
193 if (ifp != NULL && (error = linux_ifhwaddr(ifp, &lsa)) == 0)
194 error = sbuf_printf(sb, "%d\n", lsa.sa_family);
199 return (error == -1 ? ERANGE : error);
203 linsysfs_if_visible(PFS_VIS_ARGS)
205 struct ifp_nodes_queue *nq, *nq_tmp;
206 struct epoch_tracker et;
211 CURVNET_SET(TD_TO_VNET(td));
213 ifp = ifname_linux_to_ifp(td, pn->pn_name);
215 TAILQ_FOREACH_SAFE(nq, &ifp_nodes_q, ifp_nodes_next, nq_tmp) {
216 if (nq->ifp == ifp && nq->vnet == curvnet) {
228 linsysfs_net_addif(if_t ifp, void *arg)
230 struct ifp_nodes_queue *nq, *nq_tmp;
231 struct pfs_node *nic, *dir = arg;
232 char ifname[LINUX_IFNAMSIZ];
233 struct epoch_tracker et;
237 ret = ifname_bsd_to_linux_ifp(ifp, ifname, sizeof(ifname));
239 KASSERT(ret > 0, ("Interface (%s) is not converted", if_name(ifp)));
241 nic = pfs_find_node(dir, ifname);
243 nic = pfs_create_dir(dir, ifname, NULL, linsysfs_if_visible,
245 pfs_create_file(nic, "address", &linsysfs_if_addr,
246 NULL, NULL, NULL, PFS_RD);
247 pfs_create_file(nic, "addr_len", &linsysfs_if_addrlen,
248 NULL, NULL, NULL, PFS_RD);
249 pfs_create_file(nic, "flags", &linsysfs_if_flags,
250 NULL, NULL, NULL, PFS_RD);
251 pfs_create_file(nic, "ifindex", &linsysfs_if_ifindex,
252 NULL, NULL, NULL, PFS_RD);
253 pfs_create_file(nic, "mtu", &linsysfs_if_mtu,
254 NULL, NULL, NULL, PFS_RD);
255 pfs_create_file(nic, "tx_queue_len", &linsysfs_if_txq_len,
256 NULL, NULL, NULL, PFS_RD);
257 pfs_create_file(nic, "type", &linsysfs_if_type,
258 NULL, NULL, NULL, PFS_RD);
261 * There is a small window between registering the if_arrival
262 * eventhandler and creating a list of interfaces.
264 TAILQ_FOREACH_SAFE(nq, &ifp_nodes_q, ifp_nodes_next, nq_tmp) {
265 if (nq->ifp == ifp && nq->vnet == curvnet)
268 nq = malloc(sizeof(*nq), M_LINSYSFS, M_WAITOK);
272 TAILQ_INSERT_TAIL(&ifp_nodes_q, nq, ifp_nodes_next);
277 linsysfs_net_delif(if_t ifp)
279 struct ifp_nodes_queue *nq, *nq_tmp;
283 TAILQ_FOREACH_SAFE(nq, &ifp_nodes_q, ifp_nodes_next, nq_tmp) {
284 if (nq->ifp == ifp && nq->vnet == curvnet) {
285 TAILQ_REMOVE(&ifp_nodes_q, nq, ifp_nodes_next);
287 free(nq, M_LINSYSFS);
293 TAILQ_FOREACH_SAFE(nq, &ifp_nodes_q, ifp_nodes_next, nq_tmp) {
301 linsysfs_if_arrival(void *arg __unused, if_t ifp)
304 linsysfs_net_latch_hold();
305 (void)linsysfs_net_addif(ifp, net);
306 linsysfs_net_latch_rele();
310 linsysfs_if_departure(void *arg __unused, if_t ifp)
313 linsysfs_net_latch_hold();
314 linsysfs_net_delif(ifp);
315 linsysfs_net_latch_rele();
319 linsysfs_net_init(void)
321 VNET_ITERATOR_DECL(vnet_iter);
324 TAILQ_INIT(&ifp_nodes_q);
326 if_arrival_tag = EVENTHANDLER_REGISTER(ifnet_arrival_event,
327 linsysfs_if_arrival, NULL, EVENTHANDLER_PRI_ANY);
328 if_departure_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
329 linsysfs_if_departure, NULL, EVENTHANDLER_PRI_ANY);
331 linsysfs_net_latch_hold();
333 VNET_FOREACH(vnet_iter) {
334 CURVNET_SET(vnet_iter);
335 if_foreach_sleep(NULL, NULL, linsysfs_net_addif, net);
339 linsysfs_net_latch_rele();
343 linsysfs_net_uninit(void)
345 struct ifp_nodes_queue *nq, *nq_tmp;
347 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, if_arrival_tag);
348 EVENTHANDLER_DEREGISTER(ifnet_departure_event, if_departure_tag);
350 linsysfs_net_latch_hold();
351 TAILQ_FOREACH_SAFE(nq, &ifp_nodes_q, ifp_nodes_next, nq_tmp) {
352 TAILQ_REMOVE(&ifp_nodes_q, nq, ifp_nodes_next);
353 free(nq, M_LINSYSFS);
355 linsysfs_net_latch_rele();