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
42struct in_addr {
43 __be32 s_addr;
44};
45
46
47
48
49
50
51
52
53
54typedef void rxhand_f(uchar *pkt, unsigned dport,
55 struct in_addr sip, unsigned sport,
56 unsigned len);
57
58
59
60
61
62
63
64
65
66
67
68typedef void rxhand_icmp_f(unsigned type, unsigned code, unsigned dport,
69 struct in_addr sip, unsigned sport, uchar *pkt, unsigned len);
70
71
72
73
74typedef void thand_f(void);
75
76enum eth_state_t {
77 ETH_STATE_INIT,
78 ETH_STATE_PASSIVE,
79 ETH_STATE_ACTIVE
80};
81
82#ifdef CONFIG_DM_ETH
83
84
85
86
87
88
89
90
91struct eth_pdata {
92 phys_addr_t iobase;
93 unsigned char enetaddr[6];
94 int phy_interface;
95 int max_speed;
96};
97
98enum eth_recv_flags {
99
100
101
102
103 ETH_RECV_CHECK_DEVICE = 1 << 0,
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
132struct eth_ops {
133 int (*start)(struct udevice *dev);
134 int (*send)(struct udevice *dev, void *packet, int length);
135 int (*recv)(struct udevice *dev, int flags, uchar **packetp);
136 int (*free_pkt)(struct udevice *dev, uchar *packet, int length);
137 void (*stop)(struct udevice *dev);
138#ifdef CONFIG_MCAST_TFTP
139 int (*mcast)(struct udevice *dev, const u8 *enetaddr, int join);
140#endif
141 int (*write_hwaddr)(struct udevice *dev);
142 int (*read_rom_hwaddr)(struct udevice *dev);
143};
144
145#define eth_get_ops(dev) ((struct eth_ops *)(dev)->driver->ops)
146
147struct udevice *eth_get_dev(void);
148
149
150
151
152struct udevice *eth_get_dev_by_name(const char *devname);
153unsigned char *eth_get_ethaddr(void);
154
155
156int eth_is_active(struct udevice *dev);
157int eth_init_state_only(void);
158void eth_halt_state_only(void);
159#endif
160
161#ifndef CONFIG_DM_ETH
162struct eth_device {
163 char name[16];
164 unsigned char enetaddr[6];
165 phys_addr_t iobase;
166 int state;
167
168 int (*init)(struct eth_device *, bd_t *);
169 int (*send)(struct eth_device *, void *packet, int length);
170 int (*recv)(struct eth_device *);
171 void (*halt)(struct eth_device *);
172#ifdef CONFIG_MCAST_TFTP
173 int (*mcast)(struct eth_device *, const u8 *enetaddr, u8 set);
174#endif
175 int (*write_hwaddr)(struct eth_device *);
176 struct eth_device *next;
177 int index;
178 void *priv;
179};
180
181int eth_register(struct eth_device *dev);
182int eth_unregister(struct eth_device *dev);
183
184extern struct eth_device *eth_current;
185
186static __always_inline struct eth_device *eth_get_dev(void)
187{
188 return eth_current;
189}
190struct eth_device *eth_get_dev_by_name(const char *devname);
191struct eth_device *eth_get_dev_by_index(int index);
192
193
194static inline unsigned char *eth_get_ethaddr(void)
195{
196 if (eth_current)
197 return eth_current->enetaddr;
198 return NULL;
199}
200
201
202int eth_is_active(struct eth_device *dev);
203
204static __always_inline int eth_init_state_only(void)
205{
206 eth_get_dev()->state = ETH_STATE_ACTIVE;
207
208 return 0;
209}
210
211static __always_inline void eth_halt_state_only(void)
212{
213 eth_get_dev()->state = ETH_STATE_PASSIVE;
214}
215
216
217
218
219
220
221
222
223
224
225int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
226 int eth_number);
227
228int usb_eth_initialize(bd_t *bi);
229#endif
230
231int eth_initialize(void);
232void eth_try_another(int first_restart);
233void eth_set_current(void);
234
235int eth_get_dev_index(void);
236void eth_parse_enetaddr(const char *addr, uchar *enetaddr);
237int eth_getenv_enetaddr(const char *name, uchar *enetaddr);
238int eth_setenv_enetaddr(const char *name, const uchar *enetaddr);
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253int eth_setenv_enetaddr_by_index(const char *base_name, int index,
254 uchar *enetaddr);
255
256
257
258
259
260
261
262
263
264
265
266int eth_getenv_enetaddr_by_index(const char *base_name, int index,
267 uchar *enetaddr);
268
269int eth_init(void);
270int eth_send(void *packet, int length);
271
272#if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
273int eth_receive(void *packet, int length);
274extern void (*push_packet)(void *packet, int length);
275#endif
276int eth_rx(void);
277void eth_halt(void);
278const char *eth_get_name(void);
279
280#ifdef CONFIG_MCAST_TFTP
281int eth_mcast_join(struct in_addr mcast_addr, int join);
282u32 ether_crc(size_t len, unsigned char const *p);
283#endif
284
285
286
287
288
289
290
291
292
293
294
295struct ethernet_hdr {
296 u8 et_dest[6];
297 u8 et_src[6];
298 u16 et_protlen;
299};
300
301
302#define ETHER_HDR_SIZE (sizeof(struct ethernet_hdr))
303
304#define ETH_FCS_LEN 4
305
306struct e802_hdr {
307 u8 et_dest[6];
308 u8 et_src[6];
309 u16 et_protlen;
310 u8 et_dsap;
311 u8 et_ssap;
312 u8 et_ctl;
313 u8 et_snap1;
314 u8 et_snap2;
315 u8 et_snap3;
316 u16 et_prot;
317};
318
319
320#define E802_HDR_SIZE (sizeof(struct e802_hdr))
321
322
323
324
325struct vlan_ethernet_hdr {
326 u8 vet_dest[6];
327 u8 vet_src[6];
328 u16 vet_vlan_type;
329 u16 vet_tag;
330 u16 vet_type;
331};
332
333
334#define VLAN_ETHER_HDR_SIZE (sizeof(struct vlan_ethernet_hdr))
335
336#define PROT_IP 0x0800
337#define PROT_ARP 0x0806
338#define PROT_RARP 0x8035
339#define PROT_VLAN 0x8100
340#define PROT_IPV6 0x86dd
341#define PROT_PPP_SES 0x8864
342
343#define IPPROTO_ICMP 1
344#define IPPROTO_UDP 17
345
346
347
348
349struct ip_hdr {
350 u8 ip_hl_v;
351 u8 ip_tos;
352 u16 ip_len;
353 u16 ip_id;
354 u16 ip_off;
355 u8 ip_ttl;
356 u8 ip_p;
357 u16 ip_sum;
358 struct in_addr ip_src;
359 struct in_addr ip_dst;
360};
361
362#define IP_OFFS 0x1fff
363#define IP_FLAGS 0xe000
364#define IP_FLAGS_RES 0x8000
365#define IP_FLAGS_DFRAG 0x4000
366#define IP_FLAGS_MFRAG 0x2000
367
368#define IP_HDR_SIZE (sizeof(struct ip_hdr))
369
370
371
372
373struct ip_udp_hdr {
374 u8 ip_hl_v;
375 u8 ip_tos;
376 u16 ip_len;
377 u16 ip_id;
378 u16 ip_off;
379 u8 ip_ttl;
380 u8 ip_p;
381 u16 ip_sum;
382 struct in_addr ip_src;
383 struct in_addr ip_dst;
384 u16 udp_src;
385 u16 udp_dst;
386 u16 udp_len;
387 u16 udp_xsum;
388};
389
390#define IP_UDP_HDR_SIZE (sizeof(struct ip_udp_hdr))
391#define UDP_HDR_SIZE (IP_UDP_HDR_SIZE - IP_HDR_SIZE)
392
393
394
395
396struct arp_hdr {
397 u16 ar_hrd;
398# define ARP_ETHER 1
399 u16 ar_pro;
400 u8 ar_hln;
401# define ARP_HLEN 6
402 u8 ar_pln;
403# define ARP_PLEN 4
404 u16 ar_op;
405# define ARPOP_REQUEST 1
406# define ARPOP_REPLY 2
407
408# define RARPOP_REQUEST 3
409# define RARPOP_REPLY 4
410
411
412
413
414
415
416 u8 ar_data[0];
417#define ar_sha ar_data[0]
418#define ar_spa ar_data[ARP_HLEN]
419#define ar_tha ar_data[ARP_HLEN + ARP_PLEN]
420#define ar_tpa ar_data[ARP_HLEN + ARP_PLEN + ARP_HLEN]
421#if 0
422 u8 ar_sha[];
423 u8 ar_spa[];
424 u8 ar_tha[];
425 u8 ar_tpa[];
426#endif
427};
428
429#define ARP_HDR_SIZE (8+20)
430
431
432
433
434#define ICMP_ECHO_REPLY 0
435#define ICMP_NOT_REACH 3
436#define ICMP_REDIRECT 5
437#define ICMP_ECHO_REQUEST 8
438
439
440#define ICMP_REDIR_NET 0
441#define ICMP_REDIR_HOST 1
442
443
444#define ICMP_NOT_REACH_PORT 3
445
446struct icmp_hdr {
447 u8 type;
448 u8 code;
449 u16 checksum;
450 union {
451 struct {
452 u16 id;
453 u16 sequence;
454 } echo;
455 u32 gateway;
456 struct {
457 u16 unused;
458 u16 mtu;
459 } frag;
460 u8 data[0];
461 } un;
462};
463
464#define ICMP_HDR_SIZE (sizeof(struct icmp_hdr))
465#define IP_ICMP_HDR_SIZE (IP_HDR_SIZE + ICMP_HDR_SIZE)
466
467
468
469
470
471
472
473
474#define PKTSIZE 1522
475#define PKTSIZE_ALIGN 1536
476
477
478
479
480
481
482
483#define RINGSZ 4
484#define RINGSZ_LOG2 2
485
486
487
488
489
490
491
492
493
494
495
496
497
498extern struct in_addr net_gateway;
499extern struct in_addr net_netmask;
500
501extern struct in_addr net_dns_server;
502#if defined(CONFIG_BOOTP_DNS2)
503
504extern struct in_addr net_dns_server2;
505#endif
506extern char net_nis_domain[32];
507extern char net_hostname[32];
508extern char net_root_path[64];
509
510extern u8 net_ethaddr[6];
511extern u8 net_server_ethaddr[6];
512extern struct in_addr net_ip;
513extern struct in_addr net_server_ip;
514extern uchar *net_tx_packet;
515extern uchar *net_rx_packets[PKTBUFSRX];
516extern uchar *net_rx_packet;
517extern int net_rx_packet_len;
518extern const u8 net_bcast_ethaddr[6];
519extern const u8 net_null_ethaddr[6];
520
521#define VLAN_NONE 4095
522#define VLAN_IDMASK 0x0fff
523extern ushort net_our_vlan;
524extern ushort net_native_vlan;
525
526extern int net_restart_wrap;
527
528enum proto_t {
529 BOOTP, RARP, ARP, TFTPGET, DHCP, PING, DNS, NFS, CDP, NETCONS, SNTP,
530 TFTPSRV, TFTPPUT, LINKLOCAL
531};
532
533extern char net_boot_file_name[1024];
534
535extern u32 net_boot_file_size;
536
537extern u32 net_boot_file_expected_size_in_blocks;
538
539#if defined(CONFIG_CMD_DNS)
540extern char *net_dns_resolve;
541extern char *net_dns_env_var;
542#endif
543
544#if defined(CONFIG_CMD_PING)
545extern struct in_addr net_ping_ip;
546#endif
547
548#if defined(CONFIG_CMD_CDP)
549
550extern ushort cdp_native_vlan;
551extern ushort cdp_appliance_vlan;
552
553
554
555
556static inline int is_cdp_packet(const uchar *ethaddr)
557{
558 extern const u8 net_cdp_ethaddr[6];
559
560 return memcmp(ethaddr, net_cdp_ethaddr, 6) == 0;
561}
562#endif
563
564#if defined(CONFIG_CMD_SNTP)
565extern struct in_addr net_ntp_server;
566extern int net_ntp_time_offset;
567#endif
568
569#if defined(CONFIG_MCAST_TFTP)
570extern struct in_addr net_mcast_addr;
571#endif
572
573
574void net_init(void);
575int net_loop(enum proto_t);
576
577
578int net_start_again(void);
579
580
581int net_eth_hdr_size(void);
582
583
584int net_set_ether(uchar *xet, const uchar *dest_ethaddr, uint prot);
585int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot);
586
587
588void net_set_ip_header(uchar *pkt, struct in_addr dest, struct in_addr source);
589void net_set_udp_header(uchar *pkt, struct in_addr dest, int dport,
590 int sport, int len);
591
592
593
594
595
596
597
598
599unsigned compute_ip_checksum(const void *addr, unsigned nbytes);
600
601
602
603
604
605
606
607
608
609unsigned add_ip_checksums(unsigned offset, unsigned sum, unsigned new_sum);
610
611
612
613
614
615
616
617
618
619
620int ip_checksum_ok(const void *addr, unsigned nbytes);
621
622
623rxhand_f *net_get_udp_handler(void);
624void net_set_udp_handler(rxhand_f *);
625rxhand_f *net_get_arp_handler(void);
626void net_set_arp_handler(rxhand_f *);
627void net_set_icmp_handler(rxhand_icmp_f *f);
628void net_set_timeout_handler(ulong, thand_f *);
629
630
631enum net_loop_state {
632 NETLOOP_CONTINUE,
633 NETLOOP_RESTART,
634 NETLOOP_SUCCESS,
635 NETLOOP_FAIL
636};
637extern enum net_loop_state net_state;
638
639static inline void net_set_state(enum net_loop_state state)
640{
641 debug_cond(DEBUG_INT_STATE, "--- NetState set to %d\n", state);
642 net_state = state;
643}
644
645
646static inline void net_send_packet(uchar *pkt, int len)
647{
648
649 (void) eth_send(pkt, len);
650}
651
652
653
654
655
656
657
658
659
660
661
662int net_send_udp_packet(uchar *ether, struct in_addr dest, int dport,
663 int sport, int payload_len);
664
665
666void net_process_received_packet(uchar *in_packet, int len);
667
668#ifdef CONFIG_NETCONSOLE
669void nc_start(void);
670int nc_input_packet(uchar *pkt, struct in_addr src_ip, unsigned dest_port,
671 unsigned src_port, unsigned len);
672#endif
673
674static __always_inline int eth_is_on_demand_init(void)
675{
676#ifdef CONFIG_NETCONSOLE
677 extern enum proto_t net_loop_last_protocol;
678
679 return net_loop_last_protocol != NETCONS;
680#else
681 return 1;
682#endif
683}
684
685static inline void eth_set_last_protocol(int protocol)
686{
687#ifdef CONFIG_NETCONSOLE
688 extern enum proto_t net_loop_last_protocol;
689
690 net_loop_last_protocol = protocol;
691#endif
692}
693
694
695
696
697
698void net_auto_load(void);
699
700
701
702
703
704
705
706
707
708static inline struct in_addr net_read_ip(void *from)
709{
710 struct in_addr ip;
711
712 memcpy((void *)&ip, (void *)from, sizeof(ip));
713 return ip;
714}
715
716
717static inline u32 net_read_u32(u32 *from)
718{
719 u32 l;
720
721 memcpy((void *)&l, (void *)from, sizeof(l));
722 return l;
723}
724
725
726static inline void net_write_ip(void *to, struct in_addr ip)
727{
728 memcpy(to, (void *)&ip, sizeof(ip));
729}
730
731
732static inline void net_copy_ip(void *to, void *from)
733{
734 memcpy((void *)to, from, sizeof(struct in_addr));
735}
736
737
738static inline void net_copy_u32(u32 *to, u32 *from)
739{
740 memcpy((void *)to, (void *)from, sizeof(u32));
741}
742
743
744
745
746
747
748
749static inline int is_zero_ethaddr(const u8 *addr)
750{
751 return !(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]);
752}
753
754
755
756
757
758
759
760
761static inline int is_multicast_ethaddr(const u8 *addr)
762{
763 return 0x01 & addr[0];
764}
765
766
767
768
769
770
771
772static inline int is_broadcast_ethaddr(const u8 *addr)
773{
774 return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) ==
775 0xff;
776}
777
778
779
780
781
782
783
784
785
786
787static inline int is_valid_ethaddr(const u8 *addr)
788{
789
790
791 return !is_multicast_ethaddr(addr) && !is_zero_ethaddr(addr);
792}
793
794
795
796
797
798
799
800
801static inline void net_random_ethaddr(uchar *addr)
802{
803 int i;
804 unsigned int seed = get_timer(0);
805
806 for (i = 0; i < 6; i++)
807 addr[i] = rand_r(&seed);
808
809 addr[0] &= 0xfe;
810 addr[0] |= 0x02;
811}
812
813
814void ip_to_string(struct in_addr x, char *s);
815
816
817struct in_addr string_to_ip(const char *s);
818
819
820void vlan_to_string(ushort x, char *s);
821
822
823ushort string_to_vlan(const char *s);
824
825
826ushort getenv_vlan(char *);
827
828
829void copy_filename(char *dst, const char *src, int size);
830
831
832unsigned int random_port(void);
833
834
835
836
837
838
839
840
841
842
843
844
845int update_tftp(ulong addr, char *interface, char *devstring);
846
847
848
849#endif
850