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 * 2625092092SBrian Somers * $Id: bundle.c,v 1.1.2.45 1998/04/11 21:50:37 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) { 27025092092SBrian Somers bundle_NewPhase(bundle, PHASE_TERMINATE); 2716d666775SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 2723b0f8d2eSBrian Somers datalink_Close(dl, 0); 2733b0f8d2eSBrian Somers FsmDown(fp); 2743b0f8d2eSBrian Somers FsmClose(fp); 2753b0f8d2eSBrian Somers } else if (fp->proto == PROTO_LCP) { 2763b0f8d2eSBrian Somers int others_active; 277a611cad6SBrian Somers 2783b0f8d2eSBrian Somers others_active = 0; 2793b0f8d2eSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 2803b0f8d2eSBrian Somers if (fp != &dl->physical->link.lcp.fsm && 2813b0f8d2eSBrian Somers dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 2823b0f8d2eSBrian Somers others_active++; 2833b0f8d2eSBrian Somers 2843b0f8d2eSBrian Somers if (!others_active) { 2855828db6dSBrian Somers FsmDown(&bundle->ncp.ipcp.fsm); 2865828db6dSBrian Somers FsmClose(&bundle->ncp.ipcp.fsm); /* ST_INITIAL please */ 2876d666775SBrian Somers } 2883b0f8d2eSBrian Somers } 2893b0f8d2eSBrian Somers } 2906d666775SBrian Somers 2917a6f8720SBrian Somers int 2927a6f8720SBrian Somers bundle_LinkIsUp(const struct bundle *bundle) 2937a6f8720SBrian Somers { 2945828db6dSBrian Somers return bundle->ncp.ipcp.fsm.state == ST_OPENED; 2957a6f8720SBrian Somers } 2967a6f8720SBrian Somers 2977a6f8720SBrian Somers void 2983006ec67SBrian Somers bundle_Close(struct bundle *bundle, const char *name, int staydown) 2997a6f8720SBrian Somers { 300455aabc3SBrian Somers /* 3013006ec67SBrian Somers * Please close the given datalink. 3023b0f8d2eSBrian Somers * If name == NULL or name is the last datalink, enter TERMINATE phase 3033b0f8d2eSBrian Somers * and FsmClose all NCPs (except our MP) 3043b0f8d2eSBrian Somers * If it isn't the last datalink, just Close that datalink. 305455aabc3SBrian Somers */ 3067a6f8720SBrian Somers 3073b0f8d2eSBrian Somers struct datalink *dl, *this_dl; 3083b0f8d2eSBrian Somers int others_active; 3093006ec67SBrian Somers 3103b0f8d2eSBrian Somers if (bundle->phase == PHASE_TERMINATE || bundle->phase == PHASE_DEAD) 3113b0f8d2eSBrian Somers return; 3123b0f8d2eSBrian Somers 3133b0f8d2eSBrian Somers others_active = 0; 3143b0f8d2eSBrian Somers this_dl = NULL; 3153b0f8d2eSBrian Somers 3163b0f8d2eSBrian Somers for (dl = bundle->links; dl; dl = dl->next) { 3173b0f8d2eSBrian Somers if (name && !strcasecmp(name, dl->name)) 3183b0f8d2eSBrian Somers this_dl = dl; 3193b0f8d2eSBrian Somers if (name == NULL || this_dl == dl) { 320d345321bSBrian Somers if (staydown) 3213006ec67SBrian Somers datalink_StayDown(dl); 3223b0f8d2eSBrian Somers } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 3233b0f8d2eSBrian Somers others_active++; 3243b0f8d2eSBrian Somers } 3253b0f8d2eSBrian Somers 3263b0f8d2eSBrian Somers if (name && this_dl == NULL) { 3273b0f8d2eSBrian Somers LogPrintf(LogWARN, "%s: Invalid datalink name\n", name); 3283b0f8d2eSBrian Somers return; 3293b0f8d2eSBrian Somers } 3303b0f8d2eSBrian Somers 3313b0f8d2eSBrian Somers if (!others_active) { 3323b0f8d2eSBrian Somers if (bundle->ncp.ipcp.fsm.state > ST_CLOSED || 3333b0f8d2eSBrian Somers bundle->ncp.ipcp.fsm.state == ST_STARTING) 3343b0f8d2eSBrian Somers FsmClose(&bundle->ncp.ipcp.fsm); 3353b0f8d2eSBrian Somers else { 3365828db6dSBrian Somers if (bundle->ncp.ipcp.fsm.state > ST_INITIAL) { 3375828db6dSBrian Somers FsmClose(&bundle->ncp.ipcp.fsm); 3385828db6dSBrian Somers FsmDown(&bundle->ncp.ipcp.fsm); 339d2fd8d77SBrian Somers } 340d345321bSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 341d345321bSBrian Somers datalink_Close(dl, staydown); 3427a6f8720SBrian Somers } 3433b0f8d2eSBrian Somers } else if (this_dl && this_dl->state != DATALINK_CLOSED && 3443b0f8d2eSBrian Somers this_dl->state != DATALINK_HANGUP) 3453b0f8d2eSBrian Somers datalink_Close(this_dl, staydown); 346d2fd8d77SBrian Somers } 3477a6f8720SBrian Somers 3482f786681SBrian Somers static int 3492f786681SBrian Somers bundle_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n) 3502f786681SBrian Somers { 3512f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d); 3522f786681SBrian Somers struct datalink *dl; 353b6217683SBrian Somers struct descriptor *desc; 3542f786681SBrian Somers int result; 3552f786681SBrian Somers 3562f786681SBrian Somers result = 0; 3572f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 3582f786681SBrian Somers result += descriptor_UpdateSet(&dl->desc, r, w, e, n); 3592f786681SBrian Somers 360b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 361b6217683SBrian Somers result += descriptor_UpdateSet(desc, r, w, e, n); 362b6217683SBrian Somers 3632f786681SBrian Somers return result; 3642f786681SBrian Somers } 3652f786681SBrian Somers 3662f786681SBrian Somers static int 3672f786681SBrian Somers bundle_IsSet(struct descriptor *d, const fd_set *fdset) 3682f786681SBrian Somers { 3692f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d); 3702f786681SBrian Somers struct datalink *dl; 371b6217683SBrian Somers struct descriptor *desc; 3722f786681SBrian Somers 3732f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 3742f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 3752f786681SBrian Somers return 1; 3762f786681SBrian Somers 377b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 378b6217683SBrian Somers if (descriptor_IsSet(desc, fdset)) 379b6217683SBrian Somers return 1; 380b6217683SBrian Somers 3812f786681SBrian Somers return 0; 3822f786681SBrian Somers } 3832f786681SBrian Somers 3842f786681SBrian Somers static void 3852f786681SBrian Somers bundle_DescriptorRead(struct descriptor *d, struct bundle *bundle, 3862f786681SBrian Somers const fd_set *fdset) 3872f786681SBrian Somers { 3882f786681SBrian Somers struct datalink *dl; 389b6217683SBrian Somers struct descriptor *desc; 3902f786681SBrian Somers 3912f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 3922f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 3932f786681SBrian Somers descriptor_Read(&dl->desc, bundle, fdset); 394b6217683SBrian Somers 395b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 396b6217683SBrian Somers if (descriptor_IsSet(desc, fdset)) 397b6217683SBrian Somers descriptor_Read(desc, bundle, fdset); 3982f786681SBrian Somers } 3992f786681SBrian Somers 4002f786681SBrian Somers static void 4012f786681SBrian Somers bundle_DescriptorWrite(struct descriptor *d, struct bundle *bundle, 4022f786681SBrian Somers const fd_set *fdset) 4032f786681SBrian Somers { 4042f786681SBrian Somers struct datalink *dl; 405b6217683SBrian Somers struct descriptor *desc; 4062f786681SBrian Somers 4072f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 4082f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 4092f786681SBrian Somers descriptor_Write(&dl->desc, bundle, fdset); 410b6217683SBrian Somers 411b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 412b6217683SBrian Somers if (descriptor_IsSet(desc, fdset)) 413b6217683SBrian Somers descriptor_Write(desc, bundle, fdset); 4142f786681SBrian Somers } 4152f786681SBrian Somers 4167a6f8720SBrian Somers 4177a6f8720SBrian Somers #define MAX_TUN 256 4187a6f8720SBrian Somers /* 4197a6f8720SBrian Somers * MAX_TUN is set at 256 because that is the largest minor number 4203b0f8d2eSBrian Somers * we can use (certainly with mknod(1) anyway). The search for a 4217a6f8720SBrian Somers * device aborts when it reaches the first `Device not configured' 4227a6f8720SBrian Somers * (ENXIO) or the third `No such file or directory' (ENOENT) error. 4237a6f8720SBrian Somers */ 4247a6f8720SBrian Somers struct bundle * 425565e35e5SBrian Somers bundle_Create(const char *prefix, struct prompt *prompt, int type) 4267a6f8720SBrian Somers { 4277a6f8720SBrian Somers int s, enoentcount, err; 4287a6f8720SBrian Somers struct ifreq ifrq; 4297a6f8720SBrian Somers static struct bundle bundle; /* there can be only one */ 4307a6f8720SBrian Somers 4317a6f8720SBrian Somers if (bundle.ifname != NULL) { /* Already allocated ! */ 4327a6f8720SBrian Somers LogPrintf(LogERROR, "bundle_Create: There's only one BUNDLE !\n"); 4337a6f8720SBrian Somers return NULL; 4347a6f8720SBrian Somers } 4357a6f8720SBrian Somers 4367a6f8720SBrian Somers err = ENOENT; 4377a6f8720SBrian Somers enoentcount = 0; 4387a6f8720SBrian Somers for (bundle.unit = 0; bundle.unit <= MAX_TUN; bundle.unit++) { 4397a6f8720SBrian Somers snprintf(bundle.dev, sizeof bundle.dev, "%s%d", prefix, bundle.unit); 4407a6f8720SBrian Somers bundle.tun_fd = ID0open(bundle.dev, O_RDWR); 4417a6f8720SBrian Somers if (bundle.tun_fd >= 0) 4427a6f8720SBrian Somers break; 4437a6f8720SBrian Somers if (errno == ENXIO) { 4447a6f8720SBrian Somers bundle.unit = MAX_TUN; 4457a6f8720SBrian Somers err = errno; 4467a6f8720SBrian Somers } else if (errno == ENOENT) { 4477a6f8720SBrian Somers if (++enoentcount > 2) 4487a6f8720SBrian Somers bundle.unit = MAX_TUN; 4497a6f8720SBrian Somers } else 4507a6f8720SBrian Somers err = errno; 4517a6f8720SBrian Somers } 4527a6f8720SBrian Somers 4537a6f8720SBrian Somers if (bundle.unit > MAX_TUN) { 454b6217683SBrian Somers prompt_Printf(prompt, "No tunnel device is available (%s).\n", 45585b542cfSBrian Somers strerror(err)); 4567a6f8720SBrian Somers return NULL; 4577a6f8720SBrian Somers } 4587a6f8720SBrian Somers 4597a6f8720SBrian Somers LogSetTun(bundle.unit); 4607a6f8720SBrian Somers 4617a6f8720SBrian Somers s = socket(AF_INET, SOCK_DGRAM, 0); 4627a6f8720SBrian Somers if (s < 0) { 4637a6f8720SBrian Somers LogPrintf(LogERROR, "bundle_Create: socket(): %s\n", strerror(errno)); 4647a6f8720SBrian Somers close(bundle.tun_fd); 4657a6f8720SBrian Somers return NULL; 4667a6f8720SBrian Somers } 4677a6f8720SBrian Somers 4687a6f8720SBrian Somers bundle.ifname = strrchr(bundle.dev, '/'); 4697a6f8720SBrian Somers if (bundle.ifname == NULL) 4707a6f8720SBrian Somers bundle.ifname = bundle.dev; 4717a6f8720SBrian Somers else 4727a6f8720SBrian Somers bundle.ifname++; 4737a6f8720SBrian Somers 4747a6f8720SBrian Somers /* 4757a6f8720SBrian Somers * Now, bring up the interface. 4767a6f8720SBrian Somers */ 4777a6f8720SBrian Somers memset(&ifrq, '\0', sizeof ifrq); 4787a6f8720SBrian Somers strncpy(ifrq.ifr_name, bundle.ifname, sizeof ifrq.ifr_name - 1); 4797a6f8720SBrian Somers ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 4807a6f8720SBrian Somers if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) { 4817a6f8720SBrian Somers LogPrintf(LogERROR, "OpenTunnel: ioctl(SIOCGIFFLAGS): %s\n", 4827a6f8720SBrian Somers strerror(errno)); 4837a6f8720SBrian Somers close(s); 4847a6f8720SBrian Somers close(bundle.tun_fd); 4857a6f8720SBrian Somers bundle.ifname = NULL; 4867a6f8720SBrian Somers return NULL; 4877a6f8720SBrian Somers } 4887a6f8720SBrian Somers ifrq.ifr_flags |= IFF_UP; 4897a6f8720SBrian Somers if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) { 4907a6f8720SBrian Somers LogPrintf(LogERROR, "OpenTunnel: ioctl(SIOCSIFFLAGS): %s\n", 4917a6f8720SBrian Somers strerror(errno)); 4927a6f8720SBrian Somers close(s); 4937a6f8720SBrian Somers close(bundle.tun_fd); 4947a6f8720SBrian Somers bundle.ifname = NULL; 4957a6f8720SBrian Somers return NULL; 4967a6f8720SBrian Somers } 4977a6f8720SBrian Somers 4987a6f8720SBrian Somers close(s); 4997a6f8720SBrian Somers 5007a6f8720SBrian Somers if ((bundle.ifIndex = GetIfIndex(bundle.ifname)) < 0) { 5017a6f8720SBrian Somers LogPrintf(LogERROR, "OpenTunnel: Can't find ifindex.\n"); 5027a6f8720SBrian Somers close(bundle.tun_fd); 5037a6f8720SBrian Somers bundle.ifname = NULL; 5047a6f8720SBrian Somers return NULL; 5057a6f8720SBrian Somers } 5067a6f8720SBrian Somers 507b6217683SBrian Somers prompt_Printf(prompt, "Using interface: %s\n", bundle.ifname); 5087a6f8720SBrian Somers LogPrintf(LogPHASE, "Using interface: %s\n", bundle.ifname); 5097a6f8720SBrian Somers 510820de6ebSBrian Somers bundle.routing_seq = 0; 511a0cbd833SBrian Somers bundle.phase = PHASE_DEAD; 512a0cbd833SBrian Somers bundle.CleaningUp = 0; 5137a6f8720SBrian Somers 5146d666775SBrian Somers bundle.fsm.LayerStart = bundle_LayerStart; 5153b0f8d2eSBrian Somers bundle.fsm.LayerUp = bundle_vLayerUp; 5166d666775SBrian Somers bundle.fsm.LayerDown = bundle_LayerDown; 5176d666775SBrian Somers bundle.fsm.LayerFinish = bundle_LayerFinish; 5186d666775SBrian Somers bundle.fsm.object = &bundle; 5197a6f8720SBrian Somers 520ab886ad0SBrian Somers bundle.cfg.idle_timeout = NCP_IDLE_TIMEOUT; 521565e35e5SBrian Somers bundle.phys_type = type; 522ab886ad0SBrian Somers 523565e35e5SBrian Somers bundle.links = datalink_Create("default", &bundle, &bundle.fsm, type); 5243006ec67SBrian Somers if (bundle.links == NULL) { 5253006ec67SBrian Somers LogPrintf(LogERROR, "Cannot create data link: %s\n", strerror(errno)); 5266d666775SBrian Somers close(bundle.tun_fd); 5276d666775SBrian Somers bundle.ifname = NULL; 5282289f246SBrian Somers return NULL; 5292289f246SBrian Somers } 5302289f246SBrian Somers 5312f786681SBrian Somers bundle.desc.type = BUNDLE_DESCRIPTOR; 5322f786681SBrian Somers bundle.desc.next = NULL; 5332f786681SBrian Somers bundle.desc.UpdateSet = bundle_UpdateSet; 5342f786681SBrian Somers bundle.desc.IsSet = bundle_IsSet; 5352f786681SBrian Somers bundle.desc.Read = bundle_DescriptorRead; 5362f786681SBrian Somers bundle.desc.Write = bundle_DescriptorWrite; 5372f786681SBrian Somers 5385828db6dSBrian Somers ipcp_Init(&bundle.ncp.ipcp, &bundle, &bundle.links->physical->link, 5395828db6dSBrian Somers &bundle.fsm); 5406d666775SBrian Somers 5415ca5389aSBrian Somers memset(&bundle.filter, '\0', sizeof bundle.filter); 5425ca5389aSBrian Somers bundle.filter.in.fragok = bundle.filter.in.logok = 1; 5435ca5389aSBrian Somers bundle.filter.in.name = "IN"; 5445ca5389aSBrian Somers bundle.filter.out.fragok = bundle.filter.out.logok = 1; 5455ca5389aSBrian Somers bundle.filter.out.name = "OUT"; 5465ca5389aSBrian Somers bundle.filter.dial.name = "DIAL"; 5478390b576SBrian Somers bundle.filter.dial.logok = 1; 5485ca5389aSBrian Somers bundle.filter.alive.name = "ALIVE"; 5495ca5389aSBrian Somers bundle.filter.alive.logok = 1; 55093ee0ff2SBrian Somers memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer); 55193ee0ff2SBrian Somers bundle.idle.done = 0; 5525cf4388bSBrian Somers bundle.notify.fd = -1; 55393ee0ff2SBrian Somers 5546d666775SBrian Somers /* Clean out any leftover crud */ 5556d666775SBrian Somers bundle_CleanInterface(&bundle); 5566d666775SBrian Somers 55785602e52SBrian Somers if (prompt) { 55885602e52SBrian Somers /* Retrospectively introduce ourselves to the prompt */ 55985602e52SBrian Somers prompt->bundle = &bundle; 56085602e52SBrian Somers bundle_RegisterDescriptor(&bundle, &prompt->desc); 56185602e52SBrian Somers } 56285602e52SBrian Somers 5637a6f8720SBrian Somers return &bundle; 5647a6f8720SBrian Somers } 5657a6f8720SBrian Somers 56668a0f0ccSBrian Somers static void 56768a0f0ccSBrian Somers bundle_DownInterface(struct bundle *bundle) 56868a0f0ccSBrian Somers { 56968a0f0ccSBrian Somers struct ifreq ifrq; 57068a0f0ccSBrian Somers int s; 57168a0f0ccSBrian Somers 57268a0f0ccSBrian Somers DeleteIfRoutes(bundle, 1); 57368a0f0ccSBrian Somers 57468a0f0ccSBrian Somers s = ID0socket(AF_INET, SOCK_DGRAM, 0); 57568a0f0ccSBrian Somers if (s < 0) { 57668a0f0ccSBrian Somers LogPrintf(LogERROR, "bundle_DownInterface: socket: %s\n", strerror(errno)); 57768a0f0ccSBrian Somers return; 57868a0f0ccSBrian Somers } 57968a0f0ccSBrian Somers 58068a0f0ccSBrian Somers memset(&ifrq, '\0', sizeof ifrq); 58168a0f0ccSBrian Somers strncpy(ifrq.ifr_name, bundle->ifname, sizeof ifrq.ifr_name - 1); 58268a0f0ccSBrian Somers ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 58368a0f0ccSBrian Somers if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) { 58468a0f0ccSBrian Somers LogPrintf(LogERROR, "bundle_DownInterface: ioctl(SIOCGIFFLAGS): %s\n", 58568a0f0ccSBrian Somers strerror(errno)); 58668a0f0ccSBrian Somers close(s); 58768a0f0ccSBrian Somers return; 58868a0f0ccSBrian Somers } 58968a0f0ccSBrian Somers ifrq.ifr_flags &= ~IFF_UP; 59068a0f0ccSBrian Somers if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) { 59168a0f0ccSBrian Somers LogPrintf(LogERROR, "bundle_DownInterface: ioctl(SIOCSIFFLAGS): %s\n", 59268a0f0ccSBrian Somers strerror(errno)); 59368a0f0ccSBrian Somers close(s); 59468a0f0ccSBrian Somers return; 59568a0f0ccSBrian Somers } 59668a0f0ccSBrian Somers close(s); 59768a0f0ccSBrian Somers } 59868a0f0ccSBrian Somers 59968a0f0ccSBrian Somers void 60068a0f0ccSBrian Somers bundle_Destroy(struct bundle *bundle) 60168a0f0ccSBrian Somers { 6023006ec67SBrian Somers struct datalink *dl; 603b6217683SBrian Somers struct descriptor *desc, *ndesc; 604b6217683SBrian Somers 605565e35e5SBrian Somers if (bundle->phys_type & PHYS_DEMAND) { 606565e35e5SBrian Somers IpcpCleanInterface(&bundle->ncp.ipcp); 60768a0f0ccSBrian Somers bundle_DownInterface(bundle); 60868a0f0ccSBrian Somers } 6093006ec67SBrian Somers 6103006ec67SBrian Somers dl = bundle->links; 6113006ec67SBrian Somers while (dl) 6123006ec67SBrian Somers dl = datalink_Destroy(dl); 6133006ec67SBrian Somers 6145cf4388bSBrian Somers bundle_Notify(bundle, EX_ERRDEAD); 615b6217683SBrian Somers 616b6217683SBrian Somers desc = bundle->desc.next; 617b6217683SBrian Somers while (desc) { 618b6217683SBrian Somers ndesc = desc->next; 619b6217683SBrian Somers if (desc->type == PROMPT_DESCRIPTOR) 620b6217683SBrian Somers prompt_Destroy((struct prompt *)desc, 1); 621b6217683SBrian Somers else 622b6217683SBrian Somers LogPrintf(LogERROR, "bundle_Destroy: Don't know how to delete descriptor" 623b6217683SBrian Somers " type %d\n", desc->type); 624b6217683SBrian Somers desc = ndesc; 625b6217683SBrian Somers } 626b6217683SBrian Somers bundle->desc.next = NULL; 62768a0f0ccSBrian Somers bundle->ifname = NULL; 62868a0f0ccSBrian Somers } 62968a0f0ccSBrian Somers 6307a6f8720SBrian Somers struct rtmsg { 6317a6f8720SBrian Somers struct rt_msghdr m_rtm; 6327a6f8720SBrian Somers char m_space[64]; 6337a6f8720SBrian Somers }; 6347a6f8720SBrian Somers 6357a6f8720SBrian Somers void 636820de6ebSBrian Somers bundle_SetRoute(struct bundle *bundle, int cmd, struct in_addr dst, 6377a6f8720SBrian Somers struct in_addr gateway, struct in_addr mask, int bang) 6387a6f8720SBrian Somers { 6397a6f8720SBrian Somers struct rtmsg rtmes; 6407a6f8720SBrian Somers int s, nb, wb; 6417a6f8720SBrian Somers char *cp; 6427a6f8720SBrian Somers const char *cmdstr; 6437a6f8720SBrian Somers struct sockaddr_in rtdata; 6447a6f8720SBrian Somers 6457a6f8720SBrian Somers if (bang) 6467a6f8720SBrian Somers cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!"); 6477a6f8720SBrian Somers else 6487a6f8720SBrian Somers cmdstr = (cmd == RTM_ADD ? "Add" : "Delete"); 6497a6f8720SBrian Somers s = ID0socket(PF_ROUTE, SOCK_RAW, 0); 6507a6f8720SBrian Somers if (s < 0) { 65168a0f0ccSBrian Somers LogPrintf(LogERROR, "bundle_SetRoute: socket(): %s\n", strerror(errno)); 6527a6f8720SBrian Somers return; 6537a6f8720SBrian Somers } 6547a6f8720SBrian Somers memset(&rtmes, '\0', sizeof rtmes); 6557a6f8720SBrian Somers rtmes.m_rtm.rtm_version = RTM_VERSION; 6567a6f8720SBrian Somers rtmes.m_rtm.rtm_type = cmd; 6577a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs = RTA_DST; 658820de6ebSBrian Somers rtmes.m_rtm.rtm_seq = ++bundle->routing_seq; 6597a6f8720SBrian Somers rtmes.m_rtm.rtm_pid = getpid(); 6607a6f8720SBrian Somers rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC; 6617a6f8720SBrian Somers 6627a6f8720SBrian Somers memset(&rtdata, '\0', sizeof rtdata); 6637a6f8720SBrian Somers rtdata.sin_len = 16; 6647a6f8720SBrian Somers rtdata.sin_family = AF_INET; 6657a6f8720SBrian Somers rtdata.sin_port = 0; 6667a6f8720SBrian Somers rtdata.sin_addr = dst; 6677a6f8720SBrian Somers 6687a6f8720SBrian Somers cp = rtmes.m_space; 6697a6f8720SBrian Somers memcpy(cp, &rtdata, 16); 6707a6f8720SBrian Somers cp += 16; 6717a6f8720SBrian Somers if (cmd == RTM_ADD) 6727a6f8720SBrian Somers if (gateway.s_addr == INADDR_ANY) { 6737a6f8720SBrian Somers /* Add a route through the interface */ 6747a6f8720SBrian Somers struct sockaddr_dl dl; 6757a6f8720SBrian Somers const char *iname; 6767a6f8720SBrian Somers int ilen; 6777a6f8720SBrian Somers 6787a6f8720SBrian Somers iname = Index2Nam(bundle->ifIndex); 6797a6f8720SBrian Somers ilen = strlen(iname); 6807a6f8720SBrian Somers dl.sdl_len = sizeof dl - sizeof dl.sdl_data + ilen; 6817a6f8720SBrian Somers dl.sdl_family = AF_LINK; 6827a6f8720SBrian Somers dl.sdl_index = bundle->ifIndex; 6837a6f8720SBrian Somers dl.sdl_type = 0; 6847a6f8720SBrian Somers dl.sdl_nlen = ilen; 6857a6f8720SBrian Somers dl.sdl_alen = 0; 6867a6f8720SBrian Somers dl.sdl_slen = 0; 6877a6f8720SBrian Somers strncpy(dl.sdl_data, iname, sizeof dl.sdl_data); 6887a6f8720SBrian Somers memcpy(cp, &dl, dl.sdl_len); 6897a6f8720SBrian Somers cp += dl.sdl_len; 6907a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 6917a6f8720SBrian Somers } else { 6927a6f8720SBrian Somers rtdata.sin_addr = gateway; 6937a6f8720SBrian Somers memcpy(cp, &rtdata, 16); 6947a6f8720SBrian Somers cp += 16; 6957a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 6967a6f8720SBrian Somers } 6977a6f8720SBrian Somers 6987a6f8720SBrian Somers if (dst.s_addr == INADDR_ANY) 6997a6f8720SBrian Somers mask.s_addr = INADDR_ANY; 7007a6f8720SBrian Somers 7017a6f8720SBrian Somers if (cmd == RTM_ADD || dst.s_addr == INADDR_ANY) { 7027a6f8720SBrian Somers rtdata.sin_addr = mask; 7037a6f8720SBrian Somers memcpy(cp, &rtdata, 16); 7047a6f8720SBrian Somers cp += 16; 7057a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_NETMASK; 7067a6f8720SBrian Somers } 7077a6f8720SBrian Somers 7087a6f8720SBrian Somers nb = cp - (char *) &rtmes; 7097a6f8720SBrian Somers rtmes.m_rtm.rtm_msglen = nb; 7107a6f8720SBrian Somers wb = ID0write(s, &rtmes, nb); 7117a6f8720SBrian Somers if (wb < 0) { 71268a0f0ccSBrian Somers LogPrintf(LogTCPIP, "bundle_SetRoute failure:\n"); 71368a0f0ccSBrian Somers LogPrintf(LogTCPIP, "bundle_SetRoute: Cmd = %s\n", cmd); 71468a0f0ccSBrian Somers LogPrintf(LogTCPIP, "bundle_SetRoute: Dst = %s\n", inet_ntoa(dst)); 71568a0f0ccSBrian Somers LogPrintf(LogTCPIP, "bundle_SetRoute: Gateway = %s\n", inet_ntoa(gateway)); 71668a0f0ccSBrian Somers LogPrintf(LogTCPIP, "bundle_SetRoute: Mask = %s\n", inet_ntoa(mask)); 7177a6f8720SBrian Somers failed: 7187a6f8720SBrian Somers if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST || 7197a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) 7207a6f8720SBrian Somers if (!bang) 7217a6f8720SBrian Somers LogPrintf(LogWARN, "Add route failed: %s already exists\n", 7227a6f8720SBrian Somers inet_ntoa(dst)); 7237a6f8720SBrian Somers else { 7247a6f8720SBrian Somers rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE; 7257a6f8720SBrian Somers if ((wb = ID0write(s, &rtmes, nb)) < 0) 7267a6f8720SBrian Somers goto failed; 7277a6f8720SBrian Somers } 7287a6f8720SBrian Somers else if (cmd == RTM_DELETE && 7297a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == ESRCH || 7307a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) { 7317a6f8720SBrian Somers if (!bang) 7327a6f8720SBrian Somers LogPrintf(LogWARN, "Del route failed: %s: Non-existent\n", 7337a6f8720SBrian Somers inet_ntoa(dst)); 7347a6f8720SBrian Somers } else if (rtmes.m_rtm.rtm_errno == 0) 7357a6f8720SBrian Somers LogPrintf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr, 7367a6f8720SBrian Somers inet_ntoa(dst), strerror(errno)); 7377a6f8720SBrian Somers else 7387a6f8720SBrian Somers LogPrintf(LogWARN, "%s route failed: %s: %s\n", 7397a6f8720SBrian Somers cmdstr, inet_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno)); 7407a6f8720SBrian Somers } 7417a6f8720SBrian Somers LogPrintf(LogDEBUG, "wrote %d: cmd = %s, dst = %x, gateway = %x\n", 7427a6f8720SBrian Somers wb, cmdstr, dst.s_addr, gateway.s_addr); 7437a6f8720SBrian Somers close(s); 7447a6f8720SBrian Somers } 74583d1af55SBrian Somers 74683d1af55SBrian Somers void 747565e35e5SBrian Somers bundle_LinkLost(struct bundle *bundle, struct physical *p, int staydown) 74883d1af55SBrian Somers { 749455aabc3SBrian Somers /* 7503006ec67SBrian Somers * Locate the appropriate datalink, and Down it. 7513006ec67SBrian Somers * 7523006ec67SBrian Somers * The LayerFinish() called from the datalinks LCP will 7533006ec67SBrian Somers * potentially Down our NCPs (if it's the last link). 7543006ec67SBrian Somers * 7553006ec67SBrian Somers * The LinkClosed() called when the datalink is finally in 7563006ec67SBrian Somers * the CLOSED state MAY cause the entire datalink to be deleted 7573006ec67SBrian Somers * and MAY cause a program exit. 758455aabc3SBrian Somers */ 75983d1af55SBrian Somers 760565e35e5SBrian Somers if (p->type == PHYS_STDIN || bundle->CleaningUp) 7615b8b8060SBrian Somers staydown = 1; 762565e35e5SBrian Somers datalink_Down(p->dl, staydown); 7633006ec67SBrian Somers } 7643006ec67SBrian Somers 7653006ec67SBrian Somers void 7663006ec67SBrian Somers bundle_LinkClosed(struct bundle *bundle, struct datalink *dl) 7673006ec67SBrian Somers { 7683006ec67SBrian Somers /* 7693006ec67SBrian Somers * Our datalink has closed. 770565e35e5SBrian Somers * UpdateSet() will remove 1OFF and STDIN links. 7713b0f8d2eSBrian Somers * If it's the last data link, enter phase DEAD. 7723006ec67SBrian Somers */ 7735b8b8060SBrian Somers 7743b0f8d2eSBrian Somers struct datalink *odl; 7753b0f8d2eSBrian Somers int other_links; 7765b8b8060SBrian Somers 7773b0f8d2eSBrian Somers other_links = 0; 7783b0f8d2eSBrian Somers for (odl = bundle->links; odl; odl = odl->next) 7793b0f8d2eSBrian Somers if (odl != dl && odl->state != DATALINK_CLOSED) 7803b0f8d2eSBrian Somers other_links++; 7813b0f8d2eSBrian Somers 7823b0f8d2eSBrian Somers if (!other_links) { 783565e35e5SBrian Somers if (dl->physical->type != PHYS_DEMAND) 7843006ec67SBrian Somers bundle_DownInterface(bundle); 7855563ebdeSBrian Somers bundle_NewPhase(bundle, PHASE_DEAD); 786b6217683SBrian Somers bundle_DisplayPrompt(bundle); 7873b0f8d2eSBrian Somers } 788455aabc3SBrian Somers } 789455aabc3SBrian Somers 790455aabc3SBrian Somers void 791565e35e5SBrian Somers bundle_Open(struct bundle *bundle, const char *name, int mask) 7923006ec67SBrian Somers { 7933006ec67SBrian Somers /* 7943006ec67SBrian Somers * Please open the given datalink, or all if name == NULL 7953006ec67SBrian Somers */ 7963006ec67SBrian Somers struct datalink *dl; 7973006ec67SBrian Somers 7983006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 7993006ec67SBrian Somers if (name == NULL || !strcasecmp(dl->name, name)) { 800565e35e5SBrian Somers if (mask & dl->physical->type) 801565e35e5SBrian Somers datalink_Up(dl, 1, 1); 8023006ec67SBrian Somers if (name != NULL) 8033006ec67SBrian Somers break; 8043006ec67SBrian Somers } 8053006ec67SBrian Somers } 8063006ec67SBrian Somers 8073006ec67SBrian Somers struct datalink * 8083006ec67SBrian Somers bundle2datalink(struct bundle *bundle, const char *name) 8093006ec67SBrian Somers { 8103006ec67SBrian Somers struct datalink *dl; 8113006ec67SBrian Somers 8123006ec67SBrian Somers if (name != NULL) { 8133006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 8143006ec67SBrian Somers if (!strcasecmp(dl->name, name)) 8153006ec67SBrian Somers return dl; 8163006ec67SBrian Somers } else if (bundle->links && !bundle->links->next) 8173006ec67SBrian Somers return bundle->links; 8183006ec67SBrian Somers 8193006ec67SBrian Somers return NULL; 8203006ec67SBrian Somers } 8213006ec67SBrian Somers 8223006ec67SBrian Somers int 8233006ec67SBrian Somers bundle_FillQueues(struct bundle *bundle) 8243006ec67SBrian Somers { 8253b0f8d2eSBrian Somers int total; 8263b0f8d2eSBrian Somers 8273b0f8d2eSBrian Somers if (bundle->ncp.mp.active) { 8283b0f8d2eSBrian Somers total = mp_FillQueues(bundle); 8293b0f8d2eSBrian Somers } else { 830833882f7SBrian Somers total = link_QueueLen(&bundle->links->physical->link); 831833882f7SBrian Somers if (total == 0 && bundle->links->physical->out == NULL) 832833882f7SBrian Somers total = IpFlushPacket(&bundle->links->physical->link, bundle); 8333006ec67SBrian Somers } 8343006ec67SBrian Somers 8353b0f8d2eSBrian Somers return total + ip_QueueLen(); 8363006ec67SBrian Somers } 837aef795ccSBrian Somers 838aef795ccSBrian Somers int 839aef795ccSBrian Somers bundle_ShowLinks(struct cmdargs const *arg) 840aef795ccSBrian Somers { 841c7cc5030SBrian Somers if (arg->cx) 842b6217683SBrian Somers datalink_Show(arg->cx, arg->prompt); 843c7cc5030SBrian Somers else { 844aef795ccSBrian Somers struct datalink *dl; 845aef795ccSBrian Somers 846aef795ccSBrian Somers for (dl = arg->bundle->links; dl; dl = dl->next) 847b6217683SBrian Somers datalink_Show(dl, arg->prompt); 848c7cc5030SBrian Somers } 849aef795ccSBrian Somers 850aef795ccSBrian Somers return 0; 851aef795ccSBrian Somers } 852ab886ad0SBrian Somers 853c08717dfSBrian Somers int 854c08717dfSBrian Somers bundle_ShowStatus(struct cmdargs const *arg) 855c08717dfSBrian Somers { 856c08717dfSBrian Somers int remaining; 857c08717dfSBrian Somers 858c08717dfSBrian Somers prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle)); 859c08717dfSBrian Somers prompt_Printf(arg->prompt, " Interface: %s\n", arg->bundle->dev); 860c08717dfSBrian Somers 861c08717dfSBrian Somers prompt_Printf(arg->prompt, "\nDefaults:\n"); 862c08717dfSBrian Somers prompt_Printf(arg->prompt, " Auth name: %s\n", arg->bundle->cfg.auth.name); 863c08717dfSBrian Somers prompt_Printf(arg->prompt, " Idle Timer: "); 864c08717dfSBrian Somers if (arg->bundle->cfg.idle_timeout) { 865c08717dfSBrian Somers prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle_timeout); 866c08717dfSBrian Somers remaining = bundle_RemainingIdleTime(arg->bundle); 867c08717dfSBrian Somers if (remaining != -1) 868c08717dfSBrian Somers prompt_Printf(arg->prompt, " (%ds remaining)", remaining); 869c08717dfSBrian Somers prompt_Printf(arg->prompt, "\n"); 870c08717dfSBrian Somers } else 871c08717dfSBrian Somers prompt_Printf(arg->prompt, "disabled\n"); 872c08717dfSBrian Somers 873c08717dfSBrian Somers return 0; 874c08717dfSBrian Somers } 875c08717dfSBrian Somers 876ab886ad0SBrian Somers static void 877ab886ad0SBrian Somers bundle_IdleTimeout(void *v) 878ab886ad0SBrian Somers { 879ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v; 880ab886ad0SBrian Somers 88193ee0ff2SBrian Somers bundle->idle.done = 0; 882ab886ad0SBrian Somers LogPrintf(LogPHASE, "IPCP Idle timer expired.\n"); 883ab886ad0SBrian Somers bundle_Close(bundle, NULL, 1); 884ab886ad0SBrian Somers } 885ab886ad0SBrian Somers 886ab886ad0SBrian Somers /* 887ab886ad0SBrian Somers * Start Idle timer. If timeout is reached, we call bundle_Close() to 888ab886ad0SBrian Somers * close LCP and link. 889ab886ad0SBrian Somers */ 890ab886ad0SBrian Somers void 891ab886ad0SBrian Somers bundle_StartIdleTimer(struct bundle *bundle) 892ab886ad0SBrian Somers { 893565e35e5SBrian Somers if (!(bundle->phys_type & (PHYS_DEDICATED|PHYS_PERM)) && 894565e35e5SBrian Somers bundle->cfg.idle_timeout) { 89593ee0ff2SBrian Somers StopTimer(&bundle->idle.timer); 89693ee0ff2SBrian Somers bundle->idle.timer.func = bundle_IdleTimeout; 8973b0f8d2eSBrian Somers bundle->idle.timer.name = "idle"; 89893ee0ff2SBrian Somers bundle->idle.timer.load = bundle->cfg.idle_timeout * SECTICKS; 89993ee0ff2SBrian Somers bundle->idle.timer.state = TIMER_STOPPED; 90093ee0ff2SBrian Somers bundle->idle.timer.arg = bundle; 90193ee0ff2SBrian Somers StartTimer(&bundle->idle.timer); 90293ee0ff2SBrian Somers bundle->idle.done = time(NULL) + bundle->cfg.idle_timeout; 903ab886ad0SBrian Somers } 904ab886ad0SBrian Somers } 905ab886ad0SBrian Somers 906ab886ad0SBrian Somers void 907ab886ad0SBrian Somers bundle_SetIdleTimer(struct bundle *bundle, int value) 908ab886ad0SBrian Somers { 909ab886ad0SBrian Somers bundle->cfg.idle_timeout = value; 910ab886ad0SBrian Somers if (bundle_LinkIsUp(bundle)) 911ab886ad0SBrian Somers bundle_StartIdleTimer(bundle); 912ab886ad0SBrian Somers } 913ab886ad0SBrian Somers 914ab886ad0SBrian Somers void 915ab886ad0SBrian Somers bundle_StopIdleTimer(struct bundle *bundle) 916ab886ad0SBrian Somers { 91793ee0ff2SBrian Somers StopTimer(&bundle->idle.timer); 9184a632c80SBrian Somers bundle->idle.done = 0; 919ab886ad0SBrian Somers } 920ab886ad0SBrian Somers 921ab886ad0SBrian Somers int 922ab886ad0SBrian Somers bundle_RemainingIdleTime(struct bundle *bundle) 923ab886ad0SBrian Somers { 92493ee0ff2SBrian Somers if (bundle->idle.done) 92593ee0ff2SBrian Somers return bundle->idle.done - time(NULL); 926ab886ad0SBrian Somers return -1; 927ab886ad0SBrian Somers } 9283b0f8d2eSBrian Somers 9293b0f8d2eSBrian Somers int 9303b0f8d2eSBrian Somers bundle_IsDead(struct bundle *bundle) 9313b0f8d2eSBrian Somers { 9323b0f8d2eSBrian Somers return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp); 9333b0f8d2eSBrian Somers } 934b6217683SBrian Somers 935b6217683SBrian Somers void 936b6217683SBrian Somers bundle_RegisterDescriptor(struct bundle *bundle, struct descriptor *d) 937b6217683SBrian Somers { 938b6217683SBrian Somers d->next = bundle->desc.next; 939b6217683SBrian Somers bundle->desc.next = d; 940b6217683SBrian Somers } 941b6217683SBrian Somers 942b6217683SBrian Somers void 943b6217683SBrian Somers bundle_UnRegisterDescriptor(struct bundle *bundle, struct descriptor *d) 944b6217683SBrian Somers { 945b6217683SBrian Somers struct descriptor **desc; 946b6217683SBrian Somers 947b6217683SBrian Somers for (desc = &bundle->desc.next; *desc; desc = &(*desc)->next) 948b6217683SBrian Somers if (*desc == d) { 949b6217683SBrian Somers *desc = d->next; 950b6217683SBrian Somers break; 951b6217683SBrian Somers } 952b6217683SBrian Somers } 953b6217683SBrian Somers 954b6217683SBrian Somers void 955b6217683SBrian Somers bundle_DelPromptDescriptors(struct bundle *bundle, struct server *s) 956b6217683SBrian Somers { 957b6217683SBrian Somers struct descriptor **desc; 958b6217683SBrian Somers struct prompt *p; 959b6217683SBrian Somers 960b6217683SBrian Somers desc = &bundle->desc.next; 961b6217683SBrian Somers while (*desc) { 962b6217683SBrian Somers if ((*desc)->type == PROMPT_DESCRIPTOR) { 963b6217683SBrian Somers p = (struct prompt *)*desc; 964b6217683SBrian Somers if (p->owner == s) { 965b6217683SBrian Somers prompt_Destroy(p, 1); 966b6217683SBrian Somers desc = &bundle->desc.next; 967b6217683SBrian Somers continue; 968b6217683SBrian Somers } 969b6217683SBrian Somers } 970b6217683SBrian Somers desc = &(*desc)->next; 971b6217683SBrian Somers } 972b6217683SBrian Somers } 973b6217683SBrian Somers 974b6217683SBrian Somers void 975b6217683SBrian Somers bundle_DisplayPrompt(struct bundle *bundle) 976b6217683SBrian Somers { 977b6217683SBrian Somers struct descriptor **desc; 978b6217683SBrian Somers 979b6217683SBrian Somers for (desc = &bundle->desc.next; *desc; desc = &(*desc)->next) 980b6217683SBrian Somers if ((*desc)->type == PROMPT_DESCRIPTOR) 981b6217683SBrian Somers prompt_Required((struct prompt *)*desc); 982b6217683SBrian Somers } 983b6217683SBrian Somers 984b6217683SBrian Somers void 985b6217683SBrian Somers bundle_WriteTermPrompt(struct bundle *bundle, struct datalink *dl, 986b6217683SBrian Somers const char *data, int len) 987b6217683SBrian Somers { 988b6217683SBrian Somers struct descriptor *desc; 989b6217683SBrian Somers struct prompt *p; 990b6217683SBrian Somers 991b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 992b6217683SBrian Somers if (desc->type == PROMPT_DESCRIPTOR) { 993b6217683SBrian Somers p = (struct prompt *)desc; 994b6217683SBrian Somers if (prompt_IsTermMode(p, dl)) 995c3a119d0SBrian Somers prompt_Printf(p, "%.*s", len, data); 996b6217683SBrian Somers } 997b6217683SBrian Somers } 998b6217683SBrian Somers 999b6217683SBrian Somers void 1000b6217683SBrian Somers bundle_SetTtyCommandMode(struct bundle *bundle, struct datalink *dl) 1001b6217683SBrian Somers { 1002b6217683SBrian Somers struct descriptor *desc; 1003b6217683SBrian Somers struct prompt *p; 1004b6217683SBrian Somers 1005b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 1006b6217683SBrian Somers if (desc->type == PROMPT_DESCRIPTOR) { 1007b6217683SBrian Somers p = (struct prompt *)desc; 1008b6217683SBrian Somers if (prompt_IsTermMode(p, dl)) 1009b6217683SBrian Somers prompt_TtyCommandMode(p); 1010b6217683SBrian Somers } 1011b6217683SBrian Somers } 1012565e35e5SBrian Somers 1013565e35e5SBrian Somers static void 1014565e35e5SBrian Somers bundle_GenPhysType(struct bundle *bundle) 1015565e35e5SBrian Somers { 1016565e35e5SBrian Somers struct datalink *dl; 1017565e35e5SBrian Somers 1018565e35e5SBrian Somers bundle->phys_type = 0; 1019565e35e5SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 1020565e35e5SBrian Somers bundle->phys_type |= dl->physical->type; 1021565e35e5SBrian Somers } 1022565e35e5SBrian Somers 1023cd7bd93aSBrian Somers void 1024cd7bd93aSBrian Somers bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl, 1025cd7bd93aSBrian Somers const char *name) 1026cd7bd93aSBrian Somers { 1027cd7bd93aSBrian Somers struct datalink *ndl = datalink_Clone(dl, name); 1028cd7bd93aSBrian Somers 1029cd7bd93aSBrian Somers ndl->next = dl->next; 1030cd7bd93aSBrian Somers dl->next = ndl; 1031565e35e5SBrian Somers bundle_GenPhysType(bundle); 1032cd7bd93aSBrian Somers } 1033cd7bd93aSBrian Somers 1034cd7bd93aSBrian Somers void 1035cd7bd93aSBrian Somers bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl) 1036cd7bd93aSBrian Somers { 1037cd7bd93aSBrian Somers struct datalink **dlp; 1038cd7bd93aSBrian Somers 1039cd7bd93aSBrian Somers if (dl->state == DATALINK_CLOSED) 1040cd7bd93aSBrian Somers for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next) 1041cd7bd93aSBrian Somers if (*dlp == dl) { 1042cd7bd93aSBrian Somers *dlp = datalink_Destroy(dl); 1043cd7bd93aSBrian Somers break; 1044cd7bd93aSBrian Somers } 1045565e35e5SBrian Somers bundle_GenPhysType(bundle); 1046565e35e5SBrian Somers } 1047565e35e5SBrian Somers 1048565e35e5SBrian Somers void 1049565e35e5SBrian Somers bundle_CleanDatalinks(struct bundle *bundle) 1050565e35e5SBrian Somers { 1051565e35e5SBrian Somers struct datalink **dlp = &bundle->links; 1052565e35e5SBrian Somers 1053565e35e5SBrian Somers while (*dlp) 1054565e35e5SBrian Somers if ((*dlp)->state == DATALINK_CLOSED && 1055565e35e5SBrian Somers (*dlp)->physical->type & (PHYS_STDIN|PHYS_1OFF)) 1056565e35e5SBrian Somers *dlp = datalink_Destroy(*dlp); 1057565e35e5SBrian Somers else 1058565e35e5SBrian Somers dlp = &(*dlp)->next; 1059565e35e5SBrian Somers bundle_GenPhysType(bundle); 1060cd7bd93aSBrian Somers } 1061