]> rtime.felk.cvut.cz Git - lisovros/iproute2_canprio.git/blob - ip/ip.c
Add 'ip tuntap' support.
[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  * 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>
25 #include <errno.h>
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;
33 int show_details = 0;
34 int resolve_hosts = 0;
35 int oneline = 0;
36 int timestamp = 0;
37 char * _SL_ = NULL;
38 char *batch_file = NULL;
39 int force = 0;
40 struct rtnl_handle rth = { .fd = -1 };
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"
48 "       ip [ -force ] -batch filename\n"
49 "where  OBJECT := { link | addr | addrlabel | route | rule | neigh | ntable |\n"
50 "                   tunnel | tuntap | maddr | mroute | monitor | xfrm }\n"
51 "       OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |\n"
52 "                    -f[amily] { inet | inet6 | ipx | dnet | link } |\n"
53 "                    -o[neline] | -t[imestamp] | -b[atch] [filename] }\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         { "tunnel",     do_iptunnel },
77         { "tunl",       do_iptunnel },
78         { "tuntap",     do_iptuntap },
79         { "tap",        do_iptuntap },
80         { "monitor",    do_ipmonitor },
81         { "xfrm",       do_xfrm },
82         { "mroute",     do_multiroute },
83         { "help",       do_help },
84         { 0 }
85 };
86
87 static int do_cmd(const char *argv0, int argc, char **argv)
88 {
89         const struct cmd *c;
90
91         for (c = cmds; c->cmd; ++c) {
92                 if (matches(argv0, c->cmd) == 0)
93                         return c->func(argc-1, argv+1);
94         }
95
96         fprintf(stderr, "Object \"%s\" is unknown, try \"ip help\".\n", argv0);
97         return -1;
98 }
99
100 static int batch(const char *name)
101 {
102         char *line = NULL;
103         size_t len = 0;
104         int ret = 0;
105         int lineno = 0;
106
107         if (name && strcmp(name, "-") != 0) {
108                 if (freopen(name, "r", stdin) == NULL) {
109                         fprintf(stderr, "Cannot open file \"%s\" for reading: %s\n",
110                                 name, strerror(errno));
111                         return -1;
112                 }
113         }
114
115         if (rtnl_open(&rth, 0) < 0) {
116                 fprintf(stderr, "Cannot open rtnetlink\n");
117                 return -1;
118         }
119
120         while (getcmdline(&line, &len, stdin) != -1) {
121                 char *largv[100];
122                 int largc;
123
124                 largc = makeargs(line, largv, 100);
125                 if (largc == 0)
126                         continue;       /* blank line */
127
128                 if (do_cmd(largv[0], largc, largv)) {
129                         fprintf(stderr, "Command failed %s:%d\n", name, lineno);
130                         ret = 1;
131                         if (!force)
132                                 break;
133                 }
134         }
135         if (line)
136                 free(line);
137
138         rtnl_close(&rth);
139         return ret;
140 }
141
142
143 int main(int argc, char **argv)
144 {
145         char *basename;
146
147         basename = strrchr(argv[0], '/');
148         if (basename == NULL)
149                 basename = argv[0];
150         else
151                 basename++;
152
153         while (argc > 1) {
154                 char *opt = argv[1];
155                 if (strcmp(opt,"--") == 0) {
156                         argc--; argv++;
157                         break;
158                 }
159                 if (opt[0] != '-')
160                         break;
161                 if (opt[1] == '-')
162                         opt++;
163                 if (matches(opt, "-family") == 0) {
164                         argc--;
165                         argv++;
166                         if (argc <= 1)
167                                 usage();
168                         if (strcmp(argv[1], "inet") == 0)
169                                 preferred_family = AF_INET;
170                         else if (strcmp(argv[1], "inet6") == 0)
171                                 preferred_family = AF_INET6;
172                         else if (strcmp(argv[1], "dnet") == 0)
173                                 preferred_family = AF_DECnet;
174                         else if (strcmp(argv[1], "link") == 0)
175                                 preferred_family = AF_PACKET;
176                         else if (strcmp(argv[1], "ipx") == 0)
177                                 preferred_family = AF_IPX;
178                         else if (strcmp(argv[1], "help") == 0)
179                                 usage();
180                         else
181                                 invarg(argv[1], "invalid protocol family");
182                 } else if (strcmp(opt, "-4") == 0) {
183                         preferred_family = AF_INET;
184                 } else if (strcmp(opt, "-6") == 0) {
185                         preferred_family = AF_INET6;
186                 } else if (strcmp(opt, "-0") == 0) {
187                         preferred_family = AF_PACKET;
188                 } else if (strcmp(opt, "-I") == 0) {
189                         preferred_family = AF_IPX;
190                 } else if (strcmp(opt, "-D") == 0) {
191                         preferred_family = AF_DECnet;
192                 } else if (matches(opt, "-stats") == 0 ||
193                            matches(opt, "-statistics") == 0) {
194                         ++show_stats;
195                 } else if (matches(opt, "-details") == 0) {
196                         ++show_details;
197                 } else if (matches(opt, "-resolve") == 0) {
198                         ++resolve_hosts;
199                 } else if (matches(opt, "-oneline") == 0) {
200                         ++oneline;
201                 } else if (matches(opt, "-timestamp") == 0) {
202                         ++timestamp;
203 #if 0
204                 } else if (matches(opt, "-numeric") == 0) {
205                         rtnl_names_numeric++;
206 #endif
207                 } else if (matches(opt, "-Version") == 0) {
208                         printf("ip utility, iproute2-ss%s\n", SNAPSHOT);
209                         exit(0);
210                 } else if (matches(opt, "-force") == 0) {
211                         ++force;
212                 } else if (matches(opt, "-batch") == 0) {
213                         argc--;
214                         argv++;
215                         if (argc <= 1)
216                                 usage();
217                         batch_file = argv[1];
218                 } else if (matches(opt, "-help") == 0) {
219                         usage();
220                 } else {
221                         fprintf(stderr, "Option \"%s\" is unknown, try \"ip -help\".\n", opt);
222                         exit(-1);
223                 }
224                 argc--; argv++;
225         }
226
227         _SL_ = oneline ? "\\" : "\n" ;
228
229         if (batch_file)
230                 return batch(batch_file);
231
232         if (rtnl_open(&rth, 0) < 0)
233                 exit(1);
234
235         if (strlen(basename) > 2)
236                 return do_cmd(basename+2, argc, argv);
237
238         if (argc > 1)
239                 return do_cmd(argv[1], argc-1, argv+1);
240
241         rtnl_close(&rth);
242         usage();
243 }