]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/rp-pppoe/plugin.c
Switch to using RoaringPenguin-based PPPOE plugin
[ppp.git] / pppd / plugins / rp-pppoe / plugin.c
1 /***********************************************************************
2 *
3 * plugin.c
4 *
5 * pppd plugin for kernel-mode PPPoE on Linux
6 *
7 * Copyright (C) 2001 by Roaring Penguin Software Inc., Michal Ostrowski
8 * and Jamal Hadi Salim.
9 *
10 * Much code and many ideas derived from pppoe plugin by Michal
11 * Ostrowski and Jamal Hadi Salim, which carries this copyright:
12 *
13 * Copyright 2000 Michal Ostrowski <mostrows@styx.uwaterloo.ca>,
14 *                Jamal Hadi Salim <hadi@cyberus.ca>
15 * Borrows heavily from the PPPoATM plugin by Mitchell Blank Jr.,
16 * which is based in part on work from Jens Axboe and Paul Mackerras.
17 *
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation; either version
21 * 2 of the License, or (at your option) any later version.
22 ***********************************************************************/
23
24 static char const RCSID[] =
25 "$Id: plugin.c,v 1.1 2001/12/14 02:55:20 mostrows Exp $";
26
27 #define _GNU_SOURCE 1
28 #include "pppoe.h"
29
30 #include "pppd/pppd.h"
31 #include "pppd/fsm.h"
32 #include "pppd/lcp.h"
33 #include "pppd/ipcp.h"
34 #include "pppd/ccp.h"
35 #include "pppd/pathnames.h"
36
37 #include <linux/types.h>
38 #include <syslog.h>
39 #include <sys/ioctl.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <sys/stat.h>
43 #include <string.h>
44 #include <stdlib.h>
45 #include <errno.h>
46 #include <unistd.h>
47 #include <fcntl.h>
48 #include <signal.h>
49 #include <net/ethernet.h>
50 #include <net/if_arp.h>
51 #include "ppp_defs.h"
52 #include "if_ppp.h"
53 #include "if_pppox.h"
54
55 #define _PATH_ETHOPT         _ROOT_PATH "/etc/ppp/options."
56
57 /* From sys-linux.c in pppd -- MUST FIX THIS! */
58 extern int new_style_driver;
59
60 static char *service = NULL;
61 static char *acName = NULL;
62 static char *existingSession = NULL;
63
64 static int PPPoEDevnameHook(const char *name);
65 static option_t Options[] = {
66     { "device name", o_wild, (void *) &PPPoEDevnameHook,
67       "PPPoE device name",
68       OPT_DEVNAM | OPT_PRIVFIX | OPT_NOARG  | OPT_A2STRVAL | OPT_STATIC,
69       devnam},
70     { "rp_pppoe_service", o_string, &service,
71       "Desired PPPoE service name" },
72     { "rp_pppoe_ac",      o_string, &acName,
73       "Desired PPPoE access concentrator name" },
74     { "rp_pppoe_sess",    o_string, &existingSession,
75       "Attach to existing session (sessid:macaddr)" },
76     { NULL }
77 };
78 int (*OldDevnameHook)(const char *name) = NULL;
79 static PPPoEConnection *conn = NULL;
80
81 /**********************************************************************
82  * %FUNCTION: PPPOEInitDevice
83  * %ARGUMENTS:
84  * None
85  * %RETURNS:
86  *
87  * %DESCRIPTION:
88  * Initializes PPPoE device.
89  ***********************************************************************/
90 static int
91 PPPOEInitDevice(void)
92 {
93     conn = malloc(sizeof(PPPoEConnection));
94     if (!conn) {
95         fatal("Could not allocate memory for PPPoE session");
96     }
97     memset(conn, 0, sizeof(PPPoEConnection));
98     if (acName) {
99         SET_STRING(conn->acName, acName);
100     }
101     if (service) {
102         SET_STRING(conn->serviceName, acName);
103     }
104     SET_STRING(conn->ifName, devnam);
105     conn->discoverySocket = -1;
106     conn->sessionSocket = -1;
107     conn->useHostUniq = 1;
108     return 1;
109 }
110
111 /**********************************************************************
112  * %FUNCTION: PPPOEConnectDevice
113  * %ARGUMENTS:
114  * None
115  * %RETURNS:
116  * Non-negative if all goes well; -1 otherwise
117  * %DESCRIPTION:
118  * Connects PPPoE device.
119  ***********************************************************************/
120 static int
121 PPPOEConnectDevice(void)
122 {
123     struct sockaddr_pppox sp;
124
125     strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam));
126     if (existingSession) {
127         unsigned int mac[ETH_ALEN];
128         int i, ses;
129         if (sscanf(existingSession, "%d:%x:%x:%x:%x:%x:%x",
130                    &ses, &mac[0], &mac[1], &mac[2],
131                    &mac[3], &mac[4], &mac[5]) != 7) {
132             fatal("Illegal value for rp_pppoe_sess option");
133         }
134         conn->session = htons(ses);
135         for (i=0; i<ETH_ALEN; i++) {
136             conn->peerEth[i] = (unsigned char) mac[i];
137         }
138     } else {
139         discovery(conn);
140         if (conn->discoveryState != STATE_SESSION) {
141             fatal("Unable to complete PPPoE Discovery");
142         }
143     }
144
145 #ifdef HAVE_LICENSE
146     /* Set PPPoE session-number for further consumption */
147     pppd_pppoe_session = ntohs(conn->session);
148 #endif
149
150     /* Make the session socket */
151     conn->sessionSocket = socket(AF_PPPOX, SOCK_STREAM, PX_PROTO_OE);
152     if (conn->sessionSocket < 0) {
153         fatal("Failed to create PPPoE socket: %m");
154     }
155     sp.sa_family = AF_PPPOX;
156     sp.sa_protocol = PX_PROTO_OE;
157     sp.sa_addr.pppoe.sid = conn->session;
158     memcpy(sp.sa_addr.pppoe.dev, conn->ifName, IFNAMSIZ);
159     memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN);
160 #ifdef HAVE_LICENSE
161     /* Set remote_number for ServPoET */
162     sprintf(remote_number, "%02X:%02X:%02X:%02X:%02X:%02X",
163             (unsigned) conn->peerEth[0],
164             (unsigned) conn->peerEth[1],
165             (unsigned) conn->peerEth[2],
166             (unsigned) conn->peerEth[3],
167             (unsigned) conn->peerEth[4],
168             (unsigned) conn->peerEth[5]);
169 #endif
170
171     if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
172                 sizeof(struct sockaddr_pppox)) < 0) {
173         fatal("Failed to connect PPPoE socket: %d %m", errno);
174         return -1;
175     }
176
177     return conn->sessionSocket;
178 }
179
180 static void
181 PPPOESendConfig(int mtu,
182                 u_int32_t asyncmap,
183                 int pcomp,
184                 int accomp)
185 {
186     int sock;
187     struct ifreq ifr;
188
189     if (mtu > MAX_PPPOE_MTU) {
190         warn("Couldn't increase MTU to %d", mtu);
191         mtu = MAX_PPPOE_MTU;
192     }
193     sock = socket(AF_INET, SOCK_DGRAM, 0);
194     if (sock < 0) {
195         fatal("Couldn't create IP socket: %m");
196     }
197     strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
198     ifr.ifr_mtu = mtu;
199     if (ioctl(sock, SIOCSIFMTU, &ifr) < 0) {
200         fatal("ioctl(SIOCSIFMTU): %m");
201     }
202     (void) close (sock);
203 }
204
205
206 static void
207 PPPOERecvConfig(int mru,
208                 u_int32_t asyncmap,
209                 int pcomp,
210                 int accomp)
211 {
212     if (mru > MAX_PPPOE_MTU) {
213         error("Couldn't increase MRU to %d", mru);
214     }
215 }
216
217 /**********************************************************************
218  * %FUNCTION: PPPOEDisconnectDevice
219  * %ARGUMENTS:
220  * None
221  * %RETURNS:
222  * Nothing
223  * %DESCRIPTION:
224  * Disconnects PPPoE device
225  ***********************************************************************/
226 static void
227 PPPOEDisconnectDevice(void)
228 {
229     struct sockaddr_pppox sp;
230
231     sp.sa_family = AF_PPPOX;
232     sp.sa_protocol = PX_PROTO_OE;
233     sp.sa_addr.pppoe.sid = 0;
234     memcpy(sp.sa_addr.pppoe.dev, conn->ifName, IFNAMSIZ);
235     memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN);
236     if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
237                 sizeof(struct sockaddr_pppox)) < 0) {
238         fatal("Failed to disconnect PPPoE socket: %d %m", errno);
239         return;
240     }
241     close(conn->sessionSocket);
242 }
243
244 static void
245 PPPOEDeviceOptions(void)
246 {
247     char buf[256];
248     snprintf(buf, 256, _PATH_ETHOPT "%s",devnam);
249     if(!options_from_file(buf, 0, 0, 1))
250         exit(EXIT_OPTION_ERROR);
251
252 }
253
254 struct channel pppoe_channel;
255
256 /**********************************************************************
257  * %FUNCTION: PPPoEDevnameHook
258  * %ARGUMENTS:
259  * name -- name of device
260  * %RETURNS:
261  * 1 if we will handle this device; 0 otherwise.
262  * %DESCRIPTION:
263  * Checks if name is a valid interface name; if so, returns 1.  Also
264  * sets up devnam (string representation of device).
265  ***********************************************************************/
266 static int
267 PPPoEDevnameHook(const char *name)
268 {
269     int r = 1;
270     int fd;
271     struct ifreq ifr;
272
273     /* Open a socket */
274     if ((fd = socket(PF_PACKET, SOCK_RAW, 0)) < 0) {
275         r = 0;
276     }
277
278     /* Try getting interface index */
279     if (r) {
280         strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
281         if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
282             r = 0;
283         } else {
284             if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
285                 r = 0;
286             } else {
287                 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
288                     error("Interface %s not Ethernet", name);
289                     r=0;
290                 }
291             }
292         }
293     }
294
295     /* Close socket */
296     close(fd);
297     if (r) {
298         strncpy(devnam, name, sizeof(devnam));
299         if (the_channel != &pppoe_channel) {
300
301             the_channel = &pppoe_channel;
302             modem = 0;
303
304             lcp_allowoptions[0].neg_accompression = 0;
305             lcp_wantoptions[0].neg_accompression = 0;
306
307             lcp_allowoptions[0].neg_asyncmap = 0;
308             lcp_wantoptions[0].neg_asyncmap = 0;
309
310             lcp_allowoptions[0].neg_pcompression = 0;
311             lcp_wantoptions[0].neg_pcompression = 0;
312
313             ccp_allowoptions[0].deflate = 0 ;
314             ccp_wantoptions[0].deflate = 0 ;
315
316             ipcp_allowoptions[0].neg_vj=0;
317             ipcp_wantoptions[0].neg_vj=0;
318
319             ccp_allowoptions[0].bsd_compress = 0;
320             ccp_wantoptions[0].bsd_compress = 0;
321
322             PPPOEInitDevice();
323         }
324         return 1;
325     }
326
327     if (OldDevnameHook) r = OldDevnameHook(name);
328     return r;
329 }
330
331 /**********************************************************************
332  * %FUNCTION: plugin_init
333  * %ARGUMENTS:
334  * None
335  * %RETURNS:
336  * Nothing
337  * %DESCRIPTION:
338  * Initializes hooks for pppd plugin
339  ***********************************************************************/
340 void
341 plugin_init(void)
342 {
343     if (!ppp_available() && !new_style_driver) {
344         fatal("Linux kernel does not support PPPoE -- are you running 2.4.x?");
345     }
346
347     add_options(Options);
348
349     info("RP-PPPoE plugin version %s compiled against pppd %s",
350          RP_VERSION, VERSION);
351 }
352
353 /**********************************************************************
354 *%FUNCTION: fatalSys
355 *%ARGUMENTS:
356 * str -- error message
357 *%RETURNS:
358 * Nothing
359 *%DESCRIPTION:
360 * Prints a message plus the errno value to stderr and syslog and exits.
361 ***********************************************************************/
362 void
363 fatalSys(char const *str)
364 {
365     char buf[1024];
366     int i = errno;
367     sprintf(buf, "%.256s: %.256s", str, strerror(i));
368     printErr(buf);
369     sprintf(buf, "RP-PPPoE: %.256s: %.256s", str, strerror(i));
370     sendPADT(conn, buf);
371     exit(1);
372 }
373
374 /**********************************************************************
375 *%FUNCTION: rp_fatal
376 *%ARGUMENTS:
377 * str -- error message
378 *%RETURNS:
379 * Nothing
380 *%DESCRIPTION:
381 * Prints a message to stderr and syslog and exits.
382 ***********************************************************************/
383 void
384 rp_fatal(char const *str)
385 {
386     char buf[1024];
387     printErr(str);
388     sprintf(buf, "RP-PPPoE: %.256s", str);
389     sendPADT(conn, buf);
390     exit(1);
391 }
392 /**********************************************************************
393 *%FUNCTION: sysErr
394 *%ARGUMENTS:
395 * str -- error message
396 *%RETURNS:
397 * Nothing
398 *%DESCRIPTION:
399 * Prints a message plus the errno value to syslog.
400 ***********************************************************************/
401 void
402 sysErr(char const *str)
403 {
404     rp_fatal(str);
405 }
406
407
408 struct channel pppoe_channel = {
409     options: Options,
410     process_extra_options: &PPPOEDeviceOptions,
411     check_options: NULL,
412     connect: &PPPOEConnectDevice,
413     disconnect: &PPPOEDisconnectDevice,
414     establish_ppp: &generic_establish_ppp,
415     disestablish_ppp: &generic_disestablish_ppp,
416     send_config: &PPPOESendConfig,
417     recv_config: &PPPOERecvConfig,
418     close: NULL,
419     cleanup: NULL
420 };