| 1 |
/* SPDX-License-Identifier: LGPL-2.1-only */ |
| 2 |
/* |
| 3 |
* Copyright (c) 2003-2008 Thomas Graf <[email protected]> |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* @ingroup rtnl |
| 8 |
* @defgroup neigh Neighbours |
| 9 |
* @brief |
| 10 |
* |
| 11 |
* The neighbour table establishes bindings between protocol addresses and |
| 12 |
* link layer addresses for hosts sharing the same physical link. This |
| 13 |
* module allows you to access and manipulate the content of these tables. |
| 14 |
* |
| 15 |
* @par Neighbour States |
| 16 |
* @code |
| 17 |
* NUD_INCOMPLETE |
| 18 |
* NUD_REACHABLE |
| 19 |
* NUD_STALE |
| 20 |
* NUD_DELAY |
| 21 |
* NUD_PROBE |
| 22 |
* NUD_FAILED |
| 23 |
* NUD_NOARP |
| 24 |
* NUD_PERMANENT |
| 25 |
* @endcode |
| 26 |
* |
| 27 |
* @par Neighbour Flags |
| 28 |
* @code |
| 29 |
* NTF_USE |
| 30 |
* NTF_PROXY |
| 31 |
* NTF_ROUTER |
| 32 |
* NTF_SELF |
| 33 |
* @endcode |
| 34 |
* |
| 35 |
* @par Neighbour Identification |
| 36 |
* A neighbour is uniquely identified by the attributes listed below, whenever |
| 37 |
* you refer to an existing neighbour all of the attributes must be set. |
| 38 |
* Neighbours from caches automatically have all required attributes set. |
| 39 |
* - interface index (rtnl_neigh_set_ifindex()) |
| 40 |
* - destination address (rtnl_neigh_set_dst()) |
| 41 |
* |
| 42 |
* @par Changeable Attributes |
| 43 |
* \anchor neigh_changeable |
| 44 |
* - state (rtnl_neigh_set_state()) |
| 45 |
* - link layer address (rtnl_neigh_set_lladdr()) |
| 46 |
* |
| 47 |
* @par Required Caches for Dumping |
| 48 |
* In order to dump neighbour attributes you must provide the following |
| 49 |
* caches via nl_cache_provide() |
| 50 |
* - link cache holding all links |
| 51 |
* |
| 52 |
* @par TODO |
| 53 |
* - Document proxy settings |
| 54 |
* - Document states and their influence |
| 55 |
* |
| 56 |
* @par 1) Retrieving information about configured neighbours |
| 57 |
* @code |
| 58 |
* // The first step is to retrieve a list of all available neighbour within |
| 59 |
* // the kernel and put them into a cache. |
| 60 |
* struct nl_cache *cache = rtnl_neigh_alloc_cache(sk); |
| 61 |
* |
| 62 |
* // Neighbours can then be looked up by the interface and destination |
| 63 |
* // address: |
| 64 |
* struct rtnl_neigh *neigh = rtnl_neigh_get(cache, ifindex, dst_addr); |
| 65 |
* |
| 66 |
* // After successful usage, the object must be given back to the cache |
| 67 |
* rtnl_neigh_put(neigh); |
| 68 |
* @endcode |
| 69 |
* |
| 70 |
* @par 2) Adding new neighbours |
| 71 |
* @code |
| 72 |
* // Allocate an empty neighbour handle to be filled out with the attributes |
| 73 |
* // of the new neighbour. |
| 74 |
* struct rtnl_neigh *neigh = rtnl_neigh_alloc(); |
| 75 |
* |
| 76 |
* // Fill out the attributes of the new neighbour |
| 77 |
* rtnl_neigh_set_ifindex(neigh, ifindex); |
| 78 |
* rtnl_neigh_set_dst(neigh, dst_addr); |
| 79 |
* rtnl_neigh_set_state(neigh, rtnl_neigh_str2state("permanent")); |
| 80 |
* |
| 81 |
* // Build the netlink message and send it to the kernel, the operation will |
| 82 |
* // block until the operation has been completed. Alternatively the required |
| 83 |
* // netlink message can be built using rtnl_neigh_build_add_request() |
| 84 |
* // to be sent out using nl_send_auto_complete(). |
| 85 |
* rtnl_neigh_add(sk, neigh, NLM_F_CREATE); |
| 86 |
* |
| 87 |
* // Free the memory |
| 88 |
* rtnl_neigh_put(neigh); |
| 89 |
* @endcode |
| 90 |
* |
| 91 |
* @par 3) Deleting an existing neighbour |
| 92 |
* @code |
| 93 |
* // Allocate an empty neighbour object to be filled out with the attributes |
| 94 |
* // matching the neighbour to be deleted. Alternatively a fully equipped |
| 95 |
* // neighbour object out of a cache can be used instead. |
| 96 |
* struct rtnl_neigh *neigh = rtnl_neigh_alloc(); |
| 97 |
* |
| 98 |
* // Neighbours are uniquely identified by their interface index and |
| 99 |
* // destination address, you may fill out other attributes but they |
| 100 |
* // will have no influence. |
| 101 |
* rtnl_neigh_set_ifindex(neigh, ifindex); |
| 102 |
* rtnl_neigh_set_dst(neigh, dst_addr); |
| 103 |
* |
| 104 |
* // Build the netlink message and send it to the kernel, the operation will |
| 105 |
* // block until the operation has been completed. Alternatively the required |
| 106 |
* // netlink message can be built using rtnl_neigh_build_delete_request() |
| 107 |
* // to be sent out using nl_send_auto_complete(). |
| 108 |
* rtnl_neigh_delete(sk, neigh, 0); |
| 109 |
* |
| 110 |
* // Free the memory |
| 111 |
* rtnl_neigh_put(neigh); |
| 112 |
* @endcode |
| 113 |
* |
| 114 |
* @par 4) Changing neighbour attributes |
| 115 |
* @code |
| 116 |
* // Allocate an empty neighbour object to be filled out with the attributes |
| 117 |
* // matching the neighbour to be changed and the new parameters. Alternatively |
| 118 |
* // a fully equipped modified neighbour object out of a cache can be used. |
| 119 |
* struct rtnl_neigh *neigh = rtnl_neigh_alloc(); |
| 120 |
* |
| 121 |
* // Identify the neighbour to be changed by its interface index and |
| 122 |
* // destination address |
| 123 |
* rtnl_neigh_set_ifindex(neigh, ifindex); |
| 124 |
* rtnl_neigh_set_dst(neigh, dst_addr); |
| 125 |
* |
| 126 |
* // The link layer address may be modified, if so it is wise to change |
| 127 |
* // its state to "permanent" in order to avoid having it overwritten. |
| 128 |
* rtnl_neigh_set_lladdr(neigh, lladdr); |
| 129 |
* |
| 130 |
* // Secondly the state can be modified allowing normal neighbours to be |
| 131 |
* // converted into permanent entries or to manually confirm a neighbour. |
| 132 |
* rtnl_neigh_set_state(neigh, state); |
| 133 |
* |
| 134 |
* // Build the netlink message and send it to the kernel, the operation will |
| 135 |
* // block until the operation has been completed. Alternatively the required |
| 136 |
* // netlink message can be built using rtnl_neigh_build_change_request() |
| 137 |
* // to be sent out using nl_send_auto_complete(). |
| 138 |
* rtnl_neigh_add(sk, neigh, NLM_F_REPLACE); |
| 139 |
* |
| 140 |
* // Free the memory |
| 141 |
* rtnl_neigh_put(neigh); |
| 142 |
* @endcode |
| 143 |
* @{ |
| 144 |
*/ |
| 145 |
|
| 146 |
#include "nl-default.h" |
| 147 |
|
| 148 |
#include <netlink/netlink.h> |
| 149 |
#include <netlink/utils.h> |
| 150 |
#include <netlink/hashtable.h> |
| 151 |
#include <netlink/route/rtnl.h> |
| 152 |
#include <netlink/route/neighbour.h> |
| 153 |
#include <netlink/route/link.h> |
| 154 |
#include <netlink/hashtable.h> |
| 155 |
|
| 156 |
#include "nl-route.h" |
| 157 |
#include "nl-priv-dynamic-core/nl-core.h" |
| 158 |
#include "nl-priv-dynamic-core/cache-api.h" |
| 159 |
|
| 160 |
/** @cond SKIP */ |
| 161 |
struct rtnl_ncacheinfo { |
| 162 |
uint32_t nci_confirmed; /**< Time since neighbour validty was last confirmed */ |
| 163 |
uint32_t nci_used; /**< Time since neighbour entry was last ued */ |
| 164 |
uint32_t nci_updated; /**< Time since last update */ |
| 165 |
uint32_t nci_refcnt; /**< Reference counter */ |
| 166 |
}; |
| 167 |
|
| 168 |
struct rtnl_neigh { |
| 169 |
NLHDR_COMMON |
| 170 |
uint32_t n_family; |
| 171 |
uint32_t n_ifindex; |
| 172 |
uint16_t n_state; |
| 173 |
uint8_t n_flags; |
| 174 |
uint32_t n_ext_flags; |
| 175 |
uint8_t n_type; |
| 176 |
struct nl_addr *n_lladdr; |
| 177 |
struct nl_addr *n_dst; |
| 178 |
uint32_t n_nhid; |
| 179 |
uint32_t n_probes; |
| 180 |
struct rtnl_ncacheinfo n_cacheinfo; |
| 181 |
uint32_t n_state_mask; |
| 182 |
uint32_t n_flag_mask; |
| 183 |
uint32_t n_ext_flag_mask; |
| 184 |
uint32_t n_master; |
| 185 |
uint16_t n_vlan; |
| 186 |
}; |
| 187 |
|
| 188 |
#define NEIGH_ATTR_FLAGS 0x01 |
| 189 |
#define NEIGH_ATTR_STATE 0x02 |
| 190 |
#define NEIGH_ATTR_LLADDR 0x04 |
| 191 |
#define NEIGH_ATTR_DST 0x08 |
| 192 |
#define NEIGH_ATTR_CACHEINFO 0x10 |
| 193 |
#define NEIGH_ATTR_IFINDEX 0x20 |
| 194 |
#define NEIGH_ATTR_FAMILY 0x40 |
| 195 |
#define NEIGH_ATTR_TYPE 0x80 |
| 196 |
#define NEIGH_ATTR_PROBES 0x0100 |
| 197 |
#define NEIGH_ATTR_MASTER 0x0200 |
| 198 |
#define NEIGH_ATTR_VLAN 0x0400 |
| 199 |
#define NEIGH_ATTR_NHID 0x0800 |
| 200 |
#define NEIGH_ATTR_EXT_FLAGS 0x1000 |
| 201 |
|
| 202 |
static struct nl_cache_ops rtnl_neigh_ops; |
| 203 |
static struct nl_object_ops neigh_obj_ops; |
| 204 |
/** @endcond */ |
| 205 |
|
| 206 |
static void neigh_free_data(struct nl_object *c) |
| 207 |
{ |
| 208 |
struct rtnl_neigh *neigh = nl_object_priv(c); |
| 209 |
|
| 210 |
if (!neigh) |
| 211 |
return; |
| 212 |
|
| 213 |
nl_addr_put(neigh->n_lladdr); |
| 214 |
nl_addr_put(neigh->n_dst); |
| 215 |
} |
| 216 |
|
| 217 |
static int neigh_clone(struct nl_object *_dst, struct nl_object *_src) |
| 218 |
{ |
| 219 |
struct rtnl_neigh *dst = nl_object_priv(_dst); |
| 220 |
struct rtnl_neigh *src = nl_object_priv(_src); |
| 221 |
|
| 222 |
dst->n_lladdr = NULL; |
| 223 |
dst->n_dst = NULL; |
| 224 |
|
| 225 |
if (src->n_lladdr) |
| 226 |
if (!(dst->n_lladdr = nl_addr_clone(src->n_lladdr))) |
| 227 |
return -NLE_NOMEM; |
| 228 |
|
| 229 |
if (src->n_dst) |
| 230 |
if (!(dst->n_dst = nl_addr_clone(src->n_dst))) |
| 231 |
return -NLE_NOMEM; |
| 232 |
|
| 233 |
return 0; |
| 234 |
} |
| 235 |
|
| 236 |
static void neigh_keygen(struct nl_object *obj, uint32_t *hashkey, |
| 237 |
uint32_t table_sz) |
| 238 |
{ |
| 239 |
struct rtnl_neigh *neigh = (struct rtnl_neigh *) obj; |
| 240 |
unsigned int nkey_sz; |
| 241 |
struct nl_addr *addr = NULL; |
| 242 |
struct neigh_hash_key { |
| 243 |
uint32_t n_family; |
| 244 |
uint32_t n_ifindex; |
| 245 |
uint16_t n_vlan; |
| 246 |
char n_addr[0]; |
| 247 |
} _nl_packed *nkey; |
| 248 |
char buf[INET6_ADDRSTRLEN+5]; |
| 249 |
|
| 250 |
if (neigh->n_family == AF_BRIDGE) { |
| 251 |
if (neigh->n_lladdr) |
| 252 |
addr = neigh->n_lladdr; |
| 253 |
} else if (neigh->n_dst) { |
| 254 |
addr = neigh->n_dst; |
| 255 |
} |
| 256 |
|
| 257 |
nkey_sz = sizeof(*nkey); |
| 258 |
if (addr) |
| 259 |
nkey_sz += nl_addr_get_len(addr); |
| 260 |
|
| 261 |
nkey = calloc(1, nkey_sz); |
| 262 |
if (!nkey) { |
| 263 |
*hashkey = 0; |
| 264 |
return; |
| 265 |
} |
| 266 |
nkey->n_family = neigh->n_family; |
| 267 |
if (neigh->n_family == AF_BRIDGE) { |
| 268 |
nkey->n_vlan = neigh->n_vlan; |
| 269 |
if (neigh->n_flags & NTF_SELF) |
| 270 |
nkey->n_ifindex = neigh->n_ifindex; |
| 271 |
else |
| 272 |
nkey->n_ifindex = neigh->n_master; |
| 273 |
} else |
| 274 |
nkey->n_ifindex = neigh->n_ifindex; |
| 275 |
|
| 276 |
if (addr) |
| 277 |
memcpy(nkey->n_addr, |
| 278 |
nl_addr_get_binary_addr(addr), |
| 279 |
nl_addr_get_len(addr)); |
| 280 |
|
| 281 |
*hashkey = nl_hash(nkey, nkey_sz, 0) % table_sz; |
| 282 |
|
| 283 |
NL_DBG(5, "neigh %p key (fam %d dev %d addr %s) keysz %d hash 0x%x\n", |
| 284 |
neigh, nkey->n_family, nkey->n_ifindex, |
| 285 |
nl_addr2str(addr, buf, sizeof(buf)), |
| 286 |
nkey_sz, *hashkey); |
| 287 |
|
| 288 |
free(nkey); |
| 289 |
|
| 290 |
return; |
| 291 |
} |
| 292 |
|
| 293 |
static uint64_t neigh_compare(struct nl_object *_a, struct nl_object *_b, |
| 294 |
uint64_t attrs, int flags) |
| 295 |
{ |
| 296 |
struct rtnl_neigh *a = (struct rtnl_neigh *) _a; |
| 297 |
struct rtnl_neigh *b = (struct rtnl_neigh *) _b; |
| 298 |
uint64_t diff = 0; |
| 299 |
|
| 300 |
#define _DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ATTR, a, b, EXPR) |
| 301 |
diff |= _DIFF(NEIGH_ATTR_IFINDEX, a->n_ifindex != b->n_ifindex); |
| 302 |
diff |= _DIFF(NEIGH_ATTR_FAMILY, a->n_family != b->n_family); |
| 303 |
diff |= _DIFF(NEIGH_ATTR_TYPE, a->n_type != b->n_type); |
| 304 |
diff |= _DIFF(NEIGH_ATTR_LLADDR, nl_addr_cmp(a->n_lladdr, b->n_lladdr)); |
| 305 |
diff |= _DIFF(NEIGH_ATTR_DST, nl_addr_cmp(a->n_dst, b->n_dst)); |
| 306 |
diff |= _DIFF(NEIGH_ATTR_MASTER, a->n_master != b->n_master); |
| 307 |
diff |= _DIFF(NEIGH_ATTR_VLAN, a->n_vlan != b->n_vlan); |
| 308 |
diff |= _DIFF(NEIGH_ATTR_NHID, a->n_nhid != b->n_nhid); |
| 309 |
|
| 310 |
if (flags & LOOSE_COMPARISON) { |
| 311 |
diff |= _DIFF(NEIGH_ATTR_STATE, |
| 312 |
(a->n_state ^ b->n_state) & b->n_state_mask); |
| 313 |
diff |= _DIFF(NEIGH_ATTR_FLAGS, |
| 314 |
(a->n_flags ^ b->n_flags) & b->n_flag_mask); |
| 315 |
diff |= _DIFF(NEIGH_ATTR_EXT_FLAGS, |
| 316 |
(a->n_ext_flags ^ b->n_ext_flags) & b->n_ext_flag_mask); |
| 317 |
} else { |
| 318 |
diff |= _DIFF(NEIGH_ATTR_STATE, a->n_state != b->n_state); |
| 319 |
diff |= _DIFF(NEIGH_ATTR_FLAGS, a->n_flags != b->n_flags); |
| 320 |
diff |= _DIFF(NEIGH_ATTR_EXT_FLAGS, a->n_ext_flags != b->n_ext_flags); |
| 321 |
} |
| 322 |
#undef _DIFF |
| 323 |
|
| 324 |
return diff; |
| 325 |
} |
| 326 |
|
| 327 |
static const struct trans_tbl neigh_attrs[] = { |
| 328 |
__ADD(NEIGH_ATTR_FLAGS, flags), |
| 329 |
__ADD(NEIGH_ATTR_STATE, state), |
| 330 |
__ADD(NEIGH_ATTR_LLADDR, lladdr), |
| 331 |
__ADD(NEIGH_ATTR_DST, dst), |
| 332 |
__ADD(NEIGH_ATTR_CACHEINFO, cacheinfo), |
| 333 |
__ADD(NEIGH_ATTR_IFINDEX, ifindex), |
| 334 |
__ADD(NEIGH_ATTR_FAMILY, family), |
| 335 |
__ADD(NEIGH_ATTR_TYPE, type), |
| 336 |
__ADD(NEIGH_ATTR_PROBES, probes), |
| 337 |
__ADD(NEIGH_ATTR_MASTER, master), |
| 338 |
__ADD(NEIGH_ATTR_VLAN, vlan), |
| 339 |
__ADD(NEIGH_ATTR_NHID, nhid), |
| 340 |
__ADD(NEIGH_ATTR_EXT_FLAGS, ext_flags), |
| 341 |
}; |
| 342 |
|
| 343 |
static char *neigh_attrs2str(int attrs, char *buf, size_t len) |
| 344 |
{ |
| 345 |
return __flags2str(attrs, buf, len, neigh_attrs, |
| 346 |
ARRAY_SIZE(neigh_attrs)); |
| 347 |
} |
| 348 |
|
| 349 |
static uint32_t neigh_id_attrs_get(struct nl_object *obj) |
| 350 |
{ |
| 351 |
struct rtnl_neigh *neigh = (struct rtnl_neigh *)obj; |
| 352 |
|
| 353 |
if (neigh->n_family == AF_BRIDGE) { |
| 354 |
if (neigh->n_flags & NTF_SELF) |
| 355 |
return (NEIGH_ATTR_LLADDR | NEIGH_ATTR_FAMILY | NEIGH_ATTR_IFINDEX | |
| 356 |
((neigh->ce_mask & NEIGH_ATTR_DST) ? NEIGH_ATTR_DST: 0) | |
| 357 |
((neigh->ce_mask & NEIGH_ATTR_NHID) ? NEIGH_ATTR_NHID: 0) | |
| 358 |
((neigh->ce_mask & NEIGH_ATTR_VLAN) ? NEIGH_ATTR_VLAN : 0)); |
| 359 |
else |
| 360 |
return (NEIGH_ATTR_LLADDR | NEIGH_ATTR_FAMILY | NEIGH_ATTR_MASTER | NEIGH_ATTR_VLAN); |
| 361 |
} else |
| 362 |
return neigh_obj_ops.oo_id_attrs; |
| 363 |
} |
| 364 |
|
| 365 |
static struct nla_policy neigh_policy[NDA_MAX+1] = { |
| 366 |
[NDA_CACHEINFO] = { .minlen = sizeof(struct nda_cacheinfo) }, |
| 367 |
[NDA_PROBES] = { .type = NLA_U32 }, |
| 368 |
}; |
| 369 |
|
| 370 |
static int neigh_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who, |
| 371 |
struct nlmsghdr *n, struct nl_parser_param *pp) |
| 372 |
{ |
| 373 |
struct rtnl_neigh *neigh; |
| 374 |
int err; |
| 375 |
|
| 376 |
if ((err = rtnl_neigh_parse(n, &neigh)) < 0) |
| 377 |
return err; |
| 378 |
|
| 379 |
err = pp->pp_cb((struct nl_object *) neigh, pp); |
| 380 |
|
| 381 |
rtnl_neigh_put(neigh); |
| 382 |
return err; |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
int rtnl_neigh_parse(struct nlmsghdr *n, struct rtnl_neigh **result) |
| 387 |
{ |
| 388 |
struct rtnl_neigh *neigh; |
| 389 |
struct nlattr *tb[NDA_MAX + 1]; |
| 390 |
struct ndmsg *nm; |
| 391 |
int err; |
| 392 |
|
| 393 |
neigh = rtnl_neigh_alloc(); |
| 394 |
if (!neigh) { |
| 395 |
err = -NLE_NOMEM; |
| 396 |
goto errout; |
| 397 |
} |
| 398 |
|
| 399 |
neigh->ce_msgtype = n->nlmsg_type; |
| 400 |
nm = nlmsg_data(n); |
| 401 |
|
| 402 |
err = nlmsg_parse(n, sizeof(*nm), tb, NDA_MAX, neigh_policy); |
| 403 |
if (err < 0) |
| 404 |
goto errout; |
| 405 |
|
| 406 |
neigh->n_family = nm->ndm_family; |
| 407 |
neigh->n_ifindex = nm->ndm_ifindex; |
| 408 |
neigh->n_state = nm->ndm_state; |
| 409 |
neigh->n_flags = nm->ndm_flags; |
| 410 |
neigh->n_type = nm->ndm_type; |
| 411 |
|
| 412 |
neigh->ce_mask |= (NEIGH_ATTR_FAMILY | NEIGH_ATTR_IFINDEX | |
| 413 |
NEIGH_ATTR_STATE | NEIGH_ATTR_FLAGS | |
| 414 |
NEIGH_ATTR_TYPE); |
| 415 |
|
| 416 |
if (tb[NDA_LLADDR]) { |
| 417 |
neigh->n_lladdr = nl_addr_alloc_attr(tb[NDA_LLADDR], AF_UNSPEC); |
| 418 |
if (!neigh->n_lladdr) { |
| 419 |
err = -NLE_NOMEM; |
| 420 |
goto errout; |
| 421 |
} |
| 422 |
nl_addr_set_family(neigh->n_lladdr, |
| 423 |
nl_addr_guess_family(neigh->n_lladdr)); |
| 424 |
neigh->ce_mask |= NEIGH_ATTR_LLADDR; |
| 425 |
} |
| 426 |
|
| 427 |
if (tb[NDA_DST]) { |
| 428 |
neigh->n_dst = nl_addr_alloc_attr(tb[NDA_DST], AF_UNSPEC); |
| 429 |
if (!neigh->n_dst) { |
| 430 |
err = -NLE_NOMEM; |
| 431 |
goto errout; |
| 432 |
} |
| 433 |
nl_addr_set_family(neigh->n_dst, |
| 434 |
nl_addr_guess_family(neigh->n_dst)); |
| 435 |
neigh->ce_mask |= NEIGH_ATTR_DST; |
| 436 |
} |
| 437 |
|
| 438 |
if (tb[NDA_CACHEINFO]) { |
| 439 |
struct nda_cacheinfo *ci = nla_data(tb[NDA_CACHEINFO]); |
| 440 |
|
| 441 |
neigh->n_cacheinfo.nci_confirmed = ci->ndm_confirmed; |
| 442 |
neigh->n_cacheinfo.nci_used = ci->ndm_used; |
| 443 |
neigh->n_cacheinfo.nci_updated = ci->ndm_updated; |
| 444 |
neigh->n_cacheinfo.nci_refcnt = ci->ndm_refcnt; |
| 445 |
|
| 446 |
neigh->ce_mask |= NEIGH_ATTR_CACHEINFO; |
| 447 |
} |
| 448 |
|
| 449 |
if (tb[NDA_PROBES]) { |
| 450 |
neigh->n_probes = nla_get_u32(tb[NDA_PROBES]); |
| 451 |
neigh->ce_mask |= NEIGH_ATTR_PROBES; |
| 452 |
} |
| 453 |
|
| 454 |
if (tb[NDA_VLAN]) { |
| 455 |
neigh->n_vlan = nla_get_u16(tb[NDA_VLAN]); |
| 456 |
neigh->ce_mask |= NEIGH_ATTR_VLAN; |
| 457 |
} |
| 458 |
|
| 459 |
if (tb[NDA_NH_ID]) { |
| 460 |
neigh->n_nhid = nla_get_u32(tb[NDA_NH_ID]); |
| 461 |
neigh->ce_mask |= NEIGH_ATTR_NHID; |
| 462 |
} |
| 463 |
|
| 464 |
if (tb[NDA_FLAGS_EXT]) { |
| 465 |
neigh->n_ext_flags = nla_get_u32(tb[NDA_FLAGS_EXT]); |
| 466 |
neigh->ce_mask |= NEIGH_ATTR_EXT_FLAGS; |
| 467 |
} |
| 468 |
|
| 469 |
/* |
| 470 |
* Get the bridge index for AF_BRIDGE family entries |
| 471 |
*/ |
| 472 |
if (neigh->n_family == AF_BRIDGE) { |
| 473 |
if (tb[NDA_MASTER]) { |
| 474 |
neigh->n_master = nla_get_u32(tb[NDA_MASTER]); |
| 475 |
neigh->ce_mask |= NEIGH_ATTR_MASTER; |
| 476 |
} else { |
| 477 |
struct nl_cache *lcache = nl_cache_mngt_require_safe("route/link"); |
| 478 |
if (lcache ) { |
| 479 |
struct rtnl_link *link = rtnl_link_get(lcache, |
| 480 |
neigh->n_ifindex); |
| 481 |
if (link) { |
| 482 |
neigh->n_master = link->l_master; |
| 483 |
rtnl_link_put(link); |
| 484 |
neigh->ce_mask |= NEIGH_ATTR_MASTER; |
| 485 |
} |
| 486 |
nl_cache_put(lcache); |
| 487 |
} |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
*result = neigh; |
| 492 |
return 0; |
| 493 |
|
| 494 |
errout: |
| 495 |
rtnl_neigh_put(neigh); |
| 496 |
return err; |
| 497 |
} |
| 498 |
|
| 499 |
static int neigh_request_update(struct nl_cache *c, struct nl_sock *h) |
| 500 |
{ |
| 501 |
int family = c->c_iarg1; |
| 502 |
|
| 503 |
if (family == AF_UNSPEC) { |
| 504 |
return nl_rtgen_request(h, RTM_GETNEIGH, family, NLM_F_DUMP); |
| 505 |
} else if (family == AF_BRIDGE) { |
| 506 |
struct ifinfomsg hdr = {.ifi_family = family}; |
| 507 |
struct nl_msg *msg; |
| 508 |
int err; |
| 509 |
|
| 510 |
msg = nlmsg_alloc_simple(RTM_GETNEIGH, NLM_F_REQUEST | NLM_F_DUMP); |
| 511 |
if (!msg) |
| 512 |
return -NLE_NOMEM; |
| 513 |
|
| 514 |
err = -NLE_MSGSIZE; |
| 515 |
if (nlmsg_append(msg, &hdr, sizeof(hdr), NLMSG_ALIGNTO) < 0) |
| 516 |
goto nla_put_failure; |
| 517 |
|
| 518 |
err = nl_send_auto(h, msg); |
| 519 |
if (err > 0) |
| 520 |
err = 0; |
| 521 |
|
| 522 |
nla_put_failure: |
| 523 |
nlmsg_free(msg); |
| 524 |
return err; |
| 525 |
} |
| 526 |
|
| 527 |
return -NLE_INVAL; |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
static void neigh_dump_line(struct nl_object *a, struct nl_dump_params *p) |
| 532 |
{ |
| 533 |
char dst[INET6_ADDRSTRLEN+5], lladdr[INET6_ADDRSTRLEN+5]; |
| 534 |
struct rtnl_neigh *n = (struct rtnl_neigh *) a; |
| 535 |
struct nl_cache *link_cache; |
| 536 |
char state[128], flags[64], ext_flags[64]; |
| 537 |
char buf[128]; |
| 538 |
|
| 539 |
link_cache = nl_cache_mngt_require_safe("route/link"); |
| 540 |
|
| 541 |
if (n->n_family != AF_UNSPEC) |
| 542 |
nl_dump_line(p, "%s ", nl_af2str(n->n_family, buf, sizeof(buf))); |
| 543 |
|
| 544 |
if (n->ce_mask & NEIGH_ATTR_DST) |
| 545 |
nl_dump_line(p, "%s ", nl_addr2str(n->n_dst, dst, sizeof(dst))); |
| 546 |
|
| 547 |
if (link_cache) |
| 548 |
nl_dump(p, "dev %s ", |
| 549 |
rtnl_link_i2name(link_cache, n->n_ifindex, |
| 550 |
state, sizeof(state))); |
| 551 |
else |
| 552 |
nl_dump(p, "dev %d ", n->n_ifindex); |
| 553 |
|
| 554 |
if (n->ce_mask & NEIGH_ATTR_LLADDR) |
| 555 |
nl_dump(p, "lladdr %s ", |
| 556 |
nl_addr2str(n->n_lladdr, lladdr, sizeof(lladdr))); |
| 557 |
|
| 558 |
if (n->ce_mask & NEIGH_ATTR_VLAN) |
| 559 |
nl_dump(p, "vlan %d ", n->n_vlan); |
| 560 |
|
| 561 |
if (n->ce_mask & NEIGH_ATTR_NHID) |
| 562 |
nl_dump(p, "nhid %u ", n->n_nhid); |
| 563 |
|
| 564 |
if (n->ce_mask & NEIGH_ATTR_MASTER) { |
| 565 |
if (link_cache) |
| 566 |
nl_dump(p, "%s ", rtnl_link_i2name(link_cache, n->n_master, |
| 567 |
state, sizeof(state))); |
| 568 |
else |
| 569 |
nl_dump(p, "%d ", n->n_master); |
| 570 |
} |
| 571 |
|
| 572 |
rtnl_neigh_state2str(n->n_state, state, sizeof(state)); |
| 573 |
rtnl_neigh_flags2str(n->n_flags, flags, sizeof(flags)); |
| 574 |
rtnl_neigh_extflags2str(n->n_ext_flags, ext_flags, sizeof(ext_flags)); |
| 575 |
|
| 576 |
if (state[0]) |
| 577 |
nl_dump(p, "<%s", state); |
| 578 |
if (flags[0]) |
| 579 |
nl_dump(p, "%s%s", state[0] ? "," : "<", flags); |
| 580 |
if (ext_flags[0]) |
| 581 |
nl_dump(p, "%s%s", state[0] || flags[0] ? "," : "<", ext_flags); |
| 582 |
if (state[0] || flags[0] || ext_flags[0]) |
| 583 |
nl_dump(p, ">"); |
| 584 |
nl_dump(p, "\n"); |
| 585 |
|
| 586 |
if (link_cache) |
| 587 |
nl_cache_put(link_cache); |
| 588 |
} |
| 589 |
|
| 590 |
static void neigh_dump_details(struct nl_object *a, struct nl_dump_params *p) |
| 591 |
{ |
| 592 |
char rtn_type[32]; |
| 593 |
struct rtnl_neigh *n = (struct rtnl_neigh *) a; |
| 594 |
int hz = nl_get_user_hz(); |
| 595 |
|
| 596 |
neigh_dump_line(a, p); |
| 597 |
|
| 598 |
nl_dump_line(p, " refcnt %u type %s confirmed %u used " |
| 599 |
"%u updated %u\n", |
| 600 |
n->n_cacheinfo.nci_refcnt, |
| 601 |
nl_rtntype2str(n->n_type, rtn_type, sizeof(rtn_type)), |
| 602 |
n->n_cacheinfo.nci_confirmed/hz, |
| 603 |
n->n_cacheinfo.nci_used/hz, n->n_cacheinfo.nci_updated/hz); |
| 604 |
} |
| 605 |
|
| 606 |
static void neigh_dump_stats(struct nl_object *a, struct nl_dump_params *p) |
| 607 |
{ |
| 608 |
neigh_dump_details(a, p); |
| 609 |
} |
| 610 |
|
| 611 |
/** |
| 612 |
* @name Neighbour Object Allocation/Freeage |
| 613 |
* @{ |
| 614 |
*/ |
| 615 |
|
| 616 |
struct rtnl_neigh *rtnl_neigh_alloc(void) |
| 617 |
{ |
| 618 |
return (struct rtnl_neigh *) nl_object_alloc(&neigh_obj_ops); |
| 619 |
} |
| 620 |
|
| 621 |
void rtnl_neigh_put(struct rtnl_neigh *neigh) |
| 622 |
{ |
| 623 |
nl_object_put((struct nl_object *) neigh); |
| 624 |
} |
| 625 |
|
| 626 |
/** @} */ |
| 627 |
|
| 628 |
/** |
| 629 |
* @name Neighbour Cache Managament |
| 630 |
* @{ |
| 631 |
*/ |
| 632 |
|
| 633 |
/** |
| 634 |
* Build a neighbour cache including all neighbours currently configured in the kernel. |
| 635 |
* @arg sock Netlink socket. |
| 636 |
* @arg result Pointer to store resulting cache. |
| 637 |
* |
| 638 |
* Allocates a new neighbour cache, initializes it properly and updates it |
| 639 |
* to include all neighbours currently configured in the kernel. |
| 640 |
* |
| 641 |
* @return 0 on success or a negative error code. |
| 642 |
*/ |
| 643 |
int rtnl_neigh_alloc_cache(struct nl_sock *sock, struct nl_cache **result) |
| 644 |
{ |
| 645 |
return nl_cache_alloc_and_fill(&rtnl_neigh_ops, sock, result); |
| 646 |
} |
| 647 |
|
| 648 |
/** |
| 649 |
* Build a neighbour cache including all neighbours currently configured in the kernel. |
| 650 |
* @arg sock Netlink socket. |
| 651 |
* @arg result Pointer to store resulting cache. |
| 652 |
* @arg flags Flags to apply to cache before filling |
| 653 |
* |
| 654 |
* Allocates a new neighbour cache, initializes it properly and updates it |
| 655 |
* to include all neighbours currently configured in the kernel. |
| 656 |
* |
| 657 |
* @return 0 on success or a negative error code. |
| 658 |
*/ |
| 659 |
int rtnl_neigh_alloc_cache_flags(struct nl_sock *sock, struct nl_cache **result, |
| 660 |
unsigned int flags) |
| 661 |
{ |
| 662 |
struct nl_cache * cache; |
| 663 |
int err; |
| 664 |
|
| 665 |
cache = nl_cache_alloc(&rtnl_neigh_ops); |
| 666 |
if (!cache) |
| 667 |
return -NLE_NOMEM; |
| 668 |
|
| 669 |
nl_cache_set_flags(cache, flags); |
| 670 |
|
| 671 |
if (sock && (err = nl_cache_refill(sock, cache)) < 0) { |
| 672 |
nl_cache_free(cache); |
| 673 |
return err; |
| 674 |
} |
| 675 |
|
| 676 |
*result = cache; |
| 677 |
return 0; |
| 678 |
} |
| 679 |
|
| 680 |
/** |
| 681 |
* Look up a neighbour by interface index and destination address |
| 682 |
* @arg cache neighbour cache |
| 683 |
* @arg ifindex interface index the neighbour is on |
| 684 |
* @arg dst destination address of the neighbour |
| 685 |
* |
| 686 |
* @return neighbour handle or NULL if no match was found. |
| 687 |
*/ |
| 688 |
struct rtnl_neigh * rtnl_neigh_get(struct nl_cache *cache, int ifindex, |
| 689 |
struct nl_addr *dst) |
| 690 |
{ |
| 691 |
struct rtnl_neigh *neigh; |
| 692 |
|
| 693 |
nl_list_for_each_entry(neigh, &cache->c_items, ce_list) { |
| 694 |
if (neigh->n_ifindex == ((unsigned)ifindex) && |
| 695 |
neigh->n_family == ((unsigned)dst->a_family) && |
| 696 |
!nl_addr_cmp(neigh->n_dst, dst)) { |
| 697 |
nl_object_get((struct nl_object *) neigh); |
| 698 |
return neigh; |
| 699 |
} |
| 700 |
} |
| 701 |
|
| 702 |
return NULL; |
| 703 |
} |
| 704 |
|
| 705 |
/** |
| 706 |
* Look up a neighbour by interface index, link layer address and vlan id |
| 707 |
* @arg cache neighbour cache |
| 708 |
* @arg ifindex interface index the neighbour is on |
| 709 |
* @arg lladdr link layer address of the neighbour |
| 710 |
* @arg vlan vlan id of the neighbour |
| 711 |
* |
| 712 |
* @return neighbour handle or NULL if no match was found. |
| 713 |
*/ |
| 714 |
struct rtnl_neigh * rtnl_neigh_get_by_vlan(struct nl_cache *cache, int ifindex, |
| 715 |
struct nl_addr *lladdr, int vlan) |
| 716 |
{ |
| 717 |
struct rtnl_neigh *neigh; |
| 718 |
|
| 719 |
nl_list_for_each_entry(neigh, &cache->c_items, ce_list) { |
| 720 |
if ((neigh->n_ifindex == (unsigned)ifindex) && |
| 721 |
neigh->n_vlan == vlan && neigh->n_lladdr && |
| 722 |
!nl_addr_cmp(neigh->n_lladdr, lladdr)) { |
| 723 |
nl_object_get((struct nl_object *) neigh); |
| 724 |
return neigh; |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
return NULL; |
| 729 |
} |
| 730 |
|
| 731 |
/** @} */ |
| 732 |
|
| 733 |
/** |
| 734 |
* @name Neighbour Addition |
| 735 |
* @{ |
| 736 |
*/ |
| 737 |
|
| 738 |
static int build_neigh_msg(struct rtnl_neigh *tmpl, int cmd, int flags, |
| 739 |
struct nl_msg **result) |
| 740 |
{ |
| 741 |
struct nl_msg *msg; |
| 742 |
struct ndmsg nhdr = { |
| 743 |
.ndm_ifindex = tmpl->n_ifindex, |
| 744 |
.ndm_state = NUD_PERMANENT, |
| 745 |
}; |
| 746 |
|
| 747 |
if (tmpl->n_family != AF_BRIDGE) { |
| 748 |
if (!(tmpl->ce_mask & NEIGH_ATTR_DST)) |
| 749 |
return -NLE_MISSING_ATTR; |
| 750 |
nhdr.ndm_family = nl_addr_get_family(tmpl->n_dst); |
| 751 |
} |
| 752 |
else |
| 753 |
nhdr.ndm_family = AF_BRIDGE; |
| 754 |
|
| 755 |
if (tmpl->ce_mask & NEIGH_ATTR_FLAGS) |
| 756 |
nhdr.ndm_flags = tmpl->n_flags; |
| 757 |
|
| 758 |
if (tmpl->ce_mask & NEIGH_ATTR_STATE) |
| 759 |
nhdr.ndm_state = tmpl->n_state; |
| 760 |
|
| 761 |
msg = nlmsg_alloc_simple(cmd, flags); |
| 762 |
if (!msg) |
| 763 |
return -NLE_NOMEM; |
| 764 |
|
| 765 |
if (nlmsg_append(msg, &nhdr, sizeof(nhdr), NLMSG_ALIGNTO) < 0) |
| 766 |
goto nla_put_failure; |
| 767 |
|
| 768 |
if (tmpl->ce_mask & NEIGH_ATTR_DST) |
| 769 |
NLA_PUT_ADDR(msg, NDA_DST, tmpl->n_dst); |
| 770 |
|
| 771 |
if (tmpl->ce_mask & NEIGH_ATTR_LLADDR) |
| 772 |
NLA_PUT_ADDR(msg, NDA_LLADDR, tmpl->n_lladdr); |
| 773 |
|
| 774 |
if (tmpl->ce_mask & NEIGH_ATTR_VLAN) |
| 775 |
NLA_PUT_U16(msg, NDA_VLAN, tmpl->n_vlan); |
| 776 |
|
| 777 |
if (tmpl->ce_mask & NEIGH_ATTR_NHID) |
| 778 |
NLA_PUT_U32(msg, NDA_NH_ID, tmpl->n_nhid); |
| 779 |
|
| 780 |
if (tmpl->ce_mask & NEIGH_ATTR_EXT_FLAGS) { |
| 781 |
/* The kernel does not allow setting the locked flag from |
| 782 |
* userspace, so unset it in the request. */ |
| 783 |
uint32_t ext_flags = tmpl->n_ext_flags & |
| 784 |
~(uint32_t)NTF_EXT_LOCKED; |
| 785 |
|
| 786 |
if (ext_flags) |
| 787 |
NLA_PUT_U32(msg, NDA_FLAGS_EXT, ext_flags); |
| 788 |
} |
| 789 |
|
| 790 |
*result = msg; |
| 791 |
return 0; |
| 792 |
|
| 793 |
nla_put_failure: |
| 794 |
nlmsg_free(msg); |
| 795 |
return -NLE_MSGSIZE; |
| 796 |
} |
| 797 |
|
| 798 |
/** |
| 799 |
* Build netlink request message to add a new neighbour |
| 800 |
* @arg tmpl template with data of new neighbour |
| 801 |
* @arg flags additional netlink message flags |
| 802 |
* @arg result Pointer to store resulting message. |
| 803 |
* |
| 804 |
* Builds a new netlink message requesting a addition of a new |
| 805 |
* neighbour. The netlink message header isn't fully equipped with |
| 806 |
* all relevant fields and must thus be sent out via nl_send_auto_complete() |
| 807 |
* or supplemented as needed. \a tmpl must contain the attributes of the new |
| 808 |
* neighbour set via \c rtnl_neigh_set_* functions. |
| 809 |
* |
| 810 |
* The following attributes must be set in the template: |
| 811 |
* - Interface index (rtnl_neigh_set_ifindex()) |
| 812 |
* - State (rtnl_neigh_set_state()) |
| 813 |
* - Destination address (rtnl_neigh_set_dst()) |
| 814 |
* - Link layer address (rtnl_neigh_set_lladdr()) |
| 815 |
* |
| 816 |
* @return 0 on success or a negative error code. |
| 817 |
*/ |
| 818 |
int rtnl_neigh_build_add_request(struct rtnl_neigh *tmpl, int flags, |
| 819 |
struct nl_msg **result) |
| 820 |
{ |
| 821 |
return build_neigh_msg(tmpl, RTM_NEWNEIGH, flags, result); |
| 822 |
} |
| 823 |
|
| 824 |
/** |
| 825 |
* Add a new neighbour |
| 826 |
* @arg sk Netlink socket. |
| 827 |
* @arg tmpl template with requested changes |
| 828 |
* @arg flags additional netlink message flags |
| 829 |
* |
| 830 |
* Builds a netlink message by calling rtnl_neigh_build_add_request(), |
| 831 |
* sends the request to the kernel and waits for the next ACK to be |
| 832 |
* received and thus blocks until the request has been fullfilled. |
| 833 |
* |
| 834 |
* The following attributes must be set in the template: |
| 835 |
* - Interface index (rtnl_neigh_set_ifindex()) |
| 836 |
* - State (rtnl_neigh_set_state()) |
| 837 |
* - Destination address (rtnl_neigh_set_dst()) |
| 838 |
* - Link layer address (rtnl_neigh_set_lladdr()) |
| 839 |
* |
| 840 |
* @return 0 on success or a negative error if an error occured. |
| 841 |
*/ |
| 842 |
int rtnl_neigh_add(struct nl_sock *sk, struct rtnl_neigh *tmpl, int flags) |
| 843 |
{ |
| 844 |
int err; |
| 845 |
struct nl_msg *msg; |
| 846 |
|
| 847 |
if ((err = rtnl_neigh_build_add_request(tmpl, flags, &msg)) < 0) |
| 848 |
return err; |
| 849 |
|
| 850 |
err = nl_send_auto_complete(sk, msg); |
| 851 |
nlmsg_free(msg); |
| 852 |
if (err < 0) |
| 853 |
return err; |
| 854 |
|
| 855 |
return wait_for_ack(sk); |
| 856 |
} |
| 857 |
|
| 858 |
/** @} */ |
| 859 |
|
| 860 |
/** |
| 861 |
* @name Neighbour Deletion |
| 862 |
* @{ |
| 863 |
*/ |
| 864 |
|
| 865 |
/** |
| 866 |
* Build a netlink request message to delete a neighbour |
| 867 |
* @arg neigh neighbour to delete |
| 868 |
* @arg flags additional netlink message flags |
| 869 |
* @arg result Pointer to store resulting message. |
| 870 |
* |
| 871 |
* Builds a new netlink message requesting a deletion of a neighbour. |
| 872 |
* The netlink message header isn't fully equipped with all relevant |
| 873 |
* fields and must thus be sent out via nl_send_auto_complete() |
| 874 |
* or supplemented as needed. \a neigh must point to an existing |
| 875 |
* neighbour. |
| 876 |
* |
| 877 |
* @return 0 on success or a negative error code. |
| 878 |
*/ |
| 879 |
int rtnl_neigh_build_delete_request(struct rtnl_neigh *neigh, int flags, |
| 880 |
struct nl_msg **result) |
| 881 |
{ |
| 882 |
return build_neigh_msg(neigh, RTM_DELNEIGH, flags, result); |
| 883 |
} |
| 884 |
|
| 885 |
/** |
| 886 |
* Delete a neighbour |
| 887 |
* @arg sk Netlink socket. |
| 888 |
* @arg neigh neighbour to delete |
| 889 |
* @arg flags additional netlink message flags |
| 890 |
* |
| 891 |
* Builds a netlink message by calling rtnl_neigh_build_delete_request(), |
| 892 |
* sends the request to the kernel and waits for the next ACK to be |
| 893 |
* received and thus blocks until the request has been fullfilled. |
| 894 |
* |
| 895 |
* @return 0 on success or a negative error if an error occured. |
| 896 |
*/ |
| 897 |
int rtnl_neigh_delete(struct nl_sock *sk, struct rtnl_neigh *neigh, |
| 898 |
int flags) |
| 899 |
{ |
| 900 |
struct nl_msg *msg; |
| 901 |
int err; |
| 902 |
|
| 903 |
if ((err = rtnl_neigh_build_delete_request(neigh, flags, &msg)) < 0) |
| 904 |
return err; |
| 905 |
|
| 906 |
err = nl_send_auto_complete(sk, msg); |
| 907 |
nlmsg_free(msg); |
| 908 |
if (err < 0) |
| 909 |
return err; |
| 910 |
|
| 911 |
return wait_for_ack(sk); |
| 912 |
} |
| 913 |
|
| 914 |
/** @} */ |
| 915 |
|
| 916 |
/** |
| 917 |
* @name Neighbour States Translations |
| 918 |
* @{ |
| 919 |
*/ |
| 920 |
|
| 921 |
static const struct trans_tbl neigh_states[] = { |
| 922 |
__ADD(NUD_INCOMPLETE, incomplete), |
| 923 |
__ADD(NUD_REACHABLE, reachable), |
| 924 |
__ADD(NUD_STALE, stale), |
| 925 |
__ADD(NUD_DELAY, delay), |
| 926 |
__ADD(NUD_PROBE, probe), |
| 927 |
__ADD(NUD_FAILED, failed), |
| 928 |
__ADD(NUD_NOARP, noarp), |
| 929 |
__ADD(NUD_PERMANENT, permanent), |
| 930 |
|
| 931 |
/* Accept this value for backward compatibility. Originally |
| 932 |
* there was a typo in the string value. This was fixed later, |
| 933 |
* but we still want to successfully parse "norarp". */ |
| 934 |
__ADD(NUD_NOARP, norarp), |
| 935 |
}; |
| 936 |
|
| 937 |
char * rtnl_neigh_state2str(int state, char *buf, size_t len) |
| 938 |
{ |
| 939 |
return __flags2str(state, buf, len, neigh_states, |
| 940 |
ARRAY_SIZE(neigh_states) - 1); |
| 941 |
} |
| 942 |
|
| 943 |
int rtnl_neigh_str2state(const char *name) |
| 944 |
{ |
| 945 |
return __str2type(name, neigh_states, ARRAY_SIZE(neigh_states)); |
| 946 |
} |
| 947 |
|
| 948 |
/** @} */ |
| 949 |
|
| 950 |
/** |
| 951 |
* @name Neighbour Flags Translations |
| 952 |
* @{ |
| 953 |
*/ |
| 954 |
|
| 955 |
static const struct trans_tbl neigh_flags[] = { |
| 956 |
__ADD(NTF_USE, use), |
| 957 |
__ADD(NTF_PROXY, proxy), |
| 958 |
__ADD(NTF_ROUTER, router), |
| 959 |
__ADD(NTF_SELF, self), |
| 960 |
__ADD(NTF_MASTER, master), |
| 961 |
__ADD(NTF_EXT_LEARNED, ext_learned), |
| 962 |
__ADD(NTF_OFFLOADED, offloaded), |
| 963 |
}; |
| 964 |
|
| 965 |
static const struct trans_tbl neigh_ext_flags[] = { |
| 966 |
__ADD(NTF_EXT_MANAGED, managed), |
| 967 |
__ADD(NTF_EXT_LOCKED, locked), |
| 968 |
}; |
| 969 |
|
| 970 |
char * rtnl_neigh_flags2str(int flags, char *buf, size_t len) |
| 971 |
{ |
| 972 |
return __flags2str(flags, buf, len, neigh_flags, |
| 973 |
ARRAY_SIZE(neigh_flags)); |
| 974 |
} |
| 975 |
|
| 976 |
int rtnl_neigh_str2flag(const char *name) |
| 977 |
{ |
| 978 |
return __str2type(name, neigh_flags, ARRAY_SIZE(neigh_flags)); |
| 979 |
} |
| 980 |
|
| 981 |
char * rtnl_neigh_extflags2str(uint32_t flags, char *buf, size_t len) |
| 982 |
{ |
| 983 |
return __flags2str(flags, buf, len, neigh_ext_flags, |
| 984 |
ARRAY_SIZE(neigh_ext_flags)); |
| 985 |
} |
| 986 |
|
| 987 |
uint32_t rtnl_neigh_str2extflag(const char *name) |
| 988 |
{ |
| 989 |
return __str2type(name, neigh_ext_flags, ARRAY_SIZE(neigh_ext_flags)); |
| 990 |
} |
| 991 |
|
| 992 |
/** @} */ |
| 993 |
|
| 994 |
/** |
| 995 |
* @name Attributes |
| 996 |
* @{ |
| 997 |
*/ |
| 998 |
|
| 999 |
void rtnl_neigh_set_state(struct rtnl_neigh *neigh, int state) |
| 1000 |
{ |
| 1001 |
neigh->n_state_mask |= state; |
| 1002 |
neigh->n_state |= state; |
| 1003 |
neigh->ce_mask |= NEIGH_ATTR_STATE; |
| 1004 |
} |
| 1005 |
|
| 1006 |
int rtnl_neigh_get_state(struct rtnl_neigh *neigh) |
| 1007 |
{ |
| 1008 |
if (neigh->ce_mask & NEIGH_ATTR_STATE) |
| 1009 |
return neigh->n_state; |
| 1010 |
else |
| 1011 |
return -1; |
| 1012 |
} |
| 1013 |
|
| 1014 |
void rtnl_neigh_unset_state(struct rtnl_neigh *neigh, int state) |
| 1015 |
{ |
| 1016 |
neigh->n_state_mask |= state; |
| 1017 |
neigh->n_state &= ~state; |
| 1018 |
neigh->ce_mask |= NEIGH_ATTR_STATE; |
| 1019 |
} |
| 1020 |
|
| 1021 |
void rtnl_neigh_set_flags(struct rtnl_neigh *neigh, unsigned int flags) |
| 1022 |
{ |
| 1023 |
neigh->n_flag_mask |= flags; |
| 1024 |
neigh->n_flags |= flags; |
| 1025 |
neigh->ce_mask |= NEIGH_ATTR_FLAGS; |
| 1026 |
} |
| 1027 |
|
| 1028 |
unsigned int rtnl_neigh_get_flags(struct rtnl_neigh *neigh) |
| 1029 |
{ |
| 1030 |
return neigh->n_flags; |
| 1031 |
} |
| 1032 |
|
| 1033 |
void rtnl_neigh_unset_flags(struct rtnl_neigh *neigh, unsigned int flags) |
| 1034 |
{ |
| 1035 |
neigh->n_flag_mask |= flags; |
| 1036 |
neigh->n_flags &= ~flags; |
| 1037 |
neigh->ce_mask |= NEIGH_ATTR_FLAGS; |
| 1038 |
} |
| 1039 |
|
| 1040 |
void rtnl_neigh_set_ext_flags(struct rtnl_neigh *neigh, uint32_t ext_flags) |
| 1041 |
{ |
| 1042 |
neigh->n_ext_flag_mask |= ext_flags; |
| 1043 |
neigh->n_ext_flags |= ext_flags; |
| 1044 |
neigh->ce_mask |= NEIGH_ATTR_EXT_FLAGS; |
| 1045 |
} |
| 1046 |
|
| 1047 |
int rtnl_neigh_get_ext_flags(struct rtnl_neigh *neigh, uint32_t *out_val) |
| 1048 |
{ |
| 1049 |
if (!(neigh->ce_mask & NEIGH_ATTR_EXT_FLAGS)) |
| 1050 |
return -NLE_NOATTR; |
| 1051 |
|
| 1052 |
*out_val = neigh->n_ext_flags; |
| 1053 |
return NLE_SUCCESS; |
| 1054 |
} |
| 1055 |
|
| 1056 |
void rtnl_neigh_unset_ext_flags(struct rtnl_neigh *neigh, uint32_t ext_flags) |
| 1057 |
{ |
| 1058 |
neigh->n_ext_flag_mask |= ext_flags; |
| 1059 |
neigh->n_ext_flags &= ~ext_flags; |
| 1060 |
neigh->ce_mask |= NEIGH_ATTR_EXT_FLAGS; |
| 1061 |
} |
| 1062 |
|
| 1063 |
void rtnl_neigh_set_ifindex(struct rtnl_neigh *neigh, int ifindex) |
| 1064 |
{ |
| 1065 |
neigh->n_ifindex = ifindex; |
| 1066 |
neigh->ce_mask |= NEIGH_ATTR_IFINDEX; |
| 1067 |
} |
| 1068 |
|
| 1069 |
int rtnl_neigh_get_ifindex(struct rtnl_neigh *neigh) |
| 1070 |
{ |
| 1071 |
return neigh->n_ifindex; |
| 1072 |
} |
| 1073 |
|
| 1074 |
static inline int __assign_addr(struct rtnl_neigh *neigh, struct nl_addr **pos, |
| 1075 |
struct nl_addr *new, int flag, int nocheck) |
| 1076 |
{ |
| 1077 |
if (!nocheck) { |
| 1078 |
if (neigh->ce_mask & NEIGH_ATTR_FAMILY) { |
| 1079 |
if (neigh->n_family != ((unsigned)new->a_family)) |
| 1080 |
return -NLE_AF_MISMATCH; |
| 1081 |
} else { |
| 1082 |
neigh->n_family = new->a_family; |
| 1083 |
neigh->ce_mask |= NEIGH_ATTR_FAMILY; |
| 1084 |
} |
| 1085 |
} |
| 1086 |
|
| 1087 |
if (*pos) |
| 1088 |
nl_addr_put(*pos); |
| 1089 |
|
| 1090 |
nl_addr_get(new); |
| 1091 |
*pos = new; |
| 1092 |
|
| 1093 |
neigh->ce_mask |= flag; |
| 1094 |
|
| 1095 |
return 0; |
| 1096 |
} |
| 1097 |
|
| 1098 |
void rtnl_neigh_set_lladdr(struct rtnl_neigh *neigh, struct nl_addr *addr) |
| 1099 |
{ |
| 1100 |
__assign_addr(neigh, &neigh->n_lladdr, addr, NEIGH_ATTR_LLADDR, 1); |
| 1101 |
} |
| 1102 |
|
| 1103 |
struct nl_addr *rtnl_neigh_get_lladdr(struct rtnl_neigh *neigh) |
| 1104 |
{ |
| 1105 |
if (neigh->ce_mask & NEIGH_ATTR_LLADDR) |
| 1106 |
return neigh->n_lladdr; |
| 1107 |
else |
| 1108 |
return NULL; |
| 1109 |
} |
| 1110 |
|
| 1111 |
int rtnl_neigh_set_dst(struct rtnl_neigh *neigh, struct nl_addr *addr) |
| 1112 |
{ |
| 1113 |
return __assign_addr(neigh, &neigh->n_dst, addr, |
| 1114 |
NEIGH_ATTR_DST, 0); |
| 1115 |
} |
| 1116 |
|
| 1117 |
struct nl_addr *rtnl_neigh_get_dst(struct rtnl_neigh *neigh) |
| 1118 |
{ |
| 1119 |
if (neigh->ce_mask & NEIGH_ATTR_DST) |
| 1120 |
return neigh->n_dst; |
| 1121 |
else |
| 1122 |
return NULL; |
| 1123 |
} |
| 1124 |
|
| 1125 |
void rtnl_neigh_set_family(struct rtnl_neigh *neigh, int family) |
| 1126 |
{ |
| 1127 |
neigh->n_family = family; |
| 1128 |
neigh->ce_mask |= NEIGH_ATTR_FAMILY; |
| 1129 |
} |
| 1130 |
|
| 1131 |
int rtnl_neigh_get_family(struct rtnl_neigh *neigh) |
| 1132 |
{ |
| 1133 |
return neigh->n_family; |
| 1134 |
} |
| 1135 |
|
| 1136 |
void rtnl_neigh_set_type(struct rtnl_neigh *neigh, int type) |
| 1137 |
{ |
| 1138 |
neigh->n_type = type; |
| 1139 |
neigh->ce_mask = NEIGH_ATTR_TYPE; |
| 1140 |
} |
| 1141 |
|
| 1142 |
int rtnl_neigh_get_type(struct rtnl_neigh *neigh) |
| 1143 |
{ |
| 1144 |
if (neigh->ce_mask & NEIGH_ATTR_TYPE) |
| 1145 |
return neigh->n_type; |
| 1146 |
else |
| 1147 |
return -1; |
| 1148 |
} |
| 1149 |
|
| 1150 |
void rtnl_neigh_set_vlan(struct rtnl_neigh *neigh, int vlan) |
| 1151 |
{ |
| 1152 |
neigh->n_vlan = vlan; |
| 1153 |
neigh->ce_mask |= NEIGH_ATTR_VLAN; |
| 1154 |
} |
| 1155 |
|
| 1156 |
int rtnl_neigh_get_vlan(struct rtnl_neigh *neigh) |
| 1157 |
{ |
| 1158 |
if (neigh->ce_mask & NEIGH_ATTR_VLAN) |
| 1159 |
return neigh->n_vlan; |
| 1160 |
else |
| 1161 |
return -1; |
| 1162 |
} |
| 1163 |
|
| 1164 |
void rtnl_neigh_set_master(struct rtnl_neigh *neigh, int ifindex) |
| 1165 |
{ |
| 1166 |
neigh->n_master = ifindex; |
| 1167 |
neigh->ce_mask |= NEIGH_ATTR_MASTER; |
| 1168 |
} |
| 1169 |
|
| 1170 |
int rtnl_neigh_get_master(struct rtnl_neigh *neigh) { |
| 1171 |
return neigh->n_master; |
| 1172 |
} |
| 1173 |
|
| 1174 |
void rtnl_neigh_set_nhid(struct rtnl_neigh *neigh, uint32_t nhid) |
| 1175 |
{ |
| 1176 |
neigh->n_nhid = nhid; |
| 1177 |
neigh->ce_mask |= NEIGH_ATTR_NHID; |
| 1178 |
} |
| 1179 |
|
| 1180 |
int rtnl_neigh_get_nhid(struct rtnl_neigh *neigh, uint32_t *out_val) { |
| 1181 |
if (!(neigh->ce_mask & NEIGH_ATTR_NHID)) |
| 1182 |
return -NLE_NOATTR; |
| 1183 |
|
| 1184 |
*out_val = neigh->n_nhid; |
| 1185 |
return NLE_SUCCESS; |
| 1186 |
} |
| 1187 |
|
| 1188 |
/** @} */ |
| 1189 |
|
| 1190 |
static struct nl_object_ops neigh_obj_ops = { |
| 1191 |
.oo_name = "route/neigh", |
| 1192 |
.oo_size = sizeof(struct rtnl_neigh), |
| 1193 |
.oo_free_data = neigh_free_data, |
| 1194 |
.oo_clone = neigh_clone, |
| 1195 |
.oo_dump = { |
| 1196 |
[NL_DUMP_LINE] = neigh_dump_line, |
| 1197 |
[NL_DUMP_DETAILS] = neigh_dump_details, |
| 1198 |
[NL_DUMP_STATS] = neigh_dump_stats, |
| 1199 |
}, |
| 1200 |
.oo_compare = neigh_compare, |
| 1201 |
.oo_keygen = neigh_keygen, |
| 1202 |
.oo_attrs2str = neigh_attrs2str, |
| 1203 |
.oo_id_attrs = (NEIGH_ATTR_IFINDEX | NEIGH_ATTR_DST | NEIGH_ATTR_FAMILY), |
| 1204 |
.oo_id_attrs_get = neigh_id_attrs_get |
| 1205 |
}; |
| 1206 |
|
| 1207 |
static struct nl_af_group neigh_groups[] = { |
| 1208 |
{ AF_UNSPEC, RTNLGRP_NEIGH }, |
| 1209 |
{ AF_BRIDGE, RTNLGRP_NEIGH }, |
| 1210 |
{ END_OF_GROUP_LIST }, |
| 1211 |
}; |
| 1212 |
|
| 1213 |
static struct nl_cache_ops rtnl_neigh_ops = { |
| 1214 |
.co_name = "route/neigh", |
| 1215 |
.co_hdrsize = sizeof(struct ndmsg), |
| 1216 |
.co_msgtypes = { |
| 1217 |
{ RTM_NEWNEIGH, NL_ACT_NEW, "new" }, |
| 1218 |
{ RTM_DELNEIGH, NL_ACT_DEL, "del" }, |
| 1219 |
{ RTM_GETNEIGH, NL_ACT_GET, "get" }, |
| 1220 |
END_OF_MSGTYPES_LIST, |
| 1221 |
}, |
| 1222 |
.co_protocol = NETLINK_ROUTE, |
| 1223 |
.co_groups = neigh_groups, |
| 1224 |
.co_request_update = neigh_request_update, |
| 1225 |
.co_msg_parser = neigh_msg_parser, |
| 1226 |
.co_obj_ops = &neigh_obj_ops, |
| 1227 |
}; |
| 1228 |
|
| 1229 |
static void _nl_init neigh_init(void) |
| 1230 |
{ |
| 1231 |
nl_cache_mngt_register(&rtnl_neigh_ops); |
| 1232 |
} |
| 1233 |
|
| 1234 |
static void _nl_exit neigh_exit(void) |
| 1235 |
{ |
| 1236 |
nl_cache_mngt_unregister(&rtnl_neigh_ops); |
| 1237 |
} |
| 1238 |
|
| 1239 |
/** @} */ |