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.3 2002/02/12 04:36:00 mostrows 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;
65 static int printACNames = 0;
67 static int PPPoEDevnameHook(const char *name);
68 static option_t Options[] = {
69 { "device name", o_wild, (void *) &PPPoEDevnameHook,
71 OPT_DEVNAM | OPT_PRIVFIX | OPT_NOARG | OPT_A2STRVAL | OPT_STATIC,
73 { "rp_pppoe_service", o_string, &service,
74 "Desired PPPoE service name" },
75 { "rp_pppoe_ac", o_string, &acName,
76 "Desired PPPoE access concentrator name" },
77 { "rp_pppoe_sess", o_string, &existingSession,
78 "Attach to existing session (sessid:macaddr)" },
79 { "rp_pppoe_verbose", o_int, &printACNames,
80 "Be verbose about discovered access concentrators"},
83 int (*OldDevnameHook)(const char *name) = NULL;
84 static PPPoEConnection *conn = NULL;
86 /**********************************************************************
87 * %FUNCTION: PPPOEInitDevice
93 * Initializes PPPoE device.
94 ***********************************************************************/
98 conn = malloc(sizeof(PPPoEConnection));
100 fatal("Could not allocate memory for PPPoE session");
102 memset(conn, 0, sizeof(PPPoEConnection));
104 SET_STRING(conn->acName, acName);
107 SET_STRING(conn->serviceName, acName);
109 SET_STRING(conn->ifName, devnam);
110 conn->discoverySocket = -1;
111 conn->sessionSocket = -1;
112 conn->useHostUniq = 1;
113 conn->printACNames = printACNames;
117 /**********************************************************************
118 * %FUNCTION: PPPOEConnectDevice
122 * Non-negative if all goes well; -1 otherwise
124 * Connects PPPoE device.
125 ***********************************************************************/
127 PPPOEConnectDevice(void)
129 struct sockaddr_pppox sp;
131 strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam));
132 if (existingSession) {
133 unsigned int mac[ETH_ALEN];
135 if (sscanf(existingSession, "%d:%x:%x:%x:%x:%x:%x",
136 &ses, &mac[0], &mac[1], &mac[2],
137 &mac[3], &mac[4], &mac[5]) != 7) {
138 fatal("Illegal value for rp_pppoe_sess option");
140 conn->session = htons(ses);
141 for (i=0; i<ETH_ALEN; i++) {
142 conn->peerEth[i] = (unsigned char) mac[i];
146 if (conn->discoveryState != STATE_SESSION) {
147 fatal("Unable to complete PPPoE Discovery");
151 /* Set PPPoE session-number for further consumption */
152 ppp_session_number = ntohs(conn->session);
154 /* Make the session socket */
155 conn->sessionSocket = socket(AF_PPPOX, SOCK_STREAM, PX_PROTO_OE);
156 if (conn->sessionSocket < 0) {
157 fatal("Failed to create PPPoE socket: %m");
159 sp.sa_family = AF_PPPOX;
160 sp.sa_protocol = PX_PROTO_OE;
161 sp.sa_addr.pppoe.sid = conn->session;
162 memcpy(sp.sa_addr.pppoe.dev, conn->ifName, IFNAMSIZ);
163 memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN);
165 /* Set remote_number for ServPoET */
166 sprintf(remote_number, "%02X:%02X:%02X:%02X:%02X:%02X",
167 (unsigned) conn->peerEth[0],
168 (unsigned) conn->peerEth[1],
169 (unsigned) conn->peerEth[2],
170 (unsigned) conn->peerEth[3],
171 (unsigned) conn->peerEth[4],
172 (unsigned) conn->peerEth[5]);
174 if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
175 sizeof(struct sockaddr_pppox)) < 0) {
176 fatal("Failed to connect PPPoE socket: %d %m", errno);
180 return conn->sessionSocket;
184 PPPOESendConfig(int mtu,
192 if (mtu > MAX_PPPOE_MTU) {
193 warn("Couldn't increase MTU to %d", mtu);
196 sock = socket(AF_INET, SOCK_DGRAM, 0);
198 fatal("Couldn't create IP socket: %m");
200 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
202 if (ioctl(sock, SIOCSIFMTU, &ifr) < 0) {
203 fatal("ioctl(SIOCSIFMTU): %m");
210 PPPOERecvConfig(int mru,
215 if (mru > MAX_PPPOE_MTU) {
216 error("Couldn't increase MRU to %d", mru);
220 /**********************************************************************
221 * %FUNCTION: PPPOEDisconnectDevice
227 * Disconnects PPPoE device
228 ***********************************************************************/
230 PPPOEDisconnectDevice(void)
232 struct sockaddr_pppox sp;
234 sp.sa_family = AF_PPPOX;
235 sp.sa_protocol = PX_PROTO_OE;
236 sp.sa_addr.pppoe.sid = 0;
237 memcpy(sp.sa_addr.pppoe.dev, conn->ifName, IFNAMSIZ);
238 memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN);
239 if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
240 sizeof(struct sockaddr_pppox)) < 0) {
241 fatal("Failed to disconnect PPPoE socket: %d %m", errno);
244 close(conn->sessionSocket);
248 PPPOEDeviceOptions(void)
251 snprintf(buf, 256, _PATH_ETHOPT "%s",devnam);
252 if(!options_from_file(buf, 0, 0, 1))
253 exit(EXIT_OPTION_ERROR);
257 struct channel pppoe_channel;
259 /**********************************************************************
260 * %FUNCTION: PPPoEDevnameHook
262 * name -- name of device
264 * 1 if we will handle this device; 0 otherwise.
266 * Checks if name is a valid interface name; if so, returns 1. Also
267 * sets up devnam (string representation of device).
268 ***********************************************************************/
270 PPPoEDevnameHook(const char *name)
277 if ((fd = socket(PF_PACKET, SOCK_RAW, 0)) < 0) {
281 /* Try getting interface index */
283 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
284 if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
287 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
290 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
291 error("Interface %s not Ethernet", name);
301 strncpy(devnam, name, sizeof(devnam));
302 if (the_channel != &pppoe_channel) {
304 the_channel = &pppoe_channel;
307 lcp_allowoptions[0].neg_accompression = 0;
308 lcp_wantoptions[0].neg_accompression = 0;
310 lcp_allowoptions[0].neg_asyncmap = 0;
311 lcp_wantoptions[0].neg_asyncmap = 0;
313 lcp_allowoptions[0].neg_pcompression = 0;
314 lcp_wantoptions[0].neg_pcompression = 0;
316 ccp_allowoptions[0].deflate = 0 ;
317 ccp_wantoptions[0].deflate = 0 ;
319 ipcp_allowoptions[0].neg_vj=0;
320 ipcp_wantoptions[0].neg_vj=0;
322 ccp_allowoptions[0].bsd_compress = 0;
323 ccp_wantoptions[0].bsd_compress = 0;
330 if (OldDevnameHook) r = OldDevnameHook(name);
334 /**********************************************************************
335 * %FUNCTION: plugin_init
341 * Initializes hooks for pppd plugin
342 ***********************************************************************/
346 if (!ppp_available() && !new_style_driver) {
347 fatal("Linux kernel does not support PPPoE -- are you running 2.4.x?");
350 add_options(Options);
352 info("RP-PPPoE plugin version %s compiled against pppd %s",
353 RP_VERSION, VERSION);
356 /**********************************************************************
359 * str -- error message
363 * Prints a message plus the errno value to stderr and syslog and exits.
364 ***********************************************************************/
366 fatalSys(char const *str)
370 sprintf(buf, "%.256s: %.256s", str, strerror(i));
372 sprintf(buf, "RP-PPPoE: %.256s: %.256s", str, strerror(i));
377 /**********************************************************************
380 * str -- error message
384 * Prints a message to stderr and syslog and exits.
385 ***********************************************************************/
387 rp_fatal(char const *str)
391 sprintf(buf, "RP-PPPoE: %.256s", str);
395 /**********************************************************************
398 * str -- error message
402 * Prints a message plus the errno value to syslog.
403 ***********************************************************************/
405 sysErr(char const *str)
411 struct channel pppoe_channel = {
413 process_extra_options: &PPPOEDeviceOptions,
415 connect: &PPPOEConnectDevice,
416 disconnect: &PPPOEDisconnectDevice,
417 establish_ppp: &generic_establish_ppp,
418 disestablish_ppp: &generic_disestablish_ppp,
419 send_config: &PPPOESendConfig,
420 recv_config: &PPPOERecvConfig,