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
|
/*
* netconf_netlink.c - netconf interaction with the kernel using
* netlink
* Copyright (C) 2021 Nvidia, Inc.
* Donald Sharp
*
* This file is part of FRR.
*
* FRR is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* FRR 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 FRR; see the file COPYING. If not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#include <zebra.h>
#ifdef HAVE_NETLINK /* Netlink OSes only */
#include <ns.h>
#include "linux/netconf.h"
#include "zebra/zebra_ns.h"
#include "zebra/zebra_dplane.h"
#include "zebra/kernel_netlink.h"
#include "zebra/netconf_netlink.h"
#include "zebra/debug.h"
static struct rtattr *netconf_rta(struct netconfmsg *ncm)
{
return (struct rtattr *)((char *)ncm
+ NLMSG_ALIGN(sizeof(struct netconfmsg)));
}
int netlink_netconf_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
{
struct netconfmsg *ncm;
struct rtattr *tb[NETCONFA_MAX + 1] = {};
int len;
ifindex_t ifindex;
bool mpls_on = false;
bool mc_on = false;
if (h->nlmsg_type != RTM_NEWNETCONF && h->nlmsg_type != RTM_DELNETCONF)
return 0;
len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct netconfmsg));
if (len < 0) {
zlog_err("%s: Message received from netlink is of a broken size: %d %zu",
__func__, h->nlmsg_len,
(size_t)NLMSG_LENGTH(sizeof(struct netconfmsg)));
return -1;
}
ncm = NLMSG_DATA(h);
netlink_parse_rtattr(tb, NETCONFA_MAX, netconf_rta(ncm), len);
if (!tb[NETCONFA_IFINDEX]) {
zlog_err("%s: Message received from netlink that we expected to receive an interface on",
__func__);
return 0;
}
ifindex = *(ifindex_t *)RTA_DATA(tb[NETCONFA_IFINDEX]);
switch (ifindex) {
case NETCONFA_IFINDEX_ALL:
case NETCONFA_IFINDEX_DEFAULT:
/*
* We need the ability to handle netlink messages intended
* for all and default interfaces. I am not 100% sure
* what that is yet, or where we would store it.
*/
return 0;
default:
break;
}
if (tb[NETCONFA_INPUT]) {
mpls_on = *(bool *)RTA_DATA(tb[NETCONFA_INPUT]);
/* Create a context and pass it up for processing */
}
if (tb[NETCONFA_MC_FORWARDING]) {
mc_on = *(bool *)RTA_DATA(tb[NETCONFA_MC_FORWARDING]);
/* Create a context and pass it up for processing */
}
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("Interface %u is mpls on: %d multicast on: %d",
ifindex, mpls_on, mc_on);
return 0;
}
static int netlink_request_netconf(struct nlsock *netlink_cmd)
{
struct {
struct nlmsghdr n;
struct netconfmsg ncm;
char buf[1024];
} req;
memset(&req, 0, sizeof(req));
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct netconfmsg));
req.n.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
req.n.nlmsg_type = RTM_GETNETCONF;
req.ncm.ncm_family = AF_UNSPEC;
return netlink_request(netlink_cmd, &req);
}
int netconf_lookup_netlink(struct zebra_ns *zns)
{
int ret;
struct zebra_dplane_info dp_info;
struct nlsock *netlink_cmd = &zns->netlink_cmd;
zebra_dplane_info_from_zns(&dp_info, zns, true);
ret = netlink_request_netconf(netlink_cmd);
if (ret < 0)
return ret;
ret = netlink_parse_info(netlink_netconf_change, netlink_cmd, &dp_info,
0, 1);
return ret;
}
#endif /* HAVE_NETLINK */
|