/build/buildd/libnl-1.0~pre6/lib/route/neigh.c

00001 /*
00002  * lib/route/neigh.c    Neighbours
00003  *
00004  *      This library is free software; you can redistribute it and/or
00005  *      modify it under the terms of the GNU Lesser General Public
00006  *      License as published by the Free Software Foundation version 2.1
00007  *      of the License.
00008  *
00009  * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
00010  */
00011 
00012 /**
00013  * @ingroup rtnl
00014  * @defgroup neigh Neighbours
00015  * @brief
00016  *
00017  * The neighbour table establishes bindings between protocol addresses and
00018  * link layer addresses for hosts sharing the same physical link. This
00019  * module allows you to access and manipulate the content of these tables.
00020  *
00021  * @par Neighbour States
00022  * @code
00023  * NUD_INCOMPLETE
00024  * NUD_REACHABLE
00025  * NUD_STALE
00026  * NUD_DELAY
00027  * NUD_PROBE
00028  * NUD_FAILED
00029  * NUD_NOARP
00030  * NUD_PERMANENT
00031  * @endcode
00032  *
00033  * @par Neighbour Flags
00034  * @code
00035  * NTF_PROXY
00036  * NTF_ROUTER
00037  * @endcode
00038  *
00039  * @par Neighbour Identification
00040  * A neighbour is uniquely identified by the attributes listed below, whenever
00041  * you refer to an existing neighbour all of the attributes must be set.
00042  * Neighbours from caches automatically have all required attributes set.
00043  *   - interface index (rtnl_neigh_set_ifindex())
00044  *   - destination address (rtnl_neigh_set_dst())
00045  *
00046  * @par Changeable Attributes
00047  * \anchor neigh_changeable
00048  *  - state (rtnl_neigh_set_state())
00049  *  - link layer address (rtnl_neigh_set_lladdr())
00050  *
00051  * @par Required Caches for Dumping
00052  * In order to dump neighbour attributes you must provide the following
00053  * caches via nl_cache_provide()
00054  *  - link cache holding all links
00055  *
00056  * @par TODO
00057  *   - Document proxy settings
00058  *   - Document states and their influence
00059  *
00060  * @par 1) Retrieving information about configured neighbours
00061  * @code
00062  * // The first step is to retrieve a list of all available neighbour within
00063  * // the kernel and put them into a cache.
00064  * struct nl_cache *cache = rtnl_neigh_alloc_cache(handle);
00065  *
00066  * // Neighbours can then be looked up by the interface and destination
00067  * // address:
00068  * struct rtnl_neigh *neigh = rtnl_neigh_get(cache, ifindex, dst_addr);
00069  * 
00070  * // After successful usage, the object must be given back to the cache
00071  * rtnl_neigh_put(neigh);
00072  * @endcode
00073  *
00074  * @par 2) Adding new neighbours
00075  * @code
00076  * // Allocate an empty neighbour handle to be filled out with the attributes
00077  * // of the new neighbour.
00078  * struct rtnl_neigh *neigh = rtnl_neigh_alloc();
00079  *
00080  * // Fill out the attributes of the new neighbour
00081  * rtnl_neigh_set_ifindex(neigh, ifindex);
00082  * rtnl_neigh_set_dst(neigh, dst_addr);
00083  * rtnl_neigh_set_state(neigh, rtnl_neigh_str2state("permanent"));
00084  *
00085  * // Build the netlink message and send it to the kernel, the operation will
00086  * // block until the operation has been completed. Alternatively the required
00087  * // netlink message can be built using rtnl_neigh_build_add_request()
00088  * // to be sent out using nl_send_auto_complete().
00089  * rtnl_neigh_add(nl_handle, neigh, NLM_F_REPLACE);
00090  *
00091  * // Free the memory
00092  * rtnl_neigh_put(neigh);
00093  * @endcode
00094  *
00095  * @par 3) Deleting an existing neighbour
00096  * @code
00097  * // Allocate an empty neighbour object to be filled out with the attributes
00098  * // matching the neighbour to be deleted. Alternatively a fully equipped
00099  * // neighbour object out of a cache can be used instead.
00100  * struct rtnl_neigh *neigh = rtnl_neigh_alloc();
00101  *
00102  * // Neighbours are uniquely identified by their interface index and
00103  * // destination address, you may fill out other attributes but they
00104  * // will have no influence.
00105  * rtnl_neigh_set_ifindex(neigh, ifindex);
00106  * rtnl_neigh_set_dst(neigh, dst_addr);
00107  *
00108  * // Build the netlink message and send it to the kernel, the operation will
00109  * // block until the operation has been completed. Alternatively the required
00110  * // netlink message can be built using rtnl_neigh_build_delete_request()
00111  * // to be sent out using nl_send_auto_complete().
00112  * rtnl_neigh_delete(handle, neigh, 0);
00113  *
00114  * // Free the memory
00115  * rtnl_neigh_put(neigh);
00116  * @endcode
00117  *
00118  * @par 4) Changing neighbour attributes
00119  * @code
00120  * // Allocate an empty neighbour object to be filled out with the attributes
00121  * // matching the neighbour to be changed and the new parameters. Alternatively
00122  * // a fully equipped modified neighbour object out of a cache can be used.
00123  * struct rtnl_neigh *neigh = rtnl_neigh_alloc();
00124  *
00125  * // Identify the neighbour to be changed by its interface index and
00126  * // destination address
00127  * rtnl_neigh_set_ifindex(neigh, ifindex);
00128  * rtnl_neigh_set_dst(neigh, dst_addr);
00129  *
00130  * // The link layer address may be modified, if so it is wise to change
00131  * // its state to "permanent" in order to avoid having it overwritten.
00132  * rtnl_neigh_set_lladdr(neigh, lladdr);
00133  *
00134  * // Secondly the state can be modified allowing normal neighbours to be
00135  * // converted into permanent entries or to manually confirm a neighbour.
00136  * rtnl_neigh_set_state(neigh, state);
00137  *
00138  * // Build the netlink message and send it to the kernel, the operation will
00139  * // block until the operation has been completed. Alternatively the required
00140  * // netlink message can be built using rtnl_neigh_build_change_request()
00141  * // to be sent out using nl_send_auto_complete().
00142  * rtnl_neigh_change(handle, neigh, 0);
00143  *
00144  * // Free the memory
00145  * rtnl_neigh_put(neigh);
00146  * @endcode
00147  * @{
00148  */
00149 
00150 #include <netlink-local.h>
00151 #include <netlink/netlink.h>
00152 #include <netlink/utils.h>
00153 #include <netlink/route/rtnl.h>
00154 #include <netlink/route/neighbour.h>
00155 #include <netlink/route/link.h>
00156 
00157 /** @cond SKIP */
00158 #define NEIGH_ATTR_FLAGS        0x01
00159 #define NEIGH_ATTR_STATE        0x02
00160 #define NEIGH_ATTR_LLADDR       0x04
00161 #define NEIGH_ATTR_DST          0x08
00162 #define NEIGH_ATTR_CACHEINFO    0x10
00163 #define NEIGH_ATTR_IFINDEX      0x20
00164 #define NEIGH_ATTR_FAMILY       0x40
00165 #define NEIGH_ATTR_TYPE         0x80
00166 #define NEIGH_ATTR_PROBES       0x100
00167 
00168 static struct nl_cache_ops rtnl_neigh_ops;
00169 /** @endcond */
00170 
00171 static void neigh_free_data(struct nl_object *c)
00172 {
00173         struct rtnl_neigh *neigh = nl_object_priv(c);
00174 
00175         if (!neigh)
00176                 return;
00177 
00178         nl_addr_put(neigh->n_lladdr);
00179         nl_addr_put(neigh->n_dst);
00180 }
00181 
00182 static int neigh_filter(struct nl_object *obj, struct nl_object *filter)
00183 {
00184         struct rtnl_neigh *o = (struct rtnl_neigh *) obj;
00185         struct rtnl_neigh *f = (struct rtnl_neigh *) filter;
00186 
00187 #define REQ(F) (f->n_mask & NEIGH_ATTR_##F)
00188 #define AVAIL(F) (o->n_mask & NEIGH_ATTR_##F)
00189 #define _O(F, EXPR) (REQ(F) && (!AVAIL(F) || (EXPR)))
00190 #define _C(F, N) (REQ(F) && (!AVAIL(F) || (o->N != f->N)))
00191         if (_C(IFINDEX, n_ifindex)                                      ||
00192             _C(FAMILY,  n_family)                                       ||
00193             _C(TYPE,    n_type)                                         ||
00194             _O(LLADDR,  nl_addr_cmp(o->n_lladdr, f->n_lladdr))          ||
00195             _O(DST,     nl_addr_cmp(o->n_dst, f->n_dst))                ||
00196             _O(STATE,   f->n_state ^ (o->n_state & f->n_state_mask))    ||
00197             _O(FLAGS,   f->n_flags ^ (o->n_flags & f->n_flag_mask)))
00198                 return 0;
00199 #undef REQ
00200 #undef AVAIL
00201 #undef _O
00202 #undef _C
00203 
00204         return 1;
00205 }
00206 
00207 static struct nla_policy neigh_policy[NDA_MAX+1] = {
00208         [NDA_CACHEINFO] = { .minlen = sizeof(struct nda_cacheinfo) },
00209         [NDA_PROBES]    = { .type = NLA_U32 },
00210 };
00211 
00212 static int neigh_msg_parser(struct sockaddr_nl *who, struct nlmsghdr *n,
00213                             void *arg)
00214 {
00215         struct rtnl_neigh *neigh;
00216         struct nlattr *tb[NDA_MAX + 1];
00217         struct nl_parser_param *pp = arg;
00218         struct ndmsg *nm;
00219         int err;
00220 
00221         neigh = rtnl_neigh_alloc();
00222         if (!neigh) {
00223                 err = nl_errno(ENOMEM);
00224                 goto errout;
00225         }
00226 
00227         neigh->ce_msgtype = n->nlmsg_type;
00228         nm = nlmsg_data(n);
00229 
00230         err = nlmsg_parse(n, sizeof(*nm), tb, NDA_MAX, neigh_policy);
00231         if (err < 0)
00232                 goto errout;
00233 
00234         neigh->n_family  = nm->ndm_family;
00235         neigh->n_ifindex = nm->ndm_ifindex;
00236         neigh->n_state   = nm->ndm_state;
00237         neigh->n_flags   = nm->ndm_flags;
00238         neigh->n_type    = nm->ndm_type;
00239 
00240         neigh->n_mask |= (NEIGH_ATTR_FAMILY | NEIGH_ATTR_IFINDEX |
00241                           NEIGH_ATTR_STATE | NEIGH_ATTR_FLAGS |
00242                           NEIGH_ATTR_TYPE);
00243 
00244         if (tb[NDA_LLADDR]) {
00245                 neigh->n_lladdr = nla_get_addr(tb[NDA_LLADDR], AF_UNSPEC);
00246                 if (!neigh->n_lladdr)
00247                         goto errout;
00248                 nl_addr_set_family(neigh->n_lladdr,
00249                                    nl_addr_guess_family(neigh->n_lladdr));
00250                 neigh->n_mask |= NEIGH_ATTR_LLADDR;
00251         }
00252 
00253         if (tb[NDA_DST]) {
00254                 neigh->n_dst = nla_get_addr(tb[NDA_DST], neigh->n_family);
00255                 if (!neigh->n_dst)
00256                         goto errout;
00257                 neigh->n_mask |= NEIGH_ATTR_DST;
00258         }
00259 
00260         if (tb[NDA_CACHEINFO]) {
00261                 struct nda_cacheinfo *ci = nla_data(tb[NDA_CACHEINFO]);
00262 
00263                 neigh->n_cacheinfo.nci_confirmed = ci->ndm_confirmed;
00264                 neigh->n_cacheinfo.nci_used = ci->ndm_used;
00265                 neigh->n_cacheinfo.nci_updated = ci->ndm_updated;
00266                 neigh->n_cacheinfo.nci_refcnt = ci->ndm_refcnt;
00267                 
00268                 neigh->n_mask |= NEIGH_ATTR_CACHEINFO;
00269         }
00270 
00271         if (tb[NDA_PROBES]) {
00272                 neigh->n_probes = nla_get_u32(tb[NDA_PROBES]);
00273                 neigh->n_mask |= NEIGH_ATTR_PROBES;
00274         }
00275 
00276         err = pp->pp_cb((struct nl_object *) neigh, pp);
00277         if (err < 0)
00278                 goto errout;
00279 
00280         return P_ACCEPT;
00281 
00282 errout:
00283         rtnl_neigh_put(neigh);
00284         return err;
00285 }
00286 
00287 static int neigh_request_update(struct nl_cache *c, struct nl_handle *h)
00288 {
00289         return nl_rtgen_request(h, RTM_GETNEIGH, AF_UNSPEC, NLM_F_DUMP);
00290 }
00291 
00292 
00293 static int neigh_dump_brief(struct nl_object *a, struct nl_dump_params *p)
00294 {
00295         char dst[INET6_ADDRSTRLEN+5], lladdr[INET6_ADDRSTRLEN+5];
00296         struct rtnl_neigh *n = (struct rtnl_neigh *) a;
00297         struct nl_cache *link_cache;
00298         char state[128], flags[64];
00299 
00300         link_cache = nl_cache_mngt_require("route/link");
00301 
00302         dp_dump(p, "%s ", nl_addr2str(n->n_dst, dst, sizeof(dst)));
00303 
00304         if (link_cache)
00305                 dp_dump(p, "dev %s ",
00306                         rtnl_link_i2name(link_cache, n->n_ifindex,
00307                                          state, sizeof(state)));
00308         else
00309                 dp_dump(p, "dev %d ", n->n_ifindex);
00310 
00311         if (n->n_mask & NEIGH_ATTR_LLADDR)
00312                 dp_dump(p, "lladdr %s ",
00313                         nl_addr2str(n->n_lladdr, lladdr, sizeof(lladdr)));
00314 
00315         rtnl_neigh_state2str(n->n_state, state, sizeof(state));
00316         rtnl_neigh_flags2str(n->n_flags, flags, sizeof(flags));
00317 
00318         if (state[0])
00319                 dp_dump(p, "<%s", state);
00320         if (flags[0])
00321                 dp_dump(p, "%s%s", state[0] ? "," : "<", flags);
00322         if (state[0] || flags[0])
00323                 dp_dump(p, ">");
00324         dp_dump(p, "\n");
00325 
00326         return 1;
00327 }
00328 
00329 static int neigh_dump_full(struct nl_object *a, struct nl_dump_params *p)
00330 {
00331         char rtn_type[32];
00332         struct rtnl_neigh *n = (struct rtnl_neigh *) a;
00333         int hz = nl_get_hz();
00334 
00335         int line = neigh_dump_brief(a, p);
00336 
00337         dp_dump_line(p, line++, "    refcnt %u type %s confirmed %u used "
00338                                 "%u updated %u\n",
00339                 n->n_cacheinfo.nci_refcnt,
00340                 nl_rtntype2str(n->n_type, rtn_type, sizeof(rtn_type)),
00341                 n->n_cacheinfo.nci_confirmed/hz,
00342                 n->n_cacheinfo.nci_used/hz, n->n_cacheinfo.nci_updated/hz);
00343 
00344         return line;
00345 }
00346 
00347 static int neigh_dump_stats(struct nl_object *a, struct nl_dump_params *p)
00348 {
00349         return neigh_dump_full(a, p);
00350 }
00351 
00352 static int neigh_dump_xml(struct nl_object *obj, struct nl_dump_params *p)
00353 {
00354         struct rtnl_neigh *neigh = (struct rtnl_neigh *) obj;
00355         char buf[128];
00356         int line = 0;
00357 
00358         dp_dump_line(p, line++, "<neighbour>\n");
00359         dp_dump_line(p, line++, "  <family>%s</family>\n",
00360                      nl_af2str(neigh->n_family, buf, sizeof(buf)));
00361 
00362         if (neigh->n_mask & NEIGH_ATTR_LLADDR)
00363                 dp_dump_line(p, line++, "  <lladdr>%s</lladdr>\n",
00364                              nl_addr2str(neigh->n_lladdr, buf, sizeof(buf)));
00365 
00366         if (neigh->n_mask & NEIGH_ATTR_DST)
00367                 dp_dump_line(p, line++, "  <dst>%s</dst>\n",
00368                              nl_addr2str(neigh->n_dst, buf, sizeof(buf)));
00369 
00370         if (neigh->n_mask & NEIGH_ATTR_IFINDEX) {
00371                 struct nl_cache *link_cache;
00372         
00373                 link_cache = nl_cache_mngt_require("route/link");
00374 
00375                 if (link_cache)
00376                         dp_dump_line(p, line++, "  <device>%s</device>\n",
00377                                      rtnl_link_i2name(link_cache,
00378                                                       neigh->n_ifindex,
00379                                                       buf, sizeof(buf)));
00380                 else
00381                         dp_dump_line(p, line++, "  <device>%u</device>\n",
00382                                      neigh->n_ifindex);
00383         }
00384 
00385         if (neigh->n_mask & NEIGH_ATTR_PROBES)
00386                 dp_dump_line(p, line++, "  <probes>%u</probes>\n",
00387                              neigh->n_probes);
00388 
00389         if (neigh->n_mask & NEIGH_ATTR_TYPE)
00390                 dp_dump_line(p, line++, "  <type>%s</type>\n",
00391                              nl_rtntype2str(neigh->n_type, buf, sizeof(buf)));
00392 
00393         rtnl_neigh_flags2str(neigh->n_flags, buf, sizeof(buf));
00394         if (buf[0])
00395                 dp_dump_line(p, line++, "  <flags>%s</flags>\n", buf);
00396 
00397         rtnl_neigh_state2str(neigh->n_state, buf, sizeof(buf));
00398         if (buf[0])
00399                 dp_dump_line(p, line++, "  <state>%s</state>\n", buf);
00400 
00401         dp_dump_line(p, line++, "</neighbour>\n");
00402 
00403 #if 0
00404         struct rtnl_ncacheinfo n_cacheinfo;
00405 #endif
00406 
00407         return line;
00408 }
00409 
00410 static int neigh_dump_env(struct nl_object *obj, struct nl_dump_params *p)
00411 {
00412         struct rtnl_neigh *neigh = (struct rtnl_neigh *) obj;
00413         char buf[128];
00414         int line = 0;
00415 
00416         dp_dump_line(p, line++, "NEIGH_FAMILY=%s\n",
00417                      nl_af2str(neigh->n_family, buf, sizeof(buf)));
00418 
00419         if (neigh->n_mask & NEIGH_ATTR_LLADDR)
00420                 dp_dump_line(p, line++, "NEIGHT_LLADDR=%s\n",
00421                              nl_addr2str(neigh->n_lladdr, buf, sizeof(buf)));
00422 
00423         if (neigh->n_mask & NEIGH_ATTR_DST)
00424                 dp_dump_line(p, line++, "NEIGH_DST=%s\n",
00425                              nl_addr2str(neigh->n_dst, buf, sizeof(buf)));
00426 
00427         if (neigh->n_mask & NEIGH_ATTR_IFINDEX) {
00428                 struct nl_cache *link_cache;
00429 
00430                 dp_dump_line(p, line++, "NEIGH_IFINDEX=%u\n",
00431                              neigh->n_ifindex);
00432 
00433                 link_cache = nl_cache_mngt_require("route/link");
00434                 if (link_cache)
00435                         dp_dump_line(p, line++, "NEIGH_IFNAME=%s\n",
00436                                      rtnl_link_i2name(link_cache,
00437                                                       neigh->n_ifindex,
00438                                                       buf, sizeof(buf)));
00439         }
00440 
00441         if (neigh->n_mask & NEIGH_ATTR_PROBES)
00442                 dp_dump_line(p, line++, "NEIGH_PROBES=%u\n",
00443                              neigh->n_probes);
00444 
00445         if (neigh->n_mask & NEIGH_ATTR_TYPE)
00446                 dp_dump_line(p, line++, "NEIGH_TYPE=%s\n",
00447                              nl_rtntype2str(neigh->n_type, buf, sizeof(buf)));
00448 
00449         rtnl_neigh_flags2str(neigh->n_flags, buf, sizeof(buf));
00450         if (buf[0])
00451                 dp_dump_line(p, line++, "NEIGH_FLAGS=%s\n", buf);
00452 
00453         rtnl_neigh_state2str(neigh->n_state, buf, sizeof(buf));
00454         if (buf[0])
00455                 dp_dump_line(p, line++, "NEIGH_STATE=%s\n", buf);
00456 
00457         return line;
00458 }
00459 
00460 /**
00461  * @name Neighbour Object Allocation/Freeage
00462  * @{
00463  */
00464 
00465 /**
00466  * Allocate a new neighbour object
00467  * @return New neighbour object
00468  */
00469 struct rtnl_neigh *rtnl_neigh_alloc(void)
00470 {
00471         return (struct rtnl_neigh *) nl_object_alloc_from_ops(&rtnl_neigh_ops);
00472 }
00473 
00474 /**
00475  * Give back reference on neighbour object.
00476  * @arg neigh           Neighbour  object to be given back.
00477  *
00478  * Decrements the reference counter and frees the object if the
00479  * last reference has been released.
00480  */
00481 void rtnl_neigh_put(struct rtnl_neigh *neigh)
00482 {
00483         nl_object_put((struct nl_object *) neigh);
00484 }
00485 /**
00486  * Free neighbour object.
00487  * @arg neigh           Neighbour object to be freed.
00488  *
00489  * @note Always use rtnl_neigh_put() unless you're absolutely sure
00490  *       that no other user may have a reference on this object.
00491  */
00492 void rtnl_neigh_free(struct rtnl_neigh *neigh)
00493 {
00494         nl_object_free((struct nl_object *) neigh);
00495 }
00496 
00497 /** @} */
00498 
00499 /**
00500  * @name Neighbour Cache Managament
00501  * @{
00502  */
00503 
00504 /**
00505  * Build a neighbour cache including all neighbours currently configured in the kernel.
00506  * @arg handle          netlink handle
00507  *
00508  * Allocates a new neighbour cache, initializes it properly and updates it
00509  * to include all neighbours currently configured in the kernel.
00510  *
00511  * @note The caller is responsible for destroying and freeing the
00512  *       cache after using it.
00513  * @return The new cache or NULL if an error occured.
00514  */
00515 struct nl_cache *rtnl_neigh_alloc_cache(struct nl_handle *handle)
00516 {
00517         struct nl_cache *cache = nl_cache_alloc_from_ops(&rtnl_neigh_ops);
00518 
00519         if (cache == NULL)
00520                 return NULL;
00521 
00522         if (nl_cache_update(handle, cache) < 0) {
00523                 nl_cache_free(cache);
00524                 return NULL;
00525         }
00526 
00527         NL_DBG(2, "Returning new cache %p\n", cache);
00528 
00529         return cache;
00530 }
00531 
00532 /**
00533  * Look up a neighbour by interface index and destination address
00534  * @arg cache           neighbour cache
00535  * @arg ifindex         interface index the neighbour is on
00536  * @arg dst             destination address of the neighbour
00537  * @return neighbour handle or NULL if no match was found.
00538  */
00539 struct rtnl_neigh * rtnl_neigh_get(struct nl_cache *cache, int ifindex,
00540                                    struct nl_addr *dst)
00541 {
00542         struct rtnl_neigh *neigh;
00543 
00544         nl_list_for_each_entry(neigh, &cache->c_items, ce_list) {
00545                 if (neigh->n_ifindex == ifindex &&
00546                     !nl_addr_cmp(neigh->n_dst, dst)) {
00547                         nl_object_get((struct nl_object *) neigh);
00548                         return neigh;
00549                 }
00550         }
00551 
00552         return NULL;
00553 }
00554 
00555 /** @} */
00556 
00557 /**
00558  * @name Neighbour Addition
00559  * @{
00560  */
00561 
00562 static struct nl_msg * build_neigh_msg(struct rtnl_neigh *tmpl, int cmd,
00563                                        int flags)
00564 {
00565         struct nl_msg *msg;
00566         struct ndmsg nhdr = {
00567                 .ndm_ifindex = tmpl->n_ifindex,
00568                 .ndm_family = nl_addr_get_family(tmpl->n_dst),
00569                 .ndm_state = NUD_PERMANENT,
00570         };
00571 
00572         if (tmpl->n_mask & NEIGH_ATTR_STATE)
00573                 nhdr.ndm_state = tmpl->n_state;
00574 
00575         msg = nlmsg_build_simple(cmd, flags);
00576         if (!msg)
00577                 return NULL;
00578 
00579         if (nlmsg_append(msg, &nhdr, sizeof(nhdr), 1) < 0)
00580                 goto nla_put_failure;
00581 
00582         NLA_PUT_ADDR(msg, NDA_DST, tmpl->n_dst);
00583 
00584         if (tmpl->n_mask & NEIGH_ATTR_LLADDR)
00585                 NLA_PUT_ADDR(msg, NDA_LLADDR, tmpl->n_lladdr);
00586 
00587         return msg;
00588 
00589 nla_put_failure:
00590         nlmsg_free(msg);
00591         return NULL;
00592 }
00593 
00594 /**
00595  * Build netlink request message to add a new neighbour
00596  * @arg tmpl            template with data of new neighbour
00597  * @arg flags           additional netlink message flags
00598  *
00599  * Builds a new netlink message requesting a addition of a new
00600  * neighbour. The netlink message header isn't fully equipped with
00601  * all relevant fields and must thus be sent out via nl_send_auto_complete()
00602  * or supplemented as needed. \a tmpl must contain the attributes of the new
00603  * neighbour set via \c rtnl_neigh_set_* functions.
00604  * 
00605  * The following attributes must be set in the template:
00606  *  - Interface index (rtnl_neigh_set_ifindex())
00607  *  - State (rtnl_neigh_set_state())
00608  *  - Destination address (rtnl_neigh_set_dst())
00609  *  - Link layer address (rtnl_neigh_set_lladdr())
00610  *
00611  * @return The netlink message
00612  */
00613 struct nl_msg * rtnl_neigh_build_add_request(struct rtnl_neigh *tmpl, int flags)
00614 {
00615         return build_neigh_msg(tmpl, RTM_NEWNEIGH, NLM_F_CREATE | flags);
00616 }
00617 
00618 /**
00619  * Add a new neighbour
00620  * @arg handle          netlink handle
00621  * @arg tmpl            template with requested changes
00622  * @arg flags           additional netlink message flags
00623  *
00624  * Builds a netlink message by calling rtnl_neigh_build_add_request(),
00625  * sends the request to the kernel and waits for the next ACK to be
00626  * received and thus blocks until the request has been fullfilled.
00627  *
00628  * The following attributes must be set in the template:
00629  *  - Interface index (rtnl_neigh_set_ifindex())
00630  *  - State (rtnl_neigh_set_state())
00631  *  - Destination address (rtnl_neigh_set_dst())
00632  *  - Link layer address (rtnl_neigh_set_lladdr())
00633  *
00634  * @return 0 on sucess or a negative error if an error occured.
00635  */
00636 int rtnl_neigh_add(struct nl_handle *handle, struct rtnl_neigh *tmpl, int flags)
00637 {
00638         int err;
00639         struct nl_msg *msg;
00640         
00641         msg = rtnl_neigh_build_add_request(tmpl, flags);
00642         if (!msg)
00643                 return nl_errno(ENOMEM);
00644 
00645         err = nl_send_auto_complete(handle, msg);
00646         if (err < 0)
00647                 return err;
00648 
00649         nlmsg_free(msg);
00650         return nl_wait_for_ack(handle);
00651 }
00652 
00653 /** @} */
00654 
00655 /**
00656  * @name Neighbour Deletion
00657  * @{
00658  */
00659 
00660 /**
00661  * Build a netlink request message to delete a neighbour
00662  * @arg neigh           neighbour to delete
00663  * @arg flags           additional netlink message flags
00664  *
00665  * Builds a new netlink message requesting a deletion of a neighbour.
00666  * The netlink message header isn't fully equipped with all relevant
00667  * fields and must thus be sent out via nl_send_auto_complete()
00668  * or supplemented as needed. \a neigh must point to an existing
00669  * neighbour.
00670  *
00671  * @return The netlink message
00672  */
00673 struct nl_msg *rtnl_neigh_build_delete_request(struct rtnl_neigh *neigh,
00674                                                int flags)
00675 {
00676         return build_neigh_msg(neigh, RTM_DELNEIGH, flags);
00677 }
00678 
00679 /**
00680  * Delete a neighbour
00681  * @arg handle          netlink handle
00682  * @arg neigh           neighbour to delete
00683  * @arg flags           additional netlink message flags
00684  *
00685  * Builds a netlink message by calling rtnl_neigh_build_delete_request(),
00686  * sends the request to the kernel and waits for the next ACK to be
00687  * received and thus blocks until the request has been fullfilled.
00688  *
00689  * @return 0 on sucess or a negative error if an error occured.
00690  */
00691 int rtnl_neigh_delete(struct nl_handle *handle, struct rtnl_neigh *neigh,
00692                       int flags)
00693 {
00694         int err;
00695         struct nl_msg *msg;
00696         
00697         msg = rtnl_neigh_build_delete_request(neigh, flags);
00698         if (!msg)
00699                 return nl_errno(ENOMEM);
00700 
00701         err = nl_send_auto_complete(handle, msg);
00702         if (err < 0)
00703                 return err;
00704 
00705         nlmsg_free(msg);
00706         return nl_wait_for_ack(handle);
00707 }
00708 
00709 /** @} */
00710 
00711 /**
00712  * @name Neighbour Modification
00713  * @{
00714  */
00715 
00716 /**
00717  * Build a netlink request message to change neighbour attributes
00718  * @arg neigh           the neighbour to change
00719  * @arg flags           additional netlink message flags
00720  *
00721  * Builds a new netlink message requesting a change of a neigh
00722  * attributes. The netlink message header isn't fully equipped with
00723  * all relevant fields and must thus be sent out via nl_send_auto_complete()
00724  * or supplemented as needed.
00725  *
00726  * @return The netlink message
00727  * @note Not all attributes can be changed, see
00728  *       \ref neigh_changeable "Changeable Attributes" for a list.
00729  */
00730 struct nl_msg *rtnl_neigh_build_change_request(struct rtnl_neigh *neigh,
00731                                                int flags)
00732 {
00733         return build_neigh_msg(neigh, RTM_NEWNEIGH, NLM_F_REPLACE | flags);
00734 }
00735 
00736 /**
00737  * Change neighbour attributes
00738  * @arg handle          netlink handle
00739  * @arg neigh           neighbour to be changed
00740  * @arg flags           additional netlink message flags
00741  *
00742  * Builds a netlink message by calling rtnl_neigh_build_change_request(),
00743  * sends the request to the kernel and waits for the next ACK to be
00744  * received and thus blocks until the request has been fullfilled.
00745  *
00746  * @return 0 on sucess or a negative error if an error occured.
00747  * @note Not all attributes can be changed, see
00748  *       \ref neigh_changeable "Changeable Attributes" for a list.
00749  */
00750 int rtnl_neigh_change(struct nl_handle *handle, struct rtnl_neigh *neigh,
00751                       int flags)
00752 {
00753         int err;
00754         struct nl_msg *msg;
00755         
00756         msg = rtnl_neigh_build_change_request(neigh, flags);
00757         if (!msg)
00758                 return nl_errno(ENOMEM);
00759 
00760         err = nl_send_auto_complete(handle, msg);
00761         if (err < 0)
00762                 return err;
00763 
00764         nlmsg_free(msg);
00765         return nl_wait_for_ack(handle);
00766 }
00767 
00768 /** @} */
00769 
00770 /**
00771  * @name Neighbour States Translations
00772  * @{
00773  */
00774 
00775 static struct trans_tbl neigh_states[] = {
00776         __ADD(NUD_INCOMPLETE, incomplete)
00777         __ADD(NUD_REACHABLE, reachable)
00778         __ADD(NUD_STALE, stale)
00779         __ADD(NUD_DELAY, delay)
00780         __ADD(NUD_PROBE, probe)
00781         __ADD(NUD_FAILED, failed)
00782         __ADD(NUD_NOARP, norarp)
00783         __ADD(NUD_PERMANENT, permanent)
00784 };
00785 
00786 /**
00787  * Convert neighbour states to a character string (Reentrant).
00788  * @arg state           neighbour state
00789  * @arg buf             destination buffer
00790  * @arg len             buffer length
00791  *
00792  * Converts a neighbour state to a character string separated by
00793  * commands and stores it in the specified destination buffer.
00794  *
00795  * @return The destination buffer
00796  */
00797 char * rtnl_neigh_state2str(int state, char *buf, size_t len)
00798 {
00799         return __flags2str(state, buf, len, neigh_states,
00800             ARRAY_SIZE(neigh_states));
00801 }
00802 
00803 /**
00804  * Convert a character string to a neighbour state
00805  * @arg name            Name of cscope
00806  *
00807  * Converts the provided character string specifying a neighbour
00808  * state the corresponding numeric value.
00809  *
00810  * @return Neighbour state or a negative value if none was found.
00811  */
00812 int rtnl_neigh_str2state(const char *name)
00813 {
00814         return __str2type(name, neigh_states, ARRAY_SIZE(neigh_states));
00815 }
00816 
00817 /** @} */
00818 
00819 /**
00820  * @name Neighbour Flags Translations
00821  * @{
00822  */
00823 
00824 static struct trans_tbl neigh_flags[] = {
00825         __ADD(NTF_PROXY, proxy)
00826         __ADD(NTF_ROUTER, router)
00827 };
00828 
00829 /**
00830  * Convert neighbour flags to a character string (Reentrant).
00831  * @arg flags           neighbour flags
00832  * @arg buf             destination buffer
00833  * @arg len             buffer length
00834  *
00835  * Converts neighbour flags to a character string separated by
00836  * commands and stores it in the specified destination buffer.
00837  *
00838  * @return The destination buffer or a empty string if no flags are set.
00839  */
00840 char * rtnl_neigh_flags2str(int flags, char *buf, size_t len)
00841 {
00842         return __flags2str(flags, buf, len, neigh_flags,
00843             ARRAY_SIZE(neigh_flags));
00844 }
00845 
00846 /**
00847  * Convert a character string to a neighbour flag
00848  * @arg name            name of the flag
00849  *
00850  * Converts the provided character string specifying a neighbour
00851  * flag the corresponding numeric value.
00852  *
00853  * @return Neighbour flag or a negative value if none was found.
00854  */
00855 int rtnl_neigh_str2flag(const char *name)
00856 {
00857         return __str2type(name, neigh_flags, ARRAY_SIZE(neigh_flags));
00858 }
00859 
00860 /** @} */
00861 
00862 /**
00863  * @name Attribute Modification
00864  * @{
00865  */
00866 
00867 /**
00868  * Set a neighbour state
00869  * @arg neigh           neighbour to change
00870  * @arg state           state to set
00871  */
00872 void rtnl_neigh_set_state(struct rtnl_neigh *neigh, int state)
00873 {
00874         neigh->n_state_mask |= state;
00875         neigh->n_state |= state;
00876         neigh->n_mask |= NEIGH_ATTR_STATE;
00877 }
00878 
00879 /**
00880  * Get neighbour states
00881  * @arg neigh           neighbour handle
00882  * @return Neighbour state or -1 if not set
00883  */
00884 int rtnl_neigh_get_state(struct rtnl_neigh *neigh)
00885 {
00886         if (neigh->n_mask & NEIGH_ATTR_STATE)
00887                 return neigh->n_state;
00888         else
00889                 return -1;
00890 }
00891 
00892 /**
00893  * Unset a neigbour state
00894  * @arg neigh           neighbour to change
00895  * @arg state           state to unset
00896  */
00897 void rtnl_neigh_unset_state(struct rtnl_neigh *neigh, int state)
00898 {
00899         neigh->n_state_mask |= state;
00900         neigh->n_state &= ~state;
00901         neigh->n_mask |= NEIGH_ATTR_STATE;
00902 }
00903 
00904 /**
00905  * Set neighbour flags
00906  * @arg neigh           neighbour to change
00907  * @arg flags           flag to set
00908  */
00909 void rtnl_neigh_set_flags(struct rtnl_neigh *neigh, unsigned int flags)
00910 {
00911         neigh->n_flag_mask |= flags;
00912         neigh->n_flags |= flags;
00913         neigh->n_mask |= NEIGH_ATTR_FLAGS;
00914 }
00915 
00916 /**
00917  * Get neighbour flags
00918  * @arg neigh           neighbour handle
00919  * @return Neighbour flags
00920  */
00921 unsigned int rtnl_neigh_get_flags(struct rtnl_neigh *neigh)
00922 {
00923         return neigh->n_flags;
00924 }
00925 
00926 /**
00927  * Unset neighbour flags
00928  * @arg neigh           neighbour to change
00929  * @arg flags           flag to unset
00930  */
00931 void rtnl_neigh_unset_flags(struct rtnl_neigh *neigh, unsigned int flags)
00932 {
00933         neigh->n_flag_mask |= flags;
00934         neigh->n_flags &= ~flags;
00935         neigh->n_mask |= NEIGH_ATTR_FLAGS;
00936 }
00937 
00938 /**
00939  * Set the interface index of device this neighbour is on
00940  * @arg neigh           neighbour to change
00941  * @arg ifindex         new interface index
00942  */
00943 void rtnl_neigh_set_ifindex(struct rtnl_neigh *neigh, int ifindex)
00944 {
00945         neigh->n_ifindex = ifindex;
00946         neigh->n_mask |= NEIGH_ATTR_IFINDEX;
00947 }
00948 
00949 /**
00950  * Get the interface index of the device this neighbour is on
00951  * @arg neigh           neighbour handle
00952  * @return Interface index or RTNL_LINK_NOT_FOUND if not set
00953  */
00954 int rtnl_neigh_get_ifindex(struct rtnl_neigh *neigh)
00955 {
00956         if (neigh->n_mask & NEIGH_ATTR_IFINDEX)
00957                 return neigh->n_ifindex;
00958         else
00959                 return RTNL_LINK_NOT_FOUND;
00960 }
00961 
00962 static inline int __assign_addr(struct rtnl_neigh *neigh, struct nl_addr **pos,
00963                                 struct nl_addr *new, int flag, int nocheck)
00964 {
00965         if (!nocheck) {
00966                 if (neigh->n_mask & NEIGH_ATTR_FAMILY) {
00967                         if (new->a_family != neigh->n_family)
00968                                 return nl_error(EINVAL,
00969                                                 "Address family mismatch");
00970                 } else {
00971                         neigh->n_family = new->a_family;
00972                         neigh->n_mask |= NEIGH_ATTR_FAMILY;
00973                 }
00974         }
00975 
00976         if (*pos)
00977                 nl_addr_put(*pos);
00978 
00979         nl_addr_get(new);
00980         *pos = new;
00981 
00982         neigh->n_mask |= flag;
00983 
00984         return 0;
00985 }
00986 
00987 /**
00988  * Set link layer address of a neighbour
00989  * @arg neigh           neighbour to change
00990  * @arg addr            new link layer address
00991  *
00992  * Assigns the new link layer address to the specified neighbour handle.
00993  *
00994  * @note The prefix length of the address will be ignored.
00995  */
00996 void rtnl_neigh_set_lladdr(struct rtnl_neigh *neigh, struct nl_addr *addr)
00997 {
00998         __assign_addr(neigh, &neigh->n_lladdr, addr, NEIGH_ATTR_LLADDR, 1);
00999 }
01000 
01001 /**
01002  * Get link layer address of a neighbour
01003  * @arg neigh           neighbour handle
01004  * @return Link layer address or NULL if not set
01005  */
01006 struct nl_addr *rtnl_neigh_get_lladdr(struct rtnl_neigh *neigh)
01007 {
01008         if (neigh->n_mask & NEIGH_ATTR_LLADDR)
01009                 return neigh->n_lladdr;
01010         else
01011                 return NULL;
01012 }
01013 
01014 /**
01015  * Set destination address of a neighbour
01016  * @arg neigh           neighbour to change
01017  * @arg addr            new destination address
01018  *
01019  * Assigns the new destination address to the specified neighbour handle.
01020  * The address is validated against the address family if set already via
01021  * rtnl_neigh_set_family(). The assignment fails if the address families
01022  * mismatch. In case the address family has not been specified yet, the
01023  * address family of this new address is elected to be the requirement.
01024  * 
01025  * @return 0 on success or a negative error code.
01026  */
01027 int rtnl_neigh_set_dst(struct rtnl_neigh *neigh, struct nl_addr *addr)
01028 {
01029         return __assign_addr(neigh, &neigh->n_dst, addr,
01030                              NEIGH_ATTR_DST, 0);
01031 }
01032 
01033 /**
01034  * Get the destination address of a neighbour
01035  * @arg neigh           neighbour handle
01036  * @return Destination address or NULL if not set
01037  */
01038 struct nl_addr *rtnl_neigh_get_dst(struct rtnl_neigh *neigh)
01039 {
01040         if (neigh->n_mask & NEIGH_ATTR_DST)
01041                 return neigh->n_dst;
01042         else
01043                 return NULL;
01044 }
01045 
01046 /**
01047  * Set destination address family
01048  * @arg neigh           neighbour to change
01049  * @arg family          new destination address family
01050  */
01051 void rtnl_neigh_set_family(struct rtnl_neigh *neigh, int family)
01052 {
01053         neigh->n_family = family;
01054         neigh->n_mask |= NEIGH_ATTR_FAMILY;
01055 }
01056 
01057 /**
01058  * Set RTN type of a neighbour
01059  * @arg neigh           neighbour to change
01060  * @arg type            new rtn type
01061  */
01062 void rtnl_neigh_set_type(struct rtnl_neigh *neigh, int type)
01063 {
01064         neigh->n_type = type;
01065         neigh->n_mask = NEIGH_ATTR_TYPE;
01066 }
01067 
01068 /**
01069  * Get RTN type of a neighbour
01070  * @arg neigh           neighbour handle
01071  * @return Type or -1 if not set
01072  */
01073 int rtnl_neigh_get_type(struct rtnl_neigh *neigh)
01074 {
01075         if (neigh->n_mask & NEIGH_ATTR_TYPE)
01076                 return neigh->n_type;
01077         else
01078                 return -1;
01079 }
01080 
01081 /** @} */
01082 
01083 static struct nl_cache_ops rtnl_neigh_ops = {
01084         .co_name                = "route/neigh",
01085         .co_size                = sizeof(struct rtnl_neigh),
01086         .co_hdrsize             = sizeof(struct ndmsg),
01087         .co_msgtypes            = {
01088                                         { RTM_NEWNEIGH, "new" },
01089                                         { RTM_DELNEIGH, "delete" },
01090                                         { RTM_GETNEIGH, "get" },
01091                                         { -1, NULL },
01092                                   },
01093         .co_protocol            = NETLINK_ROUTE,
01094         .co_request_update      = neigh_request_update,
01095         .co_msg_parser          = neigh_msg_parser,
01096         .co_free_data           = neigh_free_data,
01097         .co_dump[NL_DUMP_BRIEF] = neigh_dump_brief,
01098         .co_dump[NL_DUMP_FULL]  = neigh_dump_full,
01099         .co_dump[NL_DUMP_STATS] = neigh_dump_stats,
01100         .co_dump[NL_DUMP_XML]   = neigh_dump_xml,
01101         .co_dump[NL_DUMP_ENV]   = neigh_dump_env,
01102         .co_filter              = neigh_filter,
01103 };
01104 
01105 static void __init neigh_init(void)
01106 {
01107         nl_cache_mngt_register(&rtnl_neigh_ops);
01108 }
01109 
01110 static void __exit neigh_exit(void)
01111 {
01112         nl_cache_mngt_unregister(&rtnl_neigh_ops);
01113 }
01114 
01115 /** @} */

Generated on Fri Apr 27 14:14:07 2007 for libnl by  doxygen 1.5.1