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