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 | * |
Eric Andersen | b940850 | 2001-09-05 19:32:00 +0000 | [diff] [blame] | 8 | * Author of the original route: |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 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 | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 18 | * $Id: route.c,v 1.14 2001/11/10 11:22:43 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> |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 26 | #include "inet_common.h" |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 27 | #include <net/route.h> |
| 28 | #include <linux/param.h> // HZ |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 29 | #include <stdio.h> |
| 30 | #include <errno.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include <getopt.h> |
| 35 | #include <unistd.h> |
| 36 | #include <ctype.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 37 | #include "busybox.h" |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 38 | |
| 39 | #define _(x) x |
| 40 | |
| 41 | #define RTACTION_ADD 1 |
| 42 | #define RTACTION_DEL 2 |
| 43 | #define RTACTION_HELP 3 |
| 44 | #define RTACTION_FLUSH 4 |
| 45 | #define RTACTION_SHOW 5 |
| 46 | |
| 47 | #define E_NOTFOUND 8 |
| 48 | #define E_SOCK 7 |
| 49 | #define E_LOOKUP 6 |
| 50 | #define E_VERSION 5 |
| 51 | #define E_USAGE 4 |
| 52 | #define E_OPTERR 3 |
| 53 | #define E_INTERN 2 |
| 54 | #define E_NOSUPP 1 |
| 55 | |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 56 | #if defined (SIOCADDRTOLD) || defined (RTF_IRTT) /* route */ |
| 57 | #define HAVE_NEW_ADDRT 1 |
| 58 | #endif |
| 59 | #ifdef RTF_IRTT /* route */ |
| 60 | #define HAVE_RTF_IRTT 1 |
| 61 | #endif |
| 62 | #ifdef RTF_REJECT /* route */ |
| 63 | #define HAVE_RTF_REJECT 1 |
| 64 | #endif |
| 65 | |
| 66 | #if HAVE_NEW_ADDRT |
| 67 | #define mask_in_addr(x) (((struct sockaddr_in *)&((x).rt_genmask))->sin_addr.s_addr) |
| 68 | #define full_mask(x) (x) |
| 69 | #else |
| 70 | #define mask_in_addr(x) ((x).rt_genmask) |
| 71 | #define full_mask(x) (((struct sockaddr_in *)&(x))->sin_addr.s_addr) |
| 72 | #endif |
| 73 | |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 74 | |
| 75 | |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 76 | /* add or delete a route depending on action */ |
| 77 | |
| 78 | static int |
| 79 | INET_setroute(int action, int options, char **args) |
| 80 | { |
| 81 | struct rtentry rt; |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 82 | char target[128], gateway[128] = "NONE"; |
| 83 | const char *netmask = bb_INET_default; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 84 | int xflag, isnet; |
| 85 | int skfd; |
| 86 | |
| 87 | xflag = 0; |
| 88 | |
Eric Andersen | a3c8481 | 2001-08-23 22:05:33 +0000 | [diff] [blame] | 89 | if (*args == NULL) |
| 90 | show_usage(); |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 91 | if (strcmp(*args, "-net")==0) { |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 92 | xflag = 1; |
| 93 | args++; |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 94 | } else if (strcmp(*args, "-host")==0) { |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 95 | xflag = 2; |
| 96 | args++; |
| 97 | } |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 98 | if (*args == NULL) |
| 99 | show_usage(); |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 100 | safe_strncpy(target, *args++, (sizeof target)); |
| 101 | |
| 102 | /* Clean out the RTREQ structure. */ |
| 103 | memset((char *) &rt, 0, sizeof(struct rtentry)); |
| 104 | |
| 105 | |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 106 | if ((isnet = INET_resolve(target, (struct sockaddr_in *)&rt.rt_dst, xflag!=1)) < 0) { |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 107 | error_msg(_("can't resolve %s"), target); |
| 108 | return EXIT_FAILURE; /* XXX change to E_something */ |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | switch (xflag) { |
| 112 | case 1: |
| 113 | isnet = 1; |
| 114 | break; |
Eric Andersen | b940850 | 2001-09-05 19:32:00 +0000 | [diff] [blame] | 115 | |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 116 | case 2: |
| 117 | isnet = 0; |
| 118 | break; |
Eric Andersen | b940850 | 2001-09-05 19:32:00 +0000 | [diff] [blame] | 119 | |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 120 | default: |
| 121 | break; |
| 122 | } |
Eric Andersen | b940850 | 2001-09-05 19:32:00 +0000 | [diff] [blame] | 123 | |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 124 | /* Fill in the other fields. */ |
| 125 | rt.rt_flags = (RTF_UP | RTF_HOST); |
| 126 | if (isnet) |
| 127 | rt.rt_flags &= ~RTF_HOST; |
| 128 | |
| 129 | while (*args) { |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 130 | if (strcmp(*args, "metric")==0) { |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 131 | int metric; |
Eric Andersen | b940850 | 2001-09-05 19:32:00 +0000 | [diff] [blame] | 132 | |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 133 | args++; |
| 134 | if (!*args || !isdigit(**args)) |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 135 | show_usage(); |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 136 | metric = atoi(*args); |
| 137 | #if HAVE_NEW_ADDRT |
| 138 | rt.rt_metric = metric + 1; |
| 139 | #else |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 140 | ENOSUPP("inet_setroute", "NEW_ADDRT (metric)"); /* XXX Fixme */ |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 141 | #endif |
| 142 | args++; |
| 143 | continue; |
| 144 | } |
| 145 | |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 146 | if (strcmp(*args, "netmask")==0) { |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 147 | struct sockaddr mask; |
Eric Andersen | b940850 | 2001-09-05 19:32:00 +0000 | [diff] [blame] | 148 | |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 149 | args++; |
| 150 | if (!*args || mask_in_addr(rt)) |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 151 | show_usage(); |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 152 | netmask = *args; |
| 153 | if ((isnet = INET_resolve(netmask, (struct sockaddr_in *)&mask, 0)) < 0) { |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 154 | error_msg(_("can't resolve netmask %s"), netmask); |
| 155 | return E_LOOKUP; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 156 | } |
| 157 | rt.rt_genmask = full_mask(mask); |
| 158 | args++; |
| 159 | continue; |
| 160 | } |
| 161 | |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 162 | if (strcmp(*args, "gw")==0 || strcmp(*args, "gateway")==0) { |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 163 | args++; |
| 164 | if (!*args) |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 165 | show_usage(); |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 166 | if (rt.rt_flags & RTF_GATEWAY) |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 167 | show_usage(); |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 168 | safe_strncpy(gateway, *args, (sizeof gateway)); |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 169 | if ((isnet = INET_resolve(gateway, (struct sockaddr_in *)&rt.rt_gateway, 1)) < 0) { |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 170 | error_msg(_("can't resolve gw %s"), gateway); |
| 171 | return E_LOOKUP; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 172 | } |
| 173 | if (isnet) { |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 174 | error_msg( |
| 175 | _("%s: cannot use a NETWORK as gateway!"), |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 176 | gateway); |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 177 | return E_OPTERR; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 178 | } |
| 179 | rt.rt_flags |= RTF_GATEWAY; |
| 180 | args++; |
| 181 | continue; |
| 182 | } |
| 183 | |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 184 | if (strcmp(*args, "mss")==0) { |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 185 | args++; |
| 186 | rt.rt_flags |= RTF_MSS; |
| 187 | if (!*args) |
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 | rt.rt_mss = atoi(*args); |
| 190 | args++; |
| 191 | if (rt.rt_mss < 64 || rt.rt_mss > 32768) { |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 192 | error_msg(_("Invalid MSS.")); |
| 193 | return E_OPTERR; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 194 | } |
| 195 | continue; |
| 196 | } |
| 197 | |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 198 | if (strcmp(*args, "window")==0) { |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 199 | args++; |
| 200 | if (!*args) |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 201 | show_usage(); |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 202 | rt.rt_flags |= RTF_WINDOW; |
| 203 | rt.rt_window = atoi(*args); |
| 204 | args++; |
| 205 | if (rt.rt_window < 128) { |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 206 | error_msg(_("Invalid window.")); |
| 207 | return E_OPTERR; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 208 | } |
| 209 | continue; |
| 210 | } |
| 211 | |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 212 | if (strcmp(*args, "irtt")==0) { |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 213 | args++; |
| 214 | if (!*args) |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 215 | show_usage(); |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 216 | args++; |
| 217 | #if HAVE_RTF_IRTT |
| 218 | rt.rt_flags |= RTF_IRTT; |
| 219 | rt.rt_irtt = atoi(*(args - 1)); |
Eric Andersen | b940850 | 2001-09-05 19:32:00 +0000 | [diff] [blame] | 220 | rt.rt_irtt *= (HZ / 100); /* FIXME */ |
| 221 | #if 0 /* FIXME: do we need to check anything of this? */ |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 222 | if (rt.rt_irtt < 1 || rt.rt_irtt > (120 * HZ)) { |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 223 | error_msg(_("Invalid initial rtt.")); |
| 224 | return E_OPTERR; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 225 | } |
| 226 | #endif |
| 227 | #else |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 228 | ENOSUPP("inet_setroute", "RTF_IRTT"); /* XXX Fixme */ |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 229 | #endif |
| 230 | continue; |
| 231 | } |
| 232 | |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 233 | if (strcmp(*args, "reject")==0) { |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 234 | args++; |
| 235 | #if HAVE_RTF_REJECT |
| 236 | rt.rt_flags |= RTF_REJECT; |
| 237 | #else |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 238 | ENOSUPP("inet_setroute", "RTF_REJECT"); /* XXX Fixme */ |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 239 | #endif |
| 240 | continue; |
| 241 | } |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 242 | if (strcmp(*args, "mod")==0) { |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 243 | args++; |
| 244 | rt.rt_flags |= RTF_MODIFIED; |
| 245 | continue; |
| 246 | } |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 247 | if (strcmp(*args, "dyn")==0) { |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 248 | args++; |
| 249 | rt.rt_flags |= RTF_DYNAMIC; |
| 250 | continue; |
| 251 | } |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 252 | if (strcmp(*args, "reinstate")==0) { |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 253 | args++; |
| 254 | rt.rt_flags |= RTF_REINSTATE; |
| 255 | continue; |
| 256 | } |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 257 | if (strcmp(*args, "device")==0 || strcmp(*args, "dev")==0) { |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 258 | args++; |
| 259 | if (rt.rt_dev || *args == NULL) |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 260 | show_usage(); |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 261 | rt.rt_dev = *args++; |
| 262 | continue; |
| 263 | } |
| 264 | /* nothing matches */ |
| 265 | if (!rt.rt_dev) { |
| 266 | rt.rt_dev = *args++; |
| 267 | if (*args) |
Eric Andersen | b940850 | 2001-09-05 19:32:00 +0000 | [diff] [blame] | 268 | show_usage(); /* must be last to catch typos */ |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 269 | } else { |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 270 | show_usage(); |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 271 | } |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | #if HAVE_RTF_REJECT |
| 275 | if ((rt.rt_flags & RTF_REJECT) && !rt.rt_dev) |
| 276 | rt.rt_dev = "lo"; |
| 277 | #endif |
| 278 | |
| 279 | /* sanity checks.. */ |
| 280 | if (mask_in_addr(rt)) { |
| 281 | unsigned long mask = mask_in_addr(rt); |
| 282 | mask = ~ntohl(mask); |
| 283 | if ((rt.rt_flags & RTF_HOST) && mask != 0xffffffff) { |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 284 | error_msg( |
| 285 | _("netmask %.8x doesn't make sense with host route"), |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 286 | (unsigned int)mask); |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 287 | return E_OPTERR; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 288 | } |
| 289 | if (mask & (mask + 1)) { |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 290 | error_msg(_("bogus netmask %s"), netmask); |
| 291 | return E_OPTERR; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 292 | } |
| 293 | mask = ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr; |
| 294 | if (mask & ~mask_in_addr(rt)) { |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 295 | error_msg(_("netmask doesn't match route address")); |
| 296 | return E_OPTERR; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | /* Fill out netmask if still unset */ |
| 300 | if ((action == RTACTION_ADD) && rt.rt_flags & RTF_HOST) |
| 301 | mask_in_addr(rt) = 0xffffffff; |
| 302 | |
| 303 | /* Create a socket to the INET kernel. */ |
| 304 | if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
| 305 | perror("socket"); |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 306 | return E_SOCK; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 307 | } |
| 308 | /* Tell the kernel to accept this route. */ |
| 309 | if (action == RTACTION_DEL) { |
| 310 | if (ioctl(skfd, SIOCDELRT, &rt) < 0) { |
| 311 | perror("SIOCDELRT"); |
| 312 | close(skfd); |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 313 | return E_SOCK; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 314 | } |
| 315 | } else { |
| 316 | if (ioctl(skfd, SIOCADDRT, &rt) < 0) { |
| 317 | perror("SIOCADDRT"); |
| 318 | close(skfd); |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 319 | return E_SOCK; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 320 | } |
| 321 | } |
Eric Andersen | b940850 | 2001-09-05 19:32:00 +0000 | [diff] [blame] | 322 | |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 323 | /* Close the socket. */ |
| 324 | (void) close(skfd); |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 325 | return EXIT_SUCCESS; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Eric Andersen | 863a3e1 | 2001-08-27 17:57:27 +0000 | [diff] [blame] | 328 | #ifndef RTF_UP |
| 329 | /* Keep this in sync with /usr/src/linux/include/linux/route.h */ |
| 330 | #define RTF_UP 0x0001 /* route usable */ |
| 331 | #define RTF_GATEWAY 0x0002 /* destination is a gateway */ |
| 332 | #define RTF_HOST 0x0004 /* host entry (net otherwise) */ |
| 333 | #define RTF_REINSTATE 0x0008 /* reinstate route after tmout */ |
| 334 | #define RTF_DYNAMIC 0x0010 /* created dyn. (by redirect) */ |
| 335 | #define RTF_MODIFIED 0x0020 /* modified dyn. (by redirect) */ |
| 336 | #define RTF_MTU 0x0040 /* specific MTU for this route */ |
| 337 | #ifndef RTF_MSS |
| 338 | #define RTF_MSS RTF_MTU /* Compatibility :-( */ |
| 339 | #endif |
| 340 | #define RTF_WINDOW 0x0080 /* per route window clamping */ |
| 341 | #define RTF_IRTT 0x0100 /* Initial round trip time */ |
| 342 | #define RTF_REJECT 0x0200 /* Reject route */ |
| 343 | #endif |
| 344 | |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 345 | static void displayroutes(int noresolve) |
Eric Andersen | 68be2ab | 2001-02-14 19:26:39 +0000 | [diff] [blame] | 346 | { |
| 347 | char buff[256]; |
| 348 | int nl = 0 ; |
| 349 | struct in_addr dest; |
| 350 | struct in_addr gw; |
| 351 | struct in_addr mask; |
| 352 | int flgs, ref, use, metric; |
Eric Andersen | b940850 | 2001-09-05 19:32:00 +0000 | [diff] [blame] | 353 | char flags[64]; |
Eric Andersen | 68be2ab | 2001-02-14 19:26:39 +0000 | [diff] [blame] | 354 | unsigned long int d,g,m; |
| 355 | |
| 356 | char sdest[16], sgw[16]; |
| 357 | |
Eric Andersen | f1bbb22 | 2001-02-18 20:12:25 +0000 | [diff] [blame] | 358 | FILE *fp = xfopen("/proc/net/route", "r"); |
Eric Andersen | 68be2ab | 2001-02-14 19:26:39 +0000 | [diff] [blame] | 359 | |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 360 | if(noresolve) |
| 361 | noresolve = 0x0fff; |
| 362 | |
Eric Andersen | 68be2ab | 2001-02-14 19:26:39 +0000 | [diff] [blame] | 363 | while( fgets(buff, sizeof(buff), fp) != NULL ) { |
| 364 | if(nl) { |
| 365 | int ifl = 0; |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 366 | int numeric; |
| 367 | struct sockaddr_in s_addr; |
| 368 | |
Eric Andersen | 68be2ab | 2001-02-14 19:26:39 +0000 | [diff] [blame] | 369 | while(buff[ifl]!=' ' && buff[ifl]!='\t' && buff[ifl]!='\0') |
| 370 | ifl++; |
| 371 | buff[ifl]=0; /* interface */ |
Eric Andersen | b940850 | 2001-09-05 19:32:00 +0000 | [diff] [blame] | 372 | if(sscanf(buff+ifl+1, "%lx%lx%X%d%d%d%lx", |
Eric Andersen | 68be2ab | 2001-02-14 19:26:39 +0000 | [diff] [blame] | 373 | &d, &g, &flgs, &ref, &use, &metric, &m)!=7) { |
| 374 | error_msg_and_die( "Unsuported kernel route format\n"); |
| 375 | } |
Eric Andersen | 863a3e1 | 2001-08-27 17:57:27 +0000 | [diff] [blame] | 376 | if(nl==1) |
Eric Andersen | b940850 | 2001-09-05 19:32:00 +0000 | [diff] [blame] | 377 | printf("Kernel IP routing table\n" |
| 378 | "Destination Gateway Genmask Flags Metric Ref Use Iface\n"); |
Mark Whitley | 825ae5a | 2001-02-15 23:31:40 +0000 | [diff] [blame] | 379 | |
Eric Andersen | 68be2ab | 2001-02-14 19:26:39 +0000 | [diff] [blame] | 380 | ifl = 0; /* parse flags */ |
Eric Andersen | b940850 | 2001-09-05 19:32:00 +0000 | [diff] [blame] | 381 | if(flgs&RTF_UP) { |
| 382 | if(flgs&RTF_REJECT) |
| 383 | flags[ifl++]='!'; |
| 384 | else |
| 385 | flags[ifl++]='U'; |
| 386 | if(flgs&RTF_GATEWAY) |
| 387 | flags[ifl++]='G'; |
| 388 | if(flgs&RTF_HOST) |
| 389 | flags[ifl++]='H'; |
| 390 | if(flgs&RTF_REINSTATE) |
| 391 | flags[ifl++]='R'; |
| 392 | if(flgs&RTF_DYNAMIC) |
| 393 | flags[ifl++]='D'; |
| 394 | if(flgs&RTF_MODIFIED) |
| 395 | flags[ifl++]='M'; |
| 396 | flags[ifl]=0; |
| 397 | dest.s_addr = d; |
| 398 | gw.s_addr = g; |
| 399 | mask.s_addr = m; |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 400 | memset(&s_addr, 0, sizeof(struct sockaddr_in)); |
| 401 | s_addr.sin_family = AF_INET; |
| 402 | s_addr.sin_addr = dest; |
| 403 | numeric = noresolve | 0x8000; /* default instead of * */ |
| 404 | INET_rresolve(sdest, sizeof(sdest), &s_addr, numeric, m); |
| 405 | numeric = noresolve | 0x4000; /* host instead of net */ |
| 406 | s_addr.sin_addr = gw; |
| 407 | INET_rresolve(sgw, sizeof(sgw), &s_addr, numeric, m); |
Eric Andersen | b940850 | 2001-09-05 19:32:00 +0000 | [diff] [blame] | 408 | printf("%-16s%-16s%-16s%-6s%-6d %-2d %7d %s\n", |
| 409 | sdest, sgw, |
| 410 | inet_ntoa(mask), |
| 411 | flags, metric, ref, use, buff); |
| 412 | } |
| 413 | } |
| 414 | nl++; |
| 415 | } |
Eric Andersen | 68be2ab | 2001-02-14 19:26:39 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 418 | int route_main(int argc, char **argv) |
| 419 | { |
| 420 | int what = 0; |
| 421 | |
| 422 | argc--; |
| 423 | argv++; |
| 424 | |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 425 | if (*argv == NULL || (*(argv+1)==NULL && strcmp(*argv, "-n")==0)) { |
| 426 | displayroutes(*argv != NULL); |
Eric Andersen | 26d53eb | 2001-03-07 06:33:01 +0000 | [diff] [blame] | 427 | return EXIT_SUCCESS; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 428 | } else { |
| 429 | /* check verb */ |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 430 | if (strcmp(*argv, "add")==0) |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 431 | what = RTACTION_ADD; |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 432 | else if (strcmp(*argv, "del")==0 || strcmp(*argv, "delete")==0) |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 433 | what = RTACTION_DEL; |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 434 | else if (strcmp(*argv, "flush")==0) |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 435 | what = RTACTION_FLUSH; |
| 436 | else |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 437 | show_usage(); |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 440 | return INET_setroute(what, 0, ++argv); |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 441 | } |