1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 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
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
* Copyright (C) 2009 Alexander Sack <asac@jwsdot.com>
*
* This file is part of:
* ntrack - Network Status Tracking for Desktop Applications
* http://launchpad.net/ntrack
*
* ntrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* ntrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with ntrack. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ntrackmonitor.h"
#include "ntrack-arch.h"
#include "ntracklist.h"
#include <error.h>
#include <assert.h>
/* import netlink stuff required */
#include <netlink/netlink.h>
#include <netlink/object.h>
#include <netlink/object-api.h>
#include <netlink/route/addr.h>
#include <netlink/route/link.h>
#include <netlink/route/rtnl.h>
#include <netlink/route/route.h>
#include <linux/if.h>
#ifndef ROUTE_ATTR_OIF
#define ROUTE_ATTR_OIF 0x000400
#endif
#ifndef ROUTE_ATTR_GATEWAY
#define ROUTE_ATTR_GATEWAY 0x000800
#endif
struct _nl_info
{
struct nl_handle *handle;
struct nl_handle *smart_update_handle;
struct nl_cache_mngr *smart_update_cache_mngr;
struct nl_cache *route_cache;
struct nl_cache *addr_cache;
struct nl_cache *link_cache;
struct rtnl_route *topmost_route;
int update_run;
};
static void
setup_main_handle (struct _nl_info *nl_info);
static void
setup_smart_handle (struct _nl_info *nl_info);
static int
smart_cache_update (ntrack_monitor_t *self, ntrackpointer user_data);
static int
update_caches (struct _nl_info *nl_info);
static void
update_connectivity (ntrack_monitor_t *self, struct _nl_info *nl_info);
static int
cmp_rtnl_route_metric (ntrackpointer first, ntrackpointer second);
static void
op_default_route_appeared (struct _ntrack_monitor_arch *self, struct _nl_info *nl_info);
static void
op_default_route_disappeared (struct _ntrack_monitor_arch *self, struct _nl_info *nl_info);
static void
op_default_route_changed (struct _ntrack_monitor_arch *self, struct _nl_info *nl_info);
ntrack_monitor_t*
_ntrack_arch_new (ntrack_monitor_arch_event_f callback, ntrackpointer user_data)
{
struct _ntrack_monitor_arch *arch = calloc(sizeof(struct _ntrack_monitor_arch), 1);
struct _nl_info *nl_info = calloc (sizeof (struct _nl_info), 1);
/* setup ntrack_monitor_t part */
((ntrack_monitor_t *)arch)->state = NTRACK_STATE_UNKNOWN;
/* setup struct ntrack_monitor_arch part */
arch->data = nl_info;
arch->cb = callback;
arch->cb_user_data = user_data;
nl_info->handle = nl_handle_alloc();
if (!nl_info->handle)
error (1, 255, "OOM (nl_handle_alloc)");
if (nl_connect (nl_info->handle, NETLINK_ROUTE))
error (1, 256, "NETLNK_ROUTE connect failed (nl_connect)");
nl_info->smart_update_handle = nl_handle_alloc();
if (!nl_info->smart_update_handle)
error (1, 257, "OOM (nl_handle_alloc)");
if (nl_connect (nl_info->smart_update_handle, NETLINK_ROUTE))
error (1, 258, "NETLNK_ROUTE connect failed (nl_connect)");
/* set smart update handle to nonblocking */
if (nl_socket_set_nonblocking (nl_info->smart_update_handle))
error (1,260,"cannot set smart handle to nonblocking");
setup_main_handle (nl_info);
setup_smart_handle (nl_info);
update_caches (nl_info);
update_connectivity ((ntrack_monitor_t *) arch, nl_info);
return (ntrack_monitor_t*) arch;
}
int*
_ntrack_arch_get_rfds (ntrack_monitor_t *self)
{
struct _ntrack_monitor_arch *arch = (struct _ntrack_monitor_arch*) self;
int* fds = calloc (sizeof(int), 2);
struct _nl_info *info = arch->data;
/*
* just the smart_update_handle is used to get smart
* updates hint from the application mainloop.
*/
fds[0] = nl_cache_mngr_get_fd (info->smart_update_cache_mngr);
return fds;
}
int
_ntrack_arch_process_data (ntrackpointer self, int* fds)
{
struct _ntrack_monitor_arch *arch = self;
struct _nl_info *nl_info = arch->data;
/*
* libnl is too broken to rely on caches ... so just consume
* all data from the fd until libnl is fixed
*/
int rval = nl_cache_mngr_data_ready (nl_info->smart_update_cache_mngr);
/*
* if all looks good and there was relevant data for the managed
* caches then we attempt a smart cache update. This will flush
* our caches and reinterpret the new state and emit events
* as appropriate.
*/
if(rval)
if (!smart_cache_update (self, nl_info))
return -1;
return rval;
}
static void
setup_main_handle (struct _nl_info *nl_info)
{
if (!nl_info->route_cache) {
nl_info->route_cache = rtnl_route_alloc_cache (nl_info->handle);
if (!nl_info->route_cache)
error (1, 259, "OOM (rtnl_route_alloc_cache)");
nl_cache_mngt_provide (nl_info->route_cache);
}
if (!nl_info->addr_cache) {
nl_info->addr_cache = rtnl_addr_alloc_cache (nl_info->handle);
if (!nl_info->addr_cache)
error (1, 261, "OOM (rtnl_addr_alloc_cache)");
nl_cache_mngt_provide (nl_info->addr_cache);
}
if (!nl_info->link_cache) {
nl_info->link_cache = rtnl_link_alloc_cache (nl_info->handle);
if (!nl_info->link_cache)
error (1, 263, "OOM (rtnl_link_alloc_cache)");
nl_cache_mngt_provide (nl_info->link_cache);
}
}
static void
setup_smart_handle (struct _nl_info *nl_info)
{
struct nl_cache *tmp_cache = 0;
nl_info->smart_update_handle = nl_handle_alloc ();
nl_info->smart_update_cache_mngr =
nl_cache_mngr_alloc(nl_info->smart_update_handle,
NETLINK_ROUTE,
NL_AUTO_PROVIDE);
if (!nl_info->smart_update_cache_mngr)
error (1, 265, "cache mngr alloc failed");
/* add caches for events we are interested in. We
* don't need to remember them as we are solely
* interested on whether something of relevance for
* us changed or not. If thats the case we will
* explicitly refill the primary caches and calculate
* network status changes.
*/
tmp_cache = nl_cache_mngr_add (nl_info->smart_update_cache_mngr,
"route/addr",
NULL /* change_func_t -
we dont want those
notifications */
);
if (!tmp_cache)
error (1, 266, "cache mngr add fail.");
tmp_cache = nl_cache_mngr_add (nl_info->smart_update_cache_mngr,
"route/link",
NULL /* change_func_t -
we dont want those
notifications */
);
if (!tmp_cache)
error (1, 267, "cache mngr add fail.");
tmp_cache = nl_cache_mngr_add (nl_info->smart_update_cache_mngr,
"route/route",
NULL /* change_func_t -
we dont want those
notifications */
);
if (!tmp_cache)
error (1, 268, "cache mngr add fail.");
}
static int
smart_cache_update (ntrack_monitor_t *self, ntrackpointer user_data)
{
struct _nl_info *nl_info = user_data;
update_caches(nl_info);
update_connectivity (self, nl_info);
/*
if (debug)
dump_caches(nl_info);
*/
return TRUE;
}
static int
update_caches (struct _nl_info *nl_info)
{
assert (nl_info->route_cache);
assert (nl_info->link_cache);
assert (nl_info->addr_cache);
if (nl_cache_refill (nl_info->handle, nl_info->route_cache))
error (1, 280, "REFILL route (nl_cache_refill)");
if (nl_cache_refill (nl_info->handle, nl_info->addr_cache))
error (1, 281, "REFILL link (nl_cache_refill)");
if (nl_cache_refill (nl_info->handle, nl_info->link_cache))
error (1, 281, "REFILL link (nl_cache_refill)");
return TRUE;
}
static int
cmp_rtnl_route_metric (ntrackpointer first, ntrackpointer second)
{
struct rtnl_route *first_route = first;
struct rtnl_route *second_route = second;
int metric1, metric2;
if (first_route == NULL || second_route == NULL)
return 0;
metric1 = rtnl_route_get_prio (first_route);
metric2 = rtnl_route_get_prio (second_route);
if (metric1 < metric2)
return -1;
else if (metric1 > metric2)
return 1;
return 0;
}
static void
op_default_route_appeared (struct _ntrack_monitor_arch *self, struct _nl_info *nl_info)
{
if (self->cb)
self->cb ((ntrack_monitor_t*) self, NTRACK_EVENT_CONNECT, self->cb_user_data);
}
static void
op_default_route_disappeared (struct _ntrack_monitor_arch *self, struct _nl_info *nl_info)
{
if (self->cb)
self->cb ((ntrack_monitor_t*) self, NTRACK_EVENT_DISCONNECT, self->cb_user_data);
}
static void
op_default_route_changed (struct _ntrack_monitor_arch *self, struct _nl_info *nl_info)
{
if (self->cb)
self->cb ((ntrack_monitor_t*) self, NTRACK_EVENT_RECONNECT, self->cb_user_data);
}
static int
nl_link_index_cmp (ntrackpointer a, ntrackpointer b)
{
struct rtnl_link *link_a = a;
struct rtnl_link *link_b = b;
int ifindex_a = rtnl_link_get_ifindex (link_a);
int ifindex_b = rtnl_link_get_ifindex (link_b);
if (ifindex_a < ifindex_b)
return -1;
if (ifindex_a > ifindex_b)
return 1;
return 0;
}
static struct rtnl_link*
get_nl_link_by_index (ntrack_monitor_t *self, ntrack_list_t *linklist, int iindex)
{
ntrack_list_t *i = linklist;
struct rtnl_link *result = NULL;
while (i) {
struct rtnl_link *link = i->data;
if (rtnl_link_get_ifindex(link) == iindex) {
result = link;
break;
}
if (rtnl_link_get_ifindex(link) > iindex)
break;
i = i->next;
}
return result;
}
static void
update_connectivity (ntrack_monitor_t *self, struct _nl_info *nl_info)
{
struct rtnl_route *topmost_route = NULL;
struct rtnl_route *filter_route = rtnl_route_alloc ();
struct nl_addr *dst_filter = nl_addr_alloc (0);
struct nl_object *iter;
ntrack_list_t *link_list = NULL;
/* fill link info */
iter = nl_cache_get_first (nl_info->link_cache);
while (iter) {
link_list = ntrack_list_insert_sorted (link_list, iter, nl_link_index_cmp);
nl_object_get (iter);
iter = nl_cache_get_next (iter);
}
/* get topmost route with link that has is up and has carrier */
nl_addr_set_family (dst_filter, AF_INET);
nl_addr_set_binary_addr (dst_filter, NULL, 0);
rtnl_route_set_dst (filter_route, dst_filter);
iter = nl_cache_get_first (nl_info->route_cache);
while (iter) {
struct rtnl_link *iter_link;
if (!nl_object_match_filter (iter, OBJ_CAST(filter_route)))
goto next;
iter_link = get_nl_link_by_index (self, link_list, rtnl_route_get_oif ((struct rtnl_route*) iter));
/* if we dont have a link for the current oif,
* this is not a valid topmost_route */
if (!iter_link)
goto next;
/* if new route is lower than the current topmost, continue */
if (topmost_route && cmp_rtnl_route_metric (topmost_route, iter) <= 0)
goto next;
/* if the link of the potential new topmost route is up,
we replace the current topmost_route */
if (rtnl_link_get_flags (iter_link) & IFF_LOWER_UP) {
/* release ref to current last topmost_route */
if (topmost_route)
nl_object_put (OBJ_CAST(topmost_route));
/* remember current topmost_route candidate */
topmost_route = (struct rtnl_route*) iter;
nl_object_get (OBJ_CAST (topmost_route));
}
next:
iter = nl_cache_get_next (iter);
}
/* cleanup the link references */
ntrack_list_foreach (link_list, (ntrack_list_cb_f*) nl_object_put, NULL);
ntrack_list_free (link_list);
nl_addr_put (dst_filter);
nl_object_put (OBJ_CAST (filter_route));
if (!nl_info->topmost_route && topmost_route) {
nl_info->topmost_route = topmost_route;
op_default_route_appeared ((struct _ntrack_monitor_arch*) self, nl_info);
} else if (nl_info->topmost_route && !topmost_route) {
nl_object_put (OBJ_CAST(nl_info->topmost_route));
nl_info->topmost_route = NULL;
op_default_route_disappeared ((struct _ntrack_monitor_arch*) self, nl_info);
} else if (nl_info->topmost_route && topmost_route) {
int diff_bits;
if ((diff_bits = (route_obj_ops.oo_id_attrs | ROUTE_ATTR_OIF
| ROUTE_ATTR_GATEWAY) &
nl_object_diff (OBJ_CAST (nl_info->topmost_route),
OBJ_CAST (topmost_route)))) {
char diff_str[4096];
char attr1[4096];
char attr2[4096];
nl_object_attrs2str (OBJ_CAST (nl_info->topmost_route),
diff_bits,
diff_str,
4096);
nl_object_attr_list (OBJ_CAST (nl_info->topmost_route),
attr1,
4096);
nl_object_attr_list (OBJ_CAST (topmost_route),
attr2,
4096);
printf ("DIFF_BITS for changed topmost route: %s\n", diff_str);
printf ("ATTR1 : %s\n", attr1);
printf ("ATTR2 : %s\n", attr2);
nl_object_put (OBJ_CAST(nl_info->topmost_route));
nl_info->topmost_route = topmost_route;
op_default_route_changed ((struct _ntrack_monitor_arch*) self, nl_info);
}
} else {
/* on first run of update_connectivity we emit a disappeared signal if no
* new topmost_route was found; in this way monitors can update their initial
* state to OFFLINE */
if (!nl_info->update_run)
op_default_route_disappeared ((struct _ntrack_monitor_arch*) self, nl_info);
}
/* set to update_run mode so we dont signal disappear if no topmost_route
* exists and nothing changed */
if (!nl_info->update_run)
nl_info->update_run = TRUE;
}
|