summaryrefslogtreecommitdiffstats
path: root/src/tlv_types.c
blob: 2a20f5da7c6b1abcced8a889dfeab175fa5d1d92 (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
/*
  Copyright (c) 2013-2014, Matthias Schiffer <mschiffer@universe-factory.net>
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#include "babel.h"
#include "neigh.h"
#include "packet.h"
#include "tlv_types.h"

#include <stdlib.h>


typedef struct handle_tlv_arg {
	gmrf_iface_state_t *iface;
	const gmrf_addr_t *source;

	gp_babel_node_id_t node_id;
} handle_tlv_arg_t;

static inline gp_babel_neigh_t* get_tlv_neigh(gmrf_context_t *ctx, handle_tlv_arg_t *arg) {
	gp_babel_neigh_t *neigh = gp_babel_neigh_get(arg->iface, arg->source);
	neigh->last_packet = gmrf_now(ctx->gmrf);
	return neigh;
}

static void handle_tlv_ack_req(gmrf_context_t *ctx, const gp_babel_tlv_ack_req_t *tlv, size_t len, handle_tlv_arg_t *arg) {
	if (len < sizeof(gp_babel_tlv_ack_req_t)) {
		gmrf_logf(ctx->gmrf, LOG_WARNING, "received short acknowledement request TLV.");
		return;
	}

	gp_babel_send_ack(ctx, get_tlv_neigh(ctx, arg), ntohs(tlv->nonce));
}

static void handle_tlv_ack(gmrf_context_t *ctx, const gp_babel_tlv_ack_t *tlv UNUSED, size_t len, handle_tlv_arg_t *arg UNUSED) {
	if (len < sizeof(gp_babel_tlv_ack_t)) {
		gmrf_logf(ctx->gmrf, LOG_WARNING, "received short acknowledement TLV.");
		return;
	}

	//gp_babel_ack_handle(ntohs(tlv->nonce));
}

static void handle_tlv_hello(gmrf_context_t *ctx, const gp_babel_tlv_hello_t *tlv, size_t len, handle_tlv_arg_t *arg) {
	if (len < sizeof(gp_babel_tlv_hello_t)) {
		gmrf_logf(ctx->gmrf, LOG_WARNING, "received short hello TLV.");
		return;
	}

	gmrf_logf(ctx->gmrf, LOG_DEBUG, "received hello from %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x[%s] with seqno %04x.",
		  arg->source->d[0], arg->source->d[1], arg->source->d[2], arg->source->d[3],
		  arg->source->d[4], arg->source->d[5], arg->source->d[6], arg->source->d[7],
		  gmrf_iface_get_name(ctx->gmrf, arg->iface->gmrf_iface), ntohs(tlv->seqno));

	gp_babel_neigh_t *neigh = get_tlv_neigh(ctx, arg);

	uint16_t seqno = ntohs(tlv->seqno);

	if (neigh->last_hello != gmrf_time_unspec) {
		int timediff = gp_babel_since(ctx, neigh->last_hello);
		uint16_t seqexp = neigh->last_seqno + (timediff - neigh->hello_interval/2)/neigh->hello_interval;

		/* cast to int16_t to ensure correct handling of seqno wrapping */
		if (abs((int16_t)(seqno - seqexp)) > 16) {
			gmrf_logf(ctx->gmrf, LOG_INFO, "neighbour was reset.");
			neigh->hello_log = 0;
			neigh->txcost = GP_BABEL_INFINITY;
		}
		else {
			int16_t seqdiff = seqno - neigh->last_seqno;
			if (seqdiff <= 0) {
				gmrf_logf(ctx->gmrf, LOG_DEBUG, "seqno already seen, ignoring.");
				return;
			}

			neigh->hello_log <<= seqdiff;
		}
	}
	else {
		gmrf_logf(ctx->gmrf, LOG_DEBUG, "received hello from new neighbour.");
	}

	neigh->hello_log |= 1;
	neigh->hello_interval = ntohs(tlv->interval);
	neigh->last_seqno = seqno;
	neigh->last_hello = gmrf_now(ctx->gmrf);

	if (neigh->hello_log == 1) /* new or reset neighbour */
		gp_babel_neigh_reset(ctx, arg->iface, neigh);

	gmrf_logf(ctx->gmrf, LOG_DEBUG, "accepted hello, log %04x, rxcost is %u, cost is %u now.", neigh->hello_log, gp_babel_neigh_get_rxcost(ctx, neigh), gp_babel_neigh_get_cost(ctx, neigh));
	gmrf_debug_neigh(ctx->gmrf, arg->iface->gmrf_iface, &neigh->addr, gp_babel_neigh_get_rxcost(ctx, neigh), gp_babel_neigh_get_txcost(ctx, neigh));
}

static void handle_tlv_ihu(gmrf_context_t *ctx, const gp_babel_tlv_ihu_t *tlv, size_t len, handle_tlv_arg_t *arg) {
	if (len < sizeof(gp_babel_tlv_ihu_t)) {
		gmrf_logf(ctx->gmrf, LOG_WARNING, "received short IHU TLV.");
		return;
	}

	if (tlv->ae == ADDR_ENC_GMRF) {
		if (len < sizeof(gp_babel_tlv_ihu_t)+sizeof(gmrf_addr_t)) {
			gmrf_logf(ctx->gmrf, LOG_WARNING, "received short IHU TLV.");
			return;
		}

		gmrf_addr_t iface_addr = gmrf_iface_get_addr(ctx->gmrf, arg->iface->gmrf_iface);
		if (memcmp(tlv->address, &iface_addr, sizeof(gmrf_addr_t)) != 0)
			return;
	}
	else if (tlv->ae != ADDR_ENC_UNSPEC) {
		return;
	}

	gp_babel_neigh_t *neigh = get_tlv_neigh(ctx, arg);
	neigh->ihu_interval = ntohs(tlv->interval);
	neigh->last_ihu = gmrf_now(ctx->gmrf);
	neigh->txcost = ntohs(tlv->rxcost);

	gmrf_logf(ctx->gmrf, LOG_DEBUG, "accepted IHU, txcost is %u, cost is %u now.", gp_babel_neigh_get_txcost(ctx, neigh), gp_babel_neigh_get_cost(ctx, neigh));
	gmrf_debug_neigh(ctx->gmrf, arg->iface->gmrf_iface, &neigh->addr, gp_babel_neigh_get_rxcost(ctx, neigh), gp_babel_neigh_get_txcost(ctx, neigh));
}

static void handle_tlv_node_id(gmrf_context_t *ctx, const gp_babel_tlv_node_id_t *tlv, size_t len, handle_tlv_arg_t *arg) {
	if (len < sizeof(gp_babel_tlv_node_id_t)) {
		gmrf_logf(ctx->gmrf, LOG_WARNING, "received short node id TLV.");
		return;
	}

	arg->node_id = tlv->id;
}

static void handle_tlv_update(gmrf_context_t *ctx, const gp_babel_tlv_update_t *tlv, size_t len, handle_tlv_arg_t *arg) {
	if (len < sizeof(gp_babel_tlv_update_t)) {
		gmrf_logf(ctx->gmrf, LOG_WARNING, "received short update TLV.");
		return;
	}

	if (gp_babel_node_id_equal(&tlv->node, &ctx->self)) {
		gmrf_logf(ctx->gmrf, LOG_DEBUG, "update source is myself.");
		return;
	}

	gp_babel_metric_seqno_t ms = { ntohs(tlv->metric), ntohs(tlv->seqno) };

	gp_babel_route_t *route;

	if (ms.metric == GP_BABEL_INFINITY) {
		route = gp_babel_route_find(ctx, &tlv->node);
		if (!route) {
			gmrf_logf(ctx->gmrf, LOG_DEBUG, "retract received for unknown node %04x%04x.",
				  ntohl(*(uint32_t*)tlv->node.id), ntohl(*(uint32_t*)(tlv->node.id+4)));
			return;
		}
	}
	else {
		route = gp_babel_route_get(ctx, &tlv->node);
	}

	bool feasible = gp_babel_is_feasible(route, ms);

	gp_babel_neigh_t *neigh = get_tlv_neigh(ctx, arg);
	gp_babel_nexthop_t *nexthop = gp_babel_route_nexthop_find(route, neigh);

	gmrf_logf(ctx->gmrf, LOG_DEBUG, "update received from %04x%04x, metric %u, seqno %04x.",
		  ntohl(*(uint32_t*)tlv->node.id), ntohl(*(uint32_t*)(tlv->node.id+4)),
		  ms.metric, ms.seqno);
	gmrf_logf(ctx->gmrf, LOG_DEBUG, "the update is %sfeasible and there is %s nexthop.", feasible ? "" : "not ", nexthop ? "a" : "no");

	if (!nexthop) {
		if (feasible && tlv->metric != GP_BABEL_INFINITY /* no need to ntohs */)
			nexthop = gp_babel_route_nexthop_new(route, neigh);
	}
	else {
		if (!feasible && nexthop == route->selected) {
			gmrf_logf(ctx->gmrf, LOG_DEBUG, "requesting new seqno");
			gp_babel_send_seqno_request_for(ctx, neigh, route);
			nexthop = NULL;
		}
	}

	if (!feasible && nexthop && nexthop != route->selected) {
		if (ms.metric + gp_babel_neigh_get_cost(ctx, neigh) < route->metric.metric) {
			gmrf_logf(ctx->gmrf, LOG_DEBUG, "requesting new seqno");
			gp_babel_send_seqno_request_for(ctx, neigh, route);
		}
	}

	if (!nexthop)
		return;

	gmrf_logf(ctx->gmrf, LOG_DEBUG, "the update was accepted.");

	gp_babel_route_update_nexthop(ctx, route, nexthop, ms, ntohs(tlv->interval));
}

static void handle_tlv_route_req(gmrf_context_t *ctx, const gp_babel_tlv_route_req_t *tlv, size_t len, handle_tlv_arg_t *arg) {
	if (len < sizeof(gp_babel_tlv_route_req_t)) {
		gmrf_logf(ctx->gmrf, LOG_WARNING, "received short route request TLV.");
		return;
	}

	gp_babel_neigh_t *neigh = get_tlv_neigh(ctx, arg);

	if (gp_babel_node_id_is_unspec(&tlv->node)) {
		gmrf_logf(ctx->gmrf, LOG_DEBUG, "received wildcard route request, dumping table.");
		gp_babel_send_update(ctx, arg->iface, neigh);
		return;
	}

	gp_babel_route_t *route = gp_babel_route_find(ctx, &tlv->node);
	if (!route) {
		gmrf_logf(ctx->gmrf, LOG_DEBUG, "received route request for unknown route, retracting.");
		gp_babel_send_retract(ctx, arg->iface, neigh, &tlv->node);
		return;
	}

	gmrf_logf(ctx->gmrf, LOG_DEBUG, "received route request, responding.");
	gp_babel_send_update_for_route(ctx, arg->iface, neigh, route);
}

static void handle_tlv_seqno_req(gmrf_context_t *ctx, const gp_babel_tlv_seqno_req_t *tlv, size_t len, handle_tlv_arg_t *arg) {
	if (len < sizeof(gp_babel_tlv_seqno_req_t)) {
		gmrf_logf(ctx->gmrf, LOG_WARNING, "received short seqno request TLV.");
		return;
	}

	gp_babel_route_t *route = gp_babel_route_find(ctx, &tlv->node);
	if (!route || !route->selected) {
		gmrf_logf(ctx->gmrf, LOG_DEBUG, "received seqno request for unknown/unreachable route.");
		return;
	}

	gmrf_logf(ctx->gmrf, LOG_DEBUG, "seqno request received for %04x%04x, seqno %04x.",
		  ntohl(*(uint32_t*)tlv->node.id), ntohl(*(uint32_t*)(tlv->node.id+4)),
		  ntohs(tlv->seqno));

	gp_babel_neigh_t *neigh = get_tlv_neigh(ctx, arg);

	if (!gp_babel_less(route->metric.seqno, ntohs(tlv->seqno))) {
		/* local seqno is high enough */
		gmrf_logf(ctx->gmrf, LOG_DEBUG, "sending update.");
		gp_babel_send_update_for_route(ctx, arg->iface, neigh, route);
		return;
	}

	if (!route->selected->neigh) {
		/* route is local, increment seqno */
		gmrf_logf(ctx->gmrf, LOG_DEBUG, "incrementing seqno.");
		route->selected->metric_seqno.seqno++;

		gp_babel_send_update_for_route(ctx, arg->iface, neigh, route);
		return;
	}

	if (tlv->hop_count < 2) {
		gmrf_logf(ctx->gmrf, LOG_DEBUG, "hopcount too low, discarding.");
		return;
	}

	if (route->selected->neigh == neigh) {
		gmrf_logf(ctx->gmrf, LOG_DEBUG, "not forwarding backwards.");
		return;
	}

	/* forward request */
	gmrf_logf(ctx->gmrf, LOG_DEBUG, "forwarding request.");
	gp_babel_send_seqno_request(ctx, route->selected->neigh, route, ntohs(tlv->seqno), tlv->hop_count-1);
}

static void handle_tlv(gmrf_context_t *ctx, gp_babel_tlv_type_t type, const void *data, size_t len, void *arg) {
	handle_tlv_arg_t *tlv_arg = arg;

	switch (type) {
	case TLV_ACK_REQ:
		handle_tlv_ack_req(ctx, data, len, tlv_arg);
		return;

	case TLV_ACK:
		handle_tlv_ack(ctx, data, len, tlv_arg);
		return;

	case TLV_HELLO:
		handle_tlv_hello(ctx, data, len, tlv_arg);
		return;

	case TLV_IHU:
		handle_tlv_ihu(ctx, data, len, tlv_arg);
		return;

	case TLV_NODE_ID:
		handle_tlv_node_id(ctx, data, len, tlv_arg);
		return;

	case TLV_UPDATE:
		handle_tlv_update(ctx, data, len, tlv_arg);
		return;

	case TLV_ROUTE_REQ:
		handle_tlv_route_req(ctx, data, len, tlv_arg);
		return;

	case TLV_SEQNO_REQ:
		handle_tlv_seqno_req(ctx, data, len, tlv_arg);
		return;

	default:
		gmrf_logf(ctx->gmrf, LOG_DEBUG, "received unknown TLV %u on %s.", type, gmrf_iface_get_name(ctx->gmrf, tlv_arg->iface->gmrf_iface));
		return;
	}
}

void gp_babel_handle_packet(gmrf_context_t *ctx, gmrf_iface_state_t *iface, const gmrf_addr_t *source, const gp_babel_packet_t *packet) {
	handle_tlv_arg_t arg = { .iface = iface, .source = source };

	if (!gp_babel_tlv_parse(ctx, packet, handle_tlv, &arg)) {
		gmrf_logf(ctx->gmrf, LOG_DEBUG, "received invalid packet.");
	}
}