2 * Written by Eivind Eklund <eivind@yes.no>
5 * Copyright (C) 1998, Yes Interactive. All rights reserved.
7 * Redistribution and use in any form is permitted. Redistribution in
8 * source form should include the above copyright and this set of
9 * conditions, because large sections american law seems to have been
10 * created by a bunch of jerks on drugs that are now illegal, forcing
11 * me to include this copyright-stuff instead of placing this in the
12 * public domain. The name of of 'Yes Interactive' or 'Eivind Eklund'
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 * $FreeBSD: src/usr.sbin/ppp/physical.c,v 1.34.2.8 2002/09/01 02:12:29 brian Exp $
20 * $DragonFly: src/usr.sbin/ppp/physical.c,v 1.3 2005/11/24 23:42:54 swildner Exp $
24 #include <sys/param.h>
25 #include <netinet/in.h>
26 #include <netinet/in_systm.h>
27 #include <netinet/ip.h>
28 #include <sys/socket.h>
38 #include <sys/tty.h> /* TIOCOUTQ */
43 #if defined(__OpenBSD__) || defined(__NetBSD__)
44 #include <sys/ioctl.h>
67 #include "throughput.h"
71 #include "slcompress.h"
75 #include "descriptor.h"
101 #include "netgraph.h"
108 #define PPPOTCPLINE "ppp"
110 static int physical_DescriptorWrite(struct fdescriptor
*, struct bundle
*,
114 physical_DeviceSize(void)
116 return sizeof(struct device
);
120 struct device
*(*create
)(struct physical
*);
121 struct device
*(*iov2device
)(int, struct physical
*, struct iovec
*,
122 int *, int, int *, int *);
123 int (*DeviceSize
)(void);
127 * This must come before ``tty'' so that the probe routine is
128 * able to identify it as a more specific type of terminal device.
130 { i4b_Create
, i4b_iov2device
, i4b_DeviceSize
},
132 { tty_Create
, tty_iov2device
, tty_DeviceSize
},
135 * This must come before ``udp'' so that the probe routine is
136 * able to identify it as a more specific type of SOCK_DGRAM.
138 { ether_Create
, ether_iov2device
, ether_DeviceSize
},
139 #ifdef EXPERIMENTAL_NETGRAPH
140 { ng_Create
, ng_iov2device
, ng_DeviceSize
},
144 /* Ditto for ATM devices */
145 { atm_Create
, atm_iov2device
, atm_DeviceSize
},
147 { tcp_Create
, tcp_iov2device
, tcp_DeviceSize
},
148 { udp_Create
, udp_iov2device
, udp_DeviceSize
},
149 { exec_Create
, exec_iov2device
, exec_DeviceSize
}
152 #define NDEVICES (sizeof devices / sizeof devices[0])
155 physical_UpdateSet(struct fdescriptor
*d
, fd_set
*r
, fd_set
*w
, fd_set
*e
,
158 return physical_doUpdateSet(d
, r
, w
, e
, n
, 0);
162 physical_SetDescriptor(struct physical
*p
)
164 p
->desc
.type
= PHYSICAL_DESCRIPTOR
;
165 p
->desc
.UpdateSet
= physical_UpdateSet
;
166 p
->desc
.IsSet
= physical_IsSet
;
167 p
->desc
.Read
= physical_DescriptorRead
;
168 p
->desc
.Write
= physical_DescriptorWrite
;
172 physical_Create(struct datalink
*dl
, int type
)
176 p
= (struct physical
*)malloc(sizeof(struct physical
));
180 p
->link
.type
= PHYSICAL_LINK
;
181 p
->link
.name
= dl
->name
;
182 p
->link
.len
= sizeof *p
;
184 /* The sample period is fixed - see physical2iov() & iov2physical() */
185 throughput_init(&p
->link
.stats
.total
, SAMPLE_PERIOD
);
186 p
->link
.stats
.parent
= dl
->bundle
->ncp
.mp
.active
?
187 &dl
->bundle
->ncp
.mp
.link
.stats
.total
: NULL
;
188 p
->link
.stats
.gather
= 1;
190 memset(p
->link
.Queue
, '\0', sizeof p
->link
.Queue
);
191 memset(p
->link
.proto_in
, '\0', sizeof p
->link
.proto_in
);
192 memset(p
->link
.proto_out
, '\0', sizeof p
->link
.proto_out
);
193 link_EmptyStack(&p
->link
);
196 physical_SetDescriptor(p
);
199 hdlc_Init(&p
->hdlc
, &p
->link
.lcp
);
200 async_Init(&p
->async
);
204 p
->connect_count
= 0;
207 *p
->name
.full
= '\0';
208 p
->name
.base
= p
->name
.full
;
211 p
->session_owner
= (pid_t
)-1;
213 p
->cfg
.rts_cts
= MODEM_CTSRTS
;
214 p
->cfg
.speed
= MODEM_SPEED
;
216 memcpy(p
->cfg
.devlist
, MODEM_LIST
, sizeof MODEM_LIST
);
217 p
->cfg
.ndev
= NMODEMS
;
218 p
->cfg
.cd
.necessity
= CD_DEFAULT
;
219 p
->cfg
.cd
.delay
= 0; /* reconfigured or device specific default */
221 lcp_Init(&p
->link
.lcp
, dl
->bundle
, &p
->link
, &dl
->fsmp
);
222 ccp_Init(&p
->link
.ccp
, dl
->bundle
, &p
->link
, &dl
->fsmp
);
227 static const struct parity
{
232 { "even", "P_EVEN", CS7
| PARENB
},
233 { "odd", "P_ODD", CS7
| PARENB
| PARODD
},
234 { "none", "P_ZERO", CS8
},
239 GetParityValue(const char *str
)
241 const struct parity
*pp
;
243 for (pp
= validparity
; pp
->name
; pp
++) {
244 if (strcasecmp(pp
->name
, str
) == 0 ||
245 strcasecmp(pp
->name1
, str
) == 0) {
253 physical_SetParity(struct physical
*p
, const char *str
)
255 struct termios rstio
;
258 val
= GetParityValue(str
);
262 tcgetattr(p
->fd
, &rstio
);
263 rstio
.c_cflag
&= ~(CSIZE
| PARODD
| PARENB
);
264 rstio
.c_cflag
|= val
;
265 tcsetattr(p
->fd
, TCSADRAIN
, &rstio
);
269 log_Printf(LogWARN
, "%s: %s: Invalid parity\n", p
->link
.name
, str
);
274 physical_GetSpeed(struct physical
*p
)
276 if (p
->handler
&& p
->handler
->speed
)
277 return (*p
->handler
->speed
)(p
);
283 physical_SetSpeed(struct physical
*p
, int speed
)
285 if (IntToSpeed(speed
) != B0
) {
286 p
->cfg
.speed
= speed
;
294 physical_Raw(struct physical
*p
)
296 if (p
->handler
&& p
->handler
->raw
)
297 return (*p
->handler
->raw
)(p
);
303 physical_Offline(struct physical
*p
)
305 if (p
->handler
&& p
->handler
->offline
)
306 (*p
->handler
->offline
)(p
);
307 log_Printf(LogPHASE
, "%s: Disconnected!\n", p
->link
.name
);
311 physical_Lock(struct physical
*p
)
315 if (*p
->name
.full
== '/' && p
->type
!= PHYS_DIRECT
&&
316 (res
= ID0uu_lock(p
->name
.base
)) != UU_LOCK_OK
) {
317 if (res
== UU_LOCK_INUSE
)
318 log_Printf(LogPHASE
, "%s: %s is in use\n", p
->link
.name
, p
->name
.full
);
320 log_Printf(LogPHASE
, "%s: %s is in use: uu_lock: %s\n",
321 p
->link
.name
, p
->name
.full
, uu_lockerr(res
));
329 physical_Unlock(struct physical
*p
)
331 if (*p
->name
.full
== '/' && p
->type
!= PHYS_DIRECT
&&
332 ID0uu_unlock(p
->name
.base
) == -1)
333 log_Printf(LogALERT
, "%s: Can't uu_unlock %s\n", p
->link
.name
,
338 physical_Close(struct physical
*p
)
346 log_Printf(LogDEBUG
, "%s: Close\n", p
->link
.name
);
348 if (p
->handler
&& p
->handler
->cooked
)
349 (*p
->handler
->cooked
)(p
);
351 physical_StopDeviceTimer(p
);
353 if (p
->handler
&& (p
->handler
->type
== TCP_DEVICE
||
354 p
->handler
->type
== UDP_DEVICE
))
355 /* Careful - we logged in on line ``ppp'' with IP as our host */
356 ID0logout(PPPOTCPLINE
, 1);
358 ID0logout(p
->name
.base
, 0);
361 newsid
= tcgetpgrp(p
->fd
) == getpgrp();
364 log_SetTtyCommandMode(p
->dl
);
366 throughput_stop(&p
->link
.stats
.total
);
367 throughput_log(&p
->link
.stats
.total
, LogPHASE
, p
->link
.name
);
369 if (p
->session_owner
!= (pid_t
)-1) {
370 log_Printf(LogPHASE
, "%s: HUPing %ld\n", p
->link
.name
,
371 (long)p
->session_owner
);
372 ID0kill(p
->session_owner
, SIGHUP
);
373 p
->session_owner
= (pid_t
)-1;
377 bundle_setsid(p
->dl
->bundle
, 0);
379 if (*p
->name
.full
== '/') {
380 snprintf(fn
, sizeof fn
, "%s%s.if", _PATH_VARRUN
, p
->name
.base
);
381 #ifndef RELEASE_CRUNCH
382 if (ID0unlink(fn
) == -1)
383 log_Printf(LogALERT
, "%s: Can't remove %s: %s\n",
384 p
->link
.name
, fn
, strerror(errno
));
390 if (p
->handler
&& p
->handler
->destroy
)
391 (*p
->handler
->destroy
)(p
);
393 p
->name
.base
= p
->name
.full
;
394 *p
->name
.full
= '\0';
398 physical_Destroy(struct physical
*p
)
401 throughput_destroy(&p
->link
.stats
.total
);
406 physical_DescriptorWrite(struct fdescriptor
*d
, struct bundle
*bundle
,
409 struct physical
*p
= descriptor2physical(d
);
413 p
->out
= link_Dequeue(&p
->link
);
416 nw
= physical_Write(p
, MBUF_CTOP(p
->out
), p
->out
->m_len
);
417 log_Printf(LogDEBUG
, "%s: DescriptorWrite: wrote %d(%lu) to %d\n",
418 p
->link
.name
, nw
, (unsigned long)p
->out
->m_len
, p
->fd
);
421 p
->out
->m_offset
+= nw
;
422 if (p
->out
->m_len
== 0)
423 p
->out
= m_free(p
->out
);
428 else if (errno
!= ENOBUFS
) {
429 log_Printf(LogPHASE
, "%s: write (%d): %s\n", p
->link
.name
,
430 p
->fd
, strerror(errno
));
431 datalink_Down(p
->dl
, CLOSE_NORMAL
);
434 /* else we shouldn't really have been called ! select() is broken ! */
441 physical_ShowStatus(struct cmdargs
const *arg
)
443 struct physical
*p
= arg
->cx
->physical
;
448 prompt_Printf(arg
->prompt
, "Name: %s\n", p
->link
.name
);
449 prompt_Printf(arg
->prompt
, " State: ");
451 prompt_Printf(arg
->prompt
, "closed\n");
453 slot
= physical_Slot(p
);
454 if (p
->handler
&& p
->handler
->openinfo
) {
456 prompt_Printf(arg
->prompt
, "open (%s)\n", (*p
->handler
->openinfo
)(p
));
458 prompt_Printf(arg
->prompt
, "open (%s, port %d)\n",
459 (*p
->handler
->openinfo
)(p
), slot
);
460 } else if (slot
== -1)
461 prompt_Printf(arg
->prompt
, "open\n");
463 prompt_Printf(arg
->prompt
, "open (port %d)\n", slot
);
466 prompt_Printf(arg
->prompt
, " Device: %s",
467 *p
->name
.full
? p
->name
.full
:
468 p
->type
== PHYS_DIRECT
? "unknown" : "N/A");
469 if (p
->session_owner
!= (pid_t
)-1)
470 prompt_Printf(arg
->prompt
, " (session owner: %ld)", (long)p
->session_owner
);
472 prompt_Printf(arg
->prompt
, "\n Link Type: %s\n", mode2Nam(p
->type
));
473 prompt_Printf(arg
->prompt
, " Connect Count: %d\n", p
->connect_count
);
475 if (p
->fd
>= 0 && ioctl(p
->fd
, TIOCOUTQ
, &n
) >= 0)
476 prompt_Printf(arg
->prompt
, " Physical outq: %d\n", n
);
479 prompt_Printf(arg
->prompt
, " Queued Packets: %lu\n",
480 (u_long
)link_QueueLen(&p
->link
));
481 prompt_Printf(arg
->prompt
, " Phone Number: %s\n", arg
->cx
->phone
.chosen
);
483 prompt_Printf(arg
->prompt
, "\nDefaults:\n");
485 prompt_Printf(arg
->prompt
, " Device List: ");
486 dev
= p
->cfg
.devlist
;
487 for (n
= 0; n
< p
->cfg
.ndev
; n
++) {
489 prompt_Printf(arg
->prompt
, ", ");
490 prompt_Printf(arg
->prompt
, "\"%s\"", dev
);
491 dev
+= strlen(dev
) + 1;
494 prompt_Printf(arg
->prompt
, "\n Characteristics: ");
495 if (physical_IsSync(arg
->cx
->physical
))
496 prompt_Printf(arg
->prompt
, "sync");
498 prompt_Printf(arg
->prompt
, "%dbps", p
->cfg
.speed
);
500 switch (p
->cfg
.parity
& CSIZE
) {
502 prompt_Printf(arg
->prompt
, ", cs7");
505 prompt_Printf(arg
->prompt
, ", cs8");
508 if (p
->cfg
.parity
& PARENB
) {
509 if (p
->cfg
.parity
& PARODD
)
510 prompt_Printf(arg
->prompt
, ", odd parity");
512 prompt_Printf(arg
->prompt
, ", even parity");
514 prompt_Printf(arg
->prompt
, ", no parity");
516 prompt_Printf(arg
->prompt
, ", CTS/RTS %s\n", (p
->cfg
.rts_cts
? "on" : "off"));
518 prompt_Printf(arg
->prompt
, " CD check delay: ");
519 cd
= p
->handler
? &p
->handler
->cd
: &p
->cfg
.cd
;
520 if (cd
->necessity
== CD_NOTREQUIRED
)
521 prompt_Printf(arg
->prompt
, "no cd");
522 else if (p
->cfg
.cd
.necessity
== CD_DEFAULT
) {
523 prompt_Printf(arg
->prompt
, "device specific");
525 prompt_Printf(arg
->prompt
, "%d second%s", p
->cfg
.cd
.delay
,
526 p
->cfg
.cd
.delay
== 1 ? "" : "s");
527 if (p
->cfg
.cd
.necessity
== CD_REQUIRED
)
528 prompt_Printf(arg
->prompt
, " (required!)");
530 prompt_Printf(arg
->prompt
, "\n\n");
532 throughput_disp(&p
->link
.stats
.total
, arg
->prompt
);
538 physical_DescriptorRead(struct fdescriptor
*d
, struct bundle
*bundle
,
541 struct physical
*p
= descriptor2physical(d
);
545 rbuff
= p
->input
.buf
+ p
->input
.sz
;
547 /* something to read */
548 n
= physical_Read(p
, rbuff
, sizeof p
->input
.buf
- p
->input
.sz
);
549 log_Printf(LogDEBUG
, "%s: DescriptorRead: read %d/%d from %d\n",
550 p
->link
.name
, n
, (int)(sizeof p
->input
.buf
- p
->input
.sz
), p
->fd
);
553 log_Printf(LogPHASE
, "%s: read (%d): %s\n", p
->link
.name
, p
->fd
,
556 log_Printf(LogPHASE
, "%s: read (%d): Got zero bytes\n",
557 p
->link
.name
, p
->fd
);
558 datalink_Down(p
->dl
, CLOSE_NORMAL
);
562 rbuff
-= p
->input
.sz
;
565 if (p
->link
.lcp
.fsm
.state
<= ST_CLOSED
) {
566 if (p
->type
!= PHYS_DEDICATED
) {
567 found
= hdlc_Detect((u_char
const **)&rbuff
, n
, physical_IsSync(p
));
568 if (rbuff
!= p
->input
.buf
)
569 log_WritePrompts(p
->dl
, "%.*s", (int)(rbuff
- p
->input
.buf
),
571 p
->input
.sz
= n
- (rbuff
- p
->input
.buf
);
574 /* LCP packet is detected. Turn ourselves into packet mode */
575 log_Printf(LogPHASE
, "%s: PPP packet detected, coming up\n",
577 log_SetTtyCommandMode(p
->dl
);
578 datalink_Up(p
->dl
, 0, 1);
579 link_PullPacket(&p
->link
, rbuff
, p
->input
.sz
, bundle
);
582 bcopy(rbuff
, p
->input
.buf
, p
->input
.sz
);
584 /* In -dedicated mode, we just discard input until LCP is started */
587 link_PullPacket(&p
->link
, rbuff
, n
, bundle
);
591 iov2physical(struct datalink
*dl
, struct iovec
*iov
, int *niov
, int maxiov
,
592 int fd
, int *auxfd
, int *nauxfd
)
597 p
= (struct physical
*)iov
[(*niov
)++].iov_base
;
598 p
->link
.name
= dl
->name
;
599 memset(p
->link
.Queue
, '\0', sizeof p
->link
.Queue
);
601 p
->desc
.UpdateSet
= physical_UpdateSet
;
602 p
->desc
.IsSet
= physical_IsSet
;
603 p
->desc
.Read
= physical_DescriptorRead
;
604 p
->desc
.Write
= physical_DescriptorWrite
;
605 p
->type
= PHYS_DIRECT
;
607 len
= strlen(_PATH_DEV
);
609 p
->connect_count
= 1;
611 physical_SetDevice(p
, p
->name
.full
);
613 p
->link
.lcp
.fsm
.bundle
= dl
->bundle
;
614 p
->link
.lcp
.fsm
.link
= &p
->link
;
615 memset(&p
->link
.lcp
.fsm
.FsmTimer
, '\0', sizeof p
->link
.lcp
.fsm
.FsmTimer
);
616 memset(&p
->link
.lcp
.fsm
.OpenTimer
, '\0', sizeof p
->link
.lcp
.fsm
.OpenTimer
);
617 memset(&p
->link
.lcp
.fsm
.StoppedTimer
, '\0',
618 sizeof p
->link
.lcp
.fsm
.StoppedTimer
);
619 p
->link
.lcp
.fsm
.parent
= &dl
->fsmp
;
620 lcp_SetupCallbacks(&p
->link
.lcp
);
622 p
->link
.ccp
.fsm
.bundle
= dl
->bundle
;
623 p
->link
.ccp
.fsm
.link
= &p
->link
;
624 /* Our in.state & out.state are NULL (no link-level ccp yet) */
625 memset(&p
->link
.ccp
.fsm
.FsmTimer
, '\0', sizeof p
->link
.ccp
.fsm
.FsmTimer
);
626 memset(&p
->link
.ccp
.fsm
.OpenTimer
, '\0', sizeof p
->link
.ccp
.fsm
.OpenTimer
);
627 memset(&p
->link
.ccp
.fsm
.StoppedTimer
, '\0',
628 sizeof p
->link
.ccp
.fsm
.StoppedTimer
);
629 p
->link
.ccp
.fsm
.parent
= &dl
->fsmp
;
630 ccp_SetupCallbacks(&p
->link
.ccp
);
632 p
->hdlc
.lqm
.owner
= &p
->link
.lcp
;
633 p
->hdlc
.ReportTimer
.state
= TIMER_STOPPED
;
634 p
->hdlc
.lqm
.timer
.state
= TIMER_STOPPED
;
637 p
->link
.stats
.total
.in
.SampleOctets
= (long long *)iov
[(*niov
)++].iov_base
;
638 p
->link
.stats
.total
.out
.SampleOctets
= (long long *)iov
[(*niov
)++].iov_base
;
639 p
->link
.stats
.parent
= dl
->bundle
->ncp
.mp
.active
?
640 &dl
->bundle
->ncp
.mp
.link
.stats
.total
: NULL
;
641 p
->link
.stats
.gather
= 1;
643 type
= (long)p
->handler
;
645 for (h
= 0; h
< NDEVICES
&& p
->handler
== NULL
; h
++)
646 p
->handler
= (*devices
[h
].iov2device
)(type
, p
, iov
, niov
, maxiov
,
648 if (p
->handler
== NULL
) {
649 log_Printf(LogPHASE
, "%s: Unknown link type\n", p
->link
.name
);
650 free(iov
[(*niov
)++].iov_base
);
651 physical_SetupStack(p
, "unknown", PHYSICAL_NOFORCE
);
653 log_Printf(LogPHASE
, "%s: Device %s, link type is %s\n",
654 p
->link
.name
, p
->name
.full
, p
->handler
->name
);
656 if (p
->hdlc
.lqm
.method
&& p
->hdlc
.lqm
.timer
.load
)
657 lqr_reStart(&p
->link
.lcp
);
658 hdlc_StartTimer(&p
->hdlc
);
660 throughput_restart(&p
->link
.stats
.total
, "physical throughput",
661 Enabled(dl
->bundle
, OPT_THROUGHPUT
));
667 physical_MaxDeviceSize(void)
671 biggest
= sizeof(struct device
);
672 for (sz
= n
= 0; n
< NDEVICES
; n
++)
673 if (devices
[n
].DeviceSize
) {
674 sz
= (*devices
[n
].DeviceSize
)();
683 physical2iov(struct physical
*p
, struct iovec
*iov
, int *niov
, int maxiov
,
684 int *auxfd
, int *nauxfd
)
691 hdlc_StopTimer(&p
->hdlc
);
693 timer_Stop(&p
->link
.lcp
.fsm
.FsmTimer
);
694 timer_Stop(&p
->link
.ccp
.fsm
.FsmTimer
);
695 timer_Stop(&p
->link
.lcp
.fsm
.OpenTimer
);
696 timer_Stop(&p
->link
.ccp
.fsm
.OpenTimer
);
697 timer_Stop(&p
->link
.lcp
.fsm
.StoppedTimer
);
698 timer_Stop(&p
->link
.ccp
.fsm
.StoppedTimer
);
701 p
->handler
= (struct device
*)(long)p
->handler
->type
;
704 if (Enabled(p
->dl
->bundle
, OPT_KEEPSESSION
) ||
705 tcgetpgrp(p
->fd
) == getpgrp())
706 p
->session_owner
= getpid(); /* So I'll eventually get HUP'd */
708 p
->session_owner
= (pid_t
)-1;
709 timer_Stop(&p
->link
.stats
.total
.Timer
);
712 if (*niov
+ 2 >= maxiov
) {
713 log_Printf(LogERROR
, "physical2iov: No room for physical + throughput"
720 iov
[*niov
].iov_base
= (void *)p
;
721 iov
[*niov
].iov_len
= sizeof *p
;
724 iov
[*niov
].iov_base
= p
? (void *)p
->link
.stats
.total
.in
.SampleOctets
: NULL
;
725 iov
[*niov
].iov_len
= SAMPLE_PERIOD
* sizeof(long long);
727 iov
[*niov
].iov_base
= p
? (void *)p
->link
.stats
.total
.out
.SampleOctets
: NULL
;
728 iov
[*niov
].iov_len
= SAMPLE_PERIOD
* sizeof(long long);
731 sz
= physical_MaxDeviceSize();
733 if (h
&& h
->device2iov
)
734 (*h
->device2iov
)(h
, iov
, niov
, maxiov
, auxfd
, nauxfd
);
736 iov
[*niov
].iov_base
= malloc(sz
);
738 memcpy(iov
[*niov
].iov_base
, h
, sizeof *h
);
739 iov
[*niov
].iov_len
= sz
;
743 iov
[*niov
].iov_base
= NULL
;
744 iov
[*niov
].iov_len
= sz
;
748 return p
? p
->fd
: 0;
752 physical_LockedDevice(struct physical
*p
)
754 if (p
->fd
>= 0 && *p
->name
.full
== '/' && p
->type
!= PHYS_DIRECT
)
761 physical_ChangedPid(struct physical
*p
, pid_t newpid
)
763 if (physical_LockedDevice(p
)) {
766 if ((res
= ID0uu_lock_txfr(p
->name
.base
, newpid
)) != UU_LOCK_OK
)
767 log_Printf(LogPHASE
, "uu_lock_txfr: %s\n", uu_lockerr(res
));
772 physical_IsSync(struct physical
*p
)
774 return p
->cfg
.speed
== 0;
778 physical_DeviceMTU(struct physical
*p
)
780 return p
->handler
? p
->handler
->mtu
: 0;
784 physical_GetDevice(struct physical
*p
)
790 physical_SetDeviceList(struct physical
*p
, int argc
, const char *const *argv
)
794 p
->cfg
.devlist
[sizeof p
->cfg
.devlist
- 1] = '\0';
795 for (f
= 0, pos
= 0; f
< argc
&& pos
< sizeof p
->cfg
.devlist
- 1; f
++) {
797 p
->cfg
.devlist
[pos
++] = '\0';
798 strncpy(p
->cfg
.devlist
+ pos
, argv
[f
], sizeof p
->cfg
.devlist
- pos
- 1);
799 pos
+= strlen(p
->cfg
.devlist
+ pos
);
805 physical_SetSync(struct physical
*p
)
811 physical_SetRtsCts(struct physical
*p
, int enable
)
813 p
->cfg
.rts_cts
= enable
? 1 : 0;
818 physical_Read(struct physical
*p
, void *buf
, size_t nbytes
)
822 if (p
->handler
&& p
->handler
->read
)
823 ret
= (*p
->handler
->read
)(p
, buf
, nbytes
);
825 ret
= read(p
->fd
, buf
, nbytes
);
827 log_DumpBuff(LogPHYSICAL
, "read", buf
, ret
);
833 physical_Write(struct physical
*p
, const void *buf
, size_t nbytes
)
835 log_DumpBuff(LogPHYSICAL
, "write", buf
, nbytes
);
837 if (p
->handler
&& p
->handler
->write
)
838 return (*p
->handler
->write
)(p
, buf
, nbytes
);
840 return write(p
->fd
, buf
, nbytes
);
844 physical_doUpdateSet(struct fdescriptor
*d
, fd_set
*r
, fd_set
*w
, fd_set
*e
,
847 struct physical
*p
= descriptor2physical(d
);
854 log_Printf(LogTIMER
, "%s: fdset(r) %d\n", p
->link
.name
, p
->fd
);
859 log_Printf(LogTIMER
, "%s: fdset(e) %d\n", p
->link
.name
, p
->fd
);
862 if (w
&& (force
|| link_QueueLen(&p
->link
) || p
->out
)) {
864 log_Printf(LogTIMER
, "%s: fdset(w) %d\n", p
->link
.name
, p
->fd
);
867 if (sets
&& *n
< p
->fd
+ 1)
875 physical_RemoveFromSet(struct physical
*p
, fd_set
*r
, fd_set
*w
, fd_set
*e
)
877 if (p
->handler
&& p
->handler
->removefromset
)
878 return (*p
->handler
->removefromset
)(p
, r
, w
, e
);
884 if (r
&& FD_ISSET(p
->fd
, r
)) {
886 log_Printf(LogTIMER
, "%s: fdunset(r) %d\n", p
->link
.name
, p
->fd
);
889 if (e
&& FD_ISSET(p
->fd
, e
)) {
891 log_Printf(LogTIMER
, "%s: fdunset(e) %d\n", p
->link
.name
, p
->fd
);
894 if (w
&& FD_ISSET(p
->fd
, w
)) {
896 log_Printf(LogTIMER
, "%s: fdunset(w) %d\n", p
->link
.name
, p
->fd
);
906 physical_IsSet(struct fdescriptor
*d
, const fd_set
*fdset
)
908 struct physical
*p
= descriptor2physical(d
);
909 return p
->fd
>= 0 && FD_ISSET(p
->fd
, fdset
);
913 physical_Login(struct physical
*p
, const char *name
)
915 if (p
->type
== PHYS_DIRECT
&& *p
->name
.base
&& !p
->Utmp
) {
920 memset(&ut
, 0, sizeof ut
);
922 strncpy(ut
.ut_name
, name
, sizeof ut
.ut_name
);
923 if (p
->handler
&& (p
->handler
->type
== TCP_DEVICE
||
924 p
->handler
->type
== UDP_DEVICE
)) {
925 strncpy(ut
.ut_line
, PPPOTCPLINE
, sizeof ut
.ut_line
);
926 strncpy(ut
.ut_host
, p
->name
.base
, sizeof ut
.ut_host
);
927 colon
= memchr(ut
.ut_host
, ':', sizeof ut
.ut_host
);
931 strncpy(ut
.ut_line
, p
->name
.base
, sizeof ut
.ut_line
);
932 if ((connstr
= getenv("CONNECT")))
933 /* mgetty sets this to the connection speed */
934 strncpy(ut
.ut_host
, connstr
, sizeof ut
.ut_host
);
936 p
->Utmp
= ut
.ut_time
;
941 physical_SetMode(struct physical
*p
, int mode
)
943 if ((p
->type
& (PHYS_DIRECT
|PHYS_DEDICATED
) ||
944 mode
& (PHYS_DIRECT
|PHYS_DEDICATED
)) &&
945 (!(p
->type
& PHYS_DIRECT
) || !(mode
& PHYS_BACKGROUND
))) {
946 /* Note: The -direct -> -background is for callback ! */
947 log_Printf(LogWARN
, "%s: Cannot change mode %s to %s\n", p
->link
.name
,
948 mode2Nam(p
->type
), mode2Nam(mode
));
956 physical_DeleteQueue(struct physical
*p
)
962 link_DeleteQueue(&p
->link
);
966 physical_SetDevice(struct physical
*p
, const char *name
)
968 int len
= strlen(_PATH_DEV
);
970 if (name
!= p
->name
.full
) {
971 strncpy(p
->name
.full
, name
, sizeof p
->name
.full
- 1);
972 p
->name
.full
[sizeof p
->name
.full
- 1] = '\0';
974 p
->name
.base
= *p
->name
.full
== '!' ? p
->name
.full
+ 1 :
975 strncmp(p
->name
.full
, _PATH_DEV
, len
) ?
976 p
->name
.full
: p
->name
.full
+ len
;
980 physical_Found(struct physical
*p
)
985 if (*p
->name
.full
== '/') {
986 snprintf(fn
, sizeof fn
, "%s%s.if", _PATH_VARRUN
, p
->name
.base
);
987 lockfile
= ID0fopen(fn
, "w");
988 if (lockfile
!= NULL
) {
989 fprintf(lockfile
, "%s%d\n", TUN_NAME
, p
->dl
->bundle
->unit
);
992 #ifndef RELEASE_CRUNCH
994 log_Printf(LogALERT
, "%s: Can't create %s: %s\n",
995 p
->link
.name
, fn
, strerror(errno
));
999 throughput_start(&p
->link
.stats
.total
, "physical throughput",
1000 Enabled(p
->dl
->bundle
, OPT_THROUGHPUT
));
1004 log_Printf(LogPHASE
, "%s: Connected!\n", p
->link
.name
);
1008 physical_Open(struct physical
*p
, struct bundle
*bundle
)
1010 int devno
, h
, wasfd
, err
;
1014 log_Printf(LogDEBUG
, "%s: Open: Modem is already open!\n", p
->link
.name
);
1015 /* We're going back into "term" mode */
1016 else if (p
->type
== PHYS_DIRECT
) {
1017 physical_SetDevice(p
, "");
1018 p
->fd
= STDIN_FILENO
;
1019 for (h
= 0; h
< NDEVICES
&& p
->handler
== NULL
&& p
->fd
>= 0; h
++)
1020 p
->handler
= (*devices
[h
].create
)(p
);
1022 if (p
->handler
== NULL
) {
1023 physical_SetupStack(p
, "unknown", PHYSICAL_NOFORCE
);
1024 log_Printf(LogDEBUG
, "%s: stdin is unidentified\n", p
->link
.name
);
1029 dev
= p
->cfg
.devlist
;
1031 while (devno
< p
->cfg
.ndev
&& p
->fd
< 0) {
1032 physical_SetDevice(p
, dev
);
1033 if (physical_Lock(p
)) {
1036 if (*p
->name
.full
== '/') {
1037 p
->fd
= ID0open(p
->name
.full
, O_RDWR
| O_NONBLOCK
);
1043 for (h
= 0; h
< NDEVICES
&& p
->handler
== NULL
; h
++)
1044 if ((p
->handler
= (*devices
[h
].create
)(p
)) == NULL
&& wasfd
!= p
->fd
)
1048 if (h
== NDEVICES
) {
1050 log_Printf(LogWARN
, "%s: %s: %s\n", p
->link
.name
, p
->name
.full
,
1053 log_Printf(LogWARN
, "%s: Device (%s) must begin with a '/',"
1054 " a '!' or contain at least one ':'\n", p
->link
.name
,
1061 dev
+= strlen(dev
) + 1;
1070 physical_SetupStack(struct physical
*p
, const char *who
, int how
)
1072 link_EmptyStack(&p
->link
);
1073 if (how
== PHYSICAL_FORCE_SYNC
|| how
== PHYSICAL_FORCE_SYNCNOACF
||
1074 (how
== PHYSICAL_NOFORCE
&& physical_IsSync(p
)))
1075 link_Stack(&p
->link
, &synclayer
);
1077 link_Stack(&p
->link
, &asynclayer
);
1078 link_Stack(&p
->link
, &hdlclayer
);
1080 if (how
!= PHYSICAL_FORCE_SYNCNOACF
)
1081 link_Stack(&p
->link
, &acflayer
);
1082 link_Stack(&p
->link
, &protolayer
);
1083 link_Stack(&p
->link
, &lqrlayer
);
1084 link_Stack(&p
->link
, &ccplayer
);
1085 link_Stack(&p
->link
, &vjlayer
);
1086 link_Stack(&p
->link
, &tcpmsslayer
);
1088 link_Stack(&p
->link
, &natlayer
);
1090 if (how
== PHYSICAL_FORCE_ASYNC
&& physical_IsSync(p
)) {
1091 log_Printf(LogWARN
, "Sync device setting ignored for ``%s'' device\n", who
);
1092 p
->cfg
.speed
= MODEM_SPEED
;
1093 } else if (how
== PHYSICAL_FORCE_SYNC
&& !physical_IsSync(p
)) {
1094 log_Printf(LogWARN
, "Async device setting ignored for ``%s'' device\n",
1096 physical_SetSync(p
);
1101 physical_StopDeviceTimer(struct physical
*p
)
1103 if (p
->handler
&& p
->handler
->stoptimer
)
1104 (*p
->handler
->stoptimer
)(p
);
1108 physical_AwaitCarrier(struct physical
*p
)
1110 if (p
->handler
&& p
->handler
->awaitcarrier
)
1111 return (*p
->handler
->awaitcarrier
)(p
);
1118 physical_SetAsyncParams(struct physical
*p
, u_int32_t mymap
, u_int32_t hismap
)
1120 if (p
->handler
&& p
->handler
->setasyncparams
)
1121 return (*p
->handler
->setasyncparams
)(p
, mymap
, hismap
);
1123 async_SetLinkParams(&p
->async
, mymap
, hismap
);
1127 physical_Slot(struct physical
*p
)
1129 if (p
->handler
&& p
->handler
->slot
)
1130 return (*p
->handler
->slot
)(p
);