Deleted Added
full compact
bundle.c (36452) bundle.c (36465)
1/*-
2 * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 9 unchanged lines hidden (view full) ---

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
24 * SUCH DAMAGE.
25 *
1/*-
2 * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 9 unchanged lines hidden (view full) ---

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
24 * SUCH DAMAGE.
25 *
26 * $Id: bundle.c,v 1.9 1998/05/28 23:15:29 brian Exp $
26 * $Id: bundle.c,v 1.10 1998/05/28 23:17:31 brian Exp $
27 */
28
29#include <sys/param.h>
30#include <sys/socket.h>
31#include <netinet/in.h>
32#include <net/if.h>
33#include <arpa/inet.h>
34#include <net/route.h>

--- 176 unchanged lines hidden (view full) ---

211static void
212bundle_AutoLoadTimeout(void *v)
213{
214 struct bundle *bundle = (struct bundle *)v;
215
216 if (bundle->autoload.comingup) {
217 log_Printf(LogPHASE, "autoload: Another link is required\n");
218 /* bundle_Open() stops the timer */
27 */
28
29#include <sys/param.h>
30#include <sys/socket.h>
31#include <netinet/in.h>
32#include <net/if.h>
33#include <arpa/inet.h>
34#include <net/route.h>

--- 176 unchanged lines hidden (view full) ---

211static void
212bundle_AutoLoadTimeout(void *v)
213{
214 struct bundle *bundle = (struct bundle *)v;
215
216 if (bundle->autoload.comingup) {
217 log_Printf(LogPHASE, "autoload: Another link is required\n");
218 /* bundle_Open() stops the timer */
219 bundle_Open(bundle, NULL, PHYS_DEMAND);
219 bundle_Open(bundle, NULL, PHYS_AUTO);
220 } else {
221 struct datalink *dl, *last;
222
223 timer_Stop(&bundle->autoload.timer);
224 for (last = NULL, dl = bundle->links; dl; dl = dl->next)
220 } else {
221 struct datalink *dl, *last;
222
223 timer_Stop(&bundle->autoload.timer);
224 for (last = NULL, dl = bundle->links; dl; dl = dl->next)
225 if (dl->physical->type == PHYS_DEMAND && dl->state == DATALINK_OPEN)
225 if (dl->physical->type == PHYS_AUTO && dl->state == DATALINK_OPEN)
226 last = dl;
227
228 if (last)
229 datalink_Close(last, 1);
230 }
231}
232
233static void
234bundle_StartAutoLoadTimer(struct bundle *bundle, int up)
235{
236 struct datalink *dl;
237
238 timer_Stop(&bundle->autoload.timer);
239
240 if (bundle->CleaningUp || bundle->phase != PHASE_NETWORK) {
241 dl = NULL;
242 bundle->autoload.running = 0;
243 } else if (up) {
244 for (dl = bundle->links; dl; dl = dl->next)
226 last = dl;
227
228 if (last)
229 datalink_Close(last, 1);
230 }
231}
232
233static void
234bundle_StartAutoLoadTimer(struct bundle *bundle, int up)
235{
236 struct datalink *dl;
237
238 timer_Stop(&bundle->autoload.timer);
239
240 if (bundle->CleaningUp || bundle->phase != PHASE_NETWORK) {
241 dl = NULL;
242 bundle->autoload.running = 0;
243 } else if (up) {
244 for (dl = bundle->links; dl; dl = dl->next)
245 if (dl->state == DATALINK_CLOSED && dl->physical->type == PHYS_DEMAND) {
245 if (dl->state == DATALINK_CLOSED && dl->physical->type == PHYS_AUTO) {
246 if (bundle->cfg.autoload.max.timeout) {
247 bundle->autoload.timer.func = bundle_AutoLoadTimeout;
248 bundle->autoload.timer.name = "autoload up";
249 bundle->autoload.timer.load =
250 bundle->cfg.autoload.max.timeout * SECTICKS;
251 bundle->autoload.timer.arg = bundle;
252 timer_Start(&bundle->autoload.timer);
253 bundle->autoload.done = time(NULL) + bundle->cfg.autoload.max.timeout;
254 } else
255 bundle_AutoLoadTimeout(bundle);
256 break;
257 }
258 bundle->autoload.running = (dl || bundle->cfg.autoload.min.timeout) ? 1 : 0;
259 } else {
260 int nlinks;
261 struct datalink *adl;
262
263 for (nlinks = 0, adl = NULL, dl = bundle->links; dl; dl = dl->next)
264 if (dl->state == DATALINK_OPEN) {
246 if (bundle->cfg.autoload.max.timeout) {
247 bundle->autoload.timer.func = bundle_AutoLoadTimeout;
248 bundle->autoload.timer.name = "autoload up";
249 bundle->autoload.timer.load =
250 bundle->cfg.autoload.max.timeout * SECTICKS;
251 bundle->autoload.timer.arg = bundle;
252 timer_Start(&bundle->autoload.timer);
253 bundle->autoload.done = time(NULL) + bundle->cfg.autoload.max.timeout;
254 } else
255 bundle_AutoLoadTimeout(bundle);
256 break;
257 }
258 bundle->autoload.running = (dl || bundle->cfg.autoload.min.timeout) ? 1 : 0;
259 } else {
260 int nlinks;
261 struct datalink *adl;
262
263 for (nlinks = 0, adl = NULL, dl = bundle->links; dl; dl = dl->next)
264 if (dl->state == DATALINK_OPEN) {
265 if (dl->physical->type == PHYS_DEMAND)
265 if (dl->physical->type == PHYS_AUTO)
266 adl = dl;
267 if (++nlinks > 1 && adl) {
268 if (bundle->cfg.autoload.min.timeout) {
269 bundle->autoload.timer.func = bundle_AutoLoadTimeout;
270 bundle->autoload.timer.name = "autoload down";
271 bundle->autoload.timer.load =
272 bundle->cfg.autoload.min.timeout * SECTICKS;
273 bundle->autoload.timer.arg = bundle;

--- 225 unchanged lines hidden (view full) ---

499 bundle->autoload.comingup)
500 bundle_StartAutoLoadTimer(bundle, 0);
501 } else if (bundle->autoload.timer.state != TIMER_RUNNING ||
502 !bundle->autoload.comingup)
503 bundle_StartAutoLoadTimer(bundle, 1);
504 }
505
506 if (r &&
266 adl = dl;
267 if (++nlinks > 1 && adl) {
268 if (bundle->cfg.autoload.min.timeout) {
269 bundle->autoload.timer.func = bundle_AutoLoadTimeout;
270 bundle->autoload.timer.name = "autoload down";
271 bundle->autoload.timer.load =
272 bundle->cfg.autoload.min.timeout * SECTICKS;
273 bundle->autoload.timer.arg = bundle;

--- 225 unchanged lines hidden (view full) ---

499 bundle->autoload.comingup)
500 bundle_StartAutoLoadTimer(bundle, 0);
501 } else if (bundle->autoload.timer.state != TIMER_RUNNING ||
502 !bundle->autoload.comingup)
503 bundle_StartAutoLoadTimer(bundle, 1);
504 }
505
506 if (r &&
507 (bundle->phase == PHASE_NETWORK || bundle->phys_type & PHYS_DEMAND)) {
507 (bundle->phase == PHASE_NETWORK || bundle->phys_type & PHYS_AUTO)) {
508 /* enough surplus so that we can tell if we're getting swamped */
509 want = bundle->cfg.autoload.max.packets + nlinks * 2;
510 /* but at least 20 packets ! */
511 if (want < 20)
512 want = 20;
513 if (queued < want) {
514 /* Not enough - select() for more */
515 FD_SET(bundle->dev.fd, r);

--- 92 unchanged lines hidden (view full) ---

608 */
609
610 if (bundle_Phase(bundle) == PHASE_DEAD) {
611 /*
612 * Note, we must be in AUTO mode :-/ otherwise our interface should
613 * *not* be UP and we can't receive data
614 */
615 if ((pri = PacketCheck(bundle, tun.data, n, &bundle->filter.dial)) >= 0)
508 /* enough surplus so that we can tell if we're getting swamped */
509 want = bundle->cfg.autoload.max.packets + nlinks * 2;
510 /* but at least 20 packets ! */
511 if (want < 20)
512 want = 20;
513 if (queued < want) {
514 /* Not enough - select() for more */
515 FD_SET(bundle->dev.fd, r);

--- 92 unchanged lines hidden (view full) ---

608 */
609
610 if (bundle_Phase(bundle) == PHASE_DEAD) {
611 /*
612 * Note, we must be in AUTO mode :-/ otherwise our interface should
613 * *not* be UP and we can't receive data
614 */
615 if ((pri = PacketCheck(bundle, tun.data, n, &bundle->filter.dial)) >= 0)
616 bundle_Open(bundle, NULL, PHYS_DEMAND);
616 bundle_Open(bundle, NULL, PHYS_AUTO);
617 else
618 /*
619 * Drop the packet. If we were to queue it, we'd just end up with
620 * a pile of timed-out data in our output queue by the time we get
621 * around to actually dialing. We'd also prematurely reach the
622 * threshold at which we stop select()ing to read() the tun
623 * device - breaking auto-dial.
624 */

--- 395 unchanged lines hidden (view full) ---

1020}
1021
1022void
1023bundle_LinkClosed(struct bundle *bundle, struct datalink *dl)
1024{
1025 /*
1026 * Our datalink has closed.
1027 * CleanDatalinks() (called from DoLoop()) will remove closed
617 else
618 /*
619 * Drop the packet. If we were to queue it, we'd just end up with
620 * a pile of timed-out data in our output queue by the time we get
621 * around to actually dialing. We'd also prematurely reach the
622 * threshold at which we stop select()ing to read() the tun
623 * device - breaking auto-dial.
624 */

--- 395 unchanged lines hidden (view full) ---

1020}
1021
1022void
1023bundle_LinkClosed(struct bundle *bundle, struct datalink *dl)
1024{
1025 /*
1026 * Our datalink has closed.
1027 * CleanDatalinks() (called from DoLoop()) will remove closed
1028 * 1OFF and DIRECT links.
1028 * BACKGROUND and DIRECT links.
1029 * If it's the last data link, enter phase DEAD.
1030 *
1031 * NOTE: dl may not be in our list (bundle_SendDatalink()) !
1032 */
1033
1034 struct datalink *odl;
1035 int other_links;
1036
1037 other_links = 0;
1038 for (odl = bundle->links; odl; odl = odl->next)
1039 if (odl != dl && odl->state != DATALINK_CLOSED)
1040 other_links++;
1041
1042 if (!other_links) {
1029 * If it's the last data link, enter phase DEAD.
1030 *
1031 * NOTE: dl may not be in our list (bundle_SendDatalink()) !
1032 */
1033
1034 struct datalink *odl;
1035 int other_links;
1036
1037 other_links = 0;
1038 for (odl = bundle->links; odl; odl = odl->next)
1039 if (odl != dl && odl->state != DATALINK_CLOSED)
1040 other_links++;
1041
1042 if (!other_links) {
1043 if (dl->physical->type != PHYS_DEMAND) /* Not in -auto mode */
1043 if (dl->physical->type != PHYS_AUTO) /* Not in -auto mode */
1044 bundle_DownInterface(bundle);
1045 if (bundle->ncp.ipcp.fsm.state > ST_CLOSED ||
1046 bundle->ncp.ipcp.fsm.state == ST_STARTING) {
1047 fsm_Down(&bundle->ncp.ipcp.fsm);
1048 fsm_Close(&bundle->ncp.ipcp.fsm); /* ST_INITIAL please */
1049 }
1050 bundle_NewPhase(bundle, PHASE_DEAD);
1051 bundle_StopIdleTimer(bundle);

--- 11 unchanged lines hidden (view full) ---

1063 */
1064 struct datalink *dl;
1065
1066 timer_Stop(&bundle->autoload.timer);
1067 for (dl = bundle->links; dl; dl = dl->next)
1068 if (name == NULL || !strcasecmp(dl->name, name)) {
1069 if (dl->state == DATALINK_CLOSED && (mask & dl->physical->type)) {
1070 datalink_Up(dl, 1, 1);
1044 bundle_DownInterface(bundle);
1045 if (bundle->ncp.ipcp.fsm.state > ST_CLOSED ||
1046 bundle->ncp.ipcp.fsm.state == ST_STARTING) {
1047 fsm_Down(&bundle->ncp.ipcp.fsm);
1048 fsm_Close(&bundle->ncp.ipcp.fsm); /* ST_INITIAL please */
1049 }
1050 bundle_NewPhase(bundle, PHASE_DEAD);
1051 bundle_StopIdleTimer(bundle);

--- 11 unchanged lines hidden (view full) ---

1063 */
1064 struct datalink *dl;
1065
1066 timer_Stop(&bundle->autoload.timer);
1067 for (dl = bundle->links; dl; dl = dl->next)
1068 if (name == NULL || !strcasecmp(dl->name, name)) {
1069 if (dl->state == DATALINK_CLOSED && (mask & dl->physical->type)) {
1070 datalink_Up(dl, 1, 1);
1071 if (mask == PHYS_DEMAND)
1072 /* Only one DEMAND link at a time (see the AutoLoad timer) */
1071 if (mask == PHYS_AUTO)
1072 /* Only one AUTO link at a time (see the AutoLoad timer) */
1073 break;
1074 }
1075 if (name != NULL)
1076 break;
1077 }
1078}
1079
1080struct datalink *

--- 134 unchanged lines hidden (view full) ---

1215/*
1216 * Start Idle timer. If timeout is reached, we call bundle_Close() to
1217 * close LCP and link.
1218 */
1219void
1220bundle_StartIdleTimer(struct bundle *bundle)
1221{
1222 timer_Stop(&bundle->idle.timer);
1073 break;
1074 }
1075 if (name != NULL)
1076 break;
1077 }
1078}
1079
1080struct datalink *

--- 134 unchanged lines hidden (view full) ---

1215/*
1216 * Start Idle timer. If timeout is reached, we call bundle_Close() to
1217 * close LCP and link.
1218 */
1219void
1220bundle_StartIdleTimer(struct bundle *bundle)
1221{
1222 timer_Stop(&bundle->idle.timer);
1223 if ((bundle->phys_type & (PHYS_DEDICATED|PHYS_PERM)) != bundle->phys_type &&
1223 if ((bundle->phys_type & (PHYS_DEDICATED|PHYS_DDIAL)) != bundle->phys_type &&
1224 bundle->cfg.idle_timeout) {
1225 bundle->idle.timer.func = bundle_IdleTimeout;
1226 bundle->idle.timer.name = "idle";
1227 bundle->idle.timer.load = bundle->cfg.idle_timeout * SECTICKS;
1228 bundle->idle.timer.arg = bundle;
1229 timer_Start(&bundle->idle.timer);
1230 bundle->idle.done = time(NULL) + bundle->cfg.idle_timeout;
1231 }

--- 27 unchanged lines hidden (view full) ---

1259{
1260 return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp);
1261}
1262
1263static void
1264bundle_LinkAdded(struct bundle *bundle, struct datalink *dl)
1265{
1266 bundle->phys_type |= dl->physical->type;
1224 bundle->cfg.idle_timeout) {
1225 bundle->idle.timer.func = bundle_IdleTimeout;
1226 bundle->idle.timer.name = "idle";
1227 bundle->idle.timer.load = bundle->cfg.idle_timeout * SECTICKS;
1228 bundle->idle.timer.arg = bundle;
1229 timer_Start(&bundle->idle.timer);
1230 bundle->idle.done = time(NULL) + bundle->cfg.idle_timeout;
1231 }

--- 27 unchanged lines hidden (view full) ---

1259{
1260 return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp);
1261}
1262
1263static void
1264bundle_LinkAdded(struct bundle *bundle, struct datalink *dl)
1265{
1266 bundle->phys_type |= dl->physical->type;
1267 if (dl->physical->type == PHYS_DEMAND &&
1267 if (dl->physical->type == PHYS_AUTO &&
1268 bundle->autoload.timer.state == TIMER_STOPPED &&
1269 bundle->phase == PHASE_NETWORK)
1270 bundle->autoload.running = 1;
1271}
1272
1273static void
1274bundle_LinksRemoved(struct bundle *bundle)
1275{
1276 struct datalink *dl;
1277
1278 bundle->phys_type = 0;
1279 for (dl = bundle->links; dl; dl = dl->next)
1280 bundle_LinkAdded(bundle, dl);
1281
1268 bundle->autoload.timer.state == TIMER_STOPPED &&
1269 bundle->phase == PHASE_NETWORK)
1270 bundle->autoload.running = 1;
1271}
1272
1273static void
1274bundle_LinksRemoved(struct bundle *bundle)
1275{
1276 struct datalink *dl;
1277
1278 bundle->phys_type = 0;
1279 for (dl = bundle->links; dl; dl = dl->next)
1280 bundle_LinkAdded(bundle, dl);
1281
1282 if ((bundle->phys_type & (PHYS_DEDICATED|PHYS_PERM)) == bundle->phys_type)
1282 if ((bundle->phys_type & (PHYS_DEDICATED|PHYS_DDIAL)) == bundle->phys_type)
1283 timer_Stop(&bundle->idle.timer);
1284}
1285
1286static struct datalink *
1287bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl)
1288{
1289 struct datalink **dlp;
1290

--- 25 unchanged lines hidden (view full) ---

1316void
1317bundle_CleanDatalinks(struct bundle *bundle)
1318{
1319 struct datalink **dlp = &bundle->links;
1320 int found = 0;
1321
1322 while (*dlp)
1323 if ((*dlp)->state == DATALINK_CLOSED &&
1283 timer_Stop(&bundle->idle.timer);
1284}
1285
1286static struct datalink *
1287bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl)
1288{
1289 struct datalink **dlp;
1290

--- 25 unchanged lines hidden (view full) ---

1316void
1317bundle_CleanDatalinks(struct bundle *bundle)
1318{
1319 struct datalink **dlp = &bundle->links;
1320 int found = 0;
1321
1322 while (*dlp)
1323 if ((*dlp)->state == DATALINK_CLOSED &&
1324 (*dlp)->physical->type & (PHYS_DIRECT|PHYS_1OFF)) {
1324 (*dlp)->physical->type & (PHYS_DIRECT|PHYS_BACKGROUND)) {
1325 *dlp = datalink_Destroy(*dlp);
1326 found++;
1327 } else
1328 dlp = &(*dlp)->next;
1329
1330 if (found)
1331 bundle_LinksRemoved(bundle);
1332}

--- 206 unchanged lines hidden (view full) ---

1539bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode)
1540{
1541 int omode;
1542
1543 omode = dl->physical->type;
1544 if (omode == mode)
1545 return 1;
1546
1325 *dlp = datalink_Destroy(*dlp);
1326 found++;
1327 } else
1328 dlp = &(*dlp)->next;
1329
1330 if (found)
1331 bundle_LinksRemoved(bundle);
1332}

--- 206 unchanged lines hidden (view full) ---

1539bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode)
1540{
1541 int omode;
1542
1543 omode = dl->physical->type;
1544 if (omode == mode)
1545 return 1;
1546
1547 if (mode == PHYS_DEMAND && !(bundle->phys_type & PHYS_DEMAND))
1547 if (mode == PHYS_AUTO && !(bundle->phys_type & PHYS_AUTO))
1548 /* Changing to demand-dial mode */
1549 if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) {
1550 log_Printf(LogWARN, "You must `set ifaddr' before changing mode to %s\n",
1551 mode2Nam(mode));
1552 return 0;
1553 }
1554
1555 if (!datalink_SetMode(dl, mode))
1556 return 0;
1557
1548 /* Changing to demand-dial mode */
1549 if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) {
1550 log_Printf(LogWARN, "You must `set ifaddr' before changing mode to %s\n",
1551 mode2Nam(mode));
1552 return 0;
1553 }
1554
1555 if (!datalink_SetMode(dl, mode))
1556 return 0;
1557
1558 if (mode == PHYS_DEMAND && !(bundle->phys_type & PHYS_DEMAND))
1558 if (mode == PHYS_AUTO && !(bundle->phys_type & PHYS_AUTO))
1559 ipcp_InterfaceUp(&bundle->ncp.ipcp);
1560
1561 /* Regenerate phys_type and adjust autoload & idle timers */
1562 bundle_LinksRemoved(bundle);
1563
1559 ipcp_InterfaceUp(&bundle->ncp.ipcp);
1560
1561 /* Regenerate phys_type and adjust autoload & idle timers */
1562 bundle_LinksRemoved(bundle);
1563
1564 if (omode == PHYS_DEMAND && !(bundle->phys_type & PHYS_DEMAND))
1564 if (omode == PHYS_AUTO && !(bundle->phys_type & PHYS_AUTO))
1565 /* Changing from demand-dial mode */
1566 ipcp_CleanInterface(&bundle->ncp.ipcp);
1567
1568 return 1;
1569}
1570
1571void
1572bundle_setsid(struct bundle *bundle, int holdsession)

--- 98 unchanged lines hidden ---
1565 /* Changing from demand-dial mode */
1566 ipcp_CleanInterface(&bundle->ncp.ipcp);
1567
1568 return 1;
1569}
1570
1571void
1572bundle_setsid(struct bundle *bundle, int holdsession)

--- 98 unchanged lines hidden ---