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 ***********************************************************************/
32 #include <linux/types.h>
33 #include <sys/ioctl.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
37 #include <sys/param.h>
44 #include <net/if_arp.h>
45 #include <linux/ppp_defs.h>
46 #include <linux/if_pppox.h>
48 #include <pppd/pppd.h>
49 #include <pppd/options.h>
52 #include <pppd/ipcp.h>
55 char pppd_version[] = PPPD_VERSION;
57 /* From sys-linux.c in pppd -- MUST FIX THIS! */
58 extern int new_style_driver;
60 char *pppd_pppoe_service = NULL;
61 static char *acName = NULL;
62 static char *existingSession = NULL;
63 int pppoe_verbose = 0;
64 static char *pppoe_reqd_mac = NULL;
65 unsigned char pppoe_reqd_mac_addr[6];
66 static char *pppoe_host_uniq;
67 static int pppoe_padi_timeout = PADI_TIMEOUT;
68 static int pppoe_padi_attempts = MAX_PADI_ATTEMPTS;
69 static char devnam[MAXNAMELEN];
71 static int PPPoEDevnameHook(char *cmd, char **argv, int doit);
72 static struct option Options[] = {
73 { "device name", o_wild, (void *) &PPPoEDevnameHook,
75 OPT_DEVNAM | OPT_PRIVFIX | OPT_NOARG | OPT_A2STRVAL | OPT_STATIC,
77 { "pppoe-service", o_string, &pppd_pppoe_service,
78 "Desired PPPoE service name" },
79 { "rp_pppoe_service", o_string, &pppd_pppoe_service,
80 "Legacy alias for pppoe-service", OPT_ALIAS },
81 { "pppoe-ac", o_string, &acName,
82 "Desired PPPoE access concentrator name" },
83 { "rp_pppoe_ac", o_string, &acName,
84 "Legacy alias for pppoe-ac", OPT_ALIAS },
85 { "pppoe-sess", o_string, &existingSession,
86 "Attach to existing session (sessid:macaddr)" },
87 { "rp_pppoe_sess", o_string, &existingSession,
88 "Legacy alias for pppoe-sess", OPT_ALIAS },
89 { "pppoe-verbose", o_int, &pppoe_verbose,
90 "Be verbose about discovered access concentrators" },
91 { "rp_pppoe_verbose", o_int, &pppoe_verbose,
92 "Legacy alias for pppoe-verbose", OPT_ALIAS },
93 { "pppoe-mac", o_string, &pppoe_reqd_mac,
94 "Only connect to specified MAC address" },
95 { "pppoe-host-uniq", o_string, &pppoe_host_uniq,
96 "Set the Host-Uniq to the supplied hex string" },
97 { "host-uniq", o_string, &pppoe_host_uniq,
98 "Legacy alias for pppoe-host-uniq", OPT_ALIAS },
99 { "pppoe-padi-timeout", o_int, &pppoe_padi_timeout,
100 "Initial timeout for discovery packets in seconds" },
101 { "pppoe-padi-attempts", o_int, &pppoe_padi_attempts,
102 "Number of discovery attempts" },
105 int (*OldDevnameHook)(char *cmd, char **argv, int doit) = NULL;
106 static PPPoEConnection *conn = NULL;
108 /**********************************************************************
109 * %FUNCTION: PPPOEInitDevice
115 * Initializes PPPoE device.
116 ***********************************************************************/
118 PPPOEInitDevice(void)
120 conn = malloc(sizeof(PPPoEConnection));
122 novm("PPPoE session data");
124 memset(conn, 0, sizeof(PPPoEConnection));
125 conn->ifName = devnam;
126 conn->discoverySocket = -1;
127 conn->sessionSocket = -1;
131 /**********************************************************************
132 * %FUNCTION: PPPOEConnectDevice
136 * Non-negative if all goes well; -1 otherwise
138 * Connects PPPoE device.
139 ***********************************************************************/
141 PPPOEConnectDevice(void)
143 struct sockaddr_pppox sp;
146 char remote_number[MAXNAMELEN];
148 /* Open session socket before discovery phase, to avoid losing session */
149 /* packets sent by peer just after PADS packet (noted on some Cisco */
150 /* server equipment). */
151 /* Opening this socket just before waitForPADS in the discovery() */
152 /* function would be more appropriate, but it would mess-up the code */
153 conn->sessionSocket = socket(AF_PPPOX, SOCK_STREAM, PX_PROTO_OE);
154 if (conn->sessionSocket < 0) {
155 error("Failed to create PPPoE socket: %m");
159 /* Restore configuration */
160 lcp_allowoptions[0].mru = conn->mtu = conn->storedmtu;
161 lcp_wantoptions[0].mru = conn->mru = conn->storedmru;
163 /* Update maximum MRU */
164 s = socket(AF_INET, SOCK_DGRAM, 0);
166 error("Can't get MTU for %s: %m", conn->ifName);
169 strlcpy(ifr.ifr_name, conn->ifName, sizeof(ifr.ifr_name));
170 if (ioctl(s, SIOCGIFMTU, &ifr) < 0) {
171 error("Can't get MTU for %s: %m", conn->ifName);
177 if (lcp_allowoptions[0].mru > ifr.ifr_mtu - TOTAL_OVERHEAD)
178 lcp_allowoptions[0].mru = conn->mtu = ifr.ifr_mtu - TOTAL_OVERHEAD;
179 if (lcp_wantoptions[0].mru > ifr.ifr_mtu - TOTAL_OVERHEAD)
180 lcp_wantoptions[0].mru = conn->mru = ifr.ifr_mtu - TOTAL_OVERHEAD;
182 if (pppoe_host_uniq) {
183 if (!parseHostUniq(pppoe_host_uniq, &conn->hostUniq))
184 fatal("Illegal value for pppoe-host-uniq option");
186 /* if a custom host-uniq is not supplied, use our PID */
187 pid_t pid = getpid();
188 conn->hostUniq.type = htons(TAG_HOST_UNIQ);
189 conn->hostUniq.length = htons(sizeof(pid));
190 memcpy(conn->hostUniq.payload, &pid, sizeof(pid));
193 conn->acName = acName;
194 conn->serviceName = pppd_pppoe_service;
195 ppp_set_pppdevnam(devnam);
196 if (existingSession) {
197 unsigned int mac[ETH_ALEN];
199 if (sscanf(existingSession, "%d:%x:%x:%x:%x:%x:%x",
200 &ses, &mac[0], &mac[1], &mac[2],
201 &mac[3], &mac[4], &mac[5]) != 7) {
202 fatal("Illegal value for pppoe-sess option");
204 conn->session = htons(ses);
205 for (i=0; i<ETH_ALEN; i++) {
206 conn->peerEth[i] = (unsigned char) mac[i];
209 conn->discoverySocket =
210 openInterface(conn->ifName, Eth_PPPOE_Discovery, conn->myEth);
211 if (conn->discoverySocket < 0) {
212 error("Failed to create PPPoE discovery socket: %m");
216 /* discovery1() may update conn->mtu and conn->mru */
217 lcp_allowoptions[0].mru = conn->mtu;
218 lcp_wantoptions[0].mru = conn->mru;
219 if (conn->discoveryState != STATE_RECEIVED_PADO) {
220 error("Unable to complete PPPoE Discovery phase 1");
224 /* discovery2() may update conn->mtu and conn->mru */
225 lcp_allowoptions[0].mru = conn->mtu;
226 lcp_wantoptions[0].mru = conn->mru;
227 if (conn->discoveryState != STATE_SESSION) {
228 error("Unable to complete PPPoE Discovery phase 2");
233 /* Set PPPoE session-number for further consumption */
234 ppp_set_session_number(ntohs(conn->session));
236 sp.sa_family = AF_PPPOX;
237 sp.sa_protocol = PX_PROTO_OE;
238 sp.sa_addr.pppoe.sid = conn->session;
239 memcpy(sp.sa_addr.pppoe.dev, conn->ifName, IFNAMSIZ);
240 memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN);
242 /* Set remote_number for ServPoET */
243 sprintf(remote_number, "%02X:%02X:%02X:%02X:%02X:%02X",
244 (unsigned) conn->peerEth[0],
245 (unsigned) conn->peerEth[1],
246 (unsigned) conn->peerEth[2],
247 (unsigned) conn->peerEth[3],
248 (unsigned) conn->peerEth[4],
249 (unsigned) conn->peerEth[5]);
250 warn("Connected to %s via interface %s", remote_number, conn->ifName);
251 ppp_set_remote_number(remote_number);
253 ppp_script_setenv("MACREMOTE", remote_number, 0);
254 if (conn->actualACname)
255 ppp_script_setenv("ACNAME", conn->actualACname, 0);
257 if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
258 sizeof(struct sockaddr_pppox)) < 0) {
259 error("Failed to connect PPPoE socket: %d %m", errno);
263 return conn->sessionSocket;
266 if (conn->discoverySocket >= 0) {
267 sendPADT(conn, NULL);
268 close(conn->discoverySocket);
269 conn->discoverySocket = -1;
271 close(conn->sessionSocket);
276 PPPOERecvConfig(int mru,
281 #if 0 /* broken protocol, but no point harrassing the users I guess... */
282 if (mru > MAX_PPPOE_MTU)
283 warn("Couldn't increase MRU to %d", mru);
287 /**********************************************************************
288 * %FUNCTION: PPPOEDisconnectDevice
294 * Disconnects PPPoE device
295 ***********************************************************************/
297 PPPOEDisconnectDevice(void)
299 struct sockaddr_pppox sp;
301 sp.sa_family = AF_PPPOX;
302 sp.sa_protocol = PX_PROTO_OE;
303 sp.sa_addr.pppoe.sid = 0;
304 memcpy(sp.sa_addr.pppoe.dev, conn->ifName, IFNAMSIZ);
305 memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN);
306 if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
307 sizeof(struct sockaddr_pppox)) < 0 && errno != EALREADY)
308 error("Failed to disconnect PPPoE socket: %d %m", errno);
309 close(conn->sessionSocket);
310 if (conn->discoverySocket < 0)
311 conn->discoverySocket =
312 openInterface(conn->ifName, Eth_PPPOE_Discovery, NULL);
313 if (conn->discoverySocket >= 0) {
314 sendPADT(conn, NULL);
315 close(conn->discoverySocket);
317 free(conn->actualACname);
318 conn->actualACname = NULL;
322 PPPOEDeviceOptions(void)
324 char name[MAXPATHLEN];
325 char buf[MAXPATHLEN];
327 slprintf(name, sizeof(name), "options.%s", devnam);
328 if (ppp_get_filepath(PPP_DIR_CONF, name, buf, sizeof(buf)) < sizeof(buf)) {
329 if (!ppp_options_from_file(buf, 0, 0, 1)) {
330 exit(EXIT_OPTION_ERROR);
333 exit(EXIT_OPTION_ERROR);
337 struct channel pppoe_channel;
339 /**********************************************************************
340 * %FUNCTION: PPPoEDevnameHook
342 * cmd -- the command (actually, the device name
343 * argv -- argument vector
344 * doit -- if non-zero, set device name. Otherwise, just check if possible
346 * 1 if we will handle this device; 0 otherwise.
348 * Checks if name is a valid interface name; if so, returns 1. Also
349 * sets up devnam (string representation of device).
350 ***********************************************************************/
352 PPPoEDevnameHook(char *cmd, char **argv, int doit)
359 * Take any otherwise-unrecognized option as a possible device name,
360 * and test if it is the name of a network interface with a
361 * hardware address whose sa_family is ARPHRD_ETHER.
363 if (strlen(cmd) > 4 && !strncmp(cmd, "nic-", 4)) {
364 /* Strip off "nic-" */
369 if ((fd = socket(PF_PACKET, SOCK_RAW, 0)) < 0) {
373 /* Try getting interface index */
375 strlcpy(ifr.ifr_name, cmd, sizeof(ifr.ifr_name));
376 if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
379 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
382 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
384 error("Interface %s not Ethernet", cmd);
394 strlcpy(devnam, cmd, sizeof(devnam));
395 if (the_channel != &pppoe_channel) {
397 the_channel = &pppoe_channel;
402 ppp_set_devnam(devnam);
409 /**********************************************************************
410 * %FUNCTION: plugin_init
416 * Initializes hooks for pppd plugin
417 ***********************************************************************/
421 if (!ppp_check_kernel_support() && !new_style_driver) {
422 fatal("Linux kernel does not support PPPoE -- are you running 2.4.x?");
425 ppp_add_options(Options);
427 info("PPPoE plugin from pppd %s", PPPD_VERSION);
430 void pppoe_check_options(void)
435 if (pppoe_reqd_mac != NULL) {
436 if (sscanf(pppoe_reqd_mac, "%x:%x:%x:%x:%x:%x",
437 &mac[0], &mac[1], &mac[2], &mac[3],
438 &mac[4], &mac[5]) != 6) {
439 ppp_option_error("cannot parse pppoe-mac option value");
440 exit(EXIT_OPTION_ERROR);
442 for (i = 0; i < 6; ++i)
443 conn->req_peer_mac[i] = mac[i];
447 lcp_allowoptions[0].neg_accompression = 0;
448 lcp_wantoptions[0].neg_accompression = 0;
450 lcp_allowoptions[0].neg_asyncmap = 0;
451 lcp_wantoptions[0].neg_asyncmap = 0;
453 lcp_allowoptions[0].neg_pcompression = 0;
454 lcp_wantoptions[0].neg_pcompression = 0;
456 if (lcp_allowoptions[0].mru > MAX_PPPOE_MTU)
457 lcp_allowoptions[0].mru = MAX_PPPOE_MTU;
458 if (lcp_wantoptions[0].mru > MAX_PPPOE_MTU)
459 lcp_wantoptions[0].mru = MAX_PPPOE_MTU;
461 /* Save configuration */
462 conn->storedmtu = lcp_allowoptions[0].mru;
463 conn->storedmru = lcp_wantoptions[0].mru;
465 ccp_allowoptions[0].deflate = 0;
466 ccp_wantoptions[0].deflate = 0;
468 ipcp_allowoptions[0].neg_vj = 0;
469 ipcp_wantoptions[0].neg_vj = 0;
471 ccp_allowoptions[0].bsd_compress = 0;
472 ccp_wantoptions[0].bsd_compress = 0;
474 conn->discoveryTimeout = pppoe_padi_timeout;
475 conn->discoveryAttempts = pppoe_padi_attempts;
478 struct channel pppoe_channel = {
480 .process_extra_options = &PPPOEDeviceOptions,
481 .check_options = pppoe_check_options,
482 .connect = &PPPOEConnectDevice,
483 .disconnect = &PPPOEDisconnectDevice,
484 .establish_ppp = &ppp_generic_establish,
485 .disestablish_ppp = &ppp_generic_disestablish,
487 .recv_config = &PPPOERecvConfig,