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