2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "opt_inet6.h"
32 #include <sys/types.h>
33 #include <sys/eventhandler.h>
34 #include <sys/malloc.h>
35 #include <sys/socket.h>
36 #include <sys/syslog.h>
39 #include <net/if_llatbl.h>
40 #include <netlink/netlink.h>
41 #include <netlink/netlink_ctl.h>
42 #include <netlink/netlink_route.h>
43 #include <netlink/route/route_var.h>
45 #include <netinet6/in6_var.h> /* nd6.h requires this */
46 #include <netinet6/nd6.h> /* nd6 state machine */
47 #include <netinet6/scope6_var.h> /* scope deembedding */
49 #define DEBUG_MOD_NAME nl_neigh
50 #define DEBUG_MAX_LEVEL LOG_DEBUG3
51 #include <netlink/netlink_debug.h>
52 _DECLARE_DEBUG(LOG_DEBUG);
54 static int lle_families[] = { AF_INET, AF_INET6 };
56 static eventhandler_tag lle_event_p;
58 struct netlink_walkargs {
70 lle_state_to_nl_state(int family, struct llentry *lle)
72 int state = lle->ln_state;
76 if (lle->la_flags & (LLE_STATIC | LLE_IFADDR))
79 case 0: /* ARP_LLINFO_INCOMPLETE */
80 return (NUD_INCOMPLETE);
81 case 1: /* ARP_LLINFO_REACHABLE */
82 return (NUD_REACHABLE);
83 case 2: /* ARP_LLINFO_VERIFY */
89 case ND6_LLINFO_INCOMPLETE:
90 return (NUD_INCOMPLETE);
91 case ND6_LLINFO_REACHABLE:
92 return (NUD_REACHABLE);
93 case ND6_LLINFO_STALE:
95 case ND6_LLINFO_DELAY:
97 case ND6_LLINFO_PROBE:
107 lle_flags_to_nl_flags(const struct llentry *lle)
109 uint32_t nl_flags = 0;
111 if (lle->la_flags & LLE_IFADDR)
112 nl_flags |= NTF_SELF;
113 if (lle->la_flags & LLE_PUB)
114 nl_flags |= NTF_PROXY;
115 if (lle->la_flags & LLE_STATIC)
116 nl_flags |= NTF_STICKY;
117 if (lle->ln_router != 0)
118 nl_flags |= NTF_ROUTER;
124 dump_lle_locked(struct llentry *lle, void *arg)
126 struct netlink_walkargs *wa = (struct netlink_walkargs *)arg;
127 struct nlmsghdr *hdr = &wa->hdr;
128 struct nl_writer *nw = wa->nw;
135 IF_DEBUG_LEVEL(LOG_DEBUG2) {
136 char llebuf[NHOP_PRINT_BUFSIZE];
137 llentry_print_buf_lltable(lle, llebuf, sizeof(llebuf));
138 NL_LOG(LOG_DEBUG2, "dumping %s", llebuf);
141 if (!nlmsg_reply(nw, hdr, sizeof(struct ndmsg)))
144 ndm = nlmsg_reserve_object(nw, struct ndmsg);
145 ndm->ndm_family = wa->family;
146 ndm->ndm_ifindex = wa->ifp->if_index;
147 ndm->ndm_state = lle_state_to_nl_state(wa->family, lle);
148 ndm->ndm_flags = lle_flags_to_nl_flags(lle);
150 switch (wa->family) {
153 addr.in = lle->r_l3addr.addr4;
154 nlattr_add(nw, NDA_DST, 4, &addr);
159 addr.in6 = lle->r_l3addr.addr6;
160 in6_clearscope(&addr.in6);
161 nlattr_add(nw, NDA_DST, 16, &addr);
166 if (lle->r_flags & RLLE_VALID) {
168 int addrlen = wa->ifp->if_addrlen;
169 nlattr_add(nw, NDA_LLADDR, addrlen, lle->ll_addr);
172 nlattr_add_u32(nw, NDA_PROBES, lle->la_asked);
174 struct nda_cacheinfo *cache;
175 cache = nlmsg_reserve_attr(nw, NDA_CACHEINFO, struct nda_cacheinfo);
178 /* TODO: provide confirmed/updated */
179 cache->ndm_refcnt = lle->lle_refcnt;
184 NL_LOG(LOG_DEBUG, "unable to dump lle state (ENOMEM)");
190 dump_lle(struct lltable *llt, struct llentry *lle, void *arg)
195 error = dump_lle_locked(lle, arg);
201 dump_llt(struct lltable *llt, struct netlink_walkargs *wa)
203 lltable_foreach_lle(llt, dump_lle, wa);
209 dump_llts_iface(struct netlink_walkargs *wa, struct ifnet *ifp, int family)
214 for (int i = 0; i < sizeof(lle_families) / sizeof(int); i++) {
215 int fam = lle_families[i];
216 struct lltable *llt = lltable_get(ifp, fam);
217 if (llt != NULL && (family == 0 || family == fam)) {
220 if (!dump_llt(llt, wa)) {
231 dump_llts(struct netlink_walkargs *wa, struct ifnet *ifp, int family)
233 NL_LOG(LOG_DEBUG, "Start dump ifp=%s family=%d", ifp ? if_name(ifp) : "NULL", family);
235 wa->hdr.nlmsg_flags |= NLM_F_MULTI;
238 dump_llts_iface(wa, ifp, family);
240 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
241 dump_llts_iface(wa, ifp, family);
245 NL_LOG(LOG_DEBUG, "End dump, iterated %d dumped %d", wa->count, wa->dumped);
247 if (!nlmsg_end_dump(wa->nw, wa->error, &wa->hdr)) {
248 NL_LOG(LOG_DEBUG, "Unable to add new message");
256 get_lle(struct netlink_walkargs *wa, struct ifnet *ifp, int family, struct sockaddr *dst)
258 struct lltable *llt = lltable_get(ifp, family);
263 if (dst->sa_family == AF_INET6) {
264 struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst;
266 if (IN6_IS_SCOPE_LINKLOCAL(&dst6->sin6_addr))
267 in6_set_unicast_scopeid(&dst6->sin6_addr, ifp->if_index);
270 struct llentry *lle = lla_lookup(llt, LLE_UNLOCKED, dst);
277 return (dump_lle(llt, lle, wa));
280 struct nl_parsed_neigh {
281 struct sockaddr *nda_dst;
282 struct ifnet *nda_ifp;
283 struct nlattr *nda_lladdr;
289 #define _IN(_field) offsetof(struct ndmsg, _field)
290 #define _OUT(_field) offsetof(struct nl_parsed_neigh, _field)
291 static struct nlfield_parser nlf_p_neigh[] = {
292 { .off_in = _IN(ndm_family), .off_out = _OUT(ndm_family), .cb = nlf_get_u8 },
293 { .off_in = _IN(ndm_flags), .off_out = _OUT(ndm_flags), .cb = nlf_get_u8_u32 },
294 { .off_in = _IN(ndm_state), .off_out = _OUT(ndm_state), .cb = nlf_get_u16 },
295 { .off_in = _IN(ndm_ifindex), .off_out = _OUT(nda_ifp), .cb = nlf_get_ifpz },
298 static struct nlattr_parser nla_p_neigh[] = {
299 { .type = NDA_DST, .off = _OUT(nda_dst), .cb = nlattr_get_ip },
300 { .type = NDA_LLADDR, .off = _OUT(nda_lladdr), .cb = nlattr_get_nla },
301 { .type = NDA_IFINDEX, .off = _OUT(nda_ifp), .cb = nlattr_get_ifp },
302 { .type = NDA_FLAGS_EXT, .off = _OUT(ndm_flags), .cb = nlattr_get_uint32 },
306 NL_DECLARE_PARSER(ndmsg_parser, struct ndmsg, nlf_p_neigh, nla_p_neigh);
310 * type=RTM_NEWNEIGH, flags=NLM_F_REQUEST|NLM_F_ACK|NLM_F_EXCL|NLM_F_CREATE, seq=1661941473, pid=0},
311 * {ndm_family=AF_INET6, ndm_ifindex=if_nametoindex("enp0s31f6"), ndm_state=NUD_PERMANENT, ndm_flags=0, ndm_type=RTN_UNSPEC},
313 * {{nla_len=20, nla_type=NDA_DST}, inet_pton(AF_INET6, "2a01:4f8:13a:70c::3")},
314 * {{nla_len=10, nla_type=NDA_LLADDR}, 20:4e:71:62:ae:f2}]}, iov_len=60}
318 rtnl_handle_newneigh(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt)
322 struct nl_parsed_neigh attrs = {};
323 error = nl_parse_nlmsg(hdr, &ndmsg_parser, npt, &attrs);
327 if (attrs.nda_ifp == NULL || attrs.nda_dst == NULL || attrs.nda_lladdr == NULL) {
328 if (attrs.nda_ifp == NULL)
329 NLMSG_REPORT_ERR_MSG(npt, "NDA_IFINDEX / ndm_ifindex not set");
330 if (attrs.nda_dst == NULL)
331 NLMSG_REPORT_ERR_MSG(npt, "NDA_DST not set");
332 if (attrs.nda_lladdr == NULL)
333 NLMSG_REPORT_ERR_MSG(npt, "NDA_LLADDR not set");
337 if (attrs.nda_dst->sa_family != attrs.ndm_family) {
338 NLMSG_REPORT_ERR_MSG(npt,
339 "NDA_DST family (%d) is different from ndm_family (%d)",
340 attrs.nda_dst->sa_family, attrs.ndm_family);
344 int addrlen = attrs.nda_ifp->if_addrlen;
345 if (attrs.nda_lladdr->nla_len != sizeof(struct nlattr) + addrlen) {
346 NLMSG_REPORT_ERR_MSG(npt,
347 "NDA_LLADDR address length (%d) is different from expected (%d)",
348 (int)attrs.nda_lladdr->nla_len - (int)sizeof(struct nlattr), addrlen);
352 if (attrs.ndm_state != NUD_PERMANENT) {
353 NLMSG_REPORT_ERR_MSG(npt, "ndm_state %d not supported", attrs.ndm_state);
357 const uint16_t supported_flags = NTF_PROXY | NTF_STICKY;
358 if ((attrs.ndm_flags & supported_flags) != attrs.ndm_flags) {
359 NLMSG_REPORT_ERR_MSG(npt, "ndm_flags %X not supported",
360 attrs.ndm_flags &~ supported_flags);
364 /* Replacement requires new entry creation anyway */
365 if ((hdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_REPLACE)) == 0)
368 struct lltable *llt = lltable_get(attrs.nda_ifp, attrs.ndm_family);
370 return (EAFNOSUPPORT);
373 uint8_t linkhdr[LLE_MAX_LINKHDR];
374 size_t linkhdrsize = sizeof(linkhdr);
376 if (lltable_calc_llheader(attrs.nda_ifp, attrs.ndm_family,
377 (char *)(attrs.nda_lladdr + 1), linkhdr, &linkhdrsize, &lladdr_off) != 0) {
378 NLMSG_REPORT_ERR_MSG(npt, "unable to calculate lle prepend data");
382 int lle_flags = LLE_STATIC | ((attrs.ndm_flags & NTF_PROXY) ? LLE_PUB : 0);
383 struct llentry *lle = lltable_alloc_entry(llt, lle_flags, attrs.nda_dst);
386 lltable_set_entry_addr(attrs.nda_ifp, lle, linkhdr, linkhdrsize, lladdr_off);
388 /* llentry created, try to insert or update :*/
389 IF_AFDATA_WLOCK(attrs.nda_ifp);
391 struct llentry *lle_tmp = lla_lookup(llt, LLE_EXCLUSIVE, attrs.nda_dst);
392 if (lle_tmp != NULL) {
393 if (hdr->nlmsg_flags & NLM_F_EXCL) {
394 LLE_WUNLOCK(lle_tmp);
397 } else if (hdr->nlmsg_flags & NLM_F_REPLACE) {
398 lltable_unlink_entry(llt, lle_tmp);
399 lltable_link_entry(llt, lle);
403 if (hdr->nlmsg_flags & NLM_F_CREATE)
404 lltable_link_entry(llt, lle);
408 IF_AFDATA_WUNLOCK(attrs.nda_ifp);
417 llentry_free(lle_tmp);
419 /* XXX: We're inside epoch */
420 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_RESOLVED);
427 rtnl_handle_delneigh(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt)
431 struct nl_parsed_neigh attrs = {};
432 error = nl_parse_nlmsg(hdr, &ndmsg_parser, npt, &attrs);
436 if (attrs.nda_dst == NULL) {
437 NLMSG_REPORT_ERR_MSG(npt, "NDA_DST not set");
441 if (attrs.nda_ifp == NULL) {
442 NLMSG_REPORT_ERR_MSG(npt, "no ifindex provided");
446 struct lltable *llt = lltable_get(attrs.nda_ifp, attrs.ndm_family);
448 return (EAFNOSUPPORT);
450 IF_AFDATA_WLOCK(attrs.nda_ifp);
451 struct llentry *lle = lla_lookup(llt, LLE_EXCLUSIVE, attrs.nda_dst);
453 if ((lle->la_flags & LLE_IFADDR) != 0) {
458 lltable_unlink_entry(llt, lle);
461 IF_AFDATA_WUNLOCK(attrs.nda_ifp);
463 if (error == 0 && lle != NULL)
464 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
473 rtnl_handle_getneigh(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt)
477 struct nl_parsed_neigh attrs = {};
478 error = nl_parse_nlmsg(hdr, &ndmsg_parser, npt, &attrs);
482 if (attrs.nda_dst != NULL && attrs.nda_ifp == NULL) {
483 NLMSG_REPORT_ERR_MSG(npt, "has NDA_DST but no ifindex provided");
487 struct netlink_walkargs wa = {
490 .hdr.nlmsg_pid = hdr->nlmsg_pid,
491 .hdr.nlmsg_seq = hdr->nlmsg_seq,
492 .hdr.nlmsg_flags = hdr->nlmsg_flags,
493 .hdr.nlmsg_type = NL_RTM_NEWNEIGH,
496 if (attrs.nda_dst == NULL)
497 error = dump_llts(&wa, attrs.nda_ifp, attrs.ndm_family);
499 error = get_lle(&wa, attrs.nda_ifp, attrs.ndm_family, attrs.nda_dst);
504 static const struct rtnl_cmd_handler cmd_handlers[] = {
506 .cmd = NL_RTM_NEWNEIGH,
507 .name = "RTM_NEWNEIGH",
508 .cb = &rtnl_handle_newneigh,
511 .cmd = NL_RTM_DELNEIGH,
512 .name = "RTM_DELNEIGH",
513 .cb = &rtnl_handle_delneigh,
514 .priv = PRIV_NET_ROUTE,
517 .cmd = NL_RTM_GETNEIGH,
518 .name = "RTM_GETNEIGH",
519 .cb = &rtnl_handle_getneigh,
520 .priv = PRIV_NET_ROUTE,
525 rtnl_lle_event(void *arg __unused, struct llentry *lle, int evt)
530 LLE_WLOCK_ASSERT(lle);
532 ifp = lltable_get_ifp(lle->lle_tbl);
533 family = lltable_get_af(lle->lle_tbl);
535 if (family != AF_INET && family != AF_INET6)
538 int nlmsgs_type = evt == LLENTRY_RESOLVED ? NL_RTM_NEWNEIGH : NL_RTM_DELNEIGH;
540 struct nl_writer nw = {};
541 if (!nlmsg_get_group_writer(&nw, NLMSG_SMALL, NETLINK_ROUTE, RTNLGRP_NEIGH)) {
542 NL_LOG(LOG_DEBUG, "error allocating group writer");
546 struct netlink_walkargs wa = {
547 .hdr.nlmsg_type = nlmsgs_type,
553 dump_lle_locked(lle, &wa);
557 static const struct nlhdr_parser *all_parsers[] = { &ndmsg_parser };
562 NL_VERIFY_PARSERS(all_parsers);
563 rtnl_register_messages(cmd_handlers, NL_ARRAY_LEN(cmd_handlers));
564 lle_event_p = EVENTHANDLER_REGISTER(lle_event, rtnl_lle_event, NULL,
565 EVENTHANDLER_PRI_ANY);
569 rtnl_neighs_destroy()
571 EVENTHANDLER_DEREGISTER(lle_event, lle_event_p);