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