blob: f32367db8edc8a46f9b3535bf30b0035d3f3d40c [file] [log] [blame]
Harald Welte53a2fde2020-12-01 22:21:14 +01001/*! \file mnl.c
2 *
3 * This code integrates libmnl (minimal netlink library) into the osmocom select
4 * loop abstraction. It allows other osmocom libraries or application code to
5 * create netlink sockets and subscribe to netlink events via libmnl. The completion
6 * handler / callbacks are dispatched via libosmocore select loop handling.
7 */
8
9/*
10 * (C) 2020 by Harald Welte <laforge@gnumonks.org>
11 * All Rights Reserverd.
12 *
13 * SPDX-License-Identifier: GPL-2.0+
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
Harald Welte53a2fde2020-12-01 22:21:14 +010024 */
25
26#include <osmocom/core/select.h>
27#include <osmocom/core/talloc.h>
28#include <osmocom/core/logging.h>
29#include <osmocom/core/mnl.h>
30
31#include <libmnl/libmnl.h>
32
33#include <errno.h>
34#include <string.h>
35
36/* osmo_fd call-back for when RTNL socket is readable */
37static int osmo_mnl_fd_cb(struct osmo_fd *ofd, unsigned int what)
38{
39 uint8_t buf[MNL_SOCKET_BUFFER_SIZE];
40 struct osmo_mnl *omnl = ofd->data;
41 int rc;
42
43 if (!(what & OSMO_FD_READ))
44 return 0;
45
46 rc = mnl_socket_recvfrom(omnl->mnls, buf, sizeof(buf));
47 if (rc <= 0) {
48 LOGP(DLGLOBAL, LOGL_ERROR, "Error in mnl_socket_recvfrom(): %s\n",
49 strerror(errno));
50 return -EIO;
51 }
52
53 return mnl_cb_run(buf, rc, 0, 0, omnl->mnl_cb, omnl);
54}
55
56/*! create an osmocom-wrapped limnl netlink socket.
57 * \parma[in] ctx talloc context from which to allocate
58 * \param[in] bus netlink socket bus ID (see NETLINK_* constants)
59 * \param[in] groups groups of messages to bind/subscribe to
60 * \param[in] mnl_cb callback function called for each incoming message
61 * \param[in] priv opaque private user data
62 * \returns newly-allocated osmo_mnl or NULL in case of error. */
63struct osmo_mnl *osmo_mnl_init(void *ctx, int bus, unsigned int groups, mnl_cb_t mnl_cb, void *priv)
64{
65 struct osmo_mnl *olm = talloc_zero(ctx, struct osmo_mnl);
66
67 if (!olm)
68 return NULL;
69
70 olm->priv = priv;
71 olm->mnl_cb = mnl_cb;
Harald Welte26fc9132020-12-04 10:27:41 +010072 olm->mnls = mnl_socket_open(bus);
Harald Welte53a2fde2020-12-01 22:21:14 +010073 if (!olm->mnls) {
74 LOGP(DLGLOBAL, LOGL_ERROR, "Error creating netlink socket for bus %d: %s\n",
75 bus, strerror(errno));
76 goto out_free;
77 }
78
79 if (mnl_socket_bind(olm->mnls, groups, MNL_SOCKET_AUTOPID) < 0) {
80 LOGP(DLGLOBAL, LOGL_ERROR, "Error binding netlink socket for bus %d to groups 0x%x: %s\n",
81 bus, groups, strerror(errno));
82 goto out_close;
83 }
84
85 osmo_fd_setup(&olm->ofd, mnl_socket_get_fd(olm->mnls), OSMO_FD_READ, osmo_mnl_fd_cb, olm, 0);
86
87 if (osmo_fd_register(&olm->ofd)) {
88 LOGP(DLGLOBAL, LOGL_ERROR, "Error registering netlinks socket\n");
89 goto out_close;
90 }
91
92 return olm;
93
94out_close:
95 mnl_socket_close(olm->mnls);
96out_free:
97 talloc_free(olm);
98 return NULL;
99}
100
101/*! destroy an existing osmocom-wrapped mnl netlink socket: Unregister + close + free.
102 * \param[in] omnl osmo_mnl socket previously returned by osmo_mnl_init() */
103void osmo_mnl_destroy(struct osmo_mnl *omnl)
104{
105 if (!omnl)
106 return;
107
108 osmo_fd_unregister(&omnl->ofd);
109 mnl_socket_close(omnl->mnls);
110 talloc_free(omnl);
111}