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.1 2001/12/14 02:55:20 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 /* From sys-linux.c in pppd -- MUST FIX THIS! */
58 extern int new_style_driver;
60 static char *service = NULL;
61 static char *acName = NULL;
62 static char *existingSession = NULL;
64 static int PPPoEDevnameHook(const char *name);
65 static option_t Options[] = {
66 { "device name", o_wild, (void *) &PPPoEDevnameHook,
68 OPT_DEVNAM | OPT_PRIVFIX | OPT_NOARG | OPT_A2STRVAL | OPT_STATIC,
70 { "rp_pppoe_service", o_string, &service,
71 "Desired PPPoE service name" },
72 { "rp_pppoe_ac", o_string, &acName,
73 "Desired PPPoE access concentrator name" },
74 { "rp_pppoe_sess", o_string, &existingSession,
75 "Attach to existing session (sessid:macaddr)" },
78 int (*OldDevnameHook)(const char *name) = NULL;
79 static PPPoEConnection *conn = NULL;
81 /**********************************************************************
82 * %FUNCTION: PPPOEInitDevice
88 * Initializes PPPoE device.
89 ***********************************************************************/
93 conn = malloc(sizeof(PPPoEConnection));
95 fatal("Could not allocate memory for PPPoE session");
97 memset(conn, 0, sizeof(PPPoEConnection));
99 SET_STRING(conn->acName, acName);
102 SET_STRING(conn->serviceName, acName);
104 SET_STRING(conn->ifName, devnam);
105 conn->discoverySocket = -1;
106 conn->sessionSocket = -1;
107 conn->useHostUniq = 1;
111 /**********************************************************************
112 * %FUNCTION: PPPOEConnectDevice
116 * Non-negative if all goes well; -1 otherwise
118 * Connects PPPoE device.
119 ***********************************************************************/
121 PPPOEConnectDevice(void)
123 struct sockaddr_pppox sp;
125 strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam));
126 if (existingSession) {
127 unsigned int mac[ETH_ALEN];
129 if (sscanf(existingSession, "%d:%x:%x:%x:%x:%x:%x",
130 &ses, &mac[0], &mac[1], &mac[2],
131 &mac[3], &mac[4], &mac[5]) != 7) {
132 fatal("Illegal value for rp_pppoe_sess option");
134 conn->session = htons(ses);
135 for (i=0; i<ETH_ALEN; i++) {
136 conn->peerEth[i] = (unsigned char) mac[i];
140 if (conn->discoveryState != STATE_SESSION) {
141 fatal("Unable to complete PPPoE Discovery");
146 /* Set PPPoE session-number for further consumption */
147 pppd_pppoe_session = 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]);
171 if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
172 sizeof(struct sockaddr_pppox)) < 0) {
173 fatal("Failed to connect PPPoE socket: %d %m", errno);
177 return conn->sessionSocket;
181 PPPOESendConfig(int mtu,
189 if (mtu > MAX_PPPOE_MTU) {
190 warn("Couldn't increase MTU to %d", mtu);
193 sock = socket(AF_INET, SOCK_DGRAM, 0);
195 fatal("Couldn't create IP socket: %m");
197 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
199 if (ioctl(sock, SIOCSIFMTU, &ifr) < 0) {
200 fatal("ioctl(SIOCSIFMTU): %m");
207 PPPOERecvConfig(int mru,
212 if (mru > MAX_PPPOE_MTU) {
213 error("Couldn't increase MRU to %d", mru);
217 /**********************************************************************
218 * %FUNCTION: PPPOEDisconnectDevice
224 * Disconnects PPPoE device
225 ***********************************************************************/
227 PPPOEDisconnectDevice(void)
229 struct sockaddr_pppox sp;
231 sp.sa_family = AF_PPPOX;
232 sp.sa_protocol = PX_PROTO_OE;
233 sp.sa_addr.pppoe.sid = 0;
234 memcpy(sp.sa_addr.pppoe.dev, conn->ifName, IFNAMSIZ);
235 memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN);
236 if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
237 sizeof(struct sockaddr_pppox)) < 0) {
238 fatal("Failed to disconnect PPPoE socket: %d %m", errno);
241 close(conn->sessionSocket);
245 PPPOEDeviceOptions(void)
248 snprintf(buf, 256, _PATH_ETHOPT "%s",devnam);
249 if(!options_from_file(buf, 0, 0, 1))
250 exit(EXIT_OPTION_ERROR);
254 struct channel pppoe_channel;
256 /**********************************************************************
257 * %FUNCTION: PPPoEDevnameHook
259 * name -- name of device
261 * 1 if we will handle this device; 0 otherwise.
263 * Checks if name is a valid interface name; if so, returns 1. Also
264 * sets up devnam (string representation of device).
265 ***********************************************************************/
267 PPPoEDevnameHook(const char *name)
274 if ((fd = socket(PF_PACKET, SOCK_RAW, 0)) < 0) {
278 /* Try getting interface index */
280 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
281 if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
284 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
287 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
288 error("Interface %s not Ethernet", name);
298 strncpy(devnam, name, sizeof(devnam));
299 if (the_channel != &pppoe_channel) {
301 the_channel = &pppoe_channel;
304 lcp_allowoptions[0].neg_accompression = 0;
305 lcp_wantoptions[0].neg_accompression = 0;
307 lcp_allowoptions[0].neg_asyncmap = 0;
308 lcp_wantoptions[0].neg_asyncmap = 0;
310 lcp_allowoptions[0].neg_pcompression = 0;
311 lcp_wantoptions[0].neg_pcompression = 0;
313 ccp_allowoptions[0].deflate = 0 ;
314 ccp_wantoptions[0].deflate = 0 ;
316 ipcp_allowoptions[0].neg_vj=0;
317 ipcp_wantoptions[0].neg_vj=0;
319 ccp_allowoptions[0].bsd_compress = 0;
320 ccp_wantoptions[0].bsd_compress = 0;
327 if (OldDevnameHook) r = OldDevnameHook(name);
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 /**********************************************************************
356 * str -- error message
360 * Prints a message plus the errno value to stderr and syslog and exits.
361 ***********************************************************************/
363 fatalSys(char const *str)
367 sprintf(buf, "%.256s: %.256s", str, strerror(i));
369 sprintf(buf, "RP-PPPoE: %.256s: %.256s", str, strerror(i));
374 /**********************************************************************
377 * str -- error message
381 * Prints a message to stderr and syslog and exits.
382 ***********************************************************************/
384 rp_fatal(char const *str)
388 sprintf(buf, "RP-PPPoE: %.256s", str);
392 /**********************************************************************
395 * str -- error message
399 * Prints a message plus the errno value to syslog.
400 ***********************************************************************/
402 sysErr(char const *str)
408 struct channel pppoe_channel = {
410 process_extra_options: &PPPOEDeviceOptions,
412 connect: &PPPOEConnectDevice,
413 disconnect: &PPPOEDisconnectDevice,
414 establish_ppp: &generic_establish_ppp,
415 disestablish_ppp: &generic_disestablish_ppp,
416 send_config: &PPPOESendConfig,
417 recv_config: &PPPOERecvConfig,