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.
28 #include <sys/ioctl.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
32 #include <sys/errno.h>
36 #include <linux/ppp.h>
37 #include <linux/route.h>
38 #include <linux/if_ether.h>
39 #include <netinet/in.h>
40 #include <net/if_ppp.h>
47 static int initdisc = -1; /* Initial TTY discipline */
48 static int prev_kdebugflag = 0;
49 extern int kdebugflag;
50 extern u_long netmask;
55 void die __ARGS((int));
58 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
62 #define SET_SA_FAMILY(addr, family) \
63 memset ((char *) &(addr), '\0', sizeof(addr)); \
64 addr.sa_family = (family);
67 * set_kdebugflag - Define the debugging level for the kernel
70 int set_kdebugflag (int requested_level)
72 if (ioctl(fd, PPPIOCGDEBUG, &prev_kdebugflag) < 0) {
73 syslog(LOG_ERR, "ioctl(PPPIOCGDEBUG): %m");
77 if (prev_kdebugflag != requested_level) {
78 if (ioctl(fd, PPPIOCSDEBUG, &requested_level) < 0) {
79 syslog (LOG_ERR, "ioctl(PPPIOCSDEBUG): %m");
82 syslog(LOG_INFO, "set kernel debugging level to %d", requested_level);
88 * establish_ppp - Turn the serial port into a ppp interface.
91 void establish_ppp (void)
95 if (ioctl(fd, TIOCEXCL, 0) < 0) {
96 syslog (LOG_WARNING, "ioctl(TIOCEXCL): %m");
99 if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
100 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
104 if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
105 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
109 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
110 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
114 set_kdebugflag (kdebugflag);
118 * disestablish_ppp - Restore the serial port to normal operation.
119 * This shouldn't call die() because it's called from die().
122 void disestablish_ppp(void)
128 set_kdebugflag (prev_kdebugflag);
130 * Check whether the link seems not to be 8-bit clean.
132 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
134 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
136 s = "bit 7 set to 1";
139 s = "bit 7 set to 0";
149 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
150 syslog(LOG_WARNING, "All received characters had %s", s);
154 if (ioctl(fd, TIOCSETD, &initdisc) < 0)
155 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
157 if (ioctl(fd, TIOCNXCL, 0) < 0)
158 syslog (LOG_WARNING, "ioctl(TIOCNXCL): %m");
165 * output - Output PPP packet.
168 void output (int unit, unsigned char *p, int len)
171 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
174 log_packet(p, len, "sent ");
176 if (write(fd, p, len) < 0) {
177 syslog(LOG_ERR, "write: %m");
183 * read_packet - get a PPP packet from the serial device.
186 int read_packet (unsigned char *buf)
190 len = read(fd, buf, MTU + DLLHEADERLEN);
192 if (errno == EWOULDBLOCK) {
194 MAINDEBUG((LOG_DEBUG, "read(fd): EWOULDBLOCK"));
198 syslog(LOG_ERR, "read(fd): %m");
205 * ppp_send_config - configure the transmit characteristics of
208 void ppp_send_config (int unit,int mtu,u_long asyncmap,int pcomp,int accomp)
213 MAINDEBUG ((LOG_DEBUG, "send_config: mtu = %d\n", mtu));
214 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
216 if (ioctl(s, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
217 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
221 MAINDEBUG ((LOG_DEBUG, "send_config: asyncmap = %lx\n", asyncmap));
222 if (ioctl(fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
223 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
227 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
228 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
232 x = pcomp ? x | SC_COMP_PROT : x & ~SC_COMP_PROT;
233 x = accomp ? x | SC_COMP_AC : x & ~SC_COMP_AC;
235 MAINDEBUG ((LOG_DEBUG, "send_config: flags = %x\n", x));
236 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
237 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
243 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
246 ppp_set_xaccm(unit, accm)
250 MAINDEBUG ((LOG_DEBUG, "set_xaccm: %08lx %08lx %08lx %08lx\n",
251 accm[0], accm[1], accm[2], accm[3]));
252 if (ioctl(fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
253 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
257 * ppp_recv_config - configure the receive-side characteristics of
260 void ppp_recv_config (int unit,int mru,u_long asyncmap,int pcomp,int accomp)
264 MAINDEBUG ((LOG_DEBUG, "recv_config: mru = %d\n", mru));
265 if (ioctl(fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
266 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
268 MAINDEBUG ((LOG_DEBUG, "recv_config: asyncmap = %lx\n", asyncmap));
269 if (ioctl(fd, PPPIOCRASYNCMAP, (caddr_t) &asyncmap) < 0) {
270 syslog(LOG_ERR, "ioctl(PPPIOCRASYNCMAP): %m");
274 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
275 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
279 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
280 MAINDEBUG ((LOG_DEBUG, "recv_config: flags = %x\n", x));
281 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
282 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
288 * sifvjcomp - config tcp header compression
291 int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid)
295 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
296 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
300 x = vjcomp ? x | SC_COMP_TCP : x &~ SC_COMP_TCP;
301 x = cidcomp ? x & ~SC_NO_TCP_CCID : x | SC_NO_TCP_CCID;
303 if(ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
304 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
309 if (ioctl (fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
310 syslog (LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
319 * sifup - Config the interface up and enable IP packets to pass.
326 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
327 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
328 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
332 ifr.ifr_flags |= (IFF_UP | IFF_POINTOPOINT);
333 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
334 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
341 * sifdown - Config the interface down and disable IP.
348 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
349 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
350 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
354 ifr.ifr_flags &= ~IFF_UP;
355 ifr.ifr_flags |= IFF_POINTOPOINT;
356 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
357 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
364 * sifaddr - Config the interface IP addresses and netmask.
367 int sifaddr (int unit, int our_adr, int his_adr, int net_mask)
372 SET_SA_FAMILY (ifr.ifr_addr, AF_INET);
373 SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET);
374 SET_SA_FAMILY (ifr.ifr_netmask, AF_INET);
376 strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
380 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = our_adr;
381 if (ioctl(s, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
383 syslog (LOG_ERR, "ioctl(SIOCAIFADDR): %m");
385 syslog (LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
389 * Set the gateway address
391 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = his_adr;
392 if (ioctl(s, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
393 syslog (LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
400 ((struct sockaddr_in *) &ifr.ifr_netmask)->sin_addr.s_addr = net_mask;
401 if (ioctl(s, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
402 syslog (LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
407 * Add the device route
409 memset (&rt, '\0', sizeof (rt));
411 SET_SA_FAMILY (rt.rt_dst, AF_INET);
412 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
413 rt.rt_dev = ifname; /* MJC */
415 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
416 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
417 rt.rt_flags = RTF_UP | RTF_HOST;
419 if (ioctl(s, SIOCADDRT, &rt) < 0) {
420 syslog (LOG_ERR, "ioctl(SIOCADDRT) device route: %m");
427 * cifaddr - Clear the interface IP addresses, and delete routes
428 * through the interface if possible.
431 int cifaddr (int unit, int our_adr, int his_adr)
435 * Delete the route through the device
437 memset (&rt, '\0', sizeof (rt));
439 SET_SA_FAMILY (rt.rt_dst, AF_INET);
440 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
441 rt.rt_dev = ifname; /* MJC */
443 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
444 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
445 rt.rt_flags = RTF_UP | RTF_HOST;
447 if (ioctl(s, SIOCDELRT, &rt) < 0) {
448 syslog (LOG_ERR, "ioctl(SIOCDELRT) device route: %m");
455 * path_to_route - determine the path to the proc file system data
458 FILE *route_fd = (FILE *) 0;
459 static char route_buffer [100];
461 static char *path_to_route (void);
462 static int open_route_table (void);
463 static void close_route_table (void);
464 static int read_route_table (struct rtentry *rt);
465 static int defaultroute_exists (void);
468 * path_to_route - find the path to the route tables in the proc file system
471 static char *path_to_route (void)
473 struct mntent *mntent;
476 fp = fopen (MOUNTED, "r");
478 while ((mntent = getmntent (fp)) != 0) {
479 if (strcmp (mntent->mnt_type, MNTTYPE_IGNORE) == 0)
482 if (strcmp (mntent->mnt_type, "proc") == 0) {
483 strncpy (route_buffer, mntent->mnt_dir,
484 sizeof (route_buffer)-10);
485 route_buffer [sizeof (route_buffer)-10] = '\0';
486 strcat (route_buffer, "/net/route");
489 return (route_buffer);
494 syslog (LOG_ERR, "proc file system not mounted");
499 * open_route_table - open the interface to the route table
502 static int open_route_table (void)
506 if (route_fd != (FILE *) 0)
509 path = path_to_route();
513 route_fd = fopen (path, "r");
514 if (route_fd == (FILE *) 0) {
515 syslog (LOG_ERR, "can not open %s: %m", path);
519 /* read and discard the header line. */
520 if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0) {
528 * close_route_table - close the interface to the route table
531 static void close_route_table (void)
533 if (route_fd != (FILE *) 0) {
535 route_fd = (FILE *) 0;
540 * read_route_table - read the next entry from the route table
543 static int read_route_table (struct rtentry *rt)
545 static char delims[] = " \t\n";
546 char *dev_ptr, *ptr, *dst_ptr, *gw_ptr, *flag_ptr;
548 if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0)
551 memset (rt, '\0', sizeof (struct rtentry));
553 dev_ptr = strtok (route_buffer, delims); /* interface name */
554 dst_ptr = strtok (NULL, delims); /* destination address */
555 gw_ptr = strtok (NULL, delims); /* gateway */
556 flag_ptr = strtok (NULL, delims); /* flags */
558 ptr = strtok (NULL, delims); /* reference count */
559 ptr = strtok (NULL, delims); /* useage count */
560 ptr = strtok (NULL, delims); /* metric */
561 ptr = strtok (NULL, delims); /* mask */
564 ((struct sockaddr_in *) &rt->rt_dst)->sin_addr.s_addr =
565 strtoul (dst_ptr, NULL, 16);
567 ((struct sockaddr_in *) &rt->rt_gateway)->sin_addr.s_addr =
568 strtoul (gw_ptr, NULL, 16);
570 rt->rt_flags = (short) strtoul (flag_ptr, NULL, 16);
571 rt->rt_dev = dev_ptr;
577 * defaultroute_exists - determine if there is a default route
580 static int defaultroute_exists (void)
585 if (!open_route_table())
588 while (read_route_table(&rt) != 0) {
589 if (rt.rt_flags & RTF_UP == 0)
592 if (((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr == 0L) {
594 "ppp not replacing existing default route to %s[%s]",
596 inet_ntoa (((struct sockaddr_in *) &rt.rt_gateway)->
607 * sifdefaultroute - assign a default route through the address given.
610 int sifdefaultroute (int unit, int gateway)
614 if (defaultroute_exists())
617 memset (&rt, '\0', sizeof (rt));
618 SET_SA_FAMILY (rt.rt_dst, AF_INET);
619 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
620 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
622 rt.rt_flags = RTF_UP | RTF_GATEWAY;
623 if (ioctl(s, SIOCADDRT, &rt) < 0) {
624 syslog (LOG_ERR, "default route ioctl(SIOCADDRT): %m");
631 * cifdefaultroute - delete a default route through the address given.
634 int cifdefaultroute (int unit, int gateway)
638 SET_SA_FAMILY (rt.rt_dst, AF_INET);
639 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
640 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
642 rt.rt_flags = RTF_UP | RTF_GATEWAY;
643 if (ioctl(s, SIOCDELRT, &rt) < 0) {
644 syslog (LOG_ERR, "default route ioctl(SIOCDELRT): %m");
651 * sifproxyarp - Make a proxy ARP entry for the peer.
654 int sifproxyarp (int unit, u_long his_adr)
656 struct arpreq arpreq;
658 memset (&arpreq, '\0', sizeof(arpreq));
660 * Get the hardware address of an interface on the same subnet
661 * as our local address.
663 if (!get_ether_addr(his_adr, &arpreq.arp_ha)) {
664 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
668 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
669 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
670 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
672 if (ioctl(s, SIOCSARP, (caddr_t)&arpreq) < 0) {
673 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
680 * cifproxyarp - Delete the proxy ARP entry for the peer.
683 int cifproxyarp (int unit, u_long his_adr)
685 struct arpreq arpreq;
687 memset (&arpreq, '\0', sizeof(arpreq));
688 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
690 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
691 if (ioctl(s, SIOCDARP, (caddr_t)&arpreq) < 0) {
692 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
699 * get_ether_addr - get the hardware address of an interface on the
700 * the same subnet as ipaddr.
703 int get_ether_addr (u_long ipaddr, struct sockaddr *hwaddr)
705 struct ifreq *ifr, *ifend, *ifp;
708 struct sockaddr_dl *dla;
711 struct ifreq ifs[MAX_IFS];
713 ifc.ifc_len = sizeof(ifs);
715 if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
716 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
719 MAINDEBUG ((LOG_DEBUG, "proxy arp: scanning %d interfaces for IP %s",
720 ifc.ifc_len / sizeof(struct ifreq), ip_ntoa(ipaddr)));
722 * Scan through looking for an interface with an Internet
723 * address on the same subnet as `ipaddr'.
725 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
726 for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
727 if (ifr->ifr_addr.sa_family == AF_INET) {
728 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
729 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
730 MAINDEBUG ((LOG_DEBUG, "proxy arp: examining interface %s",
733 * Check that the interface is up, and not point-to-point
736 if (ioctl(s, SIOCGIFFLAGS, &ifreq) < 0)
738 if ((ifreq.ifr_flags &
739 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
740 != (IFF_UP|IFF_BROADCAST))
743 * Get its netmask and check that it's on the right subnet.
745 if (ioctl(s, SIOCGIFNETMASK, &ifreq) < 0)
747 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
748 MAINDEBUG ((LOG_DEBUG, "proxy arp: interface addr %s mask %lx",
749 ip_ntoa(ina), ntohl(mask)));
750 if (((ipaddr ^ ina) & mask) != 0)
759 syslog(LOG_INFO, "found interface %s for proxy arp", ifreq.ifr_name);
761 * Now get the hardware address.
763 if (ioctl (s, SIOCGIFHWADDR, &ifreq) < 0) {
764 syslog(LOG_ERR, "SIOCGIFHWADDR(%s): %m", ifreq.ifr_name);
768 hwaddr->sa_family = ARPHRD_ETHER;
769 memcpy (&hwaddr->sa_data, &ifr->ifr_hwaddr, ETH_ALEN);
771 MAINDEBUG ((LOG_DEBUG,
772 "proxy arp: found hwaddr %02x:%02x:%02x:%02x:%02x:%02x",
773 (int) ((unsigned char *) &ifr->ifr_hwaddr)[0],
774 (int) ((unsigned char *) &ifr->ifr_hwaddr)[1],
775 (int) ((unsigned char *) &ifr->ifr_hwaddr)[2],
776 (int) ((unsigned char *) &ifr->ifr_hwaddr)[3],
777 (int) ((unsigned char *) &ifr->ifr_hwaddr)[4],
778 (int) ((unsigned char *) &ifr->ifr_hwaddr)[5]));
783 * ppp_available - check whether the system has any ppp interfaces
784 * (in fact we check whether we can do an ioctl on ppp0).
787 int ppp_available(void)
792 s = socket(AF_INET, SOCK_DGRAM, 0);
794 return 1; /* can't tell - maybe we're not root */
796 strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
797 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;