]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/rp-pppoe/plugin.c
Added RADIUS suppport.
[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.2 2002/01/22 16:03:05 dfs 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 char pppd_version[] = VERSION;
58
59 /* From sys-linux.c in pppd -- MUST FIX THIS! */
60 extern int new_style_driver;
61
62 static char *service = NULL;
63 static char *acName = NULL;
64 static char *existingSession = NULL;
65
66 static int PPPoEDevnameHook(const char *name);
67 static option_t Options[] = {
68     { "device name", o_wild, (void *) &PPPoEDevnameHook,
69       "PPPoE device name",
70       OPT_DEVNAM | OPT_PRIVFIX | OPT_NOARG  | OPT_A2STRVAL | OPT_STATIC,
71       devnam},
72     { "rp_pppoe_service", o_string, &service,
73       "Desired PPPoE service name" },
74     { "rp_pppoe_ac",      o_string, &acName,
75       "Desired PPPoE access concentrator name" },
76     { "rp_pppoe_sess",    o_string, &existingSession,
77       "Attach to existing session (sessid:macaddr)" },
78     { NULL }
79 };
80 int (*OldDevnameHook)(const char *name) = NULL;
81 static PPPoEConnection *conn = NULL;
82
83 /**********************************************************************
84  * %FUNCTION: PPPOEInitDevice
85  * %ARGUMENTS:
86  * None
87  * %RETURNS:
88  *
89  * %DESCRIPTION:
90  * Initializes PPPoE device.
91  ***********************************************************************/
92 static int
93 PPPOEInitDevice(void)
94 {
95     conn = malloc(sizeof(PPPoEConnection));
96     if (!conn) {
97         fatal("Could not allocate memory for PPPoE session");
98     }
99     memset(conn, 0, sizeof(PPPoEConnection));
100     if (acName) {
101         SET_STRING(conn->acName, acName);
102     }
103     if (service) {
104         SET_STRING(conn->serviceName, acName);
105     }
106     SET_STRING(conn->ifName, devnam);
107     conn->discoverySocket = -1;
108     conn->sessionSocket = -1;
109     conn->useHostUniq = 1;
110     return 1;
111 }
112
113 /**********************************************************************
114  * %FUNCTION: PPPOEConnectDevice
115  * %ARGUMENTS:
116  * None
117  * %RETURNS:
118  * Non-negative if all goes well; -1 otherwise
119  * %DESCRIPTION:
120  * Connects PPPoE device.
121  ***********************************************************************/
122 static int
123 PPPOEConnectDevice(void)
124 {
125     struct sockaddr_pppox sp;
126
127     strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam));
128     if (existingSession) {
129         unsigned int mac[ETH_ALEN];
130         int i, ses;
131         if (sscanf(existingSession, "%d:%x:%x:%x:%x:%x:%x",
132                    &ses, &mac[0], &mac[1], &mac[2],
133                    &mac[3], &mac[4], &mac[5]) != 7) {
134             fatal("Illegal value for rp_pppoe_sess option");
135         }
136         conn->session = htons(ses);
137         for (i=0; i<ETH_ALEN; i++) {
138             conn->peerEth[i] = (unsigned char) mac[i];
139         }
140     } else {
141         discovery(conn);
142         if (conn->discoveryState != STATE_SESSION) {
143             fatal("Unable to complete PPPoE Discovery");
144         }
145     }
146
147     /* Set PPPoE session-number for further consumption */
148     ppp_session_number = ntohs(conn->session);
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
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
170     if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
171                 sizeof(struct sockaddr_pppox)) < 0) {
172         fatal("Failed to connect PPPoE socket: %d %m", errno);
173         return -1;
174     }
175
176     return conn->sessionSocket;
177 }
178
179 static void
180 PPPOESendConfig(int mtu,
181                 u_int32_t asyncmap,
182                 int pcomp,
183                 int accomp)
184 {
185     int sock;
186     struct ifreq ifr;
187
188     if (mtu > MAX_PPPOE_MTU) {
189         warn("Couldn't increase MTU to %d", mtu);
190         mtu = MAX_PPPOE_MTU;
191     }
192     sock = socket(AF_INET, SOCK_DGRAM, 0);
193     if (sock < 0) {
194         fatal("Couldn't create IP socket: %m");
195     }
196     strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
197     ifr.ifr_mtu = mtu;
198     if (ioctl(sock, SIOCSIFMTU, &ifr) < 0) {
199         fatal("ioctl(SIOCSIFMTU): %m");
200     }
201     (void) close (sock);
202 }
203
204
205 static void
206 PPPOERecvConfig(int mru,
207                 u_int32_t asyncmap,
208                 int pcomp,
209                 int accomp)
210 {
211     if (mru > MAX_PPPOE_MTU) {
212         error("Couldn't increase MRU to %d", mru);
213     }
214 }
215
216 /**********************************************************************
217  * %FUNCTION: PPPOEDisconnectDevice
218  * %ARGUMENTS:
219  * None
220  * %RETURNS:
221  * Nothing
222  * %DESCRIPTION:
223  * Disconnects PPPoE device
224  ***********************************************************************/
225 static void
226 PPPOEDisconnectDevice(void)
227 {
228     struct sockaddr_pppox sp;
229
230     sp.sa_family = AF_PPPOX;
231     sp.sa_protocol = PX_PROTO_OE;
232     sp.sa_addr.pppoe.sid = 0;
233     memcpy(sp.sa_addr.pppoe.dev, conn->ifName, IFNAMSIZ);
234     memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN);
235     if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
236                 sizeof(struct sockaddr_pppox)) < 0) {
237         fatal("Failed to disconnect PPPoE socket: %d %m", errno);
238         return;
239     }
240     close(conn->sessionSocket);
241 }
242
243 static void
244 PPPOEDeviceOptions(void)
245 {
246     char buf[256];
247     snprintf(buf, 256, _PATH_ETHOPT "%s",devnam);
248     if(!options_from_file(buf, 0, 0, 1))
249         exit(EXIT_OPTION_ERROR);
250
251 }
252
253 struct channel pppoe_channel;
254
255 /**********************************************************************
256  * %FUNCTION: PPPoEDevnameHook
257  * %ARGUMENTS:
258  * name -- name of device
259  * %RETURNS:
260  * 1 if we will handle this device; 0 otherwise.
261  * %DESCRIPTION:
262  * Checks if name is a valid interface name; if so, returns 1.  Also
263  * sets up devnam (string representation of device).
264  ***********************************************************************/
265 static int
266 PPPoEDevnameHook(const char *name)
267 {
268     int r = 1;
269     int fd;
270     struct ifreq ifr;
271
272     /* Open a socket */
273     if ((fd = socket(PF_PACKET, SOCK_RAW, 0)) < 0) {
274         r = 0;
275     }
276
277     /* Try getting interface index */
278     if (r) {
279         strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
280         if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
281             r = 0;
282         } else {
283             if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
284                 r = 0;
285             } else {
286                 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
287                     error("Interface %s not Ethernet", name);
288                     r=0;
289                 }
290             }
291         }
292     }
293
294     /* Close socket */
295     close(fd);
296     if (r) {
297         strncpy(devnam, name, sizeof(devnam));
298         if (the_channel != &pppoe_channel) {
299
300             the_channel = &pppoe_channel;
301             modem = 0;
302
303             lcp_allowoptions[0].neg_accompression = 0;
304             lcp_wantoptions[0].neg_accompression = 0;
305
306             lcp_allowoptions[0].neg_asyncmap = 0;
307             lcp_wantoptions[0].neg_asyncmap = 0;
308
309             lcp_allowoptions[0].neg_pcompression = 0;
310             lcp_wantoptions[0].neg_pcompression = 0;
311
312             ccp_allowoptions[0].deflate = 0 ;
313             ccp_wantoptions[0].deflate = 0 ;
314
315             ipcp_allowoptions[0].neg_vj=0;
316             ipcp_wantoptions[0].neg_vj=0;
317
318             ccp_allowoptions[0].bsd_compress = 0;
319             ccp_wantoptions[0].bsd_compress = 0;
320
321             PPPOEInitDevice();
322         }
323         return 1;
324     }
325
326     if (OldDevnameHook) r = OldDevnameHook(name);
327     return r;
328 }
329
330 /**********************************************************************
331  * %FUNCTION: plugin_init
332  * %ARGUMENTS:
333  * None
334  * %RETURNS:
335  * Nothing
336  * %DESCRIPTION:
337  * Initializes hooks for pppd plugin
338  ***********************************************************************/
339 void
340 plugin_init(void)
341 {
342     if (!ppp_available() && !new_style_driver) {
343         fatal("Linux kernel does not support PPPoE -- are you running 2.4.x?");
344     }
345
346     add_options(Options);
347
348     info("RP-PPPoE plugin version %s compiled against pppd %s",
349          RP_VERSION, VERSION);
350 }
351
352 /**********************************************************************
353 *%FUNCTION: fatalSys
354 *%ARGUMENTS:
355 * str -- error message
356 *%RETURNS:
357 * Nothing
358 *%DESCRIPTION:
359 * Prints a message plus the errno value to stderr and syslog and exits.
360 ***********************************************************************/
361 void
362 fatalSys(char const *str)
363 {
364     char buf[1024];
365     int i = errno;
366     sprintf(buf, "%.256s: %.256s", str, strerror(i));
367     printErr(buf);
368     sprintf(buf, "RP-PPPoE: %.256s: %.256s", str, strerror(i));
369     sendPADT(conn, buf);
370     exit(1);
371 }
372
373 /**********************************************************************
374 *%FUNCTION: rp_fatal
375 *%ARGUMENTS:
376 * str -- error message
377 *%RETURNS:
378 * Nothing
379 *%DESCRIPTION:
380 * Prints a message to stderr and syslog and exits.
381 ***********************************************************************/
382 void
383 rp_fatal(char const *str)
384 {
385     char buf[1024];
386     printErr(str);
387     sprintf(buf, "RP-PPPoE: %.256s", str);
388     sendPADT(conn, buf);
389     exit(1);
390 }
391 /**********************************************************************
392 *%FUNCTION: sysErr
393 *%ARGUMENTS:
394 * str -- error message
395 *%RETURNS:
396 * Nothing
397 *%DESCRIPTION:
398 * Prints a message plus the errno value to syslog.
399 ***********************************************************************/
400 void
401 sysErr(char const *str)
402 {
403     rp_fatal(str);
404 }
405
406
407 struct channel pppoe_channel = {
408     options: Options,
409     process_extra_options: &PPPOEDeviceOptions,
410     check_options: NULL,
411     connect: &PPPOEConnectDevice,
412     disconnect: &PPPOEDisconnectDevice,
413     establish_ppp: &generic_establish_ppp,
414     disestablish_ppp: &generic_disestablish_ppp,
415     send_config: &PPPOESendConfig,
416     recv_config: &PPPOERecvConfig,
417     close: NULL,
418     cleanup: NULL
419 };