-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsr_cpu_extension_nf2.c
340 lines (299 loc) · 9.81 KB
/
sr_cpu_extension_nf2.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
/*-----------------------------------------------------------------------------
* file: sr_cpu_extension_nf2.c
* date: Mon Feb 09 16:58:30 PST 2004
* Author: Martin Casado
*
* 2007-Apr-04 04:57:55 AM - Modified to support NetFPGA v2.1 /mc
*
* Description:
*
*---------------------------------------------------------------------------*/
#include "sr_cpu_extension_nf2.h"
#include "sr_base_internal.h"
#include "sr_vns.h"
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <linux/netdevice.h>
#include <linux/sockios.h>
#include <netinet/in.h>
#include "sr_router.h"
#include "sr_interface.h"
#include "sr_integration.h"
#include "sr_ethernet.h"
#include "sr_dumper.h"
#include "real_socket_helper.h"
struct sr_ethernet_hdr
{
#ifndef ETHER_ADDR_LEN
#define ETHER_ADDR_LEN 6
#endif
uint8_t ether_dhost[ETHER_ADDR_LEN]; /* destination ethernet address */
uint8_t ether_shost[ETHER_ADDR_LEN]; /* source ethernet address */
uint16_t ether_type; /* packet type ID */
} __attribute__ ((packed)) ;
static char* copy_next_field(FILE* fp, char* line, char* buf);
static uint32_t asci_to_nboip(const char* ip);
static void asci_to_ether(const char* addr, uint8_t mac[6]);
/*-----------------------------------------------------------------------------
* Method: sr_cpu_init_hardware(..)
* scope: global
*
* Read information for each of the router's interfaces from hwfile
*
* format of the file is (1 interface per line)
*
* <name ip mask hwaddr>
*
* e.g.
*
* eth0 192.168.123.10 255.255.255.0 ca:fe:de:ad:be:ef
*
*---------------------------------------------------------------------------*/
int sr_cpu_init_hardware(struct sr_instance* sr, const char* hwfile)
{
struct sr_vns_if vns_if;
FILE* fp = 0;
char line[1024];
char buf[SR_NAMELEN];
char *tmpptr;
if ( (fp = fopen(hwfile, "r") ) == 0 )
{
fprintf(stderr, "Error: could not open cpu hardware info file: %s\n",
hwfile);
return -1;
}
Debug(" < -- Reading hw info from file %s -- >\n", hwfile);
while ( fgets( line, 1024, fp) )
{
line[1023] = 0; /* -- insurance :) -- */
/* -- read interface name into buf -- */
if(! (tmpptr = copy_next_field(fp, line, buf)) )
{
fclose(fp);
fprintf(stderr, "Bad formatting in cpu hardware file\n");
return 1;
}
Debug(" - Name [%s] ", buf);
strncpy(vns_if.name, buf, SR_NAMELEN);
/* -- read interface ip into buf -- */
if(! (tmpptr = copy_next_field(fp, tmpptr, buf)) )
{
fclose(fp);
fprintf(stderr, "Bad formatting in cpu hardware file\n");
return 1;
}
Debug(" IP [%s] ", buf);
vns_if.ip = asci_to_nboip(buf);
/* -- read interface mask into buf -- */
if(! (tmpptr = copy_next_field(fp, tmpptr, buf)) )
{
fclose(fp);
fprintf(stderr, "Bad formatting in cpu hardware file\n");
return 1;
}
Debug(" Mask [%s] ", buf);
vns_if.mask = asci_to_nboip(buf);
/* -- read interface hw address into buf -- */
if(! (tmpptr = copy_next_field(fp, tmpptr, buf)) )
{
fclose(fp);
fprintf(stderr, "Bad formatting in cpu hardware file\n");
return 1;
}
Debug(" MAC [%s]\n", buf);
asci_to_ether(buf, vns_if.addr);
sr_integ_add_interface(sr, &vns_if);
} /* -- while ( fgets ( .. ) ) -- */
Debug(" < -- -- >\n");
fclose(fp);
return 0;
} /* -- sr_cpu_init_hardware -- */
/*-----------------------------------------------------------------------------
* Method: sr_cpu_input(..)
* Scope: Local
*
*---------------------------------------------------------------------------*/
int sr_cpu_input(struct sr_instance* sr)
{
/* REQUIRES */
assert(sr);
/*
* Note: To log incoming packets, use sr_log_packet from sr_dumper.[c,h]
*/
/* RETURN 1 on success, 0 on failure.
* Note: With a 0 result, the router will shut-down
*/
printf(" ** sr_cpu_input(..) called\n");
#ifdef _CPUMODE_
byte buf[ETH_MAX_LEN];
unsigned int len = 0;
struct sr_router* router = (struct sr_router*)sr_get_subsystem(sr);
interface_t* intf;
fd_set rdset, errset;
int ret = 0, max_fd = -1, i;
struct timeval timeout;
while(1) {
// clear set
FD_ZERO(&rdset);
FD_ZERO(&errset);
max_fd = -1;
for(i = 0; i<router->num_interfaces; i++) {
// set fds for each interface, both read and error
FD_SET(router->interface[i].hw_fd, &rdset);
FD_SET(router->interface[i].hw_fd, &errset);
if(router->interface[i].hw_fd > max_fd)
max_fd = router->interface[i].hw_fd;
}
// set the timeout at 1 sec
timeout.tv_sec = 1;
timeout.tv_usec = 0;
// call select
ret = select(max_fd + 1, &rdset, NULL, &errset, &timeout);
// now check each interface fd
for(i = 0; i<router->num_interfaces; i++) {
intf = &router->interface[i];
// check if interface was enabled
if(intf->enabled) {
// check if fd is set
if(FD_ISSET(intf->hw_fd, &rdset)) {
// now read data
len = real_read_once(intf->hw_fd, buf, ETH_MAX_LEN);
// if data len is <=0
if(len <= 0)
printf(" ** sr_cpu_input(..) length error on interface %s", intf->name);
else {
// send to router
sr_integ_input(sr, buf, len, intf->name);
// log packet
sr_log_packet(sr, buf, len) ;
return 1;
}
}
// check if error fd is set
if(FD_ISSET(intf->hw_fd, &errset)) {
printf(" ** sr_cpu_input(..) error %s\n", intf->name);
return 0;
}
}
}
}
#endif
return 1;
} /* -- sr_cpu_input -- */
/*-----------------------------------------------------------------------------
* Method: sr_cpu_output(..)
* Scope: Global
*
*---------------------------------------------------------------------------*/
int sr_cpu_output(struct sr_instance* sr /* borrowed */,
uint8_t* buf /* borrowed */ ,
unsigned int len,
const char* iface /* borrowed */)
{
printf(" ** sr_cpu_output(..) called\n");
/* REQUIRES */
assert(sr);
assert(buf);
assert(iface);
#ifdef _CPUMODE_
struct sr_router* router = (struct sr_router*)sr_get_subsystem(sr);
// get interface instance
interface_t* intf = get_interface_name(router, iface);
pthread_mutex_lock(&intf->hw_lock);
int ret = real_writen(intf->hw_fd, buf, len);
pthread_mutex_unlock(&intf->hw_lock);
/* Return the length of the packet on success, -1 on failure */
return ret;
#endif
return 0;
} /* -- sr_cpu_output -- */
/*-----------------------------------------------------------------------------
* Method: copy_next_field(..)
* Scope: Local
*
*---------------------------------------------------------------------------*/
static
char* copy_next_field(FILE* fp, char* line, char* buf)
{
char* tmpptr = buf;
while ( *line && isspace((int)*line)) /* -- XXX: potential overrun here */
{ line++; }
if(! *line )
{ return 0; }
while ( *line && ! isspace((int)*line) && ((tmpptr - buf) < SR_NAMELEN))
{ *tmpptr++ = *line++; }
*tmpptr = 0;
return line;
} /* -- copy_next_field -- */
/*-----------------------------------------------------------------------------
* Method: asci_to_nboip(..)
* Scope: Local
*
*---------------------------------------------------------------------------*/
static uint32_t asci_to_nboip(const char* ip)
{
struct in_addr addr;
if ( inet_pton(AF_INET, ip, &addr) <= 0 )
{ return 0; } /* -- 0.0.0.0 unsupported so its ok .. yeah .. really -- */
return addr.s_addr;
} /* -- asci_to_nboip -- */
/*-----------------------------------------------------------------------------
* Method: asci_to_ether(..)
* Scope: Local
*
* Look away .. please ... just look away
*
*---------------------------------------------------------------------------*/
static void asci_to_ether(const char* addr, uint8_t mac[6])
{
uint32_t tmpint;
const char* buf = addr;
int i = 0;
for( i = 0; i < 6; ++i )
{
if (i)
{
while (*buf && *buf != ':')
{ buf++; }
buf++;
}
sscanf(buf, "%x", &tmpint);
mac[i] = tmpint & 0x000000ff;
}
} /* -- asci_to_ether -- */
void sr_cpu_init_interface_socket(sr_router* router) {
printf(" ** sr_cpu_init_interface(..) called\n");
char iface_name[32] = "nf2c";
int i;
for (i = 0; i < ROUTER_MAX_INTERFACES; ++i) {
sprintf(&(iface_name[4]), "%i", i);
int s = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
struct ifreq ifr;
bzero(&ifr, sizeof(struct ifreq));
strncpy(ifr.ifr_ifrn.ifrn_name, iface_name, IFNAMSIZ);
if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
perror("ioctl SIOCGIFINDEX");
exit(1);
}
struct sockaddr_ll saddr;
bzero(&saddr, sizeof(struct sockaddr_ll));
saddr.sll_family = AF_PACKET;
saddr.sll_protocol = htons(ETH_P_ALL);
saddr.sll_ifindex = ifr.ifr_ifru.ifru_ivalue;
if (bind(s, (struct sockaddr*)(&saddr), sizeof(saddr)) < 0) {
perror("bind error");
exit(1);
}
//add fd to interface hw_fd
#ifdef _CPUMODE_
router->interface[i].hw_fd = s;
#endif
}
}