]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/rp-pppoe/plugin.c
93c0906119d7240fd60ba483768c3654ef642f5f
[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
25 static char const RCSID[] =
26 "$Id: plugin.c,v 1.17 2008/06/15 04:35:50 paulus Exp $";
27
28 #define _GNU_SOURCE 1
29 #include "pppoe.h"
30
31 #include "pppd/pppd.h"
32 #include "pppd/fsm.h"
33 #include "pppd/lcp.h"
34 #include "pppd/ipcp.h"
35 #include "pppd/ccp.h"
36 /* #include "pppd/pathnames.h" */
37
38 #include <linux/types.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/if_arp.h>
50 #include <linux/ppp_defs.h>
51 #include <linux/if_pppox.h>
52
53 #ifndef _ROOT_PATH
54 #define _ROOT_PATH ""
55 #endif
56
57 #define _PATH_ETHOPT         _ROOT_PATH "/etc/ppp/options."
58
59 char pppd_version[] = VERSION;
60
61 /* From sys-linux.c in pppd -- MUST FIX THIS! */
62 extern int new_style_driver;
63
64 char *pppd_pppoe_service = NULL;
65 static char *acName = NULL;
66 static char *existingSession = NULL;
67 static int printACNames = 0;
68 static char *pppoe_reqd_mac = NULL;
69 unsigned char pppoe_reqd_mac_addr[6];
70 static char *host_uniq;
71 static int pppoe_padi_timeout = PADI_TIMEOUT;
72 static int pppoe_padi_attempts = MAX_PADI_ATTEMPTS;
73
74 static int PPPoEDevnameHook(char *cmd, char **argv, int doit);
75 static option_t Options[] = {
76     { "device name", o_wild, (void *) &PPPoEDevnameHook,
77       "PPPoE device name",
78       OPT_DEVNAM | OPT_PRIVFIX | OPT_NOARG  | OPT_A2STRVAL | OPT_STATIC,
79       devnam},
80     { "rp_pppoe_service", o_string, &pppd_pppoe_service,
81       "Desired PPPoE service name" },
82     { "rp_pppoe_ac",      o_string, &acName,
83       "Desired PPPoE access concentrator name" },
84     { "rp_pppoe_sess",    o_string, &existingSession,
85       "Attach to existing session (sessid:macaddr)" },
86     { "rp_pppoe_verbose", o_int, &printACNames,
87       "Be verbose about discovered access concentrators"},
88     { "pppoe-mac", o_string, &pppoe_reqd_mac,
89       "Only connect to specified MAC address" },
90     { "host-uniq", o_string, &host_uniq,
91       "Set the Host-Uniq to the supplied hex string" },
92     { "pppoe-padi-timeout", o_int, &pppoe_padi_timeout,
93       "Initial timeout for discovery packets in seconds" },
94     { "pppoe-padi-attempts", o_int, &pppoe_padi_attempts,
95       "Number of discovery attempts" },
96     { NULL }
97 };
98 int (*OldDevnameHook)(char *cmd, char **argv, int doit) = NULL;
99 static PPPoEConnection *conn = NULL;
100
101 /**********************************************************************
102  * %FUNCTION: PPPOEInitDevice
103  * %ARGUMENTS:
104  * None
105  * %RETURNS:
106  *
107  * %DESCRIPTION:
108  * Initializes PPPoE device.
109  ***********************************************************************/
110 static int
111 PPPOEInitDevice(void)
112 {
113     conn = malloc(sizeof(PPPoEConnection));
114     if (!conn) {
115         novm("PPPoE session data");
116     }
117     memset(conn, 0, sizeof(PPPoEConnection));
118     conn->ifName = devnam;
119     conn->discoverySocket = -1;
120     conn->sessionSocket = -1;
121     conn->printACNames = printACNames;
122     conn->discoveryTimeout = pppoe_padi_timeout;
123     conn->discoveryAttempts = pppoe_padi_attempts;
124     return 1;
125 }
126
127 /**********************************************************************
128  * %FUNCTION: PPPOEConnectDevice
129  * %ARGUMENTS:
130  * None
131  * %RETURNS:
132  * Non-negative if all goes well; -1 otherwise
133  * %DESCRIPTION:
134  * Connects PPPoE device.
135  ***********************************************************************/
136 static int
137 PPPOEConnectDevice(void)
138 {
139     struct sockaddr_pppox sp;
140     struct ifreq ifr;
141     int s;
142
143     /* Open session socket before discovery phase, to avoid losing session */
144     /* packets sent by peer just after PADS packet (noted on some Cisco    */
145     /* server equipment).                                                  */
146     /* Opening this socket just before waitForPADS in the discovery()      */
147     /* function would be more appropriate, but it would mess-up the code   */
148     conn->sessionSocket = socket(AF_PPPOX, SOCK_STREAM, PX_PROTO_OE);
149     if (conn->sessionSocket < 0) {
150         error("Failed to create PPPoE socket: %m");
151         return -1;
152     }
153
154     /* Restore configuration */
155     lcp_allowoptions[0].mru = conn->mtu;
156     lcp_wantoptions[0].mru = conn->mru;
157
158     /* Update maximum MRU */
159     s = socket(AF_INET, SOCK_DGRAM, 0);
160     if (s < 0) {
161         error("Can't get MTU for %s: %m", conn->ifName);
162         goto errout;
163     }
164     strlcpy(ifr.ifr_name, conn->ifName, sizeof(ifr.ifr_name));
165     if (ioctl(s, SIOCGIFMTU, &ifr) < 0) {
166         error("Can't get MTU for %s: %m", conn->ifName);
167         close(s);
168         goto errout;
169     }
170     close(s);
171
172     if (lcp_allowoptions[0].mru > ifr.ifr_mtu - TOTAL_OVERHEAD)
173         lcp_allowoptions[0].mru = ifr.ifr_mtu - TOTAL_OVERHEAD;
174     if (lcp_wantoptions[0].mru > ifr.ifr_mtu - TOTAL_OVERHEAD)
175         lcp_wantoptions[0].mru = ifr.ifr_mtu - TOTAL_OVERHEAD;
176
177     if (host_uniq) {
178         if (!parseHostUniq(host_uniq, &conn->hostUniq))
179             fatal("Illegal value for host-uniq option");
180     } else {
181         /* if a custom host-uniq is not supplied, use our PID */
182         pid_t pid = getpid();
183         conn->hostUniq.type = htons(TAG_HOST_UNIQ);
184         conn->hostUniq.length = htons(sizeof(pid));
185         memcpy(conn->hostUniq.payload, &pid, sizeof(pid));
186     }
187
188     conn->acName = acName;
189     conn->serviceName = pppd_pppoe_service;
190     strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam));
191     if (existingSession) {
192         unsigned int mac[ETH_ALEN];
193         int i, ses;
194         if (sscanf(existingSession, "%d:%x:%x:%x:%x:%x:%x",
195                    &ses, &mac[0], &mac[1], &mac[2],
196                    &mac[3], &mac[4], &mac[5]) != 7) {
197             fatal("Illegal value for rp_pppoe_sess option");
198         }
199         conn->session = htons(ses);
200         for (i=0; i<ETH_ALEN; i++) {
201             conn->peerEth[i] = (unsigned char) mac[i];
202         }
203     } else {
204         conn->discoverySocket =
205             openInterface(conn->ifName, Eth_PPPOE_Discovery, conn->myEth);
206         discovery(conn);
207         if (conn->discoveryState != STATE_SESSION) {
208             error("Unable to complete PPPoE Discovery");
209             goto errout;
210         }
211     }
212
213     /* Set PPPoE session-number for further consumption */
214     ppp_session_number = ntohs(conn->session);
215
216     sp.sa_family = AF_PPPOX;
217     sp.sa_protocol = PX_PROTO_OE;
218     sp.sa_addr.pppoe.sid = conn->session;
219     memcpy(sp.sa_addr.pppoe.dev, conn->ifName, IFNAMSIZ);
220     memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN);
221
222     /* Set remote_number for ServPoET */
223     sprintf(remote_number, "%02X:%02X:%02X:%02X:%02X:%02X",
224             (unsigned) conn->peerEth[0],
225             (unsigned) conn->peerEth[1],
226             (unsigned) conn->peerEth[2],
227             (unsigned) conn->peerEth[3],
228             (unsigned) conn->peerEth[4],
229             (unsigned) conn->peerEth[5]);
230
231     warn("Connected to %02X:%02X:%02X:%02X:%02X:%02X via interface %s",
232          (unsigned) conn->peerEth[0],
233          (unsigned) conn->peerEth[1],
234          (unsigned) conn->peerEth[2],
235          (unsigned) conn->peerEth[3],
236          (unsigned) conn->peerEth[4],
237          (unsigned) conn->peerEth[5],
238          conn->ifName);
239
240     script_setenv("MACREMOTE", remote_number, 0);
241
242     if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
243                 sizeof(struct sockaddr_pppox)) < 0) {
244         error("Failed to connect PPPoE socket: %d %m", errno);
245         goto errout;
246     }
247
248     return conn->sessionSocket;
249
250  errout:
251     if (conn->discoverySocket >= 0) {
252         sendPADT(conn, NULL);
253         close(conn->discoverySocket);
254         conn->discoverySocket = -1;
255     }
256     close(conn->sessionSocket);
257     return -1;
258 }
259
260 static void
261 PPPOERecvConfig(int mru,
262                 u_int32_t asyncmap,
263                 int pcomp,
264                 int accomp)
265 {
266 #if 0 /* broken protocol, but no point harrassing the users I guess... */
267     if (mru > MAX_PPPOE_MTU)
268         warn("Couldn't increase MRU to %d", mru);
269 #endif
270 }
271
272 /**********************************************************************
273  * %FUNCTION: PPPOEDisconnectDevice
274  * %ARGUMENTS:
275  * None
276  * %RETURNS:
277  * Nothing
278  * %DESCRIPTION:
279  * Disconnects PPPoE device
280  ***********************************************************************/
281 static void
282 PPPOEDisconnectDevice(void)
283 {
284     struct sockaddr_pppox sp;
285
286     sp.sa_family = AF_PPPOX;
287     sp.sa_protocol = PX_PROTO_OE;
288     sp.sa_addr.pppoe.sid = 0;
289     memcpy(sp.sa_addr.pppoe.dev, conn->ifName, IFNAMSIZ);
290     memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN);
291     if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
292                 sizeof(struct sockaddr_pppox)) < 0 && errno != EALREADY)
293         error("Failed to disconnect PPPoE socket: %d %m", errno);
294     close(conn->sessionSocket);
295     if (conn->discoverySocket >= 0) {
296         sendPADT(conn, NULL);
297         close(conn->discoverySocket);
298     }
299 }
300
301 static void
302 PPPOEDeviceOptions(void)
303 {
304     char buf[MAXPATHLEN];
305
306     strlcpy(buf, _PATH_ETHOPT, MAXPATHLEN);
307     strlcat(buf, devnam, MAXPATHLEN);
308     if (!options_from_file(buf, 0, 0, 1))
309         exit(EXIT_OPTION_ERROR);
310
311 }
312
313 struct channel pppoe_channel;
314
315 /**********************************************************************
316  * %FUNCTION: PPPoEDevnameHook
317  * %ARGUMENTS:
318  * cmd -- the command (actually, the device name
319  * argv -- argument vector
320  * doit -- if non-zero, set device name.  Otherwise, just check if possible
321  * %RETURNS:
322  * 1 if we will handle this device; 0 otherwise.
323  * %DESCRIPTION:
324  * Checks if name is a valid interface name; if so, returns 1.  Also
325  * sets up devnam (string representation of device).
326  ***********************************************************************/
327 static int
328 PPPoEDevnameHook(char *cmd, char **argv, int doit)
329 {
330     int r = 1;
331     int fd;
332     struct ifreq ifr;
333
334     /*
335      * Take any otherwise-unrecognized option as a possible device name,
336      * and test if it is the name of a network interface with a
337      * hardware address whose sa_family is ARPHRD_ETHER.
338      */
339     if (strlen(cmd) > 4 && !strncmp(cmd, "nic-", 4)) {
340         /* Strip off "nic-" */
341         cmd += 4;
342     }
343
344     /* Open a socket */
345     if ((fd = socket(PF_PACKET, SOCK_RAW, 0)) < 0) {
346         r = 0;
347     }
348
349     /* Try getting interface index */
350     if (r) {
351         strlcpy(ifr.ifr_name, cmd, sizeof(ifr.ifr_name));
352         if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
353             r = 0;
354         } else {
355             if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
356                 r = 0;
357             } else {
358                 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
359                     if (doit)
360                         error("Interface %s not Ethernet", cmd);
361                     r = 0;
362                 }
363             }
364         }
365     }
366
367     /* Close socket */
368     close(fd);
369     if (r && doit) {
370         strlcpy(devnam, cmd, sizeof(devnam));
371         if (the_channel != &pppoe_channel) {
372
373             the_channel = &pppoe_channel;
374             modem = 0;
375
376             PPPOEInitDevice();
377         }
378         return 1;
379     }
380
381     return r;
382 }
383
384 /**********************************************************************
385  * %FUNCTION: plugin_init
386  * %ARGUMENTS:
387  * None
388  * %RETURNS:
389  * Nothing
390  * %DESCRIPTION:
391  * Initializes hooks for pppd plugin
392  ***********************************************************************/
393 void
394 plugin_init(void)
395 {
396     if (!ppp_available() && !new_style_driver) {
397         fatal("Linux kernel does not support PPPoE -- are you running 2.4.x?");
398     }
399
400     add_options(Options);
401
402     info("RP-PPPoE plugin version %s compiled against pppd %s",
403          RP_VERSION, VERSION);
404 }
405
406 void pppoe_check_options(void)
407 {
408     unsigned int mac[6];
409     int i;
410
411     if (pppoe_reqd_mac != NULL) {
412         if (sscanf(pppoe_reqd_mac, "%x:%x:%x:%x:%x:%x",
413                    &mac[0], &mac[1], &mac[2], &mac[3],
414                    &mac[4], &mac[5]) != 6) {
415             option_error("cannot parse pppoe-mac option value");
416             exit(EXIT_OPTION_ERROR);
417         }
418         for (i = 0; i < 6; ++i)
419             conn->req_peer_mac[i] = mac[i];
420         conn->req_peer = 1;
421     }
422
423     lcp_allowoptions[0].neg_accompression = 0;
424     lcp_wantoptions[0].neg_accompression = 0;
425
426     lcp_allowoptions[0].neg_asyncmap = 0;
427     lcp_wantoptions[0].neg_asyncmap = 0;
428
429     lcp_allowoptions[0].neg_pcompression = 0;
430     lcp_wantoptions[0].neg_pcompression = 0;
431
432     if (lcp_allowoptions[0].mru > MAX_PPPOE_MTU)
433         lcp_allowoptions[0].mru = MAX_PPPOE_MTU;
434     if (lcp_wantoptions[0].mru > MAX_PPPOE_MTU)
435         lcp_wantoptions[0].mru = MAX_PPPOE_MTU;
436
437     /* Save configuration */
438     conn->mtu = lcp_allowoptions[0].mru;
439     conn->mru = lcp_wantoptions[0].mru;
440
441     ccp_allowoptions[0].deflate = 0;
442     ccp_wantoptions[0].deflate = 0;
443
444     ipcp_allowoptions[0].neg_vj = 0;
445     ipcp_wantoptions[0].neg_vj = 0;
446
447     ccp_allowoptions[0].bsd_compress = 0;
448     ccp_wantoptions[0].bsd_compress = 0;
449 }
450
451 struct channel pppoe_channel = {
452     .options = Options,
453     .process_extra_options = &PPPOEDeviceOptions,
454     .check_options = pppoe_check_options,
455     .connect = &PPPOEConnectDevice,
456     .disconnect = &PPPOEDisconnectDevice,
457     .establish_ppp = &generic_establish_ppp,
458     .disestablish_ppp = &generic_disestablish_ppp,
459     .send_config = NULL,
460     .recv_config = &PPPOERecvConfig,
461     .close = NULL,
462     .cleanup = NULL
463 };