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