net.h 16 KB

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