neightbl.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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-2008 Thomas Graf <tgraf@suug.ch>
  10. */
  11. /**
  12. * @ingroup rtnl
  13. * @defgroup neightbl Neighbour Tables
  14. * @brief
  15. * @{
  16. */
  17. #include <netlink-private/netlink.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 = -NLE_NOMEM;
  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 = -NLE_MISSING_ATTR;
  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. errout:
  186. rtnl_neightbl_put(ntbl);
  187. return err;
  188. }
  189. static int neightbl_request_update(struct nl_cache *c, struct nl_sock *h)
  190. {
  191. return nl_rtgen_request(h, RTM_GETNEIGHTBL, AF_UNSPEC, NLM_F_DUMP);
  192. }
  193. static void neightbl_dump_line(struct nl_object *arg, struct nl_dump_params *p)
  194. {
  195. struct rtnl_neightbl *ntbl = (struct rtnl_neightbl *) arg;
  196. nl_dump_line(p, "%s", ntbl->nt_name);
  197. if (ntbl->nt_parms.ntp_mask & NEIGHTBLPARM_ATTR_IFINDEX) {
  198. struct nl_cache *link_cache;
  199. link_cache = nl_cache_mngt_require_safe("route/link");
  200. if (link_cache) {
  201. char buf[32];
  202. nl_dump(p, "<%s> ",
  203. rtnl_link_i2name(link_cache,
  204. ntbl->nt_parms.ntp_ifindex,
  205. buf, sizeof(buf)));
  206. nl_cache_put(link_cache);
  207. } else
  208. nl_dump(p, "<%u> ", ntbl->nt_parms.ntp_ifindex);
  209. } else
  210. nl_dump(p, " ");
  211. if (ntbl->ce_mask & NEIGHTBL_ATTR_CONFIG)
  212. nl_dump(p, "entries %u ", ntbl->nt_config.ndtc_entries);
  213. if (ntbl->ce_mask & NEIGHTBL_ATTR_PARMS) {
  214. char rt[32], rt2[32];
  215. struct rtnl_neightbl_parms *pa = &ntbl->nt_parms;
  216. nl_dump(p, "reachable-time %s retransmit-time %s",
  217. nl_msec2str(pa->ntp_reachable_time, rt, sizeof(rt)),
  218. nl_msec2str(pa->ntp_retrans_time, rt2, sizeof(rt2)));
  219. }
  220. nl_dump(p, "\n");
  221. }
  222. static void neightbl_dump_details(struct nl_object *arg, struct nl_dump_params *p)
  223. {
  224. char x[32], y[32], z[32];
  225. struct rtnl_neightbl *ntbl = (struct rtnl_neightbl *) arg;
  226. neightbl_dump_line(arg, p);
  227. if (ntbl->ce_mask & NEIGHTBL_ATTR_CONFIG) {
  228. nl_dump_line(p, " key-len %u entry-size %u last-flush %s\n",
  229. ntbl->nt_config.ndtc_key_len,
  230. ntbl->nt_config.ndtc_entry_size,
  231. nl_msec2str(ntbl->nt_config.ndtc_last_flush,
  232. x, sizeof(x)));
  233. nl_dump_line(p, " gc threshold %u/%u/%u interval %s " \
  234. "chain-position %u\n",
  235. ntbl->nt_gc_thresh1, ntbl->nt_gc_thresh2,
  236. ntbl->nt_gc_thresh3,
  237. nl_msec2str(ntbl->nt_gc_interval, x, sizeof(x)),
  238. ntbl->nt_config.ndtc_hash_chain_gc);
  239. nl_dump_line(p, " hash-rand 0x%08X/0x%08X last-rand %s\n",
  240. ntbl->nt_config.ndtc_hash_rnd,
  241. ntbl->nt_config.ndtc_hash_mask,
  242. nl_msec2str(ntbl->nt_config.ndtc_last_rand,
  243. x, sizeof(x)));
  244. }
  245. if (ntbl->ce_mask & NEIGHTBL_ATTR_PARMS) {
  246. struct rtnl_neightbl_parms *pa = &ntbl->nt_parms;
  247. nl_dump_line(p, " refcnt %u pending-queue-limit %u " \
  248. "proxy-delayed-queue-limit %u\n",
  249. pa->ntp_refcnt,
  250. pa->ntp_queue_len,
  251. pa->ntp_proxy_qlen);
  252. nl_dump_line(p, " num-userspace-probes %u num-unicast-probes " \
  253. "%u num-multicast-probes %u\n",
  254. pa->ntp_app_probes,
  255. pa->ntp_ucast_probes,
  256. pa->ntp_mcast_probes);
  257. nl_dump_line(p, " min-age %s base-reachable-time %s " \
  258. "stale-check-interval %s\n",
  259. nl_msec2str(pa->ntp_locktime, x, sizeof(x)),
  260. nl_msec2str(pa->ntp_base_reachable_time,
  261. y, sizeof(y)),
  262. nl_msec2str(pa->ntp_gc_stale_time, z, sizeof(z)));
  263. nl_dump_line(p, " initial-probe-delay %s answer-delay %s " \
  264. "proxy-answer-delay %s\n",
  265. nl_msec2str(pa->ntp_probe_delay, x, sizeof(x)),
  266. nl_msec2str(pa->ntp_anycast_delay, y, sizeof(y)),
  267. nl_msec2str(pa->ntp_proxy_delay, z, sizeof(z)));
  268. }
  269. }
  270. static void neightbl_dump_stats(struct nl_object *arg, struct nl_dump_params *p)
  271. {
  272. struct rtnl_neightbl *ntbl = (struct rtnl_neightbl *) arg;
  273. neightbl_dump_details(arg, p);
  274. if (!(ntbl->ce_mask & NEIGHTBL_ATTR_STATS))
  275. return;
  276. nl_dump_line(p, " " \
  277. " lookups %" PRIu64 \
  278. " hits %" PRIu64 \
  279. " failed %" PRIu64 \
  280. " allocations %" PRIu64 \
  281. " destroys %" PRIu64 \
  282. "\n",
  283. ntbl->nt_stats.ndts_lookups,
  284. ntbl->nt_stats.ndts_hits,
  285. ntbl->nt_stats.ndts_res_failed,
  286. ntbl->nt_stats.ndts_allocs,
  287. ntbl->nt_stats.ndts_destroys);
  288. nl_dump_line(p, " " \
  289. " hash-grows %" PRIu64 \
  290. " forced-gc-runs %" PRIu64 \
  291. " periodic-gc-runs %" PRIu64 \
  292. "\n",
  293. ntbl->nt_stats.ndts_hash_grows,
  294. ntbl->nt_stats.ndts_forced_gc_runs,
  295. ntbl->nt_stats.ndts_periodic_gc_runs);
  296. nl_dump_line(p, " " \
  297. " rcv-unicast-probes %" PRIu64 \
  298. " rcv-multicast-probes %" PRIu64 \
  299. "\n",
  300. ntbl->nt_stats.ndts_rcv_probes_ucast,
  301. ntbl->nt_stats.ndts_rcv_probes_mcast);
  302. }
  303. /**
  304. * @name Allocation/Freeing
  305. * @{
  306. */
  307. struct rtnl_neightbl *rtnl_neightbl_alloc(void)
  308. {
  309. return (struct rtnl_neightbl *) nl_object_alloc(&neightbl_obj_ops);
  310. }
  311. void rtnl_neightbl_put(struct rtnl_neightbl *neightbl)
  312. {
  313. nl_object_put((struct nl_object *) neightbl);
  314. }
  315. /** @} */
  316. /**
  317. * @name Neighbour Table Cache Management
  318. * @{
  319. */
  320. /**
  321. * Build a neighbour table cache including all neighbour tables currently configured in the kernel.
  322. * @arg sk Netlink socket.
  323. * @arg result Pointer to store resulting cache.
  324. *
  325. * Allocates a new neighbour table cache, initializes it properly and
  326. * updates it to include all neighbour tables currently configured in
  327. * the kernel.
  328. *
  329. * @return 0 on success or a negative error code.
  330. */
  331. int rtnl_neightbl_alloc_cache(struct nl_sock *sk, struct nl_cache **result)
  332. {
  333. return nl_cache_alloc_and_fill(&rtnl_neightbl_ops, sk, result);
  334. }
  335. /**
  336. * Lookup neighbour table by name and optional interface index
  337. * @arg cache neighbour table cache
  338. * @arg name name of table
  339. * @arg ifindex optional interface index
  340. *
  341. * Looks up the neighbour table matching the specified name and
  342. * optionally the specified ifindex to retrieve device specific
  343. * parameter sets.
  344. *
  345. * @return ptr to neighbour table inside the cache or NULL if no
  346. * match was found.
  347. */
  348. struct rtnl_neightbl *rtnl_neightbl_get(struct nl_cache *cache,
  349. const char *name, int ifindex)
  350. {
  351. struct rtnl_neightbl *nt;
  352. if (cache->c_ops != &rtnl_neightbl_ops)
  353. return NULL;
  354. nl_list_for_each_entry(nt, &cache->c_items, ce_list) {
  355. if (!strcasecmp(nt->nt_name, name) &&
  356. ((!ifindex && !nt->nt_parms.ntp_ifindex) ||
  357. (ifindex && ifindex == nt->nt_parms.ntp_ifindex))) {
  358. nl_object_get((struct nl_object *) nt);
  359. return nt;
  360. }
  361. }
  362. return NULL;
  363. }
  364. /** @} */
  365. /**
  366. * @name Neighbour Table Modifications
  367. * @{
  368. */
  369. /**
  370. * Builds a netlink change request message to change neighbour table attributes
  371. * @arg old neighbour table to change
  372. * @arg tmpl template with requested changes
  373. * @arg result Pointer to store resulting message.
  374. *
  375. * Builds a new netlink message requesting a change of neighbour table
  376. * attributes. The netlink message header isn't fully equipped with all
  377. * relevant fields and must be sent out via nl_send_auto_complete() or
  378. * supplemented as needed.
  379. * \a old must point to a neighbour table currently configured in the
  380. * kernel and \a tmpl must contain the attributes to be changed set via
  381. * \c rtnl_neightbl_set_* functions.
  382. *
  383. * @return 0 on success or a negative error code.
  384. */
  385. int rtnl_neightbl_build_change_request(struct rtnl_neightbl *old,
  386. struct rtnl_neightbl *tmpl,
  387. struct nl_msg **result)
  388. {
  389. struct nl_msg *m, *parms = NULL;
  390. struct ndtmsg ndt = {
  391. .ndtm_family = old->nt_family,
  392. };
  393. m = nlmsg_alloc_simple(RTM_SETNEIGHTBL, 0);
  394. if (!m)
  395. return -NLE_NOMEM;
  396. if (nlmsg_append(m, &ndt, sizeof(ndt), NLMSG_ALIGNTO) < 0)
  397. goto nla_put_failure;
  398. NLA_PUT_STRING(m, NDTA_NAME, old->nt_name);
  399. if (tmpl->ce_mask & NEIGHTBL_ATTR_THRESH1)
  400. NLA_PUT_U32(m, NDTA_THRESH1, tmpl->nt_gc_thresh1);
  401. if (tmpl->ce_mask & NEIGHTBL_ATTR_THRESH2)
  402. NLA_PUT_U32(m, NDTA_THRESH2, tmpl->nt_gc_thresh2);
  403. if (tmpl->ce_mask & NEIGHTBL_ATTR_THRESH2)
  404. NLA_PUT_U32(m, NDTA_THRESH2, tmpl->nt_gc_thresh2);
  405. if (tmpl->ce_mask & NEIGHTBL_ATTR_GC_INTERVAL)
  406. NLA_PUT_U64(m, NDTA_GC_INTERVAL,
  407. tmpl->nt_gc_interval);
  408. if (tmpl->ce_mask & NEIGHTBL_ATTR_PARMS) {
  409. struct rtnl_neightbl_parms *p = &tmpl->nt_parms;
  410. parms = nlmsg_alloc();
  411. if (!parms)
  412. goto nla_put_failure;
  413. if (old->nt_parms.ntp_mask & NEIGHTBLPARM_ATTR_IFINDEX)
  414. NLA_PUT_U32(parms, NDTPA_IFINDEX,
  415. old->nt_parms.ntp_ifindex);
  416. if (p->ntp_mask & NEIGHTBLPARM_ATTR_QUEUE_LEN)
  417. NLA_PUT_U32(parms, NDTPA_QUEUE_LEN, p->ntp_queue_len);
  418. if (p->ntp_mask & NEIGHTBLPARM_ATTR_APP_PROBES)
  419. NLA_PUT_U32(parms, NDTPA_APP_PROBES, p->ntp_app_probes);
  420. if (p->ntp_mask & NEIGHTBLPARM_ATTR_UCAST_PROBES)
  421. NLA_PUT_U32(parms, NDTPA_UCAST_PROBES,
  422. p->ntp_ucast_probes);
  423. if (p->ntp_mask & NEIGHTBLPARM_ATTR_MCAST_PROBES)
  424. NLA_PUT_U32(parms, NDTPA_MCAST_PROBES,
  425. p->ntp_mcast_probes);
  426. if (p->ntp_mask & NEIGHTBLPARM_ATTR_PROXY_QLEN)
  427. NLA_PUT_U32(parms, NDTPA_PROXY_QLEN,
  428. p->ntp_proxy_qlen);
  429. if (p->ntp_mask & NEIGHTBLPARM_ATTR_BASE_REACHABLE_TIME)
  430. NLA_PUT_U64(parms, NDTPA_BASE_REACHABLE_TIME,
  431. p->ntp_base_reachable_time);
  432. if (p->ntp_mask & NEIGHTBLPARM_ATTR_RETRANS_TIME)
  433. NLA_PUT_U64(parms, NDTPA_RETRANS_TIME,
  434. p->ntp_retrans_time);
  435. if (p->ntp_mask & NEIGHTBLPARM_ATTR_GC_STALETIME)
  436. NLA_PUT_U64(parms, NDTPA_GC_STALETIME,
  437. p->ntp_gc_stale_time);
  438. if (p->ntp_mask & NEIGHTBLPARM_ATTR_DELAY_PROBE_TIME)
  439. NLA_PUT_U64(parms, NDTPA_DELAY_PROBE_TIME,
  440. p->ntp_proxy_delay);
  441. if (p->ntp_mask & NEIGHTBLPARM_ATTR_ANYCAST_DELAY)
  442. NLA_PUT_U64(parms, NDTPA_ANYCAST_DELAY,
  443. p->ntp_anycast_delay);
  444. if (p->ntp_mask & NEIGHTBLPARM_ATTR_PROXY_DELAY)
  445. NLA_PUT_U64(parms, NDTPA_PROXY_DELAY,
  446. p->ntp_proxy_delay);
  447. if (p->ntp_mask & NEIGHTBLPARM_ATTR_LOCKTIME)
  448. NLA_PUT_U64(parms, NDTPA_LOCKTIME, p->ntp_locktime);
  449. if (nla_put_nested(m, NDTA_PARMS, parms) < 0)
  450. goto nla_put_failure;
  451. nlmsg_free(parms);
  452. }
  453. *result = m;
  454. return 0;
  455. nla_put_failure:
  456. if (parms)
  457. nlmsg_free(parms);
  458. nlmsg_free(m);
  459. return -NLE_MSGSIZE;
  460. }
  461. /**
  462. * Change neighbour table attributes
  463. * @arg sk Netlink socket.
  464. * @arg old neighbour table to be changed
  465. * @arg tmpl template with requested changes
  466. *
  467. * Builds a new netlink message by calling
  468. * rtnl_neightbl_build_change_request(), sends the request to the
  469. * kernel and waits for the next ACK to be received, i.e. blocks
  470. * until the request has been processed.
  471. *
  472. * @return 0 on success or a negative error code
  473. */
  474. int rtnl_neightbl_change(struct nl_sock *sk, struct rtnl_neightbl *old,
  475. struct rtnl_neightbl *tmpl)
  476. {
  477. struct nl_msg *msg;
  478. int err;
  479. if ((err = rtnl_neightbl_build_change_request(old, tmpl, &msg)) < 0)
  480. return err;
  481. err = nl_send_auto_complete(sk, msg);
  482. nlmsg_free(msg);
  483. if (err < 0)
  484. return err;
  485. return wait_for_ack(sk);
  486. }
  487. /** @} */
  488. /**
  489. * @name Attribute Modification
  490. * @{
  491. */
  492. void rtnl_neightbl_set_family(struct rtnl_neightbl *ntbl, int family)
  493. {
  494. ntbl->nt_family = family;
  495. ntbl->ce_mask |= NEIGHTBL_ATTR_FAMILY;
  496. }
  497. void rtnl_neightbl_set_gc_interval(struct rtnl_neightbl *ntbl, uint64_t ms)
  498. {
  499. ntbl->nt_gc_interval = ms;
  500. ntbl->ce_mask |= NEIGHTBL_ATTR_GC_INTERVAL;
  501. }
  502. void rtnl_neightbl_set_gc_tresh1(struct rtnl_neightbl *ntbl, int thresh)
  503. {
  504. ntbl->nt_gc_thresh1 = thresh;
  505. ntbl->ce_mask |= NEIGHTBL_ATTR_THRESH1;
  506. }
  507. void rtnl_neightbl_set_gc_tresh2(struct rtnl_neightbl *ntbl, int thresh)
  508. {
  509. ntbl->nt_gc_thresh2 = thresh;
  510. ntbl->ce_mask |= NEIGHTBL_ATTR_THRESH2;
  511. }
  512. void rtnl_neightbl_set_gc_tresh3(struct rtnl_neightbl *ntbl, int thresh)
  513. {
  514. ntbl->nt_gc_thresh3 = thresh;
  515. ntbl->ce_mask |= NEIGHTBL_ATTR_THRESH3;
  516. }
  517. void rtnl_neightbl_set_name(struct rtnl_neightbl *ntbl, const char *name)
  518. {
  519. strncpy(ntbl->nt_name, name, sizeof(ntbl->nt_name) - 1);
  520. ntbl->ce_mask |= NEIGHTBL_ATTR_NAME;
  521. }
  522. void rtnl_neightbl_set_dev(struct rtnl_neightbl *ntbl, int ifindex)
  523. {
  524. ntbl->nt_parms.ntp_ifindex = ifindex;
  525. ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_IFINDEX;
  526. ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
  527. }
  528. /**
  529. * Set the queue length for pending requests of a neighbour table to the specified value
  530. * @arg ntbl neighbour table to change
  531. * @arg len new queue len
  532. */
  533. void rtnl_neightbl_set_queue_len(struct rtnl_neightbl *ntbl, int len)
  534. {
  535. ntbl->nt_parms.ntp_queue_len = len;
  536. ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_QUEUE_LEN;
  537. ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
  538. }
  539. /**
  540. * Set the queue length for delay proxy arp requests of a neighbour table to the specified value
  541. * @arg ntbl neighbour table to change
  542. * @arg len new queue len
  543. */
  544. void rtnl_neightbl_set_proxy_queue_len(struct rtnl_neightbl *ntbl, int len)
  545. {
  546. ntbl->nt_parms.ntp_proxy_qlen = len;
  547. ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_PROXY_QLEN;
  548. ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
  549. }
  550. /**
  551. * Set the number of application probes of a neighbour table to the specified value
  552. * @arg ntbl neighbour table to change
  553. * @arg probes new probes value
  554. */
  555. void rtnl_neightbl_set_app_probes(struct rtnl_neightbl *ntbl, int probes)
  556. {
  557. ntbl->nt_parms.ntp_app_probes = probes;
  558. ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_APP_PROBES;
  559. ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
  560. }
  561. /**
  562. * Set the number of unicast probes of a neighbour table to the specified value
  563. * @arg ntbl neighbour table to change
  564. * @arg probes new probes value
  565. */
  566. void rtnl_neightbl_set_ucast_probes(struct rtnl_neightbl *ntbl, int probes)
  567. {
  568. ntbl->nt_parms.ntp_ucast_probes = probes;
  569. ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_UCAST_PROBES;
  570. ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
  571. }
  572. /**
  573. * Set the number of multicast probes of a neighbour table to the specified value
  574. * @arg ntbl neighbour table to change
  575. * @arg probes new probes value
  576. */
  577. void rtnl_neightbl_set_mcast_probes(struct rtnl_neightbl *ntbl, int probes)
  578. {
  579. ntbl->nt_parms.ntp_mcast_probes = probes;
  580. ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_MCAST_PROBES;
  581. ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
  582. }
  583. /**
  584. * Set the base reachable time of a neighbour table to the specified value
  585. * @arg ntbl neighbour table to change
  586. * @arg ms new base reachable time in milliseconds
  587. */
  588. void rtnl_neightbl_set_base_reachable_time(struct rtnl_neightbl *ntbl,
  589. uint64_t ms)
  590. {
  591. ntbl->nt_parms.ntp_base_reachable_time = ms;
  592. ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_BASE_REACHABLE_TIME;
  593. ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
  594. }
  595. /**
  596. * Set the retransmit time of a neighbour table to the specified value
  597. * @arg ntbl neighbour table to change
  598. * @arg ms new retransmit time
  599. */
  600. void rtnl_neightbl_set_retrans_time(struct rtnl_neightbl *ntbl, uint64_t ms)
  601. {
  602. ntbl->nt_parms.ntp_retrans_time = ms;
  603. ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_RETRANS_TIME;
  604. ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
  605. }
  606. /**
  607. * Set the gc stale time of a neighbour table to the specified value
  608. * @arg ntbl neighbour table to change
  609. * @arg ms new gc stale time in milliseconds
  610. */
  611. void rtnl_neightbl_set_gc_stale_time(struct rtnl_neightbl *ntbl, uint64_t ms)
  612. {
  613. ntbl->nt_parms.ntp_gc_stale_time = ms;
  614. ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_GC_STALETIME;
  615. ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
  616. }
  617. /**
  618. * Set the first probe delay time of a neighbour table to the specified value
  619. * @arg ntbl neighbour table to change
  620. * @arg ms new first probe delay time in milliseconds
  621. */
  622. void rtnl_neightbl_set_delay_probe_time(struct rtnl_neightbl *ntbl, uint64_t ms)
  623. {
  624. ntbl->nt_parms.ntp_probe_delay = ms;
  625. ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_DELAY_PROBE_TIME;
  626. ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
  627. }
  628. /**
  629. * Set the anycast delay of a neighbour table to the specified value
  630. * @arg ntbl neighbour table to change
  631. * @arg ms new anycast delay in milliseconds
  632. */
  633. void rtnl_neightbl_set_anycast_delay(struct rtnl_neightbl *ntbl, uint64_t ms)
  634. {
  635. ntbl->nt_parms.ntp_anycast_delay = ms;
  636. ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_ANYCAST_DELAY;
  637. ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
  638. }
  639. /**
  640. * Set the proxy delay of a neighbour table to the specified value
  641. * @arg ntbl neighbour table to change
  642. * @arg ms new proxy delay in milliseconds
  643. */
  644. void rtnl_neightbl_set_proxy_delay(struct rtnl_neightbl *ntbl, uint64_t ms)
  645. {
  646. ntbl->nt_parms.ntp_proxy_delay = ms;
  647. ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_PROXY_DELAY;
  648. ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
  649. }
  650. /**
  651. * Set the locktime of a neighbour table to the specified value
  652. * @arg ntbl neighbour table to change
  653. * @arg ms new locktime in milliseconds
  654. */
  655. void rtnl_neightbl_set_locktime(struct rtnl_neightbl *ntbl, uint64_t ms)
  656. {
  657. ntbl->nt_parms.ntp_locktime = ms;
  658. ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_LOCKTIME;
  659. ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
  660. }
  661. /** @} */
  662. static struct nl_object_ops neightbl_obj_ops = {
  663. .oo_name = "route/neightbl",
  664. .oo_size = sizeof(struct rtnl_neightbl),
  665. .oo_dump = {
  666. [NL_DUMP_LINE] = neightbl_dump_line,
  667. [NL_DUMP_DETAILS] = neightbl_dump_details,
  668. [NL_DUMP_STATS] = neightbl_dump_stats,
  669. },
  670. .oo_compare = neightbl_compare,
  671. };
  672. static struct nl_cache_ops rtnl_neightbl_ops = {
  673. .co_name = "route/neightbl",
  674. .co_hdrsize = sizeof(struct rtgenmsg),
  675. .co_msgtypes = {
  676. { RTM_NEWNEIGHTBL, NL_ACT_NEW, "new" },
  677. { RTM_SETNEIGHTBL, NL_ACT_SET, "set" },
  678. { RTM_GETNEIGHTBL, NL_ACT_GET, "get" },
  679. END_OF_MSGTYPES_LIST,
  680. },
  681. .co_protocol = NETLINK_ROUTE,
  682. .co_request_update = neightbl_request_update,
  683. .co_msg_parser = neightbl_msg_parser,
  684. .co_obj_ops = &neightbl_obj_ops,
  685. };
  686. static void __init neightbl_init(void)
  687. {
  688. nl_cache_mngt_register(&rtnl_neightbl_ops);
  689. }
  690. static void __exit neightbl_exit(void)
  691. {
  692. nl_cache_mngt_unregister(&rtnl_neightbl_ops);
  693. }
  694. /** @} */