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.10 2001/03/21 07:34:26 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";
111 if (strcmp(*args, "-net")==0) {
114 } else if (strcmp(*args, "-host")==0) {
121 safe_strncpy(target, *args++, (sizeof target));
123 /* Clean out the RTREQ structure. */
124 memset((char *) &rt, 0, sizeof(struct rtentry));
127 if ((isnet = INET_resolve(target, &rt.rt_dst)) < 0) {
128 error_msg(_("can't resolve %s"), target);
129 return EXIT_FAILURE; /* XXX change to E_something */
145 /* Fill in the other fields. */
146 rt.rt_flags = (RTF_UP | RTF_HOST);
148 rt.rt_flags &= ~RTF_HOST;
151 if (strcmp(*args, "metric")==0) {
155 if (!*args || !isdigit(**args))
157 metric = atoi(*args);
159 rt.rt_metric = metric + 1;
161 ENOSUPP("inet_setroute", "NEW_ADDRT (metric)"); /* XXX Fixme */
167 if (strcmp(*args, "netmask")==0) {
168 struct sockaddr mask;
171 if (!*args || mask_in_addr(rt))
173 safe_strncpy(netmask, *args, (sizeof netmask));
174 if ((isnet = INET_resolve(netmask, &mask)) < 0) {
175 error_msg(_("can't resolve netmask %s"), netmask);
178 rt.rt_genmask = full_mask(mask);
183 if (strcmp(*args, "gw")==0 || strcmp(*args, "gateway")==0) {
187 if (rt.rt_flags & RTF_GATEWAY)
189 safe_strncpy(gateway, *args, (sizeof gateway));
190 if ((isnet = INET_resolve(gateway, &rt.rt_gateway)) < 0) {
191 error_msg(_("can't resolve gw %s"), gateway);
196 _("%s: cannot use a NETWORK as gateway!"),
200 rt.rt_flags |= RTF_GATEWAY;
205 if (strcmp(*args, "mss")==0) {
207 rt.rt_flags |= RTF_MSS;
210 rt.rt_mss = atoi(*args);
212 if (rt.rt_mss < 64 || rt.rt_mss > 32768) {
213 error_msg(_("Invalid MSS."));
219 if (strcmp(*args, "window")==0) {
223 rt.rt_flags |= RTF_WINDOW;
224 rt.rt_window = atoi(*args);
226 if (rt.rt_window < 128) {
227 error_msg(_("Invalid window."));
233 if (strcmp(*args, "irtt")==0) {
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)) {
244 error_msg(_("Invalid initial rtt."));
249 ENOSUPP("inet_setroute", "RTF_IRTT"); /* XXX Fixme */
254 if (strcmp(*args, "reject")==0) {
257 rt.rt_flags |= RTF_REJECT;
259 ENOSUPP("inet_setroute", "RTF_REJECT"); /* XXX Fixme */
263 if (strcmp(*args, "mod")==0) {
265 rt.rt_flags |= RTF_MODIFIED;
268 if (strcmp(*args, "dyn")==0) {
270 rt.rt_flags |= RTF_DYNAMIC;
273 if (strcmp(*args, "reinstate")==0) {
275 rt.rt_flags |= RTF_REINSTATE;
278 if (strcmp(*args, "device")==0 || strcmp(*args, "dev")==0) {
280 if (rt.rt_dev || *args == NULL)
285 /* nothing matches */
289 show_usage(); /* must be last to catch typos */
296 if ((rt.rt_flags & RTF_REJECT) && !rt.rt_dev)
300 /* sanity checks.. */
301 if (mask_in_addr(rt)) {
302 unsigned long mask = mask_in_addr(rt);
304 if ((rt.rt_flags & RTF_HOST) && mask != 0xffffffff) {
306 _("netmask %.8x doesn't make sense with host route"),
310 if (mask & (mask + 1)) {
311 error_msg(_("bogus netmask %s"), netmask);
314 mask = ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr;
315 if (mask & ~mask_in_addr(rt)) {
316 error_msg(_("netmask doesn't match route address"));
320 /* Fill out netmask if still unset */
321 if ((action == RTACTION_ADD) && rt.rt_flags & RTF_HOST)
322 mask_in_addr(rt) = 0xffffffff;
324 /* Create a socket to the INET kernel. */
325 if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
329 /* Tell the kernel to accept this route. */
330 if (action == RTACTION_DEL) {
331 if (ioctl(skfd, SIOCDELRT, &rt) < 0) {
337 if (ioctl(skfd, SIOCADDRT, &rt) < 0) {
344 /* Close the socket. */
349 static void displayroutes(void)
356 int flgs, ref, use, metric;
358 unsigned long int d,g,m;
360 char sdest[16], sgw[16];
363 FILE *fp = xfopen("/proc/net/route", "r");
365 while( fgets(buff, sizeof(buff), fp) != NULL ) {
368 while(buff[ifl]!=' ' && buff[ifl]!='\t' && buff[ifl]!='\0')
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");
376 printf("Kernel IP routing table\n"
377 "Destination Gateway Genmask Flags Metric Ref Use Iface\n");
381 ifl = 0; /* parse flags */
392 strcpy(sdest, (dest.s_addr==0 ? "default" :
394 strcpy(sgw, (gw.s_addr==0 ? "*" :
396 printf("%-16s%-16s%-16s%-6s%-6d %-2d %7d %s\n",
399 flags, metric, ref, use, buff);
405 int route_main(int argc, char **argv)
417 if (strcmp(*argv, "add")==0)
419 else if (strcmp(*argv, "del")==0 || strcmp(*argv, "delete")==0)
421 else if (strcmp(*argv, "flush")==0)
422 what = RTACTION_FLUSH;
427 return INET_setroute(what, 0, ++argv);