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