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 * 26673903ecSBrian Somers * $Id: bundle.c,v 1.1.2.58 1998/04/23 18:56:07 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); 118673903ecSBrian Somers bundle->phase = new; 119673903ecSBrian Somers bundle_DisplayPrompt(bundle); 120673903ecSBrian Somers break; 121455aabc3SBrian Somers 122455aabc3SBrian Somers case PHASE_TERMINATE: 123455aabc3SBrian Somers bundle->phase = new; 124673903ecSBrian Somers mp_Down(&bundle->ncp.mp); 125b6217683SBrian Somers bundle_DisplayPrompt(bundle); 126455aabc3SBrian Somers break; 1277a6f8720SBrian Somers } 1287a6f8720SBrian Somers } 1297a6f8720SBrian Somers 1307a6f8720SBrian Somers static int 1317a6f8720SBrian Somers bundle_CleanInterface(const struct bundle *bundle) 1327a6f8720SBrian Somers { 1337a6f8720SBrian Somers int s; 1347a6f8720SBrian Somers struct ifreq ifrq; 1357a6f8720SBrian Somers struct ifaliasreq ifra; 1367a6f8720SBrian Somers 1377a6f8720SBrian Somers s = ID0socket(AF_INET, SOCK_DGRAM, 0); 1387a6f8720SBrian Somers if (s < 0) { 1397a6f8720SBrian Somers LogPrintf(LogERROR, "bundle_CleanInterface: socket(): %s\n", 1407a6f8720SBrian Somers strerror(errno)); 1417a6f8720SBrian Somers return (-1); 1427a6f8720SBrian Somers } 1437a6f8720SBrian Somers strncpy(ifrq.ifr_name, bundle->ifname, sizeof ifrq.ifr_name - 1); 1447a6f8720SBrian Somers ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 1457a6f8720SBrian Somers while (ID0ioctl(s, SIOCGIFADDR, &ifrq) == 0) { 1467a6f8720SBrian Somers memset(&ifra.ifra_mask, '\0', sizeof ifra.ifra_mask); 1477a6f8720SBrian Somers strncpy(ifra.ifra_name, bundle->ifname, sizeof ifra.ifra_name - 1); 1487a6f8720SBrian Somers ifra.ifra_name[sizeof ifra.ifra_name - 1] = '\0'; 1497a6f8720SBrian Somers ifra.ifra_addr = ifrq.ifr_addr; 1507a6f8720SBrian Somers if (ID0ioctl(s, SIOCGIFDSTADDR, &ifrq) < 0) { 1517a6f8720SBrian Somers if (ifra.ifra_addr.sa_family == AF_INET) 1527a6f8720SBrian Somers LogPrintf(LogERROR, 1537a6f8720SBrian Somers "bundle_CleanInterface: Can't get dst for %s on %s !\n", 1547a6f8720SBrian Somers inet_ntoa(((struct sockaddr_in *)&ifra.ifra_addr)->sin_addr), 1557a6f8720SBrian Somers bundle->ifname); 1567a6f8720SBrian Somers return 0; 1577a6f8720SBrian Somers } 1587a6f8720SBrian Somers ifra.ifra_broadaddr = ifrq.ifr_dstaddr; 1597a6f8720SBrian Somers if (ID0ioctl(s, SIOCDIFADDR, &ifra) < 0) { 1607a6f8720SBrian Somers if (ifra.ifra_addr.sa_family == AF_INET) 1617a6f8720SBrian Somers LogPrintf(LogERROR, 1627a6f8720SBrian Somers "bundle_CleanInterface: Can't delete %s address on %s !\n", 1637a6f8720SBrian Somers inet_ntoa(((struct sockaddr_in *)&ifra.ifra_addr)->sin_addr), 1647a6f8720SBrian Somers bundle->ifname); 1657a6f8720SBrian Somers return 0; 1667a6f8720SBrian Somers } 1677a6f8720SBrian Somers } 1687a6f8720SBrian Somers 1697a6f8720SBrian Somers return 1; 1707a6f8720SBrian Somers } 1717a6f8720SBrian Somers 1726d666775SBrian Somers static void 1736d666775SBrian Somers bundle_LayerStart(void *v, struct fsm *fp) 1747a6f8720SBrian Somers { 1753006ec67SBrian Somers /* The given FSM is about to start up ! */ 1767a6f8720SBrian Somers } 1777a6f8720SBrian Somers 1785cf4388bSBrian Somers 1795cf4388bSBrian Somers static void 1805cf4388bSBrian Somers bundle_Notify(struct bundle *bundle, char c) 1815cf4388bSBrian Somers { 1825cf4388bSBrian Somers if (bundle->notify.fd != -1) { 1835cf4388bSBrian Somers if (write(bundle->notify.fd, &c, 1) == 1) 1845cf4388bSBrian Somers LogPrintf(LogPHASE, "Parent notified of success.\n"); 1855cf4388bSBrian Somers else 1865cf4388bSBrian Somers LogPrintf(LogPHASE, "Failed to notify parent of success.\n"); 1875cf4388bSBrian Somers close(bundle->notify.fd); 1885cf4388bSBrian Somers bundle->notify.fd = -1; 1895cf4388bSBrian Somers } 1905cf4388bSBrian Somers } 1913b0f8d2eSBrian Somers 1926d666775SBrian Somers static void 1933b0f8d2eSBrian Somers bundle_vLayerUp(void *v, struct fsm *fp) 1943b0f8d2eSBrian Somers { 1953b0f8d2eSBrian Somers bundle_LayerUp((struct bundle *)v, fp); 1963b0f8d2eSBrian Somers } 1973b0f8d2eSBrian Somers 1983b0f8d2eSBrian Somers void 1993b0f8d2eSBrian Somers bundle_LayerUp(struct bundle *bundle, struct fsm *fp) 2007a6f8720SBrian Somers { 2013006ec67SBrian Somers /* 2023006ec67SBrian Somers * The given fsm is now up 20349052c95SBrian Somers * If it's an LCP set our mtu (if we're multilink, add up the link 20449052c95SBrian Somers * speeds and set the MRRU). 205565e35e5SBrian Somers * If it's an NCP, tell our -background parent to go away. 2063b0f8d2eSBrian Somers * If it's the first NCP, start the idle timer. 2073006ec67SBrian Somers */ 2086d666775SBrian Somers 2095563ebdeSBrian Somers if (fp->proto == PROTO_LCP) { 2103b0f8d2eSBrian Somers if (bundle->ncp.mp.active) { 2113b0f8d2eSBrian Somers int speed; 2123b0f8d2eSBrian Somers struct datalink *dl; 2135563ebdeSBrian Somers 2143b0f8d2eSBrian Somers for (dl = bundle->links, speed = 0; dl; dl = dl->next) 2153b0f8d2eSBrian Somers speed += modem_Speed(dl->physical); 2163b0f8d2eSBrian Somers if (speed) 21749052c95SBrian Somers tun_configure(bundle, bundle->ncp.mp.peer_mrru, speed); 2183b0f8d2eSBrian Somers } else 2193b0f8d2eSBrian Somers tun_configure(bundle, fsm2lcp(fp)->his_mru, 2203b0f8d2eSBrian Somers modem_Speed(link2physical(fp->link))); 2213b0f8d2eSBrian Somers } else if (fp->proto == PROTO_IPCP) { 222ab886ad0SBrian Somers bundle_StartIdleTimer(bundle); 2235cf4388bSBrian Somers bundle_Notify(bundle, EX_NORMAL); 2247a6f8720SBrian Somers } 225ab886ad0SBrian Somers } 2267a6f8720SBrian Somers 2276d666775SBrian Somers static void 2286d666775SBrian Somers bundle_LayerDown(void *v, struct fsm *fp) 2296d666775SBrian Somers { 2306d666775SBrian Somers /* 2316d666775SBrian Somers * The given FSM has been told to come down. 232ab886ad0SBrian Somers * If it's our last NCP, stop the idle timer. 2333b0f8d2eSBrian Somers * If it's our last NCP *OR* LCP, enter TERMINATE phase. 2343b0f8d2eSBrian Somers * If it's an LCP and we're in multilink mode, adjust our tun speed. 2356d666775SBrian Somers */ 236ab886ad0SBrian Somers 237ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v; 238ab886ad0SBrian Somers 2393b0f8d2eSBrian Somers if (fp->proto == PROTO_IPCP) { 240ab886ad0SBrian Somers bundle_StopIdleTimer(bundle); 2413b0f8d2eSBrian Somers } else if (fp->proto == PROTO_LCP) { 2423b0f8d2eSBrian Somers int speed, others_active; 2433b0f8d2eSBrian Somers struct datalink *dl; 2443b0f8d2eSBrian Somers 2453b0f8d2eSBrian Somers others_active = 0; 2463b0f8d2eSBrian Somers for (dl = bundle->links, speed = 0; dl; dl = dl->next) 2473b0f8d2eSBrian Somers if (fp != &dl->physical->link.lcp.fsm && 2483b0f8d2eSBrian Somers dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) { 2493b0f8d2eSBrian Somers speed += modem_Speed(dl->physical); 2503b0f8d2eSBrian Somers others_active++; 2513b0f8d2eSBrian Somers } 2523b0f8d2eSBrian Somers if (bundle->ncp.mp.active && speed) 2533b0f8d2eSBrian Somers tun_configure(bundle, bundle->ncp.mp.link.lcp.his_mru, speed); 2543b0f8d2eSBrian Somers 2553b0f8d2eSBrian Somers if (!others_active) 2563b0f8d2eSBrian Somers bundle_NewPhase(bundle, PHASE_TERMINATE); 2573b0f8d2eSBrian Somers } 2586d666775SBrian Somers } 2596d666775SBrian Somers 2606d666775SBrian Somers static void 2616d666775SBrian Somers bundle_LayerFinish(void *v, struct fsm *fp) 2626d666775SBrian Somers { 2636d666775SBrian Somers /* The given fsm is now down (fp cannot be NULL) 2646d666775SBrian Somers * 2656d666775SBrian Somers * If it's the last LCP, FsmDown all NCPs 2663b0f8d2eSBrian Somers * If it's the last NCP, FsmClose all LCPs 2676d666775SBrian Somers */ 2686d666775SBrian Somers 2696d666775SBrian Somers struct bundle *bundle = (struct bundle *)v; 2706d666775SBrian Somers struct datalink *dl; 2716d666775SBrian Somers 2723b0f8d2eSBrian Somers if (fp->proto == PROTO_IPCP) { 27326afeaa2SBrian Somers if (bundle_Phase(bundle) != PHASE_DEAD) 27425092092SBrian Somers bundle_NewPhase(bundle, PHASE_TERMINATE); 2756d666775SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 2763b0f8d2eSBrian Somers datalink_Close(dl, 0); 2773b0f8d2eSBrian Somers FsmDown(fp); 2783b0f8d2eSBrian Somers FsmClose(fp); 2793b0f8d2eSBrian Somers } else if (fp->proto == PROTO_LCP) { 2803b0f8d2eSBrian Somers int others_active; 281a611cad6SBrian Somers 2823b0f8d2eSBrian Somers others_active = 0; 2833b0f8d2eSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 2843b0f8d2eSBrian Somers if (fp != &dl->physical->link.lcp.fsm && 2853b0f8d2eSBrian Somers dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 2863b0f8d2eSBrian Somers others_active++; 2873b0f8d2eSBrian Somers 2883b0f8d2eSBrian Somers if (!others_active) { 2895828db6dSBrian Somers FsmDown(&bundle->ncp.ipcp.fsm); 2905828db6dSBrian Somers FsmClose(&bundle->ncp.ipcp.fsm); /* ST_INITIAL please */ 2916d666775SBrian Somers } 2923b0f8d2eSBrian Somers } 2933b0f8d2eSBrian Somers } 2946d666775SBrian Somers 2957a6f8720SBrian Somers int 2967a6f8720SBrian Somers bundle_LinkIsUp(const struct bundle *bundle) 2977a6f8720SBrian Somers { 2985828db6dSBrian Somers return bundle->ncp.ipcp.fsm.state == ST_OPENED; 2997a6f8720SBrian Somers } 3007a6f8720SBrian Somers 3017a6f8720SBrian Somers void 3023006ec67SBrian Somers bundle_Close(struct bundle *bundle, const char *name, int staydown) 3037a6f8720SBrian Somers { 304455aabc3SBrian Somers /* 3053006ec67SBrian Somers * Please close the given datalink. 3063b0f8d2eSBrian Somers * If name == NULL or name is the last datalink, enter TERMINATE phase 3073b0f8d2eSBrian Somers * and FsmClose all NCPs (except our MP) 3083b0f8d2eSBrian Somers * If it isn't the last datalink, just Close that datalink. 309455aabc3SBrian Somers */ 3107a6f8720SBrian Somers 3113b0f8d2eSBrian Somers struct datalink *dl, *this_dl; 3123b0f8d2eSBrian Somers int others_active; 3133006ec67SBrian Somers 3143b0f8d2eSBrian Somers if (bundle->phase == PHASE_TERMINATE || bundle->phase == PHASE_DEAD) 3153b0f8d2eSBrian Somers return; 3163b0f8d2eSBrian Somers 3173b0f8d2eSBrian Somers others_active = 0; 3183b0f8d2eSBrian Somers this_dl = NULL; 3193b0f8d2eSBrian Somers 3203b0f8d2eSBrian Somers for (dl = bundle->links; dl; dl = dl->next) { 3213b0f8d2eSBrian Somers if (name && !strcasecmp(name, dl->name)) 3223b0f8d2eSBrian Somers this_dl = dl; 3233b0f8d2eSBrian Somers if (name == NULL || this_dl == dl) { 324d345321bSBrian Somers if (staydown) 3253006ec67SBrian Somers datalink_StayDown(dl); 3263b0f8d2eSBrian Somers } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 3273b0f8d2eSBrian Somers others_active++; 3283b0f8d2eSBrian Somers } 3293b0f8d2eSBrian Somers 3303b0f8d2eSBrian Somers if (name && this_dl == NULL) { 3313b0f8d2eSBrian Somers LogPrintf(LogWARN, "%s: Invalid datalink name\n", name); 3323b0f8d2eSBrian Somers return; 3333b0f8d2eSBrian Somers } 3343b0f8d2eSBrian Somers 3353b0f8d2eSBrian Somers if (!others_active) { 3363b0f8d2eSBrian Somers if (bundle->ncp.ipcp.fsm.state > ST_CLOSED || 3373b0f8d2eSBrian Somers bundle->ncp.ipcp.fsm.state == ST_STARTING) 3383b0f8d2eSBrian Somers FsmClose(&bundle->ncp.ipcp.fsm); 3393b0f8d2eSBrian Somers else { 3405828db6dSBrian Somers if (bundle->ncp.ipcp.fsm.state > ST_INITIAL) { 3415828db6dSBrian Somers FsmClose(&bundle->ncp.ipcp.fsm); 3425828db6dSBrian Somers FsmDown(&bundle->ncp.ipcp.fsm); 343d2fd8d77SBrian Somers } 344d345321bSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 345d345321bSBrian Somers datalink_Close(dl, staydown); 3467a6f8720SBrian Somers } 3473b0f8d2eSBrian Somers } else if (this_dl && this_dl->state != DATALINK_CLOSED && 3483b0f8d2eSBrian Somers this_dl->state != DATALINK_HANGUP) 3493b0f8d2eSBrian Somers datalink_Close(this_dl, staydown); 350d2fd8d77SBrian Somers } 3517a6f8720SBrian Somers 3522f786681SBrian Somers static int 3532f786681SBrian Somers bundle_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n) 3542f786681SBrian Somers { 3552f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d); 3562f786681SBrian Somers struct datalink *dl; 357b6217683SBrian Somers struct descriptor *desc; 3582f786681SBrian Somers int result; 3592f786681SBrian Somers 3602f786681SBrian Somers result = 0; 3612f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 3622f786681SBrian Somers result += descriptor_UpdateSet(&dl->desc, r, w, e, n); 3632f786681SBrian Somers 364b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 365b6217683SBrian Somers result += descriptor_UpdateSet(desc, r, w, e, n); 366b6217683SBrian Somers 3672f786681SBrian Somers return result; 3682f786681SBrian Somers } 3692f786681SBrian Somers 3702f786681SBrian Somers static int 3712f786681SBrian Somers bundle_IsSet(struct descriptor *d, const fd_set *fdset) 3722f786681SBrian Somers { 3732f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d); 3742f786681SBrian Somers struct datalink *dl; 375b6217683SBrian Somers struct descriptor *desc; 3762f786681SBrian Somers 3772f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 3782f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 3792f786681SBrian Somers return 1; 3802f786681SBrian Somers 381b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 382b6217683SBrian Somers if (descriptor_IsSet(desc, fdset)) 383b6217683SBrian Somers return 1; 384b6217683SBrian Somers 3852f786681SBrian Somers return 0; 3862f786681SBrian Somers } 3872f786681SBrian Somers 3882f786681SBrian Somers static void 3892f786681SBrian Somers bundle_DescriptorRead(struct descriptor *d, struct bundle *bundle, 3902f786681SBrian Somers const fd_set *fdset) 3912f786681SBrian Somers { 3922f786681SBrian Somers struct datalink *dl; 393b6217683SBrian Somers struct descriptor *desc; 3942f786681SBrian Somers 3952f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 3962f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 3972f786681SBrian Somers descriptor_Read(&dl->desc, bundle, fdset); 398b6217683SBrian Somers 399b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 400b6217683SBrian Somers if (descriptor_IsSet(desc, fdset)) 401b6217683SBrian Somers descriptor_Read(desc, bundle, fdset); 4022f786681SBrian Somers } 4032f786681SBrian Somers 4042f786681SBrian Somers static void 4052f786681SBrian Somers bundle_DescriptorWrite(struct descriptor *d, struct bundle *bundle, 4062f786681SBrian Somers const fd_set *fdset) 4072f786681SBrian Somers { 4082f786681SBrian Somers struct datalink *dl; 409b6217683SBrian Somers struct descriptor *desc; 4102f786681SBrian Somers 4112f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 4122f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 4132f786681SBrian Somers descriptor_Write(&dl->desc, bundle, fdset); 414b6217683SBrian Somers 415b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 416b6217683SBrian Somers if (descriptor_IsSet(desc, fdset)) 417b6217683SBrian Somers descriptor_Write(desc, bundle, fdset); 4182f786681SBrian Somers } 4192f786681SBrian Somers 4207a6f8720SBrian Somers 4217a6f8720SBrian Somers #define MAX_TUN 256 4227a6f8720SBrian Somers /* 4237a6f8720SBrian Somers * MAX_TUN is set at 256 because that is the largest minor number 4243b0f8d2eSBrian Somers * we can use (certainly with mknod(1) anyway). The search for a 4257a6f8720SBrian Somers * device aborts when it reaches the first `Device not configured' 4267a6f8720SBrian Somers * (ENXIO) or the third `No such file or directory' (ENOENT) error. 4277a6f8720SBrian Somers */ 4287a6f8720SBrian Somers struct bundle * 429565e35e5SBrian Somers bundle_Create(const char *prefix, struct prompt *prompt, int type) 4307a6f8720SBrian Somers { 4317a6f8720SBrian Somers int s, enoentcount, err; 4327a6f8720SBrian Somers struct ifreq ifrq; 4337a6f8720SBrian Somers static struct bundle bundle; /* there can be only one */ 4347a6f8720SBrian Somers 4357a6f8720SBrian Somers if (bundle.ifname != NULL) { /* Already allocated ! */ 4367a6f8720SBrian Somers LogPrintf(LogERROR, "bundle_Create: There's only one BUNDLE !\n"); 4377a6f8720SBrian Somers return NULL; 4387a6f8720SBrian Somers } 4397a6f8720SBrian Somers 4407a6f8720SBrian Somers err = ENOENT; 4417a6f8720SBrian Somers enoentcount = 0; 4427a6f8720SBrian Somers for (bundle.unit = 0; bundle.unit <= MAX_TUN; bundle.unit++) { 4437a6f8720SBrian Somers snprintf(bundle.dev, sizeof bundle.dev, "%s%d", prefix, bundle.unit); 4447a6f8720SBrian Somers bundle.tun_fd = ID0open(bundle.dev, O_RDWR); 4457a6f8720SBrian Somers if (bundle.tun_fd >= 0) 4467a6f8720SBrian Somers break; 4477a6f8720SBrian Somers if (errno == ENXIO) { 4487a6f8720SBrian Somers bundle.unit = MAX_TUN; 4497a6f8720SBrian Somers err = errno; 4507a6f8720SBrian Somers } else if (errno == ENOENT) { 4517a6f8720SBrian Somers if (++enoentcount > 2) 4527a6f8720SBrian Somers bundle.unit = MAX_TUN; 4537a6f8720SBrian Somers } else 4547a6f8720SBrian Somers err = errno; 4557a6f8720SBrian Somers } 4567a6f8720SBrian Somers 4577a6f8720SBrian Somers if (bundle.unit > MAX_TUN) { 458b6217683SBrian Somers prompt_Printf(prompt, "No tunnel device is available (%s).\n", 45985b542cfSBrian Somers strerror(err)); 4607a6f8720SBrian Somers return NULL; 4617a6f8720SBrian Somers } 4627a6f8720SBrian Somers 4637a6f8720SBrian Somers LogSetTun(bundle.unit); 4647a6f8720SBrian Somers 4657a6f8720SBrian Somers s = socket(AF_INET, SOCK_DGRAM, 0); 4667a6f8720SBrian Somers if (s < 0) { 4677a6f8720SBrian Somers LogPrintf(LogERROR, "bundle_Create: socket(): %s\n", strerror(errno)); 4687a6f8720SBrian Somers close(bundle.tun_fd); 4697a6f8720SBrian Somers return NULL; 4707a6f8720SBrian Somers } 4717a6f8720SBrian Somers 4727a6f8720SBrian Somers bundle.ifname = strrchr(bundle.dev, '/'); 4737a6f8720SBrian Somers if (bundle.ifname == NULL) 4747a6f8720SBrian Somers bundle.ifname = bundle.dev; 4757a6f8720SBrian Somers else 4767a6f8720SBrian Somers bundle.ifname++; 4777a6f8720SBrian Somers 4787a6f8720SBrian Somers /* 4797a6f8720SBrian Somers * Now, bring up the interface. 4807a6f8720SBrian Somers */ 4817a6f8720SBrian Somers memset(&ifrq, '\0', sizeof ifrq); 4827a6f8720SBrian Somers strncpy(ifrq.ifr_name, bundle.ifname, sizeof ifrq.ifr_name - 1); 4837a6f8720SBrian Somers ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 4847a6f8720SBrian Somers if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) { 4857a6f8720SBrian Somers LogPrintf(LogERROR, "OpenTunnel: ioctl(SIOCGIFFLAGS): %s\n", 4867a6f8720SBrian Somers strerror(errno)); 4877a6f8720SBrian Somers close(s); 4887a6f8720SBrian Somers close(bundle.tun_fd); 4897a6f8720SBrian Somers bundle.ifname = NULL; 4907a6f8720SBrian Somers return NULL; 4917a6f8720SBrian Somers } 4927a6f8720SBrian Somers ifrq.ifr_flags |= IFF_UP; 4937a6f8720SBrian Somers if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) { 4947a6f8720SBrian Somers LogPrintf(LogERROR, "OpenTunnel: ioctl(SIOCSIFFLAGS): %s\n", 4957a6f8720SBrian Somers strerror(errno)); 4967a6f8720SBrian Somers close(s); 4977a6f8720SBrian Somers close(bundle.tun_fd); 4987a6f8720SBrian Somers bundle.ifname = NULL; 4997a6f8720SBrian Somers return NULL; 5007a6f8720SBrian Somers } 5017a6f8720SBrian Somers 5027a6f8720SBrian Somers close(s); 5037a6f8720SBrian Somers 5047a6f8720SBrian Somers if ((bundle.ifIndex = GetIfIndex(bundle.ifname)) < 0) { 5057a6f8720SBrian Somers LogPrintf(LogERROR, "OpenTunnel: Can't find ifindex.\n"); 5067a6f8720SBrian Somers close(bundle.tun_fd); 5077a6f8720SBrian Somers bundle.ifname = NULL; 5087a6f8720SBrian Somers return NULL; 5097a6f8720SBrian Somers } 5107a6f8720SBrian Somers 511b6217683SBrian Somers prompt_Printf(prompt, "Using interface: %s\n", bundle.ifname); 5127a6f8720SBrian Somers LogPrintf(LogPHASE, "Using interface: %s\n", bundle.ifname); 5137a6f8720SBrian Somers 514820de6ebSBrian Somers bundle.routing_seq = 0; 515a0cbd833SBrian Somers bundle.phase = PHASE_DEAD; 516a0cbd833SBrian Somers bundle.CleaningUp = 0; 5177a6f8720SBrian Somers 5186d666775SBrian Somers bundle.fsm.LayerStart = bundle_LayerStart; 5193b0f8d2eSBrian Somers bundle.fsm.LayerUp = bundle_vLayerUp; 5206d666775SBrian Somers bundle.fsm.LayerDown = bundle_LayerDown; 5216d666775SBrian Somers bundle.fsm.LayerFinish = bundle_LayerFinish; 5226d666775SBrian Somers bundle.fsm.object = &bundle; 5237a6f8720SBrian Somers 524ab886ad0SBrian Somers bundle.cfg.idle_timeout = NCP_IDLE_TIMEOUT; 5251342caedSBrian Somers *bundle.cfg.auth.name = '\0'; 5261342caedSBrian Somers *bundle.cfg.auth.key = '\0'; 5271342caedSBrian Somers bundle.cfg.opt = OPT_IDCHECK | OPT_LOOPBACK | OPT_THROUGHPUT | OPT_UTMP; 52849052c95SBrian Somers *bundle.cfg.label = '\0'; 52949052c95SBrian Somers bundle.cfg.mtu = DEF_MTU; 530565e35e5SBrian Somers bundle.phys_type = type; 531ab886ad0SBrian Somers 532565e35e5SBrian Somers bundle.links = datalink_Create("default", &bundle, &bundle.fsm, type); 5333006ec67SBrian Somers if (bundle.links == NULL) { 5343006ec67SBrian Somers LogPrintf(LogERROR, "Cannot create data link: %s\n", strerror(errno)); 5356d666775SBrian Somers close(bundle.tun_fd); 5366d666775SBrian Somers bundle.ifname = NULL; 5372289f246SBrian Somers return NULL; 5382289f246SBrian Somers } 5392289f246SBrian Somers 5402f786681SBrian Somers bundle.desc.type = BUNDLE_DESCRIPTOR; 5412f786681SBrian Somers bundle.desc.next = NULL; 5422f786681SBrian Somers bundle.desc.UpdateSet = bundle_UpdateSet; 5432f786681SBrian Somers bundle.desc.IsSet = bundle_IsSet; 5442f786681SBrian Somers bundle.desc.Read = bundle_DescriptorRead; 5452f786681SBrian Somers bundle.desc.Write = bundle_DescriptorWrite; 5462f786681SBrian Somers 54749052c95SBrian Somers mp_Init(&bundle.ncp.mp, &bundle); 54849052c95SBrian Somers 54949052c95SBrian Somers /* Send over the first physical link by default */ 5505828db6dSBrian Somers ipcp_Init(&bundle.ncp.ipcp, &bundle, &bundle.links->physical->link, 5515828db6dSBrian Somers &bundle.fsm); 5526d666775SBrian Somers 5535ca5389aSBrian Somers memset(&bundle.filter, '\0', sizeof bundle.filter); 5545ca5389aSBrian Somers bundle.filter.in.fragok = bundle.filter.in.logok = 1; 5555ca5389aSBrian Somers bundle.filter.in.name = "IN"; 5565ca5389aSBrian Somers bundle.filter.out.fragok = bundle.filter.out.logok = 1; 5575ca5389aSBrian Somers bundle.filter.out.name = "OUT"; 5585ca5389aSBrian Somers bundle.filter.dial.name = "DIAL"; 5598390b576SBrian Somers bundle.filter.dial.logok = 1; 5605ca5389aSBrian Somers bundle.filter.alive.name = "ALIVE"; 5615ca5389aSBrian Somers bundle.filter.alive.logok = 1; 56293ee0ff2SBrian Somers memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer); 56393ee0ff2SBrian Somers bundle.idle.done = 0; 5645cf4388bSBrian Somers bundle.notify.fd = -1; 56593ee0ff2SBrian Somers 5666d666775SBrian Somers /* Clean out any leftover crud */ 5676d666775SBrian Somers bundle_CleanInterface(&bundle); 5686d666775SBrian Somers 56985602e52SBrian Somers if (prompt) { 57085602e52SBrian Somers /* Retrospectively introduce ourselves to the prompt */ 57185602e52SBrian Somers prompt->bundle = &bundle; 57285602e52SBrian Somers bundle_RegisterDescriptor(&bundle, &prompt->desc); 57385602e52SBrian Somers } 57485602e52SBrian Somers 5757a6f8720SBrian Somers return &bundle; 5767a6f8720SBrian Somers } 5777a6f8720SBrian Somers 57868a0f0ccSBrian Somers static void 57968a0f0ccSBrian Somers bundle_DownInterface(struct bundle *bundle) 58068a0f0ccSBrian Somers { 58168a0f0ccSBrian Somers struct ifreq ifrq; 58268a0f0ccSBrian Somers int s; 58368a0f0ccSBrian Somers 58468a0f0ccSBrian Somers DeleteIfRoutes(bundle, 1); 58568a0f0ccSBrian Somers 58668a0f0ccSBrian Somers s = ID0socket(AF_INET, SOCK_DGRAM, 0); 58768a0f0ccSBrian Somers if (s < 0) { 58868a0f0ccSBrian Somers LogPrintf(LogERROR, "bundle_DownInterface: socket: %s\n", strerror(errno)); 58968a0f0ccSBrian Somers return; 59068a0f0ccSBrian Somers } 59168a0f0ccSBrian Somers 59268a0f0ccSBrian Somers memset(&ifrq, '\0', sizeof ifrq); 59368a0f0ccSBrian Somers strncpy(ifrq.ifr_name, bundle->ifname, sizeof ifrq.ifr_name - 1); 59468a0f0ccSBrian Somers ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 59568a0f0ccSBrian Somers if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) { 59668a0f0ccSBrian Somers LogPrintf(LogERROR, "bundle_DownInterface: ioctl(SIOCGIFFLAGS): %s\n", 59768a0f0ccSBrian Somers strerror(errno)); 59868a0f0ccSBrian Somers close(s); 59968a0f0ccSBrian Somers return; 60068a0f0ccSBrian Somers } 60168a0f0ccSBrian Somers ifrq.ifr_flags &= ~IFF_UP; 60268a0f0ccSBrian Somers if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) { 60368a0f0ccSBrian Somers LogPrintf(LogERROR, "bundle_DownInterface: ioctl(SIOCSIFFLAGS): %s\n", 60468a0f0ccSBrian Somers strerror(errno)); 60568a0f0ccSBrian Somers close(s); 60668a0f0ccSBrian Somers return; 60768a0f0ccSBrian Somers } 60868a0f0ccSBrian Somers close(s); 60968a0f0ccSBrian Somers } 61068a0f0ccSBrian Somers 61168a0f0ccSBrian Somers void 61268a0f0ccSBrian Somers bundle_Destroy(struct bundle *bundle) 61368a0f0ccSBrian Somers { 6143006ec67SBrian Somers struct datalink *dl; 615b6217683SBrian Somers struct descriptor *desc, *ndesc; 616b6217683SBrian Somers 617565e35e5SBrian Somers if (bundle->phys_type & PHYS_DEMAND) { 618565e35e5SBrian Somers IpcpCleanInterface(&bundle->ncp.ipcp); 61968a0f0ccSBrian Somers bundle_DownInterface(bundle); 62068a0f0ccSBrian Somers } 6213006ec67SBrian Somers 6223006ec67SBrian Somers dl = bundle->links; 6233006ec67SBrian Somers while (dl) 6243006ec67SBrian Somers dl = datalink_Destroy(dl); 6253006ec67SBrian Somers 6265cf4388bSBrian Somers bundle_Notify(bundle, EX_ERRDEAD); 627b6217683SBrian Somers 628b6217683SBrian Somers desc = bundle->desc.next; 629b6217683SBrian Somers while (desc) { 630b6217683SBrian Somers ndesc = desc->next; 631b6217683SBrian Somers if (desc->type == PROMPT_DESCRIPTOR) 632b6217683SBrian Somers prompt_Destroy((struct prompt *)desc, 1); 633b6217683SBrian Somers else 634b6217683SBrian Somers LogPrintf(LogERROR, "bundle_Destroy: Don't know how to delete descriptor" 635b6217683SBrian Somers " type %d\n", desc->type); 636b6217683SBrian Somers desc = ndesc; 637b6217683SBrian Somers } 638b6217683SBrian Somers bundle->desc.next = NULL; 63968a0f0ccSBrian Somers bundle->ifname = NULL; 64068a0f0ccSBrian Somers } 64168a0f0ccSBrian Somers 6427a6f8720SBrian Somers struct rtmsg { 6437a6f8720SBrian Somers struct rt_msghdr m_rtm; 6447a6f8720SBrian Somers char m_space[64]; 6457a6f8720SBrian Somers }; 6467a6f8720SBrian Somers 6477a6f8720SBrian Somers void 648820de6ebSBrian Somers bundle_SetRoute(struct bundle *bundle, int cmd, struct in_addr dst, 6497a6f8720SBrian Somers struct in_addr gateway, struct in_addr mask, int bang) 6507a6f8720SBrian Somers { 6517a6f8720SBrian Somers struct rtmsg rtmes; 6527a6f8720SBrian Somers int s, nb, wb; 6537a6f8720SBrian Somers char *cp; 6547a6f8720SBrian Somers const char *cmdstr; 6557a6f8720SBrian Somers struct sockaddr_in rtdata; 6567a6f8720SBrian Somers 6577a6f8720SBrian Somers if (bang) 6587a6f8720SBrian Somers cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!"); 6597a6f8720SBrian Somers else 6607a6f8720SBrian Somers cmdstr = (cmd == RTM_ADD ? "Add" : "Delete"); 6617a6f8720SBrian Somers s = ID0socket(PF_ROUTE, SOCK_RAW, 0); 6627a6f8720SBrian Somers if (s < 0) { 66368a0f0ccSBrian Somers LogPrintf(LogERROR, "bundle_SetRoute: socket(): %s\n", strerror(errno)); 6647a6f8720SBrian Somers return; 6657a6f8720SBrian Somers } 6667a6f8720SBrian Somers memset(&rtmes, '\0', sizeof rtmes); 6677a6f8720SBrian Somers rtmes.m_rtm.rtm_version = RTM_VERSION; 6687a6f8720SBrian Somers rtmes.m_rtm.rtm_type = cmd; 6697a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs = RTA_DST; 670820de6ebSBrian Somers rtmes.m_rtm.rtm_seq = ++bundle->routing_seq; 6717a6f8720SBrian Somers rtmes.m_rtm.rtm_pid = getpid(); 6727a6f8720SBrian Somers rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC; 6737a6f8720SBrian Somers 6747a6f8720SBrian Somers memset(&rtdata, '\0', sizeof rtdata); 67550e5c17dSBrian Somers rtdata.sin_len = sizeof rtdata; 6767a6f8720SBrian Somers rtdata.sin_family = AF_INET; 6777a6f8720SBrian Somers rtdata.sin_port = 0; 6787a6f8720SBrian Somers rtdata.sin_addr = dst; 6797a6f8720SBrian Somers 6807a6f8720SBrian Somers cp = rtmes.m_space; 68150e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 68250e5c17dSBrian Somers cp += rtdata.sin_len; 6837a6f8720SBrian Somers if (cmd == RTM_ADD) 6847a6f8720SBrian Somers if (gateway.s_addr == INADDR_ANY) { 6857a6f8720SBrian Somers /* Add a route through the interface */ 6867a6f8720SBrian Somers struct sockaddr_dl dl; 6877a6f8720SBrian Somers const char *iname; 6887a6f8720SBrian Somers int ilen; 6897a6f8720SBrian Somers 6907a6f8720SBrian Somers iname = Index2Nam(bundle->ifIndex); 6917a6f8720SBrian Somers ilen = strlen(iname); 6927a6f8720SBrian Somers dl.sdl_len = sizeof dl - sizeof dl.sdl_data + ilen; 6937a6f8720SBrian Somers dl.sdl_family = AF_LINK; 6947a6f8720SBrian Somers dl.sdl_index = bundle->ifIndex; 6957a6f8720SBrian Somers dl.sdl_type = 0; 6967a6f8720SBrian Somers dl.sdl_nlen = ilen; 6977a6f8720SBrian Somers dl.sdl_alen = 0; 6987a6f8720SBrian Somers dl.sdl_slen = 0; 6997a6f8720SBrian Somers strncpy(dl.sdl_data, iname, sizeof dl.sdl_data); 7007a6f8720SBrian Somers memcpy(cp, &dl, dl.sdl_len); 7017a6f8720SBrian Somers cp += dl.sdl_len; 7027a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 7037a6f8720SBrian Somers } else { 7047a6f8720SBrian Somers rtdata.sin_addr = gateway; 70550e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 70650e5c17dSBrian Somers cp += rtdata.sin_len; 7077a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 7087a6f8720SBrian Somers } 7097a6f8720SBrian Somers 7107a6f8720SBrian Somers if (dst.s_addr == INADDR_ANY) 7117a6f8720SBrian Somers mask.s_addr = INADDR_ANY; 7127a6f8720SBrian Somers 7137a6f8720SBrian Somers if (cmd == RTM_ADD || dst.s_addr == INADDR_ANY) { 7147a6f8720SBrian Somers rtdata.sin_addr = mask; 71550e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 71650e5c17dSBrian Somers cp += rtdata.sin_len; 7177a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_NETMASK; 7187a6f8720SBrian Somers } 7197a6f8720SBrian Somers 7207a6f8720SBrian Somers nb = cp - (char *) &rtmes; 7217a6f8720SBrian Somers rtmes.m_rtm.rtm_msglen = nb; 7227a6f8720SBrian Somers wb = ID0write(s, &rtmes, nb); 7237a6f8720SBrian Somers if (wb < 0) { 72468a0f0ccSBrian Somers LogPrintf(LogTCPIP, "bundle_SetRoute failure:\n"); 72550e5c17dSBrian Somers LogPrintf(LogTCPIP, "bundle_SetRoute: Cmd = %s\n", cmdstr); 72668a0f0ccSBrian Somers LogPrintf(LogTCPIP, "bundle_SetRoute: Dst = %s\n", inet_ntoa(dst)); 72768a0f0ccSBrian Somers LogPrintf(LogTCPIP, "bundle_SetRoute: Gateway = %s\n", inet_ntoa(gateway)); 72868a0f0ccSBrian Somers LogPrintf(LogTCPIP, "bundle_SetRoute: Mask = %s\n", inet_ntoa(mask)); 7297a6f8720SBrian Somers failed: 7307a6f8720SBrian Somers if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST || 7317a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) 7327a6f8720SBrian Somers if (!bang) 7337a6f8720SBrian Somers LogPrintf(LogWARN, "Add route failed: %s already exists\n", 7347a6f8720SBrian Somers inet_ntoa(dst)); 7357a6f8720SBrian Somers else { 7367a6f8720SBrian Somers rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE; 7377a6f8720SBrian Somers if ((wb = ID0write(s, &rtmes, nb)) < 0) 7387a6f8720SBrian Somers goto failed; 7397a6f8720SBrian Somers } 7407a6f8720SBrian Somers else if (cmd == RTM_DELETE && 7417a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == ESRCH || 7427a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) { 7437a6f8720SBrian Somers if (!bang) 7447a6f8720SBrian Somers LogPrintf(LogWARN, "Del route failed: %s: Non-existent\n", 7457a6f8720SBrian Somers inet_ntoa(dst)); 7467a6f8720SBrian Somers } else if (rtmes.m_rtm.rtm_errno == 0) 7477a6f8720SBrian Somers LogPrintf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr, 7487a6f8720SBrian Somers inet_ntoa(dst), strerror(errno)); 7497a6f8720SBrian Somers else 7507a6f8720SBrian Somers LogPrintf(LogWARN, "%s route failed: %s: %s\n", 7517a6f8720SBrian Somers cmdstr, inet_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno)); 7527a6f8720SBrian Somers } 7537a6f8720SBrian Somers LogPrintf(LogDEBUG, "wrote %d: cmd = %s, dst = %x, gateway = %x\n", 754fe3125a0SBrian Somers wb, cmdstr, (unsigned)dst.s_addr, (unsigned)gateway.s_addr); 7557a6f8720SBrian Somers close(s); 7567a6f8720SBrian Somers } 75783d1af55SBrian Somers 75883d1af55SBrian Somers void 7593006ec67SBrian Somers bundle_LinkClosed(struct bundle *bundle, struct datalink *dl) 7603006ec67SBrian Somers { 7613006ec67SBrian Somers /* 7623006ec67SBrian Somers * Our datalink has closed. 763565e35e5SBrian Somers * UpdateSet() will remove 1OFF and STDIN links. 7643b0f8d2eSBrian Somers * If it's the last data link, enter phase DEAD. 7653006ec67SBrian Somers */ 7665b8b8060SBrian Somers 7673b0f8d2eSBrian Somers struct datalink *odl; 7683b0f8d2eSBrian Somers int other_links; 7695b8b8060SBrian Somers 7703b0f8d2eSBrian Somers other_links = 0; 7713b0f8d2eSBrian Somers for (odl = bundle->links; odl; odl = odl->next) 7723b0f8d2eSBrian Somers if (odl != dl && odl->state != DATALINK_CLOSED) 7733b0f8d2eSBrian Somers other_links++; 7743b0f8d2eSBrian Somers 7753b0f8d2eSBrian Somers if (!other_links) { 776565e35e5SBrian Somers if (dl->physical->type != PHYS_DEMAND) 7773006ec67SBrian Somers bundle_DownInterface(bundle); 77826afeaa2SBrian Somers if (bundle->ncp.ipcp.fsm.state > ST_CLOSED || 77926afeaa2SBrian Somers bundle->ncp.ipcp.fsm.state == ST_STARTING) { 78026afeaa2SBrian Somers FsmDown(&bundle->ncp.ipcp.fsm); 78126afeaa2SBrian Somers FsmClose(&bundle->ncp.ipcp.fsm); /* ST_INITIAL please */ 78226afeaa2SBrian Somers } 7835563ebdeSBrian Somers bundle_NewPhase(bundle, PHASE_DEAD); 784b6217683SBrian Somers bundle_DisplayPrompt(bundle); 7853b0f8d2eSBrian Somers } 786455aabc3SBrian Somers } 787455aabc3SBrian Somers 788455aabc3SBrian Somers void 789565e35e5SBrian Somers bundle_Open(struct bundle *bundle, const char *name, int mask) 7903006ec67SBrian Somers { 7913006ec67SBrian Somers /* 7923006ec67SBrian Somers * Please open the given datalink, or all if name == NULL 7933006ec67SBrian Somers */ 7943006ec67SBrian Somers struct datalink *dl; 7953006ec67SBrian Somers 7963006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 7973006ec67SBrian Somers if (name == NULL || !strcasecmp(dl->name, name)) { 798565e35e5SBrian Somers if (mask & dl->physical->type) 799565e35e5SBrian Somers datalink_Up(dl, 1, 1); 8003006ec67SBrian Somers if (name != NULL) 8013006ec67SBrian Somers break; 8023006ec67SBrian Somers } 8033006ec67SBrian Somers } 8043006ec67SBrian Somers 8053006ec67SBrian Somers struct datalink * 8063006ec67SBrian Somers bundle2datalink(struct bundle *bundle, const char *name) 8073006ec67SBrian Somers { 8083006ec67SBrian Somers struct datalink *dl; 8093006ec67SBrian Somers 8103006ec67SBrian Somers if (name != NULL) { 8113006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 8123006ec67SBrian Somers if (!strcasecmp(dl->name, name)) 8133006ec67SBrian Somers return dl; 8143006ec67SBrian Somers } else if (bundle->links && !bundle->links->next) 8153006ec67SBrian Somers return bundle->links; 8163006ec67SBrian Somers 8173006ec67SBrian Somers return NULL; 8183006ec67SBrian Somers } 8193006ec67SBrian Somers 8203006ec67SBrian Somers int 8213006ec67SBrian Somers bundle_FillQueues(struct bundle *bundle) 8223006ec67SBrian Somers { 8233b0f8d2eSBrian Somers int total; 8243b0f8d2eSBrian Somers 8253b0f8d2eSBrian Somers if (bundle->ncp.mp.active) { 8263b0f8d2eSBrian Somers total = mp_FillQueues(bundle); 8273b0f8d2eSBrian Somers } else { 828833882f7SBrian Somers total = link_QueueLen(&bundle->links->physical->link); 829833882f7SBrian Somers if (total == 0 && bundle->links->physical->out == NULL) 830833882f7SBrian Somers total = IpFlushPacket(&bundle->links->physical->link, bundle); 8313006ec67SBrian Somers } 8323006ec67SBrian Somers 8333b0f8d2eSBrian Somers return total + ip_QueueLen(); 8343006ec67SBrian Somers } 835aef795ccSBrian Somers 836aef795ccSBrian Somers int 837aef795ccSBrian Somers bundle_ShowLinks(struct cmdargs const *arg) 838aef795ccSBrian Somers { 839c7cc5030SBrian Somers if (arg->cx) 840b6217683SBrian Somers datalink_Show(arg->cx, arg->prompt); 841c7cc5030SBrian Somers else { 842aef795ccSBrian Somers struct datalink *dl; 843aef795ccSBrian Somers 844aef795ccSBrian Somers for (dl = arg->bundle->links; dl; dl = dl->next) 845b6217683SBrian Somers datalink_Show(dl, arg->prompt); 846c7cc5030SBrian Somers } 847aef795ccSBrian Somers 848aef795ccSBrian Somers return 0; 849aef795ccSBrian Somers } 850ab886ad0SBrian Somers 8511342caedSBrian Somers static const char * 8521342caedSBrian Somers optval(struct bundle *bundle, int bit) 8531342caedSBrian Somers { 8541342caedSBrian Somers return (bundle->cfg.opt & bit) ? "enabled" : "disabled"; 8551342caedSBrian Somers } 8561342caedSBrian Somers 857c08717dfSBrian Somers int 858c08717dfSBrian Somers bundle_ShowStatus(struct cmdargs const *arg) 859c08717dfSBrian Somers { 860c08717dfSBrian Somers int remaining; 861c08717dfSBrian Somers 862c08717dfSBrian Somers prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle)); 863c08717dfSBrian Somers prompt_Printf(arg->prompt, " Interface: %s\n", arg->bundle->dev); 864c08717dfSBrian Somers 865c08717dfSBrian Somers prompt_Printf(arg->prompt, "\nDefaults:\n"); 866c08717dfSBrian Somers prompt_Printf(arg->prompt, " Auth name: %s\n", arg->bundle->cfg.auth.name); 867c08717dfSBrian Somers prompt_Printf(arg->prompt, " Idle Timer: "); 868c08717dfSBrian Somers if (arg->bundle->cfg.idle_timeout) { 869c08717dfSBrian Somers prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle_timeout); 870c08717dfSBrian Somers remaining = bundle_RemainingIdleTime(arg->bundle); 871c08717dfSBrian Somers if (remaining != -1) 872c08717dfSBrian Somers prompt_Printf(arg->prompt, " (%ds remaining)", remaining); 873c08717dfSBrian Somers prompt_Printf(arg->prompt, "\n"); 874c08717dfSBrian Somers } else 875c08717dfSBrian Somers prompt_Printf(arg->prompt, "disabled\n"); 876ce828a6eSBrian Somers prompt_Printf(arg->prompt, " MTU: "); 877ce828a6eSBrian Somers if (arg->bundle->cfg.mtu) 878ce828a6eSBrian Somers prompt_Printf(arg->prompt, "%d\n", arg->bundle->cfg.mtu); 879ce828a6eSBrian Somers else 880ce828a6eSBrian Somers prompt_Printf(arg->prompt, "unspecified\n"); 881c08717dfSBrian Somers 8821342caedSBrian Somers prompt_Printf(arg->prompt, " ID check: %s\n", 8831342caedSBrian Somers optval(arg->bundle, OPT_IDCHECK)); 8841342caedSBrian Somers prompt_Printf(arg->prompt, " Loopback: %s\n", 8851342caedSBrian Somers optval(arg->bundle, OPT_LOOPBACK)); 8861342caedSBrian Somers prompt_Printf(arg->prompt, " PasswdAuth: %s\n", 8871342caedSBrian Somers optval(arg->bundle, OPT_PASSWDAUTH)); 8881342caedSBrian Somers prompt_Printf(arg->prompt, " Proxy: %s\n", 8891342caedSBrian Somers optval(arg->bundle, OPT_PROXY)); 8901342caedSBrian Somers prompt_Printf(arg->prompt, " Throughput: %s\n", 8911342caedSBrian Somers optval(arg->bundle, OPT_THROUGHPUT)); 8921342caedSBrian Somers prompt_Printf(arg->prompt, " Utmp: %s\n", 8931342caedSBrian Somers optval(arg->bundle, OPT_UTMP)); 8941342caedSBrian Somers 895c08717dfSBrian Somers return 0; 896c08717dfSBrian Somers } 897c08717dfSBrian Somers 898ab886ad0SBrian Somers static void 899ab886ad0SBrian Somers bundle_IdleTimeout(void *v) 900ab886ad0SBrian Somers { 901ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v; 902ab886ad0SBrian Somers 90393ee0ff2SBrian Somers bundle->idle.done = 0; 90439d94652SBrian Somers LogPrintf(LogPHASE, "Idle timer expired.\n"); 905ab886ad0SBrian Somers bundle_Close(bundle, NULL, 1); 906ab886ad0SBrian Somers } 907ab886ad0SBrian Somers 908ab886ad0SBrian Somers /* 909ab886ad0SBrian Somers * Start Idle timer. If timeout is reached, we call bundle_Close() to 910ab886ad0SBrian Somers * close LCP and link. 911ab886ad0SBrian Somers */ 912ab886ad0SBrian Somers void 913ab886ad0SBrian Somers bundle_StartIdleTimer(struct bundle *bundle) 914ab886ad0SBrian Somers { 915ee084ab9SBrian Somers if (!(bundle->phys_type & (PHYS_DEDICATED|PHYS_PERM))) { 91693ee0ff2SBrian Somers StopTimer(&bundle->idle.timer); 917ee084ab9SBrian Somers if (bundle->cfg.idle_timeout) { 91893ee0ff2SBrian Somers bundle->idle.timer.func = bundle_IdleTimeout; 9193b0f8d2eSBrian Somers bundle->idle.timer.name = "idle"; 92093ee0ff2SBrian Somers bundle->idle.timer.load = bundle->cfg.idle_timeout * SECTICKS; 92193ee0ff2SBrian Somers bundle->idle.timer.arg = bundle; 92293ee0ff2SBrian Somers StartTimer(&bundle->idle.timer); 92393ee0ff2SBrian Somers bundle->idle.done = time(NULL) + bundle->cfg.idle_timeout; 924ab886ad0SBrian Somers } 925ab886ad0SBrian Somers } 926ee084ab9SBrian Somers } 927ab886ad0SBrian Somers 928ab886ad0SBrian Somers void 929ab886ad0SBrian Somers bundle_SetIdleTimer(struct bundle *bundle, int value) 930ab886ad0SBrian Somers { 931ab886ad0SBrian Somers bundle->cfg.idle_timeout = value; 932ab886ad0SBrian Somers if (bundle_LinkIsUp(bundle)) 933ab886ad0SBrian Somers bundle_StartIdleTimer(bundle); 934ab886ad0SBrian Somers } 935ab886ad0SBrian Somers 936ab886ad0SBrian Somers void 937ab886ad0SBrian Somers bundle_StopIdleTimer(struct bundle *bundle) 938ab886ad0SBrian Somers { 93993ee0ff2SBrian Somers StopTimer(&bundle->idle.timer); 9404a632c80SBrian Somers bundle->idle.done = 0; 941ab886ad0SBrian Somers } 942ab886ad0SBrian Somers 943ab886ad0SBrian Somers int 944ab886ad0SBrian Somers bundle_RemainingIdleTime(struct bundle *bundle) 945ab886ad0SBrian Somers { 94693ee0ff2SBrian Somers if (bundle->idle.done) 94793ee0ff2SBrian Somers return bundle->idle.done - time(NULL); 948ab886ad0SBrian Somers return -1; 949ab886ad0SBrian Somers } 9503b0f8d2eSBrian Somers 9513b0f8d2eSBrian Somers int 9523b0f8d2eSBrian Somers bundle_IsDead(struct bundle *bundle) 9533b0f8d2eSBrian Somers { 9543b0f8d2eSBrian Somers return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp); 9553b0f8d2eSBrian Somers } 956b6217683SBrian Somers 957b6217683SBrian Somers void 958b6217683SBrian Somers bundle_RegisterDescriptor(struct bundle *bundle, struct descriptor *d) 959b6217683SBrian Somers { 960b6217683SBrian Somers d->next = bundle->desc.next; 961b6217683SBrian Somers bundle->desc.next = d; 962b6217683SBrian Somers } 963b6217683SBrian Somers 964b6217683SBrian Somers void 965b6217683SBrian Somers bundle_UnRegisterDescriptor(struct bundle *bundle, struct descriptor *d) 966b6217683SBrian Somers { 967b6217683SBrian Somers struct descriptor **desc; 968b6217683SBrian Somers 969b6217683SBrian Somers for (desc = &bundle->desc.next; *desc; desc = &(*desc)->next) 970b6217683SBrian Somers if (*desc == d) { 971b6217683SBrian Somers *desc = d->next; 972b6217683SBrian Somers break; 973b6217683SBrian Somers } 974b6217683SBrian Somers } 975b6217683SBrian Somers 976b6217683SBrian Somers void 977b6217683SBrian Somers bundle_DelPromptDescriptors(struct bundle *bundle, struct server *s) 978b6217683SBrian Somers { 979b6217683SBrian Somers struct descriptor **desc; 980b6217683SBrian Somers struct prompt *p; 981b6217683SBrian Somers 982b6217683SBrian Somers desc = &bundle->desc.next; 983b6217683SBrian Somers while (*desc) { 984b6217683SBrian Somers if ((*desc)->type == PROMPT_DESCRIPTOR) { 985b6217683SBrian Somers p = (struct prompt *)*desc; 986b6217683SBrian Somers if (p->owner == s) { 987b6217683SBrian Somers prompt_Destroy(p, 1); 988b6217683SBrian Somers desc = &bundle->desc.next; 989b6217683SBrian Somers continue; 990b6217683SBrian Somers } 991b6217683SBrian Somers } 992b6217683SBrian Somers desc = &(*desc)->next; 993b6217683SBrian Somers } 994b6217683SBrian Somers } 995b6217683SBrian Somers 996b6217683SBrian Somers void 997b6217683SBrian Somers bundle_DisplayPrompt(struct bundle *bundle) 998b6217683SBrian Somers { 999b6217683SBrian Somers struct descriptor **desc; 1000b6217683SBrian Somers 1001b6217683SBrian Somers for (desc = &bundle->desc.next; *desc; desc = &(*desc)->next) 1002b6217683SBrian Somers if ((*desc)->type == PROMPT_DESCRIPTOR) 1003b6217683SBrian Somers prompt_Required((struct prompt *)*desc); 1004b6217683SBrian Somers } 1005b6217683SBrian Somers 1006b6217683SBrian Somers void 1007b6217683SBrian Somers bundle_WriteTermPrompt(struct bundle *bundle, struct datalink *dl, 1008b6217683SBrian Somers const char *data, int len) 1009b6217683SBrian Somers { 1010b6217683SBrian Somers struct descriptor *desc; 1011b6217683SBrian Somers struct prompt *p; 1012b6217683SBrian Somers 1013b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 1014b6217683SBrian Somers if (desc->type == PROMPT_DESCRIPTOR) { 1015b6217683SBrian Somers p = (struct prompt *)desc; 1016b6217683SBrian Somers if (prompt_IsTermMode(p, dl)) 1017c3a119d0SBrian Somers prompt_Printf(p, "%.*s", len, data); 1018b6217683SBrian Somers } 1019b6217683SBrian Somers } 1020b6217683SBrian Somers 1021b6217683SBrian Somers void 1022b6217683SBrian Somers bundle_SetTtyCommandMode(struct bundle *bundle, struct datalink *dl) 1023b6217683SBrian Somers { 1024b6217683SBrian Somers struct descriptor *desc; 1025b6217683SBrian Somers struct prompt *p; 1026b6217683SBrian Somers 1027b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 1028b6217683SBrian Somers if (desc->type == PROMPT_DESCRIPTOR) { 1029b6217683SBrian Somers p = (struct prompt *)desc; 1030b6217683SBrian Somers if (prompt_IsTermMode(p, dl)) 1031b6217683SBrian Somers prompt_TtyCommandMode(p); 1032b6217683SBrian Somers } 1033b6217683SBrian Somers } 1034565e35e5SBrian Somers 1035565e35e5SBrian Somers static void 1036565e35e5SBrian Somers bundle_GenPhysType(struct bundle *bundle) 1037565e35e5SBrian Somers { 1038565e35e5SBrian Somers struct datalink *dl; 1039565e35e5SBrian Somers 1040565e35e5SBrian Somers bundle->phys_type = 0; 1041565e35e5SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 1042565e35e5SBrian Somers bundle->phys_type |= dl->physical->type; 1043565e35e5SBrian Somers } 1044565e35e5SBrian Somers 1045cd7bd93aSBrian Somers void 1046cd7bd93aSBrian Somers bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl, 1047cd7bd93aSBrian Somers const char *name) 1048cd7bd93aSBrian Somers { 1049cd7bd93aSBrian Somers struct datalink *ndl = datalink_Clone(dl, name); 1050cd7bd93aSBrian Somers 1051cd7bd93aSBrian Somers ndl->next = dl->next; 1052cd7bd93aSBrian Somers dl->next = ndl; 1053565e35e5SBrian Somers bundle_GenPhysType(bundle); 1054cd7bd93aSBrian Somers } 1055cd7bd93aSBrian Somers 1056cd7bd93aSBrian Somers void 1057cd7bd93aSBrian Somers bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl) 1058cd7bd93aSBrian Somers { 1059cd7bd93aSBrian Somers struct datalink **dlp; 1060cd7bd93aSBrian Somers 1061cd7bd93aSBrian Somers if (dl->state == DATALINK_CLOSED) 1062cd7bd93aSBrian Somers for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next) 1063cd7bd93aSBrian Somers if (*dlp == dl) { 1064cd7bd93aSBrian Somers *dlp = datalink_Destroy(dl); 1065cd7bd93aSBrian Somers break; 1066cd7bd93aSBrian Somers } 1067565e35e5SBrian Somers bundle_GenPhysType(bundle); 1068565e35e5SBrian Somers } 1069565e35e5SBrian Somers 1070565e35e5SBrian Somers void 1071565e35e5SBrian Somers bundle_CleanDatalinks(struct bundle *bundle) 1072565e35e5SBrian Somers { 1073565e35e5SBrian Somers struct datalink **dlp = &bundle->links; 1074565e35e5SBrian Somers 1075565e35e5SBrian Somers while (*dlp) 1076565e35e5SBrian Somers if ((*dlp)->state == DATALINK_CLOSED && 1077565e35e5SBrian Somers (*dlp)->physical->type & (PHYS_STDIN|PHYS_1OFF)) 1078565e35e5SBrian Somers *dlp = datalink_Destroy(*dlp); 1079565e35e5SBrian Somers else 1080565e35e5SBrian Somers dlp = &(*dlp)->next; 1081565e35e5SBrian Somers bundle_GenPhysType(bundle); 1082cd7bd93aSBrian Somers } 108349052c95SBrian Somers 108449052c95SBrian Somers void 108549052c95SBrian Somers bundle_SetLabel(struct bundle *bundle, const char *label) 108649052c95SBrian Somers { 108749052c95SBrian Somers if (label) 108849052c95SBrian Somers strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1); 108949052c95SBrian Somers else 109049052c95SBrian Somers *bundle->cfg.label = '\0'; 109149052c95SBrian Somers } 109249052c95SBrian Somers 109349052c95SBrian Somers const char * 109449052c95SBrian Somers bundle_GetLabel(struct bundle *bundle) 109549052c95SBrian Somers { 109649052c95SBrian Somers return *bundle->cfg.label ? bundle->cfg.label : NULL; 109749052c95SBrian Somers } 1098