139 MPPE_MasterKeyValid = 0; 140#endif 141 log_DisplayPrompts(); 142 break; 143 144 case PHASE_ESTABLISH: 145 bundle->phase = new; 146 break; 147 148 case PHASE_AUTHENTICATE: 149 bundle->phase = new; 150 log_DisplayPrompts(); 151 break; 152 153 case PHASE_NETWORK: 154 if (ncp_fsmStart(&bundle->ncp, bundle)) { 155 bundle->phase = new; 156 log_DisplayPrompts(); 157 } else { 158 log_Printf(LogPHASE, "bundle: All NCPs are disabled\n"); 159 bundle_Close(bundle, NULL, CLOSE_STAYDOWN); 160 } 161 break; 162 163 case PHASE_TERMINATE: 164 bundle->phase = new; 165 mp_Down(&bundle->ncp.mp); 166 log_DisplayPrompts(); 167 break; 168 } 169} 170 171static void 172bundle_LayerStart(void *v, struct fsm *fp) 173{ 174 /* The given FSM is about to start up ! */ 175} 176 177 178void 179bundle_Notify(struct bundle *bundle, char c) 180{ 181 if (bundle->notify.fd != -1) { 182 int ret; 183 184 ret = write(bundle->notify.fd, &c, 1); 185 if (c != EX_REDIAL && c != EX_RECONNECT) { 186 if (ret == 1) 187 log_Printf(LogCHAT, "Parent notified of %s\n", 188 c == EX_NORMAL ? "success" : "failure"); 189 else 190 log_Printf(LogERROR, "Failed to notify parent of success\n"); 191 close(bundle->notify.fd); 192 bundle->notify.fd = -1; 193 } else if (ret == 1) 194 log_Printf(LogCHAT, "Parent notified of %s\n", ex_desc(c)); 195 else 196 log_Printf(LogERROR, "Failed to notify parent of %s\n", ex_desc(c)); 197 } 198} 199 200static void 201bundle_ClearQueues(void *v) 202{ 203 struct bundle *bundle = (struct bundle *)v; 204 struct datalink *dl; 205 206 log_Printf(LogPHASE, "Clearing choked output queue\n"); 207 timer_Stop(&bundle->choked.timer); 208 209 /* 210 * Emergency time: 211 * 212 * We've had a full queue for PACKET_DEL_SECS seconds without being 213 * able to get rid of any of the packets. We've probably given up 214 * on the redials at this point, and the queued data has almost 215 * definitely been timed out by the layer above. As this is preventing 216 * us from reading the TUN_NAME device (we don't want to buffer stuff 217 * indefinitely), we may as well nuke this data and start with a clean 218 * slate ! 219 * 220 * Unfortunately, this has the side effect of shafting any compression 221 * dictionaries in use (causing the relevant RESET_REQ/RESET_ACK). 222 */ 223 224 ncp_DeleteQueues(&bundle->ncp); 225 for (dl = bundle->links; dl; dl = dl->next) 226 physical_DeleteQueue(dl->physical); 227} 228 229static void 230bundle_LinkAdded(struct bundle *bundle, struct datalink *dl) 231{ 232 bundle->phys_type.all |= dl->physical->type; 233 if (dl->state == DATALINK_OPEN) 234 bundle->phys_type.open |= dl->physical->type; 235 236 if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) 237 != bundle->phys_type.open && bundle->idle.timer.state == TIMER_STOPPED) 238 /* We may need to start our idle timer */ 239 bundle_StartIdleTimer(bundle, 0); 240} 241 242void 243bundle_LinksRemoved(struct bundle *bundle) 244{ 245 struct datalink *dl; 246 247 bundle->phys_type.all = bundle->phys_type.open = 0; 248 for (dl = bundle->links; dl; dl = dl->next) 249 bundle_LinkAdded(bundle, dl); 250 251 bundle_CalculateBandwidth(bundle); 252 mp_CheckAutoloadTimer(&bundle->ncp.mp); 253 254 if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) 255 == bundle->phys_type.open) 256 bundle_StopIdleTimer(bundle); 257} 258 259static void 260bundle_LayerUp(void *v, struct fsm *fp) 261{ 262 /* 263 * The given fsm is now up 264 * If it's an LCP, adjust our phys_mode.open value and check the 265 * autoload timer. 266 * If it's the first NCP, calculate our bandwidth 267 * If it's the first NCP, set our ``upat'' time 268 * If it's the first NCP, start the idle timer. 269 * If it's an NCP, tell our -background parent to go away. 270 * If it's the first NCP, start the autoload timer 271 */ 272 struct bundle *bundle = (struct bundle *)v; 273 274 if (fp->proto == PROTO_LCP) { 275 struct physical *p = link2physical(fp->link); 276 277 bundle_LinkAdded(bundle, p->dl); 278 mp_CheckAutoloadTimer(&bundle->ncp.mp); 279 } else if (isncp(fp->proto)) { 280 if (ncp_LayersOpen(&fp->bundle->ncp) == 1) { 281 bundle_CalculateBandwidth(fp->bundle); 282 time(&bundle->upat); 283 bundle_StartIdleTimer(bundle, 0); 284 mp_CheckAutoloadTimer(&fp->bundle->ncp.mp); 285 } 286 bundle_Notify(bundle, EX_NORMAL); 287 } else if (fp->proto == PROTO_CCP) 288 bundle_CalculateBandwidth(fp->bundle); /* Against ccp_MTUOverhead */ 289} 290 291static void 292bundle_LayerDown(void *v, struct fsm *fp) 293{ 294 /* 295 * The given FSM has been told to come down. 296 * If it's our last NCP, stop the idle timer. 297 * If it's our last NCP, clear our ``upat'' value. 298 * If it's our last NCP, stop the autoload timer 299 * If it's an LCP, adjust our phys_type.open value and any timers. 300 * If it's an LCP and we're in multilink mode, adjust our tun 301 * If it's the last LCP, down all NCPs 302 * speed and make sure our minimum sequence number is adjusted. 303 */ 304 305 struct bundle *bundle = (struct bundle *)v; 306 307 if (isncp(fp->proto)) { 308 if (ncp_LayersOpen(&fp->bundle->ncp) == 0) { 309 bundle_StopIdleTimer(bundle); 310 bundle->upat = 0; 311 mp_StopAutoloadTimer(&bundle->ncp.mp); 312 } 313 } else if (fp->proto == PROTO_LCP) { 314 struct datalink *dl; 315 struct datalink *lost; 316 int others_active; 317 318 bundle_LinksRemoved(bundle); /* adjust timers & phys_type values */ 319 320 lost = NULL; 321 others_active = 0; 322 for (dl = bundle->links; dl; dl = dl->next) { 323 if (fp == &dl->physical->link.lcp.fsm) 324 lost = dl; 325 else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 326 others_active++; 327 } 328 329 if (bundle->ncp.mp.active) { 330 bundle_CalculateBandwidth(bundle); 331 332 if (lost) 333 mp_LinkLost(&bundle->ncp.mp, lost); 334 else 335 log_Printf(LogALERT, "Oops, lost an unrecognised datalink (%s) !\n", 336 fp->link->name); 337 } 338 339 if (!others_active) 340 /* Down the NCPs. We don't expect to get fsm_Close()d ourself ! */ 341 ncp2initial(&bundle->ncp); 342 } 343} 344 345static void 346bundle_LayerFinish(void *v, struct fsm *fp) 347{ 348 /* The given fsm is now down (fp cannot be NULL) 349 * 350 * If it's the last NCP, fsm_Close all LCPs 351 */ 352 353 struct bundle *bundle = (struct bundle *)v; 354 struct datalink *dl; 355 356 if (isncp(fp->proto) && !ncp_LayersUnfinished(&bundle->ncp)) { 357 if (bundle_Phase(bundle) != PHASE_DEAD) 358 bundle_NewPhase(bundle, PHASE_TERMINATE); 359 for (dl = bundle->links; dl; dl = dl->next) 360 if (dl->state == DATALINK_OPEN) 361 datalink_Close(dl, CLOSE_STAYDOWN); 362 fsm2initial(fp); 363 } 364} 365 366void 367bundle_Close(struct bundle *bundle, const char *name, int how) 368{ 369 /* 370 * Please close the given datalink. 371 * If name == NULL or name is the last datalink, fsm_Close all NCPs 372 * (except our MP) 373 * If it isn't the last datalink, just Close that datalink. 374 */ 375 376 struct datalink *dl, *this_dl; 377 int others_active; 378 379 others_active = 0; 380 this_dl = NULL; 381 382 for (dl = bundle->links; dl; dl = dl->next) { 383 if (name && !strcasecmp(name, dl->name)) 384 this_dl = dl; 385 if (name == NULL || this_dl == dl) { 386 switch (how) { 387 case CLOSE_LCP: 388 datalink_DontHangup(dl); 389 break; 390 case CLOSE_STAYDOWN: 391 datalink_StayDown(dl); 392 break; 393 } 394 } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 395 others_active++; 396 } 397 398 if (name && this_dl == NULL) { 399 log_Printf(LogWARN, "%s: Invalid datalink name\n", name); 400 return; 401 } 402 403 if (!others_active) { 404 bundle_StopIdleTimer(bundle); 405 if (ncp_LayersUnfinished(&bundle->ncp)) 406 ncp_Close(&bundle->ncp); 407 else { 408 ncp2initial(&bundle->ncp); 409 for (dl = bundle->links; dl; dl = dl->next) 410 datalink_Close(dl, how); 411 } 412 } else if (this_dl && this_dl->state != DATALINK_CLOSED && 413 this_dl->state != DATALINK_HANGUP) 414 datalink_Close(this_dl, how); 415} 416 417void 418bundle_Down(struct bundle *bundle, int how) 419{ 420 struct datalink *dl; 421 422 for (dl = bundle->links; dl; dl = dl->next) 423 datalink_Down(dl, how); 424} 425 426static int 427bundle_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n) 428{ 429 struct bundle *bundle = descriptor2bundle(d); 430 struct datalink *dl; 431 int result, nlinks; 432 u_short ifqueue; 433 size_t queued; 434 435 result = 0; 436 437 /* If there are aren't many packets queued, look for some more. */ 438 for (nlinks = 0, dl = bundle->links; dl; dl = dl->next) 439 nlinks++; 440 441 if (nlinks) { 442 queued = r ? ncp_FillPhysicalQueues(&bundle->ncp, bundle) : 443 ncp_QueueLen(&bundle->ncp); 444 445 if (r && (bundle->phase == PHASE_NETWORK || 446 bundle->phys_type.all & PHYS_AUTO)) { 447 /* enough surplus so that we can tell if we're getting swamped */ 448 ifqueue = nlinks > bundle->cfg.ifqueue ? nlinks : bundle->cfg.ifqueue; 449 if (queued < ifqueue) { 450 /* Not enough - select() for more */ 451 if (bundle->choked.timer.state == TIMER_RUNNING) 452 timer_Stop(&bundle->choked.timer); /* Not needed any more */ 453 FD_SET(bundle->dev.fd, r); 454 if (*n < bundle->dev.fd + 1) 455 *n = bundle->dev.fd + 1; 456 log_Printf(LogTIMER, "%s: fdset(r) %d\n", TUN_NAME, bundle->dev.fd); 457 result++; 458 } else if (bundle->choked.timer.state == TIMER_STOPPED) { 459 bundle->choked.timer.func = bundle_ClearQueues; 460 bundle->choked.timer.name = "output choke"; 461 bundle->choked.timer.load = bundle->cfg.choked.timeout * SECTICKS; 462 bundle->choked.timer.arg = bundle; 463 timer_Start(&bundle->choked.timer); 464 } 465 } 466 } 467 468#ifndef NORADIUS 469 result += descriptor_UpdateSet(&bundle->radius.desc, r, w, e, n); 470#endif 471 472 /* Which links need a select() ? */ 473 for (dl = bundle->links; dl; dl = dl->next) 474 result += descriptor_UpdateSet(&dl->desc, r, w, e, n); 475 476 /* 477 * This *MUST* be called after the datalink UpdateSet()s as it 478 * might be ``holding'' one of the datalinks (death-row) and 479 * wants to be able to de-select() it from the descriptor set. 480 */ 481 result += descriptor_UpdateSet(&bundle->ncp.mp.server.desc, r, w, e, n); 482 483 return result; 484} 485 486static int 487bundle_IsSet(struct fdescriptor *d, const fd_set *fdset) 488{ 489 struct bundle *bundle = descriptor2bundle(d); 490 struct datalink *dl; 491 492 for (dl = bundle->links; dl; dl = dl->next) 493 if (descriptor_IsSet(&dl->desc, fdset)) 494 return 1; 495 496#ifndef NORADIUS 497 if (descriptor_IsSet(&bundle->radius.desc, fdset)) 498 return 1; 499#endif 500 501 if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 502 return 1; 503 504 return FD_ISSET(bundle->dev.fd, fdset); 505} 506 507static void 508bundle_DescriptorRead(struct fdescriptor *d, struct bundle *bundle, 509 const fd_set *fdset) 510{ 511 struct datalink *dl; 512 unsigned secs; 513 u_int32_t af; 514 515 if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 516 descriptor_Read(&bundle->ncp.mp.server.desc, bundle, fdset); 517 518 for (dl = bundle->links; dl; dl = dl->next) 519 if (descriptor_IsSet(&dl->desc, fdset)) 520 descriptor_Read(&dl->desc, bundle, fdset); 521 522#ifndef NORADIUS 523 if (descriptor_IsSet(&bundle->radius.desc, fdset)) 524 descriptor_Read(&bundle->radius.desc, bundle, fdset); 525#endif 526 527 if (FD_ISSET(bundle->dev.fd, fdset)) { 528 struct tun_data tun; 529 int n, pri; 530 char *data; 531 size_t sz; 532 533 if (bundle->dev.header) { 534 data = (char *)&tun; 535 sz = sizeof tun; 536 } else { 537 data = tun.data; 538 sz = sizeof tun.data; 539 } 540 541 /* something to read from tun */ 542 543 n = read(bundle->dev.fd, data, sz); 544 if (n < 0) { 545 log_Printf(LogWARN, "%s: read: %s\n", bundle->dev.Name, strerror(errno)); 546 return; 547 } 548 549 if (bundle->dev.header) { 550 n -= sz - sizeof tun.data; 551 if (n <= 0) { 552 log_Printf(LogERROR, "%s: read: Got only %d bytes of data !\n", 553 bundle->dev.Name, n); 554 return; 555 } 556 af = ntohl(tun.header.family); 557#ifndef NOINET6 558 if (af != AF_INET && af != AF_INET6) 559#else 560 if (af != AF_INET) 561#endif 562 /* XXX: Should be maintaining drop/family counts ! */ 563 return; 564 } else 565 af = AF_INET; 566 567 if (af == AF_INET && ((struct ip *)tun.data)->ip_dst.s_addr == 568 bundle->ncp.ipcp.my_ip.s_addr) { 569 /* we've been asked to send something addressed *to* us :( */ 570 if (Enabled(bundle, OPT_LOOPBACK)) { 571 pri = PacketCheck(bundle, af, tun.data, n, &bundle->filter.in, 572 NULL, NULL); 573 if (pri >= 0) { 574 n += sz - sizeof tun.data; 575 write(bundle->dev.fd, data, n); 576 log_Printf(LogDEBUG, "Looped back packet addressed to myself\n"); 577 } 578 return; 579 } else 580 log_Printf(LogDEBUG, "Oops - forwarding packet addressed to myself\n"); 581 } 582 583 /* 584 * Process on-demand dialup. Output packets are queued within the tunnel 585 * device until the appropriate NCP is opened. 586 */ 587 588 if (bundle_Phase(bundle) == PHASE_DEAD) { 589 /* 590 * Note, we must be in AUTO mode :-/ otherwise our interface should 591 * *not* be UP and we can't receive data 592 */ 593 pri = PacketCheck(bundle, af, tun.data, n, &bundle->filter.dial, 594 NULL, NULL); 595 if (pri >= 0) 596 bundle_Open(bundle, NULL, PHYS_AUTO, 0); 597 else 598 /* 599 * Drop the packet. If we were to queue it, we'd just end up with 600 * a pile of timed-out data in our output queue by the time we get 601 * around to actually dialing. We'd also prematurely reach the 602 * threshold at which we stop select()ing to read() the tun 603 * device - breaking auto-dial. 604 */ 605 return; 606 } 607 608 secs = 0; 609 pri = PacketCheck(bundle, af, tun.data, n, &bundle->filter.out, 610 NULL, &secs); 611 if (pri >= 0) { 612 /* Prepend the number of seconds timeout given in the filter */ 613 tun.header.timeout = secs; 614 ncp_Enqueue(&bundle->ncp, af, pri, (char *)&tun, n + sizeof tun.header); 615 } 616 } 617} 618 619static int 620bundle_DescriptorWrite(struct fdescriptor *d, struct bundle *bundle, 621 const fd_set *fdset) 622{ 623 struct datalink *dl; 624 int result = 0; 625 626 /* This is not actually necessary as struct mpserver doesn't Write() */ 627 if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
| 133 MPPE_MasterKeyValid = 0; 134#endif 135 log_DisplayPrompts(); 136 break; 137 138 case PHASE_ESTABLISH: 139 bundle->phase = new; 140 break; 141 142 case PHASE_AUTHENTICATE: 143 bundle->phase = new; 144 log_DisplayPrompts(); 145 break; 146 147 case PHASE_NETWORK: 148 if (ncp_fsmStart(&bundle->ncp, bundle)) { 149 bundle->phase = new; 150 log_DisplayPrompts(); 151 } else { 152 log_Printf(LogPHASE, "bundle: All NCPs are disabled\n"); 153 bundle_Close(bundle, NULL, CLOSE_STAYDOWN); 154 } 155 break; 156 157 case PHASE_TERMINATE: 158 bundle->phase = new; 159 mp_Down(&bundle->ncp.mp); 160 log_DisplayPrompts(); 161 break; 162 } 163} 164 165static void 166bundle_LayerStart(void *v, struct fsm *fp) 167{ 168 /* The given FSM is about to start up ! */ 169} 170 171 172void 173bundle_Notify(struct bundle *bundle, char c) 174{ 175 if (bundle->notify.fd != -1) { 176 int ret; 177 178 ret = write(bundle->notify.fd, &c, 1); 179 if (c != EX_REDIAL && c != EX_RECONNECT) { 180 if (ret == 1) 181 log_Printf(LogCHAT, "Parent notified of %s\n", 182 c == EX_NORMAL ? "success" : "failure"); 183 else 184 log_Printf(LogERROR, "Failed to notify parent of success\n"); 185 close(bundle->notify.fd); 186 bundle->notify.fd = -1; 187 } else if (ret == 1) 188 log_Printf(LogCHAT, "Parent notified of %s\n", ex_desc(c)); 189 else 190 log_Printf(LogERROR, "Failed to notify parent of %s\n", ex_desc(c)); 191 } 192} 193 194static void 195bundle_ClearQueues(void *v) 196{ 197 struct bundle *bundle = (struct bundle *)v; 198 struct datalink *dl; 199 200 log_Printf(LogPHASE, "Clearing choked output queue\n"); 201 timer_Stop(&bundle->choked.timer); 202 203 /* 204 * Emergency time: 205 * 206 * We've had a full queue for PACKET_DEL_SECS seconds without being 207 * able to get rid of any of the packets. We've probably given up 208 * on the redials at this point, and the queued data has almost 209 * definitely been timed out by the layer above. As this is preventing 210 * us from reading the TUN_NAME device (we don't want to buffer stuff 211 * indefinitely), we may as well nuke this data and start with a clean 212 * slate ! 213 * 214 * Unfortunately, this has the side effect of shafting any compression 215 * dictionaries in use (causing the relevant RESET_REQ/RESET_ACK). 216 */ 217 218 ncp_DeleteQueues(&bundle->ncp); 219 for (dl = bundle->links; dl; dl = dl->next) 220 physical_DeleteQueue(dl->physical); 221} 222 223static void 224bundle_LinkAdded(struct bundle *bundle, struct datalink *dl) 225{ 226 bundle->phys_type.all |= dl->physical->type; 227 if (dl->state == DATALINK_OPEN) 228 bundle->phys_type.open |= dl->physical->type; 229 230 if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) 231 != bundle->phys_type.open && bundle->idle.timer.state == TIMER_STOPPED) 232 /* We may need to start our idle timer */ 233 bundle_StartIdleTimer(bundle, 0); 234} 235 236void 237bundle_LinksRemoved(struct bundle *bundle) 238{ 239 struct datalink *dl; 240 241 bundle->phys_type.all = bundle->phys_type.open = 0; 242 for (dl = bundle->links; dl; dl = dl->next) 243 bundle_LinkAdded(bundle, dl); 244 245 bundle_CalculateBandwidth(bundle); 246 mp_CheckAutoloadTimer(&bundle->ncp.mp); 247 248 if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) 249 == bundle->phys_type.open) 250 bundle_StopIdleTimer(bundle); 251} 252 253static void 254bundle_LayerUp(void *v, struct fsm *fp) 255{ 256 /* 257 * The given fsm is now up 258 * If it's an LCP, adjust our phys_mode.open value and check the 259 * autoload timer. 260 * If it's the first NCP, calculate our bandwidth 261 * If it's the first NCP, set our ``upat'' time 262 * If it's the first NCP, start the idle timer. 263 * If it's an NCP, tell our -background parent to go away. 264 * If it's the first NCP, start the autoload timer 265 */ 266 struct bundle *bundle = (struct bundle *)v; 267 268 if (fp->proto == PROTO_LCP) { 269 struct physical *p = link2physical(fp->link); 270 271 bundle_LinkAdded(bundle, p->dl); 272 mp_CheckAutoloadTimer(&bundle->ncp.mp); 273 } else if (isncp(fp->proto)) { 274 if (ncp_LayersOpen(&fp->bundle->ncp) == 1) { 275 bundle_CalculateBandwidth(fp->bundle); 276 time(&bundle->upat); 277 bundle_StartIdleTimer(bundle, 0); 278 mp_CheckAutoloadTimer(&fp->bundle->ncp.mp); 279 } 280 bundle_Notify(bundle, EX_NORMAL); 281 } else if (fp->proto == PROTO_CCP) 282 bundle_CalculateBandwidth(fp->bundle); /* Against ccp_MTUOverhead */ 283} 284 285static void 286bundle_LayerDown(void *v, struct fsm *fp) 287{ 288 /* 289 * The given FSM has been told to come down. 290 * If it's our last NCP, stop the idle timer. 291 * If it's our last NCP, clear our ``upat'' value. 292 * If it's our last NCP, stop the autoload timer 293 * If it's an LCP, adjust our phys_type.open value and any timers. 294 * If it's an LCP and we're in multilink mode, adjust our tun 295 * If it's the last LCP, down all NCPs 296 * speed and make sure our minimum sequence number is adjusted. 297 */ 298 299 struct bundle *bundle = (struct bundle *)v; 300 301 if (isncp(fp->proto)) { 302 if (ncp_LayersOpen(&fp->bundle->ncp) == 0) { 303 bundle_StopIdleTimer(bundle); 304 bundle->upat = 0; 305 mp_StopAutoloadTimer(&bundle->ncp.mp); 306 } 307 } else if (fp->proto == PROTO_LCP) { 308 struct datalink *dl; 309 struct datalink *lost; 310 int others_active; 311 312 bundle_LinksRemoved(bundle); /* adjust timers & phys_type values */ 313 314 lost = NULL; 315 others_active = 0; 316 for (dl = bundle->links; dl; dl = dl->next) { 317 if (fp == &dl->physical->link.lcp.fsm) 318 lost = dl; 319 else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 320 others_active++; 321 } 322 323 if (bundle->ncp.mp.active) { 324 bundle_CalculateBandwidth(bundle); 325 326 if (lost) 327 mp_LinkLost(&bundle->ncp.mp, lost); 328 else 329 log_Printf(LogALERT, "Oops, lost an unrecognised datalink (%s) !\n", 330 fp->link->name); 331 } 332 333 if (!others_active) 334 /* Down the NCPs. We don't expect to get fsm_Close()d ourself ! */ 335 ncp2initial(&bundle->ncp); 336 } 337} 338 339static void 340bundle_LayerFinish(void *v, struct fsm *fp) 341{ 342 /* The given fsm is now down (fp cannot be NULL) 343 * 344 * If it's the last NCP, fsm_Close all LCPs 345 */ 346 347 struct bundle *bundle = (struct bundle *)v; 348 struct datalink *dl; 349 350 if (isncp(fp->proto) && !ncp_LayersUnfinished(&bundle->ncp)) { 351 if (bundle_Phase(bundle) != PHASE_DEAD) 352 bundle_NewPhase(bundle, PHASE_TERMINATE); 353 for (dl = bundle->links; dl; dl = dl->next) 354 if (dl->state == DATALINK_OPEN) 355 datalink_Close(dl, CLOSE_STAYDOWN); 356 fsm2initial(fp); 357 } 358} 359 360void 361bundle_Close(struct bundle *bundle, const char *name, int how) 362{ 363 /* 364 * Please close the given datalink. 365 * If name == NULL or name is the last datalink, fsm_Close all NCPs 366 * (except our MP) 367 * If it isn't the last datalink, just Close that datalink. 368 */ 369 370 struct datalink *dl, *this_dl; 371 int others_active; 372 373 others_active = 0; 374 this_dl = NULL; 375 376 for (dl = bundle->links; dl; dl = dl->next) { 377 if (name && !strcasecmp(name, dl->name)) 378 this_dl = dl; 379 if (name == NULL || this_dl == dl) { 380 switch (how) { 381 case CLOSE_LCP: 382 datalink_DontHangup(dl); 383 break; 384 case CLOSE_STAYDOWN: 385 datalink_StayDown(dl); 386 break; 387 } 388 } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 389 others_active++; 390 } 391 392 if (name && this_dl == NULL) { 393 log_Printf(LogWARN, "%s: Invalid datalink name\n", name); 394 return; 395 } 396 397 if (!others_active) { 398 bundle_StopIdleTimer(bundle); 399 if (ncp_LayersUnfinished(&bundle->ncp)) 400 ncp_Close(&bundle->ncp); 401 else { 402 ncp2initial(&bundle->ncp); 403 for (dl = bundle->links; dl; dl = dl->next) 404 datalink_Close(dl, how); 405 } 406 } else if (this_dl && this_dl->state != DATALINK_CLOSED && 407 this_dl->state != DATALINK_HANGUP) 408 datalink_Close(this_dl, how); 409} 410 411void 412bundle_Down(struct bundle *bundle, int how) 413{ 414 struct datalink *dl; 415 416 for (dl = bundle->links; dl; dl = dl->next) 417 datalink_Down(dl, how); 418} 419 420static int 421bundle_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n) 422{ 423 struct bundle *bundle = descriptor2bundle(d); 424 struct datalink *dl; 425 int result, nlinks; 426 u_short ifqueue; 427 size_t queued; 428 429 result = 0; 430 431 /* If there are aren't many packets queued, look for some more. */ 432 for (nlinks = 0, dl = bundle->links; dl; dl = dl->next) 433 nlinks++; 434 435 if (nlinks) { 436 queued = r ? ncp_FillPhysicalQueues(&bundle->ncp, bundle) : 437 ncp_QueueLen(&bundle->ncp); 438 439 if (r && (bundle->phase == PHASE_NETWORK || 440 bundle->phys_type.all & PHYS_AUTO)) { 441 /* enough surplus so that we can tell if we're getting swamped */ 442 ifqueue = nlinks > bundle->cfg.ifqueue ? nlinks : bundle->cfg.ifqueue; 443 if (queued < ifqueue) { 444 /* Not enough - select() for more */ 445 if (bundle->choked.timer.state == TIMER_RUNNING) 446 timer_Stop(&bundle->choked.timer); /* Not needed any more */ 447 FD_SET(bundle->dev.fd, r); 448 if (*n < bundle->dev.fd + 1) 449 *n = bundle->dev.fd + 1; 450 log_Printf(LogTIMER, "%s: fdset(r) %d\n", TUN_NAME, bundle->dev.fd); 451 result++; 452 } else if (bundle->choked.timer.state == TIMER_STOPPED) { 453 bundle->choked.timer.func = bundle_ClearQueues; 454 bundle->choked.timer.name = "output choke"; 455 bundle->choked.timer.load = bundle->cfg.choked.timeout * SECTICKS; 456 bundle->choked.timer.arg = bundle; 457 timer_Start(&bundle->choked.timer); 458 } 459 } 460 } 461 462#ifndef NORADIUS 463 result += descriptor_UpdateSet(&bundle->radius.desc, r, w, e, n); 464#endif 465 466 /* Which links need a select() ? */ 467 for (dl = bundle->links; dl; dl = dl->next) 468 result += descriptor_UpdateSet(&dl->desc, r, w, e, n); 469 470 /* 471 * This *MUST* be called after the datalink UpdateSet()s as it 472 * might be ``holding'' one of the datalinks (death-row) and 473 * wants to be able to de-select() it from the descriptor set. 474 */ 475 result += descriptor_UpdateSet(&bundle->ncp.mp.server.desc, r, w, e, n); 476 477 return result; 478} 479 480static int 481bundle_IsSet(struct fdescriptor *d, const fd_set *fdset) 482{ 483 struct bundle *bundle = descriptor2bundle(d); 484 struct datalink *dl; 485 486 for (dl = bundle->links; dl; dl = dl->next) 487 if (descriptor_IsSet(&dl->desc, fdset)) 488 return 1; 489 490#ifndef NORADIUS 491 if (descriptor_IsSet(&bundle->radius.desc, fdset)) 492 return 1; 493#endif 494 495 if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 496 return 1; 497 498 return FD_ISSET(bundle->dev.fd, fdset); 499} 500 501static void 502bundle_DescriptorRead(struct fdescriptor *d, struct bundle *bundle, 503 const fd_set *fdset) 504{ 505 struct datalink *dl; 506 unsigned secs; 507 u_int32_t af; 508 509 if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 510 descriptor_Read(&bundle->ncp.mp.server.desc, bundle, fdset); 511 512 for (dl = bundle->links; dl; dl = dl->next) 513 if (descriptor_IsSet(&dl->desc, fdset)) 514 descriptor_Read(&dl->desc, bundle, fdset); 515 516#ifndef NORADIUS 517 if (descriptor_IsSet(&bundle->radius.desc, fdset)) 518 descriptor_Read(&bundle->radius.desc, bundle, fdset); 519#endif 520 521 if (FD_ISSET(bundle->dev.fd, fdset)) { 522 struct tun_data tun; 523 int n, pri; 524 char *data; 525 size_t sz; 526 527 if (bundle->dev.header) { 528 data = (char *)&tun; 529 sz = sizeof tun; 530 } else { 531 data = tun.data; 532 sz = sizeof tun.data; 533 } 534 535 /* something to read from tun */ 536 537 n = read(bundle->dev.fd, data, sz); 538 if (n < 0) { 539 log_Printf(LogWARN, "%s: read: %s\n", bundle->dev.Name, strerror(errno)); 540 return; 541 } 542 543 if (bundle->dev.header) { 544 n -= sz - sizeof tun.data; 545 if (n <= 0) { 546 log_Printf(LogERROR, "%s: read: Got only %d bytes of data !\n", 547 bundle->dev.Name, n); 548 return; 549 } 550 af = ntohl(tun.header.family); 551#ifndef NOINET6 552 if (af != AF_INET && af != AF_INET6) 553#else 554 if (af != AF_INET) 555#endif 556 /* XXX: Should be maintaining drop/family counts ! */ 557 return; 558 } else 559 af = AF_INET; 560 561 if (af == AF_INET && ((struct ip *)tun.data)->ip_dst.s_addr == 562 bundle->ncp.ipcp.my_ip.s_addr) { 563 /* we've been asked to send something addressed *to* us :( */ 564 if (Enabled(bundle, OPT_LOOPBACK)) { 565 pri = PacketCheck(bundle, af, tun.data, n, &bundle->filter.in, 566 NULL, NULL); 567 if (pri >= 0) { 568 n += sz - sizeof tun.data; 569 write(bundle->dev.fd, data, n); 570 log_Printf(LogDEBUG, "Looped back packet addressed to myself\n"); 571 } 572 return; 573 } else 574 log_Printf(LogDEBUG, "Oops - forwarding packet addressed to myself\n"); 575 } 576 577 /* 578 * Process on-demand dialup. Output packets are queued within the tunnel 579 * device until the appropriate NCP is opened. 580 */ 581 582 if (bundle_Phase(bundle) == PHASE_DEAD) { 583 /* 584 * Note, we must be in AUTO mode :-/ otherwise our interface should 585 * *not* be UP and we can't receive data 586 */ 587 pri = PacketCheck(bundle, af, tun.data, n, &bundle->filter.dial, 588 NULL, NULL); 589 if (pri >= 0) 590 bundle_Open(bundle, NULL, PHYS_AUTO, 0); 591 else 592 /* 593 * Drop the packet. If we were to queue it, we'd just end up with 594 * a pile of timed-out data in our output queue by the time we get 595 * around to actually dialing. We'd also prematurely reach the 596 * threshold at which we stop select()ing to read() the tun 597 * device - breaking auto-dial. 598 */ 599 return; 600 } 601 602 secs = 0; 603 pri = PacketCheck(bundle, af, tun.data, n, &bundle->filter.out, 604 NULL, &secs); 605 if (pri >= 0) { 606 /* Prepend the number of seconds timeout given in the filter */ 607 tun.header.timeout = secs; 608 ncp_Enqueue(&bundle->ncp, af, pri, (char *)&tun, n + sizeof tun.header); 609 } 610 } 611} 612 613static int 614bundle_DescriptorWrite(struct fdescriptor *d, struct bundle *bundle, 615 const fd_set *fdset) 616{ 617 struct datalink *dl; 618 int result = 0; 619 620 /* This is not actually necessary as struct mpserver doesn't Write() */ 621 if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
|
715 } 716#endif 717 if (errno != ENOENT || ++enoentcount > 2) { 718 err = errno; 719 break; 720 } 721 } else 722 err = errno; 723 } 724 725 if (bundle.dev.fd < 0) { 726 if (unit == -1) 727 log_Printf(LogWARN, "No available tunnel devices found (%s)\n", 728 strerror(err)); 729 else 730 log_Printf(LogWARN, "%s%d: %s\n", prefix, unit, strerror(err)); 731 return NULL; 732 } 733 734 log_SetTun(bundle.unit); 735 736 ifname = strrchr(bundle.dev.Name, '/'); 737 if (ifname == NULL) 738 ifname = bundle.dev.Name; 739 else 740 ifname++; 741 742 bundle.iface = iface_Create(ifname); 743 if (bundle.iface == NULL) { 744 close(bundle.dev.fd); 745 return NULL; 746 } 747 748#ifdef TUNSIFMODE 749 /* Make sure we're POINTOPOINT & IFF_MULTICAST */ 750 iff = IFF_POINTOPOINT | IFF_MULTICAST; 751 if (ID0ioctl(bundle.dev.fd, TUNSIFMODE, &iff) < 0) 752 log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFMODE): %s\n", 753 strerror(errno)); 754#endif 755 756#ifdef TUNSLMODE 757 /* Make sure we're not prepending sockaddrs */ 758 iff = 0; 759 if (ID0ioctl(bundle.dev.fd, TUNSLMODE, &iff) < 0) 760 log_Printf(LogERROR, "bundle_Create: ioctl(TUNSLMODE): %s\n", 761 strerror(errno)); 762#endif 763 764#ifdef TUNSIFHEAD 765 /* We want the address family please ! */ 766 iff = 1; 767 if (ID0ioctl(bundle.dev.fd, TUNSIFHEAD, &iff) < 0) { 768 log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFHEAD): %s\n", 769 strerror(errno)); 770 bundle.dev.header = 0; 771 } else 772 bundle.dev.header = 1; 773#else 774#ifdef __OpenBSD__ 775 /* Always present for OpenBSD */ 776 bundle.dev.header = 1; 777#else 778 /* 779 * If TUNSIFHEAD isn't available and we're not OpenBSD, assume 780 * everything's AF_INET (hopefully the tun device won't pass us 781 * anything else !). 782 */ 783 bundle.dev.header = 0; 784#endif 785#endif 786 787 log_Printf(LogPHASE, "Using interface: %s\n", ifname); 788 789 bundle.bandwidth = 0; 790 bundle.routing_seq = 0; 791 bundle.phase = PHASE_DEAD; 792 bundle.CleaningUp = 0; 793 bundle.NatEnabled = 0; 794 795 bundle.fsm.LayerStart = bundle_LayerStart; 796 bundle.fsm.LayerUp = bundle_LayerUp; 797 bundle.fsm.LayerDown = bundle_LayerDown; 798 bundle.fsm.LayerFinish = bundle_LayerFinish; 799 bundle.fsm.object = &bundle; 800 801 bundle.cfg.idle.timeout = NCP_IDLE_TIMEOUT; 802 bundle.cfg.idle.min_timeout = 0; 803 *bundle.cfg.auth.name = '\0'; 804 *bundle.cfg.auth.key = '\0'; 805 bundle.cfg.opt = OPT_IDCHECK | OPT_LOOPBACK | OPT_SROUTES | OPT_TCPMSSFIXUP | 806 OPT_THROUGHPUT | OPT_UTMP; 807#ifndef NOINET6 808 bundle.cfg.opt |= OPT_IPCP; 809 if (probe.ipv6_available) 810 bundle.cfg.opt |= OPT_IPV6CP; 811#endif 812 *bundle.cfg.label = '\0'; 813 bundle.cfg.ifqueue = DEF_IFQUEUE; 814 bundle.cfg.choked.timeout = CHOKED_TIMEOUT; 815 bundle.phys_type.all = type; 816 bundle.phys_type.open = 0; 817 bundle.upat = 0; 818 819 bundle.links = datalink_Create("deflink", &bundle, type); 820 if (bundle.links == NULL) { 821 log_Printf(LogALERT, "Cannot create data link: %s\n", strerror(errno)); 822 iface_Destroy(bundle.iface); 823 bundle.iface = NULL; 824 close(bundle.dev.fd); 825 return NULL; 826 } 827 828 bundle.desc.type = BUNDLE_DESCRIPTOR; 829 bundle.desc.UpdateSet = bundle_UpdateSet; 830 bundle.desc.IsSet = bundle_IsSet; 831 bundle.desc.Read = bundle_DescriptorRead; 832 bundle.desc.Write = bundle_DescriptorWrite; 833 834 ncp_Init(&bundle.ncp, &bundle); 835 836 memset(&bundle.filter, '\0', sizeof bundle.filter); 837 bundle.filter.in.fragok = bundle.filter.in.logok = 1; 838 bundle.filter.in.name = "IN"; 839 bundle.filter.out.fragok = bundle.filter.out.logok = 1; 840 bundle.filter.out.name = "OUT"; 841 bundle.filter.dial.name = "DIAL"; 842 bundle.filter.dial.logok = 1; 843 bundle.filter.alive.name = "ALIVE"; 844 bundle.filter.alive.logok = 1; 845 { 846 int i; 847 for (i = 0; i < MAXFILTERS; i++) { 848 bundle.filter.in.rule[i].f_action = A_NONE; 849 bundle.filter.out.rule[i].f_action = A_NONE; 850 bundle.filter.dial.rule[i].f_action = A_NONE; 851 bundle.filter.alive.rule[i].f_action = A_NONE; 852 } 853 } 854 memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer); 855 bundle.idle.done = 0; 856 bundle.notify.fd = -1; 857 memset(&bundle.choked.timer, '\0', sizeof bundle.choked.timer); 858#ifndef NORADIUS 859 radius_Init(&bundle.radius); 860#endif 861 862 /* Clean out any leftover crud */ 863 iface_Clear(bundle.iface, &bundle.ncp, 0, IFACE_CLEAR_ALL); 864 865 bundle_LockTun(&bundle); 866 867 return &bundle; 868} 869 870static void 871bundle_DownInterface(struct bundle *bundle) 872{ 873 route_IfDelete(bundle, 1); 874 iface_ClearFlags(bundle->iface->name, IFF_UP); 875} 876 877void 878bundle_Destroy(struct bundle *bundle) 879{ 880 struct datalink *dl; 881 882 /* 883 * Clean up the interface. We don't really need to do the timer_Stop()s, 884 * mp_Down(), iface_Clear() and bundle_DownInterface() unless we're getting 885 * out under exceptional conditions such as a descriptor exception. 886 */ 887 timer_Stop(&bundle->idle.timer); 888 timer_Stop(&bundle->choked.timer); 889 mp_Down(&bundle->ncp.mp); 890 iface_Clear(bundle->iface, &bundle->ncp, 0, IFACE_CLEAR_ALL); 891 bundle_DownInterface(bundle); 892 893#ifndef NORADIUS 894 /* Tell the radius server the bad news */ 895 radius_Destroy(&bundle->radius); 896#endif 897 898 /* Again, these are all DATALINK_CLOSED unless we're abending */ 899 dl = bundle->links; 900 while (dl) 901 dl = datalink_Destroy(dl); 902 903 ncp_Destroy(&bundle->ncp); 904 905 close(bundle->dev.fd); 906 bundle_UnlockTun(bundle); 907 908 /* In case we never made PHASE_NETWORK */ 909 bundle_Notify(bundle, EX_ERRDEAD); 910 911 iface_Destroy(bundle->iface); 912 bundle->iface = NULL; 913} 914 915void 916bundle_LinkClosed(struct bundle *bundle, struct datalink *dl) 917{ 918 /* 919 * Our datalink has closed. 920 * CleanDatalinks() (called from DoLoop()) will remove closed 921 * BACKGROUND, FOREGROUND and DIRECT links. 922 * If it's the last data link, enter phase DEAD. 923 * 924 * NOTE: dl may not be in our list (bundle_SendDatalink()) ! 925 */ 926 927 struct datalink *odl; 928 int other_links; 929 930 log_SetTtyCommandMode(dl); 931 932 other_links = 0; 933 for (odl = bundle->links; odl; odl = odl->next) 934 if (odl != dl && odl->state != DATALINK_CLOSED) 935 other_links++; 936 937 if (!other_links) { 938 if (dl->physical->type != PHYS_AUTO) /* Not in -auto mode */ 939 bundle_DownInterface(bundle); 940 ncp2initial(&bundle->ncp); 941 bundle_NewPhase(bundle, PHASE_DEAD); 942 bundle_StopIdleTimer(bundle); 943 } 944} 945 946void 947bundle_Open(struct bundle *bundle, const char *name, int mask, int force) 948{ 949 /* 950 * Please open the given datalink, or all if name == NULL 951 */ 952 struct datalink *dl; 953 954 for (dl = bundle->links; dl; dl = dl->next) 955 if (name == NULL || !strcasecmp(dl->name, name)) { 956 if ((mask & dl->physical->type) && 957 (dl->state == DATALINK_CLOSED || 958 (force && dl->state == DATALINK_OPENING && 959 dl->dial.timer.state == TIMER_RUNNING) || 960 dl->state == DATALINK_READY)) { 961 timer_Stop(&dl->dial.timer); /* We're finished with this */ 962 datalink_Up(dl, 1, 1); 963 if (mask & PHYS_AUTO) 964 break; /* Only one AUTO link at a time */ 965 } 966 if (name != NULL) 967 break; 968 } 969} 970 971struct datalink * 972bundle2datalink(struct bundle *bundle, const char *name) 973{ 974 struct datalink *dl; 975 976 if (name != NULL) { 977 for (dl = bundle->links; dl; dl = dl->next) 978 if (!strcasecmp(dl->name, name)) 979 return dl; 980 } else if (bundle->links && !bundle->links->next) 981 return bundle->links; 982 983 return NULL; 984} 985 986int 987bundle_ShowLinks(struct cmdargs const *arg) 988{ 989 struct datalink *dl; 990 struct pppThroughput *t; 991 unsigned long long octets; 992 int secs; 993 994 for (dl = arg->bundle->links; dl; dl = dl->next) { 995 octets = MAX(dl->physical->link.stats.total.in.OctetsPerSecond, 996 dl->physical->link.stats.total.out.OctetsPerSecond); 997 998 prompt_Printf(arg->prompt, "Name: %s [%s, %s]", 999 dl->name, mode2Nam(dl->physical->type), datalink_State(dl)); 1000 if (dl->physical->link.stats.total.rolling && dl->state == DATALINK_OPEN) 1001 prompt_Printf(arg->prompt, " bandwidth %d, %llu bps (%llu bytes/sec)", 1002 dl->mp.bandwidth ? dl->mp.bandwidth : 1003 physical_GetSpeed(dl->physical), 1004 octets * 8, octets); 1005 prompt_Printf(arg->prompt, "\n"); 1006 } 1007 1008 t = &arg->bundle->ncp.mp.link.stats.total; 1009 octets = MAX(t->in.OctetsPerSecond, t->out.OctetsPerSecond); 1010 secs = t->downtime ? 0 : throughput_uptime(t); 1011 if (secs > t->SamplePeriod) 1012 secs = t->SamplePeriod; 1013 if (secs) 1014 prompt_Printf(arg->prompt, "Currently averaging %llu bps (%llu bytes/sec)" 1015 " over the last %d secs\n", octets * 8, octets, secs); 1016 1017 return 0; 1018} 1019 1020static const char * 1021optval(struct bundle *bundle, int bit) 1022{ 1023 return (bundle->cfg.opt & bit) ? "enabled" : "disabled"; 1024} 1025 1026int 1027bundle_ShowStatus(struct cmdargs const *arg) 1028{ 1029 int remaining; 1030 1031 prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle)); 1032 prompt_Printf(arg->prompt, " Device: %s\n", arg->bundle->dev.Name); 1033 prompt_Printf(arg->prompt, " Interface: %s @ %lubps", 1034 arg->bundle->iface->name, arg->bundle->bandwidth); 1035 1036 if (arg->bundle->upat) { 1037 int secs = bundle_Uptime(arg->bundle); 1038 1039 prompt_Printf(arg->prompt, ", up time %d:%02d:%02d", secs / 3600, 1040 (secs / 60) % 60, secs % 60); 1041 } 1042 prompt_Printf(arg->prompt, "\n Queued: %lu of %u\n", 1043 (unsigned long)ncp_QueueLen(&arg->bundle->ncp), 1044 arg->bundle->cfg.ifqueue); 1045 1046 prompt_Printf(arg->prompt, "\nDefaults:\n"); 1047 prompt_Printf(arg->prompt, " Label: %s\n", 1048 arg->bundle->cfg.label); 1049 prompt_Printf(arg->prompt, " Auth name: %s\n", 1050 arg->bundle->cfg.auth.name); 1051 prompt_Printf(arg->prompt, " Diagnostic socket: "); 1052 if (*server.cfg.sockname != '\0') { 1053 prompt_Printf(arg->prompt, "%s", server.cfg.sockname); 1054 if (server.cfg.mask != (mode_t)-1) 1055 prompt_Printf(arg->prompt, ", mask 0%03o", (int)server.cfg.mask); 1056 prompt_Printf(arg->prompt, "%s\n", server.fd == -1 ? " (not open)" : ""); 1057 } else if (server.cfg.port != 0) 1058 prompt_Printf(arg->prompt, "TCP port %d%s\n", server.cfg.port, 1059 server.fd == -1 ? " (not open)" : ""); 1060 else 1061 prompt_Printf(arg->prompt, "none\n"); 1062 1063 prompt_Printf(arg->prompt, " Choked Timer: %ds\n", 1064 arg->bundle->cfg.choked.timeout); 1065 1066#ifndef NORADIUS 1067 radius_Show(&arg->bundle->radius, arg->prompt); 1068#endif 1069 1070 prompt_Printf(arg->prompt, " Idle Timer: "); 1071 if (arg->bundle->cfg.idle.timeout) { 1072 prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle.timeout); 1073 if (arg->bundle->cfg.idle.min_timeout) 1074 prompt_Printf(arg->prompt, ", min %ds", 1075 arg->bundle->cfg.idle.min_timeout); 1076 remaining = bundle_RemainingIdleTime(arg->bundle); 1077 if (remaining != -1) 1078 prompt_Printf(arg->prompt, " (%ds remaining)", remaining); 1079 prompt_Printf(arg->prompt, "\n"); 1080 } else 1081 prompt_Printf(arg->prompt, "disabled\n"); 1082 1083 prompt_Printf(arg->prompt, " Filter Decap: %-20.20s", 1084 optval(arg->bundle, OPT_FILTERDECAP)); 1085 prompt_Printf(arg->prompt, " ID check: %s\n", 1086 optval(arg->bundle, OPT_IDCHECK)); 1087 prompt_Printf(arg->prompt, " Iface-Alias: %-20.20s", 1088 optval(arg->bundle, OPT_IFACEALIAS)); 1089#ifndef NOINET6 1090 prompt_Printf(arg->prompt, " IPCP: %s\n", 1091 optval(arg->bundle, OPT_IPCP)); 1092 prompt_Printf(arg->prompt, " IPV6CP: %-20.20s", 1093 optval(arg->bundle, OPT_IPV6CP)); 1094#endif 1095 prompt_Printf(arg->prompt, " Keep-Session: %s\n", 1096 optval(arg->bundle, OPT_KEEPSESSION)); 1097 prompt_Printf(arg->prompt, " Loopback: %-20.20s", 1098 optval(arg->bundle, OPT_LOOPBACK)); 1099 prompt_Printf(arg->prompt, " PasswdAuth: %s\n", 1100 optval(arg->bundle, OPT_PASSWDAUTH)); 1101 prompt_Printf(arg->prompt, " Proxy: %-20.20s", 1102 optval(arg->bundle, OPT_PROXY)); 1103 prompt_Printf(arg->prompt, " Proxyall: %s\n", 1104 optval(arg->bundle, OPT_PROXYALL)); 1105 prompt_Printf(arg->prompt, " Sticky Routes: %-20.20s", 1106 optval(arg->bundle, OPT_SROUTES)); 1107 prompt_Printf(arg->prompt, " TCPMSS Fixup: %s\n", 1108 optval(arg->bundle, OPT_TCPMSSFIXUP)); 1109 prompt_Printf(arg->prompt, " Throughput: %-20.20s", 1110 optval(arg->bundle, OPT_THROUGHPUT)); 1111 prompt_Printf(arg->prompt, " Utmp Logging: %s\n", 1112 optval(arg->bundle, OPT_UTMP)); 1113 1114 return 0; 1115} 1116 1117static void 1118bundle_IdleTimeout(void *v) 1119{ 1120 struct bundle *bundle = (struct bundle *)v; 1121 1122 log_Printf(LogPHASE, "Idle timer expired\n"); 1123 bundle_StopIdleTimer(bundle); 1124 bundle_Close(bundle, NULL, CLOSE_STAYDOWN); 1125} 1126 1127/* 1128 * Start Idle timer. If timeout is reached, we call bundle_Close() to 1129 * close LCP and link. 1130 */ 1131void 1132bundle_StartIdleTimer(struct bundle *bundle, unsigned secs) 1133{ 1134 timer_Stop(&bundle->idle.timer); 1135 if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) != 1136 bundle->phys_type.open && bundle->cfg.idle.timeout) { 1137 time_t now = time(NULL); 1138 1139 if (secs == 0) 1140 secs = bundle->cfg.idle.timeout; 1141 1142 /* We want at least `secs' */ 1143 if (bundle->cfg.idle.min_timeout > secs && bundle->upat) { 1144 int up = now - bundle->upat; 1145 1146 if ((long long)bundle->cfg.idle.min_timeout - up > (long long)secs) 1147 /* Only increase from the current `remaining' value */ 1148 secs = bundle->cfg.idle.min_timeout - up; 1149 } 1150 bundle->idle.timer.func = bundle_IdleTimeout; 1151 bundle->idle.timer.name = "idle"; 1152 bundle->idle.timer.load = secs * SECTICKS; 1153 bundle->idle.timer.arg = bundle; 1154 timer_Start(&bundle->idle.timer); 1155 bundle->idle.done = now + secs; 1156 } 1157} 1158 1159void 1160bundle_SetIdleTimer(struct bundle *bundle, int timeout, int min_timeout) 1161{ 1162 bundle->cfg.idle.timeout = timeout; 1163 if (min_timeout >= 0) 1164 bundle->cfg.idle.min_timeout = min_timeout; 1165 if (ncp_LayersOpen(&bundle->ncp)) 1166 bundle_StartIdleTimer(bundle, 0); 1167} 1168 1169void 1170bundle_StopIdleTimer(struct bundle *bundle) 1171{ 1172 timer_Stop(&bundle->idle.timer); 1173 bundle->idle.done = 0; 1174} 1175 1176static int 1177bundle_RemainingIdleTime(struct bundle *bundle) 1178{ 1179 if (bundle->idle.done) 1180 return bundle->idle.done - time(NULL); 1181 return -1; 1182} 1183 1184int 1185bundle_IsDead(struct bundle *bundle) 1186{ 1187 return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp); 1188} 1189 1190static struct datalink * 1191bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl) 1192{ 1193 struct datalink **dlp; 1194 1195 for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next) 1196 if (*dlp == dl) { 1197 *dlp = dl->next; 1198 dl->next = NULL; 1199 bundle_LinksRemoved(bundle); 1200 return dl; 1201 } 1202 1203 return NULL; 1204} 1205 1206static void 1207bundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl) 1208{ 1209 struct datalink **dlp = &bundle->links; 1210 1211 while (*dlp) 1212 dlp = &(*dlp)->next; 1213 1214 *dlp = dl; 1215 dl->next = NULL; 1216 1217 bundle_LinkAdded(bundle, dl); 1218 mp_CheckAutoloadTimer(&bundle->ncp.mp); 1219} 1220 1221void 1222bundle_CleanDatalinks(struct bundle *bundle) 1223{ 1224 struct datalink **dlp = &bundle->links; 1225 int found = 0; 1226 1227 while (*dlp) 1228 if ((*dlp)->state == DATALINK_CLOSED && 1229 (*dlp)->physical->type & 1230 (PHYS_DIRECT|PHYS_BACKGROUND|PHYS_FOREGROUND)) { 1231 *dlp = datalink_Destroy(*dlp); 1232 found++; 1233 } else 1234 dlp = &(*dlp)->next; 1235 1236 if (found) 1237 bundle_LinksRemoved(bundle); 1238} 1239 1240int 1241bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl, 1242 const char *name) 1243{ 1244 if (bundle2datalink(bundle, name)) { 1245 log_Printf(LogWARN, "Clone: %s: name already exists\n", name); 1246 return 0; 1247 } 1248 1249 bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name)); 1250 return 1; 1251} 1252 1253void 1254bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl) 1255{ 1256 dl = bundle_DatalinkLinkout(bundle, dl); 1257 if (dl) 1258 datalink_Destroy(dl); 1259} 1260 1261void 1262bundle_SetLabel(struct bundle *bundle, const char *label) 1263{ 1264 if (label) 1265 strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1); 1266 else 1267 *bundle->cfg.label = '\0'; 1268} 1269 1270const char * 1271bundle_GetLabel(struct bundle *bundle) 1272{ 1273 return *bundle->cfg.label ? bundle->cfg.label : NULL; 1274} 1275 1276int 1277bundle_LinkSize() 1278{ 1279 struct iovec iov[SCATTER_SEGMENTS]; 1280 int niov, expect, f; 1281 1282 iov[0].iov_len = strlen(Version) + 1; 1283 iov[0].iov_base = NULL; 1284 niov = 1; 1285 if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) { 1286 log_Printf(LogERROR, "Cannot determine space required for link\n"); 1287 return 0; 1288 } 1289 1290 for (f = expect = 0; f < niov; f++) 1291 expect += iov[f].iov_len; 1292 1293 return expect; 1294} 1295 1296void 1297bundle_ReceiveDatalink(struct bundle *bundle, int s) 1298{ 1299 char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int) * SEND_MAXFD]; 1300 int niov, expect, f, *fd, nfd, onfd, got; 1301 struct iovec iov[SCATTER_SEGMENTS]; 1302 struct cmsghdr *cmsg; 1303 struct msghdr msg; 1304 struct datalink *dl; 1305 pid_t pid; 1306 1307 log_Printf(LogPHASE, "Receiving datalink\n"); 1308 1309 /* 1310 * Create our scatter/gather array - passing NULL gets the space 1311 * allocation requirement rather than actually flattening the 1312 * structures. 1313 */ 1314 iov[0].iov_len = strlen(Version) + 1; 1315 iov[0].iov_base = NULL; 1316 niov = 1; 1317 if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) { 1318 log_Printf(LogERROR, "Cannot determine space required for link\n"); 1319 return; 1320 } 1321 1322 /* Allocate the scatter/gather array for recvmsg() */ 1323 for (f = expect = 0; f < niov; f++) { 1324 if ((iov[f].iov_base = malloc(iov[f].iov_len)) == NULL) { 1325 log_Printf(LogERROR, "Cannot allocate space to receive link\n"); 1326 return; 1327 } 1328 if (f) 1329 expect += iov[f].iov_len; 1330 } 1331 1332 /* Set up our message */ 1333 cmsg = (struct cmsghdr *)cmsgbuf; 1334 cmsg->cmsg_len = sizeof cmsgbuf; 1335 cmsg->cmsg_level = SOL_SOCKET; 1336 cmsg->cmsg_type = 0; 1337 1338 memset(&msg, '\0', sizeof msg); 1339 msg.msg_name = NULL; 1340 msg.msg_namelen = 0; 1341 msg.msg_iov = iov; 1342 msg.msg_iovlen = 1; /* Only send the version at the first pass */ 1343 msg.msg_control = cmsgbuf; 1344 msg.msg_controllen = sizeof cmsgbuf; 1345 1346 log_Printf(LogDEBUG, "Expecting %u scatter/gather bytes\n", 1347 (unsigned)iov[0].iov_len); 1348 1349 if ((got = recvmsg(s, &msg, MSG_WAITALL)) != iov[0].iov_len) { 1350 if (got == -1) 1351 log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno)); 1352 else 1353 log_Printf(LogERROR, "Failed recvmsg: Got %d, not %u\n", 1354 got, (unsigned)iov[0].iov_len); 1355 while (niov--) 1356 free(iov[niov].iov_base); 1357 return; 1358 } 1359 1360 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { 1361 log_Printf(LogERROR, "Recvmsg: no descriptors received !\n"); 1362 while (niov--) 1363 free(iov[niov].iov_base); 1364 return; 1365 } 1366 1367 fd = (int *)CMSG_DATA(cmsg); 1368 nfd = ((caddr_t)cmsg + cmsg->cmsg_len - (caddr_t)fd) / sizeof(int); 1369 1370 if (nfd < 2) { 1371 log_Printf(LogERROR, "Recvmsg: %d descriptor%s received (too few) !\n", 1372 nfd, nfd == 1 ? "" : "s"); 1373 while (nfd--) 1374 close(fd[nfd]); 1375 while (niov--) 1376 free(iov[niov].iov_base); 1377 return; 1378 } 1379 1380 /* 1381 * We've successfully received two or more open file descriptors 1382 * through our socket, plus a version string. Make sure it's the 1383 * correct version, and drop the connection if it's not. 1384 */ 1385 if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) { 1386 log_Printf(LogWARN, "Cannot receive datalink, incorrect version" 1387 " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len, 1388 (char *)iov[0].iov_base, Version); 1389 while (nfd--) 1390 close(fd[nfd]); 1391 while (niov--) 1392 free(iov[niov].iov_base); 1393 return; 1394 } 1395 1396 /* 1397 * Everything looks good. Send the other side our process id so that 1398 * they can transfer lock ownership, and wait for them to send the 1399 * actual link data. 1400 */ 1401 pid = getpid(); 1402 if ((got = write(fd[1], &pid, sizeof pid)) != sizeof pid) { 1403 if (got == -1) 1404 log_Printf(LogERROR, "Failed write: %s\n", strerror(errno)); 1405 else 1406 log_Printf(LogERROR, "Failed write: Got %d, not %d\n", got, 1407 (int)(sizeof pid)); 1408 while (nfd--) 1409 close(fd[nfd]); 1410 while (niov--) 1411 free(iov[niov].iov_base); 1412 return; 1413 } 1414 1415 if ((got = readv(fd[1], iov + 1, niov - 1)) != expect) { 1416 if (got == -1) 1417 log_Printf(LogERROR, "Failed write: %s\n", strerror(errno)); 1418 else 1419 log_Printf(LogERROR, "Failed write: Got %d, not %d\n", got, expect); 1420 while (nfd--) 1421 close(fd[nfd]); 1422 while (niov--) 1423 free(iov[niov].iov_base); 1424 return; 1425 } 1426 close(fd[1]); 1427 1428 onfd = nfd; /* We've got this many in our array */ 1429 nfd -= 2; /* Don't include p->fd and our reply descriptor */ 1430 niov = 1; /* Skip the version id */ 1431 dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, fd[0], 1432 fd + 2, &nfd); 1433 if (dl) { 1434 1435 if (nfd) { 1436 log_Printf(LogERROR, "bundle_ReceiveDatalink: Failed to handle %d " 1437 "auxiliary file descriptors (%d remain)\n", onfd, nfd); 1438 datalink_Destroy(dl); 1439 while (nfd--) 1440 close(fd[onfd--]); 1441 close(fd[0]); 1442 } else { 1443 bundle_DatalinkLinkin(bundle, dl); 1444 datalink_AuthOk(dl); 1445 bundle_CalculateBandwidth(dl->bundle); 1446 } 1447 } else { 1448 while (nfd--) 1449 close(fd[onfd--]); 1450 close(fd[0]); 1451 close(fd[1]); 1452 } 1453 1454 free(iov[0].iov_base); 1455} 1456 1457void 1458bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun) 1459{ 1460 char cmsgbuf[CMSG_SPACE(sizeof(int) * SEND_MAXFD)]; 1461 const char *constlock; 1462 char *lock; 1463 struct cmsghdr *cmsg; 1464 struct msghdr msg; 1465 struct iovec iov[SCATTER_SEGMENTS]; 1466 int niov, f, expect, newsid, fd[SEND_MAXFD], nfd, reply[2], got; 1467 pid_t newpid; 1468 1469 log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name); 1470 1471 /* Record the base device name for a lock transfer later */ 1472 constlock = physical_LockedDevice(dl->physical); 1473 if (constlock) { 1474 lock = alloca(strlen(constlock) + 1); 1475 strcpy(lock, constlock); 1476 } else 1477 lock = NULL; 1478 1479 bundle_LinkClosed(dl->bundle, dl); 1480 bundle_DatalinkLinkout(dl->bundle, dl); 1481 1482 /* Build our scatter/gather array */ 1483 iov[0].iov_len = strlen(Version) + 1; 1484 iov[0].iov_base = strdup(Version); 1485 niov = 1; 1486 nfd = 0; 1487 1488 fd[0] = datalink2iov(dl, iov, &niov, SCATTER_SEGMENTS, fd + 2, &nfd); 1489 1490 if (fd[0] != -1 && socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, reply) != -1) { 1491 /* 1492 * fd[1] is used to get the peer process id back, then to confirm that 1493 * we've transferred any device locks to that process id. 1494 */ 1495 fd[1] = reply[1]; 1496 1497 nfd += 2; /* Include fd[0] and fd[1] */ 1498 memset(&msg, '\0', sizeof msg); 1499 1500 msg.msg_name = NULL; 1501 msg.msg_namelen = 0; 1502 /* 1503 * Only send the version to start... We used to send the whole lot, but 1504 * this caused problems with our RECVBUF size as a single link is about 1505 * 22k ! This way, we should bump into no limits. 1506 */ 1507 msg.msg_iovlen = 1; 1508 msg.msg_iov = iov; 1509 msg.msg_control = cmsgbuf; 1510 msg.msg_controllen = CMSG_SPACE(sizeof(int) * nfd); 1511 msg.msg_flags = 0; 1512 1513 cmsg = (struct cmsghdr *)cmsgbuf; 1514 cmsg->cmsg_len = msg.msg_controllen; 1515 cmsg->cmsg_level = SOL_SOCKET; 1516 cmsg->cmsg_type = SCM_RIGHTS; 1517 1518 for (f = 0; f < nfd; f++) 1519 *((int *)CMSG_DATA(cmsg) + f) = fd[f]; 1520 1521 for (f = 1, expect = 0; f < niov; f++) 1522 expect += iov[f].iov_len; 1523 1524 if (setsockopt(reply[0], SOL_SOCKET, SO_SNDBUF, &expect, sizeof(int)) == -1) 1525 log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect, 1526 strerror(errno)); 1527 if (setsockopt(reply[1], SOL_SOCKET, SO_RCVBUF, &expect, sizeof(int)) == -1) 1528 log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect, 1529 strerror(errno)); 1530 1531 log_Printf(LogDEBUG, "Sending %d descriptor%s and %u bytes in scatter" 1532 "/gather array\n", nfd, nfd == 1 ? "" : "s", 1533 (unsigned)iov[0].iov_len); 1534 1535 if ((got = sendmsg(s, &msg, 0)) == -1) 1536 log_Printf(LogERROR, "Failed sendmsg: %s: %s\n", 1537 sun->sun_path, strerror(errno)); 1538 else if (got != iov[0].iov_len) 1539 log_Printf(LogERROR, "%s: Failed initial sendmsg: Only sent %d of %u\n", 1540 sun->sun_path, got, (unsigned)iov[0].iov_len); 1541 else { 1542 /* We must get the ACK before closing the descriptor ! */ 1543 int res; 1544 1545 if ((got = read(reply[0], &newpid, sizeof newpid)) == sizeof newpid) { 1546 log_Printf(LogDEBUG, "Received confirmation from pid %d\n", 1547 (int)newpid); 1548 if (lock && (res = ID0uu_lock_txfr(lock, newpid)) != UU_LOCK_OK) 1549 log_Printf(LogERROR, "uu_lock_txfr: %s\n", uu_lockerr(res)); 1550 1551 log_Printf(LogDEBUG, "Transmitting link (%d bytes)\n", expect); 1552 if ((got = writev(reply[0], iov + 1, niov - 1)) != expect) { 1553 if (got == -1) 1554 log_Printf(LogERROR, "%s: Failed writev: %s\n", 1555 sun->sun_path, strerror(errno)); 1556 else 1557 log_Printf(LogERROR, "%s: Failed writev: Wrote %d of %d\n", 1558 sun->sun_path, got, expect); 1559 } 1560 } else if (got == -1) 1561 log_Printf(LogERROR, "%s: Failed socketpair read: %s\n", 1562 sun->sun_path, strerror(errno)); 1563 else 1564 log_Printf(LogERROR, "%s: Failed socketpair read: Got %d of %d\n", 1565 sun->sun_path, got, (int)(sizeof newpid)); 1566 } 1567 1568 close(reply[0]); 1569 close(reply[1]); 1570 1571 newsid = Enabled(dl->bundle, OPT_KEEPSESSION) || 1572 tcgetpgrp(fd[0]) == getpgrp(); 1573 while (nfd) 1574 close(fd[--nfd]); 1575 if (newsid) 1576 bundle_setsid(dl->bundle, got != -1); 1577 } 1578 close(s); 1579 1580 while (niov--) 1581 free(iov[niov].iov_base); 1582} 1583 1584int 1585bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl, 1586 const char *name) 1587{ 1588 struct datalink *dl; 1589 1590 if (!strcasecmp(ndl->name, name)) 1591 return 1; 1592 1593 for (dl = bundle->links; dl; dl = dl->next) 1594 if (!strcasecmp(dl->name, name)) 1595 return 0; 1596 1597 datalink_Rename(ndl, name); 1598 return 1; 1599} 1600 1601int 1602bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode) 1603{ 1604 int omode; 1605 1606 omode = dl->physical->type; 1607 if (omode == mode) 1608 return 1; 1609 1610 if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO)) 1611 /* First auto link */ 1612 if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) { 1613 log_Printf(LogWARN, "You must `set ifaddr' or `open' before" 1614 " changing mode to %s\n", mode2Nam(mode)); 1615 return 0; 1616 } 1617 1618 if (!datalink_SetMode(dl, mode)) 1619 return 0; 1620 1621 if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO) && 1622 bundle->phase != PHASE_NETWORK) 1623 /* First auto link, we need an interface */ 1624 ipcp_InterfaceUp(&bundle->ncp.ipcp); 1625 1626 /* Regenerate phys_type and adjust idle timer */ 1627 bundle_LinksRemoved(bundle); 1628 1629 return 1; 1630} 1631 1632void 1633bundle_setsid(struct bundle *bundle, int holdsession) 1634{ 1635 /* 1636 * Lose the current session. This means getting rid of our pid 1637 * too so that the tty device will really go away, and any getty 1638 * etc will be allowed to restart. 1639 */ 1640 pid_t pid, orig; 1641 int fds[2]; 1642 char done; 1643 struct datalink *dl; 1644 1645 if (!holdsession && bundle_IsDead(bundle)) { 1646 /* 1647 * No need to lose our session after all... we're going away anyway 1648 * 1649 * We should really stop the timer and pause if holdsession is set and 1650 * the bundle's dead, but that leaves other resources lying about :-( 1651 */ 1652 return; 1653 } 1654 1655 orig = getpid(); 1656 if (pipe(fds) == -1) { 1657 log_Printf(LogERROR, "pipe: %s\n", strerror(errno)); 1658 return; 1659 } 1660 switch ((pid = fork())) { 1661 case -1: 1662 log_Printf(LogERROR, "fork: %s\n", strerror(errno)); 1663 close(fds[0]); 1664 close(fds[1]); 1665 return; 1666 case 0: 1667 close(fds[1]); 1668 read(fds[0], &done, 1); /* uu_locks are mine ! */ 1669 close(fds[0]); 1670 if (pipe(fds) == -1) { 1671 log_Printf(LogERROR, "pipe(2): %s\n", strerror(errno)); 1672 return; 1673 } 1674 switch ((pid = fork())) { 1675 case -1: 1676 log_Printf(LogERROR, "fork(2): %s\n", strerror(errno)); 1677 close(fds[0]); 1678 close(fds[1]); 1679 return; 1680 case 0: 1681 close(fds[1]); 1682 bundle_LockTun(bundle); /* update pid */ 1683 read(fds[0], &done, 1); /* uu_locks are mine ! */ 1684 close(fds[0]); 1685 setsid(); 1686 bundle_ChangedPID(bundle); 1687 log_Printf(LogDEBUG, "%d -> %d: %s session control\n", 1688 (int)orig, (int)getpid(), 1689 holdsession ? "Passed" : "Dropped"); 1690 timer_InitService(0); /* Start the Timer Service */ 1691 break; 1692 default: 1693 close(fds[0]); 1694 /* Give away all our physical locks (to the final process) */ 1695 for (dl = bundle->links; dl; dl = dl->next) 1696 if (dl->state != DATALINK_CLOSED) 1697 physical_ChangedPid(dl->physical, pid); 1698 write(fds[1], "!", 1); /* done */ 1699 close(fds[1]); 1700 _exit(0); 1701 break; 1702 } 1703 break; 1704 default: 1705 close(fds[0]); 1706 /* Give away all our physical locks (to the intermediate process) */ 1707 for (dl = bundle->links; dl; dl = dl->next) 1708 if (dl->state != DATALINK_CLOSED) 1709 physical_ChangedPid(dl->physical, pid); 1710 write(fds[1], "!", 1); /* done */ 1711 close(fds[1]); 1712 if (holdsession) { 1713 int fd, status; 1714 1715 timer_TermService(); 1716 signal(SIGPIPE, SIG_DFL); 1717 signal(SIGALRM, SIG_DFL); 1718 signal(SIGHUP, SIG_DFL); 1719 signal(SIGTERM, SIG_DFL); 1720 signal(SIGINT, SIG_DFL); 1721 signal(SIGQUIT, SIG_DFL); 1722 for (fd = getdtablesize(); fd >= 0; fd--) 1723 close(fd); 1724 /* 1725 * Reap the intermediate process. As we're not exiting but the 1726 * intermediate is, we don't want it to become defunct. 1727 */ 1728 waitpid(pid, &status, 0); 1729 /* Tweak our process arguments.... */ 1730 SetTitle("session owner"); 1731#ifndef NOSUID 1732 setuid(ID0realuid()); 1733#endif 1734 /* 1735 * Hang around for a HUP. This should happen as soon as the 1736 * ppp that we passed our ctty descriptor to closes it. 1737 * NOTE: If this process dies, the passed descriptor becomes 1738 * invalid and will give a select() error by setting one 1739 * of the error fds, aborting the other ppp. We don't 1740 * want that to happen ! 1741 */ 1742 pause(); 1743 } 1744 _exit(0); 1745 break; 1746 } 1747} 1748 1749int 1750bundle_HighestState(struct bundle *bundle) 1751{ 1752 struct datalink *dl; 1753 int result = DATALINK_CLOSED; 1754 1755 for (dl = bundle->links; dl; dl = dl->next) 1756 if (result < dl->state) 1757 result = dl->state; 1758 1759 return result; 1760} 1761 1762int 1763bundle_Exception(struct bundle *bundle, int fd) 1764{ 1765 struct datalink *dl; 1766 1767 for (dl = bundle->links; dl; dl = dl->next) 1768 if (dl->physical->fd == fd) { 1769 datalink_Down(dl, CLOSE_NORMAL); 1770 return 1; 1771 } 1772 1773 return 0; 1774} 1775 1776void 1777bundle_AdjustFilters(struct bundle *bundle, struct ncpaddr *local, 1778 struct ncpaddr *remote) 1779{ 1780 filter_AdjustAddr(&bundle->filter.in, local, remote, NULL); 1781 filter_AdjustAddr(&bundle->filter.out, local, remote, NULL); 1782 filter_AdjustAddr(&bundle->filter.dial, local, remote, NULL); 1783 filter_AdjustAddr(&bundle->filter.alive, local, remote, NULL); 1784} 1785 1786void 1787bundle_AdjustDNS(struct bundle *bundle) 1788{ 1789 struct in_addr *dns = bundle->ncp.ipcp.ns.dns; 1790 1791 filter_AdjustAddr(&bundle->filter.in, NULL, NULL, dns); 1792 filter_AdjustAddr(&bundle->filter.out, NULL, NULL, dns); 1793 filter_AdjustAddr(&bundle->filter.dial, NULL, NULL, dns); 1794 filter_AdjustAddr(&bundle->filter.alive, NULL, NULL, dns); 1795} 1796 1797void 1798bundle_CalculateBandwidth(struct bundle *bundle) 1799{ 1800 struct datalink *dl; 1801 int sp, overhead, maxoverhead; 1802 1803 bundle->bandwidth = 0; 1804 bundle->iface->mtu = 0; 1805 maxoverhead = 0; 1806 1807 for (dl = bundle->links; dl; dl = dl->next) { 1808 overhead = ccp_MTUOverhead(&dl->physical->link.ccp); 1809 if (maxoverhead < overhead) 1810 maxoverhead = overhead; 1811 if (dl->state == DATALINK_OPEN) { 1812 if ((sp = dl->mp.bandwidth) == 0 && 1813 (sp = physical_GetSpeed(dl->physical)) == 0) 1814 log_Printf(LogDEBUG, "%s: %s: Cannot determine bandwidth\n", 1815 dl->name, dl->physical->name.full); 1816 else 1817 bundle->bandwidth += sp; 1818 if (!bundle->ncp.mp.active) { 1819 bundle->iface->mtu = dl->physical->link.lcp.his_mru; 1820 break; 1821 } 1822 } 1823 } 1824 1825 if(bundle->bandwidth == 0) 1826 bundle->bandwidth = 115200; /* Shrug */ 1827 1828 if (bundle->ncp.mp.active) { 1829 bundle->iface->mtu = bundle->ncp.mp.peer_mrru; 1830 overhead = ccp_MTUOverhead(&bundle->ncp.mp.link.ccp); 1831 if (maxoverhead < overhead) 1832 maxoverhead = overhead; 1833 } else if (!bundle->iface->mtu) 1834 bundle->iface->mtu = DEF_MRU; 1835 1836#ifndef NORADIUS 1837 if (bundle->radius.valid && bundle->radius.mtu && 1838 bundle->radius.mtu < bundle->iface->mtu) { 1839 log_Printf(LogLCP, "Reducing MTU to radius value %lu\n", 1840 bundle->radius.mtu); 1841 bundle->iface->mtu = bundle->radius.mtu; 1842 } 1843#endif 1844 1845 if (maxoverhead) { 1846 log_Printf(LogLCP, "Reducing MTU from %d to %d (CCP requirement)\n", 1847 bundle->iface->mtu, bundle->iface->mtu - maxoverhead); 1848 bundle->iface->mtu -= maxoverhead; 1849 } 1850 1851 tun_configure(bundle); 1852 1853 route_UpdateMTU(bundle); 1854} 1855 1856void 1857bundle_AutoAdjust(struct bundle *bundle, int percent, int what) 1858{ 1859 struct datalink *dl, *choice, *otherlinkup; 1860 1861 choice = otherlinkup = NULL; 1862 for (dl = bundle->links; dl; dl = dl->next) 1863 if (dl->physical->type == PHYS_AUTO) { 1864 if (dl->state == DATALINK_OPEN) { 1865 if (what == AUTO_DOWN) { 1866 if (choice) 1867 otherlinkup = choice; 1868 choice = dl; 1869 } 1870 } else if (dl->state == DATALINK_CLOSED) { 1871 if (what == AUTO_UP) { 1872 choice = dl; 1873 break; 1874 } 1875 } else { 1876 /* An auto link in an intermediate state - forget it for the moment */ 1877 choice = NULL; 1878 break; 1879 } 1880 } else if (dl->state == DATALINK_OPEN && what == AUTO_DOWN) 1881 otherlinkup = dl; 1882 1883 if (choice) { 1884 if (what == AUTO_UP) { 1885 log_Printf(LogPHASE, "%d%% saturation -> Opening link ``%s''\n", 1886 percent, choice->name); 1887 datalink_Up(choice, 1, 1); 1888 mp_CheckAutoloadTimer(&bundle->ncp.mp); 1889 } else if (otherlinkup) { /* Only bring the second-last link down */ 1890 log_Printf(LogPHASE, "%d%% saturation -> Closing link ``%s''\n", 1891 percent, choice->name); 1892 datalink_Close(choice, CLOSE_STAYDOWN); 1893 mp_CheckAutoloadTimer(&bundle->ncp.mp); 1894 } 1895 } 1896} 1897 1898int 1899bundle_WantAutoloadTimer(struct bundle *bundle) 1900{ 1901 struct datalink *dl; 1902 int autolink, opened; 1903 1904 if (bundle->phase == PHASE_NETWORK) { 1905 for (autolink = opened = 0, dl = bundle->links; dl; dl = dl->next) 1906 if (dl->physical->type == PHYS_AUTO) { 1907 if (++autolink == 2 || (autolink == 1 && opened)) 1908 /* Two auto links or one auto and one open in NETWORK phase */ 1909 return 1; 1910 } else if (dl->state == DATALINK_OPEN) { 1911 opened++; 1912 if (autolink) 1913 /* One auto and one open link in NETWORK phase */ 1914 return 1; 1915 } 1916 } 1917 1918 return 0; 1919} 1920 1921void 1922bundle_ChangedPID(struct bundle *bundle) 1923{ 1924#ifdef TUNSIFPID 1925 ioctl(bundle->dev.fd, TUNSIFPID, 0); 1926#endif 1927} 1928 1929int 1930bundle_Uptime(struct bundle *bundle) 1931{ 1932 if (bundle->upat) 1933 return time(NULL) - bundle->upat; 1934 1935 return 0; 1936}
| 711 } 712#endif 713 if (errno != ENOENT || ++enoentcount > 2) { 714 err = errno; 715 break; 716 } 717 } else 718 err = errno; 719 } 720 721 if (bundle.dev.fd < 0) { 722 if (unit == -1) 723 log_Printf(LogWARN, "No available tunnel devices found (%s)\n", 724 strerror(err)); 725 else 726 log_Printf(LogWARN, "%s%d: %s\n", prefix, unit, strerror(err)); 727 return NULL; 728 } 729 730 log_SetTun(bundle.unit); 731 732 ifname = strrchr(bundle.dev.Name, '/'); 733 if (ifname == NULL) 734 ifname = bundle.dev.Name; 735 else 736 ifname++; 737 738 bundle.iface = iface_Create(ifname); 739 if (bundle.iface == NULL) { 740 close(bundle.dev.fd); 741 return NULL; 742 } 743 744#ifdef TUNSIFMODE 745 /* Make sure we're POINTOPOINT & IFF_MULTICAST */ 746 iff = IFF_POINTOPOINT | IFF_MULTICAST; 747 if (ID0ioctl(bundle.dev.fd, TUNSIFMODE, &iff) < 0) 748 log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFMODE): %s\n", 749 strerror(errno)); 750#endif 751 752#ifdef TUNSLMODE 753 /* Make sure we're not prepending sockaddrs */ 754 iff = 0; 755 if (ID0ioctl(bundle.dev.fd, TUNSLMODE, &iff) < 0) 756 log_Printf(LogERROR, "bundle_Create: ioctl(TUNSLMODE): %s\n", 757 strerror(errno)); 758#endif 759 760#ifdef TUNSIFHEAD 761 /* We want the address family please ! */ 762 iff = 1; 763 if (ID0ioctl(bundle.dev.fd, TUNSIFHEAD, &iff) < 0) { 764 log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFHEAD): %s\n", 765 strerror(errno)); 766 bundle.dev.header = 0; 767 } else 768 bundle.dev.header = 1; 769#else 770#ifdef __OpenBSD__ 771 /* Always present for OpenBSD */ 772 bundle.dev.header = 1; 773#else 774 /* 775 * If TUNSIFHEAD isn't available and we're not OpenBSD, assume 776 * everything's AF_INET (hopefully the tun device won't pass us 777 * anything else !). 778 */ 779 bundle.dev.header = 0; 780#endif 781#endif 782 783 log_Printf(LogPHASE, "Using interface: %s\n", ifname); 784 785 bundle.bandwidth = 0; 786 bundle.routing_seq = 0; 787 bundle.phase = PHASE_DEAD; 788 bundle.CleaningUp = 0; 789 bundle.NatEnabled = 0; 790 791 bundle.fsm.LayerStart = bundle_LayerStart; 792 bundle.fsm.LayerUp = bundle_LayerUp; 793 bundle.fsm.LayerDown = bundle_LayerDown; 794 bundle.fsm.LayerFinish = bundle_LayerFinish; 795 bundle.fsm.object = &bundle; 796 797 bundle.cfg.idle.timeout = NCP_IDLE_TIMEOUT; 798 bundle.cfg.idle.min_timeout = 0; 799 *bundle.cfg.auth.name = '\0'; 800 *bundle.cfg.auth.key = '\0'; 801 bundle.cfg.opt = OPT_IDCHECK | OPT_LOOPBACK | OPT_SROUTES | OPT_TCPMSSFIXUP | 802 OPT_THROUGHPUT | OPT_UTMP; 803#ifndef NOINET6 804 bundle.cfg.opt |= OPT_IPCP; 805 if (probe.ipv6_available) 806 bundle.cfg.opt |= OPT_IPV6CP; 807#endif 808 *bundle.cfg.label = '\0'; 809 bundle.cfg.ifqueue = DEF_IFQUEUE; 810 bundle.cfg.choked.timeout = CHOKED_TIMEOUT; 811 bundle.phys_type.all = type; 812 bundle.phys_type.open = 0; 813 bundle.upat = 0; 814 815 bundle.links = datalink_Create("deflink", &bundle, type); 816 if (bundle.links == NULL) { 817 log_Printf(LogALERT, "Cannot create data link: %s\n", strerror(errno)); 818 iface_Destroy(bundle.iface); 819 bundle.iface = NULL; 820 close(bundle.dev.fd); 821 return NULL; 822 } 823 824 bundle.desc.type = BUNDLE_DESCRIPTOR; 825 bundle.desc.UpdateSet = bundle_UpdateSet; 826 bundle.desc.IsSet = bundle_IsSet; 827 bundle.desc.Read = bundle_DescriptorRead; 828 bundle.desc.Write = bundle_DescriptorWrite; 829 830 ncp_Init(&bundle.ncp, &bundle); 831 832 memset(&bundle.filter, '\0', sizeof bundle.filter); 833 bundle.filter.in.fragok = bundle.filter.in.logok = 1; 834 bundle.filter.in.name = "IN"; 835 bundle.filter.out.fragok = bundle.filter.out.logok = 1; 836 bundle.filter.out.name = "OUT"; 837 bundle.filter.dial.name = "DIAL"; 838 bundle.filter.dial.logok = 1; 839 bundle.filter.alive.name = "ALIVE"; 840 bundle.filter.alive.logok = 1; 841 { 842 int i; 843 for (i = 0; i < MAXFILTERS; i++) { 844 bundle.filter.in.rule[i].f_action = A_NONE; 845 bundle.filter.out.rule[i].f_action = A_NONE; 846 bundle.filter.dial.rule[i].f_action = A_NONE; 847 bundle.filter.alive.rule[i].f_action = A_NONE; 848 } 849 } 850 memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer); 851 bundle.idle.done = 0; 852 bundle.notify.fd = -1; 853 memset(&bundle.choked.timer, '\0', sizeof bundle.choked.timer); 854#ifndef NORADIUS 855 radius_Init(&bundle.radius); 856#endif 857 858 /* Clean out any leftover crud */ 859 iface_Clear(bundle.iface, &bundle.ncp, 0, IFACE_CLEAR_ALL); 860 861 bundle_LockTun(&bundle); 862 863 return &bundle; 864} 865 866static void 867bundle_DownInterface(struct bundle *bundle) 868{ 869 route_IfDelete(bundle, 1); 870 iface_ClearFlags(bundle->iface->name, IFF_UP); 871} 872 873void 874bundle_Destroy(struct bundle *bundle) 875{ 876 struct datalink *dl; 877 878 /* 879 * Clean up the interface. We don't really need to do the timer_Stop()s, 880 * mp_Down(), iface_Clear() and bundle_DownInterface() unless we're getting 881 * out under exceptional conditions such as a descriptor exception. 882 */ 883 timer_Stop(&bundle->idle.timer); 884 timer_Stop(&bundle->choked.timer); 885 mp_Down(&bundle->ncp.mp); 886 iface_Clear(bundle->iface, &bundle->ncp, 0, IFACE_CLEAR_ALL); 887 bundle_DownInterface(bundle); 888 889#ifndef NORADIUS 890 /* Tell the radius server the bad news */ 891 radius_Destroy(&bundle->radius); 892#endif 893 894 /* Again, these are all DATALINK_CLOSED unless we're abending */ 895 dl = bundle->links; 896 while (dl) 897 dl = datalink_Destroy(dl); 898 899 ncp_Destroy(&bundle->ncp); 900 901 close(bundle->dev.fd); 902 bundle_UnlockTun(bundle); 903 904 /* In case we never made PHASE_NETWORK */ 905 bundle_Notify(bundle, EX_ERRDEAD); 906 907 iface_Destroy(bundle->iface); 908 bundle->iface = NULL; 909} 910 911void 912bundle_LinkClosed(struct bundle *bundle, struct datalink *dl) 913{ 914 /* 915 * Our datalink has closed. 916 * CleanDatalinks() (called from DoLoop()) will remove closed 917 * BACKGROUND, FOREGROUND and DIRECT links. 918 * If it's the last data link, enter phase DEAD. 919 * 920 * NOTE: dl may not be in our list (bundle_SendDatalink()) ! 921 */ 922 923 struct datalink *odl; 924 int other_links; 925 926 log_SetTtyCommandMode(dl); 927 928 other_links = 0; 929 for (odl = bundle->links; odl; odl = odl->next) 930 if (odl != dl && odl->state != DATALINK_CLOSED) 931 other_links++; 932 933 if (!other_links) { 934 if (dl->physical->type != PHYS_AUTO) /* Not in -auto mode */ 935 bundle_DownInterface(bundle); 936 ncp2initial(&bundle->ncp); 937 bundle_NewPhase(bundle, PHASE_DEAD); 938 bundle_StopIdleTimer(bundle); 939 } 940} 941 942void 943bundle_Open(struct bundle *bundle, const char *name, int mask, int force) 944{ 945 /* 946 * Please open the given datalink, or all if name == NULL 947 */ 948 struct datalink *dl; 949 950 for (dl = bundle->links; dl; dl = dl->next) 951 if (name == NULL || !strcasecmp(dl->name, name)) { 952 if ((mask & dl->physical->type) && 953 (dl->state == DATALINK_CLOSED || 954 (force && dl->state == DATALINK_OPENING && 955 dl->dial.timer.state == TIMER_RUNNING) || 956 dl->state == DATALINK_READY)) { 957 timer_Stop(&dl->dial.timer); /* We're finished with this */ 958 datalink_Up(dl, 1, 1); 959 if (mask & PHYS_AUTO) 960 break; /* Only one AUTO link at a time */ 961 } 962 if (name != NULL) 963 break; 964 } 965} 966 967struct datalink * 968bundle2datalink(struct bundle *bundle, const char *name) 969{ 970 struct datalink *dl; 971 972 if (name != NULL) { 973 for (dl = bundle->links; dl; dl = dl->next) 974 if (!strcasecmp(dl->name, name)) 975 return dl; 976 } else if (bundle->links && !bundle->links->next) 977 return bundle->links; 978 979 return NULL; 980} 981 982int 983bundle_ShowLinks(struct cmdargs const *arg) 984{ 985 struct datalink *dl; 986 struct pppThroughput *t; 987 unsigned long long octets; 988 int secs; 989 990 for (dl = arg->bundle->links; dl; dl = dl->next) { 991 octets = MAX(dl->physical->link.stats.total.in.OctetsPerSecond, 992 dl->physical->link.stats.total.out.OctetsPerSecond); 993 994 prompt_Printf(arg->prompt, "Name: %s [%s, %s]", 995 dl->name, mode2Nam(dl->physical->type), datalink_State(dl)); 996 if (dl->physical->link.stats.total.rolling && dl->state == DATALINK_OPEN) 997 prompt_Printf(arg->prompt, " bandwidth %d, %llu bps (%llu bytes/sec)", 998 dl->mp.bandwidth ? dl->mp.bandwidth : 999 physical_GetSpeed(dl->physical), 1000 octets * 8, octets); 1001 prompt_Printf(arg->prompt, "\n"); 1002 } 1003 1004 t = &arg->bundle->ncp.mp.link.stats.total; 1005 octets = MAX(t->in.OctetsPerSecond, t->out.OctetsPerSecond); 1006 secs = t->downtime ? 0 : throughput_uptime(t); 1007 if (secs > t->SamplePeriod) 1008 secs = t->SamplePeriod; 1009 if (secs) 1010 prompt_Printf(arg->prompt, "Currently averaging %llu bps (%llu bytes/sec)" 1011 " over the last %d secs\n", octets * 8, octets, secs); 1012 1013 return 0; 1014} 1015 1016static const char * 1017optval(struct bundle *bundle, int bit) 1018{ 1019 return (bundle->cfg.opt & bit) ? "enabled" : "disabled"; 1020} 1021 1022int 1023bundle_ShowStatus(struct cmdargs const *arg) 1024{ 1025 int remaining; 1026 1027 prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle)); 1028 prompt_Printf(arg->prompt, " Device: %s\n", arg->bundle->dev.Name); 1029 prompt_Printf(arg->prompt, " Interface: %s @ %lubps", 1030 arg->bundle->iface->name, arg->bundle->bandwidth); 1031 1032 if (arg->bundle->upat) { 1033 int secs = bundle_Uptime(arg->bundle); 1034 1035 prompt_Printf(arg->prompt, ", up time %d:%02d:%02d", secs / 3600, 1036 (secs / 60) % 60, secs % 60); 1037 } 1038 prompt_Printf(arg->prompt, "\n Queued: %lu of %u\n", 1039 (unsigned long)ncp_QueueLen(&arg->bundle->ncp), 1040 arg->bundle->cfg.ifqueue); 1041 1042 prompt_Printf(arg->prompt, "\nDefaults:\n"); 1043 prompt_Printf(arg->prompt, " Label: %s\n", 1044 arg->bundle->cfg.label); 1045 prompt_Printf(arg->prompt, " Auth name: %s\n", 1046 arg->bundle->cfg.auth.name); 1047 prompt_Printf(arg->prompt, " Diagnostic socket: "); 1048 if (*server.cfg.sockname != '\0') { 1049 prompt_Printf(arg->prompt, "%s", server.cfg.sockname); 1050 if (server.cfg.mask != (mode_t)-1) 1051 prompt_Printf(arg->prompt, ", mask 0%03o", (int)server.cfg.mask); 1052 prompt_Printf(arg->prompt, "%s\n", server.fd == -1 ? " (not open)" : ""); 1053 } else if (server.cfg.port != 0) 1054 prompt_Printf(arg->prompt, "TCP port %d%s\n", server.cfg.port, 1055 server.fd == -1 ? " (not open)" : ""); 1056 else 1057 prompt_Printf(arg->prompt, "none\n"); 1058 1059 prompt_Printf(arg->prompt, " Choked Timer: %ds\n", 1060 arg->bundle->cfg.choked.timeout); 1061 1062#ifndef NORADIUS 1063 radius_Show(&arg->bundle->radius, arg->prompt); 1064#endif 1065 1066 prompt_Printf(arg->prompt, " Idle Timer: "); 1067 if (arg->bundle->cfg.idle.timeout) { 1068 prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle.timeout); 1069 if (arg->bundle->cfg.idle.min_timeout) 1070 prompt_Printf(arg->prompt, ", min %ds", 1071 arg->bundle->cfg.idle.min_timeout); 1072 remaining = bundle_RemainingIdleTime(arg->bundle); 1073 if (remaining != -1) 1074 prompt_Printf(arg->prompt, " (%ds remaining)", remaining); 1075 prompt_Printf(arg->prompt, "\n"); 1076 } else 1077 prompt_Printf(arg->prompt, "disabled\n"); 1078 1079 prompt_Printf(arg->prompt, " Filter Decap: %-20.20s", 1080 optval(arg->bundle, OPT_FILTERDECAP)); 1081 prompt_Printf(arg->prompt, " ID check: %s\n", 1082 optval(arg->bundle, OPT_IDCHECK)); 1083 prompt_Printf(arg->prompt, " Iface-Alias: %-20.20s", 1084 optval(arg->bundle, OPT_IFACEALIAS)); 1085#ifndef NOINET6 1086 prompt_Printf(arg->prompt, " IPCP: %s\n", 1087 optval(arg->bundle, OPT_IPCP)); 1088 prompt_Printf(arg->prompt, " IPV6CP: %-20.20s", 1089 optval(arg->bundle, OPT_IPV6CP)); 1090#endif 1091 prompt_Printf(arg->prompt, " Keep-Session: %s\n", 1092 optval(arg->bundle, OPT_KEEPSESSION)); 1093 prompt_Printf(arg->prompt, " Loopback: %-20.20s", 1094 optval(arg->bundle, OPT_LOOPBACK)); 1095 prompt_Printf(arg->prompt, " PasswdAuth: %s\n", 1096 optval(arg->bundle, OPT_PASSWDAUTH)); 1097 prompt_Printf(arg->prompt, " Proxy: %-20.20s", 1098 optval(arg->bundle, OPT_PROXY)); 1099 prompt_Printf(arg->prompt, " Proxyall: %s\n", 1100 optval(arg->bundle, OPT_PROXYALL)); 1101 prompt_Printf(arg->prompt, " Sticky Routes: %-20.20s", 1102 optval(arg->bundle, OPT_SROUTES)); 1103 prompt_Printf(arg->prompt, " TCPMSS Fixup: %s\n", 1104 optval(arg->bundle, OPT_TCPMSSFIXUP)); 1105 prompt_Printf(arg->prompt, " Throughput: %-20.20s", 1106 optval(arg->bundle, OPT_THROUGHPUT)); 1107 prompt_Printf(arg->prompt, " Utmp Logging: %s\n", 1108 optval(arg->bundle, OPT_UTMP)); 1109 1110 return 0; 1111} 1112 1113static void 1114bundle_IdleTimeout(void *v) 1115{ 1116 struct bundle *bundle = (struct bundle *)v; 1117 1118 log_Printf(LogPHASE, "Idle timer expired\n"); 1119 bundle_StopIdleTimer(bundle); 1120 bundle_Close(bundle, NULL, CLOSE_STAYDOWN); 1121} 1122 1123/* 1124 * Start Idle timer. If timeout is reached, we call bundle_Close() to 1125 * close LCP and link. 1126 */ 1127void 1128bundle_StartIdleTimer(struct bundle *bundle, unsigned secs) 1129{ 1130 timer_Stop(&bundle->idle.timer); 1131 if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) != 1132 bundle->phys_type.open && bundle->cfg.idle.timeout) { 1133 time_t now = time(NULL); 1134 1135 if (secs == 0) 1136 secs = bundle->cfg.idle.timeout; 1137 1138 /* We want at least `secs' */ 1139 if (bundle->cfg.idle.min_timeout > secs && bundle->upat) { 1140 int up = now - bundle->upat; 1141 1142 if ((long long)bundle->cfg.idle.min_timeout - up > (long long)secs) 1143 /* Only increase from the current `remaining' value */ 1144 secs = bundle->cfg.idle.min_timeout - up; 1145 } 1146 bundle->idle.timer.func = bundle_IdleTimeout; 1147 bundle->idle.timer.name = "idle"; 1148 bundle->idle.timer.load = secs * SECTICKS; 1149 bundle->idle.timer.arg = bundle; 1150 timer_Start(&bundle->idle.timer); 1151 bundle->idle.done = now + secs; 1152 } 1153} 1154 1155void 1156bundle_SetIdleTimer(struct bundle *bundle, int timeout, int min_timeout) 1157{ 1158 bundle->cfg.idle.timeout = timeout; 1159 if (min_timeout >= 0) 1160 bundle->cfg.idle.min_timeout = min_timeout; 1161 if (ncp_LayersOpen(&bundle->ncp)) 1162 bundle_StartIdleTimer(bundle, 0); 1163} 1164 1165void 1166bundle_StopIdleTimer(struct bundle *bundle) 1167{ 1168 timer_Stop(&bundle->idle.timer); 1169 bundle->idle.done = 0; 1170} 1171 1172static int 1173bundle_RemainingIdleTime(struct bundle *bundle) 1174{ 1175 if (bundle->idle.done) 1176 return bundle->idle.done - time(NULL); 1177 return -1; 1178} 1179 1180int 1181bundle_IsDead(struct bundle *bundle) 1182{ 1183 return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp); 1184} 1185 1186static struct datalink * 1187bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl) 1188{ 1189 struct datalink **dlp; 1190 1191 for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next) 1192 if (*dlp == dl) { 1193 *dlp = dl->next; 1194 dl->next = NULL; 1195 bundle_LinksRemoved(bundle); 1196 return dl; 1197 } 1198 1199 return NULL; 1200} 1201 1202static void 1203bundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl) 1204{ 1205 struct datalink **dlp = &bundle->links; 1206 1207 while (*dlp) 1208 dlp = &(*dlp)->next; 1209 1210 *dlp = dl; 1211 dl->next = NULL; 1212 1213 bundle_LinkAdded(bundle, dl); 1214 mp_CheckAutoloadTimer(&bundle->ncp.mp); 1215} 1216 1217void 1218bundle_CleanDatalinks(struct bundle *bundle) 1219{ 1220 struct datalink **dlp = &bundle->links; 1221 int found = 0; 1222 1223 while (*dlp) 1224 if ((*dlp)->state == DATALINK_CLOSED && 1225 (*dlp)->physical->type & 1226 (PHYS_DIRECT|PHYS_BACKGROUND|PHYS_FOREGROUND)) { 1227 *dlp = datalink_Destroy(*dlp); 1228 found++; 1229 } else 1230 dlp = &(*dlp)->next; 1231 1232 if (found) 1233 bundle_LinksRemoved(bundle); 1234} 1235 1236int 1237bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl, 1238 const char *name) 1239{ 1240 if (bundle2datalink(bundle, name)) { 1241 log_Printf(LogWARN, "Clone: %s: name already exists\n", name); 1242 return 0; 1243 } 1244 1245 bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name)); 1246 return 1; 1247} 1248 1249void 1250bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl) 1251{ 1252 dl = bundle_DatalinkLinkout(bundle, dl); 1253 if (dl) 1254 datalink_Destroy(dl); 1255} 1256 1257void 1258bundle_SetLabel(struct bundle *bundle, const char *label) 1259{ 1260 if (label) 1261 strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1); 1262 else 1263 *bundle->cfg.label = '\0'; 1264} 1265 1266const char * 1267bundle_GetLabel(struct bundle *bundle) 1268{ 1269 return *bundle->cfg.label ? bundle->cfg.label : NULL; 1270} 1271 1272int 1273bundle_LinkSize() 1274{ 1275 struct iovec iov[SCATTER_SEGMENTS]; 1276 int niov, expect, f; 1277 1278 iov[0].iov_len = strlen(Version) + 1; 1279 iov[0].iov_base = NULL; 1280 niov = 1; 1281 if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) { 1282 log_Printf(LogERROR, "Cannot determine space required for link\n"); 1283 return 0; 1284 } 1285 1286 for (f = expect = 0; f < niov; f++) 1287 expect += iov[f].iov_len; 1288 1289 return expect; 1290} 1291 1292void 1293bundle_ReceiveDatalink(struct bundle *bundle, int s) 1294{ 1295 char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int) * SEND_MAXFD]; 1296 int niov, expect, f, *fd, nfd, onfd, got; 1297 struct iovec iov[SCATTER_SEGMENTS]; 1298 struct cmsghdr *cmsg; 1299 struct msghdr msg; 1300 struct datalink *dl; 1301 pid_t pid; 1302 1303 log_Printf(LogPHASE, "Receiving datalink\n"); 1304 1305 /* 1306 * Create our scatter/gather array - passing NULL gets the space 1307 * allocation requirement rather than actually flattening the 1308 * structures. 1309 */ 1310 iov[0].iov_len = strlen(Version) + 1; 1311 iov[0].iov_base = NULL; 1312 niov = 1; 1313 if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) { 1314 log_Printf(LogERROR, "Cannot determine space required for link\n"); 1315 return; 1316 } 1317 1318 /* Allocate the scatter/gather array for recvmsg() */ 1319 for (f = expect = 0; f < niov; f++) { 1320 if ((iov[f].iov_base = malloc(iov[f].iov_len)) == NULL) { 1321 log_Printf(LogERROR, "Cannot allocate space to receive link\n"); 1322 return; 1323 } 1324 if (f) 1325 expect += iov[f].iov_len; 1326 } 1327 1328 /* Set up our message */ 1329 cmsg = (struct cmsghdr *)cmsgbuf; 1330 cmsg->cmsg_len = sizeof cmsgbuf; 1331 cmsg->cmsg_level = SOL_SOCKET; 1332 cmsg->cmsg_type = 0; 1333 1334 memset(&msg, '\0', sizeof msg); 1335 msg.msg_name = NULL; 1336 msg.msg_namelen = 0; 1337 msg.msg_iov = iov; 1338 msg.msg_iovlen = 1; /* Only send the version at the first pass */ 1339 msg.msg_control = cmsgbuf; 1340 msg.msg_controllen = sizeof cmsgbuf; 1341 1342 log_Printf(LogDEBUG, "Expecting %u scatter/gather bytes\n", 1343 (unsigned)iov[0].iov_len); 1344 1345 if ((got = recvmsg(s, &msg, MSG_WAITALL)) != iov[0].iov_len) { 1346 if (got == -1) 1347 log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno)); 1348 else 1349 log_Printf(LogERROR, "Failed recvmsg: Got %d, not %u\n", 1350 got, (unsigned)iov[0].iov_len); 1351 while (niov--) 1352 free(iov[niov].iov_base); 1353 return; 1354 } 1355 1356 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { 1357 log_Printf(LogERROR, "Recvmsg: no descriptors received !\n"); 1358 while (niov--) 1359 free(iov[niov].iov_base); 1360 return; 1361 } 1362 1363 fd = (int *)CMSG_DATA(cmsg); 1364 nfd = ((caddr_t)cmsg + cmsg->cmsg_len - (caddr_t)fd) / sizeof(int); 1365 1366 if (nfd < 2) { 1367 log_Printf(LogERROR, "Recvmsg: %d descriptor%s received (too few) !\n", 1368 nfd, nfd == 1 ? "" : "s"); 1369 while (nfd--) 1370 close(fd[nfd]); 1371 while (niov--) 1372 free(iov[niov].iov_base); 1373 return; 1374 } 1375 1376 /* 1377 * We've successfully received two or more open file descriptors 1378 * through our socket, plus a version string. Make sure it's the 1379 * correct version, and drop the connection if it's not. 1380 */ 1381 if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) { 1382 log_Printf(LogWARN, "Cannot receive datalink, incorrect version" 1383 " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len, 1384 (char *)iov[0].iov_base, Version); 1385 while (nfd--) 1386 close(fd[nfd]); 1387 while (niov--) 1388 free(iov[niov].iov_base); 1389 return; 1390 } 1391 1392 /* 1393 * Everything looks good. Send the other side our process id so that 1394 * they can transfer lock ownership, and wait for them to send the 1395 * actual link data. 1396 */ 1397 pid = getpid(); 1398 if ((got = write(fd[1], &pid, sizeof pid)) != sizeof pid) { 1399 if (got == -1) 1400 log_Printf(LogERROR, "Failed write: %s\n", strerror(errno)); 1401 else 1402 log_Printf(LogERROR, "Failed write: Got %d, not %d\n", got, 1403 (int)(sizeof pid)); 1404 while (nfd--) 1405 close(fd[nfd]); 1406 while (niov--) 1407 free(iov[niov].iov_base); 1408 return; 1409 } 1410 1411 if ((got = readv(fd[1], iov + 1, niov - 1)) != expect) { 1412 if (got == -1) 1413 log_Printf(LogERROR, "Failed write: %s\n", strerror(errno)); 1414 else 1415 log_Printf(LogERROR, "Failed write: Got %d, not %d\n", got, expect); 1416 while (nfd--) 1417 close(fd[nfd]); 1418 while (niov--) 1419 free(iov[niov].iov_base); 1420 return; 1421 } 1422 close(fd[1]); 1423 1424 onfd = nfd; /* We've got this many in our array */ 1425 nfd -= 2; /* Don't include p->fd and our reply descriptor */ 1426 niov = 1; /* Skip the version id */ 1427 dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, fd[0], 1428 fd + 2, &nfd); 1429 if (dl) { 1430 1431 if (nfd) { 1432 log_Printf(LogERROR, "bundle_ReceiveDatalink: Failed to handle %d " 1433 "auxiliary file descriptors (%d remain)\n", onfd, nfd); 1434 datalink_Destroy(dl); 1435 while (nfd--) 1436 close(fd[onfd--]); 1437 close(fd[0]); 1438 } else { 1439 bundle_DatalinkLinkin(bundle, dl); 1440 datalink_AuthOk(dl); 1441 bundle_CalculateBandwidth(dl->bundle); 1442 } 1443 } else { 1444 while (nfd--) 1445 close(fd[onfd--]); 1446 close(fd[0]); 1447 close(fd[1]); 1448 } 1449 1450 free(iov[0].iov_base); 1451} 1452 1453void 1454bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun) 1455{ 1456 char cmsgbuf[CMSG_SPACE(sizeof(int) * SEND_MAXFD)]; 1457 const char *constlock; 1458 char *lock; 1459 struct cmsghdr *cmsg; 1460 struct msghdr msg; 1461 struct iovec iov[SCATTER_SEGMENTS]; 1462 int niov, f, expect, newsid, fd[SEND_MAXFD], nfd, reply[2], got; 1463 pid_t newpid; 1464 1465 log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name); 1466 1467 /* Record the base device name for a lock transfer later */ 1468 constlock = physical_LockedDevice(dl->physical); 1469 if (constlock) { 1470 lock = alloca(strlen(constlock) + 1); 1471 strcpy(lock, constlock); 1472 } else 1473 lock = NULL; 1474 1475 bundle_LinkClosed(dl->bundle, dl); 1476 bundle_DatalinkLinkout(dl->bundle, dl); 1477 1478 /* Build our scatter/gather array */ 1479 iov[0].iov_len = strlen(Version) + 1; 1480 iov[0].iov_base = strdup(Version); 1481 niov = 1; 1482 nfd = 0; 1483 1484 fd[0] = datalink2iov(dl, iov, &niov, SCATTER_SEGMENTS, fd + 2, &nfd); 1485 1486 if (fd[0] != -1 && socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, reply) != -1) { 1487 /* 1488 * fd[1] is used to get the peer process id back, then to confirm that 1489 * we've transferred any device locks to that process id. 1490 */ 1491 fd[1] = reply[1]; 1492 1493 nfd += 2; /* Include fd[0] and fd[1] */ 1494 memset(&msg, '\0', sizeof msg); 1495 1496 msg.msg_name = NULL; 1497 msg.msg_namelen = 0; 1498 /* 1499 * Only send the version to start... We used to send the whole lot, but 1500 * this caused problems with our RECVBUF size as a single link is about 1501 * 22k ! This way, we should bump into no limits. 1502 */ 1503 msg.msg_iovlen = 1; 1504 msg.msg_iov = iov; 1505 msg.msg_control = cmsgbuf; 1506 msg.msg_controllen = CMSG_SPACE(sizeof(int) * nfd); 1507 msg.msg_flags = 0; 1508 1509 cmsg = (struct cmsghdr *)cmsgbuf; 1510 cmsg->cmsg_len = msg.msg_controllen; 1511 cmsg->cmsg_level = SOL_SOCKET; 1512 cmsg->cmsg_type = SCM_RIGHTS; 1513 1514 for (f = 0; f < nfd; f++) 1515 *((int *)CMSG_DATA(cmsg) + f) = fd[f]; 1516 1517 for (f = 1, expect = 0; f < niov; f++) 1518 expect += iov[f].iov_len; 1519 1520 if (setsockopt(reply[0], SOL_SOCKET, SO_SNDBUF, &expect, sizeof(int)) == -1) 1521 log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect, 1522 strerror(errno)); 1523 if (setsockopt(reply[1], SOL_SOCKET, SO_RCVBUF, &expect, sizeof(int)) == -1) 1524 log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect, 1525 strerror(errno)); 1526 1527 log_Printf(LogDEBUG, "Sending %d descriptor%s and %u bytes in scatter" 1528 "/gather array\n", nfd, nfd == 1 ? "" : "s", 1529 (unsigned)iov[0].iov_len); 1530 1531 if ((got = sendmsg(s, &msg, 0)) == -1) 1532 log_Printf(LogERROR, "Failed sendmsg: %s: %s\n", 1533 sun->sun_path, strerror(errno)); 1534 else if (got != iov[0].iov_len) 1535 log_Printf(LogERROR, "%s: Failed initial sendmsg: Only sent %d of %u\n", 1536 sun->sun_path, got, (unsigned)iov[0].iov_len); 1537 else { 1538 /* We must get the ACK before closing the descriptor ! */ 1539 int res; 1540 1541 if ((got = read(reply[0], &newpid, sizeof newpid)) == sizeof newpid) { 1542 log_Printf(LogDEBUG, "Received confirmation from pid %d\n", 1543 (int)newpid); 1544 if (lock && (res = ID0uu_lock_txfr(lock, newpid)) != UU_LOCK_OK) 1545 log_Printf(LogERROR, "uu_lock_txfr: %s\n", uu_lockerr(res)); 1546 1547 log_Printf(LogDEBUG, "Transmitting link (%d bytes)\n", expect); 1548 if ((got = writev(reply[0], iov + 1, niov - 1)) != expect) { 1549 if (got == -1) 1550 log_Printf(LogERROR, "%s: Failed writev: %s\n", 1551 sun->sun_path, strerror(errno)); 1552 else 1553 log_Printf(LogERROR, "%s: Failed writev: Wrote %d of %d\n", 1554 sun->sun_path, got, expect); 1555 } 1556 } else if (got == -1) 1557 log_Printf(LogERROR, "%s: Failed socketpair read: %s\n", 1558 sun->sun_path, strerror(errno)); 1559 else 1560 log_Printf(LogERROR, "%s: Failed socketpair read: Got %d of %d\n", 1561 sun->sun_path, got, (int)(sizeof newpid)); 1562 } 1563 1564 close(reply[0]); 1565 close(reply[1]); 1566 1567 newsid = Enabled(dl->bundle, OPT_KEEPSESSION) || 1568 tcgetpgrp(fd[0]) == getpgrp(); 1569 while (nfd) 1570 close(fd[--nfd]); 1571 if (newsid) 1572 bundle_setsid(dl->bundle, got != -1); 1573 } 1574 close(s); 1575 1576 while (niov--) 1577 free(iov[niov].iov_base); 1578} 1579 1580int 1581bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl, 1582 const char *name) 1583{ 1584 struct datalink *dl; 1585 1586 if (!strcasecmp(ndl->name, name)) 1587 return 1; 1588 1589 for (dl = bundle->links; dl; dl = dl->next) 1590 if (!strcasecmp(dl->name, name)) 1591 return 0; 1592 1593 datalink_Rename(ndl, name); 1594 return 1; 1595} 1596 1597int 1598bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode) 1599{ 1600 int omode; 1601 1602 omode = dl->physical->type; 1603 if (omode == mode) 1604 return 1; 1605 1606 if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO)) 1607 /* First auto link */ 1608 if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) { 1609 log_Printf(LogWARN, "You must `set ifaddr' or `open' before" 1610 " changing mode to %s\n", mode2Nam(mode)); 1611 return 0; 1612 } 1613 1614 if (!datalink_SetMode(dl, mode)) 1615 return 0; 1616 1617 if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO) && 1618 bundle->phase != PHASE_NETWORK) 1619 /* First auto link, we need an interface */ 1620 ipcp_InterfaceUp(&bundle->ncp.ipcp); 1621 1622 /* Regenerate phys_type and adjust idle timer */ 1623 bundle_LinksRemoved(bundle); 1624 1625 return 1; 1626} 1627 1628void 1629bundle_setsid(struct bundle *bundle, int holdsession) 1630{ 1631 /* 1632 * Lose the current session. This means getting rid of our pid 1633 * too so that the tty device will really go away, and any getty 1634 * etc will be allowed to restart. 1635 */ 1636 pid_t pid, orig; 1637 int fds[2]; 1638 char done; 1639 struct datalink *dl; 1640 1641 if (!holdsession && bundle_IsDead(bundle)) { 1642 /* 1643 * No need to lose our session after all... we're going away anyway 1644 * 1645 * We should really stop the timer and pause if holdsession is set and 1646 * the bundle's dead, but that leaves other resources lying about :-( 1647 */ 1648 return; 1649 } 1650 1651 orig = getpid(); 1652 if (pipe(fds) == -1) { 1653 log_Printf(LogERROR, "pipe: %s\n", strerror(errno)); 1654 return; 1655 } 1656 switch ((pid = fork())) { 1657 case -1: 1658 log_Printf(LogERROR, "fork: %s\n", strerror(errno)); 1659 close(fds[0]); 1660 close(fds[1]); 1661 return; 1662 case 0: 1663 close(fds[1]); 1664 read(fds[0], &done, 1); /* uu_locks are mine ! */ 1665 close(fds[0]); 1666 if (pipe(fds) == -1) { 1667 log_Printf(LogERROR, "pipe(2): %s\n", strerror(errno)); 1668 return; 1669 } 1670 switch ((pid = fork())) { 1671 case -1: 1672 log_Printf(LogERROR, "fork(2): %s\n", strerror(errno)); 1673 close(fds[0]); 1674 close(fds[1]); 1675 return; 1676 case 0: 1677 close(fds[1]); 1678 bundle_LockTun(bundle); /* update pid */ 1679 read(fds[0], &done, 1); /* uu_locks are mine ! */ 1680 close(fds[0]); 1681 setsid(); 1682 bundle_ChangedPID(bundle); 1683 log_Printf(LogDEBUG, "%d -> %d: %s session control\n", 1684 (int)orig, (int)getpid(), 1685 holdsession ? "Passed" : "Dropped"); 1686 timer_InitService(0); /* Start the Timer Service */ 1687 break; 1688 default: 1689 close(fds[0]); 1690 /* Give away all our physical locks (to the final process) */ 1691 for (dl = bundle->links; dl; dl = dl->next) 1692 if (dl->state != DATALINK_CLOSED) 1693 physical_ChangedPid(dl->physical, pid); 1694 write(fds[1], "!", 1); /* done */ 1695 close(fds[1]); 1696 _exit(0); 1697 break; 1698 } 1699 break; 1700 default: 1701 close(fds[0]); 1702 /* Give away all our physical locks (to the intermediate process) */ 1703 for (dl = bundle->links; dl; dl = dl->next) 1704 if (dl->state != DATALINK_CLOSED) 1705 physical_ChangedPid(dl->physical, pid); 1706 write(fds[1], "!", 1); /* done */ 1707 close(fds[1]); 1708 if (holdsession) { 1709 int fd, status; 1710 1711 timer_TermService(); 1712 signal(SIGPIPE, SIG_DFL); 1713 signal(SIGALRM, SIG_DFL); 1714 signal(SIGHUP, SIG_DFL); 1715 signal(SIGTERM, SIG_DFL); 1716 signal(SIGINT, SIG_DFL); 1717 signal(SIGQUIT, SIG_DFL); 1718 for (fd = getdtablesize(); fd >= 0; fd--) 1719 close(fd); 1720 /* 1721 * Reap the intermediate process. As we're not exiting but the 1722 * intermediate is, we don't want it to become defunct. 1723 */ 1724 waitpid(pid, &status, 0); 1725 /* Tweak our process arguments.... */ 1726 SetTitle("session owner"); 1727#ifndef NOSUID 1728 setuid(ID0realuid()); 1729#endif 1730 /* 1731 * Hang around for a HUP. This should happen as soon as the 1732 * ppp that we passed our ctty descriptor to closes it. 1733 * NOTE: If this process dies, the passed descriptor becomes 1734 * invalid and will give a select() error by setting one 1735 * of the error fds, aborting the other ppp. We don't 1736 * want that to happen ! 1737 */ 1738 pause(); 1739 } 1740 _exit(0); 1741 break; 1742 } 1743} 1744 1745int 1746bundle_HighestState(struct bundle *bundle) 1747{ 1748 struct datalink *dl; 1749 int result = DATALINK_CLOSED; 1750 1751 for (dl = bundle->links; dl; dl = dl->next) 1752 if (result < dl->state) 1753 result = dl->state; 1754 1755 return result; 1756} 1757 1758int 1759bundle_Exception(struct bundle *bundle, int fd) 1760{ 1761 struct datalink *dl; 1762 1763 for (dl = bundle->links; dl; dl = dl->next) 1764 if (dl->physical->fd == fd) { 1765 datalink_Down(dl, CLOSE_NORMAL); 1766 return 1; 1767 } 1768 1769 return 0; 1770} 1771 1772void 1773bundle_AdjustFilters(struct bundle *bundle, struct ncpaddr *local, 1774 struct ncpaddr *remote) 1775{ 1776 filter_AdjustAddr(&bundle->filter.in, local, remote, NULL); 1777 filter_AdjustAddr(&bundle->filter.out, local, remote, NULL); 1778 filter_AdjustAddr(&bundle->filter.dial, local, remote, NULL); 1779 filter_AdjustAddr(&bundle->filter.alive, local, remote, NULL); 1780} 1781 1782void 1783bundle_AdjustDNS(struct bundle *bundle) 1784{ 1785 struct in_addr *dns = bundle->ncp.ipcp.ns.dns; 1786 1787 filter_AdjustAddr(&bundle->filter.in, NULL, NULL, dns); 1788 filter_AdjustAddr(&bundle->filter.out, NULL, NULL, dns); 1789 filter_AdjustAddr(&bundle->filter.dial, NULL, NULL, dns); 1790 filter_AdjustAddr(&bundle->filter.alive, NULL, NULL, dns); 1791} 1792 1793void 1794bundle_CalculateBandwidth(struct bundle *bundle) 1795{ 1796 struct datalink *dl; 1797 int sp, overhead, maxoverhead; 1798 1799 bundle->bandwidth = 0; 1800 bundle->iface->mtu = 0; 1801 maxoverhead = 0; 1802 1803 for (dl = bundle->links; dl; dl = dl->next) { 1804 overhead = ccp_MTUOverhead(&dl->physical->link.ccp); 1805 if (maxoverhead < overhead) 1806 maxoverhead = overhead; 1807 if (dl->state == DATALINK_OPEN) { 1808 if ((sp = dl->mp.bandwidth) == 0 && 1809 (sp = physical_GetSpeed(dl->physical)) == 0) 1810 log_Printf(LogDEBUG, "%s: %s: Cannot determine bandwidth\n", 1811 dl->name, dl->physical->name.full); 1812 else 1813 bundle->bandwidth += sp; 1814 if (!bundle->ncp.mp.active) { 1815 bundle->iface->mtu = dl->physical->link.lcp.his_mru; 1816 break; 1817 } 1818 } 1819 } 1820 1821 if(bundle->bandwidth == 0) 1822 bundle->bandwidth = 115200; /* Shrug */ 1823 1824 if (bundle->ncp.mp.active) { 1825 bundle->iface->mtu = bundle->ncp.mp.peer_mrru; 1826 overhead = ccp_MTUOverhead(&bundle->ncp.mp.link.ccp); 1827 if (maxoverhead < overhead) 1828 maxoverhead = overhead; 1829 } else if (!bundle->iface->mtu) 1830 bundle->iface->mtu = DEF_MRU; 1831 1832#ifndef NORADIUS 1833 if (bundle->radius.valid && bundle->radius.mtu && 1834 bundle->radius.mtu < bundle->iface->mtu) { 1835 log_Printf(LogLCP, "Reducing MTU to radius value %lu\n", 1836 bundle->radius.mtu); 1837 bundle->iface->mtu = bundle->radius.mtu; 1838 } 1839#endif 1840 1841 if (maxoverhead) { 1842 log_Printf(LogLCP, "Reducing MTU from %d to %d (CCP requirement)\n", 1843 bundle->iface->mtu, bundle->iface->mtu - maxoverhead); 1844 bundle->iface->mtu -= maxoverhead; 1845 } 1846 1847 tun_configure(bundle); 1848 1849 route_UpdateMTU(bundle); 1850} 1851 1852void 1853bundle_AutoAdjust(struct bundle *bundle, int percent, int what) 1854{ 1855 struct datalink *dl, *choice, *otherlinkup; 1856 1857 choice = otherlinkup = NULL; 1858 for (dl = bundle->links; dl; dl = dl->next) 1859 if (dl->physical->type == PHYS_AUTO) { 1860 if (dl->state == DATALINK_OPEN) { 1861 if (what == AUTO_DOWN) { 1862 if (choice) 1863 otherlinkup = choice; 1864 choice = dl; 1865 } 1866 } else if (dl->state == DATALINK_CLOSED) { 1867 if (what == AUTO_UP) { 1868 choice = dl; 1869 break; 1870 } 1871 } else { 1872 /* An auto link in an intermediate state - forget it for the moment */ 1873 choice = NULL; 1874 break; 1875 } 1876 } else if (dl->state == DATALINK_OPEN && what == AUTO_DOWN) 1877 otherlinkup = dl; 1878 1879 if (choice) { 1880 if (what == AUTO_UP) { 1881 log_Printf(LogPHASE, "%d%% saturation -> Opening link ``%s''\n", 1882 percent, choice->name); 1883 datalink_Up(choice, 1, 1); 1884 mp_CheckAutoloadTimer(&bundle->ncp.mp); 1885 } else if (otherlinkup) { /* Only bring the second-last link down */ 1886 log_Printf(LogPHASE, "%d%% saturation -> Closing link ``%s''\n", 1887 percent, choice->name); 1888 datalink_Close(choice, CLOSE_STAYDOWN); 1889 mp_CheckAutoloadTimer(&bundle->ncp.mp); 1890 } 1891 } 1892} 1893 1894int 1895bundle_WantAutoloadTimer(struct bundle *bundle) 1896{ 1897 struct datalink *dl; 1898 int autolink, opened; 1899 1900 if (bundle->phase == PHASE_NETWORK) { 1901 for (autolink = opened = 0, dl = bundle->links; dl; dl = dl->next) 1902 if (dl->physical->type == PHYS_AUTO) { 1903 if (++autolink == 2 || (autolink == 1 && opened)) 1904 /* Two auto links or one auto and one open in NETWORK phase */ 1905 return 1; 1906 } else if (dl->state == DATALINK_OPEN) { 1907 opened++; 1908 if (autolink) 1909 /* One auto and one open link in NETWORK phase */ 1910 return 1; 1911 } 1912 } 1913 1914 return 0; 1915} 1916 1917void 1918bundle_ChangedPID(struct bundle *bundle) 1919{ 1920#ifdef TUNSIFPID 1921 ioctl(bundle->dev.fd, TUNSIFPID, 0); 1922#endif 1923} 1924 1925int 1926bundle_Uptime(struct bundle *bundle) 1927{ 1928 if (bundle->upat) 1929 return time(NULL) - bundle->upat; 1930 1931 return 0; 1932}
|