]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/pppoe/plugin.c
Make a consistency in pppoe options
[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 static int printACNames = 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, &printACNames,
93       "Be verbose about discovered access concentrators" },
94     { "rp_pppoe_verbose", o_int, &printACNames,
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->printACNames = printACNames;
132     conn->discoveryTimeout = pppoe_padi_timeout;
133     conn->discoveryAttempts = pppoe_padi_attempts;
134     return 1;
135 }
136
137 /**********************************************************************
138  * %FUNCTION: PPPOEConnectDevice
139  * %ARGUMENTS:
140  * None
141  * %RETURNS:
142  * Non-negative if all goes well; -1 otherwise
143  * %DESCRIPTION:
144  * Connects PPPoE device.
145  ***********************************************************************/
146 static int
147 PPPOEConnectDevice(void)
148 {
149     struct sockaddr_pppox sp;
150     struct ifreq ifr;
151     int s;
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;
166     lcp_wantoptions[0].mru = conn->mru;
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 = ifr.ifr_mtu - TOTAL_OVERHEAD;
184     if (lcp_wantoptions[0].mru > ifr.ifr_mtu - TOTAL_OVERHEAD)
185         lcp_wantoptions[0].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     strlcpy(ppp_devnam, devnam, sizeof(ppp_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         discovery(conn);
217         if (conn->discoveryState != STATE_SESSION) {
218             error("Unable to complete PPPoE Discovery");
219             goto errout;
220         }
221     }
222
223     /* Set PPPoE session-number for further consumption */
224     ppp_session_number = ntohs(conn->session);
225
226     sp.sa_family = AF_PPPOX;
227     sp.sa_protocol = PX_PROTO_OE;
228     sp.sa_addr.pppoe.sid = conn->session;
229     memcpy(sp.sa_addr.pppoe.dev, conn->ifName, IFNAMSIZ);
230     memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN);
231
232     /* Set remote_number for ServPoET */
233     sprintf(remote_number, "%02X:%02X:%02X:%02X:%02X:%02X",
234             (unsigned) conn->peerEth[0],
235             (unsigned) conn->peerEth[1],
236             (unsigned) conn->peerEth[2],
237             (unsigned) conn->peerEth[3],
238             (unsigned) conn->peerEth[4],
239             (unsigned) conn->peerEth[5]);
240
241     warn("Connected to %02X:%02X:%02X:%02X:%02X:%02X via interface %s",
242          (unsigned) conn->peerEth[0],
243          (unsigned) conn->peerEth[1],
244          (unsigned) conn->peerEth[2],
245          (unsigned) conn->peerEth[3],
246          (unsigned) conn->peerEth[4],
247          (unsigned) conn->peerEth[5],
248          conn->ifName);
249
250     script_setenv("MACREMOTE", remote_number, 0);
251
252     if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
253                 sizeof(struct sockaddr_pppox)) < 0) {
254         error("Failed to connect PPPoE socket: %d %m", errno);
255         goto errout;
256     }
257
258     return conn->sessionSocket;
259
260  errout:
261     if (conn->discoverySocket >= 0) {
262         sendPADT(conn, NULL);
263         close(conn->discoverySocket);
264         conn->discoverySocket = -1;
265     }
266     close(conn->sessionSocket);
267     return -1;
268 }
269
270 static void
271 PPPOERecvConfig(int mru,
272                 u_int32_t asyncmap,
273                 int pcomp,
274                 int accomp)
275 {
276 #if 0 /* broken protocol, but no point harrassing the users I guess... */
277     if (mru > MAX_PPPOE_MTU)
278         warn("Couldn't increase MRU to %d", mru);
279 #endif
280 }
281
282 /**********************************************************************
283  * %FUNCTION: PPPOEDisconnectDevice
284  * %ARGUMENTS:
285  * None
286  * %RETURNS:
287  * Nothing
288  * %DESCRIPTION:
289  * Disconnects PPPoE device
290  ***********************************************************************/
291 static void
292 PPPOEDisconnectDevice(void)
293 {
294     struct sockaddr_pppox sp;
295
296     sp.sa_family = AF_PPPOX;
297     sp.sa_protocol = PX_PROTO_OE;
298     sp.sa_addr.pppoe.sid = 0;
299     memcpy(sp.sa_addr.pppoe.dev, conn->ifName, IFNAMSIZ);
300     memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN);
301     if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
302                 sizeof(struct sockaddr_pppox)) < 0 && errno != EALREADY)
303         error("Failed to disconnect PPPoE socket: %d %m", errno);
304     close(conn->sessionSocket);
305     if (conn->discoverySocket >= 0) {
306         sendPADT(conn, NULL);
307         close(conn->discoverySocket);
308     }
309 }
310
311 static void
312 PPPOEDeviceOptions(void)
313 {
314     char buf[MAXPATHLEN];
315
316     strlcpy(buf, _PATH_ETHOPT, MAXPATHLEN);
317     strlcat(buf, devnam, MAXPATHLEN);
318     if (!options_from_file(buf, 0, 0, 1))
319         exit(EXIT_OPTION_ERROR);
320
321 }
322
323 struct channel pppoe_channel;
324
325 /**********************************************************************
326  * %FUNCTION: PPPoEDevnameHook
327  * %ARGUMENTS:
328  * cmd -- the command (actually, the device name
329  * argv -- argument vector
330  * doit -- if non-zero, set device name.  Otherwise, just check if possible
331  * %RETURNS:
332  * 1 if we will handle this device; 0 otherwise.
333  * %DESCRIPTION:
334  * Checks if name is a valid interface name; if so, returns 1.  Also
335  * sets up devnam (string representation of device).
336  ***********************************************************************/
337 static int
338 PPPoEDevnameHook(char *cmd, char **argv, int doit)
339 {
340     int r = 1;
341     int fd;
342     struct ifreq ifr;
343
344     /*
345      * Take any otherwise-unrecognized option as a possible device name,
346      * and test if it is the name of a network interface with a
347      * hardware address whose sa_family is ARPHRD_ETHER.
348      */
349     if (strlen(cmd) > 4 && !strncmp(cmd, "nic-", 4)) {
350         /* Strip off "nic-" */
351         cmd += 4;
352     }
353
354     /* Open a socket */
355     if ((fd = socket(PF_PACKET, SOCK_RAW, 0)) < 0) {
356         r = 0;
357     }
358
359     /* Try getting interface index */
360     if (r) {
361         strlcpy(ifr.ifr_name, cmd, sizeof(ifr.ifr_name));
362         if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
363             r = 0;
364         } else {
365             if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
366                 r = 0;
367             } else {
368                 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
369                     if (doit)
370                         error("Interface %s not Ethernet", cmd);
371                     r = 0;
372                 }
373             }
374         }
375     }
376
377     /* Close socket */
378     close(fd);
379     if (r && doit) {
380         strlcpy(devnam, cmd, sizeof(devnam));
381         if (the_channel != &pppoe_channel) {
382
383             the_channel = &pppoe_channel;
384             modem = 0;
385
386             PPPOEInitDevice();
387         }
388         return 1;
389     }
390
391     return r;
392 }
393
394 /**********************************************************************
395  * %FUNCTION: plugin_init
396  * %ARGUMENTS:
397  * None
398  * %RETURNS:
399  * Nothing
400  * %DESCRIPTION:
401  * Initializes hooks for pppd plugin
402  ***********************************************************************/
403 void
404 plugin_init(void)
405 {
406     if (!ppp_available() && !new_style_driver) {
407         fatal("Linux kernel does not support PPPoE -- are you running 2.4.x?");
408     }
409
410     add_options(Options);
411
412     info("PPPoE plugin from pppd %s", VERSION);
413 }
414
415 void pppoe_check_options(void)
416 {
417     unsigned int mac[6];
418     int i;
419
420     if (pppoe_reqd_mac != NULL) {
421         if (sscanf(pppoe_reqd_mac, "%x:%x:%x:%x:%x:%x",
422                    &mac[0], &mac[1], &mac[2], &mac[3],
423                    &mac[4], &mac[5]) != 6) {
424             option_error("cannot parse pppoe-mac option value");
425             exit(EXIT_OPTION_ERROR);
426         }
427         for (i = 0; i < 6; ++i)
428             conn->req_peer_mac[i] = mac[i];
429         conn->req_peer = 1;
430     }
431
432     lcp_allowoptions[0].neg_accompression = 0;
433     lcp_wantoptions[0].neg_accompression = 0;
434
435     lcp_allowoptions[0].neg_asyncmap = 0;
436     lcp_wantoptions[0].neg_asyncmap = 0;
437
438     lcp_allowoptions[0].neg_pcompression = 0;
439     lcp_wantoptions[0].neg_pcompression = 0;
440
441     if (lcp_allowoptions[0].mru > MAX_PPPOE_MTU)
442         lcp_allowoptions[0].mru = MAX_PPPOE_MTU;
443     if (lcp_wantoptions[0].mru > MAX_PPPOE_MTU)
444         lcp_wantoptions[0].mru = MAX_PPPOE_MTU;
445
446     /* Save configuration */
447     conn->mtu = lcp_allowoptions[0].mru;
448     conn->mru = lcp_wantoptions[0].mru;
449
450     ccp_allowoptions[0].deflate = 0;
451     ccp_wantoptions[0].deflate = 0;
452
453     ipcp_allowoptions[0].neg_vj = 0;
454     ipcp_wantoptions[0].neg_vj = 0;
455
456     ccp_allowoptions[0].bsd_compress = 0;
457     ccp_wantoptions[0].bsd_compress = 0;
458 }
459
460 struct channel pppoe_channel = {
461     .options = Options,
462     .process_extra_options = &PPPOEDeviceOptions,
463     .check_options = pppoe_check_options,
464     .connect = &PPPOEConnectDevice,
465     .disconnect = &PPPOEDisconnectDevice,
466     .establish_ppp = &generic_establish_ppp,
467     .disestablish_ppp = &generic_disestablish_ppp,
468     .send_config = NULL,
469     .recv_config = &PPPOERecvConfig,
470     .close = NULL,
471     .cleanup = NULL
472 };