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