net.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * LiMon Monitor (LiMon) - Network.
  3. *
  4. * Copyright 1994 - 2000 Neil Russell.
  5. * (See License)
  6. *
  7. *
  8. * History
  9. * 9/16/00 bor adapted to TQM823L/STK8xxL board, RARP/TFTP boot added
  10. */
  11. #ifndef __NET_H__
  12. #define __NET_H__
  13. #if !defined(CONFIG_NET_MULTI) && defined(CONFIG_8xx)
  14. #include <commproc.h>
  15. #if defined(FEC_ENET) || defined(SCC_ENET)
  16. #define CONFIG_NET_MULTI
  17. #endif
  18. #endif
  19. #include <asm/byteorder.h> /* for nton* / ntoh* stuff */
  20. /*
  21. * The number of receive packet buffers, and the required packet buffer
  22. * alignment in memory.
  23. *
  24. */
  25. #ifndef CONFIG_EEPRO100
  26. #define PKTBUFSRX 4
  27. #else
  28. #define PKTBUFSRX 8
  29. #endif
  30. #define PKTALIGN 32
  31. typedef ulong IPaddr_t;
  32. /*
  33. * The current receive packet handler. Called with a pointer to the
  34. * application packet, and a protocol type (PORT_BOOTPC or PORT_TFTP).
  35. * All other packets are dealt with without calling the handler.
  36. */
  37. typedef void rxhand_f(uchar *, unsigned, unsigned, unsigned);
  38. /*
  39. * A timeout handler. Called after time interval has expired.
  40. */
  41. typedef void thand_f(void);
  42. #ifdef CONFIG_NET_MULTI
  43. #define NAMESIZE 16
  44. enum eth_state_t {
  45. ETH_STATE_INIT,
  46. ETH_STATE_PASSIVE,
  47. ETH_STATE_ACTIVE
  48. };
  49. struct eth_device {
  50. char name[NAMESIZE];
  51. unsigned char enetaddr[6];
  52. int iobase;
  53. int state;
  54. int (*init) (struct eth_device*, bd_t*);
  55. int (*send) (struct eth_device*, volatile void* pachet, int length);
  56. int (*recv) (struct eth_device*);
  57. void (*halt) (struct eth_device*);
  58. struct eth_device *next;
  59. void *priv;
  60. };
  61. extern int eth_initialize(bd_t *bis); /* Initialize network subsystem */
  62. extern int eth_register(struct eth_device* dev);/* Register network device */
  63. extern void eth_try_another(int first_restart); /* Change the device */
  64. extern struct eth_device *eth_get_dev(void); /* get the current device MAC */
  65. extern void eth_set_enetaddr(int num, char* a); /* Set new MAC address */
  66. #endif
  67. extern int eth_init(bd_t *bis); /* Initialize the device */
  68. extern int eth_send(volatile void *packet, int length); /* Send a packet */
  69. extern int eth_rx(void); /* Check for received packets */
  70. extern void eth_halt(void); /* stop SCC */
  71. /**********************************************************************/
  72. /*
  73. * Protocol headers.
  74. */
  75. /*
  76. * Ethernet header
  77. */
  78. typedef struct {
  79. uchar et_dest[6]; /* Destination node */
  80. uchar et_src[6]; /* Source node */
  81. ushort et_protlen; /* Protocol or length */
  82. uchar et_dsap; /* 802 DSAP */
  83. uchar et_ssap; /* 802 SSAP */
  84. uchar et_ctl; /* 802 control */
  85. uchar et_snap1; /* SNAP */
  86. uchar et_snap2;
  87. uchar et_snap3;
  88. ushort et_prot; /* 802 protocol */
  89. } Ethernet_t;
  90. #define ETHER_HDR_SIZE 14 /* Ethernet header size */
  91. #define E802_HDR_SIZE 22 /* 802 ethernet header size */
  92. #define PROT_IP 0x0800 /* IP protocol */
  93. #define PROT_ARP 0x0806 /* IP ARP protocol */
  94. #define PROT_RARP 0x8035 /* IP ARP protocol */
  95. #define IPPROTO_ICMP 1 /* Internet Control Message Protocol */
  96. #define IPPROTO_UDP 17 /* User Datagram Protocol */
  97. /*
  98. * Internet Protocol (IP) header.
  99. */
  100. typedef struct {
  101. uchar ip_hl_v; /* header length and version */
  102. uchar ip_tos; /* type of service */
  103. ushort ip_len; /* total length */
  104. ushort ip_id; /* identification */
  105. ushort ip_off; /* fragment offset field */
  106. uchar ip_ttl; /* time to live */
  107. uchar ip_p; /* protocol */
  108. ushort ip_sum; /* checksum */
  109. IPaddr_t ip_src; /* Source IP address */
  110. IPaddr_t ip_dst; /* Destination IP address */
  111. ushort udp_src; /* UDP source port */
  112. ushort udp_dst; /* UDP destination port */
  113. ushort udp_len; /* Length of UDP packet */
  114. ushort udp_xsum; /* Checksum */
  115. } IP_t;
  116. #define IP_HDR_SIZE_NO_UDP (sizeof (IP_t) - 8)
  117. #define IP_HDR_SIZE (sizeof (IP_t))
  118. /*
  119. * Address Resolution Protocol (ARP) header.
  120. */
  121. typedef struct
  122. {
  123. ushort ar_hrd; /* Format of hardware address */
  124. # define ARP_ETHER 1 /* Ethernet hardware address */
  125. ushort ar_pro; /* Format of protocol address */
  126. uchar ar_hln; /* Length of hardware address */
  127. uchar ar_pln; /* Length of protocol address */
  128. ushort ar_op; /* Operation */
  129. # define ARPOP_REQUEST 1 /* Request to resolve address */
  130. # define ARPOP_REPLY 2 /* Response to previous request */
  131. # define RARPOP_REQUEST 3 /* Request to resolve address */
  132. # define RARPOP_REPLY 4 /* Response to previous request */
  133. /*
  134. * The remaining fields are variable in size, according to
  135. * the sizes above, and are defined as appropriate for
  136. * specific hardware/protocol combinations.
  137. */
  138. uchar ar_data[0];
  139. #if 0
  140. uchar ar_sha[]; /* Sender hardware address */
  141. uchar ar_spa[]; /* Sender protocol address */
  142. uchar ar_tha[]; /* Target hardware address */
  143. uchar ar_tpa[]; /* Target protocol address */
  144. #endif /* 0 */
  145. } ARP_t;
  146. #define ARP_HDR_SIZE (8+20) /* Size assuming ethernet */
  147. /*
  148. * ICMP stuff (just enough to handle (host) redirect messages)
  149. */
  150. #define ICMP_REDIRECT 5 /* Redirect (change route) */
  151. /* Codes for REDIRECT. */
  152. #define ICMP_REDIR_NET 0 /* Redirect Net */
  153. #define ICMP_REDIR_HOST 1 /* Redirect Host */
  154. typedef struct icmphdr {
  155. uchar type;
  156. uchar code;
  157. ushort checksum;
  158. union {
  159. struct {
  160. ushort id;
  161. ushort sequence;
  162. } echo;
  163. ulong gateway;
  164. struct {
  165. ushort __unused;
  166. ushort mtu;
  167. } frag;
  168. } un;
  169. } ICMP_t;
  170. /*
  171. * Maximum packet size; used to allocate packet storage.
  172. * TFTP packets can be 524 bytes + IP header + ethernet header.
  173. * Lets be conservative, and go for 38 * 16. (Must also be
  174. * a multiple of 32 bytes).
  175. */
  176. /*
  177. * AS.HARNOIS : Better to set PKTSIZE to maximum size because
  178. * traffic type is not always controlled
  179. * maximum packet size = 1518
  180. * maximum packet size and multiple of 32 bytes = 1536
  181. */
  182. #define PKTSIZE 1518
  183. #define PKTSIZE_ALIGN 1536
  184. /*#define PKTSIZE 608*/
  185. /*
  186. * Maximum receive ring size; that is, the number of packets
  187. * we can buffer before overflow happens. Basically, this just
  188. * needs to be enough to prevent a packet being discarded while
  189. * we are processing the previous one.
  190. */
  191. #define RINGSZ 4
  192. #define RINGSZ_LOG2 2
  193. /**********************************************************************/
  194. /*
  195. * Globals.
  196. *
  197. * Note:
  198. *
  199. * All variables of type IPaddr_t are stored in NETWORK byte order
  200. * (big endian).
  201. */
  202. /* net.c */
  203. /** BOOTP EXTENTIONS **/
  204. extern IPaddr_t NetOurGatewayIP; /* Our gateway IP addresse */
  205. extern IPaddr_t NetOurSubnetMask; /* Our subnet mask (0 = unknown)*/
  206. extern IPaddr_t NetOurDNSIP; /* Our Domain Name Server (0 = unknown)*/
  207. extern char NetOurNISDomain[32]; /* Our NIS domain */
  208. extern char NetOurHostName[32]; /* Our hostname */
  209. extern char NetOurRootPath[64]; /* Our root path */
  210. extern ushort NetBootFileSize; /* Our boot file size in blocks */
  211. /** END OF BOOTP EXTENTIONS **/
  212. extern ulong NetBootFileXferSize; /* size of bootfile in bytes */
  213. extern uchar NetOurEther[6]; /* Our ethernet address */
  214. extern uchar NetServerEther[6]; /* Boot server enet address */
  215. extern IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */
  216. extern IPaddr_t NetServerIP; /* Server IP addr (0 = unknown) */
  217. extern volatile uchar * NetTxPacket; /* THE transmit packet */
  218. extern volatile uchar * NetRxPackets[PKTBUFSRX];/* Receive packets */
  219. extern volatile uchar * NetRxPkt; /* Current receive packet */
  220. extern int NetRxPktLen; /* Current rx packet length */
  221. extern unsigned NetIPID; /* IP ID (counting) */
  222. extern uchar NetBcastAddr[6]; /* Ethernet boardcast address */
  223. extern int NetState; /* Network loop state */
  224. #define NETLOOP_CONTINUE 1
  225. #define NETLOOP_RESTART 2
  226. #define NETLOOP_SUCCESS 3
  227. #define NETLOOP_FAIL 4
  228. #ifdef CONFIG_NET_MULTI
  229. extern int NetRestartWrap; /* Tried all network devices */
  230. #endif
  231. typedef enum { BOOTP, RARP, ARP, TFTP, DHCP } proto_t;
  232. /* from net/net.c */
  233. extern char BootFile[128]; /* Boot File name */
  234. /* Initialize the network adapter */
  235. extern int NetLoop(proto_t);
  236. /* Shutdown adapters and cleanup */
  237. extern void NetStop(void);
  238. /* Load failed. Start again. */
  239. extern void NetStartAgain(void);
  240. /* Set ethernet header */
  241. extern void NetSetEther(volatile uchar *, uchar *, uint);
  242. /* Set IP header */
  243. extern void NetSetIP(volatile uchar *, IPaddr_t, int, int, int);
  244. /* Checksum */
  245. extern int NetCksumOk(uchar *, int); /* Return true if cksum OK */
  246. extern uint NetCksum(uchar *, int); /* Calculate the checksum */
  247. /* Set callbacks */
  248. extern void NetSetHandler(rxhand_f *); /* Set RX packet handler */
  249. extern void NetSetTimeout(int, thand_f *); /* Set timeout handler */
  250. /* Transmit "NetTxPacket" */
  251. extern void NetSendPacket(volatile uchar *, int);
  252. /* Processes a received packet */
  253. extern void NetReceive(volatile uchar *, int);
  254. /* Print an IP address on the console */
  255. extern void print_IPaddr (IPaddr_t);
  256. /*
  257. * The following functions are a bit ugly, but necessary to deal with
  258. * alignment restrictions on ARM.
  259. *
  260. * We're using inline functions, which had the smallest memory
  261. * footprint in our tests.
  262. */
  263. /* return IP *in network byteorder* */
  264. static inline IPaddr_t NetReadIP(void *from)
  265. {
  266. IPaddr_t ip;
  267. memcpy((void*)&ip, from, sizeof(ip));
  268. return ip;
  269. }
  270. /* return ulong *in network byteorder* */
  271. static inline ulong NetReadLong(ulong *from)
  272. {
  273. ulong l;
  274. memcpy((void*)&l, (void*)from, sizeof(l));
  275. return l;
  276. }
  277. /* write IP *in network byteorder* */
  278. static inline void NetWriteIP(void *to, IPaddr_t ip)
  279. {
  280. memcpy(to, (void*)&ip, sizeof(ip));
  281. }
  282. /* copy IP */
  283. static inline void NetCopyIP(void *to, void *from)
  284. {
  285. memcpy(to, from, sizeof(IPaddr_t));
  286. }
  287. /* copy ulong */
  288. static inline void NetCopyLong(ulong *to, ulong *from)
  289. {
  290. memcpy((void*)to, (void*)from, sizeof(ulong));
  291. }
  292. /* Convert an IP address to a string */
  293. extern void ip_to_string (IPaddr_t x, char *s);
  294. /* read an IP address from a environment variable */
  295. extern IPaddr_t getenv_IPaddr (char *);
  296. /* copy a filename (allow for "..." notation, limit length) */
  297. extern void copy_filename (uchar *dst, uchar *src, int size);
  298. /**********************************************************************/
  299. #endif /* __NET_H__ */