X-Git-Url: https://git.ozlabs.org/?p=ppp.git;a=blobdiff_plain;f=pppd%2Fplugins%2Frp-pppoe%2Fplugin.c;h=93c0906119d7240fd60ba483768c3654ef642f5f;hp=6885751bbebd7aba6cecdabc4e23a3c618816a47;hb=f1e3aa2dc7e7772d8491c6ff61e4e6d28af33d4b;hpb=9c0f3f00059b45e62849fe5a7a3164bbe6eca00e diff --git a/pppd/plugins/rp-pppoe/plugin.c b/pppd/plugins/rp-pppoe/plugin.c index 6885751..93c0906 100644 --- a/pppd/plugins/rp-pppoe/plugin.c +++ b/pppd/plugins/rp-pppoe/plugin.c @@ -19,10 +19,11 @@ * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. +* ***********************************************************************/ static char const RCSID[] = -"$Id: plugin.c,v 1.8 2003/03/04 05:21:38 fcusack Exp $"; +"$Id: plugin.c,v 1.17 2008/06/15 04:35:50 paulus Exp $"; #define _GNU_SOURCE 1 #include "pppoe.h" @@ -32,10 +33,9 @@ static char const RCSID[] = #include "pppd/lcp.h" #include "pppd/ipcp.h" #include "pppd/ccp.h" -#include "pppd/pathnames.h" +/* #include "pppd/pathnames.h" */ #include -#include #include #include #include @@ -46,11 +46,13 @@ static char const RCSID[] = #include #include #include -#include #include -#include "ppp_defs.h" -#include "if_ppp.h" -#include "if_pppox.h" +#include +#include + +#ifndef _ROOT_PATH +#define _ROOT_PATH "" +#endif #define _PATH_ETHOPT _ROOT_PATH "/etc/ppp/options." @@ -63,6 +65,11 @@ char *pppd_pppoe_service = NULL; static char *acName = NULL; static char *existingSession = NULL; static int printACNames = 0; +static char *pppoe_reqd_mac = NULL; +unsigned char pppoe_reqd_mac_addr[6]; +static char *host_uniq; +static int pppoe_padi_timeout = PADI_TIMEOUT; +static int pppoe_padi_attempts = MAX_PADI_ATTEMPTS; static int PPPoEDevnameHook(char *cmd, char **argv, int doit); static option_t Options[] = { @@ -78,6 +85,14 @@ static option_t Options[] = { "Attach to existing session (sessid:macaddr)" }, { "rp_pppoe_verbose", o_int, &printACNames, "Be verbose about discovered access concentrators"}, + { "pppoe-mac", o_string, &pppoe_reqd_mac, + "Only connect to specified MAC address" }, + { "host-uniq", o_string, &host_uniq, + "Set the Host-Uniq to the supplied hex string" }, + { "pppoe-padi-timeout", o_int, &pppoe_padi_timeout, + "Initial timeout for discovery packets in seconds" }, + { "pppoe-padi-attempts", o_int, &pppoe_padi_attempts, + "Number of discovery attempts" }, { NULL } }; int (*OldDevnameHook)(char *cmd, char **argv, int doit) = NULL; @@ -97,20 +112,15 @@ PPPOEInitDevice(void) { conn = malloc(sizeof(PPPoEConnection)); if (!conn) { - fatal("Could not allocate memory for PPPoE session"); + novm("PPPoE session data"); } memset(conn, 0, sizeof(PPPoEConnection)); - if (acName) { - SET_STRING(conn->acName, acName); - } - if (pppd_pppoe_service) { - SET_STRING(conn->serviceName, pppd_pppoe_service); - } - SET_STRING(conn->ifName, devnam); + conn->ifName = devnam; conn->discoverySocket = -1; conn->sessionSocket = -1; - conn->useHostUniq = 1; conn->printACNames = printACNames; + conn->discoveryTimeout = pppoe_padi_timeout; + conn->discoveryAttempts = pppoe_padi_attempts; return 1; } @@ -127,7 +137,56 @@ static int PPPOEConnectDevice(void) { struct sockaddr_pppox sp; + struct ifreq ifr; + int s; + + /* Open session socket before discovery phase, to avoid losing session */ + /* packets sent by peer just after PADS packet (noted on some Cisco */ + /* server equipment). */ + /* Opening this socket just before waitForPADS in the discovery() */ + /* function would be more appropriate, but it would mess-up the code */ + conn->sessionSocket = socket(AF_PPPOX, SOCK_STREAM, PX_PROTO_OE); + if (conn->sessionSocket < 0) { + error("Failed to create PPPoE socket: %m"); + return -1; + } + /* Restore configuration */ + lcp_allowoptions[0].mru = conn->mtu; + lcp_wantoptions[0].mru = conn->mru; + + /* Update maximum MRU */ + s = socket(AF_INET, SOCK_DGRAM, 0); + if (s < 0) { + error("Can't get MTU for %s: %m", conn->ifName); + goto errout; + } + strlcpy(ifr.ifr_name, conn->ifName, sizeof(ifr.ifr_name)); + if (ioctl(s, SIOCGIFMTU, &ifr) < 0) { + error("Can't get MTU for %s: %m", conn->ifName); + close(s); + goto errout; + } + close(s); + + if (lcp_allowoptions[0].mru > ifr.ifr_mtu - TOTAL_OVERHEAD) + lcp_allowoptions[0].mru = ifr.ifr_mtu - TOTAL_OVERHEAD; + if (lcp_wantoptions[0].mru > ifr.ifr_mtu - TOTAL_OVERHEAD) + lcp_wantoptions[0].mru = ifr.ifr_mtu - TOTAL_OVERHEAD; + + if (host_uniq) { + if (!parseHostUniq(host_uniq, &conn->hostUniq)) + fatal("Illegal value for host-uniq option"); + } else { + /* if a custom host-uniq is not supplied, use our PID */ + pid_t pid = getpid(); + conn->hostUniq.type = htons(TAG_HOST_UNIQ); + conn->hostUniq.length = htons(sizeof(pid)); + memcpy(conn->hostUniq.payload, &pid, sizeof(pid)); + } + + conn->acName = acName; + conn->serviceName = pppd_pppoe_service; strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam)); if (existingSession) { unsigned int mac[ETH_ALEN]; @@ -142,20 +201,18 @@ PPPOEConnectDevice(void) conn->peerEth[i] = (unsigned char) mac[i]; } } else { + conn->discoverySocket = + openInterface(conn->ifName, Eth_PPPOE_Discovery, conn->myEth); discovery(conn); if (conn->discoveryState != STATE_SESSION) { - fatal("Unable to complete PPPoE Discovery"); + error("Unable to complete PPPoE Discovery"); + goto errout; } } /* Set PPPoE session-number for further consumption */ ppp_session_number = ntohs(conn->session); - /* Make the session socket */ - conn->sessionSocket = socket(AF_PPPOX, SOCK_STREAM, PX_PROTO_OE); - if (conn->sessionSocket < 0) { - fatal("Failed to create PPPoE socket: %m"); - } sp.sa_family = AF_PPPOX; sp.sa_protocol = PX_PROTO_OE; sp.sa_addr.pppoe.sid = conn->session; @@ -171,55 +228,45 @@ PPPOEConnectDevice(void) (unsigned) conn->peerEth[4], (unsigned) conn->peerEth[5]); + warn("Connected to %02X:%02X:%02X:%02X:%02X:%02X via interface %s", + (unsigned) conn->peerEth[0], + (unsigned) conn->peerEth[1], + (unsigned) conn->peerEth[2], + (unsigned) conn->peerEth[3], + (unsigned) conn->peerEth[4], + (unsigned) conn->peerEth[5], + conn->ifName); + + script_setenv("MACREMOTE", remote_number, 0); + if (connect(conn->sessionSocket, (struct sockaddr *) &sp, sizeof(struct sockaddr_pppox)) < 0) { - fatal("Failed to connect PPPoE socket: %d %m", errno); - return -1; + error("Failed to connect PPPoE socket: %d %m", errno); + goto errout; } return conn->sessionSocket; -} -static int -PPPOESendConfig(int mtu, - u_int32_t asyncmap, - int pcomp, - int accomp) -{ - int sock; - struct ifreq ifr; - - if (mtu > MAX_PPPOE_MTU) { - warn("Couldn't increase MTU to %d", mtu); - mtu = MAX_PPPOE_MTU; - } - sock = socket(AF_INET, SOCK_DGRAM, 0); - if (sock < 0) { - error("Couldn't create IP socket: %m"); - return -1; + errout: + if (conn->discoverySocket >= 0) { + sendPADT(conn, NULL); + close(conn->discoverySocket); + conn->discoverySocket = -1; } - strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); - ifr.ifr_mtu = mtu; - if (ioctl(sock, SIOCSIFMTU, &ifr) < 0) { - error("ioctl(SIOCSIFMTU): %m"); - return -1; - } - (void) close (sock); - return 0; + close(conn->sessionSocket); + return -1; } - -static int +static void PPPOERecvConfig(int mru, u_int32_t asyncmap, int pcomp, int accomp) { - if (mru > MAX_PPPOE_MTU) { - error("Couldn't increase MRU to %d", mru); - return -1; - } - return 0; +#if 0 /* broken protocol, but no point harrassing the users I guess... */ + if (mru > MAX_PPPOE_MTU) + warn("Couldn't increase MRU to %d", mru); +#endif } /********************************************************************** @@ -242,19 +289,23 @@ PPPOEDisconnectDevice(void) memcpy(sp.sa_addr.pppoe.dev, conn->ifName, IFNAMSIZ); memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN); if (connect(conn->sessionSocket, (struct sockaddr *) &sp, - sizeof(struct sockaddr_pppox)) < 0) { - fatal("Failed to disconnect PPPoE socket: %d %m", errno); - return; - } + sizeof(struct sockaddr_pppox)) < 0 && errno != EALREADY) + error("Failed to disconnect PPPoE socket: %d %m", errno); close(conn->sessionSocket); + if (conn->discoverySocket >= 0) { + sendPADT(conn, NULL); + close(conn->discoverySocket); + } } static void PPPOEDeviceOptions(void) { - char buf[256]; - snprintf(buf, 256, _PATH_ETHOPT "%s",devnam); - if(!options_from_file(buf, 0, 0, 1)) + char buf[MAXPATHLEN]; + + strlcpy(buf, _PATH_ETHOPT, MAXPATHLEN); + strlcat(buf, devnam, MAXPATHLEN); + if (!options_from_file(buf, 0, 0, 1)) exit(EXIT_OPTION_ERROR); } @@ -280,15 +331,14 @@ PPPoEDevnameHook(char *cmd, char **argv, int doit) int fd; struct ifreq ifr; - /* Only do it if name is "ethXXX" or "nic-XXXX. In latter case, - strip off the "nic-" */ - /* Thanks to Russ Couturier for this fix */ + /* + * Take any otherwise-unrecognized option as a possible device name, + * and test if it is the name of a network interface with a + * hardware address whose sa_family is ARPHRD_ETHER. + */ if (strlen(cmd) > 4 && !strncmp(cmd, "nic-", 4)) { /* Strip off "nic-" */ cmd += 4; - } else if (strlen(cmd) < 4 || strncmp(cmd, "eth", 3)) { - if (OldDevnameHook) return OldDevnameHook(cmd, argv, doit); - return 0; } /* Open a socket */ @@ -298,7 +348,7 @@ PPPoEDevnameHook(char *cmd, char **argv, int doit) /* Try getting interface index */ if (r) { - strncpy(ifr.ifr_name, cmd, sizeof(ifr.ifr_name)); + strlcpy(ifr.ifr_name, cmd, sizeof(ifr.ifr_name)); if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) { r = 0; } else { @@ -306,8 +356,9 @@ PPPoEDevnameHook(char *cmd, char **argv, int doit) r = 0; } else { if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) { - error("Interface %s not Ethernet", cmd); - r=0; + if (doit) + error("Interface %s not Ethernet", cmd); + r = 0; } } } @@ -315,37 +366,18 @@ PPPoEDevnameHook(char *cmd, char **argv, int doit) /* Close socket */ close(fd); - if (r) { - strncpy(devnam, cmd, sizeof(devnam)); + if (r && doit) { + strlcpy(devnam, cmd, sizeof(devnam)); if (the_channel != &pppoe_channel) { the_channel = &pppoe_channel; modem = 0; - lcp_allowoptions[0].neg_accompression = 0; - lcp_wantoptions[0].neg_accompression = 0; - - lcp_allowoptions[0].neg_asyncmap = 0; - lcp_wantoptions[0].neg_asyncmap = 0; - - lcp_allowoptions[0].neg_pcompression = 0; - lcp_wantoptions[0].neg_pcompression = 0; - - ccp_allowoptions[0].deflate = 0 ; - ccp_wantoptions[0].deflate = 0 ; - - ipcp_allowoptions[0].neg_vj=0; - ipcp_wantoptions[0].neg_vj=0; - - ccp_allowoptions[0].bsd_compress = 0; - ccp_wantoptions[0].bsd_compress = 0; - PPPOEInitDevice(); } return 1; } - if (OldDevnameHook) r = OldDevnameHook(cmd, argv, doit); return r; } @@ -371,71 +403,61 @@ plugin_init(void) RP_VERSION, VERSION); } -/********************************************************************** -*%FUNCTION: fatalSys -*%ARGUMENTS: -* str -- error message -*%RETURNS: -* Nothing -*%DESCRIPTION: -* Prints a message plus the errno value to stderr and syslog and exits. -***********************************************************************/ -void -fatalSys(char const *str) +void pppoe_check_options(void) { - char buf[1024]; - int i = errno; - sprintf(buf, "%.256s: %.256s", str, strerror(i)); - printErr(buf); - sprintf(buf, "RP-PPPoE: %.256s: %.256s", str, strerror(i)); - sendPADT(conn, buf); - exit(1); -} + unsigned int mac[6]; + int i; + + if (pppoe_reqd_mac != NULL) { + if (sscanf(pppoe_reqd_mac, "%x:%x:%x:%x:%x:%x", + &mac[0], &mac[1], &mac[2], &mac[3], + &mac[4], &mac[5]) != 6) { + option_error("cannot parse pppoe-mac option value"); + exit(EXIT_OPTION_ERROR); + } + for (i = 0; i < 6; ++i) + conn->req_peer_mac[i] = mac[i]; + conn->req_peer = 1; + } -/********************************************************************** -*%FUNCTION: rp_fatal -*%ARGUMENTS: -* str -- error message -*%RETURNS: -* Nothing -*%DESCRIPTION: -* Prints a message to stderr and syslog and exits. -***********************************************************************/ -void -rp_fatal(char const *str) -{ - char buf[1024]; - printErr(str); - sprintf(buf, "RP-PPPoE: %.256s", str); - sendPADT(conn, buf); - exit(1); -} -/********************************************************************** -*%FUNCTION: sysErr -*%ARGUMENTS: -* str -- error message -*%RETURNS: -* Nothing -*%DESCRIPTION: -* Prints a message plus the errno value to syslog. -***********************************************************************/ -void -sysErr(char const *str) -{ - rp_fatal(str); -} + lcp_allowoptions[0].neg_accompression = 0; + lcp_wantoptions[0].neg_accompression = 0; + + lcp_allowoptions[0].neg_asyncmap = 0; + lcp_wantoptions[0].neg_asyncmap = 0; + + lcp_allowoptions[0].neg_pcompression = 0; + lcp_wantoptions[0].neg_pcompression = 0; + + if (lcp_allowoptions[0].mru > MAX_PPPOE_MTU) + lcp_allowoptions[0].mru = MAX_PPPOE_MTU; + if (lcp_wantoptions[0].mru > MAX_PPPOE_MTU) + lcp_wantoptions[0].mru = MAX_PPPOE_MTU; + /* Save configuration */ + conn->mtu = lcp_allowoptions[0].mru; + conn->mru = lcp_wantoptions[0].mru; + + ccp_allowoptions[0].deflate = 0; + ccp_wantoptions[0].deflate = 0; + + ipcp_allowoptions[0].neg_vj = 0; + ipcp_wantoptions[0].neg_vj = 0; + + ccp_allowoptions[0].bsd_compress = 0; + ccp_wantoptions[0].bsd_compress = 0; +} struct channel pppoe_channel = { - options: Options, - process_extra_options: &PPPOEDeviceOptions, - check_options: NULL, - connect: &PPPOEConnectDevice, - disconnect: &PPPOEDisconnectDevice, - establish_ppp: &generic_establish_ppp, - disestablish_ppp: &generic_disestablish_ppp, - send_config: &PPPOESendConfig, - recv_config: &PPPOERecvConfig, - close: NULL, - cleanup: NULL + .options = Options, + .process_extra_options = &PPPOEDeviceOptions, + .check_options = pppoe_check_options, + .connect = &PPPOEConnectDevice, + .disconnect = &PPPOEDisconnectDevice, + .establish_ppp = &generic_establish_ppp, + .disestablish_ppp = &generic_disestablish_ppp, + .send_config = NULL, + .recv_config = &PPPOERecvConfig, + .close = NULL, + .cleanup = NULL };