osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * ip.c "ip" utility frontend. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 10 | * |
| 11 | * |
| 12 | * Changes: |
| 13 | * |
| 14 | * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses |
| 15 | */ |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <unistd.h> |
| 20 | #include <syslog.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <sys/socket.h> |
| 23 | #include <netinet/in.h> |
| 24 | #include <string.h> |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 25 | #include <errno.h> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 26 | |
| 27 | #include "SNAPSHOT.h" |
| 28 | #include "utils.h" |
| 29 | #include "ip_common.h" |
| 30 | |
| 31 | int preferred_family = AF_UNSPEC; |
| 32 | int show_stats = 0; |
Patrick McHardy | 1d93483 | 2007-08-22 10:49:01 -0700 | [diff] [blame] | 33 | int show_details = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 34 | int resolve_hosts = 0; |
| 35 | int oneline = 0; |
shemminger | 90f9302 | 2005-06-07 21:55:55 +0000 | [diff] [blame] | 36 | int timestamp = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 37 | char * _SL_ = NULL; |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 38 | char *batch_file = NULL; |
| 39 | int force = 0; |
Stephen Hemminger | 3bfa73f | 2006-09-26 10:41:57 -0700 | [diff] [blame] | 40 | struct rtnl_handle rth = { .fd = -1 }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 41 | |
| 42 | static void usage(void) __attribute__((noreturn)); |
| 43 | |
| 44 | static void usage(void) |
| 45 | { |
| 46 | fprintf(stderr, |
| 47 | "Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }\n" |
shemminger | 3d418dc | 2005-09-01 22:29:33 +0000 | [diff] [blame] | 48 | " ip [ -force ] [-batch filename\n" |
shemminger | 09954dc | 2006-01-10 18:43:32 +0000 | [diff] [blame] | 49 | "where OBJECT := { link | addr | route | rule | neigh | ntable | tunnel |\n" |
Stephen Hemminger | 147da5d | 2007-09-05 11:51:10 +0100 | [diff] [blame] | 50 | " maddr | mroute | monitor | xfrm }\n" |
Patrick McHardy | 1d93483 | 2007-08-22 10:49:01 -0700 | [diff] [blame] | 51 | " OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |\n" |
shemminger | 90f9302 | 2005-06-07 21:55:55 +0000 | [diff] [blame] | 52 | " -f[amily] { inet | inet6 | ipx | dnet | link } |\n" |
| 53 | " -o[neline] | -t[imestamp] }\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 54 | exit(-1); |
| 55 | } |
| 56 | |
shemminger | fc57a9d | 2005-09-07 17:43:00 +0000 | [diff] [blame] | 57 | static int do_help(int argc, char **argv) |
| 58 | { |
| 59 | usage(); |
| 60 | } |
| 61 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 62 | static const struct cmd { |
| 63 | const char *cmd; |
| 64 | int (*func)(int argc, char **argv); |
| 65 | } cmds[] = { |
shemminger | ede7239 | 2005-11-22 17:30:43 +0000 | [diff] [blame] | 66 | { "address", do_ipaddr }, |
| 67 | { "maddress", do_multiaddr }, |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 68 | { "route", do_iproute }, |
| 69 | { "rule", do_iprule }, |
shemminger | ede7239 | 2005-11-22 17:30:43 +0000 | [diff] [blame] | 70 | { "neighbor", do_ipneigh }, |
| 71 | { "neighbour", do_ipneigh }, |
shemminger | 09954dc | 2006-01-10 18:43:32 +0000 | [diff] [blame] | 72 | { "ntable", do_ipntable }, |
| 73 | { "ntbl", do_ipntable }, |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 74 | { "link", do_iplink }, |
| 75 | { "tunnel", do_iptunnel }, |
shemminger | ede7239 | 2005-11-22 17:30:43 +0000 | [diff] [blame] | 76 | { "tunl", do_iptunnel }, |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 77 | { "monitor", do_ipmonitor }, |
| 78 | { "xfrm", do_xfrm }, |
shemminger | 84616f8 | 2005-10-07 16:41:34 +0000 | [diff] [blame] | 79 | { "mroute", do_multiroute }, |
shemminger | fc57a9d | 2005-09-07 17:43:00 +0000 | [diff] [blame] | 80 | { "help", do_help }, |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 81 | { 0 } |
| 82 | }; |
| 83 | |
| 84 | static int do_cmd(const char *argv0, int argc, char **argv) |
| 85 | { |
shemminger | e25d697 | 2005-12-02 20:02:26 +0000 | [diff] [blame] | 86 | const struct cmd *c; |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 87 | |
shemminger | ede7239 | 2005-11-22 17:30:43 +0000 | [diff] [blame] | 88 | for (c = cmds; c->cmd; ++c) { |
shemminger | e25d697 | 2005-12-02 20:02:26 +0000 | [diff] [blame] | 89 | if (matches(argv0, c->cmd) == 0) |
| 90 | return c->func(argc-1, argv+1); |
shemminger | ede7239 | 2005-11-22 17:30:43 +0000 | [diff] [blame] | 91 | } |
| 92 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 93 | fprintf(stderr, "Object \"%s\" is unknown, try \"ip help\".\n", argv0); |
shemminger | ede7239 | 2005-11-22 17:30:43 +0000 | [diff] [blame] | 94 | return -1; |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static int batch(const char *name) |
| 98 | { |
| 99 | char *line = NULL; |
| 100 | size_t len = 0; |
| 101 | int ret = 0; |
| 102 | int lineno = 0; |
| 103 | |
| 104 | if (name && strcmp(name, "-") != 0) { |
| 105 | if (freopen(name, "r", stdin) == NULL) { |
| 106 | fprintf(stderr, "Cannot open file \"%s\" for reading: %s=n", |
| 107 | name, strerror(errno)); |
| 108 | return -1; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | if (rtnl_open(&rth, 0) < 0) { |
| 113 | fprintf(stderr, "Cannot open rtnetlink\n"); |
| 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | while (getcmdline(&line, &len, stdin) != -1) { |
| 118 | char *largv[100]; |
| 119 | int largc; |
| 120 | |
| 121 | largc = makeargs(line, largv, 100); |
| 122 | if (largc == 0) |
| 123 | continue; /* blank line */ |
| 124 | |
| 125 | if (do_cmd(largv[0], largc, largv)) { |
| 126 | fprintf(stderr, "Command failed %s:%d\n", name, lineno); |
| 127 | ret = 1; |
| 128 | if (!force) |
| 129 | break; |
| 130 | } |
| 131 | } |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 132 | if (line) |
| 133 | free(line); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 134 | |
| 135 | rtnl_close(&rth); |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 140 | int main(int argc, char **argv) |
| 141 | { |
| 142 | char *basename; |
| 143 | |
| 144 | basename = strrchr(argv[0], '/'); |
| 145 | if (basename == NULL) |
| 146 | basename = argv[0]; |
| 147 | else |
| 148 | basename++; |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 149 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 150 | while (argc > 1) { |
| 151 | char *opt = argv[1]; |
| 152 | if (strcmp(opt,"--") == 0) { |
| 153 | argc--; argv++; |
| 154 | break; |
| 155 | } |
| 156 | if (opt[0] != '-') |
| 157 | break; |
| 158 | if (opt[1] == '-') |
| 159 | opt++; |
| 160 | if (matches(opt, "-family") == 0) { |
| 161 | argc--; |
| 162 | argv++; |
| 163 | if (argc <= 1) |
| 164 | usage(); |
| 165 | if (strcmp(argv[1], "inet") == 0) |
| 166 | preferred_family = AF_INET; |
| 167 | else if (strcmp(argv[1], "inet6") == 0) |
| 168 | preferred_family = AF_INET6; |
| 169 | else if (strcmp(argv[1], "dnet") == 0) |
| 170 | preferred_family = AF_DECnet; |
| 171 | else if (strcmp(argv[1], "link") == 0) |
| 172 | preferred_family = AF_PACKET; |
| 173 | else if (strcmp(argv[1], "ipx") == 0) |
| 174 | preferred_family = AF_IPX; |
| 175 | else if (strcmp(argv[1], "help") == 0) |
| 176 | usage(); |
| 177 | else |
| 178 | invarg(argv[1], "invalid protocol family"); |
| 179 | } else if (strcmp(opt, "-4") == 0) { |
| 180 | preferred_family = AF_INET; |
| 181 | } else if (strcmp(opt, "-6") == 0) { |
| 182 | preferred_family = AF_INET6; |
| 183 | } else if (strcmp(opt, "-0") == 0) { |
| 184 | preferred_family = AF_PACKET; |
| 185 | } else if (strcmp(opt, "-I") == 0) { |
| 186 | preferred_family = AF_IPX; |
| 187 | } else if (strcmp(opt, "-D") == 0) { |
| 188 | preferred_family = AF_DECnet; |
| 189 | } else if (matches(opt, "-stats") == 0 || |
| 190 | matches(opt, "-statistics") == 0) { |
| 191 | ++show_stats; |
Patrick McHardy | 1d93483 | 2007-08-22 10:49:01 -0700 | [diff] [blame] | 192 | } else if (matches(opt, "-details") == 0) { |
| 193 | ++show_details; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 194 | } else if (matches(opt, "-resolve") == 0) { |
| 195 | ++resolve_hosts; |
| 196 | } else if (matches(opt, "-oneline") == 0) { |
| 197 | ++oneline; |
shemminger | 90f9302 | 2005-06-07 21:55:55 +0000 | [diff] [blame] | 198 | } else if (matches(opt, "-timestamp") == 0) { |
| 199 | ++timestamp; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 200 | #if 0 |
| 201 | } else if (matches(opt, "-numeric") == 0) { |
| 202 | rtnl_names_numeric++; |
| 203 | #endif |
| 204 | } else if (matches(opt, "-Version") == 0) { |
| 205 | printf("ip utility, iproute2-ss%s\n", SNAPSHOT); |
| 206 | exit(0); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 207 | } else if (matches(opt, "-force") == 0) { |
| 208 | ++force; |
| 209 | } else if (matches(opt, "-batch") == 0) { |
| 210 | argc--; |
| 211 | argv++; |
| 212 | if (argc <= 1) |
| 213 | usage(); |
| 214 | batch_file = argv[1]; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 215 | } else if (matches(opt, "-help") == 0) { |
| 216 | usage(); |
| 217 | } else { |
| 218 | fprintf(stderr, "Option \"%s\" is unknown, try \"ip -help\".\n", opt); |
| 219 | exit(-1); |
| 220 | } |
| 221 | argc--; argv++; |
| 222 | } |
| 223 | |
| 224 | _SL_ = oneline ? "\\" : "\n" ; |
| 225 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 226 | if (batch_file) |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 227 | return batch(batch_file); |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 228 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 229 | if (rtnl_open(&rth, 0) < 0) |
| 230 | exit(1); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 231 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 232 | if (strlen(basename) > 2) |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 233 | return do_cmd(basename+2, argc, argv); |
| 234 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 235 | if (argc > 1) |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 236 | return do_cmd(argv[1], argc-1, argv+1); |
| 237 | |
| 238 | rtnl_close(&rth); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 239 | usage(); |
| 240 | } |