]>
Commit | Line | Data |
---|---|---|
93863328 DL |
1 | /* |
2 | * seg6.c "ip sr/seg6" | |
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 | * version 2 as published by the Free Software Foundation; | |
7 | * | |
8 | * Author: David Lebrun <david.lebrun@uclouvain.be> | |
9 | */ | |
10 | ||
11 | #include <stdio.h> | |
12 | #include <stdlib.h> | |
13 | #include <string.h> | |
14 | #include <unistd.h> | |
15 | #include <errno.h> | |
16 | #include <sys/types.h> | |
17 | #include <sys/socket.h> | |
18 | #include <arpa/inet.h> | |
19 | #include <sys/ioctl.h> | |
20 | #include <linux/if.h> | |
21 | ||
22 | #include <linux/genetlink.h> | |
23 | #include <linux/seg6_genl.h> | |
24 | #include <linux/seg6_hmac.h> | |
25 | ||
26 | #include "utils.h" | |
27 | #include "ip_common.h" | |
28 | #include "libgenl.h" | |
111f79ad | 29 | #include "json_print.h" |
93863328 DL |
30 | |
31 | #define HMAC_KEY_PROMPT "Enter secret for HMAC key ID (blank to delete): " | |
32 | ||
33 | static void usage(void) | |
34 | { | |
35 | fprintf(stderr, "Usage: ip sr { COMMAND | help }\n"); | |
36 | fprintf(stderr, " ip sr hmac show\n"); | |
37 | fprintf(stderr, " ip sr hmac set KEYID ALGO\n"); | |
38 | fprintf(stderr, " ip sr tunsrc show\n"); | |
39 | fprintf(stderr, " ip sr tunsrc set ADDRESS\n"); | |
40 | fprintf(stderr, "where ALGO := { sha1 | sha256 }\n"); | |
41 | exit(-1); | |
42 | } | |
43 | ||
44 | static struct rtnl_handle grth = { .fd = -1 }; | |
45 | static int genl_family = -1; | |
46 | ||
47 | #define SEG6_REQUEST(_req, _bufsiz, _cmd, _flags) \ | |
48 | GENL_REQUEST(_req, _bufsiz, genl_family, 0, \ | |
49 | SEG6_GENL_VERSION, _cmd, _flags) | |
50 | ||
51 | static struct { | |
52 | unsigned int cmd; | |
6caad8f5 | 53 | inet_prefix addr; |
93863328 DL |
54 | __u32 keyid; |
55 | const char *pass; | |
56 | __u8 alg_id; | |
57 | } opts; | |
58 | ||
111f79ad SH |
59 | static void print_dumphmac(struct rtattr *attrs[]) |
60 | { | |
61 | char secret[64]; | |
62 | char *algstr; | |
63 | __u8 slen = rta_getattr_u8(attrs[SEG6_ATTR_SECRETLEN]); | |
64 | __u8 alg_id = rta_getattr_u8(attrs[SEG6_ATTR_ALGID]); | |
65 | ||
66 | memset(secret, 0, 64); | |
67 | ||
68 | if (slen > 63) { | |
69 | fprintf(stderr, "HMAC secret length %d > 63, truncated\n", slen); | |
70 | slen = 63; | |
71 | } | |
72 | ||
73 | memcpy(secret, RTA_DATA(attrs[SEG6_ATTR_SECRET]), slen); | |
74 | ||
75 | switch (alg_id) { | |
76 | case SEG6_HMAC_ALGO_SHA1: | |
77 | algstr = "sha1"; | |
78 | break; | |
79 | case SEG6_HMAC_ALGO_SHA256: | |
80 | algstr = "sha256"; | |
81 | break; | |
82 | default: | |
83 | algstr = "<unknown>"; | |
84 | } | |
85 | ||
86 | print_uint(PRINT_ANY, "hmac", "hmac %u ", | |
87 | rta_getattr_u32(attrs[SEG6_ATTR_HMACKEYID])); | |
88 | print_string(PRINT_ANY, "algo", "algo %s ", algstr); | |
89 | print_string(PRINT_ANY, "secret", "secret \"%s\"\n", secret); | |
90 | } | |
91 | ||
92 | static void print_tunsrc(struct rtattr *attrs[]) | |
93 | { | |
94 | const char *dst | |
95 | = rt_addr_n2a(AF_INET6, 16, | |
96 | RTA_DATA(attrs[SEG6_ATTR_DST])); | |
97 | ||
98 | print_string(PRINT_ANY, "tunsrc", | |
99 | "tunsrc addr %s\n", dst); | |
100 | } | |
101 | ||
93863328 DL |
102 | static int process_msg(const struct sockaddr_nl *who, struct nlmsghdr *n, |
103 | void *arg) | |
104 | { | |
105 | struct rtattr *attrs[SEG6_ATTR_MAX + 1]; | |
106 | struct genlmsghdr *ghdr; | |
93863328 DL |
107 | int len = n->nlmsg_len; |
108 | ||
109 | if (n->nlmsg_type != genl_family) | |
110 | return -1; | |
111 | ||
112 | len -= NLMSG_LENGTH(GENL_HDRLEN); | |
113 | if (len < 0) | |
114 | return -1; | |
115 | ||
116 | ghdr = NLMSG_DATA(n); | |
117 | ||
118 | parse_rtattr(attrs, SEG6_ATTR_MAX, (void *)ghdr + GENL_HDRLEN, len); | |
119 | ||
111f79ad | 120 | open_json_object(NULL); |
93863328 DL |
121 | switch (ghdr->cmd) { |
122 | case SEG6_CMD_DUMPHMAC: | |
111f79ad | 123 | print_dumphmac(attrs); |
93863328 | 124 | break; |
111f79ad | 125 | |
93863328 | 126 | case SEG6_CMD_GET_TUNSRC: |
111f79ad | 127 | print_tunsrc(attrs); |
93863328 DL |
128 | break; |
129 | } | |
111f79ad | 130 | close_json_object(); |
93863328 DL |
131 | |
132 | return 0; | |
133 | } | |
134 | ||
135 | static int seg6_do_cmd(void) | |
136 | { | |
137 | SEG6_REQUEST(req, 1024, opts.cmd, NLM_F_REQUEST); | |
86bf43c7 | 138 | struct nlmsghdr *answer; |
93863328 DL |
139 | int repl = 0, dump = 0; |
140 | ||
141 | if (genl_family < 0) { | |
142 | if (rtnl_open_byproto(&grth, 0, NETLINK_GENERIC) < 0) { | |
143 | fprintf(stderr, "Cannot open generic netlink socket\n"); | |
144 | exit(1); | |
145 | } | |
146 | genl_family = genl_resolve_family(&grth, SEG6_GENL_NAME); | |
147 | if (genl_family < 0) | |
148 | exit(1); | |
149 | req.n.nlmsg_type = genl_family; | |
150 | } | |
151 | ||
152 | switch (opts.cmd) { | |
153 | case SEG6_CMD_SETHMAC: | |
154 | { | |
155 | addattr32(&req.n, sizeof(req), SEG6_ATTR_HMACKEYID, opts.keyid); | |
156 | addattr8(&req.n, sizeof(req), SEG6_ATTR_SECRETLEN, | |
157 | strlen(opts.pass)); | |
158 | addattr8(&req.n, sizeof(req), SEG6_ATTR_ALGID, opts.alg_id); | |
159 | if (strlen(opts.pass)) | |
160 | addattr_l(&req.n, sizeof(req), SEG6_ATTR_SECRET, | |
161 | opts.pass, strlen(opts.pass)); | |
162 | break; | |
163 | } | |
164 | case SEG6_CMD_SET_TUNSRC: | |
6caad8f5 | 165 | addattr_l(&req.n, sizeof(req), SEG6_ATTR_DST, opts.addr.data, |
93863328 DL |
166 | sizeof(struct in6_addr)); |
167 | break; | |
168 | case SEG6_CMD_DUMPHMAC: | |
169 | dump = 1; | |
170 | break; | |
171 | case SEG6_CMD_GET_TUNSRC: | |
172 | repl = 1; | |
173 | break; | |
174 | } | |
175 | ||
176 | if (!repl && !dump) { | |
86bf43c7 | 177 | if (rtnl_talk(&grth, &req.n, NULL) < 0) |
93863328 DL |
178 | return -1; |
179 | } else if (repl) { | |
86bf43c7 | 180 | if (rtnl_talk(&grth, &req.n, &answer) < 0) |
93863328 | 181 | return -2; |
111f79ad | 182 | new_json_obj(json); |
86bf43c7 | 183 | if (process_msg(NULL, answer, stdout) < 0) { |
93863328 DL |
184 | fprintf(stderr, "Error parsing reply\n"); |
185 | exit(1); | |
186 | } | |
111f79ad | 187 | delete_json_obj(); |
86bf43c7 | 188 | free(answer); |
93863328 DL |
189 | } else { |
190 | req.n.nlmsg_flags |= NLM_F_DUMP; | |
191 | req.n.nlmsg_seq = grth.dump = ++grth.seq; | |
192 | if (rtnl_send(&grth, &req, req.n.nlmsg_len) < 0) { | |
193 | perror("Failed to send dump request"); | |
194 | exit(1); | |
195 | } | |
196 | ||
111f79ad | 197 | new_json_obj(json); |
93863328 DL |
198 | if (rtnl_dump_filter(&grth, process_msg, stdout) < 0) { |
199 | fprintf(stderr, "Dump terminated\n"); | |
200 | exit(1); | |
201 | } | |
111f79ad SH |
202 | delete_json_obj(); |
203 | fflush(stdout); | |
93863328 DL |
204 | } |
205 | ||
206 | return 0; | |
207 | } | |
208 | ||
209 | int do_seg6(int argc, char **argv) | |
210 | { | |
211 | if (argc < 1 || matches(*argv, "help") == 0) | |
212 | usage(); | |
213 | ||
214 | memset(&opts, 0, sizeof(opts)); | |
215 | ||
216 | if (matches(*argv, "hmac") == 0) { | |
217 | NEXT_ARG(); | |
218 | if (matches(*argv, "show") == 0) { | |
219 | opts.cmd = SEG6_CMD_DUMPHMAC; | |
220 | } else if (matches(*argv, "set") == 0) { | |
221 | NEXT_ARG(); | |
222 | if (get_u32(&opts.keyid, *argv, 0) || opts.keyid == 0) | |
223 | invarg("hmac KEYID value is invalid", *argv); | |
224 | NEXT_ARG(); | |
225 | if (strcmp(*argv, "sha1") == 0) { | |
226 | opts.alg_id = SEG6_HMAC_ALGO_SHA1; | |
227 | } else if (strcmp(*argv, "sha256") == 0) { | |
228 | opts.alg_id = SEG6_HMAC_ALGO_SHA256; | |
229 | } else { | |
230 | invarg("hmac ALGO value is invalid", *argv); | |
231 | } | |
232 | opts.cmd = SEG6_CMD_SETHMAC; | |
233 | opts.pass = getpass(HMAC_KEY_PROMPT); | |
234 | } else { | |
235 | invarg("unknown", *argv); | |
236 | } | |
237 | } else if (matches(*argv, "tunsrc") == 0) { | |
238 | NEXT_ARG(); | |
239 | if (matches(*argv, "show") == 0) { | |
240 | opts.cmd = SEG6_CMD_GET_TUNSRC; | |
241 | } else if (matches(*argv, "set") == 0) { | |
242 | NEXT_ARG(); | |
243 | opts.cmd = SEG6_CMD_SET_TUNSRC; | |
6caad8f5 | 244 | get_addr(&opts.addr, *argv, AF_INET6); |
93863328 DL |
245 | } else { |
246 | invarg("unknown", *argv); | |
247 | } | |
248 | } else { | |
249 | invarg("unknown", *argv); | |
250 | } | |
251 | ||
252 | return seg6_do_cmd(); | |
253 | } |