1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
/*
* dev.c - A ethernet Device.
*
* Copyright (c) 1994, 1995, 1996 Eric Schenk.
* All rights reserved. Please see the file LICENSE which should be
* distributed with this software for terms of use.
*
* Patched from ppp.c to support ethernet devices
* like isdn4linux
* Wim Bonis bonis@kiss.de
*
* Further modifications by Eric Schenk to merge into diald mainline
* sources.
*
*/
#include "diald.h"
static int dead = 1;
/* internal flag to shortcut repeated calls to setaddr */
static int rx_count = -1;
/*
* fork_connect()
*
* Added by JPD and AJY, this function is provided to provide a general
* connect and disconnect functionality for dev mode. This is needed to
* reliably control isdn4linux and ippp0 because although it appears to
* diald as a device, it is actually a link that can be brought up and
* down and can have errors connecting (which you'd like to send back to
* diald so that it knows it doesn't have a connection) and it is nice
* to be able to control when the link goes down.
*/
void fork_connect(char *program)
{
block_signals();
dial_pid = fork();
if (dial_pid < 0) {
unblock_signals();
syslog(LOG_ERR, "failed to fork dialer: %m");
/* FIXME: Probably this should not be fatal */
die(1);
}
if (dial_pid == 0) {
/* change the signal actions back to the defaults, then unblock them. */
default_sigacts();
unblock_signals();
execl("/bin/sh", "sh", "-c", program, (char *)0);
syslog(LOG_ERR, "could not exec /bin/sh: %m");
_exit(127);
/* NOTREACHED */
}
/* This unblock must not be earlier, otherwise the child can finish
* a reset dial_pid to 0 before the parent gets past the above test.
* This kills the parent.
*/
unblock_signals();
/* AJY, added the program name so I could tell the difference between my */
/* connector and my disconnector */
syslog(LOG_INFO,"Running connect %s (pid = %d).",program,dial_pid);
}
void dev_start()
{
link_iface = -1 ;
rx_count = -1;
syslog(LOG_INFO, "Open device %s%d",device_node,device_iface);
dead = 0;
}
/*
* SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
* if it exists.
*/
#define SET_SA_FAMILY(addr, family) \
memset ((char *) &(addr), '\0', sizeof(addr)); \
addr.sa_family = (family);
/*
* Find the interface number of the ppp device that pppd opened up and
* do any routing we might need to do.
* If pppd has not yet opened the device, then return 0, else return 1.
*/
int dev_set_addrs()
{
ulong laddr = 0, raddr = 0;
/* Try to get the interface number if we don't know it yet. */
if (link_iface == -1) {
link_iface = device_iface;
syslog(LOG_INFO,"Old %s , New device : %s %d",device,device_node,device_iface);
}
/* Ok then, see if pppd has upped the interface yet. */
if (link_iface != -1) {
struct ifreq ifr;
SET_SA_FAMILY (ifr.ifr_addr, AF_INET);
SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET);
SET_SA_FAMILY (ifr.ifr_netmask, AF_INET);
sprintf(ifr.ifr_name, device);
if (ioctl(snoopfd, SIOCGIFFLAGS, (caddr_t) &ifr) == -1) {
syslog(LOG_ERR,"failed to read interface status from device %s",device);
return 0;
}
if (!(ifr.ifr_flags & IFF_UP))
return 0; /* interface is not up yet */
if (route_wait) {
/* set the initial rx counter once the link is up */
if (rx_count == -1) rx_count = dev_rx_count();
/* check if we got the routing packet yet */
if (dev_rx_count() == rx_count) return 0;
}
/* Ok, the interface is up, grab the addresses. */
if (ioctl(snoopfd, SIOCGIFADDR, (caddr_t) &ifr) == -1)
syslog(LOG_ERR,"failed to get local address from device %s: %m",device);
else
laddr = ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr;
if (ioctl(snoopfd, SIOCGIFDSTADDR, (caddr_t) &ifr) == -1)
syslog(LOG_ERR,"failed to get remote address: %m");
else
raddr = ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr;
if (dynamic_addrs) {
/* only do the configuration in dynamic mode. */
struct in_addr addr;
addr.s_addr = raddr;
strcpy(remote_ip,inet_ntoa(addr));
addr.s_addr = laddr;
strcpy(local_ip,inet_ntoa(addr));
local_addr = laddr;
syslog(LOG_INFO,"New addresses: local %s, remote %s.",
local_ip,remote_ip);
if (!do_reroute) {
proxy_config(local_ip,remote_ip);
set_ptp("sl",proxy_iface,remote_ip,1);
add_routes("sl",proxy_iface,local_ip,remote_ip,1);
}
}
/* Set the ptp routing for the new interface */
/* this was moved from about 15 lines above by ajy so that for */
/* dynamic addresses, we have the remote address when we make */
/* the call to setup the route */
set_ptp(device_node,link_iface,remote_ip,0);
if (do_reroute)
add_routes(device_node,link_iface,local_ip,remote_ip,0);
return 1;
}
return 0;
}
int dev_dead()
{
if (dead)
dev_reroute();
return (dead);
}
int dev_rx_count()
{
char buf[128];
int packets = 0;
FILE *fp;
sprintf(buf,"%s %s%d",path_ifconfig,device_node,link_iface);
if ((fp = popen(buf,"r"))==NULL) {
syslog(LOG_ERR,"Could not run command '%s': %m",buf);
return 0; /* assume half dead in this case... */
}
while (fgets(buf,128,fp)) {
if (sscanf(buf," RX packets:%d",&packets) == 1) {
break;
}
}
fclose(fp);
return packets;
}
void dev_stop()
{
/* FIXME: There should be something here that actually can shut
* down whatever is driving the ether device, or at least try.
* [The trick used by the ISDN people seems to be to hang up
* in the delroute scripts. The ip-down scripts make sense
* for this as well. This might well be good enough.]
*/
dead = 1;
}
void dev_reroute()
{
/* Kill the alternate routing */
if (do_reroute && link_iface != -1)
del_routes(device_node,link_iface,local_ip,remote_ip,0);
link_iface = -1;
/* Restore the original proxy routing */
proxy_config(orig_local_ip,orig_remote_ip);
set_ptp("sl",proxy_iface,orig_remote_ip,1);
add_routes("sl",proxy_iface,orig_local_ip,orig_remote_ip,1);
local_addr = inet_addr(orig_local_ip);
}
/* Dummy proc. This should never get called */
void dev_kill()
{
}
/* Dummy proc. This should never get called */
void dev_zombie()
{
}
|