]> git.proxmox.com Git - mirror_frr.git/blame - nhrpd/nhrp_vty.c
Merge pull request #5810 from donaldsharp/fix_yang_routemap
[mirror_frr.git] / nhrpd / nhrp_vty.c
CommitLineData
2fb975da
TT
1/* NHRP vty handling
2 * Copyright (c) 2014-2015 Timo Teräs
3 *
4 * This file is free software: you may copy, redistribute and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 */
9
10#include "zebra.h"
11#include "command.h"
12#include "zclient.h"
13#include "stream.h"
901fda98 14#include "filter.h"
2fb975da
TT
15
16#include "nhrpd.h"
17#include "netlink.h"
18
19static struct cmd_node zebra_node = {
996c9314 20 .node = ZEBRA_NODE,
2fb975da 21 .prompt = "%s(config-router)# ",
996c9314 22 .vtysh = 1,
2fb975da
TT
23};
24
25static struct cmd_node nhrp_interface_node = {
996c9314 26 .node = INTERFACE_NODE,
2fb975da 27 .prompt = "%s(config-if)# ",
996c9314 28 .vtysh = 1,
2fb975da
TT
29};
30
819dc8bb 31#define NHRP_DEBUG_FLAGS_CMD "<all|common|event|interface|kernel|route|vici>"
2fb975da 32
996c9314
LB
33#define NHRP_DEBUG_FLAGS_STR \
34 "All messages\n" \
35 "Common messages (default)\n" \
36 "Event manager messages\n" \
37 "Interface messages\n" \
38 "Kernel messages\n" \
39 "Route messages\n" \
2fb975da
TT
40 "VICI messages\n"
41
42static const struct message debug_flags_desc[] = {
996c9314
LB
43 {NHRP_DEBUG_ALL, "all"}, {NHRP_DEBUG_COMMON, "common"},
44 {NHRP_DEBUG_IF, "interface"}, {NHRP_DEBUG_KERNEL, "kernel"},
45 {NHRP_DEBUG_ROUTE, "route"}, {NHRP_DEBUG_VICI, "vici"},
46 {NHRP_DEBUG_EVENT, "event"}, {0}};
2fb975da
TT
47
48static const struct message interface_flags_desc[] = {
996c9314
LB
49 {NHRP_IFF_SHORTCUT, "shortcut"},
50 {NHRP_IFF_REDIRECT, "redirect"},
51 {NHRP_IFF_REG_NO_UNIQUE, "registration no-unique"},
52 {0}};
2fb975da
TT
53
54static int nhrp_vty_return(struct vty *vty, int ret)
55{
996c9314
LB
56 static const char *const errmsgs[] = {
57 [NHRP_ERR_FAIL] = "Command failed",
58 [NHRP_ERR_NO_MEMORY] = "Out of memory",
59 [NHRP_ERR_UNSUPPORTED_INTERFACE] =
60 "NHRP not supported on this interface",
61 [NHRP_ERR_NHRP_NOT_ENABLED] =
62 "NHRP not enabled (set 'nhrp network-id' first)",
63 [NHRP_ERR_ENTRY_EXISTS] = "Entry exists already",
64 [NHRP_ERR_ENTRY_NOT_FOUND] = "Entry not found",
65 [NHRP_ERR_PROTOCOL_ADDRESS_MISMATCH] =
66 "Protocol address family does not match command (ip/ipv6 mismatch)",
2fb975da
TT
67 };
68 const char *str = NULL;
69 char buf[256];
70
71 if (ret == NHRP_OK)
72 return CMD_SUCCESS;
73
6c8ca260 74 if (ret > 0 && ret <= NHRP_ERR_MAX)
2fb975da
TT
75 if (errmsgs[ret])
76 str = errmsgs[ret];
77
78 if (!str) {
79 str = buf;
80 snprintf(buf, sizeof(buf), "Unknown error %d", ret);
81 }
82
996c9314 83 vty_out(vty, "%% %s\n", str);
2fb975da 84
996c9314
LB
85 return CMD_WARNING_CONFIG_FAILED;
86 ;
2fb975da
TT
87}
88
996c9314
LB
89static int toggle_flag(struct vty *vty, const struct message *flag_desc,
90 const char *name, int on_off, unsigned *flags)
2fb975da
TT
91{
92 int i;
93
94 for (i = 0; flag_desc[i].str != NULL; i++) {
95 if (strcmp(flag_desc[i].str, name) != 0)
96 continue;
97 if (on_off)
98 *flags |= flag_desc[i].key;
99 else
100 *flags &= ~flag_desc[i].key;
101 return CMD_SUCCESS;
102 }
103
996c9314
LB
104 vty_out(vty, "%% Invalid value %s\n", name);
105 return CMD_WARNING_CONFIG_FAILED;
106 ;
2fb975da
TT
107}
108
109#ifndef NO_DEBUG
110
87f6dc50
DS
111DEFUN_NOSH(show_debugging_nhrp, show_debugging_nhrp_cmd,
112 "show debugging [nhrp]",
113 SHOW_STR
114 "Debugging information\n"
115 "NHRP configuration\n")
2fb975da
TT
116{
117 int i;
118
996c9314 119 vty_out(vty, "NHRP debugging status:\n");
2fb975da
TT
120
121 for (i = 0; debug_flags_desc[i].str != NULL; i++) {
122 if (debug_flags_desc[i].key == NHRP_DEBUG_ALL)
123 continue;
124 if (!(debug_flags_desc[i].key & debug_flags))
125 continue;
126
996c9314 127 vty_out(vty, " NHRP %s debugging is on\n",
96ade3ed 128 debug_flags_desc[i].str);
2fb975da
TT
129 }
130
131 return CMD_SUCCESS;
132}
133
134DEFUN(debug_nhrp, debug_nhrp_cmd,
135 "debug nhrp " NHRP_DEBUG_FLAGS_CMD,
136 "Enable debug messages for specific or all parts.\n"
137 "NHRP information\n"
138 NHRP_DEBUG_FLAGS_STR)
139{
996c9314
LB
140 return toggle_flag(vty, debug_flags_desc, argv[2]->text, 1,
141 &debug_flags);
2fb975da
TT
142}
143
144DEFUN(no_debug_nhrp, no_debug_nhrp_cmd,
145 "no debug nhrp " NHRP_DEBUG_FLAGS_CMD,
146 NO_STR
147 "Disable debug messages for specific or all parts.\n"
148 "NHRP information\n"
149 NHRP_DEBUG_FLAGS_STR)
150{
996c9314
LB
151 return toggle_flag(vty, debug_flags_desc, argv[3]->text, 0,
152 &debug_flags);
2fb975da
TT
153}
154
155#endif /* NO_DEBUG */
156
157static int nhrp_config_write(struct vty *vty)
158{
159#ifndef NO_DEBUG
160 if (debug_flags == NHRP_DEBUG_ALL) {
996c9314 161 vty_out(vty, "debug nhrp all\n");
2fb975da
TT
162 } else {
163 int i;
164
165 for (i = 0; debug_flags_desc[i].str != NULL; i++) {
166 if (debug_flags_desc[i].key == NHRP_DEBUG_ALL)
167 continue;
168 if (!(debug_flags & debug_flags_desc[i].key))
169 continue;
996c9314
LB
170 vty_out(vty, "debug nhrp %s\n",
171 debug_flags_desc[i].str);
2fb975da
TT
172 }
173 }
996c9314 174 vty_out(vty, "!\n");
2fb975da
TT
175#endif /* NO_DEBUG */
176
177 if (nhrp_event_socket_path) {
996c9314 178 vty_out(vty, "nhrp event socket %s\n", nhrp_event_socket_path);
2fb975da
TT
179 }
180 if (netlink_nflog_group) {
996c9314 181 vty_out(vty, "nhrp nflog-group %d\n", netlink_nflog_group);
2fb975da
TT
182 }
183
184 return 0;
185}
186
187#define IP_STR "IP information\n"
188#define IPV6_STR "IPv6 information\n"
819dc8bb 189#define AFI_CMD "<ip|ipv6>"
2fb975da
TT
190#define AFI_STR IP_STR IPV6_STR
191#define NHRP_STR "Next Hop Resolution Protocol functions\n"
192
819dc8bb 193static afi_t cmd_to_afi(const struct cmd_token *tok)
2fb975da 194{
819dc8bb 195 return strcmp(tok->text, "ipv6") == 0 ? AFI_IP6 : AFI_IP;
2fb975da
TT
196}
197
198static const char *afi_to_cmd(afi_t afi)
199{
996c9314
LB
200 if (afi == AFI_IP6)
201 return "ipv6";
2fb975da
TT
202 return "ip";
203}
204
205DEFUN(nhrp_event_socket, nhrp_event_socket_cmd,
206 "nhrp event socket SOCKET",
207 NHRP_STR
208 "Event Manager commands\n"
209 "Event Manager unix socket path\n"
201f6271 210 "Unix path for the socket\n")
2fb975da 211{
819dc8bb 212 evmgr_set_socket(argv[3]->arg);
2fb975da
TT
213 return CMD_SUCCESS;
214}
215
216DEFUN(no_nhrp_event_socket, no_nhrp_event_socket_cmd,
217 "no nhrp event socket [SOCKET]",
218 NO_STR
219 NHRP_STR
220 "Event Manager commands\n"
221 "Event Manager unix socket path\n"
201f6271 222 "Unix path for the socket\n")
2fb975da
TT
223{
224 evmgr_set_socket(NULL);
225 return CMD_SUCCESS;
226}
227
228DEFUN(nhrp_nflog_group, nhrp_nflog_group_cmd,
819dc8bb 229 "nhrp nflog-group (1-65535)",
2fb975da
TT
230 NHRP_STR
231 "Specify NFLOG group number\n"
232 "NFLOG group number\n")
233{
234 uint32_t nfgroup;
235
facfee22 236 nfgroup = strtoul(argv[2]->arg, NULL, 10);
2fb975da
TT
237 netlink_set_nflog_group(nfgroup);
238
239 return CMD_SUCCESS;
240}
241
242DEFUN(no_nhrp_nflog_group, no_nhrp_nflog_group_cmd,
819dc8bb 243 "no nhrp nflog-group [(1-65535)]",
2fb975da
TT
244 NO_STR
245 NHRP_STR
246 "Specify NFLOG group number\n"
247 "NFLOG group number\n")
248{
249 netlink_set_nflog_group(0);
250 return CMD_SUCCESS;
251}
252
253DEFUN(tunnel_protection, tunnel_protection_cmd,
819dc8bb 254 "tunnel protection vici profile PROFILE [fallback-profile FALLBACK]",
2fb975da
TT
255 "NHRP/GRE integration\n"
256 "IPsec protection\n"
257 "VICI (StrongSwan)\n"
258 "IPsec profile\n"
259 "IPsec profile name\n"
260 "Fallback IPsec profile\n"
261 "Fallback IPsec profile name\n")
262{
819dc8bb 263 VTY_DECLVAR_CONTEXT(interface, ifp);
2fb975da 264
819dc8bb 265 nhrp_interface_set_protection(ifp, argv[4]->arg,
996c9314 266 argc > 6 ? argv[6]->arg : NULL);
2fb975da
TT
267 return CMD_SUCCESS;
268}
269
270DEFUN(no_tunnel_protection, no_tunnel_protection_cmd,
271 "no tunnel protection",
272 NO_STR
273 "NHRP/GRE integration\n"
274 "IPsec protection\n")
275{
819dc8bb 276 VTY_DECLVAR_CONTEXT(interface, ifp);
2fb975da
TT
277
278 nhrp_interface_set_protection(ifp, NULL, NULL);
279 return CMD_SUCCESS;
280}
281
282DEFUN(tunnel_source, tunnel_source_cmd,
283 "tunnel source INTERFACE",
284 "NHRP/GRE integration\n"
285 "Tunnel device binding tracking\n"
286 "Interface name\n")
287{
819dc8bb
DL
288 VTY_DECLVAR_CONTEXT(interface, ifp);
289 nhrp_interface_set_source(ifp, argv[2]->arg);
2fb975da
TT
290 return CMD_SUCCESS;
291}
292
293DEFUN(no_tunnel_source, no_tunnel_source_cmd,
294 "no tunnel source",
295 "NHRP/GRE integration\n"
296 "Tunnel device binding tracking\n"
297 "Interface name\n")
298{
819dc8bb 299 VTY_DECLVAR_CONTEXT(interface, ifp);
2fb975da
TT
300 nhrp_interface_set_source(ifp, NULL);
301 return CMD_SUCCESS;
302}
303
304DEFUN(if_nhrp_network_id, if_nhrp_network_id_cmd,
819dc8bb 305 AFI_CMD " nhrp network-id (1-4294967295)",
2fb975da
TT
306 AFI_STR
307 NHRP_STR
308 "Enable NHRP and specify network-id\n"
309 "System local ID to specify interface group\n")
310{
819dc8bb 311 VTY_DECLVAR_CONTEXT(interface, ifp);
2fb975da
TT
312 struct nhrp_interface *nifp = ifp->info;
313 afi_t afi = cmd_to_afi(argv[0]);
314
facfee22 315 nifp->afi[afi].network_id = strtoul(argv[3]->arg, NULL, 10);
2fb975da
TT
316 nhrp_interface_update(ifp);
317
318 return CMD_SUCCESS;
319}
320
321DEFUN(if_no_nhrp_network_id, if_no_nhrp_network_id_cmd,
819dc8bb 322 "no " AFI_CMD " nhrp network-id [(1-4294967295)]",
2fb975da
TT
323 NO_STR
324 AFI_STR
325 NHRP_STR
326 "Enable NHRP and specify network-id\n"
327 "System local ID to specify interface group\n")
328{
819dc8bb 329 VTY_DECLVAR_CONTEXT(interface, ifp);
2fb975da 330 struct nhrp_interface *nifp = ifp->info;
819dc8bb 331 afi_t afi = cmd_to_afi(argv[1]);
2fb975da
TT
332
333 nifp->afi[afi].network_id = 0;
334 nhrp_interface_update(ifp);
335
336 return CMD_SUCCESS;
337}
338
339DEFUN(if_nhrp_flags, if_nhrp_flags_cmd,
819dc8bb 340 AFI_CMD " nhrp <shortcut|redirect>",
2fb975da
TT
341 AFI_STR
342 NHRP_STR
343 "Allow shortcut establishment\n"
344 "Send redirect notifications\n")
345{
819dc8bb 346 VTY_DECLVAR_CONTEXT(interface, ifp);
2fb975da
TT
347 struct nhrp_interface *nifp = ifp->info;
348 afi_t afi = cmd_to_afi(argv[0]);
349
996c9314
LB
350 return toggle_flag(vty, interface_flags_desc, argv[2]->text, 1,
351 &nifp->afi[afi].flags);
2fb975da
TT
352}
353
354DEFUN(if_no_nhrp_flags, if_no_nhrp_flags_cmd,
819dc8bb 355 "no " AFI_CMD " nhrp <shortcut|redirect>",
2fb975da
TT
356 NO_STR
357 AFI_STR
358 NHRP_STR
359 "Allow shortcut establishment\n"
360 "Send redirect notifications\n")
361{
819dc8bb 362 VTY_DECLVAR_CONTEXT(interface, ifp);
2fb975da 363 struct nhrp_interface *nifp = ifp->info;
819dc8bb 364 afi_t afi = cmd_to_afi(argv[1]);
2fb975da 365
996c9314
LB
366 return toggle_flag(vty, interface_flags_desc, argv[3]->text, 0,
367 &nifp->afi[afi].flags);
2fb975da
TT
368}
369
370DEFUN(if_nhrp_reg_flags, if_nhrp_reg_flags_cmd,
819dc8bb 371 AFI_CMD " nhrp registration no-unique",
2fb975da
TT
372 AFI_STR
373 NHRP_STR
374 "Registration configuration\n"
375 "Don't set unique flag\n")
376{
819dc8bb 377 VTY_DECLVAR_CONTEXT(interface, ifp);
2fb975da
TT
378 struct nhrp_interface *nifp = ifp->info;
379 afi_t afi = cmd_to_afi(argv[0]);
380 char name[256];
819dc8bb 381 snprintf(name, sizeof(name), "registration %s", argv[3]->text);
996c9314
LB
382 return toggle_flag(vty, interface_flags_desc, name, 1,
383 &nifp->afi[afi].flags);
2fb975da
TT
384}
385
386DEFUN(if_no_nhrp_reg_flags, if_no_nhrp_reg_flags_cmd,
819dc8bb 387 "no " AFI_CMD " nhrp registration no-unique",
2fb975da
TT
388 NO_STR
389 AFI_STR
390 NHRP_STR
391 "Registration configuration\n"
392 "Don't set unique flag\n")
393{
819dc8bb 394 VTY_DECLVAR_CONTEXT(interface, ifp);
2fb975da 395 struct nhrp_interface *nifp = ifp->info;
819dc8bb 396 afi_t afi = cmd_to_afi(argv[1]);
2fb975da 397 char name[256];
819dc8bb 398 snprintf(name, sizeof(name), "registration %s", argv[4]->text);
996c9314
LB
399 return toggle_flag(vty, interface_flags_desc, name, 0,
400 &nifp->afi[afi].flags);
2fb975da
TT
401}
402
403DEFUN(if_nhrp_holdtime, if_nhrp_holdtime_cmd,
819dc8bb 404 AFI_CMD " nhrp holdtime (1-65000)",
2fb975da
TT
405 AFI_STR
406 NHRP_STR
407 "Specify NBMA address validity time\n"
408 "Time in seconds that NBMA addresses are advertised valid\n")
409{
819dc8bb 410 VTY_DECLVAR_CONTEXT(interface, ifp);
2fb975da
TT
411 struct nhrp_interface *nifp = ifp->info;
412 afi_t afi = cmd_to_afi(argv[0]);
413
facfee22 414 nifp->afi[afi].holdtime = strtoul(argv[3]->arg, NULL, 10);
2fb975da
TT
415 nhrp_interface_update(ifp);
416
417 return CMD_SUCCESS;
418}
419
420DEFUN(if_no_nhrp_holdtime, if_no_nhrp_holdtime_cmd,
819dc8bb 421 "no " AFI_CMD " nhrp holdtime [(1-65000)]",
2fb975da
TT
422 NO_STR
423 AFI_STR
424 NHRP_STR
425 "Specify NBMA address validity time\n"
426 "Time in seconds that NBMA addresses are advertised valid\n")
427{
819dc8bb 428 VTY_DECLVAR_CONTEXT(interface, ifp);
2fb975da 429 struct nhrp_interface *nifp = ifp->info;
819dc8bb 430 afi_t afi = cmd_to_afi(argv[1]);
2fb975da
TT
431
432 nifp->afi[afi].holdtime = NHRPD_DEFAULT_HOLDTIME;
433 nhrp_interface_update(ifp);
434
435 return CMD_SUCCESS;
436}
437
438DEFUN(if_nhrp_mtu, if_nhrp_mtu_cmd,
819dc8bb 439 "ip nhrp mtu <(576-1500)|opennhrp>",
2fb975da
TT
440 IP_STR
441 NHRP_STR
442 "Configure NHRP advertised MTU\n"
443 "MTU value\n"
efd7904e 444 "Advertise bound interface MTU similar to OpenNHRP\n")
2fb975da 445{
819dc8bb 446 VTY_DECLVAR_CONTEXT(interface, ifp);
2fb975da
TT
447 struct nhrp_interface *nifp = ifp->info;
448
819dc8bb 449 if (argv[3]->arg[0] == 'o') {
2fb975da
TT
450 nifp->afi[AFI_IP].configured_mtu = -1;
451 } else {
996c9314
LB
452 nifp->afi[AFI_IP].configured_mtu =
453 strtoul(argv[3]->arg, NULL, 10);
2fb975da
TT
454 }
455 nhrp_interface_update_mtu(ifp, AFI_IP);
456
457 return CMD_SUCCESS;
458}
459
460DEFUN(if_no_nhrp_mtu, if_no_nhrp_mtu_cmd,
819dc8bb 461 "no ip nhrp mtu [(576-1500)|opennhrp]",
2fb975da
TT
462 NO_STR
463 IP_STR
464 NHRP_STR
465 "Configure NHRP advertised MTU\n"
466 "MTU value\n"
efd7904e 467 "Advertise bound interface MTU similar to OpenNHRP\n")
2fb975da 468{
819dc8bb 469 VTY_DECLVAR_CONTEXT(interface, ifp);
2fb975da
TT
470 struct nhrp_interface *nifp = ifp->info;
471
472 nifp->afi[AFI_IP].configured_mtu = 0;
473 nhrp_interface_update_mtu(ifp, AFI_IP);
474 return CMD_SUCCESS;
475}
476
477DEFUN(if_nhrp_map, if_nhrp_map_cmd,
819dc8bb 478 AFI_CMD " nhrp map <A.B.C.D|X:X::X:X> <A.B.C.D|local>",
2fb975da
TT
479 AFI_STR
480 NHRP_STR
481 "Nexthop Server configuration\n"
482 "IPv4 protocol address\n"
483 "IPv6 protocol address\n"
484 "IPv4 NBMA address\n"
485 "Handle protocol address locally\n")
486{
819dc8bb 487 VTY_DECLVAR_CONTEXT(interface, ifp);
2fb975da
TT
488 afi_t afi = cmd_to_afi(argv[0]);
489 union sockunion proto_addr, nbma_addr;
490 struct nhrp_cache *c;
491
996c9314
LB
492 if (str2sockunion(argv[3]->arg, &proto_addr) < 0
493 || afi2family(afi) != sockunion_family(&proto_addr))
2fb975da
TT
494 return nhrp_vty_return(vty, NHRP_ERR_PROTOCOL_ADDRESS_MISMATCH);
495
496 c = nhrp_cache_get(ifp, &proto_addr, 1);
497 if (!c)
498 return nhrp_vty_return(vty, NHRP_ERR_FAIL);
499
500 c->map = 1;
7e045c3d 501 if (strmatch(argv[4]->text, "local")) {
996c9314
LB
502 nhrp_cache_update_binding(c, NHRP_CACHE_LOCAL, 0, NULL, 0,
503 NULL);
504 } else {
819dc8bb 505 if (str2sockunion(argv[4]->arg, &nbma_addr) < 0)
2fb975da
TT
506 return nhrp_vty_return(vty, NHRP_ERR_FAIL);
507 nhrp_cache_update_binding(c, NHRP_CACHE_STATIC, 0,
996c9314
LB
508 nhrp_peer_get(ifp, &nbma_addr), 0,
509 NULL);
2fb975da
TT
510 }
511
512 return CMD_SUCCESS;
513}
514
2d4eab22 515DEFUN(if_no_nhrp_map, if_no_nhrp_map_cmd,
c1c17a0f 516 "no " AFI_CMD " nhrp map <A.B.C.D|X:X::X:X>",
2d4eab22
TT
517 NO_STR
518 AFI_STR
519 NHRP_STR
520 "Nexthop Server configuration\n"
521 "IPv4 protocol address\n"
522 "IPv6 protocol address\n")
523{
996c9314 524 VTY_DECLVAR_CONTEXT(interface, ifp);
2d4eab22 525 afi_t afi = cmd_to_afi(argv[1]);
b5503506 526 union sockunion proto_addr, nbma_addr;
2d4eab22
TT
527 struct nhrp_cache *c;
528
996c9314
LB
529 if (str2sockunion(argv[4]->arg, &proto_addr) < 0
530 || afi2family(afi) != sockunion_family(&proto_addr))
2d4eab22
TT
531 return nhrp_vty_return(vty, NHRP_ERR_PROTOCOL_ADDRESS_MISMATCH);
532
533 c = nhrp_cache_get(ifp, &proto_addr, 0);
534 if (!c || !c->map)
535 return nhrp_vty_return(vty, NHRP_ERR_ENTRY_NOT_FOUND);
536
b5503506
DA
537 nhrp_cache_update_binding(c, c->cur.type, -1,
538 nhrp_peer_get(ifp, &nbma_addr), 0, NULL);
2d4eab22
TT
539 return CMD_SUCCESS;
540}
541
2fb975da 542DEFUN(if_nhrp_nhs, if_nhrp_nhs_cmd,
819dc8bb 543 AFI_CMD " nhrp nhs <A.B.C.D|X:X::X:X|dynamic> nbma <A.B.C.D|FQDN>",
2fb975da
TT
544 AFI_STR
545 NHRP_STR
546 "Nexthop Server configuration\n"
547 "IPv4 protocol address\n"
548 "IPv6 protocol address\n"
549 "Automatic detection of protocol address\n"
819dc8bb 550 "NBMA address\n"
2fb975da
TT
551 "IPv4 NBMA address\n"
552 "Fully qualified domain name for NBMA address(es)\n")
553{
819dc8bb 554 VTY_DECLVAR_CONTEXT(interface, ifp);
2fb975da
TT
555 afi_t afi = cmd_to_afi(argv[0]);
556 union sockunion proto_addr;
557 int ret;
558
819dc8bb 559 if (str2sockunion(argv[3]->arg, &proto_addr) < 0)
2fb975da
TT
560 sockunion_family(&proto_addr) = AF_UNSPEC;
561
819dc8bb 562 ret = nhrp_nhs_add(ifp, afi, &proto_addr, argv[5]->arg);
2fb975da
TT
563 return nhrp_vty_return(vty, ret);
564}
565
566DEFUN(if_no_nhrp_nhs, if_no_nhrp_nhs_cmd,
819dc8bb 567 "no " AFI_CMD " nhrp nhs <A.B.C.D|X:X::X:X|dynamic> nbma <A.B.C.D|FQDN>",
2fb975da
TT
568 NO_STR
569 AFI_STR
570 NHRP_STR
571 "Nexthop Server configuration\n"
572 "IPv4 protocol address\n"
573 "IPv6 protocol address\n"
574 "Automatic detection of protocol address\n"
819dc8bb 575 "NBMA address\n"
2fb975da
TT
576 "IPv4 NBMA address\n"
577 "Fully qualified domain name for NBMA address(es)\n")
578{
819dc8bb
DL
579 VTY_DECLVAR_CONTEXT(interface, ifp);
580 afi_t afi = cmd_to_afi(argv[1]);
2fb975da
TT
581 union sockunion proto_addr;
582 int ret;
583
819dc8bb 584 if (str2sockunion(argv[4]->arg, &proto_addr) < 0)
2fb975da
TT
585 sockunion_family(&proto_addr) = AF_UNSPEC;
586
819dc8bb 587 ret = nhrp_nhs_del(ifp, afi, &proto_addr, argv[6]->arg);
2fb975da
TT
588 return nhrp_vty_return(vty, ret);
589}
590
591struct info_ctx {
592 struct vty *vty;
593 afi_t afi;
594 int count;
595};
596
597static void show_ip_nhrp_cache(struct nhrp_cache *c, void *pctx)
598{
599 struct info_ctx *ctx = pctx;
600 struct vty *vty = ctx->vty;
601 char buf[2][SU_ADDRSTRLEN];
602
603 if (ctx->afi != family2afi(sockunion_family(&c->remote_addr)))
604 return;
605
606 if (!ctx->count) {
996c9314
LB
607 vty_out(vty, "%-8s %-8s %-24s %-24s %-6s %s\n", "Iface", "Type",
608 "Protocol", "NBMA", "Flags", "Identity");
2fb975da
TT
609 }
610 ctx->count++;
611
996c9314 612 vty_out(ctx->vty, "%-8s %-8s %-24s %-24s %c%c%c %s\n", c->ifp->name,
2fb975da
TT
613 nhrp_cache_type_str[c->cur.type],
614 sockunion2str(&c->remote_addr, buf[0], sizeof buf[0]),
996c9314
LB
615 c->cur.peer ? sockunion2str(&c->cur.peer->vc->remote.nbma,
616 buf[1], sizeof buf[1])
617 : "-",
618 c->used ? 'U' : ' ', c->t_timeout ? 'T' : ' ',
2fb975da 619 c->t_auth ? 'A' : ' ',
96ade3ed 620 c->cur.peer ? c->cur.peer->vc->remote.id : "-");
2fb975da
TT
621}
622
996c9314
LB
623static void show_ip_nhrp_nhs(struct nhrp_nhs *n, struct nhrp_registration *reg,
624 void *pctx)
0ca036b4
TT
625{
626 struct info_ctx *ctx = pctx;
627 struct vty *vty = ctx->vty;
628 char buf[2][SU_ADDRSTRLEN];
629
630 if (!ctx->count) {
996c9314
LB
631 vty_out(vty, "%-8s %-24s %-16s %-16s\n", "Iface", "FQDN",
632 "NBMA", "Protocol");
0ca036b4
TT
633 }
634 ctx->count++;
635
996c9314
LB
636 vty_out(vty, "%-8s %-24s %-16s %-16s\n", n->ifp->name, n->nbma_fqdn,
637 (reg && reg->peer) ? sockunion2str(&reg->peer->vc->remote.nbma,
638 buf[0], sizeof buf[0])
639 : "-",
640 sockunion2str(reg ? &reg->proto_addr : &n->proto_addr, buf[1],
641 sizeof buf[1]));
0ca036b4
TT
642}
643
644static void show_ip_nhrp_shortcut(struct nhrp_shortcut *s, void *pctx)
645{
646 struct info_ctx *ctx = pctx;
647 struct nhrp_cache *c;
648 struct vty *vty = ctx->vty;
649 char buf1[PREFIX_STRLEN], buf2[SU_ADDRSTRLEN];
650
651 if (!ctx->count) {
996c9314 652 vty_out(vty, "%-8s %-24s %-24s %s\n", "Type", "Prefix", "Via",
96ade3ed 653 "Identity");
0ca036b4
TT
654 }
655 ctx->count++;
656
657 c = s->cache;
996c9314 658 vty_out(ctx->vty, "%-8s %-24s %-24s %s\n", nhrp_cache_type_str[s->type],
0ca036b4
TT
659 prefix2str(s->p, buf1, sizeof buf1),
660 c ? sockunion2str(&c->remote_addr, buf2, sizeof buf2) : "",
96ade3ed 661 (c && c->cur.peer) ? c->cur.peer->vc->remote.id : "");
0ca036b4
TT
662}
663
2fb975da
TT
664static void show_ip_opennhrp_cache(struct nhrp_cache *c, void *pctx)
665{
666 struct info_ctx *ctx = pctx;
2fb975da
TT
667 char buf[SU_ADDRSTRLEN];
668
669 if (ctx->afi != family2afi(sockunion_family(&c->remote_addr)))
670 return;
671
181039f3 672 vty_out(ctx->vty,
996c9314
LB
673 "Type: %s\n"
674 "Flags:%s%s\n"
675 "Protocol-Address: %s/%zu\n",
676 nhrp_cache_type_str[c->cur.type],
677 (c->cur.peer && c->cur.peer->online) ? " up" : "",
678 c->used ? " used" : "",
679 sockunion2str(&c->remote_addr, buf, sizeof buf),
680 8 * family2addrsize(sockunion_family(&c->remote_addr)));
2fb975da
TT
681
682 if (c->cur.peer) {
996c9314
LB
683 vty_out(ctx->vty, "NBMA-Address: %s\n",
684 sockunion2str(&c->cur.peer->vc->remote.nbma, buf,
685 sizeof buf));
2fb975da
TT
686 }
687
688 if (sockunion_family(&c->cur.remote_nbma_natoa) != AF_UNSPEC) {
996c9314
LB
689 vty_out(ctx->vty, "NBMA-NAT-OA-Address: %s\n",
690 sockunion2str(&c->cur.remote_nbma_natoa, buf,
691 sizeof buf));
2fb975da
TT
692 }
693
181039f3 694 vty_out(ctx->vty, "\n\n");
2fb975da
TT
695}
696
2fb975da 697DEFUN(show_ip_nhrp, show_ip_nhrp_cmd,
0ca036b4 698 "show " AFI_CMD " nhrp [cache|nhs|shortcut|opennhrp]",
2fb975da
TT
699 SHOW_STR
700 AFI_STR
701 "NHRP information\n"
702 "Forwarding cache information\n"
0ca036b4 703 "Next hop server information\n"
2fb975da
TT
704 "Shortcut information\n"
705 "opennhrpctl style cache dump\n")
706{
f4e14fdb 707 struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
2fb975da
TT
708 struct interface *ifp;
709 struct info_ctx ctx = {
996c9314 710 .vty = vty, .afi = cmd_to_afi(argv[1]),
2fb975da
TT
711 };
712
819dc8bb 713 if (argc <= 3 || argv[3]->text[0] == 'c') {
451fda4f 714 FOR_ALL_INTERFACES (vrf, ifp)
2fb975da 715 nhrp_cache_foreach(ifp, show_ip_nhrp_cache, &ctx);
0ca036b4 716 } else if (argv[3]->text[0] == 'n') {
451fda4f 717 FOR_ALL_INTERFACES (vrf, ifp)
0ca036b4
TT
718 nhrp_nhs_foreach(ifp, ctx.afi, show_ip_nhrp_nhs, &ctx);
719 } else if (argv[3]->text[0] == 's') {
720 nhrp_shortcut_foreach(ctx.afi, show_ip_nhrp_shortcut, &ctx);
721 } else {
996c9314 722 vty_out(vty, "Status: ok\n\n");
2fb975da 723 ctx.count++;
451fda4f 724 FOR_ALL_INTERFACES (vrf, ifp)
2fb975da 725 nhrp_cache_foreach(ifp, show_ip_opennhrp_cache, &ctx);
2fb975da
TT
726 }
727
728 if (!ctx.count) {
996c9314 729 vty_out(vty, "%% No entries\n");
2fb975da
TT
730 return CMD_WARNING;
731 }
732
733 return CMD_SUCCESS;
734}
735
736static void show_dmvpn_entry(struct nhrp_vc *vc, void *ctx)
737{
738 struct vty *vty = ctx;
739 char buf[2][SU_ADDRSTRLEN];
740
996c9314 741 vty_out(vty, "%-24s %-24s %c %-4d %-24s\n",
2fb975da
TT
742 sockunion2str(&vc->local.nbma, buf[0], sizeof buf[0]),
743 sockunion2str(&vc->remote.nbma, buf[1], sizeof buf[1]),
996c9314 744 notifier_active(&vc->notifier_list) ? 'n' : ' ', vc->ipsec,
96ade3ed 745 vc->remote.id);
2fb975da
TT
746}
747
748DEFUN(show_dmvpn, show_dmvpn_cmd,
749 "show dmvpn",
750 SHOW_STR
751 "DMVPN information\n")
752{
996c9314
LB
753 vty_out(vty, "%-24s %-24s %-6s %-4s %-24s\n", "Src", "Dst", "Flags",
754 "SAs", "Identity");
2fb975da
TT
755
756 nhrp_vc_foreach(show_dmvpn_entry, vty);
757
758 return CMD_SUCCESS;
759}
760
761static void clear_nhrp_cache(struct nhrp_cache *c, void *data)
762{
763 struct info_ctx *ctx = data;
764 if (c->cur.type <= NHRP_CACHE_CACHED) {
765 nhrp_cache_update_binding(c, c->cur.type, -1, NULL, 0, NULL);
766 ctx->count++;
767 }
768}
769
770static void clear_nhrp_shortcut(struct nhrp_shortcut *s, void *data)
771{
772 struct info_ctx *ctx = data;
773 nhrp_shortcut_purge(s, 1);
774 ctx->count++;
775}
776
777DEFUN(clear_nhrp, clear_nhrp_cmd,
819dc8bb 778 "clear " AFI_CMD " nhrp <cache|shortcut>",
2fb975da
TT
779 CLEAR_STR
780 AFI_STR
781 NHRP_STR
782 "Dynamic cache entries\n"
783 "Shortcut entries\n")
784{
f4e14fdb 785 struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
2fb975da
TT
786 struct interface *ifp;
787 struct info_ctx ctx = {
996c9314 788 .vty = vty, .afi = cmd_to_afi(argv[1]), .count = 0,
2fb975da
TT
789 };
790
819dc8bb 791 if (argc <= 3 || argv[3]->text[0] == 'c') {
451fda4f 792 FOR_ALL_INTERFACES (vrf, ifp)
2fb975da
TT
793 nhrp_cache_foreach(ifp, clear_nhrp_cache, &ctx);
794 } else {
795 nhrp_shortcut_foreach(ctx.afi, clear_nhrp_shortcut, &ctx);
796 }
797
798 if (!ctx.count) {
996c9314 799 vty_out(vty, "%% No entries\n");
2fb975da
TT
800 return CMD_WARNING;
801 }
802
996c9314 803 vty_out(vty, "%% %d entries cleared\n", ctx.count);
2fb975da
TT
804 return CMD_SUCCESS;
805}
806
807struct write_map_ctx {
808 struct vty *vty;
809 int family;
810 const char *aficmd;
811};
812
813static void interface_config_write_nhrp_map(struct nhrp_cache *c, void *data)
814{
815 struct write_map_ctx *ctx = data;
816 struct vty *vty = ctx->vty;
817 char buf[2][SU_ADDRSTRLEN];
818
996c9314
LB
819 if (!c->map)
820 return;
821 if (sockunion_family(&c->remote_addr) != ctx->family)
822 return;
2fb975da 823
996c9314 824 vty_out(vty, " %s nhrp map %s %s\n", ctx->aficmd,
2fb975da 825 sockunion2str(&c->remote_addr, buf[0], sizeof buf[0]),
996c9314
LB
826 c->cur.type == NHRP_CACHE_LOCAL
827 ? "local"
828 : sockunion2str(&c->cur.peer->vc->remote.nbma, buf[1],
829 sizeof buf[1]));
2fb975da
TT
830}
831
832static int interface_config_write(struct vty *vty)
833{
f4e14fdb 834 struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
2fb975da 835 struct write_map_ctx mapctx;
2fb975da
TT
836 struct interface *ifp;
837 struct nhrp_interface *nifp;
838 struct nhrp_nhs *nhs;
839 const char *aficmd;
840 afi_t afi;
841 char buf[SU_ADDRSTRLEN];
842 int i;
843
451fda4f 844 FOR_ALL_INTERFACES (vrf, ifp) {
a8b828f3 845 vty_frame(vty, "interface %s\n", ifp->name);
2fb975da 846 if (ifp->desc)
996c9314 847 vty_out(vty, " description %s\n", ifp->desc);
2fb975da
TT
848
849 nifp = ifp->info;
850 if (nifp->ipsec_profile) {
851 vty_out(vty, " tunnel protection vici profile %s",
852 nifp->ipsec_profile);
853 if (nifp->ipsec_fallback_profile)
854 vty_out(vty, " fallback-profile %s",
855 nifp->ipsec_fallback_profile);
996c9314 856 vty_out(vty, "\n");
2fb975da
TT
857 }
858 if (nifp->source)
996c9314 859 vty_out(vty, " tunnel source %s\n", nifp->source);
2fb975da
TT
860
861 for (afi = 0; afi < AFI_MAX; afi++) {
862 struct nhrp_afi_data *ad = &nifp->afi[afi];
863
864 aficmd = afi_to_cmd(afi);
865
866 if (ad->network_id)
996c9314
LB
867 vty_out(vty, " %s nhrp network-id %u\n", aficmd,
868 ad->network_id);
2fb975da
TT
869
870 if (ad->holdtime != NHRPD_DEFAULT_HOLDTIME)
996c9314
LB
871 vty_out(vty, " %s nhrp holdtime %u\n", aficmd,
872 ad->holdtime);
2fb975da
TT
873
874 if (ad->configured_mtu < 0)
996c9314 875 vty_out(vty, " %s nhrp mtu opennhrp\n", aficmd);
2fb975da 876 else if (ad->configured_mtu)
996c9314
LB
877 vty_out(vty, " %s nhrp mtu %u\n", aficmd,
878 ad->configured_mtu);
2fb975da
TT
879
880 for (i = 0; interface_flags_desc[i].str != NULL; i++) {
881 if (!(ad->flags & interface_flags_desc[i].key))
882 continue;
996c9314
LB
883 vty_out(vty, " %s nhrp %s\n", aficmd,
884 interface_flags_desc[i].str);
2fb975da
TT
885 }
886
996c9314 887 mapctx = (struct write_map_ctx){
2fb975da
TT
888 .vty = vty,
889 .family = afi2family(afi),
890 .aficmd = aficmd,
891 };
996c9314
LB
892 nhrp_cache_foreach(ifp, interface_config_write_nhrp_map,
893 &mapctx);
2fb975da 894
996c9314
LB
895 list_for_each_entry(nhs, &ad->nhslist_head,
896 nhslist_entry)
897 {
898 vty_out(vty, " %s nhrp nhs %s nbma %s\n",
2fb975da 899 aficmd,
996c9314
LB
900 sockunion_family(&nhs->proto_addr)
901 == AF_UNSPEC
902 ? "dynamic"
903 : sockunion2str(
904 &nhs->proto_addr, buf,
905 sizeof buf),
96ade3ed 906 nhs->nbma_fqdn);
2fb975da
TT
907 }
908 }
909
a8b828f3 910 vty_endframe(vty, "!\n");
2fb975da
TT
911 }
912
913 return 0;
914}
915
916void nhrp_config_init(void)
917{
918 install_node(&zebra_node, nhrp_config_write);
919 install_default(ZEBRA_NODE);
920
d2057ea3 921 /* access-list commands */
996c9314 922 access_list_init();
d2057ea3 923
2fb975da
TT
924 /* global commands */
925 install_element(VIEW_NODE, &show_debugging_nhrp_cmd);
926 install_element(VIEW_NODE, &show_ip_nhrp_cmd);
927 install_element(VIEW_NODE, &show_dmvpn_cmd);
2fb975da
TT
928 install_element(ENABLE_NODE, &clear_nhrp_cmd);
929
930 install_element(ENABLE_NODE, &debug_nhrp_cmd);
931 install_element(ENABLE_NODE, &no_debug_nhrp_cmd);
932
933 install_element(CONFIG_NODE, &debug_nhrp_cmd);
934 install_element(CONFIG_NODE, &no_debug_nhrp_cmd);
935
936 install_element(CONFIG_NODE, &nhrp_event_socket_cmd);
937 install_element(CONFIG_NODE, &no_nhrp_event_socket_cmd);
938 install_element(CONFIG_NODE, &nhrp_nflog_group_cmd);
939 install_element(CONFIG_NODE, &no_nhrp_nflog_group_cmd);
940
941 /* interface specific commands */
942 install_node(&nhrp_interface_node, interface_config_write);
2fb975da 943
819dc8bb 944 if_cmd_init();
2fb975da
TT
945 install_element(INTERFACE_NODE, &tunnel_protection_cmd);
946 install_element(INTERFACE_NODE, &no_tunnel_protection_cmd);
947 install_element(INTERFACE_NODE, &tunnel_source_cmd);
948 install_element(INTERFACE_NODE, &no_tunnel_source_cmd);
949 install_element(INTERFACE_NODE, &if_nhrp_network_id_cmd);
950 install_element(INTERFACE_NODE, &if_no_nhrp_network_id_cmd);
951 install_element(INTERFACE_NODE, &if_nhrp_holdtime_cmd);
952 install_element(INTERFACE_NODE, &if_no_nhrp_holdtime_cmd);
953 install_element(INTERFACE_NODE, &if_nhrp_mtu_cmd);
954 install_element(INTERFACE_NODE, &if_no_nhrp_mtu_cmd);
955 install_element(INTERFACE_NODE, &if_nhrp_flags_cmd);
956 install_element(INTERFACE_NODE, &if_no_nhrp_flags_cmd);
957 install_element(INTERFACE_NODE, &if_nhrp_reg_flags_cmd);
958 install_element(INTERFACE_NODE, &if_no_nhrp_reg_flags_cmd);
959 install_element(INTERFACE_NODE, &if_nhrp_map_cmd);
2d4eab22 960 install_element(INTERFACE_NODE, &if_no_nhrp_map_cmd);
2fb975da
TT
961 install_element(INTERFACE_NODE, &if_nhrp_nhs_cmd);
962 install_element(INTERFACE_NODE, &if_no_nhrp_nhs_cmd);
963}