]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/ipnetconf.c
ip: mroute: Print offload indication
[mirror_iproute2.git] / ip / ipnetconf.c
1 /*
2 * ipnetconf.c "ip netconf".
3 *
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.
8 *
9 * Authors: Nicolas Dichtel, <nicolas.dichtel@6wind.com>
10 *
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <syslog.h>
17 #include <fcntl.h>
18 #include <string.h>
19 #include <sys/time.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <errno.h>
23
24 #include "rt_names.h"
25 #include "utils.h"
26 #include "ip_common.h"
27
28 static struct {
29 int family;
30 int ifindex;
31 } filter;
32
33 static void usage(void) __attribute__((noreturn));
34
35 static void usage(void)
36 {
37 fprintf(stderr, "Usage: ip netconf show [ dev STRING ]\n");
38 exit(-1);
39 }
40
41 static void print_onoff(FILE *f, const char *flag, __u32 val)
42 {
43 fprintf(f, "%s %s ", flag, val ? "on" : "off");
44 }
45
46 static struct rtattr *netconf_rta(struct netconfmsg *ncm)
47 {
48 return (struct rtattr *)((char *)ncm
49 + NLMSG_ALIGN(sizeof(struct netconfmsg)));
50 }
51
52 int print_netconf(const struct sockaddr_nl *who, struct rtnl_ctrl_data *ctrl,
53 struct nlmsghdr *n, void *arg)
54 {
55 FILE *fp = (FILE *)arg;
56 struct netconfmsg *ncm = NLMSG_DATA(n);
57 int len = n->nlmsg_len;
58 struct rtattr *tb[NETCONFA_MAX+1];
59 int ifindex = 0;
60
61 if (n->nlmsg_type == NLMSG_ERROR)
62 return -1;
63 if (n->nlmsg_type != RTM_NEWNETCONF) {
64 fprintf(stderr, "Not RTM_NEWNETCONF: %08x %08x %08x\n",
65 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
66
67 return -1;
68 }
69 len -= NLMSG_SPACE(sizeof(*ncm));
70 if (len < 0) {
71 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
72 return -1;
73 }
74
75 if (filter.family && filter.family != ncm->ncm_family)
76 return 0;
77
78 parse_rtattr(tb, NETCONFA_MAX, netconf_rta(ncm),
79 NLMSG_PAYLOAD(n, sizeof(*ncm)));
80
81 if (tb[NETCONFA_IFINDEX])
82 ifindex = rta_getattr_u32(tb[NETCONFA_IFINDEX]);
83
84 if (filter.ifindex && filter.ifindex != ifindex)
85 return 0;
86
87 switch (ncm->ncm_family) {
88 case AF_INET:
89 fprintf(fp, "ipv4 ");
90 break;
91 case AF_INET6:
92 fprintf(fp, "ipv6 ");
93 break;
94 case AF_MPLS:
95 fprintf(fp, "mpls ");
96 break;
97 default:
98 fprintf(fp, "unknown ");
99 break;
100 }
101
102 if (tb[NETCONFA_IFINDEX]) {
103 switch (ifindex) {
104 case NETCONFA_IFINDEX_ALL:
105 fprintf(fp, "all ");
106 break;
107 case NETCONFA_IFINDEX_DEFAULT:
108 fprintf(fp, "default ");
109 break;
110 default:
111 fprintf(fp, "dev %s ", ll_index_to_name(ifindex));
112 break;
113 }
114 }
115
116 if (tb[NETCONFA_FORWARDING])
117 print_onoff(fp, "forwarding",
118 rta_getattr_u32(tb[NETCONFA_FORWARDING]));
119 if (tb[NETCONFA_RP_FILTER]) {
120 __u32 rp_filter = rta_getattr_u32(tb[NETCONFA_RP_FILTER]);
121
122 if (rp_filter == 0)
123 fprintf(fp, "rp_filter off ");
124 else if (rp_filter == 1)
125 fprintf(fp, "rp_filter strict ");
126 else if (rp_filter == 2)
127 fprintf(fp, "rp_filter loose ");
128 else
129 fprintf(fp, "rp_filter unknown mode ");
130 }
131 if (tb[NETCONFA_MC_FORWARDING])
132 print_onoff(fp, "mc_forwarding",
133 rta_getattr_u32(tb[NETCONFA_MC_FORWARDING]));
134
135 if (tb[NETCONFA_PROXY_NEIGH])
136 print_onoff(fp, "proxy_neigh",
137 rta_getattr_u32(tb[NETCONFA_PROXY_NEIGH]));
138
139 if (tb[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN])
140 print_onoff(fp, "ignore_routes_with_linkdown",
141 rta_getattr_u32(tb[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN]));
142
143 if (tb[NETCONFA_INPUT])
144 print_onoff(fp, "input", rta_getattr_u32(tb[NETCONFA_INPUT]));
145
146 fprintf(fp, "\n");
147 fflush(fp);
148 return 0;
149 }
150
151 static int print_netconf2(const struct sockaddr_nl *who,
152 struct nlmsghdr *n, void *arg)
153 {
154 return print_netconf(who, NULL, n, arg);
155 }
156
157 void ipnetconf_reset_filter(int ifindex)
158 {
159 memset(&filter, 0, sizeof(filter));
160 filter.ifindex = ifindex;
161 }
162
163 static int do_show(int argc, char **argv)
164 {
165 struct {
166 struct nlmsghdr n;
167 struct netconfmsg ncm;
168 char buf[1024];
169 } req = {
170 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct netconfmsg)),
171 .n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
172 .n.nlmsg_type = RTM_GETNETCONF,
173 };
174
175 ipnetconf_reset_filter(0);
176 filter.family = preferred_family;
177
178 while (argc > 0) {
179 if (strcmp(*argv, "dev") == 0) {
180 NEXT_ARG();
181 filter.ifindex = ll_name_to_index(*argv);
182 if (filter.ifindex <= 0) {
183 fprintf(stderr, "Device \"%s\" does not exist.\n",
184 *argv);
185 return -1;
186 }
187 }
188 argv++; argc--;
189 }
190
191 ll_init_map(&rth);
192
193 if (filter.ifindex && filter.family != AF_UNSPEC) {
194 req.ncm.ncm_family = filter.family;
195 addattr_l(&req.n, sizeof(req), NETCONFA_IFINDEX,
196 &filter.ifindex, sizeof(filter.ifindex));
197
198 if (rtnl_send(&rth, &req.n, req.n.nlmsg_len) < 0) {
199 perror("Can not send request");
200 exit(1);
201 }
202 rtnl_listen(&rth, print_netconf, stdout);
203 } else {
204 rth.flags = RTNL_HANDLE_F_SUPPRESS_NLERR;
205 dump:
206 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETNETCONF) < 0) {
207 perror("Cannot send dump request");
208 exit(1);
209 }
210 if (rtnl_dump_filter(&rth, print_netconf2, stdout) < 0) {
211 /* kernel does not support netconf dump on AF_UNSPEC;
212 * fall back to requesting by family
213 */
214 if (errno == EOPNOTSUPP &&
215 filter.family == AF_UNSPEC) {
216 filter.family = AF_INET;
217 goto dump;
218 }
219 perror("RTNETLINK answers");
220 fprintf(stderr, "Dump terminated\n");
221 exit(1);
222 }
223 if (preferred_family == AF_UNSPEC && filter.family == AF_INET) {
224 preferred_family = AF_INET6;
225 filter.family = AF_INET6;
226 goto dump;
227 }
228 }
229 return 0;
230 }
231
232 int do_ipnetconf(int argc, char **argv)
233 {
234 if (argc > 0) {
235 if (matches(*argv, "show") == 0 ||
236 matches(*argv, "lst") == 0 ||
237 matches(*argv, "list") == 0)
238 return do_show(argc-1, argv+1);
239 if (matches(*argv, "help") == 0)
240 usage();
241 } else
242 return do_show(0, NULL);
243
244 fprintf(stderr, "Command \"%s\" is unknown, try \"ip netconf help\".\n", *argv);
245 exit(-1);
246 }