]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/ipnetconf.c
ip: Add support for MPLS netconf
[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
23 #include "rt_names.h"
24 #include "utils.h"
25 #include "ip_common.h"
26
27 static struct {
28 int family;
29 int ifindex;
30 } filter;
31
32 static void usage(void) __attribute__((noreturn));
33
34 static void usage(void)
35 {
36 fprintf(stderr, "Usage: ip netconf show [ dev STRING ]\n");
37 exit(-1);
38 }
39
40 static void print_onoff(FILE *f, const char *flag, __u32 val)
41 {
42 fprintf(f, "%s %s ", flag, val ? "on" : "off");
43 }
44
45 static struct rtattr *netconf_rta(struct netconfmsg *ncm)
46 {
47 return (struct rtattr *)((char *)ncm
48 + NLMSG_ALIGN(sizeof(struct netconfmsg)));
49 }
50
51 int print_netconf(const struct sockaddr_nl *who, struct rtnl_ctrl_data *ctrl,
52 struct nlmsghdr *n, void *arg)
53 {
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];
58
59 if (n->nlmsg_type == NLMSG_ERROR)
60 return -1;
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);
64
65 return -1;
66 }
67 len -= NLMSG_SPACE(sizeof(*ncm));
68 if (len < 0) {
69 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
70 return -1;
71 }
72
73 if (filter.family && filter.family != ncm->ncm_family)
74 return 0;
75
76 parse_rtattr(tb, NETCONFA_MAX, netconf_rta(ncm),
77 NLMSG_PAYLOAD(n, sizeof(*ncm)));
78
79 switch (ncm->ncm_family) {
80 case AF_INET:
81 fprintf(fp, "ipv4 ");
82 break;
83 case AF_INET6:
84 fprintf(fp, "ipv6 ");
85 break;
86 case AF_MPLS:
87 fprintf(fp, "mpls ");
88 break;
89 default:
90 fprintf(fp, "unknown ");
91 break;
92 }
93
94 if (tb[NETCONFA_IFINDEX]) {
95 int *ifindex = (int *)rta_getattr_str(tb[NETCONFA_IFINDEX]);
96
97 switch (*ifindex) {
98 case NETCONFA_IFINDEX_ALL:
99 fprintf(fp, "all ");
100 break;
101 case NETCONFA_IFINDEX_DEFAULT:
102 fprintf(fp, "default ");
103 break;
104 default:
105 fprintf(fp, "dev %s ", ll_index_to_name(*ifindex));
106 break;
107 }
108 }
109
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]);
115
116 if (rp_filter == 0)
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 ");
122 else
123 fprintf(fp, "rp_filter unknown mode ");
124 }
125 if (tb[NETCONFA_MC_FORWARDING])
126 print_onoff(fp, "mc_forwarding",
127 rta_getattr_u32(tb[NETCONFA_MC_FORWARDING]));
128
129 if (tb[NETCONFA_PROXY_NEIGH])
130 print_onoff(fp, "proxy_neigh",
131 rta_getattr_u32(tb[NETCONFA_PROXY_NEIGH]));
132
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]));
136
137 if (tb[NETCONFA_INPUT])
138 print_onoff(fp, "input", rta_getattr_u32(tb[NETCONFA_INPUT]));
139
140 fprintf(fp, "\n");
141 fflush(fp);
142 return 0;
143 }
144
145 static int print_netconf2(const struct sockaddr_nl *who,
146 struct nlmsghdr *n, void *arg)
147 {
148 return print_netconf(who, NULL, n, arg);
149 }
150
151 void ipnetconf_reset_filter(int ifindex)
152 {
153 memset(&filter, 0, sizeof(filter));
154 filter.ifindex = ifindex;
155 }
156
157 static int do_show(int argc, char **argv)
158 {
159 struct {
160 struct nlmsghdr n;
161 struct netconfmsg ncm;
162 char buf[1024];
163 } req = {
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,
167 };
168
169 ipnetconf_reset_filter(0);
170 filter.family = preferred_family;
171 if (filter.family == AF_UNSPEC)
172 filter.family = AF_INET;
173
174 while (argc > 0) {
175 if (strcmp(*argv, "dev") == 0) {
176 NEXT_ARG();
177 filter.ifindex = ll_name_to_index(*argv);
178 if (filter.ifindex <= 0) {
179 fprintf(stderr, "Device \"%s\" does not exist.\n",
180 *argv);
181 return -1;
182 }
183 }
184 argv++; argc--;
185 }
186
187 ll_init_map(&rth);
188 if (filter.ifindex) {
189 req.ncm.ncm_family = filter.family;
190 if (filter.ifindex)
191 addattr_l(&req.n, sizeof(req), NETCONFA_IFINDEX,
192 &filter.ifindex, sizeof(filter.ifindex));
193
194 if (rtnl_send(&rth, &req.n, req.n.nlmsg_len) < 0) {
195 perror("Can not send request");
196 exit(1);
197 }
198 rtnl_listen(&rth, print_netconf, stdout);
199 } else {
200 dump:
201 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETNETCONF) < 0) {
202 perror("Cannot send dump request");
203 exit(1);
204 }
205 if (rtnl_dump_filter(&rth, print_netconf2, stdout) < 0) {
206 fprintf(stderr, "Dump terminated\n");
207 exit(1);
208 }
209 if (preferred_family == AF_UNSPEC) {
210 preferred_family = AF_INET6;
211 filter.family = AF_INET6;
212 goto dump;
213 }
214 }
215 return 0;
216 }
217
218 int do_ipnetconf(int argc, char **argv)
219 {
220 if (argc > 0) {
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)
226 usage();
227 } else
228 return do_show(0, NULL);
229
230 fprintf(stderr, "Command \"%s\" is unknown, try \"ip netconf help\".\n", *argv);
231 exit(-1);
232 }