forked from m-labs/milkymist
/
microudp.c
408 lines (348 loc) · 11.2 KB
/
microudp.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
* Milkymist SoC (Software)
* Copyright (C) 2007, 2008, 2009, 2010, 2011 Sebastien Bourdeauducq
*
* 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, version 3 of the License.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <crc.h>
#include <irq.h>
#include <system.h>
#include <hw/minimac.h>
#include <hw/sysctl.h>
#include <hw/interrupts.h>
#include <net/microudp.h>
#define ETHERTYPE_ARP 0x0806
#define ETHERTYPE_IP 0x0800
struct ethernet_header {
unsigned char preamble[8];
unsigned char destmac[6];
unsigned char srcmac[6];
unsigned short ethertype;
} __attribute__((packed));
static void fill_eth_header(struct ethernet_header *h, const unsigned char *destmac, const unsigned char *srcmac, unsigned short ethertype)
{
int i;
for(i=0;i<7;i++)
h->preamble[i] = 0x55;
h->preamble[7] = 0xd5;
for(i=0;i<6;i++)
h->destmac[i] = destmac[i];
for(i=0;i<6;i++)
h->srcmac[i] = srcmac[i];
h->ethertype = ethertype;
}
#define ARP_HWTYPE_ETHERNET 0x0001
#define ARP_PROTO_IP 0x0800
#define ARP_OPCODE_REQUEST 0x0001
#define ARP_OPCODE_REPLY 0x0002
struct arp_frame {
unsigned short hwtype;
unsigned short proto;
unsigned char hwsize;
unsigned char protosize;
unsigned short opcode;
unsigned char sender_mac[6];
unsigned int sender_ip;
unsigned char target_mac[6];
unsigned int target_ip;
unsigned char padding[18];
} __attribute__((packed));
#define IP_IPV4 0x45
#define IP_DONT_FRAGMENT 0x4000
#define IP_TTL 64
#define IP_PROTO_UDP 0x11
struct ip_header {
unsigned char version;
unsigned char diff_services;
unsigned short total_length;
unsigned short identification;
unsigned short fragment_offset;
unsigned char ttl;
unsigned char proto;
unsigned short checksum;
unsigned int src_ip;
unsigned int dst_ip;
} __attribute__((packed));
struct udp_header {
unsigned short src_port;
unsigned short dst_port;
unsigned short length;
unsigned short checksum;
} __attribute__((packed));
struct udp_frame {
struct ip_header ip;
struct udp_header udp;
char payload[];
} __attribute__((packed));
struct ethernet_frame {
struct ethernet_header eth_header;
union {
struct arp_frame arp;
struct udp_frame udp;
} contents;
} __attribute__((packed));
typedef union {
struct ethernet_frame frame;
unsigned char raw[1532];
} ethernet_buffer;
static int rxlen;
static ethernet_buffer *rxbuffer;
static ethernet_buffer *rxbuffer0;
static ethernet_buffer *rxbuffer1;
static int txlen;
static ethernet_buffer *txbuffer;
static void send_packet(void)
{
unsigned int crc;
crc = crc32(&txbuffer->raw[8], txlen-8);
txbuffer->raw[txlen ] = (crc & 0xff);
txbuffer->raw[txlen+1] = (crc & 0xff00) >> 8;
txbuffer->raw[txlen+2] = (crc & 0xff0000) >> 16;
txbuffer->raw[txlen+3] = (crc & 0xff000000) >> 24;
txlen += 4;
CSR_MINIMAC_TXCOUNT = txlen;
while((irq_pending() & IRQ_ETHTX) == 0);
irq_ack(IRQ_ETHTX);
}
static unsigned char my_mac[6];
static unsigned int my_ip;
/* ARP cache - one entry only */
static unsigned char cached_mac[6];
static unsigned int cached_ip;
static void process_arp(void)
{
if(rxlen < 68) return;
if(rxbuffer->frame.contents.arp.hwtype != ARP_HWTYPE_ETHERNET) return;
if(rxbuffer->frame.contents.arp.proto != ARP_PROTO_IP) return;
if(rxbuffer->frame.contents.arp.hwsize != 6) return;
if(rxbuffer->frame.contents.arp.protosize != 4) return;
if(rxbuffer->frame.contents.arp.opcode == ARP_OPCODE_REPLY) {
if(rxbuffer->frame.contents.arp.sender_ip == cached_ip) {
int i;
for(i=0;i<6;i++)
cached_mac[i] = rxbuffer->frame.contents.arp.sender_mac[i];
}
return;
}
if(rxbuffer->frame.contents.arp.opcode == ARP_OPCODE_REQUEST) {
if(rxbuffer->frame.contents.arp.target_ip == my_ip) {
int i;
fill_eth_header(&txbuffer->frame.eth_header,
rxbuffer->frame.contents.arp.sender_mac,
my_mac,
ETHERTYPE_ARP);
txlen = 68;
txbuffer->frame.contents.arp.hwtype = ARP_HWTYPE_ETHERNET;
txbuffer->frame.contents.arp.proto = ARP_PROTO_IP;
txbuffer->frame.contents.arp.hwsize = 6;
txbuffer->frame.contents.arp.protosize = 4;
txbuffer->frame.contents.arp.opcode = ARP_OPCODE_REPLY;
txbuffer->frame.contents.arp.sender_ip = my_ip;
for(i=0;i<6;i++)
txbuffer->frame.contents.arp.sender_mac[i] = my_mac[i];
txbuffer->frame.contents.arp.target_ip = rxbuffer->frame.contents.arp.sender_ip;
for(i=0;i<6;i++)
txbuffer->frame.contents.arp.target_mac[i] = rxbuffer->frame.contents.arp.sender_mac[i];
send_packet();
}
return;
}
}
static const unsigned char broadcast[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
int microudp_arp_resolve(unsigned int ip)
{
int i;
int tries;
int timeout;
if(cached_ip == ip) {
for(i=0;i<6;i++)
if(cached_mac[i]) return 1;
}
cached_ip = ip;
for(i=0;i<6;i++)
cached_mac[i] = 0;
for(tries=0;tries<5;tries++) {
/* Send an ARP request */
fill_eth_header(&txbuffer->frame.eth_header,
broadcast,
my_mac,
ETHERTYPE_ARP);
txlen = 68;
txbuffer->frame.contents.arp.hwtype = ARP_HWTYPE_ETHERNET;
txbuffer->frame.contents.arp.proto = ARP_PROTO_IP;
txbuffer->frame.contents.arp.hwsize = 6;
txbuffer->frame.contents.arp.protosize = 4;
txbuffer->frame.contents.arp.opcode = ARP_OPCODE_REQUEST;
txbuffer->frame.contents.arp.sender_ip = my_ip;
for(i=0;i<6;i++)
txbuffer->frame.contents.arp.sender_mac[i] = my_mac[i];
txbuffer->frame.contents.arp.target_ip = ip;
for(i=0;i<6;i++)
txbuffer->frame.contents.arp.target_mac[i] = 0;
send_packet();
/* Do we get a reply ? */
for(timeout=0;timeout<2000000;timeout++) {
microudp_service();
for(i=0;i<6;i++)
if(cached_mac[i]) return 1;
}
}
return 0;
}
static unsigned short ip_checksum(unsigned int r, void *buffer, unsigned int length, int complete)
{
unsigned char *ptr;
int i;
ptr = (unsigned char *)buffer;
length >>= 1;
for(i=0;i<length;i++)
r += ((unsigned int)(ptr[2*i]) << 8)|(unsigned int)(ptr[2*i+1]) ;
/* Add overflows */
while(r >> 16)
r = (r & 0xffff) + (r >> 16);
if(complete) {
r = ~r;
r &= 0xffff;
if(r == 0) r = 0xffff;
}
return r;
}
void *microudp_get_tx_buffer(void)
{
return txbuffer->frame.contents.udp.payload;
}
struct pseudo_header {
unsigned int src_ip;
unsigned int dst_ip;
unsigned char zero;
unsigned char proto;
unsigned short length;
} __attribute__((packed));
int microudp_send(unsigned short src_port, unsigned short dst_port, unsigned int length)
{
struct pseudo_header h;
unsigned int r;
if((cached_mac[0] == 0) && (cached_mac[1] == 0) && (cached_mac[2] == 0)
&& (cached_mac[3] == 0) && (cached_mac[4] == 0) && (cached_mac[5] == 0))
return 0;
txlen = length + sizeof(struct ethernet_header) + sizeof(struct udp_frame) + 8;
if(txlen < 72) txlen = 72;
fill_eth_header(&txbuffer->frame.eth_header,
cached_mac,
my_mac,
ETHERTYPE_IP);
txbuffer->frame.contents.udp.ip.version = IP_IPV4;
txbuffer->frame.contents.udp.ip.diff_services = 0;
txbuffer->frame.contents.udp.ip.total_length = length + sizeof(struct udp_frame);
txbuffer->frame.contents.udp.ip.identification = 0;
txbuffer->frame.contents.udp.ip.fragment_offset = IP_DONT_FRAGMENT;
txbuffer->frame.contents.udp.ip.ttl = IP_TTL;
h.proto = txbuffer->frame.contents.udp.ip.proto = IP_PROTO_UDP;
txbuffer->frame.contents.udp.ip.checksum = 0;
h.src_ip = txbuffer->frame.contents.udp.ip.src_ip = my_ip;
h.dst_ip = txbuffer->frame.contents.udp.ip.dst_ip = cached_ip;
txbuffer->frame.contents.udp.ip.checksum = ip_checksum(0, &txbuffer->frame.contents.udp.ip,
sizeof(struct ip_header), 1);
txbuffer->frame.contents.udp.udp.src_port = src_port;
txbuffer->frame.contents.udp.udp.dst_port = dst_port;
h.length = txbuffer->frame.contents.udp.udp.length = length + sizeof(struct udp_header);
txbuffer->frame.contents.udp.udp.checksum = 0;
h.zero = 0;
r = ip_checksum(0, &h, sizeof(struct pseudo_header), 0);
if(length & 1) {
txbuffer->frame.contents.udp.payload[length] = 0;
length++;
}
r = ip_checksum(r, &txbuffer->frame.contents.udp.udp,
sizeof(struct udp_header)+length, 1);
txbuffer->frame.contents.udp.udp.checksum = r;
send_packet();
return 1;
}
static udp_callback rx_callback;
static void process_ip(void)
{
if(rxlen < (sizeof(struct ethernet_header)+sizeof(struct udp_frame))) return;
/* We don't verify UDP and IP checksums and rely on the Ethernet checksum solely */
if(rxbuffer->frame.contents.udp.ip.version != IP_IPV4) return;
// check disabled for QEMU compatibility
//if(rxbuffer->frame.contents.udp.ip.diff_services != 0) return;
if(rxbuffer->frame.contents.udp.ip.total_length < sizeof(struct udp_frame)) return;
// check disabled for QEMU compatibility
//if(rxbuffer->frame.contents.udp.ip.fragment_offset != IP_DONT_FRAGMENT) return;
if(rxbuffer->frame.contents.udp.ip.proto != IP_PROTO_UDP) return;
if(rxbuffer->frame.contents.udp.ip.dst_ip != my_ip) return;
if(rxbuffer->frame.contents.udp.udp.length < sizeof(struct udp_header)) return;
if(rx_callback)
rx_callback(rxbuffer->frame.contents.udp.ip.src_ip, rxbuffer->frame.contents.udp.udp.src_port, rxbuffer->frame.contents.udp.udp.dst_port, rxbuffer->frame.contents.udp.payload, rxbuffer->frame.contents.udp.udp.length-sizeof(struct udp_header));
}
void microudp_set_callback(udp_callback callback)
{
rx_callback = callback;
}
static void process_frame(void)
{
int i;
unsigned int received_crc;
unsigned int computed_crc;
flush_cpu_dcache();
for(i=0;i<7;i++)
if(rxbuffer->frame.eth_header.preamble[i] != 0x55) return;
if(rxbuffer->frame.eth_header.preamble[7] != 0xd5) return;
received_crc = ((unsigned int)rxbuffer->raw[rxlen-1] << 24)
|((unsigned int)rxbuffer->raw[rxlen-2] << 16)
|((unsigned int)rxbuffer->raw[rxlen-3] << 8)
|((unsigned int)rxbuffer->raw[rxlen-4]);
computed_crc = crc32(&rxbuffer->raw[8], rxlen-12);
if(received_crc != computed_crc) return;
rxlen -= 4; /* strip CRC here to be consistent with TX */
if(rxbuffer->frame.eth_header.ethertype == ETHERTYPE_ARP) process_arp();
else if(rxbuffer->frame.eth_header.ethertype == ETHERTYPE_IP) process_ip();
}
void microudp_start(unsigned char *macaddr, unsigned int ip)
{
int i;
irq_ack(IRQ_ETHRX|IRQ_ETHTX);
rxbuffer0 = (ethernet_buffer *)MINIMAC_RX0_BASE;
rxbuffer1 = (ethernet_buffer *)MINIMAC_RX1_BASE;
txbuffer = (ethernet_buffer *)MINIMAC_TX_BASE;
for(i=0;i<6;i++)
my_mac[i] = macaddr[i];
my_ip = ip;
cached_ip = 0;
for(i=0;i<6;i++)
cached_mac[i] = 0;
rx_callback = (udp_callback)0;
CSR_MINIMAC_STATE0 = MINIMAC_STATE_LOADED;
CSR_MINIMAC_STATE1 = MINIMAC_STATE_LOADED;
CSR_MINIMAC_SETUP = 0;
}
void microudp_service(void)
{
if(irq_pending() & IRQ_ETHRX) {
if(CSR_MINIMAC_STATE0 == MINIMAC_STATE_PENDING) {
rxlen = CSR_MINIMAC_COUNT0;
rxbuffer = rxbuffer0;
process_frame();
CSR_MINIMAC_STATE0 = MINIMAC_STATE_LOADED;
}
if(CSR_MINIMAC_STATE1 == MINIMAC_STATE_PENDING) {
rxlen = CSR_MINIMAC_COUNT1;
rxbuffer = rxbuffer1;
process_frame();
CSR_MINIMAC_STATE1 = MINIMAC_STATE_LOADED;
}
irq_ack(IRQ_ETHRX);
}
}