]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/pppoe/common.c
Merge branch 'pppoe-discovery' of https://github.com/pali/ppp
[ppp.git] / pppd / plugins / 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.3 2008/06/09 08:34:23 paulus Exp $";
18
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #define _GNU_SOURCE 1
24 #include "pppoe.h"
25 #include "pppd/pppd.h"
26
27 #include <string.h>
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <syslog.h>     /* for LOG_DEBUG */
31
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35
36 /**********************************************************************
37 *%FUNCTION: parsePacket
38 *%ARGUMENTS:
39 * packet -- the PPPoE discovery packet to parse
40 * func -- function called for each tag in the packet
41 * extra -- an opaque data pointer supplied to parsing function
42 *%RETURNS:
43 * 0 if everything went well; -1 if there was an error
44 *%DESCRIPTION:
45 * Parses a PPPoE discovery packet, calling "func" for each tag in the packet.
46 * "func" is passed the additional argument "extra".
47 ***********************************************************************/
48 int
49 parsePacket(PPPoEPacket *packet, ParseFunc *func, void *extra)
50 {
51     UINT16_t len = ntohs(packet->length);
52     unsigned char *curTag;
53     UINT16_t tagType, tagLen;
54
55     if (PPPOE_VER(packet->vertype) != 1) {
56         error("Invalid PPPoE version (%d)", PPPOE_VER(packet->vertype));
57         return -1;
58     }
59     if (PPPOE_TYPE(packet->vertype) != 1) {
60         error("Invalid PPPoE type (%d)", PPPOE_TYPE(packet->vertype));
61         return -1;
62     }
63
64     /* Do some sanity checks on packet */
65     if (len > ETH_JUMBO_LEN - PPPOE_OVERHEAD) { /* 6-byte overhead for PPPoE header */
66         error("Invalid PPPoE packet length (%u)", len);
67         return -1;
68     }
69
70     /* Step through the tags */
71     curTag = packet->payload;
72     while (curTag - packet->payload + TAG_HDR_SIZE <= len) {
73         /* Alignment is not guaranteed, so do this by hand... */
74         tagType = (curTag[0] << 8) + curTag[1];
75         tagLen = (curTag[2] << 8) + curTag[3];
76         if (tagType == TAG_END_OF_LIST) {
77             return 0;
78         }
79         if ((curTag - packet->payload) + tagLen + TAG_HDR_SIZE > len) {
80             error("Invalid PPPoE tag length (%u)", tagLen);
81             return -1;
82         }
83         func(tagType, tagLen, curTag+TAG_HDR_SIZE, extra);
84         curTag = curTag + TAG_HDR_SIZE + tagLen;
85     }
86     return 0;
87 }
88
89 /***********************************************************************
90 *%FUNCTION: sendPADT
91 *%ARGUMENTS:
92 * conn -- PPPoE connection
93 * msg -- if non-NULL, extra error message to include in PADT packet.
94 *%RETURNS:
95 * Nothing
96 *%DESCRIPTION:
97 * Sends a PADT packet
98 ***********************************************************************/
99 void
100 sendPADT(PPPoEConnection *conn, char const *msg)
101 {
102     PPPoEPacket packet;
103     unsigned char *cursor = packet.payload;
104
105     UINT16_t plen = 0;
106
107     /* Do nothing if no session established yet */
108     if (!conn->session) return;
109
110     /* Do nothing if no discovery socket */
111     if (conn->discoverySocket < 0) return;
112
113     memcpy(packet.ethHdr.h_dest, conn->peerEth, ETH_ALEN);
114     memcpy(packet.ethHdr.h_source, conn->myEth, ETH_ALEN);
115
116     packet.ethHdr.h_proto = htons(Eth_PPPOE_Discovery);
117     packet.vertype = PPPOE_VER_TYPE(1, 1);
118     packet.code = CODE_PADT;
119     packet.session = conn->session;
120
121     /* Reset Session to zero so there is no possibility of
122        recursive calls to this function by any signal handler */
123     conn->session = 0;
124
125     /* If we're using Host-Uniq, copy it over */
126     if (conn->hostUniq.length) {
127         int len = ntohs(conn->hostUniq.length);
128         memcpy(cursor, &conn->hostUniq, len + TAG_HDR_SIZE);
129         cursor += len + TAG_HDR_SIZE;
130         plen += len + TAG_HDR_SIZE;
131     }
132
133     /* Copy error message */
134     if (msg) {
135         PPPoETag err;
136         size_t elen = strlen(msg);
137         err.type = htons(TAG_GENERIC_ERROR);
138         err.length = htons(elen);
139         strcpy((char*) err.payload, msg);
140         memcpy(cursor, &err, elen + TAG_HDR_SIZE);
141         cursor += elen + TAG_HDR_SIZE;
142         plen += elen + TAG_HDR_SIZE;
143     }
144
145     /* Copy cookie and relay-ID if needed */
146     if (conn->cookie.type) {
147         CHECK_ROOM(cursor, packet.payload,
148                    ntohs(conn->cookie.length) + TAG_HDR_SIZE);
149         memcpy(cursor, &conn->cookie, ntohs(conn->cookie.length) + TAG_HDR_SIZE);
150         cursor += ntohs(conn->cookie.length) + TAG_HDR_SIZE;
151         plen += ntohs(conn->cookie.length) + TAG_HDR_SIZE;
152     }
153
154     if (conn->relayId.type) {
155         CHECK_ROOM(cursor, packet.payload,
156                    ntohs(conn->relayId.length) + TAG_HDR_SIZE);
157         memcpy(cursor, &conn->relayId, ntohs(conn->relayId.length) + TAG_HDR_SIZE);
158         cursor += ntohs(conn->relayId.length) + TAG_HDR_SIZE;
159         plen += ntohs(conn->relayId.length) + TAG_HDR_SIZE;
160     }
161
162     packet.length = htons(plen);
163     sendPacket(conn, conn->discoverySocket, &packet, (int) (plen + HDR_SIZE));
164     info("Sent PADT");
165 }
166
167 static void
168 pppoe_printpkt_hex(void (*printer)(void *, char *, ...), void *arg, unsigned char const *buf, int len)
169 {
170     int i;
171     int base;
172
173     /* do NOT dump PAP packets */
174     if (len >= 2 && buf[0] == 0xC0 && buf[1] == 0x23) {
175         printer(arg, "(PAP Authentication Frame -- Contents not dumped)\n");
176         return;
177     }
178
179     for (base=0; base<len; base += 16) {
180         for (i=base; i<base+16; i++) {
181             if (i < len) {
182                 printer(arg, "%02x ", (unsigned) buf[i]);
183             } else {
184                 printer(arg, "   ");
185             }
186         }
187         printer(arg, "  ");
188         for (i=base; i<base+16; i++) {
189             if (i < len) {
190                 if (isprint(buf[i])) {
191                     printer(arg, "%c", buf[i]);
192                 } else {
193                     printer(arg, ".");
194                 }
195             } else {
196                 break;
197             }
198         }
199         printer(arg, "\n");
200     }
201 }
202
203 #define EH(x)   (x)[0], (x)[1], (x)[2], (x)[3], (x)[4], (x)[5]
204
205 /* Print out a PPPOE packet for debugging */
206 void pppoe_printpkt(PPPoEPacket *packet,
207                     void (*printer)(void *, char *, ...), void *arg)
208 {
209     int len = ntohs(packet->length);
210     int i, j, tag, tlen, text;
211
212     switch (ntohs(packet->ethHdr.h_proto)) {
213     case ETH_PPPOE_DISCOVERY:
214         printer(arg, "PPPOE Discovery V%dT%d ", PPPOE_VER(packet->vertype),
215                 PPPOE_TYPE(packet->vertype));
216         switch (packet->code) {
217         case CODE_PADI:
218             printer(arg, "PADI");
219             break;
220         case CODE_PADO:
221             printer(arg, "PADO");
222             break;
223         case CODE_PADR:
224             printer(arg, "PADR");
225             break;
226         case CODE_PADS:
227             printer(arg, "PADS");
228             break;
229         case CODE_PADT:
230             printer(arg, "PADT");
231             break;
232         default:
233             printer(arg, "unknown code %x", packet->code);
234         }
235         printer(arg, " session 0x%x length %d\n", ntohs(packet->session), len);
236         break;
237     case ETH_PPPOE_SESSION:
238         printer(arg, "PPPOE Session V%dT%d", PPPOE_VER(packet->vertype),
239                 PPPOE_TYPE(packet->vertype));
240         printer(arg, " code 0x%x session 0x%x length %d\n", packet->code,
241                 ntohs(packet->session), len);
242         break;
243     default:
244         printer(arg, "Unknown ethernet frame with proto = 0x%x\n",
245                 ntohs(packet->ethHdr.h_proto));
246     }
247
248     printer(arg, " dst %02x:%02x:%02x:%02x:%02x:%02x ", EH(packet->ethHdr.h_dest));
249     printer(arg, " src %02x:%02x:%02x:%02x:%02x:%02x\n", EH(packet->ethHdr.h_source));
250     if (pppoe_verbose >= 2)
251         pppoe_printpkt_hex(printer, arg, packet->payload, ntohs(packet->length));
252     if (ntohs(packet->ethHdr.h_proto) != ETH_PPPOE_DISCOVERY)
253         return;
254
255     for (i = 0; i + TAG_HDR_SIZE <= len; i += tlen) {
256         tag = (packet->payload[i] << 8) + packet->payload[i+1];
257         tlen = (packet->payload[i+2] << 8) + packet->payload[i+3];
258         if (i + tlen + TAG_HDR_SIZE > len)
259             break;
260         text = 0;
261         i += TAG_HDR_SIZE;
262         printer(arg, " [");
263         switch (tag) {
264         case TAG_END_OF_LIST:
265             printer(arg, "end-of-list");
266             break;
267         case TAG_SERVICE_NAME:
268             printer(arg, "service-name");
269             text = 1;
270             break;
271         case TAG_AC_NAME:
272             printer(arg, "AC-name");
273             text = 1;
274             break;
275         case TAG_HOST_UNIQ:
276             printer(arg, "host-uniq");
277             break;
278         case TAG_AC_COOKIE:
279             printer(arg, "AC-cookie");
280             break;
281         case TAG_VENDOR_SPECIFIC:
282             printer(arg, "vendor-specific");
283             break;
284         case TAG_RELAY_SESSION_ID:
285             printer(arg, "relay-session-id");
286             break;
287         case TAG_PPP_MAX_PAYLOAD:
288             printer(arg, "PPP-max-payload");
289             break;
290         case TAG_SERVICE_NAME_ERROR:
291             printer(arg, "service-name-error");
292             text = 1;
293             break;
294         case TAG_AC_SYSTEM_ERROR:
295             printer(arg, "AC-system-error");
296             text = 1;
297             break;
298         case TAG_GENERIC_ERROR:
299             printer(arg, "generic-error");
300             text = 1;
301             break;
302         default:
303             printer(arg, "unknown tag 0x%x", tag);
304         }
305         if (tlen) {
306             if (text)
307                 printer(arg, " %.*s", tlen, &packet->payload[i]);
308             else {
309                 for (j = 0; j < tlen && j < 32; j++)
310                     printer(arg, " %02x", (unsigned) *(&packet->payload[i]+j));
311                 if (j < tlen)
312                     printer(arg, "... (length %d)", tlen);
313             }
314         }
315         printer(arg, "]");
316     }
317     printer(arg, "\n");
318 }
319
320 void pppoe_log_packet(const char *prefix, PPPoEPacket *packet)
321 {
322     init_pr_log(prefix, LOG_DEBUG);
323     pppoe_printpkt(packet, pr_log, NULL);
324     end_pr_log();
325 }