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