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 /*
237c478bd9Sstevel@tonic-gate  * PPPoE common utilities and data.
247c478bd9Sstevel@tonic-gate  *
25*32529ec1Srshoaib  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
267c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <unistd.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate #include <netdb.h>
367c478bd9Sstevel@tonic-gate #include <assert.h>
377c478bd9Sstevel@tonic-gate #include <stropts.h>
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <inet/common.h>
407c478bd9Sstevel@tonic-gate #include <netinet/in.h>
417c478bd9Sstevel@tonic-gate #include <net/sppptun.h>
427c478bd9Sstevel@tonic-gate #include <net/pppoe.h>
437c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include "common.h"
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /* Not all functions are used by all applications.  Let lint know this. */
487c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /* Common I/O buffers */
517c478bd9Sstevel@tonic-gate uint32_t pkt_input[PKT_INPUT_LEN / sizeof (uint32_t)];
527c478bd9Sstevel@tonic-gate uint32_t pkt_octl[PKT_OCTL_LEN / sizeof (uint32_t)];
537c478bd9Sstevel@tonic-gate uint32_t pkt_output[PKT_OUTPUT_LEN / sizeof (uint32_t)];
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate const char tunnam[] = "/dev/" PPP_TUN_NAME;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate const ether_addr_t ether_bcast = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * Wrapper for standard strerror() function -- the standard allows
617c478bd9Sstevel@tonic-gate  * that routine to return NULL, and that's inconvenient to handle.
627c478bd9Sstevel@tonic-gate  * This function never returns NULL.
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate const char *
mystrerror(int err)657c478bd9Sstevel@tonic-gate mystrerror(int err)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	const char *estr;
687c478bd9Sstevel@tonic-gate 	static char ebuf[64];
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	if ((estr = strerror(err)) != NULL)
717c478bd9Sstevel@tonic-gate 		return (estr);
727c478bd9Sstevel@tonic-gate 	(void) snprintf(ebuf, sizeof (ebuf), "Error:%d", err);
737c478bd9Sstevel@tonic-gate 	return (ebuf);
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate  * Wrapper for standard perror() function -- the standard definition
787c478bd9Sstevel@tonic-gate  * of perror doesn't include the program name in the output and is
797c478bd9Sstevel@tonic-gate  * thus inconvenient to use.
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate void
myperror(const char * emsg)827c478bd9Sstevel@tonic-gate myperror(const char *emsg)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s\n", myname, emsg,
857c478bd9Sstevel@tonic-gate 	    mystrerror(errno));
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate  * Wrapper for standard getmsg() function.  Completely discards any
907c478bd9Sstevel@tonic-gate  * fragmented messages because we don't expect ever to see these from
917c478bd9Sstevel@tonic-gate  * a properly functioning tunnel driver.  Returns flags
927c478bd9Sstevel@tonic-gate  * (MORECTL|MOREDATA) as seen by interface.
937c478bd9Sstevel@tonic-gate  */
947c478bd9Sstevel@tonic-gate int
mygetmsg(int fd,struct strbuf * ctrl,struct strbuf * data,int * flags)957c478bd9Sstevel@tonic-gate mygetmsg(int fd, struct strbuf *ctrl, struct strbuf *data, int *flags)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	int retv;
987c478bd9Sstevel@tonic-gate 	int hadflags;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	hadflags = getmsg(fd, ctrl, data, flags);
1017c478bd9Sstevel@tonic-gate 	if (hadflags <= 0 || !(hadflags & (MORECTL | MOREDATA)))
1027c478bd9Sstevel@tonic-gate 		return (hadflags);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	do {
1057c478bd9Sstevel@tonic-gate 		if (flags != NULL)
1067c478bd9Sstevel@tonic-gate 			*flags = 0;
1077c478bd9Sstevel@tonic-gate 		retv = getmsg(fd, ctrl, data, flags);
1087c478bd9Sstevel@tonic-gate 	} while (retv > 0 || (retv < 0 && errno == EINTR));
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	/*
1117c478bd9Sstevel@tonic-gate 	 * What remains at this point is the tail end of the
1127c478bd9Sstevel@tonic-gate 	 * truncated message.  Toss it.
1137c478bd9Sstevel@tonic-gate 	 */
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	return (retv < 0 ? retv : hadflags);
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate /*
1197c478bd9Sstevel@tonic-gate  * Common wrapper function for STREAMS I_STR ioctl.  Returns -1 on
1207c478bd9Sstevel@tonic-gate  * failure, 0 for success.
1217c478bd9Sstevel@tonic-gate  */
1227c478bd9Sstevel@tonic-gate int
strioctl(int fd,int cmd,void * ptr,int ilen,int olen)1237c478bd9Sstevel@tonic-gate strioctl(int fd, int cmd, void *ptr, int ilen, int olen)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	struct strioctl	str;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	str.ic_cmd = cmd;
1287c478bd9Sstevel@tonic-gate 	str.ic_timout = 0;	/* Default timeout; 15 seconds */
1297c478bd9Sstevel@tonic-gate 	str.ic_len = ilen;
1307c478bd9Sstevel@tonic-gate 	str.ic_dp = ptr;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	if (ioctl(fd, I_STR, &str) == -1) {
1337c478bd9Sstevel@tonic-gate 		return (-1);
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 	if (str.ic_len != olen) {
1367c478bd9Sstevel@tonic-gate 		errno = EINVAL;
1377c478bd9Sstevel@tonic-gate 		return (-1);
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 	return (0);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate  * Format a PPPoE header in the user's buffer.  The returned pointer
1447c478bd9Sstevel@tonic-gate  * is either identical to the first argument, or is NULL if it's not
1457c478bd9Sstevel@tonic-gate  * usable.  On entry, dptr should point to the first byte after the
1467c478bd9Sstevel@tonic-gate  * Ethertype field, codeval should be one of the POECODE_* values, and
1477c478bd9Sstevel@tonic-gate  * sessionid should be the assigned session ID number or one of the
1487c478bd9Sstevel@tonic-gate  * special POESESS_* values.
1497c478bd9Sstevel@tonic-gate  */
1507c478bd9Sstevel@tonic-gate poep_t *
poe_mkheader(void * dptr,uint8_t codeval,int sessionid)1517c478bd9Sstevel@tonic-gate poe_mkheader(void *dptr, uint8_t codeval, int sessionid)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate 	poep_t *poep;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	/* Discard obvious junk. */
1567c478bd9Sstevel@tonic-gate 	assert(dptr != NULL && IS_P2ALIGNED(dptr, sizeof (poep_t *)));
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	/* Initialize the header */
1597c478bd9Sstevel@tonic-gate 	poep = (poep_t *)dptr;
1607c478bd9Sstevel@tonic-gate 	poep->poep_version_type = POE_VERSION;
1617c478bd9Sstevel@tonic-gate 	poep->poep_code = codeval;
1627c478bd9Sstevel@tonic-gate 	poep->poep_session_id = htons(sessionid);
1637c478bd9Sstevel@tonic-gate 	poep->poep_length = htons(0);
1647c478bd9Sstevel@tonic-gate 	return (poep);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate  * Validate that a given tag is intact.  This is intended to be used
1697c478bd9Sstevel@tonic-gate  * in tag-parsing loops before attempting to access the tag data.
1707c478bd9Sstevel@tonic-gate  */
1717c478bd9Sstevel@tonic-gate boolean_t
poe_tagcheck(const poep_t * poep,int length,const uint8_t * tptr)1727c478bd9Sstevel@tonic-gate poe_tagcheck(const poep_t *poep, int length, const uint8_t *tptr)
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate 	int plen;
1757c478bd9Sstevel@tonic-gate 	const uint8_t *tstart, *tend;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	if (poep == NULL || !IS_P2ALIGNED(poep, sizeof (uint16_t)) ||
1787c478bd9Sstevel@tonic-gate 	    tptr == NULL || length < sizeof (*poep))
1797c478bd9Sstevel@tonic-gate 		return (B_FALSE);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	plen = poe_length(poep);
1827c478bd9Sstevel@tonic-gate 	if (plen + sizeof (*poep) > length)
1837c478bd9Sstevel@tonic-gate 		return (B_FALSE);
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	tstart = (const uint8_t *)(poep+1);
1867c478bd9Sstevel@tonic-gate 	tend = tstart + plen;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	/*
1897c478bd9Sstevel@tonic-gate 	 * Note careful dereference of tptr; it might be near the end
1907c478bd9Sstevel@tonic-gate 	 * already, so we have to range check it before dereferencing
1917c478bd9Sstevel@tonic-gate 	 * to get the actual tag length.  Yes, it looks like we have
1927c478bd9Sstevel@tonic-gate 	 * duplicate array end checks.  No, they're not duplicates.
1937c478bd9Sstevel@tonic-gate 	 */
1947c478bd9Sstevel@tonic-gate 	if (tptr < tstart || tptr+POET_HDRLEN > tend ||
1957c478bd9Sstevel@tonic-gate 	    tptr+POET_HDRLEN+POET_GET_LENG(tptr) > tend)
1967c478bd9Sstevel@tonic-gate 		return (B_FALSE);
1977c478bd9Sstevel@tonic-gate 	return (B_TRUE);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate static int
poe_tag_insert(poep_t * poep,uint16_t ttype,const void * data,size_t dlen)2017c478bd9Sstevel@tonic-gate poe_tag_insert(poep_t *poep, uint16_t ttype, const void *data, size_t dlen)
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	int plen;
2047c478bd9Sstevel@tonic-gate 	uint8_t *dp;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	plen = poe_length(poep);
2077c478bd9Sstevel@tonic-gate 	if (data == NULL)
2087c478bd9Sstevel@tonic-gate 		dlen = 0;
2097c478bd9Sstevel@tonic-gate 	if (sizeof (*poep) + plen + POET_HDRLEN + dlen > PPPOE_MSGMAX)
2107c478bd9Sstevel@tonic-gate 		return (-1);
2117c478bd9Sstevel@tonic-gate 	dp = (uint8_t *)(poep + 1) + plen;
2127c478bd9Sstevel@tonic-gate 	POET_SET_TYPE(dp, ttype);
2137c478bd9Sstevel@tonic-gate 	POET_SET_LENG(dp, dlen);
2147c478bd9Sstevel@tonic-gate 	if (dlen > 0)
2157c478bd9Sstevel@tonic-gate 		(void) memcpy(POET_DATA(dp), data, dlen);
2167c478bd9Sstevel@tonic-gate 	poep->poep_length = htons(plen + POET_HDRLEN + dlen);
2177c478bd9Sstevel@tonic-gate 	return (0);
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate /*
2217c478bd9Sstevel@tonic-gate  * Add a tag with text string data to a PPPoE packet being
2227c478bd9Sstevel@tonic-gate  * constructed.  Returns -1 if it doesn't fit, or 0 for success.
2237c478bd9Sstevel@tonic-gate  */
2247c478bd9Sstevel@tonic-gate int
poe_add_str(poep_t * poep,uint16_t ttype,const char * str)2257c478bd9Sstevel@tonic-gate poe_add_str(poep_t *poep, uint16_t ttype, const char *str)
2267c478bd9Sstevel@tonic-gate {
2277c478bd9Sstevel@tonic-gate 	return (poe_tag_insert(poep, ttype, str, strlen(str)));
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate /*
2317c478bd9Sstevel@tonic-gate  * Add a tag with 32-bit integer data to a PPPoE packet being
2327c478bd9Sstevel@tonic-gate  * constructed.  Returns -1 if it doesn't fit, or 0 for success.
2337c478bd9Sstevel@tonic-gate  */
2347c478bd9Sstevel@tonic-gate int
poe_add_long(poep_t * poep,uint16_t ttype,uint32_t val)2357c478bd9Sstevel@tonic-gate poe_add_long(poep_t *poep, uint16_t ttype, uint32_t val)
2367c478bd9Sstevel@tonic-gate {
2377c478bd9Sstevel@tonic-gate 	val = htonl(val);
2387c478bd9Sstevel@tonic-gate 	return (poe_tag_insert(poep, ttype, &val, sizeof (val)));
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate /*
2427c478bd9Sstevel@tonic-gate  * Add a tag with two 32-bit integers to a PPPoE packet being
2437c478bd9Sstevel@tonic-gate  * constructed.  Returns -1 if it doesn't fit, or 0 for success.
2447c478bd9Sstevel@tonic-gate  */
2457c478bd9Sstevel@tonic-gate int
poe_two_longs(poep_t * poep,uint16_t ttype,uint32_t val1,uint32_t val2)2467c478bd9Sstevel@tonic-gate poe_two_longs(poep_t *poep, uint16_t ttype, uint32_t val1, uint32_t val2)
2477c478bd9Sstevel@tonic-gate {
2487c478bd9Sstevel@tonic-gate 	uint32_t vals[2];
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	vals[0] = htonl(val1);
2517c478bd9Sstevel@tonic-gate 	vals[1] = htonl(val2);
2527c478bd9Sstevel@tonic-gate 	return (poe_tag_insert(poep, ttype, vals, sizeof (vals)));
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate /*
2567c478bd9Sstevel@tonic-gate  * Copy a single tag and its data from one PPPoE packet to a PPPoE
2577c478bd9Sstevel@tonic-gate  * packet being constructed.  Returns -1 if it doesn't fit, or 0 for
2587c478bd9Sstevel@tonic-gate  * success.
2597c478bd9Sstevel@tonic-gate  */
2607c478bd9Sstevel@tonic-gate int
poe_tag_copy(poep_t * poep,const uint8_t * tagp)2617c478bd9Sstevel@tonic-gate poe_tag_copy(poep_t *poep, const uint8_t *tagp)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	int tlen;
2647c478bd9Sstevel@tonic-gate 	int plen;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	tlen = POET_GET_LENG(tagp) + POET_HDRLEN;
2677c478bd9Sstevel@tonic-gate 	plen = poe_length(poep);
2687c478bd9Sstevel@tonic-gate 	if (sizeof (*poep) + plen + tlen > PPPOE_MSGMAX)
2697c478bd9Sstevel@tonic-gate 		return (-1);
2707c478bd9Sstevel@tonic-gate 	(void) memcpy((uint8_t *)(poep + 1) + plen, tagp, tlen);
2717c478bd9Sstevel@tonic-gate 	poep->poep_length = htons(tlen + plen);
2727c478bd9Sstevel@tonic-gate 	return (0);
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate struct tag_list {
2767c478bd9Sstevel@tonic-gate 	int tl_type;
2777c478bd9Sstevel@tonic-gate 	const char *tl_name;
2787c478bd9Sstevel@tonic-gate };
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate /* List of PPPoE data tag types. */
2817c478bd9Sstevel@tonic-gate static const struct tag_list tag_list[] = {
2827c478bd9Sstevel@tonic-gate 	{ POETT_END, "End-Of-List" },
2837c478bd9Sstevel@tonic-gate 	{ POETT_SERVICE, "Service-Name" },
2847c478bd9Sstevel@tonic-gate 	{ POETT_ACCESS, "AC-Name" },
2857c478bd9Sstevel@tonic-gate 	{ POETT_UNIQ, "Host-Uniq" },
2867c478bd9Sstevel@tonic-gate 	{ POETT_COOKIE, "AC-Cookie" },
2877c478bd9Sstevel@tonic-gate 	{ POETT_VENDOR, "Vendor-Specific" },
2887c478bd9Sstevel@tonic-gate 	{ POETT_RELAY, "Relay-Session-Id" },
2897c478bd9Sstevel@tonic-gate 	{ POETT_NAMERR, "Service-Name-Error" },
2907c478bd9Sstevel@tonic-gate 	{ POETT_SYSERR, "AC-System-Error" },
2917c478bd9Sstevel@tonic-gate 	{ POETT_GENERR, "Generic-Error" },
2927c478bd9Sstevel@tonic-gate 	{ POETT_MULTI, "Multicast-Capable" },
2937c478bd9Sstevel@tonic-gate 	{ POETT_HURL, "Host-URL" },
2947c478bd9Sstevel@tonic-gate 	{ POETT_MOTM, "Message-Of-The-Minute" },
2957c478bd9Sstevel@tonic-gate 	{ POETT_RTEADD, "IP-Route-Add" },
2967c478bd9Sstevel@tonic-gate 	{ 0, NULL }
2977c478bd9Sstevel@tonic-gate };
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate /* List of PPPoE message code numbers. */
3007c478bd9Sstevel@tonic-gate static const struct tag_list code_list[] = {
3017c478bd9Sstevel@tonic-gate 	{ POECODE_DATA, "Data" },
3027c478bd9Sstevel@tonic-gate 	{ POECODE_PADO, "Active Discovery Offer" },
3037c478bd9Sstevel@tonic-gate 	{ POECODE_PADI, "Active Discovery Initiation" },
3047c478bd9Sstevel@tonic-gate 	{ POECODE_PADR, "Active Discovery Request" },
3057c478bd9Sstevel@tonic-gate 	{ POECODE_PADS, "Active Discovery Session-confirmation" },
3067c478bd9Sstevel@tonic-gate 	{ POECODE_PADT, "Active Discovery Terminate" },
3077c478bd9Sstevel@tonic-gate 	{ POECODE_PADM, "Active Discovery Message" },
3087c478bd9Sstevel@tonic-gate 	{ POECODE_PADN, "Active Discovery Network" },
3097c478bd9Sstevel@tonic-gate 	{ 0, NULL }
3107c478bd9Sstevel@tonic-gate };
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate /*
3137c478bd9Sstevel@tonic-gate  * Given a tag type number, return a pointer to a string describing
3147c478bd9Sstevel@tonic-gate  * the tag.
3157c478bd9Sstevel@tonic-gate  */
3167c478bd9Sstevel@tonic-gate const char *
poe_tagname(uint16_t tagtype)3177c478bd9Sstevel@tonic-gate poe_tagname(uint16_t tagtype)
3187c478bd9Sstevel@tonic-gate {
3197c478bd9Sstevel@tonic-gate 	const struct tag_list *tlp;
3207c478bd9Sstevel@tonic-gate 	static char tname[32];
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	for (tlp = tag_list; tlp->tl_name != NULL; tlp++)
3237c478bd9Sstevel@tonic-gate 		if (tagtype == tlp->tl_type)
3247c478bd9Sstevel@tonic-gate 			return (tlp->tl_name);
3257c478bd9Sstevel@tonic-gate 	(void) sprintf(tname, "Tag%d", tagtype);
3267c478bd9Sstevel@tonic-gate 	return (tname);
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate /*
3307c478bd9Sstevel@tonic-gate  * Given a PPPoE message code number, return a pointer to a string
3317c478bd9Sstevel@tonic-gate  * describing the message.
3327c478bd9Sstevel@tonic-gate  */
3337c478bd9Sstevel@tonic-gate const char *
poe_codename(uint8_t codetype)3347c478bd9Sstevel@tonic-gate poe_codename(uint8_t codetype)
3357c478bd9Sstevel@tonic-gate {
3367c478bd9Sstevel@tonic-gate 	const struct tag_list *tlp;
3377c478bd9Sstevel@tonic-gate 	static char tname[32];
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	for (tlp = code_list; tlp->tl_name != NULL; tlp++)
3407c478bd9Sstevel@tonic-gate 		if (codetype == tlp->tl_type)
3417c478bd9Sstevel@tonic-gate 			return (tlp->tl_name);
3427c478bd9Sstevel@tonic-gate 	(void) sprintf(tname, "Code%d", codetype);
3437c478bd9Sstevel@tonic-gate 	return (tname);
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate /*
3477c478bd9Sstevel@tonic-gate  * Given a tunnel driver address structure, return a pointer to a
3487c478bd9Sstevel@tonic-gate  * string naming that Ethernet host.
3497c478bd9Sstevel@tonic-gate  */
3507c478bd9Sstevel@tonic-gate const char *
ehost2(const struct ether_addr * ea)3517c478bd9Sstevel@tonic-gate ehost2(const struct ether_addr *ea)
3527c478bd9Sstevel@tonic-gate {
3537c478bd9Sstevel@tonic-gate 	static char hbuf[MAXHOSTNAMELEN+1];
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	if (ea == NULL)
3567c478bd9Sstevel@tonic-gate 		return ("NULL");
3577c478bd9Sstevel@tonic-gate 	if (ether_ntohost(hbuf, ea) == 0)
3587c478bd9Sstevel@tonic-gate 		return (hbuf);
3597c478bd9Sstevel@tonic-gate 	return (ether_ntoa(ea));
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate const char *
ehost(const ppptun_atype * pap)3637c478bd9Sstevel@tonic-gate ehost(const ppptun_atype *pap)
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate 	return (ehost2((const struct ether_addr *)pap));
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate /*
3697c478bd9Sstevel@tonic-gate  * Given an Internet address (in network byte order), return a pointer
3707c478bd9Sstevel@tonic-gate  * to a string naming the host.
3717c478bd9Sstevel@tonic-gate  */
3727c478bd9Sstevel@tonic-gate const char *
ihost(uint32_t haddr)3737c478bd9Sstevel@tonic-gate ihost(uint32_t haddr)
3747c478bd9Sstevel@tonic-gate {
3757c478bd9Sstevel@tonic-gate 	struct hostent *hp;
3767c478bd9Sstevel@tonic-gate 	struct sockaddr_in sin;
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	(void) memset(&sin, '\0', sizeof (sin));
3797c478bd9Sstevel@tonic-gate 	sin.sin_addr.s_addr = haddr;
3807c478bd9Sstevel@tonic-gate 	hp = gethostbyaddr((const char *)&sin, sizeof (sin), AF_INET);
3817c478bd9Sstevel@tonic-gate 	if (hp != NULL)
3827c478bd9Sstevel@tonic-gate 		return (hp->h_name);
3837c478bd9Sstevel@tonic-gate 	return (inet_ntoa(sin.sin_addr));
3847c478bd9Sstevel@tonic-gate }
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate int
hexdecode(char chr)3877c478bd9Sstevel@tonic-gate hexdecode(char chr)
3887c478bd9Sstevel@tonic-gate {
3897c478bd9Sstevel@tonic-gate 	if (chr >= '0' && chr <= '9')
3907c478bd9Sstevel@tonic-gate 		return ((int)(chr - '0'));
391*32529ec1Srshoaib 	if (chr >= 'a' && chr <= 'f')
3927c478bd9Sstevel@tonic-gate 		return ((int)(chr - 'a' + 10));
3937c478bd9Sstevel@tonic-gate 	return ((int)(chr - 'A' + 10));
3947c478bd9Sstevel@tonic-gate }
395