]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/ipila.c
ip xfrm: support setting XFRMA_SET_MARK_MASK attribute in states
[mirror_iproute2.git] / ip / ipila.c
1 /*
2 * ipila.c ILA (Identifier Locator Addressing) support
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Tom Herbert <tom@herbertland.com>
10 */
11
12 #include <netdb.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <net/if.h>
17 #include <linux/ila.h>
18 #include <linux/genetlink.h>
19 #include <linux/ip.h>
20 #include <arpa/inet.h>
21
22 #include "libgenl.h"
23 #include "utils.h"
24 #include "ip_common.h"
25 #include "ila_common.h"
26 #include "json_print.h"
27
28 static void usage(void)
29 {
30 fprintf(stderr,
31 "Usage: ip ila add loc_match LOCATOR_MATCH loc LOCATOR [ dev DEV ] OPTIONS\n"
32 " ip ila del loc_match LOCATOR_MATCH [ loc LOCATOR ] [ dev DEV ]\n"
33 " ip ila list\n"
34 "OPTIONS := [ csum-mode { adj-transport | neutral-map | neutral-map-auto | no-action } ]\n"
35 " [ ident-type { luid | use-format } ]\n");
36
37 exit(-1);
38 }
39
40 /* netlink socket */
41 static struct rtnl_handle genl_rth = { .fd = -1 };
42 static int genl_family = -1;
43
44 #define ILA_REQUEST(_req, _bufsiz, _cmd, _flags) \
45 GENL_REQUEST(_req, _bufsiz, genl_family, 0, \
46 ILA_GENL_VERSION, _cmd, _flags)
47
48 #define ILA_RTA(g) ((struct rtattr *)(((char *)(g)) + \
49 NLMSG_ALIGN(sizeof(struct genlmsghdr))))
50
51 static void print_addr64(__u64 addr, char *buff, size_t len)
52 {
53 __u16 *words = (__u16 *)&addr;
54 __u16 v;
55 int i, ret;
56 size_t written = 0;
57 char *sep = ":";
58
59 for (i = 0; i < 4; i++) {
60 v = ntohs(words[i]);
61
62 if (i == 3)
63 sep = "";
64
65 ret = snprintf(&buff[written], len - written, "%x%s", v, sep);
66 written += ret;
67 }
68 }
69
70 static void print_ila_locid(const char *tag, int attr, struct rtattr *tb[])
71 {
72 char abuf[256];
73
74 if (tb[attr])
75 print_addr64(rta_getattr_u64(tb[attr]),
76 abuf, sizeof(abuf));
77 else
78 snprintf(abuf, sizeof(abuf), "-");
79
80 /* 20 = sizeof("xxxx:xxxx:xxxx:xxxx") */
81 print_string(PRINT_ANY, tag, "%-20s", abuf);
82 }
83
84 static int print_ila_mapping(struct nlmsghdr *n, void *arg)
85 {
86 struct genlmsghdr *ghdr;
87 struct rtattr *tb[ILA_ATTR_MAX + 1];
88 int len = n->nlmsg_len;
89
90 if (n->nlmsg_type != genl_family)
91 return 0;
92
93 len -= NLMSG_LENGTH(GENL_HDRLEN);
94 if (len < 0)
95 return -1;
96
97 ghdr = NLMSG_DATA(n);
98 parse_rtattr(tb, ILA_ATTR_MAX, (void *) ghdr + GENL_HDRLEN, len);
99
100 open_json_object(NULL);
101 print_ila_locid("locator_match", ILA_ATTR_LOCATOR_MATCH, tb);
102 print_ila_locid("locator", ILA_ATTR_LOCATOR, tb);
103
104 if (tb[ILA_ATTR_IFINDEX]) {
105 __u32 ifindex
106 = rta_getattr_u32(tb[ILA_ATTR_IFINDEX]);
107
108 print_color_string(PRINT_ANY, COLOR_IFNAME,
109 "interface", "%-16s",
110 ll_index_to_name(ifindex));
111 } else {
112 print_string(PRINT_FP, NULL, "%-10s ", "-");
113 }
114
115 if (tb[ILA_ATTR_CSUM_MODE]) {
116 __u8 csum = rta_getattr_u8(tb[ILA_ATTR_CSUM_MODE]);
117
118 print_string(PRINT_ANY, "csum_mode", "%s",
119 ila_csum_mode2name(csum));
120 } else
121 print_string(PRINT_FP, NULL, "%-10s ", "-");
122
123 if (tb[ILA_ATTR_IDENT_TYPE])
124 print_string(PRINT_ANY, "ident_type", "%s",
125 ila_ident_type2name(rta_getattr_u8(
126 tb[ILA_ATTR_IDENT_TYPE])));
127 else
128 print_string(PRINT_FP, NULL, "%s", "-");
129
130 print_nl();
131 close_json_object();
132
133 return 0;
134 }
135
136 #define NLMSG_BUF_SIZE 4096
137
138 static int do_list(int argc, char **argv)
139 {
140 ILA_REQUEST(req, 1024, ILA_CMD_GET, NLM_F_REQUEST | NLM_F_DUMP);
141
142 if (argc > 0) {
143 fprintf(stderr, "\"ip ila show\" does not take "
144 "any arguments.\n");
145 return -1;
146 }
147
148 if (rtnl_send(&genl_rth, (void *)&req, req.n.nlmsg_len) < 0) {
149 perror("Cannot send dump request");
150 exit(1);
151 }
152
153 new_json_obj(json);
154 if (rtnl_dump_filter(&genl_rth, print_ila_mapping, stdout) < 0) {
155 fprintf(stderr, "Dump terminated\n");
156 return 1;
157 }
158 delete_json_obj();
159 fflush(stdout);
160
161 return 0;
162 }
163
164 static int ila_parse_opt(int argc, char **argv, struct nlmsghdr *n,
165 bool adding)
166 {
167 __u64 locator = 0;
168 __u64 locator_match = 0;
169 int ifindex = 0;
170 int csum_mode = 0;
171 int ident_type = 0;
172 bool loc_set = false;
173 bool loc_match_set = false;
174 bool ifindex_set = false;
175 bool csum_mode_set = false;
176 bool ident_type_set = false;
177
178 while (argc > 0) {
179 if (!matches(*argv, "loc")) {
180 NEXT_ARG();
181
182 if (get_addr64(&locator, *argv) < 0) {
183 fprintf(stderr, "Bad locator: %s\n", *argv);
184 return -1;
185 }
186 loc_set = true;
187 } else if (!matches(*argv, "loc_match")) {
188 NEXT_ARG();
189
190 if (get_addr64(&locator_match, *argv) < 0) {
191 fprintf(stderr, "Bad locator to match: %s\n",
192 *argv);
193 return -1;
194 }
195 loc_match_set = true;
196 } else if (!matches(*argv, "csum-mode")) {
197 NEXT_ARG();
198
199 csum_mode = ila_csum_name2mode(*argv);
200 if (csum_mode < 0) {
201 fprintf(stderr, "Bad csum-mode: %s\n",
202 *argv);
203 return -1;
204 }
205 csum_mode_set = true;
206 } else if (!matches(*argv, "ident-type")) {
207 NEXT_ARG();
208
209 ident_type = ila_ident_name2type(*argv);
210 if (ident_type < 0) {
211 fprintf(stderr, "Bad ident-type: %s\n",
212 *argv);
213 return -1;
214 }
215 ident_type_set = true;
216 } else if (!matches(*argv, "dev")) {
217 NEXT_ARG();
218
219 ifindex = ll_name_to_index(*argv);
220 if (ifindex == 0) {
221 fprintf(stderr, "No such interface: %s\n",
222 *argv);
223 return -1;
224 }
225 ifindex_set = true;
226 } else {
227 usage();
228 return -1;
229 }
230 argc--, argv++;
231 }
232
233 if (adding) {
234 if (!loc_set) {
235 fprintf(stderr, "ila: missing locator\n");
236 return -1;
237 }
238 if (!loc_match_set) {
239 fprintf(stderr, "ila: missing locator0match\n");
240 return -1;
241 }
242 }
243
244 if (loc_match_set)
245 addattr64(n, 1024, ILA_ATTR_LOCATOR_MATCH, locator_match);
246
247 if (loc_set)
248 addattr64(n, 1024, ILA_ATTR_LOCATOR, locator);
249
250 if (ifindex_set)
251 addattr32(n, 1024, ILA_ATTR_IFINDEX, ifindex);
252
253 if (csum_mode_set)
254 addattr8(n, 1024, ILA_ATTR_CSUM_MODE, csum_mode);
255
256 if (ident_type_set)
257 addattr8(n, 1024, ILA_ATTR_IDENT_TYPE, ident_type);
258
259 return 0;
260 }
261
262 static int do_add(int argc, char **argv)
263 {
264 ILA_REQUEST(req, 1024, ILA_CMD_ADD, NLM_F_REQUEST);
265
266 ila_parse_opt(argc, argv, &req.n, true);
267
268 if (rtnl_talk(&genl_rth, &req.n, NULL) < 0)
269 return -2;
270
271 return 0;
272 }
273
274 static int do_del(int argc, char **argv)
275 {
276 ILA_REQUEST(req, 1024, ILA_CMD_DEL, NLM_F_REQUEST);
277
278 ila_parse_opt(argc, argv, &req.n, false);
279
280 if (rtnl_talk(&genl_rth, &req.n, NULL) < 0)
281 return -2;
282
283 return 0;
284 }
285
286 int do_ipila(int argc, char **argv)
287 {
288 if (argc < 1)
289 usage();
290
291 if (matches(*argv, "help") == 0)
292 usage();
293
294 if (genl_init_handle(&genl_rth, ILA_GENL_NAME, &genl_family))
295 exit(1);
296
297 if (matches(*argv, "add") == 0)
298 return do_add(argc-1, argv+1);
299 if (matches(*argv, "delete") == 0)
300 return do_del(argc-1, argv+1);
301 if (matches(*argv, "list") == 0)
302 return do_list(argc-1, argv+1);
303
304 fprintf(stderr, "Command \"%s\" is unknown, try \"ip ila help\".\n",
305 *argv);
306 exit(-1);
307 }