]>
git.proxmox.com Git - mirror_iproute2.git/blob - ip/ipaddrlabel.c
2 * ipaddrlabel.c "ip addrlabel"
4 * Copyright (C)2007 USAGI/WIDE Project
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.
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.
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
23 * Authors: YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <netinet/ip.h>
35 #include <arpa/inet.h>
37 #include <linux/types.h>
38 #include <linux/if_addrlabel.h>
42 #include "ip_common.h"
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))
47 extern struct rtnl_handle rth
;
49 static void usage(void) __attribute__((noreturn
));
51 static void usage(void)
53 fprintf(stderr
, "Usage: ip addrlabel [ list | add | del | flush ] prefix PREFIX [ dev DEV ] [ label LABEL ]\n");
57 int print_addrlabel(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
, void *arg
)
59 FILE *fp
= (FILE*)arg
;
60 struct ifaddrlblmsg
*ifal
= NLMSG_DATA(n
);
61 int len
= n
->nlmsg_len
;
63 struct rtattr
*tb
[IFAL_MAX
+1];
66 if (n
->nlmsg_type
!= RTM_NEWADDRLABEL
&& n
->nlmsg_type
!= RTM_DELADDRLABEL
)
69 len
-= NLMSG_LENGTH(sizeof(*ifal
));
73 parse_rtattr(tb
, IFAL_MAX
, IFAL_RTA(ifal
), len
);
75 if (ifal
->ifal_family
== AF_INET
)
77 else if (ifal
->ifal_family
== AF_INET6
)
80 if (n
->nlmsg_type
== RTM_DELADDRLABEL
)
81 fprintf(fp
, "Deleted ");
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
]),
89 ifal
->ifal_prefixlen
);
93 fprintf(fp
, "dev %s ", ll_index_to_name(ifal
->ifal_index
));
95 if (tb
[IFAL_LABEL
] && RTA_PAYLOAD(tb
[IFAL_LABEL
]) == sizeof(int32_t)) {
97 memcpy(&label
, RTA_DATA(tb
[IFAL_LABEL
]), sizeof(label
));
98 fprintf(fp
, "label %d ", label
);
106 static int ipaddrlabel_list(int argc
, char **argv
)
108 int af
= preferred_family
;
114 fprintf(stderr
, "\"ip addrlabel show\" does not take any arguments.\n");
118 if (rtnl_wilddump_request(&rth
, af
, RTM_GETADDRLABEL
) < 0) {
119 perror("Cannot send dump request");
123 if (rtnl_dump_filter(&rth
, print_addrlabel
, stdout
, NULL
, NULL
) < 0) {
124 fprintf(stderr
, "Dump terminated\n");
132 static int ipaddrlabel_modify(int cmd
, int argc
, char **argv
)
136 struct ifaddrlblmsg ifal
;
141 uint32_t label
= 0xffffffffUL
;
145 memset(&req
, 0, sizeof(req
));
146 memset(&prefix
, 0, sizeof(prefix
));
148 req
.n
.nlmsg_type
= cmd
;
149 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrlblmsg
));
150 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
151 req
.ifal
.ifal_family
= preferred_family
;
152 req
.ifal
.ifal_prefixlen
= 0;
153 req
.ifal
.ifal_index
= 0;
155 if (cmd
== RTM_NEWADDRLABEL
) {
156 req
.n
.nlmsg_flags
|= NLM_F_CREATE
|NLM_F_EXCL
;
160 if (strcmp(*argv
, "prefix") == 0) {
163 get_prefix(&prefix
, *argv
, preferred_family
);
164 } else if (strcmp(*argv
, "dev") == 0) {
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) {
171 if (get_u32(&label
, *argv
, 0) || label
== 0xffffffffUL
)
172 invarg("label is invalid\n", *argv
);
178 fprintf(stderr
, "Not enough information: \"prefix\" argument is required.\n");
182 fprintf(stderr
, "Not enough information: \"label\" argument is required.\n");
185 addattr32(&req
.n
, sizeof(req
), IFAL_LABEL
, label
);
186 addattr_l(&req
.n
, sizeof(req
), IFAL_ADDRESS
, &prefix
.data
, prefix
.bytelen
);
187 req
.ifal
.ifal_prefixlen
= prefix
.bitlen
;
189 if (req
.ifal
.ifal_family
== AF_UNSPEC
)
190 req
.ifal
.ifal_family
= AF_INET6
;
192 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0)
199 static int flush_addrlabel(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
, void *arg
)
201 struct rtnl_handle rth2
;
202 struct rtmsg
*r
= NLMSG_DATA(n
);
203 int len
= n
->nlmsg_len
;
204 struct rtattr
* tb
[IFAL_MAX
+1];
206 len
-= NLMSG_LENGTH(sizeof(*r
));
210 parse_rtattr(tb
, IFAL_MAX
, RTM_RTA(r
), len
);
212 if (tb
[IFAL_ADDRESS
]) {
213 n
->nlmsg_type
= RTM_DELADDRLABEL
;
214 n
->nlmsg_flags
= NLM_F_REQUEST
;
216 if (rtnl_open(&rth2
, 0) < 0)
219 if (rtnl_talk(&rth2
, n
, 0, 0, NULL
, NULL
, NULL
) < 0)
228 static int ipaddrlabel_flush(int argc
, char **argv
)
230 int af
= preferred_family
;
236 fprintf(stderr
, "\"ip addrlabel flush\" does not allow extra arguments\n");
240 if (rtnl_wilddump_request(&rth
, af
, RTM_GETADDRLABEL
) < 0) {
241 perror("Cannot send dump request");
245 if (rtnl_dump_filter(&rth
, flush_addrlabel
, NULL
, NULL
, NULL
) < 0) {
246 fprintf(stderr
, "Flush terminated\n");
253 int do_ipaddrlabel(int argc
, char **argv
)
256 return ipaddrlabel_list(0, NULL
);
257 } else if (matches(argv
[0], "list") == 0 ||
258 matches(argv
[0], "show") == 0) {
259 return ipaddrlabel_list(argc
-1, argv
+1);
260 } else if (matches(argv
[0], "add") == 0) {
261 return ipaddrlabel_modify(RTM_NEWADDRLABEL
, argc
-1, argv
+1);
262 } else if (matches(argv
[0], "delete") == 0) {
263 return ipaddrlabel_modify(RTM_DELADDRLABEL
, argc
-1, argv
+1);
264 } else if (matches(argv
[0], "flush") == 0) {
265 return ipaddrlabel_flush(argc
-1, argv
+1);
266 } else if (matches(argv
[0], "help") == 0)
269 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip addrlabel help\".\n", *argv
);