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"
106 #define PPPOTCPLINE "ppp"
108 static int physical_DescriptorWrite(struct fdescriptor
*, struct bundle
*,
112 physical_DeviceSize(void)
114 return sizeof(struct device
);
118 struct device
*(*create
)(struct physical
*);
119 struct device
*(*iov2device
)(int, struct physical
*, struct iovec
*,
120 int *, int, int *, int *);
121 unsigned (*DeviceSize
)(void);
123 { tty_Create
, tty_iov2device
, tty_DeviceSize
},
126 * This must come before ``udp'' so that the probe routine is
127 * able to identify it as a more specific type of SOCK_DGRAM.
129 { ether_Create
, ether_iov2device
, ether_DeviceSize
},
130 #ifdef EXPERIMENTAL_NETGRAPH
131 { ng_Create
, ng_iov2device
, ng_DeviceSize
},
135 /* Ditto for ATM devices */
136 { atm_Create
, atm_iov2device
, atm_DeviceSize
},
138 { tcp_Create
, tcp_iov2device
, tcp_DeviceSize
},
139 { udp_Create
, udp_iov2device
, udp_DeviceSize
},
140 { exec_Create
, exec_iov2device
, exec_DeviceSize
}
143 #define NDEVICES (sizeof devices / sizeof devices[0])
146 physical_UpdateSet(struct fdescriptor
*d
, fd_set
*r
, fd_set
*w
, fd_set
*e
,
149 return physical_doUpdateSet(d
, r
, w
, e
, n
, 0);
153 physical_SetDescriptor(struct physical
*p
)
155 p
->desc
.type
= PHYSICAL_DESCRIPTOR
;
156 p
->desc
.UpdateSet
= physical_UpdateSet
;
157 p
->desc
.IsSet
= physical_IsSet
;
158 p
->desc
.Read
= physical_DescriptorRead
;
159 p
->desc
.Write
= physical_DescriptorWrite
;
163 physical_Create(struct datalink
*dl
, int type
)
167 p
= (struct physical
*)malloc(sizeof(struct physical
));
171 p
->link
.type
= PHYSICAL_LINK
;
172 p
->link
.name
= dl
->name
;
173 p
->link
.len
= sizeof *p
;
175 /* The sample period is fixed - see physical2iov() & iov2physical() */
176 throughput_init(&p
->link
.stats
.total
, SAMPLE_PERIOD
);
177 p
->link
.stats
.parent
= dl
->bundle
->ncp
.mp
.active
?
178 &dl
->bundle
->ncp
.mp
.link
.stats
.total
: NULL
;
179 p
->link
.stats
.gather
= 1;
181 memset(p
->link
.Queue
, '\0', sizeof p
->link
.Queue
);
182 memset(p
->link
.proto_in
, '\0', sizeof p
->link
.proto_in
);
183 memset(p
->link
.proto_out
, '\0', sizeof p
->link
.proto_out
);
184 link_EmptyStack(&p
->link
);
187 physical_SetDescriptor(p
);
190 hdlc_Init(&p
->hdlc
, &p
->link
.lcp
);
191 async_Init(&p
->async
);
195 p
->connect_count
= 0;
198 *p
->name
.full
= '\0';
199 p
->name
.base
= p
->name
.full
;
202 p
->session_owner
= (pid_t
)-1;
204 p
->cfg
.rts_cts
= MODEM_CTSRTS
;
205 p
->cfg
.speed
= MODEM_SPEED
;
207 memcpy(p
->cfg
.devlist
, MODEM_LIST
, sizeof MODEM_LIST
);
208 p
->cfg
.ndev
= NMODEMS
;
209 p
->cfg
.cd
.necessity
= CD_DEFAULT
;
210 p
->cfg
.cd
.delay
= 0; /* reconfigured or device specific default */
212 lcp_Init(&p
->link
.lcp
, dl
->bundle
, &p
->link
, &dl
->fsmp
);
213 ccp_Init(&p
->link
.ccp
, dl
->bundle
, &p
->link
, &dl
->fsmp
);
218 static const struct parity
{
223 { "even", "P_EVEN", CS7
| PARENB
},
224 { "odd", "P_ODD", CS7
| PARENB
| PARODD
},
225 { "none", "P_ZERO", CS8
},
230 GetParityValue(const char *str
)
232 const struct parity
*pp
;
234 for (pp
= validparity
; pp
->name
; pp
++) {
235 if (strcasecmp(pp
->name
, str
) == 0 ||
236 strcasecmp(pp
->name1
, str
) == 0) {
244 physical_SetParity(struct physical
*p
, const char *str
)
246 struct termios rstio
;
249 val
= GetParityValue(str
);
253 tcgetattr(p
->fd
, &rstio
);
254 rstio
.c_cflag
&= ~(CSIZE
| PARODD
| PARENB
);
255 rstio
.c_cflag
|= val
;
256 tcsetattr(p
->fd
, TCSADRAIN
, &rstio
);
260 log_Printf(LogWARN
, "%s: %s: Invalid parity\n", p
->link
.name
, str
);
265 physical_GetSpeed(struct physical
*p
)
267 if (p
->handler
&& p
->handler
->speed
)
268 return (*p
->handler
->speed
)(p
);
274 physical_SetSpeed(struct physical
*p
, unsigned speed
)
276 if (UnsignedToSpeed(speed
) != B0
) {
277 p
->cfg
.speed
= speed
;
285 physical_Raw(struct physical
*p
)
287 if (p
->handler
&& p
->handler
->raw
)
288 return (*p
->handler
->raw
)(p
);
294 physical_Offline(struct physical
*p
)
296 if (p
->handler
&& p
->handler
->offline
)
297 (*p
->handler
->offline
)(p
);
298 log_Printf(LogPHASE
, "%s: Disconnected!\n", p
->link
.name
);
302 physical_Lock(struct physical
*p
)
306 if (*p
->name
.full
== '/' && p
->type
!= PHYS_DIRECT
&&
307 (res
= ID0uu_lock(p
->name
.base
)) != UU_LOCK_OK
) {
308 if (res
== UU_LOCK_INUSE
)
309 log_Printf(LogPHASE
, "%s: %s is in use\n", p
->link
.name
, p
->name
.full
);
311 log_Printf(LogPHASE
, "%s: %s is in use: uu_lock: %s\n",
312 p
->link
.name
, p
->name
.full
, uu_lockerr(res
));
320 physical_Unlock(struct physical
*p
)
322 if (*p
->name
.full
== '/' && p
->type
!= PHYS_DIRECT
&&
323 ID0uu_unlock(p
->name
.base
) == -1)
324 log_Printf(LogALERT
, "%s: Can't uu_unlock %s\n", p
->link
.name
,
329 physical_Close(struct physical
*p
)
337 log_Printf(LogDEBUG
, "%s: Close\n", p
->link
.name
);
339 if (p
->handler
&& p
->handler
->cooked
)
340 (*p
->handler
->cooked
)(p
);
342 physical_StopDeviceTimer(p
);
344 if (p
->handler
&& (p
->handler
->type
== TCP_DEVICE
||
345 p
->handler
->type
== UDP_DEVICE
))
346 /* Careful - we logged in on line ``ppp'' with IP as our host */
347 ID0logout(PPPOTCPLINE
, 1);
349 ID0logout(p
->name
.base
, 0);
352 newsid
= tcgetpgrp(p
->fd
) == getpgrp();
355 log_SetTtyCommandMode(p
->dl
);
357 throughput_stop(&p
->link
.stats
.total
);
358 throughput_log(&p
->link
.stats
.total
, LogPHASE
, p
->link
.name
);
360 if (p
->session_owner
!= (pid_t
)-1) {
361 log_Printf(LogPHASE
, "%s: HUPing %ld\n", p
->link
.name
,
362 (long)p
->session_owner
);
363 ID0kill(p
->session_owner
, SIGHUP
);
364 p
->session_owner
= (pid_t
)-1;
368 bundle_setsid(p
->dl
->bundle
, 0);
370 if (*p
->name
.full
== '/') {
371 snprintf(fn
, sizeof fn
, "%s%s.if", _PATH_VARRUN
, p
->name
.base
);
372 if (ID0unlink(fn
) == -1)
373 log_Printf(LogALERT
, "%s: Can't remove %s: %s\n",
374 p
->link
.name
, fn
, strerror(errno
));
377 if (p
->handler
&& p
->handler
->destroy
)
378 (*p
->handler
->destroy
)(p
);
380 p
->name
.base
= p
->name
.full
;
381 *p
->name
.full
= '\0';
385 physical_Destroy(struct physical
*p
)
388 throughput_destroy(&p
->link
.stats
.total
);
393 physical_DescriptorWrite(struct fdescriptor
*d
, struct bundle
*bundle __unused
,
394 const fd_set
*fdset __unused
)
396 struct physical
*p
= descriptor2physical(d
);
400 p
->out
= link_Dequeue(&p
->link
);
403 nw
= physical_Write(p
, MBUF_CTOP(p
->out
), p
->out
->m_len
);
404 log_Printf(LogDEBUG
, "%s: DescriptorWrite: wrote %d(%lu) to %d\n",
405 p
->link
.name
, nw
, (unsigned long)p
->out
->m_len
, p
->fd
);
408 p
->out
->m_offset
+= nw
;
409 if (p
->out
->m_len
== 0)
410 p
->out
= m_free(p
->out
);
415 else if (errno
!= ENOBUFS
) {
416 log_Printf(LogPHASE
, "%s: write (%d): %s\n", p
->link
.name
,
417 p
->fd
, strerror(errno
));
418 datalink_Down(p
->dl
, CLOSE_NORMAL
);
421 /* else we shouldn't really have been called ! select() is broken ! */
428 physical_ShowStatus(struct cmdargs
const *arg
)
430 struct physical
*p
= arg
->cx
->physical
;
435 prompt_Printf(arg
->prompt
, "Name: %s\n", p
->link
.name
);
436 prompt_Printf(arg
->prompt
, " State: ");
438 prompt_Printf(arg
->prompt
, "closed\n");
440 slot
= physical_Slot(p
);
441 if (p
->handler
&& p
->handler
->openinfo
) {
443 prompt_Printf(arg
->prompt
, "open (%s)\n", (*p
->handler
->openinfo
)(p
));
445 prompt_Printf(arg
->prompt
, "open (%s, port %d)\n",
446 (*p
->handler
->openinfo
)(p
), slot
);
447 } else if (slot
== -1)
448 prompt_Printf(arg
->prompt
, "open\n");
450 prompt_Printf(arg
->prompt
, "open (port %d)\n", slot
);
453 prompt_Printf(arg
->prompt
, " Device: %s",
454 *p
->name
.full
? p
->name
.full
:
455 p
->type
== PHYS_DIRECT
? "unknown" : "N/A");
456 if (p
->session_owner
!= (pid_t
)-1)
457 prompt_Printf(arg
->prompt
, " (session owner: %ld)", (long)p
->session_owner
);
459 prompt_Printf(arg
->prompt
, "\n Link Type: %s\n", mode2Nam(p
->type
));
460 prompt_Printf(arg
->prompt
, " Connect Count: %d\n", p
->connect_count
);
462 if (p
->fd
>= 0 && ioctl(p
->fd
, TIOCOUTQ
, &n
) >= 0)
463 prompt_Printf(arg
->prompt
, " Physical outq: %d\n", n
);
466 prompt_Printf(arg
->prompt
, " Queued Packets: %lu\n",
467 (u_long
)link_QueueLen(&p
->link
));
468 prompt_Printf(arg
->prompt
, " Phone Number: %s\n", arg
->cx
->phone
.chosen
);
470 prompt_Printf(arg
->prompt
, "\nDefaults:\n");
472 prompt_Printf(arg
->prompt
, " Device List: ");
473 dev
= p
->cfg
.devlist
;
474 for (n
= 0; n
< p
->cfg
.ndev
; n
++) {
476 prompt_Printf(arg
->prompt
, ", ");
477 prompt_Printf(arg
->prompt
, "\"%s\"", dev
);
478 dev
+= strlen(dev
) + 1;
481 prompt_Printf(arg
->prompt
, "\n Characteristics: ");
482 if (physical_IsSync(arg
->cx
->physical
))
483 prompt_Printf(arg
->prompt
, "sync");
485 prompt_Printf(arg
->prompt
, "%dbps", p
->cfg
.speed
);
487 switch (p
->cfg
.parity
& CSIZE
) {
489 prompt_Printf(arg
->prompt
, ", cs7");
492 prompt_Printf(arg
->prompt
, ", cs8");
495 if (p
->cfg
.parity
& PARENB
) {
496 if (p
->cfg
.parity
& PARODD
)
497 prompt_Printf(arg
->prompt
, ", odd parity");
499 prompt_Printf(arg
->prompt
, ", even parity");
501 prompt_Printf(arg
->prompt
, ", no parity");
503 prompt_Printf(arg
->prompt
, ", CTS/RTS %s\n", (p
->cfg
.rts_cts
? "on" : "off"));
505 prompt_Printf(arg
->prompt
, " CD check delay: ");
506 cd
= p
->handler
? &p
->handler
->cd
: &p
->cfg
.cd
;
507 if (cd
->necessity
== CD_NOTREQUIRED
)
508 prompt_Printf(arg
->prompt
, "no cd");
509 else if (p
->cfg
.cd
.necessity
== CD_DEFAULT
) {
510 prompt_Printf(arg
->prompt
, "device specific");
512 prompt_Printf(arg
->prompt
, "%d second%s", p
->cfg
.cd
.delay
,
513 p
->cfg
.cd
.delay
== 1 ? "" : "s");
514 if (p
->cfg
.cd
.necessity
== CD_REQUIRED
)
515 prompt_Printf(arg
->prompt
, " (required!)");
517 prompt_Printf(arg
->prompt
, "\n\n");
519 throughput_disp(&p
->link
.stats
.total
, arg
->prompt
);
525 physical_DescriptorRead(struct fdescriptor
*d
, struct bundle
*bundle
,
526 const fd_set
*fdset __unused
)
528 struct physical
*p
= descriptor2physical(d
);
532 rbuff
= p
->input
.buf
+ p
->input
.sz
;
534 /* something to read */
535 n
= physical_Read(p
, rbuff
, sizeof p
->input
.buf
- p
->input
.sz
);
536 log_Printf(LogDEBUG
, "%s: DescriptorRead: read %d/%d from %d\n",
537 p
->link
.name
, n
, (int)(sizeof p
->input
.buf
- p
->input
.sz
), p
->fd
);
540 log_Printf(LogPHASE
, "%s: read (%d): %s\n", p
->link
.name
, p
->fd
,
543 log_Printf(LogPHASE
, "%s: read (%d): Got zero bytes\n",
544 p
->link
.name
, p
->fd
);
545 datalink_Down(p
->dl
, CLOSE_NORMAL
);
549 rbuff
-= p
->input
.sz
;
552 if (p
->link
.lcp
.fsm
.state
<= ST_CLOSED
) {
553 if (p
->type
!= PHYS_DEDICATED
) {
554 found
= hdlc_Detect((u_char
const **)(void *)&rbuff
, n
, physical_IsSync(p
));
555 if (rbuff
!= p
->input
.buf
)
556 log_WritePrompts(p
->dl
, "%.*s", (int)(rbuff
- p
->input
.buf
),
558 p
->input
.sz
= n
- (rbuff
- p
->input
.buf
);
561 /* LCP packet is detected. Turn ourselves into packet mode */
562 log_Printf(LogPHASE
, "%s: PPP packet detected, coming up\n",
564 log_SetTtyCommandMode(p
->dl
);
565 datalink_Up(p
->dl
, 0, 1);
566 link_PullPacket(&p
->link
, rbuff
, p
->input
.sz
, bundle
);
569 bcopy(rbuff
, p
->input
.buf
, p
->input
.sz
);
571 /* In -dedicated mode, we just discard input until LCP is started */
574 link_PullPacket(&p
->link
, rbuff
, n
, bundle
);
578 iov2physical(struct datalink
*dl
, struct iovec
*iov
, int *niov
, int maxiov
,
579 int fd
, int *auxfd
, int *nauxfd
)
585 p
= (struct physical
*)iov
[(*niov
)++].iov_base
;
586 p
->link
.name
= dl
->name
;
587 memset(p
->link
.Queue
, '\0', sizeof p
->link
.Queue
);
589 p
->desc
.UpdateSet
= physical_UpdateSet
;
590 p
->desc
.IsSet
= physical_IsSet
;
591 p
->desc
.Read
= physical_DescriptorRead
;
592 p
->desc
.Write
= physical_DescriptorWrite
;
593 p
->type
= PHYS_DIRECT
;
596 p
->connect_count
= 1;
598 physical_SetDevice(p
, p
->name
.full
);
600 p
->link
.lcp
.fsm
.bundle
= dl
->bundle
;
601 p
->link
.lcp
.fsm
.link
= &p
->link
;
602 memset(&p
->link
.lcp
.fsm
.FsmTimer
, '\0', sizeof p
->link
.lcp
.fsm
.FsmTimer
);
603 memset(&p
->link
.lcp
.fsm
.OpenTimer
, '\0', sizeof p
->link
.lcp
.fsm
.OpenTimer
);
604 memset(&p
->link
.lcp
.fsm
.StoppedTimer
, '\0',
605 sizeof p
->link
.lcp
.fsm
.StoppedTimer
);
606 p
->link
.lcp
.fsm
.parent
= &dl
->fsmp
;
607 lcp_SetupCallbacks(&p
->link
.lcp
);
609 p
->link
.ccp
.fsm
.bundle
= dl
->bundle
;
610 p
->link
.ccp
.fsm
.link
= &p
->link
;
611 /* Our in.state & out.state are NULL (no link-level ccp yet) */
612 memset(&p
->link
.ccp
.fsm
.FsmTimer
, '\0', sizeof p
->link
.ccp
.fsm
.FsmTimer
);
613 memset(&p
->link
.ccp
.fsm
.OpenTimer
, '\0', sizeof p
->link
.ccp
.fsm
.OpenTimer
);
614 memset(&p
->link
.ccp
.fsm
.StoppedTimer
, '\0',
615 sizeof p
->link
.ccp
.fsm
.StoppedTimer
);
616 p
->link
.ccp
.fsm
.parent
= &dl
->fsmp
;
617 ccp_SetupCallbacks(&p
->link
.ccp
);
619 p
->hdlc
.lqm
.owner
= &p
->link
.lcp
;
620 p
->hdlc
.ReportTimer
.state
= TIMER_STOPPED
;
621 p
->hdlc
.lqm
.timer
.state
= TIMER_STOPPED
;
624 p
->link
.stats
.total
.in
.SampleOctets
= (long long *)iov
[(*niov
)++].iov_base
;
625 p
->link
.stats
.total
.out
.SampleOctets
= (long long *)iov
[(*niov
)++].iov_base
;
626 p
->link
.stats
.parent
= dl
->bundle
->ncp
.mp
.active
?
627 &dl
->bundle
->ncp
.mp
.link
.stats
.total
: NULL
;
628 p
->link
.stats
.gather
= 1;
630 type
= (long)p
->handler
;
632 for (h
= 0; h
< NDEVICES
&& p
->handler
== NULL
; h
++)
633 p
->handler
= (*devices
[h
].iov2device
)(type
, p
, iov
, niov
, maxiov
,
635 if (p
->handler
== NULL
) {
636 log_Printf(LogPHASE
, "%s: Unknown link type\n", p
->link
.name
);
637 free(iov
[(*niov
)++].iov_base
);
638 physical_SetupStack(p
, "unknown", PHYSICAL_NOFORCE
);
640 log_Printf(LogPHASE
, "%s: Device %s, link type is %s\n",
641 p
->link
.name
, p
->name
.full
, p
->handler
->name
);
643 if (p
->hdlc
.lqm
.method
&& p
->hdlc
.lqm
.timer
.load
)
644 lqr_reStart(&p
->link
.lcp
);
645 hdlc_StartTimer(&p
->hdlc
);
647 throughput_restart(&p
->link
.stats
.total
, "physical throughput",
648 Enabled(dl
->bundle
, OPT_THROUGHPUT
));
654 physical_MaxDeviceSize(void)
656 unsigned biggest
, sz
, n
;
658 biggest
= sizeof(struct device
);
659 for (n
= 0; n
< NDEVICES
; n
++)
660 if (devices
[n
].DeviceSize
) {
661 sz
= (*devices
[n
].DeviceSize
)();
670 physical2iov(struct physical
*p
, struct iovec
*iov
, int *niov
, int maxiov
,
671 int *auxfd
, int *nauxfd
)
678 hdlc_StopTimer(&p
->hdlc
);
680 timer_Stop(&p
->link
.lcp
.fsm
.FsmTimer
);
681 timer_Stop(&p
->link
.ccp
.fsm
.FsmTimer
);
682 timer_Stop(&p
->link
.lcp
.fsm
.OpenTimer
);
683 timer_Stop(&p
->link
.ccp
.fsm
.OpenTimer
);
684 timer_Stop(&p
->link
.lcp
.fsm
.StoppedTimer
);
685 timer_Stop(&p
->link
.ccp
.fsm
.StoppedTimer
);
688 p
->handler
= (struct device
*)(long)p
->handler
->type
;
691 if (Enabled(p
->dl
->bundle
, OPT_KEEPSESSION
) ||
692 tcgetpgrp(p
->fd
) == getpgrp())
693 p
->session_owner
= getpid(); /* So I'll eventually get HUP'd */
695 p
->session_owner
= (pid_t
)-1;
696 timer_Stop(&p
->link
.stats
.total
.Timer
);
699 if (*niov
+ 2 >= maxiov
) {
700 log_Printf(LogERROR
, "physical2iov: No room for physical + throughput"
707 iov
[*niov
].iov_base
= (void *)p
;
708 iov
[*niov
].iov_len
= sizeof *p
;
711 iov
[*niov
].iov_base
= p
? (void *)p
->link
.stats
.total
.in
.SampleOctets
: NULL
;
712 iov
[*niov
].iov_len
= SAMPLE_PERIOD
* sizeof(long long);
714 iov
[*niov
].iov_base
= p
? (void *)p
->link
.stats
.total
.out
.SampleOctets
: NULL
;
715 iov
[*niov
].iov_len
= SAMPLE_PERIOD
* sizeof(long long);
718 sz
= physical_MaxDeviceSize();
720 if (h
&& h
->device2iov
)
721 (*h
->device2iov
)(h
, iov
, niov
, maxiov
, auxfd
, nauxfd
);
723 iov
[*niov
].iov_base
= malloc(sz
);
725 memcpy(iov
[*niov
].iov_base
, h
, sizeof *h
);
726 iov
[*niov
].iov_len
= sz
;
730 iov
[*niov
].iov_base
= NULL
;
731 iov
[*niov
].iov_len
= sz
;
735 return p
? p
->fd
: 0;
739 physical_LockedDevice(struct physical
*p
)
741 if (p
->fd
>= 0 && *p
->name
.full
== '/' && p
->type
!= PHYS_DIRECT
)
748 physical_ChangedPid(struct physical
*p
, pid_t newpid
)
750 if (physical_LockedDevice(p
)) {
753 if ((res
= ID0uu_lock_txfr(p
->name
.base
, newpid
)) != UU_LOCK_OK
)
754 log_Printf(LogPHASE
, "uu_lock_txfr: %s\n", uu_lockerr(res
));
759 physical_IsSync(struct physical
*p
)
761 return p
->cfg
.speed
== 0;
765 physical_DeviceMTU(struct physical
*p
)
767 return p
->handler
? p
->handler
->mtu
: 0;
771 physical_GetDevice(struct physical
*p
)
777 physical_SetDeviceList(struct physical
*p
, int argc
, const char *const *argv
)
782 p
->cfg
.devlist
[sizeof p
->cfg
.devlist
- 1] = '\0';
783 for (f
= 0, pos
= 0; f
< argc
&& pos
< sizeof p
->cfg
.devlist
- 1; f
++) {
785 p
->cfg
.devlist
[pos
++] = '\0';
786 strncpy(p
->cfg
.devlist
+ pos
, argv
[f
], sizeof p
->cfg
.devlist
- pos
- 1);
787 pos
+= strlen(p
->cfg
.devlist
+ pos
);
793 physical_SetSync(struct physical
*p
)
799 physical_SetRtsCts(struct physical
*p
, int enable
)
801 p
->cfg
.rts_cts
= enable
? 1 : 0;
806 physical_Read(struct physical
*p
, void *buf
, size_t nbytes
)
810 if (p
->handler
&& p
->handler
->read
)
811 ret
= (*p
->handler
->read
)(p
, buf
, nbytes
);
813 ret
= read(p
->fd
, buf
, nbytes
);
815 log_DumpBuff(LogPHYSICAL
, "read", buf
, ret
);
821 physical_Write(struct physical
*p
, const void *buf
, size_t nbytes
)
823 log_DumpBuff(LogPHYSICAL
, "write", buf
, nbytes
);
825 if (p
->handler
&& p
->handler
->write
)
826 return (*p
->handler
->write
)(p
, buf
, nbytes
);
828 return write(p
->fd
, buf
, nbytes
);
832 physical_doUpdateSet(struct fdescriptor
*d
, fd_set
*r
, fd_set
*w
, fd_set
*e
,
835 struct physical
*p
= descriptor2physical(d
);
842 log_Printf(LogTIMER
, "%s: fdset(r) %d\n", p
->link
.name
, p
->fd
);
847 log_Printf(LogTIMER
, "%s: fdset(e) %d\n", p
->link
.name
, p
->fd
);
850 if (w
&& (force
|| link_QueueLen(&p
->link
) || p
->out
)) {
852 log_Printf(LogTIMER
, "%s: fdset(w) %d\n", p
->link
.name
, p
->fd
);
855 if (sets
&& *n
< p
->fd
+ 1)
863 physical_RemoveFromSet(struct physical
*p
, fd_set
*r
, fd_set
*w
, fd_set
*e
)
865 if (p
->handler
&& p
->handler
->removefromset
)
866 return (*p
->handler
->removefromset
)(p
, r
, w
, e
);
872 if (r
&& FD_ISSET(p
->fd
, r
)) {
874 log_Printf(LogTIMER
, "%s: fdunset(r) %d\n", p
->link
.name
, p
->fd
);
877 if (e
&& FD_ISSET(p
->fd
, e
)) {
879 log_Printf(LogTIMER
, "%s: fdunset(e) %d\n", p
->link
.name
, p
->fd
);
882 if (w
&& FD_ISSET(p
->fd
, w
)) {
884 log_Printf(LogTIMER
, "%s: fdunset(w) %d\n", p
->link
.name
, p
->fd
);
894 physical_IsSet(struct fdescriptor
*d
, const fd_set
*fdset
)
896 struct physical
*p
= descriptor2physical(d
);
897 return p
->fd
>= 0 && FD_ISSET(p
->fd
, fdset
);
901 physical_Login(struct physical
*p
, const char *name
)
903 if (p
->type
== PHYS_DIRECT
&& *p
->name
.base
&& !p
->Utmp
) {
908 memset(&ut
, 0, sizeof ut
);
910 strncpy(ut
.ut_name
, name
, sizeof ut
.ut_name
);
911 if (p
->handler
&& (p
->handler
->type
== TCP_DEVICE
||
912 p
->handler
->type
== UDP_DEVICE
)) {
913 strncpy(ut
.ut_line
, PPPOTCPLINE
, sizeof ut
.ut_line
);
914 strncpy(ut
.ut_host
, p
->name
.base
, sizeof ut
.ut_host
);
915 colon
= memchr(ut
.ut_host
, ':', sizeof ut
.ut_host
);
919 strncpy(ut
.ut_line
, p
->name
.base
, sizeof ut
.ut_line
);
920 if ((connstr
= getenv("CONNECT")))
921 /* mgetty sets this to the connection speed */
922 strncpy(ut
.ut_host
, connstr
, sizeof ut
.ut_host
);
924 p
->Utmp
= ut
.ut_time
;
929 physical_SetMode(struct physical
*p
, int mode
)
931 if ((p
->type
& (PHYS_DIRECT
|PHYS_DEDICATED
) ||
932 mode
& (PHYS_DIRECT
|PHYS_DEDICATED
)) &&
933 (!(p
->type
& PHYS_DIRECT
) || !(mode
& PHYS_BACKGROUND
))) {
934 /* Note: The -direct -> -background is for callback ! */
935 log_Printf(LogWARN
, "%s: Cannot change mode %s to %s\n", p
->link
.name
,
936 mode2Nam(p
->type
), mode2Nam(mode
));
944 physical_DeleteQueue(struct physical
*p
)
950 link_DeleteQueue(&p
->link
);
954 physical_SetDevice(struct physical
*p
, const char *name
)
956 int len
= strlen(_PATH_DEV
);
958 if (name
!= p
->name
.full
) {
959 strncpy(p
->name
.full
, name
, sizeof p
->name
.full
- 1);
960 p
->name
.full
[sizeof p
->name
.full
- 1] = '\0';
962 p
->name
.base
= *p
->name
.full
== '!' ? p
->name
.full
+ 1 :
963 strncmp(p
->name
.full
, _PATH_DEV
, len
) ?
964 p
->name
.full
: p
->name
.full
+ len
;
968 physical_Found(struct physical
*p
)
973 if (*p
->name
.full
== '/') {
974 snprintf(fn
, sizeof fn
, "%s%s.if", _PATH_VARRUN
, p
->name
.base
);
975 lockfile
= ID0fopen(fn
, "w");
976 if (lockfile
!= NULL
) {
977 fprintf(lockfile
, "%s%d\n", TUN_NAME
, p
->dl
->bundle
->unit
);
980 log_Printf(LogALERT
, "%s: Can't create %s: %s\n",
981 p
->link
.name
, fn
, strerror(errno
));
984 throughput_start(&p
->link
.stats
.total
, "physical throughput",
985 Enabled(p
->dl
->bundle
, OPT_THROUGHPUT
));
989 log_Printf(LogPHASE
, "%s: Connected!\n", p
->link
.name
);
993 physical_Open(struct physical
*p
)
996 int devno
, wasfd
, err
;
1000 log_Printf(LogDEBUG
, "%s: Open: Modem is already open!\n", p
->link
.name
);
1001 /* We're going back into "term" mode */
1002 else if (p
->type
== PHYS_DIRECT
) {
1003 physical_SetDevice(p
, "");
1004 p
->fd
= STDIN_FILENO
;
1005 for (h
= 0; h
< NDEVICES
&& p
->handler
== NULL
&& p
->fd
>= 0; h
++)
1006 p
->handler
= (*devices
[h
].create
)(p
);
1008 if (p
->handler
== NULL
) {
1009 physical_SetupStack(p
, "unknown", PHYSICAL_NOFORCE
);
1010 log_Printf(LogDEBUG
, "%s: stdin is unidentified\n", p
->link
.name
);
1015 dev
= p
->cfg
.devlist
;
1017 while (devno
< p
->cfg
.ndev
&& p
->fd
< 0) {
1018 physical_SetDevice(p
, dev
);
1019 if (physical_Lock(p
)) {
1022 if (*p
->name
.full
== '/') {
1023 p
->fd
= ID0open(p
->name
.full
, O_RDWR
| O_NONBLOCK
);
1029 for (h
= 0; h
< NDEVICES
&& p
->handler
== NULL
; h
++)
1030 if ((p
->handler
= (*devices
[h
].create
)(p
)) == NULL
&& wasfd
!= p
->fd
)
1034 if (h
== NDEVICES
) {
1036 log_Printf(LogWARN
, "%s: %s: %s\n", p
->link
.name
, p
->name
.full
,
1039 log_Printf(LogWARN
, "%s: Device (%s) must begin with a '/',"
1040 " a '!' or contain at least one ':'\n", p
->link
.name
,
1047 dev
+= strlen(dev
) + 1;
1056 physical_SetupStack(struct physical
*p
, const char *who
, int how
)
1058 link_EmptyStack(&p
->link
);
1059 if (how
== PHYSICAL_FORCE_SYNC
|| how
== PHYSICAL_FORCE_SYNCNOACF
||
1060 (how
== PHYSICAL_NOFORCE
&& physical_IsSync(p
)))
1061 link_Stack(&p
->link
, &synclayer
);
1063 link_Stack(&p
->link
, &asynclayer
);
1064 link_Stack(&p
->link
, &hdlclayer
);
1066 if (how
!= PHYSICAL_FORCE_SYNCNOACF
)
1067 link_Stack(&p
->link
, &acflayer
);
1068 link_Stack(&p
->link
, &protolayer
);
1069 link_Stack(&p
->link
, &lqrlayer
);
1070 link_Stack(&p
->link
, &ccplayer
);
1071 link_Stack(&p
->link
, &vjlayer
);
1072 link_Stack(&p
->link
, &tcpmsslayer
);
1074 link_Stack(&p
->link
, &natlayer
);
1076 if (how
== PHYSICAL_FORCE_ASYNC
&& physical_IsSync(p
)) {
1077 log_Printf(LogWARN
, "Sync device setting ignored for ``%s'' device\n", who
);
1078 p
->cfg
.speed
= MODEM_SPEED
;
1079 } else if (how
== PHYSICAL_FORCE_SYNC
&& !physical_IsSync(p
)) {
1080 log_Printf(LogWARN
, "Async device setting ignored for ``%s'' device\n",
1082 physical_SetSync(p
);
1087 physical_StopDeviceTimer(struct physical
*p
)
1089 if (p
->handler
&& p
->handler
->stoptimer
)
1090 (*p
->handler
->stoptimer
)(p
);
1094 physical_AwaitCarrier(struct physical
*p
)
1096 if (p
->handler
&& p
->handler
->awaitcarrier
)
1097 return (*p
->handler
->awaitcarrier
)(p
);
1104 physical_SetAsyncParams(struct physical
*p
, u_int32_t mymap
, u_int32_t hismap
)
1106 if (p
->handler
&& p
->handler
->setasyncparams
)
1107 return (*p
->handler
->setasyncparams
)(p
, mymap
, hismap
);
1109 async_SetLinkParams(&p
->async
, mymap
, hismap
);
1113 physical_Slot(struct physical
*p
)
1115 if (p
->handler
&& p
->handler
->slot
)
1116 return (*p
->handler
->slot
)(p
);