2 * Copyright (C) 2000 Lennert Buytenhek
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <sys/types.h>
29 #include <sys/socket.h>
31 #include <linux/in6.h>
32 #include <linux/if_bridge.h>
34 #include "libbridge.h"
35 #include "libbridge_private.h"
37 static int br_socket_fd = -1;
41 if ((br_socket_fd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0)
46 void br_shutdown(void)
52 /* If /sys/class/net/XXX/bridge exists then it must be a bridge */
53 static int isbridge(const struct dirent *entry)
55 char path[SYSFS_PATH_MAX];
58 snprintf(path, SYSFS_PATH_MAX, SYSFS_CLASS_NET "%s/bridge", entry->d_name);
59 return stat(path, &st) == 0 && S_ISDIR(st.st_mode);
63 * New interface uses sysfs to find bridges
65 static int new_foreach_bridge(int (*iterator)(const char *name, void *),
68 struct dirent **namelist;
71 count = scandir(SYSFS_CLASS_NET, &namelist, isbridge, alphasort);
75 for (i = 0; i < count; i++) {
76 if (iterator(namelist[i]->d_name, arg))
80 for (i = 0; i < count; i++)
88 * Go over all bridges and call iterator function.
89 * if iterator returns non-zero then stop.
91 int br_foreach_bridge(int (*iterator)(const char *, void *), void *arg)
93 return new_foreach_bridge(iterator, arg);
97 * Iterate over all ports in bridge (using sysfs).
99 int br_foreach_port(const char *brname,
100 int (*iterator)(const char *br, const char *port, void *arg),
104 struct dirent **namelist;
105 char path[SYSFS_PATH_MAX];
107 snprintf(path, SYSFS_PATH_MAX, SYSFS_CLASS_NET "%s/brif", brname);
108 count = scandir(path, &namelist, 0, alphasort);
110 for (i = 0; i < count; i++) {
111 if (namelist[i]->d_name[0] == '.'
112 && (namelist[i]->d_name[1] == '\0'
113 || (namelist[i]->d_name[1] == '.'
114 && namelist[i]->d_name[2] == '\0')))
117 if (iterator(brname, namelist[i]->d_name, arg))
120 for (i = 0; i < count; i++)