2 /* This file is derived from pppd's sys-linux.c file, and is under
3 the following copyright: */
6 * sys-linux.c - System-dependent procedures for setting up
7 * PPP interfaces on Linux systems
9 * Copyright (c) 1989 Carnegie Mellon University.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms are permitted
13 * provided that the above copyright notice and this paragraph are
14 * duplicated in all such forms and that any documentation,
15 * advertising materials, and other materials related to such
16 * distribution and use acknowledge that the software was developed
17 * by Carnegie Mellon University. The name of the
18 * University may not be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
22 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 #include <sys/types.h>
32 * This is to bypass problems with earlier kernels.
36 #undef _I386_STRING_H_
37 #define _I386_STRING_H_
38 #undef _LINUX_STRING_H_
39 #define _LINUX_STRING_H_
42 * Continue with the rest of the include sequences.
45 #include <sys/ioctl.h>
46 #include <sys/socket.h>
48 #include <sys/errno.h>
62 /* This is in netdevice.h. However, this compile will fail miserably if
63 you attempt to include netdevice.h because it has so many references
64 to __memcpy functions which it should not attempt to do. So, since I
65 really don't use it, but it must be defined, define it now. */
68 #define MAX_ADDR_LEN 7
71 #include "../pppd.small/usr.include.net/if.h"
72 #include "../pppd.small/usr.include.net/ppp_defs.h"
73 #include "../pppd.small/usr.include.net/if_arp.h"
74 #include "../pppd.small/usr.include.net/if_ppp.h"
75 #include "../pppd.small/usr.include.net/route.h"
76 #include <linux/if_ether.h>
77 #include <netinet/in.h>
78 #include <arpa/inet.h>
86 static int initdisc = -1; /* Initial TTY discipline */
87 static int prev_kdebugflag = 0;
88 static int has_default_route = 0;
89 static int has_proxy_arp = 0;
90 static int driver_version = 0;
91 static int driver_modification = 0;
92 static int driver_patch = 0;
93 static int restore_term = 0; /* 1 => we've munged the terminal */
94 static struct termios inittermios; /* Initial TTY termios */
96 int sockfd; /* socket for doing interface ioctls */
98 static char *lock_file;
101 #define syslog(a,b,c...) ({ printf(b, ## c); })
103 #define MAINDEBUG(x) syslog x
107 #define FLAGS_GOOD (IFF_UP | IFF_BROADCAST)
108 #define FLAGS_MASK (IFF_UP | IFF_BROADCAST | \
109 IFF_POINTOPOINT | IFF_LOOPBACK | IFF_NOARP)
112 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
116 #define SET_SA_FAMILY(addr, family) \
117 memset ((char *) &(addr), '\0', sizeof(addr)); \
118 addr.sa_family = (family);
122 * path_to_proc - determine the path to the proc file system data
125 FILE *route_fd = (FILE *) 0;
126 static char route_buffer [512];
128 static char *path_to_proc (void);
129 static int open_route_table (void);
130 static void close_route_table (void);
131 static int read_route_table (struct rtentry *rt);
132 static int defaultroute_exists (void);
135 * path_to_proc - find the path to the route tables in the proc file system
138 static char *path_to_proc (void)
141 strcpy(buf, "/proc");
146 * close_route_table - close the interface to the route table
149 static void close_route_table (void)
151 if (route_fd != (FILE *) 0)
154 route_fd = (FILE *) 0;
159 * open_route_table - open the interface to the route table
162 static int open_route_table (void)
168 path = path_to_proc();
174 strcat (path, "/net/route");
175 route_fd = fopen (path, "r");
176 if (route_fd == (FILE *) 0)
178 syslog (LOG_ERR, "can not open %s: %m", path);
185 * read_route_table - read the next entry from the route table
188 #define delims " \t\n"
189 static int read_route_table (struct rtentry *rt)
191 /*static char delims[] = " \t\n";*/
192 char *dev_ptr, *ptr, *dst_ptr, *gw_ptr, *flag_ptr;
194 memset (rt, '\0', sizeof (struct rtentry));
198 if (fgets (route_buffer, sizeof (route_buffer), route_fd) ==
204 dev_ptr = strtok (route_buffer, delims); /* interface name */
205 dst_ptr = strtok (NULL, delims); /* destination address */
206 gw_ptr = strtok (NULL, delims); /* gateway */
207 flag_ptr = strtok (NULL, delims); /* flags */
209 if (flag_ptr == (char *) 0) /* assume that we failed, somewhere. */
214 /* Discard that stupid header line which should never
215 * have been there in the first place !! */
216 if (isxdigit (*dst_ptr) && isxdigit (*gw_ptr) && isxdigit (*flag_ptr))
222 ((struct sockaddr_in *) &rt->rt_dst)->sin_addr.s_addr =
223 strtoul (dst_ptr, NULL, 16);
225 ((struct sockaddr_in *) &rt->rt_gateway)->sin_addr.s_addr =
226 strtoul (gw_ptr, NULL, 16);
228 rt->rt_flags = (short) strtoul (flag_ptr, NULL, 16);
229 rt->rt_dev = dev_ptr;
236 * defaultroute_exists - determine if there is a default route
239 static int defaultroute_exists (void)
244 if (!open_route_table())
249 while (read_route_table(&rt) != 0)
251 if ((rt.rt_flags & RTF_UP) == 0)
256 if (((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr == 0L)
259 "ppp not replacing existing default route to %s[%s]",
261 inet_ntoa (((struct sockaddr_in *) &rt.rt_gateway)->
274 * sifdefaultroute - assign a default route through the address given.
277 int sifdefaultroute (int unit, int gateway)
281 if (has_default_route == 0)
283 if (defaultroute_exists())
288 memset (&rt, '\0', sizeof (rt));
289 SET_SA_FAMILY (rt.rt_dst, AF_INET);
290 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
291 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
293 rt.rt_flags = RTF_UP | RTF_GATEWAY;
294 if (ioctl(sockfd, SIOCADDRT, &rt) < 0)
296 syslog (LOG_ERR, "default route ioctl(SIOCADDRT): %m");
300 has_default_route = 1;
305 * cifdefaultroute - delete a default route through the address given.
308 int cifdefaultroute (int unit, int gateway)
312 if (has_default_route)
314 memset (&rt, '\0', sizeof (rt));
315 SET_SA_FAMILY (rt.rt_dst, AF_INET);
316 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
317 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
319 rt.rt_flags = RTF_UP | RTF_GATEWAY;
320 if (ioctl(sockfd, SIOCDELRT, &rt) < 0 && errno != ESRCH)
324 syslog (LOG_ERR, "default route ioctl(SIOCDELRT): %m");
329 has_default_route = 0;
334 * sifproxyarp - Make a proxy ARP entry for the peer.
337 int sifproxyarp (int unit, u_int32_t his_adr)
339 struct arpreq arpreq;
341 * Sometime in the 1.3 series kernels, the arp request added a device name.
343 #include <linux/version.h>
344 #if LINUX_VERSION_CODE < 66381
345 char arpreq_arp_dev[32];
347 #define arpreq_arp_dev arpreq.arp_dev
350 if (has_proxy_arp == 0)
352 memset (&arpreq, '\0', sizeof(arpreq));
354 * Get the hardware address of an interface on the same subnet
355 * as our local address.
357 if (!get_ether_addr(his_adr, &arpreq.arp_ha, arpreq_arp_dev))
359 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
363 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
364 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
365 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
367 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0)
369 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
379 * cifproxyarp - Delete the proxy ARP entry for the peer.
382 int cifproxyarp (int unit, u_int32_t his_adr)
384 struct arpreq arpreq;
386 if (has_proxy_arp == 1)
388 memset (&arpreq, '\0', sizeof(arpreq));
389 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
390 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
392 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
393 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0)
395 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
404 * get_ether_addr - get the hardware address of an interface on the
405 * the same subnet as ipaddr.
408 char * ip_ntoa(u_int32_t x) {
414 static int local_get_ether_addr (u_int32_t ipaddr, struct sockaddr *hwaddr,
415 char *name, struct ifreq *ifs, int ifs_len)
417 struct ifreq *ifr, *ifend, *ifp;
420 struct sockaddr_dl *dla;
424 * Request the total list of all devices configured on your system.
426 ifc.ifc_len = ifs_len;
428 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0)
430 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
434 MAINDEBUG ((LOG_DEBUG, "proxy arp: scanning %d interfaces for IP %s",
435 ifc.ifc_len / sizeof(struct ifreq), ip_ntoa(ipaddr)));
437 * Scan through looking for an interface with an Internet
438 * address on the same subnet as `ipaddr'.
440 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
441 for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
443 if (ifr->ifr_addr.sa_family == AF_INET)
445 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
446 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
447 MAINDEBUG ((LOG_DEBUG, "proxy arp: examining interface %s",
450 * Check that the interface is up, and not point-to-point
453 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
458 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
463 * Get its netmask and check that it's on the right subnet.
465 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
470 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
471 MAINDEBUG ((LOG_DEBUG, "proxy arp: interface addr %s mask %lx",
472 ip_ntoa(ina), ntohl(mask)));
474 if (((ipaddr ^ ina) & mask) != 0)
487 memcpy (name, ifreq.ifr_name, sizeof(ifreq.ifr_name));
488 syslog(LOG_INFO, "found interface %s for proxy arp", name);
490 * Now get the hardware address.
492 memset (&ifreq.ifr_hwaddr, 0, sizeof (struct sockaddr));
493 if (ioctl (sockfd, SIOCGIFHWADDR, &ifreq) < 0)
495 syslog(LOG_ERR, "SIOCGIFHWADDR(%s): %m", ifreq.ifr_name);
501 sizeof (struct sockaddr));
503 MAINDEBUG ((LOG_DEBUG,
504 "proxy arp: found hwaddr %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
505 (int) ((unsigned char *) &hwaddr->sa_data)[0],
506 (int) ((unsigned char *) &hwaddr->sa_data)[1],
507 (int) ((unsigned char *) &hwaddr->sa_data)[2],
508 (int) ((unsigned char *) &hwaddr->sa_data)[3],
509 (int) ((unsigned char *) &hwaddr->sa_data)[4],
510 (int) ((unsigned char *) &hwaddr->sa_data)[5],
511 (int) ((unsigned char *) &hwaddr->sa_data)[6],
512 (int) ((unsigned char *) &hwaddr->sa_data)[7]));
516 int get_ether_addr (u_int32_t ipaddr, struct sockaddr *hwaddr, char *name)
522 * Allocate memory to hold the request.
524 ifs_len = MAX_IFS * sizeof (struct ifreq);
525 base_addr = (void *) malloc (ifs_len);
526 if (base_addr == (void *) 0)
528 syslog(LOG_ERR, "malloc(%d) failed to return memory", ifs_len);
532 * Find the hardware address associated with the controller
534 answer = local_get_ether_addr (ipaddr, hwaddr, name,
535 (struct ifreq *) base_addr, ifs_len);