]> git.ozlabs.org Git - ppp.git/blob - pppd/sys-sunos4.c
use asm/byteorder.h, not endian.h
[ppp.git] / pppd / sys-sunos4.c
1 /*
2  * System-dependent procedures for pppd under SunOS 4.
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-sunos4.c,v 1.6 1996/09/26 06:23:34 paulus Exp $";
30 #endif
31
32 #include <stdio.h>
33 #include <stddef.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <unistd.h>
40 #include <termios.h>
41 #include <signal.h>
42 #include <malloc.h>
43 #include <utmp.h>
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/socket.h>
47 #include <sys/sockio.h>
48 #include <sys/stream.h>
49 #include <sys/stropts.h>
50 #include <sys/syslog.h>
51 #include <sys/stat.h>
52 #include <sys/time.h>
53 #include <sys/poll.h>
54 #include <net/if.h>
55 #include <net/if_arp.h>
56 #include <net/nit_if.h>
57 #include <net/route.h>
58 #include <net/ppp_defs.h>
59 #include <net/pppio.h>
60 #include <netinet/in.h>
61
62 #include "pppd.h"
63
64 #if defined(sun) && defined(sparc)
65 #include <alloca.h>
66 #ifndef __GNUC__
67 extern void *alloca();
68 #endif
69 #endif /*sparc*/
70
71 static int      pppfd;
72 static int      fdmuxid = -1;
73 static int      iffd;
74 static int      sockfd;
75
76 static int      restore_term;
77 static struct termios inittermios;
78 static struct winsize wsinfo;   /* Initial window size info */
79 static pid_t    parent_pid;     /* PID of our parent */
80
81 extern u_char   inpacket_buf[]; /* borrowed from main.c */
82
83 static int      link_mtu, link_mru;
84
85 #define NMODULES        32
86 static int      tty_nmodules;
87 static char     tty_modules[NMODULES][FMNAMESZ+1];
88
89 static int      if_is_up;       /* Interface has been marked up */
90 static u_int32_t ifaddrs[2];    /* local and remote addresses */
91 static u_int32_t default_route_gateway; /* Gateway for default route added */
92 static u_int32_t proxy_arp_addr;        /* Addr for proxy arp entry added */
93
94 /* Prototypes for procedures local to this file. */
95 static int translate_speed __P((int));
96 static int baud_rate_of __P((int));
97 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
98 static int strioctl __P((int, int, void *, int, int));
99
100
101 /*
102  * sys_init - System-dependent initialization.
103  */
104 void
105 sys_init()
106 {
107     int x;
108
109     /* Get an internet socket for doing socket ioctl's on. */
110     if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
111         syslog(LOG_ERR, "Couldn't create IP socket: %m");
112         die(1);
113     }
114
115     /*
116      * We may want to send a SIGHUP to the session leader associated
117      * with our controlling terminal later.  Because SunOS doesn't
118      * have getsid(), we make do with sending the signal to our
119      * parent process.
120      */
121     parent_pid = getppid();
122
123     /*
124      * Open the ppp device.
125      */
126     pppfd = open("/dev/ppp", O_RDWR | O_NONBLOCK, 0);
127     if (pppfd < 0) {
128         syslog(LOG_ERR, "Can't open /dev/ppp: %m");
129         die(1);
130     }
131     if (kdebugflag) {
132         x = PPPDBG_LOG + PPPDBG_DRIVER;
133         strioctl(pppfd, PPPIO_DEBUG, &x, sizeof(int), 0);
134     }
135
136     /* Assign a new PPA and get its unit number. */
137     if (strioctl(pppfd, PPPIO_NEWPPA, &ifunit, 0, sizeof(int)) < 0) {
138         syslog(LOG_ERR, "Can't create new PPP interface: %m");
139         die(1);
140     }
141
142     /*
143      * Open the ppp device again and push the if_ppp module on it.
144      */
145     iffd = open("/dev/ppp", O_RDWR, 0);
146     if (iffd < 0) {
147         syslog(LOG_ERR, "Can't open /dev/ppp (2): %m");
148         die(1);
149     }
150     if (kdebugflag) {
151         x = PPPDBG_LOG + PPPDBG_DRIVER;
152         strioctl(iffd, PPPIO_DEBUG, &x, sizeof(int), 0);
153     }
154     if (strioctl(iffd, PPPIO_ATTACH, &ifunit, sizeof(int), 0) < 0) {
155         syslog(LOG_ERR, "Couldn't attach ppp interface to device: %m");
156         die(1);
157     }
158     if (ioctl(iffd, I_PUSH, "if_ppp") < 0) {
159         syslog(LOG_ERR, "Can't push ppp interface module: %m");
160         die(1);
161     }
162     if (kdebugflag) {
163         x = PPPDBG_LOG + PPPDBG_IF;
164         strioctl(iffd, PPPIO_DEBUG, &x, sizeof(int), 0);
165     }
166     if (strioctl(iffd, PPPIO_NEWPPA, &ifunit, sizeof(int), 0) < 0) {
167         syslog(LOG_ERR, "Couldn't create ppp interface unit: %m");
168         die(1);
169     }
170     x = PPP_IP;
171     if (strioctl(iffd, PPPIO_BIND, &x, sizeof(int), 0) < 0) {
172         syslog(LOG_ERR, "Couldn't bind ppp interface to IP SAP: %m");
173         die(1);
174     }
175 }
176
177 /*
178  * sys_cleanup - restore any system state we modified before exiting:
179  * mark the interface down, delete default route and/or proxy arp entry.
180  * This shouldn't call die() because it's called from die().
181  */
182 void
183 sys_cleanup()
184 {
185     if (if_is_up)
186         sifdown(0);
187     if (ifaddrs[0])
188         cifaddr(0, ifaddrs[0], ifaddrs[1]);
189     if (default_route_gateway)
190         cifdefaultroute(0, default_route_gateway);
191     if (proxy_arp_addr)
192         cifproxyarp(0, proxy_arp_addr);
193 }
194
195 /*
196  * sys_close - Clean up in a child process before execing.
197  */
198 void
199 sys_close()
200 {
201     close(iffd);
202     close(pppfd);
203     close(sockfd);
204 }
205
206 /*
207  * sys_check_options - check the options that the user specified
208  */
209 void
210 sys_check_options()
211 {
212 }
213
214
215 /*
216  * daemon - Detach us from controlling terminal session.
217  */
218 int
219 daemon(nochdir, noclose)
220     int nochdir, noclose;
221 {
222     int pid;
223
224     if ((pid = fork()) < 0)
225         return -1;
226     if (pid != 0)
227         exit(0);                /* parent dies */
228     setsid();
229     if (!nochdir)
230         chdir("/");
231     if (!noclose) {
232         fclose(stdin);          /* don't need stdin, stdout, stderr */
233         fclose(stdout);
234         fclose(stderr);
235     }
236     return 0;
237 }
238
239 /*
240  * ppp_available - check whether the system has any ppp interfaces
241  */
242 int
243 ppp_available()
244 {
245     struct stat buf;
246
247     return stat("/dev/ppp", &buf) >= 0;
248 }
249
250 /*
251  * establish_ppp - Turn the serial port into a ppp interface.
252  */
253 void
254 establish_ppp(fd)
255     int fd;
256 {
257     int i;
258
259     /* Pop any existing modules off the tty stream. */
260     for (i = 0;; ++i)
261         if (ioctl(fd, I_LOOK, tty_modules[i]) < 0
262             || ioctl(fd, I_POP, 0) < 0)
263             break;
264     tty_nmodules = i;
265
266     /* Push the async hdlc module and the compressor module. */
267     if (ioctl(fd, I_PUSH, "ppp_ahdl") < 0) {
268         syslog(LOG_ERR, "Couldn't push PPP Async HDLC module: %m");
269         die(1);
270     }
271     if (ioctl(fd, I_PUSH, "ppp_comp") < 0) {
272         syslog(LOG_ERR, "Couldn't push PPP compression module: %m");
273 /*      die(1); */
274     }
275
276     /* Link the serial port under the PPP multiplexor. */
277     if ((fdmuxid = ioctl(pppfd, I_LINK, fd)) < 0) {
278         syslog(LOG_ERR, "Can't link tty to PPP mux: %m");
279         die(1);
280     }
281 }
282
283 /*
284  * restore_loop - reattach the ppp unit to the loopback.
285  * This doesn't need to do anything because disestablish_ppp does it.
286  */
287 void
288 restore_loop()
289 {
290 }
291
292 /*
293  * disestablish_ppp - Restore the serial port to normal operation.
294  * It attempts to reconstruct the stream with the previously popped
295  * modules.  This shouldn't call die() because it's called from die().
296  */
297 void
298 disestablish_ppp(fd)
299     int fd;
300 {
301     int i;
302
303     if (fdmuxid >= 0) {
304         if (ioctl(pppfd, I_UNLINK, fdmuxid) < 0) {
305             if (!hungup)
306                 syslog(LOG_ERR, "Can't unlink tty from PPP mux: %m");
307         }
308         fdmuxid = -1;
309
310         if (!hungup) {
311             while (ioctl(fd, I_POP, 0) >= 0)
312                 ;
313             for (i = tty_nmodules - 1; i >= 0; --i)
314                 if (ioctl(fd, I_PUSH, tty_modules[i]) < 0)
315                     syslog(LOG_ERR, "Couldn't restore tty module %s: %m",
316                            tty_modules[i]);
317         }
318         if (hungup && default_device && parent_pid > 0) {
319             /*
320              * If we have received a hangup, we need to send a SIGHUP
321              * to the terminal's controlling process.  The reason is
322              * that the original stream head for the terminal hasn't
323              * seen the M_HANGUP message (it went up through the ppp
324              * driver to the stream head for our fd to /dev/ppp).
325              * Actually we send the signal to the process that invoked
326              * pppd, since SunOS doesn't have getsid().
327              */
328             kill(parent_pid, SIGHUP);
329         }
330     }
331 }
332
333 /*
334  * Check whether the link seems not to be 8-bit clean.
335  */
336 void
337 clean_check()
338 {
339     int x;
340     char *s;
341
342     if (strioctl(pppfd, PPPIO_GCLEAN, &x, 0, sizeof(x)) < 0)
343         return;
344     s = NULL;
345     switch (~x) {
346     case RCV_B7_0:
347         s = "bit 7 set to 1";
348         break;
349     case RCV_B7_1:
350         s = "bit 7 set to 0";
351         break;
352     case RCV_EVNP:
353         s = "odd parity";
354         break;
355     case RCV_ODDP:
356         s = "even parity";
357         break;
358     }
359     if (s != NULL) {
360         syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
361         syslog(LOG_WARNING, "All received characters had %s", s);
362     }
363 }
364
365 /*
366  * List of valid speeds.
367  */
368 struct speed {
369     int speed_int, speed_val;
370 } speeds[] = {
371 #ifdef B50
372     { 50, B50 },
373 #endif
374 #ifdef B75
375     { 75, B75 },
376 #endif
377 #ifdef B110
378     { 110, B110 },
379 #endif
380 #ifdef B134
381     { 134, B134 },
382 #endif
383 #ifdef B150
384     { 150, B150 },
385 #endif
386 #ifdef B200
387     { 200, B200 },
388 #endif
389 #ifdef B300
390     { 300, B300 },
391 #endif
392 #ifdef B600
393     { 600, B600 },
394 #endif
395 #ifdef B1200
396     { 1200, B1200 },
397 #endif
398 #ifdef B1800
399     { 1800, B1800 },
400 #endif
401 #ifdef B2000
402     { 2000, B2000 },
403 #endif
404 #ifdef B2400
405     { 2400, B2400 },
406 #endif
407 #ifdef B3600
408     { 3600, B3600 },
409 #endif
410 #ifdef B4800
411     { 4800, B4800 },
412 #endif
413 #ifdef B7200
414     { 7200, B7200 },
415 #endif
416 #ifdef B9600
417     { 9600, B9600 },
418 #endif
419 #ifdef B19200
420     { 19200, B19200 },
421 #endif
422 #ifdef B38400
423     { 38400, B38400 },
424 #endif
425 #ifdef EXTA
426     { 19200, EXTA },
427 #endif
428 #ifdef EXTB
429     { 38400, EXTB },
430 #endif
431 #ifdef B57600
432     { 57600, B57600 },
433 #endif
434 #ifdef B115200
435     { 115200, B115200 },
436 #endif
437     { 0, 0 }
438 };
439
440 /*
441  * Translate from bits/second to a speed_t.
442  */
443 static int
444 translate_speed(bps)
445     int bps;
446 {
447     struct speed *speedp;
448
449     if (bps == 0)
450         return 0;
451     for (speedp = speeds; speedp->speed_int; speedp++)
452         if (bps == speedp->speed_int)
453             return speedp->speed_val;
454     syslog(LOG_WARNING, "speed %d not supported", bps);
455     return 0;
456 }
457
458 /*
459  * Translate from a speed_t to bits/second.
460  */
461 static int
462 baud_rate_of(speed)
463     int speed;
464 {
465     struct speed *speedp;
466
467     if (speed == 0)
468         return 0;
469     for (speedp = speeds; speedp->speed_int; speedp++)
470         if (speed == speedp->speed_val)
471             return speedp->speed_int;
472     return 0;
473 }
474
475 /*
476  * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
477  * at the requested speed, etc.  If `local' is true, set CLOCAL
478  * regardless of whether the modem option was specified.
479  */
480 void
481 set_up_tty(fd, local)
482     int fd, local;
483 {
484     int speed;
485     struct termios tios;
486
487     if (tcgetattr(fd, &tios) < 0) {
488         syslog(LOG_ERR, "tcgetattr: %m");
489         die(1);
490     }
491
492     if (!restore_term) {
493         inittermios = tios;
494         ioctl(fd, TIOCGWINSZ, &wsinfo);
495     }
496
497     tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
498     if (crtscts > 0)
499         tios.c_cflag |= CRTSCTS;
500     else if (crtscts < 0)
501         tios.c_cflag &= ~CRTSCTS;
502
503     tios.c_cflag |= CS8 | CREAD | HUPCL;
504     if (local || !modem)
505         tios.c_cflag |= CLOCAL;
506     tios.c_iflag = IGNBRK | IGNPAR;
507     tios.c_oflag = 0;
508     tios.c_lflag = 0;
509     tios.c_cc[VMIN] = 1;
510     tios.c_cc[VTIME] = 0;
511
512     if (crtscts == -2) {
513         tios.c_iflag |= IXON | IXOFF;
514         tios.c_cc[VSTOP] = 0x13;        /* DC3 = XOFF = ^S */
515         tios.c_cc[VSTART] = 0x11;       /* DC1 = XON  = ^Q */
516     }
517
518     speed = translate_speed(inspeed);
519     if (speed) {
520         cfsetospeed(&tios, speed);
521         cfsetispeed(&tios, speed);
522     } else {
523         speed = cfgetospeed(&tios);
524         /*
525          * We can't proceed if the serial port speed is 0,
526          * since that implies that the serial port is disabled.
527          */
528         if (speed == B0) {
529             syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
530                    devnam);
531             die(1);
532         }
533     }
534
535     if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
536         syslog(LOG_ERR, "tcsetattr: %m");
537         die(1);
538     }
539
540     baud_rate = inspeed = baud_rate_of(speed);
541     restore_term = 1;
542 }
543
544 /*
545  * restore_tty - restore the terminal to the saved settings.
546  */
547 void
548 restore_tty(fd)
549     int fd;
550 {
551     if (restore_term) {
552         if (!default_device) {
553             /*
554              * Turn off echoing, because otherwise we can get into
555              * a loop with the tty and the modem echoing to each other.
556              * We presume we are the sole user of this tty device, so
557              * when we close it, it will revert to its defaults anyway.
558              */
559             inittermios.c_lflag &= ~(ECHO | ECHONL);
560         }
561         if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
562             if (!hungup && errno != ENXIO)
563                 syslog(LOG_WARNING, "tcsetattr: %m");
564         ioctl(fd, TIOCSWINSZ, &wsinfo);
565         restore_term = 0;
566     }
567 }
568
569 /*
570  * setdtr - control the DTR line on the serial port.
571  * This is called from die(), so it shouldn't call die().
572  */
573 void
574 setdtr(fd, on)
575 int fd, on;
576 {
577     int modembits = TIOCM_DTR;
578
579     ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
580 }
581
582 /*
583  * open_loopback - open the device we use for getting packets
584  * in demand mode.  Under Solaris 2, we use our existing fd
585  * to the ppp driver.
586  */
587 void
588 open_ppp_loopback()
589 {
590 }
591
592 /*
593  * output - Output PPP packet.
594  */
595 void
596 output(unit, p, len)
597     int unit;
598     u_char *p;
599     int len;
600 {
601     struct strbuf data;
602     int retries;
603     struct pollfd pfd;
604
605     if (debug)
606         log_packet(p, len, "sent ");
607
608     data.len = len;
609     data.buf = (caddr_t) p;
610     retries = 4;
611     while (putmsg(pppfd, NULL, &data, 0) < 0) {
612         if (--retries < 0 || (errno != EWOULDBLOCK && errno != EAGAIN)) {
613             if (errno != ENXIO)
614                 syslog(LOG_ERR, "Couldn't send packet: %m");
615             break;
616         }
617         pfd.fd = pppfd;
618         pfd.events = POLLOUT;
619         poll(&pfd, 1, 250);     /* wait for up to 0.25 seconds */
620     }
621 }
622
623
624 /*
625  * wait_input - wait until there is data available on fd,
626  * for the length of time specified by *timo (indefinite
627  * if timo is NULL).
628  */
629 void
630 wait_input(timo)
631     struct timeval *timo;
632 {
633     int t;
634     struct pollfd pfd;
635
636     t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
637     pfd.fd = pppfd;
638     pfd.events = POLLIN | POLLPRI | POLLHUP;
639     if (poll(&pfd, 1, t) < 0 && errno != EINTR) {
640         syslog(LOG_ERR, "poll: %m");
641         die(1);
642     }
643 }
644
645 /*
646  * wait_loop_output - wait until there is data available on the
647  * loopback, for the length of time specified by *timo (indefinite
648  * if timo is NULL).
649  */
650 void
651 wait_loop_output(timo)
652     struct timeval *timo;
653 {
654     wait_input(timo);
655 }
656
657 /*
658  * wait_time - wait for a given length of time or until a
659  * signal is received.
660  */
661 void
662 wait_time(timo)
663     struct timeval *timo;
664 {
665     int n;
666
667     n = select(0, NULL, NULL, NULL, timo);
668     if (n < 0 && errno != EINTR) {
669         syslog(LOG_ERR, "select: %m");
670         die(1);
671     }
672 }
673
674
675 /*
676  * read_packet - get a PPP packet from the serial device.
677  */
678 int
679 read_packet(buf)
680     u_char *buf;
681 {
682     struct strbuf ctrl, data;
683     int flags, len;
684     unsigned char ctrlbuf[64];
685
686     for (;;) {
687         data.maxlen = PPP_MRU + PPP_HDRLEN;
688         data.buf = (caddr_t) buf;
689         ctrl.maxlen = sizeof(ctrlbuf);
690         ctrl.buf = (caddr_t) ctrlbuf;
691         flags = 0;
692         len = getmsg(pppfd, &ctrl, &data, &flags);
693         if (len < 0) {
694             if (errno = EAGAIN || errno == EINTR)
695                 return -1;
696             syslog(LOG_ERR, "Error reading packet: %m");
697             die(1);
698         }
699
700         if (ctrl.len <= 0)
701             return data.len;
702
703         /*
704          * Got a M_PROTO or M_PCPROTO message.  Huh?
705          */
706         if (debug)
707             syslog(LOG_DEBUG, "got ctrl msg len=%d", ctrl.len);
708
709     }
710 }
711
712 /*
713  * get_loop_output - get outgoing packets from the ppp device,
714  * and detect when we want to bring the real link up.
715  * Return value is 1 if we need to bring up the link, 0 otherwise.
716  */
717 int
718 get_loop_output()
719 {
720     int len;
721     int rv = 0;
722
723     while ((len = read_packet(inpacket_buf)) > 0) {
724         if (loop_frame(inpacket_buf, len))
725             rv = 1;
726     }
727     return rv;
728 }
729
730 /*
731  * ppp_send_config - configure the transmit characteristics of
732  * the ppp interface.
733  */
734 void
735 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
736     int unit, mtu;
737     u_int32_t asyncmap;
738     int pcomp, accomp;
739 {
740     int cf[2];
741
742     link_mtu = mtu;
743     if (strioctl(pppfd, PPPIO_MTU, &mtu, sizeof(mtu), 0) < 0) {
744         if (hungup && errno == ENXIO)
745             return;
746         syslog(LOG_ERR, "Couldn't set MTU: %m");
747     }
748     if (strioctl(pppfd, PPPIO_XACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
749         syslog(LOG_ERR, "Couldn't set transmit ACCM: %m");
750     }
751     cf[0] = (pcomp? COMP_PROT: 0) + (accomp? COMP_AC: 0);
752     cf[1] = COMP_PROT | COMP_AC;
753     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
754         syslog(LOG_ERR, "Couldn't set prot/AC compression: %m");
755     }
756 }
757
758 /*
759  * ppp_set_xaccm - set the extended transmit ACCM for the interface.
760  */
761 void
762 ppp_set_xaccm(unit, accm)
763     int unit;
764     ext_accm accm;
765 {
766     if (strioctl(pppfd, PPPIO_XACCM, accm, sizeof(ext_accm), 0) < 0) {
767         if (!hungup || errno != ENXIO)
768             syslog(LOG_WARNING, "Couldn't set extended ACCM: %m");
769     }
770 }
771
772 /*
773  * ppp_recv_config - configure the receive-side characteristics of
774  * the ppp interface.
775  */
776 void
777 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
778     int unit, mru;
779     u_int32_t asyncmap;
780     int pcomp, accomp;
781 {
782     int cf[2];
783
784     link_mru = mru;
785     if (strioctl(pppfd, PPPIO_MRU, &mru, sizeof(mru), 0) < 0) {
786         if (hungup && errno == ENXIO)
787             return;
788         syslog(LOG_ERR, "Couldn't set MRU: %m");
789     }
790     if (strioctl(pppfd, PPPIO_RACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
791         syslog(LOG_ERR, "Couldn't set receive ACCM: %m");
792     }
793     cf[0] = (pcomp? DECOMP_PROT: 0) + (accomp? DECOMP_AC: 0);
794     cf[1] = DECOMP_PROT | DECOMP_AC;
795     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
796         syslog(LOG_ERR, "Couldn't set prot/AC decompression: %m");
797     }
798 }
799
800 /*
801  * ccp_test - ask kernel whether a given compression method
802  * is acceptable for use.
803  */
804 int
805 ccp_test(unit, opt_ptr, opt_len, for_transmit)
806     int unit, opt_len, for_transmit;
807     u_char *opt_ptr;
808 {
809     if (strioctl(pppfd, (for_transmit? PPPIO_XCOMP: PPPIO_RCOMP),
810                  opt_ptr, opt_len, 0) >= 0)
811         return 1;
812     return (errno == ENOSR)? 0: -1;
813 }
814
815 /*
816  * ccp_flags_set - inform kernel about the current state of CCP.
817  */
818 void
819 ccp_flags_set(unit, isopen, isup)
820     int unit, isopen, isup;
821 {
822     int cf[2];
823
824     cf[0] = (isopen? CCP_ISOPEN: 0) + (isup? CCP_ISUP: 0);
825     cf[1] = CCP_ISOPEN | CCP_ISUP | CCP_ERROR | CCP_FATALERROR;
826     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
827         if (!hungup || errno != ENXIO)
828             syslog(LOG_ERR, "Couldn't set kernel CCP state: %m");
829     }
830 }
831
832 /*
833  * get_idle_time - return how long the link has been idle.
834  */
835 int
836 get_idle_time(u, ip)
837     int u;
838     struct ppp_idle *ip;
839 {
840     return strioctl(pppfd, PPPIO_GIDLE, ip, 0, sizeof(struct ppp_idle)) >= 0;
841 }
842
843
844 /*
845  * ccp_fatal_error - returns 1 if decompression was disabled as a
846  * result of an error detected after decompression of a packet,
847  * 0 otherwise.  This is necessary because of patent nonsense.
848  */
849 int
850 ccp_fatal_error(unit)
851     int unit;
852 {
853     int cf[2];
854
855     cf[0] = cf[1] = 0;
856     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
857         if (errno != ENXIO && errno != EINVAL)
858             syslog(LOG_ERR, "Couldn't get compression flags: %m");
859         return 0;
860     }
861     return cf[0] & CCP_FATALERROR;
862 }
863
864 /*
865  * sifvjcomp - config tcp header compression
866  */
867 int
868 sifvjcomp(u, vjcomp, xcidcomp, xmaxcid)
869     int u, vjcomp, xcidcomp, xmaxcid;
870 {
871     int cf[2];
872     char maxcid[2];
873
874     if (vjcomp) {
875         maxcid[0] = xcidcomp;
876         maxcid[1] = 15;         /* XXX should be rmaxcid */
877         if (strioctl(pppfd, PPPIO_VJINIT, maxcid, sizeof(maxcid), 0) < 0) {
878             syslog(LOG_ERR, "Couldn't initialize VJ compression: %m");
879         }
880     }
881
882     cf[0] = (vjcomp? COMP_VJC + DECOMP_VJC: 0)  /* XXX this is wrong */
883         + (xcidcomp? COMP_VJCCID + DECOMP_VJCCID: 0);
884     cf[1] = COMP_VJC + DECOMP_VJC + COMP_VJCCID + DECOMP_VJCCID;
885     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
886         if (vjcomp)
887             syslog(LOG_ERR, "Couldn't enable VJ compression: %m");
888     }
889
890     return 1;
891 }
892
893 /*
894  * sifup - Config the interface up and enable IP packets to pass.
895  */
896 int
897 sifup(u)
898     int u;
899 {
900     struct ifreq ifr;
901
902     strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
903     if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
904         syslog(LOG_ERR, "Couldn't mark interface up (get): %m");
905         return 0;
906     }
907     ifr.ifr_flags |= IFF_UP;
908     if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
909         syslog(LOG_ERR, "Couldn't mark interface up (set): %m");
910         return 0;
911     }
912     if_is_up = 1;
913     return 1;
914 }
915
916 /*
917  * sifdown - Config the interface down and disable IP.
918  */
919 int
920 sifdown(u)
921     int u;
922 {
923     struct ifreq ifr;
924
925     strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
926     if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
927         syslog(LOG_ERR, "Couldn't mark interface down (get): %m");
928         return 0;
929     }
930     if ((ifr.ifr_flags & IFF_UP) != 0) {
931         ifr.ifr_flags &= ~IFF_UP;
932         if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
933             syslog(LOG_ERR, "Couldn't mark interface down (set): %m");
934             return 0;
935         }
936     }
937     if_is_up = 0;
938     return 1;
939 }
940
941 /*
942  * sifnpmode - Set the mode for handling packets for a given NP.
943  */
944 int
945 sifnpmode(u, proto, mode)
946     int u;
947     int proto;
948     enum NPmode mode;
949 {
950     int npi[2];
951
952     npi[0] = proto;
953     npi[1] = (int) mode;
954     if (strioctl(pppfd, PPPIO_NPMODE, npi, 2 * sizeof(int), 0) < 0) {
955         syslog(LOG_ERR, "ioctl(set NP %d mode to %d): %m", proto, mode);
956         return 0;
957     }
958     return 1;
959 }
960
961 #define INET_ADDR(x)    (((struct sockaddr_in *) &(x))->sin_addr.s_addr)
962
963 /*
964  * sifaddr - Config the interface IP addresses and netmask.
965  */
966 int
967 sifaddr(u, o, h, m)
968     int u;
969     u_int32_t o, h, m;
970 {
971     struct ifreq ifr;
972
973     memset(&ifr, 0, sizeof(ifr));
974     strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
975     ifr.ifr_addr.sa_family = AF_INET;
976     INET_ADDR(ifr.ifr_addr) = m;
977     if (ioctl(sockfd, SIOCSIFNETMASK, &ifr) < 0) {
978         syslog(LOG_ERR, "Couldn't set IP netmask: %m");
979     }
980     ifr.ifr_addr.sa_family = AF_INET;
981     INET_ADDR(ifr.ifr_addr) = o;
982     if (ioctl(sockfd, SIOCSIFADDR, &ifr) < 0) {
983         syslog(LOG_ERR, "Couldn't set local IP address: %m");
984     }
985     ifr.ifr_dstaddr.sa_family = AF_INET;
986     INET_ADDR(ifr.ifr_dstaddr) = h;
987     if (ioctl(sockfd, SIOCSIFDSTADDR, &ifr) < 0) {
988         syslog(LOG_ERR, "Couldn't set remote IP address: %m");
989     }
990     ifr.ifr_metric = link_mtu;
991     if (ioctl(sockfd, SIOCSIFMTU, &ifr) < 0) {
992         syslog(LOG_ERR, "Couldn't set IP MTU: %m");
993     }
994     ifaddrs[0] = o;
995     ifaddrs[1] = h;
996
997     return 1;
998 }
999
1000 /*
1001  * cifaddr - Clear the interface IP addresses, and delete routes
1002  * through the interface if possible.
1003  */
1004 int
1005 cifaddr(u, o, h)
1006     int u;
1007     u_int32_t o, h;
1008 {
1009     struct rtentry rt;
1010
1011     bzero(&rt, sizeof(rt));
1012     rt.rt_dst.sa_family = AF_INET;
1013     INET_ADDR(rt.rt_dst) = h;
1014     rt.rt_gateway.sa_family = AF_INET;
1015     INET_ADDR(rt.rt_gateway) = o;
1016     rt.rt_flags = RTF_HOST;
1017     if (ioctl(sockfd, SIOCDELRT, &rt) < 0)
1018         syslog(LOG_ERR, "Couldn't delete route through interface: %m");
1019     ifaddrs[0] = 0;
1020     return 1;
1021 }
1022
1023 /*
1024  * sifdefaultroute - assign a default route through the address given.
1025  */
1026 int
1027 sifdefaultroute(u, g)
1028     int u;
1029     u_int32_t g;
1030 {
1031     struct rtentry rt;
1032
1033     bzero(&rt, sizeof(rt));
1034     rt.rt_dst.sa_family = AF_INET;
1035     INET_ADDR(rt.rt_dst) = 0;
1036     rt.rt_gateway.sa_family = AF_INET;
1037     INET_ADDR(rt.rt_gateway) = g;
1038     rt.rt_flags = RTF_GATEWAY;
1039
1040     if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
1041         syslog(LOG_ERR, "Can't add default route: %m");
1042         return 0;
1043     }
1044
1045     default_route_gateway = g;
1046     return 1;
1047 }
1048
1049 /*
1050  * cifdefaultroute - delete a default route through the address given.
1051  */
1052 int
1053 cifdefaultroute(u, g)
1054     int u;
1055     u_int32_t g;
1056 {
1057     struct rtentry rt;
1058
1059     bzero(&rt, sizeof(rt));
1060     rt.rt_dst.sa_family = AF_INET;
1061     INET_ADDR(rt.rt_dst) = 0;
1062     rt.rt_gateway.sa_family = AF_INET;
1063     INET_ADDR(rt.rt_gateway) = g;
1064     rt.rt_flags = RTF_GATEWAY;
1065
1066     if (ioctl(sockfd, SIOCDELRT, &rt) < 0) {
1067         syslog(LOG_ERR, "Can't delete default route: %m");
1068         return 0;
1069     }
1070
1071     default_route_gateway = 0;
1072     return 1;
1073 }
1074
1075 /*
1076  * sifproxyarp - Make a proxy ARP entry for the peer.
1077  */
1078 int
1079 sifproxyarp(unit, hisaddr)
1080     int unit;
1081     u_int32_t hisaddr;
1082 {
1083     struct arpreq arpreq;
1084
1085     bzero(&arpreq, sizeof(arpreq));
1086     if (!get_ether_addr(hisaddr, &arpreq.arp_ha))
1087         return 0;
1088
1089     arpreq.arp_pa.sa_family = AF_INET;
1090     INET_ADDR(arpreq.arp_pa) = hisaddr;
1091     arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1092     if (ioctl(sockfd, SIOCSARP, (caddr_t) &arpreq) < 0) {
1093         syslog(LOG_ERR, "Couldn't set proxy ARP entry: %m");
1094         return 0;
1095     }
1096
1097     proxy_arp_addr = hisaddr;
1098     return 1;
1099 }
1100
1101 /*
1102  * cifproxyarp - Delete the proxy ARP entry for the peer.
1103  */
1104 int
1105 cifproxyarp(unit, hisaddr)
1106     int unit;
1107     u_int32_t hisaddr;
1108 {
1109     struct arpreq arpreq;
1110
1111     bzero(&arpreq, sizeof(arpreq));
1112     arpreq.arp_pa.sa_family = AF_INET;
1113     INET_ADDR(arpreq.arp_pa) = hisaddr;
1114     if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1115         syslog(LOG_ERR, "Couldn't delete proxy ARP entry: %m");
1116         return 0;
1117     }
1118
1119     proxy_arp_addr = 0;
1120     return 1;
1121 }
1122
1123 /*
1124  * get_ether_addr - get the hardware address of an interface on the
1125  * the same subnet as ipaddr.
1126  */
1127 #define MAX_IFS         32
1128
1129 static int
1130 get_ether_addr(ipaddr, hwaddr)
1131     u_int32_t ipaddr;
1132     struct sockaddr *hwaddr;
1133 {
1134     struct ifreq *ifr, *ifend;
1135     u_int32_t ina, mask;
1136     struct ifreq ifreq;
1137     struct ifconf ifc;
1138     struct ifreq ifs[MAX_IFS];
1139     int nit_fd;
1140
1141     ifc.ifc_len = sizeof(ifs);
1142     ifc.ifc_req = ifs;
1143     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1144         syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
1145         return 0;
1146     }
1147
1148     /*
1149      * Scan through looking for an interface with an Internet
1150      * address on the same subnet as `ipaddr'.
1151      */
1152     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1153     for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1154             ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1155         if (ifr->ifr_addr.sa_family == AF_INET) {
1156
1157             /*
1158              * Check that the interface is up, and not point-to-point
1159              * or loopback.
1160              */
1161             strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1162             if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1163                 continue;
1164             if ((ifreq.ifr_flags &
1165                  (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1166                  != (IFF_UP|IFF_BROADCAST))
1167                 continue;
1168
1169             /*
1170              * Get its netmask and check that it's on the right subnet.
1171              */
1172             if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1173                 continue;
1174             ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1175             mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1176             if ((ipaddr & mask) != (ina & mask))
1177                 continue;
1178
1179             break;
1180         }
1181     }
1182
1183     if (ifr >= ifend)
1184         return 0;
1185     syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
1186
1187     /*
1188      * Grab the physical address for this interface.
1189      */
1190     if ((nit_fd = open("/dev/nit", O_RDONLY)) < 0) {
1191         syslog(LOG_ERR, "Couldn't open /dev/nit: %m");
1192         return 0;
1193     }
1194     strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1195     if (ioctl(nit_fd, NIOCBIND, &ifreq) < 0
1196         || ioctl(nit_fd, SIOCGIFADDR, &ifreq) < 0) {
1197         syslog(LOG_ERR, "Couldn't get hardware address for %s: %m",
1198                ifreq.ifr_name);
1199         close(nit_fd);
1200         return 0;
1201     }
1202
1203     hwaddr->sa_family = AF_UNSPEC;
1204     memcpy(hwaddr->sa_data, ifreq.ifr_addr.sa_data, 6);
1205     close(nit_fd);
1206     return 1;
1207 }
1208
1209 #define WTMPFILE        "/usr/adm/wtmp"
1210
1211 int
1212 logwtmp(line, name, host)
1213     char *line, *name, *host;
1214 {
1215     int fd;
1216     struct stat buf;
1217     struct utmp ut;
1218
1219     if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1220         return;
1221     if (!fstat(fd, &buf)) {
1222         (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1223         (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1224         (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1225         (void)time(&ut.ut_time);
1226         if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1227             (void)ftruncate(fd, buf.st_size);
1228     }
1229     close(fd);
1230 }
1231
1232 /*
1233  * Return user specified netmask, modified by any mask we might determine
1234  * for address `addr' (in network byte order).
1235  * Here we scan through the system's list of interfaces, looking for
1236  * any non-point-to-point interfaces which might appear to be on the same
1237  * network as `addr'.  If we find any, we OR in their netmask to the
1238  * user-specified netmask.
1239  */
1240 u_int32_t
1241 GetMask(addr)
1242     u_int32_t addr;
1243 {
1244     u_int32_t mask, nmask, ina;
1245     struct ifreq *ifr, *ifend, ifreq;
1246     struct ifconf ifc;
1247
1248     addr = ntohl(addr);
1249     if (IN_CLASSA(addr))        /* determine network mask for address class */
1250         nmask = IN_CLASSA_NET;
1251     else if (IN_CLASSB(addr))
1252         nmask = IN_CLASSB_NET;
1253     else
1254         nmask = IN_CLASSC_NET;
1255     /* class D nets are disallowed by bad_ip_adrs */
1256     mask = netmask | htonl(nmask);
1257
1258     /*
1259      * Scan through the system's network interfaces.
1260      */
1261     ifc.ifc_len = MAX_IFS * sizeof(struct ifreq);
1262     ifc.ifc_req = alloca(ifc.ifc_len);
1263     if (ifc.ifc_req == 0)
1264         return mask;
1265     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1266         syslog(LOG_WARNING, "Couldn't get system interface list: %m");
1267         return mask;
1268     }
1269     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1270     for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1271         /*
1272          * Check the interface's internet address.
1273          */
1274         if (ifr->ifr_addr.sa_family != AF_INET)
1275             continue;
1276         ina = INET_ADDR(ifr->ifr_addr);
1277         if ((ntohl(ina) & nmask) != (addr & nmask))
1278             continue;
1279         /*
1280          * Check that the interface is up, and not point-to-point or loopback.
1281          */
1282         strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1283         if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1284             continue;
1285         if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1286             != IFF_UP)
1287             continue;
1288         /*
1289          * Get its netmask and OR it into our mask.
1290          */
1291         if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1292             continue;
1293         mask |= INET_ADDR(ifreq.ifr_addr);
1294     }
1295
1296     return mask;
1297 }
1298
1299 static int
1300 strioctl(fd, cmd, ptr, ilen, olen)
1301     int fd, cmd, ilen, olen;
1302     void *ptr;
1303 {
1304     struct strioctl str;
1305
1306     str.ic_cmd = cmd;
1307     str.ic_timout = 0;
1308     str.ic_len = ilen;
1309     str.ic_dp = ptr;
1310     if (ioctl(fd, I_STR, &str) == -1)
1311         return -1;
1312     if (str.ic_len != olen)
1313         syslog(LOG_DEBUG, "strioctl: expected %d bytes, got %d for cmd %x\n",
1314                olen, str.ic_len, cmd);
1315     return 0;
1316 }
1317
1318 /*
1319  * Code for locking/unlocking the serial device.
1320  * This code is derived from chat.c.
1321  */
1322
1323 #if !defined(HDB) && !defined(SUNOS3)
1324 #define HDB     1               /* ascii lock files are the default */
1325 #endif
1326
1327 #ifndef LOCK_DIR
1328 # if HDB
1329 #  define       PIDSTRING
1330 #  define       LOCK_PREFIX     "/usr/spool/locks/LCK.."
1331 # else /* HDB */
1332 #  define       LOCK_PREFIX     "/usr/spool/uucp/LCK.."
1333 # endif /* HDB */
1334 #endif /* LOCK_DIR */
1335
1336 static char *lock_file;         /* name of lock file created */
1337
1338 /*
1339  * lock - create a lock file for the named device.
1340  */
1341 int
1342 lock(dev)
1343     char *dev;
1344 {
1345     char hdb_lock_buffer[12];
1346     int fd, pid, n;
1347     char *p;
1348
1349     if ((p = strrchr(dev, '/')) != NULL)
1350         dev = p + 1;
1351     lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1352     if (lock_file == NULL)
1353         novm("lock file name");
1354     strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1355
1356     while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1357         if (errno == EEXIST
1358             && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1359             /* Read the lock file to find out who has the device locked */
1360 #ifdef PIDSTRING
1361             n = read(fd, hdb_lock_buffer, 11);
1362             if (n > 0) {
1363                 hdb_lock_buffer[n] = 0;
1364                 pid = atoi(hdb_lock_buffer);
1365             }
1366 #else
1367             n = read(fd, &pid, sizeof(pid));
1368 #endif
1369             if (n <= 0) {
1370                 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1371                 close(fd);
1372             } else {
1373                 if (kill(pid, 0) == -1 && errno == ESRCH) {
1374                     /* pid no longer exists - remove the lock file */
1375                     if (unlink(lock_file) == 0) {
1376                         close(fd);
1377                         syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1378                                dev, pid);
1379                         continue;
1380                     } else
1381                         syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1382                                dev);
1383                 } else
1384                     syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1385                            dev, pid);
1386             }
1387             close(fd);
1388         } else
1389             syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1390         free(lock_file);
1391         lock_file = NULL;
1392         return -1;
1393     }
1394
1395 #ifdef PIDSTRING
1396     sprintf(hdb_lock_buffer, "%10d\n", getpid());
1397     write(fd, hdb_lock_buffer, 11);
1398 #else
1399     pid = getpid();
1400     write(fd, &pid, sizeof pid);
1401 #endif
1402
1403     close(fd);
1404     return 0;
1405 }
1406
1407 /*
1408  * unlock - remove our lockfile
1409  */
1410 void
1411 unlock()
1412 {
1413     if (lock_file) {
1414         unlink(lock_file);
1415         free(lock_file);
1416         lock_file = NULL;
1417     }
1418 }
1419
1420 /*
1421  * SunOS doesn't have strtoul :-(
1422  */
1423 unsigned long
1424 strtoul(str, ptr, base)
1425     char *str, **ptr;
1426     int base;
1427 {
1428     return (unsigned long) strtol(str, ptr, base);
1429 }
1430
1431 /*
1432  * Or strerror :-(
1433  */
1434 extern char *sys_errlist[];
1435 extern int sys_nerr;
1436
1437 char *
1438 strerror(n)
1439     int n;
1440 {
1441     static char unknown[32];
1442
1443     if (n > 0 && n < sys_nerr)
1444         return sys_errlist[n];
1445     sprintf(unknown, "Error %d", n);
1446     return unknown;
1447 }