Coverage Report

Created: 2026-11-11 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openvswitch/lib/route-table.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2011, 2012, 2013, 2014, 2017 Nicira, Inc.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at:
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
17
#include <config.h>
18
19
#include "route-table.h"
20
21
#include <errno.h>
22
#include <sys/types.h>
23
#include <netinet/in.h>
24
#include <arpa/inet.h>
25
#include <sys/socket.h>
26
#include <linux/fib_rules.h>
27
#include <linux/rtnetlink.h>
28
#include <net/if.h>
29
30
#include "coverage.h"
31
#include "hash.h"
32
#include "netdev.h"
33
#include "netlink.h"
34
#include "netlink-notifier.h"
35
#include "netlink-socket.h"
36
#include "openvswitch/list.h"
37
#include "openvswitch/ofpbuf.h"
38
#include "ovs-router.h"
39
#include "packets.h"
40
#include "rtnetlink.h"
41
#include "tnl-ports.h"
42
#include "openvswitch/vlog.h"
43
44
/* Below constants were added in different Linux versions, so define them just
45
 * in case we're building with old headers. (We can't test for it with #ifdef
46
 * because it's an enum.) */
47
0
#define RTA_MARK 16 /* Linux 2.6.36 */
48
0
#define RTA_NH_ID 30 /* Linux 5.3 */
49
0
#define FRA_SUPPRESS_PREFIXLEN 14 /* Linux 3.12 */
50
0
#define FRA_TABLE 15 /* Linux 2.6.19 */
51
0
#define FRA_PROTOCOL 21 /* Linux 4.17 */
52
53
/* Linux 4.1 added RTA_VIA. */
54
#ifndef HAVE_RTA_VIA
55
#define RTA_VIA 18
56
struct rtvia {
57
    sa_family_t rtvia_family;
58
    uint8_t     rtvia_addr[];
59
};
60
#endif
61
62
VLOG_DEFINE_THIS_MODULE(route_table);
63
64
COVERAGE_DEFINE(route_table_dump);
65
66
BUILD_ASSERT_DECL((enum rt_class_t) CLS_DEFAULT == RT_TABLE_DEFAULT);
67
BUILD_ASSERT_DECL((enum rt_class_t) CLS_MAIN == RT_TABLE_MAIN);
68
BUILD_ASSERT_DECL((enum rt_class_t) CLS_LOCAL == RT_TABLE_LOCAL);
69
70
static struct ovs_mutex route_table_mutex = OVS_MUTEX_INITIALIZER;
71
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
72
73
/* Global change number for route-table, which should be incremented
74
 * every time route_table_reset() is called.  */
75
static uint64_t rt_change_seq;
76
77
static struct nln *nln = NULL;
78
static struct route_table_msg nln_rtmsg_change;
79
static struct nln_notifier *route_notifier = NULL;
80
static struct nln_notifier *route6_notifier = NULL;
81
static struct nln_notifier *rule_notifier = NULL;
82
static struct nln_notifier *rule6_notifier = NULL;
83
static struct nln_notifier *name_notifier = NULL;
84
85
static bool route_table_valid = false;
86
static bool rules_valid = false;
87
88
static int route_nln_parse(struct ofpbuf *, void *change);
89
90
static void rule_handle_msg(const struct route_table_msg *);
91
static int rule_parse(struct ofpbuf *, void *change);
92
93
static void route_table_reset(void);
94
static void route_table_handle_msg(const struct route_table_msg *, void *aux,
95
                                   uint32_t table);
96
static void route_table_change(struct route_table_msg *, void *aux);
97
static void rules_change(const struct route_table_msg *, void *aux);
98
static void route_map_clear(void);
99
100
static void name_table_init(void);
101
static void name_table_change(const struct rtnetlink_change *, void *);
102
103
static void
104
route_data_destroy_nexthops__(struct route_data *rd)
105
0
{
106
0
    struct route_data_nexthop *rdnh;
107
108
0
    LIST_FOR_EACH_POP (rdnh, nexthop_node, &rd->nexthops) {
109
0
        if (rdnh && rdnh != &rd->primary_next_hop__) {
110
0
            free(rdnh);
111
0
        }
112
0
    }
113
0
}
114
115
void
116
route_data_destroy(struct route_data *rd)
117
0
{
118
0
    route_data_destroy_nexthops__(rd);
119
0
}
120
121
uint64_t
122
route_table_get_change_seq(void)
123
0
{
124
0
    return rt_change_seq;
125
0
}
126
127
/* Users of the route_table module should register themselves with this
128
 * function before making any other route_table function calls. */
129
void
130
route_table_init(void)
131
    OVS_EXCLUDED(route_table_mutex)
132
0
{
133
0
    ovs_mutex_lock(&route_table_mutex);
134
0
    ovs_assert(!nln);
135
0
    ovs_assert(!route_notifier);
136
0
    ovs_assert(!route6_notifier);
137
0
    ovs_assert(!rule_notifier);
138
0
    ovs_assert(!rule6_notifier);
139
140
0
    ovs_router_init();
141
0
    nln = nln_create(NETLINK_ROUTE, route_nln_parse, &nln_rtmsg_change);
142
143
0
    route_notifier =
144
0
        nln_notifier_create(nln, RTNLGRP_IPV4_ROUTE,
145
0
                            (nln_notify_func *) route_table_change, NULL);
146
0
    route6_notifier =
147
0
        nln_notifier_create(nln, RTNLGRP_IPV6_ROUTE,
148
0
                            (nln_notify_func *) route_table_change, NULL);
149
150
0
    rule_notifier =
151
0
        nln_notifier_create(nln, RTNLGRP_IPV4_RULE,
152
0
                            (nln_notify_func *) rules_change, NULL);
153
0
    rule6_notifier =
154
0
        nln_notifier_create(nln, RTNLGRP_IPV6_RULE,
155
0
                            (nln_notify_func *) rules_change, NULL);
156
157
0
    route_table_reset();
158
0
    name_table_init();
159
160
0
    ovs_mutex_unlock(&route_table_mutex);
161
0
}
162
163
/* Run periodically to update the locally maintained routing table. */
164
void
165
route_table_run(void)
166
    OVS_EXCLUDED(route_table_mutex)
167
0
{
168
0
    ovs_mutex_lock(&route_table_mutex);
169
0
    if (nln) {
170
0
        rtnetlink_run();
171
0
        nln_run(nln);
172
173
0
        if (!route_table_valid || !rules_valid) {
174
0
            route_table_reset();
175
0
        }
176
0
    }
177
0
    ovs_mutex_unlock(&route_table_mutex);
178
0
}
179
180
/* Causes poll_block() to wake up when route_table updates are required. */
181
void
182
route_table_wait(void)
183
    OVS_EXCLUDED(route_table_mutex)
184
0
{
185
0
    ovs_mutex_lock(&route_table_mutex);
186
0
    if (nln) {
187
0
        rtnetlink_wait();
188
0
        nln_wait(nln);
189
0
    }
190
0
    ovs_mutex_unlock(&route_table_mutex);
191
0
}
192
193
bool
194
route_table_dump_one_table(uint32_t id, sa_family_t family,
195
                           route_table_handle_msg_callback *handle_msg_cb,
196
                           void *aux)
197
0
{
198
0
    uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
199
0
    struct ofpbuf request, reply, buf;
200
0
    struct rtmsg *rq_msg;
201
0
    bool filtered = true;
202
0
    struct nl_dump dump;
203
204
0
    ofpbuf_init(&request, 0);
205
206
0
    nl_msg_put_nlmsghdr(&request, sizeof *rq_msg, RTM_GETROUTE, NLM_F_REQUEST);
207
208
0
    rq_msg = ofpbuf_put_zeros(&request, sizeof *rq_msg);
209
0
    rq_msg->rtm_family = family;
210
211
0
    if (id > UCHAR_MAX) {
212
0
        rq_msg->rtm_table = RT_TABLE_UNSPEC;
213
0
        nl_msg_put_u32(&request, RTA_TABLE, id);
214
0
    } else {
215
0
        rq_msg->rtm_table = id;
216
0
    }
217
218
0
    nl_dump_start(&dump, NETLINK_ROUTE, &request);
219
0
    ofpbuf_uninit(&request);
220
221
0
    ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
222
0
    while (nl_dump_next(&dump, &reply, &buf)) {
223
0
        struct route_table_msg msg;
224
225
0
        if (route_table_parse(&reply, &msg)) {
226
0
            struct nlmsghdr *nlmsghdr = nl_msg_nlmsghdr(&reply);
227
228
            /* Older kernels do not support filtering. */
229
0
            if (!(nlmsghdr->nlmsg_flags & NLM_F_DUMP_FILTERED)) {
230
0
                filtered = false;
231
0
            }
232
0
            handle_msg_cb(&msg, aux, id);
233
0
            route_data_destroy(&msg.rd);
234
0
        }
235
0
    }
236
0
    ofpbuf_uninit(&buf);
237
0
    nl_dump_done(&dump);
238
239
0
    return filtered;
240
0
}
241
242
static void
243
rules_dump(void)
244
0
{
245
0
    uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
246
0
    struct ofpbuf request, reply, buf;
247
0
    struct fib_rule_hdr *rq_msg;
248
0
    struct nl_dump dump;
249
250
0
    ofpbuf_init(&request, 0);
251
252
0
    nl_msg_put_nlmsghdr(&request, sizeof *rq_msg, RTM_GETRULE, NLM_F_REQUEST);
253
254
0
    rq_msg = ofpbuf_put_zeros(&request, sizeof *rq_msg);
255
0
    rq_msg->family = AF_UNSPEC;
256
257
0
    nl_dump_start(&dump, NETLINK_ROUTE, &request);
258
0
    ofpbuf_uninit(&request);
259
260
0
    ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
261
0
    while (nl_dump_next(&dump, &reply, &buf)) {
262
0
        struct route_table_msg msg;
263
264
0
        if (rule_parse(&reply, &msg)) {
265
0
            rule_handle_msg(&msg);
266
0
        }
267
0
    }
268
0
    ofpbuf_uninit(&buf);
269
0
    nl_dump_done(&dump);
270
0
}
271
272
static void
273
route_table_reset(void)
274
0
{
275
0
    route_map_clear();
276
0
    netdev_get_addrs_list_flush();
277
0
    route_table_valid = true;
278
0
    rules_valid = true;
279
0
    rt_change_seq++;
280
0
    COVERAGE_INC(route_table_dump);
281
0
    rules_dump();
282
0
}
283
284
static void
285
rule_handle_msg(const struct route_table_msg *change)
286
0
{
287
0
    if (change->relevant) {
288
0
        const struct rule_data *rd = &change->rud;
289
0
        sa_family_t family = rd->ipv4 ? AF_INET : AF_INET6;
290
291
0
        route_table_dump_one_table(rd->lookup_table, family,
292
0
                                   route_table_handle_msg, NULL);
293
0
        ovs_router_rule_add(rd->prio, rd->invert, false, rd->src_len,
294
0
                            &rd->from_addr, rd->lookup_table, rd->ipv4);
295
0
    }
296
0
}
297
298
static int
299
route_nln_parse(struct ofpbuf *buf, void *change_)
300
0
{
301
0
    const struct nlmsghdr *nlmsg = buf->data;
302
303
0
    if (nlmsg->nlmsg_type == RTM_NEWROUTE ||
304
0
        nlmsg->nlmsg_type == RTM_DELROUTE) {
305
0
        return route_table_parse(buf, change_);
306
0
    } else if (nlmsg->nlmsg_type == RTM_NEWRULE ||
307
0
               nlmsg->nlmsg_type == RTM_DELRULE) {
308
0
        return rule_parse(buf, change_);
309
0
    }
310
311
0
    VLOG_DBG_RL(&rl, "received unsupported rtnetlink route message");
312
0
    return 0;
313
0
}
314
315
/* Return RTNLGRP_IPV4_RULE or RTNLGRP_IPV6_RULE on success, 0 on parse
316
 * error. */
317
static int
318
rule_parse(struct ofpbuf *buf, void *change_)
319
0
{
320
0
    struct route_table_msg *change = change_;
321
0
    bool ipv4 = false;
322
0
    bool parsed;
323
324
0
    static const struct nl_policy policy[] = {
325
0
        [FRA_PRIORITY] = { .type = NL_A_U32, .optional = true },
326
0
        [FRA_SRC] = { .type = NL_A_U32, .optional = true },
327
0
        [FRA_TABLE] = { .type = NL_A_U32, .optional = true },
328
0
    };
329
330
0
    static const struct nl_policy policy6[] = {
331
0
        [FRA_PRIORITY] = { .type = NL_A_U32, .optional = true },
332
0
        [FRA_SRC] = { .type = NL_A_IPV6, .optional = true },
333
0
        [FRA_TABLE] = { .type = NL_A_U32, .optional = true },
334
0
    };
335
336
0
    struct nlattr *attrs[ARRAY_SIZE(policy)];
337
0
    const struct fib_rule_hdr *frh;
338
339
0
    frh = ofpbuf_at(buf, NLMSG_HDRLEN, sizeof *frh);
340
0
    if (!frh || frh->action != FR_ACT_TO_TBL || frh->tos || frh->dst_len) {
341
        /* Unsupported rule. */
342
0
        return 0;
343
0
    }
344
345
0
    if (frh->family == AF_INET) {
346
0
        parsed = nl_policy_parse(buf, NLMSG_HDRLEN +
347
0
                                 sizeof(struct fib_rule_hdr), policy, attrs,
348
0
                                 ARRAY_SIZE(policy));
349
0
        ipv4 = true;
350
0
    } else if (frh->family == AF_INET6) {
351
0
        parsed = nl_policy_parse(buf, NLMSG_HDRLEN +
352
0
                                 sizeof(struct fib_rule_hdr), policy6, attrs,
353
0
                                 ARRAY_SIZE(policy6));
354
0
    } else {
355
0
        VLOG_DBG_RL(&rl, "received non AF_INET rtnetlink route message");
356
0
        return 0;
357
0
    }
358
359
0
    if (parsed) {
360
0
        const struct nlmsghdr *nlmsg;
361
362
0
        nlmsg = buf->data;
363
364
0
        memset(change, 0, sizeof *change);
365
0
        change->relevant = true;
366
0
        change->nlmsg_type = nlmsg->nlmsg_type;
367
0
        change->rud.invert = false;
368
0
        change->rud.src_len = frh->src_len;
369
0
        change->rud.lookup_table = frh->table;
370
0
        change->rud.ipv4 = ipv4;
371
372
0
        if (frh->flags & FIB_RULE_INVERT) {
373
            /* Invert the matching of rule selector. */
374
0
            change->rud.invert = true;
375
0
        }
376
377
0
        if (attrs[FRA_PRIORITY]) {
378
0
            change->rud.prio = nl_attr_get_u32(attrs[FRA_PRIORITY]);
379
0
        }
380
381
0
        if (attrs[FRA_SRC]) {
382
0
            if (ipv4) {
383
0
                ovs_be32 src = nl_attr_get_be32(attrs[FRA_SRC]);
384
0
                in6_addr_set_mapped_ipv4(&change->rud.from_addr, src);
385
0
            } else {
386
0
                change->rud.from_addr = nl_attr_get_in6_addr(attrs[FRA_SRC]);
387
0
            }
388
0
        } else {
389
0
            if (ipv4) {
390
0
                in6_addr_set_mapped_ipv4(&change->rud.from_addr, 0);
391
0
            } else {
392
0
                change->rud.from_addr = in6addr_any;
393
0
            }
394
0
        }
395
396
0
        if (attrs[FRA_TABLE]) {
397
0
            change->rud.lookup_table = nl_attr_get_u32(attrs[FRA_TABLE]);
398
0
        } else {
399
0
            change->relevant = false;
400
0
        }
401
402
0
        if (change->rud.invert && !change->rud.src_len) {
403
0
            change->relevant = false;
404
0
        }
405
0
    } else {
406
0
        VLOG_DBG_RL(&rl, "received unparseable rtnetlink rule message");
407
0
        return 0;
408
0
    }
409
410
    /* Check if there are any additional attributes that aren't supported
411
     * currently by OVS rule-based route lookup. */
412
0
    if (change->relevant) {
413
0
        size_t offset = NLMSG_HDRLEN + sizeof(struct fib_rule_hdr);
414
0
        struct nlattr *nla;
415
0
        size_t left;
416
417
0
        NL_ATTR_FOR_EACH (nla, left, ofpbuf_at(buf, offset, 0),
418
0
                          buf->size - offset) {
419
0
            uint16_t type = nl_attr_type(nla);
420
421
0
            if ((type > FRA_SRC && type < FRA_PRIORITY) ||
422
0
                (type > FRA_PRIORITY && type < FRA_SUPPRESS_PREFIXLEN) ||
423
0
                (type > FRA_TABLE && type < FRA_PROTOCOL) ||
424
0
                type > FRA_PROTOCOL) {
425
0
                change->relevant = false;
426
0
                break;
427
0
            }
428
0
        }
429
0
    }
430
431
    /* Success. */
432
0
    return ipv4 ? RTNLGRP_IPV4_RULE : RTNLGRP_IPV6_RULE;
433
0
}
434
435
/* Returns true if the given route requires nexthop information (output
436
 * interface, nexthop IP, ...).  Returns false for special route types
437
 * that don't need this information. */
438
static bool
439
route_type_needs_nexthop(unsigned char rtmsg_type)
440
0
{
441
0
    switch (rtmsg_type) {
442
0
    case RTN_BLACKHOLE:
443
0
    case RTN_THROW:
444
0
    case RTN_UNREACHABLE:
445
0
    case RTN_PROHIBIT:
446
0
        return false;
447
448
0
    default:
449
0
        return true;
450
0
    }
451
0
}
452
453
static int
454
route_table_parse__(struct ofpbuf *buf, size_t ofs,
455
                    const struct nlmsghdr *nlmsg,
456
                    const struct rtmsg *rtm,
457
                    const struct rtnexthop *rtnh,
458
                    struct route_table_msg *change)
459
0
{
460
0
    bool parsed, ipv4 = false;
461
462
0
    static const struct nl_policy policy[] = {
463
0
        [RTA_DST] = { .type = NL_A_U32, .optional = true  },
464
0
        [RTA_OIF] = { .type = NL_A_U32, .optional = true },
465
0
        [RTA_GATEWAY] = { .type = NL_A_U32, .optional = true },
466
0
        [RTA_MARK] = { .type = NL_A_U32, .optional = true },
467
0
        [RTA_PREFSRC] = { .type = NL_A_U32, .optional = true },
468
0
        [RTA_TABLE] = { .type = NL_A_U32, .optional = true },
469
0
        [RTA_PRIORITY] = { .type = NL_A_U32, .optional = true },
470
0
        [RTA_VIA] = { .type = NL_A_RTA_VIA, .optional = true },
471
0
        [RTA_MULTIPATH] = { .type = NL_A_NESTED, .optional = true },
472
0
        [RTA_NH_ID] = { .type = NL_A_U32, .optional = true },
473
0
    };
474
475
0
    static const struct nl_policy policy6[] = {
476
0
        [RTA_DST] = { .type = NL_A_IPV6, .optional = true },
477
0
        [RTA_OIF] = { .type = NL_A_U32, .optional = true },
478
0
        [RTA_MARK] = { .type = NL_A_U32, .optional = true },
479
0
        [RTA_GATEWAY] = { .type = NL_A_IPV6, .optional = true },
480
0
        [RTA_PREFSRC] = { .type = NL_A_IPV6, .optional = true },
481
0
        [RTA_TABLE] = { .type = NL_A_U32, .optional = true },
482
0
        [RTA_PRIORITY] = { .type = NL_A_U32, .optional = true },
483
0
        [RTA_VIA] = { .type = NL_A_RTA_VIA, .optional = true },
484
0
        [RTA_MULTIPATH] = { .type = NL_A_NESTED, .optional = true },
485
0
        [RTA_NH_ID] = { .type = NL_A_U32, .optional = true },
486
0
    };
487
488
0
    struct nlattr *attrs[ARRAY_SIZE(policy)];
489
490
0
    if (rtm->rtm_family == AF_INET) {
491
0
        parsed = nl_policy_parse(buf, ofs, policy, attrs,
492
0
                                 ARRAY_SIZE(policy));
493
0
        ipv4 = true;
494
0
    } else if (rtm->rtm_family == AF_INET6) {
495
0
        parsed = nl_policy_parse(buf, ofs, policy6, attrs,
496
0
                                 ARRAY_SIZE(policy6));
497
0
    } else {
498
0
        VLOG_DBG_RL(&rl, "received non AF_INET rtnetlink route message");
499
0
        return 0;
500
0
    }
501
502
0
    if (parsed) {
503
0
        struct route_data_nexthop *rdnh = NULL;
504
0
        int rta_oif;      /* Output interface index. */
505
506
0
        memset(change, 0, sizeof *change);
507
508
0
        ovs_list_init(&change->rd.nexthops);
509
0
        rdnh = rtnh ? xzalloc(sizeof *rdnh) : &change->rd.primary_next_hop__;
510
0
        ovs_list_insert(&change->rd.nexthops, &rdnh->nexthop_node);
511
512
0
        rdnh->family = rtm->rtm_family;
513
0
        change->relevant = true;
514
515
0
        if (rtm->rtm_scope == RT_SCOPE_NOWHERE) {
516
0
            change->relevant = false;
517
0
        }
518
519
0
        if (rtm->rtm_type != RTN_UNICAST &&
520
0
            rtm->rtm_type != RTN_LOCAL) {
521
0
            change->relevant = false;
522
0
        }
523
524
0
        change->rd.rta_table_id = rtm->rtm_table;
525
0
        if (attrs[RTA_TABLE]) {
526
0
            change->rd.rta_table_id = nl_attr_get_u32(attrs[RTA_TABLE]);
527
0
        }
528
529
0
        change->nlmsg_type     = nlmsg->nlmsg_type;
530
0
        change->rd.rtm_dst_len = rtm->rtm_dst_len;
531
0
        change->rd.rtm_protocol = rtm->rtm_protocol;
532
0
        change->rd.rtn_local = rtm->rtm_type == RTN_LOCAL;
533
0
        if (attrs[RTA_OIF] && rtnh) {
534
0
            VLOG_DBG_RL(&rl, "unexpected RTA_OIF attribute while parsing "
535
0
                             "nested RTA_MULTIPATH attributes");
536
0
            goto error_out;
537
0
        }
538
0
        if (attrs[RTA_OIF] || rtnh) {
539
0
            rta_oif = rtnh ? rtnh->rtnh_ifindex
540
0
                           : nl_attr_get_u32(attrs[RTA_OIF]);
541
542
0
            if (!if_indextoname(rta_oif, rdnh->ifname)) {
543
0
                int error = errno;
544
545
0
                VLOG_DBG_RL(&rl, "could not find interface name[%u]: %s",
546
0
                            rta_oif, ovs_strerror(error));
547
0
                if (error == ENXIO) {
548
0
                    change->relevant = false;
549
0
                } else {
550
0
                    goto error_out;
551
0
                }
552
0
            }
553
0
        }
554
555
0
        if (attrs[RTA_DST]) {
556
0
            if (ipv4) {
557
0
                ovs_be32 dst;
558
0
                dst = nl_attr_get_be32(attrs[RTA_DST]);
559
0
                in6_addr_set_mapped_ipv4(&change->rd.rta_dst, dst);
560
0
            } else {
561
0
                change->rd.rta_dst = nl_attr_get_in6_addr(attrs[RTA_DST]);
562
0
            }
563
0
        } else if (ipv4) {
564
0
            in6_addr_set_mapped_ipv4(&change->rd.rta_dst, 0);
565
0
        }
566
0
        if (attrs[RTA_PREFSRC]) {
567
0
            if (ipv4) {
568
0
                ovs_be32 prefsrc;
569
0
                prefsrc = nl_attr_get_be32(attrs[RTA_PREFSRC]);
570
0
                in6_addr_set_mapped_ipv4(&change->rd.rta_prefsrc, prefsrc);
571
0
            } else {
572
0
                change->rd.rta_prefsrc =
573
0
                    nl_attr_get_in6_addr(attrs[RTA_PREFSRC]);
574
0
            }
575
0
        }
576
0
        if (attrs[RTA_GATEWAY]) {
577
0
            if (ipv4) {
578
0
                ovs_be32 gw;
579
0
                gw = nl_attr_get_be32(attrs[RTA_GATEWAY]);
580
0
                in6_addr_set_mapped_ipv4(&rdnh->addr, gw);
581
0
            } else {
582
0
                rdnh->addr = nl_attr_get_in6_addr(attrs[RTA_GATEWAY]);
583
0
            }
584
0
        }
585
0
        if (attrs[RTA_MARK]) {
586
0
            change->rd.rta_mark = nl_attr_get_u32(attrs[RTA_MARK]);
587
0
        }
588
0
        if (attrs[RTA_PRIORITY]) {
589
0
            change->rd.rta_priority = nl_attr_get_u32(attrs[RTA_PRIORITY]);
590
0
        }
591
0
        if (attrs[RTA_NH_ID]) {
592
0
            change->rd.rta_nhid = nl_attr_get_u32(attrs[RTA_NH_ID]);
593
0
        }
594
0
        if (attrs[RTA_VIA]) {
595
0
            const struct rtvia *rtvia = nl_attr_get(attrs[RTA_VIA]);
596
0
            ovs_be32 addr;
597
598
0
            if (attrs[RTA_GATEWAY]) {
599
0
                VLOG_DBG_RL(&rl, "route message can not contain both "
600
0
                                 "RTA_GATEWAY and RTA_VIA");
601
0
                goto error_out;
602
0
            }
603
604
0
            rdnh->family = rtvia->rtvia_family;
605
606
0
            switch (rdnh->family) {
607
0
            case AF_INET:
608
0
                if (nl_attr_get_size(attrs[RTA_VIA])
609
0
                        - sizeof *rtvia < sizeof addr) {
610
0
                    VLOG_DBG_RL(&rl, "got short message while parsing RTA_VIA "
611
0
                                     "attribute for family AF_INET");
612
0
                    goto error_out;
613
0
                }
614
0
                memcpy(&addr, rtvia->rtvia_addr, sizeof addr);
615
0
                in6_addr_set_mapped_ipv4(&rdnh->addr, addr);
616
0
                break;
617
618
0
            case AF_INET6:
619
0
                if (nl_attr_get_size(attrs[RTA_VIA])
620
0
                        - sizeof *rtvia < sizeof rdnh->addr) {
621
0
                    VLOG_DBG_RL(&rl, "got short message while parsing RTA_VIA "
622
0
                                     "attribute for family AF_INET6");
623
0
                    goto error_out;
624
0
                }
625
0
                memcpy(&rdnh->addr, rtvia->rtvia_addr, sizeof rdnh->addr);
626
0
                break;
627
628
0
            default:
629
0
                VLOG_DBG_RL(&rl, "unsupported address family, %d, "
630
0
                                 "in via attribute", rdnh->family);
631
0
                goto error_out;
632
0
            }
633
0
        }
634
0
        if (attrs[RTA_MULTIPATH]) {
635
0
            const struct nlattr *nla;
636
0
            size_t left;
637
638
0
            if (rtnh) {
639
0
                VLOG_DBG_RL(&rl, "unexpected nested RTA_MULTIPATH attribute");
640
0
                goto error_out;
641
0
            }
642
643
            /* The change->rd->nexthops list is unconditionally populated with
644
             * a single rdnh entry as we start parsing above.  Multiple
645
             * branches above may access it or jump to error_out, and having it
646
             * on the list is the only way to ensure proper cleanup.
647
             *
648
             * Getting to this point, we know that the above branches has not
649
             * provided next hop information, because information about
650
             * multiple next hops is encoded in the nested attributes after the
651
             * RTA_MULTIPATH attribute.
652
             *
653
             * Before retrieving those we need to remove the empty rdnh entry
654
             * from the list. */
655
0
            route_data_destroy_nexthops__(&change->rd);
656
657
0
            NL_NESTED_FOR_EACH (nla, left, attrs[RTA_MULTIPATH]) {
658
0
                struct route_table_msg mp_change;
659
0
                struct rtnexthop *mp_rtnh;
660
0
                struct ofpbuf mp_buf;
661
662
0
                ofpbuf_use_const(&mp_buf, nla, nla->nla_len);
663
0
                mp_rtnh = ofpbuf_try_pull(&mp_buf, sizeof *mp_rtnh);
664
665
0
                if (!mp_rtnh) {
666
0
                    VLOG_DBG_RL(&rl, "got short message while parsing "
667
0
                                     "multipath attribute");
668
0
                    goto error_out;
669
0
                }
670
671
0
                if (!route_table_parse__(&mp_buf, 0, nlmsg, rtm, mp_rtnh,
672
0
                                         &mp_change)) {
673
0
                    goto error_out;
674
0
                }
675
0
                ovs_list_push_back_all(&change->rd.nexthops,
676
0
                                       &mp_change.rd.nexthops);
677
0
            }
678
0
        }
679
0
        if (route_type_needs_nexthop(rtm->rtm_type)
680
0
            && !attrs[RTA_OIF] && !attrs[RTA_GATEWAY]
681
0
            && !attrs[RTA_VIA] && !attrs[RTA_MULTIPATH]) {
682
0
            if (!attrs[RTA_NH_ID]) {
683
0
                VLOG_DBG_RL(&rl, "route message needs an RTA_OIF, "
684
0
                                 "RTA_GATEWAY, RTA_VIA, RTA_MULTIPATH or "
685
0
                                 "RTA_NH_ID attribute");
686
0
                goto error_out;
687
0
            }
688
689
            /* The next hop is in the nexthop object named by RTA_NH_ID, so
690
             * the entry added at the start of parsing was never filled in. */
691
0
            route_data_destroy_nexthops__(&change->rd);
692
0
        }
693
        /* Add any additional RTA attribute processing before RTA_MULTIPATH. */
694
695
        /* Ensure that the change->rd->nexthops list is cleared in cases when
696
         * the route does not need a next hop. */
697
0
        if (!route_type_needs_nexthop(rtm->rtm_type)) {
698
0
            route_data_destroy_nexthops__(&change->rd);
699
0
        }
700
0
    } else {
701
0
        VLOG_DBG_RL(&rl, "received unparseable rtnetlink route message");
702
0
        goto error_out;
703
0
    }
704
705
    /* Success. */
706
0
    return ipv4 ? RTNLGRP_IPV4_ROUTE : RTNLGRP_IPV6_ROUTE;
707
708
0
error_out:
709
0
    route_data_destroy(&change->rd);
710
0
    return 0;
711
0
}
712
713
/* Parse Netlink message in buf, which is expected to contain a UAPI rtmsg
714
 * header and associated route attributes.
715
 *
716
 * Return RTNLGRP_IPV4_ROUTE or RTNLGRP_IPV6_ROUTE on success, and 0 on a parse
717
 * error.
718
 *
719
 * On success, memory may have been allocated, and it is the caller's
720
 * responsibility to free it with a call to route_data_destroy().
721
 *
722
 * In case of error, any allocated memory will be freed before returning. */
723
int
724
route_table_parse(struct ofpbuf *buf, void *change)
725
0
{
726
0
    struct nlmsghdr *nlmsg;
727
0
    struct rtmsg *rtm;
728
729
0
    nlmsg = ofpbuf_at(buf, 0, NLMSG_HDRLEN);
730
0
    rtm = ofpbuf_at(buf, NLMSG_HDRLEN, sizeof *rtm);
731
732
0
    if (!nlmsg || !rtm) {
733
0
        return 0;
734
0
    }
735
736
0
    return route_table_parse__(buf, NLMSG_HDRLEN + sizeof *rtm,
737
0
                               nlmsg, rtm, NULL, change);
738
0
}
739
740
static void
741
route_table_change(struct route_table_msg *change, void *aux OVS_UNUSED)
742
0
{
743
0
    if (!change
744
0
        || (change->relevant
745
0
            && ovs_router_is_referenced(change->rd.rta_table_id))) {
746
0
        route_table_valid = false;
747
0
    }
748
0
    if (change) {
749
0
        route_data_destroy(&change->rd);
750
0
    }
751
0
}
752
753
static void
754
route_table_handle_msg(const struct route_table_msg *change,
755
                       void *aux OVS_UNUSED, uint32_t table)
756
0
{
757
0
    if (change->relevant && change->nlmsg_type == RTM_NEWROUTE
758
0
            && !ovs_list_is_empty(&change->rd.nexthops)) {
759
0
        const struct route_data *rd = &change->rd;
760
0
        const struct route_data_nexthop *rdnh;
761
762
        /* The ovs-router module currently does not implement lookup or
763
         * storage for routes with multiple next hops.  For backwards
764
         * compatibility, we use the first next hop. */
765
0
        rdnh = CONTAINER_OF(ovs_list_front(&change->rd.nexthops),
766
0
                            const struct route_data_nexthop, nexthop_node);
767
768
0
        ovs_router_insert(table, rd->rta_mark, &rd->rta_dst,
769
0
                          IN6_IS_ADDR_V4MAPPED(&rd->rta_dst)
770
0
                          ? rd->rtm_dst_len + 96 : rd->rtm_dst_len,
771
0
                          false, rdnh->ifname, &rdnh->addr, &rd->rta_prefsrc);
772
0
    }
773
0
}
774
775
static void
776
rules_change(const struct route_table_msg *change OVS_UNUSED,
777
             void *aux OVS_UNUSED)
778
0
{
779
0
    if (!change || change->relevant) {
780
0
        rules_valid = false;
781
0
    }
782
0
}
783
784
static void
785
route_map_clear(void)
786
0
{
787
0
    ovs_router_rules_flush(false);
788
0
    ovs_router_flush(false);
789
0
}
790
791
bool
792
route_table_fallback_lookup(const struct in6_addr *ip6_dst OVS_UNUSED,
793
                            char name[] OVS_UNUSED,
794
                            struct in6_addr *gw6)
795
0
{
796
0
    *gw6 = in6addr_any;
797
0
    return false;
798
0
}
799
800

801
/* name_table . */
802
803
static void
804
name_table_init(void)
805
0
{
806
0
    name_notifier = rtnetlink_notifier_create(name_table_change, NULL);
807
0
}
808
809
810
static void
811
name_table_change(const struct rtnetlink_change *change,
812
                  void *aux OVS_UNUSED)
813
0
{
814
0
    if (change && change->irrelevant) {
815
0
        return;
816
0
    }
817
818
    /* Changes to interface status can cause routing table changes that some
819
     * versions of the linux kernel do not advertise for some reason. */
820
0
    route_table_valid = false;
821
822
0
    if (change && change->nlmsg_type == RTM_DELLINK) {
823
0
        if (change->ifname) {
824
0
            tnl_port_map_delete_ipdev(change->ifname);
825
0
        }
826
0
    }
827
0
}