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/ethernet.h>
50 #include <net/if_arp.h>
51 #include <linux/ppp_defs.h>
52 #include <linux/if_pppox.h>
58 #define _PATH_ETHOPT _ROOT_PATH "/etc/ppp/options."
60 char pppd_version[] = VERSION;
62 /* From sys-linux.c in pppd -- MUST FIX THIS! */
63 extern int new_style_driver;
65 char *pppd_pppoe_service = NULL;
66 static char *acName = NULL;
67 static char *existingSession = NULL;
68 static int printACNames = 0;
69 static char *pppoe_reqd_mac = NULL;
70 unsigned char pppoe_reqd_mac_addr[6];
72 static int PPPoEDevnameHook(char *cmd, char **argv, int doit);
73 static option_t Options[] = {
74 { "device name", o_wild, (void *) &PPPoEDevnameHook,
76 OPT_DEVNAM | OPT_PRIVFIX | OPT_NOARG | OPT_A2STRVAL | OPT_STATIC,
78 { "rp_pppoe_service", o_string, &pppd_pppoe_service,
79 "Desired PPPoE service name" },
80 { "rp_pppoe_ac", o_string, &acName,
81 "Desired PPPoE access concentrator name" },
82 { "rp_pppoe_sess", o_string, &existingSession,
83 "Attach to existing session (sessid:macaddr)" },
84 { "rp_pppoe_verbose", o_int, &printACNames,
85 "Be verbose about discovered access concentrators"},
86 { "pppoe-mac", o_string, &pppoe_reqd_mac,
87 "Only connect to specified MAC address" },
90 int (*OldDevnameHook)(char *cmd, char **argv, int doit) = NULL;
91 static PPPoEConnection *conn = NULL;
93 /**********************************************************************
94 * %FUNCTION: PPPOEInitDevice
100 * Initializes PPPoE device.
101 ***********************************************************************/
103 PPPOEInitDevice(void)
105 conn = malloc(sizeof(PPPoEConnection));
107 novm("PPPoE session data");
109 memset(conn, 0, sizeof(PPPoEConnection));
110 conn->ifName = devnam;
111 conn->discoverySocket = -1;
112 conn->sessionSocket = -1;
113 conn->useHostUniq = 1;
114 conn->printACNames = printACNames;
115 conn->discoveryTimeout = PADI_TIMEOUT;
119 /**********************************************************************
120 * %FUNCTION: PPPOEConnectDevice
124 * Non-negative if all goes well; -1 otherwise
126 * Connects PPPoE device.
127 ***********************************************************************/
129 PPPOEConnectDevice(void)
131 struct sockaddr_pppox sp;
135 /* Open session socket before discovery phase, to avoid losing session */
136 /* packets sent by peer just after PADS packet (noted on some Cisco */
137 /* server equipment). */
138 /* Opening this socket just before waitForPADS in the discovery() */
139 /* function would be more appropriate, but it would mess-up the code */
140 conn->sessionSocket = socket(AF_PPPOX, SOCK_STREAM, PX_PROTO_OE);
141 if (conn->sessionSocket < 0) {
142 error("Failed to create PPPoE socket: %m");
146 /* Restore configuration */
147 lcp_allowoptions[0].mru = conn->mtu;
148 lcp_wantoptions[0].mru = conn->mru;
150 /* Update maximum MRU */
151 s = socket(AF_INET, SOCK_DGRAM, 0);
153 error("Can't get MTU for %s: %m", conn->ifName);
156 strncpy(ifr.ifr_name, conn->ifName, sizeof(ifr.ifr_name));
157 if (ioctl(s, SIOCGIFMTU, &ifr) < 0) {
158 error("Can't get MTU for %s: %m", conn->ifName);
164 if (lcp_allowoptions[0].mru > ifr.ifr_mtu - TOTAL_OVERHEAD)
165 lcp_allowoptions[0].mru = ifr.ifr_mtu - TOTAL_OVERHEAD;
166 if (lcp_wantoptions[0].mru > ifr.ifr_mtu - TOTAL_OVERHEAD)
167 lcp_wantoptions[0].mru = ifr.ifr_mtu - TOTAL_OVERHEAD;
169 conn->acName = acName;
170 conn->serviceName = pppd_pppoe_service;
171 strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam));
172 if (existingSession) {
173 unsigned int mac[ETH_ALEN];
175 if (sscanf(existingSession, "%d:%x:%x:%x:%x:%x:%x",
176 &ses, &mac[0], &mac[1], &mac[2],
177 &mac[3], &mac[4], &mac[5]) != 7) {
178 fatal("Illegal value for rp_pppoe_sess option");
180 conn->session = htons(ses);
181 for (i=0; i<ETH_ALEN; i++) {
182 conn->peerEth[i] = (unsigned char) mac[i];
185 conn->discoverySocket =
186 openInterface(conn->ifName, Eth_PPPOE_Discovery, conn->myEth);
188 if (conn->discoveryState != STATE_SESSION) {
189 error("Unable to complete PPPoE Discovery");
194 /* Set PPPoE session-number for further consumption */
195 ppp_session_number = ntohs(conn->session);
197 sp.sa_family = AF_PPPOX;
198 sp.sa_protocol = PX_PROTO_OE;
199 sp.sa_addr.pppoe.sid = conn->session;
200 memcpy(sp.sa_addr.pppoe.dev, conn->ifName, IFNAMSIZ);
201 memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN);
203 /* Set remote_number for ServPoET */
204 sprintf(remote_number, "%02X:%02X:%02X:%02X:%02X:%02X",
205 (unsigned) conn->peerEth[0],
206 (unsigned) conn->peerEth[1],
207 (unsigned) conn->peerEth[2],
208 (unsigned) conn->peerEth[3],
209 (unsigned) conn->peerEth[4],
210 (unsigned) conn->peerEth[5]);
212 warn("Connected to %02X:%02X:%02X:%02X:%02X:%02X via interface %s",
213 (unsigned) conn->peerEth[0],
214 (unsigned) conn->peerEth[1],
215 (unsigned) conn->peerEth[2],
216 (unsigned) conn->peerEth[3],
217 (unsigned) conn->peerEth[4],
218 (unsigned) conn->peerEth[5],
221 script_setenv("MACREMOTE", remote_number, 0);
223 if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
224 sizeof(struct sockaddr_pppox)) < 0) {
225 error("Failed to connect PPPoE socket: %d %m", errno);
229 return conn->sessionSocket;
232 if (conn->discoverySocket >= 0) {
233 sendPADT(conn, NULL);
234 close(conn->discoverySocket);
235 conn->discoverySocket = -1;
237 close(conn->sessionSocket);
242 PPPOERecvConfig(int mru,
247 #if 0 /* broken protocol, but no point harrassing the users I guess... */
248 if (mru > MAX_PPPOE_MTU)
249 warn("Couldn't increase MRU to %d", mru);
253 /**********************************************************************
254 * %FUNCTION: PPPOEDisconnectDevice
260 * Disconnects PPPoE device
261 ***********************************************************************/
263 PPPOEDisconnectDevice(void)
265 struct sockaddr_pppox sp;
267 sp.sa_family = AF_PPPOX;
268 sp.sa_protocol = PX_PROTO_OE;
269 sp.sa_addr.pppoe.sid = 0;
270 memcpy(sp.sa_addr.pppoe.dev, conn->ifName, IFNAMSIZ);
271 memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN);
272 if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
273 sizeof(struct sockaddr_pppox)) < 0 && errno != EALREADY)
274 error("Failed to disconnect PPPoE socket: %d %m", errno);
275 close(conn->sessionSocket);
276 if (conn->discoverySocket >= 0) {
277 sendPADT(conn, NULL);
278 close(conn->discoverySocket);
283 PPPOEDeviceOptions(void)
286 snprintf(buf, 256, _PATH_ETHOPT "%s", devnam);
287 if (!options_from_file(buf, 0, 0, 1))
288 exit(EXIT_OPTION_ERROR);
292 struct channel pppoe_channel;
294 /**********************************************************************
295 * %FUNCTION: PPPoEDevnameHook
297 * cmd -- the command (actually, the device name
298 * argv -- argument vector
299 * doit -- if non-zero, set device name. Otherwise, just check if possible
301 * 1 if we will handle this device; 0 otherwise.
303 * Checks if name is a valid interface name; if so, returns 1. Also
304 * sets up devnam (string representation of device).
305 ***********************************************************************/
307 PPPoEDevnameHook(char *cmd, char **argv, int doit)
314 * Take any otherwise-unrecognized option as a possible device name,
315 * and test if it is the name of a network interface with a
316 * hardware address whose sa_family is ARPHRD_ETHER.
318 if (strlen(cmd) > 4 && !strncmp(cmd, "nic-", 4)) {
319 /* Strip off "nic-" */
324 if ((fd = socket(PF_PACKET, SOCK_RAW, 0)) < 0) {
328 /* Try getting interface index */
330 strncpy(ifr.ifr_name, cmd, sizeof(ifr.ifr_name));
331 if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
334 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
337 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
339 error("Interface %s not Ethernet", cmd);
349 strncpy(devnam, cmd, sizeof(devnam));
350 if (the_channel != &pppoe_channel) {
352 the_channel = &pppoe_channel;
363 /**********************************************************************
364 * %FUNCTION: plugin_init
370 * Initializes hooks for pppd plugin
371 ***********************************************************************/
375 if (!ppp_available() && !new_style_driver) {
376 fatal("Linux kernel does not support PPPoE -- are you running 2.4.x?");
379 add_options(Options);
381 info("RP-PPPoE plugin version %s compiled against pppd %s",
382 RP_VERSION, VERSION);
385 void pppoe_check_options(void)
390 if (pppoe_reqd_mac != NULL) {
391 if (sscanf(pppoe_reqd_mac, "%x:%x:%x:%x:%x:%x",
392 &mac[0], &mac[1], &mac[2], &mac[3],
393 &mac[4], &mac[5]) != 6) {
394 option_error("cannot parse pppoe-mac option value");
395 exit(EXIT_OPTION_ERROR);
397 for (i = 0; i < 6; ++i)
398 conn->req_peer_mac[i] = mac[i];
402 lcp_allowoptions[0].neg_accompression = 0;
403 lcp_wantoptions[0].neg_accompression = 0;
405 lcp_allowoptions[0].neg_asyncmap = 0;
406 lcp_wantoptions[0].neg_asyncmap = 0;
408 lcp_allowoptions[0].neg_pcompression = 0;
409 lcp_wantoptions[0].neg_pcompression = 0;
411 if (lcp_allowoptions[0].mru > MAX_PPPOE_MTU)
412 lcp_allowoptions[0].mru = MAX_PPPOE_MTU;
413 if (lcp_wantoptions[0].mru > MAX_PPPOE_MTU)
414 lcp_wantoptions[0].mru = MAX_PPPOE_MTU;
416 /* Save configuration */
417 conn->mtu = lcp_allowoptions[0].mru;
418 conn->mru = lcp_wantoptions[0].mru;
420 ccp_allowoptions[0].deflate = 0;
421 ccp_wantoptions[0].deflate = 0;
423 ipcp_allowoptions[0].neg_vj = 0;
424 ipcp_wantoptions[0].neg_vj = 0;
426 ccp_allowoptions[0].bsd_compress = 0;
427 ccp_wantoptions[0].bsd_compress = 0;
430 struct channel pppoe_channel = {
432 .process_extra_options = &PPPOEDeviceOptions,
433 .check_options = pppoe_check_options,
434 .connect = &PPPOEConnectDevice,
435 .disconnect = &PPPOEDisconnectDevice,
436 .establish_ppp = &generic_establish_ppp,
437 .disestablish_ppp = &generic_disestablish_ppp,
439 .recv_config = &PPPOERecvConfig,