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