1 /***********************************************************************
5 * pppd plugin for kernel-mode PPPoE on Linux
7 * Copyright (C) 2001 by Roaring Penguin Software Inc., Michal Ostrowski
8 * and Jamal Hadi Salim.
10 * Much code and many ideas derived from pppoe plugin by Michal
11 * Ostrowski and Jamal Hadi Salim, which carries this copyright:
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.
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.
23 ***********************************************************************/
25 static char const RCSID[] =
26 "$Id: plugin.c,v 1.17 2008/06/15 04:35:50 paulus Exp $";
31 #include "pppd/pppd.h"
34 #include "pppd/ipcp.h"
36 /* #include "pppd/pathnames.h" */
38 #include <linux/types.h>
39 #include <sys/ioctl.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
49 #include <net/if_arp.h>
50 #include <linux/ppp_defs.h>
51 #include <linux/if_pppox.h>
57 #define _PATH_ETHOPT _ROOT_PATH "/etc/ppp/options."
59 char pppd_version[] = VERSION;
61 /* From sys-linux.c in pppd -- MUST FIX THIS! */
62 extern int new_style_driver;
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;
74 static int PPPoEDevnameHook(char *cmd, char **argv, int doit);
75 static option_t Options[] = {
76 { "device name", o_wild, (void *) &PPPoEDevnameHook,
78 OPT_DEVNAM | OPT_PRIVFIX | OPT_NOARG | OPT_A2STRVAL | OPT_STATIC,
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" },
108 int (*OldDevnameHook)(char *cmd, char **argv, int doit) = NULL;
109 static PPPoEConnection *conn = NULL;
111 /**********************************************************************
112 * %FUNCTION: PPPOEInitDevice
118 * Initializes PPPoE device.
119 ***********************************************************************/
121 PPPOEInitDevice(void)
123 conn = malloc(sizeof(PPPoEConnection));
125 novm("PPPoE session data");
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;
136 /**********************************************************************
137 * %FUNCTION: PPPOEConnectDevice
141 * Non-negative if all goes well; -1 otherwise
143 * Connects PPPoE device.
144 ***********************************************************************/
146 PPPOEConnectDevice(void)
148 struct sockaddr_pppox sp;
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");
163 /* Restore configuration */
164 lcp_allowoptions[0].mru = conn->mtu;
165 lcp_wantoptions[0].mru = conn->mru;
167 /* Update maximum MRU */
168 s = socket(AF_INET, SOCK_DGRAM, 0);
170 error("Can't get MTU for %s: %m", conn->ifName);
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);
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;
186 if (pppoe_host_uniq) {
187 if (!parseHostUniq(pppoe_host_uniq, &conn->hostUniq))
188 fatal("Illegal value for pppoe-host-uniq option");
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));
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];
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");
208 conn->session = htons(ses);
209 for (i=0; i<ETH_ALEN; i++) {
210 conn->peerEth[i] = (unsigned char) mac[i];
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");
220 if (conn->discoveryState != STATE_SESSION) {
221 error("Unable to complete PPPoE Discovery");
226 /* Set PPPoE session-number for further consumption */
227 ppp_session_number = ntohs(conn->session);
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);
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]);
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],
253 script_setenv("MACREMOTE", remote_number, 0);
255 if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
256 sizeof(struct sockaddr_pppox)) < 0) {
257 error("Failed to connect PPPoE socket: %d %m", errno);
261 return conn->sessionSocket;
264 if (conn->discoverySocket >= 0) {
265 sendPADT(conn, NULL);
266 close(conn->discoverySocket);
267 conn->discoverySocket = -1;
269 close(conn->sessionSocket);
274 PPPOERecvConfig(int mru,
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);
285 /**********************************************************************
286 * %FUNCTION: PPPOEDisconnectDevice
292 * Disconnects PPPoE device
293 ***********************************************************************/
295 PPPOEDisconnectDevice(void)
297 struct sockaddr_pppox sp;
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);
315 PPPOEDeviceOptions(void)
317 char buf[MAXPATHLEN];
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);
326 struct channel pppoe_channel;
328 /**********************************************************************
329 * %FUNCTION: PPPoEDevnameHook
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
335 * 1 if we will handle this device; 0 otherwise.
337 * Checks if name is a valid interface name; if so, returns 1. Also
338 * sets up devnam (string representation of device).
339 ***********************************************************************/
341 PPPoEDevnameHook(char *cmd, char **argv, int doit)
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.
352 if (strlen(cmd) > 4 && !strncmp(cmd, "nic-", 4)) {
353 /* Strip off "nic-" */
358 if ((fd = socket(PF_PACKET, SOCK_RAW, 0)) < 0) {
362 /* Try getting interface index */
364 strlcpy(ifr.ifr_name, cmd, sizeof(ifr.ifr_name));
365 if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
368 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
371 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
373 error("Interface %s not Ethernet", cmd);
383 strlcpy(devnam, cmd, sizeof(devnam));
384 if (the_channel != &pppoe_channel) {
386 the_channel = &pppoe_channel;
397 /**********************************************************************
398 * %FUNCTION: plugin_init
404 * Initializes hooks for pppd plugin
405 ***********************************************************************/
409 if (!ppp_available() && !new_style_driver) {
410 fatal("Linux kernel does not support PPPoE -- are you running 2.4.x?");
413 add_options(Options);
415 info("PPPoE plugin from pppd %s", VERSION);
418 void pppoe_check_options(void)
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);
430 for (i = 0; i < 6; ++i)
431 conn->req_peer_mac[i] = mac[i];
435 lcp_allowoptions[0].neg_accompression = 0;
436 lcp_wantoptions[0].neg_accompression = 0;
438 lcp_allowoptions[0].neg_asyncmap = 0;
439 lcp_wantoptions[0].neg_asyncmap = 0;
441 lcp_allowoptions[0].neg_pcompression = 0;
442 lcp_wantoptions[0].neg_pcompression = 0;
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;
449 /* Save configuration */
450 conn->mtu = lcp_allowoptions[0].mru;
451 conn->mru = lcp_wantoptions[0].mru;
453 ccp_allowoptions[0].deflate = 0;
454 ccp_wantoptions[0].deflate = 0;
456 ipcp_allowoptions[0].neg_vj = 0;
457 ipcp_wantoptions[0].neg_vj = 0;
459 ccp_allowoptions[0].bsd_compress = 0;
460 ccp_wantoptions[0].bsd_compress = 0;
463 struct channel pppoe_channel = {
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,
472 .recv_config = &PPPOERecvConfig,