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
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.
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]
23 * PPPoE common utilities and data.
25 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
26 * Use is subject to license terms.
29 #pragma ident "%Z%%M% %I% %E% SMI"
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>
47 /* Not all functions are used by all applications. Let lint know this. */
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)];
55 const char tunnam
[] = "/dev/" PPP_TUN_NAME
;
57 const ether_addr_t ether_bcast
= { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
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.
70 if ((estr
= strerror(err
)) != NULL
)
72 (void) snprintf(ebuf
, sizeof (ebuf
), "Error:%d", err
);
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.
82 myperror(const char *emsg
)
84 (void) fprintf(stderr
, "%s: %s: %s\n", myname
, emsg
,
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.
95 mygetmsg(int fd
, struct strbuf
*ctrl
, struct strbuf
*data
, int *flags
)
100 hadflags
= getmsg(fd
, ctrl
, data
, flags
);
101 if (hadflags
<= 0 || !(hadflags
& (MORECTL
| MOREDATA
)))
107 retv
= getmsg(fd
, ctrl
, data
, flags
);
108 } while (retv
> 0 || (retv
< 0 && errno
== EINTR
));
111 * What remains at this point is the tail end of the
112 * truncated message. Toss it.
115 return (retv
< 0 ? retv
: hadflags
);
119 * Common wrapper function for STREAMS I_STR ioctl. Returns -1 on
120 * failure, 0 for success.
123 strioctl(int fd
, int cmd
, void *ptr
, int ilen
, int olen
)
128 str
.ic_timout
= 0; /* Default timeout; 15 seconds */
132 if (ioctl(fd
, I_STR
, &str
) == -1) {
135 if (str
.ic_len
!= olen
) {
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.
151 poe_mkheader(void *dptr
, uint8_t codeval
, int sessionid
)
155 /* Discard obvious junk. */
156 assert(dptr
!= NULL
&& IS_P2ALIGNED(dptr
, sizeof (poep_t
*)));
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);
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.
172 poe_tagcheck(const poep_t
*poep
, int length
, const uint8_t *tptr
)
175 const uint8_t *tstart
, *tend
;
177 if (poep
== NULL
|| !IS_P2ALIGNED(poep
, sizeof (uint16_t)) ||
178 tptr
== NULL
|| length
< sizeof (*poep
))
181 plen
= poe_length(poep
);
182 if (plen
+ sizeof (*poep
) > length
)
185 tstart
= (const uint8_t *)(poep
+1);
186 tend
= tstart
+ plen
;
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.
194 if (tptr
< tstart
|| tptr
+POET_HDRLEN
> tend
||
195 tptr
+POET_HDRLEN
+POET_GET_LENG(tptr
) > tend
)
201 poe_tag_insert(poep_t
*poep
, uint16_t ttype
, const void *data
, size_t dlen
)
206 plen
= poe_length(poep
);
209 if (sizeof (*poep
) + plen
+ POET_HDRLEN
+ dlen
> PPPOE_MSGMAX
)
211 dp
= (uint8_t *)(poep
+ 1) + plen
;
212 POET_SET_TYPE(dp
, ttype
);
213 POET_SET_LENG(dp
, dlen
);
215 (void) memcpy(POET_DATA(dp
), data
, dlen
);
216 poep
->poep_length
= htons(plen
+ POET_HDRLEN
+ dlen
);
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.
225 poe_add_str(poep_t
*poep
, uint16_t ttype
, const char *str
)
227 return (poe_tag_insert(poep
, ttype
, str
, strlen(str
)));
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.
235 poe_add_long(poep_t
*poep
, uint16_t ttype
, uint32_t val
)
238 return (poe_tag_insert(poep
, ttype
, &val
, sizeof (val
)));
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.
246 poe_two_longs(poep_t
*poep
, uint16_t ttype
, uint32_t val1
, uint32_t val2
)
250 vals
[0] = htonl(val1
);
251 vals
[1] = htonl(val2
);
252 return (poe_tag_insert(poep
, ttype
, vals
, sizeof (vals
)));
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
261 poe_tag_copy(poep_t
*poep
, const uint8_t *tagp
)
266 tlen
= POET_GET_LENG(tagp
) + POET_HDRLEN
;
267 plen
= poe_length(poep
);
268 if (sizeof (*poep
) + plen
+ tlen
> PPPOE_MSGMAX
)
270 (void) memcpy((uint8_t *)(poep
+ 1) + plen
, tagp
, tlen
);
271 poep
->poep_length
= htons(tlen
+ plen
);
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" },
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" },
313 * Given a tag type number, return a pointer to a string describing
317 poe_tagname(uint16_t tagtype
)
319 const struct tag_list
*tlp
;
320 static char tname
[32];
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
);
330 * Given a PPPoE message code number, return a pointer to a string
331 * describing the message.
334 poe_codename(uint8_t codetype
)
336 const struct tag_list
*tlp
;
337 static char tname
[32];
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
);
347 * Given a tunnel driver address structure, return a pointer to a
348 * string naming that Ethernet host.
351 ehost2(const struct ether_addr
*ea
)
353 static char hbuf
[MAXHOSTNAMELEN
+1];
357 if (ether_ntohost(hbuf
, ea
) == 0)
359 return (ether_ntoa(ea
));
363 ehost(const ppptun_atype
*pap
)
365 return (ehost2((const struct ether_addr
*)pap
));
369 * Given an Internet address (in network byte order), return a pointer
370 * to a string naming the host.
373 ihost(uint32_t haddr
)
376 struct sockaddr_in sin
;
378 (void) memset(&sin
, '\0', sizeof (sin
));
379 sin
.sin_addr
.s_addr
= haddr
;
380 hp
= gethostbyaddr((const char *)&sin
, sizeof (sin
), AF_INET
);
383 return (inet_ntoa(sin
.sin_addr
));
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));