net.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. /*
  2. * LiMon Monitor (LiMon) - Network.
  3. *
  4. * Copyright 1994 - 2000 Neil Russell.
  5. * (See License)
  6. * SPDX-License-Identifier: GPL-2.0
  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/cache.h>
  17. #include <asm/byteorder.h> /* for nton* / ntoh* stuff */
  18. #define DEBUG_LL_STATE 0 /* Link local state machine changes */
  19. #define DEBUG_DEV_PKT 0 /* Packets or info directed to the device */
  20. #define DEBUG_NET_PKT 0 /* Packets on info on the network at large */
  21. #define DEBUG_INT_STATE 0 /* Internal network state changes */
  22. /*
  23. * The number of receive packet buffers, and the required packet buffer
  24. * alignment in memory.
  25. *
  26. */
  27. #ifdef CONFIG_SYS_RX_ETH_BUFFER
  28. # define PKTBUFSRX CONFIG_SYS_RX_ETH_BUFFER
  29. #else
  30. # define PKTBUFSRX 4
  31. #endif
  32. #define PKTALIGN ARCH_DMA_MINALIGN
  33. /* IPv4 addresses are always 32 bits in size */
  34. typedef __be32 IPaddr_t;
  35. /**
  36. * An incoming packet handler.
  37. * @param pkt pointer to the application packet
  38. * @param dport destination UDP port
  39. * @param sip source IP address
  40. * @param sport source UDP port
  41. * @param len packet length
  42. */
  43. typedef void rxhand_f(uchar *pkt, unsigned dport,
  44. IPaddr_t sip, unsigned sport,
  45. unsigned len);
  46. /**
  47. * An incoming ICMP packet handler.
  48. * @param type ICMP type
  49. * @param code ICMP code
  50. * @param dport destination UDP port
  51. * @param sip source IP address
  52. * @param sport source UDP port
  53. * @param pkt pointer to the ICMP packet data
  54. * @param len packet length
  55. */
  56. typedef void rxhand_icmp_f(unsigned type, unsigned code, unsigned dport,
  57. IPaddr_t sip, unsigned sport, uchar *pkt, unsigned len);
  58. /*
  59. * A timeout handler. Called after time interval has expired.
  60. */
  61. typedef void thand_f(void);
  62. enum eth_state_t {
  63. ETH_STATE_INIT,
  64. ETH_STATE_PASSIVE,
  65. ETH_STATE_ACTIVE
  66. };
  67. #ifdef CONFIG_DM_ETH
  68. /**
  69. * struct eth_pdata - Platform data for Ethernet MAC controllers
  70. *
  71. * @iobase: The base address of the hardware registers
  72. * @enetaddr: The Ethernet MAC address that is loaded from EEPROM or env
  73. */
  74. struct eth_pdata {
  75. phys_addr_t iobase;
  76. unsigned char enetaddr[6];
  77. };
  78. /**
  79. * struct eth_ops - functions of Ethernet MAC controllers
  80. *
  81. * start: Prepare the hardware to send and receive packets
  82. * send: Send the bytes passed in "packet" as a packet on the wire
  83. * recv: Check if the hardware received a packet. Call the network stack if so
  84. * stop: Stop the hardware from looking for packets - may be called even if
  85. * state == PASSIVE
  86. * mcast: Join or leave a multicast group (for TFTP) - optional
  87. * write_hwaddr: Write a MAC address to the hardware (used to pass it to Linux
  88. * on some platforms like ARM). This function expects the
  89. * eth_pdata::enetaddr field to be populated - optional
  90. * read_rom_hwaddr: Some devices have a backup of the MAC address stored in a
  91. * ROM on the board. This is how the driver should expose it
  92. * to the network stack. This function should fill in the
  93. * eth_pdata::enetaddr field - optional
  94. */
  95. struct eth_ops {
  96. int (*start)(struct udevice *dev);
  97. int (*send)(struct udevice *dev, void *packet, int length);
  98. int (*recv)(struct udevice *dev);
  99. void (*stop)(struct udevice *dev);
  100. #ifdef CONFIG_MCAST_TFTP
  101. int (*mcast)(struct udevice *dev, const u8 *enetaddr, int join);
  102. #endif
  103. int (*write_hwaddr)(struct udevice *dev);
  104. int (*read_rom_hwaddr)(struct udevice *dev);
  105. };
  106. #define eth_get_ops(dev) ((struct eth_ops *)(dev)->driver->ops)
  107. struct udevice *eth_get_dev(void); /* get the current device */
  108. unsigned char *eth_get_ethaddr(void); /* get the current device MAC */
  109. /* Used only when NetConsole is enabled */
  110. int eth_init_state_only(void); /* Set active state */
  111. void eth_halt_state_only(void); /* Set passive state */
  112. #endif
  113. #ifndef CONFIG_DM_ETH
  114. struct eth_device {
  115. char name[16];
  116. unsigned char enetaddr[6];
  117. phys_addr_t iobase;
  118. int state;
  119. int (*init) (struct eth_device *, bd_t *);
  120. int (*send) (struct eth_device *, void *packet, int length);
  121. int (*recv) (struct eth_device *);
  122. void (*halt) (struct eth_device *);
  123. #ifdef CONFIG_MCAST_TFTP
  124. int (*mcast) (struct eth_device *, const u8 *enetaddr, u8 set);
  125. #endif
  126. int (*write_hwaddr) (struct eth_device *);
  127. struct eth_device *next;
  128. int index;
  129. void *priv;
  130. };
  131. int eth_register(struct eth_device *dev);/* Register network device */
  132. int eth_unregister(struct eth_device *dev);/* Remove network device */
  133. extern struct eth_device *eth_current;
  134. static inline __attribute__((always_inline))
  135. struct eth_device *eth_get_dev(void)
  136. {
  137. return eth_current;
  138. }
  139. struct eth_device *eth_get_dev_by_name(const char *devname);
  140. struct eth_device *eth_get_dev_by_index(int index); /* get dev @ index */
  141. /* get the current device MAC */
  142. static inline unsigned char *eth_get_ethaddr(void)
  143. {
  144. if (eth_current)
  145. return eth_current->enetaddr;
  146. return NULL;
  147. }
  148. /* Set active state */
  149. static inline __attribute__((always_inline)) int eth_init_state_only(void)
  150. {
  151. eth_get_dev()->state = ETH_STATE_ACTIVE;
  152. return 0;
  153. }
  154. /* Set passive state */
  155. static inline __attribute__((always_inline)) void eth_halt_state_only(void)
  156. {
  157. eth_get_dev()->state = ETH_STATE_PASSIVE;
  158. }
  159. /*
  160. * Set the hardware address for an ethernet interface based on 'eth%daddr'
  161. * environment variable (or just 'ethaddr' if eth_number is 0).
  162. * Args:
  163. * base_name - base name for device (normally "eth")
  164. * eth_number - value of %d (0 for first device of this type)
  165. * Returns:
  166. * 0 is success, non-zero is error status from driver.
  167. */
  168. int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
  169. int eth_number);
  170. int usb_eth_initialize(bd_t *bi);
  171. #endif
  172. int eth_initialize(void); /* Initialize network subsystem */
  173. void eth_try_another(int first_restart); /* Change the device */
  174. void eth_set_current(void); /* set nterface to ethcur var */
  175. int eth_get_dev_index(void); /* get the device index */
  176. void eth_parse_enetaddr(const char *addr, uchar *enetaddr);
  177. int eth_getenv_enetaddr(char *name, uchar *enetaddr);
  178. int eth_setenv_enetaddr(char *name, const uchar *enetaddr);
  179. /*
  180. * Get the hardware address for an ethernet interface .
  181. * Args:
  182. * base_name - base name for device (normally "eth")
  183. * index - device index number (0 for first)
  184. * enetaddr - returns 6 byte hardware address
  185. * Returns:
  186. * Return true if the address is valid.
  187. */
  188. int eth_getenv_enetaddr_by_index(const char *base_name, int index,
  189. uchar *enetaddr);
  190. int eth_init(void); /* Initialize the device */
  191. int eth_send(void *packet, int length); /* Send a packet */
  192. #ifdef CONFIG_API
  193. int eth_receive(void *packet, int length); /* Receive a packet*/
  194. extern void (*push_packet)(void *packet, int length);
  195. #endif
  196. int eth_rx(void); /* Check for received packets */
  197. void eth_halt(void); /* stop SCC */
  198. const char *eth_get_name(void); /* get name of current device */
  199. #ifdef CONFIG_MCAST_TFTP
  200. int eth_mcast_join(IPaddr_t mcast_addr, int join);
  201. u32 ether_crc(size_t len, unsigned char const *p);
  202. #endif
  203. /**********************************************************************/
  204. /*
  205. * Protocol headers.
  206. */
  207. /*
  208. * Ethernet header
  209. */
  210. struct ethernet_hdr {
  211. uchar et_dest[6]; /* Destination node */
  212. uchar et_src[6]; /* Source node */
  213. ushort et_protlen; /* Protocol or length */
  214. };
  215. /* Ethernet header size */
  216. #define ETHER_HDR_SIZE (sizeof(struct ethernet_hdr))
  217. #define ETH_FCS_LEN 4 /* Octets in the FCS */
  218. struct e802_hdr {
  219. uchar et_dest[6]; /* Destination node */
  220. uchar et_src[6]; /* Source node */
  221. ushort et_protlen; /* Protocol or length */
  222. uchar et_dsap; /* 802 DSAP */
  223. uchar et_ssap; /* 802 SSAP */
  224. uchar et_ctl; /* 802 control */
  225. uchar et_snap1; /* SNAP */
  226. uchar et_snap2;
  227. uchar et_snap3;
  228. ushort et_prot; /* 802 protocol */
  229. };
  230. /* 802 + SNAP + ethernet header size */
  231. #define E802_HDR_SIZE (sizeof(struct e802_hdr))
  232. /*
  233. * Virtual LAN Ethernet header
  234. */
  235. struct vlan_ethernet_hdr {
  236. uchar vet_dest[6]; /* Destination node */
  237. uchar vet_src[6]; /* Source node */
  238. ushort vet_vlan_type; /* PROT_VLAN */
  239. ushort vet_tag; /* TAG of VLAN */
  240. ushort vet_type; /* protocol type */
  241. };
  242. /* VLAN Ethernet header size */
  243. #define VLAN_ETHER_HDR_SIZE (sizeof(struct vlan_ethernet_hdr))
  244. #define PROT_IP 0x0800 /* IP protocol */
  245. #define PROT_ARP 0x0806 /* IP ARP protocol */
  246. #define PROT_RARP 0x8035 /* IP ARP protocol */
  247. #define PROT_VLAN 0x8100 /* IEEE 802.1q protocol */
  248. #define IPPROTO_ICMP 1 /* Internet Control Message Protocol */
  249. #define IPPROTO_UDP 17 /* User Datagram Protocol */
  250. /*
  251. * Internet Protocol (IP) header.
  252. */
  253. struct ip_hdr {
  254. uchar ip_hl_v; /* header length and version */
  255. uchar ip_tos; /* type of service */
  256. ushort ip_len; /* total length */
  257. ushort ip_id; /* identification */
  258. ushort ip_off; /* fragment offset field */
  259. uchar ip_ttl; /* time to live */
  260. uchar ip_p; /* protocol */
  261. ushort ip_sum; /* checksum */
  262. IPaddr_t ip_src; /* Source IP address */
  263. IPaddr_t ip_dst; /* Destination IP address */
  264. };
  265. #define IP_OFFS 0x1fff /* ip offset *= 8 */
  266. #define IP_FLAGS 0xe000 /* first 3 bits */
  267. #define IP_FLAGS_RES 0x8000 /* reserved */
  268. #define IP_FLAGS_DFRAG 0x4000 /* don't fragments */
  269. #define IP_FLAGS_MFRAG 0x2000 /* more fragments */
  270. #define IP_HDR_SIZE (sizeof(struct ip_hdr))
  271. /*
  272. * Internet Protocol (IP) + UDP header.
  273. */
  274. struct ip_udp_hdr {
  275. uchar ip_hl_v; /* header length and version */
  276. uchar ip_tos; /* type of service */
  277. ushort ip_len; /* total length */
  278. ushort ip_id; /* identification */
  279. ushort ip_off; /* fragment offset field */
  280. uchar ip_ttl; /* time to live */
  281. uchar ip_p; /* protocol */
  282. ushort ip_sum; /* checksum */
  283. IPaddr_t ip_src; /* Source IP address */
  284. IPaddr_t ip_dst; /* Destination IP address */
  285. ushort udp_src; /* UDP source port */
  286. ushort udp_dst; /* UDP destination port */
  287. ushort udp_len; /* Length of UDP packet */
  288. ushort udp_xsum; /* Checksum */
  289. };
  290. #define IP_UDP_HDR_SIZE (sizeof(struct ip_udp_hdr))
  291. #define UDP_HDR_SIZE (IP_UDP_HDR_SIZE - IP_HDR_SIZE)
  292. /*
  293. * Address Resolution Protocol (ARP) header.
  294. */
  295. struct arp_hdr {
  296. ushort ar_hrd; /* Format of hardware address */
  297. # define ARP_ETHER 1 /* Ethernet hardware address */
  298. ushort ar_pro; /* Format of protocol address */
  299. uchar ar_hln; /* Length of hardware address */
  300. # define ARP_HLEN 6
  301. uchar ar_pln; /* Length of protocol address */
  302. # define ARP_PLEN 4
  303. ushort ar_op; /* Operation */
  304. # define ARPOP_REQUEST 1 /* Request to resolve address */
  305. # define ARPOP_REPLY 2 /* Response to previous request */
  306. # define RARPOP_REQUEST 3 /* Request to resolve address */
  307. # define RARPOP_REPLY 4 /* Response to previous request */
  308. /*
  309. * The remaining fields are variable in size, according to
  310. * the sizes above, and are defined as appropriate for
  311. * specific hardware/protocol combinations.
  312. */
  313. uchar ar_data[0];
  314. #define ar_sha ar_data[0]
  315. #define ar_spa ar_data[ARP_HLEN]
  316. #define ar_tha ar_data[ARP_HLEN + ARP_PLEN]
  317. #define ar_tpa ar_data[ARP_HLEN + ARP_PLEN + ARP_HLEN]
  318. #if 0
  319. uchar ar_sha[]; /* Sender hardware address */
  320. uchar ar_spa[]; /* Sender protocol address */
  321. uchar ar_tha[]; /* Target hardware address */
  322. uchar ar_tpa[]; /* Target protocol address */
  323. #endif /* 0 */
  324. };
  325. #define ARP_HDR_SIZE (8+20) /* Size assuming ethernet */
  326. /*
  327. * ICMP stuff (just enough to handle (host) redirect messages)
  328. */
  329. #define ICMP_ECHO_REPLY 0 /* Echo reply */
  330. #define ICMP_NOT_REACH 3 /* Detination unreachable */
  331. #define ICMP_REDIRECT 5 /* Redirect (change route) */
  332. #define ICMP_ECHO_REQUEST 8 /* Echo request */
  333. /* Codes for REDIRECT. */
  334. #define ICMP_REDIR_NET 0 /* Redirect Net */
  335. #define ICMP_REDIR_HOST 1 /* Redirect Host */
  336. /* Codes for NOT_REACH */
  337. #define ICMP_NOT_REACH_PORT 3 /* Port unreachable */
  338. struct icmp_hdr {
  339. uchar type;
  340. uchar code;
  341. ushort checksum;
  342. union {
  343. struct {
  344. ushort id;
  345. ushort sequence;
  346. } echo;
  347. ulong gateway;
  348. struct {
  349. ushort unused;
  350. ushort mtu;
  351. } frag;
  352. uchar data[0];
  353. } un;
  354. };
  355. #define ICMP_HDR_SIZE (sizeof(struct icmp_hdr))
  356. #define IP_ICMP_HDR_SIZE (IP_HDR_SIZE + ICMP_HDR_SIZE)
  357. /*
  358. * Maximum packet size; used to allocate packet storage.
  359. * TFTP packets can be 524 bytes + IP header + ethernet header.
  360. * Lets be conservative, and go for 38 * 16. (Must also be
  361. * a multiple of 32 bytes).
  362. */
  363. /*
  364. * AS.HARNOIS : Better to set PKTSIZE to maximum size because
  365. * traffic type is not always controlled
  366. * maximum packet size = 1518
  367. * maximum packet size and multiple of 32 bytes = 1536
  368. */
  369. #define PKTSIZE 1518
  370. #define PKTSIZE_ALIGN 1536
  371. /*#define PKTSIZE 608*/
  372. /*
  373. * Maximum receive ring size; that is, the number of packets
  374. * we can buffer before overflow happens. Basically, this just
  375. * needs to be enough to prevent a packet being discarded while
  376. * we are processing the previous one.
  377. */
  378. #define RINGSZ 4
  379. #define RINGSZ_LOG2 2
  380. /**********************************************************************/
  381. /*
  382. * Globals.
  383. *
  384. * Note:
  385. *
  386. * All variables of type IPaddr_t are stored in NETWORK byte order
  387. * (big endian).
  388. */
  389. /* net.c */
  390. /** BOOTP EXTENTIONS **/
  391. extern IPaddr_t NetOurGatewayIP; /* Our gateway IP address */
  392. extern IPaddr_t NetOurSubnetMask; /* Our subnet mask (0 = unknown) */
  393. extern IPaddr_t NetOurDNSIP; /* Our Domain Name Server (0 = unknown) */
  394. #if defined(CONFIG_BOOTP_DNS2)
  395. extern IPaddr_t NetOurDNS2IP; /* Our 2nd Domain Name Server (0 = unknown) */
  396. #endif
  397. extern char NetOurNISDomain[32]; /* Our NIS domain */
  398. extern char NetOurHostName[32]; /* Our hostname */
  399. extern char NetOurRootPath[64]; /* Our root path */
  400. extern ushort NetBootFileSize; /* Our boot file size in blocks */
  401. /** END OF BOOTP EXTENTIONS **/
  402. extern ulong NetBootFileXferSize; /* size of bootfile in bytes */
  403. extern uchar NetOurEther[6]; /* Our ethernet address */
  404. extern uchar NetServerEther[6]; /* Boot server enet address */
  405. extern IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */
  406. extern IPaddr_t NetServerIP; /* Server IP addr (0 = unknown) */
  407. extern uchar *NetTxPacket; /* THE transmit packet */
  408. #ifdef CONFIG_DM_ETH
  409. extern uchar *net_rx_packets[PKTBUFSRX]; /* Receive packets */
  410. #else
  411. extern uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */
  412. #endif
  413. extern uchar *NetRxPacket; /* Current receive packet */
  414. extern int NetRxPacketLen; /* Current rx packet length */
  415. extern unsigned NetIPID; /* IP ID (counting) */
  416. extern uchar NetBcastAddr[6]; /* Ethernet boardcast address */
  417. extern uchar NetEtherNullAddr[6];
  418. #define VLAN_NONE 4095 /* untagged */
  419. #define VLAN_IDMASK 0x0fff /* mask of valid vlan id */
  420. extern ushort NetOurVLAN; /* Our VLAN */
  421. extern ushort NetOurNativeVLAN; /* Our Native VLAN */
  422. extern int NetRestartWrap; /* Tried all network devices */
  423. enum proto_t {
  424. BOOTP, RARP, ARP, TFTPGET, DHCP, PING, DNS, NFS, CDP, NETCONS, SNTP,
  425. TFTPSRV, TFTPPUT, LINKLOCAL
  426. };
  427. /* from net/net.c */
  428. extern char BootFile[128]; /* Boot File name */
  429. #if defined(CONFIG_CMD_DNS)
  430. extern char *NetDNSResolve; /* The host to resolve */
  431. extern char *NetDNSenvvar; /* the env var to put the ip into */
  432. #endif
  433. #if defined(CONFIG_CMD_PING)
  434. extern IPaddr_t NetPingIP; /* the ip address to ping */
  435. #endif
  436. #if defined(CONFIG_CMD_CDP)
  437. /* when CDP completes these hold the return values */
  438. extern ushort CDPNativeVLAN; /* CDP returned native VLAN */
  439. extern ushort CDPApplianceVLAN; /* CDP returned appliance VLAN */
  440. /*
  441. * Check for a CDP packet by examining the received MAC address field
  442. */
  443. static inline int is_cdp_packet(const uchar *et_addr)
  444. {
  445. extern const uchar NetCDPAddr[6];
  446. return memcmp(et_addr, NetCDPAddr, 6) == 0;
  447. }
  448. #endif
  449. #if defined(CONFIG_CMD_SNTP)
  450. extern IPaddr_t NetNtpServerIP; /* the ip address to NTP */
  451. extern int NetTimeOffset; /* offset time from UTC */
  452. #endif
  453. #if defined(CONFIG_MCAST_TFTP)
  454. extern IPaddr_t Mcast_addr;
  455. #endif
  456. /* Initialize the network adapter */
  457. void net_init(void);
  458. int NetLoop(enum proto_t);
  459. /* Shutdown adapters and cleanup */
  460. void NetStop(void);
  461. /* Load failed. Start again. */
  462. void NetStartAgain(void);
  463. /* Get size of the ethernet header when we send */
  464. int NetEthHdrSize(void);
  465. /* Set ethernet header; returns the size of the header */
  466. int NetSetEther(uchar *, uchar *, uint);
  467. int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot);
  468. /* Set IP header */
  469. void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source);
  470. void net_set_udp_header(uchar *pkt, IPaddr_t dest, int dport,
  471. int sport, int len);
  472. /**
  473. * compute_ip_checksum() - Compute IP checksum
  474. *
  475. * @addr: Address to check (must be 16-bit aligned)
  476. * @nbytes: Number of bytes to check (normally a multiple of 2)
  477. * @return 16-bit IP checksum
  478. */
  479. unsigned compute_ip_checksum(const void *addr, unsigned nbytes);
  480. /**
  481. * add_ip_checksums() - add two IP checksums
  482. *
  483. * @offset: Offset of first sum (if odd we do a byte-swap)
  484. * @sum: First checksum
  485. * @new_sum: New checksum to add
  486. * @return updated 16-bit IP checksum
  487. */
  488. unsigned add_ip_checksums(unsigned offset, unsigned sum, unsigned new_sum);
  489. /**
  490. * ip_checksum_ok() - check if a checksum is correct
  491. *
  492. * This works by making sure the checksum sums to 0
  493. *
  494. * @addr: Address to check (must be 16-bit aligned)
  495. * @nbytes: Number of bytes to check (normally a multiple of 2)
  496. * @return true if the checksum matches, false if not
  497. */
  498. int ip_checksum_ok(const void *addr, unsigned nbytes);
  499. /* Callbacks */
  500. rxhand_f *net_get_udp_handler(void); /* Get UDP RX packet handler */
  501. void net_set_udp_handler(rxhand_f *); /* Set UDP RX packet handler */
  502. rxhand_f *net_get_arp_handler(void); /* Get ARP RX packet handler */
  503. void net_set_arp_handler(rxhand_f *); /* Set ARP RX packet handler */
  504. void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */
  505. void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */
  506. /* Network loop state */
  507. enum net_loop_state {
  508. NETLOOP_CONTINUE,
  509. NETLOOP_RESTART,
  510. NETLOOP_SUCCESS,
  511. NETLOOP_FAIL
  512. };
  513. extern enum net_loop_state net_state;
  514. static inline void net_set_state(enum net_loop_state state)
  515. {
  516. debug_cond(DEBUG_INT_STATE, "--- NetState set to %d\n", state);
  517. net_state = state;
  518. }
  519. /* Transmit a packet */
  520. static inline void NetSendPacket(uchar *pkt, int len)
  521. {
  522. (void) eth_send(pkt, len);
  523. }
  524. /*
  525. * Transmit "NetTxPacket" as UDP packet, performing ARP request if needed
  526. * (ether will be populated)
  527. *
  528. * @param ether Raw packet buffer
  529. * @param dest IP address to send the datagram to
  530. * @param dport Destination UDP port
  531. * @param sport Source UDP port
  532. * @param payload_len Length of data after the UDP header
  533. */
  534. int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport,
  535. int sport, int payload_len);
  536. #ifndef CONFIG_DM_ETH
  537. #define NetReceive(in_packet, len) net_process_received_packet(in_packet, len)
  538. #endif
  539. /* Processes a received packet */
  540. void net_process_received_packet(uchar *in_packet, int len);
  541. #ifdef CONFIG_NETCONSOLE
  542. void NcStart(void);
  543. int nc_input_packet(uchar *pkt, IPaddr_t src_ip, unsigned dest_port,
  544. unsigned src_port, unsigned len);
  545. #endif
  546. static inline __attribute__((always_inline)) int eth_is_on_demand_init(void)
  547. {
  548. #ifdef CONFIG_NETCONSOLE
  549. extern enum proto_t net_loop_last_protocol;
  550. return net_loop_last_protocol != NETCONS;
  551. #else
  552. return 1;
  553. #endif
  554. }
  555. static inline void eth_set_last_protocol(int protocol)
  556. {
  557. #ifdef CONFIG_NETCONSOLE
  558. extern enum proto_t net_loop_last_protocol;
  559. net_loop_last_protocol = protocol;
  560. #endif
  561. }
  562. /*
  563. * Check if autoload is enabled. If so, use either NFS or TFTP to download
  564. * the boot file.
  565. */
  566. void net_auto_load(void);
  567. /*
  568. * The following functions are a bit ugly, but necessary to deal with
  569. * alignment restrictions on ARM.
  570. *
  571. * We're using inline functions, which had the smallest memory
  572. * footprint in our tests.
  573. */
  574. /* return IP *in network byteorder* */
  575. static inline IPaddr_t NetReadIP(void *from)
  576. {
  577. IPaddr_t ip;
  578. memcpy((void *)&ip, (void *)from, sizeof(ip));
  579. return ip;
  580. }
  581. /* return ulong *in network byteorder* */
  582. static inline ulong NetReadLong(ulong *from)
  583. {
  584. ulong l;
  585. memcpy((void *)&l, (void *)from, sizeof(l));
  586. return l;
  587. }
  588. /* write IP *in network byteorder* */
  589. static inline void NetWriteIP(void *to, IPaddr_t ip)
  590. {
  591. memcpy(to, (void *)&ip, sizeof(ip));
  592. }
  593. /* copy IP */
  594. static inline void NetCopyIP(void *to, void *from)
  595. {
  596. memcpy((void *)to, from, sizeof(IPaddr_t));
  597. }
  598. /* copy ulong */
  599. static inline void NetCopyLong(ulong *to, ulong *from)
  600. {
  601. memcpy((void *)to, (void *)from, sizeof(ulong));
  602. }
  603. /**
  604. * is_zero_ether_addr - Determine if give Ethernet address is all zeros.
  605. * @addr: Pointer to a six-byte array containing the Ethernet address
  606. *
  607. * Return true if the address is all zeroes.
  608. */
  609. static inline int is_zero_ether_addr(const u8 *addr)
  610. {
  611. return !(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]);
  612. }
  613. /**
  614. * is_multicast_ether_addr - Determine if the Ethernet address is a multicast.
  615. * @addr: Pointer to a six-byte array containing the Ethernet address
  616. *
  617. * Return true if the address is a multicast address.
  618. * By definition the broadcast address is also a multicast address.
  619. */
  620. static inline int is_multicast_ether_addr(const u8 *addr)
  621. {
  622. return 0x01 & addr[0];
  623. }
  624. /*
  625. * is_broadcast_ether_addr - Determine if the Ethernet address is broadcast
  626. * @addr: Pointer to a six-byte array containing the Ethernet address
  627. *
  628. * Return true if the address is the broadcast address.
  629. */
  630. static inline int is_broadcast_ether_addr(const u8 *addr)
  631. {
  632. return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) ==
  633. 0xff;
  634. }
  635. /*
  636. * is_valid_ether_addr - Determine if the given Ethernet address is valid
  637. * @addr: Pointer to a six-byte array containing the Ethernet address
  638. *
  639. * Check that the Ethernet address (MAC) is not 00:00:00:00:00:00, is not
  640. * a multicast address, and is not FF:FF:FF:FF:FF:FF.
  641. *
  642. * Return true if the address is valid.
  643. */
  644. static inline int is_valid_ether_addr(const u8 *addr)
  645. {
  646. /* FF:FF:FF:FF:FF:FF is a multicast address so we don't need to
  647. * explicitly check for it here. */
  648. return !is_multicast_ether_addr(addr) && !is_zero_ether_addr(addr);
  649. }
  650. /**
  651. * eth_random_addr - Generate software assigned random Ethernet address
  652. * @addr: Pointer to a six-byte array containing the Ethernet address
  653. *
  654. * Generate a random Ethernet address (MAC) that is not multicast
  655. * and has the local assigned bit set.
  656. */
  657. static inline void eth_random_addr(uchar *addr)
  658. {
  659. int i;
  660. unsigned int seed = get_timer(0);
  661. for (i = 0; i < 6; i++)
  662. addr[i] = rand_r(&seed);
  663. addr[0] &= 0xfe; /* clear multicast bit */
  664. addr[0] |= 0x02; /* set local assignment bit (IEEE802) */
  665. }
  666. /* Convert an IP address to a string */
  667. void ip_to_string(IPaddr_t x, char *s);
  668. /* Convert a string to ip address */
  669. IPaddr_t string_to_ip(const char *s);
  670. /* Convert a VLAN id to a string */
  671. void VLAN_to_string(ushort x, char *s);
  672. /* Convert a string to a vlan id */
  673. ushort string_to_VLAN(const char *s);
  674. /* read a VLAN id from an environment variable */
  675. ushort getenv_VLAN(char *);
  676. /* copy a filename (allow for "..." notation, limit length) */
  677. void copy_filename(char *dst, const char *src, int size);
  678. /* get a random source port */
  679. unsigned int random_port(void);
  680. /* Update U-Boot over TFTP */
  681. int update_tftp(ulong addr);
  682. /**********************************************************************/
  683. #endif /* __NET_H__ */