Skip to content
Snippets Groups Projects
hostmngr.c 13.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • Anjan Chanda's avatar
    Anjan Chanda committed
    /*
     * topology_main.c - main implementation file for topologyd.
     *
     * Copyright (C) 2023 IOPSYS Software Solutions AB. All rights reserved.
     *
     * See LICENSE file for license related information.
     *
     */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    #include <errno.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <sys/ioctl.h>
    #include <net/if_arp.h>
    #include <net/if.h>
    #include <arpa/inet.h>
    
    #include <netlink/route/link.h>
    
    #include <json-c/json.h>
    #include <libubox/blobmsg.h>
    #include <libubox/blobmsg_json.h>
    #include <libubox/uloop.h>
    #include <libubox/ustream.h>
    #include <libubox/utils.h>
    #include <libubus.h>
    
    #include <easy/easy.h>
    
    #include "hlist.h"
    #include "util.h"
    
    #include "useropts.h"
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    #include "debug.h"
    #include "timer.h"
    #include "neigh.h"
    #include "topology.h"
    
    
    static int signal_pending;
    
    static void topologyd_sighandler(int sig)
    {
    	signal_pending = sig;
    }
    
    void enum_interfaces_cb(struct nl_object *obj, void *arg)
    {
    	struct enum_interfaces_arg {
    		struct list_head *iflist;
    		int *n;
    	} *argp = arg;
    	struct ip_address ips[32] = {0};
    	struct local_interface *iface;
    	struct rtnl_link *link;
    	int num_ipaddrs = 32;
    	struct nl_addr *a;
    	int ret;
    
    
    	fprintf(stderr, "%s: %d\n", __func__, __LINE__);
    	nl_object_get(obj);
    	link = (struct rtnl_link *)obj;
    
    	if (rtnl_link_get_arptype(link) == ARPHRD_VOID)
    		goto out;
    
    	iface = calloc(1, sizeof(*iface));
    	if (!iface) {
    		*argp->n = 0;
    		return;
    	}
    
    	strncpy(iface->ifname, rtnl_link_get_name(link), 16);
    	iface->ifindex = rtnl_link_get_ifindex(link);
    
    	a = rtnl_link_get_addr(link);
    	if (nl_addr_get_len(a) == 6)
    		memcpy(iface->macaddr, nl_addr_get_binary_addr(a), nl_addr_get_len(a));
    
    	iface->ifflags = rtnl_link_get_flags(link);
    	iface->operstate = rtnl_link_get_operstate(link);
    	iface->is_bridge = if_isbridge(iface->ifname);
    	if (!iface->is_bridge) {
    		int br_ifindex;
    
    		br_ifindex = if_isbridge_interface(iface->ifname);
    		if (br_ifindex > 0) {
    			iface->is_brif = true;
    			iface->brport = if_brportnum(iface->ifname);
    			iface->br_ifindex = br_ifindex;
    		}
    	}
    
    	fprintf(stderr, "%s: interface %s found\n", __func__, iface->ifname);
    	ret = if_getaddrs(iface->ifname, ips, &num_ipaddrs);
    	if (!ret && num_ipaddrs > 0) {
    		iface->ipaddrs = calloc(num_ipaddrs, sizeof(struct ip_address));
    		if (iface->ipaddrs) {
    			iface->num_ipaddrs = num_ipaddrs;
    			memcpy(iface->ipaddrs, ips, num_ipaddrs * sizeof(struct ip_address));
    		}
    	}
    
    	if (is_wifi_interface(iface->ifname))
    		iface->mediatype = IF_MEDIA_WIFI;
    	else
    		iface->mediatype = IF_MEDIA_ETH;
    
    	list_add_tail(&iface->list, argp->iflist);
    	*argp->n += 1;
    
    out:
    	nl_object_put((struct nl_object *)link);
    }
    
    int enum_interfaces(struct list_head *head, int *num)
    {
    	struct nl_cache *cache;
    	struct nl_sock *sk;
    	int ret = 0;
    	struct enum_interfaces_arg {
    		struct list_head *head;
    		int *n;
    	} arg = {
    		.head = head,
    		.n = num,
    	};
    
    	sk = nl_socket_alloc();
    	if (sk == NULL) {
    		ret = -errno;
    		return ret;
    	}
    
    	nl_connect(sk, NETLINK_ROUTE);
    
    	ret = rtnl_link_alloc_cache(sk, AF_UNSPEC, &cache);
    	if (ret) {
    		nl_socket_free(sk);
    		ret = -errno;
    		return -1;
    	}
    
    	nl_cache_foreach(cache, enum_interfaces_cb, &arg);
    	nl_cache_put(cache);
    	nl_socket_free(sk);
    	return 0;
    }
    
    int topology_enumerate_interfaces(struct topologyd_private *priv)
    {
    	return enum_interfaces(&priv->iflist, &priv->num_local_interfaces);
    }
    
    void topology_print_interfaces(struct topologyd_private *priv)
    {
    	struct local_interface *e;
    	int i = 0;
    
    	if (priv->num_local_interfaces == 0 || list_empty(&priv->iflist))
    		return;
    
    	list_for_each_entry(e, &priv->iflist, list) {
    		fprintf(stderr, "%d: ifname = %16s, mac = " MACFMT ", ifindex = %3d, flags = 0x%08x, opstate = 0x%02x, is-bridge = %d\n",
    			i++, e->ifname, MAC2STR(e->macaddr), e->ifindex,
    			e->ifflags, e->operstate, e->is_bridge);
    	}
    }
    
    int topology_exclude_interfaces(struct topologyd_private *priv)
    {
    	struct local_interface *e;
    
    	//TODO: exclude wan interface(s) by default.
    
    	list_for_each_entry(e, &priv->iflist, list) {
    		if (!strncmp(e->ifname, "eth4", 4))
    			e->exclude = 1;
    	}
    
    	return 0;
    }
    
    int topology_get_interface_neighbors(struct topologyd_private *priv)
    {
    	struct local_interface *e;
    
    	if (priv->num_local_interfaces == 0 || list_empty(&priv->iflist))
    		return 0;
    
    	list_for_each_entry(e, &priv->iflist, list) {
    		if (e->exclude || hwaddr_is_zero(e->macaddr))
    			continue;
    
    		topologyd_get_known_neighbors(priv, e->ifname);
    	}
    
    	return 0;
    }
    
    void remove_newline(char *buf)
    {
    	int len = strlen(buf) - 1;
    
    	if (buf[len] == '\n')
    		buf[len] = 0;
    }
    
    int read_dhcpv4_lease_table(void **clients, int *num_clients)
    {
    	struct dhcp_clients {
    		uint8_t macaddr[6];
    		char ipv4[24];
    		char hostname[256];
    		unsigned long leasetime;
    	} *hosts = NULL, *tmp = NULL;
    	FILE *f = NULL;
    	char line[256];
    	int cnt = 0;
    	int ret = 0;
    
    	f = fopen("/var/dhcp.leases", "r");
    	if (!f)
    		return -1;
    
    	while (fgets(line, sizeof(line), f) != NULL) {
    
    		unsigned long leasetime = 0;
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    		char macaddr[18] = {0};
    		char ipaddr[24] = {0};
    		char hostname[256] = {0};
    		char clid[256] = {0};
    
    		remove_newline(line);
    
    		if (sscanf(line, "%lu %17s %23s %255s %255s", &leasetime,
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
    			   macaddr, ipaddr, hostname, clid) == 5) {
    
    			hosts = realloc(hosts, (cnt + 1) * sizeof(struct dhcp_clients));
    			if (!hosts) {
    				*num_clients = 0;
    				ret = -1;
    				goto out;
    			}
    
    			tmp = hosts + cnt;
    			memset(tmp, 0, sizeof(struct dhcp_clients));
    			hwaddr_aton(macaddr, tmp->macaddr);
    			strncpy(tmp->ipv4, ipaddr, sizeof(ipaddr)-1);
    			strncpy(tmp->hostname, hostname, sizeof(hostname)-1);
    			tmp->leasetime = leasetime;
    			cnt += 1;
    		}
    	}
    
    	*clients = hosts;
    	*num_clients = cnt;
    	fprintf(stderr, "%s: num-dhcphosts = %d\n", __func__, *num_clients);
    out:
    	fclose(f);
    	return ret;
    }
    
    void topology_update_hostname_of_neighbors(struct topologyd_private *priv,
    					   void *dhclients, int num)
    {
    	struct dhcp_clients {
    		uint8_t macaddr[6];
    		char ipv4[24];
    		char hostname[256];
    		unsigned long leasetime;
    	} *h, *hosts = (struct dhcp_clients *)dhclients;
    	struct neigh_entry *e = NULL, *n;
    	int i;
    
    
    	if (num == 0 || !hosts)
    		return;
    
    	for (i = 0; i < num; i++) {
    		h = hosts + i;
    		e = neigh_lookup(&priv->neigh_q, h->macaddr);
    		if (e) {
    			strncpy(e->hostname, h->hostname, sizeof(e->hostname) - 1);
    			inet_aton(h->ipv4, &e->ipv4.addr.ip4);
    			e->ipv4.family = AF_INET;
    			e->leasetime = h->leasetime;
    
    			n = neigh_lookup_by_ipaddr(priv, &e->ipv4);
    			if (!n) {
    				uint32_t key = ipaddr_hash(&e->ipv4);
    
    				fprintf(stderr, "@@@@@@@@@@@@@@@ Host " MACFMT " with ip = %s (0x%x)added to iptable at idx = %d\n", MAC2STR(e->macaddr), h->ipv4, e->ipv4.addr.ip4.s_addr, key);
    				hlist_add_head(&e->iphlist, &priv->nftable.iptable[key]);
    			}
    		}
    	}
    }
    
    int topology_get_neigh_hostname(struct neigh_queue *q, uint8_t *macaddr)
    {
    	struct topologyd_private *priv = container_of(q, struct topologyd_private, neigh_q);	//TODO:
    	struct dhcp_clients {
    		uint8_t macaddr[6];
    		char ipv4[24];
    		char hostname[256];
    		unsigned long leasetime;
    	} *h, *hosts;
    	struct neigh_entry *e = NULL, *n;
    	int i;
    
    	void *entries = NULL;
    	int num = 0;
    	int ret;
    
    	e = neigh_lookup(q, macaddr);
    	if (!e)
    		return -1;
    
    	ret = read_dhcpv4_lease_table(&entries, &num);
    	if (ret || num <= 0)
    		return -1;
    
    	fprintf(stderr, "%s: -- %d -- num-dhcp-entries = %d\n", __func__, __LINE__, num);
    	hosts = (struct dhcp_clients *)entries;
    	for (i = 0; i < num; i++) {
    		h = hosts + i;
    		if (hwaddr_equal(h->macaddr, macaddr)) {
    			strncpy(e->hostname, h->hostname, sizeof(e->hostname) - 1);
    			inet_aton(h->ipv4, &e->ipv4.addr.ip4);
    			e->ipv4.family = AF_INET;
    			e->leasetime = h->leasetime;
    
    			fprintf(stderr, "%s: -- %d -- expected -------\n", __func__, __LINE__);
    			n = neigh_lookup_by_ipaddr(priv, &e->ipv4);
    			if (!n) {
    				uint32_t key = ipaddr_hash(&e->ipv4);
    
    				fprintf(stderr, "@@@@@@@@@@@@@@@ Host " MACFMT " with ip = %s (0x%x)added to iptable at idx = %d\n", MAC2STR(e->macaddr), h->ipv4, e->ipv4.addr.ip4.s_addr, key);
    				hlist_add_head(&e->iphlist, &priv->nftable.iptable[key]);
    			}
    
    			ret = 0;
    			break;
    		}
    	}
    
    	free(entries);
    	return ret;
    }
    
    int topology_get_neighbors_hostname(struct topologyd_private *priv)
    {
    	void *entries = NULL;
    	int num = 0;
    	int ret;
    
    	ret = read_dhcpv4_lease_table(&entries, &num);
    	if (!ret && num > 0) {
    		topology_update_hostname_of_neighbors(priv, entries, num);
    		free(entries);
    	}
    
    	return 0;
    }
    
    int topology_register_notifier_file(struct topologyd_private *priv, const char *filename)
    {
    #if 0	//TODO
    	int wd;
    
    	priv->fd_dhcp4 = inotify_init1(IN_NONBLOCK);
    	if (priv->fd_dhcp4 == -1) {
    		fprintf(stderr, "Error: %s\n", strerror(errno));
    		return -1;
    	}
    
    	wd = inotify_add_watch(fd, filename, IN_MODIFY);
    	if (wd == -1) {
    		close(priv->fd_dhcp4);
    		return -1;
    	}
    
    	//TODO; uloop_fd_add() etc.
    #endif
    	return 0;
    }
    
    char *topology_brport_to_ifname(struct topologyd_private *priv, uint16_t brport)
    {
    	struct local_interface *iface;
    
    	if (list_empty(&priv->iflist))
    		return NULL;
    
    	list_for_each_entry(iface, &priv->iflist, list) {
    		if (iface->is_brif && iface->brport == brport)
    			return iface->ifname;
    	}
    
    	return NULL;
    }
    
    struct local_interface *topologyd_ifname_to_interface(struct topologyd_private *priv,
    						      const char *ifname)
    {
    	struct local_interface *iface;
    
    
    	if (!ifname || !priv)
    		return NULL;
    
    	if (list_empty(&priv->iflist))
    		return NULL;
    
    	list_for_each_entry(iface, &priv->iflist, list) {
    		if (!strncmp(iface->ifname, ifname, 16))
    			return iface;
    	}
    
    	return NULL;
    }
    
    int topology_handle_ethport_carrier_off(struct topologyd_private *priv,
    					const char *ifname, int off)
    {
    	if (!priv || !ifname)
    		return -1;
    
    	neigh_set_unreachable(&priv->neigh_q, ifname, off);
    	return 0;
    }
    
    void heartbeat_timer_cb(atimer_t *t)
    {
    	struct topologyd_private *p = container_of(t, struct topologyd_private, hbtimer);
    
    	UNUSED(p);
    	switch (signal_pending) {
    	case SIGUSR2:
    		signal_pending = 0;
    		err("%s", "Received SIGUSR2\n");
    		break;
    	default:
    		break;
    	}
    
    	timer_set(t, 1000);
    }
    
    void topology_timer_cb(atimer_t *t)
    {
    	//struct topologyd_private *p = container_of(t, struct topologyd_private, topotimer);
    
    	timer_set(t, 60000);
    }
    
    static void topologyd_periodic_refresh(atimer_t *t)
    {
    	struct topologyd_private *p = container_of(t, struct topologyd_private, refreshtimer);
    
    	topologyd_get_1905_topology(p);
    	timer_set(t, 5000);
    }
    
    int topologyd_init_private(struct topologyd_private *priv)
    {
    	int ret;
    
    	fprintf(stderr, "%s --->\n", __func__);
    
    #if 0
    	priv->use_ieee1905 = false;
    	priv->i1905 = lookup_object(priv->bus, IEEE1905_OBJECT);
    	if (priv->i1905 != OBJECT_INVALID) {
    		fprintf(stderr, "%s: Successfully got %s object (id = %u)\n",
    			__func__, IEEE1905_OBJECT, priv->i1905);
    		priv->i1905_topology = lookup_object(priv->bus, IEEE1905_TOPOLOGY_OBJECT);
    		if (priv->i1905_topology != OBJECT_INVALID) {
    			fprintf(stderr, "%s: Successfully got %s object (id = %u)\n",
    				__func__, IEEE1905_TOPOLOGY_OBJECT, priv->i1905_topology);
    			priv->use_ieee1905 = true;
    		}
    	} else {
    		fprintf(stderr, "%s: Failed to get %s object\n", __func__, IEEE1905_OBJECT);
    	}
    #endif
    	INIT_LIST_HEAD(&priv->iflist);
    	ret = topology_enumerate_interfaces(priv);
    	if (!ret)
    		topology_print_interfaces(priv);
    
    	topology_exclude_interfaces(priv);
    
    	neigh_queue_init(&priv->neigh_q);
    
    	topology_get_interface_neighbors(priv);
    	topology_get_neighbors_hostname(priv);
    
    	topology_register_notifier_file(priv, "/var/dhcp.leases");
    
    	nfct_get_entries_nolo(priv);
    
    	return ret;
    }
    
    void topologyd_exit_private(struct topologyd_private *priv)
    {
    	neigh_history_free(&priv->neigh_q);
    	neigh_queue_free(&priv->neigh_q);
    	return;
    }
    
    int topologyd_init(void **priv, void *opts)
    {
    	struct topologyd_useropts *uopts = (struct topologyd_useropts *)opts;
    	struct topologyd_private *p;
    	int ret;
    
    
    	set_sighandler(SIGUSR2, topologyd_sighandler);
    	set_sighandler(SIGPIPE, SIG_IGN);
    	/* and SIGINT/SIGTERM handlers from uloop cancel uloop */
    
    	*priv = NULL;
    	p = calloc(1, sizeof(struct topologyd_private));
    	if (!p)
    		return -1;
    
    
    	uloop_init();
    	p->bus = ubus_connect(uopts->ubus_sockpath);
    	if (!p->bus) {
    		err("Failed to connect to ubus\n");
    		goto out_err;
    	}
    	ubus_add_uloop(p->bus);
    
    #if 0
    	/* read from config */
    	topologyd_config_defaults(&p->cfg);
    	ret = topologyd_reconfig(&p->cfg, uopts->confpath, uopts->conffile);
    	if (ret) {
    		err("Invalid config\n");
    		goto out_err;
    	}
    
    	topologyd_dump_config(&p->cfg);
    #endif
    
    	ret = topologyd_init_private(p);
    	if (ret)
    		goto out_err;
    
    
    	ret = topologyd_publish_object(p, uopts->objname);
    	if (ret)
    		goto out_err;
    
    	ret = topologyd_register_local_events(p);
    	if (ret)
    		goto out_err;
    
    	ret = topologyd_register_nlevents(p);
    	if (ret)
    		goto out_err;
    
    	timer_init(&p->hbtimer, heartbeat_timer_cb);
    	timer_init(&p->topotimer, topology_timer_cb);
    	timer_init(&p->refreshtimer, topologyd_periodic_refresh);
    
    	timer_set(&p->hbtimer, 1000);
    	timer_set(&p->topotimer, 1500);
    	timer_set(&p->refreshtimer, 1000);
    
    	*priv = p;
    	return 0;
    
    out_err:
    	uloop_done();
    	free(p);
    	return -1;
    }
    
    void topologyd_run(void *handle)
    {
    	UNUSED(handle);
    
    	uloop_run();
    }
    
    int topologyd_exit(void *handle)
    {
    	struct topologyd_private *priv = (struct topologyd_private *)handle;
    
    	if (!priv)
    		return 0;
    
    	timer_del(&priv->topotimer);
    	timer_del(&priv->hbtimer);
    	timer_del(&priv->refreshtimer);
    
    	topologyd_unregister_local_events(priv);
    	topologyd_unregister_nlevents(priv);
    
    	topologyd_remove_object(priv);
    	topologyd_exit_private(priv);
    	//topologyd_config_free(&priv->cfg);
    
    	ubus_free(priv->bus);
    	uloop_done();
    	free(priv);
    	fprintf(stderr, "topologyd_exit\n");
    	return 0;
    }
    
    int topologyd_main(void *useropts)
    {
    	struct topologyd_useropts *opts = (struct topologyd_useropts *)useropts;
    	void *ctx;
    	int ret;
    
    
    	if (opts->daemonize)
    		do_daemonize(opts->pidfile);
    
    	start_logging(opts);
    
    	ret = topologyd_init(&ctx, opts);
    	if (ret) {
    
    		err("%s : Failed to init.\n", HOSTS_OBJECT);
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    		return -1;
    	}
    
    	topologyd_run(ctx);
    	topologyd_exit(ctx);
    
    	stop_logging();
    
    	return 0;
    }