17a6f8720SBrian Somers /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni *
47a6f8720SBrian Somers * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
57a6f8720SBrian Somers * All rights reserved.
67a6f8720SBrian Somers *
77a6f8720SBrian Somers * Redistribution and use in source and binary forms, with or without
87a6f8720SBrian Somers * modification, are permitted provided that the following conditions
97a6f8720SBrian Somers * are met:
107a6f8720SBrian Somers * 1. Redistributions of source code must retain the above copyright
117a6f8720SBrian Somers * notice, this list of conditions and the following disclaimer.
127a6f8720SBrian Somers * 2. Redistributions in binary form must reproduce the above copyright
137a6f8720SBrian Somers * notice, this list of conditions and the following disclaimer in the
147a6f8720SBrian Somers * documentation and/or other materials provided with the distribution.
157a6f8720SBrian Somers *
167a6f8720SBrian Somers * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
177a6f8720SBrian Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
187a6f8720SBrian Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
197a6f8720SBrian Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
207a6f8720SBrian Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
217a6f8720SBrian Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
227a6f8720SBrian Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
237a6f8720SBrian Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
247a6f8720SBrian Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
257a6f8720SBrian Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
267a6f8720SBrian Somers * SUCH DAMAGE.
277a6f8720SBrian Somers */
287a6f8720SBrian Somers
291384bd27SBrian Somers #include <sys/param.h>
307a6f8720SBrian Somers #include <sys/socket.h>
317a6f8720SBrian Somers #include <netinet/in.h>
327a6f8720SBrian Somers #include <net/if.h>
333a7b6d76SBrian Somers #include <net/if_tun.h> /* For TUNS* ioctls */
347a6f8720SBrian Somers #include <net/route.h>
35eaa4df37SBrian Somers #include <netinet/in_systm.h>
36eaa4df37SBrian Somers #include <netinet/ip.h>
371fa665f5SBrian Somers #include <sys/un.h>
387a6f8720SBrian Somers
397a6f8720SBrian Somers #include <errno.h>
407a6f8720SBrian Somers #include <fcntl.h>
41cf0a3940SBrian Somers #ifdef __OpenBSD__
42cf0a3940SBrian Somers #include <util.h>
43cf0a3940SBrian Somers #else
44cf0a3940SBrian Somers #include <libutil.h>
45cf0a3940SBrian Somers #endif
4647723d29SBrian Somers #include <paths.h>
476eafd353SBrian Somers #include <stdarg.h>
487a6f8720SBrian Somers #include <stdio.h>
496f384573SBrian Somers #include <stdlib.h>
507a6f8720SBrian Somers #include <string.h>
5196c9bb21SBrian Somers #include <sys/uio.h>
5254cd8e13SBrian Somers #include <sys/wait.h>
537a6f8720SBrian Somers #include <termios.h>
547a6f8720SBrian Somers #include <unistd.h>
557a6f8720SBrian Somers
565d9e6103SBrian Somers #include "layer.h"
57c9e11a11SBrian Somers #include "defs.h"
587a6f8720SBrian Somers #include "command.h"
597a6f8720SBrian Somers #include "mbuf.h"
607a6f8720SBrian Somers #include "log.h"
617a6f8720SBrian Somers #include "id.h"
627a6f8720SBrian Somers #include "timer.h"
637a6f8720SBrian Somers #include "fsm.h"
647a6f8720SBrian Somers #include "iplist.h"
65879ed6faSBrian Somers #include "lqr.h"
66455aabc3SBrian Somers #include "hdlc.h"
677a6f8720SBrian Somers #include "throughput.h"
68eaa4df37SBrian Somers #include "slcompress.h"
6930949fd4SBrian Somers #include "ncpaddr.h"
7030949fd4SBrian Somers #include "ip.h"
717a6f8720SBrian Somers #include "ipcp.h"
725ca5389aSBrian Somers #include "filter.h"
732f786681SBrian Somers #include "descriptor.h"
747a6f8720SBrian Somers #include "route.h"
757a6f8720SBrian Somers #include "lcp.h"
767a6f8720SBrian Somers #include "ccp.h"
773b0f8d2eSBrian Somers #include "link.h"
783b0f8d2eSBrian Somers #include "mp.h"
79972a1bcfSBrian Somers #ifndef NORADIUS
80972a1bcfSBrian Somers #include "radius.h"
81972a1bcfSBrian Somers #endif
8230949fd4SBrian Somers #include "ipv6cp.h"
8330949fd4SBrian Somers #include "ncp.h"
843b0f8d2eSBrian Somers #include "bundle.h"
85455aabc3SBrian Somers #include "async.h"
86455aabc3SBrian Somers #include "physical.h"
87455aabc3SBrian Somers #include "auth.h"
885d9e6103SBrian Somers #include "proto.h"
89455aabc3SBrian Somers #include "chap.h"
90455aabc3SBrian Somers #include "tun.h"
9185b542cfSBrian Somers #include "prompt.h"
923006ec67SBrian Somers #include "chat.h"
9392b09558SBrian Somers #include "cbcp.h"
943006ec67SBrian Somers #include "datalink.h"
958fa6ebe4SBrian Somers #include "iface.h"
9674457d3dSBrian Somers #include "server.h"
97971abb29SBrian Somers #include "probe.h"
98fb11a9c2SBrian Somers #ifndef NODES
99019d32bfSBrian Somers #include "mppe.h"
10003a2501aSBrian Somers #endif
1017a6f8720SBrian Somers
10291cbd2eeSBrian Somers #define SCATTER_SEGMENTS 7 /* version, datalink, name, physical,
10391cbd2eeSBrian Somers throughput, throughput, device */
10487c3786eSBrian Somers
1052cb305afSBrian Somers #define SEND_MAXFD 3 /* Max file descriptors passed through
10687c3786eSBrian Somers the local domain socket */
10796c9bb21SBrian Somers
10804eaa58cSBrian Somers static int bundle_RemainingIdleTime(struct bundle *);
10904eaa58cSBrian Somers
110182c898aSBrian Somers static const char * const PhaseNames[] = {
111455aabc3SBrian Somers "Dead", "Establish", "Authenticate", "Network", "Terminate"
112455aabc3SBrian Somers };
113455aabc3SBrian Somers
114455aabc3SBrian Somers const char *
bundle_PhaseName(struct bundle * bundle)115455aabc3SBrian Somers bundle_PhaseName(struct bundle *bundle)
1167a6f8720SBrian Somers {
117455aabc3SBrian Somers return bundle->phase <= PHASE_TERMINATE ?
118455aabc3SBrian Somers PhaseNames[bundle->phase] : "unknown";
1197a6f8720SBrian Somers }
1207a6f8720SBrian Somers
121455aabc3SBrian Somers void
bundle_NewPhase(struct bundle * bundle,u_int new)1225563ebdeSBrian Somers bundle_NewPhase(struct bundle *bundle, u_int new)
123455aabc3SBrian Somers {
124aef795ccSBrian Somers if (new == bundle->phase)
125aef795ccSBrian Somers return;
126aef795ccSBrian Somers
127e2ebb036SBrian Somers if (new <= PHASE_TERMINATE)
128dd7e2610SBrian Somers log_Printf(LogPHASE, "bundle: %s\n", PhaseNames[new]);
1297a6f8720SBrian Somers
130455aabc3SBrian Somers switch (new) {
131455aabc3SBrian Somers case PHASE_DEAD:
132455aabc3SBrian Somers bundle->phase = new;
133fb11a9c2SBrian Somers #ifndef NODES
134019d32bfSBrian Somers MPPE_MasterKeyValid = 0;
13564602637SBrian Somers #endif
136019d32bfSBrian Somers log_DisplayPrompts();
137455aabc3SBrian Somers break;
138455aabc3SBrian Somers
139455aabc3SBrian Somers case PHASE_ESTABLISH:
140455aabc3SBrian Somers bundle->phase = new;
141455aabc3SBrian Somers break;
142455aabc3SBrian Somers
143455aabc3SBrian Somers case PHASE_AUTHENTICATE:
144455aabc3SBrian Somers bundle->phase = new;
1450f2f3eb3SBrian Somers log_DisplayPrompts();
146455aabc3SBrian Somers break;
147455aabc3SBrian Somers
148455aabc3SBrian Somers case PHASE_NETWORK:
14930949fd4SBrian Somers if (ncp_fsmStart(&bundle->ncp, bundle)) {
150673903ecSBrian Somers bundle->phase = new;
1510f2f3eb3SBrian Somers log_DisplayPrompts();
15230949fd4SBrian Somers } else {
15330949fd4SBrian Somers log_Printf(LogPHASE, "bundle: All NCPs are disabled\n");
15430949fd4SBrian Somers bundle_Close(bundle, NULL, CLOSE_STAYDOWN);
15530949fd4SBrian Somers }
156673903ecSBrian Somers break;
157455aabc3SBrian Somers
158455aabc3SBrian Somers case PHASE_TERMINATE:
159455aabc3SBrian Somers bundle->phase = new;
160673903ecSBrian Somers mp_Down(&bundle->ncp.mp);
1610f2f3eb3SBrian Somers log_DisplayPrompts();
162455aabc3SBrian Somers break;
1637a6f8720SBrian Somers }
1647a6f8720SBrian Somers }
1657a6f8720SBrian Somers
1666d666775SBrian Somers static void
bundle_LayerStart(void * v __unused,struct fsm * fp __unused)167057f1760SBrian Somers bundle_LayerStart(void *v __unused, struct fsm *fp __unused)
1687a6f8720SBrian Somers {
1693006ec67SBrian Somers /* The given FSM is about to start up ! */
1707a6f8720SBrian Somers }
1717a6f8720SBrian Somers
1725cf4388bSBrian Somers
173b42135deSBrian Somers void
bundle_Notify(struct bundle * bundle,char c)1745cf4388bSBrian Somers bundle_Notify(struct bundle *bundle, char c)
1755cf4388bSBrian Somers {
1765cf4388bSBrian Somers if (bundle->notify.fd != -1) {
177b42135deSBrian Somers int ret;
178b42135deSBrian Somers
179b42135deSBrian Somers ret = write(bundle->notify.fd, &c, 1);
180b42135deSBrian Somers if (c != EX_REDIAL && c != EX_RECONNECT) {
181b42135deSBrian Somers if (ret == 1)
182b42135deSBrian Somers log_Printf(LogCHAT, "Parent notified of %s\n",
1835a83ad1eSBrian Somers c == EX_NORMAL ? "success" : "failure");
1845cf4388bSBrian Somers else
185b42135deSBrian Somers log_Printf(LogERROR, "Failed to notify parent of success\n");
1865cf4388bSBrian Somers close(bundle->notify.fd);
1875cf4388bSBrian Somers bundle->notify.fd = -1;
188b42135deSBrian Somers } else if (ret == 1)
189b42135deSBrian Somers log_Printf(LogCHAT, "Parent notified of %s\n", ex_desc(c));
190b42135deSBrian Somers else
191b42135deSBrian Somers log_Printf(LogERROR, "Failed to notify parent of %s\n", ex_desc(c));
1925cf4388bSBrian Somers }
1935cf4388bSBrian Somers }
1943b0f8d2eSBrian Somers
1956d666775SBrian Somers static void
bundle_ClearQueues(void * v)1966f8e9f0aSBrian Somers bundle_ClearQueues(void *v)
1976f8e9f0aSBrian Somers {
1986f8e9f0aSBrian Somers struct bundle *bundle = (struct bundle *)v;
1996f8e9f0aSBrian Somers struct datalink *dl;
2006f8e9f0aSBrian Somers
2016f8e9f0aSBrian Somers log_Printf(LogPHASE, "Clearing choked output queue\n");
2026f8e9f0aSBrian Somers timer_Stop(&bundle->choked.timer);
2036f8e9f0aSBrian Somers
2046f8e9f0aSBrian Somers /*
2056f8e9f0aSBrian Somers * Emergency time:
2066f8e9f0aSBrian Somers *
2076f8e9f0aSBrian Somers * We've had a full queue for PACKET_DEL_SECS seconds without being
2086f8e9f0aSBrian Somers * able to get rid of any of the packets. We've probably given up
2096f8e9f0aSBrian Somers * on the redials at this point, and the queued data has almost
2106f8e9f0aSBrian Somers * definitely been timed out by the layer above. As this is preventing
2116f8e9f0aSBrian Somers * us from reading the TUN_NAME device (we don't want to buffer stuff
2126f8e9f0aSBrian Somers * indefinitely), we may as well nuke this data and start with a clean
2136f8e9f0aSBrian Somers * slate !
2146f8e9f0aSBrian Somers *
2156f8e9f0aSBrian Somers * Unfortunately, this has the side effect of shafting any compression
2166f8e9f0aSBrian Somers * dictionaries in use (causing the relevant RESET_REQ/RESET_ACK).
2176f8e9f0aSBrian Somers */
2186f8e9f0aSBrian Somers
21930949fd4SBrian Somers ncp_DeleteQueues(&bundle->ncp);
2206f8e9f0aSBrian Somers for (dl = bundle->links; dl; dl = dl->next)
2216f8e9f0aSBrian Somers physical_DeleteQueue(dl->physical);
2226f8e9f0aSBrian Somers }
2236f8e9f0aSBrian Somers
2246f8e9f0aSBrian Somers static void
bundle_LinkAdded(struct bundle * bundle,struct datalink * dl)225ff0f9439SBrian Somers bundle_LinkAdded(struct bundle *bundle, struct datalink *dl)
226ff0f9439SBrian Somers {
227ff0f9439SBrian Somers bundle->phys_type.all |= dl->physical->type;
228ff0f9439SBrian Somers if (dl->state == DATALINK_OPEN)
229ff0f9439SBrian Somers bundle->phys_type.open |= dl->physical->type;
230ff0f9439SBrian Somers
231bf1eaec5SBrian Somers #ifndef NORADIUS
232bf1eaec5SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL))
233bf1eaec5SBrian Somers != bundle->phys_type.open && bundle->session.timer.state == TIMER_STOPPED)
234bf1eaec5SBrian Somers if (bundle->radius.sessiontime)
235bf1eaec5SBrian Somers bundle_StartSessionTimer(bundle, 0);
236bf1eaec5SBrian Somers #endif
237bf1eaec5SBrian Somers
238ff0f9439SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL))
239ff0f9439SBrian Somers != bundle->phys_type.open && bundle->idle.timer.state == TIMER_STOPPED)
240ff0f9439SBrian Somers /* We may need to start our idle timer */
2410a4b6c5cSBrian Somers bundle_StartIdleTimer(bundle, 0);
242ff0f9439SBrian Somers }
243ff0f9439SBrian Somers
24492b09558SBrian Somers void
bundle_LinksRemoved(struct bundle * bundle)245ff0f9439SBrian Somers bundle_LinksRemoved(struct bundle *bundle)
246ff0f9439SBrian Somers {
247ff0f9439SBrian Somers struct datalink *dl;
248ff0f9439SBrian Somers
249ff0f9439SBrian Somers bundle->phys_type.all = bundle->phys_type.open = 0;
250ff0f9439SBrian Somers for (dl = bundle->links; dl; dl = dl->next)
251ff0f9439SBrian Somers bundle_LinkAdded(bundle, dl);
252ff0f9439SBrian Somers
253ab2de065SBrian Somers bundle_CalculateBandwidth(bundle);
254ab2de065SBrian Somers mp_CheckAutoloadTimer(&bundle->ncp.mp);
255ab2de065SBrian Somers
256ff0f9439SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL))
257bf1eaec5SBrian Somers == bundle->phys_type.open) {
258bf1eaec5SBrian Somers #ifndef NORADIUS
259bf1eaec5SBrian Somers if (bundle->radius.sessiontime)
260bf1eaec5SBrian Somers bundle_StopSessionTimer(bundle);
261bf1eaec5SBrian Somers #endif
262ff0f9439SBrian Somers bundle_StopIdleTimer(bundle);
263ff0f9439SBrian Somers }
264bf1eaec5SBrian Somers }
26504eaa58cSBrian Somers
26604eaa58cSBrian Somers static void
bundle_LayerUp(void * v,struct fsm * fp)2676f384573SBrian Somers bundle_LayerUp(void *v, struct fsm *fp)
2687a6f8720SBrian Somers {
2693006ec67SBrian Somers /*
2703006ec67SBrian Somers * The given fsm is now up
271ab2de065SBrian Somers * If it's an LCP, adjust our phys_mode.open value and check the
272ab2de065SBrian Somers * autoload timer.
273ab2de065SBrian Somers * If it's the first NCP, calculate our bandwidth
274dade2407SBrian Somers * If it's the first NCP, set our ``upat'' time
2753b0f8d2eSBrian Somers * If it's the first NCP, start the idle timer.
276ab2de065SBrian Somers * If it's an NCP, tell our -background parent to go away.
277ab2de065SBrian Somers * If it's the first NCP, start the autoload timer
2783006ec67SBrian Somers */
2796f384573SBrian Somers struct bundle *bundle = (struct bundle *)v;
2806d666775SBrian Somers
2815563ebdeSBrian Somers if (fp->proto == PROTO_LCP) {
282ff0f9439SBrian Somers struct physical *p = link2physical(fp->link);
283ff0f9439SBrian Somers
284ff0f9439SBrian Somers bundle_LinkAdded(bundle, p->dl);
285ab2de065SBrian Somers mp_CheckAutoloadTimer(&bundle->ncp.mp);
28630949fd4SBrian Somers } else if (isncp(fp->proto)) {
28730949fd4SBrian Somers if (ncp_LayersOpen(&fp->bundle->ncp) == 1) {
288ab2de065SBrian Somers bundle_CalculateBandwidth(fp->bundle);
289dade2407SBrian Somers time(&bundle->upat);
290bf1eaec5SBrian Somers #ifndef NORADIUS
291bf1eaec5SBrian Somers if (bundle->radius.sessiontime)
292bf1eaec5SBrian Somers bundle_StartSessionTimer(bundle, 0);
293bf1eaec5SBrian Somers #endif
2940a4b6c5cSBrian Somers bundle_StartIdleTimer(bundle, 0);
295ab2de065SBrian Somers mp_CheckAutoloadTimer(&fp->bundle->ncp.mp);
29630949fd4SBrian Somers }
29730949fd4SBrian Somers bundle_Notify(bundle, EX_NORMAL);
2986301d506SBrian Somers } else if (fp->proto == PROTO_CCP)
2996301d506SBrian Somers bundle_CalculateBandwidth(fp->bundle); /* Against ccp_MTUOverhead */
300ab886ad0SBrian Somers }
3017a6f8720SBrian Somers
3026d666775SBrian Somers static void
bundle_LayerDown(void * v,struct fsm * fp)3036d666775SBrian Somers bundle_LayerDown(void *v, struct fsm *fp)
3046d666775SBrian Somers {
3056d666775SBrian Somers /*
3066d666775SBrian Somers * The given FSM has been told to come down.
307ab886ad0SBrian Somers * If it's our last NCP, stop the idle timer.
308dade2407SBrian Somers * If it's our last NCP, clear our ``upat'' value.
309ab2de065SBrian Somers * If it's our last NCP, stop the autoload timer
310ff0f9439SBrian Somers * If it's an LCP, adjust our phys_type.open value and any timers.
31186b1f0d7SBrian Somers * If it's an LCP and we're in multilink mode, adjust our tun
312c8f30703SBrian Somers * If it's the last LCP, down all NCPs
31386b1f0d7SBrian Somers * speed and make sure our minimum sequence number is adjusted.
3146d666775SBrian Somers */
315ab886ad0SBrian Somers
316ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v;
317ab886ad0SBrian Somers
31830949fd4SBrian Somers if (isncp(fp->proto)) {
31930949fd4SBrian Somers if (ncp_LayersOpen(&fp->bundle->ncp) == 0) {
320bf1eaec5SBrian Somers #ifndef NORADIUS
321bf1eaec5SBrian Somers if (bundle->radius.sessiontime)
322bf1eaec5SBrian Somers bundle_StopSessionTimer(bundle);
323bf1eaec5SBrian Somers #endif
324ab886ad0SBrian Somers bundle_StopIdleTimer(bundle);
325dade2407SBrian Somers bundle->upat = 0;
326ab2de065SBrian Somers mp_StopAutoloadTimer(&bundle->ncp.mp);
32730949fd4SBrian Somers }
328ab2de065SBrian Somers } else if (fp->proto == PROTO_LCP) {
3293b0f8d2eSBrian Somers struct datalink *dl;
33086b1f0d7SBrian Somers struct datalink *lost;
331c8f30703SBrian Somers int others_active;
332c8f30703SBrian Somers
333c8f30703SBrian Somers bundle_LinksRemoved(bundle); /* adjust timers & phys_type values */
3343b0f8d2eSBrian Somers
33586b1f0d7SBrian Somers lost = NULL;
336c8f30703SBrian Somers others_active = 0;
337c8f30703SBrian Somers for (dl = bundle->links; dl; dl = dl->next) {
33886b1f0d7SBrian Somers if (fp == &dl->physical->link.lcp.fsm)
33986b1f0d7SBrian Somers lost = dl;
340c8f30703SBrian Somers else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP)
341c8f30703SBrian Somers others_active++;
342c8f30703SBrian Somers }
34386b1f0d7SBrian Somers
344c8f30703SBrian Somers if (bundle->ncp.mp.active) {
345ab2de065SBrian Somers bundle_CalculateBandwidth(bundle);
34686b1f0d7SBrian Somers
34786b1f0d7SBrian Somers if (lost)
34886b1f0d7SBrian Somers mp_LinkLost(&bundle->ncp.mp, lost);
34986b1f0d7SBrian Somers else
350a33b2ef7SBrian Somers log_Printf(LogALERT, "Oops, lost an unrecognised datalink (%s) !\n",
35186b1f0d7SBrian Somers fp->link->name);
3523b0f8d2eSBrian Somers }
353c8f30703SBrian Somers
354356bf92dSBrian Somers if (!others_active) {
355c8f30703SBrian Somers /* Down the NCPs. We don't expect to get fsm_Close()d ourself ! */
35630949fd4SBrian Somers ncp2initial(&bundle->ncp);
357356bf92dSBrian Somers mp_Down(&bundle->ncp.mp);
358356bf92dSBrian Somers }
3596d666775SBrian Somers }
360ff0f9439SBrian Somers }
3616d666775SBrian Somers
3626d666775SBrian Somers static void
bundle_LayerFinish(void * v,struct fsm * fp)3636d666775SBrian Somers bundle_LayerFinish(void *v, struct fsm *fp)
3646d666775SBrian Somers {
3656d666775SBrian Somers /* The given fsm is now down (fp cannot be NULL)
3666d666775SBrian Somers *
367dd7e2610SBrian Somers * If it's the last NCP, fsm_Close all LCPs
368356bf92dSBrian Somers * If it's the last NCP, bring any MP layer down
3696d666775SBrian Somers */
3706d666775SBrian Somers
3716d666775SBrian Somers struct bundle *bundle = (struct bundle *)v;
3726d666775SBrian Somers struct datalink *dl;
3736d666775SBrian Somers
37430949fd4SBrian Somers if (isncp(fp->proto) && !ncp_LayersUnfinished(&bundle->ncp)) {
37526afeaa2SBrian Somers if (bundle_Phase(bundle) != PHASE_DEAD)
37625092092SBrian Somers bundle_NewPhase(bundle, PHASE_TERMINATE);
3776d666775SBrian Somers for (dl = bundle->links; dl; dl = dl->next)
378c8f30703SBrian Somers if (dl->state == DATALINK_OPEN)
379d4d5d2f8SBrian Somers datalink_Close(dl, CLOSE_STAYDOWN);
38009206a6fSBrian Somers fsm2initial(fp);
381356bf92dSBrian Somers mp_Down(&bundle->ncp.mp);
3823b0f8d2eSBrian Somers }
3833b0f8d2eSBrian Somers }
3846d666775SBrian Somers
3857a6f8720SBrian Somers void
bundle_Close(struct bundle * bundle,const char * name,int how)3869c81b87dSBrian Somers bundle_Close(struct bundle *bundle, const char *name, int how)
3877a6f8720SBrian Somers {
388455aabc3SBrian Somers /*
3893006ec67SBrian Somers * Please close the given datalink.
390dd7e2610SBrian Somers * If name == NULL or name is the last datalink, fsm_Close all NCPs
3916f384573SBrian Somers * (except our MP)
3923b0f8d2eSBrian Somers * If it isn't the last datalink, just Close that datalink.
393455aabc3SBrian Somers */
3947a6f8720SBrian Somers
3953b0f8d2eSBrian Somers struct datalink *dl, *this_dl;
3963b0f8d2eSBrian Somers int others_active;
3973006ec67SBrian Somers
3983b0f8d2eSBrian Somers others_active = 0;
3993b0f8d2eSBrian Somers this_dl = NULL;
4003b0f8d2eSBrian Somers
4013b0f8d2eSBrian Somers for (dl = bundle->links; dl; dl = dl->next) {
4023b0f8d2eSBrian Somers if (name && !strcasecmp(name, dl->name))
4033b0f8d2eSBrian Somers this_dl = dl;
4043b0f8d2eSBrian Somers if (name == NULL || this_dl == dl) {
4059c81b87dSBrian Somers switch (how) {
4069c81b87dSBrian Somers case CLOSE_LCP:
4079c81b87dSBrian Somers datalink_DontHangup(dl);
4082fc2f705SBrian Somers break;
4099c81b87dSBrian Somers case CLOSE_STAYDOWN:
4103006ec67SBrian Somers datalink_StayDown(dl);
4119c81b87dSBrian Somers break;
4129c81b87dSBrian Somers }
4133b0f8d2eSBrian Somers } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP)
4143b0f8d2eSBrian Somers others_active++;
4153b0f8d2eSBrian Somers }
4163b0f8d2eSBrian Somers
4173b0f8d2eSBrian Somers if (name && this_dl == NULL) {
418dd7e2610SBrian Somers log_Printf(LogWARN, "%s: Invalid datalink name\n", name);
4193b0f8d2eSBrian Somers return;
4203b0f8d2eSBrian Somers }
4213b0f8d2eSBrian Somers
4223b0f8d2eSBrian Somers if (!others_active) {
423bf1eaec5SBrian Somers #ifndef NORADIUS
424bf1eaec5SBrian Somers if (bundle->radius.sessiontime)
425bf1eaec5SBrian Somers bundle_StopSessionTimer(bundle);
426bf1eaec5SBrian Somers #endif
42704eaa58cSBrian Somers bundle_StopIdleTimer(bundle);
42830949fd4SBrian Somers if (ncp_LayersUnfinished(&bundle->ncp))
42930949fd4SBrian Somers ncp_Close(&bundle->ncp);
4303b0f8d2eSBrian Somers else {
43130949fd4SBrian Somers ncp2initial(&bundle->ncp);
432356bf92dSBrian Somers mp_Down(&bundle->ncp.mp);
433d345321bSBrian Somers for (dl = bundle->links; dl; dl = dl->next)
4349c81b87dSBrian Somers datalink_Close(dl, how);
4357a6f8720SBrian Somers }
4363b0f8d2eSBrian Somers } else if (this_dl && this_dl->state != DATALINK_CLOSED &&
4373b0f8d2eSBrian Somers this_dl->state != DATALINK_HANGUP)
4389c81b87dSBrian Somers datalink_Close(this_dl, how);
439d2fd8d77SBrian Somers }
4407a6f8720SBrian Somers
4411bc9b5baSBrian Somers void
bundle_Down(struct bundle * bundle,int how)442899011c4SBrian Somers bundle_Down(struct bundle *bundle, int how)
4431bc9b5baSBrian Somers {
4441bc9b5baSBrian Somers struct datalink *dl;
4451bc9b5baSBrian Somers
4461bc9b5baSBrian Somers for (dl = bundle->links; dl; dl = dl->next)
447899011c4SBrian Somers datalink_Down(dl, how);
4481bc9b5baSBrian Somers }
4491bc9b5baSBrian Somers
4502f786681SBrian Somers static int
bundle_UpdateSet(struct fdescriptor * d,fd_set * r,fd_set * w,fd_set * e,int * n)451f013f33eSBrian Somers bundle_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
4522f786681SBrian Somers {
4532f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d);
4542f786681SBrian Somers struct datalink *dl;
45526af0ae9SBrian Somers int result, nlinks;
4566c1d6731SBrian Somers u_short ifqueue;
45726af0ae9SBrian Somers size_t queued;
4582f786681SBrian Somers
4592f786681SBrian Somers result = 0;
4602f786681SBrian Somers
461078c562eSBrian Somers /* If there are aren't many packets queued, look for some more. */
46204eaa58cSBrian Somers for (nlinks = 0, dl = bundle->links; dl; dl = dl->next)
46304eaa58cSBrian Somers nlinks++;
46404eaa58cSBrian Somers
46504eaa58cSBrian Somers if (nlinks) {
46630949fd4SBrian Somers queued = r ? ncp_FillPhysicalQueues(&bundle->ncp, bundle) :
46730949fd4SBrian Somers ncp_QueueLen(&bundle->ncp);
46804eaa58cSBrian Somers
469ff0f9439SBrian Somers if (r && (bundle->phase == PHASE_NETWORK ||
470ff0f9439SBrian Somers bundle->phys_type.all & PHYS_AUTO)) {
47104eaa58cSBrian Somers /* enough surplus so that we can tell if we're getting swamped */
4726c1d6731SBrian Somers ifqueue = nlinks > bundle->cfg.ifqueue ? nlinks : bundle->cfg.ifqueue;
4736c1d6731SBrian Somers if (queued < ifqueue) {
47404eaa58cSBrian Somers /* Not enough - select() for more */
4756f8e9f0aSBrian Somers if (bundle->choked.timer.state == TIMER_RUNNING)
4766f8e9f0aSBrian Somers timer_Stop(&bundle->choked.timer); /* Not needed any more */
47704eaa58cSBrian Somers FD_SET(bundle->dev.fd, r);
478faefde08SBrian Somers if (*n < bundle->dev.fd + 1)
479faefde08SBrian Somers *n = bundle->dev.fd + 1;
4801384bd27SBrian Somers log_Printf(LogTIMER, "%s: fdset(r) %d\n", TUN_NAME, bundle->dev.fd);
481078c562eSBrian Somers result++;
4826f8e9f0aSBrian Somers } else if (bundle->choked.timer.state == TIMER_STOPPED) {
4836f8e9f0aSBrian Somers bundle->choked.timer.func = bundle_ClearQueues;
4846f8e9f0aSBrian Somers bundle->choked.timer.name = "output choke";
4856f8e9f0aSBrian Somers bundle->choked.timer.load = bundle->cfg.choked.timeout * SECTICKS;
4866f8e9f0aSBrian Somers bundle->choked.timer.arg = bundle;
4876f8e9f0aSBrian Somers timer_Start(&bundle->choked.timer);
488078c562eSBrian Somers }
48904eaa58cSBrian Somers }
49004eaa58cSBrian Somers }
491078c562eSBrian Somers
492f0cdd9c0SBrian Somers #ifndef NORADIUS
493f0cdd9c0SBrian Somers result += descriptor_UpdateSet(&bundle->radius.desc, r, w, e, n);
494f0cdd9c0SBrian Somers #endif
495f0cdd9c0SBrian Somers
49671555108SBrian Somers /* Which links need a select() ? */
49771555108SBrian Somers for (dl = bundle->links; dl; dl = dl->next)
49871555108SBrian Somers result += descriptor_UpdateSet(&dl->desc, r, w, e, n);
49971555108SBrian Somers
500ea722969SBrian Somers /*
501ea722969SBrian Somers * This *MUST* be called after the datalink UpdateSet()s as it
50204eaa58cSBrian Somers * might be ``holding'' one of the datalinks (death-row) and
5038e7bd08eSBrian Somers * wants to be able to de-select() it from the descriptor set.
504ea722969SBrian Somers */
5050f2f3eb3SBrian Somers result += descriptor_UpdateSet(&bundle->ncp.mp.server.desc, r, w, e, n);
506ea722969SBrian Somers
5072f786681SBrian Somers return result;
5082f786681SBrian Somers }
5092f786681SBrian Somers
5102f786681SBrian Somers static int
bundle_IsSet(struct fdescriptor * d,const fd_set * fdset)511f013f33eSBrian Somers bundle_IsSet(struct fdescriptor *d, const fd_set *fdset)
5122f786681SBrian Somers {
5132f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d);
5142f786681SBrian Somers struct datalink *dl;
5152f786681SBrian Somers
5162f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next)
5172f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset))
5182f786681SBrian Somers return 1;
5192f786681SBrian Somers
520f0cdd9c0SBrian Somers #ifndef NORADIUS
521f0cdd9c0SBrian Somers if (descriptor_IsSet(&bundle->radius.desc, fdset))
522f0cdd9c0SBrian Somers return 1;
523f0cdd9c0SBrian Somers #endif
524f0cdd9c0SBrian Somers
525332b9de0SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
526332b9de0SBrian Somers return 1;
527332b9de0SBrian Somers
528faefde08SBrian Somers return FD_ISSET(bundle->dev.fd, fdset);
5292f786681SBrian Somers }
5302f786681SBrian Somers
5312f786681SBrian Somers static void
bundle_DescriptorRead(struct fdescriptor * d __unused,struct bundle * bundle,const fd_set * fdset)532057f1760SBrian Somers bundle_DescriptorRead(struct fdescriptor *d __unused, struct bundle *bundle,
5332f786681SBrian Somers const fd_set *fdset)
5342f786681SBrian Somers {
5352f786681SBrian Somers struct datalink *dl;
5360a4b6c5cSBrian Somers unsigned secs;
53730949fd4SBrian Somers u_int32_t af;
5382f786681SBrian Somers
539ea722969SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
540ea722969SBrian Somers descriptor_Read(&bundle->ncp.mp.server.desc, bundle, fdset);
541ea722969SBrian Somers
5422f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next)
5432f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset))
5442f786681SBrian Somers descriptor_Read(&dl->desc, bundle, fdset);
545b6217683SBrian Somers
546f0cdd9c0SBrian Somers #ifndef NORADIUS
547f0cdd9c0SBrian Somers if (descriptor_IsSet(&bundle->radius.desc, fdset))
548f0cdd9c0SBrian Somers descriptor_Read(&bundle->radius.desc, bundle, fdset);
549f0cdd9c0SBrian Somers #endif
550f0cdd9c0SBrian Somers
551faefde08SBrian Somers if (FD_ISSET(bundle->dev.fd, fdset)) {
552078c562eSBrian Somers struct tun_data tun;
553078c562eSBrian Somers int n, pri;
554c1d57c38SBrian Somers u_char *data;
5553a7b6d76SBrian Somers size_t sz;
5563a7b6d76SBrian Somers
5573a7b6d76SBrian Somers if (bundle->dev.header) {
558c1d57c38SBrian Somers data = (u_char *)&tun;
5593a7b6d76SBrian Somers sz = sizeof tun;
5603a7b6d76SBrian Somers } else {
5613a7b6d76SBrian Somers data = tun.data;
5623a7b6d76SBrian Somers sz = sizeof tun.data;
5633a7b6d76SBrian Somers }
564078c562eSBrian Somers
565078c562eSBrian Somers /* something to read from tun */
5663a7b6d76SBrian Somers
5673a7b6d76SBrian Somers n = read(bundle->dev.fd, data, sz);
568078c562eSBrian Somers if (n < 0) {
5693a7b6d76SBrian Somers log_Printf(LogWARN, "%s: read: %s\n", bundle->dev.Name, strerror(errno));
570078c562eSBrian Somers return;
571078c562eSBrian Somers }
5723a7b6d76SBrian Somers
5733a7b6d76SBrian Somers if (bundle->dev.header) {
5743a7b6d76SBrian Somers n -= sz - sizeof tun.data;
575078c562eSBrian Somers if (n <= 0) {
5763a7b6d76SBrian Somers log_Printf(LogERROR, "%s: read: Got only %d bytes of data !\n",
5773a7b6d76SBrian Somers bundle->dev.Name, n);
578078c562eSBrian Somers return;
579078c562eSBrian Somers }
58030949fd4SBrian Somers af = ntohl(tun.header.family);
58130949fd4SBrian Somers #ifndef NOINET6
5821136c6acSBrian Somers if (af != AF_INET && af != AF_INET6)
58330949fd4SBrian Somers #else
58430949fd4SBrian Somers if (af != AF_INET)
58530949fd4SBrian Somers #endif
5863a7b6d76SBrian Somers /* XXX: Should be maintaining drop/family counts ! */
587078c562eSBrian Somers return;
58830949fd4SBrian Somers } else
58930949fd4SBrian Somers af = AF_INET;
590078c562eSBrian Somers
59130949fd4SBrian Somers if (af == AF_INET && ((struct ip *)tun.data)->ip_dst.s_addr ==
592078c562eSBrian Somers bundle->ncp.ipcp.my_ip.s_addr) {
593078c562eSBrian Somers /* we've been asked to send something addressed *to* us :( */
594078c562eSBrian Somers if (Enabled(bundle, OPT_LOOPBACK)) {
59530949fd4SBrian Somers pri = PacketCheck(bundle, af, tun.data, n, &bundle->filter.in,
59630949fd4SBrian Somers NULL, NULL);
597078c562eSBrian Somers if (pri >= 0) {
5983a7b6d76SBrian Somers n += sz - sizeof tun.data;
5993a7b6d76SBrian Somers write(bundle->dev.fd, data, n);
600078c562eSBrian Somers log_Printf(LogDEBUG, "Looped back packet addressed to myself\n");
601078c562eSBrian Somers }
602078c562eSBrian Somers return;
603078c562eSBrian Somers } else
604078c562eSBrian Somers log_Printf(LogDEBUG, "Oops - forwarding packet addressed to myself\n");
605078c562eSBrian Somers }
606078c562eSBrian Somers
607078c562eSBrian Somers /*
60830949fd4SBrian Somers * Process on-demand dialup. Output packets are queued within the tunnel
60930949fd4SBrian Somers * device until the appropriate NCP is opened.
610078c562eSBrian Somers */
611078c562eSBrian Somers
612078c562eSBrian Somers if (bundle_Phase(bundle) == PHASE_DEAD) {
613078c562eSBrian Somers /*
614078c562eSBrian Somers * Note, we must be in AUTO mode :-/ otherwise our interface should
615078c562eSBrian Somers * *not* be UP and we can't receive data
616078c562eSBrian Somers */
61730949fd4SBrian Somers pri = PacketCheck(bundle, af, tun.data, n, &bundle->filter.dial,
61830949fd4SBrian Somers NULL, NULL);
619040cfe28SBrian Somers if (pri >= 0)
620ba23f397SBrian Somers bundle_Open(bundle, NULL, PHYS_AUTO, 0);
621078c562eSBrian Somers else
622078c562eSBrian Somers /*
623078c562eSBrian Somers * Drop the packet. If we were to queue it, we'd just end up with
624078c562eSBrian Somers * a pile of timed-out data in our output queue by the time we get
625078c562eSBrian Somers * around to actually dialing. We'd also prematurely reach the
626078c562eSBrian Somers * threshold at which we stop select()ing to read() the tun
627078c562eSBrian Somers * device - breaking auto-dial.
628078c562eSBrian Somers */
629078c562eSBrian Somers return;
630078c562eSBrian Somers }
631078c562eSBrian Somers
6320a4b6c5cSBrian Somers secs = 0;
63330949fd4SBrian Somers pri = PacketCheck(bundle, af, tun.data, n, &bundle->filter.out,
63430949fd4SBrian Somers NULL, &secs);
6350a4b6c5cSBrian Somers if (pri >= 0) {
6360a4b6c5cSBrian Somers /* Prepend the number of seconds timeout given in the filter */
6370a4b6c5cSBrian Somers tun.header.timeout = secs;
63830949fd4SBrian Somers ncp_Enqueue(&bundle->ncp, af, pri, (char *)&tun, n + sizeof tun.header);
6390a4b6c5cSBrian Somers }
640078c562eSBrian Somers }
641078c562eSBrian Somers }
6422f786681SBrian Somers
6431af29a6eSBrian Somers static int
bundle_DescriptorWrite(struct fdescriptor * d __unused,struct bundle * bundle,const fd_set * fdset)644057f1760SBrian Somers bundle_DescriptorWrite(struct fdescriptor *d __unused, struct bundle *bundle,
6452f786681SBrian Somers const fd_set *fdset)
6462f786681SBrian Somers {
6472f786681SBrian Somers struct datalink *dl;
6481af29a6eSBrian Somers int result = 0;
6492f786681SBrian Somers
650ea722969SBrian Somers /* This is not actually necessary as struct mpserver doesn't Write() */
651ea722969SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
652fb11a9c2SBrian Somers if (descriptor_Write(&bundle->ncp.mp.server.desc, bundle, fdset) == 1)
653fb11a9c2SBrian Somers result++;
654ea722969SBrian Somers
6552f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next)
6562f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset))
657fb11a9c2SBrian Somers switch (descriptor_Write(&dl->desc, bundle, fdset)) {
658fb11a9c2SBrian Somers case -1:
659fb11a9c2SBrian Somers datalink_ComeDown(dl, CLOSE_NORMAL);
660fb11a9c2SBrian Somers break;
661fb11a9c2SBrian Somers case 1:
662fb11a9c2SBrian Somers result++;
663fb11a9c2SBrian Somers }
6641af29a6eSBrian Somers
6651af29a6eSBrian Somers return result;
6662f786681SBrian Somers }
6672f786681SBrian Somers
668da66dd13SBrian Somers void
bundle_LockTun(struct bundle * bundle)6691384bd27SBrian Somers bundle_LockTun(struct bundle *bundle)
6701384bd27SBrian Somers {
6711384bd27SBrian Somers FILE *lockfile;
67252847614SBrian Somers char pidfile[PATH_MAX];
6731384bd27SBrian Somers
6741384bd27SBrian Somers snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit);
6751384bd27SBrian Somers lockfile = ID0fopen(pidfile, "w");
6761384bd27SBrian Somers if (lockfile != NULL) {
6771384bd27SBrian Somers fprintf(lockfile, "%d\n", (int)getpid());
6781384bd27SBrian Somers fclose(lockfile);
67928c8ec2fSWarner Losh } else
6801384bd27SBrian Somers log_Printf(LogERROR, "Warning: Can't create %s: %s\n",
6811384bd27SBrian Somers pidfile, strerror(errno));
6821384bd27SBrian Somers }
6831384bd27SBrian Somers
6841384bd27SBrian Somers static void
bundle_UnlockTun(struct bundle * bundle)6851384bd27SBrian Somers bundle_UnlockTun(struct bundle *bundle)
6861384bd27SBrian Somers {
68752847614SBrian Somers char pidfile[PATH_MAX];
6881384bd27SBrian Somers
6891384bd27SBrian Somers snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit);
6901384bd27SBrian Somers ID0unlink(pidfile);
6911384bd27SBrian Somers }
6927a6f8720SBrian Somers
6937a6f8720SBrian Somers struct bundle *
bundle_Create(const char * prefix,int type,int unit)694cf0a3940SBrian Somers bundle_Create(const char *prefix, int type, int unit)
6957a6f8720SBrian Somers {
6967a6f8720SBrian Somers static struct bundle bundle; /* there can be only one */
697c0593e34SBrian Somers int enoentcount, err, minunit, maxunit;
6984e5196e9SBrian Somers const char *ifname;
699fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
700fc3034caSBrian Somers int kldtried;
701fc3034caSBrian Somers #endif
7023a7b6d76SBrian Somers #if defined(TUNSIFMODE) || defined(TUNSLMODE) || defined(TUNSIFHEAD)
7030f203c7eSBrian Somers int iff;
7040f203c7eSBrian Somers #endif
7057a6f8720SBrian Somers
7068fa6ebe4SBrian Somers if (bundle.iface != NULL) { /* Already allocated ! */
707a33b2ef7SBrian Somers log_Printf(LogALERT, "bundle_Create: There's only one BUNDLE !\n");
7087a6f8720SBrian Somers return NULL;
7097a6f8720SBrian Somers }
7107a6f8720SBrian Somers
711c0593e34SBrian Somers if (unit == -1) {
712c0593e34SBrian Somers minunit = 0;
713c0593e34SBrian Somers maxunit = -1;
714c0593e34SBrian Somers } else {
715c0593e34SBrian Somers minunit = unit;
716c0593e34SBrian Somers maxunit = unit + 1;
717c0593e34SBrian Somers }
7187a6f8720SBrian Somers err = ENOENT;
7197a6f8720SBrian Somers enoentcount = 0;
720fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
721fc3034caSBrian Somers kldtried = 0;
722fc3034caSBrian Somers #endif
723c0593e34SBrian Somers for (bundle.unit = minunit; bundle.unit != maxunit; bundle.unit++) {
724faefde08SBrian Somers snprintf(bundle.dev.Name, sizeof bundle.dev.Name, "%s%d",
725faefde08SBrian Somers prefix, bundle.unit);
726faefde08SBrian Somers bundle.dev.fd = ID0open(bundle.dev.Name, O_RDWR);
727faefde08SBrian Somers if (bundle.dev.fd >= 0)
7287a6f8720SBrian Somers break;
729728ef5b2SBrian Somers else if (errno == ENXIO || errno == ENOENT) {
730fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
731c0593e34SBrian Somers if (bundle.unit == minunit && !kldtried++) {
732fc3034caSBrian Somers /*
733fdb4bb1bSBrian Somers * Attempt to load the tunnel interface KLD if it isn't loaded
734fdb4bb1bSBrian Somers * already.
735fc3034caSBrian Somers */
7365476d2e5SBrian Somers if (loadmodules(LOAD_VERBOSLY, "if_tun", NULL))
7375476d2e5SBrian Somers bundle.unit--;
738fc3034caSBrian Somers continue;
739fc3034caSBrian Somers }
740fc3034caSBrian Somers #endif
741c4c6616aSBrian Somers if (errno != ENOENT || ++enoentcount > 2) {
7427a6f8720SBrian Somers err = errno;
743107d62e7SBrian Somers break;
744c4c6616aSBrian Somers }
7457a6f8720SBrian Somers } else
7467a6f8720SBrian Somers err = errno;
7477a6f8720SBrian Somers }
7487a6f8720SBrian Somers
749faefde08SBrian Somers if (bundle.dev.fd < 0) {
750c0593e34SBrian Somers if (unit == -1)
751c0593e34SBrian Somers log_Printf(LogWARN, "No available tunnel devices found (%s)\n",
75285b542cfSBrian Somers strerror(err));
753c0593e34SBrian Somers else
754c0593e34SBrian Somers log_Printf(LogWARN, "%s%d: %s\n", prefix, unit, strerror(err));
7557a6f8720SBrian Somers return NULL;
7567a6f8720SBrian Somers }
7577a6f8720SBrian Somers
758f2f5156fSBrian Somers log_SetTun(bundle.unit, NULL);
7597a6f8720SBrian Somers
7608fa6ebe4SBrian Somers ifname = strrchr(bundle.dev.Name, '/');
7618fa6ebe4SBrian Somers if (ifname == NULL)
7628fa6ebe4SBrian Somers ifname = bundle.dev.Name;
7637a6f8720SBrian Somers else
7648fa6ebe4SBrian Somers ifname++;
7658fa6ebe4SBrian Somers
7668fa6ebe4SBrian Somers bundle.iface = iface_Create(ifname);
7678fa6ebe4SBrian Somers if (bundle.iface == NULL) {
7688fa6ebe4SBrian Somers close(bundle.dev.fd);
7698fa6ebe4SBrian Somers return NULL;
7708fa6ebe4SBrian Somers }
7717a6f8720SBrian Somers
7720f203c7eSBrian Somers #ifdef TUNSIFMODE
773ebdcbc67SBrian Somers /* Make sure we're POINTOPOINT & IFF_MULTICAST */
774ebdcbc67SBrian Somers iff = IFF_POINTOPOINT | IFF_MULTICAST;
7750f203c7eSBrian Somers if (ID0ioctl(bundle.dev.fd, TUNSIFMODE, &iff) < 0)
7760f203c7eSBrian Somers log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFMODE): %s\n",
7770f203c7eSBrian Somers strerror(errno));
7780f203c7eSBrian Somers #endif
7790f203c7eSBrian Somers
7806ca65df0SBrian Somers #ifdef TUNSLMODE
7813a7b6d76SBrian Somers /* Make sure we're not prepending sockaddrs */
7826ca65df0SBrian Somers iff = 0;
7836ca65df0SBrian Somers if (ID0ioctl(bundle.dev.fd, TUNSLMODE, &iff) < 0)
7846ca65df0SBrian Somers log_Printf(LogERROR, "bundle_Create: ioctl(TUNSLMODE): %s\n",
7856ca65df0SBrian Somers strerror(errno));
7866ca65df0SBrian Somers #endif
7876ca65df0SBrian Somers
7883a7b6d76SBrian Somers #ifdef TUNSIFHEAD
7893a7b6d76SBrian Somers /* We want the address family please ! */
7903a7b6d76SBrian Somers iff = 1;
7913a7b6d76SBrian Somers if (ID0ioctl(bundle.dev.fd, TUNSIFHEAD, &iff) < 0) {
7923a7b6d76SBrian Somers log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFHEAD): %s\n",
7933a7b6d76SBrian Somers strerror(errno));
7943a7b6d76SBrian Somers bundle.dev.header = 0;
7953a7b6d76SBrian Somers } else
7963a7b6d76SBrian Somers bundle.dev.header = 1;
7973a7b6d76SBrian Somers #else
7983a7b6d76SBrian Somers #ifdef __OpenBSD__
7993a7b6d76SBrian Somers /* Always present for OpenBSD */
8003a7b6d76SBrian Somers bundle.dev.header = 1;
8013a7b6d76SBrian Somers #else
8023a7b6d76SBrian Somers /*
8033a7b6d76SBrian Somers * If TUNSIFHEAD isn't available and we're not OpenBSD, assume
8043a7b6d76SBrian Somers * everything's AF_INET (hopefully the tun device won't pass us
8053a7b6d76SBrian Somers * anything else !).
8063a7b6d76SBrian Somers */
8073a7b6d76SBrian Somers bundle.dev.header = 0;
8083a7b6d76SBrian Somers #endif
8093a7b6d76SBrian Somers #endif
8103a7b6d76SBrian Somers
8118fa6ebe4SBrian Somers log_Printf(LogPHASE, "Using interface: %s\n", ifname);
8127a6f8720SBrian Somers
813ab2de065SBrian Somers bundle.bandwidth = 0;
814820de6ebSBrian Somers bundle.routing_seq = 0;
815a0cbd833SBrian Somers bundle.phase = PHASE_DEAD;
816a0cbd833SBrian Somers bundle.CleaningUp = 0;
81767b072f7SBrian Somers bundle.NatEnabled = 0;
8187a6f8720SBrian Somers
8196d666775SBrian Somers bundle.fsm.LayerStart = bundle_LayerStart;
8206f384573SBrian Somers bundle.fsm.LayerUp = bundle_LayerUp;
8216d666775SBrian Somers bundle.fsm.LayerDown = bundle_LayerDown;
8226d666775SBrian Somers bundle.fsm.LayerFinish = bundle_LayerFinish;
8236d666775SBrian Somers bundle.fsm.object = &bundle;
8247a6f8720SBrian Somers
825dade2407SBrian Somers bundle.cfg.idle.timeout = NCP_IDLE_TIMEOUT;
826dade2407SBrian Somers bundle.cfg.idle.min_timeout = 0;
8271342caedSBrian Somers *bundle.cfg.auth.name = '\0';
8281342caedSBrian Somers *bundle.cfg.auth.key = '\0';
8290508c09aSBrian Somers bundle.cfg.optmask = (1ull << OPT_IDCHECK) | (1ull << OPT_LOOPBACK) |
8300508c09aSBrian Somers (1ull << OPT_SROUTES) | (1ull << OPT_TCPMSSFIXUP) |
8310508c09aSBrian Somers (1ull << OPT_THROUGHPUT) | (1ull << OPT_UTMP) |
8320508c09aSBrian Somers (1ull << OPT_NAS_IP_ADDRESS) |
8330508c09aSBrian Somers (1ull << OPT_NAS_IDENTIFIER);
834971abb29SBrian Somers #ifndef NOINET6
8350508c09aSBrian Somers opt_enable(&bundle, OPT_IPCP);
836971abb29SBrian Somers if (probe.ipv6_available)
8370508c09aSBrian Somers opt_enable(&bundle, OPT_IPV6CP);
838971abb29SBrian Somers #endif
83949052c95SBrian Somers *bundle.cfg.label = '\0';
8406c1d6731SBrian Somers bundle.cfg.ifqueue = DEF_IFQUEUE;
8416f8e9f0aSBrian Somers bundle.cfg.choked.timeout = CHOKED_TIMEOUT;
842ff0f9439SBrian Somers bundle.phys_type.all = type;
843ff0f9439SBrian Somers bundle.phys_type.open = 0;
844dade2407SBrian Somers bundle.upat = 0;
845ab886ad0SBrian Somers
8466f384573SBrian Somers bundle.links = datalink_Create("deflink", &bundle, type);
8473006ec67SBrian Somers if (bundle.links == NULL) {
848a33b2ef7SBrian Somers log_Printf(LogALERT, "Cannot create data link: %s\n", strerror(errno));
849f2f5156fSBrian Somers iface_Free(bundle.iface);
8508fa6ebe4SBrian Somers bundle.iface = NULL;
851faefde08SBrian Somers close(bundle.dev.fd);
8522289f246SBrian Somers return NULL;
8532289f246SBrian Somers }
8542289f246SBrian Somers
8552f786681SBrian Somers bundle.desc.type = BUNDLE_DESCRIPTOR;
8562f786681SBrian Somers bundle.desc.UpdateSet = bundle_UpdateSet;
8572f786681SBrian Somers bundle.desc.IsSet = bundle_IsSet;
8582f786681SBrian Somers bundle.desc.Read = bundle_DescriptorRead;
8592f786681SBrian Somers bundle.desc.Write = bundle_DescriptorWrite;
8602f786681SBrian Somers
86130949fd4SBrian Somers ncp_Init(&bundle.ncp, &bundle);
8626d666775SBrian Somers
8635ca5389aSBrian Somers memset(&bundle.filter, '\0', sizeof bundle.filter);
8645ca5389aSBrian Somers bundle.filter.in.fragok = bundle.filter.in.logok = 1;
8655ca5389aSBrian Somers bundle.filter.in.name = "IN";
8665ca5389aSBrian Somers bundle.filter.out.fragok = bundle.filter.out.logok = 1;
8675ca5389aSBrian Somers bundle.filter.out.name = "OUT";
8685ca5389aSBrian Somers bundle.filter.dial.name = "DIAL";
8698390b576SBrian Somers bundle.filter.dial.logok = 1;
8705ca5389aSBrian Somers bundle.filter.alive.name = "ALIVE";
8715ca5389aSBrian Somers bundle.filter.alive.logok = 1;
872cad7e742SBrian Somers {
873cad7e742SBrian Somers int i;
874cad7e742SBrian Somers for (i = 0; i < MAXFILTERS; i++) {
875cad7e742SBrian Somers bundle.filter.in.rule[i].f_action = A_NONE;
876cad7e742SBrian Somers bundle.filter.out.rule[i].f_action = A_NONE;
877cad7e742SBrian Somers bundle.filter.dial.rule[i].f_action = A_NONE;
878cad7e742SBrian Somers bundle.filter.alive.rule[i].f_action = A_NONE;
879cad7e742SBrian Somers }
880cad7e742SBrian Somers }
88193ee0ff2SBrian Somers memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer);
88293ee0ff2SBrian Somers bundle.idle.done = 0;
8835cf4388bSBrian Somers bundle.notify.fd = -1;
8846f8e9f0aSBrian Somers memset(&bundle.choked.timer, '\0', sizeof bundle.choked.timer);
885972a1bcfSBrian Somers #ifndef NORADIUS
886972a1bcfSBrian Somers radius_Init(&bundle.radius);
887972a1bcfSBrian Somers #endif
88893ee0ff2SBrian Somers
8896d666775SBrian Somers /* Clean out any leftover crud */
89030949fd4SBrian Somers iface_Clear(bundle.iface, &bundle.ncp, 0, IFACE_CLEAR_ALL);
8916d666775SBrian Somers
8921384bd27SBrian Somers bundle_LockTun(&bundle);
8931384bd27SBrian Somers
8947a6f8720SBrian Somers return &bundle;
8957a6f8720SBrian Somers }
8967a6f8720SBrian Somers
89768a0f0ccSBrian Somers static void
bundle_DownInterface(struct bundle * bundle)89868a0f0ccSBrian Somers bundle_DownInterface(struct bundle *bundle)
89968a0f0ccSBrian Somers {
900dd7e2610SBrian Somers route_IfDelete(bundle, 1);
901dc744e19SBrian Somers iface_ClearFlags(bundle->iface->name, IFF_UP);
90268a0f0ccSBrian Somers }
90368a0f0ccSBrian Somers
90468a0f0ccSBrian Somers void
bundle_Destroy(struct bundle * bundle)90568a0f0ccSBrian Somers bundle_Destroy(struct bundle *bundle)
90668a0f0ccSBrian Somers {
9073006ec67SBrian Somers struct datalink *dl;
908b6217683SBrian Somers
909ea722969SBrian Somers /*
91030949fd4SBrian Somers * Clean up the interface. We don't really need to do the timer_Stop()s,
91130949fd4SBrian Somers * mp_Down(), iface_Clear() and bundle_DownInterface() unless we're getting
9128e7bd08eSBrian Somers * out under exceptional conditions such as a descriptor exception.
913ea722969SBrian Somers */
91404eaa58cSBrian Somers timer_Stop(&bundle->idle.timer);
9156f8e9f0aSBrian Somers timer_Stop(&bundle->choked.timer);
91666f634b6SBrian Somers mp_Down(&bundle->ncp.mp);
91730949fd4SBrian Somers iface_Clear(bundle->iface, &bundle->ncp, 0, IFACE_CLEAR_ALL);
91868a0f0ccSBrian Somers bundle_DownInterface(bundle);
9193006ec67SBrian Somers
920972a1bcfSBrian Somers #ifndef NORADIUS
921972a1bcfSBrian Somers /* Tell the radius server the bad news */
922972a1bcfSBrian Somers radius_Destroy(&bundle->radius);
923972a1bcfSBrian Somers #endif
924972a1bcfSBrian Somers
925ea722969SBrian Somers /* Again, these are all DATALINK_CLOSED unless we're abending */
9263006ec67SBrian Somers dl = bundle->links;
9273006ec67SBrian Somers while (dl)
9283006ec67SBrian Somers dl = datalink_Destroy(dl);
9293006ec67SBrian Somers
93030949fd4SBrian Somers ncp_Destroy(&bundle->ncp);
931442f8495SBrian Somers
9321384bd27SBrian Somers close(bundle->dev.fd);
9331384bd27SBrian Somers bundle_UnlockTun(bundle);
9341384bd27SBrian Somers
935ea722969SBrian Somers /* In case we never made PHASE_NETWORK */
9365cf4388bSBrian Somers bundle_Notify(bundle, EX_ERRDEAD);
937b6217683SBrian Somers
9388fa6ebe4SBrian Somers iface_Destroy(bundle->iface);
9398fa6ebe4SBrian Somers bundle->iface = NULL;
94068a0f0ccSBrian Somers }
94168a0f0ccSBrian Somers
94283d1af55SBrian Somers void
bundle_LinkClosed(struct bundle * bundle,struct datalink * dl)9433006ec67SBrian Somers bundle_LinkClosed(struct bundle *bundle, struct datalink *dl)
9443006ec67SBrian Somers {
9453006ec67SBrian Somers /*
9463006ec67SBrian Somers * Our datalink has closed.
947ea722969SBrian Somers * CleanDatalinks() (called from DoLoop()) will remove closed
948f6a4e748SBrian Somers * BACKGROUND, FOREGROUND and DIRECT links.
9493b0f8d2eSBrian Somers * If it's the last data link, enter phase DEAD.
950ea722969SBrian Somers *
951ea722969SBrian Somers * NOTE: dl may not be in our list (bundle_SendDatalink()) !
9523006ec67SBrian Somers */
9535b8b8060SBrian Somers
9543b0f8d2eSBrian Somers struct datalink *odl;
9553b0f8d2eSBrian Somers int other_links;
9565b8b8060SBrian Somers
957bf1d3ff6SBrian Somers log_SetTtyCommandMode(dl);
958bf1d3ff6SBrian Somers
9593b0f8d2eSBrian Somers other_links = 0;
9603b0f8d2eSBrian Somers for (odl = bundle->links; odl; odl = odl->next)
9613b0f8d2eSBrian Somers if (odl != dl && odl->state != DATALINK_CLOSED)
9623b0f8d2eSBrian Somers other_links++;
9633b0f8d2eSBrian Somers
9643b0f8d2eSBrian Somers if (!other_links) {
96581358fa3SBrian Somers if (dl->physical->type != PHYS_AUTO) /* Not in -auto mode */
96603704096SBrian Somers bundle_DownInterface(bundle);
96730949fd4SBrian Somers ncp2initial(&bundle->ncp);
968356bf92dSBrian Somers mp_Down(&bundle->ncp.mp);
9695563ebdeSBrian Somers bundle_NewPhase(bundle, PHASE_DEAD);
970bf1eaec5SBrian Somers #ifndef NORADIUS
971bf1eaec5SBrian Somers if (bundle->radius.sessiontime)
972bf1eaec5SBrian Somers bundle_StopSessionTimer(bundle);
973bf1eaec5SBrian Somers #endif
9740f2f3eb3SBrian Somers bundle_StopIdleTimer(bundle);
975ab2de065SBrian Somers }
976455aabc3SBrian Somers }
977455aabc3SBrian Somers
978455aabc3SBrian Somers void
bundle_Open(struct bundle * bundle,const char * name,int mask,int force)979ba23f397SBrian Somers bundle_Open(struct bundle *bundle, const char *name, int mask, int force)
9803006ec67SBrian Somers {
9813006ec67SBrian Somers /*
9823006ec67SBrian Somers * Please open the given datalink, or all if name == NULL
9833006ec67SBrian Somers */
9843006ec67SBrian Somers struct datalink *dl;
9853006ec67SBrian Somers
9863006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next)
9873006ec67SBrian Somers if (name == NULL || !strcasecmp(dl->name, name)) {
988ba23f397SBrian Somers if ((mask & dl->physical->type) &&
989ba23f397SBrian Somers (dl->state == DATALINK_CLOSED ||
990ba23f397SBrian Somers (force && dl->state == DATALINK_OPENING &&
9915e269efeSBrian Somers dl->dial.timer.state == TIMER_RUNNING) ||
9925e269efeSBrian Somers dl->state == DATALINK_READY)) {
9935e269efeSBrian Somers timer_Stop(&dl->dial.timer); /* We're finished with this */
994565e35e5SBrian Somers datalink_Up(dl, 1, 1);
995ab2de065SBrian Somers if (mask & PHYS_AUTO)
9965e269efeSBrian Somers break; /* Only one AUTO link at a time */
99704eaa58cSBrian Somers }
9983006ec67SBrian Somers if (name != NULL)
9993006ec67SBrian Somers break;
10003006ec67SBrian Somers }
10013006ec67SBrian Somers }
10023006ec67SBrian Somers
10033006ec67SBrian Somers struct datalink *
bundle2datalink(struct bundle * bundle,const char * name)10043006ec67SBrian Somers bundle2datalink(struct bundle *bundle, const char *name)
10053006ec67SBrian Somers {
10063006ec67SBrian Somers struct datalink *dl;
10073006ec67SBrian Somers
10083006ec67SBrian Somers if (name != NULL) {
10093006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next)
10103006ec67SBrian Somers if (!strcasecmp(dl->name, name))
10113006ec67SBrian Somers return dl;
10123006ec67SBrian Somers } else if (bundle->links && !bundle->links->next)
10133006ec67SBrian Somers return bundle->links;
10143006ec67SBrian Somers
10153006ec67SBrian Somers return NULL;
10163006ec67SBrian Somers }
10173006ec67SBrian Somers
10183006ec67SBrian Somers int
bundle_ShowLinks(struct cmdargs const * arg)1019aef795ccSBrian Somers bundle_ShowLinks(struct cmdargs const *arg)
1020aef795ccSBrian Somers {
1021aef795ccSBrian Somers struct datalink *dl;
1022ab2de065SBrian Somers struct pppThroughput *t;
102391cbd2eeSBrian Somers unsigned long long octets;
1024ab2de065SBrian Somers int secs;
1025aef795ccSBrian Somers
10269c53a7b1SBrian Somers for (dl = arg->bundle->links; dl; dl = dl->next) {
102791cbd2eeSBrian Somers octets = MAX(dl->physical->link.stats.total.in.OctetsPerSecond,
102891cbd2eeSBrian Somers dl->physical->link.stats.total.out.OctetsPerSecond);
102991cbd2eeSBrian Somers
1030d4156d00SBrian Somers prompt_Printf(arg->prompt, "Name: %s [%s, %s]",
1031d4156d00SBrian Somers dl->name, mode2Nam(dl->physical->type), datalink_State(dl));
103211572abfSBrian Somers if (dl->physical->link.stats.total.rolling && dl->state == DATALINK_OPEN)
1033e531f89aSBrian Somers prompt_Printf(arg->prompt, " bandwidth %d, %llu bps (%llu bytes/sec)",
1034ab2de065SBrian Somers dl->mp.bandwidth ? dl->mp.bandwidth :
1035ab2de065SBrian Somers physical_GetSpeed(dl->physical),
103691cbd2eeSBrian Somers octets * 8, octets);
10379c53a7b1SBrian Somers prompt_Printf(arg->prompt, "\n");
10389c53a7b1SBrian Somers }
1039aef795ccSBrian Somers
104011572abfSBrian Somers t = &arg->bundle->ncp.mp.link.stats.total;
104191cbd2eeSBrian Somers octets = MAX(t->in.OctetsPerSecond, t->out.OctetsPerSecond);
1042ab2de065SBrian Somers secs = t->downtime ? 0 : throughput_uptime(t);
1043ab2de065SBrian Somers if (secs > t->SamplePeriod)
1044ab2de065SBrian Somers secs = t->SamplePeriod;
1045ab2de065SBrian Somers if (secs)
1046e531f89aSBrian Somers prompt_Printf(arg->prompt, "Currently averaging %llu bps (%llu bytes/sec)"
104791cbd2eeSBrian Somers " over the last %d secs\n", octets * 8, octets, secs);
1048ab2de065SBrian Somers
1049aef795ccSBrian Somers return 0;
1050aef795ccSBrian Somers }
1051ab886ad0SBrian Somers
10521342caedSBrian Somers static const char *
optval(struct bundle * bundle,int opt)10530508c09aSBrian Somers optval(struct bundle *bundle, int opt)
10541342caedSBrian Somers {
10550508c09aSBrian Somers return Enabled(bundle, opt) ? "enabled" : "disabled";
10561342caedSBrian Somers }
10571342caedSBrian Somers
1058c08717dfSBrian Somers int
bundle_ShowStatus(struct cmdargs const * arg)1059c08717dfSBrian Somers bundle_ShowStatus(struct cmdargs const *arg)
1060c08717dfSBrian Somers {
1061c08717dfSBrian Somers int remaining;
1062c08717dfSBrian Somers
1063c08717dfSBrian Somers prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle));
1064faefde08SBrian Somers prompt_Printf(arg->prompt, " Device: %s\n", arg->bundle->dev.Name);
1065dade2407SBrian Somers prompt_Printf(arg->prompt, " Interface: %s @ %lubps",
1066ab2de065SBrian Somers arg->bundle->iface->name, arg->bundle->bandwidth);
1067c08717dfSBrian Somers
1068dade2407SBrian Somers if (arg->bundle->upat) {
106946df5aa7SBrian Somers int secs = bundle_Uptime(arg->bundle);
1070dade2407SBrian Somers
1071dade2407SBrian Somers prompt_Printf(arg->prompt, ", up time %d:%02d:%02d", secs / 3600,
1072dade2407SBrian Somers (secs / 60) % 60, secs % 60);
1073dade2407SBrian Somers }
1074669b9965SBrian Somers prompt_Printf(arg->prompt, "\n Queued: %lu of %u\n",
107530949fd4SBrian Somers (unsigned long)ncp_QueueLen(&arg->bundle->ncp),
107677fc031dSBrian Somers arg->bundle->cfg.ifqueue);
1077dade2407SBrian Somers
10786c1d6731SBrian Somers prompt_Printf(arg->prompt, "\nDefaults:\n");
107974457d3dSBrian Somers prompt_Printf(arg->prompt, " Label: %s\n",
108074457d3dSBrian Somers arg->bundle->cfg.label);
1081610b185fSBrian Somers prompt_Printf(arg->prompt, " Auth name: %s\n",
1082610b185fSBrian Somers arg->bundle->cfg.auth.name);
108374457d3dSBrian Somers prompt_Printf(arg->prompt, " Diagnostic socket: ");
108437b8a5c7SBrian Somers if (*server.cfg.sockname != '\0') {
108537b8a5c7SBrian Somers prompt_Printf(arg->prompt, "%s", server.cfg.sockname);
108637b8a5c7SBrian Somers if (server.cfg.mask != (mode_t)-1)
108737b8a5c7SBrian Somers prompt_Printf(arg->prompt, ", mask 0%03o", (int)server.cfg.mask);
108837b8a5c7SBrian Somers prompt_Printf(arg->prompt, "%s\n", server.fd == -1 ? " (not open)" : "");
108937b8a5c7SBrian Somers } else if (server.cfg.port != 0)
109074457d3dSBrian Somers prompt_Printf(arg->prompt, "TCP port %d%s\n", server.cfg.port,
109174457d3dSBrian Somers server.fd == -1 ? " (not open)" : "");
109274457d3dSBrian Somers else
109374457d3dSBrian Somers prompt_Printf(arg->prompt, "none\n");
109404eaa58cSBrian Somers
1095057f1760SBrian Somers prompt_Printf(arg->prompt, " Choked Timer: %us\n",
10966f8e9f0aSBrian Somers arg->bundle->cfg.choked.timeout);
1097972a1bcfSBrian Somers
1098972a1bcfSBrian Somers #ifndef NORADIUS
1099972a1bcfSBrian Somers radius_Show(&arg->bundle->radius, arg->prompt);
1100972a1bcfSBrian Somers #endif
1101972a1bcfSBrian Somers
1102c08717dfSBrian Somers prompt_Printf(arg->prompt, " Idle Timer: ");
1103dade2407SBrian Somers if (arg->bundle->cfg.idle.timeout) {
1104057f1760SBrian Somers prompt_Printf(arg->prompt, "%us", arg->bundle->cfg.idle.timeout);
1105dade2407SBrian Somers if (arg->bundle->cfg.idle.min_timeout)
1106057f1760SBrian Somers prompt_Printf(arg->prompt, ", min %us",
1107dade2407SBrian Somers arg->bundle->cfg.idle.min_timeout);
1108c08717dfSBrian Somers remaining = bundle_RemainingIdleTime(arg->bundle);
1109c08717dfSBrian Somers if (remaining != -1)
1110c08717dfSBrian Somers prompt_Printf(arg->prompt, " (%ds remaining)", remaining);
1111c08717dfSBrian Somers prompt_Printf(arg->prompt, "\n");
1112c08717dfSBrian Somers } else
1113c08717dfSBrian Somers prompt_Printf(arg->prompt, "disabled\n");
1114c08717dfSBrian Somers
111530949fd4SBrian Somers prompt_Printf(arg->prompt, " Filter Decap: %-20.20s",
111698251667SBrian Somers optval(arg->bundle, OPT_FILTERDECAP));
111730949fd4SBrian Somers prompt_Printf(arg->prompt, " ID check: %s\n",
11181342caedSBrian Somers optval(arg->bundle, OPT_IDCHECK));
111930949fd4SBrian Somers prompt_Printf(arg->prompt, " Iface-Alias: %-20.20s",
112030949fd4SBrian Somers optval(arg->bundle, OPT_IFACEALIAS));
112130949fd4SBrian Somers #ifndef NOINET6
112230949fd4SBrian Somers prompt_Printf(arg->prompt, " IPCP: %s\n",
112330949fd4SBrian Somers optval(arg->bundle, OPT_IPCP));
112430949fd4SBrian Somers prompt_Printf(arg->prompt, " IPV6CP: %-20.20s",
112530949fd4SBrian Somers optval(arg->bundle, OPT_IPV6CP));
112630949fd4SBrian Somers #endif
112798251667SBrian Somers prompt_Printf(arg->prompt, " Keep-Session: %s\n",
1128ac685e31SBrian Somers optval(arg->bundle, OPT_KEEPSESSION));
112998251667SBrian Somers prompt_Printf(arg->prompt, " Loopback: %-20.20s",
11301342caedSBrian Somers optval(arg->bundle, OPT_LOOPBACK));
113198251667SBrian Somers prompt_Printf(arg->prompt, " PasswdAuth: %s\n",
11321342caedSBrian Somers optval(arg->bundle, OPT_PASSWDAUTH));
113398251667SBrian Somers prompt_Printf(arg->prompt, " Proxy: %-20.20s",
11341342caedSBrian Somers optval(arg->bundle, OPT_PROXY));
113598251667SBrian Somers prompt_Printf(arg->prompt, " Proxyall: %s\n",
11363afe5ccbSBrian Somers optval(arg->bundle, OPT_PROXYALL));
113730949fd4SBrian Somers prompt_Printf(arg->prompt, " Sticky Routes: %-20.20s",
113830949fd4SBrian Somers optval(arg->bundle, OPT_SROUTES));
113930949fd4SBrian Somers prompt_Printf(arg->prompt, " TCPMSS Fixup: %s\n",
114094d7be52SBrian Somers optval(arg->bundle, OPT_TCPMSSFIXUP));
114130949fd4SBrian Somers prompt_Printf(arg->prompt, " Throughput: %-20.20s",
11421342caedSBrian Somers optval(arg->bundle, OPT_THROUGHPUT));
114330949fd4SBrian Somers prompt_Printf(arg->prompt, " Utmp Logging: %s\n",
11441342caedSBrian Somers optval(arg->bundle, OPT_UTMP));
11450508c09aSBrian Somers prompt_Printf(arg->prompt, " NAS-IP-Address: %-20.20s",
11460508c09aSBrian Somers optval(arg->bundle, OPT_NAS_IP_ADDRESS));
11470508c09aSBrian Somers prompt_Printf(arg->prompt, " NAS-Identifier: %s\n",
11480508c09aSBrian Somers optval(arg->bundle, OPT_NAS_IDENTIFIER));
11491342caedSBrian Somers
1150c08717dfSBrian Somers return 0;
1151c08717dfSBrian Somers }
1152c08717dfSBrian Somers
1153ab886ad0SBrian Somers static void
bundle_IdleTimeout(void * v)1154ab886ad0SBrian Somers bundle_IdleTimeout(void *v)
1155ab886ad0SBrian Somers {
1156ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v;
1157ab886ad0SBrian Somers
1158b42135deSBrian Somers log_Printf(LogPHASE, "Idle timer expired\n");
115904eaa58cSBrian Somers bundle_StopIdleTimer(bundle);
11609c81b87dSBrian Somers bundle_Close(bundle, NULL, CLOSE_STAYDOWN);
1161ab886ad0SBrian Somers }
1162ab886ad0SBrian Somers
1163ab886ad0SBrian Somers /*
1164ab886ad0SBrian Somers * Start Idle timer. If timeout is reached, we call bundle_Close() to
1165ab886ad0SBrian Somers * close LCP and link.
1166ab886ad0SBrian Somers */
1167ab886ad0SBrian Somers void
bundle_StartIdleTimer(struct bundle * bundle,unsigned secs)11680a4b6c5cSBrian Somers bundle_StartIdleTimer(struct bundle *bundle, unsigned secs)
1169ab886ad0SBrian Somers {
1170dd7e2610SBrian Somers timer_Stop(&bundle->idle.timer);
1171ff0f9439SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) !=
1172dade2407SBrian Somers bundle->phys_type.open && bundle->cfg.idle.timeout) {
11730a4b6c5cSBrian Somers time_t now = time(NULL);
1174dade2407SBrian Somers
11750a4b6c5cSBrian Somers if (secs == 0)
1176dade2407SBrian Somers secs = bundle->cfg.idle.timeout;
11770a4b6c5cSBrian Somers
11780a4b6c5cSBrian Somers /* We want at least `secs' */
1179dade2407SBrian Somers if (bundle->cfg.idle.min_timeout > secs && bundle->upat) {
1180057f1760SBrian Somers unsigned up = now - bundle->upat;
1181dade2407SBrian Somers
1182057f1760SBrian Somers if (bundle->cfg.idle.min_timeout > up &&
1183057f1760SBrian Somers bundle->cfg.idle.min_timeout - up > (long long)secs)
11840a4b6c5cSBrian Somers /* Only increase from the current `remaining' value */
1185dade2407SBrian Somers secs = bundle->cfg.idle.min_timeout - up;
1186dade2407SBrian Somers }
118793ee0ff2SBrian Somers bundle->idle.timer.func = bundle_IdleTimeout;
11883b0f8d2eSBrian Somers bundle->idle.timer.name = "idle";
1189dade2407SBrian Somers bundle->idle.timer.load = secs * SECTICKS;
119093ee0ff2SBrian Somers bundle->idle.timer.arg = bundle;
1191dd7e2610SBrian Somers timer_Start(&bundle->idle.timer);
11920a4b6c5cSBrian Somers bundle->idle.done = now + secs;
1193ab886ad0SBrian Somers }
1194ab886ad0SBrian Somers }
1195ab886ad0SBrian Somers
1196ab886ad0SBrian Somers void
bundle_SetIdleTimer(struct bundle * bundle,unsigned timeout,unsigned min_timeout)1197057f1760SBrian Somers bundle_SetIdleTimer(struct bundle *bundle, unsigned timeout,
1198057f1760SBrian Somers unsigned min_timeout)
1199ab886ad0SBrian Somers {
1200dade2407SBrian Somers bundle->cfg.idle.timeout = timeout;
1201dade2407SBrian Somers bundle->cfg.idle.min_timeout = min_timeout;
120230949fd4SBrian Somers if (ncp_LayersOpen(&bundle->ncp))
12030a4b6c5cSBrian Somers bundle_StartIdleTimer(bundle, 0);
1204ab886ad0SBrian Somers }
1205ab886ad0SBrian Somers
1206ab886ad0SBrian Somers void
bundle_StopIdleTimer(struct bundle * bundle)1207ab886ad0SBrian Somers bundle_StopIdleTimer(struct bundle *bundle)
1208ab886ad0SBrian Somers {
1209dd7e2610SBrian Somers timer_Stop(&bundle->idle.timer);
12104a632c80SBrian Somers bundle->idle.done = 0;
1211ab886ad0SBrian Somers }
1212ab886ad0SBrian Somers
121304eaa58cSBrian Somers static int
bundle_RemainingIdleTime(struct bundle * bundle)1214ab886ad0SBrian Somers bundle_RemainingIdleTime(struct bundle *bundle)
1215ab886ad0SBrian Somers {
121693ee0ff2SBrian Somers if (bundle->idle.done)
121793ee0ff2SBrian Somers return bundle->idle.done - time(NULL);
1218ab886ad0SBrian Somers return -1;
1219ab886ad0SBrian Somers }
12203b0f8d2eSBrian Somers
1221bf1eaec5SBrian Somers #ifndef NORADIUS
1222bf1eaec5SBrian Somers
1223bf1eaec5SBrian Somers static void
bundle_SessionTimeout(void * v)1224bf1eaec5SBrian Somers bundle_SessionTimeout(void *v)
1225bf1eaec5SBrian Somers {
1226bf1eaec5SBrian Somers struct bundle *bundle = (struct bundle *)v;
1227bf1eaec5SBrian Somers
1228bf1eaec5SBrian Somers log_Printf(LogPHASE, "Session-Timeout timer expired\n");
1229bf1eaec5SBrian Somers bundle_StopSessionTimer(bundle);
1230bf1eaec5SBrian Somers bundle_Close(bundle, NULL, CLOSE_STAYDOWN);
1231bf1eaec5SBrian Somers }
1232bf1eaec5SBrian Somers
1233bf1eaec5SBrian Somers void
bundle_StartSessionTimer(struct bundle * bundle,unsigned secs)1234bf1eaec5SBrian Somers bundle_StartSessionTimer(struct bundle *bundle, unsigned secs)
1235bf1eaec5SBrian Somers {
1236bf1eaec5SBrian Somers timer_Stop(&bundle->session.timer);
1237bf1eaec5SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) !=
1238bf1eaec5SBrian Somers bundle->phys_type.open && bundle->radius.sessiontime) {
1239bf1eaec5SBrian Somers time_t now = time(NULL);
1240bf1eaec5SBrian Somers
1241bf1eaec5SBrian Somers if (secs == 0)
1242bf1eaec5SBrian Somers secs = bundle->radius.sessiontime;
1243bf1eaec5SBrian Somers
1244bf1eaec5SBrian Somers bundle->session.timer.func = bundle_SessionTimeout;
1245bf1eaec5SBrian Somers bundle->session.timer.name = "session";
1246bf1eaec5SBrian Somers bundle->session.timer.load = secs * SECTICKS;
1247bf1eaec5SBrian Somers bundle->session.timer.arg = bundle;
1248bf1eaec5SBrian Somers timer_Start(&bundle->session.timer);
1249bf1eaec5SBrian Somers bundle->session.done = now + secs;
1250bf1eaec5SBrian Somers }
1251bf1eaec5SBrian Somers }
1252bf1eaec5SBrian Somers
1253bf1eaec5SBrian Somers void
bundle_StopSessionTimer(struct bundle * bundle)1254bf1eaec5SBrian Somers bundle_StopSessionTimer(struct bundle *bundle)
1255bf1eaec5SBrian Somers {
1256bf1eaec5SBrian Somers timer_Stop(&bundle->session.timer);
1257bf1eaec5SBrian Somers bundle->session.done = 0;
1258bf1eaec5SBrian Somers }
1259bf1eaec5SBrian Somers
1260bf1eaec5SBrian Somers #endif
1261bf1eaec5SBrian Somers
12623b0f8d2eSBrian Somers int
bundle_IsDead(struct bundle * bundle)12633b0f8d2eSBrian Somers bundle_IsDead(struct bundle *bundle)
12643b0f8d2eSBrian Somers {
12653b0f8d2eSBrian Somers return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp);
12663b0f8d2eSBrian Somers }
1267b6217683SBrian Somers
126804eaa58cSBrian Somers static struct datalink *
bundle_DatalinkLinkout(struct bundle * bundle,struct datalink * dl)126904eaa58cSBrian Somers bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl)
1270cd7bd93aSBrian Somers {
1271cd7bd93aSBrian Somers struct datalink **dlp;
1272cd7bd93aSBrian Somers
1273cd7bd93aSBrian Somers for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next)
1274cd7bd93aSBrian Somers if (*dlp == dl) {
127504eaa58cSBrian Somers *dlp = dl->next;
127604eaa58cSBrian Somers dl->next = NULL;
127704eaa58cSBrian Somers bundle_LinksRemoved(bundle);
127804eaa58cSBrian Somers return dl;
1279cd7bd93aSBrian Somers }
128004eaa58cSBrian Somers
128104eaa58cSBrian Somers return NULL;
128204eaa58cSBrian Somers }
128304eaa58cSBrian Somers
128404eaa58cSBrian Somers static void
bundle_DatalinkLinkin(struct bundle * bundle,struct datalink * dl)128504eaa58cSBrian Somers bundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl)
128604eaa58cSBrian Somers {
128704eaa58cSBrian Somers struct datalink **dlp = &bundle->links;
128804eaa58cSBrian Somers
128904eaa58cSBrian Somers while (*dlp)
129004eaa58cSBrian Somers dlp = &(*dlp)->next;
129104eaa58cSBrian Somers
129204eaa58cSBrian Somers *dlp = dl;
129304eaa58cSBrian Somers dl->next = NULL;
129404eaa58cSBrian Somers
129504eaa58cSBrian Somers bundle_LinkAdded(bundle, dl);
1296ab2de065SBrian Somers mp_CheckAutoloadTimer(&bundle->ncp.mp);
1297565e35e5SBrian Somers }
1298565e35e5SBrian Somers
1299565e35e5SBrian Somers void
bundle_CleanDatalinks(struct bundle * bundle)1300565e35e5SBrian Somers bundle_CleanDatalinks(struct bundle *bundle)
1301565e35e5SBrian Somers {
1302565e35e5SBrian Somers struct datalink **dlp = &bundle->links;
130304eaa58cSBrian Somers int found = 0;
1304565e35e5SBrian Somers
1305565e35e5SBrian Somers while (*dlp)
1306565e35e5SBrian Somers if ((*dlp)->state == DATALINK_CLOSED &&
1307f6a4e748SBrian Somers (*dlp)->physical->type &
1308f6a4e748SBrian Somers (PHYS_DIRECT|PHYS_BACKGROUND|PHYS_FOREGROUND)) {
1309565e35e5SBrian Somers *dlp = datalink_Destroy(*dlp);
131004eaa58cSBrian Somers found++;
131104eaa58cSBrian Somers } else
1312565e35e5SBrian Somers dlp = &(*dlp)->next;
131304eaa58cSBrian Somers
131404eaa58cSBrian Somers if (found)
131504eaa58cSBrian Somers bundle_LinksRemoved(bundle);
131604eaa58cSBrian Somers }
131704eaa58cSBrian Somers
131804eaa58cSBrian Somers int
bundle_DatalinkClone(struct bundle * bundle,struct datalink * dl,const char * name)131904eaa58cSBrian Somers bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl,
132004eaa58cSBrian Somers const char *name)
132104eaa58cSBrian Somers {
132204eaa58cSBrian Somers if (bundle2datalink(bundle, name)) {
132304eaa58cSBrian Somers log_Printf(LogWARN, "Clone: %s: name already exists\n", name);
132404eaa58cSBrian Somers return 0;
132504eaa58cSBrian Somers }
132604eaa58cSBrian Somers
132704eaa58cSBrian Somers bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name));
132804eaa58cSBrian Somers return 1;
132904eaa58cSBrian Somers }
133004eaa58cSBrian Somers
133104eaa58cSBrian Somers void
bundle_DatalinkRemove(struct bundle * bundle,struct datalink * dl)133204eaa58cSBrian Somers bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl)
133304eaa58cSBrian Somers {
133404eaa58cSBrian Somers dl = bundle_DatalinkLinkout(bundle, dl);
133504eaa58cSBrian Somers if (dl)
133604eaa58cSBrian Somers datalink_Destroy(dl);
1337cd7bd93aSBrian Somers }
133849052c95SBrian Somers
133949052c95SBrian Somers void
bundle_SetLabel(struct bundle * bundle,const char * label)134049052c95SBrian Somers bundle_SetLabel(struct bundle *bundle, const char *label)
134149052c95SBrian Somers {
134249052c95SBrian Somers if (label)
134349052c95SBrian Somers strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1);
134449052c95SBrian Somers else
134549052c95SBrian Somers *bundle->cfg.label = '\0';
134649052c95SBrian Somers }
134749052c95SBrian Somers
134849052c95SBrian Somers const char *
bundle_GetLabel(struct bundle * bundle)134949052c95SBrian Somers bundle_GetLabel(struct bundle *bundle)
135049052c95SBrian Somers {
135149052c95SBrian Somers return *bundle->cfg.label ? bundle->cfg.label : NULL;
135249052c95SBrian Somers }
13531fa665f5SBrian Somers
13542cb305afSBrian Somers int
bundle_LinkSize(void)1355672eba24SJohn Baldwin bundle_LinkSize(void)
13561fa665f5SBrian Somers {
135796c9bb21SBrian Somers struct iovec iov[SCATTER_SEGMENTS];
13582cb305afSBrian Somers int niov, expect, f;
135987c3786eSBrian Somers
136096c9bb21SBrian Somers iov[0].iov_len = strlen(Version) + 1;
13612cb305afSBrian Somers iov[0].iov_base = NULL;
13622cb305afSBrian Somers niov = 1;
13632cb305afSBrian Somers if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) {
13642cb305afSBrian Somers log_Printf(LogERROR, "Cannot determine space required for link\n");
13652cb305afSBrian Somers return 0;
136654cd8e13SBrian Somers }
13676f384573SBrian Somers
136896c9bb21SBrian Somers for (f = expect = 0; f < niov; f++)
136996c9bb21SBrian Somers expect += iov[f].iov_len;
137096c9bb21SBrian Somers
13712cb305afSBrian Somers return expect;
137287c3786eSBrian Somers }
137396c9bb21SBrian Somers
13742cb305afSBrian Somers void
bundle_ReceiveDatalink(struct bundle * bundle,int s)13752cb305afSBrian Somers bundle_ReceiveDatalink(struct bundle *bundle, int s)
13762cb305afSBrian Somers {
13772cb305afSBrian Somers char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int) * SEND_MAXFD];
1378057f1760SBrian Somers int niov, expect, f, *fd, nfd, onfd;
1379057f1760SBrian Somers ssize_t got;
13802cb305afSBrian Somers struct iovec iov[SCATTER_SEGMENTS];
13812cb305afSBrian Somers struct cmsghdr *cmsg;
13822cb305afSBrian Somers struct msghdr msg;
13832cb305afSBrian Somers struct datalink *dl;
13842cb305afSBrian Somers pid_t pid;
13852cb305afSBrian Somers
13862cb305afSBrian Somers log_Printf(LogPHASE, "Receiving datalink\n");
13872cb305afSBrian Somers
13882cb305afSBrian Somers /*
13892cb305afSBrian Somers * Create our scatter/gather array - passing NULL gets the space
13902cb305afSBrian Somers * allocation requirement rather than actually flattening the
13912cb305afSBrian Somers * structures.
13922cb305afSBrian Somers */
13932cb305afSBrian Somers iov[0].iov_len = strlen(Version) + 1;
13942cb305afSBrian Somers iov[0].iov_base = NULL;
13952cb305afSBrian Somers niov = 1;
13962cb305afSBrian Somers if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) {
13972cb305afSBrian Somers log_Printf(LogERROR, "Cannot determine space required for link\n");
13982cb305afSBrian Somers return;
13992cb305afSBrian Somers }
14002cb305afSBrian Somers
14012cb305afSBrian Somers /* Allocate the scatter/gather array for recvmsg() */
14022cb305afSBrian Somers for (f = expect = 0; f < niov; f++) {
14032cb305afSBrian Somers if ((iov[f].iov_base = malloc(iov[f].iov_len)) == NULL) {
14042cb305afSBrian Somers log_Printf(LogERROR, "Cannot allocate space to receive link\n");
14052cb305afSBrian Somers return;
14062cb305afSBrian Somers }
1407cbee9754SBrian Somers if (f)
14082cb305afSBrian Somers expect += iov[f].iov_len;
14092cb305afSBrian Somers }
14102cb305afSBrian Somers
14112cb305afSBrian Somers /* Set up our message */
14122cb305afSBrian Somers cmsg = (struct cmsghdr *)cmsgbuf;
14132cb305afSBrian Somers cmsg->cmsg_len = sizeof cmsgbuf;
14142cb305afSBrian Somers cmsg->cmsg_level = SOL_SOCKET;
14152cb305afSBrian Somers cmsg->cmsg_type = 0;
14162cb305afSBrian Somers
141796c9bb21SBrian Somers memset(&msg, '\0', sizeof msg);
14182cb305afSBrian Somers msg.msg_name = NULL;
14192cb305afSBrian Somers msg.msg_namelen = 0;
142096c9bb21SBrian Somers msg.msg_iov = iov;
1421cbee9754SBrian Somers msg.msg_iovlen = 1; /* Only send the version at the first pass */
142296c9bb21SBrian Somers msg.msg_control = cmsgbuf;
142396c9bb21SBrian Somers msg.msg_controllen = sizeof cmsgbuf;
142496c9bb21SBrian Somers
1425209dc102SBrian Somers log_Printf(LogDEBUG, "Expecting %u scatter/gather bytes\n",
1426209dc102SBrian Somers (unsigned)iov[0].iov_len);
14272cb305afSBrian Somers
1428057f1760SBrian Somers if ((got = recvmsg(s, &msg, MSG_WAITALL)) != (ssize_t)iov[0].iov_len) {
1429cbee9754SBrian Somers if (got == -1)
143096c9bb21SBrian Somers log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno));
143196c9bb21SBrian Somers else
14321814213eSMarcel Moolenaar log_Printf(LogERROR, "Failed recvmsg: Got %zd, not %u\n",
1433209dc102SBrian Somers got, (unsigned)iov[0].iov_len);
143496c9bb21SBrian Somers while (niov--)
143596c9bb21SBrian Somers free(iov[niov].iov_base);
143696c9bb21SBrian Somers return;
143796c9bb21SBrian Somers }
143896c9bb21SBrian Somers
14392cb305afSBrian Somers if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
14402cb305afSBrian Somers log_Printf(LogERROR, "Recvmsg: no descriptors received !\n");
14412cb305afSBrian Somers while (niov--)
14422cb305afSBrian Somers free(iov[niov].iov_base);
14432cb305afSBrian Somers return;
144487c3786eSBrian Somers }
144587c3786eSBrian Somers
14462bc21ed9SDavid Malone fd = (int *)CMSG_DATA(cmsg);
14472bc21ed9SDavid Malone nfd = ((caddr_t)cmsg + cmsg->cmsg_len - (caddr_t)fd) / sizeof(int);
14482cb305afSBrian Somers
14492cb305afSBrian Somers if (nfd < 2) {
14508e7bd08eSBrian Somers log_Printf(LogERROR, "Recvmsg: %d descriptor%s received (too few) !\n",
14512cb305afSBrian Somers nfd, nfd == 1 ? "" : "s");
14522cb305afSBrian Somers while (nfd--)
14532cb305afSBrian Somers close(fd[nfd]);
1454ad5b0e8bSBrian Somers while (niov--)
1455ad5b0e8bSBrian Somers free(iov[niov].iov_base);
1456ad5b0e8bSBrian Somers return;
145754cd8e13SBrian Somers }
145854cd8e13SBrian Somers
145987c3786eSBrian Somers /*
1460cbee9754SBrian Somers * We've successfully received two or more open file descriptors
1461cbee9754SBrian Somers * through our socket, plus a version string. Make sure it's the
1462cbee9754SBrian Somers * correct version, and drop the connection if it's not.
146387c3786eSBrian Somers */
146496c9bb21SBrian Somers if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) {
146596c9bb21SBrian Somers log_Printf(LogWARN, "Cannot receive datalink, incorrect version"
146696c9bb21SBrian Somers " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len,
14677d81ddf5SBrian Somers (char *)iov[0].iov_base, Version);
14682cb305afSBrian Somers while (nfd--)
14692cb305afSBrian Somers close(fd[nfd]);
147096c9bb21SBrian Somers while (niov--)
147196c9bb21SBrian Somers free(iov[niov].iov_base);
147296c9bb21SBrian Somers return;
147396c9bb21SBrian Somers }
147496c9bb21SBrian Somers
1475cbee9754SBrian Somers /*
1476cbee9754SBrian Somers * Everything looks good. Send the other side our process id so that
1477cbee9754SBrian Somers * they can transfer lock ownership, and wait for them to send the
1478cbee9754SBrian Somers * actual link data.
1479cbee9754SBrian Somers */
1480cbee9754SBrian Somers pid = getpid();
1481cbee9754SBrian Somers if ((got = write(fd[1], &pid, sizeof pid)) != sizeof pid) {
1482cbee9754SBrian Somers if (got == -1)
1483cbee9754SBrian Somers log_Printf(LogERROR, "Failed write: %s\n", strerror(errno));
1484cbee9754SBrian Somers else
14851814213eSMarcel Moolenaar log_Printf(LogERROR, "Failed write: Got %zd, not %d\n", got,
1486cbee9754SBrian Somers (int)(sizeof pid));
1487cbee9754SBrian Somers while (nfd--)
1488cbee9754SBrian Somers close(fd[nfd]);
1489cbee9754SBrian Somers while (niov--)
1490cbee9754SBrian Somers free(iov[niov].iov_base);
1491cbee9754SBrian Somers return;
1492cbee9754SBrian Somers }
1493cbee9754SBrian Somers
1494cbee9754SBrian Somers if ((got = readv(fd[1], iov + 1, niov - 1)) != expect) {
1495cbee9754SBrian Somers if (got == -1)
1496cbee9754SBrian Somers log_Printf(LogERROR, "Failed write: %s\n", strerror(errno));
1497cbee9754SBrian Somers else
14981814213eSMarcel Moolenaar log_Printf(LogERROR, "Failed write: Got %zd, not %d\n", got, expect);
1499cbee9754SBrian Somers while (nfd--)
1500cbee9754SBrian Somers close(fd[nfd]);
1501cbee9754SBrian Somers while (niov--)
1502cbee9754SBrian Somers free(iov[niov].iov_base);
1503cbee9754SBrian Somers return;
1504cbee9754SBrian Somers }
1505cbee9754SBrian Somers close(fd[1]);
1506cbee9754SBrian Somers
15072cb305afSBrian Somers onfd = nfd; /* We've got this many in our array */
15088e7bd08eSBrian Somers nfd -= 2; /* Don't include p->fd and our reply descriptor */
15092cb305afSBrian Somers niov = 1; /* Skip the version id */
151087c3786eSBrian Somers dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, fd[0],
15112cb305afSBrian Somers fd + 2, &nfd);
1512b7c5748eSBrian Somers if (dl) {
15132cb305afSBrian Somers
151487c3786eSBrian Somers if (nfd) {
151587c3786eSBrian Somers log_Printf(LogERROR, "bundle_ReceiveDatalink: Failed to handle %d "
15162cb305afSBrian Somers "auxiliary file descriptors (%d remain)\n", onfd, nfd);
151787c3786eSBrian Somers datalink_Destroy(dl);
151887c3786eSBrian Somers while (nfd--)
151987c3786eSBrian Somers close(fd[onfd--]);
15202cb305afSBrian Somers close(fd[0]);
152187c3786eSBrian Somers } else {
152204eaa58cSBrian Somers bundle_DatalinkLinkin(bundle, dl);
1523b7c5748eSBrian Somers datalink_AuthOk(dl);
1524ab2de065SBrian Somers bundle_CalculateBandwidth(dl->bundle);
152587c3786eSBrian Somers }
152687c3786eSBrian Somers } else {
152787c3786eSBrian Somers while (nfd--)
152887c3786eSBrian Somers close(fd[onfd--]);
152987c3786eSBrian Somers close(fd[0]);
15302cb305afSBrian Somers close(fd[1]);
153187c3786eSBrian Somers }
153296c9bb21SBrian Somers
153396c9bb21SBrian Somers free(iov[0].iov_base);
15341fa665f5SBrian Somers }
15351fa665f5SBrian Somers
15361fa665f5SBrian Somers void
bundle_SendDatalink(struct datalink * dl,int s,struct sockaddr_un * sun)153796c9bb21SBrian Somers bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun)
15381fa665f5SBrian Somers {
15392bc21ed9SDavid Malone char cmsgbuf[CMSG_SPACE(sizeof(int) * SEND_MAXFD)];
15402cb305afSBrian Somers const char *constlock;
15412cb305afSBrian Somers char *lock;
154287c3786eSBrian Somers struct cmsghdr *cmsg;
154396c9bb21SBrian Somers struct msghdr msg;
154496c9bb21SBrian Somers struct iovec iov[SCATTER_SEGMENTS];
1545057f1760SBrian Somers int niov, f, expect, newsid, fd[SEND_MAXFD], nfd, reply[2];
1546057f1760SBrian Somers ssize_t got;
154785fd273aSBrian Somers pid_t newpid;
15486f384573SBrian Somers
1549dd7e2610SBrian Somers log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name);
15506f384573SBrian Somers
15512cb305afSBrian Somers /* Record the base device name for a lock transfer later */
15522cb305afSBrian Somers constlock = physical_LockedDevice(dl->physical);
15532cb305afSBrian Somers if (constlock) {
15542cb305afSBrian Somers lock = alloca(strlen(constlock) + 1);
15552cb305afSBrian Somers strcpy(lock, constlock);
15562cb305afSBrian Somers } else
15572cb305afSBrian Somers lock = NULL;
15582cb305afSBrian Somers
155904eaa58cSBrian Somers bundle_LinkClosed(dl->bundle, dl);
15600f2f3eb3SBrian Somers bundle_DatalinkLinkout(dl->bundle, dl);
1561ea722969SBrian Somers
156296c9bb21SBrian Somers /* Build our scatter/gather array */
156396c9bb21SBrian Somers iov[0].iov_len = strlen(Version) + 1;
156496c9bb21SBrian Somers iov[0].iov_base = strdup(Version);
156596c9bb21SBrian Somers niov = 1;
156687c3786eSBrian Somers nfd = 0;
15676f384573SBrian Somers
15682cb305afSBrian Somers fd[0] = datalink2iov(dl, iov, &niov, SCATTER_SEGMENTS, fd + 2, &nfd);
15696f384573SBrian Somers
15702cb305afSBrian Somers if (fd[0] != -1 && socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, reply) != -1) {
15712cb305afSBrian Somers /*
15722cb305afSBrian Somers * fd[1] is used to get the peer process id back, then to confirm that
15732cb305afSBrian Somers * we've transferred any device locks to that process id.
15742cb305afSBrian Somers */
15752cb305afSBrian Somers fd[1] = reply[1];
157687c3786eSBrian Somers
15772cb305afSBrian Somers nfd += 2; /* Include fd[0] and fd[1] */
157896c9bb21SBrian Somers memset(&msg, '\0', sizeof msg);
157954cd8e13SBrian Somers
15802cb305afSBrian Somers msg.msg_name = NULL;
15812cb305afSBrian Somers msg.msg_namelen = 0;
1582cbee9754SBrian Somers /*
1583cbee9754SBrian Somers * Only send the version to start... We used to send the whole lot, but
1584cbee9754SBrian Somers * this caused problems with our RECVBUF size as a single link is about
1585cbee9754SBrian Somers * 22k ! This way, we should bump into no limits.
1586cbee9754SBrian Somers */
1587cbee9754SBrian Somers msg.msg_iovlen = 1;
158896c9bb21SBrian Somers msg.msg_iov = iov;
15892cb305afSBrian Somers msg.msg_control = cmsgbuf;
15902bc21ed9SDavid Malone msg.msg_controllen = CMSG_SPACE(sizeof(int) * nfd);
15912cb305afSBrian Somers msg.msg_flags = 0;
159254cd8e13SBrian Somers
15932cb305afSBrian Somers cmsg = (struct cmsghdr *)cmsgbuf;
15942cb305afSBrian Somers cmsg->cmsg_len = msg.msg_controllen;
159554cd8e13SBrian Somers cmsg->cmsg_level = SOL_SOCKET;
159654cd8e13SBrian Somers cmsg->cmsg_type = SCM_RIGHTS;
159787c3786eSBrian Somers
15982cb305afSBrian Somers for (f = 0; f < nfd; f++)
15992bc21ed9SDavid Malone *((int *)CMSG_DATA(cmsg) + f) = fd[f];
160047723d29SBrian Somers
1601cbee9754SBrian Somers for (f = 1, expect = 0; f < niov; f++)
160296c9bb21SBrian Somers expect += iov[f].iov_len;
160347723d29SBrian Somers
1604cbee9754SBrian Somers if (setsockopt(reply[0], SOL_SOCKET, SO_SNDBUF, &expect, sizeof(int)) == -1)
1605cbee9754SBrian Somers log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect,
1606cbee9754SBrian Somers strerror(errno));
1607cbee9754SBrian Somers if (setsockopt(reply[1], SOL_SOCKET, SO_RCVBUF, &expect, sizeof(int)) == -1)
1608cbee9754SBrian Somers log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect,
1609cbee9754SBrian Somers strerror(errno));
1610cbee9754SBrian Somers
1611209dc102SBrian Somers log_Printf(LogDEBUG, "Sending %d descriptor%s and %u bytes in scatter"
1612209dc102SBrian Somers "/gather array\n", nfd, nfd == 1 ? "" : "s",
1613209dc102SBrian Somers (unsigned)iov[0].iov_len);
161447723d29SBrian Somers
16152cb305afSBrian Somers if ((got = sendmsg(s, &msg, 0)) == -1)
16162cb305afSBrian Somers log_Printf(LogERROR, "Failed sendmsg: %s: %s\n",
16172cb305afSBrian Somers sun->sun_path, strerror(errno));
1618057f1760SBrian Somers else if (got != (ssize_t)iov[0].iov_len)
16191814213eSMarcel Moolenaar log_Printf(LogERROR, "%s: Failed initial sendmsg: Only sent %zd of %u\n",
1620209dc102SBrian Somers sun->sun_path, got, (unsigned)iov[0].iov_len);
16212cb305afSBrian Somers else {
16228e7bd08eSBrian Somers /* We must get the ACK before closing the descriptor ! */
16232cb305afSBrian Somers int res;
16242cb305afSBrian Somers
1625cbee9754SBrian Somers if ((got = read(reply[0], &newpid, sizeof newpid)) == sizeof newpid) {
162642df3c25SBrian Somers log_Printf(LogDEBUG, "Received confirmation from pid %ld\n",
162742df3c25SBrian Somers (long)newpid);
16282cb305afSBrian Somers if (lock && (res = ID0uu_lock_txfr(lock, newpid)) != UU_LOCK_OK)
1629b42135deSBrian Somers log_Printf(LogERROR, "uu_lock_txfr: %s\n", uu_lockerr(res));
16302cb305afSBrian Somers
1631cbee9754SBrian Somers log_Printf(LogDEBUG, "Transmitting link (%d bytes)\n", expect);
1632cbee9754SBrian Somers if ((got = writev(reply[0], iov + 1, niov - 1)) != expect) {
1633cbee9754SBrian Somers if (got == -1)
1634cbee9754SBrian Somers log_Printf(LogERROR, "%s: Failed writev: %s\n",
1635cbee9754SBrian Somers sun->sun_path, strerror(errno));
1636cbee9754SBrian Somers else
16371814213eSMarcel Moolenaar log_Printf(LogERROR, "%s: Failed writev: Wrote %zd of %d\n",
1638cbee9754SBrian Somers sun->sun_path, got, expect);
1639cbee9754SBrian Somers }
1640cbee9754SBrian Somers } else if (got == -1)
1641cbee9754SBrian Somers log_Printf(LogERROR, "%s: Failed socketpair read: %s\n",
1642cbee9754SBrian Somers sun->sun_path, strerror(errno));
1643cbee9754SBrian Somers else
16441814213eSMarcel Moolenaar log_Printf(LogERROR, "%s: Failed socketpair read: Got %zd of %d\n",
1645cbee9754SBrian Somers sun->sun_path, got, (int)(sizeof newpid));
16462cb305afSBrian Somers }
16472cb305afSBrian Somers
16482cb305afSBrian Somers close(reply[0]);
16492cb305afSBrian Somers close(reply[1]);
165054cd8e13SBrian Somers
1651ac685e31SBrian Somers newsid = Enabled(dl->bundle, OPT_KEEPSESSION) ||
165287c3786eSBrian Somers tcgetpgrp(fd[0]) == getpgrp();
165387c3786eSBrian Somers while (nfd)
165487c3786eSBrian Somers close(fd[--nfd]);
16551384bd27SBrian Somers if (newsid)
16562cb305afSBrian Somers bundle_setsid(dl->bundle, got != -1);
165747723d29SBrian Somers }
165885fd273aSBrian Somers close(s);
165996c9bb21SBrian Somers
166096c9bb21SBrian Somers while (niov--)
166196c9bb21SBrian Somers free(iov[niov].iov_base);
16621fa665f5SBrian Somers }
1663dd0645c5SBrian Somers
1664dd0645c5SBrian Somers int
bundle_RenameDatalink(struct bundle * bundle,struct datalink * ndl,const char * name)166558d55334SBrian Somers bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl,
166658d55334SBrian Somers const char *name)
166758d55334SBrian Somers {
166858d55334SBrian Somers struct datalink *dl;
166958d55334SBrian Somers
167058d55334SBrian Somers if (!strcasecmp(ndl->name, name))
167158d55334SBrian Somers return 1;
167258d55334SBrian Somers
167358d55334SBrian Somers for (dl = bundle->links; dl; dl = dl->next)
167458d55334SBrian Somers if (!strcasecmp(dl->name, name))
167558d55334SBrian Somers return 0;
167658d55334SBrian Somers
167758d55334SBrian Somers datalink_Rename(ndl, name);
167858d55334SBrian Somers return 1;
167958d55334SBrian Somers }
168058d55334SBrian Somers
168158d55334SBrian Somers int
bundle_SetMode(struct bundle * bundle,struct datalink * dl,int mode)1682dd0645c5SBrian Somers bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode)
1683dd0645c5SBrian Somers {
1684dd0645c5SBrian Somers int omode;
1685dd0645c5SBrian Somers
1686dd0645c5SBrian Somers omode = dl->physical->type;
1687dd0645c5SBrian Somers if (omode == mode)
1688dd0645c5SBrian Somers return 1;
1689dd0645c5SBrian Somers
1690ff0f9439SBrian Somers if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO))
1691ff0f9439SBrian Somers /* First auto link */
1692dd0645c5SBrian Somers if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) {
1693ff0f9439SBrian Somers log_Printf(LogWARN, "You must `set ifaddr' or `open' before"
1694ff0f9439SBrian Somers " changing mode to %s\n", mode2Nam(mode));
1695dd0645c5SBrian Somers return 0;
1696dd0645c5SBrian Somers }
1697dd0645c5SBrian Somers
1698dd0645c5SBrian Somers if (!datalink_SetMode(dl, mode))
1699dd0645c5SBrian Somers return 0;
1700dd0645c5SBrian Somers
1701ff0f9439SBrian Somers if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO) &&
1702ff0f9439SBrian Somers bundle->phase != PHASE_NETWORK)
1703ff0f9439SBrian Somers /* First auto link, we need an interface */
1704dd0645c5SBrian Somers ipcp_InterfaceUp(&bundle->ncp.ipcp);
1705dd0645c5SBrian Somers
1706ab2de065SBrian Somers /* Regenerate phys_type and adjust idle timer */
170704eaa58cSBrian Somers bundle_LinksRemoved(bundle);
1708dd0645c5SBrian Somers
1709dd0645c5SBrian Somers return 1;
1710dd0645c5SBrian Somers }
17111384bd27SBrian Somers
17121384bd27SBrian Somers void
bundle_setsid(struct bundle * bundle,int holdsession)17131384bd27SBrian Somers bundle_setsid(struct bundle *bundle, int holdsession)
17141384bd27SBrian Somers {
17151384bd27SBrian Somers /*
17161384bd27SBrian Somers * Lose the current session. This means getting rid of our pid
17171384bd27SBrian Somers * too so that the tty device will really go away, and any getty
17181384bd27SBrian Somers * etc will be allowed to restart.
17191384bd27SBrian Somers */
17201384bd27SBrian Somers pid_t pid, orig;
17211384bd27SBrian Somers int fds[2];
17221384bd27SBrian Somers char done;
17231384bd27SBrian Somers struct datalink *dl;
17241384bd27SBrian Somers
1725e62ce959SBrian Somers if (!holdsession && bundle_IsDead(bundle)) {
1726e62ce959SBrian Somers /*
1727e62ce959SBrian Somers * No need to lose our session after all... we're going away anyway
1728e62ce959SBrian Somers *
1729e62ce959SBrian Somers * We should really stop the timer and pause if holdsession is set and
1730e62ce959SBrian Somers * the bundle's dead, but that leaves other resources lying about :-(
1731e62ce959SBrian Somers */
1732e62ce959SBrian Somers return;
1733e62ce959SBrian Somers }
1734e62ce959SBrian Somers
17351384bd27SBrian Somers orig = getpid();
17361384bd27SBrian Somers if (pipe(fds) == -1) {
17371384bd27SBrian Somers log_Printf(LogERROR, "pipe: %s\n", strerror(errno));
17381384bd27SBrian Somers return;
17391384bd27SBrian Somers }
17401384bd27SBrian Somers switch ((pid = fork())) {
17411384bd27SBrian Somers case -1:
17421384bd27SBrian Somers log_Printf(LogERROR, "fork: %s\n", strerror(errno));
17431384bd27SBrian Somers close(fds[0]);
17441384bd27SBrian Somers close(fds[1]);
17451384bd27SBrian Somers return;
17461384bd27SBrian Somers case 0:
17471384bd27SBrian Somers close(fds[1]);
17484be0e57dSBrian Somers read(fds[0], &done, 1); /* uu_locks are mine ! */
17494be0e57dSBrian Somers close(fds[0]);
17501384bd27SBrian Somers if (pipe(fds) == -1) {
17511384bd27SBrian Somers log_Printf(LogERROR, "pipe(2): %s\n", strerror(errno));
17521384bd27SBrian Somers return;
17531384bd27SBrian Somers }
17541384bd27SBrian Somers switch ((pid = fork())) {
17551384bd27SBrian Somers case -1:
1756a33b2ef7SBrian Somers log_Printf(LogERROR, "fork(2): %s\n", strerror(errno));
17571384bd27SBrian Somers close(fds[0]);
17581384bd27SBrian Somers close(fds[1]);
17591384bd27SBrian Somers return;
17601384bd27SBrian Somers case 0:
17611384bd27SBrian Somers close(fds[1]);
17624be0e57dSBrian Somers bundle_LockTun(bundle); /* update pid */
17634be0e57dSBrian Somers read(fds[0], &done, 1); /* uu_locks are mine ! */
17644be0e57dSBrian Somers close(fds[0]);
17651384bd27SBrian Somers setsid();
176606b47306SBrian Somers bundle_ChangedPID(bundle);
176742df3c25SBrian Somers log_Printf(LogDEBUG, "%ld -> %ld: %s session control\n",
176842df3c25SBrian Somers (long)orig, (long)getpid(),
17691384bd27SBrian Somers holdsession ? "Passed" : "Dropped");
17707e778f13SBrian Somers timer_InitService(0); /* Start the Timer Service */
17711384bd27SBrian Somers break;
17721384bd27SBrian Somers default:
17734be0e57dSBrian Somers close(fds[0]);
17745d9e6103SBrian Somers /* Give away all our physical locks (to the final process) */
17751384bd27SBrian Somers for (dl = bundle->links; dl; dl = dl->next)
17761384bd27SBrian Somers if (dl->state != DATALINK_CLOSED)
17775d9e6103SBrian Somers physical_ChangedPid(dl->physical, pid);
17784be0e57dSBrian Somers write(fds[1], "!", 1); /* done */
17794be0e57dSBrian Somers close(fds[1]);
17802cb305afSBrian Somers _exit(0);
17811384bd27SBrian Somers break;
17821384bd27SBrian Somers }
17831384bd27SBrian Somers break;
17841384bd27SBrian Somers default:
17854be0e57dSBrian Somers close(fds[0]);
17865d9e6103SBrian Somers /* Give away all our physical locks (to the intermediate process) */
17871384bd27SBrian Somers for (dl = bundle->links; dl; dl = dl->next)
17881384bd27SBrian Somers if (dl->state != DATALINK_CLOSED)
17895d9e6103SBrian Somers physical_ChangedPid(dl->physical, pid);
17904be0e57dSBrian Somers write(fds[1], "!", 1); /* done */
17914be0e57dSBrian Somers close(fds[1]);
17921384bd27SBrian Somers if (holdsession) {
17931384bd27SBrian Somers int fd, status;
17941384bd27SBrian Somers
17951384bd27SBrian Somers timer_TermService();
17961384bd27SBrian Somers signal(SIGPIPE, SIG_DFL);
17971384bd27SBrian Somers signal(SIGALRM, SIG_DFL);
17981384bd27SBrian Somers signal(SIGHUP, SIG_DFL);
17991384bd27SBrian Somers signal(SIGTERM, SIG_DFL);
18001384bd27SBrian Somers signal(SIGINT, SIG_DFL);
18011384bd27SBrian Somers signal(SIGQUIT, SIG_DFL);
18021384bd27SBrian Somers for (fd = getdtablesize(); fd >= 0; fd--)
18031384bd27SBrian Somers close(fd);
18041384bd27SBrian Somers /*
18051384bd27SBrian Somers * Reap the intermediate process. As we're not exiting but the
18061384bd27SBrian Somers * intermediate is, we don't want it to become defunct.
18071384bd27SBrian Somers */
18081384bd27SBrian Somers waitpid(pid, &status, 0);
18098e7b8599SBrian Somers /* Tweak our process arguments.... */
1810ebe96675SBrian Somers SetTitle("session owner");
181168602c3eSBrian Somers #ifndef NOSUID
1812a19a5c02SBrian Somers setuid(ID0realuid());
181368602c3eSBrian Somers #endif
18141384bd27SBrian Somers /*
18151384bd27SBrian Somers * Hang around for a HUP. This should happen as soon as the
18168e7bd08eSBrian Somers * ppp that we passed our ctty descriptor to closes it.
18178e7bd08eSBrian Somers * NOTE: If this process dies, the passed descriptor becomes
18181384bd27SBrian Somers * invalid and will give a select() error by setting one
18191384bd27SBrian Somers * of the error fds, aborting the other ppp. We don't
18201384bd27SBrian Somers * want that to happen !
18211384bd27SBrian Somers */
18221384bd27SBrian Somers pause();
18231384bd27SBrian Somers }
18242cb305afSBrian Somers _exit(0);
18251384bd27SBrian Somers break;
18261384bd27SBrian Somers }
18271384bd27SBrian Somers }
18289b5f8ffdSBrian Somers
1829057f1760SBrian Somers unsigned
bundle_HighestState(struct bundle * bundle)18309b5f8ffdSBrian Somers bundle_HighestState(struct bundle *bundle)
18319b5f8ffdSBrian Somers {
18329b5f8ffdSBrian Somers struct datalink *dl;
1833057f1760SBrian Somers unsigned result = DATALINK_CLOSED;
18349b5f8ffdSBrian Somers
18359b5f8ffdSBrian Somers for (dl = bundle->links; dl; dl = dl->next)
18369b5f8ffdSBrian Somers if (result < dl->state)
18379b5f8ffdSBrian Somers result = dl->state;
18389b5f8ffdSBrian Somers
18399b5f8ffdSBrian Somers return result;
18409b5f8ffdSBrian Somers }
1841991c2a7bSBrian Somers
1842991c2a7bSBrian Somers int
bundle_Exception(struct bundle * bundle,int fd)1843991c2a7bSBrian Somers bundle_Exception(struct bundle *bundle, int fd)
1844991c2a7bSBrian Somers {
1845991c2a7bSBrian Somers struct datalink *dl;
1846991c2a7bSBrian Somers
1847991c2a7bSBrian Somers for (dl = bundle->links; dl; dl = dl->next)
1848991c2a7bSBrian Somers if (dl->physical->fd == fd) {
1849991c2a7bSBrian Somers datalink_Down(dl, CLOSE_NORMAL);
1850991c2a7bSBrian Somers return 1;
1851991c2a7bSBrian Somers }
1852991c2a7bSBrian Somers
1853991c2a7bSBrian Somers return 0;
1854991c2a7bSBrian Somers }
18551d1fc017SBrian Somers
18561d1fc017SBrian Somers void
bundle_AdjustFilters(struct bundle * bundle,struct ncpaddr * local,struct ncpaddr * remote)185730949fd4SBrian Somers bundle_AdjustFilters(struct bundle *bundle, struct ncpaddr *local,
185830949fd4SBrian Somers struct ncpaddr *remote)
18591d1fc017SBrian Somers {
186030949fd4SBrian Somers filter_AdjustAddr(&bundle->filter.in, local, remote, NULL);
186130949fd4SBrian Somers filter_AdjustAddr(&bundle->filter.out, local, remote, NULL);
186230949fd4SBrian Somers filter_AdjustAddr(&bundle->filter.dial, local, remote, NULL);
186330949fd4SBrian Somers filter_AdjustAddr(&bundle->filter.alive, local, remote, NULL);
1864d568d6c4SBrian Somers }
1865d568d6c4SBrian Somers
1866d568d6c4SBrian Somers void
bundle_AdjustDNS(struct bundle * bundle)186730949fd4SBrian Somers bundle_AdjustDNS(struct bundle *bundle)
1868d568d6c4SBrian Somers {
186930949fd4SBrian Somers struct in_addr *dns = bundle->ncp.ipcp.ns.dns;
187030949fd4SBrian Somers
1871d568d6c4SBrian Somers filter_AdjustAddr(&bundle->filter.in, NULL, NULL, dns);
1872d568d6c4SBrian Somers filter_AdjustAddr(&bundle->filter.out, NULL, NULL, dns);
1873d568d6c4SBrian Somers filter_AdjustAddr(&bundle->filter.dial, NULL, NULL, dns);
1874d568d6c4SBrian Somers filter_AdjustAddr(&bundle->filter.alive, NULL, NULL, dns);
18751d1fc017SBrian Somers }
1876ab2de065SBrian Somers
1877ab2de065SBrian Somers void
bundle_CalculateBandwidth(struct bundle * bundle)1878ab2de065SBrian Somers bundle_CalculateBandwidth(struct bundle *bundle)
1879ab2de065SBrian Somers {
1880ab2de065SBrian Somers struct datalink *dl;
18816301d506SBrian Somers int sp, overhead, maxoverhead;
1882ab2de065SBrian Somers
1883ab2de065SBrian Somers bundle->bandwidth = 0;
1884c8b9fb53SBrian Somers bundle->iface->mtu = 0;
18856301d506SBrian Somers maxoverhead = 0;
18866301d506SBrian Somers
18876301d506SBrian Somers for (dl = bundle->links; dl; dl = dl->next) {
18886301d506SBrian Somers overhead = ccp_MTUOverhead(&dl->physical->link.ccp);
18896301d506SBrian Somers if (maxoverhead < overhead)
18906301d506SBrian Somers maxoverhead = overhead;
1891ab2de065SBrian Somers if (dl->state == DATALINK_OPEN) {
1892ab2de065SBrian Somers if ((sp = dl->mp.bandwidth) == 0 &&
1893ab2de065SBrian Somers (sp = physical_GetSpeed(dl->physical)) == 0)
1894ab2de065SBrian Somers log_Printf(LogDEBUG, "%s: %s: Cannot determine bandwidth\n",
1895ab2de065SBrian Somers dl->name, dl->physical->name.full);
1896ab2de065SBrian Somers else
1897ab2de065SBrian Somers bundle->bandwidth += sp;
1898ab2de065SBrian Somers if (!bundle->ncp.mp.active) {
1899c8b9fb53SBrian Somers bundle->iface->mtu = dl->physical->link.lcp.his_mru;
1900ab2de065SBrian Somers break;
1901ab2de065SBrian Somers }
1902ab2de065SBrian Somers }
19036301d506SBrian Somers }
1904ab2de065SBrian Somers
1905ab2de065SBrian Somers if (bundle->bandwidth == 0)
1906ab2de065SBrian Somers bundle->bandwidth = 115200; /* Shrug */
1907ab2de065SBrian Somers
19086301d506SBrian Somers if (bundle->ncp.mp.active) {
1909c8b9fb53SBrian Somers bundle->iface->mtu = bundle->ncp.mp.peer_mrru;
19106301d506SBrian Somers overhead = ccp_MTUOverhead(&bundle->ncp.mp.link.ccp);
19116301d506SBrian Somers if (maxoverhead < overhead)
19126301d506SBrian Somers maxoverhead = overhead;
19136301d506SBrian Somers } else if (!bundle->iface->mtu)
1914c8b9fb53SBrian Somers bundle->iface->mtu = DEF_MRU;
1915ab2de065SBrian Somers
1916ab2de065SBrian Somers #ifndef NORADIUS
191794d7be52SBrian Somers if (bundle->radius.valid && bundle->radius.mtu &&
1918c8b9fb53SBrian Somers bundle->radius.mtu < bundle->iface->mtu) {
1919ab2de065SBrian Somers log_Printf(LogLCP, "Reducing MTU to radius value %lu\n",
1920ab2de065SBrian Somers bundle->radius.mtu);
1921c8b9fb53SBrian Somers bundle->iface->mtu = bundle->radius.mtu;
1922ab2de065SBrian Somers }
1923ab2de065SBrian Somers #endif
1924ab2de065SBrian Somers
19256301d506SBrian Somers if (maxoverhead) {
1926057f1760SBrian Somers log_Printf(LogLCP, "Reducing MTU from %lu to %lu (CCP requirement)\n",
19276301d506SBrian Somers bundle->iface->mtu, bundle->iface->mtu - maxoverhead);
19286301d506SBrian Somers bundle->iface->mtu -= maxoverhead;
19296301d506SBrian Somers }
19306301d506SBrian Somers
193194d7be52SBrian Somers tun_configure(bundle);
193203a2501aSBrian Somers
193303a2501aSBrian Somers route_UpdateMTU(bundle);
1934ab2de065SBrian Somers }
1935ab2de065SBrian Somers
1936ab2de065SBrian Somers void
bundle_AutoAdjust(struct bundle * bundle,int percent,int what)1937ab2de065SBrian Somers bundle_AutoAdjust(struct bundle *bundle, int percent, int what)
1938ab2de065SBrian Somers {
1939ab2de065SBrian Somers struct datalink *dl, *choice, *otherlinkup;
1940ab2de065SBrian Somers
1941ab2de065SBrian Somers choice = otherlinkup = NULL;
1942ab2de065SBrian Somers for (dl = bundle->links; dl; dl = dl->next)
1943ab2de065SBrian Somers if (dl->physical->type == PHYS_AUTO) {
1944ab2de065SBrian Somers if (dl->state == DATALINK_OPEN) {
1945ab2de065SBrian Somers if (what == AUTO_DOWN) {
1946ab2de065SBrian Somers if (choice)
1947ab2de065SBrian Somers otherlinkup = choice;
1948ab2de065SBrian Somers choice = dl;
1949ab2de065SBrian Somers }
1950ab2de065SBrian Somers } else if (dl->state == DATALINK_CLOSED) {
1951ab2de065SBrian Somers if (what == AUTO_UP) {
1952ab2de065SBrian Somers choice = dl;
1953ab2de065SBrian Somers break;
1954ab2de065SBrian Somers }
1955ab2de065SBrian Somers } else {
1956ab2de065SBrian Somers /* An auto link in an intermediate state - forget it for the moment */
1957ab2de065SBrian Somers choice = NULL;
1958ab2de065SBrian Somers break;
1959ab2de065SBrian Somers }
1960ab2de065SBrian Somers } else if (dl->state == DATALINK_OPEN && what == AUTO_DOWN)
1961ab2de065SBrian Somers otherlinkup = dl;
1962ab2de065SBrian Somers
1963ab2de065SBrian Somers if (choice) {
1964ab2de065SBrian Somers if (what == AUTO_UP) {
1965ab2de065SBrian Somers log_Printf(LogPHASE, "%d%% saturation -> Opening link ``%s''\n",
1966ab2de065SBrian Somers percent, choice->name);
1967ab2de065SBrian Somers datalink_Up(choice, 1, 1);
1968a339e644SBrian Somers mp_CheckAutoloadTimer(&bundle->ncp.mp);
1969ab2de065SBrian Somers } else if (otherlinkup) { /* Only bring the second-last link down */
1970ab2de065SBrian Somers log_Printf(LogPHASE, "%d%% saturation -> Closing link ``%s''\n",
1971ab2de065SBrian Somers percent, choice->name);
1972d2f5232dSBrian Somers datalink_Close(choice, CLOSE_STAYDOWN);
1973a339e644SBrian Somers mp_CheckAutoloadTimer(&bundle->ncp.mp);
1974ab2de065SBrian Somers }
1975ab2de065SBrian Somers }
1976ab2de065SBrian Somers }
1977ab2de065SBrian Somers
1978ab2de065SBrian Somers int
bundle_WantAutoloadTimer(struct bundle * bundle)1979ab2de065SBrian Somers bundle_WantAutoloadTimer(struct bundle *bundle)
1980ab2de065SBrian Somers {
1981ab2de065SBrian Somers struct datalink *dl;
1982ab2de065SBrian Somers int autolink, opened;
1983ab2de065SBrian Somers
1984ab2de065SBrian Somers if (bundle->phase == PHASE_NETWORK) {
1985ab2de065SBrian Somers for (autolink = opened = 0, dl = bundle->links; dl; dl = dl->next)
1986ab2de065SBrian Somers if (dl->physical->type == PHYS_AUTO) {
1987ab2de065SBrian Somers if (++autolink == 2 || (autolink == 1 && opened))
1988ab2de065SBrian Somers /* Two auto links or one auto and one open in NETWORK phase */
1989ab2de065SBrian Somers return 1;
1990ab2de065SBrian Somers } else if (dl->state == DATALINK_OPEN) {
1991ab2de065SBrian Somers opened++;
1992ab2de065SBrian Somers if (autolink)
1993ab2de065SBrian Somers /* One auto and one open link in NETWORK phase */
1994ab2de065SBrian Somers return 1;
1995ab2de065SBrian Somers }
1996ab2de065SBrian Somers }
1997ab2de065SBrian Somers
1998ab2de065SBrian Somers return 0;
1999ab2de065SBrian Somers }
200006b47306SBrian Somers
200106b47306SBrian Somers void
bundle_ChangedPID(struct bundle * bundle)200206b47306SBrian Somers bundle_ChangedPID(struct bundle *bundle)
200306b47306SBrian Somers {
200406b47306SBrian Somers #ifdef TUNSIFPID
200506b47306SBrian Somers ioctl(bundle->dev.fd, TUNSIFPID, 0);
200606b47306SBrian Somers #endif
200706b47306SBrian Somers }
200846df5aa7SBrian Somers
200946df5aa7SBrian Somers int
bundle_Uptime(struct bundle * bundle)201046df5aa7SBrian Somers bundle_Uptime(struct bundle *bundle)
201146df5aa7SBrian Somers {
201246df5aa7SBrian Somers if (bundle->upat)
201346df5aa7SBrian Somers return time(NULL) - bundle->upat;
201446df5aa7SBrian Somers
201546df5aa7SBrian Somers return 0;
201646df5aa7SBrian Somers }
2017