17a6f8720SBrian Somers /*- 27a6f8720SBrian Somers * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org> 37a6f8720SBrian Somers * All rights reserved. 47a6f8720SBrian Somers * 57a6f8720SBrian Somers * Redistribution and use in source and binary forms, with or without 67a6f8720SBrian Somers * modification, are permitted provided that the following conditions 77a6f8720SBrian Somers * are met: 87a6f8720SBrian Somers * 1. Redistributions of source code must retain the above copyright 97a6f8720SBrian Somers * notice, this list of conditions and the following disclaimer. 107a6f8720SBrian Somers * 2. Redistributions in binary form must reproduce the above copyright 117a6f8720SBrian Somers * notice, this list of conditions and the following disclaimer in the 127a6f8720SBrian Somers * documentation and/or other materials provided with the distribution. 137a6f8720SBrian Somers * 147a6f8720SBrian Somers * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 157a6f8720SBrian Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 167a6f8720SBrian Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 177a6f8720SBrian Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 187a6f8720SBrian Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 197a6f8720SBrian Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 207a6f8720SBrian Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 217a6f8720SBrian Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 227a6f8720SBrian Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 237a6f8720SBrian Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 247a6f8720SBrian Somers * SUCH DAMAGE. 257a6f8720SBrian Somers * 2626afeaa2SBrian Somers * $Id: bundle.c,v 1.1.2.47 1998/04/16 00:25:49 brian Exp $ 277a6f8720SBrian Somers */ 287a6f8720SBrian Somers 292764b86aSBrian Somers #include <sys/types.h> 307a6f8720SBrian Somers #include <sys/socket.h> 317a6f8720SBrian Somers #include <netinet/in.h> 327a6f8720SBrian Somers #include <net/if.h> 337a6f8720SBrian Somers #include <arpa/inet.h> 347a6f8720SBrian Somers #include <net/route.h> 357a6f8720SBrian Somers #include <net/if_dl.h> 36eaa4df37SBrian Somers #include <netinet/in_systm.h> 37eaa4df37SBrian Somers #include <netinet/ip.h> 387a6f8720SBrian Somers 397a6f8720SBrian Somers #include <errno.h> 407a6f8720SBrian Somers #include <fcntl.h> 417a6f8720SBrian Somers #include <stdio.h> 427a6f8720SBrian Somers #include <string.h> 437a6f8720SBrian Somers #include <sys/ioctl.h> 447a6f8720SBrian Somers #include <termios.h> 457a6f8720SBrian Somers #include <unistd.h> 467a6f8720SBrian Somers 477a6f8720SBrian Somers #include "command.h" 487a6f8720SBrian Somers #include "mbuf.h" 497a6f8720SBrian Somers #include "log.h" 507a6f8720SBrian Somers #include "id.h" 517a6f8720SBrian Somers #include "defs.h" 527a6f8720SBrian Somers #include "timer.h" 537a6f8720SBrian Somers #include "fsm.h" 547a6f8720SBrian Somers #include "iplist.h" 55879ed6faSBrian Somers #include "lqr.h" 56455aabc3SBrian Somers #include "hdlc.h" 577a6f8720SBrian Somers #include "throughput.h" 58eaa4df37SBrian Somers #include "slcompress.h" 597a6f8720SBrian Somers #include "ipcp.h" 605ca5389aSBrian Somers #include "filter.h" 612f786681SBrian Somers #include "descriptor.h" 627a6f8720SBrian Somers #include "route.h" 637a6f8720SBrian Somers #include "lcp.h" 647a6f8720SBrian Somers #include "ccp.h" 653b0f8d2eSBrian Somers #include "link.h" 663b0f8d2eSBrian Somers #include "mp.h" 673b0f8d2eSBrian Somers #include "bundle.h" 68455aabc3SBrian Somers #include "async.h" 69455aabc3SBrian Somers #include "physical.h" 702289f246SBrian Somers #include "modem.h" 71455aabc3SBrian Somers #include "auth.h" 72455aabc3SBrian Somers #include "lcpproto.h" 73455aabc3SBrian Somers #include "chap.h" 74455aabc3SBrian Somers #include "tun.h" 7585b542cfSBrian Somers #include "prompt.h" 763006ec67SBrian Somers #include "chat.h" 773006ec67SBrian Somers #include "datalink.h" 783006ec67SBrian Somers #include "ip.h" 797a6f8720SBrian Somers 80455aabc3SBrian Somers static const char *PhaseNames[] = { 81455aabc3SBrian Somers "Dead", "Establish", "Authenticate", "Network", "Terminate" 82455aabc3SBrian Somers }; 83455aabc3SBrian Somers 84455aabc3SBrian Somers const char * 85455aabc3SBrian Somers bundle_PhaseName(struct bundle *bundle) 867a6f8720SBrian Somers { 87455aabc3SBrian Somers return bundle->phase <= PHASE_TERMINATE ? 88455aabc3SBrian Somers PhaseNames[bundle->phase] : "unknown"; 897a6f8720SBrian Somers } 907a6f8720SBrian Somers 91455aabc3SBrian Somers void 925563ebdeSBrian Somers bundle_NewPhase(struct bundle *bundle, u_int new) 93455aabc3SBrian Somers { 94aef795ccSBrian Somers if (new == bundle->phase) 95aef795ccSBrian Somers return; 96aef795ccSBrian Somers 97e2ebb036SBrian Somers if (new <= PHASE_TERMINATE) 98e2ebb036SBrian Somers LogPrintf(LogPHASE, "bundle: %s\n", PhaseNames[new]); 997a6f8720SBrian Somers 100455aabc3SBrian Somers switch (new) { 101455aabc3SBrian Somers case PHASE_DEAD: 102455aabc3SBrian Somers bundle->phase = new; 103455aabc3SBrian Somers break; 104455aabc3SBrian Somers 105455aabc3SBrian Somers case PHASE_ESTABLISH: 106455aabc3SBrian Somers bundle->phase = new; 107455aabc3SBrian Somers break; 108455aabc3SBrian Somers 109455aabc3SBrian Somers case PHASE_AUTHENTICATE: 110455aabc3SBrian Somers bundle->phase = new; 111b6217683SBrian Somers bundle_DisplayPrompt(bundle); 112455aabc3SBrian Somers break; 113455aabc3SBrian Somers 114455aabc3SBrian Somers case PHASE_NETWORK: 1155828db6dSBrian Somers ipcp_Setup(&bundle->ncp.ipcp); 1165828db6dSBrian Somers FsmUp(&bundle->ncp.ipcp.fsm); 1175828db6dSBrian Somers FsmOpen(&bundle->ncp.ipcp.fsm); 118455aabc3SBrian Somers /* Fall through */ 119455aabc3SBrian Somers 120455aabc3SBrian Somers case PHASE_TERMINATE: 121455aabc3SBrian Somers bundle->phase = new; 122b6217683SBrian Somers bundle_DisplayPrompt(bundle); 123455aabc3SBrian Somers break; 1247a6f8720SBrian Somers } 1257a6f8720SBrian Somers } 1267a6f8720SBrian Somers 1277a6f8720SBrian Somers static int 1287a6f8720SBrian Somers bundle_CleanInterface(const struct bundle *bundle) 1297a6f8720SBrian Somers { 1307a6f8720SBrian Somers int s; 1317a6f8720SBrian Somers struct ifreq ifrq; 1327a6f8720SBrian Somers struct ifaliasreq ifra; 1337a6f8720SBrian Somers 1347a6f8720SBrian Somers s = ID0socket(AF_INET, SOCK_DGRAM, 0); 1357a6f8720SBrian Somers if (s < 0) { 1367a6f8720SBrian Somers LogPrintf(LogERROR, "bundle_CleanInterface: socket(): %s\n", 1377a6f8720SBrian Somers strerror(errno)); 1387a6f8720SBrian Somers return (-1); 1397a6f8720SBrian Somers } 1407a6f8720SBrian Somers strncpy(ifrq.ifr_name, bundle->ifname, sizeof ifrq.ifr_name - 1); 1417a6f8720SBrian Somers ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 1427a6f8720SBrian Somers while (ID0ioctl(s, SIOCGIFADDR, &ifrq) == 0) { 1437a6f8720SBrian Somers memset(&ifra.ifra_mask, '\0', sizeof ifra.ifra_mask); 1447a6f8720SBrian Somers strncpy(ifra.ifra_name, bundle->ifname, sizeof ifra.ifra_name - 1); 1457a6f8720SBrian Somers ifra.ifra_name[sizeof ifra.ifra_name - 1] = '\0'; 1467a6f8720SBrian Somers ifra.ifra_addr = ifrq.ifr_addr; 1477a6f8720SBrian Somers if (ID0ioctl(s, SIOCGIFDSTADDR, &ifrq) < 0) { 1487a6f8720SBrian Somers if (ifra.ifra_addr.sa_family == AF_INET) 1497a6f8720SBrian Somers LogPrintf(LogERROR, 1507a6f8720SBrian Somers "bundle_CleanInterface: Can't get dst for %s on %s !\n", 1517a6f8720SBrian Somers inet_ntoa(((struct sockaddr_in *)&ifra.ifra_addr)->sin_addr), 1527a6f8720SBrian Somers bundle->ifname); 1537a6f8720SBrian Somers return 0; 1547a6f8720SBrian Somers } 1557a6f8720SBrian Somers ifra.ifra_broadaddr = ifrq.ifr_dstaddr; 1567a6f8720SBrian Somers if (ID0ioctl(s, SIOCDIFADDR, &ifra) < 0) { 1577a6f8720SBrian Somers if (ifra.ifra_addr.sa_family == AF_INET) 1587a6f8720SBrian Somers LogPrintf(LogERROR, 1597a6f8720SBrian Somers "bundle_CleanInterface: Can't delete %s address on %s !\n", 1607a6f8720SBrian Somers inet_ntoa(((struct sockaddr_in *)&ifra.ifra_addr)->sin_addr), 1617a6f8720SBrian Somers bundle->ifname); 1627a6f8720SBrian Somers return 0; 1637a6f8720SBrian Somers } 1647a6f8720SBrian Somers } 1657a6f8720SBrian Somers 1667a6f8720SBrian Somers return 1; 1677a6f8720SBrian Somers } 1687a6f8720SBrian Somers 1696d666775SBrian Somers static void 1706d666775SBrian Somers bundle_LayerStart(void *v, struct fsm *fp) 1717a6f8720SBrian Somers { 1723006ec67SBrian Somers /* The given FSM is about to start up ! */ 1737a6f8720SBrian Somers } 1747a6f8720SBrian Somers 1755cf4388bSBrian Somers 1765cf4388bSBrian Somers static void 1775cf4388bSBrian Somers bundle_Notify(struct bundle *bundle, char c) 1785cf4388bSBrian Somers { 1795cf4388bSBrian Somers if (bundle->notify.fd != -1) { 1805cf4388bSBrian Somers if (write(bundle->notify.fd, &c, 1) == 1) 1815cf4388bSBrian Somers LogPrintf(LogPHASE, "Parent notified of success.\n"); 1825cf4388bSBrian Somers else 1835cf4388bSBrian Somers LogPrintf(LogPHASE, "Failed to notify parent of success.\n"); 1845cf4388bSBrian Somers close(bundle->notify.fd); 1855cf4388bSBrian Somers bundle->notify.fd = -1; 1865cf4388bSBrian Somers } 1875cf4388bSBrian Somers } 1883b0f8d2eSBrian Somers 1896d666775SBrian Somers static void 1903b0f8d2eSBrian Somers bundle_vLayerUp(void *v, struct fsm *fp) 1913b0f8d2eSBrian Somers { 1923b0f8d2eSBrian Somers bundle_LayerUp((struct bundle *)v, fp); 1933b0f8d2eSBrian Somers } 1943b0f8d2eSBrian Somers 1953b0f8d2eSBrian Somers void 1963b0f8d2eSBrian Somers bundle_LayerUp(struct bundle *bundle, struct fsm *fp) 1977a6f8720SBrian Somers { 1983006ec67SBrian Somers /* 1993006ec67SBrian Somers * The given fsm is now up 2003b0f8d2eSBrian Somers * If it's an LCP (including MP initialisation), set our mtu 2013b0f8d2eSBrian Somers * (This routine is also called from mp_Init() with it's LCP) 202565e35e5SBrian Somers * If it's an NCP, tell our -background parent to go away. 2033b0f8d2eSBrian Somers * If it's the first NCP, start the idle timer. 2043006ec67SBrian Somers */ 2056d666775SBrian Somers 2065563ebdeSBrian Somers if (fp->proto == PROTO_LCP) { 2073b0f8d2eSBrian Somers if (bundle->ncp.mp.active) { 2083b0f8d2eSBrian Somers int speed; 2093b0f8d2eSBrian Somers struct datalink *dl; 2105563ebdeSBrian Somers 2113b0f8d2eSBrian Somers for (dl = bundle->links, speed = 0; dl; dl = dl->next) 2123b0f8d2eSBrian Somers speed += modem_Speed(dl->physical); 2133b0f8d2eSBrian Somers if (speed) 2143b0f8d2eSBrian Somers tun_configure(bundle, bundle->ncp.mp.link.lcp.his_mru, speed); 2153b0f8d2eSBrian Somers } else 2163b0f8d2eSBrian Somers tun_configure(bundle, fsm2lcp(fp)->his_mru, 2173b0f8d2eSBrian Somers modem_Speed(link2physical(fp->link))); 2183b0f8d2eSBrian Somers } else if (fp->proto == PROTO_IPCP) { 219ab886ad0SBrian Somers bundle_StartIdleTimer(bundle); 2205cf4388bSBrian Somers bundle_Notify(bundle, EX_NORMAL); 2217a6f8720SBrian Somers } 222ab886ad0SBrian Somers } 2237a6f8720SBrian Somers 2246d666775SBrian Somers static void 2256d666775SBrian Somers bundle_LayerDown(void *v, struct fsm *fp) 2266d666775SBrian Somers { 2276d666775SBrian Somers /* 2286d666775SBrian Somers * The given FSM has been told to come down. 229ab886ad0SBrian Somers * If it's our last NCP, stop the idle timer. 2303b0f8d2eSBrian Somers * If it's our last NCP *OR* LCP, enter TERMINATE phase. 2313b0f8d2eSBrian Somers * If it's an LCP and we're in multilink mode, adjust our tun speed. 2326d666775SBrian Somers */ 233ab886ad0SBrian Somers 234ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v; 235ab886ad0SBrian Somers 2363b0f8d2eSBrian Somers if (fp->proto == PROTO_IPCP) { 237ab886ad0SBrian Somers bundle_StopIdleTimer(bundle); 2383b0f8d2eSBrian Somers } else if (fp->proto == PROTO_LCP) { 2393b0f8d2eSBrian Somers int speed, others_active; 2403b0f8d2eSBrian Somers struct datalink *dl; 2413b0f8d2eSBrian Somers 2423b0f8d2eSBrian Somers others_active = 0; 2433b0f8d2eSBrian Somers for (dl = bundle->links, speed = 0; dl; dl = dl->next) 2443b0f8d2eSBrian Somers if (fp != &dl->physical->link.lcp.fsm && 2453b0f8d2eSBrian Somers dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) { 2463b0f8d2eSBrian Somers speed += modem_Speed(dl->physical); 2473b0f8d2eSBrian Somers others_active++; 2483b0f8d2eSBrian Somers } 2493b0f8d2eSBrian Somers if (bundle->ncp.mp.active && speed) 2503b0f8d2eSBrian Somers tun_configure(bundle, bundle->ncp.mp.link.lcp.his_mru, speed); 2513b0f8d2eSBrian Somers 2523b0f8d2eSBrian Somers if (!others_active) 2533b0f8d2eSBrian Somers bundle_NewPhase(bundle, PHASE_TERMINATE); 2543b0f8d2eSBrian Somers } 2556d666775SBrian Somers } 2566d666775SBrian Somers 2576d666775SBrian Somers static void 2586d666775SBrian Somers bundle_LayerFinish(void *v, struct fsm *fp) 2596d666775SBrian Somers { 2606d666775SBrian Somers /* The given fsm is now down (fp cannot be NULL) 2616d666775SBrian Somers * 2626d666775SBrian Somers * If it's the last LCP, FsmDown all NCPs 2633b0f8d2eSBrian Somers * If it's the last NCP, FsmClose all LCPs 2646d666775SBrian Somers */ 2656d666775SBrian Somers 2666d666775SBrian Somers struct bundle *bundle = (struct bundle *)v; 2676d666775SBrian Somers struct datalink *dl; 2686d666775SBrian Somers 2693b0f8d2eSBrian Somers if (fp->proto == PROTO_IPCP) { 27026afeaa2SBrian Somers if (bundle_Phase(bundle) != PHASE_DEAD) 27125092092SBrian Somers bundle_NewPhase(bundle, PHASE_TERMINATE); 2726d666775SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 2733b0f8d2eSBrian Somers datalink_Close(dl, 0); 2743b0f8d2eSBrian Somers FsmDown(fp); 2753b0f8d2eSBrian Somers FsmClose(fp); 2763b0f8d2eSBrian Somers } else if (fp->proto == PROTO_LCP) { 2773b0f8d2eSBrian Somers int others_active; 278a611cad6SBrian Somers 2793b0f8d2eSBrian Somers others_active = 0; 2803b0f8d2eSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 2813b0f8d2eSBrian Somers if (fp != &dl->physical->link.lcp.fsm && 2823b0f8d2eSBrian Somers dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 2833b0f8d2eSBrian Somers others_active++; 2843b0f8d2eSBrian Somers 2853b0f8d2eSBrian Somers if (!others_active) { 2865828db6dSBrian Somers FsmDown(&bundle->ncp.ipcp.fsm); 2875828db6dSBrian Somers FsmClose(&bundle->ncp.ipcp.fsm); /* ST_INITIAL please */ 2886d666775SBrian Somers } 2893b0f8d2eSBrian Somers } 2903b0f8d2eSBrian Somers } 2916d666775SBrian Somers 2927a6f8720SBrian Somers int 2937a6f8720SBrian Somers bundle_LinkIsUp(const struct bundle *bundle) 2947a6f8720SBrian Somers { 2955828db6dSBrian Somers return bundle->ncp.ipcp.fsm.state == ST_OPENED; 2967a6f8720SBrian Somers } 2977a6f8720SBrian Somers 2987a6f8720SBrian Somers void 2993006ec67SBrian Somers bundle_Close(struct bundle *bundle, const char *name, int staydown) 3007a6f8720SBrian Somers { 301455aabc3SBrian Somers /* 3023006ec67SBrian Somers * Please close the given datalink. 3033b0f8d2eSBrian Somers * If name == NULL or name is the last datalink, enter TERMINATE phase 3043b0f8d2eSBrian Somers * and FsmClose all NCPs (except our MP) 3053b0f8d2eSBrian Somers * If it isn't the last datalink, just Close that datalink. 306455aabc3SBrian Somers */ 3077a6f8720SBrian Somers 3083b0f8d2eSBrian Somers struct datalink *dl, *this_dl; 3093b0f8d2eSBrian Somers int others_active; 3103006ec67SBrian Somers 3113b0f8d2eSBrian Somers if (bundle->phase == PHASE_TERMINATE || bundle->phase == PHASE_DEAD) 3123b0f8d2eSBrian Somers return; 3133b0f8d2eSBrian Somers 3143b0f8d2eSBrian Somers others_active = 0; 3153b0f8d2eSBrian Somers this_dl = NULL; 3163b0f8d2eSBrian Somers 3173b0f8d2eSBrian Somers for (dl = bundle->links; dl; dl = dl->next) { 3183b0f8d2eSBrian Somers if (name && !strcasecmp(name, dl->name)) 3193b0f8d2eSBrian Somers this_dl = dl; 3203b0f8d2eSBrian Somers if (name == NULL || this_dl == dl) { 321d345321bSBrian Somers if (staydown) 3223006ec67SBrian Somers datalink_StayDown(dl); 3233b0f8d2eSBrian Somers } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 3243b0f8d2eSBrian Somers others_active++; 3253b0f8d2eSBrian Somers } 3263b0f8d2eSBrian Somers 3273b0f8d2eSBrian Somers if (name && this_dl == NULL) { 3283b0f8d2eSBrian Somers LogPrintf(LogWARN, "%s: Invalid datalink name\n", name); 3293b0f8d2eSBrian Somers return; 3303b0f8d2eSBrian Somers } 3313b0f8d2eSBrian Somers 3323b0f8d2eSBrian Somers if (!others_active) { 3333b0f8d2eSBrian Somers if (bundle->ncp.ipcp.fsm.state > ST_CLOSED || 3343b0f8d2eSBrian Somers bundle->ncp.ipcp.fsm.state == ST_STARTING) 3353b0f8d2eSBrian Somers FsmClose(&bundle->ncp.ipcp.fsm); 3363b0f8d2eSBrian Somers else { 3375828db6dSBrian Somers if (bundle->ncp.ipcp.fsm.state > ST_INITIAL) { 3385828db6dSBrian Somers FsmClose(&bundle->ncp.ipcp.fsm); 3395828db6dSBrian Somers FsmDown(&bundle->ncp.ipcp.fsm); 340d2fd8d77SBrian Somers } 341d345321bSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 342d345321bSBrian Somers datalink_Close(dl, staydown); 3437a6f8720SBrian Somers } 3443b0f8d2eSBrian Somers } else if (this_dl && this_dl->state != DATALINK_CLOSED && 3453b0f8d2eSBrian Somers this_dl->state != DATALINK_HANGUP) 3463b0f8d2eSBrian Somers datalink_Close(this_dl, staydown); 347d2fd8d77SBrian Somers } 3487a6f8720SBrian Somers 3492f786681SBrian Somers static int 3502f786681SBrian Somers bundle_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n) 3512f786681SBrian Somers { 3522f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d); 3532f786681SBrian Somers struct datalink *dl; 354b6217683SBrian Somers struct descriptor *desc; 3552f786681SBrian Somers int result; 3562f786681SBrian Somers 3572f786681SBrian Somers result = 0; 3582f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 3592f786681SBrian Somers result += descriptor_UpdateSet(&dl->desc, r, w, e, n); 3602f786681SBrian Somers 361b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 362b6217683SBrian Somers result += descriptor_UpdateSet(desc, r, w, e, n); 363b6217683SBrian Somers 3642f786681SBrian Somers return result; 3652f786681SBrian Somers } 3662f786681SBrian Somers 3672f786681SBrian Somers static int 3682f786681SBrian Somers bundle_IsSet(struct descriptor *d, const fd_set *fdset) 3692f786681SBrian Somers { 3702f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d); 3712f786681SBrian Somers struct datalink *dl; 372b6217683SBrian Somers struct descriptor *desc; 3732f786681SBrian Somers 3742f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 3752f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 3762f786681SBrian Somers return 1; 3772f786681SBrian Somers 378b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 379b6217683SBrian Somers if (descriptor_IsSet(desc, fdset)) 380b6217683SBrian Somers return 1; 381b6217683SBrian Somers 3822f786681SBrian Somers return 0; 3832f786681SBrian Somers } 3842f786681SBrian Somers 3852f786681SBrian Somers static void 3862f786681SBrian Somers bundle_DescriptorRead(struct descriptor *d, struct bundle *bundle, 3872f786681SBrian Somers const fd_set *fdset) 3882f786681SBrian Somers { 3892f786681SBrian Somers struct datalink *dl; 390b6217683SBrian Somers struct descriptor *desc; 3912f786681SBrian Somers 3922f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 3932f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 3942f786681SBrian Somers descriptor_Read(&dl->desc, bundle, fdset); 395b6217683SBrian Somers 396b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 397b6217683SBrian Somers if (descriptor_IsSet(desc, fdset)) 398b6217683SBrian Somers descriptor_Read(desc, bundle, fdset); 3992f786681SBrian Somers } 4002f786681SBrian Somers 4012f786681SBrian Somers static void 4022f786681SBrian Somers bundle_DescriptorWrite(struct descriptor *d, struct bundle *bundle, 4032f786681SBrian Somers const fd_set *fdset) 4042f786681SBrian Somers { 4052f786681SBrian Somers struct datalink *dl; 406b6217683SBrian Somers struct descriptor *desc; 4072f786681SBrian Somers 4082f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 4092f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 4102f786681SBrian Somers descriptor_Write(&dl->desc, bundle, fdset); 411b6217683SBrian Somers 412b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 413b6217683SBrian Somers if (descriptor_IsSet(desc, fdset)) 414b6217683SBrian Somers descriptor_Write(desc, bundle, fdset); 4152f786681SBrian Somers } 4162f786681SBrian Somers 4177a6f8720SBrian Somers 4187a6f8720SBrian Somers #define MAX_TUN 256 4197a6f8720SBrian Somers /* 4207a6f8720SBrian Somers * MAX_TUN is set at 256 because that is the largest minor number 4213b0f8d2eSBrian Somers * we can use (certainly with mknod(1) anyway). The search for a 4227a6f8720SBrian Somers * device aborts when it reaches the first `Device not configured' 4237a6f8720SBrian Somers * (ENXIO) or the third `No such file or directory' (ENOENT) error. 4247a6f8720SBrian Somers */ 4257a6f8720SBrian Somers struct bundle * 426565e35e5SBrian Somers bundle_Create(const char *prefix, struct prompt *prompt, int type) 4277a6f8720SBrian Somers { 4287a6f8720SBrian Somers int s, enoentcount, err; 4297a6f8720SBrian Somers struct ifreq ifrq; 4307a6f8720SBrian Somers static struct bundle bundle; /* there can be only one */ 4317a6f8720SBrian Somers 4327a6f8720SBrian Somers if (bundle.ifname != NULL) { /* Already allocated ! */ 4337a6f8720SBrian Somers LogPrintf(LogERROR, "bundle_Create: There's only one BUNDLE !\n"); 4347a6f8720SBrian Somers return NULL; 4357a6f8720SBrian Somers } 4367a6f8720SBrian Somers 4377a6f8720SBrian Somers err = ENOENT; 4387a6f8720SBrian Somers enoentcount = 0; 4397a6f8720SBrian Somers for (bundle.unit = 0; bundle.unit <= MAX_TUN; bundle.unit++) { 4407a6f8720SBrian Somers snprintf(bundle.dev, sizeof bundle.dev, "%s%d", prefix, bundle.unit); 4417a6f8720SBrian Somers bundle.tun_fd = ID0open(bundle.dev, O_RDWR); 4427a6f8720SBrian Somers if (bundle.tun_fd >= 0) 4437a6f8720SBrian Somers break; 4447a6f8720SBrian Somers if (errno == ENXIO) { 4457a6f8720SBrian Somers bundle.unit = MAX_TUN; 4467a6f8720SBrian Somers err = errno; 4477a6f8720SBrian Somers } else if (errno == ENOENT) { 4487a6f8720SBrian Somers if (++enoentcount > 2) 4497a6f8720SBrian Somers bundle.unit = MAX_TUN; 4507a6f8720SBrian Somers } else 4517a6f8720SBrian Somers err = errno; 4527a6f8720SBrian Somers } 4537a6f8720SBrian Somers 4547a6f8720SBrian Somers if (bundle.unit > MAX_TUN) { 455b6217683SBrian Somers prompt_Printf(prompt, "No tunnel device is available (%s).\n", 45685b542cfSBrian Somers strerror(err)); 4577a6f8720SBrian Somers return NULL; 4587a6f8720SBrian Somers } 4597a6f8720SBrian Somers 4607a6f8720SBrian Somers LogSetTun(bundle.unit); 4617a6f8720SBrian Somers 4627a6f8720SBrian Somers s = socket(AF_INET, SOCK_DGRAM, 0); 4637a6f8720SBrian Somers if (s < 0) { 4647a6f8720SBrian Somers LogPrintf(LogERROR, "bundle_Create: socket(): %s\n", strerror(errno)); 4657a6f8720SBrian Somers close(bundle.tun_fd); 4667a6f8720SBrian Somers return NULL; 4677a6f8720SBrian Somers } 4687a6f8720SBrian Somers 4697a6f8720SBrian Somers bundle.ifname = strrchr(bundle.dev, '/'); 4707a6f8720SBrian Somers if (bundle.ifname == NULL) 4717a6f8720SBrian Somers bundle.ifname = bundle.dev; 4727a6f8720SBrian Somers else 4737a6f8720SBrian Somers bundle.ifname++; 4747a6f8720SBrian Somers 4757a6f8720SBrian Somers /* 4767a6f8720SBrian Somers * Now, bring up the interface. 4777a6f8720SBrian Somers */ 4787a6f8720SBrian Somers memset(&ifrq, '\0', sizeof ifrq); 4797a6f8720SBrian Somers strncpy(ifrq.ifr_name, bundle.ifname, sizeof ifrq.ifr_name - 1); 4807a6f8720SBrian Somers ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 4817a6f8720SBrian Somers if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) { 4827a6f8720SBrian Somers LogPrintf(LogERROR, "OpenTunnel: ioctl(SIOCGIFFLAGS): %s\n", 4837a6f8720SBrian Somers strerror(errno)); 4847a6f8720SBrian Somers close(s); 4857a6f8720SBrian Somers close(bundle.tun_fd); 4867a6f8720SBrian Somers bundle.ifname = NULL; 4877a6f8720SBrian Somers return NULL; 4887a6f8720SBrian Somers } 4897a6f8720SBrian Somers ifrq.ifr_flags |= IFF_UP; 4907a6f8720SBrian Somers if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) { 4917a6f8720SBrian Somers LogPrintf(LogERROR, "OpenTunnel: ioctl(SIOCSIFFLAGS): %s\n", 4927a6f8720SBrian Somers strerror(errno)); 4937a6f8720SBrian Somers close(s); 4947a6f8720SBrian Somers close(bundle.tun_fd); 4957a6f8720SBrian Somers bundle.ifname = NULL; 4967a6f8720SBrian Somers return NULL; 4977a6f8720SBrian Somers } 4987a6f8720SBrian Somers 4997a6f8720SBrian Somers close(s); 5007a6f8720SBrian Somers 5017a6f8720SBrian Somers if ((bundle.ifIndex = GetIfIndex(bundle.ifname)) < 0) { 5027a6f8720SBrian Somers LogPrintf(LogERROR, "OpenTunnel: Can't find ifindex.\n"); 5037a6f8720SBrian Somers close(bundle.tun_fd); 5047a6f8720SBrian Somers bundle.ifname = NULL; 5057a6f8720SBrian Somers return NULL; 5067a6f8720SBrian Somers } 5077a6f8720SBrian Somers 508b6217683SBrian Somers prompt_Printf(prompt, "Using interface: %s\n", bundle.ifname); 5097a6f8720SBrian Somers LogPrintf(LogPHASE, "Using interface: %s\n", bundle.ifname); 5107a6f8720SBrian Somers 511820de6ebSBrian Somers bundle.routing_seq = 0; 512a0cbd833SBrian Somers bundle.phase = PHASE_DEAD; 513a0cbd833SBrian Somers bundle.CleaningUp = 0; 5147a6f8720SBrian Somers 5156d666775SBrian Somers bundle.fsm.LayerStart = bundle_LayerStart; 5163b0f8d2eSBrian Somers bundle.fsm.LayerUp = bundle_vLayerUp; 5176d666775SBrian Somers bundle.fsm.LayerDown = bundle_LayerDown; 5186d666775SBrian Somers bundle.fsm.LayerFinish = bundle_LayerFinish; 5196d666775SBrian Somers bundle.fsm.object = &bundle; 5207a6f8720SBrian Somers 521ab886ad0SBrian Somers bundle.cfg.idle_timeout = NCP_IDLE_TIMEOUT; 5221342caedSBrian Somers *bundle.cfg.auth.name = '\0'; 5231342caedSBrian Somers *bundle.cfg.auth.key = '\0'; 5241342caedSBrian Somers bundle.cfg.opt = OPT_IDCHECK | OPT_LOOPBACK | OPT_THROUGHPUT | OPT_UTMP; 525565e35e5SBrian Somers bundle.phys_type = type; 526ab886ad0SBrian Somers 527565e35e5SBrian Somers bundle.links = datalink_Create("default", &bundle, &bundle.fsm, type); 5283006ec67SBrian Somers if (bundle.links == NULL) { 5293006ec67SBrian Somers LogPrintf(LogERROR, "Cannot create data link: %s\n", strerror(errno)); 5306d666775SBrian Somers close(bundle.tun_fd); 5316d666775SBrian Somers bundle.ifname = NULL; 5322289f246SBrian Somers return NULL; 5332289f246SBrian Somers } 5342289f246SBrian Somers 5352f786681SBrian Somers bundle.desc.type = BUNDLE_DESCRIPTOR; 5362f786681SBrian Somers bundle.desc.next = NULL; 5372f786681SBrian Somers bundle.desc.UpdateSet = bundle_UpdateSet; 5382f786681SBrian Somers bundle.desc.IsSet = bundle_IsSet; 5392f786681SBrian Somers bundle.desc.Read = bundle_DescriptorRead; 5402f786681SBrian Somers bundle.desc.Write = bundle_DescriptorWrite; 5412f786681SBrian Somers 5421342caedSBrian Somers /* XXX: what's an IPCP link anyway :-( */ 5435828db6dSBrian Somers ipcp_Init(&bundle.ncp.ipcp, &bundle, &bundle.links->physical->link, 5445828db6dSBrian Somers &bundle.fsm); 5456d666775SBrian Somers 5465ca5389aSBrian Somers memset(&bundle.filter, '\0', sizeof bundle.filter); 5475ca5389aSBrian Somers bundle.filter.in.fragok = bundle.filter.in.logok = 1; 5485ca5389aSBrian Somers bundle.filter.in.name = "IN"; 5495ca5389aSBrian Somers bundle.filter.out.fragok = bundle.filter.out.logok = 1; 5505ca5389aSBrian Somers bundle.filter.out.name = "OUT"; 5515ca5389aSBrian Somers bundle.filter.dial.name = "DIAL"; 5528390b576SBrian Somers bundle.filter.dial.logok = 1; 5535ca5389aSBrian Somers bundle.filter.alive.name = "ALIVE"; 5545ca5389aSBrian Somers bundle.filter.alive.logok = 1; 55593ee0ff2SBrian Somers memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer); 55693ee0ff2SBrian Somers bundle.idle.done = 0; 5575cf4388bSBrian Somers bundle.notify.fd = -1; 55893ee0ff2SBrian Somers 5596d666775SBrian Somers /* Clean out any leftover crud */ 5606d666775SBrian Somers bundle_CleanInterface(&bundle); 5616d666775SBrian Somers 56285602e52SBrian Somers if (prompt) { 56385602e52SBrian Somers /* Retrospectively introduce ourselves to the prompt */ 56485602e52SBrian Somers prompt->bundle = &bundle; 56585602e52SBrian Somers bundle_RegisterDescriptor(&bundle, &prompt->desc); 56685602e52SBrian Somers } 56785602e52SBrian Somers 5687a6f8720SBrian Somers return &bundle; 5697a6f8720SBrian Somers } 5707a6f8720SBrian Somers 57168a0f0ccSBrian Somers static void 57268a0f0ccSBrian Somers bundle_DownInterface(struct bundle *bundle) 57368a0f0ccSBrian Somers { 57468a0f0ccSBrian Somers struct ifreq ifrq; 57568a0f0ccSBrian Somers int s; 57668a0f0ccSBrian Somers 57768a0f0ccSBrian Somers DeleteIfRoutes(bundle, 1); 57868a0f0ccSBrian Somers 57968a0f0ccSBrian Somers s = ID0socket(AF_INET, SOCK_DGRAM, 0); 58068a0f0ccSBrian Somers if (s < 0) { 58168a0f0ccSBrian Somers LogPrintf(LogERROR, "bundle_DownInterface: socket: %s\n", strerror(errno)); 58268a0f0ccSBrian Somers return; 58368a0f0ccSBrian Somers } 58468a0f0ccSBrian Somers 58568a0f0ccSBrian Somers memset(&ifrq, '\0', sizeof ifrq); 58668a0f0ccSBrian Somers strncpy(ifrq.ifr_name, bundle->ifname, sizeof ifrq.ifr_name - 1); 58768a0f0ccSBrian Somers ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 58868a0f0ccSBrian Somers if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) { 58968a0f0ccSBrian Somers LogPrintf(LogERROR, "bundle_DownInterface: ioctl(SIOCGIFFLAGS): %s\n", 59068a0f0ccSBrian Somers strerror(errno)); 59168a0f0ccSBrian Somers close(s); 59268a0f0ccSBrian Somers return; 59368a0f0ccSBrian Somers } 59468a0f0ccSBrian Somers ifrq.ifr_flags &= ~IFF_UP; 59568a0f0ccSBrian Somers if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) { 59668a0f0ccSBrian Somers LogPrintf(LogERROR, "bundle_DownInterface: ioctl(SIOCSIFFLAGS): %s\n", 59768a0f0ccSBrian Somers strerror(errno)); 59868a0f0ccSBrian Somers close(s); 59968a0f0ccSBrian Somers return; 60068a0f0ccSBrian Somers } 60168a0f0ccSBrian Somers close(s); 60268a0f0ccSBrian Somers } 60368a0f0ccSBrian Somers 60468a0f0ccSBrian Somers void 60568a0f0ccSBrian Somers bundle_Destroy(struct bundle *bundle) 60668a0f0ccSBrian Somers { 6073006ec67SBrian Somers struct datalink *dl; 608b6217683SBrian Somers struct descriptor *desc, *ndesc; 609b6217683SBrian Somers 610565e35e5SBrian Somers if (bundle->phys_type & PHYS_DEMAND) { 611565e35e5SBrian Somers IpcpCleanInterface(&bundle->ncp.ipcp); 61268a0f0ccSBrian Somers bundle_DownInterface(bundle); 61368a0f0ccSBrian Somers } 6143006ec67SBrian Somers 6153006ec67SBrian Somers dl = bundle->links; 6163006ec67SBrian Somers while (dl) 6173006ec67SBrian Somers dl = datalink_Destroy(dl); 6183006ec67SBrian Somers 6195cf4388bSBrian Somers bundle_Notify(bundle, EX_ERRDEAD); 620b6217683SBrian Somers 621b6217683SBrian Somers desc = bundle->desc.next; 622b6217683SBrian Somers while (desc) { 623b6217683SBrian Somers ndesc = desc->next; 624b6217683SBrian Somers if (desc->type == PROMPT_DESCRIPTOR) 625b6217683SBrian Somers prompt_Destroy((struct prompt *)desc, 1); 626b6217683SBrian Somers else 627b6217683SBrian Somers LogPrintf(LogERROR, "bundle_Destroy: Don't know how to delete descriptor" 628b6217683SBrian Somers " type %d\n", desc->type); 629b6217683SBrian Somers desc = ndesc; 630b6217683SBrian Somers } 631b6217683SBrian Somers bundle->desc.next = NULL; 63268a0f0ccSBrian Somers bundle->ifname = NULL; 63368a0f0ccSBrian Somers } 63468a0f0ccSBrian Somers 6357a6f8720SBrian Somers struct rtmsg { 6367a6f8720SBrian Somers struct rt_msghdr m_rtm; 6377a6f8720SBrian Somers char m_space[64]; 6387a6f8720SBrian Somers }; 6397a6f8720SBrian Somers 6407a6f8720SBrian Somers void 641820de6ebSBrian Somers bundle_SetRoute(struct bundle *bundle, int cmd, struct in_addr dst, 6427a6f8720SBrian Somers struct in_addr gateway, struct in_addr mask, int bang) 6437a6f8720SBrian Somers { 6447a6f8720SBrian Somers struct rtmsg rtmes; 6457a6f8720SBrian Somers int s, nb, wb; 6467a6f8720SBrian Somers char *cp; 6477a6f8720SBrian Somers const char *cmdstr; 6487a6f8720SBrian Somers struct sockaddr_in rtdata; 6497a6f8720SBrian Somers 6507a6f8720SBrian Somers if (bang) 6517a6f8720SBrian Somers cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!"); 6527a6f8720SBrian Somers else 6537a6f8720SBrian Somers cmdstr = (cmd == RTM_ADD ? "Add" : "Delete"); 6547a6f8720SBrian Somers s = ID0socket(PF_ROUTE, SOCK_RAW, 0); 6557a6f8720SBrian Somers if (s < 0) { 65668a0f0ccSBrian Somers LogPrintf(LogERROR, "bundle_SetRoute: socket(): %s\n", strerror(errno)); 6577a6f8720SBrian Somers return; 6587a6f8720SBrian Somers } 6597a6f8720SBrian Somers memset(&rtmes, '\0', sizeof rtmes); 6607a6f8720SBrian Somers rtmes.m_rtm.rtm_version = RTM_VERSION; 6617a6f8720SBrian Somers rtmes.m_rtm.rtm_type = cmd; 6627a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs = RTA_DST; 663820de6ebSBrian Somers rtmes.m_rtm.rtm_seq = ++bundle->routing_seq; 6647a6f8720SBrian Somers rtmes.m_rtm.rtm_pid = getpid(); 6657a6f8720SBrian Somers rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC; 6667a6f8720SBrian Somers 6677a6f8720SBrian Somers memset(&rtdata, '\0', sizeof rtdata); 6687a6f8720SBrian Somers rtdata.sin_len = 16; 6697a6f8720SBrian Somers rtdata.sin_family = AF_INET; 6707a6f8720SBrian Somers rtdata.sin_port = 0; 6717a6f8720SBrian Somers rtdata.sin_addr = dst; 6727a6f8720SBrian Somers 6737a6f8720SBrian Somers cp = rtmes.m_space; 6747a6f8720SBrian Somers memcpy(cp, &rtdata, 16); 6757a6f8720SBrian Somers cp += 16; 6767a6f8720SBrian Somers if (cmd == RTM_ADD) 6777a6f8720SBrian Somers if (gateway.s_addr == INADDR_ANY) { 6787a6f8720SBrian Somers /* Add a route through the interface */ 6797a6f8720SBrian Somers struct sockaddr_dl dl; 6807a6f8720SBrian Somers const char *iname; 6817a6f8720SBrian Somers int ilen; 6827a6f8720SBrian Somers 6837a6f8720SBrian Somers iname = Index2Nam(bundle->ifIndex); 6847a6f8720SBrian Somers ilen = strlen(iname); 6857a6f8720SBrian Somers dl.sdl_len = sizeof dl - sizeof dl.sdl_data + ilen; 6867a6f8720SBrian Somers dl.sdl_family = AF_LINK; 6877a6f8720SBrian Somers dl.sdl_index = bundle->ifIndex; 6887a6f8720SBrian Somers dl.sdl_type = 0; 6897a6f8720SBrian Somers dl.sdl_nlen = ilen; 6907a6f8720SBrian Somers dl.sdl_alen = 0; 6917a6f8720SBrian Somers dl.sdl_slen = 0; 6927a6f8720SBrian Somers strncpy(dl.sdl_data, iname, sizeof dl.sdl_data); 6937a6f8720SBrian Somers memcpy(cp, &dl, dl.sdl_len); 6947a6f8720SBrian Somers cp += dl.sdl_len; 6957a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 6967a6f8720SBrian Somers } else { 6977a6f8720SBrian Somers rtdata.sin_addr = gateway; 6987a6f8720SBrian Somers memcpy(cp, &rtdata, 16); 6997a6f8720SBrian Somers cp += 16; 7007a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 7017a6f8720SBrian Somers } 7027a6f8720SBrian Somers 7037a6f8720SBrian Somers if (dst.s_addr == INADDR_ANY) 7047a6f8720SBrian Somers mask.s_addr = INADDR_ANY; 7057a6f8720SBrian Somers 7067a6f8720SBrian Somers if (cmd == RTM_ADD || dst.s_addr == INADDR_ANY) { 7077a6f8720SBrian Somers rtdata.sin_addr = mask; 7087a6f8720SBrian Somers memcpy(cp, &rtdata, 16); 7097a6f8720SBrian Somers cp += 16; 7107a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_NETMASK; 7117a6f8720SBrian Somers } 7127a6f8720SBrian Somers 7137a6f8720SBrian Somers nb = cp - (char *) &rtmes; 7147a6f8720SBrian Somers rtmes.m_rtm.rtm_msglen = nb; 7157a6f8720SBrian Somers wb = ID0write(s, &rtmes, nb); 7167a6f8720SBrian Somers if (wb < 0) { 71768a0f0ccSBrian Somers LogPrintf(LogTCPIP, "bundle_SetRoute failure:\n"); 71868a0f0ccSBrian Somers LogPrintf(LogTCPIP, "bundle_SetRoute: Cmd = %s\n", cmd); 71968a0f0ccSBrian Somers LogPrintf(LogTCPIP, "bundle_SetRoute: Dst = %s\n", inet_ntoa(dst)); 72068a0f0ccSBrian Somers LogPrintf(LogTCPIP, "bundle_SetRoute: Gateway = %s\n", inet_ntoa(gateway)); 72168a0f0ccSBrian Somers LogPrintf(LogTCPIP, "bundle_SetRoute: Mask = %s\n", inet_ntoa(mask)); 7227a6f8720SBrian Somers failed: 7237a6f8720SBrian Somers if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST || 7247a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) 7257a6f8720SBrian Somers if (!bang) 7267a6f8720SBrian Somers LogPrintf(LogWARN, "Add route failed: %s already exists\n", 7277a6f8720SBrian Somers inet_ntoa(dst)); 7287a6f8720SBrian Somers else { 7297a6f8720SBrian Somers rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE; 7307a6f8720SBrian Somers if ((wb = ID0write(s, &rtmes, nb)) < 0) 7317a6f8720SBrian Somers goto failed; 7327a6f8720SBrian Somers } 7337a6f8720SBrian Somers else if (cmd == RTM_DELETE && 7347a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == ESRCH || 7357a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) { 7367a6f8720SBrian Somers if (!bang) 7377a6f8720SBrian Somers LogPrintf(LogWARN, "Del route failed: %s: Non-existent\n", 7387a6f8720SBrian Somers inet_ntoa(dst)); 7397a6f8720SBrian Somers } else if (rtmes.m_rtm.rtm_errno == 0) 7407a6f8720SBrian Somers LogPrintf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr, 7417a6f8720SBrian Somers inet_ntoa(dst), strerror(errno)); 7427a6f8720SBrian Somers else 7437a6f8720SBrian Somers LogPrintf(LogWARN, "%s route failed: %s: %s\n", 7447a6f8720SBrian Somers cmdstr, inet_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno)); 7457a6f8720SBrian Somers } 7467a6f8720SBrian Somers LogPrintf(LogDEBUG, "wrote %d: cmd = %s, dst = %x, gateway = %x\n", 7477a6f8720SBrian Somers wb, cmdstr, dst.s_addr, gateway.s_addr); 7487a6f8720SBrian Somers close(s); 7497a6f8720SBrian Somers } 75083d1af55SBrian Somers 75183d1af55SBrian Somers void 752565e35e5SBrian Somers bundle_LinkLost(struct bundle *bundle, struct physical *p, int staydown) 75383d1af55SBrian Somers { 754455aabc3SBrian Somers /* 7553006ec67SBrian Somers * Locate the appropriate datalink, and Down it. 7563006ec67SBrian Somers * 7573006ec67SBrian Somers * The LayerFinish() called from the datalinks LCP will 7583006ec67SBrian Somers * potentially Down our NCPs (if it's the last link). 7593006ec67SBrian Somers * 7603006ec67SBrian Somers * The LinkClosed() called when the datalink is finally in 7613006ec67SBrian Somers * the CLOSED state MAY cause the entire datalink to be deleted 7623006ec67SBrian Somers * and MAY cause a program exit. 763455aabc3SBrian Somers */ 76483d1af55SBrian Somers 765565e35e5SBrian Somers if (p->type == PHYS_STDIN || bundle->CleaningUp) 7665b8b8060SBrian Somers staydown = 1; 767565e35e5SBrian Somers datalink_Down(p->dl, staydown); 7683006ec67SBrian Somers } 7693006ec67SBrian Somers 7703006ec67SBrian Somers void 7713006ec67SBrian Somers bundle_LinkClosed(struct bundle *bundle, struct datalink *dl) 7723006ec67SBrian Somers { 7733006ec67SBrian Somers /* 7743006ec67SBrian Somers * Our datalink has closed. 775565e35e5SBrian Somers * UpdateSet() will remove 1OFF and STDIN links. 7763b0f8d2eSBrian Somers * If it's the last data link, enter phase DEAD. 7773006ec67SBrian Somers */ 7785b8b8060SBrian Somers 7793b0f8d2eSBrian Somers struct datalink *odl; 7803b0f8d2eSBrian Somers int other_links; 7815b8b8060SBrian Somers 7823b0f8d2eSBrian Somers other_links = 0; 7833b0f8d2eSBrian Somers for (odl = bundle->links; odl; odl = odl->next) 7843b0f8d2eSBrian Somers if (odl != dl && odl->state != DATALINK_CLOSED) 7853b0f8d2eSBrian Somers other_links++; 7863b0f8d2eSBrian Somers 7873b0f8d2eSBrian Somers if (!other_links) { 788565e35e5SBrian Somers if (dl->physical->type != PHYS_DEMAND) 7893006ec67SBrian Somers bundle_DownInterface(bundle); 79026afeaa2SBrian Somers if (bundle->ncp.ipcp.fsm.state > ST_CLOSED || 79126afeaa2SBrian Somers bundle->ncp.ipcp.fsm.state == ST_STARTING) { 79226afeaa2SBrian Somers FsmDown(&bundle->ncp.ipcp.fsm); 79326afeaa2SBrian Somers FsmClose(&bundle->ncp.ipcp.fsm); /* ST_INITIAL please */ 79426afeaa2SBrian Somers } 7955563ebdeSBrian Somers bundle_NewPhase(bundle, PHASE_DEAD); 796b6217683SBrian Somers bundle_DisplayPrompt(bundle); 7973b0f8d2eSBrian Somers } 798455aabc3SBrian Somers } 799455aabc3SBrian Somers 800455aabc3SBrian Somers void 801565e35e5SBrian Somers bundle_Open(struct bundle *bundle, const char *name, int mask) 8023006ec67SBrian Somers { 8033006ec67SBrian Somers /* 8043006ec67SBrian Somers * Please open the given datalink, or all if name == NULL 8053006ec67SBrian Somers */ 8063006ec67SBrian Somers struct datalink *dl; 8073006ec67SBrian Somers 8083006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 8093006ec67SBrian Somers if (name == NULL || !strcasecmp(dl->name, name)) { 810565e35e5SBrian Somers if (mask & dl->physical->type) 811565e35e5SBrian Somers datalink_Up(dl, 1, 1); 8123006ec67SBrian Somers if (name != NULL) 8133006ec67SBrian Somers break; 8143006ec67SBrian Somers } 8153006ec67SBrian Somers } 8163006ec67SBrian Somers 8173006ec67SBrian Somers struct datalink * 8183006ec67SBrian Somers bundle2datalink(struct bundle *bundle, const char *name) 8193006ec67SBrian Somers { 8203006ec67SBrian Somers struct datalink *dl; 8213006ec67SBrian Somers 8223006ec67SBrian Somers if (name != NULL) { 8233006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 8243006ec67SBrian Somers if (!strcasecmp(dl->name, name)) 8253006ec67SBrian Somers return dl; 8263006ec67SBrian Somers } else if (bundle->links && !bundle->links->next) 8273006ec67SBrian Somers return bundle->links; 8283006ec67SBrian Somers 8293006ec67SBrian Somers return NULL; 8303006ec67SBrian Somers } 8313006ec67SBrian Somers 8323006ec67SBrian Somers int 8333006ec67SBrian Somers bundle_FillQueues(struct bundle *bundle) 8343006ec67SBrian Somers { 8353b0f8d2eSBrian Somers int total; 8363b0f8d2eSBrian Somers 8373b0f8d2eSBrian Somers if (bundle->ncp.mp.active) { 8383b0f8d2eSBrian Somers total = mp_FillQueues(bundle); 8393b0f8d2eSBrian Somers } else { 840833882f7SBrian Somers total = link_QueueLen(&bundle->links->physical->link); 841833882f7SBrian Somers if (total == 0 && bundle->links->physical->out == NULL) 842833882f7SBrian Somers total = IpFlushPacket(&bundle->links->physical->link, bundle); 8433006ec67SBrian Somers } 8443006ec67SBrian Somers 8453b0f8d2eSBrian Somers return total + ip_QueueLen(); 8463006ec67SBrian Somers } 847aef795ccSBrian Somers 848aef795ccSBrian Somers int 849aef795ccSBrian Somers bundle_ShowLinks(struct cmdargs const *arg) 850aef795ccSBrian Somers { 851c7cc5030SBrian Somers if (arg->cx) 852b6217683SBrian Somers datalink_Show(arg->cx, arg->prompt); 853c7cc5030SBrian Somers else { 854aef795ccSBrian Somers struct datalink *dl; 855aef795ccSBrian Somers 856aef795ccSBrian Somers for (dl = arg->bundle->links; dl; dl = dl->next) 857b6217683SBrian Somers datalink_Show(dl, arg->prompt); 858c7cc5030SBrian Somers } 859aef795ccSBrian Somers 860aef795ccSBrian Somers return 0; 861aef795ccSBrian Somers } 862ab886ad0SBrian Somers 8631342caedSBrian Somers static const char * 8641342caedSBrian Somers optval(struct bundle *bundle, int bit) 8651342caedSBrian Somers { 8661342caedSBrian Somers return (bundle->cfg.opt & bit) ? "enabled" : "disabled"; 8671342caedSBrian Somers } 8681342caedSBrian Somers 869c08717dfSBrian Somers int 870c08717dfSBrian Somers bundle_ShowStatus(struct cmdargs const *arg) 871c08717dfSBrian Somers { 872c08717dfSBrian Somers int remaining; 873c08717dfSBrian Somers 874c08717dfSBrian Somers prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle)); 875c08717dfSBrian Somers prompt_Printf(arg->prompt, " Interface: %s\n", arg->bundle->dev); 876c08717dfSBrian Somers 877c08717dfSBrian Somers prompt_Printf(arg->prompt, "\nDefaults:\n"); 878c08717dfSBrian Somers prompt_Printf(arg->prompt, " Auth name: %s\n", arg->bundle->cfg.auth.name); 879c08717dfSBrian Somers prompt_Printf(arg->prompt, " Idle Timer: "); 880c08717dfSBrian Somers if (arg->bundle->cfg.idle_timeout) { 881c08717dfSBrian Somers prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle_timeout); 882c08717dfSBrian Somers remaining = bundle_RemainingIdleTime(arg->bundle); 883c08717dfSBrian Somers if (remaining != -1) 884c08717dfSBrian Somers prompt_Printf(arg->prompt, " (%ds remaining)", remaining); 885c08717dfSBrian Somers prompt_Printf(arg->prompt, "\n"); 886c08717dfSBrian Somers } else 887c08717dfSBrian Somers prompt_Printf(arg->prompt, "disabled\n"); 888c08717dfSBrian Somers 8891342caedSBrian Somers prompt_Printf(arg->prompt, " ID check: %s\n", 8901342caedSBrian Somers optval(arg->bundle, OPT_IDCHECK)); 8911342caedSBrian Somers prompt_Printf(arg->prompt, " Loopback: %s\n", 8921342caedSBrian Somers optval(arg->bundle, OPT_LOOPBACK)); 8931342caedSBrian Somers prompt_Printf(arg->prompt, " MS Ext: %s\n", 8941342caedSBrian Somers optval(arg->bundle, OPT_MSEXT)); 8951342caedSBrian Somers prompt_Printf(arg->prompt, " PasswdAuth: %s\n", 8961342caedSBrian Somers optval(arg->bundle, OPT_PASSWDAUTH)); 8971342caedSBrian Somers prompt_Printf(arg->prompt, " Proxy: %s\n", 8981342caedSBrian Somers optval(arg->bundle, OPT_PROXY)); 8991342caedSBrian Somers prompt_Printf(arg->prompt, " Throughput: %s\n", 9001342caedSBrian Somers optval(arg->bundle, OPT_THROUGHPUT)); 9011342caedSBrian Somers prompt_Printf(arg->prompt, " Utmp: %s\n", 9021342caedSBrian Somers optval(arg->bundle, OPT_UTMP)); 9031342caedSBrian Somers 904c08717dfSBrian Somers return 0; 905c08717dfSBrian Somers } 906c08717dfSBrian Somers 907ab886ad0SBrian Somers static void 908ab886ad0SBrian Somers bundle_IdleTimeout(void *v) 909ab886ad0SBrian Somers { 910ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v; 911ab886ad0SBrian Somers 91293ee0ff2SBrian Somers bundle->idle.done = 0; 913ab886ad0SBrian Somers LogPrintf(LogPHASE, "IPCP Idle timer expired.\n"); 914ab886ad0SBrian Somers bundle_Close(bundle, NULL, 1); 915ab886ad0SBrian Somers } 916ab886ad0SBrian Somers 917ab886ad0SBrian Somers /* 918ab886ad0SBrian Somers * Start Idle timer. If timeout is reached, we call bundle_Close() to 919ab886ad0SBrian Somers * close LCP and link. 920ab886ad0SBrian Somers */ 921ab886ad0SBrian Somers void 922ab886ad0SBrian Somers bundle_StartIdleTimer(struct bundle *bundle) 923ab886ad0SBrian Somers { 924565e35e5SBrian Somers if (!(bundle->phys_type & (PHYS_DEDICATED|PHYS_PERM)) && 925565e35e5SBrian Somers bundle->cfg.idle_timeout) { 92693ee0ff2SBrian Somers StopTimer(&bundle->idle.timer); 92793ee0ff2SBrian Somers bundle->idle.timer.func = bundle_IdleTimeout; 9283b0f8d2eSBrian Somers bundle->idle.timer.name = "idle"; 92993ee0ff2SBrian Somers bundle->idle.timer.load = bundle->cfg.idle_timeout * SECTICKS; 93093ee0ff2SBrian Somers bundle->idle.timer.state = TIMER_STOPPED; 93193ee0ff2SBrian Somers bundle->idle.timer.arg = bundle; 93293ee0ff2SBrian Somers StartTimer(&bundle->idle.timer); 93393ee0ff2SBrian Somers bundle->idle.done = time(NULL) + bundle->cfg.idle_timeout; 934ab886ad0SBrian Somers } 935ab886ad0SBrian Somers } 936ab886ad0SBrian Somers 937ab886ad0SBrian Somers void 938ab886ad0SBrian Somers bundle_SetIdleTimer(struct bundle *bundle, int value) 939ab886ad0SBrian Somers { 940ab886ad0SBrian Somers bundle->cfg.idle_timeout = value; 941ab886ad0SBrian Somers if (bundle_LinkIsUp(bundle)) 942ab886ad0SBrian Somers bundle_StartIdleTimer(bundle); 943ab886ad0SBrian Somers } 944ab886ad0SBrian Somers 945ab886ad0SBrian Somers void 946ab886ad0SBrian Somers bundle_StopIdleTimer(struct bundle *bundle) 947ab886ad0SBrian Somers { 94893ee0ff2SBrian Somers StopTimer(&bundle->idle.timer); 9494a632c80SBrian Somers bundle->idle.done = 0; 950ab886ad0SBrian Somers } 951ab886ad0SBrian Somers 952ab886ad0SBrian Somers int 953ab886ad0SBrian Somers bundle_RemainingIdleTime(struct bundle *bundle) 954ab886ad0SBrian Somers { 95593ee0ff2SBrian Somers if (bundle->idle.done) 95693ee0ff2SBrian Somers return bundle->idle.done - time(NULL); 957ab886ad0SBrian Somers return -1; 958ab886ad0SBrian Somers } 9593b0f8d2eSBrian Somers 9603b0f8d2eSBrian Somers int 9613b0f8d2eSBrian Somers bundle_IsDead(struct bundle *bundle) 9623b0f8d2eSBrian Somers { 9633b0f8d2eSBrian Somers return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp); 9643b0f8d2eSBrian Somers } 965b6217683SBrian Somers 966b6217683SBrian Somers void 967b6217683SBrian Somers bundle_RegisterDescriptor(struct bundle *bundle, struct descriptor *d) 968b6217683SBrian Somers { 969b6217683SBrian Somers d->next = bundle->desc.next; 970b6217683SBrian Somers bundle->desc.next = d; 971b6217683SBrian Somers } 972b6217683SBrian Somers 973b6217683SBrian Somers void 974b6217683SBrian Somers bundle_UnRegisterDescriptor(struct bundle *bundle, struct descriptor *d) 975b6217683SBrian Somers { 976b6217683SBrian Somers struct descriptor **desc; 977b6217683SBrian Somers 978b6217683SBrian Somers for (desc = &bundle->desc.next; *desc; desc = &(*desc)->next) 979b6217683SBrian Somers if (*desc == d) { 980b6217683SBrian Somers *desc = d->next; 981b6217683SBrian Somers break; 982b6217683SBrian Somers } 983b6217683SBrian Somers } 984b6217683SBrian Somers 985b6217683SBrian Somers void 986b6217683SBrian Somers bundle_DelPromptDescriptors(struct bundle *bundle, struct server *s) 987b6217683SBrian Somers { 988b6217683SBrian Somers struct descriptor **desc; 989b6217683SBrian Somers struct prompt *p; 990b6217683SBrian Somers 991b6217683SBrian Somers desc = &bundle->desc.next; 992b6217683SBrian Somers while (*desc) { 993b6217683SBrian Somers if ((*desc)->type == PROMPT_DESCRIPTOR) { 994b6217683SBrian Somers p = (struct prompt *)*desc; 995b6217683SBrian Somers if (p->owner == s) { 996b6217683SBrian Somers prompt_Destroy(p, 1); 997b6217683SBrian Somers desc = &bundle->desc.next; 998b6217683SBrian Somers continue; 999b6217683SBrian Somers } 1000b6217683SBrian Somers } 1001b6217683SBrian Somers desc = &(*desc)->next; 1002b6217683SBrian Somers } 1003b6217683SBrian Somers } 1004b6217683SBrian Somers 1005b6217683SBrian Somers void 1006b6217683SBrian Somers bundle_DisplayPrompt(struct bundle *bundle) 1007b6217683SBrian Somers { 1008b6217683SBrian Somers struct descriptor **desc; 1009b6217683SBrian Somers 1010b6217683SBrian Somers for (desc = &bundle->desc.next; *desc; desc = &(*desc)->next) 1011b6217683SBrian Somers if ((*desc)->type == PROMPT_DESCRIPTOR) 1012b6217683SBrian Somers prompt_Required((struct prompt *)*desc); 1013b6217683SBrian Somers } 1014b6217683SBrian Somers 1015b6217683SBrian Somers void 1016b6217683SBrian Somers bundle_WriteTermPrompt(struct bundle *bundle, struct datalink *dl, 1017b6217683SBrian Somers const char *data, int len) 1018b6217683SBrian Somers { 1019b6217683SBrian Somers struct descriptor *desc; 1020b6217683SBrian Somers struct prompt *p; 1021b6217683SBrian Somers 1022b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 1023b6217683SBrian Somers if (desc->type == PROMPT_DESCRIPTOR) { 1024b6217683SBrian Somers p = (struct prompt *)desc; 1025b6217683SBrian Somers if (prompt_IsTermMode(p, dl)) 1026c3a119d0SBrian Somers prompt_Printf(p, "%.*s", len, data); 1027b6217683SBrian Somers } 1028b6217683SBrian Somers } 1029b6217683SBrian Somers 1030b6217683SBrian Somers void 1031b6217683SBrian Somers bundle_SetTtyCommandMode(struct bundle *bundle, struct datalink *dl) 1032b6217683SBrian Somers { 1033b6217683SBrian Somers struct descriptor *desc; 1034b6217683SBrian Somers struct prompt *p; 1035b6217683SBrian Somers 1036b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 1037b6217683SBrian Somers if (desc->type == PROMPT_DESCRIPTOR) { 1038b6217683SBrian Somers p = (struct prompt *)desc; 1039b6217683SBrian Somers if (prompt_IsTermMode(p, dl)) 1040b6217683SBrian Somers prompt_TtyCommandMode(p); 1041b6217683SBrian Somers } 1042b6217683SBrian Somers } 1043565e35e5SBrian Somers 1044565e35e5SBrian Somers static void 1045565e35e5SBrian Somers bundle_GenPhysType(struct bundle *bundle) 1046565e35e5SBrian Somers { 1047565e35e5SBrian Somers struct datalink *dl; 1048565e35e5SBrian Somers 1049565e35e5SBrian Somers bundle->phys_type = 0; 1050565e35e5SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 1051565e35e5SBrian Somers bundle->phys_type |= dl->physical->type; 1052565e35e5SBrian Somers } 1053565e35e5SBrian Somers 1054cd7bd93aSBrian Somers void 1055cd7bd93aSBrian Somers bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl, 1056cd7bd93aSBrian Somers const char *name) 1057cd7bd93aSBrian Somers { 1058cd7bd93aSBrian Somers struct datalink *ndl = datalink_Clone(dl, name); 1059cd7bd93aSBrian Somers 1060cd7bd93aSBrian Somers ndl->next = dl->next; 1061cd7bd93aSBrian Somers dl->next = ndl; 1062565e35e5SBrian Somers bundle_GenPhysType(bundle); 1063cd7bd93aSBrian Somers } 1064cd7bd93aSBrian Somers 1065cd7bd93aSBrian Somers void 1066cd7bd93aSBrian Somers bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl) 1067cd7bd93aSBrian Somers { 1068cd7bd93aSBrian Somers struct datalink **dlp; 1069cd7bd93aSBrian Somers 1070cd7bd93aSBrian Somers if (dl->state == DATALINK_CLOSED) 1071cd7bd93aSBrian Somers for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next) 1072cd7bd93aSBrian Somers if (*dlp == dl) { 1073cd7bd93aSBrian Somers *dlp = datalink_Destroy(dl); 1074cd7bd93aSBrian Somers break; 1075cd7bd93aSBrian Somers } 1076565e35e5SBrian Somers bundle_GenPhysType(bundle); 1077565e35e5SBrian Somers } 1078565e35e5SBrian Somers 1079565e35e5SBrian Somers void 1080565e35e5SBrian Somers bundle_CleanDatalinks(struct bundle *bundle) 1081565e35e5SBrian Somers { 1082565e35e5SBrian Somers struct datalink **dlp = &bundle->links; 1083565e35e5SBrian Somers 1084565e35e5SBrian Somers while (*dlp) 1085565e35e5SBrian Somers if ((*dlp)->state == DATALINK_CLOSED && 1086565e35e5SBrian Somers (*dlp)->physical->type & (PHYS_STDIN|PHYS_1OFF)) 1087565e35e5SBrian Somers *dlp = datalink_Destroy(*dlp); 1088565e35e5SBrian Somers else 1089565e35e5SBrian Somers dlp = &(*dlp)->next; 1090565e35e5SBrian Somers bundle_GenPhysType(bundle); 1091cd7bd93aSBrian Somers } 1092