]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/ipaddrlabel.c
Merge branch 'master' into iproute2-next
[mirror_iproute2.git] / ip / ipaddrlabel.c
CommitLineData
4759758c
YH
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
4d98ab00 17 * along with this program; if not, see <http://www.gnu.org/licenses>.
4759758c
YH
18 *
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>
4759758c
YH
29#include <fcntl.h>
30#include <sys/socket.h>
31#include <netinet/in.h>
32#include <netinet/ip.h>
33#include <arpa/inet.h>
34#include <string.h>
35#include <linux/types.h>
36#include <linux/if_addrlabel.h>
37
38#include "rt_names.h"
39#include "utils.h"
40#include "ip_common.h"
a7ad1c8a 41#include "json_print.h"
4759758c 42
56f5daac
SH
43#define IFAL_RTA(r) ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ifaddrlblmsg))))
44#define IFAL_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ifaddrlblmsg))
4759758c
YH
45
46extern struct rtnl_handle rth;
47
48static void usage(void) __attribute__((noreturn));
49
50static void usage(void)
51{
27ff1a56
PS
52 fprintf(stderr, "Usage: ip addrlabel { add | del } prefix PREFIX [ dev DEV ] [ label LABEL ]\n");
53 fprintf(stderr, " ip addrlabel [ list | flush | help ]\n");
4759758c
YH
54 exit(-1);
55}
56
57int print_addrlabel(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
58{
4759758c
YH
59 struct ifaddrlblmsg *ifal = NLMSG_DATA(n);
60 int len = n->nlmsg_len;
4759758c 61 struct rtattr *tb[IFAL_MAX+1];
4759758c
YH
62
63 if (n->nlmsg_type != RTM_NEWADDRLABEL && n->nlmsg_type != RTM_DELADDRLABEL)
64 return 0;
65
66 len -= NLMSG_LENGTH(sizeof(*ifal));
67 if (len < 0)
68 return -1;
69
70 parse_rtattr(tb, IFAL_MAX, IFAL_RTA(ifal), len);
71
a7ad1c8a 72 open_json_object(NULL);
4759758c 73 if (n->nlmsg_type == RTM_DELADDRLABEL)
a7ad1c8a 74 print_bool(PRINT_ANY, "deleted", "Deleted ", true);
4759758c
YH
75
76 if (tb[IFAL_ADDRESS]) {
a7ad1c8a
SH
77 const char *host
78 = format_host_rta(ifal->ifal_family,
79 tb[IFAL_ADDRESS]);
80
81 print_string(PRINT_FP, NULL, "prefix ", NULL);
82 print_color_string(PRINT_ANY,
83 ifa_family_color(ifal->ifal_family),
84 "address", "%s", host);
85
86 print_uint(PRINT_ANY, "prefixlen", "/%u ",
87 ifal->ifal_prefixlen);
4759758c
YH
88 }
89
a7ad1c8a
SH
90 if (ifal->ifal_index) {
91 print_string(PRINT_FP, NULL, "dev ", NULL);
92 print_color_string(PRINT_ANY, COLOR_IFNAME,
93 "ifname", "%s ",
94 ll_index_to_name(ifal->ifal_index));
95 }
4759758c 96
97870334 97 if (tb[IFAL_LABEL] && RTA_PAYLOAD(tb[IFAL_LABEL]) == sizeof(uint32_t)) {
a7ad1c8a 98 uint32_t label = rta_getattr_u32(RTA_DATA(tb[IFAL_LABEL]));
56f5daac 99
a7ad1c8a
SH
100 print_uint(PRINT_ANY,
101 "label", "label %u ", label);
4759758c 102 }
a7ad1c8a
SH
103 print_string(PRINT_FP, NULL, "\n", "");
104 close_json_object();
4759758c 105
4759758c
YH
106 return 0;
107}
108
109static int ipaddrlabel_list(int argc, char **argv)
110{
111 int af = preferred_family;
112
113 if (af == AF_UNSPEC)
114 af = AF_INET6;
115
116 if (argc > 0) {
117 fprintf(stderr, "\"ip addrlabel show\" does not take any arguments.\n");
118 return -1;
119 }
120
39360023 121 if (rtnl_addrlbldump_req(&rth, af) < 0) {
4759758c
YH
122 perror("Cannot send dump request");
123 return 1;
124 }
125
a7ad1c8a 126 new_json_obj(json);
cd70f3f5 127 if (rtnl_dump_filter(&rth, print_addrlabel, stdout) < 0) {
4759758c
YH
128 fprintf(stderr, "Dump terminated\n");
129 return 1;
130 }
a7ad1c8a 131 delete_json_obj();
4759758c
YH
132
133 return 0;
134}
135
136
137static int ipaddrlabel_modify(int cmd, int argc, char **argv)
138{
139 struct {
4806867a 140 struct nlmsghdr n;
4759758c 141 struct ifaddrlblmsg ifal;
56f5daac 142 char buf[1024];
d17b136f
PS
143 } req = {
144 .n.nlmsg_type = cmd,
145 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrlblmsg)),
146 .n.nlmsg_flags = NLM_F_REQUEST,
147 .ifal.ifal_family = preferred_family,
148 };
149
150 inet_prefix prefix = {};
4759758c 151 uint32_t label = 0xffffffffUL;
6579feea 152 char *p = NULL;
0612519e 153 char *l = NULL;
4759758c 154
4759758c
YH
155 if (cmd == RTM_NEWADDRLABEL) {
156 req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
157 }
158
159 while (argc > 0) {
160 if (strcmp(*argv, "prefix") == 0) {
161 NEXT_ARG();
6579feea 162 p = *argv;
4759758c
YH
163 get_prefix(&prefix, *argv, preferred_family);
164 } else if (strcmp(*argv, "dev") == 0) {
165 NEXT_ARG();
166 if ((req.ifal.ifal_index = ll_name_to_index(*argv)) == 0)
167 invarg("dev is invalid\n", *argv);
168 } else if (strcmp(*argv, "label") == 0) {
169 NEXT_ARG();
6579feea 170 l = *argv;
4759758c
YH
171 if (get_u32(&label, *argv, 0) || label == 0xffffffffUL)
172 invarg("label is invalid\n", *argv);
173 }
174 argc--;
175 argv++;
176 }
6579feea
VC
177 if (p == NULL) {
178 fprintf(stderr, "Not enough information: \"prefix\" argument is required.\n");
179 return -1;
180 }
181 if (l == NULL) {
182 fprintf(stderr, "Not enough information: \"label\" argument is required.\n");
183 return -1;
184 }
4759758c
YH
185 addattr32(&req.n, sizeof(req), IFAL_LABEL, label);
186 addattr_l(&req.n, sizeof(req), IFAL_ADDRESS, &prefix.data, prefix.bytelen);
3490740b 187 req.ifal.ifal_prefixlen = prefix.bitlen;
4759758c
YH
188
189 if (req.ifal.ifal_family == AF_UNSPEC)
190 req.ifal.ifal_family = AF_INET6;
191
86bf43c7 192 if (rtnl_talk(&rth, &req.n, NULL) < 0)
906cafe3 193 return -2;
4759758c
YH
194
195 return 0;
196}
197
198
199static int flush_addrlabel(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
200{
201 struct rtnl_handle rth2;
202 struct rtmsg *r = NLMSG_DATA(n);
203 int len = n->nlmsg_len;
56f5daac 204 struct rtattr *tb[IFAL_MAX+1];
4759758c
YH
205
206 len -= NLMSG_LENGTH(sizeof(*r));
207 if (len < 0)
208 return -1;
209
210 parse_rtattr(tb, IFAL_MAX, RTM_RTA(r), len);
211
212 if (tb[IFAL_ADDRESS]) {
213 n->nlmsg_type = RTM_DELADDRLABEL;
214 n->nlmsg_flags = NLM_F_REQUEST;
215
216 if (rtnl_open(&rth2, 0) < 0)
217 return -1;
218
86bf43c7 219 if (rtnl_talk(&rth2, n, NULL) < 0)
4759758c
YH
220 return -2;
221
222 rtnl_close(&rth2);
223 }
224
225 return 0;
226}
227
228static int ipaddrlabel_flush(int argc, char **argv)
229{
230 int af = preferred_family;
231
232 if (af == AF_UNSPEC)
233 af = AF_INET6;
234
235 if (argc > 0) {
236 fprintf(stderr, "\"ip addrlabel flush\" does not allow extra arguments\n");
237 return -1;
238 }
239
39360023 240 if (rtnl_addrlbldump_req(&rth, af) < 0) {
4759758c 241 perror("Cannot send dump request");
906cafe3 242 return -1;
4759758c
YH
243 }
244
cd70f3f5 245 if (rtnl_dump_filter(&rth, flush_addrlabel, NULL) < 0) {
4759758c 246 fprintf(stderr, "Flush terminated\n");
906cafe3 247 return -1;
4759758c
YH
248 }
249
250 return 0;
251}
252
253int do_ipaddrlabel(int argc, char **argv)
254{
255 if (argc < 1) {
256 return ipaddrlabel_list(0, NULL);
257 } else if (matches(argv[0], "list") == 0 ||
473544d9 258 matches(argv[0], "lst") == 0 ||
4759758c
YH
259 matches(argv[0], "show") == 0) {
260 return ipaddrlabel_list(argc-1, argv+1);
261 } else if (matches(argv[0], "add") == 0) {
262 return ipaddrlabel_modify(RTM_NEWADDRLABEL, argc-1, argv+1);
263 } else if (matches(argv[0], "delete") == 0) {
264 return ipaddrlabel_modify(RTM_DELADDRLABEL, argc-1, argv+1);
265 } else if (matches(argv[0], "flush") == 0) {
266 return ipaddrlabel_flush(argc-1, argv+1);
267 } else if (matches(argv[0], "help") == 0)
268 usage();
269
270 fprintf(stderr, "Command \"%s\" is unknown, try \"ip addrlabel help\".\n", *argv);
271 exit(-1);
272}