summaryrefslogtreecommitdiffstats
path: root/nhrpd/nhrp_config.c
blob: c7f931f26e177c6c7ced1a397c4548beb55902cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*  
 * NHRP configuration.
 * Copyright (c) 2014 Timo Teräs
 *
 * This file is free software: you may copy, redistribute and/or modify it  
 * under the terms of the GNU General Public License as published by the  
 * Free Software Foundation, either version 2 of the License, or (at your  
 * option) any later version.  
 *  
 * This file is distributed in the hope that it will be useful, but  
 * WITHOUT ANY WARRANTY; without even the implied warranty of  
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
 * General Public License for more details.  
 *  
 * You should have received a copy of the GNU General Public License  
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.  
 */

/* quagga's includes */
#include "zebra.h"
#include "command.h"
#include "zclient.h"
#include "stream.h"

#include "nhrpd.h"

static struct zclient *zclient;

static struct cmd_node zebra_node = {
	ZEBRA_NODE,
	"%s(config-router)# ",
	1 /* vtysh? yes */
};

static struct cmd_node debug_node = {
	DEBUG_NODE,
	"",
	1
};

static const struct message debug_type[] = {
	{ NHRP_DEBUG_ALL, "all" },
	{ NHRP_DEBUG_COMMON, "common" },
	{ NHRP_DEBUG_KERNEL, "kernel" },
	{ NHRP_DEBUG_ROUTE, "route" },
	{ 0, NULL },
};

#if 0
/* Zebra route add and delete treatment (ipv6). */
static int
babel_zebra_read_ipv6 (int command, struct zclient *zclient,
		       zebra_size_t length)
{
    struct stream *s;
    struct zapi_ipv6 api;
    unsigned long ifindex = -1;
    struct in6_addr nexthop;
    struct prefix_ipv6 prefix;

    s = zclient->ibuf;
    ifindex = 0;
    memset (&nexthop, 0, sizeof (struct in6_addr));
    memset (&api, 0, sizeof(struct zapi_ipv6));
    memset (&prefix, 0, sizeof (struct prefix_ipv6));

    /* Type, flags, message. */
    api.type = stream_getc (s);
    api.flags = stream_getc (s);
    api.message = stream_getc (s);

    /* IPv6 prefix. */
    prefix.family = AF_INET6;
    prefix.prefixlen = stream_getc (s);
    stream_get (&prefix.prefix, s, PSIZE (prefix.prefixlen));

    /* Nexthop, ifindex, distance, metric. */
    if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) {
        api.nexthop_num = stream_getc (s);
        stream_get (&nexthop, s, sizeof(nexthop));
    }
    if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX)) {
        api.ifindex_num = stream_getc (s);
        ifindex = stream_getl (s);
    }
    if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
        api.distance = stream_getc (s);
    else
        api.distance = 0;
    if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
        api.metric = stream_getl (s);
    else
        api.metric = 0;

    if (command == ZEBRA_IPV6_ROUTE_ADD)
        babel_ipv6_route_add(&api, &prefix, ifindex, &nexthop);
    else
        babel_ipv6_route_delete(&api, &prefix, ifindex);

    return 0;
}

static int
babel_zebra_read_ipv4 (int command, struct zclient *zclient,
		       zebra_size_t length)
{
    struct stream *s;
    struct zapi_ipv4 api;
    unsigned long ifindex = -1;
    struct in_addr nexthop;
    struct prefix_ipv4 prefix;

    s = zclient->ibuf;
    ifindex = 0;
    memset (&nexthop, 0, sizeof (struct in_addr));
    memset (&api, 0, sizeof(struct zapi_ipv4));
    memset (&prefix, 0, sizeof (struct prefix_ipv4));

    /* Type, flags, message. */
    api.type = stream_getc (s);
    api.flags = stream_getc (s);
    api.message = stream_getc (s);

    /* IPv6 prefix. */
    prefix.family = AF_INET;
    prefix.prefixlen = stream_getc (s);
    stream_get (&prefix.prefix, s, PSIZE (prefix.prefixlen));

    /* Nexthop, ifindex, distance, metric. */
    if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) {
        api.nexthop_num = stream_getc (s);
        stream_get (&nexthop, s, sizeof(nexthop));
    }
    if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX)) {
        api.ifindex_num = stream_getc (s);
        ifindex = stream_getl (s);
    }
    if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
        api.distance = stream_getc (s);
    else
        api.distance = 0;
    if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
        api.metric = stream_getl (s);
    else
        api.metric = 0;

    if (command == ZEBRA_IPV6_ROUTE_ADD) {
        babel_ipv4_route_add(&api, &prefix, ifindex, &nexthop);
    } else {
        babel_ipv4_route_delete(&api, &prefix, ifindex);
    }

    return 0;
}

/* [NHRP Command] */
DEFUN(nhrp_redistribute_type, nhrp_redistribute_type_cmd,
	"redistribute " QUAGGA_REDIST_STR_BABELD,
	"Redistribute\n"
	QUAGGA_REDIST_HELP_STR_BABELD)
{
	int type;

	type = proto_redistnum(AFI_IP6, argv[0]);
	if (type < 0)
		type = proto_redistnum(AFI_IP, argv[0]);
	if (type < 0) {
		vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE);
		return CMD_WARNING;
	}
	zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, type);
	return CMD_SUCCESS;
}

/* [Babel Command] */
DEFUN (no_babel_redistribute_type,
       no_babel_redistribute_type_cmd,
       "no redistribute " QUAGGA_REDIST_STR_BABELD,
       NO_STR
       "Redistribute\n"
       QUAGGA_REDIST_HELP_STR_BABELD)
{
    int type;

    type = proto_redistnum(AFI_IP6, argv[0]);

    if (type < 0)
        type = proto_redistnum(AFI_IP, argv[0]);

    if (type < 0) {
        vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE);
        return CMD_WARNING;
    }

    zclient_redistribute (ZEBRA_REDISTRIBUTE_DELETE, zclient, type);
    /* perhaps should we remove xroutes having the same type... */
    return CMD_SUCCESS;
}
#endif

#ifndef NO_DEBUG

DEFUN(show_debugging_nhrp, show_debugging_nhrp_cmd,
	"show debugging nhrp",
	SHOW_STR
	"Debugging information\n"
	"NHRP configuration\n")
{
	int i;

	vty_out(vty, "NHRP debugging status:%s", VTY_NEWLINE);

	for (i = 0; debug_type[i].str != NULL; i++) {
		if (debug_type[i].key == NHRP_DEBUG_ALL)
			continue;
		if (!(debug_type[i].key & debug_flags))
			continue;

		vty_out(vty, "  NHRP %s debugging is on%s", 
			debug_type[i].str, VTY_NEWLINE);
	}

	return CMD_SUCCESS;
}

static int toggle_debug(struct vty *vty, const char *type, int on_off)
{
	int i;

	for (i = 0; debug_type[i].str != NULL; i++) {
		if (strcmp(debug_type[i].str, type) != 0)
			continue;
		if (on_off)
			debug_flags |= debug_type[i].key;
		else
			debug_flags &= ~debug_type[i].key;
		return CMD_SUCCESS;
	}
	vty_out(vty, "Invalid type %s%s", type, VTY_NEWLINE);
	return CMD_WARNING;
}

DEFUN (debug_nhrp, debug_nhrp_cmd,
	"debug nhrp (common|kernel|route|all)",
	"Enable debug messages for specific or all parts.\n"
	"NHRP information\n"
	"Common messages (default)\n"
	"Kernel messages\n"
	"Route messages\n"
	"All messages\n")
{
	return toggle_debug(vty, argv[0], 1);
}

DEFUN (no_debug_nhrp, no_debug_nhrp_cmd,
	"no debug nhrp (common|kernel|route|all)",
	NO_STR
	"Disable debug messages for specific or all parts.\n"
	"NHRP information\n"
	"Common messages (default)\n"
	"Kernel messages\n"
	"Route messages\n"
	"All messages\n")
{
	return toggle_debug(vty, argv[0], 0);
}

#endif /* NO_DEBUG */

static int nhrp_config_write(struct vty *vty)
{
	int lines = 0;

#ifndef NO_DEBUG
	if (debug_flags == NHRP_DEBUG_ALL) {
		vty_out (vty, "debug nhrp all%s", VTY_NEWLINE);
		lines++;
	} else {
		int i;

		for (i = 0; debug_type[i].str != NULL; i++) {
			if (debug_type[i].key == NHRP_DEBUG_ALL)
				continue;
			if (!(debug_flags & debug_type[i].key))
				continue;
			vty_out (vty, "debug nhrp %s%s", debug_type[i].str, VTY_NEWLINE);
			lines++;
		}
	}
#endif /* NO_DEBUG */

	if (lines) {
		vty_out (vty, "!%s", VTY_NEWLINE);
		lines++;
	}

	if (!zclient->enable) {
		vty_out (vty, "no router zebra%s", VTY_NEWLINE);
		lines++;
	} else if (!zclient->redist[ZEBRA_ROUTE_NHRP]) {
		vty_out(vty, "router zebra%s", VTY_NEWLINE);
		vty_out(vty, " no redistribute nhrp%s", VTY_NEWLINE);
		lines += 2;
	}

	return lines;
}

void nhrp_zebra_init(void)
{
	zclient = zclient_new();
#if 0
	zclient->interface_add = babel_interface_add;
	zclient->interface_delete = babel_interface_delete;
	zclient->interface_up = babel_interface_up;
	zclient->interface_down = babel_interface_down;
	zclient->interface_address_add = babel_interface_address_add;
	zclient->interface_address_delete = babel_interface_address_delete;
	zclient->ipv4_route_add = babel_zebra_read_ipv4;
	zclient->ipv4_route_delete = babel_zebra_read_ipv4;
	zclient->ipv6_route_add = babel_zebra_read_ipv6;
	zclient->ipv6_route_delete = babel_zebra_read_ipv6;
#endif
	zclient_init(zclient, ZEBRA_ROUTE_NHRP);

	install_node(&zebra_node, nhrp_config_write);
	install_default(ZEBRA_NODE);

	install_element(VIEW_NODE, &show_debugging_nhrp_cmd);

	install_element(ENABLE_NODE, &show_debugging_nhrp_cmd);
	install_element(ENABLE_NODE, &debug_nhrp_cmd);
	install_element(ENABLE_NODE, &no_debug_nhrp_cmd);

	install_element(CONFIG_NODE, &debug_nhrp_cmd);
	install_element(CONFIG_NODE, &no_debug_nhrp_cmd);
}

void nhrp_zebra_terminate(void)
{
	zclient_stop(zclient);
}