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 nw->ifp = ifp;
367
368 if (nlmsg_end(nw))
369 return (true);
370
371 enomem:
372 NL_LOG(LOG_DEBUG, "unable to dump interface %s state (ENOMEM)", if_name(ifp));
373 nlmsg_abort(nw);
374 return (false);
375 }
376
377 static bool
check_ifmsg(void * hdr,struct nl_pstate * npt)378 check_ifmsg(void *hdr, struct nl_pstate *npt)
379 {
380 struct ifinfomsg *ifm = hdr;
381
382 if (ifm->__ifi_pad != 0 || ifm->ifi_type != 0 ||
383 ifm->ifi_flags != 0 || ifm->ifi_change != 0) {
384 nlmsg_report_err_msg(npt,
385 "strict checking: non-zero values in ifinfomsg header");
386 return (false);
387 }
388
389 return (true);
390 }
391
392 #define _IN(_field) offsetof(struct ifinfomsg, _field)
393 #define _OUT(_field) offsetof(struct nl_parsed_link, _field)
394 static const struct nlfield_parser nlf_p_if[] = {
395 { .off_in = _IN(ifi_type), .off_out = _OUT(ifi_type), .cb = nlf_get_u16 },
396 { .off_in = _IN(ifi_index), .off_out = _OUT(ifi_index), .cb = nlf_get_u32 },
397 { .off_in = _IN(ifi_flags), .off_out = _OUT(ifi_flags), .cb = nlf_get_u32 },
398 { .off_in = _IN(ifi_change), .off_out = _OUT(ifi_change), .cb = nlf_get_u32 },
399 };
400
401 static const struct nlattr_parser nla_p_linfo[] = {
402 { .type = IFLA_INFO_KIND, .off = _OUT(ifla_cloner), .cb = nlattr_get_stringn },
403 { .type = IFLA_INFO_DATA, .off = _OUT(ifla_idata), .cb = nlattr_get_nla },
404 };
405 NL_DECLARE_ATTR_PARSER(linfo_parser, nla_p_linfo);
406
407 static const struct nlattr_parser nla_p_if[] = {
408 { .type = IFLA_ADDRESS, .off = _OUT(ifla_address), .cb = nlattr_get_nla },
409 { .type = IFLA_IFNAME, .off = _OUT(ifla_ifname), .cb = nlattr_get_string },
410 { .type = IFLA_MTU, .off = _OUT(ifla_mtu), .cb = nlattr_get_uint32 },
411 { .type = IFLA_LINK, .off = _OUT(ifla_link), .cb = nlattr_get_uint32 },
412 { .type = IFLA_LINKINFO, .arg = &linfo_parser, .cb = nlattr_get_nested },
413 { .type = IFLA_IFALIAS, .off = _OUT(ifla_ifalias), .cb = nlattr_get_string },
414 { .type = IFLA_GROUP, .off = _OUT(ifla_group), .cb = nlattr_get_string },
415 { .type = IFLA_ALT_IFNAME, .off = _OUT(ifla_ifname), .cb = nlattr_get_string },
416 };
417 #undef _IN
418 #undef _OUT
419 NL_DECLARE_STRICT_PARSER(ifmsg_parser, struct ifinfomsg, check_ifmsg, nlf_p_if, nla_p_if);
420
421 static bool
match_iface(if_t ifp,void * _arg)422 match_iface(if_t ifp, void *_arg)
423 {
424 struct nl_parsed_link *attrs = (struct nl_parsed_link *)_arg;
425
426 if (attrs->ifi_index != 0 && attrs->ifi_index != if_getindex(ifp))
427 return (false);
428 if (attrs->ifi_type != 0 && attrs->ifi_index != if_gettype(ifp))
429 return (false);
430 if (attrs->ifla_ifname != NULL && strcmp(attrs->ifla_ifname, if_name(ifp)))
431 return (false);
432 /* TODO: add group match */
433
434 return (true);
435 }
436
437 static int
dump_cb(if_t ifp,void * _arg)438 dump_cb(if_t ifp, void *_arg)
439 {
440 struct netlink_walkargs *wa = (struct netlink_walkargs *)_arg;
441 if (!dump_iface(wa->nw, ifp, &wa->hdr, 0))
442 return (ENOMEM);
443 return (0);
444 }
445
446 /*
447 * {nlmsg_len=52, nlmsg_type=RTM_GETLINK, nlmsg_flags=NLM_F_REQUEST, nlmsg_seq=1662842818, nlmsg_pid=0},
448 * {ifi_family=AF_PACKET, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0},
449 * [
450 * [{nla_len=10, nla_type=IFLA_IFNAME}, "vnet9"],
451 * [{nla_len=8, nla_type=IFLA_EXT_MASK}, RTEXT_FILTER_VF]
452 * ]
453 */
454 static int
rtnl_handle_getlink(struct nlmsghdr * hdr,struct nlpcb * nlp,struct nl_pstate * npt)455 rtnl_handle_getlink(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt)
456 {
457 struct epoch_tracker et;
458 if_t ifp;
459 int error = 0;
460
461 struct nl_parsed_link attrs = {};
462 error = nl_parse_nlmsg(hdr, &ifmsg_parser, npt, &attrs);
463 if (error != 0)
464 return (error);
465
466 struct netlink_walkargs wa = {
467 .so = nlp,
468 .nw = npt->nw,
469 .hdr.nlmsg_pid = hdr->nlmsg_pid,
470 .hdr.nlmsg_seq = hdr->nlmsg_seq,
471 .hdr.nlmsg_flags = hdr->nlmsg_flags,
472 .hdr.nlmsg_type = NL_RTM_NEWLINK,
473 };
474
475 /* Fast track for an interface w/ explicit name or index match */
476 if ((attrs.ifi_index != 0) || (attrs.ifla_ifname != NULL)) {
477 if (attrs.ifi_index != 0) {
478 NLP_LOG(LOG_DEBUG3, nlp, "fast track -> searching index %u",
479 attrs.ifi_index);
480 NET_EPOCH_ENTER(et);
481 ifp = ifnet_byindex_ref(attrs.ifi_index);
482 NET_EPOCH_EXIT(et);
483 } else {
484 NLP_LOG(LOG_DEBUG3, nlp, "fast track -> searching name %s",
485 attrs.ifla_ifname);
486 ifp = ifunit_ref(attrs.ifla_ifname);
487 }
488
489 if (ifp != NULL) {
490 if (match_iface(ifp, &attrs)) {
491 if (!dump_iface(wa.nw, ifp, &wa.hdr, 0))
492 error = ENOMEM;
493 } else
494 error = ENODEV;
495 if_rele(ifp);
496 } else
497 error = ENODEV;
498 return (error);
499 }
500
501 /* Always treat non-direct-match as a multipart message */
502 wa.hdr.nlmsg_flags |= NLM_F_MULTI;
503
504 /*
505 * Fetching some link properties require performing ioctl's that may be blocking.
506 * Address it by saving referenced pointers of the matching links,
507 * exiting from epoch and going through the list one-by-one.
508 */
509
510 NL_LOG(LOG_DEBUG2, "Start dump");
511 if_foreach_sleep(match_iface, &attrs, dump_cb, &wa);
512 NL_LOG(LOG_DEBUG2, "End dump, iterated %d dumped %d", wa.count, wa.dumped);
513
514 if (!nlmsg_end_dump(wa.nw, error, &wa.hdr)) {
515 NL_LOG(LOG_DEBUG, "Unable to finalize the dump");
516 return (ENOMEM);
517 }
518
519 return (error);
520 }
521
522 /*
523 * sendmsg(3, {msg_name={sa_family=AF_NETLINK, nl_pid=0, nl_groups=00000000}, msg_namelen=12, msg_iov=[{iov_base=[
524 * {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},
525 * {ifi_family=AF_UNSPEC, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0},
526 * {nla_len=11, nla_type=IFLA_IFNAME}, "dummy0"],
527 * [
528 * {nla_len=16, nla_type=IFLA_LINKINFO},
529 * [
530 * {nla_len=9, nla_type=IFLA_INFO_KIND}, "dummy"...
531 * ]
532 * ]
533 */
534
535 static int
rtnl_handle_dellink(struct nlmsghdr * hdr,struct nlpcb * nlp,struct nl_pstate * npt)536 rtnl_handle_dellink(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt)
537 {
538 struct epoch_tracker et;
539 if_t ifp;
540 int error;
541
542 struct nl_parsed_link attrs = {};
543 error = nl_parse_nlmsg(hdr, &ifmsg_parser, npt, &attrs);
544 if (error != 0)
545 return (error);
546
547 NET_EPOCH_ENTER(et);
548 ifp = ifnet_byindex_ref(attrs.ifi_index);
549 NET_EPOCH_EXIT(et);
550 if (ifp == NULL) {
551 NLP_LOG(LOG_DEBUG, nlp, "unable to find interface %u", attrs.ifi_index);
552 return (ENOENT);
553 }
554 NLP_LOG(LOG_DEBUG3, nlp, "mapped ifindex %u to %s", attrs.ifi_index, if_name(ifp));
555
556 sx_xlock(&ifnet_detach_sxlock);
557 error = if_clone_destroy(if_name(ifp));
558 sx_xunlock(&ifnet_detach_sxlock);
559
560 NLP_LOG(LOG_DEBUG2, nlp, "deleting interface %s returned %d", if_name(ifp), error);
561
562 if_rele(ifp);
563 return (error);
564 }
565
566 /*
567 * New link:
568 * type=RTM_NEWLINK, flags=NLM_F_REQUEST|NLM_F_ACK|NLM_F_EXCL|NLM_F_CREATE, seq=1668185590, pid=0},
569 * {ifi_family=AF_UNSPEC, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0}
570 * [
571 * {{nla_len=8, nla_type=IFLA_MTU}, 123},
572 * {{nla_len=10, nla_type=IFLA_IFNAME}, "vlan1"},
573 * {{nla_len=24, nla_type=IFLA_LINKINFO},
574 * [
575 * {{nla_len=8, nla_type=IFLA_INFO_KIND}, "vlan"...},
576 * {{nla_len=12, nla_type=IFLA_INFO_DATA}, "\x06\x00\x01\x00\x7b\x00\x00\x00"}]}]}
577 *
578 * Update link:
579 * type=RTM_NEWLINK, flags=NLM_F_REQUEST|NLM_F_ACK, seq=1668185923, pid=0},
580 * {ifi_family=AF_UNSPEC, ifi_type=ARPHRD_NETROM, ifi_index=if_nametoindex("lo"), ifi_flags=0, ifi_change=0},
581 * {{nla_len=8, nla_type=IFLA_MTU}, 123}}
582 *
583 *
584 * Check command availability:
585 * type=RTM_NEWLINK, flags=NLM_F_REQUEST|NLM_F_ACK, seq=0, pid=0},
586 * {ifi_family=AF_UNSPEC, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0}
587 */
588
589
590 static int
create_link(struct nlmsghdr * hdr,struct nl_parsed_link * lattrs,struct nlattr_bmask * bm,struct nlpcb * nlp,struct nl_pstate * npt)591 create_link(struct nlmsghdr *hdr, struct nl_parsed_link *lattrs,
592 struct nlattr_bmask *bm, struct nlpcb *nlp, struct nl_pstate *npt)
593 {
594 if (lattrs->ifla_ifname == NULL || strlen(lattrs->ifla_ifname) == 0) {
595 NLMSG_REPORT_ERR_MSG(npt, "empty IFLA_IFNAME attribute");
596 return (EINVAL);
597 }
598 if (lattrs->ifla_cloner == NULL || strlen(lattrs->ifla_cloner) == 0) {
599 NLMSG_REPORT_ERR_MSG(npt, "empty IFLA_INFO_KIND attribute");
600 return (EINVAL);
601 }
602
603 struct ifc_data_nl ifd = {
604 .flags = IFC_F_CREATE,
605 .lattrs = lattrs,
606 .bm = bm,
607 .npt = npt,
608 };
609 if (ifc_create_ifp_nl(lattrs->ifla_ifname, &ifd) && ifd.error == 0)
610 nl_store_ifp_cookie(npt, ifd.ifp);
611
612 return (ifd.error);
613 }
614
615 static int
modify_link(struct nlmsghdr * hdr,struct nl_parsed_link * lattrs,struct nlattr_bmask * bm,struct nlpcb * nlp,struct nl_pstate * npt)616 modify_link(struct nlmsghdr *hdr, struct nl_parsed_link *lattrs,
617 struct nlattr_bmask *bm, struct nlpcb *nlp, struct nl_pstate *npt)
618 {
619 if_t ifp = NULL;
620 struct epoch_tracker et;
621
622 if (lattrs->ifi_index == 0 && lattrs->ifla_ifname == NULL) {
623 /*
624 * Applications like ip(8) verify RTM_NEWLINK command
625 * existence by calling it with empty arguments. Always
626 * return "innocent" error in that case.
627 */
628 NLMSG_REPORT_ERR_MSG(npt, "empty ifi_index field");
629 return (EPERM);
630 }
631
632 if (lattrs->ifi_index != 0) {
633 NET_EPOCH_ENTER(et);
634 ifp = ifnet_byindex_ref(lattrs->ifi_index);
635 NET_EPOCH_EXIT(et);
636 if (ifp == NULL) {
637 NLMSG_REPORT_ERR_MSG(npt, "unable to find interface #%u",
638 lattrs->ifi_index);
639 return (ENOENT);
640 }
641 }
642
643 if (ifp == NULL && lattrs->ifla_ifname != NULL) {
644 ifp = ifunit_ref(lattrs->ifla_ifname);
645 if (ifp == NULL) {
646 NLMSG_REPORT_ERR_MSG(npt, "unable to find interface %s",
647 lattrs->ifla_ifname);
648 return (ENOENT);
649 }
650 }
651
652 MPASS(ifp != NULL);
653
654 /*
655 * Modification request can address either
656 * 1) cloned interface, in which case we call the cloner-specific
657 * modification routine
658 * or
659 * 2) non-cloned (e.g. "physical") interface, in which case we call
660 * generic modification routine
661 */
662 struct ifc_data_nl ifd = { .lattrs = lattrs, .bm = bm, .npt = npt };
663 if (!ifc_modify_ifp_nl(ifp, &ifd))
664 ifd.error = nl_modify_ifp_generic(ifp, lattrs, bm, npt);
665
666 if_rele(ifp);
667
668 return (ifd.error);
669 }
670
671
672 static int
rtnl_handle_newlink(struct nlmsghdr * hdr,struct nlpcb * nlp,struct nl_pstate * npt)673 rtnl_handle_newlink(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt)
674 {
675 struct nlattr_bmask bm;
676 int error;
677
678 struct nl_parsed_link attrs = {};
679 error = nl_parse_nlmsg(hdr, &ifmsg_parser, npt, &attrs);
680 if (error != 0)
681 return (error);
682 nl_get_attrs_bmask_nlmsg(hdr, &ifmsg_parser, &bm);
683
684 if (hdr->nlmsg_flags & NLM_F_CREATE)
685 return (create_link(hdr, &attrs, &bm, nlp, npt));
686 else
687 return (modify_link(hdr, &attrs, &bm, nlp, npt));
688 }
689
690 static void
set_scope6(struct sockaddr * sa,uint32_t ifindex)691 set_scope6(struct sockaddr *sa, uint32_t ifindex)
692 {
693 #ifdef INET6
694 if (sa != NULL && sa->sa_family == AF_INET6) {
695 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
696
697 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr))
698 in6_set_unicast_scopeid(&sa6->sin6_addr, ifindex);
699 }
700 #endif
701 }
702
703 static bool
check_sa_family(const struct sockaddr * sa,int family,const char * attr_name,struct nl_pstate * npt)704 check_sa_family(const struct sockaddr *sa, int family, const char *attr_name,
705 struct nl_pstate *npt)
706 {
707 if (sa == NULL || sa->sa_family == family)
708 return (true);
709
710 nlmsg_report_err_msg(npt, "wrong family for %s attribute: %d != %d",
711 attr_name, family, sa->sa_family);
712 return (false);
713 }
714
715 struct nl_parsed_ifa {
716 uint8_t ifa_family;
717 uint8_t ifa_prefixlen;
718 uint8_t ifa_scope;
719 uint32_t ifa_index;
720 uint32_t ifa_flags;
721 uint32_t ifaf_vhid;
722 uint32_t ifaf_flags;
723 struct sockaddr *ifa_address;
724 struct sockaddr *ifa_local;
725 struct sockaddr *ifa_broadcast;
726 struct ifa_cacheinfo *ifa_cacheinfo;
727 struct sockaddr *f_ifa_addr;
728 struct sockaddr *f_ifa_dst;
729 };
730
731 static int
nlattr_get_cinfo(struct nlattr * nla,struct nl_pstate * npt,const void * arg __unused,void * target)732 nlattr_get_cinfo(struct nlattr *nla, struct nl_pstate *npt,
733 const void *arg __unused, void *target)
734 {
735 if (__predict_false(NLA_DATA_LEN(nla) != sizeof(struct ifa_cacheinfo))) {
736 NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not ifa_cacheinfo",
737 nla->nla_type, NLA_DATA_LEN(nla));
738 return (EINVAL);
739 }
740 *((struct ifa_cacheinfo **)target) = (struct ifa_cacheinfo *)NL_RTA_DATA(nla);
741 return (0);
742 }
743
744 #define _IN(_field) offsetof(struct ifaddrmsg, _field)
745 #define _OUT(_field) offsetof(struct nl_parsed_ifa, _field)
746 static const struct nlfield_parser nlf_p_ifa[] = {
747 { .off_in = _IN(ifa_family), .off_out = _OUT(ifa_family), .cb = nlf_get_u8 },
748 { .off_in = _IN(ifa_prefixlen), .off_out = _OUT(ifa_prefixlen), .cb = nlf_get_u8 },
749 { .off_in = _IN(ifa_scope), .off_out = _OUT(ifa_scope), .cb = nlf_get_u8 },
750 { .off_in = _IN(ifa_flags), .off_out = _OUT(ifa_flags), .cb = nlf_get_u8_u32 },
751 { .off_in = _IN(ifa_index), .off_out = _OUT(ifa_index), .cb = nlf_get_u32 },
752 };
753
754 static const struct nlattr_parser nla_p_ifa_fbsd[] = {
755 { .type = IFAF_VHID, .off = _OUT(ifaf_vhid), .cb = nlattr_get_uint32 },
756 { .type = IFAF_FLAGS, .off = _OUT(ifaf_flags), .cb = nlattr_get_uint32 },
757 };
758 NL_DECLARE_ATTR_PARSER(ifa_fbsd_parser, nla_p_ifa_fbsd);
759
760 static const struct nlattr_parser nla_p_ifa[] = {
761 { .type = IFA_ADDRESS, .off = _OUT(ifa_address), .cb = nlattr_get_ip },
762 { .type = IFA_LOCAL, .off = _OUT(ifa_local), .cb = nlattr_get_ip },
763 { .type = IFA_BROADCAST, .off = _OUT(ifa_broadcast), .cb = nlattr_get_ip },
764 { .type = IFA_CACHEINFO, .off = _OUT(ifa_cacheinfo), .cb = nlattr_get_cinfo },
765 { .type = IFA_FLAGS, .off = _OUT(ifa_flags), .cb = nlattr_get_uint32 },
766 { .type = IFA_FREEBSD, .arg = &ifa_fbsd_parser, .cb = nlattr_get_nested },
767 };
768 #undef _IN
769 #undef _OUT
770
771 static bool
post_p_ifa(void * _attrs,struct nl_pstate * npt)772 post_p_ifa(void *_attrs, struct nl_pstate *npt)
773 {
774 struct nl_parsed_ifa *attrs = (struct nl_parsed_ifa *)_attrs;
775
776 if (!check_sa_family(attrs->ifa_address, attrs->ifa_family, "IFA_ADDRESS", npt))
777 return (false);
778 if (!check_sa_family(attrs->ifa_local, attrs->ifa_family, "IFA_LOCAL", npt))
779 return (false);
780 if (!check_sa_family(attrs->ifa_broadcast, attrs->ifa_family, "IFA_BROADADDR", npt))
781 return (false);
782
783 set_scope6(attrs->ifa_address, attrs->ifa_index);
784 set_scope6(attrs->ifa_local, attrs->ifa_index);
785
786 return (true);
787 }
788
789 NL_DECLARE_PARSER_EXT(ifa_parser, struct ifaddrmsg, NULL, nlf_p_ifa, nla_p_ifa, post_p_ifa);
790
791
792 /*
793
794 {ifa_family=AF_INET, ifa_prefixlen=8, ifa_flags=IFA_F_PERMANENT, ifa_scope=RT_SCOPE_HOST, ifa_index=if_nametoindex("lo")},
795 [
796 {{nla_len=8, nla_type=IFA_ADDRESS}, inet_addr("127.0.0.1")},
797 {{nla_len=8, nla_type=IFA_LOCAL}, inet_addr("127.0.0.1")},
798 {{nla_len=7, nla_type=IFA_LABEL}, "lo"},
799 {{nla_len=8, nla_type=IFA_FLAGS}, IFA_F_PERMANENT},
800 {{nla_len=20, nla_type=IFA_CACHEINFO}, {ifa_prefered=4294967295, ifa_valid=4294967295, cstamp=3619, tstamp=3619}}]},
801 ---
802
803 {{len=72, type=RTM_NEWADDR, flags=NLM_F_MULTI, seq=1642191126, pid=566735},
804 {ifa_family=AF_INET6, ifa_prefixlen=96, ifa_flags=IFA_F_PERMANENT, ifa_scope=RT_SCOPE_UNIVERSE, ifa_index=if_nametoindex("virbr0")},
805 [
806 {{nla_len=20, nla_type=IFA_ADDRESS}, inet_pton(AF_INET6, "2a01:4f8:13a:70c:ffff::1")},
807 {{nla_len=20, nla_type=IFA_CACHEINFO}, {ifa_prefered=4294967295, ifa_valid=4294967295, cstamp=4283, tstamp=4283}},
808 {{nla_len=8, nla_type=IFA_FLAGS}, IFA_F_PERMANENT}]},
809 */
810
811 static uint8_t
ifa_get_scope(const struct ifaddr * ifa)812 ifa_get_scope(const struct ifaddr *ifa)
813 {
814 const struct sockaddr *sa;
815 uint8_t addr_scope = RT_SCOPE_UNIVERSE;
816
817 sa = ifa->ifa_addr;
818 switch (sa->sa_family) {
819 #ifdef INET
820 case AF_INET:
821 {
822 struct in_addr addr;
823 addr = ((const struct sockaddr_in *)sa)->sin_addr;
824 if (IN_LOOPBACK(ntohl(addr.s_addr)))
825 addr_scope = RT_SCOPE_HOST;
826 else if (IN_LINKLOCAL(ntohl(addr.s_addr)))
827 addr_scope = RT_SCOPE_LINK;
828 break;
829 }
830 #endif
831 #ifdef INET6
832 case AF_INET6:
833 {
834 const struct in6_addr *addr;
835 addr = &((const struct sockaddr_in6 *)sa)->sin6_addr;
836 if (IN6_IS_ADDR_LOOPBACK(addr))
837 addr_scope = RT_SCOPE_HOST;
838 else if (IN6_IS_ADDR_LINKLOCAL(addr))
839 addr_scope = RT_SCOPE_LINK;
840 break;
841 }
842 #endif
843 }
844
845 return (addr_scope);
846 }
847
848 #ifdef INET6
849 static uint8_t
inet6_get_plen(const struct in6_addr * addr)850 inet6_get_plen(const struct in6_addr *addr)
851 {
852
853 return (bitcount32(addr->s6_addr32[0]) + bitcount32(addr->s6_addr32[1]) +
854 bitcount32(addr->s6_addr32[2]) + bitcount32(addr->s6_addr32[3]));
855 }
856 #endif
857
858 static uint8_t
get_sa_plen(const struct sockaddr * sa)859 get_sa_plen(const struct sockaddr *sa)
860 {
861 #ifdef INET
862 const struct in_addr *paddr;
863 #endif
864 #ifdef INET6
865 const struct in6_addr *paddr6;
866 #endif
867
868 switch (sa->sa_family) {
869 #ifdef INET
870 case AF_INET:
871 paddr = &(((const struct sockaddr_in *)sa)->sin_addr);
872 return bitcount32(paddr->s_addr);
873 #endif
874 #ifdef INET6
875 case AF_INET6:
876 paddr6 = &(((const struct sockaddr_in6 *)sa)->sin6_addr);
877 return inet6_get_plen(paddr6);
878 #endif
879 }
880
881 return (0);
882 }
883
884 #ifdef INET6
885 static uint32_t
in6_flags_to_nl(uint32_t flags)886 in6_flags_to_nl(uint32_t flags)
887 {
888 uint32_t nl_flags = 0;
889
890 if (flags & IN6_IFF_TEMPORARY)
891 nl_flags |= IFA_F_TEMPORARY;
892 if (flags & IN6_IFF_NODAD)
893 nl_flags |= IFA_F_NODAD;
894 if (flags & IN6_IFF_DEPRECATED)
895 nl_flags |= IFA_F_DEPRECATED;
896 if (flags & IN6_IFF_TENTATIVE)
897 nl_flags |= IFA_F_TENTATIVE;
898 if ((flags & (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) == 0)
899 flags |= IFA_F_PERMANENT;
900 if (flags & IN6_IFF_DUPLICATED)
901 flags |= IFA_F_DADFAILED;
902 return (nl_flags);
903 }
904
905 static uint32_t
nl_flags_to_in6(uint32_t flags)906 nl_flags_to_in6(uint32_t flags)
907 {
908 uint32_t in6_flags = 0;
909
910 if (flags & IFA_F_TEMPORARY)
911 in6_flags |= IN6_IFF_TEMPORARY;
912 if (flags & IFA_F_NODAD)
913 in6_flags |= IN6_IFF_NODAD;
914 if (flags & IFA_F_DEPRECATED)
915 in6_flags |= IN6_IFF_DEPRECATED;
916 if (flags & IFA_F_TENTATIVE)
917 in6_flags |= IN6_IFF_TENTATIVE;
918 if (flags & IFA_F_DADFAILED)
919 in6_flags |= IN6_IFF_DUPLICATED;
920
921 return (in6_flags);
922 }
923
924 static void
export_cache_info6(struct nl_writer * nw,const struct in6_ifaddr * ia)925 export_cache_info6(struct nl_writer *nw, const struct in6_ifaddr *ia)
926 {
927 struct ifa_cacheinfo ci = {
928 .cstamp = ia->ia6_createtime * 1000,
929 .tstamp = ia->ia6_updatetime * 1000,
930 .ifa_prefered = ia->ia6_lifetime.ia6t_pltime,
931 .ifa_valid = ia->ia6_lifetime.ia6t_vltime,
932 };
933
934 nlattr_add(nw, IFA_CACHEINFO, sizeof(ci), &ci);
935 }
936 #endif
937
938 static void
export_cache_info(struct nl_writer * nw,struct ifaddr * ifa)939 export_cache_info(struct nl_writer *nw, struct ifaddr *ifa)
940 {
941 switch (ifa->ifa_addr->sa_family) {
942 #ifdef INET6
943 case AF_INET6:
944 export_cache_info6(nw, (struct in6_ifaddr *)ifa);
945 break;
946 #endif
947 }
948 }
949
950 /*
951 * {'attrs': [('IFA_ADDRESS', '12.0.0.1'),
952 ('IFA_LOCAL', '12.0.0.1'),
953 ('IFA_LABEL', 'eth10'),
954 ('IFA_FLAGS', 128),
955 ('IFA_CACHEINFO', {'ifa_preferred': 4294967295, 'ifa_valid': 4294967295, 'cstamp': 63745746, 'tstamp': 63745746})],
956 */
957 static bool
dump_iface_addr(struct nl_writer * nw,if_t ifp,struct ifaddr * ifa,const struct nlmsghdr * hdr)958 dump_iface_addr(struct nl_writer *nw, if_t ifp, struct ifaddr *ifa,
959 const struct nlmsghdr *hdr)
960 {
961 struct ifaddrmsg *ifamsg;
962 struct sockaddr *sa = ifa->ifa_addr;
963 struct sockaddr *sa_dst = ifa->ifa_dstaddr;
964
965 NL_LOG(LOG_DEBUG3, "dumping ifa %p type %s(%d) for interface %s",
966 ifa, rib_print_family(sa->sa_family), sa->sa_family, if_name(ifp));
967
968 if (!nlmsg_reply(nw, hdr, sizeof(struct ifaddrmsg)))
969 goto enomem;
970
971 ifamsg = nlmsg_reserve_object(nw, struct ifaddrmsg);
972 ifamsg->ifa_family = sa->sa_family;
973 ifamsg->ifa_prefixlen = get_sa_plen(ifa->ifa_netmask);
974 ifamsg->ifa_flags = 0; // ifa_flags is useless
975 ifamsg->ifa_scope = ifa_get_scope(ifa);
976 ifamsg->ifa_index = if_getindex(ifp);
977
978 if ((if_getflags(ifp) & IFF_POINTOPOINT) && sa_dst != NULL && sa_dst->sa_family != 0) {
979 /* P2P interface may have IPv6 LL with no dst address */
980 dump_sa(nw, IFA_ADDRESS, sa_dst);
981 dump_sa(nw, IFA_LOCAL, sa);
982 } else {
983 dump_sa(nw, IFA_ADDRESS, sa);
984 #ifdef INET
985 /*
986 * In most cases, IFA_ADDRESS == IFA_LOCAL
987 * Skip IFA_LOCAL for anything except INET
988 */
989 if (sa->sa_family == AF_INET)
990 dump_sa(nw, IFA_LOCAL, sa);
991 #endif
992 }
993 if (if_getflags(ifp) & IFF_BROADCAST)
994 dump_sa(nw, IFA_BROADCAST, ifa->ifa_broadaddr);
995
996 nlattr_add_string(nw, IFA_LABEL, if_name(ifp));
997
998 uint32_t nl_ifa_flags = 0;
999 #ifdef INET6
1000 if (sa->sa_family == AF_INET6) {
1001 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1002 nl_ifa_flags = in6_flags_to_nl(ia->ia6_flags);
1003 }
1004 #endif
1005 nlattr_add_u32(nw, IFA_FLAGS, nl_ifa_flags);
1006
1007 export_cache_info(nw, ifa);
1008
1009 /* Store FreeBSD-specific attributes */
1010 int off = nlattr_add_nested(nw, IFA_FREEBSD);
1011 if (off != 0) {
1012 if (ifa->ifa_carp != NULL && carp_get_vhid_p != NULL) {
1013 uint32_t vhid = (uint32_t)(*carp_get_vhid_p)(ifa);
1014 nlattr_add_u32(nw, IFAF_VHID, vhid);
1015 }
1016 #ifdef INET6
1017 if (sa->sa_family == AF_INET6) {
1018 uint32_t ifa_flags = ((struct in6_ifaddr *)ifa)->ia6_flags;
1019
1020 nlattr_add_u32(nw, IFAF_FLAGS, ifa_flags);
1021 }
1022 #endif
1023
1024 nlattr_set_len(nw, off);
1025 }
1026
1027 if (nlmsg_end(nw))
1028 return (true);
1029 enomem:
1030 NL_LOG(LOG_DEBUG, "Failed to dump ifa type %s(%d) for interface %s",
1031 rib_print_family(sa->sa_family), sa->sa_family, if_name(ifp));
1032 nlmsg_abort(nw);
1033 return (false);
1034 }
1035
1036 static int
dump_iface_addrs(struct netlink_walkargs * wa,if_t ifp)1037 dump_iface_addrs(struct netlink_walkargs *wa, if_t ifp)
1038 {
1039 struct ifaddr *ifa;
1040 struct ifa_iter it;
1041 int error = 0;
1042
1043 for (ifa = ifa_iter_start(ifp, &it); ifa != NULL; ifa = ifa_iter_next(&it)) {
1044 if (wa->family != 0 && wa->family != ifa->ifa_addr->sa_family)
1045 continue;
1046 if (ifa->ifa_addr->sa_family == AF_LINK)
1047 continue;
1048 if (prison_if(wa->cred, ifa->ifa_addr) != 0)
1049 continue;
1050 wa->count++;
1051 if (!dump_iface_addr(wa->nw, ifp, ifa, &wa->hdr)) {
1052 error = ENOMEM;
1053 break;
1054 }
1055 wa->dumped++;
1056 }
1057 ifa_iter_finish(&it);
1058
1059 return (error);
1060 }
1061
1062 static int
rtnl_handle_getaddr(struct nlmsghdr * hdr,struct nlpcb * nlp,struct nl_pstate * npt)1063 rtnl_handle_getaddr(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt)
1064 {
1065 if_t ifp;
1066 int error = 0;
1067
1068 struct nl_parsed_ifa attrs = {};
1069 error = nl_parse_nlmsg(hdr, &ifa_parser, npt, &attrs);
1070 if (error != 0)
1071 return (error);
1072
1073 struct netlink_walkargs wa = {
1074 .so = nlp,
1075 .nw = npt->nw,
1076 .cred = nlp_get_cred(nlp),
1077 .family = attrs.ifa_family,
1078 .hdr.nlmsg_pid = hdr->nlmsg_pid,
1079 .hdr.nlmsg_seq = hdr->nlmsg_seq,
1080 .hdr.nlmsg_flags = hdr->nlmsg_flags | NLM_F_MULTI,
1081 .hdr.nlmsg_type = NL_RTM_NEWADDR,
1082 };
1083
1084 NL_LOG(LOG_DEBUG2, "Start dump");
1085
1086 if (attrs.ifa_index != 0) {
1087 ifp = ifnet_byindex(attrs.ifa_index);
1088 if (ifp == NULL)
1089 error = ENOENT;
1090 else
1091 error = dump_iface_addrs(&wa, ifp);
1092 } else {
1093 struct if_iter it;
1094
1095 for (ifp = if_iter_start(&it); ifp != NULL; ifp = if_iter_next(&it)) {
1096 error = dump_iface_addrs(&wa, ifp);
1097 if (error != 0)
1098 break;
1099 }
1100 if_iter_finish(&it);
1101 }
1102
1103 NL_LOG(LOG_DEBUG2, "End dump, iterated %d dumped %d", wa.count, wa.dumped);
1104
1105 if (!nlmsg_end_dump(wa.nw, error, &wa.hdr)) {
1106 NL_LOG(LOG_DEBUG, "Unable to finalize the dump");
1107 return (ENOMEM);
1108 }
1109
1110 return (error);
1111 }
1112
1113 #ifdef INET
1114 static int
handle_newaddr_inet(struct nlmsghdr * hdr,struct nl_parsed_ifa * attrs,if_t ifp,struct nlpcb * nlp,struct nl_pstate * npt)1115 handle_newaddr_inet(struct nlmsghdr *hdr, struct nl_parsed_ifa *attrs,
1116 if_t ifp, struct nlpcb *nlp, struct nl_pstate *npt)
1117 {
1118 int plen = attrs->ifa_prefixlen;
1119 int if_flags = if_getflags(ifp);
1120 struct sockaddr_in *addr, *dst;
1121
1122 if (plen > 32) {
1123 nlmsg_report_err_msg(npt, "invalid ifa_prefixlen");
1124 return (EINVAL);
1125 };
1126
1127 if (if_flags & IFF_POINTOPOINT) {
1128 /*
1129 * Only P2P IFAs are allowed by the implementation.
1130 */
1131 if (attrs->ifa_address == NULL || attrs->ifa_local == NULL) {
1132 nlmsg_report_err_msg(npt, "Empty IFA_LOCAL/IFA_ADDRESS");
1133 return (EINVAL);
1134 }
1135 addr = (struct sockaddr_in *)attrs->ifa_local;
1136 dst = (struct sockaddr_in *)attrs->ifa_address;
1137 } else {
1138 /*
1139 * Map the Netlink attributes to FreeBSD ifa layout.
1140 * If only IFA_ADDRESS or IFA_LOCAL is set OR
1141 * both are set to the same value => ifa is not p2p
1142 * and the attribute value contains interface address.
1143 *
1144 * Otherwise (both IFA_ADDRESS and IFA_LOCAL are set and
1145 * different), IFA_LOCAL contains an interface address and
1146 * IFA_ADDRESS contains peer address.
1147 */
1148 addr = (struct sockaddr_in *)attrs->ifa_local;
1149 if (addr == NULL)
1150 addr = (struct sockaddr_in *)attrs->ifa_address;
1151
1152 if (addr == NULL) {
1153 nlmsg_report_err_msg(npt, "Empty IFA_LOCAL/IFA_ADDRESS");
1154 return (EINVAL);
1155 }
1156
1157 /* Generate broadcast address if not set */
1158 if ((if_flags & IFF_BROADCAST) && attrs->ifa_broadcast == NULL) {
1159 uint32_t s_baddr;
1160 struct sockaddr_in *sin_brd;
1161
1162 if (plen == 31)
1163 s_baddr = INADDR_BROADCAST; /* RFC 3021 */
1164 else {
1165 uint32_t s_mask;
1166
1167 s_mask = htonl(plen ? ~((1 << (32 - plen)) - 1) : 0);
1168 s_baddr = addr->sin_addr.s_addr | ~s_mask;
1169 }
1170
1171 sin_brd = (struct sockaddr_in *)npt_alloc(npt, sizeof(*sin_brd));
1172 if (sin_brd == NULL)
1173 return (ENOMEM);
1174 sin_brd->sin_family = AF_INET;
1175 sin_brd->sin_len = sizeof(*sin_brd);
1176 sin_brd->sin_addr.s_addr = s_baddr;
1177 attrs->ifa_broadcast = (struct sockaddr *)sin_brd;
1178 }
1179 dst = (struct sockaddr_in *)attrs->ifa_broadcast;
1180 }
1181
1182 struct sockaddr_in mask = {
1183 .sin_len = sizeof(struct sockaddr_in),
1184 .sin_family = AF_INET,
1185 .sin_addr.s_addr = htonl(plen ? ~((1 << (32 - plen)) - 1) : 0),
1186 };
1187 struct in_aliasreq req = {
1188 .ifra_addr = *addr,
1189 .ifra_mask = mask,
1190 .ifra_vhid = attrs->ifaf_vhid,
1191 };
1192 if (dst != NULL)
1193 req.ifra_dstaddr = *dst;
1194
1195 return (in_control_ioctl(SIOCAIFADDR, &req, ifp, nlp_get_cred(nlp)));
1196 }
1197
1198 static int
handle_deladdr_inet(struct nlmsghdr * hdr,struct nl_parsed_ifa * attrs,if_t ifp,struct nlpcb * nlp,struct nl_pstate * npt)1199 handle_deladdr_inet(struct nlmsghdr *hdr, struct nl_parsed_ifa *attrs,
1200 if_t ifp, struct nlpcb *nlp, struct nl_pstate *npt)
1201 {
1202 struct sockaddr *addr = attrs->ifa_local;
1203
1204 if (addr == NULL)
1205 addr = attrs->ifa_address;
1206
1207 if (addr == NULL) {
1208 nlmsg_report_err_msg(npt, "empty IFA_ADDRESS/IFA_LOCAL");
1209 return (EINVAL);
1210 }
1211
1212 struct ifreq req = { .ifr_addr = *addr };
1213
1214 return (in_control_ioctl(SIOCDIFADDR, &req, ifp, nlp_get_cred(nlp)));
1215 }
1216 #endif
1217
1218 #ifdef INET6
1219 static int
handle_newaddr_inet6(struct nlmsghdr * hdr,struct nl_parsed_ifa * attrs,if_t ifp,struct nlpcb * nlp,struct nl_pstate * npt)1220 handle_newaddr_inet6(struct nlmsghdr *hdr, struct nl_parsed_ifa *attrs,
1221 if_t ifp, struct nlpcb *nlp, struct nl_pstate *npt)
1222 {
1223 struct sockaddr_in6 *addr, *dst;
1224
1225 if (attrs->ifa_prefixlen > 128) {
1226 nlmsg_report_err_msg(npt, "invalid ifa_prefixlen");
1227 return (EINVAL);
1228 }
1229
1230 /*
1231 * In IPv6 implementation, adding non-P2P address to the P2P interface
1232 * is allowed.
1233 */
1234 addr = (struct sockaddr_in6 *)(attrs->ifa_local);
1235 dst = (struct sockaddr_in6 *)(attrs->ifa_address);
1236
1237 if (addr == NULL) {
1238 addr = dst;
1239 dst = NULL;
1240 } else if (dst != NULL) {
1241 if (IN6_ARE_ADDR_EQUAL(&addr->sin6_addr, &dst->sin6_addr)) {
1242 /*
1243 * Sometimes Netlink users fills in both attributes
1244 * with the same address. It still means "non-p2p".
1245 */
1246 dst = NULL;
1247 }
1248 }
1249
1250 if (addr == NULL) {
1251 nlmsg_report_err_msg(npt, "Empty IFA_LOCAL/IFA_ADDRESS");
1252 return (EINVAL);
1253 }
1254
1255 uint32_t flags = nl_flags_to_in6(attrs->ifa_flags) | attrs->ifaf_flags;
1256
1257 uint32_t pltime = 0, vltime = 0;
1258 if (attrs->ifa_cacheinfo != 0) {
1259 pltime = attrs->ifa_cacheinfo->ifa_prefered;
1260 vltime = attrs->ifa_cacheinfo->ifa_valid;
1261 }
1262
1263 struct sockaddr_in6 mask = {
1264 .sin6_len = sizeof(struct sockaddr_in6),
1265 .sin6_family = AF_INET6,
1266 };
1267 ip6_writemask(&mask.sin6_addr, attrs->ifa_prefixlen);
1268
1269 struct in6_aliasreq req = {
1270 .ifra_addr = *addr,
1271 .ifra_prefixmask = mask,
1272 .ifra_flags = flags,
1273 .ifra_lifetime = { .ia6t_vltime = vltime, .ia6t_pltime = pltime },
1274 .ifra_vhid = attrs->ifaf_vhid,
1275 };
1276 if (dst != NULL)
1277 req.ifra_dstaddr = *dst;
1278
1279 return (in6_control_ioctl(SIOCAIFADDR_IN6, &req, ifp, nlp_get_cred(nlp)));
1280 }
1281
1282 static int
handle_deladdr_inet6(struct nlmsghdr * hdr,struct nl_parsed_ifa * attrs,if_t ifp,struct nlpcb * nlp,struct nl_pstate * npt)1283 handle_deladdr_inet6(struct nlmsghdr *hdr, struct nl_parsed_ifa *attrs,
1284 if_t ifp, struct nlpcb *nlp, struct nl_pstate *npt)
1285 {
1286 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)attrs->ifa_local;
1287
1288 if (addr == NULL)
1289 addr = (struct sockaddr_in6 *)(attrs->ifa_address);
1290
1291 if (addr == NULL) {
1292 nlmsg_report_err_msg(npt, "Empty IFA_LOCAL/IFA_ADDRESS");
1293 return (EINVAL);
1294 }
1295
1296 struct in6_ifreq req = { .ifr_addr = *addr };
1297
1298 return (in6_control_ioctl(SIOCDIFADDR_IN6, &req, ifp, nlp_get_cred(nlp)));
1299 }
1300 #endif
1301
1302
1303 static int
rtnl_handle_addr(struct nlmsghdr * hdr,struct nlpcb * nlp,struct nl_pstate * npt)1304 rtnl_handle_addr(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt)
1305 {
1306 struct epoch_tracker et;
1307 int error;
1308
1309 struct nl_parsed_ifa attrs = {};
1310 error = nl_parse_nlmsg(hdr, &ifa_parser, npt, &attrs);
1311 if (error != 0)
1312 return (error);
1313
1314 NET_EPOCH_ENTER(et);
1315 if_t ifp = ifnet_byindex_ref(attrs.ifa_index);
1316 NET_EPOCH_EXIT(et);
1317
1318 if (ifp == NULL) {
1319 nlmsg_report_err_msg(npt, "Unable to find interface with index %u",
1320 attrs.ifa_index);
1321 return (ENOENT);
1322 }
1323 int if_flags = if_getflags(ifp);
1324
1325 #if defined(INET) || defined(INET6)
1326 bool new = hdr->nlmsg_type == NL_RTM_NEWADDR;
1327 #endif
1328
1329 /*
1330 * TODO: Properly handle NLM_F_CREATE / NLM_F_EXCL.
1331 * The current ioctl-based KPI always does an implicit create-or-replace.
1332 * It is not possible to specify fine-grained options.
1333 */
1334
1335 switch (attrs.ifa_family) {
1336 #ifdef INET
1337 case AF_INET:
1338 if (new)
1339 error = handle_newaddr_inet(hdr, &attrs, ifp, nlp, npt);
1340 else
1341 error = handle_deladdr_inet(hdr, &attrs, ifp, nlp, npt);
1342 break;
1343 #endif
1344 #ifdef INET6
1345 case AF_INET6:
1346 if (new)
1347 error = handle_newaddr_inet6(hdr, &attrs, ifp, nlp, npt);
1348 else
1349 error = handle_deladdr_inet6(hdr, &attrs, ifp, nlp, npt);
1350 break;
1351 #endif
1352 default:
1353 error = EAFNOSUPPORT;
1354 }
1355
1356 if (error == 0 && !(if_flags & IFF_UP) && (if_getflags(ifp) & IFF_UP))
1357 if_up(ifp);
1358
1359 if_rele(ifp);
1360
1361 return (error);
1362 }
1363
1364
1365 static void
rtnl_handle_ifaddr(void * arg __unused,struct ifaddr * ifa,int cmd)1366 rtnl_handle_ifaddr(void *arg __unused, struct ifaddr *ifa, int cmd)
1367 {
1368 struct nlmsghdr hdr = {};
1369 struct nl_writer nw;
1370 uint32_t group = 0;
1371
1372 switch (ifa->ifa_addr->sa_family) {
1373 #ifdef INET
1374 case AF_INET:
1375 group = RTNLGRP_IPV4_IFADDR;
1376 break;
1377 #endif
1378 #ifdef INET6
1379 case AF_INET6:
1380 group = RTNLGRP_IPV6_IFADDR;
1381 break;
1382 #endif
1383 default:
1384 NL_LOG(LOG_DEBUG2, "ifa notification for unknown AF: %d",
1385 ifa->ifa_addr->sa_family);
1386 return;
1387 }
1388
1389 if (!nl_writer_group(&nw, NLMSG_LARGE, NETLINK_ROUTE, group, 0,
1390 false)) {
1391 NL_LOG(LOG_DEBUG, "error allocating group writer");
1392 return;
1393 }
1394
1395 hdr.nlmsg_type = (cmd == RTM_DELETE) ? NL_RTM_DELADDR : NL_RTM_NEWADDR;
1396
1397 dump_iface_addr(&nw, ifa->ifa_ifp, ifa, &hdr);
1398 nlmsg_flush(&nw);
1399 }
1400
1401 static void
rtnl_handle_ifevent(if_t ifp,int nlmsg_type,int if_flags_mask)1402 rtnl_handle_ifevent(if_t ifp, int nlmsg_type, int if_flags_mask)
1403 {
1404 struct nlmsghdr hdr = { .nlmsg_type = nlmsg_type };
1405 struct nl_writer nw;
1406
1407 if (!nl_writer_group(&nw, NLMSG_LARGE, NETLINK_ROUTE, RTNLGRP_LINK, 0,
1408 false)) {
1409 NL_LOG(LOG_DEBUG, "error allocating group writer");
1410 return;
1411 }
1412 dump_iface(&nw, ifp, &hdr, if_flags_mask);
1413 nlmsg_flush(&nw);
1414 }
1415
1416 static void
rtnl_handle_ifattach(void * arg,if_t ifp)1417 rtnl_handle_ifattach(void *arg, if_t ifp)
1418 {
1419 NL_LOG(LOG_DEBUG2, "ifnet %s", if_name(ifp));
1420 rtnl_handle_ifevent(ifp, NL_RTM_NEWLINK, 0);
1421 }
1422
1423 static void
rtnl_handle_ifdetach(void * arg,if_t ifp)1424 rtnl_handle_ifdetach(void *arg, if_t ifp)
1425 {
1426 NL_LOG(LOG_DEBUG2, "ifnet %s", if_name(ifp));
1427 rtnl_handle_ifevent(ifp, NL_RTM_DELLINK, 0);
1428 }
1429
1430 static void
rtnl_handle_iflink(void * arg,if_t ifp,int link_state __unused)1431 rtnl_handle_iflink(void *arg, if_t ifp, int link_state __unused)
1432 {
1433 NL_LOG(LOG_DEBUG2, "ifnet %s", if_name(ifp));
1434 rtnl_handle_ifevent(ifp, NL_RTM_NEWLINK, 0);
1435 }
1436
1437 void
rtnl_handle_ifnet_event(if_t ifp,int if_flags_mask)1438 rtnl_handle_ifnet_event(if_t ifp, int if_flags_mask)
1439 {
1440 NL_LOG(LOG_DEBUG2, "ifnet %s", if_name(ifp));
1441 rtnl_handle_ifevent(ifp, NL_RTM_NEWLINK, if_flags_mask);
1442 }
1443
1444 static const struct rtnl_cmd_handler cmd_handlers[] = {
1445 {
1446 .cmd = NL_RTM_GETLINK,
1447 .name = "RTM_GETLINK",
1448 .cb = &rtnl_handle_getlink,
1449 .flags = RTNL_F_NOEPOCH | RTNL_F_ALLOW_NONVNET_JAIL,
1450 },
1451 {
1452 .cmd = NL_RTM_DELLINK,
1453 .name = "RTM_DELLINK",
1454 .cb = &rtnl_handle_dellink,
1455 .priv = PRIV_NET_IFDESTROY,
1456 .flags = RTNL_F_NOEPOCH,
1457 },
1458 {
1459 .cmd = NL_RTM_NEWLINK,
1460 .name = "RTM_NEWLINK",
1461 .cb = &rtnl_handle_newlink,
1462 .priv = PRIV_NET_IFCREATE,
1463 .flags = RTNL_F_NOEPOCH,
1464 },
1465 {
1466 .cmd = NL_RTM_GETADDR,
1467 .name = "RTM_GETADDR",
1468 .cb = &rtnl_handle_getaddr,
1469 .flags = RTNL_F_ALLOW_NONVNET_JAIL,
1470 },
1471 {
1472 .cmd = NL_RTM_NEWADDR,
1473 .name = "RTM_NEWADDR",
1474 .cb = &rtnl_handle_addr,
1475 .priv = PRIV_NET_ADDIFADDR,
1476 .flags = RTNL_F_NOEPOCH,
1477 },
1478 {
1479 .cmd = NL_RTM_DELADDR,
1480 .name = "RTM_DELADDR",
1481 .cb = &rtnl_handle_addr,
1482 .priv = PRIV_NET_DELIFADDR,
1483 .flags = RTNL_F_NOEPOCH,
1484 },
1485 };
1486
1487 static const struct nlhdr_parser *all_parsers[] = {
1488 &ifmsg_parser, &ifa_parser, &ifa_fbsd_parser,
1489 };
1490
1491 void
rtnl_iface_add_cloner(struct nl_cloner * cloner)1492 rtnl_iface_add_cloner(struct nl_cloner *cloner)
1493 {
1494 sx_xlock(&rtnl_cloner_lock);
1495 SLIST_INSERT_HEAD(&nl_cloners, cloner, next);
1496 sx_xunlock(&rtnl_cloner_lock);
1497 }
1498
1499 void
rtnl_iface_del_cloner(struct nl_cloner * cloner)1500 rtnl_iface_del_cloner(struct nl_cloner *cloner)
1501 {
1502 sx_xlock(&rtnl_cloner_lock);
1503 SLIST_REMOVE(&nl_cloners, cloner, nl_cloner, next);
1504 sx_xunlock(&rtnl_cloner_lock);
1505 }
1506
1507 void
rtnl_ifaces_init(void)1508 rtnl_ifaces_init(void)
1509 {
1510 ifattach_event = EVENTHANDLER_REGISTER(
1511 ifnet_arrival_event, rtnl_handle_ifattach, NULL,
1512 EVENTHANDLER_PRI_ANY);
1513 ifdetach_event = EVENTHANDLER_REGISTER(
1514 ifnet_departure_event, rtnl_handle_ifdetach, NULL,
1515 EVENTHANDLER_PRI_ANY);
1516 ifaddr_event = EVENTHANDLER_REGISTER(
1517 rt_addrmsg, rtnl_handle_ifaddr, NULL,
1518 EVENTHANDLER_PRI_ANY);
1519 iflink_event = EVENTHANDLER_REGISTER(
1520 ifnet_link_event, rtnl_handle_iflink, NULL,
1521 EVENTHANDLER_PRI_ANY);
1522 NL_VERIFY_PARSERS(all_parsers);
1523 rtnl_register_messages(cmd_handlers, nitems(cmd_handlers));
1524 }
1525
1526 void
rtnl_ifaces_destroy(void)1527 rtnl_ifaces_destroy(void)
1528 {
1529 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, ifattach_event);
1530 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_event);
1531 EVENTHANDLER_DEREGISTER(rt_addrmsg, ifaddr_event);
1532 EVENTHANDLER_DEREGISTER(ifnet_link_event, iflink_event);
1533 }
1534