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 */
108 #include <net/if_arp.h>
109 #include <net/route.h>
110 #include <netinet/if_ether.h>
112 #include <linux/types.h>
113 #include <linux/if.h>
114 #include <linux/if_arp.h>
115 #include <linux/route.h>
116 #include <linux/if_ether.h>
118 #include <netinet/in.h>
119 #include <arpa/inet.h>
121 #include <linux/ppp_defs.h>
122 #include <linux/if_ppp.h>
130 #if __GLIBC__ >= 2 && \
131 !(defined(__powerpc__) && __GLIBC__ == 2 && __GLIBC_MINOR__ == 0)
132 #include <netipx/ipx.h>
134 #include <linux/ipx.h>
136 #endif /* IPX_CHANGE */
139 #include <pcap-bpf.h>
140 #include <linux/filter.h>
141 #endif /* PPP_FILTER */
144 #include <sys/locks.h>
150 * This is in linux/include/net/ipv6.h.
154 struct in6_addr ifr6_addr;
155 __u32 ifr6_prefixlen;
156 unsigned int ifr6_ifindex;
160 #define IN6_LLADDR_FROM_EUI64(sin6, eui64) do { \
161 memset(&sin6.s6_addr, 0, sizeof(struct in6_addr)); \
162 sin6.s6_addr16[0] = htons(0xfe80); \
163 eui64_copy(eui64, sin6.s6_addr32[2]); \
166 static const eui64_t nulleui64;
169 /* We can get an EIO error on an ioctl if the modem has hung up */
170 #define ok_error(num) ((num)==EIO)
172 static int tty_disc = N_TTY; /* The TTY discipline */
173 static int ppp_disc = N_PPP; /* The PPP discpline */
174 static int initfdflags = -1; /* Initial file descriptor flags for fd */
175 static int ppp_fd = -1; /* fd which is set to PPP discipline */
176 static int sock_fd = -1; /* socket for doing interface ioctls */
177 static int slave_fd = -1; /* pty for old-style demand mode, slave */
178 static int master_fd = -1; /* pty for old-style demand mode, master */
180 static int sock6_fd = -1;
184 * For the old-style kernel driver, this is the same as ppp_fd.
185 * For the new-style driver, it is the fd of an instance of /dev/ppp
186 * which is attached to the ppp unit and is used for controlling it.
188 int ppp_dev_fd = -1; /* fd for /dev/ppp (new style driver) */
190 static int chindex; /* channel index (new style driver) */
192 static fd_set in_fds; /* set of fds that wait_input waits for */
193 static int max_in_fd; /* highest fd set in in_fds */
195 static int has_proxy_arp = 0;
196 static int driver_version = 0;
197 static int driver_modification = 0;
198 static int driver_patch = 0;
199 static int driver_is_old = 0;
200 static int restore_term = 0; /* 1 => we've munged the terminal */
201 static struct termios inittermios; /* Initial TTY termios */
203 int new_style_driver = 0;
205 static char loop_name[20];
206 static unsigned char inbuf[512]; /* buffer for chars read from loopback */
208 static int if_is_up; /* Interface has been marked up */
209 static int if6_is_up; /* Interface has been marked up for IPv6, to help differentiate */
210 static int have_default_route; /* Gateway for default route added */
211 static int have_default_route6; /* Gateway for default IPv6 route added */
212 static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
213 static char proxy_arp_dev[16]; /* Device for proxy arp entry */
214 static u_int32_t our_old_addr; /* for detecting address changes */
215 static int dynaddr_set; /* 1 if ip_dynaddr set */
216 static int looped; /* 1 if using loop */
217 static int link_mtu; /* mtu for the link (not bundle) */
219 static struct utsname utsname; /* for the kernel version */
220 static int kernel_version;
221 #define KVERSION(j,n,p) ((j)*1000000 + (n)*1000 + (p))
225 #define FLAGS_GOOD (IFF_UP | IFF_BROADCAST)
226 #define FLAGS_MASK (IFF_UP | IFF_BROADCAST | \
227 IFF_POINTOPOINT | IFF_LOOPBACK | IFF_NOARP)
229 #define SIN_ADDR(x) (((struct sockaddr_in *) (&(x)))->sin_addr.s_addr)
231 /* Prototypes for procedures local to this file. */
232 static int modify_flags(int fd, int clear_bits, int set_bits);
233 static int translate_speed (int bps);
234 static int baud_rate_of (int speed);
235 static void close_route_table (void);
236 static int open_route_table (void);
237 static int read_route_table (struct rtentry *rt);
238 static int defaultroute_exists (struct rtentry *rt, int metric);
239 static int defaultroute6_exists (struct in6_rtmsg *rt, int metric);
240 static int get_ether_addr (u_int32_t ipaddr, struct sockaddr *hwaddr,
241 char *name, int namelen);
242 static void decode_version (char *buf, int *version, int *mod, int *patch);
243 static int set_kdebugflag(int level);
244 static int ppp_registered(void);
245 static int make_ppp_unit(void);
246 static int setifstate (int u, int state);
248 extern u_char inpacket_buf[]; /* borrowed from main.c */
250 extern int dfl_route_metric;
253 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
257 #define SET_SA_FAMILY(addr, family) \
258 memset ((char *) &(addr), '\0', sizeof(addr)); \
259 addr.sa_family = (family);
262 * Determine if the PPP connection should still be present.
267 /* new_fd is the fd of a tty */
268 static void set_ppp_fd (int new_fd)
271 if (!new_style_driver)
275 static int still_ppp(void)
277 if (new_style_driver)
278 return !hungup && ppp_fd >= 0;
279 if (!hungup || ppp_fd == slave_fd)
282 set_ppp_fd(slave_fd);
289 * modify_flags - set and clear flag bits controlling the kernel
292 static int modify_flags(int fd, int clear_bits, int set_bits)
296 if (ioctl(fd, PPPIOCGFLAGS, &flags) == -1)
298 flags = (flags & ~clear_bits) | set_bits;
299 if (ioctl(fd, PPPIOCSFLAGS, &flags) == -1)
306 error("Failed to set PPP kernel option flags: %m");
310 /********************************************************************
312 * sys_init - System-dependent initialization.
317 /* Get an internet socket for doing socket ioctls. */
318 sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
320 fatal("Couldn't create IP socket: %m(%d)", errno);
323 sock6_fd = socket(AF_INET6, SOCK_DGRAM, 0);
325 sock6_fd = -errno; /* save errno for later */
332 /********************************************************************
334 * sys_cleanup - restore any system state we modified before exiting:
335 * mark the interface down, delete default route and/or proxy arp entry.
336 * This shouldn't call die() because it's called from die().
339 void sys_cleanup(void)
342 * Take down the device
352 * Delete any routes through the device.
354 if (have_default_route)
355 cifdefaultroute(0, 0, 0);
357 if (have_default_route6)
358 cif6defaultroute(0, nulleui64, nulleui64);
362 cifproxyarp(0, proxy_arp_addr);
365 /********************************************************************
367 * sys_close - Clean up in a child process before execing.
372 if (new_style_driver && ppp_dev_fd >= 0)
386 /********************************************************************
388 * set_kdebugflag - Define the debugging level for the kernel
391 static int set_kdebugflag (int requested_level)
395 if (ioctl(ppp_dev_fd, PPPIOCSDEBUG, &requested_level) < 0) {
396 if ( ! ok_error (errno) )
397 error("ioctl(PPPIOCSDEBUG): %m (line %d)", __LINE__);
403 /********************************************************************
405 * tty_establish_ppp - Turn the serial port into a ppp interface.
408 int tty_establish_ppp (int tty_fd)
413 * Ensure that the tty device is in exclusive mode.
415 if (ioctl(tty_fd, TIOCEXCL, 0) < 0) {
416 if ( ! ok_error ( errno ))
417 warn("Couldn't make tty exclusive: %m");
420 * Demand mode - prime the old ppp device to relinquish the unit.
422 if (!new_style_driver && looped
423 && ioctl(slave_fd, PPPIOCXFERUNIT, 0) < 0) {
424 error("ioctl(transfer ppp unit): %m, line %d", __LINE__);
428 * Set the current tty to the PPP discpline
432 #define N_SYNC_PPP 14
434 ppp_disc = (new_style_driver && sync_serial)? N_SYNC_PPP: N_PPP;
435 if (ioctl(tty_fd, TIOCSETD, &ppp_disc) < 0) {
436 if ( ! ok_error (errno) ) {
437 error("Couldn't set tty to PPP discipline: %m");
442 ret_fd = generic_establish_ppp(tty_fd);
444 #define SC_RCVB (SC_RCV_B7_0 | SC_RCV_B7_1 | SC_RCV_EVNP | SC_RCV_ODDP)
445 #define SC_LOGB (SC_DEBUG | SC_LOG_INPKT | SC_LOG_OUTPKT | SC_LOG_RAWIN \
449 modify_flags(ppp_fd, SC_RCVB | SC_LOGB,
450 (kdebugflag * SC_DEBUG) & SC_LOGB);
452 if (ioctl(tty_fd, TIOCSETD, &tty_disc) < 0 && !ok_error(errno))
453 warn("Couldn't reset tty to normal line discipline: %m");
459 /********************************************************************
461 * generic_establish_ppp - Turn the fd into a ppp interface.
463 int generic_establish_ppp (int fd)
467 if (new_style_driver) {
470 /* If a ppp_fd is already open, close it first */
477 /* Open an instance of /dev/ppp and connect the channel to it */
478 if (ioctl(fd, PPPIOCGCHAN, &chindex) == -1) {
479 error("Couldn't get channel number: %m");
482 dbglog("using channel %d", chindex);
483 fd = open("/dev/ppp", O_RDWR);
485 error("Couldn't reopen /dev/ppp: %m");
488 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
489 if (ioctl(fd, PPPIOCATTCHAN, &chindex) < 0) {
490 error("Couldn't attach to channel %d: %m", chindex);
493 flags = fcntl(fd, F_GETFL);
494 if (flags == -1 || fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
495 warn("Couldn't set /dev/ppp (channel) to nonblock: %m");
500 if (!looped && !multilink) {
502 * Create a new PPP unit.
504 if (make_ppp_unit() < 0)
509 modify_flags(ppp_dev_fd, SC_LOOP_TRAFFIC, 0);
513 if (ioctl(fd, PPPIOCCONNECT, &ifunit) < 0) {
514 error("Couldn't attach to PPP unit %d: %m", ifunit);
521 * Old-style driver: find out which interface we were given.
524 if (ioctl(fd, PPPIOCGUNIT, &x) < 0) {
525 if (ok_error (errno))
527 fatal("ioctl(PPPIOCGUNIT): %m (line %d)", __LINE__);
529 /* Check that we got the same unit again. */
530 if (looped && x != ifunit)
531 fatal("transfer_ppp failed: wanted unit %d, got %d", ifunit, x);
535 * Fetch the initial file flags and reset blocking mode on the file.
537 initfdflags = fcntl(fd, F_GETFL);
538 if (initfdflags == -1 ||
539 fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
540 if ( ! ok_error (errno))
541 warn("Couldn't set device to non-blocking mode: %m");
546 * Enable debug in the driver if requested.
549 set_kdebugflag (kdebugflag);
561 /********************************************************************
563 * tty_disestablish_ppp - Restore the serial port to normal operation.
564 * This shouldn't call die() because it's called from die().
567 void tty_disestablish_ppp(int tty_fd)
571 * Flush the tty output buffer so that the TIOCSETD doesn't hang.
573 if (tcflush(tty_fd, TCIOFLUSH) < 0)
575 warn("tcflush failed: %m");
579 * Restore the previous line discipline
581 if (ioctl(tty_fd, TIOCSETD, &tty_disc) < 0) {
582 if ( ! ok_error (errno))
583 error("ioctl(TIOCSETD, N_TTY): %m (line %d)", __LINE__);
586 if (ioctl(tty_fd, TIOCNXCL, 0) < 0) {
587 if ( ! ok_error (errno))
588 warn("ioctl(TIOCNXCL): %m (line %d)", __LINE__);
591 /* Reset non-blocking mode on fd. */
592 if (initfdflags != -1 && fcntl(tty_fd, F_SETFL, initfdflags) < 0) {
593 if ( ! ok_error (errno))
594 warn("Couldn't restore device fd flags: %m");
600 generic_disestablish_ppp(tty_fd);
603 /********************************************************************
605 * generic_disestablish_ppp - Restore device components to normal
606 * operation, and reconnect the ppp unit to the loopback if in demand
607 * mode. This shouldn't call die() because it's called from die().
609 void generic_disestablish_ppp(int dev_fd)
611 if (new_style_driver) {
615 modify_flags(ppp_dev_fd, 0, SC_LOOP_TRAFFIC);
617 } else if (!doing_multilink && ppp_dev_fd >= 0) {
619 remove_fd(ppp_dev_fd);
623 /* old-style driver */
625 set_ppp_fd(slave_fd);
632 * make_ppp_unit - make a new ppp unit for ppp_dev_fd.
633 * Assumes new_style_driver.
635 static int make_ppp_unit(void)
639 if (ppp_dev_fd >= 0) {
640 dbglog("in make_ppp_unit, already had /dev/ppp open?");
643 ppp_dev_fd = open("/dev/ppp", O_RDWR);
645 fatal("Couldn't open /dev/ppp: %m");
646 flags = fcntl(ppp_dev_fd, F_GETFL);
648 || fcntl(ppp_dev_fd, F_SETFL, flags | O_NONBLOCK) == -1)
649 warn("Couldn't set /dev/ppp to nonblock: %m");
652 x = ioctl(ppp_dev_fd, PPPIOCNEWUNIT, &ifunit);
653 if (x < 0 && req_unit >= 0 && errno == EEXIST) {
654 warn("Couldn't allocate PPP unit %d as it is already in use", req_unit);
656 x = ioctl(ppp_dev_fd, PPPIOCNEWUNIT, &ifunit);
659 error("Couldn't create new ppp unit: %m");
661 if (x == 0 && req_ifname[0] != '\0') {
663 char t[MAXIFNAMELEN];
664 memset(&ifr, 0, sizeof(struct ifreq));
665 slprintf(t, sizeof(t), "%s%d", PPP_DRV_NAME, ifunit);
666 strlcpy(ifr.ifr_name, t, IF_NAMESIZE);
667 strlcpy(ifr.ifr_newname, req_ifname, IF_NAMESIZE);
668 x = ioctl(sock_fd, SIOCSIFNAME, &ifr);
670 error("Couldn't rename interface %s to %s: %m", t, req_ifname);
672 info("Renamed interface %s to %s", t, req_ifname);
679 * cfg_bundle - configure the existing bundle.
680 * Used in demand mode.
682 void cfg_bundle(int mrru, int mtru, int rssn, int tssn)
684 if (!new_style_driver)
687 /* set the mrru, mtu and flags */
688 if (ioctl(ppp_dev_fd, PPPIOCSMRRU, &mrru) < 0)
689 error("Couldn't set MRRU: %m");
691 modify_flags(ppp_dev_fd, SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ|SC_MULTILINK,
692 ((rssn? SC_MP_SHORTSEQ: 0) | (tssn? SC_MP_XSHORTSEQ: 0)
693 | (mrru? SC_MULTILINK: 0)));
695 /* connect up the channel */
696 if (ioctl(ppp_fd, PPPIOCCONNECT, &ifunit) < 0)
697 fatal("Couldn't attach to PPP unit %d: %m", ifunit);
702 * make_new_bundle - create a new PPP unit (i.e. a bundle)
703 * and connect our channel to it. This should only get called
704 * if `multilink' was set at the time establish_ppp was called.
705 * In demand mode this uses our existing bundle instead of making
708 void make_new_bundle(int mrru, int mtru, int rssn, int tssn)
710 if (!new_style_driver)
713 /* make us a ppp unit */
714 if (make_ppp_unit() < 0)
717 /* set the mrru and flags */
718 cfg_bundle(mrru, mtru, rssn, tssn);
722 * bundle_attach - attach our link to a given PPP unit.
723 * We assume the unit is controlled by another pppd.
725 int bundle_attach(int ifnum)
729 if (!new_style_driver)
732 master_fd = open("/dev/ppp", O_RDWR);
734 fatal("Couldn't open /dev/ppp: %m");
735 if (ioctl(master_fd, PPPIOCATTACH, &ifnum) < 0) {
736 if (errno == ENXIO) {
738 return 0; /* doesn't still exist */
740 fatal("Couldn't attach to interface unit %d: %m\n", ifnum);
742 if (ioctl(ppp_fd, PPPIOCCONNECT, &ifnum) < 0)
743 fatal("Couldn't connect to interface unit %d: %m", ifnum);
744 modify_flags(master_fd, 0, SC_MULTILINK);
752 * destroy_bundle - tell the driver to destroy our bundle.
754 void destroy_bundle(void)
756 if (ppp_dev_fd >= 0) {
758 remove_fd(ppp_dev_fd);
763 /********************************************************************
765 * clean_check - Fetch the flags for the device and generate
766 * appropriate error messages.
768 void clean_check(void)
774 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
776 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
778 s = "all had bit 7 set to 1";
782 s = "all had bit 7 set to 0";
786 s = "all had odd parity";
790 s = "all had even parity";
795 warn("Receive serial link is not 8-bit clean:");
796 warn("Problem: %s", s);
804 * List of valid speeds.
808 int speed_int, speed_val;
889 { 1000000, B1000000 },
892 { 1152000, B1152000 },
895 { 1500000, B1500000 },
898 { 2000000, B2000000 },
901 { 2500000, B2500000 },
904 { 3000000, B3000000 },
907 { 3500000, B3500000 },
910 { 4000000, B4000000 },
915 /********************************************************************
917 * Translate from bits/second to a speed_t.
920 static int translate_speed (int bps)
922 struct speed *speedp;
925 for (speedp = speeds; speedp->speed_int; speedp++) {
926 if (bps == speedp->speed_int)
927 return speedp->speed_val;
929 warn("speed %d not supported", bps);
934 /********************************************************************
936 * Translate from a speed_t to bits/second.
939 static int baud_rate_of (int speed)
941 struct speed *speedp;
944 for (speedp = speeds; speedp->speed_int; speedp++) {
945 if (speed == speedp->speed_val)
946 return speedp->speed_int;
952 /********************************************************************
954 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
955 * at the requested speed, etc. If `local' is true, set CLOCAL
956 * regardless of whether the modem option was specified.
959 void set_up_tty(int tty_fd, int local)
965 if (tcgetattr(tty_fd, &tios) < 0) {
966 if (!ok_error(errno))
967 fatal("tcgetattr: %m (line %d)", __LINE__);
974 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
975 tios.c_cflag |= CS8 | CREAD | HUPCL;
977 tios.c_iflag = IGNBRK | IGNPAR;
981 tios.c_cc[VTIME] = 0;
984 tios.c_cflag ^= (CLOCAL | HUPCL);
988 tios.c_cflag |= CRTSCTS;
992 tios.c_iflag |= IXON | IXOFF;
993 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
994 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
998 tios.c_cflag &= ~CRTSCTS;
1006 tios.c_cflag |= CSTOPB;
1008 speed = translate_speed(inspeed);
1010 cfsetospeed (&tios, speed);
1011 cfsetispeed (&tios, speed);
1014 * We can't proceed if the serial port speed is B0,
1015 * since that implies that the serial port is disabled.
1018 speed = cfgetospeed(&tios);
1020 fatal("Baud rate for %s is 0; need explicit baud rate", devnam);
1023 while (tcsetattr(tty_fd, TCSAFLUSH, &tios) < 0 && !ok_error(errno))
1025 fatal("tcsetattr: %m (line %d)", __LINE__);
1027 baud_rate = baud_rate_of(speed);
1031 /********************************************************************
1033 * setdtr - control the DTR line on the serial port.
1034 * This is called from die(), so it shouldn't call die().
1037 void setdtr (int tty_fd, int on)
1039 int modembits = TIOCM_DTR;
1041 ioctl(tty_fd, (on ? TIOCMBIS : TIOCMBIC), &modembits);
1044 /********************************************************************
1046 * restore_tty - restore the terminal to the saved settings.
1049 void restore_tty (int tty_fd)
1054 * Turn off echoing, because otherwise we can get into
1055 * a loop with the tty and the modem echoing to each other.
1056 * We presume we are the sole user of this tty device, so
1057 * when we close it, it will revert to its defaults anyway.
1059 if (!default_device)
1060 inittermios.c_lflag &= ~(ECHO | ECHONL);
1062 if (tcsetattr(tty_fd, TCSAFLUSH, &inittermios) < 0) {
1063 if (! ok_error (errno))
1064 warn("tcsetattr: %m (line %d)", __LINE__);
1069 /********************************************************************
1071 * output - Output PPP packet.
1074 void output (int unit, unsigned char *p, int len)
1079 dump_packet("sent", p, len);
1080 if (snoop_send_hook) snoop_send_hook(p, len);
1082 if (len < PPP_HDRLEN)
1084 if (new_style_driver) {
1087 proto = (p[0] << 8) + p[1];
1088 if (ppp_dev_fd >= 0 && !(proto >= 0xc000 || proto == PPP_CCPFRAG))
1091 if (write(fd, p, len) < 0) {
1092 if (errno == EWOULDBLOCK || errno == EAGAIN || errno == ENOBUFS
1093 || errno == ENXIO || errno == EIO || errno == EINTR)
1094 warn("write: warning: %m (%d)", errno);
1096 error("write: %m (%d)", errno);
1100 /********************************************************************
1102 * wait_input - wait until there is data available,
1103 * for the length of time specified by *timo (indefinite
1107 void wait_input(struct timeval *timo)
1114 n = select(max_in_fd + 1, &ready, NULL, &exc, timo);
1115 if (n < 0 && errno != EINTR)
1116 fatal("select: %m");
1120 * add_fd - add an fd to the set that wait_input waits for.
1124 if (fd >= FD_SETSIZE)
1125 fatal("internal error: file descriptor too large (%d)", fd);
1126 FD_SET(fd, &in_fds);
1132 * remove_fd - remove an fd from the set that wait_input waits for.
1134 void remove_fd(int fd)
1136 FD_CLR(fd, &in_fds);
1140 /********************************************************************
1142 * read_packet - get a PPP packet from the serial device.
1145 int read_packet (unsigned char *buf)
1149 len = PPP_MRU + PPP_HDRLEN;
1150 if (new_style_driver) {
1151 *buf++ = PPP_ALLSTATIONS;
1157 nr = read(ppp_fd, buf, len);
1158 if (nr < 0 && errno != EWOULDBLOCK && errno != EAGAIN
1159 && errno != EIO && errno != EINTR)
1161 if (nr < 0 && errno == ENXIO)
1164 if (nr < 0 && new_style_driver && ppp_dev_fd >= 0 && !bundle_eof) {
1165 /* N.B. we read ppp_fd first since LCP packets come in there. */
1166 nr = read(ppp_dev_fd, buf, len);
1167 if (nr < 0 && errno != EWOULDBLOCK && errno != EAGAIN
1168 && errno != EIO && errno != EINTR)
1169 error("read /dev/ppp: %m");
1170 if (nr < 0 && errno == ENXIO)
1172 if (nr == 0 && doing_multilink) {
1173 remove_fd(ppp_dev_fd);
1177 if (new_style_driver && ppp_fd < 0 && ppp_dev_fd < 0)
1179 return (new_style_driver && nr > 0)? nr+2: nr;
1182 /********************************************************************
1184 * get_loop_output - get outgoing packets from the ppp device,
1185 * and detect when we want to bring the real link up.
1186 * Return value is 1 if we need to bring up the link, 0 otherwise.
1189 get_loop_output(void)
1194 if (new_style_driver) {
1195 while ((n = read_packet(inpacket_buf)) > 0)
1196 if (loop_frame(inpacket_buf, n))
1201 while ((n = read(master_fd, inbuf, sizeof(inbuf))) > 0)
1202 if (loop_chars(inbuf, n))
1206 fatal("eof on loopback");
1208 if (errno != EWOULDBLOCK && errno != EAGAIN)
1209 fatal("read from loopback: %m(%d)", errno);
1215 * netif_set_mtu - set the MTU on the PPP network interface.
1218 netif_set_mtu(int unit, int mtu)
1222 memset (&ifr, '\0', sizeof (ifr));
1223 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1226 if (ifunit >= 0 && ioctl(sock_fd, SIOCSIFMTU, (caddr_t) &ifr) < 0)
1227 error("ioctl(SIOCSIFMTU): %m (line %d)", __LINE__);
1231 * netif_get_mtu - get the MTU on the PPP network interface.
1234 netif_get_mtu(int unit)
1238 memset (&ifr, '\0', sizeof (ifr));
1239 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1241 if (ifunit >= 0 && ioctl(sock_fd, SIOCGIFMTU, (caddr_t) &ifr) < 0) {
1242 error("ioctl(SIOCGIFMTU): %m (line %d)", __LINE__);
1248 /********************************************************************
1250 * tty_send_config - configure the transmit characteristics of
1251 * the ppp interface.
1254 void tty_send_config(int mtu, u_int32_t asyncmap, int pcomp, int accomp)
1261 if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
1262 if (errno != EIO && errno != ENOTTY)
1263 error("Couldn't set transmit async character map: %m");
1268 x = (pcomp? SC_COMP_PROT: 0) | (accomp? SC_COMP_AC: 0)
1269 | (sync_serial? SC_SYNC: 0);
1270 modify_flags(ppp_fd, SC_COMP_PROT|SC_COMP_AC|SC_SYNC, x);
1273 /********************************************************************
1275 * tty_set_xaccm - set the extended transmit ACCM for the interface.
1278 void tty_set_xaccm (ext_accm accm)
1282 if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY) {
1283 if ( ! ok_error (errno))
1284 warn("ioctl(set extended ACCM): %m (line %d)", __LINE__);
1288 /********************************************************************
1290 * tty_recv_config - configure the receive-side characteristics of
1291 * the ppp interface.
1294 void tty_recv_config(int mru, u_int32_t asyncmap, int pcomp, int accomp)
1297 * If we were called because the link has gone down then there is nothing
1298 * which may be done. Just return without incident.
1303 * Set the receiver parameters
1305 if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
1306 if (errno != EIO && errno != ENOTTY)
1307 error("Couldn't set channel receive MRU: %m");
1309 if (new_style_driver && ppp_dev_fd >= 0
1310 && ioctl(ppp_dev_fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
1311 error("Couldn't set MRU in generic PPP layer: %m");
1313 if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
1314 if (errno != EIO && errno != ENOTTY)
1315 error("Couldn't set channel receive asyncmap: %m");
1319 /********************************************************************
1321 * ccp_test - ask kernel whether a given compression method
1322 * is acceptable for use.
1326 ccp_test(int unit, u_char *opt_ptr, int opt_len, int for_transmit)
1328 struct ppp_option_data data;
1330 memset (&data, '\0', sizeof (data));
1332 data.length = opt_len;
1333 data.transmit = for_transmit;
1335 if (ioctl(ppp_dev_fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
1338 return (errno == ENOBUFS)? 0: -1;
1341 /********************************************************************
1343 * ccp_flags_set - inform kernel about the current state of CCP.
1346 void ccp_flags_set (int unit, int isopen, int isup)
1350 x = (isopen? SC_CCP_OPEN: 0) | (isup? SC_CCP_UP: 0);
1351 if (still_ppp() && ppp_dev_fd >= 0)
1352 modify_flags(ppp_dev_fd, SC_CCP_OPEN|SC_CCP_UP, x);
1357 * set_filters - set the active and pass filters in the kernel driver.
1359 int set_filters(struct bpf_program *pass, struct bpf_program *active)
1361 struct sock_fprog fp;
1363 fp.len = pass->bf_len;
1364 fp.filter = (struct sock_filter *) pass->bf_insns;
1365 if (ioctl(ppp_dev_fd, PPPIOCSPASS, &fp) < 0) {
1366 if (errno == ENOTTY)
1367 warn("kernel does not support PPP filtering");
1369 error("Couldn't set pass-filter in kernel: %m");
1372 fp.len = active->bf_len;
1373 fp.filter = (struct sock_filter *) active->bf_insns;
1374 if (ioctl(ppp_dev_fd, PPPIOCSACTIVE, &fp) < 0) {
1375 error("Couldn't set active-filter in kernel: %m");
1380 #endif /* PPP_FILTER */
1382 /********************************************************************
1384 * get_idle_time - return how long the link has been idle.
1387 get_idle_time(int u, struct ppp_idle *ip)
1389 return ioctl(ppp_dev_fd, PPPIOCGIDLE, ip) >= 0;
1392 /********************************************************************
1394 * get_ppp_stats - return statistics for the link.
1397 get_ppp_stats(int u, struct pppd_stats *stats)
1399 struct ifpppstatsreq req;
1401 memset (&req, 0, sizeof (req));
1403 req.stats_ptr = (caddr_t) &req.stats;
1404 strlcpy(req.ifr__name, ifname, sizeof(req.ifr__name));
1405 if (ioctl(sock_fd, SIOCGPPPSTATS, &req) < 0) {
1406 error("Couldn't get PPP statistics: %m");
1409 stats->bytes_in = req.stats.p.ppp_ibytes;
1410 stats->bytes_out = req.stats.p.ppp_obytes;
1411 stats->pkts_in = req.stats.p.ppp_ipackets;
1412 stats->pkts_out = req.stats.p.ppp_opackets;
1416 /********************************************************************
1418 * ccp_fatal_error - returns 1 if decompression was disabled as a
1419 * result of an error detected after decompression of a packet,
1420 * 0 otherwise. This is necessary because of patent nonsense.
1423 int ccp_fatal_error (int unit)
1427 if (ioctl(ppp_dev_fd, PPPIOCGFLAGS, &flags) < 0) {
1428 error("Couldn't read compression error flags: %m");
1431 return flags & SC_DC_FERROR;
1434 /********************************************************************
1436 * path_to_procfs - find the path to the proc file system mount point
1438 static char proc_path[MAXPATHLEN];
1439 static int proc_path_len;
1441 static char *path_to_procfs(const char *tail)
1443 struct mntent *mntent;
1446 if (proc_path_len == 0) {
1447 /* Default the mount location of /proc */
1448 strlcpy (proc_path, "/proc", sizeof(proc_path));
1450 fp = fopen(MOUNTED, "r");
1452 while ((mntent = getmntent(fp)) != NULL) {
1453 if (strcmp(mntent->mnt_type, MNTTYPE_IGNORE) == 0)
1455 if (strcmp(mntent->mnt_type, "proc") == 0) {
1456 strlcpy(proc_path, mntent->mnt_dir, sizeof(proc_path));
1457 proc_path_len = strlen(proc_path);
1465 strlcpy(proc_path + proc_path_len, tail,
1466 sizeof(proc_path) - proc_path_len);
1471 * /proc/net/route parsing stuff.
1473 #define ROUTE_MAX_COLS 12
1474 FILE *route_fd = (FILE *) 0;
1475 static char route_buffer[512];
1476 static int route_dev_col, route_dest_col, route_gw_col;
1477 static int route_flags_col, route_metric_col, route_mask_col;
1478 static int route_num_cols;
1480 static int open_route_table (void);
1481 static void close_route_table (void);
1482 static int read_route_table (struct rtentry *rt);
1484 /********************************************************************
1486 * close_route_table - close the interface to the route table
1489 static void close_route_table (void)
1491 if (route_fd != (FILE *) 0) {
1493 route_fd = (FILE *) 0;
1497 /********************************************************************
1499 * open_route_table - open the interface to the route table
1501 static char route_delims[] = " \t\n";
1503 static int open_route_table (void)
1507 close_route_table();
1509 path = path_to_procfs("/net/route");
1510 route_fd = fopen (path, "r");
1511 if (route_fd == NULL) {
1512 error("can't open routing table %s: %m", path);
1516 route_dev_col = 0; /* default to usual columns */
1519 route_flags_col = 3;
1520 route_metric_col = 6;
1524 /* parse header line */
1525 if (fgets(route_buffer, sizeof(route_buffer), route_fd) != 0) {
1526 char *p = route_buffer, *q;
1528 for (col = 0; col < ROUTE_MAX_COLS; ++col) {
1530 if ((q = strtok(p, route_delims)) == 0)
1532 if (strcasecmp(q, "iface") == 0)
1533 route_dev_col = col;
1534 else if (strcasecmp(q, "destination") == 0)
1535 route_dest_col = col;
1536 else if (strcasecmp(q, "gateway") == 0)
1538 else if (strcasecmp(q, "flags") == 0)
1539 route_flags_col = col;
1540 else if (strcasecmp(q, "mask") == 0)
1541 route_mask_col = col;
1544 if (used && col >= route_num_cols)
1545 route_num_cols = col + 1;
1553 /********************************************************************
1555 * read_route_table - read the next entry from the route table
1558 static int read_route_table(struct rtentry *rt)
1560 char *cols[ROUTE_MAX_COLS], *p;
1563 memset (rt, '\0', sizeof (struct rtentry));
1565 if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0)
1569 for (col = 0; col < route_num_cols; ++col) {
1570 cols[col] = strtok(p, route_delims);
1571 if (cols[col] == NULL)
1572 return 0; /* didn't get enough columns */
1576 SIN_ADDR(rt->rt_dst) = strtoul(cols[route_dest_col], NULL, 16);
1577 SIN_ADDR(rt->rt_gateway) = strtoul(cols[route_gw_col], NULL, 16);
1578 SIN_ADDR(rt->rt_genmask) = strtoul(cols[route_mask_col], NULL, 16);
1580 rt->rt_flags = (short) strtoul(cols[route_flags_col], NULL, 16);
1581 rt->rt_metric = (short) strtoul(cols[route_metric_col], NULL, 10);
1582 rt->rt_dev = cols[route_dev_col];
1587 /********************************************************************
1589 * defaultroute_exists - determine if there is a default route
1590 * with the given metric (or negative for any)
1593 static int defaultroute_exists (struct rtentry *rt, int metric)
1597 if (!open_route_table())
1600 while (read_route_table(rt) != 0) {
1601 if ((rt->rt_flags & RTF_UP) == 0)
1604 if (kernel_version > KVERSION(2,1,0) && SIN_ADDR(rt->rt_genmask) != 0)
1606 if (SIN_ADDR(rt->rt_dst) == 0L && (metric < 0
1607 || rt->rt_metric == metric)) {
1613 close_route_table();
1618 * have_route_to - determine if the system has any route to
1619 * a given IP address. `addr' is in network byte order.
1620 * Return value is 1 if yes, 0 if no, -1 if don't know.
1621 * For demand mode to work properly, we have to ignore routes
1622 * through our own interface.
1624 int have_route_to(u_int32_t addr)
1629 if (!open_route_table())
1630 return -1; /* don't know */
1632 while (read_route_table(&rt)) {
1633 if ((rt.rt_flags & RTF_UP) == 0 || strcmp(rt.rt_dev, ifname) == 0)
1635 if ((addr & SIN_ADDR(rt.rt_genmask)) == SIN_ADDR(rt.rt_dst)) {
1641 close_route_table();
1645 /********************************************************************
1647 * sifdefaultroute - assign a default route through the address given.
1650 int sifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
1654 if (defaultroute_exists(&rt, dfl_route_metric) && strcmp(rt.rt_dev, ifname) != 0) {
1655 if (rt.rt_flags & RTF_GATEWAY)
1656 error("not replacing existing default route via %I with metric %d",
1657 SIN_ADDR(rt.rt_gateway), dfl_route_metric);
1659 error("not replacing existing default route through %s with metric %d",
1660 rt.rt_dev, dfl_route_metric);
1664 memset (&rt, 0, sizeof (rt));
1665 SET_SA_FAMILY (rt.rt_dst, AF_INET);
1668 rt.rt_metric = dfl_route_metric + 1; /* +1 for binary compatibility */
1670 if (kernel_version > KVERSION(2,1,0)) {
1671 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
1672 SIN_ADDR(rt.rt_genmask) = 0L;
1675 rt.rt_flags = RTF_UP;
1676 if (ioctl(sock_fd, SIOCADDRT, &rt) < 0) {
1677 if ( ! ok_error ( errno ))
1678 error("default route ioctl(SIOCADDRT): %m");
1682 have_default_route = 1;
1686 /********************************************************************
1688 * cifdefaultroute - delete a default route through the address given.
1691 int cifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
1695 have_default_route = 0;
1697 memset (&rt, '\0', sizeof (rt));
1698 SET_SA_FAMILY (rt.rt_dst, AF_INET);
1699 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
1704 rt.rt_metric = dfl_route_metric + 1; /* +1 for binary compatibility */
1706 if (kernel_version > KVERSION(2,1,0)) {
1707 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
1708 SIN_ADDR(rt.rt_genmask) = 0L;
1711 rt.rt_flags = RTF_UP;
1712 if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH) {
1714 if ( ! ok_error ( errno ))
1715 error("default route ioctl(SIOCDELRT): %m");
1725 * /proc/net/ipv6_route parsing stuff.
1727 static int route_dest_plen_col;
1728 static int open_route6_table (void);
1729 static int read_route6_table (struct in6_rtmsg *rt);
1731 /********************************************************************
1733 * open_route6_table - open the interface to the route table
1735 static int open_route6_table (void)
1739 close_route_table();
1741 path = path_to_procfs("/net/ipv6_route");
1742 route_fd = fopen (path, "r");
1743 if (route_fd == NULL) {
1744 error("can't open routing table %s: %m", path);
1748 /* default to usual columns */
1750 route_dest_plen_col = 1;
1752 route_metric_col = 5;
1753 route_flags_col = 8;
1755 route_num_cols = 10;
1760 /********************************************************************
1762 * read_route6_table - read the next entry from the route table
1765 static void hex_to_in6_addr(struct in6_addr *addr, const char *s)
1772 for (i = 0; i < 4; i++) {
1773 memcpy(hex8, s + 8*i, 8);
1774 v = strtoul(hex8, NULL, 16);
1775 addr->s6_addr32[i] = v;
1779 static int read_route6_table(struct in6_rtmsg *rt)
1781 char *cols[ROUTE_MAX_COLS], *p;
1784 memset (rt, '\0', sizeof (struct in6_rtmsg));
1786 if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0)
1790 for (col = 0; col < route_num_cols; ++col) {
1791 cols[col] = strtok(p, route_delims);
1792 if (cols[col] == NULL)
1793 return 0; /* didn't get enough columns */
1797 hex_to_in6_addr(&rt->rtmsg_dst, cols[route_dest_col]);
1798 rt->rtmsg_dst_len = strtoul(cols[route_dest_plen_col], NULL, 16);
1799 hex_to_in6_addr(&rt->rtmsg_gateway, cols[route_gw_col]);
1801 rt->rtmsg_metric = strtoul(cols[route_metric_col], NULL, 16);
1802 rt->rtmsg_flags = strtoul(cols[route_flags_col], NULL, 16);
1803 rt->rtmsg_ifindex = if_nametoindex(cols[route_dev_col]);
1808 /********************************************************************
1810 * defaultroute6_exists - determine if there is a default route
1813 static int defaultroute6_exists (struct in6_rtmsg *rt, int metric)
1817 if (!open_route6_table())
1820 while (read_route6_table(rt) != 0) {
1821 if ((rt->rtmsg_flags & RTF_UP) == 0)
1824 if (rt->rtmsg_dst_len != 0)
1826 if (rt->rtmsg_dst.s6_addr32[0] == 0L
1827 && rt->rtmsg_dst.s6_addr32[1] == 0L
1828 && rt->rtmsg_dst.s6_addr32[2] == 0L
1829 && rt->rtmsg_dst.s6_addr32[3] == 0L
1830 && (metric < 0 || rt->rtmsg_metric == metric)) {
1836 close_route_table();
1840 /********************************************************************
1842 * sif6defaultroute - assign a default route through the address given.
1844 * If the global default_rt_repl_rest flag is set, then this function
1845 * already replaced the original system defaultroute with some other
1846 * route and it should just replace the current defaultroute with
1847 * another one, without saving the current route. Use: demand mode,
1848 * when pppd sets first a defaultroute it it's temporary ppp0 addresses
1849 * and then changes the temporary addresses to the addresses for the real
1850 * ppp connection when it has come up.
1853 int sif6defaultroute (int unit, eui64_t ouraddr, eui64_t gateway)
1855 struct in6_rtmsg rt;
1856 char buf[IF_NAMESIZE];
1858 if (defaultroute6_exists(&rt, dfl_route_metric) &&
1859 rt.rtmsg_ifindex != if_nametoindex(ifname)) {
1860 if (rt.rtmsg_flags & RTF_GATEWAY)
1861 error("not replacing existing default route via gateway");
1863 error("not replacing existing default route through %s",
1864 if_indextoname(rt.rtmsg_ifindex, buf));
1868 memset (&rt, 0, sizeof (rt));
1870 rt.rtmsg_ifindex = if_nametoindex(ifname);
1871 rt.rtmsg_metric = dfl_route_metric + 1; /* +1 for binary compatibility */
1872 rt.rtmsg_dst_len = 0;
1874 rt.rtmsg_flags = RTF_UP;
1875 if (ioctl(sock6_fd, SIOCADDRT, &rt) < 0) {
1876 if ( ! ok_error ( errno ))
1877 error("default route ioctl(SIOCADDRT): %m");
1881 have_default_route6 = 1;
1885 /********************************************************************
1887 * cif6defaultroute - delete a default route through the address given.
1890 int cif6defaultroute (int unit, eui64_t ouraddr, eui64_t gateway)
1892 struct in6_rtmsg rt;
1894 have_default_route6 = 0;
1896 memset (&rt, '\0', sizeof (rt));
1898 rt.rtmsg_ifindex = if_nametoindex(ifname);
1899 rt.rtmsg_metric = dfl_route_metric + 1; /* +1 for binary compatibility */
1900 rt.rtmsg_dst_len = 0;
1902 rt.rtmsg_flags = RTF_UP;
1903 if (ioctl(sock6_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH) {
1905 if ( ! ok_error ( errno ))
1906 error("default route ioctl(SIOCDELRT): %m");
1915 /********************************************************************
1917 * sifproxyarp - Make a proxy ARP entry for the peer.
1920 int sifproxyarp (int unit, u_int32_t his_adr)
1922 struct arpreq arpreq;
1925 if (has_proxy_arp == 0) {
1926 memset (&arpreq, '\0', sizeof(arpreq));
1928 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1929 SIN_ADDR(arpreq.arp_pa) = his_adr;
1930 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1932 * Get the hardware address of an interface on the same subnet
1933 * as our local address.
1935 if (!get_ether_addr(his_adr, &arpreq.arp_ha, proxy_arp_dev,
1936 sizeof(proxy_arp_dev))) {
1937 error("Cannot determine ethernet address for proxy ARP");
1940 strlcpy(arpreq.arp_dev, proxy_arp_dev, sizeof(arpreq.arp_dev));
1942 if (ioctl(sock_fd, SIOCSARP, (caddr_t)&arpreq) < 0) {
1943 if ( ! ok_error ( errno ))
1944 error("ioctl(SIOCSARP): %m");
1947 proxy_arp_addr = his_adr;
1951 forw_path = path_to_procfs("/sys/net/ipv4/ip_forward");
1952 if (forw_path != 0) {
1953 int fd = open(forw_path, O_WRONLY);
1955 if (write(fd, "1", 1) != 1)
1956 error("Couldn't enable IP forwarding: %m");
1966 /********************************************************************
1968 * cifproxyarp - Delete the proxy ARP entry for the peer.
1971 int cifproxyarp (int unit, u_int32_t his_adr)
1973 struct arpreq arpreq;
1975 if (has_proxy_arp) {
1977 memset (&arpreq, '\0', sizeof(arpreq));
1978 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1979 SIN_ADDR(arpreq.arp_pa) = his_adr;
1980 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1981 strlcpy(arpreq.arp_dev, proxy_arp_dev, sizeof(arpreq.arp_dev));
1983 if (ioctl(sock_fd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1984 if ( ! ok_error ( errno ))
1985 warn("ioctl(SIOCDARP): %m");
1992 /********************************************************************
1994 * get_ether_addr - get the hardware address of an interface on the
1995 * the same subnet as ipaddr.
1998 static int get_ether_addr (u_int32_t ipaddr,
1999 struct sockaddr *hwaddr,
2000 char *name, int namelen)
2002 struct ifreq *ifr, *ifend;
2003 u_int32_t ina, mask;
2005 struct ifreq ifreq, bestifreq;
2007 struct ifreq ifs[MAX_IFS];
2009 u_int32_t bestmask=0;
2010 int found_interface = 0;
2012 ifc.ifc_len = sizeof(ifs);
2014 if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0) {
2015 if ( ! ok_error ( errno ))
2016 error("ioctl(SIOCGIFCONF): %m (line %d)", __LINE__);
2021 * Scan through looking for an interface with an Internet
2022 * address on the same subnet as `ipaddr'.
2024 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
2025 for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
2026 if (ifr->ifr_addr.sa_family == AF_INET) {
2027 ina = SIN_ADDR(ifr->ifr_addr);
2028 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
2030 * Check that the interface is up, and not point-to-point
2033 if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
2036 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
2039 * Get its netmask and check that it's on the right subnet.
2041 if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
2044 mask = SIN_ADDR(ifreq.ifr_addr);
2046 if (((ipaddr ^ ina) & mask) != 0)
2047 continue; /* no match */
2049 if (mask >= bestmask) {
2050 /* Compare using >= instead of > -- it is possible for
2051 an interface to have a netmask of 0.0.0.0 */
2052 found_interface = 1;
2059 if (!found_interface) return 0;
2061 strlcpy(name, bestifreq.ifr_name, namelen);
2063 /* trim off the :1 in eth0:1 */
2064 aliasp = strchr(name, ':');
2068 info("found interface %s for proxy arp", name);
2070 * Now get the hardware address.
2072 memset (&bestifreq.ifr_hwaddr, 0, sizeof (struct sockaddr));
2073 if (ioctl (sock_fd, SIOCGIFHWADDR, &bestifreq) < 0) {
2074 error("SIOCGIFHWADDR(%s): %m", bestifreq.ifr_name);
2079 &bestifreq.ifr_hwaddr,
2080 sizeof (struct sockaddr));
2086 * get_if_hwaddr - get the hardware address for the specified
2087 * network interface device.
2090 get_if_hwaddr(u_char *addr, char *name)
2095 sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
2098 memset(&ifreq.ifr_hwaddr, 0, sizeof(struct sockaddr));
2099 strlcpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name));
2100 ret = ioctl(sock_fd, SIOCGIFHWADDR, &ifreq);
2103 memcpy(addr, ifreq.ifr_hwaddr.sa_data, 6);
2108 * get_first_ethernet - return the name of the first ethernet-style
2109 * interface on this system.
2111 static char first_ether_name[IF_NAMESIZE];
2113 get_first_ethernet(void)
2115 struct if_nameindex *if_ni, *i;
2119 sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
2123 if_ni = if_nameindex();
2129 first_ether_name[0] = 0;
2131 for (i = if_ni; !(i->if_index == 0 && i->if_name == NULL); i++) {
2132 memset(&ifreq.ifr_hwaddr, 0, sizeof(struct sockaddr));
2133 strlcpy(ifreq.ifr_name, i->if_name, sizeof(ifreq.ifr_name));
2134 ret = ioctl(sock_fd, SIOCGIFHWADDR, &ifreq);
2135 if (ret >= 0 && ifreq.ifr_hwaddr.sa_family == ARPHRD_ETHER) {
2136 strlcpy(first_ether_name, i->if_name, sizeof(first_ether_name));
2141 if_freenameindex(if_ni);
2144 if (!first_ether_name[0])
2147 return first_ether_name;
2150 /********************************************************************
2152 * Return user specified netmask, modified by any mask we might determine
2153 * for address `addr' (in network byte order).
2154 * Here we scan through the system's list of interfaces, looking for
2155 * any non-point-to-point interfaces which might appear to be on the same
2156 * network as `addr'. If we find any, we OR in their netmask to the
2157 * user-specified netmask.
2160 u_int32_t GetMask (u_int32_t addr)
2162 u_int32_t mask, nmask, ina;
2163 struct ifreq *ifr, *ifend, ifreq;
2165 struct ifreq ifs[MAX_IFS];
2169 if (IN_CLASSA(addr)) /* determine network mask for address class */
2170 nmask = IN_CLASSA_NET;
2171 else if (IN_CLASSB(addr))
2172 nmask = IN_CLASSB_NET;
2174 nmask = IN_CLASSC_NET;
2176 /* class D nets are disallowed by bad_ip_adrs */
2177 mask = netmask | htonl(nmask);
2179 * Scan through the system's network interfaces.
2181 ifc.ifc_len = sizeof(ifs);
2183 if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0) {
2184 if ( ! ok_error ( errno ))
2185 warn("ioctl(SIOCGIFCONF): %m (line %d)", __LINE__);
2189 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
2190 for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
2192 * Check the interface's internet address.
2194 if (ifr->ifr_addr.sa_family != AF_INET)
2196 ina = SIN_ADDR(ifr->ifr_addr);
2197 if (((ntohl(ina) ^ addr) & nmask) != 0)
2200 * Check that the interface is up, and not point-to-point nor loopback.
2202 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
2203 if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
2206 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
2209 * Get its netmask and OR it into our mask.
2211 if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
2213 mask |= SIN_ADDR(ifreq.ifr_addr);
2219 /********************************************************************
2221 * Internal routine to decode the version.modification.patch level
2224 static void decode_version (char *buf, int *version,
2225 int *modification, int *patch)
2229 *version = (int) strtoul (buf, &endp, 10);
2233 if (endp != buf && *endp == '.') {
2235 *modification = (int) strtoul (buf, &endp, 10);
2236 if (endp != buf && *endp == '.') {
2238 *patch = (int) strtoul (buf, &buf, 10);
2243 /********************************************************************
2245 * Procedure to determine if the PPP line discipline is registered.
2249 ppp_registered(void)
2257 * We used to open the serial device and set it to the ppp line
2258 * discipline here, in order to create a ppp unit. But that is
2259 * not a good idea - the user might have specified a device that
2260 * they can't open (permission, or maybe it doesn't really exist).
2261 * So we grab a pty master/slave pair and use that.
2263 if (!get_pty(&mfd, &local_fd, slave, 0)) {
2264 no_ppp_msg = "Couldn't determine if PPP is supported (no free ptys)";
2269 * Try to put the device into the PPP discipline.
2271 if (ioctl(local_fd, TIOCSETD, &ppp_disc) < 0) {
2272 error("ioctl(TIOCSETD(PPP)): %m (line %d)", __LINE__);
2281 /********************************************************************
2283 * ppp_available - check whether the system has any ppp interfaces
2284 * (in fact we check whether we can do an ioctl on ppp0).
2287 int ppp_available(void)
2292 int my_version, my_modification, my_patch;
2293 int osmaj, osmin, ospatch;
2295 /* get the kernel version now, since we are called before sys_init */
2297 osmaj = osmin = ospatch = 0;
2298 sscanf(utsname.release, "%d.%d.%d", &osmaj, &osmin, &ospatch);
2299 kernel_version = KVERSION(osmaj, osmin, ospatch);
2301 fd = open("/dev/ppp", O_RDWR);
2303 new_style_driver = 1;
2305 /* XXX should get from driver */
2307 driver_modification = 4;
2313 if (kernel_version >= KVERSION(2,3,13)) {
2314 error("Couldn't open the /dev/ppp device: %m");
2315 if (errno == ENOENT)
2317 "You need to create the /dev/ppp device node by\n"
2318 "executing the following command as root:\n"
2319 " mknod /dev/ppp c 108 0\n";
2320 else if (errno == ENODEV || errno == ENXIO)
2322 "Please load the ppp_generic kernel module.\n";
2326 /* we are running on a really really old kernel */
2328 "This system lacks kernel support for PPP. This could be because\n"
2329 "the PPP kernel module could not be loaded, or because PPP was not\n"
2330 "included in the kernel configuration. If PPP was included as a\n"
2331 "module, try `/sbin/modprobe -v ppp'. If that fails, check that\n"
2332 "ppp.o exists in /lib/modules/`uname -r`/net.\n"
2333 "See README.linux file in the ppp distribution for more details.\n";
2336 * Open a socket for doing the ioctl operations.
2338 s = socket(AF_INET, SOCK_DGRAM, 0);
2342 strlcpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
2343 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
2345 * If the device did not exist then attempt to create one by putting the
2346 * current tty into the PPP discipline. If this works then obtain the
2347 * flags for the device again.
2350 if (ppp_registered()) {
2351 strlcpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
2352 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
2356 * Ensure that the hardware address is for PPP and not something else
2359 ok = ioctl (s, SIOCGIFHWADDR, (caddr_t) &ifr) >= 0;
2361 if (ok && ((ifr.ifr_hwaddr.sa_family & ~0xFF) != ARPHRD_PPP))
2365 * This is the PPP device. Validate the version of the driver at this
2366 * point to ensure that this program will work with the driver.
2369 char abBuffer [1024];
2371 ifr.ifr_data = abBuffer;
2372 size = ioctl (s, SIOCGPPPVER, (caddr_t) &ifr);
2374 error("Couldn't read driver version: %m");
2376 no_ppp_msg = "Sorry, couldn't verify kernel driver version\n";
2379 decode_version(abBuffer,
2381 &driver_modification,
2384 * Validate the version of the driver against the version that we used.
2386 decode_version(VERSION,
2391 /* The version numbers must match */
2392 if (driver_version != my_version)
2395 /* The modification levels must be legal */
2396 if (driver_modification < 3) {
2397 if (driver_modification >= 2) {
2398 /* we can cope with 2.2.0 and above */
2406 slprintf(route_buffer, sizeof(route_buffer),
2407 "Sorry - PPP driver version %d.%d.%d is out of date\n",
2408 driver_version, driver_modification, driver_patch);
2410 no_ppp_msg = route_buffer;
2418 #ifndef HAVE_LOGWTMP
2419 /********************************************************************
2421 * Update the wtmp file with the appropriate user name and tty device.
2424 void logwtmp (const char *line, const char *name, const char *host)
2426 struct utmp ut, *utp;
2427 pid_t mypid = getpid();
2433 * Update the signon database for users.
2434 * Christoph Lameter: Copied from poeigl-1.36 Jan 3, 1996
2436 utmpname(_PATH_UTMP);
2438 while ((utp = getutent()) && (utp->ut_pid != mypid))
2442 memcpy(&ut, utp, sizeof(ut));
2444 /* some gettys/telnetds don't initialize utmp... */
2445 memset(&ut, 0, sizeof(ut));
2447 if (ut.ut_id[0] == 0)
2448 strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
2450 strncpy(ut.ut_user, name, sizeof(ut.ut_user));
2451 strncpy(ut.ut_line, line, sizeof(ut.ut_line));
2455 ut.ut_type = USER_PROCESS;
2458 /* Insert the host name if one is supplied */
2460 strncpy (ut.ut_host, host, sizeof(ut.ut_host));
2462 /* Insert the IP address of the remote system if IP is enabled */
2463 if (ipcp_protent.enabled_flag && ipcp_hisoptions[0].neg_addr)
2464 memcpy(&ut.ut_addr, (char *) &ipcp_hisoptions[0].hisaddr,
2465 sizeof(ut.ut_addr));
2467 /* CL: Makes sure that the logout works */
2468 if (*host == 0 && *name==0)
2474 * Update the wtmp file.
2477 updwtmp(_PATH_WTMP, &ut);
2479 wtmp = open(_PATH_WTMP, O_APPEND|O_WRONLY);
2481 flock(wtmp, LOCK_EX);
2483 if (write (wtmp, (char *)&ut, sizeof(ut)) != sizeof(ut))
2484 warn("error writing %s: %m", _PATH_WTMP);
2486 flock(wtmp, LOCK_UN);
2492 #endif /* HAVE_LOGWTMP */
2494 /********************************************************************
2496 * sifvjcomp - config tcp header compression
2499 int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid)
2504 if (ioctl(ppp_dev_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
2505 error("Couldn't set up TCP header compression: %m");
2510 x = (vjcomp? SC_COMP_TCP: 0) | (cidcomp? 0: SC_NO_TCP_CCID);
2511 modify_flags(ppp_dev_fd, SC_COMP_TCP|SC_NO_TCP_CCID, x);
2516 /********************************************************************
2518 * sifup - Config the interface up and enable IP packets to pass.
2525 if ((ret = setifstate(u, 1)))
2531 /********************************************************************
2533 * sifdown - Disable the indicated protocol and config the interface
2534 * down if there are no remaining protocols.
2539 if (if_is_up && --if_is_up > 0)
2547 return setifstate(u, 0);
2551 /********************************************************************
2553 * sif6up - Config the interface up for IPv6
2560 if ((ret = setifstate(u, 1)))
2566 /********************************************************************
2568 * sif6down - Disable the IPv6CP protocol and config the interface
2569 * down if there are no remaining protocols.
2572 int sif6down (int u)
2579 return setifstate(u, 0);
2583 /********************************************************************
2585 * setifstate - Config the interface up or down
2588 static int setifstate (int u, int state)
2592 memset (&ifr, '\0', sizeof (ifr));
2593 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2594 if (ioctl(sock_fd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
2595 if (! ok_error (errno))
2596 error("ioctl (SIOCGIFFLAGS): %m (line %d)", __LINE__);
2601 ifr.ifr_flags |= IFF_UP;
2603 ifr.ifr_flags &= ~IFF_UP;
2604 ifr.ifr_flags |= IFF_POINTOPOINT;
2605 if (ioctl(sock_fd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
2606 if (! ok_error (errno))
2607 error("ioctl(SIOCSIFFLAGS): %m (line %d)", __LINE__);
2613 /********************************************************************
2615 * sifaddr - Config the interface IP addresses and netmask.
2618 int sifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr,
2624 memset (&ifr, '\0', sizeof (ifr));
2625 memset (&rt, '\0', sizeof (rt));
2627 SET_SA_FAMILY (ifr.ifr_addr, AF_INET);
2628 SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET);
2629 SET_SA_FAMILY (ifr.ifr_netmask, AF_INET);
2631 strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2633 * Set our IP address
2635 SIN_ADDR(ifr.ifr_addr) = our_adr;
2636 if (ioctl(sock_fd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
2637 if (errno != EEXIST) {
2638 if (! ok_error (errno))
2639 error("ioctl(SIOCSIFADDR): %m (line %d)", __LINE__);
2642 warn("ioctl(SIOCSIFADDR): Address already exists");
2647 * Set the gateway address
2650 SIN_ADDR(ifr.ifr_dstaddr) = his_adr;
2651 if (ioctl(sock_fd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
2652 if (! ok_error (errno))
2653 error("ioctl(SIOCSIFDSTADDR): %m (line %d)", __LINE__);
2659 * For recent kernels, force the netmask to 255.255.255.255.
2661 if (kernel_version >= KVERSION(2,1,16))
2663 if (net_mask != 0) {
2664 SIN_ADDR(ifr.ifr_netmask) = net_mask;
2665 if (ioctl(sock_fd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
2666 if (! ok_error (errno))
2667 error("ioctl(SIOCSIFNETMASK): %m (line %d)", __LINE__);
2672 * Add the device route
2674 if (kernel_version < KVERSION(2,1,16)) {
2675 SET_SA_FAMILY (rt.rt_dst, AF_INET);
2676 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
2679 SIN_ADDR(rt.rt_gateway) = 0L;
2680 SIN_ADDR(rt.rt_dst) = his_adr;
2681 rt.rt_flags = RTF_UP | RTF_HOST;
2683 if (kernel_version > KVERSION(2,1,0)) {
2684 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
2685 SIN_ADDR(rt.rt_genmask) = -1L;
2688 if (ioctl(sock_fd, SIOCADDRT, &rt) < 0) {
2689 if (! ok_error (errno))
2690 error("ioctl(SIOCADDRT) device route: %m (line %d)", __LINE__);
2695 /* set ip_dynaddr in demand mode if address changes */
2696 if (demand && tune_kernel && !dynaddr_set
2697 && our_old_addr && our_old_addr != our_adr) {
2698 /* set ip_dynaddr if possible */
2702 path = path_to_procfs("/sys/net/ipv4/ip_dynaddr");
2703 if (path != 0 && (fd = open(path, O_WRONLY)) >= 0) {
2704 if (write(fd, "1", 1) != 1)
2705 error("Couldn't enable dynamic IP addressing: %m");
2708 dynaddr_set = 1; /* only 1 attempt */
2715 /********************************************************************
2717 * cifaddr - Clear the interface IP addresses, and delete routes
2718 * through the interface if possible.
2721 int cifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr)
2725 if (kernel_version < KVERSION(2,1,16)) {
2727 * Delete the route through the device
2730 memset (&rt, '\0', sizeof (rt));
2732 SET_SA_FAMILY (rt.rt_dst, AF_INET);
2733 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
2736 SIN_ADDR(rt.rt_gateway) = 0;
2737 SIN_ADDR(rt.rt_dst) = his_adr;
2738 rt.rt_flags = RTF_UP | RTF_HOST;
2740 if (kernel_version > KVERSION(2,1,0)) {
2741 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
2742 SIN_ADDR(rt.rt_genmask) = -1L;
2745 if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH) {
2746 if (still_ppp() && ! ok_error (errno))
2747 error("ioctl(SIOCDELRT) device route: %m (line %d)", __LINE__);
2752 /* This way it is possible to have an IPX-only or IPv6-only interface */
2753 memset(&ifr, 0, sizeof(ifr));
2754 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
2755 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
2757 if (ioctl(sock_fd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
2758 if (! ok_error (errno)) {
2759 error("ioctl(SIOCSIFADDR): %m (line %d)", __LINE__);
2764 our_old_addr = our_adr;
2770 /********************************************************************
2772 * sif6addr - Config the interface with an IPv6 link-local address
2774 int sif6addr (int unit, eui64_t our_eui64, eui64_t his_eui64)
2776 struct in6_ifreq ifr6;
2778 struct in6_rtmsg rt6;
2782 error("IPv6 socket creation failed: %m");
2785 memset(&ifr, 0, sizeof (ifr));
2786 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
2787 if (ioctl(sock6_fd, SIOCGIFINDEX, (caddr_t) &ifr) < 0) {
2788 error("sif6addr: ioctl(SIOCGIFINDEX): %m (line %d)", __LINE__);
2792 /* Local interface */
2793 memset(&ifr6, 0, sizeof(ifr6));
2794 IN6_LLADDR_FROM_EUI64(ifr6.ifr6_addr, our_eui64);
2795 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
2796 ifr6.ifr6_prefixlen = 128;
2798 if (ioctl(sock6_fd, SIOCSIFADDR, &ifr6) < 0) {
2799 error("sif6addr: ioctl(SIOCSIFADDR): %m (line %d)", __LINE__);
2803 /* Route to remote host */
2804 memset(&rt6, 0, sizeof(rt6));
2805 IN6_LLADDR_FROM_EUI64(rt6.rtmsg_dst, his_eui64);
2806 rt6.rtmsg_flags = RTF_UP;
2807 rt6.rtmsg_dst_len = 128;
2808 rt6.rtmsg_ifindex = ifr.ifr_ifindex;
2809 rt6.rtmsg_metric = 1;
2811 if (ioctl(sock6_fd, SIOCADDRT, &rt6) < 0) {
2812 error("sif6addr: ioctl(SIOCADDRT): %m (line %d)", __LINE__);
2820 /********************************************************************
2822 * cif6addr - Remove IPv6 address from interface
2824 int cif6addr (int unit, eui64_t our_eui64, eui64_t his_eui64)
2827 struct in6_ifreq ifr6;
2831 error("IPv6 socket creation failed: %m");
2834 memset(&ifr, 0, sizeof(ifr));
2835 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
2836 if (ioctl(sock6_fd, SIOCGIFINDEX, (caddr_t) &ifr) < 0) {
2837 error("cif6addr: ioctl(SIOCGIFINDEX): %m (line %d)", __LINE__);
2841 memset(&ifr6, 0, sizeof(ifr6));
2842 IN6_LLADDR_FROM_EUI64(ifr6.ifr6_addr, our_eui64);
2843 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
2844 ifr6.ifr6_prefixlen = 128;
2846 if (ioctl(sock6_fd, SIOCDIFADDR, &ifr6) < 0) {
2847 if (errno != EADDRNOTAVAIL) {
2848 if (! ok_error (errno))
2849 error("cif6addr: ioctl(SIOCDIFADDR): %m (line %d)", __LINE__);
2852 warn("cif6addr: ioctl(SIOCDIFADDR): No such address");
2861 * get_pty - get a pty master/slave pair and chown the slave side
2862 * to the uid given. Assumes slave_name points to >= 16 bytes of space.
2865 get_pty(int *master_fdp, int *slave_fdp, char *slave_name, int uid)
2867 int i, mfd, sfd = -1;
2869 struct termios tios;
2873 * Try the unix98 way first.
2875 mfd = open("/dev/ptmx", O_RDWR);
2878 if (ioctl(mfd, TIOCGPTN, &ptn) >= 0) {
2879 slprintf(pty_name, sizeof(pty_name), "/dev/pts/%d", ptn);
2880 chmod(pty_name, S_IRUSR | S_IWUSR);
2883 if (ioctl(mfd, TIOCSPTLCK, &ptn) < 0)
2884 warn("Couldn't unlock pty slave %s: %m", pty_name);
2886 if ((sfd = open(pty_name, O_RDWR | O_NOCTTY)) < 0)
2888 warn("Couldn't open pty slave %s: %m", pty_name);
2893 #endif /* TIOCGPTN */
2896 /* the old way - scan through the pty name space */
2897 for (i = 0; i < 64; ++i) {
2898 slprintf(pty_name, sizeof(pty_name), "/dev/pty%c%x",
2899 'p' + i / 16, i % 16);
2900 mfd = open(pty_name, O_RDWR, 0);
2903 sfd = open(pty_name, O_RDWR | O_NOCTTY, 0);
2905 fchown(sfd, uid, -1);
2906 fchmod(sfd, S_IRUSR | S_IWUSR);
2917 strlcpy(slave_name, pty_name, 16);
2920 if (tcgetattr(sfd, &tios) == 0) {
2921 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
2922 tios.c_cflag |= CS8 | CREAD | CLOCAL;
2923 tios.c_iflag = IGNPAR;
2926 if (tcsetattr(sfd, TCSAFLUSH, &tios) < 0)
2927 warn("couldn't set attributes on pty: %m");
2929 warn("couldn't get attributes on pty: %m");
2934 /********************************************************************
2936 * open_loopback - open the device we use for getting packets
2937 * in demand mode. Under Linux, we use a pty master/slave pair.
2940 open_ppp_loopback(void)
2945 if (new_style_driver) {
2946 /* allocate ourselves a ppp unit */
2947 if (make_ppp_unit() < 0)
2949 modify_flags(ppp_dev_fd, 0, SC_LOOP_TRAFFIC);
2950 set_kdebugflag(kdebugflag);
2955 if (!get_pty(&master_fd, &slave_fd, loop_name, 0))
2956 fatal("No free pty for loopback");
2958 set_ppp_fd(slave_fd);
2960 flags = fcntl(master_fd, F_GETFL);
2962 fcntl(master_fd, F_SETFL, flags | O_NONBLOCK) == -1)
2963 warn("couldn't set master loopback to nonblock: %m");
2965 flags = fcntl(ppp_fd, F_GETFL);
2967 fcntl(ppp_fd, F_SETFL, flags | O_NONBLOCK) == -1)
2968 warn("couldn't set slave loopback to nonblock: %m");
2970 if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0)
2971 fatal("ioctl(TIOCSETD): %m (line %d)", __LINE__);
2973 * Find out which interface we were given.
2975 if (ioctl(ppp_fd, PPPIOCGUNIT, &ifunit) < 0)
2976 fatal("ioctl(PPPIOCGUNIT): %m (line %d)", __LINE__);
2978 * Enable debug in the driver if requested.
2980 set_kdebugflag (kdebugflag);
2985 /********************************************************************
2987 * sifnpmode - Set the mode for handling packets for a given NP.
2991 sifnpmode(int u, int proto, enum NPmode mode)
2995 npi.protocol = proto;
2997 if (ioctl(ppp_dev_fd, PPPIOCSNPMODE, (caddr_t) &npi) < 0) {
2998 if (! ok_error (errno))
2999 error("ioctl(PPPIOCSNPMODE, %d, %d): %m", proto, mode);
3006 /********************************************************************
3008 * sipxfaddr - Config the interface IPX networknumber
3011 int sipxfaddr (int unit, unsigned long int network, unsigned char * node )
3018 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
3020 skfd = socket (AF_IPX, SOCK_DGRAM, 0);
3022 if (! ok_error (errno))
3023 dbglog("socket(AF_IPX): %m (line %d)", __LINE__);
3027 memset (&ifr, '\0', sizeof (ifr));
3028 strlcpy (ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
3030 memcpy (sipx->sipx_node, node, IPX_NODE_LEN);
3031 sipx->sipx_family = AF_IPX;
3032 sipx->sipx_port = 0;
3033 sipx->sipx_network = htonl (network);
3034 sipx->sipx_type = IPX_FRAME_ETHERII;
3035 sipx->sipx_action = IPX_CRTITF;
3037 * Set the IPX device
3039 if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
3041 if (errno != EEXIST) {
3042 if (! ok_error (errno))
3043 dbglog("ioctl(SIOCSIFADDR, CRTITF): %m (line %d)", __LINE__);
3046 warn("ioctl(SIOCSIFADDR, CRTITF): Address already exists");
3055 /********************************************************************
3057 * cipxfaddr - Clear the information for the IPX network. The IPX routes
3058 * are removed and the device is no longer able to pass IPX
3062 int cipxfaddr (int unit)
3069 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
3071 skfd = socket (AF_IPX, SOCK_DGRAM, 0);
3073 if (! ok_error (errno))
3074 dbglog("socket(AF_IPX): %m (line %d)", __LINE__);
3078 memset (&ifr, '\0', sizeof (ifr));
3079 strlcpy (ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
3081 sipx->sipx_type = IPX_FRAME_ETHERII;
3082 sipx->sipx_action = IPX_DLTITF;
3083 sipx->sipx_family = AF_IPX;
3085 * Set the IPX device
3087 if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
3088 if (! ok_error (errno))
3089 info("ioctl(SIOCSIFADDR, IPX_DLTITF): %m (line %d)", __LINE__);
3099 * Use the hostname as part of the random number seed.
3108 for (p = hostname; *p != 0; ++p)
3113 /********************************************************************
3115 * sys_check_options - check the options that the user specified
3119 sys_check_options(void)
3123 * Disable the IPX protocol if the support is not present in the kernel.
3127 if (ipxcp_protent.enabled_flag) {
3128 struct stat stat_buf;
3129 if ( ((path = path_to_procfs("/net/ipx/interface")) == NULL
3130 && (path = path_to_procfs("/net/ipx_interface")) == NULL)
3131 || lstat(path, &stat_buf) < 0) {
3132 error("IPX support is not present in the kernel\n");
3133 ipxcp_protent.enabled_flag = 0;
3137 if (demand && driver_is_old) {
3138 option_error("demand dialling is not supported by kernel driver "
3139 "version %d.%d.%d", driver_version, driver_modification,
3143 if (multilink && !new_style_driver) {
3144 warn("Warning: multilink is not supported by the kernel driver");
3152 * ether_to_eui64 - Convert 48-bit Ethernet address into 64-bit EUI
3154 * convert the 48-bit MAC address of eth0 into EUI 64. caller also assumes
3155 * that the system has a properly configured Ethernet interface for this
3156 * function to return non-zero.
3159 ether_to_eui64(eui64_t *p_eui64)
3163 const unsigned char *ptr;
3165 skfd = socket(PF_INET6, SOCK_DGRAM, 0);
3168 warn("could not open IPv6 socket");
3172 strcpy(ifr.ifr_name, "eth0");
3173 if(ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0)
3176 warn("could not obtain hardware address for eth0");
3182 * And convert the EUI-48 into EUI-64, per RFC 2472 [sec 4.1]
3184 ptr = (unsigned char *) ifr.ifr_hwaddr.sa_data;
3185 p_eui64->e8[0] = ptr[0] | 0x02;
3186 p_eui64->e8[1] = ptr[1];
3187 p_eui64->e8[2] = ptr[2];
3188 p_eui64->e8[3] = 0xFF;
3189 p_eui64->e8[4] = 0xFE;
3190 p_eui64->e8[5] = ptr[3];
3191 p_eui64->e8[6] = ptr[4];
3192 p_eui64->e8[7] = ptr[5];
3198 /********************************************************************
3200 * get_time - Get current time, monotonic if possible.
3203 get_time(struct timeval *tv)
3205 /* Old glibc (< 2.3.4) does define CLOCK_MONOTONIC, but kernel may have it.
3206 * Runtime checking makes it safe. */
3207 #ifndef CLOCK_MONOTONIC
3208 #define CLOCK_MONOTONIC 1
3210 static int monotonic = -1;
3215 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
3219 tv->tv_sec = ts.tv_sec;
3220 tv->tv_usec = ts.tv_nsec / 1000;
3223 } else if (monotonic > 0)
3227 warn("Couldn't use monotonic clock source: %m");
3230 return gettimeofday(tv, NULL);