1 /***********************************************************************
5 * Implementation of user-space PPPoE redirector for Linux.
7 * Common functions used by PPPoE client and server
9 * Copyright (C) 2000 by Roaring Penguin Software Inc.
11 * This program may be distributed according to the terms of the GNU
12 * General Public License, version 2 or (at your option) any later version.
14 ***********************************************************************/
16 static char const RCSID[] =
17 "$Id: common.c,v 1.2 2004/01/13 04:03:58 paulus Exp $";
33 /**********************************************************************
34 *%FUNCTION: parsePacket
36 * packet -- the PPPoE discovery packet to parse
37 * func -- function called for each tag in the packet
38 * extra -- an opaque data pointer supplied to parsing function
40 * 0 if everything went well; -1 if there was an error
42 * Parses a PPPoE discovery packet, calling "func" for each tag in the packet.
43 * "func" is passed the additional argument "extra".
44 ***********************************************************************/
46 parsePacket(PPPoEPacket *packet, ParseFunc *func, void *extra)
48 UINT16_t len = ntohs(packet->length);
49 unsigned char *curTag;
50 UINT16_t tagType, tagLen;
52 if (packet->ver != 1) {
53 syslog(LOG_ERR, "Invalid PPPoE version (%d)", (int) packet->ver);
56 if (packet->type != 1) {
57 syslog(LOG_ERR, "Invalid PPPoE type (%d)", (int) packet->type);
61 /* Do some sanity checks on packet */
62 if (len > ETH_DATA_LEN - 6) { /* 6-byte overhead for PPPoE header */
63 syslog(LOG_ERR, "Invalid PPPoE packet length (%u)", len);
67 /* Step through the tags */
68 curTag = packet->payload;
69 while(curTag - packet->payload < len) {
70 /* Alignment is not guaranteed, so do this by hand... */
71 tagType = (((UINT16_t) curTag[0]) << 8) +
73 tagLen = (((UINT16_t) curTag[2]) << 8) +
75 if (tagType == TAG_END_OF_LIST) {
78 if ((curTag - packet->payload) + tagLen + TAG_HDR_SIZE > len) {
79 syslog(LOG_ERR, "Invalid PPPoE tag length (%u)", tagLen);
82 func(tagType, tagLen, curTag+TAG_HDR_SIZE, extra);
83 curTag = curTag + TAG_HDR_SIZE + tagLen;
88 /**********************************************************************
91 * packet -- the PPPoE discovery packet to parse
92 * type -- the type of the tag to look for
93 * tag -- will be filled in with tag contents
95 * A pointer to the tag if one of the specified type is found; NULL
98 * Looks for a specific tag type.
99 ***********************************************************************/
101 findTag(PPPoEPacket *packet, UINT16_t type, PPPoETag *tag)
103 UINT16_t len = ntohs(packet->length);
104 unsigned char *curTag;
105 UINT16_t tagType, tagLen;
107 if (packet->ver != 1) {
108 syslog(LOG_ERR, "Invalid PPPoE version (%d)", (int) packet->ver);
111 if (packet->type != 1) {
112 syslog(LOG_ERR, "Invalid PPPoE type (%d)", (int) packet->type);
116 /* Do some sanity checks on packet */
117 if (len > ETH_DATA_LEN - 6) { /* 6-byte overhead for PPPoE header */
118 syslog(LOG_ERR, "Invalid PPPoE packet length (%u)", len);
122 /* Step through the tags */
123 curTag = packet->payload;
124 while(curTag - packet->payload < len) {
125 /* Alignment is not guaranteed, so do this by hand... */
126 tagType = (((UINT16_t) curTag[0]) << 8) +
127 (UINT16_t) curTag[1];
128 tagLen = (((UINT16_t) curTag[2]) << 8) +
129 (UINT16_t) curTag[3];
130 if (tagType == TAG_END_OF_LIST) {
133 if ((curTag - packet->payload) + tagLen + TAG_HDR_SIZE > len) {
134 syslog(LOG_ERR, "Invalid PPPoE tag length (%u)", tagLen);
137 if (tagType == type) {
138 memcpy(tag, curTag, tagLen + TAG_HDR_SIZE);
141 curTag = curTag + TAG_HDR_SIZE + tagLen;
146 /**********************************************************************
149 * str -- error message
153 * Prints a message to stderr and syslog.
154 ***********************************************************************/
156 printErr(char const *str)
158 fprintf(stderr, "pppoe: %s\n", str);
159 syslog(LOG_ERR, "%s", str);
163 /**********************************************************************
166 * str -- string to copy
168 * A malloc'd copy of str. Exits if malloc fails.
169 ***********************************************************************/
171 strDup(char const *str)
173 char *copy = malloc(strlen(str)+1);
175 rp_fatal("strdup failed");
181 #ifdef PPPOE_STANDALONE
182 /**********************************************************************
183 *%FUNCTION: computeTCPChecksum
185 * ipHdr -- pointer to IP header
186 * tcpHdr -- pointer to TCP header
188 * The computed TCP checksum
189 ***********************************************************************/
191 computeTCPChecksum(unsigned char *ipHdr, unsigned char *tcpHdr)
194 UINT16_t count = ipHdr[2] * 256 + ipHdr[3];
195 unsigned char *addr = tcpHdr;
196 unsigned char pseudoHeader[12];
198 /* Count number of bytes in TCP header and data */
199 count -= (ipHdr[0] & 0x0F) * 4;
201 memcpy(pseudoHeader, ipHdr+12, 8);
203 pseudoHeader[9] = ipHdr[9];
204 pseudoHeader[10] = (count >> 8) & 0xFF;
205 pseudoHeader[11] = (count & 0xFF);
207 /* Checksum the pseudo-header */
208 sum += * (UINT16_t *) pseudoHeader;
209 sum += * ((UINT16_t *) (pseudoHeader+2));
210 sum += * ((UINT16_t *) (pseudoHeader+4));
211 sum += * ((UINT16_t *) (pseudoHeader+6));
212 sum += * ((UINT16_t *) (pseudoHeader+8));
213 sum += * ((UINT16_t *) (pseudoHeader+10));
215 /* Checksum the TCP header and data */
217 sum += * (UINT16_t *) addr;
226 sum = (sum & 0xffff) + (sum >> 16);
228 return (UINT16_t) (~sum & 0xFFFF);
231 /**********************************************************************
234 * packet -- PPPoE session packet
235 * dir -- either "incoming" or "outgoing"
236 * clampMss -- clamp value
240 * Clamps MSS option if TCP SYN flag is set.
241 ***********************************************************************/
243 clampMSS(PPPoEPacket *packet, char const *dir, int clampMss)
245 unsigned char *tcpHdr;
246 unsigned char *ipHdr;
248 unsigned char *endHdr;
249 unsigned char *mssopt = NULL;
254 /* check PPP protocol type */
255 if (packet->payload[0] & 0x01) {
256 /* 8 bit protocol type */
259 if (packet->payload[0] != 0x21) {
260 /* Nope, ignore it */
264 ipHdr = packet->payload + 1;
267 /* 16 bit protocol type */
270 if (packet->payload[0] != 0x00 ||
271 packet->payload[1] != 0x21) {
272 /* Nope, ignore it */
276 ipHdr = packet->payload + 2;
280 /* Is it too short? */
281 len = (int) ntohs(packet->length);
283 /* 20 byte IP header; 20 byte TCP header; at least 1 or 2 byte PPP protocol */
287 /* Verify once more that it's IPv4 */
288 if ((ipHdr[0] & 0xF0) != 0x40) {
292 /* Is it a fragment that's not at the beginning of the packet? */
293 if ((ipHdr[6] & 0x1F) || ipHdr[7]) {
294 /* Yup, don't touch! */
298 if (ipHdr[9] != 0x06) {
302 /* Get start of TCP header */
303 tcpHdr = ipHdr + (ipHdr[0] & 0x0F) * 4;
306 if (!(tcpHdr[13] & 0x02)) {
310 /* Compute and verify TCP checksum -- do not touch a packet with a bad
312 csum = computeTCPChecksum(ipHdr, tcpHdr);
314 syslog(LOG_ERR, "Bad TCP checksum %x", (unsigned int) csum);
316 /* Upper layers will drop it */
320 /* Look for existing MSS option */
321 endHdr = tcpHdr + ((tcpHdr[12] & 0xF0) >> 2);
323 while (opt < endHdr) {
324 if (!*opt) break; /* End of options */
332 /* Something fishy about MSS option length. */
334 "Bogus length for MSS option (%u) from %u.%u.%u.%u",
335 (unsigned int) opt[1],
336 (unsigned int) ipHdr[12],
337 (unsigned int) ipHdr[13],
338 (unsigned int) ipHdr[14],
339 (unsigned int) ipHdr[15]);
346 /* Someone's trying to attack us? */
348 "Bogus TCP option length (%u) from %u.%u.%u.%u",
349 (unsigned int) opt[1],
350 (unsigned int) ipHdr[12],
351 (unsigned int) ipHdr[13],
352 (unsigned int) ipHdr[14],
353 (unsigned int) ipHdr[15]);
359 /* Found existing MSS option? */
363 /* If MSS exists and it's low enough, do nothing */
365 unsigned mss = mssopt[2] * 256 + mssopt[3];
366 if (mss <= clampMss) {
370 mssopt[2] = (((unsigned) clampMss) >> 8) & 0xFF;
371 mssopt[3] = ((unsigned) clampMss) & 0xFF;
373 /* No MSS option. Don't add one; we'll have to use 536. */
377 /* Recompute TCP checksum */
380 csum = computeTCPChecksum(ipHdr, tcpHdr);
381 (* (UINT16_t *) (tcpHdr+16)) = csum;
383 #endif /* PPPOE_STANDALONE */
385 /***********************************************************************
388 * conn -- PPPoE connection
389 * msg -- if non-NULL, extra error message to include in PADT packet.
393 * Sends a PADT packet
394 ***********************************************************************/
396 sendPADT(PPPoEConnection *conn, char const *msg)
399 unsigned char *cursor = packet.payload;
403 /* Do nothing if no session established yet */
404 if (!conn->session) return;
406 /* Do nothing if no discovery socket */
407 if (conn->discoverySocket < 0) return;
409 memcpy(packet.ethHdr.h_dest, conn->peerEth, ETH_ALEN);
410 memcpy(packet.ethHdr.h_source, conn->myEth, ETH_ALEN);
412 packet.ethHdr.h_proto = htons(Eth_PPPOE_Discovery);
415 packet.code = CODE_PADT;
416 packet.session = conn->session;
418 /* Reset Session to zero so there is no possibility of
419 recursive calls to this function by any signal handler */
422 /* If we're using Host-Uniq, copy it over */
423 if (conn->useHostUniq) {
425 pid_t pid = getpid();
426 hostUniq.type = htons(TAG_HOST_UNIQ);
427 hostUniq.length = htons(sizeof(pid));
428 memcpy(hostUniq.payload, &pid, sizeof(pid));
429 memcpy(cursor, &hostUniq, sizeof(pid) + TAG_HDR_SIZE);
430 cursor += sizeof(pid) + TAG_HDR_SIZE;
431 plen += sizeof(pid) + TAG_HDR_SIZE;
434 /* Copy error message */
437 size_t elen = strlen(msg);
438 err.type = htons(TAG_GENERIC_ERROR);
439 err.length = htons(elen);
440 strcpy(err.payload, msg);
441 memcpy(cursor, &err, elen + TAG_HDR_SIZE);
442 cursor += elen + TAG_HDR_SIZE;
443 plen += elen + TAG_HDR_SIZE;
446 /* Copy cookie and relay-ID if needed */
447 if (conn->cookie.type) {
448 CHECK_ROOM(cursor, packet.payload,
449 ntohs(conn->cookie.length) + TAG_HDR_SIZE);
450 memcpy(cursor, &conn->cookie, ntohs(conn->cookie.length) + TAG_HDR_SIZE);
451 cursor += ntohs(conn->cookie.length) + TAG_HDR_SIZE;
452 plen += ntohs(conn->cookie.length) + TAG_HDR_SIZE;
455 if (conn->relayId.type) {
456 CHECK_ROOM(cursor, packet.payload,
457 ntohs(conn->relayId.length) + TAG_HDR_SIZE);
458 memcpy(cursor, &conn->relayId, ntohs(conn->relayId.length) + TAG_HDR_SIZE);
459 cursor += ntohs(conn->relayId.length) + TAG_HDR_SIZE;
460 plen += ntohs(conn->relayId.length) + TAG_HDR_SIZE;
463 packet.length = htons(plen);
464 sendPacket(conn, conn->discoverySocket, &packet, (int) (plen + HDR_SIZE));
465 if (conn->debugFile) {
466 dumpPacket(conn->debugFile, &packet, "SENT");
467 fprintf(conn->debugFile, "\n");
468 fflush(conn->debugFile);
470 syslog(LOG_INFO,"Sent PADT");
473 /**********************************************************************
474 *%FUNCTION: parseLogErrs
479 * extra -- extra user data
483 * Picks error tags out of a packet and logs them.
484 ***********************************************************************/
486 parseLogErrs(UINT16_t type, UINT16_t len, unsigned char *data,
490 case TAG_SERVICE_NAME_ERROR:
491 syslog(LOG_ERR, "PADT: Service-Name-Error: %.*s", (int) len, data);
492 fprintf(stderr, "PADT: Service-Name-Error: %.*s\n", (int) len, data);
494 case TAG_AC_SYSTEM_ERROR:
495 syslog(LOG_ERR, "PADT: System-Error: %.*s", (int) len, data);
496 fprintf(stderr, "PADT: System-Error: %.*s\n", (int) len, data);
498 case TAG_GENERIC_ERROR:
499 syslog(LOG_ERR, "PADT: Generic-Error: %.*s", (int) len, data);
500 fprintf(stderr, "PADT: Generic-Error: %.*s\n", (int) len, data);