Teach libc5 what a sighandler_t is
[oweals/busybox.git] / route.c
1 /* 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  *
18  * $Id: route.c,v 1.10 2001/03/21 07:34:26 andersen Exp $
19  *
20  * displayroute() code added by Vladimir N. Oleynik <dzo@simtreas.ru>
21  * adjustments by Larry Doolittle  <LRDoolittle@lbl.gov>
22  */
23
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>
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>
39 #include "busybox.h"
40
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
60 static int
61 INET_resolve(char *name, struct sockaddr *sa)
62 {
63         struct sockaddr_in *s_in = (struct sockaddr_in *)sa;
64         
65         s_in->sin_family = AF_INET;
66         s_in->sin_port = 0;
67
68         /* Default is special, meaning 0.0.0.0. */
69         if (strcmp(name, "default")==0) {
70                 s_in->sin_addr.s_addr = INADDR_ANY;
71                 return 1;
72         }
73         /* Look to see if it's a dotted quad. */
74         if (inet_aton(name, &s_in->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
101 static int
102 INET_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
111         if (strcmp(*args, "-net")==0) {
112                 xflag = 1;
113                 args++;
114         } else if (strcmp(*args, "-host")==0) {
115                 xflag = 2;
116                 args++;
117         }
118         if (*args == NULL)
119                 show_usage();
120
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) {
128                 error_msg(_("can't resolve %s"), target);
129                 return EXIT_FAILURE;   /* XXX change to E_something */
130         }
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) {
151                 if (strcmp(*args, "metric")==0) {
152                         int metric;
153                         
154                         args++;
155                         if (!*args || !isdigit(**args))
156                                 show_usage();
157                         metric = atoi(*args);
158 #if HAVE_NEW_ADDRT
159                         rt.rt_metric = metric + 1;
160 #else
161                         ENOSUPP("inet_setroute", "NEW_ADDRT (metric)");  /* XXX Fixme */
162 #endif
163                         args++;
164                         continue;
165                 }
166
167                 if (strcmp(*args, "netmask")==0) {
168                         struct sockaddr mask;
169                         
170                         args++;
171                         if (!*args || mask_in_addr(rt))
172                                 show_usage();
173                         safe_strncpy(netmask, *args, (sizeof netmask));
174                         if ((isnet = INET_resolve(netmask, &mask)) < 0) {
175                                 error_msg(_("can't resolve netmask %s"), netmask);
176                                 return E_LOOKUP;
177                         }
178                         rt.rt_genmask = full_mask(mask);
179                         args++;
180                         continue;
181                 }
182
183                 if (strcmp(*args, "gw")==0 || strcmp(*args, "gateway")==0) {
184                         args++;
185                         if (!*args)
186                                 show_usage();
187                         if (rt.rt_flags & RTF_GATEWAY)
188                                 show_usage();
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);
192                                 return E_LOOKUP;
193                         }
194                         if (isnet) {
195                                 error_msg(
196                                         _("%s: cannot use a NETWORK as gateway!"),
197                                         gateway);
198                                 return E_OPTERR;
199                         }
200                         rt.rt_flags |= RTF_GATEWAY;
201                         args++;
202                         continue;
203                 }
204
205                 if (strcmp(*args, "mss")==0) {
206                         args++;
207                         rt.rt_flags |= RTF_MSS;
208                         if (!*args)
209                                 show_usage();
210                         rt.rt_mss = atoi(*args);
211                         args++;
212                         if (rt.rt_mss < 64 || rt.rt_mss > 32768) {
213                                 error_msg(_("Invalid MSS."));
214                                 return E_OPTERR;
215                         }
216                         continue;
217                 }
218
219                 if (strcmp(*args, "window")==0) {
220                         args++;
221                         if (!*args)
222                                 show_usage();
223                         rt.rt_flags |= RTF_WINDOW;
224                         rt.rt_window = atoi(*args);
225                         args++;
226                         if (rt.rt_window < 128) {
227                                 error_msg(_("Invalid window."));
228                                 return E_OPTERR;
229                         }
230                         continue;
231                 }
232
233                 if (strcmp(*args, "irtt")==0) {
234                         args++;
235                         if (!*args)
236                                 show_usage();
237                         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)) {
244                                 error_msg(_("Invalid initial rtt."));
245                                 return E_OPTERR;
246                         }
247 #endif
248 #else
249                         ENOSUPP("inet_setroute", "RTF_IRTT"); /* XXX Fixme */
250 #endif
251                         continue;
252                 }
253
254                 if (strcmp(*args, "reject")==0) {
255                         args++;
256 #if HAVE_RTF_REJECT
257                         rt.rt_flags |= RTF_REJECT;
258 #else
259                         ENOSUPP("inet_setroute", "RTF_REJECT"); /* XXX Fixme */
260 #endif
261                         continue;
262                 }
263                 if (strcmp(*args, "mod")==0) {
264                         args++;
265                         rt.rt_flags |= RTF_MODIFIED;
266                         continue;
267                 }
268                 if (strcmp(*args, "dyn")==0) {
269                         args++;
270                         rt.rt_flags |= RTF_DYNAMIC;
271                         continue;
272                 }
273                 if (strcmp(*args, "reinstate")==0) {
274                         args++;
275                         rt.rt_flags |= RTF_REINSTATE;
276                         continue;
277                 }
278                 if (strcmp(*args, "device")==0 || strcmp(*args, "dev")==0) {
279                         args++;
280                         if (rt.rt_dev || *args == NULL)
281                                 show_usage();
282                         rt.rt_dev = *args++;
283                         continue;
284                 }
285                 /* nothing matches */
286                 if (!rt.rt_dev) {
287                         rt.rt_dev = *args++;
288                         if (*args)
289                                 show_usage();   /* must be last to catch typos */
290                 } else {
291                         show_usage();
292                 }
293         }
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) {
305                         error_msg(
306                                 _("netmask %.8x doesn't make sense with host route"),
307                                 (unsigned int)mask);
308                         return E_OPTERR;
309                 }
310                 if (mask & (mask + 1)) {
311                         error_msg(_("bogus netmask %s"), netmask);
312                         return E_OPTERR;
313                 }
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"));
317                         return E_OPTERR;
318                 }
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");
327                 return E_SOCK;
328         }
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);
334                         return E_SOCK;
335                 }
336         } else {
337                 if (ioctl(skfd, SIOCADDRT, &rt) < 0) {
338                         perror("SIOCADDRT");
339                         close(skfd);
340                         return E_SOCK;
341                 }
342         }
343         
344         /* Close the socket. */
345         (void) close(skfd);
346         return EXIT_SUCCESS;
347 }
348
349 static void displayroutes(void)
350 {
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
363         FILE *fp = xfopen("/proc/net/route", "r");
364
365         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                         }
375                         if(nl==1) {
376                 printf("Kernel IP routing table\n"
377 "Destination     Gateway         Genmask         Flags Metric Ref    Use Iface\n");
378                         }
379
380
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;
389                         dest.s_addr = d;
390                         gw.s_addr   = g;
391                         mask.s_addr = m;
392                         strcpy(sdest,  (dest.s_addr==0 ? "default" :
393                                         inet_ntoa(dest)));
394                         strcpy(sgw,    (gw.s_addr==0   ? "*"       :
395                                         inet_ntoa(gw)));
396                         printf("%-16s%-16s%-16s%-6s%-6d %-2d %7d %s\n",
397                                 sdest, sgw,
398                                 inet_ntoa(mask),
399                                 flags, metric, ref, use, buff);
400                 }
401         nl++;
402         }
403 }
404
405 int route_main(int argc, char **argv)
406 {
407         int what = 0;
408
409         argc--;
410         argv++;
411
412         if (*argv == NULL) {
413                 displayroutes();
414                 return EXIT_SUCCESS;
415         } else {
416                 /* check verb */
417                 if (strcmp(*argv, "add")==0)
418                         what = RTACTION_ADD;
419                 else if (strcmp(*argv, "del")==0 || strcmp(*argv, "delete")==0)
420                         what = RTACTION_DEL;
421                 else if (strcmp(*argv, "flush")==0)
422                         what = RTACTION_FLUSH;
423                 else
424                         show_usage();
425         }
426
427         return INET_setroute(what, 0, ++argv);
428 }