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