1
2
3
4
5
6
7
8
9
10
11
12#ifndef __NET_H__
13#define __NET_H__
14
15#if defined(CONFIG_8xx)
16#include <commproc.h>
17#endif
18
19#include <asm/cache.h>
20#include <asm/byteorder.h>
21
22#define DEBUG_LL_STATE 0
23#define DEBUG_DEV_PKT 0
24#define DEBUG_NET_PKT 0
25#define DEBUG_INT_STATE 0
26
27
28
29
30
31
32
33#ifdef CONFIG_SYS_RX_ETH_BUFFER
34# define PKTBUFSRX CONFIG_SYS_RX_ETH_BUFFER
35#else
36# define PKTBUFSRX 4
37#endif
38
39#define PKTALIGN ARCH_DMA_MINALIGN
40
41
42typedef u32 IPaddr_t;
43
44
45
46
47
48
49
50
51
52
53typedef void rxhand_f(uchar *pkt, unsigned dport,
54 IPaddr_t sip, unsigned sport,
55 unsigned len);
56
57
58
59
60
61
62
63
64
65
66
67typedef void rxhand_icmp_f(unsigned type, unsigned code, unsigned dport,
68 IPaddr_t sip, unsigned sport, uchar *pkt, unsigned len);
69
70
71
72
73typedef void thand_f(void);
74
75enum eth_state_t {
76 ETH_STATE_INIT,
77 ETH_STATE_PASSIVE,
78 ETH_STATE_ACTIVE
79};
80
81struct eth_device {
82 char name[16];
83 unsigned char enetaddr[6];
84 int iobase;
85 int state;
86
87 int (*init) (struct eth_device *, bd_t *);
88 int (*send) (struct eth_device *, void *packet, int length);
89 int (*recv) (struct eth_device *);
90 void (*halt) (struct eth_device *);
91#ifdef CONFIG_MCAST_TFTP
92 int (*mcast) (struct eth_device *, u32 ip, u8 set);
93#endif
94 int (*write_hwaddr) (struct eth_device *);
95 struct eth_device *next;
96 int index;
97 void *priv;
98};
99
100extern int eth_initialize(bd_t *bis);
101extern int eth_register(struct eth_device* dev);
102extern int eth_unregister(struct eth_device *dev);
103extern void eth_try_another(int first_restart);
104extern void eth_set_current(void);
105
106static inline __attribute__((always_inline))
107struct eth_device *eth_get_dev(void)
108{
109 extern struct eth_device *eth_current;
110
111 return eth_current;
112}
113extern struct eth_device *eth_get_dev_by_name(const char *devname);
114extern struct eth_device *eth_get_dev_by_index(int index);
115extern int eth_get_dev_index(void);
116extern void eth_parse_enetaddr(const char *addr, uchar *enetaddr);
117extern int eth_getenv_enetaddr(char *name, uchar *enetaddr);
118extern int eth_setenv_enetaddr(char *name, const uchar *enetaddr);
119
120
121
122
123
124
125
126
127
128
129extern int eth_getenv_enetaddr_by_index(const char *base_name, int index,
130 uchar *enetaddr);
131
132#ifdef CONFIG_RANDOM_MACADDR
133
134
135
136
137
138
139
140
141
142
143
144
145
146extern void eth_random_enetaddr(uchar *enetaddr);
147#endif
148
149extern int usb_eth_initialize(bd_t *bi);
150extern int eth_init(bd_t *bis);
151extern int eth_send(void *packet, int length);
152
153#ifdef CONFIG_API
154extern int eth_receive(void *packet, int length);
155extern void (*push_packet)(void *packet, int length);
156#endif
157extern int eth_rx(void);
158extern void eth_halt(void);
159extern char *eth_get_name(void);
160
161
162static inline __attribute__((always_inline)) int eth_init_state_only(bd_t *bis)
163{
164 eth_get_dev()->state = ETH_STATE_ACTIVE;
165
166 return 0;
167}
168
169static inline __attribute__((always_inline)) void eth_halt_state_only(void)
170{
171 eth_get_dev()->state = ETH_STATE_PASSIVE;
172}
173
174
175
176
177
178
179
180
181
182
183int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
184 int eth_number);
185
186#ifdef CONFIG_MCAST_TFTP
187int eth_mcast_join(IPaddr_t mcast_addr, u8 join);
188u32 ether_crc(size_t len, unsigned char const *p);
189#endif
190
191
192
193
194
195
196
197
198
199
200
201struct ethernet_hdr {
202 uchar et_dest[6];
203 uchar et_src[6];
204 ushort et_protlen;
205};
206
207
208#define ETHER_HDR_SIZE (sizeof(struct ethernet_hdr))
209
210struct e802_hdr {
211 uchar et_dest[6];
212 uchar et_src[6];
213 ushort et_protlen;
214 uchar et_dsap;
215 uchar et_ssap;
216 uchar et_ctl;
217 uchar et_snap1;
218 uchar et_snap2;
219 uchar et_snap3;
220 ushort et_prot;
221};
222
223
224#define E802_HDR_SIZE (sizeof(struct e802_hdr))
225
226
227
228
229struct vlan_ethernet_hdr {
230 uchar vet_dest[6];
231 uchar vet_src[6];
232 ushort vet_vlan_type;
233 ushort vet_tag;
234 ushort vet_type;
235};
236
237
238#define VLAN_ETHER_HDR_SIZE (sizeof(struct vlan_ethernet_hdr))
239
240#define PROT_IP 0x0800
241#define PROT_ARP 0x0806
242#define PROT_RARP 0x8035
243#define PROT_VLAN 0x8100
244
245#define IPPROTO_ICMP 1
246#define IPPROTO_UDP 17
247
248
249
250
251struct ip_hdr {
252 uchar ip_hl_v;
253 uchar ip_tos;
254 ushort ip_len;
255 ushort ip_id;
256 ushort ip_off;
257 uchar ip_ttl;
258 uchar ip_p;
259 ushort ip_sum;
260 IPaddr_t ip_src;
261 IPaddr_t ip_dst;
262};
263
264#define IP_OFFS 0x1fff
265#define IP_FLAGS 0xe000
266#define IP_FLAGS_RES 0x8000
267#define IP_FLAGS_DFRAG 0x4000
268#define IP_FLAGS_MFRAG 0x2000
269
270#define IP_HDR_SIZE (sizeof(struct ip_hdr))
271
272
273
274
275struct ip_udp_hdr {
276 uchar ip_hl_v;
277 uchar ip_tos;
278 ushort ip_len;
279 ushort ip_id;
280 ushort ip_off;
281 uchar ip_ttl;
282 uchar ip_p;
283 ushort ip_sum;
284 IPaddr_t ip_src;
285 IPaddr_t ip_dst;
286 ushort udp_src;
287 ushort udp_dst;
288 ushort udp_len;
289 ushort udp_xsum;
290};
291
292#define IP_UDP_HDR_SIZE (sizeof(struct ip_udp_hdr))
293#define UDP_HDR_SIZE (IP_UDP_HDR_SIZE - IP_HDR_SIZE)
294
295
296
297
298struct arp_hdr {
299 ushort ar_hrd;
300# define ARP_ETHER 1
301 ushort ar_pro;
302 uchar ar_hln;
303# define ARP_HLEN 6
304 uchar ar_pln;
305# define ARP_PLEN 4
306 ushort ar_op;
307# define ARPOP_REQUEST 1
308# define ARPOP_REPLY 2
309
310# define RARPOP_REQUEST 3
311# define RARPOP_REPLY 4
312
313
314
315
316
317
318 uchar ar_data[0];
319#define ar_sha ar_data[0]
320#define ar_spa ar_data[ARP_HLEN]
321#define ar_tha ar_data[ARP_HLEN + ARP_PLEN]
322#define ar_tpa ar_data[ARP_HLEN + ARP_PLEN + ARP_HLEN]
323#if 0
324 uchar ar_sha[];
325 uchar ar_spa[];
326 uchar ar_tha[];
327 uchar ar_tpa[];
328#endif
329};
330
331#define ARP_HDR_SIZE (8+20)
332
333
334
335
336#define ICMP_ECHO_REPLY 0
337#define ICMP_NOT_REACH 3
338#define ICMP_REDIRECT 5
339#define ICMP_ECHO_REQUEST 8
340
341
342#define ICMP_REDIR_NET 0
343#define ICMP_REDIR_HOST 1
344
345
346#define ICMP_NOT_REACH_PORT 3
347
348struct icmp_hdr {
349 uchar type;
350 uchar code;
351 ushort checksum;
352 union {
353 struct {
354 ushort id;
355 ushort sequence;
356 } echo;
357 ulong gateway;
358 struct {
359 ushort __unused;
360 ushort mtu;
361 } frag;
362 uchar data[0];
363 } un;
364};
365
366#define ICMP_HDR_SIZE (sizeof(struct icmp_hdr))
367#define IP_ICMP_HDR_SIZE (IP_HDR_SIZE + ICMP_HDR_SIZE)
368
369
370
371
372
373
374
375
376
377
378
379
380
381#define PKTSIZE 1518
382#define PKTSIZE_ALIGN 1536
383
384
385
386
387
388
389
390
391#define RINGSZ 4
392#define RINGSZ_LOG2 2
393
394
395
396
397
398
399
400
401
402
403
404
405
406extern IPaddr_t NetOurGatewayIP;
407extern IPaddr_t NetOurSubnetMask;
408extern IPaddr_t NetOurDNSIP;
409#if defined(CONFIG_BOOTP_DNS2)
410extern IPaddr_t NetOurDNS2IP;
411#endif
412extern char NetOurNISDomain[32];
413extern char NetOurHostName[32];
414extern char NetOurRootPath[64];
415extern ushort NetBootFileSize;
416
417extern ulong NetBootFileXferSize;
418extern uchar NetOurEther[6];
419extern uchar NetServerEther[6];
420extern IPaddr_t NetOurIP;
421extern IPaddr_t NetServerIP;
422extern uchar *NetTxPacket;
423extern uchar *NetRxPackets[PKTBUFSRX];
424extern uchar *NetRxPacket;
425extern int NetRxPacketLen;
426extern unsigned NetIPID;
427extern uchar NetBcastAddr[6];
428extern uchar NetEtherNullAddr[6];
429
430#define VLAN_NONE 4095
431#define VLAN_IDMASK 0x0fff
432extern ushort NetOurVLAN;
433extern ushort NetOurNativeVLAN;
434
435extern int NetRestartWrap;
436
437enum proto_t {
438 BOOTP, RARP, ARP, TFTPGET, DHCP, PING, DNS, NFS, CDP, NETCONS, SNTP,
439 TFTPSRV, TFTPPUT, LINKLOCAL
440};
441
442
443extern char BootFile[128];
444
445#if defined(CONFIG_CMD_DNS)
446extern char *NetDNSResolve;
447extern char *NetDNSenvvar;
448#endif
449
450#if defined(CONFIG_CMD_PING)
451extern IPaddr_t NetPingIP;
452#endif
453
454#if defined(CONFIG_CMD_CDP)
455
456extern ushort CDPNativeVLAN;
457extern ushort CDPApplianceVLAN;
458
459
460
461
462static inline int is_cdp_packet(const uchar *et_addr)
463{
464 extern const uchar NetCDPAddr[6];
465
466 return memcmp(et_addr, NetCDPAddr, 6) == 0;
467}
468#endif
469
470#if defined(CONFIG_CMD_SNTP)
471extern IPaddr_t NetNtpServerIP;
472extern int NetTimeOffset;
473#endif
474
475#if defined(CONFIG_MCAST_TFTP)
476extern IPaddr_t Mcast_addr;
477#endif
478
479
480extern void net_init(void);
481extern int NetLoop(enum proto_t);
482
483
484extern void NetStop(void);
485
486
487extern void NetStartAgain(void);
488
489
490extern int NetEthHdrSize(void);
491
492
493extern int NetSetEther(uchar *, uchar *, uint);
494extern int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot);
495
496
497extern void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source);
498extern void net_set_udp_header(uchar *pkt, IPaddr_t dest, int dport,
499 int sport, int len);
500
501
502extern int NetCksumOk(uchar *, int);
503extern uint NetCksum(uchar *, int);
504
505
506extern rxhand_f *net_get_udp_handler(void);
507extern void net_set_udp_handler(rxhand_f *);
508extern rxhand_f *net_get_arp_handler(void);
509extern void net_set_arp_handler(rxhand_f *);
510extern void net_set_icmp_handler(rxhand_icmp_f *f);
511extern void NetSetTimeout(ulong, thand_f *);
512
513
514enum net_loop_state {
515 NETLOOP_CONTINUE,
516 NETLOOP_RESTART,
517 NETLOOP_SUCCESS,
518 NETLOOP_FAIL
519};
520static inline void net_set_state(enum net_loop_state state)
521{
522 extern enum net_loop_state net_state;
523
524 debug_cond(DEBUG_INT_STATE, "--- NetState set to %d\n", state);
525 net_state = state;
526}
527
528
529static inline void NetSendPacket(uchar *pkt, int len)
530{
531 (void) eth_send(pkt, len);
532}
533
534
535
536
537
538
539
540
541
542
543
544extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport,
545 int sport, int payload_len);
546
547
548extern void NetReceive(uchar *, int);
549
550#ifdef CONFIG_NETCONSOLE
551void NcStart(void);
552int nc_input_packet(uchar *pkt, IPaddr_t src_ip, unsigned dest_port,
553 unsigned src_port, unsigned len);
554#endif
555
556static inline __attribute__((always_inline)) int eth_is_on_demand_init(void)
557{
558#ifdef CONFIG_NETCONSOLE
559 extern enum proto_t net_loop_last_protocol;
560
561 return net_loop_last_protocol != NETCONS;
562#else
563 return 1;
564#endif
565}
566
567static inline void eth_set_last_protocol(int protocol)
568{
569#ifdef CONFIG_NETCONSOLE
570 extern enum proto_t net_loop_last_protocol;
571
572 net_loop_last_protocol = protocol;
573#endif
574}
575
576
577
578
579
580void net_auto_load(void);
581
582
583
584
585
586
587
588
589
590static inline IPaddr_t NetReadIP(void *from)
591{
592 IPaddr_t ip;
593
594 memcpy((void *)&ip, (void *)from, sizeof(ip));
595 return ip;
596}
597
598
599static inline ulong NetReadLong(ulong *from)
600{
601 ulong l;
602
603 memcpy((void *)&l, (void *)from, sizeof(l));
604 return l;
605}
606
607
608static inline void NetWriteIP(void *to, IPaddr_t ip)
609{
610 memcpy(to, (void *)&ip, sizeof(ip));
611}
612
613
614static inline void NetCopyIP(void *to, void *from)
615{
616 memcpy((void *)to, from, sizeof(IPaddr_t));
617}
618
619
620static inline void NetCopyLong(ulong *to, ulong *from)
621{
622 memcpy((void *)to, (void *)from, sizeof(ulong));
623}
624
625
626
627
628
629
630
631static inline int is_zero_ether_addr(const u8 *addr)
632{
633 return !(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]);
634}
635
636
637
638
639
640
641
642
643static inline int is_multicast_ether_addr(const u8 *addr)
644{
645 return 0x01 & addr[0];
646}
647
648
649
650
651
652
653
654static inline int is_broadcast_ether_addr(const u8 *addr)
655{
656 return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) ==
657 0xff;
658}
659
660
661
662
663
664
665
666
667
668
669static inline int is_valid_ether_addr(const u8 *addr)
670{
671
672
673 return !is_multicast_ether_addr(addr) && !is_zero_ether_addr(addr);
674}
675
676
677extern void ip_to_string(IPaddr_t x, char *s);
678
679
680extern IPaddr_t string_to_ip(const char *s);
681
682
683extern void VLAN_to_string(ushort x, char *s);
684
685
686extern ushort string_to_VLAN(const char *s);
687
688
689extern ushort getenv_VLAN(char *);
690
691
692extern void copy_filename(char *dst, const char *src, int size);
693
694
695extern unsigned int random_port(void);
696
697
698
699#endif
700