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/param.h>
29 #include <sys/eventhandler.h>
30 #include <sys/kernel.h>
32 #include <sys/malloc.h>
33 #include <sys/mutex.h>
35 #include <sys/socket.h>
38 #include <net/if_var.h>
41 #include <compat/linux/linux.h>
42 #include <compat/linux/linux_common.h>
43 #include <fs/pseudofs/pseudofs.h>
45 #include <compat/linsysfs/linsysfs.h>
48 static eventhandler_tag if_arrival_tag, if_departure_tag;
50 static uint32_t net_latch_count = 0;
51 static struct mtx net_latch_mtx;
52 MTX_SYSINIT(net_latch_mtx, &net_latch_mtx, "lsfnet", MTX_DEF);
54 struct ifp_nodes_queue {
55 TAILQ_ENTRY(ifp_nodes_queue) ifp_nodes_next;
60 TAILQ_HEAD(,ifp_nodes_queue) ifp_nodes_q;
63 linsysfs_net_latch_hold(void)
66 mtx_lock(&net_latch_mtx);
67 if (net_latch_count++ > 0)
68 mtx_sleep(&net_latch_count, &net_latch_mtx, PDROP, "lsfnet", 0);
70 mtx_unlock(&net_latch_mtx);
74 linsysfs_net_latch_rele(void)
77 mtx_lock(&net_latch_mtx);
78 if (--net_latch_count > 0)
79 wakeup_one(&net_latch_count);
80 mtx_unlock(&net_latch_mtx);
84 linsysfs_if_addr(PFS_FILL_ARGS)
86 struct epoch_tracker et;
87 struct l_sockaddr lsa;
91 CURVNET_SET(TD_TO_VNET(td));
93 ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name);
94 if (ifp != NULL && (error = linux_ifhwaddr(ifp, &lsa)) == 0)
95 error = sbuf_printf(sb, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n",
96 lsa.sa_data[0], lsa.sa_data[1], lsa.sa_data[2],
97 lsa.sa_data[3], lsa.sa_data[4], lsa.sa_data[5]);
102 return (error == -1 ? ERANGE : error);
106 linsysfs_if_addrlen(PFS_FILL_ARGS)
109 sbuf_printf(sb, "%d\n", LINUX_IFHWADDRLEN);
114 linsysfs_if_flags(PFS_FILL_ARGS)
116 struct epoch_tracker et;
120 CURVNET_SET(TD_TO_VNET(td));
122 ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name);
124 error = sbuf_printf(sb, "0x%x\n", linux_ifflags(ifp));
129 return (error == -1 ? ERANGE : error);
133 linsysfs_if_ifindex(PFS_FILL_ARGS)
135 struct epoch_tracker et;
139 CURVNET_SET(TD_TO_VNET(td));
141 ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name);
143 error = sbuf_printf(sb, "%u\n", if_getindex(ifp));
148 return (error == -1 ? ERANGE : error);
152 linsysfs_if_mtu(PFS_FILL_ARGS)
154 struct epoch_tracker et;
158 CURVNET_SET(TD_TO_VNET(td));
160 ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name);
162 error = sbuf_printf(sb, "%u\n", if_getmtu(ifp));
167 return (error == -1 ? ERANGE : error);
171 linsysfs_if_txq_len(PFS_FILL_ARGS)
175 sbuf_printf(sb, "1000\n");
180 linsysfs_if_type(PFS_FILL_ARGS)
182 struct epoch_tracker et;
183 struct l_sockaddr lsa;
187 CURVNET_SET(TD_TO_VNET(td));
189 ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name);
190 if (ifp != NULL && (error = linux_ifhwaddr(ifp, &lsa)) == 0)
191 error = sbuf_printf(sb, "%d\n", lsa.sa_family);
196 return (error == -1 ? ERANGE : error);
200 linsysfs_if_visible(PFS_VIS_ARGS)
202 struct ifp_nodes_queue *nq, *nq_tmp;
203 struct epoch_tracker et;
208 CURVNET_SET(TD_TO_VNET(td));
210 ifp = ifname_linux_to_ifp(td, pn->pn_name);
212 TAILQ_FOREACH_SAFE(nq, &ifp_nodes_q, ifp_nodes_next, nq_tmp) {
213 if (nq->ifp == ifp && nq->vnet == curvnet) {
225 linsysfs_net_addif(if_t ifp, void *arg)
227 struct ifp_nodes_queue *nq, *nq_tmp;
228 struct pfs_node *nic, *dir = arg;
229 char ifname[LINUX_IFNAMSIZ];
230 struct epoch_tracker et;
234 ret = ifname_bsd_to_linux_ifp(ifp, ifname, sizeof(ifname));
236 KASSERT(ret > 0, ("Interface (%s) is not converted", if_name(ifp)));
238 nic = pfs_find_node(dir, ifname);
240 nic = pfs_create_dir(dir, ifname, NULL, linsysfs_if_visible,
242 pfs_create_file(nic, "address", &linsysfs_if_addr,
243 NULL, NULL, NULL, PFS_RD);
244 pfs_create_file(nic, "addr_len", &linsysfs_if_addrlen,
245 NULL, NULL, NULL, PFS_RD);
246 pfs_create_file(nic, "flags", &linsysfs_if_flags,
247 NULL, NULL, NULL, PFS_RD);
248 pfs_create_file(nic, "ifindex", &linsysfs_if_ifindex,
249 NULL, NULL, NULL, PFS_RD);
250 pfs_create_file(nic, "mtu", &linsysfs_if_mtu,
251 NULL, NULL, NULL, PFS_RD);
252 pfs_create_file(nic, "tx_queue_len", &linsysfs_if_txq_len,
253 NULL, NULL, NULL, PFS_RD);
254 pfs_create_file(nic, "type", &linsysfs_if_type,
255 NULL, NULL, NULL, PFS_RD);
258 * There is a small window between registering the if_arrival
259 * eventhandler and creating a list of interfaces.
261 TAILQ_FOREACH_SAFE(nq, &ifp_nodes_q, ifp_nodes_next, nq_tmp) {
262 if (nq->ifp == ifp && nq->vnet == curvnet)
265 nq = malloc(sizeof(*nq), M_LINSYSFS, M_WAITOK);
269 TAILQ_INSERT_TAIL(&ifp_nodes_q, nq, ifp_nodes_next);
274 linsysfs_net_delif(if_t ifp)
276 struct ifp_nodes_queue *nq, *nq_tmp;
280 TAILQ_FOREACH_SAFE(nq, &ifp_nodes_q, ifp_nodes_next, nq_tmp) {
281 if (nq->ifp == ifp && nq->vnet == curvnet) {
282 TAILQ_REMOVE(&ifp_nodes_q, nq, ifp_nodes_next);
284 free(nq, M_LINSYSFS);
290 TAILQ_FOREACH_SAFE(nq, &ifp_nodes_q, ifp_nodes_next, nq_tmp) {
298 linsysfs_if_arrival(void *arg __unused, if_t ifp)
301 linsysfs_net_latch_hold();
302 (void)linsysfs_net_addif(ifp, net);
303 linsysfs_net_latch_rele();
307 linsysfs_if_departure(void *arg __unused, if_t ifp)
310 linsysfs_net_latch_hold();
311 linsysfs_net_delif(ifp);
312 linsysfs_net_latch_rele();
316 linsysfs_net_init(void)
318 VNET_ITERATOR_DECL(vnet_iter);
321 TAILQ_INIT(&ifp_nodes_q);
323 if_arrival_tag = EVENTHANDLER_REGISTER(ifnet_arrival_event,
324 linsysfs_if_arrival, NULL, EVENTHANDLER_PRI_ANY);
325 if_departure_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
326 linsysfs_if_departure, NULL, EVENTHANDLER_PRI_ANY);
328 linsysfs_net_latch_hold();
330 VNET_FOREACH(vnet_iter) {
331 CURVNET_SET(vnet_iter);
332 if_foreach_sleep(NULL, NULL, linsysfs_net_addif, net);
336 linsysfs_net_latch_rele();
340 linsysfs_net_uninit(void)
342 struct ifp_nodes_queue *nq, *nq_tmp;
344 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, if_arrival_tag);
345 EVENTHANDLER_DEREGISTER(ifnet_departure_event, if_departure_tag);
347 linsysfs_net_latch_hold();
348 TAILQ_FOREACH_SAFE(nq, &ifp_nodes_q, ifp_nodes_next, nq_tmp) {
349 TAILQ_REMOVE(&ifp_nodes_q, nq, ifp_nodes_next);
350 free(nq, M_LINSYSFS);
352 linsysfs_net_latch_rele();