1/*
2 * Copyright (C) 2000 Lennert Buytenhek
3 *
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.
8 *
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.
13 *
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.
17 */
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <errno.h>
22#include <string.h>
23#include <sys/fcntl.h>
24#include <sys/ioctl.h>
25
26#include "libbridge.h"
27#include "libbridge_private.h"
28
29
30int br_add_bridge(const char *brname)
31{
32	int ret;
33
34#ifdef SIOCBRADDBR
35	ret = ioctl(br_socket_fd, SIOCBRADDBR, brname);
36	if (ret < 0)
37#endif
38	{
39		char _br[IFNAMSIZ];
40		unsigned long arg[3]
41			= { BRCTL_ADD_BRIDGE, (unsigned long) _br };
42
43		strncpy(_br, brname, IFNAMSIZ);
44		ret = ioctl(br_socket_fd, SIOCSIFBR, arg);
45	}
46
47	return ret < 0 ? errno : 0;
48}
49
50int br_del_bridge(const char *brname)
51{
52	int ret;
53
54#ifdef SIOCBRDELBR
55	ret = ioctl(br_socket_fd, SIOCBRDELBR, brname);
56	if (ret < 0)
57#endif
58	{
59		char _br[IFNAMSIZ];
60		unsigned long arg[3]
61			= { BRCTL_DEL_BRIDGE, (unsigned long) _br };
62
63		strncpy(_br, brname, IFNAMSIZ);
64		ret = ioctl(br_socket_fd, SIOCSIFBR, arg);
65	}
66	return  ret < 0 ? errno : 0;
67}
68
69int br_add_interface(const char *bridge, const char *dev)
70{
71	struct ifreq ifr;
72	int err;
73	int ifindex = if_nametoindex(dev);
74
75	if (ifindex == 0)
76		return ENODEV;
77
78	strncpy(ifr.ifr_name, bridge, IFNAMSIZ);
79#ifdef SIOCBRADDIF
80	ifr.ifr_ifindex = ifindex;
81	err = ioctl(br_socket_fd, SIOCBRADDIF, &ifr);
82	if (err < 0)
83#endif
84	{
85		unsigned long args[4] = { BRCTL_ADD_IF, ifindex, 0, 0 };
86
87		ifr.ifr_data = (char *) args;
88		err = ioctl(br_socket_fd, SIOCDEVPRIVATE, &ifr);
89	}
90
91	return err < 0 ? errno : 0;
92}
93
94int br_del_interface(const char *bridge, const char *dev)
95{
96	struct ifreq ifr;
97	int err;
98	int ifindex = if_nametoindex(dev);
99
100	if (ifindex == 0)
101		return ENODEV;
102
103	strncpy(ifr.ifr_name, bridge, IFNAMSIZ);
104#ifdef SIOCBRDELIF
105	ifr.ifr_ifindex = ifindex;
106	err = ioctl(br_socket_fd, SIOCBRDELIF, &ifr);
107	if (err < 0)
108#endif
109	{
110		unsigned long args[4] = { BRCTL_DEL_IF, ifindex, 0, 0 };
111
112		ifr.ifr_data = (char *) args;
113		err = ioctl(br_socket_fd, SIOCDEVPRIVATE, &ifr);
114	}
115
116	return err < 0 ? errno : 0;
117}
118