2 * sys-linux.c - System-dependent procedures for setting up
3 * PPP interfaces on Linux systems
5 * Copyright (c) 1989 Carnegie Mellon University.
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by Carnegie Mellon University. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
31 #include <sys/ioctl.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
35 #include <sys/errno.h>
39 #include <net/if_arp.h>
40 #include <linux/ppp.h>
41 #include <linux/route.h>
42 #include <linux/if_ether.h>
43 #include <netinet/in.h>
51 static int initdisc = -1; /* Initial TTY discipline */
52 static int prev_kdebugflag = 0;
54 static int restore_term; /* 1 => we've munged the terminal */
55 static struct termios inittermios; /* Initial TTY termios */
57 static int driver_version = 0;
58 static int driver_modification = 0;
59 static int driver_patch = 0;
64 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
68 #define SET_SA_FAMILY(addr, family) \
69 memset ((char *) &(addr), '\0', sizeof(addr)); \
70 addr.sa_family = (family);
73 * sys_init - System-dependent initialization.
78 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
79 setlogmask(LOG_UPTO(LOG_INFO));
81 setlogmask(LOG_UPTO(LOG_DEBUG));
85 * note_debug_level - note a change in the debug level.
91 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
92 setlogmask(LOG_UPTO(LOG_DEBUG));
94 setlogmask(LOG_UPTO(LOG_WARNING));
99 * set_kdebugflag - Define the debugging level for the kernel
102 int set_kdebugflag (int requested_level)
104 if (ioctl(fd, PPPIOCGDEBUG, &prev_kdebugflag) < 0) {
105 syslog(LOG_ERR, "ioctl(PPPIOCGDEBUG): %m");
109 if (prev_kdebugflag != requested_level) {
110 if (ioctl(fd, PPPIOCSDEBUG, &requested_level) < 0) {
111 syslog (LOG_ERR, "ioctl(PPPIOCSDEBUG): %m");
114 syslog(LOG_INFO, "set kernel debugging level to %d", requested_level);
120 * establish_ppp - Turn the serial port into a ppp interface.
123 void establish_ppp (void)
128 if (ioctl(fd, TIOCEXCL, 0) < 0) {
129 syslog (LOG_WARNING, "ioctl(TIOCEXCL): %m");
132 if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
133 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
137 if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
138 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
141 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
142 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
146 set_kdebugflag (kdebugflag);
148 syslog (LOG_NOTICE, "Using version %d.%d.%d of PPP driver",
149 driver_version, driver_modification, driver_patch);
153 * disestablish_ppp - Restore the serial port to normal operation.
154 * This shouldn't call die() because it's called from die().
157 void disestablish_ppp(void)
163 set_kdebugflag (prev_kdebugflag);
165 * Check whether the link seems not to be 8-bit clean.
167 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
169 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
171 s = "bit 7 set to 1";
174 s = "bit 7 set to 0";
184 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
185 syslog(LOG_WARNING, "All received characters had %s", s);
189 if (ioctl(fd, TIOCSETD, &initdisc) < 0)
190 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
192 if (ioctl(fd, TIOCNXCL, 0) < 0)
193 syslog (LOG_WARNING, "ioctl(TIOCNXCL): %m");
201 * XXX assume speed_t values numerically equal bits per second
202 * (so we can ask for any speed).
204 #define translate_speed(bps) (bps)
205 #define baud_rate_of(speed) (speed)
209 * List of valid speeds.
212 int speed_int, speed_val;
284 * Translate from bits/second to a speed_t.
290 struct speed *speedp;
294 for (speedp = speeds; speedp->speed_int; speedp++)
295 if (bps == speedp->speed_int)
296 return speedp->speed_val;
297 syslog(LOG_WARNING, "speed %d not supported", bps);
302 * Translate from a speed_t to bits/second.
308 struct speed *speedp;
312 for (speedp = speeds; speedp->speed_int; speedp++)
313 if (speed == speedp->speed_val)
314 return speedp->speed_int;
320 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
321 * at the requested speed, etc. If `local' is true, set CLOCAL
322 * regardless of whether the modem option was specified.
324 set_up_tty(fd, local)
330 if (tcgetattr(fd, &tios) < 0) {
331 syslog(LOG_ERR, "tcgetattr: %m");
339 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL | CRTSCTS);
341 tios.c_cflag |= CRTSCTS;
343 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
346 tios.c_cflag |= CS8 | CREAD | HUPCL;
348 tios.c_cflag |= CLOCAL;
349 tios.c_iflag = IGNBRK | IGNPAR;
353 tios.c_cc[VTIME] = 0;
356 tios.c_iflag |= IXOFF;
357 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
358 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
361 speed = translate_speed(inspeed);
363 cfsetospeed(&tios, speed);
364 cfsetispeed(&tios, speed);
366 speed = cfgetospeed(&tios);
368 * We can't proceed if the serial port speed is B0,
369 * since that implies that the serial port is disabled.
372 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
378 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
379 syslog(LOG_ERR, "tcsetattr: %m");
383 baud_rate = baud_rate_of(speed);
388 * setdtr - control the DTR line on the serial port.
389 * This is called from die(), so it shouldn't call die().
394 int modembits = TIOCM_DTR;
396 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
400 * restore_tty - restore the terminal to the saved settings.
406 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
408 syslog(LOG_WARNING, "tcsetattr: %m");
414 * output - Output PPP packet.
417 void output (int unit, unsigned char *p, int len)
420 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
423 log_packet(p, len, "sent ");
425 if (write(fd, p, len) < 0) {
426 syslog(LOG_ERR, "write: %m");
432 * wait_input - wait until there is data available on fd,
433 * for the length of time specified by *timo (indefinite
437 struct timeval *timo;
444 n = select(fd+1, &ready, NULL, &ready, timo);
445 if (n < 0 && errno != EINTR) {
446 syslog(LOG_ERR, "select: %m");
453 * read_packet - get a PPP packet from the serial device.
456 int read_packet (unsigned char *buf)
460 len = read(fd, buf, MTU + DLLHEADERLEN);
462 if (errno == EWOULDBLOCK) {
464 MAINDEBUG((LOG_DEBUG, "read(fd): EWOULDBLOCK"));
468 syslog(LOG_ERR, "read(fd): %m");
475 * ppp_send_config - configure the transmit characteristics of
478 void ppp_send_config (int unit,int mtu,uint32 asyncmap,int pcomp,int accomp)
483 MAINDEBUG ((LOG_DEBUG, "send_config: mtu = %d\n", mtu));
484 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
486 if (ioctl(s, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
487 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
491 MAINDEBUG ((LOG_DEBUG, "send_config: asyncmap = %lx\n", asyncmap));
492 if (ioctl(fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
493 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
497 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
498 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
502 x = pcomp ? x | SC_COMP_PROT : x & ~SC_COMP_PROT;
503 x = accomp ? x | SC_COMP_AC : x & ~SC_COMP_AC;
505 MAINDEBUG ((LOG_DEBUG, "send_config: flags = %x\n", x));
506 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
507 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
513 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
516 ppp_set_xaccm(unit, accm)
520 MAINDEBUG ((LOG_DEBUG, "set_xaccm: %08lx %08lx %08lx %08lx\n",
521 accm[0], accm[1], accm[2], accm[3]));
522 if (ioctl(fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
523 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
527 * ppp_recv_config - configure the receive-side characteristics of
530 void ppp_recv_config (int unit,int mru,uint32 asyncmap,int pcomp,int accomp)
534 MAINDEBUG ((LOG_DEBUG, "recv_config: mru = %d\n", mru));
535 if (ioctl(fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
536 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
538 MAINDEBUG ((LOG_DEBUG, "recv_config: asyncmap = %lx\n", asyncmap));
539 if (ioctl(fd, PPPIOCRASYNCMAP, (caddr_t) &asyncmap) < 0) {
540 syslog(LOG_ERR, "ioctl(PPPIOCRASYNCMAP): %m");
544 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
545 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
549 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
550 MAINDEBUG ((LOG_DEBUG, "recv_config: flags = %x\n", x));
551 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
552 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
558 * ccp_test - ask kernel whether a given compression method
559 * is acceptable for use.
562 ccp_test(unit, opt_ptr, opt_len, for_transmit)
563 int unit, opt_len, for_transmit;
566 struct ppp_comp_data data;
569 data.length = opt_len;
570 data.transmit = for_transmit;
571 return ioctl(fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0;
575 * ccp_flags_set - inform kernel about the current state of CCP.
578 ccp_flags_set(unit, isopen, isup)
579 int unit, isopen, isup;
583 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
584 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
587 x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
588 x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
589 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
590 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
594 * sifvjcomp - config tcp header compression
597 int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid)
601 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
602 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
606 x = vjcomp ? x | SC_COMP_TCP : x &~ SC_COMP_TCP;
607 x = cidcomp ? x & ~SC_NO_TCP_CCID : x | SC_NO_TCP_CCID;
609 if(ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
610 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
615 if (ioctl (fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
616 syslog (LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
625 * sifup - Config the interface up and enable IP packets to pass.
632 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
633 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
634 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
638 ifr.ifr_flags |= (IFF_UP | IFF_POINTOPOINT);
639 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
640 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
647 * sifdown - Config the interface down and disable IP.
654 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
655 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
656 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
660 ifr.ifr_flags &= ~IFF_UP;
661 ifr.ifr_flags |= IFF_POINTOPOINT;
662 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
663 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
670 * sifaddr - Config the interface IP addresses and netmask.
673 int sifaddr (int unit, int our_adr, int his_adr, int net_mask)
678 SET_SA_FAMILY (ifr.ifr_addr, AF_INET);
679 SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET);
680 SET_SA_FAMILY (ifr.ifr_netmask, AF_INET);
682 strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
686 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = our_adr;
687 if (ioctl(s, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
689 syslog (LOG_ERR, "ioctl(SIOCAIFADDR): %m");
691 syslog (LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
695 * Set the gateway address
697 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = his_adr;
698 if (ioctl(s, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
699 syslog (LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
706 ((struct sockaddr_in *) &ifr.ifr_netmask)->sin_addr.s_addr = net_mask;
707 if (ioctl(s, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
708 syslog (LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
713 * Add the device route
715 memset (&rt, '\0', sizeof (rt));
717 SET_SA_FAMILY (rt.rt_dst, AF_INET);
718 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
719 rt.rt_dev = ifname; /* MJC */
721 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
722 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
723 rt.rt_flags = RTF_UP | RTF_HOST;
725 if (ioctl(s, SIOCADDRT, &rt) < 0) {
726 syslog (LOG_ERR, "ioctl(SIOCADDRT) device route: %m");
733 * cifaddr - Clear the interface IP addresses, and delete routes
734 * through the interface if possible.
737 int cifaddr (int unit, int our_adr, int his_adr)
741 * Delete the route through the device
743 memset (&rt, '\0', sizeof (rt));
745 SET_SA_FAMILY (rt.rt_dst, AF_INET);
746 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
747 rt.rt_dev = ifname; /* MJC */
749 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
750 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
751 rt.rt_flags = RTF_UP | RTF_HOST;
753 if (ioctl(s, SIOCDELRT, &rt) < 0) {
754 syslog (LOG_ERR, "ioctl(SIOCDELRT) device route: %m");
761 * path_to_route - determine the path to the proc file system data
764 FILE *route_fd = (FILE *) 0;
765 static char route_buffer [100];
767 static char *path_to_route (void);
768 static int open_route_table (void);
769 static void close_route_table (void);
770 static int read_route_table (struct rtentry *rt);
771 static int defaultroute_exists (void);
774 * path_to_route - find the path to the route tables in the proc file system
777 static char *path_to_route (void)
779 struct mntent *mntent;
782 fp = fopen (MOUNTED, "r");
784 while ((mntent = getmntent (fp)) != 0) {
785 if (strcmp (mntent->mnt_type, MNTTYPE_IGNORE) == 0)
788 if (strcmp (mntent->mnt_type, "proc") == 0) {
789 strncpy (route_buffer, mntent->mnt_dir,
790 sizeof (route_buffer)-10);
791 route_buffer [sizeof (route_buffer)-10] = '\0';
792 strcat (route_buffer, "/net/route");
795 return (route_buffer);
800 syslog (LOG_ERR, "proc file system not mounted");
805 * open_route_table - open the interface to the route table
808 static int open_route_table (void)
812 if (route_fd != (FILE *) 0)
815 path = path_to_route();
819 route_fd = fopen (path, "r");
820 if (route_fd == (FILE *) 0) {
821 syslog (LOG_ERR, "can not open %s: %m", path);
825 /* read and discard the header line. */
826 if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0) {
834 * close_route_table - close the interface to the route table
837 static void close_route_table (void)
839 if (route_fd != (FILE *) 0) {
841 route_fd = (FILE *) 0;
846 * read_route_table - read the next entry from the route table
849 static int read_route_table (struct rtentry *rt)
851 static char delims[] = " \t\n";
852 char *dev_ptr, *ptr, *dst_ptr, *gw_ptr, *flag_ptr;
854 if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0)
857 memset (rt, '\0', sizeof (struct rtentry));
859 dev_ptr = strtok (route_buffer, delims); /* interface name */
860 dst_ptr = strtok (NULL, delims); /* destination address */
861 gw_ptr = strtok (NULL, delims); /* gateway */
862 flag_ptr = strtok (NULL, delims); /* flags */
864 ptr = strtok (NULL, delims); /* reference count */
865 ptr = strtok (NULL, delims); /* useage count */
866 ptr = strtok (NULL, delims); /* metric */
867 ptr = strtok (NULL, delims); /* mask */
870 ((struct sockaddr_in *) &rt->rt_dst)->sin_addr.s_addr =
871 strtoul (dst_ptr, NULL, 16);
873 ((struct sockaddr_in *) &rt->rt_gateway)->sin_addr.s_addr =
874 strtoul (gw_ptr, NULL, 16);
876 rt->rt_flags = (short) strtoul (flag_ptr, NULL, 16);
877 rt->rt_dev = dev_ptr;
883 * defaultroute_exists - determine if there is a default route
886 static int defaultroute_exists (void)
891 if (!open_route_table())
894 while (read_route_table(&rt) != 0) {
895 if (rt.rt_flags & RTF_UP == 0)
898 if (((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr == 0L) {
900 "ppp not replacing existing default route to %s[%s]",
902 inet_ntoa (((struct sockaddr_in *) &rt.rt_gateway)->
913 * sifdefaultroute - assign a default route through the address given.
916 int sifdefaultroute (int unit, int gateway)
920 if (defaultroute_exists())
923 memset (&rt, '\0', sizeof (rt));
924 SET_SA_FAMILY (rt.rt_dst, AF_INET);
925 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
926 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
928 rt.rt_flags = RTF_UP | RTF_GATEWAY;
929 if (ioctl(s, SIOCADDRT, &rt) < 0) {
930 syslog (LOG_ERR, "default route ioctl(SIOCADDRT): %m");
937 * cifdefaultroute - delete a default route through the address given.
940 int cifdefaultroute (int unit, int gateway)
944 SET_SA_FAMILY (rt.rt_dst, AF_INET);
945 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
946 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
948 rt.rt_flags = RTF_UP | RTF_GATEWAY;
949 if (ioctl(s, SIOCDELRT, &rt) < 0) {
950 syslog (LOG_ERR, "default route ioctl(SIOCDELRT): %m");
957 * sifproxyarp - Make a proxy ARP entry for the peer.
960 int sifproxyarp (int unit, uint32 his_adr)
962 struct arpreq arpreq;
964 memset (&arpreq, '\0', sizeof(arpreq));
966 * Get the hardware address of an interface on the same subnet
967 * as our local address.
969 if (!get_ether_addr(his_adr, &arpreq.arp_ha)) {
970 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
974 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
975 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
976 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
978 if (ioctl(s, SIOCSARP, (caddr_t)&arpreq) < 0) {
979 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
986 * cifproxyarp - Delete the proxy ARP entry for the peer.
989 int cifproxyarp (int unit, uint32 his_adr)
991 struct arpreq arpreq;
993 memset (&arpreq, '\0', sizeof(arpreq));
994 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
996 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
997 if (ioctl(s, SIOCDARP, (caddr_t)&arpreq) < 0) {
998 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
1005 * get_ether_addr - get the hardware address of an interface on the
1006 * the same subnet as ipaddr.
1009 int get_ether_addr (uint32 ipaddr, struct sockaddr *hwaddr)
1011 struct ifreq *ifr, *ifend, *ifp;
1014 struct sockaddr_dl *dla;
1017 struct ifreq ifs[MAX_IFS];
1019 ifc.ifc_len = sizeof(ifs);
1021 if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
1022 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
1025 MAINDEBUG ((LOG_DEBUG, "proxy arp: scanning %d interfaces for IP %s",
1026 ifc.ifc_len / sizeof(struct ifreq), ip_ntoa(ipaddr)));
1028 * Scan through looking for an interface with an Internet
1029 * address on the same subnet as `ipaddr'.
1031 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
1032 for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
1033 if (ifr->ifr_addr.sa_family == AF_INET) {
1034 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1035 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1036 MAINDEBUG ((LOG_DEBUG, "proxy arp: examining interface %s",
1039 * Check that the interface is up, and not point-to-point
1042 if (ioctl(s, SIOCGIFFLAGS, &ifreq) < 0)
1044 if ((ifreq.ifr_flags &
1045 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1046 != (IFF_UP|IFF_BROADCAST))
1049 * Get its netmask and check that it's on the right subnet.
1051 if (ioctl(s, SIOCGIFNETMASK, &ifreq) < 0)
1053 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1054 MAINDEBUG ((LOG_DEBUG, "proxy arp: interface addr %s mask %lx",
1055 ip_ntoa(ina), ntohl(mask)));
1056 if (((ipaddr ^ ina) & mask) != 0)
1065 syslog(LOG_INFO, "found interface %s for proxy arp", ifreq.ifr_name);
1067 * Now get the hardware address.
1069 if (ioctl (s, SIOCGIFHWADDR, &ifreq) < 0) {
1070 syslog(LOG_ERR, "SIOCGIFHWADDR(%s): %m", ifreq.ifr_name);
1074 hwaddr->sa_family = ARPHRD_ETHER;
1075 #ifndef old_ifr_hwaddr
1076 memcpy (&hwaddr->sa_data, &ifreq.ifr_hwaddr, ETH_ALEN);
1078 memcpy (&hwaddr->sa_data, &ifreq.ifr_hwaddr.sa_data, ETH_ALEN);
1081 MAINDEBUG ((LOG_DEBUG,
1082 "proxy arp: found hwaddr %02x:%02x:%02x:%02x:%02x:%02x",
1083 (int) ((unsigned char *) &hwaddr->sa_data)[0],
1084 (int) ((unsigned char *) &hwaddr->sa_data)[1],
1085 (int) ((unsigned char *) &hwaddr->sa_data)[2],
1086 (int) ((unsigned char *) &hwaddr->sa_data)[3],
1087 (int) ((unsigned char *) &hwaddr->sa_data)[4],
1088 (int) ((unsigned char *) &hwaddr->sa_data)[5]));
1093 * Internal routine to decode the version.modification.patch level
1096 static void decode_version (char *buf, int *version,
1097 int *modification, int *patch)
1100 *version = (int) strtoul (buf, &buf, 10);
1106 *modification = (int) strtoul (buf, &buf, 10);
1109 *patch = (int) strtoul (buf, &buf, 10);
1121 * ppp_available - check whether the system has any ppp interfaces
1122 * (in fact we check whether we can do an ioctl on ppp0).
1125 int ppp_available(void)
1129 int my_version, my_modification, my_patch;
1131 * Open a socket for doing the ioctl operations.
1133 s = socket(AF_INET, SOCK_DGRAM, 0);
1137 strncpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
1138 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1140 * Ensure that the hardware address is for PPP and not something else
1143 ok = ioctl (s, SIOCGIFHWADDR, (caddr_t) &ifr) >= 0;
1145 if (ok != 0 && ifr.ifr_hwaddr.sa_family != ARPHRD_PPP)
1148 * This is the PPP device. Validate the version of the driver at this
1149 * point to ensure that this program will work with the driver.
1152 char abBuffer [1024];
1155 ifr.ifr_data = abBuffer;
1156 size = ioctl (s, SIOCDEVPRIVATE, (caddr_t) &ifr);
1160 decode_version (abBuffer,
1162 &driver_modification,
1169 driver_modification =
1173 * Validate the version of the driver against the version that we used.
1175 decode_version (PPP_VERSION,
1180 /* The version numbers must match */
1181 if (driver_version != my_version)
1184 /* The modification levels must be legal */
1185 if (driver_modification < my_modification)
1189 fprintf(stderr, "Sorry - PPP driver version %d.%d.%d is out of date\n",
1190 driver_version, driver_modification, driver_patch);
1198 logwtmp(line, name, host)
1199 char *line, *name, *host;
1203 memset (&ut, 0, sizeof (ut));
1204 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1205 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1206 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1207 (void)time(&ut.ut_time);
1209 pututline (&ut); /* Write the line to the proper place */
1210 endutent(); /* Indicate operation is complete */