blob: 337b3582590f7ffbbff1b73ceece0bfb328862a5 [file] [log] [blame]
Eric Andersenec455952001-02-14 08:11:27 +00001/* route
2 *
3 * Similar to the standard Unix route, but with only the necessary
4 * parts for AF_INET
5 *
6 * Bjorn Wesen, Axis Communications AB
7 *
8 * Author of the original route:
9 * Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
10 * (derived from FvK's 'route.c 1.70 01/04/94')
11 *
12 * This program is free software; you can redistribute it
13 * and/or modify it under the terms of the GNU General
14 * Public License as published by the Free Software
15 * Foundation; either version 2 of the License, or (at
16 * your option) any later version.
17 *
Eric Andersen3e6ff902001-03-09 21:24:12 +000018 * $Id: route.c,v 1.9 2001/03/09 21:24:12 andersen Exp $
Eric Andersenec455952001-02-14 08:11:27 +000019 *
Eric Andersen68be2ab2001-02-14 19:26:39 +000020 * displayroute() code added by Vladimir N. Oleynik <dzo@simtreas.ru>
Eric Andersen26d53eb2001-03-07 06:33:01 +000021 * adjustments by Larry Doolittle <LRDoolittle@lbl.gov>
Eric Andersenec455952001-02-14 08:11:27 +000022 */
23
Eric Andersenec455952001-02-14 08:11:27 +000024#include <sys/types.h>
25#include <sys/ioctl.h>
26#include <sys/socket.h>
27#include <net/route.h>
28#include <linux/param.h> // HZ
29#include <netinet/in.h>
30#include <arpa/inet.h>
31#include <stdio.h>
32#include <errno.h>
33#include <fcntl.h>
34#include <stdlib.h>
35#include <string.h>
36#include <getopt.h>
37#include <unistd.h>
38#include <ctype.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000039#include "busybox.h"
Eric Andersenec455952001-02-14 08:11:27 +000040
41#define _(x) x
42
43#define RTACTION_ADD 1
44#define RTACTION_DEL 2
45#define RTACTION_HELP 3
46#define RTACTION_FLUSH 4
47#define RTACTION_SHOW 5
48
49#define E_NOTFOUND 8
50#define E_SOCK 7
51#define E_LOOKUP 6
52#define E_VERSION 5
53#define E_USAGE 4
54#define E_OPTERR 3
55#define E_INTERN 2
56#define E_NOSUPP 1
57
58/* resolve XXX.YYY.ZZZ.QQQ -> binary */
59
60static int
61INET_resolve(char *name, struct sockaddr *sa)
62{
63 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
64
65 sin->sin_family = AF_INET;
66 sin->sin_port = 0;
67
68 /* Default is special, meaning 0.0.0.0. */
Mark Whitley99806ad2001-02-15 23:00:48 +000069 if (strcmp(name, "default")==0) {
Eric Andersenec455952001-02-14 08:11:27 +000070 sin->sin_addr.s_addr = INADDR_ANY;
Mark Whitley99806ad2001-02-15 23:00:48 +000071 return 1;
Eric Andersenec455952001-02-14 08:11:27 +000072 }
73 /* Look to see if it's a dotted quad. */
74 if (inet_aton(name, &sin->sin_addr)) {
75 return 0;
76 }
77 /* guess not.. */
78 return -1;
79}
80
81#if defined (SIOCADDRTOLD) || defined (RTF_IRTT) /* route */
82#define HAVE_NEW_ADDRT 1
83#endif
84#ifdef RTF_IRTT /* route */
85#define HAVE_RTF_IRTT 1
86#endif
87#ifdef RTF_REJECT /* route */
88#define HAVE_RTF_REJECT 1
89#endif
90
91#if HAVE_NEW_ADDRT
92#define mask_in_addr(x) (((struct sockaddr_in *)&((x).rt_genmask))->sin_addr.s_addr)
93#define full_mask(x) (x)
94#else
95#define mask_in_addr(x) ((x).rt_genmask)
96#define full_mask(x) (((struct sockaddr_in *)&(x))->sin_addr.s_addr)
97#endif
98
99/* add or delete a route depending on action */
100
101static int
102INET_setroute(int action, int options, char **args)
103{
104 struct rtentry rt;
105 char target[128], gateway[128] = "NONE", netmask[128] = "default";
106 int xflag, isnet;
107 int skfd;
108
109 xflag = 0;
110
Mark Whitley99806ad2001-02-15 23:00:48 +0000111 if (strcmp(*args, "-net")==0) {
Eric Andersenec455952001-02-14 08:11:27 +0000112 xflag = 1;
113 args++;
Mark Whitley99806ad2001-02-15 23:00:48 +0000114 } else if (strcmp(*args, "-host")==0) {
Eric Andersenec455952001-02-14 08:11:27 +0000115 xflag = 2;
116 args++;
117 }
118 if (*args == NULL)
Eric Andersen67991cf2001-02-14 21:23:06 +0000119 show_usage();
Eric Andersenec455952001-02-14 08:11:27 +0000120
121 safe_strncpy(target, *args++, (sizeof target));
122
123 /* Clean out the RTREQ structure. */
124 memset((char *) &rt, 0, sizeof(struct rtentry));
125
126
127 if ((isnet = INET_resolve(target, &rt.rt_dst)) < 0) {
Mark Whitley99806ad2001-02-15 23:00:48 +0000128 error_msg(_("can't resolve %s"), target);
129 return EXIT_FAILURE; /* XXX change to E_something */
Eric Andersenec455952001-02-14 08:11:27 +0000130 }
131
132 switch (xflag) {
133 case 1:
134 isnet = 1;
135 break;
136
137 case 2:
138 isnet = 0;
139 break;
140
141 default:
142 break;
143 }
144
145 /* Fill in the other fields. */
146 rt.rt_flags = (RTF_UP | RTF_HOST);
147 if (isnet)
148 rt.rt_flags &= ~RTF_HOST;
149
150 while (*args) {
Mark Whitley99806ad2001-02-15 23:00:48 +0000151 if (strcmp(*args, "metric")==0) {
Eric Andersenec455952001-02-14 08:11:27 +0000152 int metric;
153
154 args++;
155 if (!*args || !isdigit(**args))
Eric Andersen67991cf2001-02-14 21:23:06 +0000156 show_usage();
Eric Andersenec455952001-02-14 08:11:27 +0000157 metric = atoi(*args);
158#if HAVE_NEW_ADDRT
159 rt.rt_metric = metric + 1;
160#else
Mark Whitley99806ad2001-02-15 23:00:48 +0000161 ENOSUPP("inet_setroute", "NEW_ADDRT (metric)"); /* XXX Fixme */
Eric Andersenec455952001-02-14 08:11:27 +0000162#endif
163 args++;
164 continue;
165 }
166
Mark Whitley99806ad2001-02-15 23:00:48 +0000167 if (strcmp(*args, "netmask")==0) {
Eric Andersenec455952001-02-14 08:11:27 +0000168 struct sockaddr mask;
169
170 args++;
171 if (!*args || mask_in_addr(rt))
Eric Andersen67991cf2001-02-14 21:23:06 +0000172 show_usage();
Eric Andersenec455952001-02-14 08:11:27 +0000173 safe_strncpy(netmask, *args, (sizeof netmask));
174 if ((isnet = INET_resolve(netmask, &mask)) < 0) {
Mark Whitley99806ad2001-02-15 23:00:48 +0000175 error_msg(_("can't resolve netmask %s"), netmask);
176 return E_LOOKUP;
Eric Andersenec455952001-02-14 08:11:27 +0000177 }
178 rt.rt_genmask = full_mask(mask);
179 args++;
180 continue;
181 }
182
Mark Whitley99806ad2001-02-15 23:00:48 +0000183 if (strcmp(*args, "gw")==0 || strcmp(*args, "gateway")==0) {
Eric Andersenec455952001-02-14 08:11:27 +0000184 args++;
185 if (!*args)
Eric Andersen67991cf2001-02-14 21:23:06 +0000186 show_usage();
Eric Andersenec455952001-02-14 08:11:27 +0000187 if (rt.rt_flags & RTF_GATEWAY)
Eric Andersen67991cf2001-02-14 21:23:06 +0000188 show_usage();
Eric Andersenec455952001-02-14 08:11:27 +0000189 safe_strncpy(gateway, *args, (sizeof gateway));
190 if ((isnet = INET_resolve(gateway, &rt.rt_gateway)) < 0) {
Mark Whitley99806ad2001-02-15 23:00:48 +0000191 error_msg(_("can't resolve gw %s"), gateway);
192 return E_LOOKUP;
Eric Andersenec455952001-02-14 08:11:27 +0000193 }
194 if (isnet) {
Mark Whitley99806ad2001-02-15 23:00:48 +0000195 error_msg(
196 _("%s: cannot use a NETWORK as gateway!"),
Eric Andersenec455952001-02-14 08:11:27 +0000197 gateway);
Mark Whitley99806ad2001-02-15 23:00:48 +0000198 return E_OPTERR;
Eric Andersenec455952001-02-14 08:11:27 +0000199 }
200 rt.rt_flags |= RTF_GATEWAY;
201 args++;
202 continue;
203 }
204
Mark Whitley99806ad2001-02-15 23:00:48 +0000205 if (strcmp(*args, "mss")==0) {
Eric Andersenec455952001-02-14 08:11:27 +0000206 args++;
207 rt.rt_flags |= RTF_MSS;
208 if (!*args)
Eric Andersen67991cf2001-02-14 21:23:06 +0000209 show_usage();
Eric Andersenec455952001-02-14 08:11:27 +0000210 rt.rt_mss = atoi(*args);
211 args++;
212 if (rt.rt_mss < 64 || rt.rt_mss > 32768) {
Mark Whitley99806ad2001-02-15 23:00:48 +0000213 error_msg(_("Invalid MSS."));
214 return E_OPTERR;
Eric Andersenec455952001-02-14 08:11:27 +0000215 }
216 continue;
217 }
218
Mark Whitley99806ad2001-02-15 23:00:48 +0000219 if (strcmp(*args, "window")==0) {
Eric Andersenec455952001-02-14 08:11:27 +0000220 args++;
221 if (!*args)
Eric Andersen67991cf2001-02-14 21:23:06 +0000222 show_usage();
Eric Andersenec455952001-02-14 08:11:27 +0000223 rt.rt_flags |= RTF_WINDOW;
224 rt.rt_window = atoi(*args);
225 args++;
226 if (rt.rt_window < 128) {
Mark Whitley99806ad2001-02-15 23:00:48 +0000227 error_msg(_("Invalid window."));
228 return E_OPTERR;
Eric Andersenec455952001-02-14 08:11:27 +0000229 }
230 continue;
231 }
232
Mark Whitley99806ad2001-02-15 23:00:48 +0000233 if (strcmp(*args, "irtt")==0) {
Eric Andersenec455952001-02-14 08:11:27 +0000234 args++;
235 if (!*args)
Eric Andersen67991cf2001-02-14 21:23:06 +0000236 show_usage();
Eric Andersenec455952001-02-14 08:11:27 +0000237 args++;
238#if HAVE_RTF_IRTT
239 rt.rt_flags |= RTF_IRTT;
240 rt.rt_irtt = atoi(*(args - 1));
241 rt.rt_irtt *= (HZ / 100); /* FIXME */
242#if 0 /* FIXME: do we need to check anything of this? */
243 if (rt.rt_irtt < 1 || rt.rt_irtt > (120 * HZ)) {
Mark Whitley99806ad2001-02-15 23:00:48 +0000244 error_msg(_("Invalid initial rtt."));
245 return E_OPTERR;
Eric Andersenec455952001-02-14 08:11:27 +0000246 }
247#endif
248#else
Mark Whitley99806ad2001-02-15 23:00:48 +0000249 ENOSUPP("inet_setroute", "RTF_IRTT"); /* XXX Fixme */
Eric Andersenec455952001-02-14 08:11:27 +0000250#endif
251 continue;
252 }
253
Mark Whitley99806ad2001-02-15 23:00:48 +0000254 if (strcmp(*args, "reject")==0) {
Eric Andersenec455952001-02-14 08:11:27 +0000255 args++;
256#if HAVE_RTF_REJECT
257 rt.rt_flags |= RTF_REJECT;
258#else
Mark Whitley99806ad2001-02-15 23:00:48 +0000259 ENOSUPP("inet_setroute", "RTF_REJECT"); /* XXX Fixme */
Eric Andersenec455952001-02-14 08:11:27 +0000260#endif
261 continue;
262 }
Mark Whitley99806ad2001-02-15 23:00:48 +0000263 if (strcmp(*args, "mod")==0) {
Eric Andersenec455952001-02-14 08:11:27 +0000264 args++;
265 rt.rt_flags |= RTF_MODIFIED;
266 continue;
267 }
Mark Whitley99806ad2001-02-15 23:00:48 +0000268 if (strcmp(*args, "dyn")==0) {
Eric Andersenec455952001-02-14 08:11:27 +0000269 args++;
270 rt.rt_flags |= RTF_DYNAMIC;
271 continue;
272 }
Mark Whitley99806ad2001-02-15 23:00:48 +0000273 if (strcmp(*args, "reinstate")==0) {
Eric Andersenec455952001-02-14 08:11:27 +0000274 args++;
275 rt.rt_flags |= RTF_REINSTATE;
276 continue;
277 }
Mark Whitley99806ad2001-02-15 23:00:48 +0000278 if (strcmp(*args, "device")==0 || strcmp(*args, "dev")==0) {
Eric Andersenec455952001-02-14 08:11:27 +0000279 args++;
280 if (rt.rt_dev || *args == NULL)
Eric Andersen67991cf2001-02-14 21:23:06 +0000281 show_usage();
Eric Andersenec455952001-02-14 08:11:27 +0000282 rt.rt_dev = *args++;
283 continue;
284 }
285 /* nothing matches */
286 if (!rt.rt_dev) {
287 rt.rt_dev = *args++;
288 if (*args)
Eric Andersen67991cf2001-02-14 21:23:06 +0000289 show_usage(); /* must be last to catch typos */
Mark Whitley99806ad2001-02-15 23:00:48 +0000290 } else {
Eric Andersen67991cf2001-02-14 21:23:06 +0000291 show_usage();
Mark Whitley99806ad2001-02-15 23:00:48 +0000292 }
Eric Andersenec455952001-02-14 08:11:27 +0000293 }
294
295#if HAVE_RTF_REJECT
296 if ((rt.rt_flags & RTF_REJECT) && !rt.rt_dev)
297 rt.rt_dev = "lo";
298#endif
299
300 /* sanity checks.. */
301 if (mask_in_addr(rt)) {
302 unsigned long mask = mask_in_addr(rt);
303 mask = ~ntohl(mask);
304 if ((rt.rt_flags & RTF_HOST) && mask != 0xffffffff) {
Mark Whitley99806ad2001-02-15 23:00:48 +0000305 error_msg(
306 _("netmask %.8x doesn't make sense with host route"),
Eric Andersenec455952001-02-14 08:11:27 +0000307 (unsigned int)mask);
Mark Whitley99806ad2001-02-15 23:00:48 +0000308 return E_OPTERR;
Eric Andersenec455952001-02-14 08:11:27 +0000309 }
310 if (mask & (mask + 1)) {
Mark Whitley99806ad2001-02-15 23:00:48 +0000311 error_msg(_("bogus netmask %s"), netmask);
312 return E_OPTERR;
Eric Andersenec455952001-02-14 08:11:27 +0000313 }
314 mask = ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr;
315 if (mask & ~mask_in_addr(rt)) {
Mark Whitley99806ad2001-02-15 23:00:48 +0000316 error_msg(_("netmask doesn't match route address"));
317 return E_OPTERR;
Eric Andersenec455952001-02-14 08:11:27 +0000318 }
319 }
320 /* Fill out netmask if still unset */
321 if ((action == RTACTION_ADD) && rt.rt_flags & RTF_HOST)
322 mask_in_addr(rt) = 0xffffffff;
323
324 /* Create a socket to the INET kernel. */
325 if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
326 perror("socket");
Mark Whitley99806ad2001-02-15 23:00:48 +0000327 return E_SOCK;
Eric Andersenec455952001-02-14 08:11:27 +0000328 }
329 /* Tell the kernel to accept this route. */
330 if (action == RTACTION_DEL) {
331 if (ioctl(skfd, SIOCDELRT, &rt) < 0) {
332 perror("SIOCDELRT");
333 close(skfd);
Mark Whitley99806ad2001-02-15 23:00:48 +0000334 return E_SOCK;
Eric Andersenec455952001-02-14 08:11:27 +0000335 }
336 } else {
337 if (ioctl(skfd, SIOCADDRT, &rt) < 0) {
338 perror("SIOCADDRT");
339 close(skfd);
Mark Whitley99806ad2001-02-15 23:00:48 +0000340 return E_SOCK;
Eric Andersenec455952001-02-14 08:11:27 +0000341 }
342 }
343
344 /* Close the socket. */
345 (void) close(skfd);
Mark Whitley99806ad2001-02-15 23:00:48 +0000346 return EXIT_SUCCESS;
Eric Andersenec455952001-02-14 08:11:27 +0000347}
348
Eric Andersen3e6ff902001-03-09 21:24:12 +0000349static void displayroutes(void)
Eric Andersen68be2ab2001-02-14 19:26:39 +0000350{
351 char buff[256];
352 int nl = 0 ;
353 struct in_addr dest;
354 struct in_addr gw;
355 struct in_addr mask;
356 int flgs, ref, use, metric;
357 char flags[4];
358 unsigned long int d,g,m;
359
360 char sdest[16], sgw[16];
361
362
Eric Andersenf1bbb222001-02-18 20:12:25 +0000363 FILE *fp = xfopen("/proc/net/route", "r");
Eric Andersen68be2ab2001-02-14 19:26:39 +0000364
Eric Andersen68be2ab2001-02-14 19:26:39 +0000365 while( fgets(buff, sizeof(buff), fp) != NULL ) {
366 if(nl) {
367 int ifl = 0;
368 while(buff[ifl]!=' ' && buff[ifl]!='\t' && buff[ifl]!='\0')
369 ifl++;
370 buff[ifl]=0; /* interface */
371 if(sscanf(buff+ifl+1, "%lx%lx%d%d%d%d%lx",
372 &d, &g, &flgs, &ref, &use, &metric, &m)!=7) {
373 error_msg_and_die( "Unsuported kernel route format\n");
374 }
Mark Whitley825ae5a2001-02-15 23:31:40 +0000375 if(nl==1) {
376 printf("Kernel IP routing table\n"
377"Destination Gateway Genmask Flags Metric Ref Use Iface\n");
378 }
379
Eric Andersen68be2ab2001-02-14 19:26:39 +0000380
381 ifl = 0; /* parse flags */
382 if(flgs&1)
383 flags[ifl++]='U';
384 if(flgs&2)
385 flags[ifl++]='G';
386 if(flgs&4)
387 flags[ifl++]='H';
388 flags[ifl]=0;
Eric Andersenf1bbb222001-02-18 20:12:25 +0000389 dest.s_addr = d;
390 gw.s_addr = g;
391 mask.s_addr = m;
Eric Andersen68be2ab2001-02-14 19:26:39 +0000392 strcpy(sdest, (dest.s_addr==0 ? "default" :
393 inet_ntoa(dest)));
394 strcpy(sgw, (gw.s_addr==0 ? "*" :
395 inet_ntoa(gw)));
Eric Andersen26d53eb2001-03-07 06:33:01 +0000396 printf("%-16s%-16s%-16s%-6s%-6d %-2d %7d %s\n",
Eric Andersen68be2ab2001-02-14 19:26:39 +0000397 sdest, sgw,
398 inet_ntoa(mask),
399 flags, metric, ref, use, buff);
400 }
401 nl++;
402 }
403}
404
Eric Andersenec455952001-02-14 08:11:27 +0000405int route_main(int argc, char **argv)
406{
407 int what = 0;
408
409 argc--;
410 argv++;
411
412 if (*argv == NULL) {
Eric Andersen68be2ab2001-02-14 19:26:39 +0000413 displayroutes();
Eric Andersen26d53eb2001-03-07 06:33:01 +0000414 return EXIT_SUCCESS;
Eric Andersenec455952001-02-14 08:11:27 +0000415 } else {
416 /* check verb */
Mark Whitley99806ad2001-02-15 23:00:48 +0000417 if (strcmp(*argv, "add")==0)
Eric Andersenec455952001-02-14 08:11:27 +0000418 what = RTACTION_ADD;
Mark Whitley99806ad2001-02-15 23:00:48 +0000419 else if (strcmp(*argv, "del")==0 || strcmp(*argv, "delete")==0)
Eric Andersenec455952001-02-14 08:11:27 +0000420 what = RTACTION_DEL;
Mark Whitley99806ad2001-02-15 23:00:48 +0000421 else if (strcmp(*argv, "flush")==0)
Eric Andersenec455952001-02-14 08:11:27 +0000422 what = RTACTION_FLUSH;
423 else
Eric Andersen67991cf2001-02-14 21:23:06 +0000424 show_usage();
Eric Andersenec455952001-02-14 08:11:27 +0000425 }
426
Mark Whitley99806ad2001-02-15 23:00:48 +0000427 return INET_setroute(what, 0, ++argv);
Eric Andersenec455952001-02-14 08:11:27 +0000428}