xref: /titanic_52/usr/src/stand/lib/inet/mac.c (revision fa9e4066f08beec538e775443c5be79dd423fcab)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*fa9e4066Sahrens  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <socket_impl.h>
317c478bd9Sstevel@tonic-gate #include <socket_inet.h>
327c478bd9Sstevel@tonic-gate #include <sys/param.h>
337c478bd9Sstevel@tonic-gate #include <sys/time.h>
347c478bd9Sstevel@tonic-gate #include <sys/socket.h>
357c478bd9Sstevel@tonic-gate #include <net/if.h>
367c478bd9Sstevel@tonic-gate #include <net/if_arp.h>
377c478bd9Sstevel@tonic-gate #include <net/if_types.h>
387c478bd9Sstevel@tonic-gate #include <netinet/in_systm.h>
397c478bd9Sstevel@tonic-gate #include <netinet/in.h>
407c478bd9Sstevel@tonic-gate #include <netinet/if_ether.h>
417c478bd9Sstevel@tonic-gate #include <sys/promif.h>
427c478bd9Sstevel@tonic-gate #include <sys/prom_plat.h>
437c478bd9Sstevel@tonic-gate #include <sys/salib.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include "mac.h"
467c478bd9Sstevel@tonic-gate #include "mac_impl.h"
477c478bd9Sstevel@tonic-gate #include "atm_inet.h"
487c478bd9Sstevel@tonic-gate #include "ethernet_inet.h"
497c478bd9Sstevel@tonic-gate #include "fddi_inet.h"
507c478bd9Sstevel@tonic-gate #include "token_inet.h"
517c478bd9Sstevel@tonic-gate #include "ibd_inet.h"
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * MAC layer interface
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate boolean_t		initialized;	/* Boolean state */
577c478bd9Sstevel@tonic-gate struct mac_type		mac_state;
587c478bd9Sstevel@tonic-gate int			arp_index;	/* current arp table index */
597c478bd9Sstevel@tonic-gate static struct arptable	atable[ARP_TABLE_SIZE];
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #if	!defined(__i386)
627c478bd9Sstevel@tonic-gate struct ofw_net_types {
637c478bd9Sstevel@tonic-gate 	char	*n_name;	/* OFW network media name */
647c478bd9Sstevel@tonic-gate 	int	n_type;		/* IFT */
657c478bd9Sstevel@tonic-gate } ofw_types[] = {
667c478bd9Sstevel@tonic-gate 	{ "atm",	IFT_ATM	},
677c478bd9Sstevel@tonic-gate 	{ "ethernet",	IFT_ETHER },
687c478bd9Sstevel@tonic-gate 	{ "fddi",	IFT_FDDI },
697c478bd9Sstevel@tonic-gate 	{ "token-ring",	IFT_ISO88025 },
707c478bd9Sstevel@tonic-gate 	{ "ipib",	IFT_IB }
717c478bd9Sstevel@tonic-gate };
727c478bd9Sstevel@tonic-gate #endif	/* !__i386 */
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  * given the mac type, initialize the mac interface state.
767c478bd9Sstevel@tonic-gate  */
777c478bd9Sstevel@tonic-gate void
787c478bd9Sstevel@tonic-gate mac_init(char *bootdevicename)
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate 	int		type = 0;
817c478bd9Sstevel@tonic-gate #if !defined(__i386)
827c478bd9Sstevel@tonic-gate 	static char	*mtu_name = "max-frame-size";
837c478bd9Sstevel@tonic-gate 	static char	*chosen_net = "chosen-network-type";
847c478bd9Sstevel@tonic-gate 	static char	*supported_net = "supported-network-types";
857c478bd9Sstevel@tonic-gate 	static char	*netiftype = "network-interface-type";
86*fa9e4066Sahrens 	pnode_t		node;
877c478bd9Sstevel@tonic-gate 	char		*wp, *media_type;
887c478bd9Sstevel@tonic-gate 	int		len = 0, i;
897c478bd9Sstevel@tonic-gate #endif	/* !__i386 */
907c478bd9Sstevel@tonic-gate 	char		tmpbuf[MAXNAMELEN];
917c478bd9Sstevel@tonic-gate 	char		devname[MAXNAMELEN];
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	if (initialized)
947c478bd9Sstevel@tonic-gate 		return;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	mac_state.mac_in_timeout = MAC_IN_TIMEOUT;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate #ifdef	DEBUG
997c478bd9Sstevel@tonic-gate 	printf("mac_init: device path: %s\n", bootdevicename);
1007c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	if ((mac_state.mac_dev = prom_open(bootdevicename)) == 0) {
1037c478bd9Sstevel@tonic-gate 		(void) snprintf(tmpbuf, sizeof (tmpbuf),
1047c478bd9Sstevel@tonic-gate 		    "Cannot prom_open network device %s.", bootdevicename);
1057c478bd9Sstevel@tonic-gate 		prom_panic(tmpbuf);
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate #if !defined(__i386)
1097c478bd9Sstevel@tonic-gate 	(void) prom_devname_from_pathname(bootdevicename, devname);
1107c478bd9Sstevel@tonic-gate #else
1117c478bd9Sstevel@tonic-gate 	(void) strcpy(devname, bootdevicename);
1127c478bd9Sstevel@tonic-gate #endif	/* !__i386 */
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1157c478bd9Sstevel@tonic-gate 	printf("mac_init: Network device name: %s\n", devname);
1167c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate #if !defined(__i386)
1197c478bd9Sstevel@tonic-gate 	/*
1207c478bd9Sstevel@tonic-gate 	 * Ask the prom for our MTU and media type. "chosen-network-type"
1217c478bd9Sstevel@tonic-gate 	 * is of the form of "<network type>,<speed (Mbps)>,<connector type>,
1227c478bd9Sstevel@tonic-gate 	 * <duplex mode>: e.g.: "ethernet,100,rj45,full"
1237c478bd9Sstevel@tonic-gate 	 */
1247c478bd9Sstevel@tonic-gate 	node = prom_finddevice(devname);
1257c478bd9Sstevel@tonic-gate 	if (node != OBP_NONODE && node != OBP_BADNODE) {
1267c478bd9Sstevel@tonic-gate 		if (prom_getproplen(node, mtu_name) == sizeof (ihandle_t)) {
1277c478bd9Sstevel@tonic-gate 			(void) prom_getprop(node, mtu_name,
1287c478bd9Sstevel@tonic-gate 			    (caddr_t)&mac_state.mac_mtu);
1297c478bd9Sstevel@tonic-gate 		}
1307c478bd9Sstevel@tonic-gate 		bzero(tmpbuf, sizeof (tmpbuf));
1317c478bd9Sstevel@tonic-gate 		/*
1327c478bd9Sstevel@tonic-gate 		 * The following order of looking for properties is
1337c478bd9Sstevel@tonic-gate 		 * from FWARC 2002/345.
1347c478bd9Sstevel@tonic-gate 		 */
1357c478bd9Sstevel@tonic-gate 		if ((len = prom_getproplen(node, netiftype)) > 0 &&
1367c478bd9Sstevel@tonic-gate 		    len < sizeof (tmpbuf)) {
1377c478bd9Sstevel@tonic-gate 			(void) prom_getprop(node, netiftype, tmpbuf);
1387c478bd9Sstevel@tonic-gate 		} else if ((len = prom_getproplen(node, chosen_net)) > 0 &&
1397c478bd9Sstevel@tonic-gate 		    len < sizeof (tmpbuf)) {
1407c478bd9Sstevel@tonic-gate 			(void) prom_getprop(node, chosen_net, tmpbuf);
1417c478bd9Sstevel@tonic-gate 		} else if ((len = prom_getproplen(node, supported_net)) > 0 &&
1427c478bd9Sstevel@tonic-gate 		    len < sizeof (tmpbuf)) {
1437c478bd9Sstevel@tonic-gate 			(void) prom_getprop(node, supported_net, tmpbuf);
1447c478bd9Sstevel@tonic-gate 		}
1457c478bd9Sstevel@tonic-gate 		media_type = NULL;
1467c478bd9Sstevel@tonic-gate 		if (len > 0) {
1477c478bd9Sstevel@tonic-gate 			if ((wp = strstr(tmpbuf, ",")) != NULL)
1487c478bd9Sstevel@tonic-gate 				*wp = '\0';
1497c478bd9Sstevel@tonic-gate 			media_type = tmpbuf;
1507c478bd9Sstevel@tonic-gate 		}
1517c478bd9Sstevel@tonic-gate 		if (media_type != NULL) {
1527c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1537c478bd9Sstevel@tonic-gate 			printf("mac_init: Media type: %s\n", media_type);
1547c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
1557c478bd9Sstevel@tonic-gate 			for (i = 0; i < sizeof (ofw_types) /
1567c478bd9Sstevel@tonic-gate 			    sizeof (struct ofw_net_types); i++) {
1577c478bd9Sstevel@tonic-gate 				if (strcmp(ofw_types[i].n_name,
1587c478bd9Sstevel@tonic-gate 				    media_type) == 0) {
1597c478bd9Sstevel@tonic-gate 					type = ofw_types[i].n_type;
1607c478bd9Sstevel@tonic-gate 					break;
1617c478bd9Sstevel@tonic-gate 				}
1627c478bd9Sstevel@tonic-gate 			}
1637c478bd9Sstevel@tonic-gate 		}
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate #else
1667c478bd9Sstevel@tonic-gate 	type = IFT_ETHER;	/* default to ethernet */
1677c478bd9Sstevel@tonic-gate #endif	/* !__i386 */
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	switch (type) {
1707c478bd9Sstevel@tonic-gate 	case IFT_ATM:
1717c478bd9Sstevel@tonic-gate 		/*
1727c478bd9Sstevel@tonic-gate 		 * ATM is currently treated mostly like ethernet,
1737c478bd9Sstevel@tonic-gate 		 * with the exception that the MTU is most likely
1747c478bd9Sstevel@tonic-gate 		 * different.
1757c478bd9Sstevel@tonic-gate 		 */
1767c478bd9Sstevel@tonic-gate 		mac_state.mac_type = IFT_ATM;
1777c478bd9Sstevel@tonic-gate 		mac_state.mac_arp_timeout = ATM_ARP_TIMEOUT;
1787c478bd9Sstevel@tonic-gate 		mac_state.mac_in_timeout = ATM_IN_TIMEOUT;
1797c478bd9Sstevel@tonic-gate 		if (mac_state.mac_mtu == 0)
1807c478bd9Sstevel@tonic-gate 			mac_state.mac_mtu = ATMSIZE;
1817c478bd9Sstevel@tonic-gate 		mac_state.mac_addr_len = sizeof (ether_addr_t);
1827c478bd9Sstevel@tonic-gate 		mac_state.mac_addr_buf = bkmem_alloc(mac_state.mac_addr_len);
1837c478bd9Sstevel@tonic-gate 		if (mac_state.mac_addr_buf == NULL)
1847c478bd9Sstevel@tonic-gate 			prom_panic("mac_init: Cannot allocate memory.");
1857c478bd9Sstevel@tonic-gate 		if (prom_getmacaddr(mac_state.mac_dev,
1867c478bd9Sstevel@tonic-gate 		    (caddr_t)mac_state.mac_addr_buf) != 0)
1877c478bd9Sstevel@tonic-gate 			prom_panic("mac_init: Cannot obtain MAC address.");
1887c478bd9Sstevel@tonic-gate 		mac_state.mac_arp = ether_arp;
1897c478bd9Sstevel@tonic-gate 		mac_state.mac_rarp = ether_revarp;
1907c478bd9Sstevel@tonic-gate 		mac_state.mac_header_len = ether_header_len;
1917c478bd9Sstevel@tonic-gate 		mac_state.mac_input = ether_input;
1927c478bd9Sstevel@tonic-gate 		mac_state.mac_output = ether_output;
1937c478bd9Sstevel@tonic-gate 		break;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	case IFT_FDDI:
1967c478bd9Sstevel@tonic-gate 		/*
1977c478bd9Sstevel@tonic-gate 		 * FDDI is currently treated mostly like ethernet,
1987c478bd9Sstevel@tonic-gate 		 * with the exception that the MTU is most likely
1997c478bd9Sstevel@tonic-gate 		 * different.
2007c478bd9Sstevel@tonic-gate 		 */
2017c478bd9Sstevel@tonic-gate 		mac_state.mac_type = IFT_FDDI;
2027c478bd9Sstevel@tonic-gate 		mac_state.mac_arp_timeout = FDDI_ARP_TIMEOUT;
2037c478bd9Sstevel@tonic-gate 		mac_state.mac_in_timeout = FDDI_IN_TIMEOUT;
2047c478bd9Sstevel@tonic-gate 		if (mac_state.mac_mtu == 0)
2057c478bd9Sstevel@tonic-gate 			mac_state.mac_mtu = FDDISIZE;
2067c478bd9Sstevel@tonic-gate 		mac_state.mac_addr_len = sizeof (ether_addr_t);
2077c478bd9Sstevel@tonic-gate 		mac_state.mac_addr_buf = bkmem_alloc(mac_state.mac_addr_len);
2087c478bd9Sstevel@tonic-gate 		if (mac_state.mac_addr_buf == NULL)
2097c478bd9Sstevel@tonic-gate 			prom_panic("mac_init: Cannot allocate memory.");
2107c478bd9Sstevel@tonic-gate 		if (prom_getmacaddr(mac_state.mac_dev,
2117c478bd9Sstevel@tonic-gate 		    (caddr_t)mac_state.mac_addr_buf) != 0)
2127c478bd9Sstevel@tonic-gate 			prom_panic("mac_init: Cannot obtain MAC address.");
2137c478bd9Sstevel@tonic-gate 		mac_state.mac_arp = ether_arp;
2147c478bd9Sstevel@tonic-gate 		mac_state.mac_rarp = ether_revarp;
2157c478bd9Sstevel@tonic-gate 		mac_state.mac_header_len = ether_header_len;
2167c478bd9Sstevel@tonic-gate 		mac_state.mac_input = ether_input;
2177c478bd9Sstevel@tonic-gate 		mac_state.mac_output = ether_output;
2187c478bd9Sstevel@tonic-gate 		break;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	case IFT_ISO88025:
2217c478bd9Sstevel@tonic-gate 		/*
2227c478bd9Sstevel@tonic-gate 		 * Token ring is currently treated mostly like ethernet,
2237c478bd9Sstevel@tonic-gate 		 * with the exception that the MTU is most likely different.
2247c478bd9Sstevel@tonic-gate 		 */
2257c478bd9Sstevel@tonic-gate 		mac_state.mac_type = IFT_ISO88025;
2267c478bd9Sstevel@tonic-gate 		mac_state.mac_arp_timeout = TOKEN_ARP_TIMEOUT;
2277c478bd9Sstevel@tonic-gate 		mac_state.mac_in_timeout = TOKEN_IN_TIMEOUT;
2287c478bd9Sstevel@tonic-gate 		if (mac_state.mac_mtu == 0)
2297c478bd9Sstevel@tonic-gate 			mac_state.mac_mtu = TOKENSIZE;
2307c478bd9Sstevel@tonic-gate 		mac_state.mac_addr_len = sizeof (ether_addr_t);
2317c478bd9Sstevel@tonic-gate 		mac_state.mac_addr_buf = bkmem_alloc(mac_state.mac_addr_len);
2327c478bd9Sstevel@tonic-gate 		if (mac_state.mac_addr_buf == NULL)
2337c478bd9Sstevel@tonic-gate 			prom_panic("mac_init: Cannot allocate memory.");
2347c478bd9Sstevel@tonic-gate 		if (prom_getmacaddr(mac_state.mac_dev,
2357c478bd9Sstevel@tonic-gate 		    (caddr_t)mac_state.mac_addr_buf) != 0)
2367c478bd9Sstevel@tonic-gate 			prom_panic("mac_init: Cannot obtain MAC address.");
2377c478bd9Sstevel@tonic-gate 		mac_state.mac_arp = ether_arp;
2387c478bd9Sstevel@tonic-gate 		mac_state.mac_rarp = ether_revarp;
2397c478bd9Sstevel@tonic-gate 		mac_state.mac_header_len = ether_header_len;
2407c478bd9Sstevel@tonic-gate 		mac_state.mac_input = ether_input;
2417c478bd9Sstevel@tonic-gate 		mac_state.mac_output = ether_output;
2427c478bd9Sstevel@tonic-gate 		break;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	case IFT_IB:
2457c478bd9Sstevel@tonic-gate 		mac_state.mac_type = IFT_IB;
2467c478bd9Sstevel@tonic-gate 		ibd_init();
2477c478bd9Sstevel@tonic-gate 		break;
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	case IFT_ETHER:
2507c478bd9Sstevel@tonic-gate 		/* FALLTHRU - default to ethernet */
2517c478bd9Sstevel@tonic-gate 	default:
2527c478bd9Sstevel@tonic-gate 		mac_state.mac_type = IFT_ETHER;
2537c478bd9Sstevel@tonic-gate 		mac_state.mac_mtu = ETHERSIZE;
2547c478bd9Sstevel@tonic-gate 		mac_state.mac_arp_timeout = ETHER_ARP_TIMEOUT;
2557c478bd9Sstevel@tonic-gate 		mac_state.mac_in_timeout = ETHER_IN_TIMEOUT;
2567c478bd9Sstevel@tonic-gate 		if (mac_state.mac_mtu == 0)
2577c478bd9Sstevel@tonic-gate 			mac_state.mac_mtu = ETHERSIZE;
2587c478bd9Sstevel@tonic-gate 		mac_state.mac_addr_len = sizeof (ether_addr_t);
2597c478bd9Sstevel@tonic-gate 		mac_state.mac_addr_buf = bkmem_alloc(mac_state.mac_addr_len);
2607c478bd9Sstevel@tonic-gate 		if (mac_state.mac_addr_buf == NULL)
2617c478bd9Sstevel@tonic-gate 			prom_panic("mac_init: Cannot allocate memory.");
2627c478bd9Sstevel@tonic-gate 		if (prom_getmacaddr(mac_state.mac_dev,
2637c478bd9Sstevel@tonic-gate 		    (caddr_t)mac_state.mac_addr_buf) != 0)
2647c478bd9Sstevel@tonic-gate 			prom_panic("mac_init: Cannot obtain MAC address.");
2657c478bd9Sstevel@tonic-gate 		mac_state.mac_arp = ether_arp;
2667c478bd9Sstevel@tonic-gate 		mac_state.mac_rarp = ether_revarp;
2677c478bd9Sstevel@tonic-gate 		mac_state.mac_header_len = ether_header_len;
2687c478bd9Sstevel@tonic-gate 		mac_state.mac_input = ether_input;
2697c478bd9Sstevel@tonic-gate 		mac_state.mac_output = ether_output;
2707c478bd9Sstevel@tonic-gate 		break;
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	mac_state.mac_buf = bkmem_alloc(mac_state.mac_mtu);
2747c478bd9Sstevel@tonic-gate 	if (mac_state.mac_buf == NULL)
2757c478bd9Sstevel@tonic-gate 		prom_panic("mac_init: Cannot allocate netbuf memory.");
2767c478bd9Sstevel@tonic-gate 	else
2777c478bd9Sstevel@tonic-gate 		initialized = B_TRUE;
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate void
2817c478bd9Sstevel@tonic-gate mac_fini()
2827c478bd9Sstevel@tonic-gate {
2837c478bd9Sstevel@tonic-gate 	if (mac_state.mac_addr_buf != NULL) {
2847c478bd9Sstevel@tonic-gate 		bkmem_free((caddr_t)mac_state.mac_addr_buf,
2857c478bd9Sstevel@tonic-gate 		    mac_state.mac_addr_len);
2867c478bd9Sstevel@tonic-gate 		mac_state.mac_addr_buf = NULL;
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate 	if (mac_state.mac_buf != NULL) {
2897c478bd9Sstevel@tonic-gate 		bkmem_free(mac_state.mac_buf, mac_state.mac_mtu);
2907c478bd9Sstevel@tonic-gate 		mac_state.mac_buf = NULL;
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate 	(void) prom_close(mac_state.mac_dev);
2937c478bd9Sstevel@tonic-gate 	initialized = B_FALSE;
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate /* MAC layer specific socket initialization */
2977c478bd9Sstevel@tonic-gate void
2987c478bd9Sstevel@tonic-gate mac_socket_init(struct inetboot_socket *isp)
2997c478bd9Sstevel@tonic-gate {
3007c478bd9Sstevel@tonic-gate 	isp->input[MEDIA_LVL] = mac_state.mac_input;
3017c478bd9Sstevel@tonic-gate 	isp->output[MEDIA_LVL] = mac_state.mac_output;
3027c478bd9Sstevel@tonic-gate 	isp->close[MEDIA_LVL] = NULL;
3037c478bd9Sstevel@tonic-gate 	isp->headerlen[MEDIA_LVL] = mac_state.mac_header_len;
3047c478bd9Sstevel@tonic-gate 	isp->in_timeout = mac_state.mac_in_timeout;
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate /*
3087c478bd9Sstevel@tonic-gate  * Add an entry to the ARP table. All values in table are network order.
3097c478bd9Sstevel@tonic-gate  * No checking is done to determine whether there's duplicates.
3107c478bd9Sstevel@tonic-gate  */
3117c478bd9Sstevel@tonic-gate void
3127c478bd9Sstevel@tonic-gate mac_set_arp(struct in_addr *ip, void *hp, int hl)
3137c478bd9Sstevel@tonic-gate {
3147c478bd9Sstevel@tonic-gate 	atable[arp_index].ia.s_addr = ip->s_addr;
3157c478bd9Sstevel@tonic-gate 	bcopy(hp, (char *)atable[arp_index].ha, hl);
3167c478bd9Sstevel@tonic-gate 	atable[arp_index].hl = hl;
3177c478bd9Sstevel@tonic-gate 	arp_index++;
3187c478bd9Sstevel@tonic-gate 	if (arp_index >= ARP_TABLE_SIZE)
3197c478bd9Sstevel@tonic-gate 		arp_index = 0;
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate /*
3237c478bd9Sstevel@tonic-gate  * Retrieve an entry from the ARP table using network-order IP address as
3247c478bd9Sstevel@tonic-gate  * search criteria. HW address buffer is filled in up to hl in len. (make
3257c478bd9Sstevel@tonic-gate  * sure the buffer is big enough given the mac type)
3267c478bd9Sstevel@tonic-gate  *
3277c478bd9Sstevel@tonic-gate  * Returns TRUE if successful, FALSE otherwise. Will wait timeout milliseconds
3287c478bd9Sstevel@tonic-gate  * for a response.
3297c478bd9Sstevel@tonic-gate  */
3307c478bd9Sstevel@tonic-gate int
3317c478bd9Sstevel@tonic-gate mac_get_arp(struct in_addr *ip, void *hp, int hl, uint32_t timeout)
3327c478bd9Sstevel@tonic-gate {
3337c478bd9Sstevel@tonic-gate 	int i, result;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	for (i = 0; i < ARP_TABLE_SIZE; i++) {
3367c478bd9Sstevel@tonic-gate 		if (ip->s_addr == atable[i].ia.s_addr) {
3377c478bd9Sstevel@tonic-gate 			bcopy((char *)atable[i].ha, hp, hl);
3387c478bd9Sstevel@tonic-gate 			return (TRUE);
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	/* Not found. ARP for it. */
3437c478bd9Sstevel@tonic-gate 	bzero(hp, hl);
3447c478bd9Sstevel@tonic-gate 	result = mac_state.mac_arp(ip, hp, timeout);
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	if (result) {
3477c478bd9Sstevel@tonic-gate 		/* Cool - add it to the arp table */
3487c478bd9Sstevel@tonic-gate 		mac_set_arp(ip, hp, hl);
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 	return (result);
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate int
3547c478bd9Sstevel@tonic-gate mac_get_mtu(void)
3557c478bd9Sstevel@tonic-gate {
3567c478bd9Sstevel@tonic-gate 	if (!initialized)
3577c478bd9Sstevel@tonic-gate 		return (-1);
3587c478bd9Sstevel@tonic-gate 	else
3597c478bd9Sstevel@tonic-gate 		return (mac_state.mac_mtu);
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate int
3637c478bd9Sstevel@tonic-gate mac_get_dev(void)
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate 	if (!initialized)
3667c478bd9Sstevel@tonic-gate 		return (-1);
3677c478bd9Sstevel@tonic-gate 	else
3687c478bd9Sstevel@tonic-gate 		return (mac_state.mac_dev);
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate uint8_t *
3727c478bd9Sstevel@tonic-gate mac_get_addr_buf(void)
3737c478bd9Sstevel@tonic-gate {
3747c478bd9Sstevel@tonic-gate 	if (!initialized)
3757c478bd9Sstevel@tonic-gate 		return (NULL);
3767c478bd9Sstevel@tonic-gate 	else
3777c478bd9Sstevel@tonic-gate 		return (mac_state.mac_addr_buf);
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate int
3817c478bd9Sstevel@tonic-gate mac_get_addr_len(void)
3827c478bd9Sstevel@tonic-gate {
3837c478bd9Sstevel@tonic-gate 	if (!initialized)
3847c478bd9Sstevel@tonic-gate 		return (-1);
3857c478bd9Sstevel@tonic-gate 	else
3867c478bd9Sstevel@tonic-gate 		return (mac_state.mac_addr_len);
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate int
3907c478bd9Sstevel@tonic-gate mac_get_type(void)
3917c478bd9Sstevel@tonic-gate {
3927c478bd9Sstevel@tonic-gate 	if (!initialized)
3937c478bd9Sstevel@tonic-gate 		return (-1);
3947c478bd9Sstevel@tonic-gate 	else
3957c478bd9Sstevel@tonic-gate 		return (mac_state.mac_type);
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate int
3997c478bd9Sstevel@tonic-gate mac_get_arp_timeout(void)
4007c478bd9Sstevel@tonic-gate {
4017c478bd9Sstevel@tonic-gate 	if (!initialized)
4027c478bd9Sstevel@tonic-gate 		return (-1);
4037c478bd9Sstevel@tonic-gate 	else
4047c478bd9Sstevel@tonic-gate 		return (mac_state.mac_arp_timeout);
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate int
4087c478bd9Sstevel@tonic-gate mac_get_hdr_len(void)
4097c478bd9Sstevel@tonic-gate {
4107c478bd9Sstevel@tonic-gate 	if (!initialized)
4117c478bd9Sstevel@tonic-gate 		return (-1);
4127c478bd9Sstevel@tonic-gate 	else
4137c478bd9Sstevel@tonic-gate 		return (mac_state.mac_header_len(NULL));
4147c478bd9Sstevel@tonic-gate }
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate int
4177c478bd9Sstevel@tonic-gate mac_call_arp(struct in_addr *addr, void *buf, uint32_t timeout)
4187c478bd9Sstevel@tonic-gate {
4197c478bd9Sstevel@tonic-gate 	return (mac_state.mac_arp(addr, buf, timeout));
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate void
4237c478bd9Sstevel@tonic-gate mac_call_rarp(void)
4247c478bd9Sstevel@tonic-gate {
4257c478bd9Sstevel@tonic-gate 	mac_state.mac_rarp();
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate /*
4297c478bd9Sstevel@tonic-gate  * Map a IFT_ type to an RFC 1700 arp hwtype.
4307c478bd9Sstevel@tonic-gate  */
4317c478bd9Sstevel@tonic-gate uint8_t
4327c478bd9Sstevel@tonic-gate mac_arp_type(uint8_t ift_type)
4337c478bd9Sstevel@tonic-gate {
4347c478bd9Sstevel@tonic-gate 	uint8_t arptype;
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	switch (ift_type) {
4377c478bd9Sstevel@tonic-gate 	case IFT_ISO88025:
4387c478bd9Sstevel@tonic-gate 		arptype = 4;	/* token ring */
4397c478bd9Sstevel@tonic-gate 		break;
4407c478bd9Sstevel@tonic-gate 	case IFT_ATM:
4417c478bd9Sstevel@tonic-gate 		arptype = 16;	/* ATM */
4427c478bd9Sstevel@tonic-gate 		break;
4437c478bd9Sstevel@tonic-gate 	case IFT_FDDI:
4447c478bd9Sstevel@tonic-gate 		arptype = 18;	/* Fiber Channel */
4457c478bd9Sstevel@tonic-gate 		break;
4467c478bd9Sstevel@tonic-gate 	case IFT_IB:
4477c478bd9Sstevel@tonic-gate 		arptype = 32;	/* Infiniband */
4487c478bd9Sstevel@tonic-gate 		break;
4497c478bd9Sstevel@tonic-gate 	case IFT_ETHER:
4507c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
4517c478bd9Sstevel@tonic-gate 	default:
4527c478bd9Sstevel@tonic-gate 		arptype = 1;	/* default to ethernet */
4537c478bd9Sstevel@tonic-gate 		break;
4547c478bd9Sstevel@tonic-gate 	}
4557c478bd9Sstevel@tonic-gate 	return (arptype);
4567c478bd9Sstevel@tonic-gate }
457