]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/pppoe/plugin.c
pppoe: Rename variable printACNames to pppoe_verbose to match cmdline option
[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 #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 int pppoe_verbose = 0;
68 static char *pppoe_reqd_mac = NULL;
69 unsigned char pppoe_reqd_mac_addr[6];
70 static char *pppoe_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     { "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
152     /* Open session socket before discovery phase, to avoid losing session */
153     /* packets sent by peer just after PADS packet (noted on some Cisco    */
154     /* server equipment).                                                  */
155     /* Opening this socket just before waitForPADS in the discovery()      */
156     /* function would be more appropriate, but it would mess-up the code   */
157     conn->sessionSocket = socket(AF_PPPOX, SOCK_STREAM, PX_PROTO_OE);
158     if (conn->sessionSocket < 0) {
159         error("Failed to create PPPoE socket: %m");
160         return -1;
161     }
162
163     /* Restore configuration */
164     lcp_allowoptions[0].mru = conn->mtu;
165     lcp_wantoptions[0].mru = conn->mru;
166
167     /* Update maximum MRU */
168     s = socket(AF_INET, SOCK_DGRAM, 0);
169     if (s < 0) {
170         error("Can't get MTU for %s: %m", conn->ifName);
171         goto errout;
172     }
173     strlcpy(ifr.ifr_name, conn->ifName, sizeof(ifr.ifr_name));
174     if (ioctl(s, SIOCGIFMTU, &ifr) < 0) {
175         error("Can't get MTU for %s: %m", conn->ifName);
176         close(s);
177         goto errout;
178     }
179     close(s);
180
181     if (lcp_allowoptions[0].mru > ifr.ifr_mtu - TOTAL_OVERHEAD)
182         lcp_allowoptions[0].mru = ifr.ifr_mtu - TOTAL_OVERHEAD;
183     if (lcp_wantoptions[0].mru > ifr.ifr_mtu - TOTAL_OVERHEAD)
184         lcp_wantoptions[0].mru = ifr.ifr_mtu - TOTAL_OVERHEAD;
185
186     if (pppoe_host_uniq) {
187         if (!parseHostUniq(pppoe_host_uniq, &conn->hostUniq))
188             fatal("Illegal value for pppoe-host-uniq option");
189     } else {
190         /* if a custom host-uniq is not supplied, use our PID */
191         pid_t pid = getpid();
192         conn->hostUniq.type = htons(TAG_HOST_UNIQ);
193         conn->hostUniq.length = htons(sizeof(pid));
194         memcpy(conn->hostUniq.payload, &pid, sizeof(pid));
195     }
196
197     conn->acName = acName;
198     conn->serviceName = pppd_pppoe_service;
199     strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam));
200     if (existingSession) {
201         unsigned int mac[ETH_ALEN];
202         int i, ses;
203         if (sscanf(existingSession, "%d:%x:%x:%x:%x:%x:%x",
204                    &ses, &mac[0], &mac[1], &mac[2],
205                    &mac[3], &mac[4], &mac[5]) != 7) {
206             fatal("Illegal value for pppoe-sess option");
207         }
208         conn->session = htons(ses);
209         for (i=0; i<ETH_ALEN; i++) {
210             conn->peerEth[i] = (unsigned char) mac[i];
211         }
212     } else {
213         conn->discoverySocket =
214             openInterface(conn->ifName, Eth_PPPOE_Discovery, conn->myEth);
215         if (conn->discoverySocket < 0) {
216             error("Failed to create PPPoE discovery socket: %m");
217             goto errout;
218         }
219         discovery(conn);
220         if (conn->discoveryState != STATE_SESSION) {
221             error("Unable to complete PPPoE Discovery");
222             goto errout;
223         }
224     }
225
226     /* Set PPPoE session-number for further consumption */
227     ppp_session_number = ntohs(conn->session);
228
229     sp.sa_family = AF_PPPOX;
230     sp.sa_protocol = PX_PROTO_OE;
231     sp.sa_addr.pppoe.sid = conn->session;
232     memcpy(sp.sa_addr.pppoe.dev, conn->ifName, IFNAMSIZ);
233     memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN);
234
235     /* Set remote_number for ServPoET */
236     sprintf(remote_number, "%02X:%02X:%02X:%02X:%02X:%02X",
237             (unsigned) conn->peerEth[0],
238             (unsigned) conn->peerEth[1],
239             (unsigned) conn->peerEth[2],
240             (unsigned) conn->peerEth[3],
241             (unsigned) conn->peerEth[4],
242             (unsigned) conn->peerEth[5]);
243
244     warn("Connected to %02X:%02X:%02X:%02X:%02X:%02X via interface %s",
245          (unsigned) conn->peerEth[0],
246          (unsigned) conn->peerEth[1],
247          (unsigned) conn->peerEth[2],
248          (unsigned) conn->peerEth[3],
249          (unsigned) conn->peerEth[4],
250          (unsigned) conn->peerEth[5],
251          conn->ifName);
252
253     script_setenv("MACREMOTE", remote_number, 0);
254
255     if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
256                 sizeof(struct sockaddr_pppox)) < 0) {
257         error("Failed to connect PPPoE socket: %d %m", errno);
258         goto errout;
259     }
260
261     return conn->sessionSocket;
262
263  errout:
264     if (conn->discoverySocket >= 0) {
265         sendPADT(conn, NULL);
266         close(conn->discoverySocket);
267         conn->discoverySocket = -1;
268     }
269     close(conn->sessionSocket);
270     return -1;
271 }
272
273 static void
274 PPPOERecvConfig(int mru,
275                 u_int32_t asyncmap,
276                 int pcomp,
277                 int accomp)
278 {
279 #if 0 /* broken protocol, but no point harrassing the users I guess... */
280     if (mru > MAX_PPPOE_MTU)
281         warn("Couldn't increase MRU to %d", mru);
282 #endif
283 }
284
285 /**********************************************************************
286  * %FUNCTION: PPPOEDisconnectDevice
287  * %ARGUMENTS:
288  * None
289  * %RETURNS:
290  * Nothing
291  * %DESCRIPTION:
292  * Disconnects PPPoE device
293  ***********************************************************************/
294 static void
295 PPPOEDisconnectDevice(void)
296 {
297     struct sockaddr_pppox sp;
298
299     sp.sa_family = AF_PPPOX;
300     sp.sa_protocol = PX_PROTO_OE;
301     sp.sa_addr.pppoe.sid = 0;
302     memcpy(sp.sa_addr.pppoe.dev, conn->ifName, IFNAMSIZ);
303     memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN);
304     if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
305                 sizeof(struct sockaddr_pppox)) < 0 && errno != EALREADY)
306         error("Failed to disconnect PPPoE socket: %d %m", errno);
307     close(conn->sessionSocket);
308     if (conn->discoverySocket >= 0) {
309         sendPADT(conn, NULL);
310         close(conn->discoverySocket);
311     }
312 }
313
314 static void
315 PPPOEDeviceOptions(void)
316 {
317     char buf[MAXPATHLEN];
318
319     strlcpy(buf, _PATH_ETHOPT, MAXPATHLEN);
320     strlcat(buf, devnam, MAXPATHLEN);
321     if (!options_from_file(buf, 0, 0, 1))
322         exit(EXIT_OPTION_ERROR);
323
324 }
325
326 struct channel pppoe_channel;
327
328 /**********************************************************************
329  * %FUNCTION: PPPoEDevnameHook
330  * %ARGUMENTS:
331  * cmd -- the command (actually, the device name
332  * argv -- argument vector
333  * doit -- if non-zero, set device name.  Otherwise, just check if possible
334  * %RETURNS:
335  * 1 if we will handle this device; 0 otherwise.
336  * %DESCRIPTION:
337  * Checks if name is a valid interface name; if so, returns 1.  Also
338  * sets up devnam (string representation of device).
339  ***********************************************************************/
340 static int
341 PPPoEDevnameHook(char *cmd, char **argv, int doit)
342 {
343     int r = 1;
344     int fd;
345     struct ifreq ifr;
346
347     /*
348      * Take any otherwise-unrecognized option as a possible device name,
349      * and test if it is the name of a network interface with a
350      * hardware address whose sa_family is ARPHRD_ETHER.
351      */
352     if (strlen(cmd) > 4 && !strncmp(cmd, "nic-", 4)) {
353         /* Strip off "nic-" */
354         cmd += 4;
355     }
356
357     /* Open a socket */
358     if ((fd = socket(PF_PACKET, SOCK_RAW, 0)) < 0) {
359         r = 0;
360     }
361
362     /* Try getting interface index */
363     if (r) {
364         strlcpy(ifr.ifr_name, cmd, sizeof(ifr.ifr_name));
365         if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
366             r = 0;
367         } else {
368             if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
369                 r = 0;
370             } else {
371                 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
372                     if (doit)
373                         error("Interface %s not Ethernet", cmd);
374                     r = 0;
375                 }
376             }
377         }
378     }
379
380     /* Close socket */
381     close(fd);
382     if (r && doit) {
383         strlcpy(devnam, cmd, sizeof(devnam));
384         if (the_channel != &pppoe_channel) {
385
386             the_channel = &pppoe_channel;
387             modem = 0;
388
389             PPPOEInitDevice();
390         }
391         return 1;
392     }
393
394     return r;
395 }
396
397 /**********************************************************************
398  * %FUNCTION: plugin_init
399  * %ARGUMENTS:
400  * None
401  * %RETURNS:
402  * Nothing
403  * %DESCRIPTION:
404  * Initializes hooks for pppd plugin
405  ***********************************************************************/
406 void
407 plugin_init(void)
408 {
409     if (!ppp_available() && !new_style_driver) {
410         fatal("Linux kernel does not support PPPoE -- are you running 2.4.x?");
411     }
412
413     add_options(Options);
414
415     info("PPPoE plugin from pppd %s", VERSION);
416 }
417
418 void pppoe_check_options(void)
419 {
420     unsigned int mac[6];
421     int i;
422
423     if (pppoe_reqd_mac != NULL) {
424         if (sscanf(pppoe_reqd_mac, "%x:%x:%x:%x:%x:%x",
425                    &mac[0], &mac[1], &mac[2], &mac[3],
426                    &mac[4], &mac[5]) != 6) {
427             option_error("cannot parse pppoe-mac option value");
428             exit(EXIT_OPTION_ERROR);
429         }
430         for (i = 0; i < 6; ++i)
431             conn->req_peer_mac[i] = mac[i];
432         conn->req_peer = 1;
433     }
434
435     lcp_allowoptions[0].neg_accompression = 0;
436     lcp_wantoptions[0].neg_accompression = 0;
437
438     lcp_allowoptions[0].neg_asyncmap = 0;
439     lcp_wantoptions[0].neg_asyncmap = 0;
440
441     lcp_allowoptions[0].neg_pcompression = 0;
442     lcp_wantoptions[0].neg_pcompression = 0;
443
444     if (lcp_allowoptions[0].mru > MAX_PPPOE_MTU)
445         lcp_allowoptions[0].mru = MAX_PPPOE_MTU;
446     if (lcp_wantoptions[0].mru > MAX_PPPOE_MTU)
447         lcp_wantoptions[0].mru = MAX_PPPOE_MTU;
448
449     /* Save configuration */
450     conn->mtu = lcp_allowoptions[0].mru;
451     conn->mru = lcp_wantoptions[0].mru;
452
453     ccp_allowoptions[0].deflate = 0;
454     ccp_wantoptions[0].deflate = 0;
455
456     ipcp_allowoptions[0].neg_vj = 0;
457     ipcp_wantoptions[0].neg_vj = 0;
458
459     ccp_allowoptions[0].bsd_compress = 0;
460     ccp_wantoptions[0].bsd_compress = 0;
461 }
462
463 struct channel pppoe_channel = {
464     .options = Options,
465     .process_extra_options = &PPPOEDeviceOptions,
466     .check_options = pppoe_check_options,
467     .connect = &PPPOEConnectDevice,
468     .disconnect = &PPPOEDisconnectDevice,
469     .establish_ppp = &generic_establish_ppp,
470     .disestablish_ppp = &generic_disestablish_ppp,
471     .send_config = NULL,
472     .recv_config = &PPPOERecvConfig,
473     .close = NULL,
474     .cleanup = NULL
475 };