Fix it so it compiles under RedHat 5.0 linux with glibc 2.0.7.
[mpls-ppp.git] / pppd / sys-linux.c
blob29aa77fef300b9d3ef69334848723bb359825452
1 /*
2 * sys-linux.c - System-dependent procedures for setting up
3 * PPP interfaces on Linux systems
5 * Copyright (c) 1989 Carnegie Mellon University.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by Carnegie Mellon University. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 #include <sys/ioctl.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <sys/time.h>
25 #include <sys/errno.h>
26 #include <sys/file.h>
27 #include <sys/stat.h>
28 #include <sys/utsname.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <syslog.h>
33 #include <string.h>
34 #include <time.h>
35 #include <memory.h>
36 #include <utmp.h>
37 #include <mntent.h>
38 #include <signal.h>
39 #include <fcntl.h>
40 #include <ctype.h>
41 #include <termios.h>
42 #include <unistd.h>
44 /* This is in netdevice.h. However, this compile will fail miserably if
45 you attempt to include netdevice.h because it has so many references
46 to __memcpy functions which it should not attempt to do. So, since I
47 really don't use it, but it must be defined, define it now. */
49 #ifndef MAX_ADDR_LEN
50 #define MAX_ADDR_LEN 7
51 #endif
53 #if __GLIBC__ >= 2
54 #include <asm/types.h> /* glibc 2 conflicts with linux/types.h */
55 #include <net/if.h>
56 #include <net/if_arp.h>
57 #include <net/route.h>
58 #include <netinet/if_ether.h>
59 #else
60 #include <linux/types.h>
61 #include <linux/if.h>
62 #include <linux/if_arp.h>
63 #include <linux/route.h>
64 #include <linux/if_ether.h>
65 #endif
66 #include <netinet/in.h>
67 #include <arpa/inet.h>
69 #include <linux/ppp_defs.h>
70 #include <linux/if_ppp.h>
72 #include "pppd.h"
73 #include "fsm.h"
74 #include "ipcp.h"
75 #include "patchlevel.h"
77 #ifdef IPX_CHANGE
78 #include "ipxcp.h"
79 #if __GLIBC__ >= 2 && \
80 !(defined(__powerpc__) && __GLIBC__ == 2 && __GLIBC_MINOR__ == 0)
81 #include <netipx/ipx.h>
82 #else
83 #include <linux/ipx.h>
84 #endif
85 #endif /* IPX_CHANGE */
87 #ifdef LOCKLIB
88 #include <sys/locks.h>
89 #endif
91 #ifndef RTF_DEFAULT /* Normally in <linux/route.h> from <net/route.h> */
92 #define RTF_DEFAULT 0
93 #endif
95 #define ok_error(num) ((num)==EIO)
97 static int tty_disc = N_TTY; /* The TTY discipline */
98 static int ppp_disc = N_PPP; /* The PPP discpline */
99 static int initfdflags = -1; /* Initial file descriptor flags for fd */
100 static int ppp_fd = -1; /* fd which is set to PPP discipline */
101 static int sock_fd = -1; /* socket for doing interface ioctls */
102 static int slave_fd = -1;
103 static int master_fd = -1;
105 static int has_proxy_arp = 0;
106 static int driver_version = 0;
107 static int driver_modification = 0;
108 static int driver_patch = 0;
109 static int driver_is_old = 0;
110 static int restore_term = 0; /* 1 => we've munged the terminal */
111 static struct termios inittermios; /* Initial TTY termios */
113 static char loop_name[20];
114 static unsigned char inbuf[512]; /* buffer for chars read from loopback */
116 static int if_is_up; /* Interface has been marked up */
117 static u_int32_t default_route_gateway; /* Gateway for default route added */
118 static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
120 static char *lock_file;
122 static struct utsname utsname; /* for the kernel version */
123 static int kernel_version;
124 #define KVERSION(j,n,p) ((j)*1000000 + (n)*1000 + (p))
126 #define MAX_IFS 100
128 #define FLAGS_GOOD (IFF_UP | IFF_BROADCAST)
129 #define FLAGS_MASK (IFF_UP | IFF_BROADCAST | \
130 IFF_POINTOPOINT | IFF_LOOPBACK | IFF_NOARP)
132 /* Prototypes for procedures local to this file. */
133 static int get_flags (void);
134 static void set_flags (int flags);
135 static int translate_speed (int bps);
136 static int baud_rate_of (int speed);
137 static char *path_to_route (void);
138 static void close_route_table (void);
139 static int open_route_table (void);
140 static int read_route_table (struct rtentry *rt);
141 static int defaultroute_exists (struct rtentry *rt);
142 static int get_ether_addr (u_int32_t ipaddr, struct sockaddr *hwaddr,
143 char *name);
144 static void decode_version (char *buf, int *version, int *mod, int *patch);
146 extern u_char inpacket_buf[]; /* borrowed from main.c */
149 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
150 * if it exists.
153 #define SET_SA_FAMILY(addr, family) \
154 memset ((char *) &(addr), '\0', sizeof(addr)); \
155 addr.sa_family = (family);
158 * Determine if the PPP connection should still be present.
161 extern int hungup;
163 #ifndef LOCK_PREFIX
164 #define LOCK_PREFIX "/var/lock/LCK.."
165 #endif
167 static void set_ppp_fd (int new_fd)
169 SYSDEBUG ((LOG_DEBUG, "setting ppp_fd to %d\n", ppp_fd));
170 ppp_fd = new_fd;
173 static int still_ppp(void)
175 if (!hungup || ppp_fd == slave_fd)
176 return 1;
177 if (slave_fd >= 0) {
178 set_ppp_fd(slave_fd);
179 return 1;
181 return 0;
184 /********************************************************************
186 * Functions to read and set the flags value in the device driver
189 static int get_flags (void)
191 int flags;
193 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &flags) < 0)
195 if ( ok_error (errno) )
197 flags = 0;
199 else
201 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
202 quit();
206 SYSDEBUG ((LOG_DEBUG, "get flags = %x\n", flags));
207 return flags;
210 /********************************************************************/
212 static void set_flags (int flags)
214 SYSDEBUG ((LOG_DEBUG, "set flags = %x\n", flags));
216 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &flags) < 0)
218 if (! ok_error (errno) )
220 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS, %x): %m(%d)", flags, errno);
221 quit();
226 /********************************************************************
228 * sys_init - System-dependent initialization.
231 void sys_init(void)
233 int osmaj, osmin, ospatch;
235 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
236 setlogmask(LOG_UPTO(LOG_INFO));
237 if (debug)
239 setlogmask(LOG_UPTO(LOG_DEBUG));
242 /* Get an internet socket for doing socket ioctls. */
243 sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
244 if (sock_fd < 0)
246 if ( ! ok_error ( errno ))
248 syslog(LOG_ERR, "Couldn't create IP socket: %m(%d)", errno);
249 die(1);
253 uname(&utsname);
254 osmaj = osmin = ospatch = 0;
255 sscanf(utsname.release, "%d.%d.%d", &osmaj, &osmin, &ospatch);
256 kernel_version = KVERSION(osmaj, osmin, ospatch);
259 /********************************************************************
261 * sys_cleanup - restore any system state we modified before exiting:
262 * mark the interface down, delete default route and/or proxy arp entry.
263 * This should call die() because it's called from die().
266 void sys_cleanup(void)
269 * Take down the device
271 if (if_is_up)
273 sifdown(0);
276 * Delete any routes through the device.
278 if (default_route_gateway != 0)
280 cifdefaultroute(0, 0, default_route_gateway);
283 if (has_proxy_arp)
285 cifproxyarp(0, proxy_arp_addr);
289 /********************************************************************
291 * sys_close - Clean up in a child process before execing.
293 void
294 sys_close(void)
296 close(sock_fd);
297 sock_fd = -1;
298 closelog();
301 /********************************************************************
303 * note_debug_level - note a change in the debug level.
306 void note_debug_level (void)
308 if (debug)
310 SYSDEBUG ((LOG_INFO, "Debug turned ON, Level %d", debug));
311 setlogmask(LOG_UPTO(LOG_DEBUG));
313 else
315 setlogmask(LOG_UPTO(LOG_WARNING));
319 /********************************************************************
321 * set_kdebugflag - Define the debugging level for the kernel
324 int set_kdebugflag (int requested_level)
326 if (ioctl(ppp_fd, PPPIOCSDEBUG, &requested_level) < 0)
328 if ( ! ok_error (errno) )
330 syslog (LOG_ERR, "ioctl(PPPIOCSDEBUG): %m");
332 return (0);
334 SYSDEBUG ((LOG_INFO, "set kernel debugging level to %d",
335 requested_level));
336 return (1);
339 /********************************************************************
341 * establish_ppp - Turn the serial port into a ppp interface.
344 void establish_ppp (int tty_fd)
346 int x;
348 * The current PPP device will be the tty file.
350 set_ppp_fd (tty_fd);
352 * Ensure that the tty device is in exclusive mode.
354 if (ioctl(tty_fd, TIOCEXCL, 0) < 0)
356 if ( ! ok_error ( errno ))
358 syslog (LOG_WARNING, "ioctl(TIOCEXCL): %m");
362 * Demand mode - prime the old ppp device to relinquish the unit.
364 if (demand && ioctl(slave_fd, PPPIOCXFERUNIT, 0) < 0)
366 syslog(LOG_ERR, "ioctl(transfer ppp unit): %m(%d)", errno);
367 die(1);
370 * Set the current tty to the PPP discpline
372 if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0)
374 if ( ! ok_error (errno) )
376 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
377 die(1);
381 * Find out which interface we were given.
383 if (ioctl(ppp_fd, PPPIOCGUNIT, &x) < 0)
385 if ( ! ok_error (errno))
387 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m(%d)", errno);
388 die(1);
392 * Check that we got the same unit again.
394 if (demand)
396 if (x != ifunit)
398 syslog(LOG_ERR, "transfer_ppp failed: wanted unit %d, got %d",
399 ifunit, x);
400 die(1);
404 ifunit = x;
406 * Enable debug in the driver if requested.
408 if (!demand)
409 set_kdebugflag (kdebugflag);
411 set_flags (get_flags() & ~(SC_RCV_B7_0 | SC_RCV_B7_1 |
412 SC_RCV_EVNP | SC_RCV_ODDP));
414 SYSDEBUG ((LOG_NOTICE, "Using version %d.%d.%d of PPP driver",
415 driver_version, driver_modification, driver_patch));
417 * Fetch the initial file flags and reset blocking mode on the file.
419 initfdflags = fcntl(ppp_fd, F_GETFL);
421 if (initfdflags == -1 ||
422 fcntl(ppp_fd, F_SETFL, initfdflags | O_NONBLOCK) == -1)
424 if ( ! ok_error (errno))
426 syslog(LOG_WARNING,
427 "Couldn't set device to non-blocking mode: %m");
432 /********************************************************************
434 * disestablish_ppp - Restore the serial port to normal operation.
435 * This shouldn't call die() because it's called from die().
438 void disestablish_ppp(int tty_fd)
441 * Attempt to restore the previous tty settings
443 if (!hungup)
446 * Restore the previous line discipline
448 if (ioctl(tty_fd, TIOCSETD, &tty_disc) < 0)
450 if ( ! ok_error (errno))
452 syslog(LOG_ERR, "ioctl(TIOCSETD, N_TTY): %m");
456 if (ioctl(tty_fd, TIOCNXCL, 0) < 0)
458 if ( ! ok_error (errno))
460 syslog (LOG_WARNING, "ioctl(TIOCNXCL): %m(%d)", errno);
464 /* Reset non-blocking mode on fd. */
465 if (initfdflags != -1 && fcntl(tty_fd, F_SETFL, initfdflags) < 0)
467 if ( ! ok_error (errno))
469 syslog (LOG_WARNING,
470 "Couldn't restore device fd flags: %m");
474 initfdflags = -1;
477 /********************************************************************
479 * clean_check - Fetch the flags for the device and generate
480 * appropriate error messages.
482 void clean_check(void)
484 int x;
485 char *s;
487 if (still_ppp())
489 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0)
491 s = NULL;
492 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP))
494 case SC_RCV_B7_0:
495 case SC_RCV_B7_0 | SC_RCV_EVNP:
496 case SC_RCV_B7_0 | SC_RCV_ODDP:
497 case SC_RCV_B7_0 | SC_RCV_ODDP | SC_RCV_EVNP:
498 s = "all had bit 7 set to 1";
499 break;
501 case SC_RCV_B7_1:
502 case SC_RCV_B7_1 | SC_RCV_EVNP:
503 case SC_RCV_B7_1 | SC_RCV_ODDP:
504 case SC_RCV_B7_1 | SC_RCV_ODDP | SC_RCV_EVNP:
505 s = "all had bit 7 set to 0";
506 break;
508 case SC_RCV_EVNP:
509 s = "all had odd parity";
510 break;
512 case SC_RCV_ODDP:
513 s = "all had even parity";
514 break;
517 if (s != NULL)
519 syslog(LOG_WARNING, "Receive serial link is not"
520 " 8-bit clean:");
521 syslog(LOG_WARNING, "Problem: %s", s);
529 * List of valid speeds.
532 struct speed {
533 int speed_int, speed_val;
534 } speeds[] = {
535 #ifdef B50
536 { 50, B50 },
537 #endif
538 #ifdef B75
539 { 75, B75 },
540 #endif
541 #ifdef B110
542 { 110, B110 },
543 #endif
544 #ifdef B134
545 { 134, B134 },
546 #endif
547 #ifdef B150
548 { 150, B150 },
549 #endif
550 #ifdef B200
551 { 200, B200 },
552 #endif
553 #ifdef B300
554 { 300, B300 },
555 #endif
556 #ifdef B600
557 { 600, B600 },
558 #endif
559 #ifdef B1200
560 { 1200, B1200 },
561 #endif
562 #ifdef B1800
563 { 1800, B1800 },
564 #endif
565 #ifdef B2000
566 { 2000, B2000 },
567 #endif
568 #ifdef B2400
569 { 2400, B2400 },
570 #endif
571 #ifdef B3600
572 { 3600, B3600 },
573 #endif
574 #ifdef B4800
575 { 4800, B4800 },
576 #endif
577 #ifdef B7200
578 { 7200, B7200 },
579 #endif
580 #ifdef B9600
581 { 9600, B9600 },
582 #endif
583 #ifdef B19200
584 { 19200, B19200 },
585 #endif
586 #ifdef B38400
587 { 38400, B38400 },
588 #endif
589 #ifdef B57600
590 { 57600, B57600 },
591 #endif
592 #ifdef B115200
593 { 115200, B115200 },
594 #endif
595 #ifdef EXTA
596 { 19200, EXTA },
597 #endif
598 #ifdef EXTB
599 { 38400, EXTB },
600 #endif
601 #ifdef B230400
602 { 230400, B230400 },
603 #endif
604 #ifdef B460800
605 { 460800, B460800 },
606 #endif
607 { 0, 0 }
610 /********************************************************************
612 * Translate from bits/second to a speed_t.
615 static int translate_speed (int bps)
617 struct speed *speedp;
619 if (bps != 0)
621 for (speedp = speeds; speedp->speed_int; speedp++)
623 if (bps == speedp->speed_int)
625 return speedp->speed_val;
628 syslog(LOG_WARNING, "speed %d not supported", bps);
630 return 0;
633 /********************************************************************
635 * Translate from a speed_t to bits/second.
638 static int baud_rate_of (int speed)
640 struct speed *speedp;
642 if (speed != 0)
644 for (speedp = speeds; speedp->speed_int; speedp++)
646 if (speed == speedp->speed_val)
648 return speedp->speed_int;
652 return 0;
655 /********************************************************************
657 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
658 * at the requested speed, etc. If `local' is true, set CLOCAL
659 * regardless of whether the modem option was specified.
662 void set_up_tty (int tty_fd, int local)
664 int speed;
665 struct termios tios;
667 if (tcgetattr(tty_fd, &tios) < 0)
669 syslog(LOG_ERR, "tcgetattr: %m(%d)", errno);
670 die(1);
673 if (!restore_term)
675 inittermios = tios;
678 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
679 tios.c_cflag |= CS8 | CREAD | HUPCL;
681 tios.c_iflag = IGNBRK | IGNPAR;
682 tios.c_oflag = 0;
683 tios.c_lflag = 0;
684 tios.c_cc[VMIN] = 1;
685 tios.c_cc[VTIME] = 0;
687 if (local || !modem)
689 tios.c_cflag ^= (CLOCAL | HUPCL);
692 switch (crtscts)
694 case 1:
695 tios.c_cflag |= CRTSCTS;
696 break;
698 case -2:
699 tios.c_iflag |= IXON | IXOFF;
700 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
701 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
702 break;
704 case -1:
705 tios.c_cflag &= ~CRTSCTS;
706 break;
708 default:
709 break;
712 speed = translate_speed(inspeed);
713 if (speed)
715 cfsetospeed (&tios, speed);
716 cfsetispeed (&tios, speed);
719 * We can't proceed if the serial port speed is B0,
720 * since that implies that the serial port is disabled.
722 else
724 speed = cfgetospeed(&tios);
725 if (speed == B0)
727 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
728 devnam);
729 die (1);
733 if (tcsetattr(tty_fd, TCSAFLUSH, &tios) < 0)
735 syslog(LOG_ERR, "tcsetattr: %m");
736 die(1);
739 baud_rate = baud_rate_of(speed);
740 restore_term = TRUE;
743 /********************************************************************
745 * setdtr - control the DTR line on the serial port.
746 * This is called from die(), so it shouldn't call die().
749 void setdtr (int tty_fd, int on)
751 int modembits = TIOCM_DTR;
753 ioctl(tty_fd, (on ? TIOCMBIS : TIOCMBIC), &modembits);
756 /********************************************************************
758 * restore_tty - restore the terminal to the saved settings.
761 void restore_tty (int tty_fd)
763 if (restore_term)
765 restore_term = 0;
767 * Turn off echoing, because otherwise we can get into
768 * a loop with the tty and the modem echoing to each other.
769 * We presume we are the sole user of this tty device, so
770 * when we close it, it will revert to its defaults anyway.
772 if (!default_device)
774 inittermios.c_lflag &= ~(ECHO | ECHONL);
777 if (tcsetattr(tty_fd, TCSAFLUSH, &inittermios) < 0)
779 if (! ok_error (errno))
781 syslog(LOG_WARNING, "tcsetattr: %m");
787 /********************************************************************
789 * output - Output PPP packet.
792 void output (int unit, unsigned char *p, int len)
794 if (debug)
796 log_packet(p, len, "sent ", LOG_DEBUG);
799 if (write(ppp_fd, p, len) < 0)
801 if (errno == EWOULDBLOCK || errno == ENOBUFS
802 || errno == ENXIO || errno == EIO)
804 syslog(LOG_WARNING, "write: warning: %m(%d)", errno);
806 else
808 syslog(LOG_ERR, "write: %m(%d)", errno);
809 die(1);
814 /********************************************************************
816 * wait_input - wait until there is data available on ppp_fd,
817 * for the length of time specified by *timo (indefinite
818 * if timo is NULL).
821 void wait_input (struct timeval *timo)
823 fd_set ready;
824 int n;
826 FD_ZERO(&ready);
827 FD_SET(ppp_fd, &ready);
829 n = select(ppp_fd + 1, &ready, NULL, &ready, timo);
830 if (n < 0 && errno != EINTR)
832 syslog(LOG_ERR, "select: %m(%d)", errno);
833 die(1);
837 /********************************************************************
839 * wait_loop_output - wait until there is data available on the
840 * loopback, for the length of time specified by *timo (indefinite
841 * if timo is NULL).
843 void wait_loop_output(timo)
844 struct timeval *timo;
846 fd_set ready;
847 int n;
849 FD_ZERO(&ready);
850 FD_SET(master_fd, &ready);
851 n = select(master_fd + 1, &ready, NULL, &ready, timo);
852 if (n < 0 && errno != EINTR)
854 syslog(LOG_ERR, "select: %m(%d)", errno);
855 die(1);
859 /********************************************************************
861 * wait_time - wait for a given length of time or until a
862 * signal is received.
865 void wait_time(timo)
866 struct timeval *timo;
868 int n;
870 n = select(0, NULL, NULL, NULL, timo);
871 if (n < 0 && errno != EINTR) {
872 syslog(LOG_ERR, "select: %m(%d)", errno);
873 die(1);
877 /********************************************************************
879 * read_packet - get a PPP packet from the serial device.
882 int read_packet (unsigned char *buf)
884 int len;
886 len = read(ppp_fd, buf, PPP_MTU + PPP_HDRLEN);
887 if (len < 0)
889 if (errno == EWOULDBLOCK)
891 return -1;
893 syslog(LOG_ERR, "read: %m(%d)", errno);
894 die(1);
896 return len;
899 /********************************************************************
901 * get_loop_output - get outgoing packets from the ppp device,
902 * and detect when we want to bring the real link up.
903 * Return value is 1 if we need to bring up the link, 0 otherwise.
906 get_loop_output(void)
908 int rv = 0;
909 int n = read(master_fd, inbuf, sizeof(inbuf));
911 while (n > 0)
913 if (loop_chars(inbuf, n))
915 rv = 1;
917 n = read(master_fd, inbuf, sizeof(inbuf));
920 if (n == 0)
922 syslog(LOG_ERR, "eof on loopback");
923 die(1);
926 if (errno != EWOULDBLOCK)
928 syslog(LOG_ERR, "read from loopback: %m(%d)", errno);
929 die(1);
932 return rv;
935 /********************************************************************
937 * ppp_send_config - configure the transmit characteristics of
938 * the ppp interface.
941 void ppp_send_config (int unit,int mtu,u_int32_t asyncmap,int pcomp,int accomp)
943 u_int x;
944 struct ifreq ifr;
946 SYSDEBUG ((LOG_DEBUG, "send_config: mtu = %d\n", mtu));
948 * Ensure that the link is still up.
950 if (still_ppp())
953 * Set the MTU and other parameters for the ppp device
955 memset (&ifr, '\0', sizeof (ifr));
956 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
957 ifr.ifr_mtu = mtu;
959 if (ioctl(sock_fd, SIOCSIFMTU, (caddr_t) &ifr) < 0)
961 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m(%d)", errno);
962 quit();
965 SYSDEBUG ((LOG_DEBUG, "send_config: asyncmap = %lx\n", asyncmap));
966 if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0)
968 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m(%d)", errno);
969 quit();
972 x = get_flags();
973 x = pcomp ? x | SC_COMP_PROT : x & ~SC_COMP_PROT;
974 x = accomp ? x | SC_COMP_AC : x & ~SC_COMP_AC;
975 set_flags(x);
979 /********************************************************************
981 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
984 void ppp_set_xaccm (int unit, ext_accm accm)
986 SYSDEBUG ((LOG_DEBUG, "set_xaccm: %08lx %08lx %08lx %08lx\n",
987 accm[0], accm[1], accm[2], accm[3]));
989 if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
991 if ( ! ok_error (errno))
993 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m(%d)", errno);
998 /********************************************************************
1000 * ppp_recv_config - configure the receive-side characteristics of
1001 * the ppp interface.
1004 void ppp_recv_config (int unit,int mru,u_int32_t asyncmap,int pcomp,int accomp)
1006 u_int x;
1008 SYSDEBUG ((LOG_DEBUG, "recv_config: mru = %d\n", mru));
1010 * If we were called because the link has gone down then there is nothing
1011 * which may be done. Just return without incident.
1013 if (!still_ppp())
1015 return;
1018 * Set the receiver parameters
1020 if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
1022 if ( ! ok_error (errno))
1024 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m(%d)", errno);
1028 SYSDEBUG ((LOG_DEBUG, "recv_config: asyncmap = %lx\n", asyncmap));
1029 if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0)
1031 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m(%d)", errno);
1032 quit();
1035 x = get_flags();
1036 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
1037 set_flags (x);
1040 /********************************************************************
1042 * ccp_test - ask kernel whether a given compression method
1043 * is acceptable for use.
1046 int ccp_test (int unit, u_char *opt_ptr, int opt_len, int for_transmit)
1048 struct ppp_option_data data;
1050 memset (&data, '\0', sizeof (data));
1051 data.ptr = opt_ptr;
1052 data.length = opt_len;
1053 data.transmit = for_transmit;
1055 if (ioctl(ppp_fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
1057 return 1;
1060 return (errno == ENOBUFS)? 0: -1;
1063 /********************************************************************
1065 * ccp_flags_set - inform kernel about the current state of CCP.
1068 void ccp_flags_set (int unit, int isopen, int isup)
1070 if (still_ppp())
1072 int x = get_flags();
1073 x = isopen? x | SC_CCP_OPEN : x &~ SC_CCP_OPEN;
1074 x = isup? x | SC_CCP_UP : x &~ SC_CCP_UP;
1075 set_flags (x);
1079 /********************************************************************
1081 * get_idle_time - return how long the link has been idle.
1084 get_idle_time(u, ip)
1085 int u;
1086 struct ppp_idle *ip;
1088 return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0;
1091 /********************************************************************
1093 * ccp_fatal_error - returns 1 if decompression was disabled as a
1094 * result of an error detected after decompression of a packet,
1095 * 0 otherwise. This is necessary because of patent nonsense.
1098 int ccp_fatal_error (int unit)
1100 int x = get_flags();
1102 return x & SC_DC_FERROR;
1106 * path_to_route - determine the path to the proc file system data
1109 FILE *route_fd = (FILE *) 0;
1110 static char route_buffer [512];
1112 static char *path_to_route (void);
1113 static int open_route_table (void);
1114 static void close_route_table (void);
1115 static int read_route_table (struct rtentry *rt);
1117 /********************************************************************
1119 * path_to_procfs - find the path to the proc file system mount point
1122 static int path_to_procfs (void)
1124 struct mntent *mntent;
1125 FILE *fp;
1127 fp = fopen (MOUNTED, "r");
1128 if (fp != 0)
1130 mntent = getmntent (fp);
1131 while (mntent != (struct mntent *) 0)
1133 if (strcmp (mntent->mnt_type, MNTTYPE_IGNORE) != 0)
1135 if (strcmp (mntent->mnt_type, "proc") == 0)
1137 strncpy (route_buffer, mntent->mnt_dir,
1138 sizeof (route_buffer)-10);
1139 route_buffer [sizeof (route_buffer)-10] = '\0';
1140 fclose (fp);
1141 return 1;
1144 mntent = getmntent (fp);
1146 fclose (fp);
1149 /* Default the mount location of /proc */
1150 strncpy (route_buffer, "/proc", sizeof (route_buffer)-10);
1151 return 1;
1154 /********************************************************************
1156 * path_to_route - find the path to the route tables in the proc file system
1159 static char *path_to_route (void)
1161 if (! path_to_procfs())
1163 syslog (LOG_ERR, "proc file system not mounted");
1164 return 0;
1166 strcat (route_buffer, "/net/route");
1167 return (route_buffer);
1170 /********************************************************************
1172 * close_route_table - close the interface to the route table
1175 static void close_route_table (void)
1177 if (route_fd != (FILE *) 0)
1179 fclose (route_fd);
1180 route_fd = (FILE *) 0;
1184 /********************************************************************
1186 * open_route_table - open the interface to the route table
1189 static int open_route_table (void)
1191 char *path;
1193 close_route_table();
1195 path = path_to_route();
1196 if (path == NULL)
1198 return 0;
1201 route_fd = fopen (path, "r");
1202 if (route_fd == (FILE *) 0)
1204 syslog (LOG_ERR, "can not open %s: %m(%d)", path, errno);
1205 return 0;
1207 return 1;
1210 /********************************************************************
1212 * read_route_table - read the next entry from the route table
1215 static int read_route_table (struct rtentry *rt)
1217 static char delims[] = " \t\n";
1218 char *dev_ptr, *dst_ptr, *gw_ptr, *flag_ptr;
1220 memset (rt, '\0', sizeof (struct rtentry));
1222 for (;;)
1224 if (fgets (route_buffer, sizeof (route_buffer), route_fd) ==
1225 (char *) 0)
1227 return 0;
1230 dev_ptr = strtok (route_buffer, delims); /* interface name */
1231 dst_ptr = strtok (NULL, delims); /* destination address */
1232 gw_ptr = strtok (NULL, delims); /* gateway */
1233 flag_ptr = strtok (NULL, delims); /* flags */
1235 if (flag_ptr == (char *) 0) /* assume that we failed, somewhere. */
1237 return 0;
1240 /* Discard that stupid header line which should never
1241 * have been there in the first place !! */
1242 if (isxdigit (*dst_ptr) && isxdigit (*gw_ptr) && isxdigit (*flag_ptr))
1244 break;
1248 ((struct sockaddr_in *) &rt->rt_dst)->sin_addr.s_addr =
1249 strtoul (dst_ptr, NULL, 16);
1251 ((struct sockaddr_in *) &rt->rt_gateway)->sin_addr.s_addr =
1252 strtoul (gw_ptr, NULL, 16);
1254 rt->rt_flags = (short) strtoul (flag_ptr, NULL, 16);
1255 rt->rt_dev = dev_ptr;
1257 return 1;
1260 /********************************************************************
1262 * defaultroute_exists - determine if there is a default route
1265 static int defaultroute_exists (struct rtentry *rt)
1267 int result = 0;
1269 if (!open_route_table())
1271 return 0;
1274 while (read_route_table(rt) != 0)
1276 if ((rt->rt_flags & RTF_UP) == 0)
1278 continue;
1281 if (((struct sockaddr_in *) (&rt->rt_dst))->sin_addr.s_addr == 0L)
1283 result = 1;
1284 break;
1288 close_route_table();
1289 return result;
1292 /********************************************************************
1294 * sifdefaultroute - assign a default route through the address given.
1297 int sifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
1299 struct rtentry rt;
1301 if (defaultroute_exists(&rt))
1303 struct in_addr old_gateway =
1304 ((struct sockaddr_in *) (&rt.rt_gateway))-> sin_addr;
1306 if (old_gateway.s_addr != gateway)
1308 syslog (LOG_ERR,
1309 "not replacing existing default route to %s [%s]",
1310 rt.rt_dev,
1311 inet_ntoa (old_gateway));
1313 return 0;
1316 memset (&rt, '\0', sizeof (rt));
1317 SET_SA_FAMILY (rt.rt_dst, AF_INET);
1318 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
1320 if (kernel_version > KVERSION(2,1,0)) {
1321 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
1322 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = 0L;
1325 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
1327 rt.rt_flags = RTF_UP | RTF_GATEWAY | RTF_DEFAULT;
1328 if (ioctl(sock_fd, SIOCADDRT, &rt) < 0)
1330 if ( ! ok_error ( errno ))
1332 syslog (LOG_ERR, "default route ioctl(SIOCADDRT): %m(%d)", errno);
1334 return 0;
1337 default_route_gateway = gateway;
1338 return 1;
1341 /********************************************************************
1343 * cifdefaultroute - delete a default route through the address given.
1346 int cifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
1348 struct rtentry rt;
1350 default_route_gateway = 0;
1352 memset (&rt, '\0', sizeof (rt));
1353 SET_SA_FAMILY (rt.rt_dst, AF_INET);
1354 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
1356 if (kernel_version > KVERSION(2,1,0)) {
1357 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
1358 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = 0L;
1361 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
1363 rt.rt_flags = RTF_UP | RTF_GATEWAY | RTF_DEFAULT;
1364 if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH)
1366 if (still_ppp())
1368 if ( ! ok_error ( errno ))
1370 syslog (LOG_ERR,
1371 "default route ioctl(SIOCDELRT): %m(%d)", errno);
1373 return 0;
1377 return 1;
1380 /********************************************************************
1382 * sifproxyarp - Make a proxy ARP entry for the peer.
1385 int sifproxyarp (int unit, u_int32_t his_adr)
1387 struct arpreq arpreq;
1389 if (has_proxy_arp == 0)
1391 memset (&arpreq, '\0', sizeof(arpreq));
1393 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1394 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
1395 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1397 * Get the hardware address of an interface on the same subnet
1398 * as our local address.
1400 if (!get_ether_addr(his_adr, &arpreq.arp_ha, arpreq.arp_dev))
1402 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
1403 return 0;
1406 if (ioctl(sock_fd, SIOCSARP, (caddr_t)&arpreq) < 0)
1408 if ( ! ok_error ( errno ))
1410 syslog(LOG_ERR, "ioctl(SIOCSARP): %m(%d)", errno);
1412 return 0;
1416 proxy_arp_addr = his_adr;
1417 has_proxy_arp = 1;
1418 return 1;
1421 /********************************************************************
1423 * cifproxyarp - Delete the proxy ARP entry for the peer.
1426 int cifproxyarp (int unit, u_int32_t his_adr)
1428 struct arpreq arpreq;
1430 if (has_proxy_arp == 1)
1432 memset (&arpreq, '\0', sizeof(arpreq));
1433 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1434 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
1435 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1437 if (ioctl(sock_fd, SIOCDARP, (caddr_t)&arpreq) < 0)
1439 if ( ! ok_error ( errno ))
1441 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m(%d)", errno);
1443 return 0;
1446 has_proxy_arp = 0;
1447 return 1;
1450 /********************************************************************
1452 * get_ether_addr - get the hardware address of an interface on the
1453 * the same subnet as ipaddr.
1456 static int get_ether_addr (u_int32_t ipaddr,
1457 struct sockaddr *hwaddr,
1458 char *name)
1460 struct ifreq *ifr, *ifend;
1461 u_int32_t ina, mask;
1462 struct ifreq ifreq;
1463 struct ifconf ifc;
1464 struct ifreq ifs[MAX_IFS];
1466 ifc.ifc_len = sizeof(ifs);
1467 ifc.ifc_req = ifs;
1468 if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0)
1470 if ( ! ok_error ( errno ))
1472 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m(%d)", errno);
1474 return 0;
1477 SYSDEBUG ((LOG_DEBUG, "proxy arp: scanning %d interfaces for IP %s",
1478 ifc.ifc_len / sizeof(struct ifreq), ip_ntoa(ipaddr)));
1480 * Scan through looking for an interface with an Internet
1481 * address on the same subnet as `ipaddr'.
1483 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
1484 for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
1486 if (ifr->ifr_addr.sa_family == AF_INET)
1488 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1489 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1490 SYSDEBUG ((LOG_DEBUG, "proxy arp: examining interface %s",
1491 ifreq.ifr_name));
1493 * Check that the interface is up, and not point-to-point
1494 * nor loopback.
1496 if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
1498 continue;
1501 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
1503 continue;
1506 * Get its netmask and check that it's on the right subnet.
1508 if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
1510 continue;
1513 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1514 SYSDEBUG ((LOG_DEBUG, "proxy arp: interface addr %s mask %lx",
1515 ip_ntoa(ina), ntohl(mask)));
1517 if (((ipaddr ^ ina) & mask) != 0)
1519 continue;
1521 break;
1525 if (ifr >= ifend)
1527 return 0;
1530 memcpy (name, ifreq.ifr_name, sizeof(ifreq.ifr_name));
1531 syslog(LOG_INFO, "found interface %s for proxy arp", name);
1533 * Now get the hardware address.
1535 memset (&ifreq.ifr_hwaddr, 0, sizeof (struct sockaddr));
1536 if (ioctl (sock_fd, SIOCGIFHWADDR, &ifreq) < 0)
1538 syslog(LOG_ERR, "SIOCGIFHWADDR(%s): %m(%d)", ifreq.ifr_name, errno);
1539 return 0;
1542 memcpy (hwaddr,
1543 &ifreq.ifr_hwaddr,
1544 sizeof (struct sockaddr));
1546 SYSDEBUG ((LOG_DEBUG,
1547 "proxy arp: found hwaddr %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1548 (int) ((unsigned char *) &hwaddr->sa_data)[0],
1549 (int) ((unsigned char *) &hwaddr->sa_data)[1],
1550 (int) ((unsigned char *) &hwaddr->sa_data)[2],
1551 (int) ((unsigned char *) &hwaddr->sa_data)[3],
1552 (int) ((unsigned char *) &hwaddr->sa_data)[4],
1553 (int) ((unsigned char *) &hwaddr->sa_data)[5],
1554 (int) ((unsigned char *) &hwaddr->sa_data)[6],
1555 (int) ((unsigned char *) &hwaddr->sa_data)[7]));
1556 return 1;
1559 /********************************************************************
1561 * Return user specified netmask, modified by any mask we might determine
1562 * for address `addr' (in network byte order).
1563 * Here we scan through the system's list of interfaces, looking for
1564 * any non-point-to-point interfaces which might appear to be on the same
1565 * network as `addr'. If we find any, we OR in their netmask to the
1566 * user-specified netmask.
1569 u_int32_t GetMask (u_int32_t addr)
1571 u_int32_t mask, nmask, ina;
1572 struct ifreq *ifr, *ifend, ifreq;
1573 struct ifconf ifc;
1574 struct ifreq ifs[MAX_IFS];
1576 addr = ntohl(addr);
1578 if (IN_CLASSA(addr)) /* determine network mask for address class */
1580 nmask = IN_CLASSA_NET;
1582 else
1584 if (IN_CLASSB(addr))
1586 nmask = IN_CLASSB_NET;
1588 else
1590 nmask = IN_CLASSC_NET;
1594 /* class D nets are disallowed by bad_ip_adrs */
1595 mask = netmask | htonl(nmask);
1597 * Scan through the system's network interfaces.
1599 ifc.ifc_len = sizeof(ifs);
1600 ifc.ifc_req = ifs;
1601 if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0)
1603 if ( ! ok_error ( errno ))
1605 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m(%d)", errno);
1607 return mask;
1610 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1611 for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
1614 * Check the interface's internet address.
1616 if (ifr->ifr_addr.sa_family != AF_INET)
1618 continue;
1620 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1621 if (((ntohl(ina) ^ addr) & nmask) != 0)
1623 continue;
1626 * Check that the interface is up, and not point-to-point nor loopback.
1628 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1629 if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
1631 continue;
1634 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
1636 continue;
1639 * Get its netmask and OR it into our mask.
1641 if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
1643 continue;
1645 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1646 break;
1648 return mask;
1651 /********************************************************************
1653 * Internal routine to decode the version.modification.patch level
1656 static void decode_version (char *buf, int *version,
1657 int *modification, int *patch)
1659 *version = (int) strtoul (buf, &buf, 10);
1660 *modification = 0;
1661 *patch = 0;
1663 if (*buf == '.')
1665 ++buf;
1666 *modification = (int) strtoul (buf, &buf, 10);
1667 if (*buf == '.')
1669 ++buf;
1670 *patch = (int) strtoul (buf, &buf, 10);
1674 if (*buf != '\0')
1676 *version =
1677 *modification =
1678 *patch = 0;
1682 /********************************************************************
1684 * Procedure to determine if the PPP line discipline is registered.
1688 ppp_registered(void)
1690 int local_fd;
1691 int init_disc = -1;
1692 int initfdflags;
1694 local_fd = open(devnam, O_NONBLOCK | O_RDWR, 0);
1695 if (local_fd < 0)
1697 syslog(LOG_ERR, "Failed to open %s: %m(%d)", devnam, errno);
1698 return 0;
1701 initfdflags = fcntl(local_fd, F_GETFL);
1702 if (initfdflags == -1)
1704 syslog(LOG_ERR, "Couldn't get device fd flags: %m(%d)", errno);
1705 close (local_fd);
1706 return 0;
1709 initfdflags &= ~O_NONBLOCK;
1710 fcntl(local_fd, F_SETFL, initfdflags);
1712 * Read the initial line dicipline and try to put the device into the
1713 * PPP dicipline.
1715 if (ioctl(local_fd, TIOCGETD, &init_disc) < 0)
1717 syslog(LOG_ERR, "ioctl(TIOCGETD): %m(%d)", errno);
1718 close (local_fd);
1719 return 0;
1722 if (ioctl(local_fd, TIOCSETD, &ppp_disc) < 0)
1724 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
1725 close (local_fd);
1726 return 0;
1729 if (ioctl(local_fd, TIOCSETD, &init_disc) < 0)
1731 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
1732 close (local_fd);
1733 return 0;
1736 close (local_fd);
1737 return 1;
1740 /********************************************************************
1742 * ppp_available - check whether the system has any ppp interfaces
1743 * (in fact we check whether we can do an ioctl on ppp0).
1746 int ppp_available(void)
1748 int s, ok;
1749 struct ifreq ifr;
1750 int size;
1751 int my_version, my_modification, my_patch;
1752 extern char *no_ppp_msg;
1754 * Open a socket for doing the ioctl operations.
1756 s = socket(AF_INET, SOCK_DGRAM, 0);
1757 if (s < 0)
1759 return 0;
1762 strncpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
1763 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1765 * If the device did not exist then attempt to create one by putting the
1766 * current tty into the PPP discipline. If this works then obtain the
1767 * flags for the device again.
1769 if (!ok)
1771 if (ppp_registered())
1773 strncpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
1774 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1778 * Ensure that the hardware address is for PPP and not something else
1780 if (ok)
1782 ok = ioctl (s, SIOCGIFHWADDR, (caddr_t) &ifr) >= 0;
1785 if (ok && ((ifr.ifr_hwaddr.sa_family & ~0xFF) != ARPHRD_PPP))
1787 ok = 0;
1790 if (!ok)
1792 no_ppp_msg =
1793 "This system lacks kernel support for PPP. This could be because\n"
1794 "the PPP kernel module is not loaded, or because the kernel is\n"
1795 "not configured for PPP. See the README.linux file in the\n"
1796 "ppp-2.3.5 distribution.\n";
1799 * This is the PPP device. Validate the version of the driver at this
1800 * point to ensure that this program will work with the driver.
1802 else
1804 char abBuffer [1024];
1806 ifr.ifr_data = abBuffer;
1807 size = ioctl (s, SIOCGPPPVER, (caddr_t) &ifr);
1808 if (size < 0) {
1809 syslog(LOG_ERR, "Couldn't read driver version: %m");
1810 ok = 0;
1811 no_ppp_msg = "Sorry, couldn't verify kernel driver version\n";
1813 } else {
1814 decode_version(abBuffer,
1815 &driver_version,
1816 &driver_modification,
1817 &driver_patch);
1819 * Validate the version of the driver against the version that we used.
1821 decode_version(VERSION,
1822 &my_version,
1823 &my_modification,
1824 &my_patch);
1826 /* The version numbers must match */
1827 if (driver_version != my_version)
1829 ok = 0;
1832 /* The modification levels must be legal */
1833 if (driver_modification < my_modification)
1835 if (driver_modification >= 2) {
1836 /* we can cope with 2.2.0 and above */
1837 driver_is_old = 1;
1838 } else {
1839 ok = 0;
1843 close (s);
1844 if (!ok)
1846 sprintf (route_buffer,
1847 "Sorry - PPP driver version %d.%d.%d is out of date\n",
1848 driver_version, driver_modification, driver_patch);
1850 no_ppp_msg = route_buffer;
1854 return ok;
1857 /********************************************************************
1859 * Update the wtmp file with the appropriate user name and tty device.
1862 void logwtmp (const char *line, const char *name, const char *host)
1864 int wtmp;
1865 struct utmp ut, *utp;
1866 pid_t mypid = getpid();
1868 * Update the signon database for users.
1869 * Christoph Lameter: Copied from poeigl-1.36 Jan 3, 1996
1871 utmpname(_PATH_UTMP);
1872 setutent();
1873 while ((utp = getutent()) && (utp->ut_pid != mypid))
1874 /* nothing */;
1876 /* Is this call really necessary? There is another one after the 'put' */
1877 endutent();
1879 if (utp)
1881 memcpy(&ut, utp, sizeof(ut));
1883 else
1885 /* some gettys/telnetds don't initialize utmp... */
1886 memset(&ut, 0, sizeof(ut));
1889 if (ut.ut_id[0] == 0)
1891 strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
1894 strncpy(ut.ut_user, name, sizeof(ut.ut_user));
1895 strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1897 time(&ut.ut_time);
1899 ut.ut_type = USER_PROCESS;
1900 ut.ut_pid = mypid;
1902 /* Insert the host name if one is supplied */
1903 if (*host)
1905 strncpy (ut.ut_host, host, sizeof(ut.ut_host));
1908 /* Insert the IP address of the remote system if IP is enabled */
1909 if (ipcp_protent.enabled_flag && ipcp_hisoptions[0].neg_addr)
1911 memcpy (&ut.ut_addr, (char *) &ipcp_hisoptions[0].hisaddr,
1912 sizeof(ut.ut_addr));
1915 /* CL: Makes sure that the logout works */
1916 if (*host == 0 && *name==0)
1918 ut.ut_host[0]=0;
1921 pututline(&ut);
1922 endutent();
1924 * Update the wtmp file.
1926 wtmp = open(_PATH_WTMP, O_APPEND|O_WRONLY);
1927 if (wtmp >= 0)
1929 flock(wtmp, LOCK_EX);
1931 /* we really should check for error on the write for a full disk! */
1932 write (wtmp, (char *)&ut, sizeof(ut));
1933 close (wtmp);
1935 flock(wtmp, LOCK_UN);
1939 /********************************************************************
1940 * Code for locking/unlocking the serial device.
1941 * This code is derived from chat.c.
1945 * lock - create a lock file for the named device
1948 int lock (char *dev)
1950 #ifdef LOCKLIB
1951 int result;
1952 lock_file = malloc(strlen(dev) + 1);
1953 if (lock_file == NULL)
1955 novm("lock file name");
1957 strcpy (lock_file, dev);
1958 result = mklock (dev, (void *) 0);
1960 if (result > 0)
1962 syslog (LOG_NOTICE, "Device %s is locked by pid %d", dev, result);
1963 free (lock_file);
1964 lock_file = NULL;
1965 result = -1;
1967 else
1969 if (result < 0)
1971 syslog (LOG_ERR, "Can't create lock file %s", lock_file);
1972 free (lock_file);
1973 lock_file = NULL;
1974 result = -1;
1977 return (result);
1978 #else
1979 char hdb_lock_buffer[12];
1980 int fd, n;
1981 int pid = getpid();
1982 char *p;
1984 p = strrchr(dev, '/');
1985 if (p != NULL)
1987 dev = ++p;
1990 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1991 if (lock_file == NULL)
1993 novm("lock file name");
1996 strcpy (lock_file, LOCK_PREFIX);
1997 strcat (lock_file, dev);
1999 * Attempt to create the lock file at this point.
2001 while (1)
2003 fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644);
2004 if (fd >= 0)
2006 pid = getpid();
2007 #ifndef PID_BINARY
2008 sprintf (hdb_lock_buffer, "%010d\n", pid);
2009 write (fd, hdb_lock_buffer, 11);
2010 #else
2011 write(fd, &pid, sizeof (pid));
2012 #endif
2013 close(fd);
2014 return 0;
2017 * If the file exists then check to see if the pid is stale
2019 if (errno == EEXIST)
2021 fd = open(lock_file, O_RDONLY, 0);
2022 if (fd < 0)
2024 if (errno == ENOENT) /* This is just a timing problem. */
2026 continue;
2028 break;
2031 /* Read the lock file to find out who has the device locked */
2032 n = read (fd, hdb_lock_buffer, 11);
2033 close (fd);
2034 if (n < 0)
2036 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
2037 break;
2040 /* See the process still exists. */
2041 if (n > 0)
2043 #ifndef PID_BINARY
2044 hdb_lock_buffer[n] = '\0';
2045 sscanf (hdb_lock_buffer, " %d", &pid);
2046 #else
2047 pid = ((int *) hdb_lock_buffer)[0];
2048 #endif
2049 if (pid == 0 || pid == getpid()
2050 || (kill(pid, 0) == -1 && errno == ESRCH))
2052 n = 0;
2056 /* If the process does not exist then try to remove the lock */
2057 if (n == 0 && unlink (lock_file) == 0)
2059 syslog (LOG_NOTICE, "Removed stale lock on %s (pid %d)",
2060 dev, pid);
2061 continue;
2064 syslog (LOG_NOTICE, "Device %s is locked by pid %d", dev, pid);
2065 break;
2068 syslog(LOG_ERR, "Can't create lock file %s: %m(%d)", lock_file, errno);
2069 break;
2072 free(lock_file);
2073 lock_file = NULL;
2074 return -1;
2075 #endif
2079 /********************************************************************
2081 * unlock - remove our lockfile
2084 void unlock(void)
2086 if (lock_file)
2088 #ifdef LOCKLIB
2089 (void) rmlock (lock_file, (void *) 0);
2090 #else
2091 unlink(lock_file);
2092 #endif
2093 free(lock_file);
2094 lock_file = NULL;
2098 /********************************************************************
2100 * sifvjcomp - config tcp header compression
2103 int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid)
2105 u_int x = get_flags();
2107 if (vjcomp)
2109 if (ioctl (ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0)
2111 if (! ok_error (errno))
2113 syslog (LOG_ERR, "ioctl(PPPIOCSMAXCID): %m(%d)", errno);
2115 vjcomp = 0;
2119 x = vjcomp ? x | SC_COMP_TCP : x &~ SC_COMP_TCP;
2120 x = cidcomp ? x & ~SC_NO_TCP_CCID : x | SC_NO_TCP_CCID;
2121 set_flags (x);
2123 return 1;
2126 /********************************************************************
2128 * sifup - Config the interface up and enable IP packets to pass.
2131 int sifup (int u)
2133 struct ifreq ifr;
2135 memset (&ifr, '\0', sizeof (ifr));
2136 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2137 if (ioctl(sock_fd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0)
2139 if (! ok_error (errno))
2141 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m(%d)", errno);
2143 return 0;
2146 ifr.ifr_flags |= (IFF_UP | IFF_POINTOPOINT);
2147 if (ioctl(sock_fd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0)
2149 if (! ok_error (errno))
2151 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m(%d)", errno);
2153 return 0;
2155 if_is_up = 1;
2156 return 1;
2159 /********************************************************************
2161 * sifdown - Config the interface down and disable IP.
2164 int sifdown (int u)
2166 struct ifreq ifr;
2168 if_is_up = 0;
2170 memset (&ifr, '\0', sizeof (ifr));
2171 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2172 if (ioctl(sock_fd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0)
2174 if (! ok_error (errno))
2176 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m(%d)", errno);
2178 return 0;
2181 ifr.ifr_flags &= ~IFF_UP;
2182 ifr.ifr_flags |= IFF_POINTOPOINT;
2183 if (ioctl(sock_fd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0)
2185 if (! ok_error (errno))
2187 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m(%d)", errno);
2189 return 0;
2191 return 1;
2194 /********************************************************************
2196 * sifaddr - Config the interface IP addresses and netmask.
2199 int sifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr,
2200 u_int32_t net_mask)
2202 struct ifreq ifr;
2203 struct rtentry rt;
2205 memset (&ifr, '\0', sizeof (ifr));
2206 memset (&rt, '\0', sizeof (rt));
2208 SET_SA_FAMILY (ifr.ifr_addr, AF_INET);
2209 SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET);
2210 SET_SA_FAMILY (ifr.ifr_netmask, AF_INET);
2212 strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2214 * Set our IP address
2216 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = our_adr;
2217 if (ioctl(sock_fd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
2219 if (errno != EEXIST)
2221 if (! ok_error (errno))
2223 syslog (LOG_ERR, "ioctl(SIOCAIFADDR): %m(%d)", errno);
2226 else
2228 syslog (LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
2230 return (0);
2233 * Set the gateway address
2235 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = his_adr;
2236 if (ioctl(sock_fd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0)
2238 if (! ok_error (errno))
2240 syslog (LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m(%d)", errno);
2242 return (0);
2245 * Set the netmask.
2246 * For recent kernels, force the netmask to 255.255.255.255.
2248 if (kernel_version >= KVERSION(2,1,16))
2249 net_mask = ~0L;
2250 if (net_mask != 0)
2252 ((struct sockaddr_in *) &ifr.ifr_netmask)->sin_addr.s_addr = net_mask;
2253 if (ioctl(sock_fd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0)
2255 if (! ok_error (errno))
2257 syslog (LOG_ERR, "ioctl(SIOCSIFNETMASK): %m(%d)", errno);
2259 return (0);
2263 * Add the device route
2265 if (kernel_version < KVERSION(2,1,16)) {
2266 SET_SA_FAMILY (rt.rt_dst, AF_INET);
2267 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
2268 rt.rt_dev = ifname;
2270 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0L;
2271 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
2272 rt.rt_flags = RTF_UP | RTF_HOST;
2274 if (kernel_version > KVERSION(2,1,0)) {
2275 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
2276 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = -1L;
2279 if (ioctl(sock_fd, SIOCADDRT, &rt) < 0)
2281 if (! ok_error (errno))
2283 syslog (LOG_ERR, "ioctl(SIOCADDRT) device route: %m(%d)", errno);
2285 return (0);
2288 return 1;
2291 /********************************************************************
2293 * cifaddr - Clear the interface IP addresses, and delete routes
2294 * through the interface if possible.
2297 int cifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr)
2299 struct rtentry rt;
2301 if (kernel_version < KVERSION(2,1,16)) {
2303 * Delete the route through the device
2305 memset (&rt, '\0', sizeof (rt));
2307 SET_SA_FAMILY (rt.rt_dst, AF_INET);
2308 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
2309 rt.rt_dev = ifname;
2311 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
2312 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
2313 rt.rt_flags = RTF_UP | RTF_HOST;
2315 if (kernel_version > KVERSION(2,1,0)) {
2316 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
2317 ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = -1L;
2320 if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH)
2322 if (still_ppp() && ! ok_error (errno))
2324 syslog (LOG_ERR, "ioctl(SIOCDELRT) device route: %m(%d)", errno);
2326 return (0);
2329 return 1;
2332 /********************************************************************
2334 * open_loopback - open the device we use for getting packets
2335 * in demand mode. Under Linux, we use our existing fd
2336 * to the ppp driver.
2338 void
2339 open_ppp_loopback(void)
2341 int flags, i;
2342 struct termios tios;
2344 master_fd = -1;
2345 for (i = 0; i < 64; ++i) {
2346 sprintf(loop_name, "/dev/pty%c%x", 'p' + i / 16, i % 16);
2347 master_fd = open(loop_name, O_RDWR | O_NOCTTY, 0);
2348 if (master_fd >= 0)
2349 break;
2351 if (master_fd < 0) {
2352 syslog(LOG_ERR, "No free pty for loopback");
2353 die(1);
2355 SYSDEBUG((LOG_DEBUG, "using %s for loopback", loop_name));
2356 loop_name[5] = 't';
2357 slave_fd = open(loop_name, O_RDWR | O_NOCTTY, 0);
2358 if (slave_fd < 0) {
2359 syslog(LOG_ERR, "Couldn't open %s for loopback: %m", loop_name);
2360 die(1);
2363 set_ppp_fd(slave_fd);
2365 if (tcgetattr(ppp_fd, &tios) == 0)
2367 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
2368 tios.c_cflag |= CS8 | CREAD;
2369 tios.c_iflag = IGNPAR | CLOCAL;
2370 tios.c_oflag = 0;
2371 tios.c_lflag = 0;
2372 if (tcsetattr(ppp_fd, TCSAFLUSH, &tios) < 0)
2374 syslog(LOG_WARNING, "couldn't set attributes on loopback: %m(%d)", errno);
2378 flags = fcntl(master_fd, F_GETFL);
2379 if (flags == -1 ||
2380 fcntl(master_fd, F_SETFL, flags | O_NONBLOCK) == -1)
2382 syslog(LOG_WARNING, "couldn't set master loopback to nonblock: %m(%d)", errno);
2385 flags = fcntl(ppp_fd, F_GETFL);
2386 if (flags == -1 ||
2387 fcntl(ppp_fd, F_SETFL, flags | O_NONBLOCK) == -1)
2389 syslog(LOG_WARNING, "couldn't set slave loopback to nonblock: %m(%d)", errno);
2392 if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0)
2394 syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
2395 die(1);
2398 * Find out which interface we were given.
2400 if (ioctl(ppp_fd, PPPIOCGUNIT, &ifunit) < 0)
2402 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m(%d)", errno);
2403 die(1);
2406 * Enable debug in the driver if requested.
2408 set_kdebugflag (kdebugflag);
2411 /********************************************************************
2413 * restore_loop - reattach the ppp unit to the loopback.
2415 * The kernel ppp driver automatically reattaches the ppp unit to
2416 * the loopback if the serial port is set to a line discipline other
2417 * than ppp, or if it detects a modem hangup. The former will happen
2418 * in disestablish_ppp if the latter hasn't already happened, so we
2419 * shouldn't need to do anything.
2421 * Just to be sure, set the real serial port to the normal discipline.
2424 void
2425 restore_loop(void)
2427 if (ppp_fd != slave_fd)
2429 (void) ioctl(ppp_fd, TIOCSETD, &tty_disc);
2430 set_ppp_fd(slave_fd);
2434 /********************************************************************
2436 * sifnpmode - Set the mode for handling packets for a given NP.
2440 sifnpmode(u, proto, mode)
2441 int u;
2442 int proto;
2443 enum NPmode mode;
2445 struct npioctl npi;
2447 npi.protocol = proto;
2448 npi.mode = mode;
2449 if (ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi) < 0)
2451 if (! ok_error (errno))
2453 syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE, %d, %d): %m(%d)",
2454 proto, mode, errno);
2455 syslog(LOG_ERR, "ppp_fd=%d slave_fd=%d\n", ppp_fd, slave_fd);
2457 return 0;
2459 return 1;
2463 /********************************************************************
2465 * sipxfaddr - Config the interface IPX networknumber
2468 int sipxfaddr (int unit, unsigned long int network, unsigned char * node )
2470 int result = 1;
2472 #ifdef IPX_CHANGE
2473 int skfd;
2474 struct ifreq ifr;
2475 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
2477 skfd = socket (AF_IPX, SOCK_DGRAM, 0);
2478 if (skfd < 0)
2480 if (! ok_error (errno))
2482 syslog (LOG_DEBUG, "socket(AF_IPX): %m(%d)", errno);
2484 result = 0;
2486 else
2488 memset (&ifr, '\0', sizeof (ifr));
2489 strcpy (ifr.ifr_name, ifname);
2491 memcpy (sipx->sipx_node, node, IPX_NODE_LEN);
2492 sipx->sipx_family = AF_IPX;
2493 sipx->sipx_port = 0;
2494 sipx->sipx_network = htonl (network);
2495 sipx->sipx_type = IPX_FRAME_ETHERII;
2496 sipx->sipx_action = IPX_CRTITF;
2498 * Set the IPX device
2500 if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
2502 result = 0;
2503 if (errno != EEXIST)
2505 if (! ok_error (errno))
2507 syslog (LOG_DEBUG,
2508 "ioctl(SIOCAIFADDR, CRTITF): %m(%d)", errno);
2511 else
2513 syslog (LOG_WARNING,
2514 "ioctl(SIOCAIFADDR, CRTITF): Address already exists");
2517 close (skfd);
2519 #endif
2520 return result;
2523 /********************************************************************
2525 * cipxfaddr - Clear the information for the IPX network. The IPX routes
2526 * are removed and the device is no longer able to pass IPX
2527 * frames.
2530 int cipxfaddr (int unit)
2532 int result = 1;
2534 #ifdef IPX_CHANGE
2535 int skfd;
2536 struct ifreq ifr;
2537 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
2539 skfd = socket (AF_IPX, SOCK_DGRAM, 0);
2540 if (skfd < 0)
2542 if (! ok_error (errno))
2544 syslog (LOG_DEBUG, "socket(AF_IPX): %m(%d)", errno);
2546 result = 0;
2548 else
2550 memset (&ifr, '\0', sizeof (ifr));
2551 strcpy (ifr.ifr_name, ifname);
2553 sipx->sipx_type = IPX_FRAME_ETHERII;
2554 sipx->sipx_action = IPX_DLTITF;
2555 sipx->sipx_family = AF_IPX;
2557 * Set the IPX device
2559 if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
2561 if (! ok_error (errno))
2563 syslog (LOG_INFO,
2564 "ioctl(SIOCAIFADDR, IPX_DLTITF): %m(%d)", errno);
2566 result = 0;
2568 close (skfd);
2570 #endif
2571 return result;
2575 * daemon - Detach us from controlling terminal session.
2578 daemon(nochdir, noclose)
2579 int nochdir, noclose;
2581 int pid;
2583 if ((pid = fork()) < 0)
2584 return -1;
2585 if (pid != 0)
2586 exit(0); /* parent dies */
2587 setsid();
2588 if (!nochdir)
2589 chdir("/");
2590 if (!noclose) {
2591 fclose(stdin); /* don't need stdin, stdout, stderr */
2592 fclose(stdout);
2593 fclose(stderr);
2595 return 0;
2599 * Use the hostname as part of the random number seed.
2602 get_host_seed()
2604 int h;
2605 char *p = hostname;
2607 h = 407;
2608 for (p = hostname; *p != 0; ++p)
2609 h = h * 37 + *p;
2610 return h;
2613 /********************************************************************
2615 * sys_check_options - check the options that the user specified
2618 void
2619 sys_check_options(void)
2621 #ifdef IPX_CHANGE
2622 struct stat stat_buf;
2624 * Disable the IPX protocol if the support is not present in the kernel.
2625 * If we disable it then ensure that IP support is enabled.
2627 while (ipxcp_protent.enabled_flag)
2629 if (path_to_procfs())
2631 strcat (route_buffer, "/net/ipx_interface");
2632 if (lstat (route_buffer, &stat_buf) >= 0)
2634 break;
2637 syslog (LOG_ERR, "IPX support is not present in the kernel\n");
2638 ipxcp_protent.enabled_flag = 0;
2639 ipcp_protent.enabled_flag = 1;
2640 break;
2642 #endif
2643 if (demand && driver_is_old) {
2644 option_error("demand dialling is not supported by kernel driver version "
2645 "%d.%d.%d", driver_version, driver_modification,
2646 driver_patch);
2647 demand = 0;