neightbl.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. /*
  2. * lib/route/neightbl.c neighbour tables
  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-2006 Thomas Graf <tgraf@suug.ch>
  10. */
  11. /**
  12. * @ingroup rtnl
  13. * @defgroup neightbl Neighbour Tables
  14. * @brief
  15. * @{
  16. */
  17. #include <netlink-local.h>
  18. #include <netlink/netlink.h>
  19. #include <netlink/utils.h>
  20. #include <netlink/route/rtnl.h>
  21. #include <netlink/route/neightbl.h>
  22. #include <netlink/route/link.h>
  23. /** @cond SKIP */
  24. #define NEIGHTBL_ATTR_FAMILY 0x001
  25. #define NEIGHTBL_ATTR_STATS 0x002
  26. #define NEIGHTBL_ATTR_NAME 0x004
  27. #define NEIGHTBL_ATTR_THRESH1 0x008
  28. #define NEIGHTBL_ATTR_THRESH2 0x010
  29. #define NEIGHTBL_ATTR_THRESH3 0x020
  30. #define NEIGHTBL_ATTR_CONFIG 0x040
  31. #define NEIGHTBL_ATTR_PARMS 0x080
  32. #define NEIGHTBL_ATTR_GC_INTERVAL 0x100
  33. #define NEIGHTBLPARM_ATTR_IFINDEX 0x0001
  34. #define NEIGHTBLPARM_ATTR_REFCNT 0x0002
  35. #define NEIGHTBLPARM_ATTR_QUEUE_LEN 0x0004
  36. #define NEIGHTBLPARM_ATTR_APP_PROBES 0x0008
  37. #define NEIGHTBLPARM_ATTR_UCAST_PROBES 0x0010
  38. #define NEIGHTBLPARM_ATTR_MCAST_PROBES 0x0020
  39. #define NEIGHTBLPARM_ATTR_PROXY_QLEN 0x0040
  40. #define NEIGHTBLPARM_ATTR_REACHABLE_TIME 0x0080
  41. #define NEIGHTBLPARM_ATTR_BASE_REACHABLE_TIME 0x0100
  42. #define NEIGHTBLPARM_ATTR_RETRANS_TIME 0x0200
  43. #define NEIGHTBLPARM_ATTR_GC_STALETIME 0x0400
  44. #define NEIGHTBLPARM_ATTR_DELAY_PROBE_TIME 0x0800
  45. #define NEIGHTBLPARM_ATTR_ANYCAST_DELAY 0x1000
  46. #define NEIGHTBLPARM_ATTR_PROXY_DELAY 0x2000
  47. #define NEIGHTBLPARM_ATTR_LOCKTIME 0x4000
  48. static struct nl_cache_ops rtnl_neightbl_ops;
  49. static struct nl_object_ops neightbl_obj_ops;
  50. /** @endcond */
  51. static int neightbl_compare(struct nl_object *_a, struct nl_object *_b,
  52. uint32_t attrs, int flags)
  53. {
  54. struct rtnl_neightbl *a = (struct rtnl_neightbl *) _a;
  55. struct rtnl_neightbl *b = (struct rtnl_neightbl *) _b;
  56. int diff = 0;
  57. #define NT_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, NEIGHTBL_ATTR_##ATTR, a, b, EXPR)
  58. diff |= NT_DIFF(FAMILY, a->nt_family != b->nt_family);
  59. diff |= NT_DIFF(NAME, strcmp(a->nt_name, b->nt_name));
  60. diff |= NT_DIFF(THRESH1, a->nt_gc_thresh1 != b->nt_gc_thresh1);
  61. diff |= NT_DIFF(THRESH2, a->nt_gc_thresh2 != b->nt_gc_thresh2);
  62. diff |= NT_DIFF(THRESH3, a->nt_gc_thresh3 != b->nt_gc_thresh3);
  63. diff |= NT_DIFF(GC_INTERVAL, a->nt_gc_interval != b->nt_gc_interval);
  64. #undef NT_DIFF
  65. if (!(a->ce_mask & NEIGHTBL_ATTR_PARMS) &&
  66. !(b->ce_mask & NEIGHTBL_ATTR_PARMS))
  67. return diff;
  68. /* XXX: FIXME: Compare parameter table */
  69. #if 0
  70. #define REQ(F) (fp->ntp_mask & NEIGHTBLPARM_ATTR_##F)
  71. #define AVAIL(F) (op->ntp_mask & NEIGHTBLPARM_ATTR_##F)
  72. #define _C(F, N) (REQ(F) && (!AVAIL(F) || (op->N != fp->N)))
  73. if (_C(IFINDEX, ntp_ifindex) ||
  74. _C(QUEUE_LEN, ntp_queue_len) ||
  75. _C(APP_PROBES, ntp_app_probes) ||
  76. _C(UCAST_PROBES, ntp_ucast_probes) ||
  77. _C(MCAST_PROBES, ntp_mcast_probes) ||
  78. _C(PROXY_QLEN, ntp_proxy_qlen) ||
  79. _C(LOCKTIME, ntp_locktime) ||
  80. _C(RETRANS_TIME, ntp_retrans_time) ||
  81. _C(BASE_REACHABLE_TIME, ntp_base_reachable_time) ||
  82. _C(GC_STALETIME, ntp_gc_stale_time) ||
  83. _C(DELAY_PROBE_TIME, ntp_probe_delay) ||
  84. _C(ANYCAST_DELAY, ntp_anycast_delay) ||
  85. _C(PROXY_DELAY, ntp_proxy_delay))
  86. return 0;
  87. #undef REQ
  88. #undef AVAIL
  89. #undef _C
  90. #endif
  91. return diff;
  92. }
  93. static struct nla_policy neightbl_policy[NDTA_MAX+1] = {
  94. [NDTA_NAME] = { .type = NLA_STRING,
  95. .maxlen = NTBLNAMSIZ },
  96. [NDTA_THRESH1] = { .type = NLA_U32 },
  97. [NDTA_THRESH2] = { .type = NLA_U32 },
  98. [NDTA_THRESH3] = { .type = NLA_U32 },
  99. [NDTA_GC_INTERVAL] = { .type = NLA_U32 },
  100. [NDTA_CONFIG] = { .minlen = sizeof(struct ndt_config) },
  101. [NDTA_STATS] = { .minlen = sizeof(struct ndt_stats) },
  102. [NDTA_PARMS] = { .type = NLA_NESTED },
  103. };
  104. static int neightbl_msg_parser(struct nl_cache_ops *ops,
  105. struct sockaddr_nl *who, struct nlmsghdr *n,
  106. struct nl_parser_param *pp)
  107. {
  108. struct rtnl_neightbl *ntbl;
  109. struct nlattr *tb[NDTA_MAX + 1];
  110. struct rtgenmsg *rtmsg;
  111. int err;
  112. ntbl = rtnl_neightbl_alloc();
  113. if (!ntbl) {
  114. err = nl_errno(ENOMEM);
  115. goto errout;
  116. }
  117. ntbl->ce_msgtype = n->nlmsg_type;
  118. rtmsg = nlmsg_data(n);
  119. err = nlmsg_parse(n, sizeof(*rtmsg), tb, NDTA_MAX, neightbl_policy);
  120. if (err < 0)
  121. goto errout;
  122. ntbl->nt_family = rtmsg->rtgen_family;
  123. if (tb[NDTA_NAME] == NULL) {
  124. err = nl_error(EINVAL, "NDTA_NAME is missing");
  125. goto errout;
  126. }
  127. nla_strlcpy(ntbl->nt_name, tb[NDTA_NAME], NTBLNAMSIZ);
  128. ntbl->ce_mask |= NEIGHTBL_ATTR_NAME;
  129. if (tb[NDTA_THRESH1]) {
  130. ntbl->nt_gc_thresh1 = nla_get_u32(tb[NDTA_THRESH1]);
  131. ntbl->ce_mask |= NEIGHTBL_ATTR_THRESH1;
  132. }
  133. if (tb[NDTA_THRESH2]) {
  134. ntbl->nt_gc_thresh2 = nla_get_u32(tb[NDTA_THRESH2]);
  135. ntbl->ce_mask |= NEIGHTBL_ATTR_THRESH2;
  136. }
  137. if (tb[NDTA_THRESH3]) {
  138. ntbl->nt_gc_thresh3 = nla_get_u32(tb[NDTA_THRESH3]);
  139. ntbl->ce_mask |= NEIGHTBL_ATTR_THRESH3;
  140. }
  141. if (tb[NDTA_GC_INTERVAL]) {
  142. ntbl->nt_gc_interval = nla_get_u32(tb[NDTA_GC_INTERVAL]);
  143. ntbl->ce_mask |= NEIGHTBL_ATTR_GC_INTERVAL;
  144. }
  145. if (tb[NDTA_CONFIG]) {
  146. nla_memcpy(&ntbl->nt_config, tb[NDTA_CONFIG],
  147. sizeof(ntbl->nt_config));
  148. ntbl->ce_mask |= NEIGHTBL_ATTR_CONFIG;
  149. }
  150. if (tb[NDTA_STATS]) {
  151. nla_memcpy(&ntbl->nt_stats, tb[NDTA_STATS],
  152. sizeof(ntbl->nt_stats));
  153. ntbl->ce_mask |= NEIGHTBL_ATTR_STATS;
  154. }
  155. if (tb[NDTA_PARMS]) {
  156. struct nlattr *tbp[NDTPA_MAX + 1];
  157. struct rtnl_neightbl_parms *p = &ntbl->nt_parms;
  158. err = nla_parse_nested(tbp, NDTPA_MAX, tb[NDTA_PARMS], NULL);
  159. if (err < 0)
  160. goto errout;
  161. #define COPY_ENTRY(name, var) \
  162. if (tbp[NDTPA_ ##name]) { \
  163. p->ntp_ ##var = nla_get_u32(tbp[NDTPA_ ##name]); \
  164. p->ntp_mask |= NEIGHTBLPARM_ATTR_ ##name; \
  165. }
  166. COPY_ENTRY(IFINDEX, ifindex);
  167. COPY_ENTRY(REFCNT, refcnt);
  168. COPY_ENTRY(QUEUE_LEN, queue_len);
  169. COPY_ENTRY(APP_PROBES, app_probes);
  170. COPY_ENTRY(UCAST_PROBES, ucast_probes);
  171. COPY_ENTRY(MCAST_PROBES, mcast_probes);
  172. COPY_ENTRY(PROXY_QLEN, proxy_qlen);
  173. COPY_ENTRY(PROXY_DELAY, proxy_delay);
  174. COPY_ENTRY(ANYCAST_DELAY, anycast_delay);
  175. COPY_ENTRY(LOCKTIME, locktime);
  176. COPY_ENTRY(REACHABLE_TIME, reachable_time);
  177. COPY_ENTRY(BASE_REACHABLE_TIME, base_reachable_time);
  178. COPY_ENTRY(RETRANS_TIME, retrans_time);
  179. COPY_ENTRY(GC_STALETIME, gc_stale_time);
  180. COPY_ENTRY(DELAY_PROBE_TIME, probe_delay);
  181. #undef COPY_ENTRY
  182. ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
  183. }
  184. err = pp->pp_cb((struct nl_object *) ntbl, pp);
  185. if (err < 0)
  186. goto errout;
  187. err = P_ACCEPT;
  188. errout:
  189. rtnl_neightbl_put(ntbl);
  190. return err;
  191. }
  192. static int neightbl_request_update(struct nl_cache *c, struct nl_handle *h)
  193. {
  194. return nl_rtgen_request(h, RTM_GETNEIGHTBL, AF_UNSPEC, NLM_F_DUMP);
  195. }
  196. static int neightbl_dump_brief(struct nl_object *arg, struct nl_dump_params *p)
  197. {
  198. int line = 1;
  199. struct rtnl_neightbl *ntbl = (struct rtnl_neightbl *) arg;
  200. dp_dump(p, "%s", ntbl->nt_name);
  201. if (ntbl->nt_parms.ntp_mask & NEIGHTBLPARM_ATTR_IFINDEX) {
  202. struct nl_cache *link_cache;
  203. link_cache = nl_cache_mngt_require("route/link");
  204. if (link_cache) {
  205. char buf[32];
  206. dp_dump(p, "<%s> ",
  207. rtnl_link_i2name(link_cache,
  208. ntbl->nt_parms.ntp_ifindex,
  209. buf, sizeof(buf)));
  210. } else
  211. dp_dump(p, "<%u> ", ntbl->nt_parms.ntp_ifindex);
  212. } else
  213. dp_dump(p, " ");
  214. if (ntbl->ce_mask & NEIGHTBL_ATTR_CONFIG)
  215. dp_dump(p, "entries %u ", ntbl->nt_config.ndtc_entries);
  216. if (ntbl->ce_mask & NEIGHTBL_ATTR_PARMS) {
  217. char rt[32], rt2[32];
  218. struct rtnl_neightbl_parms *pa = &ntbl->nt_parms;
  219. dp_dump(p, "reachable-time %s retransmit-time %s",
  220. nl_msec2str(pa->ntp_reachable_time, rt, sizeof(rt)),
  221. nl_msec2str(pa->ntp_retrans_time, rt2, sizeof(rt2)));
  222. }
  223. dp_dump(p, "\n");
  224. return line;
  225. }
  226. static int neightbl_dump_full(struct nl_object *arg, struct nl_dump_params *p)
  227. {
  228. char x[32], y[32], z[32];
  229. struct rtnl_neightbl *ntbl = (struct rtnl_neightbl *) arg;
  230. int line = neightbl_dump_brief(arg, p);
  231. if (ntbl->ce_mask & NEIGHTBL_ATTR_CONFIG) {
  232. dp_new_line(p, line++);
  233. dp_dump(p, " key-len %u entry-size %u last-flush %s\n",
  234. ntbl->nt_config.ndtc_key_len,
  235. ntbl->nt_config.ndtc_entry_size,
  236. nl_msec2str(ntbl->nt_config.ndtc_last_flush,
  237. x, sizeof(x)));
  238. dp_new_line(p, line++);
  239. dp_dump(p, " gc threshold %u/%u/%u interval %s " \
  240. "chain-position %u\n",
  241. ntbl->nt_gc_thresh1, ntbl->nt_gc_thresh2,
  242. ntbl->nt_gc_thresh3,
  243. nl_msec2str(ntbl->nt_gc_interval, x, sizeof(x)),
  244. ntbl->nt_config.ndtc_hash_chain_gc);
  245. dp_new_line(p, line++);
  246. dp_dump(p, " hash-rand 0x%08X/0x%08X last-rand %s\n",
  247. ntbl->nt_config.ndtc_hash_rnd,
  248. ntbl->nt_config.ndtc_hash_mask,
  249. nl_msec2str(ntbl->nt_config.ndtc_last_rand,
  250. x, sizeof(x)));
  251. }
  252. if (ntbl->ce_mask & NEIGHTBL_ATTR_PARMS) {
  253. struct rtnl_neightbl_parms *pa = &ntbl->nt_parms;
  254. dp_new_line(p, line++);
  255. dp_dump(p, " refcnt %u pending-queue-limit %u " \
  256. "proxy-delayed-queue-limit %u\n",
  257. pa->ntp_refcnt,
  258. pa->ntp_queue_len,
  259. pa->ntp_proxy_qlen);
  260. dp_new_line(p, line++);
  261. dp_dump(p, " num-userspace-probes %u num-unicast-probes " \
  262. "%u num-multicast-probes %u\n",
  263. pa->ntp_app_probes,
  264. pa->ntp_ucast_probes,
  265. pa->ntp_mcast_probes);
  266. dp_new_line(p, line++);
  267. dp_dump(p, " min-age %s base-reachable-time %s " \
  268. "stale-check-interval %s\n",
  269. nl_msec2str(pa->ntp_locktime, x, sizeof(x)),
  270. nl_msec2str(pa->ntp_base_reachable_time,
  271. y, sizeof(y)),
  272. nl_msec2str(pa->ntp_gc_stale_time, z, sizeof(z)));
  273. dp_new_line(p, line++);
  274. dp_dump(p, " initial-probe-delay %s answer-delay %s " \
  275. "proxy-answer-delay %s\n",
  276. nl_msec2str(pa->ntp_probe_delay, x, sizeof(x)),
  277. nl_msec2str(pa->ntp_anycast_delay, y, sizeof(y)),
  278. nl_msec2str(pa->ntp_proxy_delay, z, sizeof(z)));
  279. }
  280. return line;
  281. }
  282. static int neightbl_dump_stats(struct nl_object *arg, struct nl_dump_params *p)
  283. {
  284. struct rtnl_neightbl *ntbl = (struct rtnl_neightbl *) arg;
  285. int line = neightbl_dump_full(arg, p);
  286. if (!(ntbl->ce_mask & NEIGHTBL_ATTR_STATS))
  287. return line;
  288. dp_new_line(p, line++);
  289. dp_dump(p, " lookups %lld hits %lld failed %lld " \
  290. "allocations %lld destroys %lld\n",
  291. ntbl->nt_stats.ndts_lookups,
  292. ntbl->nt_stats.ndts_hits,
  293. ntbl->nt_stats.ndts_res_failed,
  294. ntbl->nt_stats.ndts_allocs,
  295. ntbl->nt_stats.ndts_destroys);
  296. dp_new_line(p, line++);
  297. dp_dump(p, " hash-grows %lld forced-gc-runs %lld " \
  298. "periodic-gc-runs %lld\n",
  299. ntbl->nt_stats.ndts_hash_grows,
  300. ntbl->nt_stats.ndts_forced_gc_runs,
  301. ntbl->nt_stats.ndts_periodic_gc_runs);
  302. dp_dump(p, " rcv-unicast-probes %lld rcv-multicast-probes %lld\n",
  303. ntbl->nt_stats.ndts_rcv_probes_ucast,
  304. ntbl->nt_stats.ndts_rcv_probes_mcast);
  305. return line;
  306. }
  307. /**
  308. * @name Allocation/Freeing
  309. * @{
  310. */
  311. struct rtnl_neightbl *rtnl_neightbl_alloc(void)
  312. {
  313. return (struct rtnl_neightbl *) nl_object_alloc(&neightbl_obj_ops);
  314. }
  315. void rtnl_neightbl_put(struct rtnl_neightbl *neightbl)
  316. {
  317. nl_object_put((struct nl_object *) neightbl);
  318. }
  319. /** @} */
  320. /**
  321. * @name Neighbour Table Cache Management
  322. * @{
  323. */
  324. /**
  325. * Build a neighbour table cache including all neighbour tables currently configured in the kernel.
  326. * @arg handle netlink handle
  327. *
  328. * Allocates a new neighbour table cache, initializes it properly and
  329. * updates it to include all neighbour tables currently configured in
  330. * the kernel.
  331. *
  332. * @note The caller is responsible for destroying and freeing the
  333. * cache after using it.
  334. * @return The new cache or NULL if an error occured.
  335. */
  336. struct nl_cache * rtnl_neightbl_alloc_cache(struct nl_handle *handle)
  337. {
  338. struct nl_cache * cache;
  339. cache = nl_cache_alloc(&rtnl_neightbl_ops);
  340. if (cache == NULL)
  341. return NULL;
  342. if (handle && nl_cache_refill(handle, cache) < 0) {
  343. nl_cache_free(cache);
  344. return NULL;
  345. }
  346. return cache;
  347. }
  348. /**
  349. * Lookup neighbour table by name and optional interface index
  350. * @arg cache neighbour table cache
  351. * @arg name name of table
  352. * @arg ifindex optional interface index
  353. *
  354. * Looks up the neighbour table matching the specified name and
  355. * optionally the specified ifindex to retrieve device specific
  356. * parameter sets.
  357. *
  358. * @return ptr to neighbour table inside the cache or NULL if no
  359. * match was found.
  360. */
  361. struct rtnl_neightbl *rtnl_neightbl_get(struct nl_cache *cache,
  362. const char *name, int ifindex)
  363. {
  364. struct rtnl_neightbl *nt;
  365. if (cache->c_ops != &rtnl_neightbl_ops)
  366. return NULL;
  367. nl_list_for_each_entry(nt, &cache->c_items, ce_list) {
  368. if (!strcasecmp(nt->nt_name, name) &&
  369. ((!ifindex && !nt->nt_parms.ntp_ifindex) ||
  370. (ifindex && ifindex == nt->nt_parms.ntp_ifindex))) {
  371. nl_object_get((struct nl_object *) nt);
  372. return nt;
  373. }
  374. }
  375. return NULL;
  376. }
  377. /** @} */
  378. /**
  379. * @name Neighbour Table Modifications
  380. * @{
  381. */
  382. /**
  383. * Builds a netlink change request message to change neighbour table attributes
  384. * @arg old neighbour table to change
  385. * @arg tmpl template with requested changes
  386. *
  387. * Builds a new netlink message requesting a change of neighbour table
  388. * attributes. The netlink message header isn't fully equipped with all
  389. * relevant fields and must be sent out via nl_send_auto_complete() or
  390. * supplemented as needed.
  391. * \a old must point to a neighbour table currently configured in the
  392. * kernel and \a tmpl must contain the attributes to be changed set via
  393. * \c rtnl_neightbl_set_* functions.
  394. *
  395. * @return New netlink message
  396. */
  397. struct nl_msg * rtnl_neightbl_build_change_request(struct rtnl_neightbl *old,
  398. struct rtnl_neightbl *tmpl)
  399. {
  400. struct nl_msg *m;
  401. struct ndtmsg ndt = {
  402. .ndtm_family = old->nt_family,
  403. };
  404. m = nlmsg_alloc_simple(RTM_SETNEIGHTBL, 0);
  405. nlmsg_append(m, &ndt, sizeof(ndt), NLMSG_ALIGNTO);
  406. nla_put_string(m, NDTA_NAME, old->nt_name);
  407. if (tmpl->ce_mask & NEIGHTBL_ATTR_THRESH1)
  408. nla_put_u32(m, NDTA_THRESH1, tmpl->nt_gc_thresh1);
  409. if (tmpl->ce_mask & NEIGHTBL_ATTR_THRESH2)
  410. nla_put_u32(m, NDTA_THRESH2, tmpl->nt_gc_thresh2);
  411. if (tmpl->ce_mask & NEIGHTBL_ATTR_THRESH2)
  412. nla_put_u32(m, NDTA_THRESH2, tmpl->nt_gc_thresh2);
  413. if (tmpl->ce_mask & NEIGHTBL_ATTR_GC_INTERVAL)
  414. nla_put_u64(m, NDTA_GC_INTERVAL,
  415. tmpl->nt_gc_interval);
  416. if (tmpl->ce_mask & NEIGHTBL_ATTR_PARMS) {
  417. struct rtnl_neightbl_parms *p = &tmpl->nt_parms;
  418. struct nl_msg *parms = nlmsg_alloc();
  419. if (old->nt_parms.ntp_mask & NEIGHTBLPARM_ATTR_IFINDEX)
  420. nla_put_u32(parms, NDTPA_IFINDEX,
  421. old->nt_parms.ntp_ifindex);
  422. if (p->ntp_mask & NEIGHTBLPARM_ATTR_QUEUE_LEN)
  423. nla_put_u32(parms, NDTPA_QUEUE_LEN, p->ntp_queue_len);
  424. if (p->ntp_mask & NEIGHTBLPARM_ATTR_APP_PROBES)
  425. nla_put_u32(parms, NDTPA_APP_PROBES, p->ntp_app_probes);
  426. if (p->ntp_mask & NEIGHTBLPARM_ATTR_UCAST_PROBES)
  427. nla_put_u32(parms, NDTPA_UCAST_PROBES,
  428. p->ntp_ucast_probes);
  429. if (p->ntp_mask & NEIGHTBLPARM_ATTR_MCAST_PROBES)
  430. nla_put_u32(parms, NDTPA_MCAST_PROBES,
  431. p->ntp_mcast_probes);
  432. if (p->ntp_mask & NEIGHTBLPARM_ATTR_PROXY_QLEN)
  433. nla_put_u32(parms, NDTPA_PROXY_QLEN,
  434. p->ntp_proxy_qlen);
  435. if (p->ntp_mask & NEIGHTBLPARM_ATTR_BASE_REACHABLE_TIME)
  436. nla_put_u64(parms, NDTPA_BASE_REACHABLE_TIME,
  437. p->ntp_base_reachable_time);
  438. if (p->ntp_mask & NEIGHTBLPARM_ATTR_RETRANS_TIME)
  439. nla_put_u64(parms, NDTPA_RETRANS_TIME,
  440. p->ntp_retrans_time);
  441. if (p->ntp_mask & NEIGHTBLPARM_ATTR_GC_STALETIME)
  442. nla_put_u64(parms, NDTPA_GC_STALETIME,
  443. p->ntp_gc_stale_time);
  444. if (p->ntp_mask & NEIGHTBLPARM_ATTR_DELAY_PROBE_TIME)
  445. nla_put_u64(parms, NDTPA_DELAY_PROBE_TIME,
  446. p->ntp_proxy_delay);
  447. if (p->ntp_mask & NEIGHTBLPARM_ATTR_ANYCAST_DELAY)
  448. nla_put_u64(parms, NDTPA_ANYCAST_DELAY,
  449. p->ntp_anycast_delay);
  450. if (p->ntp_mask & NEIGHTBLPARM_ATTR_PROXY_DELAY)
  451. nla_put_u64(parms, NDTPA_PROXY_DELAY,
  452. p->ntp_proxy_delay);
  453. if (p->ntp_mask & NEIGHTBLPARM_ATTR_LOCKTIME)
  454. nla_put_u64(parms, NDTPA_LOCKTIME, p->ntp_locktime);
  455. nla_put_nested(m, NDTA_PARMS, parms);
  456. nlmsg_free(parms);
  457. }
  458. return m;
  459. }
  460. /**
  461. * Change neighbour table attributes
  462. * @arg handle netlink handle
  463. * @arg old neighbour table to be changed
  464. * @arg tmpl template with requested changes
  465. *
  466. * Builds a new netlink message by calling
  467. * rtnl_neightbl_build_change_request(), sends the request to the
  468. * kernel and waits for the next ACK to be received, i.e. blocks
  469. * until the request has been processed.
  470. *
  471. * @return 0 on success or a negative error code
  472. */
  473. int rtnl_neightbl_change(struct nl_handle *handle, struct rtnl_neightbl *old,
  474. struct rtnl_neightbl *tmpl)
  475. {
  476. int err;
  477. struct nl_msg *msg;
  478. msg = rtnl_neightbl_build_change_request(old, tmpl);
  479. err = nl_send_auto_complete(handle, msg);
  480. nlmsg_free(msg);
  481. if (err < 0)
  482. return err;
  483. return nl_wait_for_ack(handle);
  484. }
  485. /** @} */
  486. /**
  487. * @name Attribute Modification
  488. * @{
  489. */
  490. void rtnl_neightbl_set_family(struct rtnl_neightbl *ntbl, int family)
  491. {
  492. ntbl->nt_family = family;
  493. ntbl->ce_mask |= NEIGHTBL_ATTR_FAMILY;
  494. }
  495. void rtnl_neightbl_set_gc_interval(struct rtnl_neightbl *ntbl, uint64_t ms)
  496. {
  497. ntbl->nt_gc_interval = ms;
  498. ntbl->ce_mask |= NEIGHTBL_ATTR_GC_INTERVAL;
  499. }
  500. void rtnl_neightbl_set_gc_tresh1(struct rtnl_neightbl *ntbl, int thresh)
  501. {
  502. ntbl->nt_gc_thresh1 = thresh;
  503. ntbl->ce_mask |= NEIGHTBL_ATTR_THRESH1;
  504. }
  505. void rtnl_neightbl_set_gc_tresh2(struct rtnl_neightbl *ntbl, int thresh)
  506. {
  507. ntbl->nt_gc_thresh2 = thresh;
  508. ntbl->ce_mask |= NEIGHTBL_ATTR_THRESH2;
  509. }
  510. void rtnl_neightbl_set_gc_tresh3(struct rtnl_neightbl *ntbl, int thresh)
  511. {
  512. ntbl->nt_gc_thresh3 = thresh;
  513. ntbl->ce_mask |= NEIGHTBL_ATTR_THRESH3;
  514. }
  515. void rtnl_neightbl_set_name(struct rtnl_neightbl *ntbl, const char *name)
  516. {
  517. strncpy(ntbl->nt_name, name, sizeof(ntbl->nt_name) - 1);
  518. ntbl->ce_mask |= NEIGHTBL_ATTR_NAME;
  519. }
  520. void rtnl_neightbl_set_dev(struct rtnl_neightbl *ntbl, int ifindex)
  521. {
  522. ntbl->nt_parms.ntp_ifindex = ifindex;
  523. ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_IFINDEX;
  524. ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
  525. }
  526. /**
  527. * Set the queue length for pending requests of a neighbour table to the specified value
  528. * @arg ntbl neighbour table to change
  529. * @arg len new queue len
  530. */
  531. void rtnl_neightbl_set_queue_len(struct rtnl_neightbl *ntbl, int len)
  532. {
  533. ntbl->nt_parms.ntp_queue_len = len;
  534. ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_QUEUE_LEN;
  535. ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
  536. }
  537. /**
  538. * Set the queue length for delay proxy arp requests of a neighbour table to the specified value
  539. * @arg ntbl neighbour table to change
  540. * @arg len new queue len
  541. */
  542. void rtnl_neightbl_set_proxy_queue_len(struct rtnl_neightbl *ntbl, int len)
  543. {
  544. ntbl->nt_parms.ntp_proxy_qlen = len;
  545. ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_PROXY_QLEN;
  546. ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
  547. }
  548. /**
  549. * Set the number of application probes of a neighbour table to the specified value
  550. * @arg ntbl neighbour table to change
  551. * @arg probes new probes value
  552. */
  553. void rtnl_neightbl_set_app_probes(struct rtnl_neightbl *ntbl, int probes)
  554. {
  555. ntbl->nt_parms.ntp_app_probes = probes;
  556. ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_APP_PROBES;
  557. ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
  558. }
  559. /**
  560. * Set the number of unicast probes of a neighbour table to the specified value
  561. * @arg ntbl neighbour table to change
  562. * @arg probes new probes value
  563. */
  564. void rtnl_neightbl_set_ucast_probes(struct rtnl_neightbl *ntbl, int probes)
  565. {
  566. ntbl->nt_parms.ntp_ucast_probes = probes;
  567. ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_UCAST_PROBES;
  568. ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
  569. }
  570. /**
  571. * Set the number of multicast probes of a neighbour table to the specified value
  572. * @arg ntbl neighbour table to change
  573. * @arg probes new probes value
  574. */
  575. void rtnl_neightbl_set_mcast_probes(struct rtnl_neightbl *ntbl, int probes)
  576. {
  577. ntbl->nt_parms.ntp_mcast_probes = probes;
  578. ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_MCAST_PROBES;
  579. ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
  580. }
  581. /**
  582. * Set the base reachable time of a neighbour table to the specified value
  583. * @arg ntbl neighbour table to change
  584. * @arg ms new base reachable time in milliseconds
  585. */
  586. void rtnl_neightbl_set_base_reachable_time(struct rtnl_neightbl *ntbl,
  587. uint64_t ms)
  588. {
  589. ntbl->nt_parms.ntp_base_reachable_time = ms;
  590. ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_BASE_REACHABLE_TIME;
  591. ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
  592. }
  593. /**
  594. * Set the retransmit time of a neighbour table to the specified value
  595. * @arg ntbl neighbour table to change
  596. * @arg ms new retransmit time
  597. */
  598. void rtnl_neightbl_set_retrans_time(struct rtnl_neightbl *ntbl, uint64_t ms)
  599. {
  600. ntbl->nt_parms.ntp_retrans_time = ms;
  601. ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_RETRANS_TIME;
  602. ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
  603. }
  604. /**
  605. * Set the gc stale time of a neighbour table to the specified value
  606. * @arg ntbl neighbour table to change
  607. * @arg ms new gc stale time in milliseconds
  608. */
  609. void rtnl_neightbl_set_gc_stale_time(struct rtnl_neightbl *ntbl, uint64_t ms)
  610. {
  611. ntbl->nt_parms.ntp_gc_stale_time = ms;
  612. ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_GC_STALETIME;
  613. ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
  614. }
  615. /**
  616. * Set the first probe delay time of a neighbour table to the specified value
  617. * @arg ntbl neighbour table to change
  618. * @arg ms new first probe delay time in milliseconds
  619. */
  620. void rtnl_neightbl_set_delay_probe_time(struct rtnl_neightbl *ntbl, uint64_t ms)
  621. {
  622. ntbl->nt_parms.ntp_probe_delay = ms;
  623. ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_DELAY_PROBE_TIME;
  624. ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
  625. }
  626. /**
  627. * Set the anycast delay of a neighbour table to the specified value
  628. * @arg ntbl neighbour table to change
  629. * @arg ms new anycast delay in milliseconds
  630. */
  631. void rtnl_neightbl_set_anycast_delay(struct rtnl_neightbl *ntbl, uint64_t ms)
  632. {
  633. ntbl->nt_parms.ntp_anycast_delay = ms;
  634. ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_ANYCAST_DELAY;
  635. ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
  636. }
  637. /**
  638. * Set the proxy delay of a neighbour table to the specified value
  639. * @arg ntbl neighbour table to change
  640. * @arg ms new proxy delay in milliseconds
  641. */
  642. void rtnl_neightbl_set_proxy_delay(struct rtnl_neightbl *ntbl, uint64_t ms)
  643. {
  644. ntbl->nt_parms.ntp_proxy_delay = ms;
  645. ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_PROXY_DELAY;
  646. ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
  647. }
  648. /**
  649. * Set the locktime of a neighbour table to the specified value
  650. * @arg ntbl neighbour table to change
  651. * @arg ms new locktime in milliseconds
  652. */
  653. void rtnl_neightbl_set_locktime(struct rtnl_neightbl *ntbl, uint64_t ms)
  654. {
  655. ntbl->nt_parms.ntp_locktime = ms;
  656. ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_LOCKTIME;
  657. ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
  658. }
  659. /** @} */
  660. static struct nl_object_ops neightbl_obj_ops = {
  661. .oo_name = "route/neightbl",
  662. .oo_size = sizeof(struct rtnl_neightbl),
  663. .oo_dump[NL_DUMP_BRIEF] = neightbl_dump_brief,
  664. .oo_dump[NL_DUMP_FULL] = neightbl_dump_full,
  665. .oo_dump[NL_DUMP_STATS] = neightbl_dump_stats,
  666. .oo_compare = neightbl_compare,
  667. };
  668. static struct nl_cache_ops rtnl_neightbl_ops = {
  669. .co_name = "route/neightbl",
  670. .co_hdrsize = sizeof(struct rtgenmsg),
  671. .co_msgtypes = {
  672. { RTM_NEWNEIGHTBL, NL_ACT_NEW, "new" },
  673. { RTM_SETNEIGHTBL, NL_ACT_SET, "set" },
  674. { RTM_GETNEIGHTBL, NL_ACT_GET, "get" },
  675. END_OF_MSGTYPES_LIST,
  676. },
  677. .co_protocol = NETLINK_ROUTE,
  678. .co_request_update = neightbl_request_update,
  679. .co_msg_parser = neightbl_msg_parser,
  680. .co_obj_ops = &neightbl_obj_ops,
  681. };
  682. static void __init neightbl_init(void)
  683. {
  684. nl_cache_mngt_register(&rtnl_neightbl_ops);
  685. }
  686. static void __exit neightbl_exit(void)
  687. {
  688. nl_cache_mngt_unregister(&rtnl_neightbl_ops);
  689. }
  690. /** @} */