Harald Welte | 9e7fe00 | 2018-06-04 20:09:26 +0200 | [diff] [blame] | 1 | /* Simple Osmocom System Monitor (osysmon): Support for monitoring net-devices */ |
| 2 | |
| 3 | /* (C) 2018 by Harald Welte <laforge@gnumonks.org> |
| 4 | * All Rights Reserved. |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 21 | * MA 02110-1301, USA. |
| 22 | */ |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <unistd.h> |
| 27 | #include <string.h> |
| 28 | #include <time.h> |
| 29 | #include <arpa/inet.h> |
| 30 | |
| 31 | #include <linux/if.h> |
| 32 | #include <linux/if_link.h> |
| 33 | #include <linux/rtnetlink.h> |
| 34 | |
| 35 | #include <libmnl/libmnl.h> |
| 36 | #include <talloc.h> |
| 37 | |
| 38 | #include "value_node.h" |
| 39 | #include "osysmon.h" |
| 40 | |
| 41 | struct rtnl_client_state { |
| 42 | struct mnl_socket *nl; |
| 43 | }; |
| 44 | |
| 45 | |
| 46 | struct netdev { |
| 47 | struct llist_head list; |
| 48 | struct { |
| 49 | const char *name; |
| 50 | } cfg; |
| 51 | }; |
| 52 | |
| 53 | static struct netdev *netdev_find(struct osysmon_state *os, const char *name) |
| 54 | { |
| 55 | struct netdev *nd; |
| 56 | llist_for_each_entry(nd, &os->netdevs, list) { |
| 57 | if (!strcmp(name, nd->cfg.name)) |
| 58 | return nd; |
| 59 | } |
| 60 | return NULL; |
| 61 | } |
| 62 | |
| 63 | static struct netdev *netdev_create(struct osysmon_state *os, const char *name) |
| 64 | { |
| 65 | struct netdev *nd; |
| 66 | |
| 67 | if (netdev_find(os, name)) |
| 68 | return NULL; |
| 69 | |
| 70 | nd = talloc_zero(os, struct netdev); |
| 71 | if (!nd) |
| 72 | return NULL; |
| 73 | nd->cfg.name = talloc_strdup(os, name); |
| 74 | llist_add_tail(&nd->list, &os->netdevs); |
| 75 | return nd; |
| 76 | } |
| 77 | |
| 78 | static void netdev_destroy(struct netdev *nd) |
| 79 | { |
| 80 | llist_del(&nd->list); |
| 81 | talloc_free(nd); |
| 82 | } |
| 83 | |
| 84 | /*********************************************************************** |
| 85 | * VTY |
| 86 | ***********************************************************************/ |
| 87 | |
| 88 | static struct cmd_node netdev_node = { |
| 89 | NETDEV_NODE, |
| 90 | "%s(config-netdev)# ", |
| 91 | 1, |
| 92 | }; |
| 93 | |
| 94 | int osysmon_netdev_go_parent(struct vty *vty) |
| 95 | { |
| 96 | switch (vty->node) { |
| 97 | case NETDEV_NODE: |
| 98 | vty->node = CONFIG_NODE; |
| 99 | vty->index = NULL; |
| 100 | break; |
| 101 | default: |
| 102 | break; |
| 103 | } |
| 104 | return vty->node; |
| 105 | } |
| 106 | |
| 107 | DEFUN(cfg_netdev, cfg_netdev_cmd, |
| 108 | "netdev NAME", |
| 109 | "") |
| 110 | { |
| 111 | struct netdev *nd; |
| 112 | nd = netdev_find(g_oss, argv[0]); |
| 113 | if (!nd) |
| 114 | nd = netdev_create(g_oss, argv[0]); |
| 115 | OSMO_ASSERT(nd); |
| 116 | |
| 117 | return CMD_SUCCESS; |
| 118 | } |
| 119 | |
| 120 | DEFUN(cfg_no_netdev, cfg_no_netdev_cmd, |
| 121 | "no netdev NAME", |
| 122 | "") |
| 123 | { |
| 124 | struct netdev *nd; |
| 125 | nd = netdev_find(g_oss, argv[0]); |
| 126 | if (!nd) { |
| 127 | vty_out(vty, "Netdev %s doesn't exist in configuration%s", argv[0], VTY_NEWLINE); |
| 128 | return CMD_WARNING; |
| 129 | } |
| 130 | netdev_destroy(nd); |
| 131 | return CMD_SUCCESS; |
| 132 | } |
| 133 | |
| 134 | static void write_one_netdev(struct vty *vty, struct netdev *nd) |
| 135 | { |
| 136 | vty_out(vty, "netdev %s%s", nd->cfg.name, VTY_NEWLINE); |
| 137 | } |
| 138 | |
| 139 | static int config_write_netdev(struct vty *vty) |
| 140 | { |
| 141 | struct netdev *nd; |
| 142 | |
| 143 | llist_for_each_entry(nd, &g_oss->netdevs, list) |
| 144 | write_one_netdev(vty, nd); |
| 145 | return CMD_SUCCESS; |
| 146 | } |
| 147 | |
| 148 | static void osysmon_rtnl_vty_init(void) |
| 149 | { |
| 150 | install_element(CONFIG_NODE, &cfg_netdev_cmd); |
| 151 | install_element(CONFIG_NODE, &cfg_no_netdev_cmd); |
| 152 | install_node(&netdev_node, config_write_netdev); |
| 153 | } |
| 154 | |
| 155 | |
| 156 | /*********************************************************************** |
| 157 | * Interface Level |
| 158 | ***********************************************************************/ |
| 159 | |
| 160 | static int if_attr_cb(const struct nlattr *attr, void *data) |
| 161 | { |
| 162 | const struct nlattr **tb = data; |
| 163 | int type = mnl_attr_get_type(attr); |
| 164 | |
| 165 | /* skip unsupported attribute in user-space */ |
| 166 | if (mnl_attr_type_valid(attr, IFLA_MAX) < 0) |
| 167 | return MNL_CB_OK; |
| 168 | |
| 169 | switch(type) { |
| 170 | case IFLA_ADDRESS: |
| 171 | if (mnl_attr_validate(attr, MNL_TYPE_BINARY) < 0) { |
| 172 | perror("mnl_attr_validate"); |
| 173 | return MNL_CB_ERROR; |
| 174 | } |
| 175 | break; |
| 176 | case IFLA_MTU: |
| 177 | if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) { |
| 178 | perror("mnl_attr_validate"); |
| 179 | return MNL_CB_ERROR; |
| 180 | } |
| 181 | break; |
| 182 | case IFLA_IFNAME: |
| 183 | if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) { |
| 184 | perror("mnl_attr_validate"); |
| 185 | return MNL_CB_ERROR; |
| 186 | } |
| 187 | break; |
| 188 | } |
| 189 | tb[type] = attr; |
| 190 | return MNL_CB_OK; |
| 191 | } |
| 192 | |
| 193 | static int data_cb(const struct nlmsghdr *nlh, void *data) |
| 194 | { |
| 195 | struct ifinfomsg *ifm = mnl_nlmsg_get_payload(nlh); |
| 196 | struct value_node *parent = data; |
| 197 | struct value_node *vn_if; |
| 198 | const char *name; |
| 199 | char buf[32]; |
| 200 | |
| 201 | struct nlattr *tb[IFLA_MAX+1] = {}; |
| 202 | mnl_attr_parse(nlh, sizeof(*ifm), if_attr_cb, tb); |
| 203 | if (!tb[IFLA_IFNAME]) |
| 204 | return MNL_CB_OK; |
| 205 | name = mnl_attr_get_str(tb[IFLA_IFNAME]); |
| 206 | |
| 207 | /* skip any non-configured interface names */ |
| 208 | if (!netdev_find(g_oss, name)) |
| 209 | return MNL_CB_OK; |
| 210 | |
| 211 | vn_if = value_node_find_or_add(parent, talloc_strdup(parent, name)); |
| 212 | OSMO_ASSERT(vn_if); |
| 213 | vn_if->idx = ifm->ifi_index; |
| 214 | |
| 215 | if (tb[IFLA_ADDRESS] && mnl_attr_get_payload_len(tb[IFLA_ADDRESS]) == 6) { |
| 216 | uint8_t *hwaddr = mnl_attr_get_payload(tb[IFLA_ADDRESS]); |
| 217 | snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x", |
| 218 | hwaddr[0], hwaddr[1], hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5]); |
| 219 | value_node_add(vn_if, vn_if, "hwaddr", buf); |
| 220 | } |
| 221 | if (ifm->ifi_flags & IFF_RUNNING) |
| 222 | value_node_add(vn_if, vn_if, "running", "true"); |
| 223 | else |
| 224 | value_node_add(vn_if, vn_if, "running", "false"); |
| 225 | |
| 226 | if (ifm->ifi_flags & IFF_UP) |
| 227 | value_node_add(vn_if, vn_if, "up", "true"); |
| 228 | else |
| 229 | value_node_add(vn_if, vn_if, "up", "false"); |
| 230 | |
| 231 | return MNL_CB_OK; |
| 232 | } |
| 233 | |
| 234 | static int rtnl_update_link(struct rtnl_client_state *rcs, struct value_node *parent) |
| 235 | { |
| 236 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 237 | struct nlmsghdr *nlh; |
| 238 | struct rtgenmsg *rt; |
| 239 | int ret; |
| 240 | unsigned int seq, portid; |
| 241 | |
| 242 | nlh = mnl_nlmsg_put_header(buf); |
| 243 | nlh->nlmsg_type = RTM_GETLINK; |
| 244 | nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP; |
| 245 | nlh->nlmsg_seq = seq = time(NULL); |
| 246 | rt = mnl_nlmsg_put_extra_header(nlh, sizeof(struct rtgenmsg)); |
| 247 | rt->rtgen_family = AF_PACKET; |
| 248 | |
| 249 | portid = mnl_socket_get_portid(rcs->nl); |
| 250 | |
| 251 | if (mnl_socket_sendto(rcs->nl, nlh, nlh->nlmsg_len) < 0) { |
| 252 | perror("mnl_socket_sendto"); |
| 253 | exit(EXIT_FAILURE); |
| 254 | } |
| 255 | |
| 256 | ret = mnl_socket_recvfrom(rcs->nl, buf, sizeof(buf)); |
| 257 | while (ret > 0) { |
| 258 | ret = mnl_cb_run(buf, ret, seq, portid, data_cb, parent); |
| 259 | if (ret <= MNL_CB_STOP) |
| 260 | break; |
| 261 | ret = mnl_socket_recvfrom(rcs->nl, buf, sizeof(buf)); |
| 262 | } |
| 263 | if (ret == -1) { |
| 264 | perror("error"); |
| 265 | return -1; |
| 266 | } |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | |
| 271 | |
| 272 | /*********************************************************************** |
| 273 | * L3 Address Level |
| 274 | ***********************************************************************/ |
| 275 | |
| 276 | static int inet_attr_cb(const struct nlattr *attr, void *data) |
| 277 | { |
| 278 | const struct nlattr **tb = data; |
| 279 | int type = mnl_attr_get_type(attr); |
| 280 | |
| 281 | /* skip unsupported attribute in user-space */ |
| 282 | if (mnl_attr_type_valid(attr, IFA_MAX) < 0) |
| 283 | return MNL_CB_OK; |
| 284 | |
| 285 | switch(type) { |
| 286 | case IFA_ADDRESS: |
| 287 | if (mnl_attr_validate(attr, MNL_TYPE_BINARY) < 0) { |
| 288 | perror("mnl_attr_validate"); |
| 289 | return MNL_CB_ERROR; |
| 290 | } |
| 291 | break; |
| 292 | } |
| 293 | tb[type] = attr; |
| 294 | return MNL_CB_OK; |
| 295 | } |
| 296 | |
| 297 | static int inet_data_cb(const struct nlmsghdr *nlh, void *data) |
| 298 | { |
| 299 | struct ifaddrmsg *ifa = mnl_nlmsg_get_payload(nlh); |
| 300 | struct nlattr *tb[IFA_MAX + 1] = {}; |
| 301 | struct value_node *parent = data; |
| 302 | struct value_node *vn_if; |
| 303 | |
| 304 | vn_if = value_node_find_by_idx(parent, ifa->ifa_index); |
| 305 | if (!vn_if) |
| 306 | return MNL_CB_OK; |
| 307 | |
| 308 | if (ifa->ifa_family != AF_INET) |
| 309 | return MNL_CB_OK; |
| 310 | |
| 311 | mnl_attr_parse(nlh, sizeof(*ifa), inet_attr_cb, tb); |
| 312 | if (tb[IFA_ADDRESS]) { |
| 313 | struct in_addr *addr = mnl_attr_get_payload(tb[IFA_ADDRESS]); |
| 314 | char out[INET_ADDRSTRLEN+32]; |
| 315 | snprintf(out, sizeof(out), "%s/%u", inet_ntoa(*addr), ifa->ifa_prefixlen); |
| 316 | value_node_add(vn_if, vn_if, "ip", out); |
| 317 | } |
| 318 | |
| 319 | return MNL_CB_OK; |
| 320 | } |
| 321 | |
| 322 | static int rtnl_update_addr(struct rtnl_client_state *rcs, struct value_node *parent) |
| 323 | { |
| 324 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 325 | struct nlmsghdr *nlh; |
| 326 | struct rtgenmsg *rt; |
| 327 | int ret; |
| 328 | unsigned int seq, portid; |
| 329 | |
| 330 | nlh = mnl_nlmsg_put_header(buf); |
| 331 | nlh->nlmsg_type = RTM_GETADDR; |
| 332 | nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP; |
| 333 | nlh->nlmsg_seq = seq = time(NULL); |
| 334 | rt = mnl_nlmsg_put_extra_header(nlh, sizeof(struct rtgenmsg)); |
| 335 | rt->rtgen_family = AF_INET; |
| 336 | |
| 337 | portid = mnl_socket_get_portid(rcs->nl); |
| 338 | |
| 339 | if (mnl_socket_sendto(rcs->nl, nlh, nlh->nlmsg_len) < 0) { |
| 340 | perror("mnl_socket_sendto"); |
| 341 | exit(EXIT_FAILURE); |
| 342 | } |
| 343 | |
| 344 | ret = mnl_socket_recvfrom(rcs->nl, buf, sizeof(buf)); |
| 345 | while (ret > 0) { |
| 346 | ret = mnl_cb_run(buf, ret, seq, portid, inet_data_cb, parent); |
| 347 | if (ret <= MNL_CB_STOP) |
| 348 | break; |
| 349 | ret = mnl_socket_recvfrom(rcs->nl, buf, sizeof(buf)); |
| 350 | } |
| 351 | if (ret == -1) { |
| 352 | perror("error"); |
| 353 | return -1; |
| 354 | } |
| 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | |
| 360 | |
| 361 | /*********************************************************************** |
| 362 | * Common Code / API |
| 363 | ***********************************************************************/ |
| 364 | |
| 365 | int osysmon_rtnl_init() |
| 366 | { |
| 367 | osysmon_rtnl_vty_init(); |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | |
| 372 | |
| 373 | struct rtnl_client_state *rtnl_init(void *ctx) |
| 374 | { |
| 375 | struct mnl_socket *nl; |
| 376 | struct rtnl_client_state *rcs; |
| 377 | |
| 378 | nl = mnl_socket_open(NETLINK_ROUTE); |
| 379 | if (nl == NULL) { |
| 380 | perror("mnl_socket_open"); |
| 381 | return NULL; |
| 382 | } |
| 383 | if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) { |
| 384 | perror("mnl_socket_bind"); |
| 385 | mnl_socket_close(nl); |
| 386 | return NULL; |
| 387 | } |
| 388 | |
| 389 | rcs = talloc_zero(ctx, struct rtnl_client_state); |
| 390 | if (!rcs) { |
| 391 | mnl_socket_close(nl); |
| 392 | return NULL; |
| 393 | } |
| 394 | |
| 395 | rcs->nl = nl; |
| 396 | |
| 397 | return rcs; |
| 398 | } |
| 399 | |
| 400 | int osysmon_rtnl_poll(struct value_node *parent) |
| 401 | { |
| 402 | struct value_node *vn_net; |
| 403 | |
| 404 | if (!g_oss->rcs) |
| 405 | g_oss->rcs = rtnl_init(NULL); |
| 406 | |
| 407 | vn_net = value_node_add(parent, parent, "netdev", NULL); |
| 408 | |
| 409 | if (!g_oss->rcs) |
| 410 | return -1; |
| 411 | |
| 412 | rtnl_update_link(g_oss->rcs, vn_net); |
| 413 | rtnl_update_addr(g_oss->rcs, vn_net); |
| 414 | |
| 415 | return 0; |
| 416 | } |