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