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