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->ifName = devnam;
112 conn->discoverySocket = -1;
113 conn->sessionSocket = -1;
114 conn->useHostUniq = 1;
115 conn->printACNames = printACNames;
116 conn->discoveryTimeout = PADI_TIMEOUT;
120 /**********************************************************************
121 * %FUNCTION: PPPOEConnectDevice
125 * Non-negative if all goes well; -1 otherwise
127 * Connects PPPoE device.
128 ***********************************************************************/
130 PPPOEConnectDevice(void)
132 struct sockaddr_pppox sp;
136 /* Restore configuration */
137 lcp_allowoptions[0].mru = conn->mtu;
138 lcp_wantoptions[0].mru = conn->mru;
140 /* Update maximum MRU */
141 s = socket(AF_INET, SOCK_DGRAM, 0);
143 error("Can't get MTU for %s: %m", conn->ifName);
146 strncpy(ifr.ifr_name, conn->ifName, sizeof(ifr.ifr_name));
147 if (ioctl(s, SIOCGIFMTU, &ifr) < 0) {
148 error("Can't get MTU for %s: %m", conn->ifName);
154 if (lcp_allowoptions[0].mru > ifr.ifr_mtu - TOTAL_OVERHEAD)
155 lcp_allowoptions[0].mru = ifr.ifr_mtu - TOTAL_OVERHEAD;
156 if (lcp_wantoptions[0].mru > ifr.ifr_mtu - TOTAL_OVERHEAD)
157 lcp_wantoptions[0].mru = ifr.ifr_mtu - TOTAL_OVERHEAD;
159 conn->acName = acName;
160 conn->serviceName = pppd_pppoe_service;
161 strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam));
162 if (existingSession) {
163 unsigned int mac[ETH_ALEN];
165 if (sscanf(existingSession, "%d:%x:%x:%x:%x:%x:%x",
166 &ses, &mac[0], &mac[1], &mac[2],
167 &mac[3], &mac[4], &mac[5]) != 7) {
168 fatal("Illegal value for rp_pppoe_sess option");
170 conn->session = htons(ses);
171 for (i=0; i<ETH_ALEN; i++) {
172 conn->peerEth[i] = (unsigned char) mac[i];
176 if (conn->discoveryState != STATE_SESSION) {
177 error("Unable to complete PPPoE Discovery");
182 /* Set PPPoE session-number for further consumption */
183 ppp_session_number = ntohs(conn->session);
185 /* Make the session socket */
186 conn->sessionSocket = socket(AF_PPPOX, SOCK_STREAM, PX_PROTO_OE);
187 if (conn->sessionSocket < 0) {
188 error("Failed to create PPPoE socket: %m");
192 sp.sa_family = AF_PPPOX;
193 sp.sa_protocol = PX_PROTO_OE;
194 sp.sa_addr.pppoe.sid = conn->session;
195 memcpy(sp.sa_addr.pppoe.dev, conn->ifName, IFNAMSIZ);
196 memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN);
198 /* Set remote_number for ServPoET */
199 sprintf(remote_number, "%02X:%02X:%02X:%02X:%02X:%02X",
200 (unsigned) conn->peerEth[0],
201 (unsigned) conn->peerEth[1],
202 (unsigned) conn->peerEth[2],
203 (unsigned) conn->peerEth[3],
204 (unsigned) conn->peerEth[4],
205 (unsigned) conn->peerEth[5]);
207 warn("Connected to %02X:%02X:%02X:%02X:%02X:%02X via interface %s",
208 (unsigned) conn->peerEth[0],
209 (unsigned) conn->peerEth[1],
210 (unsigned) conn->peerEth[2],
211 (unsigned) conn->peerEth[3],
212 (unsigned) conn->peerEth[4],
213 (unsigned) conn->peerEth[5],
216 script_setenv("MACREMOTE", remote_number, 0);
218 if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
219 sizeof(struct sockaddr_pppox)) < 0) {
220 error("Failed to connect PPPoE socket: %d %m", errno);
221 close(conn->sessionSocket);
225 return conn->sessionSocket;
228 if (conn->discoverySocket >= 0) {
229 sendPADT(conn, NULL);
230 close(conn->discoverySocket);
231 conn->discoverySocket = -1;
237 PPPOERecvConfig(int mru,
242 #if 0 /* broken protocol, but no point harrassing the users I guess... */
243 if (mru > MAX_PPPOE_MTU)
244 warn("Couldn't increase MRU to %d", mru);
248 /**********************************************************************
249 * %FUNCTION: PPPOEDisconnectDevice
255 * Disconnects PPPoE device
256 ***********************************************************************/
258 PPPOEDisconnectDevice(void)
260 struct sockaddr_pppox sp;
262 sp.sa_family = AF_PPPOX;
263 sp.sa_protocol = PX_PROTO_OE;
264 sp.sa_addr.pppoe.sid = 0;
265 memcpy(sp.sa_addr.pppoe.dev, conn->ifName, IFNAMSIZ);
266 memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN);
267 if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
268 sizeof(struct sockaddr_pppox)) < 0)
269 error("Failed to disconnect PPPoE socket: %d %m", errno);
270 close(conn->sessionSocket);
271 /* don't send PADT?? */
272 if (conn->discoverySocket >= 0)
273 close(conn->discoverySocket);
277 PPPOEDeviceOptions(void)
280 snprintf(buf, 256, _PATH_ETHOPT "%s", devnam);
281 if (!options_from_file(buf, 0, 0, 1))
282 exit(EXIT_OPTION_ERROR);
286 struct channel pppoe_channel;
288 /**********************************************************************
289 * %FUNCTION: PPPoEDevnameHook
291 * cmd -- the command (actually, the device name
292 * argv -- argument vector
293 * doit -- if non-zero, set device name. Otherwise, just check if possible
295 * 1 if we will handle this device; 0 otherwise.
297 * Checks if name is a valid interface name; if so, returns 1. Also
298 * sets up devnam (string representation of device).
299 ***********************************************************************/
301 PPPoEDevnameHook(char *cmd, char **argv, int doit)
308 * Take any otherwise-unrecognized option as a possible device name,
309 * and test if it is the name of a network interface with a
310 * hardware address whose sa_family is ARPHRD_ETHER.
312 if (strlen(cmd) > 4 && !strncmp(cmd, "nic-", 4)) {
313 /* Strip off "nic-" */
318 if ((fd = socket(PF_PACKET, SOCK_RAW, 0)) < 0) {
322 /* Try getting interface index */
324 strncpy(ifr.ifr_name, cmd, sizeof(ifr.ifr_name));
325 if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
328 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
331 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
333 error("Interface %s not Ethernet", cmd);
343 strncpy(devnam, cmd, sizeof(devnam));
344 if (the_channel != &pppoe_channel) {
346 the_channel = &pppoe_channel;
357 /**********************************************************************
358 * %FUNCTION: plugin_init
364 * Initializes hooks for pppd plugin
365 ***********************************************************************/
369 if (!ppp_available() && !new_style_driver) {
370 fatal("Linux kernel does not support PPPoE -- are you running 2.4.x?");
373 add_options(Options);
375 info("RP-PPPoE plugin version %s compiled against pppd %s",
376 RP_VERSION, VERSION);
379 void pppoe_check_options(void)
384 if (pppoe_reqd_mac != NULL) {
385 if (sscanf(pppoe_reqd_mac, "%x:%x:%x:%x:%x:%x",
386 &mac[0], &mac[1], &mac[2], &mac[3],
387 &mac[4], &mac[5]) != 6) {
388 option_error("cannot parse pppoe-mac option value");
389 exit(EXIT_OPTION_ERROR);
391 for (i = 0; i < 6; ++i)
392 conn->req_peer_mac[i] = mac[i];
396 lcp_allowoptions[0].neg_accompression = 0;
397 lcp_wantoptions[0].neg_accompression = 0;
399 lcp_allowoptions[0].neg_asyncmap = 0;
400 lcp_wantoptions[0].neg_asyncmap = 0;
402 lcp_allowoptions[0].neg_pcompression = 0;
403 lcp_wantoptions[0].neg_pcompression = 0;
405 if (lcp_allowoptions[0].mru > MAX_PPPOE_MTU)
406 lcp_allowoptions[0].mru = MAX_PPPOE_MTU;
407 if (lcp_wantoptions[0].mru > MAX_PPPOE_MTU)
408 lcp_wantoptions[0].mru = MAX_PPPOE_MTU;
410 /* Save configuration */
411 conn->mtu = lcp_allowoptions[0].mru;
412 conn->mru = lcp_wantoptions[0].mru;
414 ccp_allowoptions[0].deflate = 0;
415 ccp_wantoptions[0].deflate = 0;
417 ipcp_allowoptions[0].neg_vj = 0;
418 ipcp_wantoptions[0].neg_vj = 0;
420 ccp_allowoptions[0].bsd_compress = 0;
421 ccp_wantoptions[0].bsd_compress = 0;
424 struct channel pppoe_channel = {
426 .process_extra_options = &PPPOEDeviceOptions,
427 .check_options = pppoe_check_options,
428 .connect = &PPPOEConnectDevice,
429 .disconnect = &PPPOEDisconnectDevice,
430 .establish_ppp = &generic_establish_ppp,
431 .disestablish_ppp = &generic_disestablish_ppp,
433 .recv_config = &PPPOERecvConfig,