2 * netconf_netlink.c - netconf interaction with the kernel using
4 * Copyright (C) 2021 Nvidia, Inc.
7 * This file is part of FRR.
9 * FRR is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
14 * FRR is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with FRR; see the file COPYING. If not, write to the Free
21 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 #ifdef HAVE_NETLINK /* Netlink OSes only */
30 #include "linux/netconf.h"
32 #include "lib/lib_errors.h"
33 #include "zebra/zebra_ns.h"
34 #include "zebra/zebra_dplane.h"
35 #include "zebra/kernel_netlink.h"
36 #include "zebra/netconf_netlink.h"
37 #include "zebra/debug.h"
39 static struct rtattr
*netconf_rta(struct netconfmsg
*ncm
)
41 return (struct rtattr
*)((char *)ncm
+
42 NLMSG_ALIGN(sizeof(struct netconfmsg
)));
46 * Handle netconf update about a single interface: create dplane
47 * context, and enqueue for processing in the main zebra pthread.
50 netlink_netconf_dplane_update(ns_id_t ns_id
, afi_t afi
, ifindex_t ifindex
,
51 enum dplane_netconf_status_e mpls_on
,
52 enum dplane_netconf_status_e mcast_on
,
53 enum dplane_netconf_status_e linkdown_on
)
55 struct zebra_dplane_ctx
*ctx
;
57 ctx
= dplane_ctx_alloc();
58 dplane_ctx_set_op(ctx
, DPLANE_OP_INTF_NETCONFIG
);
59 dplane_ctx_set_ns_id(ctx
, ns_id
);
60 dplane_ctx_set_afi(ctx
, afi
);
61 dplane_ctx_set_ifindex(ctx
, ifindex
);
63 dplane_ctx_set_netconf_mpls(ctx
, mpls_on
);
64 dplane_ctx_set_netconf_mcast(ctx
, mcast_on
);
65 dplane_ctx_set_netconf_linkdown(ctx
, linkdown_on
);
67 /* Enqueue ctx for main pthread to process */
68 dplane_provider_enqueue_to_zebra(ctx
);
74 * Parse and process an incoming netlink netconf update.
76 int netlink_netconf_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
78 struct netconfmsg
*ncm
;
79 struct rtattr
*tb
[NETCONFA_MAX
+ 1] = {};
84 enum dplane_netconf_status_e mpls_on
= DPLANE_NETCONF_STATUS_UNKNOWN
;
85 enum dplane_netconf_status_e mcast_on
= DPLANE_NETCONF_STATUS_UNKNOWN
;
86 enum dplane_netconf_status_e linkdown_on
=
87 DPLANE_NETCONF_STATUS_UNKNOWN
;
89 if (h
->nlmsg_type
!= RTM_NEWNETCONF
&& h
->nlmsg_type
!= RTM_DELNETCONF
)
92 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct netconfmsg
));
94 zlog_err("%s: Message received from netlink is of a broken size: %d, min %zu",
95 __func__
, h
->nlmsg_len
,
96 (size_t)NLMSG_LENGTH(sizeof(struct netconfmsg
)));
103 * FRR does not have an internal representation of afi_t for
104 * the MPLS Address Family that the kernel has. So let's
105 * just call it v4. This is ok because the kernel appears
106 * to do a good job of not sending data that is mixed/matched
110 if (ncm
->ncm_family
== AF_MPLS
)
114 afi
= family2afi(ncm
->ncm_family
);
116 netlink_parse_rtattr(tb
, NETCONFA_MAX
, netconf_rta(ncm
), len
);
118 if (!tb
[NETCONFA_IFINDEX
]) {
119 zlog_err("NETCONF message received from netlink without an ifindex");
123 ifindex
= *(ifindex_t
*)RTA_DATA(tb
[NETCONFA_IFINDEX
]);
125 if (tb
[NETCONFA_INPUT
]) {
126 ival
= *(uint32_t *)RTA_DATA(tb
[NETCONFA_INPUT
]);
128 mpls_on
= DPLANE_NETCONF_STATUS_ENABLED
;
130 mpls_on
= DPLANE_NETCONF_STATUS_DISABLED
;
133 if (tb
[NETCONFA_MC_FORWARDING
]) {
134 ival
= *(uint32_t *)RTA_DATA(tb
[NETCONFA_MC_FORWARDING
]);
136 mcast_on
= DPLANE_NETCONF_STATUS_ENABLED
;
138 mcast_on
= DPLANE_NETCONF_STATUS_DISABLED
;
141 if (tb
[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
]) {
142 ival
= *(uint32_t *)RTA_DATA(
143 tb
[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
]);
145 linkdown_on
= DPLANE_NETCONF_STATUS_ENABLED
;
147 linkdown_on
= DPLANE_NETCONF_STATUS_DISABLED
;
150 if (IS_ZEBRA_DEBUG_KERNEL
)
152 "%s: interface %u is mpls on: %d multicast on: %d linkdown: %d",
153 __func__
, ifindex
, mpls_on
, mcast_on
, linkdown_on
);
155 /* Create a dplane context and pass it along for processing */
156 netlink_netconf_dplane_update(ns_id
, afi
, ifindex
, mpls_on
, mcast_on
,
163 * Request info from the host OS. This only sends the request; any replies
164 * are processed asynchronously.
166 int netlink_request_netconf(int sockfd
)
171 struct netconfmsg ncm
;
175 nls
= kernel_netlink_nlsock_lookup(sockfd
);
177 if (IS_ZEBRA_DEBUG_KERNEL
)
178 zlog_debug("%s: nlsock %s", __func__
, nls
? nls
->name
: "NULL");
183 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct netconfmsg
));
184 req
.n
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
;
185 req
.n
.nlmsg_type
= RTM_GETNETCONF
;
186 req
.ncm
.ncm_family
= AF_UNSPEC
;
188 return netlink_request(nls
, &req
);
191 extern struct zebra_privs_t zserv_privs
;
193 * Currently netconf has no ability to set from netlink.
194 * So we've received a request to do this work in the data plane.
195 * as such we need to set the value via the /proc system
197 enum netlink_msg_status
netlink_put_intf_netconfig(struct nl_batch
*bth
,
198 struct zebra_dplane_ctx
*ctx
)
200 const char *ifname
= dplane_ctx_get_ifname(ctx
);
201 enum dplane_netconf_status_e mpls_on
= dplane_ctx_get_netconf_mpls(ctx
);
203 char mpls_proc
[PATH_MAX
];
204 int fd
, ret
= FRR_NETLINK_ERROR
;
206 snprintf(mpls_proc
, sizeof(mpls_proc
),
207 "/proc/sys/net/mpls/conf/%s/input", ifname
);
209 if (mpls_on
== DPLANE_NETCONF_STATUS_ENABLED
)
210 snprintf(set
, sizeof(set
), "1\n");
211 else if (mpls_on
== DPLANE_NETCONF_STATUS_DISABLED
)
212 snprintf(set
, sizeof(set
), "0\n");
216 "%s: Expected interface %s to be set to ENABLED or DISABLED was %d",
217 __func__
, ifname
, mpls_on
);
221 frr_with_privs (&zserv_privs
) {
222 fd
= open(mpls_proc
, O_WRONLY
);
226 "%s: Unable to open %s for writing: %s(%d)",
227 __func__
, mpls_proc
, safe_strerror(errno
),
231 if (write(fd
, set
, 2) == 2)
232 ret
= FRR_NETLINK_SUCCESS
;
234 flog_err_sys(EC_LIB_SOCKET
,
235 "%s: Unsuccessful write to %s: %s(%d)",
236 __func__
, mpls_proc
, safe_strerror(errno
),
243 #endif /* HAVE_NETLINK */