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