net.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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 u8 net_ethaddr[6]; /* Our ethernet address */
  419. extern u8 net_server_ethaddr[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 *net_tx_packet; /* THE transmit packet */
  423. extern uchar *net_rx_packets[PKTBUFSRX]; /* Receive packets */
  424. extern uchar *net_rx_packet; /* Current receive packet */
  425. extern int net_rx_packet_len; /* Current rx packet length */
  426. extern const u8 net_bcast_ethaddr[6]; /* Ethernet broadcast address */
  427. extern const u8 net_null_ethaddr[6];
  428. #define VLAN_NONE 4095 /* untagged */
  429. #define VLAN_IDMASK 0x0fff /* mask of valid vlan id */
  430. extern ushort net_our_vlan; /* Our VLAN */
  431. extern ushort net_native_vlan; /* Our Native VLAN */
  432. extern int net_restart_wrap; /* Tried all network devices */
  433. enum proto_t {
  434. BOOTP, RARP, ARP, TFTPGET, DHCP, PING, DNS, NFS, CDP, NETCONS, SNTP,
  435. TFTPSRV, TFTPPUT, LINKLOCAL
  436. };
  437. extern char net_boot_file_name[128];/* Boot File name */
  438. /* The actual transferred size of the bootfile (in bytes) */
  439. extern u32 net_boot_file_size;
  440. /* Boot file size in blocks as reported by the DHCP server */
  441. extern u32 net_boot_file_expected_size_in_blocks;
  442. #if defined(CONFIG_CMD_DNS)
  443. extern char *net_dns_resolve; /* The host to resolve */
  444. extern char *net_dns_env_var; /* the env var to put the ip into */
  445. #endif
  446. #if defined(CONFIG_CMD_PING)
  447. extern struct in_addr net_ping_ip; /* the ip address to ping */
  448. #endif
  449. #if defined(CONFIG_CMD_CDP)
  450. /* when CDP completes these hold the return values */
  451. extern ushort cdp_native_vlan; /* CDP returned native VLAN */
  452. extern ushort cdp_appliance_vlan; /* CDP returned appliance VLAN */
  453. /*
  454. * Check for a CDP packet by examining the received MAC address field
  455. */
  456. static inline int is_cdp_packet(const uchar *ethaddr)
  457. {
  458. extern const u8 net_cdp_ethaddr[6];
  459. return memcmp(ethaddr, net_cdp_ethaddr, 6) == 0;
  460. }
  461. #endif
  462. #if defined(CONFIG_CMD_SNTP)
  463. extern struct in_addr net_ntp_server; /* the ip address to NTP */
  464. extern int net_ntp_time_offset; /* offset time from UTC */
  465. #endif
  466. #if defined(CONFIG_MCAST_TFTP)
  467. extern struct in_addr net_mcast_addr;
  468. #endif
  469. /* Initialize the network adapter */
  470. void net_init(void);
  471. int net_loop(enum proto_t);
  472. /* Load failed. Start again. */
  473. int net_start_again(void);
  474. /* Get size of the ethernet header when we send */
  475. int net_eth_hdr_size(void);
  476. /* Set ethernet header; returns the size of the header */
  477. int net_set_ether(uchar *xet, const uchar *dest_ethaddr, uint prot);
  478. int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot);
  479. /* Set IP header */
  480. void net_set_ip_header(uchar *pkt, struct in_addr dest, struct in_addr source);
  481. void net_set_udp_header(uchar *pkt, struct in_addr dest, int dport,
  482. int sport, int len);
  483. /**
  484. * compute_ip_checksum() - Compute IP checksum
  485. *
  486. * @addr: Address to check (must be 16-bit aligned)
  487. * @nbytes: Number of bytes to check (normally a multiple of 2)
  488. * @return 16-bit IP checksum
  489. */
  490. unsigned compute_ip_checksum(const void *addr, unsigned nbytes);
  491. /**
  492. * add_ip_checksums() - add two IP checksums
  493. *
  494. * @offset: Offset of first sum (if odd we do a byte-swap)
  495. * @sum: First checksum
  496. * @new_sum: New checksum to add
  497. * @return updated 16-bit IP checksum
  498. */
  499. unsigned add_ip_checksums(unsigned offset, unsigned sum, unsigned new_sum);
  500. /**
  501. * ip_checksum_ok() - check if a checksum is correct
  502. *
  503. * This works by making sure the checksum sums to 0
  504. *
  505. * @addr: Address to check (must be 16-bit aligned)
  506. * @nbytes: Number of bytes to check (normally a multiple of 2)
  507. * @return true if the checksum matches, false if not
  508. */
  509. int ip_checksum_ok(const void *addr, unsigned nbytes);
  510. /* Callbacks */
  511. rxhand_f *net_get_udp_handler(void); /* Get UDP RX packet handler */
  512. void net_set_udp_handler(rxhand_f *); /* Set UDP RX packet handler */
  513. rxhand_f *net_get_arp_handler(void); /* Get ARP RX packet handler */
  514. void net_set_arp_handler(rxhand_f *); /* Set ARP RX packet handler */
  515. void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */
  516. void net_set_timeout_handler(ulong, thand_f *);/* Set timeout handler */
  517. /* Network loop state */
  518. enum net_loop_state {
  519. NETLOOP_CONTINUE,
  520. NETLOOP_RESTART,
  521. NETLOOP_SUCCESS,
  522. NETLOOP_FAIL
  523. };
  524. extern enum net_loop_state net_state;
  525. static inline void net_set_state(enum net_loop_state state)
  526. {
  527. debug_cond(DEBUG_INT_STATE, "--- NetState set to %d\n", state);
  528. net_state = state;
  529. }
  530. /* Transmit a packet */
  531. static inline void net_send_packet(uchar *pkt, int len)
  532. {
  533. /* Currently no way to return errors from eth_send() */
  534. (void) eth_send(pkt, len);
  535. }
  536. /*
  537. * Transmit "net_tx_packet" as UDP packet, performing ARP request if needed
  538. * (ether will be populated)
  539. *
  540. * @param ether Raw packet buffer
  541. * @param dest IP address to send the datagram to
  542. * @param dport Destination UDP port
  543. * @param sport Source UDP port
  544. * @param payload_len Length of data after the UDP header
  545. */
  546. int net_send_udp_packet(uchar *ether, struct in_addr dest, int dport,
  547. int sport, int payload_len);
  548. /* Processes a received packet */
  549. void net_process_received_packet(uchar *in_packet, int len);
  550. #ifdef CONFIG_NETCONSOLE
  551. void nc_start(void);
  552. int nc_input_packet(uchar *pkt, struct in_addr src_ip, unsigned dest_port,
  553. unsigned src_port, unsigned len);
  554. #endif
  555. static inline __attribute__((always_inline)) int eth_is_on_demand_init(void)
  556. {
  557. #ifdef CONFIG_NETCONSOLE
  558. extern enum proto_t net_loop_last_protocol;
  559. return net_loop_last_protocol != NETCONS;
  560. #else
  561. return 1;
  562. #endif
  563. }
  564. static inline void eth_set_last_protocol(int protocol)
  565. {
  566. #ifdef CONFIG_NETCONSOLE
  567. extern enum proto_t net_loop_last_protocol;
  568. net_loop_last_protocol = protocol;
  569. #endif
  570. }
  571. /*
  572. * Check if autoload is enabled. If so, use either NFS or TFTP to download
  573. * the boot file.
  574. */
  575. void net_auto_load(void);
  576. /*
  577. * The following functions are a bit ugly, but necessary to deal with
  578. * alignment restrictions on ARM.
  579. *
  580. * We're using inline functions, which had the smallest memory
  581. * footprint in our tests.
  582. */
  583. /* return IP *in network byteorder* */
  584. static inline struct in_addr net_read_ip(void *from)
  585. {
  586. struct in_addr ip;
  587. memcpy((void *)&ip, (void *)from, sizeof(ip));
  588. return ip;
  589. }
  590. /* return ulong *in network byteorder* */
  591. static inline ulong net_read_long(ulong *from)
  592. {
  593. ulong l;
  594. memcpy((void *)&l, (void *)from, sizeof(l));
  595. return l;
  596. }
  597. /* write IP *in network byteorder* */
  598. static inline void net_write_ip(void *to, struct in_addr ip)
  599. {
  600. memcpy(to, (void *)&ip, sizeof(ip));
  601. }
  602. /* copy IP */
  603. static inline void net_copy_ip(void *to, void *from)
  604. {
  605. memcpy((void *)to, from, sizeof(struct in_addr));
  606. }
  607. /* copy ulong */
  608. static inline void net_copy_long(ulong *to, ulong *from)
  609. {
  610. memcpy((void *)to, (void *)from, sizeof(ulong));
  611. }
  612. /**
  613. * is_zero_ethaddr - Determine if give Ethernet address is all zeros.
  614. * @addr: Pointer to a six-byte array containing the Ethernet address
  615. *
  616. * Return true if the address is all zeroes.
  617. */
  618. static inline int is_zero_ethaddr(const u8 *addr)
  619. {
  620. return !(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]);
  621. }
  622. /**
  623. * is_multicast_ethaddr - Determine if the Ethernet address is a multicast.
  624. * @addr: Pointer to a six-byte array containing the Ethernet address
  625. *
  626. * Return true if the address is a multicast address.
  627. * By definition the broadcast address is also a multicast address.
  628. */
  629. static inline int is_multicast_ethaddr(const u8 *addr)
  630. {
  631. return 0x01 & addr[0];
  632. }
  633. /*
  634. * is_broadcast_ethaddr - Determine if the Ethernet address is broadcast
  635. * @addr: Pointer to a six-byte array containing the Ethernet address
  636. *
  637. * Return true if the address is the broadcast address.
  638. */
  639. static inline int is_broadcast_ethaddr(const u8 *addr)
  640. {
  641. return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) ==
  642. 0xff;
  643. }
  644. /*
  645. * is_valid_ethaddr - Determine if the given Ethernet address is valid
  646. * @addr: Pointer to a six-byte array containing the Ethernet address
  647. *
  648. * Check that the Ethernet address (MAC) is not 00:00:00:00:00:00, is not
  649. * a multicast address, and is not FF:FF:FF:FF:FF:FF.
  650. *
  651. * Return true if the address is valid.
  652. */
  653. static inline int is_valid_ethaddr(const u8 *addr)
  654. {
  655. /* FF:FF:FF:FF:FF:FF is a multicast address so we don't need to
  656. * explicitly check for it here. */
  657. return !is_multicast_ethaddr(addr) && !is_zero_ethaddr(addr);
  658. }
  659. /**
  660. * net_random_ethaddr - Generate software assigned random Ethernet address
  661. * @addr: Pointer to a six-byte array containing the Ethernet address
  662. *
  663. * Generate a random Ethernet address (MAC) that is not multicast
  664. * and has the local assigned bit set.
  665. */
  666. static inline void net_random_ethaddr(uchar *addr)
  667. {
  668. int i;
  669. unsigned int seed = get_timer(0);
  670. for (i = 0; i < 6; i++)
  671. addr[i] = rand_r(&seed);
  672. addr[0] &= 0xfe; /* clear multicast bit */
  673. addr[0] |= 0x02; /* set local assignment bit (IEEE802) */
  674. }
  675. /* Convert an IP address to a string */
  676. void ip_to_string(struct in_addr x, char *s);
  677. /* Convert a string to ip address */
  678. struct in_addr string_to_ip(const char *s);
  679. /* Convert a VLAN id to a string */
  680. void vlan_to_string(ushort x, char *s);
  681. /* Convert a string to a vlan id */
  682. ushort string_to_vlan(const char *s);
  683. /* read a VLAN id from an environment variable */
  684. ushort getenv_vlan(char *);
  685. /* copy a filename (allow for "..." notation, limit length) */
  686. void copy_filename(char *dst, const char *src, int size);
  687. /* get a random source port */
  688. unsigned int random_port(void);
  689. /* Update U-Boot over TFTP */
  690. int update_tftp(ulong addr);
  691. /**********************************************************************/
  692. #endif /* __NET_H__ */