2 * ipnetconf.c "ip netconf".
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.
9 * Authors: Nicolas Dichtel, <nicolas.dichtel@6wind.com>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
25 #include "ip_common.h"
32 static const char * const rp_filter_names
[] = {
33 "off", "strict", "loose"
36 static void usage(void) __attribute__((noreturn
));
38 static void usage(void)
40 fprintf(stderr
, "Usage: ip netconf show [ dev STRING ]\n");
44 static void print_onoff(FILE *fp
, const char *flag
, __u32 val
)
46 if (is_json_context())
47 print_bool(PRINT_JSON
, flag
, NULL
, val
);
49 fprintf(fp
, "%s %s ", flag
, val
? "on" : "off");
52 static struct rtattr
*netconf_rta(struct netconfmsg
*ncm
)
54 return (struct rtattr
*)((char *)ncm
55 + NLMSG_ALIGN(sizeof(struct netconfmsg
)));
58 int print_netconf(const struct sockaddr_nl
*who
, struct rtnl_ctrl_data
*ctrl
,
59 struct nlmsghdr
*n
, void *arg
)
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];
67 if (n
->nlmsg_type
== NLMSG_ERROR
)
70 if (n
->nlmsg_type
!= RTM_NEWNETCONF
&&
71 n
->nlmsg_type
!= RTM_DELNETCONF
) {
72 fprintf(stderr
, "Not a netconf message: %08x %08x %08x\n",
73 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
77 len
-= NLMSG_SPACE(sizeof(*ncm
));
79 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
83 if (filter
.family
&& filter
.family
!= ncm
->ncm_family
)
86 parse_rtattr(tb
, NETCONFA_MAX
, netconf_rta(ncm
),
87 NLMSG_PAYLOAD(n
, sizeof(*ncm
)));
89 if (tb
[NETCONFA_IFINDEX
])
90 ifindex
= rta_getattr_u32(tb
[NETCONFA_IFINDEX
]);
92 if (filter
.ifindex
&& filter
.ifindex
!= ifindex
)
95 open_json_object(NULL
);
96 if (n
->nlmsg_type
== RTM_DELNETCONF
)
97 print_bool(PRINT_ANY
, "deleted", "Deleted ", true);
99 print_string(PRINT_ANY
, "family",
100 "%s ", family_name(ncm
->ncm_family
));
102 if (tb
[NETCONFA_IFINDEX
]) {
106 case NETCONFA_IFINDEX_ALL
:
109 case NETCONFA_IFINDEX_DEFAULT
:
113 dev
= ll_index_to_name(ifindex
);
116 print_color_string(PRINT_ANY
, COLOR_IFNAME
,
117 "interface", "%s ", dev
);
120 if (tb
[NETCONFA_FORWARDING
])
121 print_onoff(fp
, "forwarding",
122 rta_getattr_u32(tb
[NETCONFA_FORWARDING
]));
124 if (tb
[NETCONFA_RP_FILTER
]) {
125 __u32 rp_filter
= rta_getattr_u32(tb
[NETCONFA_RP_FILTER
]);
127 if (rp_filter
< ARRAY_SIZE(rp_filter_names
))
128 print_string(PRINT_ANY
, "rp_filter",
130 rp_filter_names
[rp_filter
]);
132 print_uint(PRINT_ANY
, "rp_filter",
133 "rp_filter %u ", rp_filter
);
136 if (tb
[NETCONFA_MC_FORWARDING
])
137 print_onoff(fp
, "mc_forwarding",
138 rta_getattr_u32(tb
[NETCONFA_MC_FORWARDING
]));
140 if (tb
[NETCONFA_PROXY_NEIGH
])
141 print_onoff(fp
, "proxy_neigh",
142 rta_getattr_u32(tb
[NETCONFA_PROXY_NEIGH
]));
144 if (tb
[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
])
145 print_onoff(fp
, "ignore_routes_with_linkdown",
146 rta_getattr_u32(tb
[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
]));
148 if (tb
[NETCONFA_INPUT
])
149 print_onoff(fp
, "input", rta_getattr_u32(tb
[NETCONFA_INPUT
]));
152 print_string(PRINT_FP
, NULL
, "\n", NULL
);
157 static int print_netconf2(const struct sockaddr_nl
*who
,
158 struct nlmsghdr
*n
, void *arg
)
160 return print_netconf(who
, NULL
, n
, arg
);
163 void ipnetconf_reset_filter(int ifindex
)
165 memset(&filter
, 0, sizeof(filter
));
166 filter
.ifindex
= ifindex
;
169 static int do_show(int argc
, char **argv
)
173 struct netconfmsg ncm
;
176 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct netconfmsg
)),
177 .n
.nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
,
178 .n
.nlmsg_type
= RTM_GETNETCONF
,
181 ipnetconf_reset_filter(0);
182 filter
.family
= preferred_family
;
185 if (strcmp(*argv
, "dev") == 0) {
187 filter
.ifindex
= ll_name_to_index(*argv
);
188 if (filter
.ifindex
<= 0) {
190 "Device \"%s\" does not exist.\n",
200 if (filter
.ifindex
&& filter
.family
!= AF_UNSPEC
) {
201 req
.ncm
.ncm_family
= filter
.family
;
202 addattr_l(&req
.n
, sizeof(req
), NETCONFA_IFINDEX
,
203 &filter
.ifindex
, sizeof(filter
.ifindex
));
205 if (rtnl_send(&rth
, &req
.n
, req
.n
.nlmsg_len
) < 0) {
206 perror("Can not send request");
209 rtnl_listen(&rth
, print_netconf
, stdout
);
211 rth
.flags
= RTNL_HANDLE_F_SUPPRESS_NLERR
;
213 if (rtnl_wilddump_request(&rth
, filter
.family
,
214 RTM_GETNETCONF
) < 0) {
215 perror("Cannot send dump request");
220 if (rtnl_dump_filter(&rth
, print_netconf2
, stdout
) < 0) {
221 /* kernel does not support netconf dump on AF_UNSPEC;
222 * fall back to requesting by family
224 if (errno
== EOPNOTSUPP
&&
225 filter
.family
== AF_UNSPEC
) {
226 filter
.family
= AF_INET
;
229 perror("RTNETLINK answers");
230 fprintf(stderr
, "Dump terminated\n");
234 if (preferred_family
== AF_UNSPEC
&& filter
.family
== AF_INET
) {
235 preferred_family
= AF_INET6
;
236 filter
.family
= AF_INET6
;
243 int do_ipnetconf(int argc
, char **argv
)
246 if (matches(*argv
, "show") == 0 ||
247 matches(*argv
, "lst") == 0 ||
248 matches(*argv
, "list") == 0)
249 return do_show(argc
-1, argv
+1);
250 if (matches(*argv
, "help") == 0)
253 return do_show(0, NULL
);
256 "Command \"%s\" is unknown, try \"ip netconf help\".\n",