1
2
3
4
5
6
7
8
9
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
27static struct {
28 int family;
29 int ifindex;
30} filter;
31
32static const char * const rp_filter_names[] = {
33 "off", "strict", "loose"
34};
35
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
44static struct rtattr *netconf_rta(struct netconfmsg *ncm)
45{
46 return (struct rtattr *)((char *)ncm
47 + NLMSG_ALIGN(sizeof(struct netconfmsg)));
48}
49
50int print_netconf(struct rtnl_ctrl_data *ctrl, struct nlmsghdr *n, void *arg)
51{
52 FILE *fp = (FILE *)arg;
53 struct netconfmsg *ncm = NLMSG_DATA(n);
54 int len = n->nlmsg_len;
55 struct rtattr *tb[NETCONFA_MAX+1];
56 int ifindex = 0;
57
58 if (n->nlmsg_type == NLMSG_ERROR)
59 return -1;
60
61 if (n->nlmsg_type != RTM_NEWNETCONF &&
62 n->nlmsg_type != RTM_DELNETCONF) {
63 fprintf(stderr, "Not a netconf message: %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
77 parse_rtattr(tb, NETCONFA_MAX, netconf_rta(ncm),
78 NLMSG_PAYLOAD(n, sizeof(*ncm)));
79
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
86 open_json_object(NULL);
87 if (n->nlmsg_type == RTM_DELNETCONF)
88 print_bool(PRINT_ANY, "deleted", "Deleted ", true);
89
90 print_string(PRINT_ANY, "family",
91 "%s ", family_name(ncm->ncm_family));
92
93 if (tb[NETCONFA_IFINDEX]) {
94 const char *dev;
95
96 switch (ifindex) {
97 case NETCONFA_IFINDEX_ALL:
98 dev = "all";
99 break;
100 case NETCONFA_IFINDEX_DEFAULT:
101 dev = "default";
102 break;
103 default:
104 dev = ll_index_to_name(ifindex);
105 break;
106 }
107 print_color_string(PRINT_ANY, COLOR_IFNAME,
108 "interface", "%s ", dev);
109 }
110
111 if (tb[NETCONFA_FORWARDING])
112 print_on_off(PRINT_ANY, "forwarding", "forwarding %s ",
113 rta_getattr_u32(tb[NETCONFA_FORWARDING]));
114
115 if (tb[NETCONFA_RP_FILTER]) {
116 __u32 rp_filter = rta_getattr_u32(tb[NETCONFA_RP_FILTER]);
117
118 if (rp_filter < ARRAY_SIZE(rp_filter_names))
119 print_string(PRINT_ANY, "rp_filter",
120 "rp_filter %s ",
121 rp_filter_names[rp_filter]);
122 else
123 print_uint(PRINT_ANY, "rp_filter",
124 "rp_filter %u ", rp_filter);
125 }
126
127 if (tb[NETCONFA_MC_FORWARDING])
128 print_on_off(PRINT_ANY, "mc_forwarding", "mc_forwarding %s ",
129 rta_getattr_u32(tb[NETCONFA_MC_FORWARDING]));
130
131 if (tb[NETCONFA_PROXY_NEIGH])
132 print_on_off(PRINT_ANY, "proxy_neigh", "proxy_neigh %s ",
133 rta_getattr_u32(tb[NETCONFA_PROXY_NEIGH]));
134
135 if (tb[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN])
136 print_on_off(PRINT_ANY, "ignore_routes_with_linkdown",
137 "ignore_routes_with_linkdown %s ",
138 rta_getattr_u32(tb[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN]));
139
140 if (tb[NETCONFA_INPUT])
141 print_on_off(PRINT_ANY, "input", "input %s ",
142 rta_getattr_u32(tb[NETCONFA_INPUT]));
143
144 close_json_object();
145 print_string(PRINT_FP, NULL, "\n", NULL);
146 fflush(fp);
147 return 0;
148}
149
150static int print_netconf2(struct nlmsghdr *n, void *arg)
151{
152 return print_netconf(NULL, n, arg);
153}
154
155void ipnetconf_reset_filter(int ifindex)
156{
157 memset(&filter, 0, sizeof(filter));
158 filter.ifindex = ifindex;
159}
160
161static int do_show(int argc, char **argv)
162{
163 struct {
164 struct nlmsghdr n;
165 struct netconfmsg ncm;
166 char buf[1024];
167 } req = {
168 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct netconfmsg)),
169 .n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
170 .n.nlmsg_type = RTM_GETNETCONF,
171 };
172
173 ipnetconf_reset_filter(0);
174 filter.family = preferred_family;
175
176 while (argc > 0) {
177 if (strcmp(*argv, "dev") == 0) {
178 NEXT_ARG();
179 filter.ifindex = ll_name_to_index(*argv);
180 if (filter.ifindex <= 0) {
181 fprintf(stderr,
182 "Device \"%s\" does not exist.\n",
183 *argv);
184 return -1;
185 }
186 }
187 argv++; argc--;
188 }
189
190 ll_init_map(&rth);
191
192 if (filter.ifindex && filter.family != AF_UNSPEC) {
193 req.ncm.ncm_family = filter.family;
194 addattr_l(&req.n, sizeof(req), NETCONFA_IFINDEX,
195 &filter.ifindex, sizeof(filter.ifindex));
196
197 if (rtnl_send(&rth, &req.n, req.n.nlmsg_len) < 0) {
198 perror("Can not send request");
199 exit(1);
200 }
201 rtnl_listen(&rth, print_netconf, stdout);
202 } else {
203 rth.flags = RTNL_HANDLE_F_SUPPRESS_NLERR;
204dump:
205 if (rtnl_netconfdump_req(&rth, filter.family) < 0) {
206 perror("Cannot send dump request");
207 exit(1);
208 }
209
210 new_json_obj(json);
211 if (rtnl_dump_filter(&rth, print_netconf2, stdout) < 0) {
212
213
214
215 if (errno == EOPNOTSUPP &&
216 filter.family == AF_UNSPEC) {
217 filter.family = AF_INET;
218 goto dump;
219 }
220 perror("RTNETLINK answers");
221 fprintf(stderr, "Dump terminated\n");
222 exit(1);
223 }
224 delete_json_obj();
225 if (preferred_family == AF_UNSPEC && filter.family == AF_INET) {
226 preferred_family = AF_INET6;
227 filter.family = AF_INET6;
228 goto dump;
229 }
230 }
231 return 0;
232}
233
234int do_ipnetconf(int argc, char **argv)
235{
236 if (argc > 0) {
237 if (matches(*argv, "show") == 0 ||
238 matches(*argv, "lst") == 0 ||
239 matches(*argv, "list") == 0)
240 return do_show(argc-1, argv+1);
241 if (matches(*argv, "help") == 0)
242 usage();
243 } else
244 return do_show(0, NULL);
245
246 fprintf(stderr,
247 "Command \"%s\" is unknown, try \"ip netconf help\".\n",
248 *argv);
249 exit(-1);
250}
251