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
|
/*
routing.c, manipulating routing table for PPTP Client
Copyright (C) 2006 Free Software Foundation
This program 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., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA
*/
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include "routing.h"
#include "config.h"
#if defined (__SVR4) && defined (__sun) /* Solaris */
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <errno.h>
#include "util.h"
/* PF_ROUTE socket*/
int rts;
/* Destination and gateway addresses */
struct sockaddr_in rdst, rgw;
/* Request sequence */
int rseq;
int dorouting;
#else /* Solaris */
/* route to the server */
char *route;
#endif /* Solaris */
/*
Design discussion.
The primary task of this module is to add a host route to the PPTP
server so that the kernel continues to deliver PPTP control and data
connection packets to the server despite the new PPP interface that is
created. The flag --nohostroute is to disable this.
A secondary task may be to implement all-to-tunnel routing if the
appropriate flag is specified on the command line. The flag
--route-all is to implement this (not yet implemented).
Caveat.
It is not possible from the "ip route" command to determine if a host
route already exists, so it isn't practical to put the routing table
back exactly as it was.
We have a choice of either leaving our route lying around, or
destroying a route that the user had pre-arranged. Both are
unfortunate. The flag --remove-host-route is to remove the route
regardless (not yet implemented).
*/
void routing_init(char *ip) {
#if defined (__SVR4) && defined (__sun) /* Solaris */
rdst.sin_family = AF_INET;
if ( ! inet_pton(AF_INET, ip, &rdst.sin_addr) ) {
log("Cannot convert address: %s", strerror(errno));
return;
}
if ( (rts = socket(PF_ROUTE, SOCK_RAW, AF_INET )) < 0 ) {
log("Cannot open routing socket: %s", strerror(errno));
return;
}
struct rt_msg rtm = {
.hdr.rtm_msglen = sizeof(struct rt_msg),
.hdr.rtm_version = RTM_VERSION,
.hdr.rtm_type = RTM_GET,
.hdr.rtm_addrs = RTA_DST,
.hdr.rtm_pid = getpid(),
.hdr.rtm_seq = ++rseq,
.addrs[RTAX_DST] = rdst
};
if ( write(rts, &rtm, rtm.hdr.rtm_msglen) != rtm.hdr.rtm_msglen ) {
log("Error writing to routing socket: %s", strerror(errno));
close(rts);
return;
}
while ( read(rts, &rtm, sizeof(struct rt_msg)) > 0 )
if ( rtm.hdr.rtm_pid == getpid() && rtm.hdr.rtm_seq == rseq) {
/* Check if host route already present */
if ( ( rtm.hdr.rtm_flags & RTF_HOST ) != RTF_HOST ) {
rgw = rtm.addrs[RTAX_GATEWAY];
dorouting = 1;
}
break;
}
#endif /* Solaris */
#if defined(__linux)
char buf[256];
char tbuf[256];
const char *uid;
FILE *p;
snprintf(buf, 255, "%s route get %s", IP_BINARY, ip);
p = popen(buf, "r");
fgets(buf, 255, p);
/* TODO: check for failure of fgets */
uid = strstr(buf, " uid");
if (uid) {
snprintf(tbuf, uid - buf + 1, "%s", buf);
route = strdup(tbuf);
} else {
route = strdup(buf);
}
pclose(p);
/* TODO: check for failure of command */
#endif /* __linux__ */
}
void routing_start(void) {
#if defined (__SVR4) && defined (__sun) /* Solaris */
if ( ! dorouting )
return;
struct rt_msg rtm = {
.hdr.rtm_msglen = sizeof(struct rt_msg),
.hdr.rtm_version = RTM_VERSION,
.hdr.rtm_type = RTM_ADD,
.hdr.rtm_flags = RTF_HOST | RTF_GATEWAY | RTF_STATIC,
.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY,
.hdr.rtm_pid = getpid(),
.hdr.rtm_seq = ++rseq,
.addrs[RTAX_DST] = rdst,
.addrs[RTAX_GATEWAY] = rgw
};
if ( write(rts, &rtm, rtm.hdr.rtm_msglen) != rtm.hdr.rtm_msglen ) {
log("Error adding route: %s", strerror(errno));
}
#endif
#if defined(__linux__)
char buf[256];
FILE *p;
snprintf(buf, 255, "%s route replace %s", IP_BINARY, route);
p = popen(buf, "r");
pclose(p);
#endif /* __linux__ */
}
void routing_end(void) {
#if defined (__SVR4) && defined (__sun) /* Solaris */
if ( ! dorouting)
return;
struct rt_msg rtm = {
.hdr.rtm_msglen = sizeof(struct rt_msg),
.hdr.rtm_version = RTM_VERSION,
.hdr.rtm_type = RTM_DELETE,
.hdr.rtm_flags = RTF_HOST | RTF_GATEWAY | RTF_STATIC,
.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY,
.hdr.rtm_pid = getpid(),
.hdr.rtm_seq = ++rseq,
.addrs[RTAX_DST] = rdst,
.addrs[RTAX_GATEWAY] = rgw
};
if ( write(rts, &rtm, rtm.hdr.rtm_msglen) != rtm.hdr.rtm_msglen ) {
log("Error deleting route: %s", strerror(errno));
}
#endif /* Solaris */
#if defined(__linux__)
char buf[256];
FILE *p;
snprintf(buf, 255, "%s route delete %s", IP_BINARY, route);
p = popen(buf, "r");
pclose(p);
#endif /* __linux__ */
}
|