]> git.ozlabs.org Git - ppp.git/blob - pppd/sys-aix4.c
make install depend on pppstats, removed saber nonsense
[ppp.git] / pppd / sys-aix4.c
1 /*
2  * sys-aix4.c - System-dependent procedures for setting up
3  * PPP interfaces on AIX systems which use the STREAMS ppp interface.
4  *
5  * Copyright (c) 1989 Carnegie Mellon University.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms are permitted
9  * provided that the above copyright notice and this paragraph are
10  * duplicated in all such forms and that any documentation,
11  * advertising materials, and other materials related to such
12  * distribution and use acknowledge that the software was developed
13  * by Carnegie Mellon University.  The name of the
14  * University may not be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19  */
20
21 #ifndef lint
22 static char rcsid[] = "$Id: sys-aix4.c,v 1.4 1995/04/27 00:33:49 paulus Exp $";
23 #endif
24
25 /*
26  * TODO:
27  */
28
29 #include <stdio.h>
30 /*
31 #include <stdlib.h>
32 */
33 #include <errno.h>
34 #include <syslog.h>
35 #include <termios.h>
36 #include <sys/termiox.h>
37 #include <fcntl.h>
38 #include <string.h>
39 #include <time.h>
40 #include <utmp.h>
41 #include <poll.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <sys/time.h>
45 #include <sys/stream.h>
46 #include <sys/stropts.h>
47
48 #include <net/if.h>
49 #include <net/ppp_defs.h>
50 #include <net/ppp_str.h>
51 #include <net/route.h>
52 #include <net/if_arp.h>
53 #include <netinet/in.h>
54
55 #include "pppd.h"
56
57 #ifndef ifr_mtu
58 #define ifr_mtu         ifr_metric
59 #endif
60
61 #define MAXMODULES      10      /* max number of module names to save */
62 static struct   modlist {
63     char        modname[FMNAMESZ+1];
64 } str_modules[MAXMODULES];
65 static int      str_module_count = 0;
66 static int      pushed_ppp;
67 static int      closed_stdio;
68
69 static int      restore_term;   /* 1 => we've munged the terminal */
70 static struct termios inittermios; /* Initial TTY termios */
71
72 int sockfd;                     /* socket for doing interface ioctls */
73
74 /*
75  * sys_init - System-dependent initialization.
76  */
77 void
78 sys_init()
79 {
80     openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
81     setlogmask(LOG_UPTO(LOG_INFO));
82     if (debug)
83         setlogmask(LOG_UPTO(LOG_DEBUG));
84
85     /* Get an internet socket for doing socket ioctl's on. */
86     if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
87         syslog(LOG_ERR, "Couldn't create IP socket: %m");
88         die(1);
89     }
90 }
91
92 /*
93  * note_debug_level - note a change in the debug level.
94  */
95 void
96 note_debug_level()
97 {
98     if (debug) {
99         syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
100         setlogmask(LOG_UPTO(LOG_DEBUG));
101     } else {
102         setlogmask(LOG_UPTO(LOG_WARNING));
103     }
104 }
105
106 /*
107  * daemon - Detach us from the terminal session.
108  */
109 int
110 daemon(nochdir, noclose)
111     int nochdir, noclose;
112 {
113     int pid;
114
115     if ((pid = fork()) < 0)
116         return -1;
117     if (pid != 0)
118         exit(0);                /* parent dies */
119     setsid();
120     if (!nochdir)
121         chdir("/");
122     if (!noclose) {
123         fclose(stdin);          /* don't need stdin, stdout, stderr */
124         fclose(stdout);
125         fclose(stderr);
126     }
127     return 0;
128 }
129
130
131 /*
132  * ppp_available - check if this kernel supports PPP.
133  */
134 int
135 ppp_available()
136 {
137     int fd, ret;
138
139     fd = open("/dev/tty", O_RDONLY, 0);
140     if (fd < 0)
141         return 1;               /* can't find out - assume we have ppp */
142     ret = ioctl(fd, I_FIND, "pppasync") >= 0;
143     close(fd);
144     return ret;
145 }
146
147
148 /*
149  * establish_ppp - Turn the serial port into a ppp interface.
150  */
151 void
152 establish_ppp()
153 {
154     /* go through and save the name of all the modules, then pop em */
155     for (;;) { 
156         if (ioctl(fd, I_LOOK, str_modules[str_module_count].modname) < 0 ||
157             ioctl(fd, I_POP, 0) < 0)
158             break;
159         MAINDEBUG((LOG_DEBUG, "popped stream module : %s",
160                    str_modules[str_module_count].modname));
161         str_module_count++;
162     }
163
164     /* now push the async/fcs module */
165     if (ioctl(fd, I_PUSH, "pppasync") < 0) {
166         syslog(LOG_ERR, "ioctl(I_PUSH, ppp_async): %m");
167         die(1);
168     }
169     /* push the compress module */
170     if (ioctl(fd, I_PUSH, "pppcomp") < 0) {
171         syslog(LOG_WARNING, "ioctl(I_PUSH, ppp_comp): %m");
172     }
173     /* finally, push the ppp_if module that actually handles the */
174     /* network interface */ 
175     if (ioctl(fd, I_PUSH, "pppif") < 0) {
176         syslog(LOG_ERR, "ioctl(I_PUSH, ppp_if): %m");
177         die(1);
178     }
179     pushed_ppp = 1;
180     /* read mode, message non-discard mode
181     if (ioctl(fd, I_SRDOPT, RMSGN) < 0) {
182         syslog(LOG_ERR, "ioctl(I_SRDOPT, RMSGN): %m");
183         die(1);
184     }
185 */
186     /*
187      * Find out which interface we were given.
188      * (ppp_if handles this ioctl)
189      */
190     if (ioctl(fd, SIOCGETU, &ifunit) < 0) {
191         syslog(LOG_ERR, "ioctl(SIOCGETU): %m");
192         die(1);
193     }
194
195     /* Set debug flags in driver */
196     if (ioctl(fd, SIOCSIFDEBUG, kdebugflag) < 0) {
197         syslog(LOG_ERR, "ioctl(SIOCSIFDEBUG): %m");
198     }
199
200     /* close stdin, stdout, stderr if they might refer to the device */
201     if (default_device && !closed_stdio) {
202         int i;
203
204         for (i = 0; i <= 2; ++i)
205             if (i != fd && i != sockfd)
206                 close(i);
207         closed_stdio = 1;
208     }
209 }
210
211 /*
212  * disestablish_ppp - Restore the serial port to normal operation.
213  * It attempts to reconstruct the stream with the previously popped
214  * modules.  This shouldn't call die() because it's called from die().
215  */
216 void
217 disestablish_ppp()
218 {
219     int flags;
220     char *s;
221
222     if (hungup) {
223         /* we can't push or pop modules after the stream has hung up */
224         str_module_count = 0;
225         restore_term = 0;       /* nor can we fix up terminal settings */
226         return;
227     }
228
229     if (pushed_ppp) {
230         /*
231          * Check whether the link seems not to be 8-bit clean.
232          */
233         if (ioctl(fd, SIOCGIFDEBUG, (caddr_t) &flags) == 0) {
234             s = NULL;
235             switch (~flags & PAI_FLAGS_HIBITS) {
236             case PAI_FLAGS_B7_0:
237                 s = "bit 7 set to 1";
238                 break;
239             case PAI_FLAGS_B7_1:
240                 s = "bit 7 set to 0";
241                 break;
242             case PAI_FLAGS_PAR_EVEN:
243                 s = "odd parity";
244                 break;
245             case PAI_FLAGS_PAR_ODD:
246                 s = "even parity";
247                 break;
248             }
249             if (s != NULL) {
250                 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
251                 syslog(LOG_WARNING, "All received characters had %s", s);
252             }
253         }
254     }
255
256     while (ioctl(fd, I_POP, 0) == 0)    /* pop any we pushed */
257         ;
258     pushed_ppp = 0;
259   
260     for (; str_module_count > 0; str_module_count--) {
261         if (ioctl(fd, I_PUSH, str_modules[str_module_count-1].modname)) {
262             if (errno != ENXIO)
263                 syslog(LOG_WARNING, "str_restore: couldn't push module %s: %m",
264                        str_modules[str_module_count-1].modname);
265         } else {
266             MAINDEBUG((LOG_INFO, "str_restore: pushed module %s",
267                        str_modules[str_module_count-1].modname));
268         }
269     }
270 }
271
272
273 /*
274  * List of valid speeds.
275  */
276 struct speed {
277     int speed_int, speed_val;
278 } speeds[] = {
279 #ifdef B50
280     { 50, B50 },
281 #endif
282 #ifdef B75
283     { 75, B75 },
284 #endif
285 #ifdef B110
286     { 110, B110 },
287 #endif
288 #ifdef B134
289     { 134, B134 },
290 #endif
291 #ifdef B150
292     { 150, B150 },
293 #endif
294 #ifdef B200
295     { 200, B200 },
296 #endif
297 #ifdef B300
298     { 300, B300 },
299 #endif
300 #ifdef B600
301     { 600, B600 },
302 #endif
303 #ifdef B1200
304     { 1200, B1200 },
305 #endif
306 #ifdef B1800
307     { 1800, B1800 },
308 #endif
309 #ifdef B2000
310     { 2000, B2000 },
311 #endif
312 #ifdef B2400
313     { 2400, B2400 },
314 #endif
315 #ifdef B3600
316     { 3600, B3600 },
317 #endif
318 #ifdef B4800
319     { 4800, B4800 },
320 #endif
321 #ifdef B7200
322     { 7200, B7200 },
323 #endif
324 #ifdef B9600
325     { 9600, B9600 },
326 #endif
327 #ifdef B19200
328     { 19200, B19200 },
329 #endif
330 #ifdef B38400
331     { 38400, B38400 },
332 #endif
333 #ifdef EXTA
334     { 19200, EXTA },
335 #endif
336 #ifdef EXTB
337     { 38400, EXTB },
338 #endif
339 #ifdef B57600
340     { 57600, B57600 },
341 #endif
342 #ifdef B115200
343     { 115200, B115200 },
344 #endif
345     { 0, 0 }
346 };
347
348 /*
349  * Translate from bits/second to a speed_t.
350  */
351 int
352 translate_speed(bps)
353     int bps;
354 {
355     struct speed *speedp;
356
357     if (bps == 0)
358         return 0;
359     for (speedp = speeds; speedp->speed_int; speedp++)
360         if (bps == speedp->speed_int)
361             return speedp->speed_val;
362     syslog(LOG_WARNING, "speed %d not supported", bps);
363     return 0;
364 }
365
366 /*
367  * Translate from a speed_t to bits/second.
368  */
369 int
370 baud_rate_of(speed)
371     int speed;
372 {
373     struct speed *speedp;
374
375     if (speed == 0)
376         return 0;
377     for (speedp = speeds; speedp->speed_int; speedp++)
378         if (speed == speedp->speed_val)
379             return speedp->speed_int;
380     return 0;
381 }
382
383 /*
384  * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
385  * at the requested speed, etc.  If `local' is true, set CLOCAL
386  * regardless of whether the modem option was specified.
387  */
388 set_up_tty(fd, local)
389     int fd, local;
390 {
391     int speed;
392     struct termios tios;
393     struct termiox tiox;
394
395     if (tcgetattr(fd, &tios) < 0) {
396         syslog(LOG_ERR, "tcgetattr: %m");
397         die(1);
398     }
399
400     if (!restore_term)
401         inittermios = tios;
402
403     tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
404     if (crtscts == 1) {
405         bzero(&tiox, sizeof(tiox));
406         ioctl(fd, TCGETX, &tiox);
407         tiox.x_hflag = RTSXOFF;
408         ioctl(fd, TCSETX, &tiox);
409         tios.c_cflag &= ~(IXOFF);
410     } else if (crtscts < 0) {
411         bzero(&tiox, sizeof(tiox));
412         ioctl(fd, TCGETX, &tiox);
413         tiox.x_hflag &= ~RTSXOFF;
414         ioctl(fd, TCSETX, &tiox);
415     }
416
417
418     tios.c_cflag |= CS8 | CREAD | HUPCL;
419     if (local || !modem)
420         tios.c_cflag |= CLOCAL;
421     tios.c_iflag = IGNBRK | IGNPAR;
422     tios.c_oflag = 0;
423     tios.c_lflag = 0;
424     tios.c_cc[VMIN] = 1;
425     tios.c_cc[VTIME] = 0;
426
427     if (crtscts == 2) {
428         tios.c_iflag |= IXOFF;
429         tios.c_cc[VSTOP] = 0x13;        /* DC3 = XOFF = ^S */
430         tios.c_cc[VSTART] = 0x11;       /* DC1 = XON  = ^Q */
431     }
432
433     speed = translate_speed(inspeed);
434     if (speed) {
435         cfsetospeed(&tios, speed);
436         cfsetispeed(&tios, speed);
437     } else {
438         speed = cfgetospeed(&tios);
439         /*
440          * We can't proceed if the serial port speed is B0,
441          * since that implies that the serial port is disabled.
442          */
443         if (speed == B0) {
444             syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
445                    devnam);
446             die(1);
447         }
448     }
449
450     if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
451         syslog(LOG_ERR, "tcsetattr: %m");
452         die(1);
453     }
454
455     baud_rate = inspeed = baud_rate_of(speed);
456     restore_term = 1;
457 }
458
459 /*
460  * restore_tty - restore the terminal to the saved settings.
461  */
462 void
463 restore_tty()
464 {
465     if (restore_term) {
466         if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
467             if (errno != ENXIO)
468                 syslog(LOG_WARNING, "tcsetattr: %m");
469         restore_term = 0;
470     }
471 }
472
473 /*
474  * setdtr - control the DTR line on the serial port.
475  * This is called from die(), so it shouldn't call die().
476  */
477 setdtr(fd, on)
478 int fd, on;
479 {
480     int modembits = TIOCM_DTR;
481
482     ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
483 }
484
485
486 /*
487  * output - Output PPP packet.
488  */
489 void
490 output(unit, p, len)
491     int unit;
492     u_char *p;
493     int len;
494 {
495     struct strbuf       str;
496
497     if (unit != 0)
498         MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
499     if (debug)
500         log_packet(p, len, "sent ");
501
502     str.len = len;
503     str.buf = (caddr_t) p;
504     if (putmsg(fd, NULL, &str, 0) < 0) {
505         if (errno != ENXIO) {
506             syslog(LOG_ERR, "putmsg: %m");
507             die(1);
508         }
509     }
510 }
511
512 /*
513  * wait_input - wait for input, for a length of time specified in *timo.
514  */
515 wait_input(timo)
516     struct timeval *timo;
517 {
518     int t;
519     struct pollfd pfd;
520
521     t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
522     pfd.fd = fd;
523     pfd.events = POLLIN | POLLPRI | POLLHUP;
524     if (poll(&pfd, 1, t) < 0 && errno != EINTR) {
525         syslog(LOG_ERR, "poll: %m");
526         die(1);
527     }
528 }
529
530 /*
531  * read_packet - get a PPP packet from the serial device.
532  */
533 int
534 read_packet(buf)
535     u_char *buf;
536 {
537     struct strbuf str, ctl;
538     int len, i;
539     unsigned char ctlbuf[16];
540
541     str.maxlen = PPP_MTU + PPP_HDRLEN;
542     str.buf = (caddr_t) buf;
543     ctl.maxlen = sizeof(ctlbuf);
544     ctl.buf = (caddr_t) ctlbuf;
545     i = 0;
546     len = getmsg(fd, &ctl, &str, &i);
547     if (len < 0) {
548         if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
549             return -1;
550         }
551         syslog(LOG_ERR, "getmsg(fd) %m");
552         die(1);
553     }
554     if (len) 
555         MAINDEBUG((LOG_DEBUG, "getmsg returned 0x%x",len));
556     if (ctl.len > 0)
557         syslog(LOG_NOTICE, "got ctrl msg len %d %x %x\n", ctl.len,
558                ctlbuf[0], ctlbuf[1]);
559
560     if (str.len < 0) {
561         MAINDEBUG((LOG_DEBUG, "getmsg short return length %d", str.len));
562         return -1;
563     }
564
565     return str.len;
566 }
567
568
569 /*
570  * ppp_send_config - configure the transmit characteristics of
571  * the ppp interface.
572  */
573 void
574 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
575     int unit, mtu;
576     u_int32_t asyncmap;
577     int pcomp, accomp;
578 {
579     int c;
580     struct ifreq ifr;
581
582     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
583     ifr.ifr_mtu = mtu;
584     if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
585         syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
586         quit();
587     }
588
589     if(ioctl(fd, SIOCSIFASYNCMAP, asyncmap) < 0) {
590         syslog(LOG_ERR, "ioctl(SIOCSIFASYNCMAP): %m");
591         quit();
592     }
593
594     c = (pcomp? 1: 0);
595     if(ioctl(fd, SIOCSIFCOMPPROT, c) < 0) {
596         syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
597         quit();
598     }
599
600     c = (accomp? 1: 0);
601     if(ioctl(fd, SIOCSIFCOMPAC, c) < 0) {
602         syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
603         quit();
604     }
605 }
606
607
608 /*
609  * ppp_set_xaccm - set the extended transmit ACCM for the interface.
610  */
611 void
612 ppp_set_xaccm(unit, accm)
613     int unit;
614     ext_accm accm;
615 {
616     if (ioctl(fd, SIOCSIFXASYNCMAP, accm) < 0 && errno != ENOTTY)
617         syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
618 }
619
620
621 /*
622  * ppp_recv_config - configure the receive-side characteristics of
623  * the ppp interface.
624  */
625 void
626 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
627     int unit, mru;
628     u_int32_t asyncmap;
629     int pcomp, accomp;
630 {
631     char c;
632
633     if (ioctl(fd, SIOCSIFMRU, mru) < 0) {
634         syslog(LOG_ERR, "ioctl(SIOCSIFMRU): %m");
635     }
636
637     if (ioctl(fd, SIOCSIFRASYNCMAP, (caddr_t) asyncmap) < 0) {
638         syslog(LOG_ERR, "ioctl(SIOCSIFRASYNCMAP): %m");
639     }
640
641     c = 2 + (pcomp? 1: 0);
642     if(ioctl(fd, SIOCSIFCOMPPROT, c) < 0) {
643         syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
644     }
645
646     c = 2 + (accomp? 1: 0);
647     if (ioctl(fd, SIOCSIFCOMPAC, c) < 0) {
648         syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
649     }
650 }
651
652 /*
653  * ccp_test - ask kernel whether a given compression method
654  * is acceptable for use.
655  */
656 ccp_test(unit, opt_ptr, opt_len, for_transmit)
657     int unit, opt_len, for_transmit;
658     u_char *opt_ptr;
659 {
660     struct ppp_option_data data;
661
662     if ((unsigned) opt_len > MAX_PPP_OPTION)
663         opt_len = MAX_PPP_OPTION;
664     data.length = opt_len;
665     data.transmit = for_transmit;
666     BCOPY(opt_ptr, data.opt_data, opt_len);
667     return ioctl(fd, SIOCSCOMPRESS, &data) >= 0;
668 }
669
670 /*
671  * ccp_flags_set - inform kernel about the current state of CCP.
672  */
673 void
674 ccp_flags_set(unit, isopen, isup)
675     int unit, isopen, isup;
676 {
677     int x;
678
679     x = (isopen? 1: 0) + (isup? 2: 0);
680     if (ioctl(fd, SIOCSIFCOMP, x) < 0 && errno != ENOTTY)
681         syslog(LOG_ERR, "ioctl (SIOCSIFCOMP): %m");
682 }
683
684 /*
685  * ccp_fatal_error - returns 1 if decompression was disabled as a
686  * result of an error detected after decompression of a packet,
687  * 0 otherwise.  This is necessary because of patent nonsense.
688  */
689 int
690 ccp_fatal_error(unit)
691     int unit;
692 {
693     int x;
694
695     if (ioctl(fd, SIOCGIFCOMP, &x) < 0) {
696         syslog(LOG_ERR, "ioctl(SIOCGIFCOMP): %m");
697         return 0;
698     }
699     return x & CCP_FATALERROR;
700 }
701
702 /*
703  * sifvjcomp - config tcp header compression
704  */
705 int
706 sifvjcomp(u, vjcomp, cidcomp, maxcid)
707     int u, vjcomp, cidcomp, maxcid;
708 {
709     int x;
710
711     x = (vjcomp? 1: 0) + (cidcomp? 0: 2) + (maxcid << 4);
712     if (ioctl(fd, SIOCSIFVJCOMP, x) < 0) {
713         syslog(LOG_ERR, "ioctl(SIOCSIFVJCOMP): %m");
714         return 0;
715     }
716     return 1;
717 }
718
719 /*
720  * sifup - Config the interface up and enable IP packets to pass.
721  */
722 int
723 sifup(u)
724     int u;
725 {
726     struct ifreq ifr;
727     struct npioctl npi;
728
729     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
730     if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
731         syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
732         return 0;
733     }
734     ifr.ifr_flags |= IFF_UP;
735     if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
736         syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
737         return 0;
738     }
739     npi.protocol = PPP_IP;
740     npi.mode = NPMODE_PASS;
741     if (ioctl(fd, SIOCSETNPMODE, &npi) < 0) {
742         if (errno != ENOTTY) {
743             syslog(LOG_ERR, "ioctl(SIOCSETNPMODE): %m");
744             return 0;
745         }
746     }
747
748     return 1;
749 }
750
751 /*
752  * sifdown - Config the interface down.
753  */
754 int
755 sifdown(u)
756     int u;
757 {
758     struct ifreq ifr;
759     int rv;
760     struct npioctl npi;
761
762     rv = 1;
763     npi.protocol = PPP_IP;
764     npi.mode = NPMODE_ERROR;
765     if (ioctl(fd, SIOCSETNPMODE, &npi) < 0) {
766         if (errno != ENOTTY) {
767             syslog(LOG_ERR, "ioctl(SIOCSETNPMODE): %m");
768             rv = 0;
769         }
770     }
771
772     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
773     if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
774         syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
775         rv = 0;
776     } else {
777         ifr.ifr_flags &= ~IFF_UP;
778         if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
779             syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
780             rv = 0;
781         }
782     }
783     return rv;
784 }
785
786 /*
787  * SET_SA_FAMILY - initialize a struct sockaddr, setting the sa_family field.
788  */
789 #define SET_SA_FAMILY(addr, family)             \
790     BZERO((char *) &(addr), sizeof(addr));      \
791     addr.sa_family = (family);
792
793 /*
794  * sifaddr - Config the interface IP addresses and netmask.
795  */
796 int
797 sifaddr(u, o, h, m)
798     int u;
799     u_int32_t o, h, m;
800 {
801     int ret;
802     struct ifreq ifr;
803
804     ret = 1;
805     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
806     SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
807     if (m != 0) {
808         syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
809         ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
810         if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
811             syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
812             ret = 0;
813         }
814     }
815
816     ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
817     if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
818         syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
819         ret = 0;
820     }
821
822     SET_SA_FAMILY(ifr.ifr_dstaddr, AF_INET);
823     ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
824     if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
825         syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
826         ret = 0;
827     }
828     ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
829     if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
830         syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
831         ret = 0;
832     }
833     return ret;
834 }
835
836 /*
837  * cifaddr - Clear the interface IP addresses, and delete routes
838  * through the interface if possible.
839  */
840 int
841 cifaddr(u, o, h)
842     int u;
843     u_int32_t o, h;
844 {
845     struct ortentry rt;
846
847     SET_SA_FAMILY(rt.rt_dst, AF_INET);
848     ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
849     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
850     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
851     rt.rt_flags = RTF_HOST;
852     if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
853         syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
854         return 0;
855     }
856     return 1;
857 }
858
859 /*
860  * sifdefaultroute - assign a default route through the address given.
861  */
862 int
863 sifdefaultroute(u, g)
864     int u;
865     u_int32_t g;
866 {
867     struct ortentry rt;
868
869     SET_SA_FAMILY(rt.rt_dst, AF_INET);
870     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
871     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
872     rt.rt_flags = RTF_GATEWAY;
873     if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
874         syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
875         return 0;
876     }
877     return 1;
878 }
879
880 /*
881  * cifdefaultroute - delete a default route through the address given.
882  */
883 int
884 cifdefaultroute(u, g)
885     int u;
886     u_int32_t g;
887 {
888     struct ortentry rt;
889
890     SET_SA_FAMILY(rt.rt_dst, AF_INET);
891     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
892     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
893     rt.rt_flags = RTF_GATEWAY;
894     if (ioctl(sockfd, SIOCDELRT, &rt) < 0) {
895         syslog(LOG_ERR, "default route ioctl(SIOCDELRT): %m");
896         return 0;
897     }
898     return 1;
899 }
900
901 /*
902  * sifproxyarp - Make a proxy ARP entry for the peer.
903  */
904 int
905 sifproxyarp(unit, hisaddr)
906     int unit;
907     u_int32_t hisaddr;
908 {
909     struct arpreq arpreq;
910
911     BZERO(&arpreq, sizeof(arpreq));
912
913     /*
914      * Get the hardware address of an interface on the same subnet
915      * as our local address.
916      */
917     if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
918         syslog(LOG_WARNING, "Cannot determine ethernet address for proxy ARP");
919         return 0;
920     }
921
922     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
923     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
924     arpreq.arp_flags = ATF_PERM | ATF_PUBL;
925     if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
926         syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
927         return 0;
928     }
929
930     return 1;
931 }
932
933 /*
934  * cifproxyarp - Delete the proxy ARP entry for the peer.
935  */
936 int
937 cifproxyarp(unit, hisaddr)
938     int unit;
939     u_int32_t hisaddr;
940 {
941     struct arpreq arpreq;
942
943     BZERO(&arpreq, sizeof(arpreq));
944     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
945     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
946     if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
947         syslog(LOG_ERR, "ioctl(SIOCDARP): %m");
948         return 0;
949     }
950     return 1;
951 }
952
953 /*
954  * get_ether_addr - get the hardware address of an interface on the
955  * the same subnet as ipaddr.  Code borrowed from myetheraddr.c
956  * in the cslip-2.6 distribution, which is subject to the following
957  * copyright notice (which also applies to logwtmp below):
958  *
959  * Copyright (c) 1990, 1992 The Regents of the University of California.
960  * All rights reserved.
961  *
962  * Redistribution and use in source and binary forms, with or without
963  * modification, are permitted provided that: (1) source code distributions
964  * retain the above copyright notice and this paragraph in its entirety, (2)
965  * distributions including binary code include the above copyright notice and
966  * this paragraph in its entirety in the documentation or other materials
967  * provided with the distribution, and (3) all advertising materials mentioning
968  * features or use of this software display the following acknowledgement:
969  * ``This product includes software developed by the University of California,
970  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
971  * the University nor the names of its contributors may be used to endorse
972  * or promote products derived from this software without specific prior
973  * written permission.
974  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
975  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
976  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
977  */
978
979 #include <nlist.h>
980 #include <arpa/inet.h>
981 #include <netinet/in_var.h>
982 #include <net/if_dl.h>
983
984 /* Cast a struct sockaddr to a structaddr_in */
985 #define SATOSIN(sa) ((struct sockaddr_in *)(sa))
986
987 /* Determine if "bits" is set in "flag" */
988 #define ALLSET(flag, bits) (((flag) & (bits)) == (bits))
989
990 static struct nlist nl[] = {
991 #define N_IFNET 0
992         { "ifnet" },
993         { 0 }
994 };
995
996 int kvm_read(int fd, off_t offset, void *buf, int nbytes)
997 {
998     if (lseek(fd, offset, SEEK_SET) != offset) {
999         syslog(LOG_ERR,"lseek in kmem: %m");
1000         return(0);
1001     }
1002     return(read(fd, buf, nbytes));
1003 }
1004
1005 int
1006 get_ether_addr(ipaddr, hwaddr)
1007     u_long ipaddr;
1008     struct sockaddr *hwaddr;
1009 {
1010     int         kd;
1011     register struct ifnet *ifp;
1012     register struct arpcom *ac;
1013     struct arpcom arpcom;
1014     struct in_addr *inp;
1015     register struct ifaddr *ifa;
1016     register struct in_ifaddr *in;
1017     union {
1018         struct ifaddr ifa;
1019         struct in_ifaddr in;
1020     } ifaddr;
1021     struct ifaddr       *ifap;
1022     struct sockaddr     ifaddrsaddr;
1023     struct sockaddr_in  ifmasksaddr;
1024     struct sockaddr_in  *ifinetptr;
1025     struct sockaddr_dl  *iflinkptr;
1026     u_long addr, mask, ifip;
1027     int         found = 0;
1028
1029 /* Open kernel memory for reading */
1030     if ((kd = open("/dev/kmem", O_RDONLY)) < 0) {
1031         syslog(LOG_ERR, "/dev/kmem: %m");
1032         return 0;
1033     }
1034
1035 /* Fetch namelist */
1036     if (nlist("/unix", nl) != 0) {
1037         syslog(LOG_ERR, "nlist(): %m");
1038         return 0;
1039     }
1040
1041     ac = &arpcom;
1042     ifp = &arpcom.ac_if;
1043     ifa = &ifaddr.ifa;
1044     in = &ifaddr.in;
1045
1046     if (kvm_read(kd, nl[N_IFNET].n_value, (char *)&addr, sizeof(addr))
1047         != sizeof(addr)) {
1048         syslog(LOG_ERR, "error reading ifnet addr");
1049         return 0;
1050     }
1051     for ( ; addr && !found; addr = (u_long)ifp->if_next) {
1052         if (kvm_read(kd, addr, (char *)ac, sizeof(*ac)) != sizeof(*ac)) {
1053             syslog(LOG_ERR, "error reading ifnet");
1054             return 0;
1055         }
1056
1057 /* Only look at configured, broadcast interfaces */
1058         if (!ALLSET(ifp->if_flags, IFF_UP | IFF_BROADCAST))
1059             continue;
1060 /* This probably can't happen... */
1061         if (ifp->if_addrlist == 0)
1062             continue;
1063
1064 /* Get interface ip address */
1065         for (ifap = ifp->if_addrlist; ifap; ifap=ifaddr.ifa.ifa_next) {
1066             if (kvm_read(kd, (u_long)ifap, (char *)&ifaddr,
1067                      sizeof(ifaddr)) != sizeof(ifaddr)) {
1068                 syslog(LOG_ERR, "error reading ifaddr");
1069                 return 0;
1070             }
1071             if (kvm_read(kd, (u_long)ifaddr.ifa.ifa_addr, &ifaddrsaddr,
1072                 sizeof(struct sockaddr_in)) != sizeof(struct sockaddr_in)) {
1073                 syslog(LOG_ERR, "error reading ifaddrsaddr");
1074                 return(0);
1075             }
1076             if (kvm_read(kd, (u_long)ifaddr.ifa.ifa_netmask, &ifmasksaddr,
1077                 sizeof(struct sockaddr_in)) != sizeof(struct sockaddr_in)) {
1078                 syslog(LOG_ERR, "error reading ifmasksaddr");
1079                 return(0);
1080             }
1081     /* Check if this interface on the right subnet */
1082             switch (ifaddrsaddr.sa_family) {
1083             case AF_LINK :
1084                 hwaddr->sa_family = AF_UNSPEC;
1085                 iflinkptr = (struct sockaddr_dl *) &ifaddrsaddr;
1086                 bcopy(LLADDR(iflinkptr),hwaddr->sa_data,iflinkptr->sdl_alen);
1087                 break;
1088             case AF_INET:
1089                 ifinetptr= (struct sockaddr_in *) &ifaddrsaddr;
1090                 ifip = ifinetptr->sin_addr.s_addr;
1091                 if (kvm_read(kd, (u_long)ifaddr.ifa.ifa_netmask, &ifmasksaddr,
1092                     sizeof(struct sockaddr_in)) != sizeof(struct sockaddr_in)) {
1093                     syslog(LOG_ERR, "error reading ifmasksaddr");
1094                     return(0);
1095                 }
1096                 mask = ifmasksaddr.sin_addr.s_addr;
1097                 if ((ipaddr & mask) == (ifip & mask))
1098                     ++found;
1099                 break;
1100             default:
1101                 break;
1102             }
1103    
1104         }
1105     }
1106     return(found);
1107 }
1108
1109 /*
1110  * Return user specified netmask, modified by any mask we might determine
1111  * for address `addr' (in network byte order).
1112  * Here we scan through the system's list of interfaces, looking for
1113  * any non-point-to-point interfaces which might appear to be on the same
1114  * network as `addr'.  If we find any, we OR in their netmask to the
1115  * user-specified netmask.
1116  */
1117 u_int32_t
1118 GetMask(addr)
1119     u_int32_t addr;
1120 {
1121     u_int32_t mask, nmask, ina;
1122     struct ifreq *ifr, *ifend, ifreq;
1123     struct ifconf ifc;
1124     struct ifreq ifs[MAX_IFS];
1125
1126     addr = ntohl(addr);
1127     if (IN_CLASSA(addr))        /* determine network mask for address class */
1128         nmask = IN_CLASSA_NET;
1129     else if (IN_CLASSB(addr))
1130         nmask = IN_CLASSB_NET;
1131     else
1132         nmask = IN_CLASSC_NET;
1133     /* class D nets are disallowed by bad_ip_adrs */
1134     mask = netmask | htonl(nmask);
1135
1136     /*
1137      * Scan through the system's network interfaces.
1138      */
1139     ifc.ifc_len = sizeof(ifs);
1140     ifc.ifc_req = ifs;
1141     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1142         syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1143         return mask;
1144     }
1145     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1146     for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1147                 ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len)) {
1148         /*
1149          * Check the interface's internet address.
1150          */
1151         if (ifr->ifr_addr.sa_family != AF_INET)
1152             continue;
1153         ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1154         if ((ntohl(ina) & nmask) != (addr & nmask))
1155             continue;
1156         /*
1157          * Check that the interface is up, and not point-to-point or loopback.
1158          */
1159         strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1160         if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1161             continue;
1162         if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1163             != IFF_UP)
1164             continue;
1165         /*
1166          * Get its netmask and OR it into our mask.
1167          */
1168         if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1169             continue;
1170         mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1171     }
1172
1173     return mask;
1174 }
1175
1176 #define WTMPFILE        "/var/adm/wtmp"
1177
1178 int
1179 logwtmp(line, name, host)
1180     char *line, *name, *host;
1181 {
1182     int fd;
1183     struct stat buf;
1184     struct utmp ut;
1185
1186     if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1187         return;
1188     if (!fstat(fd, &buf)) {
1189         (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1190         (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1191         (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1192         (void)time(&ut.ut_time);
1193         if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1194             (void)ftruncate(fd, buf.st_size);
1195     }
1196     close(fd);
1197 }
1198
1199 /*
1200  * Code for locking/unlocking the serial device.
1201  */
1202
1203 static  char *devlocked = (char *) 0;
1204
1205 int lock(char *device)
1206 {
1207     char        *devname;
1208     int         rc;
1209
1210     if (devname = strrchr(device,'/'))
1211         ++devname;
1212     else
1213         devname = device;
1214
1215     if ((rc = ttylock(devname)) == 0) {
1216         devlocked = (char *) malloc(strlen(devname) + 1);
1217         sprintf(devlocked,"%s",devname);
1218     } else
1219         devlocked = (char *) 0;
1220
1221     return(rc);
1222 }
1223
1224 int unlock()
1225 {
1226     int rc = 0;
1227
1228     if (devlocked) {
1229         rc = ttyunlock(devlocked);
1230         free(devlocked);
1231         devlocked = (char *) 0;
1232     }
1233     return(rc);
1234 }
1235