neigh.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. /*
  2. * lib/route/neigh.c Neighbours
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation version 2.1
  7. * of the License.
  8. *
  9. * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
  10. */
  11. /**
  12. * @ingroup rtnl
  13. * @defgroup neigh Neighbours
  14. * @brief
  15. *
  16. * The neighbour table establishes bindings between protocol addresses and
  17. * link layer addresses for hosts sharing the same physical link. This
  18. * module allows you to access and manipulate the content of these tables.
  19. *
  20. * @par Neighbour States
  21. * @code
  22. * NUD_INCOMPLETE
  23. * NUD_REACHABLE
  24. * NUD_STALE
  25. * NUD_DELAY
  26. * NUD_PROBE
  27. * NUD_FAILED
  28. * NUD_NOARP
  29. * NUD_PERMANENT
  30. * @endcode
  31. *
  32. * @par Neighbour Flags
  33. * @code
  34. * NTF_USE
  35. * NTF_PROXY
  36. * NTF_ROUTER
  37. * @endcode
  38. *
  39. * @par Neighbour Identification
  40. * A neighbour is uniquely identified by the attributes listed below, whenever
  41. * you refer to an existing neighbour all of the attributes must be set.
  42. * Neighbours from caches automatically have all required attributes set.
  43. * - interface index (rtnl_neigh_set_ifindex())
  44. * - destination address (rtnl_neigh_set_dst())
  45. *
  46. * @par Changeable Attributes
  47. * \anchor neigh_changeable
  48. * - state (rtnl_neigh_set_state())
  49. * - link layer address (rtnl_neigh_set_lladdr())
  50. *
  51. * @par Required Caches for Dumping
  52. * In order to dump neighbour attributes you must provide the following
  53. * caches via nl_cache_provide()
  54. * - link cache holding all links
  55. *
  56. * @par TODO
  57. * - Document proxy settings
  58. * - Document states and their influence
  59. *
  60. * @par 1) Retrieving information about configured neighbours
  61. * @code
  62. * // The first step is to retrieve a list of all available neighbour within
  63. * // the kernel and put them into a cache.
  64. * struct nl_cache *cache = rtnl_neigh_alloc_cache(sk);
  65. *
  66. * // Neighbours can then be looked up by the interface and destination
  67. * // address:
  68. * struct rtnl_neigh *neigh = rtnl_neigh_get(cache, ifindex, dst_addr);
  69. *
  70. * // After successful usage, the object must be given back to the cache
  71. * rtnl_neigh_put(neigh);
  72. * @endcode
  73. *
  74. * @par 2) Adding new neighbours
  75. * @code
  76. * // Allocate an empty neighbour handle to be filled out with the attributes
  77. * // of the new neighbour.
  78. * struct rtnl_neigh *neigh = rtnl_neigh_alloc();
  79. *
  80. * // Fill out the attributes of the new neighbour
  81. * rtnl_neigh_set_ifindex(neigh, ifindex);
  82. * rtnl_neigh_set_dst(neigh, dst_addr);
  83. * rtnl_neigh_set_state(neigh, rtnl_neigh_str2state("permanent"));
  84. *
  85. * // Build the netlink message and send it to the kernel, the operation will
  86. * // block until the operation has been completed. Alternatively the required
  87. * // netlink message can be built using rtnl_neigh_build_add_request()
  88. * // to be sent out using nl_send_auto_complete().
  89. * rtnl_neigh_add(sk, neigh, NLM_F_CREATE);
  90. *
  91. * // Free the memory
  92. * rtnl_neigh_put(neigh);
  93. * @endcode
  94. *
  95. * @par 3) Deleting an existing neighbour
  96. * @code
  97. * // Allocate an empty neighbour object to be filled out with the attributes
  98. * // matching the neighbour to be deleted. Alternatively a fully equipped
  99. * // neighbour object out of a cache can be used instead.
  100. * struct rtnl_neigh *neigh = rtnl_neigh_alloc();
  101. *
  102. * // Neighbours are uniquely identified by their interface index and
  103. * // destination address, you may fill out other attributes but they
  104. * // will have no influence.
  105. * rtnl_neigh_set_ifindex(neigh, ifindex);
  106. * rtnl_neigh_set_dst(neigh, dst_addr);
  107. *
  108. * // Build the netlink message and send it to the kernel, the operation will
  109. * // block until the operation has been completed. Alternatively the required
  110. * // netlink message can be built using rtnl_neigh_build_delete_request()
  111. * // to be sent out using nl_send_auto_complete().
  112. * rtnl_neigh_delete(sk, neigh, 0);
  113. *
  114. * // Free the memory
  115. * rtnl_neigh_put(neigh);
  116. * @endcode
  117. *
  118. * @par 4) Changing neighbour attributes
  119. * @code
  120. * // Allocate an empty neighbour object to be filled out with the attributes
  121. * // matching the neighbour to be changed and the new parameters. Alternatively
  122. * // a fully equipped modified neighbour object out of a cache can be used.
  123. * struct rtnl_neigh *neigh = rtnl_neigh_alloc();
  124. *
  125. * // Identify the neighbour to be changed by its interface index and
  126. * // destination address
  127. * rtnl_neigh_set_ifindex(neigh, ifindex);
  128. * rtnl_neigh_set_dst(neigh, dst_addr);
  129. *
  130. * // The link layer address may be modified, if so it is wise to change
  131. * // its state to "permanent" in order to avoid having it overwritten.
  132. * rtnl_neigh_set_lladdr(neigh, lladdr);
  133. *
  134. * // Secondly the state can be modified allowing normal neighbours to be
  135. * // converted into permanent entries or to manually confirm a neighbour.
  136. * rtnl_neigh_set_state(neigh, state);
  137. *
  138. * // Build the netlink message and send it to the kernel, the operation will
  139. * // block until the operation has been completed. Alternatively the required
  140. * // netlink message can be built using rtnl_neigh_build_change_request()
  141. * // to be sent out using nl_send_auto_complete().
  142. * rtnl_neigh_add(sk, neigh, NLM_F_REPLACE);
  143. *
  144. * // Free the memory
  145. * rtnl_neigh_put(neigh);
  146. * @endcode
  147. * @{
  148. */
  149. #include <netlink-private/netlink.h>
  150. #include <netlink/netlink.h>
  151. #include <netlink/utils.h>
  152. #include <netlink/hashtable.h>
  153. #include <netlink/route/rtnl.h>
  154. #include <netlink/route/neighbour.h>
  155. #include <netlink/route/link.h>
  156. #include <netlink/hashtable.h>
  157. /** @cond SKIP */
  158. #define NEIGH_ATTR_FLAGS 0x01
  159. #define NEIGH_ATTR_STATE 0x02
  160. #define NEIGH_ATTR_LLADDR 0x04
  161. #define NEIGH_ATTR_DST 0x08
  162. #define NEIGH_ATTR_CACHEINFO 0x10
  163. #define NEIGH_ATTR_IFINDEX 0x20
  164. #define NEIGH_ATTR_FAMILY 0x40
  165. #define NEIGH_ATTR_TYPE 0x80
  166. #define NEIGH_ATTR_PROBES 0x100
  167. #define NEIGH_ATTR_MASTER 0x200
  168. static struct nl_cache_ops rtnl_neigh_ops;
  169. static struct nl_object_ops neigh_obj_ops;
  170. /** @endcond */
  171. static void neigh_free_data(struct nl_object *c)
  172. {
  173. struct rtnl_neigh *neigh = nl_object_priv(c);
  174. if (!neigh)
  175. return;
  176. nl_addr_put(neigh->n_lladdr);
  177. nl_addr_put(neigh->n_dst);
  178. }
  179. static int neigh_clone(struct nl_object *_dst, struct nl_object *_src)
  180. {
  181. struct rtnl_neigh *dst = nl_object_priv(_dst);
  182. struct rtnl_neigh *src = nl_object_priv(_src);
  183. if (src->n_lladdr)
  184. if (!(dst->n_lladdr = nl_addr_clone(src->n_lladdr)))
  185. return -NLE_NOMEM;
  186. if (src->n_dst)
  187. if (!(dst->n_dst = nl_addr_clone(src->n_dst)))
  188. return -NLE_NOMEM;
  189. return 0;
  190. }
  191. static void neigh_keygen(struct nl_object *obj, uint32_t *hashkey,
  192. uint32_t table_sz)
  193. {
  194. struct rtnl_neigh *neigh = (struct rtnl_neigh *) obj;
  195. unsigned int nkey_sz;
  196. struct nl_addr *addr = NULL;
  197. struct neigh_hash_key {
  198. uint32_t n_family;
  199. uint32_t n_ifindex;
  200. char n_addr[0];
  201. } __attribute__((packed)) *nkey;
  202. #ifdef NL_DEBUG
  203. char buf[INET6_ADDRSTRLEN+5];
  204. #endif
  205. if (neigh->n_family == AF_BRIDGE) {
  206. if (neigh->n_lladdr)
  207. addr = neigh->n_lladdr;
  208. } else if (neigh->n_dst) {
  209. addr = neigh->n_dst;
  210. }
  211. nkey_sz = sizeof(*nkey);
  212. if (addr)
  213. nkey_sz += nl_addr_get_len(addr);
  214. nkey = calloc(1, nkey_sz);
  215. if (!nkey) {
  216. *hashkey = 0;
  217. return;
  218. }
  219. nkey->n_family = neigh->n_family;
  220. if (neigh->n_family == AF_BRIDGE)
  221. nkey->n_ifindex = neigh->n_master;
  222. else
  223. nkey->n_ifindex = neigh->n_ifindex;
  224. if (addr)
  225. memcpy(nkey->n_addr,
  226. nl_addr_get_binary_addr(addr),
  227. nl_addr_get_len(addr));
  228. *hashkey = nl_hash(nkey, nkey_sz, 0) % table_sz;
  229. NL_DBG(5, "neigh %p key (fam %d dev %d addr %s) keysz %d hash 0x%x\n",
  230. neigh, nkey->n_family, nkey->n_ifindex,
  231. nl_addr2str(addr, buf, sizeof(buf)),
  232. nkey_sz, *hashkey);
  233. free(nkey);
  234. return;
  235. }
  236. static int neigh_compare(struct nl_object *_a, struct nl_object *_b,
  237. uint32_t attrs, int flags)
  238. {
  239. struct rtnl_neigh *a = (struct rtnl_neigh *) _a;
  240. struct rtnl_neigh *b = (struct rtnl_neigh *) _b;
  241. int diff = 0;
  242. #define NEIGH_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, NEIGH_ATTR_##ATTR, a, b, EXPR)
  243. diff |= NEIGH_DIFF(IFINDEX, a->n_ifindex != b->n_ifindex);
  244. diff |= NEIGH_DIFF(FAMILY, a->n_family != b->n_family);
  245. diff |= NEIGH_DIFF(TYPE, a->n_type != b->n_type);
  246. diff |= NEIGH_DIFF(LLADDR, nl_addr_cmp(a->n_lladdr, b->n_lladdr));
  247. diff |= NEIGH_DIFF(DST, nl_addr_cmp(a->n_dst, b->n_dst));
  248. diff |= NEIGH_DIFF(MASTER, a->n_master != b->n_master);
  249. if (flags & LOOSE_COMPARISON) {
  250. diff |= NEIGH_DIFF(STATE,
  251. (a->n_state ^ b->n_state) & b->n_state_mask);
  252. diff |= NEIGH_DIFF(FLAGS,
  253. (a->n_flags ^ b->n_flags) & b->n_flag_mask);
  254. } else {
  255. diff |= NEIGH_DIFF(STATE, a->n_state != b->n_state);
  256. diff |= NEIGH_DIFF(FLAGS, a->n_flags != b->n_flags);
  257. }
  258. #undef NEIGH_DIFF
  259. return diff;
  260. }
  261. static const struct trans_tbl neigh_attrs[] = {
  262. __ADD(NEIGH_ATTR_FLAGS, flags)
  263. __ADD(NEIGH_ATTR_STATE, state)
  264. __ADD(NEIGH_ATTR_LLADDR, lladdr)
  265. __ADD(NEIGH_ATTR_DST, dst)
  266. __ADD(NEIGH_ATTR_CACHEINFO, cacheinfo)
  267. __ADD(NEIGH_ATTR_IFINDEX, ifindex)
  268. __ADD(NEIGH_ATTR_FAMILY, family)
  269. __ADD(NEIGH_ATTR_TYPE, type)
  270. __ADD(NEIGH_ATTR_PROBES, probes)
  271. };
  272. static char *neigh_attrs2str(int attrs, char *buf, size_t len)
  273. {
  274. return __flags2str(attrs, buf, len, neigh_attrs,
  275. ARRAY_SIZE(neigh_attrs));
  276. }
  277. static uint32_t neigh_id_attrs_get(struct nl_object *obj)
  278. {
  279. struct rtnl_neigh *neigh = (struct rtnl_neigh *)obj;
  280. if (neigh->n_family == AF_BRIDGE)
  281. return (NEIGH_ATTR_LLADDR | NEIGH_ATTR_FAMILY | NEIGH_ATTR_MASTER);
  282. else
  283. return (NEIGH_ATTR_IFINDEX | NEIGH_ATTR_DST | NEIGH_ATTR_FAMILY);
  284. }
  285. static struct nla_policy neigh_policy[NDA_MAX+1] = {
  286. [NDA_CACHEINFO] = { .minlen = sizeof(struct nda_cacheinfo) },
  287. [NDA_PROBES] = { .type = NLA_U32 },
  288. };
  289. static int neigh_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
  290. struct nlmsghdr *n, struct nl_parser_param *pp)
  291. {
  292. struct rtnl_neigh *neigh;
  293. int err;
  294. if ((err = rtnl_neigh_parse(n, &neigh)) < 0)
  295. return err;
  296. err = pp->pp_cb((struct nl_object *) neigh, pp);
  297. rtnl_neigh_put(neigh);
  298. return err;
  299. }
  300. int rtnl_neigh_parse(struct nlmsghdr *n, struct rtnl_neigh **result)
  301. {
  302. struct rtnl_neigh *neigh;
  303. struct nlattr *tb[NDA_MAX + 1];
  304. struct ndmsg *nm;
  305. int err;
  306. neigh = rtnl_neigh_alloc();
  307. if (!neigh) {
  308. err = -NLE_NOMEM;
  309. goto errout;
  310. }
  311. neigh->ce_msgtype = n->nlmsg_type;
  312. nm = nlmsg_data(n);
  313. err = nlmsg_parse(n, sizeof(*nm), tb, NDA_MAX, neigh_policy);
  314. if (err < 0)
  315. goto errout;
  316. neigh->n_family = nm->ndm_family;
  317. neigh->n_ifindex = nm->ndm_ifindex;
  318. neigh->n_state = nm->ndm_state;
  319. neigh->n_flags = nm->ndm_flags;
  320. neigh->n_type = nm->ndm_type;
  321. neigh->ce_mask |= (NEIGH_ATTR_FAMILY | NEIGH_ATTR_IFINDEX |
  322. NEIGH_ATTR_STATE | NEIGH_ATTR_FLAGS |
  323. NEIGH_ATTR_TYPE);
  324. if (tb[NDA_LLADDR]) {
  325. neigh->n_lladdr = nl_addr_alloc_attr(tb[NDA_LLADDR], AF_UNSPEC);
  326. if (!neigh->n_lladdr) {
  327. err = -NLE_NOMEM;
  328. goto errout;
  329. }
  330. nl_addr_set_family(neigh->n_lladdr,
  331. nl_addr_guess_family(neigh->n_lladdr));
  332. neigh->ce_mask |= NEIGH_ATTR_LLADDR;
  333. }
  334. if (tb[NDA_DST]) {
  335. neigh->n_dst = nl_addr_alloc_attr(tb[NDA_DST], neigh->n_family);
  336. if (!neigh->n_dst) {
  337. err = -NLE_NOMEM;
  338. goto errout;
  339. }
  340. neigh->ce_mask |= NEIGH_ATTR_DST;
  341. }
  342. if (tb[NDA_CACHEINFO]) {
  343. struct nda_cacheinfo *ci = nla_data(tb[NDA_CACHEINFO]);
  344. neigh->n_cacheinfo.nci_confirmed = ci->ndm_confirmed;
  345. neigh->n_cacheinfo.nci_used = ci->ndm_used;
  346. neigh->n_cacheinfo.nci_updated = ci->ndm_updated;
  347. neigh->n_cacheinfo.nci_refcnt = ci->ndm_refcnt;
  348. neigh->ce_mask |= NEIGH_ATTR_CACHEINFO;
  349. }
  350. if (tb[NDA_PROBES]) {
  351. neigh->n_probes = nla_get_u32(tb[NDA_PROBES]);
  352. neigh->ce_mask |= NEIGH_ATTR_PROBES;
  353. }
  354. /*
  355. * Get the bridge index for AF_BRIDGE family entries
  356. */
  357. if (neigh->n_family == AF_BRIDGE) {
  358. struct nl_cache *lcache = nl_cache_mngt_require_safe("route/link");
  359. if (lcache ) {
  360. struct rtnl_link *link = rtnl_link_get(lcache,
  361. neigh->n_ifindex);
  362. if (link) {
  363. neigh->n_master = link->l_master;
  364. rtnl_link_put(link);
  365. neigh->ce_mask |= NEIGH_ATTR_MASTER;
  366. }
  367. nl_cache_put(lcache);
  368. }
  369. }
  370. *result = neigh;
  371. return 0;
  372. errout:
  373. rtnl_neigh_put(neigh);
  374. return err;
  375. }
  376. static int neigh_request_update(struct nl_cache *c, struct nl_sock *h)
  377. {
  378. int family = c->c_iarg1;
  379. return nl_rtgen_request(h, RTM_GETNEIGH, family, NLM_F_DUMP);
  380. }
  381. static void neigh_dump_line(struct nl_object *a, struct nl_dump_params *p)
  382. {
  383. char dst[INET6_ADDRSTRLEN+5], lladdr[INET6_ADDRSTRLEN+5];
  384. struct rtnl_neigh *n = (struct rtnl_neigh *) a;
  385. struct nl_cache *link_cache;
  386. char state[128], flags[64];
  387. link_cache = nl_cache_mngt_require_safe("route/link");
  388. if (n->n_family != AF_BRIDGE)
  389. nl_dump_line(p, "%s ", nl_addr2str(n->n_dst, dst, sizeof(dst)));
  390. if (link_cache)
  391. nl_dump(p, "dev %s ",
  392. rtnl_link_i2name(link_cache, n->n_ifindex,
  393. state, sizeof(state)));
  394. else
  395. nl_dump(p, "dev %d ", n->n_ifindex);
  396. if (n->ce_mask & NEIGH_ATTR_LLADDR)
  397. nl_dump(p, "lladdr %s ",
  398. nl_addr2str(n->n_lladdr, lladdr, sizeof(lladdr)));
  399. rtnl_neigh_state2str(n->n_state, state, sizeof(state));
  400. rtnl_neigh_flags2str(n->n_flags, flags, sizeof(flags));
  401. if (state[0])
  402. nl_dump(p, "<%s", state);
  403. if (flags[0])
  404. nl_dump(p, "%s%s", state[0] ? "," : "<", flags);
  405. if (state[0] || flags[0])
  406. nl_dump(p, ">");
  407. nl_dump(p, "\n");
  408. if (link_cache)
  409. nl_cache_put(link_cache);
  410. }
  411. static void neigh_dump_details(struct nl_object *a, struct nl_dump_params *p)
  412. {
  413. char rtn_type[32];
  414. struct rtnl_neigh *n = (struct rtnl_neigh *) a;
  415. int hz = nl_get_user_hz();
  416. neigh_dump_line(a, p);
  417. nl_dump_line(p, " refcnt %u type %s confirmed %u used "
  418. "%u updated %u\n",
  419. n->n_cacheinfo.nci_refcnt,
  420. nl_rtntype2str(n->n_type, rtn_type, sizeof(rtn_type)),
  421. n->n_cacheinfo.nci_confirmed/hz,
  422. n->n_cacheinfo.nci_used/hz, n->n_cacheinfo.nci_updated/hz);
  423. }
  424. static void neigh_dump_stats(struct nl_object *a, struct nl_dump_params *p)
  425. {
  426. neigh_dump_details(a, p);
  427. }
  428. /**
  429. * @name Neighbour Object Allocation/Freeage
  430. * @{
  431. */
  432. struct rtnl_neigh *rtnl_neigh_alloc(void)
  433. {
  434. return (struct rtnl_neigh *) nl_object_alloc(&neigh_obj_ops);
  435. }
  436. void rtnl_neigh_put(struct rtnl_neigh *neigh)
  437. {
  438. nl_object_put((struct nl_object *) neigh);
  439. }
  440. /** @} */
  441. /**
  442. * @name Neighbour Cache Managament
  443. * @{
  444. */
  445. /**
  446. * Build a neighbour cache including all neighbours currently configured in the kernel.
  447. * @arg sock Netlink socket.
  448. * @arg result Pointer to store resulting cache.
  449. *
  450. * Allocates a new neighbour cache, initializes it properly and updates it
  451. * to include all neighbours currently configured in the kernel.
  452. *
  453. * @return 0 on success or a negative error code.
  454. */
  455. int rtnl_neigh_alloc_cache(struct nl_sock *sock, struct nl_cache **result)
  456. {
  457. return nl_cache_alloc_and_fill(&rtnl_neigh_ops, sock, result);
  458. }
  459. /**
  460. * Look up a neighbour by interface index and destination address
  461. * @arg cache neighbour cache
  462. * @arg ifindex interface index the neighbour is on
  463. * @arg dst destination address of the neighbour
  464. *
  465. * @return neighbour handle or NULL if no match was found.
  466. */
  467. struct rtnl_neigh * rtnl_neigh_get(struct nl_cache *cache, int ifindex,
  468. struct nl_addr *dst)
  469. {
  470. struct rtnl_neigh *neigh;
  471. nl_list_for_each_entry(neigh, &cache->c_items, ce_list) {
  472. if (neigh->n_ifindex == ifindex &&
  473. !nl_addr_cmp(neigh->n_dst, dst)) {
  474. nl_object_get((struct nl_object *) neigh);
  475. return neigh;
  476. }
  477. }
  478. return NULL;
  479. }
  480. /** @} */
  481. /**
  482. * @name Neighbour Addition
  483. * @{
  484. */
  485. static int build_neigh_msg(struct rtnl_neigh *tmpl, int cmd, int flags,
  486. struct nl_msg **result)
  487. {
  488. struct nl_msg *msg;
  489. struct ndmsg nhdr = {
  490. .ndm_ifindex = tmpl->n_ifindex,
  491. .ndm_state = NUD_PERMANENT,
  492. };
  493. if (tmpl->n_family != AF_BRIDGE) {
  494. if (!(tmpl->ce_mask & NEIGH_ATTR_DST))
  495. return -NLE_MISSING_ATTR;
  496. nhdr.ndm_family = nl_addr_get_family(tmpl->n_dst);
  497. }
  498. else
  499. nhdr.ndm_family = AF_BRIDGE;
  500. if (tmpl->ce_mask & NEIGH_ATTR_FLAGS)
  501. nhdr.ndm_flags = tmpl->n_flags;
  502. if (tmpl->ce_mask & NEIGH_ATTR_STATE)
  503. nhdr.ndm_state = tmpl->n_state;
  504. msg = nlmsg_alloc_simple(cmd, flags);
  505. if (!msg)
  506. return -NLE_NOMEM;
  507. if (nlmsg_append(msg, &nhdr, sizeof(nhdr), NLMSG_ALIGNTO) < 0)
  508. goto nla_put_failure;
  509. if (tmpl->n_family != AF_BRIDGE)
  510. NLA_PUT_ADDR(msg, NDA_DST, tmpl->n_dst);
  511. if (tmpl->ce_mask & NEIGH_ATTR_LLADDR)
  512. NLA_PUT_ADDR(msg, NDA_LLADDR, tmpl->n_lladdr);
  513. *result = msg;
  514. return 0;
  515. nla_put_failure:
  516. nlmsg_free(msg);
  517. return -NLE_MSGSIZE;
  518. }
  519. /**
  520. * Build netlink request message to add a new neighbour
  521. * @arg tmpl template with data of new neighbour
  522. * @arg flags additional netlink message flags
  523. * @arg result Pointer to store resulting message.
  524. *
  525. * Builds a new netlink message requesting a addition of a new
  526. * neighbour. The netlink message header isn't fully equipped with
  527. * all relevant fields and must thus be sent out via nl_send_auto_complete()
  528. * or supplemented as needed. \a tmpl must contain the attributes of the new
  529. * neighbour set via \c rtnl_neigh_set_* functions.
  530. *
  531. * The following attributes must be set in the template:
  532. * - Interface index (rtnl_neigh_set_ifindex())
  533. * - State (rtnl_neigh_set_state())
  534. * - Destination address (rtnl_neigh_set_dst())
  535. * - Link layer address (rtnl_neigh_set_lladdr())
  536. *
  537. * @return 0 on success or a negative error code.
  538. */
  539. int rtnl_neigh_build_add_request(struct rtnl_neigh *tmpl, int flags,
  540. struct nl_msg **result)
  541. {
  542. return build_neigh_msg(tmpl, RTM_NEWNEIGH, flags, result);
  543. }
  544. /**
  545. * Add a new neighbour
  546. * @arg sk Netlink socket.
  547. * @arg tmpl template with requested changes
  548. * @arg flags additional netlink message flags
  549. *
  550. * Builds a netlink message by calling rtnl_neigh_build_add_request(),
  551. * sends the request to the kernel and waits for the next ACK to be
  552. * received and thus blocks until the request has been fullfilled.
  553. *
  554. * The following attributes must be set in the template:
  555. * - Interface index (rtnl_neigh_set_ifindex())
  556. * - State (rtnl_neigh_set_state())
  557. * - Destination address (rtnl_neigh_set_dst())
  558. * - Link layer address (rtnl_neigh_set_lladdr())
  559. *
  560. * @return 0 on sucess or a negative error if an error occured.
  561. */
  562. int rtnl_neigh_add(struct nl_sock *sk, struct rtnl_neigh *tmpl, int flags)
  563. {
  564. int err;
  565. struct nl_msg *msg;
  566. if ((err = rtnl_neigh_build_add_request(tmpl, flags, &msg)) < 0)
  567. return err;
  568. err = nl_send_auto_complete(sk, msg);
  569. nlmsg_free(msg);
  570. if (err < 0)
  571. return err;
  572. return wait_for_ack(sk);
  573. }
  574. /** @} */
  575. /**
  576. * @name Neighbour Deletion
  577. * @{
  578. */
  579. /**
  580. * Build a netlink request message to delete a neighbour
  581. * @arg neigh neighbour to delete
  582. * @arg flags additional netlink message flags
  583. * @arg result Pointer to store resulting message.
  584. *
  585. * Builds a new netlink message requesting a deletion of a neighbour.
  586. * The netlink message header isn't fully equipped with all relevant
  587. * fields and must thus be sent out via nl_send_auto_complete()
  588. * or supplemented as needed. \a neigh must point to an existing
  589. * neighbour.
  590. *
  591. * @return 0 on success or a negative error code.
  592. */
  593. int rtnl_neigh_build_delete_request(struct rtnl_neigh *neigh, int flags,
  594. struct nl_msg **result)
  595. {
  596. return build_neigh_msg(neigh, RTM_DELNEIGH, flags, result);
  597. }
  598. /**
  599. * Delete a neighbour
  600. * @arg sk Netlink socket.
  601. * @arg neigh neighbour to delete
  602. * @arg flags additional netlink message flags
  603. *
  604. * Builds a netlink message by calling rtnl_neigh_build_delete_request(),
  605. * sends the request to the kernel and waits for the next ACK to be
  606. * received and thus blocks until the request has been fullfilled.
  607. *
  608. * @return 0 on sucess or a negative error if an error occured.
  609. */
  610. int rtnl_neigh_delete(struct nl_sock *sk, struct rtnl_neigh *neigh,
  611. int flags)
  612. {
  613. struct nl_msg *msg;
  614. int err;
  615. if ((err = rtnl_neigh_build_delete_request(neigh, flags, &msg)) < 0)
  616. return err;
  617. err = nl_send_auto_complete(sk, msg);
  618. nlmsg_free(msg);
  619. if (err < 0)
  620. return err;
  621. return wait_for_ack(sk);
  622. }
  623. /** @} */
  624. /**
  625. * @name Neighbour States Translations
  626. * @{
  627. */
  628. static const struct trans_tbl neigh_states[] = {
  629. __ADD(NUD_INCOMPLETE, incomplete)
  630. __ADD(NUD_REACHABLE, reachable)
  631. __ADD(NUD_STALE, stale)
  632. __ADD(NUD_DELAY, delay)
  633. __ADD(NUD_PROBE, probe)
  634. __ADD(NUD_FAILED, failed)
  635. __ADD(NUD_NOARP, norarp)
  636. __ADD(NUD_PERMANENT, permanent)
  637. };
  638. char * rtnl_neigh_state2str(int state, char *buf, size_t len)
  639. {
  640. return __flags2str(state, buf, len, neigh_states,
  641. ARRAY_SIZE(neigh_states));
  642. }
  643. int rtnl_neigh_str2state(const char *name)
  644. {
  645. return __str2type(name, neigh_states, ARRAY_SIZE(neigh_states));
  646. }
  647. /** @} */
  648. /**
  649. * @name Neighbour Flags Translations
  650. * @{
  651. */
  652. static const struct trans_tbl neigh_flags[] = {
  653. __ADD(NTF_USE, use)
  654. __ADD(NTF_PROXY, proxy)
  655. __ADD(NTF_ROUTER, router)
  656. };
  657. char * rtnl_neigh_flags2str(int flags, char *buf, size_t len)
  658. {
  659. return __flags2str(flags, buf, len, neigh_flags,
  660. ARRAY_SIZE(neigh_flags));
  661. }
  662. int rtnl_neigh_str2flag(const char *name)
  663. {
  664. return __str2type(name, neigh_flags, ARRAY_SIZE(neigh_flags));
  665. }
  666. /** @} */
  667. /**
  668. * @name Attributes
  669. * @{
  670. */
  671. void rtnl_neigh_set_state(struct rtnl_neigh *neigh, int state)
  672. {
  673. neigh->n_state_mask |= state;
  674. neigh->n_state |= state;
  675. neigh->ce_mask |= NEIGH_ATTR_STATE;
  676. }
  677. int rtnl_neigh_get_state(struct rtnl_neigh *neigh)
  678. {
  679. if (neigh->ce_mask & NEIGH_ATTR_STATE)
  680. return neigh->n_state;
  681. else
  682. return -1;
  683. }
  684. void rtnl_neigh_unset_state(struct rtnl_neigh *neigh, int state)
  685. {
  686. neigh->n_state_mask |= state;
  687. neigh->n_state &= ~state;
  688. neigh->ce_mask |= NEIGH_ATTR_STATE;
  689. }
  690. void rtnl_neigh_set_flags(struct rtnl_neigh *neigh, unsigned int flags)
  691. {
  692. neigh->n_flag_mask |= flags;
  693. neigh->n_flags |= flags;
  694. neigh->ce_mask |= NEIGH_ATTR_FLAGS;
  695. }
  696. unsigned int rtnl_neigh_get_flags(struct rtnl_neigh *neigh)
  697. {
  698. return neigh->n_flags;
  699. }
  700. void rtnl_neigh_unset_flags(struct rtnl_neigh *neigh, unsigned int flags)
  701. {
  702. neigh->n_flag_mask |= flags;
  703. neigh->n_flags &= ~flags;
  704. neigh->ce_mask |= NEIGH_ATTR_FLAGS;
  705. }
  706. void rtnl_neigh_set_ifindex(struct rtnl_neigh *neigh, int ifindex)
  707. {
  708. neigh->n_ifindex = ifindex;
  709. neigh->ce_mask |= NEIGH_ATTR_IFINDEX;
  710. }
  711. int rtnl_neigh_get_ifindex(struct rtnl_neigh *neigh)
  712. {
  713. return neigh->n_ifindex;
  714. }
  715. static inline int __assign_addr(struct rtnl_neigh *neigh, struct nl_addr **pos,
  716. struct nl_addr *new, int flag, int nocheck)
  717. {
  718. if (!nocheck) {
  719. if (neigh->ce_mask & NEIGH_ATTR_FAMILY) {
  720. if (new->a_family != neigh->n_family)
  721. return -NLE_AF_MISMATCH;
  722. } else {
  723. neigh->n_family = new->a_family;
  724. neigh->ce_mask |= NEIGH_ATTR_FAMILY;
  725. }
  726. }
  727. if (*pos)
  728. nl_addr_put(*pos);
  729. nl_addr_get(new);
  730. *pos = new;
  731. neigh->ce_mask |= flag;
  732. return 0;
  733. }
  734. void rtnl_neigh_set_lladdr(struct rtnl_neigh *neigh, struct nl_addr *addr)
  735. {
  736. __assign_addr(neigh, &neigh->n_lladdr, addr, NEIGH_ATTR_LLADDR, 1);
  737. }
  738. struct nl_addr *rtnl_neigh_get_lladdr(struct rtnl_neigh *neigh)
  739. {
  740. if (neigh->ce_mask & NEIGH_ATTR_LLADDR)
  741. return neigh->n_lladdr;
  742. else
  743. return NULL;
  744. }
  745. int rtnl_neigh_set_dst(struct rtnl_neigh *neigh, struct nl_addr *addr)
  746. {
  747. return __assign_addr(neigh, &neigh->n_dst, addr,
  748. NEIGH_ATTR_DST, 0);
  749. }
  750. struct nl_addr *rtnl_neigh_get_dst(struct rtnl_neigh *neigh)
  751. {
  752. if (neigh->ce_mask & NEIGH_ATTR_DST)
  753. return neigh->n_dst;
  754. else
  755. return NULL;
  756. }
  757. void rtnl_neigh_set_family(struct rtnl_neigh *neigh, int family)
  758. {
  759. neigh->n_family = family;
  760. neigh->ce_mask |= NEIGH_ATTR_FAMILY;
  761. }
  762. int rtnl_neigh_get_family(struct rtnl_neigh *neigh)
  763. {
  764. return neigh->n_family;
  765. }
  766. void rtnl_neigh_set_type(struct rtnl_neigh *neigh, int type)
  767. {
  768. neigh->n_type = type;
  769. neigh->ce_mask = NEIGH_ATTR_TYPE;
  770. }
  771. int rtnl_neigh_get_type(struct rtnl_neigh *neigh)
  772. {
  773. if (neigh->ce_mask & NEIGH_ATTR_TYPE)
  774. return neigh->n_type;
  775. else
  776. return -1;
  777. }
  778. /** @} */
  779. static struct nl_object_ops neigh_obj_ops = {
  780. .oo_name = "route/neigh",
  781. .oo_size = sizeof(struct rtnl_neigh),
  782. .oo_free_data = neigh_free_data,
  783. .oo_clone = neigh_clone,
  784. .oo_dump = {
  785. [NL_DUMP_LINE] = neigh_dump_line,
  786. [NL_DUMP_DETAILS] = neigh_dump_details,
  787. [NL_DUMP_STATS] = neigh_dump_stats,
  788. },
  789. .oo_compare = neigh_compare,
  790. .oo_keygen = neigh_keygen,
  791. .oo_attrs2str = neigh_attrs2str,
  792. .oo_id_attrs = (NEIGH_ATTR_IFINDEX | NEIGH_ATTR_DST | NEIGH_ATTR_FAMILY),
  793. .oo_id_attrs_get = neigh_id_attrs_get
  794. };
  795. static struct nl_af_group neigh_groups[] = {
  796. { AF_UNSPEC, RTNLGRP_NEIGH },
  797. { AF_BRIDGE, RTNLGRP_NEIGH },
  798. { END_OF_GROUP_LIST },
  799. };
  800. static struct nl_cache_ops rtnl_neigh_ops = {
  801. .co_name = "route/neigh",
  802. .co_hdrsize = sizeof(struct ndmsg),
  803. .co_msgtypes = {
  804. { RTM_NEWNEIGH, NL_ACT_NEW, "new" },
  805. { RTM_DELNEIGH, NL_ACT_DEL, "del" },
  806. { RTM_GETNEIGH, NL_ACT_GET, "get" },
  807. END_OF_MSGTYPES_LIST,
  808. },
  809. .co_protocol = NETLINK_ROUTE,
  810. .co_groups = neigh_groups,
  811. .co_request_update = neigh_request_update,
  812. .co_msg_parser = neigh_msg_parser,
  813. .co_obj_ops = &neigh_obj_ops,
  814. };
  815. static void __init neigh_init(void)
  816. {
  817. nl_cache_mngt_register(&rtnl_neigh_ops);
  818. }
  819. static void __exit neigh_exit(void)
  820. {
  821. nl_cache_mngt_unregister(&rtnl_neigh_ops);
  822. }
  823. /** @} */