1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 #include "opt_inet.h"
30 #include "opt_inet6.h"
31 #include <sys/types.h>
32 #include <sys/eventhandler.h>
33 #include <sys/kernel.h>
34 #include <sys/jail.h>
35 #include <sys/malloc.h>
36 #include <sys/socket.h>
37 #include <sys/sockio.h>
38 #include <sys/syslog.h>
39
40 #include <net/if.h>
41 #include <net/if_dl.h>
42 #include <net/if_media.h>
43 #include <net/if_var.h>
44 #include <net/if_clone.h>
45 #include <net/route.h>
46 #include <net/route/nhop.h>
47 #include <net/route/route_ctl.h>
48 #include <netinet/in_var.h>
49 #include <netinet6/in6_var.h>
50 #include <netinet6/scope6_var.h> /* scope deembedding */
51 #include <netlink/netlink.h>
52 #include <netlink/netlink_ctl.h>
53 #include <netlink/netlink_route.h>
54 #include <netlink/route/route_var.h>
55
56 #define DEBUG_MOD_NAME nl_iface
57 #define DEBUG_MAX_LEVEL LOG_DEBUG3
58 #include <netlink/netlink_debug.h>
59 _DECLARE_DEBUG(LOG_INFO);
60
61 struct netlink_walkargs {
62 struct nl_writer *nw;
63 struct nlmsghdr hdr;
64 struct nlpcb *so;
65 struct ucred *cred;
66 uint32_t fibnum;
67 int family;
68 int error;
69 int count;
70 int dumped;
71 };
72
73 static eventhandler_tag ifdetach_event, ifattach_event, iflink_event, ifaddr_event;
74
75 static SLIST_HEAD(, nl_cloner) nl_cloners = SLIST_HEAD_INITIALIZER(nl_cloners);
76
77 static struct sx rtnl_cloner_lock;
78 SX_SYSINIT(rtnl_cloner_lock, &rtnl_cloner_lock, "rtnl cloner lock");
79
80 /* These are external hooks for CARP. */
81 extern int (*carp_get_vhid_p)(struct ifaddr *);
82
83 /*
84 * RTM_GETLINK request
85 * sendto(3, {{len=32, type=RTM_GETLINK, flags=NLM_F_REQUEST|NLM_F_DUMP, seq=1641940952, pid=0},
86 * {ifi_family=AF_INET, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0}}, 32, 0, NULL, 0) = 32
87 *
88 * Reply:
89 * {ifi_family=AF_UNSPEC, ifi_type=ARPHRD_ETHER, ifi_index=if_nametoindex("enp0s31f6"), ifi_flags=IFF_UP|IFF_BROADCAST|IFF_RUNNING|IFF_MULTICAST|IFF_LOWER_UP, ifi_change=0},
90 {{nla_len=10, nla_type=IFLA_ADDRESS}, "\xfe\x54\x00\x52\x3e\x90"}
91
92 [
93 {{nla_len=14, nla_type=IFLA_IFNAME}, "enp0s31f6"},
94 {{nla_len=8, nla_type=IFLA_TXQLEN}, 1000},
95 {{nla_len=5, nla_type=IFLA_OPERSTATE}, 6},
96 {{nla_len=5, nla_type=IFLA_LINKMODE}, 0},
97 {{nla_len=8, nla_type=IFLA_MTU}, 1500},
98 {{nla_len=8, nla_type=IFLA_MIN_MTU}, 68},
99 {{nla_len=8, nla_type=IFLA_MAX_MTU}, 9000},
100 {{nla_len=8, nla_type=IFLA_GROUP}, 0},
101 {{nla_len=8, nla_type=IFLA_PROMISCUITY}, 0},
102 {{nla_len=8, nla_type=IFLA_NUM_TX_QUEUES}, 1},
103 {{nla_len=8, nla_type=IFLA_GSO_MAX_SEGS}, 65535},
104 {{nla_len=8, nla_type=IFLA_GSO_MAX_SIZE}, 65536},
105 {{nla_len=8, nla_type=IFLA_NUM_RX_QUEUES}, 1},
106 {{nla_len=5, nla_type=IFLA_CARRIER}, 1},
107 {{nla_len=13, nla_type=IFLA_QDISC}, "fq_codel"},
108 {{nla_len=8, nla_type=IFLA_CARRIER_CHANGES}, 2},
109 {{nla_len=5, nla_type=IFLA_PROTO_DOWN}, 0},
110 {{nla_len=8, nla_type=IFLA_CARRIER_UP_COUNT}, 1},
111 {{nla_len=8, nla_type=IFLA_CARRIER_DOWN_COUNT}, 1},
112 */
113
114 struct if_state {
115 uint8_t ifla_operstate;
116 uint8_t ifla_carrier;
117 };
118
119 static void
get_operstate_ether(if_t ifp,struct if_state * pstate)120 get_operstate_ether(if_t ifp, struct if_state *pstate)
121 {
122 struct ifmediareq ifmr = {};
123 int error;
124 error = if_ioctl(ifp, SIOCGIFMEDIA, (void *)&ifmr);
125
126 if (error != 0) {
127 NL_LOG(LOG_DEBUG, "error calling SIOCGIFMEDIA on %s: %d",
128 if_name(ifp), error);
129 return;
130 }
131
132 switch (IFM_TYPE(ifmr.ifm_active)) {
133 case IFM_ETHER:
134 if (ifmr.ifm_status & IFM_ACTIVE) {
135 pstate->ifla_carrier = 1;
136 if (if_getflags(ifp) & IFF_MONITOR)
137 pstate->ifla_operstate = IF_OPER_DORMANT;
138 else
139 pstate->ifla_operstate = IF_OPER_UP;
140 } else
141 pstate->ifla_operstate = IF_OPER_DOWN;
142 }
143 }
144
145 static bool
get_stats(struct nl_writer * nw,if_t ifp)146 get_stats(struct nl_writer *nw, if_t ifp)
147 {
148 struct rtnl_link_stats64 *stats;
149
150 int nla_len = sizeof(struct nlattr) + sizeof(*stats);
151 struct nlattr *nla = nlmsg_reserve_data(nw, nla_len, struct nlattr);
152 if (nla == NULL)
153 return (false);
154 nla->nla_type = IFLA_STATS64;
155 nla->nla_len = nla_len;
156 stats = (struct rtnl_link_stats64 *)(nla + 1);
157
158 stats->rx_packets = if_getcounter(ifp, IFCOUNTER_IPACKETS);
159 stats->tx_packets = if_getcounter(ifp, IFCOUNTER_OPACKETS);
160 stats->rx_bytes = if_getcounter(ifp, IFCOUNTER_IBYTES);
161 stats->tx_bytes = if_getcounter(ifp, IFCOUNTER_OBYTES);
162 stats->rx_errors = if_getcounter(ifp, IFCOUNTER_IERRORS);
163 stats->tx_errors = if_getcounter(ifp, IFCOUNTER_OERRORS);
164 stats->rx_dropped = if_getcounter(ifp, IFCOUNTER_IQDROPS);
165 stats->tx_dropped = if_getcounter(ifp, IFCOUNTER_OQDROPS);
166 stats->multicast = if_getcounter(ifp, IFCOUNTER_IMCASTS);
167 stats->rx_nohandler = if_getcounter(ifp, IFCOUNTER_NOPROTO);
168
169 return (true);
170 }
171
172 static void
get_operstate(if_t ifp,struct if_state * pstate)173 get_operstate(if_t ifp, struct if_state *pstate)
174 {
175 pstate->ifla_operstate = IF_OPER_UNKNOWN;
176 pstate->ifla_carrier = 0; /* no carrier */
177
178 switch (if_gettype(ifp)) {
179 case IFT_ETHER:
180 case IFT_L2VLAN:
181 get_operstate_ether(ifp, pstate);
182 break;
183 default:
184 /* Map admin state to the operstate */
185 if (if_getflags(ifp) & IFF_UP) {
186 pstate->ifla_operstate = IF_OPER_UP;
187 pstate->ifla_carrier = 1;
188 } else
189 pstate->ifla_operstate = IF_OPER_DOWN;
190 break;
191 }
192 }
193
194 static void
get_hwaddr(struct nl_writer * nw,if_t ifp)195 get_hwaddr(struct nl_writer *nw, if_t ifp)
196 {
197 struct ifreq ifr = {};
198
199 if (if_gethwaddr(ifp, &ifr) == 0) {
200 nlattr_add(nw, IFLAF_ORIG_HWADDR, if_getaddrlen(ifp),
201 ifr.ifr_addr.sa_data);
202 }
203 }
204
205 static unsigned
ifp_flags_to_netlink(const if_t ifp)206 ifp_flags_to_netlink(const if_t ifp)
207 {
208 return (if_getflags(ifp) | if_getdrvflags(ifp));
209 }
210
211 #define LLADDR_CONST(s) ((const void *)((s)->sdl_data + (s)->sdl_nlen))
212 static bool
dump_sa(struct nl_writer * nw,int attr,const struct sockaddr * sa)213 dump_sa(struct nl_writer *nw, int attr, const struct sockaddr *sa)
214 {
215 uint32_t addr_len = 0;
216 const void *addr_data = NULL;
217 #ifdef INET6
218 struct in6_addr addr6;
219 #endif
220
221 if (sa == NULL)
222 return (true);
223
224 switch (sa->sa_family) {
225 #ifdef INET
226 case AF_INET:
227 addr_len = sizeof(struct in_addr);
228 addr_data = &((const struct sockaddr_in *)sa)->sin_addr;
229 break;
230 #endif
231 #ifdef INET6
232 case AF_INET6:
233 in6_splitscope(&((const struct sockaddr_in6 *)sa)->sin6_addr, &addr6, &addr_len);
234 addr_len = sizeof(struct in6_addr);
235 addr_data = &addr6;
236 break;
237 #endif
238 case AF_LINK:
239 addr_len = ((const struct sockaddr_dl *)sa)->sdl_alen;
240 addr_data = LLADDR_CONST((const struct sockaddr_dl *)sa);
241 break;
242 case AF_UNSPEC:
243 /* Ignore empty SAs without warning */
244 return (true);
245 default:
246 NL_LOG(LOG_DEBUG2, "unsupported family: %d, skipping", sa->sa_family);
247 return (true);
248 }
249
250 return (nlattr_add(nw, attr, addr_len, addr_data));
251 }
252
253 static bool
dump_iface_caps(struct nl_writer * nw,struct ifnet * ifp)254 dump_iface_caps(struct nl_writer *nw, struct ifnet *ifp)
255 {
256 int off = nlattr_add_nested(nw, IFLAF_CAPS);
257 uint32_t active_caps[roundup2(IFCAP_B_SIZE, 32) / 32] = {};
258 uint32_t all_caps[roundup2(IFCAP_B_SIZE, 32) / 32] = {};
259
260 MPASS(sizeof(active_caps) >= 8);
261 MPASS(sizeof(all_caps) >= 8);
262
263 if (off == 0)
264 return (false);
265
266 active_caps[0] = (uint32_t)if_getcapabilities(ifp);
267 all_caps[0] = (uint32_t)if_getcapenable(ifp);
268 active_caps[1] = (uint32_t)if_getcapabilities2(ifp);
269 all_caps[1] = (uint32_t)if_getcapenable2(ifp);
270
271 nlattr_add_u32(nw, NLA_BITSET_SIZE, IFCAP_B_SIZE);
272 nlattr_add(nw, NLA_BITSET_MASK, sizeof(all_caps), all_caps);
273 nlattr_add(nw, NLA_BITSET_VALUE, sizeof(active_caps), active_caps);
274
275 nlattr_set_len(nw, off);
276
277 return (true);
278 }
279
280 /*
281 * Dumps interface state, properties and metrics.
282 * @nw: message writer
283 * @ifp: target interface
284 * @hdr: template header
285 * @if_flags_mask: changed if_[drv]_flags bitmask
286 *
287 * This function is called without epoch and MAY sleep.
288 */
289 static bool
dump_iface(struct nl_writer * nw,if_t ifp,const struct nlmsghdr * hdr,int if_flags_mask)290 dump_iface(struct nl_writer *nw, if_t ifp, const struct nlmsghdr *hdr,
291 int if_flags_mask)
292 {
293 struct epoch_tracker et;
294 struct ifinfomsg *ifinfo;
295
296 NL_LOG(LOG_DEBUG3, "dumping interface %s data", if_name(ifp));
297
298 if (!nlmsg_reply(nw, hdr, sizeof(struct ifinfomsg)))
299 goto enomem;
300
301 ifinfo = nlmsg_reserve_object(nw, struct ifinfomsg);
302 ifinfo->ifi_family = AF_UNSPEC;
303 ifinfo->__ifi_pad = 0;
304 ifinfo->ifi_type = if_gettype(ifp);
305 ifinfo->ifi_index = if_getindex(ifp);
306 ifinfo->ifi_flags = ifp_flags_to_netlink(ifp);
307 ifinfo->ifi_change = if_flags_mask;
308
309 struct if_state ifs = {};
310 get_operstate(ifp, &ifs);
311
312 if (ifs.ifla_operstate == IF_OPER_UP)
313 ifinfo->ifi_flags |= IFF_LOWER_UP;
314
315 nlattr_add_string(nw, IFLA_IFNAME, if_name(ifp));
316 nlattr_add_u8(nw, IFLA_OPERSTATE, ifs.ifla_operstate);
317 nlattr_add_u8(nw, IFLA_CARRIER, ifs.ifla_carrier);
318
319 /*
320 nlattr_add_u8(nw, IFLA_PROTO_DOWN, val);
321 nlattr_add_u8(nw, IFLA_LINKMODE, val);
322 */
323 if (if_getaddrlen(ifp) != 0) {
324 struct ifaddr *ifa;
325 struct ifa_iter it;
326
327 NET_EPOCH_ENTER(et);
328 ifa = ifa_iter_start(ifp, &it);
329 if (ifa != NULL)
330 dump_sa(nw, IFLA_ADDRESS, ifa->ifa_addr);
331 ifa_iter_finish(&it);
332 NET_EPOCH_EXIT(et);
333 }
334
335 if ((if_getbroadcastaddr(ifp) != NULL)) {
336 nlattr_add(nw, IFLA_BROADCAST, if_getaddrlen(ifp),
337 if_getbroadcastaddr(ifp));
338 }
339
340 nlattr_add_u32(nw, IFLA_MTU, if_getmtu(ifp));
341 /*
342 nlattr_add_u32(nw, IFLA_MIN_MTU, 60);
343 nlattr_add_u32(nw, IFLA_MAX_MTU, 9000);
344 nlattr_add_u32(nw, IFLA_GROUP, 0);
345 */
346
347 if (if_getdescr(ifp) != NULL)
348 nlattr_add_string(nw, IFLA_IFALIAS, if_getdescr(ifp));
349
350 /* Store FreeBSD-specific attributes */
351 int off = nlattr_add_nested(nw, IFLA_FREEBSD);
352 if (off != 0) {
353 get_hwaddr(nw, ifp);
354 dump_iface_caps(nw, ifp);
355
356 nlattr_set_len(nw, off);
357 }
358
359 get_stats(nw, ifp);
360
361 uint32_t val = (if_getflags(ifp) & IFF_PROMISC) != 0;
362 nlattr_add_u32(nw, IFLA_PROMISCUITY, val);
363
364 ifc_dump_ifp_nl(ifp, nw);
365
366 if (nlmsg_end(nw))
367 return (true);
368
369 enomem:
370 NL_LOG(LOG_DEBUG, "unable to dump interface %s state (ENOMEM)", if_name(ifp));
371 nlmsg_abort(nw);
372 return (false);
373 }
374
375 static bool
check_ifmsg(void * hdr,struct nl_pstate * npt)376 check_ifmsg(void *hdr, struct nl_pstate *npt)
377 {
378 struct ifinfomsg *ifm = hdr;
379
380 if (ifm->__ifi_pad != 0 || ifm->ifi_type != 0 ||
381 ifm->ifi_flags != 0 || ifm->ifi_change != 0) {
382 nlmsg_report_err_msg(npt,
383 "strict checking: non-zero values in ifinfomsg header");
384 return (false);
385 }
386
387 return (true);
388 }
389
390 #define _IN(_field) offsetof(struct ifinfomsg, _field)
391 #define _OUT(_field) offsetof(struct nl_parsed_link, _field)
392 static const struct nlfield_parser nlf_p_if[] = {
393 { .off_in = _IN(ifi_type), .off_out = _OUT(ifi_type), .cb = nlf_get_u16 },
394 { .off_in = _IN(ifi_index), .off_out = _OUT(ifi_index), .cb = nlf_get_u32 },
395 { .off_in = _IN(ifi_flags), .off_out = _OUT(ifi_flags), .cb = nlf_get_u32 },
396 { .off_in = _IN(ifi_change), .off_out = _OUT(ifi_change), .cb = nlf_get_u32 },
397 };
398
399 static const struct nlattr_parser nla_p_linfo[] = {
400 { .type = IFLA_INFO_KIND, .off = _OUT(ifla_cloner), .cb = nlattr_get_stringn },
401 { .type = IFLA_INFO_DATA, .off = _OUT(ifla_idata), .cb = nlattr_get_nla },
402 };
403 NL_DECLARE_ATTR_PARSER(linfo_parser, nla_p_linfo);
404
405 static const struct nlattr_parser nla_p_if[] = {
406 { .type = IFLA_ADDRESS, .off = _OUT(ifla_address), .cb = nlattr_get_nla },
407 { .type = IFLA_IFNAME, .off = _OUT(ifla_ifname), .cb = nlattr_get_string },
408 { .type = IFLA_MTU, .off = _OUT(ifla_mtu), .cb = nlattr_get_uint32 },
409 { .type = IFLA_LINK, .off = _OUT(ifla_link), .cb = nlattr_get_uint32 },
410 { .type = IFLA_LINKINFO, .arg = &linfo_parser, .cb = nlattr_get_nested },
411 { .type = IFLA_IFALIAS, .off = _OUT(ifla_ifalias), .cb = nlattr_get_string },
412 { .type = IFLA_GROUP, .off = _OUT(ifla_group), .cb = nlattr_get_string },
413 { .type = IFLA_ALT_IFNAME, .off = _OUT(ifla_ifname), .cb = nlattr_get_string },
414 };
415 #undef _IN
416 #undef _OUT
417 NL_DECLARE_STRICT_PARSER(ifmsg_parser, struct ifinfomsg, check_ifmsg, nlf_p_if, nla_p_if);
418
419 static bool
match_iface(if_t ifp,void * _arg)420 match_iface(if_t ifp, void *_arg)
421 {
422 struct nl_parsed_link *attrs = (struct nl_parsed_link *)_arg;
423
424 if (attrs->ifi_index != 0 && attrs->ifi_index != if_getindex(ifp))
425 return (false);
426 if (attrs->ifi_type != 0 && attrs->ifi_index != if_gettype(ifp))
427 return (false);
428 if (attrs->ifla_ifname != NULL && strcmp(attrs->ifla_ifname, if_name(ifp)))
429 return (false);
430 /* TODO: add group match */
431
432 return (true);
433 }
434
435 static int
dump_cb(if_t ifp,void * _arg)436 dump_cb(if_t ifp, void *_arg)
437 {
438 struct netlink_walkargs *wa = (struct netlink_walkargs *)_arg;
439 if (!dump_iface(wa->nw, ifp, &wa->hdr, 0))
440 return (ENOMEM);
441 return (0);
442 }
443
444 /*
445 * {nlmsg_len=52, nlmsg_type=RTM_GETLINK, nlmsg_flags=NLM_F_REQUEST, nlmsg_seq=1662842818, nlmsg_pid=0},
446 * {ifi_family=AF_PACKET, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0},
447 * [
448 * [{nla_len=10, nla_type=IFLA_IFNAME}, "vnet9"],
449 * [{nla_len=8, nla_type=IFLA_EXT_MASK}, RTEXT_FILTER_VF]
450 * ]
451 */
452 static int
rtnl_handle_getlink(struct nlmsghdr * hdr,struct nlpcb * nlp,struct nl_pstate * npt)453 rtnl_handle_getlink(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt)
454 {
455 struct epoch_tracker et;
456 if_t ifp;
457 int error = 0;
458
459 struct nl_parsed_link attrs = {};
460 error = nl_parse_nlmsg(hdr, &ifmsg_parser, npt, &attrs);
461 if (error != 0)
462 return (error);
463
464 struct netlink_walkargs wa = {
465 .so = nlp,
466 .nw = npt->nw,
467 .hdr.nlmsg_pid = hdr->nlmsg_pid,
468 .hdr.nlmsg_seq = hdr->nlmsg_seq,
469 .hdr.nlmsg_flags = hdr->nlmsg_flags,
470 .hdr.nlmsg_type = NL_RTM_NEWLINK,
471 };
472
473 /* Fast track for an interface w/ explicit name or index match */
474 if ((attrs.ifi_index != 0) || (attrs.ifla_ifname != NULL)) {
475 if (attrs.ifi_index != 0) {
476 NLP_LOG(LOG_DEBUG3, nlp, "fast track -> searching index %u",
477 attrs.ifi_index);
478 NET_EPOCH_ENTER(et);
479 ifp = ifnet_byindex_ref(attrs.ifi_index);
480 NET_EPOCH_EXIT(et);
481 } else {
482 NLP_LOG(LOG_DEBUG3, nlp, "fast track -> searching name %s",
483 attrs.ifla_ifname);
484 ifp = ifunit_ref(attrs.ifla_ifname);
485 }
486
487 if (ifp != NULL) {
488 if (match_iface(ifp, &attrs)) {
489 if (!dump_iface(wa.nw, ifp, &wa.hdr, 0))
490 error = ENOMEM;
491 } else
492 error = ENODEV;
493 if_rele(ifp);
494 } else
495 error = ENODEV;
496 return (error);
497 }
498
499 /* Always treat non-direct-match as a multipart message */
500 wa.hdr.nlmsg_flags |= NLM_F_MULTI;
501
502 /*
503 * Fetching some link properties require performing ioctl's that may be blocking.
504 * Address it by saving referenced pointers of the matching links,
505 * exiting from epoch and going through the list one-by-one.
506 */
507
508 NL_LOG(LOG_DEBUG2, "Start dump");
509 if_foreach_sleep(match_iface, &attrs, dump_cb, &wa);
510 NL_LOG(LOG_DEBUG2, "End dump, iterated %d dumped %d", wa.count, wa.dumped);
511
512 if (!nlmsg_end_dump(wa.nw, error, &wa.hdr)) {
513 NL_LOG(LOG_DEBUG, "Unable to finalize the dump");
514 return (ENOMEM);
515 }
516
517 return (error);
518 }
519
520 /*
521 * sendmsg(3, {msg_name={sa_family=AF_NETLINK, nl_pid=0, nl_groups=00000000}, msg_namelen=12, msg_iov=[{iov_base=[
522 * {nlmsg_len=60, nlmsg_type=RTM_NEWLINK, nlmsg_flags=NLM_F_REQUEST|NLM_F_ACK|NLM_F_EXCL|NLM_F_CREATE, nlmsg_seq=1662715618, nlmsg_pid=0},
523 * {ifi_family=AF_UNSPEC, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0},
524 * {nla_len=11, nla_type=IFLA_IFNAME}, "dummy0"],
525 * [
526 * {nla_len=16, nla_type=IFLA_LINKINFO},
527 * [
528 * {nla_len=9, nla_type=IFLA_INFO_KIND}, "dummy"...
529 * ]
530 * ]
531 */
532
533 static int
rtnl_handle_dellink(struct nlmsghdr * hdr,struct nlpcb * nlp,struct nl_pstate * npt)534 rtnl_handle_dellink(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt)
535 {
536 struct epoch_tracker et;
537 if_t ifp;
538 int error;
539
540 struct nl_parsed_link attrs = {};
541 error = nl_parse_nlmsg(hdr, &ifmsg_parser, npt, &attrs);
542 if (error != 0)
543 return (error);
544
545 NET_EPOCH_ENTER(et);
546 ifp = ifnet_byindex_ref(attrs.ifi_index);
547 NET_EPOCH_EXIT(et);
548 if (ifp == NULL) {
549 NLP_LOG(LOG_DEBUG, nlp, "unable to find interface %u", attrs.ifi_index);
550 return (ENOENT);
551 }
552 NLP_LOG(LOG_DEBUG3, nlp, "mapped ifindex %u to %s", attrs.ifi_index, if_name(ifp));
553
554 sx_xlock(&ifnet_detach_sxlock);
555 error = if_clone_destroy(if_name(ifp));
556 sx_xunlock(&ifnet_detach_sxlock);
557
558 NLP_LOG(LOG_DEBUG2, nlp, "deleting interface %s returned %d", if_name(ifp), error);
559
560 if_rele(ifp);
561 return (error);
562 }
563
564 /*
565 * New link:
566 * type=RTM_NEWLINK, flags=NLM_F_REQUEST|NLM_F_ACK|NLM_F_EXCL|NLM_F_CREATE, seq=1668185590, pid=0},
567 * {ifi_family=AF_UNSPEC, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0}
568 * [
569 * {{nla_len=8, nla_type=IFLA_MTU}, 123},
570 * {{nla_len=10, nla_type=IFLA_IFNAME}, "vlan1"},
571 * {{nla_len=24, nla_type=IFLA_LINKINFO},
572 * [
573 * {{nla_len=8, nla_type=IFLA_INFO_KIND}, "vlan"...},
574 * {{nla_len=12, nla_type=IFLA_INFO_DATA}, "\x06\x00\x01\x00\x7b\x00\x00\x00"}]}]}
575 *
576 * Update link:
577 * type=RTM_NEWLINK, flags=NLM_F_REQUEST|NLM_F_ACK, seq=1668185923, pid=0},
578 * {ifi_family=AF_UNSPEC, ifi_type=ARPHRD_NETROM, ifi_index=if_nametoindex("lo"), ifi_flags=0, ifi_change=0},
579 * {{nla_len=8, nla_type=IFLA_MTU}, 123}}
580 *
581 *
582 * Check command availability:
583 * type=RTM_NEWLINK, flags=NLM_F_REQUEST|NLM_F_ACK, seq=0, pid=0},
584 * {ifi_family=AF_UNSPEC, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0}
585 */
586
587
588 static int
create_link(struct nlmsghdr * hdr,struct nl_parsed_link * lattrs,struct nlattr_bmask * bm,struct nlpcb * nlp,struct nl_pstate * npt)589 create_link(struct nlmsghdr *hdr, struct nl_parsed_link *lattrs,
590 struct nlattr_bmask *bm, struct nlpcb *nlp, struct nl_pstate *npt)
591 {
592 if (lattrs->ifla_ifname == NULL || strlen(lattrs->ifla_ifname) == 0) {
593 NLMSG_REPORT_ERR_MSG(npt, "empty IFLA_IFNAME attribute");
594 return (EINVAL);
595 }
596 if (lattrs->ifla_cloner == NULL || strlen(lattrs->ifla_cloner) == 0) {
597 NLMSG_REPORT_ERR_MSG(npt, "empty IFLA_INFO_KIND attribute");
598 return (EINVAL);
599 }
600
601 struct ifc_data_nl ifd = {
602 .flags = IFC_F_CREATE,
603 .lattrs = lattrs,
604 .bm = bm,
605 .npt = npt,
606 };
607 if (ifc_create_ifp_nl(lattrs->ifla_ifname, &ifd) && ifd.error == 0)
608 nl_store_ifp_cookie(npt, ifd.ifp);
609
610 return (ifd.error);
611 }
612
613 static int
modify_link(struct nlmsghdr * hdr,struct nl_parsed_link * lattrs,struct nlattr_bmask * bm,struct nlpcb * nlp,struct nl_pstate * npt)614 modify_link(struct nlmsghdr *hdr, struct nl_parsed_link *lattrs,
615 struct nlattr_bmask *bm, struct nlpcb *nlp, struct nl_pstate *npt)
616 {
617 if_t ifp = NULL;
618 struct epoch_tracker et;
619
620 if (lattrs->ifi_index == 0 && lattrs->ifla_ifname == NULL) {
621 /*
622 * Applications like ip(8) verify RTM_NEWLINK command
623 * existence by calling it with empty arguments. Always
624 * return "innocent" error in that case.
625 */
626 NLMSG_REPORT_ERR_MSG(npt, "empty ifi_index field");
627 return (EPERM);
628 }
629
630 if (lattrs->ifi_index != 0) {
631 NET_EPOCH_ENTER(et);
632 ifp = ifnet_byindex_ref(lattrs->ifi_index);
633 NET_EPOCH_EXIT(et);
634 if (ifp == NULL) {
635 NLMSG_REPORT_ERR_MSG(npt, "unable to find interface #%u",
636 lattrs->ifi_index);
637 return (ENOENT);
638 }
639 }
640
641 if (ifp == NULL && lattrs->ifla_ifname != NULL) {
642 ifp = ifunit_ref(lattrs->ifla_ifname);
643 if (ifp == NULL) {
644 NLMSG_REPORT_ERR_MSG(npt, "unable to find interface %s",
645 lattrs->ifla_ifname);
646 return (ENOENT);
647 }
648 }
649
650 MPASS(ifp != NULL);
651
652 /*
653 * Modification request can address either
654 * 1) cloned interface, in which case we call the cloner-specific
655 * modification routine
656 * or
657 * 2) non-cloned (e.g. "physical") interface, in which case we call
658 * generic modification routine
659 */
660 struct ifc_data_nl ifd = { .lattrs = lattrs, .bm = bm, .npt = npt };
661 if (!ifc_modify_ifp_nl(ifp, &ifd))
662 ifd.error = nl_modify_ifp_generic(ifp, lattrs, bm, npt);
663
664 if_rele(ifp);
665
666 return (ifd.error);
667 }
668
669
670 static int
rtnl_handle_newlink(struct nlmsghdr * hdr,struct nlpcb * nlp,struct nl_pstate * npt)671 rtnl_handle_newlink(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt)
672 {
673 struct nlattr_bmask bm;
674 int error;
675
676 struct nl_parsed_link attrs = {};
677 error = nl_parse_nlmsg(hdr, &ifmsg_parser, npt, &attrs);
678 if (error != 0)
679 return (error);
680 nl_get_attrs_bmask_nlmsg(hdr, &ifmsg_parser, &bm);
681
682 if (hdr->nlmsg_flags & NLM_F_CREATE)
683 return (create_link(hdr, &attrs, &bm, nlp, npt));
684 else
685 return (modify_link(hdr, &attrs, &bm, nlp, npt));
686 }
687
688 static void
set_scope6(struct sockaddr * sa,uint32_t ifindex)689 set_scope6(struct sockaddr *sa, uint32_t ifindex)
690 {
691 #ifdef INET6
692 if (sa != NULL && sa->sa_family == AF_INET6) {
693 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
694
695 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr))
696 in6_set_unicast_scopeid(&sa6->sin6_addr, ifindex);
697 }
698 #endif
699 }
700
701 static bool
check_sa_family(const struct sockaddr * sa,int family,const char * attr_name,struct nl_pstate * npt)702 check_sa_family(const struct sockaddr *sa, int family, const char *attr_name,
703 struct nl_pstate *npt)
704 {
705 if (sa == NULL || sa->sa_family == family)
706 return (true);
707
708 nlmsg_report_err_msg(npt, "wrong family for %s attribute: %d != %d",
709 attr_name, family, sa->sa_family);
710 return (false);
711 }
712
713 struct nl_parsed_ifa {
714 uint8_t ifa_family;
715 uint8_t ifa_prefixlen;
716 uint8_t ifa_scope;
717 uint32_t ifa_index;
718 uint32_t ifa_flags;
719 uint32_t ifaf_vhid;
720 uint32_t ifaf_flags;
721 struct sockaddr *ifa_address;
722 struct sockaddr *ifa_local;
723 struct sockaddr *ifa_broadcast;
724 struct ifa_cacheinfo *ifa_cacheinfo;
725 struct sockaddr *f_ifa_addr;
726 struct sockaddr *f_ifa_dst;
727 };
728
729 static int
nlattr_get_cinfo(struct nlattr * nla,struct nl_pstate * npt,const void * arg __unused,void * target)730 nlattr_get_cinfo(struct nlattr *nla, struct nl_pstate *npt,
731 const void *arg __unused, void *target)
732 {
733 if (__predict_false(NLA_DATA_LEN(nla) != sizeof(struct ifa_cacheinfo))) {
734 NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not ifa_cacheinfo",
735 nla->nla_type, NLA_DATA_LEN(nla));
736 return (EINVAL);
737 }
738 *((struct ifa_cacheinfo **)target) = (struct ifa_cacheinfo *)NL_RTA_DATA(nla);
739 return (0);
740 }
741
742 #define _IN(_field) offsetof(struct ifaddrmsg, _field)
743 #define _OUT(_field) offsetof(struct nl_parsed_ifa, _field)
744 static const struct nlfield_parser nlf_p_ifa[] = {
745 { .off_in = _IN(ifa_family), .off_out = _OUT(ifa_family), .cb = nlf_get_u8 },
746 { .off_in = _IN(ifa_prefixlen), .off_out = _OUT(ifa_prefixlen), .cb = nlf_get_u8 },
747 { .off_in = _IN(ifa_scope), .off_out = _OUT(ifa_scope), .cb = nlf_get_u8 },
748 { .off_in = _IN(ifa_flags), .off_out = _OUT(ifa_flags), .cb = nlf_get_u8_u32 },
749 { .off_in = _IN(ifa_index), .off_out = _OUT(ifa_index), .cb = nlf_get_u32 },
750 };
751
752 static const struct nlattr_parser nla_p_ifa_fbsd[] = {
753 { .type = IFAF_VHID, .off = _OUT(ifaf_vhid), .cb = nlattr_get_uint32 },
754 { .type = IFAF_FLAGS, .off = _OUT(ifaf_flags), .cb = nlattr_get_uint32 },
755 };
756 NL_DECLARE_ATTR_PARSER(ifa_fbsd_parser, nla_p_ifa_fbsd);
757
758 static const struct nlattr_parser nla_p_ifa[] = {
759 { .type = IFA_ADDRESS, .off = _OUT(ifa_address), .cb = nlattr_get_ip },
760 { .type = IFA_LOCAL, .off = _OUT(ifa_local), .cb = nlattr_get_ip },
761 { .type = IFA_BROADCAST, .off = _OUT(ifa_broadcast), .cb = nlattr_get_ip },
762 { .type = IFA_CACHEINFO, .off = _OUT(ifa_cacheinfo), .cb = nlattr_get_cinfo },
763 { .type = IFA_FLAGS, .off = _OUT(ifa_flags), .cb = nlattr_get_uint32 },
764 { .type = IFA_FREEBSD, .arg = &ifa_fbsd_parser, .cb = nlattr_get_nested },
765 };
766 #undef _IN
767 #undef _OUT
768
769 static bool
post_p_ifa(void * _attrs,struct nl_pstate * npt)770 post_p_ifa(void *_attrs, struct nl_pstate *npt)
771 {
772 struct nl_parsed_ifa *attrs = (struct nl_parsed_ifa *)_attrs;
773
774 if (!check_sa_family(attrs->ifa_address, attrs->ifa_family, "IFA_ADDRESS", npt))
775 return (false);
776 if (!check_sa_family(attrs->ifa_local, attrs->ifa_family, "IFA_LOCAL", npt))
777 return (false);
778 if (!check_sa_family(attrs->ifa_broadcast, attrs->ifa_family, "IFA_BROADADDR", npt))
779 return (false);
780
781 set_scope6(attrs->ifa_address, attrs->ifa_index);
782 set_scope6(attrs->ifa_local, attrs->ifa_index);
783
784 return (true);
785 }
786
787 NL_DECLARE_PARSER_EXT(ifa_parser, struct ifaddrmsg, NULL, nlf_p_ifa, nla_p_ifa, post_p_ifa);
788
789
790 /*
791
792 {ifa_family=AF_INET, ifa_prefixlen=8, ifa_flags=IFA_F_PERMANENT, ifa_scope=RT_SCOPE_HOST, ifa_index=if_nametoindex("lo")},
793 [
794 {{nla_len=8, nla_type=IFA_ADDRESS}, inet_addr("127.0.0.1")},
795 {{nla_len=8, nla_type=IFA_LOCAL}, inet_addr("127.0.0.1")},
796 {{nla_len=7, nla_type=IFA_LABEL}, "lo"},
797 {{nla_len=8, nla_type=IFA_FLAGS}, IFA_F_PERMANENT},
798 {{nla_len=20, nla_type=IFA_CACHEINFO}, {ifa_prefered=4294967295, ifa_valid=4294967295, cstamp=3619, tstamp=3619}}]},
799 ---
800
801 {{len=72, type=RTM_NEWADDR, flags=NLM_F_MULTI, seq=1642191126, pid=566735},
802 {ifa_family=AF_INET6, ifa_prefixlen=96, ifa_flags=IFA_F_PERMANENT, ifa_scope=RT_SCOPE_UNIVERSE, ifa_index=if_nametoindex("virbr0")},
803 [
804 {{nla_len=20, nla_type=IFA_ADDRESS}, inet_pton(AF_INET6, "2a01:4f8:13a:70c:ffff::1")},
805 {{nla_len=20, nla_type=IFA_CACHEINFO}, {ifa_prefered=4294967295, ifa_valid=4294967295, cstamp=4283, tstamp=4283}},
806 {{nla_len=8, nla_type=IFA_FLAGS}, IFA_F_PERMANENT}]},
807 */
808
809 static uint8_t
ifa_get_scope(const struct ifaddr * ifa)810 ifa_get_scope(const struct ifaddr *ifa)
811 {
812 const struct sockaddr *sa;
813 uint8_t addr_scope = RT_SCOPE_UNIVERSE;
814
815 sa = ifa->ifa_addr;
816 switch (sa->sa_family) {
817 #ifdef INET
818 case AF_INET:
819 {
820 struct in_addr addr;
821 addr = ((const struct sockaddr_in *)sa)->sin_addr;
822 if (IN_LOOPBACK(ntohl(addr.s_addr)))
823 addr_scope = RT_SCOPE_HOST;
824 else if (IN_LINKLOCAL(ntohl(addr.s_addr)))
825 addr_scope = RT_SCOPE_LINK;
826 break;
827 }
828 #endif
829 #ifdef INET6
830 case AF_INET6:
831 {
832 const struct in6_addr *addr;
833 addr = &((const struct sockaddr_in6 *)sa)->sin6_addr;
834 if (IN6_IS_ADDR_LOOPBACK(addr))
835 addr_scope = RT_SCOPE_HOST;
836 else if (IN6_IS_ADDR_LINKLOCAL(addr))
837 addr_scope = RT_SCOPE_LINK;
838 break;
839 }
840 #endif
841 }
842
843 return (addr_scope);
844 }
845
846 #ifdef INET6
847 static uint8_t
inet6_get_plen(const struct in6_addr * addr)848 inet6_get_plen(const struct in6_addr *addr)
849 {
850
851 return (bitcount32(addr->s6_addr32[0]) + bitcount32(addr->s6_addr32[1]) +
852 bitcount32(addr->s6_addr32[2]) + bitcount32(addr->s6_addr32[3]));
853 }
854 #endif
855
856 static uint8_t
get_sa_plen(const struct sockaddr * sa)857 get_sa_plen(const struct sockaddr *sa)
858 {
859 #ifdef INET
860 const struct in_addr *paddr;
861 #endif
862 #ifdef INET6
863 const struct in6_addr *paddr6;
864 #endif
865
866 switch (sa->sa_family) {
867 #ifdef INET
868 case AF_INET:
869 paddr = &(((const struct sockaddr_in *)sa)->sin_addr);
870 return bitcount32(paddr->s_addr);
871 #endif
872 #ifdef INET6
873 case AF_INET6:
874 paddr6 = &(((const struct sockaddr_in6 *)sa)->sin6_addr);
875 return inet6_get_plen(paddr6);
876 #endif
877 }
878
879 return (0);
880 }
881
882 #ifdef INET6
883 static uint32_t
in6_flags_to_nl(uint32_t flags)884 in6_flags_to_nl(uint32_t flags)
885 {
886 uint32_t nl_flags = 0;
887
888 if (flags & IN6_IFF_TEMPORARY)
889 nl_flags |= IFA_F_TEMPORARY;
890 if (flags & IN6_IFF_NODAD)
891 nl_flags |= IFA_F_NODAD;
892 if (flags & IN6_IFF_DEPRECATED)
893 nl_flags |= IFA_F_DEPRECATED;
894 if (flags & IN6_IFF_TENTATIVE)
895 nl_flags |= IFA_F_TENTATIVE;
896 if ((flags & (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) == 0)
897 flags |= IFA_F_PERMANENT;
898 if (flags & IN6_IFF_DUPLICATED)
899 flags |= IFA_F_DADFAILED;
900 return (nl_flags);
901 }
902
903 static uint32_t
nl_flags_to_in6(uint32_t flags)904 nl_flags_to_in6(uint32_t flags)
905 {
906 uint32_t in6_flags = 0;
907
908 if (flags & IFA_F_TEMPORARY)
909 in6_flags |= IN6_IFF_TEMPORARY;
910 if (flags & IFA_F_NODAD)
911 in6_flags |= IN6_IFF_NODAD;
912 if (flags & IFA_F_DEPRECATED)
913 in6_flags |= IN6_IFF_DEPRECATED;
914 if (flags & IFA_F_TENTATIVE)
915 in6_flags |= IN6_IFF_TENTATIVE;
916 if (flags & IFA_F_DADFAILED)
917 in6_flags |= IN6_IFF_DUPLICATED;
918
919 return (in6_flags);
920 }
921
922 static void
export_cache_info6(struct nl_writer * nw,const struct in6_ifaddr * ia)923 export_cache_info6(struct nl_writer *nw, const struct in6_ifaddr *ia)
924 {
925 struct ifa_cacheinfo ci = {
926 .cstamp = ia->ia6_createtime * 1000,
927 .tstamp = ia->ia6_updatetime * 1000,
928 .ifa_prefered = ia->ia6_lifetime.ia6t_pltime,
929 .ifa_valid = ia->ia6_lifetime.ia6t_vltime,
930 };
931
932 nlattr_add(nw, IFA_CACHEINFO, sizeof(ci), &ci);
933 }
934 #endif
935
936 static void
export_cache_info(struct nl_writer * nw,struct ifaddr * ifa)937 export_cache_info(struct nl_writer *nw, struct ifaddr *ifa)
938 {
939 switch (ifa->ifa_addr->sa_family) {
940 #ifdef INET6
941 case AF_INET6:
942 export_cache_info6(nw, (struct in6_ifaddr *)ifa);
943 break;
944 #endif
945 }
946 }
947
948 /*
949 * {'attrs': [('IFA_ADDRESS', '12.0.0.1'),
950 ('IFA_LOCAL', '12.0.0.1'),
951 ('IFA_LABEL', 'eth10'),
952 ('IFA_FLAGS', 128),
953 ('IFA_CACHEINFO', {'ifa_preferred': 4294967295, 'ifa_valid': 4294967295, 'cstamp': 63745746, 'tstamp': 63745746})],
954 */
955 static bool
dump_iface_addr(struct nl_writer * nw,if_t ifp,struct ifaddr * ifa,const struct nlmsghdr * hdr)956 dump_iface_addr(struct nl_writer *nw, if_t ifp, struct ifaddr *ifa,
957 const struct nlmsghdr *hdr)
958 {
959 struct ifaddrmsg *ifamsg;
960 struct sockaddr *sa = ifa->ifa_addr;
961 struct sockaddr *sa_dst = ifa->ifa_dstaddr;
962
963 NL_LOG(LOG_DEBUG3, "dumping ifa %p type %s(%d) for interface %s",
964 ifa, rib_print_family(sa->sa_family), sa->sa_family, if_name(ifp));
965
966 if (!nlmsg_reply(nw, hdr, sizeof(struct ifaddrmsg)))
967 goto enomem;
968
969 ifamsg = nlmsg_reserve_object(nw, struct ifaddrmsg);
970 ifamsg->ifa_family = sa->sa_family;
971 ifamsg->ifa_prefixlen = get_sa_plen(ifa->ifa_netmask);
972 ifamsg->ifa_flags = 0; // ifa_flags is useless
973 ifamsg->ifa_scope = ifa_get_scope(ifa);
974 ifamsg->ifa_index = if_getindex(ifp);
975
976 if ((if_getflags(ifp) & IFF_POINTOPOINT) && sa_dst != NULL && sa_dst->sa_family != 0) {
977 /* P2P interface may have IPv6 LL with no dst address */
978 dump_sa(nw, IFA_ADDRESS, sa_dst);
979 dump_sa(nw, IFA_LOCAL, sa);
980 } else {
981 dump_sa(nw, IFA_ADDRESS, sa);
982 #ifdef INET
983 /*
984 * In most cases, IFA_ADDRESS == IFA_LOCAL
985 * Skip IFA_LOCAL for anything except INET
986 */
987 if (sa->sa_family == AF_INET)
988 dump_sa(nw, IFA_LOCAL, sa);
989 #endif
990 }
991 if (if_getflags(ifp) & IFF_BROADCAST)
992 dump_sa(nw, IFA_BROADCAST, ifa->ifa_broadaddr);
993
994 nlattr_add_string(nw, IFA_LABEL, if_name(ifp));
995
996 uint32_t nl_ifa_flags = 0;
997 #ifdef INET6
998 if (sa->sa_family == AF_INET6) {
999 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1000 nl_ifa_flags = in6_flags_to_nl(ia->ia6_flags);
1001 }
1002 #endif
1003 nlattr_add_u32(nw, IFA_FLAGS, nl_ifa_flags);
1004
1005 export_cache_info(nw, ifa);
1006
1007 /* Store FreeBSD-specific attributes */
1008 int off = nlattr_add_nested(nw, IFA_FREEBSD);
1009 if (off != 0) {
1010 if (ifa->ifa_carp != NULL && carp_get_vhid_p != NULL) {
1011 uint32_t vhid = (uint32_t)(*carp_get_vhid_p)(ifa);
1012 nlattr_add_u32(nw, IFAF_VHID, vhid);
1013 }
1014 #ifdef INET6
1015 if (sa->sa_family == AF_INET6) {
1016 uint32_t ifa_flags = ((struct in6_ifaddr *)ifa)->ia6_flags;
1017
1018 nlattr_add_u32(nw, IFAF_FLAGS, ifa_flags);
1019 }
1020 #endif
1021
1022 nlattr_set_len(nw, off);
1023 }
1024
1025 if (nlmsg_end(nw))
1026 return (true);
1027 enomem:
1028 NL_LOG(LOG_DEBUG, "Failed to dump ifa type %s(%d) for interface %s",
1029 rib_print_family(sa->sa_family), sa->sa_family, if_name(ifp));
1030 nlmsg_abort(nw);
1031 return (false);
1032 }
1033
1034 static int
dump_iface_addrs(struct netlink_walkargs * wa,if_t ifp)1035 dump_iface_addrs(struct netlink_walkargs *wa, if_t ifp)
1036 {
1037 struct ifaddr *ifa;
1038 struct ifa_iter it;
1039 int error = 0;
1040
1041 for (ifa = ifa_iter_start(ifp, &it); ifa != NULL; ifa = ifa_iter_next(&it)) {
1042 if (wa->family != 0 && wa->family != ifa->ifa_addr->sa_family)
1043 continue;
1044 if (ifa->ifa_addr->sa_family == AF_LINK)
1045 continue;
1046 if (prison_if(wa->cred, ifa->ifa_addr) != 0)
1047 continue;
1048 wa->count++;
1049 if (!dump_iface_addr(wa->nw, ifp, ifa, &wa->hdr)) {
1050 error = ENOMEM;
1051 break;
1052 }
1053 wa->dumped++;
1054 }
1055 ifa_iter_finish(&it);
1056
1057 return (error);
1058 }
1059
1060 static int
rtnl_handle_getaddr(struct nlmsghdr * hdr,struct nlpcb * nlp,struct nl_pstate * npt)1061 rtnl_handle_getaddr(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt)
1062 {
1063 if_t ifp;
1064 int error = 0;
1065
1066 struct nl_parsed_ifa attrs = {};
1067 error = nl_parse_nlmsg(hdr, &ifa_parser, npt, &attrs);
1068 if (error != 0)
1069 return (error);
1070
1071 struct netlink_walkargs wa = {
1072 .so = nlp,
1073 .nw = npt->nw,
1074 .cred = nlp_get_cred(nlp),
1075 .family = attrs.ifa_family,
1076 .hdr.nlmsg_pid = hdr->nlmsg_pid,
1077 .hdr.nlmsg_seq = hdr->nlmsg_seq,
1078 .hdr.nlmsg_flags = hdr->nlmsg_flags | NLM_F_MULTI,
1079 .hdr.nlmsg_type = NL_RTM_NEWADDR,
1080 };
1081
1082 NL_LOG(LOG_DEBUG2, "Start dump");
1083
1084 if (attrs.ifa_index != 0) {
1085 ifp = ifnet_byindex(attrs.ifa_index);
1086 if (ifp == NULL)
1087 error = ENOENT;
1088 else
1089 error = dump_iface_addrs(&wa, ifp);
1090 } else {
1091 struct if_iter it;
1092
1093 for (ifp = if_iter_start(&it); ifp != NULL; ifp = if_iter_next(&it)) {
1094 error = dump_iface_addrs(&wa, ifp);
1095 if (error != 0)
1096 break;
1097 }
1098 if_iter_finish(&it);
1099 }
1100
1101 NL_LOG(LOG_DEBUG2, "End dump, iterated %d dumped %d", wa.count, wa.dumped);
1102
1103 if (!nlmsg_end_dump(wa.nw, error, &wa.hdr)) {
1104 NL_LOG(LOG_DEBUG, "Unable to finalize the dump");
1105 return (ENOMEM);
1106 }
1107
1108 return (error);
1109 }
1110
1111 #ifdef INET
1112 static int
handle_newaddr_inet(struct nlmsghdr * hdr,struct nl_parsed_ifa * attrs,if_t ifp,struct nlpcb * nlp,struct nl_pstate * npt)1113 handle_newaddr_inet(struct nlmsghdr *hdr, struct nl_parsed_ifa *attrs,
1114 if_t ifp, struct nlpcb *nlp, struct nl_pstate *npt)
1115 {
1116 int plen = attrs->ifa_prefixlen;
1117 int if_flags = if_getflags(ifp);
1118 struct sockaddr_in *addr, *dst;
1119
1120 if (plen > 32) {
1121 nlmsg_report_err_msg(npt, "invalid ifa_prefixlen");
1122 return (EINVAL);
1123 };
1124
1125 if (if_flags & IFF_POINTOPOINT) {
1126 /*
1127 * Only P2P IFAs are allowed by the implementation.
1128 */
1129 if (attrs->ifa_address == NULL || attrs->ifa_local == NULL) {
1130 nlmsg_report_err_msg(npt, "Empty IFA_LOCAL/IFA_ADDRESS");
1131 return (EINVAL);
1132 }
1133 addr = (struct sockaddr_in *)attrs->ifa_local;
1134 dst = (struct sockaddr_in *)attrs->ifa_address;
1135 } else {
1136 /*
1137 * Map the Netlink attributes to FreeBSD ifa layout.
1138 * If only IFA_ADDRESS or IFA_LOCAL is set OR
1139 * both are set to the same value => ifa is not p2p
1140 * and the attribute value contains interface address.
1141 *
1142 * Otherwise (both IFA_ADDRESS and IFA_LOCAL are set and
1143 * different), IFA_LOCAL contains an interface address and
1144 * IFA_ADDRESS contains peer address.
1145 */
1146 addr = (struct sockaddr_in *)attrs->ifa_local;
1147 if (addr == NULL)
1148 addr = (struct sockaddr_in *)attrs->ifa_address;
1149
1150 if (addr == NULL) {
1151 nlmsg_report_err_msg(npt, "Empty IFA_LOCAL/IFA_ADDRESS");
1152 return (EINVAL);
1153 }
1154
1155 /* Generate broadcast address if not set */
1156 if ((if_flags & IFF_BROADCAST) && attrs->ifa_broadcast == NULL) {
1157 uint32_t s_baddr;
1158 struct sockaddr_in *sin_brd;
1159
1160 if (plen == 31)
1161 s_baddr = INADDR_BROADCAST; /* RFC 3021 */
1162 else {
1163 uint32_t s_mask;
1164
1165 s_mask = htonl(plen ? ~((1 << (32 - plen)) - 1) : 0);
1166 s_baddr = addr->sin_addr.s_addr | ~s_mask;
1167 }
1168
1169 sin_brd = (struct sockaddr_in *)npt_alloc(npt, sizeof(*sin_brd));
1170 if (sin_brd == NULL)
1171 return (ENOMEM);
1172 sin_brd->sin_family = AF_INET;
1173 sin_brd->sin_len = sizeof(*sin_brd);
1174 sin_brd->sin_addr.s_addr = s_baddr;
1175 attrs->ifa_broadcast = (struct sockaddr *)sin_brd;
1176 }
1177 dst = (struct sockaddr_in *)attrs->ifa_broadcast;
1178 }
1179
1180 struct sockaddr_in mask = {
1181 .sin_len = sizeof(struct sockaddr_in),
1182 .sin_family = AF_INET,
1183 .sin_addr.s_addr = htonl(plen ? ~((1 << (32 - plen)) - 1) : 0),
1184 };
1185 struct in_aliasreq req = {
1186 .ifra_addr = *addr,
1187 .ifra_mask = mask,
1188 .ifra_vhid = attrs->ifaf_vhid,
1189 };
1190 if (dst != NULL)
1191 req.ifra_dstaddr = *dst;
1192
1193 return (in_control_ioctl(SIOCAIFADDR, &req, ifp, nlp_get_cred(nlp)));
1194 }
1195
1196 static int
handle_deladdr_inet(struct nlmsghdr * hdr,struct nl_parsed_ifa * attrs,if_t ifp,struct nlpcb * nlp,struct nl_pstate * npt)1197 handle_deladdr_inet(struct nlmsghdr *hdr, struct nl_parsed_ifa *attrs,
1198 if_t ifp, struct nlpcb *nlp, struct nl_pstate *npt)
1199 {
1200 struct sockaddr *addr = attrs->ifa_local;
1201
1202 if (addr == NULL)
1203 addr = attrs->ifa_address;
1204
1205 if (addr == NULL) {
1206 nlmsg_report_err_msg(npt, "empty IFA_ADDRESS/IFA_LOCAL");
1207 return (EINVAL);
1208 }
1209
1210 struct ifreq req = { .ifr_addr = *addr };
1211
1212 return (in_control_ioctl(SIOCDIFADDR, &req, ifp, nlp_get_cred(nlp)));
1213 }
1214 #endif
1215
1216 #ifdef INET6
1217 static int
handle_newaddr_inet6(struct nlmsghdr * hdr,struct nl_parsed_ifa * attrs,if_t ifp,struct nlpcb * nlp,struct nl_pstate * npt)1218 handle_newaddr_inet6(struct nlmsghdr *hdr, struct nl_parsed_ifa *attrs,
1219 if_t ifp, struct nlpcb *nlp, struct nl_pstate *npt)
1220 {
1221 struct sockaddr_in6 *addr, *dst;
1222
1223 if (attrs->ifa_prefixlen > 128) {
1224 nlmsg_report_err_msg(npt, "invalid ifa_prefixlen");
1225 return (EINVAL);
1226 }
1227
1228 /*
1229 * In IPv6 implementation, adding non-P2P address to the P2P interface
1230 * is allowed.
1231 */
1232 addr = (struct sockaddr_in6 *)(attrs->ifa_local);
1233 dst = (struct sockaddr_in6 *)(attrs->ifa_address);
1234
1235 if (addr == NULL) {
1236 addr = dst;
1237 dst = NULL;
1238 } else if (dst != NULL) {
1239 if (IN6_ARE_ADDR_EQUAL(&addr->sin6_addr, &dst->sin6_addr)) {
1240 /*
1241 * Sometimes Netlink users fills in both attributes
1242 * with the same address. It still means "non-p2p".
1243 */
1244 dst = NULL;
1245 }
1246 }
1247
1248 if (addr == NULL) {
1249 nlmsg_report_err_msg(npt, "Empty IFA_LOCAL/IFA_ADDRESS");
1250 return (EINVAL);
1251 }
1252
1253 uint32_t flags = nl_flags_to_in6(attrs->ifa_flags) | attrs->ifaf_flags;
1254
1255 uint32_t pltime = 0, vltime = 0;
1256 if (attrs->ifa_cacheinfo != 0) {
1257 pltime = attrs->ifa_cacheinfo->ifa_prefered;
1258 vltime = attrs->ifa_cacheinfo->ifa_valid;
1259 }
1260
1261 struct sockaddr_in6 mask = {
1262 .sin6_len = sizeof(struct sockaddr_in6),
1263 .sin6_family = AF_INET6,
1264 };
1265 ip6_writemask(&mask.sin6_addr, attrs->ifa_prefixlen);
1266
1267 struct in6_aliasreq req = {
1268 .ifra_addr = *addr,
1269 .ifra_prefixmask = mask,
1270 .ifra_flags = flags,
1271 .ifra_lifetime = { .ia6t_vltime = vltime, .ia6t_pltime = pltime },
1272 .ifra_vhid = attrs->ifaf_vhid,
1273 };
1274 if (dst != NULL)
1275 req.ifra_dstaddr = *dst;
1276
1277 return (in6_control_ioctl(SIOCAIFADDR_IN6, &req, ifp, nlp_get_cred(nlp)));
1278 }
1279
1280 static int
handle_deladdr_inet6(struct nlmsghdr * hdr,struct nl_parsed_ifa * attrs,if_t ifp,struct nlpcb * nlp,struct nl_pstate * npt)1281 handle_deladdr_inet6(struct nlmsghdr *hdr, struct nl_parsed_ifa *attrs,
1282 if_t ifp, struct nlpcb *nlp, struct nl_pstate *npt)
1283 {
1284 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)attrs->ifa_local;
1285
1286 if (addr == NULL)
1287 addr = (struct sockaddr_in6 *)(attrs->ifa_address);
1288
1289 if (addr == NULL) {
1290 nlmsg_report_err_msg(npt, "Empty IFA_LOCAL/IFA_ADDRESS");
1291 return (EINVAL);
1292 }
1293
1294 struct in6_ifreq req = { .ifr_addr = *addr };
1295
1296 return (in6_control_ioctl(SIOCDIFADDR_IN6, &req, ifp, nlp_get_cred(nlp)));
1297 }
1298 #endif
1299
1300
1301 static int
rtnl_handle_addr(struct nlmsghdr * hdr,struct nlpcb * nlp,struct nl_pstate * npt)1302 rtnl_handle_addr(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt)
1303 {
1304 struct epoch_tracker et;
1305 int error;
1306
1307 struct nl_parsed_ifa attrs = {};
1308 error = nl_parse_nlmsg(hdr, &ifa_parser, npt, &attrs);
1309 if (error != 0)
1310 return (error);
1311
1312 NET_EPOCH_ENTER(et);
1313 if_t ifp = ifnet_byindex_ref(attrs.ifa_index);
1314 NET_EPOCH_EXIT(et);
1315
1316 if (ifp == NULL) {
1317 nlmsg_report_err_msg(npt, "Unable to find interface with index %u",
1318 attrs.ifa_index);
1319 return (ENOENT);
1320 }
1321 int if_flags = if_getflags(ifp);
1322
1323 #if defined(INET) || defined(INET6)
1324 bool new = hdr->nlmsg_type == NL_RTM_NEWADDR;
1325 #endif
1326
1327 /*
1328 * TODO: Properly handle NLM_F_CREATE / NLM_F_EXCL.
1329 * The current ioctl-based KPI always does an implicit create-or-replace.
1330 * It is not possible to specify fine-grained options.
1331 */
1332
1333 switch (attrs.ifa_family) {
1334 #ifdef INET
1335 case AF_INET:
1336 if (new)
1337 error = handle_newaddr_inet(hdr, &attrs, ifp, nlp, npt);
1338 else
1339 error = handle_deladdr_inet(hdr, &attrs, ifp, nlp, npt);
1340 break;
1341 #endif
1342 #ifdef INET6
1343 case AF_INET6:
1344 if (new)
1345 error = handle_newaddr_inet6(hdr, &attrs, ifp, nlp, npt);
1346 else
1347 error = handle_deladdr_inet6(hdr, &attrs, ifp, nlp, npt);
1348 break;
1349 #endif
1350 default:
1351 error = EAFNOSUPPORT;
1352 }
1353
1354 if (error == 0 && !(if_flags & IFF_UP) && (if_getflags(ifp) & IFF_UP))
1355 if_up(ifp);
1356
1357 if_rele(ifp);
1358
1359 return (error);
1360 }
1361
1362
1363 static void
rtnl_handle_ifaddr(void * arg __unused,struct ifaddr * ifa,int cmd)1364 rtnl_handle_ifaddr(void *arg __unused, struct ifaddr *ifa, int cmd)
1365 {
1366 struct nlmsghdr hdr = {};
1367 struct nl_writer nw;
1368 uint32_t group = 0;
1369
1370 switch (ifa->ifa_addr->sa_family) {
1371 #ifdef INET
1372 case AF_INET:
1373 group = RTNLGRP_IPV4_IFADDR;
1374 break;
1375 #endif
1376 #ifdef INET6
1377 case AF_INET6:
1378 group = RTNLGRP_IPV6_IFADDR;
1379 break;
1380 #endif
1381 default:
1382 NL_LOG(LOG_DEBUG2, "ifa notification for unknown AF: %d",
1383 ifa->ifa_addr->sa_family);
1384 return;
1385 }
1386
1387 if (!nl_writer_group(&nw, NLMSG_LARGE, NETLINK_ROUTE, group, 0,
1388 false)) {
1389 NL_LOG(LOG_DEBUG, "error allocating group writer");
1390 return;
1391 }
1392
1393 hdr.nlmsg_type = (cmd == RTM_DELETE) ? NL_RTM_DELADDR : NL_RTM_NEWADDR;
1394
1395 dump_iface_addr(&nw, ifa->ifa_ifp, ifa, &hdr);
1396 nlmsg_flush(&nw);
1397 }
1398
1399 static void
rtnl_handle_ifevent(if_t ifp,int nlmsg_type,int if_flags_mask)1400 rtnl_handle_ifevent(if_t ifp, int nlmsg_type, int if_flags_mask)
1401 {
1402 struct nlmsghdr hdr = { .nlmsg_type = nlmsg_type };
1403 struct nl_writer nw;
1404
1405 if (!nl_writer_group(&nw, NLMSG_LARGE, NETLINK_ROUTE, RTNLGRP_LINK, 0,
1406 false)) {
1407 NL_LOG(LOG_DEBUG, "error allocating group writer");
1408 return;
1409 }
1410 dump_iface(&nw, ifp, &hdr, if_flags_mask);
1411 nlmsg_flush(&nw);
1412 }
1413
1414 static void
rtnl_handle_ifattach(void * arg,if_t ifp)1415 rtnl_handle_ifattach(void *arg, if_t ifp)
1416 {
1417 NL_LOG(LOG_DEBUG2, "ifnet %s", if_name(ifp));
1418 rtnl_handle_ifevent(ifp, NL_RTM_NEWLINK, 0);
1419 }
1420
1421 static void
rtnl_handle_ifdetach(void * arg,if_t ifp)1422 rtnl_handle_ifdetach(void *arg, if_t ifp)
1423 {
1424 NL_LOG(LOG_DEBUG2, "ifnet %s", if_name(ifp));
1425 rtnl_handle_ifevent(ifp, NL_RTM_DELLINK, 0);
1426 }
1427
1428 static void
rtnl_handle_iflink(void * arg,if_t ifp,int link_state __unused)1429 rtnl_handle_iflink(void *arg, if_t ifp, int link_state __unused)
1430 {
1431 NL_LOG(LOG_DEBUG2, "ifnet %s", if_name(ifp));
1432 rtnl_handle_ifevent(ifp, NL_RTM_NEWLINK, 0);
1433 }
1434
1435 void
rtnl_handle_ifnet_event(if_t ifp,int if_flags_mask)1436 rtnl_handle_ifnet_event(if_t ifp, int if_flags_mask)
1437 {
1438 NL_LOG(LOG_DEBUG2, "ifnet %s", if_name(ifp));
1439 rtnl_handle_ifevent(ifp, NL_RTM_NEWLINK, if_flags_mask);
1440 }
1441
1442 static const struct rtnl_cmd_handler cmd_handlers[] = {
1443 {
1444 .cmd = NL_RTM_GETLINK,
1445 .name = "RTM_GETLINK",
1446 .cb = &rtnl_handle_getlink,
1447 .flags = RTNL_F_NOEPOCH | RTNL_F_ALLOW_NONVNET_JAIL,
1448 },
1449 {
1450 .cmd = NL_RTM_DELLINK,
1451 .name = "RTM_DELLINK",
1452 .cb = &rtnl_handle_dellink,
1453 .priv = PRIV_NET_IFDESTROY,
1454 .flags = RTNL_F_NOEPOCH,
1455 },
1456 {
1457 .cmd = NL_RTM_NEWLINK,
1458 .name = "RTM_NEWLINK",
1459 .cb = &rtnl_handle_newlink,
1460 .priv = PRIV_NET_IFCREATE,
1461 .flags = RTNL_F_NOEPOCH,
1462 },
1463 {
1464 .cmd = NL_RTM_GETADDR,
1465 .name = "RTM_GETADDR",
1466 .cb = &rtnl_handle_getaddr,
1467 .flags = RTNL_F_ALLOW_NONVNET_JAIL,
1468 },
1469 {
1470 .cmd = NL_RTM_NEWADDR,
1471 .name = "RTM_NEWADDR",
1472 .cb = &rtnl_handle_addr,
1473 .priv = PRIV_NET_ADDIFADDR,
1474 .flags = RTNL_F_NOEPOCH,
1475 },
1476 {
1477 .cmd = NL_RTM_DELADDR,
1478 .name = "RTM_DELADDR",
1479 .cb = &rtnl_handle_addr,
1480 .priv = PRIV_NET_DELIFADDR,
1481 .flags = RTNL_F_NOEPOCH,
1482 },
1483 };
1484
1485 static const struct nlhdr_parser *all_parsers[] = {
1486 &ifmsg_parser, &ifa_parser, &ifa_fbsd_parser,
1487 };
1488
1489 void
rtnl_iface_add_cloner(struct nl_cloner * cloner)1490 rtnl_iface_add_cloner(struct nl_cloner *cloner)
1491 {
1492 sx_xlock(&rtnl_cloner_lock);
1493 SLIST_INSERT_HEAD(&nl_cloners, cloner, next);
1494 sx_xunlock(&rtnl_cloner_lock);
1495 }
1496
1497 void
rtnl_iface_del_cloner(struct nl_cloner * cloner)1498 rtnl_iface_del_cloner(struct nl_cloner *cloner)
1499 {
1500 sx_xlock(&rtnl_cloner_lock);
1501 SLIST_REMOVE(&nl_cloners, cloner, nl_cloner, next);
1502 sx_xunlock(&rtnl_cloner_lock);
1503 }
1504
1505 void
rtnl_ifaces_init(void)1506 rtnl_ifaces_init(void)
1507 {
1508 ifattach_event = EVENTHANDLER_REGISTER(
1509 ifnet_arrival_event, rtnl_handle_ifattach, NULL,
1510 EVENTHANDLER_PRI_ANY);
1511 ifdetach_event = EVENTHANDLER_REGISTER(
1512 ifnet_departure_event, rtnl_handle_ifdetach, NULL,
1513 EVENTHANDLER_PRI_ANY);
1514 ifaddr_event = EVENTHANDLER_REGISTER(
1515 rt_addrmsg, rtnl_handle_ifaddr, NULL,
1516 EVENTHANDLER_PRI_ANY);
1517 iflink_event = EVENTHANDLER_REGISTER(
1518 ifnet_link_event, rtnl_handle_iflink, NULL,
1519 EVENTHANDLER_PRI_ANY);
1520 NL_VERIFY_PARSERS(all_parsers);
1521 rtnl_register_messages(cmd_handlers, nitems(cmd_handlers));
1522 }
1523
1524 void
rtnl_ifaces_destroy(void)1525 rtnl_ifaces_destroy(void)
1526 {
1527 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, ifattach_event);
1528 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_event);
1529 EVENTHANDLER_DEREGISTER(rt_addrmsg, ifaddr_event);
1530 EVENTHANDLER_DEREGISTER(ifnet_link_event, iflink_event);
1531 }
1532