]> git.ozlabs.org Git - ppp.git/blob - pppd/sys-sunos4.c
allow for COPTS from Makefile.top
[ppp.git] / pppd / sys-sunos4.c
1 /*
2  * System-dependent procedures for pppd under SunOS 4.
3  *
4  * Copyright (c) 1994 The Australian National University.
5  * All rights reserved.
6  *
7  * Permission to use, copy, modify, and distribute this software and its
8  * documentation is hereby granted, provided that the above copyright
9  * notice appears in all copies.  This software is provided without any
10  * warranty, express or implied. The Australian National University
11  * makes no representations about the suitability of this software for
12  * any purpose.
13  *
14  * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
15  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
17  * THE AUSTRALIAN NATIONAL UNIVERSITY HAVE BEEN ADVISED OF THE POSSIBILITY
18  * OF SUCH DAMAGE.
19  *
20  * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
21  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
23  * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
24  * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
25  * OR MODIFICATIONS.
26  */
27
28 #ifndef lint
29 static char rcsid[] = "$Id: sys-sunos4.c,v 1.5 1996/05/27 00:00:56 paulus Exp $";
30 #endif
31
32 #include <stdio.h>
33 #include <stddef.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <unistd.h>
40 #include <termios.h>
41 #include <signal.h>
42 #include <malloc.h>
43 #include <utmp.h>
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/socket.h>
47 #include <sys/sockio.h>
48 #include <sys/stream.h>
49 #include <sys/stropts.h>
50 #include <sys/syslog.h>
51 #include <sys/stat.h>
52 #include <sys/time.h>
53 #include <sys/poll.h>
54 #include <net/if.h>
55 #include <net/if_arp.h>
56 #include <net/nit_if.h>
57 #include <net/route.h>
58 #include <net/ppp_defs.h>
59 #include <net/pppio.h>
60 #include <netinet/in.h>
61
62 #include "pppd.h"
63
64 #if defined(sun) && defined(sparc)
65 #include <alloca.h>
66 #endif /*sparc*/
67
68 static int      pppfd;
69 static int      fdmuxid = -1;
70 static int      iffd;
71 static int      sockfd;
72
73 static int      restore_term;
74 static struct termios inittermios;
75 static struct winsize wsinfo;   /* Initial window size info */
76 static pid_t    parent_pid;     /* PID of our parent */
77
78 extern u_char   inpacket_buf[]; /* borrowed from main.c */
79
80 static int      link_mtu, link_mru;
81
82 #define NMODULES        32
83 static int      tty_nmodules;
84 static char     tty_modules[NMODULES][FMNAMESZ+1];
85
86 static int      if_is_up;       /* Interface has been marked up */
87 static u_int32_t ifaddrs[2];    /* local and remote addresses */
88 static u_int32_t default_route_gateway; /* Gateway for default route added */
89 static u_int32_t proxy_arp_addr;        /* Addr for proxy arp entry added */
90
91 /* Prototypes for procedures local to this file. */
92 static int translate_speed __P((int));
93 static int baud_rate_of __P((int));
94 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
95 static int strioctl __P((int, int, void *, int, int));
96
97
98 /*
99  * sys_init - System-dependent initialization.
100  */
101 void
102 sys_init()
103 {
104     int x;
105
106     /* Get an internet socket for doing socket ioctl's on. */
107     if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
108         syslog(LOG_ERR, "Couldn't create IP socket: %m");
109         die(1);
110     }
111
112     /*
113      * We may want to send a SIGHUP to the session leader associated
114      * with our controlling terminal later.  Because SunOS doesn't
115      * have getsid(), we make do with sending the signal to our
116      * parent process.
117      */
118     parent_pid = getppid();
119
120     /*
121      * Open the ppp device.
122      */
123     pppfd = open("/dev/ppp", O_RDWR | O_NONBLOCK, 0);
124     if (pppfd < 0) {
125         syslog(LOG_ERR, "Can't open /dev/ppp: %m");
126         die(1);
127     }
128     if (kdebugflag) {
129         x = PPPDBG_LOG + PPPDBG_DRIVER;
130         strioctl(pppfd, PPPIO_DEBUG, &x, sizeof(int), 0);
131     }
132
133     /* Assign a new PPA and get its unit number. */
134     if (strioctl(pppfd, PPPIO_NEWPPA, &ifunit, 0, sizeof(int)) < 0) {
135         syslog(LOG_ERR, "Can't create new PPP interface: %m");
136         die(1);
137     }
138
139     /*
140      * Open the ppp device again and push the if_ppp module on it.
141      */
142     iffd = open("/dev/ppp", O_RDWR, 0);
143     if (iffd < 0) {
144         syslog(LOG_ERR, "Can't open /dev/ppp (2): %m");
145         die(1);
146     }
147     if (kdebugflag) {
148         x = PPPDBG_LOG + PPPDBG_DRIVER;
149         strioctl(iffd, PPPIO_DEBUG, &x, sizeof(int), 0);
150     }
151     if (strioctl(iffd, PPPIO_ATTACH, &ifunit, sizeof(int), 0) < 0) {
152         syslog(LOG_ERR, "Couldn't attach ppp interface to device: %m");
153         die(1);
154     }
155     if (ioctl(iffd, I_PUSH, "if_ppp") < 0) {
156         syslog(LOG_ERR, "Can't push ppp interface module: %m");
157         die(1);
158     }
159     if (kdebugflag) {
160         x = PPPDBG_LOG + PPPDBG_IF;
161         strioctl(iffd, PPPIO_DEBUG, &x, sizeof(int), 0);
162     }
163     if (strioctl(iffd, PPPIO_NEWPPA, &ifunit, sizeof(int), 0) < 0) {
164         syslog(LOG_ERR, "Couldn't create ppp interface unit: %m");
165         die(1);
166     }
167     x = PPP_IP;
168     if (strioctl(iffd, PPPIO_BIND, &x, sizeof(int), 0) < 0) {
169         syslog(LOG_ERR, "Couldn't bind ppp interface to IP SAP: %m");
170         die(1);
171     }
172 }
173
174 /*
175  * sys_cleanup - restore any system state we modified before exiting:
176  * mark the interface down, delete default route and/or proxy arp entry.
177  * This shouldn't call die() because it's called from die().
178  */
179 void
180 sys_cleanup()
181 {
182     if (if_is_up)
183         sifdown(0);
184     if (ifaddrs[0])
185         cifaddr(0, ifaddrs[0], ifaddrs[1]);
186     if (default_route_gateway)
187         cifdefaultroute(0, default_route_gateway);
188     if (proxy_arp_addr)
189         cifproxyarp(0, proxy_arp_addr);
190 }
191
192 /*
193  * sys_close - Clean up in a child process before execing.
194  */
195 void
196 sys_close()
197 {
198     close(iffd);
199     close(pppfd);
200     close(sockfd);
201 }
202
203 /*
204  * sys_check_options - check the options that the user specified
205  */
206 void
207 sys_check_options()
208 {
209 }
210
211
212 /*
213  * daemon - Detach us from controlling terminal session.
214  */
215 int
216 daemon(nochdir, noclose)
217     int nochdir, noclose;
218 {
219     int pid;
220
221     if ((pid = fork()) < 0)
222         return -1;
223     if (pid != 0)
224         exit(0);                /* parent dies */
225     setsid();
226     if (!nochdir)
227         chdir("/");
228     if (!noclose) {
229         fclose(stdin);          /* don't need stdin, stdout, stderr */
230         fclose(stdout);
231         fclose(stderr);
232     }
233     return 0;
234 }
235
236 /*
237  * ppp_available - check whether the system has any ppp interfaces
238  */
239 int
240 ppp_available()
241 {
242     struct stat buf;
243
244     return stat("/dev/ppp", &buf) >= 0;
245 }
246
247 /*
248  * establish_ppp - Turn the serial port into a ppp interface.
249  */
250 void
251 establish_ppp(fd)
252     int fd;
253 {
254     int i;
255
256     /* Pop any existing modules off the tty stream. */
257     for (i = 0;; ++i)
258         if (ioctl(fd, I_LOOK, tty_modules[i]) < 0
259             || ioctl(fd, I_POP, 0) < 0)
260             break;
261     tty_nmodules = i;
262
263     /* Push the async hdlc module and the compressor module. */
264     if (ioctl(fd, I_PUSH, "ppp_ahdl") < 0) {
265         syslog(LOG_ERR, "Couldn't push PPP Async HDLC module: %m");
266         die(1);
267     }
268     if (ioctl(fd, I_PUSH, "ppp_comp") < 0) {
269         syslog(LOG_ERR, "Couldn't push PPP compression module: %m");
270 /*      die(1); */
271     }
272
273     /* Link the serial port under the PPP multiplexor. */
274     if ((fdmuxid = ioctl(pppfd, I_LINK, fd)) < 0) {
275         syslog(LOG_ERR, "Can't link tty to PPP mux: %m");
276         die(1);
277     }
278 }
279
280 /*
281  * restore_loop - reattach the ppp unit to the loopback.
282  * This doesn't need to do anything because disestablish_ppp does it.
283  */
284 void
285 restore_loop()
286 {
287 }
288
289 /*
290  * disestablish_ppp - Restore the serial port to normal operation.
291  * It attempts to reconstruct the stream with the previously popped
292  * modules.  This shouldn't call die() because it's called from die().
293  */
294 void
295 disestablish_ppp(fd)
296     int fd;
297 {
298     int i;
299
300     if (fdmuxid >= 0) {
301         if (ioctl(pppfd, I_UNLINK, fdmuxid) < 0) {
302             if (!hungup)
303                 syslog(LOG_ERR, "Can't unlink tty from PPP mux: %m");
304         }
305         fdmuxid = -1;
306
307         if (!hungup) {
308             while (ioctl(fd, I_POP, 0) >= 0)
309                 ;
310             for (i = tty_nmodules - 1; i >= 0; --i)
311                 if (ioctl(fd, I_PUSH, tty_modules[i]) < 0)
312                     syslog(LOG_ERR, "Couldn't restore tty module %s: %m",
313                            tty_modules[i]);
314         }
315         if (hungup && default_device && parent_pid > 0) {
316             /*
317              * If we have received a hangup, we need to send a SIGHUP
318              * to the terminal's controlling process.  The reason is
319              * that the original stream head for the terminal hasn't
320              * seen the M_HANGUP message (it went up through the ppp
321              * driver to the stream head for our fd to /dev/ppp).
322              * Actually we send the signal to the process that invoked
323              * pppd, since SunOS doesn't have getsid().
324              */
325             kill(parent_pid, SIGHUP);
326         }
327     }
328 }
329
330 /*
331  * Check whether the link seems not to be 8-bit clean.
332  */
333 void
334 clean_check()
335 {
336     int x;
337     char *s;
338
339     if (strioctl(pppfd, PPPIO_GCLEAN, &x, 0, sizeof(x)) < 0)
340         return;
341     s = NULL;
342     switch (~x) {
343     case RCV_B7_0:
344         s = "bit 7 set to 1";
345         break;
346     case RCV_B7_1:
347         s = "bit 7 set to 0";
348         break;
349     case RCV_EVNP:
350         s = "odd parity";
351         break;
352     case RCV_ODDP:
353         s = "even parity";
354         break;
355     }
356     if (s != NULL) {
357         syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
358         syslog(LOG_WARNING, "All received characters had %s", s);
359     }
360 }
361
362 /*
363  * List of valid speeds.
364  */
365 struct speed {
366     int speed_int, speed_val;
367 } speeds[] = {
368 #ifdef B50
369     { 50, B50 },
370 #endif
371 #ifdef B75
372     { 75, B75 },
373 #endif
374 #ifdef B110
375     { 110, B110 },
376 #endif
377 #ifdef B134
378     { 134, B134 },
379 #endif
380 #ifdef B150
381     { 150, B150 },
382 #endif
383 #ifdef B200
384     { 200, B200 },
385 #endif
386 #ifdef B300
387     { 300, B300 },
388 #endif
389 #ifdef B600
390     { 600, B600 },
391 #endif
392 #ifdef B1200
393     { 1200, B1200 },
394 #endif
395 #ifdef B1800
396     { 1800, B1800 },
397 #endif
398 #ifdef B2000
399     { 2000, B2000 },
400 #endif
401 #ifdef B2400
402     { 2400, B2400 },
403 #endif
404 #ifdef B3600
405     { 3600, B3600 },
406 #endif
407 #ifdef B4800
408     { 4800, B4800 },
409 #endif
410 #ifdef B7200
411     { 7200, B7200 },
412 #endif
413 #ifdef B9600
414     { 9600, B9600 },
415 #endif
416 #ifdef B19200
417     { 19200, B19200 },
418 #endif
419 #ifdef B38400
420     { 38400, B38400 },
421 #endif
422 #ifdef EXTA
423     { 19200, EXTA },
424 #endif
425 #ifdef EXTB
426     { 38400, EXTB },
427 #endif
428 #ifdef B57600
429     { 57600, B57600 },
430 #endif
431 #ifdef B115200
432     { 115200, B115200 },
433 #endif
434     { 0, 0 }
435 };
436
437 /*
438  * Translate from bits/second to a speed_t.
439  */
440 static int
441 translate_speed(bps)
442     int bps;
443 {
444     struct speed *speedp;
445
446     if (bps == 0)
447         return 0;
448     for (speedp = speeds; speedp->speed_int; speedp++)
449         if (bps == speedp->speed_int)
450             return speedp->speed_val;
451     syslog(LOG_WARNING, "speed %d not supported", bps);
452     return 0;
453 }
454
455 /*
456  * Translate from a speed_t to bits/second.
457  */
458 static int
459 baud_rate_of(speed)
460     int speed;
461 {
462     struct speed *speedp;
463
464     if (speed == 0)
465         return 0;
466     for (speedp = speeds; speedp->speed_int; speedp++)
467         if (speed == speedp->speed_val)
468             return speedp->speed_int;
469     return 0;
470 }
471
472 /*
473  * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
474  * at the requested speed, etc.  If `local' is true, set CLOCAL
475  * regardless of whether the modem option was specified.
476  */
477 void
478 set_up_tty(fd, local)
479     int fd, local;
480 {
481     int speed;
482     struct termios tios;
483
484     if (tcgetattr(fd, &tios) < 0) {
485         syslog(LOG_ERR, "tcgetattr: %m");
486         die(1);
487     }
488
489     if (!restore_term) {
490         inittermios = tios;
491         ioctl(fd, TIOCGWINSZ, &wsinfo);
492     }
493
494     tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
495     if (crtscts > 0)
496         tios.c_cflag |= CRTSCTS;
497     else if (crtscts < 0)
498         tios.c_cflag &= ~CRTSCTS;
499
500     tios.c_cflag |= CS8 | CREAD | HUPCL;
501     if (local || !modem)
502         tios.c_cflag |= CLOCAL;
503     tios.c_iflag = IGNBRK | IGNPAR;
504     tios.c_oflag = 0;
505     tios.c_lflag = 0;
506     tios.c_cc[VMIN] = 1;
507     tios.c_cc[VTIME] = 0;
508
509     if (crtscts == -2) {
510         tios.c_iflag |= IXON | IXOFF;
511         tios.c_cc[VSTOP] = 0x13;        /* DC3 = XOFF = ^S */
512         tios.c_cc[VSTART] = 0x11;       /* DC1 = XON  = ^Q */
513     }
514
515     speed = translate_speed(inspeed);
516     if (speed) {
517         cfsetospeed(&tios, speed);
518         cfsetispeed(&tios, speed);
519     } else {
520         speed = cfgetospeed(&tios);
521         /*
522          * We can't proceed if the serial port speed is 0,
523          * since that implies that the serial port is disabled.
524          */
525         if (speed == B0) {
526             syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
527                    devnam);
528             die(1);
529         }
530     }
531
532     if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
533         syslog(LOG_ERR, "tcsetattr: %m");
534         die(1);
535     }
536
537     baud_rate = inspeed = baud_rate_of(speed);
538     restore_term = 1;
539 }
540
541 /*
542  * restore_tty - restore the terminal to the saved settings.
543  */
544 void
545 restore_tty(fd)
546     int fd;
547 {
548     if (restore_term) {
549         if (!default_device) {
550             /*
551              * Turn off echoing, because otherwise we can get into
552              * a loop with the tty and the modem echoing to each other.
553              * We presume we are the sole user of this tty device, so
554              * when we close it, it will revert to its defaults anyway.
555              */
556             inittermios.c_lflag &= ~(ECHO | ECHONL);
557         }
558         if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
559             if (!hungup && errno != ENXIO)
560                 syslog(LOG_WARNING, "tcsetattr: %m");
561         ioctl(fd, TIOCSWINSZ, &wsinfo);
562         restore_term = 0;
563     }
564 }
565
566 /*
567  * setdtr - control the DTR line on the serial port.
568  * This is called from die(), so it shouldn't call die().
569  */
570 void
571 setdtr(fd, on)
572 int fd, on;
573 {
574     int modembits = TIOCM_DTR;
575
576     ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
577 }
578
579 /*
580  * open_loopback - open the device we use for getting packets
581  * in demand mode.  Under Solaris 2, we use our existing fd
582  * to the ppp driver.
583  */
584 void
585 open_ppp_loopback()
586 {
587 }
588
589 /*
590  * output - Output PPP packet.
591  */
592 void
593 output(unit, p, len)
594     int unit;
595     u_char *p;
596     int len;
597 {
598     struct strbuf data;
599     int retries;
600     struct pollfd pfd;
601
602     if (debug)
603         log_packet(p, len, "sent ");
604
605     data.len = len;
606     data.buf = (caddr_t) p;
607     retries = 4;
608     while (putmsg(pppfd, NULL, &data, 0) < 0) {
609         if (--retries < 0 || (errno != EWOULDBLOCK && errno != EAGAIN)) {
610             if (errno != ENXIO)
611                 syslog(LOG_ERR, "Couldn't send packet: %m");
612             break;
613         }
614         pfd.fd = pppfd;
615         pfd.events = POLLOUT;
616         poll(&pfd, 1, 250);     /* wait for up to 0.25 seconds */
617     }
618 }
619
620
621 /*
622  * wait_input - wait until there is data available on fd,
623  * for the length of time specified by *timo (indefinite
624  * if timo is NULL).
625  */
626 void
627 wait_input(timo)
628     struct timeval *timo;
629 {
630     int t;
631     struct pollfd pfd;
632
633     t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
634     pfd.fd = pppfd;
635     pfd.events = POLLIN | POLLPRI | POLLHUP;
636     if (poll(&pfd, 1, t) < 0 && errno != EINTR) {
637         syslog(LOG_ERR, "poll: %m");
638         die(1);
639     }
640 }
641
642 /*
643  * wait_loop_output - wait until there is data available on the
644  * loopback, for the length of time specified by *timo (indefinite
645  * if timo is NULL).
646  */
647 void
648 wait_loop_output(timo)
649     struct timeval *timo;
650 {
651     wait_input(timo);
652 }
653
654 /*
655  * wait_time - wait for a given length of time or until a
656  * signal is received.
657  */
658 void
659 wait_time(timo)
660     struct timeval *timo;
661 {
662     int n;
663
664     n = select(0, NULL, NULL, NULL, timo);
665     if (n < 0 && errno != EINTR) {
666         syslog(LOG_ERR, "select: %m");
667         die(1);
668     }
669 }
670
671
672 /*
673  * read_packet - get a PPP packet from the serial device.
674  */
675 int
676 read_packet(buf)
677     u_char *buf;
678 {
679     struct strbuf ctrl, data;
680     int flags, len;
681     unsigned char ctrlbuf[64];
682
683     for (;;) {
684         data.maxlen = PPP_MRU + PPP_HDRLEN;
685         data.buf = (caddr_t) buf;
686         ctrl.maxlen = sizeof(ctrlbuf);
687         ctrl.buf = (caddr_t) ctrlbuf;
688         flags = 0;
689         len = getmsg(pppfd, &ctrl, &data, &flags);
690         if (len < 0) {
691             if (errno = EAGAIN || errno == EINTR)
692                 return -1;
693             syslog(LOG_ERR, "Error reading packet: %m");
694             die(1);
695         }
696
697         if (ctrl.len <= 0)
698             return data.len;
699
700         /*
701          * Got a M_PROTO or M_PCPROTO message.  Huh?
702          */
703         if (debug)
704             syslog(LOG_DEBUG, "got ctrl msg len=%d", ctrl.len);
705
706     }
707 }
708
709 /*
710  * get_loop_output - get outgoing packets from the ppp device,
711  * and detect when we want to bring the real link up.
712  * Return value is 1 if we need to bring up the link, 0 otherwise.
713  */
714 int
715 get_loop_output()
716 {
717     int len;
718     int rv = 0;
719
720     while ((len = read_packet(inpacket_buf)) > 0) {
721         if (loop_frame(inpacket_buf, len))
722             rv = 1;
723     }
724     return rv;
725 }
726
727 /*
728  * ppp_send_config - configure the transmit characteristics of
729  * the ppp interface.
730  */
731 void
732 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
733     int unit, mtu;
734     u_int32_t asyncmap;
735     int pcomp, accomp;
736 {
737     int cf[2];
738
739     link_mtu = mtu;
740     if (strioctl(pppfd, PPPIO_MTU, &mtu, sizeof(mtu), 0) < 0) {
741         if (hungup && errno == ENXIO)
742             return;
743         syslog(LOG_ERR, "Couldn't set MTU: %m");
744     }
745     if (strioctl(pppfd, PPPIO_XACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
746         syslog(LOG_ERR, "Couldn't set transmit ACCM: %m");
747     }
748     cf[0] = (pcomp? COMP_PROT: 0) + (accomp? COMP_AC: 0);
749     cf[1] = COMP_PROT | COMP_AC;
750     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
751         syslog(LOG_ERR, "Couldn't set prot/AC compression: %m");
752     }
753 }
754
755 /*
756  * ppp_set_xaccm - set the extended transmit ACCM for the interface.
757  */
758 void
759 ppp_set_xaccm(unit, accm)
760     int unit;
761     ext_accm accm;
762 {
763     if (strioctl(pppfd, PPPIO_XACCM, accm, sizeof(ext_accm), 0) < 0) {
764         if (!hungup || errno != ENXIO)
765             syslog(LOG_WARNING, "Couldn't set extended ACCM: %m");
766     }
767 }
768
769 /*
770  * ppp_recv_config - configure the receive-side characteristics of
771  * the ppp interface.
772  */
773 void
774 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
775     int unit, mru;
776     u_int32_t asyncmap;
777     int pcomp, accomp;
778 {
779     int cf[2];
780
781     link_mru = mru;
782     if (strioctl(pppfd, PPPIO_MRU, &mru, sizeof(mru), 0) < 0) {
783         if (hungup && errno == ENXIO)
784             return;
785         syslog(LOG_ERR, "Couldn't set MRU: %m");
786     }
787     if (strioctl(pppfd, PPPIO_RACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
788         syslog(LOG_ERR, "Couldn't set receive ACCM: %m");
789     }
790     cf[0] = (pcomp? DECOMP_PROT: 0) + (accomp? DECOMP_AC: 0);
791     cf[1] = DECOMP_PROT | DECOMP_AC;
792     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
793         syslog(LOG_ERR, "Couldn't set prot/AC decompression: %m");
794     }
795 }
796
797 /*
798  * ccp_test - ask kernel whether a given compression method
799  * is acceptable for use.
800  */
801 int
802 ccp_test(unit, opt_ptr, opt_len, for_transmit)
803     int unit, opt_len, for_transmit;
804     u_char *opt_ptr;
805 {
806     if (strioctl(pppfd, (for_transmit? PPPIO_XCOMP: PPPIO_RCOMP),
807                  opt_ptr, opt_len, 0) >= 0)
808         return 1;
809     return (errno == ENOSR)? 0: -1;
810 }
811
812 /*
813  * ccp_flags_set - inform kernel about the current state of CCP.
814  */
815 void
816 ccp_flags_set(unit, isopen, isup)
817     int unit, isopen, isup;
818 {
819     int cf[2];
820
821     cf[0] = (isopen? CCP_ISOPEN: 0) + (isup? CCP_ISUP: 0);
822     cf[1] = CCP_ISOPEN | CCP_ISUP | CCP_ERROR | CCP_FATALERROR;
823     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
824         if (!hungup || errno != ENXIO)
825             syslog(LOG_ERR, "Couldn't set kernel CCP state: %m");
826     }
827 }
828
829 /*
830  * get_idle_time - return how long the link has been idle.
831  */
832 int
833 get_idle_time(u, ip)
834     int u;
835     struct ppp_idle *ip;
836 {
837     return strioctl(pppfd, PPPIO_GIDLE, ip, 0, sizeof(struct ppp_idle)) >= 0;
838 }
839
840
841 /*
842  * ccp_fatal_error - returns 1 if decompression was disabled as a
843  * result of an error detected after decompression of a packet,
844  * 0 otherwise.  This is necessary because of patent nonsense.
845  */
846 int
847 ccp_fatal_error(unit)
848     int unit;
849 {
850     int cf[2];
851
852     cf[0] = cf[1] = 0;
853     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
854         if (errno != ENXIO && errno != EINVAL)
855             syslog(LOG_ERR, "Couldn't get compression flags: %m");
856         return 0;
857     }
858     return cf[0] & CCP_FATALERROR;
859 }
860
861 /*
862  * sifvjcomp - config tcp header compression
863  */
864 int
865 sifvjcomp(u, vjcomp, xcidcomp, xmaxcid)
866     int u, vjcomp, xcidcomp, xmaxcid;
867 {
868     int cf[2];
869     char maxcid[2];
870
871     if (vjcomp) {
872         maxcid[0] = xcidcomp;
873         maxcid[1] = 15;         /* XXX should be rmaxcid */
874         if (strioctl(pppfd, PPPIO_VJINIT, maxcid, sizeof(maxcid), 0) < 0) {
875             syslog(LOG_ERR, "Couldn't initialize VJ compression: %m");
876         }
877     }
878
879     cf[0] = (vjcomp? COMP_VJC + DECOMP_VJC: 0)  /* XXX this is wrong */
880         + (xcidcomp? COMP_VJCCID + DECOMP_VJCCID: 0);
881     cf[1] = COMP_VJC + DECOMP_VJC + COMP_VJCCID + DECOMP_VJCCID;
882     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
883         if (vjcomp)
884             syslog(LOG_ERR, "Couldn't enable VJ compression: %m");
885     }
886
887     return 1;
888 }
889
890 /*
891  * sifup - Config the interface up and enable IP packets to pass.
892  */
893 int
894 sifup(u)
895     int u;
896 {
897     struct ifreq ifr;
898
899     strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
900     if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
901         syslog(LOG_ERR, "Couldn't mark interface up (get): %m");
902         return 0;
903     }
904     ifr.ifr_flags |= IFF_UP;
905     if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
906         syslog(LOG_ERR, "Couldn't mark interface up (set): %m");
907         return 0;
908     }
909     if_is_up = 1;
910     return 1;
911 }
912
913 /*
914  * sifdown - Config the interface down and disable IP.
915  */
916 int
917 sifdown(u)
918     int u;
919 {
920     struct ifreq ifr;
921
922     strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
923     if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
924         syslog(LOG_ERR, "Couldn't mark interface down (get): %m");
925         return 0;
926     }
927     if ((ifr.ifr_flags & IFF_UP) != 0) {
928         ifr.ifr_flags &= ~IFF_UP;
929         if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
930             syslog(LOG_ERR, "Couldn't mark interface down (set): %m");
931             return 0;
932         }
933     }
934     if_is_up = 0;
935     return 1;
936 }
937
938 /*
939  * sifnpmode - Set the mode for handling packets for a given NP.
940  */
941 int
942 sifnpmode(u, proto, mode)
943     int u;
944     int proto;
945     enum NPmode mode;
946 {
947     int npi[2];
948
949     npi[0] = proto;
950     npi[1] = (int) mode;
951     if (strioctl(pppfd, PPPIO_NPMODE, &npi, 2 * sizeof(int), 0) < 0) {
952         syslog(LOG_ERR, "ioctl(set NP %d mode to %d): %m", proto, mode);
953         return 0;
954     }
955     return 1;
956 }
957
958 #define INET_ADDR(x)    (((struct sockaddr_in *) &(x))->sin_addr.s_addr)
959
960 /*
961  * sifaddr - Config the interface IP addresses and netmask.
962  */
963 int
964 sifaddr(u, o, h, m)
965     int u;
966     u_int32_t o, h, m;
967 {
968     struct ifreq ifr;
969
970     memset(&ifr, 0, sizeof(ifr));
971     strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
972     ifr.ifr_addr.sa_family = AF_INET;
973     INET_ADDR(ifr.ifr_addr) = m;
974     if (ioctl(sockfd, SIOCSIFNETMASK, &ifr) < 0) {
975         syslog(LOG_ERR, "Couldn't set IP netmask: %m");
976     }
977     ifr.ifr_addr.sa_family = AF_INET;
978     INET_ADDR(ifr.ifr_addr) = o;
979     if (ioctl(sockfd, SIOCSIFADDR, &ifr) < 0) {
980         syslog(LOG_ERR, "Couldn't set local IP address: %m");
981     }
982     ifr.ifr_dstaddr.sa_family = AF_INET;
983     INET_ADDR(ifr.ifr_dstaddr) = h;
984     if (ioctl(sockfd, SIOCSIFDSTADDR, &ifr) < 0) {
985         syslog(LOG_ERR, "Couldn't set remote IP address: %m");
986     }
987     ifr.ifr_metric = link_mtu;
988     if (ioctl(sockfd, SIOCSIFMTU, &ifr) < 0) {
989         syslog(LOG_ERR, "Couldn't set IP MTU: %m");
990     }
991     ifaddrs[0] = o;
992     ifaddrs[1] = h;
993
994     return 1;
995 }
996
997 /*
998  * cifaddr - Clear the interface IP addresses, and delete routes
999  * through the interface if possible.
1000  */
1001 int
1002 cifaddr(u, o, h)
1003     int u;
1004     u_int32_t o, h;
1005 {
1006     struct rtentry rt;
1007
1008     bzero(&rt, sizeof(rt));
1009     rt.rt_dst.sa_family = AF_INET;
1010     INET_ADDR(rt.rt_dst) = h;
1011     rt.rt_gateway.sa_family = AF_INET;
1012     INET_ADDR(rt.rt_gateway) = o;
1013     rt.rt_flags = RTF_HOST;
1014     if (ioctl(sockfd, SIOCDELRT, &rt) < 0)
1015         syslog(LOG_ERR, "Couldn't delete route through interface: %m");
1016     ifaddrs[0] = 0;
1017     return 1;
1018 }
1019
1020 /*
1021  * sifdefaultroute - assign a default route through the address given.
1022  */
1023 int
1024 sifdefaultroute(u, g)
1025     int u;
1026     u_int32_t g;
1027 {
1028     struct rtentry rt;
1029
1030     bzero(&rt, sizeof(rt));
1031     rt.rt_dst.sa_family = AF_INET;
1032     INET_ADDR(rt.rt_dst) = 0;
1033     rt.rt_gateway.sa_family = AF_INET;
1034     INET_ADDR(rt.rt_gateway) = g;
1035     rt.rt_flags = RTF_GATEWAY;
1036
1037     if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
1038         syslog(LOG_ERR, "Can't add default route: %m");
1039         return 0;
1040     }
1041
1042     default_route_gateway = g;
1043     return 1;
1044 }
1045
1046 /*
1047  * cifdefaultroute - delete a default route through the address given.
1048  */
1049 int
1050 cifdefaultroute(u, g)
1051     int u;
1052     u_int32_t g;
1053 {
1054     struct rtentry rt;
1055
1056     bzero(&rt, sizeof(rt));
1057     rt.rt_dst.sa_family = AF_INET;
1058     INET_ADDR(rt.rt_dst) = 0;
1059     rt.rt_gateway.sa_family = AF_INET;
1060     INET_ADDR(rt.rt_gateway) = g;
1061     rt.rt_flags = RTF_GATEWAY;
1062
1063     if (ioctl(sockfd, SIOCDELRT, &rt) < 0) {
1064         syslog(LOG_ERR, "Can't delete default route: %m");
1065         return 0;
1066     }
1067
1068     default_route_gateway = 0;
1069     return 1;
1070 }
1071
1072 /*
1073  * sifproxyarp - Make a proxy ARP entry for the peer.
1074  */
1075 int
1076 sifproxyarp(unit, hisaddr)
1077     int unit;
1078     u_int32_t hisaddr;
1079 {
1080     struct arpreq arpreq;
1081
1082     bzero(&arpreq, sizeof(arpreq));
1083     if (!get_ether_addr(hisaddr, &arpreq.arp_ha))
1084         return 0;
1085
1086     arpreq.arp_pa.sa_family = AF_INET;
1087     INET_ADDR(arpreq.arp_pa) = hisaddr;
1088     arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1089     if (ioctl(sockfd, SIOCSARP, (caddr_t) &arpreq) < 0) {
1090         syslog(LOG_ERR, "Couldn't set proxy ARP entry: %m");
1091         return 0;
1092     }
1093
1094     proxy_arp_addr = hisaddr;
1095     return 1;
1096 }
1097
1098 /*
1099  * cifproxyarp - Delete the proxy ARP entry for the peer.
1100  */
1101 int
1102 cifproxyarp(unit, hisaddr)
1103     int unit;
1104     u_int32_t hisaddr;
1105 {
1106     struct arpreq arpreq;
1107
1108     bzero(&arpreq, sizeof(arpreq));
1109     arpreq.arp_pa.sa_family = AF_INET;
1110     INET_ADDR(arpreq.arp_pa) = hisaddr;
1111     if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1112         syslog(LOG_ERR, "Couldn't delete proxy ARP entry: %m");
1113         return 0;
1114     }
1115
1116     proxy_arp_addr = 0;
1117     return 1;
1118 }
1119
1120 /*
1121  * get_ether_addr - get the hardware address of an interface on the
1122  * the same subnet as ipaddr.
1123  */
1124 #define MAX_IFS         32
1125
1126 static int
1127 get_ether_addr(ipaddr, hwaddr)
1128     u_int32_t ipaddr;
1129     struct sockaddr *hwaddr;
1130 {
1131     struct ifreq *ifr, *ifend;
1132     u_int32_t ina, mask;
1133     struct ifreq ifreq;
1134     struct ifconf ifc;
1135     struct ifreq ifs[MAX_IFS];
1136     int nit_fd;
1137
1138     ifc.ifc_len = sizeof(ifs);
1139     ifc.ifc_req = ifs;
1140     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1141         syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
1142         return 0;
1143     }
1144
1145     /*
1146      * Scan through looking for an interface with an Internet
1147      * address on the same subnet as `ipaddr'.
1148      */
1149     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1150     for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1151             ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1152         if (ifr->ifr_addr.sa_family == AF_INET) {
1153
1154             /*
1155              * Check that the interface is up, and not point-to-point
1156              * or loopback.
1157              */
1158             strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1159             if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1160                 continue;
1161             if ((ifreq.ifr_flags &
1162                  (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1163                  != (IFF_UP|IFF_BROADCAST))
1164                 continue;
1165
1166             /*
1167              * Get its netmask and check that it's on the right subnet.
1168              */
1169             if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1170                 continue;
1171             ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1172             mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1173             if ((ipaddr & mask) != (ina & mask))
1174                 continue;
1175
1176             break;
1177         }
1178     }
1179
1180     if (ifr >= ifend)
1181         return 0;
1182     syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
1183
1184     /*
1185      * Grab the physical address for this interface.
1186      */
1187     if ((nit_fd = open("/dev/nit", O_RDONLY)) < 0) {
1188         syslog(LOG_ERR, "Couldn't open /dev/nit: %m");
1189         return 0;
1190     }
1191     strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1192     if (ioctl(nit_fd, NIOCBIND, &ifreq) < 0
1193         || ioctl(nit_fd, SIOCGIFADDR, &ifreq) < 0) {
1194         syslog(LOG_ERR, "Couldn't get hardware address for %s: %m",
1195                ifreq.ifr_name);
1196         close(nit_fd);
1197         return 0;
1198     }
1199
1200     hwaddr->sa_family = AF_UNSPEC;
1201     memcpy(hwaddr->sa_data, ifreq.ifr_addr.sa_data, 6);
1202     close(nit_fd);
1203     return 1;
1204 }
1205
1206 #define WTMPFILE        "/usr/adm/wtmp"
1207
1208 int
1209 logwtmp(line, name, host)
1210     char *line, *name, *host;
1211 {
1212     int fd;
1213     struct stat buf;
1214     struct utmp ut;
1215
1216     if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1217         return;
1218     if (!fstat(fd, &buf)) {
1219         (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1220         (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1221         (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1222         (void)time(&ut.ut_time);
1223         if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1224             (void)ftruncate(fd, buf.st_size);
1225     }
1226     close(fd);
1227 }
1228
1229 /*
1230  * Return user specified netmask, modified by any mask we might determine
1231  * for address `addr' (in network byte order).
1232  * Here we scan through the system's list of interfaces, looking for
1233  * any non-point-to-point interfaces which might appear to be on the same
1234  * network as `addr'.  If we find any, we OR in their netmask to the
1235  * user-specified netmask.
1236  */
1237 u_int32_t
1238 GetMask(addr)
1239     u_int32_t addr;
1240 {
1241     u_int32_t mask, nmask, ina;
1242     struct ifreq *ifr, *ifend, ifreq;
1243     struct ifconf ifc;
1244
1245     addr = ntohl(addr);
1246     if (IN_CLASSA(addr))        /* determine network mask for address class */
1247         nmask = IN_CLASSA_NET;
1248     else if (IN_CLASSB(addr))
1249         nmask = IN_CLASSB_NET;
1250     else
1251         nmask = IN_CLASSC_NET;
1252     /* class D nets are disallowed by bad_ip_adrs */
1253     mask = netmask | htonl(nmask);
1254
1255     /*
1256      * Scan through the system's network interfaces.
1257      */
1258     ifc.ifc_len = MAX_IFS * sizeof(struct ifreq);
1259     ifc.ifc_req = alloca(ifc.ifc_len);
1260     if (ifc.ifc_req == 0)
1261         return mask;
1262     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1263         syslog(LOG_WARNING, "Couldn't get system interface list: %m");
1264         return mask;
1265     }
1266     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1267     for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1268         /*
1269          * Check the interface's internet address.
1270          */
1271         if (ifr->ifr_addr.sa_family != AF_INET)
1272             continue;
1273         ina = INET_ADDR(ifr->ifr_addr);
1274         if ((ntohl(ina) & nmask) != (addr & nmask))
1275             continue;
1276         /*
1277          * Check that the interface is up, and not point-to-point or loopback.
1278          */
1279         strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1280         if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1281             continue;
1282         if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1283             != IFF_UP)
1284             continue;
1285         /*
1286          * Get its netmask and OR it into our mask.
1287          */
1288         if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1289             continue;
1290         mask |= INET_ADDR(ifreq.ifr_addr);
1291     }
1292
1293     return mask;
1294 }
1295
1296 static int
1297 strioctl(fd, cmd, ptr, ilen, olen)
1298     int fd, cmd, ilen, olen;
1299     void *ptr;
1300 {
1301     struct strioctl str;
1302
1303     str.ic_cmd = cmd;
1304     str.ic_timout = 0;
1305     str.ic_len = ilen;
1306     str.ic_dp = ptr;
1307     if (ioctl(fd, I_STR, &str) == -1)
1308         return -1;
1309     if (str.ic_len != olen)
1310         syslog(LOG_DEBUG, "strioctl: expected %d bytes, got %d for cmd %x\n",
1311                olen, str.ic_len, cmd);
1312     return 0;
1313 }
1314
1315 /*
1316  * Code for locking/unlocking the serial device.
1317  * This code is derived from chat.c.
1318  */
1319
1320 #if !defined(HDB) && !defined(SUNOS3)
1321 #define HDB     1               /* ascii lock files are the default */
1322 #endif
1323
1324 #ifndef LOCK_DIR
1325 # if HDB
1326 #  define       PIDSTRING
1327 #  define       LOCK_PREFIX     "/usr/spool/locks/LCK.."
1328 # else /* HDB */
1329 #  define       LOCK_PREFIX     "/usr/spool/uucp/LCK.."
1330 # endif /* HDB */
1331 #endif /* LOCK_DIR */
1332
1333 static char *lock_file;         /* name of lock file created */
1334
1335 /*
1336  * lock - create a lock file for the named device.
1337  */
1338 int
1339 lock(dev)
1340     char *dev;
1341 {
1342     char hdb_lock_buffer[12];
1343     int fd, pid, n;
1344     char *p;
1345
1346     if ((p = strrchr(dev, '/')) != NULL)
1347         dev = p + 1;
1348     lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1349     if (lock_file == NULL)
1350         novm("lock file name");
1351     strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1352
1353     while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1354         if (errno == EEXIST
1355             && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1356             /* Read the lock file to find out who has the device locked */
1357 #ifdef PIDSTRING
1358             n = read(fd, hdb_lock_buffer, 11);
1359             if (n > 0) {
1360                 hdb_lock_buffer[n] = 0;
1361                 pid = atoi(hdb_lock_buffer);
1362             }
1363 #else
1364             n = read(fd, &pid, sizeof(pid));
1365 #endif
1366             if (n <= 0) {
1367                 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1368                 close(fd);
1369             } else {
1370                 if (kill(pid, 0) == -1 && errno == ESRCH) {
1371                     /* pid no longer exists - remove the lock file */
1372                     if (unlink(lock_file) == 0) {
1373                         close(fd);
1374                         syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1375                                dev, pid);
1376                         continue;
1377                     } else
1378                         syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1379                                dev);
1380                 } else
1381                     syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1382                            dev, pid);
1383             }
1384             close(fd);
1385         } else
1386             syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1387         free(lock_file);
1388         lock_file = NULL;
1389         return -1;
1390     }
1391
1392 #ifdef PIDSTRING
1393     sprintf(hdb_lock_buffer, "%10d\n", getpid());
1394     write(fd, hdb_lock_buffer, 11);
1395 #else
1396     pid = getpid();
1397     write(fd, &pid, sizeof pid);
1398 #endif
1399
1400     close(fd);
1401     return 0;
1402 }
1403
1404 /*
1405  * unlock - remove our lockfile
1406  */
1407 void
1408 unlock()
1409 {
1410     if (lock_file) {
1411         unlink(lock_file);
1412         free(lock_file);
1413         lock_file = NULL;
1414     }
1415 }
1416
1417 /*
1418  * SunOS doesn't have strtoul :-(
1419  */
1420 unsigned long
1421 strtoul(str, ptr, base)
1422     char *str, **ptr;
1423     int base;
1424 {
1425     return (unsigned long) strtol(str, ptr, base);
1426 }
1427
1428 /*
1429  * Or strerror :-(
1430  */
1431 extern char *sys_errlist[];
1432 extern int sys_nerr;
1433
1434 char *
1435 strerror(n)
1436     int n;
1437 {
1438     static char unknown[32];
1439
1440     if (n > 0 && n < sys_nerr)
1441         return sys_errlist[n];
1442     sprintf(unknown, "Error %d", n);
1443     return unknown;
1444 }