net.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. #endif /* CONFIG_8xx */
  16. #include <asm/byteorder.h> /* for nton* / ntoh* stuff */
  17. /*
  18. * The number of receive packet buffers, and the required packet buffer
  19. * alignment in memory.
  20. *
  21. */
  22. #ifdef CONFIG_SYS_RX_ETH_BUFFER
  23. # define PKTBUFSRX CONFIG_SYS_RX_ETH_BUFFER
  24. #else
  25. # define PKTBUFSRX 4
  26. #endif
  27. #define PKTALIGN 32
  28. typedef ulong IPaddr_t;
  29. /**
  30. * An incoming packet handler.
  31. * @param pkt pointer to the application packet
  32. * @param dport destination UDP port
  33. * @param sip source IP address
  34. * @param sport source UDP port
  35. * @param len packet length
  36. */
  37. typedef void rxhand_f(uchar *pkt, unsigned dport,
  38. IPaddr_t sip, unsigned sport,
  39. unsigned len);
  40. /*
  41. * A timeout handler. Called after time interval has expired.
  42. */
  43. typedef void thand_f(void);
  44. #define NAMESIZE 16
  45. enum eth_state_t {
  46. ETH_STATE_INIT,
  47. ETH_STATE_PASSIVE,
  48. ETH_STATE_ACTIVE
  49. };
  50. struct eth_device {
  51. char name[NAMESIZE];
  52. unsigned char enetaddr[6];
  53. int iobase;
  54. int state;
  55. int (*init) (struct eth_device*, bd_t*);
  56. int (*send) (struct eth_device*, volatile void* packet, int length);
  57. int (*recv) (struct eth_device*);
  58. void (*halt) (struct eth_device*);
  59. #ifdef CONFIG_MCAST_TFTP
  60. int (*mcast) (struct eth_device*, u32 ip, u8 set);
  61. #endif
  62. int (*write_hwaddr) (struct eth_device*);
  63. struct eth_device *next;
  64. void *priv;
  65. };
  66. extern int eth_initialize(bd_t *bis); /* Initialize network subsystem */
  67. extern int eth_register(struct eth_device* dev);/* Register network device */
  68. extern void eth_try_another(int first_restart); /* Change the device */
  69. extern void eth_set_current(void); /* set nterface to ethcur var */
  70. extern struct eth_device *eth_get_dev(void); /* get the current device MAC */
  71. extern struct eth_device *eth_get_dev_by_name(const char *devname);
  72. extern struct eth_device *eth_get_dev_by_index(int index); /* get dev @ index */
  73. extern int eth_get_dev_index (void); /* get the device index */
  74. extern void eth_parse_enetaddr(const char *addr, uchar *enetaddr);
  75. extern int eth_getenv_enetaddr(char *name, uchar *enetaddr);
  76. extern int eth_setenv_enetaddr(char *name, const uchar *enetaddr);
  77. /*
  78. * Get the hardware address for an ethernet interface .
  79. * Args:
  80. * base_name - base name for device (normally "eth")
  81. * index - device index number (0 for first)
  82. * enetaddr - returns 6 byte hardware address
  83. * Returns:
  84. * Return true if the address is valid.
  85. */
  86. extern int eth_getenv_enetaddr_by_index(const char *base_name, int index,
  87. uchar *enetaddr);
  88. extern int usb_eth_initialize(bd_t *bi);
  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. #ifdef CONFIG_API
  92. extern int eth_receive(volatile void *packet, int length); /* Receive a packet*/
  93. #endif
  94. extern int eth_rx(void); /* Check for received packets */
  95. extern void eth_halt(void); /* stop SCC */
  96. extern char *eth_get_name(void); /* get name of current device */
  97. /*
  98. * Set the hardware address for an ethernet interface based on 'eth%daddr'
  99. * environment variable (or just 'ethaddr' if eth_number is 0).
  100. * Args:
  101. * base_name - base name for device (normally "eth")
  102. * eth_number - value of %d (0 for first device of this type)
  103. * Returns:
  104. * 0 is success, non-zero is error status from driver.
  105. */
  106. int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
  107. int eth_number);
  108. #ifdef CONFIG_MCAST_TFTP
  109. int eth_mcast_join( IPaddr_t mcast_addr, u8 join);
  110. u32 ether_crc (size_t len, unsigned char const *p);
  111. #endif
  112. /**********************************************************************/
  113. /*
  114. * Protocol headers.
  115. */
  116. /*
  117. * Ethernet header
  118. */
  119. typedef struct {
  120. uchar et_dest[6]; /* Destination node */
  121. uchar et_src[6]; /* Source node */
  122. ushort et_protlen; /* Protocol or length */
  123. uchar et_dsap; /* 802 DSAP */
  124. uchar et_ssap; /* 802 SSAP */
  125. uchar et_ctl; /* 802 control */
  126. uchar et_snap1; /* SNAP */
  127. uchar et_snap2;
  128. uchar et_snap3;
  129. ushort et_prot; /* 802 protocol */
  130. } Ethernet_t;
  131. #define ETHER_HDR_SIZE 14 /* Ethernet header size */
  132. #define E802_HDR_SIZE 22 /* 802 ethernet header size */
  133. /*
  134. * Ethernet header
  135. */
  136. typedef struct {
  137. uchar vet_dest[6]; /* Destination node */
  138. uchar vet_src[6]; /* Source node */
  139. ushort vet_vlan_type; /* PROT_VLAN */
  140. ushort vet_tag; /* TAG of VLAN */
  141. ushort vet_type; /* protocol type */
  142. } VLAN_Ethernet_t;
  143. #define VLAN_ETHER_HDR_SIZE 18 /* VLAN Ethernet header size */
  144. #define PROT_IP 0x0800 /* IP protocol */
  145. #define PROT_ARP 0x0806 /* IP ARP protocol */
  146. #define PROT_RARP 0x8035 /* IP ARP protocol */
  147. #define PROT_VLAN 0x8100 /* IEEE 802.1q protocol */
  148. #define IPPROTO_ICMP 1 /* Internet Control Message Protocol */
  149. #define IPPROTO_UDP 17 /* User Datagram Protocol */
  150. /*
  151. * Internet Protocol (IP) header.
  152. */
  153. typedef struct {
  154. uchar ip_hl_v; /* header length and version */
  155. uchar ip_tos; /* type of service */
  156. ushort ip_len; /* total length */
  157. ushort ip_id; /* identification */
  158. ushort ip_off; /* fragment offset field */
  159. uchar ip_ttl; /* time to live */
  160. uchar ip_p; /* protocol */
  161. ushort ip_sum; /* checksum */
  162. IPaddr_t ip_src; /* Source IP address */
  163. IPaddr_t ip_dst; /* Destination IP address */
  164. ushort udp_src; /* UDP source port */
  165. ushort udp_dst; /* UDP destination port */
  166. ushort udp_len; /* Length of UDP packet */
  167. ushort udp_xsum; /* Checksum */
  168. } IP_t;
  169. #define IP_OFFS 0x1fff /* ip offset *= 8 */
  170. #define IP_FLAGS 0xe000 /* first 3 bits */
  171. #define IP_FLAGS_RES 0x8000 /* reserved */
  172. #define IP_FLAGS_DFRAG 0x4000 /* don't fragments */
  173. #define IP_FLAGS_MFRAG 0x2000 /* more fragments */
  174. #define IP_HDR_SIZE_NO_UDP (sizeof (IP_t) - 8)
  175. #define IP_HDR_SIZE (sizeof (IP_t))
  176. /*
  177. * Address Resolution Protocol (ARP) header.
  178. */
  179. typedef struct
  180. {
  181. ushort ar_hrd; /* Format of hardware address */
  182. # define ARP_ETHER 1 /* Ethernet hardware address */
  183. ushort ar_pro; /* Format of protocol address */
  184. uchar ar_hln; /* Length of hardware address */
  185. uchar ar_pln; /* Length of protocol address */
  186. ushort ar_op; /* Operation */
  187. # define ARPOP_REQUEST 1 /* Request to resolve address */
  188. # define ARPOP_REPLY 2 /* Response to previous request */
  189. # define RARPOP_REQUEST 3 /* Request to resolve address */
  190. # define RARPOP_REPLY 4 /* Response to previous request */
  191. /*
  192. * The remaining fields are variable in size, according to
  193. * the sizes above, and are defined as appropriate for
  194. * specific hardware/protocol combinations.
  195. */
  196. uchar ar_data[0];
  197. #if 0
  198. uchar ar_sha[]; /* Sender hardware address */
  199. uchar ar_spa[]; /* Sender protocol address */
  200. uchar ar_tha[]; /* Target hardware address */
  201. uchar ar_tpa[]; /* Target protocol address */
  202. #endif /* 0 */
  203. } ARP_t;
  204. #define ARP_HDR_SIZE (8+20) /* Size assuming ethernet */
  205. /*
  206. * ICMP stuff (just enough to handle (host) redirect messages)
  207. */
  208. #define ICMP_ECHO_REPLY 0 /* Echo reply */
  209. #define ICMP_REDIRECT 5 /* Redirect (change route) */
  210. #define ICMP_ECHO_REQUEST 8 /* Echo request */
  211. /* Codes for REDIRECT. */
  212. #define ICMP_REDIR_NET 0 /* Redirect Net */
  213. #define ICMP_REDIR_HOST 1 /* Redirect Host */
  214. typedef struct icmphdr {
  215. uchar type;
  216. uchar code;
  217. ushort checksum;
  218. union {
  219. struct {
  220. ushort id;
  221. ushort sequence;
  222. } echo;
  223. ulong gateway;
  224. struct {
  225. ushort __unused;
  226. ushort mtu;
  227. } frag;
  228. } un;
  229. } ICMP_t;
  230. /*
  231. * Maximum packet size; used to allocate packet storage.
  232. * TFTP packets can be 524 bytes + IP header + ethernet header.
  233. * Lets be conservative, and go for 38 * 16. (Must also be
  234. * a multiple of 32 bytes).
  235. */
  236. /*
  237. * AS.HARNOIS : Better to set PKTSIZE to maximum size because
  238. * traffic type is not always controlled
  239. * maximum packet size = 1518
  240. * maximum packet size and multiple of 32 bytes = 1536
  241. */
  242. #define PKTSIZE 1518
  243. #define PKTSIZE_ALIGN 1536
  244. /*#define PKTSIZE 608*/
  245. /*
  246. * Maximum receive ring size; that is, the number of packets
  247. * we can buffer before overflow happens. Basically, this just
  248. * needs to be enough to prevent a packet being discarded while
  249. * we are processing the previous one.
  250. */
  251. #define RINGSZ 4
  252. #define RINGSZ_LOG2 2
  253. /**********************************************************************/
  254. /*
  255. * Globals.
  256. *
  257. * Note:
  258. *
  259. * All variables of type IPaddr_t are stored in NETWORK byte order
  260. * (big endian).
  261. */
  262. /* net.c */
  263. /** BOOTP EXTENTIONS **/
  264. extern IPaddr_t NetOurGatewayIP; /* Our gateway IP addresse */
  265. extern IPaddr_t NetOurSubnetMask; /* Our subnet mask (0 = unknown)*/
  266. extern IPaddr_t NetOurDNSIP; /* Our Domain Name Server (0 = unknown)*/
  267. #if defined(CONFIG_BOOTP_DNS2)
  268. extern IPaddr_t NetOurDNS2IP; /* Our 2nd Domain Name Server (0 = unknown)*/
  269. #endif
  270. extern char NetOurNISDomain[32]; /* Our NIS domain */
  271. extern char NetOurHostName[32]; /* Our hostname */
  272. extern char NetOurRootPath[64]; /* Our root path */
  273. extern ushort NetBootFileSize; /* Our boot file size in blocks */
  274. /** END OF BOOTP EXTENTIONS **/
  275. extern ulong NetBootFileXferSize; /* size of bootfile in bytes */
  276. extern uchar NetOurEther[6]; /* Our ethernet address */
  277. extern uchar NetServerEther[6]; /* Boot server enet address */
  278. extern IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */
  279. extern IPaddr_t NetServerIP; /* Server IP addr (0 = unknown) */
  280. extern volatile uchar * NetTxPacket; /* THE transmit packet */
  281. extern volatile uchar * NetRxPackets[PKTBUFSRX];/* Receive packets */
  282. extern volatile uchar * NetRxPacket; /* Current receive packet */
  283. extern int NetRxPacketLen; /* Current rx packet length */
  284. extern unsigned NetIPID; /* IP ID (counting) */
  285. extern uchar NetBcastAddr[6]; /* Ethernet boardcast address */
  286. extern uchar NetEtherNullAddr[6];
  287. #define VLAN_NONE 4095 /* untagged */
  288. #define VLAN_IDMASK 0x0fff /* mask of valid vlan id */
  289. extern ushort NetOurVLAN; /* Our VLAN */
  290. extern ushort NetOurNativeVLAN; /* Our Native VLAN */
  291. extern uchar NetCDPAddr[6]; /* Ethernet CDP address */
  292. extern ushort CDPNativeVLAN; /* CDP returned native VLAN */
  293. extern ushort CDPApplianceVLAN; /* CDP returned appliance VLAN */
  294. extern int NetState; /* Network loop state */
  295. #define NETLOOP_CONTINUE 1
  296. #define NETLOOP_RESTART 2
  297. #define NETLOOP_SUCCESS 3
  298. #define NETLOOP_FAIL 4
  299. extern int NetRestartWrap; /* Tried all network devices */
  300. typedef enum { BOOTP, RARP, ARP, TFTP, DHCP, PING, DNS, NFS, CDP, NETCONS, SNTP,
  301. TFTPSRV } proto_t;
  302. /* from net/net.c */
  303. extern char BootFile[128]; /* Boot File name */
  304. #if defined(CONFIG_CMD_DNS)
  305. extern char *NetDNSResolve; /* The host to resolve */
  306. extern char *NetDNSenvvar; /* the env var to put the ip into */
  307. #endif
  308. #if defined(CONFIG_CMD_PING)
  309. extern IPaddr_t NetPingIP; /* the ip address to ping */
  310. #endif
  311. #if defined(CONFIG_CMD_CDP)
  312. /* when CDP completes these hold the return values */
  313. extern ushort CDPNativeVLAN;
  314. extern ushort CDPApplianceVLAN;
  315. #endif
  316. #if defined(CONFIG_CMD_SNTP)
  317. extern IPaddr_t NetNtpServerIP; /* the ip address to NTP */
  318. extern int NetTimeOffset; /* offset time from UTC */
  319. #endif
  320. /* Initialize the network adapter */
  321. extern int NetLoop(proto_t);
  322. /* Shutdown adapters and cleanup */
  323. extern void NetStop(void);
  324. /* Load failed. Start again. */
  325. extern void NetStartAgain(void);
  326. /* Get size of the ethernet header when we send */
  327. extern int NetEthHdrSize(void);
  328. /* Set ethernet header; returns the size of the header */
  329. extern int NetSetEther(volatile uchar *, uchar *, uint);
  330. /* Set IP header */
  331. extern void NetSetIP(volatile uchar *, IPaddr_t, int, int, int);
  332. /* Checksum */
  333. extern int NetCksumOk(uchar *, int); /* Return true if cksum OK */
  334. extern uint NetCksum(uchar *, int); /* Calculate the checksum */
  335. /* Set callbacks */
  336. extern void NetSetHandler(rxhand_f *); /* Set RX packet handler */
  337. extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */
  338. /* Transmit "NetTxPacket" */
  339. extern void NetSendPacket(volatile uchar *, int);
  340. /* Transmit UDP packet, performing ARP request if needed */
  341. extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len);
  342. /* Processes a received packet */
  343. extern void NetReceive(volatile uchar *, int);
  344. /*
  345. * The following functions are a bit ugly, but necessary to deal with
  346. * alignment restrictions on ARM.
  347. *
  348. * We're using inline functions, which had the smallest memory
  349. * footprint in our tests.
  350. */
  351. /* return IP *in network byteorder* */
  352. static inline IPaddr_t NetReadIP(volatile void *from)
  353. {
  354. IPaddr_t ip;
  355. memcpy((void*)&ip, (void*)from, sizeof(ip));
  356. return ip;
  357. }
  358. /* return ulong *in network byteorder* */
  359. static inline ulong NetReadLong(ulong *from)
  360. {
  361. ulong l;
  362. memcpy((void*)&l, (void*)from, sizeof(l));
  363. return l;
  364. }
  365. /* write IP *in network byteorder* */
  366. static inline void NetWriteIP(void *to, IPaddr_t ip)
  367. {
  368. memcpy(to, (void*)&ip, sizeof(ip));
  369. }
  370. /* copy IP */
  371. static inline void NetCopyIP(volatile void *to, void *from)
  372. {
  373. memcpy((void*)to, from, sizeof(IPaddr_t));
  374. }
  375. /* copy ulong */
  376. static inline void NetCopyLong(ulong *to, ulong *from)
  377. {
  378. memcpy((void*)to, (void*)from, sizeof(ulong));
  379. }
  380. /**
  381. * is_zero_ether_addr - Determine if give Ethernet address is all zeros.
  382. * @addr: Pointer to a six-byte array containing the Ethernet address
  383. *
  384. * Return true if the address is all zeroes.
  385. */
  386. static inline int is_zero_ether_addr(const u8 *addr)
  387. {
  388. return !(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]);
  389. }
  390. /**
  391. * is_multicast_ether_addr - Determine if the Ethernet address is a multicast.
  392. * @addr: Pointer to a six-byte array containing the Ethernet address
  393. *
  394. * Return true if the address is a multicast address.
  395. * By definition the broadcast address is also a multicast address.
  396. */
  397. static inline int is_multicast_ether_addr(const u8 *addr)
  398. {
  399. return (0x01 & addr[0]);
  400. }
  401. /*
  402. * is_broadcast_ether_addr - Determine if the Ethernet address is broadcast
  403. * @addr: Pointer to a six-byte array containing the Ethernet address
  404. *
  405. * Return true if the address is the broadcast address.
  406. */
  407. static inline int is_broadcast_ether_addr(const u8 *addr)
  408. {
  409. return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) == 0xff;
  410. }
  411. /*
  412. * is_valid_ether_addr - Determine if the given Ethernet address is valid
  413. * @addr: Pointer to a six-byte array containing the Ethernet address
  414. *
  415. * Check that the Ethernet address (MAC) is not 00:00:00:00:00:00, is not
  416. * a multicast address, and is not FF:FF:FF:FF:FF:FF.
  417. *
  418. * Return true if the address is valid.
  419. */
  420. static inline int is_valid_ether_addr(const u8 *addr)
  421. {
  422. /* FF:FF:FF:FF:FF:FF is a multicast address so we don't need to
  423. * explicitly check for it here. */
  424. return !is_multicast_ether_addr(addr) && !is_zero_ether_addr(addr);
  425. }
  426. /* Convert an IP address to a string */
  427. extern void ip_to_string (IPaddr_t x, char *s);
  428. /* Convert a string to ip address */
  429. extern IPaddr_t string_to_ip(const char *s);
  430. /* Convert a VLAN id to a string */
  431. extern void VLAN_to_string (ushort x, char *s);
  432. /* Convert a string to a vlan id */
  433. extern ushort string_to_VLAN(const char *s);
  434. /* read a VLAN id from an environment variable */
  435. extern ushort getenv_VLAN(char *);
  436. /* copy a filename (allow for "..." notation, limit length) */
  437. extern void copy_filename (char *dst, const char *src, int size);
  438. /* get a random source port */
  439. extern unsigned int random_port(void);
  440. /**********************************************************************/
  441. #endif /* __NET_H__ */