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 * 260f2f3eb3SBrian Somers * $Id: bundle.c,v 1.3 1998/05/23 17:05:26 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> 4347723d29SBrian Somers #include <paths.h> 447a6f8720SBrian Somers #include <stdio.h> 456f384573SBrian Somers #include <stdlib.h> 467a6f8720SBrian Somers #include <string.h> 477a6f8720SBrian Somers #include <sys/ioctl.h> 4896c9bb21SBrian Somers #include <sys/uio.h> 497a6f8720SBrian Somers #include <termios.h> 507a6f8720SBrian Somers #include <unistd.h> 517a6f8720SBrian Somers 527a6f8720SBrian Somers #include "command.h" 537a6f8720SBrian Somers #include "mbuf.h" 547a6f8720SBrian Somers #include "log.h" 557a6f8720SBrian Somers #include "id.h" 567a6f8720SBrian Somers #include "defs.h" 577a6f8720SBrian Somers #include "timer.h" 587a6f8720SBrian Somers #include "fsm.h" 597a6f8720SBrian Somers #include "iplist.h" 60879ed6faSBrian Somers #include "lqr.h" 61455aabc3SBrian Somers #include "hdlc.h" 627a6f8720SBrian Somers #include "throughput.h" 63eaa4df37SBrian Somers #include "slcompress.h" 647a6f8720SBrian Somers #include "ipcp.h" 655ca5389aSBrian Somers #include "filter.h" 662f786681SBrian Somers #include "descriptor.h" 677a6f8720SBrian Somers #include "route.h" 687a6f8720SBrian Somers #include "lcp.h" 697a6f8720SBrian Somers #include "ccp.h" 703b0f8d2eSBrian Somers #include "link.h" 713b0f8d2eSBrian Somers #include "mp.h" 723b0f8d2eSBrian Somers #include "bundle.h" 73455aabc3SBrian Somers #include "async.h" 74455aabc3SBrian Somers #include "physical.h" 752289f246SBrian Somers #include "modem.h" 76078c562eSBrian Somers #include "loadalias.h" 77455aabc3SBrian Somers #include "auth.h" 78455aabc3SBrian Somers #include "lcpproto.h" 79455aabc3SBrian Somers #include "chap.h" 80455aabc3SBrian Somers #include "tun.h" 8185b542cfSBrian Somers #include "prompt.h" 823006ec67SBrian Somers #include "chat.h" 833006ec67SBrian Somers #include "datalink.h" 843006ec67SBrian Somers #include "ip.h" 857a6f8720SBrian Somers 8696c9bb21SBrian Somers #define SCATTER_SEGMENTS 4 /* version, datalink, name, physical */ 8796c9bb21SBrian Somers #define SOCKET_OVERHEAD 100 /* additional buffer space for large */ 8896c9bb21SBrian Somers /* {recv,send}msg() calls */ 8996c9bb21SBrian Somers 9004eaa58cSBrian Somers static int bundle_RemainingIdleTime(struct bundle *); 9104eaa58cSBrian Somers static int bundle_RemainingAutoLoadTime(struct bundle *); 9204eaa58cSBrian Somers 93455aabc3SBrian Somers static const char *PhaseNames[] = { 94455aabc3SBrian Somers "Dead", "Establish", "Authenticate", "Network", "Terminate" 95455aabc3SBrian Somers }; 96455aabc3SBrian Somers 97455aabc3SBrian Somers const char * 98455aabc3SBrian Somers bundle_PhaseName(struct bundle *bundle) 997a6f8720SBrian Somers { 100455aabc3SBrian Somers return bundle->phase <= PHASE_TERMINATE ? 101455aabc3SBrian Somers PhaseNames[bundle->phase] : "unknown"; 1027a6f8720SBrian Somers } 1037a6f8720SBrian Somers 104455aabc3SBrian Somers void 1055563ebdeSBrian Somers bundle_NewPhase(struct bundle *bundle, u_int new) 106455aabc3SBrian Somers { 107aef795ccSBrian Somers if (new == bundle->phase) 108aef795ccSBrian Somers return; 109aef795ccSBrian Somers 110e2ebb036SBrian Somers if (new <= PHASE_TERMINATE) 111dd7e2610SBrian Somers log_Printf(LogPHASE, "bundle: %s\n", PhaseNames[new]); 1127a6f8720SBrian Somers 113455aabc3SBrian Somers switch (new) { 114455aabc3SBrian Somers case PHASE_DEAD: 1150f2f3eb3SBrian Somers log_DisplayPrompts(); 116455aabc3SBrian Somers bundle->phase = new; 117455aabc3SBrian Somers break; 118455aabc3SBrian Somers 119455aabc3SBrian Somers case PHASE_ESTABLISH: 120455aabc3SBrian Somers bundle->phase = new; 121455aabc3SBrian Somers break; 122455aabc3SBrian Somers 123455aabc3SBrian Somers case PHASE_AUTHENTICATE: 124455aabc3SBrian Somers bundle->phase = new; 1250f2f3eb3SBrian Somers log_DisplayPrompts(); 126455aabc3SBrian Somers break; 127455aabc3SBrian Somers 128455aabc3SBrian Somers case PHASE_NETWORK: 1295828db6dSBrian Somers ipcp_Setup(&bundle->ncp.ipcp); 130dd7e2610SBrian Somers fsm_Up(&bundle->ncp.ipcp.fsm); 131dd7e2610SBrian Somers fsm_Open(&bundle->ncp.ipcp.fsm); 132673903ecSBrian Somers bundle->phase = new; 1330f2f3eb3SBrian Somers log_DisplayPrompts(); 134673903ecSBrian Somers break; 135455aabc3SBrian Somers 136455aabc3SBrian Somers case PHASE_TERMINATE: 137455aabc3SBrian Somers bundle->phase = new; 138673903ecSBrian Somers mp_Down(&bundle->ncp.mp); 1390f2f3eb3SBrian Somers log_DisplayPrompts(); 140455aabc3SBrian Somers break; 1417a6f8720SBrian Somers } 1427a6f8720SBrian Somers } 1437a6f8720SBrian Somers 1447a6f8720SBrian Somers static int 1457a6f8720SBrian Somers bundle_CleanInterface(const struct bundle *bundle) 1467a6f8720SBrian Somers { 1477a6f8720SBrian Somers int s; 1487a6f8720SBrian Somers struct ifreq ifrq; 1497a6f8720SBrian Somers struct ifaliasreq ifra; 1507a6f8720SBrian Somers 1517a6f8720SBrian Somers s = ID0socket(AF_INET, SOCK_DGRAM, 0); 1527a6f8720SBrian Somers if (s < 0) { 153dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_CleanInterface: socket(): %s\n", 1547a6f8720SBrian Somers strerror(errno)); 1557a6f8720SBrian Somers return (-1); 1567a6f8720SBrian Somers } 157faefde08SBrian Somers strncpy(ifrq.ifr_name, bundle->ifp.Name, sizeof ifrq.ifr_name - 1); 1587a6f8720SBrian Somers ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 1597a6f8720SBrian Somers while (ID0ioctl(s, SIOCGIFADDR, &ifrq) == 0) { 1607a6f8720SBrian Somers memset(&ifra.ifra_mask, '\0', sizeof ifra.ifra_mask); 161faefde08SBrian Somers strncpy(ifra.ifra_name, bundle->ifp.Name, sizeof ifra.ifra_name - 1); 1627a6f8720SBrian Somers ifra.ifra_name[sizeof ifra.ifra_name - 1] = '\0'; 1637a6f8720SBrian Somers ifra.ifra_addr = ifrq.ifr_addr; 1647a6f8720SBrian Somers if (ID0ioctl(s, SIOCGIFDSTADDR, &ifrq) < 0) { 1657a6f8720SBrian Somers if (ifra.ifra_addr.sa_family == AF_INET) 166dd7e2610SBrian Somers log_Printf(LogERROR, 1677a6f8720SBrian Somers "bundle_CleanInterface: Can't get dst for %s on %s !\n", 1687a6f8720SBrian Somers inet_ntoa(((struct sockaddr_in *)&ifra.ifra_addr)->sin_addr), 169faefde08SBrian Somers bundle->ifp.Name); 1700f8037a9SBrian Somers close(s); 1717a6f8720SBrian Somers return 0; 1727a6f8720SBrian Somers } 1737a6f8720SBrian Somers ifra.ifra_broadaddr = ifrq.ifr_dstaddr; 1747a6f8720SBrian Somers if (ID0ioctl(s, SIOCDIFADDR, &ifra) < 0) { 1757a6f8720SBrian Somers if (ifra.ifra_addr.sa_family == AF_INET) 176dd7e2610SBrian Somers log_Printf(LogERROR, 1777a6f8720SBrian Somers "bundle_CleanInterface: Can't delete %s address on %s !\n", 1787a6f8720SBrian Somers inet_ntoa(((struct sockaddr_in *)&ifra.ifra_addr)->sin_addr), 179faefde08SBrian Somers bundle->ifp.Name); 1800f8037a9SBrian Somers close(s); 1817a6f8720SBrian Somers return 0; 1827a6f8720SBrian Somers } 1837a6f8720SBrian Somers } 1840f8037a9SBrian Somers close(s); 1857a6f8720SBrian Somers 1867a6f8720SBrian Somers return 1; 1877a6f8720SBrian Somers } 1887a6f8720SBrian Somers 1896d666775SBrian Somers static void 1906d666775SBrian Somers bundle_LayerStart(void *v, struct fsm *fp) 1917a6f8720SBrian Somers { 1923006ec67SBrian Somers /* The given FSM is about to start up ! */ 1937a6f8720SBrian Somers } 1947a6f8720SBrian Somers 1955cf4388bSBrian Somers 1965cf4388bSBrian Somers static void 1975cf4388bSBrian Somers bundle_Notify(struct bundle *bundle, char c) 1985cf4388bSBrian Somers { 1995cf4388bSBrian Somers if (bundle->notify.fd != -1) { 2005cf4388bSBrian Somers if (write(bundle->notify.fd, &c, 1) == 1) 201dd7e2610SBrian Somers log_Printf(LogPHASE, "Parent notified of success.\n"); 2025cf4388bSBrian Somers else 203dd7e2610SBrian Somers log_Printf(LogPHASE, "Failed to notify parent of success.\n"); 2045cf4388bSBrian Somers close(bundle->notify.fd); 2055cf4388bSBrian Somers bundle->notify.fd = -1; 2065cf4388bSBrian Somers } 2075cf4388bSBrian Somers } 2083b0f8d2eSBrian Somers 2096d666775SBrian Somers static void 21004eaa58cSBrian Somers bundle_AutoLoadTimeout(void *v) 21104eaa58cSBrian Somers { 21204eaa58cSBrian Somers struct bundle *bundle = (struct bundle *)v; 21304eaa58cSBrian Somers 21404eaa58cSBrian Somers if (bundle->autoload.comingup) { 21504eaa58cSBrian Somers log_Printf(LogPHASE, "autoload: Another link is required\n"); 21604eaa58cSBrian Somers /* bundle_Open() stops the timer */ 21704eaa58cSBrian Somers bundle_Open(bundle, NULL, PHYS_DEMAND); 21804eaa58cSBrian Somers } else { 21904eaa58cSBrian Somers struct datalink *dl, *last; 22004eaa58cSBrian Somers 22104eaa58cSBrian Somers timer_Stop(&bundle->autoload.timer); 22204eaa58cSBrian Somers for (last = NULL, dl = bundle->links; dl; dl = dl->next) 22304eaa58cSBrian Somers if (dl->physical->type == PHYS_DEMAND && dl->state == DATALINK_OPEN) 22404eaa58cSBrian Somers last = dl; 22504eaa58cSBrian Somers 22604eaa58cSBrian Somers if (last) 22704eaa58cSBrian Somers datalink_Close(last, 1); 22804eaa58cSBrian Somers } 22904eaa58cSBrian Somers } 23004eaa58cSBrian Somers 23104eaa58cSBrian Somers static void 23204eaa58cSBrian Somers bundle_StartAutoLoadTimer(struct bundle *bundle, int up) 23304eaa58cSBrian Somers { 23404eaa58cSBrian Somers struct datalink *dl; 23504eaa58cSBrian Somers 23604eaa58cSBrian Somers timer_Stop(&bundle->autoload.timer); 23704eaa58cSBrian Somers 23804eaa58cSBrian Somers if (bundle->CleaningUp || bundle->phase != PHASE_NETWORK) { 23904eaa58cSBrian Somers dl = NULL; 24004eaa58cSBrian Somers bundle->autoload.running = 0; 24104eaa58cSBrian Somers } else if (up) { 24204eaa58cSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 24304eaa58cSBrian Somers if (dl->state == DATALINK_CLOSED && dl->physical->type == PHYS_DEMAND) { 24404eaa58cSBrian Somers if (bundle->cfg.autoload.max.timeout) { 24504eaa58cSBrian Somers bundle->autoload.timer.func = bundle_AutoLoadTimeout; 24604eaa58cSBrian Somers bundle->autoload.timer.name = "autoload up"; 24704eaa58cSBrian Somers bundle->autoload.timer.load = 24804eaa58cSBrian Somers bundle->cfg.autoload.max.timeout * SECTICKS; 24904eaa58cSBrian Somers bundle->autoload.timer.arg = bundle; 25004eaa58cSBrian Somers timer_Start(&bundle->autoload.timer); 25104eaa58cSBrian Somers bundle->autoload.done = time(NULL) + bundle->cfg.autoload.max.timeout; 25204eaa58cSBrian Somers } else 25304eaa58cSBrian Somers bundle_AutoLoadTimeout(bundle); 25404eaa58cSBrian Somers break; 25504eaa58cSBrian Somers } 25604eaa58cSBrian Somers bundle->autoload.running = (dl || bundle->cfg.autoload.min.timeout) ? 1 : 0; 25704eaa58cSBrian Somers } else { 25804eaa58cSBrian Somers int nlinks; 25904eaa58cSBrian Somers struct datalink *adl; 26004eaa58cSBrian Somers 26104eaa58cSBrian Somers for (nlinks = 0, adl = NULL, dl = bundle->links; dl; dl = dl->next) 26204eaa58cSBrian Somers if (dl->state == DATALINK_OPEN) { 26304eaa58cSBrian Somers if (dl->physical->type == PHYS_DEMAND) 26404eaa58cSBrian Somers adl = dl; 26504eaa58cSBrian Somers if (++nlinks > 1 && adl) { 26604eaa58cSBrian Somers if (bundle->cfg.autoload.min.timeout) { 26704eaa58cSBrian Somers bundle->autoload.timer.func = bundle_AutoLoadTimeout; 26804eaa58cSBrian Somers bundle->autoload.timer.name = "autoload down"; 26904eaa58cSBrian Somers bundle->autoload.timer.load = 27004eaa58cSBrian Somers bundle->cfg.autoload.min.timeout * SECTICKS; 27104eaa58cSBrian Somers bundle->autoload.timer.arg = bundle; 27204eaa58cSBrian Somers timer_Start(&bundle->autoload.timer); 27304eaa58cSBrian Somers bundle->autoload.done = 27404eaa58cSBrian Somers time(NULL) + bundle->cfg.autoload.min.timeout; 27504eaa58cSBrian Somers } 27604eaa58cSBrian Somers break; 27704eaa58cSBrian Somers } 27804eaa58cSBrian Somers } 27904eaa58cSBrian Somers 28004eaa58cSBrian Somers bundle->autoload.running = 1; 28104eaa58cSBrian Somers } 28204eaa58cSBrian Somers 28304eaa58cSBrian Somers bundle->autoload.comingup = up ? 1 : 0; 28404eaa58cSBrian Somers } 28504eaa58cSBrian Somers 28604eaa58cSBrian Somers static void 28704eaa58cSBrian Somers bundle_StopAutoLoadTimer(struct bundle *bundle) 28804eaa58cSBrian Somers { 28904eaa58cSBrian Somers timer_Stop(&bundle->autoload.timer); 29004eaa58cSBrian Somers bundle->autoload.done = 0; 29104eaa58cSBrian Somers } 29204eaa58cSBrian Somers 29304eaa58cSBrian Somers static int 29404eaa58cSBrian Somers bundle_RemainingAutoLoadTime(struct bundle *bundle) 29504eaa58cSBrian Somers { 29604eaa58cSBrian Somers if (bundle->autoload.done) 29704eaa58cSBrian Somers return bundle->autoload.done - time(NULL); 29804eaa58cSBrian Somers return -1; 29904eaa58cSBrian Somers } 30004eaa58cSBrian Somers 30104eaa58cSBrian Somers 30204eaa58cSBrian Somers static void 3036f384573SBrian Somers bundle_LayerUp(void *v, struct fsm *fp) 3047a6f8720SBrian Somers { 3053006ec67SBrian Somers /* 3063006ec67SBrian Somers * The given fsm is now up 30749052c95SBrian Somers * If it's an LCP set our mtu (if we're multilink, add up the link 30804eaa58cSBrian Somers * speeds and set the MRRU) and start our autoload timer. 309565e35e5SBrian Somers * If it's an NCP, tell our -background parent to go away. 3103b0f8d2eSBrian Somers * If it's the first NCP, start the idle timer. 3113006ec67SBrian Somers */ 3126f384573SBrian Somers struct bundle *bundle = (struct bundle *)v; 3136d666775SBrian Somers 3145563ebdeSBrian Somers if (fp->proto == PROTO_LCP) { 3153b0f8d2eSBrian Somers if (bundle->ncp.mp.active) { 3163b0f8d2eSBrian Somers struct datalink *dl; 3175563ebdeSBrian Somers 318faefde08SBrian Somers bundle->ifp.Speed = 0; 319faefde08SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 320eeab6bf5SBrian Somers if (dl->state == DATALINK_OPEN) 321faefde08SBrian Somers bundle->ifp.Speed += modem_Speed(dl->physical); 322faefde08SBrian Somers tun_configure(bundle, bundle->ncp.mp.peer_mrru); 32304eaa58cSBrian Somers bundle->autoload.running = 1; 324faefde08SBrian Somers } else { 325faefde08SBrian Somers bundle->ifp.Speed = modem_Speed(link2physical(fp->link)); 326faefde08SBrian Somers tun_configure(bundle, fsm2lcp(fp)->his_mru); 327faefde08SBrian Somers } 3283b0f8d2eSBrian Somers } else if (fp->proto == PROTO_IPCP) { 329ab886ad0SBrian Somers bundle_StartIdleTimer(bundle); 3305cf4388bSBrian Somers bundle_Notify(bundle, EX_NORMAL); 3317a6f8720SBrian Somers } 332ab886ad0SBrian Somers } 3337a6f8720SBrian Somers 3346d666775SBrian Somers static void 3356d666775SBrian Somers bundle_LayerDown(void *v, struct fsm *fp) 3366d666775SBrian Somers { 3376d666775SBrian Somers /* 3386d666775SBrian Somers * The given FSM has been told to come down. 339ab886ad0SBrian Somers * If it's our last NCP, stop the idle timer. 34086b1f0d7SBrian Somers * If it's an LCP and we're in multilink mode, adjust our tun 34186b1f0d7SBrian Somers * speed and make sure our minimum sequence number is adjusted. 3426d666775SBrian Somers */ 343ab886ad0SBrian Somers 344ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v; 345ab886ad0SBrian Somers 346078c562eSBrian Somers if (fp->proto == PROTO_IPCP) 347ab886ad0SBrian Somers bundle_StopIdleTimer(bundle); 348078c562eSBrian Somers else if (fp->proto == PROTO_LCP && bundle->ncp.mp.active) { 3493b0f8d2eSBrian Somers struct datalink *dl; 35086b1f0d7SBrian Somers struct datalink *lost; 3513b0f8d2eSBrian Somers 352faefde08SBrian Somers bundle->ifp.Speed = 0; 35386b1f0d7SBrian Somers lost = NULL; 354faefde08SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 35586b1f0d7SBrian Somers if (fp == &dl->physical->link.lcp.fsm) 35686b1f0d7SBrian Somers lost = dl; 35786b1f0d7SBrian Somers else if (dl->state == DATALINK_OPEN) 358faefde08SBrian Somers bundle->ifp.Speed += modem_Speed(dl->physical); 35986b1f0d7SBrian Somers 360faefde08SBrian Somers if (bundle->ifp.Speed) 361faefde08SBrian Somers /* Don't configure down to a speed of 0 */ 362faefde08SBrian Somers tun_configure(bundle, bundle->ncp.mp.link.lcp.his_mru); 36386b1f0d7SBrian Somers 36486b1f0d7SBrian Somers if (lost) 36586b1f0d7SBrian Somers mp_LinkLost(&bundle->ncp.mp, lost); 36686b1f0d7SBrian Somers else 36786b1f0d7SBrian Somers log_Printf(LogERROR, "Oops, lost an unrecognised datalink (%s) !\n", 36886b1f0d7SBrian Somers fp->link->name); 3693b0f8d2eSBrian Somers } 3706d666775SBrian Somers } 3716d666775SBrian Somers 3726d666775SBrian Somers static void 3736d666775SBrian Somers bundle_LayerFinish(void *v, struct fsm *fp) 3746d666775SBrian Somers { 3756d666775SBrian Somers /* The given fsm is now down (fp cannot be NULL) 3766d666775SBrian Somers * 377dd7e2610SBrian Somers * If it's the last LCP, fsm_Down all NCPs 378dd7e2610SBrian Somers * If it's the last NCP, fsm_Close all LCPs 3796d666775SBrian Somers */ 3806d666775SBrian Somers 3816d666775SBrian Somers struct bundle *bundle = (struct bundle *)v; 3826d666775SBrian Somers struct datalink *dl; 3836d666775SBrian Somers 3843b0f8d2eSBrian Somers if (fp->proto == PROTO_IPCP) { 38526afeaa2SBrian Somers if (bundle_Phase(bundle) != PHASE_DEAD) 38625092092SBrian Somers bundle_NewPhase(bundle, PHASE_TERMINATE); 3876d666775SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 3883b0f8d2eSBrian Somers datalink_Close(dl, 0); 389dd7e2610SBrian Somers fsm_Down(fp); 390dd7e2610SBrian Somers fsm_Close(fp); 3913b0f8d2eSBrian Somers } else if (fp->proto == PROTO_LCP) { 3923b0f8d2eSBrian Somers int others_active; 393a611cad6SBrian Somers 3943b0f8d2eSBrian Somers others_active = 0; 3953b0f8d2eSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 3963b0f8d2eSBrian Somers if (fp != &dl->physical->link.lcp.fsm && 3973b0f8d2eSBrian Somers dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 3983b0f8d2eSBrian Somers others_active++; 3993b0f8d2eSBrian Somers 4003b0f8d2eSBrian Somers if (!others_active) { 401dd7e2610SBrian Somers fsm_Down(&bundle->ncp.ipcp.fsm); 402dd7e2610SBrian Somers fsm_Close(&bundle->ncp.ipcp.fsm); /* ST_INITIAL please */ 4036d666775SBrian Somers } 4043b0f8d2eSBrian Somers } 4053b0f8d2eSBrian Somers } 4066d666775SBrian Somers 4077a6f8720SBrian Somers int 4087a6f8720SBrian Somers bundle_LinkIsUp(const struct bundle *bundle) 4097a6f8720SBrian Somers { 4105828db6dSBrian Somers return bundle->ncp.ipcp.fsm.state == ST_OPENED; 4117a6f8720SBrian Somers } 4127a6f8720SBrian Somers 4137a6f8720SBrian Somers void 4143006ec67SBrian Somers bundle_Close(struct bundle *bundle, const char *name, int staydown) 4157a6f8720SBrian Somers { 416455aabc3SBrian Somers /* 4173006ec67SBrian Somers * Please close the given datalink. 418dd7e2610SBrian Somers * If name == NULL or name is the last datalink, fsm_Close all NCPs 4196f384573SBrian Somers * (except our MP) 4203b0f8d2eSBrian Somers * If it isn't the last datalink, just Close that datalink. 421455aabc3SBrian Somers */ 4227a6f8720SBrian Somers 4233b0f8d2eSBrian Somers struct datalink *dl, *this_dl; 4243b0f8d2eSBrian Somers int others_active; 4253006ec67SBrian Somers 4263b0f8d2eSBrian Somers if (bundle->phase == PHASE_TERMINATE || bundle->phase == PHASE_DEAD) 4273b0f8d2eSBrian Somers return; 4283b0f8d2eSBrian Somers 4293b0f8d2eSBrian Somers others_active = 0; 4303b0f8d2eSBrian Somers this_dl = NULL; 4313b0f8d2eSBrian Somers 4323b0f8d2eSBrian Somers for (dl = bundle->links; dl; dl = dl->next) { 4333b0f8d2eSBrian Somers if (name && !strcasecmp(name, dl->name)) 4343b0f8d2eSBrian Somers this_dl = dl; 4353b0f8d2eSBrian Somers if (name == NULL || this_dl == dl) { 436d345321bSBrian Somers if (staydown) 4373006ec67SBrian Somers datalink_StayDown(dl); 4383b0f8d2eSBrian Somers } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 4393b0f8d2eSBrian Somers others_active++; 4403b0f8d2eSBrian Somers } 4413b0f8d2eSBrian Somers 4423b0f8d2eSBrian Somers if (name && this_dl == NULL) { 443dd7e2610SBrian Somers log_Printf(LogWARN, "%s: Invalid datalink name\n", name); 4443b0f8d2eSBrian Somers return; 4453b0f8d2eSBrian Somers } 4463b0f8d2eSBrian Somers 4473b0f8d2eSBrian Somers if (!others_active) { 44804eaa58cSBrian Somers bundle_StopIdleTimer(bundle); 44904eaa58cSBrian Somers bundle_StopAutoLoadTimer(bundle); 4503b0f8d2eSBrian Somers if (bundle->ncp.ipcp.fsm.state > ST_CLOSED || 4513b0f8d2eSBrian Somers bundle->ncp.ipcp.fsm.state == ST_STARTING) 452dd7e2610SBrian Somers fsm_Close(&bundle->ncp.ipcp.fsm); 4533b0f8d2eSBrian Somers else { 4545828db6dSBrian Somers if (bundle->ncp.ipcp.fsm.state > ST_INITIAL) { 455dd7e2610SBrian Somers fsm_Close(&bundle->ncp.ipcp.fsm); 456dd7e2610SBrian Somers fsm_Down(&bundle->ncp.ipcp.fsm); 457d2fd8d77SBrian Somers } 458d345321bSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 459d345321bSBrian Somers datalink_Close(dl, staydown); 4607a6f8720SBrian Somers } 4613b0f8d2eSBrian Somers } else if (this_dl && this_dl->state != DATALINK_CLOSED && 4623b0f8d2eSBrian Somers this_dl->state != DATALINK_HANGUP) 4633b0f8d2eSBrian Somers datalink_Close(this_dl, staydown); 464d2fd8d77SBrian Somers } 4657a6f8720SBrian Somers 4661bc9b5baSBrian Somers void 4671bc9b5baSBrian Somers bundle_Down(struct bundle *bundle) 4681bc9b5baSBrian Somers { 4691bc9b5baSBrian Somers struct datalink *dl; 4701bc9b5baSBrian Somers 4711bc9b5baSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 4721bc9b5baSBrian Somers datalink_Down(dl, 1); 4731bc9b5baSBrian Somers } 4741bc9b5baSBrian Somers 4752f786681SBrian Somers static int 4762f786681SBrian Somers bundle_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n) 4772f786681SBrian Somers { 4782f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d); 4792f786681SBrian Somers struct datalink *dl; 48004eaa58cSBrian Somers int result, want, queued, nlinks; 4812f786681SBrian Somers 4822f786681SBrian Somers result = 0; 4832f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 4842f786681SBrian Somers result += descriptor_UpdateSet(&dl->desc, r, w, e, n); 4852f786681SBrian Somers 486078c562eSBrian Somers /* If there are aren't many packets queued, look for some more. */ 48704eaa58cSBrian Somers for (nlinks = 0, dl = bundle->links; dl; dl = dl->next) 48804eaa58cSBrian Somers nlinks++; 48904eaa58cSBrian Somers 49004eaa58cSBrian Somers if (nlinks) { 49104eaa58cSBrian Somers queued = r ? bundle_FillQueues(bundle) : ip_QueueLen(); 49204eaa58cSBrian Somers if (bundle->autoload.running) { 49304eaa58cSBrian Somers if (queued < bundle->cfg.autoload.max.packets) { 49404eaa58cSBrian Somers if (queued > bundle->cfg.autoload.min.packets) 49504eaa58cSBrian Somers bundle_StopAutoLoadTimer(bundle); 49604eaa58cSBrian Somers else if (bundle->autoload.timer.state != TIMER_RUNNING || 49704eaa58cSBrian Somers bundle->autoload.comingup) 49804eaa58cSBrian Somers bundle_StartAutoLoadTimer(bundle, 0); 49904eaa58cSBrian Somers } else if (bundle->autoload.timer.state != TIMER_RUNNING || 50004eaa58cSBrian Somers !bundle->autoload.comingup) 50104eaa58cSBrian Somers bundle_StartAutoLoadTimer(bundle, 1); 50204eaa58cSBrian Somers } 50304eaa58cSBrian Somers 5040f2f3eb3SBrian Somers if (r && 5050f2f3eb3SBrian Somers (bundle->phase == PHASE_NETWORK || bundle->phys_type & PHYS_DEMAND)) { 50604eaa58cSBrian Somers /* enough surplus so that we can tell if we're getting swamped */ 50704eaa58cSBrian Somers want = bundle->cfg.autoload.max.packets + nlinks * 2; 50804eaa58cSBrian Somers /* but at least 20 packets ! */ 50904eaa58cSBrian Somers if (want < 20) 51004eaa58cSBrian Somers want = 20; 51104eaa58cSBrian Somers if (queued < want) { 51204eaa58cSBrian Somers /* Not enough - select() for more */ 51304eaa58cSBrian Somers FD_SET(bundle->dev.fd, r); 514faefde08SBrian Somers if (*n < bundle->dev.fd + 1) 515faefde08SBrian Somers *n = bundle->dev.fd + 1; 51624989c68SBrian Somers log_Printf(LogTIMER, "tun: fdset(r) %d\n", bundle->dev.fd); 517078c562eSBrian Somers result++; 518078c562eSBrian Somers } 51904eaa58cSBrian Somers } 52004eaa58cSBrian Somers } 521078c562eSBrian Somers 522ea722969SBrian Somers /* 523ea722969SBrian Somers * This *MUST* be called after the datalink UpdateSet()s as it 52404eaa58cSBrian Somers * might be ``holding'' one of the datalinks (death-row) and 52504eaa58cSBrian Somers * wants to be able to de-select() it from the descriptor set. 526ea722969SBrian Somers */ 5270f2f3eb3SBrian Somers result += descriptor_UpdateSet(&bundle->ncp.mp.server.desc, r, w, e, n); 528ea722969SBrian Somers 5292f786681SBrian Somers return result; 5302f786681SBrian Somers } 5312f786681SBrian Somers 5322f786681SBrian Somers static int 5332f786681SBrian Somers bundle_IsSet(struct descriptor *d, const fd_set *fdset) 5342f786681SBrian Somers { 5352f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d); 5362f786681SBrian Somers struct datalink *dl; 5372f786681SBrian Somers 5382f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 5392f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 5402f786681SBrian Somers return 1; 5412f786681SBrian Somers 542332b9de0SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 543332b9de0SBrian Somers return 1; 544332b9de0SBrian Somers 545faefde08SBrian Somers return FD_ISSET(bundle->dev.fd, fdset); 5462f786681SBrian Somers } 5472f786681SBrian Somers 5482f786681SBrian Somers static void 5492f786681SBrian Somers bundle_DescriptorRead(struct descriptor *d, struct bundle *bundle, 5502f786681SBrian Somers const fd_set *fdset) 5512f786681SBrian Somers { 5522f786681SBrian Somers struct datalink *dl; 5532f786681SBrian Somers 554ea722969SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 555ea722969SBrian Somers descriptor_Read(&bundle->ncp.mp.server.desc, bundle, fdset); 556ea722969SBrian Somers 5572f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 5582f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 5592f786681SBrian Somers descriptor_Read(&dl->desc, bundle, fdset); 560b6217683SBrian Somers 561faefde08SBrian Somers if (FD_ISSET(bundle->dev.fd, fdset)) { 562078c562eSBrian Somers struct tun_data tun; 563078c562eSBrian Somers int n, pri; 564078c562eSBrian Somers 565078c562eSBrian Somers /* something to read from tun */ 566faefde08SBrian Somers n = read(bundle->dev.fd, &tun, sizeof tun); 567078c562eSBrian Somers if (n < 0) { 568078c562eSBrian Somers log_Printf(LogERROR, "read from tun: %s\n", strerror(errno)); 569078c562eSBrian Somers return; 570078c562eSBrian Somers } 571078c562eSBrian Somers n -= sizeof tun - sizeof tun.data; 572078c562eSBrian Somers if (n <= 0) { 573078c562eSBrian Somers log_Printf(LogERROR, "read from tun: Only %d bytes read\n", n); 574078c562eSBrian Somers return; 575078c562eSBrian Somers } 576078c562eSBrian Somers if (!tun_check_header(tun, AF_INET)) 577078c562eSBrian Somers return; 578078c562eSBrian Somers 579078c562eSBrian Somers if (((struct ip *)tun.data)->ip_dst.s_addr == 580078c562eSBrian Somers bundle->ncp.ipcp.my_ip.s_addr) { 581078c562eSBrian Somers /* we've been asked to send something addressed *to* us :( */ 582078c562eSBrian Somers if (Enabled(bundle, OPT_LOOPBACK)) { 583078c562eSBrian Somers pri = PacketCheck(bundle, tun.data, n, &bundle->filter.in); 584078c562eSBrian Somers if (pri >= 0) { 585078c562eSBrian Somers struct mbuf *bp; 586078c562eSBrian Somers 587078c562eSBrian Somers #ifndef NOALIAS 588078c562eSBrian Somers if (alias_IsEnabled()) { 589078c562eSBrian Somers (*PacketAlias.In)(tun.data, sizeof tun.data); 590078c562eSBrian Somers n = ntohs(((struct ip *)tun.data)->ip_len); 591078c562eSBrian Somers } 592078c562eSBrian Somers #endif 593078c562eSBrian Somers bp = mbuf_Alloc(n, MB_IPIN); 594078c562eSBrian Somers memcpy(MBUF_CTOP(bp), tun.data, n); 595078c562eSBrian Somers ip_Input(bundle, bp); 596078c562eSBrian Somers log_Printf(LogDEBUG, "Looped back packet addressed to myself\n"); 597078c562eSBrian Somers } 598078c562eSBrian Somers return; 599078c562eSBrian Somers } else 600078c562eSBrian Somers log_Printf(LogDEBUG, "Oops - forwarding packet addressed to myself\n"); 601078c562eSBrian Somers } 602078c562eSBrian Somers 603078c562eSBrian Somers /* 604078c562eSBrian Somers * Process on-demand dialup. Output packets are queued within tunnel 605078c562eSBrian Somers * device until IPCP is opened. 606078c562eSBrian Somers */ 607078c562eSBrian Somers 608078c562eSBrian Somers if (bundle_Phase(bundle) == PHASE_DEAD) { 609078c562eSBrian Somers /* 610078c562eSBrian Somers * Note, we must be in AUTO mode :-/ otherwise our interface should 611078c562eSBrian Somers * *not* be UP and we can't receive data 612078c562eSBrian Somers */ 613078c562eSBrian Somers if ((pri = PacketCheck(bundle, tun.data, n, &bundle->filter.dial)) >= 0) 614078c562eSBrian Somers bundle_Open(bundle, NULL, PHYS_DEMAND); 615078c562eSBrian Somers else 616078c562eSBrian Somers /* 617078c562eSBrian Somers * Drop the packet. If we were to queue it, we'd just end up with 618078c562eSBrian Somers * a pile of timed-out data in our output queue by the time we get 619078c562eSBrian Somers * around to actually dialing. We'd also prematurely reach the 620078c562eSBrian Somers * threshold at which we stop select()ing to read() the tun 621078c562eSBrian Somers * device - breaking auto-dial. 622078c562eSBrian Somers */ 623078c562eSBrian Somers return; 624078c562eSBrian Somers } 625078c562eSBrian Somers 626078c562eSBrian Somers pri = PacketCheck(bundle, tun.data, n, &bundle->filter.out); 627078c562eSBrian Somers if (pri >= 0) { 628078c562eSBrian Somers #ifndef NOALIAS 629078c562eSBrian Somers if (alias_IsEnabled()) { 630078c562eSBrian Somers (*PacketAlias.Out)(tun.data, sizeof tun.data); 631078c562eSBrian Somers n = ntohs(((struct ip *)tun.data)->ip_len); 632078c562eSBrian Somers } 633078c562eSBrian Somers #endif 634078c562eSBrian Somers ip_Enqueue(pri, tun.data, n); 635078c562eSBrian Somers } 636078c562eSBrian Somers } 6372f786681SBrian Somers } 6382f786681SBrian Somers 6392f786681SBrian Somers static void 6402f786681SBrian Somers bundle_DescriptorWrite(struct descriptor *d, struct bundle *bundle, 6412f786681SBrian Somers const fd_set *fdset) 6422f786681SBrian Somers { 6432f786681SBrian Somers struct datalink *dl; 6442f786681SBrian Somers 645ea722969SBrian Somers /* This is not actually necessary as struct mpserver doesn't Write() */ 646ea722969SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 647ea722969SBrian Somers descriptor_Write(&bundle->ncp.mp.server.desc, bundle, fdset); 648ea722969SBrian Somers 6492f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 6502f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 6512f786681SBrian Somers descriptor_Write(&dl->desc, bundle, fdset); 6522f786681SBrian Somers } 6532f786681SBrian Somers 6547a6f8720SBrian Somers 6557a6f8720SBrian Somers struct bundle * 6560f2f3eb3SBrian Somers bundle_Create(const char *prefix, int type) 6577a6f8720SBrian Somers { 6587a6f8720SBrian Somers int s, enoentcount, err; 6597a6f8720SBrian Somers struct ifreq ifrq; 6607a6f8720SBrian Somers static struct bundle bundle; /* there can be only one */ 6617a6f8720SBrian Somers 662faefde08SBrian Somers if (bundle.ifp.Name != NULL) { /* Already allocated ! */ 663dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_Create: There's only one BUNDLE !\n"); 6647a6f8720SBrian Somers return NULL; 6657a6f8720SBrian Somers } 6667a6f8720SBrian Somers 6677a6f8720SBrian Somers err = ENOENT; 6687a6f8720SBrian Somers enoentcount = 0; 669107d62e7SBrian Somers for (bundle.unit = 0; ; bundle.unit++) { 670faefde08SBrian Somers snprintf(bundle.dev.Name, sizeof bundle.dev.Name, "%s%d", 671faefde08SBrian Somers prefix, bundle.unit); 672faefde08SBrian Somers bundle.dev.fd = ID0open(bundle.dev.Name, O_RDWR); 673faefde08SBrian Somers if (bundle.dev.fd >= 0) 6747a6f8720SBrian Somers break; 675107d62e7SBrian Somers else if (errno == ENXIO) { 6767a6f8720SBrian Somers err = errno; 677107d62e7SBrian Somers break; 6787a6f8720SBrian Somers } else if (errno == ENOENT) { 6797a6f8720SBrian Somers if (++enoentcount > 2) 680107d62e7SBrian Somers break; 6817a6f8720SBrian Somers } else 6827a6f8720SBrian Somers err = errno; 6837a6f8720SBrian Somers } 6847a6f8720SBrian Somers 685faefde08SBrian Somers if (bundle.dev.fd < 0) { 686dd7e2610SBrian Somers log_Printf(LogWARN, "No available tunnel devices found (%s).\n", 68785b542cfSBrian Somers strerror(err)); 6887a6f8720SBrian Somers return NULL; 6897a6f8720SBrian Somers } 6907a6f8720SBrian Somers 691dd7e2610SBrian Somers log_SetTun(bundle.unit); 6927a6f8720SBrian Somers 6937a6f8720SBrian Somers s = socket(AF_INET, SOCK_DGRAM, 0); 6947a6f8720SBrian Somers if (s < 0) { 695dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_Create: socket(): %s\n", strerror(errno)); 696faefde08SBrian Somers close(bundle.dev.fd); 6977a6f8720SBrian Somers return NULL; 6987a6f8720SBrian Somers } 6997a6f8720SBrian Somers 700faefde08SBrian Somers bundle.ifp.Name = strrchr(bundle.dev.Name, '/'); 701faefde08SBrian Somers if (bundle.ifp.Name == NULL) 702faefde08SBrian Somers bundle.ifp.Name = bundle.dev.Name; 7037a6f8720SBrian Somers else 704faefde08SBrian Somers bundle.ifp.Name++; 7057a6f8720SBrian Somers 7067a6f8720SBrian Somers /* 7077a6f8720SBrian Somers * Now, bring up the interface. 7087a6f8720SBrian Somers */ 7097a6f8720SBrian Somers memset(&ifrq, '\0', sizeof ifrq); 710faefde08SBrian Somers strncpy(ifrq.ifr_name, bundle.ifp.Name, sizeof ifrq.ifr_name - 1); 7117a6f8720SBrian Somers ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 7127a6f8720SBrian Somers if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) { 713dd7e2610SBrian Somers log_Printf(LogERROR, "OpenTunnel: ioctl(SIOCGIFFLAGS): %s\n", 7147a6f8720SBrian Somers strerror(errno)); 7157a6f8720SBrian Somers close(s); 716faefde08SBrian Somers close(bundle.dev.fd); 717faefde08SBrian Somers bundle.ifp.Name = NULL; 7187a6f8720SBrian Somers return NULL; 7197a6f8720SBrian Somers } 7207a6f8720SBrian Somers ifrq.ifr_flags |= IFF_UP; 7217a6f8720SBrian Somers if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) { 722dd7e2610SBrian Somers log_Printf(LogERROR, "OpenTunnel: ioctl(SIOCSIFFLAGS): %s\n", 7237a6f8720SBrian Somers strerror(errno)); 7247a6f8720SBrian Somers close(s); 725faefde08SBrian Somers close(bundle.dev.fd); 726faefde08SBrian Somers bundle.ifp.Name = NULL; 7277a6f8720SBrian Somers return NULL; 7287a6f8720SBrian Somers } 7297a6f8720SBrian Somers 7307a6f8720SBrian Somers close(s); 7317a6f8720SBrian Somers 732faefde08SBrian Somers if ((bundle.ifp.Index = GetIfIndex(bundle.ifp.Name)) < 0) { 733faefde08SBrian Somers log_Printf(LogERROR, "OpenTunnel: Can't find interface index.\n"); 734faefde08SBrian Somers close(bundle.dev.fd); 735faefde08SBrian Somers bundle.ifp.Name = NULL; 7367a6f8720SBrian Somers return NULL; 7377a6f8720SBrian Somers } 738faefde08SBrian Somers log_Printf(LogPHASE, "Using interface: %s\n", bundle.ifp.Name); 7397a6f8720SBrian Somers 740faefde08SBrian Somers bundle.ifp.Speed = 0; 7417a6f8720SBrian Somers 742820de6ebSBrian Somers bundle.routing_seq = 0; 743a0cbd833SBrian Somers bundle.phase = PHASE_DEAD; 744a0cbd833SBrian Somers bundle.CleaningUp = 0; 7457a6f8720SBrian Somers 7466d666775SBrian Somers bundle.fsm.LayerStart = bundle_LayerStart; 7476f384573SBrian Somers bundle.fsm.LayerUp = bundle_LayerUp; 7486d666775SBrian Somers bundle.fsm.LayerDown = bundle_LayerDown; 7496d666775SBrian Somers bundle.fsm.LayerFinish = bundle_LayerFinish; 7506d666775SBrian Somers bundle.fsm.object = &bundle; 7517a6f8720SBrian Somers 752ab886ad0SBrian Somers bundle.cfg.idle_timeout = NCP_IDLE_TIMEOUT; 7531342caedSBrian Somers *bundle.cfg.auth.name = '\0'; 7541342caedSBrian Somers *bundle.cfg.auth.key = '\0'; 755610b185fSBrian Somers bundle.cfg.opt = OPT_SROUTES | OPT_IDCHECK | OPT_LOOPBACK | 756610b185fSBrian Somers OPT_THROUGHPUT | OPT_UTMP; 75749052c95SBrian Somers *bundle.cfg.label = '\0'; 75849052c95SBrian Somers bundle.cfg.mtu = DEF_MTU; 75904eaa58cSBrian Somers bundle.cfg.autoload.max.packets = 0; 76004eaa58cSBrian Somers bundle.cfg.autoload.max.timeout = 0; 76104eaa58cSBrian Somers bundle.cfg.autoload.min.packets = 0; 76204eaa58cSBrian Somers bundle.cfg.autoload.min.timeout = 0; 763565e35e5SBrian Somers bundle.phys_type = type; 764ab886ad0SBrian Somers 7656f384573SBrian Somers bundle.links = datalink_Create("deflink", &bundle, type); 7663006ec67SBrian Somers if (bundle.links == NULL) { 767dd7e2610SBrian Somers log_Printf(LogERROR, "Cannot create data link: %s\n", strerror(errno)); 768faefde08SBrian Somers close(bundle.dev.fd); 769faefde08SBrian Somers bundle.ifp.Name = NULL; 7702289f246SBrian Somers return NULL; 7712289f246SBrian Somers } 7722289f246SBrian Somers 7732f786681SBrian Somers bundle.desc.type = BUNDLE_DESCRIPTOR; 7742f786681SBrian Somers bundle.desc.UpdateSet = bundle_UpdateSet; 7752f786681SBrian Somers bundle.desc.IsSet = bundle_IsSet; 7762f786681SBrian Somers bundle.desc.Read = bundle_DescriptorRead; 7772f786681SBrian Somers bundle.desc.Write = bundle_DescriptorWrite; 7782f786681SBrian Somers 77949052c95SBrian Somers mp_Init(&bundle.ncp.mp, &bundle); 78049052c95SBrian Somers 78149052c95SBrian Somers /* Send over the first physical link by default */ 7825828db6dSBrian Somers ipcp_Init(&bundle.ncp.ipcp, &bundle, &bundle.links->physical->link, 7835828db6dSBrian Somers &bundle.fsm); 7846d666775SBrian Somers 7855ca5389aSBrian Somers memset(&bundle.filter, '\0', sizeof bundle.filter); 7865ca5389aSBrian Somers bundle.filter.in.fragok = bundle.filter.in.logok = 1; 7875ca5389aSBrian Somers bundle.filter.in.name = "IN"; 7885ca5389aSBrian Somers bundle.filter.out.fragok = bundle.filter.out.logok = 1; 7895ca5389aSBrian Somers bundle.filter.out.name = "OUT"; 7905ca5389aSBrian Somers bundle.filter.dial.name = "DIAL"; 7918390b576SBrian Somers bundle.filter.dial.logok = 1; 7925ca5389aSBrian Somers bundle.filter.alive.name = "ALIVE"; 7935ca5389aSBrian Somers bundle.filter.alive.logok = 1; 79493ee0ff2SBrian Somers memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer); 79593ee0ff2SBrian Somers bundle.idle.done = 0; 7965cf4388bSBrian Somers bundle.notify.fd = -1; 79704eaa58cSBrian Somers memset(&bundle.autoload.timer, '\0', sizeof bundle.autoload.timer); 79804eaa58cSBrian Somers bundle.autoload.done = 0; 79904eaa58cSBrian Somers bundle.autoload.running = 0; 80093ee0ff2SBrian Somers 8016d666775SBrian Somers /* Clean out any leftover crud */ 8026d666775SBrian Somers bundle_CleanInterface(&bundle); 8036d666775SBrian Somers 80485602e52SBrian Somers if (prompt) { 80585602e52SBrian Somers /* Retrospectively introduce ourselves to the prompt */ 80685602e52SBrian Somers prompt->bundle = &bundle; 80785602e52SBrian Somers bundle_RegisterDescriptor(&bundle, &prompt->desc); 80885602e52SBrian Somers } 80985602e52SBrian Somers 8107a6f8720SBrian Somers return &bundle; 8117a6f8720SBrian Somers } 8127a6f8720SBrian Somers 81368a0f0ccSBrian Somers static void 81468a0f0ccSBrian Somers bundle_DownInterface(struct bundle *bundle) 81568a0f0ccSBrian Somers { 81668a0f0ccSBrian Somers struct ifreq ifrq; 81768a0f0ccSBrian Somers int s; 81868a0f0ccSBrian Somers 819dd7e2610SBrian Somers route_IfDelete(bundle, 1); 82068a0f0ccSBrian Somers 82168a0f0ccSBrian Somers s = ID0socket(AF_INET, SOCK_DGRAM, 0); 82268a0f0ccSBrian Somers if (s < 0) { 823dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_DownInterface: socket: %s\n", strerror(errno)); 82468a0f0ccSBrian Somers return; 82568a0f0ccSBrian Somers } 82668a0f0ccSBrian Somers 82768a0f0ccSBrian Somers memset(&ifrq, '\0', sizeof ifrq); 828faefde08SBrian Somers strncpy(ifrq.ifr_name, bundle->ifp.Name, sizeof ifrq.ifr_name - 1); 82968a0f0ccSBrian Somers ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 83068a0f0ccSBrian Somers if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) { 831dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_DownInterface: ioctl(SIOCGIFFLAGS): %s\n", 83268a0f0ccSBrian Somers strerror(errno)); 83368a0f0ccSBrian Somers close(s); 83468a0f0ccSBrian Somers return; 83568a0f0ccSBrian Somers } 83668a0f0ccSBrian Somers ifrq.ifr_flags &= ~IFF_UP; 83768a0f0ccSBrian Somers if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) { 838dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_DownInterface: ioctl(SIOCSIFFLAGS): %s\n", 83968a0f0ccSBrian Somers strerror(errno)); 84068a0f0ccSBrian Somers close(s); 84168a0f0ccSBrian Somers return; 84268a0f0ccSBrian Somers } 84368a0f0ccSBrian Somers close(s); 84468a0f0ccSBrian Somers } 84568a0f0ccSBrian Somers 84668a0f0ccSBrian Somers void 84768a0f0ccSBrian Somers bundle_Destroy(struct bundle *bundle) 84868a0f0ccSBrian Somers { 8493006ec67SBrian Somers struct datalink *dl; 850b6217683SBrian Somers 851ea722969SBrian Somers /* 85204eaa58cSBrian Somers * Clean up the interface. We don't need to timer_Stop()s, mp_Down(), 853ea722969SBrian Somers * ipcp_CleanInterface() and bundle_DownInterface() unless we're getting 854ea722969SBrian Somers * out under exceptional conditions such as a descriptor exception. 855ea722969SBrian Somers */ 85604eaa58cSBrian Somers timer_Stop(&bundle->idle.timer); 85704eaa58cSBrian Somers timer_Stop(&bundle->autoload.timer); 85866f634b6SBrian Somers mp_Down(&bundle->ncp.mp); 859dd7e2610SBrian Somers ipcp_CleanInterface(&bundle->ncp.ipcp); 86068a0f0ccSBrian Somers bundle_DownInterface(bundle); 8613006ec67SBrian Somers 862ea722969SBrian Somers /* Again, these are all DATALINK_CLOSED unless we're abending */ 8633006ec67SBrian Somers dl = bundle->links; 8643006ec67SBrian Somers while (dl) 8653006ec67SBrian Somers dl = datalink_Destroy(dl); 8663006ec67SBrian Somers 867ea722969SBrian Somers /* In case we never made PHASE_NETWORK */ 8685cf4388bSBrian Somers bundle_Notify(bundle, EX_ERRDEAD); 869b6217683SBrian Somers 870faefde08SBrian Somers bundle->ifp.Name = NULL; 87168a0f0ccSBrian Somers } 87268a0f0ccSBrian Somers 8737a6f8720SBrian Somers struct rtmsg { 8747a6f8720SBrian Somers struct rt_msghdr m_rtm; 8757a6f8720SBrian Somers char m_space[64]; 8767a6f8720SBrian Somers }; 8777a6f8720SBrian Somers 878610b185fSBrian Somers int 879820de6ebSBrian Somers bundle_SetRoute(struct bundle *bundle, int cmd, struct in_addr dst, 8807a6f8720SBrian Somers struct in_addr gateway, struct in_addr mask, int bang) 8817a6f8720SBrian Somers { 8827a6f8720SBrian Somers struct rtmsg rtmes; 8837a6f8720SBrian Somers int s, nb, wb; 8847a6f8720SBrian Somers char *cp; 8857a6f8720SBrian Somers const char *cmdstr; 8867a6f8720SBrian Somers struct sockaddr_in rtdata; 887610b185fSBrian Somers int result = 1; 8887a6f8720SBrian Somers 8897a6f8720SBrian Somers if (bang) 8907a6f8720SBrian Somers cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!"); 8917a6f8720SBrian Somers else 8927a6f8720SBrian Somers cmdstr = (cmd == RTM_ADD ? "Add" : "Delete"); 8937a6f8720SBrian Somers s = ID0socket(PF_ROUTE, SOCK_RAW, 0); 8947a6f8720SBrian Somers if (s < 0) { 895dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_SetRoute: socket(): %s\n", strerror(errno)); 896610b185fSBrian Somers return result; 8977a6f8720SBrian Somers } 8987a6f8720SBrian Somers memset(&rtmes, '\0', sizeof rtmes); 8997a6f8720SBrian Somers rtmes.m_rtm.rtm_version = RTM_VERSION; 9007a6f8720SBrian Somers rtmes.m_rtm.rtm_type = cmd; 9017a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs = RTA_DST; 902820de6ebSBrian Somers rtmes.m_rtm.rtm_seq = ++bundle->routing_seq; 9037a6f8720SBrian Somers rtmes.m_rtm.rtm_pid = getpid(); 9047a6f8720SBrian Somers rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC; 9057a6f8720SBrian Somers 9067a6f8720SBrian Somers memset(&rtdata, '\0', sizeof rtdata); 90750e5c17dSBrian Somers rtdata.sin_len = sizeof rtdata; 9087a6f8720SBrian Somers rtdata.sin_family = AF_INET; 9097a6f8720SBrian Somers rtdata.sin_port = 0; 9107a6f8720SBrian Somers rtdata.sin_addr = dst; 9117a6f8720SBrian Somers 9127a6f8720SBrian Somers cp = rtmes.m_space; 91350e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 91450e5c17dSBrian Somers cp += rtdata.sin_len; 915e43ebac1SBrian Somers if (cmd == RTM_ADD) { 9167a6f8720SBrian Somers if (gateway.s_addr == INADDR_ANY) { 9177a6f8720SBrian Somers /* Add a route through the interface */ 9187a6f8720SBrian Somers struct sockaddr_dl dl; 9197a6f8720SBrian Somers const char *iname; 9207a6f8720SBrian Somers int ilen; 9217a6f8720SBrian Somers 922faefde08SBrian Somers iname = Index2Nam(bundle->ifp.Index); 9237a6f8720SBrian Somers ilen = strlen(iname); 9247a6f8720SBrian Somers dl.sdl_len = sizeof dl - sizeof dl.sdl_data + ilen; 9257a6f8720SBrian Somers dl.sdl_family = AF_LINK; 926faefde08SBrian Somers dl.sdl_index = bundle->ifp.Index; 9277a6f8720SBrian Somers dl.sdl_type = 0; 9287a6f8720SBrian Somers dl.sdl_nlen = ilen; 9297a6f8720SBrian Somers dl.sdl_alen = 0; 9307a6f8720SBrian Somers dl.sdl_slen = 0; 9317a6f8720SBrian Somers strncpy(dl.sdl_data, iname, sizeof dl.sdl_data); 9327a6f8720SBrian Somers memcpy(cp, &dl, dl.sdl_len); 9337a6f8720SBrian Somers cp += dl.sdl_len; 9347a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 9357a6f8720SBrian Somers } else { 9367a6f8720SBrian Somers rtdata.sin_addr = gateway; 93750e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 93850e5c17dSBrian Somers cp += rtdata.sin_len; 9397a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 9407a6f8720SBrian Somers } 941e43ebac1SBrian Somers } 9427a6f8720SBrian Somers 9437a6f8720SBrian Somers if (dst.s_addr == INADDR_ANY) 9447a6f8720SBrian Somers mask.s_addr = INADDR_ANY; 9457a6f8720SBrian Somers 9467a6f8720SBrian Somers if (cmd == RTM_ADD || dst.s_addr == INADDR_ANY) { 9477a6f8720SBrian Somers rtdata.sin_addr = mask; 94850e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 94950e5c17dSBrian Somers cp += rtdata.sin_len; 9507a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_NETMASK; 9517a6f8720SBrian Somers } 9527a6f8720SBrian Somers 9537a6f8720SBrian Somers nb = cp - (char *) &rtmes; 9547a6f8720SBrian Somers rtmes.m_rtm.rtm_msglen = nb; 9557a6f8720SBrian Somers wb = ID0write(s, &rtmes, nb); 9567a6f8720SBrian Somers if (wb < 0) { 957dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute failure:\n"); 958dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Cmd = %s\n", cmdstr); 959dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Dst = %s\n", inet_ntoa(dst)); 960dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Gateway = %s\n", inet_ntoa(gateway)); 961dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Mask = %s\n", inet_ntoa(mask)); 9627a6f8720SBrian Somers failed: 9637a6f8720SBrian Somers if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST || 964e43ebac1SBrian Somers (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) { 965610b185fSBrian Somers if (!bang) { 966dd7e2610SBrian Somers log_Printf(LogWARN, "Add route failed: %s already exists\n", 9677a6f8720SBrian Somers inet_ntoa(dst)); 968610b185fSBrian Somers result = 0; /* Don't add to our dynamic list */ 969610b185fSBrian Somers } else { 9707a6f8720SBrian Somers rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE; 9717a6f8720SBrian Somers if ((wb = ID0write(s, &rtmes, nb)) < 0) 9727a6f8720SBrian Somers goto failed; 9737a6f8720SBrian Somers } 974e43ebac1SBrian Somers } else if (cmd == RTM_DELETE && 9757a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == ESRCH || 9767a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) { 9777a6f8720SBrian Somers if (!bang) 978dd7e2610SBrian Somers log_Printf(LogWARN, "Del route failed: %s: Non-existent\n", 9797a6f8720SBrian Somers inet_ntoa(dst)); 9807a6f8720SBrian Somers } else if (rtmes.m_rtm.rtm_errno == 0) 981dd7e2610SBrian Somers log_Printf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr, 9827a6f8720SBrian Somers inet_ntoa(dst), strerror(errno)); 9837a6f8720SBrian Somers else 984dd7e2610SBrian Somers log_Printf(LogWARN, "%s route failed: %s: %s\n", 9857a6f8720SBrian Somers cmdstr, inet_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno)); 9867a6f8720SBrian Somers } 987dd7e2610SBrian Somers log_Printf(LogDEBUG, "wrote %d: cmd = %s, dst = %x, gateway = %x\n", 988fe3125a0SBrian Somers wb, cmdstr, (unsigned)dst.s_addr, (unsigned)gateway.s_addr); 9897a6f8720SBrian Somers close(s); 990610b185fSBrian Somers 991610b185fSBrian Somers return result; 9927a6f8720SBrian Somers } 99383d1af55SBrian Somers 99483d1af55SBrian Somers void 9953006ec67SBrian Somers bundle_LinkClosed(struct bundle *bundle, struct datalink *dl) 9963006ec67SBrian Somers { 9973006ec67SBrian Somers /* 9983006ec67SBrian Somers * Our datalink has closed. 999ea722969SBrian Somers * CleanDatalinks() (called from DoLoop()) will remove closed 1000ea722969SBrian Somers * 1OFF and DIRECT links. 10013b0f8d2eSBrian Somers * If it's the last data link, enter phase DEAD. 1002ea722969SBrian Somers * 1003ea722969SBrian Somers * NOTE: dl may not be in our list (bundle_SendDatalink()) ! 10043006ec67SBrian Somers */ 10055b8b8060SBrian Somers 10063b0f8d2eSBrian Somers struct datalink *odl; 10073b0f8d2eSBrian Somers int other_links; 10085b8b8060SBrian Somers 10093b0f8d2eSBrian Somers other_links = 0; 10103b0f8d2eSBrian Somers for (odl = bundle->links; odl; odl = odl->next) 10113b0f8d2eSBrian Somers if (odl != dl && odl->state != DATALINK_CLOSED) 10123b0f8d2eSBrian Somers other_links++; 10133b0f8d2eSBrian Somers 10143b0f8d2eSBrian Somers if (!other_links) { 101503704096SBrian Somers if (dl->physical->type != PHYS_DEMAND) /* Not in -auto mode */ 101603704096SBrian Somers bundle_DownInterface(bundle); 101726afeaa2SBrian Somers if (bundle->ncp.ipcp.fsm.state > ST_CLOSED || 101826afeaa2SBrian Somers bundle->ncp.ipcp.fsm.state == ST_STARTING) { 1019dd7e2610SBrian Somers fsm_Down(&bundle->ncp.ipcp.fsm); 1020dd7e2610SBrian Somers fsm_Close(&bundle->ncp.ipcp.fsm); /* ST_INITIAL please */ 102126afeaa2SBrian Somers } 10225563ebdeSBrian Somers bundle_NewPhase(bundle, PHASE_DEAD); 10230f2f3eb3SBrian Somers bundle_StopIdleTimer(bundle); 102404eaa58cSBrian Somers bundle_StopAutoLoadTimer(bundle); 102504eaa58cSBrian Somers bundle->autoload.running = 0; 102604eaa58cSBrian Somers } else 102704eaa58cSBrian Somers bundle->autoload.running = 1; 1028455aabc3SBrian Somers } 1029455aabc3SBrian Somers 1030455aabc3SBrian Somers void 1031565e35e5SBrian Somers bundle_Open(struct bundle *bundle, const char *name, int mask) 10323006ec67SBrian Somers { 10333006ec67SBrian Somers /* 10343006ec67SBrian Somers * Please open the given datalink, or all if name == NULL 10353006ec67SBrian Somers */ 10363006ec67SBrian Somers struct datalink *dl; 10373006ec67SBrian Somers 103804eaa58cSBrian Somers timer_Stop(&bundle->autoload.timer); 10393006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 10403006ec67SBrian Somers if (name == NULL || !strcasecmp(dl->name, name)) { 104104eaa58cSBrian Somers if (dl->state == DATALINK_CLOSED && (mask & dl->physical->type)) { 1042565e35e5SBrian Somers datalink_Up(dl, 1, 1); 104304eaa58cSBrian Somers if (mask == PHYS_DEMAND) 104404eaa58cSBrian Somers /* Only one DEMAND link at a time (see the AutoLoad timer) */ 104504eaa58cSBrian Somers break; 104604eaa58cSBrian Somers } 10473006ec67SBrian Somers if (name != NULL) 10483006ec67SBrian Somers break; 10493006ec67SBrian Somers } 10503006ec67SBrian Somers } 10513006ec67SBrian Somers 10523006ec67SBrian Somers struct datalink * 10533006ec67SBrian Somers bundle2datalink(struct bundle *bundle, const char *name) 10543006ec67SBrian Somers { 10553006ec67SBrian Somers struct datalink *dl; 10563006ec67SBrian Somers 10573006ec67SBrian Somers if (name != NULL) { 10583006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 10593006ec67SBrian Somers if (!strcasecmp(dl->name, name)) 10603006ec67SBrian Somers return dl; 10613006ec67SBrian Somers } else if (bundle->links && !bundle->links->next) 10623006ec67SBrian Somers return bundle->links; 10633006ec67SBrian Somers 10643006ec67SBrian Somers return NULL; 10653006ec67SBrian Somers } 10663006ec67SBrian Somers 10673006ec67SBrian Somers int 10683006ec67SBrian Somers bundle_FillQueues(struct bundle *bundle) 10693006ec67SBrian Somers { 10703b0f8d2eSBrian Somers int total; 10713b0f8d2eSBrian Somers 10721bc9b5baSBrian Somers if (bundle->ncp.mp.active) 10733b0f8d2eSBrian Somers total = mp_FillQueues(bundle); 10741bc9b5baSBrian Somers else { 10751bc9b5baSBrian Somers struct datalink *dl; 10761bc9b5baSBrian Somers int add; 10771bc9b5baSBrian Somers 10781bc9b5baSBrian Somers for (total = 0, dl = bundle->links; dl; dl = dl->next) 10791bc9b5baSBrian Somers if (dl->state == DATALINK_OPEN) { 10801bc9b5baSBrian Somers add = link_QueueLen(&dl->physical->link); 10811bc9b5baSBrian Somers if (add == 0 && dl->physical->out == NULL) 10821bc9b5baSBrian Somers add = ip_FlushPacket(&dl->physical->link, bundle); 10831bc9b5baSBrian Somers total += add; 10841bc9b5baSBrian Somers } 10853006ec67SBrian Somers } 10863006ec67SBrian Somers 10873b0f8d2eSBrian Somers return total + ip_QueueLen(); 10883006ec67SBrian Somers } 1089aef795ccSBrian Somers 1090aef795ccSBrian Somers int 1091aef795ccSBrian Somers bundle_ShowLinks(struct cmdargs const *arg) 1092aef795ccSBrian Somers { 1093aef795ccSBrian Somers struct datalink *dl; 1094aef795ccSBrian Somers 10959c53a7b1SBrian Somers for (dl = arg->bundle->links; dl; dl = dl->next) { 10969c53a7b1SBrian Somers prompt_Printf(arg->prompt, "Name: %s [%s]", dl->name, datalink_State(dl)); 1097eeab6bf5SBrian Somers if (dl->physical->link.throughput.rolling && dl->state == DATALINK_OPEN) 1098eeab6bf5SBrian Somers prompt_Printf(arg->prompt, " (weight %d, %d bytes/sec)", 1099eeab6bf5SBrian Somers dl->mp.weight, 11009c53a7b1SBrian Somers dl->physical->link.throughput.OctetsPerSecond); 11019c53a7b1SBrian Somers prompt_Printf(arg->prompt, "\n"); 11029c53a7b1SBrian Somers } 1103aef795ccSBrian Somers 1104aef795ccSBrian Somers return 0; 1105aef795ccSBrian Somers } 1106ab886ad0SBrian Somers 11071342caedSBrian Somers static const char * 11081342caedSBrian Somers optval(struct bundle *bundle, int bit) 11091342caedSBrian Somers { 11101342caedSBrian Somers return (bundle->cfg.opt & bit) ? "enabled" : "disabled"; 11111342caedSBrian Somers } 11121342caedSBrian Somers 1113c08717dfSBrian Somers int 1114c08717dfSBrian Somers bundle_ShowStatus(struct cmdargs const *arg) 1115c08717dfSBrian Somers { 1116c08717dfSBrian Somers int remaining; 1117c08717dfSBrian Somers 1118c08717dfSBrian Somers prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle)); 1119faefde08SBrian Somers prompt_Printf(arg->prompt, " Device: %s\n", arg->bundle->dev.Name); 1120faefde08SBrian Somers prompt_Printf(arg->prompt, " Interface: %s @ %lubps\n", 1121faefde08SBrian Somers arg->bundle->ifp.Name, arg->bundle->ifp.Speed); 1122c08717dfSBrian Somers 1123c08717dfSBrian Somers prompt_Printf(arg->prompt, "\nDefaults:\n"); 1124643f4904SBrian Somers prompt_Printf(arg->prompt, " Label: %s\n", arg->bundle->cfg.label); 1125610b185fSBrian Somers prompt_Printf(arg->prompt, " Auth name: %s\n", 1126610b185fSBrian Somers arg->bundle->cfg.auth.name); 112704eaa58cSBrian Somers prompt_Printf(arg->prompt, " Auto Load: Up after %ds of >= %d packets\n", 112804eaa58cSBrian Somers arg->bundle->cfg.autoload.max.timeout, 112904eaa58cSBrian Somers arg->bundle->cfg.autoload.max.packets); 113004eaa58cSBrian Somers prompt_Printf(arg->prompt, " Down after %ds of <= %d" 113104eaa58cSBrian Somers " packets\n", arg->bundle->cfg.autoload.min.timeout, 113204eaa58cSBrian Somers arg->bundle->cfg.autoload.min.packets); 113304eaa58cSBrian Somers if (arg->bundle->autoload.timer.state == TIMER_RUNNING) 113404eaa58cSBrian Somers prompt_Printf(arg->prompt, " %ds remaining 'till " 113504eaa58cSBrian Somers "a link comes %s\n", 113604eaa58cSBrian Somers bundle_RemainingAutoLoadTime(arg->bundle), 113704eaa58cSBrian Somers arg->bundle->autoload.comingup ? "up" : "down"); 113804eaa58cSBrian Somers else 113904eaa58cSBrian Somers prompt_Printf(arg->prompt, " %srunning with %d" 114004eaa58cSBrian Somers " packets queued\n", arg->bundle->autoload.running ? 114104eaa58cSBrian Somers "" : "not ", ip_QueueLen()); 114204eaa58cSBrian Somers 1143c08717dfSBrian Somers prompt_Printf(arg->prompt, " Idle Timer: "); 1144c08717dfSBrian Somers if (arg->bundle->cfg.idle_timeout) { 1145c08717dfSBrian Somers prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle_timeout); 1146c08717dfSBrian Somers remaining = bundle_RemainingIdleTime(arg->bundle); 1147c08717dfSBrian Somers if (remaining != -1) 1148c08717dfSBrian Somers prompt_Printf(arg->prompt, " (%ds remaining)", remaining); 1149c08717dfSBrian Somers prompt_Printf(arg->prompt, "\n"); 1150c08717dfSBrian Somers } else 1151c08717dfSBrian Somers prompt_Printf(arg->prompt, "disabled\n"); 1152ce828a6eSBrian Somers prompt_Printf(arg->prompt, " MTU: "); 1153ce828a6eSBrian Somers if (arg->bundle->cfg.mtu) 1154ce828a6eSBrian Somers prompt_Printf(arg->prompt, "%d\n", arg->bundle->cfg.mtu); 1155ce828a6eSBrian Somers else 1156ce828a6eSBrian Somers prompt_Printf(arg->prompt, "unspecified\n"); 1157c08717dfSBrian Somers 1158610b185fSBrian Somers prompt_Printf(arg->prompt, " Sticky Routes: %s\n", 1159610b185fSBrian Somers optval(arg->bundle, OPT_SROUTES)); 11601342caedSBrian Somers prompt_Printf(arg->prompt, " ID check: %s\n", 11611342caedSBrian Somers optval(arg->bundle, OPT_IDCHECK)); 11621342caedSBrian Somers prompt_Printf(arg->prompt, " Loopback: %s\n", 11631342caedSBrian Somers optval(arg->bundle, OPT_LOOPBACK)); 11641342caedSBrian Somers prompt_Printf(arg->prompt, " PasswdAuth: %s\n", 11651342caedSBrian Somers optval(arg->bundle, OPT_PASSWDAUTH)); 11661342caedSBrian Somers prompt_Printf(arg->prompt, " Proxy: %s\n", 11671342caedSBrian Somers optval(arg->bundle, OPT_PROXY)); 11681342caedSBrian Somers prompt_Printf(arg->prompt, " Throughput: %s\n", 11691342caedSBrian Somers optval(arg->bundle, OPT_THROUGHPUT)); 1170610b185fSBrian Somers prompt_Printf(arg->prompt, " Utmp Logging: %s\n", 11711342caedSBrian Somers optval(arg->bundle, OPT_UTMP)); 11721342caedSBrian Somers 1173c08717dfSBrian Somers return 0; 1174c08717dfSBrian Somers } 1175c08717dfSBrian Somers 1176ab886ad0SBrian Somers static void 1177ab886ad0SBrian Somers bundle_IdleTimeout(void *v) 1178ab886ad0SBrian Somers { 1179ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v; 1180ab886ad0SBrian Somers 1181dd7e2610SBrian Somers log_Printf(LogPHASE, "Idle timer expired.\n"); 118204eaa58cSBrian Somers bundle_StopIdleTimer(bundle); 1183ab886ad0SBrian Somers bundle_Close(bundle, NULL, 1); 1184ab886ad0SBrian Somers } 1185ab886ad0SBrian Somers 1186ab886ad0SBrian Somers /* 1187ab886ad0SBrian Somers * Start Idle timer. If timeout is reached, we call bundle_Close() to 1188ab886ad0SBrian Somers * close LCP and link. 1189ab886ad0SBrian Somers */ 1190ab886ad0SBrian Somers void 1191ab886ad0SBrian Somers bundle_StartIdleTimer(struct bundle *bundle) 1192ab886ad0SBrian Somers { 1193dd7e2610SBrian Somers timer_Stop(&bundle->idle.timer); 119404eaa58cSBrian Somers if ((bundle->phys_type & (PHYS_DEDICATED|PHYS_PERM)) != bundle->phys_type && 119504eaa58cSBrian Somers bundle->cfg.idle_timeout) { 119693ee0ff2SBrian Somers bundle->idle.timer.func = bundle_IdleTimeout; 11973b0f8d2eSBrian Somers bundle->idle.timer.name = "idle"; 119893ee0ff2SBrian Somers bundle->idle.timer.load = bundle->cfg.idle_timeout * SECTICKS; 119993ee0ff2SBrian Somers bundle->idle.timer.arg = bundle; 1200dd7e2610SBrian Somers timer_Start(&bundle->idle.timer); 120193ee0ff2SBrian Somers bundle->idle.done = time(NULL) + bundle->cfg.idle_timeout; 1202ab886ad0SBrian Somers } 1203ab886ad0SBrian Somers } 1204ab886ad0SBrian Somers 1205ab886ad0SBrian Somers void 1206ab886ad0SBrian Somers bundle_SetIdleTimer(struct bundle *bundle, int value) 1207ab886ad0SBrian Somers { 1208ab886ad0SBrian Somers bundle->cfg.idle_timeout = value; 1209ab886ad0SBrian Somers if (bundle_LinkIsUp(bundle)) 1210ab886ad0SBrian Somers bundle_StartIdleTimer(bundle); 1211ab886ad0SBrian Somers } 1212ab886ad0SBrian Somers 1213ab886ad0SBrian Somers void 1214ab886ad0SBrian Somers bundle_StopIdleTimer(struct bundle *bundle) 1215ab886ad0SBrian Somers { 1216dd7e2610SBrian Somers timer_Stop(&bundle->idle.timer); 12174a632c80SBrian Somers bundle->idle.done = 0; 1218ab886ad0SBrian Somers } 1219ab886ad0SBrian Somers 122004eaa58cSBrian Somers static int 1221ab886ad0SBrian Somers bundle_RemainingIdleTime(struct bundle *bundle) 1222ab886ad0SBrian Somers { 122393ee0ff2SBrian Somers if (bundle->idle.done) 122493ee0ff2SBrian Somers return bundle->idle.done - time(NULL); 1225ab886ad0SBrian Somers return -1; 1226ab886ad0SBrian Somers } 12273b0f8d2eSBrian Somers 12283b0f8d2eSBrian Somers int 12293b0f8d2eSBrian Somers bundle_IsDead(struct bundle *bundle) 12303b0f8d2eSBrian Somers { 12313b0f8d2eSBrian Somers return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp); 12323b0f8d2eSBrian Somers } 1233b6217683SBrian Somers 1234565e35e5SBrian Somers static void 123504eaa58cSBrian Somers bundle_LinkAdded(struct bundle *bundle, struct datalink *dl) 123604eaa58cSBrian Somers { 123704eaa58cSBrian Somers bundle->phys_type |= dl->physical->type; 123804eaa58cSBrian Somers if (dl->physical->type == PHYS_DEMAND && 123904eaa58cSBrian Somers bundle->autoload.timer.state == TIMER_STOPPED && 124004eaa58cSBrian Somers bundle->phase == PHASE_NETWORK) 124104eaa58cSBrian Somers bundle->autoload.running = 1; 124204eaa58cSBrian Somers } 124304eaa58cSBrian Somers 124404eaa58cSBrian Somers static void 124504eaa58cSBrian Somers bundle_LinksRemoved(struct bundle *bundle) 1246565e35e5SBrian Somers { 1247565e35e5SBrian Somers struct datalink *dl; 1248565e35e5SBrian Somers 1249565e35e5SBrian Somers bundle->phys_type = 0; 1250565e35e5SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 125104eaa58cSBrian Somers bundle_LinkAdded(bundle, dl); 125204eaa58cSBrian Somers 125304eaa58cSBrian Somers if ((bundle->phys_type & (PHYS_DEDICATED|PHYS_PERM)) == bundle->phys_type) 125404eaa58cSBrian Somers timer_Stop(&bundle->idle.timer); 1255565e35e5SBrian Somers } 1256565e35e5SBrian Somers 125704eaa58cSBrian Somers static struct datalink * 125804eaa58cSBrian Somers bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl) 1259cd7bd93aSBrian Somers { 1260cd7bd93aSBrian Somers struct datalink **dlp; 1261cd7bd93aSBrian Somers 1262cd7bd93aSBrian Somers for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next) 1263cd7bd93aSBrian Somers if (*dlp == dl) { 126404eaa58cSBrian Somers *dlp = dl->next; 126504eaa58cSBrian Somers dl->next = NULL; 126604eaa58cSBrian Somers bundle_LinksRemoved(bundle); 126704eaa58cSBrian Somers return dl; 1268cd7bd93aSBrian Somers } 126904eaa58cSBrian Somers 127004eaa58cSBrian Somers return NULL; 127104eaa58cSBrian Somers } 127204eaa58cSBrian Somers 127304eaa58cSBrian Somers static void 127404eaa58cSBrian Somers bundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl) 127504eaa58cSBrian Somers { 127604eaa58cSBrian Somers struct datalink **dlp = &bundle->links; 127704eaa58cSBrian Somers 127804eaa58cSBrian Somers while (*dlp) 127904eaa58cSBrian Somers dlp = &(*dlp)->next; 128004eaa58cSBrian Somers 128104eaa58cSBrian Somers *dlp = dl; 128204eaa58cSBrian Somers dl->next = NULL; 128304eaa58cSBrian Somers 128404eaa58cSBrian Somers bundle_LinkAdded(bundle, dl); 1285565e35e5SBrian Somers } 1286565e35e5SBrian Somers 1287565e35e5SBrian Somers void 1288565e35e5SBrian Somers bundle_CleanDatalinks(struct bundle *bundle) 1289565e35e5SBrian Somers { 1290565e35e5SBrian Somers struct datalink **dlp = &bundle->links; 129104eaa58cSBrian Somers int found = 0; 1292565e35e5SBrian Somers 1293565e35e5SBrian Somers while (*dlp) 1294565e35e5SBrian Somers if ((*dlp)->state == DATALINK_CLOSED && 129504eaa58cSBrian Somers (*dlp)->physical->type & (PHYS_DIRECT|PHYS_1OFF)) { 1296565e35e5SBrian Somers *dlp = datalink_Destroy(*dlp); 129704eaa58cSBrian Somers found++; 129804eaa58cSBrian Somers } else 1299565e35e5SBrian Somers dlp = &(*dlp)->next; 130004eaa58cSBrian Somers 130104eaa58cSBrian Somers if (found) 130204eaa58cSBrian Somers bundle_LinksRemoved(bundle); 130304eaa58cSBrian Somers } 130404eaa58cSBrian Somers 130504eaa58cSBrian Somers int 130604eaa58cSBrian Somers bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl, 130704eaa58cSBrian Somers const char *name) 130804eaa58cSBrian Somers { 130904eaa58cSBrian Somers if (bundle2datalink(bundle, name)) { 131004eaa58cSBrian Somers log_Printf(LogWARN, "Clone: %s: name already exists\n", name); 131104eaa58cSBrian Somers return 0; 131204eaa58cSBrian Somers } 131304eaa58cSBrian Somers 131404eaa58cSBrian Somers bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name)); 131504eaa58cSBrian Somers return 1; 131604eaa58cSBrian Somers } 131704eaa58cSBrian Somers 131804eaa58cSBrian Somers void 131904eaa58cSBrian Somers bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl) 132004eaa58cSBrian Somers { 132104eaa58cSBrian Somers dl = bundle_DatalinkLinkout(bundle, dl); 132204eaa58cSBrian Somers if (dl) 132304eaa58cSBrian Somers datalink_Destroy(dl); 1324cd7bd93aSBrian Somers } 132549052c95SBrian Somers 132649052c95SBrian Somers void 132749052c95SBrian Somers bundle_SetLabel(struct bundle *bundle, const char *label) 132849052c95SBrian Somers { 132949052c95SBrian Somers if (label) 133049052c95SBrian Somers strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1); 133149052c95SBrian Somers else 133249052c95SBrian Somers *bundle->cfg.label = '\0'; 133349052c95SBrian Somers } 133449052c95SBrian Somers 133549052c95SBrian Somers const char * 133649052c95SBrian Somers bundle_GetLabel(struct bundle *bundle) 133749052c95SBrian Somers { 133849052c95SBrian Somers return *bundle->cfg.label ? bundle->cfg.label : NULL; 133949052c95SBrian Somers } 13401fa665f5SBrian Somers 13411fa665f5SBrian Somers void 134296c9bb21SBrian Somers bundle_ReceiveDatalink(struct bundle *bundle, int s, struct sockaddr_un *sun) 13431fa665f5SBrian Somers { 134496c9bb21SBrian Somers char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int)]; 134596c9bb21SBrian Somers struct cmsghdr *cmsg = (struct cmsghdr *)cmsgbuf; 134696c9bb21SBrian Somers struct msghdr msg; 134796c9bb21SBrian Somers struct iovec iov[SCATTER_SEGMENTS]; 1348b7c5748eSBrian Somers struct datalink *dl; 134996c9bb21SBrian Somers int niov, link_fd, expect, f; 13506f384573SBrian Somers 1351dd7e2610SBrian Somers log_Printf(LogPHASE, "Receiving datalink\n"); 13526f384573SBrian Somers 135396c9bb21SBrian Somers /* Create our scatter/gather array */ 135496c9bb21SBrian Somers niov = 1; 135596c9bb21SBrian Somers iov[0].iov_len = strlen(Version) + 1; 135696c9bb21SBrian Somers iov[0].iov_base = (char *)malloc(iov[0].iov_len); 135796c9bb21SBrian Somers if (datalink2iov(NULL, iov, &niov, sizeof iov / sizeof *iov) == -1) 13586f384573SBrian Somers return; 13596f384573SBrian Somers 136096c9bb21SBrian Somers for (f = expect = 0; f < niov; f++) 136196c9bb21SBrian Somers expect += iov[f].iov_len; 136296c9bb21SBrian Somers 136396c9bb21SBrian Somers /* Set up our message */ 136496c9bb21SBrian Somers cmsg->cmsg_len = sizeof cmsgbuf; 136596c9bb21SBrian Somers cmsg->cmsg_level = SOL_SOCKET; 136696c9bb21SBrian Somers cmsg->cmsg_type = SCM_RIGHTS; 136796c9bb21SBrian Somers 136896c9bb21SBrian Somers memset(&msg, '\0', sizeof msg); 136996c9bb21SBrian Somers msg.msg_name = (caddr_t)sun; 137096c9bb21SBrian Somers msg.msg_namelen = sizeof *sun; 137196c9bb21SBrian Somers msg.msg_iov = iov; 137296c9bb21SBrian Somers msg.msg_iovlen = niov; 137396c9bb21SBrian Somers msg.msg_control = cmsgbuf; 137496c9bb21SBrian Somers msg.msg_controllen = sizeof cmsgbuf; 137596c9bb21SBrian Somers 137696c9bb21SBrian Somers log_Printf(LogDEBUG, "Expecting %d scatter/gather bytes\n", expect); 137796c9bb21SBrian Somers f = expect + 100; 137896c9bb21SBrian Somers setsockopt(s, SOL_SOCKET, SO_RCVBUF, &f, sizeof f); 137996c9bb21SBrian Somers if ((f = recvmsg(s, &msg, MSG_WAITALL)) != expect) { 138096c9bb21SBrian Somers if (f == -1) 138196c9bb21SBrian Somers log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno)); 138296c9bb21SBrian Somers else 138396c9bb21SBrian Somers log_Printf(LogERROR, "Failed recvmsg: Got %d, not %d\n", f, expect); 138496c9bb21SBrian Somers while (niov--) 138596c9bb21SBrian Somers free(iov[niov].iov_base); 138696c9bb21SBrian Somers return; 138796c9bb21SBrian Somers } 138896c9bb21SBrian Somers 138996c9bb21SBrian Somers /* We've successfully received an open file descriptor through our socket */ 139096c9bb21SBrian Somers link_fd = *(int *)CMSG_DATA(cmsg); 139196c9bb21SBrian Somers 1392c0d9a877SBrian Somers write(s, "!",1 ); /* ACK */ 1393c0d9a877SBrian Somers 139496c9bb21SBrian Somers if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) { 139596c9bb21SBrian Somers log_Printf(LogWARN, "Cannot receive datalink, incorrect version" 139696c9bb21SBrian Somers " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len, 139796c9bb21SBrian Somers iov[0].iov_base, Version); 139896c9bb21SBrian Somers close(link_fd); 139996c9bb21SBrian Somers while (niov--) 140096c9bb21SBrian Somers free(iov[niov].iov_base); 140196c9bb21SBrian Somers return; 140296c9bb21SBrian Somers } 140396c9bb21SBrian Somers 140496c9bb21SBrian Somers niov = 1; 1405b7c5748eSBrian Somers dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, link_fd); 1406b7c5748eSBrian Somers if (dl) { 140704eaa58cSBrian Somers bundle_DatalinkLinkin(bundle, dl); 1408b7c5748eSBrian Somers datalink_AuthOk(dl); 140996c9bb21SBrian Somers } else 141096c9bb21SBrian Somers close(link_fd); 141196c9bb21SBrian Somers 141296c9bb21SBrian Somers free(iov[0].iov_base); 14131fa665f5SBrian Somers } 14141fa665f5SBrian Somers 14151fa665f5SBrian Somers void 141696c9bb21SBrian Somers bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun) 14171fa665f5SBrian Somers { 1418c0d9a877SBrian Somers char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int)], ack; 141996c9bb21SBrian Somers struct cmsghdr *cmsg = (struct cmsghdr *)cmsgbuf; 142096c9bb21SBrian Somers struct msghdr msg; 142196c9bb21SBrian Somers struct iovec iov[SCATTER_SEGMENTS]; 142296c9bb21SBrian Somers int niov, link_fd, f, expect; 14236f384573SBrian Somers 1424dd7e2610SBrian Somers log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name); 14256f384573SBrian Somers 142604eaa58cSBrian Somers bundle_LinkClosed(dl->bundle, dl); 14270f2f3eb3SBrian Somers bundle_DatalinkLinkout(dl->bundle, dl); 1428ea722969SBrian Somers 142996c9bb21SBrian Somers /* Build our scatter/gather array */ 143096c9bb21SBrian Somers iov[0].iov_len = strlen(Version) + 1; 143196c9bb21SBrian Somers iov[0].iov_base = strdup(Version); 143296c9bb21SBrian Somers niov = 1; 14336f384573SBrian Somers 143496c9bb21SBrian Somers link_fd = datalink2iov(dl, iov, &niov, sizeof iov / sizeof *iov); 14356f384573SBrian Somers 14366f384573SBrian Somers if (link_fd != -1) { 143796c9bb21SBrian Somers cmsg->cmsg_len = sizeof cmsgbuf; 143896c9bb21SBrian Somers cmsg->cmsg_level = SOL_SOCKET; 143996c9bb21SBrian Somers cmsg->cmsg_type = SCM_RIGHTS; 144096c9bb21SBrian Somers *(int *)CMSG_DATA(cmsg) = link_fd; 144147723d29SBrian Somers 144296c9bb21SBrian Somers memset(&msg, '\0', sizeof msg); 144396c9bb21SBrian Somers msg.msg_name = (caddr_t)sun; 144496c9bb21SBrian Somers msg.msg_namelen = sizeof *sun; 144596c9bb21SBrian Somers msg.msg_iov = iov; 144696c9bb21SBrian Somers msg.msg_iovlen = niov; 144796c9bb21SBrian Somers msg.msg_control = cmsgbuf; 144896c9bb21SBrian Somers msg.msg_controllen = sizeof cmsgbuf; 144947723d29SBrian Somers 145096c9bb21SBrian Somers for (f = expect = 0; f < niov; f++) 145196c9bb21SBrian Somers expect += iov[f].iov_len; 145247723d29SBrian Somers 145396c9bb21SBrian Somers log_Printf(LogDEBUG, "Sending %d bytes in scatter/gather array\n", expect); 145447723d29SBrian Somers 145596c9bb21SBrian Somers f = expect + SOCKET_OVERHEAD; 145696c9bb21SBrian Somers setsockopt(s, SOL_SOCKET, SO_SNDBUF, &f, sizeof f); 145796c9bb21SBrian Somers if (sendmsg(s, &msg, 0) == -1) 145896c9bb21SBrian Somers log_Printf(LogERROR, "Failed sendmsg: %s\n", strerror(errno)); 1459c0d9a877SBrian Somers /* We must get the ACK before closing the descriptor ! */ 1460c0d9a877SBrian Somers read(s, &ack, 1); 146196c9bb21SBrian Somers close(link_fd); 146247723d29SBrian Somers } 146396c9bb21SBrian Somers 146496c9bb21SBrian Somers while (niov--) 146596c9bb21SBrian Somers free(iov[niov].iov_base); 14661fa665f5SBrian Somers } 1467dd0645c5SBrian Somers 1468dd0645c5SBrian Somers int 146958d55334SBrian Somers bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl, 147058d55334SBrian Somers const char *name) 147158d55334SBrian Somers { 147258d55334SBrian Somers struct datalink *dl; 147358d55334SBrian Somers 147458d55334SBrian Somers if (!strcasecmp(ndl->name, name)) 147558d55334SBrian Somers return 1; 147658d55334SBrian Somers 147758d55334SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 147858d55334SBrian Somers if (!strcasecmp(dl->name, name)) 147958d55334SBrian Somers return 0; 148058d55334SBrian Somers 148158d55334SBrian Somers datalink_Rename(ndl, name); 148258d55334SBrian Somers return 1; 148358d55334SBrian Somers } 148458d55334SBrian Somers 148558d55334SBrian Somers int 1486dd0645c5SBrian Somers bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode) 1487dd0645c5SBrian Somers { 1488dd0645c5SBrian Somers int omode; 1489dd0645c5SBrian Somers 1490dd0645c5SBrian Somers omode = dl->physical->type; 1491dd0645c5SBrian Somers if (omode == mode) 1492dd0645c5SBrian Somers return 1; 1493dd0645c5SBrian Somers 1494dd0645c5SBrian Somers if (mode == PHYS_DEMAND && !(bundle->phys_type & PHYS_DEMAND)) 1495dd0645c5SBrian Somers /* Changing to demand-dial mode */ 1496dd0645c5SBrian Somers if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) { 1497dd0645c5SBrian Somers log_Printf(LogWARN, "You must `set ifaddr' before changing mode to %s\n", 1498dd0645c5SBrian Somers mode2Nam(mode)); 1499dd0645c5SBrian Somers return 0; 1500dd0645c5SBrian Somers } 1501dd0645c5SBrian Somers 1502dd0645c5SBrian Somers if (!datalink_SetMode(dl, mode)) 1503dd0645c5SBrian Somers return 0; 1504dd0645c5SBrian Somers 1505dd0645c5SBrian Somers if (mode == PHYS_DEMAND && !(bundle->phys_type & PHYS_DEMAND)) 1506dd0645c5SBrian Somers ipcp_InterfaceUp(&bundle->ncp.ipcp); 1507dd0645c5SBrian Somers 150804eaa58cSBrian Somers /* Regenerate phys_type and adjust autoload & idle timers */ 150904eaa58cSBrian Somers bundle_LinksRemoved(bundle); 1510dd0645c5SBrian Somers 1511dd0645c5SBrian Somers if (omode == PHYS_DEMAND && !(bundle->phys_type & PHYS_DEMAND)) 1512dd0645c5SBrian Somers /* Changing from demand-dial mode */ 1513dd0645c5SBrian Somers ipcp_CleanInterface(&bundle->ncp.ipcp); 1514dd0645c5SBrian Somers 1515dd0645c5SBrian Somers return 1; 1516dd0645c5SBrian Somers } 1517