Add basic support for mini2440 board to barebox.
[barebox-mini2440.git] / include / net.h
blobc695e5f72ac3371a4f2ba98916d90ed169d47228
1 /*
2 * LiMon Monitor (LiMon) - Network.
4 * Copyright 1994 - 2000 Neil Russell.
5 * (See License)
8 * History
9 * 9/16/00 bor adapted to TQM823L/STK8xxL board, RARP/TFTP boot added
12 #ifndef __NET_H__
13 #define __NET_H__
15 #include <driver.h>
16 #include <linux/types.h>
17 #include <param.h>
18 #include <malloc.h>
19 #include <stdlib.h>
20 #include <clock.h>
21 #include <asm/byteorder.h> /* for nton* / ntoh* stuff */
24 /* The number of receive packet buffers */
25 #define PKTBUFSRX 4
27 struct device_d;
29 struct eth_device {
30 int iobase;
31 int state;
32 int active;
34 int (*init) (struct eth_device*);
36 int (*open) (struct eth_device*);
37 int (*send) (struct eth_device*, void *packet, int length);
38 int (*recv) (struct eth_device*);
39 void (*halt) (struct eth_device*);
40 int (*get_ethaddr) (struct eth_device*, unsigned char *adr);
41 int (*set_ethaddr) (struct eth_device*, unsigned char *adr);
43 struct eth_device *next;
44 void *priv;
46 struct device_d dev;
48 struct list_head list;
51 int eth_register(struct eth_device* dev); /* Register network device */
52 void eth_unregister(struct eth_device* dev); /* Unregister network device */
54 int eth_open(void); /* open the device */
55 int eth_send(void *packet, int length); /* Send a packet */
56 int eth_rx(void); /* Check for received packets */
57 void eth_halt(void); /* stop SCC */
58 char *eth_get_name(void); /* get name of current device */
61 * Ethernet header
63 struct ethernet {
64 uint8_t et_dest[6]; /* Destination node */
65 uint8_t et_src[6]; /* Source node */
66 uint16_t et_protlen; /* Protocol or length */
67 } __attribute__ ((packed));
69 #define ETHER_HDR_SIZE 14 /* Ethernet header size */
71 #define PROT_IP 0x0800 /* IP protocol */
72 #define PROT_ARP 0x0806 /* IP ARP protocol */
73 #define PROT_RARP 0x8035 /* IP ARP protocol */
74 #define PROT_VLAN 0x8100 /* IEEE 802.1q protocol */
76 #define IPPROTO_ICMP 1 /* Internet Control Message Protocol */
77 #define IPPROTO_UDP 17 /* User Datagram Protocol */
80 * Internet Protocol (IP) header.
82 struct iphdr {
83 uint8_t hl_v;
84 uint8_t tos;
85 uint16_t tot_len;
86 uint16_t id;
87 uint16_t frag_off;
88 uint8_t ttl;
89 uint8_t protocol;
90 uint16_t check;
91 uint32_t saddr;
92 uint32_t daddr;
93 /* The options start here. */
94 } __attribute__ ((packed));
96 struct udphdr {
97 uint16_t uh_sport; /* source port */
98 uint16_t uh_dport; /* destination port */
99 uint16_t uh_ulen; /* udp length */
100 uint16_t uh_sum; /* udp checksum */
101 } __attribute__ ((packed));
104 * Address Resolution Protocol (ARP) header.
106 struct arprequest
108 uint16_t ar_hrd; /* Format of hardware address */
109 #define ARP_ETHER 1 /* Ethernet hardware address */
110 uint16_t ar_pro; /* Format of protocol address */
111 uint8_t ar_hln; /* Length of hardware address */
112 uint8_t ar_pln; /* Length of protocol address */
113 uint16_t ar_op; /* Operation */
114 #define ARPOP_REQUEST 1 /* Request to resolve address */
115 #define ARPOP_REPLY 2 /* Response to previous request */
117 #define RARPOP_REQUEST 3 /* Request to resolve address */
118 #define RARPOP_REPLY 4 /* Response to previous request */
121 * The remaining fields are variable in size, according to
122 * the sizes above, and are defined as appropriate for
123 * specific hardware/protocol combinations.
125 uint8_t ar_data[0];
126 } __attribute__ ((packed));
128 #define ARP_HDR_SIZE (8 + 20) /* Size assuming ethernet */
131 * ICMP stuff (just enough to handle (host) redirect messages)
133 #define ICMP_ECHO_REPLY 0 /* Echo reply */
134 #define ICMP_REDIRECT 5 /* Redirect (change route) */
135 #define ICMP_ECHO_REQUEST 8 /* Echo request */
137 /* Codes for REDIRECT. */
138 #define ICMP_REDIR_NET 0 /* Redirect Net */
139 #define ICMP_REDIR_HOST 1 /* Redirect Host */
141 struct icmphdr {
142 uint8_t type;
143 uint8_t code;
144 uint16_t checksum;
145 union {
146 struct {
147 uint16_t id;
148 uint16_t sequence;
149 } echo;
150 uint32_t gateway;
151 struct {
152 uint16_t __unused;
153 uint16_t mtu;
154 } frag;
155 } un;
156 } __attribute__ ((packed));
160 * Maximum packet size; used to allocate packet storage.
161 * TFTP packets can be 524 bytes + IP header + ethernet header.
162 * Lets be conservative, and go for 38 * 16. (Must also be
163 * a multiple of 32 bytes).
165 #define PKTSIZE 1518
167 /**********************************************************************/
169 * Globals.
171 * Note:
173 * All variables of type IPaddr_t are stored in NETWORK byte order
174 * (big endian).
177 extern unsigned char *NetRxPackets[PKTBUFSRX];/* Receive packets */
179 void net_set_ip(IPaddr_t ip);
180 void net_set_serverip(IPaddr_t ip);
181 void net_set_netmask(IPaddr_t ip);
182 void net_set_gateway(IPaddr_t ip);
183 IPaddr_t net_get_ip(void);
184 IPaddr_t net_get_serverip(void);
186 /* Do the work */
187 void net_poll(void);
189 static inline struct iphdr *net_eth_to_iphdr(char *pkt)
191 return (struct iphdr *)(pkt + ETHER_HDR_SIZE);
194 static inline struct udphdr *net_eth_to_udphdr(char *pkt)
196 return (struct udphdr *)(net_eth_to_iphdr(pkt) + 1);
199 static inline struct icmphdr *net_eth_to_icmphdr(char *pkt)
201 return (struct icmphdr *)(net_eth_to_iphdr(pkt) + 1);
204 static inline char *net_eth_to_icmp_payload(char *pkt)
206 return (char *)(net_eth_to_icmphdr(pkt) + 1);
209 static inline char *net_eth_to_udp_payload(char *pkt)
211 return (char *)(net_eth_to_udphdr(pkt) + 1);
214 static inline int net_eth_to_udplen(char *pkt)
216 struct udphdr *udp = net_eth_to_udphdr(pkt);
217 return ntohs(udp->uh_ulen) - 8;
220 int net_checksum_ok(unsigned char *, int); /* Return true if cksum OK */
221 uint16_t net_checksum(unsigned char *, int); /* Calculate the checksum */
223 /* Print an IP address on the console */
224 void print_IPaddr (IPaddr_t);
227 * The following functions are a bit ugly, but necessary to deal with
228 * alignment restrictions on ARM.
230 * We're using inline functions, which had the smallest memory
231 * footprint in our tests.
233 /* return IP *in network byteorder* */
234 static inline IPaddr_t net_read_ip(void *from)
236 IPaddr_t ip;
237 memcpy((void*)&ip, from, sizeof(ip));
238 return ip;
241 /* return uint32 *in network byteorder* */
242 static inline uint32_t net_read_uint32(uint32_t *from)
244 ulong l;
245 memcpy((void*)&l, (void*)from, sizeof(l));
246 return l;
249 /* write IP *in network byteorder* */
250 static inline void net_write_ip(void *to, IPaddr_t ip)
252 memcpy(to, (void*)&ip, sizeof(ip));
255 /* copy IP */
256 static inline void net_copy_ip(void *to, void *from)
258 memcpy(to, from, sizeof(IPaddr_t));
261 /* copy ulong */
262 static inline void net_copy_uint32(uint32_t *to, uint32_t *from)
264 memcpy(to, from, sizeof(uint32_t));
267 /* Convert an IP address to a string */
268 char *ip_to_string (IPaddr_t x, char *s);
270 /* Convert a string to ip address */
271 int string_to_ip(const char *s, IPaddr_t *ip);
273 IPaddr_t getenv_ip(const char *name);
274 int setenv_ip(const char *name, IPaddr_t ip);
276 int string_to_ethaddr(const char *str, char *enetaddr);
277 void ethaddr_to_string(const unsigned char *enetaddr, char *str);
279 #ifdef CONFIG_NET_RESOLV
280 IPaddr_t resolv(char *host);
281 #else
282 static inline IPaddr_t resolv(char *host)
284 IPaddr_t ip = 0;
285 string_to_ip(host, &ip);
286 return ip;
288 #endif
291 * is_zero_ether_addr - Determine if give Ethernet address is all zeros.
292 * @addr: Pointer to a six-byte array containing the Ethernet address
294 * Return true if the address is all zeroes.
296 static inline int is_zero_ether_addr(const u8 *addr)
298 return !(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]);
302 * is_multicast_ether_addr - Determine if the Ethernet address is a multicast.
303 * @addr: Pointer to a six-byte array containing the Ethernet address
305 * Return true if the address is a multicast address.
306 * By definition the broadcast address is also a multicast address.
308 static inline int is_multicast_ether_addr(const u8 *addr)
310 return (0x01 & addr[0]);
314 * is_local_ether_addr - Determine if the Ethernet address is locally-assigned one (IEEE 802).
315 * @addr: Pointer to a six-byte array containing the Ethernet address
317 * Return true if the address is a local address.
319 static inline int is_local_ether_addr(const u8 *addr)
321 return (0x02 & addr[0]);
325 * is_broadcast_ether_addr - Determine if the Ethernet address is broadcast
326 * @addr: Pointer to a six-byte array containing the Ethernet address
328 * Return true if the address is the broadcast address.
330 static inline int is_broadcast_ether_addr(const u8 *addr)
332 return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) == 0xff;
336 * random_ether_addr - Generate software assigned random Ethernet address
337 * @addr: Pointer to a six-byte array containing the Ethernet address
339 * Generate a random Ethernet address (MAC) that is not multicast
340 * and has the local assigned bit set.
342 static inline void random_ether_addr(u8 *addr)
344 srand(get_time_ns());
345 get_random_bytes(addr, 6);
346 addr [0] &= 0xfe; /* clear multicast bit */
347 addr [0] |= 0x02; /* set local assignment bit (IEEE802) */
351 * is_valid_ether_addr - Determine if the given Ethernet address is valid
352 * @addr: Pointer to a six-byte array containing the Ethernet address
354 * Check that the Ethernet address (MAC) is not 00:00:00:00:00:00, is not
355 * a multicast address, and is not FF:FF:FF:FF:FF:FF.
357 * Return true if the address is valid.
359 static inline int is_valid_ether_addr(const u8 *addr)
361 /* FF:FF:FF:FF:FF:FF is a multicast address so we don't need to
362 * explicitly check for it here. */
363 return !is_multicast_ether_addr(addr) && !is_zero_ether_addr(addr);
366 typedef void rx_handler_f(char *packet, unsigned int len);
368 void eth_set_current(struct eth_device *eth);
369 struct eth_device *eth_get_current(void);
370 struct eth_device *eth_get_byname(char *name);
371 void net_update_env(void);
374 * net_receive - Pass a received packet from an ethernet driver to the protocol stack
375 * @pkt: Pointer to the packet
376 * @len: length of the packet
378 * Return 0 if the packet is successfully handled. Can be ignored
380 int net_receive(unsigned char *pkt, int len);
382 struct net_connection {
383 struct ethernet *et;
384 struct iphdr *ip;
385 struct udphdr *udp;
386 struct icmphdr *icmp;
387 unsigned char *packet;
388 struct list_head list;
389 rx_handler_f *handler;
390 int proto;
393 static inline char *net_alloc_packet(void)
395 return memalign(32, PKTSIZE);
398 struct net_connection *net_udp_new(IPaddr_t dest, uint16_t dport,
399 rx_handler_f *handler);
401 struct net_connection *net_icmp_new(IPaddr_t dest, rx_handler_f *handler);
403 void net_unregister(struct net_connection *con);
405 static inline int net_udp_bind(struct net_connection *con, int sport)
407 con->udp->uh_sport = ntohs(sport);
408 return 0;
411 static inline unsigned char *net_udp_get_payload(struct net_connection *con)
413 return con->packet + sizeof(struct ethernet) + sizeof(struct iphdr) +
414 sizeof(struct udphdr);
417 int net_udp_send(struct net_connection *con, int len);
418 int net_icmp_send(struct net_connection *con, int len);
420 #endif /* __NET_H__ */