2020-10-04 07:28:41 +01:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
2021-02-05 13:08:41 +00:00
|
|
|
* Copyright (C) 2010 - 2021 Andy Green <andy@warmcat.com>
|
2020-10-04 07:28:41 +01:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* We mainly focus on the routing table / gateways because those are the
|
|
|
|
* elements that decide if we can get on to the internet or not.
|
|
|
|
*
|
|
|
|
* We also need to understand the source addresses of possible outgoing routes,
|
|
|
|
* and follow LINK down (ifconfig down) to clean up routes on the interface idx
|
|
|
|
* going down that are not otherwise cleaned.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <private-lib-core.h>
|
|
|
|
|
|
|
|
#include <asm/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <linux/netlink.h>
|
|
|
|
#include <linux/rtnetlink.h>
|
|
|
|
|
2021-01-21 05:54:16 +00:00
|
|
|
/* work around CentOS 7 -Wconversion problem */
|
|
|
|
#undef RTA_ALIGNTO
|
|
|
|
#define RTA_ALIGNTO 4U
|
|
|
|
|
2021-02-05 13:08:41 +00:00
|
|
|
//#define lwsl_netlink lwsl_notice
|
2021-03-07 09:44:29 +00:00
|
|
|
#define lwsl_netlink lwsl_info
|
2021-02-05 13:08:41 +00:00
|
|
|
|
2020-10-04 07:28:41 +01:00
|
|
|
static void
|
|
|
|
lws_netlink_coldplug_done_cb(lws_sorted_usec_list_t *sul)
|
|
|
|
{
|
|
|
|
struct lws_context *ctx = lws_container_of(sul, struct lws_context,
|
|
|
|
sul_nl_coldplug);
|
|
|
|
ctx->nl_initial_done = 1;
|
|
|
|
|
|
|
|
/* if nothing is there to intercept anything, go all the way */
|
|
|
|
lws_state_transition_steps(&ctx->mgr_system, LWS_SYSTATE_OPERATIONAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
rops_handle_POLLIN_netlink(struct lws_context_per_thread *pt, struct lws *wsi,
|
|
|
|
struct lws_pollfd *pollfd)
|
|
|
|
{
|
2021-03-07 09:44:29 +00:00
|
|
|
struct lws_context *cx = pt->context;
|
2021-02-05 13:08:41 +00:00
|
|
|
uint8_t s[4096]
|
2020-10-04 07:28:41 +01:00
|
|
|
#if defined(_DEBUG)
|
|
|
|
, route_change = 0
|
|
|
|
#endif
|
|
|
|
#if defined(LWS_WITH_SYS_SMD)
|
|
|
|
, gateway_change = 0
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
struct sockaddr_nl nladdr;
|
|
|
|
lws_route_t robj, *rou;
|
|
|
|
struct nlmsghdr *h;
|
|
|
|
struct msghdr msg;
|
|
|
|
struct iovec iov;
|
2021-02-05 13:08:41 +00:00
|
|
|
unsigned int n;
|
|
|
|
char buf[72];
|
2020-10-04 07:28:41 +01:00
|
|
|
|
|
|
|
if (!(pollfd->revents & LWS_POLLIN))
|
|
|
|
return LWS_HPI_RET_HANDLED;
|
|
|
|
|
2021-03-07 09:44:29 +00:00
|
|
|
if (!cx->nl_initial_done && pt == &cx->pt[0]) {
|
2020-10-04 07:28:41 +01:00
|
|
|
/*
|
|
|
|
* While netlink info still coming, keep moving the timer for
|
|
|
|
* calling it "done" to +100ms until after it stops coming
|
|
|
|
*/
|
2021-03-07 09:44:29 +00:00
|
|
|
lws_context_lock(cx, __func__);
|
|
|
|
lws_sul_schedule(cx, 0, &cx->sul_nl_coldplug,
|
2020-10-04 07:28:41 +01:00
|
|
|
lws_netlink_coldplug_done_cb,
|
|
|
|
100 * LWS_US_PER_MS);
|
2021-03-07 09:44:29 +00:00
|
|
|
lws_context_unlock(cx);
|
2020-10-04 07:28:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
memset(&msg, 0, sizeof(msg));
|
|
|
|
|
|
|
|
iov.iov_base = (void *)s;
|
|
|
|
iov.iov_len = sizeof(s);
|
2021-02-05 13:08:41 +00:00
|
|
|
|
2020-10-04 07:28:41 +01:00
|
|
|
msg.msg_name = (void *)&(nladdr);
|
|
|
|
msg.msg_namelen = sizeof(nladdr);
|
|
|
|
|
|
|
|
msg.msg_iov = &iov;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
n = (unsigned int)recvmsg(wsi->desc.sockfd, &msg, 0);
|
2021-02-05 13:08:41 +00:00
|
|
|
if ((int)n < 0) {
|
|
|
|
lwsl_notice("%s: recvmsg failed\n", __func__);
|
2020-10-04 07:28:41 +01:00
|
|
|
return LWS_HPI_RET_PLEASE_CLOSE_ME;
|
2021-02-05 13:08:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// lwsl_hexdump_notice(s, (size_t)n);
|
2020-10-04 07:28:41 +01:00
|
|
|
|
|
|
|
h = (struct nlmsghdr *)s;
|
|
|
|
|
2021-02-05 13:08:41 +00:00
|
|
|
/* we can get a bunch of messages coalesced in one read*/
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
for ( ; NLMSG_OK(h, n); h = NLMSG_NEXT(h, n)) {
|
2020-10-04 07:28:41 +01:00
|
|
|
struct ifaddrmsg *ifam;
|
|
|
|
struct rtattr *ra;
|
|
|
|
struct rtmsg *rm;
|
2021-02-05 13:08:41 +00:00
|
|
|
#if !defined(LWS_WITH_NO_LOGS) && defined(_DEBUG)
|
|
|
|
struct ndmsg *nd;
|
|
|
|
#endif
|
2020-12-12 06:21:40 +00:00
|
|
|
unsigned int ra_len;
|
2020-10-04 07:28:41 +01:00
|
|
|
uint8_t *p;
|
|
|
|
|
2021-02-05 13:08:41 +00:00
|
|
|
struct ifinfomsg *ifi;
|
|
|
|
struct rtattr *attribute;
|
|
|
|
lws_sockaddr46 *sa46;
|
|
|
|
unsigned int len;
|
|
|
|
|
|
|
|
lwsl_netlink("%s: RTM %d\n", __func__, h->nlmsg_type);
|
|
|
|
|
|
|
|
memset(&robj, 0, sizeof(robj));
|
|
|
|
robj.if_idx = -1;
|
|
|
|
robj.priority = -1;
|
|
|
|
rm = (struct rtmsg *)NLMSG_DATA(h);
|
|
|
|
|
2020-10-04 07:28:41 +01:00
|
|
|
/*
|
|
|
|
* We have to care about NEWLINK so we can understand when a
|
|
|
|
* network interface went down, and clear the related routes.
|
|
|
|
*
|
|
|
|
* We don't get individual DELROUTEs for these.
|
|
|
|
*/
|
|
|
|
|
2021-02-05 13:08:41 +00:00
|
|
|
switch (h->nlmsg_type) {
|
|
|
|
case RTM_NEWLINK:
|
|
|
|
|
|
|
|
ifi = NLMSG_DATA(h);
|
|
|
|
len = (unsigned int)(h->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)));
|
|
|
|
|
|
|
|
/* loop over all attributes for the NEWLINK message */
|
|
|
|
for (attribute = IFLA_RTA(ifi); RTA_OK(attribute, len);
|
|
|
|
attribute = RTA_NEXT(attribute, len)) {
|
|
|
|
lwsl_netlink("%s: if attr %d\n", __func__,
|
|
|
|
(int)attribute->rta_type);
|
|
|
|
switch(attribute->rta_type) {
|
|
|
|
case IFLA_IFNAME:
|
|
|
|
lwsl_netlink("NETLINK ifidx %d : %s\n",
|
|
|
|
ifi->ifi_index,
|
|
|
|
(char *)RTA_DATA(attribute));
|
|
|
|
break;
|
|
|
|
case RTA_SRC:
|
|
|
|
sa46 = (lws_sockaddr46 *)RTA_DATA(attribute);
|
|
|
|
if (sa46->sa4.sin_family == 0xffff) {
|
|
|
|
lwsl_netlink("%s: down %d\n",
|
|
|
|
__func__,
|
|
|
|
ifi->ifi_index);
|
|
|
|
lws_pt_lock(pt, __func__);
|
|
|
|
_lws_route_table_ifdown(pt,
|
|
|
|
ifi->ifi_index);
|
|
|
|
_lws_route_pt_close_unroutable(pt);
|
|
|
|
lws_pt_unlock(pt);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
} /* switch */
|
|
|
|
} /* for loop */
|
|
|
|
|
|
|
|
lwsl_netlink("%s: NEWLINK ifi_index %d, flags 0x%x\n",
|
|
|
|
__func__, ifi->ifi_index, ifi->ifi_flags);
|
2020-10-04 07:28:41 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Despite "New"link this is actually telling us there
|
|
|
|
* is some change on the network interface IFF_ state
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!(ifi->ifi_flags & IFF_UP)) {
|
|
|
|
/*
|
|
|
|
* Interface is down, so scrub all routes that
|
|
|
|
* applied to it
|
|
|
|
*/
|
2021-02-05 13:08:41 +00:00
|
|
|
lwsl_netlink("%s: NEWLINK: ifdown %d\n",
|
|
|
|
__func__, ifi->ifi_index);
|
2020-10-04 07:28:41 +01:00
|
|
|
lws_pt_lock(pt, __func__);
|
|
|
|
_lws_route_table_ifdown(pt, ifi->ifi_index);
|
|
|
|
lws_pt_unlock(pt);
|
|
|
|
}
|
2021-02-05 13:08:41 +00:00
|
|
|
continue; /* ie, not break, no second half */
|
2020-10-04 07:28:41 +01:00
|
|
|
|
2021-02-05 13:08:41 +00:00
|
|
|
case RTM_NEWADDR:
|
|
|
|
case RTM_DELADDR:
|
2020-10-04 07:28:41 +01:00
|
|
|
|
|
|
|
ifam = (struct ifaddrmsg *)NLMSG_DATA(h);
|
|
|
|
|
|
|
|
robj.source_ads = 1;
|
|
|
|
robj.dest_len = ifam->ifa_prefixlen;
|
2020-12-12 06:21:40 +00:00
|
|
|
robj.if_idx = (int)ifam->ifa_index;
|
2020-10-04 07:28:41 +01:00
|
|
|
robj.scope = ifam->ifa_scope;
|
|
|
|
robj.ifa_flags = ifam->ifa_flags;
|
|
|
|
robj.dest.sa4.sin_family = ifam->ifa_family;
|
|
|
|
|
|
|
|
/* address attributes */
|
|
|
|
ra = (struct rtattr *)IFA_RTA(ifam);
|
2020-12-12 06:21:40 +00:00
|
|
|
ra_len = (unsigned int)IFA_PAYLOAD(h);
|
|
|
|
|
2021-02-05 13:08:41 +00:00
|
|
|
lwsl_netlink("%s: %s\n", __func__,
|
|
|
|
h->nlmsg_type == RTM_NEWADDR ?
|
|
|
|
"NEWADDR" : "DELADDR");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RTM_NEWROUTE:
|
|
|
|
case RTM_DELROUTE:
|
|
|
|
|
|
|
|
lwsl_netlink("%s: %s\n", __func__,
|
|
|
|
h->nlmsg_type == RTM_NEWROUTE ?
|
|
|
|
"NEWROUTE" : "DELROUTE");
|
2020-12-12 06:21:40 +00:00
|
|
|
|
2020-10-04 07:28:41 +01:00
|
|
|
/* route attributes */
|
|
|
|
ra = (struct rtattr *)RTM_RTA(rm);
|
2020-12-12 06:21:40 +00:00
|
|
|
ra_len = (unsigned int)RTM_PAYLOAD(h);
|
2021-02-05 13:08:41 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RTM_DELNEIGH:
|
|
|
|
case RTM_NEWNEIGH:
|
|
|
|
lwsl_netlink("%s: %s\n", __func__,
|
|
|
|
h->nlmsg_type == RTM_NEWNEIGH ? "NEWNEIGH" :
|
|
|
|
"DELNEIGH");
|
|
|
|
#if !defined(LWS_WITH_NO_LOGS) && defined(_DEBUG)
|
|
|
|
nd = (struct ndmsg *)rm;
|
|
|
|
lwsl_netlink("%s: fam %u, ifidx %u, flags 0x%x\n",
|
|
|
|
__func__, nd->ndm_family, nd->ndm_ifindex,
|
|
|
|
nd->ndm_flags);
|
|
|
|
#endif
|
|
|
|
ra = (struct rtattr *)RTM_RTA(rm);
|
|
|
|
ra_len = (unsigned int)RTM_PAYLOAD(h);
|
|
|
|
for ( ; RTA_OK(ra, ra_len); ra = RTA_NEXT(ra, ra_len)) {
|
|
|
|
lwsl_netlink("%s: atr %d\n", __func__, ra->rta_type);
|
|
|
|
switch (ra->rta_type) {
|
|
|
|
case NDA_DST:
|
|
|
|
lwsl_netlink("%s: dst len %d\n",
|
|
|
|
__func__, ra->rta_len);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lws_pt_lock(pt, __func__);
|
|
|
|
_lws_route_pt_close_unroutable(pt);
|
|
|
|
lws_pt_unlock(pt);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
default:
|
|
|
|
lwsl_netlink("%s: *** Unknown RTM_%d\n", __func__,
|
|
|
|
h->nlmsg_type);
|
|
|
|
continue;
|
|
|
|
} /* switch */
|
2020-10-04 07:28:41 +01:00
|
|
|
|
|
|
|
robj.proto = rm->rtm_protocol;
|
|
|
|
|
|
|
|
for ( ; RTA_OK(ra, ra_len); ra = RTA_NEXT(ra, ra_len)) {
|
2021-02-05 13:08:41 +00:00
|
|
|
// lwsl_netlink("%s: atr %d\n", __func__, ra->rta_type);
|
2020-10-04 07:28:41 +01:00
|
|
|
switch (ra->rta_type) {
|
2021-02-05 13:08:41 +00:00
|
|
|
case RTA_PREFSRC: /* protocol ads: preferred src ads */
|
|
|
|
case RTA_SRC:
|
|
|
|
lws_sa46_copy_address(&robj.src, RTA_DATA(ra),
|
|
|
|
rm->rtm_family);
|
|
|
|
robj.src_len = rm->rtm_src_len;
|
|
|
|
lws_sa46_write_numeric_address(&robj.src, buf, sizeof(buf));
|
|
|
|
lwsl_netlink("%s: RTA_SRC: %s\n", __func__, buf);
|
|
|
|
break;
|
2020-10-04 07:28:41 +01:00
|
|
|
case RTA_DST:
|
|
|
|
lws_sa46_copy_address(&robj.dest, RTA_DATA(ra),
|
|
|
|
rm->rtm_family);
|
|
|
|
robj.dest_len = rm->rtm_dst_len;
|
2021-02-05 13:08:41 +00:00
|
|
|
lws_sa46_write_numeric_address(&robj.dest, buf, sizeof(buf));
|
|
|
|
lwsl_netlink("%s: RTA_DST: %s\n", __func__, buf);
|
2020-10-04 07:28:41 +01:00
|
|
|
break;
|
|
|
|
case RTA_GATEWAY:
|
|
|
|
lws_sa46_copy_address(&robj.gateway,
|
|
|
|
RTA_DATA(ra),
|
|
|
|
rm->rtm_family);
|
|
|
|
#if defined(LWS_WITH_SYS_SMD)
|
|
|
|
gateway_change = 1;
|
|
|
|
#endif
|
|
|
|
break;
|
2021-02-05 13:08:41 +00:00
|
|
|
case RTA_IIF: /* int: input interface index */
|
2020-10-04 07:28:41 +01:00
|
|
|
case RTA_OIF: /* int: output interface index */
|
2021-02-05 13:08:41 +00:00
|
|
|
if (h->nlmsg_type != RTM_NEWADDR &&
|
|
|
|
h->nlmsg_type != RTM_DELADDR) {
|
|
|
|
robj.if_idx = *(int *)RTA_DATA(ra);
|
|
|
|
lwsl_netlink("%s: ifidx %d\n", __func__, robj.if_idx);
|
|
|
|
}
|
2020-10-04 07:28:41 +01:00
|
|
|
break;
|
|
|
|
case RTA_PRIORITY: /* int: priority of route */
|
|
|
|
p = RTA_DATA(ra);
|
|
|
|
robj.priority = p[3] << 24 | p[2] << 16 |
|
|
|
|
p[1] << 8 | p[0];
|
|
|
|
break;
|
|
|
|
case RTA_CACHEINFO: /* struct rta_cacheinfo */
|
|
|
|
break;
|
|
|
|
#if defined(LWS_HAVE_RTA_PREF)
|
|
|
|
case RTA_PREF: /* char: RFC4191 v6 router preference */
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case RTA_TABLE: /* int */
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
lwsl_info("%s: unknown attr type %d\n",
|
|
|
|
__func__, ra->rta_type);
|
|
|
|
break;
|
|
|
|
}
|
2021-02-05 13:08:41 +00:00
|
|
|
} /* for */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* the second half, once all the attributes were collected
|
|
|
|
*/
|
|
|
|
|
2020-10-04 07:28:41 +01:00
|
|
|
|
|
|
|
switch (h->nlmsg_type) {
|
|
|
|
|
|
|
|
case RTM_DELROUTE:
|
|
|
|
/*
|
|
|
|
* This will also take down wsi marked as using it
|
|
|
|
*/
|
2021-02-05 13:08:41 +00:00
|
|
|
lwsl_netlink("%s: DELROUTE: if_idx %d\n", __func__,
|
|
|
|
robj.if_idx);
|
2020-10-04 07:28:41 +01:00
|
|
|
lws_pt_lock(pt, __func__);
|
2021-02-05 13:08:41 +00:00
|
|
|
_lws_route_remove(pt, &robj, 0);
|
2020-10-04 07:28:41 +01:00
|
|
|
lws_pt_unlock(pt);
|
|
|
|
goto inform;
|
|
|
|
|
|
|
|
case RTM_NEWROUTE:
|
|
|
|
|
2021-02-05 13:08:41 +00:00
|
|
|
lwsl_netlink("%s: NEWROUTE rtm_type %d\n", __func__,
|
|
|
|
rm->rtm_type);
|
|
|
|
|
2020-10-04 07:28:41 +01:00
|
|
|
/*
|
|
|
|
* We don't want any routing debris like /32 or broadcast
|
|
|
|
* in our routing table... we will collect source addresses
|
|
|
|
* bound to interfaces via NEWADDR
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (rm->rtm_type != RTN_UNICAST &&
|
|
|
|
rm->rtm_type != RTN_LOCAL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (rm->rtm_flags & RTM_F_CLONED)
|
|
|
|
break;
|
|
|
|
|
2021-02-05 13:08:41 +00:00
|
|
|
goto ana;
|
|
|
|
|
|
|
|
case RTM_DELADDR:
|
|
|
|
lwsl_notice("%s: DELADDR\n", __func__);
|
|
|
|
#if defined(_DEBUG)
|
|
|
|
_lws_routing_entry_dump(&robj);
|
|
|
|
#endif
|
|
|
|
lws_pt_lock(pt, __func__);
|
|
|
|
_lws_route_remove(pt, &robj, LRR_MATCH_SRC | LRR_IGNORE_PRI);
|
|
|
|
_lws_route_pt_close_unroutable(pt);
|
|
|
|
lws_pt_unlock(pt);
|
|
|
|
break;
|
2020-10-04 07:28:41 +01:00
|
|
|
|
|
|
|
case RTM_NEWADDR:
|
2021-02-05 13:08:41 +00:00
|
|
|
|
|
|
|
lwsl_netlink("%s: NEWADDR\n", __func__);
|
|
|
|
ana:
|
2020-10-04 07:28:41 +01:00
|
|
|
rou = lws_malloc(sizeof(*rou), __func__);
|
|
|
|
if (!rou) {
|
|
|
|
lwsl_err("%s: oom\n", __func__);
|
|
|
|
return LWS_HPI_RET_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
*rou = robj;
|
|
|
|
|
|
|
|
lws_pt_lock(pt, __func__);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We lock the pt before getting the uidx, so it
|
|
|
|
* cannot race
|
|
|
|
*/
|
|
|
|
|
2021-03-07 09:44:29 +00:00
|
|
|
rou->uidx = _lws_route_get_uidx(cx);
|
|
|
|
lws_dll2_add_tail(&rou->list, &cx->routing_table);
|
2020-10-04 07:28:41 +01:00
|
|
|
|
|
|
|
_lws_route_pt_close_unroutable(pt);
|
|
|
|
|
|
|
|
lws_pt_unlock(pt);
|
|
|
|
|
|
|
|
inform:
|
|
|
|
#if defined(_DEBUG)
|
|
|
|
route_change = 1;
|
|
|
|
#endif
|
|
|
|
#if defined(LWS_WITH_SYS_SMD)
|
|
|
|
/*
|
|
|
|
* Reflect the route add / del event using SMD.
|
|
|
|
* Participants interested can refer to the pt
|
|
|
|
* routing table
|
|
|
|
*/
|
2021-03-07 09:44:29 +00:00
|
|
|
(void)lws_smd_msg_printf(cx, LWSSMDCL_NETWORK,
|
2020-10-04 07:28:41 +01:00
|
|
|
"{\"rt\":\"%s\"}\n",
|
|
|
|
(h->nlmsg_type == RTM_DELROUTE) ?
|
|
|
|
"del" : "add");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// lwsl_info("%s: unknown msg type %d\n", __func__,
|
|
|
|
// h->nlmsg_type);
|
|
|
|
break;
|
|
|
|
}
|
2021-02-05 13:08:41 +00:00
|
|
|
} /* message iterator */
|
2020-10-04 07:28:41 +01:00
|
|
|
|
|
|
|
#if defined(LWS_WITH_SYS_SMD)
|
|
|
|
if (gateway_change)
|
|
|
|
/*
|
|
|
|
* If a route with a gw was added or deleted, retrigger captive
|
|
|
|
* portal detection if we have that
|
|
|
|
*/
|
2021-03-07 09:44:29 +00:00
|
|
|
(void)lws_smd_msg_printf(cx, LWSSMDCL_NETWORK,
|
2020-10-04 07:28:41 +01:00
|
|
|
"{\"trigger\": \"cpdcheck\", "
|
|
|
|
"\"src\":\"gw-change\"}");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(_DEBUG)
|
|
|
|
if (route_change) {
|
2021-03-07 09:44:29 +00:00
|
|
|
lws_context_lock(cx, __func__);
|
|
|
|
_lws_routing_table_dump(cx);
|
|
|
|
lws_context_unlock(cx);
|
2020-10-04 07:28:41 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return LWS_HPI_RET_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct nl_req_s {
|
|
|
|
struct nlmsghdr hdr;
|
|
|
|
struct rtmsg gen;
|
|
|
|
};
|
|
|
|
|
|
|
|
int
|
|
|
|
rops_pt_init_destroy_netlink(struct lws_context *context,
|
|
|
|
const struct lws_context_creation_info *info,
|
|
|
|
struct lws_context_per_thread *pt, int destroy)
|
|
|
|
{
|
|
|
|
struct sockaddr_nl sanl;
|
|
|
|
struct nl_req_s req;
|
|
|
|
struct msghdr msg;
|
|
|
|
struct iovec iov;
|
|
|
|
struct lws *wsi;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (destroy) {
|
2020-11-16 19:32:58 +00:00
|
|
|
|
2020-10-04 07:28:41 +01:00
|
|
|
/*
|
|
|
|
* pt netlink wsi closed + freed as part of pt's destroy
|
|
|
|
* wsi mass close, just need to take down the routing table
|
|
|
|
*/
|
|
|
|
_lws_route_table_empty(pt);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-07 09:44:29 +00:00
|
|
|
if (context->netlink)
|
2020-10-04 07:28:41 +01:00
|
|
|
return 0;
|
|
|
|
|
2020-11-16 19:32:58 +00:00
|
|
|
if (pt > &context->pt[0])
|
|
|
|
/* we can only have one netlink socket */
|
|
|
|
return 0;
|
|
|
|
|
2020-10-04 07:28:41 +01:00
|
|
|
lwsl_info("%s: creating netlink skt\n", __func__);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We want a netlink socket per pt as well
|
|
|
|
*/
|
2020-12-24 16:06:50 +00:00
|
|
|
|
|
|
|
lws_context_lock(context, __func__);
|
|
|
|
wsi = __lws_wsi_create_with_role(context, (int)(pt - &context->pt[0]),
|
2020-10-04 07:28:41 +01:00
|
|
|
&role_ops_netlink);
|
2020-12-24 16:06:50 +00:00
|
|
|
lws_context_unlock(context);
|
2020-10-04 07:28:41 +01:00
|
|
|
if (!wsi)
|
|
|
|
goto bail;
|
|
|
|
|
|
|
|
wsi->desc.sockfd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
|
|
|
|
if (wsi->desc.sockfd == LWS_SOCK_INVALID) {
|
|
|
|
lwsl_err("%s: unable to open netlink\n", __func__);
|
|
|
|
goto bail1;
|
|
|
|
}
|
|
|
|
|
2020-12-25 05:54:19 +00:00
|
|
|
__lws_lc_tag(&context->lcg[LWSLCG_VHOST], &wsi->lc, "netlink");
|
|
|
|
|
2020-10-04 07:28:41 +01:00
|
|
|
memset(&sanl, 0, sizeof(sanl));
|
|
|
|
sanl.nl_family = AF_NETLINK;
|
2020-12-12 06:21:40 +00:00
|
|
|
sanl.nl_pid = (uint32_t)getpid();
|
2021-02-05 13:08:41 +00:00
|
|
|
sanl.nl_groups = (1 << (RTNLGRP_LINK - 1)) |
|
|
|
|
(1 << (RTNLGRP_IPV4_ROUTE - 1)) |
|
|
|
|
(1 << (RTNLGRP_IPV4_IFADDR - 1))
|
2020-10-04 07:28:41 +01:00
|
|
|
#if defined(LWS_WITH_IPV6)
|
2021-02-05 13:08:41 +00:00
|
|
|
| (1 << (RTNLGRP_IPV6_ROUTE - 1)) |
|
|
|
|
(1 << (RTNLGRP_IPV6_IFADDR - 1))
|
2020-10-04 07:28:41 +01:00
|
|
|
#endif
|
2021-02-05 13:08:41 +00:00
|
|
|
;
|
2020-10-04 07:28:41 +01:00
|
|
|
|
|
|
|
if (bind(wsi->desc.sockfd, (struct sockaddr*)&sanl, sizeof(sanl)) < 0) {
|
|
|
|
lwsl_err("%s: netlink bind failed\n", __func__);
|
|
|
|
goto bail2;
|
|
|
|
}
|
|
|
|
|
2021-03-07 09:44:29 +00:00
|
|
|
context->netlink = wsi;
|
2020-10-04 07:28:41 +01:00
|
|
|
if (lws_wsi_inject_to_loop(pt, wsi))
|
|
|
|
goto bail2;
|
|
|
|
|
2020-11-16 19:32:58 +00:00
|
|
|
/* if (lws_change_pollfd(wsi, 0, LWS_POLLIN)) {
|
2020-10-04 07:28:41 +01:00
|
|
|
lwsl_err("%s: pollfd in fail\n", __func__);
|
|
|
|
goto bail2;
|
|
|
|
}
|
2020-11-16 19:32:58 +00:00
|
|
|
*/
|
2020-10-04 07:28:41 +01:00
|
|
|
/*
|
|
|
|
* Since we're starting the PT, ask to be sent all the existing routes.
|
|
|
|
*
|
|
|
|
* This requires CAP_ADMIN, or root... we do this early before dropping
|
|
|
|
* privs
|
|
|
|
*/
|
|
|
|
|
|
|
|
memset(&sanl, 0, sizeof(sanl));
|
|
|
|
memset(&msg, 0, sizeof(msg));
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
|
|
|
|
sanl.nl_family = AF_NETLINK;
|
|
|
|
|
|
|
|
req.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(req.gen));
|
|
|
|
req.hdr.nlmsg_type = RTM_GETROUTE;
|
|
|
|
req.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
|
|
|
|
req.hdr.nlmsg_seq = 1;
|
2020-12-12 06:21:40 +00:00
|
|
|
req.hdr.nlmsg_pid = (uint32_t)getpid();
|
2020-10-04 07:28:41 +01:00
|
|
|
req.gen.rtm_family = AF_PACKET;
|
|
|
|
req.gen.rtm_table = RT_TABLE_DEFAULT;
|
|
|
|
|
|
|
|
iov.iov_base = &req;
|
|
|
|
iov.iov_len = req.hdr.nlmsg_len;
|
|
|
|
msg.msg_iov = &iov;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
msg.msg_name = &sanl;
|
|
|
|
msg.msg_namelen = sizeof(sanl);
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
n = (int)sendmsg(wsi->desc.sockfd, (struct msghdr *)&msg, 0);
|
2020-10-04 07:28:41 +01:00
|
|
|
if (n < 0) {
|
|
|
|
lwsl_notice("%s: rt dump req failed... permissions? errno %d\n",
|
|
|
|
__func__, LWS_ERRNO);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Responses are going to come asynchronously, since we can't process
|
|
|
|
* DNS lookups properly until we collected the initial netlink responses
|
|
|
|
* let's set a timer that will let us advance from lws_system
|
|
|
|
* LWS_SYSTATE_IFACE_COLDPLUG
|
|
|
|
*/
|
|
|
|
|
|
|
|
lwsl_debug("%s: starting netlink coldplug wait\n", __func__);
|
|
|
|
lws_sul_schedule(context, 0, &context->sul_nl_coldplug,
|
|
|
|
lws_netlink_coldplug_done_cb, 450 * LWS_US_PER_MS);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
bail2:
|
2021-02-02 20:21:09 +00:00
|
|
|
__lws_lc_untag(&wsi->lc);
|
2020-10-04 07:28:41 +01:00
|
|
|
compatible_close(wsi->desc.sockfd);
|
|
|
|
bail1:
|
|
|
|
lws_free(wsi);
|
|
|
|
bail:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
roles: compress role ops structs
role ops are usually only sparsely filled, there are currently 20
function pointers but several roles only fill in two. No single
role has more than 14 of the ops. On a 32/64 bit build this part
of the ops struct takes a fixed 80 / 160 bytes then.
First reduce the type of the callback reason part from uint16_t to
uint8_t, this saves 12 bytes unconditionally.
Change to a separate function pointer array with a nybble index
array, it costs 10 bytes for the index and a pointer to the
separate array, for 32-bit the cost is
2 + (4 x ops_used)
and for 64-bit
6 + (8 x ops_used)
for 2 x ops_used it means 32-bit: 10 vs 80 / 64-bit: 22 vs 160
For a typical system with h1 (9), h2 (14), listen (2), netlink (2),
pipe (1), raw_skt (3), ws (12), == 43 ops_used out of 140, it means
the .rodata for this reduced from 32-bit: 560 -> 174 (386 byte
saving) and 64-bit: 1120 -> 350 (770 byte saving)
This doesn't account for the changed function ops calling code, two
ways were tried, a preprocessor macro and explicit functions
For an x86_64 gcc 10 build with most options, release mode,
.text + .rodata
before patch: 553282
accessor macro: 552714 (568 byte saving)
accessor functions: 553674 (392 bytes worse than without patch)
therefore we went with the macros
2020-10-19 13:55:21 +01:00
|
|
|
static const lws_rops_t rops_table_netlink[] = {
|
|
|
|
/* 1 */ { .pt_init_destroy = rops_pt_init_destroy_netlink },
|
|
|
|
/* 2 */ { .handle_POLLIN = rops_handle_POLLIN_netlink },
|
|
|
|
};
|
|
|
|
|
2020-10-04 07:28:41 +01:00
|
|
|
const struct lws_role_ops role_ops_netlink = {
|
|
|
|
/* role name */ "netlink",
|
|
|
|
/* alpn id */ NULL,
|
roles: compress role ops structs
role ops are usually only sparsely filled, there are currently 20
function pointers but several roles only fill in two. No single
role has more than 14 of the ops. On a 32/64 bit build this part
of the ops struct takes a fixed 80 / 160 bytes then.
First reduce the type of the callback reason part from uint16_t to
uint8_t, this saves 12 bytes unconditionally.
Change to a separate function pointer array with a nybble index
array, it costs 10 bytes for the index and a pointer to the
separate array, for 32-bit the cost is
2 + (4 x ops_used)
and for 64-bit
6 + (8 x ops_used)
for 2 x ops_used it means 32-bit: 10 vs 80 / 64-bit: 22 vs 160
For a typical system with h1 (9), h2 (14), listen (2), netlink (2),
pipe (1), raw_skt (3), ws (12), == 43 ops_used out of 140, it means
the .rodata for this reduced from 32-bit: 560 -> 174 (386 byte
saving) and 64-bit: 1120 -> 350 (770 byte saving)
This doesn't account for the changed function ops calling code, two
ways were tried, a preprocessor macro and explicit functions
For an x86_64 gcc 10 build with most options, release mode,
.text + .rodata
before patch: 553282
accessor macro: 552714 (568 byte saving)
accessor functions: 553674 (392 bytes worse than without patch)
therefore we went with the macros
2020-10-19 13:55:21 +01:00
|
|
|
|
|
|
|
/* rops_table */ rops_table_netlink,
|
|
|
|
/* rops_idx */ {
|
|
|
|
/* LWS_ROPS_check_upgrades */
|
|
|
|
/* LWS_ROPS_pt_init_destroy */ 0x01,
|
|
|
|
/* LWS_ROPS_init_vhost */
|
|
|
|
/* LWS_ROPS_destroy_vhost */ 0x00,
|
|
|
|
/* LWS_ROPS_service_flag_pending */
|
|
|
|
/* LWS_ROPS_handle_POLLIN */ 0x02,
|
|
|
|
/* LWS_ROPS_handle_POLLOUT */
|
|
|
|
/* LWS_ROPS_perform_user_POLLOUT */ 0x00,
|
|
|
|
/* LWS_ROPS_callback_on_writable */
|
|
|
|
/* LWS_ROPS_tx_credit */ 0x00,
|
|
|
|
/* LWS_ROPS_write_role_protocol */
|
|
|
|
/* LWS_ROPS_encapsulation_parent */ 0x00,
|
|
|
|
/* LWS_ROPS_alpn_negotiated */
|
|
|
|
/* LWS_ROPS_close_via_role_protocol */ 0x00,
|
|
|
|
/* LWS_ROPS_close_role */
|
|
|
|
/* LWS_ROPS_close_kill_connection */ 0x00,
|
|
|
|
/* LWS_ROPS_destroy_role */
|
|
|
|
/* LWS_ROPS_adoption_bind */ 0x00,
|
|
|
|
/* LWS_ROPS_client_bind */
|
|
|
|
/* LWS_ROPS_issue_keepalive */ 0x00,
|
|
|
|
},
|
|
|
|
|
2020-10-04 07:28:41 +01:00
|
|
|
/* adoption_cb clnt, srv */ { 0, 0 },
|
|
|
|
/* rx_cb clnt, srv */ { 0, 0 },
|
|
|
|
/* writeable cb clnt, srv */ { 0, 0 },
|
|
|
|
/* close cb clnt, srv */ { 0, 0 },
|
|
|
|
/* protocol_bind_cb c,s */ { 0, 0 },
|
|
|
|
/* protocol_unbind_cb c,s */ { 0, 0 },
|
|
|
|
/* file_handle */ 0,
|
|
|
|
};
|