2 * LiMon Monitor (LiMon) - Network.
4 * Copyright 1994 - 2000 Neil Russell.
9 * 9/16/00 bor adapted to TQM823L/STK8xxL board, RARP/TFTP boot added
16 #include <linux/types.h>
21 #include <asm/byteorder.h> /* for nton* / ntoh* stuff */
24 /* The number of receive packet buffers */
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
;
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 */
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.
93 /* The options start here. */
94 } __attribute__ ((packed
));
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.
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.
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 */
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).
167 /**********************************************************************/
173 * All variables of type IPaddr_t are stored in NETWORK byte order
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);
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
)
237 memcpy((void*)&ip
, from
, sizeof(ip
));
241 /* return uint32 *in network byteorder* */
242 static inline uint32_t net_read_uint32(uint32_t *from
)
245 memcpy((void*)&l
, (void*)from
, sizeof(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
));
256 static inline void net_copy_ip(void *to
, void *from
)
258 memcpy(to
, from
, sizeof(IPaddr_t
));
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
);
282 static inline IPaddr_t
resolv(char *host
)
285 string_to_ip(host
, &ip
);
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
{
386 struct icmphdr
*icmp
;
387 unsigned char *packet
;
388 struct list_head list
;
389 rx_handler_f
*handler
;
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
);
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__ */