neigh.c 27 KB

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