2 * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
33 #include <net/if_tun.h> /* For TUNS* ioctls */
34 #include <net/route.h>
35 #include <netinet/in_systm.h>
36 #include <netinet/ip.h>
67 #include "throughput.h"
68 #include "slcompress.h"
73 #include "descriptor.h"
102 #define SCATTER_SEGMENTS 7 /* version, datalink, name, physical,
103 throughput, throughput, device */
105 #define SEND_MAXFD 3 /* Max file descriptors passed through
106 the local domain socket */
108 static int bundle_RemainingIdleTime(struct bundle
*);
110 static const char * const PhaseNames
[] = {
111 "Dead", "Establish", "Authenticate", "Network", "Terminate"
115 bundle_PhaseName(struct bundle
*bundle
)
117 return bundle
->phase
<= PHASE_TERMINATE
?
118 PhaseNames
[bundle
->phase
] : "unknown";
122 bundle_NewPhase(struct bundle
*bundle
, u_int
new)
124 if (new == bundle
->phase
)
127 if (new <= PHASE_TERMINATE
)
128 log_Printf(LogPHASE
, "bundle: %s\n", PhaseNames
[new]);
134 MPPE_MasterKeyValid
= 0;
136 log_DisplayPrompts();
139 case PHASE_ESTABLISH
:
143 case PHASE_AUTHENTICATE
:
145 log_DisplayPrompts();
149 if (ncp_fsmStart(&bundle
->ncp
, bundle
)) {
151 log_DisplayPrompts();
153 log_Printf(LogPHASE
, "bundle: All NCPs are disabled\n");
154 bundle_Close(bundle
, NULL
, CLOSE_STAYDOWN
);
158 case PHASE_TERMINATE
:
160 mp_Down(&bundle
->ncp
.mp
);
161 log_DisplayPrompts();
167 bundle_LayerStart(void *v __unused
, struct fsm
*fp __unused
)
169 /* The given FSM is about to start up ! */
174 bundle_Notify(struct bundle
*bundle
, char c
)
176 if (bundle
->notify
.fd
!= -1) {
179 ret
= write(bundle
->notify
.fd
, &c
, 1);
180 if (c
!= EX_REDIAL
&& c
!= EX_RECONNECT
) {
182 log_Printf(LogCHAT
, "Parent notified of %s\n",
183 c
== EX_NORMAL
? "success" : "failure");
185 log_Printf(LogERROR
, "Failed to notify parent of success\n");
186 close(bundle
->notify
.fd
);
187 bundle
->notify
.fd
= -1;
189 log_Printf(LogCHAT
, "Parent notified of %s\n", ex_desc(c
));
191 log_Printf(LogERROR
, "Failed to notify parent of %s\n", ex_desc(c
));
196 bundle_ClearQueues(void *v
)
198 struct bundle
*bundle
= (struct bundle
*)v
;
201 log_Printf(LogPHASE
, "Clearing choked output queue\n");
202 timer_Stop(&bundle
->choked
.timer
);
207 * We've had a full queue for PACKET_DEL_SECS seconds without being
208 * able to get rid of any of the packets. We've probably given up
209 * on the redials at this point, and the queued data has almost
210 * definitely been timed out by the layer above. As this is preventing
211 * us from reading the TUN_NAME device (we don't want to buffer stuff
212 * indefinitely), we may as well nuke this data and start with a clean
215 * Unfortunately, this has the side effect of shafting any compression
216 * dictionaries in use (causing the relevant RESET_REQ/RESET_ACK).
219 ncp_DeleteQueues(&bundle
->ncp
);
220 for (dl
= bundle
->links
; dl
; dl
= dl
->next
)
221 physical_DeleteQueue(dl
->physical
);
225 bundle_LinkAdded(struct bundle
*bundle
, struct datalink
*dl
)
227 bundle
->phys_type
.all
|= dl
->physical
->type
;
228 if (dl
->state
== DATALINK_OPEN
)
229 bundle
->phys_type
.open
|= dl
->physical
->type
;
232 if ((bundle
->phys_type
.open
& (PHYS_DEDICATED
|PHYS_DDIAL
))
233 != bundle
->phys_type
.open
&& bundle
->session
.timer
.state
== TIMER_STOPPED
)
234 if (bundle
->radius
.sessiontime
)
235 bundle_StartSessionTimer(bundle
, 0);
238 if ((bundle
->phys_type
.open
& (PHYS_DEDICATED
|PHYS_DDIAL
))
239 != bundle
->phys_type
.open
&& bundle
->idle
.timer
.state
== TIMER_STOPPED
)
240 /* We may need to start our idle timer */
241 bundle_StartIdleTimer(bundle
, 0);
245 bundle_LinksRemoved(struct bundle
*bundle
)
249 bundle
->phys_type
.all
= bundle
->phys_type
.open
= 0;
250 for (dl
= bundle
->links
; dl
; dl
= dl
->next
)
251 bundle_LinkAdded(bundle
, dl
);
253 bundle_CalculateBandwidth(bundle
);
254 mp_CheckAutoloadTimer(&bundle
->ncp
.mp
);
256 if ((bundle
->phys_type
.open
& (PHYS_DEDICATED
|PHYS_DDIAL
))
257 == bundle
->phys_type
.open
) {
259 if (bundle
->radius
.sessiontime
)
260 bundle_StopSessionTimer(bundle
);
262 bundle_StopIdleTimer(bundle
);
267 bundle_LayerUp(void *v
, struct fsm
*fp
)
270 * The given fsm is now up
271 * If it's an LCP, adjust our phys_mode.open value and check the
273 * If it's the first NCP, calculate our bandwidth
274 * If it's the first NCP, set our ``upat'' time
275 * If it's the first NCP, start the idle timer.
276 * If it's an NCP, tell our -background parent to go away.
277 * If it's the first NCP, start the autoload timer
279 struct bundle
*bundle
= (struct bundle
*)v
;
281 if (fp
->proto
== PROTO_LCP
) {
282 struct physical
*p
= link2physical(fp
->link
);
284 bundle_LinkAdded(bundle
, p
->dl
);
285 mp_CheckAutoloadTimer(&bundle
->ncp
.mp
);
286 } else if (isncp(fp
->proto
)) {
287 if (ncp_LayersOpen(&fp
->bundle
->ncp
) == 1) {
288 bundle_CalculateBandwidth(fp
->bundle
);
291 if (bundle
->radius
.sessiontime
)
292 bundle_StartSessionTimer(bundle
, 0);
294 bundle_StartIdleTimer(bundle
, 0);
295 mp_CheckAutoloadTimer(&fp
->bundle
->ncp
.mp
);
297 bundle_Notify(bundle
, EX_NORMAL
);
298 } else if (fp
->proto
== PROTO_CCP
)
299 bundle_CalculateBandwidth(fp
->bundle
); /* Against ccp_MTUOverhead */
303 bundle_LayerDown(void *v
, struct fsm
*fp
)
306 * The given FSM has been told to come down.
307 * If it's our last NCP, stop the idle timer.
308 * If it's our last NCP, clear our ``upat'' value.
309 * If it's our last NCP, stop the autoload timer
310 * If it's an LCP, adjust our phys_type.open value and any timers.
311 * If it's an LCP and we're in multilink mode, adjust our tun
312 * If it's the last LCP, down all NCPs
313 * speed and make sure our minimum sequence number is adjusted.
316 struct bundle
*bundle
= (struct bundle
*)v
;
318 if (isncp(fp
->proto
)) {
319 if (ncp_LayersOpen(&fp
->bundle
->ncp
) == 0) {
321 if (bundle
->radius
.sessiontime
)
322 bundle_StopSessionTimer(bundle
);
324 bundle_StopIdleTimer(bundle
);
326 mp_StopAutoloadTimer(&bundle
->ncp
.mp
);
328 } else if (fp
->proto
== PROTO_LCP
) {
330 struct datalink
*lost
;
333 bundle_LinksRemoved(bundle
); /* adjust timers & phys_type values */
337 for (dl
= bundle
->links
; dl
; dl
= dl
->next
) {
338 if (fp
== &dl
->physical
->link
.lcp
.fsm
)
340 else if (dl
->state
!= DATALINK_CLOSED
&& dl
->state
!= DATALINK_HANGUP
)
344 if (bundle
->ncp
.mp
.active
) {
345 bundle_CalculateBandwidth(bundle
);
348 mp_LinkLost(&bundle
->ncp
.mp
, lost
);
350 log_Printf(LogALERT
, "Oops, lost an unrecognised datalink (%s) !\n",
354 if (!others_active
) {
355 /* Down the NCPs. We don't expect to get fsm_Close()d ourself ! */
356 ncp2initial(&bundle
->ncp
);
357 mp_Down(&bundle
->ncp
.mp
);
363 bundle_LayerFinish(void *v
, struct fsm
*fp
)
365 /* The given fsm is now down (fp cannot be NULL)
367 * If it's the last NCP, fsm_Close all LCPs
368 * If it's the last NCP, bring any MP layer down
371 struct bundle
*bundle
= (struct bundle
*)v
;
374 if (isncp(fp
->proto
) && !ncp_LayersUnfinished(&bundle
->ncp
)) {
375 if (bundle_Phase(bundle
) != PHASE_DEAD
)
376 bundle_NewPhase(bundle
, PHASE_TERMINATE
);
377 for (dl
= bundle
->links
; dl
; dl
= dl
->next
)
378 if (dl
->state
== DATALINK_OPEN
)
379 datalink_Close(dl
, CLOSE_STAYDOWN
);
381 mp_Down(&bundle
->ncp
.mp
);
386 bundle_Close(struct bundle
*bundle
, const char *name
, int how
)
389 * Please close the given datalink.
390 * If name == NULL or name is the last datalink, fsm_Close all NCPs
392 * If it isn't the last datalink, just Close that datalink.
395 struct datalink
*dl
, *this_dl
;
401 for (dl
= bundle
->links
; dl
; dl
= dl
->next
) {
402 if (name
&& !strcasecmp(name
, dl
->name
))
404 if (name
== NULL
|| this_dl
== dl
) {
407 datalink_DontHangup(dl
);
410 datalink_StayDown(dl
);
413 } else if (dl
->state
!= DATALINK_CLOSED
&& dl
->state
!= DATALINK_HANGUP
)
417 if (name
&& this_dl
== NULL
) {
418 log_Printf(LogWARN
, "%s: Invalid datalink name\n", name
);
422 if (!others_active
) {
424 if (bundle
->radius
.sessiontime
)
425 bundle_StopSessionTimer(bundle
);
427 bundle_StopIdleTimer(bundle
);
428 if (ncp_LayersUnfinished(&bundle
->ncp
))
429 ncp_Close(&bundle
->ncp
);
431 ncp2initial(&bundle
->ncp
);
432 mp_Down(&bundle
->ncp
.mp
);
433 for (dl
= bundle
->links
; dl
; dl
= dl
->next
)
434 datalink_Close(dl
, how
);
436 } else if (this_dl
&& this_dl
->state
!= DATALINK_CLOSED
&&
437 this_dl
->state
!= DATALINK_HANGUP
)
438 datalink_Close(this_dl
, how
);
442 bundle_Down(struct bundle
*bundle
, int how
)
446 for (dl
= bundle
->links
; dl
; dl
= dl
->next
)
447 datalink_Down(dl
, how
);
451 bundle_UpdateSet(struct fdescriptor
*d
, fd_set
*r
, fd_set
*w
, fd_set
*e
, int *n
)
453 struct bundle
*bundle
= descriptor2bundle(d
);
461 /* If there are aren't many packets queued, look for some more. */
462 for (nlinks
= 0, dl
= bundle
->links
; dl
; dl
= dl
->next
)
466 queued
= r
? ncp_FillPhysicalQueues(&bundle
->ncp
, bundle
) :
467 ncp_QueueLen(&bundle
->ncp
);
469 if (r
&& (bundle
->phase
== PHASE_NETWORK
||
470 bundle
->phys_type
.all
& PHYS_AUTO
)) {
471 /* enough surplus so that we can tell if we're getting swamped */
472 ifqueue
= nlinks
> bundle
->cfg
.ifqueue
? nlinks
: bundle
->cfg
.ifqueue
;
473 if (queued
< ifqueue
) {
474 /* Not enough - select() for more */
475 if (bundle
->choked
.timer
.state
== TIMER_RUNNING
)
476 timer_Stop(&bundle
->choked
.timer
); /* Not needed any more */
477 FD_SET(bundle
->dev
.fd
, r
);
478 if (*n
< bundle
->dev
.fd
+ 1)
479 *n
= bundle
->dev
.fd
+ 1;
480 log_Printf(LogTIMER
, "%s: fdset(r) %d\n", TUN_NAME
, bundle
->dev
.fd
);
482 } else if (bundle
->choked
.timer
.state
== TIMER_STOPPED
) {
483 bundle
->choked
.timer
.func
= bundle_ClearQueues
;
484 bundle
->choked
.timer
.name
= "output choke";
485 bundle
->choked
.timer
.load
= bundle
->cfg
.choked
.timeout
* SECTICKS
;
486 bundle
->choked
.timer
.arg
= bundle
;
487 timer_Start(&bundle
->choked
.timer
);
493 result
+= descriptor_UpdateSet(&bundle
->radius
.desc
, r
, w
, e
, n
);
496 /* Which links need a select() ? */
497 for (dl
= bundle
->links
; dl
; dl
= dl
->next
)
498 result
+= descriptor_UpdateSet(&dl
->desc
, r
, w
, e
, n
);
501 * This *MUST* be called after the datalink UpdateSet()s as it
502 * might be ``holding'' one of the datalinks (death-row) and
503 * wants to be able to de-select() it from the descriptor set.
505 result
+= descriptor_UpdateSet(&bundle
->ncp
.mp
.server
.desc
, r
, w
, e
, n
);
511 bundle_IsSet(struct fdescriptor
*d
, const fd_set
*fdset
)
513 struct bundle
*bundle
= descriptor2bundle(d
);
516 for (dl
= bundle
->links
; dl
; dl
= dl
->next
)
517 if (descriptor_IsSet(&dl
->desc
, fdset
))
521 if (descriptor_IsSet(&bundle
->radius
.desc
, fdset
))
525 if (descriptor_IsSet(&bundle
->ncp
.mp
.server
.desc
, fdset
))
528 return FD_ISSET(bundle
->dev
.fd
, fdset
);
532 bundle_DescriptorRead(struct fdescriptor
*d __unused
, struct bundle
*bundle
,
539 if (descriptor_IsSet(&bundle
->ncp
.mp
.server
.desc
, fdset
))
540 descriptor_Read(&bundle
->ncp
.mp
.server
.desc
, bundle
, fdset
);
542 for (dl
= bundle
->links
; dl
; dl
= dl
->next
)
543 if (descriptor_IsSet(&dl
->desc
, fdset
))
544 descriptor_Read(&dl
->desc
, bundle
, fdset
);
547 if (descriptor_IsSet(&bundle
->radius
.desc
, fdset
))
548 descriptor_Read(&bundle
->radius
.desc
, bundle
, fdset
);
551 if (FD_ISSET(bundle
->dev
.fd
, fdset
)) {
557 if (bundle
->dev
.header
) {
558 data
= (u_char
*)&tun
;
562 sz
= sizeof tun
.data
;
565 /* something to read from tun */
567 n
= read(bundle
->dev
.fd
, data
, sz
);
569 log_Printf(LogWARN
, "%s: read: %s\n", bundle
->dev
.Name
, strerror(errno
));
573 if (bundle
->dev
.header
) {
574 n
-= sz
- sizeof tun
.data
;
576 log_Printf(LogERROR
, "%s: read: Got only %d bytes of data !\n",
577 bundle
->dev
.Name
, n
);
580 af
= ntohl(tun
.header
.family
);
582 if (af
!= AF_INET
&& af
!= AF_INET6
)
586 /* XXX: Should be maintaining drop/family counts ! */
591 if (af
== AF_INET
&& ((struct ip
*)tun
.data
)->ip_dst
.s_addr
==
592 bundle
->ncp
.ipcp
.my_ip
.s_addr
) {
593 /* we've been asked to send something addressed *to* us :( */
594 if (Enabled(bundle
, OPT_LOOPBACK
)) {
595 pri
= PacketCheck(bundle
, af
, tun
.data
, n
, &bundle
->filter
.in
,
598 n
+= sz
- sizeof tun
.data
;
599 write(bundle
->dev
.fd
, data
, n
);
600 log_Printf(LogDEBUG
, "Looped back packet addressed to myself\n");
604 log_Printf(LogDEBUG
, "Oops - forwarding packet addressed to myself\n");
608 * Process on-demand dialup. Output packets are queued within the tunnel
609 * device until the appropriate NCP is opened.
612 if (bundle_Phase(bundle
) == PHASE_DEAD
) {
614 * Note, we must be in AUTO mode :-/ otherwise our interface should
615 * *not* be UP and we can't receive data
617 pri
= PacketCheck(bundle
, af
, tun
.data
, n
, &bundle
->filter
.dial
,
620 bundle_Open(bundle
, NULL
, PHYS_AUTO
, 0);
623 * Drop the packet. If we were to queue it, we'd just end up with
624 * a pile of timed-out data in our output queue by the time we get
625 * around to actually dialing. We'd also prematurely reach the
626 * threshold at which we stop select()ing to read() the tun
627 * device - breaking auto-dial.
633 pri
= PacketCheck(bundle
, af
, tun
.data
, n
, &bundle
->filter
.out
,
636 /* Prepend the number of seconds timeout given in the filter */
637 tun
.header
.timeout
= secs
;
638 ncp_Enqueue(&bundle
->ncp
, af
, pri
, (char *)&tun
, n
+ sizeof tun
.header
);
644 bundle_DescriptorWrite(struct fdescriptor
*d __unused
, struct bundle
*bundle
,
650 /* This is not actually necessary as struct mpserver doesn't Write() */
651 if (descriptor_IsSet(&bundle
->ncp
.mp
.server
.desc
, fdset
))
652 if (descriptor_Write(&bundle
->ncp
.mp
.server
.desc
, bundle
, fdset
) == 1)
655 for (dl
= bundle
->links
; dl
; dl
= dl
->next
)
656 if (descriptor_IsSet(&dl
->desc
, fdset
))
657 switch (descriptor_Write(&dl
->desc
, bundle
, fdset
)) {
659 datalink_ComeDown(dl
, CLOSE_NORMAL
);
669 bundle_LockTun(struct bundle
*bundle
)
672 char pidfile
[PATH_MAX
];
674 snprintf(pidfile
, sizeof pidfile
, "%stun%d.pid", _PATH_VARRUN
, bundle
->unit
);
675 lockfile
= ID0fopen(pidfile
, "w");
676 if (lockfile
!= NULL
) {
677 fprintf(lockfile
, "%d\n", (int)getpid());
680 #ifndef RELEASE_CRUNCH
682 log_Printf(LogERROR
, "Warning: Can't create %s: %s\n",
683 pidfile
, strerror(errno
));
688 bundle_UnlockTun(struct bundle
*bundle
)
690 char pidfile
[PATH_MAX
];
692 snprintf(pidfile
, sizeof pidfile
, "%stun%d.pid", _PATH_VARRUN
, bundle
->unit
);
697 bundle_Create(const char *prefix
, int type
, int unit
)
699 static struct bundle bundle
; /* there can be only one */
700 int enoentcount
, err
, minunit
, maxunit
;
702 #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
705 #if defined(TUNSIFMODE) || defined(TUNSLMODE) || defined(TUNSIFHEAD)
709 if (bundle
.iface
!= NULL
) { /* Already allocated ! */
710 log_Printf(LogALERT
, "bundle_Create: There's only one BUNDLE !\n");
723 #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
726 for (bundle
.unit
= minunit
; bundle
.unit
!= maxunit
; bundle
.unit
++) {
727 snprintf(bundle
.dev
.Name
, sizeof bundle
.dev
.Name
, "%s%d",
728 prefix
, bundle
.unit
);
729 bundle
.dev
.fd
= ID0open(bundle
.dev
.Name
, O_RDWR
);
730 if (bundle
.dev
.fd
>= 0)
732 else if (errno
== ENXIO
|| errno
== ENOENT
) {
733 #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
734 if (bundle
.unit
== minunit
&& !kldtried
++) {
736 * Attempt to load the tunnel interface KLD if it isn't loaded
739 if (loadmodules(LOAD_VERBOSLY
, "if_tun", NULL
))
744 if (errno
!= ENOENT
|| ++enoentcount
> 2) {
752 if (bundle
.dev
.fd
< 0) {
754 log_Printf(LogWARN
, "No available tunnel devices found (%s)\n",
757 log_Printf(LogWARN
, "%s%d: %s\n", prefix
, unit
, strerror(err
));
761 log_SetTun(bundle
.unit
);
763 ifname
= strrchr(bundle
.dev
.Name
, '/');
765 ifname
= bundle
.dev
.Name
;
769 bundle
.iface
= iface_Create(ifname
);
770 if (bundle
.iface
== NULL
) {
771 close(bundle
.dev
.fd
);
776 /* Make sure we're POINTOPOINT & IFF_MULTICAST */
777 iff
= IFF_POINTOPOINT
| IFF_MULTICAST
;
778 if (ID0ioctl(bundle
.dev
.fd
, TUNSIFMODE
, &iff
) < 0)
779 log_Printf(LogERROR
, "bundle_Create: ioctl(TUNSIFMODE): %s\n",
784 /* Make sure we're not prepending sockaddrs */
786 if (ID0ioctl(bundle
.dev
.fd
, TUNSLMODE
, &iff
) < 0)
787 log_Printf(LogERROR
, "bundle_Create: ioctl(TUNSLMODE): %s\n",
792 /* We want the address family please ! */
794 if (ID0ioctl(bundle
.dev
.fd
, TUNSIFHEAD
, &iff
) < 0) {
795 log_Printf(LogERROR
, "bundle_Create: ioctl(TUNSIFHEAD): %s\n",
797 bundle
.dev
.header
= 0;
799 bundle
.dev
.header
= 1;
802 /* Always present for OpenBSD */
803 bundle
.dev
.header
= 1;
806 * If TUNSIFHEAD isn't available and we're not OpenBSD, assume
807 * everything's AF_INET (hopefully the tun device won't pass us
810 bundle
.dev
.header
= 0;
814 log_Printf(LogPHASE
, "Using interface: %s\n", ifname
);
816 bundle
.bandwidth
= 0;
817 bundle
.routing_seq
= 0;
818 bundle
.phase
= PHASE_DEAD
;
819 bundle
.CleaningUp
= 0;
820 bundle
.NatEnabled
= 0;
822 bundle
.fsm
.LayerStart
= bundle_LayerStart
;
823 bundle
.fsm
.LayerUp
= bundle_LayerUp
;
824 bundle
.fsm
.LayerDown
= bundle_LayerDown
;
825 bundle
.fsm
.LayerFinish
= bundle_LayerFinish
;
826 bundle
.fsm
.object
= &bundle
;
828 bundle
.cfg
.idle
.timeout
= NCP_IDLE_TIMEOUT
;
829 bundle
.cfg
.idle
.min_timeout
= 0;
830 *bundle
.cfg
.auth
.name
= '\0';
831 *bundle
.cfg
.auth
.key
= '\0';
832 bundle
.cfg
.optmask
= (1ull << OPT_IDCHECK
) | (1ull << OPT_LOOPBACK
) |
833 (1ull << OPT_SROUTES
) | (1ull << OPT_TCPMSSFIXUP
) |
834 (1ull << OPT_THROUGHPUT
) | (1ull << OPT_UTMP
) |
835 (1ull << OPT_NAS_IP_ADDRESS
) |
836 (1ull << OPT_NAS_IDENTIFIER
);
838 opt_enable(&bundle
, OPT_IPCP
);
839 if (probe
.ipv6_available
)
840 opt_enable(&bundle
, OPT_IPV6CP
);
842 *bundle
.cfg
.label
= '\0';
843 bundle
.cfg
.ifqueue
= DEF_IFQUEUE
;
844 bundle
.cfg
.choked
.timeout
= CHOKED_TIMEOUT
;
845 bundle
.phys_type
.all
= type
;
846 bundle
.phys_type
.open
= 0;
849 bundle
.links
= datalink_Create("deflink", &bundle
, type
);
850 if (bundle
.links
== NULL
) {
851 log_Printf(LogALERT
, "Cannot create data link: %s\n", strerror(errno
));
852 iface_Destroy(bundle
.iface
);
854 close(bundle
.dev
.fd
);
858 bundle
.desc
.type
= BUNDLE_DESCRIPTOR
;
859 bundle
.desc
.UpdateSet
= bundle_UpdateSet
;
860 bundle
.desc
.IsSet
= bundle_IsSet
;
861 bundle
.desc
.Read
= bundle_DescriptorRead
;
862 bundle
.desc
.Write
= bundle_DescriptorWrite
;
864 ncp_Init(&bundle
.ncp
, &bundle
);
866 memset(&bundle
.filter
, '\0', sizeof bundle
.filter
);
867 bundle
.filter
.in
.fragok
= bundle
.filter
.in
.logok
= 1;
868 bundle
.filter
.in
.name
= "IN";
869 bundle
.filter
.out
.fragok
= bundle
.filter
.out
.logok
= 1;
870 bundle
.filter
.out
.name
= "OUT";
871 bundle
.filter
.dial
.name
= "DIAL";
872 bundle
.filter
.dial
.logok
= 1;
873 bundle
.filter
.alive
.name
= "ALIVE";
874 bundle
.filter
.alive
.logok
= 1;
877 for (i
= 0; i
< MAXFILTERS
; i
++) {
878 bundle
.filter
.in
.rule
[i
].f_action
= A_NONE
;
879 bundle
.filter
.out
.rule
[i
].f_action
= A_NONE
;
880 bundle
.filter
.dial
.rule
[i
].f_action
= A_NONE
;
881 bundle
.filter
.alive
.rule
[i
].f_action
= A_NONE
;
884 memset(&bundle
.idle
.timer
, '\0', sizeof bundle
.idle
.timer
);
885 bundle
.idle
.done
= 0;
886 bundle
.notify
.fd
= -1;
887 memset(&bundle
.choked
.timer
, '\0', sizeof bundle
.choked
.timer
);
889 radius_Init(&bundle
.radius
);
892 /* Clean out any leftover crud */
893 iface_Clear(bundle
.iface
, &bundle
.ncp
, 0, IFACE_CLEAR_ALL
);
895 bundle_LockTun(&bundle
);
901 bundle_DownInterface(struct bundle
*bundle
)
903 route_IfDelete(bundle
, 1);
904 iface_ClearFlags(bundle
->iface
->name
, IFF_UP
);
908 bundle_Destroy(struct bundle
*bundle
)
913 * Clean up the interface. We don't really need to do the timer_Stop()s,
914 * mp_Down(), iface_Clear() and bundle_DownInterface() unless we're getting
915 * out under exceptional conditions such as a descriptor exception.
917 timer_Stop(&bundle
->idle
.timer
);
918 timer_Stop(&bundle
->choked
.timer
);
919 mp_Down(&bundle
->ncp
.mp
);
920 iface_Clear(bundle
->iface
, &bundle
->ncp
, 0, IFACE_CLEAR_ALL
);
921 bundle_DownInterface(bundle
);
924 /* Tell the radius server the bad news */
925 radius_Destroy(&bundle
->radius
);
928 /* Again, these are all DATALINK_CLOSED unless we're abending */
931 dl
= datalink_Destroy(dl
);
933 ncp_Destroy(&bundle
->ncp
);
935 close(bundle
->dev
.fd
);
936 bundle_UnlockTun(bundle
);
938 /* In case we never made PHASE_NETWORK */
939 bundle_Notify(bundle
, EX_ERRDEAD
);
941 iface_Destroy(bundle
->iface
);
942 bundle
->iface
= NULL
;
946 bundle_LinkClosed(struct bundle
*bundle
, struct datalink
*dl
)
949 * Our datalink has closed.
950 * CleanDatalinks() (called from DoLoop()) will remove closed
951 * BACKGROUND, FOREGROUND and DIRECT links.
952 * If it's the last data link, enter phase DEAD.
954 * NOTE: dl may not be in our list (bundle_SendDatalink()) !
957 struct datalink
*odl
;
960 log_SetTtyCommandMode(dl
);
963 for (odl
= bundle
->links
; odl
; odl
= odl
->next
)
964 if (odl
!= dl
&& odl
->state
!= DATALINK_CLOSED
)
968 if (dl
->physical
->type
!= PHYS_AUTO
) /* Not in -auto mode */
969 bundle_DownInterface(bundle
);
970 ncp2initial(&bundle
->ncp
);
971 mp_Down(&bundle
->ncp
.mp
);
972 bundle_NewPhase(bundle
, PHASE_DEAD
);
974 if (bundle
->radius
.sessiontime
)
975 bundle_StopSessionTimer(bundle
);
977 bundle_StopIdleTimer(bundle
);
982 bundle_Open(struct bundle
*bundle
, const char *name
, int mask
, int force
)
985 * Please open the given datalink, or all if name == NULL
989 for (dl
= bundle
->links
; dl
; dl
= dl
->next
)
990 if (name
== NULL
|| !strcasecmp(dl
->name
, name
)) {
991 if ((mask
& dl
->physical
->type
) &&
992 (dl
->state
== DATALINK_CLOSED
||
993 (force
&& dl
->state
== DATALINK_OPENING
&&
994 dl
->dial
.timer
.state
== TIMER_RUNNING
) ||
995 dl
->state
== DATALINK_READY
)) {
996 timer_Stop(&dl
->dial
.timer
); /* We're finished with this */
997 datalink_Up(dl
, 1, 1);
998 if (mask
& PHYS_AUTO
)
999 break; /* Only one AUTO link at a time */
1007 bundle2datalink(struct bundle
*bundle
, const char *name
)
1009 struct datalink
*dl
;
1012 for (dl
= bundle
->links
; dl
; dl
= dl
->next
)
1013 if (!strcasecmp(dl
->name
, name
))
1015 } else if (bundle
->links
&& !bundle
->links
->next
)
1016 return bundle
->links
;
1022 bundle_ShowLinks(struct cmdargs
const *arg
)
1024 struct datalink
*dl
;
1025 struct pppThroughput
*t
;
1026 unsigned long long octets
;
1029 for (dl
= arg
->bundle
->links
; dl
; dl
= dl
->next
) {
1030 octets
= MAX(dl
->physical
->link
.stats
.total
.in
.OctetsPerSecond
,
1031 dl
->physical
->link
.stats
.total
.out
.OctetsPerSecond
);
1033 prompt_Printf(arg
->prompt
, "Name: %s [%s, %s]",
1034 dl
->name
, mode2Nam(dl
->physical
->type
), datalink_State(dl
));
1035 if (dl
->physical
->link
.stats
.total
.rolling
&& dl
->state
== DATALINK_OPEN
)
1036 prompt_Printf(arg
->prompt
, " bandwidth %d, %llu bps (%llu bytes/sec)",
1037 dl
->mp
.bandwidth
? dl
->mp
.bandwidth
:
1038 physical_GetSpeed(dl
->physical
),
1039 octets
* 8, octets
);
1040 prompt_Printf(arg
->prompt
, "\n");
1043 t
= &arg
->bundle
->ncp
.mp
.link
.stats
.total
;
1044 octets
= MAX(t
->in
.OctetsPerSecond
, t
->out
.OctetsPerSecond
);
1045 secs
= t
->downtime
? 0 : throughput_uptime(t
);
1046 if (secs
> t
->SamplePeriod
)
1047 secs
= t
->SamplePeriod
;
1049 prompt_Printf(arg
->prompt
, "Currently averaging %llu bps (%llu bytes/sec)"
1050 " over the last %d secs\n", octets
* 8, octets
, secs
);
1056 optval(struct bundle
*bundle
, int opt
)
1058 return Enabled(bundle
, opt
) ? "enabled" : "disabled";
1062 bundle_ShowStatus(struct cmdargs
const *arg
)
1066 prompt_Printf(arg
->prompt
, "Phase %s\n", bundle_PhaseName(arg
->bundle
));
1067 prompt_Printf(arg
->prompt
, " Device: %s\n", arg
->bundle
->dev
.Name
);
1068 prompt_Printf(arg
->prompt
, " Interface: %s @ %lubps",
1069 arg
->bundle
->iface
->name
, arg
->bundle
->bandwidth
);
1071 if (arg
->bundle
->upat
) {
1072 int secs
= bundle_Uptime(arg
->bundle
);
1074 prompt_Printf(arg
->prompt
, ", up time %d:%02d:%02d", secs
/ 3600,
1075 (secs
/ 60) % 60, secs
% 60);
1077 prompt_Printf(arg
->prompt
, "\n Queued: %lu of %u\n",
1078 (unsigned long)ncp_QueueLen(&arg
->bundle
->ncp
),
1079 arg
->bundle
->cfg
.ifqueue
);
1081 prompt_Printf(arg
->prompt
, "\nDefaults:\n");
1082 prompt_Printf(arg
->prompt
, " Label: %s\n",
1083 arg
->bundle
->cfg
.label
);
1084 prompt_Printf(arg
->prompt
, " Auth name: %s\n",
1085 arg
->bundle
->cfg
.auth
.name
);
1086 prompt_Printf(arg
->prompt
, " Diagnostic socket: ");
1087 if (*server
.cfg
.sockname
!= '\0') {
1088 prompt_Printf(arg
->prompt
, "%s", server
.cfg
.sockname
);
1089 if (server
.cfg
.mask
!= (mode_t
)-1)
1090 prompt_Printf(arg
->prompt
, ", mask 0%03o", (int)server
.cfg
.mask
);
1091 prompt_Printf(arg
->prompt
, "%s\n", server
.fd
== -1 ? " (not open)" : "");
1092 } else if (server
.cfg
.port
!= 0)
1093 prompt_Printf(arg
->prompt
, "TCP port %d%s\n", server
.cfg
.port
,
1094 server
.fd
== -1 ? " (not open)" : "");
1096 prompt_Printf(arg
->prompt
, "none\n");
1098 prompt_Printf(arg
->prompt
, " Choked Timer: %us\n",
1099 arg
->bundle
->cfg
.choked
.timeout
);
1102 radius_Show(&arg
->bundle
->radius
, arg
->prompt
);
1105 prompt_Printf(arg
->prompt
, " Idle Timer: ");
1106 if (arg
->bundle
->cfg
.idle
.timeout
) {
1107 prompt_Printf(arg
->prompt
, "%us", arg
->bundle
->cfg
.idle
.timeout
);
1108 if (arg
->bundle
->cfg
.idle
.min_timeout
)
1109 prompt_Printf(arg
->prompt
, ", min %us",
1110 arg
->bundle
->cfg
.idle
.min_timeout
);
1111 remaining
= bundle_RemainingIdleTime(arg
->bundle
);
1112 if (remaining
!= -1)
1113 prompt_Printf(arg
->prompt
, " (%ds remaining)", remaining
);
1114 prompt_Printf(arg
->prompt
, "\n");
1116 prompt_Printf(arg
->prompt
, "disabled\n");
1118 prompt_Printf(arg
->prompt
, " Filter Decap: %-20.20s",
1119 optval(arg
->bundle
, OPT_FILTERDECAP
));
1120 prompt_Printf(arg
->prompt
, " ID check: %s\n",
1121 optval(arg
->bundle
, OPT_IDCHECK
));
1122 prompt_Printf(arg
->prompt
, " Iface-Alias: %-20.20s",
1123 optval(arg
->bundle
, OPT_IFACEALIAS
));
1125 prompt_Printf(arg
->prompt
, " IPCP: %s\n",
1126 optval(arg
->bundle
, OPT_IPCP
));
1127 prompt_Printf(arg
->prompt
, " IPV6CP: %-20.20s",
1128 optval(arg
->bundle
, OPT_IPV6CP
));
1130 prompt_Printf(arg
->prompt
, " Keep-Session: %s\n",
1131 optval(arg
->bundle
, OPT_KEEPSESSION
));
1132 prompt_Printf(arg
->prompt
, " Loopback: %-20.20s",
1133 optval(arg
->bundle
, OPT_LOOPBACK
));
1134 prompt_Printf(arg
->prompt
, " PasswdAuth: %s\n",
1135 optval(arg
->bundle
, OPT_PASSWDAUTH
));
1136 prompt_Printf(arg
->prompt
, " Proxy: %-20.20s",
1137 optval(arg
->bundle
, OPT_PROXY
));
1138 prompt_Printf(arg
->prompt
, " Proxyall: %s\n",
1139 optval(arg
->bundle
, OPT_PROXYALL
));
1140 prompt_Printf(arg
->prompt
, " Sticky Routes: %-20.20s",
1141 optval(arg
->bundle
, OPT_SROUTES
));
1142 prompt_Printf(arg
->prompt
, " TCPMSS Fixup: %s\n",
1143 optval(arg
->bundle
, OPT_TCPMSSFIXUP
));
1144 prompt_Printf(arg
->prompt
, " Throughput: %-20.20s",
1145 optval(arg
->bundle
, OPT_THROUGHPUT
));
1146 prompt_Printf(arg
->prompt
, " Utmp Logging: %s\n",
1147 optval(arg
->bundle
, OPT_UTMP
));
1148 prompt_Printf(arg
->prompt
, " NAS-IP-Address: %-20.20s",
1149 optval(arg
->bundle
, OPT_NAS_IP_ADDRESS
));
1150 prompt_Printf(arg
->prompt
, " NAS-Identifier: %s\n",
1151 optval(arg
->bundle
, OPT_NAS_IDENTIFIER
));
1157 bundle_IdleTimeout(void *v
)
1159 struct bundle
*bundle
= (struct bundle
*)v
;
1161 log_Printf(LogPHASE
, "Idle timer expired\n");
1162 bundle_StopIdleTimer(bundle
);
1163 bundle_Close(bundle
, NULL
, CLOSE_STAYDOWN
);
1167 * Start Idle timer. If timeout is reached, we call bundle_Close() to
1168 * close LCP and link.
1171 bundle_StartIdleTimer(struct bundle
*bundle
, unsigned secs
)
1173 timer_Stop(&bundle
->idle
.timer
);
1174 if ((bundle
->phys_type
.open
& (PHYS_DEDICATED
|PHYS_DDIAL
)) !=
1175 bundle
->phys_type
.open
&& bundle
->cfg
.idle
.timeout
) {
1176 time_t now
= time(NULL
);
1179 secs
= bundle
->cfg
.idle
.timeout
;
1181 /* We want at least `secs' */
1182 if (bundle
->cfg
.idle
.min_timeout
> secs
&& bundle
->upat
) {
1183 unsigned up
= now
- bundle
->upat
;
1185 if (bundle
->cfg
.idle
.min_timeout
> up
&&
1186 bundle
->cfg
.idle
.min_timeout
- up
> (long long)secs
)
1187 /* Only increase from the current `remaining' value */
1188 secs
= bundle
->cfg
.idle
.min_timeout
- up
;
1190 bundle
->idle
.timer
.func
= bundle_IdleTimeout
;
1191 bundle
->idle
.timer
.name
= "idle";
1192 bundle
->idle
.timer
.load
= secs
* SECTICKS
;
1193 bundle
->idle
.timer
.arg
= bundle
;
1194 timer_Start(&bundle
->idle
.timer
);
1195 bundle
->idle
.done
= now
+ secs
;
1200 bundle_SetIdleTimer(struct bundle
*bundle
, unsigned timeout
,
1201 unsigned min_timeout
)
1203 bundle
->cfg
.idle
.timeout
= timeout
;
1204 bundle
->cfg
.idle
.min_timeout
= min_timeout
;
1205 if (ncp_LayersOpen(&bundle
->ncp
))
1206 bundle_StartIdleTimer(bundle
, 0);
1210 bundle_StopIdleTimer(struct bundle
*bundle
)
1212 timer_Stop(&bundle
->idle
.timer
);
1213 bundle
->idle
.done
= 0;
1217 bundle_RemainingIdleTime(struct bundle
*bundle
)
1219 if (bundle
->idle
.done
)
1220 return bundle
->idle
.done
- time(NULL
);
1227 bundle_SessionTimeout(void *v
)
1229 struct bundle
*bundle
= (struct bundle
*)v
;
1231 log_Printf(LogPHASE
, "Session-Timeout timer expired\n");
1232 bundle_StopSessionTimer(bundle
);
1233 bundle_Close(bundle
, NULL
, CLOSE_STAYDOWN
);
1237 bundle_StartSessionTimer(struct bundle
*bundle
, unsigned secs
)
1239 timer_Stop(&bundle
->session
.timer
);
1240 if ((bundle
->phys_type
.open
& (PHYS_DEDICATED
|PHYS_DDIAL
)) !=
1241 bundle
->phys_type
.open
&& bundle
->radius
.sessiontime
) {
1242 time_t now
= time(NULL
);
1245 secs
= bundle
->radius
.sessiontime
;
1247 bundle
->session
.timer
.func
= bundle_SessionTimeout
;
1248 bundle
->session
.timer
.name
= "session";
1249 bundle
->session
.timer
.load
= secs
* SECTICKS
;
1250 bundle
->session
.timer
.arg
= bundle
;
1251 timer_Start(&bundle
->session
.timer
);
1252 bundle
->session
.done
= now
+ secs
;
1257 bundle_StopSessionTimer(struct bundle
*bundle
)
1259 timer_Stop(&bundle
->session
.timer
);
1260 bundle
->session
.done
= 0;
1266 bundle_IsDead(struct bundle
*bundle
)
1268 return !bundle
->links
|| (bundle
->phase
== PHASE_DEAD
&& bundle
->CleaningUp
);
1271 static struct datalink
*
1272 bundle_DatalinkLinkout(struct bundle
*bundle
, struct datalink
*dl
)
1274 struct datalink
**dlp
;
1276 for (dlp
= &bundle
->links
; *dlp
; dlp
= &(*dlp
)->next
)
1280 bundle_LinksRemoved(bundle
);
1288 bundle_DatalinkLinkin(struct bundle
*bundle
, struct datalink
*dl
)
1290 struct datalink
**dlp
= &bundle
->links
;
1293 dlp
= &(*dlp
)->next
;
1298 bundle_LinkAdded(bundle
, dl
);
1299 mp_CheckAutoloadTimer(&bundle
->ncp
.mp
);
1303 bundle_CleanDatalinks(struct bundle
*bundle
)
1305 struct datalink
**dlp
= &bundle
->links
;
1309 if ((*dlp
)->state
== DATALINK_CLOSED
&&
1310 (*dlp
)->physical
->type
&
1311 (PHYS_DIRECT
|PHYS_BACKGROUND
|PHYS_FOREGROUND
)) {
1312 *dlp
= datalink_Destroy(*dlp
);
1315 dlp
= &(*dlp
)->next
;
1318 bundle_LinksRemoved(bundle
);
1322 bundle_DatalinkClone(struct bundle
*bundle
, struct datalink
*dl
,
1325 if (bundle2datalink(bundle
, name
)) {
1326 log_Printf(LogWARN
, "Clone: %s: name already exists\n", name
);
1330 bundle_DatalinkLinkin(bundle
, datalink_Clone(dl
, name
));
1335 bundle_DatalinkRemove(struct bundle
*bundle
, struct datalink
*dl
)
1337 dl
= bundle_DatalinkLinkout(bundle
, dl
);
1339 datalink_Destroy(dl
);
1343 bundle_SetLabel(struct bundle
*bundle
, const char *label
)
1346 strncpy(bundle
->cfg
.label
, label
, sizeof bundle
->cfg
.label
- 1);
1348 *bundle
->cfg
.label
= '\0';
1352 bundle_GetLabel(struct bundle
*bundle
)
1354 return *bundle
->cfg
.label
? bundle
->cfg
.label
: NULL
;
1360 struct iovec iov
[SCATTER_SEGMENTS
];
1361 int niov
, expect
, f
;
1363 iov
[0].iov_len
= strlen(Version
) + 1;
1364 iov
[0].iov_base
= NULL
;
1366 if (datalink2iov(NULL
, iov
, &niov
, SCATTER_SEGMENTS
, NULL
, NULL
) == -1) {
1367 log_Printf(LogERROR
, "Cannot determine space required for link\n");
1371 for (f
= expect
= 0; f
< niov
; f
++)
1372 expect
+= iov
[f
].iov_len
;
1378 bundle_ReceiveDatalink(struct bundle
*bundle
, int s
)
1380 char cmsgbuf
[sizeof(struct cmsghdr
) + sizeof(int) * SEND_MAXFD
];
1381 int niov
, expect
, f
, *fd
, nfd
, onfd
;
1383 struct iovec iov
[SCATTER_SEGMENTS
];
1384 struct cmsghdr
*cmsg
;
1386 struct datalink
*dl
;
1389 log_Printf(LogPHASE
, "Receiving datalink\n");
1392 * Create our scatter/gather array - passing NULL gets the space
1393 * allocation requirement rather than actually flattening the
1396 iov
[0].iov_len
= strlen(Version
) + 1;
1397 iov
[0].iov_base
= NULL
;
1399 if (datalink2iov(NULL
, iov
, &niov
, SCATTER_SEGMENTS
, NULL
, NULL
) == -1) {
1400 log_Printf(LogERROR
, "Cannot determine space required for link\n");
1404 /* Allocate the scatter/gather array for recvmsg() */
1405 for (f
= expect
= 0; f
< niov
; f
++) {
1406 if ((iov
[f
].iov_base
= malloc(iov
[f
].iov_len
)) == NULL
) {
1407 log_Printf(LogERROR
, "Cannot allocate space to receive link\n");
1411 expect
+= iov
[f
].iov_len
;
1414 /* Set up our message */
1415 cmsg
= (struct cmsghdr
*)cmsgbuf
;
1416 cmsg
->cmsg_len
= sizeof cmsgbuf
;
1417 cmsg
->cmsg_level
= SOL_SOCKET
;
1418 cmsg
->cmsg_type
= 0;
1420 memset(&msg
, '\0', sizeof msg
);
1421 msg
.msg_name
= NULL
;
1422 msg
.msg_namelen
= 0;
1424 msg
.msg_iovlen
= 1; /* Only send the version at the first pass */
1425 msg
.msg_control
= cmsgbuf
;
1426 msg
.msg_controllen
= sizeof cmsgbuf
;
1428 log_Printf(LogDEBUG
, "Expecting %u scatter/gather bytes\n",
1429 (unsigned)iov
[0].iov_len
);
1431 if ((got
= recvmsg(s
, &msg
, MSG_WAITALL
)) != (ssize_t
)iov
[0].iov_len
) {
1433 log_Printf(LogERROR
, "Failed recvmsg: %s\n", strerror(errno
));
1435 log_Printf(LogERROR
, "Failed recvmsg: Got %zd, not %u\n",
1436 got
, (unsigned)iov
[0].iov_len
);
1438 free(iov
[niov
].iov_base
);
1442 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
1443 log_Printf(LogERROR
, "Recvmsg: no descriptors received !\n");
1445 free(iov
[niov
].iov_base
);
1449 fd
= (int *)CMSG_DATA(cmsg
);
1450 nfd
= ((caddr_t
)cmsg
+ cmsg
->cmsg_len
- (caddr_t
)fd
) / sizeof(int);
1453 log_Printf(LogERROR
, "Recvmsg: %d descriptor%s received (too few) !\n",
1454 nfd
, nfd
== 1 ? "" : "s");
1458 free(iov
[niov
].iov_base
);
1463 * We've successfully received two or more open file descriptors
1464 * through our socket, plus a version string. Make sure it's the
1465 * correct version, and drop the connection if it's not.
1467 if (strncmp(Version
, iov
[0].iov_base
, iov
[0].iov_len
)) {
1468 log_Printf(LogWARN
, "Cannot receive datalink, incorrect version"
1469 " (\"%.*s\", not \"%s\")\n", (int)iov
[0].iov_len
,
1470 (char *)iov
[0].iov_base
, Version
);
1474 free(iov
[niov
].iov_base
);
1479 * Everything looks good. Send the other side our process id so that
1480 * they can transfer lock ownership, and wait for them to send the
1484 if ((got
= write(fd
[1], &pid
, sizeof pid
)) != sizeof pid
) {
1486 log_Printf(LogERROR
, "Failed write: %s\n", strerror(errno
));
1488 log_Printf(LogERROR
, "Failed write: Got %zd, not %d\n", got
,
1493 free(iov
[niov
].iov_base
);
1497 if ((got
= readv(fd
[1], iov
+ 1, niov
- 1)) != expect
) {
1499 log_Printf(LogERROR
, "Failed write: %s\n", strerror(errno
));
1501 log_Printf(LogERROR
, "Failed write: Got %zd, not %d\n", got
, expect
);
1505 free(iov
[niov
].iov_base
);
1510 onfd
= nfd
; /* We've got this many in our array */
1511 nfd
-= 2; /* Don't include p->fd and our reply descriptor */
1512 niov
= 1; /* Skip the version id */
1513 dl
= iov2datalink(bundle
, iov
, &niov
, sizeof iov
/ sizeof *iov
, fd
[0],
1518 log_Printf(LogERROR
, "bundle_ReceiveDatalink: Failed to handle %d "
1519 "auxiliary file descriptors (%d remain)\n", onfd
, nfd
);
1520 datalink_Destroy(dl
);
1525 bundle_DatalinkLinkin(bundle
, dl
);
1526 datalink_AuthOk(dl
);
1527 bundle_CalculateBandwidth(dl
->bundle
);
1536 free(iov
[0].iov_base
);
1540 bundle_SendDatalink(struct datalink
*dl
, int s
, struct sockaddr_un
*sun
)
1542 char cmsgbuf
[CMSG_SPACE(sizeof(int) * SEND_MAXFD
)];
1543 const char *constlock
;
1545 struct cmsghdr
*cmsg
;
1547 struct iovec iov
[SCATTER_SEGMENTS
];
1548 int niov
, f
, expect
, newsid
, fd
[SEND_MAXFD
], nfd
, reply
[2];
1552 log_Printf(LogPHASE
, "Transmitting datalink %s\n", dl
->name
);
1554 /* Record the base device name for a lock transfer later */
1555 constlock
= physical_LockedDevice(dl
->physical
);
1557 lock
= alloca(strlen(constlock
) + 1);
1558 strcpy(lock
, constlock
);
1562 bundle_LinkClosed(dl
->bundle
, dl
);
1563 bundle_DatalinkLinkout(dl
->bundle
, dl
);
1565 /* Build our scatter/gather array */
1566 iov
[0].iov_len
= strlen(Version
) + 1;
1567 iov
[0].iov_base
= strdup(Version
);
1571 fd
[0] = datalink2iov(dl
, iov
, &niov
, SCATTER_SEGMENTS
, fd
+ 2, &nfd
);
1573 if (fd
[0] != -1 && socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, reply
) != -1) {
1575 * fd[1] is used to get the peer process id back, then to confirm that
1576 * we've transferred any device locks to that process id.
1580 nfd
+= 2; /* Include fd[0] and fd[1] */
1581 memset(&msg
, '\0', sizeof msg
);
1583 msg
.msg_name
= NULL
;
1584 msg
.msg_namelen
= 0;
1586 * Only send the version to start... We used to send the whole lot, but
1587 * this caused problems with our RECVBUF size as a single link is about
1588 * 22k ! This way, we should bump into no limits.
1592 msg
.msg_control
= cmsgbuf
;
1593 msg
.msg_controllen
= CMSG_SPACE(sizeof(int) * nfd
);
1596 cmsg
= (struct cmsghdr
*)cmsgbuf
;
1597 cmsg
->cmsg_len
= msg
.msg_controllen
;
1598 cmsg
->cmsg_level
= SOL_SOCKET
;
1599 cmsg
->cmsg_type
= SCM_RIGHTS
;
1601 for (f
= 0; f
< nfd
; f
++)
1602 *((int *)CMSG_DATA(cmsg
) + f
) = fd
[f
];
1604 for (f
= 1, expect
= 0; f
< niov
; f
++)
1605 expect
+= iov
[f
].iov_len
;
1607 if (setsockopt(reply
[0], SOL_SOCKET
, SO_SNDBUF
, &expect
, sizeof(int)) == -1)
1608 log_Printf(LogERROR
, "setsockopt(SO_RCVBUF, %d): %s\n", expect
,
1610 if (setsockopt(reply
[1], SOL_SOCKET
, SO_RCVBUF
, &expect
, sizeof(int)) == -1)
1611 log_Printf(LogERROR
, "setsockopt(SO_RCVBUF, %d): %s\n", expect
,
1614 log_Printf(LogDEBUG
, "Sending %d descriptor%s and %u bytes in scatter"
1615 "/gather array\n", nfd
, nfd
== 1 ? "" : "s",
1616 (unsigned)iov
[0].iov_len
);
1618 if ((got
= sendmsg(s
, &msg
, 0)) == -1)
1619 log_Printf(LogERROR
, "Failed sendmsg: %s: %s\n",
1620 sun
->sun_path
, strerror(errno
));
1621 else if (got
!= (ssize_t
)iov
[0].iov_len
)
1622 log_Printf(LogERROR
, "%s: Failed initial sendmsg: Only sent %zd of %u\n",
1623 sun
->sun_path
, got
, (unsigned)iov
[0].iov_len
);
1625 /* We must get the ACK before closing the descriptor ! */
1628 if ((got
= read(reply
[0], &newpid
, sizeof newpid
)) == sizeof newpid
) {
1629 log_Printf(LogDEBUG
, "Received confirmation from pid %ld\n",
1631 if (lock
&& (res
= ID0uu_lock_txfr(lock
, newpid
)) != UU_LOCK_OK
)
1632 log_Printf(LogERROR
, "uu_lock_txfr: %s\n", uu_lockerr(res
));
1634 log_Printf(LogDEBUG
, "Transmitting link (%d bytes)\n", expect
);
1635 if ((got
= writev(reply
[0], iov
+ 1, niov
- 1)) != expect
) {
1637 log_Printf(LogERROR
, "%s: Failed writev: %s\n",
1638 sun
->sun_path
, strerror(errno
));
1640 log_Printf(LogERROR
, "%s: Failed writev: Wrote %zd of %d\n",
1641 sun
->sun_path
, got
, expect
);
1643 } else if (got
== -1)
1644 log_Printf(LogERROR
, "%s: Failed socketpair read: %s\n",
1645 sun
->sun_path
, strerror(errno
));
1647 log_Printf(LogERROR
, "%s: Failed socketpair read: Got %zd of %d\n",
1648 sun
->sun_path
, got
, (int)(sizeof newpid
));
1654 newsid
= Enabled(dl
->bundle
, OPT_KEEPSESSION
) ||
1655 tcgetpgrp(fd
[0]) == getpgrp();
1659 bundle_setsid(dl
->bundle
, got
!= -1);
1664 free(iov
[niov
].iov_base
);
1668 bundle_RenameDatalink(struct bundle
*bundle
, struct datalink
*ndl
,
1671 struct datalink
*dl
;
1673 if (!strcasecmp(ndl
->name
, name
))
1676 for (dl
= bundle
->links
; dl
; dl
= dl
->next
)
1677 if (!strcasecmp(dl
->name
, name
))
1680 datalink_Rename(ndl
, name
);
1685 bundle_SetMode(struct bundle
*bundle
, struct datalink
*dl
, int mode
)
1689 omode
= dl
->physical
->type
;
1693 if (mode
== PHYS_AUTO
&& !(bundle
->phys_type
.all
& PHYS_AUTO
))
1694 /* First auto link */
1695 if (bundle
->ncp
.ipcp
.peer_ip
.s_addr
== INADDR_ANY
) {
1696 log_Printf(LogWARN
, "You must `set ifaddr' or `open' before"
1697 " changing mode to %s\n", mode2Nam(mode
));
1701 if (!datalink_SetMode(dl
, mode
))
1704 if (mode
== PHYS_AUTO
&& !(bundle
->phys_type
.all
& PHYS_AUTO
) &&
1705 bundle
->phase
!= PHASE_NETWORK
)
1706 /* First auto link, we need an interface */
1707 ipcp_InterfaceUp(&bundle
->ncp
.ipcp
);
1709 /* Regenerate phys_type and adjust idle timer */
1710 bundle_LinksRemoved(bundle
);
1716 bundle_setsid(struct bundle
*bundle
, int holdsession
)
1719 * Lose the current session. This means getting rid of our pid
1720 * too so that the tty device will really go away, and any getty
1721 * etc will be allowed to restart.
1726 struct datalink
*dl
;
1728 if (!holdsession
&& bundle_IsDead(bundle
)) {
1730 * No need to lose our session after all... we're going away anyway
1732 * We should really stop the timer and pause if holdsession is set and
1733 * the bundle's dead, but that leaves other resources lying about :-(
1739 if (pipe(fds
) == -1) {
1740 log_Printf(LogERROR
, "pipe: %s\n", strerror(errno
));
1743 switch ((pid
= fork())) {
1745 log_Printf(LogERROR
, "fork: %s\n", strerror(errno
));
1751 read(fds
[0], &done
, 1); /* uu_locks are mine ! */
1753 if (pipe(fds
) == -1) {
1754 log_Printf(LogERROR
, "pipe(2): %s\n", strerror(errno
));
1757 switch ((pid
= fork())) {
1759 log_Printf(LogERROR
, "fork(2): %s\n", strerror(errno
));
1765 bundle_LockTun(bundle
); /* update pid */
1766 read(fds
[0], &done
, 1); /* uu_locks are mine ! */
1769 bundle_ChangedPID(bundle
);
1770 log_Printf(LogDEBUG
, "%ld -> %ld: %s session control\n",
1771 (long)orig
, (long)getpid(),
1772 holdsession
? "Passed" : "Dropped");
1773 timer_InitService(0); /* Start the Timer Service */
1777 /* Give away all our physical locks (to the final process) */
1778 for (dl
= bundle
->links
; dl
; dl
= dl
->next
)
1779 if (dl
->state
!= DATALINK_CLOSED
)
1780 physical_ChangedPid(dl
->physical
, pid
);
1781 write(fds
[1], "!", 1); /* done */
1789 /* Give away all our physical locks (to the intermediate process) */
1790 for (dl
= bundle
->links
; dl
; dl
= dl
->next
)
1791 if (dl
->state
!= DATALINK_CLOSED
)
1792 physical_ChangedPid(dl
->physical
, pid
);
1793 write(fds
[1], "!", 1); /* done */
1798 timer_TermService();
1799 signal(SIGPIPE
, SIG_DFL
);
1800 signal(SIGALRM
, SIG_DFL
);
1801 signal(SIGHUP
, SIG_DFL
);
1802 signal(SIGTERM
, SIG_DFL
);
1803 signal(SIGINT
, SIG_DFL
);
1804 signal(SIGQUIT
, SIG_DFL
);
1805 for (fd
= getdtablesize(); fd
>= 0; fd
--)
1808 * Reap the intermediate process. As we're not exiting but the
1809 * intermediate is, we don't want it to become defunct.
1811 waitpid(pid
, &status
, 0);
1812 /* Tweak our process arguments.... */
1813 SetTitle("session owner");
1815 setuid(ID0realuid());
1818 * Hang around for a HUP. This should happen as soon as the
1819 * ppp that we passed our ctty descriptor to closes it.
1820 * NOTE: If this process dies, the passed descriptor becomes
1821 * invalid and will give a select() error by setting one
1822 * of the error fds, aborting the other ppp. We don't
1823 * want that to happen !
1833 bundle_HighestState(struct bundle
*bundle
)
1835 struct datalink
*dl
;
1836 unsigned result
= DATALINK_CLOSED
;
1838 for (dl
= bundle
->links
; dl
; dl
= dl
->next
)
1839 if (result
< dl
->state
)
1846 bundle_Exception(struct bundle
*bundle
, int fd
)
1848 struct datalink
*dl
;
1850 for (dl
= bundle
->links
; dl
; dl
= dl
->next
)
1851 if (dl
->physical
->fd
== fd
) {
1852 datalink_Down(dl
, CLOSE_NORMAL
);
1860 bundle_AdjustFilters(struct bundle
*bundle
, struct ncpaddr
*local
,
1861 struct ncpaddr
*remote
)
1863 filter_AdjustAddr(&bundle
->filter
.in
, local
, remote
, NULL
);
1864 filter_AdjustAddr(&bundle
->filter
.out
, local
, remote
, NULL
);
1865 filter_AdjustAddr(&bundle
->filter
.dial
, local
, remote
, NULL
);
1866 filter_AdjustAddr(&bundle
->filter
.alive
, local
, remote
, NULL
);
1870 bundle_AdjustDNS(struct bundle
*bundle
)
1872 struct in_addr
*dns
= bundle
->ncp
.ipcp
.ns
.dns
;
1874 filter_AdjustAddr(&bundle
->filter
.in
, NULL
, NULL
, dns
);
1875 filter_AdjustAddr(&bundle
->filter
.out
, NULL
, NULL
, dns
);
1876 filter_AdjustAddr(&bundle
->filter
.dial
, NULL
, NULL
, dns
);
1877 filter_AdjustAddr(&bundle
->filter
.alive
, NULL
, NULL
, dns
);
1881 bundle_CalculateBandwidth(struct bundle
*bundle
)
1883 struct datalink
*dl
;
1884 int sp
, overhead
, maxoverhead
;
1886 bundle
->bandwidth
= 0;
1887 bundle
->iface
->mtu
= 0;
1890 for (dl
= bundle
->links
; dl
; dl
= dl
->next
) {
1891 overhead
= ccp_MTUOverhead(&dl
->physical
->link
.ccp
);
1892 if (maxoverhead
< overhead
)
1893 maxoverhead
= overhead
;
1894 if (dl
->state
== DATALINK_OPEN
) {
1895 if ((sp
= dl
->mp
.bandwidth
) == 0 &&
1896 (sp
= physical_GetSpeed(dl
->physical
)) == 0)
1897 log_Printf(LogDEBUG
, "%s: %s: Cannot determine bandwidth\n",
1898 dl
->name
, dl
->physical
->name
.full
);
1900 bundle
->bandwidth
+= sp
;
1901 if (!bundle
->ncp
.mp
.active
) {
1902 bundle
->iface
->mtu
= dl
->physical
->link
.lcp
.his_mru
;
1908 if (bundle
->bandwidth
== 0)
1909 bundle
->bandwidth
= 115200; /* Shrug */
1911 if (bundle
->ncp
.mp
.active
) {
1912 bundle
->iface
->mtu
= bundle
->ncp
.mp
.peer_mrru
;
1913 overhead
= ccp_MTUOverhead(&bundle
->ncp
.mp
.link
.ccp
);
1914 if (maxoverhead
< overhead
)
1915 maxoverhead
= overhead
;
1916 } else if (!bundle
->iface
->mtu
)
1917 bundle
->iface
->mtu
= DEF_MRU
;
1920 if (bundle
->radius
.valid
&& bundle
->radius
.mtu
&&
1921 bundle
->radius
.mtu
< bundle
->iface
->mtu
) {
1922 log_Printf(LogLCP
, "Reducing MTU to radius value %lu\n",
1923 bundle
->radius
.mtu
);
1924 bundle
->iface
->mtu
= bundle
->radius
.mtu
;
1929 log_Printf(LogLCP
, "Reducing MTU from %lu to %lu (CCP requirement)\n",
1930 bundle
->iface
->mtu
, bundle
->iface
->mtu
- maxoverhead
);
1931 bundle
->iface
->mtu
-= maxoverhead
;
1934 tun_configure(bundle
);
1936 route_UpdateMTU(bundle
);
1940 bundle_AutoAdjust(struct bundle
*bundle
, int percent
, int what
)
1942 struct datalink
*dl
, *choice
, *otherlinkup
;
1944 choice
= otherlinkup
= NULL
;
1945 for (dl
= bundle
->links
; dl
; dl
= dl
->next
)
1946 if (dl
->physical
->type
== PHYS_AUTO
) {
1947 if (dl
->state
== DATALINK_OPEN
) {
1948 if (what
== AUTO_DOWN
) {
1950 otherlinkup
= choice
;
1953 } else if (dl
->state
== DATALINK_CLOSED
) {
1954 if (what
== AUTO_UP
) {
1959 /* An auto link in an intermediate state - forget it for the moment */
1963 } else if (dl
->state
== DATALINK_OPEN
&& what
== AUTO_DOWN
)
1967 if (what
== AUTO_UP
) {
1968 log_Printf(LogPHASE
, "%d%% saturation -> Opening link ``%s''\n",
1969 percent
, choice
->name
);
1970 datalink_Up(choice
, 1, 1);
1971 mp_CheckAutoloadTimer(&bundle
->ncp
.mp
);
1972 } else if (otherlinkup
) { /* Only bring the second-last link down */
1973 log_Printf(LogPHASE
, "%d%% saturation -> Closing link ``%s''\n",
1974 percent
, choice
->name
);
1975 datalink_Close(choice
, CLOSE_STAYDOWN
);
1976 mp_CheckAutoloadTimer(&bundle
->ncp
.mp
);
1982 bundle_WantAutoloadTimer(struct bundle
*bundle
)
1984 struct datalink
*dl
;
1985 int autolink
, opened
;
1987 if (bundle
->phase
== PHASE_NETWORK
) {
1988 for (autolink
= opened
= 0, dl
= bundle
->links
; dl
; dl
= dl
->next
)
1989 if (dl
->physical
->type
== PHYS_AUTO
) {
1990 if (++autolink
== 2 || (autolink
== 1 && opened
))
1991 /* Two auto links or one auto and one open in NETWORK phase */
1993 } else if (dl
->state
== DATALINK_OPEN
) {
1996 /* One auto and one open link in NETWORK phase */
2005 bundle_ChangedPID(struct bundle
*bundle
)
2008 ioctl(bundle
->dev
.fd
, TUNSIFPID
, 0);
2013 bundle_Uptime(struct bundle
*bundle
)
2016 return time(NULL
) - bundle
->upat
;