net.h 21 KB

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