1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * PPPoE common utilities and data.
24*7c478bd9Sstevel@tonic-gate  *
25*7c478bd9Sstevel@tonic-gate  * Copyright 2000-2002 Sun Microsystems, Inc.  All rights reserved.
26*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
27*7c478bd9Sstevel@tonic-gate  */
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #include <stdio.h>
32*7c478bd9Sstevel@tonic-gate #include <unistd.h>
33*7c478bd9Sstevel@tonic-gate #include <string.h>
34*7c478bd9Sstevel@tonic-gate #include <errno.h>
35*7c478bd9Sstevel@tonic-gate #include <netdb.h>
36*7c478bd9Sstevel@tonic-gate #include <assert.h>
37*7c478bd9Sstevel@tonic-gate #include <stropts.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
39*7c478bd9Sstevel@tonic-gate #include <inet/common.h>
40*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
41*7c478bd9Sstevel@tonic-gate #include <net/sppptun.h>
42*7c478bd9Sstevel@tonic-gate #include <net/pppoe.h>
43*7c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate #include "common.h"
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate /* Not all functions are used by all applications.  Let lint know this. */
48*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate /* Common I/O buffers */
51*7c478bd9Sstevel@tonic-gate uint32_t pkt_input[PKT_INPUT_LEN / sizeof (uint32_t)];
52*7c478bd9Sstevel@tonic-gate uint32_t pkt_octl[PKT_OCTL_LEN / sizeof (uint32_t)];
53*7c478bd9Sstevel@tonic-gate uint32_t pkt_output[PKT_OUTPUT_LEN / sizeof (uint32_t)];
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate const char tunnam[] = "/dev/" PPP_TUN_NAME;
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate const ether_addr_t ether_bcast = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate /*
60*7c478bd9Sstevel@tonic-gate  * Wrapper for standard strerror() function -- the standard allows
61*7c478bd9Sstevel@tonic-gate  * that routine to return NULL, and that's inconvenient to handle.
62*7c478bd9Sstevel@tonic-gate  * This function never returns NULL.
63*7c478bd9Sstevel@tonic-gate  */
64*7c478bd9Sstevel@tonic-gate const char *
65*7c478bd9Sstevel@tonic-gate mystrerror(int err)
66*7c478bd9Sstevel@tonic-gate {
67*7c478bd9Sstevel@tonic-gate 	const char *estr;
68*7c478bd9Sstevel@tonic-gate 	static char ebuf[64];
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 	if ((estr = strerror(err)) != NULL)
71*7c478bd9Sstevel@tonic-gate 		return (estr);
72*7c478bd9Sstevel@tonic-gate 	(void) snprintf(ebuf, sizeof (ebuf), "Error:%d", err);
73*7c478bd9Sstevel@tonic-gate 	return (ebuf);
74*7c478bd9Sstevel@tonic-gate }
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate /*
77*7c478bd9Sstevel@tonic-gate  * Wrapper for standard perror() function -- the standard definition
78*7c478bd9Sstevel@tonic-gate  * of perror doesn't include the program name in the output and is
79*7c478bd9Sstevel@tonic-gate  * thus inconvenient to use.
80*7c478bd9Sstevel@tonic-gate  */
81*7c478bd9Sstevel@tonic-gate void
82*7c478bd9Sstevel@tonic-gate myperror(const char *emsg)
83*7c478bd9Sstevel@tonic-gate {
84*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s\n", myname, emsg,
85*7c478bd9Sstevel@tonic-gate 	    mystrerror(errno));
86*7c478bd9Sstevel@tonic-gate }
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate /*
89*7c478bd9Sstevel@tonic-gate  * Wrapper for standard getmsg() function.  Completely discards any
90*7c478bd9Sstevel@tonic-gate  * fragmented messages because we don't expect ever to see these from
91*7c478bd9Sstevel@tonic-gate  * a properly functioning tunnel driver.  Returns flags
92*7c478bd9Sstevel@tonic-gate  * (MORECTL|MOREDATA) as seen by interface.
93*7c478bd9Sstevel@tonic-gate  */
94*7c478bd9Sstevel@tonic-gate int
95*7c478bd9Sstevel@tonic-gate mygetmsg(int fd, struct strbuf *ctrl, struct strbuf *data, int *flags)
96*7c478bd9Sstevel@tonic-gate {
97*7c478bd9Sstevel@tonic-gate 	int retv;
98*7c478bd9Sstevel@tonic-gate 	int hadflags;
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	hadflags = getmsg(fd, ctrl, data, flags);
101*7c478bd9Sstevel@tonic-gate 	if (hadflags <= 0 || !(hadflags & (MORECTL | MOREDATA)))
102*7c478bd9Sstevel@tonic-gate 		return (hadflags);
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	do {
105*7c478bd9Sstevel@tonic-gate 		if (flags != NULL)
106*7c478bd9Sstevel@tonic-gate 			*flags = 0;
107*7c478bd9Sstevel@tonic-gate 		retv = getmsg(fd, ctrl, data, flags);
108*7c478bd9Sstevel@tonic-gate 	} while (retv > 0 || (retv < 0 && errno == EINTR));
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	/*
111*7c478bd9Sstevel@tonic-gate 	 * What remains at this point is the tail end of the
112*7c478bd9Sstevel@tonic-gate 	 * truncated message.  Toss it.
113*7c478bd9Sstevel@tonic-gate 	 */
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 	return (retv < 0 ? retv : hadflags);
116*7c478bd9Sstevel@tonic-gate }
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate /*
119*7c478bd9Sstevel@tonic-gate  * Common wrapper function for STREAMS I_STR ioctl.  Returns -1 on
120*7c478bd9Sstevel@tonic-gate  * failure, 0 for success.
121*7c478bd9Sstevel@tonic-gate  */
122*7c478bd9Sstevel@tonic-gate int
123*7c478bd9Sstevel@tonic-gate strioctl(int fd, int cmd, void *ptr, int ilen, int olen)
124*7c478bd9Sstevel@tonic-gate {
125*7c478bd9Sstevel@tonic-gate 	struct strioctl	str;
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	str.ic_cmd = cmd;
128*7c478bd9Sstevel@tonic-gate 	str.ic_timout = 0;	/* Default timeout; 15 seconds */
129*7c478bd9Sstevel@tonic-gate 	str.ic_len = ilen;
130*7c478bd9Sstevel@tonic-gate 	str.ic_dp = ptr;
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd, I_STR, &str) == -1) {
133*7c478bd9Sstevel@tonic-gate 		return (-1);
134*7c478bd9Sstevel@tonic-gate 	}
135*7c478bd9Sstevel@tonic-gate 	if (str.ic_len != olen) {
136*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
137*7c478bd9Sstevel@tonic-gate 		return (-1);
138*7c478bd9Sstevel@tonic-gate 	}
139*7c478bd9Sstevel@tonic-gate 	return (0);
140*7c478bd9Sstevel@tonic-gate }
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate /*
143*7c478bd9Sstevel@tonic-gate  * Format a PPPoE header in the user's buffer.  The returned pointer
144*7c478bd9Sstevel@tonic-gate  * is either identical to the first argument, or is NULL if it's not
145*7c478bd9Sstevel@tonic-gate  * usable.  On entry, dptr should point to the first byte after the
146*7c478bd9Sstevel@tonic-gate  * Ethertype field, codeval should be one of the POECODE_* values, and
147*7c478bd9Sstevel@tonic-gate  * sessionid should be the assigned session ID number or one of the
148*7c478bd9Sstevel@tonic-gate  * special POESESS_* values.
149*7c478bd9Sstevel@tonic-gate  */
150*7c478bd9Sstevel@tonic-gate poep_t *
151*7c478bd9Sstevel@tonic-gate poe_mkheader(void *dptr, uint8_t codeval, int sessionid)
152*7c478bd9Sstevel@tonic-gate {
153*7c478bd9Sstevel@tonic-gate 	poep_t *poep;
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	/* Discard obvious junk. */
156*7c478bd9Sstevel@tonic-gate 	assert(dptr != NULL && IS_P2ALIGNED(dptr, sizeof (poep_t *)));
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 	/* Initialize the header */
159*7c478bd9Sstevel@tonic-gate 	poep = (poep_t *)dptr;
160*7c478bd9Sstevel@tonic-gate 	poep->poep_version_type = POE_VERSION;
161*7c478bd9Sstevel@tonic-gate 	poep->poep_code = codeval;
162*7c478bd9Sstevel@tonic-gate 	poep->poep_session_id = htons(sessionid);
163*7c478bd9Sstevel@tonic-gate 	poep->poep_length = htons(0);
164*7c478bd9Sstevel@tonic-gate 	return (poep);
165*7c478bd9Sstevel@tonic-gate }
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate /*
168*7c478bd9Sstevel@tonic-gate  * Validate that a given tag is intact.  This is intended to be used
169*7c478bd9Sstevel@tonic-gate  * in tag-parsing loops before attempting to access the tag data.
170*7c478bd9Sstevel@tonic-gate  */
171*7c478bd9Sstevel@tonic-gate boolean_t
172*7c478bd9Sstevel@tonic-gate poe_tagcheck(const poep_t *poep, int length, const uint8_t *tptr)
173*7c478bd9Sstevel@tonic-gate {
174*7c478bd9Sstevel@tonic-gate 	int plen;
175*7c478bd9Sstevel@tonic-gate 	const uint8_t *tstart, *tend;
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 	if (poep == NULL || !IS_P2ALIGNED(poep, sizeof (uint16_t)) ||
178*7c478bd9Sstevel@tonic-gate 	    tptr == NULL || length < sizeof (*poep))
179*7c478bd9Sstevel@tonic-gate 		return (B_FALSE);
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	plen = poe_length(poep);
182*7c478bd9Sstevel@tonic-gate 	if (plen + sizeof (*poep) > length)
183*7c478bd9Sstevel@tonic-gate 		return (B_FALSE);
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 	tstart = (const uint8_t *)(poep+1);
186*7c478bd9Sstevel@tonic-gate 	tend = tstart + plen;
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 	/*
189*7c478bd9Sstevel@tonic-gate 	 * Note careful dereference of tptr; it might be near the end
190*7c478bd9Sstevel@tonic-gate 	 * already, so we have to range check it before dereferencing
191*7c478bd9Sstevel@tonic-gate 	 * to get the actual tag length.  Yes, it looks like we have
192*7c478bd9Sstevel@tonic-gate 	 * duplicate array end checks.  No, they're not duplicates.
193*7c478bd9Sstevel@tonic-gate 	 */
194*7c478bd9Sstevel@tonic-gate 	if (tptr < tstart || tptr+POET_HDRLEN > tend ||
195*7c478bd9Sstevel@tonic-gate 	    tptr+POET_HDRLEN+POET_GET_LENG(tptr) > tend)
196*7c478bd9Sstevel@tonic-gate 		return (B_FALSE);
197*7c478bd9Sstevel@tonic-gate 	return (B_TRUE);
198*7c478bd9Sstevel@tonic-gate }
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate static int
201*7c478bd9Sstevel@tonic-gate poe_tag_insert(poep_t *poep, uint16_t ttype, const void *data, size_t dlen)
202*7c478bd9Sstevel@tonic-gate {
203*7c478bd9Sstevel@tonic-gate 	int plen;
204*7c478bd9Sstevel@tonic-gate 	uint8_t *dp;
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	plen = poe_length(poep);
207*7c478bd9Sstevel@tonic-gate 	if (data == NULL)
208*7c478bd9Sstevel@tonic-gate 		dlen = 0;
209*7c478bd9Sstevel@tonic-gate 	if (sizeof (*poep) + plen + POET_HDRLEN + dlen > PPPOE_MSGMAX)
210*7c478bd9Sstevel@tonic-gate 		return (-1);
211*7c478bd9Sstevel@tonic-gate 	dp = (uint8_t *)(poep + 1) + plen;
212*7c478bd9Sstevel@tonic-gate 	POET_SET_TYPE(dp, ttype);
213*7c478bd9Sstevel@tonic-gate 	POET_SET_LENG(dp, dlen);
214*7c478bd9Sstevel@tonic-gate 	if (dlen > 0)
215*7c478bd9Sstevel@tonic-gate 		(void) memcpy(POET_DATA(dp), data, dlen);
216*7c478bd9Sstevel@tonic-gate 	poep->poep_length = htons(plen + POET_HDRLEN + dlen);
217*7c478bd9Sstevel@tonic-gate 	return (0);
218*7c478bd9Sstevel@tonic-gate }
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate /*
221*7c478bd9Sstevel@tonic-gate  * Add a tag with text string data to a PPPoE packet being
222*7c478bd9Sstevel@tonic-gate  * constructed.  Returns -1 if it doesn't fit, or 0 for success.
223*7c478bd9Sstevel@tonic-gate  */
224*7c478bd9Sstevel@tonic-gate int
225*7c478bd9Sstevel@tonic-gate poe_add_str(poep_t *poep, uint16_t ttype, const char *str)
226*7c478bd9Sstevel@tonic-gate {
227*7c478bd9Sstevel@tonic-gate 	return (poe_tag_insert(poep, ttype, str, strlen(str)));
228*7c478bd9Sstevel@tonic-gate }
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate /*
231*7c478bd9Sstevel@tonic-gate  * Add a tag with 32-bit integer data to a PPPoE packet being
232*7c478bd9Sstevel@tonic-gate  * constructed.  Returns -1 if it doesn't fit, or 0 for success.
233*7c478bd9Sstevel@tonic-gate  */
234*7c478bd9Sstevel@tonic-gate int
235*7c478bd9Sstevel@tonic-gate poe_add_long(poep_t *poep, uint16_t ttype, uint32_t val)
236*7c478bd9Sstevel@tonic-gate {
237*7c478bd9Sstevel@tonic-gate 	val = htonl(val);
238*7c478bd9Sstevel@tonic-gate 	return (poe_tag_insert(poep, ttype, &val, sizeof (val)));
239*7c478bd9Sstevel@tonic-gate }
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate /*
242*7c478bd9Sstevel@tonic-gate  * Add a tag with two 32-bit integers to a PPPoE packet being
243*7c478bd9Sstevel@tonic-gate  * constructed.  Returns -1 if it doesn't fit, or 0 for success.
244*7c478bd9Sstevel@tonic-gate  */
245*7c478bd9Sstevel@tonic-gate int
246*7c478bd9Sstevel@tonic-gate poe_two_longs(poep_t *poep, uint16_t ttype, uint32_t val1, uint32_t val2)
247*7c478bd9Sstevel@tonic-gate {
248*7c478bd9Sstevel@tonic-gate 	uint32_t vals[2];
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate 	vals[0] = htonl(val1);
251*7c478bd9Sstevel@tonic-gate 	vals[1] = htonl(val2);
252*7c478bd9Sstevel@tonic-gate 	return (poe_tag_insert(poep, ttype, vals, sizeof (vals)));
253*7c478bd9Sstevel@tonic-gate }
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate /*
256*7c478bd9Sstevel@tonic-gate  * Copy a single tag and its data from one PPPoE packet to a PPPoE
257*7c478bd9Sstevel@tonic-gate  * packet being constructed.  Returns -1 if it doesn't fit, or 0 for
258*7c478bd9Sstevel@tonic-gate  * success.
259*7c478bd9Sstevel@tonic-gate  */
260*7c478bd9Sstevel@tonic-gate int
261*7c478bd9Sstevel@tonic-gate poe_tag_copy(poep_t *poep, const uint8_t *tagp)
262*7c478bd9Sstevel@tonic-gate {
263*7c478bd9Sstevel@tonic-gate 	int tlen;
264*7c478bd9Sstevel@tonic-gate 	int plen;
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate 	tlen = POET_GET_LENG(tagp) + POET_HDRLEN;
267*7c478bd9Sstevel@tonic-gate 	plen = poe_length(poep);
268*7c478bd9Sstevel@tonic-gate 	if (sizeof (*poep) + plen + tlen > PPPOE_MSGMAX)
269*7c478bd9Sstevel@tonic-gate 		return (-1);
270*7c478bd9Sstevel@tonic-gate 	(void) memcpy((uint8_t *)(poep + 1) + plen, tagp, tlen);
271*7c478bd9Sstevel@tonic-gate 	poep->poep_length = htons(tlen + plen);
272*7c478bd9Sstevel@tonic-gate 	return (0);
273*7c478bd9Sstevel@tonic-gate }
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate struct tag_list {
276*7c478bd9Sstevel@tonic-gate 	int tl_type;
277*7c478bd9Sstevel@tonic-gate 	const char *tl_name;
278*7c478bd9Sstevel@tonic-gate };
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate /* List of PPPoE data tag types. */
281*7c478bd9Sstevel@tonic-gate static const struct tag_list tag_list[] = {
282*7c478bd9Sstevel@tonic-gate 	{ POETT_END, "End-Of-List" },
283*7c478bd9Sstevel@tonic-gate 	{ POETT_SERVICE, "Service-Name" },
284*7c478bd9Sstevel@tonic-gate 	{ POETT_ACCESS, "AC-Name" },
285*7c478bd9Sstevel@tonic-gate 	{ POETT_UNIQ, "Host-Uniq" },
286*7c478bd9Sstevel@tonic-gate 	{ POETT_COOKIE, "AC-Cookie" },
287*7c478bd9Sstevel@tonic-gate 	{ POETT_VENDOR, "Vendor-Specific" },
288*7c478bd9Sstevel@tonic-gate 	{ POETT_RELAY, "Relay-Session-Id" },
289*7c478bd9Sstevel@tonic-gate 	{ POETT_NAMERR, "Service-Name-Error" },
290*7c478bd9Sstevel@tonic-gate 	{ POETT_SYSERR, "AC-System-Error" },
291*7c478bd9Sstevel@tonic-gate 	{ POETT_GENERR, "Generic-Error" },
292*7c478bd9Sstevel@tonic-gate 	{ POETT_MULTI, "Multicast-Capable" },
293*7c478bd9Sstevel@tonic-gate 	{ POETT_HURL, "Host-URL" },
294*7c478bd9Sstevel@tonic-gate 	{ POETT_MOTM, "Message-Of-The-Minute" },
295*7c478bd9Sstevel@tonic-gate 	{ POETT_RTEADD, "IP-Route-Add" },
296*7c478bd9Sstevel@tonic-gate 	{ 0, NULL }
297*7c478bd9Sstevel@tonic-gate };
298*7c478bd9Sstevel@tonic-gate 
299*7c478bd9Sstevel@tonic-gate /* List of PPPoE message code numbers. */
300*7c478bd9Sstevel@tonic-gate static const struct tag_list code_list[] = {
301*7c478bd9Sstevel@tonic-gate 	{ POECODE_DATA, "Data" },
302*7c478bd9Sstevel@tonic-gate 	{ POECODE_PADO, "Active Discovery Offer" },
303*7c478bd9Sstevel@tonic-gate 	{ POECODE_PADI, "Active Discovery Initiation" },
304*7c478bd9Sstevel@tonic-gate 	{ POECODE_PADR, "Active Discovery Request" },
305*7c478bd9Sstevel@tonic-gate 	{ POECODE_PADS, "Active Discovery Session-confirmation" },
306*7c478bd9Sstevel@tonic-gate 	{ POECODE_PADT, "Active Discovery Terminate" },
307*7c478bd9Sstevel@tonic-gate 	{ POECODE_PADM, "Active Discovery Message" },
308*7c478bd9Sstevel@tonic-gate 	{ POECODE_PADN, "Active Discovery Network" },
309*7c478bd9Sstevel@tonic-gate 	{ 0, NULL }
310*7c478bd9Sstevel@tonic-gate };
311*7c478bd9Sstevel@tonic-gate 
312*7c478bd9Sstevel@tonic-gate /*
313*7c478bd9Sstevel@tonic-gate  * Given a tag type number, return a pointer to a string describing
314*7c478bd9Sstevel@tonic-gate  * the tag.
315*7c478bd9Sstevel@tonic-gate  */
316*7c478bd9Sstevel@tonic-gate const char *
317*7c478bd9Sstevel@tonic-gate poe_tagname(uint16_t tagtype)
318*7c478bd9Sstevel@tonic-gate {
319*7c478bd9Sstevel@tonic-gate 	const struct tag_list *tlp;
320*7c478bd9Sstevel@tonic-gate 	static char tname[32];
321*7c478bd9Sstevel@tonic-gate 
322*7c478bd9Sstevel@tonic-gate 	for (tlp = tag_list; tlp->tl_name != NULL; tlp++)
323*7c478bd9Sstevel@tonic-gate 		if (tagtype == tlp->tl_type)
324*7c478bd9Sstevel@tonic-gate 			return (tlp->tl_name);
325*7c478bd9Sstevel@tonic-gate 	(void) sprintf(tname, "Tag%d", tagtype);
326*7c478bd9Sstevel@tonic-gate 	return (tname);
327*7c478bd9Sstevel@tonic-gate }
328*7c478bd9Sstevel@tonic-gate 
329*7c478bd9Sstevel@tonic-gate /*
330*7c478bd9Sstevel@tonic-gate  * Given a PPPoE message code number, return a pointer to a string
331*7c478bd9Sstevel@tonic-gate  * describing the message.
332*7c478bd9Sstevel@tonic-gate  */
333*7c478bd9Sstevel@tonic-gate const char *
334*7c478bd9Sstevel@tonic-gate poe_codename(uint8_t codetype)
335*7c478bd9Sstevel@tonic-gate {
336*7c478bd9Sstevel@tonic-gate 	const struct tag_list *tlp;
337*7c478bd9Sstevel@tonic-gate 	static char tname[32];
338*7c478bd9Sstevel@tonic-gate 
339*7c478bd9Sstevel@tonic-gate 	for (tlp = code_list; tlp->tl_name != NULL; tlp++)
340*7c478bd9Sstevel@tonic-gate 		if (codetype == tlp->tl_type)
341*7c478bd9Sstevel@tonic-gate 			return (tlp->tl_name);
342*7c478bd9Sstevel@tonic-gate 	(void) sprintf(tname, "Code%d", codetype);
343*7c478bd9Sstevel@tonic-gate 	return (tname);
344*7c478bd9Sstevel@tonic-gate }
345*7c478bd9Sstevel@tonic-gate 
346*7c478bd9Sstevel@tonic-gate /*
347*7c478bd9Sstevel@tonic-gate  * Given a tunnel driver address structure, return a pointer to a
348*7c478bd9Sstevel@tonic-gate  * string naming that Ethernet host.
349*7c478bd9Sstevel@tonic-gate  */
350*7c478bd9Sstevel@tonic-gate const char *
351*7c478bd9Sstevel@tonic-gate ehost2(const struct ether_addr *ea)
352*7c478bd9Sstevel@tonic-gate {
353*7c478bd9Sstevel@tonic-gate 	static char hbuf[MAXHOSTNAMELEN+1];
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate 	if (ea == NULL)
356*7c478bd9Sstevel@tonic-gate 		return ("NULL");
357*7c478bd9Sstevel@tonic-gate 	if (ether_ntohost(hbuf, ea) == 0)
358*7c478bd9Sstevel@tonic-gate 		return (hbuf);
359*7c478bd9Sstevel@tonic-gate 	return (ether_ntoa(ea));
360*7c478bd9Sstevel@tonic-gate }
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate const char *
363*7c478bd9Sstevel@tonic-gate ehost(const ppptun_atype *pap)
364*7c478bd9Sstevel@tonic-gate {
365*7c478bd9Sstevel@tonic-gate 	return (ehost2((const struct ether_addr *)pap));
366*7c478bd9Sstevel@tonic-gate }
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate /*
369*7c478bd9Sstevel@tonic-gate  * Given an Internet address (in network byte order), return a pointer
370*7c478bd9Sstevel@tonic-gate  * to a string naming the host.
371*7c478bd9Sstevel@tonic-gate  */
372*7c478bd9Sstevel@tonic-gate const char *
373*7c478bd9Sstevel@tonic-gate ihost(uint32_t haddr)
374*7c478bd9Sstevel@tonic-gate {
375*7c478bd9Sstevel@tonic-gate 	struct hostent *hp;
376*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in sin;
377*7c478bd9Sstevel@tonic-gate 
378*7c478bd9Sstevel@tonic-gate 	(void) memset(&sin, '\0', sizeof (sin));
379*7c478bd9Sstevel@tonic-gate 	sin.sin_addr.s_addr = haddr;
380*7c478bd9Sstevel@tonic-gate 	hp = gethostbyaddr((const char *)&sin, sizeof (sin), AF_INET);
381*7c478bd9Sstevel@tonic-gate 	if (hp != NULL)
382*7c478bd9Sstevel@tonic-gate 		return (hp->h_name);
383*7c478bd9Sstevel@tonic-gate 	return (inet_ntoa(sin.sin_addr));
384*7c478bd9Sstevel@tonic-gate }
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate int
387*7c478bd9Sstevel@tonic-gate hexdecode(char chr)
388*7c478bd9Sstevel@tonic-gate {
389*7c478bd9Sstevel@tonic-gate 	if (chr >= '0' && chr <= '9')
390*7c478bd9Sstevel@tonic-gate 		return ((int)(chr - '0'));
391*7c478bd9Sstevel@tonic-gate 	if (chr >= 'a' && chr <= 'F')
392*7c478bd9Sstevel@tonic-gate 		return ((int)(chr - 'a' + 10));
393*7c478bd9Sstevel@tonic-gate 	return ((int)(chr - 'A' + 10));
394*7c478bd9Sstevel@tonic-gate }
395