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/ppp_defs.h>
40 #include <net/if_arp.h>
41 #include <linux/ppp.h>
42 #include <linux/route.h>
43 #include <linux/if_ether.h>
44 #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 int sockfd; /* socket for doing interface ioctls */
59 static int driver_version = 0;
60 static int driver_modification = 0;
61 static int driver_patch = 0;
63 static char *lock_file;
68 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
72 #define SET_SA_FAMILY(addr, family) \
73 memset ((char *) &(addr), '\0', sizeof(addr)); \
74 addr.sa_family = (family);
77 * sys_init - System-dependent initialization.
82 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
83 setlogmask(LOG_UPTO(LOG_INFO));
85 setlogmask(LOG_UPTO(LOG_DEBUG));
87 /* Get an internet socket for doing socket ioctl's on. */
88 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
89 syslog(LOG_ERR, "Couldn't create IP socket: %m");
95 * ccp_fatal_error - returns 1 if decompression was disabled as a
96 * result of an error detected after decompression of a packet,
97 * 0 otherwise. This is necessary because of patent nonsense.
99 int ccp_fatal_error (int unit)
103 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
104 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
107 return x & SC_DC_FERROR;
111 * note_debug_level - note a change in the debug level.
114 void note_debug_level(void)
117 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
118 setlogmask(LOG_UPTO(LOG_DEBUG));
120 setlogmask(LOG_UPTO(LOG_WARNING));
125 * set_kdebugflag - Define the debugging level for the kernel
128 int set_kdebugflag (int requested_level)
130 if (ioctl(fd, PPPIOCGDEBUG, &prev_kdebugflag) < 0) {
131 syslog(LOG_ERR, "ioctl(PPPIOCGDEBUG): %m");
135 if (prev_kdebugflag != requested_level) {
136 if (ioctl(fd, PPPIOCSDEBUG, &requested_level) < 0) {
137 syslog (LOG_ERR, "ioctl(PPPIOCSDEBUG): %m");
140 syslog(LOG_INFO, "set kernel debugging level to %d", requested_level);
146 * establish_ppp - Turn the serial port into a ppp interface.
149 void establish_ppp (void)
154 if (ioctl(fd, TIOCEXCL, 0) < 0) {
155 syslog (LOG_WARNING, "ioctl(TIOCEXCL): %m");
158 if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
159 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
163 if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
164 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
167 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
168 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
172 set_kdebugflag (kdebugflag);
174 syslog (LOG_NOTICE, "Using version %d.%d.%d of PPP driver",
175 driver_version, driver_modification, driver_patch);
179 * disestablish_ppp - Restore the serial port to normal operation.
180 * This shouldn't call die() because it's called from die().
183 void disestablish_ppp(void)
189 set_kdebugflag (prev_kdebugflag);
191 * Check whether the link seems not to be 8-bit clean.
193 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
195 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
197 s = "bit 7 set to 1";
200 s = "bit 7 set to 0";
210 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
211 syslog(LOG_WARNING, "All received characters had %s", s);
215 if (ioctl(fd, TIOCSETD, &initdisc) < 0)
216 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
218 if (ioctl(fd, TIOCNXCL, 0) < 0)
219 syslog (LOG_WARNING, "ioctl(TIOCNXCL): %m");
226 * List of valid speeds.
229 unsigned int speed_int;
296 { 115200U, B115200 },
302 * Translate from bits/second to a speed_t.
304 int translate_speed (unsigned int bps)
306 struct speed *speedp;
310 for (speedp = speeds; speedp->speed_int; speedp++)
311 if (bps == speedp->speed_int)
312 return speedp->speed_val;
313 syslog(LOG_WARNING, "speed %u not supported", bps);
318 * Translate from a speed_t to bits/second.
321 unsigned int baud_rate_of (int speed)
323 struct speed *speedp;
327 for (speedp = speeds; speedp->speed_int; speedp++)
328 if (speed == speedp->speed_val)
329 return speedp->speed_int;
334 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
335 * at the requested speed, etc. If `local' is true, set CLOCAL
336 * regardless of whether the modem option was specified.
339 void set_up_tty (int fd, int local)
345 if (tcgetattr(fd, &tios) < 0) {
346 syslog(LOG_ERR, "tcgetattr: %m");
353 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL | CRTSCTS);
355 tios.c_cflag |= CRTSCTS;
357 tios.c_cflag |= CS8 | CREAD | HUPCL;
359 tios.c_cflag |= CLOCAL;
360 tios.c_iflag = IGNBRK | IGNPAR;
364 tios.c_cc[VTIME] = 0;
367 tios.c_iflag |= IXOFF;
368 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
369 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
372 speed = translate_speed(inspeed);
374 cfsetospeed(&tios, speed);
375 cfsetispeed(&tios, speed);
377 speed = cfgetospeed(&tios);
379 * We can't proceed if the serial port speed is B0,
380 * since that implies that the serial port is disabled.
383 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
389 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
390 syslog(LOG_ERR, "tcsetattr: %m");
394 baud_rate = baud_rate_of(speed);
399 * setdtr - control the DTR line on the serial port.
400 * This is called from die(), so it shouldn't call die().
402 void setdtr (int fd, int on)
404 int modembits = TIOCM_DTR;
406 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
410 * restore_tty - restore the terminal to the saved settings.
412 void restore_tty(void)
415 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
417 syslog(LOG_WARNING, "tcsetattr: %m");
423 * output - Output PPP packet.
426 void output (int unit, unsigned char *p, int len)
429 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
432 log_packet(p, len, "sent ");
434 if (write(fd, p, len) < 0) {
435 syslog(LOG_ERR, "write: %m");
441 * wait_input - wait until there is data available on fd,
442 * for the length of time specified by *timo (indefinite
446 void wait_input (struct timeval *timo)
453 n = select(fd+1, &ready, NULL, &ready, timo);
454 if (n < 0 && errno != EINTR) {
455 syslog(LOG_ERR, "select: %m");
462 * read_packet - get a PPP packet from the serial device.
465 int read_packet (unsigned char *buf)
469 len = read(fd, buf, PPP_MTU + PPP_HDRLEN);
471 if (errno == EWOULDBLOCK) {
474 syslog(LOG_ERR, "read(fd): %m");
481 * ppp_send_config - configure the transmit characteristics of
484 void ppp_send_config (int unit,int mtu,u_int32_t asyncmap,int pcomp,int accomp)
489 MAINDEBUG ((LOG_DEBUG, "send_config: mtu = %d\n", mtu));
490 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
492 if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
493 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
497 MAINDEBUG ((LOG_DEBUG, "send_config: asyncmap = %lx\n", asyncmap));
498 if (ioctl(fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
499 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
503 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
504 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
508 x = pcomp ? x | SC_COMP_PROT : x & ~SC_COMP_PROT;
509 x = accomp ? x | SC_COMP_AC : x & ~SC_COMP_AC;
511 MAINDEBUG ((LOG_DEBUG, "send_config: flags = %x\n", x));
512 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
513 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
519 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
521 void ppp_set_xaccm (int unit, ext_accm accm)
523 MAINDEBUG ((LOG_DEBUG, "set_xaccm: %08lx %08lx %08lx %08lx\n",
524 accm[0], accm[1], accm[2], accm[3]));
525 if (ioctl(fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
526 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
530 * ppp_recv_config - configure the receive-side characteristics of
533 void ppp_recv_config (int unit,int mru,u_int32_t asyncmap,int pcomp,int accomp)
537 MAINDEBUG ((LOG_DEBUG, "recv_config: mru = %d\n", mru));
538 if (ioctl(fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
539 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
541 MAINDEBUG ((LOG_DEBUG, "recv_config: asyncmap = %lx\n", asyncmap));
542 if (ioctl(fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
543 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
547 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
548 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
552 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
553 MAINDEBUG ((LOG_DEBUG, "recv_config: flags = %x\n", x));
554 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
555 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
561 * ccp_test - ask kernel whether a given compression method
562 * is acceptable for use.
564 int ccp_test (int unit, u_char *opt_ptr, int opt_len, int for_transmit)
566 struct ppp_option_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.
577 void ccp_flags_set (int unit, int isopen, int isup)
581 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
582 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
585 x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
586 x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
587 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
588 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
592 * sifvjcomp - config tcp header compression
595 int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid)
599 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
600 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
604 x = vjcomp ? x | SC_COMP_TCP : x &~ SC_COMP_TCP;
605 x = cidcomp ? x & ~SC_NO_TCP_CCID : x | SC_NO_TCP_CCID;
607 if(ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
608 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
613 if (ioctl (fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
614 syslog (LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
623 * sifup - Config the interface up and enable IP packets to pass.
630 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
631 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
632 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
636 ifr.ifr_flags |= (IFF_UP | IFF_POINTOPOINT);
637 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
638 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
645 * sifdown - Config the interface down and disable IP.
652 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
653 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
654 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
658 ifr.ifr_flags &= ~IFF_UP;
659 ifr.ifr_flags |= IFF_POINTOPOINT;
660 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
661 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
668 * sifaddr - Config the interface IP addresses and netmask.
671 int sifaddr (int unit, int our_adr, int his_adr, int net_mask)
676 SET_SA_FAMILY (ifr.ifr_addr, AF_INET);
677 SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET);
678 SET_SA_FAMILY (ifr.ifr_netmask, AF_INET);
680 strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
684 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = our_adr;
685 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
687 syslog (LOG_ERR, "ioctl(SIOCAIFADDR): %m");
689 syslog (LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
693 * Set the gateway address
695 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = his_adr;
696 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
697 syslog (LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
704 ((struct sockaddr_in *) &ifr.ifr_netmask)->sin_addr.s_addr = net_mask;
705 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
706 syslog (LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
711 * Add the device route
713 memset (&rt, '\0', sizeof (rt));
715 SET_SA_FAMILY (rt.rt_dst, AF_INET);
716 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
717 rt.rt_dev = ifname; /* MJC */
719 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
720 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
721 rt.rt_flags = RTF_UP | RTF_HOST;
723 if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
724 syslog (LOG_ERR, "ioctl(SIOCADDRT) device route: %m");
731 * cifaddr - Clear the interface IP addresses, and delete routes
732 * through the interface if possible.
735 int cifaddr (int unit, int our_adr, int his_adr)
739 * Delete the route through the device
741 memset (&rt, '\0', sizeof (rt));
743 SET_SA_FAMILY (rt.rt_dst, AF_INET);
744 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
745 rt.rt_dev = ifname; /* MJC */
747 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
748 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
749 rt.rt_flags = RTF_UP | RTF_HOST;
751 if (ioctl(sockfd, SIOCDELRT, &rt) < 0) {
752 syslog (LOG_ERR, "ioctl(SIOCDELRT) device route: %m");
759 * path_to_route - determine the path to the proc file system data
762 FILE *route_fd = (FILE *) 0;
763 static char route_buffer [100];
765 static char *path_to_route (void);
766 static int open_route_table (void);
767 static void close_route_table (void);
768 static int read_route_table (struct rtentry *rt);
769 static int defaultroute_exists (void);
772 * path_to_route - find the path to the route tables in the proc file system
775 static char *path_to_route (void)
777 struct mntent *mntent;
780 fp = fopen (MOUNTED, "r");
782 while ((mntent = getmntent (fp)) != 0) {
783 if (strcmp (mntent->mnt_type, MNTTYPE_IGNORE) == 0)
786 if (strcmp (mntent->mnt_type, "proc") == 0) {
787 strncpy (route_buffer, mntent->mnt_dir,
788 sizeof (route_buffer)-10);
789 route_buffer [sizeof (route_buffer)-10] = '\0';
790 strcat (route_buffer, "/net/route");
793 return (route_buffer);
798 syslog (LOG_ERR, "proc file system not mounted");
803 * open_route_table - open the interface to the route table
806 static int open_route_table (void)
810 if (route_fd != (FILE *) 0)
813 path = path_to_route();
817 route_fd = fopen (path, "r");
818 if (route_fd == (FILE *) 0) {
819 syslog (LOG_ERR, "can not open %s: %m", path);
823 /* read and discard the header line. */
824 if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0) {
832 * close_route_table - close the interface to the route table
835 static void close_route_table (void)
837 if (route_fd != (FILE *) 0) {
839 route_fd = (FILE *) 0;
844 * read_route_table - read the next entry from the route table
847 static int read_route_table (struct rtentry *rt)
849 static char delims[] = " \t\n";
850 char *dev_ptr, *ptr, *dst_ptr, *gw_ptr, *flag_ptr;
852 if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0)
855 memset (rt, '\0', sizeof (struct rtentry));
857 dev_ptr = strtok (route_buffer, delims); /* interface name */
858 dst_ptr = strtok (NULL, delims); /* destination address */
859 gw_ptr = strtok (NULL, delims); /* gateway */
860 flag_ptr = strtok (NULL, delims); /* flags */
862 ((struct sockaddr_in *) &rt->rt_dst)->sin_addr.s_addr =
863 strtoul (dst_ptr, NULL, 16); /* network order */
865 ((struct sockaddr_in *) &rt->rt_gateway)->sin_addr.s_addr =
866 strtoul (gw_ptr, NULL, 16); /* network order */
868 rt->rt_flags = (short) strtoul (flag_ptr, NULL, 16);
869 rt->rt_dev = dev_ptr;
875 * defaultroute_exists - determine if there is a default route
878 static int defaultroute_exists (void)
883 if (!open_route_table())
886 while (read_route_table(&rt) != 0) {
887 if (rt.rt_flags & RTF_UP == 0)
890 if (((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr == 0L) {
892 "ppp not replacing existing default route to %s[%s]",
894 inet_ntoa (((struct sockaddr_in *) &rt.rt_gateway)->
905 * sifdefaultroute - assign a default route through the address given.
908 int sifdefaultroute (int unit, int gateway)
912 if (defaultroute_exists())
915 memset (&rt, '\0', sizeof (rt));
916 SET_SA_FAMILY (rt.rt_dst, AF_INET);
917 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
918 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
920 rt.rt_flags = RTF_UP | RTF_GATEWAY;
921 if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
922 syslog (LOG_ERR, "default route ioctl(SIOCADDRT): %m");
929 * cifdefaultroute - delete a default route through the address given.
932 int cifdefaultroute (int unit, int gateway)
936 SET_SA_FAMILY (rt.rt_dst, AF_INET);
937 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
938 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
940 rt.rt_flags = RTF_UP | RTF_GATEWAY;
941 if (ioctl(sockfd, SIOCDELRT, &rt) < 0) {
942 syslog (LOG_ERR, "default route ioctl(SIOCDELRT): %m");
949 * sifproxyarp - Make a proxy ARP entry for the peer.
952 int sifproxyarp (int unit, u_int32_t his_adr)
954 struct arpreq arpreq;
956 memset (&arpreq, '\0', sizeof(arpreq));
958 * Get the hardware address of an interface on the same subnet
959 * as our local address.
961 if (!get_ether_addr(his_adr, &arpreq.arp_ha)) {
962 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
966 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
967 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
968 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
970 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
971 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
978 * cifproxyarp - Delete the proxy ARP entry for the peer.
981 int cifproxyarp (int unit, u_int32_t his_adr)
983 struct arpreq arpreq;
985 memset (&arpreq, '\0', sizeof(arpreq));
986 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
988 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
989 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
990 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
997 * get_ether_addr - get the hardware address of an interface on the
998 * the same subnet as ipaddr.
1001 int get_ether_addr (u_int32_t ipaddr, struct sockaddr *hwaddr)
1003 struct ifreq *ifr, *ifend, *ifp;
1005 u_int32_t ina, mask;
1006 struct sockaddr_dl *dla;
1009 struct ifreq ifs[MAX_IFS];
1011 ifc.ifc_len = sizeof(ifs);
1013 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1014 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
1017 MAINDEBUG ((LOG_DEBUG, "proxy arp: scanning %d interfaces for IP %s",
1018 ifc.ifc_len / sizeof(struct ifreq), ip_ntoa(ipaddr)));
1020 * Scan through looking for an interface with an Internet
1021 * address on the same subnet as `ipaddr'.
1023 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
1024 for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
1025 if (ifr->ifr_addr.sa_family == AF_INET) {
1026 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1027 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1028 MAINDEBUG ((LOG_DEBUG, "proxy arp: examining interface %s",
1031 * Check that the interface is up, and not point-to-point
1034 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1036 if ((ifreq.ifr_flags &
1037 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1038 != (IFF_UP|IFF_BROADCAST))
1041 * Get its netmask and check that it's on the right subnet.
1043 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1045 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1046 MAINDEBUG ((LOG_DEBUG, "proxy arp: interface addr %s mask %lx",
1047 ip_ntoa(ina), ntohl(mask)));
1048 if (((ipaddr ^ ina) & mask) != 0)
1057 syslog(LOG_INFO, "found interface %s for proxy arp", ifreq.ifr_name);
1059 * Now get the hardware address.
1061 if (ioctl (s, SIOCGIFHWADDR, &ifreq) < 0) {
1062 syslog(LOG_ERR, "SIOCGIFHWADDR(%s): %m", ifreq.ifr_name);
1066 /* Use the proper family for token ring support. */
1067 if (ifreq.ifr_hwaddr.sa_family == 0)
1068 hwaddr->sa_family = ARPHRD_ETHER;
1070 hwaddr->sa_family = ifreq.ifr_hwaddr.sa_family;
1072 /* Well, ethernet has the longest address so far . . . . */
1073 memcpy (&hwaddr->sa_data, &ifreq.ifr_hwaddr.sa_data, ETH_ALEN);
1075 MAINDEBUG ((LOG_DEBUG,
1076 "proxy arp: found hwaddr %02x:%02x:%02x:%02x:%02x:%02x",
1077 (int) ((unsigned char *) &hwaddr->sa_data)[0],
1078 (int) ((unsigned char *) &hwaddr->sa_data)[1],
1079 (int) ((unsigned char *) &hwaddr->sa_data)[2],
1080 (int) ((unsigned char *) &hwaddr->sa_data)[3],
1081 (int) ((unsigned char *) &hwaddr->sa_data)[4],
1082 (int) ((unsigned char *) &hwaddr->sa_data)[5]));
1087 * Internal routine to decode the version.modification.patch level
1090 static void decode_version (char *buf, int *version,
1091 int *modification, int *patch)
1094 *version = (int) strtoul (buf, &buf, 10);
1100 *modification = (int) strtoul (buf, &buf, 10);
1103 *patch = (int) strtoul (buf, &buf, 10);
1115 * ppp_available - check whether the system has any ppp interfaces
1116 * (in fact we check whether we can do an ioctl on ppp0).
1119 int ppp_available(void)
1123 int my_version, my_modification, my_patch;
1125 * Open a socket for doing the ioctl operations.
1127 s = socket(AF_INET, SOCK_DGRAM, 0);
1131 strncpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
1132 ok = ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1134 * Ensure that the hardware address is for PPP and not something else
1137 ok = ioctl (s, SIOCGIFHWADDR, (caddr_t) &ifr) >= 0;
1139 if (ok != 0 && ifr.ifr_hwaddr.sa_family != ARPHRD_PPP)
1142 * This is the PPP device. Validate the version of the driver at this
1143 * point to ensure that this program will work with the driver.
1146 char abBuffer [1024];
1149 ifr.ifr_data = abBuffer;
1150 size = ioctl (s, SIOCGPPPVER, (caddr_t) &ifr);
1154 decode_version (abBuffer,
1156 &driver_modification,
1163 driver_modification =
1167 * Validate the version of the driver against the version that we used.
1169 decode_version (PPP_VERSION,
1174 /* The version numbers must match */
1175 if (driver_version != my_version)
1178 /* The modification levels must be legal */
1179 if (driver_modification < my_modification)
1185 fprintf(stderr, "Sorry - PPP driver version %d.%d.%d is out of date\n",
1186 driver_version, driver_modification, driver_patch);
1187 exit (1); /* avoid confusing error message 'ppp is not supported'. */
1193 int logwtmp (char *line, char *name, char *host)
1197 memset (&ut, 0, sizeof (ut));
1198 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1199 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1200 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1201 (void)time(&ut.ut_time);
1203 pututline (&ut); /* Write the line to the proper place */
1204 endutent(); /* Indicate operation is complete */
1208 * Code for locking/unlocking the serial device.
1209 * This code is derived from chat.c.
1212 #define LOCKPREFIX "/usr/spool/uucp/LCK.."
1215 * lock - create a lock file for the named device
1221 char hdb_lock_buffer[12];
1225 if ((p = strrchr(dev, '/')) != NULL)
1227 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1228 if (lock_file == NULL)
1229 novm("lock file name");
1230 strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1232 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1234 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1235 /* Read the lock file to find out who has the device locked */
1236 n = read(fd, hdb_lock_buffer, 11);
1238 hdb_lock_buffer[n] = 0;
1239 pid = atoi(hdb_lock_buffer);
1242 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1245 if (kill(pid, 0) == -1 && errno == ESRCH) {
1246 /* pid no longer exists - remove the lock file */
1247 if (unlink(lock_file) == 0) {
1249 syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1253 syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1256 syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1261 syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1267 sprintf(hdb_lock_buffer, "%10d\n", getpid());
1268 write(fd, hdb_lock_buffer, 11);
1275 * unlock - remove our lockfile