File: route.c

package info (click to toggle)
diald 0.99.4-5
  • links: PTS
  • area: main
  • in suites: sarge, woody
  • size: 1,076 kB
  • ctags: 936
  • sloc: ansic: 7,109; tcl: 977; sh: 891; perl: 306; makefile: 110
file content (240 lines) | stat: -rw-r--r-- 6,268 bytes parent folder | download | duplicates (3)
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
/*
 * route.c - diald routing code.
 *
 * Copyright (c) 1994, 1995, 1996 Eric Schenk.
 * Copyright (c) 1999 Mike Jagdis.
 * All rights reserved. Please see the file LICENSE which should be
 * distributed with this software for terms of use.
 */

#include "diald.h"

#define dres_crap 1

static void
add_routes(char *desc, char *iface, char *lip, char *rip, int metric)
{
    char win[32];
    char buf[1024];

    /* FIXME: Should not report an error if the error was just that the
     *        route we tried to add already exists.
     *        (A new error reported by more recent kernels.)
     */

    if (debug&DEBUG_VERBOSE)
	mon_syslog(LOG_DEBUG, "%s: Establishing routes for %s", desc, iface);

    if (window == 0)
	win[0] = 0;
    else
    	sprintf(win,"window %d",window);

#if 1
    /* FIXME: this is only needed for 2.0 kernels. 2.2 and beyond
     * create routes automatically when the interface is configured.
     * On 2.2 and later kernels this just creates some annoying
     * duplicate routes. But if the metric is non-zero we can,
     * and should, get rid of the original zero metric route.
     */
    if (rip) {
	if (path_ip && *path_ip) {
#ifdef dres_crap
	    sprintf(buf,"route add %s dev %s",
                    rip, iface);
#else            
	    sprintf(buf,"%s route add %s dev %s scope link%s%s metric %d %s",
		path_ip, rip, iface,
		lip ? " src " : "",
		lip ? lip : "",
		metric, win);
#endif            
	} else {
	    sprintf(buf,"%s add %s metric %d %s dev %s",
		path_route, rip, metric, win, iface);
	}
	run_shell(SHELL_WAIT, desc, buf, -1);

	if (metric) {
	    if (path_ip && *path_ip) {
#ifdef dres_crap
		sprintf(buf,"route del %s dev %s",
                        rip, iface);
#else                
		sprintf(buf,"%s route del %s dev %s scope link%s%s metric 0 %s",
		    path_ip, rip, iface,
		    lip ? " src " : "",
		    lip ? lip : "",
		    win);
#endif                
	    } else {
		sprintf(buf,"%s del %s metric 0 %s dev %s",
		    path_route, rip, win, iface); 
	    }
	    run_shell(SHELL_WAIT, desc, buf, -1);
	}
    }
#endif

    /* Add in a default route for the link if required. */
    if (default_route) {
	if (path_ip && *path_ip) {
#ifdef dres_crap
	    sprintf(buf, "route add default dev %s",
                    iface);
#else            
	    sprintf(buf, "%s route add default dev %s scope link%s%s metric %d %s",
		path_ip, iface,
		lip ? " src " : "",
		lip ? lip : "",
		metric, win);
#endif            
	} else {
	    sprintf(buf,"%s add default metric %d %s netmask 0.0.0.0 dev %s",
		path_route, metric, win, iface);
	}
        run_shell(SHELL_WAIT, desc, buf, -1);
    }

    /* call addroute script */
    if (addroute) {
        sprintf(buf,"%s %s %s \"%s\" \"%s\" %d %d",
	    addroute, iface, (netmask)?netmask:"default",
	    lip, rip, metric, window);
	run_shell(SHELL_WAIT, desc, buf, -1);
    }

    if (proxyarp && rip) set_proxyarp(inet_addr(rip));
}


static void
del_routes(char *desc, char *iface, char *lip, char *rip, int metric)
{
    char buf[1024];

    if (debug&DEBUG_VERBOSE)
	mon_syslog(LOG_DEBUG, "%s: Removing routes for %s", desc, iface);

    if (proxyarp && rip) clear_proxyarp(inet_addr(rip));

    if (delroute) {
	/* call delroute <iface> <netmask> <local> <remote> */
        sprintf(buf, "%s %s %s \"%s\" \"%s\" %d",
	    delroute, iface,
	    (netmask) ? netmask : "default",
	    lip, rip, metric);
        run_shell(SHELL_WAIT, desc, buf, -1);
    }

    if (default_route) {
	if (path_ip && *path_ip) {
	    sprintf(buf, "%s route del default dev %s scope link%s%s metric %d",
		path_ip, iface,
		lip ? " src " : "",
		lip ? lip : "",
		metric); 
	} else {
	    sprintf(buf, "%s del default metric %d netmask 0.0.0.0 dev %s",
		path_route, metric, iface);
	}
        run_shell(SHELL_WAIT, desc, buf, -1);
    }
}


void
iface_start(char *mode, char *iftype, int ifunit,
    char *lip, char *rip, char *bip, int metric)
{
    char *iface, desc[32], buf[1024];

    strcpy(desc, "start ");
    snprintf(desc+6, sizeof(desc)-6-1, "%s%d", iftype, ifunit);
    iface = desc+6;

    if (monitors) {
	sprintf(buf, "INTERFACE\n%s\n%s\n%s\n", desc+6, lip, rip);
	mon_write(MONITOR_INTERFACE, buf, strlen(buf));
    }

    /* mark the interface as up */
    if (ifsetup) {
	sprintf(buf, "%s start %s %s",
	    ifsetup, mode, iface);
	run_shell(SHELL_WAIT, desc, buf, -1);
	return;
    }

    /* With no ifsetup script we have to do it all ourselves. */
    if (lip) {
	sprintf(buf,"%s %s %s%s%s%s%s netmask %s metric %d mtu %d up",
	    path_ifconfig, iface, lip,
	    rip ? " pointopoint " : "",
	    rip ? rip : "",
	    bip ? " broadcast " : "",
	    bip ? bip : "",
	    netmask ? netmask : "255.255.255.255",
	    metric, mtu);
	run_shell(SHELL_WAIT, desc, buf, -1);
    }

    add_routes(desc, iface, lip, rip, metric);
}


void
iface_stop(char *mode, char *iftype, int ifunit,
    char *lip, char *rip, char *bip, int metric)
{
    char *iface, desc[32], buf[128];

    strcpy(desc, "stop ");
    snprintf(desc+5, sizeof(desc)-5-1, "%s%d", iftype, ifunit);
    iface = desc+5;

    if (ifsetup) {
	sprintf(buf, "%s stop %s %s",
	    ifsetup, mode, iface);
	run_shell(SHELL_WAIT, desc, buf, -1);
	return;
    }

    /* With no ifsetup script we have to do it all ourselves. */

    /* Deleting the addresses has the effect of deleting routes as well
     * on 2.1 and later kernels but we still call del_routes first
     * because the user delroutes scripts may have been abused to do
     * something "special".
     * 2.0.x kernels have a different behaviour when the address is
     * set to 0.0.0.0 so we use 127.0.0.1 as a "safe" local address.
     */
    del_routes(desc, iface, lip, rip, metric);

    sprintf(buf, "%s %s %s",
	path_ifconfig, iface,
#ifdef HAVE_AF_PACKET
	af_packet ? "0.0.0.0" : "127.0.0.1"
#else
	"127.0.0.1"
#endif
    );
    run_shell(SHELL_WAIT, desc, buf, -1);
}


void
iface_down(char *mode, char *iftype, int ifunit,
    char *lip, char *rip, char *bip, int metric)
{
    char *iface, desc[32], buf[128];

    strcpy(desc, "down ");
    snprintf(desc+5, sizeof(desc)-5-1, "%s%d", iftype, ifunit);
    iface = desc+5;

    sprintf(buf, "%s %s down",
	path_ifconfig, iface
    );
    run_shell(SHELL_WAIT, desc, buf, -1);
}