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 $
22 #include <sys/param.h>
23 #include <netinet/in.h>
24 #include <netinet/in_systm.h>
25 #include <netinet/ip.h>
26 #include <sys/socket.h>
39 #include <sys/tty.h> /* TIOCOUTQ */
44 #if defined(__OpenBSD__) || defined(__NetBSD__)
45 #include <sys/ioctl.h>
68 #include "throughput.h"
72 #include "slcompress.h"
76 #include "descriptor.h"
103 #define PPPOTCPLINE "ppp"
105 static int physical_DescriptorWrite(struct fdescriptor
*, struct bundle
*,
109 physical_DeviceSize(void)
111 return sizeof(struct device
);
115 struct device
*(*create
)(struct physical
*);
116 struct device
*(*iov2device
)(int, struct physical
*, struct iovec
*,
117 int *, int, int *, int *);
118 unsigned (*DeviceSize
)(void);
120 { tty_Create
, tty_iov2device
, tty_DeviceSize
},
123 * This must come before ``udp'' so that the probe routine is
124 * able to identify it as a more specific type of SOCK_DGRAM.
126 { ether_Create
, ether_iov2device
, ether_DeviceSize
},
127 #ifdef EXPERIMENTAL_NETGRAPH
128 { ng_Create
, ng_iov2device
, ng_DeviceSize
},
131 { tcp_Create
, tcp_iov2device
, tcp_DeviceSize
},
132 { udp_Create
, udp_iov2device
, udp_DeviceSize
},
133 { exec_Create
, exec_iov2device
, exec_DeviceSize
}
136 #define NDEVICES NELEM(devices)
139 physical_UpdateSet(struct fdescriptor
*d
, fd_set
*r
, fd_set
*w
, fd_set
*e
,
142 return physical_doUpdateSet(d
, r
, w
, e
, n
, 0);
146 physical_SetDescriptor(struct physical
*p
)
148 p
->desc
.type
= PHYSICAL_DESCRIPTOR
;
149 p
->desc
.UpdateSet
= physical_UpdateSet
;
150 p
->desc
.IsSet
= physical_IsSet
;
151 p
->desc
.Read
= physical_DescriptorRead
;
152 p
->desc
.Write
= physical_DescriptorWrite
;
156 physical_Create(struct datalink
*dl
, int type
)
160 p
= (struct physical
*)malloc(sizeof(struct physical
));
164 p
->link
.type
= PHYSICAL_LINK
;
165 p
->link
.name
= dl
->name
;
166 p
->link
.len
= sizeof *p
;
168 /* The sample period is fixed - see physical2iov() & iov2physical() */
169 throughput_init(&p
->link
.stats
.total
, SAMPLE_PERIOD
);
170 p
->link
.stats
.parent
= dl
->bundle
->ncp
.mp
.active
?
171 &dl
->bundle
->ncp
.mp
.link
.stats
.total
: NULL
;
172 p
->link
.stats
.gather
= 1;
174 memset(p
->link
.Queue
, '\0', sizeof p
->link
.Queue
);
175 memset(p
->link
.proto_in
, '\0', sizeof p
->link
.proto_in
);
176 memset(p
->link
.proto_out
, '\0', sizeof p
->link
.proto_out
);
177 link_EmptyStack(&p
->link
);
180 physical_SetDescriptor(p
);
183 hdlc_Init(&p
->hdlc
, &p
->link
.lcp
);
184 async_Init(&p
->async
);
188 p
->connect_count
= 0;
191 *p
->name
.full
= '\0';
192 p
->name
.base
= p
->name
.full
;
195 p
->session_owner
= (pid_t
)-1;
197 p
->cfg
.rts_cts
= MODEM_CTSRTS
;
198 p
->cfg
.speed
= MODEM_SPEED
;
200 memcpy(p
->cfg
.devlist
, MODEM_LIST
, sizeof MODEM_LIST
);
201 p
->cfg
.ndev
= NMODEMS
;
202 p
->cfg
.cd
.necessity
= CD_DEFAULT
;
203 p
->cfg
.cd
.delay
= 0; /* reconfigured or device specific default */
205 lcp_Init(&p
->link
.lcp
, dl
->bundle
, &p
->link
, &dl
->fsmp
);
206 ccp_Init(&p
->link
.ccp
, dl
->bundle
, &p
->link
, &dl
->fsmp
);
211 static const struct parity
{
216 { "even", "P_EVEN", CS7
| PARENB
},
217 { "odd", "P_ODD", CS7
| PARENB
| PARODD
},
218 { "none", "P_ZERO", CS8
},
223 GetParityValue(const char *str
)
225 const struct parity
*pp
;
227 for (pp
= validparity
; pp
->name
; pp
++) {
228 if (strcasecmp(pp
->name
, str
) == 0 ||
229 strcasecmp(pp
->name1
, str
) == 0) {
237 physical_SetParity(struct physical
*p
, const char *str
)
239 struct termios rstio
;
242 val
= GetParityValue(str
);
246 tcgetattr(p
->fd
, &rstio
);
247 rstio
.c_cflag
&= ~(CSIZE
| PARODD
| PARENB
);
248 rstio
.c_cflag
|= val
;
249 tcsetattr(p
->fd
, TCSADRAIN
, &rstio
);
253 log_Printf(LogWARN
, "%s: %s: Invalid parity\n", p
->link
.name
, str
);
258 physical_GetSpeed(struct physical
*p
)
260 if (p
->handler
&& p
->handler
->speed
)
261 return (*p
->handler
->speed
)(p
);
267 physical_SetSpeed(struct physical
*p
, unsigned speed
)
269 if (UnsignedToSpeed(speed
) != B0
) {
270 p
->cfg
.speed
= speed
;
278 physical_Raw(struct physical
*p
)
280 if (p
->handler
&& p
->handler
->raw
)
281 return (*p
->handler
->raw
)(p
);
287 physical_Offline(struct physical
*p
)
289 if (p
->handler
&& p
->handler
->offline
)
290 (*p
->handler
->offline
)(p
);
291 log_Printf(LogPHASE
, "%s: Disconnected!\n", p
->link
.name
);
295 physical_Lock(struct physical
*p
)
299 if (*p
->name
.full
== '/' && p
->type
!= PHYS_DIRECT
&&
300 (res
= ID0uu_lock(p
->name
.base
)) != UU_LOCK_OK
) {
301 if (res
== UU_LOCK_INUSE
)
302 log_Printf(LogPHASE
, "%s: %s is in use\n", p
->link
.name
, p
->name
.full
);
304 log_Printf(LogPHASE
, "%s: %s is in use: uu_lock: %s\n",
305 p
->link
.name
, p
->name
.full
, uu_lockerr(res
));
313 physical_Unlock(struct physical
*p
)
315 if (*p
->name
.full
== '/' && p
->type
!= PHYS_DIRECT
&&
316 ID0uu_unlock(p
->name
.base
) == -1)
317 log_Printf(LogALERT
, "%s: Can't uu_unlock %s\n", p
->link
.name
,
322 physical_Close(struct physical
*p
)
330 log_Printf(LogDEBUG
, "%s: Close\n", p
->link
.name
);
332 if (p
->handler
&& p
->handler
->cooked
)
333 (*p
->handler
->cooked
)(p
);
335 physical_StopDeviceTimer(p
);
337 if (p
->handler
&& (p
->handler
->type
== TCP_DEVICE
||
338 p
->handler
->type
== UDP_DEVICE
))
339 /* Careful - we logged in on line ``ppp'' with IP as our host */
340 ID0logout(PPPOTCPLINE
, 1);
342 ID0logout(p
->name
.base
, 0);
345 newsid
= tcgetpgrp(p
->fd
) == getpgrp();
348 log_SetTtyCommandMode(p
->dl
);
350 throughput_stop(&p
->link
.stats
.total
);
351 throughput_log(&p
->link
.stats
.total
, LogPHASE
, p
->link
.name
);
353 if (p
->session_owner
!= (pid_t
)-1) {
354 log_Printf(LogPHASE
, "%s: HUPing %ld\n", p
->link
.name
,
355 (long)p
->session_owner
);
356 ID0kill(p
->session_owner
, SIGHUP
);
357 p
->session_owner
= (pid_t
)-1;
361 bundle_setsid(p
->dl
->bundle
, 0);
363 if (*p
->name
.full
== '/') {
364 snprintf(fn
, sizeof fn
, "%s%s.if", _PATH_VARRUN
, p
->name
.base
);
365 if (ID0unlink(fn
) == -1)
366 log_Printf(LogALERT
, "%s: Can't remove %s: %s\n",
367 p
->link
.name
, fn
, strerror(errno
));
370 if (p
->handler
&& p
->handler
->destroy
)
371 (*p
->handler
->destroy
)(p
);
373 p
->name
.base
= p
->name
.full
;
374 *p
->name
.full
= '\0';
378 physical_Destroy(struct physical
*p
)
381 throughput_destroy(&p
->link
.stats
.total
);
386 physical_DescriptorWrite(struct fdescriptor
*d
, struct bundle
*bundle __unused
,
387 const fd_set
*fdset __unused
)
389 struct physical
*p
= descriptor2physical(d
);
393 p
->out
= link_Dequeue(&p
->link
);
396 nw
= physical_Write(p
, MBUF_CTOP(p
->out
), p
->out
->m_len
);
397 log_Printf(LogDEBUG
, "%s: DescriptorWrite: wrote %d(%lu) to %d\n",
398 p
->link
.name
, nw
, (unsigned long)p
->out
->m_len
, p
->fd
);
401 p
->out
->m_offset
+= nw
;
402 if (p
->out
->m_len
== 0)
403 p
->out
= m_free(p
->out
);
408 else if (errno
!= ENOBUFS
) {
409 log_Printf(LogPHASE
, "%s: write (%d): %s\n", p
->link
.name
,
410 p
->fd
, strerror(errno
));
411 datalink_Down(p
->dl
, CLOSE_NORMAL
);
414 /* else we shouldn't really have been called ! select() is broken ! */
421 physical_ShowStatus(struct cmdargs
const *arg
)
423 struct physical
*p
= arg
->cx
->physical
;
428 prompt_Printf(arg
->prompt
, "Name: %s\n", p
->link
.name
);
429 prompt_Printf(arg
->prompt
, " State: ");
431 prompt_Printf(arg
->prompt
, "closed\n");
433 slot
= physical_Slot(p
);
434 if (p
->handler
&& p
->handler
->openinfo
) {
436 prompt_Printf(arg
->prompt
, "open (%s)\n", (*p
->handler
->openinfo
)(p
));
438 prompt_Printf(arg
->prompt
, "open (%s, port %d)\n",
439 (*p
->handler
->openinfo
)(p
), slot
);
440 } else if (slot
== -1)
441 prompt_Printf(arg
->prompt
, "open\n");
443 prompt_Printf(arg
->prompt
, "open (port %d)\n", slot
);
446 prompt_Printf(arg
->prompt
, " Device: %s",
447 *p
->name
.full
? p
->name
.full
:
448 p
->type
== PHYS_DIRECT
? "unknown" : "N/A");
449 if (p
->session_owner
!= (pid_t
)-1)
450 prompt_Printf(arg
->prompt
, " (session owner: %ld)", (long)p
->session_owner
);
452 prompt_Printf(arg
->prompt
, "\n Link Type: %s\n", mode2Nam(p
->type
));
453 prompt_Printf(arg
->prompt
, " Connect Count: %d\n", p
->connect_count
);
455 if (p
->fd
>= 0 && ioctl(p
->fd
, TIOCOUTQ
, &n
) >= 0)
456 prompt_Printf(arg
->prompt
, " Physical outq: %d\n", n
);
459 prompt_Printf(arg
->prompt
, " Queued Packets: %lu\n",
460 (u_long
)link_QueueLen(&p
->link
));
461 prompt_Printf(arg
->prompt
, " Phone Number: %s\n", arg
->cx
->phone
.chosen
);
463 prompt_Printf(arg
->prompt
, "\nDefaults:\n");
465 prompt_Printf(arg
->prompt
, " Device List: ");
466 dev
= p
->cfg
.devlist
;
467 for (n
= 0; n
< p
->cfg
.ndev
; n
++) {
469 prompt_Printf(arg
->prompt
, ", ");
470 prompt_Printf(arg
->prompt
, "\"%s\"", dev
);
471 dev
+= strlen(dev
) + 1;
474 prompt_Printf(arg
->prompt
, "\n Characteristics: ");
475 if (physical_IsSync(arg
->cx
->physical
))
476 prompt_Printf(arg
->prompt
, "sync");
478 prompt_Printf(arg
->prompt
, "%dbps", p
->cfg
.speed
);
480 switch (p
->cfg
.parity
& CSIZE
) {
482 prompt_Printf(arg
->prompt
, ", cs7");
485 prompt_Printf(arg
->prompt
, ", cs8");
488 if (p
->cfg
.parity
& PARENB
) {
489 if (p
->cfg
.parity
& PARODD
)
490 prompt_Printf(arg
->prompt
, ", odd parity");
492 prompt_Printf(arg
->prompt
, ", even parity");
494 prompt_Printf(arg
->prompt
, ", no parity");
496 prompt_Printf(arg
->prompt
, ", CTS/RTS %s\n", (p
->cfg
.rts_cts
? "on" : "off"));
498 prompt_Printf(arg
->prompt
, " CD check delay: ");
499 cd
= p
->handler
? &p
->handler
->cd
: &p
->cfg
.cd
;
500 if (cd
->necessity
== CD_NOTREQUIRED
)
501 prompt_Printf(arg
->prompt
, "no cd");
502 else if (p
->cfg
.cd
.necessity
== CD_DEFAULT
) {
503 prompt_Printf(arg
->prompt
, "device specific");
505 prompt_Printf(arg
->prompt
, "%d second%s", p
->cfg
.cd
.delay
,
506 p
->cfg
.cd
.delay
== 1 ? "" : "s");
507 if (p
->cfg
.cd
.necessity
== CD_REQUIRED
)
508 prompt_Printf(arg
->prompt
, " (required!)");
510 prompt_Printf(arg
->prompt
, "\n\n");
512 throughput_disp(&p
->link
.stats
.total
, arg
->prompt
);
518 physical_DescriptorRead(struct fdescriptor
*d
, struct bundle
*bundle
,
519 const fd_set
*fdset __unused
)
521 struct physical
*p
= descriptor2physical(d
);
525 rbuff
= p
->input
.buf
+ p
->input
.sz
;
527 /* something to read */
528 n
= physical_Read(p
, rbuff
, sizeof p
->input
.buf
- p
->input
.sz
);
529 log_Printf(LogDEBUG
, "%s: DescriptorRead: read %d/%d from %d\n",
530 p
->link
.name
, n
, (int)(sizeof p
->input
.buf
- p
->input
.sz
), p
->fd
);
533 log_Printf(LogPHASE
, "%s: read (%d): %s\n", p
->link
.name
, p
->fd
,
536 log_Printf(LogPHASE
, "%s: read (%d): Got zero bytes\n",
537 p
->link
.name
, p
->fd
);
538 datalink_Down(p
->dl
, CLOSE_NORMAL
);
542 rbuff
-= p
->input
.sz
;
545 if (p
->link
.lcp
.fsm
.state
<= ST_CLOSED
) {
546 if (p
->type
!= PHYS_DEDICATED
) {
547 found
= hdlc_Detect((u_char
const **)(void *)&rbuff
, n
, physical_IsSync(p
));
548 if (rbuff
!= p
->input
.buf
)
549 log_WritePrompts(p
->dl
, "%.*s", (int)(rbuff
- p
->input
.buf
),
551 p
->input
.sz
= n
- (rbuff
- p
->input
.buf
);
554 /* LCP packet is detected. Turn ourselves into packet mode */
555 log_Printf(LogPHASE
, "%s: PPP packet detected, coming up\n",
557 log_SetTtyCommandMode(p
->dl
);
558 datalink_Up(p
->dl
, 0, 1);
559 link_PullPacket(&p
->link
, rbuff
, p
->input
.sz
, bundle
);
562 bcopy(rbuff
, p
->input
.buf
, p
->input
.sz
);
564 /* In -dedicated mode, we just discard input until LCP is started */
567 link_PullPacket(&p
->link
, rbuff
, n
, bundle
);
571 iov2physical(struct datalink
*dl
, struct iovec
*iov
, int *niov
, int maxiov
,
572 int fd
, int *auxfd
, int *nauxfd
)
578 p
= (struct physical
*)iov
[(*niov
)++].iov_base
;
579 p
->link
.name
= dl
->name
;
580 memset(p
->link
.Queue
, '\0', sizeof p
->link
.Queue
);
582 p
->desc
.UpdateSet
= physical_UpdateSet
;
583 p
->desc
.IsSet
= physical_IsSet
;
584 p
->desc
.Read
= physical_DescriptorRead
;
585 p
->desc
.Write
= physical_DescriptorWrite
;
586 p
->type
= PHYS_DIRECT
;
589 p
->connect_count
= 1;
591 physical_SetDevice(p
, p
->name
.full
);
593 p
->link
.lcp
.fsm
.bundle
= dl
->bundle
;
594 p
->link
.lcp
.fsm
.link
= &p
->link
;
595 memset(&p
->link
.lcp
.fsm
.FsmTimer
, '\0', sizeof p
->link
.lcp
.fsm
.FsmTimer
);
596 memset(&p
->link
.lcp
.fsm
.OpenTimer
, '\0', sizeof p
->link
.lcp
.fsm
.OpenTimer
);
597 memset(&p
->link
.lcp
.fsm
.StoppedTimer
, '\0',
598 sizeof p
->link
.lcp
.fsm
.StoppedTimer
);
599 p
->link
.lcp
.fsm
.parent
= &dl
->fsmp
;
600 lcp_SetupCallbacks(&p
->link
.lcp
);
602 p
->link
.ccp
.fsm
.bundle
= dl
->bundle
;
603 p
->link
.ccp
.fsm
.link
= &p
->link
;
604 /* Our in.state & out.state are NULL (no link-level ccp yet) */
605 memset(&p
->link
.ccp
.fsm
.FsmTimer
, '\0', sizeof p
->link
.ccp
.fsm
.FsmTimer
);
606 memset(&p
->link
.ccp
.fsm
.OpenTimer
, '\0', sizeof p
->link
.ccp
.fsm
.OpenTimer
);
607 memset(&p
->link
.ccp
.fsm
.StoppedTimer
, '\0',
608 sizeof p
->link
.ccp
.fsm
.StoppedTimer
);
609 p
->link
.ccp
.fsm
.parent
= &dl
->fsmp
;
610 ccp_SetupCallbacks(&p
->link
.ccp
);
612 p
->hdlc
.lqm
.owner
= &p
->link
.lcp
;
613 p
->hdlc
.ReportTimer
.state
= TIMER_STOPPED
;
614 p
->hdlc
.lqm
.timer
.state
= TIMER_STOPPED
;
617 p
->link
.stats
.total
.in
.SampleOctets
= (long long *)iov
[(*niov
)++].iov_base
;
618 p
->link
.stats
.total
.out
.SampleOctets
= (long long *)iov
[(*niov
)++].iov_base
;
619 p
->link
.stats
.parent
= dl
->bundle
->ncp
.mp
.active
?
620 &dl
->bundle
->ncp
.mp
.link
.stats
.total
: NULL
;
621 p
->link
.stats
.gather
= 1;
623 type
= (long)p
->handler
;
625 for (h
= 0; h
< NDEVICES
&& p
->handler
== NULL
; h
++)
626 p
->handler
= (*devices
[h
].iov2device
)(type
, p
, iov
, niov
, maxiov
,
628 if (p
->handler
== NULL
) {
629 log_Printf(LogPHASE
, "%s: Unknown link type\n", p
->link
.name
);
630 free(iov
[(*niov
)++].iov_base
);
631 physical_SetupStack(p
, "unknown", PHYSICAL_NOFORCE
);
633 log_Printf(LogPHASE
, "%s: Device %s, link type is %s\n",
634 p
->link
.name
, p
->name
.full
, p
->handler
->name
);
636 if (p
->hdlc
.lqm
.method
&& p
->hdlc
.lqm
.timer
.load
)
637 lqr_reStart(&p
->link
.lcp
);
638 hdlc_StartTimer(&p
->hdlc
);
640 throughput_restart(&p
->link
.stats
.total
, "physical throughput",
641 Enabled(dl
->bundle
, OPT_THROUGHPUT
));
647 physical_MaxDeviceSize(void)
649 unsigned biggest
, sz
, n
;
651 biggest
= sizeof(struct device
);
652 for (n
= 0; n
< NDEVICES
; n
++)
653 if (devices
[n
].DeviceSize
) {
654 sz
= (*devices
[n
].DeviceSize
)();
663 physical2iov(struct physical
*p
, struct iovec
*iov
, int *niov
, int maxiov
,
664 int *auxfd
, int *nauxfd
)
671 hdlc_StopTimer(&p
->hdlc
);
673 timer_Stop(&p
->link
.lcp
.fsm
.FsmTimer
);
674 timer_Stop(&p
->link
.ccp
.fsm
.FsmTimer
);
675 timer_Stop(&p
->link
.lcp
.fsm
.OpenTimer
);
676 timer_Stop(&p
->link
.ccp
.fsm
.OpenTimer
);
677 timer_Stop(&p
->link
.lcp
.fsm
.StoppedTimer
);
678 timer_Stop(&p
->link
.ccp
.fsm
.StoppedTimer
);
681 p
->handler
= (struct device
*)(long)p
->handler
->type
;
684 if (Enabled(p
->dl
->bundle
, OPT_KEEPSESSION
) ||
685 tcgetpgrp(p
->fd
) == getpgrp())
686 p
->session_owner
= getpid(); /* So I'll eventually get HUP'd */
688 p
->session_owner
= (pid_t
)-1;
689 timer_Stop(&p
->link
.stats
.total
.Timer
);
692 if (*niov
+ 2 >= maxiov
) {
693 log_Printf(LogERROR
, "physical2iov: No room for physical + throughput"
700 iov
[*niov
].iov_base
= (void *)p
;
701 iov
[*niov
].iov_len
= sizeof *p
;
704 iov
[*niov
].iov_base
= p
? (void *)p
->link
.stats
.total
.in
.SampleOctets
: NULL
;
705 iov
[*niov
].iov_len
= SAMPLE_PERIOD
* sizeof(long long);
707 iov
[*niov
].iov_base
= p
? (void *)p
->link
.stats
.total
.out
.SampleOctets
: NULL
;
708 iov
[*niov
].iov_len
= SAMPLE_PERIOD
* sizeof(long long);
711 sz
= physical_MaxDeviceSize();
713 if (h
&& h
->device2iov
)
714 (*h
->device2iov
)(h
, iov
, niov
, maxiov
, auxfd
, nauxfd
);
716 iov
[*niov
].iov_base
= malloc(sz
);
718 memcpy(iov
[*niov
].iov_base
, h
, sizeof *h
);
719 iov
[*niov
].iov_len
= sz
;
723 iov
[*niov
].iov_base
= NULL
;
724 iov
[*niov
].iov_len
= sz
;
728 return p
? p
->fd
: 0;
732 physical_LockedDevice(struct physical
*p
)
734 if (p
->fd
>= 0 && *p
->name
.full
== '/' && p
->type
!= PHYS_DIRECT
)
741 physical_ChangedPid(struct physical
*p
, pid_t newpid
)
743 if (physical_LockedDevice(p
)) {
746 if ((res
= ID0uu_lock_txfr(p
->name
.base
, newpid
)) != UU_LOCK_OK
)
747 log_Printf(LogPHASE
, "uu_lock_txfr: %s\n", uu_lockerr(res
));
752 physical_IsSync(struct physical
*p
)
754 return p
->cfg
.speed
== 0;
758 physical_DeviceMTU(struct physical
*p
)
760 return p
->handler
? p
->handler
->mtu
: 0;
764 physical_GetDevice(struct physical
*p
)
770 physical_SetDeviceList(struct physical
*p
, int argc
, const char *const *argv
)
775 p
->cfg
.devlist
[sizeof p
->cfg
.devlist
- 1] = '\0';
776 for (f
= 0, pos
= 0; f
< argc
&& pos
< sizeof p
->cfg
.devlist
- 1; f
++) {
778 p
->cfg
.devlist
[pos
++] = '\0';
779 strncpy(p
->cfg
.devlist
+ pos
, argv
[f
], sizeof p
->cfg
.devlist
- pos
- 1);
780 pos
+= strlen(p
->cfg
.devlist
+ pos
);
786 physical_SetSync(struct physical
*p
)
792 physical_SetRtsCts(struct physical
*p
, int enable
)
794 p
->cfg
.rts_cts
= enable
? 1 : 0;
799 physical_Read(struct physical
*p
, void *buf
, size_t nbytes
)
803 if (p
->handler
&& p
->handler
->read
)
804 ret
= (*p
->handler
->read
)(p
, buf
, nbytes
);
806 ret
= read(p
->fd
, buf
, nbytes
);
808 log_DumpBuff(LogPHYSICAL
, "read", buf
, ret
);
814 physical_Write(struct physical
*p
, const void *buf
, size_t nbytes
)
816 log_DumpBuff(LogPHYSICAL
, "write", buf
, nbytes
);
818 if (p
->handler
&& p
->handler
->write
)
819 return (*p
->handler
->write
)(p
, buf
, nbytes
);
821 return write(p
->fd
, buf
, nbytes
);
825 physical_doUpdateSet(struct fdescriptor
*d
, fd_set
*r
, fd_set
*w
, fd_set
*e
,
828 struct physical
*p
= descriptor2physical(d
);
835 log_Printf(LogTIMER
, "%s: fdset(r) %d\n", p
->link
.name
, p
->fd
);
840 log_Printf(LogTIMER
, "%s: fdset(e) %d\n", p
->link
.name
, p
->fd
);
843 if (w
&& (force
|| link_QueueLen(&p
->link
) || p
->out
)) {
845 log_Printf(LogTIMER
, "%s: fdset(w) %d\n", p
->link
.name
, p
->fd
);
848 if (sets
&& *n
< p
->fd
+ 1)
856 physical_RemoveFromSet(struct physical
*p
, fd_set
*r
, fd_set
*w
, fd_set
*e
)
858 if (p
->handler
&& p
->handler
->removefromset
)
859 return (*p
->handler
->removefromset
)(p
, r
, w
, e
);
865 if (r
&& FD_ISSET(p
->fd
, r
)) {
867 log_Printf(LogTIMER
, "%s: fdunset(r) %d\n", p
->link
.name
, p
->fd
);
870 if (e
&& FD_ISSET(p
->fd
, e
)) {
872 log_Printf(LogTIMER
, "%s: fdunset(e) %d\n", p
->link
.name
, p
->fd
);
875 if (w
&& FD_ISSET(p
->fd
, w
)) {
877 log_Printf(LogTIMER
, "%s: fdunset(w) %d\n", p
->link
.name
, p
->fd
);
887 physical_IsSet(struct fdescriptor
*d
, const fd_set
*fdset
)
889 struct physical
*p
= descriptor2physical(d
);
890 return p
->fd
>= 0 && FD_ISSET(p
->fd
, fdset
);
894 physical_Login(struct physical
*p
, const char *name
)
896 if (p
->type
== PHYS_DIRECT
&& *p
->name
.base
&& !p
->Utmp
) {
901 memset(&ut
, 0, sizeof ut
);
903 strncpy(ut
.ut_name
, name
, sizeof ut
.ut_name
);
904 if (p
->handler
&& (p
->handler
->type
== TCP_DEVICE
||
905 p
->handler
->type
== UDP_DEVICE
)) {
906 strncpy(ut
.ut_line
, PPPOTCPLINE
, sizeof ut
.ut_line
);
907 strncpy(ut
.ut_host
, p
->name
.base
, sizeof ut
.ut_host
);
908 colon
= memchr(ut
.ut_host
, ':', sizeof ut
.ut_host
);
912 strncpy(ut
.ut_line
, p
->name
.base
, sizeof ut
.ut_line
);
913 if ((connstr
= getenv("CONNECT")))
914 /* mgetty sets this to the connection speed */
915 strncpy(ut
.ut_host
, connstr
, sizeof ut
.ut_host
);
917 p
->Utmp
= ut
.ut_time
;
922 physical_SetMode(struct physical
*p
, int mode
)
924 if ((p
->type
& (PHYS_DIRECT
|PHYS_DEDICATED
) ||
925 mode
& (PHYS_DIRECT
|PHYS_DEDICATED
)) &&
926 (!(p
->type
& PHYS_DIRECT
) || !(mode
& PHYS_BACKGROUND
))) {
927 /* Note: The -direct -> -background is for callback ! */
928 log_Printf(LogWARN
, "%s: Cannot change mode %s to %s\n", p
->link
.name
,
929 mode2Nam(p
->type
), mode2Nam(mode
));
937 physical_DeleteQueue(struct physical
*p
)
943 link_DeleteQueue(&p
->link
);
947 physical_SetDevice(struct physical
*p
, const char *name
)
949 int len
= strlen(_PATH_DEV
);
951 if (name
!= p
->name
.full
) {
952 strncpy(p
->name
.full
, name
, sizeof p
->name
.full
- 1);
953 p
->name
.full
[sizeof p
->name
.full
- 1] = '\0';
955 p
->name
.base
= *p
->name
.full
== '!' ? p
->name
.full
+ 1 :
956 strncmp(p
->name
.full
, _PATH_DEV
, len
) ?
957 p
->name
.full
: p
->name
.full
+ len
;
961 physical_Found(struct physical
*p
)
966 if (*p
->name
.full
== '/') {
967 snprintf(fn
, sizeof fn
, "%s%s.if", _PATH_VARRUN
, p
->name
.base
);
968 lockfile
= ID0fopen(fn
, "w");
969 if (lockfile
!= NULL
) {
970 fprintf(lockfile
, "%s%d\n", TUN_NAME
, p
->dl
->bundle
->unit
);
974 log_Printf(LogALERT
, "%s: Can't create %s: %s\n",
975 p
->link
.name
, fn
, strerror(errno
));
978 throughput_start(&p
->link
.stats
.total
, "physical throughput",
979 Enabled(p
->dl
->bundle
, OPT_THROUGHPUT
));
983 log_Printf(LogPHASE
, "%s: Connected!\n", p
->link
.name
);
987 physical_Open(struct physical
*p
)
990 int devno
, wasfd
, err
;
994 log_Printf(LogDEBUG
, "%s: Open: Modem is already open!\n", p
->link
.name
);
995 /* We're going back into "term" mode */
996 else if (p
->type
== PHYS_DIRECT
) {
997 physical_SetDevice(p
, "");
998 p
->fd
= STDIN_FILENO
;
999 for (h
= 0; h
< NDEVICES
&& p
->handler
== NULL
&& p
->fd
>= 0; h
++)
1000 p
->handler
= (*devices
[h
].create
)(p
);
1002 if (p
->handler
== NULL
) {
1003 physical_SetupStack(p
, "unknown", PHYSICAL_NOFORCE
);
1004 log_Printf(LogDEBUG
, "%s: stdin is unidentified\n", p
->link
.name
);
1009 dev
= p
->cfg
.devlist
;
1011 while (devno
< p
->cfg
.ndev
&& p
->fd
< 0) {
1012 physical_SetDevice(p
, dev
);
1013 if (physical_Lock(p
)) {
1016 if (*p
->name
.full
== '/') {
1017 p
->fd
= ID0open(p
->name
.full
, O_RDWR
| O_NONBLOCK
);
1023 for (h
= 0; h
< NDEVICES
&& p
->handler
== NULL
; h
++)
1024 if ((p
->handler
= (*devices
[h
].create
)(p
)) == NULL
&& wasfd
!= p
->fd
)
1028 if (h
== NDEVICES
) {
1030 log_Printf(LogWARN
, "%s: %s: %s\n", p
->link
.name
, p
->name
.full
,
1033 log_Printf(LogWARN
, "%s: Device (%s) must begin with a '/',"
1034 " a '!' or contain at least one ':'\n", p
->link
.name
,
1041 dev
+= strlen(dev
) + 1;
1050 physical_SetupStack(struct physical
*p
, const char *who
, int how
)
1052 link_EmptyStack(&p
->link
);
1053 if (how
== PHYSICAL_FORCE_SYNC
|| how
== PHYSICAL_FORCE_SYNCNOACF
||
1054 (how
== PHYSICAL_NOFORCE
&& physical_IsSync(p
)))
1055 link_Stack(&p
->link
, &synclayer
);
1057 link_Stack(&p
->link
, &asynclayer
);
1058 link_Stack(&p
->link
, &hdlclayer
);
1060 if (how
!= PHYSICAL_FORCE_SYNCNOACF
)
1061 link_Stack(&p
->link
, &acflayer
);
1062 link_Stack(&p
->link
, &protolayer
);
1063 link_Stack(&p
->link
, &lqrlayer
);
1064 link_Stack(&p
->link
, &ccplayer
);
1065 link_Stack(&p
->link
, &vjlayer
);
1066 link_Stack(&p
->link
, &tcpmsslayer
);
1068 link_Stack(&p
->link
, &natlayer
);
1070 if (how
== PHYSICAL_FORCE_ASYNC
&& physical_IsSync(p
)) {
1071 log_Printf(LogWARN
, "Sync device setting ignored for ``%s'' device\n", who
);
1072 p
->cfg
.speed
= MODEM_SPEED
;
1073 } else if (how
== PHYSICAL_FORCE_SYNC
&& !physical_IsSync(p
)) {
1074 log_Printf(LogWARN
, "Async device setting ignored for ``%s'' device\n",
1076 physical_SetSync(p
);
1081 physical_StopDeviceTimer(struct physical
*p
)
1083 if (p
->handler
&& p
->handler
->stoptimer
)
1084 (*p
->handler
->stoptimer
)(p
);
1088 physical_AwaitCarrier(struct physical
*p
)
1090 if (p
->handler
&& p
->handler
->awaitcarrier
)
1091 return (*p
->handler
->awaitcarrier
)(p
);
1098 physical_SetAsyncParams(struct physical
*p
, u_int32_t mymap
, u_int32_t hismap
)
1100 if (p
->handler
&& p
->handler
->setasyncparams
)
1101 return (*p
->handler
->setasyncparams
)(p
, mymap
, hismap
);
1103 async_SetLinkParams(&p
->async
, mymap
, hismap
);
1107 physical_Slot(struct physical
*p
)
1109 if (p
->handler
&& p
->handler
->slot
)
1110 return (*p
->handler
->slot
)(p
);