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 $";
35 #include "pppd/pppd.h"
38 #include "pppd/ipcp.h"
40 /* #include "pppd/pathnames.h" */
42 #include <linux/types.h>
43 #include <sys/ioctl.h>
44 #include <sys/types.h>
45 #include <sys/socket.h>
53 #include <net/if_arp.h>
54 #include <linux/ppp_defs.h>
55 #include <linux/if_pppox.h>
61 #define _PATH_ETHOPT _ROOT_PATH "/etc/ppp/options."
63 char pppd_version[] = VERSION;
65 /* From sys-linux.c in pppd -- MUST FIX THIS! */
66 extern int new_style_driver;
68 char *pppd_pppoe_service = NULL;
69 static char *acName = NULL;
70 static char *existingSession = NULL;
71 static int printACNames = 0;
72 static char *pppoe_reqd_mac = NULL;
73 unsigned char pppoe_reqd_mac_addr[6];
74 static char *pppoe_host_uniq;
75 static int pppoe_padi_timeout = PADI_TIMEOUT;
76 static int pppoe_padi_attempts = MAX_PADI_ATTEMPTS;
78 static int PPPoEDevnameHook(char *cmd, char **argv, int doit);
79 static option_t Options[] = {
80 { "device name", o_wild, (void *) &PPPoEDevnameHook,
82 OPT_DEVNAM | OPT_PRIVFIX | OPT_NOARG | OPT_A2STRVAL | OPT_STATIC,
84 { "pppoe-service", o_string, &pppd_pppoe_service,
85 "Desired PPPoE service name" },
86 { "rp_pppoe_service", o_string, &pppd_pppoe_service,
87 "Legacy alias for pppoe-service", OPT_ALIAS },
88 { "pppoe-ac", o_string, &acName,
89 "Desired PPPoE access concentrator name" },
90 { "rp_pppoe_ac", o_string, &acName,
91 "Legacy alias for pppoe-ac", OPT_ALIAS },
92 { "pppoe-sess", o_string, &existingSession,
93 "Attach to existing session (sessid:macaddr)" },
94 { "rp_pppoe_sess", o_string, &existingSession,
95 "Legacy alias for pppoe-sess", OPT_ALIAS },
96 { "pppoe-verbose", o_int, &printACNames,
97 "Be verbose about discovered access concentrators" },
98 { "rp_pppoe_verbose", o_int, &printACNames,
99 "Legacy alias for pppoe-verbose", OPT_ALIAS },
100 { "pppoe-mac", o_string, &pppoe_reqd_mac,
101 "Only connect to specified MAC address" },
102 { "pppoe-host-uniq", o_string, &pppoe_host_uniq,
103 "Set the Host-Uniq to the supplied hex string" },
104 { "host-uniq", o_string, &pppoe_host_uniq,
105 "Legacy alias for pppoe-host-uniq", OPT_ALIAS },
106 { "pppoe-padi-timeout", o_int, &pppoe_padi_timeout,
107 "Initial timeout for discovery packets in seconds" },
108 { "pppoe-padi-attempts", o_int, &pppoe_padi_attempts,
109 "Number of discovery attempts" },
112 int (*OldDevnameHook)(char *cmd, char **argv, int doit) = NULL;
113 static PPPoEConnection *conn = NULL;
115 /**********************************************************************
116 * %FUNCTION: PPPOEInitDevice
122 * Initializes PPPoE device.
123 ***********************************************************************/
125 PPPOEInitDevice(void)
127 conn = malloc(sizeof(PPPoEConnection));
129 novm("PPPoE session data");
131 memset(conn, 0, sizeof(PPPoEConnection));
132 conn->ifName = devnam;
133 conn->discoverySocket = -1;
134 conn->sessionSocket = -1;
135 conn->printACNames = printACNames;
136 conn->discoveryTimeout = pppoe_padi_timeout;
137 conn->discoveryAttempts = pppoe_padi_attempts;
141 /**********************************************************************
142 * %FUNCTION: PPPOEConnectDevice
146 * Non-negative if all goes well; -1 otherwise
148 * Connects PPPoE device.
149 ***********************************************************************/
151 PPPOEConnectDevice(void)
153 struct sockaddr_pppox sp;
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");
168 /* Restore configuration */
169 lcp_allowoptions[0].mru = conn->mtu;
170 lcp_wantoptions[0].mru = conn->mru;
172 /* Update maximum MRU */
173 s = socket(AF_INET, SOCK_DGRAM, 0);
175 error("Can't get MTU for %s: %m", conn->ifName);
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);
186 if (lcp_allowoptions[0].mru > ifr.ifr_mtu - TOTAL_OVERHEAD)
187 lcp_allowoptions[0].mru = ifr.ifr_mtu - TOTAL_OVERHEAD;
188 if (lcp_wantoptions[0].mru > ifr.ifr_mtu - TOTAL_OVERHEAD)
189 lcp_wantoptions[0].mru = ifr.ifr_mtu - TOTAL_OVERHEAD;
191 if (pppoe_host_uniq) {
192 if (!parseHostUniq(pppoe_host_uniq, &conn->hostUniq))
193 fatal("Illegal value for pppoe-host-uniq option");
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));
202 conn->acName = acName;
203 conn->serviceName = pppd_pppoe_service;
204 strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam));
205 if (existingSession) {
206 unsigned int mac[ETH_ALEN];
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");
213 conn->session = htons(ses);
214 for (i=0; i<ETH_ALEN; i++) {
215 conn->peerEth[i] = (unsigned char) mac[i];
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");
225 if (conn->discoveryState != STATE_SESSION) {
226 error("Unable to complete PPPoE Discovery");
231 /* Set PPPoE session-number for further consumption */
232 ppp_session_number = ntohs(conn->session);
234 sp.sa_family = AF_PPPOX;
235 sp.sa_protocol = PX_PROTO_OE;
236 sp.sa_addr.pppoe.sid = conn->session;
237 memcpy(sp.sa_addr.pppoe.dev, conn->ifName, IFNAMSIZ);
238 memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN);
240 /* Set remote_number for ServPoET */
241 sprintf(remote_number, "%02X:%02X:%02X:%02X:%02X:%02X",
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]);
249 warn("Connected to %02X:%02X:%02X:%02X:%02X:%02X via interface %s",
250 (unsigned) conn->peerEth[0],
251 (unsigned) conn->peerEth[1],
252 (unsigned) conn->peerEth[2],
253 (unsigned) conn->peerEth[3],
254 (unsigned) conn->peerEth[4],
255 (unsigned) conn->peerEth[5],
258 script_setenv("MACREMOTE", remote_number, 0);
260 if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
261 sizeof(struct sockaddr_pppox)) < 0) {
262 error("Failed to connect PPPoE socket: %d %m", errno);
266 return conn->sessionSocket;
269 if (conn->discoverySocket >= 0) {
270 sendPADT(conn, NULL);
271 close(conn->discoverySocket);
272 conn->discoverySocket = -1;
274 close(conn->sessionSocket);
279 PPPOERecvConfig(int mru,
284 #if 0 /* broken protocol, but no point harrassing the users I guess... */
285 if (mru > MAX_PPPOE_MTU)
286 warn("Couldn't increase MRU to %d", mru);
290 /**********************************************************************
291 * %FUNCTION: PPPOEDisconnectDevice
297 * Disconnects PPPoE device
298 ***********************************************************************/
300 PPPOEDisconnectDevice(void)
302 struct sockaddr_pppox sp;
304 sp.sa_family = AF_PPPOX;
305 sp.sa_protocol = PX_PROTO_OE;
306 sp.sa_addr.pppoe.sid = 0;
307 memcpy(sp.sa_addr.pppoe.dev, conn->ifName, IFNAMSIZ);
308 memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN);
309 if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
310 sizeof(struct sockaddr_pppox)) < 0 && errno != EALREADY)
311 error("Failed to disconnect PPPoE socket: %d %m", errno);
312 close(conn->sessionSocket);
313 if (conn->discoverySocket >= 0) {
314 sendPADT(conn, NULL);
315 close(conn->discoverySocket);
320 PPPOEDeviceOptions(void)
322 char buf[MAXPATHLEN];
324 strlcpy(buf, _PATH_ETHOPT, MAXPATHLEN);
325 strlcat(buf, devnam, MAXPATHLEN);
326 if (!options_from_file(buf, 0, 0, 1))
327 exit(EXIT_OPTION_ERROR);
331 struct channel pppoe_channel;
333 /**********************************************************************
334 * %FUNCTION: PPPoEDevnameHook
336 * cmd -- the command (actually, the device name
337 * argv -- argument vector
338 * doit -- if non-zero, set device name. Otherwise, just check if possible
340 * 1 if we will handle this device; 0 otherwise.
342 * Checks if name is a valid interface name; if so, returns 1. Also
343 * sets up devnam (string representation of device).
344 ***********************************************************************/
346 PPPoEDevnameHook(char *cmd, char **argv, int doit)
353 * Take any otherwise-unrecognized option as a possible device name,
354 * and test if it is the name of a network interface with a
355 * hardware address whose sa_family is ARPHRD_ETHER.
357 if (strlen(cmd) > 4 && !strncmp(cmd, "nic-", 4)) {
358 /* Strip off "nic-" */
363 if ((fd = socket(PF_PACKET, SOCK_RAW, 0)) < 0) {
367 /* Try getting interface index */
369 strlcpy(ifr.ifr_name, cmd, sizeof(ifr.ifr_name));
370 if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
373 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
376 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
378 error("Interface %s not Ethernet", cmd);
388 strlcpy(devnam, cmd, sizeof(devnam));
389 if (the_channel != &pppoe_channel) {
391 the_channel = &pppoe_channel;
402 /**********************************************************************
403 * %FUNCTION: plugin_init
409 * Initializes hooks for pppd plugin
410 ***********************************************************************/
414 if (!ppp_available() && !new_style_driver) {
415 fatal("Linux kernel does not support PPPoE -- are you running 2.4.x?");
418 add_options(Options);
420 info("PPPoE plugin from pppd %s", VERSION);
423 void pppoe_check_options(void)
428 if (pppoe_reqd_mac != NULL) {
429 if (sscanf(pppoe_reqd_mac, "%x:%x:%x:%x:%x:%x",
430 &mac[0], &mac[1], &mac[2], &mac[3],
431 &mac[4], &mac[5]) != 6) {
432 option_error("cannot parse pppoe-mac option value");
433 exit(EXIT_OPTION_ERROR);
435 for (i = 0; i < 6; ++i)
436 conn->req_peer_mac[i] = mac[i];
440 lcp_allowoptions[0].neg_accompression = 0;
441 lcp_wantoptions[0].neg_accompression = 0;
443 lcp_allowoptions[0].neg_asyncmap = 0;
444 lcp_wantoptions[0].neg_asyncmap = 0;
446 lcp_allowoptions[0].neg_pcompression = 0;
447 lcp_wantoptions[0].neg_pcompression = 0;
449 if (lcp_allowoptions[0].mru > MAX_PPPOE_MTU)
450 lcp_allowoptions[0].mru = MAX_PPPOE_MTU;
451 if (lcp_wantoptions[0].mru > MAX_PPPOE_MTU)
452 lcp_wantoptions[0].mru = MAX_PPPOE_MTU;
454 /* Save configuration */
455 conn->mtu = lcp_allowoptions[0].mru;
456 conn->mru = lcp_wantoptions[0].mru;
458 ccp_allowoptions[0].deflate = 0;
459 ccp_wantoptions[0].deflate = 0;
461 ipcp_allowoptions[0].neg_vj = 0;
462 ipcp_wantoptions[0].neg_vj = 0;
464 ccp_allowoptions[0].bsd_compress = 0;
465 ccp_wantoptions[0].bsd_compress = 0;
468 struct channel pppoe_channel = {
470 .process_extra_options = &PPPOEDeviceOptions,
471 .check_options = pppoe_check_options,
472 .connect = &PPPOEConnectDevice,
473 .disconnect = &PPPOEDisconnectDevice,
474 .establish_ppp = &generic_establish_ppp,
475 .disestablish_ppp = &generic_disestablish_ppp,
477 .recv_config = &PPPOERecvConfig,