net.h 13 KB

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