1 /*
2 * Written by Eivind Eklund <eivind@yes.no>
3 * for Yes Interactive
4 *
5 * Copyright (C) 1998, Yes Interactive. All rights reserved.
6 *
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.
18 *
19 */
20
21 #include <sys/param.h>
22 #include <netinet/in.h>
23 #include <netinet/in_systm.h>
24 #include <netinet/ip.h>
25 #include <sys/socket.h>
26 #include <sys/time.h>
27 #include <sys/un.h>
28
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <paths.h>
32 #ifdef NOSUID
33 #include <signal.h>
34 #endif
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/uio.h>
40 #include <sysexits.h>
41 #include <termios.h>
42 #include <time.h>
43 #include <unistd.h>
44 #include <utmpx.h>
45 #if defined(__OpenBSD__) || defined(__NetBSD__)
46 #include <sys/ioctl.h>
47 #include <util.h>
48 #else
49 #include <libutil.h>
50 #endif
51
52 #include "layer.h"
53 #ifndef NONAT
54 #include "nat_cmd.h"
55 #endif
56 #include "proto.h"
57 #include "acf.h"
58 #include "vjcomp.h"
59 #include "defs.h"
60 #include "command.h"
61 #include "mbuf.h"
62 #include "log.h"
63 #include "id.h"
64 #include "timer.h"
65 #include "fsm.h"
66 #include "lqr.h"
67 #include "hdlc.h"
68 #include "lcp.h"
69 #include "throughput.h"
70 #include "sync.h"
71 #include "async.h"
72 #include "iplist.h"
73 #include "slcompress.h"
74 #include "ncpaddr.h"
75 #include "ipcp.h"
76 #include "filter.h"
77 #include "descriptor.h"
78 #include "ccp.h"
79 #include "link.h"
80 #include "physical.h"
81 #include "mp.h"
82 #ifndef NORADIUS
83 #include "radius.h"
84 #endif
85 #include "ipv6cp.h"
86 #include "ncp.h"
87 #include "bundle.h"
88 #include "prompt.h"
89 #include "chat.h"
90 #include "auth.h"
91 #include "main.h"
92 #include "chap.h"
93 #include "cbcp.h"
94 #include "datalink.h"
95 #include "tcp.h"
96 #include "udp.h"
97 #include "exec.h"
98 #include "tty.h"
99 #ifndef NONETGRAPH
100 #include "ether.h"
101 #include "netgraph.h"
102 #endif
103 #include "tcpmss.h"
104
105 static int physical_DescriptorWrite(struct fdescriptor *, struct bundle *,
106 const fd_set *);
107
108 static unsigned
physical_DeviceSize(void)109 physical_DeviceSize(void)
110 {
111 return sizeof(struct device);
112 }
113
114 struct {
115 struct device *(*create)(struct physical *);
116 struct device *(*iov2device)(int, struct physical *, struct iovec *,
117 int *, int, int *, int *);
118 unsigned (*DeviceSize)(void);
119 } devices[] = {
120 { tty_Create, tty_iov2device, tty_DeviceSize },
121 #ifndef NONETGRAPH
122 /*
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.
125 */
126 { ether_Create, ether_iov2device, ether_DeviceSize },
127 #ifdef EXPERIMENTAL_NETGRAPH
128 { ng_Create, ng_iov2device, ng_DeviceSize },
129 #endif
130 #endif
131 { tcp_Create, tcp_iov2device, tcp_DeviceSize },
132 { udp_Create, udp_iov2device, udp_DeviceSize },
133 { exec_Create, exec_iov2device, exec_DeviceSize }
134 };
135
136 #define NDEVICES (sizeof devices / sizeof devices[0])
137
138 static int
physical_UpdateSet(struct fdescriptor * d,fd_set * r,fd_set * w,fd_set * e,int * n)139 physical_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e,
140 int *n)
141 {
142 return physical_doUpdateSet(d, r, w, e, n, 0);
143 }
144
145 void
physical_SetDescriptor(struct physical * p)146 physical_SetDescriptor(struct physical *p)
147 {
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;
153 }
154
155 struct physical *
physical_Create(struct datalink * dl,int type)156 physical_Create(struct datalink *dl, int type)
157 {
158 struct physical *p;
159
160 p = (struct physical *)malloc(sizeof(struct physical));
161 if (!p)
162 return NULL;
163
164 p->link.type = PHYSICAL_LINK;
165 p->link.name = dl->name;
166 p->link.len = sizeof *p;
167
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;
173
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);
178
179 p->handler = NULL;
180 physical_SetDescriptor(p);
181 p->type = type;
182
183 hdlc_Init(&p->hdlc, &p->link.lcp);
184 async_Init(&p->async);
185
186 p->fd = -1;
187 p->out = NULL;
188 p->connect_count = 0;
189 p->dl = dl;
190 p->input.sz = 0;
191 *p->name.full = '\0';
192 p->name.base = p->name.full;
193
194 p->Utmp = 0;
195 p->session_owner = (pid_t)-1;
196
197 p->cfg.rts_cts = MODEM_CTSRTS;
198 p->cfg.speed = MODEM_SPEED;
199 p->cfg.parity = CS8;
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 */
204
205 lcp_Init(&p->link.lcp, dl->bundle, &p->link, &dl->fsmp);
206 ccp_Init(&p->link.ccp, dl->bundle, &p->link, &dl->fsmp);
207
208 return p;
209 }
210
211 static const struct parity {
212 const char *name;
213 const char *name1;
214 int set;
215 } validparity[] = {
216 { "even", "P_EVEN", CS7 | PARENB },
217 { "odd", "P_ODD", CS7 | PARENB | PARODD },
218 { "none", "P_ZERO", CS8 },
219 { NULL, NULL, 0 },
220 };
221
222 static int
GetParityValue(const char * str)223 GetParityValue(const char *str)
224 {
225 const struct parity *pp;
226
227 for (pp = validparity; pp->name; pp++) {
228 if (strcasecmp(pp->name, str) == 0 ||
229 strcasecmp(pp->name1, str) == 0) {
230 return pp->set;
231 }
232 }
233 return (-1);
234 }
235
236 int
physical_SetParity(struct physical * p,const char * str)237 physical_SetParity(struct physical *p, const char *str)
238 {
239 struct termios rstio;
240 int val;
241
242 val = GetParityValue(str);
243 if (val > 0) {
244 p->cfg.parity = val;
245 if (p->fd >= 0) {
246 tcgetattr(p->fd, &rstio);
247 rstio.c_cflag &= ~(CSIZE | PARODD | PARENB);
248 rstio.c_cflag |= val;
249 tcsetattr(p->fd, TCSADRAIN, &rstio);
250 }
251 return 0;
252 }
253 log_Printf(LogWARN, "%s: %s: Invalid parity\n", p->link.name, str);
254 return -1;
255 }
256
257 unsigned
physical_GetSpeed(struct physical * p)258 physical_GetSpeed(struct physical *p)
259 {
260 if (p->handler && p->handler->speed)
261 return (*p->handler->speed)(p);
262
263 return 0;
264 }
265
266 int
physical_SetSpeed(struct physical * p,unsigned speed)267 physical_SetSpeed(struct physical *p, unsigned speed)
268 {
269 if (UnsignedToSpeed(speed) != B0) {
270 p->cfg.speed = speed;
271 return 1;
272 }
273
274 return 0;
275 }
276
277 int
physical_Raw(struct physical * p)278 physical_Raw(struct physical *p)
279 {
280 if (p->handler && p->handler->raw)
281 return (*p->handler->raw)(p);
282
283 return 1;
284 }
285
286 void
physical_Offline(struct physical * p)287 physical_Offline(struct physical *p)
288 {
289 if (p->handler && p->handler->offline)
290 (*p->handler->offline)(p);
291 log_Printf(LogPHASE, "%s: Disconnected!\n", p->link.name);
292 }
293
294 static int
physical_Lock(struct physical * p)295 physical_Lock(struct physical *p)
296 {
297 int res;
298
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);
303 else
304 log_Printf(LogPHASE, "%s: %s is in use: uu_lock: %s\n",
305 p->link.name, p->name.full, uu_lockerr(res));
306 return 0;
307 }
308
309 return 1;
310 }
311
312 static void
physical_Unlock(struct physical * p)313 physical_Unlock(struct physical *p)
314 {
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,
318 p->name.base);
319 }
320
321 void
physical_Close(struct physical * p)322 physical_Close(struct physical *p)
323 {
324 int newsid;
325 char fn[PATH_MAX];
326 struct utmpx ut;
327
328 if (p->fd < 0)
329 return;
330
331 log_Printf(LogDEBUG, "%s: Close\n", p->link.name);
332
333 if (p->handler && p->handler->cooked)
334 (*p->handler->cooked)(p);
335
336 physical_StopDeviceTimer(p);
337 if (p->Utmp) {
338 memset(&ut, 0, sizeof ut);
339 ut.ut_type = DEAD_PROCESS;
340 gettimeofday(&ut.ut_tv, NULL);
341 snprintf(ut.ut_id, sizeof ut.ut_id, "%xppp", (int)getpid());
342 ID0logout(&ut);
343 p->Utmp = 0;
344 }
345 newsid = tcgetpgrp(p->fd) == getpgrp();
346 close(p->fd);
347 p->fd = -1;
348 log_SetTtyCommandMode(p->dl);
349
350 throughput_stop(&p->link.stats.total);
351 throughput_log(&p->link.stats.total, LogPHASE, p->link.name);
352
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;
358 }
359
360 if (newsid)
361 bundle_setsid(p->dl->bundle, 0);
362
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));
368 }
369 physical_Unlock(p);
370 if (p->handler && p->handler->destroy)
371 (*p->handler->destroy)(p);
372 p->handler = NULL;
373 p->name.base = p->name.full;
374 *p->name.full = '\0';
375 }
376
377 void
physical_Destroy(struct physical * p)378 physical_Destroy(struct physical *p)
379 {
380 physical_Close(p);
381 throughput_destroy(&p->link.stats.total);
382 free(p);
383 }
384
385 static int
physical_DescriptorWrite(struct fdescriptor * d,struct bundle * bundle __unused,const fd_set * fdset __unused)386 physical_DescriptorWrite(struct fdescriptor *d, struct bundle *bundle __unused,
387 const fd_set *fdset __unused)
388 {
389 struct physical *p = descriptor2physical(d);
390 int nw, result = 0;
391
392 if (p->out == NULL)
393 p->out = link_Dequeue(&p->link);
394
395 if (p->out) {
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);
399 if (nw > 0) {
400 p->out->m_len -= nw;
401 p->out->m_offset += nw;
402 if (p->out->m_len == 0)
403 p->out = m_free(p->out);
404 result = 1;
405 } else if (nw < 0) {
406 if (errno == EAGAIN)
407 result = 1;
408 else if (errno != ENOBUFS) {
409 log_Printf(LogPHASE, "%s: write (fd %d, len %zd): %s\n", p->link.name,
410 p->fd, p->out->m_len, strerror(errno));
411 datalink_Down(p->dl, CLOSE_NORMAL);
412 }
413 }
414 /* else we shouldn't really have been called ! select() is broken ! */
415 }
416
417 return result;
418 }
419
420 int
physical_ShowStatus(struct cmdargs const * arg)421 physical_ShowStatus(struct cmdargs const *arg)
422 {
423 struct physical *p = arg->cx->physical;
424 struct cd *cd;
425 const char *dev;
426 int n, slot;
427
428 prompt_Printf(arg->prompt, "Name: %s\n", p->link.name);
429 prompt_Printf(arg->prompt, " State: ");
430 if (p->fd < 0)
431 prompt_Printf(arg->prompt, "closed\n");
432 else {
433 slot = physical_Slot(p);
434 if (p->handler && p->handler->openinfo) {
435 if (slot == -1)
436 prompt_Printf(arg->prompt, "open (%s)\n", (*p->handler->openinfo)(p));
437 else
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");
442 else
443 prompt_Printf(arg->prompt, "open (port %d)\n", slot);
444 }
445
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);
451
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);
454 #ifdef TIOCOUTQ
455 if (p->fd >= 0 && ioctl(p->fd, TIOCOUTQ, &n) >= 0)
456 prompt_Printf(arg->prompt, " Physical outq: %d\n", n);
457 #endif
458
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);
462
463 prompt_Printf(arg->prompt, "\nDefaults:\n");
464
465 prompt_Printf(arg->prompt, " Device List: ");
466 dev = p->cfg.devlist;
467 for (n = 0; n < p->cfg.ndev; n++) {
468 if (n)
469 prompt_Printf(arg->prompt, ", ");
470 prompt_Printf(arg->prompt, "\"%s\"", dev);
471 dev += strlen(dev) + 1;
472 }
473
474 prompt_Printf(arg->prompt, "\n Characteristics: ");
475 if (physical_IsSync(arg->cx->physical))
476 prompt_Printf(arg->prompt, "sync");
477 else
478 prompt_Printf(arg->prompt, "%dbps", p->cfg.speed);
479
480 switch (p->cfg.parity & CSIZE) {
481 case CS7:
482 prompt_Printf(arg->prompt, ", cs7");
483 break;
484 case CS8:
485 prompt_Printf(arg->prompt, ", cs8");
486 break;
487 }
488 if (p->cfg.parity & PARENB) {
489 if (p->cfg.parity & PARODD)
490 prompt_Printf(arg->prompt, ", odd parity");
491 else
492 prompt_Printf(arg->prompt, ", even parity");
493 } else
494 prompt_Printf(arg->prompt, ", no parity");
495
496 prompt_Printf(arg->prompt, ", CTS/RTS %s\n", (p->cfg.rts_cts ? "on" : "off"));
497
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");
504 } else {
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!)");
509 }
510 prompt_Printf(arg->prompt, "\n\n");
511
512 throughput_disp(&p->link.stats.total, arg->prompt);
513
514 return 0;
515 }
516
517 void
physical_DescriptorRead(struct fdescriptor * d,struct bundle * bundle,const fd_set * fdset __unused)518 physical_DescriptorRead(struct fdescriptor *d, struct bundle *bundle,
519 const fd_set *fdset __unused)
520 {
521 struct physical *p = descriptor2physical(d);
522 u_char *rbuff;
523 int n, found;
524
525 rbuff = p->input.buf + p->input.sz;
526
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);
531 if (n <= 0) {
532 if (n < 0)
533 log_Printf(LogPHASE, "%s: read (%d): %s\n", p->link.name, p->fd,
534 strerror(errno));
535 else
536 log_Printf(LogPHASE, "%s: read (%d): Got zero bytes\n",
537 p->link.name, p->fd);
538 datalink_Down(p->dl, CLOSE_NORMAL);
539 return;
540 }
541
542 rbuff -= p->input.sz;
543 n += p->input.sz;
544
545 if (p->link.lcp.fsm.state <= ST_CLOSED) {
546 if (p->type != PHYS_DEDICATED) {
547 found = hdlc_Detect((u_char const **)&rbuff, n, physical_IsSync(p));
548 if (rbuff != p->input.buf)
549 log_WritePrompts(p->dl, "%.*s", (int)(rbuff - p->input.buf),
550 p->input.buf);
551 p->input.sz = n - (rbuff - p->input.buf);
552
553 if (found) {
554 /* LCP packet is detected. Turn ourselves into packet mode */
555 log_Printf(LogPHASE, "%s: PPP packet detected, coming up\n",
556 p->link.name);
557 log_SetTtyCommandMode(p->dl);
558 datalink_Up(p->dl, 0, 1);
559 link_PullPacket(&p->link, rbuff, p->input.sz, bundle);
560 p->input.sz = 0;
561 } else
562 bcopy(rbuff, p->input.buf, p->input.sz);
563 } else
564 /* In -dedicated mode, we just discard input until LCP is started */
565 p->input.sz = 0;
566 } else if (n > 0)
567 link_PullPacket(&p->link, rbuff, n, bundle);
568 }
569
570 struct physical *
iov2physical(struct datalink * dl,struct iovec * iov,int * niov,int maxiov,int fd,int * auxfd,int * nauxfd)571 iov2physical(struct datalink *dl, struct iovec *iov, int *niov, int maxiov,
572 int fd, int *auxfd, int *nauxfd)
573 {
574 struct physical *p;
575 int type;
576 unsigned h;
577
578 p = (struct physical *)iov[(*niov)++].iov_base;
579 p->link.name = dl->name;
580 memset(p->link.Queue, '\0', sizeof p->link.Queue);
581
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;
587 p->dl = dl;
588 p->out = NULL;
589 p->connect_count = 1;
590
591 physical_SetDevice(p, p->name.full);
592
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);
601
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);
611
612 p->hdlc.lqm.owner = &p->link.lcp;
613 p->hdlc.ReportTimer.state = TIMER_STOPPED;
614 p->hdlc.lqm.timer.state = TIMER_STOPPED;
615
616 p->fd = fd;
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;
622
623 type = (long)p->handler;
624 p->handler = NULL;
625 for (h = 0; h < NDEVICES && p->handler == NULL; h++)
626 p->handler = (*devices[h].iov2device)(type, p, iov, niov, maxiov,
627 auxfd, nauxfd);
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);
632 } else
633 log_Printf(LogPHASE, "%s: Device %s, link type is %s\n",
634 p->link.name, p->name.full, p->handler->name);
635
636 if (p->hdlc.lqm.method && p->hdlc.lqm.timer.load)
637 lqr_reStart(&p->link.lcp);
638 hdlc_StartTimer(&p->hdlc);
639
640 throughput_restart(&p->link.stats.total, "physical throughput",
641 Enabled(dl->bundle, OPT_THROUGHPUT));
642
643 return p;
644 }
645
646 unsigned
physical_MaxDeviceSize(void)647 physical_MaxDeviceSize(void)
648 {
649 unsigned biggest, sz, n;
650
651 biggest = sizeof(struct device);
652 for (n = 0; n < NDEVICES; n++)
653 if (devices[n].DeviceSize) {
654 sz = (*devices[n].DeviceSize)();
655 if (biggest < sz)
656 biggest = sz;
657 }
658
659 return biggest;
660 }
661
662 int
physical2iov(struct physical * p,struct iovec * iov,int * niov,int maxiov,int * auxfd,int * nauxfd)663 physical2iov(struct physical *p, struct iovec *iov, int *niov, int maxiov,
664 int *auxfd, int *nauxfd)
665 {
666 struct device *h;
667 int sz;
668
669 h = NULL;
670 if (p) {
671 hdlc_StopTimer(&p->hdlc);
672 lqr_StopTimer(p);
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);
679 if (p->handler) {
680 h = p->handler;
681 p->handler = (struct device *)(long)p->handler->type;
682 }
683
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 */
687 else
688 p->session_owner = (pid_t)-1;
689 timer_Stop(&p->link.stats.total.Timer);
690 }
691
692 if (*niov + 2 >= maxiov) {
693 log_Printf(LogERROR, "physical2iov: No room for physical + throughput"
694 " + device !\n");
695 if (p)
696 free(p);
697 return -1;
698 }
699
700 iov[*niov].iov_base = (void *)p;
701 iov[*niov].iov_len = sizeof *p;
702 (*niov)++;
703
704 iov[*niov].iov_base = p ? (void *)p->link.stats.total.in.SampleOctets : NULL;
705 iov[*niov].iov_len = SAMPLE_PERIOD * sizeof(long long);
706 (*niov)++;
707 iov[*niov].iov_base = p ? (void *)p->link.stats.total.out.SampleOctets : NULL;
708 iov[*niov].iov_len = SAMPLE_PERIOD * sizeof(long long);
709 (*niov)++;
710
711 sz = physical_MaxDeviceSize();
712 if (p) {
713 if (h && h->device2iov)
714 (*h->device2iov)(h, iov, niov, maxiov, auxfd, nauxfd);
715 else {
716 if ((iov[*niov].iov_base = malloc(sz)) == NULL) {
717 log_Printf(LogALERT, "physical2iov: Out of memory (%d bytes)\n", sz);
718 AbortProgram(EX_OSERR);
719 }
720 if (h)
721 memcpy(iov[*niov].iov_base, h, sizeof *h);
722 iov[*niov].iov_len = sz;
723 (*niov)++;
724 }
725 } else {
726 iov[*niov].iov_base = NULL;
727 iov[*niov].iov_len = sz;
728 (*niov)++;
729 }
730
731 return p ? p->fd : 0;
732 }
733
734 const char *
physical_LockedDevice(struct physical * p)735 physical_LockedDevice(struct physical *p)
736 {
737 if (p->fd >= 0 && *p->name.full == '/' && p->type != PHYS_DIRECT)
738 return p->name.base;
739
740 return NULL;
741 }
742
743 void
physical_ChangedPid(struct physical * p,pid_t newpid)744 physical_ChangedPid(struct physical *p, pid_t newpid)
745 {
746 if (physical_LockedDevice(p)) {
747 int res;
748
749 if ((res = ID0uu_lock_txfr(p->name.base, newpid)) != UU_LOCK_OK)
750 log_Printf(LogPHASE, "uu_lock_txfr: %s\n", uu_lockerr(res));
751 }
752 }
753
754 int
physical_IsSync(struct physical * p)755 physical_IsSync(struct physical *p)
756 {
757 return p->cfg.speed == 0;
758 }
759
760 u_short
physical_DeviceMTU(struct physical * p)761 physical_DeviceMTU(struct physical *p)
762 {
763 return p->handler ? p->handler->mtu : 0;
764 }
765
physical_GetDevice(struct physical * p)766 const char *physical_GetDevice(struct physical *p)
767 {
768 return p->name.full;
769 }
770
771 void
physical_SetDeviceList(struct physical * p,int argc,const char * const * argv)772 physical_SetDeviceList(struct physical *p, int argc, const char *const *argv)
773 {
774 unsigned pos;
775 int f;
776
777 p->cfg.devlist[sizeof p->cfg.devlist - 1] = '\0';
778 for (f = 0, pos = 0; f < argc && pos < sizeof p->cfg.devlist - 1; f++) {
779 if (pos)
780 p->cfg.devlist[pos++] = '\0';
781 strncpy(p->cfg.devlist + pos, argv[f], sizeof p->cfg.devlist - pos - 1);
782 pos += strlen(p->cfg.devlist + pos);
783 }
784 p->cfg.ndev = f;
785 }
786
787 void
physical_SetSync(struct physical * p)788 physical_SetSync(struct physical *p)
789 {
790 p->cfg.speed = 0;
791 }
792
793 int
physical_SetRtsCts(struct physical * p,int enable)794 physical_SetRtsCts(struct physical *p, int enable)
795 {
796 p->cfg.rts_cts = enable ? 1 : 0;
797 return 1;
798 }
799
800 ssize_t
physical_Read(struct physical * p,void * buf,size_t nbytes)801 physical_Read(struct physical *p, void *buf, size_t nbytes)
802 {
803 ssize_t ret;
804
805 if (p->handler && p->handler->read)
806 ret = (*p->handler->read)(p, buf, nbytes);
807 else
808 ret = read(p->fd, buf, nbytes);
809
810 log_DumpBuff(LogPHYSICAL, "read", buf, ret);
811
812 return ret;
813 }
814
815 ssize_t
physical_Write(struct physical * p,const void * buf,size_t nbytes)816 physical_Write(struct physical *p, const void *buf, size_t nbytes)
817 {
818 log_DumpBuff(LogPHYSICAL, "write", buf, nbytes);
819
820 if (p->handler && p->handler->write)
821 return (*p->handler->write)(p, buf, nbytes);
822
823 return write(p->fd, buf, nbytes);
824 }
825
826 int
physical_doUpdateSet(struct fdescriptor * d,fd_set * r,fd_set * w,fd_set * e,int * n,int force)827 physical_doUpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e,
828 int *n, int force)
829 {
830 struct physical *p = descriptor2physical(d);
831 int sets;
832
833 sets = 0;
834 if (p->fd >= 0) {
835 if (r) {
836 FD_SET(p->fd, r);
837 log_Printf(LogTIMER, "%s: fdset(r) %d\n", p->link.name, p->fd);
838 sets++;
839 }
840 if (e) {
841 FD_SET(p->fd, e);
842 log_Printf(LogTIMER, "%s: fdset(e) %d\n", p->link.name, p->fd);
843 sets++;
844 }
845 if (w && (force || link_QueueLen(&p->link) || p->out)) {
846 FD_SET(p->fd, w);
847 log_Printf(LogTIMER, "%s: fdset(w) %d\n", p->link.name, p->fd);
848 sets++;
849 }
850 if (sets && *n < p->fd + 1)
851 *n = p->fd + 1;
852 }
853
854 return sets;
855 }
856
857 int
physical_RemoveFromSet(struct physical * p,fd_set * r,fd_set * w,fd_set * e)858 physical_RemoveFromSet(struct physical *p, fd_set *r, fd_set *w, fd_set *e)
859 {
860 if (p->handler && p->handler->removefromset)
861 return (*p->handler->removefromset)(p, r, w, e);
862 else {
863 int sets;
864
865 sets = 0;
866 if (p->fd >= 0) {
867 if (r && FD_ISSET(p->fd, r)) {
868 FD_CLR(p->fd, r);
869 log_Printf(LogTIMER, "%s: fdunset(r) %d\n", p->link.name, p->fd);
870 sets++;
871 }
872 if (e && FD_ISSET(p->fd, e)) {
873 FD_CLR(p->fd, e);
874 log_Printf(LogTIMER, "%s: fdunset(e) %d\n", p->link.name, p->fd);
875 sets++;
876 }
877 if (w && FD_ISSET(p->fd, w)) {
878 FD_CLR(p->fd, w);
879 log_Printf(LogTIMER, "%s: fdunset(w) %d\n", p->link.name, p->fd);
880 sets++;
881 }
882 }
883
884 return sets;
885 }
886 }
887
888 int
physical_IsSet(struct fdescriptor * d,const fd_set * fdset)889 physical_IsSet(struct fdescriptor *d, const fd_set *fdset)
890 {
891 struct physical *p = descriptor2physical(d);
892 return p->fd >= 0 && FD_ISSET(p->fd, fdset);
893 }
894
895 void
physical_Login(struct physical * p,const char * name)896 physical_Login(struct physical *p, const char *name)
897 {
898 if (p->type == PHYS_DIRECT && *p->name.base && !p->Utmp) {
899 struct utmpx ut;
900 const char *connstr;
901 char *colon;
902
903 memset(&ut, 0, sizeof ut);
904 ut.ut_type = USER_PROCESS;
905 gettimeofday(&ut.ut_tv, NULL);
906 snprintf(ut.ut_id, sizeof ut.ut_id, "%xppp", (int)getpid());
907 strncpy(ut.ut_user, name, sizeof ut.ut_user);
908 if (p->handler && (p->handler->type == TCP_DEVICE ||
909 p->handler->type == UDP_DEVICE)) {
910 strncpy(ut.ut_host, p->name.base, sizeof ut.ut_host);
911 colon = memchr(ut.ut_host, ':', sizeof ut.ut_host);
912 if (colon)
913 *colon = '\0';
914 } else
915 strncpy(ut.ut_line, p->name.base, sizeof ut.ut_line);
916 if ((connstr = getenv("CONNECT")))
917 /* mgetty sets this to the connection speed */
918 strncpy(ut.ut_host, connstr, sizeof ut.ut_host);
919 ID0login(&ut);
920 p->Utmp = 1;
921 }
922 }
923
924 int
physical_SetMode(struct physical * p,int mode)925 physical_SetMode(struct physical *p, int mode)
926 {
927 if ((p->type & (PHYS_DIRECT|PHYS_DEDICATED) ||
928 mode & (PHYS_DIRECT|PHYS_DEDICATED)) &&
929 (!(p->type & PHYS_DIRECT) || !(mode & PHYS_BACKGROUND))) {
930 /* Note: The -direct -> -background is for callback ! */
931 log_Printf(LogWARN, "%s: Cannot change mode %s to %s\n", p->link.name,
932 mode2Nam(p->type), mode2Nam(mode));
933 return 0;
934 }
935 p->type = mode;
936 return 1;
937 }
938
939 void
physical_DeleteQueue(struct physical * p)940 physical_DeleteQueue(struct physical *p)
941 {
942 if (p->out) {
943 m_freem(p->out);
944 p->out = NULL;
945 }
946 link_DeleteQueue(&p->link);
947 }
948
949 void
physical_SetDevice(struct physical * p,const char * name)950 physical_SetDevice(struct physical *p, const char *name)
951 {
952 int len = strlen(_PATH_DEV);
953
954 if (name != p->name.full) {
955 strncpy(p->name.full, name, sizeof p->name.full - 1);
956 p->name.full[sizeof p->name.full - 1] = '\0';
957 }
958 p->name.base = *p->name.full == '!' ? p->name.full + 1 :
959 strncmp(p->name.full, _PATH_DEV, len) ?
960 p->name.full : p->name.full + len;
961 }
962
963 static void
physical_Found(struct physical * p)964 physical_Found(struct physical *p)
965 {
966 FILE *lockfile;
967 char fn[PATH_MAX];
968
969 if (*p->name.full == '/') {
970 snprintf(fn, sizeof fn, "%s%s.if", _PATH_VARRUN, p->name.base);
971 lockfile = ID0fopen(fn, "w");
972 if (lockfile != NULL) {
973 fprintf(lockfile, "%s%d\n", TUN_NAME, p->dl->bundle->unit);
974 fclose(lockfile);
975 } else
976 log_Printf(LogALERT, "%s: Can't create %s: %s\n",
977 p->link.name, fn, strerror(errno));
978 }
979
980 throughput_start(&p->link.stats.total, "physical throughput",
981 Enabled(p->dl->bundle, OPT_THROUGHPUT));
982 p->connect_count++;
983 p->input.sz = 0;
984
985 log_Printf(LogPHASE, "%s: Connected!\n", p->link.name);
986 }
987
988 int
physical_Open(struct physical * p)989 physical_Open(struct physical *p)
990 {
991 char *dev;
992 int devno, wasfd, err;
993 unsigned h;
994
995 if (p->fd >= 0)
996 log_Printf(LogDEBUG, "%s: Open: Modem is already open!\n", p->link.name);
997 /* We're going back into "term" mode */
998 else if (p->type == PHYS_DIRECT) {
999 physical_SetDevice(p, "");
1000 p->fd = STDIN_FILENO;
1001 for (h = 0; h < NDEVICES && p->handler == NULL && p->fd >= 0; h++)
1002 p->handler = (*devices[h].create)(p);
1003 close(STDOUT_FILENO);
1004 if (p->fd >= 0) {
1005 if (p->handler == NULL) {
1006 physical_SetupStack(p, "unknown", PHYSICAL_NOFORCE);
1007 log_Printf(LogDEBUG, "%s: stdin is unidentified\n", p->link.name);
1008 }
1009 physical_Found(p);
1010 }
1011 } else {
1012 dev = p->cfg.devlist;
1013 devno = 0;
1014 while (devno < p->cfg.ndev && p->fd < 0) {
1015 physical_SetDevice(p, dev);
1016 if (physical_Lock(p)) {
1017 err = 0;
1018
1019 if (*p->name.full == '/') {
1020 p->fd = ID0open(p->name.full, O_RDWR | O_NONBLOCK);
1021 if (p->fd < 0)
1022 err = errno;
1023 }
1024
1025 wasfd = p->fd;
1026 for (h = 0; h < NDEVICES && p->handler == NULL; h++)
1027 if ((p->handler = (*devices[h].create)(p)) == NULL && wasfd != p->fd)
1028 break;
1029
1030 if (p->fd < 0) {
1031 if (h == NDEVICES) {
1032 if (err)
1033 log_Printf(LogWARN, "%s: %s: %s\n", p->link.name, p->name.full,
1034 strerror(errno));
1035 else
1036 log_Printf(LogWARN, "%s: Device (%s) must begin with a '/',"
1037 " a '!' or contain at least one ':'\n", p->link.name,
1038 p->name.full);
1039 }
1040 physical_Unlock(p);
1041 } else
1042 physical_Found(p);
1043 }
1044 dev += strlen(dev) + 1;
1045 devno++;
1046 }
1047 }
1048
1049 return p->fd;
1050 }
1051
1052 void
physical_SetupStack(struct physical * p,const char * who,int how)1053 physical_SetupStack(struct physical *p, const char *who, int how)
1054 {
1055 link_EmptyStack(&p->link);
1056 if (how == PHYSICAL_FORCE_SYNC || how == PHYSICAL_FORCE_SYNCNOACF ||
1057 (how == PHYSICAL_NOFORCE && physical_IsSync(p)))
1058 link_Stack(&p->link, &synclayer);
1059 else {
1060 link_Stack(&p->link, &asynclayer);
1061 link_Stack(&p->link, &hdlclayer);
1062 }
1063 if (how != PHYSICAL_FORCE_SYNCNOACF)
1064 link_Stack(&p->link, &acflayer);
1065 link_Stack(&p->link, &protolayer);
1066 link_Stack(&p->link, &lqrlayer);
1067 link_Stack(&p->link, &ccplayer);
1068 link_Stack(&p->link, &vjlayer);
1069 link_Stack(&p->link, &tcpmsslayer);
1070 #ifndef NONAT
1071 link_Stack(&p->link, &natlayer);
1072 #endif
1073 if (how == PHYSICAL_FORCE_ASYNC && physical_IsSync(p)) {
1074 log_Printf(LogWARN, "Sync device setting ignored for ``%s'' device\n", who);
1075 p->cfg.speed = MODEM_SPEED;
1076 } else if (how == PHYSICAL_FORCE_SYNC && !physical_IsSync(p)) {
1077 log_Printf(LogWARN, "Async device setting ignored for ``%s'' device\n",
1078 who);
1079 physical_SetSync(p);
1080 }
1081 }
1082
1083 void
physical_StopDeviceTimer(struct physical * p)1084 physical_StopDeviceTimer(struct physical *p)
1085 {
1086 if (p->handler && p->handler->stoptimer)
1087 (*p->handler->stoptimer)(p);
1088 }
1089
1090 int
physical_AwaitCarrier(struct physical * p)1091 physical_AwaitCarrier(struct physical *p)
1092 {
1093 if (p->handler && p->handler->awaitcarrier)
1094 return (*p->handler->awaitcarrier)(p);
1095
1096 return CARRIER_OK;
1097 }
1098
1099
1100 void
physical_SetAsyncParams(struct physical * p,u_int32_t mymap,u_int32_t hismap)1101 physical_SetAsyncParams(struct physical *p, u_int32_t mymap, u_int32_t hismap)
1102 {
1103 if (p->handler && p->handler->setasyncparams)
1104 return (*p->handler->setasyncparams)(p, mymap, hismap);
1105
1106 async_SetLinkParams(&p->async, mymap, hismap);
1107 }
1108
1109 int
physical_Slot(struct physical * p)1110 physical_Slot(struct physical *p)
1111 {
1112 if (p->handler && p->handler->slot)
1113 return (*p->handler->slot)(p);
1114
1115 return -1;
1116 }
1117
1118 int
physical_SetPPPoEnonstandard(struct physical * p,int enable)1119 physical_SetPPPoEnonstandard(struct physical *p, int enable)
1120 {
1121 p->cfg.nonstandard_pppoe = enable ? 1 : 0;
1122 p->cfg.pppoe_configured = 1;
1123 return 1;
1124 }
1125