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 |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 4 | * parts for AF_INET and AF_INET6 |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 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 | 0552b9a | 2002-12-11 03:57:12 +0000 | [diff] [blame] | 18 | * $Id: route.c,v 1.20 2002/12/11 03:57:12 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 | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 22 | * |
| 23 | * IPV6 support added by Bart Visscher <magick@linux-fan.com> |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 24 | */ |
| 25 | |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 26 | #include <sys/types.h> |
| 27 | #include <sys/ioctl.h> |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 28 | #include "inet_common.h" |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 29 | #include <net/route.h> |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 30 | #include <net/if.h> |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 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 | |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 58 | #if defined (SIOCADDRTOLD) || defined (RTF_IRTT) /* route */ |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 59 | #define HAVE_NEW_ADDRT 1 |
| 60 | #endif |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 61 | #ifdef RTF_IRTT /* route */ |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 62 | #define HAVE_RTF_IRTT 1 |
| 63 | #endif |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 64 | #ifdef RTF_REJECT /* route */ |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 65 | #define HAVE_RTF_REJECT 1 |
| 66 | #endif |
| 67 | |
| 68 | #if HAVE_NEW_ADDRT |
| 69 | #define mask_in_addr(x) (((struct sockaddr_in *)&((x).rt_genmask))->sin_addr.s_addr) |
| 70 | #define full_mask(x) (x) |
| 71 | #else |
| 72 | #define mask_in_addr(x) ((x).rt_genmask) |
| 73 | #define full_mask(x) (((struct sockaddr_in *)&(x))->sin_addr.s_addr) |
| 74 | #endif |
| 75 | |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 76 | |
| 77 | |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 78 | /* add or delete a route depending on action */ |
| 79 | |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 80 | static int INET_setroute(int action, int options, char **args) |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 81 | { |
| 82 | struct rtentry rt; |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 83 | char target[128], gateway[128] = "NONE"; |
| 84 | const char *netmask = bb_INET_default; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 85 | int xflag, isnet; |
| 86 | int skfd; |
| 87 | |
| 88 | xflag = 0; |
| 89 | |
Eric Andersen | a3c8481 | 2001-08-23 22:05:33 +0000 | [diff] [blame] | 90 | if (*args == NULL) |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 91 | show_usage(); |
| 92 | if (strcmp(*args, "-net") == 0) { |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 93 | xflag = 1; |
| 94 | args++; |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 95 | } else if (strcmp(*args, "-host") == 0) { |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 96 | xflag = 2; |
| 97 | args++; |
| 98 | } |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 99 | if (*args == NULL) |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 100 | show_usage(); |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 101 | safe_strncpy(target, *args++, (sizeof target)); |
| 102 | |
| 103 | /* Clean out the RTREQ structure. */ |
| 104 | memset((char *) &rt, 0, sizeof(struct rtentry)); |
| 105 | |
| 106 | |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 107 | if ((isnet = |
| 108 | INET_resolve(target, (struct sockaddr_in *) &rt.rt_dst, |
| 109 | xflag != 1)) < 0) { |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 110 | error_msg(_("can't resolve %s"), target); |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 111 | return EXIT_FAILURE; /* XXX change to E_something */ |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | switch (xflag) { |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 115 | case 1: |
| 116 | isnet = 1; |
| 117 | break; |
Eric Andersen | b940850 | 2001-09-05 19:32:00 +0000 | [diff] [blame] | 118 | |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 119 | case 2: |
| 120 | isnet = 0; |
| 121 | break; |
Eric Andersen | b940850 | 2001-09-05 19:32:00 +0000 | [diff] [blame] | 122 | |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 123 | default: |
| 124 | break; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 125 | } |
Eric Andersen | b940850 | 2001-09-05 19:32:00 +0000 | [diff] [blame] | 126 | |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 127 | /* Fill in the other fields. */ |
| 128 | rt.rt_flags = (RTF_UP | RTF_HOST); |
| 129 | if (isnet) |
| 130 | rt.rt_flags &= ~RTF_HOST; |
| 131 | |
| 132 | while (*args) { |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 133 | if (strcmp(*args, "metric") == 0) { |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 134 | int metric; |
Eric Andersen | b940850 | 2001-09-05 19:32:00 +0000 | [diff] [blame] | 135 | |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 136 | args++; |
| 137 | if (!*args || !isdigit(**args)) |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 138 | show_usage(); |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 139 | metric = atoi(*args); |
| 140 | #if HAVE_NEW_ADDRT |
| 141 | rt.rt_metric = metric + 1; |
| 142 | #else |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 143 | ENOSUPP("inet_setroute", "NEW_ADDRT (metric)"); /* XXX Fixme */ |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 144 | #endif |
| 145 | args++; |
| 146 | continue; |
| 147 | } |
| 148 | |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 149 | if (strcmp(*args, "netmask") == 0) { |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 150 | struct sockaddr mask; |
Eric Andersen | b940850 | 2001-09-05 19:32:00 +0000 | [diff] [blame] | 151 | |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 152 | args++; |
| 153 | if (!*args || mask_in_addr(rt)) |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 154 | show_usage(); |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 155 | netmask = *args; |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 156 | if ((isnet = |
| 157 | INET_resolve(netmask, (struct sockaddr_in *) &mask, |
| 158 | 0)) < 0) { |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 159 | error_msg(_("can't resolve netmask %s"), netmask); |
| 160 | return E_LOOKUP; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 161 | } |
| 162 | rt.rt_genmask = full_mask(mask); |
| 163 | args++; |
| 164 | continue; |
| 165 | } |
| 166 | |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 167 | if (strcmp(*args, "gw") == 0 || strcmp(*args, "gateway") == 0) { |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 168 | args++; |
| 169 | if (!*args) |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 170 | show_usage(); |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 171 | if (rt.rt_flags & RTF_GATEWAY) |
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(gateway, *args, (sizeof gateway)); |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 174 | if ((isnet = |
| 175 | INET_resolve(gateway, (struct sockaddr_in *) &rt.rt_gateway, |
| 176 | 1)) < 0) { |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 177 | error_msg(_("can't resolve gw %s"), gateway); |
| 178 | return E_LOOKUP; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 179 | } |
| 180 | if (isnet) { |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 181 | error_msg(_("%s: cannot use a NETWORK as gateway!"), gateway); |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 182 | return E_OPTERR; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 183 | } |
| 184 | rt.rt_flags |= RTF_GATEWAY; |
| 185 | args++; |
| 186 | continue; |
| 187 | } |
| 188 | |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 189 | if (strcmp(*args, "mss") == 0) { |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 190 | args++; |
| 191 | rt.rt_flags |= RTF_MSS; |
| 192 | if (!*args) |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 193 | show_usage(); |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 194 | rt.rt_mss = atoi(*args); |
| 195 | args++; |
| 196 | if (rt.rt_mss < 64 || rt.rt_mss > 32768) { |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 197 | error_msg(_("Invalid MSS.")); |
| 198 | return E_OPTERR; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 199 | } |
| 200 | continue; |
| 201 | } |
| 202 | |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 203 | if (strcmp(*args, "window") == 0) { |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 204 | args++; |
| 205 | if (!*args) |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 206 | show_usage(); |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 207 | rt.rt_flags |= RTF_WINDOW; |
| 208 | rt.rt_window = atoi(*args); |
| 209 | args++; |
| 210 | if (rt.rt_window < 128) { |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 211 | error_msg(_("Invalid window.")); |
| 212 | return E_OPTERR; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 213 | } |
| 214 | continue; |
| 215 | } |
| 216 | |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 217 | if (strcmp(*args, "irtt") == 0) { |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 218 | args++; |
| 219 | if (!*args) |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 220 | show_usage(); |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 221 | args++; |
| 222 | #if HAVE_RTF_IRTT |
| 223 | rt.rt_flags |= RTF_IRTT; |
| 224 | rt.rt_irtt = atoi(*(args - 1)); |
Eric Andersen | 0552b9a | 2002-12-11 03:57:12 +0000 | [diff] [blame] | 225 | rt.rt_irtt *= (sysconf(_SC_CLK_TCK) / 100); /* FIXME */ |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 226 | #if 0 /* FIXME: do we need to check anything of this? */ |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 227 | if (rt.rt_irtt < 1 || rt.rt_irtt > (120 * HZ)) { |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 228 | error_msg(_("Invalid initial rtt.")); |
| 229 | return E_OPTERR; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 230 | } |
| 231 | #endif |
| 232 | #else |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 233 | ENOSUPP("inet_setroute", "RTF_IRTT"); /* XXX Fixme */ |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 234 | #endif |
| 235 | continue; |
| 236 | } |
| 237 | |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 238 | if (strcmp(*args, "reject") == 0) { |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 239 | args++; |
| 240 | #if HAVE_RTF_REJECT |
| 241 | rt.rt_flags |= RTF_REJECT; |
| 242 | #else |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 243 | ENOSUPP("inet_setroute", "RTF_REJECT"); /* XXX Fixme */ |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 244 | #endif |
| 245 | continue; |
| 246 | } |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 247 | if (strcmp(*args, "mod") == 0) { |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 248 | args++; |
| 249 | rt.rt_flags |= RTF_MODIFIED; |
| 250 | continue; |
| 251 | } |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 252 | if (strcmp(*args, "dyn") == 0) { |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 253 | args++; |
| 254 | rt.rt_flags |= RTF_DYNAMIC; |
| 255 | continue; |
| 256 | } |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 257 | if (strcmp(*args, "reinstate") == 0) { |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 258 | args++; |
| 259 | rt.rt_flags |= RTF_REINSTATE; |
| 260 | continue; |
| 261 | } |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 262 | if (strcmp(*args, "device") == 0 || strcmp(*args, "dev") == 0) { |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 263 | args++; |
| 264 | if (rt.rt_dev || *args == NULL) |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 265 | show_usage(); |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 266 | rt.rt_dev = *args++; |
| 267 | continue; |
| 268 | } |
| 269 | /* nothing matches */ |
| 270 | if (!rt.rt_dev) { |
| 271 | rt.rt_dev = *args++; |
| 272 | if (*args) |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 273 | show_usage(); /* must be last to catch typos */ |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 274 | } else { |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 275 | show_usage(); |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 276 | } |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | #if HAVE_RTF_REJECT |
| 280 | if ((rt.rt_flags & RTF_REJECT) && !rt.rt_dev) |
| 281 | rt.rt_dev = "lo"; |
| 282 | #endif |
| 283 | |
| 284 | /* sanity checks.. */ |
| 285 | if (mask_in_addr(rt)) { |
| 286 | unsigned long mask = mask_in_addr(rt); |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 287 | |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 288 | mask = ~ntohl(mask); |
| 289 | if ((rt.rt_flags & RTF_HOST) && mask != 0xffffffff) { |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 290 | error_msg(_("netmask %.8x doesn't make sense with host route"), |
| 291 | (unsigned int) mask); |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 292 | return E_OPTERR; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 293 | } |
| 294 | if (mask & (mask + 1)) { |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 295 | error_msg(_("bogus netmask %s"), netmask); |
| 296 | return E_OPTERR; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 297 | } |
| 298 | mask = ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr; |
| 299 | if (mask & ~mask_in_addr(rt)) { |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 300 | error_msg(_("netmask doesn't match route address")); |
| 301 | return E_OPTERR; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | /* Fill out netmask if still unset */ |
| 305 | if ((action == RTACTION_ADD) && rt.rt_flags & RTF_HOST) |
| 306 | mask_in_addr(rt) = 0xffffffff; |
| 307 | |
| 308 | /* Create a socket to the INET kernel. */ |
| 309 | if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
| 310 | perror("socket"); |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 311 | return E_SOCK; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 312 | } |
| 313 | /* Tell the kernel to accept this route. */ |
| 314 | if (action == RTACTION_DEL) { |
| 315 | if (ioctl(skfd, SIOCDELRT, &rt) < 0) { |
| 316 | perror("SIOCDELRT"); |
| 317 | close(skfd); |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 318 | return E_SOCK; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 319 | } |
| 320 | } else { |
| 321 | if (ioctl(skfd, SIOCADDRT, &rt) < 0) { |
| 322 | perror("SIOCADDRT"); |
| 323 | close(skfd); |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 324 | return E_SOCK; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 325 | } |
| 326 | } |
Eric Andersen | b940850 | 2001-09-05 19:32:00 +0000 | [diff] [blame] | 327 | |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 328 | /* Close the socket. */ |
| 329 | (void) close(skfd); |
Mark Whitley | 99806ad | 2001-02-15 23:00:48 +0000 | [diff] [blame] | 330 | return EXIT_SUCCESS; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Glenn L McGrath | 8ae4cab | 2002-11-26 09:02:06 +0000 | [diff] [blame] | 333 | #ifdef CONFIG_FEATURE_IPV6 |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 334 | static int INET6_setroute(int action, int options, char **args) |
| 335 | { |
| 336 | struct in6_rtmsg rt; |
| 337 | struct ifreq ifr; |
| 338 | struct sockaddr_in6 sa6; |
| 339 | char target[128], gateway[128] = "NONE"; |
| 340 | int metric, prefix_len; |
| 341 | char *devname = NULL; |
| 342 | char *cp; |
| 343 | int skfd; |
| 344 | |
| 345 | if (*args == NULL) |
| 346 | show_usage(); |
| 347 | |
| 348 | strcpy(target, *args++); |
| 349 | if (!strcmp(target, "default")) { |
| 350 | prefix_len = 0; |
| 351 | memset(&sa6, 0, sizeof(sa6)); |
| 352 | } else { |
| 353 | if ((cp = strchr(target, '/'))) { |
| 354 | prefix_len = atol(cp + 1); |
| 355 | if ((prefix_len < 0) || (prefix_len > 128)) |
| 356 | show_usage(); |
| 357 | *cp = 0; |
| 358 | } else { |
| 359 | prefix_len = 128; |
| 360 | } |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 361 | if (INET6_resolve(target, (struct sockaddr_in6 *) &sa6) < 0) { |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 362 | error_msg(_("can't resolve %s"), target); |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 363 | return EXIT_FAILURE; /* XXX change to E_something */ |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
| 367 | /* Clean out the RTREQ structure. */ |
| 368 | memset((char *) &rt, 0, sizeof(struct in6_rtmsg)); |
| 369 | |
| 370 | memcpy(&rt.rtmsg_dst, sa6.sin6_addr.s6_addr, sizeof(struct in6_addr)); |
| 371 | |
| 372 | /* Fill in the other fields. */ |
| 373 | rt.rtmsg_flags = RTF_UP; |
| 374 | if (prefix_len == 128) |
| 375 | rt.rtmsg_flags |= RTF_HOST; |
| 376 | rt.rtmsg_metric = 1; |
| 377 | rt.rtmsg_dst_len = prefix_len; |
| 378 | |
| 379 | while (*args) { |
| 380 | if (!strcmp(*args, "metric")) { |
| 381 | |
| 382 | args++; |
| 383 | if (!*args || !isdigit(**args)) |
| 384 | show_usage(); |
| 385 | metric = atoi(*args); |
| 386 | rt.rtmsg_metric = metric; |
| 387 | args++; |
| 388 | continue; |
| 389 | } |
| 390 | if (!strcmp(*args, "gw") || !strcmp(*args, "gateway")) { |
| 391 | args++; |
| 392 | if (!*args) |
| 393 | show_usage(); |
| 394 | if (rt.rtmsg_flags & RTF_GATEWAY) |
| 395 | show_usage(); |
| 396 | strcpy(gateway, *args); |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 397 | if (INET6_resolve(gateway, (struct sockaddr_in6 *) &sa6) < 0) { |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 398 | error_msg(_("can't resolve gw %s"), gateway); |
| 399 | return (E_LOOKUP); |
| 400 | } |
| 401 | memcpy(&rt.rtmsg_gateway, sa6.sin6_addr.s6_addr, |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 402 | sizeof(struct in6_addr)); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 403 | rt.rtmsg_flags |= RTF_GATEWAY; |
| 404 | args++; |
| 405 | continue; |
| 406 | } |
| 407 | if (!strcmp(*args, "mod")) { |
| 408 | args++; |
| 409 | rt.rtmsg_flags |= RTF_MODIFIED; |
| 410 | continue; |
| 411 | } |
| 412 | if (!strcmp(*args, "dyn")) { |
| 413 | args++; |
| 414 | rt.rtmsg_flags |= RTF_DYNAMIC; |
| 415 | continue; |
| 416 | } |
| 417 | if (!strcmp(*args, "device") || !strcmp(*args, "dev")) { |
| 418 | args++; |
| 419 | if (!*args) |
| 420 | show_usage(); |
| 421 | } else if (args[1]) |
| 422 | show_usage(); |
| 423 | |
| 424 | devname = *args; |
| 425 | args++; |
| 426 | } |
| 427 | |
| 428 | /* Create a socket to the INET6 kernel. */ |
| 429 | if ((skfd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { |
| 430 | perror("socket"); |
| 431 | return (E_SOCK); |
| 432 | } |
| 433 | if (devname) { |
| 434 | memset(&ifr, 0, sizeof(ifr)); |
| 435 | strcpy(ifr.ifr_name, devname); |
| 436 | |
| 437 | if (ioctl(skfd, SIOGIFINDEX, &ifr) < 0) { |
| 438 | perror("SIOGIFINDEX"); |
| 439 | return (E_SOCK); |
| 440 | } |
| 441 | rt.rtmsg_ifindex = ifr.ifr_ifindex; |
| 442 | } else |
| 443 | rt.rtmsg_ifindex = 0; |
| 444 | |
| 445 | /* Tell the kernel to accept this route. */ |
| 446 | if (action == RTACTION_DEL) { |
| 447 | if (ioctl(skfd, SIOCDELRT, &rt) < 0) { |
| 448 | perror("SIOCDELRT"); |
| 449 | close(skfd); |
| 450 | return (E_SOCK); |
| 451 | } |
| 452 | } else { |
| 453 | if (ioctl(skfd, SIOCADDRT, &rt) < 0) { |
| 454 | perror("SIOCADDRT"); |
| 455 | close(skfd); |
| 456 | return (E_SOCK); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | /* Close the socket. */ |
| 461 | (void) close(skfd); |
| 462 | return (0); |
| 463 | } |
| 464 | #endif |
| 465 | |
Eric Andersen | 863a3e1 | 2001-08-27 17:57:27 +0000 | [diff] [blame] | 466 | #ifndef RTF_UP |
| 467 | /* Keep this in sync with /usr/src/linux/include/linux/route.h */ |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 468 | #define RTF_UP 0x0001 /* route usable */ |
| 469 | #define RTF_GATEWAY 0x0002 /* destination is a gateway */ |
| 470 | #define RTF_HOST 0x0004 /* host entry (net otherwise) */ |
| 471 | #define RTF_REINSTATE 0x0008 /* reinstate route after tmout */ |
| 472 | #define RTF_DYNAMIC 0x0010 /* created dyn. (by redirect) */ |
| 473 | #define RTF_MODIFIED 0x0020 /* modified dyn. (by redirect) */ |
| 474 | #define RTF_MTU 0x0040 /* specific MTU for this route */ |
Eric Andersen | 863a3e1 | 2001-08-27 17:57:27 +0000 | [diff] [blame] | 475 | #ifndef RTF_MSS |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 476 | #define RTF_MSS RTF_MTU /* Compatibility :-( */ |
Eric Andersen | 863a3e1 | 2001-08-27 17:57:27 +0000 | [diff] [blame] | 477 | #endif |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 478 | #define RTF_WINDOW 0x0080 /* per route window clamping */ |
| 479 | #define RTF_IRTT 0x0100 /* Initial round trip time */ |
| 480 | #define RTF_REJECT 0x0200 /* Reject route */ |
Eric Andersen | 863a3e1 | 2001-08-27 17:57:27 +0000 | [diff] [blame] | 481 | #endif |
| 482 | |
Robert Griebl | 820098f | 2002-05-14 23:03:23 +0000 | [diff] [blame] | 483 | void displayroutes(int noresolve, int netstatfmt) |
Eric Andersen | 68be2ab | 2001-02-14 19:26:39 +0000 | [diff] [blame] | 484 | { |
| 485 | char buff[256]; |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 486 | int nl = 0; |
Eric Andersen | 68be2ab | 2001-02-14 19:26:39 +0000 | [diff] [blame] | 487 | struct in_addr dest; |
| 488 | struct in_addr gw; |
| 489 | struct in_addr mask; |
Robert Griebl | 820098f | 2002-05-14 23:03:23 +0000 | [diff] [blame] | 490 | int flgs, ref, use, metric, mtu, win, ir; |
Eric Andersen | b940850 | 2001-09-05 19:32:00 +0000 | [diff] [blame] | 491 | char flags[64]; |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 492 | unsigned long int d, g, m; |
Eric Andersen | 68be2ab | 2001-02-14 19:26:39 +0000 | [diff] [blame] | 493 | |
| 494 | char sdest[16], sgw[16]; |
| 495 | |
Eric Andersen | f1bbb22 | 2001-02-18 20:12:25 +0000 | [diff] [blame] | 496 | FILE *fp = xfopen("/proc/net/route", "r"); |
Eric Andersen | 68be2ab | 2001-02-14 19:26:39 +0000 | [diff] [blame] | 497 | |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 498 | if (noresolve) |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 499 | noresolve = 0x0fff; |
| 500 | |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 501 | while (fgets(buff, sizeof(buff), fp) != NULL) { |
| 502 | if (nl) { |
Eric Andersen | 68be2ab | 2001-02-14 19:26:39 +0000 | [diff] [blame] | 503 | int ifl = 0; |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 504 | int numeric; |
| 505 | struct sockaddr_in s_addr; |
| 506 | |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 507 | while (buff[ifl] != ' ' && buff[ifl] != '\t' && buff[ifl] != '\0') |
Eric Andersen | 68be2ab | 2001-02-14 19:26:39 +0000 | [diff] [blame] | 508 | ifl++; |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 509 | buff[ifl] = 0; /* interface */ |
| 510 | if (sscanf(buff + ifl + 1, "%lx%lx%X%d%d%d%lx%d%d%d", |
| 511 | &d, &g, &flgs, &ref, &use, &metric, &m, &mtu, &win, |
| 512 | &ir) != 10) { |
| 513 | error_msg_and_die("Unsuported kernel route format\n"); |
Eric Andersen | 68be2ab | 2001-02-14 19:26:39 +0000 | [diff] [blame] | 514 | } |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 515 | if (nl == 1) { |
Robert Griebl | 820098f | 2002-05-14 23:03:23 +0000 | [diff] [blame] | 516 | printf("Kernel IP routing table\n"); |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 517 | printf |
| 518 | ("Destination Gateway Genmask Flags %s Iface\n", |
| 519 | netstatfmt ? " MSS Window irtt" : "Metric Ref Use"); |
Robert Griebl | 820098f | 2002-05-14 23:03:23 +0000 | [diff] [blame] | 520 | } |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 521 | ifl = 0; /* parse flags */ |
| 522 | if (flgs & RTF_UP) { |
| 523 | if (flgs & RTF_REJECT) |
| 524 | flags[ifl++] = '!'; |
| 525 | else |
| 526 | flags[ifl++] = 'U'; |
| 527 | if (flgs & RTF_GATEWAY) |
| 528 | flags[ifl++] = 'G'; |
| 529 | if (flgs & RTF_HOST) |
| 530 | flags[ifl++] = 'H'; |
| 531 | if (flgs & RTF_REINSTATE) |
| 532 | flags[ifl++] = 'R'; |
| 533 | if (flgs & RTF_DYNAMIC) |
| 534 | flags[ifl++] = 'D'; |
| 535 | if (flgs & RTF_MODIFIED) |
| 536 | flags[ifl++] = 'M'; |
| 537 | flags[ifl] = 0; |
| 538 | dest.s_addr = d; |
| 539 | gw.s_addr = g; |
| 540 | mask.s_addr = m; |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 541 | memset(&s_addr, 0, sizeof(struct sockaddr_in)); |
| 542 | s_addr.sin_family = AF_INET; |
| 543 | s_addr.sin_addr = dest; |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 544 | numeric = noresolve | 0x8000; /* default instead of * */ |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 545 | INET_rresolve(sdest, sizeof(sdest), &s_addr, numeric, m); |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 546 | numeric = noresolve | 0x4000; /* host instead of net */ |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 547 | s_addr.sin_addr = gw; |
| 548 | INET_rresolve(sgw, sizeof(sgw), &s_addr, numeric, m); |
Robert Griebl | c30c5e8 | 2002-05-16 19:14:15 +0000 | [diff] [blame] | 549 | |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 550 | printf("%-16s%-16s%-16s%-6s", sdest, sgw, inet_ntoa(mask), |
| 551 | flags); |
| 552 | if (netstatfmt) |
| 553 | printf("%5d %-5d %6d %s\n", mtu, win, ir, buff); |
| 554 | else |
| 555 | printf("%-6d %-2d %7d %s\n", metric, ref, use, buff); |
Eric Andersen | b940850 | 2001-09-05 19:32:00 +0000 | [diff] [blame] | 556 | } |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 557 | } |
Eric Andersen | b940850 | 2001-09-05 19:32:00 +0000 | [diff] [blame] | 558 | nl++; |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 559 | } |
Eric Andersen | 68be2ab | 2001-02-14 19:26:39 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Glenn L McGrath | 8ae4cab | 2002-11-26 09:02:06 +0000 | [diff] [blame] | 562 | #ifdef CONFIG_FEATURE_IPV6 |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 563 | static void INET6_displayroutes(int noresolve) |
| 564 | { |
| 565 | char buff[256]; |
| 566 | char iface[16], flags[16]; |
| 567 | char addr6[128], naddr6[128]; |
| 568 | struct sockaddr_in6 saddr6, snaddr6; |
| 569 | int iflags, metric, refcnt, use, prefix_len, slen; |
| 570 | int numeric; |
| 571 | |
| 572 | char addr6p[8][5], saddr6p[8][5], naddr6p[8][5]; |
| 573 | |
| 574 | FILE *fp = xfopen("/proc/net/ipv6_route", "r"); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 575 | |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 576 | flags[0] = 'U'; |
| 577 | |
| 578 | if (noresolve) |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 579 | noresolve = 0x0fff; |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 580 | numeric = noresolve | 0x8000; /* default instead of * */ |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 581 | |
| 582 | printf("Kernel IPv6 routing table\n" |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 583 | "Destination " |
| 584 | "Next Hop " |
| 585 | "Flags Metric Ref Use Iface\n"); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 586 | |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 587 | while (fgets(buff, sizeof(buff), fp) != NULL) { |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 588 | int ifl; |
| 589 | |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 590 | if (sscanf(buff, "%4s%4s%4s%4s%4s%4s%4s%4s %02x " |
| 591 | "%4s%4s%4s%4s%4s%4s%4s%4s %02x " |
| 592 | "%4s%4s%4s%4s%4s%4s%4s%4s %08x %08x %08x %08x %s\n", |
| 593 | addr6p[0], addr6p[1], addr6p[2], addr6p[3], |
| 594 | addr6p[4], addr6p[5], addr6p[6], addr6p[7], |
| 595 | &prefix_len, |
| 596 | saddr6p[0], saddr6p[1], saddr6p[2], saddr6p[3], |
| 597 | saddr6p[4], saddr6p[5], saddr6p[6], saddr6p[7], |
| 598 | &slen, |
| 599 | naddr6p[0], naddr6p[1], naddr6p[2], naddr6p[3], |
| 600 | naddr6p[4], naddr6p[5], naddr6p[6], naddr6p[7], |
| 601 | &metric, &use, &refcnt, &iflags, iface) != 31) { |
| 602 | error_msg_and_die("Unsuported kernel route format\n"); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 605 | ifl = 1; /* parse flags */ |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 606 | if (!(iflags & RTF_UP)) |
| 607 | continue; |
| 608 | if (iflags & RTF_GATEWAY) |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 609 | flags[ifl++] = 'G'; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 610 | if (iflags & RTF_HOST) |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 611 | flags[ifl++] = 'H'; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 612 | if (iflags & RTF_DEFAULT) |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 613 | flags[ifl++] = 'D'; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 614 | if (iflags & RTF_ADDRCONF) |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 615 | flags[ifl++] = 'A'; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 616 | if (iflags & RTF_CACHE) |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 617 | flags[ifl++] = 'C'; |
| 618 | flags[ifl] = 0; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 619 | |
| 620 | /* Fetch and resolve the target address. */ |
| 621 | snprintf(addr6, sizeof(addr6), "%s:%s:%s:%s:%s:%s:%s:%s", |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 622 | addr6p[0], addr6p[1], addr6p[2], addr6p[3], |
| 623 | addr6p[4], addr6p[5], addr6p[6], addr6p[7]); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 624 | inet_pton(AF_INET6, addr6, (struct sockaddr *) &saddr6.sin6_addr); |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 625 | saddr6.sin6_family = AF_INET6; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 626 | |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 627 | INET6_rresolve(addr6, sizeof(addr6), (struct sockaddr_in6 *) &saddr6, |
| 628 | numeric); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 629 | snprintf(addr6, sizeof(addr6), "%s/%d", addr6, prefix_len); |
| 630 | |
| 631 | /* Fetch and resolve the nexthop address. */ |
| 632 | snprintf(naddr6, sizeof(naddr6), "%s:%s:%s:%s:%s:%s:%s:%s", |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 633 | naddr6p[0], naddr6p[1], naddr6p[2], naddr6p[3], |
| 634 | naddr6p[4], naddr6p[5], naddr6p[6], naddr6p[7]); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 635 | inet_pton(AF_INET6, naddr6, (struct sockaddr *) &snaddr6.sin6_addr); |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 636 | snaddr6.sin6_family = AF_INET6; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 637 | |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 638 | INET6_rresolve(naddr6, sizeof(naddr6), |
| 639 | (struct sockaddr_in6 *) &snaddr6, numeric); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 640 | |
| 641 | /* Print the info. */ |
| 642 | printf("%-43s %-39s %-5s %-6d %-2d %7d %-8s\n", |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 643 | addr6, naddr6, flags, metric, refcnt, use, iface); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | #endif |
| 647 | |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 648 | int route_main(int argc, char **argv) |
| 649 | { |
Robert Griebl | 820098f | 2002-05-14 23:03:23 +0000 | [diff] [blame] | 650 | int opt; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 651 | int what = 0; |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 652 | |
Glenn L McGrath | 8ae4cab | 2002-11-26 09:02:06 +0000 | [diff] [blame] | 653 | #ifdef CONFIG_FEATURE_IPV6 |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 654 | int af = AF_INET; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 655 | #endif |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 656 | |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 657 | if (!argv[1] || (argv[1][0] == '-')) { |
Robert Griebl | 820098f | 2002-05-14 23:03:23 +0000 | [diff] [blame] | 658 | /* check options */ |
| 659 | int noresolve = 0; |
| 660 | int extended = 0; |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 661 | |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 662 | while ((opt = getopt(argc, argv, "A:ne")) > 0) { |
Robert Griebl | 820098f | 2002-05-14 23:03:23 +0000 | [diff] [blame] | 663 | switch (opt) { |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 664 | case 'n': |
| 665 | noresolve = 1; |
| 666 | break; |
| 667 | case 'e': |
| 668 | extended = 1; |
| 669 | break; |
| 670 | case 'A': |
Glenn L McGrath | 8ae4cab | 2002-11-26 09:02:06 +0000 | [diff] [blame] | 671 | #ifdef CONFIG_FEATURE_IPV6 |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 672 | if (strcmp(optarg, "inet6") == 0) |
| 673 | af = AF_INET6; |
| 674 | break; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 675 | #endif |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 676 | default: |
| 677 | show_usage(); |
Robert Griebl | 820098f | 2002-05-14 23:03:23 +0000 | [diff] [blame] | 678 | } |
| 679 | } |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 680 | |
Glenn L McGrath | 8ae4cab | 2002-11-26 09:02:06 +0000 | [diff] [blame] | 681 | #ifdef CONFIG_FEATURE_IPV6 |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 682 | if (af == AF_INET6) |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 683 | INET6_displayroutes(*argv != NULL); |
| 684 | else |
| 685 | #endif |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 686 | displayroutes(noresolve, extended); |
Eric Andersen | 26d53eb | 2001-03-07 06:33:01 +0000 | [diff] [blame] | 687 | return EXIT_SUCCESS; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 688 | } else { |
| 689 | /* check verb */ |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 690 | if (strcmp(argv[1], "add") == 0) |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 691 | what = RTACTION_ADD; |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 692 | else if (strcmp(argv[1], "del") == 0 |
| 693 | || strcmp(argv[1], "delete") == 0) |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 694 | what = RTACTION_DEL; |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 695 | else if (strcmp(argv[1], "flush") == 0) |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 696 | what = RTACTION_FLUSH; |
| 697 | else |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 698 | show_usage(); |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Glenn L McGrath | 8ae4cab | 2002-11-26 09:02:06 +0000 | [diff] [blame] | 701 | #ifdef CONFIG_FEATURE_IPV6 |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 702 | if (af == AF_INET6) |
| 703 | return INET6_setroute(what, 0, argv + 2); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 704 | #endif |
Glenn L McGrath | 7c58e9b | 2002-08-22 18:24:43 +0000 | [diff] [blame] | 705 | return INET_setroute(what, 0, argv + 2); |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 706 | } |