]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/ipnetconf.c
03f98ace91450fb1c5260a9fcac73169321a76a3
[mirror_iproute2.git] / ip / ipnetconf.c
1 /*
2 * ipnetconf.c "ip netconf".
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: Nicolas Dichtel, <nicolas.dichtel@6wind.com>
10 *
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17 #include <string.h>
18 #include <sys/time.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <errno.h>
22
23 #include "rt_names.h"
24 #include "utils.h"
25 #include "ip_common.h"
26
27 static struct {
28 int family;
29 int ifindex;
30 } filter;
31
32 static const char * const rp_filter_names[] = {
33 "off", "strict", "loose"
34 };
35
36 static void usage(void) __attribute__((noreturn));
37
38 static void usage(void)
39 {
40 fprintf(stderr, "Usage: ip netconf show [ dev STRING ]\n");
41 exit(-1);
42 }
43
44 static void print_onoff(FILE *fp, const char *flag, __u32 val)
45 {
46 if (is_json_context())
47 print_bool(PRINT_JSON, flag, NULL, val);
48 else
49 fprintf(fp, "%s %s ", flag, val ? "on" : "off");
50 }
51
52 static struct rtattr *netconf_rta(struct netconfmsg *ncm)
53 {
54 return (struct rtattr *)((char *)ncm
55 + NLMSG_ALIGN(sizeof(struct netconfmsg)));
56 }
57
58 int print_netconf(const struct sockaddr_nl *who, struct rtnl_ctrl_data *ctrl,
59 struct nlmsghdr *n, void *arg)
60 {
61 FILE *fp = (FILE *)arg;
62 struct netconfmsg *ncm = NLMSG_DATA(n);
63 int len = n->nlmsg_len;
64 struct rtattr *tb[NETCONFA_MAX+1];
65 int ifindex = 0;
66
67 if (n->nlmsg_type == NLMSG_ERROR)
68 return -1;
69 if (n->nlmsg_type != RTM_NEWNETCONF) {
70 fprintf(stderr, "Not RTM_NEWNETCONF: %08x %08x %08x\n",
71 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
72
73 return -1;
74 }
75 len -= NLMSG_SPACE(sizeof(*ncm));
76 if (len < 0) {
77 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
78 return -1;
79 }
80
81 if (filter.family && filter.family != ncm->ncm_family)
82 return 0;
83
84 parse_rtattr(tb, NETCONFA_MAX, netconf_rta(ncm),
85 NLMSG_PAYLOAD(n, sizeof(*ncm)));
86
87 if (tb[NETCONFA_IFINDEX])
88 ifindex = rta_getattr_u32(tb[NETCONFA_IFINDEX]);
89
90 if (filter.ifindex && filter.ifindex != ifindex)
91 return 0;
92
93 open_json_object(NULL);
94 print_string(PRINT_ANY, "family",
95 "%s ", family_name(ncm->ncm_family));
96
97 if (tb[NETCONFA_IFINDEX]) {
98 const char *dev;
99
100 switch (ifindex) {
101 case NETCONFA_IFINDEX_ALL:
102 dev = "all";
103 break;
104 case NETCONFA_IFINDEX_DEFAULT:
105 dev = "default";
106 break;
107 default:
108 dev = ll_index_to_name(ifindex);
109 break;
110 }
111 print_color_string(PRINT_ANY, COLOR_IFNAME,
112 "interface", "%s ", dev);
113 }
114
115 if (tb[NETCONFA_FORWARDING])
116 print_onoff(fp, "forwarding",
117 rta_getattr_u32(tb[NETCONFA_FORWARDING]));
118
119 if (tb[NETCONFA_RP_FILTER]) {
120 __u32 rp_filter = rta_getattr_u32(tb[NETCONFA_RP_FILTER]);
121
122 if (rp_filter < ARRAY_SIZE(rp_filter_names))
123 print_string(PRINT_ANY, "rp_filter",
124 "rp_filter %s ",
125 rp_filter_names[rp_filter]);
126 else
127 print_uint(PRINT_ANY, "rp_filter",
128 "rp_filter %u ", rp_filter);
129 }
130
131 if (tb[NETCONFA_MC_FORWARDING])
132 print_onoff(fp, "mc_forwarding",
133 rta_getattr_u32(tb[NETCONFA_MC_FORWARDING]));
134
135 if (tb[NETCONFA_PROXY_NEIGH])
136 print_onoff(fp, "proxy_neigh",
137 rta_getattr_u32(tb[NETCONFA_PROXY_NEIGH]));
138
139 if (tb[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN])
140 print_onoff(fp, "ignore_routes_with_linkdown",
141 rta_getattr_u32(tb[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN]));
142
143 if (tb[NETCONFA_INPUT])
144 print_onoff(fp, "input", rta_getattr_u32(tb[NETCONFA_INPUT]));
145
146 close_json_object();
147 print_string(PRINT_FP, NULL, "\n", NULL);
148 fflush(fp);
149 return 0;
150 }
151
152 static int print_netconf2(const struct sockaddr_nl *who,
153 struct nlmsghdr *n, void *arg)
154 {
155 return print_netconf(who, NULL, n, arg);
156 }
157
158 void ipnetconf_reset_filter(int ifindex)
159 {
160 memset(&filter, 0, sizeof(filter));
161 filter.ifindex = ifindex;
162 }
163
164 static int do_show(int argc, char **argv)
165 {
166 struct {
167 struct nlmsghdr n;
168 struct netconfmsg ncm;
169 char buf[1024];
170 } req = {
171 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct netconfmsg)),
172 .n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
173 .n.nlmsg_type = RTM_GETNETCONF,
174 };
175
176 ipnetconf_reset_filter(0);
177 filter.family = preferred_family;
178
179 while (argc > 0) {
180 if (strcmp(*argv, "dev") == 0) {
181 NEXT_ARG();
182 filter.ifindex = ll_name_to_index(*argv);
183 if (filter.ifindex <= 0) {
184 fprintf(stderr,
185 "Device \"%s\" does not exist.\n",
186 *argv);
187 return -1;
188 }
189 }
190 argv++; argc--;
191 }
192
193 ll_init_map(&rth);
194
195 if (filter.ifindex && filter.family != AF_UNSPEC) {
196 req.ncm.ncm_family = filter.family;
197 addattr_l(&req.n, sizeof(req), NETCONFA_IFINDEX,
198 &filter.ifindex, sizeof(filter.ifindex));
199
200 if (rtnl_send(&rth, &req.n, req.n.nlmsg_len) < 0) {
201 perror("Can not send request");
202 exit(1);
203 }
204 rtnl_listen(&rth, print_netconf, stdout);
205 } else {
206 rth.flags = RTNL_HANDLE_F_SUPPRESS_NLERR;
207 dump:
208 if (rtnl_wilddump_request(&rth, filter.family,
209 RTM_GETNETCONF) < 0) {
210 perror("Cannot send dump request");
211 exit(1);
212 }
213
214 new_json_obj(json);
215 if (rtnl_dump_filter(&rth, print_netconf2, stdout) < 0) {
216 /* kernel does not support netconf dump on AF_UNSPEC;
217 * fall back to requesting by family
218 */
219 if (errno == EOPNOTSUPP &&
220 filter.family == AF_UNSPEC) {
221 filter.family = AF_INET;
222 goto dump;
223 }
224 perror("RTNETLINK answers");
225 fprintf(stderr, "Dump terminated\n");
226 exit(1);
227 }
228 delete_json_obj();
229 if (preferred_family == AF_UNSPEC && filter.family == AF_INET) {
230 preferred_family = AF_INET6;
231 filter.family = AF_INET6;
232 goto dump;
233 }
234 }
235 return 0;
236 }
237
238 int do_ipnetconf(int argc, char **argv)
239 {
240 if (argc > 0) {
241 if (matches(*argv, "show") == 0 ||
242 matches(*argv, "lst") == 0 ||
243 matches(*argv, "list") == 0)
244 return do_show(argc-1, argv+1);
245 if (matches(*argv, "help") == 0)
246 usage();
247 } else
248 return do_show(0, NULL);
249
250 fprintf(stderr,
251 "Command \"%s\" is unknown, try \"ip netconf help\".\n",
252 *argv);
253 exit(-1);
254 }