]>
Commit | Line | Data |
---|---|---|
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> | |
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 | ||
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 | |
46 | extern struct rtnl_handle rth; | |
47 | ||
48 | static void usage(void) __attribute__((noreturn)); | |
49 | ||
50 | static 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 | ||
57 | int print_addrlabel(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) | |
58 | { | |
56f5daac | 59 | FILE *fp = (FILE *)arg; |
4759758c YH |
60 | struct ifaddrlblmsg *ifal = NLMSG_DATA(n); |
61 | int len = n->nlmsg_len; | |
4759758c | 62 | struct rtattr *tb[IFAL_MAX+1]; |
4759758c YH |
63 | |
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 | ||
4759758c YH |
73 | if (n->nlmsg_type == RTM_DELADDRLABEL) |
74 | fprintf(fp, "Deleted "); | |
75 | ||
76 | if (tb[IFAL_ADDRESS]) { | |
77 | fprintf(fp, "prefix %s/%u ", | |
d49f934c PS |
78 | format_host_rta(ifal->ifal_family, |
79 | tb[IFAL_ADDRESS]), | |
4759758c YH |
80 | ifal->ifal_prefixlen); |
81 | } | |
82 | ||
83 | if (ifal->ifal_index) | |
84 | fprintf(fp, "dev %s ", ll_index_to_name(ifal->ifal_index)); | |
85 | ||
97870334 HL |
86 | if (tb[IFAL_LABEL] && RTA_PAYLOAD(tb[IFAL_LABEL]) == sizeof(uint32_t)) { |
87 | uint32_t label; | |
56f5daac | 88 | |
4759758c | 89 | memcpy(&label, RTA_DATA(tb[IFAL_LABEL]), sizeof(label)); |
97870334 | 90 | fprintf(fp, "label %u ", label); |
4759758c YH |
91 | } |
92 | ||
93 | fprintf(fp, "\n"); | |
94 | fflush(fp); | |
95 | return 0; | |
96 | } | |
97 | ||
98 | static 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 | ||
cd70f3f5 | 115 | if (rtnl_dump_filter(&rth, print_addrlabel, stdout) < 0) { |
4759758c YH |
116 | fprintf(stderr, "Dump terminated\n"); |
117 | return 1; | |
118 | } | |
119 | ||
120 | return 0; | |
121 | } | |
122 | ||
123 | ||
124 | static int ipaddrlabel_modify(int cmd, int argc, char **argv) | |
125 | { | |
126 | struct { | |
4806867a | 127 | struct nlmsghdr n; |
4759758c | 128 | struct ifaddrlblmsg ifal; |
56f5daac | 129 | char buf[1024]; |
d17b136f PS |
130 | } req = { |
131 | .n.nlmsg_type = cmd, | |
132 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrlblmsg)), | |
133 | .n.nlmsg_flags = NLM_F_REQUEST, | |
134 | .ifal.ifal_family = preferred_family, | |
135 | }; | |
136 | ||
137 | inet_prefix prefix = {}; | |
4759758c | 138 | uint32_t label = 0xffffffffUL; |
6579feea | 139 | char *p = NULL; |
0612519e | 140 | char *l = NULL; |
4759758c | 141 | |
4759758c YH |
142 | if (cmd == RTM_NEWADDRLABEL) { |
143 | req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL; | |
144 | } | |
145 | ||
146 | while (argc > 0) { | |
147 | if (strcmp(*argv, "prefix") == 0) { | |
148 | NEXT_ARG(); | |
6579feea | 149 | p = *argv; |
4759758c YH |
150 | get_prefix(&prefix, *argv, preferred_family); |
151 | } else if (strcmp(*argv, "dev") == 0) { | |
152 | NEXT_ARG(); | |
153 | if ((req.ifal.ifal_index = ll_name_to_index(*argv)) == 0) | |
154 | invarg("dev is invalid\n", *argv); | |
155 | } else if (strcmp(*argv, "label") == 0) { | |
156 | NEXT_ARG(); | |
6579feea | 157 | l = *argv; |
4759758c YH |
158 | if (get_u32(&label, *argv, 0) || label == 0xffffffffUL) |
159 | invarg("label is invalid\n", *argv); | |
160 | } | |
161 | argc--; | |
162 | argv++; | |
163 | } | |
6579feea VC |
164 | if (p == NULL) { |
165 | fprintf(stderr, "Not enough information: \"prefix\" argument is required.\n"); | |
166 | return -1; | |
167 | } | |
168 | if (l == NULL) { | |
169 | fprintf(stderr, "Not enough information: \"label\" argument is required.\n"); | |
170 | return -1; | |
171 | } | |
4759758c YH |
172 | addattr32(&req.n, sizeof(req), IFAL_LABEL, label); |
173 | addattr_l(&req.n, sizeof(req), IFAL_ADDRESS, &prefix.data, prefix.bytelen); | |
3490740b | 174 | req.ifal.ifal_prefixlen = prefix.bitlen; |
4759758c YH |
175 | |
176 | if (req.ifal.ifal_family == AF_UNSPEC) | |
177 | req.ifal.ifal_family = AF_INET6; | |
178 | ||
c079e121 | 179 | if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) |
906cafe3 | 180 | return -2; |
4759758c YH |
181 | |
182 | return 0; | |
183 | } | |
184 | ||
185 | ||
186 | static int flush_addrlabel(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) | |
187 | { | |
188 | struct rtnl_handle rth2; | |
189 | struct rtmsg *r = NLMSG_DATA(n); | |
190 | int len = n->nlmsg_len; | |
56f5daac | 191 | struct rtattr *tb[IFAL_MAX+1]; |
4759758c YH |
192 | |
193 | len -= NLMSG_LENGTH(sizeof(*r)); | |
194 | if (len < 0) | |
195 | return -1; | |
196 | ||
197 | parse_rtattr(tb, IFAL_MAX, RTM_RTA(r), len); | |
198 | ||
199 | if (tb[IFAL_ADDRESS]) { | |
200 | n->nlmsg_type = RTM_DELADDRLABEL; | |
201 | n->nlmsg_flags = NLM_F_REQUEST; | |
202 | ||
203 | if (rtnl_open(&rth2, 0) < 0) | |
204 | return -1; | |
205 | ||
c079e121 | 206 | if (rtnl_talk(&rth2, n, NULL, 0) < 0) |
4759758c YH |
207 | return -2; |
208 | ||
209 | rtnl_close(&rth2); | |
210 | } | |
211 | ||
212 | return 0; | |
213 | } | |
214 | ||
215 | static int ipaddrlabel_flush(int argc, char **argv) | |
216 | { | |
217 | int af = preferred_family; | |
218 | ||
219 | if (af == AF_UNSPEC) | |
220 | af = AF_INET6; | |
221 | ||
222 | if (argc > 0) { | |
223 | fprintf(stderr, "\"ip addrlabel flush\" does not allow extra arguments\n"); | |
224 | return -1; | |
225 | } | |
226 | ||
227 | if (rtnl_wilddump_request(&rth, af, RTM_GETADDRLABEL) < 0) { | |
228 | perror("Cannot send dump request"); | |
906cafe3 | 229 | return -1; |
4759758c YH |
230 | } |
231 | ||
cd70f3f5 | 232 | if (rtnl_dump_filter(&rth, flush_addrlabel, NULL) < 0) { |
4759758c | 233 | fprintf(stderr, "Flush terminated\n"); |
906cafe3 | 234 | return -1; |
4759758c YH |
235 | } |
236 | ||
237 | return 0; | |
238 | } | |
239 | ||
240 | int do_ipaddrlabel(int argc, char **argv) | |
241 | { | |
242 | if (argc < 1) { | |
243 | return ipaddrlabel_list(0, NULL); | |
244 | } else if (matches(argv[0], "list") == 0 || | |
473544d9 | 245 | matches(argv[0], "lst") == 0 || |
4759758c YH |
246 | matches(argv[0], "show") == 0) { |
247 | return ipaddrlabel_list(argc-1, argv+1); | |
248 | } else if (matches(argv[0], "add") == 0) { | |
249 | return ipaddrlabel_modify(RTM_NEWADDRLABEL, argc-1, argv+1); | |
250 | } else if (matches(argv[0], "delete") == 0) { | |
251 | return ipaddrlabel_modify(RTM_DELADDRLABEL, argc-1, argv+1); | |
252 | } else if (matches(argv[0], "flush") == 0) { | |
253 | return ipaddrlabel_flush(argc-1, argv+1); | |
254 | } else if (matches(argv[0], "help") == 0) | |
255 | usage(); | |
256 | ||
257 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip addrlabel help\".\n", *argv); | |
258 | exit(-1); | |
259 | } |