blob: aa31ead0686311a7db127b1bdb4e05f9f1fddaee [file] [log] [blame]
Nicolas Dichtel4852ba72012-12-12 09:05:51 -08001/*
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
27static struct
28{
29 int family;
30 int ifindex;
31} filter;
32
33static void usage(void) __attribute__((noreturn));
34
35static void usage(void)
36{
37 fprintf(stderr, "Usage: ip netconf show [ dev STRING ]\n");
38 exit(-1);
39}
40
41#define NETCONF_RTA(r) ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct netconfmsg))))
42
43int print_netconf(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
44{
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];
49
50 if (n->nlmsg_type == NLMSG_ERROR)
51 return -1;
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);
55
56 return -1;
57 }
58 len -= NLMSG_SPACE(sizeof(*ncm));
59 if (len < 0) {
60 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
61 return -1;
62 }
63
64 if (filter.family && filter.family != ncm->ncm_family)
65 return 0;
66
67 parse_rtattr(tb, NETCONFA_MAX, NETCONF_RTA(ncm),
68 NLMSG_PAYLOAD(n, sizeof(*ncm)));
69
70 switch (ncm->ncm_family) {
71 case AF_INET:
72 fprintf(fp, "ipv4 ");
73 break;
74 case AF_INET6:
75 fprintf(fp, "ipv6 ");
76 break;
77 default:
78 fprintf(fp, "unknown ");
79 break;
80 }
81
82 if (tb[NETCONFA_IFINDEX]) {
83 int *ifindex = (int *)RTA_DATA(tb[NETCONFA_IFINDEX]);
84
85 switch (*ifindex) {
86 case NETCONFA_IFINDEX_ALL:
87 fprintf(fp, "all ");
88 break;
89 case NETCONFA_IFINDEX_DEFAULT:
90 fprintf(fp, "default ");
91 break;
92 default:
93 fprintf(fp, "dev %s ", ll_index_to_name(*ifindex));
94 break;
95 }
96 }
97
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]);
103
104 if (rp_filter == 0)
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 ");
110 else
111 fprintf(fp, "rp_filter unknown mode ");
112 }
113 if (tb[NETCONFA_MC_FORWARDING])
114 fprintf(fp, "mc_forwarding %d ",
115 *(int *)RTA_DATA(tb[NETCONFA_MC_FORWARDING]));
116
Stephen Hemminger29cc8642013-12-17 22:26:56 -0800117 if (tb[NETCONFA_PROXY_NEIGH])
118 fprintf(fp, "proxy_neigh %s ",
119 *(int *)RTA_DATA(tb[NETCONFA_PROXY_NEIGH])?"on":"off");
120
Nicolas Dichtel4852ba72012-12-12 09:05:51 -0800121 fprintf(fp, "\n");
122 fflush(fp);
123 return 0;
124}
125
vadimk093b7642014-10-20 12:25:17 +0300126void ipnetconf_reset_filter(int ifindex)
Nicolas Dichtel4852ba72012-12-12 09:05:51 -0800127{
128 memset(&filter, 0, sizeof(filter));
vadimk093b7642014-10-20 12:25:17 +0300129 filter.ifindex = ifindex;
Nicolas Dichtel4852ba72012-12-12 09:05:51 -0800130}
131
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800132static int do_show(int argc, char **argv)
Nicolas Dichtel4852ba72012-12-12 09:05:51 -0800133{
134 struct {
135 struct nlmsghdr n;
136 struct netconfmsg ncm;
137 char buf[1024];
138 } req;
139
vadimk093b7642014-10-20 12:25:17 +0300140 ipnetconf_reset_filter(0);
Nicolas Dichtel4852ba72012-12-12 09:05:51 -0800141 filter.family = preferred_family;
142 if (filter.family == AF_UNSPEC)
143 filter.family = AF_INET;
Nicolas Dichtel4852ba72012-12-12 09:05:51 -0800144
145 while (argc > 0) {
146 if (strcmp(*argv, "dev") == 0) {
147 NEXT_ARG();
148 filter.ifindex = ll_name_to_index(*argv);
149 if (filter.ifindex <= 0) {
150 fprintf(stderr, "Device \"%s\" does not exist.\n",
151 *argv);
152 return -1;
153 }
154 }
155 argv++; argc--;
156 }
157
158 ll_init_map(&rth);
Nicolas Dichtelf7431e22013-05-17 01:47:33 -0700159 if (filter.ifindex) {
160 memset(&req, 0, sizeof(req));
161 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct netconfmsg));
162 req.n.nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK;
163 req.n.nlmsg_type = RTM_GETNETCONF;
164 req.ncm.ncm_family = filter.family;
165 if (filter.ifindex)
166 addattr_l(&req.n, sizeof(req), NETCONFA_IFINDEX,
167 &filter.ifindex, sizeof(filter.ifindex));
Nicolas Dichtel4852ba72012-12-12 09:05:51 -0800168
Stephen Hemmingerd2468da2013-12-20 08:15:02 -0800169 if (rtnl_send(&rth, &req.n, req.n.nlmsg_len) < 0) {
170 perror("Can not send request");
171 exit(1);
172 }
Nicolas Dichtelf7431e22013-05-17 01:47:33 -0700173 rtnl_listen(&rth, print_netconf, stdout);
174 } else {
175dump:
176 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETNETCONF) < 0) {
177 perror("Cannot send dump request");
178 exit(1);
179 }
180 if (rtnl_dump_filter(&rth, print_netconf, stdout) < 0) {
181 fprintf(stderr, "Dump terminated\n");
182 exit(1);
183 }
184 if (preferred_family == AF_UNSPEC) {
185 preferred_family = AF_INET6;
186 filter.family = AF_INET6;
187 goto dump;
188 }
189 }
Nicolas Dichtel4852ba72012-12-12 09:05:51 -0800190 return 0;
191}
192
193int do_ipnetconf(int argc, char **argv)
194{
195 if (argc > 0) {
196 if (matches(*argv, "show") == 0 ||
197 matches(*argv, "lst") == 0 ||
198 matches(*argv, "list") == 0)
199 return do_show(argc-1, argv+1);
200 if (matches(*argv, "help") == 0)
201 usage();
202 } else
203 return do_show(0, NULL);
204
205 fprintf(stderr, "Command \"%s\" is unknown, try \"ip netconf help\".\n", *argv);
206 exit(-1);
207}