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