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 * 261fa665f5SBrian Somers * $Id: bundle.c,v 1.1.2.63 1998/04/27 01:40: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> 38e43ebac1SBrian Somers #include <net/if_tun.h> 391fa665f5SBrian Somers #include <sys/un.h> 407a6f8720SBrian Somers 417a6f8720SBrian Somers #include <errno.h> 427a6f8720SBrian Somers #include <fcntl.h> 437a6f8720SBrian Somers #include <stdio.h> 447a6f8720SBrian Somers #include <string.h> 457a6f8720SBrian Somers #include <sys/ioctl.h> 467a6f8720SBrian Somers #include <termios.h> 477a6f8720SBrian Somers #include <unistd.h> 487a6f8720SBrian Somers 497a6f8720SBrian Somers #include "command.h" 507a6f8720SBrian Somers #include "mbuf.h" 517a6f8720SBrian Somers #include "log.h" 527a6f8720SBrian Somers #include "id.h" 537a6f8720SBrian Somers #include "defs.h" 547a6f8720SBrian Somers #include "timer.h" 557a6f8720SBrian Somers #include "fsm.h" 567a6f8720SBrian Somers #include "iplist.h" 57879ed6faSBrian Somers #include "lqr.h" 58455aabc3SBrian Somers #include "hdlc.h" 597a6f8720SBrian Somers #include "throughput.h" 60eaa4df37SBrian Somers #include "slcompress.h" 617a6f8720SBrian Somers #include "ipcp.h" 625ca5389aSBrian Somers #include "filter.h" 632f786681SBrian Somers #include "descriptor.h" 647a6f8720SBrian Somers #include "route.h" 657a6f8720SBrian Somers #include "lcp.h" 667a6f8720SBrian Somers #include "ccp.h" 673b0f8d2eSBrian Somers #include "link.h" 683b0f8d2eSBrian Somers #include "mp.h" 693b0f8d2eSBrian Somers #include "bundle.h" 70455aabc3SBrian Somers #include "async.h" 71455aabc3SBrian Somers #include "physical.h" 722289f246SBrian Somers #include "modem.h" 73455aabc3SBrian Somers #include "auth.h" 74455aabc3SBrian Somers #include "lcpproto.h" 75455aabc3SBrian Somers #include "chap.h" 76455aabc3SBrian Somers #include "tun.h" 7785b542cfSBrian Somers #include "prompt.h" 783006ec67SBrian Somers #include "chat.h" 793006ec67SBrian Somers #include "datalink.h" 803006ec67SBrian Somers #include "ip.h" 817a6f8720SBrian Somers 82455aabc3SBrian Somers static const char *PhaseNames[] = { 83455aabc3SBrian Somers "Dead", "Establish", "Authenticate", "Network", "Terminate" 84455aabc3SBrian Somers }; 85455aabc3SBrian Somers 86455aabc3SBrian Somers const char * 87455aabc3SBrian Somers bundle_PhaseName(struct bundle *bundle) 887a6f8720SBrian Somers { 89455aabc3SBrian Somers return bundle->phase <= PHASE_TERMINATE ? 90455aabc3SBrian Somers PhaseNames[bundle->phase] : "unknown"; 917a6f8720SBrian Somers } 927a6f8720SBrian Somers 93455aabc3SBrian Somers void 945563ebdeSBrian Somers bundle_NewPhase(struct bundle *bundle, u_int new) 95455aabc3SBrian Somers { 96aef795ccSBrian Somers if (new == bundle->phase) 97aef795ccSBrian Somers return; 98aef795ccSBrian Somers 99e2ebb036SBrian Somers if (new <= PHASE_TERMINATE) 100e2ebb036SBrian Somers LogPrintf(LogPHASE, "bundle: %s\n", PhaseNames[new]); 1017a6f8720SBrian Somers 102455aabc3SBrian Somers switch (new) { 103455aabc3SBrian Somers case PHASE_DEAD: 104455aabc3SBrian Somers bundle->phase = new; 105455aabc3SBrian Somers break; 106455aabc3SBrian Somers 107455aabc3SBrian Somers case PHASE_ESTABLISH: 108455aabc3SBrian Somers bundle->phase = new; 109455aabc3SBrian Somers break; 110455aabc3SBrian Somers 111455aabc3SBrian Somers case PHASE_AUTHENTICATE: 112455aabc3SBrian Somers bundle->phase = new; 113b6217683SBrian Somers bundle_DisplayPrompt(bundle); 114455aabc3SBrian Somers break; 115455aabc3SBrian Somers 116455aabc3SBrian Somers case PHASE_NETWORK: 1175828db6dSBrian Somers ipcp_Setup(&bundle->ncp.ipcp); 1185828db6dSBrian Somers FsmUp(&bundle->ncp.ipcp.fsm); 1195828db6dSBrian Somers FsmOpen(&bundle->ncp.ipcp.fsm); 120673903ecSBrian Somers bundle->phase = new; 121673903ecSBrian Somers bundle_DisplayPrompt(bundle); 122673903ecSBrian Somers break; 123455aabc3SBrian Somers 124455aabc3SBrian Somers case PHASE_TERMINATE: 125455aabc3SBrian Somers bundle->phase = new; 126673903ecSBrian Somers mp_Down(&bundle->ncp.mp); 127b6217683SBrian Somers bundle_DisplayPrompt(bundle); 128455aabc3SBrian Somers break; 1297a6f8720SBrian Somers } 1307a6f8720SBrian Somers } 1317a6f8720SBrian Somers 1327a6f8720SBrian Somers static int 1337a6f8720SBrian Somers bundle_CleanInterface(const struct bundle *bundle) 1347a6f8720SBrian Somers { 1357a6f8720SBrian Somers int s; 1367a6f8720SBrian Somers struct ifreq ifrq; 1377a6f8720SBrian Somers struct ifaliasreq ifra; 1387a6f8720SBrian Somers 1397a6f8720SBrian Somers s = ID0socket(AF_INET, SOCK_DGRAM, 0); 1407a6f8720SBrian Somers if (s < 0) { 1417a6f8720SBrian Somers LogPrintf(LogERROR, "bundle_CleanInterface: socket(): %s\n", 1427a6f8720SBrian Somers strerror(errno)); 1437a6f8720SBrian Somers return (-1); 1447a6f8720SBrian Somers } 1457a6f8720SBrian Somers strncpy(ifrq.ifr_name, bundle->ifname, sizeof ifrq.ifr_name - 1); 1467a6f8720SBrian Somers ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 1477a6f8720SBrian Somers while (ID0ioctl(s, SIOCGIFADDR, &ifrq) == 0) { 1487a6f8720SBrian Somers memset(&ifra.ifra_mask, '\0', sizeof ifra.ifra_mask); 1497a6f8720SBrian Somers strncpy(ifra.ifra_name, bundle->ifname, sizeof ifra.ifra_name - 1); 1507a6f8720SBrian Somers ifra.ifra_name[sizeof ifra.ifra_name - 1] = '\0'; 1517a6f8720SBrian Somers ifra.ifra_addr = ifrq.ifr_addr; 1527a6f8720SBrian Somers if (ID0ioctl(s, SIOCGIFDSTADDR, &ifrq) < 0) { 1537a6f8720SBrian Somers if (ifra.ifra_addr.sa_family == AF_INET) 1547a6f8720SBrian Somers LogPrintf(LogERROR, 1557a6f8720SBrian Somers "bundle_CleanInterface: Can't get dst for %s on %s !\n", 1567a6f8720SBrian Somers inet_ntoa(((struct sockaddr_in *)&ifra.ifra_addr)->sin_addr), 1577a6f8720SBrian Somers bundle->ifname); 1587a6f8720SBrian Somers return 0; 1597a6f8720SBrian Somers } 1607a6f8720SBrian Somers ifra.ifra_broadaddr = ifrq.ifr_dstaddr; 1617a6f8720SBrian Somers if (ID0ioctl(s, SIOCDIFADDR, &ifra) < 0) { 1627a6f8720SBrian Somers if (ifra.ifra_addr.sa_family == AF_INET) 1637a6f8720SBrian Somers LogPrintf(LogERROR, 1647a6f8720SBrian Somers "bundle_CleanInterface: Can't delete %s address on %s !\n", 1657a6f8720SBrian Somers inet_ntoa(((struct sockaddr_in *)&ifra.ifra_addr)->sin_addr), 1667a6f8720SBrian Somers bundle->ifname); 1677a6f8720SBrian Somers return 0; 1687a6f8720SBrian Somers } 1697a6f8720SBrian Somers } 1707a6f8720SBrian Somers 1717a6f8720SBrian Somers return 1; 1727a6f8720SBrian Somers } 1737a6f8720SBrian Somers 1746d666775SBrian Somers static void 1756d666775SBrian Somers bundle_LayerStart(void *v, struct fsm *fp) 1767a6f8720SBrian Somers { 1773006ec67SBrian Somers /* The given FSM is about to start up ! */ 1787a6f8720SBrian Somers } 1797a6f8720SBrian Somers 1805cf4388bSBrian Somers 1815cf4388bSBrian Somers static void 1825cf4388bSBrian Somers bundle_Notify(struct bundle *bundle, char c) 1835cf4388bSBrian Somers { 1845cf4388bSBrian Somers if (bundle->notify.fd != -1) { 1855cf4388bSBrian Somers if (write(bundle->notify.fd, &c, 1) == 1) 1865cf4388bSBrian Somers LogPrintf(LogPHASE, "Parent notified of success.\n"); 1875cf4388bSBrian Somers else 1885cf4388bSBrian Somers LogPrintf(LogPHASE, "Failed to notify parent of success.\n"); 1895cf4388bSBrian Somers close(bundle->notify.fd); 1905cf4388bSBrian Somers bundle->notify.fd = -1; 1915cf4388bSBrian Somers } 1925cf4388bSBrian Somers } 1933b0f8d2eSBrian Somers 1946d666775SBrian Somers static void 1953b0f8d2eSBrian Somers bundle_vLayerUp(void *v, struct fsm *fp) 1963b0f8d2eSBrian Somers { 1973b0f8d2eSBrian Somers bundle_LayerUp((struct bundle *)v, fp); 1983b0f8d2eSBrian Somers } 1993b0f8d2eSBrian Somers 2003b0f8d2eSBrian Somers void 2013b0f8d2eSBrian Somers bundle_LayerUp(struct bundle *bundle, struct fsm *fp) 2027a6f8720SBrian Somers { 2033006ec67SBrian Somers /* 2043006ec67SBrian Somers * The given fsm is now up 20549052c95SBrian Somers * If it's an LCP set our mtu (if we're multilink, add up the link 20649052c95SBrian Somers * speeds and set the MRRU). 207565e35e5SBrian Somers * If it's an NCP, tell our -background parent to go away. 2083b0f8d2eSBrian Somers * If it's the first NCP, start the idle timer. 2093006ec67SBrian Somers */ 2106d666775SBrian Somers 2115563ebdeSBrian Somers if (fp->proto == PROTO_LCP) { 2123b0f8d2eSBrian Somers if (bundle->ncp.mp.active) { 2133b0f8d2eSBrian Somers int speed; 2143b0f8d2eSBrian Somers struct datalink *dl; 2155563ebdeSBrian Somers 2163b0f8d2eSBrian Somers for (dl = bundle->links, speed = 0; dl; dl = dl->next) 2173b0f8d2eSBrian Somers speed += modem_Speed(dl->physical); 2183b0f8d2eSBrian Somers if (speed) 21949052c95SBrian Somers tun_configure(bundle, bundle->ncp.mp.peer_mrru, speed); 2203b0f8d2eSBrian Somers } else 2213b0f8d2eSBrian Somers tun_configure(bundle, fsm2lcp(fp)->his_mru, 2223b0f8d2eSBrian Somers modem_Speed(link2physical(fp->link))); 2233b0f8d2eSBrian Somers } else if (fp->proto == PROTO_IPCP) { 224ab886ad0SBrian Somers bundle_StartIdleTimer(bundle); 2255cf4388bSBrian Somers bundle_Notify(bundle, EX_NORMAL); 2267a6f8720SBrian Somers } 227ab886ad0SBrian Somers } 2287a6f8720SBrian Somers 2296d666775SBrian Somers static void 2306d666775SBrian Somers bundle_LayerDown(void *v, struct fsm *fp) 2316d666775SBrian Somers { 2326d666775SBrian Somers /* 2336d666775SBrian Somers * The given FSM has been told to come down. 234ab886ad0SBrian Somers * If it's our last NCP, stop the idle timer. 2353b0f8d2eSBrian Somers * If it's our last NCP *OR* LCP, enter TERMINATE phase. 2363b0f8d2eSBrian Somers * If it's an LCP and we're in multilink mode, adjust our tun speed. 2376d666775SBrian Somers */ 238ab886ad0SBrian Somers 239ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v; 240ab886ad0SBrian Somers 2413b0f8d2eSBrian Somers if (fp->proto == PROTO_IPCP) { 242ab886ad0SBrian Somers bundle_StopIdleTimer(bundle); 2433b0f8d2eSBrian Somers } else if (fp->proto == PROTO_LCP) { 2443b0f8d2eSBrian Somers int speed, others_active; 2453b0f8d2eSBrian Somers struct datalink *dl; 2463b0f8d2eSBrian Somers 2473b0f8d2eSBrian Somers others_active = 0; 2483b0f8d2eSBrian Somers for (dl = bundle->links, speed = 0; dl; dl = dl->next) 2493b0f8d2eSBrian Somers if (fp != &dl->physical->link.lcp.fsm && 2503b0f8d2eSBrian Somers dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) { 2513b0f8d2eSBrian Somers speed += modem_Speed(dl->physical); 2523b0f8d2eSBrian Somers others_active++; 2533b0f8d2eSBrian Somers } 2543b0f8d2eSBrian Somers if (bundle->ncp.mp.active && speed) 2553b0f8d2eSBrian Somers tun_configure(bundle, bundle->ncp.mp.link.lcp.his_mru, speed); 2563b0f8d2eSBrian Somers 2573b0f8d2eSBrian Somers if (!others_active) 2583b0f8d2eSBrian Somers bundle_NewPhase(bundle, PHASE_TERMINATE); 2593b0f8d2eSBrian Somers } 2606d666775SBrian Somers } 2616d666775SBrian Somers 2626d666775SBrian Somers static void 2636d666775SBrian Somers bundle_LayerFinish(void *v, struct fsm *fp) 2646d666775SBrian Somers { 2656d666775SBrian Somers /* The given fsm is now down (fp cannot be NULL) 2666d666775SBrian Somers * 2676d666775SBrian Somers * If it's the last LCP, FsmDown all NCPs 2683b0f8d2eSBrian Somers * If it's the last NCP, FsmClose all LCPs 2696d666775SBrian Somers */ 2706d666775SBrian Somers 2716d666775SBrian Somers struct bundle *bundle = (struct bundle *)v; 2726d666775SBrian Somers struct datalink *dl; 2736d666775SBrian Somers 2743b0f8d2eSBrian Somers if (fp->proto == PROTO_IPCP) { 27526afeaa2SBrian Somers if (bundle_Phase(bundle) != PHASE_DEAD) 27625092092SBrian Somers bundle_NewPhase(bundle, PHASE_TERMINATE); 2776d666775SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 2783b0f8d2eSBrian Somers datalink_Close(dl, 0); 2793b0f8d2eSBrian Somers FsmDown(fp); 2803b0f8d2eSBrian Somers FsmClose(fp); 2813b0f8d2eSBrian Somers } else if (fp->proto == PROTO_LCP) { 2823b0f8d2eSBrian Somers int others_active; 283a611cad6SBrian Somers 2843b0f8d2eSBrian Somers others_active = 0; 2853b0f8d2eSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 2863b0f8d2eSBrian Somers if (fp != &dl->physical->link.lcp.fsm && 2873b0f8d2eSBrian Somers dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 2883b0f8d2eSBrian Somers others_active++; 2893b0f8d2eSBrian Somers 2903b0f8d2eSBrian Somers if (!others_active) { 2915828db6dSBrian Somers FsmDown(&bundle->ncp.ipcp.fsm); 2925828db6dSBrian Somers FsmClose(&bundle->ncp.ipcp.fsm); /* ST_INITIAL please */ 2936d666775SBrian Somers } 2943b0f8d2eSBrian Somers } 2953b0f8d2eSBrian Somers } 2966d666775SBrian Somers 2977a6f8720SBrian Somers int 2987a6f8720SBrian Somers bundle_LinkIsUp(const struct bundle *bundle) 2997a6f8720SBrian Somers { 3005828db6dSBrian Somers return bundle->ncp.ipcp.fsm.state == ST_OPENED; 3017a6f8720SBrian Somers } 3027a6f8720SBrian Somers 3037a6f8720SBrian Somers void 3043006ec67SBrian Somers bundle_Close(struct bundle *bundle, const char *name, int staydown) 3057a6f8720SBrian Somers { 306455aabc3SBrian Somers /* 3073006ec67SBrian Somers * Please close the given datalink. 3083b0f8d2eSBrian Somers * If name == NULL or name is the last datalink, enter TERMINATE phase 3093b0f8d2eSBrian Somers * and FsmClose all NCPs (except our MP) 3103b0f8d2eSBrian Somers * If it isn't the last datalink, just Close that datalink. 311455aabc3SBrian Somers */ 3127a6f8720SBrian Somers 3133b0f8d2eSBrian Somers struct datalink *dl, *this_dl; 3143b0f8d2eSBrian Somers int others_active; 3153006ec67SBrian Somers 3163b0f8d2eSBrian Somers if (bundle->phase == PHASE_TERMINATE || bundle->phase == PHASE_DEAD) 3173b0f8d2eSBrian Somers return; 3183b0f8d2eSBrian Somers 3193b0f8d2eSBrian Somers others_active = 0; 3203b0f8d2eSBrian Somers this_dl = NULL; 3213b0f8d2eSBrian Somers 3223b0f8d2eSBrian Somers for (dl = bundle->links; dl; dl = dl->next) { 3233b0f8d2eSBrian Somers if (name && !strcasecmp(name, dl->name)) 3243b0f8d2eSBrian Somers this_dl = dl; 3253b0f8d2eSBrian Somers if (name == NULL || this_dl == dl) { 326d345321bSBrian Somers if (staydown) 3273006ec67SBrian Somers datalink_StayDown(dl); 3283b0f8d2eSBrian Somers } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 3293b0f8d2eSBrian Somers others_active++; 3303b0f8d2eSBrian Somers } 3313b0f8d2eSBrian Somers 3323b0f8d2eSBrian Somers if (name && this_dl == NULL) { 3333b0f8d2eSBrian Somers LogPrintf(LogWARN, "%s: Invalid datalink name\n", name); 3343b0f8d2eSBrian Somers return; 3353b0f8d2eSBrian Somers } 3363b0f8d2eSBrian Somers 3373b0f8d2eSBrian Somers if (!others_active) { 3383b0f8d2eSBrian Somers if (bundle->ncp.ipcp.fsm.state > ST_CLOSED || 3393b0f8d2eSBrian Somers bundle->ncp.ipcp.fsm.state == ST_STARTING) 3403b0f8d2eSBrian Somers FsmClose(&bundle->ncp.ipcp.fsm); 3413b0f8d2eSBrian Somers else { 3425828db6dSBrian Somers if (bundle->ncp.ipcp.fsm.state > ST_INITIAL) { 3435828db6dSBrian Somers FsmClose(&bundle->ncp.ipcp.fsm); 3445828db6dSBrian Somers FsmDown(&bundle->ncp.ipcp.fsm); 345d2fd8d77SBrian Somers } 346d345321bSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 347d345321bSBrian Somers datalink_Close(dl, staydown); 3487a6f8720SBrian Somers } 3493b0f8d2eSBrian Somers } else if (this_dl && this_dl->state != DATALINK_CLOSED && 3503b0f8d2eSBrian Somers this_dl->state != DATALINK_HANGUP) 3513b0f8d2eSBrian Somers datalink_Close(this_dl, staydown); 352d2fd8d77SBrian Somers } 3537a6f8720SBrian Somers 3542f786681SBrian Somers static int 3552f786681SBrian Somers bundle_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n) 3562f786681SBrian Somers { 3572f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d); 3582f786681SBrian Somers struct datalink *dl; 359b6217683SBrian Somers struct descriptor *desc; 3602f786681SBrian Somers int result; 3612f786681SBrian Somers 3622f786681SBrian Somers result = 0; 3632f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 3642f786681SBrian Somers result += descriptor_UpdateSet(&dl->desc, r, w, e, n); 3652f786681SBrian Somers 366b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 367b6217683SBrian Somers result += descriptor_UpdateSet(desc, r, w, e, n); 368b6217683SBrian Somers 3692f786681SBrian Somers return result; 3702f786681SBrian Somers } 3712f786681SBrian Somers 3722f786681SBrian Somers static int 3732f786681SBrian Somers bundle_IsSet(struct descriptor *d, const fd_set *fdset) 3742f786681SBrian Somers { 3752f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d); 3762f786681SBrian Somers struct datalink *dl; 377b6217683SBrian Somers struct descriptor *desc; 3782f786681SBrian Somers 3792f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 3802f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 3812f786681SBrian Somers return 1; 3822f786681SBrian Somers 383b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 384b6217683SBrian Somers if (descriptor_IsSet(desc, fdset)) 385b6217683SBrian Somers return 1; 386b6217683SBrian Somers 3872f786681SBrian Somers return 0; 3882f786681SBrian Somers } 3892f786681SBrian Somers 3902f786681SBrian Somers static void 3912f786681SBrian Somers bundle_DescriptorRead(struct descriptor *d, struct bundle *bundle, 3922f786681SBrian Somers const fd_set *fdset) 3932f786681SBrian Somers { 3942f786681SBrian Somers struct datalink *dl; 395b6217683SBrian Somers struct descriptor *desc; 3962f786681SBrian Somers 3972f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 3982f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 3992f786681SBrian Somers descriptor_Read(&dl->desc, bundle, fdset); 400b6217683SBrian Somers 401b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 402b6217683SBrian Somers if (descriptor_IsSet(desc, fdset)) 403b6217683SBrian Somers descriptor_Read(desc, bundle, fdset); 4042f786681SBrian Somers } 4052f786681SBrian Somers 4062f786681SBrian Somers static void 4072f786681SBrian Somers bundle_DescriptorWrite(struct descriptor *d, struct bundle *bundle, 4082f786681SBrian Somers const fd_set *fdset) 4092f786681SBrian Somers { 4102f786681SBrian Somers struct datalink *dl; 411b6217683SBrian Somers struct descriptor *desc; 4122f786681SBrian Somers 4132f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 4142f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 4152f786681SBrian Somers descriptor_Write(&dl->desc, bundle, fdset); 416b6217683SBrian Somers 417b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 418b6217683SBrian Somers if (descriptor_IsSet(desc, fdset)) 419b6217683SBrian Somers descriptor_Write(desc, bundle, fdset); 4202f786681SBrian Somers } 4212f786681SBrian Somers 4227a6f8720SBrian Somers 4237a6f8720SBrian Somers struct bundle * 424565e35e5SBrian Somers bundle_Create(const char *prefix, struct prompt *prompt, int type) 4257a6f8720SBrian Somers { 4267a6f8720SBrian Somers int s, enoentcount, err; 4277a6f8720SBrian Somers struct ifreq ifrq; 4287a6f8720SBrian Somers static struct bundle bundle; /* there can be only one */ 4297a6f8720SBrian Somers 4307a6f8720SBrian Somers if (bundle.ifname != NULL) { /* Already allocated ! */ 4317a6f8720SBrian Somers LogPrintf(LogERROR, "bundle_Create: There's only one BUNDLE !\n"); 4327a6f8720SBrian Somers return NULL; 4337a6f8720SBrian Somers } 4347a6f8720SBrian Somers 4357a6f8720SBrian Somers err = ENOENT; 4367a6f8720SBrian Somers enoentcount = 0; 437107d62e7SBrian Somers for (bundle.unit = 0; ; bundle.unit++) { 4387a6f8720SBrian Somers snprintf(bundle.dev, sizeof bundle.dev, "%s%d", prefix, bundle.unit); 4397a6f8720SBrian Somers bundle.tun_fd = ID0open(bundle.dev, O_RDWR); 4407a6f8720SBrian Somers if (bundle.tun_fd >= 0) 4417a6f8720SBrian Somers break; 442107d62e7SBrian Somers else if (errno == ENXIO) { 4437a6f8720SBrian Somers err = errno; 444107d62e7SBrian Somers break; 4457a6f8720SBrian Somers } else if (errno == ENOENT) { 4467a6f8720SBrian Somers if (++enoentcount > 2) 447107d62e7SBrian Somers break; 4487a6f8720SBrian Somers } else 4497a6f8720SBrian Somers err = errno; 4507a6f8720SBrian Somers } 4517a6f8720SBrian Somers 452107d62e7SBrian Somers if (bundle.tun_fd < 0) { 453107d62e7SBrian Somers LogPrintf(LogWARN, "No available tunnel devices found (%s).\n", 45485b542cfSBrian Somers strerror(err)); 4557a6f8720SBrian Somers return NULL; 4567a6f8720SBrian Somers } 4577a6f8720SBrian Somers 4587a6f8720SBrian Somers LogSetTun(bundle.unit); 4597a6f8720SBrian Somers 4607a6f8720SBrian Somers s = socket(AF_INET, SOCK_DGRAM, 0); 4617a6f8720SBrian Somers if (s < 0) { 4627a6f8720SBrian Somers LogPrintf(LogERROR, "bundle_Create: socket(): %s\n", strerror(errno)); 4637a6f8720SBrian Somers close(bundle.tun_fd); 4647a6f8720SBrian Somers return NULL; 4657a6f8720SBrian Somers } 4667a6f8720SBrian Somers 4677a6f8720SBrian Somers bundle.ifname = strrchr(bundle.dev, '/'); 4687a6f8720SBrian Somers if (bundle.ifname == NULL) 4697a6f8720SBrian Somers bundle.ifname = bundle.dev; 4707a6f8720SBrian Somers else 4717a6f8720SBrian Somers bundle.ifname++; 4727a6f8720SBrian Somers 4737a6f8720SBrian Somers /* 4747a6f8720SBrian Somers * Now, bring up the interface. 4757a6f8720SBrian Somers */ 4767a6f8720SBrian Somers memset(&ifrq, '\0', sizeof ifrq); 4777a6f8720SBrian Somers strncpy(ifrq.ifr_name, bundle.ifname, sizeof ifrq.ifr_name - 1); 4787a6f8720SBrian Somers ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 4797a6f8720SBrian Somers if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) { 4807a6f8720SBrian Somers LogPrintf(LogERROR, "OpenTunnel: ioctl(SIOCGIFFLAGS): %s\n", 4817a6f8720SBrian Somers strerror(errno)); 4827a6f8720SBrian Somers close(s); 4837a6f8720SBrian Somers close(bundle.tun_fd); 4847a6f8720SBrian Somers bundle.ifname = NULL; 4857a6f8720SBrian Somers return NULL; 4867a6f8720SBrian Somers } 4877a6f8720SBrian Somers ifrq.ifr_flags |= IFF_UP; 4887a6f8720SBrian Somers if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) { 4897a6f8720SBrian Somers LogPrintf(LogERROR, "OpenTunnel: ioctl(SIOCSIFFLAGS): %s\n", 4907a6f8720SBrian Somers strerror(errno)); 4917a6f8720SBrian Somers close(s); 4927a6f8720SBrian Somers close(bundle.tun_fd); 4937a6f8720SBrian Somers bundle.ifname = NULL; 4947a6f8720SBrian Somers return NULL; 4957a6f8720SBrian Somers } 4967a6f8720SBrian Somers 4977a6f8720SBrian Somers close(s); 4987a6f8720SBrian Somers 4997a6f8720SBrian Somers if ((bundle.ifIndex = GetIfIndex(bundle.ifname)) < 0) { 5007a6f8720SBrian Somers LogPrintf(LogERROR, "OpenTunnel: Can't find ifindex.\n"); 5017a6f8720SBrian Somers close(bundle.tun_fd); 5027a6f8720SBrian Somers bundle.ifname = NULL; 5037a6f8720SBrian Somers return NULL; 5047a6f8720SBrian Somers } 5057a6f8720SBrian Somers 506b6217683SBrian Somers prompt_Printf(prompt, "Using interface: %s\n", bundle.ifname); 5077a6f8720SBrian Somers LogPrintf(LogPHASE, "Using interface: %s\n", bundle.ifname); 5087a6f8720SBrian Somers 509820de6ebSBrian Somers bundle.routing_seq = 0; 510a0cbd833SBrian Somers bundle.phase = PHASE_DEAD; 511a0cbd833SBrian Somers bundle.CleaningUp = 0; 5127a6f8720SBrian Somers 5136d666775SBrian Somers bundle.fsm.LayerStart = bundle_LayerStart; 5143b0f8d2eSBrian Somers bundle.fsm.LayerUp = bundle_vLayerUp; 5156d666775SBrian Somers bundle.fsm.LayerDown = bundle_LayerDown; 5166d666775SBrian Somers bundle.fsm.LayerFinish = bundle_LayerFinish; 5176d666775SBrian Somers bundle.fsm.object = &bundle; 5187a6f8720SBrian Somers 519ab886ad0SBrian Somers bundle.cfg.idle_timeout = NCP_IDLE_TIMEOUT; 5201342caedSBrian Somers *bundle.cfg.auth.name = '\0'; 5211342caedSBrian Somers *bundle.cfg.auth.key = '\0'; 5221342caedSBrian Somers bundle.cfg.opt = OPT_IDCHECK | OPT_LOOPBACK | OPT_THROUGHPUT | OPT_UTMP; 52349052c95SBrian Somers *bundle.cfg.label = '\0'; 52449052c95SBrian Somers bundle.cfg.mtu = DEF_MTU; 525565e35e5SBrian Somers bundle.phys_type = type; 526ab886ad0SBrian Somers 527d47dceb8SBrian Somers bundle.links = datalink_Create("deflink", &bundle, &bundle.fsm, type); 5283006ec67SBrian Somers if (bundle.links == NULL) { 5293006ec67SBrian Somers LogPrintf(LogERROR, "Cannot create data link: %s\n", strerror(errno)); 5306d666775SBrian Somers close(bundle.tun_fd); 5316d666775SBrian Somers bundle.ifname = NULL; 5322289f246SBrian Somers return NULL; 5332289f246SBrian Somers } 5342289f246SBrian Somers 5352f786681SBrian Somers bundle.desc.type = BUNDLE_DESCRIPTOR; 5362f786681SBrian Somers bundle.desc.next = NULL; 5372f786681SBrian Somers bundle.desc.UpdateSet = bundle_UpdateSet; 5382f786681SBrian Somers bundle.desc.IsSet = bundle_IsSet; 5392f786681SBrian Somers bundle.desc.Read = bundle_DescriptorRead; 5402f786681SBrian Somers bundle.desc.Write = bundle_DescriptorWrite; 5412f786681SBrian Somers 54249052c95SBrian Somers mp_Init(&bundle.ncp.mp, &bundle); 54349052c95SBrian Somers 54449052c95SBrian Somers /* Send over the first physical link by default */ 5455828db6dSBrian Somers ipcp_Init(&bundle.ncp.ipcp, &bundle, &bundle.links->physical->link, 5465828db6dSBrian Somers &bundle.fsm); 5476d666775SBrian Somers 5485ca5389aSBrian Somers memset(&bundle.filter, '\0', sizeof bundle.filter); 5495ca5389aSBrian Somers bundle.filter.in.fragok = bundle.filter.in.logok = 1; 5505ca5389aSBrian Somers bundle.filter.in.name = "IN"; 5515ca5389aSBrian Somers bundle.filter.out.fragok = bundle.filter.out.logok = 1; 5525ca5389aSBrian Somers bundle.filter.out.name = "OUT"; 5535ca5389aSBrian Somers bundle.filter.dial.name = "DIAL"; 5548390b576SBrian Somers bundle.filter.dial.logok = 1; 5555ca5389aSBrian Somers bundle.filter.alive.name = "ALIVE"; 5565ca5389aSBrian Somers bundle.filter.alive.logok = 1; 55793ee0ff2SBrian Somers memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer); 55893ee0ff2SBrian Somers bundle.idle.done = 0; 5595cf4388bSBrian Somers bundle.notify.fd = -1; 56093ee0ff2SBrian Somers 5616d666775SBrian Somers /* Clean out any leftover crud */ 5626d666775SBrian Somers bundle_CleanInterface(&bundle); 5636d666775SBrian Somers 56485602e52SBrian Somers if (prompt) { 56585602e52SBrian Somers /* Retrospectively introduce ourselves to the prompt */ 56685602e52SBrian Somers prompt->bundle = &bundle; 56785602e52SBrian Somers bundle_RegisterDescriptor(&bundle, &prompt->desc); 56885602e52SBrian Somers } 56985602e52SBrian Somers 5707a6f8720SBrian Somers return &bundle; 5717a6f8720SBrian Somers } 5727a6f8720SBrian Somers 57368a0f0ccSBrian Somers static void 57468a0f0ccSBrian Somers bundle_DownInterface(struct bundle *bundle) 57568a0f0ccSBrian Somers { 57668a0f0ccSBrian Somers struct ifreq ifrq; 57768a0f0ccSBrian Somers int s; 57868a0f0ccSBrian Somers 57968a0f0ccSBrian Somers DeleteIfRoutes(bundle, 1); 58068a0f0ccSBrian Somers 58168a0f0ccSBrian Somers s = ID0socket(AF_INET, SOCK_DGRAM, 0); 58268a0f0ccSBrian Somers if (s < 0) { 58368a0f0ccSBrian Somers LogPrintf(LogERROR, "bundle_DownInterface: socket: %s\n", strerror(errno)); 58468a0f0ccSBrian Somers return; 58568a0f0ccSBrian Somers } 58668a0f0ccSBrian Somers 58768a0f0ccSBrian Somers memset(&ifrq, '\0', sizeof ifrq); 58868a0f0ccSBrian Somers strncpy(ifrq.ifr_name, bundle->ifname, sizeof ifrq.ifr_name - 1); 58968a0f0ccSBrian Somers ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 59068a0f0ccSBrian Somers if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) { 59168a0f0ccSBrian Somers LogPrintf(LogERROR, "bundle_DownInterface: ioctl(SIOCGIFFLAGS): %s\n", 59268a0f0ccSBrian Somers strerror(errno)); 59368a0f0ccSBrian Somers close(s); 59468a0f0ccSBrian Somers return; 59568a0f0ccSBrian Somers } 59668a0f0ccSBrian Somers ifrq.ifr_flags &= ~IFF_UP; 59768a0f0ccSBrian Somers if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) { 59868a0f0ccSBrian Somers LogPrintf(LogERROR, "bundle_DownInterface: ioctl(SIOCSIFFLAGS): %s\n", 59968a0f0ccSBrian Somers strerror(errno)); 60068a0f0ccSBrian Somers close(s); 60168a0f0ccSBrian Somers return; 60268a0f0ccSBrian Somers } 60368a0f0ccSBrian Somers close(s); 60468a0f0ccSBrian Somers } 60568a0f0ccSBrian Somers 60668a0f0ccSBrian Somers void 60768a0f0ccSBrian Somers bundle_Destroy(struct bundle *bundle) 60868a0f0ccSBrian Somers { 6093006ec67SBrian Somers struct datalink *dl; 610b6217683SBrian Somers struct descriptor *desc, *ndesc; 611b6217683SBrian Somers 612565e35e5SBrian Somers if (bundle->phys_type & PHYS_DEMAND) { 613565e35e5SBrian Somers IpcpCleanInterface(&bundle->ncp.ipcp); 61468a0f0ccSBrian Somers bundle_DownInterface(bundle); 61568a0f0ccSBrian Somers } 6163006ec67SBrian Somers 6173006ec67SBrian Somers dl = bundle->links; 6183006ec67SBrian Somers while (dl) 6193006ec67SBrian Somers dl = datalink_Destroy(dl); 6203006ec67SBrian Somers 6215cf4388bSBrian Somers bundle_Notify(bundle, EX_ERRDEAD); 622b6217683SBrian Somers 623b6217683SBrian Somers desc = bundle->desc.next; 624b6217683SBrian Somers while (desc) { 625b6217683SBrian Somers ndesc = desc->next; 626b6217683SBrian Somers if (desc->type == PROMPT_DESCRIPTOR) 627b6217683SBrian Somers prompt_Destroy((struct prompt *)desc, 1); 628b6217683SBrian Somers else 629b6217683SBrian Somers LogPrintf(LogERROR, "bundle_Destroy: Don't know how to delete descriptor" 630b6217683SBrian Somers " type %d\n", desc->type); 631b6217683SBrian Somers desc = ndesc; 632b6217683SBrian Somers } 633b6217683SBrian Somers bundle->desc.next = NULL; 63468a0f0ccSBrian Somers bundle->ifname = NULL; 63568a0f0ccSBrian Somers } 63668a0f0ccSBrian Somers 6377a6f8720SBrian Somers struct rtmsg { 6387a6f8720SBrian Somers struct rt_msghdr m_rtm; 6397a6f8720SBrian Somers char m_space[64]; 6407a6f8720SBrian Somers }; 6417a6f8720SBrian Somers 6427a6f8720SBrian Somers void 643820de6ebSBrian Somers bundle_SetRoute(struct bundle *bundle, int cmd, struct in_addr dst, 6447a6f8720SBrian Somers struct in_addr gateway, struct in_addr mask, int bang) 6457a6f8720SBrian Somers { 6467a6f8720SBrian Somers struct rtmsg rtmes; 6477a6f8720SBrian Somers int s, nb, wb; 6487a6f8720SBrian Somers char *cp; 6497a6f8720SBrian Somers const char *cmdstr; 6507a6f8720SBrian Somers struct sockaddr_in rtdata; 6517a6f8720SBrian Somers 6527a6f8720SBrian Somers if (bang) 6537a6f8720SBrian Somers cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!"); 6547a6f8720SBrian Somers else 6557a6f8720SBrian Somers cmdstr = (cmd == RTM_ADD ? "Add" : "Delete"); 6567a6f8720SBrian Somers s = ID0socket(PF_ROUTE, SOCK_RAW, 0); 6577a6f8720SBrian Somers if (s < 0) { 65868a0f0ccSBrian Somers LogPrintf(LogERROR, "bundle_SetRoute: socket(): %s\n", strerror(errno)); 6597a6f8720SBrian Somers return; 6607a6f8720SBrian Somers } 6617a6f8720SBrian Somers memset(&rtmes, '\0', sizeof rtmes); 6627a6f8720SBrian Somers rtmes.m_rtm.rtm_version = RTM_VERSION; 6637a6f8720SBrian Somers rtmes.m_rtm.rtm_type = cmd; 6647a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs = RTA_DST; 665820de6ebSBrian Somers rtmes.m_rtm.rtm_seq = ++bundle->routing_seq; 6667a6f8720SBrian Somers rtmes.m_rtm.rtm_pid = getpid(); 6677a6f8720SBrian Somers rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC; 6687a6f8720SBrian Somers 6697a6f8720SBrian Somers memset(&rtdata, '\0', sizeof rtdata); 67050e5c17dSBrian Somers rtdata.sin_len = sizeof rtdata; 6717a6f8720SBrian Somers rtdata.sin_family = AF_INET; 6727a6f8720SBrian Somers rtdata.sin_port = 0; 6737a6f8720SBrian Somers rtdata.sin_addr = dst; 6747a6f8720SBrian Somers 6757a6f8720SBrian Somers cp = rtmes.m_space; 67650e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 67750e5c17dSBrian Somers cp += rtdata.sin_len; 678e43ebac1SBrian Somers if (cmd == RTM_ADD) { 6797a6f8720SBrian Somers if (gateway.s_addr == INADDR_ANY) { 6807a6f8720SBrian Somers /* Add a route through the interface */ 6817a6f8720SBrian Somers struct sockaddr_dl dl; 6827a6f8720SBrian Somers const char *iname; 6837a6f8720SBrian Somers int ilen; 6847a6f8720SBrian Somers 6857a6f8720SBrian Somers iname = Index2Nam(bundle->ifIndex); 6867a6f8720SBrian Somers ilen = strlen(iname); 6877a6f8720SBrian Somers dl.sdl_len = sizeof dl - sizeof dl.sdl_data + ilen; 6887a6f8720SBrian Somers dl.sdl_family = AF_LINK; 6897a6f8720SBrian Somers dl.sdl_index = bundle->ifIndex; 6907a6f8720SBrian Somers dl.sdl_type = 0; 6917a6f8720SBrian Somers dl.sdl_nlen = ilen; 6927a6f8720SBrian Somers dl.sdl_alen = 0; 6937a6f8720SBrian Somers dl.sdl_slen = 0; 6947a6f8720SBrian Somers strncpy(dl.sdl_data, iname, sizeof dl.sdl_data); 6957a6f8720SBrian Somers memcpy(cp, &dl, dl.sdl_len); 6967a6f8720SBrian Somers cp += dl.sdl_len; 6977a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 6987a6f8720SBrian Somers } else { 6997a6f8720SBrian Somers rtdata.sin_addr = gateway; 70050e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 70150e5c17dSBrian Somers cp += rtdata.sin_len; 7027a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 7037a6f8720SBrian Somers } 704e43ebac1SBrian Somers } 7057a6f8720SBrian Somers 7067a6f8720SBrian Somers if (dst.s_addr == INADDR_ANY) 7077a6f8720SBrian Somers mask.s_addr = INADDR_ANY; 7087a6f8720SBrian Somers 7097a6f8720SBrian Somers if (cmd == RTM_ADD || dst.s_addr == INADDR_ANY) { 7107a6f8720SBrian Somers rtdata.sin_addr = mask; 71150e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 71250e5c17dSBrian Somers cp += rtdata.sin_len; 7137a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_NETMASK; 7147a6f8720SBrian Somers } 7157a6f8720SBrian Somers 7167a6f8720SBrian Somers nb = cp - (char *) &rtmes; 7177a6f8720SBrian Somers rtmes.m_rtm.rtm_msglen = nb; 7187a6f8720SBrian Somers wb = ID0write(s, &rtmes, nb); 7197a6f8720SBrian Somers if (wb < 0) { 72068a0f0ccSBrian Somers LogPrintf(LogTCPIP, "bundle_SetRoute failure:\n"); 72150e5c17dSBrian Somers LogPrintf(LogTCPIP, "bundle_SetRoute: Cmd = %s\n", cmdstr); 72268a0f0ccSBrian Somers LogPrintf(LogTCPIP, "bundle_SetRoute: Dst = %s\n", inet_ntoa(dst)); 72368a0f0ccSBrian Somers LogPrintf(LogTCPIP, "bundle_SetRoute: Gateway = %s\n", inet_ntoa(gateway)); 72468a0f0ccSBrian Somers LogPrintf(LogTCPIP, "bundle_SetRoute: Mask = %s\n", inet_ntoa(mask)); 7257a6f8720SBrian Somers failed: 7267a6f8720SBrian Somers if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST || 727e43ebac1SBrian Somers (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) { 7287a6f8720SBrian Somers if (!bang) 7297a6f8720SBrian Somers LogPrintf(LogWARN, "Add route failed: %s already exists\n", 7307a6f8720SBrian Somers inet_ntoa(dst)); 7317a6f8720SBrian Somers else { 7327a6f8720SBrian Somers rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE; 7337a6f8720SBrian Somers if ((wb = ID0write(s, &rtmes, nb)) < 0) 7347a6f8720SBrian Somers goto failed; 7357a6f8720SBrian Somers } 736e43ebac1SBrian Somers } else if (cmd == RTM_DELETE && 7377a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == ESRCH || 7387a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) { 7397a6f8720SBrian Somers if (!bang) 7407a6f8720SBrian Somers LogPrintf(LogWARN, "Del route failed: %s: Non-existent\n", 7417a6f8720SBrian Somers inet_ntoa(dst)); 7427a6f8720SBrian Somers } else if (rtmes.m_rtm.rtm_errno == 0) 7437a6f8720SBrian Somers LogPrintf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr, 7447a6f8720SBrian Somers inet_ntoa(dst), strerror(errno)); 7457a6f8720SBrian Somers else 7467a6f8720SBrian Somers LogPrintf(LogWARN, "%s route failed: %s: %s\n", 7477a6f8720SBrian Somers cmdstr, inet_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno)); 7487a6f8720SBrian Somers } 7497a6f8720SBrian Somers LogPrintf(LogDEBUG, "wrote %d: cmd = %s, dst = %x, gateway = %x\n", 750fe3125a0SBrian Somers wb, cmdstr, (unsigned)dst.s_addr, (unsigned)gateway.s_addr); 7517a6f8720SBrian Somers close(s); 7527a6f8720SBrian Somers } 75383d1af55SBrian Somers 75483d1af55SBrian Somers void 7553006ec67SBrian Somers bundle_LinkClosed(struct bundle *bundle, struct datalink *dl) 7563006ec67SBrian Somers { 7573006ec67SBrian Somers /* 7583006ec67SBrian Somers * Our datalink has closed. 759565e35e5SBrian Somers * UpdateSet() will remove 1OFF and STDIN links. 7603b0f8d2eSBrian Somers * If it's the last data link, enter phase DEAD. 7613006ec67SBrian Somers */ 7625b8b8060SBrian Somers 7633b0f8d2eSBrian Somers struct datalink *odl; 7643b0f8d2eSBrian Somers int other_links; 7655b8b8060SBrian Somers 7663b0f8d2eSBrian Somers other_links = 0; 7673b0f8d2eSBrian Somers for (odl = bundle->links; odl; odl = odl->next) 7683b0f8d2eSBrian Somers if (odl != dl && odl->state != DATALINK_CLOSED) 7693b0f8d2eSBrian Somers other_links++; 7703b0f8d2eSBrian Somers 7713b0f8d2eSBrian Somers if (!other_links) { 772565e35e5SBrian Somers if (dl->physical->type != PHYS_DEMAND) 7733006ec67SBrian Somers bundle_DownInterface(bundle); 77426afeaa2SBrian Somers if (bundle->ncp.ipcp.fsm.state > ST_CLOSED || 77526afeaa2SBrian Somers bundle->ncp.ipcp.fsm.state == ST_STARTING) { 77626afeaa2SBrian Somers FsmDown(&bundle->ncp.ipcp.fsm); 77726afeaa2SBrian Somers FsmClose(&bundle->ncp.ipcp.fsm); /* ST_INITIAL please */ 77826afeaa2SBrian Somers } 7795563ebdeSBrian Somers bundle_NewPhase(bundle, PHASE_DEAD); 780b6217683SBrian Somers bundle_DisplayPrompt(bundle); 7813b0f8d2eSBrian Somers } 782455aabc3SBrian Somers } 783455aabc3SBrian Somers 784455aabc3SBrian Somers void 785565e35e5SBrian Somers bundle_Open(struct bundle *bundle, const char *name, int mask) 7863006ec67SBrian Somers { 7873006ec67SBrian Somers /* 7883006ec67SBrian Somers * Please open the given datalink, or all if name == NULL 7893006ec67SBrian Somers */ 7903006ec67SBrian Somers struct datalink *dl; 7913006ec67SBrian Somers 7923006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 7933006ec67SBrian Somers if (name == NULL || !strcasecmp(dl->name, name)) { 794565e35e5SBrian Somers if (mask & dl->physical->type) 795565e35e5SBrian Somers datalink_Up(dl, 1, 1); 7963006ec67SBrian Somers if (name != NULL) 7973006ec67SBrian Somers break; 7983006ec67SBrian Somers } 7993006ec67SBrian Somers } 8003006ec67SBrian Somers 8013006ec67SBrian Somers struct datalink * 8023006ec67SBrian Somers bundle2datalink(struct bundle *bundle, const char *name) 8033006ec67SBrian Somers { 8043006ec67SBrian Somers struct datalink *dl; 8053006ec67SBrian Somers 8063006ec67SBrian Somers if (name != NULL) { 8073006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 8083006ec67SBrian Somers if (!strcasecmp(dl->name, name)) 8093006ec67SBrian Somers return dl; 8103006ec67SBrian Somers } else if (bundle->links && !bundle->links->next) 8113006ec67SBrian Somers return bundle->links; 8123006ec67SBrian Somers 8133006ec67SBrian Somers return NULL; 8143006ec67SBrian Somers } 8153006ec67SBrian Somers 8163006ec67SBrian Somers int 8173006ec67SBrian Somers bundle_FillQueues(struct bundle *bundle) 8183006ec67SBrian Somers { 8193b0f8d2eSBrian Somers int total; 8203b0f8d2eSBrian Somers 8213b0f8d2eSBrian Somers if (bundle->ncp.mp.active) { 8223b0f8d2eSBrian Somers total = mp_FillQueues(bundle); 8233b0f8d2eSBrian Somers } else { 824833882f7SBrian Somers total = link_QueueLen(&bundle->links->physical->link); 825833882f7SBrian Somers if (total == 0 && bundle->links->physical->out == NULL) 826833882f7SBrian Somers total = IpFlushPacket(&bundle->links->physical->link, bundle); 8273006ec67SBrian Somers } 8283006ec67SBrian Somers 8293b0f8d2eSBrian Somers return total + ip_QueueLen(); 8303006ec67SBrian Somers } 831aef795ccSBrian Somers 832aef795ccSBrian Somers int 833aef795ccSBrian Somers bundle_ShowLinks(struct cmdargs const *arg) 834aef795ccSBrian Somers { 835aef795ccSBrian Somers struct datalink *dl; 836aef795ccSBrian Somers 837aef795ccSBrian Somers for (dl = arg->bundle->links; dl; dl = dl->next) 838643f4904SBrian Somers prompt_Printf(arg->prompt, "Name: %s [%s]\n", dl->name, datalink_State(dl)); 839aef795ccSBrian Somers 840aef795ccSBrian Somers return 0; 841aef795ccSBrian Somers } 842ab886ad0SBrian Somers 8431342caedSBrian Somers static const char * 8441342caedSBrian Somers optval(struct bundle *bundle, int bit) 8451342caedSBrian Somers { 8461342caedSBrian Somers return (bundle->cfg.opt & bit) ? "enabled" : "disabled"; 8471342caedSBrian Somers } 8481342caedSBrian Somers 849c08717dfSBrian Somers int 850c08717dfSBrian Somers bundle_ShowStatus(struct cmdargs const *arg) 851c08717dfSBrian Somers { 852c08717dfSBrian Somers int remaining; 853c08717dfSBrian Somers 854c08717dfSBrian Somers prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle)); 855c08717dfSBrian Somers prompt_Printf(arg->prompt, " Interface: %s\n", arg->bundle->dev); 856c08717dfSBrian Somers 857c08717dfSBrian Somers prompt_Printf(arg->prompt, "\nDefaults:\n"); 858643f4904SBrian Somers prompt_Printf(arg->prompt, " Label: %s\n", arg->bundle->cfg.label); 859c08717dfSBrian Somers prompt_Printf(arg->prompt, " Auth name: %s\n", arg->bundle->cfg.auth.name); 860c08717dfSBrian Somers prompt_Printf(arg->prompt, " Idle Timer: "); 861c08717dfSBrian Somers if (arg->bundle->cfg.idle_timeout) { 862c08717dfSBrian Somers prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle_timeout); 863c08717dfSBrian Somers remaining = bundle_RemainingIdleTime(arg->bundle); 864c08717dfSBrian Somers if (remaining != -1) 865c08717dfSBrian Somers prompt_Printf(arg->prompt, " (%ds remaining)", remaining); 866c08717dfSBrian Somers prompt_Printf(arg->prompt, "\n"); 867c08717dfSBrian Somers } else 868c08717dfSBrian Somers prompt_Printf(arg->prompt, "disabled\n"); 869ce828a6eSBrian Somers prompt_Printf(arg->prompt, " MTU: "); 870ce828a6eSBrian Somers if (arg->bundle->cfg.mtu) 871ce828a6eSBrian Somers prompt_Printf(arg->prompt, "%d\n", arg->bundle->cfg.mtu); 872ce828a6eSBrian Somers else 873ce828a6eSBrian Somers prompt_Printf(arg->prompt, "unspecified\n"); 874c08717dfSBrian Somers 8751342caedSBrian Somers prompt_Printf(arg->prompt, " ID check: %s\n", 8761342caedSBrian Somers optval(arg->bundle, OPT_IDCHECK)); 8771342caedSBrian Somers prompt_Printf(arg->prompt, " Loopback: %s\n", 8781342caedSBrian Somers optval(arg->bundle, OPT_LOOPBACK)); 8791342caedSBrian Somers prompt_Printf(arg->prompt, " PasswdAuth: %s\n", 8801342caedSBrian Somers optval(arg->bundle, OPT_PASSWDAUTH)); 8811342caedSBrian Somers prompt_Printf(arg->prompt, " Proxy: %s\n", 8821342caedSBrian Somers optval(arg->bundle, OPT_PROXY)); 8831342caedSBrian Somers prompt_Printf(arg->prompt, " Throughput: %s\n", 8841342caedSBrian Somers optval(arg->bundle, OPT_THROUGHPUT)); 8851342caedSBrian Somers prompt_Printf(arg->prompt, " Utmp: %s\n", 8861342caedSBrian Somers optval(arg->bundle, OPT_UTMP)); 8871342caedSBrian Somers 888c08717dfSBrian Somers return 0; 889c08717dfSBrian Somers } 890c08717dfSBrian Somers 891ab886ad0SBrian Somers static void 892ab886ad0SBrian Somers bundle_IdleTimeout(void *v) 893ab886ad0SBrian Somers { 894ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v; 895ab886ad0SBrian Somers 89693ee0ff2SBrian Somers bundle->idle.done = 0; 89739d94652SBrian Somers LogPrintf(LogPHASE, "Idle timer expired.\n"); 898ab886ad0SBrian Somers bundle_Close(bundle, NULL, 1); 899ab886ad0SBrian Somers } 900ab886ad0SBrian Somers 901ab886ad0SBrian Somers /* 902ab886ad0SBrian Somers * Start Idle timer. If timeout is reached, we call bundle_Close() to 903ab886ad0SBrian Somers * close LCP and link. 904ab886ad0SBrian Somers */ 905ab886ad0SBrian Somers void 906ab886ad0SBrian Somers bundle_StartIdleTimer(struct bundle *bundle) 907ab886ad0SBrian Somers { 908ee084ab9SBrian Somers if (!(bundle->phys_type & (PHYS_DEDICATED|PHYS_PERM))) { 90993ee0ff2SBrian Somers StopTimer(&bundle->idle.timer); 910ee084ab9SBrian Somers if (bundle->cfg.idle_timeout) { 91193ee0ff2SBrian Somers bundle->idle.timer.func = bundle_IdleTimeout; 9123b0f8d2eSBrian Somers bundle->idle.timer.name = "idle"; 91393ee0ff2SBrian Somers bundle->idle.timer.load = bundle->cfg.idle_timeout * SECTICKS; 91493ee0ff2SBrian Somers bundle->idle.timer.arg = bundle; 91593ee0ff2SBrian Somers StartTimer(&bundle->idle.timer); 91693ee0ff2SBrian Somers bundle->idle.done = time(NULL) + bundle->cfg.idle_timeout; 917ab886ad0SBrian Somers } 918ab886ad0SBrian Somers } 919ee084ab9SBrian Somers } 920ab886ad0SBrian Somers 921ab886ad0SBrian Somers void 922ab886ad0SBrian Somers bundle_SetIdleTimer(struct bundle *bundle, int value) 923ab886ad0SBrian Somers { 924ab886ad0SBrian Somers bundle->cfg.idle_timeout = value; 925ab886ad0SBrian Somers if (bundle_LinkIsUp(bundle)) 926ab886ad0SBrian Somers bundle_StartIdleTimer(bundle); 927ab886ad0SBrian Somers } 928ab886ad0SBrian Somers 929ab886ad0SBrian Somers void 930ab886ad0SBrian Somers bundle_StopIdleTimer(struct bundle *bundle) 931ab886ad0SBrian Somers { 93293ee0ff2SBrian Somers StopTimer(&bundle->idle.timer); 9334a632c80SBrian Somers bundle->idle.done = 0; 934ab886ad0SBrian Somers } 935ab886ad0SBrian Somers 936ab886ad0SBrian Somers int 937ab886ad0SBrian Somers bundle_RemainingIdleTime(struct bundle *bundle) 938ab886ad0SBrian Somers { 93993ee0ff2SBrian Somers if (bundle->idle.done) 94093ee0ff2SBrian Somers return bundle->idle.done - time(NULL); 941ab886ad0SBrian Somers return -1; 942ab886ad0SBrian Somers } 9433b0f8d2eSBrian Somers 9443b0f8d2eSBrian Somers int 9453b0f8d2eSBrian Somers bundle_IsDead(struct bundle *bundle) 9463b0f8d2eSBrian Somers { 9473b0f8d2eSBrian Somers return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp); 9483b0f8d2eSBrian Somers } 949b6217683SBrian Somers 950b6217683SBrian Somers void 951b6217683SBrian Somers bundle_RegisterDescriptor(struct bundle *bundle, struct descriptor *d) 952b6217683SBrian Somers { 953b6217683SBrian Somers d->next = bundle->desc.next; 954b6217683SBrian Somers bundle->desc.next = d; 955b6217683SBrian Somers } 956b6217683SBrian Somers 957b6217683SBrian Somers void 958b6217683SBrian Somers bundle_UnRegisterDescriptor(struct bundle *bundle, struct descriptor *d) 959b6217683SBrian Somers { 960b6217683SBrian Somers struct descriptor **desc; 961b6217683SBrian Somers 962b6217683SBrian Somers for (desc = &bundle->desc.next; *desc; desc = &(*desc)->next) 963b6217683SBrian Somers if (*desc == d) { 964b6217683SBrian Somers *desc = d->next; 965b6217683SBrian Somers break; 966b6217683SBrian Somers } 967b6217683SBrian Somers } 968b6217683SBrian Somers 969b6217683SBrian Somers void 970b6217683SBrian Somers bundle_DelPromptDescriptors(struct bundle *bundle, struct server *s) 971b6217683SBrian Somers { 972b6217683SBrian Somers struct descriptor **desc; 973b6217683SBrian Somers struct prompt *p; 974b6217683SBrian Somers 975b6217683SBrian Somers desc = &bundle->desc.next; 976b6217683SBrian Somers while (*desc) { 977b6217683SBrian Somers if ((*desc)->type == PROMPT_DESCRIPTOR) { 978b6217683SBrian Somers p = (struct prompt *)*desc; 979b6217683SBrian Somers if (p->owner == s) { 980b6217683SBrian Somers prompt_Destroy(p, 1); 981b6217683SBrian Somers desc = &bundle->desc.next; 982b6217683SBrian Somers continue; 983b6217683SBrian Somers } 984b6217683SBrian Somers } 985b6217683SBrian Somers desc = &(*desc)->next; 986b6217683SBrian Somers } 987b6217683SBrian Somers } 988b6217683SBrian Somers 989b6217683SBrian Somers void 990b6217683SBrian Somers bundle_DisplayPrompt(struct bundle *bundle) 991b6217683SBrian Somers { 992b6217683SBrian Somers struct descriptor **desc; 993b6217683SBrian Somers 994b6217683SBrian Somers for (desc = &bundle->desc.next; *desc; desc = &(*desc)->next) 995b6217683SBrian Somers if ((*desc)->type == PROMPT_DESCRIPTOR) 996b6217683SBrian Somers prompt_Required((struct prompt *)*desc); 997b6217683SBrian Somers } 998b6217683SBrian Somers 999b6217683SBrian Somers void 1000b6217683SBrian Somers bundle_WriteTermPrompt(struct bundle *bundle, struct datalink *dl, 1001b6217683SBrian Somers const char *data, int len) 1002b6217683SBrian Somers { 1003b6217683SBrian Somers struct descriptor *desc; 1004b6217683SBrian Somers struct prompt *p; 1005b6217683SBrian Somers 1006b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 1007b6217683SBrian Somers if (desc->type == PROMPT_DESCRIPTOR) { 1008b6217683SBrian Somers p = (struct prompt *)desc; 1009b6217683SBrian Somers if (prompt_IsTermMode(p, dl)) 1010c3a119d0SBrian Somers prompt_Printf(p, "%.*s", len, data); 1011b6217683SBrian Somers } 1012b6217683SBrian Somers } 1013b6217683SBrian Somers 1014b6217683SBrian Somers void 1015b6217683SBrian Somers bundle_SetTtyCommandMode(struct bundle *bundle, struct datalink *dl) 1016b6217683SBrian Somers { 1017b6217683SBrian Somers struct descriptor *desc; 1018b6217683SBrian Somers struct prompt *p; 1019b6217683SBrian Somers 1020b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 1021b6217683SBrian Somers if (desc->type == PROMPT_DESCRIPTOR) { 1022b6217683SBrian Somers p = (struct prompt *)desc; 1023b6217683SBrian Somers if (prompt_IsTermMode(p, dl)) 1024b6217683SBrian Somers prompt_TtyCommandMode(p); 1025b6217683SBrian Somers } 1026b6217683SBrian Somers } 1027565e35e5SBrian Somers 1028565e35e5SBrian Somers static void 1029565e35e5SBrian Somers bundle_GenPhysType(struct bundle *bundle) 1030565e35e5SBrian Somers { 1031565e35e5SBrian Somers struct datalink *dl; 1032565e35e5SBrian Somers 1033565e35e5SBrian Somers bundle->phys_type = 0; 1034565e35e5SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 1035565e35e5SBrian Somers bundle->phys_type |= dl->physical->type; 1036565e35e5SBrian Somers } 1037565e35e5SBrian Somers 1038cd7bd93aSBrian Somers void 1039cd7bd93aSBrian Somers bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl, 1040cd7bd93aSBrian Somers const char *name) 1041cd7bd93aSBrian Somers { 1042cd7bd93aSBrian Somers struct datalink *ndl = datalink_Clone(dl, name); 1043cd7bd93aSBrian Somers 1044cd7bd93aSBrian Somers ndl->next = dl->next; 1045cd7bd93aSBrian Somers dl->next = ndl; 1046565e35e5SBrian Somers bundle_GenPhysType(bundle); 1047cd7bd93aSBrian Somers } 1048cd7bd93aSBrian Somers 1049cd7bd93aSBrian Somers void 1050cd7bd93aSBrian Somers bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl) 1051cd7bd93aSBrian Somers { 1052cd7bd93aSBrian Somers struct datalink **dlp; 1053cd7bd93aSBrian Somers 1054cd7bd93aSBrian Somers if (dl->state == DATALINK_CLOSED) 1055cd7bd93aSBrian Somers for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next) 1056cd7bd93aSBrian Somers if (*dlp == dl) { 1057cd7bd93aSBrian Somers *dlp = datalink_Destroy(dl); 1058cd7bd93aSBrian Somers break; 1059cd7bd93aSBrian Somers } 1060565e35e5SBrian Somers bundle_GenPhysType(bundle); 1061565e35e5SBrian Somers } 1062565e35e5SBrian Somers 1063565e35e5SBrian Somers void 1064565e35e5SBrian Somers bundle_CleanDatalinks(struct bundle *bundle) 1065565e35e5SBrian Somers { 1066565e35e5SBrian Somers struct datalink **dlp = &bundle->links; 1067565e35e5SBrian Somers 1068565e35e5SBrian Somers while (*dlp) 1069565e35e5SBrian Somers if ((*dlp)->state == DATALINK_CLOSED && 1070565e35e5SBrian Somers (*dlp)->physical->type & (PHYS_STDIN|PHYS_1OFF)) 1071565e35e5SBrian Somers *dlp = datalink_Destroy(*dlp); 1072565e35e5SBrian Somers else 1073565e35e5SBrian Somers dlp = &(*dlp)->next; 1074565e35e5SBrian Somers bundle_GenPhysType(bundle); 1075cd7bd93aSBrian Somers } 107649052c95SBrian Somers 107749052c95SBrian Somers void 107849052c95SBrian Somers bundle_SetLabel(struct bundle *bundle, const char *label) 107949052c95SBrian Somers { 108049052c95SBrian Somers if (label) 108149052c95SBrian Somers strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1); 108249052c95SBrian Somers else 108349052c95SBrian Somers *bundle->cfg.label = '\0'; 108449052c95SBrian Somers } 108549052c95SBrian Somers 108649052c95SBrian Somers const char * 108749052c95SBrian Somers bundle_GetLabel(struct bundle *bundle) 108849052c95SBrian Somers { 108949052c95SBrian Somers return *bundle->cfg.label ? bundle->cfg.label : NULL; 109049052c95SBrian Somers } 10911fa665f5SBrian Somers 10921fa665f5SBrian Somers void 10931fa665f5SBrian Somers bundle_SendDatalink(struct datalink *dl, int fd) 10941fa665f5SBrian Somers { 10951fa665f5SBrian Somers LogPrintf(LogERROR, "Can't send link yet !\n"); 10961fa665f5SBrian Somers close(fd); 10971fa665f5SBrian Somers } 10981fa665f5SBrian Somers 10991fa665f5SBrian Somers void 11001fa665f5SBrian Somers bundle_ReceiveDatalink(struct bundle *bundle, int fd) 11011fa665f5SBrian Somers { 11021fa665f5SBrian Somers LogPrintf(LogERROR, "Can't receive link yet !\n"); 11031fa665f5SBrian Somers close(fd); 11041fa665f5SBrian Somers } 1105