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.
22 ***********************************************************************/
24 static char const RCSID[] =
25 "$Id: plugin.c,v 1.2 2002/01/22 16:03:05 dfs Exp $";
30 #include "pppd/pppd.h"
33 #include "pppd/ipcp.h"
35 #include "pppd/pathnames.h"
37 #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>
55 #define _PATH_ETHOPT _ROOT_PATH "/etc/ppp/options."
57 char pppd_version[] = VERSION;
59 /* From sys-linux.c in pppd -- MUST FIX THIS! */
60 extern int new_style_driver;
62 static char *service = NULL;
63 static char *acName = NULL;
64 static char *existingSession = NULL;
66 static int PPPoEDevnameHook(const char *name);
67 static option_t Options[] = {
68 { "device name", o_wild, (void *) &PPPoEDevnameHook,
70 OPT_DEVNAM | OPT_PRIVFIX | OPT_NOARG | OPT_A2STRVAL | OPT_STATIC,
72 { "rp_pppoe_service", o_string, &service,
73 "Desired PPPoE service name" },
74 { "rp_pppoe_ac", o_string, &acName,
75 "Desired PPPoE access concentrator name" },
76 { "rp_pppoe_sess", o_string, &existingSession,
77 "Attach to existing session (sessid:macaddr)" },
80 int (*OldDevnameHook)(const char *name) = NULL;
81 static PPPoEConnection *conn = NULL;
83 /**********************************************************************
84 * %FUNCTION: PPPOEInitDevice
90 * Initializes PPPoE device.
91 ***********************************************************************/
95 conn = malloc(sizeof(PPPoEConnection));
97 fatal("Could not allocate memory for PPPoE session");
99 memset(conn, 0, sizeof(PPPoEConnection));
101 SET_STRING(conn->acName, acName);
104 SET_STRING(conn->serviceName, acName);
106 SET_STRING(conn->ifName, devnam);
107 conn->discoverySocket = -1;
108 conn->sessionSocket = -1;
109 conn->useHostUniq = 1;
113 /**********************************************************************
114 * %FUNCTION: PPPOEConnectDevice
118 * Non-negative if all goes well; -1 otherwise
120 * Connects PPPoE device.
121 ***********************************************************************/
123 PPPOEConnectDevice(void)
125 struct sockaddr_pppox sp;
127 strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam));
128 if (existingSession) {
129 unsigned int mac[ETH_ALEN];
131 if (sscanf(existingSession, "%d:%x:%x:%x:%x:%x:%x",
132 &ses, &mac[0], &mac[1], &mac[2],
133 &mac[3], &mac[4], &mac[5]) != 7) {
134 fatal("Illegal value for rp_pppoe_sess option");
136 conn->session = htons(ses);
137 for (i=0; i<ETH_ALEN; i++) {
138 conn->peerEth[i] = (unsigned char) mac[i];
142 if (conn->discoveryState != STATE_SESSION) {
143 fatal("Unable to complete PPPoE Discovery");
147 /* Set PPPoE session-number for further consumption */
148 ppp_session_number = ntohs(conn->session);
150 /* Make the session socket */
151 conn->sessionSocket = socket(AF_PPPOX, SOCK_STREAM, PX_PROTO_OE);
152 if (conn->sessionSocket < 0) {
153 fatal("Failed to create PPPoE socket: %m");
155 sp.sa_family = AF_PPPOX;
156 sp.sa_protocol = PX_PROTO_OE;
157 sp.sa_addr.pppoe.sid = conn->session;
158 memcpy(sp.sa_addr.pppoe.dev, conn->ifName, IFNAMSIZ);
159 memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN);
161 /* Set remote_number for ServPoET */
162 sprintf(remote_number, "%02X:%02X:%02X:%02X:%02X:%02X",
163 (unsigned) conn->peerEth[0],
164 (unsigned) conn->peerEth[1],
165 (unsigned) conn->peerEth[2],
166 (unsigned) conn->peerEth[3],
167 (unsigned) conn->peerEth[4],
168 (unsigned) conn->peerEth[5]);
170 if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
171 sizeof(struct sockaddr_pppox)) < 0) {
172 fatal("Failed to connect PPPoE socket: %d %m", errno);
176 return conn->sessionSocket;
180 PPPOESendConfig(int mtu,
188 if (mtu > MAX_PPPOE_MTU) {
189 warn("Couldn't increase MTU to %d", mtu);
192 sock = socket(AF_INET, SOCK_DGRAM, 0);
194 fatal("Couldn't create IP socket: %m");
196 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
198 if (ioctl(sock, SIOCSIFMTU, &ifr) < 0) {
199 fatal("ioctl(SIOCSIFMTU): %m");
206 PPPOERecvConfig(int mru,
211 if (mru > MAX_PPPOE_MTU) {
212 error("Couldn't increase MRU to %d", mru);
216 /**********************************************************************
217 * %FUNCTION: PPPOEDisconnectDevice
223 * Disconnects PPPoE device
224 ***********************************************************************/
226 PPPOEDisconnectDevice(void)
228 struct sockaddr_pppox sp;
230 sp.sa_family = AF_PPPOX;
231 sp.sa_protocol = PX_PROTO_OE;
232 sp.sa_addr.pppoe.sid = 0;
233 memcpy(sp.sa_addr.pppoe.dev, conn->ifName, IFNAMSIZ);
234 memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN);
235 if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
236 sizeof(struct sockaddr_pppox)) < 0) {
237 fatal("Failed to disconnect PPPoE socket: %d %m", errno);
240 close(conn->sessionSocket);
244 PPPOEDeviceOptions(void)
247 snprintf(buf, 256, _PATH_ETHOPT "%s",devnam);
248 if(!options_from_file(buf, 0, 0, 1))
249 exit(EXIT_OPTION_ERROR);
253 struct channel pppoe_channel;
255 /**********************************************************************
256 * %FUNCTION: PPPoEDevnameHook
258 * name -- name of device
260 * 1 if we will handle this device; 0 otherwise.
262 * Checks if name is a valid interface name; if so, returns 1. Also
263 * sets up devnam (string representation of device).
264 ***********************************************************************/
266 PPPoEDevnameHook(const char *name)
273 if ((fd = socket(PF_PACKET, SOCK_RAW, 0)) < 0) {
277 /* Try getting interface index */
279 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
280 if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
283 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
286 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
287 error("Interface %s not Ethernet", name);
297 strncpy(devnam, name, sizeof(devnam));
298 if (the_channel != &pppoe_channel) {
300 the_channel = &pppoe_channel;
303 lcp_allowoptions[0].neg_accompression = 0;
304 lcp_wantoptions[0].neg_accompression = 0;
306 lcp_allowoptions[0].neg_asyncmap = 0;
307 lcp_wantoptions[0].neg_asyncmap = 0;
309 lcp_allowoptions[0].neg_pcompression = 0;
310 lcp_wantoptions[0].neg_pcompression = 0;
312 ccp_allowoptions[0].deflate = 0 ;
313 ccp_wantoptions[0].deflate = 0 ;
315 ipcp_allowoptions[0].neg_vj=0;
316 ipcp_wantoptions[0].neg_vj=0;
318 ccp_allowoptions[0].bsd_compress = 0;
319 ccp_wantoptions[0].bsd_compress = 0;
326 if (OldDevnameHook) r = OldDevnameHook(name);
330 /**********************************************************************
331 * %FUNCTION: plugin_init
337 * Initializes hooks for pppd plugin
338 ***********************************************************************/
342 if (!ppp_available() && !new_style_driver) {
343 fatal("Linux kernel does not support PPPoE -- are you running 2.4.x?");
346 add_options(Options);
348 info("RP-PPPoE plugin version %s compiled against pppd %s",
349 RP_VERSION, VERSION);
352 /**********************************************************************
355 * str -- error message
359 * Prints a message plus the errno value to stderr and syslog and exits.
360 ***********************************************************************/
362 fatalSys(char const *str)
366 sprintf(buf, "%.256s: %.256s", str, strerror(i));
368 sprintf(buf, "RP-PPPoE: %.256s: %.256s", str, strerror(i));
373 /**********************************************************************
376 * str -- error message
380 * Prints a message to stderr and syslog and exits.
381 ***********************************************************************/
383 rp_fatal(char const *str)
387 sprintf(buf, "RP-PPPoE: %.256s", str);
391 /**********************************************************************
394 * str -- error message
398 * Prints a message plus the errno value to syslog.
399 ***********************************************************************/
401 sysErr(char const *str)
407 struct channel pppoe_channel = {
409 process_extra_options: &PPPOEDeviceOptions,
411 connect: &PPPOEConnectDevice,
412 disconnect: &PPPOEDisconnectDevice,
413 establish_ppp: &generic_establish_ppp,
414 disestablish_ppp: &generic_disestablish_ppp,
415 send_config: &PPPOESendConfig,
416 recv_config: &PPPOERecvConfig,