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