blob: b4cd78409471989056ae5f30e069c7f8159b9f48 [file] [log] [blame]
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +11001/*
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
Stephen Hemminger4d98ab02013-12-06 15:05:07 -080017 * along with this program; if not, see <http://www.gnu.org/licenses>.
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +110018 *
19 *
20 * Based on iprule.c.
21 *
22 * Authors: YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
23 *
24 */
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <unistd.h>
29#include <syslog.h>
30#include <fcntl.h>
31#include <sys/socket.h>
32#include <netinet/in.h>
33#include <netinet/ip.h>
34#include <arpa/inet.h>
35#include <string.h>
36#include <linux/types.h>
37#include <linux/if_addrlabel.h>
38
39#include "rt_names.h"
40#include "utils.h"
41#include "ip_common.h"
42
Stephen Hemminger56f5daa2016-03-21 11:52:19 -070043#define IFAL_RTA(r) ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ifaddrlblmsg))))
44#define IFAL_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ifaddrlblmsg))
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +110045
46extern struct rtnl_handle rth;
47
48static void usage(void) __attribute__((noreturn));
49
50static void usage(void)
51{
Phil Sutter27ff1a52016-03-02 19:19:50 +010052 fprintf(stderr, "Usage: ip addrlabel { add | del } prefix PREFIX [ dev DEV ] [ label LABEL ]\n");
53 fprintf(stderr, " ip addrlabel [ list | flush | help ]\n");
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +110054 exit(-1);
55}
56
57int print_addrlabel(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
58{
Stephen Hemminger56f5daa2016-03-21 11:52:19 -070059 FILE *fp = (FILE *)arg;
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +110060 struct ifaddrlblmsg *ifal = NLMSG_DATA(n);
61 int len = n->nlmsg_len;
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +110062 struct rtattr *tb[IFAL_MAX+1];
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +110063
64 if (n->nlmsg_type != RTM_NEWADDRLABEL && n->nlmsg_type != RTM_DELADDRLABEL)
65 return 0;
66
67 len -= NLMSG_LENGTH(sizeof(*ifal));
68 if (len < 0)
69 return -1;
70
71 parse_rtattr(tb, IFAL_MAX, IFAL_RTA(ifal), len);
72
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +110073 if (n->nlmsg_type == RTM_DELADDRLABEL)
74 fprintf(fp, "Deleted ");
75
76 if (tb[IFAL_ADDRESS]) {
77 fprintf(fp, "prefix %s/%u ",
Phil Sutterd49f9342016-03-22 19:35:17 +010078 format_host_rta(ifal->ifal_family,
79 tb[IFAL_ADDRESS]),
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +110080 ifal->ifal_prefixlen);
81 }
82
83 if (ifal->ifal_index)
84 fprintf(fp, "dev %s ", ll_index_to_name(ifal->ifal_index));
85
Hangbin Liu97870332013-11-19 23:46:20 +080086 if (tb[IFAL_LABEL] && RTA_PAYLOAD(tb[IFAL_LABEL]) == sizeof(uint32_t)) {
87 uint32_t label;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -070088
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +110089 memcpy(&label, RTA_DATA(tb[IFAL_LABEL]), sizeof(label));
Hangbin Liu97870332013-11-19 23:46:20 +080090 fprintf(fp, "label %u ", label);
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +110091 }
92
93 fprintf(fp, "\n");
94 fflush(fp);
95 return 0;
96}
97
98static int ipaddrlabel_list(int argc, char **argv)
99{
100 int af = preferred_family;
101
102 if (af == AF_UNSPEC)
103 af = AF_INET6;
104
105 if (argc > 0) {
106 fprintf(stderr, "\"ip addrlabel show\" does not take any arguments.\n");
107 return -1;
108 }
109
110 if (rtnl_wilddump_request(&rth, af, RTM_GETADDRLABEL) < 0) {
111 perror("Cannot send dump request");
112 return 1;
113 }
114
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800115 if (rtnl_dump_filter(&rth, print_addrlabel, stdout) < 0) {
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +1100116 fprintf(stderr, "Dump terminated\n");
117 return 1;
118 }
119
120 return 0;
121}
122
123
124static int ipaddrlabel_modify(int cmd, int argc, char **argv)
125{
126 struct {
Stephen Hemminger48068672014-02-17 10:56:31 -0800127 struct nlmsghdr n;
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +1100128 struct ifaddrlblmsg ifal;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700129 char buf[1024];
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +1100130 } req;
131
132 inet_prefix prefix;
133 uint32_t label = 0xffffffffUL;
Varun Chandramohan6579fee2008-06-02 13:55:56 +0530134 char *p = NULL;
Stephen Hemminger06125192014-02-17 10:55:31 -0800135 char *l = NULL;
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +1100136
137 memset(&req, 0, sizeof(req));
138 memset(&prefix, 0, sizeof(prefix));
139
140 req.n.nlmsg_type = cmd;
141 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrlblmsg));
142 req.n.nlmsg_flags = NLM_F_REQUEST;
143 req.ifal.ifal_family = preferred_family;
144 req.ifal.ifal_prefixlen = 0;
145 req.ifal.ifal_index = 0;
146
147 if (cmd == RTM_NEWADDRLABEL) {
148 req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
149 }
150
151 while (argc > 0) {
152 if (strcmp(*argv, "prefix") == 0) {
153 NEXT_ARG();
Varun Chandramohan6579fee2008-06-02 13:55:56 +0530154 p = *argv;
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +1100155 get_prefix(&prefix, *argv, preferred_family);
156 } else if (strcmp(*argv, "dev") == 0) {
157 NEXT_ARG();
158 if ((req.ifal.ifal_index = ll_name_to_index(*argv)) == 0)
159 invarg("dev is invalid\n", *argv);
160 } else if (strcmp(*argv, "label") == 0) {
161 NEXT_ARG();
Varun Chandramohan6579fee2008-06-02 13:55:56 +0530162 l = *argv;
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +1100163 if (get_u32(&label, *argv, 0) || label == 0xffffffffUL)
164 invarg("label is invalid\n", *argv);
165 }
166 argc--;
167 argv++;
168 }
Varun Chandramohan6579fee2008-06-02 13:55:56 +0530169 if (p == NULL) {
170 fprintf(stderr, "Not enough information: \"prefix\" argument is required.\n");
171 return -1;
172 }
173 if (l == NULL) {
174 fprintf(stderr, "Not enough information: \"label\" argument is required.\n");
175 return -1;
176 }
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +1100177 addattr32(&req.n, sizeof(req), IFAL_LABEL, label);
178 addattr_l(&req.n, sizeof(req), IFAL_ADDRESS, &prefix.data, prefix.bytelen);
Varun Chandramohan34907402008-02-14 15:21:08 +0530179 req.ifal.ifal_prefixlen = prefix.bitlen;
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +1100180
181 if (req.ifal.ifal_family == AF_UNSPEC)
182 req.ifal.ifal_family = AF_INET6;
183
Stephen Hemmingerc079e122015-05-27 12:26:14 -0700184 if (rtnl_talk(&rth, &req.n, NULL, 0) < 0)
Stephen Hemminger906cafe2015-05-06 09:55:07 -0700185 return -2;
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +1100186
187 return 0;
188}
189
190
191static int flush_addrlabel(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
192{
193 struct rtnl_handle rth2;
194 struct rtmsg *r = NLMSG_DATA(n);
195 int len = n->nlmsg_len;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700196 struct rtattr *tb[IFAL_MAX+1];
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +1100197
198 len -= NLMSG_LENGTH(sizeof(*r));
199 if (len < 0)
200 return -1;
201
202 parse_rtattr(tb, IFAL_MAX, RTM_RTA(r), len);
203
204 if (tb[IFAL_ADDRESS]) {
205 n->nlmsg_type = RTM_DELADDRLABEL;
206 n->nlmsg_flags = NLM_F_REQUEST;
207
208 if (rtnl_open(&rth2, 0) < 0)
209 return -1;
210
Stephen Hemmingerc079e122015-05-27 12:26:14 -0700211 if (rtnl_talk(&rth2, n, NULL, 0) < 0)
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +1100212 return -2;
213
214 rtnl_close(&rth2);
215 }
216
217 return 0;
218}
219
220static int ipaddrlabel_flush(int argc, char **argv)
221{
222 int af = preferred_family;
223
224 if (af == AF_UNSPEC)
225 af = AF_INET6;
226
227 if (argc > 0) {
228 fprintf(stderr, "\"ip addrlabel flush\" does not allow extra arguments\n");
229 return -1;
230 }
231
232 if (rtnl_wilddump_request(&rth, af, RTM_GETADDRLABEL) < 0) {
233 perror("Cannot send dump request");
Stephen Hemminger906cafe2015-05-06 09:55:07 -0700234 return -1;
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +1100235 }
236
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800237 if (rtnl_dump_filter(&rth, flush_addrlabel, NULL) < 0) {
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +1100238 fprintf(stderr, "Flush terminated\n");
Stephen Hemminger906cafe2015-05-06 09:55:07 -0700239 return -1;
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +1100240 }
241
242 return 0;
243}
244
245int do_ipaddrlabel(int argc, char **argv)
246{
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +1100247 if (argc < 1) {
248 return ipaddrlabel_list(0, NULL);
249 } else if (matches(argv[0], "list") == 0 ||
Mark Einon473544d2015-03-16 09:59:09 +0000250 matches(argv[0], "lst") == 0 ||
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +1100251 matches(argv[0], "show") == 0) {
252 return ipaddrlabel_list(argc-1, argv+1);
253 } else if (matches(argv[0], "add") == 0) {
254 return ipaddrlabel_modify(RTM_NEWADDRLABEL, argc-1, argv+1);
255 } else if (matches(argv[0], "delete") == 0) {
256 return ipaddrlabel_modify(RTM_DELADDRLABEL, argc-1, argv+1);
257 } else if (matches(argv[0], "flush") == 0) {
258 return ipaddrlabel_flush(argc-1, argv+1);
259 } else if (matches(argv[0], "help") == 0)
260 usage();
261
262 fprintf(stderr, "Command \"%s\" is unknown, try \"ip addrlabel help\".\n", *argv);
263 exit(-1);
264}