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>
20 #include <sys/socket.h>
21 #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];
59 if (n
->nlmsg_type
== NLMSG_ERROR
)
61 if (n
->nlmsg_type
!= RTM_NEWNETCONF
) {
62 fprintf(stderr
, "Not RTM_NEWNETCONF: %08x %08x %08x\n",
63 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
67 len
-= NLMSG_SPACE(sizeof(*ncm
));
69 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
73 if (filter
.family
&& filter
.family
!= ncm
->ncm_family
)
76 parse_rtattr(tb
, NETCONFA_MAX
, netconf_rta(ncm
),
77 NLMSG_PAYLOAD(n
, sizeof(*ncm
)));
79 switch (ncm
->ncm_family
) {
90 fprintf(fp
, "unknown ");
94 if (tb
[NETCONFA_IFINDEX
]) {
95 int *ifindex
= (int *)rta_getattr_str(tb
[NETCONFA_IFINDEX
]);
98 case NETCONFA_IFINDEX_ALL
:
101 case NETCONFA_IFINDEX_DEFAULT
:
102 fprintf(fp
, "default ");
105 fprintf(fp
, "dev %s ", ll_index_to_name(*ifindex
));
110 if (tb
[NETCONFA_FORWARDING
])
111 print_onoff(fp
, "forwarding",
112 rta_getattr_u32(tb
[NETCONFA_FORWARDING
]));
113 if (tb
[NETCONFA_RP_FILTER
]) {
114 __u32 rp_filter
= rta_getattr_u32(tb
[NETCONFA_RP_FILTER
]);
117 fprintf(fp
, "rp_filter off ");
118 else if (rp_filter
== 1)
119 fprintf(fp
, "rp_filter strict ");
120 else if (rp_filter
== 2)
121 fprintf(fp
, "rp_filter loose ");
123 fprintf(fp
, "rp_filter unknown mode ");
125 if (tb
[NETCONFA_MC_FORWARDING
])
126 print_onoff(fp
, "mc_forwarding",
127 rta_getattr_u32(tb
[NETCONFA_MC_FORWARDING
]));
129 if (tb
[NETCONFA_PROXY_NEIGH
])
130 print_onoff(fp
, "proxy_neigh",
131 rta_getattr_u32(tb
[NETCONFA_PROXY_NEIGH
]));
133 if (tb
[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
])
134 print_onoff(fp
, "ignore_routes_with_linkdown",
135 rta_getattr_u32(tb
[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
]));
137 if (tb
[NETCONFA_INPUT
])
138 print_onoff(fp
, "input", rta_getattr_u32(tb
[NETCONFA_INPUT
]));
145 static int print_netconf2(const struct sockaddr_nl
*who
,
146 struct nlmsghdr
*n
, void *arg
)
148 return print_netconf(who
, NULL
, n
, arg
);
151 void ipnetconf_reset_filter(int ifindex
)
153 memset(&filter
, 0, sizeof(filter
));
154 filter
.ifindex
= ifindex
;
157 static int do_show(int argc
, char **argv
)
161 struct netconfmsg ncm
;
164 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct netconfmsg
)),
165 .n
.nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
,
166 .n
.nlmsg_type
= RTM_GETNETCONF
,
169 ipnetconf_reset_filter(0);
170 filter
.family
= preferred_family
;
171 if (filter
.family
== AF_UNSPEC
)
172 filter
.family
= AF_INET
;
175 if (strcmp(*argv
, "dev") == 0) {
177 filter
.ifindex
= ll_name_to_index(*argv
);
178 if (filter
.ifindex
<= 0) {
179 fprintf(stderr
, "Device \"%s\" does not exist.\n",
188 if (filter
.ifindex
) {
189 req
.ncm
.ncm_family
= filter
.family
;
191 addattr_l(&req
.n
, sizeof(req
), NETCONFA_IFINDEX
,
192 &filter
.ifindex
, sizeof(filter
.ifindex
));
194 if (rtnl_send(&rth
, &req
.n
, req
.n
.nlmsg_len
) < 0) {
195 perror("Can not send request");
198 rtnl_listen(&rth
, print_netconf
, stdout
);
201 if (rtnl_wilddump_request(&rth
, filter
.family
, RTM_GETNETCONF
) < 0) {
202 perror("Cannot send dump request");
205 if (rtnl_dump_filter(&rth
, print_netconf2
, stdout
) < 0) {
206 fprintf(stderr
, "Dump terminated\n");
209 if (preferred_family
== AF_UNSPEC
) {
210 preferred_family
= AF_INET6
;
211 filter
.family
= AF_INET6
;
218 int do_ipnetconf(int argc
, char **argv
)
221 if (matches(*argv
, "show") == 0 ||
222 matches(*argv
, "lst") == 0 ||
223 matches(*argv
, "list") == 0)
224 return do_show(argc
-1, argv
+1);
225 if (matches(*argv
, "help") == 0)
228 return do_show(0, NULL
);
230 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip netconf help\".\n", *argv
);