152942Sbrian/*- 252942Sbrian * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org> 352942Sbrian * All rights reserved. 452942Sbrian * 552942Sbrian * Redistribution and use in source and binary forms, with or without 652942Sbrian * modification, are permitted provided that the following conditions 752942Sbrian * are met: 852942Sbrian * 1. Redistributions of source code must retain the above copyright 952942Sbrian * notice, this list of conditions and the following disclaimer. 1052942Sbrian * 2. Redistributions in binary form must reproduce the above copyright 1152942Sbrian * notice, this list of conditions and the following disclaimer in the 1252942Sbrian * documentation and/or other materials provided with the distribution. 1352942Sbrian * 1452942Sbrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1552942Sbrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1652942Sbrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1752942Sbrian * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1852942Sbrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1952942Sbrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2052942Sbrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2152942Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2252942Sbrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2352942Sbrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2452942Sbrian * SUCH DAMAGE. 2552942Sbrian * 2652942Sbrian * $FreeBSD$ 2752942Sbrian */ 2852942Sbrian 2952942Sbrian#include <sys/param.h> 3052942Sbrian#include <sys/socket.h> 3152942Sbrian#include <sys/un.h> 3252942Sbrian#include <netinet/in.h> 3352942Sbrian#include <arpa/inet.h> 3452942Sbrian#include <netdb.h> 3552942Sbrian#include <netgraph.h> 3652942Sbrian#include <net/ethernet.h> 3774916Sbrian#include <net/if.h> 3874916Sbrian#include <net/route.h> 3952942Sbrian#include <netinet/in_systm.h> 4052942Sbrian#include <netinet/ip.h> 4152942Sbrian#include <netgraph/ng_ether.h> 4252942Sbrian#include <netgraph/ng_message.h> 4352942Sbrian#include <netgraph/ng_pppoe.h> 4452942Sbrian#include <netgraph/ng_socket.h> 4552942Sbrian 4652942Sbrian#include <errno.h> 4752942Sbrian#include <stdio.h> 4852942Sbrian#include <stdlib.h> 4952942Sbrian#include <string.h> 5052942Sbrian#include <sysexits.h> 5152942Sbrian#include <sys/fcntl.h> 5271006Sbrian#include <sys/stat.h> 5352942Sbrian#include <sys/uio.h> 5452942Sbrian#include <termios.h> 5552942Sbrian#include <sys/time.h> 5696582Sbrian#include <syslog.h> 5752942Sbrian#include <unistd.h> 5852942Sbrian 5952942Sbrian#include "layer.h" 6052942Sbrian#include "defs.h" 6152942Sbrian#include "mbuf.h" 6252942Sbrian#include "log.h" 6352942Sbrian#include "timer.h" 6452942Sbrian#include "lqr.h" 6552942Sbrian#include "hdlc.h" 6652942Sbrian#include "throughput.h" 6752942Sbrian#include "fsm.h" 6852942Sbrian#include "lcp.h" 6952942Sbrian#include "ccp.h" 7052942Sbrian#include "link.h" 7152942Sbrian#include "async.h" 7252942Sbrian#include "descriptor.h" 7352942Sbrian#include "physical.h" 7452942Sbrian#include "main.h" 7552942Sbrian#include "mp.h" 7652942Sbrian#include "chat.h" 7752942Sbrian#include "auth.h" 7852942Sbrian#include "chap.h" 7952942Sbrian#include "cbcp.h" 8052942Sbrian#include "datalink.h" 8152942Sbrian#include "slcompress.h" 8252942Sbrian#include "iplist.h" 8381634Sbrian#include "ncpaddr.h" 8481634Sbrian#include "ip.h" 8552942Sbrian#include "ipcp.h" 8652942Sbrian#include "filter.h" 8752942Sbrian#ifndef NORADIUS 8852942Sbrian#include "radius.h" 8952942Sbrian#endif 9081634Sbrian#include "ipv6cp.h" 9181634Sbrian#include "ncp.h" 9252942Sbrian#include "bundle.h" 9352942Sbrian#include "id.h" 9474916Sbrian#include "iface.h" 9596582Sbrian#include "route.h" 9652942Sbrian#include "ether.h" 9752942Sbrian 9852942Sbrian 9952942Sbrian#define PPPOE_NODE_TYPE_LEN (sizeof NG_PPPOE_NODE_TYPE - 1) /* "PPPoE" */ 10052942Sbrian 10152942Sbrianstruct etherdevice { 10252942Sbrian struct device dev; /* What struct physical knows about */ 10352942Sbrian int cs; /* Control socket */ 10452942Sbrian int connected; /* Are we connected yet ? */ 10552942Sbrian int timeout; /* Seconds attempting to connect */ 10652942Sbrian char hook[sizeof TUN_NAME + 11]; /* Our socket node hook */ 10796582Sbrian u_int32_t slot; /* ifindex << 24 | unit */ 10852942Sbrian}; 10952942Sbrian 11052942Sbrian#define device2ether(d) \ 11152942Sbrian ((d)->type == ETHER_DEVICE ? (struct etherdevice *)d : NULL) 11252942Sbrian 113134789Sbrianunsigned 11452942Sbrianether_DeviceSize(void) 11552942Sbrian{ 11652942Sbrian return sizeof(struct etherdevice); 11752942Sbrian} 11852942Sbrian 11952942Sbrianstatic ssize_t 12052942Sbrianether_Write(struct physical *p, const void *v, size_t n) 12152942Sbrian{ 12252942Sbrian struct etherdevice *dev = device2ether(p->handler); 12352942Sbrian 124134789Sbrian return NgSendData(p->fd, dev->hook, v, n) == -1 ? -1 : (ssize_t)n; 12552942Sbrian} 12652942Sbrian 12752942Sbrianstatic ssize_t 12852942Sbrianether_Read(struct physical *p, void *v, size_t n) 12952942Sbrian{ 13052942Sbrian char hook[sizeof TUN_NAME + 11]; 13152942Sbrian 13252942Sbrian return NgRecvData(p->fd, v, n, hook); 13352942Sbrian} 13452942Sbrian 13552942Sbrianstatic int 13652942Sbrianether_RemoveFromSet(struct physical *p, fd_set *r, fd_set *w, fd_set *e) 13752942Sbrian{ 13852942Sbrian struct etherdevice *dev = device2ether(p->handler); 13952942Sbrian int result; 14052942Sbrian 14152942Sbrian if (r && dev->cs >= 0 && FD_ISSET(dev->cs, r)) { 14252942Sbrian FD_CLR(dev->cs, r); 14352942Sbrian log_Printf(LogTIMER, "%s: fdunset(ctrl) %d\n", p->link.name, dev->cs); 14452942Sbrian result = 1; 14552942Sbrian } else 14652942Sbrian result = 0; 14752942Sbrian 14852942Sbrian /* Careful... physical_RemoveFromSet() called us ! */ 14952942Sbrian 15052942Sbrian p->handler->removefromset = NULL; 15152942Sbrian result += physical_RemoveFromSet(p, r, w, e); 15252942Sbrian p->handler->removefromset = ether_RemoveFromSet; 15352942Sbrian 15452942Sbrian return result; 15552942Sbrian} 15652942Sbrian 15752942Sbrianstatic void 15852942Sbrianether_Free(struct physical *p) 15952942Sbrian{ 16052942Sbrian struct etherdevice *dev = device2ether(p->handler); 16152942Sbrian 16252942Sbrian physical_SetDescriptor(p); 16352942Sbrian if (dev->cs != -1) 16452942Sbrian close(dev->cs); 16552942Sbrian free(dev); 16652942Sbrian} 16752942Sbrian 16852942Sbrianstatic const char * 16952942Sbrianether_OpenInfo(struct physical *p) 17052942Sbrian{ 17152942Sbrian struct etherdevice *dev = device2ether(p->handler); 17252942Sbrian 17352942Sbrian switch (dev->connected) { 17452942Sbrian case CARRIER_PENDING: 17552942Sbrian return "negotiating"; 17652942Sbrian case CARRIER_OK: 17752942Sbrian return "established"; 17852942Sbrian } 17952942Sbrian 18052942Sbrian return "disconnected"; 18152942Sbrian} 18252942Sbrian 18396582Sbrianstatic int 18496582Sbrianether_Slot(struct physical *p) 18596582Sbrian{ 18696582Sbrian struct etherdevice *dev = device2ether(p->handler); 18796582Sbrian 18896582Sbrian return dev->slot; 18996582Sbrian} 19096582Sbrian 19196582Sbrian 19252942Sbrianstatic void 19352942Sbrianether_device2iov(struct device *d, struct iovec *iov, int *niov, 194134789Sbrian int maxiov __unused, int *auxfd, int *nauxfd) 19552942Sbrian{ 196196513Sbrian struct etherdevice *dev; 19752942Sbrian int sz = physical_MaxDeviceSize(); 19852942Sbrian 199196513Sbrian iov[*niov].iov_base = d = realloc(d, sz); 200196513Sbrian if (d == NULL) { 20152942Sbrian log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz); 20252942Sbrian AbortProgram(EX_OSERR); 20352942Sbrian } 20452942Sbrian iov[*niov].iov_len = sz; 20552942Sbrian (*niov)++; 20652942Sbrian 207196513Sbrian dev = device2ether(d); 20852942Sbrian if (dev->cs >= 0) { 20952942Sbrian *auxfd = dev->cs; 21052942Sbrian (*nauxfd)++; 21152942Sbrian } 21252942Sbrian} 21352942Sbrian 21452942Sbrianstatic void 21552942Sbrianether_MessageIn(struct etherdevice *dev) 21652942Sbrian{ 21752942Sbrian char msgbuf[sizeof(struct ng_mesg) + sizeof(struct ngpppoe_sts)]; 21852942Sbrian struct ng_mesg *rep = (struct ng_mesg *)msgbuf; 21952942Sbrian struct ngpppoe_sts *sts = (struct ngpppoe_sts *)(msgbuf + sizeof *rep); 22096582Sbrian char *end, unknown[14], sessionid[5]; 22152942Sbrian const char *msg; 22252942Sbrian struct timeval t; 22366898Sbrian fd_set *r; 22496582Sbrian u_long slot; 22598638Sbrian int asciilen, ret; 22652942Sbrian 22752942Sbrian if (dev->cs < 0) 22852942Sbrian return; 22952942Sbrian 23066898Sbrian if ((r = mkfdset()) == NULL) { 23166898Sbrian log_Printf(LogERROR, "DoLoop: Cannot create fd_set\n"); 23266898Sbrian return; 23366898Sbrian } 23466898Sbrian 23599086Sbrian while (1) { 23699086Sbrian zerofdset(r); 23799086Sbrian FD_SET(dev->cs, r); 23899086Sbrian t.tv_sec = t.tv_usec = 0; 23999086Sbrian ret = select(dev->cs + 1, r, NULL, NULL, &t); 24052942Sbrian 24199086Sbrian if (ret <= 0) 24299086Sbrian break; 24352942Sbrian 24499086Sbrian if (NgRecvMsg(dev->cs, rep, sizeof msgbuf, NULL) <= 0) 24599086Sbrian break; 24652942Sbrian 24799086Sbrian if (rep->header.version != NG_VERSION) { 24899086Sbrian log_Printf(LogWARN, "%ld: Unexpected netgraph version, expected %ld\n", 24999086Sbrian (long)rep->header.version, (long)NG_VERSION); 25099086Sbrian break; 25199086Sbrian } 25252942Sbrian 25399086Sbrian if (rep->header.typecookie != NGM_PPPOE_COOKIE) { 25499086Sbrian log_Printf(LogWARN, "%ld: Unexpected netgraph cookie, expected %ld\n", 25599086Sbrian (long)rep->header.typecookie, (long)NGM_PPPOE_COOKIE); 25690975Sbrian break; 25799086Sbrian } 25852942Sbrian 25999086Sbrian asciilen = 0; 26099086Sbrian switch (rep->header.cmd) { 26199086Sbrian case NGM_PPPOE_SET_FLAG: msg = "SET_FLAG"; break; 26299086Sbrian case NGM_PPPOE_CONNECT: msg = "CONNECT"; break; 26399086Sbrian case NGM_PPPOE_LISTEN: msg = "LISTEN"; break; 26499086Sbrian case NGM_PPPOE_OFFER: msg = "OFFER"; break; 26599086Sbrian case NGM_PPPOE_SUCCESS: msg = "SUCCESS"; break; 26699086Sbrian case NGM_PPPOE_FAIL: msg = "FAIL"; break; 26799086Sbrian case NGM_PPPOE_CLOSE: msg = "CLOSE"; break; 26899086Sbrian case NGM_PPPOE_GET_STATUS: msg = "GET_STATUS"; break; 26999086Sbrian case NGM_PPPOE_ACNAME: 27099086Sbrian msg = "ACNAME"; 27199086Sbrian if (setenv("ACNAME", sts->hook, 1) != 0) 27299086Sbrian log_Printf(LogWARN, "setenv: cannot set ACNAME=%s: %m", sts->hook); 27399086Sbrian asciilen = rep->header.arglen; 27499086Sbrian break; 27599086Sbrian case NGM_PPPOE_SESSIONID: 27699086Sbrian msg = "SESSIONID"; 27799086Sbrian snprintf(sessionid, sizeof sessionid, "%04x", *(u_int16_t *)sts); 27899086Sbrian if (setenv("SESSIONID", sessionid, 1) != 0) 27999086Sbrian syslog(LOG_WARNING, "setenv: cannot set SESSIONID=%s: %m", 28099086Sbrian sessionid); 28199086Sbrian /* Use this in preference to our interface index */ 28299086Sbrian slot = strtoul(sessionid, &end, 16); 28399086Sbrian if (end != sessionid && *end == '\0') 28499086Sbrian dev->slot = slot; 28599086Sbrian break; 28699086Sbrian default: 28799086Sbrian snprintf(unknown, sizeof unknown, "<%d>", (int)rep->header.cmd); 28899086Sbrian msg = unknown; 28999086Sbrian break; 29099086Sbrian } 29152942Sbrian 29299086Sbrian if (asciilen) 29399086Sbrian log_Printf(LogPHASE, "Received NGM_PPPOE_%s (hook \"%.*s\")\n", 29499086Sbrian msg, asciilen, sts->hook); 29599086Sbrian else 29699086Sbrian log_Printf(LogPHASE, "Received NGM_PPPOE_%s\n", msg); 29799086Sbrian 29899086Sbrian switch (rep->header.cmd) { 29999086Sbrian case NGM_PPPOE_SUCCESS: 30099086Sbrian dev->connected = CARRIER_OK; 30199086Sbrian break; 30299086Sbrian case NGM_PPPOE_FAIL: 30399086Sbrian case NGM_PPPOE_CLOSE: 30499086Sbrian dev->connected = CARRIER_LOST; 30599086Sbrian break; 30699086Sbrian } 30752942Sbrian } 30899086Sbrian free(r); 30952942Sbrian} 31052942Sbrian 31152942Sbrianstatic int 31252942Sbrianether_AwaitCarrier(struct physical *p) 31352942Sbrian{ 31452942Sbrian struct etherdevice *dev = device2ether(p->handler); 31552942Sbrian 31653071Sbrian if (dev->connected != CARRIER_OK && !dev->timeout--) 31752942Sbrian dev->connected = CARRIER_LOST; 31852942Sbrian else if (dev->connected == CARRIER_PENDING) 31952942Sbrian ether_MessageIn(dev); 32052942Sbrian 32152942Sbrian return dev->connected; 32252942Sbrian} 32352942Sbrian 32452942Sbrianstatic const struct device baseetherdevice = { 32552942Sbrian ETHER_DEVICE, 32652942Sbrian "ether", 32778410Sbrian 1492, 32853733Sbrian { CD_REQUIRED, DEF_ETHERCDDELAY }, 32952942Sbrian ether_AwaitCarrier, 33052942Sbrian ether_RemoveFromSet, 33152942Sbrian NULL, 33252942Sbrian NULL, 33352942Sbrian NULL, 33452942Sbrian NULL, 33593418Sbrian NULL, 33652942Sbrian ether_Free, 33752942Sbrian ether_Read, 33852942Sbrian ether_Write, 33952942Sbrian ether_device2iov, 34052942Sbrian NULL, 34196582Sbrian ether_OpenInfo, 34296582Sbrian ether_Slot 34352942Sbrian}; 34452942Sbrian 34552942Sbrianstruct device * 34652942Sbrianether_iov2device(int type, struct physical *p, struct iovec *iov, int *niov, 347134789Sbrian int maxiov __unused, int *auxfd, int *nauxfd) 34852942Sbrian{ 34952942Sbrian if (type == ETHER_DEVICE) { 35052942Sbrian struct etherdevice *dev = (struct etherdevice *)iov[(*niov)++].iov_base; 35152942Sbrian 35252942Sbrian dev = realloc(dev, sizeof *dev); /* Reduce to the correct size */ 35352942Sbrian if (dev == NULL) { 35452942Sbrian log_Printf(LogALERT, "Failed to allocate memory: %d\n", 35552942Sbrian (int)(sizeof *dev)); 35652942Sbrian AbortProgram(EX_OSERR); 35752942Sbrian } 35852942Sbrian 35952942Sbrian if (*nauxfd) { 36052942Sbrian dev->cs = *auxfd; 36152942Sbrian (*nauxfd)--; 36252942Sbrian } else 36352942Sbrian dev->cs = -1; 36452942Sbrian 36552942Sbrian /* Refresh function pointers etc */ 36652942Sbrian memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev); 36752942Sbrian 36852942Sbrian physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF); 36952942Sbrian return &dev->dev; 37052942Sbrian } 37152942Sbrian 37252942Sbrian return NULL; 37352942Sbrian} 37452942Sbrian 37552942Sbrianstatic int 37658028Sbrianether_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n) 37752942Sbrian{ 37852942Sbrian struct physical *p = descriptor2physical(d); 37952942Sbrian struct etherdevice *dev = device2ether(p->handler); 38052942Sbrian int result; 38152942Sbrian 38252942Sbrian if (r && dev->cs >= 0) { 38352942Sbrian FD_SET(dev->cs, r); 38452942Sbrian log_Printf(LogTIMER, "%s(ctrl): fdset(r) %d\n", p->link.name, dev->cs); 38552942Sbrian result = 1; 38652942Sbrian } else 38752942Sbrian result = 0; 38852942Sbrian 38952942Sbrian result += physical_doUpdateSet(d, r, w, e, n, 0); 39052942Sbrian 39152942Sbrian return result; 39252942Sbrian} 39352942Sbrian 39452942Sbrianstatic int 39558028Sbrianether_IsSet(struct fdescriptor *d, const fd_set *fdset) 39652942Sbrian{ 39752942Sbrian struct physical *p = descriptor2physical(d); 39852942Sbrian struct etherdevice *dev = device2ether(p->handler); 39952942Sbrian int result; 40052942Sbrian 40152942Sbrian result = dev->cs >= 0 && FD_ISSET(dev->cs, fdset); 40252942Sbrian result += physical_IsSet(d, fdset); 40352942Sbrian 40452942Sbrian return result; 40552942Sbrian} 40652942Sbrian 40752942Sbrianstatic void 40858028Sbrianether_DescriptorRead(struct fdescriptor *d, struct bundle *bundle, 40952942Sbrian const fd_set *fdset) 41052942Sbrian{ 41152942Sbrian struct physical *p = descriptor2physical(d); 41252942Sbrian struct etherdevice *dev = device2ether(p->handler); 41352942Sbrian 41452942Sbrian if (dev->cs >= 0 && FD_ISSET(dev->cs, fdset)) { 41552942Sbrian ether_MessageIn(dev); 41652942Sbrian if (dev->connected == CARRIER_LOST) { 41752942Sbrian log_Printf(LogPHASE, "%s: Device disconnected\n", p->link.name); 41852942Sbrian datalink_Down(p->dl, CLOSE_NORMAL); 41952942Sbrian return; 42052942Sbrian } 42152942Sbrian } 42252942Sbrian 42352942Sbrian if (physical_IsSet(d, fdset)) 42452942Sbrian physical_DescriptorRead(d, bundle, fdset); 42552942Sbrian} 42652942Sbrian 42752942Sbrianstatic struct device * 42852942Sbrianether_Abandon(struct etherdevice *dev, struct physical *p) 42952942Sbrian{ 43052942Sbrian /* Abandon our node construction */ 43152942Sbrian close(dev->cs); 43252942Sbrian close(p->fd); 43352942Sbrian p->fd = -2; /* Nobody else need try.. */ 43452942Sbrian free(dev); 43552942Sbrian 43652942Sbrian return NULL; 43752942Sbrian} 43852942Sbrian 43952942Sbrianstruct device * 44052942Sbrianether_Create(struct physical *p) 44152942Sbrian{ 44252942Sbrian u_char rbuf[2048]; 44352942Sbrian struct etherdevice *dev; 44452942Sbrian struct ng_mesg *resp; 44552942Sbrian const struct hooklist *hlist; 44652942Sbrian const struct nodeinfo *ninfo; 447134789Sbrian char *path, *sessionid; 448134789Sbrian const char *mode; 449134789Sbrian size_t ifacelen; 450134789Sbrian unsigned f; 45152942Sbrian 45252942Sbrian dev = NULL; 45374916Sbrian path = NULL; 45474916Sbrian ifacelen = 0; 45552942Sbrian if (p->fd < 0 && !strncasecmp(p->name.full, NG_PPPOE_NODE_TYPE, 45652942Sbrian PPPOE_NODE_TYPE_LEN) && 45752942Sbrian p->name.full[PPPOE_NODE_TYPE_LEN] == ':') { 45852942Sbrian const struct linkinfo *nlink; 45952942Sbrian struct ngpppoe_init_data *data; 46052942Sbrian struct ngm_mkpeer mkp; 46152942Sbrian struct ngm_connect ngc; 46252942Sbrian const char *iface, *provider; 46374916Sbrian char etherid[12]; 46474916Sbrian int providerlen; 46552942Sbrian char connectpath[sizeof dev->hook + 2]; /* .:<hook> */ 46652942Sbrian 46752942Sbrian p->fd--; /* We own the device - change fd */ 46852942Sbrian 46993418Sbrian loadmodules(LOAD_VERBOSLY, "netgraph", "ng_ether", "ng_pppoe", "ng_socket", 47093418Sbrian NULL); 47152942Sbrian 47252942Sbrian if ((dev = malloc(sizeof *dev)) == NULL) 47352942Sbrian return NULL; 47452942Sbrian 47552942Sbrian iface = p->name.full + PPPOE_NODE_TYPE_LEN + 1; 47652942Sbrian 47752942Sbrian provider = strchr(iface, ':'); 47852942Sbrian if (provider) { 47952942Sbrian ifacelen = provider - iface; 48052942Sbrian provider++; 48152942Sbrian providerlen = strlen(provider); 48252942Sbrian } else { 48352942Sbrian ifacelen = strlen(iface); 48452942Sbrian provider = ""; 48552942Sbrian providerlen = 0; 48652942Sbrian } 48752942Sbrian 48852942Sbrian /* 48952942Sbrian * We're going to do this (where tunN is our tunnel device): 49052942Sbrian * 49152942Sbrian * .---------. 49252942Sbrian * | ether | 49352942Sbrian * | <iface> | dev->cs 49452942Sbrian * `---------' | 49552942Sbrian * (orphan) p->fd | 49652942Sbrian * | | | 49752942Sbrian * | | | 49852942Sbrian * (ethernet) | | 49952942Sbrian * .---------. .-----------. 50052942Sbrian * | pppoe | | socket | 50152942Sbrian * | <iface> |(tunN)<---->(tunN)| <unnamed> | 50252942Sbrian * `--------- `-----------' 50352942Sbrian * (tunX) 50452942Sbrian * ^ 50552942Sbrian * | 50652942Sbrian * `--->(tunX) 50752942Sbrian */ 50852942Sbrian 50952942Sbrian /* Create a socket node */ 51053535Sbrian if (ID0NgMkSockNode(NULL, &dev->cs, &p->fd) == -1) { 51152942Sbrian log_Printf(LogWARN, "Cannot create netgraph socket node: %s\n", 51252942Sbrian strerror(errno)); 51352942Sbrian free(dev); 51493418Sbrian p->fd = -2; 51552942Sbrian return NULL; 51652942Sbrian } 51752942Sbrian 51852942Sbrian /* 51952942Sbrian * Ask for a list of hooks attached to the "ether" node. This node should 52052942Sbrian * magically exist as a way of hooking stuff onto an ethernet device 52152942Sbrian */ 52252942Sbrian path = (char *)alloca(ifacelen + 2); 523134833Smarcel sprintf(path, "%.*s:", (int)ifacelen, iface); 52452942Sbrian if (NgSendMsg(dev->cs, path, NGM_GENERIC_COOKIE, NGM_LISTHOOKS, 52552942Sbrian NULL, 0) < 0) { 52652942Sbrian log_Printf(LogWARN, "%s Cannot send a netgraph message: %s\n", 52752942Sbrian path, strerror(errno)); 52852942Sbrian return ether_Abandon(dev, p); 52952942Sbrian } 53052942Sbrian 53152942Sbrian /* Get our list back */ 53252942Sbrian resp = (struct ng_mesg *)rbuf; 53382277Sbrian if (NgRecvMsg(dev->cs, resp, sizeof rbuf, NULL) <= 0) { 53452942Sbrian log_Printf(LogWARN, "Cannot get netgraph response: %s\n", 53552942Sbrian strerror(errno)); 53652942Sbrian return ether_Abandon(dev, p); 53752942Sbrian } 53852942Sbrian 53952942Sbrian hlist = (const struct hooklist *)resp->data; 54052942Sbrian ninfo = &hlist->nodeinfo; 54152942Sbrian 54252942Sbrian /* Make sure we've got the right type of node */ 54352942Sbrian if (strncmp(ninfo->type, NG_ETHER_NODE_TYPE, 54452942Sbrian sizeof NG_ETHER_NODE_TYPE - 1)) { 54552942Sbrian log_Printf(LogWARN, "%s Unexpected node type ``%s'' (wanted ``" 54652942Sbrian NG_ETHER_NODE_TYPE "'')\n", path, ninfo->type); 54752942Sbrian return ether_Abandon(dev, p); 54852942Sbrian } 54952942Sbrian 55052963Sbrian log_Printf(LogDEBUG, "List of netgraph node ``%s'' (id %x) hooks:\n", 55152942Sbrian path, ninfo->id); 55252942Sbrian 55352942Sbrian /* look for a hook already attached. */ 55452942Sbrian for (f = 0; f < ninfo->hooks; f++) { 55552942Sbrian nlink = &hlist->link[f]; 55652942Sbrian 55752942Sbrian log_Printf(LogDEBUG, " Found %s -> %s\n", nlink->ourhook, 55852942Sbrian nlink->peerhook); 55952942Sbrian 56052942Sbrian if (!strcmp(nlink->ourhook, NG_ETHER_HOOK_ORPHAN) || 56152942Sbrian !strcmp(nlink->ourhook, NG_ETHER_HOOK_DIVERT)) { 56252942Sbrian /* 56352942Sbrian * Something is using the data coming out of this ``ether'' node. 56452942Sbrian * If it's a PPPoE node, we use that node, otherwise we complain that 56552942Sbrian * someone else is using the node. 56652942Sbrian */ 56752942Sbrian if (!strcmp(nlink->nodeinfo.type, NG_PPPOE_NODE_TYPE)) 56852942Sbrian /* Use this PPPoE node ! */ 56952963Sbrian snprintf(ngc.path, sizeof ngc.path, "[%x]:", nlink->nodeinfo.id); 57052942Sbrian else { 57152942Sbrian log_Printf(LogWARN, "%s Node type ``%s'' is currently active\n", 57252942Sbrian path, nlink->nodeinfo.type); 57352942Sbrian return ether_Abandon(dev, p); 57452942Sbrian } 57552942Sbrian break; 57652942Sbrian } 57752942Sbrian } 57852942Sbrian 57952942Sbrian if (f == ninfo->hooks) { 58052942Sbrian /* 58152942Sbrian * Create a new ``PPPoE'' node connected to the ``ether'' node using 58293418Sbrian * the ``orphan'' and ``ethernet'' hooks 58352942Sbrian */ 58452942Sbrian snprintf(mkp.type, sizeof mkp.type, "%s", NG_PPPOE_NODE_TYPE); 58552942Sbrian snprintf(mkp.ourhook, sizeof mkp.ourhook, "%s", NG_ETHER_HOOK_ORPHAN); 58652942Sbrian snprintf(mkp.peerhook, sizeof mkp.peerhook, "%s", NG_PPPOE_HOOK_ETHERNET); 58752963Sbrian snprintf(etherid, sizeof etherid, "[%x]:", ninfo->id); 58852942Sbrian 58952942Sbrian log_Printf(LogDEBUG, "Creating PPPoE netgraph node %s%s -> %s\n", 59052942Sbrian etherid, mkp.ourhook, mkp.peerhook); 59152942Sbrian 59252942Sbrian if (NgSendMsg(dev->cs, etherid, NGM_GENERIC_COOKIE, 59352942Sbrian NGM_MKPEER, &mkp, sizeof mkp) < 0) { 59452942Sbrian log_Printf(LogWARN, "%s Cannot create PPPoE netgraph node: %s\n", 59552942Sbrian etherid, strerror(errno)); 59652942Sbrian return ether_Abandon(dev, p); 59752942Sbrian } 59852942Sbrian 59952942Sbrian snprintf(ngc.path, sizeof ngc.path, "%s%s", path, NG_ETHER_HOOK_ORPHAN); 60052942Sbrian } 60152942Sbrian 60252942Sbrian snprintf(dev->hook, sizeof dev->hook, "%s%d", 60352942Sbrian TUN_NAME, p->dl->bundle->unit); 60452942Sbrian 60552942Sbrian /* 60652942Sbrian * Connect the PPPoE node to our socket node. 60752942Sbrian * ngc.path has already been set up 60852942Sbrian */ 60952942Sbrian snprintf(ngc.ourhook, sizeof ngc.ourhook, "%s", dev->hook); 61052942Sbrian memcpy(ngc.peerhook, ngc.ourhook, sizeof ngc.peerhook); 61152942Sbrian 61252942Sbrian log_Printf(LogDEBUG, "Connecting netgraph socket .:%s -> %s:%s\n", 61352942Sbrian ngc.ourhook, ngc.path, ngc.peerhook); 61452942Sbrian if (NgSendMsg(dev->cs, ".:", NGM_GENERIC_COOKIE, 61552942Sbrian NGM_CONNECT, &ngc, sizeof ngc) < 0) { 61652942Sbrian log_Printf(LogWARN, "Cannot connect PPPoE and socket netgraph " 61752942Sbrian "nodes: %s\n", strerror(errno)); 61852942Sbrian return ether_Abandon(dev, p); 61952942Sbrian } 62052942Sbrian 62179854Sbrian /* Bring the Ethernet interface up */ 62279854Sbrian path[ifacelen] = '\0'; /* Remove the trailing ':' */ 62379854Sbrian if (!iface_SetFlags(path, IFF_UP)) 62479854Sbrian log_Printf(LogWARN, "%s: Failed to set the IFF_UP flag on %s\n", 62579854Sbrian p->link.name, path); 62679854Sbrian 627132818Sglebius snprintf(connectpath, sizeof connectpath, ".:%s", dev->hook); 628132818Sglebius 629132818Sglebius /* Configure node to 3Com mode if needed */ 630132818Sglebius if (p->cfg.pppoe_configured) { 631132818Sglebius mode = p->cfg.nonstandard_pppoe ? NG_PPPOE_NONSTANDARD : NG_PPPOE_STANDARD; 632132818Sglebius if (NgSendMsg(dev->cs, connectpath, NGM_PPPOE_COOKIE, 633132818Sglebius NGM_PPPOE_SETMODE, mode, strlen(mode) + 1) == -1) { 634132818Sglebius log_Printf(LogWARN, "``%s'': Cannot configure netgraph node: %s\n", 635132818Sglebius connectpath, strerror(errno)); 636132818Sglebius return ether_Abandon(dev, p); 637132818Sglebius } 638132818Sglebius } 639132818Sglebius 64052942Sbrian /* And finally, request a connection to the given provider */ 64152942Sbrian 64268032Sbrian data = (struct ngpppoe_init_data *)alloca(sizeof *data + providerlen); 64352942Sbrian snprintf(data->hook, sizeof data->hook, "%s", dev->hook); 64468846Sbrian memcpy(data->data, provider, providerlen); 64568846Sbrian data->data_len = providerlen; 64652942Sbrian 64752942Sbrian log_Printf(LogDEBUG, "Sending PPPOE_CONNECT to %s\n", connectpath); 64852942Sbrian if (NgSendMsg(dev->cs, connectpath, NGM_PPPOE_COOKIE, 64952942Sbrian NGM_PPPOE_CONNECT, data, sizeof *data + providerlen) == -1) { 65052942Sbrian log_Printf(LogWARN, "``%s'': Cannot start netgraph node: %s\n", 65152942Sbrian connectpath, strerror(errno)); 65252942Sbrian return ether_Abandon(dev, p); 65352942Sbrian } 65452942Sbrian 65553062Sbrian /* Hook things up so that we monitor dev->cs */ 65653062Sbrian p->desc.UpdateSet = ether_UpdateSet; 65753062Sbrian p->desc.IsSet = ether_IsSet; 65853062Sbrian p->desc.Read = ether_DescriptorRead; 65953733Sbrian 66053733Sbrian memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev); 66153733Sbrian switch (p->cfg.cd.necessity) { 66253733Sbrian case CD_VARIABLE: 66353733Sbrian dev->dev.cd.delay = p->cfg.cd.delay; 66453733Sbrian break; 66553733Sbrian case CD_REQUIRED: 66653733Sbrian dev->dev.cd = p->cfg.cd; 66753733Sbrian break; 66853733Sbrian case CD_NOTREQUIRED: 66953733Sbrian log_Printf(LogWARN, "%s: Carrier must be set, using ``set cd %d!''\n", 67053733Sbrian p->link.name, dev->dev.cd.delay); 67153733Sbrian case CD_DEFAULT: 67253733Sbrian break; 67353733Sbrian } 67453733Sbrian 67553733Sbrian dev->timeout = dev->dev.cd.delay; 67653733Sbrian dev->connected = CARRIER_PENDING; 67796582Sbrian /* This will be overridden by our session id - if provided by netgraph */ 67896582Sbrian dev->slot = GetIfIndex(path); 67952942Sbrian } else { 68052942Sbrian /* See if we're a netgraph socket */ 68171006Sbrian struct stat st; 68252942Sbrian 68371006Sbrian if (fstat(p->fd, &st) != -1 && (st.st_mode & S_IFSOCK)) { 68471006Sbrian struct sockaddr_storage ssock; 68571006Sbrian struct sockaddr *sock = (struct sockaddr *)&ssock; 68671006Sbrian int sz; 68752942Sbrian 68871006Sbrian sz = sizeof ssock; 68971006Sbrian if (getsockname(p->fd, sock, &sz) == -1) { 69071006Sbrian log_Printf(LogPHASE, "%s: Link is a closed socket !\n", p->link.name); 69171006Sbrian close(p->fd); 69271006Sbrian p->fd = -1; 69352942Sbrian return NULL; 69452942Sbrian } 69552942Sbrian 69671006Sbrian if (sock->sa_family == AF_NETGRAPH) { 69771006Sbrian /* 69871006Sbrian * It's a netgraph node... We can't determine hook names etc, so we 69971006Sbrian * stay pretty impartial.... 70071006Sbrian */ 70171006Sbrian log_Printf(LogPHASE, "%s: Link is a netgraph node\n", p->link.name); 70271006Sbrian 70371006Sbrian if ((dev = malloc(sizeof *dev)) == NULL) { 70471006Sbrian log_Printf(LogWARN, "%s: Cannot allocate an ether device: %s\n", 70571006Sbrian p->link.name, strerror(errno)); 70671006Sbrian return NULL; 70771006Sbrian } 70871006Sbrian 70971006Sbrian memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev); 71071006Sbrian dev->cs = -1; 71171006Sbrian dev->timeout = 0; 71271006Sbrian dev->connected = CARRIER_OK; 71371006Sbrian *dev->hook = '\0'; 71496582Sbrian 71596582Sbrian /* 71696582Sbrian * If we're being envoked from pppoed(8), we may have a SESSIONID 71796582Sbrian * set in the environment. If so, use it as the slot 71896582Sbrian */ 71996582Sbrian if ((sessionid = getenv("SESSIONID")) != NULL) { 72096582Sbrian char *end; 72196582Sbrian u_long slot; 72296582Sbrian 72396582Sbrian slot = strtoul(sessionid, &end, 16); 72496582Sbrian dev->slot = end != sessionid && *end == '\0' ? slot : 0; 72596582Sbrian } else 72696582Sbrian dev->slot = 0; 72771006Sbrian } 72852942Sbrian } 72952942Sbrian } 73052942Sbrian 73152942Sbrian if (dev) { 73252942Sbrian physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF); 73352942Sbrian return &dev->dev; 73452942Sbrian } 73552942Sbrian 73652942Sbrian return NULL; 73752942Sbrian} 738