]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/rp-pppoe/plugin.c
Merge branch 'plog-usability' of https://github.com/mityada/ppp
[ppp.git] / pppd / plugins / rp-pppoe / plugin.c
1 /***********************************************************************
2 *
3 * plugin.c
4 *
5 * pppd plugin for kernel-mode PPPoE on Linux
6 *
7 * Copyright (C) 2001 by Roaring Penguin Software Inc., Michal Ostrowski
8 * and Jamal Hadi Salim.
9 *
10 * Much code and many ideas derived from pppoe plugin by Michal
11 * Ostrowski and Jamal Hadi Salim, which carries this copyright:
12 *
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.
17 *
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 *
23 ***********************************************************************/
24
25 static char const RCSID[] =
26 "$Id: plugin.c,v 1.17 2008/06/15 04:35:50 paulus Exp $";
27
28 #define _GNU_SOURCE 1
29 #include "pppoe.h"
30
31 #include "pppd/pppd.h"
32 #include "pppd/fsm.h"
33 #include "pppd/lcp.h"
34 #include "pppd/ipcp.h"
35 #include "pppd/ccp.h"
36 /* #include "pppd/pathnames.h" */
37
38 #include <linux/types.h>
39 #include <sys/ioctl.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <sys/stat.h>
43 #include <string.h>
44 #include <stdlib.h>
45 #include <errno.h>
46 #include <unistd.h>
47 #include <fcntl.h>
48 #include <signal.h>
49 #include <net/ethernet.h>
50 #include <net/if_arp.h>
51 #include <linux/ppp_defs.h>
52 #include <linux/if_pppox.h>
53
54 #ifndef _ROOT_PATH
55 #define _ROOT_PATH ""
56 #endif
57
58 #define _PATH_ETHOPT         _ROOT_PATH "/etc/ppp/options."
59
60 char pppd_version[] = VERSION;
61
62 /* From sys-linux.c in pppd -- MUST FIX THIS! */
63 extern int new_style_driver;
64
65 char *pppd_pppoe_service = NULL;
66 static char *acName = NULL;
67 static char *existingSession = NULL;
68 static int printACNames = 0;
69 static char *pppoe_reqd_mac = NULL;
70 unsigned char pppoe_reqd_mac_addr[6];
71
72 static int PPPoEDevnameHook(char *cmd, char **argv, int doit);
73 static option_t Options[] = {
74     { "device name", o_wild, (void *) &PPPoEDevnameHook,
75       "PPPoE device name",
76       OPT_DEVNAM | OPT_PRIVFIX | OPT_NOARG  | OPT_A2STRVAL | OPT_STATIC,
77       devnam},
78     { "rp_pppoe_service", o_string, &pppd_pppoe_service,
79       "Desired PPPoE service name" },
80     { "rp_pppoe_ac",      o_string, &acName,
81       "Desired PPPoE access concentrator name" },
82     { "rp_pppoe_sess",    o_string, &existingSession,
83       "Attach to existing session (sessid:macaddr)" },
84     { "rp_pppoe_verbose", o_int, &printACNames,
85       "Be verbose about discovered access concentrators"},
86     { "pppoe-mac", o_string, &pppoe_reqd_mac,
87       "Only connect to specified MAC address" },
88     { NULL }
89 };
90 int (*OldDevnameHook)(char *cmd, char **argv, int doit) = NULL;
91 static PPPoEConnection *conn = NULL;
92
93 /**********************************************************************
94  * %FUNCTION: PPPOEInitDevice
95  * %ARGUMENTS:
96  * None
97  * %RETURNS:
98  *
99  * %DESCRIPTION:
100  * Initializes PPPoE device.
101  ***********************************************************************/
102 static int
103 PPPOEInitDevice(void)
104 {
105     conn = malloc(sizeof(PPPoEConnection));
106     if (!conn) {
107         novm("PPPoE session data");
108     }
109     memset(conn, 0, sizeof(PPPoEConnection));
110     conn->ifName = devnam;
111     conn->discoverySocket = -1;
112     conn->sessionSocket = -1;
113     conn->useHostUniq = 1;
114     conn->printACNames = printACNames;
115     conn->discoveryTimeout = PADI_TIMEOUT;
116     return 1;
117 }
118
119 /**********************************************************************
120  * %FUNCTION: PPPOEConnectDevice
121  * %ARGUMENTS:
122  * None
123  * %RETURNS:
124  * Non-negative if all goes well; -1 otherwise
125  * %DESCRIPTION:
126  * Connects PPPoE device.
127  ***********************************************************************/
128 static int
129 PPPOEConnectDevice(void)
130 {
131     struct sockaddr_pppox sp;
132     struct ifreq ifr;
133     int s;
134
135     /* Open session socket before discovery phase, to avoid losing session */
136     /* packets sent by peer just after PADS packet (noted on some Cisco    */
137     /* server equipment).                                                  */
138     /* Opening this socket just before waitForPADS in the discovery()      */
139     /* function would be more appropriate, but it would mess-up the code   */
140     conn->sessionSocket = socket(AF_PPPOX, SOCK_STREAM, PX_PROTO_OE);
141     if (conn->sessionSocket < 0) {
142         error("Failed to create PPPoE socket: %m");
143         return -1;
144     }
145
146     /* Restore configuration */
147     lcp_allowoptions[0].mru = conn->mtu;
148     lcp_wantoptions[0].mru = conn->mru;
149
150     /* Update maximum MRU */
151     s = socket(AF_INET, SOCK_DGRAM, 0);
152     if (s < 0) {
153         error("Can't get MTU for %s: %m", conn->ifName);
154         goto errout;
155     }
156     strncpy(ifr.ifr_name, conn->ifName, sizeof(ifr.ifr_name));
157     if (ioctl(s, SIOCGIFMTU, &ifr) < 0) {
158         error("Can't get MTU for %s: %m", conn->ifName);
159         close(s);
160         goto errout;
161     }
162     close(s);
163
164     if (lcp_allowoptions[0].mru > ifr.ifr_mtu - TOTAL_OVERHEAD)
165         lcp_allowoptions[0].mru = ifr.ifr_mtu - TOTAL_OVERHEAD;
166     if (lcp_wantoptions[0].mru > ifr.ifr_mtu - TOTAL_OVERHEAD)
167         lcp_wantoptions[0].mru = ifr.ifr_mtu - TOTAL_OVERHEAD;
168
169     conn->acName = acName;
170     conn->serviceName = pppd_pppoe_service;
171     strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam));
172     if (existingSession) {
173         unsigned int mac[ETH_ALEN];
174         int i, ses;
175         if (sscanf(existingSession, "%d:%x:%x:%x:%x:%x:%x",
176                    &ses, &mac[0], &mac[1], &mac[2],
177                    &mac[3], &mac[4], &mac[5]) != 7) {
178             fatal("Illegal value for rp_pppoe_sess option");
179         }
180         conn->session = htons(ses);
181         for (i=0; i<ETH_ALEN; i++) {
182             conn->peerEth[i] = (unsigned char) mac[i];
183         }
184     } else {
185         conn->discoverySocket =
186             openInterface(conn->ifName, Eth_PPPOE_Discovery, conn->myEth);
187         discovery(conn);
188         if (conn->discoveryState != STATE_SESSION) {
189             error("Unable to complete PPPoE Discovery");
190             goto errout;
191         }
192     }
193
194     /* Set PPPoE session-number for further consumption */
195     ppp_session_number = ntohs(conn->session);
196
197     sp.sa_family = AF_PPPOX;
198     sp.sa_protocol = PX_PROTO_OE;
199     sp.sa_addr.pppoe.sid = conn->session;
200     memcpy(sp.sa_addr.pppoe.dev, conn->ifName, IFNAMSIZ);
201     memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN);
202
203     /* Set remote_number for ServPoET */
204     sprintf(remote_number, "%02X:%02X:%02X:%02X:%02X:%02X",
205             (unsigned) conn->peerEth[0],
206             (unsigned) conn->peerEth[1],
207             (unsigned) conn->peerEth[2],
208             (unsigned) conn->peerEth[3],
209             (unsigned) conn->peerEth[4],
210             (unsigned) conn->peerEth[5]);
211
212     warn("Connected to %02X:%02X:%02X:%02X:%02X:%02X via interface %s",
213          (unsigned) conn->peerEth[0],
214          (unsigned) conn->peerEth[1],
215          (unsigned) conn->peerEth[2],
216          (unsigned) conn->peerEth[3],
217          (unsigned) conn->peerEth[4],
218          (unsigned) conn->peerEth[5],
219          conn->ifName);
220
221     script_setenv("MACREMOTE", remote_number, 0);
222
223     if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
224                 sizeof(struct sockaddr_pppox)) < 0) {
225         error("Failed to connect PPPoE socket: %d %m", errno);
226         goto errout;
227     }
228
229     return conn->sessionSocket;
230
231  errout:
232     if (conn->discoverySocket >= 0) {
233         sendPADT(conn, NULL);
234         close(conn->discoverySocket);
235         conn->discoverySocket = -1;
236     }
237     close(conn->sessionSocket);
238     return -1;
239 }
240
241 static void
242 PPPOERecvConfig(int mru,
243                 u_int32_t asyncmap,
244                 int pcomp,
245                 int accomp)
246 {
247 #if 0 /* broken protocol, but no point harrassing the users I guess... */
248     if (mru > MAX_PPPOE_MTU)
249         warn("Couldn't increase MRU to %d", mru);
250 #endif
251 }
252
253 /**********************************************************************
254  * %FUNCTION: PPPOEDisconnectDevice
255  * %ARGUMENTS:
256  * None
257  * %RETURNS:
258  * Nothing
259  * %DESCRIPTION:
260  * Disconnects PPPoE device
261  ***********************************************************************/
262 static void
263 PPPOEDisconnectDevice(void)
264 {
265     struct sockaddr_pppox sp;
266
267     sp.sa_family = AF_PPPOX;
268     sp.sa_protocol = PX_PROTO_OE;
269     sp.sa_addr.pppoe.sid = 0;
270     memcpy(sp.sa_addr.pppoe.dev, conn->ifName, IFNAMSIZ);
271     memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN);
272     if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
273                 sizeof(struct sockaddr_pppox)) < 0 && errno != EALREADY)
274         error("Failed to disconnect PPPoE socket: %d %m", errno);
275     close(conn->sessionSocket);
276     if (conn->discoverySocket >= 0) {
277         sendPADT(conn, NULL);
278         close(conn->discoverySocket);
279     }
280 }
281
282 static void
283 PPPOEDeviceOptions(void)
284 {
285     char buf[256];
286     snprintf(buf, 256, _PATH_ETHOPT "%s", devnam);
287     if (!options_from_file(buf, 0, 0, 1))
288         exit(EXIT_OPTION_ERROR);
289
290 }
291
292 struct channel pppoe_channel;
293
294 /**********************************************************************
295  * %FUNCTION: PPPoEDevnameHook
296  * %ARGUMENTS:
297  * cmd -- the command (actually, the device name
298  * argv -- argument vector
299  * doit -- if non-zero, set device name.  Otherwise, just check if possible
300  * %RETURNS:
301  * 1 if we will handle this device; 0 otherwise.
302  * %DESCRIPTION:
303  * Checks if name is a valid interface name; if so, returns 1.  Also
304  * sets up devnam (string representation of device).
305  ***********************************************************************/
306 static int
307 PPPoEDevnameHook(char *cmd, char **argv, int doit)
308 {
309     int r = 1;
310     int fd;
311     struct ifreq ifr;
312
313     /*
314      * Take any otherwise-unrecognized option as a possible device name,
315      * and test if it is the name of a network interface with a
316      * hardware address whose sa_family is ARPHRD_ETHER.
317      */
318     if (strlen(cmd) > 4 && !strncmp(cmd, "nic-", 4)) {
319         /* Strip off "nic-" */
320         cmd += 4;
321     }
322
323     /* Open a socket */
324     if ((fd = socket(PF_PACKET, SOCK_RAW, 0)) < 0) {
325         r = 0;
326     }
327
328     /* Try getting interface index */
329     if (r) {
330         strncpy(ifr.ifr_name, cmd, sizeof(ifr.ifr_name));
331         if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
332             r = 0;
333         } else {
334             if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
335                 r = 0;
336             } else {
337                 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
338                     if (doit)
339                         error("Interface %s not Ethernet", cmd);
340                     r = 0;
341                 }
342             }
343         }
344     }
345
346     /* Close socket */
347     close(fd);
348     if (r && doit) {
349         strncpy(devnam, cmd, sizeof(devnam));
350         if (the_channel != &pppoe_channel) {
351
352             the_channel = &pppoe_channel;
353             modem = 0;
354
355             PPPOEInitDevice();
356         }
357         return 1;
358     }
359
360     return r;
361 }
362
363 /**********************************************************************
364  * %FUNCTION: plugin_init
365  * %ARGUMENTS:
366  * None
367  * %RETURNS:
368  * Nothing
369  * %DESCRIPTION:
370  * Initializes hooks for pppd plugin
371  ***********************************************************************/
372 void
373 plugin_init(void)
374 {
375     if (!ppp_available() && !new_style_driver) {
376         fatal("Linux kernel does not support PPPoE -- are you running 2.4.x?");
377     }
378
379     add_options(Options);
380
381     info("RP-PPPoE plugin version %s compiled against pppd %s",
382          RP_VERSION, VERSION);
383 }
384
385 void pppoe_check_options(void)
386 {
387     unsigned int mac[6];
388     int i;
389
390     if (pppoe_reqd_mac != NULL) {
391         if (sscanf(pppoe_reqd_mac, "%x:%x:%x:%x:%x:%x",
392                    &mac[0], &mac[1], &mac[2], &mac[3],
393                    &mac[4], &mac[5]) != 6) {
394             option_error("cannot parse pppoe-mac option value");
395             exit(EXIT_OPTION_ERROR);
396         }
397         for (i = 0; i < 6; ++i)
398             conn->req_peer_mac[i] = mac[i];
399         conn->req_peer = 1;
400     }
401
402     lcp_allowoptions[0].neg_accompression = 0;
403     lcp_wantoptions[0].neg_accompression = 0;
404
405     lcp_allowoptions[0].neg_asyncmap = 0;
406     lcp_wantoptions[0].neg_asyncmap = 0;
407
408     lcp_allowoptions[0].neg_pcompression = 0;
409     lcp_wantoptions[0].neg_pcompression = 0;
410
411     if (lcp_allowoptions[0].mru > MAX_PPPOE_MTU)
412         lcp_allowoptions[0].mru = MAX_PPPOE_MTU;
413     if (lcp_wantoptions[0].mru > MAX_PPPOE_MTU)
414         lcp_wantoptions[0].mru = MAX_PPPOE_MTU;
415
416     /* Save configuration */
417     conn->mtu = lcp_allowoptions[0].mru;
418     conn->mru = lcp_wantoptions[0].mru;
419
420     ccp_allowoptions[0].deflate = 0;
421     ccp_wantoptions[0].deflate = 0;
422
423     ipcp_allowoptions[0].neg_vj = 0;
424     ipcp_wantoptions[0].neg_vj = 0;
425
426     ccp_allowoptions[0].bsd_compress = 0;
427     ccp_wantoptions[0].bsd_compress = 0;
428 }
429
430 struct channel pppoe_channel = {
431     .options = Options,
432     .process_extra_options = &PPPOEDeviceOptions,
433     .check_options = pppoe_check_options,
434     .connect = &PPPOEConnectDevice,
435     .disconnect = &PPPOEDisconnectDevice,
436     .establish_ppp = &generic_establish_ppp,
437     .disestablish_ppp = &generic_disestablish_ppp,
438     .send_config = NULL,
439     .recv_config = &PPPOERecvConfig,
440     .close = NULL,
441     .cleanup = NULL
442 };