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 * 26332b9de0SBrian Somers * $Id: bundle.c,v 1.1.2.85 1998/05/16 23:47:20 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 90455aabc3SBrian Somers static const char *PhaseNames[] = { 91455aabc3SBrian Somers "Dead", "Establish", "Authenticate", "Network", "Terminate" 92455aabc3SBrian Somers }; 93455aabc3SBrian Somers 94455aabc3SBrian Somers const char * 95455aabc3SBrian Somers bundle_PhaseName(struct bundle *bundle) 967a6f8720SBrian Somers { 97455aabc3SBrian Somers return bundle->phase <= PHASE_TERMINATE ? 98455aabc3SBrian Somers PhaseNames[bundle->phase] : "unknown"; 997a6f8720SBrian Somers } 1007a6f8720SBrian Somers 101455aabc3SBrian Somers void 1025563ebdeSBrian Somers bundle_NewPhase(struct bundle *bundle, u_int new) 103455aabc3SBrian Somers { 104aef795ccSBrian Somers if (new == bundle->phase) 105aef795ccSBrian Somers return; 106aef795ccSBrian Somers 107e2ebb036SBrian Somers if (new <= PHASE_TERMINATE) 108dd7e2610SBrian Somers log_Printf(LogPHASE, "bundle: %s\n", PhaseNames[new]); 1097a6f8720SBrian Somers 110455aabc3SBrian Somers switch (new) { 111455aabc3SBrian Somers case PHASE_DEAD: 112455aabc3SBrian Somers bundle->phase = new; 113455aabc3SBrian Somers break; 114455aabc3SBrian Somers 115455aabc3SBrian Somers case PHASE_ESTABLISH: 116455aabc3SBrian Somers bundle->phase = new; 117455aabc3SBrian Somers break; 118455aabc3SBrian Somers 119455aabc3SBrian Somers case PHASE_AUTHENTICATE: 120455aabc3SBrian Somers bundle->phase = new; 121b6217683SBrian Somers bundle_DisplayPrompt(bundle); 122455aabc3SBrian Somers break; 123455aabc3SBrian Somers 124455aabc3SBrian Somers case PHASE_NETWORK: 1255828db6dSBrian Somers ipcp_Setup(&bundle->ncp.ipcp); 126dd7e2610SBrian Somers fsm_Up(&bundle->ncp.ipcp.fsm); 127dd7e2610SBrian Somers fsm_Open(&bundle->ncp.ipcp.fsm); 128673903ecSBrian Somers bundle->phase = new; 129673903ecSBrian Somers bundle_DisplayPrompt(bundle); 130673903ecSBrian Somers break; 131455aabc3SBrian Somers 132455aabc3SBrian Somers case PHASE_TERMINATE: 133455aabc3SBrian Somers bundle->phase = new; 134673903ecSBrian Somers mp_Down(&bundle->ncp.mp); 135b6217683SBrian Somers bundle_DisplayPrompt(bundle); 136455aabc3SBrian Somers break; 1377a6f8720SBrian Somers } 1387a6f8720SBrian Somers } 1397a6f8720SBrian Somers 1407a6f8720SBrian Somers static int 1417a6f8720SBrian Somers bundle_CleanInterface(const struct bundle *bundle) 1427a6f8720SBrian Somers { 1437a6f8720SBrian Somers int s; 1447a6f8720SBrian Somers struct ifreq ifrq; 1457a6f8720SBrian Somers struct ifaliasreq ifra; 1467a6f8720SBrian Somers 1477a6f8720SBrian Somers s = ID0socket(AF_INET, SOCK_DGRAM, 0); 1487a6f8720SBrian Somers if (s < 0) { 149dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_CleanInterface: socket(): %s\n", 1507a6f8720SBrian Somers strerror(errno)); 1517a6f8720SBrian Somers return (-1); 1527a6f8720SBrian Somers } 153faefde08SBrian Somers strncpy(ifrq.ifr_name, bundle->ifp.Name, sizeof ifrq.ifr_name - 1); 1547a6f8720SBrian Somers ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 1557a6f8720SBrian Somers while (ID0ioctl(s, SIOCGIFADDR, &ifrq) == 0) { 1567a6f8720SBrian Somers memset(&ifra.ifra_mask, '\0', sizeof ifra.ifra_mask); 157faefde08SBrian Somers strncpy(ifra.ifra_name, bundle->ifp.Name, sizeof ifra.ifra_name - 1); 1587a6f8720SBrian Somers ifra.ifra_name[sizeof ifra.ifra_name - 1] = '\0'; 1597a6f8720SBrian Somers ifra.ifra_addr = ifrq.ifr_addr; 1607a6f8720SBrian Somers if (ID0ioctl(s, SIOCGIFDSTADDR, &ifrq) < 0) { 1617a6f8720SBrian Somers if (ifra.ifra_addr.sa_family == AF_INET) 162dd7e2610SBrian Somers log_Printf(LogERROR, 1637a6f8720SBrian Somers "bundle_CleanInterface: Can't get dst for %s on %s !\n", 1647a6f8720SBrian Somers inet_ntoa(((struct sockaddr_in *)&ifra.ifra_addr)->sin_addr), 165faefde08SBrian Somers bundle->ifp.Name); 1667a6f8720SBrian Somers return 0; 1677a6f8720SBrian Somers } 1687a6f8720SBrian Somers ifra.ifra_broadaddr = ifrq.ifr_dstaddr; 1697a6f8720SBrian Somers if (ID0ioctl(s, SIOCDIFADDR, &ifra) < 0) { 1707a6f8720SBrian Somers if (ifra.ifra_addr.sa_family == AF_INET) 171dd7e2610SBrian Somers log_Printf(LogERROR, 1727a6f8720SBrian Somers "bundle_CleanInterface: Can't delete %s address on %s !\n", 1737a6f8720SBrian Somers inet_ntoa(((struct sockaddr_in *)&ifra.ifra_addr)->sin_addr), 174faefde08SBrian Somers bundle->ifp.Name); 1757a6f8720SBrian Somers return 0; 1767a6f8720SBrian Somers } 1777a6f8720SBrian Somers } 1787a6f8720SBrian Somers 1797a6f8720SBrian Somers return 1; 1807a6f8720SBrian Somers } 1817a6f8720SBrian Somers 1826d666775SBrian Somers static void 1836d666775SBrian Somers bundle_LayerStart(void *v, struct fsm *fp) 1847a6f8720SBrian Somers { 1853006ec67SBrian Somers /* The given FSM is about to start up ! */ 1867a6f8720SBrian Somers } 1877a6f8720SBrian Somers 1885cf4388bSBrian Somers 1895cf4388bSBrian Somers static void 1905cf4388bSBrian Somers bundle_Notify(struct bundle *bundle, char c) 1915cf4388bSBrian Somers { 1925cf4388bSBrian Somers if (bundle->notify.fd != -1) { 1935cf4388bSBrian Somers if (write(bundle->notify.fd, &c, 1) == 1) 194dd7e2610SBrian Somers log_Printf(LogPHASE, "Parent notified of success.\n"); 1955cf4388bSBrian Somers else 196dd7e2610SBrian Somers log_Printf(LogPHASE, "Failed to notify parent of success.\n"); 1975cf4388bSBrian Somers close(bundle->notify.fd); 1985cf4388bSBrian Somers bundle->notify.fd = -1; 1995cf4388bSBrian Somers } 2005cf4388bSBrian Somers } 2013b0f8d2eSBrian Somers 2026d666775SBrian Somers static void 2036f384573SBrian Somers bundle_LayerUp(void *v, struct fsm *fp) 2047a6f8720SBrian Somers { 2053006ec67SBrian Somers /* 2063006ec67SBrian Somers * The given fsm is now up 20749052c95SBrian Somers * If it's an LCP set our mtu (if we're multilink, add up the link 20849052c95SBrian Somers * speeds and set the MRRU). 209565e35e5SBrian Somers * If it's an NCP, tell our -background parent to go away. 2103b0f8d2eSBrian Somers * If it's the first NCP, start the idle timer. 2113006ec67SBrian Somers */ 2126f384573SBrian Somers struct bundle *bundle = (struct bundle *)v; 2136d666775SBrian Somers 2145563ebdeSBrian Somers if (fp->proto == PROTO_LCP) { 2153b0f8d2eSBrian Somers if (bundle->ncp.mp.active) { 2163b0f8d2eSBrian Somers struct datalink *dl; 2175563ebdeSBrian Somers 218faefde08SBrian Somers bundle->ifp.Speed = 0; 219faefde08SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 220eeab6bf5SBrian Somers if (dl->state == DATALINK_OPEN) 221faefde08SBrian Somers bundle->ifp.Speed += modem_Speed(dl->physical); 222faefde08SBrian Somers tun_configure(bundle, bundle->ncp.mp.peer_mrru); 223faefde08SBrian Somers } else { 224faefde08SBrian Somers bundle->ifp.Speed = modem_Speed(link2physical(fp->link)); 225faefde08SBrian Somers tun_configure(bundle, fsm2lcp(fp)->his_mru); 226faefde08SBrian Somers } 2273b0f8d2eSBrian Somers } else if (fp->proto == PROTO_IPCP) { 228ab886ad0SBrian Somers bundle_StartIdleTimer(bundle); 2295cf4388bSBrian Somers bundle_Notify(bundle, EX_NORMAL); 2307a6f8720SBrian Somers } 231ab886ad0SBrian Somers } 2327a6f8720SBrian Somers 2336d666775SBrian Somers static void 2346d666775SBrian Somers bundle_LayerDown(void *v, struct fsm *fp) 2356d666775SBrian Somers { 2366d666775SBrian Somers /* 2376d666775SBrian Somers * The given FSM has been told to come down. 238ab886ad0SBrian Somers * If it's our last NCP, stop the idle timer. 2393b0f8d2eSBrian Somers * If it's an LCP and we're in multilink mode, adjust our tun speed. 2406d666775SBrian Somers */ 241ab886ad0SBrian Somers 242ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v; 243ab886ad0SBrian Somers 244078c562eSBrian Somers if (fp->proto == PROTO_IPCP) 245ab886ad0SBrian Somers bundle_StopIdleTimer(bundle); 246078c562eSBrian Somers else if (fp->proto == PROTO_LCP && bundle->ncp.mp.active) { 2473b0f8d2eSBrian Somers struct datalink *dl; 2483b0f8d2eSBrian Somers 249faefde08SBrian Somers bundle->ifp.Speed = 0; 250faefde08SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 251078c562eSBrian Somers if (fp != &dl->physical->link.lcp.fsm && dl->state == DATALINK_OPEN) 252faefde08SBrian Somers bundle->ifp.Speed += modem_Speed(dl->physical); 253faefde08SBrian Somers if (bundle->ifp.Speed) 254faefde08SBrian Somers /* Don't configure down to a speed of 0 */ 255faefde08SBrian Somers tun_configure(bundle, bundle->ncp.mp.link.lcp.his_mru); 2563b0f8d2eSBrian Somers } 2576d666775SBrian Somers } 2586d666775SBrian Somers 2596d666775SBrian Somers static void 2606d666775SBrian Somers bundle_LayerFinish(void *v, struct fsm *fp) 2616d666775SBrian Somers { 2626d666775SBrian Somers /* The given fsm is now down (fp cannot be NULL) 2636d666775SBrian Somers * 264dd7e2610SBrian Somers * If it's the last LCP, fsm_Down all NCPs 265dd7e2610SBrian Somers * If it's the last NCP, fsm_Close all LCPs 2666d666775SBrian Somers */ 2676d666775SBrian Somers 2686d666775SBrian Somers struct bundle *bundle = (struct bundle *)v; 2696d666775SBrian Somers struct datalink *dl; 2706d666775SBrian Somers 2713b0f8d2eSBrian Somers if (fp->proto == PROTO_IPCP) { 27226afeaa2SBrian Somers if (bundle_Phase(bundle) != PHASE_DEAD) 27325092092SBrian Somers bundle_NewPhase(bundle, PHASE_TERMINATE); 2746d666775SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 2753b0f8d2eSBrian Somers datalink_Close(dl, 0); 276dd7e2610SBrian Somers fsm_Down(fp); 277dd7e2610SBrian Somers fsm_Close(fp); 2783b0f8d2eSBrian Somers } else if (fp->proto == PROTO_LCP) { 2793b0f8d2eSBrian Somers int others_active; 280a611cad6SBrian Somers 2813b0f8d2eSBrian Somers others_active = 0; 2823b0f8d2eSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 2833b0f8d2eSBrian Somers if (fp != &dl->physical->link.lcp.fsm && 2843b0f8d2eSBrian Somers dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 2853b0f8d2eSBrian Somers others_active++; 2863b0f8d2eSBrian Somers 2873b0f8d2eSBrian Somers if (!others_active) { 288dd7e2610SBrian Somers fsm_Down(&bundle->ncp.ipcp.fsm); 289dd7e2610SBrian Somers fsm_Close(&bundle->ncp.ipcp.fsm); /* ST_INITIAL please */ 2906d666775SBrian Somers } 2913b0f8d2eSBrian Somers } 2923b0f8d2eSBrian Somers } 2936d666775SBrian Somers 2947a6f8720SBrian Somers int 2957a6f8720SBrian Somers bundle_LinkIsUp(const struct bundle *bundle) 2967a6f8720SBrian Somers { 2975828db6dSBrian Somers return bundle->ncp.ipcp.fsm.state == ST_OPENED; 2987a6f8720SBrian Somers } 2997a6f8720SBrian Somers 3007a6f8720SBrian Somers void 3013006ec67SBrian Somers bundle_Close(struct bundle *bundle, const char *name, int staydown) 3027a6f8720SBrian Somers { 303455aabc3SBrian Somers /* 3043006ec67SBrian Somers * Please close the given datalink. 305dd7e2610SBrian Somers * If name == NULL or name is the last datalink, fsm_Close all NCPs 3066f384573SBrian Somers * (except our MP) 3073b0f8d2eSBrian Somers * If it isn't the last datalink, just Close that datalink. 308455aabc3SBrian Somers */ 3097a6f8720SBrian Somers 3103b0f8d2eSBrian Somers struct datalink *dl, *this_dl; 3113b0f8d2eSBrian Somers int others_active; 3123006ec67SBrian Somers 3133b0f8d2eSBrian Somers if (bundle->phase == PHASE_TERMINATE || bundle->phase == PHASE_DEAD) 3143b0f8d2eSBrian Somers return; 3153b0f8d2eSBrian Somers 3163b0f8d2eSBrian Somers others_active = 0; 3173b0f8d2eSBrian Somers this_dl = NULL; 3183b0f8d2eSBrian Somers 3193b0f8d2eSBrian Somers for (dl = bundle->links; dl; dl = dl->next) { 3203b0f8d2eSBrian Somers if (name && !strcasecmp(name, dl->name)) 3213b0f8d2eSBrian Somers this_dl = dl; 3223b0f8d2eSBrian Somers if (name == NULL || this_dl == dl) { 323d345321bSBrian Somers if (staydown) 3243006ec67SBrian Somers datalink_StayDown(dl); 3253b0f8d2eSBrian Somers } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 3263b0f8d2eSBrian Somers others_active++; 3273b0f8d2eSBrian Somers } 3283b0f8d2eSBrian Somers 3293b0f8d2eSBrian Somers if (name && this_dl == NULL) { 330dd7e2610SBrian Somers log_Printf(LogWARN, "%s: Invalid datalink name\n", name); 3313b0f8d2eSBrian Somers return; 3323b0f8d2eSBrian Somers } 3333b0f8d2eSBrian Somers 3343b0f8d2eSBrian Somers if (!others_active) { 3353b0f8d2eSBrian Somers if (bundle->ncp.ipcp.fsm.state > ST_CLOSED || 3363b0f8d2eSBrian Somers bundle->ncp.ipcp.fsm.state == ST_STARTING) 337dd7e2610SBrian Somers fsm_Close(&bundle->ncp.ipcp.fsm); 3383b0f8d2eSBrian Somers else { 3395828db6dSBrian Somers if (bundle->ncp.ipcp.fsm.state > ST_INITIAL) { 340dd7e2610SBrian Somers fsm_Close(&bundle->ncp.ipcp.fsm); 341dd7e2610SBrian Somers fsm_Down(&bundle->ncp.ipcp.fsm); 342d2fd8d77SBrian Somers } 343d345321bSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 344d345321bSBrian Somers datalink_Close(dl, staydown); 3457a6f8720SBrian Somers } 3463b0f8d2eSBrian Somers } else if (this_dl && this_dl->state != DATALINK_CLOSED && 3473b0f8d2eSBrian Somers this_dl->state != DATALINK_HANGUP) 3483b0f8d2eSBrian Somers datalink_Close(this_dl, staydown); 349d2fd8d77SBrian Somers } 3507a6f8720SBrian Somers 3511bc9b5baSBrian Somers void 3521bc9b5baSBrian Somers bundle_Down(struct bundle *bundle) 3531bc9b5baSBrian Somers { 3541bc9b5baSBrian Somers struct datalink *dl; 3551bc9b5baSBrian Somers 3561bc9b5baSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 3571bc9b5baSBrian Somers datalink_Down(dl, 1); 3581bc9b5baSBrian Somers } 3591bc9b5baSBrian Somers 3602f786681SBrian Somers static int 3612f786681SBrian Somers bundle_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n) 3622f786681SBrian Somers { 3632f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d); 3642f786681SBrian Somers struct datalink *dl; 365b6217683SBrian Somers struct descriptor *desc; 3662f786681SBrian Somers int result; 3672f786681SBrian Somers 3682f786681SBrian Somers result = 0; 3692f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 3702f786681SBrian Somers result += descriptor_UpdateSet(&dl->desc, r, w, e, n); 3712f786681SBrian Somers 372b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 373b6217683SBrian Somers result += descriptor_UpdateSet(desc, r, w, e, n); 374b6217683SBrian Somers 375078c562eSBrian Somers /* If there are aren't many packets queued, look for some more. */ 37624989c68SBrian Somers if (r && bundle->links && bundle_FillQueues(bundle) < 20) { 377faefde08SBrian Somers if (*n < bundle->dev.fd + 1) 378faefde08SBrian Somers *n = bundle->dev.fd + 1; 379faefde08SBrian Somers FD_SET(bundle->dev.fd, r); 38024989c68SBrian Somers log_Printf(LogTIMER, "tun: fdset(r) %d\n", bundle->dev.fd); 381078c562eSBrian Somers result++; 382078c562eSBrian Somers } 383078c562eSBrian Somers 384ea722969SBrian Somers /* 385ea722969SBrian Somers * This *MUST* be called after the datalink UpdateSet()s as it 386ea722969SBrian Somers * might be ``holding'' one of the datalinks and wants to be 387ea722969SBrian Somers * able to de-select() from the descriptor set 388ea722969SBrian Somers */ 389ea722969SBrian Somers descriptor_UpdateSet(&bundle->ncp.mp.server.desc, r, w, e, n); 390ea722969SBrian Somers 3912f786681SBrian Somers return result; 3922f786681SBrian Somers } 3932f786681SBrian Somers 3942f786681SBrian Somers static int 3952f786681SBrian Somers bundle_IsSet(struct descriptor *d, const fd_set *fdset) 3962f786681SBrian Somers { 3972f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d); 3982f786681SBrian Somers struct datalink *dl; 399b6217683SBrian Somers struct descriptor *desc; 4002f786681SBrian Somers 4012f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 4022f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 4032f786681SBrian Somers return 1; 4042f786681SBrian Somers 405b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 406b6217683SBrian Somers if (descriptor_IsSet(desc, fdset)) 407b6217683SBrian Somers return 1; 408b6217683SBrian Somers 409332b9de0SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 410332b9de0SBrian Somers return 1; 411332b9de0SBrian Somers 412faefde08SBrian Somers return FD_ISSET(bundle->dev.fd, fdset); 4132f786681SBrian Somers } 4142f786681SBrian Somers 4152f786681SBrian Somers static void 4162f786681SBrian Somers bundle_DescriptorRead(struct descriptor *d, struct bundle *bundle, 4172f786681SBrian Somers const fd_set *fdset) 4182f786681SBrian Somers { 4192f786681SBrian Somers struct datalink *dl; 420b6217683SBrian Somers struct descriptor *desc; 4212f786681SBrian Somers 422ea722969SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 423ea722969SBrian Somers descriptor_Read(&bundle->ncp.mp.server.desc, bundle, fdset); 424ea722969SBrian Somers 4252f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 4262f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 4272f786681SBrian Somers descriptor_Read(&dl->desc, bundle, fdset); 428b6217683SBrian Somers 429b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 430b6217683SBrian Somers if (descriptor_IsSet(desc, fdset)) 431b6217683SBrian Somers descriptor_Read(desc, bundle, fdset); 432078c562eSBrian Somers 433faefde08SBrian Somers if (FD_ISSET(bundle->dev.fd, fdset)) { 434078c562eSBrian Somers struct tun_data tun; 435078c562eSBrian Somers int n, pri; 436078c562eSBrian Somers 437078c562eSBrian Somers /* something to read from tun */ 438faefde08SBrian Somers n = read(bundle->dev.fd, &tun, sizeof tun); 439078c562eSBrian Somers if (n < 0) { 440078c562eSBrian Somers log_Printf(LogERROR, "read from tun: %s\n", strerror(errno)); 441078c562eSBrian Somers return; 442078c562eSBrian Somers } 443078c562eSBrian Somers n -= sizeof tun - sizeof tun.data; 444078c562eSBrian Somers if (n <= 0) { 445078c562eSBrian Somers log_Printf(LogERROR, "read from tun: Only %d bytes read\n", n); 446078c562eSBrian Somers return; 447078c562eSBrian Somers } 448078c562eSBrian Somers if (!tun_check_header(tun, AF_INET)) 449078c562eSBrian Somers return; 450078c562eSBrian Somers 451078c562eSBrian Somers if (((struct ip *)tun.data)->ip_dst.s_addr == 452078c562eSBrian Somers bundle->ncp.ipcp.my_ip.s_addr) { 453078c562eSBrian Somers /* we've been asked to send something addressed *to* us :( */ 454078c562eSBrian Somers if (Enabled(bundle, OPT_LOOPBACK)) { 455078c562eSBrian Somers pri = PacketCheck(bundle, tun.data, n, &bundle->filter.in); 456078c562eSBrian Somers if (pri >= 0) { 457078c562eSBrian Somers struct mbuf *bp; 458078c562eSBrian Somers 459078c562eSBrian Somers #ifndef NOALIAS 460078c562eSBrian Somers if (alias_IsEnabled()) { 461078c562eSBrian Somers (*PacketAlias.In)(tun.data, sizeof tun.data); 462078c562eSBrian Somers n = ntohs(((struct ip *)tun.data)->ip_len); 463078c562eSBrian Somers } 464078c562eSBrian Somers #endif 465078c562eSBrian Somers bp = mbuf_Alloc(n, MB_IPIN); 466078c562eSBrian Somers memcpy(MBUF_CTOP(bp), tun.data, n); 467078c562eSBrian Somers ip_Input(bundle, bp); 468078c562eSBrian Somers log_Printf(LogDEBUG, "Looped back packet addressed to myself\n"); 469078c562eSBrian Somers } 470078c562eSBrian Somers return; 471078c562eSBrian Somers } else 472078c562eSBrian Somers log_Printf(LogDEBUG, "Oops - forwarding packet addressed to myself\n"); 473078c562eSBrian Somers } 474078c562eSBrian Somers 475078c562eSBrian Somers /* 476078c562eSBrian Somers * Process on-demand dialup. Output packets are queued within tunnel 477078c562eSBrian Somers * device until IPCP is opened. 478078c562eSBrian Somers */ 479078c562eSBrian Somers 480078c562eSBrian Somers if (bundle_Phase(bundle) == PHASE_DEAD) { 481078c562eSBrian Somers /* 482078c562eSBrian Somers * Note, we must be in AUTO mode :-/ otherwise our interface should 483078c562eSBrian Somers * *not* be UP and we can't receive data 484078c562eSBrian Somers */ 485078c562eSBrian Somers if ((pri = PacketCheck(bundle, tun.data, n, &bundle->filter.dial)) >= 0) 486078c562eSBrian Somers bundle_Open(bundle, NULL, PHYS_DEMAND); 487078c562eSBrian Somers else 488078c562eSBrian Somers /* 489078c562eSBrian Somers * Drop the packet. If we were to queue it, we'd just end up with 490078c562eSBrian Somers * a pile of timed-out data in our output queue by the time we get 491078c562eSBrian Somers * around to actually dialing. We'd also prematurely reach the 492078c562eSBrian Somers * threshold at which we stop select()ing to read() the tun 493078c562eSBrian Somers * device - breaking auto-dial. 494078c562eSBrian Somers */ 495078c562eSBrian Somers return; 496078c562eSBrian Somers } 497078c562eSBrian Somers 498078c562eSBrian Somers pri = PacketCheck(bundle, tun.data, n, &bundle->filter.out); 499078c562eSBrian Somers if (pri >= 0) { 500078c562eSBrian Somers #ifndef NOALIAS 501078c562eSBrian Somers if (alias_IsEnabled()) { 502078c562eSBrian Somers (*PacketAlias.Out)(tun.data, sizeof tun.data); 503078c562eSBrian Somers n = ntohs(((struct ip *)tun.data)->ip_len); 504078c562eSBrian Somers } 505078c562eSBrian Somers #endif 506078c562eSBrian Somers ip_Enqueue(pri, tun.data, n); 507078c562eSBrian Somers } 508078c562eSBrian Somers } 5092f786681SBrian Somers } 5102f786681SBrian Somers 5112f786681SBrian Somers static void 5122f786681SBrian Somers bundle_DescriptorWrite(struct descriptor *d, struct bundle *bundle, 5132f786681SBrian Somers const fd_set *fdset) 5142f786681SBrian Somers { 5152f786681SBrian Somers struct datalink *dl; 516b6217683SBrian Somers struct descriptor *desc; 5172f786681SBrian Somers 518ea722969SBrian Somers /* This is not actually necessary as struct mpserver doesn't Write() */ 519ea722969SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 520ea722969SBrian Somers descriptor_Write(&bundle->ncp.mp.server.desc, bundle, fdset); 521ea722969SBrian Somers 5222f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 5232f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 5242f786681SBrian Somers descriptor_Write(&dl->desc, bundle, fdset); 525b6217683SBrian Somers 526b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 527b6217683SBrian Somers if (descriptor_IsSet(desc, fdset)) 528b6217683SBrian Somers descriptor_Write(desc, bundle, fdset); 5292f786681SBrian Somers } 5302f786681SBrian Somers 5317a6f8720SBrian Somers 5327a6f8720SBrian Somers struct bundle * 533565e35e5SBrian Somers bundle_Create(const char *prefix, struct prompt *prompt, int type) 5347a6f8720SBrian Somers { 5357a6f8720SBrian Somers int s, enoentcount, err; 5367a6f8720SBrian Somers struct ifreq ifrq; 5377a6f8720SBrian Somers static struct bundle bundle; /* there can be only one */ 5387a6f8720SBrian Somers 539faefde08SBrian Somers if (bundle.ifp.Name != NULL) { /* Already allocated ! */ 540dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_Create: There's only one BUNDLE !\n"); 5417a6f8720SBrian Somers return NULL; 5427a6f8720SBrian Somers } 5437a6f8720SBrian Somers 5447a6f8720SBrian Somers err = ENOENT; 5457a6f8720SBrian Somers enoentcount = 0; 546107d62e7SBrian Somers for (bundle.unit = 0; ; bundle.unit++) { 547faefde08SBrian Somers snprintf(bundle.dev.Name, sizeof bundle.dev.Name, "%s%d", 548faefde08SBrian Somers prefix, bundle.unit); 549faefde08SBrian Somers bundle.dev.fd = ID0open(bundle.dev.Name, O_RDWR); 550faefde08SBrian Somers if (bundle.dev.fd >= 0) 5517a6f8720SBrian Somers break; 552107d62e7SBrian Somers else if (errno == ENXIO) { 5537a6f8720SBrian Somers err = errno; 554107d62e7SBrian Somers break; 5557a6f8720SBrian Somers } else if (errno == ENOENT) { 5567a6f8720SBrian Somers if (++enoentcount > 2) 557107d62e7SBrian Somers break; 5587a6f8720SBrian Somers } else 5597a6f8720SBrian Somers err = errno; 5607a6f8720SBrian Somers } 5617a6f8720SBrian Somers 562faefde08SBrian Somers if (bundle.dev.fd < 0) { 563dd7e2610SBrian Somers log_Printf(LogWARN, "No available tunnel devices found (%s).\n", 56485b542cfSBrian Somers strerror(err)); 5657a6f8720SBrian Somers return NULL; 5667a6f8720SBrian Somers } 5677a6f8720SBrian Somers 568dd7e2610SBrian Somers log_SetTun(bundle.unit); 5697a6f8720SBrian Somers 5707a6f8720SBrian Somers s = socket(AF_INET, SOCK_DGRAM, 0); 5717a6f8720SBrian Somers if (s < 0) { 572dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_Create: socket(): %s\n", strerror(errno)); 573faefde08SBrian Somers close(bundle.dev.fd); 5747a6f8720SBrian Somers return NULL; 5757a6f8720SBrian Somers } 5767a6f8720SBrian Somers 577faefde08SBrian Somers bundle.ifp.Name = strrchr(bundle.dev.Name, '/'); 578faefde08SBrian Somers if (bundle.ifp.Name == NULL) 579faefde08SBrian Somers bundle.ifp.Name = bundle.dev.Name; 5807a6f8720SBrian Somers else 581faefde08SBrian Somers bundle.ifp.Name++; 5827a6f8720SBrian Somers 5837a6f8720SBrian Somers /* 5847a6f8720SBrian Somers * Now, bring up the interface. 5857a6f8720SBrian Somers */ 5867a6f8720SBrian Somers memset(&ifrq, '\0', sizeof ifrq); 587faefde08SBrian Somers strncpy(ifrq.ifr_name, bundle.ifp.Name, sizeof ifrq.ifr_name - 1); 5887a6f8720SBrian Somers ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 5897a6f8720SBrian Somers if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) { 590dd7e2610SBrian Somers log_Printf(LogERROR, "OpenTunnel: ioctl(SIOCGIFFLAGS): %s\n", 5917a6f8720SBrian Somers strerror(errno)); 5927a6f8720SBrian Somers close(s); 593faefde08SBrian Somers close(bundle.dev.fd); 594faefde08SBrian Somers bundle.ifp.Name = NULL; 5957a6f8720SBrian Somers return NULL; 5967a6f8720SBrian Somers } 5977a6f8720SBrian Somers ifrq.ifr_flags |= IFF_UP; 5987a6f8720SBrian Somers if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) { 599dd7e2610SBrian Somers log_Printf(LogERROR, "OpenTunnel: ioctl(SIOCSIFFLAGS): %s\n", 6007a6f8720SBrian Somers strerror(errno)); 6017a6f8720SBrian Somers close(s); 602faefde08SBrian Somers close(bundle.dev.fd); 603faefde08SBrian Somers bundle.ifp.Name = NULL; 6047a6f8720SBrian Somers return NULL; 6057a6f8720SBrian Somers } 6067a6f8720SBrian Somers 6077a6f8720SBrian Somers close(s); 6087a6f8720SBrian Somers 609faefde08SBrian Somers if ((bundle.ifp.Index = GetIfIndex(bundle.ifp.Name)) < 0) { 610faefde08SBrian Somers log_Printf(LogERROR, "OpenTunnel: Can't find interface index.\n"); 611faefde08SBrian Somers close(bundle.dev.fd); 612faefde08SBrian Somers bundle.ifp.Name = NULL; 6137a6f8720SBrian Somers return NULL; 6147a6f8720SBrian Somers } 615faefde08SBrian Somers prompt_Printf(prompt, "Using interface: %s\n", bundle.ifp.Name); 616faefde08SBrian Somers log_Printf(LogPHASE, "Using interface: %s\n", bundle.ifp.Name); 6177a6f8720SBrian Somers 618faefde08SBrian Somers bundle.ifp.Speed = 0; 6197a6f8720SBrian Somers 620820de6ebSBrian Somers bundle.routing_seq = 0; 621a0cbd833SBrian Somers bundle.phase = PHASE_DEAD; 622a0cbd833SBrian Somers bundle.CleaningUp = 0; 6237a6f8720SBrian Somers 6246d666775SBrian Somers bundle.fsm.LayerStart = bundle_LayerStart; 6256f384573SBrian Somers bundle.fsm.LayerUp = bundle_LayerUp; 6266d666775SBrian Somers bundle.fsm.LayerDown = bundle_LayerDown; 6276d666775SBrian Somers bundle.fsm.LayerFinish = bundle_LayerFinish; 6286d666775SBrian Somers bundle.fsm.object = &bundle; 6297a6f8720SBrian Somers 630ab886ad0SBrian Somers bundle.cfg.idle_timeout = NCP_IDLE_TIMEOUT; 6311342caedSBrian Somers *bundle.cfg.auth.name = '\0'; 6321342caedSBrian Somers *bundle.cfg.auth.key = '\0'; 633610b185fSBrian Somers bundle.cfg.opt = OPT_SROUTES | OPT_IDCHECK | OPT_LOOPBACK | 634610b185fSBrian Somers OPT_THROUGHPUT | OPT_UTMP; 63549052c95SBrian Somers *bundle.cfg.label = '\0'; 63649052c95SBrian Somers bundle.cfg.mtu = DEF_MTU; 637565e35e5SBrian Somers bundle.phys_type = type; 638ab886ad0SBrian Somers 6396f384573SBrian Somers bundle.links = datalink_Create("deflink", &bundle, type); 6403006ec67SBrian Somers if (bundle.links == NULL) { 641dd7e2610SBrian Somers log_Printf(LogERROR, "Cannot create data link: %s\n", strerror(errno)); 642faefde08SBrian Somers close(bundle.dev.fd); 643faefde08SBrian Somers bundle.ifp.Name = NULL; 6442289f246SBrian Somers return NULL; 6452289f246SBrian Somers } 6462289f246SBrian Somers 6472f786681SBrian Somers bundle.desc.type = BUNDLE_DESCRIPTOR; 6482f786681SBrian Somers bundle.desc.next = NULL; 6492f786681SBrian Somers bundle.desc.UpdateSet = bundle_UpdateSet; 6502f786681SBrian Somers bundle.desc.IsSet = bundle_IsSet; 6512f786681SBrian Somers bundle.desc.Read = bundle_DescriptorRead; 6522f786681SBrian Somers bundle.desc.Write = bundle_DescriptorWrite; 6532f786681SBrian Somers 65449052c95SBrian Somers mp_Init(&bundle.ncp.mp, &bundle); 65549052c95SBrian Somers 65649052c95SBrian Somers /* Send over the first physical link by default */ 6575828db6dSBrian Somers ipcp_Init(&bundle.ncp.ipcp, &bundle, &bundle.links->physical->link, 6585828db6dSBrian Somers &bundle.fsm); 6596d666775SBrian Somers 6605ca5389aSBrian Somers memset(&bundle.filter, '\0', sizeof bundle.filter); 6615ca5389aSBrian Somers bundle.filter.in.fragok = bundle.filter.in.logok = 1; 6625ca5389aSBrian Somers bundle.filter.in.name = "IN"; 6635ca5389aSBrian Somers bundle.filter.out.fragok = bundle.filter.out.logok = 1; 6645ca5389aSBrian Somers bundle.filter.out.name = "OUT"; 6655ca5389aSBrian Somers bundle.filter.dial.name = "DIAL"; 6668390b576SBrian Somers bundle.filter.dial.logok = 1; 6675ca5389aSBrian Somers bundle.filter.alive.name = "ALIVE"; 6685ca5389aSBrian Somers bundle.filter.alive.logok = 1; 66993ee0ff2SBrian Somers memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer); 67093ee0ff2SBrian Somers bundle.idle.done = 0; 6715cf4388bSBrian Somers bundle.notify.fd = -1; 67293ee0ff2SBrian Somers 6736d666775SBrian Somers /* Clean out any leftover crud */ 6746d666775SBrian Somers bundle_CleanInterface(&bundle); 6756d666775SBrian Somers 67685602e52SBrian Somers if (prompt) { 67785602e52SBrian Somers /* Retrospectively introduce ourselves to the prompt */ 67885602e52SBrian Somers prompt->bundle = &bundle; 67985602e52SBrian Somers bundle_RegisterDescriptor(&bundle, &prompt->desc); 68085602e52SBrian Somers } 68185602e52SBrian Somers 6827a6f8720SBrian Somers return &bundle; 6837a6f8720SBrian Somers } 6847a6f8720SBrian Somers 68568a0f0ccSBrian Somers static void 68668a0f0ccSBrian Somers bundle_DownInterface(struct bundle *bundle) 68768a0f0ccSBrian Somers { 68868a0f0ccSBrian Somers struct ifreq ifrq; 68968a0f0ccSBrian Somers int s; 69068a0f0ccSBrian Somers 691dd7e2610SBrian Somers route_IfDelete(bundle, 1); 69268a0f0ccSBrian Somers 69368a0f0ccSBrian Somers s = ID0socket(AF_INET, SOCK_DGRAM, 0); 69468a0f0ccSBrian Somers if (s < 0) { 695dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_DownInterface: socket: %s\n", strerror(errno)); 69668a0f0ccSBrian Somers return; 69768a0f0ccSBrian Somers } 69868a0f0ccSBrian Somers 69968a0f0ccSBrian Somers memset(&ifrq, '\0', sizeof ifrq); 700faefde08SBrian Somers strncpy(ifrq.ifr_name, bundle->ifp.Name, sizeof ifrq.ifr_name - 1); 70168a0f0ccSBrian Somers ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 70268a0f0ccSBrian Somers if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) { 703dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_DownInterface: ioctl(SIOCGIFFLAGS): %s\n", 70468a0f0ccSBrian Somers strerror(errno)); 70568a0f0ccSBrian Somers close(s); 70668a0f0ccSBrian Somers return; 70768a0f0ccSBrian Somers } 70868a0f0ccSBrian Somers ifrq.ifr_flags &= ~IFF_UP; 70968a0f0ccSBrian Somers if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) { 710dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_DownInterface: ioctl(SIOCSIFFLAGS): %s\n", 71168a0f0ccSBrian Somers strerror(errno)); 71268a0f0ccSBrian Somers close(s); 71368a0f0ccSBrian Somers return; 71468a0f0ccSBrian Somers } 71568a0f0ccSBrian Somers close(s); 71668a0f0ccSBrian Somers } 71768a0f0ccSBrian Somers 71868a0f0ccSBrian Somers void 71968a0f0ccSBrian Somers bundle_Destroy(struct bundle *bundle) 72068a0f0ccSBrian Somers { 7213006ec67SBrian Somers struct datalink *dl; 722b6217683SBrian Somers struct descriptor *desc, *ndesc; 723b6217683SBrian Somers 724ea722969SBrian Somers /* 725ea722969SBrian Somers * Clean up the interface. We don't need to mp_Down(), 726ea722969SBrian Somers * ipcp_CleanInterface() and bundle_DownInterface() unless we're getting 727ea722969SBrian Somers * out under exceptional conditions such as a descriptor exception. 728ea722969SBrian Somers */ 72966f634b6SBrian Somers mp_Down(&bundle->ncp.mp); 730dd7e2610SBrian Somers ipcp_CleanInterface(&bundle->ncp.ipcp); 73168a0f0ccSBrian Somers bundle_DownInterface(bundle); 7323006ec67SBrian Somers 733ea722969SBrian Somers /* Again, these are all DATALINK_CLOSED unless we're abending */ 7343006ec67SBrian Somers dl = bundle->links; 7353006ec67SBrian Somers while (dl) 7363006ec67SBrian Somers dl = datalink_Destroy(dl); 7373006ec67SBrian Somers 738ea722969SBrian Somers /* In case we never made PHASE_NETWORK */ 7395cf4388bSBrian Somers bundle_Notify(bundle, EX_ERRDEAD); 740b6217683SBrian Somers 741ea722969SBrian Somers /* Finally, destroy our prompts */ 742b6217683SBrian Somers desc = bundle->desc.next; 743b6217683SBrian Somers while (desc) { 744b6217683SBrian Somers ndesc = desc->next; 745b6217683SBrian Somers if (desc->type == PROMPT_DESCRIPTOR) 746b6217683SBrian Somers prompt_Destroy((struct prompt *)desc, 1); 747b6217683SBrian Somers else 748dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_Destroy: Don't know how to delete descriptor" 749b6217683SBrian Somers " type %d\n", desc->type); 750b6217683SBrian Somers desc = ndesc; 751b6217683SBrian Somers } 752b6217683SBrian Somers bundle->desc.next = NULL; 753faefde08SBrian Somers bundle->ifp.Name = NULL; 75468a0f0ccSBrian Somers } 75568a0f0ccSBrian Somers 7567a6f8720SBrian Somers struct rtmsg { 7577a6f8720SBrian Somers struct rt_msghdr m_rtm; 7587a6f8720SBrian Somers char m_space[64]; 7597a6f8720SBrian Somers }; 7607a6f8720SBrian Somers 761610b185fSBrian Somers int 762820de6ebSBrian Somers bundle_SetRoute(struct bundle *bundle, int cmd, struct in_addr dst, 7637a6f8720SBrian Somers struct in_addr gateway, struct in_addr mask, int bang) 7647a6f8720SBrian Somers { 7657a6f8720SBrian Somers struct rtmsg rtmes; 7667a6f8720SBrian Somers int s, nb, wb; 7677a6f8720SBrian Somers char *cp; 7687a6f8720SBrian Somers const char *cmdstr; 7697a6f8720SBrian Somers struct sockaddr_in rtdata; 770610b185fSBrian Somers int result = 1; 7717a6f8720SBrian Somers 7727a6f8720SBrian Somers if (bang) 7737a6f8720SBrian Somers cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!"); 7747a6f8720SBrian Somers else 7757a6f8720SBrian Somers cmdstr = (cmd == RTM_ADD ? "Add" : "Delete"); 7767a6f8720SBrian Somers s = ID0socket(PF_ROUTE, SOCK_RAW, 0); 7777a6f8720SBrian Somers if (s < 0) { 778dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_SetRoute: socket(): %s\n", strerror(errno)); 779610b185fSBrian Somers return result; 7807a6f8720SBrian Somers } 7817a6f8720SBrian Somers memset(&rtmes, '\0', sizeof rtmes); 7827a6f8720SBrian Somers rtmes.m_rtm.rtm_version = RTM_VERSION; 7837a6f8720SBrian Somers rtmes.m_rtm.rtm_type = cmd; 7847a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs = RTA_DST; 785820de6ebSBrian Somers rtmes.m_rtm.rtm_seq = ++bundle->routing_seq; 7867a6f8720SBrian Somers rtmes.m_rtm.rtm_pid = getpid(); 7877a6f8720SBrian Somers rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC; 7887a6f8720SBrian Somers 7897a6f8720SBrian Somers memset(&rtdata, '\0', sizeof rtdata); 79050e5c17dSBrian Somers rtdata.sin_len = sizeof rtdata; 7917a6f8720SBrian Somers rtdata.sin_family = AF_INET; 7927a6f8720SBrian Somers rtdata.sin_port = 0; 7937a6f8720SBrian Somers rtdata.sin_addr = dst; 7947a6f8720SBrian Somers 7957a6f8720SBrian Somers cp = rtmes.m_space; 79650e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 79750e5c17dSBrian Somers cp += rtdata.sin_len; 798e43ebac1SBrian Somers if (cmd == RTM_ADD) { 7997a6f8720SBrian Somers if (gateway.s_addr == INADDR_ANY) { 8007a6f8720SBrian Somers /* Add a route through the interface */ 8017a6f8720SBrian Somers struct sockaddr_dl dl; 8027a6f8720SBrian Somers const char *iname; 8037a6f8720SBrian Somers int ilen; 8047a6f8720SBrian Somers 805faefde08SBrian Somers iname = Index2Nam(bundle->ifp.Index); 8067a6f8720SBrian Somers ilen = strlen(iname); 8077a6f8720SBrian Somers dl.sdl_len = sizeof dl - sizeof dl.sdl_data + ilen; 8087a6f8720SBrian Somers dl.sdl_family = AF_LINK; 809faefde08SBrian Somers dl.sdl_index = bundle->ifp.Index; 8107a6f8720SBrian Somers dl.sdl_type = 0; 8117a6f8720SBrian Somers dl.sdl_nlen = ilen; 8127a6f8720SBrian Somers dl.sdl_alen = 0; 8137a6f8720SBrian Somers dl.sdl_slen = 0; 8147a6f8720SBrian Somers strncpy(dl.sdl_data, iname, sizeof dl.sdl_data); 8157a6f8720SBrian Somers memcpy(cp, &dl, dl.sdl_len); 8167a6f8720SBrian Somers cp += dl.sdl_len; 8177a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 8187a6f8720SBrian Somers } else { 8197a6f8720SBrian Somers rtdata.sin_addr = gateway; 82050e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 82150e5c17dSBrian Somers cp += rtdata.sin_len; 8227a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 8237a6f8720SBrian Somers } 824e43ebac1SBrian Somers } 8257a6f8720SBrian Somers 8267a6f8720SBrian Somers if (dst.s_addr == INADDR_ANY) 8277a6f8720SBrian Somers mask.s_addr = INADDR_ANY; 8287a6f8720SBrian Somers 8297a6f8720SBrian Somers if (cmd == RTM_ADD || dst.s_addr == INADDR_ANY) { 8307a6f8720SBrian Somers rtdata.sin_addr = mask; 83150e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 83250e5c17dSBrian Somers cp += rtdata.sin_len; 8337a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_NETMASK; 8347a6f8720SBrian Somers } 8357a6f8720SBrian Somers 8367a6f8720SBrian Somers nb = cp - (char *) &rtmes; 8377a6f8720SBrian Somers rtmes.m_rtm.rtm_msglen = nb; 8387a6f8720SBrian Somers wb = ID0write(s, &rtmes, nb); 8397a6f8720SBrian Somers if (wb < 0) { 840dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute failure:\n"); 841dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Cmd = %s\n", cmdstr); 842dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Dst = %s\n", inet_ntoa(dst)); 843dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Gateway = %s\n", inet_ntoa(gateway)); 844dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Mask = %s\n", inet_ntoa(mask)); 8457a6f8720SBrian Somers failed: 8467a6f8720SBrian Somers if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST || 847e43ebac1SBrian Somers (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) { 848610b185fSBrian Somers if (!bang) { 849dd7e2610SBrian Somers log_Printf(LogWARN, "Add route failed: %s already exists\n", 8507a6f8720SBrian Somers inet_ntoa(dst)); 851610b185fSBrian Somers result = 0; /* Don't add to our dynamic list */ 852610b185fSBrian Somers } else { 8537a6f8720SBrian Somers rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE; 8547a6f8720SBrian Somers if ((wb = ID0write(s, &rtmes, nb)) < 0) 8557a6f8720SBrian Somers goto failed; 8567a6f8720SBrian Somers } 857e43ebac1SBrian Somers } else if (cmd == RTM_DELETE && 8587a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == ESRCH || 8597a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) { 8607a6f8720SBrian Somers if (!bang) 861dd7e2610SBrian Somers log_Printf(LogWARN, "Del route failed: %s: Non-existent\n", 8627a6f8720SBrian Somers inet_ntoa(dst)); 8637a6f8720SBrian Somers } else if (rtmes.m_rtm.rtm_errno == 0) 864dd7e2610SBrian Somers log_Printf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr, 8657a6f8720SBrian Somers inet_ntoa(dst), strerror(errno)); 8667a6f8720SBrian Somers else 867dd7e2610SBrian Somers log_Printf(LogWARN, "%s route failed: %s: %s\n", 8687a6f8720SBrian Somers cmdstr, inet_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno)); 8697a6f8720SBrian Somers } 870dd7e2610SBrian Somers log_Printf(LogDEBUG, "wrote %d: cmd = %s, dst = %x, gateway = %x\n", 871fe3125a0SBrian Somers wb, cmdstr, (unsigned)dst.s_addr, (unsigned)gateway.s_addr); 8727a6f8720SBrian Somers close(s); 873610b185fSBrian Somers 874610b185fSBrian Somers return result; 8757a6f8720SBrian Somers } 87683d1af55SBrian Somers 87783d1af55SBrian Somers void 8783006ec67SBrian Somers bundle_LinkClosed(struct bundle *bundle, struct datalink *dl) 8793006ec67SBrian Somers { 8803006ec67SBrian Somers /* 8813006ec67SBrian Somers * Our datalink has closed. 882ea722969SBrian Somers * CleanDatalinks() (called from DoLoop()) will remove closed 883ea722969SBrian Somers * 1OFF and DIRECT links. 8843b0f8d2eSBrian Somers * If it's the last data link, enter phase DEAD. 885ea722969SBrian Somers * 886ea722969SBrian Somers * NOTE: dl may not be in our list (bundle_SendDatalink()) ! 8873006ec67SBrian Somers */ 8885b8b8060SBrian Somers 8893b0f8d2eSBrian Somers struct datalink *odl; 8903b0f8d2eSBrian Somers int other_links; 8915b8b8060SBrian Somers 8923b0f8d2eSBrian Somers other_links = 0; 8933b0f8d2eSBrian Somers for (odl = bundle->links; odl; odl = odl->next) 8943b0f8d2eSBrian Somers if (odl != dl && odl->state != DATALINK_CLOSED) 8953b0f8d2eSBrian Somers other_links++; 8963b0f8d2eSBrian Somers 8973b0f8d2eSBrian Somers if (!other_links) { 89826afeaa2SBrian Somers if (bundle->ncp.ipcp.fsm.state > ST_CLOSED || 89926afeaa2SBrian Somers bundle->ncp.ipcp.fsm.state == ST_STARTING) { 900dd7e2610SBrian Somers fsm_Down(&bundle->ncp.ipcp.fsm); 901dd7e2610SBrian Somers fsm_Close(&bundle->ncp.ipcp.fsm); /* ST_INITIAL please */ 90226afeaa2SBrian Somers } 903ea722969SBrian Somers bundle_DownInterface(bundle); 9045563ebdeSBrian Somers bundle_NewPhase(bundle, PHASE_DEAD); 905b6217683SBrian Somers bundle_DisplayPrompt(bundle); 9063b0f8d2eSBrian Somers } 907455aabc3SBrian Somers } 908455aabc3SBrian Somers 909455aabc3SBrian Somers void 910565e35e5SBrian Somers bundle_Open(struct bundle *bundle, const char *name, int mask) 9113006ec67SBrian Somers { 9123006ec67SBrian Somers /* 9133006ec67SBrian Somers * Please open the given datalink, or all if name == NULL 9143006ec67SBrian Somers */ 9153006ec67SBrian Somers struct datalink *dl; 9163006ec67SBrian Somers 9173006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 9183006ec67SBrian Somers if (name == NULL || !strcasecmp(dl->name, name)) { 919565e35e5SBrian Somers if (mask & dl->physical->type) 920565e35e5SBrian Somers datalink_Up(dl, 1, 1); 9213006ec67SBrian Somers if (name != NULL) 9223006ec67SBrian Somers break; 9233006ec67SBrian Somers } 9243006ec67SBrian Somers } 9253006ec67SBrian Somers 9263006ec67SBrian Somers struct datalink * 9273006ec67SBrian Somers bundle2datalink(struct bundle *bundle, const char *name) 9283006ec67SBrian Somers { 9293006ec67SBrian Somers struct datalink *dl; 9303006ec67SBrian Somers 9313006ec67SBrian Somers if (name != NULL) { 9323006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 9333006ec67SBrian Somers if (!strcasecmp(dl->name, name)) 9343006ec67SBrian Somers return dl; 9353006ec67SBrian Somers } else if (bundle->links && !bundle->links->next) 9363006ec67SBrian Somers return bundle->links; 9373006ec67SBrian Somers 9383006ec67SBrian Somers return NULL; 9393006ec67SBrian Somers } 9403006ec67SBrian Somers 9413006ec67SBrian Somers int 9423006ec67SBrian Somers bundle_FillQueues(struct bundle *bundle) 9433006ec67SBrian Somers { 9443b0f8d2eSBrian Somers int total; 9453b0f8d2eSBrian Somers 9461bc9b5baSBrian Somers if (bundle->ncp.mp.active) 9473b0f8d2eSBrian Somers total = mp_FillQueues(bundle); 9481bc9b5baSBrian Somers else { 9491bc9b5baSBrian Somers struct datalink *dl; 9501bc9b5baSBrian Somers int add; 9511bc9b5baSBrian Somers 9521bc9b5baSBrian Somers for (total = 0, dl = bundle->links; dl; dl = dl->next) 9531bc9b5baSBrian Somers if (dl->state == DATALINK_OPEN) { 9541bc9b5baSBrian Somers add = link_QueueLen(&dl->physical->link); 9551bc9b5baSBrian Somers if (add == 0 && dl->physical->out == NULL) 9561bc9b5baSBrian Somers add = ip_FlushPacket(&dl->physical->link, bundle); 9571bc9b5baSBrian Somers total += add; 9581bc9b5baSBrian Somers } 9593006ec67SBrian Somers } 9603006ec67SBrian Somers 9613b0f8d2eSBrian Somers return total + ip_QueueLen(); 9623006ec67SBrian Somers } 963aef795ccSBrian Somers 964aef795ccSBrian Somers int 965aef795ccSBrian Somers bundle_ShowLinks(struct cmdargs const *arg) 966aef795ccSBrian Somers { 967aef795ccSBrian Somers struct datalink *dl; 968aef795ccSBrian Somers 9699c53a7b1SBrian Somers for (dl = arg->bundle->links; dl; dl = dl->next) { 9709c53a7b1SBrian Somers prompt_Printf(arg->prompt, "Name: %s [%s]", dl->name, datalink_State(dl)); 971eeab6bf5SBrian Somers if (dl->physical->link.throughput.rolling && dl->state == DATALINK_OPEN) 972eeab6bf5SBrian Somers prompt_Printf(arg->prompt, " (weight %d, %d bytes/sec)", 973eeab6bf5SBrian Somers dl->mp.weight, 9749c53a7b1SBrian Somers dl->physical->link.throughput.OctetsPerSecond); 9759c53a7b1SBrian Somers prompt_Printf(arg->prompt, "\n"); 9769c53a7b1SBrian Somers } 977aef795ccSBrian Somers 978aef795ccSBrian Somers return 0; 979aef795ccSBrian Somers } 980ab886ad0SBrian Somers 9811342caedSBrian Somers static const char * 9821342caedSBrian Somers optval(struct bundle *bundle, int bit) 9831342caedSBrian Somers { 9841342caedSBrian Somers return (bundle->cfg.opt & bit) ? "enabled" : "disabled"; 9851342caedSBrian Somers } 9861342caedSBrian Somers 987c08717dfSBrian Somers int 988c08717dfSBrian Somers bundle_ShowStatus(struct cmdargs const *arg) 989c08717dfSBrian Somers { 990c08717dfSBrian Somers int remaining; 991c08717dfSBrian Somers 992c08717dfSBrian Somers prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle)); 993faefde08SBrian Somers prompt_Printf(arg->prompt, " Device: %s\n", arg->bundle->dev.Name); 994faefde08SBrian Somers prompt_Printf(arg->prompt, " Interface: %s @ %lubps\n", 995faefde08SBrian Somers arg->bundle->ifp.Name, arg->bundle->ifp.Speed); 996c08717dfSBrian Somers 997c08717dfSBrian Somers prompt_Printf(arg->prompt, "\nDefaults:\n"); 998643f4904SBrian Somers prompt_Printf(arg->prompt, " Label: %s\n", arg->bundle->cfg.label); 999610b185fSBrian Somers prompt_Printf(arg->prompt, " Auth name: %s\n", 1000610b185fSBrian Somers arg->bundle->cfg.auth.name); 1001c08717dfSBrian Somers prompt_Printf(arg->prompt, " Idle Timer: "); 1002c08717dfSBrian Somers if (arg->bundle->cfg.idle_timeout) { 1003c08717dfSBrian Somers prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle_timeout); 1004c08717dfSBrian Somers remaining = bundle_RemainingIdleTime(arg->bundle); 1005c08717dfSBrian Somers if (remaining != -1) 1006c08717dfSBrian Somers prompt_Printf(arg->prompt, " (%ds remaining)", remaining); 1007c08717dfSBrian Somers prompt_Printf(arg->prompt, "\n"); 1008c08717dfSBrian Somers } else 1009c08717dfSBrian Somers prompt_Printf(arg->prompt, "disabled\n"); 1010ce828a6eSBrian Somers prompt_Printf(arg->prompt, " MTU: "); 1011ce828a6eSBrian Somers if (arg->bundle->cfg.mtu) 1012ce828a6eSBrian Somers prompt_Printf(arg->prompt, "%d\n", arg->bundle->cfg.mtu); 1013ce828a6eSBrian Somers else 1014ce828a6eSBrian Somers prompt_Printf(arg->prompt, "unspecified\n"); 1015c08717dfSBrian Somers 1016610b185fSBrian Somers prompt_Printf(arg->prompt, " Sticky Routes: %s\n", 1017610b185fSBrian Somers optval(arg->bundle, OPT_SROUTES)); 10181342caedSBrian Somers prompt_Printf(arg->prompt, " ID check: %s\n", 10191342caedSBrian Somers optval(arg->bundle, OPT_IDCHECK)); 10201342caedSBrian Somers prompt_Printf(arg->prompt, " Loopback: %s\n", 10211342caedSBrian Somers optval(arg->bundle, OPT_LOOPBACK)); 10221342caedSBrian Somers prompt_Printf(arg->prompt, " PasswdAuth: %s\n", 10231342caedSBrian Somers optval(arg->bundle, OPT_PASSWDAUTH)); 10241342caedSBrian Somers prompt_Printf(arg->prompt, " Proxy: %s\n", 10251342caedSBrian Somers optval(arg->bundle, OPT_PROXY)); 10261342caedSBrian Somers prompt_Printf(arg->prompt, " Throughput: %s\n", 10271342caedSBrian Somers optval(arg->bundle, OPT_THROUGHPUT)); 1028610b185fSBrian Somers prompt_Printf(arg->prompt, " Utmp Logging: %s\n", 10291342caedSBrian Somers optval(arg->bundle, OPT_UTMP)); 10301342caedSBrian Somers 1031c08717dfSBrian Somers return 0; 1032c08717dfSBrian Somers } 1033c08717dfSBrian Somers 1034ab886ad0SBrian Somers static void 1035ab886ad0SBrian Somers bundle_IdleTimeout(void *v) 1036ab886ad0SBrian Somers { 1037ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v; 1038ab886ad0SBrian Somers 103993ee0ff2SBrian Somers bundle->idle.done = 0; 1040dd7e2610SBrian Somers log_Printf(LogPHASE, "Idle timer expired.\n"); 1041ab886ad0SBrian Somers bundle_Close(bundle, NULL, 1); 1042ab886ad0SBrian Somers } 1043ab886ad0SBrian Somers 1044ab886ad0SBrian Somers /* 1045ab886ad0SBrian Somers * Start Idle timer. If timeout is reached, we call bundle_Close() to 1046ab886ad0SBrian Somers * close LCP and link. 1047ab886ad0SBrian Somers */ 1048ab886ad0SBrian Somers void 1049ab886ad0SBrian Somers bundle_StartIdleTimer(struct bundle *bundle) 1050ab886ad0SBrian Somers { 1051ee084ab9SBrian Somers if (!(bundle->phys_type & (PHYS_DEDICATED|PHYS_PERM))) { 1052dd7e2610SBrian Somers timer_Stop(&bundle->idle.timer); 1053ee084ab9SBrian Somers if (bundle->cfg.idle_timeout) { 105493ee0ff2SBrian Somers bundle->idle.timer.func = bundle_IdleTimeout; 10553b0f8d2eSBrian Somers bundle->idle.timer.name = "idle"; 105693ee0ff2SBrian Somers bundle->idle.timer.load = bundle->cfg.idle_timeout * SECTICKS; 105793ee0ff2SBrian Somers bundle->idle.timer.arg = bundle; 1058dd7e2610SBrian Somers timer_Start(&bundle->idle.timer); 105993ee0ff2SBrian Somers bundle->idle.done = time(NULL) + bundle->cfg.idle_timeout; 1060ab886ad0SBrian Somers } 1061ab886ad0SBrian Somers } 1062ee084ab9SBrian Somers } 1063ab886ad0SBrian Somers 1064ab886ad0SBrian Somers void 1065ab886ad0SBrian Somers bundle_SetIdleTimer(struct bundle *bundle, int value) 1066ab886ad0SBrian Somers { 1067ab886ad0SBrian Somers bundle->cfg.idle_timeout = value; 1068ab886ad0SBrian Somers if (bundle_LinkIsUp(bundle)) 1069ab886ad0SBrian Somers bundle_StartIdleTimer(bundle); 1070ab886ad0SBrian Somers } 1071ab886ad0SBrian Somers 1072ab886ad0SBrian Somers void 1073ab886ad0SBrian Somers bundle_StopIdleTimer(struct bundle *bundle) 1074ab886ad0SBrian Somers { 1075dd7e2610SBrian Somers timer_Stop(&bundle->idle.timer); 10764a632c80SBrian Somers bundle->idle.done = 0; 1077ab886ad0SBrian Somers } 1078ab886ad0SBrian Somers 1079ab886ad0SBrian Somers int 1080ab886ad0SBrian Somers bundle_RemainingIdleTime(struct bundle *bundle) 1081ab886ad0SBrian Somers { 108293ee0ff2SBrian Somers if (bundle->idle.done) 108393ee0ff2SBrian Somers return bundle->idle.done - time(NULL); 1084ab886ad0SBrian Somers return -1; 1085ab886ad0SBrian Somers } 10863b0f8d2eSBrian Somers 10873b0f8d2eSBrian Somers int 10883b0f8d2eSBrian Somers bundle_IsDead(struct bundle *bundle) 10893b0f8d2eSBrian Somers { 10903b0f8d2eSBrian Somers return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp); 10913b0f8d2eSBrian Somers } 1092b6217683SBrian Somers 1093b6217683SBrian Somers void 1094b6217683SBrian Somers bundle_RegisterDescriptor(struct bundle *bundle, struct descriptor *d) 1095b6217683SBrian Somers { 1096b6217683SBrian Somers d->next = bundle->desc.next; 1097b6217683SBrian Somers bundle->desc.next = d; 1098b6217683SBrian Somers } 1099b6217683SBrian Somers 1100b6217683SBrian Somers void 1101b6217683SBrian Somers bundle_UnRegisterDescriptor(struct bundle *bundle, struct descriptor *d) 1102b6217683SBrian Somers { 1103b6217683SBrian Somers struct descriptor **desc; 1104b6217683SBrian Somers 1105b6217683SBrian Somers for (desc = &bundle->desc.next; *desc; desc = &(*desc)->next) 1106b6217683SBrian Somers if (*desc == d) { 1107b6217683SBrian Somers *desc = d->next; 1108b6217683SBrian Somers break; 1109b6217683SBrian Somers } 1110b6217683SBrian Somers } 1111b6217683SBrian Somers 1112b6217683SBrian Somers void 1113b6217683SBrian Somers bundle_DelPromptDescriptors(struct bundle *bundle, struct server *s) 1114b6217683SBrian Somers { 1115b6217683SBrian Somers struct descriptor **desc; 1116b6217683SBrian Somers struct prompt *p; 1117b6217683SBrian Somers 1118b6217683SBrian Somers desc = &bundle->desc.next; 1119b6217683SBrian Somers while (*desc) { 1120b6217683SBrian Somers if ((*desc)->type == PROMPT_DESCRIPTOR) { 1121b6217683SBrian Somers p = (struct prompt *)*desc; 1122b6217683SBrian Somers if (p->owner == s) { 1123b6217683SBrian Somers prompt_Destroy(p, 1); 1124b6217683SBrian Somers desc = &bundle->desc.next; 1125b6217683SBrian Somers continue; 1126b6217683SBrian Somers } 1127b6217683SBrian Somers } 1128b6217683SBrian Somers desc = &(*desc)->next; 1129b6217683SBrian Somers } 1130b6217683SBrian Somers } 1131b6217683SBrian Somers 1132b6217683SBrian Somers void 1133b6217683SBrian Somers bundle_DisplayPrompt(struct bundle *bundle) 1134b6217683SBrian Somers { 1135b6217683SBrian Somers struct descriptor **desc; 1136b6217683SBrian Somers 1137b6217683SBrian Somers for (desc = &bundle->desc.next; *desc; desc = &(*desc)->next) 1138b6217683SBrian Somers if ((*desc)->type == PROMPT_DESCRIPTOR) 1139b6217683SBrian Somers prompt_Required((struct prompt *)*desc); 1140b6217683SBrian Somers } 1141b6217683SBrian Somers 1142b6217683SBrian Somers void 1143b6217683SBrian Somers bundle_WriteTermPrompt(struct bundle *bundle, struct datalink *dl, 1144b6217683SBrian Somers const char *data, int len) 1145b6217683SBrian Somers { 1146b6217683SBrian Somers struct descriptor *desc; 1147b6217683SBrian Somers struct prompt *p; 1148b6217683SBrian Somers 1149b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 1150b6217683SBrian Somers if (desc->type == PROMPT_DESCRIPTOR) { 1151b6217683SBrian Somers p = (struct prompt *)desc; 1152b6217683SBrian Somers if (prompt_IsTermMode(p, dl)) 1153c3a119d0SBrian Somers prompt_Printf(p, "%.*s", len, data); 1154b6217683SBrian Somers } 1155b6217683SBrian Somers } 1156b6217683SBrian Somers 1157b6217683SBrian Somers void 1158b6217683SBrian Somers bundle_SetTtyCommandMode(struct bundle *bundle, struct datalink *dl) 1159b6217683SBrian Somers { 1160b6217683SBrian Somers struct descriptor *desc; 1161b6217683SBrian Somers struct prompt *p; 1162b6217683SBrian Somers 1163b6217683SBrian Somers for (desc = bundle->desc.next; desc; desc = desc->next) 1164b6217683SBrian Somers if (desc->type == PROMPT_DESCRIPTOR) { 1165b6217683SBrian Somers p = (struct prompt *)desc; 1166b6217683SBrian Somers if (prompt_IsTermMode(p, dl)) 1167b6217683SBrian Somers prompt_TtyCommandMode(p); 1168b6217683SBrian Somers } 1169b6217683SBrian Somers } 1170565e35e5SBrian Somers 1171565e35e5SBrian Somers static void 1172565e35e5SBrian Somers bundle_GenPhysType(struct bundle *bundle) 1173565e35e5SBrian Somers { 1174565e35e5SBrian Somers struct datalink *dl; 1175565e35e5SBrian Somers 1176565e35e5SBrian Somers bundle->phys_type = 0; 1177565e35e5SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 1178565e35e5SBrian Somers bundle->phys_type |= dl->physical->type; 1179565e35e5SBrian Somers } 1180565e35e5SBrian Somers 1181e8607d38SBrian Somers int 1182cd7bd93aSBrian Somers bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl, 1183cd7bd93aSBrian Somers const char *name) 1184cd7bd93aSBrian Somers { 1185e8607d38SBrian Somers struct datalink *ndl; 1186cd7bd93aSBrian Somers 1187e8607d38SBrian Somers ndl = bundle2datalink(bundle, name); 1188e8607d38SBrian Somers if (!ndl) { 1189e8607d38SBrian Somers ndl = datalink_Clone(dl, name); 1190cd7bd93aSBrian Somers ndl->next = dl->next; 1191cd7bd93aSBrian Somers dl->next = ndl; 1192565e35e5SBrian Somers bundle_GenPhysType(bundle); 1193e8607d38SBrian Somers return 1; 1194e8607d38SBrian Somers } 1195e8607d38SBrian Somers 1196e8607d38SBrian Somers log_Printf(LogWARN, "Clone: %s: name already exists\n", ndl->name); 1197e8607d38SBrian Somers return 0; 1198cd7bd93aSBrian Somers } 1199cd7bd93aSBrian Somers 1200cd7bd93aSBrian Somers void 1201cd7bd93aSBrian Somers bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl) 1202cd7bd93aSBrian Somers { 1203cd7bd93aSBrian Somers struct datalink **dlp; 1204cd7bd93aSBrian Somers 1205cd7bd93aSBrian Somers if (dl->state == DATALINK_CLOSED) 1206cd7bd93aSBrian Somers for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next) 1207cd7bd93aSBrian Somers if (*dlp == dl) { 1208cd7bd93aSBrian Somers *dlp = datalink_Destroy(dl); 1209cd7bd93aSBrian Somers break; 1210cd7bd93aSBrian Somers } 1211565e35e5SBrian Somers bundle_GenPhysType(bundle); 1212565e35e5SBrian Somers } 1213565e35e5SBrian Somers 1214565e35e5SBrian Somers void 1215565e35e5SBrian Somers bundle_CleanDatalinks(struct bundle *bundle) 1216565e35e5SBrian Somers { 1217565e35e5SBrian Somers struct datalink **dlp = &bundle->links; 1218565e35e5SBrian Somers 1219565e35e5SBrian Somers while (*dlp) 1220565e35e5SBrian Somers if ((*dlp)->state == DATALINK_CLOSED && 12216f384573SBrian Somers (*dlp)->physical->type & (PHYS_DIRECT|PHYS_1OFF)) 1222565e35e5SBrian Somers *dlp = datalink_Destroy(*dlp); 1223565e35e5SBrian Somers else 1224565e35e5SBrian Somers dlp = &(*dlp)->next; 1225565e35e5SBrian Somers bundle_GenPhysType(bundle); 1226cd7bd93aSBrian Somers } 122749052c95SBrian Somers 122849052c95SBrian Somers void 122949052c95SBrian Somers bundle_SetLabel(struct bundle *bundle, const char *label) 123049052c95SBrian Somers { 123149052c95SBrian Somers if (label) 123249052c95SBrian Somers strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1); 123349052c95SBrian Somers else 123449052c95SBrian Somers *bundle->cfg.label = '\0'; 123549052c95SBrian Somers } 123649052c95SBrian Somers 123749052c95SBrian Somers const char * 123849052c95SBrian Somers bundle_GetLabel(struct bundle *bundle) 123949052c95SBrian Somers { 124049052c95SBrian Somers return *bundle->cfg.label ? bundle->cfg.label : NULL; 124149052c95SBrian Somers } 12421fa665f5SBrian Somers 12431fa665f5SBrian Somers void 124496c9bb21SBrian Somers bundle_ReceiveDatalink(struct bundle *bundle, int s, struct sockaddr_un *sun) 12451fa665f5SBrian Somers { 124696c9bb21SBrian Somers char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int)]; 124796c9bb21SBrian Somers struct cmsghdr *cmsg = (struct cmsghdr *)cmsgbuf; 124896c9bb21SBrian Somers struct msghdr msg; 124996c9bb21SBrian Somers struct iovec iov[SCATTER_SEGMENTS]; 1250b7c5748eSBrian Somers struct datalink *dl; 125196c9bb21SBrian Somers int niov, link_fd, expect, f; 12526f384573SBrian Somers 1253dd7e2610SBrian Somers log_Printf(LogPHASE, "Receiving datalink\n"); 12546f384573SBrian Somers 125596c9bb21SBrian Somers /* Create our scatter/gather array */ 125696c9bb21SBrian Somers niov = 1; 125796c9bb21SBrian Somers iov[0].iov_len = strlen(Version) + 1; 125896c9bb21SBrian Somers iov[0].iov_base = (char *)malloc(iov[0].iov_len); 125996c9bb21SBrian Somers if (datalink2iov(NULL, iov, &niov, sizeof iov / sizeof *iov) == -1) 12606f384573SBrian Somers return; 12616f384573SBrian Somers 126296c9bb21SBrian Somers for (f = expect = 0; f < niov; f++) 126396c9bb21SBrian Somers expect += iov[f].iov_len; 126496c9bb21SBrian Somers 126596c9bb21SBrian Somers /* Set up our message */ 126696c9bb21SBrian Somers cmsg->cmsg_len = sizeof cmsgbuf; 126796c9bb21SBrian Somers cmsg->cmsg_level = SOL_SOCKET; 126896c9bb21SBrian Somers cmsg->cmsg_type = SCM_RIGHTS; 126996c9bb21SBrian Somers 127096c9bb21SBrian Somers memset(&msg, '\0', sizeof msg); 127196c9bb21SBrian Somers msg.msg_name = (caddr_t)sun; 127296c9bb21SBrian Somers msg.msg_namelen = sizeof *sun; 127396c9bb21SBrian Somers msg.msg_iov = iov; 127496c9bb21SBrian Somers msg.msg_iovlen = niov; 127596c9bb21SBrian Somers msg.msg_control = cmsgbuf; 127696c9bb21SBrian Somers msg.msg_controllen = sizeof cmsgbuf; 127796c9bb21SBrian Somers 127896c9bb21SBrian Somers log_Printf(LogDEBUG, "Expecting %d scatter/gather bytes\n", expect); 127996c9bb21SBrian Somers f = expect + 100; 128096c9bb21SBrian Somers setsockopt(s, SOL_SOCKET, SO_RCVBUF, &f, sizeof f); 128196c9bb21SBrian Somers if ((f = recvmsg(s, &msg, MSG_WAITALL)) != expect) { 128296c9bb21SBrian Somers if (f == -1) 128396c9bb21SBrian Somers log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno)); 128496c9bb21SBrian Somers else 128596c9bb21SBrian Somers log_Printf(LogERROR, "Failed recvmsg: Got %d, not %d\n", f, expect); 128696c9bb21SBrian Somers while (niov--) 128796c9bb21SBrian Somers free(iov[niov].iov_base); 128896c9bb21SBrian Somers return; 128996c9bb21SBrian Somers } 129096c9bb21SBrian Somers 129196c9bb21SBrian Somers /* We've successfully received an open file descriptor through our socket */ 129296c9bb21SBrian Somers link_fd = *(int *)CMSG_DATA(cmsg); 129396c9bb21SBrian Somers 1294c0d9a877SBrian Somers write(s, "!",1 ); /* ACK */ 1295c0d9a877SBrian Somers 129696c9bb21SBrian Somers if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) { 129796c9bb21SBrian Somers log_Printf(LogWARN, "Cannot receive datalink, incorrect version" 129896c9bb21SBrian Somers " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len, 129996c9bb21SBrian Somers iov[0].iov_base, Version); 130096c9bb21SBrian Somers close(link_fd); 130196c9bb21SBrian Somers while (niov--) 130296c9bb21SBrian Somers free(iov[niov].iov_base); 130396c9bb21SBrian Somers return; 130496c9bb21SBrian Somers } 130596c9bb21SBrian Somers 130696c9bb21SBrian Somers niov = 1; 1307b7c5748eSBrian Somers dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, link_fd); 1308b7c5748eSBrian Somers if (dl) { 1309b7c5748eSBrian Somers dl->next = bundle->links; 1310b7c5748eSBrian Somers bundle->links = dl; 13116f384573SBrian Somers bundle_GenPhysType(bundle); 1312b7c5748eSBrian Somers datalink_AuthOk(dl); 131396c9bb21SBrian Somers } else 131496c9bb21SBrian Somers close(link_fd); 131596c9bb21SBrian Somers 131696c9bb21SBrian Somers free(iov[0].iov_base); 13171fa665f5SBrian Somers } 13181fa665f5SBrian Somers 13191fa665f5SBrian Somers void 132096c9bb21SBrian Somers bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun) 13211fa665f5SBrian Somers { 1322c0d9a877SBrian Somers char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int)], ack; 132396c9bb21SBrian Somers struct cmsghdr *cmsg = (struct cmsghdr *)cmsgbuf; 132496c9bb21SBrian Somers struct msghdr msg; 132596c9bb21SBrian Somers struct iovec iov[SCATTER_SEGMENTS]; 132696c9bb21SBrian Somers int niov, link_fd, f, expect; 13276f384573SBrian Somers struct datalink **pdl; 13286f384573SBrian Somers struct bundle *bundle = dl->bundle; 13296f384573SBrian Somers 1330dd7e2610SBrian Somers log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name); 13316f384573SBrian Somers 13326f384573SBrian Somers /* First, un-hook the datalink */ 13336f384573SBrian Somers for (pdl = &bundle->links; *pdl; pdl = &(*pdl)->next) 13346f384573SBrian Somers if (*pdl == dl) { 13356f384573SBrian Somers *pdl = dl->next; 13366f384573SBrian Somers dl->next = NULL; 13376f384573SBrian Somers break; 13386f384573SBrian Somers } 13396f384573SBrian Somers 1340ea722969SBrian Somers bundle_GenPhysType(bundle); 1341ea722969SBrian Somers bundle_LinkClosed(bundle, dl); 1342ea722969SBrian Somers 134396c9bb21SBrian Somers /* Build our scatter/gather array */ 134496c9bb21SBrian Somers iov[0].iov_len = strlen(Version) + 1; 134596c9bb21SBrian Somers iov[0].iov_base = strdup(Version); 134696c9bb21SBrian Somers niov = 1; 13476f384573SBrian Somers 134896c9bb21SBrian Somers link_fd = datalink2iov(dl, iov, &niov, sizeof iov / sizeof *iov); 13496f384573SBrian Somers 13506f384573SBrian Somers if (link_fd != -1) { 135196c9bb21SBrian Somers cmsg->cmsg_len = sizeof cmsgbuf; 135296c9bb21SBrian Somers cmsg->cmsg_level = SOL_SOCKET; 135396c9bb21SBrian Somers cmsg->cmsg_type = SCM_RIGHTS; 135496c9bb21SBrian Somers *(int *)CMSG_DATA(cmsg) = link_fd; 135547723d29SBrian Somers 135696c9bb21SBrian Somers memset(&msg, '\0', sizeof msg); 135796c9bb21SBrian Somers msg.msg_name = (caddr_t)sun; 135896c9bb21SBrian Somers msg.msg_namelen = sizeof *sun; 135996c9bb21SBrian Somers msg.msg_iov = iov; 136096c9bb21SBrian Somers msg.msg_iovlen = niov; 136196c9bb21SBrian Somers msg.msg_control = cmsgbuf; 136296c9bb21SBrian Somers msg.msg_controllen = sizeof cmsgbuf; 136347723d29SBrian Somers 136496c9bb21SBrian Somers for (f = expect = 0; f < niov; f++) 136596c9bb21SBrian Somers expect += iov[f].iov_len; 136647723d29SBrian Somers 136796c9bb21SBrian Somers log_Printf(LogDEBUG, "Sending %d bytes in scatter/gather array\n", expect); 136847723d29SBrian Somers 136996c9bb21SBrian Somers f = expect + SOCKET_OVERHEAD; 137096c9bb21SBrian Somers setsockopt(s, SOL_SOCKET, SO_SNDBUF, &f, sizeof f); 137196c9bb21SBrian Somers if (sendmsg(s, &msg, 0) == -1) 137296c9bb21SBrian Somers log_Printf(LogERROR, "Failed sendmsg: %s\n", strerror(errno)); 1373c0d9a877SBrian Somers /* We must get the ACK before closing the descriptor ! */ 1374c0d9a877SBrian Somers read(s, &ack, 1); 137596c9bb21SBrian Somers close(link_fd); 137647723d29SBrian Somers } 137796c9bb21SBrian Somers 137896c9bb21SBrian Somers while (niov--) 137996c9bb21SBrian Somers free(iov[niov].iov_base); 13801fa665f5SBrian Somers } 1381dd0645c5SBrian Somers 1382dd0645c5SBrian Somers int 138358d55334SBrian Somers bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl, 138458d55334SBrian Somers const char *name) 138558d55334SBrian Somers { 138658d55334SBrian Somers struct datalink *dl; 138758d55334SBrian Somers 138858d55334SBrian Somers if (!strcasecmp(ndl->name, name)) 138958d55334SBrian Somers return 1; 139058d55334SBrian Somers 139158d55334SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 139258d55334SBrian Somers if (!strcasecmp(dl->name, name)) 139358d55334SBrian Somers return 0; 139458d55334SBrian Somers 139558d55334SBrian Somers datalink_Rename(ndl, name); 139658d55334SBrian Somers return 1; 139758d55334SBrian Somers } 139858d55334SBrian Somers 139958d55334SBrian Somers int 1400dd0645c5SBrian Somers bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode) 1401dd0645c5SBrian Somers { 1402dd0645c5SBrian Somers int omode; 1403dd0645c5SBrian Somers 1404dd0645c5SBrian Somers omode = dl->physical->type; 1405dd0645c5SBrian Somers if (omode == mode) 1406dd0645c5SBrian Somers return 1; 1407dd0645c5SBrian Somers 1408dd0645c5SBrian Somers if (mode == PHYS_DEMAND && !(bundle->phys_type & PHYS_DEMAND)) 1409dd0645c5SBrian Somers /* Changing to demand-dial mode */ 1410dd0645c5SBrian Somers if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) { 1411dd0645c5SBrian Somers log_Printf(LogWARN, "You must `set ifaddr' before changing mode to %s\n", 1412dd0645c5SBrian Somers mode2Nam(mode)); 1413dd0645c5SBrian Somers return 0; 1414dd0645c5SBrian Somers } 1415dd0645c5SBrian Somers 1416dd0645c5SBrian Somers if (!datalink_SetMode(dl, mode)) 1417dd0645c5SBrian Somers return 0; 1418dd0645c5SBrian Somers 1419dd0645c5SBrian Somers if (mode == PHYS_DEMAND && !(bundle->phys_type & PHYS_DEMAND)) 1420dd0645c5SBrian Somers ipcp_InterfaceUp(&bundle->ncp.ipcp); 1421dd0645c5SBrian Somers 1422dd0645c5SBrian Somers bundle_GenPhysType(bundle); 1423dd0645c5SBrian Somers 1424dd0645c5SBrian Somers if (omode == PHYS_DEMAND && !(bundle->phys_type & PHYS_DEMAND)) 1425dd0645c5SBrian Somers /* Changing from demand-dial mode */ 1426dd0645c5SBrian Somers ipcp_CleanInterface(&bundle->ncp.ipcp); 1427dd0645c5SBrian Somers 1428dd0645c5SBrian Somers return 1; 1429dd0645c5SBrian Somers } 1430