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