ipaddrlabel.c revision 3490740b98b83da556c593f63292993e6155b81b
1/*
2 * ipaddrlabel.c	"ip addrlabel"
3 *
4 * Copyright (C)2007 USAGI/WIDE Project
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 *
20 *
21 * Based on iprule.c.
22 *
23 * Authors:	YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
24 *
25 */
26
27#include <stdio.h>
28#include <stdlib.h>
29#include <unistd.h>
30#include <syslog.h>
31#include <fcntl.h>
32#include <sys/socket.h>
33#include <netinet/in.h>
34#include <netinet/ip.h>
35#include <arpa/inet.h>
36#include <string.h>
37#include <linux/types.h>
38#include <linux/if_addrlabel.h>
39
40#include "rt_names.h"
41#include "utils.h"
42#include "ip_common.h"
43
44#define IFAL_RTA(r)	((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ifaddrlblmsg))))
45#define IFAL_PAYLOAD(n)	NLMSG_PAYLOAD(n,sizeof(struct ifaddrlblmsg))
46
47extern struct rtnl_handle rth;
48
49static void usage(void) __attribute__((noreturn));
50
51static void usage(void)
52{
53	fprintf(stderr, "Usage: ip addrlabel [ list | add | del | flush ] prefix PREFIX [ dev DEV ] [ label LABEL ]\n");
54	exit(-1);
55}
56
57int print_addrlabel(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
58{
59	FILE *fp = (FILE*)arg;
60	struct ifaddrlblmsg *ifal = NLMSG_DATA(n);
61	int len = n->nlmsg_len;
62	int host_len = -1;
63	struct rtattr *tb[IFAL_MAX+1];
64	char abuf[256];
65
66	if (n->nlmsg_type != RTM_NEWADDRLABEL && n->nlmsg_type != RTM_DELADDRLABEL)
67		return 0;
68
69	len -= NLMSG_LENGTH(sizeof(*ifal));
70	if (len < 0)
71		return -1;
72
73	parse_rtattr(tb, IFAL_MAX, IFAL_RTA(ifal), len);
74
75	if (ifal->ifal_family == AF_INET)
76		host_len = 32;
77	else if (ifal->ifal_family == AF_INET6)
78		host_len = 128;
79
80	if (n->nlmsg_type == RTM_DELADDRLABEL)
81		fprintf(fp, "Deleted ");
82
83	if (tb[IFAL_ADDRESS]) {
84		fprintf(fp, "prefix %s/%u ",
85			format_host(ifal->ifal_family,
86				    RTA_PAYLOAD(tb[IFAL_ADDRESS]),
87				    RTA_DATA(tb[IFAL_ADDRESS]),
88				    abuf, sizeof(abuf)),
89			ifal->ifal_prefixlen);
90	}
91
92	if (ifal->ifal_index)
93		fprintf(fp, "dev %s ", ll_index_to_name(ifal->ifal_index));
94
95	if (tb[IFAL_LABEL] && RTA_PAYLOAD(tb[IFAL_LABEL]) == sizeof(int32_t)) {
96		int32_t label;
97		memcpy(&label, RTA_DATA(tb[IFAL_LABEL]), sizeof(label));
98		fprintf(fp, "label %d ", label);
99	}
100
101	fprintf(fp, "\n");
102	fflush(fp);
103	return 0;
104}
105
106static int ipaddrlabel_list(int argc, char **argv)
107{
108	int af = preferred_family;
109
110	if (af == AF_UNSPEC)
111		af = AF_INET6;
112
113	if (argc > 0) {
114		fprintf(stderr, "\"ip addrlabel show\" does not take any arguments.\n");
115		return -1;
116	}
117
118	if (rtnl_wilddump_request(&rth, af, RTM_GETADDRLABEL) < 0) {
119		perror("Cannot send dump request");
120		return 1;
121	}
122
123	if (rtnl_dump_filter(&rth, print_addrlabel, stdout, NULL, NULL) < 0) {
124		fprintf(stderr, "Dump terminated\n");
125		return 1;
126	}
127
128	return 0;
129}
130
131
132static int ipaddrlabel_modify(int cmd, int argc, char **argv)
133{
134	struct {
135		struct nlmsghdr 	n;
136		struct ifaddrlblmsg	ifal;
137		char   			buf[1024];
138	} req;
139
140	inet_prefix prefix;
141	uint32_t label = 0xffffffffUL;
142
143	memset(&req, 0, sizeof(req));
144	memset(&prefix, 0, sizeof(prefix));
145
146	req.n.nlmsg_type = cmd;
147	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrlblmsg));
148	req.n.nlmsg_flags = NLM_F_REQUEST;
149	req.ifal.ifal_family = preferred_family;
150	req.ifal.ifal_prefixlen = 0;
151	req.ifal.ifal_index = 0;
152
153	if (cmd == RTM_NEWADDRLABEL) {
154		req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
155	}
156
157	while (argc > 0) {
158		if (strcmp(*argv, "prefix") == 0) {
159			NEXT_ARG();
160			get_prefix(&prefix, *argv, preferred_family);
161		} else if (strcmp(*argv, "dev") == 0) {
162			NEXT_ARG();
163			if ((req.ifal.ifal_index = ll_name_to_index(*argv)) == 0)
164				invarg("dev is invalid\n", *argv);
165		} else if (strcmp(*argv, "label") == 0) {
166			NEXT_ARG();
167			if (get_u32(&label, *argv, 0) || label == 0xffffffffUL)
168				invarg("label is invalid\n", *argv);
169		}
170		argc--;
171		argv++;
172	}
173
174	addattr32(&req.n, sizeof(req), IFAL_LABEL, label);
175	addattr_l(&req.n, sizeof(req), IFAL_ADDRESS, &prefix.data, prefix.bytelen);
176	req.ifal.ifal_prefixlen = prefix.bitlen;
177
178	if (req.ifal.ifal_family == AF_UNSPEC)
179		req.ifal.ifal_family = AF_INET6;
180
181	if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
182		return 2;
183
184	return 0;
185}
186
187
188static int flush_addrlabel(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
189{
190	struct rtnl_handle rth2;
191	struct rtmsg *r = NLMSG_DATA(n);
192	int len = n->nlmsg_len;
193	struct rtattr * tb[IFAL_MAX+1];
194
195	len -= NLMSG_LENGTH(sizeof(*r));
196	if (len < 0)
197		return -1;
198
199	parse_rtattr(tb, IFAL_MAX, RTM_RTA(r), len);
200
201	if (tb[IFAL_ADDRESS]) {
202		n->nlmsg_type = RTM_DELADDRLABEL;
203		n->nlmsg_flags = NLM_F_REQUEST;
204
205		if (rtnl_open(&rth2, 0) < 0)
206			return -1;
207
208		if (rtnl_talk(&rth2, n, 0, 0, NULL, NULL, NULL) < 0)
209			return -2;
210
211		rtnl_close(&rth2);
212	}
213
214	return 0;
215}
216
217static int ipaddrlabel_flush(int argc, char **argv)
218{
219	int af = preferred_family;
220
221	if (af == AF_UNSPEC)
222		af = AF_INET6;
223
224	if (argc > 0) {
225		fprintf(stderr, "\"ip addrlabel flush\" does not allow extra arguments\n");
226		return -1;
227	}
228
229	if (rtnl_wilddump_request(&rth, af, RTM_GETADDRLABEL) < 0) {
230		perror("Cannot send dump request");
231		return 1;
232	}
233
234	if (rtnl_dump_filter(&rth, flush_addrlabel, NULL, NULL, NULL) < 0) {
235		fprintf(stderr, "Flush terminated\n");
236		return 1;
237	}
238
239	return 0;
240}
241
242int do_ipaddrlabel(int argc, char **argv)
243{
244	if (argc < 1) {
245		return ipaddrlabel_list(0, NULL);
246	} else if (matches(argv[0], "list") == 0 ||
247		   matches(argv[0], "show") == 0) {
248		return ipaddrlabel_list(argc-1, argv+1);
249	} else if (matches(argv[0], "add") == 0) {
250		return ipaddrlabel_modify(RTM_NEWADDRLABEL, argc-1, argv+1);
251	} else if (matches(argv[0], "delete") == 0) {
252		return ipaddrlabel_modify(RTM_DELADDRLABEL, argc-1, argv+1);
253	} else if (matches(argv[0], "flush") == 0) {
254		return ipaddrlabel_flush(argc-1, argv+1);
255	} else if (matches(argv[0], "help") == 0)
256		usage();
257
258	fprintf(stderr, "Command \"%s\" is unknown, try \"ip addrlabel help\".\n", *argv);
259	exit(-1);
260}
261
262