]> git.ozlabs.org Git - ppp.git/blob - pppd/sys-ultrix.c
add packet filtering and demand dial support (?); fix copyright;
[ppp.git] / pppd / sys-ultrix.c
1 /*
2  * sys-ultrix.c - System-dependent procedures for setting up
3  * PPP interfaces on Ultrix systems.
4  *
5  * Copyright (c) 1989 Carnegie Mellon University.
6  * Copyright (c) 1995 The Australian National University.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms are permitted
10  * provided that the above copyright notice and this paragraph are
11  * duplicated in all such forms and that any documentation,
12  * advertising materials, and other materials related to such
13  * distribution and use acknowledge that the software was developed
14  * by Carnegie Mellon University and The Australian National University.
15  * The names of the Universities may not be used to endorse or promote
16  * products derived from this software without specific prior written
17  * permission.
18  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21  */
22
23 #ifndef lint
24 static char rcsid[] = "$Id: sys-ultrix.c,v 1.16 1996/04/04 04:04:19 paulus Exp $";
25 #endif
26
27 /*
28  * TODO:
29  */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <syslog.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <termios.h>
39 #include <utmp.h>
40 #include <sys/types.h>
41 #include <sys/file.h>
42 #include <sys/socket.h>
43 #include <sys/ioctl.h>
44 #include <sys/time.h>
45 #include <sys/errno.h>
46 #include <sys/stat.h>
47
48 #include <net/if.h>
49 #include <net/ppp_defs.h>
50 #include <net/if_ppp.h>
51 #include <net/route.h>
52 #include <netinet/in.h>
53
54 #include "pppd.h"
55
56 static int initdisc = -1;       /* Initial TTY discipline for ppp_fd */
57 static int initfdflags = -1;    /* Initial file descriptor flags for ppp_fd */
58 static int ppp_fd = -1;         /* fd which is set to PPP discipline */
59
60 static int restore_term;        /* 1 => we've munged the terminal */
61 static struct termios inittermios; /* Initial TTY termios */
62 static struct winsize wsinfo;   /* Initial window size info */
63
64 static char *lock_file;         /* name of lock file created */
65
66 static int loop_slave = -1;
67 static int loop_master;
68 static char loop_name[20];
69
70 static unsigned char inbuf[512]; /* buffer for chars read from loopback */
71
72 static int sockfd;              /* socket for doing interface ioctls */
73
74 static int if_is_up;            /* the interface is currently up */
75 static u_int32_t ifaddrs[2];    /* local and remote addresses */
76 static u_int32_t default_route_gateway; /* gateway addr for default route */
77 static u_int32_t proxy_arp_addr;        /* remote addr for proxy arp */
78
79 /* Prototypes for procedures local to this file. */
80 static int translate_speed __P((int));
81 static int baud_rate_of __P((int));
82 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
83
84
85 /*
86  * sys_init - System-dependent initialization.
87  */
88 void
89 sys_init()
90 {
91     openlog("pppd", LOG_PID);
92
93     /* Get an internet socket for doing socket ioctl's on. */
94     if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
95         syslog(LOG_ERR, "Couldn't create IP socket: %m");
96         die(1);
97     }
98 }
99
100 /*
101  * sys_cleanup - restore any system state we modified before exiting:
102  * mark the interface down, delete default route and/or proxy arp entry.
103  * This should call die() because it's called from die().
104  */
105 void
106 sys_cleanup()
107 {
108     struct ifreq ifr;
109
110     if (if_is_up) {
111         strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
112         if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) >= 0
113             && ((ifr.ifr_flags & IFF_UP) != 0)) {
114             ifr.ifr_flags &= ~IFF_UP;
115             ioctl(sockfd, SIOCSIFFLAGS, &ifr);
116         }
117     }
118     if (ifaddrs[0])
119         cifaddr(0, ifaddrs[0], ifaddrs[1]);
120     if (default_route_gateway)
121         cifdefaultroute(0, default_route_gateway);
122     if (proxy_arp_addr)
123         cifproxyarp(0, proxy_arp_addr);
124 }
125
126 /*
127  * sys_close - Clean up in a child process before execing.
128  */
129 void
130 sys_close()
131 {
132     close(sockfd);
133     if (loop_slave >= 0) {
134         close(loop_slave);
135         close(loop_master);
136     }
137     closelog();
138 }
139
140 /*
141  * sys_check_options - check the options that the user specified
142  */
143 void
144 sys_check_options()
145 {
146     if (demand) {
147         fprintf(stderr,
148                 "Sorry - demand-dialling is not supported under Ultrix\n");
149         exit(1);
150     }
151 }
152
153
154 /*
155  * note_debug_level - note a change in the debug level.
156  */
157 void
158 note_debug_level()
159 {
160 }
161
162
163 /*
164  * daemon - Detach us from the terminal session.
165  */
166 int
167 daemon(nochdir, noclose)
168     int nochdir, noclose;
169 {
170     int pid;
171
172     if ((pid = fork()) < 0)
173         return -1;
174     if (pid != 0)
175         exit(0);                /* parent dies */
176     setsid();
177     if (!nochdir)
178         chdir("/");
179     if (!noclose) {
180         fclose(stdin);          /* don't need stdin, stdout, stderr */
181         fclose(stdout);
182         fclose(stderr);
183     }
184     return 0;
185 }
186
187 /*
188  * ppp_available - check whether the system has any ppp interfaces
189  * (in fact we check whether we can do an ioctl on ppp0).
190  */
191 int
192 ppp_available()
193 {
194     int s, ok;
195     struct ifreq ifr;
196     extern char *no_ppp_msg;
197
198     if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
199         return 1;               /* can't tell */
200
201     strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
202     ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
203     close(s);
204
205     no_ppp_msg = "\
206 This system lacks kernel support for PPP.  To include PPP support\n\
207 in the kernel, please follow the steps detailed in the README.ultrix\n\
208 file in the ppp-2.2 distribution.\n";
209     return ok;
210 }
211
212 /*
213  * establish_ppp - Turn the serial port into a ppp interface.
214  */
215 void
216 establish_ppp(fd)
217     int fd;
218 {
219     int pppdisc = PPPDISC;
220     int x;
221
222     /*
223      * Save the old line discipline of fd, and set it to PPP.
224      */
225     if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
226         syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
227         die(1);
228     }
229     if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
230         syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
231         die(1);
232     }
233
234     /*
235      * Find out which interface we were given.
236      */
237     if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {  
238         syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
239         die(1);
240     }
241
242     ppp_fd = fd;
243
244     /*
245      * Enable debug in the driver if requested.
246      */
247     if (kdebugflag) {
248         if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
249             syslog(LOG_WARNING, "ioctl (PPPIOCGFLAGS): %m");
250         } else {
251             x |= (kdebugflag & 0xFF) * SC_DEBUG;
252             if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
253                 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
254         }
255     }
256
257     /*
258      * Set device for non-blocking reads.
259      */
260     if ((initfdflags = fcntl(fd, F_GETFL)) == -1
261         || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
262         syslog(LOG_WARNING, "Couldn't set device to non-blocking mode: %m");
263     }
264 }
265
266 /*
267  * restore_loop - reattach the ppp unit to the loopback.
268  */
269 void
270 restore_loop()
271 {
272 }
273
274 /*
275  * disestablish_ppp - Restore the serial port to normal operation.
276  * This shouldn't call die() because it's called from die().
277  */
278 void
279 disestablish_ppp(fd)
280      int fd;
281 {
282     /* Reset non-blocking mode on fd. */
283     if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
284         syslog(LOG_WARNING, "Couldn't restore device fd flags: %m");
285     initfdflags = -1;
286
287     /* Restore old line discipline. */
288     if (initdisc >= 0 && ioctl(fd, TIOCSETD, &initdisc) < 0)
289         syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
290     initdisc = -1;
291
292     if (fd == ppp_fd)
293         ppp_fd = -1;
294 }
295
296 /*
297  * Check whether the link seems not to be 8-bit clean.
298  */
299 void
300 clean_check()
301 {
302     int x;
303     char *s;
304
305     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
306         s = NULL;
307         switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
308         case SC_RCV_B7_0:
309             s = "bit 7 set to 1";
310             break;
311         case SC_RCV_B7_1:
312             s = "bit 7 set to 0";
313             break;
314         case SC_RCV_EVNP:
315             s = "odd parity";
316             break;
317         case SC_RCV_ODDP:
318             s = "even parity";
319             break;
320         }
321         if (s != NULL) {
322             syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
323             syslog(LOG_WARNING, "All received characters had %s", s);
324         }
325     }
326 }
327
328
329 /*
330  * List of valid speeds.
331  */
332 struct speed {
333     int speed_int, speed_val;
334 } speeds[] = {
335 #ifdef B50
336     { 50, B50 },
337 #endif
338 #ifdef B75
339     { 75, B75 },
340 #endif
341 #ifdef B110
342     { 110, B110 },
343 #endif
344 #ifdef B134
345     { 134, B134 },
346 #endif
347 #ifdef B150
348     { 150, B150 },
349 #endif
350 #ifdef B200
351     { 200, B200 },
352 #endif
353 #ifdef B300
354     { 300, B300 },
355 #endif
356 #ifdef B600
357     { 600, B600 },
358 #endif
359 #ifdef B1200
360     { 1200, B1200 },
361 #endif
362 #ifdef B1800
363     { 1800, B1800 },
364 #endif
365 #ifdef B2000
366     { 2000, B2000 },
367 #endif
368 #ifdef B2400
369     { 2400, B2400 },
370 #endif
371 #ifdef B3600
372     { 3600, B3600 },
373 #endif
374 #ifdef B4800
375     { 4800, B4800 },
376 #endif
377 #ifdef B7200
378     { 7200, B7200 },
379 #endif
380 #ifdef B9600
381     { 9600, B9600 },
382 #endif
383 #ifdef B19200
384     { 19200, B19200 },
385 #endif
386 #ifdef B38400
387     { 38400, B38400 },
388 #endif
389 #ifdef EXTA
390     { 19200, EXTA },
391 #endif
392 #ifdef EXTB
393     { 38400, EXTB },
394 #endif
395 #ifdef B57600
396     { 57600, B57600 },
397 #endif
398 #ifdef B115200
399     { 115200, B115200 },
400 #endif
401     { 0, 0 }
402 };
403
404 /*
405  * Translate from bits/second to a speed_t.
406  */
407 int
408 translate_speed(bps)
409     int bps;
410 {
411     struct speed *speedp;
412
413     if (bps == 0)
414         return 0;
415     for (speedp = speeds; speedp->speed_int; speedp++)
416         if (bps == speedp->speed_int)
417             return speedp->speed_val;
418     syslog(LOG_WARNING, "speed %d not supported", bps);
419     return 0;
420 }
421
422 /*
423  * Translate from a speed_t to bits/second.
424  */
425 int
426 baud_rate_of(speed)
427     int speed;
428 {
429     struct speed *speedp;
430
431     if (speed == 0)
432         return 0;
433     for (speedp = speeds; speedp->speed_int; speedp++)
434         if (speed == speedp->speed_val)
435             return speedp->speed_int;
436     return 0;
437 }
438
439 /*
440  * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
441  * at the requested speed, etc.  If `local' is true, set CLOCAL
442  * regardless of whether the modem option was specified.
443  */
444 void
445 set_up_tty(fd, local)
446     int fd, local;
447 {
448     int speed, x;
449     struct termios tios;
450
451     if (tcgetattr(fd, &tios) < 0) {
452         syslog(LOG_ERR, "tcgetattr: %m");
453         die(1);
454     }
455
456     if (!restore_term) {
457         inittermios = tios;
458         ioctl(fd, TIOCGWINSZ, &wsinfo);
459     }
460
461     tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
462 #ifdef CRTSCTS
463     if (crtscts > 0 && !local)
464         tios.c_cflag |= CRTSCTS;
465     else if (crtscts < 0)
466         tios.c_cflag &= ~CRTSCTS;
467 #endif  /* CRTSCTS */
468
469     tios.c_cflag |= CS8 | CREAD | HUPCL;
470     if (local || !modem)
471         tios.c_cflag |= CLOCAL;
472     tios.c_iflag = IGNBRK | IGNPAR;
473     tios.c_oflag = 0;
474     tios.c_lflag = 0;
475     tios.c_cc[VMIN] = 1;
476     tios.c_cc[VTIME] = 0;
477
478     if (crtscts == -2) {
479         tios.c_iflag |= IXON | IXOFF;
480         tios.c_cc[VSTOP] = 0x13;        /* DC3 = XOFF = ^S */
481         tios.c_cc[VSTART] = 0x11;       /* DC1 = XON  = ^Q */
482     }
483
484     speed = translate_speed(inspeed);
485     if (speed) {
486         cfsetospeed(&tios, speed);
487         cfsetispeed(&tios, speed);
488     } else {
489         speed = cfgetospeed(&tios);
490         /*
491          * We can't proceed if the serial port speed is B0,
492          * since that implies that the serial port is disabled.
493          */
494         if (speed == B0) {
495             syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
496                    devnam);
497             die(1);
498         }
499     }
500
501     if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
502         syslog(LOG_ERR, "tcsetattr: %m");
503         die(1);
504     }
505
506     x = 0;
507     if (ioctl(fd, (crtscts > 0 || modem)? TIOCMODEM: TIOCNMODEM, &x) < 0)
508         syslog(LOG_WARNING, "TIOC(N)MODEM: %m");
509     if (ioctl(fd, (local || !modem)? TIOCNCAR: TIOCCAR) < 0)
510         syslog(LOG_WARNING, "TIOC(N)CAR: %m");
511
512     baud_rate = inspeed = baud_rate_of(speed);
513     restore_term = TRUE;
514 }
515
516 /*
517  * restore_tty - restore the terminal to the saved settings.
518  */
519 void
520 restore_tty(fd)
521     int fd;
522 {
523     if (restore_term) {
524         if (!default_device) {
525             /*
526              * Turn off echoing, because otherwise we can get into
527              * a loop with the tty and the modem echoing to each other.
528              * We presume we are the sole user of this tty device, so
529              * when we close it, it will revert to its defaults anyway.
530              */
531             inittermios.c_lflag &= ~(ECHO | ECHONL);
532         }
533         if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
534             if (errno != ENXIO)
535                 syslog(LOG_WARNING, "tcsetattr: %m");
536         ioctl(fd, TIOCSWINSZ, &wsinfo);
537         restore_term = FALSE;
538     }
539 }
540
541 /*
542  * setdtr - control the DTR line on the serial port.
543  * This is called from die(), so it shouldn't call die().
544  */
545 void
546 setdtr(fd, on)
547 int fd, on;
548 {
549     int modembits = TIOCM_DTR;
550
551     ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
552 }
553
554
555 /*
556  * open_ppp_loopback - open the device we use for getting
557  * packets in demand mode, and connect it to a ppp interface.
558  * Here we use would use a pty, except that Ultrix ptys are brain-dead.
559  */
560 void
561 open_ppp_loopback()
562 {
563     syslog(LOG_ERR, "open_ppp_loopback called!");
564     die(1);
565 }
566
567
568 /*
569  * output - Output PPP packet.
570  */
571 void
572 output(unit, p, len)
573     int unit;
574     u_char *p;
575     int len;
576 {
577     if (debug)
578         log_packet(p, len, "sent ");
579
580     if (write(ttyfd, p, len) < 0) {
581         if (errno != EIO)
582             syslog(LOG_ERR, "write: %m");
583     }
584 }
585
586
587 /*
588  * wait_input - wait until there is data available on ttyfd,
589  * for the length of time specified by *timo (indefinite
590  * if timo is NULL).
591  */
592 void
593 wait_input(timo)
594     struct timeval *timo;
595 {
596     fd_set ready;
597     int n;
598
599     FD_ZERO(&ready);
600     FD_SET(ttyfd, &ready);
601     n = select(ttyfd+1, &ready, NULL, &ready, timo);
602     if (n < 0 && errno != EINTR) {
603         syslog(LOG_ERR, "select: %m");
604         die(1);
605     }
606 }
607
608
609 /*
610  * wait_loop_output - wait until there is data available on the
611  * loopback, for the length of time specified by *timo (indefinite
612  * if timo is NULL).
613  */
614 wait_loop_output(timo)
615     struct timeval *timo;
616 {
617     wait_time(timo);
618 }
619
620
621 /*
622  * wait_time - wait for a given length of time or until a
623  * signal is received.
624  */
625 wait_time(timo)
626     struct timeval *timo;
627 {
628     int n;
629
630     n = select(0, NULL, NULL, NULL, timo);
631     if (n < 0 && errno != EINTR) {
632         syslog(LOG_ERR, "select: %m");
633         die(1);
634     }
635 }
636
637
638 /*
639  * read_packet - get a PPP packet from the serial device.
640  */
641 int
642 read_packet(buf)
643     u_char *buf;
644 {
645     int len;
646
647     if ((len = read(ttyfd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
648         if (errno == EWOULDBLOCK || errno == EINTR)
649             return -1;
650         syslog(LOG_ERR, "read(fd): %m");
651         die(1);
652     }
653     return len;
654 }
655
656
657 /*
658  * get_loop_output - read characters from the loopback, form them
659  * into frames, and detect when we want to bring the real link up.
660  * Return value is 1 if we need to bring up the link, 0 otherwise.
661  */
662 int
663 get_loop_output()
664 {
665     return 0;
666 }
667
668
669 /*
670  * ppp_send_config - configure the transmit characteristics of
671  * the ppp interface.
672  */
673 void
674 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
675     int unit, mtu;
676     u_int32_t asyncmap;
677     int pcomp, accomp;
678 {
679     u_int x;
680
681     if (ioctl(ppp_fd, PPPIOCSMTU, &mtu) < 0) {
682         syslog(LOG_ERR, "ioctl(PPPIOCSMTU): %m");
683         quit();
684     }
685
686     if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
687         syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
688         quit();
689     }
690
691     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
692         syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
693         quit();
694     }
695     x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
696     x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
697     if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
698         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
699         quit();
700     }
701 }
702
703
704 /*
705  * ppp_set_xaccm - set the extended transmit ACCM for the interface.
706  */
707 void
708 ppp_set_xaccm(unit, accm)
709     int unit;
710     ext_accm accm;
711 {
712     if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
713         syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
714 }
715
716
717 /*
718  * ppp_recv_config - configure the receive-side characteristics of
719  * the ppp interface.
720  */
721 void
722 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
723     int unit, mru;
724     u_int32_t asyncmap;
725     int pcomp, accomp;
726 {
727     int x;
728
729     if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
730         syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
731         quit();
732     }
733     if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
734         syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
735         quit();
736     }
737     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
738         syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
739         quit();
740     }
741     x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
742     if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
743         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
744         quit();
745     }
746 }
747
748 /*
749  * ccp_test - ask kernel whether a given compression method
750  * is acceptable for use.  Returns 1 if the method and parameters
751  * are OK, 0 if the method is known but the parameters are not OK
752  * (e.g. code size should be reduced), or -1 if the method is unknown.
753  */
754 int
755 ccp_test(unit, opt_ptr, opt_len, for_transmit)
756     int unit, opt_len, for_transmit;
757     u_char *opt_ptr;
758 {
759     struct ppp_option_data data;
760
761     data.ptr = opt_ptr;
762     data.length = opt_len;
763     data.transmit = for_transmit;
764     if (ioctl(ttyfd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
765         return 1;
766     return (errno == ENOBUFS)? 0: -1;
767 }
768
769 /*
770  * ccp_flags_set - inform kernel about the current state of CCP.
771  */
772 void
773 ccp_flags_set(unit, isopen, isup)
774     int unit, isopen, isup;
775 {
776     int x;
777
778     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
779         syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
780         return;
781     }
782     x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
783     x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
784     if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
785         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
786 }
787
788 /*
789  * ccp_fatal_error - returns 1 if decompression was disabled as a
790  * result of an error detected after decompression of a packet,
791  * 0 otherwise.  This is necessary because of patent nonsense.
792  */
793 int
794 ccp_fatal_error(unit)
795     int unit;
796 {
797     int x;
798
799     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
800         syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
801         return 0;
802     }
803     return x & SC_DC_FERROR;
804 }
805
806 /*
807  * get_idle_time - return how long the link has been idle.
808  */
809 int
810 get_idle_time(u, ip)
811     int u;
812     struct ppp_idle *ip;
813 {
814     return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0;
815 }
816
817
818 /*
819  * set_filters - transfer the pass and active filters to the kernel.
820  */
821 int
822 set_filters(pass, active)
823     struct bpf_program *pass, *active;
824 {
825     int ret = 1;
826
827     if (pass->bf_len > 0) {
828         if (ioctl(ppp_fd, PPPIOCSPASS, pass) < 0) {
829             syslog(LOG_ERR, "Couldn't set pass-filter in kernel: %m");
830             ret = 0;
831         }
832     }
833     if (active->bf_len > 0) {
834         if (ioctl(ppp_fd, PPPIOCSACTIVE, active) < 0) {
835             syslog(LOG_ERR, "Couldn't set active-filter in kernel: %m");
836             ret = 0;
837         }
838     }
839     return ret;
840 }
841
842
843 /*
844  * sifvjcomp - config tcp header compression
845  */
846 int
847 sifvjcomp(u, vjcomp, cidcomp, maxcid)
848     int u, vjcomp, cidcomp, maxcid;
849 {
850     u_int x;
851
852     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
853         syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
854         return 0;
855     }
856     x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
857     x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
858     if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
859         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
860         return 0;
861     }
862     if (ioctl(ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
863         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
864         return 0;
865     }
866     return 1;
867 }
868
869 /*
870  * sifup - Config the interface up and enable IP packets to pass.
871  */
872 int
873 sifup(u)
874     int u;
875 {
876     struct ifreq ifr;
877     struct npioctl npi;
878
879     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
880     if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
881         syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
882         return 0;
883     }
884     ifr.ifr_flags |= IFF_UP;
885     if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
886         syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
887         return 0;
888     }
889     if_is_up = 1;
890     npi.protocol = PPP_IP;
891     npi.mode = NPMODE_PASS;
892     if (ioctl(ppp_fd, PPPIOCSNPMODE, &npi) < 0) {
893         syslog(LOG_ERR, "ioctl(set IP mode to PASS): %m");
894         return 0;
895     }
896     return 1;
897 }
898
899 /*
900  * sifnpmode - Set the mode for handling packets for a given NP.
901  */
902 int
903 sifnpmode(u, proto, mode)
904     int u;
905     int proto;
906     enum NPmode mode;
907 {
908     struct npioctl npi;
909
910     npi.protocol = proto;
911     npi.mode = mode;
912     if (ioctl(ppp_fd, PPPIOCSNPMODE, &npi) < 0) {
913         syslog(LOG_ERR, "ioctl(set NP %d mode to %d): %m", proto, mode);
914         return 0;
915     }
916     return 1;
917 }
918
919 /*
920  * sifdown - Config the interface down and disable IP.
921  */
922 int
923 sifdown(u)
924     int u;
925 {
926     struct ifreq ifr;
927     int rv;
928     struct npioctl npi;
929
930     rv = 1;
931     npi.protocol = PPP_IP;
932     npi.mode = NPMODE_ERROR;
933     ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi);
934     /* ignore errors, because ppp_fd might have been closed by now. */
935
936     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
937     if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
938         syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
939         rv = 0;
940     } else {
941         ifr.ifr_flags &= ~IFF_UP;
942         if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
943             syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
944             rv = 0;
945         } else
946             if_is_up = 0;
947     }
948     return rv;
949 }
950
951 /*
952  * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
953  * if it exists.
954  */
955 #define SET_SA_FAMILY(addr, family)             \
956     BZERO((char *) &(addr), sizeof(addr));      \
957     addr.sa_family = (family); 
958
959 /*
960  * sifaddr - Config the interface IP addresses and netmask.
961  */
962 int
963 sifaddr(u, o, h, m)
964     int u;
965     u_int32_t o, h, m;
966 {
967     int ret;
968     struct ifreq ifr;
969
970     ret = 1;
971     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
972     SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
973     if (m != 0) {
974         ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
975         syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
976         if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
977             syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
978             ret = 0;
979         }
980     }
981     ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
982     if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
983         syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
984         ret = 0;
985     }
986     ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
987     if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
988         syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
989         ret = 0;
990     }
991     ifaddrs[0] = o;
992     ifaddrs[1] = h;
993     return ret;
994 }
995
996 /*
997  * cifaddr - Clear the interface IP addresses, and delete routes
998  * through the interface if possible.
999  */
1000 int
1001 cifaddr(u, o, h)
1002     int u;
1003     u_int32_t o, h;
1004 {
1005     struct rtentry rt;
1006
1007     ifaddrs[0] = 0;
1008     BZERO(&rt, sizeof(rt));
1009     SET_SA_FAMILY(rt.rt_dst, AF_INET);
1010     ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
1011     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1012     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
1013     rt.rt_flags = RTF_HOST;
1014     if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
1015         syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
1016         return 0;
1017     }
1018     return 1;
1019 }
1020
1021 /*
1022  * sifdefaultroute - assign a default route through the address given.
1023  */
1024 int
1025 sifdefaultroute(u, g)
1026 {
1027     struct rtentry rt;
1028
1029     BZERO(&rt, sizeof(rt));
1030     SET_SA_FAMILY(rt.rt_dst, AF_INET);
1031     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1032     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1033     rt.rt_flags = RTF_GATEWAY;
1034     if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
1035         syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
1036         return 0;
1037     }
1038     default_route_gateway = g;
1039     return 1;
1040 }
1041
1042 /*
1043  * cifdefaultroute - delete a default route through the address given.
1044  */
1045 int
1046 cifdefaultroute(u, g)
1047 {
1048     struct rtentry rt;
1049
1050     BZERO(&rt, sizeof(rt));
1051     SET_SA_FAMILY(rt.rt_dst, AF_INET);
1052     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1053     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1054     rt.rt_flags = RTF_GATEWAY;
1055     if (ioctl(sockfd, SIOCDELRT, &rt) < 0)
1056         syslog(LOG_WARNING, "default route ioctl(SIOCDELRT): %m");
1057     default_route_gateway = 0;
1058     return 1;
1059 }
1060
1061 /*
1062  * sifproxyarp - Make a proxy ARP entry for the peer.
1063  */
1064 int
1065 sifproxyarp(unit, hisaddr)
1066     int unit;
1067     u_int32_t hisaddr;
1068 {
1069     struct arpreq arpreq;
1070
1071     BZERO(&arpreq, sizeof(arpreq));
1072
1073     /*
1074      * Get the hardware address of an interface on the same subnet
1075      * as our local address.
1076      */
1077     if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
1078         syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
1079         return 0;
1080     }
1081
1082     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1083     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1084     arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1085     if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
1086         syslog(LOG_ERR, "Couldn't add proxy arp entry: %m");
1087         return 0;
1088     }
1089
1090     proxy_arp_addr = hisaddr;
1091     return 1;
1092 }
1093
1094 /*
1095  * cifproxyarp - Delete the proxy ARP entry for the peer.
1096  */
1097 int
1098 cifproxyarp(unit, hisaddr)
1099     int unit;
1100     u_int32_t hisaddr;
1101 {
1102     struct arpreq arpreq;
1103
1104     BZERO(&arpreq, sizeof(arpreq));
1105     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1106     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1107     if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1108         syslog(LOG_WARNING, "Couldn't delete proxy arp entry: %m");
1109         return 0;
1110     }
1111     proxy_arp_addr = 0;
1112     return 1;
1113 }
1114
1115 /*
1116  * get_ether_addr - get the hardware address of an interface on the
1117  * the same subnet as ipaddr.
1118  */
1119 #define MAX_IFS         32
1120
1121 static int
1122 get_ether_addr(ipaddr, hwaddr)
1123     u_int32_t ipaddr;
1124     struct sockaddr *hwaddr;
1125 {
1126     struct ifreq *ifr, *ifend;
1127     u_int32_t ina, mask;
1128     struct ifreq ifreq;
1129     struct ifconf ifc;
1130     struct ifreq ifs[MAX_IFS];
1131     struct ifdevea ifdevea;
1132
1133     ifc.ifc_len = sizeof(ifs);
1134     ifc.ifc_req = ifs;
1135     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1136         syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
1137         return 0;
1138     }
1139
1140     /*
1141      * Scan through looking for an interface with an Internet
1142      * address on the same subnet as `ipaddr'.
1143      */
1144     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1145     for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1146             ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1147         if (ifr->ifr_addr.sa_family == AF_INET) {
1148             ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1149             strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1150             /*
1151              * Check that the interface is up, and not point-to-point
1152              * or loopback.
1153              */
1154             if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1155                 continue;
1156             if ((ifreq.ifr_flags &
1157                  (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1158                  != (IFF_UP|IFF_BROADCAST))
1159                 continue;
1160             /*
1161              * Get its netmask and check that it's on the right subnet.
1162              */
1163             if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1164                 continue;
1165             mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1166             if ((ipaddr & mask) != (ina & mask))
1167                 continue;
1168
1169             break;
1170         }
1171     }
1172
1173     if (ifr >= ifend)
1174         return 0;
1175     syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
1176
1177     /*
1178      * Grab the physical address for this interface.
1179      */
1180     strncpy(ifdevea.ifr_name, ifr->ifr_name, sizeof(ifdevea.ifr_name));
1181     if (ioctl(sockfd, SIOCRPHYSADDR, &ifdevea) < 0) {
1182         syslog(LOG_ERR, "Couldn't get h/w address for %s: %m", ifr->ifr_name);
1183         return 0;
1184     }
1185
1186     hwaddr->sa_family = AF_UNSPEC;
1187     BCOPY(ifdevea.current_pa, hwaddr->sa_data, 6);
1188     return 1;
1189 }
1190
1191 /*
1192  * Return user specified netmask, modified by any mask we might determine
1193  * for address `addr' (in network byte order).
1194  * Here we scan through the system's list of interfaces, looking for
1195  * any non-point-to-point interfaces which might appear to be on the same
1196  * network as `addr'.  If we find any, we OR in their netmask to the
1197  * user-specified netmask.
1198  */
1199 u_int32_t
1200 GetMask(addr)
1201     u_int32_t addr;
1202 {
1203     u_int32_t mask, nmask, ina;
1204     struct ifreq *ifr, *ifend, ifreq;
1205     struct ifconf ifc;
1206     struct ifreq ifs[MAX_IFS];
1207
1208     addr = ntohl(addr);
1209     if (IN_CLASSA(addr))        /* determine network mask for address class */
1210         nmask = IN_CLASSA_NET;
1211     else if (IN_CLASSB(addr))
1212         nmask = IN_CLASSB_NET;
1213     else
1214         nmask = IN_CLASSC_NET;
1215     /* class D nets are disallowed by bad_ip_adrs */
1216     mask = netmask | htonl(nmask);
1217
1218     /*
1219      * Scan through the system's network interfaces.
1220      */
1221     ifc.ifc_len = sizeof(ifs);
1222     ifc.ifc_req = ifs;
1223     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1224         syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1225         return mask;
1226     }
1227     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1228     for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1229                 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1230         /*
1231          * Check the interface's internet address.
1232          */
1233         if (ifr->ifr_addr.sa_family != AF_INET)
1234             continue;
1235         ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1236         if ((ntohl(ina) & nmask) != (addr & nmask))
1237             continue;
1238         /*
1239          * Check that the interface is up, and not point-to-point or loopback.
1240          */
1241         strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1242         if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1243             continue;
1244         if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1245             != IFF_UP)
1246             continue;
1247         /*
1248          * Get its netmask and OR it into our mask.
1249          */
1250         if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1251             continue;
1252         mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1253     }
1254
1255     return mask;
1256 }
1257
1258
1259 /*
1260   Seems like strdup() is not part of string package in Ultrix.
1261   If I understood the man-page on the sun this should work.
1262
1263   Robert Olsson
1264 */
1265
1266 char *strdup( in ) char *in;
1267 {
1268   char* dup;
1269   if(! (dup = (char *) malloc( strlen( in ) +1 ))) return NULL;
1270   (void) strcpy( dup, in );
1271   return dup;
1272 }
1273
1274 /*
1275  * This logwtmp() implementation is subject to the following copyright:
1276  *
1277  * Copyright (c) 1988 The Regents of the University of California.
1278  * All rights reserved.
1279  *
1280  * Redistribution and use in source and binary forms are permitted
1281  * provided that the above copyright notice and this paragraph are
1282  * duplicated in all such forms and that any documentation,
1283  * advertising materials, and other materials related to such
1284  * distribution and use acknowledge that the software was developed
1285  * by the University of California, Berkeley.  The name of the
1286  * University may not be used to endorse or promote products derived
1287  * from this software without specific prior written permission.
1288  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1289  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1290  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1291  */
1292
1293 #define WTMPFILE        "/usr/adm/wtmp"
1294
1295 int
1296 logwtmp(line, name, host)
1297     char *line, *name, *host;
1298 {
1299     int fd;
1300     struct stat buf;
1301     struct utmp ut;
1302
1303     if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1304         return;
1305     if (!fstat(fd, &buf)) {
1306         (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1307         (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1308         (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1309         (void)time(&ut.ut_time);
1310         if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1311             (void)ftruncate(fd, buf.st_size);
1312     }
1313     close(fd);
1314 }
1315
1316 /*
1317  * Routines for locking and unlocking the serial device, moved here
1318  * from chat.c.
1319  */
1320
1321 #define LOCK_PREFIX     "/usr/spool/uucp/LCK.."
1322
1323 /*
1324  * lock - create a lock file for the named device
1325  */
1326 int
1327 lock(dev)
1328     char *dev;
1329 {
1330     int fd, pid, n;
1331     char *p;
1332
1333     if ((p = strrchr(dev, '/')) != NULL)
1334         dev = p + 1;
1335     lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1336     if (lock_file == NULL)
1337         novm("lock file name");
1338     strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1339
1340     while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1341         if (errno == EEXIST
1342             && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1343             /* Read the lock file to find out who has the device locked */
1344             n = read(fd, &pid, sizeof(pid));
1345             if (n <= 0) {
1346                 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1347                 close(fd);
1348             } else {
1349                 if (kill(pid, 0) == -1 && errno == ESRCH) {
1350                     /* pid no longer exists - remove the lock file */
1351                     if (unlink(lock_file) == 0) {
1352                         close(fd);
1353                         syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1354                                dev, pid);
1355                         continue;
1356                     } else
1357                         syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1358                                dev);
1359                 } else
1360                     syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1361                            dev, pid);
1362             }
1363             close(fd);
1364         } else
1365             syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1366         free(lock_file);
1367         lock_file = NULL;
1368         return -1;
1369     }
1370
1371     pid = getpid();
1372     write(fd, &pid, sizeof pid);
1373
1374     close(fd);
1375     return 0;
1376 }
1377
1378 /*
1379  * unlock - remove our lockfile
1380  */
1381 void
1382 unlock()
1383 {
1384     if (lock_file) {
1385         unlink(lock_file);
1386         free(lock_file);
1387         lock_file = NULL;
1388     }
1389 }