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_ppp.h>
53 #include <linux/if_pppox.h>
59 #define _PATH_ETHOPT _ROOT_PATH "/etc/ppp/options."
61 char pppd_version[] = VERSION;
63 /* From sys-linux.c in pppd -- MUST FIX THIS! */
64 extern int new_style_driver;
66 char *pppd_pppoe_service = NULL;
67 static char *acName = NULL;
68 static char *existingSession = NULL;
69 static int printACNames = 0;
70 static char *pppoe_reqd_mac = NULL;
71 unsigned char pppoe_reqd_mac_addr[6];
73 static int PPPoEDevnameHook(char *cmd, char **argv, int doit);
74 static option_t Options[] = {
75 { "device name", o_wild, (void *) &PPPoEDevnameHook,
77 OPT_DEVNAM | OPT_PRIVFIX | OPT_NOARG | OPT_A2STRVAL | OPT_STATIC,
79 { "rp_pppoe_service", o_string, &pppd_pppoe_service,
80 "Desired PPPoE service name" },
81 { "rp_pppoe_ac", o_string, &acName,
82 "Desired PPPoE access concentrator name" },
83 { "rp_pppoe_sess", o_string, &existingSession,
84 "Attach to existing session (sessid:macaddr)" },
85 { "rp_pppoe_verbose", o_int, &printACNames,
86 "Be verbose about discovered access concentrators"},
87 { "pppoe-mac", o_string, &pppoe_reqd_mac,
88 "Only connect to specified MAC address" },
91 int (*OldDevnameHook)(char *cmd, char **argv, int doit) = NULL;
92 static PPPoEConnection *conn = NULL;
94 /**********************************************************************
95 * %FUNCTION: PPPOEInitDevice
101 * Initializes PPPoE device.
102 ***********************************************************************/
104 PPPOEInitDevice(void)
106 conn = malloc(sizeof(PPPoEConnection));
108 novm("PPPoE session data");
110 memset(conn, 0, sizeof(PPPoEConnection));
111 conn->acName = acName;
112 conn->serviceName = pppd_pppoe_service;
113 conn->ifName = devnam;
114 conn->discoverySocket = -1;
115 conn->sessionSocket = -1;
116 conn->useHostUniq = 1;
117 conn->printACNames = printACNames;
118 conn->discoveryTimeout = PADI_TIMEOUT;
122 /**********************************************************************
123 * %FUNCTION: PPPOEConnectDevice
127 * Non-negative if all goes well; -1 otherwise
129 * Connects PPPoE device.
130 ***********************************************************************/
132 PPPOEConnectDevice(void)
134 struct sockaddr_pppox sp;
136 strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam));
137 if (existingSession) {
138 unsigned int mac[ETH_ALEN];
140 if (sscanf(existingSession, "%d:%x:%x:%x:%x:%x:%x",
141 &ses, &mac[0], &mac[1], &mac[2],
142 &mac[3], &mac[4], &mac[5]) != 7) {
143 fatal("Illegal value for rp_pppoe_sess option");
145 conn->session = htons(ses);
146 for (i=0; i<ETH_ALEN; i++) {
147 conn->peerEth[i] = (unsigned char) mac[i];
151 if (conn->discoveryState != STATE_SESSION) {
152 error("Unable to complete PPPoE Discovery");
157 /* Set PPPoE session-number for further consumption */
158 ppp_session_number = ntohs(conn->session);
160 /* Make the session socket */
161 conn->sessionSocket = socket(AF_PPPOX, SOCK_STREAM, PX_PROTO_OE);
162 if (conn->sessionSocket < 0) {
163 error("Failed to create PPPoE socket: %m");
166 sp.sa_family = AF_PPPOX;
167 sp.sa_protocol = PX_PROTO_OE;
168 sp.sa_addr.pppoe.sid = conn->session;
169 memcpy(sp.sa_addr.pppoe.dev, conn->ifName, IFNAMSIZ);
170 memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN);
172 /* Set remote_number for ServPoET */
173 sprintf(remote_number, "%02X:%02X:%02X:%02X:%02X:%02X",
174 (unsigned) conn->peerEth[0],
175 (unsigned) conn->peerEth[1],
176 (unsigned) conn->peerEth[2],
177 (unsigned) conn->peerEth[3],
178 (unsigned) conn->peerEth[4],
179 (unsigned) conn->peerEth[5]);
181 warn("Connected to %02X:%02X:%02X:%02X:%02X:%02X via interface %s",
182 (unsigned) conn->peerEth[0],
183 (unsigned) conn->peerEth[1],
184 (unsigned) conn->peerEth[2],
185 (unsigned) conn->peerEth[3],
186 (unsigned) conn->peerEth[4],
187 (unsigned) conn->peerEth[5],
190 script_setenv("MACREMOTE", remote_number, 0);
192 if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
193 sizeof(struct sockaddr_pppox)) < 0) {
194 error("Failed to connect PPPoE socket: %d %m", errno);
195 close(conn->sessionSocket);
199 return conn->sessionSocket;
202 if (conn->discoverySocket >= 0) {
203 sendPADT(conn, NULL);
204 close(conn->discoverySocket);
205 conn->discoverySocket = -1;
211 PPPOERecvConfig(int mru,
216 #if 0 /* broken protocol, but no point harrassing the users I guess... */
217 if (mru > MAX_PPPOE_MTU)
218 warn("Couldn't increase MRU to %d", mru);
222 /**********************************************************************
223 * %FUNCTION: PPPOEDisconnectDevice
229 * Disconnects PPPoE device
230 ***********************************************************************/
232 PPPOEDisconnectDevice(void)
234 struct sockaddr_pppox sp;
236 sp.sa_family = AF_PPPOX;
237 sp.sa_protocol = PX_PROTO_OE;
238 sp.sa_addr.pppoe.sid = 0;
239 memcpy(sp.sa_addr.pppoe.dev, conn->ifName, IFNAMSIZ);
240 memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN);
241 if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
242 sizeof(struct sockaddr_pppox)) < 0)
243 error("Failed to disconnect PPPoE socket: %d %m", errno);
244 close(conn->sessionSocket);
245 /* don't send PADT?? */
246 if (conn->discoverySocket >= 0)
247 close(conn->discoverySocket);
251 PPPOEDeviceOptions(void)
254 snprintf(buf, 256, _PATH_ETHOPT "%s", devnam);
255 if (!options_from_file(buf, 0, 0, 1))
256 exit(EXIT_OPTION_ERROR);
260 struct channel pppoe_channel;
262 /**********************************************************************
263 * %FUNCTION: PPPoEDevnameHook
265 * cmd -- the command (actually, the device name
266 * argv -- argument vector
267 * doit -- if non-zero, set device name. Otherwise, just check if possible
269 * 1 if we will handle this device; 0 otherwise.
271 * Checks if name is a valid interface name; if so, returns 1. Also
272 * sets up devnam (string representation of device).
273 ***********************************************************************/
275 PPPoEDevnameHook(char *cmd, char **argv, int doit)
282 * Take any otherwise-unrecognized option as a possible device name,
283 * and test if it is the name of a network interface with a
284 * hardware address whose sa_family is ARPHRD_ETHER.
286 if (strlen(cmd) > 4 && !strncmp(cmd, "nic-", 4)) {
287 /* Strip off "nic-" */
292 if ((fd = socket(PF_PACKET, SOCK_RAW, 0)) < 0) {
296 /* Try getting interface index */
298 strncpy(ifr.ifr_name, cmd, sizeof(ifr.ifr_name));
299 if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
302 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
305 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
307 error("Interface %s not Ethernet", cmd);
317 strncpy(devnam, cmd, sizeof(devnam));
318 if (the_channel != &pppoe_channel) {
320 the_channel = &pppoe_channel;
331 /**********************************************************************
332 * %FUNCTION: plugin_init
338 * Initializes hooks for pppd plugin
339 ***********************************************************************/
343 if (!ppp_available() && !new_style_driver) {
344 fatal("Linux kernel does not support PPPoE -- are you running 2.4.x?");
347 add_options(Options);
349 info("RP-PPPoE plugin version %s compiled against pppd %s",
350 RP_VERSION, VERSION);
353 void pppoe_check_options(void)
358 if (pppoe_reqd_mac != NULL) {
359 if (sscanf(pppoe_reqd_mac, "%x:%x:%x:%x:%x:%x",
360 &mac[0], &mac[1], &mac[2], &mac[3],
361 &mac[4], &mac[5]) != 6) {
362 option_error("cannot parse pppoe-mac option value");
363 exit(EXIT_OPTION_ERROR);
365 for (i = 0; i < 6; ++i)
366 conn->req_peer_mac[i] = mac[i];
370 lcp_allowoptions[0].neg_accompression = 0;
371 lcp_wantoptions[0].neg_accompression = 0;
373 lcp_allowoptions[0].neg_asyncmap = 0;
374 lcp_wantoptions[0].neg_asyncmap = 0;
376 lcp_allowoptions[0].neg_pcompression = 0;
377 lcp_wantoptions[0].neg_pcompression = 0;
379 if (lcp_allowoptions[0].mru > MAX_PPPOE_MTU)
380 lcp_allowoptions[0].mru = MAX_PPPOE_MTU;
381 if (lcp_wantoptions[0].mru > MAX_PPPOE_MTU)
382 lcp_wantoptions[0].mru = MAX_PPPOE_MTU;
384 ccp_allowoptions[0].deflate = 0;
385 ccp_wantoptions[0].deflate = 0;
387 ipcp_allowoptions[0].neg_vj = 0;
388 ipcp_wantoptions[0].neg_vj = 0;
390 ccp_allowoptions[0].bsd_compress = 0;
391 ccp_wantoptions[0].bsd_compress = 0;
394 struct channel pppoe_channel = {
396 .process_extra_options = &PPPOEDeviceOptions,
397 .check_options = pppoe_check_options,
398 .connect = &PPPOEConnectDevice,
399 .disconnect = &PPPOEDisconnectDevice,
400 .establish_ppp = &generic_establish_ppp,
401 .disestablish_ppp = &generic_disestablish_ppp,
403 .recv_config = &PPPOERecvConfig,