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