2 * sys-linux.c - System-dependent procedures for setting up
3 * PPP interfaces on Linux systems
5 * Copyright (c) 1994-2004 Paul Mackerras. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. The name(s) of the authors of this software must not be used to
15 * endorse or promote products derived from this software without
16 * prior written permission.
18 * 3. Redistributions of any form whatsoever must retain the following
20 * "This product includes software developed by Paul Mackerras
21 * <paulus@samba.org>".
23 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
24 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
25 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
26 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
27 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
28 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
29 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31 * Derived from main.c and pppd.h, which are:
33 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
47 * 3. The name "Carnegie Mellon University" must not be used to
48 * endorse or promote products derived from this software without
49 * prior written permission. For permission or any legal
50 * details, please contact
51 * Office of Technology Transfer
52 * Carnegie Mellon University
54 * Pittsburgh, PA 15213-3890
55 * (412) 268-4387, fax: (412) 268-7395
56 * tech-transfer@andrew.cmu.edu
58 * 4. Redistributions of any form whatsoever must retain the following
60 * "This product includes software developed by Computing Services
61 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
63 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
64 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
65 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
66 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
67 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
68 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
69 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
72 #include <sys/ioctl.h>
73 #include <sys/types.h>
74 #include <sys/socket.h>
78 #include <sys/utsname.h>
79 #include <sys/sysmacros.h>
96 /* This is in netdevice.h. However, this compile will fail miserably if
97 you attempt to include netdevice.h because it has so many references
98 to __memcpy functions which it should not attempt to do. So, since I
99 really don't use it, but it must be defined, define it now. */
102 #define MAX_ADDR_LEN 7
105 #if !defined(__GLIBC__) || __GLIBC__ >= 2
106 #include <asm/types.h> /* glibc 2 conflicts with linux/types.h */
107 #include <asm/ioctls.h>
109 #include <net/if_arp.h>
110 #include <net/route.h>
111 #include <netinet/if_ether.h>
113 #include <linux/types.h>
114 #include <linux/if.h>
115 #include <linux/if_arp.h>
116 #include <linux/route.h>
117 #include <linux/if_ether.h>
119 #include <netinet/in.h>
120 #include <arpa/inet.h>
122 #include <linux/ppp_defs.h>
123 #include <linux/if_ppp.h>
126 #include <linux/netlink.h>
127 #include <linux/rtnetlink.h>
128 #include <linux/if_addr.h>
129 /* glibc versions prior to 2.24 do not define SOL_NETLINK */
131 #define SOL_NETLINK 270
133 /* linux kernel versions prior to 4.3 do not define/support NETLINK_CAP_ACK */
134 #ifndef NETLINK_CAP_ACK
135 #define NETLINK_CAP_ACK 10
145 #if __GLIBC__ >= 2 && \
146 !(defined(__powerpc__) && __GLIBC__ == 2 && __GLIBC_MINOR__ == 0)
147 #include <netipx/ipx.h>
149 #include <linux/ipx.h>
151 #endif /* IPX_CHANGE */
154 #include <pcap-bpf.h>
155 #include <linux/filter.h>
156 #endif /* PPP_FILTER */
159 #include <sys/locks.h>
163 #define BOTHER 0010000
166 unsigned int c_iflag;
167 unsigned int c_oflag;
168 unsigned int c_cflag;
169 unsigned int c_lflag;
170 unsigned char c_line;
171 unsigned char c_cc[19];
172 unsigned int c_ispeed;
173 unsigned int c_ospeed;
179 * This is in linux/include/net/ipv6.h.
183 struct in6_addr ifr6_addr;
184 __u32 ifr6_prefixlen;
185 unsigned int ifr6_ifindex;
189 #define IN6_LLADDR_FROM_EUI64(sin6, eui64) do { \
190 memset(&(sin6).s6_addr, 0, sizeof(struct in6_addr)); \
191 (sin6).s6_addr16[0] = htons(0xfe80); \
192 eui64_copy(eui64, (sin6).s6_addr32[2]); \
195 static const eui64_t nulleui64;
198 /* We can get an EIO error on an ioctl if the modem has hung up */
199 #define ok_error(num) ((num)==EIO)
201 static int tty_disc = N_TTY; /* The TTY discipline */
202 static int ppp_disc = N_PPP; /* The PPP discpline */
203 static int initfdflags = -1; /* Initial file descriptor flags for fd */
204 static int ppp_fd = -1; /* fd which is set to PPP discipline */
205 static int sock_fd = -1; /* socket for doing interface ioctls */
206 static int slave_fd = -1; /* pty for old-style demand mode, slave */
207 static int master_fd = -1; /* pty for old-style demand mode, master */
209 static int sock6_fd = -1;
213 * For the old-style kernel driver, this is the same as ppp_fd.
214 * For the new-style driver, it is the fd of an instance of /dev/ppp
215 * which is attached to the ppp unit and is used for controlling it.
217 int ppp_dev_fd = -1; /* fd for /dev/ppp (new style driver) */
219 static int chindex; /* channel index (new style driver) */
221 static fd_set in_fds; /* set of fds that wait_input waits for */
222 static int max_in_fd; /* highest fd set in in_fds */
224 static int has_proxy_arp = 0;
225 static int driver_version = 0;
226 static int driver_modification = 0;
227 static int driver_patch = 0;
228 static int driver_is_old = 0;
229 static int restore_term = 0; /* 1 => we've munged the terminal */
230 static struct termios inittermios; /* Initial TTY termios */
232 int new_style_driver = 0;
234 static char loop_name[20];
235 static unsigned char inbuf[512]; /* buffer for chars read from loopback */
237 static int if_is_up; /* Interface has been marked up */
238 static int if6_is_up; /* Interface has been marked up for IPv6, to help differentiate */
239 static int have_default_route; /* Gateway for default route added */
240 static int have_default_route6; /* Gateway for default IPv6 route added */
241 static struct rtentry old_def_rt; /* Old default route */
242 static int default_rt_repl_rest; /* replace and restore old default rt */
243 static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
244 static char proxy_arp_dev[16]; /* Device for proxy arp entry */
245 static u_int32_t our_old_addr; /* for detecting address changes */
246 static int dynaddr_set; /* 1 if ip_dynaddr set */
247 static int looped; /* 1 if using loop */
248 static int link_mtu; /* mtu for the link (not bundle) */
250 static struct utsname utsname; /* for the kernel version */
251 static int kernel_version;
252 #define KVERSION(j,n,p) ((j)*1000000 + (n)*1000 + (p))
256 #define FLAGS_GOOD (IFF_UP | IFF_BROADCAST)
257 #define FLAGS_MASK (IFF_UP | IFF_BROADCAST | \
258 IFF_POINTOPOINT | IFF_LOOPBACK | IFF_NOARP)
260 #define SIN_ADDR(x) (((struct sockaddr_in *) (&(x)))->sin_addr.s_addr)
262 /* Prototypes for procedures local to this file. */
263 static int modify_flags(int fd, int clear_bits, int set_bits);
264 static int translate_speed (int bps);
265 static int baud_rate_of (int speed);
266 static void close_route_table (void);
267 static int open_route_table (void);
268 static int read_route_table (struct rtentry *rt);
269 static int defaultroute_exists (struct rtentry *rt, int metric);
270 static int defaultroute6_exists (struct in6_rtmsg *rt, int metric);
271 static int get_ether_addr (u_int32_t ipaddr, struct sockaddr *hwaddr,
272 char *name, int namelen);
273 static void decode_version (char *buf, int *version, int *mod, int *patch);
274 static int set_kdebugflag(int level);
275 static int ppp_registered(void);
276 static int make_ppp_unit(void);
277 static int setifstate (int u, int state);
279 extern u_char inpacket_buf[]; /* borrowed from main.c */
281 extern int dfl_route_metric;
284 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
288 #define SET_SA_FAMILY(addr, family) \
289 memset ((char *) &(addr), '\0', sizeof(addr)); \
290 addr.sa_family = (family);
293 * Determine if the PPP connection should still be present.
298 /* new_fd is the fd of a tty */
299 static void set_ppp_fd (int new_fd)
302 if (!new_style_driver)
306 static int still_ppp(void)
308 if (new_style_driver)
309 return !hungup && ppp_fd >= 0;
310 if (!hungup || ppp_fd == slave_fd)
313 set_ppp_fd(slave_fd);
320 * modify_flags - set and clear flag bits controlling the kernel
323 static int modify_flags(int fd, int clear_bits, int set_bits)
327 if (ioctl(fd, PPPIOCGFLAGS, &flags) == -1)
329 flags = (flags & ~clear_bits) | set_bits;
330 if (ioctl(fd, PPPIOCSFLAGS, &flags) == -1)
337 error("Failed to set PPP kernel option flags: %m");
341 /********************************************************************
343 * sys_init - System-dependent initialization.
348 /* Get an internet socket for doing socket ioctls. */
349 sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
351 fatal("Couldn't create IP socket: %m(%d)", errno);
354 sock6_fd = socket(AF_INET6, SOCK_DGRAM, 0);
356 sock6_fd = -errno; /* save errno for later */
363 /********************************************************************
365 * sys_cleanup - restore any system state we modified before exiting:
366 * mark the interface down, delete default route and/or proxy arp entry.
367 * This shouldn't call die() because it's called from die().
370 void sys_cleanup(void)
373 * Take down the device
383 * Delete any routes through the device.
385 if (have_default_route)
386 cifdefaultroute(0, 0, 0);
388 if (have_default_route6)
389 cif6defaultroute(0, nulleui64, nulleui64);
393 cifproxyarp(0, proxy_arp_addr);
396 /********************************************************************
398 * sys_close - Clean up in a child process before execing.
403 if (new_style_driver && ppp_dev_fd >= 0)
417 /********************************************************************
419 * set_kdebugflag - Define the debugging level for the kernel
422 static int set_kdebugflag (int requested_level)
426 if (ioctl(ppp_dev_fd, PPPIOCSDEBUG, &requested_level) < 0) {
427 if ( ! ok_error (errno) )
428 error("ioctl(PPPIOCSDEBUG): %m (line %d)", __LINE__);
434 /********************************************************************
436 * tty_establish_ppp - Turn the serial port into a ppp interface.
439 int tty_establish_ppp (int tty_fd)
444 * Ensure that the tty device is in exclusive mode.
446 if (ioctl(tty_fd, TIOCEXCL, 0) < 0) {
447 if ( ! ok_error ( errno ))
448 warn("Couldn't make tty exclusive: %m");
451 * Demand mode - prime the old ppp device to relinquish the unit.
453 if (!new_style_driver && looped
454 && ioctl(slave_fd, PPPIOCXFERUNIT, 0) < 0) {
455 error("ioctl(transfer ppp unit): %m, line %d", __LINE__);
459 * Set the current tty to the PPP discpline
463 #define N_SYNC_PPP 14
465 ppp_disc = (new_style_driver && sync_serial)? N_SYNC_PPP: N_PPP;
466 if (ioctl(tty_fd, TIOCSETD, &ppp_disc) < 0) {
467 if ( ! ok_error (errno) ) {
468 error("Couldn't set tty to PPP discipline: %m");
473 ret_fd = generic_establish_ppp(tty_fd);
475 #define SC_RCVB (SC_RCV_B7_0 | SC_RCV_B7_1 | SC_RCV_EVNP | SC_RCV_ODDP)
476 #define SC_LOGB (SC_DEBUG | SC_LOG_INPKT | SC_LOG_OUTPKT | SC_LOG_RAWIN \
480 modify_flags(ppp_fd, SC_RCVB | SC_LOGB,
481 (kdebugflag * SC_DEBUG) & SC_LOGB);
483 if (ioctl(tty_fd, TIOCSETD, &tty_disc) < 0 && !ok_error(errno))
484 warn("Couldn't reset tty to normal line discipline: %m");
490 /********************************************************************
492 * generic_establish_ppp - Turn the fd into a ppp interface.
494 int generic_establish_ppp (int fd)
498 if (new_style_driver) {
501 /* If a ppp_fd is already open, close it first */
508 /* Open an instance of /dev/ppp and connect the channel to it */
509 if (ioctl(fd, PPPIOCGCHAN, &chindex) == -1) {
510 error("Couldn't get channel number: %m");
513 dbglog("using channel %d", chindex);
514 fd = open("/dev/ppp", O_RDWR);
516 error("Couldn't reopen /dev/ppp: %m");
519 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
520 if (ioctl(fd, PPPIOCATTCHAN, &chindex) < 0) {
521 error("Couldn't attach to channel %d: %m", chindex);
524 flags = fcntl(fd, F_GETFL);
525 if (flags == -1 || fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
526 warn("Couldn't set /dev/ppp (channel) to nonblock: %m");
531 if (!looped && !multilink) {
533 * Create a new PPP unit.
535 if (make_ppp_unit() < 0)
540 modify_flags(ppp_dev_fd, SC_LOOP_TRAFFIC, 0);
544 if (ioctl(fd, PPPIOCCONNECT, &ifunit) < 0) {
545 error("Couldn't attach to PPP unit %d: %m", ifunit);
552 * Old-style driver: find out which interface we were given.
555 if (ioctl(fd, PPPIOCGUNIT, &x) < 0) {
556 if (ok_error (errno))
558 fatal("ioctl(PPPIOCGUNIT): %m (line %d)", __LINE__);
560 /* Check that we got the same unit again. */
561 if (looped && x != ifunit)
562 fatal("transfer_ppp failed: wanted unit %d, got %d", ifunit, x);
566 * Fetch the initial file flags and reset blocking mode on the file.
568 initfdflags = fcntl(fd, F_GETFL);
569 if (initfdflags == -1 ||
570 fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
571 if ( ! ok_error (errno))
572 warn("Couldn't set device to non-blocking mode: %m");
577 * Enable debug in the driver if requested.
580 set_kdebugflag (kdebugflag);
592 /********************************************************************
594 * tty_disestablish_ppp - Restore the serial port to normal operation.
595 * This shouldn't call die() because it's called from die().
598 void tty_disestablish_ppp(int tty_fd)
602 * Flush the tty output buffer so that the TIOCSETD doesn't hang.
604 if (tcflush(tty_fd, TCIOFLUSH) < 0)
606 warn("tcflush failed: %m");
610 * Restore the previous line discipline
612 if (ioctl(tty_fd, TIOCSETD, &tty_disc) < 0) {
613 if ( ! ok_error (errno))
614 error("ioctl(TIOCSETD, N_TTY): %m (line %d)", __LINE__);
617 if (ioctl(tty_fd, TIOCNXCL, 0) < 0) {
618 if ( ! ok_error (errno))
619 warn("ioctl(TIOCNXCL): %m (line %d)", __LINE__);
622 /* Reset non-blocking mode on fd. */
623 if (initfdflags != -1 && fcntl(tty_fd, F_SETFL, initfdflags) < 0) {
624 if ( ! ok_error (errno))
625 warn("Couldn't restore device fd flags: %m");
631 generic_disestablish_ppp(tty_fd);
634 /********************************************************************
636 * generic_disestablish_ppp - Restore device components to normal
637 * operation, and reconnect the ppp unit to the loopback if in demand
638 * mode. This shouldn't call die() because it's called from die().
640 void generic_disestablish_ppp(int dev_fd)
642 if (new_style_driver) {
646 modify_flags(ppp_dev_fd, 0, SC_LOOP_TRAFFIC);
648 } else if (!doing_multilink && ppp_dev_fd >= 0) {
650 remove_fd(ppp_dev_fd);
654 /* old-style driver */
656 set_ppp_fd(slave_fd);
663 * make_ppp_unit - make a new ppp unit for ppp_dev_fd.
664 * Assumes new_style_driver.
666 static int make_ppp_unit(void)
670 if (ppp_dev_fd >= 0) {
671 dbglog("in make_ppp_unit, already had /dev/ppp open?");
674 ppp_dev_fd = open("/dev/ppp", O_RDWR);
676 fatal("Couldn't open /dev/ppp: %m");
677 flags = fcntl(ppp_dev_fd, F_GETFL);
679 || fcntl(ppp_dev_fd, F_SETFL, flags | O_NONBLOCK) == -1)
680 warn("Couldn't set /dev/ppp to nonblock: %m");
683 x = ioctl(ppp_dev_fd, PPPIOCNEWUNIT, &ifunit);
684 if (x < 0 && req_unit >= 0 && errno == EEXIST) {
685 warn("Couldn't allocate PPP unit %d as it is already in use", req_unit);
687 x = ioctl(ppp_dev_fd, PPPIOCNEWUNIT, &ifunit);
690 error("Couldn't create new ppp unit: %m");
692 if (x == 0 && req_ifname[0] != '\0') {
694 char t[MAXIFNAMELEN];
695 memset(&ifr, 0, sizeof(struct ifreq));
696 slprintf(t, sizeof(t), "%s%d", PPP_DRV_NAME, ifunit);
697 strlcpy(ifr.ifr_name, t, IF_NAMESIZE);
698 strlcpy(ifr.ifr_newname, req_ifname, IF_NAMESIZE);
699 x = ioctl(sock_fd, SIOCSIFNAME, &ifr);
701 error("Couldn't rename interface %s to %s: %m", t, req_ifname);
703 info("Renamed interface %s to %s", t, req_ifname);
710 * cfg_bundle - configure the existing bundle.
711 * Used in demand mode.
713 void cfg_bundle(int mrru, int mtru, int rssn, int tssn)
715 if (!new_style_driver)
718 /* set the mrru, mtu and flags */
719 if (ioctl(ppp_dev_fd, PPPIOCSMRRU, &mrru) < 0)
720 error("Couldn't set MRRU: %m");
722 modify_flags(ppp_dev_fd, SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ|SC_MULTILINK,
723 ((rssn? SC_MP_SHORTSEQ: 0) | (tssn? SC_MP_XSHORTSEQ: 0)
724 | (mrru? SC_MULTILINK: 0)));
726 /* connect up the channel */
727 if (ioctl(ppp_fd, PPPIOCCONNECT, &ifunit) < 0)
728 fatal("Couldn't attach to PPP unit %d: %m", ifunit);
733 * make_new_bundle - create a new PPP unit (i.e. a bundle)
734 * and connect our channel to it. This should only get called
735 * if `multilink' was set at the time establish_ppp was called.
736 * In demand mode this uses our existing bundle instead of making
739 void make_new_bundle(int mrru, int mtru, int rssn, int tssn)
741 if (!new_style_driver)
744 /* make us a ppp unit */
745 if (make_ppp_unit() < 0)
748 /* set the mrru and flags */
749 cfg_bundle(mrru, mtru, rssn, tssn);
753 * bundle_attach - attach our link to a given PPP unit.
754 * We assume the unit is controlled by another pppd.
756 int bundle_attach(int ifnum)
760 if (!new_style_driver)
763 master_fd = open("/dev/ppp", O_RDWR);
765 fatal("Couldn't open /dev/ppp: %m");
766 if (ioctl(master_fd, PPPIOCATTACH, &ifnum) < 0) {
767 if (errno == ENXIO) {
769 return 0; /* doesn't still exist */
771 fatal("Couldn't attach to interface unit %d: %m\n", ifnum);
773 if (ioctl(ppp_fd, PPPIOCCONNECT, &ifnum) < 0)
774 fatal("Couldn't connect to interface unit %d: %m", ifnum);
775 modify_flags(master_fd, 0, SC_MULTILINK);
783 * destroy_bundle - tell the driver to destroy our bundle.
785 void destroy_bundle(void)
787 if (ppp_dev_fd >= 0) {
789 remove_fd(ppp_dev_fd);
794 /********************************************************************
796 * clean_check - Fetch the flags for the device and generate
797 * appropriate error messages.
799 void clean_check(void)
805 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
807 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
809 s = "all had bit 7 set to 1";
813 s = "all had bit 7 set to 0";
817 s = "all had odd parity";
821 s = "all had even parity";
826 warn("Receive serial link is not 8-bit clean:");
827 warn("Problem: %s", s);
835 * List of valid speeds.
839 int speed_int, speed_val;
926 { 1000000, B1000000 },
929 { 1152000, B1152000 },
932 { 1500000, B1500000 },
935 { 2000000, B2000000 },
938 { 2500000, B2500000 },
941 { 3000000, B3000000 },
944 { 3500000, B3500000 },
947 { 4000000, B4000000 },
952 /********************************************************************
954 * Translate from bits/second to a speed_t.
957 static int translate_speed (int bps)
959 struct speed *speedp;
962 for (speedp = speeds; speedp->speed_int; speedp++) {
963 if (bps == speedp->speed_int)
964 return speedp->speed_val;
970 /********************************************************************
972 * Translate from a speed_t to bits/second.
975 static int baud_rate_of (int speed)
977 struct speed *speedp;
980 for (speedp = speeds; speedp->speed_int; speedp++) {
981 if (speed == speedp->speed_val)
982 return speedp->speed_int;
988 /********************************************************************
990 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
991 * at the requested speed, etc. If `local' is true, set CLOCAL
992 * regardless of whether the modem option was specified.
995 void set_up_tty(int tty_fd, int local)
1001 if (tcgetattr(tty_fd, &tios) < 0) {
1002 if (!ok_error(errno))
1003 fatal("tcgetattr: %m (line %d)", __LINE__);
1010 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
1011 tios.c_cflag |= CS8 | CREAD | HUPCL;
1013 tios.c_iflag = IGNBRK | IGNPAR;
1016 tios.c_cc[VMIN] = 1;
1017 tios.c_cc[VTIME] = 0;
1019 if (local || !modem)
1020 tios.c_cflag ^= (CLOCAL | HUPCL);
1024 tios.c_cflag |= CRTSCTS;
1028 tios.c_iflag |= IXON | IXOFF;
1029 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
1030 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
1034 tios.c_cflag &= ~CRTSCTS;
1042 tios.c_cflag |= CSTOPB;
1045 speed = translate_speed(inspeed);
1047 cfsetospeed (&tios, speed);
1048 cfsetispeed (&tios, speed);
1049 speed = cfgetospeed(&tios);
1051 baud_rate = baud_rate_of(speed);
1054 speed = cfgetospeed(&tios);
1055 baud_rate = baud_rate_of(speed);
1058 while (tcsetattr(tty_fd, TCSAFLUSH, &tios) < 0 && !ok_error(errno))
1060 fatal("tcsetattr: %m (line %d)", __LINE__);
1063 /* Most Linux architectures and drivers support arbitrary baud rate values via BOTHER */
1066 struct termios2 tios2;
1067 if (ioctl(tty_fd, TCGETS2, &tios2) == 0) {
1069 tios2.c_cflag &= ~CBAUD;
1070 tios2.c_cflag |= BOTHER;
1071 tios2.c_ispeed = inspeed;
1072 tios2.c_ospeed = inspeed;
1074 if (ioctl(tty_fd, TCSETS2, &tios2) == 0)
1075 baud_rate = inspeed;
1078 if ((tios2.c_cflag & CBAUD) == BOTHER && tios2.c_ospeed)
1079 baud_rate = tios2.c_ospeed;
1086 * We can't proceed if the serial port baud rate is unknown,
1087 * since that implies that the serial port is disabled.
1091 fatal("speed %d not supported", inspeed);
1093 fatal("Baud rate for %s is 0; need explicit baud rate", devnam);
1097 /********************************************************************
1099 * setdtr - control the DTR line on the serial port.
1100 * This is called from die(), so it shouldn't call die().
1103 void setdtr (int tty_fd, int on)
1105 int modembits = TIOCM_DTR;
1107 ioctl(tty_fd, (on ? TIOCMBIS : TIOCMBIC), &modembits);
1110 /********************************************************************
1112 * restore_tty - restore the terminal to the saved settings.
1115 void restore_tty (int tty_fd)
1120 * Turn off echoing, because otherwise we can get into
1121 * a loop with the tty and the modem echoing to each other.
1122 * We presume we are the sole user of this tty device, so
1123 * when we close it, it will revert to its defaults anyway.
1125 if (!default_device)
1126 inittermios.c_lflag &= ~(ECHO | ECHONL);
1128 if (tcsetattr(tty_fd, TCSAFLUSH, &inittermios) < 0) {
1129 if (! ok_error (errno))
1130 warn("tcsetattr: %m (line %d)", __LINE__);
1135 /********************************************************************
1137 * output - Output PPP packet.
1140 void output (int unit, unsigned char *p, int len)
1145 dump_packet("sent", p, len);
1146 if (snoop_send_hook) snoop_send_hook(p, len);
1148 if (len < PPP_HDRLEN)
1150 if (new_style_driver) {
1153 proto = (p[0] << 8) + p[1];
1154 if (ppp_dev_fd >= 0 && !(proto >= 0xc000 || proto == PPP_CCPFRAG))
1157 if (write(fd, p, len) < 0) {
1158 if (errno == EWOULDBLOCK || errno == EAGAIN || errno == ENOBUFS
1159 || errno == ENXIO || errno == EIO || errno == EINTR)
1160 warn("write: warning: %m (%d)", errno);
1162 error("write: %m (%d)", errno);
1166 /********************************************************************
1168 * wait_input - wait until there is data available,
1169 * for the length of time specified by *timo (indefinite
1173 void wait_input(struct timeval *timo)
1180 n = select(max_in_fd + 1, &ready, NULL, &exc, timo);
1181 if (n < 0 && errno != EINTR)
1182 fatal("select: %m");
1186 * add_fd - add an fd to the set that wait_input waits for.
1190 if (fd >= FD_SETSIZE)
1191 fatal("internal error: file descriptor too large (%d)", fd);
1192 FD_SET(fd, &in_fds);
1198 * remove_fd - remove an fd from the set that wait_input waits for.
1200 void remove_fd(int fd)
1202 FD_CLR(fd, &in_fds);
1206 /********************************************************************
1208 * read_packet - get a PPP packet from the serial device.
1211 int read_packet (unsigned char *buf)
1215 len = PPP_MRU + PPP_HDRLEN;
1216 if (new_style_driver) {
1217 *buf++ = PPP_ALLSTATIONS;
1223 nr = read(ppp_fd, buf, len);
1224 if (nr < 0 && errno != EWOULDBLOCK && errno != EAGAIN
1225 && errno != EIO && errno != EINTR)
1227 if (nr < 0 && errno == ENXIO)
1230 if (nr < 0 && new_style_driver && ppp_dev_fd >= 0 && !bundle_eof) {
1231 /* N.B. we read ppp_fd first since LCP packets come in there. */
1232 nr = read(ppp_dev_fd, buf, len);
1233 if (nr < 0 && errno != EWOULDBLOCK && errno != EAGAIN
1234 && errno != EIO && errno != EINTR)
1235 error("read /dev/ppp: %m");
1236 if (nr < 0 && errno == ENXIO)
1238 if (nr == 0 && doing_multilink) {
1239 remove_fd(ppp_dev_fd);
1243 if (new_style_driver && ppp_fd < 0 && ppp_dev_fd < 0)
1245 return (new_style_driver && nr > 0)? nr+2: nr;
1248 /********************************************************************
1250 * get_loop_output - get outgoing packets from the ppp device,
1251 * and detect when we want to bring the real link up.
1252 * Return value is 1 if we need to bring up the link, 0 otherwise.
1255 get_loop_output(void)
1260 if (new_style_driver) {
1261 while ((n = read_packet(inpacket_buf)) > 0)
1262 if (loop_frame(inpacket_buf, n))
1267 while ((n = read(master_fd, inbuf, sizeof(inbuf))) > 0)
1268 if (loop_chars(inbuf, n))
1272 fatal("eof on loopback");
1274 if (errno != EWOULDBLOCK && errno != EAGAIN)
1275 fatal("read from loopback: %m(%d)", errno);
1281 * netif_set_mtu - set the MTU on the PPP network interface.
1284 netif_set_mtu(int unit, int mtu)
1288 memset (&ifr, '\0', sizeof (ifr));
1289 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1292 if (ifunit >= 0 && ioctl(sock_fd, SIOCSIFMTU, (caddr_t) &ifr) < 0)
1293 error("ioctl(SIOCSIFMTU): %m (line %d)", __LINE__);
1297 * netif_get_mtu - get the MTU on the PPP network interface.
1300 netif_get_mtu(int unit)
1304 memset (&ifr, '\0', sizeof (ifr));
1305 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1307 if (ifunit >= 0 && ioctl(sock_fd, SIOCGIFMTU, (caddr_t) &ifr) < 0) {
1308 error("ioctl(SIOCGIFMTU): %m (line %d)", __LINE__);
1314 /********************************************************************
1316 * tty_send_config - configure the transmit characteristics of
1317 * the ppp interface.
1320 void tty_send_config(int mtu, u_int32_t asyncmap, int pcomp, int accomp)
1327 if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
1328 if (errno != EIO && errno != ENOTTY)
1329 error("Couldn't set transmit async character map: %m");
1334 x = (pcomp? SC_COMP_PROT: 0) | (accomp? SC_COMP_AC: 0)
1335 | (sync_serial? SC_SYNC: 0);
1336 modify_flags(ppp_fd, SC_COMP_PROT|SC_COMP_AC|SC_SYNC, x);
1339 /********************************************************************
1341 * tty_set_xaccm - set the extended transmit ACCM for the interface.
1344 void tty_set_xaccm (ext_accm accm)
1348 if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY) {
1349 if ( ! ok_error (errno))
1350 warn("ioctl(set extended ACCM): %m (line %d)", __LINE__);
1354 /********************************************************************
1356 * tty_recv_config - configure the receive-side characteristics of
1357 * the ppp interface.
1360 void tty_recv_config(int mru, u_int32_t asyncmap, int pcomp, int accomp)
1363 * If we were called because the link has gone down then there is nothing
1364 * which may be done. Just return without incident.
1369 * Set the receiver parameters
1371 if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
1372 if (errno != EIO && errno != ENOTTY)
1373 error("Couldn't set channel receive MRU: %m");
1375 if (new_style_driver && ppp_dev_fd >= 0
1376 && ioctl(ppp_dev_fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
1377 error("Couldn't set MRU in generic PPP layer: %m");
1379 if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
1380 if (errno != EIO && errno != ENOTTY)
1381 error("Couldn't set channel receive asyncmap: %m");
1385 /********************************************************************
1387 * ccp_test - ask kernel whether a given compression method
1388 * is acceptable for use.
1392 ccp_test(int unit, u_char *opt_ptr, int opt_len, int for_transmit)
1394 struct ppp_option_data data;
1396 memset (&data, '\0', sizeof (data));
1398 data.length = opt_len;
1399 data.transmit = for_transmit;
1401 if (ioctl(ppp_dev_fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
1404 return (errno == ENOBUFS)? 0: -1;
1407 /********************************************************************
1409 * ccp_flags_set - inform kernel about the current state of CCP.
1412 void ccp_flags_set (int unit, int isopen, int isup)
1416 x = (isopen? SC_CCP_OPEN: 0) | (isup? SC_CCP_UP: 0);
1417 if (still_ppp() && ppp_dev_fd >= 0)
1418 modify_flags(ppp_dev_fd, SC_CCP_OPEN|SC_CCP_UP, x);
1423 * set_filters - set the active and pass filters in the kernel driver.
1425 int set_filters(struct bpf_program *pass, struct bpf_program *active)
1427 struct sock_fprog fp;
1429 fp.len = pass->bf_len;
1430 fp.filter = (struct sock_filter *) pass->bf_insns;
1431 if (ioctl(ppp_dev_fd, PPPIOCSPASS, &fp) < 0) {
1432 if (errno == ENOTTY)
1433 warn("kernel does not support PPP filtering");
1435 error("Couldn't set pass-filter in kernel: %m");
1438 fp.len = active->bf_len;
1439 fp.filter = (struct sock_filter *) active->bf_insns;
1440 if (ioctl(ppp_dev_fd, PPPIOCSACTIVE, &fp) < 0) {
1441 error("Couldn't set active-filter in kernel: %m");
1446 #endif /* PPP_FILTER */
1448 /********************************************************************
1450 * get_idle_time - return how long the link has been idle.
1453 get_idle_time(int u, struct ppp_idle *ip)
1455 return ioctl(ppp_dev_fd, PPPIOCGIDLE, ip) >= 0;
1458 /********************************************************************
1460 * get_ppp_stats - return statistics for the link.
1463 get_ppp_stats(int u, struct pppd_stats *stats)
1465 struct ifpppstatsreq req;
1467 memset (&req, 0, sizeof (req));
1469 req.stats_ptr = (caddr_t) &req.stats;
1470 strlcpy(req.ifr__name, ifname, sizeof(req.ifr__name));
1471 if (ioctl(sock_fd, SIOCGPPPSTATS, &req) < 0) {
1472 error("Couldn't get PPP statistics: %m");
1475 stats->bytes_in = req.stats.p.ppp_ibytes;
1476 stats->bytes_out = req.stats.p.ppp_obytes;
1477 stats->pkts_in = req.stats.p.ppp_ipackets;
1478 stats->pkts_out = req.stats.p.ppp_opackets;
1482 /********************************************************************
1484 * ccp_fatal_error - returns 1 if decompression was disabled as a
1485 * result of an error detected after decompression of a packet,
1486 * 0 otherwise. This is necessary because of patent nonsense.
1489 int ccp_fatal_error (int unit)
1493 if (ioctl(ppp_dev_fd, PPPIOCGFLAGS, &flags) < 0) {
1494 error("Couldn't read compression error flags: %m");
1497 return flags & SC_DC_FERROR;
1500 /********************************************************************
1502 * path_to_procfs - find the path to the proc file system mount point
1504 static char proc_path[MAXPATHLEN];
1505 static int proc_path_len;
1507 static char *path_to_procfs(const char *tail)
1509 struct mntent *mntent;
1512 if (proc_path_len == 0) {
1513 /* Default the mount location of /proc */
1514 strlcpy (proc_path, "/proc", sizeof(proc_path));
1516 fp = fopen(MOUNTED, "r");
1518 while ((mntent = getmntent(fp)) != NULL) {
1519 if (strcmp(mntent->mnt_type, MNTTYPE_IGNORE) == 0)
1521 if (strcmp(mntent->mnt_type, "proc") == 0) {
1522 strlcpy(proc_path, mntent->mnt_dir, sizeof(proc_path));
1523 proc_path_len = strlen(proc_path);
1531 strlcpy(proc_path + proc_path_len, tail,
1532 sizeof(proc_path) - proc_path_len);
1537 * /proc/net/route parsing stuff.
1539 #define ROUTE_MAX_COLS 12
1540 FILE *route_fd = (FILE *) 0;
1541 static char route_buffer[512];
1542 static int route_dev_col, route_dest_col, route_gw_col;
1543 static int route_flags_col, route_metric_col, route_mask_col;
1544 static int route_num_cols;
1546 static int open_route_table (void);
1547 static void close_route_table (void);
1548 static int read_route_table (struct rtentry *rt);
1550 /********************************************************************
1552 * close_route_table - close the interface to the route table
1555 static void close_route_table (void)
1557 if (route_fd != (FILE *) 0) {
1559 route_fd = (FILE *) 0;
1563 /********************************************************************
1565 * open_route_table - open the interface to the route table
1567 static char route_delims[] = " \t\n";
1569 static int open_route_table (void)
1573 close_route_table();
1575 path = path_to_procfs("/net/route");
1576 route_fd = fopen (path, "r");
1577 if (route_fd == NULL) {
1578 error("can't open routing table %s: %m", path);
1582 route_dev_col = 0; /* default to usual columns */
1585 route_flags_col = 3;
1586 route_metric_col = 6;
1590 /* parse header line */
1591 if (fgets(route_buffer, sizeof(route_buffer), route_fd) != 0) {
1592 char *p = route_buffer, *q;
1594 for (col = 0; col < ROUTE_MAX_COLS; ++col) {
1596 if ((q = strtok(p, route_delims)) == 0)
1598 if (strcasecmp(q, "iface") == 0)
1599 route_dev_col = col;
1600 else if (strcasecmp(q, "destination") == 0)
1601 route_dest_col = col;
1602 else if (strcasecmp(q, "gateway") == 0)
1604 else if (strcasecmp(q, "flags") == 0)
1605 route_flags_col = col;
1606 else if (strcasecmp(q, "mask") == 0)
1607 route_mask_col = col;
1610 if (used && col >= route_num_cols)
1611 route_num_cols = col + 1;
1619 /********************************************************************
1621 * read_route_table - read the next entry from the route table
1624 static int read_route_table(struct rtentry *rt)
1626 char *cols[ROUTE_MAX_COLS], *p;
1629 memset (rt, '\0', sizeof (struct rtentry));
1631 if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0)
1635 for (col = 0; col < route_num_cols; ++col) {
1636 cols[col] = strtok(p, route_delims);
1637 if (cols[col] == NULL)
1638 return 0; /* didn't get enough columns */
1642 SET_SA_FAMILY (rt->rt_dst, AF_INET);
1643 SET_SA_FAMILY (rt->rt_gateway, AF_INET);
1645 SIN_ADDR(rt->rt_dst) = strtoul(cols[route_dest_col], NULL, 16);
1646 SIN_ADDR(rt->rt_gateway) = strtoul(cols[route_gw_col], NULL, 16);
1647 SIN_ADDR(rt->rt_genmask) = strtoul(cols[route_mask_col], NULL, 16);
1649 rt->rt_flags = (short) strtoul(cols[route_flags_col], NULL, 16);
1650 rt->rt_metric = (short) strtoul(cols[route_metric_col], NULL, 10);
1651 rt->rt_dev = cols[route_dev_col];
1656 /********************************************************************
1658 * defaultroute_exists - determine if there is a default route
1659 * with the given metric (or negative for any)
1662 static int defaultroute_exists (struct rtentry *rt, int metric)
1666 if (!open_route_table())
1669 while (read_route_table(rt) != 0) {
1670 if ((rt->rt_flags & RTF_UP) == 0)
1673 if (kernel_version > KVERSION(2,1,0) && SIN_ADDR(rt->rt_genmask) != 0)
1675 if (SIN_ADDR(rt->rt_dst) == 0L && (metric < 0
1676 || rt->rt_metric == metric)) {
1682 close_route_table();
1687 * have_route_to - determine if the system has any route to
1688 * a given IP address. `addr' is in network byte order.
1689 * Return value is 1 if yes, 0 if no, -1 if don't know.
1690 * For demand mode to work properly, we have to ignore routes
1691 * through our own interface.
1693 int have_route_to(u_int32_t addr)
1698 if (!open_route_table())
1699 return -1; /* don't know */
1701 while (read_route_table(&rt)) {
1702 if ((rt.rt_flags & RTF_UP) == 0 || strcmp(rt.rt_dev, ifname) == 0)
1704 if ((addr & SIN_ADDR(rt.rt_genmask)) == SIN_ADDR(rt.rt_dst)) {
1710 close_route_table();
1714 /********************************************************************
1716 * sifdefaultroute - assign a default route through the address given.
1718 * If the global default_rt_repl_rest flag is set, then this function
1719 * already replaced the original system defaultroute with some other
1720 * route and it should just replace the current defaultroute with
1721 * another one, without saving the current route. Use: demand mode,
1722 * when pppd sets first a defaultroute it it's temporary ppp0 addresses
1723 * and then changes the temporary addresses to the addresses for the real
1724 * ppp connection when it has come up.
1727 int sifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway, bool replace)
1729 struct rtentry rt, tmp_rt;
1730 struct rtentry *del_rt = NULL;
1732 if (default_rt_repl_rest) {
1733 /* We have already replaced the original defaultroute, if we
1734 * are called again, we will delete the current default route
1735 * and set the new default route in this function.
1736 * - this is normally only the case the doing demand: */
1737 if (defaultroute_exists(&tmp_rt, -1))
1739 } else if (defaultroute_exists(&old_def_rt, -1 ) &&
1740 strcmp( old_def_rt.rt_dev, ifname) != 0) {
1742 * We did not yet replace an existing default route, let's
1743 * check if we should save and replace a default route:
1745 u_int32_t old_gateway = SIN_ADDR(old_def_rt.rt_gateway);
1747 if (old_gateway != gateway) {
1749 error("not replacing default route to %s [%I]",
1750 old_def_rt.rt_dev, old_gateway);
1753 /* we need to copy rt_dev because we need it permanent too: */
1754 char * tmp_dev = malloc(strlen(old_def_rt.rt_dev)+1);
1755 strcpy(tmp_dev, old_def_rt.rt_dev);
1756 old_def_rt.rt_dev = tmp_dev;
1758 notice("replacing old default route to %s [%I]",
1759 old_def_rt.rt_dev, old_gateway);
1760 default_rt_repl_rest = 1;
1761 del_rt = &old_def_rt;
1766 memset (&rt, 0, sizeof (rt));
1767 SET_SA_FAMILY (rt.rt_dst, AF_INET);
1770 rt.rt_metric = dfl_route_metric + 1; /* +1 for binary compatibility */
1772 if (kernel_version > KVERSION(2,1,0)) {
1773 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
1774 SIN_ADDR(rt.rt_genmask) = 0L;
1777 rt.rt_flags = RTF_UP;
1778 if (ioctl(sock_fd, SIOCADDRT, &rt) < 0) {
1779 if ( ! ok_error ( errno ))
1780 error("default route ioctl(SIOCADDRT): %m");
1783 if (default_rt_repl_rest && del_rt)
1784 if (ioctl(sock_fd, SIOCDELRT, del_rt) < 0) {
1785 if ( ! ok_error ( errno ))
1786 error("del old default route ioctl(SIOCDELRT): %m(%d)", errno);
1790 have_default_route = 1;
1794 /********************************************************************
1796 * cifdefaultroute - delete a default route through the address given.
1799 int cifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
1803 have_default_route = 0;
1805 memset (&rt, '\0', sizeof (rt));
1806 SET_SA_FAMILY (rt.rt_dst, AF_INET);
1807 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
1812 rt.rt_metric = dfl_route_metric + 1; /* +1 for binary compatibility */
1814 if (kernel_version > KVERSION(2,1,0)) {
1815 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
1816 SIN_ADDR(rt.rt_genmask) = 0L;
1819 rt.rt_flags = RTF_UP;
1820 if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH) {
1822 if ( ! ok_error ( errno ))
1823 error("default route ioctl(SIOCDELRT): %m");
1827 if (default_rt_repl_rest) {
1828 notice("restoring old default route to %s [%I]",
1829 old_def_rt.rt_dev, SIN_ADDR(old_def_rt.rt_gateway));
1830 if (ioctl(sock_fd, SIOCADDRT, &old_def_rt) < 0) {
1831 if ( ! ok_error ( errno ))
1832 error("restore default route ioctl(SIOCADDRT): %m(%d)", errno);
1835 default_rt_repl_rest = 0;
1843 * /proc/net/ipv6_route parsing stuff.
1845 static int route_dest_plen_col;
1846 static int open_route6_table (void);
1847 static int read_route6_table (struct in6_rtmsg *rt);
1849 /********************************************************************
1851 * open_route6_table - open the interface to the route table
1853 static int open_route6_table (void)
1857 close_route_table();
1859 path = path_to_procfs("/net/ipv6_route");
1860 route_fd = fopen (path, "r");
1861 if (route_fd == NULL) {
1862 error("can't open routing table %s: %m", path);
1866 /* default to usual columns */
1868 route_dest_plen_col = 1;
1870 route_metric_col = 5;
1871 route_flags_col = 8;
1873 route_num_cols = 10;
1878 /********************************************************************
1880 * read_route6_table - read the next entry from the route table
1883 static void hex_to_in6_addr(struct in6_addr *addr, const char *s)
1890 for (i = 0; i < 4; i++) {
1891 memcpy(hex8, s + 8*i, 8);
1892 v = strtoul(hex8, NULL, 16);
1893 addr->s6_addr32[i] = v;
1897 static int read_route6_table(struct in6_rtmsg *rt)
1899 char *cols[ROUTE_MAX_COLS], *p;
1902 memset (rt, '\0', sizeof (struct in6_rtmsg));
1904 if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0)
1908 for (col = 0; col < route_num_cols; ++col) {
1909 cols[col] = strtok(p, route_delims);
1910 if (cols[col] == NULL)
1911 return 0; /* didn't get enough columns */
1915 hex_to_in6_addr(&rt->rtmsg_dst, cols[route_dest_col]);
1916 rt->rtmsg_dst_len = strtoul(cols[route_dest_plen_col], NULL, 16);
1917 hex_to_in6_addr(&rt->rtmsg_gateway, cols[route_gw_col]);
1919 rt->rtmsg_metric = strtoul(cols[route_metric_col], NULL, 16);
1920 rt->rtmsg_flags = strtoul(cols[route_flags_col], NULL, 16);
1921 rt->rtmsg_ifindex = if_nametoindex(cols[route_dev_col]);
1926 /********************************************************************
1928 * defaultroute6_exists - determine if there is a default route
1931 static int defaultroute6_exists (struct in6_rtmsg *rt, int metric)
1935 if (!open_route6_table())
1938 while (read_route6_table(rt) != 0) {
1939 if ((rt->rtmsg_flags & RTF_UP) == 0)
1942 if (rt->rtmsg_dst_len != 0)
1944 if (rt->rtmsg_dst.s6_addr32[0] == 0L
1945 && rt->rtmsg_dst.s6_addr32[1] == 0L
1946 && rt->rtmsg_dst.s6_addr32[2] == 0L
1947 && rt->rtmsg_dst.s6_addr32[3] == 0L
1948 && (metric < 0 || rt->rtmsg_metric == metric)) {
1954 close_route_table();
1958 /********************************************************************
1960 * sif6defaultroute - assign a default route through the address given.
1962 * If the global default_rt_repl_rest flag is set, then this function
1963 * already replaced the original system defaultroute with some other
1964 * route and it should just replace the current defaultroute with
1965 * another one, without saving the current route. Use: demand mode,
1966 * when pppd sets first a defaultroute it it's temporary ppp0 addresses
1967 * and then changes the temporary addresses to the addresses for the real
1968 * ppp connection when it has come up.
1971 int sif6defaultroute (int unit, eui64_t ouraddr, eui64_t gateway)
1973 struct in6_rtmsg rt;
1974 char buf[IF_NAMESIZE];
1976 if (defaultroute6_exists(&rt, dfl_route_metric) &&
1977 rt.rtmsg_ifindex != if_nametoindex(ifname)) {
1978 if (rt.rtmsg_flags & RTF_GATEWAY)
1979 error("not replacing existing default route via gateway");
1981 error("not replacing existing default route through %s",
1982 if_indextoname(rt.rtmsg_ifindex, buf));
1986 memset (&rt, 0, sizeof (rt));
1988 rt.rtmsg_ifindex = if_nametoindex(ifname);
1989 rt.rtmsg_metric = dfl_route_metric + 1; /* +1 for binary compatibility */
1990 rt.rtmsg_dst_len = 0;
1992 rt.rtmsg_flags = RTF_UP;
1993 if (ioctl(sock6_fd, SIOCADDRT, &rt) < 0) {
1994 if ( ! ok_error ( errno ))
1995 error("default route ioctl(SIOCADDRT): %m");
1999 have_default_route6 = 1;
2003 /********************************************************************
2005 * cif6defaultroute - delete a default route through the address given.
2008 int cif6defaultroute (int unit, eui64_t ouraddr, eui64_t gateway)
2010 struct in6_rtmsg rt;
2012 have_default_route6 = 0;
2014 memset (&rt, '\0', sizeof (rt));
2016 rt.rtmsg_ifindex = if_nametoindex(ifname);
2017 rt.rtmsg_metric = dfl_route_metric + 1; /* +1 for binary compatibility */
2018 rt.rtmsg_dst_len = 0;
2020 rt.rtmsg_flags = RTF_UP;
2021 if (ioctl(sock6_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH) {
2023 if ( ! ok_error ( errno ))
2024 error("default route ioctl(SIOCDELRT): %m");
2033 /********************************************************************
2035 * sifproxyarp - Make a proxy ARP entry for the peer.
2038 int sifproxyarp (int unit, u_int32_t his_adr)
2040 struct arpreq arpreq;
2043 if (has_proxy_arp == 0) {
2044 memset (&arpreq, '\0', sizeof(arpreq));
2046 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
2047 SIN_ADDR(arpreq.arp_pa) = his_adr;
2048 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
2050 * Get the hardware address of an interface on the same subnet
2051 * as our local address.
2053 if (!get_ether_addr(his_adr, &arpreq.arp_ha, proxy_arp_dev,
2054 sizeof(proxy_arp_dev))) {
2055 error("Cannot determine ethernet address for proxy ARP");
2058 strlcpy(arpreq.arp_dev, proxy_arp_dev, sizeof(arpreq.arp_dev));
2060 if (ioctl(sock_fd, SIOCSARP, (caddr_t)&arpreq) < 0) {
2061 if ( ! ok_error ( errno ))
2062 error("ioctl(SIOCSARP): %m");
2065 proxy_arp_addr = his_adr;
2069 forw_path = path_to_procfs("/sys/net/ipv4/ip_forward");
2070 if (forw_path != 0) {
2071 int fd = open(forw_path, O_WRONLY);
2073 if (write(fd, "1", 1) != 1)
2074 error("Couldn't enable IP forwarding: %m");
2084 /********************************************************************
2086 * cifproxyarp - Delete the proxy ARP entry for the peer.
2089 int cifproxyarp (int unit, u_int32_t his_adr)
2091 struct arpreq arpreq;
2093 if (has_proxy_arp) {
2095 memset (&arpreq, '\0', sizeof(arpreq));
2096 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
2097 SIN_ADDR(arpreq.arp_pa) = his_adr;
2098 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
2099 strlcpy(arpreq.arp_dev, proxy_arp_dev, sizeof(arpreq.arp_dev));
2101 if (ioctl(sock_fd, SIOCDARP, (caddr_t)&arpreq) < 0) {
2102 if ( ! ok_error ( errno ))
2103 warn("ioctl(SIOCDARP): %m");
2110 /********************************************************************
2112 * get_ether_addr - get the hardware address of an interface on the
2113 * the same subnet as ipaddr.
2116 static int get_ether_addr (u_int32_t ipaddr,
2117 struct sockaddr *hwaddr,
2118 char *name, int namelen)
2120 struct ifreq *ifr, *ifend;
2121 u_int32_t ina, mask;
2123 struct ifreq ifreq, bestifreq;
2125 struct ifreq ifs[MAX_IFS];
2127 u_int32_t bestmask=0;
2128 int found_interface = 0;
2130 ifc.ifc_len = sizeof(ifs);
2132 if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0) {
2133 if ( ! ok_error ( errno ))
2134 error("ioctl(SIOCGIFCONF): %m (line %d)", __LINE__);
2139 * Scan through looking for an interface with an Internet
2140 * address on the same subnet as `ipaddr'.
2142 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
2143 for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
2144 if (ifr->ifr_addr.sa_family == AF_INET) {
2145 ina = SIN_ADDR(ifr->ifr_addr);
2146 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
2148 * Check that the interface is up, and not point-to-point
2151 if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
2154 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
2157 * Get its netmask and check that it's on the right subnet.
2159 if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
2162 mask = SIN_ADDR(ifreq.ifr_addr);
2164 if (((ipaddr ^ ina) & mask) != 0)
2165 continue; /* no match */
2167 if (mask >= bestmask) {
2168 /* Compare using >= instead of > -- it is possible for
2169 an interface to have a netmask of 0.0.0.0 */
2170 found_interface = 1;
2177 if (!found_interface) return 0;
2179 strlcpy(name, bestifreq.ifr_name, namelen);
2181 /* trim off the :1 in eth0:1 */
2182 aliasp = strchr(name, ':');
2186 info("found interface %s for proxy arp", name);
2188 * Now get the hardware address.
2190 memset (&bestifreq.ifr_hwaddr, 0, sizeof (struct sockaddr));
2191 if (ioctl (sock_fd, SIOCGIFHWADDR, &bestifreq) < 0) {
2192 error("SIOCGIFHWADDR(%s): %m", bestifreq.ifr_name);
2197 &bestifreq.ifr_hwaddr,
2198 sizeof (struct sockaddr));
2204 * get_if_hwaddr - get the hardware address for the specified
2205 * network interface device.
2208 get_if_hwaddr(u_char *addr, char *name)
2213 sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
2216 memset(&ifreq.ifr_hwaddr, 0, sizeof(struct sockaddr));
2217 strlcpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name));
2218 ret = ioctl(sock_fd, SIOCGIFHWADDR, &ifreq);
2221 memcpy(addr, ifreq.ifr_hwaddr.sa_data, 6);
2226 * get_first_ether_hwaddr - get the hardware address for the first
2227 * ethernet-style interface on this system.
2230 get_first_ether_hwaddr(u_char *addr)
2232 struct if_nameindex *if_ni, *i;
2236 sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
2240 if_ni = if_nameindex();
2248 for (i = if_ni; !(i->if_index == 0 && i->if_name == NULL); i++) {
2249 memset(&ifreq.ifr_hwaddr, 0, sizeof(struct sockaddr));
2250 strlcpy(ifreq.ifr_name, i->if_name, sizeof(ifreq.ifr_name));
2251 ret = ioctl(sock_fd, SIOCGIFHWADDR, &ifreq);
2252 if (ret >= 0 && ifreq.ifr_hwaddr.sa_family == ARPHRD_ETHER) {
2253 memcpy(addr, ifreq.ifr_hwaddr.sa_data, 6);
2259 if_freenameindex(if_ni);
2265 /********************************************************************
2267 * Return user specified netmask, modified by any mask we might determine
2268 * for address `addr' (in network byte order).
2269 * Here we scan through the system's list of interfaces, looking for
2270 * any non-point-to-point interfaces which might appear to be on the same
2271 * network as `addr'. If we find any, we OR in their netmask to the
2272 * user-specified netmask.
2275 u_int32_t GetMask (u_int32_t addr)
2277 u_int32_t mask, nmask, ina;
2278 struct ifreq *ifr, *ifend, ifreq;
2280 struct ifreq ifs[MAX_IFS];
2284 if (IN_CLASSA(addr)) /* determine network mask for address class */
2285 nmask = IN_CLASSA_NET;
2286 else if (IN_CLASSB(addr))
2287 nmask = IN_CLASSB_NET;
2289 nmask = IN_CLASSC_NET;
2291 /* class D nets are disallowed by bad_ip_adrs */
2292 mask = netmask | htonl(nmask);
2294 * Scan through the system's network interfaces.
2296 ifc.ifc_len = sizeof(ifs);
2298 if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0) {
2299 if ( ! ok_error ( errno ))
2300 warn("ioctl(SIOCGIFCONF): %m (line %d)", __LINE__);
2304 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
2305 for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
2307 * Check the interface's internet address.
2309 if (ifr->ifr_addr.sa_family != AF_INET)
2311 ina = SIN_ADDR(ifr->ifr_addr);
2312 if (((ntohl(ina) ^ addr) & nmask) != 0)
2315 * Check that the interface is up, and not point-to-point nor loopback.
2317 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
2318 if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
2321 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
2324 * Get its netmask and OR it into our mask.
2326 if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
2328 mask |= SIN_ADDR(ifreq.ifr_addr);
2334 /********************************************************************
2336 * Internal routine to decode the version.modification.patch level
2339 static void decode_version (char *buf, int *version,
2340 int *modification, int *patch)
2344 *version = (int) strtoul (buf, &endp, 10);
2348 if (endp != buf && *endp == '.') {
2350 *modification = (int) strtoul (buf, &endp, 10);
2351 if (endp != buf && *endp == '.') {
2353 *patch = (int) strtoul (buf, &buf, 10);
2358 /********************************************************************
2360 * Procedure to determine if the PPP line discipline is registered.
2364 ppp_registered(void)
2372 * We used to open the serial device and set it to the ppp line
2373 * discipline here, in order to create a ppp unit. But that is
2374 * not a good idea - the user might have specified a device that
2375 * they can't open (permission, or maybe it doesn't really exist).
2376 * So we grab a pty master/slave pair and use that.
2378 if (!get_pty(&mfd, &local_fd, slave, 0)) {
2379 no_ppp_msg = "Couldn't determine if PPP is supported (no free ptys)";
2384 * Try to put the device into the PPP discipline.
2386 if (ioctl(local_fd, TIOCSETD, &ppp_disc) < 0) {
2387 error("ioctl(TIOCSETD(PPP)): %m (line %d)", __LINE__);
2396 /********************************************************************
2398 * ppp_available - check whether the system has any ppp interfaces
2399 * (in fact we check whether we can do an ioctl on ppp0).
2402 int ppp_available(void)
2407 int my_version, my_modification, my_patch;
2408 int osmaj, osmin, ospatch;
2410 /* get the kernel version now, since we are called before sys_init */
2412 osmaj = osmin = ospatch = 0;
2413 sscanf(utsname.release, "%d.%d.%d", &osmaj, &osmin, &ospatch);
2414 kernel_version = KVERSION(osmaj, osmin, ospatch);
2416 fd = open("/dev/ppp", O_RDWR);
2418 new_style_driver = 1;
2420 /* XXX should get from driver */
2422 driver_modification = 4;
2428 if (kernel_version >= KVERSION(2,3,13)) {
2429 error("Couldn't open the /dev/ppp device: %m");
2430 if (errno == ENOENT)
2432 "You need to create the /dev/ppp device node by\n"
2433 "executing the following command as root:\n"
2434 " mknod /dev/ppp c 108 0\n";
2435 else if (errno == ENODEV || errno == ENXIO)
2437 "Please load the ppp_generic kernel module.\n";
2441 /* we are running on a really really old kernel */
2443 "This system lacks kernel support for PPP. This could be because\n"
2444 "the PPP kernel module could not be loaded, or because PPP was not\n"
2445 "included in the kernel configuration. If PPP was included as a\n"
2446 "module, try `/sbin/modprobe -v ppp'. If that fails, check that\n"
2447 "ppp.o exists in /lib/modules/`uname -r`/net.\n"
2448 "See README.linux file in the ppp distribution for more details.\n";
2451 * Open a socket for doing the ioctl operations.
2453 s = socket(AF_INET, SOCK_DGRAM, 0);
2457 strlcpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
2458 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
2460 * If the device did not exist then attempt to create one by putting the
2461 * current tty into the PPP discipline. If this works then obtain the
2462 * flags for the device again.
2465 if (ppp_registered()) {
2466 strlcpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
2467 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
2471 * Ensure that the hardware address is for PPP and not something else
2474 ok = ioctl (s, SIOCGIFHWADDR, (caddr_t) &ifr) >= 0;
2476 if (ok && ((ifr.ifr_hwaddr.sa_family & ~0xFF) != ARPHRD_PPP))
2480 * This is the PPP device. Validate the version of the driver at this
2481 * point to ensure that this program will work with the driver.
2484 char abBuffer [1024];
2486 ifr.ifr_data = abBuffer;
2487 size = ioctl (s, SIOCGPPPVER, (caddr_t) &ifr);
2489 error("Couldn't read driver version: %m");
2491 no_ppp_msg = "Sorry, couldn't verify kernel driver version\n";
2494 decode_version(abBuffer,
2496 &driver_modification,
2499 * Validate the version of the driver against the version that we used.
2501 decode_version(VERSION,
2506 /* The version numbers must match */
2507 if (driver_version != my_version)
2510 /* The modification levels must be legal */
2511 if (driver_modification < 3) {
2512 if (driver_modification >= 2) {
2513 /* we can cope with 2.2.0 and above */
2521 slprintf(route_buffer, sizeof(route_buffer),
2522 "Sorry - PPP driver version %d.%d.%d is out of date\n",
2523 driver_version, driver_modification, driver_patch);
2525 no_ppp_msg = route_buffer;
2533 #ifndef HAVE_LOGWTMP
2534 /********************************************************************
2536 * Update the wtmp file with the appropriate user name and tty device.
2539 void logwtmp (const char *line, const char *name, const char *host)
2541 struct utmp ut, *utp;
2542 pid_t mypid = getpid();
2548 * Update the signon database for users.
2549 * Christoph Lameter: Copied from poeigl-1.36 Jan 3, 1996
2551 utmpname(_PATH_UTMP);
2553 while ((utp = getutent()) && (utp->ut_pid != mypid))
2557 memcpy(&ut, utp, sizeof(ut));
2559 /* some gettys/telnetds don't initialize utmp... */
2560 memset(&ut, 0, sizeof(ut));
2562 if (ut.ut_id[0] == 0)
2563 strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
2565 strncpy(ut.ut_user, name, sizeof(ut.ut_user));
2566 strncpy(ut.ut_line, line, sizeof(ut.ut_line));
2570 ut.ut_type = USER_PROCESS;
2573 /* Insert the host name if one is supplied */
2575 strncpy (ut.ut_host, host, sizeof(ut.ut_host));
2577 /* Insert the IP address of the remote system if IP is enabled */
2578 if (ipcp_protent.enabled_flag && ipcp_hisoptions[0].neg_addr)
2579 memcpy(&ut.ut_addr, (char *) &ipcp_hisoptions[0].hisaddr,
2580 sizeof(ut.ut_addr));
2582 /* CL: Makes sure that the logout works */
2583 if (*host == 0 && *name==0)
2589 * Update the wtmp file.
2592 updwtmp(_PATH_WTMP, &ut);
2594 wtmp = open(_PATH_WTMP, O_APPEND|O_WRONLY);
2596 flock(wtmp, LOCK_EX);
2598 if (write (wtmp, (char *)&ut, sizeof(ut)) != sizeof(ut))
2599 warn("error writing %s: %m", _PATH_WTMP);
2601 flock(wtmp, LOCK_UN);
2607 #endif /* HAVE_LOGWTMP */
2609 /********************************************************************
2611 * sifvjcomp - config tcp header compression
2614 int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid)
2619 if (ioctl(ppp_dev_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
2620 error("Couldn't set up TCP header compression: %m");
2625 x = (vjcomp? SC_COMP_TCP: 0) | (cidcomp? 0: SC_NO_TCP_CCID);
2626 modify_flags(ppp_dev_fd, SC_COMP_TCP|SC_NO_TCP_CCID, x);
2631 /********************************************************************
2633 * sifup - Config the interface up and enable IP packets to pass.
2640 if ((ret = setifstate(u, 1)))
2646 /********************************************************************
2648 * sifdown - Disable the indicated protocol and config the interface
2649 * down if there are no remaining protocols.
2654 if (if_is_up && --if_is_up > 0)
2662 return setifstate(u, 0);
2666 /********************************************************************
2668 * sif6up - Config the interface up for IPv6
2675 if ((ret = setifstate(u, 1)))
2681 /********************************************************************
2683 * sif6down - Disable the IPv6CP protocol and config the interface
2684 * down if there are no remaining protocols.
2687 int sif6down (int u)
2694 return setifstate(u, 0);
2698 /********************************************************************
2700 * setifstate - Config the interface up or down
2703 static int setifstate (int u, int state)
2707 memset (&ifr, '\0', sizeof (ifr));
2708 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2709 if (ioctl(sock_fd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
2710 if (! ok_error (errno))
2711 error("ioctl (SIOCGIFFLAGS): %m (line %d)", __LINE__);
2716 ifr.ifr_flags |= IFF_UP;
2718 ifr.ifr_flags &= ~IFF_UP;
2719 ifr.ifr_flags |= IFF_POINTOPOINT;
2720 if (ioctl(sock_fd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
2721 if (! ok_error (errno))
2722 error("ioctl(SIOCSIFFLAGS): %m (line %d)", __LINE__);
2728 /********************************************************************
2730 * sifaddr - Config the interface IP addresses and netmask.
2733 int sifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr,
2739 memset (&ifr, '\0', sizeof (ifr));
2740 memset (&rt, '\0', sizeof (rt));
2742 SET_SA_FAMILY (ifr.ifr_addr, AF_INET);
2743 SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET);
2744 SET_SA_FAMILY (ifr.ifr_netmask, AF_INET);
2746 strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2748 * Set our IP address
2750 SIN_ADDR(ifr.ifr_addr) = our_adr;
2751 if (ioctl(sock_fd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
2752 if (errno != EEXIST) {
2753 if (! ok_error (errno))
2754 error("ioctl(SIOCSIFADDR): %m (line %d)", __LINE__);
2757 warn("ioctl(SIOCSIFADDR): Address already exists");
2762 * Set the gateway address
2765 SIN_ADDR(ifr.ifr_dstaddr) = his_adr;
2766 if (ioctl(sock_fd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
2767 if (! ok_error (errno))
2768 error("ioctl(SIOCSIFDSTADDR): %m (line %d)", __LINE__);
2774 * For recent kernels, force the netmask to 255.255.255.255.
2776 if (kernel_version >= KVERSION(2,1,16))
2778 if (net_mask != 0) {
2779 SIN_ADDR(ifr.ifr_netmask) = net_mask;
2780 if (ioctl(sock_fd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
2781 if (! ok_error (errno))
2782 error("ioctl(SIOCSIFNETMASK): %m (line %d)", __LINE__);
2787 * Add the device route
2789 if (kernel_version < KVERSION(2,1,16)) {
2790 SET_SA_FAMILY (rt.rt_dst, AF_INET);
2791 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
2794 SIN_ADDR(rt.rt_gateway) = 0L;
2795 SIN_ADDR(rt.rt_dst) = his_adr;
2796 rt.rt_flags = RTF_UP | RTF_HOST;
2798 if (kernel_version > KVERSION(2,1,0)) {
2799 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
2800 SIN_ADDR(rt.rt_genmask) = -1L;
2803 if (ioctl(sock_fd, SIOCADDRT, &rt) < 0) {
2804 if (! ok_error (errno))
2805 error("ioctl(SIOCADDRT) device route: %m (line %d)", __LINE__);
2810 /* set ip_dynaddr in demand mode if address changes */
2811 if (demand && tune_kernel && !dynaddr_set
2812 && our_old_addr && our_old_addr != our_adr) {
2813 /* set ip_dynaddr if possible */
2817 path = path_to_procfs("/sys/net/ipv4/ip_dynaddr");
2818 if (path != 0 && (fd = open(path, O_WRONLY)) >= 0) {
2819 if (write(fd, "1", 1) != 1)
2820 error("Couldn't enable dynamic IP addressing: %m");
2823 dynaddr_set = 1; /* only 1 attempt */
2830 /********************************************************************
2832 * cifaddr - Clear the interface IP addresses, and delete routes
2833 * through the interface if possible.
2836 int cifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr)
2840 if (kernel_version < KVERSION(2,1,16)) {
2842 * Delete the route through the device
2845 memset (&rt, '\0', sizeof (rt));
2847 SET_SA_FAMILY (rt.rt_dst, AF_INET);
2848 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
2851 SIN_ADDR(rt.rt_gateway) = 0;
2852 SIN_ADDR(rt.rt_dst) = his_adr;
2853 rt.rt_flags = RTF_UP | RTF_HOST;
2855 if (kernel_version > KVERSION(2,1,0)) {
2856 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
2857 SIN_ADDR(rt.rt_genmask) = -1L;
2860 if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH) {
2861 if (still_ppp() && ! ok_error (errno))
2862 error("ioctl(SIOCDELRT) device route: %m (line %d)", __LINE__);
2867 /* This way it is possible to have an IPX-only or IPv6-only interface */
2868 memset(&ifr, 0, sizeof(ifr));
2869 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
2870 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
2872 if (ioctl(sock_fd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
2873 if (! ok_error (errno)) {
2874 error("ioctl(SIOCSIFADDR): %m (line %d)", __LINE__);
2879 our_old_addr = our_adr;
2885 /********************************************************************
2887 * sif6addr_rtnetlink - Config the interface with both IPv6 link-local addresses via rtnetlink
2889 static int sif6addr_rtnetlink(unsigned int iface, eui64_t our_eui64, eui64_t his_eui64)
2892 struct nlmsghdr nlh;
2893 struct ifaddrmsg ifa;
2896 struct in6_addr addr;
2900 struct nlmsghdr nlh;
2901 struct nlmsgerr nlerr;
2903 struct sockaddr_nl nladdr;
2910 fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2912 error("sif6addr_rtnetlink: socket(NETLINK_ROUTE): %m (line %d)", __LINE__);
2917 * Tell kernel to not send to us payload of acknowledgment error message.
2918 * NETLINK_CAP_ACK option is supported since Linux kernel version 4.3 and
2919 * older kernel versions always send full payload in acknowledgment netlink
2920 * message. We ignore payload of this message as we need only error code,
2921 * to check if our set remote peer address request succeeded or failed.
2922 * So ignore return value from the following setsockopt() call as setting
2923 * option NETLINK_CAP_ACK means for us just a kernel hint / optimization.
2926 setsockopt(fd, SOL_NETLINK, NETLINK_CAP_ACK, &one, sizeof(one));
2928 memset(&nladdr, 0, sizeof(nladdr));
2929 nladdr.nl_family = AF_NETLINK;
2931 if (bind(fd, (struct sockaddr *)&nladdr, sizeof(nladdr)) < 0) {
2932 error("sif6addr_rtnetlink: bind(AF_NETLINK): %m (line %d)", __LINE__);
2937 memset(&nlreq, 0, sizeof(nlreq));
2938 nlreq.nlh.nlmsg_len = sizeof(nlreq);
2939 nlreq.nlh.nlmsg_type = RTM_NEWADDR;
2940 nlreq.nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE;
2941 nlreq.ifa.ifa_family = AF_INET6;
2942 nlreq.ifa.ifa_prefixlen = 128;
2943 nlreq.ifa.ifa_flags = IFA_F_NODAD | IFA_F_PERMANENT;
2944 nlreq.ifa.ifa_scope = RT_SCOPE_LINK;
2945 nlreq.ifa.ifa_index = iface;
2946 nlreq.addrs[0].rta.rta_len = sizeof(nlreq.addrs[0]);
2947 nlreq.addrs[0].rta.rta_type = IFA_LOCAL;
2948 IN6_LLADDR_FROM_EUI64(nlreq.addrs[0].addr, our_eui64);
2949 nlreq.addrs[1].rta.rta_len = sizeof(nlreq.addrs[1]);
2950 nlreq.addrs[1].rta.rta_type = IFA_ADDRESS;
2951 IN6_LLADDR_FROM_EUI64(nlreq.addrs[1].addr, his_eui64);
2953 memset(&nladdr, 0, sizeof(nladdr));
2954 nladdr.nl_family = AF_NETLINK;
2956 memset(&iov, 0, sizeof(iov));
2957 iov.iov_base = &nlreq;
2958 iov.iov_len = sizeof(nlreq);
2960 memset(&msg, 0, sizeof(msg));
2961 msg.msg_name = &nladdr;
2962 msg.msg_namelen = sizeof(nladdr);
2966 if (sendmsg(fd, &msg, 0) < 0) {
2967 error("sif6addr_rtnetlink: sendmsg(RTM_NEWADDR/NLM_F_CREATE): %m (line %d)", __LINE__);
2972 memset(&iov, 0, sizeof(iov));
2973 iov.iov_base = &nlresp;
2974 iov.iov_len = sizeof(nlresp);
2976 memset(&msg, 0, sizeof(msg));
2977 msg.msg_name = &nladdr;
2978 msg.msg_namelen = sizeof(nladdr);
2982 nlresplen = recvmsg(fd, &msg, 0);
2984 if (nlresplen < 0) {
2985 error("sif6addr_rtnetlink: recvmsg(NLM_F_ACK): %m (line %d)", __LINE__);
2992 if (nladdr.nl_family != AF_NETLINK) {
2993 error("sif6addr_rtnetlink: recvmsg(NLM_F_ACK): Not a netlink packet (line %d)", __LINE__);
2997 if ((size_t)nlresplen != sizeof(nlresp) || nlresp.nlh.nlmsg_len < sizeof(nlresp)) {
2998 error("sif6addr_rtnetlink: recvmsg(NLM_F_ACK): Acknowledgment netlink packet too short (line %d)", __LINE__);
3002 /* acknowledgment packet for NLM_F_ACK is NLMSG_ERROR */
3003 if (nlresp.nlh.nlmsg_type != NLMSG_ERROR) {
3004 error("sif6addr_rtnetlink: recvmsg(NLM_F_ACK): Not an acknowledgment netlink packet (line %d)", __LINE__);
3008 /* error == 0 indicates success, negative value is errno code */
3009 if (nlresp.nlerr.error != 0) {
3010 error("sif6addr_rtnetlink: %s (line %d)", strerror(-nlresp.nlerr.error), __LINE__);
3017 /********************************************************************
3019 * sif6addr - Config the interface with an IPv6 link-local address
3021 int sif6addr (int unit, eui64_t our_eui64, eui64_t his_eui64)
3023 struct in6_ifreq ifr6;
3025 struct in6_rtmsg rt6;
3029 error("IPv6 socket creation failed: %m");
3032 memset(&ifr, 0, sizeof (ifr));
3033 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
3034 if (ioctl(sock6_fd, SIOCGIFINDEX, (caddr_t) &ifr) < 0) {
3035 error("sif6addr: ioctl(SIOCGIFINDEX): %m (line %d)", __LINE__);
3039 if (kernel_version >= KVERSION(2,1,16)) {
3040 /* Set both local address and remote peer address (with route for it) via rtnetlink */
3041 return sif6addr_rtnetlink(ifr.ifr_ifindex, our_eui64, his_eui64);
3043 /* Local interface */
3044 memset(&ifr6, 0, sizeof(ifr6));
3045 IN6_LLADDR_FROM_EUI64(ifr6.ifr6_addr, our_eui64);
3046 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
3047 ifr6.ifr6_prefixlen = 128;
3049 if (ioctl(sock6_fd, SIOCSIFADDR, &ifr6) < 0) {
3050 error("sif6addr: ioctl(SIOCSIFADDR): %m (line %d)", __LINE__);
3055 * Linux kernel does not provide AF_INET6 ioctl SIOCSIFDSTADDR for
3056 * setting remote peer host address, so set only route to remote host.
3059 /* Route to remote host */
3060 memset(&rt6, 0, sizeof(rt6));
3061 IN6_LLADDR_FROM_EUI64(rt6.rtmsg_dst, his_eui64);
3062 rt6.rtmsg_flags = RTF_UP;
3063 rt6.rtmsg_dst_len = 128;
3064 rt6.rtmsg_ifindex = ifr.ifr_ifindex;
3065 rt6.rtmsg_metric = 1;
3067 if (ioctl(sock6_fd, SIOCADDRT, &rt6) < 0) {
3068 error("sif6addr: ioctl(SIOCADDRT): %m (line %d)", __LINE__);
3077 /********************************************************************
3079 * cif6addr - Remove IPv6 address from interface
3081 int cif6addr (int unit, eui64_t our_eui64, eui64_t his_eui64)
3084 struct in6_ifreq ifr6;
3088 error("IPv6 socket creation failed: %m");
3091 memset(&ifr, 0, sizeof(ifr));
3092 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
3093 if (ioctl(sock6_fd, SIOCGIFINDEX, (caddr_t) &ifr) < 0) {
3094 error("cif6addr: ioctl(SIOCGIFINDEX): %m (line %d)", __LINE__);
3098 memset(&ifr6, 0, sizeof(ifr6));
3099 IN6_LLADDR_FROM_EUI64(ifr6.ifr6_addr, our_eui64);
3100 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
3101 ifr6.ifr6_prefixlen = 128;
3103 if (ioctl(sock6_fd, SIOCDIFADDR, &ifr6) < 0) {
3104 if (errno != EADDRNOTAVAIL) {
3105 if (! ok_error (errno))
3106 error("cif6addr: ioctl(SIOCDIFADDR): %m (line %d)", __LINE__);
3109 warn("cif6addr: ioctl(SIOCDIFADDR): No such address");
3118 * get_pty - get a pty master/slave pair and chown the slave side
3119 * to the uid given. Assumes slave_name points to >= 16 bytes of space.
3122 get_pty(int *master_fdp, int *slave_fdp, char *slave_name, int uid)
3124 int i, mfd, sfd = -1;
3126 struct termios tios;
3130 * Try the unix98 way first.
3132 mfd = open("/dev/ptmx", O_RDWR);
3135 if (ioctl(mfd, TIOCGPTN, &ptn) >= 0) {
3136 slprintf(pty_name, sizeof(pty_name), "/dev/pts/%d", ptn);
3137 chmod(pty_name, S_IRUSR | S_IWUSR);
3140 if (ioctl(mfd, TIOCSPTLCK, &ptn) < 0)
3141 warn("Couldn't unlock pty slave %s: %m", pty_name);
3143 if ((sfd = open(pty_name, O_RDWR | O_NOCTTY)) < 0)
3145 warn("Couldn't open pty slave %s: %m", pty_name);
3150 #endif /* TIOCGPTN */
3153 /* the old way - scan through the pty name space */
3154 for (i = 0; i < 64; ++i) {
3155 slprintf(pty_name, sizeof(pty_name), "/dev/pty%c%x",
3156 'p' + i / 16, i % 16);
3157 mfd = open(pty_name, O_RDWR, 0);
3160 sfd = open(pty_name, O_RDWR | O_NOCTTY, 0);
3162 fchown(sfd, uid, -1);
3163 fchmod(sfd, S_IRUSR | S_IWUSR);
3174 strlcpy(slave_name, pty_name, 16);
3177 if (tcgetattr(sfd, &tios) == 0) {
3178 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
3179 tios.c_cflag |= CS8 | CREAD | CLOCAL;
3180 tios.c_iflag = IGNPAR;
3183 if (tcsetattr(sfd, TCSAFLUSH, &tios) < 0)
3184 warn("couldn't set attributes on pty: %m");
3186 warn("couldn't get attributes on pty: %m");
3191 /********************************************************************
3193 * open_loopback - open the device we use for getting packets
3194 * in demand mode. Under Linux, we use a pty master/slave pair.
3197 open_ppp_loopback(void)
3202 if (new_style_driver) {
3203 /* allocate ourselves a ppp unit */
3204 if (make_ppp_unit() < 0)
3206 modify_flags(ppp_dev_fd, 0, SC_LOOP_TRAFFIC);
3207 set_kdebugflag(kdebugflag);
3212 if (!get_pty(&master_fd, &slave_fd, loop_name, 0))
3213 fatal("No free pty for loopback");
3215 set_ppp_fd(slave_fd);
3217 flags = fcntl(master_fd, F_GETFL);
3219 fcntl(master_fd, F_SETFL, flags | O_NONBLOCK) == -1)
3220 warn("couldn't set master loopback to nonblock: %m");
3222 flags = fcntl(ppp_fd, F_GETFL);
3224 fcntl(ppp_fd, F_SETFL, flags | O_NONBLOCK) == -1)
3225 warn("couldn't set slave loopback to nonblock: %m");
3227 if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0)
3228 fatal("ioctl(TIOCSETD): %m (line %d)", __LINE__);
3230 * Find out which interface we were given.
3232 if (ioctl(ppp_fd, PPPIOCGUNIT, &ifunit) < 0)
3233 fatal("ioctl(PPPIOCGUNIT): %m (line %d)", __LINE__);
3235 * Enable debug in the driver if requested.
3237 set_kdebugflag (kdebugflag);
3242 /********************************************************************
3244 * sifnpmode - Set the mode for handling packets for a given NP.
3248 sifnpmode(int u, int proto, enum NPmode mode)
3252 npi.protocol = proto;
3254 if (ioctl(ppp_dev_fd, PPPIOCSNPMODE, (caddr_t) &npi) < 0) {
3255 if (! ok_error (errno))
3256 error("ioctl(PPPIOCSNPMODE, %d, %d): %m", proto, mode);
3263 /********************************************************************
3265 * sipxfaddr - Config the interface IPX networknumber
3268 int sipxfaddr (int unit, unsigned long int network, unsigned char * node )
3275 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
3277 skfd = socket (AF_IPX, SOCK_DGRAM, 0);
3279 if (! ok_error (errno))
3280 dbglog("socket(AF_IPX): %m (line %d)", __LINE__);
3284 memset (&ifr, '\0', sizeof (ifr));
3285 strlcpy (ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
3287 memcpy (sipx->sipx_node, node, IPX_NODE_LEN);
3288 sipx->sipx_family = AF_IPX;
3289 sipx->sipx_port = 0;
3290 sipx->sipx_network = htonl (network);
3291 sipx->sipx_type = IPX_FRAME_ETHERII;
3292 sipx->sipx_action = IPX_CRTITF;
3294 * Set the IPX device
3296 if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
3298 if (errno != EEXIST) {
3299 if (! ok_error (errno))
3300 dbglog("ioctl(SIOCSIFADDR, CRTITF): %m (line %d)", __LINE__);
3303 warn("ioctl(SIOCSIFADDR, CRTITF): Address already exists");
3312 /********************************************************************
3314 * cipxfaddr - Clear the information for the IPX network. The IPX routes
3315 * are removed and the device is no longer able to pass IPX
3319 int cipxfaddr (int unit)
3326 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
3328 skfd = socket (AF_IPX, SOCK_DGRAM, 0);
3330 if (! ok_error (errno))
3331 dbglog("socket(AF_IPX): %m (line %d)", __LINE__);
3335 memset (&ifr, '\0', sizeof (ifr));
3336 strlcpy (ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
3338 sipx->sipx_type = IPX_FRAME_ETHERII;
3339 sipx->sipx_action = IPX_DLTITF;
3340 sipx->sipx_family = AF_IPX;
3342 * Set the IPX device
3344 if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
3345 if (! ok_error (errno))
3346 info("ioctl(SIOCSIFADDR, IPX_DLTITF): %m (line %d)", __LINE__);
3356 * Use the hostname as part of the random number seed.
3365 for (p = hostname; *p != 0; ++p)
3370 /********************************************************************
3372 * sys_check_options - check the options that the user specified
3376 sys_check_options(void)
3380 * Disable the IPX protocol if the support is not present in the kernel.
3384 if (ipxcp_protent.enabled_flag) {
3385 struct stat stat_buf;
3386 if ( ((path = path_to_procfs("/net/ipx/interface")) == NULL
3387 && (path = path_to_procfs("/net/ipx_interface")) == NULL)
3388 || lstat(path, &stat_buf) < 0) {
3389 error("IPX support is not present in the kernel\n");
3390 ipxcp_protent.enabled_flag = 0;
3394 if (demand && driver_is_old) {
3395 option_error("demand dialling is not supported by kernel driver "
3396 "version %d.%d.%d", driver_version, driver_modification,
3400 if (multilink && !new_style_driver) {
3401 warn("Warning: multilink is not supported by the kernel driver");
3407 /********************************************************************
3409 * get_time - Get current time, monotonic if possible.
3412 get_time(struct timeval *tv)
3414 /* Old glibc (< 2.3.4) does define CLOCK_MONOTONIC, but kernel may have it.
3415 * Runtime checking makes it safe. */
3416 #ifndef CLOCK_MONOTONIC
3417 #define CLOCK_MONOTONIC 1
3419 static int monotonic = -1;
3424 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
3428 tv->tv_sec = ts.tv_sec;
3429 tv->tv_usec = ts.tv_nsec / 1000;
3432 } else if (monotonic > 0)
3436 warn("Couldn't use monotonic clock source: %m");
3439 return gettimeofday(tv, NULL);