net.h 11 KB

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