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