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 void usage(void) __attribute__((noreturn
));
34 static void usage(void)
36 fprintf(stderr
, "Usage: ip netconf show [ dev STRING ]\n");
40 static void print_onoff(FILE *f
, const char *flag
, __u32 val
)
42 fprintf(f
, "%s %s ", flag
, val
? "on" : "off");
45 static struct rtattr
*netconf_rta(struct netconfmsg
*ncm
)
47 return (struct rtattr
*)((char *)ncm
48 + NLMSG_ALIGN(sizeof(struct netconfmsg
)));
51 int print_netconf(const struct sockaddr_nl
*who
, struct rtnl_ctrl_data
*ctrl
,
52 struct nlmsghdr
*n
, void *arg
)
54 FILE *fp
= (FILE *)arg
;
55 struct netconfmsg
*ncm
= NLMSG_DATA(n
);
56 int len
= n
->nlmsg_len
;
57 struct rtattr
*tb
[NETCONFA_MAX
+1];
60 if (n
->nlmsg_type
== NLMSG_ERROR
)
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
);
68 len
-= NLMSG_SPACE(sizeof(*ncm
));
70 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
74 if (filter
.family
&& filter
.family
!= ncm
->ncm_family
)
77 parse_rtattr(tb
, NETCONFA_MAX
, netconf_rta(ncm
),
78 NLMSG_PAYLOAD(n
, sizeof(*ncm
)));
80 if (tb
[NETCONFA_IFINDEX
])
81 ifindex
= rta_getattr_u32(tb
[NETCONFA_IFINDEX
]);
83 if (filter
.ifindex
&& filter
.ifindex
!= ifindex
)
86 switch (ncm
->ncm_family
) {
97 fprintf(fp
, "unknown ");
101 if (tb
[NETCONFA_IFINDEX
]) {
103 case NETCONFA_IFINDEX_ALL
:
106 case NETCONFA_IFINDEX_DEFAULT
:
107 fprintf(fp
, "default ");
110 fprintf(fp
, "dev %s ", ll_index_to_name(ifindex
));
115 if (tb
[NETCONFA_FORWARDING
])
116 print_onoff(fp
, "forwarding",
117 rta_getattr_u32(tb
[NETCONFA_FORWARDING
]));
118 if (tb
[NETCONFA_RP_FILTER
]) {
119 __u32 rp_filter
= rta_getattr_u32(tb
[NETCONFA_RP_FILTER
]);
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 ");
128 fprintf(fp
, "rp_filter unknown mode ");
130 if (tb
[NETCONFA_MC_FORWARDING
])
131 print_onoff(fp
, "mc_forwarding",
132 rta_getattr_u32(tb
[NETCONFA_MC_FORWARDING
]));
134 if (tb
[NETCONFA_PROXY_NEIGH
])
135 print_onoff(fp
, "proxy_neigh",
136 rta_getattr_u32(tb
[NETCONFA_PROXY_NEIGH
]));
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
]));
142 if (tb
[NETCONFA_INPUT
])
143 print_onoff(fp
, "input", rta_getattr_u32(tb
[NETCONFA_INPUT
]));
150 static int print_netconf2(const struct sockaddr_nl
*who
,
151 struct nlmsghdr
*n
, void *arg
)
153 return print_netconf(who
, NULL
, n
, arg
);
156 void ipnetconf_reset_filter(int ifindex
)
158 memset(&filter
, 0, sizeof(filter
));
159 filter
.ifindex
= ifindex
;
162 static int do_show(int argc
, char **argv
)
166 struct netconfmsg ncm
;
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
,
174 ipnetconf_reset_filter(0);
175 filter
.family
= preferred_family
;
178 if (strcmp(*argv
, "dev") == 0) {
180 filter
.ifindex
= ll_name_to_index(*argv
);
181 if (filter
.ifindex
<= 0) {
182 fprintf(stderr
, "Device \"%s\" does not exist.\n",
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
));
197 if (rtnl_send(&rth
, &req
.n
, req
.n
.nlmsg_len
) < 0) {
198 perror("Can not send request");
201 rtnl_listen(&rth
, print_netconf
, stdout
);
203 rth
.flags
= RTNL_HANDLE_F_SUPPRESS_NLERR
;
205 if (rtnl_wilddump_request(&rth
, filter
.family
, RTM_GETNETCONF
) < 0) {
206 perror("Cannot send dump request");
209 if (rtnl_dump_filter(&rth
, print_netconf2
, stdout
) < 0) {
210 /* kernel does not support netconf dump on AF_UNSPEC;
211 * fall back to requesting by family
213 if (errno
== EOPNOTSUPP
&&
214 filter
.family
== AF_UNSPEC
) {
215 filter
.family
= AF_INET
;
218 perror("RTNETLINK answers");
219 fprintf(stderr
, "Dump terminated\n");
222 if (preferred_family
== AF_UNSPEC
&& filter
.family
== AF_INET
) {
223 preferred_family
= AF_INET6
;
224 filter
.family
= AF_INET6
;
231 int do_ipnetconf(int argc
, char **argv
)
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)
241 return do_show(0, NULL
);
243 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip netconf help\".\n", *argv
);