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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
/* $Id$
*
* dhcpcd - DHCP client daemon -
* Copyright (C) 1996 - 1997 Yoichi Hariguchi <yoichi@fore.com>
*
* Dhcpcd is an RFC2131 and RFC1541 compliant DHCP client daemon.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
/*#include <linux/netdevice.h>*/
#include <net/if.h>
#include <net/if_arp.h>
#include <arpa/inet.h>
#include <net/route.h>
#include <errno.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "if.h"
#include "dhcp.h"
#include "error-handler.h"
struct ifinfo Ifbuf;
void
ifReset(char *ifname)
{
bzero((char *)&Ifbuf, sizeof(Ifbuf));
strncpy(Ifbuf.ifname, ifname, sizeof(Ifbuf.ifname));
Ifbuf.addr = htonl(0);
Ifbuf.mask = htonl(0);
Ifbuf.bcast = inet_addr("255.255.255.255");
Ifbuf.flags = IFF_UP | IFF_BROADCAST | IFF_NOTRAILERS | IFF_RUNNING;
ifConfig(&Ifbuf,1);
}
void
ifConfig(struct ifinfo * ifinfo,int rinstall)
{
int s;
struct ifreq ifr;
struct rtentry rtent;
struct sockaddr_in *p;
strncpy(ifr.ifr_name, ifinfo->ifname, sizeof(ifr.ifr_name));
if ( (s = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
logSysExit("socket (ifConfig)");
}
if ( ioctl(s, SIOCGIFHWADDR, &ifr) < 0 ) {
logSysExit("ioctl SIOCGIFHWADDR (ifConfig)");
}
if ( ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER ) {
logQuit("ifInit: interface %s is not Ethernet\n", ifr.ifr_name);
}
/* save hardware address of the interface
*/
bcopy(ifr.ifr_hwaddr.sa_data, ifinfo->haddr, sizeof(ifinfo->haddr));
/* configure interface
*/
p = (struct sockaddr_in *)&(ifr.ifr_addr);
p->sin_family = AF_INET;
p->sin_addr.s_addr = ifinfo->addr;
if ( ioctl(s, SIOCSIFADDR, &ifr) < 0 ) {
logSysExit("ioctl SIOCSIFADDR (ifConfig)");
}
p->sin_addr.s_addr = ifinfo->bcast;
if ( ioctl(s, SIOCSIFBRDADDR, &ifr) < 0 ) {
logSysExit("ioctl SIOCSIFBRDADDR (ifConfig)");
}
p->sin_addr.s_addr = ifinfo->mask;
if ( ioctl(s, SIOCSIFNETMASK, &ifr) < 0 ) {
logSysExit("ioctl SIOCSIFNETMASK (ifConfig)");
}
ifr.ifr_flags = ifinfo->flags;
if ( ioctl(s, SIOCSIFFLAGS, &ifr) < 0 ) {
logSysExit("ioctl SIOCSIFFLAGS (ifConfig)");
}
/* set route to the interface
*/
bzero((char *)&rtent, sizeof(rtent));
p = (struct sockaddr_in *)&rtent.rt_dst;
p->sin_family = AF_INET;
p->sin_addr.s_addr = (rinstall == 2) ? (ifinfo->addr & ifinfo->mask) : ifinfo->bcast; /* dest. net address */
p = (struct sockaddr_in *)&rtent.rt_genmask;
p->sin_family = AF_INET;
p->sin_addr.s_addr = (rinstall == 2) ? ifinfo->mask : 0xffffffff;
p = (struct sockaddr_in *)&rtent.rt_gateway;
p->sin_family = AF_INET;
p->sin_addr.s_addr = ifinfo->addr; /* gateway address */
rtent.rt_dev = ifinfo->ifname; /* interface name */
rtent.rt_metric = 1; /* metric (see route.h) */
rtent.rt_flags = RTF_UP;
if ( rinstall != 0 && ioctl(s, SIOCADDRT, &rtent) < 0 ) {
logSysExit("ioctl SIOCADDRT (ifConfig)");
}
close(s);
}
void
ifDown(struct ifinfo * ifinfo)
{
int s;
struct ifreq ifr;
strncpy(ifr.ifr_name, ifinfo->ifname, sizeof(ifr.ifr_name));
if ( (s = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
logSysExit("socket (ifDown)");
}
/* down interface
*/
/* ifr.ifr_flags = ~IFF_UP; */
ifr.ifr_flags = 0;
if ( ioctl(s, SIOCSIFFLAGS, &ifr) < 0 ) {
logSysExit("ioctl SIOCSIFFLAGS (ifDown)");
}
close(s);
}
u_long
getNaturalMask(u_long inaddr)
{
if ( isClassA(inaddr) ) {
return inet_addr("255.0.0.0");
}
if ( isClassB(inaddr) ) {
return inet_addr("255.255.0.0");
}
if ( isClassC(inaddr) ) {
return inet_addr("255.255.255.0");
}
return htonl(0);
}
u_long
getNaturalBcast(u_long inaddr)
{
if ( isClassA(inaddr) ) {
return (inaddr & htonl(0xff000000)) | htonl(0x00ffffff);
}
if ( isClassB(inaddr) ) {
return (inaddr & htonl(0xffff0000)) | htonl(0x0000ffff);
}
if ( isClassC(inaddr) ) {
return (inaddr & htonl(0xffffff00)) | htonl(0x000000ff);
}
return htonl(0);
}
void
saveIfInfo(struct ifinfo *ifbuf)
{
int fd;
int isfailed;
u_long leaseLeft;
char filename[IFNAMSIZ + 128];
/* set up environmet variable on the attached network interface name
*/
if ( setenv("DHCP_DEVICE", ifbuf->ifname, 1) < 0 ) {
logRet("setenv (saveIfInfo): insufficient space");
}
isfailed = 0;
strcpy(filename, DHCP_CACHE_FILE);
strcat(filename, ifbuf->ifname);
if ( (fd = creat(filename, 0644)) < 0 ) {
logSysRet("creat (saveIfinfo)");
return;
}
if ( write(fd, (const char *)&ifbuf->addr, sizeof(ifbuf->addr)) < 0 ) {
logSysRet("write (saveIfinfo)");
isfailed = 1;
}
if ( LeaseTime == INFINITE_LEASE_TIME ) {
leaseLeft = INFINITE_LEASE_TIME;
} else {
leaseLeft = ntohl(LeaseTime) - (time(NULL) - ReqSentTime);
}
if ( write(fd, (const char *)&leaseLeft, sizeof(leaseLeft)) < 0 ) {
logSysRet("write (saveIfinfo)");
isfailed = 1;
}
if ( isfailed ) {
if ( unlink((const char*)filename) < 0 ) {
logSysRet("unlink (saveIfinfo)");
}
}
close(fd);
}
void
getIfInfo(struct ifinfo * ifinfo)
{
int s;
struct ifreq ifr;
strcpy(ifr.ifr_name, ifinfo->ifname);
if ( (s = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
logSysExit("socket (getIfInfo)");
}
if ( ioctl(s, SIOCGIFHWADDR, &ifr) < 0 ) {
logSysExit("ioctl SIOCGIFHWADDR (getIfInfo)");
}
if ( ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER ) {
logQuit("getIfInfo: interface %s is not Ethernet\n", ifr.ifr_name);
}
/* save hardware address of the interface
*/
bcopy(ifr.ifr_hwaddr.sa_data, ifinfo->haddr, sizeof(ifinfo->haddr));
/* configure interface
*/
if ( ioctl(s, SIOCGIFADDR, &ifr) < 0 ) {
logSysExit("ioctl SIOCGIFADDR (getIfInfo)");
}
ifinfo->addr = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
if ( ioctl(s, SIOCGIFBRDADDR, &ifr) < 0 ) {
logSysExit("ioctl SIOCGIFBRDADDR (getIfInfo)");
}
ifinfo->addr = ((struct sockaddr_in *)&ifr.ifr_broadaddr)->sin_addr.s_addr;
if ( ioctl(s, SIOCGIFNETMASK, &ifr) < 0 ) {
logSysExit("ioctl SIOCGIFNETMASK (getIfInfo)");
}
ifinfo->mask = ((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr.s_addr;
if ( ioctl(s, SIOCGIFFLAGS, &ifr) < 0 ) {
logSysExit("ioctl SIOCGIFFLAGS (getIfInfo)");
}
ifinfo->flags = ifr.ifr_flags;
close(s);
}
void
setDefRoute(const char *routers, struct ifinfo *ifinfo)
{
int s;
int i;
u_long gwAddr; /* router's IP address (network byte order) */
struct rtentry rtent;
struct sockaddr_in *p;
if ( (s = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
logSysExit("socket (setDefRoute)");
}
bzero((char *)&rtent, sizeof(rtent));
p = (struct sockaddr_in *)&rtent.rt_dst;
p->sin_family = AF_INET;
p->sin_addr.s_addr = htonl(0); /* dest. net address (default route) */
p = (struct sockaddr_in *)&rtent.rt_gateway;
p->sin_family = AF_INET;
/* verify rouer addresses are correct
*/
for ( i = 0; i < *routers/4; ++i ) {
gwAddr = *((u_long *)(routers+1) + i);
if ( (gwAddr & ifinfo->mask) == (ifinfo->addr & ifinfo->mask) ) {
p->sin_addr.s_addr = gwAddr;
rtent.rt_dev = ifinfo->ifname; /* interface name */
rtent.rt_metric = 2; /* metric (see route.h) */
rtent.rt_flags = RTF_GATEWAY; /* dest. is a gateway */
if ( ioctl(s, SIOCADDRT, &rtent) < 0 ) {
logSysExit("ioctl SIOCADDRT (setDefRoute)");
}
break;
}
/* TODO: also verify if the router is alive or not by using ping
*/
}
close(s);
}
|