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