]>
git.proxmox.com Git - mirror_iproute2.git/blob - ip/ipnetconf.c
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"
33 static void usage(void) __attribute__((noreturn
));
35 static void usage(void)
37 fprintf(stderr
, "Usage: ip netconf show [ dev STRING ]\n");
41 #define NETCONF_RTA(r) ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct netconfmsg))))
43 int print_netconf(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
, void *arg
)
45 FILE *fp
= (FILE*)arg
;
46 struct netconfmsg
*ncm
= NLMSG_DATA(n
);
47 int len
= n
->nlmsg_len
;
48 struct rtattr
*tb
[NETCONFA_MAX
+1];
50 if (n
->nlmsg_type
== NLMSG_ERROR
)
52 if (n
->nlmsg_type
!= RTM_NEWNETCONF
) {
53 fprintf(stderr
, "Not RTM_NEWNETCONF: %08x %08x %08x\n",
54 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
58 len
-= NLMSG_SPACE(sizeof(*ncm
));
60 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
64 if (filter
.family
&& filter
.family
!= ncm
->ncm_family
)
67 parse_rtattr(tb
, NETCONFA_MAX
, NETCONF_RTA(ncm
),
68 NLMSG_PAYLOAD(n
, sizeof(*ncm
)));
70 switch (ncm
->ncm_family
) {
78 fprintf(fp
, "unknown ");
82 if (tb
[NETCONFA_IFINDEX
]) {
83 int *ifindex
= (int *)RTA_DATA(tb
[NETCONFA_IFINDEX
]);
86 case NETCONFA_IFINDEX_ALL
:
89 case NETCONFA_IFINDEX_DEFAULT
:
90 fprintf(fp
, "default ");
93 fprintf(fp
, "dev %s ", ll_index_to_name(*ifindex
));
98 if (tb
[NETCONFA_FORWARDING
])
99 fprintf(fp
, "forwarding %s ",
100 *(int *)RTA_DATA(tb
[NETCONFA_FORWARDING
])?"on":"off");
101 if (tb
[NETCONFA_RP_FILTER
]) {
102 int rp_filter
= *(int *)RTA_DATA(tb
[NETCONFA_RP_FILTER
]);
105 fprintf(fp
, "rp_filter off ");
106 else if (rp_filter
== 1)
107 fprintf(fp
, "rp_filter strict ");
108 else if (rp_filter
== 2)
109 fprintf(fp
, "rp_filter loose ");
111 fprintf(fp
, "rp_filter unknown mode ");
113 if (tb
[NETCONFA_MC_FORWARDING
])
114 fprintf(fp
, "mc_forwarding %d ",
115 *(int *)RTA_DATA(tb
[NETCONFA_MC_FORWARDING
]));
117 if (tb
[NETCONFA_PROXY_NEIGH
])
118 fprintf(fp
, "proxy_neigh %s ",
119 *(int *)RTA_DATA(tb
[NETCONFA_PROXY_NEIGH
])?"on":"off");
126 static void ipnetconf_reset_filter(void)
128 memset(&filter
, 0, sizeof(filter
));
131 static int do_show(int argc
, char **argv
)
135 struct netconfmsg ncm
;
139 ipnetconf_reset_filter();
140 filter
.family
= preferred_family
;
141 if (filter
.family
== AF_UNSPEC
)
142 filter
.family
= AF_INET
;
145 if (strcmp(*argv
, "dev") == 0) {
147 filter
.ifindex
= ll_name_to_index(*argv
);
148 if (filter
.ifindex
<= 0) {
149 fprintf(stderr
, "Device \"%s\" does not exist.\n",
158 if (filter
.ifindex
) {
159 memset(&req
, 0, sizeof(req
));
160 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct netconfmsg
));
161 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|NLM_F_ACK
;
162 req
.n
.nlmsg_type
= RTM_GETNETCONF
;
163 req
.ncm
.ncm_family
= filter
.family
;
165 addattr_l(&req
.n
, sizeof(req
), NETCONFA_IFINDEX
,
166 &filter
.ifindex
, sizeof(filter
.ifindex
));
168 if (rtnl_send(&rth
, &req
.n
, req
.n
.nlmsg_len
) < 0) {
169 perror("Can not send request");
172 rtnl_listen(&rth
, print_netconf
, stdout
);
175 if (rtnl_wilddump_request(&rth
, filter
.family
, RTM_GETNETCONF
) < 0) {
176 perror("Cannot send dump request");
179 if (rtnl_dump_filter(&rth
, print_netconf
, stdout
) < 0) {
180 fprintf(stderr
, "Dump terminated\n");
183 if (preferred_family
== AF_UNSPEC
) {
184 preferred_family
= AF_INET6
;
185 filter
.family
= AF_INET6
;
192 int do_ipnetconf(int argc
, char **argv
)
195 if (matches(*argv
, "show") == 0 ||
196 matches(*argv
, "lst") == 0 ||
197 matches(*argv
, "list") == 0)
198 return do_show(argc
-1, argv
+1);
199 if (matches(*argv
, "help") == 0)
202 return do_show(0, NULL
);
204 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip netconf help\".\n", *argv
);