]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/rp-pppoe/common.c
a285b486a62df09ed1d3e8e727fe088e7ad48271
[ppp.git] / pppd / plugins / rp-pppoe / common.c
1 /***********************************************************************
2 *
3 * common.c
4 *
5 * Implementation of user-space PPPoE redirector for Linux.
6 *
7 * Common functions used by PPPoE client and server
8 *
9 * Copyright (C) 2000 by Roaring Penguin Software Inc.
10 *
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.
13 *
14 ***********************************************************************/
15
16 static char const RCSID[] =
17 "$Id: common.c,v 1.1 2001/12/14 02:55:20 mostrows Exp $";
18
19 #include "pppoe.h"
20
21 #ifdef HAVE_SYSLOG_H
22 #include <syslog.h>
23 #endif
24
25 #include <string.h>
26 #include <errno.h>
27 #include <stdlib.h>
28
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32
33 /**********************************************************************
34 *%FUNCTION: parsePacket
35 *%ARGUMENTS:
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
39 *%RETURNS:
40 * 0 if everything went well; -1 if there was an error
41 *%DESCRIPTION:
42 * Parses a PPPoE discovery packet, calling "func" for each tag in the packet.
43 * "func" is passed the additional argument "extra".
44 ***********************************************************************/
45 int
46 parsePacket(PPPoEPacket *packet, ParseFunc *func, void *extra)
47 {
48     UINT16_t len = ntohs(packet->length);
49     unsigned char *curTag;
50     UINT16_t tagType, tagLen;
51
52     if (packet->ver != 1) {
53         syslog(LOG_ERR, "Invalid PPPoE version (%d)", (int) packet->ver);
54         return -1;
55     }
56     if (packet->type != 1) {
57         syslog(LOG_ERR, "Invalid PPPoE type (%d)", (int) packet->type);
58         return -1;
59     }
60
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);
64         return -1;
65     }
66
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) +
72             (UINT16_t) curTag[1];
73         tagLen = (((UINT16_t) curTag[2]) << 8) +
74             (UINT16_t) curTag[3];
75         if (tagType == TAG_END_OF_LIST) {
76             return 0;
77         }
78         if ((curTag - packet->payload) + tagLen + TAG_HDR_SIZE > len) {
79             syslog(LOG_ERR, "Invalid PPPoE tag length (%u)", tagLen);
80             return -1;
81         }
82         func(tagType, tagLen, curTag+TAG_HDR_SIZE, extra);
83         curTag = curTag + TAG_HDR_SIZE + tagLen;
84     }
85     return 0;
86 }
87
88 /**********************************************************************
89 *%FUNCTION: findTag
90 *%ARGUMENTS:
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
94 *%RETURNS:
95 * A pointer to the tag if one of the specified type is found; NULL
96 * otherwise. 
97 *%DESCRIPTION:
98 * Looks for a specific tag type.
99 ***********************************************************************/
100 unsigned char *
101 findTag(PPPoEPacket *packet, UINT16_t type, PPPoETag *tag)
102 {
103     UINT16_t len = ntohs(packet->length);
104     unsigned char *curTag;
105     UINT16_t tagType, tagLen;
106
107     if (packet->ver != 1) {
108         syslog(LOG_ERR, "Invalid PPPoE version (%d)", (int) packet->ver);
109         return NULL;
110     }
111     if (packet->type != 1) {
112         syslog(LOG_ERR, "Invalid PPPoE type (%d)", (int) packet->type);
113         return NULL;
114     }
115
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);
119         return NULL;
120     }
121
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) {
131             return NULL;
132         }
133         if ((curTag - packet->payload) + tagLen + TAG_HDR_SIZE > len) {
134             syslog(LOG_ERR, "Invalid PPPoE tag length (%u)", tagLen);
135             return NULL;
136         }
137         if (tagType == type) {
138             memcpy(tag, curTag, tagLen + TAG_HDR_SIZE);
139             return curTag;
140         }
141         curTag = curTag + TAG_HDR_SIZE + tagLen;
142     }
143     return NULL;
144 }
145
146 /**********************************************************************
147 *%FUNCTION: printErr
148 *%ARGUMENTS:
149 * str -- error message
150 *%RETURNS:
151 * Nothing
152 *%DESCRIPTION:
153 * Prints a message to stderr and syslog.
154 ***********************************************************************/
155 void
156 printErr(char const *str)
157 {
158     fprintf(stderr, "pppoe: %s\n", str);
159     syslog(LOG_ERR, "%s", str);
160 }
161
162
163 /**********************************************************************
164 *%FUNCTION: strDup
165 *%ARGUMENTS:
166 * str -- string to copy
167 *%RETURNS:
168 * A malloc'd copy of str.  Exits if malloc fails.
169 ***********************************************************************/
170 char *
171 strDup(char const *str)
172 {
173     char *copy = malloc(strlen(str)+1);
174     if (!copy) {
175         rp_fatal("strdup failed");
176     }
177     strcpy(copy, str);
178     return copy;
179 }
180
181 /**********************************************************************
182 *%FUNCTION: computeTCPChecksum
183 *%ARGUMENTS:
184 * ipHdr -- pointer to IP header
185 * tcpHdr -- pointer to TCP header
186 *%RETURNS:
187 * The computed TCP checksum
188 ***********************************************************************/
189 UINT16_t
190 computeTCPChecksum(unsigned char *ipHdr, unsigned char *tcpHdr)
191 {
192     UINT32_t sum = 0;
193     UINT16_t count = ipHdr[2] * 256 + ipHdr[3];
194     unsigned char *addr = tcpHdr;
195     unsigned char pseudoHeader[12];
196
197     /* Count number of bytes in TCP header and data */
198     count -= (ipHdr[0] & 0x0F) * 4;
199
200     memcpy(pseudoHeader, ipHdr+12, 8);
201     pseudoHeader[8] = 0;
202     pseudoHeader[9] = ipHdr[9];
203     pseudoHeader[10] = (count >> 8) & 0xFF;
204     pseudoHeader[11] = (count & 0xFF);
205
206     /* Checksum the pseudo-header */
207     sum += * (UINT16_t *) pseudoHeader;
208     sum += * ((UINT16_t *) (pseudoHeader+2));
209     sum += * ((UINT16_t *) (pseudoHeader+4));
210     sum += * ((UINT16_t *) (pseudoHeader+6));
211     sum += * ((UINT16_t *) (pseudoHeader+8));
212     sum += * ((UINT16_t *) (pseudoHeader+10));
213
214     /* Checksum the TCP header and data */
215     while (count > 1) {
216         sum += * (UINT16_t *) addr;
217         addr += 2;
218         count -= 2;
219     }
220     if (count > 0) {
221         sum += *addr;
222     }
223
224     while(sum >> 16) {
225         sum = (sum & 0xffff) + (sum >> 16);
226     }
227     return (UINT16_t) (~sum & 0xFFFF);
228 }
229
230 /**********************************************************************
231 *%FUNCTION: clampMSS
232 *%ARGUMENTS:
233 * packet -- PPPoE session packet
234 * dir -- either "incoming" or "outgoing"
235 * clampMss -- clamp value
236 *%RETURNS:
237 * Nothing
238 *%DESCRIPTION:
239 * Clamps MSS option if TCP SYN flag is set.
240 ***********************************************************************/
241 void
242 clampMSS(PPPoEPacket *packet, char const *dir, int clampMss)
243 {
244     unsigned char *tcpHdr;
245     unsigned char *ipHdr;
246     unsigned char *opt;
247     unsigned char *endHdr;
248     unsigned char *mssopt = NULL;
249     UINT16_t csum;
250
251     int len;
252
253     /* Is it IPv4? */
254     if (packet->payload[0] != 0x00 ||
255         packet->payload[1] != 0x21) {
256         /* Nope, ignore it */
257         return;
258     }
259
260     ipHdr = packet->payload + 2;
261
262     /* Is it too short? */
263     len = (int) ntohs(packet->length);
264     if (len < 42) {
265         /* 20 byte IP header; 20 byte TCP header; 2 byte PPP protocol */
266         return;
267     }
268
269     /* Verify once more that it's IPv4 */
270     if ((ipHdr[0] & 0xF0) != 0x40) {
271         return;
272     }
273
274     /* Is it a fragment that's not at the beginning of the packet? */
275     if ((ipHdr[6] & 0x1F) || ipHdr[7]) {
276         /* Yup, don't touch! */
277         return;
278     }
279     /* Is it TCP? */
280     if (ipHdr[9] != 0x06) {
281         return;
282     }
283
284     /* Get start of TCP header */
285     tcpHdr = ipHdr + (ipHdr[0] & 0x0F) * 4;
286
287     /* Is SYN set? */
288     if (!(tcpHdr[13] & 0x02)) {
289         return;
290     }
291
292     /* Compute and verify TCP checksum -- do not touch a packet with a bad
293        checksum */
294     csum = computeTCPChecksum(ipHdr, tcpHdr);
295     if (csum) {
296         syslog(LOG_ERR, "Bad TCP checksum %x", (unsigned int) csum);
297
298         /* Upper layers will drop it */
299         return;
300     }
301
302     /* Look for existing MSS option */
303     endHdr = tcpHdr + ((tcpHdr[12] & 0xF0) >> 2);
304     opt = tcpHdr + 20;
305     while (opt < endHdr) {
306         if (!*opt) break;       /* End of options */
307         switch(*opt) {
308         case 1:
309             opt++;
310             break;
311
312         case 2:
313             if (opt[1] != 4) {
314                 /* Something fishy about MSS option length. */
315                 syslog(LOG_ERR,
316                        "Bogus length for MSS option (%u) from %u.%u.%u.%u",
317                        (unsigned int) opt[1],
318                        (unsigned int) ipHdr[12],
319                        (unsigned int) ipHdr[13],
320                        (unsigned int) ipHdr[14],
321                        (unsigned int) ipHdr[15]);
322                 return;
323             }
324             mssopt = opt;
325             break;
326         default:
327             if (opt[1] < 2) {
328                 /* Someone's trying to attack us? */
329                 syslog(LOG_ERR,
330                        "Bogus TCP option length (%u) from %u.%u.%u.%u",
331                        (unsigned int) opt[1],
332                        (unsigned int) ipHdr[12],
333                        (unsigned int) ipHdr[13],
334                        (unsigned int) ipHdr[14],
335                        (unsigned int) ipHdr[15]);
336                 return;
337             }
338             opt += (opt[1]);
339             break;
340         }
341         /* Found existing MSS option? */
342         if (mssopt) break;
343     }
344
345     /* If MSS exists and it's low enough, do nothing */
346     if (mssopt) {
347         unsigned mss = mssopt[2] * 256 + mssopt[3];
348         if (mss <= clampMss) {
349             return;
350         }
351
352         mssopt[2] = (((unsigned) clampMss) >> 8) & 0xFF;
353         mssopt[3] = ((unsigned) clampMss) & 0xFF;
354     } else {
355         /* No MSS option.  Don't add one; we'll have to use 536. */
356         return;
357     }
358
359     /* Recompute TCP checksum */
360     tcpHdr[16] = 0;
361     tcpHdr[17] = 0;
362     csum = computeTCPChecksum(ipHdr, tcpHdr);
363     (* (UINT16_t *) (tcpHdr+16)) = csum;
364 }
365
366 /***********************************************************************
367 *%FUNCTION: sendPADT
368 *%ARGUMENTS:
369 * conn -- PPPoE connection
370 * msg -- if non-NULL, extra error message to include in PADT packet.
371 *%RETURNS:
372 * Nothing
373 *%DESCRIPTION:
374 * Sends a PADT packet
375 ***********************************************************************/
376 void
377 sendPADT(PPPoEConnection *conn, char const *msg)
378 {
379     PPPoEPacket packet;
380     unsigned char *cursor = packet.payload;
381
382     UINT16_t plen = 0;
383
384     /* Do nothing if no session established yet */
385     if (!conn->session) return;
386
387     /* Do nothing if no discovery socket */
388     if (conn->discoverySocket < 0) return;
389
390     memcpy(packet.ethHdr.h_dest, conn->peerEth, ETH_ALEN);
391     memcpy(packet.ethHdr.h_source, conn->myEth, ETH_ALEN);
392
393     packet.ethHdr.h_proto = htons(Eth_PPPOE_Discovery);
394     packet.ver = 1;
395     packet.type = 1;
396     packet.code = CODE_PADT;
397     packet.session = conn->session;
398
399     /* Reset Session to zero so there is no possibility of
400        recursive calls to this function by any signal handler */
401     conn->session = 0;
402
403     /* If we're using Host-Uniq, copy it over */
404     if (conn->useHostUniq) {
405         PPPoETag hostUniq;
406         pid_t pid = getpid();
407         hostUniq.type = htons(TAG_HOST_UNIQ);
408         hostUniq.length = htons(sizeof(pid));
409         memcpy(hostUniq.payload, &pid, sizeof(pid));
410         memcpy(cursor, &hostUniq, sizeof(pid) + TAG_HDR_SIZE);
411         cursor += sizeof(pid) + TAG_HDR_SIZE;
412         plen += sizeof(pid) + TAG_HDR_SIZE;
413     }
414
415     /* Copy error message */
416     if (msg) {
417         PPPoETag err;
418         size_t elen = strlen(msg);
419         err.type = htons(TAG_GENERIC_ERROR);
420         err.length = htons(elen);
421         strcpy(err.payload, msg);
422         memcpy(cursor, &err, elen + TAG_HDR_SIZE);
423         cursor += elen + TAG_HDR_SIZE;
424         plen += elen + TAG_HDR_SIZE;
425     }
426             
427     /* Copy cookie and relay-ID if needed */
428     if (conn->cookie.type) {
429         CHECK_ROOM(cursor, packet.payload,
430                    ntohs(conn->cookie.length) + TAG_HDR_SIZE);
431         memcpy(cursor, &conn->cookie, ntohs(conn->cookie.length) + TAG_HDR_SIZE);
432         cursor += ntohs(conn->cookie.length) + TAG_HDR_SIZE;
433         plen += ntohs(conn->cookie.length) + TAG_HDR_SIZE;
434     }
435
436     if (conn->relayId.type) {
437         CHECK_ROOM(cursor, packet.payload,
438                    ntohs(conn->relayId.length) + TAG_HDR_SIZE);
439         memcpy(cursor, &conn->relayId, ntohs(conn->relayId.length) + TAG_HDR_SIZE);
440         cursor += ntohs(conn->relayId.length) + TAG_HDR_SIZE;
441         plen += ntohs(conn->relayId.length) + TAG_HDR_SIZE;
442     }
443
444     packet.length = htons(plen);
445     sendPacket(conn, conn->discoverySocket, &packet, (int) (plen + HDR_SIZE));
446     if (conn->debugFile) {
447         dumpPacket(conn->debugFile, &packet, "SENT");
448         fprintf(conn->debugFile, "\n");
449         fflush(conn->debugFile);
450     }
451     syslog(LOG_INFO,"Sent PADT");
452 }
453
454 /**********************************************************************
455 *%FUNCTION: parseLogErrs
456 *%ARGUMENTS:
457 * type -- tag type
458 * len -- tag length
459 * data -- tag data
460 * extra -- extra user data
461 *%RETURNS:
462 * Nothing
463 *%DESCRIPTION:
464 * Picks error tags out of a packet and logs them.
465 ***********************************************************************/
466 void
467 parseLogErrs(UINT16_t type, UINT16_t len, unsigned char *data,
468              void *extra)
469 {
470     switch(type) {
471     case TAG_SERVICE_NAME_ERROR:
472         syslog(LOG_ERR, "PADT: Service-Name-Error: %.*s", (int) len, data);
473         fprintf(stderr, "PADT: Service-Name-Error: %.*s\n", (int) len, data);
474         break;
475     case TAG_AC_SYSTEM_ERROR:
476         syslog(LOG_ERR, "PADT: System-Error: %.*s", (int) len, data);
477         fprintf(stderr, "PADT: System-Error: %.*s\n", (int) len, data);
478         break;
479     case TAG_GENERIC_ERROR:
480         syslog(LOG_ERR, "PADT: Generic-Error: %.*s", (int) len, data);
481         fprintf(stderr, "PADT: Generic-Error: %.*s\n", (int) len, data);
482         break;
483     }
484 }
485