net.h 24 KB

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