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