]> git.ozlabs.org Git - ppp.git/blob - pppd/sys-svr4.c
Send a HUP to the terminal's controlling process if we get EOF.
[ppp.git] / pppd / sys-svr4.c
1 /*
2  * System-dependent procedures for pppd under Solaris 2.
3  *
4  * Copyright (c) 1994 The Australian National University.
5  * All rights reserved.
6  *
7  * Permission to use, copy, modify, and distribute this software and its
8  * documentation is hereby granted, provided that the above copyright
9  * notice appears in all copies.  This software is provided without any
10  * warranty, express or implied. The Australian National University
11  * makes no representations about the suitability of this software for
12  * any purpose.
13  *
14  * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
15  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
17  * THE AUSTRALIAN NATIONAL UNIVERSITY HAVE BEEN ADVISED OF THE POSSIBILITY
18  * OF SUCH DAMAGE.
19  *
20  * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
21  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
23  * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
24  * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
25  * OR MODIFICATIONS.
26  */
27
28 #ifndef lint
29 static char rcsid[] = "$Id: sys-svr4.c,v 1.4 1995/08/10 06:53:39 paulus Exp $";
30 #endif
31
32 #include <stdio.h>
33 #include <stddef.h>
34 #include <stdlib.h>
35 #include <ctype.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39 #include <termios.h>
40 #include <signal.h>
41 #include <sys/types.h>
42 #include <sys/ioccom.h>
43 #include <sys/stream.h>
44 #include <sys/stropts.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
47 #include <sys/syslog.h>
48 #include <sys/systeminfo.h>
49 #include <sys/dlpi.h>
50 #include <sys/stat.h>
51 #include <net/if.h>
52 #include <net/if_arp.h>
53 #include <net/route.h>
54 #include <net/ppp_defs.h>
55 #include <net/pppio.h>
56 #include <netinet/in.h>
57
58 #include "pppd.h"
59
60 static int      pppfd;
61 static int      fdmuxid = -1;
62 static int      ipfd;
63 static int      ipmuxid = -1;
64
65 static int      restore_term;
66 static struct termios inittermios;
67 static pid_t    tty_sid;        /* original session ID for terminal */
68
69 static int      link_mtu, link_mru;
70
71 #define NMODULES        32
72 static int      tty_nmodules;
73 static char     tty_modules[NMODULES][FMNAMESZ+1];
74
75 /*
76  * sys_init - System-dependent initialization.
77  */
78 void
79 sys_init()
80 {
81     openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
82     setlogmask(LOG_UPTO(LOG_INFO));
83     if (debug)
84         setlogmask(LOG_UPTO(LOG_DEBUG));
85
86     ipfd = open("/dev/ip", O_RDWR, 0);
87     if (ipfd < 0) {
88         syslog(LOG_ERR, "Couldn't open IP device: %m");
89         die(1);
90     }
91 }
92
93 /*
94  * daemon - Detach us from controlling terminal session.
95  */
96 int
97 daemon(nochdir, noclose)
98     int nochdir, noclose;
99 {
100     int pid;
101
102     if ((pid = fork()) < 0)
103         return -1;
104     if (pid != 0)
105         exit(0);                /* parent dies */
106     setsid();
107     if (!nochdir)
108         chdir("/");
109     if (!noclose) {
110         fclose(stdin);          /* don't need stdin, stdout, stderr */
111         fclose(stdout);
112         fclose(stderr);
113     }
114     return 0;
115 }
116
117 /*
118  * note_debug_level - note a change in the debug level.
119  */
120 void
121 note_debug_level()
122 {
123     if (debug) {
124         syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
125         setlogmask(LOG_UPTO(LOG_DEBUG));
126     } else {
127         setlogmask(LOG_UPTO(LOG_WARNING));
128     }
129 }
130
131 /*
132  * ppp_available - check whether the system has any ppp interfaces
133  */
134 int
135 ppp_available()
136 {
137     struct stat buf;
138
139     return stat("/dev/ppp", &buf) >= 0;
140 }
141
142 /*
143  * establish_ppp - Turn the serial port into a ppp interface.
144  */
145 void
146 establish_ppp()
147 {
148     int i, ifd;
149
150     if (default_device)
151         tty_sid = getsid((pid_t)0);
152
153     pppfd = open("/dev/ppp", O_RDWR | O_NONBLOCK, 0);
154     if (pppfd < 0) {
155         syslog(LOG_ERR, "Can't open /dev/ppp: %m");
156         die(1);
157     }
158
159     /* Assign a new PPA and get its unit number. */
160     if (strioctl(pppfd, PPPIO_NEWPPA, &ifunit, 0, sizeof(int)) < 0) {
161         syslog(LOG_ERR, "Can't create new PPP interface: %m");
162         die(1);
163     }
164
165     /*
166      * Open the ppp device again and link it under the ip multiplexor.
167      * IP will assign a unit number which hopefully is the same as ifunit.
168      * I don't know any way to be certain they will be the same. :-(
169      */
170     ifd = open("/dev/ppp", O_RDWR, 0);
171     if (ifd < 0) {
172         syslog(LOG_ERR, "Can't open /dev/ppp (2): %m");
173         die(1);
174     }
175     if (ioctl(ifd, I_PUSH, "ip") < 0) {
176         syslog(LOG_ERR, "Can't push IP module: %m");
177         close(ifd);
178         die(1);
179     }
180     ipmuxid = ioctl(ipfd, I_LINK, ifd);
181     close(ifd);
182     if (ipmuxid < 0) {
183         syslog(LOG_ERR, "Can't link PPP device to IP: %m");
184         die(1);
185     }
186
187     /* Pop any existing modules off the tty stream. */
188     for (i = 0;; ++i)
189         if (ioctl(fd, I_LOOK, tty_modules[i]) < 0
190             || ioctl(fd, I_POP, 0) < 0)
191             break;
192     tty_nmodules = i;
193
194     /* Push the async hdlc module and the compressor module. */
195     if (ioctl(fd, I_PUSH, "ppp_ahdl") < 0) {
196         syslog(LOG_ERR, "Couldn't push PPP Async HDLC module: %m");
197         die(1);
198     }
199     if (ioctl(fd, I_PUSH, "ppp_comp") < 0) {
200         syslog(LOG_ERR, "Couldn't push PPP compression module: %m");
201 /*      die(1); */
202     }
203
204     /* Link the serial port under the PPP multiplexor. */
205     if ((fdmuxid = ioctl(pppfd, I_LINK, fd)) < 0) {
206         syslog(LOG_ERR, "Can't link tty to PPP mux: %m");
207         die(1);
208     }
209 }
210
211 /*
212  * disestablish_ppp - Restore the serial port to normal operation.
213  * This shouldn't call die() because it's called from die().
214  */
215 void
216 disestablish_ppp()
217 {
218     int i;
219
220     if (fdmuxid >= 0) {
221         if (ioctl(pppfd, I_UNLINK, fdmuxid) < 0) {
222             if (!hungup)
223                 syslog(LOG_ERR, "Can't unlink tty from PPP mux: %m");
224         }
225         fdmuxid = -1;
226
227         if (!hungup) {
228             while (ioctl(fd, I_POP, 0) >= 0)
229                 ;
230             for (i = tty_nmodules - 1; i >= 0; --i)
231                 if (ioctl(fd, I_PUSH, tty_modules[i]) < 0)
232                     syslog(LOG_ERR, "Couldn't restore tty module %s: %m",
233                            tty_modules[i]);
234         }
235         if (hungup && default_device && tty_sid > 0) {
236             /*
237              * If we have received a hangup, we need to send a SIGHUP
238              * to the terminal's controlling process.  The reason is
239              * that the original stream head for the terminal hasn't
240              * seen the M_HANGUP message (it went up through the ppp
241              * driver to the stream head for our fd to /dev/ppp).
242              */
243             kill(tty_sid, SIGHUP);
244         }
245     }
246 }
247
248 /*
249  * List of valid speeds.
250  */
251 struct speed {
252     int speed_int, speed_val;
253 } speeds[] = {
254 #ifdef B50
255     { 50, B50 },
256 #endif
257 #ifdef B75
258     { 75, B75 },
259 #endif
260 #ifdef B110
261     { 110, B110 },
262 #endif
263 #ifdef B134
264     { 134, B134 },
265 #endif
266 #ifdef B150
267     { 150, B150 },
268 #endif
269 #ifdef B200
270     { 200, B200 },
271 #endif
272 #ifdef B300
273     { 300, B300 },
274 #endif
275 #ifdef B600
276     { 600, B600 },
277 #endif
278 #ifdef B1200
279     { 1200, B1200 },
280 #endif
281 #ifdef B1800
282     { 1800, B1800 },
283 #endif
284 #ifdef B2000
285     { 2000, B2000 },
286 #endif
287 #ifdef B2400
288     { 2400, B2400 },
289 #endif
290 #ifdef B3600
291     { 3600, B3600 },
292 #endif
293 #ifdef B4800
294     { 4800, B4800 },
295 #endif
296 #ifdef B7200
297     { 7200, B7200 },
298 #endif
299 #ifdef B9600
300     { 9600, B9600 },
301 #endif
302 #ifdef B19200
303     { 19200, B19200 },
304 #endif
305 #ifdef B38400
306     { 38400, B38400 },
307 #endif
308 #ifdef EXTA
309     { 19200, EXTA },
310 #endif
311 #ifdef EXTB
312     { 38400, EXTB },
313 #endif
314 #ifdef B57600
315     { 57600, B57600 },
316 #endif
317 #ifdef B115200
318     { 115200, B115200 },
319 #endif
320     { 0, 0 }
321 };
322
323 /*
324  * Translate from bits/second to a speed_t.
325  */
326 int
327 translate_speed(bps)
328     int bps;
329 {
330     struct speed *speedp;
331
332     if (bps == 0)
333         return 0;
334     for (speedp = speeds; speedp->speed_int; speedp++)
335         if (bps == speedp->speed_int)
336             return speedp->speed_val;
337     syslog(LOG_WARNING, "speed %d not supported", bps);
338     return 0;
339 }
340
341 /*
342  * Translate from a speed_t to bits/second.
343  */
344 int
345 baud_rate_of(speed)
346     int speed;
347 {
348     struct speed *speedp;
349
350     if (speed == 0)
351         return 0;
352     for (speedp = speeds; speedp->speed_int; speedp++)
353         if (speed == speedp->speed_val)
354             return speedp->speed_int;
355     return 0;
356 }
357
358 /*
359  * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
360  * at the requested speed, etc.  If `local' is true, set CLOCAL
361  * regardless of whether the modem option was specified.
362  */
363 set_up_tty(fd, local)
364     int fd, local;
365 {
366     int speed;
367     struct termios tios;
368
369     if (tcgetattr(fd, &tios) < 0) {
370         syslog(LOG_ERR, "tcgetattr: %m");
371         die(1);
372     }
373
374     if (!restore_term)
375         inittermios = tios;
376
377     tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
378     if (crtscts > 0)
379         tios.c_cflag |= CRTSCTS;
380     else if (crtscts < 0)
381         tios.c_cflag &= ~CRTSCTS;
382
383     tios.c_cflag |= CS8 | CREAD | HUPCL;
384     if (local || !modem)
385         tios.c_cflag |= CLOCAL;
386     tios.c_iflag = IGNBRK | IGNPAR;
387     tios.c_oflag = 0;
388     tios.c_lflag = 0;
389     tios.c_cc[VMIN] = 1;
390     tios.c_cc[VTIME] = 0;
391
392     if (crtscts == 2) {
393         tios.c_iflag |= IXOFF;
394         tios.c_cc[VSTOP] = 0x13;        /* DC3 = XOFF = ^S */
395         tios.c_cc[VSTART] = 0x11;       /* DC1 = XON  = ^Q */
396     }
397
398     speed = translate_speed(inspeed);
399     if (speed) {
400         cfsetospeed(&tios, speed);
401         cfsetispeed(&tios, speed);
402     } else {
403         speed = cfgetospeed(&tios);
404         /*
405          * We can't proceed if the serial port speed is 0,
406          * since that implies that the serial port is disabled.
407          */
408         if (speed == B0) {
409             syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
410                    devnam);
411             die(1);
412         }
413     }
414
415     if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
416         syslog(LOG_ERR, "tcsetattr: %m");
417         die(1);
418     }
419
420     baud_rate = inspeed = baud_rate_of(speed);
421     restore_term = 1;
422 }
423
424 /*
425  * restore_tty - restore the terminal to the saved settings.
426  */
427 void
428 restore_tty()
429 {
430     if (restore_term) {
431         if (!default_device) {
432             /*
433              * Turn off echoing, because otherwise we can get into
434              * a loop with the tty and the modem echoing to each other.
435              * We presume we are the sole user of this tty device, so
436              * when we close it, it will revert to its defaults anyway.
437              */
438             inittermios.c_lflag &= ~(ECHO | ECHONL);
439         }
440         if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
441             if (!hungup && errno != ENXIO)
442                 syslog(LOG_WARNING, "tcsetattr: %m");
443         restore_term = 0;
444     }
445 }
446
447 /*
448  * setdtr - control the DTR line on the serial port.
449  * This is called from die(), so it shouldn't call die().
450  */
451 setdtr(fd, on)
452 int fd, on;
453 {
454     int modembits = TIOCM_DTR;
455
456     ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
457 }
458
459 /*
460  * output - Output PPP packet.
461  */
462 void
463 output(unit, p, len)
464     int unit;
465     u_char *p;
466     int len;
467 {
468     struct strbuf data;
469
470     if (debug)
471         log_packet(p, len, "sent ");
472
473     data.len = len;
474     data.buf = (caddr_t) p;
475     if (putmsg(pppfd, NULL, &data, 0) < 0) {
476         if (errno != ENXIO) {
477             syslog(LOG_ERR, "Couldn't send packet: %m");
478             die(1);
479         }
480     }
481 }
482
483 /*
484  * wait_input - wait until there is data available on fd,
485  * for the length of time specified by *timo (indefinite
486  * if timo is NULL).
487  */
488 wait_input(timo)
489     struct timeval *timo;
490 {
491     int t;
492     struct pollfd pfd;
493
494     t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
495     pfd.fd = pppfd;
496     pfd.events = POLLIN | POLLPRI | POLLHUP;
497     if (poll(&pfd, 1, t) < 0 && errno != EINTR) {
498         syslog(LOG_ERR, "poll: %m");
499         die(1);
500     }
501 }
502
503 /*
504  * read_packet - get a PPP packet from the serial device.
505  */
506 int
507 read_packet(buf)
508     u_char *buf;
509 {
510     struct strbuf ctrl, data;
511     int flags, len;
512     unsigned char ctrlbuf[sizeof(union DL_primitives) + 64];
513
514     for (;;) {
515         data.maxlen = PPP_MRU + PPP_HDRLEN;
516         data.buf = (caddr_t) buf;
517         ctrl.maxlen = sizeof(ctrlbuf);
518         ctrl.buf = (caddr_t) ctrlbuf;
519         flags = 0;
520         len = getmsg(pppfd, &ctrl, &data, &flags);
521         if (len < 0) {
522             if (errno = EAGAIN || errno == EINTR)
523                 return -1;
524             syslog(LOG_ERR, "Error reading packet: %m");
525             die(1);
526         }
527
528         if (ctrl.len <= 0)
529             return data.len;
530
531         /*
532          * Got a M_PROTO or M_PCPROTO message.  Interpret it
533          * as a DLPI primitive.
534          */
535         if (debug)
536             syslog(LOG_DEBUG, "got dlpi prim 0x%x, len=%d",
537                    ((union DL_primitives *)ctrlbuf)->dl_primitive, ctrl.len);
538
539     }
540 }
541
542 /*
543  * ppp_send_config - configure the transmit characteristics of
544  * the ppp interface.
545  */
546 void
547 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
548     int unit, mtu;
549     u_int32_t asyncmap;
550     int pcomp, accomp;
551 {
552     int cf[2];
553
554     link_mtu = mtu;
555     if (strioctl(pppfd, PPPIO_MTU, &mtu, sizeof(mtu), 0) < 0) {
556         if (hungup && errno == ENXIO)
557             return;
558         syslog(LOG_ERR, "Couldn't set MTU: %m");
559     }
560     if (strioctl(pppfd, PPPIO_XACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
561         syslog(LOG_ERR, "Couldn't set transmit ACCM: %m");
562     }
563     cf[0] = (pcomp? COMP_PROT: 0) + (accomp? COMP_AC: 0);
564     cf[1] = COMP_PROT | COMP_AC;
565     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
566         syslog(LOG_ERR, "Couldn't set prot/AC compression: %m");
567     }
568 }
569
570 /*
571  * ppp_set_xaccm - set the extended transmit ACCM for the interface.
572  */
573 void
574 ppp_set_xaccm(unit, accm)
575     int unit;
576     ext_accm accm;
577 {
578     if (strioctl(pppfd, PPPIO_XACCM, accm, sizeof(ext_accm), 0) < 0) {
579         if (!hungup || errno != ENXIO)
580             syslog(LOG_WARNING, "Couldn't set extended ACCM: %m");
581     }
582 }
583
584 /*
585  * ppp_recv_config - configure the receive-side characteristics of
586  * the ppp interface.
587  */
588 void
589 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
590     int unit, mru;
591     u_int32_t asyncmap;
592     int pcomp, accomp;
593 {
594     int cf[2];
595
596     link_mru = mru;
597     if (strioctl(pppfd, PPPIO_MRU, &mru, sizeof(mru), 0) < 0) {
598         if (hungup && errno == ENXIO)
599             return;
600         syslog(LOG_ERR, "Couldn't set MRU: %m");
601     }
602     if (strioctl(pppfd, PPPIO_RACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
603         syslog(LOG_ERR, "Couldn't set receive ACCM: %m");
604     }
605     cf[0] = (pcomp? DECOMP_PROT: 0) + (accomp? DECOMP_AC: 0);
606     cf[1] = DECOMP_PROT | DECOMP_AC;
607     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
608         syslog(LOG_ERR, "Couldn't set prot/AC decompression: %m");
609     }
610 }
611
612 /*
613  * ccp_test - ask kernel whether a given compression method
614  * is acceptable for use.
615  */
616 ccp_test(unit, opt_ptr, opt_len, for_transmit)
617     int unit, opt_len, for_transmit;
618     u_char *opt_ptr;
619 {
620     return strioctl(pppfd, (for_transmit? PPPIO_XCOMP: PPPIO_RCOMP),
621                     opt_ptr, opt_len, 0) >= 0;
622 }
623
624 /*
625  * ccp_flags_set - inform kernel about the current state of CCP.
626  */
627 void
628 ccp_flags_set(unit, isopen, isup)
629     int unit, isopen, isup;
630 {
631     int cf[2];
632
633     cf[0] = (isopen? CCP_ISOPEN: 0) + (isup? CCP_ISUP: 0);
634     cf[1] = CCP_ISOPEN | CCP_ISUP | CCP_ERROR | CCP_FATALERROR;
635     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
636         if (!hungup || errno != ENXIO)
637             syslog(LOG_ERR, "Couldn't set kernel CCP state: %m");
638     }
639 }
640
641 /*
642  * ccp_fatal_error - returns 1 if decompression was disabled as a
643  * result of an error detected after decompression of a packet,
644  * 0 otherwise.  This is necessary because of patent nonsense.
645  */
646 int
647 ccp_fatal_error(unit)
648     int unit;
649 {
650     int cf[2];
651
652     cf[0] = cf[1] = 0;
653     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
654         if (errno != ENXIO && errno != EINVAL)
655             syslog(LOG_ERR, "Couldn't get compression flags: %m");
656         return 0;
657     }
658     return cf[0] & CCP_FATALERROR;
659 }
660
661 /*
662  * sifvjcomp - config tcp header compression
663  */
664 int
665 sifvjcomp(u, vjcomp, xcidcomp, xmaxcid)
666     int u, vjcomp, xcidcomp, xmaxcid;
667 {
668     int cf[2];
669     char maxcid[2];
670
671     if (vjcomp) {
672         maxcid[0] = xcidcomp;
673         maxcid[1] = 15;         /* XXX should be rmaxcid */
674         if (strioctl(pppfd, PPPIO_VJINIT, maxcid, sizeof(maxcid), 0) < 0) {
675             syslog(LOG_ERR, "Couldn't initialize VJ compression: %m");
676         }
677     }
678
679     cf[0] = (vjcomp? COMP_VJC + DECOMP_VJC: 0)  /* XXX this is wrong */
680         + (xcidcomp? COMP_VJCCID + DECOMP_VJCCID: 0);
681     cf[1] = COMP_VJC + DECOMP_VJC + COMP_VJCCID + DECOMP_VJCCID;
682     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
683         if (vjcomp)
684             syslog(LOG_ERR, "Couldn't enable VJ compression: %m");
685     }
686
687     return 1;
688 }
689
690 /*
691  * sifup - Config the interface up and enable IP packets to pass.
692  */
693 int
694 sifup(u)
695     int u;
696 {
697     struct ifreq ifr;
698
699     strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
700     if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) < 0) {
701         syslog(LOG_ERR, "Couldn't mark interface up (get): %m");
702         return 0;
703     }
704     ifr.ifr_flags |= IFF_UP;
705     if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) {
706         syslog(LOG_ERR, "Couldn't mark interface up (set): %m");
707         return 0;
708     }
709     return 1;
710 }
711
712 /*
713  * sifdown - Config the interface down and disable IP.
714  */
715 int
716 sifdown(u)
717     int u;
718 {
719     struct ifreq ifr;
720
721     if (ipmuxid < 0)
722         return 1;
723     strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
724     if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) < 0) {
725         syslog(LOG_ERR, "Couldn't mark interface down (get): %m");
726         return 0;
727     }
728     ifr.ifr_flags &= ~IFF_UP;
729     if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) {
730         syslog(LOG_ERR, "Couldn't mark interface down (set): %m");
731         return 0;
732     }
733     return 1;
734 }
735
736 #define INET_ADDR(x)    (((struct sockaddr_in *) &(x))->sin_addr.s_addr)
737
738 /*
739  * sifaddr - Config the interface IP addresses and netmask.
740  */
741 int
742 sifaddr(u, o, h, m)
743     int u;
744     u_int32_t o, h, m;
745 {
746     struct ifreq ifr;
747
748     memset(&ifr, 0, sizeof(ifr));
749     strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
750     ifr.ifr_addr.sa_family = AF_INET;
751     INET_ADDR(ifr.ifr_addr) = o;
752     if (ioctl(ipfd, SIOCSIFADDR, &ifr) < 0) {
753         syslog(LOG_ERR, "Couldn't set local IP address: %m");
754     }
755     ifr.ifr_dstaddr.sa_family = AF_INET;
756     INET_ADDR(ifr.ifr_dstaddr) = h;
757     if (ioctl(ipfd, SIOCSIFDSTADDR, &ifr) < 0) {
758         syslog(LOG_ERR, "Couldn't set remote IP address: %m");
759     }
760     ifr.ifr_addr.sa_family = AF_INET;
761     INET_ADDR(ifr.ifr_addr) = m;
762     if (ioctl(ipfd, SIOCSIFNETMASK, &ifr) < 0) {
763         syslog(LOG_ERR, "Couldn't set IP netmask: %m");
764     }
765     ifr.ifr_metric = link_mtu;
766     if (ioctl(ipfd, SIOCSIFMTU, &ifr) < 0) {
767         syslog(LOG_ERR, "Couldn't set IP MTU: %m");
768     }
769
770     return 1;
771 }
772
773 /*
774  * cifaddr - Clear the interface IP addresses, and delete routes
775  * through the interface if possible.
776  */
777 int
778 cifaddr(u, o, h)
779     int u;
780     u_int32_t o, h;
781 {
782 #if 0
783     if (ipmuxid >= 0) {
784         if (ioctl(ipfd, I_UNLINK, ipmuxid) < 0) {
785             syslog(LOG_ERR, "Can't remove ppp interface unit: %m");
786             return 0;
787         }
788         ipmuxid = -1;
789     }
790 #endif
791     return 1;
792 }
793
794 /*
795  * sifdefaultroute - assign a default route through the address given.
796  */
797 int
798 sifdefaultroute(u, g)
799     int u;
800     u_int32_t g;
801 {
802     struct rtentry rt;
803
804     rt.rt_dst.sa_family = AF_INET;
805     INET_ADDR(rt.rt_dst) = 0;
806     rt.rt_gateway.sa_family = AF_INET;
807     INET_ADDR(rt.rt_gateway) = g;
808     rt.rt_flags = RTF_GATEWAY;
809
810     if (ioctl(ipfd, SIOCADDRT, &rt) < 0) {
811         syslog(LOG_ERR, "Can't add default route: %m");
812         return 0;
813     }
814
815     return 1;
816 }
817
818 /*
819  * cifdefaultroute - delete a default route through the address given.
820  */
821 int
822 cifdefaultroute(u, g)
823     int u;
824     u_int32_t g;
825 {
826     struct rtentry rt;
827
828     rt.rt_dst.sa_family = AF_INET;
829     INET_ADDR(rt.rt_dst) = 0;
830     rt.rt_gateway.sa_family = AF_INET;
831     INET_ADDR(rt.rt_gateway) = g;
832     rt.rt_flags = RTF_GATEWAY;
833
834     if (ioctl(ipfd, SIOCDELRT, &rt) < 0) {
835         syslog(LOG_ERR, "Can't delete default route: %m");
836         return 0;
837     }
838
839     return 1;
840 }
841
842 /*
843  * sifproxyarp - Make a proxy ARP entry for the peer.
844  */
845 int
846 sifproxyarp(unit, hisaddr)
847     int unit;
848     u_int32_t hisaddr;
849 {
850     struct arpreq arpreq;
851
852     memset(&arpreq, 0, sizeof(arpreq));
853     if (!get_ether_addr(hisaddr, &arpreq.arp_ha))
854         return 0;
855
856     arpreq.arp_pa.sa_family = AF_INET;
857     INET_ADDR(arpreq.arp_pa) = hisaddr;
858     arpreq.arp_flags = ATF_PERM | ATF_PUBL;
859     if (ioctl(ipfd, SIOCSARP, (caddr_t) &arpreq) < 0) {
860         syslog(LOG_ERR, "Couldn't set proxy ARP entry: %m");
861         return 0;
862     }
863
864     return 1;
865 }
866
867 /*
868  * cifproxyarp - Delete the proxy ARP entry for the peer.
869  */
870 int
871 cifproxyarp(unit, hisaddr)
872     int unit;
873     u_int32_t hisaddr;
874 {
875     struct arpreq arpreq;
876
877     memset(&arpreq, 0, sizeof(arpreq));
878     arpreq.arp_pa.sa_family = AF_INET;
879     INET_ADDR(arpreq.arp_pa) = hisaddr;
880     if (ioctl(ipfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
881         syslog(LOG_ERR, "Couldn't delete proxy ARP entry: %m");
882         return 0;
883     }
884
885     return 1;
886 }
887
888 /*
889  * get_ether_addr - get the hardware address of an interface on the
890  * the same subnet as ipaddr.
891  */
892 #define MAX_IFS         32
893
894 int
895 get_ether_addr(ipaddr, hwaddr)
896     u_int32_t ipaddr;
897     struct sockaddr *hwaddr;
898 {
899     struct ifreq *ifr, *ifend, ifreq;
900     int nif;
901     struct ifconf ifc;
902     u_int32_t ina, mask;
903
904     /*
905      * Scan through the system's network interfaces.
906      */
907     if (ioctl(ipfd, SIOCGIFNUM, &nif) < 0)
908         nif = MAX_IFS;
909     ifc.ifc_len = nif * sizeof(struct ifreq);
910     ifc.ifc_req = alloca(ifc.ifc_len);
911     if (ifc.ifc_req == 0)
912         return 0;
913     if (ioctl(ipfd, SIOCGIFCONF, &ifc) < 0) {
914         syslog(LOG_WARNING, "Couldn't get system interface list: %m");
915         return 0;
916     }
917     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
918     for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
919         if (ifr->ifr_addr.sa_family != AF_INET)
920             continue;
921         /*
922          * Check that the interface is up, and not point-to-point or loopback.
923          */
924         strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
925         if (ioctl(ipfd, SIOCGIFFLAGS, &ifreq) < 0)
926             continue;
927         if ((ifreq.ifr_flags &
928              (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
929             != (IFF_UP|IFF_BROADCAST))
930             continue;
931         /*
932          * Get its netmask and check that it's on the right subnet.
933          */
934         if (ioctl(ipfd, SIOCGIFNETMASK, &ifreq) < 0)
935             continue;
936         ina = INET_ADDR(ifr->ifr_addr);
937         mask = INET_ADDR(ifreq.ifr_addr);
938         if ((ipaddr & mask) == (ina & mask))
939             break;
940     }
941
942     if (ifr >= ifend) {
943         syslog(LOG_WARNING, "No suitable interface found for proxy ARP");
944         return 0;
945     }
946
947     syslog(LOG_INFO, "found interface %s for proxy ARP", ifr->ifr_name);
948     if (!get_hw_addr(ifr->ifr_name, hwaddr)) {
949         syslog(LOG_ERR, "Couldn't get hardware address for %s", ifr->ifr_name);
950         return 0;
951     }
952
953     return 1;
954 }
955
956 /*
957  * get_hw_addr - obtain the hardware address for a named interface.
958  */
959 int
960 get_hw_addr(name, hwaddr)
961     char *name;
962     struct sockaddr *hwaddr;
963 {
964     char *p, *q;
965     int unit, iffd, adrlen;
966     unsigned char *adrp;
967     char ifdev[24];
968     struct {
969         union DL_primitives prim;
970         char space[64];
971     } reply;
972
973     /*
974      * We have to open the device and ask it for its hardware address.
975      * First split apart the device name and unit.
976      */
977     strcpy(ifdev, "/dev/");
978     q = ifdev + 5;              /* strlen("/dev/") */
979     while (*name != 0 && !isdigit(*name))
980         *q++ = *name++;
981     *q = 0;
982     unit = atoi(name);
983
984     /*
985      * Open the device and do a DLPI attach and phys_addr_req.
986      */
987     iffd = open(ifdev, O_RDWR);
988     if (iffd < 0) {
989         syslog(LOG_ERR, "Can't open %s: %m", ifdev);
990         return 0;
991     }
992     if (dlpi_attach(iffd, unit) < 0
993         || dlpi_get_reply(iffd, &reply.prim, DL_OK_ACK, sizeof(reply)) < 0
994         || dlpi_phys_addr_req(iffd) < 0
995         || dlpi_get_reply(iffd, &reply.prim, DL_PHYS_ADDR_ACK,
996                           sizeof(reply)) < 0) {
997         close(iffd);
998         return 0;
999     }
1000
1001     hwaddr->sa_family = AF_UNSPEC;
1002     adrlen = reply.prim.physaddr_ack.dl_addr_length;
1003     adrp = (unsigned char *)&reply + reply.prim.physaddr_ack.dl_addr_offset;
1004     memcpy(hwaddr->sa_data, adrp, adrlen);
1005
1006     return 1;
1007 }
1008
1009 int
1010 dlpi_attach(fd, ppa)
1011     int fd, ppa;
1012 {
1013     dl_attach_req_t req;
1014     struct strbuf buf;
1015
1016     req.dl_primitive = DL_ATTACH_REQ;
1017     req.dl_ppa = ppa;
1018     buf.len = sizeof(req);
1019     buf.buf = (void *) &req;
1020     return putmsg(fd, &buf, NULL, RS_HIPRI);
1021 }
1022
1023 int
1024 dlpi_phys_addr_req(fd)
1025     int fd;
1026 {
1027     dl_phys_addr_req_t req;
1028     struct strbuf buf;
1029
1030     req.dl_primitive = DL_PHYS_ADDR_REQ;
1031     req.dl_addr_type = DL_CURR_PHYS_ADDR;
1032     buf.len = sizeof(req);
1033     buf.buf = (void *) &req;
1034     return putmsg(fd, &buf, NULL, RS_HIPRI);
1035 }
1036
1037 int
1038 dlpi_get_reply(fd, reply, expected_prim, maxlen)
1039     union DL_primitives *reply;
1040     int fd, expected_prim, maxlen;
1041 {
1042     struct strbuf buf;
1043     int flags, n;
1044     struct pollfd pfd;
1045
1046     /*
1047      * Use poll to wait for a message with a timeout.
1048      */
1049     pfd.fd = fd;
1050     pfd.events = POLLIN | POLLPRI;
1051     do {
1052         n = poll(&pfd, 1, 1000);
1053     } while (n == -1 && errno == EINTR);
1054     if (n <= 0)
1055         return -1;
1056
1057     /*
1058      * Get the reply.
1059      */
1060     buf.maxlen = maxlen;
1061     buf.buf = (void *) reply;
1062     flags = 0;
1063     if (getmsg(fd, &buf, NULL, &flags) < 0)
1064         return -1;
1065
1066     if (buf.len < sizeof(ulong)) {
1067         if (debug)
1068             syslog(LOG_DEBUG, "dlpi response short (len=%d)\n", buf.len);
1069         return -1;
1070     }
1071
1072     if (reply->dl_primitive == expected_prim)
1073         return 0;
1074
1075     if (debug) {
1076         if (reply->dl_primitive == DL_ERROR_ACK) {
1077             syslog(LOG_DEBUG, "dlpi error %d (unix errno %d) for prim %x\n",
1078                    reply->error_ack.dl_errno, reply->error_ack.dl_unix_errno,
1079                    reply->error_ack.dl_error_primitive);
1080         } else {
1081             syslog(LOG_DEBUG, "dlpi unexpected response prim %x\n",
1082                    reply->dl_primitive);
1083         }
1084     }
1085
1086     return -1;
1087 }
1088
1089 /*
1090  * Return user specified netmask, modified by any mask we might determine
1091  * for address `addr' (in network byte order).
1092  * Here we scan through the system's list of interfaces, looking for
1093  * any non-point-to-point interfaces which might appear to be on the same
1094  * network as `addr'.  If we find any, we OR in their netmask to the
1095  * user-specified netmask.
1096  */
1097 u_int32_t
1098 GetMask(addr)
1099     u_int32_t addr;
1100 {
1101     u_int32_t mask, nmask, ina;
1102     struct ifreq *ifr, *ifend, ifreq;
1103     int nif;
1104     struct ifconf ifc;
1105
1106     addr = ntohl(addr);
1107     if (IN_CLASSA(addr))        /* determine network mask for address class */
1108         nmask = IN_CLASSA_NET;
1109     else if (IN_CLASSB(addr))
1110         nmask = IN_CLASSB_NET;
1111     else
1112         nmask = IN_CLASSC_NET;
1113     /* class D nets are disallowed by bad_ip_adrs */
1114     mask = netmask | htonl(nmask);
1115
1116     /*
1117      * Scan through the system's network interfaces.
1118      */
1119     if (ioctl(ipfd, SIOCGIFNUM, &nif) < 0)
1120         nif = MAX_IFS;
1121     ifc.ifc_len = nif * sizeof(struct ifreq);
1122     ifc.ifc_req = alloca(ifc.ifc_len);
1123     if (ifc.ifc_req == 0)
1124         return mask;
1125     if (ioctl(ipfd, SIOCGIFCONF, &ifc) < 0) {
1126         syslog(LOG_WARNING, "Couldn't get system interface list: %m");
1127         return mask;
1128     }
1129     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1130     for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1131         /*
1132          * Check the interface's internet address.
1133          */
1134         if (ifr->ifr_addr.sa_family != AF_INET)
1135             continue;
1136         ina = INET_ADDR(ifr->ifr_addr);
1137         if ((ntohl(ina) & nmask) != (addr & nmask))
1138             continue;
1139         /*
1140          * Check that the interface is up, and not point-to-point or loopback.
1141          */
1142         strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1143         if (ioctl(ipfd, SIOCGIFFLAGS, &ifreq) < 0)
1144             continue;
1145         if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1146             != IFF_UP)
1147             continue;
1148         /*
1149          * Get its netmask and OR it into our mask.
1150          */
1151         if (ioctl(ipfd, SIOCGIFNETMASK, &ifreq) < 0)
1152             continue;
1153         mask |= INET_ADDR(ifreq.ifr_addr);
1154     }
1155
1156     return mask;
1157 }
1158
1159 /*
1160  * logwtmp - write an accounting record to the /var/adm/wtmp file.
1161  */
1162 int
1163 logwtmp(line, name, host)
1164     char *line, *name, *host;
1165 {
1166 }
1167
1168 /*
1169  * gethostid - return the serial number of this machine.
1170  */
1171 int
1172 gethostid()
1173 {
1174     char buf[32];
1175
1176     if (sysinfo(SI_HW_SERIAL, buf, sizeof(buf)) < 0) {
1177         syslog(LOG_ERR, "sysinfo: %m");
1178         return 0;
1179     }
1180     return (int) strtoul(buf, NULL, 16);
1181 }
1182
1183 int
1184 strioctl(fd, cmd, ptr, ilen, olen)
1185     int fd, cmd, ilen, olen;
1186     char *ptr;
1187 {
1188     struct strioctl str;
1189
1190     str.ic_cmd = cmd;
1191     str.ic_timout = 0;
1192     str.ic_len = ilen;
1193     str.ic_dp = ptr;
1194     if (ioctl(fd, I_STR, &str) == -1)
1195         return -1;
1196     if (str.ic_len != olen)
1197         syslog(LOG_DEBUG, "strioctl: expected %d bytes, got %d for cmd %x\n",
1198                olen, str.ic_len, cmd);
1199     return 0;
1200 }
1201
1202 int
1203 lock(dev)
1204     char *dev;
1205 {
1206     return 1;
1207 }
1208
1209 int
1210 unlock()
1211 {
1212     return 1;
1213 }