2 * interface monitor by Pontus Fuchs <pontus.fuchs@gmail.com>
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
12 #include "iface_monitor.h"
16 #if defined(HAVE_LIBNL)
21 * Use Netlink to get indications of new/removed intrfaces.
29 #include <netlink/msg.h>
31 #include <netlink/attr.h>
33 #include <netlink/route/link.h>
38 * Apparently, some versions of libnl drag in headers that define IFF_UP
39 * and others don't. Include <net/if.h> iff IFF_UP isn't already defined,
40 * so that if <linux/if.h> has been included by some or all of the
41 * netlink headers, we don't include <net/if.h> and get a bunch of
42 * complaints about various structures being redefined.
47 /* libnl 1.x compatibility code */
49 #define nl_sock nl_handle
50 #define nl_socket_disable_seq_check nl_disable_sequence_check
52 static inline struct nl_handle
*nl_socket_alloc(void)
54 return nl_handle_alloc();
57 static inline void nl_socket_free(struct nl_sock
*h
)
61 #endif /* HAVE_LIBNL1 */
63 static struct nl_sock
*iface_mon_sock
;
66 iface_mon_handler2(struct nl_object
*obj
, void *arg
)
68 struct rtnl_link
*filter
;
69 struct rtnl_link
*link_obj
;
72 iface_mon_cb cb
= (iface_mon_cb
)arg
;
74 filter
= rtnl_link_alloc();
76 fprintf(stderr
, "error allocating filter\n");
80 if (nl_object_match_filter (obj
, OBJ_CAST (filter
)) == 0) {
81 rtnl_link_put(filter
);
85 link_obj
= (struct rtnl_link
*) obj
;
86 flags
= rtnl_link_get_flags (link_obj
);
87 ifname
= rtnl_link_get_name(link_obj
);
90 * You can't bind a PF_PACKET socket to an interface that's not
91 * up, so an interface going down is an "interface should be
92 * removed" indication.
94 * XXX - what indication, if any, do we get if the interface
95 * *completely goes away*?
97 * XXX - can we get events if an interface's link-layer or
98 * network addresses change?
100 up
= (flags
& IFF_UP
) ? 1 : 0;
105 int msg_type
= nl_object_get_msgtype(obj
);
115 /* Ignore other events */
120 rtnl_link_put(filter
);
126 iface_mon_handler(struct nl_msg
*msg
, void *arg
)
128 nl_msg_parse (msg
, &iface_mon_handler2
, arg
);
133 iface_mon_event(void)
135 nl_recvmsgs_default(iface_mon_sock
);
139 iface_mon_get_sock(void)
141 return nl_socket_get_fd(iface_mon_sock
);
145 iface_mon_start(iface_mon_cb cb
)
149 iface_mon_sock
= nl_socket_alloc();
150 if (!iface_mon_sock
) {
151 fprintf(stderr
, "Failed to allocate netlink socket.\n");
155 nl_socket_disable_seq_check(iface_mon_sock
);
157 nl_socket_modify_cb(iface_mon_sock
, NL_CB_VALID
, NL_CB_CUSTOM
, iface_mon_handler
, (void *)cb
);
159 if (nl_connect(iface_mon_sock
, NETLINK_ROUTE
)) {
160 fprintf(stderr
, "Failed to connect to generic netlink.\n");
162 goto out_handle_destroy
;
165 nl_socket_add_membership(iface_mon_sock
, RTNLGRP_LINK
);
170 nl_socket_free(iface_mon_sock
);
178 nl_socket_free(iface_mon_sock
);
179 iface_mon_sock
= NULL
;
183 iface_mon_enable(bool enable
)
189 nl_socket_add_membership(iface_mon_sock
, RTNLGRP_LINK
);
191 nl_socket_drop_membership(iface_mon_sock
, RTNLGRP_LINK
);
195 #elif defined(__APPLE__)
200 * Use a PF_SYSTEM socket to get indications of new/removed intrfaces.
208 #include <sys/socket.h>
209 #include <sys/ioctl.h>
210 #include <sys/types.h>
212 #include <sys/kern_event.h>
217 static iface_mon_cb callback
;
220 iface_mon_start(iface_mon_cb cb
)
223 struct kev_request key
;
225 /* Create a socket of type PF_SYSTEM to listen for events. */
226 s
= socket(PF_SYSTEM
, SOCK_RAW
, SYSPROTO_EVENT
);
231 * Ask for DLIL messages.
233 * XXX - also ask for KEV_INET_SUBCLASS and KEV_INET6_SUBCLASS,
234 * to detect new or changed network addresses, so those can be
235 * updated as well? Can we specify multiple filters on a socket,
236 * or must we specify KEV_ANY_SUBCLASS and filter the events after
239 key
.vendor_code
= KEV_VENDOR_APPLE
;
240 key
.kev_class
= KEV_NETWORK_CLASS
;
241 key
.kev_subclass
= KEV_DL_SUBCLASS
;
242 if (ioctl(s
, SIOCSKEVFILT
, &key
) == -1) {
259 iface_mon_get_sock(void)
265 * Size of buffer for kernel network event.
267 #define NET_EVENT_DATA_SIZE (KEV_MSG_HEADER_SIZE + sizeof (struct net_event_data))
270 iface_mon_event(void)
272 char msg
[NET_EVENT_DATA_SIZE
];
274 struct kern_event_msg
*kem
;
275 struct net_event_data
*evd
;
277 char ifr_name
[IFNAMSIZ
];
279 received
= recv(s
, msg
, sizeof msg
, 0);
281 /* Error - ignore. */
284 if ((size_t)received
< sizeof msg
) {
285 /* Short read - ignore. */
288 kem
= (struct kern_event_msg
*)msg
;
289 evd_len
= kem
->total_size
- KEV_MSG_HEADER_SIZE
;
290 if (evd_len
!= sizeof (struct net_event_data
)) {
291 /* Length of the message is bogus. */
294 evd
= (struct net_event_data
*)&kem
->event_data
[0];
295 snprintf(ifr_name
, IFNAMSIZ
, "%s%u", evd
->if_name
, evd
->if_unit
);
298 * Check type of event.
300 * Note: if we also ask for KEV_INET_SUBCLASS, we will get
304 * KEV_INET_CHANGED_ADDR
305 * KEV_INET_CHANGED_ADDR
306 * KEV_INET_SIFDSTADDR
307 * KEV_INET_SIFBRDADDR
308 * KEV_INET_SIFNETMASK
310 * reflecting network address changes, with the data being a
311 * struct kev_in_data rather than struct net_event_data, and
312 * if we also ask for KEV_INET6_SUBCLASS, we will get events
315 * KEV_INET6_NEW_LL_ADDR
316 * KEV_INET6_NEW_USER_ADDR
317 * KEV_INET6_NEW_RTADV_ADDR
318 * KEV_INET6_ADDR_DELETED
320 * with the data being a struct kev_in6_data.
322 switch (kem
->event_code
) {
324 case KEV_DL_IF_ATTACHED
:
326 * A new interface has arrived.
328 * XXX - what we really want is "a new BPFable interface
329 * has arrived", but that's not available. While we're
330 * asking for additional help from BPF, it'd also be
331 * nice if we could ask it for a list of all interfaces
332 * that have had bpfattach()/bpf_attach() done on them,
333 * so we don't have to try to open the device in order
334 * to see whether we should show it as something on
335 * which we can capture.
337 callback(ifr_name
, 1, 1);
340 case KEV_DL_IF_DETACHED
:
342 * An existing interface has been removed.
344 * XXX - use KEV_DL_IF_DETACHING instead, as that's
345 * called shortly after bpfdetach() is called, and
346 * bpfdetach() makes an interface no longer BPFable,
347 * and that's what we *really* care about.
349 callback(ifr_name
, 0, 0);
354 * Is there any reason to care about:
359 * KEV_DL_LINK_ADDRESS_CHANGED
360 * KEV_DL_IFCAP_CHANGED
362 * or any of the other events? On Snow Leopard and, I think,
363 * earlier releases, you can't attach a BPF device to an
364 * interface that's not up, so KEV_DL_SIFFLAGS might be
365 * worth listening to so that we only say "here's a new
366 * interface" when it goes up; on Lion (and possibly Mountain
367 * Lion), an interface doesn't have to be up in order to
368 * have a BPF device attached to it.
375 iface_mon_enable(bool enable _U_
)
379 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
382 * FreeBSD, NetBSD, OpenBSD, DragonFly BSD.
384 * Use a PF_ROUTE socket to get indications of new/removed intrfaces.
392 #include <sys/types.h>
393 #include <sys/socket.h>
395 #include <net/route.h>
396 #include <net/if_dl.h>
398 #include <netinet/in.h>
399 #include <netinet/in_var.h>
402 static iface_mon_cb callback
;
405 iface_mon_start(iface_mon_cb cb
)
408 unsigned char msgfilter
[] = {
413 /* Create a socket of type PF_ROUTE to listen for events. */
414 s
= socket(PF_ROUTE
, SOCK_RAW
, 0);
420 * This OS supports filtering PF_ROUTE sockets for specific
421 * events; we only want interface announcement events.
423 * If this fails, we just live with extra events that we ignore,
424 * which we also do on platforms that don't support filtering.
426 (void) setsockopt(s
, PF_ROUTE
, RO_MSGFILTER
, &msgfilter
, sizeof msgfilter
);
430 * This OS supports getting error reports from recvmsg() if a
431 * receive buffer overflow occurs. If that happens, it means
432 * that we may have lost interface reports, so we should
433 * probably just refetch all interface data.
435 * If we can't get those error reports, we're out of luck.
438 (void) setsockopt(s
, SOL_SOCKET
, SO_RERROR
, &n
, sizeof(n
));
452 iface_mon_get_sock(void)
458 iface_mon_event(void)
463 struct if_announcemsghdr ifan
;
467 bool message_seen
= false;
470 iov
[0].iov_base
= &msgbuf
;
471 iov
[0].iov_len
= sizeof msgbuf
;
472 memset(&msg
, 0, sizeof msg
);
476 while (!message_seen
) {
477 received
= recvmsg(s
, &msg
, 0);
478 if (received
== -1) {
479 if (errno
== ENOBUFS
) {
481 * Receive buffer overflow. Keep reading,
482 * to get any messages in the socket buffer.
484 * XXX - that means we may have lost indications;
485 * should there be a callback that indicates that
486 * all interface data should be refreshed?
491 * Other error - just ignore.
498 * We've seen a message.
504 * XXX - should we check received, to make sure it's large
507 if (msgbuf
.hd
.rtm_version
!= RTM_VERSION
)
510 switch (msgbuf
.hd
.rtm_type
) {
513 switch (msgbuf
.ifan
.ifan_what
) {
517 * A new interface has arrived.
519 * XXX - see comment about interface arrivals in the
520 * macOS section; it applies here as well.
522 callback(msgbuf
.ifan
.ifan_name
, 1, 1);
527 * An existing interface has been removed.
529 callback(msgbuf
.ifan
.ifan_name
, 0, 0);
534 * Ignore other notifications.
542 * Ignore other messages.
550 iface_mon_enable(bool enable _U_
)
554 #else /* don't have something we support */
557 iface_mon_start(iface_mon_cb cb _U_
)
568 iface_mon_get_sock(void)
574 iface_mon_event(void)
579 iface_mon_enable(bool enable _U_
)
583 #endif /* HAVE_LIBNL */
585 #endif /* HAVE_LIBPCAP */
588 * Editor modelines - https://www.wireshark.org/tools/modelines.html
593 * indent-tabs-mode: nil
596 * vi: set shiftwidth=4 tabstop=8 expandtab:
597 * :indentSize=4:tabSize=8:noTabs=true: