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