This repository has been archived by the owner on Dec 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute.c
542 lines (486 loc) · 12.7 KB
/
route.c
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*
* dhcpcd - route management
* Copyright (c) 2006-2016 Roy Marples <[email protected]>
* 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 AUTHOR 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 AUTHOR 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 <assert.h>
#include <ctype.h>
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "config.h"
#include "common.h"
#include "dhcpcd.h"
#include "if.h"
#include "ipv4.h"
#include "ipv4ll.h"
#include "ipv6.h"
#include "route.h"
#include "sa.h"
void
rt_init(struct dhcpcd_ctx *ctx)
{
TAILQ_INIT(&ctx->routes);
TAILQ_INIT(&ctx->kroutes);
TAILQ_INIT(&ctx->froutes);
}
static void
rt_desc(const char *cmd, const struct rt *rt)
{
char dest[INET_MAX_ADDRSTRLEN], gateway[INET_MAX_ADDRSTRLEN];
int prefix;
struct dhcpcd_ctx *ctx;
const char *ifname;
bool gateway_unspec;
assert(cmd != NULL);
assert(rt != NULL);
assert(rt->rt_ifp != NULL);
ctx = rt->rt_ifp->ctx;
ifname = rt->rt_ifp->name;
sa_addrtop(&rt->rt_dest, dest, sizeof(dest));
prefix = sa_toprefix(&rt->rt_netmask);
sa_addrtop(&rt->rt_gateway, gateway, sizeof(gateway));
gateway_unspec = sa_is_unspecified(&rt->rt_gateway);
if (rt->rt_flags & RTF_HOST) {
if (gateway_unspec)
logger(ctx, LOG_INFO, "%s: %s host route to %s",
ifname, cmd, dest);
else
logger(ctx, LOG_INFO, "%s: %s host route to %s via %s",
ifname, cmd, dest, gateway);
} else if (sa_is_unspecified(&rt->rt_dest) &&
sa_is_unspecified(&rt->rt_netmask))
{
if (gateway_unspec)
logger(ctx, LOG_INFO, "%s: %s default route",
ifname, cmd);
else
logger(ctx, LOG_INFO, "%s: %s default route via %s",
ifname, cmd, gateway);
} else if (gateway_unspec)
logger(ctx, LOG_INFO, "%s: %s%s route to %s/%d",
ifname, cmd,
rt->rt_flags & RTF_REJECT ? " reject" : "",
dest, prefix);
else
logger(ctx, LOG_INFO, "%s: %s%s route to %s/%d via %s",
ifname, cmd,
rt->rt_flags & RTF_REJECT ? " reject" : "",
dest, prefix, gateway);
}
void
rt_headclear(struct rt_head *rts, int af)
{
struct rt *rt, *rtn;
struct dhcpcd_ctx *ctx;
if (rts == NULL)
return;
if ((rt = TAILQ_FIRST(rts)) == NULL)
return;
ctx = rt->rt_ifp->ctx;
assert(&ctx->froutes != rts);
TAILQ_FOREACH_SAFE(rt, rts, rt_next, rtn) {
if (af != AF_UNSPEC &&
rt->rt_dest.sa_family != af &&
rt->rt_gateway.sa_family != af)
continue;
TAILQ_REMOVE(rts, rt, rt_next);
TAILQ_INSERT_TAIL(&ctx->froutes, rt, rt_next);
}
}
static void
rt_headfree(struct rt_head *rts)
{
struct rt *rt;
while ((rt = TAILQ_FIRST(rts))) {
TAILQ_REMOVE(rts, rt, rt_next);
free(rt);
}
}
void
rt_dispose(struct dhcpcd_ctx *ctx)
{
assert(ctx != NULL);
rt_headfree(&ctx->routes);
rt_headfree(&ctx->kroutes);
rt_headfree(&ctx->froutes);
}
struct rt *
rt_new(struct interface *ifp)
{
struct rt *rt;
struct dhcpcd_ctx *ctx;
assert(ifp != NULL);
ctx = ifp->ctx;
if ((rt = TAILQ_FIRST(&ctx->froutes)) != NULL)
TAILQ_REMOVE(&ctx->froutes, rt, rt_next);
else if ((rt = malloc(sizeof(*rt))) == NULL) {
logger(ctx, LOG_ERR, "%s: %m", __func__);
return NULL;
}
memset(rt, 0, sizeof(*rt));
rt->rt_ifp = ifp;
#ifdef HAVE_ROUTE_METRIC
rt->rt_metric = ifp->metric;
#endif
return rt;
}
void
rt_free(struct rt *rt)
{
assert(rt != NULL);
assert(rt->rt_ifp->ctx != NULL);
TAILQ_INSERT_TAIL(&rt->rt_ifp->ctx->froutes, rt, rt_next);
}
void
rt_freeif(struct interface *ifp)
{
struct dhcpcd_ctx *ctx;
struct rt *rt, *rtn;
if (ifp == NULL)
return;
ctx = ifp->ctx;
TAILQ_FOREACH_SAFE(rt, &ctx->routes, rt_next, rtn) {
if (rt->rt_ifp == ifp) {
TAILQ_REMOVE(&ctx->routes, rt, rt_next);
rt_free(rt);
}
}
TAILQ_FOREACH_SAFE(rt, &ctx->kroutes, rt_next, rtn) {
if (rt->rt_ifp == ifp) {
TAILQ_REMOVE(&ctx->kroutes, rt, rt_next);
rt_free(rt);
}
}
}
struct rt *
rt_find(struct rt_head *rts, const struct rt *f)
{
struct rt *rt;
assert(rts != NULL);
assert(f != NULL);
TAILQ_FOREACH(rt, rts, rt_next) {
if (sa_cmp(&rt->rt_dest, &f->rt_dest) == 0 &&
#ifdef HAVE_ROUTE_METRIC
(f->rt_ifp == NULL ||
rt->rt_ifp->metric == f->rt_ifp->metric) &&
#endif
sa_cmp(&rt->rt_netmask, &f->rt_netmask) == 0)
return rt;
}
return NULL;
}
static void
rt_kfree(struct rt *rt)
{
struct dhcpcd_ctx *ctx;
struct rt *f;
assert(rt != NULL);
ctx = rt->rt_ifp->ctx;
if ((f = rt_find(&ctx->kroutes, rt)) != NULL) {
TAILQ_REMOVE(&ctx->kroutes, f, rt_next);
rt_free(f);
}
}
/* If something other than dhcpcd removes a route,
* we need to remove it from our internal table. */
void
rt_recvrt(int cmd, const struct rt *rt)
{
struct dhcpcd_ctx *ctx;
struct rt *f;
assert(rt != NULL);
ctx = rt->rt_ifp->ctx;
f = rt_find(&ctx->kroutes, rt);
switch(cmd) {
case RTM_DELETE:
if (f != NULL) {
TAILQ_REMOVE(&ctx->kroutes, f, rt_next);
rt_free(f);
}
if ((f = rt_find(&ctx->routes, rt)) != NULL) {
TAILQ_REMOVE(&ctx->routes, f, rt_next);
rt_desc("deleted", f);
rt_free(f);
}
break;
case RTM_ADD:
if (f != NULL)
break;
if ((f = rt_new(rt->rt_ifp)) == NULL)
break;
memcpy(f, rt, sizeof(*f));
TAILQ_INSERT_TAIL(&ctx->kroutes, f, rt_next);
break;
}
#ifdef HAVE_ROUTE_METRIC
if (rt->rt_dest.sa_family == AF_INET)
ipv4ll_recvrt(cmd, rt);
#endif
}
static bool
rt_add(struct rt *nrt, struct rt *ort)
{
struct dhcpcd_ctx *ctx;
bool change;
unsigned long long options;
assert(nrt != NULL);
ctx = nrt->rt_ifp->ctx;
/* Don't set default routes if not asked to */
options = nrt->rt_ifp->options == NULL ?
ctx->options : nrt->rt_ifp->options->options;
if (!(options & DHCPCD_GATEWAY) &&
sa_is_unspecified(&nrt->rt_dest) &&
sa_is_unspecified(&nrt->rt_netmask))
return false;
rt_desc(ort == NULL ? "adding" : "changing", nrt);
change = false;
if (ort == NULL) {
ort = rt_find(&ctx->kroutes, nrt);
if (ort != NULL &&
((ort->rt_flags & RTF_REJECT &&
nrt->rt_flags & RTF_REJECT) ||
(ort->rt_ifp == nrt->rt_ifp &&
#ifdef HAVE_ROUTE_METRIC
ort->rt_metric == nrt->rt_metric &&
#endif
sa_cmp(&ort->rt_gateway, &nrt->rt_gateway) == 0)))
{
if (ort->rt_mtu == nrt->rt_mtu)
return true;
change = true;
}
} else if (ort->rt_dflags & RTDF_FAKE &&
!(nrt->rt_dflags & RTDF_FAKE) &&
ort->rt_ifp == nrt->rt_ifp &&
#ifdef HAVE_ROUTE_METRIC
ort->rt_metric == nrt->rt_metric &&
#endif
sa_cmp(&ort->rt_dest, &nrt->rt_dest) == 0 &&
sa_cmp(&ort->rt_netmask, &nrt->rt_netmask) == 0 &&
sa_cmp(&ort->rt_gateway, &nrt->rt_gateway) == 0)
{
if (ort->rt_mtu == nrt->rt_mtu)
return true;
change = true;
}
#ifdef RTF_CLONING
/* BSD can set routes to be cloning routes.
* Cloned routes inherit the parent flags.
* As such, we need to delete and re-add the route to flush children
* to correct the flags. */
if (change && ort != NULL && ort->rt_flags & RTF_CLONING)
change = true;
#endif
if (change) {
if (if_route(RTM_CHANGE, nrt) != -1)
return true;
if (errno != ESRCH)
logger(ctx, LOG_ERR, "if_route (CHG): %m");
}
#ifdef HAVE_ROUTE_METRIC
/* With route metrics, we can safely add the new route before
* deleting the old route. */
if (if_route(RTM_ADD, nrt) != -1) {
if (ort != NULL) {
if (if_route(RTM_DELETE, ort) == -1 && errno != ESRCH)
logger(ctx, LOG_ERR, "if_route (DEL): %m");
rt_kfree(ort);
}
return true;
}
/* If the kernel claims the route exists we need to rip out the
* old one first. */
if (errno != EEXIST || ort == NULL)
goto logerr;
#endif
/* No route metrics, we need to delete the old route before
* adding the new one. */
#ifdef ROUTE_PER_GATEWAY
errno = 0;
#endif
if (ort != NULL) {
if (if_route(RTM_DELETE, ort) == -1 && errno != ESRCH)
logger(ctx, LOG_ERR, "if_route (DEL): %m");
else
rt_kfree(ort);
}
#ifdef ROUTE_PER_GATEWAY
/* The OS allows many routes to the same dest with different gateways.
* dhcpcd does not support this yet, so for the time being just keep on
* deleting the route until there is an error. */
if (ort != NULL && errno == 0) {
for (;;) {
if (if_route(RTM_DELETE, ort) == -1)
break;
}
}
#endif
if (if_route(RTM_ADD, nrt) != -1)
return true;
#ifdef HAVE_ROUTE_METRIC
logerr:
#endif
logger(ctx, LOG_ERR, "if_route (ADD): %m");
return false;
}
static bool
rt_delete(struct rt *rt)
{
int retval;
rt_desc("deleting", rt);
retval = if_route(RTM_DELETE, rt) == -1 ? false : true;
if (!retval && errno != ENOENT && errno != ESRCH)
logger(rt->rt_ifp->ctx, LOG_ERR,
"%s: if_delroute: %m", rt->rt_ifp->name);
/* Remove the route from our kernel table so we can add a
* IPv4LL default route if possible. */
else
rt_kfree(rt);
return retval;
}
static bool
rt_cmp(const struct rt *r1, const struct rt *r2)
{
return (r1->rt_ifp == r2->rt_ifp &&
#ifdef HAVE_ROUTE_METRIC
r1->rt_metric == r2->rt_metric &&
#endif
sa_cmp(&r1->rt_gateway, &r1->rt_gateway) == 0);
}
static bool
rt_doroute(struct rt *rt)
{
struct dhcpcd_ctx *ctx;
struct rt *or;
ctx = rt->rt_ifp->ctx;
/* Do we already manage it? */
if ((or = rt_find(&ctx->routes, rt))) {
if (rt->rt_dflags & RTDF_FAKE)
return true;
if (or->rt_dflags & RTDF_FAKE ||
!rt_cmp(rt, or) ||
(rt->rt_ifa.sa_family != AF_UNSPEC &&
sa_cmp(&or->rt_ifa, &rt->rt_ifa) != 0) ||
or->rt_mtu != rt->rt_mtu)
{
if (!rt_add(rt, or))
return false;
}
TAILQ_REMOVE(&ctx->routes, or, rt_next);
rt_free(or);
} else {
if (rt->rt_dflags & RTDF_FAKE) {
if ((or = rt_find(&ctx->kroutes, rt)) == NULL)
return false;
if (!rt_cmp(rt, or))
return false;
} else {
if (!rt_add(rt, NULL))
return false;
}
}
return true;
}
void
rt_build(struct dhcpcd_ctx *ctx, int af)
{
struct rt_head routes, added;
struct rt *rt, *rtn;
unsigned long long o;
#ifdef INET6
bool have_default = false;
#endif
/* We need to have the interfaces in the correct order to ensure
* our routes are managed correctly. */
if_sortinterfaces(ctx);
TAILQ_INIT(&routes);
TAILQ_INIT(&added);
switch (af) {
#ifdef INET
case AF_INET:
if (!inet_getroutes(ctx, &routes))
return;
break;
#endif
#ifdef INET6
case AF_INET6:
if (!inet6_getroutes(ctx, &routes))
return;
break;
#endif
}
TAILQ_FOREACH_SAFE(rt, &routes, rt_next, rtn) {
if (rt->rt_dest.sa_family != af &&
rt->rt_gateway.sa_family != af)
continue;
/* Is this route already in our table? */
if ((rt_find(&added, rt)) != NULL)
continue;
if (rt_doroute(rt)) {
TAILQ_REMOVE(&routes, rt, rt_next);
TAILQ_INSERT_TAIL(&added, rt, rt_next);
#ifdef INET6
if (sa_is_unspecified(&rt->rt_dest) &&
sa_is_unspecified(&rt->rt_netmask))
have_default = true;
#endif
}
}
/* Remove old routes we used to manage
* If we own the default route, but not RA management itself
* then we need to preserve the last best default route we had */
TAILQ_FOREACH_SAFE(rt, &ctx->routes, rt_next, rtn) {
if (rt->rt_dest.sa_family != af &&
rt->rt_gateway.sa_family != af)
continue;
TAILQ_REMOVE(&ctx->routes, rt, rt_next);
if (rt_find(&added, rt) == NULL) {
o = rt->rt_ifp->options ?
rt->rt_ifp->options->options :
ctx->options;
#ifdef INET6
if (!have_default &&
(o & DHCPCD_IPV6RA_OWN_DEFAULT) &&
!(o & DHCPCD_IPV6RA_OWN) &&
rt->rt_dest.sa_family == AF_INET6 &&
sa_is_unspecified(&rt->rt_dest))
have_default = true;
/* no need to add it back to our routing table
* as we delete an exiting route when we add
* a new one */
else
#endif
if ((o &
(DHCPCD_EXITING | DHCPCD_PERSISTENT)) !=
(DHCPCD_EXITING | DHCPCD_PERSISTENT))
rt_delete(rt);
}
TAILQ_INSERT_TAIL(&ctx->froutes, rt, rt_next);
}
rt_headclear(&ctx->routes, af);
TAILQ_CONCAT(&ctx->routes, &added, rt_next);
rt_headclear(&routes, AF_UNSPEC);
}