| 1 |
/* |
| 2 |
* Cisco router simulation platform. |
| 3 |
* Copyright (c) 2007 Christophe Fillot ([email protected]) |
| 4 |
* |
| 5 |
* ATM bridge (RFC1483) |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <string.h> |
| 11 |
#include <unistd.h> |
| 12 |
#include <pthread.h> |
| 13 |
#include <errno.h> |
| 14 |
#include <sys/select.h> |
| 15 |
#include <sys/time.h> |
| 16 |
#include <sys/types.h> |
| 17 |
|
| 18 |
#include "utils.h" |
| 19 |
#include "registry.h" |
| 20 |
#include "net_io.h" |
| 21 |
#include "atm.h" |
| 22 |
#include "atm_vsar.h" |
| 23 |
#include "atm_bridge.h" |
| 24 |
|
| 25 |
#define ATM_BRIDGE_LOCK(t) pthread_mutex_lock(&(t)->lock) |
| 26 |
#define ATM_BRIDGE_UNLOCK(t) pthread_mutex_unlock(&(t)->lock) |
| 27 |
|
| 28 |
/* Acquire a reference to an ATM bridge (increment reference count) */ |
| 29 |
atm_bridge_t *atm_bridge_acquire(char *name) |
| 30 |
{ |
| 31 |
return(registry_find(name,OBJ_TYPE_ATM_BRIDGE)); |
| 32 |
} |
| 33 |
|
| 34 |
/* Release an ATM switch (decrement reference count) */ |
| 35 |
int atm_bridge_release(char *name) |
| 36 |
{ |
| 37 |
return(registry_unref(name,OBJ_TYPE_ATM_BRIDGE)); |
| 38 |
} |
| 39 |
|
| 40 |
/* Receive an ATM cell */ |
| 41 |
static int atm_bridge_recv_cell(netio_desc_t *nio, |
| 42 |
u_char *atm_cell,ssize_t cell_len, |
| 43 |
atm_bridge_t *t) |
| 44 |
{ |
| 45 |
m_uint32_t atm_hdr,vpi,vci; |
| 46 |
int status,res = 0; |
| 47 |
|
| 48 |
if (cell_len != ATM_CELL_SIZE) |
| 49 |
return(-1); |
| 50 |
|
| 51 |
ATM_BRIDGE_LOCK(t); |
| 52 |
|
| 53 |
/* check the VPI/VCI */ |
| 54 |
atm_hdr = m_ntoh32(atm_cell); |
| 55 |
|
| 56 |
vpi = (atm_hdr & ATM_HDR_VPI_MASK) >> ATM_HDR_VPI_SHIFT; |
| 57 |
vci = (atm_hdr & ATM_HDR_VCI_MASK) >> ATM_HDR_VCI_SHIFT; |
| 58 |
|
| 59 |
if ((t->vpi != vpi) || (t->vci != vci)) |
| 60 |
goto done; |
| 61 |
|
| 62 |
if ((status = atm_aal5_recv(&t->arc,atm_cell)) == 1) { |
| 63 |
/* Got AAL5 packet, check RFC1483b encapsulation */ |
| 64 |
if ((t->arc.len > ATM_RFC1483B_HLEN) && |
| 65 |
!memcmp(t->arc.buffer,atm_rfc1483b_header,ATM_RFC1483B_HLEN)) |
| 66 |
{ |
| 67 |
netio_send(t->eth_nio, |
| 68 |
t->arc.buffer+ATM_RFC1483B_HLEN, |
| 69 |
t->arc.len-ATM_RFC1483B_HLEN); |
| 70 |
} |
| 71 |
|
| 72 |
atm_aal5_recv_reset(&t->arc); |
| 73 |
} else { |
| 74 |
if (status < 0) { |
| 75 |
atm_aal5_recv_reset(&t->arc); |
| 76 |
res = -1; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
done: |
| 81 |
ATM_BRIDGE_UNLOCK(t); |
| 82 |
return(res); |
| 83 |
} |
| 84 |
|
| 85 |
/* Receive an Ethernet packet */ |
| 86 |
static int atm_bridge_recv_pkt(netio_desc_t *nio,u_char *pkt,ssize_t len, |
| 87 |
atm_bridge_t *t) |
| 88 |
{ |
| 89 |
return(atm_aal5_send_rfc1483b(t->atm_nio,t->vpi,t->vci,pkt,len)); |
| 90 |
} |
| 91 |
|
| 92 |
/* Create a virtual ATM bridge */ |
| 93 |
atm_bridge_t *atm_bridge_create(char *name) |
| 94 |
{ |
| 95 |
atm_bridge_t *t; |
| 96 |
|
| 97 |
/* Allocate a new switch structure */ |
| 98 |
if (!(t = malloc(sizeof(*t)))) |
| 99 |
return NULL; |
| 100 |
|
| 101 |
memset(t,0,sizeof(*t)); |
| 102 |
pthread_mutex_init(&t->lock,NULL); |
| 103 |
atm_aal5_recv_reset(&t->arc); |
| 104 |
|
| 105 |
if (!(t->name = strdup(name))) |
| 106 |
goto err_name; |
| 107 |
|
| 108 |
/* Record this object in registry */ |
| 109 |
if (registry_add(t->name,OBJ_TYPE_ATM_BRIDGE,t) == -1) { |
| 110 |
fprintf(stderr,"atm_bridge_create: unable to create bridge '%s'\n", |
| 111 |
name); |
| 112 |
goto err_reg; |
| 113 |
} |
| 114 |
|
| 115 |
return t; |
| 116 |
|
| 117 |
err_reg: |
| 118 |
free(t->name); |
| 119 |
err_name: |
| 120 |
free(t); |
| 121 |
return NULL; |
| 122 |
} |
| 123 |
|
| 124 |
/* Configure an ATM bridge */ |
| 125 |
int atm_bridge_configure(atm_bridge_t *t,char *eth_nio, |
| 126 |
char *atm_nio,u_int vpi,u_int vci) |
| 127 |
{ |
| 128 |
netio_desc_t *e_nio,*a_nio; |
| 129 |
|
| 130 |
ATM_BRIDGE_LOCK(t); |
| 131 |
|
| 132 |
if (t->eth_nio || t->atm_nio) |
| 133 |
goto error; |
| 134 |
|
| 135 |
e_nio = netio_acquire(eth_nio); |
| 136 |
a_nio = netio_acquire(atm_nio); |
| 137 |
|
| 138 |
if (!e_nio || !a_nio) |
| 139 |
goto error; |
| 140 |
|
| 141 |
t->eth_nio = e_nio; |
| 142 |
t->atm_nio = a_nio; |
| 143 |
t->vpi = vpi; |
| 144 |
t->vci = vci; |
| 145 |
|
| 146 |
/* Add ATM RX listener */ |
| 147 |
if (netio_rxl_add(t->atm_nio,(netio_rx_handler_t)atm_bridge_recv_cell, |
| 148 |
t,NULL) == -1) |
| 149 |
goto error; |
| 150 |
|
| 151 |
/* Add Ethernet RX listener */ |
| 152 |
if (netio_rxl_add(t->eth_nio,(netio_rx_handler_t)atm_bridge_recv_pkt, |
| 153 |
t,NULL) == -1) |
| 154 |
goto error; |
| 155 |
|
| 156 |
ATM_BRIDGE_UNLOCK(t); |
| 157 |
return(0); |
| 158 |
|
| 159 |
error: |
| 160 |
ATM_BRIDGE_UNLOCK(t); |
| 161 |
return(-1); |
| 162 |
} |
| 163 |
|
| 164 |
/* Release NIO used by an ATM bridge */ |
| 165 |
static void atm_bridge_clear_config(atm_bridge_t *t) |
| 166 |
{ |
| 167 |
if (t != NULL) { |
| 168 |
/* release ethernet NIO */ |
| 169 |
if (t->eth_nio) { |
| 170 |
netio_rxl_remove(t->eth_nio); |
| 171 |
netio_release(t->eth_nio->name); |
| 172 |
} |
| 173 |
|
| 174 |
/* release ATM NIO */ |
| 175 |
if (t->atm_nio) { |
| 176 |
netio_rxl_remove(t->atm_nio); |
| 177 |
netio_release(t->atm_nio->name); |
| 178 |
} |
| 179 |
|
| 180 |
t->eth_nio = t->atm_nio = NULL; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
/* Unconfigure an ATM bridge */ |
| 185 |
int atm_bridge_unconfigure(atm_bridge_t *t) |
| 186 |
{ |
| 187 |
ATM_BRIDGE_LOCK(t); |
| 188 |
atm_bridge_clear_config(t); |
| 189 |
ATM_BRIDGE_UNLOCK(t); |
| 190 |
return(0); |
| 191 |
} |
| 192 |
|
| 193 |
/* Free resources used by an ATM bridge */ |
| 194 |
static int atm_bridge_free(void *data,void *arg) |
| 195 |
{ |
| 196 |
atm_bridge_t *t = data; |
| 197 |
|
| 198 |
atm_bridge_clear_config(t); |
| 199 |
free(t->name); |
| 200 |
free(t); |
| 201 |
return(TRUE); |
| 202 |
} |
| 203 |
|
| 204 |
/* Delete an ATM bridge */ |
| 205 |
int atm_bridge_delete(char *name) |
| 206 |
{ |
| 207 |
return(registry_delete_if_unused(name,OBJ_TYPE_ATM_BRIDGE, |
| 208 |
atm_bridge_free,NULL)); |
| 209 |
} |
| 210 |
|
| 211 |
/* Delete all ATM switches */ |
| 212 |
int atm_bridge_delete_all(void) |
| 213 |
{ |
| 214 |
return(registry_delete_type(OBJ_TYPE_ATM_BRIDGE,atm_bridge_free,NULL)); |
| 215 |
} |
| 216 |
|
| 217 |
/* Create a new interface */ |
| 218 |
int atm_bridge_cfg_create_if(atm_bridge_t *t,char **tokens,int count) |
| 219 |
{ |
| 220 |
netio_desc_t *nio = NULL; |
| 221 |
int nio_type; |
| 222 |
|
| 223 |
/* at least: IF, interface name, NetIO type */ |
| 224 |
if (count < 3) { |
| 225 |
fprintf(stderr,"atmsw_cfg_create_if: invalid interface description\n"); |
| 226 |
return(-1); |
| 227 |
} |
| 228 |
|
| 229 |
nio_type = netio_get_type(tokens[2]); |
| 230 |
switch(nio_type) { |
| 231 |
case NETIO_TYPE_UNIX: |
| 232 |
if (count != 5) { |
| 233 |
fprintf(stderr,"ATMSW: invalid number of arguments " |
| 234 |
"for UNIX NIO '%s'\n",tokens[1]); |
| 235 |
break; |
| 236 |
} |
| 237 |
|
| 238 |
nio = netio_desc_create_unix(tokens[1],tokens[3],tokens[4]); |
| 239 |
break; |
| 240 |
|
| 241 |
case NETIO_TYPE_UDP: |
| 242 |
if (count != 6) { |
| 243 |
fprintf(stderr,"ATMSW: invalid number of arguments " |
| 244 |
"for UDP NIO '%s'\n",tokens[1]); |
| 245 |
break; |
| 246 |
} |
| 247 |
|
| 248 |
nio = netio_desc_create_udp(tokens[1],atoi(tokens[3]), |
| 249 |
tokens[4],atoi(tokens[5])); |
| 250 |
break; |
| 251 |
|
| 252 |
case NETIO_TYPE_TCP_CLI: |
| 253 |
if (count != 5) { |
| 254 |
fprintf(stderr,"ATMSW: invalid number of arguments " |
| 255 |
"for TCP CLI NIO '%s'\n",tokens[1]); |
| 256 |
break; |
| 257 |
} |
| 258 |
|
| 259 |
nio = netio_desc_create_tcp_cli(tokens[1],tokens[3],tokens[4]); |
| 260 |
break; |
| 261 |
|
| 262 |
case NETIO_TYPE_TCP_SER: |
| 263 |
if (count != 4) { |
| 264 |
fprintf(stderr,"ATMSW: invalid number of arguments " |
| 265 |
"for TCP SER NIO '%s'\n",tokens[1]); |
| 266 |
break; |
| 267 |
} |
| 268 |
|
| 269 |
nio = netio_desc_create_tcp_ser(tokens[1],tokens[3]); |
| 270 |
break; |
| 271 |
|
| 272 |
default: |
| 273 |
fprintf(stderr,"ATMSW: unknown/invalid NETIO type '%s'\n", |
| 274 |
tokens[2]); |
| 275 |
} |
| 276 |
|
| 277 |
if (!nio) { |
| 278 |
fprintf(stderr,"ATMSW: unable to create NETIO descriptor of " |
| 279 |
"interface %s\n",tokens[1]); |
| 280 |
return(-1); |
| 281 |
} |
| 282 |
|
| 283 |
netio_release(nio->name); |
| 284 |
return(0); |
| 285 |
} |
| 286 |
|
| 287 |
/* Bridge setup */ |
| 288 |
int atm_bridge_cfg_setup(atm_bridge_t *t,char **tokens,int count) |
| 289 |
{ |
| 290 |
/* 5 parameters: "BRIDGE", Eth_IF, ATM_IF, VPI, VCI */ |
| 291 |
if (count != 5) { |
| 292 |
fprintf(stderr,"ATM Bridge: invalid VPC descriptor.\n"); |
| 293 |
return(-1); |
| 294 |
} |
| 295 |
|
| 296 |
return(atm_bridge_configure(t,tokens[1],tokens[2], |
| 297 |
atoi(tokens[3]),atoi(tokens[4]))); |
| 298 |
} |
| 299 |
|
| 300 |
#define ATM_BRIDGE_MAX_TOKENS 16 |
| 301 |
|
| 302 |
/* Handle an ATMSW configuration line */ |
| 303 |
int atm_bridge_handle_cfg_line(atm_bridge_t *t,char *str) |
| 304 |
{ |
| 305 |
char *tokens[ATM_BRIDGE_MAX_TOKENS]; |
| 306 |
int count; |
| 307 |
|
| 308 |
if ((count = m_strsplit(str,':',tokens,ATM_BRIDGE_MAX_TOKENS)) <= 1) |
| 309 |
return(-1); |
| 310 |
|
| 311 |
if (!strcmp(tokens[0],"IF")) |
| 312 |
return(atm_bridge_cfg_create_if(t,tokens,count)); |
| 313 |
else if (!strcmp(tokens[0],"BRIDGE")) |
| 314 |
return(atm_bridge_cfg_setup(t,tokens,count)); |
| 315 |
|
| 316 |
fprintf(stderr,"ATM Bridge: " |
| 317 |
"Unknown statement \"%s\" (allowed: IF,BRIDGE)\n", |
| 318 |
tokens[0]); |
| 319 |
return(-1); |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
/* Read an ATM bridge configuration file */ |
| 324 |
int atm_bridge_read_cfg_file(atm_bridge_t *t,char *filename) |
| 325 |
{ |
| 326 |
char buffer[1024],*ptr; |
| 327 |
FILE *fd; |
| 328 |
|
| 329 |
if (!(fd = fopen(filename,"r"))) { |
| 330 |
perror("fopen"); |
| 331 |
return(-1); |
| 332 |
} |
| 333 |
|
| 334 |
while(!feof(fd)) { |
| 335 |
if (!fgets(buffer,sizeof(buffer),fd)) |
| 336 |
break; |
| 337 |
|
| 338 |
/* skip comments and end of line */ |
| 339 |
if ((ptr = strpbrk(buffer,"#\r\n")) != NULL) |
| 340 |
*ptr = 0; |
| 341 |
|
| 342 |
/* analyze non-empty lines */ |
| 343 |
if (strchr(buffer,':')) |
| 344 |
atm_bridge_handle_cfg_line(t,buffer); |
| 345 |
} |
| 346 |
|
| 347 |
fclose(fd); |
| 348 |
return(0); |
| 349 |
} |
| 350 |
|
| 351 |
/* Start a virtual ATM bridge */ |
| 352 |
int atm_bridge_start(char *filename) |
| 353 |
{ |
| 354 |
atm_bridge_t *t; |
| 355 |
|
| 356 |
if (!(t = atm_bridge_create("default"))) { |
| 357 |
fprintf(stderr,"ATM Bridge: unable to create virtual fabric table.\n"); |
| 358 |
return(-1); |
| 359 |
} |
| 360 |
|
| 361 |
if (atm_bridge_read_cfg_file(t,filename) == -1) { |
| 362 |
fprintf(stderr,"ATM Bridge: unable to parse configuration file.\n"); |
| 363 |
return(-1); |
| 364 |
} |
| 365 |
|
| 366 |
atm_bridge_release("default"); |
| 367 |
return(0); |
| 368 |
} |