if_ether.h revision 38451
18876Srgrimes/*	$NetBSD: if_ether.h,v 1.25 1997/01/17 17:06:06 mikel Exp $	*/
24Srgrimes
34Srgrimes/*
44Srgrimes * Copyright (c) 1982, 1986, 1993
58876Srgrimes *	The Regents of the University of California.  All rights reserved.
64Srgrimes *
74Srgrimes * Redistribution and use in source and binary forms, with or without
84Srgrimes * modification, are permitted provided that the following conditions
94Srgrimes * are met:
104Srgrimes * 1. Redistributions of source code must retain the above copyright
118876Srgrimes *    notice, this list of conditions and the following disclaimer.
128876Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
134Srgrimes *    notice, this list of conditions and the following disclaimer in the
144Srgrimes *    documentation and/or other materials provided with the distribution.
158876Srgrimes * 3. All advertising materials mentioning features or use of this software
164Srgrimes *    must display the following acknowledgement:
178876Srgrimes *	This product includes software developed by the University of
184Srgrimes *	California, Berkeley and its contributors.
194Srgrimes * 4. Neither the name of the University nor the names of its contributors
204Srgrimes *    may be used to endorse or promote products derived from this software
214Srgrimes *    without specific prior written permission.
228876Srgrimes *
234Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
244Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
254Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2612515Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
274Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28623Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
294Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
304Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
314Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
324Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
334Srgrimes * SUCH DAMAGE.
342056Swollman *
352056Swollman *	@(#)if_ether.h	8.1 (Berkeley) 6/10/93
362056Swollman */
372056Swollman
382056Swollman/*
392056Swollman * Ethernet address - 6 octets
404Srgrimes * this is only used by the ethers(3) functions.
414Srgrimes */
424Srgrimesstruct ether_addr {
434Srgrimes	u_int8_t ether_addr_octet[6];
444Srgrimes};
454Srgrimes
464Srgrimes/*
474Srgrimes * Structure of a 10Mb/s Ethernet header.
484Srgrimes */
494Srgrimes#define	ETHER_ADDR_LEN	6
504Srgrimes
514Srgrimesstruct	ether_header {
524Srgrimes	u_int8_t  ether_dhost[ETHER_ADDR_LEN];
534Srgrimes	u_int8_t  ether_shost[ETHER_ADDR_LEN];
544Srgrimes	u_int16_t ether_type;
554Srgrimes};
564Srgrimes
574Srgrimes#define	ETHERTYPE_PUP		0x0200	/* PUP protocol */
584Srgrimes#define	ETHERTYPE_IP		0x0800	/* IP protocol */
594Srgrimes#define	ETHERTYPE_ARP		0x0806	/* address resolution protocol */
6012515Sphk#define	ETHERTYPE_REVARP	0x8035	/* reverse addr resolution protocol */
6112515Sphk
6212515Sphk/*
6312515Sphk * The ETHERTYPE_NTRAILER packet types starting at ETHERTYPE_TRAIL have
6412515Sphk * (type-ETHERTYPE_TRAIL)*512 bytes of data followed
6512473Sbde * by an ETHER type (as given above) and then the (variable-length) header.
664Srgrimes */
674Srgrimes#define	ETHERTYPE_TRAIL		0x1000		/* Trailer packet */
684Srgrimes#define	ETHERTYPE_NTRAILER	16
694Srgrimes
704Srgrimes#define	ETHER_IS_MULTICAST(addr) (*(addr) & 0x01) /* is address mcast/bcast? */
714Srgrimes
724Srgrimes#define	ETHERMTU	1500
734Srgrimes#define	ETHERMIN	(60-14)
744Srgrimes
754Srgrimes#ifdef _KERNEL
764Srgrimes/*
774Srgrimes * Macro to map an IP multicast address to an Ethernet multicast address.
784Srgrimes * The high-order 25 bits of the Ethernet address are statically assigned,
794Srgrimes * and the low-order 23 bits are taken from the low end of the IP address.
804Srgrimes */
814Srgrimes#define ETHER_MAP_IP_MULTICAST(ipaddr, enaddr)				\
824Srgrimes	/* struct in_addr *ipaddr; */					\
834Srgrimes	/* u_int8_t enaddr[ETHER_ADDR_LEN]; */				\
844Srgrimes{									\
854Srgrimes	(enaddr)[0] = 0x01;						\
864Srgrimes	(enaddr)[1] = 0x00;						\
874Srgrimes	(enaddr)[2] = 0x5e;						\
884Srgrimes	(enaddr)[3] = ((u_int8_t *)ipaddr)[1] & 0x7f;			\
894Srgrimes	(enaddr)[4] = ((u_int8_t *)ipaddr)[2];				\
904Srgrimes	(enaddr)[5] = ((u_int8_t *)ipaddr)[3];				\
914Srgrimes}
924Srgrimes#endif
934Srgrimes
944Srgrimes/*
954Srgrimes * Ethernet Address Resolution Protocol.
964Srgrimes *
974Srgrimes * See RFC 826 for protocol description.  Structure below is adapted
984Srgrimes * to resolving internet addresses.  Field names used correspond to
994Srgrimes * RFC 826.
1004Srgrimes */
1014Srgrimesstruct	ether_arp {
1024Srgrimes	struct	 arphdr ea_hdr;			/* fixed-size header */
1034Srgrimes	u_int8_t arp_sha[ETHER_ADDR_LEN];	/* sender hardware address */
1044Srgrimes	u_int8_t arp_spa[4];			/* sender protocol address */
1054Srgrimes	u_int8_t arp_tha[ETHER_ADDR_LEN];	/* target hardware address */
1064Srgrimes	u_int8_t arp_tpa[4];			/* target protocol address */
1074Srgrimes};
1084Srgrimes#define	arp_hrd	ea_hdr.ar_hrd
1094Srgrimes#define	arp_pro	ea_hdr.ar_pro
1104Srgrimes#define	arp_hln	ea_hdr.ar_hln
1114Srgrimes#define	arp_pln	ea_hdr.ar_pln
1124Srgrimes#define	arp_op	ea_hdr.ar_op
1134Srgrimes
1144Srgrimes/*
1154Srgrimes * Structure shared between the ethernet driver modules and
1164Srgrimes * the address resolution code.  For example, each ec_softc or il_softc
1174Srgrimes * begins with this structure.
1184Srgrimes */
1194Srgrimesstruct	arpcom {
1204Srgrimes	struct	 ifnet ac_if;			/* network-visible interface */
1214Srgrimes	u_int8_t ac_enaddr[ETHER_ADDR_LEN];	/* ethernet hardware address */
1224Srgrimes	char	 ac__pad[2];			/* be nice to m68k ports */
1234Srgrimes	LIST_HEAD(, ether_multi) ac_multiaddrs;	/* list of ether multicast addrs */
1244Srgrimes	int	 ac_multicnt;			/* length of ac_multiaddrs list */
1254Srgrimes};
1264Srgrimes
1274Srgrimesstruct llinfo_arp {
1284Srgrimes	LIST_ENTRY(llinfo_arp) la_list;
1294Srgrimes	struct	rtentry *la_rt;
1304Srgrimes	struct	mbuf *la_hold;		/* last packet until resolved/timeout */
1314Srgrimes	long	la_asked;		/* last time we QUERIED for this addr */
1324Srgrimes#define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */
1334Srgrimes};
1344Srgrimes
1354Srgrimesstruct sockaddr_inarp {
1364Srgrimes	u_int8_t  sin_len;
1374Srgrimes	u_int8_t  sin_family;
1384Srgrimes	u_int16_t sin_port;
1394Srgrimes	struct	  in_addr sin_addr;
1404Srgrimes	struct	  in_addr sin_srcaddr;
1414Srgrimes	u_int16_t sin_tos;
1424Srgrimes	u_int16_t sin_other;
1434Srgrimes#define SIN_PROXY 1
1444Srgrimes};
1454Srgrimes
1464Srgrimes/*
1474Srgrimes * IP and ethernet specific routing flags
1484Srgrimes */
1494Srgrimes#define	RTF_USETRAILERS	RTF_PROTO1	/* use trailers */
1504Srgrimes#define	RTF_ANNOUNCE	RTF_PROTO2	/* announce new arp entry */
1514Srgrimes
1524Srgrimes#ifdef	_KERNEL
1534Srgrimesu_int8_t etherbroadcastaddr[ETHER_ADDR_LEN];
1544Srgrimesu_int8_t ether_ipmulticast_min[ETHER_ADDR_LEN];
1554Srgrimesu_int8_t ether_ipmulticast_max[ETHER_ADDR_LEN];
1564Srgrimesstruct	ifqueue arpintrq;
1574Srgrimes
1584Srgrimesvoid	arpwhohas(struct arpcom *, struct in_addr *);
1594Srgrimesvoid	arpintr(void);
1604Srgrimesint	arpresolve(struct arpcom *,
1614Srgrimes	    struct rtentry *, struct mbuf *, struct sockaddr *, u_char *);
1624Srgrimesvoid	arp_ifinit(struct arpcom *, struct ifaddr *);
1634Srgrimesvoid	arp_rtrequest(int, struct rtentry *, struct sockaddr *);
1644Srgrimes
1654Srgrimesint	ether_addmulti(struct ifreq *, struct arpcom *);
1664Srgrimesint	ether_delmulti(struct ifreq *, struct arpcom *);
1674Srgrimes#endif /* _KERNEL */
1684Srgrimes
1694Srgrimes/*
1704Srgrimes * Ethernet multicast address structure.  There is one of these for each
1714Srgrimes * multicast address or range of multicast addresses that we are supposed
1724Srgrimes * to listen to on a particular interface.  They are kept in a linked list,
1734Srgrimes * rooted in the interface's arpcom structure.  (This really has nothing to
1744Srgrimes * do with ARP, or with the Internet address family, but this appears to be
1754Srgrimes * the minimally-disrupting place to put it.)
1764Srgrimes */
1774Srgrimesstruct ether_multi {
1784Srgrimes	u_int8_t enm_addrlo[ETHER_ADDR_LEN]; /* low  or only address of range */
1794Srgrimes	u_int8_t enm_addrhi[ETHER_ADDR_LEN]; /* high or only address of range */
1804Srgrimes	struct	 arpcom *enm_ac;	/* back pointer to arpcom */
1814Srgrimes	u_int	 enm_refcount;		/* no. claims to this addr/range */
1824Srgrimes	LIST_ENTRY(ether_multi) enm_list;
1834Srgrimes};
1844Srgrimes
1854Srgrimes/*
1864Srgrimes * Structure used by macros below to remember position when stepping through
1874Srgrimes * all of the ether_multi records.
1884Srgrimes */
1894Srgrimesstruct ether_multistep {
1904Srgrimes	struct ether_multi  *e_enm;
1914Srgrimes};
1924Srgrimes
1934Srgrimes/*
1944Srgrimes * Macro for looking up the ether_multi record for a given range of Ethernet
1954Srgrimes * multicast addresses connected to a given arpcom structure.  If no matching
1964Srgrimes * record is found, "enm" returns NULL.
1974Srgrimes */
1984Srgrimes#define ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm)			\
1994Srgrimes	/* u_int8_t addrlo[ETHER_ADDR_LEN]; */				\
2004Srgrimes	/* u_int8_t addrhi[ETHER_ADDR_LEN]; */				\
2014Srgrimes	/* struct arpcom *ac; */					\
2024Srgrimes	/* struct ether_multi *enm; */					\
20312473Sbde{									\
2042112Swollman	for ((enm) = (ac)->ac_multiaddrs.lh_first;			\
2052112Swollman	    (enm) != NULL &&						\
2062112Swollman	    (bcmp((enm)->enm_addrlo, (addrlo), ETHER_ADDR_LEN) != 0 ||	\
2072112Swollman	     bcmp((enm)->enm_addrhi, (addrhi), ETHER_ADDR_LEN) != 0);	\
2082112Swollman		(enm) = (enm)->enm_list.le_next);			\
2094Srgrimes}
2104Srgrimes
2114Srgrimes/*
2124Srgrimes * Macro to step through all of the ether_multi records, one at a time.
2134Srgrimes * The current position is remembered in "step", which the caller must
2144Srgrimes * provide.  ETHER_FIRST_MULTI(), below, must be called to initialize "step"
2154Srgrimes * and get the first record.  Both macros return a NULL "enm" when there
2164Srgrimes * are no remaining records.
2174Srgrimes */
2184Srgrimes#define ETHER_NEXT_MULTI(step, enm) \
2194Srgrimes	/* struct ether_multistep step; */  \
2204Srgrimes	/* struct ether_multi *enm; */  \
2214Srgrimes{ \
2224Srgrimes	if (((enm) = (step).e_enm) != NULL) \
2234Srgrimes		(step).e_enm = (enm)->enm_list.le_next; \
2244Srgrimes}
2254Srgrimes
2264Srgrimes#define ETHER_FIRST_MULTI(step, ac, enm) \
2274Srgrimes	/* struct ether_multistep step; */ \
2284Srgrimes	/* struct arpcom *ac; */ \
2294Srgrimes	/* struct ether_multi *enm; */ \
2304Srgrimes{ \
2314Srgrimes	(step).e_enm = (ac)->ac_multiaddrs.lh_first; \
2324Srgrimes	ETHER_NEXT_MULTI((step), (enm)); \
2334Srgrimes}
2344Srgrimes
2354Srgrimes#ifdef _KERNEL
2364Srgrimesvoid arp_rtrequest(int, struct rtentry *, struct sockaddr *);
2374Srgrimesint arpresolve(struct arpcom *, struct rtentry *, struct mbuf *,
2384Srgrimes		    struct sockaddr *, u_char *);
2394Srgrimesvoid arpintr(void);
2404Srgrimesint arpioctl(u_long, caddr_t);
2414Srgrimesvoid arp_ifinit(struct arpcom *, struct ifaddr *);
2424Srgrimesvoid revarpinput(struct mbuf *);
2434Srgrimesvoid in_revarpinput(struct mbuf *);
2444Srgrimesvoid revarprequest(struct ifnet *);
2454Srgrimesint revarpwhoarewe(struct ifnet *, struct in_addr *, struct in_addr *);
2464Srgrimesint revarpwhoami(struct in_addr *, struct ifnet *);
2474Srgrimesint db_show_arptab(void);
2484Srgrimes#endif
2494Srgrimes
2504Srgrimes/*
2514Srgrimes * Prototype ethers(3) functions.
2524Srgrimes */
2534Srgrimes#ifndef _KERNEL
2544Srgrimes#include <sys/cdefs.h>
2554Srgrimes__BEGIN_DECLS
2564Srgrimeschar *	ether_ntoa(struct ether_addr *);
2574Srgrimesstruct ether_addr *
258798Swollman	ether_aton(char *);
2594Srgrimesint	ether_ntohost(char *, struct ether_addr *);
260798Swollmanint	ether_hostton(char *, struct ether_addr *);
2614Srgrimesint	ether_line(char *, struct ether_addr *, char *);
2628876Srgrimes__END_DECLS
263#endif
264