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