]> rtime.felk.cvut.cz Git - lisovros/iproute2_canprio.git/blob - ip/ip.c
Add reference to tc-codel(8) to the SEE ALSO section
[lisovros/iproute2_canprio.git] / ip / ip.c
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 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <syslog.h>
16 #include <fcntl.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <string.h>
20 #include <errno.h>
21
22 #include "SNAPSHOT.h"
23 #include "utils.h"
24 #include "ip_common.h"
25
26 int preferred_family = AF_UNSPEC;
27 int show_stats = 0;
28 int show_details = 0;
29 int resolve_hosts = 0;
30 int oneline = 0;
31 int timestamp = 0;
32 char * _SL_ = NULL;
33 char *batch_file = NULL;
34 int force = 0;
35 int max_flush_loops = 10;
36
37 struct rtnl_handle rth = { .fd = -1 };
38
39 static void usage(void) __attribute__((noreturn));
40
41 static void usage(void)
42 {
43         fprintf(stderr,
44 "Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }\n"
45 "       ip [ -force ] -batch filename\n"
46 "where  OBJECT := { link | addr | addrlabel | route | rule | neigh | ntable |\n"
47 "                   tunnel | tuntap | maddr | mroute | mrule | monitor | xfrm |\n"
48 "                   netns | l2tp }\n"
49 "       OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |\n"
50 "                    -f[amily] { inet | inet6 | ipx | dnet | link } |\n"
51 "                    -l[oops] { maximum-addr-flush-attempts } |\n"
52 "                    -o[neline] | -t[imestamp] | -b[atch] [filename] |\n"
53 "                    -rc[vbuf] [size]}\n");
54         exit(-1);
55 }
56
57 static int do_help(int argc, char **argv)
58 {
59         usage();
60 }
61
62 static const struct cmd {
63         const char *cmd;
64         int (*func)(int argc, char **argv);
65 } cmds[] = {
66         { "address",    do_ipaddr },
67         { "addrlabel",  do_ipaddrlabel },
68         { "maddress",   do_multiaddr },
69         { "route",      do_iproute },
70         { "rule",       do_iprule },
71         { "neighbor",   do_ipneigh },
72         { "neighbour",  do_ipneigh },
73         { "ntable",     do_ipntable },
74         { "ntbl",       do_ipntable },
75         { "link",       do_iplink },
76         { "l2tp",       do_ipl2tp },
77         { "tunnel",     do_iptunnel },
78         { "tunl",       do_iptunnel },
79         { "tuntap",     do_iptuntap },
80         { "tap",        do_iptuntap },
81         { "monitor",    do_ipmonitor },
82         { "xfrm",       do_xfrm },
83         { "mroute",     do_multiroute },
84         { "mrule",      do_multirule },
85         { "netns",      do_netns },
86         { "help",       do_help },
87         { 0 }
88 };
89
90 static int do_cmd(const char *argv0, int argc, char **argv)
91 {
92         const struct cmd *c;
93
94         for (c = cmds; c->cmd; ++c) {
95                 if (matches(argv0, c->cmd) == 0) {
96                         return -(c->func(argc-1, argv+1));
97                 }
98         }
99
100         fprintf(stderr, "Object \"%s\" is unknown, try \"ip help\".\n", argv0);
101         return EXIT_FAILURE;
102 }
103
104 static int batch(const char *name)
105 {
106         char *line = NULL;
107         size_t len = 0;
108         int ret = EXIT_SUCCESS;
109
110         if (name && strcmp(name, "-") != 0) {
111                 if (freopen(name, "r", stdin) == NULL) {
112                         fprintf(stderr, "Cannot open file \"%s\" for reading: %s\n",
113                                 name, strerror(errno));
114                         return EXIT_FAILURE;
115                 }
116         }
117
118         if (rtnl_open(&rth, 0) < 0) {
119                 fprintf(stderr, "Cannot open rtnetlink\n");
120                 return EXIT_FAILURE;
121         }
122
123         cmdlineno = 0;
124         while (getcmdline(&line, &len, stdin) != -1) {
125                 char *largv[100];
126                 int largc;
127
128                 largc = makeargs(line, largv, 100);
129                 if (largc == 0)
130                         continue;       /* blank line */
131
132                 if (do_cmd(largv[0], largc, largv)) {
133                         fprintf(stderr, "Command failed %s:%d\n", name, cmdlineno);
134                         ret = EXIT_FAILURE;
135                         if (!force)
136                                 break;
137                 }
138         }
139         if (line)
140                 free(line);
141
142         rtnl_close(&rth);
143         return ret;
144 }
145
146
147 int main(int argc, char **argv)
148 {
149         char *basename;
150
151         basename = strrchr(argv[0], '/');
152         if (basename == NULL)
153                 basename = argv[0];
154         else
155                 basename++;
156
157         while (argc > 1) {
158                 char *opt = argv[1];
159                 if (strcmp(opt,"--") == 0) {
160                         argc--; argv++;
161                         break;
162                 }
163                 if (opt[0] != '-')
164                         break;
165                 if (opt[1] == '-')
166                         opt++;
167                 if (matches(opt, "-loops") == 0) {
168                         argc--;
169                         argv++;
170                         if (argc <= 1)
171                                 usage();
172                         max_flush_loops = atoi(argv[1]);
173                 } else if (matches(opt, "-family") == 0) {
174                         argc--;
175                         argv++;
176                         if (argc <= 1)
177                                 usage();
178                         if (strcmp(argv[1], "inet") == 0)
179                                 preferred_family = AF_INET;
180                         else if (strcmp(argv[1], "inet6") == 0)
181                                 preferred_family = AF_INET6;
182                         else if (strcmp(argv[1], "dnet") == 0)
183                                 preferred_family = AF_DECnet;
184                         else if (strcmp(argv[1], "link") == 0)
185                                 preferred_family = AF_PACKET;
186                         else if (strcmp(argv[1], "ipx") == 0)
187                                 preferred_family = AF_IPX;
188                         else if (strcmp(argv[1], "help") == 0)
189                                 usage();
190                         else
191                                 invarg(argv[1], "invalid protocol family");
192                 } else if (strcmp(opt, "-4") == 0) {
193                         preferred_family = AF_INET;
194                 } else if (strcmp(opt, "-6") == 0) {
195                         preferred_family = AF_INET6;
196                 } else if (strcmp(opt, "-0") == 0) {
197                         preferred_family = AF_PACKET;
198                 } else if (strcmp(opt, "-I") == 0) {
199                         preferred_family = AF_IPX;
200                 } else if (strcmp(opt, "-D") == 0) {
201                         preferred_family = AF_DECnet;
202                 } else if (matches(opt, "-stats") == 0 ||
203                            matches(opt, "-statistics") == 0) {
204                         ++show_stats;
205                 } else if (matches(opt, "-details") == 0) {
206                         ++show_details;
207                 } else if (matches(opt, "-resolve") == 0) {
208                         ++resolve_hosts;
209                 } else if (matches(opt, "-oneline") == 0) {
210                         ++oneline;
211                 } else if (matches(opt, "-timestamp") == 0) {
212                         ++timestamp;
213 #if 0
214                 } else if (matches(opt, "-numeric") == 0) {
215                         rtnl_names_numeric++;
216 #endif
217                 } else if (matches(opt, "-Version") == 0) {
218                         printf("ip utility, iproute2-ss%s\n", SNAPSHOT);
219                         exit(0);
220                 } else if (matches(opt, "-force") == 0) {
221                         ++force;
222                 } else if (matches(opt, "-batch") == 0) {
223                         argc--;
224                         argv++;
225                         if (argc <= 1)
226                                 usage();
227                         batch_file = argv[1];
228                 } else if (matches(opt, "-rcvbuf") == 0) {
229                         unsigned int size;
230
231                         argc--;
232                         argv++;
233                         if (argc <= 1)
234                                 usage();
235                         if (get_unsigned(&size, argv[1], 0)) {
236                                 fprintf(stderr, "Invalid rcvbuf size '%s'\n",
237                                         argv[1]);
238                                 exit(-1);
239                         }
240                         rcvbuf = size;
241                 } else if (matches(opt, "-help") == 0) {
242                         usage();
243                 } else {
244                         fprintf(stderr, "Option \"%s\" is unknown, try \"ip -help\".\n", opt);
245                         exit(-1);
246                 }
247                 argc--; argv++;
248         }
249
250         _SL_ = oneline ? "\\" : "\n" ;
251
252         if (batch_file)
253                 return batch(batch_file);
254
255         if (rtnl_open(&rth, 0) < 0)
256                 exit(1);
257
258         if (strlen(basename) > 2)
259                 return do_cmd(basename+2, argc, argv);
260
261         if (argc > 1)
262                 return do_cmd(argv[1], argc-1, argv+1);
263
264         rtnl_close(&rth);
265         usage();
266 }