]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/rp-pppoe/plugin.c
Add pppoe-mac option to rp-pppoe plugin
[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->acName = acName;
112     conn->serviceName = pppd_pppoe_service;
113     conn->ifName = devnam;
114     conn->discoverySocket = -1;
115     conn->sessionSocket = -1;
116     conn->useHostUniq = 1;
117     conn->printACNames = printACNames;
118     conn->discoveryTimeout = PADI_TIMEOUT;
119     return 1;
120 }
121
122 /**********************************************************************
123  * %FUNCTION: PPPOEConnectDevice
124  * %ARGUMENTS:
125  * None
126  * %RETURNS:
127  * Non-negative if all goes well; -1 otherwise
128  * %DESCRIPTION:
129  * Connects PPPoE device.
130  ***********************************************************************/
131 static int
132 PPPOEConnectDevice(void)
133 {
134     struct sockaddr_pppox sp;
135
136     strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam));
137     if (existingSession) {
138         unsigned int mac[ETH_ALEN];
139         int i, ses;
140         if (sscanf(existingSession, "%d:%x:%x:%x:%x:%x:%x",
141                    &ses, &mac[0], &mac[1], &mac[2],
142                    &mac[3], &mac[4], &mac[5]) != 7) {
143             fatal("Illegal value for rp_pppoe_sess option");
144         }
145         conn->session = htons(ses);
146         for (i=0; i<ETH_ALEN; i++) {
147             conn->peerEth[i] = (unsigned char) mac[i];
148         }
149     } else {
150         discovery(conn);
151         if (conn->discoveryState != STATE_SESSION) {
152             error("Unable to complete PPPoE Discovery");
153             return -1;
154         }
155     }
156
157     /* Set PPPoE session-number for further consumption */
158     ppp_session_number = ntohs(conn->session);
159
160     /* Make the session socket */
161     conn->sessionSocket = socket(AF_PPPOX, SOCK_STREAM, PX_PROTO_OE);
162     if (conn->sessionSocket < 0) {
163         error("Failed to create PPPoE socket: %m");
164         goto errout;
165     }
166     sp.sa_family = AF_PPPOX;
167     sp.sa_protocol = PX_PROTO_OE;
168     sp.sa_addr.pppoe.sid = conn->session;
169     memcpy(sp.sa_addr.pppoe.dev, conn->ifName, IFNAMSIZ);
170     memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN);
171
172     /* Set remote_number for ServPoET */
173     sprintf(remote_number, "%02X:%02X:%02X:%02X:%02X:%02X",
174             (unsigned) conn->peerEth[0],
175             (unsigned) conn->peerEth[1],
176             (unsigned) conn->peerEth[2],
177             (unsigned) conn->peerEth[3],
178             (unsigned) conn->peerEth[4],
179             (unsigned) conn->peerEth[5]);
180
181     warn("Connected to %02X:%02X:%02X:%02X:%02X:%02X via interface %s",
182          (unsigned) conn->peerEth[0],
183          (unsigned) conn->peerEth[1],
184          (unsigned) conn->peerEth[2],
185          (unsigned) conn->peerEth[3],
186          (unsigned) conn->peerEth[4],
187          (unsigned) conn->peerEth[5],
188          conn->ifName);
189
190     script_setenv("MACREMOTE", remote_number, 0);
191
192     if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
193                 sizeof(struct sockaddr_pppox)) < 0) {
194         error("Failed to connect PPPoE socket: %d %m", errno);
195         close(conn->sessionSocket);
196         goto errout;
197     }
198
199     return conn->sessionSocket;
200
201  errout:
202     if (conn->discoverySocket >= 0) {
203         sendPADT(conn, NULL);
204         close(conn->discoverySocket);
205         conn->discoverySocket = -1;
206     }
207     return -1;
208 }
209
210 static void
211 PPPOERecvConfig(int mru,
212                 u_int32_t asyncmap,
213                 int pcomp,
214                 int accomp)
215 {
216 #if 0 /* broken protocol, but no point harrassing the users I guess... */
217     if (mru > MAX_PPPOE_MTU)
218         warn("Couldn't increase MRU to %d", mru);
219 #endif
220 }
221
222 /**********************************************************************
223  * %FUNCTION: PPPOEDisconnectDevice
224  * %ARGUMENTS:
225  * None
226  * %RETURNS:
227  * Nothing
228  * %DESCRIPTION:
229  * Disconnects PPPoE device
230  ***********************************************************************/
231 static void
232 PPPOEDisconnectDevice(void)
233 {
234     struct sockaddr_pppox sp;
235
236     sp.sa_family = AF_PPPOX;
237     sp.sa_protocol = PX_PROTO_OE;
238     sp.sa_addr.pppoe.sid = 0;
239     memcpy(sp.sa_addr.pppoe.dev, conn->ifName, IFNAMSIZ);
240     memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN);
241     if (connect(conn->sessionSocket, (struct sockaddr *) &sp,
242                 sizeof(struct sockaddr_pppox)) < 0)
243         error("Failed to disconnect PPPoE socket: %d %m", errno);
244     close(conn->sessionSocket);
245     /* don't send PADT?? */
246     if (conn->discoverySocket >= 0)
247         close(conn->discoverySocket);
248 }
249
250 static void
251 PPPOEDeviceOptions(void)
252 {
253     char buf[256];
254     snprintf(buf, 256, _PATH_ETHOPT "%s", devnam);
255     if (!options_from_file(buf, 0, 0, 1))
256         exit(EXIT_OPTION_ERROR);
257
258 }
259
260 struct channel pppoe_channel;
261
262 /**********************************************************************
263  * %FUNCTION: PPPoEDevnameHook
264  * %ARGUMENTS:
265  * cmd -- the command (actually, the device name
266  * argv -- argument vector
267  * doit -- if non-zero, set device name.  Otherwise, just check if possible
268  * %RETURNS:
269  * 1 if we will handle this device; 0 otherwise.
270  * %DESCRIPTION:
271  * Checks if name is a valid interface name; if so, returns 1.  Also
272  * sets up devnam (string representation of device).
273  ***********************************************************************/
274 static int
275 PPPoEDevnameHook(char *cmd, char **argv, int doit)
276 {
277     int r = 1;
278     int fd;
279     struct ifreq ifr;
280
281     /*
282      * Take any otherwise-unrecognized option as a possible device name,
283      * and test if it is the name of a network interface with a
284      * hardware address whose sa_family is ARPHRD_ETHER.
285      */
286     if (strlen(cmd) > 4 && !strncmp(cmd, "nic-", 4)) {
287         /* Strip off "nic-" */
288         cmd += 4;
289     }
290
291     /* Open a socket */
292     if ((fd = socket(PF_PACKET, SOCK_RAW, 0)) < 0) {
293         r = 0;
294     }
295
296     /* Try getting interface index */
297     if (r) {
298         strncpy(ifr.ifr_name, cmd, sizeof(ifr.ifr_name));
299         if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
300             r = 0;
301         } else {
302             if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
303                 r = 0;
304             } else {
305                 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
306                     if (doit)
307                         error("Interface %s not Ethernet", cmd);
308                     r = 0;
309                 }
310             }
311         }
312     }
313
314     /* Close socket */
315     close(fd);
316     if (r && doit) {
317         strncpy(devnam, cmd, sizeof(devnam));
318         if (the_channel != &pppoe_channel) {
319
320             the_channel = &pppoe_channel;
321             modem = 0;
322
323             PPPOEInitDevice();
324         }
325         return 1;
326     }
327
328     return r;
329 }
330
331 /**********************************************************************
332  * %FUNCTION: plugin_init
333  * %ARGUMENTS:
334  * None
335  * %RETURNS:
336  * Nothing
337  * %DESCRIPTION:
338  * Initializes hooks for pppd plugin
339  ***********************************************************************/
340 void
341 plugin_init(void)
342 {
343     if (!ppp_available() && !new_style_driver) {
344         fatal("Linux kernel does not support PPPoE -- are you running 2.4.x?");
345     }
346
347     add_options(Options);
348
349     info("RP-PPPoE plugin version %s compiled against pppd %s",
350          RP_VERSION, VERSION);
351 }
352
353 void pppoe_check_options(void)
354 {
355     unsigned int mac[6];
356     int i;
357
358     if (pppoe_reqd_mac != NULL) {
359         if (sscanf(pppoe_reqd_mac, "%x:%x:%x:%x:%x:%x",
360                    &mac[0], &mac[1], &mac[2], &mac[3],
361                    &mac[4], &mac[5]) != 6) {
362             option_error("cannot parse pppoe-mac option value");
363             exit(EXIT_OPTION_ERROR);
364         }
365         for (i = 0; i < 6; ++i)
366             conn->req_peer_mac[i] = mac[i];
367         conn->req_peer = 1;
368     }
369
370     lcp_allowoptions[0].neg_accompression = 0;
371     lcp_wantoptions[0].neg_accompression = 0;
372
373     lcp_allowoptions[0].neg_asyncmap = 0;
374     lcp_wantoptions[0].neg_asyncmap = 0;
375
376     lcp_allowoptions[0].neg_pcompression = 0;
377     lcp_wantoptions[0].neg_pcompression = 0;
378
379     if (lcp_allowoptions[0].mru > MAX_PPPOE_MTU)
380         lcp_allowoptions[0].mru = MAX_PPPOE_MTU;
381     if (lcp_wantoptions[0].mru > MAX_PPPOE_MTU)
382         lcp_wantoptions[0].mru = MAX_PPPOE_MTU;
383
384     ccp_allowoptions[0].deflate = 0;
385     ccp_wantoptions[0].deflate = 0;
386
387     ipcp_allowoptions[0].neg_vj = 0;
388     ipcp_wantoptions[0].neg_vj = 0;
389
390     ccp_allowoptions[0].bsd_compress = 0;
391     ccp_wantoptions[0].bsd_compress = 0;
392 }
393
394 struct channel pppoe_channel = {
395     .options = Options,
396     .process_extra_options = &PPPOEDeviceOptions,
397     .check_options = pppoe_check_options,
398     .connect = &PPPOEConnectDevice,
399     .disconnect = &PPPOEDisconnectDevice,
400     .establish_ppp = &generic_establish_ppp,
401     .disestablish_ppp = &generic_disestablish_ppp,
402     .send_config = NULL,
403     .recv_config = &PPPOERecvConfig,
404     .close = NULL,
405     .cleanup = NULL
406 };