summaryrefslogtreecommitdiff
path: root/release/src/router/bridge/libbridge/libbridge_init.c
blob: 787c467f182b6755e48a31b8c7991176476749b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 * $Id: libbridge_init.c,v 1.1.1.4 2003/10/14 08:09:37 sparq Exp $
 *
 * Copyright (C) 2000 Lennert Buytenhek
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include "libbridge.h"
#include "libbridge_private.h"

int br_socket_fd;
struct bridge *bridge_list;

static void __bridge_info_copy(struct bridge_info *info, struct __bridge_info *i)
{
	memcpy(&info->designated_root, &i->designated_root, 8);
	memcpy(&info->bridge_id, &i->bridge_id, 8);
	info->root_path_cost = i->root_path_cost;
	info->topology_change = i->topology_change;
	info->topology_change_detected = i->topology_change_detected;
	info->root_port = i->root_port;
	info->stp_enabled = i->stp_enabled;
	__jiffies_to_tv(&info->max_age, i->max_age);
	__jiffies_to_tv(&info->hello_time, i->hello_time);
	__jiffies_to_tv(&info->forward_delay, i->forward_delay);
	__jiffies_to_tv(&info->bridge_max_age, i->bridge_max_age);
	__jiffies_to_tv(&info->bridge_hello_time, i->bridge_hello_time);
	__jiffies_to_tv(&info->bridge_forward_delay, i->bridge_forward_delay);
	__jiffies_to_tv(&info->ageing_time, i->ageing_time);
	__jiffies_to_tv(&info->gc_interval, i->gc_interval);
	__jiffies_to_tv(&info->hello_timer_value, i->hello_timer_value);
	__jiffies_to_tv(&info->tcn_timer_value, i->tcn_timer_value);
	__jiffies_to_tv(&info->topology_change_timer_value,
			i->topology_change_timer_value);
	__jiffies_to_tv(&info->gc_timer_value, i->gc_timer_value);
}

static void __port_info_copy(struct port_info *info, struct __port_info *i)
{
	memcpy(&info->designated_root, &i->designated_root, 8);
	memcpy(&info->designated_bridge, &i->designated_bridge, 8);
	info->port_id = i->port_id;
	info->designated_port = i->designated_port;
	info->path_cost = i->path_cost;
	info->designated_cost = i->designated_cost;
	info->state = i->state;
	info->top_change_ack = i->top_change_ack;
	info->config_pending = i->config_pending;
	__jiffies_to_tv(&info->message_age_timer_value,
			i->message_age_timer_value);
	__jiffies_to_tv(&info->forward_delay_timer_value,
			i->forward_delay_timer_value);
	__jiffies_to_tv(&info->hold_timer_value,
			i->hold_timer_value);
}

int br_read_info(struct bridge *br)
{
	struct __bridge_info i;

	if (if_indextoname(br->ifindex, br->ifname) == NULL)
		return 1;

	if (br_device_ioctl(br, BRCTL_GET_BRIDGE_INFO,
			    (unsigned long)&i, 0, 0) < 0)
		return 1;

	__bridge_info_copy(&br->info, &i);
	return 0;
}

int br_read_port_info(struct port *p)
{
	struct __port_info i;

	if (br_device_ioctl(p->parent, BRCTL_GET_PORT_INFO,
			    (unsigned long)&i, p->index, 0) < 0)
		return errno;

	__port_info_copy(&p->info, &i);
	return 0;
}

void br_nuke_bridge(struct bridge *b)
{
	struct port *p;

	p = b->firstport;
	while (p != NULL) {
		struct port *pnext;

		pnext = p->next;
		free(p);
		p = pnext;
	}

	free(b);
}

int br_make_port_list(struct bridge *br)
{
	int err;
	int i;
	int ifindices[256];

	if (br_device_ioctl(br, BRCTL_GET_PORT_LIST, (unsigned long)ifindices,
			    0, 0) < 0)
		return errno;

	for (i=255;i>=0;i--) {
		struct port *p;

		if (!ifindices[i])
			continue;

		p = malloc(sizeof(struct port));
		p->index = i;
		p->ifindex = ifindices[i];
		p->parent = br;
		br->ports[i] = p;
		p->next = br->firstport;
		br->firstport = p;
		if ((err = br_read_port_info(p)) != 0)
			goto error_out;
	}

	return 0;

 error_out:
	while (++i < 256)
		free(br->ports[i]);

	return err;
}

int br_make_bridge_list()
{
	int err;
	int i;
	int ifindices[32];
	int num;

	num = br_ioctl(BRCTL_GET_BRIDGES, (unsigned long)ifindices, 32);
	if (num < 0)
		return errno;

	bridge_list = NULL;
	for (i=0;i<num;i++) {
		struct bridge *br;

		br = malloc(sizeof(struct bridge));
		memset(br, 0, sizeof(struct bridge));
		br->ifindex = ifindices[i];
		br->firstport = NULL;
		br->next = bridge_list;
		bridge_list = br;
		if ((err = br_read_info(br)) != 0)
			goto error_out;
		if ((err = br_make_port_list(br)) != 0)
			goto error_out;
	}

	return 0;

 error_out:
	while (bridge_list != NULL) {
		struct bridge *nxt;

		nxt = bridge_list->next;
		br_nuke_bridge(bridge_list);
		bridge_list = nxt;
	}

	return err;
}

int br_init()
{
	int err;

	if ((br_socket_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
		return errno;

	if (br_get_version() != BRCTL_VERSION)
		return 12345;

	if ((err = br_make_bridge_list()) != 0)
		return err;

	return 0;
}

int br_refresh()
{
	struct bridge *b;

	b = bridge_list;
	while (b != NULL) {
		struct bridge *bnext;

		bnext = b->next;
		br_nuke_bridge(b);
		b = bnext;
	}

	return br_make_bridge_list();
}