net.h 19 KB

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