forked from GNS3/dynamips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatm_bridge.c
368 lines (297 loc) · 8.7 KB
/
atm_bridge.c
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* Cisco router simulation platform.
* Copyright (c) 2007 Christophe Fillot ([email protected])
*
* ATM bridge (RFC1483)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include "utils.h"
#include "registry.h"
#include "net_io.h"
#include "atm.h"
#include "atm_vsar.h"
#include "atm_bridge.h"
#define ATM_BRIDGE_LOCK(t) pthread_mutex_lock(&(t)->lock)
#define ATM_BRIDGE_UNLOCK(t) pthread_mutex_unlock(&(t)->lock)
/* Acquire a reference to an ATM bridge (increment reference count) */
atm_bridge_t *atm_bridge_acquire(char *name)
{
return(registry_find(name,OBJ_TYPE_ATM_BRIDGE));
}
/* Release an ATM switch (decrement reference count) */
int atm_bridge_release(char *name)
{
return(registry_unref(name,OBJ_TYPE_ATM_BRIDGE));
}
/* Receive an ATM cell */
static int atm_bridge_recv_cell(netio_desc_t *nio,
u_char *atm_cell,ssize_t cell_len,
atm_bridge_t *t)
{
m_uint32_t atm_hdr,vpi,vci;
int status,res = 0;
if (cell_len != ATM_CELL_SIZE)
return(-1);
ATM_BRIDGE_LOCK(t);
/* check the VPI/VCI */
atm_hdr = m_ntoh32(atm_cell);
vpi = (atm_hdr & ATM_HDR_VPI_MASK) >> ATM_HDR_VPI_SHIFT;
vci = (atm_hdr & ATM_HDR_VCI_MASK) >> ATM_HDR_VCI_SHIFT;
if ((t->vpi != vpi) || (t->vci != vci))
goto done;
if ((status = atm_aal5_recv(&t->arc,atm_cell)) == 1) {
/* Got AAL5 packet, check RFC1483b encapsulation */
if ((t->arc.len > ATM_RFC1483B_HLEN) &&
!memcmp(t->arc.buffer,atm_rfc1483b_header,ATM_RFC1483B_HLEN))
{
netio_send(t->eth_nio,
t->arc.buffer+ATM_RFC1483B_HLEN,
t->arc.len-ATM_RFC1483B_HLEN);
}
atm_aal5_recv_reset(&t->arc);
} else {
if (status < 0) {
atm_aal5_recv_reset(&t->arc);
res = -1;
}
}
done:
ATM_BRIDGE_UNLOCK(t);
return(res);
}
/* Receive an Ethernet packet */
static int atm_bridge_recv_pkt(netio_desc_t *nio,u_char *pkt,ssize_t len,
atm_bridge_t *t)
{
return(atm_aal5_send_rfc1483b(t->atm_nio,t->vpi,t->vci,pkt,len));
}
/* Create a virtual ATM bridge */
atm_bridge_t *atm_bridge_create(char *name)
{
atm_bridge_t *t;
/* Allocate a new switch structure */
if (!(t = malloc(sizeof(*t))))
return NULL;
memset(t,0,sizeof(*t));
pthread_mutex_init(&t->lock,NULL);
atm_aal5_recv_reset(&t->arc);
if (!(t->name = strdup(name)))
goto err_name;
/* Record this object in registry */
if (registry_add(t->name,OBJ_TYPE_ATM_BRIDGE,t) == -1) {
fprintf(stderr,"atm_bridge_create: unable to create bridge '%s'\n",
name);
goto err_reg;
}
return t;
err_reg:
free(t->name);
err_name:
free(t);
return NULL;
}
/* Configure an ATM bridge */
int atm_bridge_configure(atm_bridge_t *t,char *eth_nio,
char *atm_nio,u_int vpi,u_int vci)
{
netio_desc_t *e_nio,*a_nio;
ATM_BRIDGE_LOCK(t);
if (t->eth_nio || t->atm_nio)
goto error;
e_nio = netio_acquire(eth_nio);
a_nio = netio_acquire(atm_nio);
if (!e_nio || !a_nio)
goto error;
t->eth_nio = e_nio;
t->atm_nio = a_nio;
t->vpi = vpi;
t->vci = vci;
/* Add ATM RX listener */
if (netio_rxl_add(t->atm_nio,(netio_rx_handler_t)atm_bridge_recv_cell,
t,NULL) == -1)
goto error;
/* Add Ethernet RX listener */
if (netio_rxl_add(t->eth_nio,(netio_rx_handler_t)atm_bridge_recv_pkt,
t,NULL) == -1)
goto error;
ATM_BRIDGE_UNLOCK(t);
return(0);
error:
ATM_BRIDGE_UNLOCK(t);
return(-1);
}
/* Release NIO used by an ATM bridge */
static void atm_bridge_clear_config(atm_bridge_t *t)
{
if (t != NULL) {
/* release ethernet NIO */
if (t->eth_nio) {
netio_rxl_remove(t->eth_nio);
netio_release(t->eth_nio->name);
}
/* release ATM NIO */
if (t->atm_nio) {
netio_rxl_remove(t->atm_nio);
netio_release(t->atm_nio->name);
}
t->eth_nio = t->atm_nio = NULL;
}
}
/* Unconfigure an ATM bridge */
int atm_bridge_unconfigure(atm_bridge_t *t)
{
ATM_BRIDGE_LOCK(t);
atm_bridge_clear_config(t);
ATM_BRIDGE_UNLOCK(t);
return(0);
}
/* Free resources used by an ATM bridge */
static int atm_bridge_free(void *data,void *arg)
{
atm_bridge_t *t = data;
atm_bridge_clear_config(t);
free(t->name);
free(t);
return(TRUE);
}
/* Delete an ATM bridge */
int atm_bridge_delete(char *name)
{
return(registry_delete_if_unused(name,OBJ_TYPE_ATM_BRIDGE,
atm_bridge_free,NULL));
}
/* Delete all ATM switches */
int atm_bridge_delete_all(void)
{
return(registry_delete_type(OBJ_TYPE_ATM_BRIDGE,atm_bridge_free,NULL));
}
/* Create a new interface */
int atm_bridge_cfg_create_if(atm_bridge_t *t,char **tokens,int count)
{
netio_desc_t *nio = NULL;
int nio_type;
/* at least: IF, interface name, NetIO type */
if (count < 3) {
fprintf(stderr,"atmsw_cfg_create_if: invalid interface description\n");
return(-1);
}
nio_type = netio_get_type(tokens[2]);
switch(nio_type) {
case NETIO_TYPE_UNIX:
if (count != 5) {
fprintf(stderr,"ATMSW: invalid number of arguments "
"for UNIX NIO '%s'\n",tokens[1]);
break;
}
nio = netio_desc_create_unix(tokens[1],tokens[3],tokens[4]);
break;
case NETIO_TYPE_UDP:
if (count != 6) {
fprintf(stderr,"ATMSW: invalid number of arguments "
"for UDP NIO '%s'\n",tokens[1]);
break;
}
nio = netio_desc_create_udp(tokens[1],atoi(tokens[3]),
tokens[4],atoi(tokens[5]));
break;
case NETIO_TYPE_TCP_CLI:
if (count != 5) {
fprintf(stderr,"ATMSW: invalid number of arguments "
"for TCP CLI NIO '%s'\n",tokens[1]);
break;
}
nio = netio_desc_create_tcp_cli(tokens[1],tokens[3],tokens[4]);
break;
case NETIO_TYPE_TCP_SER:
if (count != 4) {
fprintf(stderr,"ATMSW: invalid number of arguments "
"for TCP SER NIO '%s'\n",tokens[1]);
break;
}
nio = netio_desc_create_tcp_ser(tokens[1],tokens[3]);
break;
default:
fprintf(stderr,"ATMSW: unknown/invalid NETIO type '%s'\n",
tokens[2]);
}
if (!nio) {
fprintf(stderr,"ATMSW: unable to create NETIO descriptor of "
"interface %s\n",tokens[1]);
return(-1);
}
netio_release(nio->name);
return(0);
}
/* Bridge setup */
int atm_bridge_cfg_setup(atm_bridge_t *t,char **tokens,int count)
{
/* 5 parameters: "BRIDGE", Eth_IF, ATM_IF, VPI, VCI */
if (count != 5) {
fprintf(stderr,"ATM Bridge: invalid VPC descriptor.\n");
return(-1);
}
return(atm_bridge_configure(t,tokens[1],tokens[2],
atoi(tokens[3]),atoi(tokens[4])));
}
#define ATM_BRIDGE_MAX_TOKENS 16
/* Handle an ATMSW configuration line */
int atm_bridge_handle_cfg_line(atm_bridge_t *t,char *str)
{
char *tokens[ATM_BRIDGE_MAX_TOKENS];
int count;
if ((count = m_strsplit(str,':',tokens,ATM_BRIDGE_MAX_TOKENS)) <= 1)
return(-1);
if (!strcmp(tokens[0],"IF"))
return(atm_bridge_cfg_create_if(t,tokens,count));
else if (!strcmp(tokens[0],"BRIDGE"))
return(atm_bridge_cfg_setup(t,tokens,count));
fprintf(stderr,"ATM Bridge: "
"Unknown statement \"%s\" (allowed: IF,BRIDGE)\n",
tokens[0]);
return(-1);
}
/* Read an ATM bridge configuration file */
int atm_bridge_read_cfg_file(atm_bridge_t *t,char *filename)
{
char buffer[1024],*ptr;
FILE *fd;
if (!(fd = fopen(filename,"r"))) {
perror("fopen");
return(-1);
}
while(!feof(fd)) {
if (!fgets(buffer,sizeof(buffer),fd))
break;
/* skip comments and end of line */
if ((ptr = strpbrk(buffer,"#\r\n")) != NULL)
*ptr = 0;
/* analyze non-empty lines */
if (strchr(buffer,':'))
atm_bridge_handle_cfg_line(t,buffer);
}
fclose(fd);
return(0);
}
/* Start a virtual ATM bridge */
int atm_bridge_start(char *filename)
{
atm_bridge_t *t;
if (!(t = atm_bridge_create("default"))) {
fprintf(stderr,"ATM Bridge: unable to create virtual fabric table.\n");
return(-1);
}
if (atm_bridge_read_cfg_file(t,filename) == -1) {
fprintf(stderr,"ATM Bridge: unable to parse configuration file.\n");
return(-1);
}
atm_bridge_release("default");
return(0);
}