1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * LiMon Monitor (LiMon) - Network.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright 1994 - 2000 Neil Russell.
5*4882a593Smuzhiyun * (See License)
6*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * History
9*4882a593Smuzhiyun * 9/16/00 bor adapted to TQM823L/STK8xxL board, RARP/TFTP boot added
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #ifndef __NET_H__
13*4882a593Smuzhiyun #define __NET_H__
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun #include <asm/cache.h>
16*4882a593Smuzhiyun #include <asm/byteorder.h> /* for nton* / ntoh* stuff */
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #define DEBUG_LL_STATE 0 /* Link local state machine changes */
19*4882a593Smuzhiyun #define DEBUG_DEV_PKT 0 /* Packets or info directed to the device */
20*4882a593Smuzhiyun #define DEBUG_NET_PKT 0 /* Packets on info on the network at large */
21*4882a593Smuzhiyun #define DEBUG_INT_STATE 0 /* Internal network state changes */
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun /*
24*4882a593Smuzhiyun * The number of receive packet buffers, and the required packet buffer
25*4882a593Smuzhiyun * alignment in memory.
26*4882a593Smuzhiyun *
27*4882a593Smuzhiyun */
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #ifdef CONFIG_SYS_RX_ETH_BUFFER
30*4882a593Smuzhiyun # define PKTBUFSRX CONFIG_SYS_RX_ETH_BUFFER
31*4882a593Smuzhiyun #else
32*4882a593Smuzhiyun # define PKTBUFSRX 4
33*4882a593Smuzhiyun #endif
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun #define PKTALIGN ARCH_DMA_MINALIGN
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun /* ARP hardware address length */
38*4882a593Smuzhiyun #define ARP_HLEN 6
39*4882a593Smuzhiyun /*
40*4882a593Smuzhiyun * The size of a MAC address in string form, each digit requires two chars
41*4882a593Smuzhiyun * and five separator characters to form '00:00:00:00:00:00'.
42*4882a593Smuzhiyun */
43*4882a593Smuzhiyun #define ARP_HLEN_ASCII (ARP_HLEN * 2) + (ARP_HLEN - 1)
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun /* IPv4 addresses are always 32 bits in size */
46*4882a593Smuzhiyun struct in_addr {
47*4882a593Smuzhiyun __be32 s_addr;
48*4882a593Smuzhiyun };
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun /**
51*4882a593Smuzhiyun * An incoming packet handler.
52*4882a593Smuzhiyun * @param pkt pointer to the application packet
53*4882a593Smuzhiyun * @param dport destination UDP port
54*4882a593Smuzhiyun * @param sip source IP address
55*4882a593Smuzhiyun * @param sport source UDP port
56*4882a593Smuzhiyun * @param len packet length
57*4882a593Smuzhiyun */
58*4882a593Smuzhiyun typedef void rxhand_f(uchar *pkt, unsigned dport,
59*4882a593Smuzhiyun struct in_addr sip, unsigned sport,
60*4882a593Smuzhiyun unsigned len);
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun /**
63*4882a593Smuzhiyun * An incoming ICMP packet handler.
64*4882a593Smuzhiyun * @param type ICMP type
65*4882a593Smuzhiyun * @param code ICMP code
66*4882a593Smuzhiyun * @param dport destination UDP port
67*4882a593Smuzhiyun * @param sip source IP address
68*4882a593Smuzhiyun * @param sport source UDP port
69*4882a593Smuzhiyun * @param pkt pointer to the ICMP packet data
70*4882a593Smuzhiyun * @param len packet length
71*4882a593Smuzhiyun */
72*4882a593Smuzhiyun typedef void rxhand_icmp_f(unsigned type, unsigned code, unsigned dport,
73*4882a593Smuzhiyun struct in_addr sip, unsigned sport, uchar *pkt, unsigned len);
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun /*
76*4882a593Smuzhiyun * A timeout handler. Called after time interval has expired.
77*4882a593Smuzhiyun */
78*4882a593Smuzhiyun typedef void thand_f(void);
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun enum eth_state_t {
81*4882a593Smuzhiyun ETH_STATE_INIT,
82*4882a593Smuzhiyun ETH_STATE_PASSIVE,
83*4882a593Smuzhiyun ETH_STATE_ACTIVE
84*4882a593Smuzhiyun };
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun #ifdef CONFIG_DM_ETH
87*4882a593Smuzhiyun /**
88*4882a593Smuzhiyun * struct eth_pdata - Platform data for Ethernet MAC controllers
89*4882a593Smuzhiyun *
90*4882a593Smuzhiyun * @iobase: The base address of the hardware registers
91*4882a593Smuzhiyun * @enetaddr: The Ethernet MAC address that is loaded from EEPROM or env
92*4882a593Smuzhiyun * @phy_interface: PHY interface to use - see PHY_INTERFACE_MODE_...
93*4882a593Smuzhiyun * @max_speed: Maximum speed of Ethernet connection supported by MAC
94*4882a593Smuzhiyun */
95*4882a593Smuzhiyun struct eth_pdata {
96*4882a593Smuzhiyun phys_addr_t iobase;
97*4882a593Smuzhiyun unsigned char enetaddr[ARP_HLEN];
98*4882a593Smuzhiyun int phy_interface;
99*4882a593Smuzhiyun int max_speed;
100*4882a593Smuzhiyun };
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun enum eth_recv_flags {
103*4882a593Smuzhiyun /*
104*4882a593Smuzhiyun * Check hardware device for new packets (otherwise only return those
105*4882a593Smuzhiyun * which are already in the memory buffer ready to process)
106*4882a593Smuzhiyun */
107*4882a593Smuzhiyun ETH_RECV_CHECK_DEVICE = 1 << 0,
108*4882a593Smuzhiyun };
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun /**
111*4882a593Smuzhiyun * struct eth_ops - functions of Ethernet MAC controllers
112*4882a593Smuzhiyun *
113*4882a593Smuzhiyun * start: Prepare the hardware to send and receive packets
114*4882a593Smuzhiyun * send: Send the bytes passed in "packet" as a packet on the wire
115*4882a593Smuzhiyun * recv: Check if the hardware received a packet. If so, set the pointer to the
116*4882a593Smuzhiyun * packet buffer in the packetp parameter. If not, return an error or 0 to
117*4882a593Smuzhiyun * indicate that the hardware receive FIFO is empty. If 0 is returned, the
118*4882a593Smuzhiyun * network stack will not process the empty packet, but free_pkt() will be
119*4882a593Smuzhiyun * called if supplied
120*4882a593Smuzhiyun * free_pkt: Give the driver an opportunity to manage its packet buffer memory
121*4882a593Smuzhiyun * when the network stack is finished processing it. This will only be
122*4882a593Smuzhiyun * called when no error was returned from recv - optional
123*4882a593Smuzhiyun * stop: Stop the hardware from looking for packets - may be called even if
124*4882a593Smuzhiyun * state == PASSIVE
125*4882a593Smuzhiyun * mcast: Join or leave a multicast group (for TFTP) - optional
126*4882a593Smuzhiyun * write_hwaddr: Write a MAC address to the hardware (used to pass it to Linux
127*4882a593Smuzhiyun * on some platforms like ARM). This function expects the
128*4882a593Smuzhiyun * eth_pdata::enetaddr field to be populated. The method can
129*4882a593Smuzhiyun * return -ENOSYS to indicate that this is not implemented for
130*4882a593Smuzhiyun this hardware - optional.
131*4882a593Smuzhiyun * read_rom_hwaddr: Some devices have a backup of the MAC address stored in a
132*4882a593Smuzhiyun * ROM on the board. This is how the driver should expose it
133*4882a593Smuzhiyun * to the network stack. This function should fill in the
134*4882a593Smuzhiyun * eth_pdata::enetaddr field - optional
135*4882a593Smuzhiyun */
136*4882a593Smuzhiyun struct eth_ops {
137*4882a593Smuzhiyun int (*start)(struct udevice *dev);
138*4882a593Smuzhiyun int (*send)(struct udevice *dev, void *packet, int length);
139*4882a593Smuzhiyun int (*recv)(struct udevice *dev, int flags, uchar **packetp);
140*4882a593Smuzhiyun int (*free_pkt)(struct udevice *dev, uchar *packet, int length);
141*4882a593Smuzhiyun void (*stop)(struct udevice *dev);
142*4882a593Smuzhiyun #ifdef CONFIG_MCAST_TFTP
143*4882a593Smuzhiyun int (*mcast)(struct udevice *dev, const u8 *enetaddr, int join);
144*4882a593Smuzhiyun #endif
145*4882a593Smuzhiyun int (*write_hwaddr)(struct udevice *dev);
146*4882a593Smuzhiyun int (*read_rom_hwaddr)(struct udevice *dev);
147*4882a593Smuzhiyun };
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun #define eth_get_ops(dev) ((struct eth_ops *)(dev)->driver->ops)
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun struct udevice *eth_get_dev(void); /* get the current device */
152*4882a593Smuzhiyun /*
153*4882a593Smuzhiyun * The devname can be either an exact name given by the driver or device tree
154*4882a593Smuzhiyun * or it can be an alias of the form "eth%d"
155*4882a593Smuzhiyun */
156*4882a593Smuzhiyun struct udevice *eth_get_dev_by_name(const char *devname);
157*4882a593Smuzhiyun unsigned char *eth_get_ethaddr(void); /* get the current device MAC */
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun /* Used only when NetConsole is enabled */
160*4882a593Smuzhiyun int eth_is_active(struct udevice *dev); /* Test device for active state */
161*4882a593Smuzhiyun int eth_init_state_only(void); /* Set active state */
162*4882a593Smuzhiyun void eth_halt_state_only(void); /* Set passive state */
163*4882a593Smuzhiyun #endif
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun #ifndef CONFIG_DM_ETH
166*4882a593Smuzhiyun struct eth_device {
167*4882a593Smuzhiyun #define ETH_NAME_LEN 16
168*4882a593Smuzhiyun char name[ETH_NAME_LEN];
169*4882a593Smuzhiyun unsigned char enetaddr[ARP_HLEN];
170*4882a593Smuzhiyun phys_addr_t iobase;
171*4882a593Smuzhiyun int state;
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun int (*init)(struct eth_device *, bd_t *);
174*4882a593Smuzhiyun int (*send)(struct eth_device *, void *packet, int length);
175*4882a593Smuzhiyun int (*recv)(struct eth_device *);
176*4882a593Smuzhiyun void (*halt)(struct eth_device *);
177*4882a593Smuzhiyun #ifdef CONFIG_MCAST_TFTP
178*4882a593Smuzhiyun int (*mcast)(struct eth_device *, const u8 *enetaddr, u8 set);
179*4882a593Smuzhiyun #endif
180*4882a593Smuzhiyun int (*write_hwaddr)(struct eth_device *);
181*4882a593Smuzhiyun struct eth_device *next;
182*4882a593Smuzhiyun int index;
183*4882a593Smuzhiyun void *priv;
184*4882a593Smuzhiyun };
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun int eth_register(struct eth_device *dev);/* Register network device */
187*4882a593Smuzhiyun int eth_unregister(struct eth_device *dev);/* Remove network device */
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun extern struct eth_device *eth_current;
190*4882a593Smuzhiyun
eth_get_dev(void)191*4882a593Smuzhiyun static __always_inline struct eth_device *eth_get_dev(void)
192*4882a593Smuzhiyun {
193*4882a593Smuzhiyun return eth_current;
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun struct eth_device *eth_get_dev_by_name(const char *devname);
196*4882a593Smuzhiyun struct eth_device *eth_get_dev_by_index(int index); /* get dev @ index */
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun /* get the current device MAC */
eth_get_ethaddr(void)199*4882a593Smuzhiyun static inline unsigned char *eth_get_ethaddr(void)
200*4882a593Smuzhiyun {
201*4882a593Smuzhiyun if (eth_current)
202*4882a593Smuzhiyun return eth_current->enetaddr;
203*4882a593Smuzhiyun return NULL;
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun /* Used only when NetConsole is enabled */
207*4882a593Smuzhiyun int eth_is_active(struct eth_device *dev); /* Test device for active state */
208*4882a593Smuzhiyun /* Set active state */
eth_init_state_only(void)209*4882a593Smuzhiyun static __always_inline int eth_init_state_only(void)
210*4882a593Smuzhiyun {
211*4882a593Smuzhiyun eth_get_dev()->state = ETH_STATE_ACTIVE;
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun return 0;
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun /* Set passive state */
eth_halt_state_only(void)216*4882a593Smuzhiyun static __always_inline void eth_halt_state_only(void)
217*4882a593Smuzhiyun {
218*4882a593Smuzhiyun eth_get_dev()->state = ETH_STATE_PASSIVE;
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun /*
222*4882a593Smuzhiyun * Set the hardware address for an ethernet interface based on 'eth%daddr'
223*4882a593Smuzhiyun * environment variable (or just 'ethaddr' if eth_number is 0).
224*4882a593Smuzhiyun * Args:
225*4882a593Smuzhiyun * base_name - base name for device (normally "eth")
226*4882a593Smuzhiyun * eth_number - value of %d (0 for first device of this type)
227*4882a593Smuzhiyun * Returns:
228*4882a593Smuzhiyun * 0 is success, non-zero is error status from driver.
229*4882a593Smuzhiyun */
230*4882a593Smuzhiyun int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
231*4882a593Smuzhiyun int eth_number);
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun int usb_eth_initialize(bd_t *bi);
234*4882a593Smuzhiyun #endif
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun int eth_initialize(void); /* Initialize network subsystem */
237*4882a593Smuzhiyun void eth_try_another(int first_restart); /* Change the device */
238*4882a593Smuzhiyun void eth_set_current(void); /* set nterface to ethcur var */
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun int eth_get_dev_index(void); /* get the device index */
241*4882a593Smuzhiyun void eth_parse_enetaddr(const char *addr, uchar *enetaddr);
242*4882a593Smuzhiyun int eth_env_get_enetaddr(const char *name, uchar *enetaddr);
243*4882a593Smuzhiyun int eth_env_set_enetaddr(const char *name, const uchar *enetaddr);
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun /**
246*4882a593Smuzhiyun * eth_env_set_enetaddr_by_index() - set the MAC address environment variable
247*4882a593Smuzhiyun *
248*4882a593Smuzhiyun * This sets up an environment variable with the given MAC address (@enetaddr).
249*4882a593Smuzhiyun * The environment variable to be set is defined by <@base_name><@index>addr.
250*4882a593Smuzhiyun * If @index is 0 it is omitted. For common Ethernet this means ethaddr,
251*4882a593Smuzhiyun * eth1addr, etc.
252*4882a593Smuzhiyun *
253*4882a593Smuzhiyun * @base_name: Base name for variable, typically "eth"
254*4882a593Smuzhiyun * @index: Index of interface being updated (>=0)
255*4882a593Smuzhiyun * @enetaddr: Pointer to MAC address to put into the variable
256*4882a593Smuzhiyun * @return 0 if OK, other value on error
257*4882a593Smuzhiyun */
258*4882a593Smuzhiyun int eth_env_set_enetaddr_by_index(const char *base_name, int index,
259*4882a593Smuzhiyun uchar *enetaddr);
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun /*
263*4882a593Smuzhiyun * Initialize USB ethernet device with CONFIG_DM_ETH
264*4882a593Smuzhiyun * Returns:
265*4882a593Smuzhiyun * 0 is success, non-zero is error status.
266*4882a593Smuzhiyun */
267*4882a593Smuzhiyun int usb_ether_init(void);
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun /*
270*4882a593Smuzhiyun * Get the hardware address for an ethernet interface .
271*4882a593Smuzhiyun * Args:
272*4882a593Smuzhiyun * base_name - base name for device (normally "eth")
273*4882a593Smuzhiyun * index - device index number (0 for first)
274*4882a593Smuzhiyun * enetaddr - returns 6 byte hardware address
275*4882a593Smuzhiyun * Returns:
276*4882a593Smuzhiyun * Return true if the address is valid.
277*4882a593Smuzhiyun */
278*4882a593Smuzhiyun int eth_env_get_enetaddr_by_index(const char *base_name, int index,
279*4882a593Smuzhiyun uchar *enetaddr);
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun int eth_init(void); /* Initialize the device */
282*4882a593Smuzhiyun int eth_send(void *packet, int length); /* Send a packet */
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun #if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
285*4882a593Smuzhiyun int eth_receive(void *packet, int length); /* Receive a packet*/
286*4882a593Smuzhiyun extern void (*push_packet)(void *packet, int length);
287*4882a593Smuzhiyun #endif
288*4882a593Smuzhiyun int eth_rx(void); /* Check for received packets */
289*4882a593Smuzhiyun void eth_halt(void); /* stop SCC */
290*4882a593Smuzhiyun const char *eth_get_name(void); /* get name of current device */
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun #ifdef CONFIG_MCAST_TFTP
293*4882a593Smuzhiyun int eth_mcast_join(struct in_addr mcast_addr, int join);
294*4882a593Smuzhiyun u32 ether_crc(size_t len, unsigned char const *p);
295*4882a593Smuzhiyun #endif
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun /**********************************************************************/
299*4882a593Smuzhiyun /*
300*4882a593Smuzhiyun * Protocol headers.
301*4882a593Smuzhiyun */
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun /*
304*4882a593Smuzhiyun * Ethernet header
305*4882a593Smuzhiyun */
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun struct ethernet_hdr {
308*4882a593Smuzhiyun u8 et_dest[ARP_HLEN]; /* Destination node */
309*4882a593Smuzhiyun u8 et_src[ARP_HLEN]; /* Source node */
310*4882a593Smuzhiyun u16 et_protlen; /* Protocol or length */
311*4882a593Smuzhiyun } __attribute__((packed));
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun /* Ethernet header size */
314*4882a593Smuzhiyun #define ETHER_HDR_SIZE (sizeof(struct ethernet_hdr))
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun #define ETH_FCS_LEN 4 /* Octets in the FCS */
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun struct e802_hdr {
319*4882a593Smuzhiyun u8 et_dest[ARP_HLEN]; /* Destination node */
320*4882a593Smuzhiyun u8 et_src[ARP_HLEN]; /* Source node */
321*4882a593Smuzhiyun u16 et_protlen; /* Protocol or length */
322*4882a593Smuzhiyun u8 et_dsap; /* 802 DSAP */
323*4882a593Smuzhiyun u8 et_ssap; /* 802 SSAP */
324*4882a593Smuzhiyun u8 et_ctl; /* 802 control */
325*4882a593Smuzhiyun u8 et_snap1; /* SNAP */
326*4882a593Smuzhiyun u8 et_snap2;
327*4882a593Smuzhiyun u8 et_snap3;
328*4882a593Smuzhiyun u16 et_prot; /* 802 protocol */
329*4882a593Smuzhiyun } __attribute__((packed));
330*4882a593Smuzhiyun
331*4882a593Smuzhiyun /* 802 + SNAP + ethernet header size */
332*4882a593Smuzhiyun #define E802_HDR_SIZE (sizeof(struct e802_hdr))
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun /*
335*4882a593Smuzhiyun * Virtual LAN Ethernet header
336*4882a593Smuzhiyun */
337*4882a593Smuzhiyun struct vlan_ethernet_hdr {
338*4882a593Smuzhiyun u8 vet_dest[ARP_HLEN]; /* Destination node */
339*4882a593Smuzhiyun u8 vet_src[ARP_HLEN]; /* Source node */
340*4882a593Smuzhiyun u16 vet_vlan_type; /* PROT_VLAN */
341*4882a593Smuzhiyun u16 vet_tag; /* TAG of VLAN */
342*4882a593Smuzhiyun u16 vet_type; /* protocol type */
343*4882a593Smuzhiyun } __attribute__((packed));
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun /* VLAN Ethernet header size */
346*4882a593Smuzhiyun #define VLAN_ETHER_HDR_SIZE (sizeof(struct vlan_ethernet_hdr))
347*4882a593Smuzhiyun
348*4882a593Smuzhiyun #define PROT_IP 0x0800 /* IP protocol */
349*4882a593Smuzhiyun #define PROT_ARP 0x0806 /* IP ARP protocol */
350*4882a593Smuzhiyun #define PROT_RARP 0x8035 /* IP ARP protocol */
351*4882a593Smuzhiyun #define PROT_VLAN 0x8100 /* IEEE 802.1q protocol */
352*4882a593Smuzhiyun #define PROT_IPV6 0x86dd /* IPv6 over bluebook */
353*4882a593Smuzhiyun #define PROT_PPP_SES 0x8864 /* PPPoE session messages */
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun #define IPPROTO_ICMP 1 /* Internet Control Message Protocol */
356*4882a593Smuzhiyun #define IPPROTO_UDP 17 /* User Datagram Protocol */
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun /*
359*4882a593Smuzhiyun * Internet Protocol (IP) header.
360*4882a593Smuzhiyun */
361*4882a593Smuzhiyun struct ip_hdr {
362*4882a593Smuzhiyun u8 ip_hl_v; /* header length and version */
363*4882a593Smuzhiyun u8 ip_tos; /* type of service */
364*4882a593Smuzhiyun u16 ip_len; /* total length */
365*4882a593Smuzhiyun u16 ip_id; /* identification */
366*4882a593Smuzhiyun u16 ip_off; /* fragment offset field */
367*4882a593Smuzhiyun u8 ip_ttl; /* time to live */
368*4882a593Smuzhiyun u8 ip_p; /* protocol */
369*4882a593Smuzhiyun u16 ip_sum; /* checksum */
370*4882a593Smuzhiyun struct in_addr ip_src; /* Source IP address */
371*4882a593Smuzhiyun struct in_addr ip_dst; /* Destination IP address */
372*4882a593Smuzhiyun } __attribute__((packed));
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun #define IP_OFFS 0x1fff /* ip offset *= 8 */
375*4882a593Smuzhiyun #define IP_FLAGS 0xe000 /* first 3 bits */
376*4882a593Smuzhiyun #define IP_FLAGS_RES 0x8000 /* reserved */
377*4882a593Smuzhiyun #define IP_FLAGS_DFRAG 0x4000 /* don't fragments */
378*4882a593Smuzhiyun #define IP_FLAGS_MFRAG 0x2000 /* more fragments */
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun #define IP_HDR_SIZE (sizeof(struct ip_hdr))
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun /*
383*4882a593Smuzhiyun * Internet Protocol (IP) + UDP header.
384*4882a593Smuzhiyun */
385*4882a593Smuzhiyun struct ip_udp_hdr {
386*4882a593Smuzhiyun u8 ip_hl_v; /* header length and version */
387*4882a593Smuzhiyun u8 ip_tos; /* type of service */
388*4882a593Smuzhiyun u16 ip_len; /* total length */
389*4882a593Smuzhiyun u16 ip_id; /* identification */
390*4882a593Smuzhiyun u16 ip_off; /* fragment offset field */
391*4882a593Smuzhiyun u8 ip_ttl; /* time to live */
392*4882a593Smuzhiyun u8 ip_p; /* protocol */
393*4882a593Smuzhiyun u16 ip_sum; /* checksum */
394*4882a593Smuzhiyun struct in_addr ip_src; /* Source IP address */
395*4882a593Smuzhiyun struct in_addr ip_dst; /* Destination IP address */
396*4882a593Smuzhiyun u16 udp_src; /* UDP source port */
397*4882a593Smuzhiyun u16 udp_dst; /* UDP destination port */
398*4882a593Smuzhiyun u16 udp_len; /* Length of UDP packet */
399*4882a593Smuzhiyun u16 udp_xsum; /* Checksum */
400*4882a593Smuzhiyun } __attribute__((packed));
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun #define IP_UDP_HDR_SIZE (sizeof(struct ip_udp_hdr))
403*4882a593Smuzhiyun #define UDP_HDR_SIZE (IP_UDP_HDR_SIZE - IP_HDR_SIZE)
404*4882a593Smuzhiyun
405*4882a593Smuzhiyun /*
406*4882a593Smuzhiyun * Address Resolution Protocol (ARP) header.
407*4882a593Smuzhiyun */
408*4882a593Smuzhiyun struct arp_hdr {
409*4882a593Smuzhiyun u16 ar_hrd; /* Format of hardware address */
410*4882a593Smuzhiyun # define ARP_ETHER 1 /* Ethernet hardware address */
411*4882a593Smuzhiyun u16 ar_pro; /* Format of protocol address */
412*4882a593Smuzhiyun u8 ar_hln; /* Length of hardware address */
413*4882a593Smuzhiyun u8 ar_pln; /* Length of protocol address */
414*4882a593Smuzhiyun # define ARP_PLEN 4
415*4882a593Smuzhiyun u16 ar_op; /* Operation */
416*4882a593Smuzhiyun # define ARPOP_REQUEST 1 /* Request to resolve address */
417*4882a593Smuzhiyun # define ARPOP_REPLY 2 /* Response to previous request */
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun # define RARPOP_REQUEST 3 /* Request to resolve address */
420*4882a593Smuzhiyun # define RARPOP_REPLY 4 /* Response to previous request */
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun /*
423*4882a593Smuzhiyun * The remaining fields are variable in size, according to
424*4882a593Smuzhiyun * the sizes above, and are defined as appropriate for
425*4882a593Smuzhiyun * specific hardware/protocol combinations.
426*4882a593Smuzhiyun */
427*4882a593Smuzhiyun u8 ar_data[0];
428*4882a593Smuzhiyun #define ar_sha ar_data[0]
429*4882a593Smuzhiyun #define ar_spa ar_data[ARP_HLEN]
430*4882a593Smuzhiyun #define ar_tha ar_data[ARP_HLEN + ARP_PLEN]
431*4882a593Smuzhiyun #define ar_tpa ar_data[ARP_HLEN + ARP_PLEN + ARP_HLEN]
432*4882a593Smuzhiyun #if 0
433*4882a593Smuzhiyun u8 ar_sha[]; /* Sender hardware address */
434*4882a593Smuzhiyun u8 ar_spa[]; /* Sender protocol address */
435*4882a593Smuzhiyun u8 ar_tha[]; /* Target hardware address */
436*4882a593Smuzhiyun u8 ar_tpa[]; /* Target protocol address */
437*4882a593Smuzhiyun #endif /* 0 */
438*4882a593Smuzhiyun } __attribute__((packed));
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun #define ARP_HDR_SIZE (8+20) /* Size assuming ethernet */
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun /*
443*4882a593Smuzhiyun * ICMP stuff (just enough to handle (host) redirect messages)
444*4882a593Smuzhiyun */
445*4882a593Smuzhiyun #define ICMP_ECHO_REPLY 0 /* Echo reply */
446*4882a593Smuzhiyun #define ICMP_NOT_REACH 3 /* Detination unreachable */
447*4882a593Smuzhiyun #define ICMP_REDIRECT 5 /* Redirect (change route) */
448*4882a593Smuzhiyun #define ICMP_ECHO_REQUEST 8 /* Echo request */
449*4882a593Smuzhiyun
450*4882a593Smuzhiyun /* Codes for REDIRECT. */
451*4882a593Smuzhiyun #define ICMP_REDIR_NET 0 /* Redirect Net */
452*4882a593Smuzhiyun #define ICMP_REDIR_HOST 1 /* Redirect Host */
453*4882a593Smuzhiyun
454*4882a593Smuzhiyun /* Codes for NOT_REACH */
455*4882a593Smuzhiyun #define ICMP_NOT_REACH_PORT 3 /* Port unreachable */
456*4882a593Smuzhiyun
457*4882a593Smuzhiyun struct icmp_hdr {
458*4882a593Smuzhiyun u8 type;
459*4882a593Smuzhiyun u8 code;
460*4882a593Smuzhiyun u16 checksum;
461*4882a593Smuzhiyun union {
462*4882a593Smuzhiyun struct {
463*4882a593Smuzhiyun u16 id;
464*4882a593Smuzhiyun u16 sequence;
465*4882a593Smuzhiyun } echo;
466*4882a593Smuzhiyun u32 gateway;
467*4882a593Smuzhiyun struct {
468*4882a593Smuzhiyun u16 unused;
469*4882a593Smuzhiyun u16 mtu;
470*4882a593Smuzhiyun } frag;
471*4882a593Smuzhiyun u8 data[0];
472*4882a593Smuzhiyun } un;
473*4882a593Smuzhiyun } __attribute__((packed));
474*4882a593Smuzhiyun
475*4882a593Smuzhiyun #define ICMP_HDR_SIZE (sizeof(struct icmp_hdr))
476*4882a593Smuzhiyun #define IP_ICMP_HDR_SIZE (IP_HDR_SIZE + ICMP_HDR_SIZE)
477*4882a593Smuzhiyun
478*4882a593Smuzhiyun /*
479*4882a593Smuzhiyun * Maximum packet size; used to allocate packet storage. Use
480*4882a593Smuzhiyun * the maxium Ethernet frame size as specified by the Ethernet
481*4882a593Smuzhiyun * standard including the 802.1Q tag (VLAN tagging).
482*4882a593Smuzhiyun * maximum packet size = 1522
483*4882a593Smuzhiyun * maximum packet size and multiple of 32 bytes = 1536
484*4882a593Smuzhiyun */
485*4882a593Smuzhiyun #define PKTSIZE 1522
486*4882a593Smuzhiyun #define PKTSIZE_ALIGN 1536
487*4882a593Smuzhiyun
488*4882a593Smuzhiyun /*
489*4882a593Smuzhiyun * Maximum receive ring size; that is, the number of packets
490*4882a593Smuzhiyun * we can buffer before overflow happens. Basically, this just
491*4882a593Smuzhiyun * needs to be enough to prevent a packet being discarded while
492*4882a593Smuzhiyun * we are processing the previous one.
493*4882a593Smuzhiyun */
494*4882a593Smuzhiyun #define RINGSZ 4
495*4882a593Smuzhiyun #define RINGSZ_LOG2 2
496*4882a593Smuzhiyun
497*4882a593Smuzhiyun /**********************************************************************/
498*4882a593Smuzhiyun /*
499*4882a593Smuzhiyun * Globals.
500*4882a593Smuzhiyun *
501*4882a593Smuzhiyun * Note:
502*4882a593Smuzhiyun *
503*4882a593Smuzhiyun * All variables of type struct in_addr are stored in NETWORK byte order
504*4882a593Smuzhiyun * (big endian).
505*4882a593Smuzhiyun */
506*4882a593Smuzhiyun
507*4882a593Smuzhiyun /* net.c */
508*4882a593Smuzhiyun /** BOOTP EXTENTIONS **/
509*4882a593Smuzhiyun extern struct in_addr net_gateway; /* Our gateway IP address */
510*4882a593Smuzhiyun extern struct in_addr net_netmask; /* Our subnet mask (0 = unknown) */
511*4882a593Smuzhiyun /* Our Domain Name Server (0 = unknown) */
512*4882a593Smuzhiyun extern struct in_addr net_dns_server;
513*4882a593Smuzhiyun #if defined(CONFIG_BOOTP_DNS2)
514*4882a593Smuzhiyun /* Our 2nd Domain Name Server (0 = unknown) */
515*4882a593Smuzhiyun extern struct in_addr net_dns_server2;
516*4882a593Smuzhiyun #endif
517*4882a593Smuzhiyun extern char net_nis_domain[32]; /* Our IS domain */
518*4882a593Smuzhiyun extern char net_hostname[32]; /* Our hostname */
519*4882a593Smuzhiyun extern char net_root_path[64]; /* Our root path */
520*4882a593Smuzhiyun /** END OF BOOTP EXTENTIONS **/
521*4882a593Smuzhiyun extern u8 net_ethaddr[ARP_HLEN]; /* Our ethernet address */
522*4882a593Smuzhiyun extern u8 net_server_ethaddr[ARP_HLEN]; /* Boot server enet address */
523*4882a593Smuzhiyun extern struct in_addr net_ip; /* Our IP addr (0 = unknown) */
524*4882a593Smuzhiyun extern struct in_addr net_server_ip; /* Server IP addr (0 = unknown) */
525*4882a593Smuzhiyun extern uchar *net_tx_packet; /* THE transmit packet */
526*4882a593Smuzhiyun extern uchar *net_rx_packets[PKTBUFSRX]; /* Receive packets */
527*4882a593Smuzhiyun extern uchar *net_rx_packet; /* Current receive packet */
528*4882a593Smuzhiyun extern int net_rx_packet_len; /* Current rx packet length */
529*4882a593Smuzhiyun extern const u8 net_bcast_ethaddr[ARP_HLEN]; /* Ethernet broadcast address */
530*4882a593Smuzhiyun extern const u8 net_null_ethaddr[ARP_HLEN];
531*4882a593Smuzhiyun
532*4882a593Smuzhiyun #define VLAN_NONE 4095 /* untagged */
533*4882a593Smuzhiyun #define VLAN_IDMASK 0x0fff /* mask of valid vlan id */
534*4882a593Smuzhiyun extern ushort net_our_vlan; /* Our VLAN */
535*4882a593Smuzhiyun extern ushort net_native_vlan; /* Our Native VLAN */
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun extern int net_restart_wrap; /* Tried all network devices */
538*4882a593Smuzhiyun
539*4882a593Smuzhiyun enum proto_t {
540*4882a593Smuzhiyun BOOTP, RARP, ARP, TFTPGET, DHCP, PING, DNS, NFS, CDP, NETCONS, SNTP,
541*4882a593Smuzhiyun TFTPSRV, TFTPPUT, LINKLOCAL, FASTBOOT
542*4882a593Smuzhiyun };
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun extern char net_boot_file_name[1024];/* Boot File name */
545*4882a593Smuzhiyun /* The actual transferred size of the bootfile (in bytes) */
546*4882a593Smuzhiyun extern u32 net_boot_file_size;
547*4882a593Smuzhiyun /* Boot file size in blocks as reported by the DHCP server */
548*4882a593Smuzhiyun extern u32 net_boot_file_expected_size_in_blocks;
549*4882a593Smuzhiyun
550*4882a593Smuzhiyun #if defined(CONFIG_CMD_DNS)
551*4882a593Smuzhiyun extern char *net_dns_resolve; /* The host to resolve */
552*4882a593Smuzhiyun extern char *net_dns_env_var; /* the env var to put the ip into */
553*4882a593Smuzhiyun #endif
554*4882a593Smuzhiyun
555*4882a593Smuzhiyun #if defined(CONFIG_UDP_FUNCTION_FASTBOOT)
556*4882a593Smuzhiyun int do_fastboot_udp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]);
557*4882a593Smuzhiyun #endif
558*4882a593Smuzhiyun
559*4882a593Smuzhiyun #if defined(CONFIG_CMD_PING)
560*4882a593Smuzhiyun extern struct in_addr net_ping_ip; /* the ip address to ping */
561*4882a593Smuzhiyun #endif
562*4882a593Smuzhiyun
563*4882a593Smuzhiyun #if defined(CONFIG_CMD_CDP)
564*4882a593Smuzhiyun /* when CDP completes these hold the return values */
565*4882a593Smuzhiyun extern ushort cdp_native_vlan; /* CDP returned native VLAN */
566*4882a593Smuzhiyun extern ushort cdp_appliance_vlan; /* CDP returned appliance VLAN */
567*4882a593Smuzhiyun
568*4882a593Smuzhiyun /*
569*4882a593Smuzhiyun * Check for a CDP packet by examining the received MAC address field
570*4882a593Smuzhiyun */
is_cdp_packet(const uchar * ethaddr)571*4882a593Smuzhiyun static inline int is_cdp_packet(const uchar *ethaddr)
572*4882a593Smuzhiyun {
573*4882a593Smuzhiyun extern const u8 net_cdp_ethaddr[ARP_HLEN];
574*4882a593Smuzhiyun
575*4882a593Smuzhiyun return memcmp(ethaddr, net_cdp_ethaddr, ARP_HLEN) == 0;
576*4882a593Smuzhiyun }
577*4882a593Smuzhiyun #endif
578*4882a593Smuzhiyun
579*4882a593Smuzhiyun #if defined(CONFIG_CMD_SNTP)
580*4882a593Smuzhiyun extern struct in_addr net_ntp_server; /* the ip address to NTP */
581*4882a593Smuzhiyun extern int net_ntp_time_offset; /* offset time from UTC */
582*4882a593Smuzhiyun #endif
583*4882a593Smuzhiyun
584*4882a593Smuzhiyun #if defined(CONFIG_MCAST_TFTP)
585*4882a593Smuzhiyun extern struct in_addr net_mcast_addr;
586*4882a593Smuzhiyun #endif
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun /* Initialize the network adapter */
589*4882a593Smuzhiyun void net_init(void);
590*4882a593Smuzhiyun int net_loop(enum proto_t);
591*4882a593Smuzhiyun
592*4882a593Smuzhiyun /* Load failed. Start again. */
593*4882a593Smuzhiyun int net_start_again(void);
594*4882a593Smuzhiyun
595*4882a593Smuzhiyun /* Get size of the ethernet header when we send */
596*4882a593Smuzhiyun int net_eth_hdr_size(void);
597*4882a593Smuzhiyun
598*4882a593Smuzhiyun /* Set ethernet header; returns the size of the header */
599*4882a593Smuzhiyun int net_set_ether(uchar *xet, const uchar *dest_ethaddr, uint prot);
600*4882a593Smuzhiyun int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot);
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun /* Set IP header */
603*4882a593Smuzhiyun void net_set_ip_header(uchar *pkt, struct in_addr dest, struct in_addr source);
604*4882a593Smuzhiyun void net_set_udp_header(uchar *pkt, struct in_addr dest, int dport,
605*4882a593Smuzhiyun int sport, int len);
606*4882a593Smuzhiyun
607*4882a593Smuzhiyun /**
608*4882a593Smuzhiyun * compute_ip_checksum() - Compute IP checksum
609*4882a593Smuzhiyun *
610*4882a593Smuzhiyun * @addr: Address to check (must be 16-bit aligned)
611*4882a593Smuzhiyun * @nbytes: Number of bytes to check (normally a multiple of 2)
612*4882a593Smuzhiyun * @return 16-bit IP checksum
613*4882a593Smuzhiyun */
614*4882a593Smuzhiyun unsigned compute_ip_checksum(const void *addr, unsigned nbytes);
615*4882a593Smuzhiyun
616*4882a593Smuzhiyun /**
617*4882a593Smuzhiyun * add_ip_checksums() - add two IP checksums
618*4882a593Smuzhiyun *
619*4882a593Smuzhiyun * @offset: Offset of first sum (if odd we do a byte-swap)
620*4882a593Smuzhiyun * @sum: First checksum
621*4882a593Smuzhiyun * @new_sum: New checksum to add
622*4882a593Smuzhiyun * @return updated 16-bit IP checksum
623*4882a593Smuzhiyun */
624*4882a593Smuzhiyun unsigned add_ip_checksums(unsigned offset, unsigned sum, unsigned new_sum);
625*4882a593Smuzhiyun
626*4882a593Smuzhiyun /**
627*4882a593Smuzhiyun * ip_checksum_ok() - check if a checksum is correct
628*4882a593Smuzhiyun *
629*4882a593Smuzhiyun * This works by making sure the checksum sums to 0
630*4882a593Smuzhiyun *
631*4882a593Smuzhiyun * @addr: Address to check (must be 16-bit aligned)
632*4882a593Smuzhiyun * @nbytes: Number of bytes to check (normally a multiple of 2)
633*4882a593Smuzhiyun * @return true if the checksum matches, false if not
634*4882a593Smuzhiyun */
635*4882a593Smuzhiyun int ip_checksum_ok(const void *addr, unsigned nbytes);
636*4882a593Smuzhiyun
637*4882a593Smuzhiyun /* Callbacks */
638*4882a593Smuzhiyun rxhand_f *net_get_udp_handler(void); /* Get UDP RX packet handler */
639*4882a593Smuzhiyun void net_set_udp_handler(rxhand_f *); /* Set UDP RX packet handler */
640*4882a593Smuzhiyun rxhand_f *net_get_arp_handler(void); /* Get ARP RX packet handler */
641*4882a593Smuzhiyun void net_set_arp_handler(rxhand_f *); /* Set ARP RX packet handler */
642*4882a593Smuzhiyun void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */
643*4882a593Smuzhiyun void net_set_timeout_handler(ulong, thand_f *);/* Set timeout handler */
644*4882a593Smuzhiyun
645*4882a593Smuzhiyun /* Network loop state */
646*4882a593Smuzhiyun enum net_loop_state {
647*4882a593Smuzhiyun NETLOOP_CONTINUE,
648*4882a593Smuzhiyun NETLOOP_RESTART,
649*4882a593Smuzhiyun NETLOOP_SUCCESS,
650*4882a593Smuzhiyun NETLOOP_FAIL
651*4882a593Smuzhiyun };
652*4882a593Smuzhiyun extern enum net_loop_state net_state;
653*4882a593Smuzhiyun
net_set_state(enum net_loop_state state)654*4882a593Smuzhiyun static inline void net_set_state(enum net_loop_state state)
655*4882a593Smuzhiyun {
656*4882a593Smuzhiyun debug_cond(DEBUG_INT_STATE, "--- NetState set to %d\n", state);
657*4882a593Smuzhiyun net_state = state;
658*4882a593Smuzhiyun }
659*4882a593Smuzhiyun
660*4882a593Smuzhiyun /* Transmit a packet */
net_send_packet(uchar * pkt,int len)661*4882a593Smuzhiyun static inline void net_send_packet(uchar *pkt, int len)
662*4882a593Smuzhiyun {
663*4882a593Smuzhiyun /* Currently no way to return errors from eth_send() */
664*4882a593Smuzhiyun (void) eth_send(pkt, len);
665*4882a593Smuzhiyun }
666*4882a593Smuzhiyun
667*4882a593Smuzhiyun /*
668*4882a593Smuzhiyun * Transmit "net_tx_packet" as UDP packet, performing ARP request if needed
669*4882a593Smuzhiyun * (ether will be populated)
670*4882a593Smuzhiyun *
671*4882a593Smuzhiyun * @param ether Raw packet buffer
672*4882a593Smuzhiyun * @param dest IP address to send the datagram to
673*4882a593Smuzhiyun * @param dport Destination UDP port
674*4882a593Smuzhiyun * @param sport Source UDP port
675*4882a593Smuzhiyun * @param payload_len Length of data after the UDP header
676*4882a593Smuzhiyun */
677*4882a593Smuzhiyun int net_send_udp_packet(uchar *ether, struct in_addr dest, int dport,
678*4882a593Smuzhiyun int sport, int payload_len);
679*4882a593Smuzhiyun
680*4882a593Smuzhiyun /* Processes a received packet */
681*4882a593Smuzhiyun void net_process_received_packet(uchar *in_packet, int len);
682*4882a593Smuzhiyun
683*4882a593Smuzhiyun #ifdef CONFIG_NETCONSOLE
684*4882a593Smuzhiyun void nc_start(void);
685*4882a593Smuzhiyun int nc_input_packet(uchar *pkt, struct in_addr src_ip, unsigned dest_port,
686*4882a593Smuzhiyun unsigned src_port, unsigned len);
687*4882a593Smuzhiyun #endif
688*4882a593Smuzhiyun
eth_is_on_demand_init(void)689*4882a593Smuzhiyun static __always_inline int eth_is_on_demand_init(void)
690*4882a593Smuzhiyun {
691*4882a593Smuzhiyun #ifdef CONFIG_NETCONSOLE
692*4882a593Smuzhiyun extern enum proto_t net_loop_last_protocol;
693*4882a593Smuzhiyun
694*4882a593Smuzhiyun return net_loop_last_protocol != NETCONS;
695*4882a593Smuzhiyun #else
696*4882a593Smuzhiyun return 1;
697*4882a593Smuzhiyun #endif
698*4882a593Smuzhiyun }
699*4882a593Smuzhiyun
eth_set_last_protocol(int protocol)700*4882a593Smuzhiyun static inline void eth_set_last_protocol(int protocol)
701*4882a593Smuzhiyun {
702*4882a593Smuzhiyun #ifdef CONFIG_NETCONSOLE
703*4882a593Smuzhiyun extern enum proto_t net_loop_last_protocol;
704*4882a593Smuzhiyun
705*4882a593Smuzhiyun net_loop_last_protocol = protocol;
706*4882a593Smuzhiyun #endif
707*4882a593Smuzhiyun }
708*4882a593Smuzhiyun
709*4882a593Smuzhiyun /*
710*4882a593Smuzhiyun * Check if autoload is enabled. If so, use either NFS or TFTP to download
711*4882a593Smuzhiyun * the boot file.
712*4882a593Smuzhiyun */
713*4882a593Smuzhiyun void net_auto_load(void);
714*4882a593Smuzhiyun
715*4882a593Smuzhiyun /*
716*4882a593Smuzhiyun * The following functions are a bit ugly, but necessary to deal with
717*4882a593Smuzhiyun * alignment restrictions on ARM.
718*4882a593Smuzhiyun *
719*4882a593Smuzhiyun * We're using inline functions, which had the smallest memory
720*4882a593Smuzhiyun * footprint in our tests.
721*4882a593Smuzhiyun */
722*4882a593Smuzhiyun /* return IP *in network byteorder* */
net_read_ip(void * from)723*4882a593Smuzhiyun static inline struct in_addr net_read_ip(void *from)
724*4882a593Smuzhiyun {
725*4882a593Smuzhiyun struct in_addr ip;
726*4882a593Smuzhiyun
727*4882a593Smuzhiyun memcpy((void *)&ip, (void *)from, sizeof(ip));
728*4882a593Smuzhiyun return ip;
729*4882a593Smuzhiyun }
730*4882a593Smuzhiyun
731*4882a593Smuzhiyun /* return ulong *in network byteorder* */
net_read_u32(u32 * from)732*4882a593Smuzhiyun static inline u32 net_read_u32(u32 *from)
733*4882a593Smuzhiyun {
734*4882a593Smuzhiyun u32 l;
735*4882a593Smuzhiyun
736*4882a593Smuzhiyun memcpy((void *)&l, (void *)from, sizeof(l));
737*4882a593Smuzhiyun return l;
738*4882a593Smuzhiyun }
739*4882a593Smuzhiyun
740*4882a593Smuzhiyun /* write IP *in network byteorder* */
net_write_ip(void * to,struct in_addr ip)741*4882a593Smuzhiyun static inline void net_write_ip(void *to, struct in_addr ip)
742*4882a593Smuzhiyun {
743*4882a593Smuzhiyun memcpy(to, (void *)&ip, sizeof(ip));
744*4882a593Smuzhiyun }
745*4882a593Smuzhiyun
746*4882a593Smuzhiyun /* copy IP */
net_copy_ip(void * to,void * from)747*4882a593Smuzhiyun static inline void net_copy_ip(void *to, void *from)
748*4882a593Smuzhiyun {
749*4882a593Smuzhiyun memcpy((void *)to, from, sizeof(struct in_addr));
750*4882a593Smuzhiyun }
751*4882a593Smuzhiyun
752*4882a593Smuzhiyun /* copy ulong */
net_copy_u32(u32 * to,u32 * from)753*4882a593Smuzhiyun static inline void net_copy_u32(u32 *to, u32 *from)
754*4882a593Smuzhiyun {
755*4882a593Smuzhiyun memcpy((void *)to, (void *)from, sizeof(u32));
756*4882a593Smuzhiyun }
757*4882a593Smuzhiyun
758*4882a593Smuzhiyun /**
759*4882a593Smuzhiyun * is_zero_ethaddr - Determine if give Ethernet address is all zeros.
760*4882a593Smuzhiyun * @addr: Pointer to a six-byte array containing the Ethernet address
761*4882a593Smuzhiyun *
762*4882a593Smuzhiyun * Return true if the address is all zeroes.
763*4882a593Smuzhiyun */
is_zero_ethaddr(const u8 * addr)764*4882a593Smuzhiyun static inline int is_zero_ethaddr(const u8 *addr)
765*4882a593Smuzhiyun {
766*4882a593Smuzhiyun return !(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]);
767*4882a593Smuzhiyun }
768*4882a593Smuzhiyun
769*4882a593Smuzhiyun /**
770*4882a593Smuzhiyun * is_multicast_ethaddr - Determine if the Ethernet address is a multicast.
771*4882a593Smuzhiyun * @addr: Pointer to a six-byte array containing the Ethernet address
772*4882a593Smuzhiyun *
773*4882a593Smuzhiyun * Return true if the address is a multicast address.
774*4882a593Smuzhiyun * By definition the broadcast address is also a multicast address.
775*4882a593Smuzhiyun */
is_multicast_ethaddr(const u8 * addr)776*4882a593Smuzhiyun static inline int is_multicast_ethaddr(const u8 *addr)
777*4882a593Smuzhiyun {
778*4882a593Smuzhiyun return 0x01 & addr[0];
779*4882a593Smuzhiyun }
780*4882a593Smuzhiyun
781*4882a593Smuzhiyun /*
782*4882a593Smuzhiyun * is_broadcast_ethaddr - Determine if the Ethernet address is broadcast
783*4882a593Smuzhiyun * @addr: Pointer to a six-byte array containing the Ethernet address
784*4882a593Smuzhiyun *
785*4882a593Smuzhiyun * Return true if the address is the broadcast address.
786*4882a593Smuzhiyun */
is_broadcast_ethaddr(const u8 * addr)787*4882a593Smuzhiyun static inline int is_broadcast_ethaddr(const u8 *addr)
788*4882a593Smuzhiyun {
789*4882a593Smuzhiyun return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) ==
790*4882a593Smuzhiyun 0xff;
791*4882a593Smuzhiyun }
792*4882a593Smuzhiyun
793*4882a593Smuzhiyun /*
794*4882a593Smuzhiyun * is_valid_ethaddr - Determine if the given Ethernet address is valid
795*4882a593Smuzhiyun * @addr: Pointer to a six-byte array containing the Ethernet address
796*4882a593Smuzhiyun *
797*4882a593Smuzhiyun * Check that the Ethernet address (MAC) is not 00:00:00:00:00:00, is not
798*4882a593Smuzhiyun * a multicast address, and is not FF:FF:FF:FF:FF:FF.
799*4882a593Smuzhiyun *
800*4882a593Smuzhiyun * Return true if the address is valid.
801*4882a593Smuzhiyun */
is_valid_ethaddr(const u8 * addr)802*4882a593Smuzhiyun static inline int is_valid_ethaddr(const u8 *addr)
803*4882a593Smuzhiyun {
804*4882a593Smuzhiyun /* FF:FF:FF:FF:FF:FF is a multicast address so we don't need to
805*4882a593Smuzhiyun * explicitly check for it here. */
806*4882a593Smuzhiyun return !is_multicast_ethaddr(addr) && !is_zero_ethaddr(addr);
807*4882a593Smuzhiyun }
808*4882a593Smuzhiyun
809*4882a593Smuzhiyun /**
810*4882a593Smuzhiyun * net_random_ethaddr - Generate software assigned random Ethernet address
811*4882a593Smuzhiyun * @addr: Pointer to a six-byte array containing the Ethernet address
812*4882a593Smuzhiyun *
813*4882a593Smuzhiyun * Generate a random Ethernet address (MAC) that is not multicast
814*4882a593Smuzhiyun * and has the local assigned bit set.
815*4882a593Smuzhiyun */
net_random_ethaddr(uchar * addr)816*4882a593Smuzhiyun static inline void net_random_ethaddr(uchar *addr)
817*4882a593Smuzhiyun {
818*4882a593Smuzhiyun int i;
819*4882a593Smuzhiyun unsigned int seed = get_timer(0);
820*4882a593Smuzhiyun
821*4882a593Smuzhiyun for (i = 0; i < 6; i++)
822*4882a593Smuzhiyun addr[i] = rand_r(&seed);
823*4882a593Smuzhiyun
824*4882a593Smuzhiyun addr[0] &= 0xfe; /* clear multicast bit */
825*4882a593Smuzhiyun addr[0] |= 0x02; /* set local assignment bit (IEEE802) */
826*4882a593Smuzhiyun }
827*4882a593Smuzhiyun
828*4882a593Smuzhiyun /* Convert an IP address to a string */
829*4882a593Smuzhiyun void ip_to_string(struct in_addr x, char *s);
830*4882a593Smuzhiyun
831*4882a593Smuzhiyun /* Convert a string to ip address */
832*4882a593Smuzhiyun struct in_addr string_to_ip(const char *s);
833*4882a593Smuzhiyun
834*4882a593Smuzhiyun /* Convert a VLAN id to a string */
835*4882a593Smuzhiyun void vlan_to_string(ushort x, char *s);
836*4882a593Smuzhiyun
837*4882a593Smuzhiyun /* Convert a string to a vlan id */
838*4882a593Smuzhiyun ushort string_to_vlan(const char *s);
839*4882a593Smuzhiyun
840*4882a593Smuzhiyun /* read a VLAN id from an environment variable */
841*4882a593Smuzhiyun ushort env_get_vlan(char *);
842*4882a593Smuzhiyun
843*4882a593Smuzhiyun /* copy a filename (allow for "..." notation, limit length) */
844*4882a593Smuzhiyun void copy_filename(char *dst, const char *src, int size);
845*4882a593Smuzhiyun
846*4882a593Smuzhiyun /* get a random source port */
847*4882a593Smuzhiyun unsigned int random_port(void);
848*4882a593Smuzhiyun
849*4882a593Smuzhiyun /**
850*4882a593Smuzhiyun * update_tftp - Update firmware over TFTP (via DFU)
851*4882a593Smuzhiyun *
852*4882a593Smuzhiyun * This function updates board's firmware via TFTP
853*4882a593Smuzhiyun *
854*4882a593Smuzhiyun * @param addr - memory address where data is stored
855*4882a593Smuzhiyun * @param interface - the DFU medium name - e.g. "mmc"
856*4882a593Smuzhiyun * @param devstring - the DFU medium number - e.g. "1"
857*4882a593Smuzhiyun *
858*4882a593Smuzhiyun * @return - 0 on success, other value on failure
859*4882a593Smuzhiyun */
860*4882a593Smuzhiyun int update_tftp(ulong addr, char *interface, char *devstring);
861*4882a593Smuzhiyun
862*4882a593Smuzhiyun /**********************************************************************/
863*4882a593Smuzhiyun
864*4882a593Smuzhiyun #endif /* __NET_H__ */
865