3 * Similar to the standard Unix route, but with only the necessary
6 * Bjorn Wesen, Axis Communications AB
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')
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.
18 * $Id: route.c,v 1.12 2001/08/27 17:57:27 andersen Exp $
20 * displayroute() code added by Vladimir N. Oleynik <dzo@simtreas.ru>
21 * adjustments by Larry Doolittle <LRDoolittle@lbl.gov>
24 #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>
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
58 /* resolve XXX.YYY.ZZZ.QQQ -> binary */
61 INET_resolve(char *name, struct sockaddr *sa)
63 struct sockaddr_in *s_in = (struct sockaddr_in *)sa;
65 s_in->sin_family = AF_INET;
68 /* Default is special, meaning 0.0.0.0. */
69 if (strcmp(name, "default")==0) {
70 s_in->sin_addr.s_addr = INADDR_ANY;
73 /* Look to see if it's a dotted quad. */
74 if (inet_aton(name, &s_in->sin_addr)) {
81 #if defined (SIOCADDRTOLD) || defined (RTF_IRTT) /* route */
82 #define HAVE_NEW_ADDRT 1
84 #ifdef RTF_IRTT /* route */
85 #define HAVE_RTF_IRTT 1
87 #ifdef RTF_REJECT /* route */
88 #define HAVE_RTF_REJECT 1
92 #define mask_in_addr(x) (((struct sockaddr_in *)&((x).rt_genmask))->sin_addr.s_addr)
93 #define full_mask(x) (x)
95 #define mask_in_addr(x) ((x).rt_genmask)
96 #define full_mask(x) (((struct sockaddr_in *)&(x))->sin_addr.s_addr)
99 /* add or delete a route depending on action */
102 INET_setroute(int action, int options, char **args)
105 char target[128], gateway[128] = "NONE", netmask[128] = "default";
113 if (strcmp(*args, "-net")==0) {
116 } else if (strcmp(*args, "-host")==0) {
120 safe_strncpy(target, *args++, (sizeof target));
122 /* Clean out the RTREQ structure. */
123 memset((char *) &rt, 0, sizeof(struct rtentry));
126 if ((isnet = INET_resolve(target, &rt.rt_dst)) < 0) {
127 error_msg(_("can't resolve %s"), target);
128 return EXIT_FAILURE; /* XXX change to E_something */
144 /* Fill in the other fields. */
145 rt.rt_flags = (RTF_UP | RTF_HOST);
147 rt.rt_flags &= ~RTF_HOST;
150 if (strcmp(*args, "metric")==0) {
154 if (!*args || !isdigit(**args))
156 metric = atoi(*args);
158 rt.rt_metric = metric + 1;
160 ENOSUPP("inet_setroute", "NEW_ADDRT (metric)"); /* XXX Fixme */
166 if (strcmp(*args, "netmask")==0) {
167 struct sockaddr mask;
170 if (!*args || mask_in_addr(rt))
172 safe_strncpy(netmask, *args, (sizeof netmask));
173 if ((isnet = INET_resolve(netmask, &mask)) < 0) {
174 error_msg(_("can't resolve netmask %s"), netmask);
177 rt.rt_genmask = full_mask(mask);
182 if (strcmp(*args, "gw")==0 || strcmp(*args, "gateway")==0) {
186 if (rt.rt_flags & RTF_GATEWAY)
188 safe_strncpy(gateway, *args, (sizeof gateway));
189 if ((isnet = INET_resolve(gateway, &rt.rt_gateway)) < 0) {
190 error_msg(_("can't resolve gw %s"), gateway);
195 _("%s: cannot use a NETWORK as gateway!"),
199 rt.rt_flags |= RTF_GATEWAY;
204 if (strcmp(*args, "mss")==0) {
206 rt.rt_flags |= RTF_MSS;
209 rt.rt_mss = atoi(*args);
211 if (rt.rt_mss < 64 || rt.rt_mss > 32768) {
212 error_msg(_("Invalid MSS."));
218 if (strcmp(*args, "window")==0) {
222 rt.rt_flags |= RTF_WINDOW;
223 rt.rt_window = atoi(*args);
225 if (rt.rt_window < 128) {
226 error_msg(_("Invalid window."));
232 if (strcmp(*args, "irtt")==0) {
238 rt.rt_flags |= RTF_IRTT;
239 rt.rt_irtt = atoi(*(args - 1));
240 rt.rt_irtt *= (HZ / 100); /* FIXME */
241 #if 0 /* FIXME: do we need to check anything of this? */
242 if (rt.rt_irtt < 1 || rt.rt_irtt > (120 * HZ)) {
243 error_msg(_("Invalid initial rtt."));
248 ENOSUPP("inet_setroute", "RTF_IRTT"); /* XXX Fixme */
253 if (strcmp(*args, "reject")==0) {
256 rt.rt_flags |= RTF_REJECT;
258 ENOSUPP("inet_setroute", "RTF_REJECT"); /* XXX Fixme */
262 if (strcmp(*args, "mod")==0) {
264 rt.rt_flags |= RTF_MODIFIED;
267 if (strcmp(*args, "dyn")==0) {
269 rt.rt_flags |= RTF_DYNAMIC;
272 if (strcmp(*args, "reinstate")==0) {
274 rt.rt_flags |= RTF_REINSTATE;
277 if (strcmp(*args, "device")==0 || strcmp(*args, "dev")==0) {
279 if (rt.rt_dev || *args == NULL)
284 /* nothing matches */
288 show_usage(); /* must be last to catch typos */
295 if ((rt.rt_flags & RTF_REJECT) && !rt.rt_dev)
299 /* sanity checks.. */
300 if (mask_in_addr(rt)) {
301 unsigned long mask = mask_in_addr(rt);
303 if ((rt.rt_flags & RTF_HOST) && mask != 0xffffffff) {
305 _("netmask %.8x doesn't make sense with host route"),
309 if (mask & (mask + 1)) {
310 error_msg(_("bogus netmask %s"), netmask);
313 mask = ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr;
314 if (mask & ~mask_in_addr(rt)) {
315 error_msg(_("netmask doesn't match route address"));
319 /* Fill out netmask if still unset */
320 if ((action == RTACTION_ADD) && rt.rt_flags & RTF_HOST)
321 mask_in_addr(rt) = 0xffffffff;
323 /* Create a socket to the INET kernel. */
324 if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
328 /* Tell the kernel to accept this route. */
329 if (action == RTACTION_DEL) {
330 if (ioctl(skfd, SIOCDELRT, &rt) < 0) {
336 if (ioctl(skfd, SIOCADDRT, &rt) < 0) {
343 /* Close the socket. */
349 /* Keep this in sync with /usr/src/linux/include/linux/route.h */
350 #define RTF_UP 0x0001 /* route usable */
351 #define RTF_GATEWAY 0x0002 /* destination is a gateway */
352 #define RTF_HOST 0x0004 /* host entry (net otherwise) */
353 #define RTF_REINSTATE 0x0008 /* reinstate route after tmout */
354 #define RTF_DYNAMIC 0x0010 /* created dyn. (by redirect) */
355 #define RTF_MODIFIED 0x0020 /* modified dyn. (by redirect) */
356 #define RTF_MTU 0x0040 /* specific MTU for this route */
358 #define RTF_MSS RTF_MTU /* Compatibility :-( */
360 #define RTF_WINDOW 0x0080 /* per route window clamping */
361 #define RTF_IRTT 0x0100 /* Initial round trip time */
362 #define RTF_REJECT 0x0200 /* Reject route */
365 static void displayroutes(void)
372 int flgs, ref, use, metric;
374 unsigned long int d,g,m;
376 char sdest[16], sgw[16];
379 FILE *fp = xfopen("/proc/net/route", "r");
381 while( fgets(buff, sizeof(buff), fp) != NULL ) {
384 while(buff[ifl]!=' ' && buff[ifl]!='\t' && buff[ifl]!='\0')
386 buff[ifl]=0; /* interface */
387 if(sscanf(buff+ifl+1, "%lx%lx%d%d%d%d%lx",
388 &d, &g, &flgs, &ref, &use, &metric, &m)!=7) {
389 error_msg_and_die( "Unsuported kernel route format\n");
392 printf("Kernel IP routing table\n"
393 "Destination Gateway Genmask Flags Metric Ref Use Iface\n");
395 ifl = 0; /* parse flags */
402 if(flgs&RTF_REINSTATE)
406 if(flgs&RTF_MODIFIED)
414 strcpy(sdest, (dest.s_addr==0 ? "default" :
416 strcpy(sgw, (gw.s_addr==0 ? "*" :
418 printf("%-16s%-16s%-16s%-6s%-6d %-2d %7d %s\n",
421 flags, metric, ref, use, buff);
427 int route_main(int argc, char **argv)
439 if (strcmp(*argv, "add")==0)
441 else if (strcmp(*argv, "del")==0 || strcmp(*argv, "delete")==0)
443 else if (strcmp(*argv, "flush")==0)
444 what = RTACTION_FLUSH;
449 return INET_setroute(what, 0, ++argv);