This commit was generated by cvs2svn to compensate for changes in r786,
authorDaniel Hokka Zakrisson <dhokka@cs.princeton.edu>
Fri, 27 Jul 2007 19:49:44 +0000 (19:49 +0000)
committerDaniel Hokka Zakrisson <dhokka@cs.princeton.edu>
Fri, 27 Jul 2007 19:49:44 +0000 (19:49 +0000)
which included commits to RCS files with non-trunk default branches.

138 files changed:
COPYING [new file with mode: 0644]
ChangeLog [new file with mode: 0644]
Makefile [new file with mode: 0644]
Makefile.opts.in [new file with mode: 0644]
Makefile.rules [new file with mode: 0644]
aclocal.m4 [new file with mode: 0644]
configure [new file with mode: 0755]
configure.in [new file with mode: 0644]
doc/Doxyfile [new file with mode: 0644]
doc/Makefile [new file with mode: 0644]
doc/libnl.css [new file with mode: 0644]
include/Makefile [new file with mode: 0644]
include/linux/gen_stats.h [new file with mode: 0644]
include/linux/if.h [new file with mode: 0644]
include/linux/if_arp.h [new file with mode: 0644]
include/linux/if_ether.h [new file with mode: 0644]
include/linux/pkt_cls.h [new file with mode: 0644]
include/linux/pkt_sched.h [new file with mode: 0644]
include/netlink-local.h [new file with mode: 0644]
include/netlink-tc.h [new file with mode: 0644]
include/netlink-types.h [new file with mode: 0644]
include/netlink/addr.h [new file with mode: 0644]
include/netlink/attr.h [new file with mode: 0644]
include/netlink/cache.h [new file with mode: 0644]
include/netlink/data.h [new file with mode: 0644]
include/netlink/fib_lookup/lookup.h [new file with mode: 0644]
include/netlink/fib_lookup/request.h [new file with mode: 0644]
include/netlink/handlers.h [new file with mode: 0644]
include/netlink/list.h [new file with mode: 0644]
include/netlink/msg.h [new file with mode: 0644]
include/netlink/netlink-compat.h [new file with mode: 0644]
include/netlink/netlink-kernel.h [new file with mode: 0644]
include/netlink/netlink.h [new file with mode: 0644]
include/netlink/object.h [new file with mode: 0644]
include/netlink/route/addr.h [new file with mode: 0644]
include/netlink/route/class-modules.h [new file with mode: 0644]
include/netlink/route/class.h [new file with mode: 0644]
include/netlink/route/classifier-modules.h [new file with mode: 0644]
include/netlink/route/classifier.h [new file with mode: 0644]
include/netlink/route/cls/fw.h [new file with mode: 0644]
include/netlink/route/cls/police.h [new file with mode: 0644]
include/netlink/route/cls/u32.h [new file with mode: 0644]
include/netlink/route/link.h [new file with mode: 0644]
include/netlink/route/neighbour.h [new file with mode: 0644]
include/netlink/route/neightbl.h [new file with mode: 0644]
include/netlink/route/nexthop.h [new file with mode: 0644]
include/netlink/route/qdisc-modules.h [new file with mode: 0644]
include/netlink/route/qdisc.h [new file with mode: 0644]
include/netlink/route/route.h [new file with mode: 0644]
include/netlink/route/rtnl.h [new file with mode: 0644]
include/netlink/route/rule.h [new file with mode: 0644]
include/netlink/route/sch/cbq.h [new file with mode: 0644]
include/netlink/route/sch/dsmark.h [new file with mode: 0644]
include/netlink/route/sch/fifo.h [new file with mode: 0644]
include/netlink/route/sch/htb.h [new file with mode: 0644]
include/netlink/route/sch/netem.h [new file with mode: 0644]
include/netlink/route/sch/prio.h [new file with mode: 0644]
include/netlink/route/sch/red.h [new file with mode: 0644]
include/netlink/route/sch/sfq.h [new file with mode: 0644]
include/netlink/route/sch/tbf.h [new file with mode: 0644]
include/netlink/route/tc.h [new file with mode: 0644]
include/netlink/rtnetlink-kernel.h [new file with mode: 0644]
include/netlink/types.h [new file with mode: 0644]
include/netlink/utils.h [new file with mode: 0644]
install-sh [new file with mode: 0755]
lib/Makefile [new file with mode: 0644]
lib/addr.c [new file with mode: 0644]
lib/attr.c [new file with mode: 0644]
lib/cache.c [new file with mode: 0644]
lib/cache_mngt.c [new file with mode: 0644]
lib/data.c [new file with mode: 0644]
lib/defs.h [new file with mode: 0644]
lib/defs.h.in [new file with mode: 0644]
lib/fib_lookup/lookup.c [new file with mode: 0644]
lib/fib_lookup/request.c [new file with mode: 0644]
lib/handlers.c [new file with mode: 0644]
lib/msg.c [new file with mode: 0644]
lib/nl.c [new file with mode: 0644]
lib/object.c [new file with mode: 0644]
lib/route/addr.c [new file with mode: 0644]
lib/route/class.c [new file with mode: 0644]
lib/route/classifier.c [new file with mode: 0644]
lib/route/cls/fw.c [new file with mode: 0644]
lib/route/cls/police.c [new file with mode: 0644]
lib/route/cls/u32.c [new file with mode: 0644]
lib/route/link.c [new file with mode: 0644]
lib/route/neigh.c [new file with mode: 0644]
lib/route/neightbl.c [new file with mode: 0644]
lib/route/nexthop.c [new file with mode: 0644]
lib/route/qdisc.c [new file with mode: 0644]
lib/route/route.c [new file with mode: 0644]
lib/route/rtnl.c [new file with mode: 0644]
lib/route/rule.c [new file with mode: 0644]
lib/route/sch/blackhole.c [new file with mode: 0644]
lib/route/sch/cbq.c [new file with mode: 0644]
lib/route/sch/dsmark.c [new file with mode: 0644]
lib/route/sch/fifo.c [new file with mode: 0644]
lib/route/sch/htb.c [new file with mode: 0644]
lib/route/sch/netem.c [new file with mode: 0644]
lib/route/sch/prio.c [new file with mode: 0644]
lib/route/sch/red.c [new file with mode: 0644]
lib/route/sch/sfq.c [new file with mode: 0644]
lib/route/sch/tbf.c [new file with mode: 0644]
lib/route/tc.c [new file with mode: 0644]
lib/utils.c [new file with mode: 0644]
libnl-1.pc [new file with mode: 0644]
libnl-1.pc.in [new file with mode: 0644]
libnl.spec [new file with mode: 0644]
src/Makefile [new file with mode: 0644]
src/f_addr.c [new file with mode: 0644]
src/f_link.c [new file with mode: 0644]
src/f_neigh.c [new file with mode: 0644]
src/f_route.c [new file with mode: 0644]
src/nl-addr-add.c [new file with mode: 0644]
src/nl-addr-delete.c [new file with mode: 0644]
src/nl-addr-dump.c [new file with mode: 0644]
src/nl-fib-lookup.c [new file with mode: 0644]
src/nl-link-dump.c [new file with mode: 0644]
src/nl-link-ifindex2name.c [new file with mode: 0644]
src/nl-link-name2ifindex.c [new file with mode: 0644]
src/nl-link-set.c [new file with mode: 0644]
src/nl-link-stats.c [new file with mode: 0644]
src/nl-list-sockets.c [new file with mode: 0644]
src/nl-monitor.c [new file with mode: 0644]
src/nl-neigh-add.c [new file with mode: 0644]
src/nl-neigh-delete.c [new file with mode: 0644]
src/nl-neigh-dump.c [new file with mode: 0644]
src/nl-neightbl-dump.c [new file with mode: 0644]
src/nl-qdisc-add.c [new file with mode: 0644]
src/nl-qdisc-delete.c [new file with mode: 0644]
src/nl-qdisc-dump.c [new file with mode: 0644]
src/nl-route-dump.c [new file with mode: 0644]
src/nl-route-get.c [new file with mode: 0644]
src/nl-rule-dump.c [new file with mode: 0644]
src/nl-tctree-dump.c [new file with mode: 0644]
src/nl-util-addr.c [new file with mode: 0644]
src/utils.c [new file with mode: 0644]
src/utils.h [new file with mode: 0644]

diff --git a/COPYING b/COPYING
new file mode 100644 (file)
index 0000000..371ec20
--- /dev/null
+++ b/COPYING
@@ -0,0 +1,461 @@
+
+                  GNU LESSER GENERAL PUBLIC LICENSE
+                       Version 2.1, February 1999
+
+ Copyright (C) 1991, 1999 Free Software Foundation, Inc.
+     59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+[This is the first released version of the Lesser GPL.  It also counts
+ as the successor of the GNU Library Public License, version 2, hence
+ the version number 2.1.]
+
+                            Preamble
+
+  The licenses for most software are designed to take away your
+freedom to share and change it.  By contrast, the GNU General Public
+Licenses are intended to guarantee your freedom to share and change
+free software--to make sure the software is free for all its users.
+
+  This license, the Lesser General Public License, applies to some
+specially designated software packages--typically libraries--of the
+Free Software Foundation and other authors who decide to use it.  You
+can use it too, but we suggest you first think carefully about whether
+this license or the ordinary General Public License is the better
+strategy to use in any particular case, based on the explanations
+below.
+
+  When we speak of free software, we are referring to freedom of use,
+not price.  Our General Public Licenses are designed to make sure that
+you have the freedom to distribute copies of free software (and charge
+for this service if you wish); that you receive source code or can get
+it if you want it; that you can change the software and use pieces of
+it in new free programs; and that you are informed that you can do
+these things.
+
+  To protect your rights, we need to make restrictions that forbid
+distributors to deny you these rights or to ask you to surrender these
+rights.  These restrictions translate to certain responsibilities for
+you if you distribute copies of the library or if you modify it.
+
+  For example, if you distribute copies of the library, whether gratis
+or for a fee, you must give the recipients all the rights that we gave
+you.  You must make sure that they, too, receive or can get the source
+code.  If you link other code with the library, you must provide
+complete object files to the recipients, so that they can relink them
+with the library after making changes to the library and recompiling
+it.  And you must show them these terms so they know their rights.
+
+  We protect your rights with a two-step method: (1) we copyright the
+library, and (2) we offer you this license, which gives you legal
+permission to copy, distribute and/or modify the library.
+
+  To protect each distributor, we want to make it very clear that
+there is no warranty for the free library.  Also, if the library is
+modified by someone else and passed on, the recipients should know
+that what they have is not the original version, so that the original
+author's reputation will not be affected by problems that might be
+introduced by others.
+\f
+  Finally, software patents pose a constant threat to the existence of
+any free program.  We wish to make sure that a company cannot
+effectively restrict the users of a free program by obtaining a
+restrictive license from a patent holder.  Therefore, we insist that
+any patent license obtained for a version of the library must be
+consistent with the full freedom of use specified in this license.
+
+  Most GNU software, including some libraries, is covered by the
+ordinary GNU General Public License.  This license, the GNU Lesser
+General Public License, applies to certain designated libraries, and
+is quite different from the ordinary General Public License.  We use
+this license for certain libraries in order to permit linking those
+libraries into non-free programs.
+
+  When a program is linked with a library, whether statically or using
+a shared library, the combination of the two is legally speaking a
+combined work, a derivative of the original library.  The ordinary
+General Public License therefore permits such linking only if the
+entire combination fits its criteria of freedom.  The Lesser General
+Public License permits more lax criteria for linking other code with
+the library.
+
+  We call this license the "Lesser" General Public License because it
+does Less to protect the user's freedom than the ordinary General
+Public License.  It also provides other free software developers Less
+of an advantage over competing non-free programs.  These disadvantages
+are the reason we use the ordinary General Public License for many
+libraries.  However, the Lesser license provides advantages in certain
+special circumstances.
+
+  For example, on rare occasions, there may be a special need to
+encourage the widest possible use of a certain library, so that it
+becomes a de-facto standard.  To achieve this, non-free programs must
+be allowed to use the library.  A more frequent case is that a free
+library does the same job as widely used non-free libraries.  In this
+case, there is little to gain by limiting the free library to free
+software only, so we use the Lesser General Public License.
+
+  In other cases, permission to use a particular library in non-free
+programs enables a greater number of people to use a large body of
+free software.  For example, permission to use the GNU C Library in
+non-free programs enables many more people to use the whole GNU
+operating system, as well as its variant, the GNU/Linux operating
+system.
+
+  Although the Lesser General Public License is Less protective of the
+users' freedom, it does ensure that the user of a program that is
+linked with the Library has the freedom and the wherewithal to run
+that program using a modified version of the Library.
+
+  The precise terms and conditions for copying, distribution and
+modification follow.  Pay close attention to the difference between a
+"work based on the library" and a "work that uses the library".  The
+former contains code derived from the library, whereas the latter must
+be combined with the library in order to run.
+\f
+                  GNU LESSER GENERAL PUBLIC LICENSE
+   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+  0. This License Agreement applies to any software library or other
+program which contains a notice placed by the copyright holder or
+other authorized party saying it may be distributed under the terms of
+this Lesser General Public License (also called "this License").
+Each licensee is addressed as "you".
+
+  A "library" means a collection of software functions and/or data
+prepared so as to be conveniently linked with application programs
+(which use some of those functions and data) to form executables.
+
+  The "Library", below, refers to any such software library or work
+which has been distributed under these terms.  A "work based on the
+Library" means either the Library or any derivative work under
+copyright law: that is to say, a work containing the Library or a
+portion of it, either verbatim or with modifications and/or translated
+straightforwardly into another language.  (Hereinafter, translation is
+included without limitation in the term "modification".)
+
+  "Source code" for a work means the preferred form of the work for
+making modifications to it.  For a library, complete source code means
+all the source code for all modules it contains, plus any associated
+interface definition files, plus the scripts used to control
+compilation and installation of the library.
+
+  Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope.  The act of
+running a program using the Library is not restricted, and output from
+such a program is covered only if its contents constitute a work based
+on the Library (independent of the use of the Library in a tool for
+writing it).  Whether that is true depends on what the Library does
+and what the program that uses the Library does.
+
+  1. You may copy and distribute verbatim copies of the Library's
+complete source code as you receive it, in any medium, provided that
+you conspicuously and appropriately publish on each copy an
+appropriate copyright notice and disclaimer of warranty; keep intact
+all the notices that refer to this License and to the absence of any
+warranty; and distribute a copy of this License along with the
+Library.
+
+  You may charge a fee for the physical act of transferring a copy,
+and you may at your option offer warranty protection in exchange for a
+fee.
+\f
+  2. You may modify your copy or copies of the Library or any portion
+of it, thus forming a work based on the Library, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+    a) The modified work must itself be a software library.
+
+    b) You must cause the files modified to carry prominent notices
+    stating that you changed the files and the date of any change.
+
+    c) You must cause the whole of the work to be licensed at no
+    charge to all third parties under the terms of this License.
+
+    d) If a facility in the modified Library refers to a function or a
+    table of data to be supplied by an application program that uses
+    the facility, other than as an argument passed when the facility
+    is invoked, then you must make a good faith effort to ensure that,
+    in the event an application does not supply such function or
+    table, the facility still operates, and performs whatever part of
+    its purpose remains meaningful.
+
+    (For example, a function in a library to compute square roots has
+    a purpose that is entirely well-defined independent of the
+    application.  Therefore, Subsection 2d requires that any
+    application-supplied function or table used by this function must
+    be optional: if the application does not supply it, the square
+    root function must still compute square roots.)
+
+These requirements apply to the modified work as a whole.  If
+identifiable sections of that work are not derived from the Library,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works.  But when you
+distribute the same sections as part of a whole which is a work based
+on the Library, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote
+it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Library.
+
+In addition, mere aggregation of another work not based on the Library
+with the Library (or with a work based on the Library) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+  3. You may opt to apply the terms of the ordinary GNU General Public
+License instead of this License to a given copy of the Library.  To do
+this, you must alter all the notices that refer to this License, so
+that they refer to the ordinary GNU General Public License, version 2,
+instead of to this License.  (If a newer version than version 2 of the
+ordinary GNU General Public License has appeared, then you can specify
+that version instead if you wish.)  Do not make any other change in
+these notices.
+\f
+  Once this change is made in a given copy, it is irreversible for
+that copy, so the ordinary GNU General Public License applies to all
+subsequent copies and derivative works made from that copy.
+
+  This option is useful when you wish to copy part of the code of
+the Library into a program that is not a library.
+
+  4. You may copy and distribute the Library (or a portion or
+derivative of it, under Section 2) in object code or executable form
+under the terms of Sections 1 and 2 above provided that you accompany
+it with the complete corresponding machine-readable source code, which
+must be distributed under the terms of Sections 1 and 2 above on a
+medium customarily used for software interchange.
+
+  If distribution of object code is made by offering access to copy
+from a designated place, then offering equivalent access to copy the
+source code from the same place satisfies the requirement to
+distribute the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+  5. A program that contains no derivative of any portion of the
+Library, but is designed to work with the Library by being compiled or
+linked with it, is called a "work that uses the Library".  Such a
+work, in isolation, is not a derivative work of the Library, and
+therefore falls outside the scope of this License.
+
+  However, linking a "work that uses the Library" with the Library
+creates an executable that is a derivative of the Library (because it
+contains portions of the Library), rather than a "work that uses the
+library".  The executable is therefore covered by this License.
+Section 6 states terms for distribution of such executables.
+
+  When a "work that uses the Library" uses material from a header file
+that is part of the Library, the object code for the work may be a
+derivative work of the Library even though the source code is not.
+Whether this is true is especially significant if the work can be
+linked without the Library, or if the work is itself a library.  The
+threshold for this to be true is not precisely defined by law.
+
+  If such an object file uses only numerical parameters, data
+structure layouts and accessors, and small macros and small inline
+functions (ten lines or less in length), then the use of the object
+file is unrestricted, regardless of whether it is legally a derivative
+work.  (Executables containing this object code plus portions of the
+Library will still fall under Section 6.)
+
+  Otherwise, if the work is a derivative of the Library, you may
+distribute the object code for the work under the terms of Section 6.
+Any executables containing that work also fall under Section 6,
+whether or not they are linked directly with the Library itself.
+\f
+  6. As an exception to the Sections above, you may also combine or
+link a "work that uses the Library" with the Library to produce a
+work containing portions of the Library, and distribute that work
+under terms of your choice, provided that the terms permit
+modification of the work for the customer's own use and reverse
+engineering for debugging such modifications.
+
+  You must give prominent notice with each copy of the work that the
+Library is used in it and that the Library and its use are covered by
+this License.  You must supply a copy of this License.  If the work
+during execution displays copyright notices, you must include the
+copyright notice for the Library among them, as well as a reference
+directing the user to the copy of this License.  Also, you must do one
+of these things:
+
+    a) Accompany the work with the complete corresponding
+    machine-readable source code for the Library including whatever
+    changes were used in the work (which must be distributed under
+    Sections 1 and 2 above); and, if the work is an executable linked
+    with the Library, with the complete machine-readable "work that
+    uses the Library", as object code and/or source code, so that the
+    user can modify the Library and then relink to produce a modified
+    executable containing the modified Library.  (It is understood
+    that the user who changes the contents of definitions files in the
+    Library will not necessarily be able to recompile the application
+    to use the modified definitions.)
+
+    b) Use a suitable shared library mechanism for linking with the
+    Library.  A suitable mechanism is one that (1) uses at run time a
+    copy of the library already present on the user's computer system,
+    rather than copying library functions into the executable, and (2)
+    will operate properly with a modified version of the library, if
+    the user installs one, as long as the modified version is
+    interface-compatible with the version that the work was made with.
+
+    c) Accompany the work with a written offer, valid for at least
+    three years, to give the same user the materials specified in
+    Subsection 6a, above, for a charge no more than the cost of
+    performing this distribution.
+
+    d) If distribution of the work is made by offering access to copy
+    from a designated place, offer equivalent access to copy the above
+    specified materials from the same place.
+
+    e) Verify that the user has already received a copy of these
+    materials or that you have already sent this user a copy.
+
+  For an executable, the required form of the "work that uses the
+Library" must include any data and utility programs needed for
+reproducing the executable from it.  However, as a special exception,
+the materials to be distributed need not include anything that is
+normally distributed (in either source or binary form) with the major
+components (compiler, kernel, and so on) of the operating system on
+which the executable runs, unless that component itself accompanies
+the executable.
+
+  It may happen that this requirement contradicts the license
+restrictions of other proprietary libraries that do not normally
+accompany the operating system.  Such a contradiction means you cannot
+use both them and the Library together in an executable that you
+distribute.
+\f
+  7. You may place library facilities that are a work based on the
+Library side-by-side in a single library together with other library
+facilities not covered by this License, and distribute such a combined
+library, provided that the separate distribution of the work based on
+the Library and of the other library facilities is otherwise
+permitted, and provided that you do these two things:
+
+    a) Accompany the combined library with a copy of the same work
+    based on the Library, uncombined with any other library
+    facilities.  This must be distributed under the terms of the
+    Sections above.
+
+    b) Give prominent notice with the combined library of the fact
+    that part of it is a work based on the Library, and explaining
+    where to find the accompanying uncombined form of the same work.
+
+  8. You may not copy, modify, sublicense, link with, or distribute
+the Library except as expressly provided under this License.  Any
+attempt otherwise to copy, modify, sublicense, link with, or
+distribute the Library is void, and will automatically terminate your
+rights under this License.  However, parties who have received copies,
+or rights, from you under this License will not have their licenses
+terminated so long as such parties remain in full compliance.
+
+  9. You are not required to accept this License, since you have not
+signed it.  However, nothing else grants you permission to modify or
+distribute the Library or its derivative works.  These actions are
+prohibited by law if you do not accept this License.  Therefore, by
+modifying or distributing the Library (or any work based on the
+Library), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Library or works based on it.
+
+  10. Each time you redistribute the Library (or any work based on the
+Library), the recipient automatically receives a license from the
+original licensor to copy, distribute, link with or modify the Library
+subject to these terms and conditions.  You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties with
+this License.
+\f
+  11. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License.  If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Library at all.  For example, if a patent
+license would not permit royalty-free redistribution of the Library by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Library.
+
+If any portion of this section is held invalid or unenforceable under
+any particular circumstance, the balance of the section is intended to
+apply, and the section as a whole is intended to apply in other
+circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system which is
+implemented by public license practices.  Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+  12. If the distribution and/or use of the Library is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Library under this License
+may add an explicit geographical distribution limitation excluding those
+countries, so that distribution is permitted only in or among
+countries not thus excluded.  In such case, this License incorporates
+the limitation as if written in the body of this License.
+
+  13. The Free Software Foundation may publish revised and/or new
+versions of the Lesser General Public License from time to time.
+Such new versions will be similar in spirit to the present version,
+but may differ in detail to address new problems or concerns.
+
+Each version is given a distinguishing version number.  If the Library
+specifies a version number of this License which applies to it and
+"any later version", you have the option of following the terms and
+conditions either of that version or of any later version published by
+the Free Software Foundation.  If the Library does not specify a
+license version number, you may choose any version ever published by
+the Free Software Foundation.
+\f
+  14. If you wish to incorporate parts of the Library into other free
+programs whose distribution conditions are incompatible with these,
+write to the author to ask for permission.  For software which is
+copyrighted by the Free Software Foundation, write to the Free
+Software Foundation; we sometimes make exceptions for this.  Our
+decision will be guided by the two goals of preserving the free status
+of all derivatives of our free software and of promoting the sharing
+and reuse of software generally.
+
+                            NO WARRANTY
+
+  15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
+WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
+EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
+OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
+KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
+LIBRARY IS WITH YOU.  SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
+THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+  16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
+WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
+AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
+FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
+CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
+LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
+RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
+FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
+SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
+DAMAGES.
+
+                     END OF TERMS AND CONDITIONS
diff --git a/ChangeLog b/ChangeLog
new file mode 100644 (file)
index 0000000..658d251
--- /dev/null
+++ b/ChangeLog
@@ -0,0 +1,260 @@
+Summary of Changes from 1.0-pre5 to 1.0-pre6
+================================================
+       Christopher Aillon <caillon@redhat.com>
+         o Use $(libdir) instead of $(prefix)/lib for 64bit awesomeness.
+
+       Thomas Graf <tgraf@suug.ch>
+         o Extend nl_msg to include source address, destination address
+           and the protocol being used.
+         o Make nl_send*() take a nl_msg instead of a nlmsghdr (API BREAK)
+         o Change callbacks to take a nl_msg instead of source address
+           and nlmsghdr (API BREAK)
+         o caches must specify the protocol they're hooked up from now on
+           if they intend to be associated with message types.
+         o cache_mngt_associate now takes the protocol besides the message
+           type to allow for multiple protocols to be supported (API BREAK)
+         o overwrite destination address in nl_send() when specified in the
+           message itself, allows for unbound addressing.
+         o Support for netlink based fib_lookup()
+         o Documentation fixes
+         o Fix double nlmsg_free() in nl_recvmsgs() while receiving
+           a multipart message and the read was interrupted.
+         o Change cache operations to store names for message types.
+         o Provide interface to convert message type to character string.
+         o Add dp_dump_msgtype to prefix each dumped element with a
+           pretty printed message type.
+         o netlink fib lookup support
+         o nl_recvmsgs() debugging
+         o use nl_cachemngt_type2name() when pretty printing netlink header
+         o Routing protocol translations.
+         o Routing metric translations.
+         o Revised route dumping
+         o Nexthop flag translations.
+         o Add support for IFF_DORMANT
+
+       Petr Gotthard <petr.gotthard@siemens.com>
+       Siemens AG Oesterreich
+         o Fix access to obj after freeing it
+         o Fix u32 selector access after realloc() 
+         o Fix missing out-of-memory error handling in various places
+         o Enhance nl-monitor to have group selection selectable and
+           demonstrate usage of select()
+         o Don't ignore IFF_RUNNING any longer
+         o fw classifier support
+       
+       Patrick McHardy <kaber@trash.net>
+         o Fix conflicting types for __u64
+         o Fix printf format string warnings
+         o Fix object cloning
+         o Deal with structure padding in nl_object_clone
+         o Fix nl_addr leak
+         o Set ce_msgtype in all parsed objects
+         o Fix addr flag filter
+         o Fix RTNLGRP definitions (was based on broken kernel version)
+         o Export nl_get_errno()
+         o Add function to get/set peer pid
+         o Add IFF_LOWER_UP
+         o Add/export some missing accessor functions
+         o print /0 prefix in nl_addr2str()
+         o Fix invalid free in nl_addr_parse for AF_UNSPEC addresses
+         o Use __str2flags instead of __str2type in rtnl_link_str2flags()
+         o Make sure object and filter types match in nl_object_match()
+         o Add support for credential passing over netlink sockets (API BREAK)
+         o Add support for custom dump callbacks
+         o Add NL_DUMP_ENV format
+
+       Michael Biebl <biebl@teco.edu>
+       "Alex V. Myltsev" <avm@altlinux.ru>
+         o Makefile fixes
+
+
+Summary of Changes from 1.0-pre4 to 1.0-pre5
+================================================
+       Thomas Graf <tgraf@suug.ch>
+         o Use minimized local copies for <linux/if.h>, <linux/if_arp.h>,
+           and <linux/if_ether.h> to avoid compile troubles with
+           applications including <net/if*.h>
+           Reported by Christopher Aillon.
+
+Summary of Changes from 1.0-pre3 to 1.0-pre4
+================================================
+       Thomas Graf <tgraf@suug.ch>
+         o Fix wrong rtnl_addr_set_prefixlen() external declaration,
+           reported by Dan Williams.
+         o Fix nl_addr_parse() to not change the original string
+           for prefixes.
+         o Do not build documentation per default, but have the user
+           issue 'make gendoc'
+         o Assume neighbours to be permanent, set NUD_PERMANENT if not
+           specified otherwise.
+
+Summary of Changes from 1.0-pre2 to 1.0-pre3
+================================================
+       Thomas Graf <tgraf@suug.ch>
+         o Fix SFQ parser to allocate qdisc options.
+         o Fix rule statistics dumping to not call itself.
+         o Complete Netem qdisc interface.
+         o Add rtnl_*_put() and rtnl_*_free() to increase readability.
+         o Cleanup of nl-* tools
+         o Fix inclusion guards of route/neightbl.h
+         o Fix nl_connect() to only modify rx/tx socket buffers if not
+           already modified by the user.
+         o Fix wrong nl_handle_alloc() prototype.
+         o Fix typo in route/addr.c causing label to be marked as
+           local address.
+         o Use ~0UL as default prefix length instead of 0.
+         o Fix neighbour message parser to correctly store core.
+           attributes and provide them again.
+         o Fix neighbour message parser to correctly guess address family.
+           to make it compatible with nl_addr_parse() and ether llc
+           addresses.
+         o Add rtnl_route_table2str(), rtnl_route_str2table().
+         o Add nl_cache_nitems_filter() to find out if a filter produces
+           any matches.
+         o Remove rtnl_rule_set_(dst|src)_str() (obsolete).
+         o Remove scope and protocol field of routing rule.
+         o Complete routing rules module.
+         o Move realms translations from route to rtnl module.
+
+Summary of Changes from 1.0-pre1 to 1.0-pre2
+================================================
+       Thomas Graf <tgraf@suug.ch>
+         o More API documentation
+         o Added flags argument to rtnl_addr_(add|build_add_request)().
+         o Added rtnl_addr_(set|get)_multicast().
+         o Moved scope translations routines from route/route.c to
+           route/rtnl.c, required by other modules as well.
+         o Removed old rtattr bits from rtnetlink-kernel.h
+         o Customized libnl.css for doxygen documentation
+         o Removed non-reentrant translation routines, only bloating
+           the code and too risky.
+         o Fixed wrong version number from 1.0-pre1.
+         o Reenabled unfinished policer module.
+         o Reworked TBF module, automatic caluclation of transmit times,
+           limit setable via latency, automatic cell size calculation,
+           options TLV generation. (untested)
+         o Renamed nl_xmittime() to rtnl_tc_calc_txtime().
+         o Renamde nl_build_rtable() to rtnl_tc_build_rate_table()
+
+       Petr Gotthard <petr.gotthard@siemens.com>,
+       Siemens AG Oesterreich
+         o Fix symlinks to libnl library files to be moveable
+         o Fix extern struct prototypes meant to be static.
+         o Add empty install target to src/Makefile
+
+       Simon Stelling <blubb@gentoo.org>
+         o Use LIBDIR instead of $(prefix)/lib for users to alllow librariers
+           into $(prefix)/lib64.
+
+Summary of Changes from 0.5.0 to 1.0-pre1
+================================================
+       Thomas Graf <tgraf@suug.ch>
+         o Uncountable number of changes, rewrite of certain modules,
+           several major API breakages
+       
+       Petr Gotthard <petr.gotthard@siemens.com>,
+       Siemens AG Oesterreich
+         o added class_build, rtnl_class_build_add_request, rtnl_class_add
+         o added HTB (Hierachical Token Bucket) class support
+         o added nl_xmittime, nl_build_rtable
+         o added nl_data_append to realloc a nl_data structure
+         o added rtnl_rcopy_ratespec as reverse to rtnl_copy_ratespec
+         o fixed byte order conversion of rtnl_filter.protocol
+         o SuSE and Fedora Linux compile fixes
+         o fixed u32 classifier support
+         o added rtnl_u32_set_handle, rtnl_u32_set_classid, rtnl_u32_set_flags
+           and several rtnl_u32_add_key_... operations to u32 classifier
+
+Summary of Changes from 0.4.4 to 0.5.0
+================================================
+       Thomas Graf <tgraf@suug.ch>
+         o API documentation
+         o nl_cache_filter to manually filter on a object
+         o partial routing support
+         o routing rules support
+         o Propely set address family when setting addresses
+         o debug flag and some rare messages, more to come
+         o make error mesage verboseness configureable
+         o tc fixes to wait for ack
+         o cleanup and adaption of address code to latest internal API
+         o various cleanups
+         o dozens of API breakages (better now than later)
+
+       Daniel Hottinger <hotti@hotti.ch>
+         o arch 64bit printf length modifier fixes
+
+       Baruch Even <baruch@ev-en.org>,
+       Mediatrix Telecom, inc. <ericb@mediatrix.com>
+         o address support
+
+Summary of changes from 0.4.3 to 0.4.4
+================================================
+       Thomas Graf <tgraf@suug.ch>:
+         o overall cleanups for better code quality
+         o replace dump_(brief|full|with_stats) ops with
+           dump[NL_DUMP_MAX] array to allow further additions without
+           breaking the ABI.
+         o add of send_ack callback, called whenever when oppenent
+           asks for an ACK.
+         o make nl_parse_rtattr initialize the tb buffer like in the
+           kernel, caller must no longer take care of it.
+         o remove nl_addrnattr (obsolete)
+         o fixed nl_msg_append_raw to correctly calculate length
+           for raw data not aligned to NLMSG_ALIGN
+         o fix memory leak in nl_recv in case of errors
+         o correctly check sequence numbers if more than one message
+           was sent out before the answer is being received.
+         o add workaround for buggy netlink applications not properly
+           setting NLM_F_MULTI.
+
+Summary of changes from 0.4.2 to 0.4.3
+================================================
+
+       Thomas Graf <tgraf@suug.ch>:
+         o use parser_param in nl_cache_parse
+         o EGP: dump nfilters attribute
+         o allow retrieving of filters attached to classes via
+           FILTER_CACHE_PARENT(C) cache argument
+         o filter message building API
+
+Summary of changes from 0.4.1 to 0.4.2
+================================================
+
+       Baruch Even <baruch@ev-en.org>:
+         o memory leak fix in nl_parse_rtattr
+         o reset padding to 0 when appending raw data to a nl_msg
+         o avoid overwriting nlmsg ptr when buffer extending fails
+         o typo fixes
+         o create symlinks libnl.so.0 and libnl.so
+       
+       Thomas Graf <tgraf@suug.ch>:
+         o EGP classifier support
+         o avoid null pointer in printf call
+         o added nl_cache_parse to put nl_msg's into a cache
+         o added rtnl_filter_build to build a nl_msg filter message
+         o correctly install header files
+         o nl_msg_payload/nl_msg_payloadlen to access nl_msg payload
+         o nl_parse_nested macro to simplify nested TLV parsing
+         o NL_ERROR_ASSERT compile flag to assert(0) on errors
+         o rta alignment fix in nl_msg_append_tlv
+         o added nl_msg_parse_rtattr as shortcut for nl_parse_rtattr
+           for nl_msg API
+         o added nl_parse_nested for nested TLVs
+         o added RTA_ARRAY_ELEMS macro to calculate array length
+           for array TLVs
+         o added nl_wait_for_ack to wait for the next ack
+         o added rtnl_link_build_change_request(...)
+         o added rtnl_neigh_build_*_request
+         o converted neighbour code to use nl_wait_for_ack
+         o cb_recvmsgs_ow callback to overwrite internal calls to
+           nl_recvmsgs_def
+         o cb_seq_check callback to overwrite default sequence checking
+         o added nl_parser_param as argument for message parsers including
+           a callback to be called upon successful parsing of a message.
+           Removes the requirement of having all parsed messages to be added
+           to a cache.
+         o added cb_recv_ow and nl_send_ow callbacks to overwrite internal
+           calls to nl_recv and nl_send.
+
+       Jamal Hadi Salim <hadi@cyberus.ca>
+         o Linux 2.4 compile fixes
diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..b21669a
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,64 @@
+#
+# Makefile
+#
+#      This library is free software; you can redistribute it and/or
+#      modify it under the terms of the GNU Lesser General Public
+#      License as published by the Free Software Foundation version 2.1
+#      of the License.
+#
+# Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+#
+
+ifeq ($(shell [ ! -r Makefile.opts ] && echo 1),)
+    include Makefile.opts
+endif
+
+SUBDIRS := lib include doc src
+.PHONY: all clean distclean install gendoc $(SUBDIRS)
+
+all: Makefile.opts
+       @for dir in $(SUBDIRS); do \
+               echo "Entering $$dir" && cd $$dir && $(MAKE) && cd ..; \
+       done
+
+clean: Makefile.opts
+       rm -f cscope.*
+       @for dir in $(SUBDIRS); do \
+               echo "Entering $$dir" && cd $$dir && $(MAKE) clean && cd ..; \
+       done
+
+distclean: clean
+       @$(RM) -rf Makefile.opts autom4te.cache config.log config.status
+       @for dir in $(SUBDIRS); do \
+               echo "Entering $$dir" && cd $$dir && $(MAKE) distclean && cd ..; \
+       done
+
+install: Makefile.opts
+       @for dir in $(SUBDIRS); do \
+               echo "Entering $$dir" && cd $$dir && $(MAKE) install && cd ..; \
+       done
+       mkdir -p $(DESTDIR)$(libdir)/pkgconfig/
+       install -m 0644 libnl-1.pc $(DESTDIR)$(libdir)/pkgconfig/
+
+gendoc:
+       $(MAKE) -C doc gendoc
+
+show: Makefile.opts
+       @echo "CC:          $(CC)"
+       @echo "RM:          $(RM)"
+       @echo "CFLAGS:      $(CFLAGS)"
+       @echo "DEPFLAGS:    $(DEPFLAGS)"
+       @echo "LDFLAGS:     $(LDFLAGS)"
+       @echo "DESTDIR:     $(DESTDIR)"
+       @echo "prefix:      $(prefix)"
+       @echo "libdir:      $(libdir)"
+       @echo "includedir:  $(includedir)"
+
+cscope:
+       cscope -b -q -R -Iinclude -slib -ssrc
+
+
+$(SUBDIRS):
+       cd $@ && $(MAKE)
+
+-include Makefile.rules
diff --git a/Makefile.opts.in b/Makefile.opts.in
new file mode 100644 (file)
index 0000000..87d229b
--- /dev/null
@@ -0,0 +1,39 @@
+#
+# Makefile.opts.in
+#
+#      This library is free software; you can redistribute it and/or
+#      modify it under the terms of the GNU Lesser General Public
+#      License as published by the Free Software Foundation version 2.1
+#      of the License.
+#
+# Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+#
+
+CC               := @CC@
+CFLAGS           := @CFLAGS@
+LDFLAGS          := @LDFLAGS@
+CPPFLAGS         := @CPPFLAGS@
+PACKAGE_NAME     := @PACKAGE_NAME@
+PACKAGE_VERSION  := @PACKAGE_VERSION@
+
+LIBNL_LIB        := @LIBNL_LIB@
+
+prefix           := @prefix@
+exec_prefix      := @exec_prefix@
+libdir           := @libdir@
+includedir       := @includedir@
+mandir           := @mandir@
+sysconfdir       := @sysconfdir@
+
+AR               := ar
+RM               := rm
+LN               := ln
+
+DEPFLAGS         += -M -I../include/ -I. $(CPPFLAGS)
+CFLAGS           += -g -I./include -I../include -I. $(CPPFLAGS) -D_GNU_SOURCE
+MAKEFLAGS        += --no-print-directory
+
+ifeq ($(CC),gcc)
+CFLAGS           += -Wall -ggdb
+endif
+
diff --git a/Makefile.rules b/Makefile.rules
new file mode 100644 (file)
index 0000000..de32230
--- /dev/null
@@ -0,0 +1,37 @@
+#
+# Makefile.rules
+#
+#      This library is free software; you can redistribute it and/or
+#      modify it under the terms of the GNU Lesser General Public
+#      License as published by the Free Software Foundation version 2.1
+#      of the License.
+#
+# Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+#
+
+.SUFFIXES:
+.SUFFIXES: .deps/%.d .c
+
+%.o: %.c
+       @echo "  CC $<"; \
+       $(CC) $(CFLAGS) -c -o $@ $<
+
+.deps/%.d: %.c
+       @echo "  DEP $<"; \
+       $(CC) $(DEPFLAGS) $< > $@.tmp; \
+       sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.tmp > $@; \
+       rm -f $@.tmp
+
+Makefile.opts:
+       @echo "***"
+       @echo "*** No configuration found, please run ./configure"
+       @echo "***"
+       @exit 1
+
+ifneq ($(MAKECMDGOALS),clean)
+ifneq ($(MAKECMDGOALS),distclean)
+ifneq ($(DEPS),)
+-include $(DEPS)
+endif
+endif
+endif
diff --git a/aclocal.m4 b/aclocal.m4
new file mode 100644 (file)
index 0000000..177c013
--- /dev/null
@@ -0,0 +1,831 @@
+dnl aclocal.m4 generated automatically by aclocal 1.4-p6
+
+dnl Copyright (C) 1994, 1995-8, 1999, 2001 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+dnl This program is distributed in the hope that it will be useful,
+dnl but WITHOUT ANY WARRANTY, to the extent permitted by law; without
+dnl even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+dnl PARTICULAR PURPOSE.
+
+# lib-prefix.m4 serial 4 (gettext-0.14.2)
+dnl Copyright (C) 2001-2005 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+dnl From Bruno Haible.
+
+dnl AC_LIB_ARG_WITH is synonymous to AC_ARG_WITH in autoconf-2.13, and
+dnl similar to AC_ARG_WITH in autoconf 2.52...2.57 except that is doesn't
+dnl require excessive bracketing.
+ifdef([AC_HELP_STRING],
+[AC_DEFUN([AC_LIB_ARG_WITH], [AC_ARG_WITH([$1],[[$2]],[$3],[$4])])],
+[AC_DEFUN([AC_][LIB_ARG_WITH], [AC_ARG_WITH([$1],[$2],[$3],[$4])])])
+
+dnl AC_LIB_PREFIX adds to the CPPFLAGS and LDFLAGS the flags that are needed
+dnl to access previously installed libraries. The basic assumption is that
+dnl a user will want packages to use other packages he previously installed
+dnl with the same --prefix option.
+dnl This macro is not needed if only AC_LIB_LINKFLAGS is used to locate
+dnl libraries, but is otherwise very convenient.
+AC_DEFUN([AC_LIB_PREFIX],
+[
+  AC_BEFORE([$0], [AC_LIB_LINKFLAGS])
+  AC_REQUIRE([AC_PROG_CC])
+  AC_REQUIRE([AC_CANONICAL_HOST])
+  AC_REQUIRE([AC_LIB_PREPARE_PREFIX])
+  dnl By default, look in $includedir and $libdir.
+  use_additional=yes
+  AC_LIB_WITH_FINAL_PREFIX([
+    eval additional_includedir=\"$includedir\"
+    eval additional_libdir=\"$libdir\"
+  ])
+  AC_LIB_ARG_WITH([lib-prefix],
+[  --with-lib-prefix[=DIR] search for libraries in DIR/include and DIR/lib
+  --without-lib-prefix    don't search for libraries in includedir and libdir],
+[
+    if test "X$withval" = "Xno"; then
+      use_additional=no
+    else
+      if test "X$withval" = "X"; then
+        AC_LIB_WITH_FINAL_PREFIX([
+          eval additional_includedir=\"$includedir\"
+          eval additional_libdir=\"$libdir\"
+        ])
+      else
+        additional_includedir="$withval/include"
+        additional_libdir="$withval/lib"
+      fi
+    fi
+])
+  if test $use_additional = yes; then
+    dnl Potentially add $additional_includedir to $CPPFLAGS.
+    dnl But don't add it
+    dnl   1. if it's the standard /usr/include,
+    dnl   2. if it's already present in $CPPFLAGS,
+    dnl   3. if it's /usr/local/include and we are using GCC on Linux,
+    dnl   4. if it doesn't exist as a directory.
+    if test "X$additional_includedir" != "X/usr/include"; then
+      haveit=
+      for x in $CPPFLAGS; do
+        AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"])
+        if test "X$x" = "X-I$additional_includedir"; then
+          haveit=yes
+          break
+        fi
+      done
+      if test -z "$haveit"; then
+        if test "X$additional_includedir" = "X/usr/local/include"; then
+          if test -n "$GCC"; then
+            case $host_os in
+              linux* | gnu* | k*bsd*-gnu) haveit=yes;;
+            esac
+          fi
+        fi
+        if test -z "$haveit"; then
+          if test -d "$additional_includedir"; then
+            dnl Really add $additional_includedir to $CPPFLAGS.
+            CPPFLAGS="${CPPFLAGS}${CPPFLAGS:+ }-I$additional_includedir"
+          fi
+        fi
+      fi
+    fi
+    dnl Potentially add $additional_libdir to $LDFLAGS.
+    dnl But don't add it
+    dnl   1. if it's the standard /usr/lib,
+    dnl   2. if it's already present in $LDFLAGS,
+    dnl   3. if it's /usr/local/lib and we are using GCC on Linux,
+    dnl   4. if it doesn't exist as a directory.
+    if test "X$additional_libdir" != "X/usr/lib"; then
+      haveit=
+      for x in $LDFLAGS; do
+        AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"])
+        if test "X$x" = "X-L$additional_libdir"; then
+          haveit=yes
+          break
+        fi
+      done
+      if test -z "$haveit"; then
+        if test "X$additional_libdir" = "X/usr/local/lib"; then
+          if test -n "$GCC"; then
+            case $host_os in
+              linux*) haveit=yes;;
+            esac
+          fi
+        fi
+        if test -z "$haveit"; then
+          if test -d "$additional_libdir"; then
+            dnl Really add $additional_libdir to $LDFLAGS.
+            LDFLAGS="${LDFLAGS}${LDFLAGS:+ }-L$additional_libdir"
+          fi
+        fi
+      fi
+    fi
+  fi
+])
+
+dnl AC_LIB_PREPARE_PREFIX creates variables acl_final_prefix,
+dnl acl_final_exec_prefix, containing the values to which $prefix and
+dnl $exec_prefix will expand at the end of the configure script.
+AC_DEFUN([AC_LIB_PREPARE_PREFIX],
+[
+  dnl Unfortunately, prefix and exec_prefix get only finally determined
+  dnl at the end of configure.
+  if test "X$prefix" = "XNONE"; then
+    acl_final_prefix="$ac_default_prefix"
+  else
+    acl_final_prefix="$prefix"
+  fi
+  if test "X$exec_prefix" = "XNONE"; then
+    acl_final_exec_prefix='${prefix}'
+  else
+    acl_final_exec_prefix="$exec_prefix"
+  fi
+  acl_save_prefix="$prefix"
+  prefix="$acl_final_prefix"
+  eval acl_final_exec_prefix=\"$acl_final_exec_prefix\"
+  prefix="$acl_save_prefix"
+])
+
+dnl AC_LIB_WITH_FINAL_PREFIX([statement]) evaluates statement, with the
+dnl variables prefix and exec_prefix bound to the values they will have
+dnl at the end of the configure script.
+AC_DEFUN([AC_LIB_WITH_FINAL_PREFIX],
+[
+  acl_save_prefix="$prefix"
+  prefix="$acl_final_prefix"
+  acl_save_exec_prefix="$exec_prefix"
+  exec_prefix="$acl_final_exec_prefix"
+  $1
+  exec_prefix="$acl_save_exec_prefix"
+  prefix="$acl_save_prefix"
+])
+
+# lib-link.m4 serial 6 (gettext-0.14.3)
+dnl Copyright (C) 2001-2005 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+dnl From Bruno Haible.
+
+AC_PREREQ(2.50)
+
+dnl AC_LIB_LINKFLAGS(name [, dependencies]) searches for libname and
+dnl the libraries corresponding to explicit and implicit dependencies.
+dnl Sets and AC_SUBSTs the LIB${NAME} and LTLIB${NAME} variables and
+dnl augments the CPPFLAGS variable.
+AC_DEFUN([AC_LIB_LINKFLAGS],
+[
+  AC_REQUIRE([AC_LIB_PREPARE_PREFIX])
+  AC_REQUIRE([AC_LIB_RPATH])
+  define([Name],[translit([$1],[./-], [___])])
+  define([NAME],[translit([$1],[abcdefghijklmnopqrstuvwxyz./-],
+                               [ABCDEFGHIJKLMNOPQRSTUVWXYZ___])])
+  AC_CACHE_CHECK([how to link with lib[]$1], [ac_cv_lib[]Name[]_libs], [
+    AC_LIB_LINKFLAGS_BODY([$1], [$2])
+    ac_cv_lib[]Name[]_libs="$LIB[]NAME"
+    ac_cv_lib[]Name[]_ltlibs="$LTLIB[]NAME"
+    ac_cv_lib[]Name[]_cppflags="$INC[]NAME"
+  ])
+  LIB[]NAME="$ac_cv_lib[]Name[]_libs"
+  LTLIB[]NAME="$ac_cv_lib[]Name[]_ltlibs"
+  INC[]NAME="$ac_cv_lib[]Name[]_cppflags"
+  AC_LIB_APPENDTOVAR([CPPFLAGS], [$INC]NAME)
+  AC_SUBST([LIB]NAME)
+  AC_SUBST([LTLIB]NAME)
+  dnl Also set HAVE_LIB[]NAME so that AC_LIB_HAVE_LINKFLAGS can reuse the
+  dnl results of this search when this library appears as a dependency.
+  HAVE_LIB[]NAME=yes
+  undefine([Name])
+  undefine([NAME])
+])
+
+dnl AC_LIB_HAVE_LINKFLAGS(name, dependencies, includes, testcode)
+dnl searches for libname and the libraries corresponding to explicit and
+dnl implicit dependencies, together with the specified include files and
+dnl the ability to compile and link the specified testcode. If found, it
+dnl sets and AC_SUBSTs HAVE_LIB${NAME}=yes and the LIB${NAME} and
+dnl LTLIB${NAME} variables and augments the CPPFLAGS variable, and
+dnl #defines HAVE_LIB${NAME} to 1. Otherwise, it sets and AC_SUBSTs
+dnl HAVE_LIB${NAME}=no and LIB${NAME} and LTLIB${NAME} to empty.
+AC_DEFUN([AC_LIB_HAVE_LINKFLAGS],
+[
+  AC_REQUIRE([AC_LIB_PREPARE_PREFIX])
+  AC_REQUIRE([AC_LIB_RPATH])
+  define([Name],[translit([$1],[./-], [___])])
+  define([NAME],[translit([$1],[abcdefghijklmnopqrstuvwxyz./-],
+                               [ABCDEFGHIJKLMNOPQRSTUVWXYZ___])])
+
+  dnl Search for lib[]Name and define LIB[]NAME, LTLIB[]NAME and INC[]NAME
+  dnl accordingly.
+  AC_LIB_LINKFLAGS_BODY([$1], [$2])
+
+  dnl Add $INC[]NAME to CPPFLAGS before performing the following checks,
+  dnl because if the user has installed lib[]Name and not disabled its use
+  dnl via --without-lib[]Name-prefix, he wants to use it.
+  ac_save_CPPFLAGS="$CPPFLAGS"
+  AC_LIB_APPENDTOVAR([CPPFLAGS], [$INC]NAME)
+
+  AC_CACHE_CHECK([for lib[]$1], [ac_cv_lib[]Name], [
+    ac_save_LIBS="$LIBS"
+    LIBS="$LIBS $LIB[]NAME"
+    AC_TRY_LINK([$3], [$4], [ac_cv_lib[]Name=yes], [ac_cv_lib[]Name=no])
+    LIBS="$ac_save_LIBS"
+  ])
+  if test "$ac_cv_lib[]Name" = yes; then
+    HAVE_LIB[]NAME=yes
+    AC_DEFINE([HAVE_LIB]NAME, 1, [Define if you have the $1 library.])
+    AC_MSG_CHECKING([how to link with lib[]$1])
+    AC_MSG_RESULT([$LIB[]NAME])
+  else
+    HAVE_LIB[]NAME=no
+    dnl If $LIB[]NAME didn't lead to a usable library, we don't need
+    dnl $INC[]NAME either.
+    CPPFLAGS="$ac_save_CPPFLAGS"
+    LIB[]NAME=
+    LTLIB[]NAME=
+  fi
+  AC_SUBST([HAVE_LIB]NAME)
+  AC_SUBST([LIB]NAME)
+  AC_SUBST([LTLIB]NAME)
+  undefine([Name])
+  undefine([NAME])
+])
+
+dnl Determine the platform dependent parameters needed to use rpath:
+dnl libext, shlibext, hardcode_libdir_flag_spec, hardcode_libdir_separator,
+dnl hardcode_direct, hardcode_minus_L.
+AC_DEFUN([AC_LIB_RPATH],
+[
+  dnl Tell automake >= 1.10 to complain if config.rpath is missing.
+  m4_ifdef([AC_REQUIRE_AUX_FILE], [AC_REQUIRE_AUX_FILE([config.rpath])])
+  AC_REQUIRE([AC_PROG_CC])                dnl we use $CC, $GCC, $LDFLAGS
+  AC_REQUIRE([AC_LIB_PROG_LD])            dnl we use $LD, $with_gnu_ld
+  AC_REQUIRE([AC_CANONICAL_HOST])         dnl we use $host
+  AC_REQUIRE([AC_CONFIG_AUX_DIR_DEFAULT]) dnl we use $ac_aux_dir
+  AC_CACHE_CHECK([for shared library run path origin], acl_cv_rpath, [
+    CC="$CC" GCC="$GCC" LDFLAGS="$LDFLAGS" LD="$LD" with_gnu_ld="$with_gnu_ld" \
+    ${CONFIG_SHELL-/bin/sh} "$ac_aux_dir/config.rpath" "$host" > conftest.sh
+    . ./conftest.sh
+    rm -f ./conftest.sh
+    acl_cv_rpath=done
+  ])
+  wl="$acl_cv_wl"
+  libext="$acl_cv_libext"
+  shlibext="$acl_cv_shlibext"
+  hardcode_libdir_flag_spec="$acl_cv_hardcode_libdir_flag_spec"
+  hardcode_libdir_separator="$acl_cv_hardcode_libdir_separator"
+  hardcode_direct="$acl_cv_hardcode_direct"
+  hardcode_minus_L="$acl_cv_hardcode_minus_L"
+  dnl Determine whether the user wants rpath handling at all.
+  AC_ARG_ENABLE(rpath,
+    [  --disable-rpath         do not hardcode runtime library paths],
+    :, enable_rpath=yes)
+])
+
+dnl AC_LIB_LINKFLAGS_BODY(name [, dependencies]) searches for libname and
+dnl the libraries corresponding to explicit and implicit dependencies.
+dnl Sets the LIB${NAME}, LTLIB${NAME} and INC${NAME} variables.
+AC_DEFUN([AC_LIB_LINKFLAGS_BODY],
+[
+  define([NAME],[translit([$1],[abcdefghijklmnopqrstuvwxyz./-],
+                               [ABCDEFGHIJKLMNOPQRSTUVWXYZ___])])
+  dnl By default, look in $includedir and $libdir.
+  use_additional=yes
+  AC_LIB_WITH_FINAL_PREFIX([
+    eval additional_includedir=\"$includedir\"
+    eval additional_libdir=\"$libdir\"
+  ])
+  AC_LIB_ARG_WITH([lib$1-prefix],
+[  --with-lib$1-prefix[=DIR]  search for lib$1 in DIR/include and DIR/lib
+  --without-lib$1-prefix     don't search for lib$1 in includedir and libdir],
+[
+    if test "X$withval" = "Xno"; then
+      use_additional=no
+    else
+      if test "X$withval" = "X"; then
+        AC_LIB_WITH_FINAL_PREFIX([
+          eval additional_includedir=\"$includedir\"
+          eval additional_libdir=\"$libdir\"
+        ])
+      else
+        additional_includedir="$withval/include"
+        additional_libdir="$withval/lib"
+      fi
+    fi
+])
+  dnl Search the library and its dependencies in $additional_libdir and
+  dnl $LDFLAGS. Using breadth-first-seach.
+  LIB[]NAME=
+  LTLIB[]NAME=
+  INC[]NAME=
+  rpathdirs=
+  ltrpathdirs=
+  names_already_handled=
+  names_next_round='$1 $2'
+  while test -n "$names_next_round"; do
+    names_this_round="$names_next_round"
+    names_next_round=
+    for name in $names_this_round; do
+      already_handled=
+      for n in $names_already_handled; do
+        if test "$n" = "$name"; then
+          already_handled=yes
+          break
+        fi
+      done
+      if test -z "$already_handled"; then
+        names_already_handled="$names_already_handled $name"
+        dnl See if it was already located by an earlier AC_LIB_LINKFLAGS
+        dnl or AC_LIB_HAVE_LINKFLAGS call.
+        uppername=`echo "$name" | sed -e 'y|abcdefghijklmnopqrstuvwxyz./-|ABCDEFGHIJKLMNOPQRSTUVWXYZ___|'`
+        eval value=\"\$HAVE_LIB$uppername\"
+        if test -n "$value"; then
+          if test "$value" = yes; then
+            eval value=\"\$LIB$uppername\"
+            test -z "$value" || LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$value"
+            eval value=\"\$LTLIB$uppername\"
+            test -z "$value" || LTLIB[]NAME="${LTLIB[]NAME}${LTLIB[]NAME:+ }$value"
+          else
+            dnl An earlier call to AC_LIB_HAVE_LINKFLAGS has determined
+            dnl that this library doesn't exist. So just drop it.
+            :
+          fi
+        else
+          dnl Search the library lib$name in $additional_libdir and $LDFLAGS
+          dnl and the already constructed $LIBNAME/$LTLIBNAME.
+          found_dir=
+          found_la=
+          found_so=
+          found_a=
+          if test $use_additional = yes; then
+            if test -n "$shlibext" && test -f "$additional_libdir/lib$name.$shlibext"; then
+              found_dir="$additional_libdir"
+              found_so="$additional_libdir/lib$name.$shlibext"
+              if test -f "$additional_libdir/lib$name.la"; then
+                found_la="$additional_libdir/lib$name.la"
+              fi
+            else
+              if test -f "$additional_libdir/lib$name.$libext"; then
+                found_dir="$additional_libdir"
+                found_a="$additional_libdir/lib$name.$libext"
+                if test -f "$additional_libdir/lib$name.la"; then
+                  found_la="$additional_libdir/lib$name.la"
+                fi
+              fi
+            fi
+          fi
+          if test "X$found_dir" = "X"; then
+            for x in $LDFLAGS $LTLIB[]NAME; do
+              AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"])
+              case "$x" in
+                -L*)
+                  dir=`echo "X$x" | sed -e 's/^X-L//'`
+                  if test -n "$shlibext" && test -f "$dir/lib$name.$shlibext"; then
+                    found_dir="$dir"
+                    found_so="$dir/lib$name.$shlibext"
+                    if test -f "$dir/lib$name.la"; then
+                      found_la="$dir/lib$name.la"
+                    fi
+                  else
+                    if test -f "$dir/lib$name.$libext"; then
+                      found_dir="$dir"
+                      found_a="$dir/lib$name.$libext"
+                      if test -f "$dir/lib$name.la"; then
+                        found_la="$dir/lib$name.la"
+                      fi
+                    fi
+                  fi
+                  ;;
+              esac
+              if test "X$found_dir" != "X"; then
+                break
+              fi
+            done
+          fi
+          if test "X$found_dir" != "X"; then
+            dnl Found the library.
+            LTLIB[]NAME="${LTLIB[]NAME}${LTLIB[]NAME:+ }-L$found_dir -l$name"
+            if test "X$found_so" != "X"; then
+              dnl Linking with a shared library. We attempt to hardcode its
+              dnl directory into the executable's runpath, unless it's the
+              dnl standard /usr/lib.
+              if test "$enable_rpath" = no || test "X$found_dir" = "X/usr/lib"; then
+                dnl No hardcoding is needed.
+                LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$found_so"
+              else
+                dnl Use an explicit option to hardcode DIR into the resulting
+                dnl binary.
+                dnl Potentially add DIR to ltrpathdirs.
+                dnl The ltrpathdirs will be appended to $LTLIBNAME at the end.
+                haveit=
+                for x in $ltrpathdirs; do
+                  if test "X$x" = "X$found_dir"; then
+                    haveit=yes
+                    break
+                  fi
+                done
+                if test -z "$haveit"; then
+                  ltrpathdirs="$ltrpathdirs $found_dir"
+                fi
+                dnl The hardcoding into $LIBNAME is system dependent.
+                if test "$hardcode_direct" = yes; then
+                  dnl Using DIR/libNAME.so during linking hardcodes DIR into the
+                  dnl resulting binary.
+                  LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$found_so"
+                else
+                  if test -n "$hardcode_libdir_flag_spec" && test "$hardcode_minus_L" = no; then
+                    dnl Use an explicit option to hardcode DIR into the resulting
+                    dnl binary.
+                    LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$found_so"
+                    dnl Potentially add DIR to rpathdirs.
+                    dnl The rpathdirs will be appended to $LIBNAME at the end.
+                    haveit=
+                    for x in $rpathdirs; do
+                      if test "X$x" = "X$found_dir"; then
+                        haveit=yes
+                        break
+                      fi
+                    done
+                    if test -z "$haveit"; then
+                      rpathdirs="$rpathdirs $found_dir"
+                    fi
+                  else
+                    dnl Rely on "-L$found_dir".
+                    dnl But don't add it if it's already contained in the LDFLAGS
+                    dnl or the already constructed $LIBNAME
+                    haveit=
+                    for x in $LDFLAGS $LIB[]NAME; do
+                      AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"])
+                      if test "X$x" = "X-L$found_dir"; then
+                        haveit=yes
+                        break
+                      fi
+                    done
+                    if test -z "$haveit"; then
+                      LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }-L$found_dir"
+                    fi
+                    if test "$hardcode_minus_L" != no; then
+                      dnl FIXME: Not sure whether we should use
+                      dnl "-L$found_dir -l$name" or "-L$found_dir $found_so"
+                      dnl here.
+                      LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$found_so"
+                    else
+                      dnl We cannot use $hardcode_runpath_var and LD_RUN_PATH
+                      dnl here, because this doesn't fit in flags passed to the
+                      dnl compiler. So give up. No hardcoding. This affects only
+                      dnl very old systems.
+                      dnl FIXME: Not sure whether we should use
+                      dnl "-L$found_dir -l$name" or "-L$found_dir $found_so"
+                      dnl here.
+                      LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }-l$name"
+                    fi
+                  fi
+                fi
+              fi
+            else
+              if test "X$found_a" != "X"; then
+                dnl Linking with a static library.
+                LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$found_a"
+              else
+                dnl We shouldn't come here, but anyway it's good to have a
+                dnl fallback.
+                LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }-L$found_dir -l$name"
+              fi
+            fi
+            dnl Assume the include files are nearby.
+            additional_includedir=
+            case "$found_dir" in
+              */lib | */lib/)
+                basedir=`echo "X$found_dir" | sed -e 's,^X,,' -e 's,/lib/*$,,'`
+                additional_includedir="$basedir/include"
+                ;;
+            esac
+            if test "X$additional_includedir" != "X"; then
+              dnl Potentially add $additional_includedir to $INCNAME.
+              dnl But don't add it
+              dnl   1. if it's the standard /usr/include,
+              dnl   2. if it's /usr/local/include and we are using GCC on Linux,
+              dnl   3. if it's already present in $CPPFLAGS or the already
+              dnl      constructed $INCNAME,
+              dnl   4. if it doesn't exist as a directory.
+              if test "X$additional_includedir" != "X/usr/include"; then
+                haveit=
+                if test "X$additional_includedir" = "X/usr/local/include"; then
+                  if test -n "$GCC"; then
+                    case $host_os in
+                      linux* | gnu* | k*bsd*-gnu) haveit=yes;;
+                    esac
+                  fi
+                fi
+                if test -z "$haveit"; then
+                  for x in $CPPFLAGS $INC[]NAME; do
+                    AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"])
+                    if test "X$x" = "X-I$additional_includedir"; then
+                      haveit=yes
+                      break
+                    fi
+                  done
+                  if test -z "$haveit"; then
+                    if test -d "$additional_includedir"; then
+                      dnl Really add $additional_includedir to $INCNAME.
+                      INC[]NAME="${INC[]NAME}${INC[]NAME:+ }-I$additional_includedir"
+                    fi
+                  fi
+                fi
+              fi
+            fi
+            dnl Look for dependencies.
+            if test -n "$found_la"; then
+              dnl Read the .la file. It defines the variables
+              dnl dlname, library_names, old_library, dependency_libs, current,
+              dnl age, revision, installed, dlopen, dlpreopen, libdir.
+              save_libdir="$libdir"
+              case "$found_la" in
+                */* | *\\*) . "$found_la" ;;
+                *) . "./$found_la" ;;
+              esac
+              libdir="$save_libdir"
+              dnl We use only dependency_libs.
+              for dep in $dependency_libs; do
+                case "$dep" in
+                  -L*)
+                    additional_libdir=`echo "X$dep" | sed -e 's/^X-L//'`
+                    dnl Potentially add $additional_libdir to $LIBNAME and $LTLIBNAME.
+                    dnl But don't add it
+                    dnl   1. if it's the standard /usr/lib,
+                    dnl   2. if it's /usr/local/lib and we are using GCC on Linux,
+                    dnl   3. if it's already present in $LDFLAGS or the already
+                    dnl      constructed $LIBNAME,
+                    dnl   4. if it doesn't exist as a directory.
+                    if test "X$additional_libdir" != "X/usr/lib"; then
+                      haveit=
+                      if test "X$additional_libdir" = "X/usr/local/lib"; then
+                        if test -n "$GCC"; then
+                          case $host_os in
+                            linux* | gnu* | k*bsd*-gnu) haveit=yes;;
+                          esac
+                        fi
+                      fi
+                      if test -z "$haveit"; then
+                        haveit=
+                        for x in $LDFLAGS $LIB[]NAME; do
+                          AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"])
+                          if test "X$x" = "X-L$additional_libdir"; then
+                            haveit=yes
+                            break
+                          fi
+                        done
+                        if test -z "$haveit"; then
+                          if test -d "$additional_libdir"; then
+                            dnl Really add $additional_libdir to $LIBNAME.
+                            LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }-L$additional_libdir"
+                          fi
+                        fi
+                        haveit=
+                        for x in $LDFLAGS $LTLIB[]NAME; do
+                          AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"])
+                          if test "X$x" = "X-L$additional_libdir"; then
+                            haveit=yes
+                            break
+                          fi
+                        done
+                        if test -z "$haveit"; then
+                          if test -d "$additional_libdir"; then
+                            dnl Really add $additional_libdir to $LTLIBNAME.
+                            LTLIB[]NAME="${LTLIB[]NAME}${LTLIB[]NAME:+ }-L$additional_libdir"
+                          fi
+                        fi
+                      fi
+                    fi
+                    ;;
+                  -R*)
+                    dir=`echo "X$dep" | sed -e 's/^X-R//'`
+                    if test "$enable_rpath" != no; then
+                      dnl Potentially add DIR to rpathdirs.
+                      dnl The rpathdirs will be appended to $LIBNAME at the end.
+                      haveit=
+                      for x in $rpathdirs; do
+                        if test "X$x" = "X$dir"; then
+                          haveit=yes
+                          break
+                        fi
+                      done
+                      if test -z "$haveit"; then
+                        rpathdirs="$rpathdirs $dir"
+                      fi
+                      dnl Potentially add DIR to ltrpathdirs.
+                      dnl The ltrpathdirs will be appended to $LTLIBNAME at the end.
+                      haveit=
+                      for x in $ltrpathdirs; do
+                        if test "X$x" = "X$dir"; then
+                          haveit=yes
+                          break
+                        fi
+                      done
+                      if test -z "$haveit"; then
+                        ltrpathdirs="$ltrpathdirs $dir"
+                      fi
+                    fi
+                    ;;
+                  -l*)
+                    dnl Handle this in the next round.
+                    names_next_round="$names_next_round "`echo "X$dep" | sed -e 's/^X-l//'`
+                    ;;
+                  *.la)
+                    dnl Handle this in the next round. Throw away the .la's
+                    dnl directory; it is already contained in a preceding -L
+                    dnl option.
+                    names_next_round="$names_next_round "`echo "X$dep" | sed -e 's,^X.*/,,' -e 's,^lib,,' -e 's,\.la$,,'`
+                    ;;
+                  *)
+                    dnl Most likely an immediate library name.
+                    LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$dep"
+                    LTLIB[]NAME="${LTLIB[]NAME}${LTLIB[]NAME:+ }$dep"
+                    ;;
+                esac
+              done
+            fi
+          else
+            dnl Didn't find the library; assume it is in the system directories
+            dnl known to the linker and runtime loader. (All the system
+            dnl directories known to the linker should also be known to the
+            dnl runtime loader, otherwise the system is severely misconfigured.)
+            LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }-l$name"
+            LTLIB[]NAME="${LTLIB[]NAME}${LTLIB[]NAME:+ }-l$name"
+          fi
+        fi
+      fi
+    done
+  done
+  if test "X$rpathdirs" != "X"; then
+    if test -n "$hardcode_libdir_separator"; then
+      dnl Weird platform: only the last -rpath option counts, the user must
+      dnl pass all path elements in one option. We can arrange that for a
+      dnl single library, but not when more than one $LIBNAMEs are used.
+      alldirs=
+      for found_dir in $rpathdirs; do
+        alldirs="${alldirs}${alldirs:+$hardcode_libdir_separator}$found_dir"
+      done
+      dnl Note: hardcode_libdir_flag_spec uses $libdir and $wl.
+      acl_save_libdir="$libdir"
+      libdir="$alldirs"
+      eval flag=\"$hardcode_libdir_flag_spec\"
+      libdir="$acl_save_libdir"
+      LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$flag"
+    else
+      dnl The -rpath options are cumulative.
+      for found_dir in $rpathdirs; do
+        acl_save_libdir="$libdir"
+        libdir="$found_dir"
+        eval flag=\"$hardcode_libdir_flag_spec\"
+        libdir="$acl_save_libdir"
+        LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$flag"
+      done
+    fi
+  fi
+  if test "X$ltrpathdirs" != "X"; then
+    dnl When using libtool, the option that works for both libraries and
+    dnl executables is -R. The -R options are cumulative.
+    for found_dir in $ltrpathdirs; do
+      LTLIB[]NAME="${LTLIB[]NAME}${LTLIB[]NAME:+ }-R$found_dir"
+    done
+  fi
+])
+
+dnl AC_LIB_APPENDTOVAR(VAR, CONTENTS) appends the elements of CONTENTS to VAR,
+dnl unless already present in VAR.
+dnl Works only for CPPFLAGS, not for LIB* variables because that sometimes
+dnl contains two or three consecutive elements that belong together.
+AC_DEFUN([AC_LIB_APPENDTOVAR],
+[
+  for element in [$2]; do
+    haveit=
+    for x in $[$1]; do
+      AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"])
+      if test "X$x" = "X$element"; then
+        haveit=yes
+        break
+      fi
+    done
+    if test -z "$haveit"; then
+      [$1]="${[$1]}${[$1]:+ }$element"
+    fi
+  done
+])
+
+# lib-ld.m4 serial 3 (gettext-0.13)
+dnl Copyright (C) 1996-2003 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+dnl Subroutines of libtool.m4,
+dnl with replacements s/AC_/AC_LIB/ and s/lt_cv/acl_cv/ to avoid collision
+dnl with libtool.m4.
+
+dnl From libtool-1.4. Sets the variable with_gnu_ld to yes or no.
+AC_DEFUN([AC_LIB_PROG_LD_GNU],
+[AC_CACHE_CHECK([if the linker ($LD) is GNU ld], acl_cv_prog_gnu_ld,
+[# I'd rather use --version here, but apparently some GNU ld's only accept -v.
+case `$LD -v 2>&1 </dev/null` in
+*GNU* | *'with BFD'*)
+  acl_cv_prog_gnu_ld=yes ;;
+*)
+  acl_cv_prog_gnu_ld=no ;;
+esac])
+with_gnu_ld=$acl_cv_prog_gnu_ld
+])
+
+dnl From libtool-1.4. Sets the variable LD.
+AC_DEFUN([AC_LIB_PROG_LD],
+[AC_ARG_WITH(gnu-ld,
+[  --with-gnu-ld           assume the C compiler uses GNU ld [default=no]],
+test "$withval" = no || with_gnu_ld=yes, with_gnu_ld=no)
+AC_REQUIRE([AC_PROG_CC])dnl
+AC_REQUIRE([AC_CANONICAL_HOST])dnl
+# Prepare PATH_SEPARATOR.
+# The user is always right.
+if test "${PATH_SEPARATOR+set}" != set; then
+  echo "#! /bin/sh" >conf$$.sh
+  echo  "exit 0"   >>conf$$.sh
+  chmod +x conf$$.sh
+  if (PATH="/nonexistent;."; conf$$.sh) >/dev/null 2>&1; then
+    PATH_SEPARATOR=';'
+  else
+    PATH_SEPARATOR=:
+  fi
+  rm -f conf$$.sh
+fi
+ac_prog=ld
+if test "$GCC" = yes; then
+  # Check if gcc -print-prog-name=ld gives a path.
+  AC_MSG_CHECKING([for ld used by GCC])
+  case $host in
+  *-*-mingw*)
+    # gcc leaves a trailing carriage return which upsets mingw
+    ac_prog=`($CC -print-prog-name=ld) 2>&5 | tr -d '\015'` ;;
+  *)
+    ac_prog=`($CC -print-prog-name=ld) 2>&5` ;;
+  esac
+  case $ac_prog in
+    # Accept absolute paths.
+    [[\\/]* | [A-Za-z]:[\\/]*)]
+      [re_direlt='/[^/][^/]*/\.\./']
+      # Canonicalize the path of ld
+      ac_prog=`echo $ac_prog| sed 's%\\\\%/%g'`
+      while echo $ac_prog | grep "$re_direlt" > /dev/null 2>&1; do
+       ac_prog=`echo $ac_prog| sed "s%$re_direlt%/%"`
+      done
+      test -z "$LD" && LD="$ac_prog"
+      ;;
+  "")
+    # If it fails, then pretend we aren't using GCC.
+    ac_prog=ld
+    ;;
+  *)
+    # If it is relative, then search for the first ld in PATH.
+    with_gnu_ld=unknown
+    ;;
+  esac
+elif test "$with_gnu_ld" = yes; then
+  AC_MSG_CHECKING([for GNU ld])
+else
+  AC_MSG_CHECKING([for non-GNU ld])
+fi
+AC_CACHE_VAL(acl_cv_path_LD,
+[if test -z "$LD"; then
+  IFS="${IFS=  }"; ac_save_ifs="$IFS"; IFS="${IFS}${PATH_SEPARATOR-:}"
+  for ac_dir in $PATH; do
+    test -z "$ac_dir" && ac_dir=.
+    if test -f "$ac_dir/$ac_prog" || test -f "$ac_dir/$ac_prog$ac_exeext"; then
+      acl_cv_path_LD="$ac_dir/$ac_prog"
+      # Check to see if the program is GNU ld.  I'd rather use --version,
+      # but apparently some GNU ld's only accept -v.
+      # Break only if it was the GNU/non-GNU ld that we prefer.
+      case `"$acl_cv_path_LD" -v 2>&1 < /dev/null` in
+      *GNU* | *'with BFD'*)
+       test "$with_gnu_ld" != no && break ;;
+      *)
+       test "$with_gnu_ld" != yes && break ;;
+      esac
+    fi
+  done
+  IFS="$ac_save_ifs"
+else
+  acl_cv_path_LD="$LD" # Let the user override the test with a path.
+fi])
+LD="$acl_cv_path_LD"
+if test -n "$LD"; then
+  AC_MSG_RESULT($LD)
+else
+  AC_MSG_RESULT(no)
+fi
+test -z "$LD" && AC_MSG_ERROR([no acceptable ld found in \$PATH])
+AC_LIB_PROG_LD_GNU
+])
+
diff --git a/configure b/configure
new file mode 100755 (executable)
index 0000000..f76b7b4
--- /dev/null
+++ b/configure
@@ -0,0 +1,3724 @@
+#! /bin/sh
+# Guess values for system-dependent variables and create Makefiles.
+# Generated by GNU Autoconf 2.59 for libnl 1.0-pre6.
+#
+# Report bugs to <tgraf@suug.ch>.
+#
+# Copyright (C) 2003 Free Software Foundation, Inc.
+# This configure script is free software; the Free Software Foundation
+# gives unlimited permission to copy, distribute and modify it.
+## --------------------- ##
+## M4sh Initialization.  ##
+## --------------------- ##
+
+# Be Bourne compatible
+if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then
+  emulate sh
+  NULLCMD=:
+  # Zsh 3.x and 4.x performs word splitting on ${1+"$@"}, which
+  # is contrary to our usage.  Disable this feature.
+  alias -g '${1+"$@"}'='"$@"'
+elif test -n "${BASH_VERSION+set}" && (set -o posix) >/dev/null 2>&1; then
+  set -o posix
+fi
+DUALCASE=1; export DUALCASE # for MKS sh
+
+# Support unset when possible.
+if ( (MAIL=60; unset MAIL) || exit) >/dev/null 2>&1; then
+  as_unset=unset
+else
+  as_unset=false
+fi
+
+
+# Work around bugs in pre-3.0 UWIN ksh.
+$as_unset ENV MAIL MAILPATH
+PS1='$ '
+PS2='> '
+PS4='+ '
+
+# NLS nuisances.
+for as_var in \
+  LANG LANGUAGE LC_ADDRESS LC_ALL LC_COLLATE LC_CTYPE LC_IDENTIFICATION \
+  LC_MEASUREMENT LC_MESSAGES LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER \
+  LC_TELEPHONE LC_TIME
+do
+  if (set +x; test -z "`(eval $as_var=C; export $as_var) 2>&1`"); then
+    eval $as_var=C; export $as_var
+  else
+    $as_unset $as_var
+  fi
+done
+
+# Required to use basename.
+if expr a : '\(a\)' >/dev/null 2>&1; then
+  as_expr=expr
+else
+  as_expr=false
+fi
+
+if (basename /) >/dev/null 2>&1 && test "X`basename / 2>&1`" = "X/"; then
+  as_basename=basename
+else
+  as_basename=false
+fi
+
+
+# Name of the executable.
+as_me=`$as_basename "$0" ||
+$as_expr X/"$0" : '.*/\([^/][^/]*\)/*$' \| \
+        X"$0" : 'X\(//\)$' \| \
+        X"$0" : 'X\(/\)$' \| \
+        .     : '\(.\)' 2>/dev/null ||
+echo X/"$0" |
+    sed '/^.*\/\([^/][^/]*\)\/*$/{ s//\1/; q; }
+         /^X\/\(\/\/\)$/{ s//\1/; q; }
+         /^X\/\(\/\).*/{ s//\1/; q; }
+         s/.*/./; q'`
+
+
+# PATH needs CR, and LINENO needs CR and PATH.
+# Avoid depending upon Character Ranges.
+as_cr_letters='abcdefghijklmnopqrstuvwxyz'
+as_cr_LETTERS='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
+as_cr_Letters=$as_cr_letters$as_cr_LETTERS
+as_cr_digits='0123456789'
+as_cr_alnum=$as_cr_Letters$as_cr_digits
+
+# The user is always right.
+if test "${PATH_SEPARATOR+set}" != set; then
+  echo "#! /bin/sh" >conf$$.sh
+  echo  "exit 0"   >>conf$$.sh
+  chmod +x conf$$.sh
+  if (PATH="/nonexistent;."; conf$$.sh) >/dev/null 2>&1; then
+    PATH_SEPARATOR=';'
+  else
+    PATH_SEPARATOR=:
+  fi
+  rm -f conf$$.sh
+fi
+
+
+  as_lineno_1=$LINENO
+  as_lineno_2=$LINENO
+  as_lineno_3=`(expr $as_lineno_1 + 1) 2>/dev/null`
+  test "x$as_lineno_1" != "x$as_lineno_2" &&
+  test "x$as_lineno_3"  = "x$as_lineno_2"  || {
+  # Find who we are.  Look in the path if we contain no path at all
+  # relative or not.
+  case $0 in
+    *[\\/]* ) as_myself=$0 ;;
+    *) as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
+for as_dir in $PATH
+do
+  IFS=$as_save_IFS
+  test -z "$as_dir" && as_dir=.
+  test -r "$as_dir/$0" && as_myself=$as_dir/$0 && break
+done
+
+       ;;
+  esac
+  # We did not find ourselves, most probably we were run as `sh COMMAND'
+  # in which case we are not to be found in the path.
+  if test "x$as_myself" = x; then
+    as_myself=$0
+  fi
+  if test ! -f "$as_myself"; then
+    { echo "$as_me: error: cannot find myself; rerun with an absolute path" >&2
+   { (exit 1); exit 1; }; }
+  fi
+  case $CONFIG_SHELL in
+  '')
+    as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
+for as_dir in /bin$PATH_SEPARATOR/usr/bin$PATH_SEPARATOR$PATH
+do
+  IFS=$as_save_IFS
+  test -z "$as_dir" && as_dir=.
+  for as_base in sh bash ksh sh5; do
+        case $as_dir in
+        /*)
+          if ("$as_dir/$as_base" -c '
+  as_lineno_1=$LINENO
+  as_lineno_2=$LINENO
+  as_lineno_3=`(expr $as_lineno_1 + 1) 2>/dev/null`
+  test "x$as_lineno_1" != "x$as_lineno_2" &&
+  test "x$as_lineno_3"  = "x$as_lineno_2" ') 2>/dev/null; then
+            $as_unset BASH_ENV || test "${BASH_ENV+set}" != set || { BASH_ENV=; export BASH_ENV; }
+            $as_unset ENV || test "${ENV+set}" != set || { ENV=; export ENV; }
+            CONFIG_SHELL=$as_dir/$as_base
+            export CONFIG_SHELL
+            exec "$CONFIG_SHELL" "$0" ${1+"$@"}
+          fi;;
+        esac
+       done
+done
+;;
+  esac
+
+  # Create $as_me.lineno as a copy of $as_myself, but with $LINENO
+  # uniformly replaced by the line number.  The first 'sed' inserts a
+  # line-number line before each line; the second 'sed' does the real
+  # work.  The second script uses 'N' to pair each line-number line
+  # with the numbered line, and appends trailing '-' during
+  # substitution so that $LINENO is not a special case at line end.
+  # (Raja R Harinath suggested sed '=', and Paul Eggert wrote the
+  # second 'sed' script.  Blame Lee E. McMahon for sed's syntax.  :-)
+  sed '=' <$as_myself |
+    sed '
+      N
+      s,$,-,
+      : loop
+      s,^\(['$as_cr_digits']*\)\(.*\)[$]LINENO\([^'$as_cr_alnum'_]\),\1\2\1\3,
+      t loop
+      s,-$,,
+      s,^['$as_cr_digits']*\n,,
+    ' >$as_me.lineno &&
+  chmod +x $as_me.lineno ||
+    { echo "$as_me: error: cannot create $as_me.lineno; rerun with a POSIX shell" >&2
+   { (exit 1); exit 1; }; }
+
+  # Don't try to exec as it changes $[0], causing all sort of problems
+  # (the dirname of $[0] is not the place where we might find the
+  # original and so on.  Autoconf is especially sensible to this).
+  . ./$as_me.lineno
+  # Exit status is that of the last command.
+  exit
+}
+
+
+case `echo "testing\c"; echo 1,2,3`,`echo -n testing; echo 1,2,3` in
+  *c*,-n*) ECHO_N= ECHO_C='
+' ECHO_T='     ' ;;
+  *c*,*  ) ECHO_N=-n ECHO_C= ECHO_T= ;;
+  *)       ECHO_N= ECHO_C='\c' ECHO_T= ;;
+esac
+
+if expr a : '\(a\)' >/dev/null 2>&1; then
+  as_expr=expr
+else
+  as_expr=false
+fi
+
+rm -f conf$$ conf$$.exe conf$$.file
+echo >conf$$.file
+if ln -s conf$$.file conf$$ 2>/dev/null; then
+  # We could just check for DJGPP; but this test a) works b) is more generic
+  # and c) will remain valid once DJGPP supports symlinks (DJGPP 2.04).
+  if test -f conf$$.exe; then
+    # Don't use ln at all; we don't have any links
+    as_ln_s='cp -p'
+  else
+    as_ln_s='ln -s'
+  fi
+elif ln conf$$.file conf$$ 2>/dev/null; then
+  as_ln_s=ln
+else
+  as_ln_s='cp -p'
+fi
+rm -f conf$$ conf$$.exe conf$$.file
+
+if mkdir -p . 2>/dev/null; then
+  as_mkdir_p=:
+else
+  test -d ./-p && rmdir ./-p
+  as_mkdir_p=false
+fi
+
+as_executable_p="test -f"
+
+# Sed expression to map a string onto a valid CPP name.
+as_tr_cpp="eval sed 'y%*$as_cr_letters%P$as_cr_LETTERS%;s%[^_$as_cr_alnum]%_%g'"
+
+# Sed expression to map a string onto a valid variable name.
+as_tr_sh="eval sed 'y%*+%pp%;s%[^_$as_cr_alnum]%_%g'"
+
+
+# IFS
+# We need space, tab and new line, in precisely that order.
+as_nl='
+'
+IFS="  $as_nl"
+
+# CDPATH.
+$as_unset CDPATH
+
+
+# Name of the host.
+# hostname on some systems (SVR3.2, Linux) returns a bogus exit status,
+# so uname gets run too.
+ac_hostname=`(hostname || uname -n) 2>/dev/null | sed 1q`
+
+exec 6>&1
+
+#
+# Initializations.
+#
+ac_default_prefix=/usr/local
+ac_config_libobj_dir=.
+cross_compiling=no
+subdirs=
+MFLAGS=
+MAKEFLAGS=
+SHELL=${CONFIG_SHELL-/bin/sh}
+
+# Maximum number of lines to put in a shell here document.
+# This variable seems obsolete.  It should probably be removed, and
+# only ac_max_sed_lines should be used.
+: ${ac_max_here_lines=38}
+
+# Identity of this package.
+PACKAGE_NAME='libnl'
+PACKAGE_TARNAME='libnl'
+PACKAGE_VERSION='1.0-pre6'
+PACKAGE_STRING='libnl 1.0-pre6'
+PACKAGE_BUGREPORT='tgraf@suug.ch'
+
+ac_subst_vars='SHELL PATH_SEPARATOR PACKAGE_NAME PACKAGE_TARNAME PACKAGE_VERSION PACKAGE_STRING PACKAGE_BUGREPORT exec_prefix prefix program_transform_name bindir sbindir libexecdir datadir sysconfdir sharedstatedir localstatedir libdir includedir oldincludedir infodir mandir build_alias host_alias target_alias DEFS ECHO_C ECHO_N ECHO_T LIBS CC CFLAGS LDFLAGS CPPFLAGS ac_ct_CC EXEEXT OBJEXT INSTALL_PROGRAM INSTALL_SCRIPT INSTALL_DATA LIBNL_LIB LIBOBJS LTLIBOBJS'
+ac_subst_files=''
+
+# Initialize some variables set by options.
+ac_init_help=
+ac_init_version=false
+# The variables have the same names as the options, with
+# dashes changed to underlines.
+cache_file=/dev/null
+exec_prefix=NONE
+no_create=
+no_recursion=
+prefix=NONE
+program_prefix=NONE
+program_suffix=NONE
+program_transform_name=s,x,x,
+silent=
+site=
+srcdir=
+verbose=
+x_includes=NONE
+x_libraries=NONE
+
+# Installation directory options.
+# These are left unexpanded so users can "make install exec_prefix=/foo"
+# and all the variables that are supposed to be based on exec_prefix
+# by default will actually change.
+# Use braces instead of parens because sh, perl, etc. also accept them.
+bindir='${exec_prefix}/bin'
+sbindir='${exec_prefix}/sbin'
+libexecdir='${exec_prefix}/libexec'
+datadir='${prefix}/share'
+sysconfdir='${prefix}/etc'
+sharedstatedir='${prefix}/com'
+localstatedir='${prefix}/var'
+libdir='${exec_prefix}/lib'
+includedir='${prefix}/include'
+oldincludedir='/usr/include'
+infodir='${prefix}/info'
+mandir='${prefix}/man'
+
+ac_prev=
+for ac_option
+do
+  # If the previous option needs an argument, assign it.
+  if test -n "$ac_prev"; then
+    eval "$ac_prev=\$ac_option"
+    ac_prev=
+    continue
+  fi
+
+  ac_optarg=`expr "x$ac_option" : 'x[^=]*=\(.*\)'`
+
+  # Accept the important Cygnus configure options, so we can diagnose typos.
+
+  case $ac_option in
+
+  -bindir | --bindir | --bindi | --bind | --bin | --bi)
+    ac_prev=bindir ;;
+  -bindir=* | --bindir=* | --bindi=* | --bind=* | --bin=* | --bi=*)
+    bindir=$ac_optarg ;;
+
+  -build | --build | --buil | --bui | --bu)
+    ac_prev=build_alias ;;
+  -build=* | --build=* | --buil=* | --bui=* | --bu=*)
+    build_alias=$ac_optarg ;;
+
+  -cache-file | --cache-file | --cache-fil | --cache-fi \
+  | --cache-f | --cache- | --cache | --cach | --cac | --ca | --c)
+    ac_prev=cache_file ;;
+  -cache-file=* | --cache-file=* | --cache-fil=* | --cache-fi=* \
+  | --cache-f=* | --cache-=* | --cache=* | --cach=* | --cac=* | --ca=* | --c=*)
+    cache_file=$ac_optarg ;;
+
+  --config-cache | -C)
+    cache_file=config.cache ;;
+
+  -datadir | --datadir | --datadi | --datad | --data | --dat | --da)
+    ac_prev=datadir ;;
+  -datadir=* | --datadir=* | --datadi=* | --datad=* | --data=* | --dat=* \
+  | --da=*)
+    datadir=$ac_optarg ;;
+
+  -disable-* | --disable-*)
+    ac_feature=`expr "x$ac_option" : 'x-*disable-\(.*\)'`
+    # Reject names that are not valid shell variable names.
+    expr "x$ac_feature" : ".*[^-_$as_cr_alnum]" >/dev/null &&
+      { echo "$as_me: error: invalid feature name: $ac_feature" >&2
+   { (exit 1); exit 1; }; }
+    ac_feature=`echo $ac_feature | sed 's/-/_/g'`
+    eval "enable_$ac_feature=no" ;;
+
+  -enable-* | --enable-*)
+    ac_feature=`expr "x$ac_option" : 'x-*enable-\([^=]*\)'`
+    # Reject names that are not valid shell variable names.
+    expr "x$ac_feature" : ".*[^-_$as_cr_alnum]" >/dev/null &&
+      { echo "$as_me: error: invalid feature name: $ac_feature" >&2
+   { (exit 1); exit 1; }; }
+    ac_feature=`echo $ac_feature | sed 's/-/_/g'`
+    case $ac_option in
+      *=*) ac_optarg=`echo "$ac_optarg" | sed "s/'/'\\\\\\\\''/g"`;;
+      *) ac_optarg=yes ;;
+    esac
+    eval "enable_$ac_feature='$ac_optarg'" ;;
+
+  -exec-prefix | --exec_prefix | --exec-prefix | --exec-prefi \
+  | --exec-pref | --exec-pre | --exec-pr | --exec-p | --exec- \
+  | --exec | --exe | --ex)
+    ac_prev=exec_prefix ;;
+  -exec-prefix=* | --exec_prefix=* | --exec-prefix=* | --exec-prefi=* \
+  | --exec-pref=* | --exec-pre=* | --exec-pr=* | --exec-p=* | --exec-=* \
+  | --exec=* | --exe=* | --ex=*)
+    exec_prefix=$ac_optarg ;;
+
+  -gas | --gas | --ga | --g)
+    # Obsolete; use --with-gas.
+    with_gas=yes ;;
+
+  -help | --help | --hel | --he | -h)
+    ac_init_help=long ;;
+  -help=r* | --help=r* | --hel=r* | --he=r* | -hr*)
+    ac_init_help=recursive ;;
+  -help=s* | --help=s* | --hel=s* | --he=s* | -hs*)
+    ac_init_help=short ;;
+
+  -host | --host | --hos | --ho)
+    ac_prev=host_alias ;;
+  -host=* | --host=* | --hos=* | --ho=*)
+    host_alias=$ac_optarg ;;
+
+  -includedir | --includedir | --includedi | --included | --include \
+  | --includ | --inclu | --incl | --inc)
+    ac_prev=includedir ;;
+  -includedir=* | --includedir=* | --includedi=* | --included=* | --include=* \
+  | --includ=* | --inclu=* | --incl=* | --inc=*)
+    includedir=$ac_optarg ;;
+
+  -infodir | --infodir | --infodi | --infod | --info | --inf)
+    ac_prev=infodir ;;
+  -infodir=* | --infodir=* | --infodi=* | --infod=* | --info=* | --inf=*)
+    infodir=$ac_optarg ;;
+
+  -libdir | --libdir | --libdi | --libd)
+    ac_prev=libdir ;;
+  -libdir=* | --libdir=* | --libdi=* | --libd=*)
+    libdir=$ac_optarg ;;
+
+  -libexecdir | --libexecdir | --libexecdi | --libexecd | --libexec \
+  | --libexe | --libex | --libe)
+    ac_prev=libexecdir ;;
+  -libexecdir=* | --libexecdir=* | --libexecdi=* | --libexecd=* | --libexec=* \
+  | --libexe=* | --libex=* | --libe=*)
+    libexecdir=$ac_optarg ;;
+
+  -localstatedir | --localstatedir | --localstatedi | --localstated \
+  | --localstate | --localstat | --localsta | --localst \
+  | --locals | --local | --loca | --loc | --lo)
+    ac_prev=localstatedir ;;
+  -localstatedir=* | --localstatedir=* | --localstatedi=* | --localstated=* \
+  | --localstate=* | --localstat=* | --localsta=* | --localst=* \
+  | --locals=* | --local=* | --loca=* | --loc=* | --lo=*)
+    localstatedir=$ac_optarg ;;
+
+  -mandir | --mandir | --mandi | --mand | --man | --ma | --m)
+    ac_prev=mandir ;;
+  -mandir=* | --mandir=* | --mandi=* | --mand=* | --man=* | --ma=* | --m=*)
+    mandir=$ac_optarg ;;
+
+  -nfp | --nfp | --nf)
+    # Obsolete; use --without-fp.
+    with_fp=no ;;
+
+  -no-create | --no-create | --no-creat | --no-crea | --no-cre \
+  | --no-cr | --no-c | -n)
+    no_create=yes ;;
+
+  -no-recursion | --no-recursion | --no-recursio | --no-recursi \
+  | --no-recurs | --no-recur | --no-recu | --no-rec | --no-re | --no-r)
+    no_recursion=yes ;;
+
+  -oldincludedir | --oldincludedir | --oldincludedi | --oldincluded \
+  | --oldinclude | --oldinclud | --oldinclu | --oldincl | --oldinc \
+  | --oldin | --oldi | --old | --ol | --o)
+    ac_prev=oldincludedir ;;
+  -oldincludedir=* | --oldincludedir=* | --oldincludedi=* | --oldincluded=* \
+  | --oldinclude=* | --oldinclud=* | --oldinclu=* | --oldincl=* | --oldinc=* \
+  | --oldin=* | --oldi=* | --old=* | --ol=* | --o=*)
+    oldincludedir=$ac_optarg ;;
+
+  -prefix | --prefix | --prefi | --pref | --pre | --pr | --p)
+    ac_prev=prefix ;;
+  -prefix=* | --prefix=* | --prefi=* | --pref=* | --pre=* | --pr=* | --p=*)
+    prefix=$ac_optarg ;;
+
+  -program-prefix | --program-prefix | --program-prefi | --program-pref \
+  | --program-pre | --program-pr | --program-p)
+    ac_prev=program_prefix ;;
+  -program-prefix=* | --program-prefix=* | --program-prefi=* \
+  | --program-pref=* | --program-pre=* | --program-pr=* | --program-p=*)
+    program_prefix=$ac_optarg ;;
+
+  -program-suffix | --program-suffix | --program-suffi | --program-suff \
+  | --program-suf | --program-su | --program-s)
+    ac_prev=program_suffix ;;
+  -program-suffix=* | --program-suffix=* | --program-suffi=* \
+  | --program-suff=* | --program-suf=* | --program-su=* | --program-s=*)
+    program_suffix=$ac_optarg ;;
+
+  -program-transform-name | --program-transform-name \
+  | --program-transform-nam | --program-transform-na \
+  | --program-transform-n | --program-transform- \
+  | --program-transform | --program-transfor \
+  | --program-transfo | --program-transf \
+  | --program-trans | --program-tran \
+  | --progr-tra | --program-tr | --program-t)
+    ac_prev=program_transform_name ;;
+  -program-transform-name=* | --program-transform-name=* \
+  | --program-transform-nam=* | --program-transform-na=* \
+  | --program-transform-n=* | --program-transform-=* \
+  | --program-transform=* | --program-transfor=* \
+  | --program-transfo=* | --program-transf=* \
+  | --program-trans=* | --program-tran=* \
+  | --progr-tra=* | --program-tr=* | --program-t=*)
+    program_transform_name=$ac_optarg ;;
+
+  -q | -quiet | --quiet | --quie | --qui | --qu | --q \
+  | -silent | --silent | --silen | --sile | --sil)
+    silent=yes ;;
+
+  -sbindir | --sbindir | --sbindi | --sbind | --sbin | --sbi | --sb)
+    ac_prev=sbindir ;;
+  -sbindir=* | --sbindir=* | --sbindi=* | --sbind=* | --sbin=* \
+  | --sbi=* | --sb=*)
+    sbindir=$ac_optarg ;;
+
+  -sharedstatedir | --sharedstatedir | --sharedstatedi \
+  | --sharedstated | --sharedstate | --sharedstat | --sharedsta \
+  | --sharedst | --shareds | --shared | --share | --shar \
+  | --sha | --sh)
+    ac_prev=sharedstatedir ;;
+  -sharedstatedir=* | --sharedstatedir=* | --sharedstatedi=* \
+  | --sharedstated=* | --sharedstate=* | --sharedstat=* | --sharedsta=* \
+  | --sharedst=* | --shareds=* | --shared=* | --share=* | --shar=* \
+  | --sha=* | --sh=*)
+    sharedstatedir=$ac_optarg ;;
+
+  -site | --site | --sit)
+    ac_prev=site ;;
+  -site=* | --site=* | --sit=*)
+    site=$ac_optarg ;;
+
+  -srcdir | --srcdir | --srcdi | --srcd | --src | --sr)
+    ac_prev=srcdir ;;
+  -srcdir=* | --srcdir=* | --srcdi=* | --srcd=* | --src=* | --sr=*)
+    srcdir=$ac_optarg ;;
+
+  -sysconfdir | --sysconfdir | --sysconfdi | --sysconfd | --sysconf \
+  | --syscon | --sysco | --sysc | --sys | --sy)
+    ac_prev=sysconfdir ;;
+  -sysconfdir=* | --sysconfdir=* | --sysconfdi=* | --sysconfd=* | --sysconf=* \
+  | --syscon=* | --sysco=* | --sysc=* | --sys=* | --sy=*)
+    sysconfdir=$ac_optarg ;;
+
+  -target | --target | --targe | --targ | --tar | --ta | --t)
+    ac_prev=target_alias ;;
+  -target=* | --target=* | --targe=* | --targ=* | --tar=* | --ta=* | --t=*)
+    target_alias=$ac_optarg ;;
+
+  -v | -verbose | --verbose | --verbos | --verbo | --verb)
+    verbose=yes ;;
+
+  -version | --version | --versio | --versi | --vers | -V)
+    ac_init_version=: ;;
+
+  -with-* | --with-*)
+    ac_package=`expr "x$ac_option" : 'x-*with-\([^=]*\)'`
+    # Reject names that are not valid shell variable names.
+    expr "x$ac_package" : ".*[^-_$as_cr_alnum]" >/dev/null &&
+      { echo "$as_me: error: invalid package name: $ac_package" >&2
+   { (exit 1); exit 1; }; }
+    ac_package=`echo $ac_package| sed 's/-/_/g'`
+    case $ac_option in
+      *=*) ac_optarg=`echo "$ac_optarg" | sed "s/'/'\\\\\\\\''/g"`;;
+      *) ac_optarg=yes ;;
+    esac
+    eval "with_$ac_package='$ac_optarg'" ;;
+
+  -without-* | --without-*)
+    ac_package=`expr "x$ac_option" : 'x-*without-\(.*\)'`
+    # Reject names that are not valid shell variable names.
+    expr "x$ac_package" : ".*[^-_$as_cr_alnum]" >/dev/null &&
+      { echo "$as_me: error: invalid package name: $ac_package" >&2
+   { (exit 1); exit 1; }; }
+    ac_package=`echo $ac_package | sed 's/-/_/g'`
+    eval "with_$ac_package=no" ;;
+
+  --x)
+    # Obsolete; use --with-x.
+    with_x=yes ;;
+
+  -x-includes | --x-includes | --x-include | --x-includ | --x-inclu \
+  | --x-incl | --x-inc | --x-in | --x-i)
+    ac_prev=x_includes ;;
+  -x-includes=* | --x-includes=* | --x-include=* | --x-includ=* | --x-inclu=* \
+  | --x-incl=* | --x-inc=* | --x-in=* | --x-i=*)
+    x_includes=$ac_optarg ;;
+
+  -x-libraries | --x-libraries | --x-librarie | --x-librari \
+  | --x-librar | --x-libra | --x-libr | --x-lib | --x-li | --x-l)
+    ac_prev=x_libraries ;;
+  -x-libraries=* | --x-libraries=* | --x-librarie=* | --x-librari=* \
+  | --x-librar=* | --x-libra=* | --x-libr=* | --x-lib=* | --x-li=* | --x-l=*)
+    x_libraries=$ac_optarg ;;
+
+  -*) { echo "$as_me: error: unrecognized option: $ac_option
+Try \`$0 --help' for more information." >&2
+   { (exit 1); exit 1; }; }
+    ;;
+
+  *=*)
+    ac_envvar=`expr "x$ac_option" : 'x\([^=]*\)='`
+    # Reject names that are not valid shell variable names.
+    expr "x$ac_envvar" : ".*[^_$as_cr_alnum]" >/dev/null &&
+      { echo "$as_me: error: invalid variable name: $ac_envvar" >&2
+   { (exit 1); exit 1; }; }
+    ac_optarg=`echo "$ac_optarg" | sed "s/'/'\\\\\\\\''/g"`
+    eval "$ac_envvar='$ac_optarg'"
+    export $ac_envvar ;;
+
+  *)
+    # FIXME: should be removed in autoconf 3.0.
+    echo "$as_me: WARNING: you should use --build, --host, --target" >&2
+    expr "x$ac_option" : ".*[^-._$as_cr_alnum]" >/dev/null &&
+      echo "$as_me: WARNING: invalid host type: $ac_option" >&2
+    : ${build_alias=$ac_option} ${host_alias=$ac_option} ${target_alias=$ac_option}
+    ;;
+
+  esac
+done
+
+if test -n "$ac_prev"; then
+  ac_option=--`echo $ac_prev | sed 's/_/-/g'`
+  { echo "$as_me: error: missing argument to $ac_option" >&2
+   { (exit 1); exit 1; }; }
+fi
+
+# Be sure to have absolute paths.
+for ac_var in exec_prefix prefix
+do
+  eval ac_val=$`echo $ac_var`
+  case $ac_val in
+    [\\/$]* | ?:[\\/]* | NONE | '' ) ;;
+    *)  { echo "$as_me: error: expected an absolute directory name for --$ac_var: $ac_val" >&2
+   { (exit 1); exit 1; }; };;
+  esac
+done
+
+# Be sure to have absolute paths.
+for ac_var in bindir sbindir libexecdir datadir sysconfdir sharedstatedir \
+             localstatedir libdir includedir oldincludedir infodir mandir
+do
+  eval ac_val=$`echo $ac_var`
+  case $ac_val in
+    [\\/$]* | ?:[\\/]* ) ;;
+    *)  { echo "$as_me: error: expected an absolute directory name for --$ac_var: $ac_val" >&2
+   { (exit 1); exit 1; }; };;
+  esac
+done
+
+# There might be people who depend on the old broken behavior: `$host'
+# used to hold the argument of --host etc.
+# FIXME: To remove some day.
+build=$build_alias
+host=$host_alias
+target=$target_alias
+
+# FIXME: To remove some day.
+if test "x$host_alias" != x; then
+  if test "x$build_alias" = x; then
+    cross_compiling=maybe
+    echo "$as_me: WARNING: If you wanted to set the --build type, don't use --host.
+    If a cross compiler is detected then cross compile mode will be used." >&2
+  elif test "x$build_alias" != "x$host_alias"; then
+    cross_compiling=yes
+  fi
+fi
+
+ac_tool_prefix=
+test -n "$host_alias" && ac_tool_prefix=$host_alias-
+
+test "$silent" = yes && exec 6>/dev/null
+
+
+# Find the source files, if location was not specified.
+if test -z "$srcdir"; then
+  ac_srcdir_defaulted=yes
+  # Try the directory containing this script, then its parent.
+  ac_confdir=`(dirname "$0") 2>/dev/null ||
+$as_expr X"$0" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
+        X"$0" : 'X\(//\)[^/]' \| \
+        X"$0" : 'X\(//\)$' \| \
+        X"$0" : 'X\(/\)' \| \
+        .     : '\(.\)' 2>/dev/null ||
+echo X"$0" |
+    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{ s//\1/; q; }
+         /^X\(\/\/\)[^/].*/{ s//\1/; q; }
+         /^X\(\/\/\)$/{ s//\1/; q; }
+         /^X\(\/\).*/{ s//\1/; q; }
+         s/.*/./; q'`
+  srcdir=$ac_confdir
+  if test ! -r $srcdir/$ac_unique_file; then
+    srcdir=..
+  fi
+else
+  ac_srcdir_defaulted=no
+fi
+if test ! -r $srcdir/$ac_unique_file; then
+  if test "$ac_srcdir_defaulted" = yes; then
+    { echo "$as_me: error: cannot find sources ($ac_unique_file) in $ac_confdir or .." >&2
+   { (exit 1); exit 1; }; }
+  else
+    { echo "$as_me: error: cannot find sources ($ac_unique_file) in $srcdir" >&2
+   { (exit 1); exit 1; }; }
+  fi
+fi
+(cd $srcdir && test -r ./$ac_unique_file) 2>/dev/null ||
+  { echo "$as_me: error: sources are in $srcdir, but \`cd $srcdir' does not work" >&2
+   { (exit 1); exit 1; }; }
+srcdir=`echo "$srcdir" | sed 's%\([^\\/]\)[\\/]*$%\1%'`
+ac_env_build_alias_set=${build_alias+set}
+ac_env_build_alias_value=$build_alias
+ac_cv_env_build_alias_set=${build_alias+set}
+ac_cv_env_build_alias_value=$build_alias
+ac_env_host_alias_set=${host_alias+set}
+ac_env_host_alias_value=$host_alias
+ac_cv_env_host_alias_set=${host_alias+set}
+ac_cv_env_host_alias_value=$host_alias
+ac_env_target_alias_set=${target_alias+set}
+ac_env_target_alias_value=$target_alias
+ac_cv_env_target_alias_set=${target_alias+set}
+ac_cv_env_target_alias_value=$target_alias
+ac_env_CC_set=${CC+set}
+ac_env_CC_value=$CC
+ac_cv_env_CC_set=${CC+set}
+ac_cv_env_CC_value=$CC
+ac_env_CFLAGS_set=${CFLAGS+set}
+ac_env_CFLAGS_value=$CFLAGS
+ac_cv_env_CFLAGS_set=${CFLAGS+set}
+ac_cv_env_CFLAGS_value=$CFLAGS
+ac_env_LDFLAGS_set=${LDFLAGS+set}
+ac_env_LDFLAGS_value=$LDFLAGS
+ac_cv_env_LDFLAGS_set=${LDFLAGS+set}
+ac_cv_env_LDFLAGS_value=$LDFLAGS
+ac_env_CPPFLAGS_set=${CPPFLAGS+set}
+ac_env_CPPFLAGS_value=$CPPFLAGS
+ac_cv_env_CPPFLAGS_set=${CPPFLAGS+set}
+ac_cv_env_CPPFLAGS_value=$CPPFLAGS
+
+#
+# Report the --help message.
+#
+if test "$ac_init_help" = "long"; then
+  # Omit some internal or obsolete options to make the list less imposing.
+  # This message is too long to be a string in the A/UX 3.1 sh.
+  cat <<_ACEOF
+\`configure' configures libnl 1.0-pre6 to adapt to many kinds of systems.
+
+Usage: $0 [OPTION]... [VAR=VALUE]...
+
+To assign environment variables (e.g., CC, CFLAGS...), specify them as
+VAR=VALUE.  See below for descriptions of some of the useful variables.
+
+Defaults for the options are specified in brackets.
+
+Configuration:
+  -h, --help              display this help and exit
+      --help=short        display options specific to this package
+      --help=recursive    display the short help of all the included packages
+  -V, --version           display version information and exit
+  -q, --quiet, --silent   do not print \`checking...' messages
+      --cache-file=FILE   cache test results in FILE [disabled]
+  -C, --config-cache      alias for \`--cache-file=config.cache'
+  -n, --no-create         do not create output files
+      --srcdir=DIR        find the sources in DIR [configure dir or \`..']
+
+_ACEOF
+
+  cat <<_ACEOF
+Installation directories:
+  --prefix=PREFIX         install architecture-independent files in PREFIX
+                         [$ac_default_prefix]
+  --exec-prefix=EPREFIX   install architecture-dependent files in EPREFIX
+                         [PREFIX]
+
+By default, \`make install' will install all the files in
+\`$ac_default_prefix/bin', \`$ac_default_prefix/lib' etc.  You can specify
+an installation prefix other than \`$ac_default_prefix' using \`--prefix',
+for instance \`--prefix=\$HOME'.
+
+For better control, use the options below.
+
+Fine tuning of the installation directories:
+  --bindir=DIR           user executables [EPREFIX/bin]
+  --sbindir=DIR          system admin executables [EPREFIX/sbin]
+  --libexecdir=DIR       program executables [EPREFIX/libexec]
+  --datadir=DIR          read-only architecture-independent data [PREFIX/share]
+  --sysconfdir=DIR       read-only single-machine data [PREFIX/etc]
+  --sharedstatedir=DIR   modifiable architecture-independent data [PREFIX/com]
+  --localstatedir=DIR    modifiable single-machine data [PREFIX/var]
+  --libdir=DIR           object code libraries [EPREFIX/lib]
+  --includedir=DIR       C header files [PREFIX/include]
+  --oldincludedir=DIR    C header files for non-gcc [/usr/include]
+  --infodir=DIR          info documentation [PREFIX/info]
+  --mandir=DIR           man documentation [PREFIX/man]
+_ACEOF
+
+  cat <<\_ACEOF
+_ACEOF
+fi
+
+if test -n "$ac_init_help"; then
+  case $ac_init_help in
+     short | recursive ) echo "Configuration of libnl 1.0-pre6:";;
+   esac
+  cat <<\_ACEOF
+
+Optional Features:
+  --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)
+  --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]
+  --enable-verbose-errors enable verbose errors (debugging)
+
+Some influential environment variables:
+  CC          C compiler command
+  CFLAGS      C compiler flags
+  LDFLAGS     linker flags, e.g. -L<lib dir> if you have libraries in a
+              nonstandard directory <lib dir>
+  CPPFLAGS    C/C++ preprocessor flags, e.g. -I<include dir> if you have
+              headers in a nonstandard directory <include dir>
+
+Use these variables to override the choices made by `configure' or to help
+it to find libraries and programs with nonstandard names/locations.
+
+Report bugs to <tgraf@suug.ch>.
+_ACEOF
+fi
+
+if test "$ac_init_help" = "recursive"; then
+  # If there are subdirs, report their specific --help.
+  ac_popdir=`pwd`
+  for ac_dir in : $ac_subdirs_all; do test "x$ac_dir" = x: && continue
+    test -d $ac_dir || continue
+    ac_builddir=.
+
+if test "$ac_dir" != .; then
+  ac_dir_suffix=/`echo "$ac_dir" | sed 's,^\.[\\/],,'`
+  # A "../" for each directory in $ac_dir_suffix.
+  ac_top_builddir=`echo "$ac_dir_suffix" | sed 's,/[^\\/]*,../,g'`
+else
+  ac_dir_suffix= ac_top_builddir=
+fi
+
+case $srcdir in
+  .)  # No --srcdir option.  We are building in place.
+    ac_srcdir=.
+    if test -z "$ac_top_builddir"; then
+       ac_top_srcdir=.
+    else
+       ac_top_srcdir=`echo $ac_top_builddir | sed 's,/$,,'`
+    fi ;;
+  [\\/]* | ?:[\\/]* )  # Absolute path.
+    ac_srcdir=$srcdir$ac_dir_suffix;
+    ac_top_srcdir=$srcdir ;;
+  *) # Relative path.
+    ac_srcdir=$ac_top_builddir$srcdir$ac_dir_suffix
+    ac_top_srcdir=$ac_top_builddir$srcdir ;;
+esac
+
+# Do not use `cd foo && pwd` to compute absolute paths, because
+# the directories may not exist.
+case `pwd` in
+.) ac_abs_builddir="$ac_dir";;
+*)
+  case "$ac_dir" in
+  .) ac_abs_builddir=`pwd`;;
+  [\\/]* | ?:[\\/]* ) ac_abs_builddir="$ac_dir";;
+  *) ac_abs_builddir=`pwd`/"$ac_dir";;
+  esac;;
+esac
+case $ac_abs_builddir in
+.) ac_abs_top_builddir=${ac_top_builddir}.;;
+*)
+  case ${ac_top_builddir}. in
+  .) ac_abs_top_builddir=$ac_abs_builddir;;
+  [\\/]* | ?:[\\/]* ) ac_abs_top_builddir=${ac_top_builddir}.;;
+  *) ac_abs_top_builddir=$ac_abs_builddir/${ac_top_builddir}.;;
+  esac;;
+esac
+case $ac_abs_builddir in
+.) ac_abs_srcdir=$ac_srcdir;;
+*)
+  case $ac_srcdir in
+  .) ac_abs_srcdir=$ac_abs_builddir;;
+  [\\/]* | ?:[\\/]* ) ac_abs_srcdir=$ac_srcdir;;
+  *) ac_abs_srcdir=$ac_abs_builddir/$ac_srcdir;;
+  esac;;
+esac
+case $ac_abs_builddir in
+.) ac_abs_top_srcdir=$ac_top_srcdir;;
+*)
+  case $ac_top_srcdir in
+  .) ac_abs_top_srcdir=$ac_abs_builddir;;
+  [\\/]* | ?:[\\/]* ) ac_abs_top_srcdir=$ac_top_srcdir;;
+  *) ac_abs_top_srcdir=$ac_abs_builddir/$ac_top_srcdir;;
+  esac;;
+esac
+
+    cd $ac_dir
+    # Check for guested configure; otherwise get Cygnus style configure.
+    if test -f $ac_srcdir/configure.gnu; then
+      echo
+      $SHELL $ac_srcdir/configure.gnu  --help=recursive
+    elif test -f $ac_srcdir/configure; then
+      echo
+      $SHELL $ac_srcdir/configure  --help=recursive
+    elif test -f $ac_srcdir/configure.ac ||
+          test -f $ac_srcdir/configure.in; then
+      echo
+      $ac_configure --help
+    else
+      echo "$as_me: WARNING: no configuration information is in $ac_dir" >&2
+    fi
+    cd "$ac_popdir"
+  done
+fi
+
+test -n "$ac_init_help" && exit 0
+if $ac_init_version; then
+  cat <<\_ACEOF
+libnl configure 1.0-pre6
+generated by GNU Autoconf 2.59
+
+Copyright (C) 2003 Free Software Foundation, Inc.
+This configure script is free software; the Free Software Foundation
+gives unlimited permission to copy, distribute and modify it.
+_ACEOF
+  exit 0
+fi
+exec 5>config.log
+cat >&5 <<_ACEOF
+This file contains any messages produced by compilers while
+running configure, to aid debugging if configure makes a mistake.
+
+It was created by libnl $as_me 1.0-pre6, which was
+generated by GNU Autoconf 2.59.  Invocation command line was
+
+  $ $0 $@
+
+_ACEOF
+{
+cat <<_ASUNAME
+## --------- ##
+## Platform. ##
+## --------- ##
+
+hostname = `(hostname || uname -n) 2>/dev/null | sed 1q`
+uname -m = `(uname -m) 2>/dev/null || echo unknown`
+uname -r = `(uname -r) 2>/dev/null || echo unknown`
+uname -s = `(uname -s) 2>/dev/null || echo unknown`
+uname -v = `(uname -v) 2>/dev/null || echo unknown`
+
+/usr/bin/uname -p = `(/usr/bin/uname -p) 2>/dev/null || echo unknown`
+/bin/uname -X     = `(/bin/uname -X) 2>/dev/null     || echo unknown`
+
+/bin/arch              = `(/bin/arch) 2>/dev/null              || echo unknown`
+/usr/bin/arch -k       = `(/usr/bin/arch -k) 2>/dev/null       || echo unknown`
+/usr/convex/getsysinfo = `(/usr/convex/getsysinfo) 2>/dev/null || echo unknown`
+hostinfo               = `(hostinfo) 2>/dev/null               || echo unknown`
+/bin/machine           = `(/bin/machine) 2>/dev/null           || echo unknown`
+/usr/bin/oslevel       = `(/usr/bin/oslevel) 2>/dev/null       || echo unknown`
+/bin/universe          = `(/bin/universe) 2>/dev/null          || echo unknown`
+
+_ASUNAME
+
+as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
+for as_dir in $PATH
+do
+  IFS=$as_save_IFS
+  test -z "$as_dir" && as_dir=.
+  echo "PATH: $as_dir"
+done
+
+} >&5
+
+cat >&5 <<_ACEOF
+
+
+## ----------- ##
+## Core tests. ##
+## ----------- ##
+
+_ACEOF
+
+
+# Keep a trace of the command line.
+# Strip out --no-create and --no-recursion so they do not pile up.
+# Strip out --silent because we don't want to record it for future runs.
+# Also quote any args containing shell meta-characters.
+# Make two passes to allow for proper duplicate-argument suppression.
+ac_configure_args=
+ac_configure_args0=
+ac_configure_args1=
+ac_sep=
+ac_must_keep_next=false
+for ac_pass in 1 2
+do
+  for ac_arg
+  do
+    case $ac_arg in
+    -no-create | --no-c* | -n | -no-recursion | --no-r*) continue ;;
+    -q | -quiet | --quiet | --quie | --qui | --qu | --q \
+    | -silent | --silent | --silen | --sile | --sil)
+      continue ;;
+    *" "*|*"   "*|*[\[\]\~\#\$\^\&\*\(\)\{\}\\\|\;\<\>\?\"\']*)
+      ac_arg=`echo "$ac_arg" | sed "s/'/'\\\\\\\\''/g"` ;;
+    esac
+    case $ac_pass in
+    1) ac_configure_args0="$ac_configure_args0 '$ac_arg'" ;;
+    2)
+      ac_configure_args1="$ac_configure_args1 '$ac_arg'"
+      if test $ac_must_keep_next = true; then
+       ac_must_keep_next=false # Got value, back to normal.
+      else
+       case $ac_arg in
+         *=* | --config-cache | -C | -disable-* | --disable-* \
+         | -enable-* | --enable-* | -gas | --g* | -nfp | --nf* \
+         | -q | -quiet | --q* | -silent | --sil* | -v | -verb* \
+         | -with-* | --with-* | -without-* | --without-* | --x)
+           case "$ac_configure_args0 " in
+             "$ac_configure_args1"*" '$ac_arg' "* ) continue ;;
+           esac
+           ;;
+         -* ) ac_must_keep_next=true ;;
+       esac
+      fi
+      ac_configure_args="$ac_configure_args$ac_sep'$ac_arg'"
+      # Get rid of the leading space.
+      ac_sep=" "
+      ;;
+    esac
+  done
+done
+$as_unset ac_configure_args0 || test "${ac_configure_args0+set}" != set || { ac_configure_args0=; export ac_configure_args0; }
+$as_unset ac_configure_args1 || test "${ac_configure_args1+set}" != set || { ac_configure_args1=; export ac_configure_args1; }
+
+# When interrupted or exit'd, cleanup temporary files, and complete
+# config.log.  We remove comments because anyway the quotes in there
+# would cause problems or look ugly.
+# WARNING: Be sure not to use single quotes in there, as some shells,
+# such as our DU 5.0 friend, will then `close' the trap.
+trap 'exit_status=$?
+  # Save into config.log some information that might help in debugging.
+  {
+    echo
+
+    cat <<\_ASBOX
+## ---------------- ##
+## Cache variables. ##
+## ---------------- ##
+_ASBOX
+    echo
+    # The following way of writing the cache mishandles newlines in values,
+{
+  (set) 2>&1 |
+    case `(ac_space='"'"' '"'"'; set | grep ac_space) 2>&1` in
+    *ac_space=\ *)
+      sed -n \
+       "s/'"'"'/'"'"'\\\\'"'"''"'"'/g;
+         s/^\\([_$as_cr_alnum]*_cv_[_$as_cr_alnum]*\\)=\\(.*\\)/\\1='"'"'\\2'"'"'/p"
+      ;;
+    *)
+      sed -n \
+       "s/^\\([_$as_cr_alnum]*_cv_[_$as_cr_alnum]*\\)=\\(.*\\)/\\1=\\2/p"
+      ;;
+    esac;
+}
+    echo
+
+    cat <<\_ASBOX
+## ----------------- ##
+## Output variables. ##
+## ----------------- ##
+_ASBOX
+    echo
+    for ac_var in $ac_subst_vars
+    do
+      eval ac_val=$`echo $ac_var`
+      echo "$ac_var='"'"'$ac_val'"'"'"
+    done | sort
+    echo
+
+    if test -n "$ac_subst_files"; then
+      cat <<\_ASBOX
+## ------------- ##
+## Output files. ##
+## ------------- ##
+_ASBOX
+      echo
+      for ac_var in $ac_subst_files
+      do
+       eval ac_val=$`echo $ac_var`
+       echo "$ac_var='"'"'$ac_val'"'"'"
+      done | sort
+      echo
+    fi
+
+    if test -s confdefs.h; then
+      cat <<\_ASBOX
+## ----------- ##
+## confdefs.h. ##
+## ----------- ##
+_ASBOX
+      echo
+      sed "/^$/d" confdefs.h | sort
+      echo
+    fi
+    test "$ac_signal" != 0 &&
+      echo "$as_me: caught signal $ac_signal"
+    echo "$as_me: exit $exit_status"
+  } >&5
+  rm -f core *.core &&
+  rm -rf conftest* confdefs* conf$$* $ac_clean_files &&
+    exit $exit_status
+     ' 0
+for ac_signal in 1 2 13 15; do
+  trap 'ac_signal='$ac_signal'; { (exit 1); exit 1; }' $ac_signal
+done
+ac_signal=0
+
+# confdefs.h avoids OS command line length limits that DEFS can exceed.
+rm -rf conftest* confdefs.h
+# AIX cpp loses on an empty file, so make sure it contains at least a newline.
+echo >confdefs.h
+
+# Predefined preprocessor variables.
+
+cat >>confdefs.h <<_ACEOF
+#define PACKAGE_NAME "$PACKAGE_NAME"
+_ACEOF
+
+
+cat >>confdefs.h <<_ACEOF
+#define PACKAGE_TARNAME "$PACKAGE_TARNAME"
+_ACEOF
+
+
+cat >>confdefs.h <<_ACEOF
+#define PACKAGE_VERSION "$PACKAGE_VERSION"
+_ACEOF
+
+
+cat >>confdefs.h <<_ACEOF
+#define PACKAGE_STRING "$PACKAGE_STRING"
+_ACEOF
+
+
+cat >>confdefs.h <<_ACEOF
+#define PACKAGE_BUGREPORT "$PACKAGE_BUGREPORT"
+_ACEOF
+
+
+# Let the site file select an alternate cache file if it wants to.
+# Prefer explicitly selected file to automatically selected ones.
+if test -z "$CONFIG_SITE"; then
+  if test "x$prefix" != xNONE; then
+    CONFIG_SITE="$prefix/share/config.site $prefix/etc/config.site"
+  else
+    CONFIG_SITE="$ac_default_prefix/share/config.site $ac_default_prefix/etc/config.site"
+  fi
+fi
+for ac_site_file in $CONFIG_SITE; do
+  if test -r "$ac_site_file"; then
+    { echo "$as_me:$LINENO: loading site script $ac_site_file" >&5
+echo "$as_me: loading site script $ac_site_file" >&6;}
+    sed 's/^/| /' "$ac_site_file" >&5
+    . "$ac_site_file"
+  fi
+done
+
+if test -r "$cache_file"; then
+  # Some versions of bash will fail to source /dev/null (special
+  # files actually), so we avoid doing that.
+  if test -f "$cache_file"; then
+    { echo "$as_me:$LINENO: loading cache $cache_file" >&5
+echo "$as_me: loading cache $cache_file" >&6;}
+    case $cache_file in
+      [\\/]* | ?:[\\/]* ) . $cache_file;;
+      *)                      . ./$cache_file;;
+    esac
+  fi
+else
+  { echo "$as_me:$LINENO: creating cache $cache_file" >&5
+echo "$as_me: creating cache $cache_file" >&6;}
+  >$cache_file
+fi
+
+# Check that the precious variables saved in the cache have kept the same
+# value.
+ac_cache_corrupted=false
+for ac_var in `(set) 2>&1 |
+              sed -n 's/^ac_env_\([a-zA-Z_0-9]*\)_set=.*/\1/p'`; do
+  eval ac_old_set=\$ac_cv_env_${ac_var}_set
+  eval ac_new_set=\$ac_env_${ac_var}_set
+  eval ac_old_val="\$ac_cv_env_${ac_var}_value"
+  eval ac_new_val="\$ac_env_${ac_var}_value"
+  case $ac_old_set,$ac_new_set in
+    set,)
+      { echo "$as_me:$LINENO: error: \`$ac_var' was set to \`$ac_old_val' in the previous run" >&5
+echo "$as_me: error: \`$ac_var' was set to \`$ac_old_val' in the previous run" >&2;}
+      ac_cache_corrupted=: ;;
+    ,set)
+      { echo "$as_me:$LINENO: error: \`$ac_var' was not set in the previous run" >&5
+echo "$as_me: error: \`$ac_var' was not set in the previous run" >&2;}
+      ac_cache_corrupted=: ;;
+    ,);;
+    *)
+      if test "x$ac_old_val" != "x$ac_new_val"; then
+       { echo "$as_me:$LINENO: error: \`$ac_var' has changed since the previous run:" >&5
+echo "$as_me: error: \`$ac_var' has changed since the previous run:" >&2;}
+       { echo "$as_me:$LINENO:   former value:  $ac_old_val" >&5
+echo "$as_me:   former value:  $ac_old_val" >&2;}
+       { echo "$as_me:$LINENO:   current value: $ac_new_val" >&5
+echo "$as_me:   current value: $ac_new_val" >&2;}
+       ac_cache_corrupted=:
+      fi;;
+  esac
+  # Pass precious variables to config.status.
+  if test "$ac_new_set" = set; then
+    case $ac_new_val in
+    *" "*|*"   "*|*[\[\]\~\#\$\^\&\*\(\)\{\}\\\|\;\<\>\?\"\']*)
+      ac_arg=$ac_var=`echo "$ac_new_val" | sed "s/'/'\\\\\\\\''/g"` ;;
+    *) ac_arg=$ac_var=$ac_new_val ;;
+    esac
+    case " $ac_configure_args " in
+      *" '$ac_arg' "*) ;; # Avoid dups.  Use of quotes ensures accuracy.
+      *) ac_configure_args="$ac_configure_args '$ac_arg'" ;;
+    esac
+  fi
+done
+if $ac_cache_corrupted; then
+  { echo "$as_me:$LINENO: error: changes in the environment can compromise the build" >&5
+echo "$as_me: error: changes in the environment can compromise the build" >&2;}
+  { { echo "$as_me:$LINENO: error: run \`make distclean' and/or \`rm $cache_file' and start over" >&5
+echo "$as_me: error: run \`make distclean' and/or \`rm $cache_file' and start over" >&2;}
+   { (exit 1); exit 1; }; }
+fi
+
+ac_ext=c
+ac_cpp='$CPP $CPPFLAGS'
+ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
+ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
+ac_compiler_gnu=$ac_cv_c_compiler_gnu
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+          ac_config_headers="$ac_config_headers lib/defs.h"
+
+
+save_CFLAGS="${CFLAGS}"
+save_LDFLAGS="${LDFLAGS}"
+save_CPPFLAGS="${CPPFLAGS}"
+
+ac_ext=c
+ac_cpp='$CPP $CPPFLAGS'
+ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
+ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
+ac_compiler_gnu=$ac_cv_c_compiler_gnu
+if test -n "$ac_tool_prefix"; then
+  # Extract the first word of "${ac_tool_prefix}gcc", so it can be a program name with args.
+set dummy ${ac_tool_prefix}gcc; ac_word=$2
+echo "$as_me:$LINENO: checking for $ac_word" >&5
+echo $ECHO_N "checking for $ac_word... $ECHO_C" >&6
+if test "${ac_cv_prog_CC+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  if test -n "$CC"; then
+  ac_cv_prog_CC="$CC" # Let the user override the test.
+else
+as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
+for as_dir in $PATH
+do
+  IFS=$as_save_IFS
+  test -z "$as_dir" && as_dir=.
+  for ac_exec_ext in '' $ac_executable_extensions; do
+  if $as_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+    ac_cv_prog_CC="${ac_tool_prefix}gcc"
+    echo "$as_me:$LINENO: found $as_dir/$ac_word$ac_exec_ext" >&5
+    break 2
+  fi
+done
+done
+
+fi
+fi
+CC=$ac_cv_prog_CC
+if test -n "$CC"; then
+  echo "$as_me:$LINENO: result: $CC" >&5
+echo "${ECHO_T}$CC" >&6
+else
+  echo "$as_me:$LINENO: result: no" >&5
+echo "${ECHO_T}no" >&6
+fi
+
+fi
+if test -z "$ac_cv_prog_CC"; then
+  ac_ct_CC=$CC
+  # Extract the first word of "gcc", so it can be a program name with args.
+set dummy gcc; ac_word=$2
+echo "$as_me:$LINENO: checking for $ac_word" >&5
+echo $ECHO_N "checking for $ac_word... $ECHO_C" >&6
+if test "${ac_cv_prog_ac_ct_CC+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  if test -n "$ac_ct_CC"; then
+  ac_cv_prog_ac_ct_CC="$ac_ct_CC" # Let the user override the test.
+else
+as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
+for as_dir in $PATH
+do
+  IFS=$as_save_IFS
+  test -z "$as_dir" && as_dir=.
+  for ac_exec_ext in '' $ac_executable_extensions; do
+  if $as_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+    ac_cv_prog_ac_ct_CC="gcc"
+    echo "$as_me:$LINENO: found $as_dir/$ac_word$ac_exec_ext" >&5
+    break 2
+  fi
+done
+done
+
+fi
+fi
+ac_ct_CC=$ac_cv_prog_ac_ct_CC
+if test -n "$ac_ct_CC"; then
+  echo "$as_me:$LINENO: result: $ac_ct_CC" >&5
+echo "${ECHO_T}$ac_ct_CC" >&6
+else
+  echo "$as_me:$LINENO: result: no" >&5
+echo "${ECHO_T}no" >&6
+fi
+
+  CC=$ac_ct_CC
+else
+  CC="$ac_cv_prog_CC"
+fi
+
+if test -z "$CC"; then
+  if test -n "$ac_tool_prefix"; then
+  # Extract the first word of "${ac_tool_prefix}cc", so it can be a program name with args.
+set dummy ${ac_tool_prefix}cc; ac_word=$2
+echo "$as_me:$LINENO: checking for $ac_word" >&5
+echo $ECHO_N "checking for $ac_word... $ECHO_C" >&6
+if test "${ac_cv_prog_CC+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  if test -n "$CC"; then
+  ac_cv_prog_CC="$CC" # Let the user override the test.
+else
+as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
+for as_dir in $PATH
+do
+  IFS=$as_save_IFS
+  test -z "$as_dir" && as_dir=.
+  for ac_exec_ext in '' $ac_executable_extensions; do
+  if $as_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+    ac_cv_prog_CC="${ac_tool_prefix}cc"
+    echo "$as_me:$LINENO: found $as_dir/$ac_word$ac_exec_ext" >&5
+    break 2
+  fi
+done
+done
+
+fi
+fi
+CC=$ac_cv_prog_CC
+if test -n "$CC"; then
+  echo "$as_me:$LINENO: result: $CC" >&5
+echo "${ECHO_T}$CC" >&6
+else
+  echo "$as_me:$LINENO: result: no" >&5
+echo "${ECHO_T}no" >&6
+fi
+
+fi
+if test -z "$ac_cv_prog_CC"; then
+  ac_ct_CC=$CC
+  # Extract the first word of "cc", so it can be a program name with args.
+set dummy cc; ac_word=$2
+echo "$as_me:$LINENO: checking for $ac_word" >&5
+echo $ECHO_N "checking for $ac_word... $ECHO_C" >&6
+if test "${ac_cv_prog_ac_ct_CC+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  if test -n "$ac_ct_CC"; then
+  ac_cv_prog_ac_ct_CC="$ac_ct_CC" # Let the user override the test.
+else
+as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
+for as_dir in $PATH
+do
+  IFS=$as_save_IFS
+  test -z "$as_dir" && as_dir=.
+  for ac_exec_ext in '' $ac_executable_extensions; do
+  if $as_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+    ac_cv_prog_ac_ct_CC="cc"
+    echo "$as_me:$LINENO: found $as_dir/$ac_word$ac_exec_ext" >&5
+    break 2
+  fi
+done
+done
+
+fi
+fi
+ac_ct_CC=$ac_cv_prog_ac_ct_CC
+if test -n "$ac_ct_CC"; then
+  echo "$as_me:$LINENO: result: $ac_ct_CC" >&5
+echo "${ECHO_T}$ac_ct_CC" >&6
+else
+  echo "$as_me:$LINENO: result: no" >&5
+echo "${ECHO_T}no" >&6
+fi
+
+  CC=$ac_ct_CC
+else
+  CC="$ac_cv_prog_CC"
+fi
+
+fi
+if test -z "$CC"; then
+  # Extract the first word of "cc", so it can be a program name with args.
+set dummy cc; ac_word=$2
+echo "$as_me:$LINENO: checking for $ac_word" >&5
+echo $ECHO_N "checking for $ac_word... $ECHO_C" >&6
+if test "${ac_cv_prog_CC+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  if test -n "$CC"; then
+  ac_cv_prog_CC="$CC" # Let the user override the test.
+else
+  ac_prog_rejected=no
+as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
+for as_dir in $PATH
+do
+  IFS=$as_save_IFS
+  test -z "$as_dir" && as_dir=.
+  for ac_exec_ext in '' $ac_executable_extensions; do
+  if $as_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+    if test "$as_dir/$ac_word$ac_exec_ext" = "/usr/ucb/cc"; then
+       ac_prog_rejected=yes
+       continue
+     fi
+    ac_cv_prog_CC="cc"
+    echo "$as_me:$LINENO: found $as_dir/$ac_word$ac_exec_ext" >&5
+    break 2
+  fi
+done
+done
+
+if test $ac_prog_rejected = yes; then
+  # We found a bogon in the path, so make sure we never use it.
+  set dummy $ac_cv_prog_CC
+  shift
+  if test $# != 0; then
+    # We chose a different compiler from the bogus one.
+    # However, it has the same basename, so the bogon will be chosen
+    # first if we set CC to just the basename; use the full file name.
+    shift
+    ac_cv_prog_CC="$as_dir/$ac_word${1+' '}$@"
+  fi
+fi
+fi
+fi
+CC=$ac_cv_prog_CC
+if test -n "$CC"; then
+  echo "$as_me:$LINENO: result: $CC" >&5
+echo "${ECHO_T}$CC" >&6
+else
+  echo "$as_me:$LINENO: result: no" >&5
+echo "${ECHO_T}no" >&6
+fi
+
+fi
+if test -z "$CC"; then
+  if test -n "$ac_tool_prefix"; then
+  for ac_prog in cl
+  do
+    # Extract the first word of "$ac_tool_prefix$ac_prog", so it can be a program name with args.
+set dummy $ac_tool_prefix$ac_prog; ac_word=$2
+echo "$as_me:$LINENO: checking for $ac_word" >&5
+echo $ECHO_N "checking for $ac_word... $ECHO_C" >&6
+if test "${ac_cv_prog_CC+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  if test -n "$CC"; then
+  ac_cv_prog_CC="$CC" # Let the user override the test.
+else
+as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
+for as_dir in $PATH
+do
+  IFS=$as_save_IFS
+  test -z "$as_dir" && as_dir=.
+  for ac_exec_ext in '' $ac_executable_extensions; do
+  if $as_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+    ac_cv_prog_CC="$ac_tool_prefix$ac_prog"
+    echo "$as_me:$LINENO: found $as_dir/$ac_word$ac_exec_ext" >&5
+    break 2
+  fi
+done
+done
+
+fi
+fi
+CC=$ac_cv_prog_CC
+if test -n "$CC"; then
+  echo "$as_me:$LINENO: result: $CC" >&5
+echo "${ECHO_T}$CC" >&6
+else
+  echo "$as_me:$LINENO: result: no" >&5
+echo "${ECHO_T}no" >&6
+fi
+
+    test -n "$CC" && break
+  done
+fi
+if test -z "$CC"; then
+  ac_ct_CC=$CC
+  for ac_prog in cl
+do
+  # Extract the first word of "$ac_prog", so it can be a program name with args.
+set dummy $ac_prog; ac_word=$2
+echo "$as_me:$LINENO: checking for $ac_word" >&5
+echo $ECHO_N "checking for $ac_word... $ECHO_C" >&6
+if test "${ac_cv_prog_ac_ct_CC+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  if test -n "$ac_ct_CC"; then
+  ac_cv_prog_ac_ct_CC="$ac_ct_CC" # Let the user override the test.
+else
+as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
+for as_dir in $PATH
+do
+  IFS=$as_save_IFS
+  test -z "$as_dir" && as_dir=.
+  for ac_exec_ext in '' $ac_executable_extensions; do
+  if $as_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+    ac_cv_prog_ac_ct_CC="$ac_prog"
+    echo "$as_me:$LINENO: found $as_dir/$ac_word$ac_exec_ext" >&5
+    break 2
+  fi
+done
+done
+
+fi
+fi
+ac_ct_CC=$ac_cv_prog_ac_ct_CC
+if test -n "$ac_ct_CC"; then
+  echo "$as_me:$LINENO: result: $ac_ct_CC" >&5
+echo "${ECHO_T}$ac_ct_CC" >&6
+else
+  echo "$as_me:$LINENO: result: no" >&5
+echo "${ECHO_T}no" >&6
+fi
+
+  test -n "$ac_ct_CC" && break
+done
+
+  CC=$ac_ct_CC
+fi
+
+fi
+
+
+test -z "$CC" && { { echo "$as_me:$LINENO: error: no acceptable C compiler found in \$PATH
+See \`config.log' for more details." >&5
+echo "$as_me: error: no acceptable C compiler found in \$PATH
+See \`config.log' for more details." >&2;}
+   { (exit 1); exit 1; }; }
+
+# Provide some information about the compiler.
+echo "$as_me:$LINENO:" \
+     "checking for C compiler version" >&5
+ac_compiler=`set X $ac_compile; echo $2`
+{ (eval echo "$as_me:$LINENO: \"$ac_compiler --version </dev/null >&5\"") >&5
+  (eval $ac_compiler --version </dev/null >&5) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }
+{ (eval echo "$as_me:$LINENO: \"$ac_compiler -v </dev/null >&5\"") >&5
+  (eval $ac_compiler -v </dev/null >&5) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }
+{ (eval echo "$as_me:$LINENO: \"$ac_compiler -V </dev/null >&5\"") >&5
+  (eval $ac_compiler -V </dev/null >&5) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }
+
+cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+
+int
+main ()
+{
+
+  ;
+  return 0;
+}
+_ACEOF
+ac_clean_files_save=$ac_clean_files
+ac_clean_files="$ac_clean_files a.out a.exe b.out"
+# Try to create an executable without -o first, disregard a.out.
+# It will help us diagnose broken compilers, and finding out an intuition
+# of exeext.
+echo "$as_me:$LINENO: checking for C compiler default output file name" >&5
+echo $ECHO_N "checking for C compiler default output file name... $ECHO_C" >&6
+ac_link_default=`echo "$ac_link" | sed 's/ -o *conftest[^ ]*//'`
+if { (eval echo "$as_me:$LINENO: \"$ac_link_default\"") >&5
+  (eval $ac_link_default) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; then
+  # Find the output, starting from the most likely.  This scheme is
+# not robust to junk in `.', hence go to wildcards (a.*) only as a last
+# resort.
+
+# Be careful to initialize this variable, since it used to be cached.
+# Otherwise an old cache value of `no' led to `EXEEXT = no' in a Makefile.
+ac_cv_exeext=
+# b.out is created by i960 compilers.
+for ac_file in a_out.exe a.exe conftest.exe a.out conftest a.* conftest.* b.out
+do
+  test -f "$ac_file" || continue
+  case $ac_file in
+    *.$ac_ext | *.xcoff | *.tds | *.d | *.pdb | *.xSYM | *.bb | *.bbg | *.o | *.obj )
+       ;;
+    conftest.$ac_ext )
+       # This is the source file.
+       ;;
+    [ab].out )
+       # We found the default executable, but exeext='' is most
+       # certainly right.
+       break;;
+    *.* )
+       ac_cv_exeext=`expr "$ac_file" : '[^.]*\(\..*\)'`
+       # FIXME: I believe we export ac_cv_exeext for Libtool,
+       # but it would be cool to find out if it's true.  Does anybody
+       # maintain Libtool? --akim.
+       export ac_cv_exeext
+       break;;
+    * )
+       break;;
+  esac
+done
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+{ { echo "$as_me:$LINENO: error: C compiler cannot create executables
+See \`config.log' for more details." >&5
+echo "$as_me: error: C compiler cannot create executables
+See \`config.log' for more details." >&2;}
+   { (exit 77); exit 77; }; }
+fi
+
+ac_exeext=$ac_cv_exeext
+echo "$as_me:$LINENO: result: $ac_file" >&5
+echo "${ECHO_T}$ac_file" >&6
+
+# Check the compiler produces executables we can run.  If not, either
+# the compiler is broken, or we cross compile.
+echo "$as_me:$LINENO: checking whether the C compiler works" >&5
+echo $ECHO_N "checking whether the C compiler works... $ECHO_C" >&6
+# FIXME: These cross compiler hacks should be removed for Autoconf 3.0
+# If not cross compiling, check that we can run a simple program.
+if test "$cross_compiling" != yes; then
+  if { ac_try='./$ac_file'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; }; then
+    cross_compiling=no
+  else
+    if test "$cross_compiling" = maybe; then
+       cross_compiling=yes
+    else
+       { { echo "$as_me:$LINENO: error: cannot run C compiled programs.
+If you meant to cross compile, use \`--host'.
+See \`config.log' for more details." >&5
+echo "$as_me: error: cannot run C compiled programs.
+If you meant to cross compile, use \`--host'.
+See \`config.log' for more details." >&2;}
+   { (exit 1); exit 1; }; }
+    fi
+  fi
+fi
+echo "$as_me:$LINENO: result: yes" >&5
+echo "${ECHO_T}yes" >&6
+
+rm -f a.out a.exe conftest$ac_cv_exeext b.out
+ac_clean_files=$ac_clean_files_save
+# Check the compiler produces executables we can run.  If not, either
+# the compiler is broken, or we cross compile.
+echo "$as_me:$LINENO: checking whether we are cross compiling" >&5
+echo $ECHO_N "checking whether we are cross compiling... $ECHO_C" >&6
+echo "$as_me:$LINENO: result: $cross_compiling" >&5
+echo "${ECHO_T}$cross_compiling" >&6
+
+echo "$as_me:$LINENO: checking for suffix of executables" >&5
+echo $ECHO_N "checking for suffix of executables... $ECHO_C" >&6
+if { (eval echo "$as_me:$LINENO: \"$ac_link\"") >&5
+  (eval $ac_link) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; then
+  # If both `conftest.exe' and `conftest' are `present' (well, observable)
+# catch `conftest.exe'.  For instance with Cygwin, `ls conftest' will
+# work properly (i.e., refer to `conftest.exe'), while it won't with
+# `rm'.
+for ac_file in conftest.exe conftest conftest.*; do
+  test -f "$ac_file" || continue
+  case $ac_file in
+    *.$ac_ext | *.xcoff | *.tds | *.d | *.pdb | *.xSYM | *.bb | *.bbg | *.o | *.obj ) ;;
+    *.* ) ac_cv_exeext=`expr "$ac_file" : '[^.]*\(\..*\)'`
+         export ac_cv_exeext
+         break;;
+    * ) break;;
+  esac
+done
+else
+  { { echo "$as_me:$LINENO: error: cannot compute suffix of executables: cannot compile and link
+See \`config.log' for more details." >&5
+echo "$as_me: error: cannot compute suffix of executables: cannot compile and link
+See \`config.log' for more details." >&2;}
+   { (exit 1); exit 1; }; }
+fi
+
+rm -f conftest$ac_cv_exeext
+echo "$as_me:$LINENO: result: $ac_cv_exeext" >&5
+echo "${ECHO_T}$ac_cv_exeext" >&6
+
+rm -f conftest.$ac_ext
+EXEEXT=$ac_cv_exeext
+ac_exeext=$EXEEXT
+echo "$as_me:$LINENO: checking for suffix of object files" >&5
+echo $ECHO_N "checking for suffix of object files... $ECHO_C" >&6
+if test "${ac_cv_objext+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+
+int
+main ()
+{
+
+  ;
+  return 0;
+}
+_ACEOF
+rm -f conftest.o conftest.obj
+if { (eval echo "$as_me:$LINENO: \"$ac_compile\"") >&5
+  (eval $ac_compile) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; then
+  for ac_file in `(ls conftest.o conftest.obj; ls conftest.*) 2>/dev/null`; do
+  case $ac_file in
+    *.$ac_ext | *.xcoff | *.tds | *.d | *.pdb | *.xSYM | *.bb | *.bbg ) ;;
+    *) ac_cv_objext=`expr "$ac_file" : '.*\.\(.*\)'`
+       break;;
+  esac
+done
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+{ { echo "$as_me:$LINENO: error: cannot compute suffix of object files: cannot compile
+See \`config.log' for more details." >&5
+echo "$as_me: error: cannot compute suffix of object files: cannot compile
+See \`config.log' for more details." >&2;}
+   { (exit 1); exit 1; }; }
+fi
+
+rm -f conftest.$ac_cv_objext conftest.$ac_ext
+fi
+echo "$as_me:$LINENO: result: $ac_cv_objext" >&5
+echo "${ECHO_T}$ac_cv_objext" >&6
+OBJEXT=$ac_cv_objext
+ac_objext=$OBJEXT
+echo "$as_me:$LINENO: checking whether we are using the GNU C compiler" >&5
+echo $ECHO_N "checking whether we are using the GNU C compiler... $ECHO_C" >&6
+if test "${ac_cv_c_compiler_gnu+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+
+int
+main ()
+{
+#ifndef __GNUC__
+       choke me
+#endif
+
+  ;
+  return 0;
+}
+_ACEOF
+rm -f conftest.$ac_objext
+if { (eval echo "$as_me:$LINENO: \"$ac_compile\"") >&5
+  (eval $ac_compile) 2>conftest.er1
+  ac_status=$?
+  grep -v '^ *+' conftest.er1 >conftest.err
+  rm -f conftest.er1
+  cat conftest.err >&5
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } &&
+        { ac_try='test -z "$ac_c_werror_flag"                   || test ! -s conftest.err'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; } &&
+        { ac_try='test -s conftest.$ac_objext'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; }; then
+  ac_compiler_gnu=yes
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+ac_compiler_gnu=no
+fi
+rm -f conftest.err conftest.$ac_objext conftest.$ac_ext
+ac_cv_c_compiler_gnu=$ac_compiler_gnu
+
+fi
+echo "$as_me:$LINENO: result: $ac_cv_c_compiler_gnu" >&5
+echo "${ECHO_T}$ac_cv_c_compiler_gnu" >&6
+GCC=`test $ac_compiler_gnu = yes && echo yes`
+ac_test_CFLAGS=${CFLAGS+set}
+ac_save_CFLAGS=$CFLAGS
+CFLAGS="-g"
+echo "$as_me:$LINENO: checking whether $CC accepts -g" >&5
+echo $ECHO_N "checking whether $CC accepts -g... $ECHO_C" >&6
+if test "${ac_cv_prog_cc_g+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+
+int
+main ()
+{
+
+  ;
+  return 0;
+}
+_ACEOF
+rm -f conftest.$ac_objext
+if { (eval echo "$as_me:$LINENO: \"$ac_compile\"") >&5
+  (eval $ac_compile) 2>conftest.er1
+  ac_status=$?
+  grep -v '^ *+' conftest.er1 >conftest.err
+  rm -f conftest.er1
+  cat conftest.err >&5
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } &&
+        { ac_try='test -z "$ac_c_werror_flag"                   || test ! -s conftest.err'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; } &&
+        { ac_try='test -s conftest.$ac_objext'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; }; then
+  ac_cv_prog_cc_g=yes
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+ac_cv_prog_cc_g=no
+fi
+rm -f conftest.err conftest.$ac_objext conftest.$ac_ext
+fi
+echo "$as_me:$LINENO: result: $ac_cv_prog_cc_g" >&5
+echo "${ECHO_T}$ac_cv_prog_cc_g" >&6
+if test "$ac_test_CFLAGS" = set; then
+  CFLAGS=$ac_save_CFLAGS
+elif test $ac_cv_prog_cc_g = yes; then
+  if test "$GCC" = yes; then
+    CFLAGS="-g -O2"
+  else
+    CFLAGS="-g"
+  fi
+else
+  if test "$GCC" = yes; then
+    CFLAGS="-O2"
+  else
+    CFLAGS=
+  fi
+fi
+echo "$as_me:$LINENO: checking for $CC option to accept ANSI C" >&5
+echo $ECHO_N "checking for $CC option to accept ANSI C... $ECHO_C" >&6
+if test "${ac_cv_prog_cc_stdc+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  ac_cv_prog_cc_stdc=no
+ac_save_CC=$CC
+cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+#include <stdarg.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+/* Most of the following tests are stolen from RCS 5.7's src/conf.sh.  */
+struct buf { int x; };
+FILE * (*rcsopen) (struct buf *, struct stat *, int);
+static char *e (p, i)
+     char **p;
+     int i;
+{
+  return p[i];
+}
+static char *f (char * (*g) (char **, int), char **p, ...)
+{
+  char *s;
+  va_list v;
+  va_start (v,p);
+  s = g (p, va_arg (v,int));
+  va_end (v);
+  return s;
+}
+
+/* OSF 4.0 Compaq cc is some sort of almost-ANSI by default.  It has
+   function prototypes and stuff, but not '\xHH' hex character constants.
+   These don't provoke an error unfortunately, instead are silently treated
+   as 'x'.  The following induces an error, until -std1 is added to get
+   proper ANSI mode.  Curiously '\x00'!='x' always comes out true, for an
+   array size at least.  It's necessary to write '\x00'==0 to get something
+   that's true only with -std1.  */
+int osf4_cc_array ['\x00' == 0 ? 1 : -1];
+
+int test (int i, double x);
+struct s1 {int (*f) (int a);};
+struct s2 {int (*f) (double a);};
+int pairnames (int, char **, FILE *(*)(struct buf *, struct stat *, int), int, int);
+int argc;
+char **argv;
+int
+main ()
+{
+return f (e, argv, 0) != argv[0]  ||  f (e, argv, 1) != argv[1];
+  ;
+  return 0;
+}
+_ACEOF
+# Don't try gcc -ansi; that turns off useful extensions and
+# breaks some systems' header files.
+# AIX                  -qlanglvl=ansi
+# Ultrix and OSF/1     -std1
+# HP-UX 10.20 and later        -Ae
+# HP-UX older versions -Aa -D_HPUX_SOURCE
+# SVR4                 -Xc -D__EXTENSIONS__
+for ac_arg in "" -qlanglvl=ansi -std1 -Ae "-Aa -D_HPUX_SOURCE" "-Xc -D__EXTENSIONS__"
+do
+  CC="$ac_save_CC $ac_arg"
+  rm -f conftest.$ac_objext
+if { (eval echo "$as_me:$LINENO: \"$ac_compile\"") >&5
+  (eval $ac_compile) 2>conftest.er1
+  ac_status=$?
+  grep -v '^ *+' conftest.er1 >conftest.err
+  rm -f conftest.er1
+  cat conftest.err >&5
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } &&
+        { ac_try='test -z "$ac_c_werror_flag"                   || test ! -s conftest.err'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; } &&
+        { ac_try='test -s conftest.$ac_objext'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; }; then
+  ac_cv_prog_cc_stdc=$ac_arg
+break
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+fi
+rm -f conftest.err conftest.$ac_objext
+done
+rm -f conftest.$ac_ext conftest.$ac_objext
+CC=$ac_save_CC
+
+fi
+
+case "x$ac_cv_prog_cc_stdc" in
+  x|xno)
+    echo "$as_me:$LINENO: result: none needed" >&5
+echo "${ECHO_T}none needed" >&6 ;;
+  *)
+    echo "$as_me:$LINENO: result: $ac_cv_prog_cc_stdc" >&5
+echo "${ECHO_T}$ac_cv_prog_cc_stdc" >&6
+    CC="$CC $ac_cv_prog_cc_stdc" ;;
+esac
+
+# Some people use a C++ compiler to compile C.  Since we use `exit',
+# in C++ we need to declare it.  In case someone uses the same compiler
+# for both compiling C and C++ we need to have the C++ compiler decide
+# the declaration of exit, since it's the most demanding environment.
+cat >conftest.$ac_ext <<_ACEOF
+#ifndef __cplusplus
+  choke me
+#endif
+_ACEOF
+rm -f conftest.$ac_objext
+if { (eval echo "$as_me:$LINENO: \"$ac_compile\"") >&5
+  (eval $ac_compile) 2>conftest.er1
+  ac_status=$?
+  grep -v '^ *+' conftest.er1 >conftest.err
+  rm -f conftest.er1
+  cat conftest.err >&5
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } &&
+        { ac_try='test -z "$ac_c_werror_flag"                   || test ! -s conftest.err'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; } &&
+        { ac_try='test -s conftest.$ac_objext'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; }; then
+  for ac_declaration in \
+   '' \
+   'extern "C" void std::exit (int) throw (); using std::exit;' \
+   'extern "C" void std::exit (int); using std::exit;' \
+   'extern "C" void exit (int) throw ();' \
+   'extern "C" void exit (int);' \
+   'void exit (int);'
+do
+  cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+$ac_declaration
+#include <stdlib.h>
+int
+main ()
+{
+exit (42);
+  ;
+  return 0;
+}
+_ACEOF
+rm -f conftest.$ac_objext
+if { (eval echo "$as_me:$LINENO: \"$ac_compile\"") >&5
+  (eval $ac_compile) 2>conftest.er1
+  ac_status=$?
+  grep -v '^ *+' conftest.er1 >conftest.err
+  rm -f conftest.er1
+  cat conftest.err >&5
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } &&
+        { ac_try='test -z "$ac_c_werror_flag"                   || test ! -s conftest.err'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; } &&
+        { ac_try='test -s conftest.$ac_objext'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; }; then
+  :
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+continue
+fi
+rm -f conftest.err conftest.$ac_objext conftest.$ac_ext
+  cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+$ac_declaration
+int
+main ()
+{
+exit (42);
+  ;
+  return 0;
+}
+_ACEOF
+rm -f conftest.$ac_objext
+if { (eval echo "$as_me:$LINENO: \"$ac_compile\"") >&5
+  (eval $ac_compile) 2>conftest.er1
+  ac_status=$?
+  grep -v '^ *+' conftest.er1 >conftest.err
+  rm -f conftest.er1
+  cat conftest.err >&5
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } &&
+        { ac_try='test -z "$ac_c_werror_flag"                   || test ! -s conftest.err'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; } &&
+        { ac_try='test -s conftest.$ac_objext'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; }; then
+  break
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+fi
+rm -f conftest.err conftest.$ac_objext conftest.$ac_ext
+done
+rm -f conftest*
+if test -n "$ac_declaration"; then
+  echo '#ifdef __cplusplus' >>confdefs.h
+  echo $ac_declaration      >>confdefs.h
+  echo '#endif'             >>confdefs.h
+fi
+
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+fi
+rm -f conftest.err conftest.$ac_objext conftest.$ac_ext
+ac_ext=c
+ac_cpp='$CPP $CPPFLAGS'
+ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
+ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
+ac_compiler_gnu=$ac_cv_c_compiler_gnu
+
+ac_aux_dir=
+for ac_dir in $srcdir $srcdir/.. $srcdir/../..; do
+  if test -f $ac_dir/install-sh; then
+    ac_aux_dir=$ac_dir
+    ac_install_sh="$ac_aux_dir/install-sh -c"
+    break
+  elif test -f $ac_dir/install.sh; then
+    ac_aux_dir=$ac_dir
+    ac_install_sh="$ac_aux_dir/install.sh -c"
+    break
+  elif test -f $ac_dir/shtool; then
+    ac_aux_dir=$ac_dir
+    ac_install_sh="$ac_aux_dir/shtool install -c"
+    break
+  fi
+done
+if test -z "$ac_aux_dir"; then
+  { { echo "$as_me:$LINENO: error: cannot find install-sh or install.sh in $srcdir $srcdir/.. $srcdir/../.." >&5
+echo "$as_me: error: cannot find install-sh or install.sh in $srcdir $srcdir/.. $srcdir/../.." >&2;}
+   { (exit 1); exit 1; }; }
+fi
+ac_config_guess="$SHELL $ac_aux_dir/config.guess"
+ac_config_sub="$SHELL $ac_aux_dir/config.sub"
+ac_configure="$SHELL $ac_aux_dir/configure" # This should be Cygnus configure.
+
+# Find a good install program.  We prefer a C program (faster),
+# so one script is as good as another.  But avoid the broken or
+# incompatible versions:
+# SysV /etc/install, /usr/sbin/install
+# SunOS /usr/etc/install
+# IRIX /sbin/install
+# AIX /bin/install
+# AmigaOS /C/install, which installs bootblocks on floppy discs
+# AIX 4 /usr/bin/installbsd, which doesn't work without a -g flag
+# AFS /usr/afsws/bin/install, which mishandles nonexistent args
+# SVR4 /usr/ucb/install, which tries to use the nonexistent group "staff"
+# OS/2's system install, which has a completely different semantic
+# ./install, which can be erroneously created by make from ./install.sh.
+echo "$as_me:$LINENO: checking for a BSD-compatible install" >&5
+echo $ECHO_N "checking for a BSD-compatible install... $ECHO_C" >&6
+if test -z "$INSTALL"; then
+if test "${ac_cv_path_install+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
+for as_dir in $PATH
+do
+  IFS=$as_save_IFS
+  test -z "$as_dir" && as_dir=.
+  # Account for people who put trailing slashes in PATH elements.
+case $as_dir/ in
+  ./ | .// | /cC/* | \
+  /etc/* | /usr/sbin/* | /usr/etc/* | /sbin/* | /usr/afsws/bin/* | \
+  ?:\\/os2\\/install\\/* | ?:\\/OS2\\/INSTALL\\/* | \
+  /usr/ucb/* ) ;;
+  *)
+    # OSF1 and SCO ODT 3.0 have their own names for install.
+    # Don't use installbsd from OSF since it installs stuff as root
+    # by default.
+    for ac_prog in ginstall scoinst install; do
+      for ac_exec_ext in '' $ac_executable_extensions; do
+       if $as_executable_p "$as_dir/$ac_prog$ac_exec_ext"; then
+         if test $ac_prog = install &&
+           grep dspmsg "$as_dir/$ac_prog$ac_exec_ext" >/dev/null 2>&1; then
+           # AIX install.  It has an incompatible calling convention.
+           :
+         elif test $ac_prog = install &&
+           grep pwplus "$as_dir/$ac_prog$ac_exec_ext" >/dev/null 2>&1; then
+           # program-specific install script used by HP pwplus--don't use.
+           :
+         else
+           ac_cv_path_install="$as_dir/$ac_prog$ac_exec_ext -c"
+           break 3
+         fi
+       fi
+      done
+    done
+    ;;
+esac
+done
+
+
+fi
+  if test "${ac_cv_path_install+set}" = set; then
+    INSTALL=$ac_cv_path_install
+  else
+    # As a last resort, use the slow shell script.  We don't cache a
+    # path for INSTALL within a source directory, because that will
+    # break other packages using the cache if that directory is
+    # removed, or if the path is relative.
+    INSTALL=$ac_install_sh
+  fi
+fi
+echo "$as_me:$LINENO: result: $INSTALL" >&5
+echo "${ECHO_T}$INSTALL" >&6
+
+# Use test -z because SunOS4 sh mishandles braces in ${var-val}.
+# It thinks the first close brace ends the variable substitution.
+test -z "$INSTALL_PROGRAM" && INSTALL_PROGRAM='${INSTALL}'
+
+test -z "$INSTALL_SCRIPT" && INSTALL_SCRIPT='${INSTALL}'
+
+test -z "$INSTALL_DATA" && INSTALL_DATA='${INSTALL} -m 644'
+
+
+
+echo "$as_me:$LINENO: checking for an ANSI C-conforming const" >&5
+echo $ECHO_N "checking for an ANSI C-conforming const... $ECHO_C" >&6
+if test "${ac_cv_c_const+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+
+int
+main ()
+{
+/* FIXME: Include the comments suggested by Paul. */
+#ifndef __cplusplus
+  /* Ultrix mips cc rejects this.  */
+  typedef int charset[2];
+  const charset x;
+  /* SunOS 4.1.1 cc rejects this.  */
+  char const *const *ccp;
+  char **p;
+  /* NEC SVR4.0.2 mips cc rejects this.  */
+  struct point {int x, y;};
+  static struct point const zero = {0,0};
+  /* AIX XL C 1.02.0.0 rejects this.
+     It does not let you subtract one const X* pointer from another in
+     an arm of an if-expression whose if-part is not a constant
+     expression */
+  const char *g = "string";
+  ccp = &g + (g ? g-g : 0);
+  /* HPUX 7.0 cc rejects these. */
+  ++ccp;
+  p = (char**) ccp;
+  ccp = (char const *const *) p;
+  { /* SCO 3.2v4 cc rejects this.  */
+    char *t;
+    char const *s = 0 ? (char *) 0 : (char const *) 0;
+
+    *t++ = 0;
+  }
+  { /* Someone thinks the Sun supposedly-ANSI compiler will reject this.  */
+    int x[] = {25, 17};
+    const int *foo = &x[0];
+    ++foo;
+  }
+  { /* Sun SC1.0 ANSI compiler rejects this -- but not the above. */
+    typedef const int *iptr;
+    iptr p = 0;
+    ++p;
+  }
+  { /* AIX XL C 1.02.0.0 rejects this saying
+       "k.c", line 2.27: 1506-025 (S) Operand must be a modifiable lvalue. */
+    struct s { int j; const int *ap[3]; };
+    struct s *b; b->j = 5;
+  }
+  { /* ULTRIX-32 V3.1 (Rev 9) vcc rejects this */
+    const int foo = 10;
+  }
+#endif
+
+  ;
+  return 0;
+}
+_ACEOF
+rm -f conftest.$ac_objext
+if { (eval echo "$as_me:$LINENO: \"$ac_compile\"") >&5
+  (eval $ac_compile) 2>conftest.er1
+  ac_status=$?
+  grep -v '^ *+' conftest.er1 >conftest.err
+  rm -f conftest.er1
+  cat conftest.err >&5
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } &&
+        { ac_try='test -z "$ac_c_werror_flag"                   || test ! -s conftest.err'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; } &&
+        { ac_try='test -s conftest.$ac_objext'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; }; then
+  ac_cv_c_const=yes
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+ac_cv_c_const=no
+fi
+rm -f conftest.err conftest.$ac_objext conftest.$ac_ext
+fi
+echo "$as_me:$LINENO: result: $ac_cv_c_const" >&5
+echo "${ECHO_T}$ac_cv_c_const" >&6
+if test $ac_cv_c_const = no; then
+
+cat >>confdefs.h <<\_ACEOF
+#define const
+_ACEOF
+
+fi
+
+echo "$as_me:$LINENO: checking for inline" >&5
+echo $ECHO_N "checking for inline... $ECHO_C" >&6
+if test "${ac_cv_c_inline+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  ac_cv_c_inline=no
+for ac_kw in inline __inline__ __inline; do
+  cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+#ifndef __cplusplus
+typedef int foo_t;
+static $ac_kw foo_t static_foo () {return 0; }
+$ac_kw foo_t foo () {return 0; }
+#endif
+
+_ACEOF
+rm -f conftest.$ac_objext
+if { (eval echo "$as_me:$LINENO: \"$ac_compile\"") >&5
+  (eval $ac_compile) 2>conftest.er1
+  ac_status=$?
+  grep -v '^ *+' conftest.er1 >conftest.err
+  rm -f conftest.er1
+  cat conftest.err >&5
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } &&
+        { ac_try='test -z "$ac_c_werror_flag"                   || test ! -s conftest.err'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; } &&
+        { ac_try='test -s conftest.$ac_objext'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; }; then
+  ac_cv_c_inline=$ac_kw; break
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+fi
+rm -f conftest.err conftest.$ac_objext conftest.$ac_ext
+done
+
+fi
+echo "$as_me:$LINENO: result: $ac_cv_c_inline" >&5
+echo "${ECHO_T}$ac_cv_c_inline" >&6
+
+
+case $ac_cv_c_inline in
+  inline | yes) ;;
+  *)
+    case $ac_cv_c_inline in
+      no) ac_val=;;
+      *) ac_val=$ac_cv_c_inline;;
+    esac
+    cat >>confdefs.h <<_ACEOF
+#ifndef __cplusplus
+#define inline $ac_val
+#endif
+_ACEOF
+    ;;
+esac
+
+
+#####################################################################
+##
+## libm check
+##
+#####################################################################
+M="No "
+echo "$as_me:$LINENO: checking for pow in -lm" >&5
+echo $ECHO_N "checking for pow in -lm... $ECHO_C" >&6
+if test "${ac_cv_lib_m_pow+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  ac_check_lib_save_LIBS=$LIBS
+LIBS="-lm  $LIBS"
+cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+
+/* Override any gcc2 internal prototype to avoid an error.  */
+#ifdef __cplusplus
+extern "C"
+#endif
+/* We use char because int might match the return type of a gcc2
+   builtin and then its argument prototype would still apply.  */
+char pow ();
+int
+main ()
+{
+pow ();
+  ;
+  return 0;
+}
+_ACEOF
+rm -f conftest.$ac_objext conftest$ac_exeext
+if { (eval echo "$as_me:$LINENO: \"$ac_link\"") >&5
+  (eval $ac_link) 2>conftest.er1
+  ac_status=$?
+  grep -v '^ *+' conftest.er1 >conftest.err
+  rm -f conftest.er1
+  cat conftest.err >&5
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } &&
+        { ac_try='test -z "$ac_c_werror_flag"                   || test ! -s conftest.err'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; } &&
+        { ac_try='test -s conftest$ac_exeext'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; }; then
+  ac_cv_lib_m_pow=yes
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+ac_cv_lib_m_pow=no
+fi
+rm -f conftest.err conftest.$ac_objext \
+      conftest$ac_exeext conftest.$ac_ext
+LIBS=$ac_check_lib_save_LIBS
+fi
+echo "$as_me:$LINENO: result: $ac_cv_lib_m_pow" >&5
+echo "${ECHO_T}$ac_cv_lib_m_pow" >&6
+if test $ac_cv_lib_m_pow = yes; then
+
+  LIBM="-lm"
+  M="Yes"
+
+else
+
+    echo
+    echo "*** Error: libm required ***"
+    echo
+    exit
+
+fi
+
+
+#####################################################################
+##
+## verbose error strings
+##
+#####################################################################
+# Check whether --enable-verbose-errors or --disable-verbose-errors was given.
+if test "${enable_verbose_errors+set}" = set; then
+  enableval="$enable_verbose_errors"
+
+       if test x$enableval = xyes; then
+
+cat >>confdefs.h <<_ACEOF
+#define VERBOSE_ERRORS "1"
+_ACEOF
+
+       fi
+
+fi;
+
+#####################################################################
+##
+## compile decisions
+##
+#####################################################################
+COMPILE_LIBNL="Yes "
+LIBNL_LIB="$LIBM"
+
+
+
+                    ac_config_files="$ac_config_files Makefile.opts libnl-1.pc"
+cat >confcache <<\_ACEOF
+# This file is a shell script that caches the results of configure
+# tests run on this system so they can be shared between configure
+# scripts and configure runs, see configure's option --config-cache.
+# It is not useful on other systems.  If it contains results you don't
+# want to keep, you may remove or edit it.
+#
+# config.status only pays attention to the cache file if you give it
+# the --recheck option to rerun configure.
+#
+# `ac_cv_env_foo' variables (set or unset) will be overridden when
+# loading this file, other *unset* `ac_cv_foo' will be assigned the
+# following values.
+
+_ACEOF
+
+# The following way of writing the cache mishandles newlines in values,
+# but we know of no workaround that is simple, portable, and efficient.
+# So, don't put newlines in cache variables' values.
+# Ultrix sh set writes to stderr and can't be redirected directly,
+# and sets the high bit in the cache file unless we assign to the vars.
+{
+  (set) 2>&1 |
+    case `(ac_space=' '; set | grep ac_space) 2>&1` in
+    *ac_space=\ *)
+      # `set' does not quote correctly, so add quotes (double-quote
+      # substitution turns \\\\ into \\, and sed turns \\ into \).
+      sed -n \
+       "s/'/'\\\\''/g;
+         s/^\\([_$as_cr_alnum]*_cv_[_$as_cr_alnum]*\\)=\\(.*\\)/\\1='\\2'/p"
+      ;;
+    *)
+      # `set' quotes correctly as required by POSIX, so do not add quotes.
+      sed -n \
+       "s/^\\([_$as_cr_alnum]*_cv_[_$as_cr_alnum]*\\)=\\(.*\\)/\\1=\\2/p"
+      ;;
+    esac;
+} |
+  sed '
+     t clear
+     : clear
+     s/^\([^=]*\)=\(.*[{}].*\)$/test "${\1+set}" = set || &/
+     t end
+     /^ac_cv_env/!s/^\([^=]*\)=\(.*\)$/\1=${\1=\2}/
+     : end' >>confcache
+if diff $cache_file confcache >/dev/null 2>&1; then :; else
+  if test -w $cache_file; then
+    test "x$cache_file" != "x/dev/null" && echo "updating cache $cache_file"
+    cat confcache >$cache_file
+  else
+    echo "not updating unwritable cache $cache_file"
+  fi
+fi
+rm -f confcache
+
+test "x$prefix" = xNONE && prefix=$ac_default_prefix
+# Let make expand exec_prefix.
+test "x$exec_prefix" = xNONE && exec_prefix='${prefix}'
+
+# VPATH may cause trouble with some makes, so we remove $(srcdir),
+# ${srcdir} and @srcdir@ from VPATH if srcdir is ".", strip leading and
+# trailing colons and then remove the whole line if VPATH becomes empty
+# (actually we leave an empty line to preserve line numbers).
+if test "x$srcdir" = x.; then
+  ac_vpsub='/^[         ]*VPATH[        ]*=/{
+s/:*\$(srcdir):*/:/;
+s/:*\${srcdir}:*/:/;
+s/:*@srcdir@:*/:/;
+s/^\([^=]*=[    ]*\):*/\1/;
+s/:*$//;
+s/^[^=]*=[      ]*$//;
+}'
+fi
+
+DEFS=-DHAVE_CONFIG_H
+
+ac_libobjs=
+ac_ltlibobjs=
+for ac_i in : $LIBOBJS; do test "x$ac_i" = x: && continue
+  # 1. Remove the extension, and $U if already installed.
+  ac_i=`echo "$ac_i" |
+        sed 's/\$U\././;s/\.o$//;s/\.obj$//'`
+  # 2. Add them.
+  ac_libobjs="$ac_libobjs $ac_i\$U.$ac_objext"
+  ac_ltlibobjs="$ac_ltlibobjs $ac_i"'$U.lo'
+done
+LIBOBJS=$ac_libobjs
+
+LTLIBOBJS=$ac_ltlibobjs
+
+
+
+: ${CONFIG_STATUS=./config.status}
+ac_clean_files_save=$ac_clean_files
+ac_clean_files="$ac_clean_files $CONFIG_STATUS"
+{ echo "$as_me:$LINENO: creating $CONFIG_STATUS" >&5
+echo "$as_me: creating $CONFIG_STATUS" >&6;}
+cat >$CONFIG_STATUS <<_ACEOF
+#! $SHELL
+# Generated by $as_me.
+# Run this file to recreate the current configuration.
+# Compiler output produced by configure, useful for debugging
+# configure, is in config.log if it exists.
+
+debug=false
+ac_cs_recheck=false
+ac_cs_silent=false
+SHELL=\${CONFIG_SHELL-$SHELL}
+_ACEOF
+
+cat >>$CONFIG_STATUS <<\_ACEOF
+## --------------------- ##
+## M4sh Initialization.  ##
+## --------------------- ##
+
+# Be Bourne compatible
+if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then
+  emulate sh
+  NULLCMD=:
+  # Zsh 3.x and 4.x performs word splitting on ${1+"$@"}, which
+  # is contrary to our usage.  Disable this feature.
+  alias -g '${1+"$@"}'='"$@"'
+elif test -n "${BASH_VERSION+set}" && (set -o posix) >/dev/null 2>&1; then
+  set -o posix
+fi
+DUALCASE=1; export DUALCASE # for MKS sh
+
+# Support unset when possible.
+if ( (MAIL=60; unset MAIL) || exit) >/dev/null 2>&1; then
+  as_unset=unset
+else
+  as_unset=false
+fi
+
+
+# Work around bugs in pre-3.0 UWIN ksh.
+$as_unset ENV MAIL MAILPATH
+PS1='$ '
+PS2='> '
+PS4='+ '
+
+# NLS nuisances.
+for as_var in \
+  LANG LANGUAGE LC_ADDRESS LC_ALL LC_COLLATE LC_CTYPE LC_IDENTIFICATION \
+  LC_MEASUREMENT LC_MESSAGES LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER \
+  LC_TELEPHONE LC_TIME
+do
+  if (set +x; test -z "`(eval $as_var=C; export $as_var) 2>&1`"); then
+    eval $as_var=C; export $as_var
+  else
+    $as_unset $as_var
+  fi
+done
+
+# Required to use basename.
+if expr a : '\(a\)' >/dev/null 2>&1; then
+  as_expr=expr
+else
+  as_expr=false
+fi
+
+if (basename /) >/dev/null 2>&1 && test "X`basename / 2>&1`" = "X/"; then
+  as_basename=basename
+else
+  as_basename=false
+fi
+
+
+# Name of the executable.
+as_me=`$as_basename "$0" ||
+$as_expr X/"$0" : '.*/\([^/][^/]*\)/*$' \| \
+        X"$0" : 'X\(//\)$' \| \
+        X"$0" : 'X\(/\)$' \| \
+        .     : '\(.\)' 2>/dev/null ||
+echo X/"$0" |
+    sed '/^.*\/\([^/][^/]*\)\/*$/{ s//\1/; q; }
+         /^X\/\(\/\/\)$/{ s//\1/; q; }
+         /^X\/\(\/\).*/{ s//\1/; q; }
+         s/.*/./; q'`
+
+
+# PATH needs CR, and LINENO needs CR and PATH.
+# Avoid depending upon Character Ranges.
+as_cr_letters='abcdefghijklmnopqrstuvwxyz'
+as_cr_LETTERS='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
+as_cr_Letters=$as_cr_letters$as_cr_LETTERS
+as_cr_digits='0123456789'
+as_cr_alnum=$as_cr_Letters$as_cr_digits
+
+# The user is always right.
+if test "${PATH_SEPARATOR+set}" != set; then
+  echo "#! /bin/sh" >conf$$.sh
+  echo  "exit 0"   >>conf$$.sh
+  chmod +x conf$$.sh
+  if (PATH="/nonexistent;."; conf$$.sh) >/dev/null 2>&1; then
+    PATH_SEPARATOR=';'
+  else
+    PATH_SEPARATOR=:
+  fi
+  rm -f conf$$.sh
+fi
+
+
+  as_lineno_1=$LINENO
+  as_lineno_2=$LINENO
+  as_lineno_3=`(expr $as_lineno_1 + 1) 2>/dev/null`
+  test "x$as_lineno_1" != "x$as_lineno_2" &&
+  test "x$as_lineno_3"  = "x$as_lineno_2"  || {
+  # Find who we are.  Look in the path if we contain no path at all
+  # relative or not.
+  case $0 in
+    *[\\/]* ) as_myself=$0 ;;
+    *) as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
+for as_dir in $PATH
+do
+  IFS=$as_save_IFS
+  test -z "$as_dir" && as_dir=.
+  test -r "$as_dir/$0" && as_myself=$as_dir/$0 && break
+done
+
+       ;;
+  esac
+  # We did not find ourselves, most probably we were run as `sh COMMAND'
+  # in which case we are not to be found in the path.
+  if test "x$as_myself" = x; then
+    as_myself=$0
+  fi
+  if test ! -f "$as_myself"; then
+    { { echo "$as_me:$LINENO: error: cannot find myself; rerun with an absolute path" >&5
+echo "$as_me: error: cannot find myself; rerun with an absolute path" >&2;}
+   { (exit 1); exit 1; }; }
+  fi
+  case $CONFIG_SHELL in
+  '')
+    as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
+for as_dir in /bin$PATH_SEPARATOR/usr/bin$PATH_SEPARATOR$PATH
+do
+  IFS=$as_save_IFS
+  test -z "$as_dir" && as_dir=.
+  for as_base in sh bash ksh sh5; do
+        case $as_dir in
+        /*)
+          if ("$as_dir/$as_base" -c '
+  as_lineno_1=$LINENO
+  as_lineno_2=$LINENO
+  as_lineno_3=`(expr $as_lineno_1 + 1) 2>/dev/null`
+  test "x$as_lineno_1" != "x$as_lineno_2" &&
+  test "x$as_lineno_3"  = "x$as_lineno_2" ') 2>/dev/null; then
+            $as_unset BASH_ENV || test "${BASH_ENV+set}" != set || { BASH_ENV=; export BASH_ENV; }
+            $as_unset ENV || test "${ENV+set}" != set || { ENV=; export ENV; }
+            CONFIG_SHELL=$as_dir/$as_base
+            export CONFIG_SHELL
+            exec "$CONFIG_SHELL" "$0" ${1+"$@"}
+          fi;;
+        esac
+       done
+done
+;;
+  esac
+
+  # Create $as_me.lineno as a copy of $as_myself, but with $LINENO
+  # uniformly replaced by the line number.  The first 'sed' inserts a
+  # line-number line before each line; the second 'sed' does the real
+  # work.  The second script uses 'N' to pair each line-number line
+  # with the numbered line, and appends trailing '-' during
+  # substitution so that $LINENO is not a special case at line end.
+  # (Raja R Harinath suggested sed '=', and Paul Eggert wrote the
+  # second 'sed' script.  Blame Lee E. McMahon for sed's syntax.  :-)
+  sed '=' <$as_myself |
+    sed '
+      N
+      s,$,-,
+      : loop
+      s,^\(['$as_cr_digits']*\)\(.*\)[$]LINENO\([^'$as_cr_alnum'_]\),\1\2\1\3,
+      t loop
+      s,-$,,
+      s,^['$as_cr_digits']*\n,,
+    ' >$as_me.lineno &&
+  chmod +x $as_me.lineno ||
+    { { echo "$as_me:$LINENO: error: cannot create $as_me.lineno; rerun with a POSIX shell" >&5
+echo "$as_me: error: cannot create $as_me.lineno; rerun with a POSIX shell" >&2;}
+   { (exit 1); exit 1; }; }
+
+  # Don't try to exec as it changes $[0], causing all sort of problems
+  # (the dirname of $[0] is not the place where we might find the
+  # original and so on.  Autoconf is especially sensible to this).
+  . ./$as_me.lineno
+  # Exit status is that of the last command.
+  exit
+}
+
+
+case `echo "testing\c"; echo 1,2,3`,`echo -n testing; echo 1,2,3` in
+  *c*,-n*) ECHO_N= ECHO_C='
+' ECHO_T='     ' ;;
+  *c*,*  ) ECHO_N=-n ECHO_C= ECHO_T= ;;
+  *)       ECHO_N= ECHO_C='\c' ECHO_T= ;;
+esac
+
+if expr a : '\(a\)' >/dev/null 2>&1; then
+  as_expr=expr
+else
+  as_expr=false
+fi
+
+rm -f conf$$ conf$$.exe conf$$.file
+echo >conf$$.file
+if ln -s conf$$.file conf$$ 2>/dev/null; then
+  # We could just check for DJGPP; but this test a) works b) is more generic
+  # and c) will remain valid once DJGPP supports symlinks (DJGPP 2.04).
+  if test -f conf$$.exe; then
+    # Don't use ln at all; we don't have any links
+    as_ln_s='cp -p'
+  else
+    as_ln_s='ln -s'
+  fi
+elif ln conf$$.file conf$$ 2>/dev/null; then
+  as_ln_s=ln
+else
+  as_ln_s='cp -p'
+fi
+rm -f conf$$ conf$$.exe conf$$.file
+
+if mkdir -p . 2>/dev/null; then
+  as_mkdir_p=:
+else
+  test -d ./-p && rmdir ./-p
+  as_mkdir_p=false
+fi
+
+as_executable_p="test -f"
+
+# Sed expression to map a string onto a valid CPP name.
+as_tr_cpp="eval sed 'y%*$as_cr_letters%P$as_cr_LETTERS%;s%[^_$as_cr_alnum]%_%g'"
+
+# Sed expression to map a string onto a valid variable name.
+as_tr_sh="eval sed 'y%*+%pp%;s%[^_$as_cr_alnum]%_%g'"
+
+
+# IFS
+# We need space, tab and new line, in precisely that order.
+as_nl='
+'
+IFS="  $as_nl"
+
+# CDPATH.
+$as_unset CDPATH
+
+exec 6>&1
+
+# Open the log real soon, to keep \$[0] and so on meaningful, and to
+# report actual input values of CONFIG_FILES etc. instead of their
+# values after options handling.  Logging --version etc. is OK.
+exec 5>>config.log
+{
+  echo
+  sed 'h;s/./-/g;s/^.../## /;s/...$/ ##/;p;x;p;x' <<_ASBOX
+## Running $as_me. ##
+_ASBOX
+} >&5
+cat >&5 <<_CSEOF
+
+This file was extended by libnl $as_me 1.0-pre6, which was
+generated by GNU Autoconf 2.59.  Invocation command line was
+
+  CONFIG_FILES    = $CONFIG_FILES
+  CONFIG_HEADERS  = $CONFIG_HEADERS
+  CONFIG_LINKS    = $CONFIG_LINKS
+  CONFIG_COMMANDS = $CONFIG_COMMANDS
+  $ $0 $@
+
+_CSEOF
+echo "on `(hostname || uname -n) 2>/dev/null | sed 1q`" >&5
+echo >&5
+_ACEOF
+
+# Files that config.status was made for.
+if test -n "$ac_config_files"; then
+  echo "config_files=\"$ac_config_files\"" >>$CONFIG_STATUS
+fi
+
+if test -n "$ac_config_headers"; then
+  echo "config_headers=\"$ac_config_headers\"" >>$CONFIG_STATUS
+fi
+
+if test -n "$ac_config_links"; then
+  echo "config_links=\"$ac_config_links\"" >>$CONFIG_STATUS
+fi
+
+if test -n "$ac_config_commands"; then
+  echo "config_commands=\"$ac_config_commands\"" >>$CONFIG_STATUS
+fi
+
+cat >>$CONFIG_STATUS <<\_ACEOF
+
+ac_cs_usage="\
+\`$as_me' instantiates files from templates according to the
+current configuration.
+
+Usage: $0 [OPTIONS] [FILE]...
+
+  -h, --help       print this help, then exit
+  -V, --version    print version number, then exit
+  -q, --quiet      do not print progress messages
+  -d, --debug      don't remove temporary files
+      --recheck    update $as_me by reconfiguring in the same conditions
+  --file=FILE[:TEMPLATE]
+                  instantiate the configuration file FILE
+  --header=FILE[:TEMPLATE]
+                  instantiate the configuration header FILE
+
+Configuration files:
+$config_files
+
+Configuration headers:
+$config_headers
+
+Report bugs to <bug-autoconf@gnu.org>."
+_ACEOF
+
+cat >>$CONFIG_STATUS <<_ACEOF
+ac_cs_version="\\
+libnl config.status 1.0-pre6
+configured by $0, generated by GNU Autoconf 2.59,
+  with options \\"`echo "$ac_configure_args" | sed 's/[\\""\`\$]/\\\\&/g'`\\"
+
+Copyright (C) 2003 Free Software Foundation, Inc.
+This config.status script is free software; the Free Software Foundation
+gives unlimited permission to copy, distribute and modify it."
+srcdir=$srcdir
+INSTALL="$INSTALL"
+_ACEOF
+
+cat >>$CONFIG_STATUS <<\_ACEOF
+# If no file are specified by the user, then we need to provide default
+# value.  By we need to know if files were specified by the user.
+ac_need_defaults=:
+while test $# != 0
+do
+  case $1 in
+  --*=*)
+    ac_option=`expr "x$1" : 'x\([^=]*\)='`
+    ac_optarg=`expr "x$1" : 'x[^=]*=\(.*\)'`
+    ac_shift=:
+    ;;
+  -*)
+    ac_option=$1
+    ac_optarg=$2
+    ac_shift=shift
+    ;;
+  *) # This is not an option, so the user has probably given explicit
+     # arguments.
+     ac_option=$1
+     ac_need_defaults=false;;
+  esac
+
+  case $ac_option in
+  # Handling of the options.
+_ACEOF
+cat >>$CONFIG_STATUS <<\_ACEOF
+  -recheck | --recheck | --rechec | --reche | --rech | --rec | --re | --r)
+    ac_cs_recheck=: ;;
+  --version | --vers* | -V )
+    echo "$ac_cs_version"; exit 0 ;;
+  --he | --h)
+    # Conflict between --help and --header
+    { { echo "$as_me:$LINENO: error: ambiguous option: $1
+Try \`$0 --help' for more information." >&5
+echo "$as_me: error: ambiguous option: $1
+Try \`$0 --help' for more information." >&2;}
+   { (exit 1); exit 1; }; };;
+  --help | --hel | -h )
+    echo "$ac_cs_usage"; exit 0 ;;
+  --debug | --d* | -d )
+    debug=: ;;
+  --file | --fil | --fi | --f )
+    $ac_shift
+    CONFIG_FILES="$CONFIG_FILES $ac_optarg"
+    ac_need_defaults=false;;
+  --header | --heade | --head | --hea )
+    $ac_shift
+    CONFIG_HEADERS="$CONFIG_HEADERS $ac_optarg"
+    ac_need_defaults=false;;
+  -q | -quiet | --quiet | --quie | --qui | --qu | --q \
+  | -silent | --silent | --silen | --sile | --sil | --si | --s)
+    ac_cs_silent=: ;;
+
+  # This is an error.
+  -*) { { echo "$as_me:$LINENO: error: unrecognized option: $1
+Try \`$0 --help' for more information." >&5
+echo "$as_me: error: unrecognized option: $1
+Try \`$0 --help' for more information." >&2;}
+   { (exit 1); exit 1; }; } ;;
+
+  *) ac_config_targets="$ac_config_targets $1" ;;
+
+  esac
+  shift
+done
+
+ac_configure_extra_args=
+
+if $ac_cs_silent; then
+  exec 6>/dev/null
+  ac_configure_extra_args="$ac_configure_extra_args --silent"
+fi
+
+_ACEOF
+cat >>$CONFIG_STATUS <<_ACEOF
+if \$ac_cs_recheck; then
+  echo "running $SHELL $0 " $ac_configure_args \$ac_configure_extra_args " --no-create --no-recursion" >&6
+  exec $SHELL $0 $ac_configure_args \$ac_configure_extra_args --no-create --no-recursion
+fi
+
+_ACEOF
+
+
+
+
+
+cat >>$CONFIG_STATUS <<\_ACEOF
+for ac_config_target in $ac_config_targets
+do
+  case "$ac_config_target" in
+  # Handling of arguments.
+  "Makefile.opts" ) CONFIG_FILES="$CONFIG_FILES Makefile.opts" ;;
+  "libnl-1.pc" ) CONFIG_FILES="$CONFIG_FILES libnl-1.pc" ;;
+  "lib/defs.h" ) CONFIG_HEADERS="$CONFIG_HEADERS lib/defs.h" ;;
+  *) { { echo "$as_me:$LINENO: error: invalid argument: $ac_config_target" >&5
+echo "$as_me: error: invalid argument: $ac_config_target" >&2;}
+   { (exit 1); exit 1; }; };;
+  esac
+done
+
+# If the user did not use the arguments to specify the items to instantiate,
+# then the envvar interface is used.  Set only those that are not.
+# We use the long form for the default assignment because of an extremely
+# bizarre bug on SunOS 4.1.3.
+if $ac_need_defaults; then
+  test "${CONFIG_FILES+set}" = set || CONFIG_FILES=$config_files
+  test "${CONFIG_HEADERS+set}" = set || CONFIG_HEADERS=$config_headers
+fi
+
+# Have a temporary directory for convenience.  Make it in the build tree
+# simply because there is no reason to put it here, and in addition,
+# creating and moving files from /tmp can sometimes cause problems.
+# Create a temporary directory, and hook for its removal unless debugging.
+$debug ||
+{
+  trap 'exit_status=$?; rm -rf $tmp && exit $exit_status' 0
+  trap '{ (exit 1); exit 1; }' 1 2 13 15
+}
+
+# Create a (secure) tmp directory for tmp files.
+
+{
+  tmp=`(umask 077 && mktemp -d -q "./confstatXXXXXX") 2>/dev/null` &&
+  test -n "$tmp" && test -d "$tmp"
+}  ||
+{
+  tmp=./confstat$$-$RANDOM
+  (umask 077 && mkdir $tmp)
+} ||
+{
+   echo "$me: cannot create a temporary directory in ." >&2
+   { (exit 1); exit 1; }
+}
+
+_ACEOF
+
+cat >>$CONFIG_STATUS <<_ACEOF
+
+#
+# CONFIG_FILES section.
+#
+
+# No need to generate the scripts if there are no CONFIG_FILES.
+# This happens for instance when ./config.status config.h
+if test -n "\$CONFIG_FILES"; then
+  # Protect against being on the right side of a sed subst in config.status.
+  sed 's/,@/@@/; s/@,/@@/; s/,;t t\$/@;t t/; /@;t t\$/s/[\\\\&,]/\\\\&/g;
+   s/@@/,@/; s/@@/@,/; s/@;t t\$/,;t t/' >\$tmp/subs.sed <<\\CEOF
+s,@SHELL@,$SHELL,;t t
+s,@PATH_SEPARATOR@,$PATH_SEPARATOR,;t t
+s,@PACKAGE_NAME@,$PACKAGE_NAME,;t t
+s,@PACKAGE_TARNAME@,$PACKAGE_TARNAME,;t t
+s,@PACKAGE_VERSION@,$PACKAGE_VERSION,;t t
+s,@PACKAGE_STRING@,$PACKAGE_STRING,;t t
+s,@PACKAGE_BUGREPORT@,$PACKAGE_BUGREPORT,;t t
+s,@exec_prefix@,$exec_prefix,;t t
+s,@prefix@,$prefix,;t t
+s,@program_transform_name@,$program_transform_name,;t t
+s,@bindir@,$bindir,;t t
+s,@sbindir@,$sbindir,;t t
+s,@libexecdir@,$libexecdir,;t t
+s,@datadir@,$datadir,;t t
+s,@sysconfdir@,$sysconfdir,;t t
+s,@sharedstatedir@,$sharedstatedir,;t t
+s,@localstatedir@,$localstatedir,;t t
+s,@libdir@,$libdir,;t t
+s,@includedir@,$includedir,;t t
+s,@oldincludedir@,$oldincludedir,;t t
+s,@infodir@,$infodir,;t t
+s,@mandir@,$mandir,;t t
+s,@build_alias@,$build_alias,;t t
+s,@host_alias@,$host_alias,;t t
+s,@target_alias@,$target_alias,;t t
+s,@DEFS@,$DEFS,;t t
+s,@ECHO_C@,$ECHO_C,;t t
+s,@ECHO_N@,$ECHO_N,;t t
+s,@ECHO_T@,$ECHO_T,;t t
+s,@LIBS@,$LIBS,;t t
+s,@CC@,$CC,;t t
+s,@CFLAGS@,$CFLAGS,;t t
+s,@LDFLAGS@,$LDFLAGS,;t t
+s,@CPPFLAGS@,$CPPFLAGS,;t t
+s,@ac_ct_CC@,$ac_ct_CC,;t t
+s,@EXEEXT@,$EXEEXT,;t t
+s,@OBJEXT@,$OBJEXT,;t t
+s,@INSTALL_PROGRAM@,$INSTALL_PROGRAM,;t t
+s,@INSTALL_SCRIPT@,$INSTALL_SCRIPT,;t t
+s,@INSTALL_DATA@,$INSTALL_DATA,;t t
+s,@LIBNL_LIB@,$LIBNL_LIB,;t t
+s,@LIBOBJS@,$LIBOBJS,;t t
+s,@LTLIBOBJS@,$LTLIBOBJS,;t t
+CEOF
+
+_ACEOF
+
+  cat >>$CONFIG_STATUS <<\_ACEOF
+  # Split the substitutions into bite-sized pieces for seds with
+  # small command number limits, like on Digital OSF/1 and HP-UX.
+  ac_max_sed_lines=48
+  ac_sed_frag=1 # Number of current file.
+  ac_beg=1 # First line for current file.
+  ac_end=$ac_max_sed_lines # Line after last line for current file.
+  ac_more_lines=:
+  ac_sed_cmds=
+  while $ac_more_lines; do
+    if test $ac_beg -gt 1; then
+      sed "1,${ac_beg}d; ${ac_end}q" $tmp/subs.sed >$tmp/subs.frag
+    else
+      sed "${ac_end}q" $tmp/subs.sed >$tmp/subs.frag
+    fi
+    if test ! -s $tmp/subs.frag; then
+      ac_more_lines=false
+    else
+      # The purpose of the label and of the branching condition is to
+      # speed up the sed processing (if there are no `@' at all, there
+      # is no need to browse any of the substitutions).
+      # These are the two extra sed commands mentioned above.
+      (echo ':t
+  /@[a-zA-Z_][a-zA-Z_0-9]*@/!b' && cat $tmp/subs.frag) >$tmp/subs-$ac_sed_frag.sed
+      if test -z "$ac_sed_cmds"; then
+       ac_sed_cmds="sed -f $tmp/subs-$ac_sed_frag.sed"
+      else
+       ac_sed_cmds="$ac_sed_cmds | sed -f $tmp/subs-$ac_sed_frag.sed"
+      fi
+      ac_sed_frag=`expr $ac_sed_frag + 1`
+      ac_beg=$ac_end
+      ac_end=`expr $ac_end + $ac_max_sed_lines`
+    fi
+  done
+  if test -z "$ac_sed_cmds"; then
+    ac_sed_cmds=cat
+  fi
+fi # test -n "$CONFIG_FILES"
+
+_ACEOF
+cat >>$CONFIG_STATUS <<\_ACEOF
+for ac_file in : $CONFIG_FILES; do test "x$ac_file" = x: && continue
+  # Support "outfile[:infile[:infile...]]", defaulting infile="outfile.in".
+  case $ac_file in
+  - | *:- | *:-:* ) # input from stdin
+       cat >$tmp/stdin
+       ac_file_in=`echo "$ac_file" | sed 's,[^:]*:,,'`
+       ac_file=`echo "$ac_file" | sed 's,:.*,,'` ;;
+  *:* ) ac_file_in=`echo "$ac_file" | sed 's,[^:]*:,,'`
+       ac_file=`echo "$ac_file" | sed 's,:.*,,'` ;;
+  * )   ac_file_in=$ac_file.in ;;
+  esac
+
+  # Compute @srcdir@, @top_srcdir@, and @INSTALL@ for subdirectories.
+  ac_dir=`(dirname "$ac_file") 2>/dev/null ||
+$as_expr X"$ac_file" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
+        X"$ac_file" : 'X\(//\)[^/]' \| \
+        X"$ac_file" : 'X\(//\)$' \| \
+        X"$ac_file" : 'X\(/\)' \| \
+        .     : '\(.\)' 2>/dev/null ||
+echo X"$ac_file" |
+    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{ s//\1/; q; }
+         /^X\(\/\/\)[^/].*/{ s//\1/; q; }
+         /^X\(\/\/\)$/{ s//\1/; q; }
+         /^X\(\/\).*/{ s//\1/; q; }
+         s/.*/./; q'`
+  { if $as_mkdir_p; then
+    mkdir -p "$ac_dir"
+  else
+    as_dir="$ac_dir"
+    as_dirs=
+    while test ! -d "$as_dir"; do
+      as_dirs="$as_dir $as_dirs"
+      as_dir=`(dirname "$as_dir") 2>/dev/null ||
+$as_expr X"$as_dir" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
+        X"$as_dir" : 'X\(//\)[^/]' \| \
+        X"$as_dir" : 'X\(//\)$' \| \
+        X"$as_dir" : 'X\(/\)' \| \
+        .     : '\(.\)' 2>/dev/null ||
+echo X"$as_dir" |
+    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{ s//\1/; q; }
+         /^X\(\/\/\)[^/].*/{ s//\1/; q; }
+         /^X\(\/\/\)$/{ s//\1/; q; }
+         /^X\(\/\).*/{ s//\1/; q; }
+         s/.*/./; q'`
+    done
+    test ! -n "$as_dirs" || mkdir $as_dirs
+  fi || { { echo "$as_me:$LINENO: error: cannot create directory \"$ac_dir\"" >&5
+echo "$as_me: error: cannot create directory \"$ac_dir\"" >&2;}
+   { (exit 1); exit 1; }; }; }
+
+  ac_builddir=.
+
+if test "$ac_dir" != .; then
+  ac_dir_suffix=/`echo "$ac_dir" | sed 's,^\.[\\/],,'`
+  # A "../" for each directory in $ac_dir_suffix.
+  ac_top_builddir=`echo "$ac_dir_suffix" | sed 's,/[^\\/]*,../,g'`
+else
+  ac_dir_suffix= ac_top_builddir=
+fi
+
+case $srcdir in
+  .)  # No --srcdir option.  We are building in place.
+    ac_srcdir=.
+    if test -z "$ac_top_builddir"; then
+       ac_top_srcdir=.
+    else
+       ac_top_srcdir=`echo $ac_top_builddir | sed 's,/$,,'`
+    fi ;;
+  [\\/]* | ?:[\\/]* )  # Absolute path.
+    ac_srcdir=$srcdir$ac_dir_suffix;
+    ac_top_srcdir=$srcdir ;;
+  *) # Relative path.
+    ac_srcdir=$ac_top_builddir$srcdir$ac_dir_suffix
+    ac_top_srcdir=$ac_top_builddir$srcdir ;;
+esac
+
+# Do not use `cd foo && pwd` to compute absolute paths, because
+# the directories may not exist.
+case `pwd` in
+.) ac_abs_builddir="$ac_dir";;
+*)
+  case "$ac_dir" in
+  .) ac_abs_builddir=`pwd`;;
+  [\\/]* | ?:[\\/]* ) ac_abs_builddir="$ac_dir";;
+  *) ac_abs_builddir=`pwd`/"$ac_dir";;
+  esac;;
+esac
+case $ac_abs_builddir in
+.) ac_abs_top_builddir=${ac_top_builddir}.;;
+*)
+  case ${ac_top_builddir}. in
+  .) ac_abs_top_builddir=$ac_abs_builddir;;
+  [\\/]* | ?:[\\/]* ) ac_abs_top_builddir=${ac_top_builddir}.;;
+  *) ac_abs_top_builddir=$ac_abs_builddir/${ac_top_builddir}.;;
+  esac;;
+esac
+case $ac_abs_builddir in
+.) ac_abs_srcdir=$ac_srcdir;;
+*)
+  case $ac_srcdir in
+  .) ac_abs_srcdir=$ac_abs_builddir;;
+  [\\/]* | ?:[\\/]* ) ac_abs_srcdir=$ac_srcdir;;
+  *) ac_abs_srcdir=$ac_abs_builddir/$ac_srcdir;;
+  esac;;
+esac
+case $ac_abs_builddir in
+.) ac_abs_top_srcdir=$ac_top_srcdir;;
+*)
+  case $ac_top_srcdir in
+  .) ac_abs_top_srcdir=$ac_abs_builddir;;
+  [\\/]* | ?:[\\/]* ) ac_abs_top_srcdir=$ac_top_srcdir;;
+  *) ac_abs_top_srcdir=$ac_abs_builddir/$ac_top_srcdir;;
+  esac;;
+esac
+
+
+  case $INSTALL in
+  [\\/$]* | ?:[\\/]* ) ac_INSTALL=$INSTALL ;;
+  *) ac_INSTALL=$ac_top_builddir$INSTALL ;;
+  esac
+
+  # Let's still pretend it is `configure' which instantiates (i.e., don't
+  # use $as_me), people would be surprised to read:
+  #    /* config.h.  Generated by config.status.  */
+  if test x"$ac_file" = x-; then
+    configure_input=
+  else
+    configure_input="$ac_file.  "
+  fi
+  configure_input=$configure_input"Generated from `echo $ac_file_in |
+                                    sed 's,.*/,,'` by configure."
+
+  # First look for the input files in the build tree, otherwise in the
+  # src tree.
+  ac_file_inputs=`IFS=:
+    for f in $ac_file_in; do
+      case $f in
+      -) echo $tmp/stdin ;;
+      [\\/$]*)
+        # Absolute (can't be DOS-style, as IFS=:)
+        test -f "$f" || { { echo "$as_me:$LINENO: error: cannot find input file: $f" >&5
+echo "$as_me: error: cannot find input file: $f" >&2;}
+   { (exit 1); exit 1; }; }
+        echo "$f";;
+      *) # Relative
+        if test -f "$f"; then
+          # Build tree
+          echo "$f"
+        elif test -f "$srcdir/$f"; then
+          # Source tree
+          echo "$srcdir/$f"
+        else
+          # /dev/null tree
+          { { echo "$as_me:$LINENO: error: cannot find input file: $f" >&5
+echo "$as_me: error: cannot find input file: $f" >&2;}
+   { (exit 1); exit 1; }; }
+        fi;;
+      esac
+    done` || { (exit 1); exit 1; }
+
+  if test x"$ac_file" != x-; then
+    { echo "$as_me:$LINENO: creating $ac_file" >&5
+echo "$as_me: creating $ac_file" >&6;}
+    rm -f "$ac_file"
+  fi
+_ACEOF
+cat >>$CONFIG_STATUS <<_ACEOF
+  sed "$ac_vpsub
+$extrasub
+_ACEOF
+cat >>$CONFIG_STATUS <<\_ACEOF
+:t
+/@[a-zA-Z_][a-zA-Z_0-9]*@/!b
+s,@configure_input@,$configure_input,;t t
+s,@srcdir@,$ac_srcdir,;t t
+s,@abs_srcdir@,$ac_abs_srcdir,;t t
+s,@top_srcdir@,$ac_top_srcdir,;t t
+s,@abs_top_srcdir@,$ac_abs_top_srcdir,;t t
+s,@builddir@,$ac_builddir,;t t
+s,@abs_builddir@,$ac_abs_builddir,;t t
+s,@top_builddir@,$ac_top_builddir,;t t
+s,@abs_top_builddir@,$ac_abs_top_builddir,;t t
+s,@INSTALL@,$ac_INSTALL,;t t
+" $ac_file_inputs | (eval "$ac_sed_cmds") >$tmp/out
+  rm -f $tmp/stdin
+  if test x"$ac_file" != x-; then
+    mv $tmp/out $ac_file
+  else
+    cat $tmp/out
+    rm -f $tmp/out
+  fi
+
+done
+_ACEOF
+cat >>$CONFIG_STATUS <<\_ACEOF
+
+#
+# CONFIG_HEADER section.
+#
+
+# These sed commands are passed to sed as "A NAME B NAME C VALUE D", where
+# NAME is the cpp macro being defined and VALUE is the value it is being given.
+#
+# ac_d sets the value in "#define NAME VALUE" lines.
+ac_dA='s,^\([   ]*\)#\([        ]*define[       ][      ]*\)'
+ac_dB='[        ].*$,\1#\2'
+ac_dC=' '
+ac_dD=',;t'
+# ac_u turns "#undef NAME" without trailing blanks into "#define NAME VALUE".
+ac_uA='s,^\([   ]*\)#\([        ]*\)undef\([    ][      ]*\)'
+ac_uB='$,\1#\2define\3'
+ac_uC=' '
+ac_uD=',;t'
+
+for ac_file in : $CONFIG_HEADERS; do test "x$ac_file" = x: && continue
+  # Support "outfile[:infile[:infile...]]", defaulting infile="outfile.in".
+  case $ac_file in
+  - | *:- | *:-:* ) # input from stdin
+       cat >$tmp/stdin
+       ac_file_in=`echo "$ac_file" | sed 's,[^:]*:,,'`
+       ac_file=`echo "$ac_file" | sed 's,:.*,,'` ;;
+  *:* ) ac_file_in=`echo "$ac_file" | sed 's,[^:]*:,,'`
+       ac_file=`echo "$ac_file" | sed 's,:.*,,'` ;;
+  * )   ac_file_in=$ac_file.in ;;
+  esac
+
+  test x"$ac_file" != x- && { echo "$as_me:$LINENO: creating $ac_file" >&5
+echo "$as_me: creating $ac_file" >&6;}
+
+  # First look for the input files in the build tree, otherwise in the
+  # src tree.
+  ac_file_inputs=`IFS=:
+    for f in $ac_file_in; do
+      case $f in
+      -) echo $tmp/stdin ;;
+      [\\/$]*)
+        # Absolute (can't be DOS-style, as IFS=:)
+        test -f "$f" || { { echo "$as_me:$LINENO: error: cannot find input file: $f" >&5
+echo "$as_me: error: cannot find input file: $f" >&2;}
+   { (exit 1); exit 1; }; }
+        # Do quote $f, to prevent DOS paths from being IFS'd.
+        echo "$f";;
+      *) # Relative
+        if test -f "$f"; then
+          # Build tree
+          echo "$f"
+        elif test -f "$srcdir/$f"; then
+          # Source tree
+          echo "$srcdir/$f"
+        else
+          # /dev/null tree
+          { { echo "$as_me:$LINENO: error: cannot find input file: $f" >&5
+echo "$as_me: error: cannot find input file: $f" >&2;}
+   { (exit 1); exit 1; }; }
+        fi;;
+      esac
+    done` || { (exit 1); exit 1; }
+  # Remove the trailing spaces.
+  sed 's/[      ]*$//' $ac_file_inputs >$tmp/in
+
+_ACEOF
+
+# Transform confdefs.h into two sed scripts, `conftest.defines' and
+# `conftest.undefs', that substitutes the proper values into
+# config.h.in to produce config.h.  The first handles `#define'
+# templates, and the second `#undef' templates.
+# And first: Protect against being on the right side of a sed subst in
+# config.status.  Protect against being in an unquoted here document
+# in config.status.
+rm -f conftest.defines conftest.undefs
+# Using a here document instead of a string reduces the quoting nightmare.
+# Putting comments in sed scripts is not portable.
+#
+# `end' is used to avoid that the second main sed command (meant for
+# 0-ary CPP macros) applies to n-ary macro definitions.
+# See the Autoconf documentation for `clear'.
+cat >confdef2sed.sed <<\_ACEOF
+s/[\\&,]/\\&/g
+s,[\\$`],\\&,g
+t clear
+: clear
+s,^[    ]*#[    ]*define[       ][      ]*\([^  (][^    (]*\)\(([^)]*)\)[       ]*\(.*\)$,${ac_dA}\1${ac_dB}\1\2${ac_dC}\3${ac_dD},gp
+t end
+s,^[    ]*#[    ]*define[       ][      ]*\([^  ][^     ]*\)[   ]*\(.*\)$,${ac_dA}\1${ac_dB}\1${ac_dC}\2${ac_dD},gp
+: end
+_ACEOF
+# If some macros were called several times there might be several times
+# the same #defines, which is useless.  Nevertheless, we may not want to
+# sort them, since we want the *last* AC-DEFINE to be honored.
+uniq confdefs.h | sed -n -f confdef2sed.sed >conftest.defines
+sed 's/ac_d/ac_u/g' conftest.defines >conftest.undefs
+rm -f confdef2sed.sed
+
+# This sed command replaces #undef with comments.  This is necessary, for
+# example, in the case of _POSIX_SOURCE, which is predefined and required
+# on some systems where configure will not decide to define it.
+cat >>conftest.undefs <<\_ACEOF
+s,^[    ]*#[    ]*undef[        ][      ]*[a-zA-Z_][a-zA-Z_0-9]*,/* & */,
+_ACEOF
+
+# Break up conftest.defines because some shells have a limit on the size
+# of here documents, and old seds have small limits too (100 cmds).
+echo '  # Handle all the #define templates only if necessary.' >>$CONFIG_STATUS
+echo '  if grep "^[     ]*#[    ]*define" $tmp/in >/dev/null; then' >>$CONFIG_STATUS
+echo '  # If there are no defines, we may have an empty if/fi' >>$CONFIG_STATUS
+echo '  :' >>$CONFIG_STATUS
+rm -f conftest.tail
+while grep . conftest.defines >/dev/null
+do
+  # Write a limited-size here document to $tmp/defines.sed.
+  echo '  cat >$tmp/defines.sed <<CEOF' >>$CONFIG_STATUS
+  # Speed up: don't consider the non `#define' lines.
+  echo '/^[     ]*#[    ]*define/!b' >>$CONFIG_STATUS
+  # Work around the forget-to-reset-the-flag bug.
+  echo 't clr' >>$CONFIG_STATUS
+  echo ': clr' >>$CONFIG_STATUS
+  sed ${ac_max_here_lines}q conftest.defines >>$CONFIG_STATUS
+  echo 'CEOF
+  sed -f $tmp/defines.sed $tmp/in >$tmp/out
+  rm -f $tmp/in
+  mv $tmp/out $tmp/in
+' >>$CONFIG_STATUS
+  sed 1,${ac_max_here_lines}d conftest.defines >conftest.tail
+  rm -f conftest.defines
+  mv conftest.tail conftest.defines
+done
+rm -f conftest.defines
+echo '  fi # grep' >>$CONFIG_STATUS
+echo >>$CONFIG_STATUS
+
+# Break up conftest.undefs because some shells have a limit on the size
+# of here documents, and old seds have small limits too (100 cmds).
+echo '  # Handle all the #undef templates' >>$CONFIG_STATUS
+rm -f conftest.tail
+while grep . conftest.undefs >/dev/null
+do
+  # Write a limited-size here document to $tmp/undefs.sed.
+  echo '  cat >$tmp/undefs.sed <<CEOF' >>$CONFIG_STATUS
+  # Speed up: don't consider the non `#undef'
+  echo '/^[     ]*#[    ]*undef/!b' >>$CONFIG_STATUS
+  # Work around the forget-to-reset-the-flag bug.
+  echo 't clr' >>$CONFIG_STATUS
+  echo ': clr' >>$CONFIG_STATUS
+  sed ${ac_max_here_lines}q conftest.undefs >>$CONFIG_STATUS
+  echo 'CEOF
+  sed -f $tmp/undefs.sed $tmp/in >$tmp/out
+  rm -f $tmp/in
+  mv $tmp/out $tmp/in
+' >>$CONFIG_STATUS
+  sed 1,${ac_max_here_lines}d conftest.undefs >conftest.tail
+  rm -f conftest.undefs
+  mv conftest.tail conftest.undefs
+done
+rm -f conftest.undefs
+
+cat >>$CONFIG_STATUS <<\_ACEOF
+  # Let's still pretend it is `configure' which instantiates (i.e., don't
+  # use $as_me), people would be surprised to read:
+  #    /* config.h.  Generated by config.status.  */
+  if test x"$ac_file" = x-; then
+    echo "/* Generated by configure.  */" >$tmp/config.h
+  else
+    echo "/* $ac_file.  Generated by configure.  */" >$tmp/config.h
+  fi
+  cat $tmp/in >>$tmp/config.h
+  rm -f $tmp/in
+  if test x"$ac_file" != x-; then
+    if diff $ac_file $tmp/config.h >/dev/null 2>&1; then
+      { echo "$as_me:$LINENO: $ac_file is unchanged" >&5
+echo "$as_me: $ac_file is unchanged" >&6;}
+    else
+      ac_dir=`(dirname "$ac_file") 2>/dev/null ||
+$as_expr X"$ac_file" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
+        X"$ac_file" : 'X\(//\)[^/]' \| \
+        X"$ac_file" : 'X\(//\)$' \| \
+        X"$ac_file" : 'X\(/\)' \| \
+        .     : '\(.\)' 2>/dev/null ||
+echo X"$ac_file" |
+    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{ s//\1/; q; }
+         /^X\(\/\/\)[^/].*/{ s//\1/; q; }
+         /^X\(\/\/\)$/{ s//\1/; q; }
+         /^X\(\/\).*/{ s//\1/; q; }
+         s/.*/./; q'`
+      { if $as_mkdir_p; then
+    mkdir -p "$ac_dir"
+  else
+    as_dir="$ac_dir"
+    as_dirs=
+    while test ! -d "$as_dir"; do
+      as_dirs="$as_dir $as_dirs"
+      as_dir=`(dirname "$as_dir") 2>/dev/null ||
+$as_expr X"$as_dir" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
+        X"$as_dir" : 'X\(//\)[^/]' \| \
+        X"$as_dir" : 'X\(//\)$' \| \
+        X"$as_dir" : 'X\(/\)' \| \
+        .     : '\(.\)' 2>/dev/null ||
+echo X"$as_dir" |
+    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{ s//\1/; q; }
+         /^X\(\/\/\)[^/].*/{ s//\1/; q; }
+         /^X\(\/\/\)$/{ s//\1/; q; }
+         /^X\(\/\).*/{ s//\1/; q; }
+         s/.*/./; q'`
+    done
+    test ! -n "$as_dirs" || mkdir $as_dirs
+  fi || { { echo "$as_me:$LINENO: error: cannot create directory \"$ac_dir\"" >&5
+echo "$as_me: error: cannot create directory \"$ac_dir\"" >&2;}
+   { (exit 1); exit 1; }; }; }
+
+      rm -f $ac_file
+      mv $tmp/config.h $ac_file
+    fi
+  else
+    cat $tmp/config.h
+    rm -f $tmp/config.h
+  fi
+done
+_ACEOF
+
+cat >>$CONFIG_STATUS <<\_ACEOF
+
+{ (exit 0); exit 0; }
+_ACEOF
+chmod +x $CONFIG_STATUS
+ac_clean_files=$ac_clean_files_save
+
+
+# configure is writing to config.log, and then calls config.status.
+# config.status does its own redirection, appending to config.log.
+# Unfortunately, on DOS this fails, as config.log is still kept open
+# by configure, so config.status won't be able to write to it; its
+# output is simply discarded.  So we exec the FD to /dev/null,
+# effectively closing config.log, so it can be properly (re)opened and
+# appended to by config.status.  When coming back to configure, we
+# need to make the FD available again.
+if test "$no_create" != yes; then
+  ac_cs_success=:
+  ac_config_status_args=
+  test "$silent" = yes &&
+    ac_config_status_args="$ac_config_status_args --quiet"
+  exec 5>/dev/null
+  $SHELL $CONFIG_STATUS $ac_config_status_args || ac_cs_success=false
+  exec 5>>config.log
+  # Use ||, not &&, to avoid exiting from the if with $? = 1, which
+  # would make configure fail if this is the last instruction.
+  $ac_cs_success || { (exit 1); exit 1; }
+fi
+
+
+#####################################################################
+##
+## status report
+##
+#####################################################################
+echo "
+----------------------------------------------------------------------
+SUMMARY:
+
+Included in Compilation:
+  libnl:   $COMPILE_LIBNL $LIBNL_LIB
+
+Dependencies:
+bmon:
+  libm             $M       (required)"
diff --git a/configure.in b/configure.in
new file mode 100644 (file)
index 0000000..68aa586
--- /dev/null
@@ -0,0 +1,80 @@
+#
+# configure.in
+#
+#      This library is free software; you can redistribute it and/or
+#      modify it under the terms of the GNU Lesser General Public
+#      License as published by the Free Software Foundation version 2.1
+#      of the License.
+#
+# Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+#
+
+AC_INIT(libnl, 1.0-pre6, tgraf@suug.ch)
+AC_CONFIG_HEADER(lib/defs.h)
+
+save_CFLAGS="${CFLAGS}"
+save_LDFLAGS="${LDFLAGS}"
+save_CPPFLAGS="${CPPFLAGS}"
+
+AC_PROG_CC
+AC_PROG_INSTALL
+
+AC_C_CONST
+AC_C_INLINE
+
+#####################################################################
+##
+## libm check
+##
+#####################################################################
+M="No "
+AC_CHECK_LIB(m, pow,
+[
+  LIBM="-lm"
+  M="Yes"
+],[
+    echo
+    echo "*** Error: libm required ***"
+    echo
+    exit
+])
+
+#####################################################################
+##
+## verbose error strings
+##
+#####################################################################
+AC_ARG_ENABLE(verbose-errors,
+[  --enable-verbose-errors enable verbose errors (debugging)],[
+       if test x$enableval = xyes; then
+               AC_DEFINE_UNQUOTED(VERBOSE_ERRORS,"1",[verbose errors])
+       fi
+])
+
+#####################################################################
+##
+## compile decisions
+##
+#####################################################################
+COMPILE_LIBNL="Yes "
+LIBNL_LIB="$LIBM"
+
+AC_SUBST(LIBNL_LIB)
+
+AC_OUTPUT([Makefile.opts libnl-1.pc])
+
+#####################################################################
+##
+## status report
+##
+#####################################################################
+echo "
+----------------------------------------------------------------------
+SUMMARY:
+
+Included in Compilation:
+  libnl:   $COMPILE_LIBNL $LIBNL_LIB
+
+Dependencies:
+bmon:
+  libm             $M       (required)"
diff --git a/doc/Doxyfile b/doc/Doxyfile
new file mode 100644 (file)
index 0000000..80d3a82
--- /dev/null
@@ -0,0 +1,224 @@
+# Doxyfile 0.1
+
+#---------------------------------------------------------------------------
+# Project related configuration options
+#---------------------------------------------------------------------------
+PROJECT_NAME           = libnl
+PROJECT_NUMBER         = 1.0-pre2
+OUTPUT_DIRECTORY       = ./
+CREATE_SUBDIRS         = NO
+OUTPUT_LANGUAGE        = English
+USE_WINDOWS_ENCODING   = NO
+BRIEF_MEMBER_DESC      = YES
+REPEAT_BRIEF           = YES
+ABBREVIATE_BRIEF       = 
+ALWAYS_DETAILED_SEC    = YES
+INLINE_INHERITED_MEMB  = YES
+FULL_PATH_NAMES        = YES
+STRIP_FROM_PATH        = 
+STRIP_FROM_INC_PATH    = 
+SHORT_NAMES            = NO
+JAVADOC_AUTOBRIEF      = YES
+MULTILINE_CPP_IS_BRIEF = NO
+DETAILS_AT_TOP         = YES
+INHERIT_DOCS           = YES
+DISTRIBUTE_GROUP_DOC   = YES
+SEPARATE_MEMBER_PAGES  = NO
+TAB_SIZE               = 8
+ALIASES                = arg=\param
+OPTIMIZE_OUTPUT_FOR_C  = YES
+OPTIMIZE_OUTPUT_JAVA   = NO
+SUBGROUPING            = YES
+#---------------------------------------------------------------------------
+# Build related configuration options
+#---------------------------------------------------------------------------
+EXTRACT_ALL            = NO
+EXTRACT_PRIVATE        = YES
+EXTRACT_STATIC         = NO
+EXTRACT_LOCAL_CLASSES  = YES
+EXTRACT_LOCAL_METHODS  = YES
+HIDE_UNDOC_MEMBERS     = NO
+HIDE_UNDOC_CLASSES     = NO
+HIDE_FRIEND_COMPOUNDS  = NO
+HIDE_IN_BODY_DOCS      = NO
+INTERNAL_DOCS          = NO
+CASE_SENSE_NAMES       = YES
+HIDE_SCOPE_NAMES       = NO
+SHOW_INCLUDE_FILES     = YES
+INLINE_INFO            = YES
+SORT_MEMBER_DOCS       = NO
+SORT_BRIEF_DOCS        = NO
+SORT_BY_SCOPE_NAME     = NO
+GENERATE_TODOLIST      = NO
+GENERATE_TESTLIST      = NO
+GENERATE_BUGLIST       = NO
+GENERATE_DEPRECATEDLIST= NO
+ENABLED_SECTIONS       = 
+MAX_INITIALIZER_LINES  = 30
+SHOW_USED_FILES        = NO
+SHOW_DIRECTORIES       = NO
+FILE_VERSION_FILTER    = 
+#---------------------------------------------------------------------------
+# configuration options related to warning and progress messages
+#---------------------------------------------------------------------------
+QUIET                  = YES
+WARNINGS               = YES
+WARN_IF_UNDOCUMENTED   = NO
+WARN_IF_DOC_ERROR      = YES
+WARN_NO_PARAMDOC       = NO
+WARN_FORMAT            = "$file:$line: $text"
+WARN_LOGFILE           = 
+#---------------------------------------------------------------------------
+# configuration options related to the input files
+#---------------------------------------------------------------------------
+INPUT                  = ../lib \
+                         ../include/netlink
+FILE_PATTERNS          = *.c \
+                         *.h
+RECURSIVE              = YES
+EXCLUDE                = SCCS
+EXCLUDE_SYMLINKS       = NO
+EXCLUDE_PATTERNS       = 
+EXAMPLE_PATH           = 
+EXAMPLE_PATTERNS       = 
+EXAMPLE_RECURSIVE      = NO
+IMAGE_PATH             = 
+INPUT_FILTER           = 
+FILTER_PATTERNS        = 
+FILTER_SOURCE_FILES    = NO
+#---------------------------------------------------------------------------
+# configuration options related to source browsing
+#---------------------------------------------------------------------------
+SOURCE_BROWSER         = YES
+INLINE_SOURCES         = NO
+STRIP_CODE_COMMENTS    = NO
+REFERENCED_BY_RELATION = YES
+REFERENCES_RELATION    = YES
+USE_HTAGS              = NO
+VERBATIM_HEADERS       = NO
+#---------------------------------------------------------------------------
+# configuration options related to the alphabetical class index
+#---------------------------------------------------------------------------
+ALPHABETICAL_INDEX     = NO
+COLS_IN_ALPHA_INDEX    = 5
+IGNORE_PREFIX          = nl_
+#---------------------------------------------------------------------------
+# configuration options related to the HTML output
+#---------------------------------------------------------------------------
+GENERATE_HTML          = YES
+HTML_OUTPUT            = html
+HTML_FILE_EXTENSION    = .html
+HTML_HEADER            = 
+HTML_FOOTER            = 
+HTML_STYLESHEET        = libnl.css
+HTML_ALIGN_MEMBERS     = YES
+GENERATE_HTMLHELP      = NO
+CHM_FILE               = 
+HHC_LOCATION           = 
+GENERATE_CHI           = NO
+BINARY_TOC             = NO
+TOC_EXPAND             = NO
+DISABLE_INDEX          = NO
+ENUM_VALUES_PER_LINE   = 4
+GENERATE_TREEVIEW      = NO
+TREEVIEW_WIDTH         = 250
+#---------------------------------------------------------------------------
+# configuration options related to the LaTeX output
+#---------------------------------------------------------------------------
+GENERATE_LATEX         = NO
+LATEX_OUTPUT           = latex
+LATEX_CMD_NAME         = latex
+MAKEINDEX_CMD_NAME     = makeindex
+COMPACT_LATEX          = NO
+PAPER_TYPE             = a4wide
+EXTRA_PACKAGES         = 
+LATEX_HEADER           = 
+PDF_HYPERLINKS         = NO
+USE_PDFLATEX           = NO
+LATEX_BATCHMODE        = NO
+LATEX_HIDE_INDICES     = NO
+#---------------------------------------------------------------------------
+# configuration options related to the RTF output
+#---------------------------------------------------------------------------
+GENERATE_RTF           = NO
+RTF_OUTPUT             = rtf
+COMPACT_RTF            = NO
+RTF_HYPERLINKS         = NO
+RTF_STYLESHEET_FILE    = 
+RTF_EXTENSIONS_FILE    = 
+#---------------------------------------------------------------------------
+# configuration options related to the man page output
+#---------------------------------------------------------------------------
+GENERATE_MAN           = NO
+MAN_OUTPUT             = man
+MAN_EXTENSION          = .3
+MAN_LINKS              = NO
+#---------------------------------------------------------------------------
+# configuration options related to the XML output
+#---------------------------------------------------------------------------
+GENERATE_XML           = NO
+XML_OUTPUT             = xml
+XML_SCHEMA             = 
+XML_DTD                = 
+XML_PROGRAMLISTING     = YES
+#---------------------------------------------------------------------------
+# configuration options for the AutoGen Definitions output
+#---------------------------------------------------------------------------
+GENERATE_AUTOGEN_DEF   = NO
+#---------------------------------------------------------------------------
+# configuration options related to the Perl module output
+#---------------------------------------------------------------------------
+GENERATE_PERLMOD       = NO
+PERLMOD_LATEX          = NO
+PERLMOD_PRETTY         = YES
+PERLMOD_MAKEVAR_PREFIX = 
+#---------------------------------------------------------------------------
+# Configuration options related to the preprocessor   
+#---------------------------------------------------------------------------
+ENABLE_PREPROCESSING   = YES
+MACRO_EXPANSION        = NO
+EXPAND_ONLY_PREDEF     = NO
+SEARCH_INCLUDES        = NO
+INCLUDE_PATH           = 
+INCLUDE_FILE_PATTERNS  = 
+PREDEFINED             = 
+EXPAND_AS_DEFINED      = 
+SKIP_FUNCTION_MACROS   = YES
+#---------------------------------------------------------------------------
+# Configuration::additions related to external references   
+#---------------------------------------------------------------------------
+TAGFILES               = 
+GENERATE_TAGFILE       = 
+ALLEXTERNALS           = YES
+EXTERNAL_GROUPS        = YES
+PERL_PATH              = /usr/bin/perl
+#---------------------------------------------------------------------------
+# Configuration options related to the dot tool   
+#---------------------------------------------------------------------------
+CLASS_DIAGRAMS         = NO
+HIDE_UNDOC_RELATIONS   = YES
+HAVE_DOT               = YES
+CLASS_GRAPH            = NO
+COLLABORATION_GRAPH    = NO
+GROUP_GRAPHS           = NO
+UML_LOOK               = YES
+TEMPLATE_RELATIONS     = NO
+INCLUDE_GRAPH          = NO
+INCLUDED_BY_GRAPH      = NO
+CALL_GRAPH             = NO
+GRAPHICAL_HIERARCHY    = YES
+DIRECTORY_GRAPH        = NO
+DOT_IMAGE_FORMAT       = png
+DOT_PATH               = 
+DOTFILE_DIRS           = 
+MAX_DOT_GRAPH_WIDTH    = 1024
+MAX_DOT_GRAPH_HEIGHT   = 1024
+MAX_DOT_GRAPH_DEPTH    = 0
+DOT_TRANSPARENT        = NO
+DOT_MULTI_TARGETS      = NO
+GENERATE_LEGEND        = YES
+DOT_CLEANUP            = YES
+#---------------------------------------------------------------------------
+# Configuration::additions related to the search engine   
+#---------------------------------------------------------------------------
+SEARCHENGINE           = NO
diff --git a/doc/Makefile b/doc/Makefile
new file mode 100644 (file)
index 0000000..9c34e0f
--- /dev/null
@@ -0,0 +1,35 @@
+#
+# doc/Makefile
+#
+#      This library is free software; you can redistribute it and/or
+#      modify it under the terms of the GNU Lesser General Public
+#      License as published by the Free Software Foundation version 2.1
+#      of the License.
+#
+# Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+#
+
+ifeq ($(shell [ ! -r ../Makefile.opts ] && echo 1),)
+    include ../Makefile.opts
+endif
+
+export
+
+.PHONY: all gendoc clean distclean install
+
+all:
+       @true
+
+gendoc:
+       doxygen Doxyfile
+
+clean:
+       @true
+
+distclean:
+       $(RM) -f html/*
+
+install:
+       @true
+
+$(DEPS): ../Makefile.opts
diff --git a/doc/libnl.css b/doc/libnl.css
new file mode 100644 (file)
index 0000000..42e3e27
--- /dev/null
@@ -0,0 +1,310 @@
+BODY,H1,H2,H3,H4,H5,H6,P,CENTER,TD,TH,UL,DL,DIV {
+       font-family: Geneva, Arial, Helvetica, sans-serif;
+}
+BODY,TD {
+       font-size: 90%;
+}
+H1 {
+       text-align: center;
+       font-size: 160%;
+}
+H2 {
+       font-size: 120%;
+}
+H3 {
+       font-size: 100%;
+}
+CAPTION { font-weight: bold }
+DIV.qindex {
+       width: 100%;
+       background-color: #eeeeff;
+       border: 1px solid #b0b0b0;
+       text-align: center;
+       margin: 2px;
+       padding: 2px;
+       line-height: 140%;
+}
+DIV.nav {
+       width: 100%;
+       background-color: #eeeeff;
+       border: 1px solid #b0b0b0;
+       text-align: center;
+       margin: 2px;
+       padding: 2px;
+       line-height: 140%;
+}
+DIV.navtab {
+       background-color: #eeeeff;
+       border: 1px solid #b0b0b0;
+       text-align: center;
+       margin: 2px;
+       margin-right: 15px;
+       padding: 2px;
+}
+TD.navtab {
+       font-size: 70%;
+}
+A.qindex {
+       text-decoration: none;
+       font-weight: bold;
+       color: #1A419D;
+}
+A.qindex:visited {
+       text-decoration: none;
+       font-weight: bold;
+       color: #1A419D
+}
+A.qindex:hover {
+       text-decoration: none;
+       background-color: #ddddff;
+}
+A.qindexHL {
+       text-decoration: none;
+       font-weight: bold;
+       background-color: #6666cc;
+       color: #ffffff;
+       border: 1px double #9295C2;
+}
+A.qindexHL:hover {
+       text-decoration: none;
+       background-color: #6666cc;
+       color: #ffffff;
+}
+A.qindexHL:visited { text-decoration: none; background-color: #6666cc; color: #ffffff }
+A.el { text-decoration: none; font-weight: bold }
+A.elRef { font-weight: bold }
+A.code:link { text-decoration: none; font-weight: normal; color: #0000FF}
+A.code:visited { text-decoration: none; font-weight: normal; color: #0000FF}
+A.codeRef:link { font-weight: normal; color: #0000FF}
+A.codeRef:visited { font-weight: normal; color: #0000FF}
+A:hover { text-decoration: none; background-color: #f2f2ff }
+DL.el { margin-left: -1cm }
+.fragment {
+       font-family: Fixed, monospace;
+       font-size: 95%;
+}
+PRE.fragment {
+       border: 1px solid #CCCCCC;
+       background-color: #f5f5f5;
+       margin-top: 4px;
+       margin-bottom: 4px;
+       margin-left: 2px;
+       margin-right: 8px;
+       padding-left: 6px;
+       padding-right: 6px;
+       padding-top: 4px;
+       padding-bottom: 4px;
+}
+DIV.ah { background-color: black; font-weight: bold; color: #ffffff; margin-bottom: 3px; margin-top: 3px }
+TD.md { background-color: #F4F4FB; font-weight: bold; }
+TD.mdPrefix {
+       background-color: #F4F4FB;
+       color: #606060;
+       font-size: 80%;
+}
+TD.mdname1 { background-color: #F4F4FB; font-weight: bold; color: #602020; }
+TD.mdname { background-color: #F4F4FB; font-weight: bold; color: #602020; width: 600px; }
+DIV.groupHeader {
+       margin-left: 16px;
+       margin-top: 12px;
+       margin-bottom: 6px;
+       font-weight: bold;
+}
+DIV.groupText { margin-left: 16px; font-style: italic; font-size: 90% }
+BODY {
+       background: white;
+       color: black;
+       margin-right: 20px;
+       margin-left: 20px;
+}
+TD.indexkey {
+       background-color: #eeeeff;
+       font-weight: bold;
+       padding-right  : 10px;
+       padding-top    : 2px;
+       padding-left   : 10px;
+       padding-bottom : 2px;
+       margin-left    : 0px;
+       margin-right   : 0px;
+       margin-top     : 2px;
+       margin-bottom  : 2px;
+       border: 1px solid #CCCCCC;
+}
+TD.indexvalue {
+       background-color: #eeeeff;
+       font-style: italic;
+       padding-right  : 10px;
+       padding-top    : 2px;
+       padding-left   : 10px;
+       padding-bottom : 2px;
+       margin-left    : 0px;
+       margin-right   : 0px;
+       margin-top     : 2px;
+       margin-bottom  : 2px;
+       border: 1px solid #CCCCCC;
+}
+TR.memlist {
+   background-color: #f0f0f0; 
+}
+P.formulaDsp { text-align: center; }
+IMG.formulaDsp { }
+IMG.formulaInl { vertical-align: middle; }
+SPAN.keyword       { color: #008000 }
+SPAN.keywordtype   { color: #604020 }
+SPAN.keywordflow   { color: #e08000 }
+SPAN.comment       { color: #800000 }
+SPAN.preprocessor  { color: #806020 }
+SPAN.stringliteral { color: #002080 }
+SPAN.charliteral   { color: #008080 }
+.mdTable {
+       border: 1px solid #868686;
+       background-color: #F4F4FB;
+       width: 100%;
+}
+.mdRow {
+       padding: 8px 10px;
+}
+.mdescLeft {
+       padding: 0px 8px 4px 8px;
+       font-size: 80%;
+       font-style: italic;
+       background-color: #FAFAFA;
+       border-top: 1px none #E0E0E0;
+       border-right: 1px none #E0E0E0;
+       border-bottom: 1px none #E0E0E0;
+       border-left: 1px none #E0E0E0;
+       margin: 0px;
+}
+.mdescRight {
+       padding: 0px 8px 4px 8px;
+       font-size: 80%;
+       font-style: italic;
+       background-color: #FAFAFA;
+       border-top: 1px none #E0E0E0;
+       border-right: 1px none #E0E0E0;
+       border-bottom: 1px none #E0E0E0;
+       border-left: 1px none #E0E0E0;
+       margin: 0px;
+}
+.memItemLeft {
+       padding: 1px 0px 0px 8px;
+       margin: 4px;
+       border-top-width: 1px;
+       border-right-width: 1px;
+       border-bottom-width: 1px;
+       border-left-width: 1px;
+       border-top-color: #E0E0E0;
+       border-right-color: #E0E0E0;
+       border-bottom-color: #E0E0E0;
+       border-left-color: #E0E0E0;
+       border-top-style: solid;
+       border-right-style: none;
+       border-bottom-style: none;
+       border-left-style: none;
+       background-color: #FAFAFA;
+       font-size: 80%;
+}
+.memItemRight {
+       padding: 1px 8px 0px 8px;
+       margin: 4px;
+       border-top-width: 1px;
+       border-right-width: 1px;
+       border-bottom-width: 1px;
+       border-left-width: 1px;
+       border-top-color: #E0E0E0;
+       border-right-color: #E0E0E0;
+       border-bottom-color: #E0E0E0;
+       border-left-color: #E0E0E0;
+       border-top-style: solid;
+       border-right-style: none;
+       border-bottom-style: none;
+       border-left-style: none;
+       background-color: #FAFAFA;
+       font-size: 80%;
+}
+.memTemplItemLeft {
+       padding: 1px 0px 0px 8px;
+       margin: 4px;
+       border-top-width: 1px;
+       border-right-width: 1px;
+       border-bottom-width: 1px;
+       border-left-width: 1px;
+       border-top-color: #E0E0E0;
+       border-right-color: #E0E0E0;
+       border-bottom-color: #E0E0E0;
+       border-left-color: #E0E0E0;
+       border-top-style: none;
+       border-right-style: none;
+       border-bottom-style: none;
+       border-left-style: none;
+       background-color: #FAFAFA;
+       font-size: 80%;
+}
+.memTemplItemRight {
+       padding: 1px 8px 0px 8px;
+       margin: 4px;
+       border-top-width: 1px;
+       border-right-width: 1px;
+       border-bottom-width: 1px;
+       border-left-width: 1px;
+       border-top-color: #E0E0E0;
+       border-right-color: #E0E0E0;
+       border-bottom-color: #E0E0E0;
+       border-left-color: #E0E0E0;
+       border-top-style: none;
+       border-right-style: none;
+       border-bottom-style: none;
+       border-left-style: none;
+       background-color: #FAFAFA;
+       font-size: 80%;
+}
+.memTemplParams {
+       padding: 1px 0px 0px 8px;
+       margin: 4px;
+       border-top-width: 1px;
+       border-right-width: 1px;
+       border-bottom-width: 1px;
+       border-left-width: 1px;
+       border-top-color: #E0E0E0;
+       border-right-color: #E0E0E0;
+       border-bottom-color: #E0E0E0;
+       border-left-color: #E0E0E0;
+       border-top-style: solid;
+       border-right-style: none;
+       border-bottom-style: none;
+       border-left-style: none;
+       color: #606060;
+       background-color: #FAFAFA;
+       font-size: 80%;
+}
+.search     { color: #003399;
+              font-weight: bold;
+}
+FORM.search {
+              margin-bottom: 0px;
+              margin-top: 0px;
+}
+INPUT.search { font-size: 75%;
+               color: #000080;
+               font-weight: normal;
+               background-color: #eeeeff;
+}
+TD.tiny      { font-size: 75%;
+}
+a {
+       color: #252E78;
+}
+a:visited {
+       color: #3D2185;
+}
+.dirtab { padding: 4px;
+          border-collapse: collapse;
+          border: 1px solid #b0b0b0;
+}
+TH.dirtab { background: #eeeeff;
+            font-weight: bold;
+}
+HR { height: 1px;
+     border: none;
+     border-top: 1px solid black;
+}
diff --git a/include/Makefile b/include/Makefile
new file mode 100644 (file)
index 0000000..f74539b
--- /dev/null
@@ -0,0 +1,36 @@
+#
+# include/Makefile
+#
+#      This library is free software; you can redistribute it and/or
+#      modify it under the terms of the GNU Lesser General Public
+#      License as published by the Free Software Foundation version 2.1
+#      of the License.
+#
+# Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+#
+
+ifeq ($(shell [ ! -r ../Makefile.opts ] && echo 1),)
+       include ../Makefile.opts
+endif
+
+.PHONY: all clean install 
+
+all:
+       @true
+
+clean:
+       @true
+
+distclean:
+       @true
+
+install:
+       mkdir -p $(DESTDIR)$(includedir)/netlink/route/sch/
+       mkdir -p $(DESTDIR)$(includedir)/netlink/route/cls/
+       mkdir -p $(DESTDIR)$(includedir)/netlink/fib_lookup/
+       install -m 0644 netlink/*.h $(DESTDIR)$(includedir)/netlink/
+       install -m 0644 netlink/route/*.h $(DESTDIR)$(includedir)/netlink/route/
+       install -m 0644 netlink/route/sch/*.h $(DESTDIR)$(includedir)/netlink/route/sch/
+       install -m 0644 netlink/route/cls/*.h $(DESTDIR)$(includedir)/netlink/route/cls/
+       install -m 0644 netlink/fib_lookup/*.h $(DESTDIR)$(includedir)/netlink/fib_lookup/
+
diff --git a/include/linux/gen_stats.h b/include/linux/gen_stats.h
new file mode 100644 (file)
index 0000000..ca60fc1
--- /dev/null
@@ -0,0 +1,60 @@
+#ifndef __LINUX_GEN_STATS_H
+#define __LINUX_GEN_STATS_H
+
+enum {
+       TCA_STATS_UNSPEC,
+       TCA_STATS_BASIC,
+       TCA_STATS_RATE_EST,
+       TCA_STATS_QUEUE,
+       TCA_STATS_APP,
+       __TCA_STATS_MAX,
+};
+#define TCA_STATS_MAX (__TCA_STATS_MAX - 1)
+
+/**
+ * @bytes: number of seen bytes
+ * @packets: number of seen packets
+ */
+struct gnet_stats_basic
+{
+       __u64   bytes;
+       __u32   packets;
+};
+
+/**
+ * @bps: current byte rate
+ * @pps: current packet rate
+ */
+struct gnet_stats_rate_est
+{
+       __u32   bps;
+       __u32   pps;
+};
+
+/**
+ * @qlen: queue length
+ * @backlog: backlog size of queue
+ * @drops: number of dropped packets
+ * @requeues: number of requeues
+ */
+struct gnet_stats_queue
+{
+       __u32   qlen;
+       __u32   backlog;
+       __u32   drops;
+       __u32   requeues;
+       __u32   overlimits;
+};
+
+/**
+ * @interval: sampling period
+ * @ewma_log: the log of measurement window weight
+ */
+struct gnet_estimator
+{
+       signed char     interval;
+       unsigned char   ewma_log;
+};
+
+
+#endif /* __LINUX_GEN_STATS_H */
diff --git a/include/linux/if.h b/include/linux/if.h
new file mode 100644 (file)
index 0000000..9128570
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ * INET                An implementation of the TCP/IP protocol suite for the LINUX
+ *             operating system.  INET is implemented using the  BSD Socket
+ *             interface as the means of communication with the user level.
+ *
+ *             Global definitions for the INET interface module.
+ *
+ * Version:    @(#)if.h        1.0.2   04/18/93
+ *
+ * Authors:    Original taken from Berkeley UNIX 4.3, (c) UCB 1982-1988
+ *             Ross Biro
+ *             Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
+ *
+ *             This program is free software; you can redistribute it and/or
+ *             modify it under the terms of the GNU General Public License
+ *             as published by the Free Software Foundation; either version
+ *             2 of the License, or (at your option) any later version.
+ */
+#ifndef _LINUX_IF_H
+#define _LINUX_IF_H
+
+#define        IFNAMSIZ        16
+
+/* Standard interface flags (netdevice->flags). */
+#define        IFF_UP          0x1             /* interface is up              */
+#define        IFF_BROADCAST   0x2             /* broadcast address valid      */
+#define        IFF_DEBUG       0x4             /* turn on debugging            */
+#define        IFF_LOOPBACK    0x8             /* is a loopback net            */
+#define        IFF_POINTOPOINT 0x10            /* interface is has p-p link    */
+#define        IFF_NOTRAILERS  0x20            /* avoid use of trailers        */
+#define        IFF_RUNNING     0x40            /* interface running and carrier ok */
+#define        IFF_NOARP       0x80            /* no ARP protocol              */
+#define        IFF_PROMISC     0x100           /* receive all packets          */
+#define        IFF_ALLMULTI    0x200           /* receive all multicast packets*/
+
+#define IFF_MASTER     0x400           /* master of a load balancer    */
+#define IFF_SLAVE      0x800           /* slave of a load balancer     */
+
+#define IFF_MULTICAST  0x1000          /* Supports multicast           */
+
+#define IFF_VOLATILE   (IFF_LOOPBACK|IFF_POINTOPOINT|IFF_BROADCAST|IFF_MASTER|IFF_SLAVE|IFF_RUNNING)
+
+#define IFF_PORTSEL    0x2000          /* can set media type           */
+#define IFF_AUTOMEDIA  0x4000          /* auto media select active     */
+#define IFF_DYNAMIC    0x8000          /* dialup device with changing addresses*/
+#define IFF_LOWER_UP   0x10000         /* driver signals L1 up         */
+#define IFF_DORMANT    0x20000         /* driver signals dormant       */
+
+/* Private (from user) interface flags (netdevice->priv_flags). */
+#define IFF_802_1Q_VLAN 0x1             /* 802.1Q VLAN device.          */
+#define IFF_EBRIDGE    0x2             /* Ethernet bridging device.    */
+
+#define IF_GET_IFACE   0x0001          /* for querying only */
+#define IF_GET_PROTO   0x0002
+
+/* For definitions see hdlc.h */
+#define IF_IFACE_V35   0x1000          /* V.35 serial interface        */
+#define IF_IFACE_V24   0x1001          /* V.24 serial interface        */
+#define IF_IFACE_X21   0x1002          /* X.21 serial interface        */
+#define IF_IFACE_T1    0x1003          /* T1 telco serial interface    */
+#define IF_IFACE_E1    0x1004          /* E1 telco serial interface    */
+#define IF_IFACE_SYNC_SERIAL 0x1005    /* can't be set by software     */
+#define IF_IFACE_X21D   0x1006          /* X.21 Dual Clocking (FarSite) */
+
+/* For definitions see hdlc.h */
+#define IF_PROTO_HDLC  0x2000          /* raw HDLC protocol            */
+#define IF_PROTO_PPP   0x2001          /* PPP protocol                 */
+#define IF_PROTO_CISCO 0x2002          /* Cisco HDLC protocol          */
+#define IF_PROTO_FR    0x2003          /* Frame Relay protocol         */
+#define IF_PROTO_FR_ADD_PVC 0x2004     /*    Create FR PVC             */
+#define IF_PROTO_FR_DEL_PVC 0x2005     /*    Delete FR PVC             */
+#define IF_PROTO_X25   0x2006          /* X.25                         */
+#define IF_PROTO_HDLC_ETH 0x2007       /* raw HDLC, Ethernet emulation */
+#define IF_PROTO_FR_ADD_ETH_PVC 0x2008 /*  Create FR Ethernet-bridged PVC */
+#define IF_PROTO_FR_DEL_ETH_PVC 0x2009 /*  Delete FR Ethernet-bridged PVC */
+#define IF_PROTO_FR_PVC        0x200A          /* for reading PVC status       */
+#define IF_PROTO_FR_ETH_PVC 0x200B
+#define IF_PROTO_RAW    0x200C          /* RAW Socket                   */
+
+
+/*
+ *     Device mapping structure. I'd just gone off and designed a 
+ *     beautiful scheme using only loadable modules with arguments
+ *     for driver options and along come the PCMCIA people 8)
+ *
+ *     Ah well. The get() side of this is good for WDSETUP, and it'll
+ *     be handy for debugging things. The set side is fine for now and
+ *     being very small might be worth keeping for clean configuration.
+ */
+
+struct ifmap 
+{
+       unsigned long mem_start;
+       unsigned long mem_end;
+       unsigned short base_addr; 
+       unsigned char irq;
+       unsigned char dma;
+       unsigned char port;
+       /* 3 bytes spare */
+};
+
+#endif /* _LINUX_IF_H */
diff --git a/include/linux/if_arp.h b/include/linux/if_arp.h
new file mode 100644 (file)
index 0000000..1c7417b
--- /dev/null
@@ -0,0 +1,149 @@
+/*
+ * INET                An implementation of the TCP/IP protocol suite for the LINUX
+ *             operating system.  INET is implemented using the  BSD Socket
+ *             interface as the means of communication with the user level.
+ *
+ *             Global definitions for the ARP (RFC 826) protocol.
+ *
+ * Version:    @(#)if_arp.h    1.0.1   04/16/93
+ *
+ * Authors:    Original taken from Berkeley UNIX 4.3, (c) UCB 1986-1988
+ *             Portions taken from the KA9Q/NOS (v2.00m PA0GRI) source.
+ *             Ross Biro
+ *             Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
+ *             Florian La Roche,
+ *             Jonathan Layes <layes@loran.com>
+ *             Arnaldo Carvalho de Melo <acme@conectiva.com.br> ARPHRD_HWX25
+ *
+ *             This program is free software; you can redistribute it and/or
+ *             modify it under the terms of the GNU General Public License
+ *             as published by the Free Software Foundation; either version
+ *             2 of the License, or (at your option) any later version.
+ */
+#ifndef _LINUX_IF_ARP_H
+#define _LINUX_IF_ARP_H
+
+/* ARP protocol HARDWARE identifiers. */
+#define ARPHRD_NETROM  0               /* from KA9Q: NET/ROM pseudo    */
+#define ARPHRD_ETHER   1               /* Ethernet 10Mbps              */
+#define        ARPHRD_EETHER   2               /* Experimental Ethernet        */
+#define        ARPHRD_AX25     3               /* AX.25 Level 2                */
+#define        ARPHRD_PRONET   4               /* PROnet token ring            */
+#define        ARPHRD_CHAOS    5               /* Chaosnet                     */
+#define        ARPHRD_IEEE802  6               /* IEEE 802.2 Ethernet/TR/TB    */
+#define        ARPHRD_ARCNET   7               /* ARCnet                       */
+#define        ARPHRD_APPLETLK 8               /* APPLEtalk                    */
+#define ARPHRD_DLCI    15              /* Frame Relay DLCI             */
+#define ARPHRD_ATM     19              /* ATM                          */
+#define ARPHRD_METRICOM        23              /* Metricom STRIP (new IANA id) */
+#define        ARPHRD_IEEE1394 24              /* IEEE 1394 IPv4 - RFC 2734    */
+#define ARPHRD_EUI64   27              /* EUI-64                       */
+#define ARPHRD_INFINIBAND 32           /* InfiniBand                   */
+
+/* Dummy types for non ARP hardware */
+#define ARPHRD_SLIP    256
+#define ARPHRD_CSLIP   257
+#define ARPHRD_SLIP6   258
+#define ARPHRD_CSLIP6  259
+#define ARPHRD_RSRVD   260             /* Notional KISS type           */
+#define ARPHRD_ADAPT   264
+#define ARPHRD_ROSE    270
+#define ARPHRD_X25     271             /* CCITT X.25                   */
+#define ARPHRD_HWX25   272             /* Boards with X.25 in firmware */
+#define ARPHRD_PPP     512
+#define ARPHRD_CISCO   513             /* Cisco HDLC                   */
+#define ARPHRD_HDLC    ARPHRD_CISCO
+#define ARPHRD_LAPB    516             /* LAPB                         */
+#define ARPHRD_DDCMP    517            /* Digital's DDCMP protocol     */
+#define ARPHRD_RAWHDLC 518             /* Raw HDLC                     */
+
+#define ARPHRD_TUNNEL  768             /* IPIP tunnel                  */
+#define ARPHRD_TUNNEL6 769             /* IP6IP6 tunnel                */
+#define ARPHRD_FRAD    770             /* Frame Relay Access Device    */
+#define ARPHRD_SKIP    771             /* SKIP vif                     */
+#define ARPHRD_LOOPBACK        772             /* Loopback device              */
+#define ARPHRD_LOCALTLK 773            /* Localtalk device             */
+#define ARPHRD_FDDI    774             /* Fiber Distributed Data Interface */
+#define ARPHRD_BIF      775             /* AP1000 BIF                   */
+#define ARPHRD_SIT     776             /* sit0 device - IPv6-in-IPv4   */
+#define ARPHRD_IPDDP   777             /* IP over DDP tunneller        */
+#define ARPHRD_IPGRE   778             /* GRE over IP                  */
+#define ARPHRD_PIMREG  779             /* PIMSM register interface     */
+#define ARPHRD_HIPPI   780             /* High Performance Parallel Interface */
+#define ARPHRD_ASH     781             /* Nexus 64Mbps Ash             */
+#define ARPHRD_ECONET  782             /* Acorn Econet                 */
+#define ARPHRD_IRDA    783             /* Linux-IrDA                   */
+/* ARP works differently on different FC media .. so  */
+#define ARPHRD_FCPP    784             /* Point to point fibrechannel  */
+#define ARPHRD_FCAL    785             /* Fibrechannel arbitrated loop */
+#define ARPHRD_FCPL    786             /* Fibrechannel public loop     */
+#define ARPHRD_FCFABRIC        787             /* Fibrechannel fabric          */
+       /* 787->799 reserved for fibrechannel media types */
+#define ARPHRD_IEEE802_TR 800          /* Magic type ident for TR      */
+#define ARPHRD_IEEE80211 801           /* IEEE 802.11                  */
+#define ARPHRD_IEEE80211_PRISM 802     /* IEEE 802.11 + Prism2 header  */
+#define ARPHRD_IEEE80211_RADIOTAP 803  /* IEEE 802.11 + radiotap header */
+
+#define ARPHRD_VOID      0xFFFF        /* Void type, nothing is known */
+#define ARPHRD_NONE      0xFFFE        /* zero header length */
+
+/* ARP protocol opcodes. */
+#define        ARPOP_REQUEST   1               /* ARP request                  */
+#define        ARPOP_REPLY     2               /* ARP reply                    */
+#define        ARPOP_RREQUEST  3               /* RARP request                 */
+#define        ARPOP_RREPLY    4               /* RARP reply                   */
+#define        ARPOP_InREQUEST 8               /* InARP request                */
+#define        ARPOP_InREPLY   9               /* InARP reply                  */
+#define        ARPOP_NAK       10              /* (ATM)ARP NAK                 */
+
+
+/* ARP ioctl request. */
+struct arpreq {
+  struct sockaddr      arp_pa;         /* protocol address             */
+  struct sockaddr      arp_ha;         /* hardware address             */
+  int                  arp_flags;      /* flags                        */
+  struct sockaddr       arp_netmask;    /* netmask (only for proxy arps) */
+  char                 arp_dev[16];
+};
+
+struct arpreq_old {
+  struct sockaddr      arp_pa;         /* protocol address             */
+  struct sockaddr      arp_ha;         /* hardware address             */
+  int                  arp_flags;      /* flags                        */
+  struct sockaddr       arp_netmask;    /* netmask (only for proxy arps) */
+};
+
+/* ARP Flag values. */
+#define ATF_COM                0x02            /* completed entry (ha valid)   */
+#define        ATF_PERM        0x04            /* permanent entry              */
+#define        ATF_PUBL        0x08            /* publish entry                */
+#define        ATF_USETRAILERS 0x10            /* has requested trailers       */
+#define ATF_NETMASK     0x20            /* want to use a netmask (only
+                                          for proxy entries) */
+#define ATF_DONTPUB    0x40            /* don't answer this addresses  */
+
+/*
+ *     This structure defines an ethernet arp header.
+ */
+
+struct arphdr
+{
+       unsigned short  ar_hrd;         /* format of hardware address   */
+       unsigned short  ar_pro;         /* format of protocol address   */
+       unsigned char   ar_hln;         /* length of hardware address   */
+       unsigned char   ar_pln;         /* length of protocol address   */
+       unsigned short  ar_op;          /* ARP opcode (command)         */
+
+#if 0
+        /*
+         *      Ethernet looks like this : This bit is variable sized however...
+         */
+       unsigned char           ar_sha[ETH_ALEN];       /* sender hardware address      */
+       unsigned char           ar_sip[4];              /* sender IP address            */
+       unsigned char           ar_tha[ETH_ALEN];       /* target hardware address      */
+       unsigned char           ar_tip[4];              /* target IP address            */
+#endif
+
+};
+
+#endif /* _LINUX_IF_ARP_H */
diff --git a/include/linux/if_ether.h b/include/linux/if_ether.h
new file mode 100644 (file)
index 0000000..ab7df16
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+ * INET                An implementation of the TCP/IP protocol suite for the LINUX
+ *             operating system.  INET is implemented using the  BSD Socket
+ *             interface as the means of communication with the user level.
+ *
+ *             Global definitions for the Ethernet IEEE 802.3 interface.
+ *
+ * Version:    @(#)if_ether.h  1.0.1a  02/08/94
+ *
+ * Author:     Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
+ *             Donald Becker, <becker@super.org>
+ *             Alan Cox, <alan@redhat.com>
+ *             Steve Whitehouse, <gw7rrm@eeshack3.swan.ac.uk>
+ *
+ *             This program is free software; you can redistribute it and/or
+ *             modify it under the terms of the GNU General Public License
+ *             as published by the Free Software Foundation; either version
+ *             2 of the License, or (at your option) any later version.
+ */
+#ifndef _LINUX_IF_ETHER_H
+#define _LINUX_IF_ETHER_H
+
+/*
+ *     IEEE 802.3 Ethernet magic constants.  The frame sizes omit the preamble
+ *     and FCS/CRC (frame check sequence). 
+ */
+
+#define ETH_ALEN       6               /* Octets in one ethernet addr   */
+#define ETH_HLEN       14              /* Total octets in header.       */
+#define ETH_ZLEN       60              /* Min. octets in frame sans FCS */
+#define ETH_DATA_LEN   1500            /* Max. octets in payload        */
+#define ETH_FRAME_LEN  1514            /* Max. octets in frame sans FCS */
+
+/*
+ *     These are the defined Ethernet Protocol ID's.
+ */
+
+#define ETH_P_LOOP     0x0060          /* Ethernet Loopback packet     */
+#define ETH_P_PUP      0x0200          /* Xerox PUP packet             */
+#define ETH_P_PUPAT    0x0201          /* Xerox PUP Addr Trans packet  */
+#define ETH_P_IP       0x0800          /* Internet Protocol packet     */
+#define ETH_P_X25      0x0805          /* CCITT X.25                   */
+#define ETH_P_ARP      0x0806          /* Address Resolution packet    */
+#define        ETH_P_BPQ       0x08FF          /* G8BPQ AX.25 Ethernet Packet  [ NOT AN OFFICIALLY REGISTERED ID ] */
+#define ETH_P_IEEEPUP  0x0a00          /* Xerox IEEE802.3 PUP packet */
+#define ETH_P_IEEEPUPAT        0x0a01          /* Xerox IEEE802.3 PUP Addr Trans packet */
+#define ETH_P_DEC       0x6000          /* DEC Assigned proto           */
+#define ETH_P_DNA_DL    0x6001          /* DEC DNA Dump/Load            */
+#define ETH_P_DNA_RC    0x6002          /* DEC DNA Remote Console       */
+#define ETH_P_DNA_RT    0x6003          /* DEC DNA Routing              */
+#define ETH_P_LAT       0x6004          /* DEC LAT                      */
+#define ETH_P_DIAG      0x6005          /* DEC Diagnostics              */
+#define ETH_P_CUST      0x6006          /* DEC Customer use             */
+#define ETH_P_SCA       0x6007          /* DEC Systems Comms Arch       */
+#define ETH_P_RARP      0x8035         /* Reverse Addr Res packet      */
+#define ETH_P_ATALK    0x809B          /* Appletalk DDP                */
+#define ETH_P_AARP     0x80F3          /* Appletalk AARP               */
+#define ETH_P_8021Q    0x8100          /* 802.1Q VLAN Extended Header  */
+#define ETH_P_IPX      0x8137          /* IPX over DIX                 */
+#define ETH_P_IPV6     0x86DD          /* IPv6 over bluebook           */
+#define ETH_P_WCCP     0x883E          /* Web-cache coordination protocol
+                                        * defined in draft-wilson-wrec-wccp-v2-00.txt */
+#define ETH_P_PPP_DISC 0x8863          /* PPPoE discovery messages     */
+#define ETH_P_PPP_SES  0x8864          /* PPPoE session messages       */
+#define ETH_P_MPLS_UC  0x8847          /* MPLS Unicast traffic         */
+#define ETH_P_MPLS_MC  0x8848          /* MPLS Multicast traffic       */
+#define ETH_P_ATMMPOA  0x884c          /* MultiProtocol Over ATM       */
+#define ETH_P_ATMFATE  0x8884          /* Frame-based ATM Transport
+                                        * over Ethernet
+                                        */
+#define ETH_P_AOE      0x88A2          /* ATA over Ethernet            */
+
+/*
+ *     Non DIX types. Won't clash for 1500 types.
+ */
+#define ETH_P_802_3    0x0001          /* Dummy type for 802.3 frames  */
+#define ETH_P_AX25     0x0002          /* Dummy protocol id for AX.25  */
+#define ETH_P_ALL      0x0003          /* Every packet (be careful!!!) */
+#define ETH_P_802_2    0x0004          /* 802.2 frames                 */
+#define ETH_P_SNAP     0x0005          /* Internal only                */
+#define ETH_P_DDCMP     0x0006          /* DEC DDCMP: Internal only     */
+#define ETH_P_WAN_PPP   0x0007          /* Dummy type for WAN PPP frames*/
+#define ETH_P_PPP_MP    0x0008          /* Dummy type for PPP MP frames */
+#define ETH_P_LOCALTALK 0x0009         /* Localtalk pseudo type        */
+#define ETH_P_PPPTALK  0x0010          /* Dummy type for Atalk over PPP*/
+#define ETH_P_TR_802_2 0x0011          /* 802.2 frames                 */
+#define ETH_P_MOBITEX  0x0015          /* Mobitex (kaz@cafe.net)       */
+#define ETH_P_CONTROL  0x0016          /* Card specific control frames */
+#define ETH_P_IRDA     0x0017          /* Linux-IrDA                   */
+#define ETH_P_ECONET   0x0018          /* Acorn Econet                 */
+#define ETH_P_HDLC     0x0019          /* HDLC frames                  */
+#define ETH_P_ARCNET   0x001A          /* 1A for ArcNet :-)            */
+
+/*
+ *     This is an Ethernet frame header.
+ */
+struct ethhdr {
+       unsigned char   h_dest[ETH_ALEN];       /* destination eth addr */
+       unsigned char   h_source[ETH_ALEN];     /* source ether addr    */
+       unsigned short  h_proto;                /* packet type ID field */
+} __attribute__((packed));
+
+#endif /* _LINUX_IF_ETHER_H */
diff --git a/include/linux/pkt_cls.h b/include/linux/pkt_cls.h
new file mode 100644 (file)
index 0000000..1bcdd11
--- /dev/null
@@ -0,0 +1,314 @@
+#ifndef __LINUX_PKT_CLS_H
+#define __LINUX_PKT_CLS_H
+
+/* I think i could have done better macros ; for now this is stolen from
+ * some arch/mips code - jhs
+*/
+#define _TC_MAKE32(x) ((x))
+
+#define _TC_MAKEMASK1(n) (_TC_MAKE32(1) << _TC_MAKE32(n))
+#define _TC_MAKEMASK(v,n) (_TC_MAKE32((_TC_MAKE32(1)<<(v))-1) << _TC_MAKE32(n))
+#define _TC_MAKEVALUE(v,n) (_TC_MAKE32(v) << _TC_MAKE32(n))
+#define _TC_GETVALUE(v,n,m) ((_TC_MAKE32(v) & _TC_MAKE32(m)) >> _TC_MAKE32(n))
+
+/* verdict bit breakdown 
+ *
+bit 0: when set -> this packet has been munged already
+
+bit 1: when set -> It is ok to munge this packet
+
+bit 2,3,4,5: Reclassify counter - sort of reverse TTL - if exceeded
+assume loop
+
+bit 6,7: Where this packet was last seen 
+0: Above the transmit example at the socket level
+1: on the Ingress
+2: on the Egress
+
+bit 8: when set --> Request not to classify on ingress. 
+
+bits 9,10,11: redirect counter -  redirect TTL. Loop avoidance
+
+ *
+ * */
+
+#define TC_MUNGED          _TC_MAKEMASK1(0)
+#define SET_TC_MUNGED(v)   ( TC_MUNGED | (v & ~TC_MUNGED))
+#define CLR_TC_MUNGED(v)   ( v & ~TC_MUNGED)
+
+#define TC_OK2MUNGE        _TC_MAKEMASK1(1)
+#define SET_TC_OK2MUNGE(v)   ( TC_OK2MUNGE | (v & ~TC_OK2MUNGE))
+#define CLR_TC_OK2MUNGE(v)   ( v & ~TC_OK2MUNGE)
+
+#define S_TC_VERD          _TC_MAKE32(2)
+#define M_TC_VERD          _TC_MAKEMASK(4,S_TC_VERD)
+#define G_TC_VERD(x)       _TC_GETVALUE(x,S_TC_VERD,M_TC_VERD)
+#define V_TC_VERD(x)       _TC_MAKEVALUE(x,S_TC_VERD)
+#define SET_TC_VERD(v,n)   ((V_TC_VERD(n)) | (v & ~M_TC_VERD))
+
+#define S_TC_FROM          _TC_MAKE32(6)
+#define M_TC_FROM          _TC_MAKEMASK(2,S_TC_FROM)
+#define G_TC_FROM(x)       _TC_GETVALUE(x,S_TC_FROM,M_TC_FROM)
+#define V_TC_FROM(x)       _TC_MAKEVALUE(x,S_TC_FROM)
+#define SET_TC_FROM(v,n)   ((V_TC_FROM(n)) | (v & ~M_TC_FROM))
+#define AT_STACK       0x0
+#define AT_INGRESS     0x1
+#define AT_EGRESS      0x2
+
+#define TC_NCLS          _TC_MAKEMASK1(8)
+#define SET_TC_NCLS(v)   ( TC_NCLS | (v & ~TC_NCLS))
+#define CLR_TC_NCLS(v)   ( v & ~TC_NCLS)
+
+#define S_TC_RTTL          _TC_MAKE32(9)
+#define M_TC_RTTL          _TC_MAKEMASK(3,S_TC_RTTL)
+#define G_TC_RTTL(x)       _TC_GETVALUE(x,S_TC_RTTL,M_TC_RTTL)
+#define V_TC_RTTL(x)       _TC_MAKEVALUE(x,S_TC_RTTL)
+#define SET_TC_RTTL(v,n)   ((V_TC_RTTL(n)) | (v & ~M_TC_RTTL))
+
+#define S_TC_AT          _TC_MAKE32(12)
+#define M_TC_AT          _TC_MAKEMASK(2,S_TC_AT)
+#define G_TC_AT(x)       _TC_GETVALUE(x,S_TC_AT,M_TC_AT)
+#define V_TC_AT(x)       _TC_MAKEVALUE(x,S_TC_AT)
+#define SET_TC_AT(v,n)   ((V_TC_AT(n)) | (v & ~M_TC_AT))
+
+/* Action attributes */
+enum
+{
+       TCA_ACT_UNSPEC,
+       TCA_ACT_KIND,
+       TCA_ACT_OPTIONS,
+       TCA_ACT_INDEX,
+       __TCA_ACT_MAX
+};
+
+#define TCA_ACT_MAX __TCA_ACT_MAX
+#define TCA_OLD_COMPAT (TCA_ACT_MAX+1)
+#define TCA_ACT_MAX_PRIO 32
+#define TCA_ACT_BIND   1
+#define TCA_ACT_NOBIND 0
+#define TCA_ACT_UNBIND 1
+#define TCA_ACT_NOUNBIND       0
+#define TCA_ACT_REPLACE                1
+#define TCA_ACT_NOREPLACE      0
+#define MAX_REC_LOOP 4
+#define MAX_RED_LOOP 4
+
+#define TC_ACT_UNSPEC  (-1)
+#define TC_ACT_OK              0
+#define TC_ACT_RECLASSIFY      1
+#define TC_ACT_SHOT            2
+#define TC_ACT_PIPE            3
+#define TC_ACT_STOLEN          4
+#define TC_ACT_QUEUED          5
+#define TC_ACT_REPEAT          6
+#define TC_ACT_JUMP            0x10000000
+
+/* Action type identifiers*/
+enum
+{
+       TCA_ID_UNSPEC=0,
+       TCA_ID_POLICE=1,
+       /* other actions go here */
+       __TCA_ID_MAX=255
+};
+
+#define TCA_ID_MAX __TCA_ID_MAX
+
+struct tc_police
+{
+       __u32                   index;
+       int                     action;
+#define TC_POLICE_UNSPEC       TC_ACT_UNSPEC
+#define TC_POLICE_OK           TC_ACT_OK
+#define TC_POLICE_RECLASSIFY   TC_ACT_RECLASSIFY
+#define TC_POLICE_SHOT         TC_ACT_SHOT
+#define TC_POLICE_PIPE         TC_ACT_PIPE
+
+       __u32                   limit;
+       __u32                   burst;
+       __u32                   mtu;
+       struct tc_ratespec      rate;
+       struct tc_ratespec      peakrate;
+       int                     refcnt;
+       int                     bindcnt;
+       __u32                   capab;
+};
+
+struct tcf_t
+{
+       __u32   install;
+       __u32   lastuse;
+       __u32   expires;
+};
+
+struct tc_cnt
+{
+       int                   refcnt; 
+       int                   bindcnt;
+};
+
+#define tc_gen \
+       __u32                 index; \
+       __u32                 capab; \
+       int                   action; \
+       int                   refcnt; \
+       int                   bindcnt
+
+enum
+{
+       TCA_POLICE_UNSPEC,
+       TCA_POLICE_TBF,
+       TCA_POLICE_RATE,
+       TCA_POLICE_PEAKRATE,
+       TCA_POLICE_AVRATE,
+       TCA_POLICE_RESULT,
+       __TCA_POLICE_MAX
+#define TCA_POLICE_RESULT TCA_POLICE_RESULT
+};
+
+#define TCA_POLICE_MAX (__TCA_POLICE_MAX - 1)
+
+/* U32 filters */
+
+#define TC_U32_HTID(h) ((h)&0xFFF00000)
+#define TC_U32_USERHTID(h) (TC_U32_HTID(h)>>20)
+#define TC_U32_HASH(h) (((h)>>12)&0xFF)
+#define TC_U32_NODE(h) ((h)&0xFFF)
+#define TC_U32_KEY(h) ((h)&0xFFFFF)
+#define TC_U32_UNSPEC  0
+#define TC_U32_ROOT    (0xFFF00000)
+
+enum
+{
+       TCA_U32_UNSPEC,
+       TCA_U32_CLASSID,
+       TCA_U32_HASH,
+       TCA_U32_LINK,
+       TCA_U32_DIVISOR,
+       TCA_U32_SEL,
+       TCA_U32_POLICE,
+       TCA_U32_ACT,   
+       TCA_U32_INDEV,
+       TCA_U32_PCNT,
+       __TCA_U32_MAX
+};
+
+#define TCA_U32_MAX (__TCA_U32_MAX - 1)
+
+struct tc_u32_key
+{
+       __u32           mask;
+       __u32           val;
+       int             off;
+       int             offmask;
+};
+
+struct tc_u32_sel
+{
+       unsigned char           flags;
+       unsigned char           offshift;
+       unsigned char           nkeys;
+
+       __u16                   offmask;
+       __u16                   off;
+       short                   offoff;
+
+       short                   hoff;
+       __u32                   hmask;
+       struct tc_u32_key       keys[0];
+};
+
+struct tc_u32_pcnt
+{
+       __u64 rcnt;
+       __u64 rhit;
+       __u64 kcnts[0];
+};
+/* Flags */
+
+#define TC_U32_TERMINAL                1
+#define TC_U32_OFFSET          2
+#define TC_U32_VAROFFSET       4
+#define TC_U32_EAT             8
+
+#define TC_U32_MAXDEPTH 8
+
+
+/* RSVP filter */
+
+enum
+{
+       TCA_RSVP_UNSPEC,
+       TCA_RSVP_CLASSID,
+       TCA_RSVP_DST,
+       TCA_RSVP_SRC,
+       TCA_RSVP_PINFO,
+       TCA_RSVP_POLICE,
+       __TCA_RSVP_MAX
+};
+
+#define TCA_RSVP_MAX (__TCA_RSVP_MAX - 1 )
+
+struct tc_rsvp_gpi
+{
+       __u32   key;
+       __u32   mask;
+       int     offset;
+};
+
+struct tc_rsvp_pinfo
+{
+       struct tc_rsvp_gpi dpi;
+       struct tc_rsvp_gpi spi;
+       __u8    protocol;
+       __u8    tunnelid;
+       __u8    tunnelhdr;
+};
+
+/* ROUTE filter */
+
+enum
+{
+       TCA_ROUTE4_UNSPEC,
+       TCA_ROUTE4_CLASSID,
+       TCA_ROUTE4_TO,
+       TCA_ROUTE4_FROM,
+       TCA_ROUTE4_IIF,
+       TCA_ROUTE4_POLICE,
+       __TCA_ROUTE4_MAX
+};
+
+#define TCA_ROUTE4_MAX (__TCA_ROUTE4_MAX - 1)
+
+
+/* FW filter */
+
+enum
+{
+       TCA_FW_UNSPEC,
+       TCA_FW_CLASSID,
+       TCA_FW_POLICE,
+       TCA_FW_INDEV, /*  used by CONFIG_NET_CLS_IND */
+       TCA_FW_ACT, /* used by CONFIG_NET_CLS_ACT */
+       __TCA_FW_MAX
+};
+
+#define TCA_FW_MAX (__TCA_FW_MAX - 1)
+
+/* TC index filter */
+
+enum
+{
+       TCA_TCINDEX_UNSPEC,
+       TCA_TCINDEX_HASH,
+       TCA_TCINDEX_MASK,
+       TCA_TCINDEX_SHIFT,
+       TCA_TCINDEX_FALL_THROUGH,
+       TCA_TCINDEX_CLASSID,
+       TCA_TCINDEX_POLICE,
+       __TCA_TCINDEX_MAX
+};
+
+#define TCA_TCINDEX_MAX     (__TCA_TCINDEX_MAX - 1)
+
+#endif
diff --git a/include/linux/pkt_sched.h b/include/linux/pkt_sched.h
new file mode 100644 (file)
index 0000000..60ffcb9
--- /dev/null
@@ -0,0 +1,464 @@
+#ifndef __LINUX_PKT_SCHED_H
+#define __LINUX_PKT_SCHED_H
+
+/* Logical priority bands not depending on specific packet scheduler.
+   Every scheduler will map them to real traffic classes, if it has
+   no more precise mechanism to classify packets.
+
+   These numbers have no special meaning, though their coincidence
+   with obsolete IPv6 values is not occasional :-). New IPv6 drafts
+   preferred full anarchy inspired by diffserv group.
+
+   Note: TC_PRIO_BESTEFFORT does not mean that it is the most unhappy
+   class, actually, as rule it will be handled with more care than
+   filler or even bulk.
+ */
+
+#define TC_PRIO_BESTEFFORT             0
+#define TC_PRIO_FILLER                 1
+#define TC_PRIO_BULK                   2
+#define TC_PRIO_INTERACTIVE_BULK       4
+#define TC_PRIO_INTERACTIVE            6
+#define TC_PRIO_CONTROL                        7
+
+#define TC_PRIO_MAX                    15
+
+/* Generic queue statistics, available for all the elements.
+   Particular schedulers may have also their private records.
+ */
+
+struct tc_stats
+{
+       __u64   bytes;                  /* NUmber of enqueues bytes */
+       __u32   packets;                /* Number of enqueued packets   */
+       __u32   drops;                  /* Packets dropped because of lack of resources */
+       __u32   overlimits;             /* Number of throttle events when this
+                                        * flow goes out of allocated bandwidth */
+       __u32   bps;                    /* Current flow byte rate */
+       __u32   pps;                    /* Current flow packet rate */
+       __u32   qlen;
+       __u32   backlog;
+};
+
+struct tc_estimator
+{
+       signed char     interval;
+       unsigned char   ewma_log;
+};
+
+/* "Handles"
+   ---------
+
+    All the traffic control objects have 32bit identifiers, or "handles".
+
+    They can be considered as opaque numbers from user API viewpoint,
+    but actually they always consist of two fields: major and
+    minor numbers, which are interpreted by kernel specially,
+    that may be used by applications, though not recommended.
+
+    F.e. qdisc handles always have minor number equal to zero,
+    classes (or flows) have major equal to parent qdisc major, and
+    minor uniquely identifying class inside qdisc.
+
+    Macros to manipulate handles:
+ */
+
+#define TC_H_MAJ_MASK (0xFFFF0000U)
+#define TC_H_MIN_MASK (0x0000FFFFU)
+#define TC_H_MAJ(h) ((h)&TC_H_MAJ_MASK)
+#define TC_H_MIN(h) ((h)&TC_H_MIN_MASK)
+#define TC_H_MAKE(maj,min) (((maj)&TC_H_MAJ_MASK)|((min)&TC_H_MIN_MASK))
+
+#define TC_H_UNSPEC    (0U)
+#define TC_H_ROOT      (0xFFFFFFFFU)
+#define TC_H_INGRESS    (0xFFFFFFF1U)
+
+struct tc_ratespec
+{
+       unsigned char   cell_log;
+       unsigned char   __reserved;
+       unsigned short  feature;
+       short           addend;
+       unsigned short  mpu;
+       __u32           rate;
+};
+
+/* FIFO section */
+
+struct tc_fifo_qopt
+{
+       __u32   limit;  /* Queue length: bytes for bfifo, packets for pfifo */
+};
+
+/* PRIO section */
+
+#define TCQ_PRIO_BANDS 16
+
+struct tc_prio_qopt
+{
+       int     bands;                  /* Number of bands */
+       __u8    priomap[TC_PRIO_MAX+1]; /* Map: logical priority -> PRIO band */
+};
+
+/* TBF section */
+
+struct tc_tbf_qopt
+{
+       struct tc_ratespec rate;
+       struct tc_ratespec peakrate;
+       __u32           limit;
+       __u32           buffer;
+       __u32           mtu;
+};
+
+enum
+{
+       TCA_TBF_UNSPEC,
+       TCA_TBF_PARMS,
+       TCA_TBF_RTAB,
+       TCA_TBF_PTAB,
+       __TCA_TBF_MAX,
+};
+
+#define TCA_TBF_MAX (__TCA_TBF_MAX - 1)
+
+
+/* TEQL section */
+
+/* TEQL does not require any parameters */
+
+/* SFQ section */
+
+struct tc_sfq_qopt
+{
+       unsigned        quantum;        /* Bytes per round allocated to flow */
+       int             perturb_period; /* Period of hash perturbation */
+       __u32           limit;          /* Maximal packets in queue */
+       unsigned        divisor;        /* Hash divisor  */
+       unsigned        flows;          /* Maximal number of flows  */
+};
+
+/*
+ *  NOTE: limit, divisor and flows are hardwired to code at the moment.
+ *
+ *     limit=flows=128, divisor=1024;
+ *
+ *     The only reason for this is efficiency, it is possible
+ *     to change these parameters in compile time.
+ */
+
+/* RED section */
+
+enum
+{
+       TCA_RED_UNSPEC,
+       TCA_RED_PARMS,
+       TCA_RED_STAB,
+       __TCA_RED_MAX,
+};
+
+#define TCA_RED_MAX (__TCA_RED_MAX - 1)
+
+struct tc_red_qopt
+{
+       __u32           limit;          /* HARD maximal queue length (bytes)    */
+       __u32           qth_min;        /* Min average length threshold (bytes) */
+       __u32           qth_max;        /* Max average length threshold (bytes) */
+       unsigned char   Wlog;           /* log(W)               */
+       unsigned char   Plog;           /* log(P_max/(qth_max-qth_min)) */
+       unsigned char   Scell_log;      /* cell size for idle damping */
+       unsigned char   flags;
+#define TC_RED_ECN     1
+};
+
+struct tc_red_xstats
+{
+       __u32           early;          /* Early drops */
+       __u32           pdrop;          /* Drops due to queue limits */
+       __u32           other;          /* Drops due to drop() calls */
+       __u32           marked;         /* Marked packets */
+};
+
+/* GRED section */
+
+#define MAX_DPs 16
+
+enum
+{
+       TCA_GRED_UNSPEC,
+       TCA_GRED_PARMS,
+       TCA_GRED_STAB,
+       TCA_GRED_DPS,
+          __TCA_GRED_MAX,
+};
+
+#define TCA_GRED_MAX (__TCA_GRED_MAX - 1)
+
+#define TCA_SET_OFF TCA_GRED_PARMS
+struct tc_gred_qopt
+{
+       __u32           limit;          /* HARD maximal queue length (bytes)    
+*/
+       __u32           qth_min;        /* Min average length threshold (bytes) 
+*/
+       __u32           qth_max;        /* Max average length threshold (bytes) 
+*/
+       __u32           DP;             /* upto 2^32 DPs */
+       __u32           backlog;        
+       __u32           qave;   
+       __u32           forced; 
+       __u32           early;  
+       __u32           other;  
+       __u32           pdrop;  
+
+       unsigned char   Wlog;           /* log(W)               */
+       unsigned char   Plog;           /* log(P_max/(qth_max-qth_min)) */
+       unsigned char   Scell_log;      /* cell size for idle damping */
+       __u8            prio;           /* prio of this VQ */
+       __u32   packets;
+       __u32   bytesin;
+};
+/* gred setup */
+struct tc_gred_sopt
+{
+       __u32           DPs;
+       __u32           def_DP;
+       __u8            grio;
+       __u8            pad1;
+       __u16           pad2;
+};
+
+/* HTB section */
+#define TC_HTB_NUMPRIO         8
+#define TC_HTB_MAXDEPTH                8
+#define TC_HTB_PROTOVER                3 /* the same as HTB and TC's major */
+
+struct tc_htb_opt
+{
+       struct tc_ratespec      rate;
+       struct tc_ratespec      ceil;
+       __u32   buffer;
+       __u32   cbuffer;
+       __u32   quantum;
+       __u32   level;          /* out only */
+       __u32   prio;
+};
+struct tc_htb_glob
+{
+       __u32 version;          /* to match HTB/TC */
+       __u32 rate2quantum;     /* bps->quantum divisor */
+       __u32 defcls;           /* default class number */
+       __u32 debug;            /* debug flags */
+
+       /* stats */
+       __u32 direct_pkts; /* count of non shapped packets */
+};
+enum
+{
+       TCA_HTB_UNSPEC,
+       TCA_HTB_PARMS,
+       TCA_HTB_INIT,
+       TCA_HTB_CTAB,
+       TCA_HTB_RTAB,
+       __TCA_HTB_MAX,
+};
+
+#define TCA_HTB_MAX (__TCA_HTB_MAX - 1)
+
+struct tc_htb_xstats
+{
+       __u32 lends;
+       __u32 borrows;
+       __u32 giants;   /* too big packets (rate will not be accurate) */
+       __u32 tokens;
+       __u32 ctokens;
+};
+
+/* HFSC section */
+
+struct tc_hfsc_qopt
+{
+       __u16   defcls;         /* default class */
+};
+
+struct tc_service_curve
+{
+       __u32   m1;             /* slope of the first segment in bps */
+       __u32   d;              /* x-projection of the first segment in us */
+       __u32   m2;             /* slope of the second segment in bps */
+};
+
+struct tc_hfsc_stats
+{
+       __u64   work;           /* total work done */
+       __u64   rtwork;         /* work done by real-time criteria */
+       __u32   period;         /* current period */
+       __u32   level;          /* class level in hierarchy */
+};
+
+enum
+{
+       TCA_HFSC_UNSPEC,
+       TCA_HFSC_RSC,
+       TCA_HFSC_FSC,
+       TCA_HFSC_USC,
+       __TCA_HFSC_MAX,
+};
+
+#define TCA_HFSC_MAX (__TCA_HFSC_MAX - 1)
+
+
+/* CBQ section */
+
+#define TC_CBQ_MAXPRIO         8
+#define TC_CBQ_MAXLEVEL                8
+#define TC_CBQ_DEF_EWMA                5
+
+struct tc_cbq_lssopt
+{
+       unsigned char   change;
+       unsigned char   flags;
+#define TCF_CBQ_LSS_BOUNDED    1
+#define TCF_CBQ_LSS_ISOLATED   2
+       unsigned char   ewma_log;
+       unsigned char   level;
+#define TCF_CBQ_LSS_FLAGS      1
+#define TCF_CBQ_LSS_EWMA       2
+#define TCF_CBQ_LSS_MAXIDLE    4
+#define TCF_CBQ_LSS_MINIDLE    8
+#define TCF_CBQ_LSS_OFFTIME    0x10
+#define TCF_CBQ_LSS_AVPKT      0x20
+       __u32           maxidle;
+       __u32           minidle;
+       __u32           offtime;
+       __u32           avpkt;
+};
+
+struct tc_cbq_wrropt
+{
+       unsigned char   flags;
+       unsigned char   priority;
+       unsigned char   cpriority;
+       unsigned char   __reserved;
+       __u32           allot;
+       __u32           weight;
+};
+
+struct tc_cbq_ovl
+{
+       unsigned char   strategy;
+#define        TC_CBQ_OVL_CLASSIC      0
+#define        TC_CBQ_OVL_DELAY        1
+#define        TC_CBQ_OVL_LOWPRIO      2
+#define        TC_CBQ_OVL_DROP         3
+#define        TC_CBQ_OVL_RCLASSIC     4
+       unsigned char   priority2;
+       __u16           pad;
+       __u32           penalty;
+};
+
+struct tc_cbq_police
+{
+       unsigned char   police;
+       unsigned char   __res1;
+       unsigned short  __res2;
+};
+
+struct tc_cbq_fopt
+{
+       __u32           split;
+       __u32           defmap;
+       __u32           defchange;
+};
+
+struct tc_cbq_xstats
+{
+       __u32           borrows;
+       __u32           overactions;
+       __s32           avgidle;
+       __s32           undertime;
+};
+
+enum
+{
+       TCA_CBQ_UNSPEC,
+       TCA_CBQ_LSSOPT,
+       TCA_CBQ_WRROPT,
+       TCA_CBQ_FOPT,
+       TCA_CBQ_OVL_STRATEGY,
+       TCA_CBQ_RATE,
+       TCA_CBQ_RTAB,
+       TCA_CBQ_POLICE,
+       __TCA_CBQ_MAX,
+};
+
+#define TCA_CBQ_MAX    (__TCA_CBQ_MAX - 1)
+
+/* dsmark section */
+
+enum {
+       TCA_DSMARK_UNSPEC,
+       TCA_DSMARK_INDICES,
+       TCA_DSMARK_DEFAULT_INDEX,
+       TCA_DSMARK_SET_TC_INDEX,
+       TCA_DSMARK_MASK,
+       TCA_DSMARK_VALUE,
+       __TCA_DSMARK_MAX,
+};
+
+#define TCA_DSMARK_MAX (__TCA_DSMARK_MAX - 1)
+
+/* ATM  section */
+
+enum {
+       TCA_ATM_UNSPEC,
+       TCA_ATM_FD,             /* file/socket descriptor */
+       TCA_ATM_PTR,            /* pointer to descriptor - later */
+       TCA_ATM_HDR,            /* LL header */
+       TCA_ATM_EXCESS,         /* excess traffic class (0 for CLP)  */
+       TCA_ATM_ADDR,           /* PVC address (for output only) */
+       TCA_ATM_STATE,          /* VC state (ATM_VS_*; for output only) */
+       __TCA_ATM_MAX,
+};
+
+#define TCA_ATM_MAX    (__TCA_ATM_MAX - 1)
+
+/* Network emulator */
+
+enum
+{
+       TCA_NETEM_UNSPEC,
+       TCA_NETEM_CORR,
+       TCA_NETEM_DELAY_DIST,
+       TCA_NETEM_REORDER,
+       __TCA_NETEM_MAX,
+};
+
+#define TCA_NETEM_MAX (__TCA_NETEM_MAX - 1)
+
+struct tc_netem_qopt
+{
+       __u32   latency;        /* added delay (us) */
+       __u32   limit;          /* fifo limit (packets) */
+       __u32   loss;           /* random packet loss (0=none ~0=100%) */
+       __u32   gap;            /* re-ordering gap (0 for none) */
+       __u32   duplicate;      /* random packet dup  (0=none ~0=100%) */
+       __u32   jitter;         /* random jitter in latency (us) */
+};
+
+struct tc_netem_corr
+{
+       __u32   delay_corr;     /* delay correlation */
+       __u32   loss_corr;      /* packet loss correlation */
+       __u32   dup_corr;       /* duplicate correlation  */
+};
+
+struct tc_netem_reorder
+{
+       __u32   probability;
+       __u32   correlation;
+};
+
+#define NETEM_DIST_SCALE       8192
+
+#endif
diff --git a/include/netlink-local.h b/include/netlink-local.h
new file mode 100644 (file)
index 0000000..974b043
--- /dev/null
@@ -0,0 +1,341 @@
+/*
+ * netlink-local.h             Local Netlink Interface
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_LOCAL_H_
+#define NETLINK_LOCAL_H_
+
+#include <stdio.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <math.h>
+#include <time.h>
+#include <stdarg.h>
+#include <ctype.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <inttypes.h>
+#include <assert.h>
+
+#include <arpa/inet.h>
+#include <netdb.h>
+
+#include <asm/types.h>
+
+/* local header copies */
+#include <linux/if.h>
+#include <linux/if_arp.h>
+#include <linux/if_ether.h>
+#include <linux/pkt_sched.h>
+#include <linux/pkt_cls.h>
+#include <linux/gen_stats.h>
+
+#include <netlink/netlink-compat.h>
+#include <netlink/netlink-kernel.h>
+#include <netlink/rtnetlink-kernel.h>
+
+#include <netlink/types.h>
+#include <netlink-types.h>
+
+#include <netlink/handlers.h>
+#include <netlink/cache.h>
+#include <netlink/route/tc.h>
+
+struct trans_tbl {
+       int i;
+       const char *a;
+};
+
+#define __ADD(id, name) { .i = id, .a = #name },
+
+#define NL_DBG(LVL,FMT,ARG...) \
+       do {    \
+               if (LVL <= nl_debug) \
+                       fprintf(stderr, "DBG<" #LVL ">: " FMT, ##ARG); \
+       } while (0)
+
+#define BUG()                            \
+       do {                                 \
+               fprintf(stderr, "BUG: %s:%d\n",  \
+                       __FILE__, __LINE__);         \
+               assert(0);      \
+       } while (0)
+
+#define RET_ERR(R, E)                    \
+    do {                                 \
+               errno = E;                       \
+               return -R;                       \
+       } while (0)
+
+extern int __nl_error(int, const char *, unsigned int,
+       const char *, const char *, ...);
+
+#ifdef NL_ERROR_ASSERT
+#include <assert.h>
+static inline int __assert_error(const char *file, int line, char *func,
+       const char *fmt, ...)
+{
+       va_list args;
+       fprintf(stderr, "%s:%d:%s: ", file, line, func);
+       va_start(args, fmt);
+       vfprintf(stderr, fmt, args);
+       va_end(args);
+       fprintf(stderr, "\n");
+       assert(0);
+       return 0;
+}
+#define nl_error(E, FMT,ARG...) \
+       __assert_error(__FILE__, __LINE__, __FUNCTION__, FMT, ##ARG)
+
+#else
+#define nl_error(E, FMT,ARG...) \
+       __nl_error(E, __FILE__, __LINE__, __FUNCTION__, FMT, ##ARG);
+
+#endif
+
+#define nl_errno(E)    nl_error(E, NULL)
+
+
+static inline char *__type2str(int type, char *buf, size_t len,
+                              struct trans_tbl *tbl, size_t tbl_len)
+{
+       int i;
+       for (i = 0; i < tbl_len; i++) {
+               if (tbl[i].i == type) {
+                       snprintf(buf, len, "%s", tbl[i].a);
+                       return buf;
+               }
+       }
+
+       snprintf(buf, len, "0x%x", type);
+       return buf;
+}
+
+static inline char *__flags2str(int flags, char *buf, size_t len,
+                               struct trans_tbl *tbl, size_t tbl_len)
+{
+       int i;
+       int tmp = flags;
+
+       memset(buf, 0, len);
+       
+       for (i = 0; i < tbl_len; i++) {
+               if (tbl[i].i & tmp) {
+                       tmp &= ~tbl[i].i;
+                       strncat(buf, tbl[i].a, len - strlen(buf) - 1);
+                       if ((tmp & flags))
+                               strncat(buf, ",", len - strlen(buf) - 1);
+               }
+       }
+
+       return buf;
+}
+
+static inline int __str2type(const char *buf, struct trans_tbl *tbl,
+                            size_t tbl_len)
+{
+       unsigned long l;
+       char *end;
+       int i;
+
+       if (*buf == '\0')
+               return -1;
+
+       for (i = 0; i < tbl_len; i++)
+               if (!strcasecmp(tbl[i].a, buf))
+                       return tbl[i].i;
+
+       l = strtoul(buf, &end, 0);
+       if (l == ULONG_MAX || *end != '\0')
+               return -1;
+
+       return (int) l;
+}
+
+static inline int __str2flags(const char *buf, struct trans_tbl *tbl,
+                             size_t tbl_len)
+{
+       int i, flags = 0, len;
+       char *p = (char *) buf, *t;
+
+       for (;;) {
+               if (*p == ' ')
+                       p++;
+       
+               t = strchr(p, ',');
+               len = t ? t - p : strlen(p);
+               for (i = 0; i < tbl_len; i++)
+                       if (!strncasecmp(tbl[i].a, p, len))
+                               flags |= tbl[i].i;
+
+               if (!t)
+                       return flags;
+
+               p = ++t;
+       }
+
+       return 0;
+}
+
+
+static inline void dp_new_line(struct nl_dump_params *params,
+                              int line_nr)
+{
+       if (params->dp_prefix) {
+               int i;
+               for (i = 0; i < params->dp_prefix; i++) {
+                       if (params->dp_fd)
+                               fprintf(params->dp_fd, " ");
+                       else if (params->dp_buf)
+                               strncat(params->dp_buf, " ",
+                                       params->dp_buflen -
+                                       sizeof(params->dp_buf) - 1);
+               }
+       }
+
+       if (params->dp_nl_cb)
+               params->dp_nl_cb(params, line_nr);
+}
+
+static inline void __dp_dump(struct nl_dump_params *parms, const char *fmt,
+                            va_list args)
+{
+       if (parms->dp_fd)
+               vfprintf(parms->dp_fd, fmt, args);
+       else if (parms->dp_buf || parms->dp_cb) {
+               char *buf = NULL;
+               vasprintf(&buf, fmt, args);
+               if (parms->dp_cb)
+                       parms->dp_cb(parms, buf);
+               else
+                       strncat(parms->dp_buf, buf,
+                               parms->dp_buflen - strlen(parms->dp_buf) - 1);
+               free(buf);
+       }
+}
+
+static inline void dp_dump(struct nl_dump_params *parms, const char *fmt, ...)
+{
+       va_list args;
+
+       va_start(args, fmt);
+       __dp_dump(parms, fmt, args);
+       va_end(args);
+}
+
+static inline void dp_dump_line(struct nl_dump_params *parms, int line,
+                               const char *fmt, ...)
+{
+       va_list args;
+
+       dp_new_line(parms, line);
+
+       va_start(args, fmt);
+       __dp_dump(parms, fmt, args);
+       va_end(args);
+}
+
+static inline void dump_from_ops(struct nl_object *obj,
+                                struct nl_dump_params *params)
+{
+       int type = params->dp_type;
+       char buf[64];
+
+       if (type < 0 || type > NL_DUMP_MAX)
+               BUG();
+
+       if (params->dp_dump_msgtype) {
+               dp_dump_line(params, 0, "%s ",
+                            nl_cache_mngt_type2name(obj->ce_ops,
+                                                    obj->ce_msgtype,
+                                                    buf, sizeof(buf)));
+               params->dp_pre_dump = 1;
+       } else
+               dp_new_line(params, 0);
+
+       if (obj->ce_ops->co_dump[type])
+               obj->ce_ops->co_dump[type](obj, params);
+}
+
+static inline struct nl_cache *dp_cache(struct nl_object *obj)
+{
+       if (obj->ce_cache == NULL)
+               return nl_cache_mngt_require(obj->ce_ops->co_name);
+
+       return obj->ce_cache;
+}
+
+static inline int nl_cb_call(struct nl_cb *cb, int type, struct nl_msg *msg)
+{
+       return cb->cb_set[type](msg, cb->cb_args[type]);
+}
+
+#define ARRAY_SIZE(X) (sizeof(X) / sizeof((X)[0]))
+#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
+
+#define __init __attribute__ ((constructor))
+#define __exit __attribute__ ((destructor))
+
+#define P_ACCEPT 0
+#define P_IGNORE 0
+
+#define NL_COPY_TEMPLATE(TB, PREFIX, BASE, FLAGBASE, IDX, VAR) \
+({     if (TB[PREFIX ##IDX]) { \
+               err = NL_COPY_DATA(BASE ##VAR, TB[PREFIX ##IDX]); \
+               if (err < 0) \
+                       return err; \
+               BASE ##mask |= FLAGBASE ##IDX; \
+       } })
+
+#define min(x,y) ({ \
+       typeof(x) _x = (x);     \
+       typeof(y) _y = (y);     \
+       (void) (&_x == &_y);            \
+       _x < _y ? _x : _y; })
+
+#define max(x,y) ({ \
+       typeof(x) _x = (x);     \
+       typeof(y) _y = (y);     \
+       (void) (&_x == &_y);            \
+       _x > _y ? _x : _y; })
+
+#define min_t(type,x,y) \
+       ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
+#define max_t(type,x,y) \
+       ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
+
+extern int nl_cache_parse(struct nl_cache_ops *, struct sockaddr_nl *,
+                         struct nlmsghdr *, struct nl_parser_param *);
+
+
+static inline void rtnl_copy_ratespec(struct rtnl_ratespec *dst,
+                                     struct tc_ratespec *src)
+{
+       dst->rs_cell_log = src->cell_log;
+       dst->rs_feature = src->feature;
+       dst->rs_addend = src->addend;
+       dst->rs_mpu = src->mpu;
+       dst->rs_rate = src->rate;
+}
+
+static inline void rtnl_rcopy_ratespec(struct tc_ratespec *dst,
+                                      struct rtnl_ratespec *src)
+{
+       dst->cell_log = src->rs_cell_log;
+       dst->feature = src->rs_feature;
+       dst->addend = src->rs_addend;
+       dst->mpu = src->rs_mpu;
+       dst->rate = src->rs_rate;
+}
+
+#endif
diff --git a/include/netlink-tc.h b/include/netlink-tc.h
new file mode 100644 (file)
index 0000000..475e56b
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * netlink-tc.h                Local Traffic Control Interface
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_TC_PRIV_H_
+#define NETLINK_TC_PRIV_H_
+
+#include <netlink-local.h>
+
+#define TCA_ATTR_HANDLE    0x001
+#define TCA_ATTR_PARENT    0x002
+#define TCA_ATTR_IFINDEX   0x004
+#define TCA_ATTR_KIND      0x008
+#define TCA_ATTR_FAMILY    0x010
+#define TCA_ATTR_INFO      0x020
+#define TCA_ATTR_OPTS      0x040
+#define TCA_ATTR_STATS     0x080
+#define TCA_ATTR_XSTATS    0x100
+#define TCA_ATTR_MAX     TCA_ATTR_XSTATS
+
+extern int tca_parse(struct nlattr **, int, struct rtnl_tca *,
+                    struct nla_policy *);
+extern int  tca_msg_parser(struct nlmsghdr *, struct rtnl_tca *);
+extern void tca_free_data(struct rtnl_tca *);
+extern int  tca_dump_brief(struct rtnl_tca *, const char *,
+                          struct nl_dump_params *, int);
+extern int  tca_dump_full(struct rtnl_tca *, struct nl_dump_params *, int);
+extern int  tca_dump_stats(struct rtnl_tca *,
+                               struct nl_dump_params *, int);
+extern int  tca_filter(struct rtnl_tca *, struct rtnl_tca *);
+
+extern void tca_set_ifindex(struct rtnl_tca *, int);
+extern int  tca_get_ifindex(struct rtnl_tca *);
+extern void tca_set_handle(struct rtnl_tca *, uint32_t);
+extern uint32_t tca_get_handle(struct rtnl_tca *);
+extern void tca_set_parent(struct rtnl_tca *, uint32_t);
+extern uint32_t tca_get_parent(struct rtnl_tca *);
+extern void tca_set_kind(struct rtnl_tca *, const char *);
+extern char *tca_get_kind(struct rtnl_tca *);
+extern uint64_t tca_get_stat(struct rtnl_tca *, int );
+
+extern struct nl_msg *tca_build_msg(struct rtnl_tca *tca, int type, int flags);
+
+static inline void *tca_priv(struct rtnl_tca *tca)
+{
+       return tca->tc_subdata;
+}
+
+static inline void *tca_xstats(struct rtnl_tca *tca)
+{
+       return tca->tc_xstats->d_data;
+}
+
+#endif
diff --git a/include/netlink-types.h b/include/netlink-types.h
new file mode 100644 (file)
index 0000000..3984026
--- /dev/null
@@ -0,0 +1,710 @@
+/*
+ * netlink-types.h     Netlink Types (Private)
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_LOCAL_TYPES_H_
+#define NETLINK_LOCAL_TYPES_H_
+
+#include <netlink/list.h>
+#include <netlink/route/link.h>
+#include <netlink/route/qdisc.h>
+#include <netlink/route/rtnl.h>
+
+struct nl_cache_ops;
+
+#define NL_SOCK_BUFSIZE_SET 1
+#define NL_SOCK_PASSCRED 2
+
+#define NL_MSG_CRED_PRESENT 1
+
+struct nl_handle;
+struct nl_object;
+
+struct nl_cb
+{
+       nl_recvmsg_msg_cb_t     cb_set[NL_CB_TYPE_MAX+1];
+       void *                  cb_args[NL_CB_TYPE_MAX+1];
+       
+       nl_recvmsg_err_cb_t     cb_err;
+       void *                  cb_err_arg;
+
+       /** May be used to replace nl_recvmsgs with your own implementation
+        * in all internal calls to nl_recvmsgs. */
+       int                     (*cb_recvmsgs_ow)(struct nl_handle *,
+                                                 struct nl_cb *);
+
+       /** Overwrite internal calls to nl_recv, must return the number of
+        * octets read and allocate a buffer for the received data. */
+       int                     (*cb_recv_ow)(struct nl_handle *,
+                                             struct sockaddr_nl *,
+                                             unsigned char **,
+                                             struct ucred **);
+
+       /** Overwrites internal calls to nl_send, must send the netlink
+        * message. */
+       int                     (*cb_send_ow)(struct nl_handle *,
+                                             struct nl_msg *);
+};
+
+struct nl_handle
+{
+       struct sockaddr_nl      h_local;
+       struct sockaddr_nl      h_peer;
+       int                     h_fd;
+       int                     h_proto;
+       unsigned int            h_seq_next;
+       unsigned int            h_seq_expect;
+       int                     h_flags;
+       struct nl_cb *          h_cb;
+};
+
+struct nl_cache
+{
+       struct nl_list_head     c_items;
+       int                     c_nitems;
+       int                     c_iarg1;
+       int                     c_iarg2;
+       struct nl_cache_ops *   c_ops;
+};
+
+struct nl_parser_param;
+
+struct nl_msgtype
+{
+       int                     mt_id;
+       char *                  mt_name;
+};
+
+struct nl_cache_ops
+{
+       char  *                 co_name;
+       size_t                  co_size;
+       int                     co_hdrsize;
+       int                     co_protocol;
+       
+       /**
+        * Called whenever an update of the cache is required. Must send
+        * a request message to the kernel requesting a complete dump.
+        */
+       int   (*co_request_update)(struct nl_cache *, struct nl_handle *);
+
+       /**
+        * Called whenever a new object was allocated
+        */
+       void  (*co_constructor)(struct nl_object *);
+
+       /**
+        * Called whenever a object in the cache gets destroyed, must
+        * free the type specific memory allocations
+        */
+       void  (*co_free_data)(struct nl_object *);
+
+       /**
+        * Called whenever a message was received that needs to be parsed.
+        * Must parse the message and call the paser callback function
+        * (nl_parser_param) provided via the argument.
+        */
+       int   (*co_msg_parser)(struct sockaddr_nl *, struct nlmsghdr *, void *);
+
+       /**
+        * Called whenever a dump of a cache object is requested. Must
+        * dump the specified object to the specified file descriptor
+        */
+       int   (*co_dump[NL_DUMP_MAX+1])(struct nl_object *,
+                                       struct nl_dump_params *);
+
+       /**
+        * Must compare the two specified objects and return a non-zero
+        * value if they match.
+        */
+       int   (*co_filter)(struct nl_object *, struct nl_object *);
+
+
+       struct nl_cache_ops *co_next;
+       struct nl_cache *co_major_cache;
+       struct nl_msgtype       co_msgtypes[];
+};
+
+#define NLHDR_COMMON                           \
+       int                     ce_refcnt;      \
+       struct nl_cache_ops *   ce_ops;         \
+       struct nl_cache *       ce_cache;       \
+       struct nl_object *      ce_dataref;     \
+       struct nl_list_head     ce_list;        \
+       int                     ce_msgtype;
+
+struct nl_object
+{
+       NLHDR_COMMON
+};
+
+struct nl_parser_param
+{
+       int             (*pp_cb)(struct nl_object *, struct nl_parser_param *);
+       void *            pp_arg;
+};
+
+struct nl_data
+{
+       size_t                  d_size;
+       void *                  d_data;
+};
+
+struct nl_addr
+{
+       int                     a_family;
+       unsigned int            a_maxsize;
+       unsigned int            a_len;
+       int                     a_prefixlen;
+       int                     a_refcnt;
+       unsigned char           a_addr[0];
+};
+
+struct nl_msg
+{
+       int                     nm_protocol;
+       int                     nm_flags;
+       struct sockaddr_nl      nm_src;
+       struct sockaddr_nl      nm_dst;
+       struct ucred            nm_creds;
+       struct nlmsghdr *       nm_nlh;
+};
+
+struct rtnl_link_map
+{
+       uint64_t lm_mem_start;
+       uint64_t lm_mem_end;
+       uint64_t lm_base_addr;
+       uint16_t lm_irq;
+       uint8_t  lm_dma;
+       uint8_t  lm_port;
+};
+
+#define IFQDISCSIZ     32
+
+struct rtnl_link
+{
+       NLHDR_COMMON
+
+       char            l_name[IFNAMSIZ];
+
+       uint32_t        l_family;
+       uint32_t        l_arptype;
+       uint32_t        l_index;
+       uint32_t        l_flags;
+       uint32_t        l_change;
+       uint32_t        l_mtu;
+       uint32_t        l_link;
+       uint32_t        l_txqlen;
+       uint32_t        l_weight;
+       uint32_t        l_master;
+       struct nl_addr *l_addr; 
+       struct nl_addr *l_bcast;
+       char            l_qdisc[IFQDISCSIZ];
+       struct rtnl_link_map l_map;
+       uint64_t        l_stats[RTNL_LINK_STATS_MAX+1];
+       uint32_t        l_mask; 
+       uint32_t        l_flag_mask;
+};
+
+struct rtnl_ncacheinfo
+{
+       uint32_t nci_confirmed; /**< Time since neighbour validty was last confirmed */
+       uint32_t nci_used;      /**< Time since neighbour entry was last ued */
+       uint32_t nci_updated;   /**< Time since last update */
+       uint32_t nci_refcnt;    /**< Reference counter */
+};
+
+
+struct rtnl_neigh
+{
+       NLHDR_COMMON
+       uint32_t        n_family;
+       uint32_t        n_ifindex;
+       uint16_t        n_state;
+       uint8_t         n_flags;
+       uint8_t         n_type; 
+       struct nl_addr *n_lladdr;
+       struct nl_addr *n_dst;  
+       uint32_t        n_probes;
+       struct rtnl_ncacheinfo n_cacheinfo;
+       uint32_t                n_mask;
+       uint32_t                n_state_mask;
+       uint32_t                n_flag_mask;
+};
+
+
+struct rtnl_addr_cacheinfo
+{
+       /* Preferred lifetime in seconds */
+       uint32_t aci_prefered;
+
+       /* Valid lifetime in seconds */
+       uint32_t aci_valid;
+
+       /* Timestamp of creation in 1/100s seince boottime */
+       uint32_t aci_cstamp;
+
+       /* Timestamp of last update in 1/100s since boottime */
+       uint32_t aci_tstamp;
+};
+
+struct rtnl_addr
+{
+       NLHDR_COMMON
+
+       uint8_t         a_family;
+       uint8_t         a_prefixlen;
+       uint8_t         a_flags;
+       uint8_t         a_scope;
+       uint32_t        a_ifindex;
+
+       struct nl_addr *a_peer; 
+       struct nl_addr *a_local;
+       struct nl_addr *a_bcast;
+       struct nl_addr *a_anycast;
+       struct nl_addr *a_multicast;
+
+       struct rtnl_addr_cacheinfo a_cacheinfo;
+       
+       char a_label[IFNAMSIZ];
+       uint32_t a_mask;
+       uint32_t a_flag_mask;
+};
+
+struct rtnl_nexthop
+{
+       uint8_t                 rtnh_flags;
+       uint8_t                 rtnh_flag_mask;
+       uint8_t                 rtnh_weight;
+       /* 1 byte spare */
+       uint32_t                rtnh_ifindex;
+       struct nl_addr *        rtnh_gateway;
+       uint32_t                rtnh_mask;
+
+       struct nl_list_head     rtnh_list;
+};
+
+struct rtnl_rtcacheinfo
+{
+       uint32_t        rtci_clntref;
+       uint32_t        rtci_last_use;
+       uint32_t        rtci_expires;
+       int32_t         rtci_error;
+       uint32_t        rtci_used;
+       uint32_t        rtci_id;
+       uint32_t        rtci_ts;
+       uint32_t        rtci_tsage;
+};
+
+struct rtnl_route
+{
+       NLHDR_COMMON
+
+       uint8_t                 rt_family;
+       uint8_t                 rt_dst_len;
+       uint8_t                 rt_src_len;
+       uint8_t                 rt_tos;
+       uint8_t                 rt_table;
+       uint8_t                 rt_protocol;
+       uint8_t                 rt_scope;
+       uint8_t                 rt_type;
+       uint32_t                rt_flags;
+       struct nl_addr *        rt_dst;
+       struct nl_addr *        rt_src;
+       char                    rt_iif[IFNAMSIZ];
+       uint32_t                rt_oif;
+       struct nl_addr *        rt_gateway;
+       uint32_t                rt_prio;
+       uint32_t                rt_metrics[RTAX_MAX];
+       uint32_t                rt_metrics_mask;
+       struct nl_addr *        rt_pref_src;
+       struct nl_list_head     rt_nexthops;
+       realm_t                 rt_realms;
+       struct rtnl_rtcacheinfo rt_cacheinfo;
+       uint32_t                rt_mp_algo;
+       uint32_t                rt_flag_mask;
+       uint32_t                rt_mask;
+};
+
+struct rtnl_rule
+{
+       NLHDR_COMMON
+
+       uint64_t        r_fwmark;
+       uint32_t        r_prio;
+       uint32_t        r_realms;
+       uint32_t        r_table;
+       uint8_t         r_dsfield;
+       uint8_t         r_type;
+       uint8_t         r_family;
+       uint8_t         r_src_len;
+       uint8_t         r_dst_len;
+       char            r_iif[IFNAMSIZ];
+       struct nl_addr *r_src;
+       struct nl_addr *r_dst;
+       struct nl_addr *r_srcmap;
+
+       uint32_t        r_mask; 
+};
+
+struct rtnl_neightbl_parms
+{
+       /**
+        * Interface index of the device this parameter set is assigned
+        * to or 0 for the default set.
+        */
+       uint32_t                ntp_ifindex;
+
+       /**
+        * Number of references to this parameter set.
+        */
+       uint32_t                ntp_refcnt;
+
+       /**
+        * Queue length for pending arp requests, i.e. the number of
+        * packets which are accepted from other layers while the
+        * neighbour address is still being resolved
+        */
+       uint32_t                ntp_queue_len;
+
+       /**
+        * Number of requests to send to the user level ARP daemon.
+        * Specify 0 to disable.
+        */
+       uint32_t                ntp_app_probes;
+
+       /**
+        * Maximum number of retries for unicast solicitation.
+        */
+       uint32_t                ntp_ucast_probes;
+
+       /**
+        * Maximum number of retries for multicast solicitation.
+        */
+       uint32_t                ntp_mcast_probes;
+
+       /**
+        * Base value in milliseconds to ompute reachable time, see RFC2461.
+        */
+       uint64_t                ntp_base_reachable_time;
+
+       /**
+        * Actual reachable time (read-only)
+        */
+       uint64_t                ntp_reachable_time;     /* secs */
+
+       /**
+        * The time in milliseconds between retransmitted Neighbor
+        * Solicitation messages.
+        */
+       uint64_t                ntp_retrans_time;
+
+       /**
+        * Interval in milliseconds to check for stale neighbour
+        * entries.
+        */
+       uint64_t                ntp_gc_stale_time;      /* secs */
+
+       /**
+        * Delay in milliseconds for the first time probe if
+        * the neighbour is reachable.
+        */
+       uint64_t                ntp_probe_delay;        /* secs */
+
+       /**
+        * Maximum delay in milliseconds of an answer to a neighbour
+        * solicitation message.
+        */
+       uint64_t                ntp_anycast_delay;
+
+       /**
+        * Minimum age in milliseconds before a neighbour entry
+        * may be replaced.
+        */
+       uint64_t                ntp_locktime;
+
+       /**
+        * Delay in milliseconds before answering to an ARP request
+        * for which a proxy ARP entry exists.
+        */
+       uint64_t                ntp_proxy_delay;
+
+       /**
+        * Queue length for the delayed proxy arp requests.
+        */
+       uint32_t                ntp_proxy_qlen;
+       
+       /**
+        * Mask of available parameter attributes
+        */
+       uint32_t                ntp_mask;
+};
+
+#define NTBLNAMSIZ     32
+
+/**
+ * Neighbour table
+ * @ingroup neightbl
+ */
+struct rtnl_neightbl
+{
+       NLHDR_COMMON
+
+       char                    nt_name[NTBLNAMSIZ];
+       uint32_t                nt_family;
+       uint32_t                nt_gc_thresh1;
+       uint32_t                nt_gc_thresh2;
+       uint32_t                nt_gc_thresh3;
+       uint64_t                nt_gc_interval;
+       struct ndt_config       nt_config;
+       struct rtnl_neightbl_parms nt_parms;
+       struct ndt_stats        nt_stats;
+       uint32_t                nt_mask;
+};
+
+struct rtnl_ratespec
+{
+       uint8_t                 rs_cell_log;
+       uint16_t                rs_feature;
+       uint16_t                rs_addend;
+       uint16_t                rs_mpu;
+       uint32_t                rs_rate;
+};
+
+struct rtnl_tstats
+{
+       struct {
+               uint64_t            bytes;
+               uint64_t            packets;
+       } tcs_basic;
+
+       struct {
+               uint32_t            bps;
+               uint32_t            pps;
+       } tcs_rate_est;
+
+       struct {
+               uint32_t            qlen;
+               uint32_t            backlog;
+               uint32_t            drops;
+               uint32_t            requeues;
+               uint32_t            overlimits;
+       } tcs_queue;
+};
+
+#define TCKINDSIZ      32
+
+#define NL_TCA_GENERIC(pre)                            \
+       NLHDR_COMMON                                    \
+       uint32_t                pre ##_family;          \
+       uint32_t                pre ##_ifindex;         \
+       uint32_t                pre ##_handle;          \
+       uint32_t                pre ##_parent;          \
+       uint32_t                pre ##_info;            \
+       char                    pre ##_kind[TCKINDSIZ]; \
+       struct nl_data *        pre ##_opts;            \
+       uint64_t                pre ##_stats[RTNL_TC_STATS_MAX+1]; \
+       struct nl_data *        pre ##_xstats;          \
+       void *                  pre ##_subdata;         \
+       int                     pre ##_mask
+
+
+struct rtnl_tca
+{
+       NL_TCA_GENERIC(tc);
+};
+
+struct rtnl_qdisc
+{
+       NL_TCA_GENERIC(q);
+       struct rtnl_qdisc_ops   *q_ops;
+};
+
+struct rtnl_class
+{
+       NL_TCA_GENERIC(c);
+       struct rtnl_class_ops   *c_ops;
+};
+
+struct rtnl_cls
+{
+       NL_TCA_GENERIC(c);
+       uint32_t        c_prio;
+       uint32_t        c_protocol;
+       struct rtnl_cls_ops     *c_ops;
+};
+
+struct rtnl_u32
+{
+       uint32_t                cu_divisor;
+       uint32_t                cu_hash;
+       uint32_t                cu_classid;
+       uint32_t                cu_link;
+       struct nl_data *        cu_pcnt;
+       struct nl_data *        cu_selector;
+       struct nl_data *        cu_act;
+       struct nl_data *        cu_police;
+       char                    cu_indev[IFNAMSIZ];
+       int                     cu_mask;
+};
+
+struct rtnl_fw
+{
+       uint32_t                cf_classid;
+       struct nl_data *        cf_act;
+       struct nl_data *        cf_police;
+       char                    cf_indev[IFNAMSIZ];
+       int                     cf_mask;
+};
+
+struct rtnl_dsmark_qdisc
+{
+       uint16_t        qdm_indices;
+       uint16_t        qdm_default_index;
+       uint32_t        qdm_set_tc_index;
+       uint32_t        qdm_mask;
+};
+
+struct rtnl_dsmark_class
+{
+       uint8_t         cdm_bmask;
+       uint8_t         cdm_value;
+       uint32_t        cdm_mask;
+};
+
+struct rtnl_fifo
+{
+       uint32_t        qf_limit;
+       uint32_t        qf_mask;
+};
+
+struct rtnl_prio
+{
+       uint32_t        qp_bands;
+       uint8_t         qp_priomap[TC_PRIO_MAX+1];
+       uint32_t        qp_mask;
+};
+
+struct rtnl_tbf
+{
+       uint32_t                qt_limit;
+       uint32_t                qt_mpu;
+       struct rtnl_ratespec    qt_rate;
+       uint32_t                qt_rate_bucket;
+       uint32_t                qt_rate_txtime;
+       struct rtnl_ratespec    qt_peakrate;
+       uint32_t                qt_peakrate_bucket;
+       uint32_t                qt_peakrate_txtime;
+       uint32_t                qt_mask;
+};
+
+struct rtnl_sfq
+{
+       uint32_t        qs_quantum;
+       uint32_t        qs_perturb;
+       uint32_t        qs_limit;
+       uint32_t        qs_divisor;
+       uint32_t        qs_flows;
+       uint32_t        qs_mask;
+};
+
+struct rtnl_netem_corr
+{
+       uint32_t        nmc_delay;
+       uint32_t        nmc_loss;
+       uint32_t        nmc_duplicate;
+};
+
+struct rtnl_netem_reo
+{
+       uint32_t        nmro_probability;
+       uint32_t        nmro_correlation;
+};
+
+struct rtnl_netem
+{
+       uint32_t                qnm_latency;
+       uint32_t                qnm_limit;
+       uint32_t                qnm_loss;
+       uint32_t                qnm_gap;
+       uint32_t                qnm_duplicate;
+       uint32_t                qnm_jitter;
+       uint32_t                qnm_mask;
+       struct rtnl_netem_corr  qnm_corr;
+       struct rtnl_netem_reo   qnm_ro;
+};
+
+struct rtnl_htb_qdisc
+{
+       uint32_t                qh_rate2quantum;
+       uint32_t                qh_defcls;
+       uint32_t                qh_mask;
+};
+
+struct rtnl_htb_class
+{
+       uint32_t                ch_prio;
+       uint32_t                ch_mtu;
+       struct rtnl_ratespec    ch_rate;
+       struct rtnl_ratespec    ch_ceil;
+       uint32_t                ch_rbuffer;
+       uint32_t                ch_cbuffer;
+       uint32_t                ch_mask;
+};
+
+struct rtnl_cbq
+{
+       struct tc_cbq_lssopt    cbq_lss;
+       struct tc_ratespec      cbq_rate;
+       struct tc_cbq_wrropt    cbq_wrr;
+       struct tc_cbq_ovl       cbq_ovl;
+       struct tc_cbq_fopt      cbq_fopt;
+       struct tc_cbq_police    cbq_police;
+};
+
+struct rtnl_red
+{
+       uint32_t        qr_limit;
+       uint32_t        qr_qth_min;
+       uint32_t        qr_qth_max;
+       uint8_t         qr_flags;
+       uint8_t         qr_wlog;
+       uint8_t         qr_plog;
+       uint8_t         qr_scell_log;
+       uint32_t        qr_mask;
+};
+
+struct flnl_request
+{
+       struct nl_addr *        lr_addr;
+       uint32_t                lr_fwmark;
+       uint8_t                 lr_tos;
+       uint8_t                 lr_scope;
+       uint8_t                 lr_table;
+       uint32_t                lr_mask;
+       int                     lr_refcnt;
+};
+
+
+struct flnl_result
+{
+       NLHDR_COMMON
+
+       struct flnl_request *   fr_req;
+       uint8_t                 fr_table_id;
+       uint8_t                 fr_prefixlen;
+       uint8_t                 fr_nh_sel;
+       uint8_t                 fr_type;
+       uint8_t                 fr_scope;
+       uint32_t                fr_error;
+};
+
+#endif
diff --git a/include/netlink/addr.h b/include/netlink/addr.h
new file mode 100644 (file)
index 0000000..99caa7b
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * netlink/addr.h              Abstract Address
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_ADDR_H_
+#define NETLINK_ADDR_H_
+
+#include <netlink/netlink.h>
+
+struct nl_addr;
+
+/* Creation */
+extern struct nl_addr *        nl_addr_alloc(size_t);
+extern struct nl_addr *        nl_addr_build(int, void *, size_t);
+extern struct nl_addr *        nl_addr_parse(const char *, int);
+extern struct nl_addr *        nl_addr_clone(struct nl_addr *);
+
+/* Destroyage */
+extern void            nl_addr_destroy(struct nl_addr *);
+
+/* Usage Management */
+extern struct nl_addr *        nl_addr_get(struct nl_addr *);
+extern void            nl_addr_put(struct nl_addr *);
+extern int             nl_addr_shared(struct nl_addr *);
+
+extern int             nl_addr_cmp(struct nl_addr *, struct nl_addr *);
+extern int             nl_addr_valid(char *, int);
+extern int             nl_addr_guess_family(struct nl_addr *);
+extern struct addrinfo *nl_addr_info(struct nl_addr *addr);
+extern int             nl_addr_resolve(struct nl_addr *addr, char *host, size_t hostlen);
+
+/* Access Functions */
+extern void            nl_addr_set_family(struct nl_addr *, int);
+extern int             nl_addr_get_family(struct nl_addr *);
+extern int             nl_addr_set_binary_addr(struct nl_addr *, void *,
+                                               size_t);
+extern void *          nl_addr_get_binary_addr(struct nl_addr *);
+extern unsigned int    nl_addr_get_len(struct nl_addr *);
+extern void            nl_addr_set_prefixlen(struct nl_addr *, int);
+extern unsigned int    nl_addr_get_prefixlen(struct nl_addr *);
+
+/* Address Family Translations */
+extern char *          nl_af2str(char, char *, size_t);
+extern char            nl_str2af(const char *);
+
+/* Translations to Strings */
+extern char *          nl_addr2str(struct nl_addr *, char *, size_t);
+
+#endif
diff --git a/include/netlink/attr.h b/include/netlink/attr.h
new file mode 100644 (file)
index 0000000..ecf3574
--- /dev/null
@@ -0,0 +1,265 @@
+/*
+ * netlink/attr.h              Netlink Attributes
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_ATTR_H_
+#define NETLINK_ATTR_H_
+
+#include <netlink/netlink.h>
+#include <netlink/object.h>
+#include <netlink/addr.h>
+#include <netlink/data.h>
+
+struct nl_msg;
+
+/**
+ * @name Validation Policy Types
+ * @{
+ */
+
+ /**
+  * @ingroup attr
+  * Standard attribute types to specify validation policy
+  */
+enum {
+       NLA_UNSPEC,     /**< Unspecified type */
+       NLA_U8,         /**< 8bit integer */
+       NLA_U16,        /**< 16bit integer */
+       NLA_U32,        /**< 32bit integer */
+       NLA_U64,        /**< 64bit integer */
+       NLA_STRING,     /**< character string */
+       NLA_FLAG,       /**< flag */
+       NLA_MSECS,      /**< micro seconds (64bit) */
+       NLA_NESTED,     /**< nested attributes */
+       __NLA_TYPE_MAX,
+};
+
+/**
+ * @ingroup attr
+ * Maximum netlink validation policy type
+ */
+#define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
+
+/** @} */
+
+/**
+ * @ingroup attr
+ * attribute validation policy
+ *
+ * Policies are defined as arrays of this struct, the array must
+ * be accessible by attribute type up to the highest identifier
+ * to be expected.
+ *
+ * Example:
+ * @code
+ * static struct nla_policy my_policy[ATTR_MAX+1] __read_mostly = {
+ *     [ATTR_FOO] = { .type = NLA_U16 },
+ *     [ATTR_BAR] = { .type = NLA_STRING },
+ *     [ATTR_BAZ] = { .minlen = sizeof(struct mystruct) },
+ * };
+ * @endcode
+ */
+struct nla_policy {
+       /** Type of attribute or NLA_UNSPEC */
+       uint16_t        type;
+
+       /** Minimal length of payload required to be available */
+       uint16_t        minlen;
+
+       /** Maximal length of payload required to be available */
+       uint16_t        maxlen;
+};
+
+/* size calculations */
+extern int             nla_attr_size(int payload);
+extern int             nla_total_size(int payload);
+extern int             nla_padlen(int payload);
+
+/* payload access */
+extern void *          nla_data(const struct nlattr *);
+extern int             nla_len(const struct nlattr *);
+
+/* attribute parsing */
+extern int             nla_ok(const struct nlattr *, int);
+extern struct nlattr * nla_next(const struct nlattr *, int *);
+extern int             nla_parse(struct nlattr **, int, struct nlattr *,
+                                 int, struct nla_policy *);
+extern int             nla_parse_nested(struct nlattr **, int, struct nlattr *,
+                                        struct nla_policy *);
+extern int             nla_validate(struct nlattr *, int, int,
+                                    struct nla_policy *);
+extern struct nlattr * nla_find(struct nlattr *, int, int);
+
+/* utilities */
+extern int             nla_memcpy(void *, struct nlattr *, int);
+extern size_t          nla_strlcpy(char *, const struct nlattr *, size_t);
+extern int             nla_memcmp(const struct nlattr *, const void *, size_t);
+extern int             nla_strcmp(const struct nlattr *, const char *);
+
+/* attribute construction */
+extern struct nlattr * nla_reserve(struct nl_msg *, int, int);
+extern int             nla_put(struct nl_msg *, int, int, const void *);
+extern int             nla_put_nested(struct nl_msg *, int, struct nl_msg *);
+extern int             nla_put_u8(struct nl_msg *, int, uint8_t);
+extern int             nla_put_u16(struct nl_msg *, int, uint16_t);
+extern int             nla_put_u32(struct nl_msg *, int, uint32_t);
+extern int             nla_put_u64(struct nl_msg *, int, uint64_t);
+extern int             nla_put_string(struct nl_msg *, int, const char *);
+extern int             nla_put_flag(struct nl_msg *, int);
+extern int             nla_put_msecs(struct nl_msg *, int, unsigned long);
+extern int             nla_put_data(struct nl_msg *, int, struct nl_data *);
+extern int             nla_put_addr(struct nl_msg *, int, struct nl_addr *);
+
+/* attribute nesting */
+extern struct nlattr * nla_nest_start(struct nl_msg *, int);
+extern int             nla_nest_end(struct nl_msg *, struct nlattr *);
+
+/* attribute reading */
+extern uint8_t         nla_get_u8(struct nlattr *);
+extern uint16_t                nla_get_u16(struct nlattr *);
+extern uint32_t                nla_get_u32(struct nlattr *);
+extern uint64_t                nla_get_u64(struct nlattr *);
+extern int             nla_get_flag(struct nlattr *);
+extern unsigned long   nla_get_msecs(struct nlattr *);
+extern struct nl_data *        nla_get_data(struct nlattr *);
+extern struct nl_addr *        nla_get_addr(struct nlattr *, int);
+
+/**
+ * @name Attribute Construction (Exception Based)
+ *
+ * All these functions jump to nla_put_failure in case of a failure
+ * instead of returning an error code.
+ * 
+ * @{
+ */
+
+/**
+ * @ingroup attr
+ * Add a netlink attribute to a netlink message
+ * @arg n              netlink message
+ * @arg attrtype       attribute type
+ * @arg attrlen                length of attribute payload
+ * @arg data           head of attribute payload
+ */
+#define NLA_PUT(n, attrtype, attrlen, data) \
+       do { \
+               if (nla_put(n, attrtype, attrlen, data) < 0) \
+                       goto nla_put_failure; \
+       } while(0)
+
+/**
+ * @ingroup attr
+ * Add a basic netlink attribute to a netlink message
+ * @arg n              netlink message
+ * @arg type           atomic type
+ * @arg attrtype       attribute type
+ * @arg value          head of attribute payload
+ */
+#define NLA_PUT_TYPE(n, type, attrtype, value) \
+       do { \
+               type __tmp = value; \
+               NLA_PUT(n, attrtype, sizeof(type), &__tmp); \
+       } while(0)
+
+/**
+ * Add a u8 netlink attribute to a netlink message
+ * @arg n              netlink message
+ * @arg attrtype       attribute type
+ * @arg value          numeric value
+ */
+#define NLA_PUT_U8(n, attrtype, value) \
+       NLA_PUT_TYPE(n, uint8_t, attrtype, value)
+
+/**
+ * Add a u16 netlink attribute to a netlink message
+ * @arg n              netlink message
+ * @arg attrtype       attribute type
+ * @arg value          numeric value
+ */
+#define NLA_PUT_U16(n, attrtype, value) \
+       NLA_PUT_TYPE(n, uint16_t, attrtype, value)
+
+/**
+ * Add a u32 netlink attribute to a netlink message
+ * @arg n              netlink message
+ * @arg attrtype       attribute type
+ * @arg value          numeric value
+ */
+#define NLA_PUT_U32(n, attrtype, value) \
+       NLA_PUT_TYPE(n, uint32_t, attrtype, value)
+
+/**
+ * Add a u64 netlink attribute to a netlink message
+ * @arg n              netlink message
+ * @arg attrtype       attribute type
+ * @arg value          numeric value
+ */
+#define NLA_PUT_U64(n, attrtype, value) \
+       NLA_PUT_TYPE(n, uint64_t, attrtype, value)
+
+/**
+ * Add a character string netlink attribute to a netlink message
+ * @arg n              netlink message
+ * @arg attrtype       attribute type
+ * @arg value          character string
+ */
+#define NLA_PUT_STRING(n, attrtype, value) \
+       NLA_PUT(n, attrtype, strlen(value) + 1, value)
+
+/**
+ * Add a flag netlink attribute to a netlink message
+ * @arg n              netlink message
+ * @arg attrtype       attribute type
+ */
+#define NLA_PUT_FLAG(n, attrtype) \
+       NLA_PUT(n, attrtype, 0, NULL)
+
+/**
+ * Add a msecs netlink attribute to a netlink message
+ * @arg n              netlink message
+ * @arg attrtype       attribute type
+ * @arg msecs          numeric value in micro seconds
+ */
+#define NLA_PUT_MSECS(n, attrtype, msecs) \
+       NLA_PUT_U64(n, attrtype, msecs)
+
+/**
+ * Add a address attribute to a netlink message
+ * @arg n              netlink message
+ * @arg attrtype       attribute type
+ * @arg addr           abstract address object
+ */
+#define NLA_PUT_ADDR(n, attrtype, addr) \
+       NLA_PUT(n, attrtype, nl_addr_get_len(addr), \
+               nl_addr_get_binary_addr(addr))
+
+/** @} */
+
+/**
+ * @name Iterators
+ * @{
+ */
+
+/**
+ * @ingroup attr
+ * iterate over a stream of attributes
+ * @arg pos    loop counter, set to current attribute
+ * @arg head   head of attribute stream
+ * @arg len    length of attribute stream
+ * @arg rem    initialized to len, holds bytes currently remaining in stream
+ */
+#define nla_for_each_attr(pos, head, len, rem) \
+       for (pos = head, rem = len; \
+            nla_ok(pos, rem); \
+            pos = nla_next(pos, &(rem)))
+
+/** @} */
+
+#endif
diff --git a/include/netlink/cache.h b/include/netlink/cache.h
new file mode 100644 (file)
index 0000000..3b48db2
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * netlink/cache.h             Caching Module
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_CACHE_H_
+#define NETLINK_CACHE_H_
+
+#include <netlink/netlink.h>
+#include <netlink/msg.h>
+#include <netlink/utils.h>
+#include <netlink/object.h>
+
+struct nl_cache;
+struct nl_cache_ops;
+
+/* Access Functions */
+extern int                     nl_cache_nitems(struct nl_cache *);
+extern int                     nl_cache_nitems_filter(struct nl_cache *,
+                                                      struct nl_object *);
+extern struct nl_cache_ops *   nl_cache_get_ops(struct nl_cache *);
+extern struct nl_object *      nl_cache_get_first(struct nl_cache *);
+extern struct nl_object *      nl_cache_get_last(struct nl_cache *);
+extern struct nl_object *      nl_cache_get_next(struct nl_object *);
+extern struct nl_object *      nl_cache_get_prev(struct nl_object *);
+
+/* Cache creation/deletion */
+extern struct nl_cache *       nl_cache_alloc(void);
+extern struct nl_cache *       nl_cache_alloc_from_ops(struct nl_cache_ops *);
+extern struct nl_cache *       nl_cache_alloc_name(const char *);
+extern void                    nl_cache_clear(struct nl_cache *);
+extern void                    nl_cache_free(struct nl_cache *);
+
+/* Cache modification */
+extern int                     nl_cache_add(struct nl_cache *,
+                                            struct nl_object *);
+extern int                     nl_cache_parse_and_add(struct nl_cache *,
+                                                      struct nl_msg *);
+extern void                    nl_cache_delete(struct nl_cache *,
+                                               struct nl_object *);
+extern int                     nl_cache_update(struct nl_handle *,
+                                               struct nl_cache *);
+extern int                     nl_cache_pickup(struct nl_handle *,
+                                               struct nl_cache *);
+
+/* General */
+extern int                     nl_cache_is_empty(struct nl_cache *);
+
+/* Dumping */
+extern void                    nl_cache_dump(struct nl_cache *,
+                                             struct nl_dump_params *);
+extern void                    nl_cache_dump_filter(struct nl_cache *,
+                                                    struct nl_dump_params *,
+                                                    struct nl_object *);
+
+/* Iterators */
+extern void                    nl_cache_foreach(struct nl_cache *,
+                                                void (*cb)(struct nl_object *,
+                                                           void *),
+                                                void *arg);
+extern void                    nl_cache_foreach_filter(struct nl_cache *,
+                                                       struct nl_object *,
+                                                       void (*cb)(struct
+                                                                  nl_object *,
+                                                                  void *),
+                                                       void *arg);
+
+/* --- cache management --- */
+
+/* Access Functions */
+extern char *                  nl_cache_ops_get_name(struct nl_cache_ops *);
+
+/* Message type association */
+extern struct nl_cache_ops *   nl_cache_mngt_associate(int, int);
+extern char *                  nl_cache_mngt_type2name(struct nl_cache_ops *,
+                                                       int, char *, size_t);
+
+/* Cache type management */
+extern struct nl_cache_ops *   nl_cache_mngt_lookup(const char *);
+extern int                     nl_cache_mngt_register(struct nl_cache_ops *);
+extern int                     nl_cache_mngt_unregister(struct nl_cache_ops *);
+
+/* Global cache provisioning/requiring */
+extern void                    nl_cache_mngt_provide(struct nl_cache *);
+extern void                    nl_cache_mngt_unprovide(struct nl_cache *);
+extern struct nl_cache *       nl_cache_mngt_require(const char *);
+
+#endif
diff --git a/include/netlink/data.h b/include/netlink/data.h
new file mode 100644 (file)
index 0000000..4fcdc21
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * netlink/data.h      Abstract Data
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_DATA_H_
+#define NETLINK_DATA_H_
+
+#include <netlink/netlink.h>
+
+struct nl_data;
+
+/* General */
+extern struct nl_data *        nl_data_alloc(void *, size_t);
+extern int             nl_data_append(struct nl_data *, void *, size_t);
+extern void            nl_data_free(struct nl_data *);
+
+/* Access Functions */
+extern void *          nl_data_get(struct nl_data *);
+extern size_t          nl_data_get_size(struct nl_data *);
+
+/* Misc */
+extern int             nl_data_cmp(struct nl_data *, struct nl_data *);
+
+#endif
diff --git a/include/netlink/fib_lookup/lookup.h b/include/netlink/fib_lookup/lookup.h
new file mode 100644 (file)
index 0000000..a7b995e
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * netlink/fib_lookup/fib_lookup.h     FIB Lookup
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_FIB_LOOKUP_H_
+#define NETLINK_FIB_LOOKUP_H_
+
+#include <netlink/netlink.h>
+#include <netlink/cache.h>
+#include <netlink/addr.h>
+#include <netlink/fib_lookup/request.h>
+
+struct flnl_result;
+
+extern struct flnl_result *    flnl_result_alloc(void);
+extern struct nl_cache *       flnl_result_alloc_cache(void);
+extern void                    flnl_result_put(struct flnl_result *);
+extern void                    flnl_result_free(struct flnl_result *);
+
+extern struct nl_msg *         flnl_lookup_build_request(struct flnl_request *,
+                                                         int);
+extern int                     flnl_lookup(struct nl_handle *,
+                                           struct flnl_request *,
+                                           struct nl_cache *);
+
+#endif
diff --git a/include/netlink/fib_lookup/request.h b/include/netlink/fib_lookup/request.h
new file mode 100644 (file)
index 0000000..64c912b
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * netlink/fib_lookup/request.h                FIB Lookup Request      
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_FIB_LOOKUP_REQUEST_H_
+#define NETLINK_FIB_LOOKUP_REQUEST_H_
+
+#include <netlink/netlink.h>
+#include <netlink/addr.h>
+
+struct flnl_request;
+
+extern struct flnl_request *   flnl_request_alloc(void);
+
+extern struct flnl_request *   flnl_request_get(struct flnl_request *);
+extern void                    flnl_request_put(struct flnl_request *);
+extern void                    flnl_request_free(struct flnl_request *);
+
+extern void                    flnl_request_set_fwmark(struct flnl_request *,
+                                                       uint64_t);
+extern uint64_t                        flnl_request_get_fwmark(struct flnl_request *);
+extern void                    flnl_request_set_tos(struct flnl_request *,
+                                                    int);
+extern int                     flnl_request_get_tos(struct flnl_request *);
+extern void                    flnl_request_set_scope(struct flnl_request *,
+                                                      int);
+extern int                     flnl_request_get_scope(struct flnl_request *);
+extern void                    flnl_request_set_table(struct flnl_request *,
+                                                      int);
+extern int                     flnl_request_get_table(struct flnl_request *);
+extern int                     flnl_request_set_addr(struct flnl_request *,
+                                                     struct nl_addr *);
+extern struct nl_addr *                flnl_request_get_addr(struct flnl_request *);
+
+extern int                     flnl_request_cmp(struct flnl_request *,
+                                                struct flnl_request *);
+
+#endif
diff --git a/include/netlink/handlers.h b/include/netlink/handlers.h
new file mode 100644 (file)
index 0000000..dd02c64
--- /dev/null
@@ -0,0 +1,134 @@
+/*
+ * netlink/handlers.c  default netlink message handlers
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_HANDLERS_H_
+#define NETLINK_HANDLERS_H_
+
+#include <stdio.h>
+#include <stdint.h>
+#include <sys/types.h>
+#include <netlink/netlink-compat.h>
+#include <netlink/netlink-kernel.h>
+
+struct nl_cb;
+struct nl_handle;
+struct nl_msg;
+
+/**
+ * @name Callback Typedefs
+ * @{
+ */
+
+/**
+ * nl_recvmsgs() callback for message processing customization
+ * @ingroup cb
+ * @arg msg            netlink message being processed
+ * @arg arg            argument passwd on through caller
+ */
+typedef int (*nl_recvmsg_msg_cb_t)(struct nl_msg *msg, void *arg);
+
+/**
+ * nl_recvmsgs() callback for error message processing customization
+ * @ingroup cb
+ * @arg nla            netlink address of the peer
+ * @arg nlerr          netlink error message being processed
+ * @arg arg            argument passed on through caller
+ */
+typedef int (*nl_recvmsg_err_cb_t)(struct sockaddr_nl *nla,
+                                  struct nlmsgerr *nlerr, void *arg);
+
+/** @} */
+
+/**
+ * Callback actions
+ * @ingroup cb
+ */
+enum nl_cb_action {
+       /** Proceed with wathever would come next */
+       NL_PROCEED,
+       /** Skip this message */
+       NL_SKIP,
+       /** Stop parsing altogether and discard remaining messages */
+       NL_EXIT,
+};
+
+/**
+ * Callback kinds
+ * @ingroup cb
+ */
+enum nl_cb_kind {
+       /** Default handlers (quiet) */
+       NL_CB_DEFAULT,
+       /** Verbose default handlers (error messages printed) */
+       NL_CB_VERBOSE,
+       /** Debug handlers for debugging */
+       NL_CB_DEBUG,
+       /** Customized handler specified by the user */
+       NL_CB_CUSTOM,
+       __NL_CB_KIND_MAX,
+};
+
+#define NL_CB_KIND_MAX (__NL_CB_KIND_MAX - 1)
+
+/**
+ * Callback types
+ * @ingroup cb
+ */
+enum nl_cb_type {
+       /** Message is valid */
+       NL_CB_VALID,
+       /** Last message in a series of multi part messages received */
+       NL_CB_FINISH,
+       /** Report received that data was lost */
+       NL_CB_OVERRUN,
+       /** Message wants to be skipped */
+       NL_CB_SKIPPED,
+       /** Message is an acknowledge */
+       NL_CB_ACK,
+       /** Called for every message received */
+       NL_CB_MSG_IN,
+       /** Called for every message sent out except for nl_sendto() */
+       NL_CB_MSG_OUT,
+       /** Message is malformed and invalid */
+       NL_CB_INVALID,
+       /** Called instead of internal sequence number checking */
+       NL_CB_SEQ_CHECK,
+       /** Sending of an acknowledge message has been requested */
+       NL_CB_SEND_ACK,
+       __NL_CB_TYPE_MAX,
+};
+
+#define NL_CB_TYPE_MAX (__NL_CB_TYPE_MAX - 1)
+
+extern struct nl_cb *  nl_cb_new(enum nl_cb_kind);
+extern void            nl_cb_destroy(struct nl_cb *);
+extern struct nl_cb *  nl_cb_clone(struct nl_cb *);
+
+extern int  nl_cb_set(struct nl_cb *, enum nl_cb_type, enum nl_cb_kind,
+                     nl_recvmsg_msg_cb_t, void *);
+extern int  nl_cb_set_all(struct nl_cb *, enum nl_cb_kind,
+                         nl_recvmsg_msg_cb_t, void *);
+extern int  nl_cb_err(struct nl_cb *, enum nl_cb_kind, nl_recvmsg_err_cb_t,
+                     void *);
+
+extern void nl_cb_overwrite_recvmsgs(struct nl_cb *,
+                                    int (*func)(struct nl_handle *,
+                                                struct nl_cb *));
+extern void nl_cb_overwrite_recv(struct nl_cb *,
+                                int (*func)(struct nl_handle *,
+                                            struct sockaddr_nl *,
+                                            unsigned char **,
+                                            struct ucred **));
+extern void nl_cb_overwrite_send(struct nl_cb *,
+                                int (*func)(struct nl_handle *,
+                                            struct nl_msg *));
+
+#endif
diff --git a/include/netlink/list.h b/include/netlink/list.h
new file mode 100644 (file)
index 0000000..dbfc3de
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * netlink/list.h      Netlink List Utilities
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_LIST_H_
+#define NETLINK_LIST_H_
+
+struct nl_list_head
+{
+       struct nl_list_head *   next;
+       struct nl_list_head *   prev;
+};
+
+
+static inline void __nl_list_add(struct nl_list_head *obj,
+                                struct nl_list_head *prev,
+                                struct nl_list_head *next)
+{
+       prev->next = obj;
+       obj->prev = prev;
+       next->prev = obj;
+       obj->next = next;
+}
+
+static inline void nl_list_add_tail(struct nl_list_head *obj,
+                                   struct nl_list_head *head)
+{
+       __nl_list_add(obj, head->prev, head);
+}
+
+static inline void nl_list_add_head(struct nl_list_head *obj,
+                                   struct nl_list_head *head)
+{
+       __nl_list_add(obj, head, head->next);
+}
+
+static inline void nl_list_del(struct nl_list_head *obj)
+{
+       obj->next->prev = obj->prev;
+       obj->prev->next = obj->next;
+}
+
+static inline int nl_list_empty(struct nl_list_head *head)
+{
+       return head->next == head;
+}
+
+#define nl_container_of(ptr, type, member) ({                  \
+        const typeof( ((type *)0)->member ) *__mptr = (ptr);   \
+        (type *)( (char *)__mptr - ((size_t) &((type *)0)->member));})
+
+#define nl_list_entry(ptr, type, member) \
+       nl_container_of(ptr, type, member)
+
+#define nl_list_at_tail(pos, head, member) \
+       ((pos)->member.next == (head))
+
+#define nl_list_at_head(pos, head, member) \
+       ((pos)->member.prev == (head))
+
+#define nl_list_for_each_entry(pos, head, member)                              \
+       for (pos = nl_list_entry((head)->next, typeof(*pos), member);   \
+            &(pos)->member != (head);  \
+            (pos) = nl_list_entry((pos)->member.next, typeof(*(pos)), member))
+
+#define nl_list_for_each_entry_safe(pos, n, head, member)                      \
+       for (pos = nl_list_entry((head)->next, typeof(*pos), member),   \
+               n = nl_list_entry(pos->member.next, typeof(*pos), member);      \
+            &(pos)->member != (head);                                  \
+            pos = n, n = nl_list_entry(n->member.next, typeof(*n), member))
+
+#define nl_init_list_head(head) \
+       do { (head)->next = (head); (head)->prev = (head); } while (0)
+
+#endif
diff --git a/include/netlink/msg.h b/include/netlink/msg.h
new file mode 100644 (file)
index 0000000..fa96ccb
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+ * netlink/msg.c               Netlink Messages Interface
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_MSG_H_
+#define NETLINK_MSG_H_
+
+#include <netlink/netlink.h>
+#include <netlink/object.h>
+#include <netlink/attr.h>
+
+struct nl_msg;
+struct nl_tree;
+struct ucred;
+
+/* size calculations */
+extern int               nlmsg_msg_size(int);
+extern int               nlmsg_total_size(int);
+extern int               nlmsg_padlen(int);
+
+/* payload access */
+extern void *            nlmsg_data(const struct nlmsghdr *);
+extern int               nlmsg_len(const struct nlmsghdr *);
+extern void *            nlmsg_tail(const struct nlmsghdr *);
+
+/* attribute access */
+extern struct nlattr *   nlmsg_attrdata(const struct nlmsghdr *, int);
+extern int               nlmsg_attrlen(const struct nlmsghdr *, int);
+
+/* message parsing */
+extern int               nlmsg_ok(const struct nlmsghdr *, int);
+extern struct nlmsghdr *  nlmsg_next(struct nlmsghdr *, int *);
+extern int               nlmsg_parse(struct nlmsghdr *, int, struct nlattr **,
+                                     int, struct nla_policy *);
+extern struct nlattr *   nlmsg_find_attr(struct nlmsghdr *, int, int);
+extern int               nlmsg_validate(struct nlmsghdr *, int, int,
+                                        struct nla_policy *);
+
+extern struct nl_msg *   nlmsg_new(void);
+extern struct nl_msg *   nlmsg_build(struct nlmsghdr *);
+extern struct nl_msg *   nlmsg_build_simple(int, int);
+extern struct nl_msg *   nlmsg_build_no_hdr(void);
+extern struct nl_msg *   nlmsg_convert(struct nlmsghdr *);
+extern int               nlmsg_append(struct nl_msg *, void *, size_t, int);
+
+extern struct nlmsghdr *  nlmsg_hdr(struct nl_msg *);
+extern void              nlmsg_free(struct nl_msg *);
+
+/* attribute modification */
+extern void              nlmsg_set_proto(struct nl_msg *, int);
+extern int               nlmsg_get_proto(struct nl_msg *);
+extern void              nlmsg_set_src(struct nl_msg *, struct sockaddr_nl *);
+extern struct sockaddr_nl *nlmsg_get_src(struct nl_msg *);
+extern void              nlmsg_set_dst(struct nl_msg *, struct sockaddr_nl *);
+extern struct sockaddr_nl *nlmsg_get_dst(struct nl_msg *);
+extern void              nlmsg_set_creds(struct nl_msg *, struct ucred *);
+extern struct ucred *    nlmsg_get_creds(struct nl_msg *);
+
+extern char *            nl_nlmsgtype2str(int, char *, size_t);
+extern int               nl_str2nlmsgtype(const char *);
+
+extern char *            nl_nlmsg_flags2str(int, char *, size_t);
+
+extern int               nl_msg_parse(struct nl_msg *,
+                                      void (*cb)(struct nl_object *, void *),
+                                      void *);
+
+/**
+ * @name Iterators
+ * @{
+ */
+
+/**
+ * @ingroup msg
+ * Iterate over a stream of attributes in a message
+ * @arg pos    loop counter, set to current attribute
+ * @arg nlh    netlink message header
+ * @arg hdrlen length of family header
+ * @arg rem    initialized to len, holds bytes currently remaining in stream
+ */
+#define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
+       nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
+                         nlmsg_attrlen(nlh, hdrlen), rem)
+
+/**
+ * Iterate over a stream of messages
+ * @arg pos    loop counter, set to current message
+ * @arg head   head of message stream
+ * @arg len    length of message stream
+ * @arg rem    initialized to len, holds bytes currently remaining in stream
+ */
+#define nlmsg_for_each_msg(pos, head, len, rem) \
+       for (pos = head, rem = len; \
+            nlmsg_ok(pos, rem); \
+            pos = nlmsg_next(pos, &(rem)))
+
+/** @} */
+
+#endif
diff --git a/include/netlink/netlink-compat.h b/include/netlink/netlink-compat.h
new file mode 100644 (file)
index 0000000..17ec9fc
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * netlink/netlink-compat.h    Netlink Compatability
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_COMPAT_H_
+#define NETLINK_COMPAT_H_
+
+#if !defined _LINUX_SOCKET_H && !defined _BITS_SOCKADDR_H
+typedef unsigned short  sa_family_t;
+#endif
+
+#ifndef IFNAMSIZ 
+/** Maximum length of a interface name */
+#define IFNAMSIZ 16
+#endif
+
+/* patch 2.4.x if_arp */
+#ifndef ARPHRD_INFINIBAND
+#define ARPHRD_INFINIBAND 32
+#endif
+
+/* patch 2.4.x eth header file */
+#ifndef ETH_P_MPLS_UC
+#define ETH_P_MPLS_UC  0x8847 
+#endif
+
+#ifndef ETH_P_MPLS_MC
+#define ETH_P_MPLS_MC   0x8848
+#endif
+
+#ifndef  ETH_P_EDP2
+#define ETH_P_EDP2      0x88A2
+#endif
+
+#ifndef ETH_P_HDLC
+#define ETH_P_HDLC      0x0019 
+#endif
+
+#ifndef AF_LLC
+#define AF_LLC         26
+#endif
+
+#endif
diff --git a/include/netlink/netlink-kernel.h b/include/netlink/netlink-kernel.h
new file mode 100644 (file)
index 0000000..134710d
--- /dev/null
@@ -0,0 +1,242 @@
+#ifndef __LINUX_NETLINK_H
+#define __LINUX_NETLINK_H
+
+#define NETLINK_ROUTE          0       /* Routing/device hook */
+#define NETLINK_W1             1       /* 1-wire subsystem */
+#define NETLINK_USERSOCK       2       /* Reserved for user mode socket protocols      */
+#define NETLINK_FIREWALL       3       /* Firewalling hook             */
+#define NETLINK_INET_DIAG      4       /* INET socket monitoring */
+#define NETLINK_TCPDIAG                NETLINK_INET_DIAG
+#define NETLINK_NFLOG          5       /* netfilter/iptables ULOG */
+#define NETLINK_XFRM           6       /* ipsec */
+#define NETLINK_SELINUX                7       /* SELinux event notifications */
+#define NETLINK_ISCSI          8       /* Open-iSCSI */
+#define NETLINK_AUDIT          9       /* auditing */
+#define NETLINK_FIB_LOOKUP     10
+#define NETLINK_CONNECTOR      11
+#define NETLINK_NETFILTER      12
+#define NETLINK_IP6_FW         13
+#define NETLINK_DNRTMSG                14      /* DECnet routing messages */
+#define NETLINK_KOBJECT_UEVENT 15      /* Kernel messages to userspace */
+#define NETLINK_GENERIC                16
+
+#define MAX_LINKS 32           
+
+/**
+ * Netlink socket address
+ * @ingroup nl
+ */
+struct sockaddr_nl
+{
+       /** socket family (AF_NETLINK) */
+       sa_family_t     nl_family;
+
+       /** Padding (unused) */
+       unsigned short  nl_pad;
+
+       /** Unique process ID  */
+       uint32_t        nl_pid;
+
+       /** Multicast group subscriptions */
+       uint32_t        nl_groups;
+};
+
+/**
+ * Netlink message header
+ * @ingroup msg
+ */
+struct nlmsghdr
+{
+       /**
+        * Length of message including header.
+        */
+       uint32_t        nlmsg_len;
+
+       /**
+        * Message type (content type)
+        */
+       uint16_t        nlmsg_type;
+
+       /**
+        * Message flags
+        */
+       uint16_t        nlmsg_flags;
+
+       /**
+        * Sequence number
+        */
+       uint32_t        nlmsg_seq;
+
+       /**
+        * Netlink PID of the proccess sending the message.
+        */
+       uint32_t        nlmsg_pid;
+};
+
+/**
+ * @name Standard message flags
+ * @{
+ */
+
+/**
+ * Must be set on all request messages (typically from user space to
+ * kernel space).
+ * @ingroup msg
+ */
+#define NLM_F_REQUEST          1
+
+/**
+ * Indicates the message is part of a multipart message terminated
+ * by NLMSG_DONE.
+ */
+#define NLM_F_MULTI            2
+
+/**
+ * Request for an acknowledgment on success.
+ */
+#define NLM_F_ACK              4
+
+/**
+ * Echo this request
+ */
+#define NLM_F_ECHO             8
+
+/** @} */
+
+/**
+ * @name Additional message flags for GET requests
+ * @{
+ */
+
+/**
+ * Return the complete table instead of a single entry.
+ * @ingroup msg
+ */
+#define NLM_F_ROOT     0x100
+
+/**
+ * Return all entries matching criteria passed in message content.
+ */
+#define NLM_F_MATCH    0x200
+
+/**
+ * Return an atomic snapshot of the table being referenced. This
+ * may require special privileges because it has the potential to
+ * interrupt service in the FE for a longer time.
+ */
+#define NLM_F_ATOMIC   0x400
+
+/**
+ * Dump all entries
+ */
+#define NLM_F_DUMP     (NLM_F_ROOT|NLM_F_MATCH)
+
+/** @} */
+
+/**
+ * @name Additional messsage flags for NEW requests
+ * @{
+ */
+
+/**
+ * Replace existing matching config object with this request.
+ * @ingroup msg
+ */
+#define NLM_F_REPLACE  0x100
+
+/**
+ * Don't replace the config object if it already exists.
+ */
+#define NLM_F_EXCL     0x200
+
+/**
+ * Create config object if it doesn't already exist.
+ */
+#define NLM_F_CREATE   0x400
+
+/**
+ * Add to the end of the object list.
+ */
+#define NLM_F_APPEND   0x800
+
+/** @} */
+
+#define NLMSG_ALIGNTO  4
+#define NLMSG_ALIGN(len) ( ((len)+NLMSG_ALIGNTO-1) & ~(NLMSG_ALIGNTO-1) )
+#define NLMSG_HDRLEN    ((int) NLMSG_ALIGN(sizeof(struct nlmsghdr)))
+
+/**
+ * @name Standard Message types
+ * @{
+ */
+
+/**
+ * No operation, message must be ignored
+ * @ingroup msg
+ */
+#define NLMSG_NOOP             0x1
+
+/**
+ * The message signals an error and the payload contains a nlmsgerr
+ * structure. This can be looked at as a NACK and typically it is
+ * from FEC to CPC.
+ */
+#define NLMSG_ERROR            0x2
+
+/**
+ * Message terminates a multipart message.
+ */
+#define NLMSG_DONE             0x3
+
+/**
+ * The message signals that data got lost
+ */
+#define NLMSG_OVERRUN          0x4
+
+/**
+ * Lower limit of reserved message types
+ */
+#define NLMSG_MIN_TYPE         0x10
+
+/** @} */
+
+/**
+ * Netlink error message
+ * @ingroup msg
+ */
+struct nlmsgerr
+{
+       /** Error code (errno number) */
+       int             error;
+
+       /** Original netlink message causing the error */
+       struct nlmsghdr msg;
+};
+
+#define NETLINK_ADD_MEMBERSHIP 1
+#define NETLINK_DROP_MEMBERSHIP        2
+#define NETLINK_PKTINFO                3
+
+struct nl_pktinfo
+{
+       __u32   group;
+};
+
+/**
+ * Netlink Attribute
+ * @ingroup attr
+ */
+struct nlattr
+{
+       /** Attribute length */
+       __u16           nla_len;
+
+       /** Attribute type */
+       __u16           nla_type;
+};
+
+#define NLA_ALIGNTO            4
+#define NLA_ALIGN(len)         (((len) + NLA_ALIGNTO - 1) & ~(NLA_ALIGNTO - 1))
+#define NLA_HDRLEN             ((int) NLA_ALIGN(sizeof(struct nlattr)))
+
+#endif /* __LINUX_NETLINK_H */
diff --git a/include/netlink/netlink.h b/include/netlink/netlink.h
new file mode 100644 (file)
index 0000000..6bdbb3d
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * netlink/netlink.h           Netlink Interface
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_H_
+#define NETLINK_H_
+
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netlink/types.h>
+#include <netlink/netlink-compat.h>
+#include <netlink/netlink-kernel.h>
+#include <netlink/rtnetlink-kernel.h>
+#include <netlink/handlers.h>
+
+extern int nl_debug;
+
+struct nl_handle;
+
+/* General */
+extern struct nl_handle *      nl_handle_alloc(void);
+extern struct nl_handle *      nl_handle_alloc_nondefault(enum nl_cb_kind);
+extern void                    nl_handle_destroy(struct nl_handle *);
+extern void                    nl_join_groups(struct nl_handle *, int);
+extern void                    nl_disable_sequence_check(struct nl_handle *);
+
+extern int                     nl_join_group(struct nl_handle *, int);
+extern int                     nl_set_passcred(struct nl_handle *, int);
+
+/* Access Functions */
+extern pid_t                   nl_handle_get_pid(struct nl_handle *);
+extern void                    nl_handle_set_pid(struct nl_handle *, pid_t);
+extern pid_t                   nl_handle_get_peer_pid(struct nl_handle *);
+extern void                    nl_handle_set_peer_pid(struct nl_handle *, pid_t);
+extern int                     nl_handle_get_fd(struct nl_handle *);
+extern struct sockaddr_nl *    nl_handle_get_local_addr(struct nl_handle *);
+extern struct sockaddr_nl *    nl_handle_get_peer_addr(struct nl_handle *);
+extern struct nl_cb *          nl_handle_get_cb(struct nl_handle *);
+
+/* Connection Management */
+extern int                     nl_connect(struct nl_handle *, int);
+extern void                    nl_close(struct nl_handle *);
+
+/* Send */
+extern int                     nl_sendto(struct nl_handle *, void *, size_t);
+extern int                     nl_sendmsg(struct nl_handle *, struct nl_msg *,
+                                          struct msghdr *);
+extern int                     nl_send(struct nl_handle *, struct nl_msg *);
+extern int                     nl_send_auto_complete(struct nl_handle *,
+                                                     struct nl_msg *);
+extern int                     nl_send_simple(struct nl_handle *, int, int,
+                                              void *, size_t);
+
+/* Receive */
+extern int                     nl_recv(struct nl_handle *,
+                                       struct sockaddr_nl *, unsigned char **,
+                                       struct ucred **);
+
+extern int                     nl_recvmsgs(struct nl_handle *, struct nl_cb *);
+extern int                     nl_recvmsgs_def(struct nl_handle *);
+
+extern int                     nl_wait_for_ack(struct nl_handle *);
+
+/* Netlink Family Translations */
+extern char *                  nl_nlfamily2str(int, char *, size_t);
+extern int                     nl_str2nlfamily(const char *);
+
+#endif
diff --git a/include/netlink/object.h b/include/netlink/object.h
new file mode 100644 (file)
index 0000000..6999ff2
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * netlink/object.c    Generic Cacheable Object
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_OBJECT_H_
+#define NETLINK_OBJECT_H_
+
+#include <netlink/netlink.h>
+#include <netlink/utils.h>
+
+struct nl_cache;
+struct nl_object;
+struct nl_cache_ops;
+
+/* General */
+extern struct nl_object *      nl_object_alloc(size_t size);
+extern struct nl_object *      nl_object_alloc_from_ops(struct nl_cache_ops *);
+extern struct nl_object *      nl_object_alloc_name(const char *);
+extern void                    nl_object_free(struct nl_object *);
+extern struct nl_object *      nl_object_clone(struct nl_object *obj);
+extern void                    nl_object_get(struct nl_object *);
+extern void                    nl_object_put(struct nl_object *);
+extern int                     nl_object_shared(struct nl_object *);
+extern void                    nl_object_dump(struct nl_object *,
+                                              struct nl_dump_params *);
+extern int                     nl_object_match(struct nl_object *,
+                                               struct nl_object *);
+
+/* Access Functions */
+extern int                     nl_object_get_refcnt(struct nl_object *);
+extern struct nl_cache_ops *   nl_object_get_ops(struct nl_object *);
+extern struct nl_cache *       nl_object_get_cache(struct nl_object *);
+extern inline void *           nl_object_priv(struct nl_object *);
+
+#endif
diff --git a/include/netlink/route/addr.h b/include/netlink/route/addr.h
new file mode 100644 (file)
index 0000000..91a780d
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * netlink/route/addr.c                rtnetlink addr layer
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ *                         Baruch Even <baruch@ev-en.org>,
+ *                         Mediatrix Telecom, inc. <ericb@mediatrix.com>
+ */
+
+#ifndef NETADDR_ADDR_H_
+#define NETADDR_ADDR_H_
+
+#include <netlink/netlink.h>
+#include <netlink/cache.h>
+#include <netlink/addr.h>
+
+struct rtnl_addr;
+
+/* General */
+extern struct rtnl_addr *rtnl_addr_alloc(void);
+extern void            rtnl_addr_put(struct rtnl_addr *);
+extern void            rtnl_addr_free(struct rtnl_addr *);
+
+extern struct nl_cache *rtnl_addr_alloc_cache(struct nl_handle *);
+
+/* Address Addition */
+extern struct nl_msg * rtnl_addr_build_add_request(struct rtnl_addr *, int);
+extern int             rtnl_addr_add(struct nl_handle *, struct rtnl_addr *,
+                                     int);
+
+/* Address Deletion */
+extern struct nl_msg * rtnl_addr_build_delete_request(struct rtnl_addr *, int);
+extern int             rtnl_addr_delete(struct nl_handle *,
+                                        struct rtnl_addr *, int);
+
+/* Address Flags Translations */
+extern char *          rtnl_addr_flags2str(int, char *, size_t);
+extern int             rtnl_addr_str2flags(const char *);
+
+/* Attribute Access */
+extern void            rtnl_addr_set_label(struct rtnl_addr *, const char *);
+extern char *          rtnl_addr_get_label(struct rtnl_addr *);
+
+extern void            rtnl_addr_set_ifindex(struct rtnl_addr *, int);
+extern int             rtnl_addr_get_ifindex(struct rtnl_addr *);
+
+extern void            rtnl_addr_set_family(struct rtnl_addr *, int);
+extern int             rtnl_addr_get_family(struct rtnl_addr *);
+
+extern void            rtnl_addr_set_prefixlen(struct rtnl_addr *, int);
+extern int             rtnl_addr_get_prefixlen(struct rtnl_addr *);
+
+extern void            rtnl_addr_set_scope(struct rtnl_addr *, int);
+extern int             rtnl_addr_get_scope(struct rtnl_addr *);
+
+extern void            rtnl_addr_set_flags(struct rtnl_addr *, unsigned int);
+extern void            rtnl_addr_unset_flags(struct rtnl_addr *, unsigned int);
+extern unsigned int    rtnl_addr_get_flags(struct rtnl_addr *);
+
+extern int             rtnl_addr_set_local(struct rtnl_addr *,
+                                           struct nl_addr *);
+extern struct nl_addr *        rtnl_addr_get_local(struct rtnl_addr *);
+
+extern int             rtnl_addr_set_peer(struct rtnl_addr *,
+                                          struct nl_addr *);
+extern struct nl_addr *        rtnl_addr_get_peer(struct rtnl_addr *);
+
+extern int             rtnl_addr_set_broadcast(struct rtnl_addr *,
+                                               struct nl_addr *);
+extern struct nl_addr *        rtnl_addr_get_broadcast(struct rtnl_addr *);
+
+extern int             rtnl_addr_set_anycast(struct rtnl_addr *,
+                                             struct nl_addr *);
+extern struct nl_addr *        rtnl_addr_get_anycast(struct rtnl_addr *);
+
+extern int             rtnl_addr_set_multicast(struct rtnl_addr *,
+                                               struct nl_addr *);
+extern struct nl_addr *        rtnl_addr_get_multicast(struct rtnl_addr *);
+
+#endif
diff --git a/include/netlink/route/class-modules.h b/include/netlink/route/class-modules.h
new file mode 100644 (file)
index 0000000..b524a6c
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * netlink/route/class-modules.h       Class Module API
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_CLASS_MODULES_H_
+#define NETLINK_CLASS_MODULES_H_
+
+#include <netlink/netlink.h>
+
+/**
+ * Class operations
+ * @ingroup class
+ */
+struct rtnl_class_ops
+{
+       /**
+        * Kind/Name of class
+        */
+       char co_kind[32];
+
+       /**
+        * Dump callbacks
+        */
+       int (*co_dump[NL_DUMP_MAX+1])(struct rtnl_class *,
+                                     struct nl_dump_params *, int);
+
+       /**
+        * Must return the contents supposed to be in TCA_OPTIONS
+        */
+       struct nl_msg *(*co_get_opts)(struct rtnl_class *);
+
+       /**
+        * TCA_OPTIONS message parser
+        */
+       int  (*co_msg_parser)(struct rtnl_class *);
+
+       /**
+        * Called before a class object gets destroyed
+        */
+       void (*co_free_data)(struct rtnl_class *);
+
+       /**
+        * INTERNAL (Do not use)
+        */
+       struct rtnl_class_ops *co_next;
+};
+
+extern int     rtnl_class_register(struct rtnl_class_ops *);
+extern int     rtnl_class_unregister(struct rtnl_class_ops *);
+
+#endif
diff --git a/include/netlink/route/class.h b/include/netlink/route/class.h
new file mode 100644 (file)
index 0000000..3b7c106
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * netlink/route/class.h       Classes
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_CLASS_H_
+#define NETLINK_CLASS_H_
+
+#include <netlink/netlink.h>
+#include <netlink/route/tc.h>
+#include <netlink/route/qdisc.h>
+
+struct rtnl_class;
+
+/* General */
+extern struct rtnl_class *     rtnl_class_alloc(void);
+extern void                    rtnl_class_put(struct rtnl_class *);
+extern void                    rtnl_class_free(struct rtnl_class *);
+extern struct nl_cache *       rtnl_class_alloc_cache(struct nl_handle *, int);
+
+/* leaf qdisc access */
+extern int                     rtnl_class_has_leaf_qdisc(struct rtnl_class *);
+extern struct rtnl_qdisc *     rtnl_class_leaf_qdisc(struct rtnl_class *,
+                                                     struct nl_cache *);
+
+/* class addition */
+extern struct nl_msg * rtnl_class_build_add_request(struct rtnl_class *, int);
+extern int rtnl_class_add(struct nl_handle *, struct rtnl_class *, int);
+
+/* attribute modification */
+extern void            rtnl_class_set_ifindex(struct rtnl_class *, int);
+extern int             rtnl_class_get_ifindex(struct rtnl_class *);
+extern void            rtnl_class_set_handle(struct rtnl_class *, uint32_t);
+extern uint32_t                rtnl_class_get_handle(struct rtnl_class *);
+extern void            rtnl_class_set_parent(struct rtnl_class *, uint32_t);
+extern uint32_t                rtnl_class_get_parent(struct rtnl_class *);
+extern void            rtnl_class_set_kind(struct rtnl_class *, const char *);
+extern char *          rtnl_class_get_kind(struct rtnl_class *);
+extern uint64_t                rtnl_class_get_stat(struct rtnl_class *,
+                                           enum rtnl_tc_stats_id);
+
+/* iterators */
+extern void            rtnl_class_foreach_child(struct rtnl_class *,
+                                                struct nl_cache *,
+                                                void (*cb)(struct nl_object *,
+                                                           void *),
+                                                void *);
+extern void            rtnl_class_foreach_cls(struct rtnl_class *,
+                                              struct nl_cache *,
+                                              void (*cb)(struct nl_object *,
+                                                         void *),
+                                              void *);
+
+#endif
diff --git a/include/netlink/route/classifier-modules.h b/include/netlink/route/classifier-modules.h
new file mode 100644 (file)
index 0000000..1fa84c5
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * netlink/route/classifier-modules.h   Classifier Module API
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_CLASS_MODULES_H_
+#define NETLINK_CLASS_MODULES_H_
+
+#include <netlink/netlink.h>
+
+/**
+ * Classifier operations
+ * @ingroup cls
+ */
+struct rtnl_cls_ops
+{
+       /**
+        * Kind/Name of classifier
+        */
+       char co_kind[32];
+
+       /**
+        * Dump callbacks
+        */
+       int (*co_dump[NL_DUMP_MAX+1])(struct rtnl_cls *,
+                                     struct nl_dump_params *, int);
+       /**
+        * Must return the contents supposed to be in TCA_OPTIONS
+        */
+       struct nl_msg *(*co_get_opts)(struct rtnl_cls *);
+
+       /**
+        * TCA_OPTIONS message parser
+        */
+       int (*co_msg_parser)(struct rtnl_cls *);
+
+       /**
+        * Called before a class object gets destroyed
+        */
+       void (*co_free_data)(struct rtnl_cls *);
+
+       /**
+        * INTERNAL (Do not use)
+        */
+       struct rtnl_cls_ops *co_next;
+};
+
+extern int rtnl_cls_register(struct rtnl_cls_ops *);
+extern int rtnl_cls_unregister(struct rtnl_cls_ops *);
+
+#endif
diff --git a/include/netlink/route/classifier.h b/include/netlink/route/classifier.h
new file mode 100644 (file)
index 0000000..c213725
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * netlink/route/classifier.h       Classifiers
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_CLASSIFIER_H_
+#define NETLINK_CLASSIFIER_H_
+
+#include <netlink/netlink.h>
+#include <netlink/cache.h>
+#include <netlink/route/tc.h>
+#include <netlink/utils.h>
+
+extern struct          rtnl_cls *rtnl_cls_alloc(void);
+extern void            rtnl_cls_put(struct rtnl_cls *);
+extern void            rtnl_cls_free(struct rtnl_cls *);
+
+extern struct nl_cache *rtnl_cls_alloc_cache(struct nl_handle *, int, uint32_t);
+
+/* classifier addition */
+extern int             rtnl_cls_add(struct nl_handle *, struct rtnl_cls *,
+                                    int);
+extern struct nl_msg * rtnl_cls_build_add_request(struct rtnl_cls *, int);
+
+extern struct nl_msg *rtnl_cls_build_change_request(struct rtnl_cls *, int);
+extern struct nl_msg *rtnl_cls_build_delete_request(struct rtnl_cls *, int);
+extern int  rtnl_cls_delete(struct nl_handle *, struct rtnl_cls *, int);
+
+/* attribute modification */
+extern void rtnl_cls_set_ifindex(struct rtnl_cls *, int);
+extern void rtnl_cls_set_handle(struct rtnl_cls *, uint32_t);
+extern void rtnl_cls_set_parent(struct rtnl_cls *, uint32_t);
+extern void rtnl_cls_set_kind(struct rtnl_cls *, const char *);
+
+extern void rtnl_cls_set_prio(struct rtnl_cls *, int);
+extern int  rtnl_cls_get_prio(struct rtnl_cls *);
+
+extern void rtnl_cls_set_protocol(struct rtnl_cls *, int);
+extern int  rtnl_cls_get_protocol(struct rtnl_cls *);
+
+#endif
diff --git a/include/netlink/route/cls/fw.h b/include/netlink/route/cls/fw.h
new file mode 100644 (file)
index 0000000..eaa97b2
--- /dev/null
@@ -0,0 +1,21 @@
+/*
+ * netlink/route/cls/fw.h      fw classifier
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ * Copyright (c) 2006 Petr Gotthard <petr.gotthard@siemens.com>
+ * Copyright (c) 2006 Siemens AG Oesterreich
+ */
+
+#ifndef NETLINK_FW_H_
+#define NETLINK_FW_H_
+
+#include <netlink/netlink.h>
+
+extern int     rtnl_fw_set_classid(struct rtnl_cls *, uint32_t);
+
+#endif
diff --git a/include/netlink/route/cls/police.h b/include/netlink/route/cls/police.h
new file mode 100644 (file)
index 0000000..4227c36
--- /dev/null
@@ -0,0 +1,21 @@
+/*
+ * netlink/route/cls/police.h  Policer
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_CLS_POLICE_H_
+#define NETLINK_CLS_POLICE_H_
+
+#include <netlink/netlink.h>
+#include <netlink/cache.h>
+
+extern char *  nl_police2str(int, char *, size_t);
+extern int     nl_str2police(const char *);
+
+#endif
diff --git a/include/netlink/route/cls/u32.h b/include/netlink/route/cls/u32.h
new file mode 100644 (file)
index 0000000..b258d62
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * netlink/route/cls/u32.h     u32 classifier
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_U32_H_
+#define NETLINK_U32_H_
+
+#include <netlink/netlink.h>
+#include <netlink/cache.h>
+
+extern void    rtnl_u32_set_handle(struct rtnl_cls *, int, int, int);
+extern int     rtnl_u32_set_classid(struct rtnl_cls *, uint32_t);
+
+extern int     rtnl_u32_set_flags(struct rtnl_cls *, int);
+extern int     rtnl_u32_add_key(struct rtnl_cls *, uint32_t, uint32_t,
+                                int, int);
+extern int     rtnl_u32_add_key_uint8(struct rtnl_cls *, uint8_t, uint8_t,
+                                      int, int);
+extern int     rtnl_u32_add_key_uint16(struct rtnl_cls *, uint16_t, uint16_t,
+                                       int, int);
+extern int     rtnl_u32_add_key_uint32(struct rtnl_cls *, uint32_t, uint32_t,
+                                       int, int);
+extern int     rtnl_u32_add_key_in_addr(struct rtnl_cls *, struct in_addr *,
+                                        uint8_t, int, int);
+extern int     rtnl_u32_add_key_in6_addr(struct rtnl_cls *, struct in6_addr *,
+                                         uint8_t, int, int);
+
+#endif
diff --git a/include/netlink/route/link.h b/include/netlink/route/link.h
new file mode 100644 (file)
index 0000000..28a0085
--- /dev/null
@@ -0,0 +1,143 @@
+/*
+ * netlink/route/link.h                Links (Interfaces)
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_LINK_H_
+#define NETLINK_LINK_H_
+
+#include <netlink/netlink.h>
+#include <netlink/cache.h>
+#include <netlink/addr.h>
+
+struct rtnl_link;
+
+enum rtnl_link_st {
+       RTNL_LINK_RX_PACKETS,
+       RTNL_LINK_TX_PACKETS,
+       RTNL_LINK_RX_BYTES,
+       RTNL_LINK_TX_BYTES,
+       RTNL_LINK_RX_ERRORS,
+       RTNL_LINK_TX_ERRORS,
+       RTNL_LINK_RX_DROPPED,
+       RTNL_LINK_TX_DROPPED,
+       RTNL_LINK_RX_COMPRESSED,
+       RTNL_LINK_TX_COMPRESSED,
+       RTNL_LINK_RX_FIFO_ERR,
+       RTNL_LINK_TX_FIFO_ERR,
+       RTNL_LINK_RX_LEN_ERR,
+       RTNL_LINK_RX_OVER_ERR,
+       RTNL_LINK_RX_CRC_ERR,
+       RTNL_LINK_RX_FRAME_ERR,
+       RTNL_LINK_RX_MISSED_ERR,
+       RTNL_LINK_TX_ABORT_ERR,
+       RTNL_LINK_TX_CARRIER_ERR,
+       RTNL_LINK_TX_HBEAT_ERR,
+       RTNL_LINK_TX_WIN_ERR,
+       RTNL_LINK_TX_COLLISIONS,
+       RTNL_LINK_MULTICAST,
+       __RTNL_LINK_STATS_MAX,
+};
+
+#define RTNL_LINK_STATS_MAX (__RTNL_LINK_STATS_MAX - 1)
+
+/**
+ * Special interface index stating the link was not found.
+ * @ingroup link
+ */
+#define RTNL_LINK_NOT_FOUND -1
+
+/* link object allocation/freeage */
+extern struct rtnl_link *      rtnl_link_alloc(void);
+extern void                    rtnl_link_put(struct rtnl_link *);
+extern void                    rtnl_link_free(struct rtnl_link *);
+
+/* link cache management */
+extern struct nl_cache *       rtnl_link_alloc_cache(struct nl_handle *);
+extern struct rtnl_link *      rtnl_link_get(struct nl_cache *, int);
+extern struct rtnl_link *      rtnl_link_get_by_name(struct nl_cache *,
+                                                     const char *);
+
+
+/* Link Modifications */
+extern struct nl_msg *         rtnl_link_build_change_request(struct rtnl_link *,
+                                                              struct rtnl_link *,
+                                                              int);
+extern int                     rtnl_link_change(struct nl_handle *,
+                                                struct rtnl_link *,
+                                                struct rtnl_link *, int);
+
+/* Name <-> Index Translations */
+extern char *                  rtnl_link_i2name(struct nl_cache *, int,
+                                                char *, size_t);
+extern int                     rtnl_link_name2i(struct nl_cache *,
+                                                const char *);
+
+/* Name <-> Statistic Translations */
+extern char *                  rtnl_link_stat2str(int, char *, size_t);
+extern int                     rtnl_link_str2stat(const char *);
+
+/* Link Flags Translations */
+extern char *                  rtnl_link_flags2str(int, char *, size_t);
+extern int                     rtnl_link_str2flags(const char *);
+
+/* Access Functions */
+extern void                    rtnl_link_set_qdisc(struct rtnl_link *,
+                                                   const char *);
+extern char *                  rtnl_link_get_qdisc(struct rtnl_link *);
+
+extern void                    rtnl_link_set_name(struct rtnl_link *,
+                                                  const char *);
+extern char *                  rtnl_link_get_name(struct rtnl_link *);
+
+extern void                    rtnl_link_set_flags(struct rtnl_link *,
+                                                   unsigned int);
+extern void                    rtnl_link_unset_flags(struct rtnl_link *,
+                                                     unsigned int);
+extern unsigned int            rtnl_link_get_flags(struct rtnl_link *);
+
+extern void                    rtnl_link_set_mtu(struct rtnl_link *,
+                                                 unsigned int);
+extern unsigned int            rtnl_link_get_mtu(struct rtnl_link *);
+
+extern void                    rtnl_link_set_txqlen(struct rtnl_link *,
+                                                    unsigned int);
+extern unsigned int            rtnl_link_get_txqlen(struct rtnl_link *);
+
+extern void                    rtnl_link_set_weight(struct rtnl_link *,
+                                                    unsigned int);
+extern unsigned int            rtnl_link_get_weight(struct rtnl_link *);
+
+extern void                    rtnl_link_set_ifindex(struct rtnl_link *, int);
+extern int                     rtnl_link_get_ifindex(struct rtnl_link *);
+
+extern void                    rtnl_link_set_family(struct rtnl_link *, int);
+extern int                     rtnl_link_get_family(struct rtnl_link *);
+
+extern void                    rtnl_link_set_arptype(struct rtnl_link *,
+                                                     unsigned int);
+extern unsigned int            rtnl_link_get_arptype(struct rtnl_link *);
+
+extern void                    rtnl_link_set_addr(struct rtnl_link *,
+                                                  struct nl_addr *);
+extern struct nl_addr *                rtnl_link_get_addr(struct rtnl_link *);
+
+extern void                    rtnl_link_set_broadcast(struct rtnl_link *,
+                                                       struct nl_addr *);
+extern struct nl_addr *                rtnl_link_get_broadcast(struct rtnl_link *);
+
+extern void                    rtnl_link_set_link(struct rtnl_link *, int);
+extern int                     rtnl_link_get_link(struct rtnl_link *);
+
+extern void                    rtnl_link_set_master(struct rtnl_link *, int);
+extern int                     rtnl_link_get_master(struct rtnl_link *);
+
+extern uint64_t                rtnl_link_get_stat(struct rtnl_link *, int);
+
+#endif
diff --git a/include/netlink/route/neighbour.h b/include/netlink/route/neighbour.h
new file mode 100644 (file)
index 0000000..f2cda96
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * netlink/route/neighbour.h   Neighbours
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_NEIGHBOUR_H_
+#define NETLINK_NEIGHBOUR_H_
+
+#include <netlink/netlink.h>
+#include <netlink/cache.h>
+#include <netlink/addr.h>
+
+struct rtnl_neigh;
+
+/* neighbour object allocation/freeage */
+extern struct rtnl_neigh *     rtnl_neigh_alloc(void);
+extern void                    rtnl_neigh_put(struct rtnl_neigh *);
+extern void                    rtnl_neigh_free(struct rtnl_neigh *);
+
+/* neighbour cache management */
+extern struct nl_cache *       rtnl_neigh_alloc_cache(struct nl_handle *);
+extern struct rtnl_neigh *     rtnl_neigh_get(struct nl_cache *, int,
+                                              struct nl_addr *);
+
+/* Neigbour state translations */
+extern char *                  rtnl_neigh_state2str(int, char *, size_t);
+extern int                     rtnl_neigh_str2state(const char *);
+
+/* Neighbour flags translations */
+extern char *                  rtnl_neigh_flags2str(int, char *, size_t);
+extern int                     rtnl_neigh_str2flag(const char *);
+
+/* Neighbour Addition */
+extern int                     rtnl_neigh_add(struct nl_handle *,
+                                              struct rtnl_neigh *, int);
+extern struct nl_msg *         rtnl_neigh_build_add_request(struct rtnl_neigh *, int);
+
+/* Neighbour Modification */
+extern int                     rtnl_neigh_change(struct nl_handle *,
+                                                 struct rtnl_neigh *, int);
+extern struct nl_msg *         rtnl_neigh_build_change_request(struct rtnl_neigh *, int);
+
+/* Neighbour Deletion */
+extern int                     rtnl_neigh_delete(struct nl_handle *,
+                                                 struct rtnl_neigh *, int);
+extern struct nl_msg *         rtnl_neigh_build_delete_request(struct rtnl_neigh *, int);
+
+/* Access functions */
+extern void                    rtnl_neigh_set_state(struct rtnl_neigh *, int);
+extern int                     rtnl_neigh_get_state(struct rtnl_neigh *);
+extern void                    rtnl_neigh_unset_state(struct rtnl_neigh *,
+                                                      int);
+
+extern void                    rtnl_neigh_set_flags(struct rtnl_neigh *,
+                                                    unsigned int);
+extern void                    rtnl_neigh_unset_flags(struct rtnl_neigh *,
+                                                      unsigned int);
+extern unsigned int            rtnl_neigh_get_flags(struct rtnl_neigh *);
+
+extern void                    rtnl_neigh_set_ifindex(struct rtnl_neigh *,
+                                                      int);
+extern int                     rtnl_neigh_get_ifindex(struct rtnl_neigh *);
+
+extern void                    rtnl_neigh_set_lladdr(struct rtnl_neigh *,
+                                                     struct nl_addr *);
+extern struct nl_addr *                rtnl_neigh_get_lladdr(struct rtnl_neigh *);
+
+extern int                     rtnl_neigh_set_dst(struct rtnl_neigh *,
+                                                  struct nl_addr *);
+extern struct nl_addr *                rtnl_neigh_get_dst(struct rtnl_neigh *);
+
+extern void                    rtnl_neigh_set_type(struct rtnl_neigh *, int);
+extern int                     rtnl_neigh_get_type(struct rtnl_neigh *);
+
+extern void                    rtnl_neigh_set_family(struct rtnl_neigh *, int);
+extern int                     rtnl_neigh_get_family(struct rtnl_neigh *);
+
+#endif
diff --git a/include/netlink/route/neightbl.h b/include/netlink/route/neightbl.h
new file mode 100644 (file)
index 0000000..954cabc
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * netlink/route/neightbl.h    Neighbour Tables
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_NEIGHTBL_H_
+#define NETLINK_NEIGHTBL_H_
+
+#include <netlink/netlink.h>
+#include <netlink/cache.h>
+#include <netlink/addr.h>
+
+struct rtnl_neightbl;
+
+extern struct rtnl_neightbl *rtnl_neightbl_alloc(void);
+extern void rtnl_neightbl_put(struct rtnl_neightbl *);
+extern void rtnl_neightbl_free(struct rtnl_neightbl *);
+extern struct nl_cache *rtnl_neightbl_alloc_cache(struct nl_handle *);
+extern struct rtnl_neightbl *rtnl_neightbl_get(struct nl_cache *,
+                                              const char *, int);
+extern void rtnl_neightbl_dump(struct rtnl_neightbl *, FILE *,
+                              struct nl_dump_params *);
+
+extern struct nl_msg *rtnl_neightbl_build_change_request(struct rtnl_neightbl *,
+                                                struct rtnl_neightbl *);
+extern int rtnl_neightbl_change(struct nl_handle *, struct rtnl_neightbl *,
+                               struct rtnl_neightbl *);
+
+extern void rtnl_neightbl_set_family(struct rtnl_neightbl *, int);
+extern void rtnl_neightbl_set_gc_tresh1(struct rtnl_neightbl *, int);
+extern void rtnl_neightbl_set_gc_tresh2(struct rtnl_neightbl *, int);
+extern void rtnl_neightbl_set_gc_tresh3(struct rtnl_neightbl *, int);
+extern void rtnl_neightbl_set_name(struct rtnl_neightbl *, const char *);
+extern void rtnl_neightbl_set_dev(struct rtnl_neightbl *, int);
+extern void rtnl_neightbl_set_queue_len(struct rtnl_neightbl *, int);
+extern void rtnl_neightbl_set_proxy_queue_len(struct rtnl_neightbl *, int);
+extern void rtnl_neightbl_set_app_probes(struct rtnl_neightbl *, int);
+extern void rtnl_neightbl_set_ucast_probes(struct rtnl_neightbl *, int);
+extern void rtnl_neightbl_set_mcast_probes(struct rtnl_neightbl *, int);
+extern void rtnl_neightbl_set_base_reachable_time(struct rtnl_neightbl *,
+                                                 uint64_t);
+extern void rtnl_neightbl_set_retrans_time(struct rtnl_neightbl *, uint64_t);
+extern void rtnl_neightbl_set_gc_stale_time(struct rtnl_neightbl *, uint64_t);
+extern void rtnl_neightbl_set_delay_probe_time(struct rtnl_neightbl *,
+                                              uint64_t);
+extern void rtnl_neightbl_set_anycast_delay(struct rtnl_neightbl *, uint64_t);
+extern void rtnl_neightbl_set_proxy_delay(struct rtnl_neightbl *, uint64_t);
+extern void rtnl_neightbl_set_locktime(struct rtnl_neightbl *, uint64_t);
+
+#endif
diff --git a/include/netlink/route/nexthop.h b/include/netlink/route/nexthop.h
new file mode 100644 (file)
index 0000000..a9e70f9
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * netlink/route/nexthop.h     Routing Nexthop
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_ROUTE_NEXTHOP_H_
+#define NETLINK_ROUTE_NEXTHOP_H_
+
+#include <netlink/netlink.h>
+#include <netlink/addr.h>
+
+struct rtnl_nexthop;
+
+extern struct rtnl_nexthop *   rtnl_route_nh_alloc(void);
+extern void            rtnl_route_nh_free(struct rtnl_nexthop *);
+extern void            rtnl_route_nh_set_weight(struct rtnl_nexthop *, int);
+extern void            rtnl_route_nh_set_ifindex(struct rtnl_nexthop *, int);
+extern void            rtnl_route_nh_set_gateway(struct rtnl_nexthop *,
+                                                 struct nl_addr *);
+extern void            rtnl_route_nh_set_flags(struct rtnl_nexthop *,
+                                               unsigned int);
+extern void            rtnl_route_nh_unset_flags(struct rtnl_nexthop *,
+                                                 unsigned int);
+extern unsigned int    rtnl_route_nh_get_flags(struct rtnl_nexthop *);
+
+#endif
diff --git a/include/netlink/route/qdisc-modules.h b/include/netlink/route/qdisc-modules.h
new file mode 100644 (file)
index 0000000..235f06b
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * netlink/route/qdisc-modules.h       Qdisc Module API
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_QDISC_MODULES_H_
+#define NETLINK_QDISC_MODULES_H_
+
+#include <netlink/netlink.h>
+
+/**
+ * Qdisc Operations
+ * @ingroup qdisc
+ */
+struct rtnl_qdisc_ops
+{
+       /**
+        * Kind/Name of Qdisc
+        */
+       char qo_kind[32];
+
+       /**
+        * Dump callbacks
+        */
+       int  (*qo_dump[NL_DUMP_MAX+1])(struct rtnl_qdisc *,
+                                      struct nl_dump_params *, int);
+
+       /**
+        * Must return the contents supposed to be in TCA_OPTIONS
+        */
+       struct nl_msg *(*qo_get_opts)(struct rtnl_qdisc *);
+
+       /**
+        * TCA_OPTIONS message parser
+        */
+       int  (*qo_msg_parser)(struct rtnl_qdisc *);
+
+       /**
+        * Called before a Qdisc object gets destroyed
+        */
+       void (*qo_free_data)(struct rtnl_qdisc *);
+
+       /**
+        * INTERNAL (Do not use)
+        */
+       struct rtnl_qdisc_ops *qo_next;
+};
+
+extern int rtnl_qdisc_register(struct rtnl_qdisc_ops *);
+extern int rtnl_qdisc_unregister(struct rtnl_qdisc_ops *);
+
+#endif
diff --git a/include/netlink/route/qdisc.h b/include/netlink/route/qdisc.h
new file mode 100644 (file)
index 0000000..81336f3
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * netlink/route/qdisc.h         Queueing Disciplines
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_QDISC_H_
+#define NETLINK_QDISC_H_
+
+#include <netlink/netlink.h>
+#include <netlink/cache.h>
+#include <netlink/route/tc.h>
+
+struct rtnl_qdisc;
+
+/* General */
+extern struct rtnl_qdisc *     rtnl_qdisc_alloc(void);
+extern void                    rtnl_qdisc_put(struct rtnl_qdisc *);
+extern void                    rtnl_qdisc_free(struct rtnl_qdisc *);
+
+/* Cache Management */
+extern struct nl_cache *       rtnl_qdisc_alloc_cache(struct nl_handle *);
+extern struct rtnl_qdisc *     rtnl_qdisc_get(struct nl_cache *,
+                                              int, uint32_t);
+extern struct rtnl_qdisc *     rtnl_qdisc_get_by_parent(struct nl_cache *,
+                                                        int, uint32_t);
+
+/* qdisc addition */
+extern struct nl_msg * rtnl_qdisc_build_add_request(struct rtnl_qdisc *, int);
+extern int             rtnl_qdisc_add(struct nl_handle *, struct rtnl_qdisc *,
+                                      int);
+
+/* qdisc modification */
+extern struct nl_msg * rtnl_qdisc_build_change_request(struct rtnl_qdisc *,
+                                                       struct rtnl_qdisc *);
+extern int             rtnl_qdisc_change(struct nl_handle *,
+                                         struct rtnl_qdisc *,
+                                         struct rtnl_qdisc *);
+
+/* qdisc deletion */
+extern struct nl_msg * rtnl_qdisc_build_delete_request(struct rtnl_qdisc *);
+extern int             rtnl_qdisc_delete(struct nl_handle *,
+                                         struct rtnl_qdisc *);
+
+/* attribute modifications */
+extern void            rtnl_qdisc_set_ifindex(struct rtnl_qdisc *, int);
+extern int             rtnl_qdisc_get_ifindex(struct rtnl_qdisc *);
+extern void            rtnl_qdisc_set_handle(struct rtnl_qdisc *, uint32_t);
+extern uint32_t                rtnl_qdisc_get_handle(struct rtnl_qdisc *);
+extern void            rtnl_qdisc_set_parent(struct rtnl_qdisc *, uint32_t);
+extern uint32_t                rtnl_qdisc_get_parent(struct rtnl_qdisc *);
+extern void            rtnl_qdisc_set_kind(struct rtnl_qdisc *, const char *);
+extern char *          rtnl_qdisc_get_kind(struct rtnl_qdisc *);
+extern uint64_t                rtnl_qdisc_get_stat(struct rtnl_qdisc *,
+                                           enum rtnl_tc_stats_id);
+
+/* iterators */
+extern void            rtnl_qdisc_foreach_child(struct rtnl_qdisc *,
+                                                struct nl_cache *,
+                                                void (*cb)(struct nl_object *,
+                                                           void *),
+                                                void *);
+
+extern void            rtnl_qdisc_foreach_cls(struct rtnl_qdisc *,
+                                              struct nl_cache *,
+                                              void (*cb)(struct nl_object *,
+                                                         void *),
+                                              void *);
+
+/* qdisc specific options */
+extern struct nl_msg * rtnl_qdisc_get_opts(struct rtnl_qdisc *);
+
+#endif
diff --git a/include/netlink/route/route.h b/include/netlink/route/route.h
new file mode 100644 (file)
index 0000000..c8e0085
--- /dev/null
@@ -0,0 +1,95 @@
+/*
+ * netlink/route/route.h       Routes
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_ROUTE_H_
+#define NETLINK_ROUTE_H_
+
+#include <netlink/netlink.h>
+#include <netlink/cache.h>
+#include <netlink/addr.h>
+#include <netlink/data.h>
+#include <netlink/route/nexthop.h>
+
+struct rtnl_route;
+
+/* General */
+extern struct rtnl_route *     rtnl_route_alloc(void);
+extern void                    rtnl_route_free(struct rtnl_route *);
+extern struct nl_cache *       rtnl_route_alloc_cache(struct nl_handle *);
+
+extern void            rtnl_route_get(struct rtnl_route *);
+extern void            rtnl_route_put(struct rtnl_route *);
+
+/* Access Functions */
+extern void            rtnl_route_set_table(struct rtnl_route *, int);
+extern int             rtnl_route_get_table(struct rtnl_route *);
+extern void            rtnl_route_set_scope(struct rtnl_route *, int);
+extern int             rtnl_route_get_scope(struct rtnl_route *);
+extern void            rtnl_route_set_tos(struct rtnl_route *, int);
+extern int             rtnl_route_get_tos(struct rtnl_route *);
+extern void            rtnl_route_set_realms(struct rtnl_route *, realm_t);
+extern realm_t         rtnl_route_get_realms(struct rtnl_route *);
+extern void            rtnl_route_set_protocol(struct rtnl_route *, int);
+extern int             rtnl_route_get_protocol(struct rtnl_route *);
+extern void            rtnl_route_set_prio(struct rtnl_route *, int);
+extern int             rtnl_route_get_prio(struct rtnl_route *);
+extern void            rtnl_route_set_family(struct rtnl_route *, int);
+extern int             rtnl_route_get_family(struct rtnl_route *);
+extern void            rtnl_route_set_type(struct rtnl_route *, int);
+extern int             rtnl_route_get_type(struct rtnl_route *);
+extern void            rtnl_route_set_flags(struct rtnl_route *,
+                                            unsigned int);
+extern void            rtnl_route_unset_flags(struct rtnl_route *,
+                                              unsigned int);
+extern unsigned int    rtnl_route_get_flags(struct rtnl_route *);
+extern int             rtnl_route_set_metric(struct rtnl_route *, int,
+                                             unsigned int);
+extern int             rtnl_route_unset_metric(struct rtnl_route *, int);
+extern unsigned int    rtnl_route_get_metric(struct rtnl_route *, int);
+extern int             rtnl_route_set_dst(struct rtnl_route *,
+                                          struct nl_addr *);
+extern struct nl_addr *        rtnl_route_get_dst(struct rtnl_route *);
+extern int             rtnl_route_set_src(struct rtnl_route *,
+                                          struct nl_addr *);
+extern struct nl_addr *        rtnl_route_get_src(struct rtnl_route *);
+extern int             rtnl_route_set_gateway(struct rtnl_route *,
+                                              struct nl_addr *);
+extern struct nl_addr *        rtnl_route_get_gateway(struct rtnl_route *);
+extern int             rtnl_route_set_pref_src(struct rtnl_route *,
+                                               struct nl_addr *);
+extern struct nl_addr *        rtnl_route_get_pref_src(struct rtnl_route *);
+extern void            rtnl_route_set_oif(struct rtnl_route *, int);
+extern int             rtnl_route_get_oif(struct rtnl_route *);
+extern void            rtnl_route_set_iif(struct rtnl_route *, const char *);
+extern char *          rtnl_route_get_iif(struct rtnl_route *);
+extern void            rtnl_route_set_dst_len(struct rtnl_route *, int);
+extern int             rtnl_route_get_dst_len(struct rtnl_route *);
+extern void            rtnl_route_set_src_len(struct rtnl_route *, int);
+extern int             rtnl_route_get_src_len(struct rtnl_route *);
+
+extern void            rtnl_route_add_nexthop(struct rtnl_route *,
+                                              struct rtnl_nexthop *);
+extern void            rtnl_route_remove_nexthop(struct rtnl_nexthop *);
+extern struct nl_list_head *   rtnl_route_get_nexthops(struct rtnl_route *);
+
+extern char *          rtnl_route_table2str(int, char *, size_t);
+extern int             rtnl_route_str2table(const char *);
+
+extern char *          rtnl_route_proto2str(int, char *, size_t);
+extern int             rtnl_route_str2proto(const char *);
+
+extern char *          rtnl_route_metric2str(int, char *, size_t);
+extern int             rtnl_route_str2metric(const char *);
+
+extern char *          rtnl_route_nh_flags2str(int, char *, size_t);
+extern int             rtnl_route_nh_str2flags(const char *);
+
+#endif
diff --git a/include/netlink/route/rtnl.h b/include/netlink/route/rtnl.h
new file mode 100644 (file)
index 0000000..427f5bb
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * netlink/route/rtnl.h                Routing Netlink
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_RTNL_H_
+#define NETLINK_RTNL_H_
+
+#include <netlink/netlink.h>
+
+/**
+ * @name Realms
+ * @{
+ */
+
+typedef uint32_t       realm_t;
+
+/**
+ * Mask specying the size of each realm part
+ * @ingroup rtnl
+ */
+#define RTNL_REALM_MASK (0xFFFF)
+
+/**
+ * Extract FROM realm from a realms field
+ */
+#define RTNL_REALM_FROM(realm) ((realm) >> 16)
+
+/**
+ * Extract TO realm from a realms field
+ */
+#define RTNL_REALM_TO(realm) ((realm) & RTNL_REALM_MASK)
+
+/**
+ * Build a realms field
+ */
+#define RTNL_MAKE_REALM(from, to) \
+       ((RTNL_REALM_TO(from) << 16) & RTNL_REALM_TO(to))
+
+/** @} */
+
+
+/* General */
+extern int             nl_rtgen_request(struct nl_handle *, int, int, int);
+
+/* Routing Type Translations */
+extern char *          nl_rtntype2str(int, char *, size_t);
+extern int             nl_str2rtntype(const char *);
+
+/* Scope Translations */
+extern char *          rtnl_scope2str(int, char *, size_t);
+extern int             rtnl_str2scope(const char *);
+
+/* Realms Translations */
+extern char *          rtnl_realms2str(uint32_t, char *, size_t);
+
+#endif
diff --git a/include/netlink/route/rule.h b/include/netlink/route/rule.h
new file mode 100644 (file)
index 0000000..d33fabe
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * netlink/route/rule.h                Rules
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_RULE_H_
+#define NETLINK_RULE_H_
+
+#include <netlink/netlink.h>
+#include <netlink/cache.h>
+#include <netlink/addr.h>
+#include <netlink/route/route.h>
+
+struct rtnl_rule;
+
+/* General */
+extern struct rtnl_rule *      rtnl_rule_alloc(void);
+extern void                    rtnl_rule_put(struct rtnl_rule *);
+extern void                    rtnl_rule_free(struct rtnl_rule *);
+
+extern struct nl_cache * rtnl_rule_alloc_cache(struct nl_handle *);
+extern struct nl_cache * rtnl_rule_alloc_cache_by_family(struct nl_handle *,
+                                                        int);
+extern void rtnl_rule_dump(struct rtnl_rule *, FILE *, struct nl_dump_params *);
+
+extern struct nl_msg * rtnl_rule_build_add_request(struct rtnl_rule *, int);
+extern int rtnl_rule_add(struct nl_handle *, struct rtnl_rule *, int);
+extern struct nl_msg * rtnl_rule_build_delete_request(struct rtnl_rule *, int);
+extern int rtnl_rule_delete(struct nl_handle *, struct rtnl_rule *, int);
+
+
+/* attribute modification */
+extern void            rtnl_rule_set_family(struct rtnl_rule *, int);
+extern int             rtnl_rule_get_family(struct rtnl_rule *);
+extern void            rtnl_rule_set_prio(struct rtnl_rule *, int);
+extern int             rtnl_rule_get_prio(struct rtnl_rule *);
+extern void            rtnl_rule_set_fwmark(struct rtnl_rule *, uint64_t);
+extern uint64_t                rtnl_rule_get_fwmark(struct rtnl_rule *);
+extern void            rtnl_rule_set_table(struct rtnl_rule *, int);
+extern int             rtnl_rule_get_table(struct rtnl_rule *);
+extern void            rtnl_rule_set_dsfield(struct rtnl_rule *, int);
+extern int             rtnl_rule_get_dsfield(struct rtnl_rule *);
+extern int             rtnl_rule_set_src(struct rtnl_rule *, struct nl_addr *);
+extern struct nl_addr *        rtnl_rule_get_src(struct rtnl_rule *);
+extern int             rtnl_rule_set_dst(struct rtnl_rule *, struct nl_addr *);
+extern struct nl_addr *        rtnl_rule_get_dst(struct rtnl_rule *);
+extern void            rtnl_rule_set_src_len(struct rtnl_rule *, int);
+extern int             rtnl_rule_get_src_len(struct rtnl_rule *);
+extern void            rtnl_rule_set_dst_len(struct rtnl_rule *, int);
+extern int             rtnl_rule_get_dst_len(struct rtnl_rule *);
+
+extern void            rtnl_rule_set_action(struct rtnl_rule *, int);
+extern int             rtnl_rule_get_action(struct rtnl_rule *);
+
+extern int             rtnl_rule_set_iif(struct rtnl_rule *, const char *);
+extern char *          rtnl_rule_get_iif(struct rtnl_rule *);
+
+extern void            rtnl_rule_set_classid(struct rtnl_rule *, uint32_t);
+extern uint32_t                rtnl_rule_get_classid(struct rtnl_rule *);
+
+extern void            rtnl_rule_set_realms(struct rtnl_rule *, realm_t);
+extern realm_t         rtnl_rule_get_realms(struct rtnl_rule *);
+
+#endif
diff --git a/include/netlink/route/sch/cbq.h b/include/netlink/route/sch/cbq.h
new file mode 100644 (file)
index 0000000..764b0f3
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * netlink/route/sch/cbq.h     Class Based Queueing
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_CBQ_H_
+#define NETLINK_CBQ_H_
+
+#include <netlink/netlink.h>
+#include <netlink/cache.h>
+#include <netlink/route/qdisc.h>
+
+extern char * nl_ovl_strategy2str(int, char *, size_t);
+extern int    nl_str2ovl_strategy(const char *);
+
+#endif
diff --git a/include/netlink/route/sch/dsmark.h b/include/netlink/route/sch/dsmark.h
new file mode 100644 (file)
index 0000000..2fb130a
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * netlink/route/sch/dsmark.h  DSMARK
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_DSMARK_H_
+#define NETLINK_DSMARK_H_
+
+#include <netlink/netlink.h>
+
+extern int     rtnl_class_dsmark_set_bmask(struct rtnl_class *, uint8_t);
+extern int     rtnl_class_dsmark_get_bmask(struct rtnl_class *);
+
+extern int     rtnl_class_dsmark_set_value(struct rtnl_class *, uint8_t);
+extern int     rtnl_class_dsmark_get_value(struct rtnl_class *);
+
+extern int     rtnl_qdisc_dsmark_set_indices(struct rtnl_qdisc *, uint16_t);
+extern int     rtnl_qdisc_dsmark_get_indices(struct rtnl_qdisc *);
+
+extern int     rtnl_qdisc_dsmark_set_default_index(struct rtnl_qdisc *,
+                                                   uint16_t);
+extern int     rtnl_qdisc_dsmark_get_default_index(struct rtnl_qdisc *);
+
+extern int     rtnl_qdisc_dsmark_set_set_tc_index(struct rtnl_qdisc *, int);
+extern int     rtnl_qdisc_dsmark_get_set_tc_index(struct rtnl_qdisc *);
+
+#endif
diff --git a/include/netlink/route/sch/fifo.h b/include/netlink/route/sch/fifo.h
new file mode 100644 (file)
index 0000000..b5ba8eb
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+ * netlink/route/sch/fifo.c    FIFO Qdisc
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_FIFO_H_
+#define NETLINK_FIFO_H_
+
+#include <netlink/netlink.h>
+
+extern int     rtnl_qdisc_fifo_set_limit(struct rtnl_qdisc *, int);
+extern int     rtnl_qdisc_fifo_get_limit(struct rtnl_qdisc *);
+
+#endif
diff --git a/include/netlink/route/sch/htb.h b/include/netlink/route/sch/htb.h
new file mode 100644 (file)
index 0000000..c681049
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * netlink/route/sch/htb.h     HTB Qdisc
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ * Copyright (c) 2005 Petr Gotthard <petr.gotthard@siemens.com>
+ * Copyright (c) 2005 Siemens AG Oesterreich
+ */
+
+#ifndef NETLINK_HTB_H_
+#define NETLINK_HTB_H_
+
+#include <netlink/netlink.h>
+#include <netlink/route/tc.h>
+
+extern void rtnl_htb_set_rate2quantum(struct rtnl_qdisc *, uint32_t);
+extern void rtnl_htb_set_defcls(struct rtnl_qdisc *, uint32_t);
+
+extern void rtnl_htb_set_prio(struct rtnl_class *, uint32_t);
+extern void rtnl_htb_set_mtu(struct rtnl_class *, uint32_t);
+extern void rtnl_htb_set_rate(struct rtnl_class *, uint32_t);
+extern void rtnl_htb_set_ceil(struct rtnl_class *, uint32_t);
+
+#endif
diff --git a/include/netlink/route/sch/netem.h b/include/netlink/route/sch/netem.h
new file mode 100644 (file)
index 0000000..dd3597f
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * netlink/route/sch/netem.h           Network Emulator Qdisc
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_NETEM_H_
+#define NETLINK_NETEM_H_
+
+#include <netlink/netlink.h>
+
+extern int rtnl_netem_set_limit(struct rtnl_qdisc *, int);
+extern int rtnl_netem_get_limit(struct rtnl_qdisc *);
+
+/* Packet Re-ordering */
+extern int rtnl_netem_set_gap(struct rtnl_qdisc *, int);
+extern int rtnl_netem_get_gap(struct rtnl_qdisc *);
+
+extern int rtnl_netem_set_reorder_probability(struct rtnl_qdisc *, int);
+extern int rtnl_netem_get_reorder_probability(struct rtnl_qdisc *);
+
+extern int rtnl_netem_set_reorder_correlation(struct rtnl_qdisc *, int);
+extern int rtnl_netem_get_reorder_correlation(struct rtnl_qdisc *);
+
+/* Packet Loss */
+extern int rtnl_netem_set_loss(struct rtnl_qdisc *, int);
+extern int rtnl_netem_get_loss(struct rtnl_qdisc *);
+
+extern int rtnl_netem_set_loss_correlation(struct rtnl_qdisc *, int);
+extern int rtnl_netem_get_loss_correlation(struct rtnl_qdisc *);
+
+/* Packet Duplication */
+extern int rtnl_netem_set_duplicate(struct rtnl_qdisc *, int);
+extern int rtnl_netem_get_duplicate(struct rtnl_qdisc *);
+
+extern int rtnl_netem_set_duplicate_correction(struct rtnl_qdisc *, int);
+extern int rtnl_netem_get_duplicate_correlation(struct rtnl_qdisc *);
+
+/* Packet Delay */
+extern int rtnl_netem_set_delay(struct rtnl_qdisc *, int);
+extern int rtnl_netem_get_delay(struct rtnl_qdisc *);
+
+extern int rtnl_netem_set_jitter(struct rtnl_qdisc *, int);
+extern int rtnl_netem_get_jitter(struct rtnl_qdisc *);
+
+extern int rtnl_netem_set_delay_correlation(struct rtnl_qdisc *, int);
+extern int rtnl_netem_get_delay_correlation(struct rtnl_qdisc *);
+
+#endif
diff --git a/include/netlink/route/sch/prio.h b/include/netlink/route/sch/prio.h
new file mode 100644 (file)
index 0000000..436922e
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * netlink/route/sch/prio.c    PRIO Qdisc
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_PRIO_H_
+#define NETLINK_PRIO_H_
+
+#include <netlink/netlink.h>
+
+/**
+ * @name Default Values
+ * @{
+ */
+
+/**
+ * Default number of bands.
+ * @ingroup prio
+ */
+#define QDISC_PRIO_DEFAULT_BANDS 3
+
+/**
+ * Default priority mapping.
+ * @ingroup prio
+ */
+#define QDISC_PRIO_DEFAULT_PRIOMAP \
+               { 1, 2, 2, 2, 1, 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 }
+
+/** @} */
+
+extern int  rtnl_qdisc_prio_set_bands(struct rtnl_qdisc *, int);
+extern int  rtnl_qdisc_prio_get_bands(struct rtnl_qdisc *);
+extern int  rtnl_qdisc_prio_set_priomap(struct rtnl_qdisc *, uint8_t[], int);
+extern uint8_t *rtnl_qdisc_prio_get_priomap(struct rtnl_qdisc *);
+
+extern char *  rtnl_prio2str(int, char *, size_t);
+extern int     rtnl_str2prio(const char *);
+
+#endif
diff --git a/include/netlink/route/sch/red.h b/include/netlink/route/sch/red.h
new file mode 100644 (file)
index 0000000..a4e8642
--- /dev/null
@@ -0,0 +1,17 @@
+/*
+ * netlink/route/sch/red.h     RED Qdisc
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_RED_H_
+#define NETLINK_RED_H_
+
+#include <netlink/netlink.h>
+
+#endif
diff --git a/include/netlink/route/sch/sfq.h b/include/netlink/route/sch/sfq.h
new file mode 100644 (file)
index 0000000..2482b08
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * netlink/route/sch/sfq.c     SFQ Qdisc
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_SFQ_H_
+#define NETLINK_SFQ_H_
+
+#include <netlink/netlink.h>
+
+extern int     rtnl_sfq_set_quantum(struct rtnl_qdisc *, int);
+extern int     rtnl_sfq_get_quantum(struct rtnl_qdisc *);
+
+extern int     rtnl_sfq_set_limit(struct rtnl_qdisc *, int);
+extern int     rtnl_sfq_get_limit(struct rtnl_qdisc *);
+
+extern int     rtnl_sfq_set_perturb(struct rtnl_qdisc *, int);
+extern int     rtnl_sfq_get_perturb(struct rtnl_qdisc *);
+
+extern int     rtnl_sfq_get_divisor(struct rtnl_qdisc *);
+
+#endif
diff --git a/include/netlink/route/sch/tbf.h b/include/netlink/route/sch/tbf.h
new file mode 100644 (file)
index 0000000..96f1d82
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * netlink/route/sch/tbf.h     TBF Qdisc
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_TBF_H_
+#define NETLINK_TBF_H_
+
+#include <netlink/netlink.h>
+#include <netlink/route/tc.h>
+
+extern int rtnl_qdisc_tbf_set_limit(struct rtnl_qdisc *, int);
+extern int rtnl_qdisc_tbf_set_limit_by_latency(struct rtnl_qdisc *, int);
+extern int rtnl_qdisc_tbf_get_limit(struct rtnl_qdisc *);
+
+extern int rtnl_qdisc_tbf_set_mpu(struct rtnl_qdisc *, int);
+extern int rtnl_qdisc_tbf_get_mpu(struct rtnl_qdisc *);
+
+extern int rtnl_qdisc_tbf_set_rate(struct rtnl_qdisc *, int, int, int);
+extern int rtnl_qdisc_tbf_get_rate(struct rtnl_qdisc *);
+extern int rtnl_qdisc_tbf_get_rate_bucket(struct rtnl_qdisc *);
+extern int rtnl_qdisc_tbf_get_rate_cell(struct rtnl_qdisc *);
+
+extern int rtnl_qdisc_tbf_set_peakrate(struct rtnl_qdisc *, int, int, int);
+extern int rtnl_qdisc_tbf_get_peakrate(struct rtnl_qdisc *);
+extern int rtnl_qdisc_tbf_get_peakrate_bucket(struct rtnl_qdisc *);
+extern int rtnl_qdisc_tbf_get_peakrate_cell(struct rtnl_qdisc *);
+
+#endif
diff --git a/include/netlink/route/tc.h b/include/netlink/route/tc.h
new file mode 100644 (file)
index 0000000..3834c55
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * netlink/route/tc.h          Traffic Control
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_TC_H_
+#define NETLINK_TC_H_
+
+#include <netlink/netlink.h>
+#include <netlink/cache.h>
+#include <netlink/data.h>
+
+/**
+ * TC statistics identifiers
+ * @ingroup tc
+ */
+enum rtnl_tc_stats_id {
+       RTNL_TC_PACKETS,        /**< Packets seen */
+       RTNL_TC_BYTES,          /**< Bytes seen */
+       RTNL_TC_RATE_BPS,       /**< Current bits/s (rate estimator) */
+       RTNL_TC_RATE_PPS,       /**< Current packet/s (rate estimator) */
+       RTNL_TC_QLEN,           /**< Queue length */
+       RTNL_TC_BACKLOG,        /**< Backlog length */
+       RTNL_TC_DROPS,          /**< Packets dropped */
+       RTNL_TC_REQUEUES,       /**< Number of requeues */
+       RTNL_TC_OVERLIMITS,     /**< Number of overlimits */
+       __RTNL_TC_STATS_MAX,
+};
+
+#define RTNL_TC_STATS_MAX (__RTNL_TC_STATS_MAX - 1)
+
+extern int rtnl_tc_calc_txtime(int, int);
+extern int rtnl_tc_calc_bufsize(int, int);
+extern int rtnl_tc_calc_cell_log(int);
+
+/**
+ * Number of entries in a transmission time lookup table
+ * @ingroup tc
+ */
+#define RTNL_TC_RTABLE_SIZE    256
+
+extern int rtnl_tc_build_rate_table(uint32_t *, uint8_t, uint8_t, int, int);
+
+
+/* TC Handle Translations */
+extern char *          rtnl_tc_handle2str(uint32_t, char *, size_t);
+extern int             rtnl_tc_str2handle(const char *, uint32_t *);
+
+#endif
diff --git a/include/netlink/rtnetlink-kernel.h b/include/netlink/rtnetlink-kernel.h
new file mode 100644 (file)
index 0000000..2149ee5
--- /dev/null
@@ -0,0 +1,853 @@
+#ifndef __LINUX_RTNETLINK_H
+#define __LINUX_RTNETLINK_H
+
+/**
+ * @anchor rtnlmsgtypes
+ * @name Specialized Message Types for Routing Netlink
+ * @ingroup rtnl
+ * @{
+ */
+
+enum {
+       RTM_BASE        = 16,
+#define RTM_BASE       RTM_BASE
+
+       RTM_NEWLINK     = 16,
+#define RTM_NEWLINK    RTM_NEWLINK
+       RTM_DELLINK,
+#define RTM_DELLINK    RTM_DELLINK
+       RTM_GETLINK,
+#define RTM_GETLINK    RTM_GETLINK
+       RTM_SETLINK,
+#define RTM_SETLINK    RTM_SETLINK
+
+       RTM_NEWADDR     = 20,
+#define RTM_NEWADDR    RTM_NEWADDR
+       RTM_DELADDR,
+#define RTM_DELADDR    RTM_DELADDR
+       RTM_GETADDR,
+#define RTM_GETADDR    RTM_GETADDR
+
+       RTM_NEWROUTE    = 24,
+#define RTM_NEWROUTE   RTM_NEWROUTE
+       RTM_DELROUTE,
+#define RTM_DELROUTE   RTM_DELROUTE
+       RTM_GETROUTE,
+#define RTM_GETROUTE   RTM_GETROUTE
+
+       RTM_NEWNEIGH    = 28,
+#define RTM_NEWNEIGH   RTM_NEWNEIGH
+       RTM_DELNEIGH,
+#define RTM_DELNEIGH   RTM_DELNEIGH
+       RTM_GETNEIGH,
+#define RTM_GETNEIGH   RTM_GETNEIGH
+
+       RTM_NEWRULE     = 32,
+#define RTM_NEWRULE    RTM_NEWRULE
+       RTM_DELRULE,
+#define RTM_DELRULE    RTM_DELRULE
+       RTM_GETRULE,
+#define RTM_GETRULE    RTM_GETRULE
+
+       RTM_NEWQDISC    = 36,
+#define RTM_NEWQDISC   RTM_NEWQDISC
+       RTM_DELQDISC,
+#define RTM_DELQDISC   RTM_DELQDISC
+       RTM_GETQDISC,
+#define RTM_GETQDISC   RTM_GETQDISC
+
+       RTM_NEWTCLASS   = 40,
+#define RTM_NEWTCLASS  RTM_NEWTCLASS
+       RTM_DELTCLASS,
+#define RTM_DELTCLASS  RTM_DELTCLASS
+       RTM_GETTCLASS,
+#define RTM_GETTCLASS  RTM_GETTCLASS
+
+       RTM_NEWTFILTER  = 44,
+#define RTM_NEWTFILTER RTM_NEWTFILTER
+       RTM_DELTFILTER,
+#define RTM_DELTFILTER RTM_DELTFILTER
+       RTM_GETTFILTER,
+#define RTM_GETTFILTER RTM_GETTFILTER
+
+       RTM_NEWACTION   = 48,
+#define RTM_NEWACTION   RTM_NEWACTION
+       RTM_DELACTION,
+#define RTM_DELACTION   RTM_DELACTION
+       RTM_GETACTION,
+#define RTM_GETACTION   RTM_GETACTION
+
+       RTM_NEWPREFIX   = 52,
+#define RTM_NEWPREFIX  RTM_NEWPREFIX
+       RTM_GETPREFIX   = 54,
+#define RTM_GETPREFIX  RTM_GETPREFIX
+
+       RTM_GETMULTICAST = 58,
+#define RTM_GETMULTICAST RTM_GETMULTICAST
+
+       RTM_GETANYCAST  = 62,
+#define RTM_GETANYCAST RTM_GETANYCAST
+
+       RTM_NEWNEIGHTBL = 64,
+#define RTM_NEWNEIGHTBL        RTM_NEWNEIGHTBL
+       RTM_GETNEIGHTBL = 66,
+#define RTM_GETNEIGHTBL        RTM_GETNEIGHTBL
+       RTM_SETNEIGHTBL,
+#define RTM_SETNEIGHTBL        RTM_SETNEIGHTBL
+
+       __RTM_MAX,
+};
+
+#define RTM_MAX                (((__RTM_MAX + 3) & ~3) - 1)
+
+/** @} */
+
+#define RTM_NR_MSGTYPES        (RTM_MAX + 1 - RTM_BASE)
+#define RTM_NR_FAMILIES        (RTM_NR_MSGTYPES >> 2)
+#define RTM_FAM(cmd)   (((cmd) - RTM_BASE) >> 2)
+
+
+struct rtmsg
+{
+       unsigned char           rtm_family;
+       unsigned char           rtm_dst_len;
+       unsigned char           rtm_src_len;
+       unsigned char           rtm_tos;
+
+       unsigned char           rtm_table;      /* Routing table id */
+       unsigned char           rtm_protocol;   /* Routing protocol; see below  */
+       unsigned char           rtm_scope;      /* See below */ 
+       unsigned char           rtm_type;       /* See below    */
+
+       unsigned                rtm_flags;
+};
+
+/* rtm_type */
+
+enum
+{
+       RTN_UNSPEC,
+       RTN_UNICAST,            /* Gateway or direct route      */
+       RTN_LOCAL,              /* Accept locally               */
+       RTN_BROADCAST,          /* Accept locally as broadcast,
+                                  send as broadcast */
+       RTN_ANYCAST,            /* Accept locally as broadcast,
+                                  but send as unicast */
+       RTN_MULTICAST,          /* Multicast route              */
+       RTN_BLACKHOLE,          /* Drop                         */
+       RTN_UNREACHABLE,        /* Destination is unreachable   */
+       RTN_PROHIBIT,           /* Administratively prohibited  */
+       RTN_THROW,              /* Not in this table            */
+       RTN_NAT,                /* Translate this address       */
+       RTN_XRESOLVE,           /* Use external resolver        */
+       __RTN_MAX
+};
+
+#define RTN_MAX (__RTN_MAX - 1)
+
+
+/* rtm_protocol */
+
+#define RTPROT_UNSPEC  0
+#define RTPROT_REDIRECT        1       /* Route installed by ICMP redirects;
+                                  not used by current IPv4 */
+#define RTPROT_KERNEL  2       /* Route installed by kernel            */
+#define RTPROT_BOOT    3       /* Route installed during boot          */
+#define RTPROT_STATIC  4       /* Route installed by administrator     */
+
+/* Values of protocol >= RTPROT_STATIC are not interpreted by kernel;
+   they are just passed from user and back as is.
+   It will be used by hypothetical multiple routing daemons.
+   Note that protocol values should be standardized in order to
+   avoid conflicts.
+ */
+
+#define RTPROT_GATED   8       /* Apparently, GateD */
+#define RTPROT_RA      9       /* RDISC/ND router advertisements */
+#define RTPROT_MRT     10      /* Merit MRT */
+#define RTPROT_ZEBRA   11      /* Zebra */
+#define RTPROT_BIRD    12      /* BIRD */
+#define RTPROT_DNROUTED        13      /* DECnet routing daemon */
+#define RTPROT_XORP    14      /* XORP */
+
+/* rtm_scope
+
+   Really it is not scope, but sort of distance to the destination.
+   NOWHERE are reserved for not existing destinations, HOST is our
+   local addresses, LINK are destinations, located on directly attached
+   link and UNIVERSE is everywhere in the Universe.
+
+   Intermediate values are also possible f.e. interior routes
+   could be assigned a value between UNIVERSE and LINK.
+*/
+
+enum rt_scope_t
+{
+       RT_SCOPE_UNIVERSE=0,
+/* User defined values  */
+       RT_SCOPE_SITE=200,
+       RT_SCOPE_LINK=253,
+       RT_SCOPE_HOST=254,
+       RT_SCOPE_NOWHERE=255
+};
+
+/* rtm_flags */
+
+#define RTM_F_NOTIFY           0x100   /* Notify user of route change  */
+#define RTM_F_CLONED           0x200   /* This route is cloned         */
+#define RTM_F_EQUALIZE         0x400   /* Multipath equalizer: NI      */
+#define RTM_F_PREFIX           0x800   /* Prefix addresses             */
+
+/* Reserved table identifiers */
+
+enum rt_class_t
+{
+       RT_TABLE_UNSPEC=0,
+/* User defined values */
+       RT_TABLE_DEFAULT=253,
+       RT_TABLE_MAIN=254,
+       RT_TABLE_LOCAL=255,
+       __RT_TABLE_MAX
+};
+#define RT_TABLE_MAX (__RT_TABLE_MAX - 1)
+
+
+
+/* Routing message attributes */
+
+enum rtattr_type_t
+{
+       RTA_UNSPEC,
+       RTA_DST,
+       RTA_SRC,
+       RTA_IIF,
+       RTA_OIF,
+       RTA_GATEWAY,
+       RTA_PRIORITY,
+       RTA_PREFSRC,
+       RTA_METRICS,
+       RTA_MULTIPATH,
+       RTA_PROTOINFO,
+       RTA_FLOW,
+       RTA_CACHEINFO,
+       RTA_SESSION,
+       RTA_MP_ALGO,
+       __RTA_MAX
+};
+
+#define RTA_MAX (__RTA_MAX - 1)
+
+#define RTM_RTA(r)  ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct rtmsg))))
+#define RTM_PAYLOAD(n) NLMSG_PAYLOAD(n,sizeof(struct rtmsg))
+
+/* RTM_MULTIPATH --- array of struct rtnexthop.
+ *
+ * "struct rtnexthop" describes all necessary nexthop information,
+ * i.e. parameters of path to a destination via this nexthop.
+ *
+ * At the moment it is impossible to set different prefsrc, mtu, window
+ * and rtt for different paths from multipath.
+ */
+
+struct rtnexthop
+{
+       unsigned short          rtnh_len;
+       unsigned char           rtnh_flags;
+       unsigned char           rtnh_hops;
+       int                     rtnh_ifindex;
+};
+
+/* rtnh_flags */
+
+#define RTNH_F_DEAD            1       /* Nexthop is dead (used by multipath)  */
+#define RTNH_F_PERVASIVE       2       /* Do recursive gateway lookup  */
+#define RTNH_F_ONLINK          4       /* Gateway is forced on link    */
+
+/* Macros to handle hexthops */
+
+#define RTNH_ALIGNTO   4
+#define RTNH_ALIGN(len) ( ((len)+RTNH_ALIGNTO-1) & ~(RTNH_ALIGNTO-1) )
+#define RTNH_OK(rtnh,len) ((rtnh)->rtnh_len >= sizeof(struct rtnexthop) && \
+                          ((int)(rtnh)->rtnh_len) <= (len))
+#define RTNH_NEXT(rtnh)        ((struct rtnexthop*)(((char*)(rtnh)) + RTNH_ALIGN((rtnh)->rtnh_len)))
+#define RTNH_LENGTH(len) (RTNH_ALIGN(sizeof(struct rtnexthop)) + (len))
+#define RTNH_SPACE(len)        RTNH_ALIGN(RTNH_LENGTH(len))
+#define RTNH_DATA(rtnh)   ((struct rtattr*)(((char*)(rtnh)) + RTNH_LENGTH(0)))
+
+/* RTM_CACHEINFO */
+
+struct rta_cacheinfo
+{
+       __u32   rta_clntref;
+       __u32   rta_lastuse;
+       __s32   rta_expires;
+       __u32   rta_error;
+       __u32   rta_used;
+
+#define RTNETLINK_HAVE_PEERINFO 1
+       __u32   rta_id;
+       __u32   rta_ts;
+       __u32   rta_tsage;
+};
+
+/* RTM_METRICS --- array of struct rtattr with types of RTAX_* */
+
+enum
+{
+       RTAX_UNSPEC,
+#define RTAX_UNSPEC RTAX_UNSPEC
+       RTAX_LOCK,
+#define RTAX_LOCK RTAX_LOCK
+       RTAX_MTU,
+#define RTAX_MTU RTAX_MTU
+       RTAX_WINDOW,
+#define RTAX_WINDOW RTAX_WINDOW
+       RTAX_RTT,
+#define RTAX_RTT RTAX_RTT
+       RTAX_RTTVAR,
+#define RTAX_RTTVAR RTAX_RTTVAR
+       RTAX_SSTHRESH,
+#define RTAX_SSTHRESH RTAX_SSTHRESH
+       RTAX_CWND,
+#define RTAX_CWND RTAX_CWND
+       RTAX_ADVMSS,
+#define RTAX_ADVMSS RTAX_ADVMSS
+       RTAX_REORDERING,
+#define RTAX_REORDERING RTAX_REORDERING
+       RTAX_HOPLIMIT,
+#define RTAX_HOPLIMIT RTAX_HOPLIMIT
+       RTAX_INITCWND,
+#define RTAX_INITCWND RTAX_INITCWND
+       RTAX_FEATURES,
+#define RTAX_FEATURES RTAX_FEATURES
+       __RTAX_MAX
+};
+
+#define RTAX_MAX (__RTAX_MAX - 1)
+
+/**
+ * @name Features Metric Flags
+ * @{
+ */
+
+#define RTAX_FEATURE_ECN       0x00000001
+#define RTAX_FEATURE_SACK      0x00000002
+#define RTAX_FEATURE_TIMESTAMP 0x00000004
+#define RTAX_FEATURE_ALLFRAG   0x00000008
+
+/** @} */
+
+struct rta_session
+{
+       __u8    proto;
+
+       union {
+               struct {
+                       __u16   sport;
+                       __u16   dport;
+               } ports;
+
+               struct {
+                       __u8    type;
+                       __u8    code;
+                       __u16   ident;
+               } icmpt;
+
+               __u32           spi;
+       } u;
+};
+
+
+/*********************************************************
+ *             Interface address.
+ ****/
+
+struct ifaddrmsg
+{
+       unsigned char   ifa_family;
+       unsigned char   ifa_prefixlen;  /* The prefix length            */
+       unsigned char   ifa_flags;      /* Flags                        */
+       unsigned char   ifa_scope;      /* See above                    */
+       int             ifa_index;      /* Link index                   */
+};
+
+enum
+{
+       IFA_UNSPEC,
+       IFA_ADDRESS,
+       IFA_LOCAL,
+       IFA_LABEL,
+       IFA_BROADCAST,
+       IFA_ANYCAST,
+       IFA_CACHEINFO,
+       IFA_MULTICAST,
+       __IFA_MAX
+};
+
+#define IFA_MAX (__IFA_MAX - 1)
+
+/**
+ * @name Address Flags
+ * @{
+ */
+
+#define IFA_F_SECONDARY                0x01
+#define IFA_F_TEMPORARY                IFA_F_SECONDARY
+
+#define IFA_F_DEPRECATED       0x20
+#define IFA_F_TENTATIVE                0x40
+#define IFA_F_PERMANENT                0x80
+
+struct ifa_cacheinfo
+{
+       __u32   ifa_prefered;
+       __u32   ifa_valid;
+       __u32   cstamp; /* created timestamp, hundredths of seconds */
+       __u32   tstamp; /* updated timestamp, hundredths of seconds */
+};
+
+
+#define IFA_RTA(r)  ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg))))
+#define IFA_PAYLOAD(n) NLMSG_PAYLOAD(n,sizeof(struct ifaddrmsg))
+
+/*
+   Important comment:
+   IFA_ADDRESS is prefix address, rather than local interface address.
+   It makes no difference for normally configured broadcast interfaces,
+   but for point-to-point IFA_ADDRESS is DESTINATION address,
+   local address is supplied in IFA_LOCAL attribute.
+ */
+
+/**************************************************************
+ *             Neighbour discovery.
+ ****/
+
+struct ndmsg
+{
+       unsigned char   ndm_family;
+       unsigned char   ndm_pad1;
+       unsigned short  ndm_pad2;
+       int             ndm_ifindex;    /* Link index                   */
+       __u16           ndm_state;
+       __u8            ndm_flags;
+       __u8            ndm_type;
+};
+
+enum
+{
+       NDA_UNSPEC,
+       NDA_DST,
+       NDA_LLADDR,
+       NDA_CACHEINFO,
+       NDA_PROBES,
+       __NDA_MAX
+};
+
+#define NDA_MAX (__NDA_MAX - 1)
+
+#define NDA_RTA(r)  ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
+#define NDA_PAYLOAD(n) NLMSG_PAYLOAD(n,sizeof(struct ndmsg))
+
+/*
+ *     Neighbor Cache Entry Flags
+ */
+
+#define NTF_PROXY      0x08    /* == ATF_PUBL */
+#define NTF_ROUTER     0x80
+
+/*
+ *     Neighbor Cache Entry States.
+ */
+
+#define NUD_INCOMPLETE 0x01
+#define NUD_REACHABLE  0x02
+#define NUD_STALE      0x04
+#define NUD_DELAY      0x08
+#define NUD_PROBE      0x10
+#define NUD_FAILED     0x20
+
+/* Dummy states */
+#define NUD_NOARP      0x40
+#define NUD_PERMANENT  0x80
+#define NUD_NONE       0x00
+
+
+struct nda_cacheinfo
+{
+       __u32           ndm_confirmed;
+       __u32           ndm_used;
+       __u32           ndm_updated;
+       __u32           ndm_refcnt;
+};
+
+/*****************************************************************
+ *             Neighbour tables specific messages.
+ *
+ * Message Ordering:
+ * Phase 1: foreach neighbour table
+ *          neighbour table base configuration and statistics
+ *          NDTA_NAME, NDTA_CONFIG, NDTA_THRESH[1-3], NDTA_STATS
+ *          NDTA_PARMS
+ *
+ * Phase 2: foreach neighbour table device parameter set
+ *          NDTA_NAME, NDTA_PARMS
+ ****/
+
+struct ndt_stats
+{
+       __u64           ndts_allocs;
+       __u64           ndts_destroys;
+       __u64           ndts_hash_grows;
+       __u64           ndts_res_failed;
+       __u64           ndts_lookups;
+       __u64           ndts_hits;
+       __u64           ndts_rcv_probes_mcast;
+       __u64           ndts_rcv_probes_ucast;
+       __u64           ndts_periodic_gc_runs;
+       __u64           ndts_forced_gc_runs;
+};
+
+enum {
+       NDTPA_UNSPEC,
+       NDTPA_IFINDEX,                  /* u32, read-only */
+       NDTPA_REFCNT,                   /* u32, read-only */
+       NDTPA_REACHABLE_TIME,           /* u64, msecs, read-only */
+       NDTPA_BASE_REACHABLE_TIME,      /* u64, msecs */
+       NDTPA_RETRANS_TIME,             /* u64, msecs */
+       NDTPA_GC_STALETIME,             /* u64, msecs */
+       NDTPA_DELAY_PROBE_TIME,         /* u64, msecs */
+       NDTPA_QUEUE_LEN,                /* u32 */
+       NDTPA_APP_PROBES,               /* u32 */
+       NDTPA_UCAST_PROBES,             /* u32 */
+       NDTPA_MCAST_PROBES,             /* u32 */
+       NDTPA_ANYCAST_DELAY,            /* u64, msecs */
+       NDTPA_PROXY_DELAY,              /* u64, msecs */
+       NDTPA_PROXY_QLEN,               /* u32 */
+       NDTPA_LOCKTIME,                 /* u64, msecs */
+       __NDTPA_MAX
+};
+#define NDTPA_MAX (__NDTPA_MAX - 1)
+
+struct ndtmsg
+{
+       __u8            ndtm_family;
+       __u8            ndtm_pad1;
+       __u16           ndtm_pad2;
+};
+
+struct ndt_config
+{
+       __u16           ndtc_key_len;
+       __u16           ndtc_entry_size;
+       __u32           ndtc_entries;
+       __u32           ndtc_last_flush;        /* delta to now in msecs */
+       __u32           ndtc_last_rand;         /* delta to now in msecs */
+       __u32           ndtc_hash_rnd;
+       __u32           ndtc_hash_mask;
+       __u32           ndtc_hash_chain_gc;
+       __u32           ndtc_proxy_qlen;
+};
+
+enum {
+       NDTA_UNSPEC,
+       NDTA_NAME,                      /* char * */
+       NDTA_THRESH1,                   /* u32 */
+       NDTA_THRESH2,                   /* u32 */
+       NDTA_THRESH3,                   /* u32 */
+       NDTA_CONFIG,                    /* struct ndt_config */
+       NDTA_PARMS,                     /* NDTPA_* */
+       NDTA_STATS,                     /* struct ndt_stats */
+       NDTA_GC_INTERVAL,               /* u64, msecs */
+       __NDTA_MAX
+};
+#define NDTA_MAX (__NDTA_MAX - 1)
+
+#define NDTA_RTA(r) ((struct rtattr*)(((char*)(r)) + \
+                    NLMSG_ALIGN(sizeof(struct ndtmsg))))
+#define NDTA_PAYLOAD(n) NLMSG_PAYLOAD(n,sizeof(struct ndtmsg))
+
+/****
+ *             General form of address family dependent message.
+ ****/
+
+struct rtgenmsg
+{
+       unsigned char           rtgen_family;
+};
+
+/*****************************************************************
+ *             Link layer specific messages.
+ ****/
+
+/* struct ifinfomsg
+ * passes link level specific information, not dependent
+ * on network protocol.
+ */
+
+struct ifinfomsg
+{
+       unsigned char   ifi_family;
+       unsigned char   __ifi_pad;
+       unsigned short  ifi_type;               /* ARPHRD_* */
+       int             ifi_index;              /* Link index   */
+       unsigned        ifi_flags;              /* IFF_* flags  */
+       unsigned        ifi_change;             /* IFF_* change mask */
+};
+
+/********************************************************************
+ *             prefix information 
+ ****/
+
+struct prefixmsg
+{
+       unsigned char   prefix_family;
+       int             prefix_ifindex;
+       unsigned char   prefix_type;
+       unsigned char   prefix_len;
+       unsigned char   prefix_flags;
+};
+
+enum 
+{
+       PREFIX_UNSPEC,
+       PREFIX_ADDRESS,
+       PREFIX_CACHEINFO,
+       __PREFIX_MAX
+};
+
+#define PREFIX_MAX     (__PREFIX_MAX - 1)
+
+struct prefix_cacheinfo
+{
+       __u32   preferred_time;
+       __u32   valid_time;
+};
+
+/* The struct should be in sync with struct net_device_stats */
+struct rtnl_link_stats
+{
+       __u32   rx_packets;             /* total packets received       */
+       __u32   tx_packets;             /* total packets transmitted    */
+       __u32   rx_bytes;               /* total bytes received         */
+       __u32   tx_bytes;               /* total bytes transmitted      */
+       __u32   rx_errors;              /* bad packets received         */
+       __u32   tx_errors;              /* packet transmit problems     */
+       __u32   rx_dropped;             /* no space in linux buffers    */
+       __u32   tx_dropped;             /* no space available in linux  */
+       __u32   multicast;              /* multicast packets received   */
+       __u32   collisions;
+
+       /* detailed rx_errors: */
+       __u32   rx_length_errors;
+       __u32   rx_over_errors;         /* receiver ring buff overflow  */
+       __u32   rx_crc_errors;          /* recved pkt with crc error    */
+       __u32   rx_frame_errors;        /* recv'd frame alignment error */
+       __u32   rx_fifo_errors;         /* recv'r fifo overrun          */
+       __u32   rx_missed_errors;       /* receiver missed packet       */
+
+       /* detailed tx_errors */
+       __u32   tx_aborted_errors;
+       __u32   tx_carrier_errors;
+       __u32   tx_fifo_errors;
+       __u32   tx_heartbeat_errors;
+       __u32   tx_window_errors;
+       
+       /* for cslip etc */
+       __u32   rx_compressed;
+       __u32   tx_compressed;
+};
+
+/* The struct should be in sync with struct ifmap */
+struct rtnl_link_ifmap
+{
+       __u64   mem_start;
+       __u64   mem_end;
+       __u64   base_addr;
+       __u16   irq;
+       __u8    dma;
+       __u8    port;
+};
+
+enum
+{
+       IFLA_UNSPEC,
+       IFLA_ADDRESS,
+       IFLA_BROADCAST,
+       IFLA_IFNAME,
+       IFLA_MTU,
+       IFLA_LINK,
+       IFLA_QDISC,
+       IFLA_STATS,
+       IFLA_COST,
+#define IFLA_COST IFLA_COST
+       IFLA_PRIORITY,
+#define IFLA_PRIORITY IFLA_PRIORITY
+       IFLA_MASTER,
+#define IFLA_MASTER IFLA_MASTER
+       IFLA_WIRELESS,          /* Wireless Extension event - see wireless.h */
+#define IFLA_WIRELESS IFLA_WIRELESS
+       IFLA_PROTINFO,          /* Protocol specific information for a link */
+#define IFLA_PROTINFO IFLA_PROTINFO
+       IFLA_TXQLEN,
+#define IFLA_TXQLEN IFLA_TXQLEN
+       IFLA_MAP,
+#define IFLA_MAP IFLA_MAP
+       IFLA_WEIGHT,
+#define IFLA_WEIGHT IFLA_WEIGHT
+       __IFLA_MAX
+};
+
+
+#define IFLA_MAX (__IFLA_MAX - 1)
+
+#define IFLA_RTA(r)  ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ifinfomsg))))
+#define IFLA_PAYLOAD(n) NLMSG_PAYLOAD(n,sizeof(struct ifinfomsg))
+
+/* ifi_flags.
+
+   IFF_* flags.
+
+   The only change is:
+   IFF_LOOPBACK, IFF_BROADCAST and IFF_POINTOPOINT are
+   more not changeable by user. They describe link media
+   characteristics and set by device driver.
+
+   Comments:
+   - Combination IFF_BROADCAST|IFF_POINTOPOINT is invalid
+   - If neither of these three flags are set;
+     the interface is NBMA.
+
+   - IFF_MULTICAST does not mean anything special:
+   multicasts can be used on all not-NBMA links.
+   IFF_MULTICAST means that this media uses special encapsulation
+   for multicast frames. Apparently, all IFF_POINTOPOINT and
+   IFF_BROADCAST devices are able to use multicasts too.
+ */
+
+/* IFLA_LINK.
+   For usual devices it is equal ifi_index.
+   If it is a "virtual interface" (f.e. tunnel), ifi_link
+   can point to real physical interface (f.e. for bandwidth calculations),
+   or maybe 0, what means, that real media is unknown (usual
+   for IPIP tunnels, when route to endpoint is allowed to change)
+ */
+
+/* Subtype attributes for IFLA_PROTINFO */
+enum
+{
+       IFLA_INET6_UNSPEC,
+       IFLA_INET6_FLAGS,       /* link flags                   */
+       IFLA_INET6_CONF,        /* sysctl parameters            */
+       IFLA_INET6_STATS,       /* statistics                   */
+       IFLA_INET6_MCAST,       /* MC things. What of them?     */
+       IFLA_INET6_CACHEINFO,   /* time values and max reasm size */
+       __IFLA_INET6_MAX
+};
+
+#define IFLA_INET6_MAX (__IFLA_INET6_MAX - 1)
+
+struct ifla_cacheinfo
+{
+       __u32   max_reasm_len;
+       __u32   tstamp;         /* ipv6InterfaceTable updated timestamp */
+       __u32   reachable_time;
+       __u32   retrans_time;
+};
+
+/*****************************************************************
+ *             Traffic control messages.
+ ****/
+
+struct tcmsg
+{
+       unsigned char   tcm_family;
+       unsigned char   tcm__pad1;
+       unsigned short  tcm__pad2;
+       int             tcm_ifindex;
+       __u32           tcm_handle;
+       __u32           tcm_parent;
+       __u32           tcm_info;
+};
+
+enum
+{
+       TCA_UNSPEC,
+       TCA_KIND,
+       TCA_OPTIONS,
+       TCA_STATS,
+       TCA_XSTATS,
+       TCA_RATE,
+       TCA_FCNT,
+       TCA_STATS2,
+       __TCA_MAX
+};
+
+#define TCA_MAX (__TCA_MAX - 1)
+
+#define TCA_RTA(r)  ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct tcmsg))))
+#define TCA_PAYLOAD(n) NLMSG_PAYLOAD(n,sizeof(struct tcmsg))
+
+
+/* RTnetlink multicast groups */
+
+#define RTMGRP_LINK            1
+#define RTMGRP_NOTIFY          2
+#define RTMGRP_NEIGH           4
+#define RTMGRP_TC              8
+
+#define RTMGRP_IPV4_IFADDR     0x10
+#define RTMGRP_IPV4_MROUTE     0x20
+#define RTMGRP_IPV4_ROUTE      0x40
+
+#define RTMGRP_IPV6_IFADDR     0x100
+#define RTMGRP_IPV6_MROUTE     0x200
+#define RTMGRP_IPV6_ROUTE      0x400
+#define RTMGRP_IPV6_IFINFO     0x800
+
+#define RTMGRP_DECnet_IFADDR    0x1000
+#define RTMGRP_DECnet_ROUTE     0x4000
+
+#define RTMGRP_IPV6_PREFIX     0x20000
+
+/* RTnetlink multicast groups */
+enum rtnetlink_groups {
+       RTNLGRP_NONE,
+       RTNLGRP_LINK,
+       RTNLGRP_NOTIFY,
+       RTNLGRP_NEIGH,
+       RTNLGRP_TC,
+       RTNLGRP_IPV4_IFADDR,
+       RTNLGRP_IPV4_MROUTE,
+       RTNLGRP_IPV4_ROUTE,
+       RTNLGRP_IPV4_RULE,
+       RTNLGRP_IPV6_IFADDR,
+       RTNLGRP_IPV6_MROUTE,
+       RTNLGRP_IPV6_ROUTE,
+       RTNLGRP_IPV6_IFINFO,
+       RTNLGRP_DECnet_IFADDR,
+       RTNLGRP_NOP2,
+       RTNLGRP_DECnet_ROUTE,
+       RTNLGRP_NOP3,
+       RTNLGRP_NOP4,
+       RTNLGRP_IPV6_PREFIX,
+       RTNLGRP_FIB_MAGIC,
+       __RTNLGRP_MAX
+};
+#define RTNLGRP_MAX    (__RTNLGRP_MAX - 1)
+
+
+/* TC action piece */
+struct tcamsg
+{
+       unsigned char   tca_family;
+       unsigned char   tca__pad1;
+       unsigned short  tca__pad2;
+};
+#define TA_RTA(r)  ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct tcamsg))))
+#define TA_PAYLOAD(n) NLMSG_PAYLOAD(n,sizeof(struct tcamsg))
+#define TCA_ACT_TAB 1 /* attr type must be >=1 */      
+#define TCAA_MAX 1
+
+/* End of information exported to user level */
+
+#endif /* __LINUX_RTNETLINK_H */
diff --git a/include/netlink/types.h b/include/netlink/types.h
new file mode 100644 (file)
index 0000000..903028e
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * netlink/netlink-types.h     Netlink Types
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef __NETLINK_TYPES_H_
+#define __NETLINK_TYPES_H_
+
+#include <stdio.h>
+
+/**
+ * Dumping types (dp_type)
+ * @ingroup utils
+ */
+enum nl_dump_type {
+       NL_DUMP_BRIEF,          /**< Dump object in a brief one-liner */
+       NL_DUMP_FULL,           /**< Dump all attributes but no statistics */
+       NL_DUMP_STATS,          /**< Dump all attributes including statistics */
+       NL_DUMP_XML,            /**< Dump all attribtes in XML format */
+       NL_DUMP_ENV,            /**< Dump all attribtues as env variables */
+       NL_DUMP_EVENTS,         /**< Dump event */
+       __NL_DUMP_MAX,
+};
+#define NL_DUMP_MAX (__NL_DUMP_MAX - 1)
+
+/**
+ * Dumping parameters
+ * @ingroup utils
+ */
+struct nl_dump_params
+{
+       /**
+        * Specifies the type of dump that is requested.
+        */
+       enum nl_dump_type       dp_type;
+
+       /**
+        * Specifies the number of whitespaces to be put in front
+        * of every new line (indentation).
+        */
+       int                     dp_prefix;
+
+       /**
+        * Causes the cache index to be printed for each element.
+        */
+       int                     dp_print_index;
+
+       /**
+        * Causes each element to be prefixed with the message type.
+        */
+       int                     dp_dump_msgtype;
+
+       /**
+        * A callback invoked for output
+        *
+        * Passed arguments are:
+        *  - dumping parameters
+        *  - string to append to the output
+        */
+       void                    (*dp_cb)(struct nl_dump_params *, char *);
+
+       /**
+        * A callback invoked for every new line, can be used to
+        * customize the indentation.
+        *
+        * Passed arguments are:
+        *  - dumping parameters
+        *  - line number starting from 0
+        */
+       void                    (*dp_nl_cb)(struct nl_dump_params *, int);
+
+       /**
+        * User data pointer, can be used to pass data to callbacks.
+        */
+       void                    *dp_data;
+
+       /**
+        * File descriptor the dumping output should go to
+        */
+       FILE *                  dp_fd;
+
+       /**
+        * Alternatively the output may be redirected into a buffer
+        */
+       char *                  dp_buf;
+
+       /**
+        * Length of the buffer dp_buf
+        */
+       size_t                  dp_buflen;
+
+       /**
+        * PRIVATE
+        * Set if a dump was performed prior to the actual dump handler.
+        */
+       int                     dp_pre_dump;
+};
+
+#endif
diff --git a/include/netlink/utils.h b/include/netlink/utils.h
new file mode 100644 (file)
index 0000000..3ce70a1
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * netlink/utils.h             Utility Functions
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef NETLINK_UTILS_H_
+#define NETLINK_UTILS_H_
+
+#include <netlink/netlink.h>
+#include <netlink/list.h>
+
+/**
+ * @name Probability Constants
+ * @{
+ */
+
+/**
+ * Lower probability limit
+ * @ingroup utils
+ */
+#define NL_PROB_MIN 0x0
+
+/**
+ * Upper probability limit
+ * @ingroup utils
+ */
+#define NL_PROB_MAX 0xffffffff
+
+/** @} */
+
+extern char *  nl_geterror(void);
+extern int     nl_get_errno(void);
+
+/* unit pretty-printing */
+extern double  nl_cancel_down_bytes(unsigned long long, char **);
+extern double  nl_cancel_down_bits(unsigned long long, char **);
+extern double  nl_cancel_down_us(uint32_t, char **);
+
+/* generic unit translations */
+extern long    nl_size2int(const char *);
+extern long    nl_prob2int(const char *);
+
+/* time translations */
+extern int     nl_get_hz(void);
+extern uint32_t        nl_us2ticks(uint32_t);
+extern uint32_t        nl_ticks2us(uint32_t);
+extern char *  nl_msec2str(uint64_t, char *, size_t);
+
+/* link layer protocol translations */
+extern char *  nl_llproto2str(int, char *, size_t);
+extern int     nl_str2llproto(const char *);
+
+/* ethernet protocol translations */
+extern char *  nl_ether_proto2str(int, char *, size_t);
+extern int     nl_str2ether_proto(const char *);
+
+
+#endif
diff --git a/install-sh b/install-sh
new file mode 100755 (executable)
index 0000000..6ce63b9
--- /dev/null
@@ -0,0 +1,294 @@
+#!/bin/sh
+#
+# install - install a program, script, or datafile
+#
+# This originates from X11R5 (mit/util/scripts/install.sh), which was
+# later released in X11R6 (xc/config/util/install.sh) with the
+# following copyright and license.
+#
+# Copyright (C) 1994 X Consortium
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to
+# deal in the Software without restriction, including without limitation the
+# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+# sell copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
+# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
+# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
+# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+#
+# Except as contained in this notice, the name of the X Consortium shall not
+# be used in advertising or otherwise to promote the sale, use or other deal-
+# ings in this Software without prior written authorization from the X Consor-
+# tium.
+#
+#
+# FSF changes to this file are in the public domain.
+#
+# Calling this script install-sh is preferred over install.sh, to prevent
+# `make' implicit rules from creating a file called install from it
+# when there is no Makefile.
+#
+# This script is compatible with the BSD install script, but was written
+# from scratch.  It can only install one file at a time, a restriction
+# shared with many OS's install programs.
+
+
+# set DOITPROG to echo to test this script
+
+# Don't use :- since 4.3BSD and earlier shells don't like it.
+doit="${DOITPROG-}"
+
+
+# put in absolute paths if you don't have them in your path; or use env. vars.
+
+mvprog="${MVPROG-mv}"
+cpprog="${CPPROG-cp}"
+chmodprog="${CHMODPROG-chmod}"
+chownprog="${CHOWNPROG-chown}"
+chgrpprog="${CHGRPPROG-chgrp}"
+stripprog="${STRIPPROG-strip}"
+rmprog="${RMPROG-rm}"
+mkdirprog="${MKDIRPROG-mkdir}"
+
+transformbasename=""
+transform_arg=""
+instcmd="$mvprog"
+chmodcmd="$chmodprog 0755"
+chowncmd=""
+chgrpcmd=""
+stripcmd=""
+rmcmd="$rmprog -f"
+mvcmd="$mvprog"
+src=""
+dst=""
+dir_arg=""
+
+while [ x"$1" != x ]; do
+    case $1 in
+       -c) instcmd=$cpprog
+           shift
+           continue;;
+
+       -d) dir_arg=true
+           shift
+           continue;;
+
+       -m) chmodcmd="$chmodprog $2"
+           shift
+           shift
+           continue;;
+
+       -o) chowncmd="$chownprog $2"
+           shift
+           shift
+           continue;;
+
+       -g) chgrpcmd="$chgrpprog $2"
+           shift
+           shift
+           continue;;
+
+       -s) stripcmd=$stripprog
+           shift
+           continue;;
+
+       -t=*) transformarg=`echo $1 | sed 's/-t=//'`
+           shift
+           continue;;
+
+       -b=*) transformbasename=`echo $1 | sed 's/-b=//'`
+           shift
+           continue;;
+
+       *)  if [ x"$src" = x ]
+           then
+               src=$1
+           else
+               # this colon is to work around a 386BSD /bin/sh bug
+               :
+               dst=$1
+           fi
+           shift
+           continue;;
+    esac
+done
+
+if [ x"$src" = x ]
+then
+       echo "$0: no input file specified" >&2
+       exit 1
+else
+       :
+fi
+
+if [ x"$dir_arg" != x ]; then
+       dst=$src
+       src=""
+
+       if [ -d "$dst" ]; then
+               instcmd=:
+               chmodcmd=""
+       else
+               instcmd=$mkdirprog
+       fi
+else
+
+# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
+# might cause directories to be created, which would be especially bad
+# if $src (and thus $dsttmp) contains '*'.
+
+       if [ -f "$src" ] || [ -d "$src" ]
+       then
+               :
+       else
+               echo "$0: $src does not exist" >&2
+               exit 1
+       fi
+
+       if [ x"$dst" = x ]
+       then
+               echo "$0: no destination specified" >&2
+               exit 1
+       else
+               :
+       fi
+
+# If destination is a directory, append the input filename; if your system
+# does not like double slashes in filenames, you may need to add some logic
+
+       if [ -d "$dst" ]
+       then
+               dst=$dst/`basename "$src"`
+       else
+               :
+       fi
+fi
+
+## this sed command emulates the dirname command
+dstdir=`echo "$dst" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
+
+# Make sure that the destination directory exists.
+#  this part is taken from Noah Friedman's mkinstalldirs script
+
+# Skip lots of stat calls in the usual case.
+if [ ! -d "$dstdir" ]; then
+defaultIFS='
+       '
+IFS="${IFS-$defaultIFS}"
+
+oIFS=$IFS
+# Some sh's can't handle IFS=/ for some reason.
+IFS='%'
+set - `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
+IFS=$oIFS
+
+pathcomp=''
+
+while [ $# -ne 0 ] ; do
+       pathcomp=$pathcomp$1
+       shift
+
+       if [ ! -d "$pathcomp" ] ;
+        then
+               $mkdirprog "$pathcomp"
+       else
+               :
+       fi
+
+       pathcomp=$pathcomp/
+done
+fi
+
+if [ x"$dir_arg" != x ]
+then
+       $doit $instcmd "$dst" &&
+
+       if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dst"; else : ; fi &&
+       if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dst"; else : ; fi &&
+       if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dst"; else : ; fi &&
+       if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dst"; else : ; fi
+else
+
+# If we're going to rename the final executable, determine the name now.
+
+       if [ x"$transformarg" = x ]
+       then
+               dstfile=`basename "$dst"`
+       else
+               dstfile=`basename "$dst" $transformbasename |
+                       sed $transformarg`$transformbasename
+       fi
+
+# don't allow the sed command to completely eliminate the filename
+
+       if [ x"$dstfile" = x ]
+       then
+               dstfile=`basename "$dst"`
+       else
+               :
+       fi
+
+# Make a couple of temp file names in the proper directory.
+
+       dsttmp=$dstdir/_inst.$$_
+       rmtmp=$dstdir/_rm.$$_
+
+# Trap to clean up temp files at exit.
+
+       trap 'status=$?; rm -f "$dsttmp" "$rmtmp" && exit $status' 0
+       trap '(exit $?); exit' 1 2 13 15
+
+# Move or copy the file name to the temp name
+
+       $doit $instcmd "$src" "$dsttmp" &&
+
+# and set any options; do chmod last to preserve setuid bits
+
+# If any of these fail, we abort the whole thing.  If we want to
+# ignore errors from any of these, just make sure not to ignore
+# errors from the above "$doit $instcmd $src $dsttmp" command.
+
+       if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp"; else :;fi &&
+       if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp"; else :;fi &&
+       if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dsttmp"; else :;fi &&
+       if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; else :;fi &&
+
+# Now remove or move aside any old file at destination location.  We try this
+# two ways since rm can't unlink itself on some systems and the destination
+# file might be busy for other reasons.  In this case, the final cleanup
+# might fail but the new file should still install successfully.
+
+{
+       if [ -f "$dstdir/$dstfile" ]
+       then
+               $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null ||
+               $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null ||
+               {
+                 echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
+                 (exit 1); exit
+               }
+       else
+               :
+       fi
+} &&
+
+# Now rename the file to the real destination.
+
+       $doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
+
+fi &&
+
+# The final little trick to "correctly" pass the exit status to the exit trap.
+
+{
+       (exit 0); exit
+}
diff --git a/lib/Makefile b/lib/Makefile
new file mode 100644 (file)
index 0000000..ba5ac6d
--- /dev/null
@@ -0,0 +1,76 @@
+#
+# lib/Makefile
+#
+#      This library is free software; you can redistribute it and/or
+#      modify it under the terms of the GNU Lesser General Public
+#      License as published by the Free Software Foundation version 2.1
+#      of the License.
+#
+# Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+#
+
+ifeq ($(shell [ ! -r ../Makefile.opts ] && echo 1),)
+    include ../Makefile.opts
+endif
+
+# Core
+CIN      := $(wildcard *.c)
+# NETLINK_ROUTE
+CIN      += $(wildcard route/*.c)
+# Schedulers
+CIN      += $(wildcard route/sch/*.c)
+# Classifiers
+CIN      += $(wildcard route/cls/*.c)
+# fib lookup
+CIN      += $(wildcard fib_lookup/*.c)
+
+DEPS     := $(CIN:%.c=.deps/%.d)
+OBJ      := $(CIN:%.c=%.o)
+CFLAGS   += -fPIC
+OUT_SLIB := $(PACKAGE_NAME).so.$(PACKAGE_VERSION)
+OUT_AR   := $(PACKAGE_NAME).a
+LN_SLIB  := $(PACKAGE_NAME).so
+LN1_SLIB := $(LN_SLIB).1
+
+export
+
+.PHONY: all clean install librtn.a $(OUT_SLIB)
+
+
+all:
+       @echo "  MAKE $(OUT_SLIB)"; \
+       $(MAKE) $(OUT_SLIB); \
+       $(MAKE) $(OUT_AR)
+
+$(OUT_SLIB): ../Makefile.opts $(OBJ)
+       @echo "  LD $(OUT_SLIB)"; \
+       $(CC) -shared -Wl,-soname,libnl.so.1 -o $(OUT_SLIB) $(OBJ) $(LIBNL_LIB) -lc
+       @echo "  LN $(OUT_SLIB) $(LN1_SLIB)"; \
+       rm -f $(LN1_SLIB) ; $(LN) -s $(OUT_SLIB) $(LN1_SLIB)
+       @echo "  LN $(LN1_SLIB) $(LN_SLIB)"; \
+       rm -f $(LN_SLIB) ; $(LN) -s $(LN1_SLIB) $(LN_SLIB)
+
+$(OUT_AR):  ../Makefile.opts $(OBJ)
+       @echo "  AR $(OUT_AR)"; \
+       $(AR) crus $(OUT_AR) $(OBJ)
+
+clean:
+       @echo "  CLEAN lib"; \
+       $(RM) -f $(OBJ) $(OUT_SLIB) $(LN_SLIB) $(LN1_SLIB); \
+       $(RM) -f $(OUT_SLIB) $(LN_SLIB) $(LN1_SLIB)
+
+distclean:
+       find .deps -iname \*\.d -exec rm -f '{}' \;
+
+install:
+       mkdir -p $(DESTDIR)$(libdir)/
+       install -m 0755 $(OUT_SLIB) $(DESTDIR)$(libdir)
+       install -m 0644 $(OUT_AR) $(DESTDIR)$(libdir)
+       rm -f $(DESTDIR)$(libdir)/$(LN1_SLIB)
+       $(LN) -s $(OUT_SLIB) $(DESTDIR)$(libdir)/$(LN1_SLIB)
+       rm -f $(DESTDIR)$(libdir)/$(LN_SLIB)
+       $(LN) -s $(LN1_SLIB) $(DESTDIR)$(libdir)/$(LN_SLIB)
+
+$(DEPS): ../Makefile.opts
+
+include ../Makefile.rules
diff --git a/lib/addr.c b/lib/addr.c
new file mode 100644 (file)
index 0000000..d7b7858
--- /dev/null
@@ -0,0 +1,902 @@
+/*
+ * lib/addr.c          Abstract Address
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup utils
+ * @defgroup addr Abstract Address
+ *
+ * @par 1) Transform character string to abstract address
+ * @code
+ * struct nl_addr *a = nl_addr_parse("::1", AF_UNSPEC);
+ * printf("Address family: %s\n", nl_af2str(nl_addr_get_family(a)));
+ * nl_addr_put(a);
+ * a = nl_addr_parse("11:22:33:44:55:66", AF_UNSPEC);
+ * printf("Address family: %s\n", nl_af2str(nl_addr_get_family(a)));
+ * nl_addr_put(a);
+ * @endcode
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink/netlink.h>
+#include <netlink/utils.h>
+#include <netlink/addr.h>
+#include <linux/socket.h>
+
+/* All this DECnet stuff is stolen from iproute2, thanks to whoever wrote
+ * this, probably Alexey. */
+static inline uint16_t dn_ntohs(uint16_t addr)
+{
+       union {
+               uint8_t byte[2];
+               uint16_t word;
+       } u = {
+               .word = addr,
+       };
+
+       return ((uint16_t) u.byte[0]) | (((uint16_t) u.byte[1]) << 8);
+}
+
+static inline int do_digit(char *str, uint16_t *addr, uint16_t scale,
+                          size_t *pos, size_t len, int *started)
+{
+       uint16_t tmp = *addr / scale;
+
+       if (*pos == len)
+               return 1;
+
+       if (((tmp) > 0) || *started || (scale == 1)) {
+               *str = tmp + '0';
+               *started = 1;
+               (*pos)++;
+               *addr -= (tmp * scale);
+       }
+
+       return 0;
+}
+
+static const char *dnet_ntop(char *addrbuf, size_t addrlen, char *str,
+                            size_t len)
+{
+       uint16_t addr = dn_ntohs(*(uint16_t *)addrbuf);
+       uint16_t area = addr >> 10;
+       size_t pos = 0;
+       int started = 0;
+
+       if (addrlen != 2)
+               return NULL;
+
+       addr &= 0x03ff;
+
+       if (len == 0)
+               return str;
+
+       if (do_digit(str + pos, &area, 10, &pos, len, &started))
+               return str;
+
+       if (do_digit(str + pos, &area, 1, &pos, len, &started))
+               return str;
+
+       if (pos == len)
+               return str;
+
+       *(str + pos) = '.';
+       pos++;
+       started = 0;
+
+       if (do_digit(str + pos, &addr, 1000, &pos, len, &started))
+               return str;
+
+       if (do_digit(str + pos, &addr, 100, &pos, len, &started))
+               return str;
+
+       if (do_digit(str + pos, &addr, 10, &pos, len, &started))
+               return str;
+
+       if (do_digit(str + pos, &addr, 1, &pos, len, &started))
+               return str;
+
+       if (pos == len)
+               return str;
+
+       *(str + pos) = 0;
+
+       return str;
+}
+
+static int dnet_num(const char *src, uint16_t * dst)
+{
+       int rv = 0;
+       int tmp;
+       *dst = 0;
+
+       while ((tmp = *src++) != 0) {
+               tmp -= '0';
+               if ((tmp < 0) || (tmp > 9))
+                       return rv;
+
+               rv++;
+               (*dst) *= 10;
+               (*dst) += tmp;
+       }
+
+       return rv;
+}
+
+static inline int dnet_pton(const char *src, char *addrbuf)
+{
+       uint16_t area = 0;
+       uint16_t node = 0;
+       int pos;
+
+       pos = dnet_num(src, &area);
+       if ((pos == 0) || (area > 63) ||
+           ((*(src + pos) != '.') && (*(src + pos) != ',')))
+               return -EINVAL;
+
+       pos = dnet_num(src + pos + 1, &node);
+       if ((pos == 0) || (node > 1023))
+               return -EINVAL;
+
+       *(uint16_t *)addrbuf = dn_ntohs((area << 10) | node);
+
+       return 1;
+}
+
+/**
+ * @name Creating Abstract Addresses
+ * @{
+ */
+
+/**
+ * Allocate new abstract address object.
+ * @arg maxsize                Maximum size of the binary address.
+ * @return Newly allocated address object or NULL
+ */
+struct nl_addr *nl_addr_alloc(size_t maxsize)
+{
+       struct nl_addr *addr;
+       
+       addr = calloc(1, sizeof(*addr) + maxsize);
+       if (!addr) {
+               nl_errno(ENOMEM);
+               return NULL;
+       }
+
+       addr->a_refcnt = 1;
+       addr->a_maxsize = maxsize;
+
+       return addr;
+}
+
+/**
+ * Allocate new abstract address object based on a binary address.
+ * @arg family         Address family.
+ * @arg buf            Buffer containing the binary address.
+ * @arg size           Length of binary address buffer.
+ * @return Newly allocated address handle or NULL
+ */
+struct nl_addr *nl_addr_build(int family, void *buf, size_t size)
+{
+       struct nl_addr *addr;
+
+       addr = nl_addr_alloc(size);
+       if (!addr)
+               return NULL;
+
+       addr->a_family = family;
+       addr->a_len = size;
+       addr->a_prefixlen = size*8;
+
+       if (size)
+               memcpy(addr->a_addr, buf, size);
+
+       return addr;
+}
+
+/**
+ * Allocate abstract address object based on a character string
+ * @arg addrstr                Address represented as character string.
+ * @arg hint           Address family hint or AF_UNSPEC.
+ *
+ * Regognizes the following address formats:
+ *@code
+ *  Format                      Len                Family
+ *  ----------------------------------------------------------------
+ *  IPv6 address format         16                 AF_INET6
+ *  ddd.ddd.ddd.ddd             4                  AF_INET
+ *  HH:HH:HH:HH:HH:HH           6                  AF_LLC
+ *  AA{.|,}NNNN                 2                  AF_DECnet
+ *  HH:HH:HH:...                variable           AF_UNSPEC
+ * @endcode
+ *
+ *  Special values:
+ *    - none: All bits and length set to 0.
+ *    - {default|all|any}: All bits set to 0, length based on hint or
+ *                         AF_INET if no hint is given.
+ *
+ * The prefix length may be appened at the end prefixed with a
+ * slash, e.g. 10.0.0.0/8.
+ *
+ * @return Newly allocated abstract address object or NULL.
+ */
+struct nl_addr *nl_addr_parse(const char *addrstr, int hint)
+{
+       int err, copy = 0, len = 0, family = AF_UNSPEC;
+       char *str, *prefix, buf[32];
+       struct nl_addr *addr = NULL; /* gcc ain't that smart */
+
+       str = strdup(addrstr);
+       if (!str) {
+               err = nl_errno(ENOMEM);
+               goto errout;
+       }
+
+       prefix = strchr(str, '/');
+       if (prefix)
+               *prefix = '\0';
+
+       if (!strcasecmp(str, "none")) {
+               family = hint;
+               goto prefix;
+       }
+
+       if (!strcasecmp(str, "default") ||
+           !strcasecmp(str, "all") ||
+           !strcasecmp(str, "any")) {
+                       
+               switch (hint) {
+                       case AF_INET:
+                       case AF_UNSPEC:
+                               /* Kind of a hack, we assume that if there is
+                                * no hint given the user wants to have a IPv4
+                                * address given back. */
+                               family = AF_INET;
+                               len = 4;
+                               goto prefix;
+
+                       case AF_INET6:
+                               family = AF_INET6;
+                               len = 16;
+                               goto prefix;
+
+                       case AF_LLC:
+                               family = AF_LLC;
+                               len = 6;
+                               goto prefix;
+
+                       default:
+                               err = nl_error(EINVAL, "Unsuported address" \
+                                   "family for default address");
+                               goto errout;
+               }
+       }
+
+       copy = 1;
+
+       if (hint == AF_INET || hint == AF_UNSPEC) {
+               if (inet_pton(AF_INET, str, buf) > 0) {
+                       family = AF_INET;
+                       len = 4;
+                       goto prefix;
+               }
+               if (hint == AF_INET) {
+                       err = nl_error(EINVAL, "Invalid IPv4 address");
+                       goto errout;
+               }
+       }
+
+       if (hint == AF_INET6 || hint == AF_UNSPEC) {
+               if (inet_pton(AF_INET6, str, buf) > 0) {
+                       family = AF_INET6;
+                       len = 16;
+                       goto prefix;
+               }
+               if (hint == AF_INET6) {
+                       err = nl_error(EINVAL, "Invalid IPv6 address");
+                       goto errout;
+               }
+       }
+
+       if ((hint == AF_LLC || hint == AF_UNSPEC) && strchr(str, ':')) {
+               unsigned int a, b, c, d, e, f;
+
+               if (sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
+                   &a, &b, &c, &d, &e, &f) == 6) {
+                       family = AF_LLC;
+                       len = 6;
+                       buf[0] = (unsigned char) a;
+                       buf[1] = (unsigned char) b;
+                       buf[2] = (unsigned char) c;
+                       buf[3] = (unsigned char) d;
+                       buf[4] = (unsigned char) e;
+                       buf[5] = (unsigned char) f;
+                       goto prefix;
+               }
+
+               if (hint == AF_LLC) {
+                       err = nl_error(EINVAL, "Invalid link layer address");
+                       goto errout;
+               }
+       }
+
+       if ((hint == AF_DECnet || hint == AF_UNSPEC) &&
+           (strchr(str, '.') || strchr(str, ','))) {
+               if (dnet_pton(str, buf) > 0) {
+                       family = AF_DECnet;
+                       len = 2;
+                       goto prefix;
+               }
+               if (hint == AF_DECnet) {
+                       err = nl_error(EINVAL, "Invalid DECnet address");
+                       goto errout;
+               }
+       }
+
+       if (hint == AF_UNSPEC && strchr(str, ':')) {
+               int i = 0;
+               char *s = str, *p;
+               for (;;) {
+                       long l = strtol(s, &p, 16);
+
+                       if (s == p || l > 0xff || i >= sizeof(buf)) {
+                               err = -EINVAL;
+                               goto errout;
+                       }
+
+                       buf[i++] = (unsigned char) l;
+                       if (*p == '\0')
+                               break;
+                       s = ++p;
+               }
+
+               len = i;
+               family = AF_UNSPEC;
+               goto prefix;
+       }
+
+       err = nl_error(EINVAL, "Invalid address");
+       goto errout;
+
+prefix:
+       addr = nl_addr_alloc(len);
+       if (!addr) {
+               err = nl_errno(ENOMEM);
+               goto errout;
+       }
+
+       nl_addr_set_family(addr, family);
+
+       if (copy)
+               nl_addr_set_binary_addr(addr, buf, len);
+
+       if (prefix) {
+               char *p;
+               long pl = strtol(++prefix, &p, 0);
+               if (p == prefix) {
+                       nl_addr_destroy(addr);
+                       err = -EINVAL;
+                       goto errout;
+               }
+               nl_addr_set_prefixlen(addr, pl);
+       } else
+               nl_addr_set_prefixlen(addr, len * 8);
+
+       err = 0;
+errout:
+       free(str);
+
+       return err ? NULL : addr;
+}
+
+/**
+ * Clone existing abstract address object.
+ * @arg addr           Abstract address object.
+ * @return Newly allocated abstract address object being a duplicate of the
+ *         specified address object or NULL if a failure occured.
+ */
+struct nl_addr *nl_addr_clone(struct nl_addr *addr)
+{
+       struct nl_addr *new;
+
+       new = nl_addr_build(addr->a_family, addr->a_addr, addr->a_len);
+       if (new)
+               new->a_prefixlen = addr->a_prefixlen;
+
+       return new;
+}
+
+/** @} */
+
+/**
+ * @name Destroying Abstract Addresses
+ * @{
+ */
+
+/**
+ * Destroy abstract address object.
+ * @arg addr           Abstract address object.
+ */
+void nl_addr_destroy(struct nl_addr *addr)
+{
+       if (!addr)
+               return;
+
+       if (addr->a_refcnt != 1)
+               BUG();
+
+       free(addr);
+}
+
+/** @} */
+
+/**
+ * @name Managing Usage References
+ * @{
+ */
+
+/**
+ * Request undestroyable reference of abstract address object.
+ * @arg addr           Abstract address object.
+ * @return Abstract address object of which the reference was given.
+ */
+struct nl_addr *nl_addr_get(struct nl_addr *addr)
+{
+       addr->a_refcnt++;
+
+       return addr;
+}
+
+/**
+ * Give back reference of abstract address object.
+ * @arg addr           Abstract address object.
+ * 
+ * Decrements the reference counter and destroys the object if the
+ * last reference was given back.
+ */
+void nl_addr_put(struct nl_addr *addr)
+{
+       if (!addr)
+               return;
+
+       if (addr->a_refcnt == 1)
+               nl_addr_destroy(addr);
+       else
+               addr->a_refcnt--;
+}
+
+/**
+ * Check whether an abstract address object is shared.
+ * @arg addr           Abstract address object.
+ * @return Non-zero if the abstract address object is shared, otherwise 0.
+ */
+int nl_addr_shared(struct nl_addr *addr)
+{
+       return addr->a_refcnt > 1;
+}
+
+/** @} */
+
+/**
+ * @name Miscellaneous
+ * @{
+ */
+
+/**
+ * Compares two abstract address objects.
+ * @arg a              A abstract address object.
+ * @arg b              Another abstract address object.
+ *
+ * @return Integer less than, equal to or greather than zero if \c is found,
+ *         respectively to be less than, to, or be greater than \c b.
+ */
+int nl_addr_cmp(struct nl_addr *a, struct nl_addr *b)
+{
+       int d = a->a_family - b->a_family;
+
+       if (d == 0) {
+               d = a->a_len - b->a_len;
+
+               if (a->a_len && d == 0)
+                       return memcmp(a->a_addr, b->a_addr, a->a_len);
+       }
+
+       return d;
+}
+
+/**
+ * Check if an address matches a certain family.
+ * @arg addr           Address represented as character string.
+ * @arg family         Desired address family.
+ *
+ * @return 1 if the address is of the desired address family,
+ *         otherwise 0 is returned.
+ */
+int nl_addr_valid(char *addr, int family)
+{
+       int ret;
+       char buf[32];
+
+       switch (family) {
+       case AF_INET:
+       case AF_INET6:
+               ret = inet_pton(family, addr, buf);
+               if (ret <= 0)
+                       return 0;
+               break;
+
+       case AF_DECnet:
+               ret = dnet_pton(addr, buf);
+               if (ret <= 0)
+                       return 0;
+               break;
+
+       case AF_LLC:
+               if (sscanf(addr, "%*02x:%*02x:%*02x:%*02x:%*02x:%*02x") != 6)
+                       return 0;
+               break;
+       }
+
+       return 1;
+}
+
+/**
+ * Guess address family of an abstract address object based on address size.
+ * @arg addr           Abstract address object.
+ * @return Address family or AF_UNSPEC if guessing wasn't successful.
+ */
+int nl_addr_guess_family(struct nl_addr *addr)
+{
+       switch (addr->a_len) {
+               case 4:
+                       return AF_INET;
+               case 6:
+                       return AF_LLC;
+               case 16:
+                       return AF_INET6;
+               default:
+                       return AF_UNSPEC;
+       }
+}
+
+/**
+ * Fill out sockaddr structure with values from abstract address object.
+ * @arg addr           Abstract address object.
+ * @arg sa             Destination sockaddr structure buffer.
+ * @arg salen          Length of sockaddr structure buffer.
+ *
+ * Fills out the specified sockaddr structure with the data found in the
+ * specified abstract address. The salen argument needs to be set to the
+ * size of sa but will be modified to the actual size used during before
+ * the function exits.
+ *
+ * @return 0 on success or a negative error code
+ */
+int nl_addr_fill_sockaddr(struct nl_addr *addr, struct sockaddr *sa,
+                         socklen_t *salen)
+{
+       switch (addr->a_family) {
+       case AF_INET: {
+               struct sockaddr_in *sai = (struct sockaddr_in *) sa;
+
+               if (*salen < sizeof(*sai))
+                       return -EINVAL;
+
+               sai->sin_family = addr->a_family;
+               memcpy(&sai->sin_addr, addr->a_addr, 4);
+               *salen = sizeof(*sai);
+       }
+               break;
+
+       case AF_INET6: {
+               struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
+
+               if (*salen < sizeof(*sa6))
+                       return -EINVAL;
+
+               sa6->sin6_family = addr->a_family;
+               memcpy(&sa6->sin6_addr, addr->a_addr, 16);
+               *salen = sizeof(*sa6);
+       }
+               break;
+
+       default:
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+
+/** @} */
+
+/**
+ * @name Getting Information About Addresses
+ * @{
+ */
+
+/**
+ * Call getaddrinfo() for an abstract address object.
+ * @arg addr           Abstract address object.
+ * 
+ * Calls getaddrinfo() for the specified abstract address in AI_NUMERICHOST
+ * mode.
+ *
+ * @note The caller is responsible for freeing the linked list using the
+ *       interface provided by getaddrinfo(3).
+ *
+ * @return A linked list of addrinfo handles or  NULL with an error message
+ *         associated.
+ */
+struct addrinfo *nl_addr_info(struct nl_addr *addr)
+{
+       int err;
+       struct addrinfo *res;
+       char buf[INET6_ADDRSTRLEN+5];
+       struct addrinfo hint = {
+               .ai_flags = AI_NUMERICHOST,
+               .ai_family = addr->a_family,
+       };
+
+       nl_addr2str(addr, buf, sizeof(buf));
+
+       err = getaddrinfo(buf, NULL, &hint, &res);
+       if (err != 0) {
+               nl_error(err, gai_strerror(err));
+               return NULL;
+       }
+
+       return res;
+}
+
+/**
+ * Resolve abstract address object to a name using getnameinfo().
+ * @arg addr           Abstract address object.
+ * @arg host           Destination buffer for host name.
+ * @arg hostlen                Length of destination buffer.
+ *
+ * Resolves the abstract address to a name and writes the looked up result
+ * into the host buffer. getnameinfo() is used to perform the lookup and
+ * is put into NI_NAMEREQD mode so the function will fail if the lookup
+ * couldn't be performed.
+ *
+ * @return 0 on success or a negative error code.
+ */
+int nl_addr_resolve(struct nl_addr *addr, char *host, size_t hostlen)
+{
+       int err;
+       struct sockaddr_in6 buf;
+       socklen_t salen = sizeof(buf);
+
+       err = nl_addr_fill_sockaddr(addr, (struct sockaddr *) &buf, &salen);
+       if (err < 0)
+               return err;
+
+       return getnameinfo((struct sockaddr *) &buf, salen,
+                          host, hostlen, NULL, 0, NI_NAMEREQD);
+}
+
+/** @} */
+
+/**
+ * @name Attribute Access
+ * @{
+ */
+
+/**
+ * Set address family of abstract address object.
+ * @arg addr           Abstract address object.
+ * @arg family         New address family.
+ */
+void nl_addr_set_family(struct nl_addr *addr, int family)
+{
+       addr->a_family = family;
+}
+
+/**
+ * Get address family of abstract address object.
+ * @arg addr           Abstract address object.
+ */
+int nl_addr_get_family(struct nl_addr *addr)
+{
+       return addr->a_family;
+}
+
+/**
+ * Set binary address of abstract address object.
+ * @arg addr           Abstract address object.
+ * @arg buf            Buffer containing binary address.
+ * @arg len            Length of buffer containing binary address.
+ */
+int nl_addr_set_binary_addr(struct nl_addr *addr, void *buf, size_t len)
+{
+       if (len > addr->a_maxsize)
+               return -ERANGE;
+
+       addr->a_len = len;
+       memcpy(addr->a_addr, buf, len);
+
+       return 0;
+}
+
+/**
+ * Get binary address of abstract address object.
+ * @arg addr           Abstract address object.
+ */
+void *nl_addr_get_binary_addr(struct nl_addr *addr)
+{
+       return addr->a_addr;
+}
+
+/**
+ * Get length of binary address of abstract address object.
+ * @arg addr           Abstract address object.
+ */
+unsigned int nl_addr_get_len(struct nl_addr *addr)
+{
+       return addr->a_len;
+}
+
+/**
+ * Set prefix length of abstract address object.
+ * @arg addr           Abstract address object.
+ * @arg prefixlen      New prefix length.
+ */
+void nl_addr_set_prefixlen(struct nl_addr *addr, int prefixlen)
+{
+       addr->a_prefixlen = prefixlen;
+}
+
+/**
+ * Get prefix length of abstract address object.
+ * @arg addr           Abstract address object.
+ */
+unsigned int nl_addr_get_prefixlen(struct nl_addr *addr)
+{
+       return addr->a_prefixlen;
+}
+
+/** @} */
+
+/**
+ * @name Translations to Strings
+ * @{
+ */
+
+/**
+ * Convert abstract address object to character string.
+ * @arg addr           Abstract address object.
+ * @arg buf            Destination buffer.
+ * @arg size           Size of destination buffer.
+ *
+ * Converts an abstract address to a character string and stores
+ * the result in the specified destination buffer.
+ *
+ * @return Address represented in ASCII stored in destination buffer.
+ */
+char *nl_addr2str(struct nl_addr *addr, char *buf, size_t size)
+{
+       int i;
+       char tmp[16];
+
+       if (!addr->a_len) {
+               snprintf(buf, size, "none");
+               goto prefix;
+       }
+
+       switch (addr->a_family) {
+               case AF_INET:
+                       inet_ntop(AF_INET, addr->a_addr, buf, size);
+                       break;
+
+               case AF_INET6:
+                       inet_ntop(AF_INET6, addr->a_addr, buf, size);
+                       break;
+
+               case AF_DECnet:
+                       dnet_ntop(addr->a_addr, addr->a_len, buf, size);
+                       break;
+
+               case AF_LLC:
+               default:
+                       snprintf(buf, size, "%02x", addr->a_addr[0]);
+                       for (i = 1; i < addr->a_len; i++) {
+                               snprintf(tmp, sizeof(tmp), ":%02x",
+                                        addr->a_addr[i]);
+                               strncat(buf, tmp, size - strlen(buf) - 1);
+                       }
+                       break;
+       }
+
+prefix:
+       if (addr->a_prefixlen != (8 * addr->a_len)) {
+               snprintf(tmp, sizeof(tmp), "/%u", addr->a_prefixlen);
+               strncat(buf, tmp, size - strlen(buf) - 1);
+       }
+
+       return buf;
+}
+
+/** @} */
+
+/**
+ * @name Address Family Transformations
+ * @{
+ */
+
+static struct trans_tbl afs[] = {
+       __ADD(AF_UNSPEC,unspec)
+       __ADD(AF_UNIX,unix)
+       __ADD(AF_LOCAL,local)
+       __ADD(AF_INET,inet)
+       __ADD(AF_AX25,ax25)
+       __ADD(AF_IPX,ipx)
+       __ADD(AF_APPLETALK,appletalk)
+       __ADD(AF_NETROM,netrom)
+       __ADD(AF_BRIDGE,bridge)
+       __ADD(AF_ATMPVC,atmpvc)
+       __ADD(AF_X25,x25)
+       __ADD(AF_INET6,inet6)
+       __ADD(AF_ROSE,rose)
+       __ADD(AF_DECnet,decnet)
+       __ADD(AF_NETBEUI,netbeui)
+       __ADD(AF_SECURITY,security)
+       __ADD(AF_KEY,key)
+       __ADD(AF_NETLINK,netlink)
+       __ADD(AF_ROUTE,route)
+       __ADD(AF_PACKET,packet)
+       __ADD(AF_ASH,ash)
+       __ADD(AF_ECONET,econet)
+       __ADD(AF_ATMSVC,atmsvc)
+       __ADD(AF_SNA,sna)
+       __ADD(AF_IRDA,irda)
+       __ADD(AF_PPPOX,pppox)
+       __ADD(AF_WANPIPE,wanpipe)
+       __ADD(AF_LLC,llc)
+       __ADD(AF_BLUETOOTH,bluetooth)
+};
+
+
+/**
+ * Convert address family to character string.
+ * @arg family         Address family.
+ * @arg buf            Destination buffer.
+ * @arg size           Size of destination buffer.
+ *
+ * Converts an address family to a character string and stores it in the
+ * provided buffer.
+ *
+ * @return The destination buffer or the type encoded in hexidecimal form
+ *        if no match was found.
+ */
+char *nl_af2str(char family, char *buf, size_t size)
+{
+       return __type2str(family, buf, size, afs, ARRAY_SIZE(afs));
+}
+
+/**
+ * Convert character string to address family.
+ * @arg name           Name of address family.
+ *
+ * Converts the provided character string specifying an address family
+ * to the corresponding numeric value.
+ *
+ * @return Address family as number or \c AF_UNSPEC.
+ */
+char nl_str2af(const char *name)
+{
+       char fam = __str2type(name, afs, ARRAY_SIZE(afs));
+       return fam >= 0 ? fam : AF_UNSPEC;
+}
+
+/** @} */
+
+/** @} */
diff --git a/lib/attr.c b/lib/attr.c
new file mode 100644 (file)
index 0000000..dd47b6e
--- /dev/null
@@ -0,0 +1,762 @@
+/*
+ * lib/attr.c          Netlink Attributes
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include <netlink-local.h>
+#include <netlink/netlink.h>
+#include <netlink/utils.h>
+#include <netlink/addr.h>
+#include <netlink/attr.h>
+#include <netlink/msg.h>
+#include <linux/socket.h>
+
+/**
+ * @ingroup nl
+ * @defgroup attr Attributes
+ * Netlink Attributes Construction/Parsing Interface
+ * @par 0) Introduction
+ * Netlink attributes are chained together following each other:
+ * @code
+ *    <------- nla_total_size(payload) ------->
+ *    <---- nla_attr_size(payload) ----->
+ *   +----------+- - -+- - - - - - - - - +- - -+-------- - -
+ *   |  Header  | Pad |     Payload      | Pad |  Header
+ *   +----------+- - -+- - - - - - - - - +- - -+-------- - -
+ *                     <- nla_len(nla) ->      ^
+ *   nla_data(nla)----^                        |
+ *   nla_next(nla)-----------------------------'
+ * @endcode
+ *
+ * @par
+ * The attribute header and payload must be aligned properly:
+ * @code
+ *  <------- NLA_HDRLEN ------> <-- NLA_ALIGN(payload)-->
+ * +---------------------+- - -+- - - - - - - - - -+- - -+
+ * |        Header       | Pad |     Payload       | Pad |
+ * |   (struct nlattr)   | ing |                   | ing |
+ * +---------------------+- - -+- - - - - - - - - -+- - -+
+ *  <-------------- nlattr->nla_len -------------->
+ * @endcode
+ *
+ * @par Nested TLVs:
+ * Nested TLVs are an array of TLVs nested into another TLV. This can be useful
+ * to allow subsystems to have their own formatting rules without the need to
+ * make the underlying layer be aware of it. It can also be useful to transfer
+ * arrays, lists and flattened trees.
+ * \code
+ *  <-------------------- NLA_ALIGN(...) ------------------->
+ * +---------------+- - - - - - - - - - - - - - - - - -+- - -+
+ * |               |+---------+---------+- - -+-------+|     |
+ * |  TLV Header   ||  TLV 1  |  TLV 2  |     | TLV n || Pad |
+ * |               |+---------+---------+- - -+-------+|     |
+ * +---------------+- - - - - - - - - - - - - - - - - -+- - -+
+ *                  <--------- nla_data(nla) --------->
+ * \endcode
+ *
+ * @par 1) Constructing a message with attributes
+ * @code
+ * int param1 = 10;
+ * char *param2 = "parameter text";
+ * struct nlmsghdr hdr = {
+ *     .nlmsg_type = MY_ACTION,
+ * };
+ * struct nl_msg *m = nlmsg_build(&hdr);
+ * nla_put_u32(m, 1, param1);
+ * nla_put_string(m, 2, param2);
+ * 
+ * nl_send_auto_complete(handle, nl_msg_get(m));
+ * nlmsg_free(m);
+ * @endcode
+ *
+ * @par 2) Constructing nested attributes
+ * @code
+ * struct nl_msg * nested_config(void)
+ * {
+ *     int a = 5, int b = 10;
+ *     struct nl_msg *n = nlmsg_build(NULL);
+ *     nla_put_u32(n, 10, a);
+ *     nla_put_u32(n, 20, b);
+ *     return n;
+ * }
+ *
+ * ...
+ * struct nl_msg *m = nlmsg_build(&hdr);
+ * struct nl_msg *nest = nested_config();
+ * nla_put_nested(m, 1, nest);
+ *
+ * nl_send_auto_complete(handle, nl_msg_get(m));
+ * nlmsg_free(nest);
+ * nlmsg_free(m);
+ * @endcode
+ * @{
+ */
+
+/**
+ * @name Size Calculations
+ * @{
+ */
+
+/**
+ * length of attribute not including padding
+ * @arg payload                length of payload
+ */
+int nla_attr_size(int payload)
+{
+       return NLA_HDRLEN + payload;
+}
+
+/**
+ * total length of attribute including padding
+ * @arg payload                length of payload
+ */
+int nla_total_size(int payload)
+{
+       return NLA_ALIGN(nla_attr_size(payload));
+}
+
+/**
+ * length of padding at the tail of the attribute
+ * @arg payload                length of payload
+ */
+int nla_padlen(int payload)
+{
+       return nla_total_size(payload) - nla_attr_size(payload);
+}
+
+/** @} */
+
+/**
+ * @name Payload Access
+ * @{
+ */
+
+/**
+ * head of payload
+ * @arg nla            netlink attribute
+ */
+void *nla_data(const struct nlattr *nla)
+{
+       return (char *) nla + NLA_HDRLEN;
+}
+
+/**
+ * length of payload
+ * @arg nla            netlink attribute
+ */
+int nla_len(const struct nlattr *nla)
+{
+       return nla->nla_len - NLA_HDRLEN;
+}
+
+/** @} */
+
+/**
+ * @name Attribute Parsing
+ * @{
+ */
+
+/**
+ * check if the netlink attribute fits into the remaining bytes
+ * @arg nla            netlink attribute
+ * @arg remaining      number of bytes remaining in attribute stream
+ */
+int nla_ok(const struct nlattr *nla, int remaining)
+{
+       return remaining >= sizeof(*nla) &&
+              nla->nla_len >= sizeof(*nla) &&
+              nla->nla_len <= remaining;
+}
+
+/**
+ * next netlink attribte in attribute stream
+ * @arg nla            netlink attribute
+ * @arg remaining      number of bytes remaining in attribute stream
+ *
+ * @return the next netlink attribute in the attribute stream and
+ * decrements remaining by the size of the current attribute.
+ */
+struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
+{
+       int totlen = NLA_ALIGN(nla->nla_len);
+
+       *remaining -= totlen;
+       return (struct nlattr *) ((char *) nla + totlen);
+}
+
+static uint16_t nla_attr_minlen[NLA_TYPE_MAX+1] = {
+       [NLA_U8]        = sizeof(uint8_t),
+       [NLA_U16]       = sizeof(uint16_t),
+       [NLA_U32]       = sizeof(uint32_t),
+       [NLA_U64]       = sizeof(uint64_t),
+       [NLA_STRING]    = 1,
+       [NLA_NESTED]    = NLA_HDRLEN,
+};
+
+static int validate_nla(struct nlattr *nla, int maxtype,
+                       struct nla_policy *policy)
+{
+       struct nla_policy *pt;
+       int minlen = 0;
+
+       if (nla->nla_type <= 0 || nla->nla_type > maxtype)
+               return 0;
+
+       pt = &policy[nla->nla_type];
+
+       if (pt->type > NLA_TYPE_MAX)
+               BUG();
+
+       if (pt->minlen)
+               minlen = pt->minlen;
+       else if (pt->type != NLA_UNSPEC)
+               minlen = nla_attr_minlen[pt->type];
+
+       if (pt->type == NLA_FLAG && nla_len(nla) > 0)
+               return -ERANGE;
+
+       if (nla_len(nla) < minlen)
+               return -ERANGE;
+
+       if (pt->maxlen && nla_len(nla) > pt->maxlen)
+               return -ERANGE;
+
+       if (pt->type == NLA_STRING) {
+               char *data = nla_data(nla);
+               if (data[nla_len(nla) - 1] != '\0')
+                       return -EINVAL;
+       }
+
+       return 0;
+}
+
+
+/**
+ * Parse a stream of attributes into a tb buffer
+ * @arg tb             destination array with maxtype+1 elements
+ * @arg maxtype                maximum attribute type to be expected
+ * @arg head           head of attribute stream
+ * @arg len            length of attribute stream
+ * @arg policy         validation policy
+ *
+ * Parses a stream of attributes and stores a pointer to each attribute in
+ * the tb array accessable via the attribute type. Attributes with a type
+ * exceeding maxtype will be silently ignored for backwards compatibility
+ * reasons. policy may be set to NULL if no validation is required.
+ *
+ * @return 0 on success or a negative error code.
+ */
+int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
+             struct nla_policy *policy)
+{
+       struct nlattr *nla;
+       int rem, err;
+
+       memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
+
+       nla_for_each_attr(nla, head, len, rem) {
+               uint16_t type = nla->nla_type;
+
+               if (type == 0) {
+                       fprintf(stderr, "Illegal nla->nla_type == 0\n");
+                       continue;
+               }
+
+               if (type <= maxtype) {
+                       if (policy) {
+                               err = validate_nla(nla, maxtype, policy);
+                               if (err < 0)
+                                       goto errout;
+                       }
+
+                       tb[type] = nla;
+               }
+       }
+
+       if (rem > 0)
+               fprintf(stderr, "netlink: %d bytes leftover after parsing "
+                      "attributes.\n", rem);
+
+       err = 0;
+errout:
+       return err;
+}
+
+
+/**
+ * parse nested attributes
+ * @arg tb             destination array with maxtype+1 elements
+ * @arg maxtype                maximum attribute type to be expected
+ * @arg nla            attribute containing the nested attributes
+ * @arg policy         validation policy
+ *
+ * @see nla_parse()
+ */
+int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla,
+                    struct nla_policy *policy)
+{
+       return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy);
+}
+
+/**
+ * Validate a stream of attributes
+ * @arg head           head of attribute stream
+ * @arg len            length of attribute stream
+ * @arg maxtype                maximum attribute type to be expected
+ * @arg policy         validation policy
+ *
+ * Validates all attributes in the specified attribute stream
+ * against the specified policy. Attributes with a type exceeding
+ * maxtype will be ignored. See documenation of struct nla_policy
+ * for more details.
+ *
+ * @return 0 on success or a negative error code.
+ */
+int nla_validate(struct nlattr *head, int len, int maxtype,
+                struct nla_policy *policy)
+{
+       struct nlattr *nla;
+       int rem, err;
+
+       nla_for_each_attr(nla, head, len, rem) {
+               err = validate_nla(nla, maxtype, policy);
+               if (err < 0)
+                       goto errout;
+       }
+
+       err = 0;
+errout:
+       return err;
+}
+
+/**
+ * Find a specific attribute in a stream of attributes
+ * @arg head           head of attribute stream
+ * @arg len            length of attribute stream
+ * @arg attrtype       type of attribute to look for
+ *
+ * @return the first attribute in the stream matching the specified type.
+ */
+struct nlattr *nla_find(struct nlattr *head, int len, int attrtype)
+{
+       struct nlattr *nla;
+       int rem;
+
+       nla_for_each_attr(nla, head, len, rem)
+               if (nla->nla_type == attrtype)
+                       return nla;
+
+       return NULL;
+}
+
+/** @} */
+
+/**
+ * @name Utilities
+ * @{
+ */
+
+/**
+ * Copy a netlink attribute into another memory area
+ * @arg dest           where to copy to memcpy
+ * @arg src            netlink attribute to copy from
+ * @arg count          size of the destination area
+ *
+ * Note: The number of bytes copied is limited by the length of
+ *       attribute's payload. memcpy
+ *
+ * @return the number of bytes copied.
+ */
+int nla_memcpy(void *dest, struct nlattr *src, int count)
+{
+       int minlen;
+
+       if (!src)
+               return 0;
+       
+       minlen = min_t(int, count, nla_len(src));
+       memcpy(dest, nla_data(src), minlen);
+
+       return minlen;
+}
+
+/**
+ * Copy string attribute payload into a sized buffer
+ * @arg dst            where to copy the string to
+ * @arg nla            attribute to copy the string from
+ * @arg dstsize                size of destination buffer
+ *
+ * Copies at most dstsize - 1 bytes into the destination buffer.
+ * The result is always a valid NUL-terminated string. Unlike
+ * strlcpy the destination buffer is always padded out.
+ *
+ * @return the length of the source buffer.
+ */
+size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
+{
+       size_t srclen = nla_len(nla);
+       char *src = nla_data(nla);
+
+       if (srclen > 0 && src[srclen - 1] == '\0')
+               srclen--;
+
+       if (dstsize > 0) {
+               size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
+
+               memset(dst, 0, dstsize);
+               memcpy(dst, src, len);
+       }
+
+       return srclen;
+}
+
+/**
+ * Compare an attribute with sized memory area
+ * @arg nla            netlink attribute
+ * @arg data           memory area
+ * @arg size           size of memory area
+ */
+int nla_memcmp(const struct nlattr *nla, const void *data,
+                            size_t size)
+{
+       int d = nla_len(nla) - size;
+
+       if (d == 0)
+               d = memcmp(nla_data(nla), data, size);
+
+       return d;
+}
+
+/**
+ * Compare a string attribute against a string
+ * @arg nla            netlink string attribute
+ * @arg str            another string
+ */
+int nla_strcmp(const struct nlattr *nla, const char *str)
+{
+       int len = strlen(str) + 1;
+       int d = nla_len(nla) - len;
+
+       if (d == 0)
+               d = memcmp(nla_data(nla), str, len);
+
+       return d;
+}
+
+/** @} */
+
+/**
+ * @name Attribute Construction
+ * @{
+ */
+
+/**
+ * reserve room for attribute on the skb
+ * @arg n              netlink message
+ * @arg attrtype       attribute type
+ * @arg attrlen                length of attribute payload
+ *
+ * Adds a netlink attribute header to a netlink message and reserves
+ * room for the payload but does not copy it.
+ */
+struct nlattr *nla_reserve(struct nl_msg *n, int attrtype, int attrlen)
+{
+       struct nlattr *nla;
+       int tlen;
+       
+       tlen = NLMSG_ALIGN(n->nm_nlh->nlmsg_len) + nla_total_size(attrlen);
+
+       n->nm_nlh = realloc(n->nm_nlh, tlen);
+       if (!n->nm_nlh) {
+               nl_errno(ENOMEM);
+               return NULL;
+       }
+
+       nla = (struct nlattr *) nlmsg_tail(n->nm_nlh);
+       nla->nla_type = attrtype;
+       nla->nla_len = nla_attr_size(attrlen);
+
+       memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
+       n->nm_nlh->nlmsg_len = tlen;
+
+       return nla;
+}
+
+/**
+ * Add a netlink attribute to a netlink message
+ * @arg n              netlink message
+ * @arg attrtype       attribute type
+ * @arg attrlen                length of attribute payload
+ * @arg data           head of attribute payload
+ *
+ * @return -1 if the tailroom of the skb is insufficient to store
+ * the attribute header and payload.
+ */
+int nla_put(struct nl_msg *n, int attrtype, int attrlen, const void *data)
+{
+       struct nlattr *nla;
+
+       nla = nla_reserve(n, attrtype, attrlen);
+       if (!nla)
+               return nl_errno(ENOMEM);
+
+       memcpy(nla_data(nla), data, attrlen);
+
+       return 0;
+}
+
+/**
+ * Add a nested netlink attribute to a netlink message
+ * @arg n              netlink message
+ * @arg attrtype       attribute type
+ * @arg nested         netlink attribute to nest
+ *
+ * @return -1 if the tailroom of the skb is insufficient to store
+ * the attribute header and payload.
+ */
+int nla_put_nested(struct nl_msg *n, int attrtype, struct nl_msg *nested)
+{
+       return nla_put(n, attrtype, nlmsg_len(nested->nm_nlh),
+                      nlmsg_data(nested->nm_nlh));
+}
+
+/**
+ * Add a u16 netlink attribute to a netlink message
+ * @arg n              netlink message
+ * @arg attrtype       attribute type
+ * @arg value          numeric value
+ */
+int nla_put_u8(struct nl_msg *n, int attrtype, uint8_t value)
+{
+       return nla_put(n, attrtype, sizeof(uint8_t), &value);
+}
+
+/**
+ * Add a u16 netlink attribute to a netlink message
+ * @arg n              netlink message
+ * @arg attrtype       attribute type
+ * @arg value          numeric value
+ */
+int nla_put_u16(struct nl_msg *n, int attrtype, uint16_t value)
+{
+       return nla_put(n, attrtype, sizeof(uint16_t), &value);
+}
+
+/**
+ * Add a u32 netlink attribute to a netlink message
+ * @arg n              netlink message
+ * @arg attrtype       attribute type
+ * @arg value          numeric value
+ */
+int nla_put_u32(struct nl_msg *n, int attrtype, uint32_t value)
+{
+       return nla_put(n, attrtype, sizeof(uint32_t), &value);
+}
+
+/**
+ * Add a u64 netlink attribute to a netlink message
+ * @arg n              netlink message
+ * @arg attrtype       attribute type
+ * @arg value          numeric value
+ */
+int nla_put_u64(struct nl_msg *n, int attrtype, uint64_t value)
+{
+       return nla_put(n, attrtype, sizeof(uint64_t), &value);
+}
+
+/**
+ * Add a string netlink attribute to a netlink message
+ * @arg n              netlink message
+ * @arg attrtype       attribute type
+ * @arg str            NUL terminated string
+ */
+int nla_put_string(struct nl_msg *n, int attrtype, const char *str)
+{
+       return nla_put(n, attrtype, strlen(str) + 1, str);
+}
+
+/**
+ * Add a flag netlink attribute to a netlink message
+ * @arg n              netlink message
+ * @arg attrtype       attribute type
+ */
+int nla_put_flag(struct nl_msg *n, int attrtype)
+{
+       return nla_put(n, attrtype, 0, NULL);
+}
+
+/**
+ * Add a msecs netlink attribute to a netlink message
+ * @arg n              netlink message
+ * @arg attrtype       attribute type
+ * @arg msecs          number of msecs
+ */
+int nla_put_msecs(struct nl_msg *n, int attrtype, unsigned long msecs)
+{
+       return nla_put_u64(n, attrtype, msecs);
+}
+
+/**
+ * Add an abstract data netlink attribute to a netlink message
+ * @arg n              netlink message
+ * @arg attrtype       attribute type
+ * @arg data           abstract data
+ */
+int nla_put_data(struct nl_msg *n, int attrtype, struct nl_data *data)
+{
+       return nla_put(n, attrtype, nl_data_get_size(data),
+                      nl_data_get(data));
+}
+
+/**
+ * Add an abstract address netlink attribute to a netlink message
+ * @arg n              netlink message
+ * @arg attrtype       attribute type
+ * @arg addr           abstract address
+ */
+int nla_put_addr(struct nl_msg *n, int attrtype, struct nl_addr *addr)
+{
+       return nla_put(n, attrtype, nl_addr_get_len(addr),
+                      nl_addr_get_binary_addr(addr));
+}
+
+/** @} */
+
+/**
+ * @name Attribute Nesting
+ * @{
+ */
+
+/**
+ * Start a new level of nested attributes
+ * @arg n              netlink message
+ * @arg attrtype       attribute type of container
+ *
+ * @return the container attribute
+ */
+struct nlattr *nla_nest_start(struct nl_msg *n, int attrtype)
+{
+       struct nlattr *start = (struct nlattr *) nlmsg_tail(n->nm_nlh);
+
+       if (nla_put(n, attrtype, 0, NULL) < 0)
+               return NULL;
+
+       return start;
+}
+
+/**
+ * Finalize nesting of attributes
+ * @arg n              netlink message
+ * @arg start          container attribute
+ *
+ * Corrects the container attribute header to include the all
+ * appeneded attributes.
+ *
+ * @return the total data length of the skb.
+ */
+int nla_nest_end(struct nl_msg *n, struct nlattr *start)
+{
+       start->nla_len = (unsigned char *) nlmsg_tail(n->nm_nlh) -
+                               (unsigned char *) start;
+       return 0;
+}
+
+/** @} */
+
+/**
+ * @name Attribute Reading
+ * @{
+ */
+
+/**
+ * Return payload of u32 attribute
+ * @arg nla            u32 netlink attribute
+ */
+uint32_t nla_get_u32(struct nlattr *nla)
+{
+       return *(uint32_t *) nla_data(nla);
+}
+
+/**
+ * Return payload of u16 attribute
+ * @arg nla            u16 netlink attribute
+ */
+uint16_t nla_get_u16(struct nlattr *nla)
+{
+       return *(uint16_t *) nla_data(nla);
+}
+
+/**
+ * Return payload of u8 attribute
+ * @arg nla            u8 netlink attribute
+ */
+uint8_t nla_get_u8(struct nlattr *nla)
+{
+       return *(uint8_t *) nla_data(nla);
+}
+
+/**
+ * Return payload of u64 attribute
+ * @arg nla            u64 netlink attribute
+ */
+uint64_t nla_get_u64(struct nlattr *nla)
+{
+       uint64_t tmp;
+
+       nla_memcpy(&tmp, nla, sizeof(tmp));
+
+       return tmp;
+}
+
+/**
+ * Return payload of flag attribute
+ * @arg nla            flag netlink attribute
+ */
+int nla_get_flag(struct nlattr *nla)
+{
+       return !!nla;
+}
+
+/**
+ * Return payload of msecs attribute
+ * @arg nla            msecs netlink attribute
+ *
+ * @return the number of milliseconds.
+ */
+unsigned long nla_get_msecs(struct nlattr *nla)
+{
+       return nla_get_u64(nla);
+}
+
+/**
+ * Return payload of address attribute
+ * @arg nla            address netlink attribute
+ * @arg family         address family
+ *
+ * @return Newly allocated address handle or NULL
+ */
+struct nl_addr *nla_get_addr(struct nlattr *nla, int family)
+{
+       return nl_addr_build(family, nla_data(nla), nla_len(nla));
+}
+
+/**
+ * Return payload of abstract data attribute
+ * @arg nla            abstract data netlink attribute
+ *
+ * @return Newly allocated abstract data handle or NULL
+ */
+struct nl_data *nla_get_data(struct nlattr *nla)
+{
+       return nl_data_alloc(nla_data(nla), nla_len(nla));
+}
+
+/** @} */
+
+/** @} */
diff --git a/lib/cache.c b/lib/cache.c
new file mode 100644 (file)
index 0000000..9b88c7c
--- /dev/null
@@ -0,0 +1,551 @@
+/*
+ * lib/cache.c         Caching Module
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup utils
+ * @defgroup cache Caching
+ *
+ * @code
+ *   Cache Management             |    | Type Specific Cache Operations
+ *                                      
+ *                                |    | +----------------+ +------------+
+ *                                       | request update | | msg_parser |
+ *                                |    | +----------------+ +------------+
+ *                                     +- - - - -^- - - - - - - -^- -|- - - -
+ *    nl_cache_update:            |              |               |   |
+ *          1) --------- co_request_update ------+               |   |
+ *                                |                              |   |
+ *          2) destroy old cache     +----------- pp_cb ---------|---+
+ *                                |  |                           |
+ *          3) ---------- nl_recvmsgs ----------+   +- cb_valid -+
+ *             +--------------+   |  |          |   |
+ *             | nl_cache_add |<-----+   + - - -v- -|- - - - - - - - - - -
+ *             +--------------+   |      | +-------------+
+ *                                         | nl_recvmsgs |
+ *                                |      | +-----|-^-----+
+ *                                           +---v-|---+
+ *                                |      |   | nl_recv |
+ *                                           +---------+
+ *                                |      |                 Core Netlink
+ * @endcode
+ * 
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink/netlink.h>
+#include <netlink/cache.h>
+#include <netlink/object.h>
+#include <netlink/utils.h>
+
+static inline char *nl_cache_name(struct nl_cache *cache)
+{
+       return cache->c_ops ? cache->c_ops->co_name : "unknown";
+}
+
+/**
+ * @name Access Functions
+ * @{
+ */
+
+/**
+ * Return the number of items in the cache
+ * @arg cache          cache handle
+ */
+int nl_cache_nitems(struct nl_cache *cache)
+{
+       return cache->c_nitems;
+}
+
+/**
+ * Return the number of items matching a filter in the cache
+ * @arg cache          Cache object.
+ * @arg filter         Filter object.
+ */
+int nl_cache_nitems_filter(struct nl_cache *cache, struct nl_object *filter)
+{
+       struct nl_cache_ops *ops = cache->c_ops;
+       struct nl_object *obj;
+       int nitems = 0;
+       
+       nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
+               if (filter && ops->co_filter && !ops->co_filter(obj, filter))
+                       continue;
+
+               nitems++;
+       }
+
+       return nitems;
+}
+
+/**
+ * Returns \b true if the cache is empty.
+ * @arg cache          Cache to check
+ * @return \a true if the cache is empty, otherwise \b false is returned.
+ */
+int nl_cache_is_empty(struct nl_cache *cache)
+{
+       return nl_list_empty(&cache->c_items);
+}
+
+/**
+ * Return the operations set of the cache
+ * @arg cache          cache handle
+ */
+struct nl_cache_ops *nl_cache_get_ops(struct nl_cache *cache)
+{
+       return cache->c_ops;
+}
+
+/**
+ * Return the first element in the cache
+ * @arg cache          cache handle
+ */
+struct nl_object *nl_cache_get_first(struct nl_cache *cache)
+{
+       if (nl_list_empty(&cache->c_items))
+               return NULL;
+
+       return nl_list_entry(cache->c_items.next,
+                            struct nl_object, ce_list);
+}
+
+/**
+ * Return the last element in the cache
+ * @arg cache          cache handle
+ */
+struct nl_object *nl_cache_get_last(struct nl_cache *cache)
+{
+       if (nl_list_empty(&cache->c_items))
+               return NULL;
+
+       return nl_list_entry(cache->c_items.prev,
+                            struct nl_object, ce_list);
+}
+
+/**
+ * Return the next element in the cache
+ * @arg obj            current object
+ */
+struct nl_object *nl_cache_get_next(struct nl_object *obj)
+{
+       if (nl_list_at_tail(obj, &obj->ce_cache->c_items, ce_list))
+               return NULL;
+       else
+               return nl_list_entry(obj->ce_list.next,
+                                    struct nl_object, ce_list);
+}
+
+/**
+ * Return the previous element in the cache
+ * @arg obj            current object
+ */
+struct nl_object *nl_cache_get_prev(struct nl_object *obj)
+{
+       if (nl_list_at_head(obj, &obj->ce_cache->c_items, ce_list))
+               return NULL;
+       else
+               return nl_list_entry(obj->ce_list.prev,
+                                    struct nl_object, ce_list);
+}
+
+/** @} */
+
+/**
+ * @name Cache Creation/Deletion
+ * @{
+ */
+
+/**
+ * Allocate an empty cache of no certain type
+ * 
+ * @return A newly allocated and initialized cache.
+ */
+struct nl_cache *nl_cache_alloc(void)
+{
+       struct nl_cache *cache;
+
+       cache = calloc(1, sizeof(*cache));
+       if (!cache) {
+               nl_errno(ENOMEM);
+               return NULL;
+       }
+
+       nl_init_list_head(&cache->c_items);
+       NL_DBG(2, "Allocated cache %p <%s>.\n", cache, nl_cache_name(cache));
+
+       return cache;
+}
+
+/**
+ * Allocate an empty cache based on cache operations
+ * @arg ops            cache operations to base the cache on
+ * @return A newly allocated and initialized cache.
+ */
+struct nl_cache *nl_cache_alloc_from_ops(struct nl_cache_ops *ops)
+{
+       struct nl_cache *new;
+
+       new = nl_cache_alloc();
+       if (!new)
+               return NULL;
+
+       new->c_ops = ops;
+
+       return new;
+}
+
+/**
+ * Allocate an empty cache based on type name
+ * @arg kind           Name of cache type
+ * @return A newly allocated and initialized cache.
+ */
+struct nl_cache *nl_cache_alloc_name(const char *kind)
+{
+       struct nl_cache_ops *ops;
+
+       ops = nl_cache_mngt_lookup(kind);
+       if (!ops) {
+               nl_error(ENOENT, "Unable to lookup cache \"%s\"", kind);
+               return NULL;
+       }
+
+       return nl_cache_alloc_from_ops(ops);
+}
+
+/**
+ * Clear a cache.
+ * @arg cache          cache to clear
+ *
+ * Removes all elements of a cache.
+ */
+void nl_cache_clear(struct nl_cache *cache)
+{
+       struct nl_object *obj, *tmp;
+
+       NL_DBG(1, "Clearing cache %p <%s>...\n", cache, nl_cache_name(cache));
+
+       nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list)
+               nl_cache_delete(cache, obj);
+}
+
+/**
+ * Free a cache.
+ * @arg cache          Cache to free.
+ *
+ * Removes all elements of a cache and frees all memory.
+ *
+ * @note Use this function if you are working with allocated caches.
+ */
+void nl_cache_free(struct nl_cache *cache)
+{
+       nl_cache_clear(cache);
+       NL_DBG(1, "Freeing cache %p <%s>...\n", cache, nl_cache_name(cache));
+       free(cache);
+}
+
+/** @} */
+
+/**
+ * @name Cache Modifications
+ * @{
+ */
+
+/**
+ * Add an element to the cache.
+ * @arg cache          cache to add a element to
+ * @arg obj            Common obj to be added to the cache
+ *
+ * Adds the object \c obj to the tail of the cache \c cache and. The
+ * cache is enlarged as needed.
+ *
+ * @return 0 or a negative error code.
+ */
+int nl_cache_add(struct nl_cache *cache, struct nl_object *obj)
+{
+       struct nl_object *new;
+
+       if (nl_object_shared(obj)) {
+               new = nl_object_clone(obj);
+               if (!new)
+                       return nl_errno(ENOMEM);
+
+               nl_object_put(obj);
+       } else
+               new = obj;
+
+       new->ce_cache = cache;
+
+       nl_list_add_tail(&new->ce_list, &cache->c_items);
+       cache->c_nitems++;
+
+       NL_DBG(1, "Added %p to cache %p <%s>.\n",
+              new, cache, nl_cache_name(cache));
+
+       return 0;
+}
+
+static int subsys_parse_cb(struct nl_object *c, struct nl_parser_param *p)
+{
+       return nl_cache_add((struct nl_cache *) p->pp_arg, c);
+}
+
+/** @cond SKIP */
+int nl_cache_parse(struct nl_cache_ops *ops, struct sockaddr_nl *who,
+                  struct nlmsghdr *nlh, struct nl_parser_param *params)
+{
+       int i, len, err, hdrsize;
+
+       hdrsize = ops->co_hdrsize;
+       len = nlh->nlmsg_len - nlmsg_msg_size(hdrsize);
+       if (len < 0) {
+               err = nl_error(EINVAL, "netlink message too short to "
+                                      "of kind %s", ops->co_name);
+               goto errout;
+       }
+
+       for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
+               if (ops->co_msgtypes[i].mt_id == nlh->nlmsg_type)
+                       return ops->co_msg_parser(who, nlh, params);
+
+       err = nl_error(EINVAL, "Unsupported netlink message type %d",
+                      nlh->nlmsg_type);
+errout:
+       return err;
+}
+/** @endcond */
+
+/**
+ * Parse a netlink message and add it to the cache.
+ * @arg cache          cache to add element to
+ * @arg msg            netlink message
+ *
+ * Parses a netlink message by calling the cache specific message parser
+ * and adds the new element to the cache.
+ *
+ * @return 0 or a negative error code.
+ */
+int nl_cache_parse_and_add(struct nl_cache *cache, struct nl_msg *msg)
+{
+       struct nl_parser_param p = {
+               .pp_cb = subsys_parse_cb,
+               .pp_arg = cache,
+       };
+
+       return nl_cache_parse(cache->c_ops, NULL, nlmsg_hdr(msg), &p);
+}
+
+/**
+ * Delete an element from a cache.
+ * @arg cache          cache to delete the element from
+ * @arg obj            Object to delete
+ *
+ * Deletes the object \c obj from the cache \c cache.
+ */
+void nl_cache_delete(struct nl_cache *cache, struct nl_object *obj)
+{
+       if (obj->ce_cache != cache)
+               BUG();
+
+       nl_list_del(&obj->ce_list);
+       obj->ce_cache = NULL;
+       nl_object_put(obj);
+       cache->c_nitems--;
+
+       NL_DBG(1, "Deleted %p from cache %p <%s>.\n",
+              obj, cache, nl_cache_name(cache));
+}
+
+/** @cond SKIP */
+struct update_xdata {
+       struct nl_cache_ops *ops;
+       struct nl_parser_param *params;
+};
+/** @endcond */
+
+static int update_msg_parser(struct nl_msg *msg, void *arg)
+{
+       struct update_xdata *x = arg;
+       
+       return nl_cache_parse(x->ops, &msg->nm_src, msg->nm_nlh, x->params);
+}
+
+/**
+ * Pickup a netlink dump response and put it into a cache.
+ * @arg handle         Netlink handle.
+ * @arg cache          Cache to put items into.
+ *
+ * Waits for netlink messages to arrive, parses them and puts them into
+ * the specified cache.
+ *
+ * @return 0 on success or a negative error code.
+ */
+int nl_cache_pickup(struct nl_handle *handle, struct nl_cache *cache)
+{
+       int err;
+       struct nl_cache_ops *ops = cache->c_ops;
+       struct nl_cb *cb;
+       struct nl_parser_param p = {
+               .pp_cb = subsys_parse_cb,
+               .pp_arg = cache,
+       };
+       struct update_xdata x = {
+               .ops = ops,
+               .params = &p,
+       };
+
+       NL_DBG(1, "Filling cache %p <%s>...\n", cache, nl_cache_name(cache));
+
+       cb = nl_cb_clone(nl_handle_get_cb(handle));
+       nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, update_msg_parser, &x);
+
+       err = nl_recvmsgs(handle, cb);
+       if (err < 0)
+               NL_DBG(2, "While picking up for %p <%s>, recvmsgs() returned " \
+                      "%d: %s", cache, nl_cache_name(cache),
+                      err, nl_geterror());
+
+       nl_cb_destroy(cb);
+
+       return err;
+}
+
+/**
+ * Update (synchronize) a local cache with the kernel.
+ * @arg handle         netlink handle
+ * @arg cache          cache to update
+ *
+ * Updates the local cache \c cache with the state in the kernel. During
+ * this process the cache gets emptied and refilled with the new content
+ * received from the kernel.
+ *
+ * @return 0 or a negative error code.
+ */
+int nl_cache_update(struct nl_handle *handle, struct nl_cache *cache)
+{
+       int err;
+       struct nl_cache_ops *ops = cache->c_ops;
+
+       err = ops->co_request_update(cache, handle);
+       if (err < 0)
+               return err;
+
+       NL_DBG(2, "Upading cache %p <%s>, request sent, waiting for dump...\n",
+              cache, nl_cache_name(cache));
+       nl_cache_clear(cache);
+
+       return nl_cache_pickup(handle, cache);
+}
+
+/** @} */
+
+/**
+ * @name Dumping
+ * @{
+ */
+
+/**
+ * Dump all elements of a cache.
+ * @arg cache          cache to dump
+ * @arg params         dumping parameters
+ *
+ * Dumps all elements of the \a cache to the file descriptor \a fd.
+ */
+void nl_cache_dump(struct nl_cache *cache, struct nl_dump_params *params)
+{
+       NL_DBG(1, "Dumping cache %p <%s>\n", cache, nl_cache_name(cache));
+       nl_cache_dump_filter(cache, params, NULL);
+}
+
+/**
+ * Dump all elements of a cache (filtered).
+ * @arg cache          cache to dump
+ * @arg params         dumping parameters (optional)
+ * @arg filter         filter object
+ *
+ * Dumps all elements of the \a cache to the file descriptor \a fd
+ * given they match the given filter \a filter.
+ */
+void nl_cache_dump_filter(struct nl_cache *cache,
+                         struct nl_dump_params *params,
+                         struct nl_object *filter)
+{
+       int type = params ? params->dp_type : NL_DUMP_FULL;
+       struct nl_cache_ops *ops = cache->c_ops;
+       struct nl_object *obj;
+
+       if (type > NL_DUMP_MAX || type < 0)
+               BUG();
+
+       if (!ops->co_dump[type])
+               return;
+
+       nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
+               if (filter && obj->ce_ops != filter->ce_ops)
+                       continue;
+               
+               if (filter && ops->co_filter && !ops->co_filter(obj, filter))
+                       continue;
+
+               dump_from_ops(obj, params);
+       }
+}
+
+/** @} */
+
+/**
+ * @name Iterators
+ * @{
+ */
+
+/**
+ * Call a callback on each element of the cache.
+ * @arg cache          cache to iterate on
+ * @arg cb             callback function
+ * @arg arg            argument passed to callback function
+ *
+ * Calls a callback function \a cb on each element of the \a cache.
+ * The argument \a arg is passed on the callback function.
+ */
+void nl_cache_foreach(struct nl_cache *cache,
+                     void (*cb)(struct nl_object *, void *), void *arg)
+{
+       nl_cache_foreach_filter(cache, NULL, cb, arg);
+}
+
+/**
+ * Call a callback on each element of the cache (filtered).
+ * @arg cache          cache to iterate on
+ * @arg filter         filter object
+ * @arg cb             callback function
+ * @arg arg            argument passed to callback function
+ *
+ * Calls a callback function \a cb on each element of the \a cache
+ * that matches the \a filter. The argument \a arg is passed on
+ * to the callback function.
+ */
+void nl_cache_foreach_filter(struct nl_cache *cache, struct nl_object *filter,
+                            void (*cb)(struct nl_object *, void *), void *arg)
+{
+       struct nl_object *obj, *tmp;
+       struct nl_cache_ops *ops = cache->c_ops;
+
+       nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) {
+               if (filter && ops->co_filter && !ops->co_filter(obj, filter))
+                       continue;
+
+               cb(obj, arg);
+       }
+}
+
+/** @} */
+
+/** @} */
diff --git a/lib/cache_mngt.c b/lib/cache_mngt.c
new file mode 100644 (file)
index 0000000..98a26c1
--- /dev/null
@@ -0,0 +1,254 @@
+/*
+ * lib/cache_mngt.c    Cache Management
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup utils
+ * @defgroup cache_mngt Cache Management
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink/netlink.h>
+#include <netlink/cache.h>
+#include <netlink/utils.h>
+
+/**
+ * @name Access Functions
+ * @{
+ */
+
+/**
+ * Return the cache type of the cache operations
+ * @arg ops            cache operations
+ */
+char *nl_cache_ops_get_name(struct nl_cache_ops *ops)
+{
+       return ops->co_name;
+}
+
+/** @} */
+
+/**
+ * @name Message Type Association
+ * @{
+ */
+
+static struct nl_cache_ops *cache_ops;
+
+/**
+ * Associate a message type to a set of cache operations
+ * @arg protocol               netlink protocol
+ * @arg message_type           netlink message type
+ *
+ * Associates the specified netlink message type with
+ * a registered set of cache operations.
+ *
+ * @return The cache operations or NULL if no association
+ *         could be made.
+ */
+struct nl_cache_ops *nl_cache_mngt_associate(int protocol, int message_type)
+{
+       int i;
+       struct nl_cache_ops *ops;
+
+       for (ops = cache_ops; ops; ops = ops->co_next)
+               for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
+                       if (ops->co_msgtypes[i].mt_id == message_type &&
+                           ops->co_protocol == protocol)
+                               return ops;
+
+       return NULL;
+}
+
+/**
+ * Convert message type to character string.
+ * @arg ops            Cache operations.
+ * @arg msgtype                Message type.
+ * @arg buf            Destination buffer.
+ * @arg len            Size of destination buffer.
+ *
+ * Converts a message type to a character string and stores it in the
+ * provided buffer.
+ *
+ * @return The destination buffer or the message type encoded in
+ *         hexidecimal form if no match was found.
+ */
+char *nl_cache_mngt_type2name(struct nl_cache_ops *ops, int msgtype,
+                             char *buf, size_t len)
+{
+       int i;
+
+       for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++) {
+               if (ops->co_msgtypes[i].mt_id == msgtype) {
+                       snprintf(buf, len, "%s::%s",
+                                ops->co_name,
+                                ops->co_msgtypes[i].mt_name);
+                       return buf;
+               }
+       }
+
+       snprintf(buf, len, "%s->0x%x()", ops->co_name, msgtype);
+       return buf;
+}
+
+/** @} */
+
+/**
+ * @name Cache Type Management
+ * @{
+ */
+
+/**
+ * Lookup the set cache operations of a certain cache type
+ * @arg name           name of the cache type
+ *
+ * @return The cache operations or NULL if no operations
+ *         have been registered under the specified name.
+ */
+struct nl_cache_ops *nl_cache_mngt_lookup(const char *name)
+{
+       struct nl_cache_ops *ops;
+
+       for (ops = cache_ops; ops; ops = ops->co_next)
+               if (!strcmp(ops->co_name, name))
+                       return ops;
+
+       return NULL;
+}
+
+/**
+ * Register a set of cache operations
+ * @arg ops            cache operations
+ *
+ * Called by users of caches to announce the avaibility of
+ * a certain cache type.
+ *
+ * @return 0 on success or a negative error code.
+ */
+int nl_cache_mngt_register(struct nl_cache_ops *ops)
+{
+       if (!ops->co_name)
+               return nl_error(EINVAL, "No cache name specified");
+
+       if (nl_cache_mngt_lookup(ops->co_name))
+               return nl_error(EEXIST, "Cache operations already exist");
+           
+       ops->co_next = cache_ops;
+       cache_ops = ops;
+
+       NL_DBG(1, "Registered cache operations %s\n", ops->co_name);
+
+       return 0;
+}
+
+/**
+ * Unregister a set of cache operations
+ * @arg ops            cache operations
+ *
+ * Called by users of caches to announce a set of
+ * cache operations is no longer available. The
+ * specified cache operations must have been registered
+ * previously using nl_cache_mngt_register()
+ *
+ * @return 0 on success or a negative error code
+ */
+int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
+{
+       struct nl_cache_ops *t, **tp;
+
+       for (tp = &cache_ops; (t=*tp) != NULL; tp = &t->co_next)
+               if (t == ops)
+                       break;
+
+       if (!t)
+               return nl_error(ENOENT, "No such cache operations");
+
+       NL_DBG(1, "Unregistered cache operations %s\n", ops->co_name);
+
+       *tp = t->co_next;
+       return 0;
+}
+
+/** @} */
+
+/**
+ * @name Global Cache Provisioning/Requiring
+ * @{
+ */
+
+/**
+ * Provide a cache for global use
+ * @arg cache          cache to provide
+ *
+ * Offers the specified cache to be used by other modules.
+ * Only one cache per type may be shared at a time,
+ * a previsouly provided caches will be overwritten.
+ */
+void nl_cache_mngt_provide(struct nl_cache *cache)
+{
+       struct nl_cache_ops *ops;
+
+       ops = nl_cache_mngt_lookup(cache->c_ops->co_name);
+       if (!ops)
+               BUG();
+       else
+               ops->co_major_cache = cache;
+}
+
+/**
+ * Unprovide a cache for global use
+ * @arg cache          cache to unprovide
+ *
+ * Cancels the offer to use a cache globally. The
+ * cache will no longer be returned via lookups but
+ * may still be in use.
+ */
+void nl_cache_mngt_unprovide(struct nl_cache *cache)
+{
+       struct nl_cache_ops *ops;
+
+       ops = nl_cache_mngt_lookup(cache->c_ops->co_name);
+       if (!ops)
+               BUG();
+       else if (ops->co_major_cache == cache)
+               ops->co_major_cache = NULL;
+}
+
+/**
+ * Demand the use of a global cache
+ * @arg name           name of the required cache type
+ *
+ * Trys to find a cache of the specified type for global
+ * use.
+ *
+ * @return A cache provided by another subsystem of the
+ *         specified type marked to be available.
+ */
+struct nl_cache *nl_cache_mngt_require(const char *name)
+{
+       struct nl_cache_ops *ops;
+
+       ops = nl_cache_mngt_lookup(name);
+       if (!ops || !ops->co_major_cache) {
+               fprintf(stderr, "Application BUG: Your application must "
+                       "call nl_cache_mngt_provide() and\nprovide a valid "
+                       "%s cache to be used for internal lookups.\nSee the "
+                       " API documentation for more details.\n", name);
+
+               return NULL;
+       }
+       
+       return ops->co_major_cache;
+}
+
+/** @} */
+
+/** @} */
diff --git a/lib/data.c b/lib/data.c
new file mode 100644 (file)
index 0000000..9175550
--- /dev/null
@@ -0,0 +1,162 @@
+/*
+ * lib/data.c          Abstract Data
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup utils
+ * @defgroup data Abstract Data
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink/netlink.h>
+#include <netlink/utils.h>
+#include <linux/socket.h>
+
+/**
+ * @name General
+ * @{
+ */
+
+/**
+ * Allocate a new abstract data object.
+ * @arg buf            Data buffer containing the actual data.
+ * @arg size           Size of data buffer.
+ *
+ * Allocates a new abstract data and copies the specified data
+ * buffer into the new handle.
+ * 
+ * @return Newly allocated data handle or NULL
+ */
+struct nl_data *nl_data_alloc(void *buf, size_t size)
+{
+       struct nl_data *data;
+
+       data = calloc(1, sizeof(*data));
+       if (!data)
+               goto errout;
+
+       data->d_data = calloc(1, size);
+       if (!data->d_data) {
+               free(data);
+               goto errout;
+       }
+
+       data->d_size = size;
+
+       if (buf)
+               memcpy(data->d_data, buf, size);
+
+       return data;
+errout:
+       nl_errno(ENOMEM);
+       return NULL;
+}
+
+/**
+ * Append data to an abstract data object.
+ * @arg data           Abstract data object.
+ * @arg buf            Data buffer containing the data to be appended.
+ * @arg size           Size of data to be apppended.
+ *
+ * Reallocates an abstract data and copies the specified data
+ * buffer into the new handle.
+ * 
+ * @return 0 on success or a negative error code
+ */
+int nl_data_append(struct nl_data *data, void *buf, size_t size)
+{
+       if (size < 0)
+               BUG();
+
+       if (size > 0) {
+               data->d_data = realloc(data->d_data, data->d_size + size);
+               if (!data->d_data)
+                       return nl_errno(ENOMEM);
+
+               if (buf)
+                       memcpy(data->d_data + data->d_size, buf, size);
+               else
+                       memset(data->d_data + data->d_size, 0, size);
+
+               data->d_size += size;
+       }
+
+       return 0;
+}
+
+
+/**
+ * Free an abstract data object.
+ * @arg data           Abstract data object.
+ */
+void nl_data_free(struct nl_data *data)
+{
+       if (data)
+               free(data->d_data);
+
+       free(data);
+}
+
+/** @} */
+
+/**
+ * @name Attribute Access
+ * @{
+ */
+
+/**
+ * Get data buffer of abstract data object.
+ * @arg data           Abstract data object.
+ * @return Data buffer or NULL if empty.
+ */
+void *nl_data_get(struct nl_data *data)
+{
+       return data->d_size > 0 ? data->d_data : NULL;
+}
+
+/**
+ * Get size of data buffer of abstract data object.
+ * @arg data           Abstract data object.
+ * @return Size of data buffer.
+ */
+size_t nl_data_get_size(struct nl_data *data)
+{
+       return data->d_size;
+}
+
+/** @} */
+
+/**
+ * @name Misc
+ * @{
+ */
+
+/**
+ * Compare two abstract data objects.
+ * @arg a              Abstract data object.
+ * @arg b              Another abstract data object.
+ * @return An integer less than, equal to, or greater than zero if
+ *         a is found, respectively, to be less than, to match, or
+ *         be greater than b.
+ */
+int nl_data_cmp(struct nl_data *a, struct nl_data *b)
+{
+       void *a_ = nl_data_get(a);
+       void *b_ = nl_data_get(b);
+
+       if (a_ && b_)
+               return memcmp(a_, b_, nl_data_get_size(a));
+       else
+               return -1;
+}
+
+/** @} */
+/** @} */
diff --git a/lib/defs.h b/lib/defs.h
new file mode 100644 (file)
index 0000000..f079bdf
--- /dev/null
@@ -0,0 +1,29 @@
+/* lib/defs.h.  Generated by configure.  */
+/* lib/defs.h.in.  Generated from configure.in by autoheader.  */
+
+/* Define to the address where bug reports for this package should be sent. */
+#define PACKAGE_BUGREPORT "tgraf@suug.ch"
+
+/* Define to the full name of this package. */
+#define PACKAGE_NAME "libnl"
+
+/* Define to the full name and version of this package. */
+#define PACKAGE_STRING "libnl 1.0-pre6"
+
+/* Define to the one symbol short name of this package. */
+#define PACKAGE_TARNAME "libnl"
+
+/* Define to the version of this package. */
+#define PACKAGE_VERSION "1.0-pre6"
+
+/* verbose errors */
+/* #undef VERBOSE_ERRORS */
+
+/* Define to empty if `const' does not conform to ANSI C. */
+/* #undef const */
+
+/* Define to `__inline__' or `__inline' if that's what the C compiler
+   calls it, or to nothing if 'inline' is not supported under any name.  */
+#ifndef __cplusplus
+/* #undef inline */
+#endif
diff --git a/lib/defs.h.in b/lib/defs.h.in
new file mode 100644 (file)
index 0000000..ef86caa
--- /dev/null
@@ -0,0 +1,28 @@
+/* lib/defs.h.in.  Generated from configure.in by autoheader.  */
+
+/* Define to the address where bug reports for this package should be sent. */
+#undef PACKAGE_BUGREPORT
+
+/* Define to the full name of this package. */
+#undef PACKAGE_NAME
+
+/* Define to the full name and version of this package. */
+#undef PACKAGE_STRING
+
+/* Define to the one symbol short name of this package. */
+#undef PACKAGE_TARNAME
+
+/* Define to the version of this package. */
+#undef PACKAGE_VERSION
+
+/* verbose errors */
+#undef VERBOSE_ERRORS
+
+/* Define to empty if `const' does not conform to ANSI C. */
+#undef const
+
+/* Define to `__inline__' or `__inline' if that's what the C compiler
+   calls it, or to nothing if 'inline' is not supported under any name.  */
+#ifndef __cplusplus
+#undef inline
+#endif
diff --git a/lib/fib_lookup/lookup.c b/lib/fib_lookup/lookup.c
new file mode 100644 (file)
index 0000000..6306fb4
--- /dev/null
@@ -0,0 +1,355 @@
+/*
+ * lib/fib_lookup/lookup.c     FIB Lookup
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @defgroup fib_lookup FIB Lookup
+ * @brief
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink/netlink.h>
+#include <netlink/attr.h>
+#include <netlink/utils.h>
+#include <netlink/object.h>
+#include <netlink/route/rtnl.h>
+#include <netlink/route/route.h>
+#include <netlink/fib_lookup/request.h>
+#include <netlink/fib_lookup/lookup.h>
+
+/** @cond SKIP */
+static struct nl_cache_ops fib_lookup_ops;
+
+/* not exported so far */
+struct fib_result_nl {
+       uint32_t        fl_addr;   /* To be looked up*/ 
+       uint32_t        fl_fwmark; 
+       unsigned char   fl_tos;
+       unsigned char   fl_scope;
+       unsigned char   tb_id_in;
+
+       unsigned char   tb_id;      /* Results */
+       unsigned char   prefixlen;
+       unsigned char   nh_sel;
+       unsigned char   type;
+       unsigned char   scope;
+       int             err;      
+};
+/** @endcond */
+
+static void result_free_data(struct nl_object *obj)
+{
+       struct flnl_result *res = nl_object_priv(obj);
+
+       if (res && res->fr_req)
+               flnl_request_put(res->fr_req);
+}
+
+static int result_msg_parser(struct sockaddr_nl *who, struct nlmsghdr *n,
+                            void *arg)
+{
+       struct flnl_result *res;
+       struct fib_result_nl *fr;
+       struct nl_parser_param *pp = arg;
+       struct nl_addr *addr;
+       int err = -EINVAL;
+
+       res = flnl_result_alloc();
+       if (!res)
+               goto errout;
+
+       res->ce_msgtype = n->nlmsg_type;
+
+       res->fr_req = flnl_request_alloc();
+       if (!res->fr_req)
+               goto errout;
+
+       fr = nlmsg_data(n);
+       addr = nl_addr_build(AF_INET, &fr->fl_addr, 4);
+       if (!addr)
+               goto errout;
+       err = flnl_request_set_addr(res->fr_req, addr);
+       nl_addr_put(addr);
+       if (err < 0)
+               goto errout;
+
+       flnl_request_set_fwmark(res->fr_req, fr->fl_fwmark);
+       flnl_request_set_tos(res->fr_req, fr->fl_tos);
+       flnl_request_set_scope(res->fr_req, fr->fl_scope);
+       flnl_request_set_table(res->fr_req, fr->tb_id_in);
+
+       res->fr_table_id = fr->tb_id;
+       res->fr_prefixlen = fr->prefixlen;
+       res->fr_nh_sel = fr->nh_sel;
+       res->fr_type = fr->type;
+       res->fr_scope = fr->scope;
+       res->fr_error = fr->err;
+
+       err = pp->pp_cb((struct nl_object *) res, pp);
+       if (err < 0)
+               goto errout;
+
+       /* REAL HACK, fib_lookup doesn't support ACK nor does it
+        * send a DONE message, enforce end of message stream
+        * after just the first message */
+       return NL_EXIT;
+
+errout:
+       flnl_result_put(res);
+       return err;
+}
+
+static int result_dump_brief(struct nl_object *obj, struct nl_dump_params *p)
+{
+       struct flnl_result *res = (struct flnl_result *) obj;
+       char buf[128];
+       int line = 1;
+
+       dp_dump(p, "table %s prefixlen %u next-hop-selector %u\n",
+               rtnl_route_table2str(res->fr_table_id, buf, sizeof(buf)),
+               res->fr_prefixlen, res->fr_nh_sel);
+       dp_dump_line(p, line++, "type %s ",
+                    nl_rtntype2str(res->fr_type, buf, sizeof(buf)));
+       dp_dump(p, "scope %s error %s (%d)\n",
+               rtnl_scope2str(res->fr_scope, buf, sizeof(buf)),
+               strerror(-res->fr_error), res->fr_error);
+
+       return line;
+}
+
+static int result_dump_full(struct nl_object *obj, struct nl_dump_params *p)
+{
+       return result_dump_brief(obj, p);
+}
+
+static int result_filter(struct nl_object *obj, struct nl_object *filter)
+{
+       struct flnl_result *o = (struct flnl_result *) obj;
+       struct flnl_result *f = (struct flnl_result *) filter;
+
+       if (o->fr_req && f->fr_req)
+               return flnl_request_cmp(o->fr_req, f->fr_req);
+
+       return 1;
+}
+
+/**
+ * @name FIB Result Allocation/Freeage
+ * @{
+ */
+
+/**
+ * Allocate and initialize new lookup result object.
+ * @note Free the memory after usage using flnl_result_put() or
+ *       flnl_result_free().
+ * @return Newly allocated lookup result object or NULL if an error occured.
+ */
+struct flnl_result *flnl_result_alloc(void)
+{
+       return (struct flnl_result *) nl_object_alloc_from_ops(&fib_lookup_ops);
+}
+
+/**
+ * Allocate lookup result cache.
+ *
+ * Allocates a new lookup result cache and initializes it properly.
+ *
+ * @note Free the memory after usage using nl_cache_destroy_and_free().
+ * @return Newly allocated cache or NULL if an error occured.
+ */
+struct nl_cache *flnl_result_alloc_cache(void)
+{
+       return nl_cache_alloc_from_ops(&fib_lookup_ops);
+}
+
+/**
+ * Give back reference on lookup result object.
+ * @arg res            lookup result object to be given back.
+ *
+ * Decrements the reference counter and frees the object if the
+ * last reference has been released.
+ */
+void flnl_result_put(struct flnl_result *res)
+{
+       nl_object_put((struct nl_object *) res);
+}
+
+/**
+ * Free lookup result object.
+ * @arg res            lookup result object to be freed.
+ *
+ * @note Always use flnl_result_put() unless you're absolutely sure
+ *       that no other user may have a reference on this object.
+ */
+void flnl_result_free(struct flnl_result *res)
+{
+       nl_object_free((struct nl_object *) res);
+}
+
+/** @} */
+
+/**
+ * @name Lookup
+ * @{
+ */
+
+/**
+ * Builds a netlink request message to do a lookup
+ * @arg req            Requested match.
+ * @arg flags          additional netlink message flags
+ *
+ * Builds a new netlink message requesting a change of link attributes.
+ * The netlink message header isn't fully equipped with all relevant
+ * fields and must be sent out via nl_send_auto_complete() or
+ * supplemented as needed.
+ * \a old must point to a link currently configured in the kernel
+ * and \a tmpl must contain the attributes to be changed set via
+ * \c rtnl_link_set_* functions.
+ *
+ * @return New netlink message
+ * @note Not all attributes can be changed, see
+ *       \ref link_changeable "Changeable Attributes" for more details.
+ */
+struct nl_msg *flnl_lookup_build_request(struct flnl_request *req, int flags)
+{
+       struct nl_msg *msg;
+       struct nl_addr *addr;
+       uint64_t fwmark;
+       int tos, scope, table;
+       struct fib_result_nl fr = {0};
+
+       fwmark = flnl_request_get_fwmark(req);
+       tos = flnl_request_get_tos(req);
+       scope = flnl_request_get_scope(req);
+       table = flnl_request_get_table(req);
+
+       fr.fl_fwmark = fwmark != UINT_LEAST64_MAX ? fwmark : 0;
+       fr.fl_tos = tos >= 0 ? tos : 0;
+       fr.fl_scope = scope >= 0 ? scope : RT_SCOPE_UNIVERSE;
+       fr.tb_id_in = table >= 0 ? table : RT_TABLE_UNSPEC;
+
+       addr = flnl_request_get_addr(req);
+       if (!addr) {
+               nl_error(EINVAL, "Request must specify the address");
+               return NULL;
+       }
+
+       fr.fl_addr = *(uint32_t *) nl_addr_get_binary_addr(addr);
+
+       msg = nlmsg_build_simple(0, flags);
+       if (!msg)
+               goto errout;
+
+       if (nlmsg_append(msg, &fr, sizeof(fr), 1) < 0)
+               goto errout;
+
+       return msg;
+
+errout:
+       nlmsg_free(msg);
+       return NULL;
+}
+
+/**
+ * Perform FIB Lookup
+ * @arg handle         Netlink handle.
+ * @arg req            Lookup request object.
+ * @arg cache          Cache for result.
+ *
+ * Builds a netlink message to request a FIB lookup, waits for the
+ * reply and adds the result to the specified cache.
+ *
+ * @return 0 on success or a negative error code.
+ */
+int flnl_lookup(struct nl_handle *handle, struct flnl_request *req,
+               struct nl_cache *cache)
+{
+       struct nl_msg *msg;
+       int err;
+
+       msg = flnl_lookup_build_request(req, 0);
+       if (!msg)
+               return nl_errno(ENOMEM);
+
+       err = nl_send_auto_complete(handle, msg);
+       nlmsg_free(msg);
+       if (err < 0)
+               return err;
+
+       return nl_cache_pickup(handle, cache);
+}
+
+/** @} */
+
+/**
+ * @name Attribute Access
+ * @{
+ */
+
+int flnl_result_get_table_id(struct flnl_result *res)
+{
+       return res->fr_table_id;
+}
+
+int flnl_result_get_prefixlen(struct flnl_result *res)
+{
+       return res->fr_prefixlen;
+}
+
+int flnl_result_get_nexthop_sel(struct flnl_result *res)
+{
+       return res->fr_nh_sel;
+}
+
+int flnl_result_get_type(struct flnl_result *res)
+{
+       return res->fr_type;
+}
+
+int flnl_result_get_scope(struct flnl_result *res)
+{
+       return res->fr_scope;
+}
+
+int flnl_result_get_error(struct flnl_result *res)
+{
+       return res->fr_error;
+}
+
+/** @} */
+
+static struct nl_cache_ops fib_lookup_ops = {
+       .co_name                = "fib_lookup/fib_lookup",
+       .co_size                = sizeof(struct flnl_result),
+       .co_hdrsize             = sizeof(struct fib_result_nl),
+       .co_msgtypes            = {
+                                       { 0, "any" },
+                                       { -1, NULL },
+                                 },
+       .co_protocol            = NETLINK_FIB_LOOKUP,
+       .co_msg_parser          = result_msg_parser,
+       .co_free_data           = result_free_data,
+       .co_dump[NL_DUMP_BRIEF] = result_dump_brief,
+       .co_dump[NL_DUMP_FULL]  = result_dump_full,
+       .co_filter              = result_filter,
+};
+
+static void __init fib_lookup_init(void)
+{
+       nl_cache_mngt_register(&fib_lookup_ops);
+}
+
+static void __exit fib_lookup_exit(void)
+{
+       nl_cache_mngt_unregister(&fib_lookup_ops);
+}
+
+/** @} */
diff --git a/lib/fib_lookup/request.c b/lib/fib_lookup/request.c
new file mode 100644 (file)
index 0000000..1986063
--- /dev/null
@@ -0,0 +1,277 @@
+/*
+ * lib/fib_lookup/request.c    FIB Lookup Request
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup fib_lookup
+ * @defgroup flreq Request
+ * @brief
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink/netlink.h>
+#include <netlink/attr.h>
+#include <netlink/utils.h>
+#include <netlink/object.h>
+#include <netlink/fib_lookup/request.h>
+
+/** @cond SKIP */
+#define REQUEST_ATTR_ADDR      0x01
+#define REQUEST_ATTR_FWMARK    0x02
+#define REQUEST_ATTR_TOS       0x04
+#define REQUEST_ATTR_SCOPE     0x08
+#define REQUEST_ATTR_TABLE     0x10
+/** @endcond */
+
+/**
+ * @name Lookup Request Creation/Deletion
+ * @{
+ */
+
+/**
+ * Allocate and initialize new lookup request object.
+ * @note Free the memory after usage using flnl_request_put() or
+ *       flnl_request_free().
+ * @return Newly allocated lookup request object or NULL if an error occured.
+ */
+struct flnl_request *flnl_request_alloc(void)
+{
+       struct flnl_request *req;
+
+       req = calloc(1, sizeof(*req));
+       if (req)
+               req->lr_refcnt = 1;
+       else
+               nl_errno(ENOMEM);
+
+       return req;
+}
+
+/**
+ * Request undestroyable reference of lookup request object.
+ * @arg req            Lookup request object.
+ * @return Lookup request object of which the reference was given.
+ */
+struct flnl_request *flnl_request_get(struct flnl_request *req)
+{
+       req->lr_refcnt++;
+
+       return req;
+}
+
+/**
+ * Give back reference of lookup request object.
+ * @arg req            Lookup request object to be given back.
+ * 
+ * Decrements the reference counter and destroys the object if the
+ * last reference was given back.
+ */
+void flnl_request_put(struct flnl_request *req)
+{
+       if (!req)
+               return;
+
+       if (req->lr_refcnt <= 1)
+               flnl_request_free(req);
+       else
+               req->lr_refcnt--;
+}
+
+/**
+ * Free lookup request object.
+ * @arg req            Lookup request object to be freed.
+ */
+void flnl_request_free(struct flnl_request *req)
+{
+       if (!req)
+               return;
+
+       if (req->lr_refcnt != 1)
+               BUG();
+
+       free(req);
+}
+
+/** @} */
+
+/**
+ * @name Attribute Access
+ * @{
+ */
+
+/**
+ * Set firewall mark of lookup request object.
+ * @arg req            Lookup request object.
+ * @arg fwmark         Firewall mark.
+ */
+void flnl_request_set_fwmark(struct flnl_request *req, uint64_t fwmark)
+{
+       req->lr_fwmark = fwmark;
+       req->lr_mask |= REQUEST_ATTR_FWMARK;
+}
+
+/**
+ * Get firewall mark of lookup request object.
+ * @arg req            Lookup request object.
+ * @return Firewall mark or UINT_LEAST64_MAX if not available.
+ */
+uint64_t flnl_request_get_fwmark(struct flnl_request *req)
+{
+       if (req->lr_mask & REQUEST_ATTR_FWMARK)
+               return req->lr_fwmark;
+       else
+               return UINT_LEAST64_MAX;
+}
+
+/**
+ * Set Type of Service of lookup request object.
+ * @arg req            Lookup request object.
+ * @arg tos            Type of Service.
+ */
+void flnl_request_set_tos(struct flnl_request *req, int tos)
+{
+       req->lr_tos = tos;
+       req->lr_mask |= REQUEST_ATTR_TOS;
+}
+
+/**
+ * Get Type of Service of lookup request object.
+ * @arg req            Lookup request object.
+ * @return Type of Service or -1 if not available.
+ */
+int flnl_request_get_tos(struct flnl_request *req)
+{
+       if (req->lr_mask & REQUEST_ATTR_TOS)
+               return req->lr_tos;
+       else
+               return -1;
+}
+
+/**
+ * Set Scope of lookup request object.
+ * @arg req            Lookup request oject.
+ * @arg scope          Routing scope.
+ */
+void flnl_request_set_scope(struct flnl_request *req, int scope)
+{
+       req->lr_scope = scope;
+       req->lr_mask |= REQUEST_ATTR_SCOPE;
+}
+
+/**
+ * Get scope of lookup request object.
+ * @arg req            Lookup request object.
+ * @return Scope or -1 if not available.
+ */
+int flnl_request_get_scope(struct flnl_request *req)
+{
+       if (req->lr_mask & REQUEST_ATTR_SCOPE)
+               return req->lr_scope;
+       else
+               return -1;
+}
+
+/**
+ * Set routing table of lookup request object.
+ * @arg req            Lookup request object.
+ * @arg table          Routing table.
+ */
+void flnl_request_set_table(struct flnl_request *req, int table)
+{
+       req->lr_table = table;
+       req->lr_mask |= REQUEST_ATTR_TABLE;
+}
+
+/**
+ * Get routing table of lookup request object.
+ * @arg req            Lookup request object.
+ * @return Routing table or -1 if not available.
+ */
+int flnl_request_get_table(struct flnl_request *req)
+{
+       if (req->lr_mask & REQUEST_ATTR_TABLE)
+               return req->lr_table;
+       else
+               return -1;
+}
+
+/**
+ * Set destination address of lookup request object.
+ * @arg req            Lookup request object.
+ * @arg addr           IPv4 destination address.
+ */
+int flnl_request_set_addr(struct flnl_request *req, struct nl_addr *addr)
+{
+       if (addr->a_family != AF_INET)
+               return nl_error(EINVAL, "Address must be an IPv4 address");
+
+       if (req->lr_addr)
+               nl_addr_put(req->lr_addr);
+
+       nl_addr_get(addr);
+       req->lr_addr = addr;
+
+       req->lr_mask |= REQUEST_ATTR_ADDR;
+
+       return 0;
+}
+
+/**
+ * Get destination address of lookup request object.
+ * @arg req            Lookup request object.
+ * @return Destination address or NULL if not available.
+ */
+struct nl_addr *flnl_request_get_addr(struct flnl_request *req)
+{
+       if (req->lr_mask & REQUEST_ATTR_ADDR)
+               return req->lr_addr;
+       else
+               return NULL;
+}
+
+/** @} */
+
+/**
+ * @name Miscellaneous
+ * @{
+ */
+
+/**
+ * Compares two lookup request objects.
+ * @arg a              Lookup request object.
+ * @arg b              Another lookup request object.
+ *
+ * @return Integer less than, equal to or greather than zero if \c is found,
+ *         respectively to be less than, to, or be greater than \c b.
+ */
+int flnl_request_cmp(struct flnl_request *a, struct flnl_request *b)
+{
+#define REQ(F) (a->lr_mask & REQUEST_ATTR_##F)
+#define AVAIL(F) (b->lr_mask & REQUEST_ATTR_##F)
+#define F_CUS(F, EXPR) (REQ(F) && (!AVAIL(F) || (EXPR)))
+#define F_INT(F, N) (REQ(F) && (!AVAIL(F) || (a->N != b->N)))
+
+       if (F_INT(FWMARK,       lr_fwmark)                              ||
+           F_INT(TOS,          lr_tos)                                 ||
+           F_INT(SCOPE,        lr_scope)                               ||
+           F_INT(TABLE,        lr_table)                               ||
+           F_CUS(ADDR,         nl_addr_cmp(a->lr_addr, b->lr_addr)))
+               return 0;
+#undef REQ
+#undef AVAIL
+#undef F_CUS
+#undef F_INT
+       return 1;
+}
+
+/** @} */
+
+/** @} */
diff --git a/lib/handlers.c b/lib/handlers.c
new file mode 100644 (file)
index 0000000..c433230
--- /dev/null
@@ -0,0 +1,581 @@
+/*
+ * lib/handlers.c      default netlink message handlers
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup nl
+ * @defgroup cb Callbacks/Customization
+ * 
+ * Customization via callbacks.
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink/netlink.h>
+#include <netlink/utils.h>
+#include <netlink/msg.h>
+#include <netlink/handlers.h>
+
+static void print_header_content(FILE *ofd, struct nlmsghdr *n)
+{
+       char flags[128];
+       char type[32];
+       
+       fprintf(ofd, "type=%s length=%u flags=<%s> sequence-nr=%u pid=%u",
+               nl_nlmsgtype2str(n->nlmsg_type, type, sizeof(type)),
+               n->nlmsg_len, nl_nlmsg_flags2str(n->nlmsg_flags, flags,
+               sizeof(flags)), n->nlmsg_seq, n->nlmsg_pid);
+}
+
+static inline void dump_hex(FILE *ofd, char *start, int len)
+{
+       int i, a, c, limit;
+       char ascii[21] = {0};
+
+       limit = 18;
+       fprintf(ofd, "    ");
+
+       for (i = 0, a = 0, c = 0; i < len; i++) {
+               int v = *(uint8_t *) (start + i);
+
+               fprintf(ofd, "%02x ", v);
+               ascii[a++] = isprint(v) ? v : '.';
+
+               if (c == limit-1) {
+                       fprintf(ofd, "%s\n", ascii);
+                       if (i < (len - 1))
+                               fprintf(ofd, "    ");
+                       a = c = 0;
+                       memset(ascii, 0, sizeof(ascii));
+               } else
+                       c++;
+       }
+
+       if (c != 0) {
+               for (i = 0; i < (limit - c); i++)
+                       fprintf(ofd, "   ");
+               fprintf(ofd, "%s\n", ascii);
+       }
+}
+
+static void print_hdr(FILE *ofd, struct nl_msg *msg)
+{
+       struct nlmsghdr *nlh = nlmsg_hdr(msg);
+       struct nl_cache_ops *ops;
+       char buf[128];
+
+       fprintf(ofd, "    .nlmsg_len = %d\n", nlh->nlmsg_len);
+
+       ops = nl_cache_mngt_associate(nlmsg_get_proto(msg), nlh->nlmsg_type);
+
+       fprintf(ofd, "    .nlmsg_type = %d <%s>\n", nlh->nlmsg_type,
+               ops ? nl_cache_mngt_type2name(ops, nlh->nlmsg_type,
+                                             buf, sizeof(buf))
+               : nl_nlmsgtype2str(nlh->nlmsg_type, buf, sizeof(buf)));
+       fprintf(ofd, "    .nlmsg_flags = %d <%s>\n", nlh->nlmsg_flags,
+               nl_nlmsg_flags2str(nlh->nlmsg_flags, buf, sizeof(buf)));
+       fprintf(ofd, "    .nlmsg_seq = %d\n", nlh->nlmsg_seq);
+       fprintf(ofd, "    .nlmsg_pid = %d\n", nlh->nlmsg_pid);
+
+}
+
+static void raw_dump_msg(FILE *ofd, struct nl_msg *msg)
+{
+       struct nlmsghdr *hdr = nlmsg_hdr(msg);
+       
+       fprintf(ofd, 
+       "--------------------------   BEGIN NETLINK MESSAGE "
+       "---------------------------\n");
+
+       fprintf(ofd, "  [HEADER] %Zu octets\n", sizeof(struct nlmsghdr));
+       print_hdr(ofd, msg);
+
+       if (hdr->nlmsg_type == NLMSG_ERROR &&
+           hdr->nlmsg_len >= nlmsg_msg_size(sizeof(struct nlmsgerr))) {
+               struct nl_msg *errmsg;
+               struct nlmsgerr *err = nlmsg_data(hdr);
+
+               fprintf(ofd, "  [ERRORMSG] %Zu octets\n", sizeof(*err));
+               fprintf(ofd, "    .error = %d \"%s\"\n", err->error,
+                       strerror(-err->error));
+               fprintf(ofd, "  [ORIGINAL MESSAGE] %Zu octets\n", sizeof(*hdr));
+
+               errmsg = nlmsg_build(&err->msg);
+               print_hdr(ofd, errmsg);
+               nlmsg_free(msg);
+       } else if (nlmsg_len(hdr) > 0) {
+               struct nl_cache_ops *ops;
+               int payloadlen = nlmsg_len(hdr);
+               int attrlen = 0;
+
+               ops = nl_cache_mngt_associate(nlmsg_get_proto(msg),
+                                             hdr->nlmsg_type);
+               if (ops) {
+                       attrlen = nlmsg_attrlen(hdr, ops->co_hdrsize);
+                       payloadlen -= attrlen;
+               }
+
+               fprintf(ofd, "  [PAYLOAD] %d octets\n", payloadlen);
+               dump_hex(ofd, nlmsg_data(hdr), payloadlen);
+
+               if (attrlen) {
+                       int rem, padlen;
+                       struct nlattr *nla;
+       
+                       nlmsg_for_each_attr(nla, hdr, ops->co_hdrsize, rem) {
+                               int alen = nla_len(nla);
+
+                               fprintf(ofd, "  [ATTR %02d] %d octets\n",
+                                       nla->nla_type, alen);
+                               dump_hex(ofd, nla_data(nla), alen);
+
+                               padlen = nla_padlen(alen);
+                               if (padlen > 0) {
+                                       fprintf(ofd, "  [PADDING] %d octets\n",
+                                               padlen);
+                                       dump_hex(ofd, nla_data(nla) + alen,
+                                                padlen);
+                               }
+                       }
+               }
+       }
+
+       fprintf(ofd, 
+       "---------------------------  END NETLINK MESSAGE   "
+       "---------------------------\n");
+}
+
+static int nl_valid_handler_default(struct nl_msg *msg, void *arg)
+{
+       return NL_PROCEED;
+}
+
+static int nl_finish_handler_default(struct nl_msg *msg, void *arg)
+{
+       return NL_EXIT;
+}
+
+static int nl_invalid_handler_default(struct nl_msg *msg, void *arg)
+{
+       return NL_EXIT;
+}
+
+static int nl_msg_in_handler_default(struct nl_msg *msg, void *arg)
+{
+       return NL_PROCEED;
+}
+
+static int nl_msg_out_handler_default(struct nl_msg *msg, void *arg)
+{
+       return NL_PROCEED;
+}
+
+static int nl_overrun_handler_default(struct nl_msg *msg, void *arg)
+{
+       return NL_EXIT;
+}
+
+static int nl_skipped_handler_default(struct nl_msg *msg, void *arg)
+{
+       return NL_SKIP;
+}
+
+static int nl_ack_handler_default(struct nl_msg *msg, void *arg)
+{
+       return NL_EXIT;
+}
+
+static int nl_error_handler_default(struct sockaddr_nl *who,
+                                   struct nlmsgerr *e, void *arg)
+{
+       return NL_EXIT;
+}
+
+static int nl_valid_handler_verbose(struct nl_msg *msg, void *arg)
+{
+       FILE *ofd = arg ? arg : stdout;
+
+       fprintf(ofd, "-- Warning: unhandled valid message: ");
+       print_header_content(ofd, nlmsg_hdr(msg));
+       fprintf(ofd, "\n");
+
+       return NL_PROCEED;
+}
+
+static int nl_finish_handler_verbose(struct nl_msg *msg, void *arg)
+{
+       return NL_EXIT;
+}
+
+static int nl_msg_in_handler_verbose(struct nl_msg *msg, void *arg)
+{
+       return NL_PROCEED;
+}
+
+static int nl_invalid_handler_verbose(struct nl_msg *msg, void *arg)
+{
+       FILE *ofd = arg ? arg : stderr;
+
+       fprintf(ofd, "-- Error: Invalid message: ");
+       print_header_content(ofd, nlmsg_hdr(msg));
+       fprintf(ofd, "\n");
+
+       return NL_EXIT;
+}
+
+static int nl_msg_out_handler_verbose(struct nl_msg *msg, void *arg)
+{
+       return NL_PROCEED;
+}
+
+static int nl_overrun_handler_verbose(struct nl_msg *msg, void *arg)
+{
+       FILE *ofd = arg ? arg : stderr;
+
+       fprintf(ofd, "-- Error: Netlink Overrun: ");
+       print_header_content(ofd, nlmsg_hdr(msg));
+       fprintf(ofd, "\n");
+       
+       return NL_EXIT;
+}
+
+static int nl_ack_handler_verbose(struct nl_msg *msg, void *arg)
+{
+       return NL_EXIT;
+}
+
+static int nl_skipped_handler_verbose(struct nl_msg *msg, void *arg)
+{
+       return NL_SKIP;
+}
+
+static int nl_error_handler_verbose(struct sockaddr_nl *who,
+                                   struct nlmsgerr *e, void *arg)
+{
+       FILE *ofd = arg ? arg : stderr;
+
+       fprintf(ofd, "-- Error received: %s\n-- Original message: ",
+               strerror(-e->error));
+       print_header_content(ofd, &e->msg);
+       fprintf(ofd, "\n");
+
+       return NL_EXIT;
+}
+
+static int nl_valid_handler_debug(struct nl_msg *msg, void *arg)
+{
+       FILE *ofd = arg ? arg : stderr;
+
+       fprintf(ofd, "-- Debug: Valid message: ");
+       print_header_content(ofd, nlmsg_hdr(msg));
+       fprintf(ofd, "\n");
+
+       return NL_PROCEED;
+}
+
+static int nl_finish_handler_debug(struct nl_msg *msg, void *arg)
+{
+       FILE *ofd = arg ? arg : stderr;
+
+       fprintf(ofd, "-- Debug: End of multipart message block: ");
+       print_header_content(ofd, nlmsg_hdr(msg));
+       fprintf(ofd, "\n");
+       
+       return NL_EXIT;
+}
+
+static int nl_invalid_handler_debug(struct nl_msg *msg, void *arg)
+{
+       return nl_invalid_handler_verbose(msg, arg);
+}
+
+static int nl_msg_in_handler_debug(struct nl_msg *msg, void *arg)
+{
+       FILE *ofd = arg ? arg : stderr;
+
+       fprintf(ofd, "-- Debug: Received Message:\n");
+       raw_dump_msg(ofd, msg);
+       
+       return NL_PROCEED;
+}
+
+static int nl_msg_out_handler_debug(struct nl_msg *msg, void *arg)
+{
+       FILE *ofd = arg ? arg : stderr;
+
+       fprintf(ofd, "-- Debug: Sent Message:\n");
+       raw_dump_msg(ofd, msg);
+
+       return NL_PROCEED;
+}
+
+static int nl_overrun_handler_debug(struct nl_msg *msg, void *arg)
+{
+       return nl_overrun_handler_verbose(msg, arg);
+}
+
+static int nl_skipped_handler_debug(struct nl_msg *msg, void *arg)
+{
+       FILE *ofd = arg ? arg : stderr;
+
+       fprintf(ofd, "-- Debug: Skipped message: ");
+       print_header_content(ofd, nlmsg_hdr(msg));
+       fprintf(ofd, "\n");
+
+       return NL_SKIP;
+}
+
+static int nl_ack_handler_debug(struct nl_msg *msg, void *arg)
+{
+       FILE *ofd = arg ? arg : stderr;
+
+       fprintf(ofd, "-- Debug: ACK: ");
+       print_header_content(ofd, nlmsg_hdr(msg));
+       fprintf(ofd, "\n");
+
+       return NL_EXIT;
+}
+
+static int nl_error_handler_debug(struct sockaddr_nl *who,
+                                 struct nlmsgerr *e, void *arg)
+{
+       return nl_error_handler_verbose(who, e, arg);
+}
+
+static nl_recvmsg_msg_cb_t cb_def[NL_CB_TYPE_MAX+1][NL_CB_KIND_MAX+1] = {
+       [NL_CB_VALID] = {
+               [NL_CB_DEFAULT] = nl_valid_handler_default,
+               [NL_CB_VERBOSE] = nl_valid_handler_verbose,
+               [NL_CB_DEBUG]   = nl_valid_handler_debug,
+       },
+       [NL_CB_FINISH] = {
+               [NL_CB_DEFAULT] = nl_finish_handler_default,
+               [NL_CB_VERBOSE] = nl_finish_handler_verbose,
+               [NL_CB_DEBUG]   = nl_finish_handler_debug,
+       },
+       [NL_CB_INVALID] = {
+               [NL_CB_DEFAULT] = nl_invalid_handler_default,
+               [NL_CB_VERBOSE] = nl_invalid_handler_verbose,
+               [NL_CB_DEBUG]   = nl_invalid_handler_debug,
+       },
+       [NL_CB_MSG_IN] = {
+               [NL_CB_DEFAULT] = nl_msg_in_handler_default,
+               [NL_CB_VERBOSE] = nl_msg_in_handler_verbose,
+               [NL_CB_DEBUG]   = nl_msg_in_handler_debug,
+       },
+       [NL_CB_MSG_OUT] = {
+               [NL_CB_DEFAULT] = nl_msg_out_handler_default,
+               [NL_CB_VERBOSE] = nl_msg_out_handler_verbose,
+               [NL_CB_DEBUG]   = nl_msg_out_handler_debug,
+       },
+       [NL_CB_OVERRUN] = {
+               [NL_CB_DEFAULT] = nl_overrun_handler_default,
+               [NL_CB_VERBOSE] = nl_overrun_handler_verbose,
+               [NL_CB_DEBUG]   = nl_overrun_handler_debug,
+       },
+       [NL_CB_SKIPPED] = {
+               [NL_CB_DEFAULT] = nl_skipped_handler_default,
+               [NL_CB_VERBOSE] = nl_skipped_handler_verbose,
+               [NL_CB_DEBUG]   = nl_skipped_handler_debug,
+       },
+       [NL_CB_ACK] = {
+               [NL_CB_DEFAULT] = nl_ack_handler_default,
+               [NL_CB_VERBOSE] = nl_ack_handler_verbose,
+               [NL_CB_DEBUG]   = nl_ack_handler_debug,
+       },
+};
+
+static nl_recvmsg_err_cb_t cb_err_def[NL_CB_KIND_MAX+1] = {
+       [NL_CB_DEFAULT] = nl_error_handler_default,
+       [NL_CB_VERBOSE] = nl_error_handler_verbose,
+       [NL_CB_DEBUG]   = nl_error_handler_debug,
+};
+
+/**
+ * @name Callback Handle Management
+ * @{
+ */
+
+/**
+ * Allocate a new callback handle
+ * @arg kind           callback kind to be used for initialization
+ * @return Newly allocated callback handle or NULL
+ */
+struct nl_cb *nl_cb_new(enum nl_cb_kind kind)
+{
+       int i;
+       struct nl_cb *cb;
+
+       if (kind < 0 || kind > NL_CB_KIND_MAX)
+               return NULL;
+
+       cb = calloc(1, sizeof(*cb));
+       if (!cb) {
+               nl_errno(ENOMEM);
+               return NULL;
+       }
+
+       for (i = 0; i <= NL_CB_TYPE_MAX; i++)
+               nl_cb_set(cb, i, kind, NULL, NULL);
+
+       nl_cb_err(cb, kind, NULL, NULL);
+
+       return cb;
+}
+
+/**
+ * Destroy a callback handle
+ * @arg cb             callback handle
+ */
+void nl_cb_destroy(struct nl_cb *cb)
+{
+       free(cb);
+}
+
+/**
+ * Clone an existing callback handle
+ * @arg orig           original callback handle
+ * @return Newly allocated callback handle being a duplicate of
+ *         orig or NULL
+ */
+struct nl_cb *nl_cb_clone(struct nl_cb *orig)
+{
+       struct nl_cb *cb;
+       
+       cb = nl_cb_new(NL_CB_DEFAULT);
+       if (!cb)
+               return NULL;
+
+       memcpy(cb, orig, sizeof(*orig));
+
+       return cb;
+}
+
+/** @} */
+
+/**
+ * @name Callback Setup
+ * @{
+ */
+
+/**
+ * Set up a callback 
+ * @arg cb             callback configuration
+ * @arg type           which type callback to set
+ * @arg kind           kind of callback
+ * @arg func           callback function
+ * @arg arg            argument to be passwd to callback function
+ *
+ * @return 0 on success or a negative error code
+ */
+int nl_cb_set(struct nl_cb *cb, enum nl_cb_type type, enum nl_cb_kind kind,
+             nl_recvmsg_msg_cb_t func, void *arg)
+{
+       if (type < 0 || type > NL_CB_TYPE_MAX)
+               return nl_error(ERANGE, "Callback type out of range");
+
+       if (kind < 0 || kind > NL_CB_KIND_MAX)
+               return nl_error(ERANGE, "Callback kind out of range");
+
+       if (kind == NL_CB_CUSTOM) {
+               cb->cb_set[type] = func;
+               cb->cb_args[type] = arg;
+       } else {
+               cb->cb_set[type] = cb_def[type][kind];
+               cb->cb_args[type] = arg;
+       }
+
+       return 0;
+}
+
+/**
+ * Set up a all callbacks
+ * @arg cb             callback configuration
+ * @arg kind           kind of callback
+ * @arg func           callback function
+ * @arg arg            argument to be passwd to callback function
+ *
+ * @return 0 on success or a negative error code
+ */
+int nl_cb_set_all(struct nl_cb *cb, enum nl_cb_kind kind,
+                 nl_recvmsg_msg_cb_t func, void *arg)
+{
+       int i, err;
+
+       for (i = 0; i <= NL_CB_TYPE_MAX; i++) {
+               err = nl_cb_set(cb, i, kind, func, arg);
+               if (err < 0)
+                       return err;
+       }
+
+       return 0;
+}
+
+/**
+ * Set up an error callback
+ * @arg cb             callback configuration
+ * @arg kind           kind of callback
+ * @arg func           callback function
+ * @arg arg            argument to be passed to callback function
+ */
+int nl_cb_err(struct nl_cb *cb, enum nl_cb_kind kind,
+             nl_recvmsg_err_cb_t func, void *arg)
+{
+       if (kind < 0 || kind > NL_CB_KIND_MAX)
+               return nl_error(ERANGE, "Callback kind out of range");
+
+       if (kind == NL_CB_CUSTOM) {
+               cb->cb_err = func;
+               cb->cb_err_arg = arg;
+       } else {
+               cb->cb_err = cb_err_def[kind];
+               cb->cb_err_arg = arg;
+       }
+
+       return 0;
+}
+
+/**
+ * Overwrite internal calls to nl_recvmsgs()
+ * @arg cb             callback configuration
+ * @arg func           replacement callback for nl_recvmsgs()
+ */
+void nl_cb_overwrite_recvmsgs(struct nl_cb *cb,
+                             int (*func)(struct nl_handle *, struct nl_cb *))
+{
+       cb->cb_recvmsgs_ow = func;
+}
+
+/**
+ * Overwrite internal calls to nl_recv()
+ * @arg cb             callback configuration
+ * @arg func           replacement callback for nl_recv()
+ */
+void nl_cb_overwrite_recv(struct nl_cb *cb,
+                         int (*func)(struct nl_handle *, struct sockaddr_nl *,
+                                     unsigned char **, struct ucred **))
+{
+       cb->cb_recv_ow = func;
+}
+
+/**
+ * Overwrite internal calls to nl_send()
+ * @arg cb             callback configuration
+ * @arg func           replacement callback for nl_send()
+ */
+void nl_cb_overwrite_send(struct nl_cb *cb,
+                         int (*func)(struct nl_handle *, struct nl_msg *))
+{
+       cb->cb_send_ow = func;
+}
+
+/** @} */
+
+/** @} */
diff --git a/lib/msg.c b/lib/msg.c
new file mode 100644 (file)
index 0000000..adb552f
--- /dev/null
+++ b/lib/msg.c
@@ -0,0 +1,717 @@
+/*
+ * lib/msg.c           Netlink Messages Interface
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup nl
+ * @defgroup msg Messages
+ * Netlink Message Construction/Parsing Interface
+ * 
+ * The following information is partly extracted from RFC3549
+ * (ftp://ftp.rfc-editor.org/in-notes/rfc3549.txt)
+ *
+ * @par Message Format
+ * Netlink messages consist of a byte stream with one or multiple
+ * Netlink headers and an associated payload.  If the payload is too big
+ * to fit into a single message it, can be split over multiple Netlink
+ * messages, collectively called a multipart message.  For multipart
+ * messages, the first and all following headers have the \c NLM_F_MULTI
+ * Netlink header flag set, except for the last header which has the
+ * Netlink header type \c NLMSG_DONE.
+ *
+ * @par
+ * The Netlink message header (\link nlmsghdr struct nlmsghdr\endlink) is shown below.
+ * @code   
+ * 0                   1                   2                   3
+ * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |                          Length                             |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |            Type              |           Flags              |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |                      Sequence Number                        |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |                      Process ID (PID)                       |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * @endcode
+ *
+ * @par
+ * The netlink message header and payload must be aligned properly:
+ * @code
+ *  <------- NLMSG_ALIGN(hlen) ------> <---- NLMSG_ALIGN(len) --->
+ * +----------------------------+- - -+- - - - - - - - - - -+- - -+
+ * |           Header           | Pad |       Payload       | Pad |
+ * |      struct nlmsghdr       |     |                     |     |
+ * +----------------------------+- - -+- - - - - - - - - - -+- - -+
+ * @endcode
+ * @par
+ * Message Format:
+ * @code
+ *    <--- nlmsg_total_size(payload)  --->
+ *    <-- nlmsg_msg_size(payload) ->
+ *   +----------+- - -+-------------+- - -+-------- - -
+ *   | nlmsghdr | Pad |   Payload   | Pad | nlmsghdr
+ *   +----------+- - -+-------------+- - -+-------- - -
+ *   nlmsg_data(nlh)---^                   ^
+ *   nlmsg_next(nlh)-----------------------+
+ * @endcode
+ * @par
+ * The payload may consist of arbitary data but may have strict
+ * alignment and formatting rules depening on the specific netlink
+ * families.
+ * @par
+ * @code
+ *    <---------------------- nlmsg_len(nlh) --------------------->
+ *    <------ hdrlen ------>       <- nlmsg_attrlen(nlh, hdrlen) ->
+ *   +----------------------+- - -+--------------------------------+
+ *   |     Family Header    | Pad |           Attributes           |
+ *   +----------------------+- - -+--------------------------------+
+ *   nlmsg_attrdata(nlh, hdrlen)---^
+ * @endcode
+ * @par The ACK Netlink Message
+ * This message is actually used to denote both an ACK and a NACK.
+ * Typically, the direction is from FEC to CPC (in response to an ACK
+ * request message).  However, the CPC should be able to send ACKs back
+ * to FEC when requested.
+ * @code
+ *  0                   1                   2                   3
+ *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |                       Netlink message header                  |
+ * |                       type = NLMSG_ERROR                      |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |                          Error code                           |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |                       OLD Netlink message header              |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * @endcode
+ *
+ * @par 1) Creating a new netlink message
+ * @code
+ * // The most common way to start creating a message is by providing an
+ * // defined netlink header to nlmsg_build():
+ * struct nlmsghdr hdr = {
+ *     .nlmsg_type = MY_TYPE,
+ *     .nlmsg_flags = MY_FLAGS,
+ * };
+ * struct nl_msg *msg = nlmsg_build(&hdr);
+ *
+ * // For simple usages where only the message type and flags is of
+ * // interenst a shortcut can be taken:
+ * struct nl_msg *msg = nlmsg_build_simple(MY_TYPE, MY_FLAGS);
+ *
+ * // When using a headerless message for creating nested attributes
+ * // the header is not required and nlmsg_build_no_hdr() may be used:
+ * struct nl_msg *msg = nlmsg_build_no_hdr();
+ *
+ * // The header can later be retrieved with nlmsg_hdr() and changed again:
+ * nlmsg_hdr(msg)->nlmsg_flags |= YET_ANOTHER_FLAG;
+ * @endcode
+ *
+ * @par 2) Appending data to the message
+ * @code
+ * // Payload may be added to the message via nlmsg_append(). The fourth
+ * // parameter specifies whether to pad up to NLMSG_ALIGN to make sure
+ * // that a possible further data block is properly aligned.
+ * nlmsg_append(msg, &mydata, sizeof(mydata), 0);
+ * @endcode
+ *
+ * @par 3) Cleaning up message construction
+ * @code
+ * // After successful use of the message, the memory must be freed
+ * // using nlmsg_free()
+ * nlmsg_free(msg);
+ * @endcode
+ * 
+ * @par Example 2 (Parsing messages):
+ * @code
+ * int n;
+ * unsigned char *buf;
+ * struct nlmsghdr *hdr;
+ *
+ * n = nl_recv(handle, NULL, &buf);
+ * 
+ * hdr = (struct nlmsghdr *) buf;
+ * while (nlmsg_ok(hdr, n)) {
+ *     // Process message here...
+ *     hdr = nlmsg_next(hdr, &n);
+ * }
+ * @endcode
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink/netlink.h>
+#include <netlink/utils.h>
+#include <netlink/cache.h>
+#include <netlink/attr.h>
+#include <linux/socket.h>
+
+/**
+ * @name Size Calculations
+ * @{
+ */
+
+/**
+ * length of netlink message not including padding
+ * @arg payload                length of message payload
+ */
+int nlmsg_msg_size(int payload)
+{
+       return NLMSG_HDRLEN + payload;
+}
+
+/**
+ * length of netlink message including padding
+ * @arg payload                length of message payload
+ */
+int nlmsg_total_size(int payload)
+{
+       return NLMSG_ALIGN(nlmsg_msg_size(payload));
+}
+
+/**
+ * length of padding at the message's tail
+ * @arg payload                length of message payload
+ */
+int nlmsg_padlen(int payload)
+{
+       return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
+}
+
+/** @} */
+
+/**
+ * @name Payload Access
+ * @{
+ */
+
+/**
+ * head of message payload
+ * @arg nlh            netlink messsage header
+ */
+void *nlmsg_data(const struct nlmsghdr *nlh)
+{
+       return (unsigned char *) nlh + NLMSG_HDRLEN;
+}
+
+void *nlmsg_tail(const struct nlmsghdr *nlh)
+{
+       return (unsigned char *) nlh + NLMSG_ALIGN(nlh->nlmsg_len);
+}
+
+/**
+ * length of message payload
+ * @arg nlh            netlink message header
+ */
+int nlmsg_len(const struct nlmsghdr *nlh)
+{
+       return nlh->nlmsg_len - NLMSG_HDRLEN;
+}
+
+/** @} */
+
+/**
+ * @name Attribute Access
+ * @{
+ */
+
+/**
+ * head of attributes data
+ * @arg nlh            netlink message header
+ * @arg hdrlen         length of family specific header
+ */
+struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh, int hdrlen)
+{
+       unsigned char *data = nlmsg_data(nlh);
+       return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
+}
+
+/**
+ * length of attributes data
+ * @arg nlh            netlink message header
+ * @arg hdrlen         length of family specific header
+ */
+int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
+{
+       return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
+}
+
+/** @} */
+
+/**
+ * @name Message Parsing
+ * @{
+ */
+
+/**
+ * check if the netlink message fits into the remaining bytes
+ * @arg nlh            netlink message header
+ * @arg remaining      number of bytes remaining in message stream
+ */
+int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
+{
+       return (remaining >= sizeof(struct nlmsghdr) &&
+               nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
+               nlh->nlmsg_len <= remaining);
+}
+
+/**
+ * next netlink message in message stream
+ * @arg nlh            netlink message header
+ * @arg remaining      number of bytes remaining in message stream
+ *
+ * @returns the next netlink message in the message stream and
+ * decrements remaining by the size of the current message.
+ */
+struct nlmsghdr *nlmsg_next(struct nlmsghdr *nlh, int *remaining)
+{
+       int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
+
+       *remaining -= totlen;
+
+       return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
+}
+
+/**
+ * parse attributes of a netlink message
+ * @arg nlh            netlink message header
+ * @arg hdrlen         length of family specific header
+ * @arg tb             destination array with maxtype+1 elements
+ * @arg maxtype                maximum attribute type to be expected
+ * @arg policy         validation policy
+ *
+ * See nla_parse()
+ */
+int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[],
+               int maxtype, struct nla_policy *policy)
+{
+       if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
+               return nl_errno(EINVAL);
+
+       return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
+                        nlmsg_attrlen(nlh, hdrlen), policy);
+}
+
+/**
+ * nlmsg_find_attr - find a specific attribute in a netlink message
+ * @arg nlh            netlink message header
+ * @arg hdrlen         length of familiy specific header
+ * @arg attrtype       type of attribute to look for
+ *
+ * Returns the first attribute which matches the specified type.
+ */
+struct nlattr *nlmsg_find_attr(struct nlmsghdr *nlh, int hdrlen, int attrtype)
+{
+       return nla_find(nlmsg_attrdata(nlh, hdrlen),
+                       nlmsg_attrlen(nlh, hdrlen), attrtype);
+}
+
+/**
+ * nlmsg_validate - validate a netlink message including attributes
+ * @arg nlh            netlinket message header
+ * @arg hdrlen         length of familiy specific header
+ * @arg maxtype                maximum attribute type to be expected
+ * @arg policy         validation policy
+ */
+int nlmsg_validate(struct nlmsghdr *nlh, int hdrlen, int maxtype,
+                  struct nla_policy *policy)
+{
+       if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
+               return nl_errno(EINVAL);
+
+       return nla_validate(nlmsg_attrdata(nlh, hdrlen),
+                           nlmsg_attrlen(nlh, hdrlen), maxtype, policy);
+}
+
+/** @} */
+
+/**
+ * @name Message Building/Access
+ * @{
+ */
+
+struct nl_msg *nlmsg_new(void)
+{
+       struct nl_msg *nm;
+
+       nm = calloc(1, sizeof(*nm));
+       if (!nm)
+               goto errout;
+
+       nm->nm_nlh = calloc(1, nlmsg_msg_size(0));
+       if (!nm->nm_nlh)
+               goto errout;
+
+       nm->nm_nlh->nlmsg_len = nlmsg_msg_size(0);
+       return nm;
+errout:
+       free(nm);
+       nl_errno(ENOMEM);
+       return NULL;
+}
+
+
+/**
+ * Build a new netlink message
+ * @arg hdr            Netlink message header template
+ *
+ * Builds a new netlink message with a tailroom for the netlink
+ * message header. If \a hdr is not NULL it will be used as a
+ * template for the netlink message header, otherwise the header
+ * is left blank.
+ * 
+ * @return Newly allocated netlink message or NULL
+ */ 
+struct nl_msg *nlmsg_build(struct nlmsghdr *hdr)
+{
+       struct nl_msg *nm;
+
+       nm = nlmsg_new();
+       if (nm && hdr) {
+               int size = nm->nm_nlh->nlmsg_len;
+               memcpy(nm->nm_nlh, hdr, sizeof(*hdr));
+               nm->nm_nlh->nlmsg_len = size;
+       }
+
+       return nm;
+}
+
+struct nl_msg *nlmsg_build_simple(int nlmsgtype, int flags)
+{
+       struct nlmsghdr nlh = {
+               .nlmsg_type = nlmsgtype,
+               .nlmsg_flags = flags,
+       };
+
+       return nlmsg_build(&nlh);
+}
+
+struct nl_msg *nlmsg_build_no_hdr(void)
+{
+       return nlmsg_build(NULL);
+}
+
+struct nl_msg *nlmsg_convert(struct nlmsghdr *hdr)
+{
+       struct nl_msg *nm;
+
+       nm = calloc(1, sizeof(struct nl_msg));
+       if (!nm)
+               goto errout;
+
+       nm->nm_nlh = calloc(1, NLMSG_ALIGN(hdr->nlmsg_len));
+       if (!nm->nm_nlh)
+               goto errout;
+
+       memcpy(nm->nm_nlh, hdr, NLMSG_ALIGN(hdr->nlmsg_len));
+
+       return nm;
+errout:
+       free(nm);
+       nl_errno(ENOMEM);
+       return NULL;
+}
+
+/**
+ * Append raw data to a netlink message
+ * @arg n              netlink message
+ * @arg data           data to add
+ * @arg len            length of data
+ * @arg pad            add padding at the end?
+ *
+ * Extends the netlink message as needed and appends the data of given
+ * length to the message. The length of the message is not aligned to
+ * anything. The caller is responsible to provide a length and
+ * evtentually padded data to fullfil any alignment requirements.
+ *
+ * @return 0 on success or a negative error code
+ * @attention Appending of improperly aligned raw data may result in
+ *            a corrupt message. It is left to you to add the right
+ *            amount of data to have the message aligned to NLMSG_ALIGNTO
+ *            in the end.
+ */
+int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
+{
+       void *tmp;
+
+       if (pad)
+               len = NLMSG_ALIGN(len);
+
+       tmp = realloc(n->nm_nlh, n->nm_nlh->nlmsg_len + len);
+       if (!tmp)
+               return nl_errno(ENOMEM);
+
+       n->nm_nlh = tmp;
+       memcpy((void *) n->nm_nlh + n->nm_nlh->nlmsg_len, data, len);
+       n->nm_nlh->nlmsg_len += len;
+
+       return 0;
+}
+
+
+/**
+ * nlmsg_put - Add a netlink message header
+ * @arg n              netlink message
+ * @arg pid            netlink process id
+ * @arg seq            sequence number of message
+ * @arg type           message type
+ * @arg payload                length of message payload
+ * @arg flags          message flags
+ */
+struct nlmsghdr *nlmsg_put(struct nl_msg *n, uint32_t pid, uint32_t seq,
+                          int type, int payload, int flags)
+{
+       struct nlmsghdr *nlh;
+
+       if (n->nm_nlh->nlmsg_len < NLMSG_HDRLEN)
+               BUG();
+
+       nlh = (struct nlmsghdr *) n->nm_nlh;
+       nlh->nlmsg_type = type;
+       nlh->nlmsg_len = nlmsg_msg_size(payload);
+       nlh->nlmsg_flags = flags;
+       nlh->nlmsg_pid = pid;
+       nlh->nlmsg_seq = seq;
+
+       memset((unsigned char *) nlmsg_data(nlh) + payload, 0,
+              nlmsg_padlen(payload));
+
+       return nlh;
+}
+
+/**
+ * Return actual netlink message
+ * @arg n              netlink message
+ * 
+ * Returns the actual netlink message casted to the type of the netlink
+ * message header.
+ * 
+ * @return A pointer to the netlink message.
+ */
+struct nlmsghdr *nlmsg_hdr(struct nl_msg *n)
+{
+       return n->nm_nlh;
+}
+
+/**
+ * Free a netlink message
+ * @arg n              netlink message
+ *
+ * Destroys a netlink message and frees up all used memory.
+ *
+ * @pre The message must be unused.
+ */
+void nlmsg_free(struct nl_msg *n)
+{
+       if (!n)
+               return;
+
+       free(n->nm_nlh);
+       free(n);
+}
+
+/** @} */
+
+/**
+ * @name Attribute Modification
+ * @{
+ */
+
+void nlmsg_set_proto(struct nl_msg *msg, int protocol)
+{
+       msg->nm_protocol = protocol;
+}
+
+int nlmsg_get_proto(struct nl_msg *msg)
+{
+       return msg->nm_protocol;
+}
+
+void nlmsg_set_src(struct nl_msg *msg, struct sockaddr_nl *addr)
+{
+       memcpy(&msg->nm_src, addr, sizeof(*addr));
+}
+
+struct sockaddr_nl *nlmsg_get_src(struct nl_msg *msg)
+{
+       return &msg->nm_src;
+}
+
+void nlmsg_set_dst(struct nl_msg *msg, struct sockaddr_nl *addr)
+{
+       memcpy(&msg->nm_dst, addr, sizeof(*addr));
+}
+
+struct sockaddr_nl *nlmsg_get_dst(struct nl_msg *msg)
+{
+       return &msg->nm_dst;
+}
+
+void nlmsg_set_creds(struct nl_msg *msg, struct ucred *creds)
+{
+       memcpy(&msg->nm_creds, creds, sizeof(*creds));
+       msg->nm_flags |= NL_MSG_CRED_PRESENT;
+}
+
+struct ucred *nlmsg_get_creds(struct nl_msg *msg)
+{
+       if (msg->nm_flags & NL_MSG_CRED_PRESENT)
+               return &msg->nm_creds;
+       return NULL;
+}
+
+/** @} */
+
+/**
+ * @name Netlink Message Type Translations
+ * @{
+ */
+
+static struct trans_tbl nl_msgtypes[] = {
+       __ADD(NLMSG_NOOP,NOOP)
+       __ADD(NLMSG_ERROR,ERROR)
+       __ADD(NLMSG_DONE,DONE)
+       __ADD(NLMSG_OVERRUN,OVERRUN)
+};
+
+/**
+ * Convert netlink message type number to character string.
+ * @arg type           Netlink message type.
+ * @arg buf            Destination buffer.
+ * @arg size           Size of destination buffer.
+ *
+ * Converts a netlink message type number to a character string and stores
+ * it in the provided buffer.
+ *
+ * @return The destination buffer or the type encoded in hexidecimal form
+ *        if no match was found.
+ */
+char *nl_nlmsgtype2str(int type, char *buf, size_t size)
+{
+       return __type2str(type, buf, size, nl_msgtypes,
+                         ARRAY_SIZE(nl_msgtypes));
+}
+
+/**
+ * Convert character string to netlink message type.
+ * @arg name           Name of netlink message type.
+ *
+ * Converts the provided character string specifying a netlink message type
+ * into the corresponding numeric value
+ *
+ * @return Numeric netlink message type or a negative value
+ *        if no match was found.
+ */
+int nl_str2nlmsgtype(const char *name)
+{
+       return __str2type(name, nl_msgtypes, ARRAY_SIZE(nl_msgtypes));
+}
+
+/** @} */
+
+/**
+ * @name Netlink Message Flags Translations
+ * @{
+ */
+
+/**
+ * Translate netlink message flags into a character string (Reentrant).
+ * @arg flags          netlink message flags
+ * @arg buf            destination buffer
+ * @arg len            buffer length
+ *
+ * Translates netlink message flags into a character string and stores
+ * it in the provided buffer.
+ *
+ * @return The destination buffer
+ */
+char *nl_nlmsg_flags2str(int flags, char *buf, size_t len)
+{
+       memset(buf, 0, len);
+
+#define PRINT_FLAG(f) \
+       if (flags & NLM_F_##f) { \
+               flags &= ~NLM_F_##f; \
+               strncat(buf, #f, len - strlen(buf) - 1); \
+               if (flags) \
+                       strncat(buf, ",", len - strlen(buf) - 1); \
+       }
+       
+       PRINT_FLAG(REQUEST);
+       PRINT_FLAG(MULTI);
+       PRINT_FLAG(ACK);
+       PRINT_FLAG(ECHO);
+       PRINT_FLAG(ROOT);
+       PRINT_FLAG(MATCH);
+       PRINT_FLAG(ATOMIC);
+       PRINT_FLAG(REPLACE);
+       PRINT_FLAG(EXCL);
+       PRINT_FLAG(CREATE);
+       PRINT_FLAG(APPEND);
+
+       if (flags) {
+               char s[32];
+               snprintf(s, sizeof(s), "0x%x", flags);
+               strncat(buf, s, len - strlen(buf) - 1);
+       }
+#undef PRINT_FLAG
+
+       return buf;
+}
+
+/** @} */
+
+/**
+ * @name Direct Parsing
+ * @{
+ */
+
+/** @cond SKIP */
+struct dp_xdata {
+       void (*cb)(struct nl_object *, void *);
+       void *arg;
+};
+/** @endcond */
+
+static int parse_cb(struct nl_object *obj, struct nl_parser_param *p)
+{
+       struct dp_xdata *x = p->pp_arg;
+
+       x->cb(obj, x->arg);
+       nl_object_put(obj);
+       return 0;
+}
+
+int nl_msg_parse(struct nl_msg *msg, void (*cb)(struct nl_object *, void *),
+                void *arg)
+{
+       struct nl_cache_ops *ops;
+       struct nl_parser_param p = {
+               .pp_cb = parse_cb
+       };
+       struct dp_xdata x = {
+               .cb = cb,
+               .arg = arg,
+       };
+
+       ops = nl_cache_mngt_associate(nlmsg_get_proto(msg),
+                                     nlmsg_hdr(msg)->nlmsg_type);
+       if (ops == NULL)
+               return nl_error(ENOENT, "Unknown message type %d",
+                               nlmsg_hdr(msg)->nlmsg_type);
+       p.pp_arg = &x;
+
+       return nl_cache_parse(ops, NULL, nlmsg_hdr(msg), &p);
+}
+
+/** @} */
+
+/** @} */
diff --git a/lib/nl.c b/lib/nl.c
new file mode 100644 (file)
index 0000000..600f93e
--- /dev/null
+++ b/lib/nl.c
@@ -0,0 +1,1080 @@
+/*
+ * lib/nl.c            Core Netlink Interface
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @defgroup nl Core Netlink API
+ * @brief
+ *
+ * @par 1) Creating the netlink handle
+ * @code
+ * struct nl_handle *handle;
+ *
+ * // Allocate and initialize a new netlink handle
+ * handle = nl_handle_new();
+ *
+ * // Are multiple handles being allocated? You have to provide a unique
+ * // netlink process id and overwrite the default local process id.
+ * nl_handle_set_pid(handle, MY_UNIQUE_PID);
+ *
+ * // Is this socket used for event processing? You need to disable sequence
+ * // number checking in order to be able to receive messages not explicitely
+ * // requested.
+ * nl_disable_sequence_check(handle);
+ *
+ * // Use nl_handle_get_fd() to fetch the file description, for example to
+ * // put a socket into non-blocking i/o mode.
+ * fcntl(nl_handle_get_fd(handle), F_SETFL, O_NONBLOCK);
+ * @endcode
+ *
+ * @par 2) Joining Groups
+ * @code
+ * // You may join/subscribe to as many groups as you want, don't forget
+ * // to eventually disable sequence number checking. Note: Joining must
+ * // be done before connecting/binding the socket.
+ * nl_join_groups(handle, GROUP_ID1 | GROUP_ID2);
+ * @endcode
+ *
+ * @par 3) Connecting the socket
+ * @code
+ * // Bind and connect the socket to a protocol, NETLINK_ROUTE in this example.
+ * nl_connect(handle, NETLINK_ROUTE);
+ * @endcode
+ *
+ * @par 4) Sending data
+ * @code
+ * // The most rudimentary method is to use nl_sendto() simply pushing
+ * // a piece of data to the other netlink peer. This method is not
+ * // recommended.
+ * const char buf[] = { 0x01, 0x02, 0x03, 0x04 };
+ * nl_sendto(handle, buf, sizeof(buf));
+ *
+ * // A more comfortable interface is nl_send() taking a pointer to
+ * // a netlink message.
+ * struct nl_msg *msg = my_msg_builder();
+ * nl_send(handle, nlmsg_hdr(msg));
+ *
+ * // nl_sendmsg() provides additional control over the sendmsg() message
+ * // header in order to allow more specific addressing of multiple peers etc.
+ * struct msghdr hdr = { ... };
+ * nl_sendmsg(handle, nlmsg_hdr(msg), &hdr);
+ *
+ * // You're probably too lazy to fill out the netlink pid, sequence number
+ * // and message flags all the time. nl_send_auto_complete() automatically
+ * // extends your message header as needed with an appropriate sequence
+ * // number, the netlink pid stored in the netlink handle and the message
+ * // flags NLM_F_REQUEST and NLM_F_ACK
+ * nl_send_auto_complete(handle, nlmsg_hdr(msg));
+ *
+ * // Simple protocols don't require the complex message construction interface
+ * // and may favour nl_send_simple() to easly send a bunch of payload
+ * // encapsulated in a netlink message header.
+ * nl_send_simple(handle, MY_MSG_TYPE, 0, buf, sizeof(buf));
+ * @endcode
+ *
+ * @par 5) Receiving data
+ * @code
+ * // nl_recv() receives a single message allocating a buffer for the message
+ * // content and gives back the pointer to you.
+ * struct sockaddr_nl peer;
+ * unsigned char *msg;
+ * nl_recv(handle, &peer, &msg);
+ *
+ * // nl_recvmsgs() receives a bunch of messages until the callback system
+ * // orders it to state, usually after receving a compolete multi part
+ * // message series.
+ * nl_recvmsgs(handle, my_callback_configuration);
+ *
+ * // nl_recvmsgs_def() acts just like nl_recvmsg() but uses the callback
+ * // configuration stored in the handle.
+ * nl_recvmsgs_def(handle);
+ *
+ * // In case you want to wait for the ACK to be recieved that you requested
+ * // with your latest message, you can call nl_wait_for_ack()
+ * nl_wait_for_ack(handle);
+ * @endcode
+ *
+ * @par 6) Cleaning up
+ * @code
+ * // Close the socket first to release kernel memory
+ * nl_close(handle);
+ *
+ * // Finally destroy the netlink handle
+ * nl_handle_destroy(handle);
+ * @endcode
+ * 
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink/netlink.h>
+#include <netlink/utils.h>
+#include <netlink/handlers.h>
+#include <netlink/msg.h>
+#include <netlink/attr.h>
+
+/**
+ * @name Handle Management
+ * @{
+ */
+
+/**
+ * Allocate and initialize new non-default netlink handle.
+ * @arg kind           Kind of callback handler to use per default.
+ *
+ * Allocates and initializes a new netlink handle, the netlink process id
+ * is set to the local process id which may conflict if multiple handles
+ * are created, therefore you may have to overwrite it using
+ * nl_handle_set_pid(). The initial sequence number is initialized to the
+ * current UNIX time.
+ *
+ * @return Newly allocated netlink handle or NULL.
+ */
+struct nl_handle *nl_handle_alloc_nondefault(enum nl_cb_kind kind)
+{
+       struct nl_handle *handle;
+       
+       handle = calloc(1, sizeof(*handle));
+       if (!handle)
+               goto errout;
+
+       handle->h_cb = nl_cb_new(kind);
+       if (!handle->h_cb)
+               goto errout;
+       
+       handle->h_local.nl_family = AF_NETLINK;
+       handle->h_peer.nl_family = AF_NETLINK;
+       handle->h_local.nl_pid = getpid();
+       handle->h_seq_expect = handle->h_seq_next = time(0);
+
+       return handle;
+errout:
+       nl_handle_destroy(handle);
+       nl_errno(ENOMEM);
+       return NULL;
+}
+
+/**
+ * Allocate and initialize new netlink handle.
+ *
+ * Allocates and initializes a new netlink handle, the netlink process id
+ * is set to the local process id which may conflict if multiple handles
+ * are created, therefore you may have to overwrite it using
+ * nl_handle_set_pid(). The initial sequence number is initialized to the
+ * current UNIX time. The default callback (NL_CB_DEFAULT) handlers are
+ * being used.
+ *
+ * @return Newly allocated netlink handle or NULL.
+ */
+struct nl_handle *nl_handle_alloc(void)
+{
+       return nl_handle_alloc_nondefault(NL_CB_DEFAULT);
+}
+
+/**
+ * Destroy netlink handle.
+ * @arg handle         Netlink handle.
+ */
+void nl_handle_destroy(struct nl_handle *handle)
+{
+       if (!handle)
+               return;
+
+       nl_cb_destroy(handle->h_cb);
+       free(handle);
+}
+
+/** @} */
+
+/**
+ * @name Utilities
+ * @{
+ */
+
+/**
+ * Set socket buffer size of netlink handle.
+ * @arg handle         Netlink handle.
+ * @arg rxbuf          New receive socket buffer size in bytes.
+ * @arg txbuf          New transmit socket buffer size in bytes.
+ *
+ * Sets the socket buffer size of a netlink handle to the specified
+ * values \c rxbuf and \c txbuf. Providing a value of \c 0 assumes a
+ * good default value.
+ *
+ * @note It is not required to call this function prior to nl_connect().
+ * @return 0 on sucess or a negative error code.
+ */
+int nl_set_buffer_size(struct nl_handle *handle, int rxbuf, int txbuf)
+{
+       int err;
+
+       if (rxbuf <= 0)
+               rxbuf = 32768;
+
+       if (txbuf <= 0)
+               txbuf = 32768;
+       
+       err = setsockopt(handle->h_fd, SOL_SOCKET, SO_SNDBUF,
+                        &txbuf, sizeof(txbuf));
+       if (err < 0)
+               return nl_error(errno, "setsockopt(SO_SNDBUF) failed");
+
+       err = setsockopt(handle->h_fd, SOL_SOCKET, SO_RCVBUF,
+                        &rxbuf, sizeof(rxbuf));
+       if (err < 0)
+               return nl_error(errno, "setsockopt(SO_RCVBUF) failed");
+
+       handle->h_flags |= NL_SOCK_BUFSIZE_SET;
+
+       return 0;
+}
+
+/**
+ * Enable/disable credential passing on netlink handle.
+ * @arg handle         Netlink handle
+ * @arg state          New state (0 - disabled, 1 - enabled)
+ */
+int nl_set_passcred(struct nl_handle *handle, int state)
+{
+       int err;
+
+       err = setsockopt(handle->h_fd, SOL_SOCKET, SO_PASSCRED,
+                        &state, sizeof(state));
+       if (err < 0)
+               return nl_error(errno, "setsockopt(SO_PASSCRED) failed");
+
+       if (state)
+               handle->h_flags |= NL_SOCK_PASSCRED;
+       else
+               handle->h_flags &= ~NL_SOCK_PASSCRED;
+
+       return 0;
+}
+
+/**
+ * Join multicast groups.
+ * @arg handle         Netlink handle.
+ * @arg groups         Bitmask of groups to join.
+ *
+ * @note Joining of groups must be done prior to connecting/binding
+ *       the socket (nl_connect()).
+ */
+void nl_join_groups(struct nl_handle *handle, int groups)
+{
+       handle->h_local.nl_groups |= groups;
+}
+
+#ifndef SOL_NETLINK
+#define SOL_NETLINK 270
+#endif
+
+int nl_join_group(struct nl_handle *handle, int group)
+{
+       int err;
+
+       err = setsockopt(handle->h_fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
+                        &group, sizeof(group));
+       if (err < 0)
+               return nl_error(errno, "setsockopt(NETLINK_ADD_MEMBERSHIP) "
+                                      "failed");
+
+       return 0;
+}
+
+static int noop_seq_check(struct nl_msg *msg, void *arg)
+{
+       return NL_PROCEED;
+}
+
+/**
+ * Disable sequence number checking.
+ * @arg handle         Netlink handle.
+ *
+ * Disables checking of sequence numbers on the netlink handle. This is
+ * required to allow messages to be processed which were not requested by
+ * a preceding request message, e.g. netlink events.
+ */
+void nl_disable_sequence_check(struct nl_handle *handle)
+{
+       nl_cb_set(nl_handle_get_cb(handle), NL_CB_SEQ_CHECK,
+                 NL_CB_CUSTOM, noop_seq_check, NULL);
+}
+
+/** @} */
+
+/**
+ * @name Acccess Functions
+ * @{
+ */
+
+/**
+ * Get netlink process identifier of netlink handle.
+ * @arg handle         Netlink handle.
+ * @return Netlink process identifier.
+ */
+pid_t nl_handle_get_pid(struct nl_handle *handle)
+{
+       return handle->h_local.nl_pid;
+}
+
+/**
+ * Set netlink process identifier of netlink handle.
+ * @arg handle         Netlink handle.
+ * @arg pid            New netlink process identifier.
+ */
+void nl_handle_set_pid(struct nl_handle *handle, pid_t pid)
+{
+       handle->h_local.nl_pid = pid;
+}
+
+/**
+ * Get netlink process identifier of peer from netlink handle.
+ * @arg handle         Netlink handle.
+ * @return Netlink process identifier.
+ */
+pid_t nl_handle_get_peer_pid(struct nl_handle *handle)
+{
+       return handle->h_peer.nl_pid;
+}
+
+/**
+ * Set netlink process identifier of peer in netlink handle.
+ * @arg handle         Netlink handle.
+ * @arg pid            New netlink process identifier.
+ */
+void nl_handle_set_peer_pid(struct nl_handle *handle, pid_t pid)
+{
+       handle->h_peer.nl_pid = pid;
+}
+
+/**
+ * Get file descriptor of netlink handle.
+ * @arg handle         Netlink handle.
+ * @return File descriptor of netlink socket or -1 if not connected.
+ */
+int nl_handle_get_fd(struct nl_handle *handle)
+{
+       return handle->h_fd;
+}
+
+/**
+ * Get local netlink address of netlink handle.
+ * @arg handle         Netlink handle.
+ * @return Local netlink address.
+ */
+struct sockaddr_nl *nl_handle_get_local_addr(struct nl_handle *handle)
+{
+       return &handle->h_local;
+}
+
+/**
+ * Get peer netlink address of netlink handle.
+ * @arg handle         Netlink handle.
+ * @note The peer address is undefined while the socket is unconnected.
+ * @return Netlink address of the peer.
+ */
+struct sockaddr_nl *nl_handle_get_peer_addr(struct nl_handle *handle)
+{
+       return &handle->h_peer;
+}
+
+/**
+ * Get callback configuration of netlink handle.
+ * @arg handle         Netlink handle.
+ * @return Currently active callback configuration or NULL if not available.
+ */
+struct nl_cb *nl_handle_get_cb(struct nl_handle *handle)
+{
+       return handle->h_cb;
+}
+
+/** @} */
+
+/**
+ * @name Connection Management
+ * @{
+ */
+
+/**
+ * Create and connect netlink socket.
+ * @arg handle         Netlink handle.
+ * @arg protocol       Netlink protocol to use.
+ *
+ * Creates a netlink socket using the specified protocol, binds the socket
+ * and issues a connection attempt.
+ *
+ * @return 0 on success or a negative error code.
+ */
+int nl_connect(struct nl_handle *handle, int protocol)
+{
+       int err;
+       socklen_t addrlen;
+
+       handle->h_fd = socket(AF_NETLINK, SOCK_RAW, protocol);
+       if (handle->h_fd < 0)
+               return nl_error(1, "socket(AF_NETLINK, ...) failed");
+
+       if (!(handle->h_flags & NL_SOCK_BUFSIZE_SET)) {
+               err = nl_set_buffer_size(handle, 0, 0);
+               if (err < 0)
+                       return err;
+       }
+
+       err = bind(handle->h_fd, (struct sockaddr*) &handle->h_local,
+                  sizeof(handle->h_local));
+       if (err < 0)
+               return nl_error(1, "bind() failed");
+
+       addrlen = sizeof(handle->h_local);
+       err = getsockname(handle->h_fd, (struct sockaddr *) &handle->h_local,
+                         &addrlen);
+       if (err < 0)
+               return nl_error(1, "getsockname failed");
+
+       if (addrlen != sizeof(handle->h_local))
+               return nl_error(EADDRNOTAVAIL, "Invalid address length");
+
+       if (handle->h_local.nl_family != AF_NETLINK)
+               return nl_error(EPFNOSUPPORT, "Address format not supported");
+
+       handle->h_proto = protocol;
+
+       return 0;
+}
+
+/**
+ * Close/Disconnect netlink socket.
+ * @arg handle         Netlink handle
+ */
+void nl_close(struct nl_handle *handle)
+{
+       if (handle->h_fd >= 0) {
+               close(handle->h_fd);
+               handle->h_fd = -1;
+       }
+
+       handle->h_proto = 0;
+}
+
+/** @} */
+
+/**
+ * @name Send
+ * @{
+ */
+
+/**
+ * Send raw data over netlink socket.
+ * @arg handle         Netlink handle.
+ * @arg buf            Data buffer.
+ * @arg size           Size of data buffer.
+ * @return Number of characters written on success or a negative error code.
+ */
+int nl_sendto(struct nl_handle *handle, void *buf, size_t size)
+{
+       int ret;
+
+       ret = sendto(handle->h_fd, buf, size, 0, (struct sockaddr *)
+                    &handle->h_peer, sizeof(handle->h_peer));
+       if (ret < 0)
+               return nl_errno(errno);
+
+       return ret;
+}
+
+/**
+ * Send netlink message with control over sendmsg() message header.
+ * @arg handle         Netlink handle.
+ * @arg msg            Netlink message to be sent.
+ * @arg hdr            Sendmsg() message header.
+ * @return Number of characters sent on sucess or a negative error code.
+ */
+int nl_sendmsg(struct nl_handle *handle, struct nl_msg *msg, struct msghdr *hdr)
+{
+       struct nl_cb *cb;
+       int ret;
+
+       struct iovec iov = {
+               .iov_base = (void *) nlmsg_hdr(msg),
+               .iov_len = nlmsg_hdr(msg)->nlmsg_len,
+       };
+
+       hdr->msg_iov = &iov;
+       hdr->msg_iovlen = 1;
+
+       nlmsg_set_src(msg, &handle->h_local);
+
+       cb = nl_handle_get_cb(handle);
+       if (cb->cb_set[NL_CB_MSG_OUT])
+               if (nl_cb_call(cb, NL_CB_MSG_OUT, msg) != NL_PROCEED)
+                       return 0;
+
+       ret = sendmsg(handle->h_fd, hdr, 0);
+       if (ret < 0)
+               return nl_errno(errno);
+
+       return ret;
+}
+
+
+/**
+ * Send netlink message.
+ * @arg handle         Netlink handle
+ * @arg msg            Netlink message to be sent.
+ * @see nl_sendmsg()
+ * @return Number of characters sent on success or a negative error code.
+ */
+int nl_send(struct nl_handle *handle, struct nl_msg *msg)
+{
+       struct sockaddr_nl *dst;
+       struct ucred *creds;
+       
+       struct msghdr hdr = {
+               .msg_name = (void *) &handle->h_peer,
+               .msg_namelen = sizeof(struct sockaddr_nl),
+       };
+
+       /* Overwrite destination if specified in the message itself, defaults
+        * to the peer address of the handle.
+        */
+       dst = nlmsg_get_dst(msg);
+       if (dst->nl_family == AF_NETLINK)
+               hdr.msg_name = dst;
+
+       /* Add credentials if present. */
+       creds = nlmsg_get_creds(msg);
+       if (creds != NULL) {
+               char buf[CMSG_SPACE(sizeof(struct ucred))];
+               struct cmsghdr *cmsg;
+
+               hdr.msg_control = buf;
+               hdr.msg_controllen = sizeof(buf);
+
+               cmsg = CMSG_FIRSTHDR(&hdr);
+               cmsg->cmsg_level = SOL_SOCKET;
+               cmsg->cmsg_type = SCM_CREDENTIALS;
+               cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
+               memcpy(CMSG_DATA(cmsg), creds, sizeof(struct ucred));
+       }
+
+       return nl_sendmsg(handle, msg, &hdr);
+}
+
+/**
+ * Send netlink message and check & extend header values as needed.
+ * @arg handle         Netlink handle.
+ * @arg msg            Netlink message to be sent.
+ *
+ * Checks the netlink message \c nlh for completness and extends it
+ * as required before sending it out. Checked fields include pid,
+ * sequence nr, and flags.
+ *
+ * @see nl_send()
+ * @return Number of characters sent or a negative error code.
+ */
+int nl_send_auto_complete(struct nl_handle *handle, struct nl_msg *msg)
+{
+       struct nlmsghdr *nlh;
+
+       nlh = nlmsg_hdr(msg);
+       if (nlh->nlmsg_pid == 0)
+               nlh->nlmsg_pid = handle->h_local.nl_pid;
+
+       if (nlh->nlmsg_seq == 0)
+               nlh->nlmsg_seq = handle->h_seq_next++;
+       
+       nlh->nlmsg_flags |= (NLM_F_REQUEST | NLM_F_ACK);
+
+       if (handle->h_cb->cb_send_ow)
+               return handle->h_cb->cb_send_ow(handle, msg);
+       else
+               return nl_send(handle, msg);
+}
+
+/**
+ * Send simple netlink message using nl_send_auto_complete()
+ * @arg handle         Netlink handle.
+ * @arg type           Netlink message type.
+ * @arg flags          Netlink message flags.
+ * @arg buf            Data buffer.
+ * @arg size           Size of data buffer.
+ *
+ * Builds a netlink message with the specified type and flags and
+ * appends the specified data as payload to the message.
+ *
+ * @see nl_send_auto_complete()
+ * @return Number of characters sent on success or a negative error code.
+ */
+int nl_send_simple(struct nl_handle *handle, int type, int flags, void *buf,
+                  size_t size)
+{
+       int err;
+       struct nl_msg *msg;
+       struct nlmsghdr nlh = {
+               .nlmsg_len = nlmsg_msg_size(0),
+               .nlmsg_type = type,
+               .nlmsg_flags = flags,
+       };
+
+       msg = nlmsg_build(&nlh);
+       if (!msg)
+               return nl_errno(ENOMEM);
+
+       if (buf && size)
+               nlmsg_append(msg, buf, size, 1);
+
+       err = nl_send_auto_complete(handle, msg);
+       nlmsg_free(msg);
+
+       return err;
+}
+
+/** @} */
+
+/**
+ * @name Receive
+ * @{
+ */
+
+/**
+ * Receive netlink message from netlink socket.
+ * @arg handle         Netlink handle.
+ * @arg nla            Destination pointer for peer's netlink address.
+ * @arg buf            Destination pointer for message content.
+ * @arg creds          Destination pointer for credentials.
+ *
+ * Receives a netlink message, allocates a buffer in \c *buf and
+ * stores the message content. The peer's netlink address is stored
+ * in \c *nla. The caller is responsible for freeing the buffer allocated
+ * in \c *buf if a positive value is returned.  Interruped system calls
+ * are handled by repeating the read. The input buffer size is determined
+ * by peeking before the actual read is done.
+ *
+ * A non-blocking sockets causes the function to return immediately if
+ * no data is available.
+ *
+ * @return Number of octets read, 0 on EOF or a negative error code.
+ */
+int nl_recv(struct nl_handle *handle, struct sockaddr_nl *nla,
+           unsigned char **buf, struct ucred **creds)
+{
+       int n;
+       int flags = MSG_PEEK;
+
+       struct iovec iov = {
+               .iov_len = 4096,
+       };
+
+       struct msghdr msg = {
+               .msg_name = (void *) nla,
+               .msg_namelen = sizeof(sizeof(struct sockaddr_nl)),
+               .msg_iov = &iov,
+               .msg_iovlen = 1,
+               .msg_control = NULL,
+               .msg_controllen = 0,
+               .msg_flags = 0,
+       };
+       struct cmsghdr *cmsg;
+
+       iov.iov_base = *buf = calloc(1, iov.iov_len);
+
+       if (handle->h_flags & NL_SOCK_PASSCRED) {
+               msg.msg_controllen = CMSG_SPACE(sizeof(struct ucred));
+               msg.msg_control = calloc(1, msg.msg_controllen);
+       }
+retry:
+
+       if ((n = recvmsg(handle->h_fd, &msg, flags)) <= 0) {
+               if (!n)
+                       goto abort;
+               else if (n < 0) {
+                       if (errno == EINTR)
+                               goto retry;
+                       else if (errno == EAGAIN)
+                               goto abort;
+                       else {
+                               free(msg.msg_control);
+                               free(*buf);
+                               return nl_error(errno, "recvmsg failed");
+                       }
+               }
+       }
+       
+       if (iov.iov_len < n) {
+               /* Provided buffer is not long enough, enlarge it
+                * and try again. */
+               iov.iov_len *= 2;
+               iov.iov_base = *buf = realloc(*buf, iov.iov_len);
+               goto retry;
+       } else if (msg.msg_flags & MSG_CTRUNC) {
+               msg.msg_controllen *= 2;
+               msg.msg_control = realloc(msg.msg_control, msg.msg_controllen);
+               goto retry;
+       } else if (flags != 0) {
+               /* Buffer is big enough, do the actual reading */
+               flags = 0;
+               goto retry;
+       }
+
+       if (msg.msg_namelen != sizeof(struct sockaddr_nl)) {
+               free(msg.msg_control);
+               free(*buf);
+               return nl_error(EADDRNOTAVAIL, "socket address size mismatch");
+       }
+
+       for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
+               if (cmsg->cmsg_level == SOL_SOCKET &&
+                   cmsg->cmsg_type == SCM_CREDENTIALS) {
+                       *creds = calloc(1, sizeof(struct ucred));
+                       memcpy(*creds, CMSG_DATA(cmsg), sizeof(struct ucred));
+                       break;
+               }
+       }
+
+       free(msg.msg_control);
+       return n;
+
+abort:
+       free(msg.msg_control);
+       free(*buf);
+       return 0;
+}
+
+
+/**
+ * Receive a set of messages from a netlink socket.
+ * @arg handle         netlink handle
+ * @arg cb             set of callbacks to control the behaviour.
+ *
+ * Repeatedly calls nl_recv() and parses the messages as netlink
+ * messages. Stops reading if one of the callbacks returns
+ * NL_EXIT or nl_recv returns either 0 or a negative error code.
+ *
+ * A non-blocking sockets causes the function to return immediately if
+ * no data is available.
+ *
+ * @return 0 on success or a negative error code from nl_recv().
+ */
+int nl_recvmsgs(struct nl_handle *handle, struct nl_cb *cb)
+{
+       int n, err = 0;
+       unsigned char *buf = NULL;
+       struct nlmsghdr *hdr;
+       struct sockaddr_nl nla = {0};
+       struct nl_msg *msg = NULL;
+       struct ucred *creds = NULL;
+
+continue_reading:
+       if (cb->cb_recv_ow)
+               n = cb->cb_recv_ow(handle, &nla, &buf, &creds);
+       else
+               n = nl_recv(handle, &nla, &buf, &creds);
+
+       if (n <= 0)
+               return n;
+
+       NL_DBG(3, "recvmsgs(%p): Read %d bytes\n", handle, n);
+
+       hdr = (struct nlmsghdr *) buf;
+       while (nlmsg_ok(hdr, n)) {
+               NL_DBG(3, "recgmsgs(%p): Processing valid message...\n",
+                      handle);
+
+               nlmsg_free(msg);
+               msg = nlmsg_convert(hdr);
+               if (!msg) {
+                       err = nl_errno(ENOMEM);
+                       goto out;
+               }
+
+               nlmsg_set_proto(msg, handle->h_proto);
+               nlmsg_set_src(msg, &nla);
+               if (creds)
+                       nlmsg_set_creds(msg, creds);
+
+               /* Raw callback is the first, it gives the most control
+                * to the user and he can do his very own parsing. */
+               if (cb->cb_set[NL_CB_MSG_IN]) {
+                       err = nl_cb_call(cb, NL_CB_MSG_IN, msg);
+                       if (err == NL_SKIP)
+                               goto skip;
+                       else if (err == NL_EXIT || err < 0)
+                               goto out;
+               }
+
+               /* Sequence number checking. The check may be done by
+                * the user, otherwise a very simple check is applied
+                * enforcing strict ordering */
+               if (cb->cb_set[NL_CB_SEQ_CHECK]) {
+                       err = nl_cb_call(cb, NL_CB_SEQ_CHECK, msg);
+                       if (err == NL_SKIP)
+                               goto skip;
+                       else if (err == NL_EXIT || err < 0)
+                               goto out;
+               } else if (hdr->nlmsg_seq != handle->h_seq_expect) {
+                       if (cb->cb_set[NL_CB_INVALID]) {
+                               err = nl_cb_call(cb, NL_CB_INVALID, msg);
+                               if (err == NL_SKIP)
+                                       goto skip;
+                               else if (err == NL_EXIT || err < 0)
+                                       goto out;
+                       } else
+                               goto out;
+               }
+
+               if (hdr->nlmsg_type == NLMSG_DONE ||
+                   hdr->nlmsg_type == NLMSG_ERROR ||
+                   hdr->nlmsg_type == NLMSG_NOOP ||
+                   hdr->nlmsg_type == NLMSG_OVERRUN) {
+                       /* We can't check for !NLM_F_MULTI since some netlink
+                        * users in the kernel are broken. */
+                       handle->h_seq_expect++;
+                       NL_DBG(3, "recvmsgs(%p): Increased expected " \
+                              "sequence number to %d\n",
+                              handle, handle->h_seq_expect);
+               }
+       
+               /* Other side wishes to see an ack for this message */
+               if (hdr->nlmsg_flags & NLM_F_ACK) {
+                       if (cb->cb_set[NL_CB_SEND_ACK]) {
+                               err = nl_cb_call(cb, NL_CB_SEND_ACK, msg);
+                               if (err == NL_SKIP)
+                                       goto skip;
+                               else if (err == NL_EXIT || err < 0)
+                                       goto out;
+                       } else {
+                               /* FIXME: implement */
+                       }
+               }
+
+               /* messages terminates a multpart message, this is
+                * usually the end of a message and therefore we slip
+                * out of the loop by default. the user may overrule
+                * this action by skipping this packet. */
+               if (hdr->nlmsg_type == NLMSG_DONE) {
+                       if (cb->cb_set[NL_CB_FINISH]) {
+                               err = nl_cb_call(cb, NL_CB_FINISH, msg);
+                               if (err == NL_SKIP)
+                                       goto skip;
+                               else if (err == NL_EXIT || err < 0)
+                                       goto out;
+                       }
+                       err = 0;
+                       goto out;
+               }
+
+               /* Message to be ignored, the default action is to
+                * skip this message if no callback is specified. The
+                * user may overrule this action by returning
+                * NL_PROCEED. */
+               else if (hdr->nlmsg_type == NLMSG_NOOP) {
+                       if (cb->cb_set[NL_CB_SKIPPED]) {
+                               err = nl_cb_call(cb, NL_CB_SKIPPED, msg);
+                               if (err == NL_SKIP)
+                                       goto skip;
+                               else if (err == NL_EXIT || err < 0)
+                                       goto out;
+                       } else
+                               goto skip;
+               }
+
+               /* Data got lost, report back to user. The default action is to
+                * quit parsing. The user may overrule this action by retuning
+                * NL_SKIP or NL_PROCEED (dangerous) */
+               else if (hdr->nlmsg_type == NLMSG_OVERRUN) {
+                       if (cb->cb_set[NL_CB_OVERRUN]) {
+                               err = nl_cb_call(cb, NL_CB_OVERRUN, msg);
+                               if (err == NL_SKIP)
+                                       goto skip;
+                               else if (err == NL_EXIT || err < 0)
+                                       goto out;
+                       } else
+                               goto out;
+               }
+
+               /* Message carries a nlmsgerr */
+               else if (hdr->nlmsg_type == NLMSG_ERROR) {
+                       struct nlmsgerr *e = nlmsg_data(hdr);
+
+                       if (hdr->nlmsg_len < nlmsg_msg_size(sizeof(*e))) {
+                               /* Truncated error message, the default action
+                                * is to stop parsing. The user may overrule
+                                * this action by returning NL_SKIP or
+                                * NL_PROCEED (dangerous) */
+                               if (cb->cb_set[NL_CB_INVALID]) {
+                                       err = nl_cb_call(cb, NL_CB_INVALID,
+                                                        msg);
+                                       if (err == NL_SKIP)
+                                               goto skip;
+                                       else if (err == NL_EXIT || err < 0)
+                                               goto out;
+                               } else
+                                       goto out;
+                       } else if (e->error) {
+                               /* Error message reported back from kernel. */
+                               if (cb->cb_err) {
+                                       err = cb->cb_err(&nla, e,
+                                                          cb->cb_err_arg);
+                                       if (err == NL_SKIP)
+                                               goto skip;
+                                       else if (err == NL_EXIT || err < 0) {
+                                               nl_error(-e->error,
+                                                        "Netlink Error");
+                                               err = e->error;
+                                               goto out;
+                                       }
+                               } else {
+                                       nl_error(-e->error, "Netlink Error");
+                                       err = e->error;
+                                       goto out;
+                               }
+                       } else if (cb->cb_set[NL_CB_ACK]) {
+                               /* ACK */
+                               err = nl_cb_call(cb, NL_CB_ACK, msg);
+                               if (err == NL_SKIP)
+                                       goto skip;
+                               else if (err == NL_EXIT || err < 0)
+                                       goto out;
+                       }
+               } else {
+                       /* Valid message (not checking for MULTIPART bit to
+                        * get along with broken kernels. NL_SKIP has no
+                        * effect on this.  */
+                       if (cb->cb_set[NL_CB_VALID]) {
+                               err = nl_cb_call(cb, NL_CB_VALID, msg);
+                               if (err == NL_SKIP)
+                                       goto skip;
+                               else if (err == NL_EXIT || err < 0)
+                                       goto out;
+                       }
+               }
+skip:
+               hdr = nlmsg_next(hdr, &n);
+       }
+       
+       nlmsg_free(msg);
+       free(buf);
+       free(creds);
+       buf = NULL;
+       msg = NULL;
+       creds = NULL;
+
+       /* Multipart message not yet complete, continue reading */
+       goto continue_reading;
+
+out:
+       nlmsg_free(msg);
+       free(buf);
+       free(creds);
+
+       return err;
+}
+
+/**
+ * Receive a set of message from a netlink socket using handlers in nl_handle.
+ * @arg handle         netlink handle
+ *
+ * Calls nl_recvmsgs() with the handlers configured in the netlink handle.
+ */
+int nl_recvmsgs_def(struct nl_handle *handle)
+{
+       if (handle->h_cb->cb_recvmsgs_ow)
+               return handle->h_cb->cb_recvmsgs_ow(handle, handle->h_cb);
+       else
+               return nl_recvmsgs(handle, handle->h_cb);
+}
+
+static int ack_wait_handler(struct nl_msg *msg, void *arg)
+{
+       return NL_EXIT;
+}
+
+/**
+ * Wait for ACK.
+ * @arg handle         netlink handle
+ * @pre The netlink socket must be in blocking state.
+ *
+ * Waits until an ACK is received for the latest not yet acknowledged
+ * netlink message.
+ */
+int nl_wait_for_ack(struct nl_handle *handle)
+{
+       int err;
+       struct nl_cb *cb = nl_cb_clone(nl_handle_get_cb(handle));
+
+       nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, NULL);
+
+       err = nl_recvmsgs(handle, cb);
+       nl_cb_destroy(cb);
+
+       return err;
+}
+
+/** @} */
+
+/**
+ * @name Netlink Family Translations
+ * @{
+ */
+
+static struct trans_tbl nlfamilies[] = {
+       __ADD(NETLINK_ROUTE,route)
+       __ADD(NETLINK_W1,w1)
+       __ADD(NETLINK_USERSOCK,usersock)
+       __ADD(NETLINK_FIREWALL,firewall)
+       __ADD(NETLINK_INET_DIAG,inetdiag)
+       __ADD(NETLINK_NFLOG,nflog)
+       __ADD(NETLINK_XFRM,xfrm)
+       __ADD(NETLINK_SELINUX,selinux)
+       __ADD(NETLINK_ISCSI,iscsi)
+       __ADD(NETLINK_AUDIT,audit)
+       __ADD(NETLINK_FIB_LOOKUP,fib_lookup)
+       __ADD(NETLINK_CONNECTOR,connector)
+       __ADD(NETLINK_NETFILTER,netfilter)
+       __ADD(NETLINK_IP6_FW,ip6_fw)
+       __ADD(NETLINK_DNRTMSG,dnrtmsg)
+       __ADD(NETLINK_KOBJECT_UEVENT,kobject_uevent)
+       __ADD(NETLINK_GENERIC,generic)
+};
+
+/**
+ * Convert netlink family to character string.
+ * @arg family         Netlink family.
+ * @arg buf            Destination buffer.
+ * @arg size           Size of destination buffer.
+ *
+ * Converts a netlink family to a character string and stores it in
+ * the specified destination buffer.
+ *
+ * @return The destination buffer or the family encoded in hexidecimal
+ *         form if no match was found.
+ */
+char * nl_nlfamily2str(int family, char *buf, size_t size)
+{
+       return __type2str(family, buf, size, nlfamilies,
+                         ARRAY_SIZE(nlfamilies));
+}
+
+/**
+ * Convert character string to netlink family.
+ * @arg name           Name of netlink family.
+ *
+ * Converts the provided character string specifying a netlink
+ * family to the corresponding numeric value.
+ *
+ * @return Numeric netlink family or a negative value if no match was found.
+ */
+int nl_str2nlfamily(const char *name)
+{
+       return __str2type(name, nlfamilies, ARRAY_SIZE(nlfamilies));
+}
+
+/** @} */
+/** @} */
diff --git a/lib/object.c b/lib/object.c
new file mode 100644 (file)
index 0000000..80f4047
--- /dev/null
@@ -0,0 +1,285 @@
+/*
+ * lib/object.c                Generic Cacheable Object
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup cache
+ * @defgroup object Cacheable Object
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink/netlink.h>
+#include <netlink/cache.h>
+#include <netlink/object.h>
+#include <netlink/utils.h>
+
+static inline struct nl_cache_ops *obj_ops(struct nl_object *obj)
+{
+       if (!obj->ce_ops)
+               BUG();
+
+       return obj->ce_ops;
+}
+
+/**
+ * @name Object Creation/Deletion
+ * @{
+ */
+
+/**
+ * Allocate a cacheable object
+ * @arg size           size of object
+ * @return The new object or NULL.
+ */
+struct nl_object *nl_object_alloc(size_t size)
+{
+       struct nl_object *new;
+
+       if (size < sizeof(*new))
+               BUG();
+
+       new = calloc(1, size);
+       if (!new) {
+               nl_errno(ENOMEM);
+               return NULL;
+       }
+
+       new->ce_refcnt = 1;
+       nl_init_list_head(&new->ce_list);
+
+       return new;
+}
+
+/**
+ * Allocate a new object of kind specified by the operations handle
+ * @arg ops            cache operations handle
+ * @return The new object or NULL
+ */
+struct nl_object *nl_object_alloc_from_ops(struct nl_cache_ops *ops)
+{
+       struct nl_object *new;
+
+       new = nl_object_alloc(ops->co_size);
+       if (new) {
+               new->ce_ops = ops;
+               if (ops->co_constructor)
+                       ops->co_constructor(new);
+       }
+
+       return new;
+}
+
+/**
+ * Allocate a new object of kind specified by the name
+ * @arg kind           name of object type
+ * @return The new object or nULL
+ */
+struct nl_object *nl_object_alloc_name(const char *kind)
+{
+       struct nl_cache_ops *ops;
+
+       ops = nl_cache_mngt_lookup(kind);
+       if (!ops) {
+               nl_error(ENOENT, "Unable to lookup cache kind \"%s\"", kind);
+               return NULL;
+       }
+
+       return nl_object_alloc_from_ops(ops);
+}
+
+struct nl_derived_object {
+       NLHDR_COMMON
+       char data;
+};
+
+/**
+ * Allocate a new object and copy all data from an existing object
+ * @arg obj            object to inherite data from
+ * @return The new object or NULL.
+ */
+struct nl_object *nl_object_clone(struct nl_object *obj)
+{
+       struct nl_object *new;
+       struct nl_cache_ops *ops = obj_ops(obj);
+       int doff = offsetof(struct nl_derived_object, data);
+       int size;
+
+       new = nl_object_alloc(ops->co_size);
+       if (!new)
+               return NULL;
+
+       size = ops->co_size - doff;
+       if (size < 0)
+               BUG();
+
+       new->ce_cache = obj->ce_cache;
+       new->ce_ops = obj->ce_ops;
+       new->ce_msgtype = obj->ce_msgtype;
+       if (ops->co_free_data) {
+               new->ce_dataref = obj;
+               nl_object_get(obj);
+       }
+
+       if (size)
+               memcpy((void *)new + doff, (void *)obj + doff, size);
+
+       return new;
+}
+
+/**
+ * Free a cacheable object
+ * @arg obj            object to free
+ *
+ * @return 0 or a negative error code.
+ */
+void nl_object_free(struct nl_object *obj)
+{
+       struct nl_cache_ops *ops = obj_ops(obj);
+
+       if (obj->ce_refcnt > 0)
+               NL_DBG(1, "Warning: Freeing object in used...\n");
+
+       if (obj->ce_dataref)
+               nl_object_put(obj->ce_dataref);
+       else if (ops->co_free_data)
+               ops->co_free_data(obj);
+
+       free(obj);
+}
+
+/** @} */
+
+/**
+ * @name Reference Management
+ * @{
+ */
+
+/**
+ * Acquire a reference on a object
+ * @arg obj            object to acquire reference from
+ */
+void nl_object_get(struct nl_object *obj)
+{
+       obj->ce_refcnt++;
+}
+
+/**
+ * Release a reference from an object
+ * @arg obj            object to release reference from
+ */
+void nl_object_put(struct nl_object *obj)
+{
+       if (!obj)
+               return;
+
+       obj->ce_refcnt--;
+
+       if (obj->ce_refcnt < 0)
+               BUG();
+
+       if (obj->ce_refcnt <= 0)
+               nl_object_free(obj);
+}
+
+/**
+ * Check whether this object is used by multiple users
+ * @arg obj            object to check
+ * @return true or false
+ */
+int nl_object_shared(struct nl_object *obj)
+{
+       return obj->ce_refcnt > 1;
+}
+
+/** @} */
+
+/**
+ * @name Utillities
+ * @{
+ */
+
+/**
+ * Dump this object according to the specified parameters
+ * @arg obj            object to dump
+ * @arg params         dumping parameters
+ */
+void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
+{
+       dump_from_ops(obj, params);
+}
+
+/**
+ * Match a filter against an object
+ * @arg obj            object to check
+ * @arg filter         filter object
+ *
+ * @return 0 if the object matches the filter or non-zero
+ *           if no filter procedure is available or if the
+ *           filter does not match.
+ */
+int nl_object_match(struct nl_object *obj, struct nl_object *filter)
+{
+       struct nl_cache_ops *ops = obj->ce_ops;
+
+       if (ops == filter->ce_ops &&
+           ops->co_filter && !ops->co_filter(obj, filter))
+               return 1;
+       else
+               return 0;
+}
+
+/** @} */
+
+/**
+ * @name Access Functions
+ * @{
+ */
+
+/**
+ * Get reference count of object
+ * @arg obj            object handle
+ */
+int nl_object_get_refcnt(struct nl_object *obj)
+{
+       return obj->ce_refcnt;
+}
+
+/**
+ * Get cache operations of object
+ * @arg obj            object handle
+ */
+struct nl_cache_ops *nl_object_get_ops(struct nl_object *obj)
+{
+       return obj->ce_ops;
+}
+
+/**
+ * Get cache this object is in
+ * @arg obj            object handle
+ * @return cache handle or NULL if object is not associated to a cache
+ */
+struct nl_cache *nl_object_get_cache(struct nl_object *obj)
+{
+       return obj->ce_cache;
+}
+
+/**
+ * Get the private data of object
+ * @arg obj            object handle
+ */
+inline void *nl_object_priv(struct nl_object *obj)
+{
+       return obj;
+}
+
+/** @} */
+
+/** @} */
diff --git a/lib/route/addr.c b/lib/route/addr.c
new file mode 100644 (file)
index 0000000..888daf1
--- /dev/null
@@ -0,0 +1,1278 @@
+/*
+ * lib/route/addr.c            Addresses
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ *                         Baruch Even <baruch@ev-en.org>,
+ *                         Mediatrix Telecom, inc. <ericb@mediatrix.com>
+ */
+
+/**
+ * @ingroup rtnl
+ * @defgroup rtaddr Addresses
+ * @brief
+ *
+ * @par 1) Address Addition
+ * @code
+ * // Allocate an empty address object to be filled out with the attributes
+ * // of the new address.
+ * struct rtnl_addr *addr = rtnl_addr_alloc();
+ *
+ * // Fill out the mandatory attributes of the new address. Setting the
+ * // local address will automatically set the address family and the
+ * // prefix length to the correct values.
+ * rtnl_addr_set_ifindex(addr, ifindex);
+ * rtnl_addr_set_local(addr, local_addr);
+ *
+ * // The label of the address can be specified, currently only supported
+ * // by IPv4 and DECnet.
+ * rtnl_addr_set_label(addr, "mylabel");
+ *
+ * // The peer address can be specified if necessary, in either case a peer
+ * // address will be sent to the kernel in order to fullfil the interface
+ * // requirements. If none is set, it will equal the local address.
+ * // Note: Real peer addresses are only supported by IPv4 for now.
+ * rtnl_addr_set_peer(addr, peer_addr);
+ *
+ * // In case you want to have the address have a scope other than global
+ * // it may be overwritten using rtnl_addr_set_scope(). The scope currently
+ * // cannot be set for IPv6 addresses.
+ * rtnl_addr_set_scope(addr, rtnl_str2scope("site"));
+ *
+ * // Broadcast and anycast address may be specified using the relevant
+ * // functions, the address family will be verified if one of the other
+ * // addresses has been set already. Currently only works for IPv4.
+ * rtnl_addr_set_broadcast(addr, broadcast_addr);
+ * rtnl_addr_set_anycast(addr, anycast_addr);
+ *
+ * // Build the netlink message and send it to the kernel, the operation will
+ * // block until the operation has been completed. Alternatively the required
+ * // netlink message can be built using rtnl_addr_build_add_request() to be
+ * // sent out using nl_send_auto_complete().
+ * rtnl_addr_add(handle, addr, 0);
+ *
+ * // Free the memory
+ * rtnl_addr_put(addr);
+ * @endcode
+ *
+ * @par 2) Address Deletion
+ * @code
+ * // Allocate an empty address object to be filled out with the attributes
+ * // matching the address to be deleted. Alternatively a fully equipped
+ * // address object out of a cache can be used instead.
+ * struct rtnl_addr *addr = rtnl_addr_alloc();
+ *
+ * // The only mandatory parameter besides the address family is the interface
+ * // index the address is on, i.e. leaving out all other parameters will
+ * // result in all addresses of the specified address family interface tuple
+ * // to be deleted.
+ * rtnl_addr_set_ifindex(addr, ifindex);
+ *
+ * // Specyfing the address family manually is only required if neither the
+ * // local nor peer address have been specified.
+ * rtnl_addr_set_family(addr, AF_INET);
+ *
+ * // Specyfing the local address is optional but the best choice to delete
+ * // specific addresses.
+ * rtnl_addr_set_local(addr, local_addr);
+ *
+ * // The label of the address can be specified, currently only supported
+ * // by IPv4 and DECnet.
+ * rtnl_addr_set_label(addr, "mylabel");
+ *
+ * // The peer address can be specified if necessary, in either case a peer
+ * // address will be sent to the kernel in order to fullfil the interface
+ * // requirements. If none is set, it will equal the local address.
+ * // Note: Real peer addresses are only supported by IPv4 for now.
+ * rtnl_addr_set_peer(addr, peer_addr);
+ *
+ * // Build the netlink message and send it to the kernel, the operation will
+ * // block until the operation has been completed. Alternatively the required
+ * // netlink message can be built using rtnl_addr_build_delete_request()
+ * // to be sent out using nl_send_auto_complete().
+ * rtnl_addr_delete(handle, addr, 0);
+ *
+ * // Free the memory
+ * rtnl_addr_put(addr);
+ * @endcode
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink/netlink.h>
+#include <netlink/route/rtnl.h>
+#include <netlink/route/addr.h>
+#include <netlink/route/route.h>
+#include <netlink/route/link.h>
+#include <netlink/utils.h>
+
+/** @cond SKIP */
+#define ADDR_ATTR_FAMILY       0x0001
+#define ADDR_ATTR_PREFIXLEN    0x0002
+#define ADDR_ATTR_FLAGS                0x0004
+#define ADDR_ATTR_SCOPE                0x0008
+#define ADDR_ATTR_IFINDEX      0x0010
+#define ADDR_ATTR_LABEL                0x0020
+#define ADDR_ATTR_CACHEINFO    0x0040
+#define ADDR_ATTR_PEER         0x0080
+#define ADDR_ATTR_LOCAL                0x0100
+#define ADDR_ATTR_BROADCAST    0x0200
+#define ADDR_ATTR_ANYCAST      0x0400
+#define ADDR_ATTR_MULTICAST    0x0800
+
+static struct nl_cache_ops rtnl_addr_ops;
+/** @endcond */
+
+static void addr_free_data(struct nl_object *obj)
+{
+       struct rtnl_addr *addr = nl_object_priv(obj);
+
+       if (!addr)
+               return;
+
+       nl_addr_put(addr->a_peer);
+       nl_addr_put(addr->a_local);
+       nl_addr_put(addr->a_bcast);
+       nl_addr_put(addr->a_anycast);
+       nl_addr_put(addr->a_multicast);
+}
+
+static struct nla_policy addr_policy[IFA_MAX+1] = {
+       [IFA_LABEL]     = { .type = NLA_STRING,
+                           .maxlen = IFNAMSIZ },
+       [IFA_CACHEINFO] = { .minlen = sizeof(struct ifa_cacheinfo) },
+};
+
+static int addr_msg_parser(struct sockaddr_nl *who, struct nlmsghdr *nlh,
+                          void *arg)
+{
+       struct rtnl_addr *addr;
+       struct nl_parser_param *pp = arg;
+       struct ifaddrmsg *ifa;
+       struct nlattr *tb[IFA_MAX+1];
+       int err = -ENOMEM, peer_prefix = 0;
+
+       addr = rtnl_addr_alloc();
+       if (!addr) {
+               err = nl_errno(ENOMEM);
+               goto errout;
+       }
+       addr->ce_msgtype = nlh->nlmsg_type;
+
+       err = nlmsg_parse(nlh, sizeof(*ifa), tb, IFA_MAX, addr_policy);
+       if (err < 0)
+               goto errout_free;
+
+       ifa = nlmsg_data(nlh);
+       addr->a_family = ifa->ifa_family;
+       addr->a_prefixlen = ifa->ifa_prefixlen;
+       addr->a_flags = ifa->ifa_flags;
+       addr->a_scope = ifa->ifa_scope;
+       addr->a_ifindex = ifa->ifa_index;
+
+       addr->a_mask = (ADDR_ATTR_FAMILY | ADDR_ATTR_PREFIXLEN |
+                       ADDR_ATTR_FLAGS | ADDR_ATTR_SCOPE | ADDR_ATTR_IFINDEX);
+
+       if (tb[IFA_LABEL]) {
+               nla_strlcpy(addr->a_label, tb[IFA_LABEL], IFNAMSIZ);
+               addr->a_mask |= ADDR_ATTR_LABEL;
+       }
+
+       if (tb[IFA_CACHEINFO]) {
+               struct ifa_cacheinfo *ca;
+               
+               ca = nla_data(tb[IFA_CACHEINFO]);
+               addr->a_cacheinfo.aci_prefered = ca->ifa_prefered;
+               addr->a_cacheinfo.aci_valid = ca->ifa_valid;
+               addr->a_cacheinfo.aci_cstamp = ca->cstamp;
+               addr->a_cacheinfo.aci_tstamp = ca->tstamp;
+               addr->a_mask |= ADDR_ATTR_CACHEINFO;
+       }
+
+       if (tb[IFA_LOCAL]) {
+               addr->a_local = nla_get_addr(tb[IFA_LOCAL], addr->a_family);
+               if (!addr->a_local)
+                       goto errout_free;
+               addr->a_mask |= ADDR_ATTR_LOCAL;
+       }
+
+       if (tb[IFA_ADDRESS]) {
+               struct nl_addr *a;
+
+               a = nla_get_addr(tb[IFA_ADDRESS], addr->a_family);
+               if (!a)
+                       goto errout_free;
+
+               /* IPv6 sends the local address as IFA_ADDRESS with
+                * no IFA_LOCAL, IPv4 sends both IFA_LOCAL and IFA_ADDRESS
+                * with IFA_ADDRESS being the peer address if they differ */
+               if (!tb[IFA_LOCAL] || !nl_addr_cmp(a, addr->a_local)) {
+                       nl_addr_put(addr->a_local);
+                       addr->a_local = a;
+                       addr->a_mask |= ADDR_ATTR_LOCAL;
+               } else {
+                       addr->a_peer = a;
+                       addr->a_mask |= ADDR_ATTR_PEER;
+                       peer_prefix = 1;
+               }
+       }
+
+       nl_addr_set_prefixlen(peer_prefix ? addr->a_peer : addr->a_local,
+                             addr->a_prefixlen);
+
+       if (tb[IFA_BROADCAST]) {
+               addr->a_bcast = nla_get_addr(tb[IFA_BROADCAST], addr->a_family);
+               if (!addr->a_bcast)
+                       goto errout_free;
+
+               addr->a_mask |= ADDR_ATTR_BROADCAST;
+       }
+
+       if (tb[IFA_ANYCAST]) {
+               addr->a_anycast = nla_get_addr(tb[IFA_ANYCAST], addr->a_family);
+               if (!addr->a_anycast)
+                       goto errout_free;
+
+               addr->a_mask |= ADDR_ATTR_ANYCAST;
+       }
+
+       if (tb[IFA_MULTICAST]) {
+               addr->a_multicast = nla_get_addr(tb[IFA_MULTICAST],
+                                                addr->a_family);
+               if (!addr->a_multicast)
+                       goto errout_free;
+
+               addr->a_mask |= ADDR_ATTR_MULTICAST;
+       }
+
+       err = pp->pp_cb((struct nl_object *) addr, pp);
+       if (err < 0)
+               goto errout_free;
+
+       return P_ACCEPT;
+
+errout_free:
+       rtnl_addr_free(addr);
+errout:
+       return err;
+}
+
+static int addr_request_update(struct nl_cache *cache, struct nl_handle *handle)
+{
+       return nl_rtgen_request(handle, RTM_GETADDR, AF_UNSPEC, NLM_F_DUMP);
+}
+
+static int addr_dump_brief(struct nl_object *obj, struct nl_dump_params *p)
+{
+       struct rtnl_addr *addr = (struct rtnl_addr *) obj;
+       struct nl_cache *link_cache;
+       char buf[128];
+
+       link_cache = nl_cache_mngt_require("route/link");
+
+       if (addr->a_mask & ADDR_ATTR_LOCAL)
+               dp_dump(p, "%s",
+                       nl_addr2str(addr->a_local, buf, sizeof(buf)));
+       else
+               dp_dump(p, "none");
+
+       if (addr->a_mask & ADDR_ATTR_PEER)
+               dp_dump(p, " peer %s",
+                       nl_addr2str(addr->a_peer, buf, sizeof(buf)));
+
+       dp_dump(p, " %s ", nl_af2str(addr->a_family, buf, sizeof(buf)));
+
+       if (link_cache)
+               dp_dump(p, "dev %s ",
+                       rtnl_link_i2name(link_cache, addr->a_ifindex,
+                                        buf, sizeof(buf)));
+       else
+               dp_dump(p, "dev %d ", addr->a_ifindex);
+
+       dp_dump(p, "scope %s",
+               rtnl_scope2str(addr->a_scope, buf, sizeof(buf)));
+
+       rtnl_addr_flags2str(addr->a_flags, buf, sizeof(buf));
+       if (buf[0])
+               dp_dump(p, " <%s>", buf);
+
+       dp_dump(p, "\n");
+
+       return 1;
+}
+
+static int addr_dump_full(struct nl_object *obj, struct nl_dump_params *p)
+{
+       struct rtnl_addr *addr = (struct rtnl_addr *) obj;
+       int line = addr_dump_brief(obj, p);
+       char buf[128];
+
+       if (addr->a_mask & (ADDR_ATTR_LABEL | ADDR_ATTR_BROADCAST |
+                           ADDR_ATTR_ANYCAST | ADDR_ATTR_MULTICAST)) {
+               dp_dump_line(p, line++, "  ");
+
+               if (addr->a_mask & ADDR_ATTR_LABEL)
+                       dp_dump(p, " label %s", addr->a_label);
+
+               if (addr->a_mask & ADDR_ATTR_BROADCAST)
+                       dp_dump(p, " broadcast %s",
+                               nl_addr2str(addr->a_bcast, buf, sizeof(buf)));
+
+               if (addr->a_mask & ADDR_ATTR_ANYCAST)
+                       dp_dump(p, " anycast %s",
+                               nl_addr2str(addr->a_anycast, buf,
+                                             sizeof(buf)));
+
+               if (addr->a_mask & ADDR_ATTR_MULTICAST)
+                       dp_dump(p, " multicast %s",
+                               nl_addr2str(addr->a_multicast, buf,
+                                             sizeof(buf)));
+
+               dp_dump(p, "\n");
+       }
+
+       if (addr->a_mask & ADDR_ATTR_CACHEINFO) {
+               struct rtnl_addr_cacheinfo *ci = &addr->a_cacheinfo;
+
+               dp_dump_line(p, line++, "   valid-lifetime %s",
+                            ci->aci_valid == 0xFFFFFFFFU ? "forever" :
+                            nl_msec2str(ci->aci_valid * 1000,
+                                          buf, sizeof(buf)));
+
+               dp_dump(p, " preferred-lifetime %s\n",
+                       ci->aci_prefered == 0xFFFFFFFFU ? "forever" :
+                       nl_msec2str(ci->aci_prefered * 1000,
+                                     buf, sizeof(buf)));
+
+               dp_dump_line(p, line++, "   created boot-time+%s ",
+                            nl_msec2str(addr->a_cacheinfo.aci_cstamp * 10,
+                                          buf, sizeof(buf)));
+                   
+               dp_dump(p, "last-updated boot-time+%s\n",
+                       nl_msec2str(addr->a_cacheinfo.aci_tstamp * 10,
+                                     buf, sizeof(buf)));
+       }
+
+       return line;
+}
+
+static int addr_dump_stats(struct nl_object *obj, struct nl_dump_params *p)
+{
+       return addr_dump_full(obj, p);
+}
+
+static int addr_dump_xml(struct nl_object *obj, struct nl_dump_params *p)
+{
+       struct rtnl_addr *addr = (struct rtnl_addr *) obj;
+       struct nl_cache *link_cache;
+       char buf[128];
+       int line = 0;
+
+       dp_dump_line(p, line++, "<address>\n");
+       dp_dump_line(p, line++, "  <family>%s</family>\n",
+                    nl_af2str(addr->a_family, buf, sizeof(buf)));
+
+       if (addr->a_mask & ADDR_ATTR_LOCAL)
+               dp_dump_line(p, line++, "  <local>%s</local>\n",
+                            nl_addr2str(addr->a_local, buf, sizeof(buf)));
+
+       if (addr->a_mask & ADDR_ATTR_PEER)
+               dp_dump_line(p, line++, "  <peer>%s</peer>\n",
+                            nl_addr2str(addr->a_peer, buf, sizeof(buf)));
+
+       if (addr->a_mask & ADDR_ATTR_BROADCAST)
+               dp_dump_line(p, line++, "  <broadcast>%s</broadcast>\n",
+                            nl_addr2str(addr->a_bcast, buf, sizeof(buf)));
+
+       if (addr->a_mask & ADDR_ATTR_ANYCAST)
+               dp_dump_line(p, line++, "  <anycast>%s</anycast>\n",
+                            nl_addr2str(addr->a_anycast, buf, sizeof(buf)));
+
+       if (addr->a_mask & ADDR_ATTR_MULTICAST)
+               dp_dump_line(p, line++, "  <multicast>%s</multicast>\n",
+                            nl_addr2str(addr->a_multicast, buf,
+                                          sizeof(buf)));
+
+       if (addr->a_mask & ADDR_ATTR_PREFIXLEN)
+               dp_dump_line(p, line++, "  <prefixlen>%u</prefixlen>\n",
+                            addr->a_prefixlen);
+       link_cache = nl_cache_mngt_require("route/link");
+
+       if (link_cache)
+               dp_dump_line(p, line++, "  <device>%s</device>\n",
+                            rtnl_link_i2name(link_cache, addr->a_ifindex,
+                                             buf, sizeof(buf)));
+       else
+               dp_dump_line(p, line++, "  <device>%u</device>\n",
+                            addr->a_ifindex);
+
+       if (addr->a_mask & ADDR_ATTR_SCOPE)
+               dp_dump_line(p, line++, "  <scope>%s</scope>\n",
+                            rtnl_scope2str(addr->a_scope, buf, sizeof(buf)));
+
+       if (addr->a_mask & ADDR_ATTR_LABEL)
+               dp_dump_line(p, line++, "  <label>%s</label>\n", addr->a_label);
+
+       rtnl_addr_flags2str(addr->a_flags, buf, sizeof(buf));
+       if (buf[0])
+               dp_dump_line(p, line++, "  <flags>%s</flags>\n", buf);
+
+       if (addr->a_mask & ADDR_ATTR_CACHEINFO) {
+               struct rtnl_addr_cacheinfo *ci = &addr->a_cacheinfo;
+
+               dp_dump_line(p, line++, "  <cacheinfo>\n");
+
+               dp_dump_line(p, line++, "    <valid>%s</valid>\n",
+                            ci->aci_valid == 0xFFFFFFFFU ? "forever" :
+                            nl_msec2str(ci->aci_valid * 1000,
+                                          buf, sizeof(buf)));
+
+               dp_dump_line(p, line++, "    <prefered>%s</prefered>\n",
+                            ci->aci_prefered == 0xFFFFFFFFU ? "forever" :
+                            nl_msec2str(ci->aci_prefered * 1000,
+                                        buf, sizeof(buf)));
+
+               dp_dump_line(p, line++, "    <created>%s</created>\n",
+                            nl_msec2str(addr->a_cacheinfo.aci_cstamp * 10,
+                                        buf, sizeof(buf)));
+
+               dp_dump_line(p, line++, "    <last-update>%s</last-update>\n",
+                            nl_msec2str(addr->a_cacheinfo.aci_tstamp * 10,
+                                        buf, sizeof(buf)));
+
+               dp_dump_line(p, line++, "  </cacheinfo>\n");
+       }
+
+       dp_dump_line(p, line++, "</address>\n");
+
+       return line;
+}
+
+static int addr_dump_env(struct nl_object *obj, struct nl_dump_params *p)
+{
+       struct rtnl_addr *addr = (struct rtnl_addr *) obj;
+       struct nl_cache *link_cache;
+       char buf[128];
+       int line = 0;
+
+       dp_dump_line(p, line++, "ADDR_FAMILY=%s\n",
+                    nl_af2str(addr->a_family, buf, sizeof(buf)));
+
+       if (addr->a_mask & ADDR_ATTR_LOCAL)
+               dp_dump_line(p, line++, "ADDR_LOCAL=%s\n",
+                            nl_addr2str(addr->a_local, buf, sizeof(buf)));
+
+       if (addr->a_mask & ADDR_ATTR_PEER)
+               dp_dump_line(p, line++, "ADDR_PEER=%s\n",
+                            nl_addr2str(addr->a_peer, buf, sizeof(buf)));
+
+       if (addr->a_mask & ADDR_ATTR_BROADCAST)
+               dp_dump_line(p, line++, "ADDR_BROADCAST=%s\n",
+                            nl_addr2str(addr->a_bcast, buf, sizeof(buf)));
+
+       if (addr->a_mask & ADDR_ATTR_ANYCAST)
+               dp_dump_line(p, line++, "ADDR_ANYCAST=%s\n",
+                            nl_addr2str(addr->a_anycast, buf, sizeof(buf)));
+
+       if (addr->a_mask & ADDR_ATTR_MULTICAST)
+               dp_dump_line(p, line++, "ADDR_MULTICAST=%s\n",
+                            nl_addr2str(addr->a_multicast, buf,
+                                          sizeof(buf)));
+
+       if (addr->a_mask & ADDR_ATTR_PREFIXLEN)
+               dp_dump_line(p, line++, "ADDR_PREFIXLEN=%u\n",
+                            addr->a_prefixlen);
+       link_cache = nl_cache_mngt_require("route/link");
+
+       dp_dump_line(p, line++, "ADDR_IFINDEX=%u\n", addr->a_ifindex);
+       if (link_cache)
+               dp_dump_line(p, line++, "ADDR_IFNAME=%s\n",
+                            rtnl_link_i2name(link_cache, addr->a_ifindex,
+                                             buf, sizeof(buf)));
+
+       if (addr->a_mask & ADDR_ATTR_SCOPE)
+               dp_dump_line(p, line++, "ADDR_SCOPE=%s\n",
+                            rtnl_scope2str(addr->a_scope, buf, sizeof(buf)));
+
+       if (addr->a_mask & ADDR_ATTR_LABEL)
+               dp_dump_line(p, line++, "ADDR_LABEL=%s\n", addr->a_label);
+
+       rtnl_addr_flags2str(addr->a_flags, buf, sizeof(buf));
+       if (buf[0])
+               dp_dump_line(p, line++, "ADDR_FLAGS=%s\n", buf);
+
+       if (addr->a_mask & ADDR_ATTR_CACHEINFO) {
+               struct rtnl_addr_cacheinfo *ci = &addr->a_cacheinfo;
+
+               dp_dump_line(p, line++, "ADDR_CACHEINFO_VALID=%s\n",
+                            ci->aci_valid == 0xFFFFFFFFU ? "forever" :
+                            nl_msec2str(ci->aci_valid * 1000,
+                                          buf, sizeof(buf)));
+
+               dp_dump_line(p, line++, "ADDR_CACHEINFO_PREFERED=%s\n",
+                            ci->aci_prefered == 0xFFFFFFFFU ? "forever" :
+                            nl_msec2str(ci->aci_prefered * 1000,
+                                        buf, sizeof(buf)));
+
+               dp_dump_line(p, line++, "ADDR_CACHEINFO_CREATED=%s\n",
+                            nl_msec2str(addr->a_cacheinfo.aci_cstamp * 10,
+                                        buf, sizeof(buf)));
+
+               dp_dump_line(p, line++, "ADDR_CACHEINFO_LASTUPDATE=%s\n",
+                            nl_msec2str(addr->a_cacheinfo.aci_tstamp * 10,
+                                        buf, sizeof(buf)));
+       }
+
+       return line;
+}
+
+static int addr_filter(struct nl_object *obj, struct nl_object *filter)
+{
+       struct rtnl_addr *o = (struct rtnl_addr *) obj;
+       struct rtnl_addr *f = (struct rtnl_addr *) filter;
+
+#define REQ(F) (f->a_mask & ADDR_ATTR_##F)
+#define AVAIL(F) (o->a_mask & ADDR_ATTR_##F)
+#define _O(F, EXPR) (REQ(F) && (!AVAIL(F) || (EXPR)))
+#define _C(F, N) (REQ(F) && (!AVAIL(F) || (o->N != f->N)))
+       if (_C(IFINDEX,    a_ifindex)                                   ||
+           _C(FAMILY,     a_family)                                    ||
+           _C(SCOPE,      a_scope)                                     ||
+           _O(FLAGS,      f->a_flags ^ (o->a_flags & f->a_flag_mask))  ||
+           _O(LABEL,      strcmp(o->a_label, f->a_label))              ||
+           _O(PEER,       nl_addr_cmp(o->a_peer, f->a_peer))           ||
+           _O(LOCAL,      nl_addr_cmp(o->a_local, f->a_local))         ||
+           _O(ANYCAST,    nl_addr_cmp(o->a_anycast, f->a_anycast))     ||
+           _O(MULTICAST,  nl_addr_cmp(o->a_multicast, f->a_multicast)) ||
+           _O(BROADCAST,  nl_addr_cmp(o->a_bcast, f->a_bcast)))
+               return 0;
+#undef REQ
+#undef AVAIL
+#undef _O
+#undef _C
+
+       return 1;
+}
+
+/**
+ * @name Address Object Creation/Deletion
+ * @{
+ */
+
+/**
+ * Allocate and initialize a new address object
+ * @note Free the memory after usage using rtnl_addr_put() or rtnl_addr_free().
+ * @return Newly allocated address object or NULL if an error occured.
+ */
+struct rtnl_addr *rtnl_addr_alloc(void)
+{
+       return (struct rtnl_addr *) nl_object_alloc_from_ops(&rtnl_addr_ops);
+}
+
+/**
+ * Give back a reference on a address object.
+ * @arg addr           Address object to be given back.
+ *
+ * Decrements the reference counter and frees the object if the
+ * last reference has been released.
+ */
+void rtnl_addr_put(struct rtnl_addr *addr)
+{
+       nl_object_put((struct nl_object *) addr);
+}
+
+/**
+ * Free an address object
+ * @arg addr           Address object to be freed.
+ *
+ * @note Always use rtnl_addr_put() unless you're absolutely sure
+ *       that no other user may have a reference on this object.
+ */
+void rtnl_addr_free(struct rtnl_addr *addr)
+{
+       nl_object_free((struct nl_object *) addr);
+}
+
+/** @} */
+
+/**
+ * @name Address Cache Management
+ * @{
+ */
+
+/**
+ * Allocate address cache and fill in all configured addresses
+ * @arg handle         Netlink handle.
+ *
+ * Allocates a new address cache, initializes it properly and updates it
+ * to include all addresses currently configured in the kernel.
+ *
+ * @note Free the memory after usage.
+ * @return Newly allocated cache or NULL if an error occured.
+ */
+struct nl_cache *rtnl_addr_alloc_cache(struct nl_handle *handle)
+{
+       struct nl_cache *cache;
+       
+       cache = nl_cache_alloc_from_ops(&rtnl_addr_ops);
+       if (!cache)
+               return NULL;
+
+       if (nl_cache_update(handle, cache) < 0) {
+               nl_cache_free(cache);
+               return NULL;
+       }
+
+       return cache;
+}
+
+/** @} */
+
+static struct nl_msg *build_addr_msg(struct rtnl_addr *tmpl, int cmd, int flags)
+{
+       struct nl_msg *msg;
+       struct ifaddrmsg am = {
+               .ifa_family = tmpl->a_family,
+               .ifa_index = tmpl->a_ifindex,
+               .ifa_prefixlen = tmpl->a_prefixlen,
+       };
+
+       if (tmpl->a_mask & ADDR_ATTR_SCOPE)
+               am.ifa_scope = tmpl->a_scope;
+       else {
+               /* compatibility hack */
+               if (tmpl->a_family == AF_INET &&
+                   tmpl->a_mask & ADDR_ATTR_LOCAL &&
+                   *((char *) nl_addr_get_binary_addr(tmpl->a_local)) == 127)
+                       am.ifa_scope = RT_SCOPE_HOST;
+               else
+                       am.ifa_scope = RT_SCOPE_UNIVERSE;
+       }
+
+       msg = nlmsg_build_simple(cmd, flags);
+       if (!msg)
+               goto nla_put_failure;
+
+       if (nlmsg_append(msg, &am, sizeof(am), 1) < 0)
+               goto nla_put_failure;
+
+       if (tmpl->a_mask & ADDR_ATTR_LOCAL)
+               NLA_PUT_ADDR(msg, IFA_LOCAL, tmpl->a_local);
+
+       if (tmpl->a_mask & ADDR_ATTR_PEER)
+               NLA_PUT_ADDR(msg, IFA_ADDRESS, tmpl->a_peer);
+       else
+               NLA_PUT_ADDR(msg, IFA_ADDRESS, tmpl->a_local);
+
+       if (tmpl->a_mask & ADDR_ATTR_LABEL)
+               NLA_PUT_STRING(msg, IFA_LABEL, tmpl->a_label);
+
+       if (tmpl->a_mask & ADDR_ATTR_BROADCAST)
+               NLA_PUT_ADDR(msg, IFA_BROADCAST, tmpl->a_bcast);
+
+       if (tmpl->a_mask & ADDR_ATTR_ANYCAST)
+               NLA_PUT_ADDR(msg, IFA_ANYCAST, tmpl->a_anycast);
+
+       return msg;
+
+nla_put_failure:
+       nlmsg_free(msg);
+       return NULL;
+}
+
+/**
+ * @name Address Addition
+ * @{
+ */
+
+/**
+ * Build netlink request message to request addition of new address
+ * @arg addr           Address object representing the new address.
+ * @arg flags          Additional netlink message flags.
+ *
+ * Builds a new netlink message requesting the addition of a new
+ * address. The netlink message header isn't fully equipped with
+ * all relevant fields and must thus be sent out via nl_send_auto_complete()
+ * or supplemented as needed.
+ *
+ * Minimal required attributes:
+ *   - interface index (rtnl_addr_set_ifindex())
+ *   - local address (rtnl_addr_set_local())
+ *
+ * The scope will default to universe except for loopback addresses in
+ * which case a host scope is used if not specified otherwise.
+ *
+ * @note Free the memory after usage using nlmsg_free().
+ * @return Newly allocated netlink message or NULL if an error occured.
+ */
+struct nl_msg *rtnl_addr_build_add_request(struct rtnl_addr *addr, int flags)
+{
+       int required = ADDR_ATTR_IFINDEX | ADDR_ATTR_FAMILY |
+                      ADDR_ATTR_PREFIXLEN | ADDR_ATTR_LOCAL;
+
+       if ((addr->a_mask & required) != required) {
+               nl_error(EINVAL, "Missing mandatory attributes, required are: "
+                                "ifindex, family, prefixlen, local address.");
+               return NULL;
+       }
+       
+       return build_addr_msg(addr, RTM_NEWADDR, NLM_F_CREATE | flags);
+}
+
+/**
+ * Request addition of new address
+ * @arg handle         Netlink handle.
+ * @arg addr           Address object representing the new address.
+ * @arg flags          Additional netlink message flags.
+ *
+ * Builds a netlink message by calling rtnl_addr_build_add_request(),
+ * sends the request to the kernel and waits for the next ACK to be
+ * received and thus blocks until the request has been fullfilled.
+ *
+ * @see rtnl_addr_build_add_request()
+ *
+ * @return 0 on sucess or a negative error if an error occured.
+ */
+int rtnl_addr_add(struct nl_handle *handle, struct rtnl_addr *addr, int flags)
+{
+       struct nl_msg *msg;
+       int err;
+
+       msg = rtnl_addr_build_add_request(addr, flags);
+       if (!msg)
+               return nl_get_errno();
+
+       err = nl_send_auto_complete(handle, msg);
+       nlmsg_free(msg);
+       if (err < 0)
+               return err;
+
+       return nl_wait_for_ack(handle);
+}
+
+/** @} */
+
+/**
+ * @name Address Deletion
+ * @{
+ */
+
+/**
+ * Build a netlink request message to request deletion of an address
+ * @arg addr           Address object to be deleteted.
+ * @arg flags          Additional netlink message flags.
+ *
+ * Builds a new netlink message requesting a deletion of an address.
+ * The netlink message header isn't fully equipped with all relevant
+ * fields and must thus be sent out via nl_send_auto_complete()
+ * or supplemented as needed.
+ *
+ * Minimal required attributes:
+ *   - interface index (rtnl_addr_set_ifindex())
+ *   - address family (rtnl_addr_set_family())
+ *
+ * Optional attributes:
+ *   - local address (rtnl_addr_set_local())
+ *   - label (rtnl_addr_set_label(), IPv4/DECnet only)
+ *   - peer address (rtnl_addr_set_peer(), IPv4 only)
+ *
+ * @note Free the memory after usage using nlmsg_free().
+ * @return Newly allocated netlink message or NULL if an error occured.
+ */
+struct nl_msg *rtnl_addr_build_delete_request(struct rtnl_addr *addr, int flags)
+{
+       int required = ADDR_ATTR_IFINDEX | ADDR_ATTR_FAMILY;
+
+       if ((addr->a_mask & required) != required) {
+               nl_error(EINVAL, "Missing mandatory attributes, required are: "
+                                "ifindex, family");
+               return NULL;
+       }
+       
+       return build_addr_msg(addr, RTM_DELADDR, flags);
+}
+
+/**
+ * Request deletion of an address
+ * @arg handle         Netlink handle.
+ * @arg addr           Address object to be deleted.
+ * @arg flags          Additional netlink message flags.
+ *
+ * Builds a netlink message by calling rtnl_addr_build_delete_request(),
+ * sends the request to the kernel and waits for the next ACK to be
+ * received and thus blocks until the request has been fullfilled.
+ *
+ * @see rtnl_addr_build_delete_request();
+ *
+ * @return 0 on sucess or a negative error if an error occured.
+ */
+int rtnl_addr_delete(struct nl_handle *handle, struct rtnl_addr *addr,
+                    int flags)
+{
+       struct nl_msg *msg;
+       int err;
+
+       msg = rtnl_addr_build_delete_request(addr, flags);
+       if (!msg)
+               return nl_get_errno();
+
+       err = nl_send_auto_complete(handle, msg);
+       nlmsg_free(msg);
+       if (err < 0)
+               return err;
+
+       return nl_wait_for_ack(handle);
+}
+
+/** @} */
+
+/**
+ * @name Attribute Access
+ * @{
+ */
+
+/**
+ * Set label of address object
+ * @arg addr           Address object to be modified.
+ * @arg label          New address label.
+ * 
+ * @note The maximum size of an address label is IFNAMSIZ.
+ */
+void rtnl_addr_set_label(struct rtnl_addr *addr, const char *label)
+{
+       strncpy(addr->a_label, label, sizeof(addr->a_label) - 1);
+       addr->a_mask |= ADDR_ATTR_LABEL;
+}
+
+/**
+ * Get label of address object
+ * @arg addr           Address object.
+ * @return Address label or NULL if not set.
+ */
+char *rtnl_addr_get_label(struct rtnl_addr *addr)
+{
+       if (addr->a_mask & ADDR_ATTR_LABEL)
+               return addr->a_label;
+       else
+               return NULL;
+}
+
+/**
+ * Set interface index of address object
+ * @arg addr           Address object ot be modified.
+ * @arg ifindex                New interface index this address is on.
+ */
+void rtnl_addr_set_ifindex(struct rtnl_addr *addr, int ifindex)
+{
+       addr->a_ifindex = ifindex;
+       addr->a_mask |= ADDR_ATTR_IFINDEX;
+}
+
+/**
+ * Get interface index of address object
+ * @arg addr           Address object.
+ * @return Interface index address is on or RTNL_LINK_NOT_FOUND if not set.
+ */
+int rtnl_addr_get_ifindex(struct rtnl_addr *addr)
+{
+       if (addr->a_mask & ADDR_ATTR_IFINDEX)
+               return addr->a_ifindex;
+       else
+               return RTNL_LINK_NOT_FOUND;
+}
+
+/**
+ * Set address family of address object.
+ * @arg addr           Address object to be modified.
+ * @arg family         New address family
+ *
+ * @note The address family is set automatically if one of the addresses
+ *       is set and the family hasn't been specified yet. Setting it manually
+ *       can be used to enforce family validation while setting addresses.
+ */
+void rtnl_addr_set_family(struct rtnl_addr *addr, int family)
+{
+       addr->a_family = family;
+       addr->a_mask |= ADDR_ATTR_FAMILY;
+}
+
+/**
+ * Get address family of address object.
+ * @arg addr           Address object.
+ * @return Address family or AF_UNSPEC if not set.
+ */
+int rtnl_addr_get_family(struct rtnl_addr *addr)
+{
+       if (addr->a_mask & ADDR_ATTR_FAMILY)
+               return addr->a_family;
+       else
+               return AF_UNSPEC;
+}
+
+/**
+ * Set prefix length of address object.
+ * @arg addr           Address object to be modified.
+ * @arg prefix         New prefix length.
+ */
+void rtnl_addr_set_prefixlen(struct rtnl_addr *addr, int prefix)
+{
+       addr->a_prefixlen = prefix;
+       addr->a_mask |= ADDR_ATTR_PREFIXLEN;
+}
+
+/**
+ * Get prefix length of address object.
+ * @arg addr           Address object.
+ * @return Prefix length or a negative number if not set.
+ */
+int rtnl_addr_get_prefixlen(struct rtnl_addr *addr)
+{
+       if (addr->a_mask & ADDR_ATTR_PREFIXLEN)
+               return addr->a_prefixlen;
+       else
+               return -1;
+}
+
+/**
+ * Set scope of address object.
+ * @arg addr           Address object to be modified.
+ * @arg scope          New scope.
+ */
+void rtnl_addr_set_scope(struct rtnl_addr *addr, int scope)
+{
+       addr->a_scope = scope;
+       addr->a_mask |= ADDR_ATTR_SCOPE;
+}
+
+/**
+ * Get scope of address object.
+ * @arg addr           Address object.
+ * @return Scope or a negative number if not set.
+ */
+int rtnl_addr_get_scope(struct rtnl_addr *addr)
+{
+       if (addr->a_mask & ADDR_ATTR_SCOPE)
+               return addr->a_scope;
+       else
+               return -1;
+}
+
+/**
+ * Set flags of address object.
+ * @arg addr           Address object to be modified.
+ * @arg flags          Additional flags to set.
+ *
+ * @note Existing flags that have been set will not be overwritten.
+ */
+void rtnl_addr_set_flags(struct rtnl_addr *addr, unsigned int flags)
+{
+       addr->a_flag_mask |= flags;
+       addr->a_flags |= flags;
+       addr->a_mask |= ADDR_ATTR_FLAGS;
+}
+
+/**
+ * Unset flags of address object.
+ * @arg addr           Address object to be modified.
+ * @arg flags          Flags to unset.
+ */
+void rtnl_addr_unset_flags(struct rtnl_addr *addr, unsigned int flags)
+{
+       addr->a_flag_mask |= flags;
+       addr->a_flags &= ~flags;
+       addr->a_mask |= ADDR_ATTR_FLAGS;
+}
+
+/**
+ * Get flags of address object.
+ * @arg addr           Address object.
+ * @return Flags in form of a bitmask.
+ */
+unsigned int rtnl_addr_get_flags(struct rtnl_addr *addr)
+{
+       return addr->a_flags;
+}
+
+static inline int __assign_addr(struct rtnl_addr *addr, struct nl_addr **pos,
+                               struct nl_addr *new, int flag)
+{
+       if (addr->a_mask & ADDR_ATTR_FAMILY) {
+               if (new->a_family != addr->a_family)
+                       return nl_error(EINVAL, "Address family mismatch");
+       } else
+               addr->a_family = new->a_family;
+
+       if (*pos)
+               nl_addr_put(*pos);
+
+       *pos = nl_addr_get(new);
+       addr->a_mask |= (flag | ADDR_ATTR_FAMILY);
+
+       return 0;
+}
+
+/**
+ * Set local address of address object.
+ * @arg addr           Address object to be modified.
+ * @arg local          New local address.
+ *
+ * Assigns the new local address to the specified address object. The
+ * address is validated against the address family if set already via
+ * either rtnl_addr_set_family() or by setting one of the other addresses.
+ * The assignment fails if the address families mismatch. In case the
+ * address family has not been specified yet, the address family of the
+ * new address is elected to be the new requirement.
+ *
+ * @note The address may not contain a prefix length if the peer address
+ *       has been specified already.
+ * 
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_addr_set_local(struct rtnl_addr *addr, struct nl_addr *local)
+{
+       int err;
+
+       err = __assign_addr(addr, &addr->a_local, local, ADDR_ATTR_LOCAL);
+       if (err < 0)
+               return err;
+
+       if (!(addr->a_mask & ADDR_ATTR_PEER)) {
+               addr->a_prefixlen = nl_addr_get_prefixlen(addr->a_local);
+               addr->a_mask |= ADDR_ATTR_PREFIXLEN;
+       }
+
+       return 0;
+}
+
+/**
+ * Get local address of address object.
+ * @arg addr           Address object.
+ * @return Local address or NULL if not set.
+ */
+struct nl_addr *rtnl_addr_get_local(struct rtnl_addr *addr)
+{
+       if (addr->a_mask & ADDR_ATTR_LOCAL)
+               return addr->a_local;
+       else
+               return NULL;
+}
+
+/**
+ * Set peer address of address object.
+ * @arg addr           Address object to be modified.
+ * @arg peer           New peer address.
+ *
+ * Assigns the new peer address to the specified address object. The
+ * address is validated against the address family if set already via
+ * either rtnl_addr_set_family() or by setting one of the other addresses.
+ * The assignment fails if the address families mismatch. In case the
+ * address family has not been specified yet, the address family of this
+ * new address is elected to be the requirement.
+ * 
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_addr_set_peer(struct rtnl_addr *addr, struct nl_addr *peer)
+{
+       return __assign_addr(addr, &addr->a_peer, peer, ADDR_ATTR_PEER);
+
+       addr->a_prefixlen = nl_addr_get_prefixlen(addr->a_peer);
+       addr->a_mask |= ADDR_ATTR_PREFIXLEN;
+
+       return 0;
+}
+
+/**
+ * Get peer address of address object.
+ * @arg addr           Adress object.
+ * @return Peer address or NULL if not set.
+ */
+struct nl_addr *rtnl_addr_get_peer(struct rtnl_addr *addr)
+{
+       if (addr->a_mask & ADDR_ATTR_PEER)
+               return addr->a_peer;
+       else
+               return NULL;
+}
+
+/**
+ * Set broadcast address of address object.
+ * @arg addr           Address object to be modified.
+ * @arg bcast          New broadcast address.
+ *
+ * Assigns the new broadcast address to the specified address object. The
+ * address is validated against the address family if set already via
+ * either rtnl_addr_set_family() or by setting one of the other addresses.
+ * The assignment fails if the address families mismatch. In case the
+ * address family has not been specified yet, the address family of this
+ * new address is elected to be the requirement.
+ * 
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_addr_set_broadcast(struct rtnl_addr *addr, struct nl_addr *bcast)
+{
+       return __assign_addr(addr, &addr->a_bcast, bcast, ADDR_ATTR_BROADCAST);
+}
+
+/**
+ * Get broadcast address of address object.
+ * @arg addr           Address object.
+ * @return Broadcast address or NULL if not set.
+ */
+struct nl_addr *rtnl_addr_get_broadcast(struct rtnl_addr *addr)
+{
+       if (addr->a_mask & ADDR_ATTR_BROADCAST)
+               return addr->a_bcast;
+       else
+               return NULL;
+}
+
+/**
+ * Set anycast address of address object.
+ * @arg addr           Address object to be modified.
+ * @arg anycast                New anycast address.
+ *
+ * Assigns the new anycast address to the specified address object. The
+ * address is validated against the address family if set already via
+ * either rtnl_addr_set_family() or by setting one of the other addresses.
+ * The assignment fails if the address families mismatch. In case the
+ * address family has not been specified yet, the address family of this
+ * new address is elected to be the requirement.
+ * 
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_addr_set_anycast(struct rtnl_addr *addr, struct nl_addr *anycast)
+{
+       return __assign_addr(addr, &addr->a_anycast, anycast,
+                            ADDR_ATTR_ANYCAST);
+}
+
+/**
+ * Get anycast address of address object.
+ * @arg addr           Address object.
+ * @return Anycast address or NULL if not set.
+ */
+struct nl_addr *rtnl_addr_get_anycast(struct rtnl_addr *addr)
+{
+       if (addr->a_mask & ADDR_ATTR_ANYCAST)
+               return addr->a_anycast;
+       else
+               return NULL;
+}
+
+/**
+ * Set multicast address of address object.
+ * @arg addr           Address object to be modified.
+ * @arg multicast      New multicast address.
+ *
+ * Assigns the new multicast address to the specified address object. The
+ * address is validated against the address family if set already via
+ * either rtnl_addr_set_family() or by setting one of the other addresses.
+ * The assignment fails if the address families mismatch. In case the
+ * address family has not been specified yet, the address family of this
+ * new address is elected to be the requirement.
+ * 
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_addr_set_multicast(struct rtnl_addr *addr, struct nl_addr *multicast)
+{
+       return __assign_addr(addr, &addr->a_multicast, multicast,
+                            ADDR_ATTR_MULTICAST);
+}
+
+/**
+ * Get multicast address of address object.
+ * @arg addr           Address object.
+ * @return Multicast address or NULL if not set.
+ */
+struct nl_addr *rtnl_addr_get_multicast(struct rtnl_addr *addr)
+{
+       if (addr->a_mask & ADDR_ATTR_MULTICAST)
+               return addr->a_multicast;
+       else
+               return NULL;
+}
+
+/** @} */
+
+/**
+ * @name Address Flags Translations
+ * @{
+ */
+
+static struct trans_tbl addr_flags[] = {
+       __ADD(IFA_F_SECONDARY, secondary)
+       __ADD(IFA_F_DEPRECATED, deprecated)
+       __ADD(IFA_F_TENTATIVE, tentative)
+       __ADD(IFA_F_PERMANENT, permanent)
+};
+
+/**
+ * Convert address flags to character string.
+ * @arg flags          Address flags.
+ * @arg buf            Destination buffer.
+ * @arg size           Size of destination buffer.
+ *
+ * Converts address flags to a character string separated by commas and
+ * stores the resulting character string in the specified destination buffer.
+ *
+ * @return Formatted flags as character string.
+ */
+char *rtnl_addr_flags2str(int flags, char *buf, size_t size)
+{
+       return __flags2str(flags, buf, size, addr_flags,
+                          ARRAY_SIZE(addr_flags));
+}
+
+/**
+ * Convert character string to address flags.
+ * @arg name           Name of address flags.
+ *
+ * Converts the provided character string specifying any number of address
+ * flags separated by commas to the corresponding numeric bitmask.
+ *
+ * @return Address flags in form of a bitmask.
+ */
+int rtnl_addr_str2flags(const char *name)
+{
+       return __str2flags(name, addr_flags, ARRAY_SIZE(addr_flags));
+}
+
+/** @} */
+
+static struct nl_cache_ops rtnl_addr_ops = {
+       .co_name                = "route/addr",
+       .co_size                = sizeof(struct rtnl_addr),
+       .co_hdrsize             = sizeof(struct ifaddrmsg),
+       .co_msgtypes            = {
+                                       { RTM_NEWADDR, "new" },
+                                       { RTM_DELADDR, "delete" },
+                                       { RTM_GETADDR, "get" },
+                                       { -1, NULL },
+                                 },
+       .co_protocol            = NETLINK_ROUTE,
+       .co_request_update      = addr_request_update,
+       .co_msg_parser          = addr_msg_parser,
+       .co_free_data           = addr_free_data,
+       .co_dump[NL_DUMP_BRIEF] = addr_dump_brief,
+       .co_dump[NL_DUMP_FULL]  = addr_dump_full,
+       .co_dump[NL_DUMP_STATS] = addr_dump_stats,
+       .co_dump[NL_DUMP_XML]   = addr_dump_xml,
+       .co_dump[NL_DUMP_ENV]   = addr_dump_env,
+       .co_filter              = addr_filter,
+};
+
+static void __init addr_init(void)
+{
+       nl_cache_mngt_register(&rtnl_addr_ops);
+}
+
+static void __exit neigh_exit(void)
+{
+       nl_cache_mngt_unregister(&rtnl_addr_ops);
+}
+
+/** @} */
diff --git a/lib/route/class.c b/lib/route/class.c
new file mode 100644 (file)
index 0000000..b6a81e9
--- /dev/null
@@ -0,0 +1,611 @@
+/*
+ * lib/route/class.c            Queueing Classes
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup tc
+ * @defgroup class Classes
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink-tc.h>
+#include <netlink/netlink.h>
+#include <netlink/route/tc.h>
+#include <netlink/route/class.h>
+#include <netlink/route/class-modules.h>
+#include <netlink/route/qdisc.h>
+#include <netlink/route/classifier.h>
+#include <netlink/utils.h>
+
+/** @cond SKIP */
+static struct nl_cache_ops rtnl_class_ops;
+/** @endcond */
+
+static struct rtnl_class_ops *class_ops_list;
+
+static struct rtnl_class_ops *class_lookup_ops(const char *kind)
+{
+       struct rtnl_class_ops *ops;
+
+       for (ops = class_ops_list; ops; ops = ops->co_next)
+               if (!strcmp(kind, ops->co_kind))
+                       return ops;
+
+       return NULL;
+}
+
+static inline struct rtnl_class_ops *class_ops(struct rtnl_class *class)
+{
+       if (!class->c_ops)
+               class->c_ops = class_lookup_ops(class->c_kind);
+
+       return class->c_ops;
+}
+
+/**
+ * @name Class Module API
+ * @{
+ */
+
+/**
+ * Register a class module
+ * @arg ops            class module operations
+ */
+int rtnl_class_register(struct rtnl_class_ops *ops)
+{
+       struct rtnl_class_ops *o, **op;
+
+       if (!ops->co_kind[0])
+               BUG();
+
+       for (op = &class_ops_list; (o = *op) != NULL; op = &o->co_next)
+               if (!strcasecmp(ops->co_kind, o->co_kind))
+                       return nl_errno(EEXIST);
+
+       ops->co_next = NULL;
+       *op = ops;
+
+       return 0;
+}
+
+/**
+ * Unregister a class module
+ * @arg ops            class module operations
+ */
+int rtnl_class_unregister(struct rtnl_class_ops *ops)
+{
+       struct rtnl_class_ops *o, **op;
+
+       for (op = &class_ops_list; (o = *op) != NULL; op = &o->co_next)
+               if (!strcasecmp(ops->co_kind, o->co_kind))
+                       break;
+
+       if (!o)
+               return nl_errno(ENOENT);
+
+       *op = ops->co_next;
+
+       return 0;
+}
+
+/** @} */
+
+static int class_msg_parser(struct sockaddr_nl *who, struct nlmsghdr *n,
+                           void *arg)
+{
+       int err;
+       struct nl_parser_param *pp = arg;
+       struct rtnl_class *class;
+       struct rtnl_class_ops *ops;
+
+       class = rtnl_class_alloc();
+       if (!class) {
+               err = nl_errno(ENOMEM);
+               goto errout;
+       }
+       class->ce_msgtype = n->nlmsg_type;
+
+       err = tca_msg_parser(n, (struct rtnl_tca *) class);
+       if (err < 0)
+               goto errout_free;
+
+       ops = class_ops(class);
+       if (ops && ops->co_msg_parser) {
+               err = ops->co_msg_parser(class);
+               if (err < 0)
+                       goto errout_free;
+       }
+
+       err = pp->pp_cb((struct nl_object *) class, pp);
+       if (err < 0)
+               goto errout_free;
+
+       return P_ACCEPT;
+
+errout_free:
+       rtnl_class_put(class);
+errout:
+       return err;
+}
+
+static int class_request_update(struct nl_cache *cache,
+                               struct nl_handle *handle)
+{
+       struct tcmsg tchdr = {
+               .tcm_family = AF_UNSPEC,
+               .tcm_ifindex = cache->c_iarg1,
+       };
+
+       return nl_send_simple(handle, RTM_GETTCLASS, NLM_F_DUMP, &tchdr,
+                             sizeof(tchdr));
+}
+
+static void class_free_data(struct nl_object *obj)
+{
+       struct rtnl_class *class = (struct rtnl_class *) obj;
+       struct rtnl_class_ops *ops;
+       
+       tca_free_data((struct rtnl_tca *) class);
+
+       ops = class_ops(class);
+       if (ops && ops->co_free_data)
+               ops->co_free_data(class);
+}
+
+static int class_dump_brief(struct nl_object *obj, struct nl_dump_params *p)
+{
+       struct rtnl_class *class = (struct rtnl_class *) obj;
+       struct rtnl_class_ops *ops;
+
+       int line = tca_dump_brief((struct rtnl_tca *) class, "class", p, 0);
+
+       ops = class_ops(class);
+       if (ops && ops->co_dump[NL_DUMP_BRIEF])
+               line = ops->co_dump[NL_DUMP_BRIEF](class, p, line);
+       dp_dump(p, "\n");
+
+       return line;
+}
+
+static int class_dump_full(struct nl_object *obj, struct nl_dump_params *p)
+{
+       struct rtnl_class *class = (struct rtnl_class *) obj;
+       struct rtnl_class_ops *ops;
+       int line;
+
+       line = class_dump_brief(obj, p);
+       line = tca_dump_full((struct rtnl_tca *) class, p, line);
+       
+       if (class->c_info) {
+               char buf[32];
+               dp_dump(p, "child-qdisc %s ",
+                       rtnl_tc_handle2str(class->c_info, buf, sizeof(buf)));
+       }
+
+       ops = class_ops(class);
+       if (ops && ops->co_dump[NL_DUMP_FULL])
+               line = ops->co_dump[NL_DUMP_FULL](class, p, line);
+       else if (!class->c_info)
+               dp_dump(p, "noop (no leaf qdisc)");
+
+       dp_dump(p, "\n");
+
+       return line;
+}
+
+static int class_dump_stats(struct nl_object *obj, struct nl_dump_params *p)
+{
+       struct rtnl_class *class = (struct rtnl_class *) obj;
+       struct rtnl_class_ops *ops;
+       int line;
+
+       line = class_dump_full(obj, p);
+       line = tca_dump_stats((struct rtnl_tca *) class, p, line);
+       dp_dump(p, "\n");
+
+       ops = class_ops(class);
+       if (ops && ops->co_dump[NL_DUMP_STATS])
+               line = ops->co_dump[NL_DUMP_STATS](class, p, line);
+
+       return line;
+}
+
+static int class_filter(struct nl_object *obj, struct nl_object *filter)
+{
+       return tca_filter((struct rtnl_tca *) obj, (struct rtnl_tca *) filter);
+}
+
+/**
+ * @name Class Addition/Modification
+ * @{
+ */
+
+static struct nl_msg *class_build(struct rtnl_class *class, int type, int flags)
+{
+       struct rtnl_class_ops *ops;
+       struct nl_msg *msg;
+       int err;
+
+       msg = tca_build_msg((struct rtnl_tca *) class, type, flags);
+       if (!msg)
+               goto errout;
+
+       ops = class_ops(class);
+       if (ops && ops->co_get_opts) {
+               struct nl_msg *opts;
+               
+               opts = ops->co_get_opts(class);
+               if (opts) {
+                       err = nla_put_nested(msg, TCA_OPTIONS, opts);
+                       nlmsg_free(opts);
+                       if (err < 0)
+                               goto errout;
+               }
+       }
+
+       return msg;
+errout:
+       nlmsg_free(msg);
+       return NULL;
+}
+
+/**
+ * Build a netlink message to add a new class
+ * @arg class          class to add 
+ * @arg flags          additional netlink message flags
+ *
+ * Builds a new netlink message requesting an addition of a class.
+ * The netlink message header isn't fully equipped with all relevant
+ * fields and must be sent out via nl_send_auto_complete() or
+ * supplemented as needed. 
+ *
+ * Common message flags
+ *   - NLM_F_REPLACE - replace possibly existing classes
+ *
+ * @return New netlink message
+ */
+struct nl_msg *rtnl_class_build_add_request(struct rtnl_class *class, int flags)
+{
+       return class_build(class, RTM_NEWTCLASS, NLM_F_CREATE | flags);
+}
+
+/**
+ * Add a new class
+ * @arg handle         netlink handle
+ * @arg class          class to delete
+ * @arg flags          additional netlink message flags
+ *
+ * Builds a netlink message by calling rtnl_qdisc_build_add_request(),
+ * sends the request to the kernel and waits for the next ACK to be
+ * received and thus blocks until the request has been processed.
+ *
+ * Common message flags
+ *   - NLM_F_REPLACE - replace possibly existing classes
+ *
+ * @return 0 on success or a negative error code
+ */
+int rtnl_class_add(struct nl_handle *handle, struct rtnl_class *class,
+                  int flags)
+{
+       struct nl_msg *msg;
+       int err;
+
+       msg = rtnl_class_build_add_request(class, flags);
+       if (!msg)
+               return nl_errno(ENOMEM);
+
+       err = nl_send_auto_complete(handle, msg);
+       if (err < 0)
+               return err;
+
+       nlmsg_free(msg);
+       return nl_wait_for_ack(handle);
+}
+
+/** @} */
+
+/**
+ * @name General
+ * @{
+ */
+
+/**
+ * Allocate a new class object
+ * @return New class object
+ */
+struct rtnl_class *rtnl_class_alloc(void)
+{
+       return (struct rtnl_class *) nl_object_alloc_from_ops(&rtnl_class_ops);
+}
+
+/**
+ * Give back reference on rclass object.
+ * @arg class          Class object to be given back.
+ *
+ * Decrements the reference counter and frees the object if the
+ * last reference has been released.
+ */
+void rtnl_class_put(struct rtnl_class *class)
+{
+       nl_object_put((struct nl_object *) class);
+}
+
+/**
+ * Free class object.
+ * @arg class          Class object to be freed.
+ *
+ * @note Always use rtnl_class_put() unless you're absolutely sure
+ *       that no other user may have a reference on this object.
+ */
+void rtnl_class_free(struct rtnl_class *class)
+{
+       nl_object_free((struct nl_object *) class);
+}
+
+/**
+ * Build a class cache including all classes attached to the specified interface
+ * @arg handle         netlink handle
+ * @arg ifindex                interface index of the link the classes are
+ *                      attached to.
+ *
+ * Allocates a new cache, initializes it properly and updates it to
+ * include all classes attached to the specified interface.
+ *
+ * @note The caller is responsible for destroying and freeing the
+ *       cache after using it.
+ * @return The cache or NULL if an error has occured.
+ */
+struct nl_cache * rtnl_class_alloc_cache(struct nl_handle *handle, int ifindex)
+{
+       struct nl_cache * cache;
+       
+       cache = nl_cache_alloc_from_ops(&rtnl_class_ops);
+       if (!cache)
+               return NULL;
+
+       cache->c_iarg1 = ifindex;
+       
+       if (nl_cache_update(handle, cache) < 0) {
+               nl_cache_free(cache);
+               return NULL;
+       }
+
+       return cache;
+}
+
+/** @} */
+
+/**
+ * @name Leaf Qdisc Access
+ * @{
+ */
+
+/**
+ * Determine if the class has a leaf qdisc attached
+ * @arg class          class to check
+ */
+int rtnl_class_has_leaf_qdisc(struct rtnl_class *class)
+{
+       return !!class->c_info;
+}
+
+/**
+ * Lookup the leaf qdisc of a class
+ * @arg class          the parent class
+ * @arg cache          a qdisc cache including at laest all qdiscs of the
+ *                      interface the specified class is attached to
+ * @return The qdisc from the cache or NULL if the class has no leaf qdisc
+ */
+struct rtnl_qdisc *rtnl_class_leaf_qdisc(struct rtnl_class *class,
+                                        struct nl_cache *cache)
+{
+       struct rtnl_qdisc *leaf;
+
+       if (!rtnl_class_has_leaf_qdisc(class))
+               return NULL;
+
+       leaf = rtnl_qdisc_get_by_parent(cache, class->c_ifindex,
+                                       class->c_handle);
+       if (!leaf || leaf->q_handle != class->c_info)
+               return NULL;
+
+       return leaf;
+}
+
+/** @} */
+
+/**
+ * @name Iterators
+ * @{
+ */
+
+/**
+ * Call a callback for each child class of a class
+ * @arg class          the parent class
+ * @arg cache          a class cache including all classes of the interface
+ *                      the specified class is attached to
+ * @arg cb              callback function
+ * @arg arg             argument to be passed to callback function
+ */
+void rtnl_class_foreach_child(struct rtnl_class *class, struct nl_cache *cache,
+                             void (*cb)(struct nl_object *, void *), void *arg)
+{
+       struct rtnl_class *filter;
+       
+       filter = rtnl_class_alloc();
+       if (!filter)
+               return;
+
+       rtnl_class_set_parent(filter, class->c_handle);
+       rtnl_class_set_ifindex(filter, class->c_ifindex);
+       rtnl_class_set_kind(filter, class->c_kind);
+
+       nl_cache_foreach_filter(cache, (struct nl_object *) filter, cb, arg);
+       rtnl_class_put(filter);
+}
+
+/**
+ * Call a callback for each classifier attached to the class
+ * @arg class          the parent class
+ * @arg cache          a filter cache including at least all the filters
+ *                      attached to the specified class
+ * @arg cb              callback function
+ * @arg arg             argument to be passed to callback function
+ */
+void rtnl_class_foreach_cls(struct rtnl_class *class, struct nl_cache *cache,
+                           void (*cb)(struct nl_object *, void *), void *arg)
+{
+       struct rtnl_cls *filter;
+
+       filter = rtnl_cls_alloc();
+       if (!filter)
+               return;
+
+       rtnl_cls_set_ifindex(filter, class->c_ifindex);
+       rtnl_cls_set_parent(filter, class->c_parent);
+
+       nl_cache_foreach_filter(cache, (struct nl_object *) filter, cb, arg);
+       rtnl_cls_put(filter);
+}
+
+/** @} */
+
+/**
+ * @name Attribute Modifications
+ * @{
+ */
+
+/**
+ * Set the interface index of a class to the specified value
+ * @arg class          class to be changed
+ * @arg ifindex                new interface index
+ */
+void rtnl_class_set_ifindex(struct rtnl_class *class, int ifindex)
+{
+       tca_set_ifindex((struct rtnl_tca *) class, ifindex);
+}
+
+/**
+ * Get the interface index of a class
+ * @arg class          class handle
+ * @return Interface index or RTNL_LINK_NOT_FOUND if not set
+ */
+int rtnl_class_get_ifindex(struct rtnl_class *class)
+{
+       return tca_get_ifindex((struct rtnl_tca *) class);
+}
+
+
+/**
+ * Set the handle of a class to the specified value
+ * @arg class          class to be changed
+ * @arg handle         new handle
+ */
+void rtnl_class_set_handle(struct rtnl_class *class, uint32_t handle)
+{
+       tca_set_handle((struct rtnl_tca *) class, handle);
+}
+
+/**
+ * Get the handle of a class
+ * @arg class          class handle
+ * @return Handle or 0 if not set
+ */
+uint32_t rtnl_class_get_handle(struct rtnl_class *class)
+{
+       return tca_get_handle((struct rtnl_tca *) class);
+}
+
+/**
+ * Set the parent handle of a class to the specified value
+ * @arg class          class to be changed
+ * @arg parent         new parent handle
+ */
+void rtnl_class_set_parent(struct rtnl_class *class, uint32_t parent)
+{
+       tca_set_parent((struct rtnl_tca *) class, parent);
+}
+
+/**
+ * Get the parent handle of a class
+ * @arg class          class handle
+ * @return Parent handle or 0 if not set
+ */
+uint32_t rtnl_class_get_parent(struct rtnl_class *class)
+{
+       return tca_get_parent((struct rtnl_tca *) class);
+}
+
+/**
+ * Set the kind of a class to the specified value
+ * @arg class          class to be changed
+ * @arg name           new kind name
+ */
+void rtnl_class_set_kind(struct rtnl_class *class, const char *name)
+{
+       tca_set_kind((struct rtnl_tca *) class, name);
+}
+
+/**
+ * Get the kind of a class
+ * @arg class          class handle
+ * @return Kind or NULL if not set
+ */
+char *rtnl_class_get_kind(struct rtnl_class *class)
+{
+       return tca_get_kind((struct rtnl_tca *) class);
+}
+
+/**
+ * Get the statistic specified by the id
+ * @arg class          class handle
+ * @arg id             statistic id
+ * @return The current counter of the specified statistic
+ */
+uint64_t rtnl_class_get_stat(struct rtnl_class *class,
+                            enum rtnl_tc_stats_id id)
+{
+       return tca_get_stat((struct rtnl_tca *) class, id);
+}
+
+/** @} */
+
+static struct nl_cache_ops rtnl_class_ops = {
+       .co_name                = "route/class",
+       .co_size                = sizeof(struct rtnl_class),
+       .co_hdrsize             = sizeof(struct tcmsg),
+       .co_msgtypes            = {
+                                       { RTM_NEWTCLASS, "new" },
+                                       { RTM_DELTCLASS, "del" },
+                                       { RTM_GETTCLASS, "get" },
+                                       { -1, NULL },
+                                 },
+       .co_protocol            = NETLINK_ROUTE,
+       .co_request_update      = &class_request_update,
+       .co_msg_parser          = &class_msg_parser,
+       .co_free_data           = &class_free_data,
+       .co_dump[NL_DUMP_BRIEF] = &class_dump_brief,
+       .co_dump[NL_DUMP_FULL]  = &class_dump_full,
+       .co_dump[NL_DUMP_STATS] = &class_dump_stats,
+       .co_filter              = &class_filter,
+};
+
+static void __init class_init(void)
+{
+       nl_cache_mngt_register(&rtnl_class_ops);
+}
+
+static void __exit class_exit(void)
+{
+       nl_cache_mngt_unregister(&rtnl_class_ops);
+}
+
+/** @} */
diff --git a/lib/route/classifier.c b/lib/route/classifier.c
new file mode 100644 (file)
index 0000000..30dc4cf
--- /dev/null
@@ -0,0 +1,593 @@
+/*
+ * lib/route/classifier.c       Classifier
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup tc
+ * @defgroup cls Classifiers
+ *
+ * @par Classifier Identification
+ * - protocol
+ * - priority
+ * - parent
+ * - interface
+ * - kind
+ * - handle
+ * 
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink-tc.h>
+#include <netlink/netlink.h>
+#include <netlink/utils.h>
+#include <netlink/route/tc.h>
+#include <netlink/route/classifier.h>
+#include <netlink/route/classifier-modules.h>
+#include <netlink/route/link.h>
+
+/** @cond SKIP */
+#define CLS_ATTR_PRIO          (TCA_ATTR_MAX << 1)
+#define CLS_ATTR_PROTOCOL      (TCA_ATTR_MAX << 2)
+
+static struct nl_cache_ops rtnl_cls_ops;
+/** @endcond */
+
+static struct rtnl_cls_ops *cls_ops_list;
+
+static struct rtnl_cls_ops * cls_lookup_ops(char *kind)
+{
+       struct rtnl_cls_ops *ops;
+
+       for (ops = cls_ops_list; ops; ops = ops->co_next)
+               if (!strcmp(kind, ops->co_kind))
+                       return ops;
+
+       return NULL;
+}
+
+static inline struct rtnl_cls_ops *cls_ops(struct rtnl_cls *cls)
+{
+       if (!cls->c_ops)
+               cls->c_ops = cls_lookup_ops(cls->c_kind);
+
+       return cls->c_ops;
+}
+
+/**
+ * @name Classifier Module API
+ * @{
+ */
+
+/**
+ * Register a classifier module
+ * @arg ops            classifier module operations
+ */
+int rtnl_cls_register(struct rtnl_cls_ops *ops)
+{
+       struct rtnl_cls_ops *o, **op;
+
+       if (!ops->co_kind)
+               BUG();
+
+       for (op = &cls_ops_list; (o = *op) != NULL; op = &o->co_next)
+               if (!strcasecmp(ops->co_kind, o->co_kind))
+                       return nl_errno(EEXIST);
+
+       ops->co_next = NULL;
+       *op = ops;
+
+       return 0;
+}
+
+/**
+ * Unregister a classifier module
+ * @arg ops            classifier module operations
+ */
+int rtnl_cls_unregister(struct rtnl_cls_ops *ops)
+{
+       struct rtnl_cls_ops *o, **op;
+
+       for (op = &cls_ops_list; (o = *op) != NULL; op = &o->co_next)
+               if (!strcasecmp(ops->co_kind, o->co_kind))
+                       break;
+
+       if (!o)
+               return nl_errno(ENOENT);
+
+       *op = ops->co_next;
+
+       return 0;
+}
+
+/** @} */
+
+static int cls_msg_parser(struct sockaddr_nl *who, struct nlmsghdr *nlh,
+                         void *arg)
+{
+       int err;
+       struct nl_parser_param *pp = arg;
+       struct rtnl_cls *cls;
+       struct rtnl_cls_ops *ops;
+
+       cls = rtnl_cls_alloc();
+       if (!cls) {
+               err = nl_errno(ENOMEM);
+               goto errout;
+       }
+       cls->ce_msgtype = nlh->nlmsg_type;
+
+       err = tca_msg_parser(nlh, (struct rtnl_tca *) cls);
+       if (err < 0)
+               goto errout_free;
+
+       cls->c_prio = TC_H_MAJ(cls->c_info) >> 16;
+       cls->c_protocol = ntohs(TC_H_MIN(cls->c_info));
+
+       ops = cls_ops(cls);
+       if (ops && ops->co_msg_parser) {
+               err = ops->co_msg_parser(cls);
+               if (err < 0)
+                       goto errout_free;
+       }
+
+       err = pp->pp_cb((struct nl_object *) cls, pp);
+       if (err < 0)
+               goto errout_free;
+
+       return P_ACCEPT;
+
+errout_free:
+       rtnl_cls_put(cls);
+errout:
+       return err;
+}
+
+static int cls_request_update(struct nl_cache *cache, struct nl_handle *handle)
+{
+       struct tcmsg tchdr = {
+               .tcm_family = AF_UNSPEC,
+               .tcm_ifindex = cache->c_iarg1,
+               .tcm_parent = cache->c_iarg2,
+       };
+
+       return nl_send_simple(handle, RTM_GETTFILTER, NLM_F_DUMP, &tchdr,
+                             sizeof(tchdr));
+}
+
+
+static void cls_free_data(struct nl_object *obj)
+{
+       struct rtnl_cls *cls = (struct rtnl_cls *) obj;
+       struct rtnl_cls_ops *ops;
+       
+       tca_free_data((struct rtnl_tca *) cls);
+
+       ops = cls_ops(cls);
+       if (ops && ops->co_free_data)
+               ops->co_free_data(cls);
+}
+
+static int cls_dump_brief(struct nl_object *obj, struct nl_dump_params *p)
+{
+       char buf[32];
+       struct rtnl_cls *cls = (struct rtnl_cls *) obj;
+       struct rtnl_cls_ops *ops;
+       int line;
+
+       line = tca_dump_brief((struct rtnl_tca *) cls, "cls", p, 0);
+
+       dp_dump(p, " prio %u protocol %s", cls->c_prio,
+               nl_ether_proto2str(cls->c_protocol, buf, sizeof(buf)));
+
+       ops = cls_ops(cls);
+       if (ops && ops->co_dump[NL_DUMP_BRIEF])
+               line = ops->co_dump[NL_DUMP_BRIEF](cls, p, line);
+       dp_dump(p, "\n");
+
+       return line;
+}
+
+static int cls_dump_full(struct nl_object *obj, struct nl_dump_params *p)
+{
+       struct rtnl_cls *cls = (struct rtnl_cls *) obj;
+       struct rtnl_cls_ops *ops;
+       int line;
+
+       line = cls_dump_brief(obj, p);
+       line = tca_dump_full((struct rtnl_tca *) cls, p, line);
+
+       ops = cls_ops(cls);
+       if (ops && ops->co_dump[NL_DUMP_FULL])
+               line = ops->co_dump[NL_DUMP_FULL](cls, p, line);
+       else
+               dp_dump(p, "no options\n");
+
+       return line;
+}
+
+static int cls_dump_stats(struct nl_object *obj, struct nl_dump_params *p)
+{
+       struct rtnl_cls *cls = (struct rtnl_cls *) obj;
+       struct rtnl_cls_ops *ops;
+       int line;
+
+       line = cls_dump_full(obj, p);
+       line = tca_dump_stats((struct rtnl_tca *) cls, p, line);
+       dp_dump(p, "\n");
+
+       ops = cls_ops(cls);
+       if (ops && ops->co_dump[NL_DUMP_STATS])
+               line = ops->co_dump[NL_DUMP_STATS](cls, p, line);
+
+       return line;
+}
+
+static int cls_filter(struct nl_object *obj, struct nl_object *filter)
+{
+       return tca_filter((struct rtnl_tca *) obj, (struct rtnl_tca *) filter);
+}
+
+static struct nl_msg *cls_build(struct rtnl_cls *cls, int type, int flags)
+{
+       struct nl_msg *msg;
+       struct rtnl_cls_ops *ops;
+       int err, prio, proto;
+       struct tcmsg *tchdr;
+
+       msg = tca_build_msg((struct rtnl_tca *) cls, type, flags);
+       if (!msg)
+               goto errout;
+
+       tchdr = nlmsg_data(nlmsg_hdr(msg));
+       prio = cls->c_mask & CLS_ATTR_PRIO ? cls->c_prio : 0;
+       proto = cls->c_mask & CLS_ATTR_PROTOCOL ? cls->c_protocol : ETH_P_ALL;
+       tchdr->tcm_info = TC_H_MAKE(prio << 16, htons(proto)),
+
+       ops = cls_ops(cls);
+       if (ops && ops->co_get_opts) {
+               struct nl_msg *opts;
+               
+               opts = ops->co_get_opts(cls);
+               if (opts) {
+                       err = nla_put_nested(msg, TCA_OPTIONS, opts);
+                       nlmsg_free(opts);
+                       if (err < 0)
+                               goto errout;
+               }
+       }
+
+       return msg;
+errout:
+       nlmsg_free(msg);
+       return NULL;
+}
+
+/**
+ * @name Classifier Addition/Modification/Deletion
+ * @{
+ */
+
+/**
+ * Build a netlink message to add a new classifier
+ * @arg cls            classifier to add
+ * @arg flags          additional netlink message flags
+ *
+ * Builds a new netlink message requesting an addition of a classifier
+ * The netlink message header isn't fully equipped with all relevant
+ * fields and must be sent out via nl_send_auto_complete() or
+ * supplemented as needed. \a classifier must contain the attributes of
+ * the new classifier set via \c rtnl_cls_set_* functions. \a opts
+ * may point to the clsasifier specific options.
+ *
+ * @return New netlink message
+ */
+struct nl_msg * rtnl_cls_build_add_request(struct rtnl_cls *cls, int flags)
+{
+       return cls_build(cls, RTM_NEWTFILTER, NLM_F_CREATE | flags);
+}
+
+/**
+ * Add a new classifier
+ * @arg handle         netlink handle
+ * @arg cls            classifier to add
+ * @arg flags          additional netlink message flags
+ *
+ * Builds a netlink message by calling rtnl_cls_build_add_request(),
+ * sends the request to the kernel and waits for the next ACK to be
+ * received and thus blocks until the request has been processed.
+ *
+ * @return 0 on sucess or a negative error if an error occured.
+ */
+int rtnl_cls_add(struct nl_handle *handle, struct rtnl_cls *cls, int flags)
+{
+       int err;
+       struct nl_msg *msg;
+       
+       msg = rtnl_cls_build_add_request(cls, flags);
+       if (!msg)
+               return nl_errno(ENOMEM);
+       
+       err = nl_send_auto_complete(handle, msg);
+       if (err < 0)
+               return err;
+
+       nlmsg_free(msg);
+       return nl_wait_for_ack(handle);
+}
+
+/**
+ * Build a netlink message to change classifier attributes
+ * @arg cls            classifier to change
+ * @arg flags          additional netlink message flags
+ *
+ * Builds a new netlink message requesting a change of a neigh
+ * attributes. The netlink message header isn't fully equipped with
+ * all relevant fields and must thus be sent out via nl_send_auto_complete()
+ * or supplemented as needed.
+ *
+ * @return The netlink message
+ */
+struct nl_msg *rtnl_cls_build_change_request(struct rtnl_cls *cls, int flags)
+{
+       return cls_build(cls, RTM_NEWTFILTER, NLM_F_REPLACE | flags);
+}
+
+/**
+ * Change a classifier
+ * @arg handle         netlink handle
+ * @arg cls            classifier to change
+ * @arg flags          additional netlink message flags
+ *
+ * Builds a netlink message by calling rtnl_cls_build_change_request(),
+ * sends the request to the kernel and waits for the next ACK to be
+ * received and thus blocks until the request has been processed.
+ *
+ * @return 0 on sucess or a negative error if an error occured.
+ */
+int rtnl_cls_change(struct nl_handle *handle, struct rtnl_cls *cls,
+                   int flags)
+{
+       int err;
+       struct nl_msg *msg;
+       
+       msg = rtnl_cls_build_change_request(cls, flags);
+       if (!msg)
+               return nl_errno(ENOMEM);
+       
+       err = nl_send_auto_complete(handle, msg);
+       if (err < 0)
+               return err;
+
+       nlmsg_free(msg);
+       return nl_wait_for_ack(handle);
+}
+
+/**
+ * Build a netlink request message to delete a classifier
+ * @arg cls            classifier to delete
+ * @arg flags          additional netlink message flags
+ *
+ * Builds a new netlink message requesting a deletion of a classifier.
+ * The netlink message header isn't fully equipped with all relevant
+ * fields and must thus be sent out via nl_send_auto_complete()
+ * or supplemented as needed.
+ *
+ * @return New netlink message
+ */
+struct nl_msg *rtnl_cls_build_delete_request(struct rtnl_cls *cls, int flags)
+{
+       return cls_build(cls, RTM_DELTFILTER, flags);
+}
+
+
+/**
+ * Delete a classifier
+ * @arg handle         netlink handle
+ * @arg cls            classifier to delete
+ * @arg flags          additional netlink message flags
+ *
+ * Builds a netlink message by calling rtnl_cls_build_delete_request(),
+ * sends the request to the kernel and waits for the next ACK to be
+ * received and thus blocks until the request has been processed.
+ *
+ * @return 0 on sucess or a negative error if an error occured.
+ */
+int rtnl_cls_delete(struct nl_handle *handle, struct rtnl_cls *cls, int flags)
+{
+       int err;
+       struct nl_msg *msg;
+       
+       msg = rtnl_cls_build_delete_request(cls, flags);
+       if (!msg)
+               return nl_errno(ENOMEM);
+       
+       err = nl_send_auto_complete(handle, msg);
+       if (err < 0)
+               return err;
+
+       nlmsg_free(msg);
+       return nl_wait_for_ack(handle);
+}
+
+/** @} */
+
+/**
+ * @name General API
+ * @{
+ */
+
+/**
+ * Allocate a new classifier object
+ * @return New classifier object
+ */
+struct rtnl_cls *rtnl_cls_alloc(void)
+{
+       return (struct rtnl_cls *) nl_object_alloc_from_ops(&rtnl_cls_ops);
+}
+
+/**
+ * Give back reference on classifier object.
+ * @arg cls            Classifier object to be given back.
+ *
+ * Decrements the reference counter and frees the object if the
+ * last reference has been released.
+ */
+void rtnl_cls_put(struct rtnl_cls *cls)
+{
+       nl_object_put((struct nl_object *) cls);
+}
+
+/**
+ * Free classifier object.
+ * @arg cls            Classifier object to be freed.
+ *
+ * @note Always use rtnl_cls_put() unless you're absolutely sure
+ *       that no other user may have a reference on this object.
+ */
+void rtnl_cls_free(struct rtnl_cls *cls)
+{
+       nl_object_free((struct nl_object *) cls);
+}
+
+/**
+ * Build a classifier cache including all classifiers attached to the
+ * specified class/qdisc on eht specified interface.
+ * @arg handle         netlink handle
+ * @arg ifindex                interface index of the link the classes are
+ *                      attached to.
+ * @arg parent          parent qdisc/class
+ *
+ * Allocates a new cache, initializes it properly and updates it to
+ * include all classes attached to the specified interface.
+ *
+ * @note The caller is responsible for destroying and freeing the
+ *       cache after using it.
+ * @return The cache or NULL if an error has occured.
+ */
+struct nl_cache *rtnl_cls_alloc_cache(struct nl_handle *handle,
+                                     int ifindex, uint32_t parent)
+{
+       struct nl_cache * cache;
+       
+       cache = nl_cache_alloc_from_ops(&rtnl_cls_ops);
+       if (cache == NULL)
+               return NULL;
+
+       cache->c_iarg1 = ifindex;
+       cache->c_iarg2 = parent;
+       
+       if (nl_cache_update(handle, cache) < 0) {
+               nl_cache_free(cache);
+               return NULL;
+       }
+
+       return cache;
+}
+
+void rtnl_cls_set_ifindex(struct rtnl_cls *f, int ifindex)
+{
+       tca_set_ifindex((struct rtnl_tca *) f, ifindex);
+}
+
+void rtnl_cls_set_handle(struct rtnl_cls *f, uint32_t handle)
+{
+       tca_set_handle((struct rtnl_tca *) f, handle);
+}
+
+void rtnl_cls_set_parent(struct rtnl_cls *f, uint32_t parent)
+{
+       tca_set_parent((struct rtnl_tca *) f, parent);
+}
+
+void rtnl_cls_set_kind(struct rtnl_cls *f, const char *kind)
+{
+       tca_set_kind((struct rtnl_tca *) f, kind);
+}
+
+/**
+ * Set prioroty of a classifier
+ * @arg cls            classifier to change
+ * @arg prio           new priority
+ */
+void rtnl_cls_set_prio(struct rtnl_cls *cls, int prio)
+{
+       cls->c_prio = prio;
+       cls->c_mask |= CLS_ATTR_PRIO;
+}
+
+/**
+ * Get priority of a classifier
+ * @arg cls            classifier
+ */
+int rtnl_cls_get_prio(struct rtnl_cls *cls)
+{
+       if (cls->c_mask & CLS_ATTR_PRIO)
+               return cls->c_prio;
+       else
+               return 0;
+}
+
+/**
+ * Set protocol of a classifier
+ * @arg cls            classifier to change
+ * @arg protocol       protocol identifier (ETH_P_xxx) in host byte-order
+ */
+void rtnl_cls_set_protocol(struct rtnl_cls *cls, int protocol)
+{
+       cls->c_protocol = protocol;
+       cls->c_mask |= CLS_ATTR_PROTOCOL;
+}
+
+/**
+ * Get protocol of a classifier
+ * @arg cls            classifier
+ */
+int rtnl_cls_get_protocol(struct rtnl_cls *cls)
+{
+       if (cls->c_mask & CLS_ATTR_PROTOCOL)
+               return cls->c_protocol;
+       else
+               return 0;
+}
+
+/** @} */
+
+static struct nl_cache_ops rtnl_cls_ops = {
+       .co_name                = "route/cls",
+       .co_size                = sizeof(struct rtnl_cls),
+       .co_hdrsize             = sizeof(struct tcmsg),
+       .co_msgtypes            = {
+                                       { RTM_NEWTFILTER, "new" },
+                                       { RTM_DELTFILTER, "delete" },
+                                       { RTM_GETTFILTER, "get" },
+                                       { -1, NULL },
+                                 },
+       .co_protocol            = NETLINK_ROUTE,
+       .co_request_update      = cls_request_update,
+       .co_filter              = cls_filter,
+       .co_free_data           = cls_free_data,
+       .co_msg_parser          = cls_msg_parser,
+       .co_dump[NL_DUMP_BRIEF] = cls_dump_brief,
+       .co_dump[NL_DUMP_FULL]  = cls_dump_full,
+       .co_dump[NL_DUMP_STATS] = cls_dump_stats,
+};
+
+static void __init cls_init(void)
+{
+       nl_cache_mngt_register(&rtnl_cls_ops);
+}
+
+static void __exit cls_exit(void)
+{
+       nl_cache_mngt_unregister(&rtnl_cls_ops);
+}
+
+/** @} */
diff --git a/lib/route/cls/fw.c b/lib/route/cls/fw.c
new file mode 100644 (file)
index 0000000..a0cde11
--- /dev/null
@@ -0,0 +1,226 @@
+/*
+ * lib/route/cls/fw.c          fw classifier
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ * Copyright (c) 2006 Petr Gotthard <petr.gotthard@siemens.com>
+ * Copyright (c) 2006 Siemens AG Oesterreich
+ */
+
+/**
+ * @ingroup cls
+ * @defgroup fw Firewall Classifier
+ *
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink-tc.h>
+#include <netlink/netlink.h>
+#include <netlink/route/classifier.h>
+#include <netlink/route/classifier-modules.h>
+#include <netlink/route/cls/fw.h>
+
+/** @cond SKIP */
+#define FW_ATTR_CLASSID      0x001
+#define FW_ATTR_ACTION       0x002
+#define FW_ATTR_POLICE       0x004
+#define FW_ATTR_INDEV        0x008
+/** @endcond */
+
+static inline struct rtnl_fw *fw_cls(struct rtnl_cls *cls)
+{
+       return (struct rtnl_fw *) cls->c_subdata;
+}
+
+static inline struct rtnl_fw *fw_alloc(struct rtnl_cls *cls)
+{
+       if (!cls->c_subdata)
+               cls->c_subdata = calloc(1, sizeof(struct rtnl_fw));
+
+       return fw_cls(cls);
+}
+
+static struct nla_policy fw_policy[TCA_FW_MAX+1] = {
+       [TCA_FW_CLASSID]        = { .type = NLA_U32 },
+       [TCA_FW_INDEV]          = { .type = NLA_STRING,
+                                   .maxlen = IFNAMSIZ },
+};
+
+static int fw_msg_parser(struct rtnl_cls *cls)
+{
+       int err;
+       struct nlattr *tb[TCA_FW_MAX + 1];
+       struct rtnl_fw *f;
+
+       err = tca_parse(tb, TCA_FW_MAX, (struct rtnl_tca *) cls, fw_policy);
+       if (err < 0)
+               return err;
+
+       f = fw_alloc(cls);
+       if (!f)
+               goto errout_nomem;
+
+       if (tb[TCA_FW_CLASSID]) {
+               f->cf_classid = nla_get_u32(tb[TCA_FW_CLASSID]);
+               f->cf_mask |= FW_ATTR_CLASSID;
+       }
+
+       if (tb[TCA_FW_ACT]) {
+               f->cf_act = nla_get_data(tb[TCA_FW_ACT]);
+               if (!f->cf_act)
+                       goto errout_nomem;
+               f->cf_mask |= FW_ATTR_ACTION;
+       }
+
+       if (tb[TCA_FW_POLICE]) {
+               f->cf_police = nla_get_data(tb[TCA_FW_POLICE]);
+               if (!f->cf_police)
+                       goto errout_nomem;
+               f->cf_mask |= FW_ATTR_POLICE;
+       }
+
+       if (tb[TCA_FW_INDEV]) {
+               nla_strlcpy(f->cf_indev, tb[TCA_FW_INDEV], IFNAMSIZ);
+               f->cf_mask |= FW_ATTR_INDEV;
+       }
+
+       return 0;
+
+errout_nomem:
+       err = nl_errno(ENOMEM);
+
+       return err;
+}
+
+static void fw_free_data(struct rtnl_cls *cls)
+{
+       struct rtnl_fw *f = fw_cls(cls);
+
+       if (!f)
+               return;
+
+       nl_data_free(f->cf_act);
+       nl_data_free(f->cf_police);
+
+       free(cls->c_subdata);
+}
+
+static int fw_dump_brief(struct rtnl_cls *cls, struct nl_dump_params *p,
+                         int line)
+{
+       struct rtnl_fw *f = fw_cls(cls);
+       char buf[32];
+
+       if (!f)
+               goto ignore;
+
+       if (f->cf_mask & FW_ATTR_CLASSID)
+               dp_dump(p, " target %s",
+                       rtnl_tc_handle2str(f->cf_classid, buf, sizeof(buf)));
+
+ignore:
+       return line;
+}
+
+static int fw_dump_full(struct rtnl_cls *cls, struct nl_dump_params *p,
+                        int line)
+{
+       struct rtnl_fw *f = fw_cls(cls);
+
+       if (!f)
+               goto ignore;
+
+       if (f->cf_mask & FW_ATTR_INDEV)
+               dp_dump(p, "indev %s ", f->cf_indev);
+
+ignore:
+       return line;
+}
+
+static int fw_dump_stats(struct rtnl_cls *cls, struct nl_dump_params *p,
+                         int line)
+{
+       struct rtnl_fw *f = fw_cls(cls);
+
+       if (!f)
+               goto ignore;
+
+ignore:
+       return line;
+}
+
+static struct nl_msg *fw_get_opts(struct rtnl_cls *cls)
+{
+       struct rtnl_fw *f;
+       struct nl_msg *msg;
+       
+       f = fw_cls(cls);
+       if (!f)
+               return NULL;
+
+       msg = nlmsg_build_no_hdr();
+       if (!msg)
+               return NULL;
+
+       if (f->cf_mask & FW_ATTR_CLASSID)
+               nla_put_u32(msg, TCA_FW_CLASSID, f->cf_classid);
+
+       if (f->cf_mask & FW_ATTR_ACTION)
+               nla_put_data(msg, TCA_FW_ACT, f->cf_act);
+
+       if (f->cf_mask & FW_ATTR_POLICE)
+               nla_put_data(msg, TCA_FW_POLICE, f->cf_police);
+
+       if (f->cf_mask & FW_ATTR_INDEV)
+               nla_put_string(msg, TCA_FW_INDEV, f->cf_indev);
+
+       return msg;
+}
+
+/**
+ * @name Attribute Modifications
+ * @{
+ */
+
+int rtnl_fw_set_classid(struct rtnl_cls *cls, uint32_t classid)
+{
+       struct rtnl_fw *f;
+       
+       f = fw_alloc(cls);
+       if (!f)
+               return nl_errno(ENOMEM);
+
+       f->cf_classid = classid;
+       f->cf_mask |= FW_ATTR_CLASSID;
+
+       return 0;
+}
+
+/** @} */
+
+static struct rtnl_cls_ops fw_ops = {
+       .co_kind                = "fw",
+       .co_msg_parser          = fw_msg_parser,
+       .co_free_data           = fw_free_data,
+       .co_get_opts            = fw_get_opts,
+       .co_dump[NL_DUMP_BRIEF] = fw_dump_brief,
+       .co_dump[NL_DUMP_FULL]  = fw_dump_full,
+       .co_dump[NL_DUMP_STATS] = fw_dump_stats,
+};
+
+static void __init fw_init(void)
+{
+       rtnl_cls_register(&fw_ops);
+}
+
+static void __exit fw_exit(void)
+{
+       rtnl_cls_unregister(&fw_ops);
+}
+
+/** @} */
diff --git a/lib/route/cls/police.c b/lib/route/cls/police.c
new file mode 100644 (file)
index 0000000..051c6b2
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * lib/route/cls/police.c      Policer
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include <netlink-local.h>
+#include <netlink-tc.h>
+#include <netlink/netlink.h>
+#include <netlink/utils.h>
+#include <netlink/route/tc.h>
+#include <netlink/route/classifier.h>
+#include <netlink/route/classifier-modules.h>
+#include <netlink/route/cls/police.h>
+
+/**
+ * @name Policer Type
+ * @{
+ */
+
+static struct trans_tbl police_types[] = {
+       __ADD(TC_POLICE_UNSPEC,unspec)
+       __ADD(TC_POLICE_OK,ok)
+       __ADD(TC_POLICE_RECLASSIFY,reclassify)
+       __ADD(TC_POLICE_SHOT,shot)
+#ifdef TC_POLICE_PIPE
+       __ADD(TC_POLICE_PIPE,pipe)
+#endif
+};
+
+/**
+ * Transform a policer type number into a character string (Reentrant).
+ * @arg type           policer type
+ * @arg buf            destination buffer
+ * @arg len            buffer length
+ *
+ * Transforms a policer type number into a character string and stores
+ * it in the provided buffer.
+ *
+ * @return The destination buffer or the type encoded in hex if no match was found.
+ */
+char * nl_police2str(int type, char *buf, size_t len)
+{
+       return __type2str(type, buf, len, police_types,
+                         ARRAY_SIZE(police_types));
+}
+
+/**
+ * Transform a character string into a policer type number
+ * @arg name           policer type name
+ *
+ * Transform the provided character string specifying a policer
+ * type into the corresponding numeric value
+ *
+ * @return Policer type number or a negative value.
+ */
+int nl_str2police(const char *name)
+{
+       return __str2type(name, police_types, ARRAY_SIZE(police_types));
+}
+
+/** @} */
diff --git a/lib/route/cls/u32.c b/lib/route/cls/u32.c
new file mode 100644 (file)
index 0000000..e97173a
--- /dev/null
@@ -0,0 +1,566 @@
+/*
+ * lib/route/cls/u32.c         u32 classifier
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ * Copyright (c) 2005-2006 Petr Gotthard <petr.gotthard@siemens.com>
+ * Copyright (c) 2005-2006 Siemens AG Oesterreich
+ */
+
+/**
+ * @ingroup cls
+ * @defgroup u32 Universal 32-bit Classifier
+ *
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink-tc.h>
+#include <netlink/netlink.h>
+#include <netlink/attr.h>
+#include <netlink/utils.h>
+#include <netlink/route/tc.h>
+#include <netlink/route/classifier.h>
+#include <netlink/route/classifier-modules.h>
+#include <netlink/route/cls/u32.h>
+
+/** @cond SKIP */
+#define U32_ATTR_DIVISOR      0x001
+#define U32_ATTR_HASH         0x002
+#define U32_ATTR_CLASSID      0x004
+#define U32_ATTR_LINK         0x008
+#define U32_ATTR_PCNT         0x010
+#define U32_ATTR_SELECTOR     0x020
+#define U32_ATTR_ACTION       0x040
+#define U32_ATTR_POLICE       0x080
+#define U32_ATTR_INDEV        0x100
+/** @endcond */
+
+static inline struct rtnl_u32 *u32_cls(struct rtnl_cls *cls)
+{
+       return (struct rtnl_u32 *) cls->c_subdata;
+}
+
+static inline struct rtnl_u32 *u32_alloc(struct rtnl_cls *cls)
+{
+       if (!cls->c_subdata)
+               cls->c_subdata = calloc(1, sizeof(struct rtnl_u32));
+
+       return u32_cls(cls);
+}
+
+static inline struct tc_u32_sel *u32_selector(struct rtnl_u32 *u)
+{
+       return (struct tc_u32_sel *) u->cu_selector->d_data;
+}
+
+static inline struct tc_u32_sel *u32_selector_alloc(struct rtnl_u32 *u)
+{
+       if (!u->cu_selector)
+               u->cu_selector = nl_data_alloc(NULL, sizeof(struct tc_u32_sel));
+
+       return u32_selector(u);
+}
+
+static struct nla_policy u32_policy[TCA_U32_MAX+1] = {
+       [TCA_U32_DIVISOR]       = { .type = NLA_U32 },
+       [TCA_U32_HASH]          = { .type = NLA_U32 },
+       [TCA_U32_CLASSID]       = { .type = NLA_U32 },
+       [TCA_U32_LINK]          = { .type = NLA_U32 },
+       [TCA_U32_INDEV]         = { .type = NLA_STRING,
+                                   .maxlen = IFNAMSIZ },
+       [TCA_U32_SEL]           = { .minlen = sizeof(struct tc_u32_sel) },
+       [TCA_U32_PCNT]          = { .minlen = sizeof(struct tc_u32_pcnt) },
+};
+
+static int u32_msg_parser(struct rtnl_cls *cls)
+{
+       int err;
+       struct nlattr *tb[TCA_U32_MAX + 1];
+       struct rtnl_u32 *u;
+
+       err = tca_parse(tb, TCA_U32_MAX, (struct rtnl_tca *) cls, u32_policy);
+       if (err < 0)
+               return err;
+
+       u = u32_alloc(cls);
+       if (!u)
+               goto errout_nomem;
+
+       if (tb[TCA_U32_DIVISOR]) {
+               u->cu_divisor = nla_get_u32(tb[TCA_U32_DIVISOR]);
+               u->cu_mask |= U32_ATTR_DIVISOR;
+       }
+
+       if (tb[TCA_U32_SEL]) {
+               u->cu_selector = nla_get_data(tb[TCA_U32_SEL]);
+               if (!u->cu_selector)
+                       goto errout_nomem;
+               u->cu_mask |= U32_ATTR_SELECTOR;
+       }
+
+       if (tb[TCA_U32_HASH]) {
+               u->cu_hash = nla_get_u32(tb[TCA_U32_HASH]);
+               u->cu_mask |= U32_ATTR_HASH;
+       }
+
+       if (tb[TCA_U32_CLASSID]) {
+               u->cu_classid = nla_get_u32(tb[TCA_U32_CLASSID]);
+               u->cu_mask |= U32_ATTR_CLASSID;
+       }
+
+       if (tb[TCA_U32_LINK]) {
+               u->cu_link = nla_get_u32(tb[TCA_U32_LINK]);
+               u->cu_mask |= U32_ATTR_LINK;
+       }
+
+       if (tb[TCA_U32_ACT]) {
+               u->cu_act = nla_get_data(tb[TCA_U32_ACT]);
+               if (!u->cu_act)
+                       goto errout_nomem;
+               u->cu_mask |= U32_ATTR_ACTION;
+       }
+
+       if (tb[TCA_U32_POLICE]) {
+               u->cu_police = nla_get_data(tb[TCA_U32_POLICE]);
+               if (!u->cu_police)
+                       goto errout_nomem;
+               u->cu_mask |= U32_ATTR_POLICE;
+       }
+
+       if (tb[TCA_U32_PCNT]) {
+               struct tc_u32_sel *sel;
+               int pcnt_size;
+
+               if (!tb[TCA_U32_SEL]) {
+                       err = nl_error(EINVAL, "Missing TCA_U32_SEL required "
+                                              "for TCA_U32_PCNT");
+                       goto errout;
+               }
+               
+               sel = u->cu_selector->d_data;
+               pcnt_size = sizeof(struct tc_u32_pcnt) +
+                               (sel->nkeys * sizeof(uint64_t));
+               if (nla_len(tb[TCA_U32_PCNT]) < pcnt_size) {
+                       err = nl_error(EINVAL, "Invalid size for TCA_U32_PCNT");
+                       goto errout;
+               }
+
+               u->cu_pcnt = nla_get_data(tb[TCA_U32_PCNT]);
+               if (!u->cu_pcnt)
+                       goto errout_nomem;
+               u->cu_mask |= U32_ATTR_PCNT;
+       }
+
+       if (tb[TCA_U32_INDEV]) {
+               nla_strlcpy(u->cu_indev, tb[TCA_U32_INDEV], IFNAMSIZ);
+               u->cu_mask |= U32_ATTR_INDEV;
+       }
+
+       return 0;
+
+errout_nomem:
+       err = nl_errno(ENOMEM);
+errout:
+       return err;
+}
+
+static void u32_free_data(struct rtnl_cls *cls)
+{
+       struct rtnl_u32 *u = u32_cls(cls);
+
+       if (!u)
+               return;
+
+       nl_data_free(u->cu_selector);
+       nl_data_free(u->cu_act);
+       nl_data_free(u->cu_police);
+       nl_data_free(u->cu_pcnt);
+
+       free(cls->c_subdata);
+}
+
+static int u32_dump_brief(struct rtnl_cls *cls, struct nl_dump_params *p,
+                         int line)
+{
+       struct rtnl_u32 *u = u32_cls(cls);
+       char buf[32];
+
+       if (!u)
+               goto ignore;
+
+       if (u->cu_mask & U32_ATTR_DIVISOR)
+               dp_dump(p, " divisor %u", u->cu_divisor);
+       else if (u->cu_mask & U32_ATTR_CLASSID)
+               dp_dump(p, " target %s",
+                       rtnl_tc_handle2str(u->cu_classid, buf, sizeof(buf)));
+
+ignore:
+       return line;
+}
+
+static int print_selector(struct nl_dump_params *p, struct tc_u32_sel *sel,
+                         struct rtnl_cls *cls, struct rtnl_u32 *u, int line)
+{
+       int i;
+       struct tc_u32_key *key;
+
+       if (sel->hmask || sel->hoff) {
+               /* I guess this will never be used since the kernel only
+                * exports the selector if no divisor is set but hash offset
+                * and hash mask make only sense in hash filters with divisor
+                * set */
+               dp_dump(p, " hash at %u & 0x%x", sel->hoff, sel->hmask);
+       }
+
+       if (sel->flags & (TC_U32_OFFSET | TC_U32_VAROFFSET)) {
+               dp_dump(p, " offset at %u", sel->off);
+
+               if (sel->flags & TC_U32_VAROFFSET)
+                       dp_dump(p, " variable (at %u & 0x%x) >> %u",
+                               sel->offoff, ntohs(sel->offmask), sel->offshift);
+       }
+
+       if (sel->flags) {
+               int flags = sel->flags;
+               dp_dump(p, " <");
+
+#define PRINT_FLAG(f) if (flags & TC_U32_##f) { \
+       flags &= ~TC_U32_##f; dp_dump(p, #f "%s", flags ? "," : ""); }
+
+               PRINT_FLAG(TERMINAL);
+               PRINT_FLAG(OFFSET);
+               PRINT_FLAG(VAROFFSET);
+               PRINT_FLAG(EAT);
+#undef PRINT_FLAG
+
+               dp_dump(p, ">");
+       }
+               
+       
+       for (i = 0; i < sel->nkeys; i++) {
+               key = (struct tc_u32_key *) ((char *) sel + sizeof(*sel));
+
+               dp_dump(p, "\n");
+               dp_dump_line(p, line++, "      match key at %s%u ",
+               key->offmask ? "nexthdr+" : "", key->off);
+
+               if (key->offmask)
+                       dp_dump(p, "[0x%u] ", key->offmask);
+
+               dp_dump(p, "& 0x%08x == 0x%08x", ntohl(key->mask), ntohl(key->val));
+
+               if (p->dp_type == NL_DUMP_STATS &&
+                   (u->cu_mask & U32_ATTR_PCNT)) {
+                       struct tc_u32_pcnt *pcnt = u->cu_pcnt->d_data;
+                       dp_dump(p, " successful %" PRIu64, pcnt->kcnts[i]);
+               }
+       }
+
+       return line;
+}
+
+
+static int u32_dump_full(struct rtnl_cls *cls, struct nl_dump_params *p,
+                        int line)
+{
+       struct rtnl_u32 *u = u32_cls(cls);
+       struct tc_u32_sel *s;
+
+       if (!u)
+               goto ignore;
+
+       if (!(u->cu_mask & U32_ATTR_SELECTOR)) {
+               dp_dump(p, "no-selector\n");
+               return line;
+       }
+       
+       s = u->cu_selector->d_data;
+
+       dp_dump(p, "nkeys %u ", s->nkeys);
+
+       if (u->cu_mask & U32_ATTR_HASH)
+               dp_dump(p, "ht key 0x%x hash 0x%u",
+                       TC_U32_USERHTID(u->cu_hash), TC_U32_HASH(u->cu_hash));
+
+       if (u->cu_mask & U32_ATTR_LINK)
+               dp_dump(p, "link %u ", u->cu_link);
+
+       if (u->cu_mask & U32_ATTR_INDEV)
+               dp_dump(p, "indev %s ", u->cu_indev);
+
+       line = print_selector(p, s, cls, u, line);
+       dp_dump(p, "\n");
+
+ignore:
+       return line;
+
+#if 0  
+#define U32_ATTR_ACTION       0x040
+#define U32_ATTR_POLICE       0x080
+
+       struct nl_data   act;
+       struct nl_data   police;
+#endif
+}
+
+static int u32_dump_stats(struct rtnl_cls *cls, struct nl_dump_params *p,
+                         int line)
+{
+       struct rtnl_u32 *u = u32_cls(cls);
+
+       if (!u)
+               goto ignore;
+
+       if (u->cu_mask & U32_ATTR_PCNT) {
+               struct tc_u32_pcnt *pc = u->cu_pcnt->d_data;
+               dp_dump(p, "\n");
+               dp_dump_line(p, line++, "%s         successful       hits\n");
+               dp_dump_line(p, line++, "%s           %8llu   %8llu\n",
+                            pc->rhit, pc->rcnt);
+       }
+
+ignore:
+       return line;
+}
+
+static struct nl_msg *u32_get_opts(struct rtnl_cls *cls)
+{
+       struct rtnl_u32 *u;
+       struct nl_msg *msg;
+       
+       u = u32_cls(cls);
+       if (!u)
+               return NULL;
+
+       msg = nlmsg_build_no_hdr();
+       if (!msg)
+               return NULL;
+
+       if (u->cu_mask & U32_ATTR_DIVISOR)
+               nla_put_u32(msg, TCA_U32_DIVISOR, u->cu_divisor);
+
+       if (u->cu_mask & U32_ATTR_HASH)
+               nla_put_u32(msg, TCA_U32_HASH, u->cu_hash);
+
+       if (u->cu_mask & U32_ATTR_CLASSID)
+               nla_put_u32(msg, TCA_U32_CLASSID, u->cu_classid);
+
+       if (u->cu_mask & U32_ATTR_LINK)
+               nla_put_u32(msg, TCA_U32_LINK, u->cu_link);
+
+       if (u->cu_mask & U32_ATTR_SELECTOR)
+               nla_put_data(msg, TCA_U32_SEL, u->cu_selector);
+
+       if (u->cu_mask & U32_ATTR_ACTION)
+               nla_put_data(msg, TCA_U32_ACT, u->cu_act);
+
+       if (u->cu_mask & U32_ATTR_POLICE)
+               nla_put_data(msg, TCA_U32_POLICE, u->cu_police);
+
+       if (u->cu_mask & U32_ATTR_INDEV)
+               nla_put_string(msg, TCA_U32_INDEV, u->cu_indev);
+
+       return msg;
+}
+
+/**
+ * @name Attribute Modifications
+ * @{
+ */
+
+void rtnl_u32_set_handle(struct rtnl_cls *cls, int htid, int hash,
+                        int nodeid)
+{
+       uint32_t handle = (htid << 20) | (hash << 12) | nodeid;
+
+       tca_set_handle((struct rtnl_tca *) cls, handle );
+}
+int rtnl_u32_set_classid(struct rtnl_cls *cls, uint32_t classid)
+{
+       struct rtnl_u32 *u;
+       
+       u = u32_alloc(cls);
+       if (!u)
+               return nl_errno(ENOMEM);
+
+       u->cu_classid = classid;
+       u->cu_mask |= U32_ATTR_CLASSID;
+
+       return 0;
+}
+
+/** @} */
+
+/**
+ * @name Selector Modifications
+ * @{
+ */
+
+int rtnl_u32_set_flags(struct rtnl_cls *cls, int flags)
+{
+       struct tc_u32_sel *sel;
+       struct rtnl_u32 *u;
+
+       u = u32_alloc(cls);
+       if (!u)
+               return nl_errno(ENOMEM);
+
+       sel = u32_selector_alloc(u);
+       if (!sel)
+               return nl_errno(ENOMEM);
+
+       sel->flags |= flags;
+       u->cu_mask |= U32_ATTR_SELECTOR;
+
+       return 0;
+}
+
+/**
+ * Append new 32-bit key to the selector
+ *
+ * @arg cls    classifier to be modifier
+ * @arg val    value to be matched (network byte-order)
+ * @arg mask   mask to be applied before matching (network byte-order)
+ * @arg off    offset, in bytes, to start matching
+ * @arg offmask        offset mask
+ *
+ * General selectors define the pattern, mask and offset the pattern will be
+ * matched to the packet contents. Using the general selectors you can match
+ * virtually any single bit in the IP (or upper layer) header.
+ *
+*/
+int rtnl_u32_add_key(struct rtnl_cls *cls, uint32_t val, uint32_t mask,
+                    int off, int offmask)
+{
+       struct tc_u32_sel *sel;
+       struct rtnl_u32 *u;
+       int err;
+
+       u = u32_alloc(cls);
+       if (!u)
+               return nl_errno(ENOMEM);
+
+       sel = u32_selector_alloc(u);
+       if (!sel)
+               return nl_errno(ENOMEM);
+
+       err = nl_data_append(u->cu_selector, NULL, sizeof(struct tc_u32_key));
+       if (err < 0)
+               return err;
+
+       /* the selector might have been moved by realloc */
+       sel = u32_selector(u);
+
+       sel->keys[sel->nkeys].mask = mask;
+       sel->keys[sel->nkeys].val = val & mask;
+       sel->keys[sel->nkeys].off = off;
+       sel->keys[sel->nkeys].offmask = offmask;
+       sel->nkeys++;
+       u->cu_mask |= U32_ATTR_SELECTOR;
+
+       return 0;
+}
+
+int rtnl_u32_add_key_uint8(struct rtnl_cls *cls, uint8_t val, uint8_t mask,
+                          int off, int offmask)
+{
+       int shift = 24 - 8 * (off & 3);
+
+       return rtnl_u32_add_key(cls, htonl((uint32_t)val << shift),
+                               htonl((uint32_t)mask << shift),
+                               off & ~3, offmask);
+}
+
+/**
+ * Append new selector key to match a 16-bit number
+ *
+ * @arg cls    classifier to be modified
+ * @arg val    value to be matched (host byte-order)
+ * @arg mask   mask to be applied before matching (host byte-order)
+ * @arg off    offset, in bytes, to start matching
+ * @arg offmask        offset mask
+*/
+int rtnl_u32_add_key_uint16(struct rtnl_cls *cls, uint16_t val, uint16_t mask,
+                           int off, int offmask)
+{
+       int shift = ((off & 3) == 0 ? 16 : 0);
+       if (off % 2)
+               return nl_error(EINVAL, "Invalid offset alignment");
+
+       return rtnl_u32_add_key(cls, htonl((uint32_t)val << shift),
+                               htonl((uint32_t)mask << shift),
+                               off & ~3, offmask);
+}
+
+/**
+ * Append new selector key to match a 32-bit number
+ *
+ * @arg cls    classifier to be modified
+ * @arg val    value to be matched (host byte-order)
+ * @arg mask   mask to be applied before matching (host byte-order)
+ * @arg off    offset, in bytes, to start matching
+ * @arg offmask        offset mask
+*/
+int rtnl_u32_add_key_uint32(struct rtnl_cls *cls, uint32_t val, uint32_t mask,
+                           int off, int offmask)
+{
+       return rtnl_u32_add_key(cls, htonl(val), htonl(mask),
+                               off & ~3, offmask);
+}
+
+int rtnl_u32_add_key_in_addr(struct rtnl_cls *cls, struct in_addr *addr,
+                            uint8_t bitmask, int off, int offmask)
+{
+       uint32_t mask = 0xFFFFFFFF << (32 - bitmask);
+       return rtnl_u32_add_key(cls, addr->s_addr, htonl(mask), off, offmask);
+}
+
+int rtnl_u32_add_key_in6_addr(struct rtnl_cls *cls, struct in6_addr *addr,
+                             uint8_t bitmask, int off, int offmask)
+{
+       int i;
+
+       for (i = 1; i <= 4; i++) {
+               if (32 * i - bitmask <= 0)
+                       return rtnl_u32_add_key(cls, addr->s6_addr32[i-1],
+                                               0xFFFFFFFF, off, offmask );
+               else if (32 * i - bitmask < 32) {
+                       uint32_t mask = 0xFFFFFFFF << (32 * i - bitmask);
+                       return rtnl_u32_add_key(cls, addr->s6_addr32[i-1],
+                                               htonl(mask), off, offmask );
+               }
+               /* otherwise, if (32*i - bitmask >= 32) no key is generated */
+       }
+
+       return 0;
+}
+
+/** @} */
+
+static struct rtnl_cls_ops u32_ops = {
+       .co_kind                = "u32",
+       .co_msg_parser          = u32_msg_parser,
+       .co_free_data           = u32_free_data,
+       .co_get_opts            = u32_get_opts,
+       .co_dump[NL_DUMP_BRIEF] = u32_dump_brief,
+       .co_dump[NL_DUMP_FULL]  = u32_dump_full,
+       .co_dump[NL_DUMP_STATS] = u32_dump_stats,
+};
+
+static void __init u32_init(void)
+{
+       rtnl_cls_register(&u32_ops);
+}
+
+static void __exit u32_exit(void)
+{
+       rtnl_cls_unregister(&u32_ops);
+}
+
+/** @} */
diff --git a/lib/route/link.c b/lib/route/link.c
new file mode 100644 (file)
index 0000000..9c0f68d
--- /dev/null
@@ -0,0 +1,1460 @@
+/*
+ * lib/route/link.c    Links (Interfaces)
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup rtnl
+ * @defgroup link Links (Interfaces)
+ * @brief
+ *
+ * @par Link Identification
+ * A link can be identified by either its interface index or by its
+ * name. The kernel favours the interface index but falls back to the
+ * interface name if the interface index is lesser-than 0 for kernels
+ * >= 2.6.11. Therefore you can request changes without mapping a
+ * interface name to the corresponding index first.
+ *
+ * @par Changeable Attributes
+ * @anchor link_changeable
+ *  - Link layer address
+ *  - Link layer broadcast address
+ *  - device mapping (ifmap) (>= 2.6.9)
+ *  - MTU (>= 2.6.9)
+ *  - Transmission queue length (>= 2.6.9)
+ *  - Weight (>= 2.6.9)
+ *  - Link name (only via access through interface index) (>= 2.6.9)
+ *  - Flags (>= 2.6.9)
+ *    - IFF_DEBUG
+ *    - IFF_NOTRAILERS
+ *    - IFF_NOARP
+ *    - IFF_DYNAMIC
+ *    - IFF_MULTICAST
+ *    - IFF_PORTSEL
+ *    - IFF_AUTOMEDIA
+ *    - IFF_UP
+ *    - IFF_PROMISC
+ *    - IFF_ALLMULTI
+ *
+ * @par Link Flags (linux/if.h)
+ * @anchor link_flags
+ * @code
+ *   IFF_UP            Status of link (up|down)
+ *   IFF_BROADCAST     Indicates this link allows broadcasting
+ *   IFF_MULTICAST     Indicates this link allows multicasting
+ *   IFF_ALLMULTI      Indicates this link is doing multicast routing
+ *   IFF_DEBUG         Tell the driver to do debugging (currently unused)
+ *   IFF_LOOPBACK      This is the loopback link
+ *   IFF_POINTOPOINT   Point-to-point link
+ *   IFF_NOARP         Link is unable to perform ARP
+ *   IFF_PROMISC       Status of promiscious mode flag
+ *   IFF_MASTER        Used by teql
+ *   IFF_SLAVE         Used by teql
+ *   IFF_PORTSEL       Indicates this link allows port selection
+ *   IFF_AUTOMEDIA     Indicates this link selects port automatically
+ *   IFF_DYNAMIC       Indicates the address of this link is dynamic
+ *   IFF_RUNNING       Link is running and carrier is ok.
+ *   IFF_NOTRAILERS    Unused, BSD compat.
+ * @endcode
+ *
+ * @par Notes on IFF_PROMISC and IFF_ALLMULTI flags
+ * Although you can query the status of IFF_PROMISC and IFF_ALLMULTI
+ * they do not represent the actual state in the kernel but rather
+ * whether the flag has been enabled/disabled by userspace. The link
+ * may be in promiscious mode even if IFF_PROMISC is not set in a link
+ * dump request response because promiscity might be needed by the driver
+ * for a period of time.
+ *
+ * @par 1) Retrieving information about available links
+ * @code
+ * // The first step is to retrieve a list of all available interfaces within
+ * // the kernel and put them into a cache.
+ * struct nl_cache *cache = rtnl_link_alloc_cache(nl_handle);
+ *
+ * // In a second step, a specific link may be looked up by either interface
+ * // index or interface name.
+ * struct rtnl_link *link = rtnl_link_get_by_name(cache, "lo");
+ *
+ * // rtnl_link_get_by_name() is the short version for translating the
+ * // interface name to an interface index first like this:
+ * int ifindex = rtnl_link_name2i(cache, "lo");
+ * struct rtnl_link *link = rtnl_link_get(cache, ifindex);
+ *
+ * // After successful usage, the object must be given back to the cache
+ * rtnl_link_put(link);
+ * @endcode
+ *
+ * @par 2) Changing link attributes
+ * @code
+ * // In order to change any attributes of an existing link, we must allocate
+ * // a new link to hold the change requests:
+ * struct rtnl_link *request = rtnl_link_alloc();
+ *
+ * // Now we can go on and specify the attributes we want to change:
+ * rtnl_link_set_weight(request, 300);
+ * rtnl_link_set_mtu(request, 1360);
+ *
+ * // We can also shut an interface down administratively
+ * rtnl_link_unset_flags(request, rtnl_link_str2flags("up"));
+ *
+ * // Actually, we should know which link to change, so let's look it up
+ * struct rtnl_link *old = rtnl_link_get(cache, "eth0");
+ *
+ * // Two ways exist to commit this change request, the first one is to
+ * // build the required netlink message and send it out in one single
+ * // step:
+ * rtnl_link_change(nl_handle, old, request);
+ *
+ * // An alternative way is to build the netlink message and send it
+ * // out yourself using nl_send_auto_complete()
+ * struct nl_msg *msg = rtnl_link_build_change_request(old, request);
+ * nl_send_auto_complete(nl_handle, nlmsg_hdr(msg));
+ * nlmsg_free(msg);
+ *
+ * // Don't forget to give back the link object ;->
+ * rtnl_link_put(old);
+ * @endcode
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink/netlink.h>
+#include <netlink/attr.h>
+#include <netlink/utils.h>
+#include <netlink/object.h>
+#include <netlink/route/rtnl.h>
+#include <netlink/route/link.h>
+
+/** @cond SKIP */
+#define LINK_ATTR_MTU     0x0001
+#define LINK_ATTR_LINK    0x0002
+#define LINK_ATTR_TXQLEN  0x0004
+#define LINK_ATTR_WEIGHT  0x0008
+#define LINK_ATTR_MASTER  0x0010
+#define LINK_ATTR_QDISC   0x0020
+#define LINK_ATTR_MAP     0x0040
+#define LINK_ATTR_ADDR    0x0080
+#define LINK_ATTR_BRD     0x0100
+#define LINK_ATTR_FLAGS   0x0200
+#define LINK_ATTR_IFNAME  0x0400
+#define LINK_ATTR_IFINDEX 0x0800
+#define LINK_ATTR_FAMILY  0x1000
+#define LINK_ATTR_ARPTYPE 0x2000
+#define LINK_ATTR_STATS   0x4000
+#define LINK_ATTR_CHANGE  0x8000
+
+static struct nl_cache_ops rtnl_link_ops;
+/** @endcond */
+
+static void link_free_data(struct nl_object *c)
+{
+       struct rtnl_link *link = nl_object_priv(c);
+
+       if (link) {
+               nl_addr_put(link->l_addr);
+               nl_addr_put(link->l_bcast);
+       }
+}
+
+static struct nla_policy link_policy[IFLA_MAX+1] = {
+       [IFLA_IFNAME]   = { .type = NLA_STRING,
+                           .maxlen = IFNAMSIZ },
+       [IFLA_MTU]      = { .type = NLA_U32 },
+       [IFLA_TXQLEN]   = { .type = NLA_U32 },
+       [IFLA_LINK]     = { .type = NLA_U32 },
+       [IFLA_WEIGHT]   = { .type = NLA_U32 },
+       [IFLA_MASTER]   = { .type = NLA_U32 },
+       [IFLA_QDISC]    = { .type = NLA_STRING,
+                           .maxlen = IFQDISCSIZ },
+       [IFLA_STATS]    = { .minlen = sizeof(struct rtnl_link_stats) },
+       [IFLA_MAP]      = { .minlen = sizeof(struct rtnl_link_ifmap) },
+};
+
+static int link_msg_parser(struct sockaddr_nl *who, struct nlmsghdr *n,
+                          void *arg)
+{
+       struct rtnl_link *link;
+       struct ifinfomsg *ifi;
+       struct nlattr *tb[IFLA_MAX+1];
+       struct nl_parser_param *pp = arg;
+       int err;
+
+       link = rtnl_link_alloc();
+       if (link == NULL) {
+               err = nl_errno(ENOMEM);
+               goto errout;
+       }
+               
+       link->ce_msgtype = n->nlmsg_type;
+
+       err = nlmsg_parse(n, sizeof(*ifi), tb, IFLA_MAX, link_policy);
+       if (err < 0)
+               goto errout;
+
+       if (tb[IFLA_IFNAME] == NULL) {
+               err = nl_error(EINVAL, "Missing link name TLV");
+               goto errout;
+       }
+
+       nla_strlcpy(link->l_name, tb[IFLA_IFNAME], IFNAMSIZ);
+
+       ifi = nlmsg_data(n);
+       link->l_family = ifi->ifi_family;
+       link->l_arptype = ifi->ifi_type;
+       link->l_index = ifi->ifi_index;
+       link->l_flags = ifi->ifi_flags;
+       link->l_change = ifi->ifi_change;
+       link->l_mask = (LINK_ATTR_IFNAME | LINK_ATTR_FAMILY | LINK_ATTR_ARPTYPE|
+                       LINK_ATTR_IFINDEX | LINK_ATTR_FLAGS | LINK_ATTR_CHANGE);
+
+       if (tb[IFLA_STATS]) {
+               struct rtnl_link_stats *st = nla_data(tb[IFLA_STATS]);
+               
+               link->l_stats[RTNL_LINK_RX_PACKETS]     = st->rx_packets;
+               link->l_stats[RTNL_LINK_RX_BYTES]       = st->rx_bytes;
+               link->l_stats[RTNL_LINK_RX_ERRORS]      = st->rx_errors;
+               link->l_stats[RTNL_LINK_RX_DROPPED]     = st->rx_dropped;
+               link->l_stats[RTNL_LINK_RX_COMPRESSED]  = st->rx_compressed;
+               link->l_stats[RTNL_LINK_RX_FIFO_ERR]    = st->rx_fifo_errors;
+               link->l_stats[RTNL_LINK_TX_PACKETS]     = st->tx_packets;
+               link->l_stats[RTNL_LINK_TX_BYTES]       = st->tx_bytes;
+               link->l_stats[RTNL_LINK_TX_ERRORS]      = st->tx_errors;
+               link->l_stats[RTNL_LINK_TX_DROPPED]     = st->tx_dropped;
+               link->l_stats[RTNL_LINK_TX_COMPRESSED]  = st->tx_compressed;
+               link->l_stats[RTNL_LINK_TX_FIFO_ERR]    = st->tx_fifo_errors;
+               link->l_stats[RTNL_LINK_RX_LEN_ERR]     = st->rx_length_errors;
+               link->l_stats[RTNL_LINK_RX_OVER_ERR]    = st->rx_over_errors;
+               link->l_stats[RTNL_LINK_RX_CRC_ERR]     = st->rx_crc_errors;
+               link->l_stats[RTNL_LINK_RX_FRAME_ERR]   = st->rx_frame_errors;
+               link->l_stats[RTNL_LINK_RX_MISSED_ERR]  = st->rx_missed_errors;
+               link->l_stats[RTNL_LINK_TX_ABORT_ERR]   = st->tx_aborted_errors;
+               link->l_stats[RTNL_LINK_TX_CARRIER_ERR] = st->tx_carrier_errors;
+               link->l_stats[RTNL_LINK_TX_HBEAT_ERR]   = st->tx_heartbeat_errors;
+               link->l_stats[RTNL_LINK_TX_WIN_ERR]     = st->tx_window_errors;
+               link->l_stats[RTNL_LINK_MULTICAST]      = st->multicast;
+
+               link->l_mask |= LINK_ATTR_STATS;
+       }
+
+       if (tb[IFLA_TXQLEN]) {
+               link->l_txqlen = nla_get_u32(tb[IFLA_TXQLEN]);
+               link->l_mask |= LINK_ATTR_TXQLEN;
+       }
+
+       if (tb[IFLA_MTU]) {
+               link->l_mtu = nla_get_u32(tb[IFLA_MTU]);
+               link->l_mask |= LINK_ATTR_MTU;
+       }
+
+       if (tb[IFLA_ADDRESS]) {
+               link->l_addr = nla_get_addr(tb[IFLA_ADDRESS], AF_UNSPEC);
+               if (link->l_addr == NULL)
+                       goto errout;
+               nl_addr_set_family(link->l_addr,
+                                  nl_addr_guess_family(link->l_addr));
+               link->l_mask |= LINK_ATTR_ADDR;
+       }
+
+       if (tb[IFLA_BROADCAST]) {
+               link->l_bcast = nla_get_addr(tb[IFLA_BROADCAST], AF_UNSPEC);
+               if (link->l_bcast == NULL)
+                       goto errout;
+               nl_addr_set_family(link->l_bcast,
+                                  nl_addr_guess_family(link->l_bcast));
+               link->l_mask |= LINK_ATTR_BRD;
+       }
+
+       if (tb[IFLA_LINK]) {
+               link->l_link = nla_get_u32(tb[IFLA_LINK]);
+               link->l_mask |= LINK_ATTR_LINK;
+       }
+
+       if (tb[IFLA_WEIGHT]) {
+               link->l_weight = nla_get_u32(tb[IFLA_WEIGHT]);
+               link->l_mask |= LINK_ATTR_WEIGHT;
+       }
+
+       if (tb[IFLA_QDISC]) {
+               nla_strlcpy(link->l_qdisc, tb[IFLA_QDISC], IFQDISCSIZ);
+               link->l_mask |= LINK_ATTR_QDISC;
+       }
+
+       if (tb[IFLA_MAP]) {
+               struct rtnl_link_ifmap *map =  nla_data(tb[IFLA_MAP]);
+               link->l_map.lm_mem_start = map->mem_start;
+               link->l_map.lm_mem_end   = map->mem_end;
+               link->l_map.lm_base_addr = map->base_addr;
+               link->l_map.lm_irq       = map->irq;
+               link->l_map.lm_dma       = map->dma;
+               link->l_map.lm_port      = map->port;
+               link->l_mask |= LINK_ATTR_MAP;
+       }
+
+       if (tb[IFLA_MASTER]) {
+               link->l_master = nla_get_u32(tb[IFLA_MASTER]);
+               link->l_mask |= LINK_ATTR_MASTER;
+       }
+
+       err = pp->pp_cb((struct nl_object *) link, pp);
+       if (err < 0)
+               goto errout;
+
+       return P_ACCEPT;
+
+errout:
+       rtnl_link_put(link);
+       return err;
+}
+
+static int link_request_update(struct nl_cache *c, struct nl_handle *h)
+{
+       return nl_rtgen_request(h, RTM_GETLINK, AF_UNSPEC, NLM_F_DUMP);
+}
+
+static int link_dump_brief(struct nl_object *obj, struct nl_dump_params *p)
+{
+       char buf[128];
+       struct nl_cache *cache = dp_cache(obj);
+       struct rtnl_link *link = (struct rtnl_link *) obj;
+       int line = 1;
+
+       dp_dump(p, "%s ", link->l_name);
+
+       if (link->l_mask & LINK_ATTR_LINK) {
+               struct rtnl_link *ll = rtnl_link_get(cache, link->l_link);
+               dp_dump(p, "@%s", ll ? ll->l_name : "NONE");
+               if (ll)
+                       rtnl_link_put(ll);
+       }
+
+       dp_dump(p, "%s ", nl_llproto2str(link->l_arptype, buf, sizeof(buf)));
+       dp_dump(p, "%s ", link->l_addr ?  nl_addr2str(link->l_addr, buf,
+                                                    sizeof(buf)) : "none");
+       dp_dump(p, "mtu %u ", link->l_mtu);
+
+       if (link->l_mask & LINK_ATTR_MASTER) {
+               struct rtnl_link *master = rtnl_link_get(cache, link->l_master);
+               dp_dump(p, "master %s ", master ? master->l_name : "inv");
+               if (master)
+                       rtnl_link_put(master);
+       }
+
+       rtnl_link_flags2str(link->l_flags, buf, sizeof(buf));
+       if (buf[0])
+               dp_dump(p, "<%s>", buf);
+
+       dp_dump(p, "\n");
+
+       return line;
+}
+
+static int link_dump_full(struct nl_object *obj, struct nl_dump_params *p)
+{
+       struct rtnl_link *link = (struct rtnl_link *) obj;
+       char buf[64];
+       int line;
+
+       line = link_dump_brief(obj, p);
+       dp_new_line(p, line++);
+
+       dp_dump(p, "    txqlen %u weight %u ", link->l_txqlen, link->l_weight);
+
+       if (link->l_mask & LINK_ATTR_QDISC)
+               dp_dump(p, "qdisc %s ", link->l_qdisc);
+
+       if (link->l_mask & LINK_ATTR_MAP && link->l_map.lm_irq)
+               dp_dump(p, "irq %u ", link->l_map.lm_irq);
+
+       if (link->l_mask & LINK_ATTR_IFINDEX)
+               dp_dump(p, "index %u ", link->l_index);
+
+       if (link->l_mask & LINK_ATTR_BRD)
+               dp_dump(p, "brd %s", nl_addr2str(link->l_bcast, buf,
+                                                  sizeof(buf)));
+
+       dp_dump(p, "\n");
+
+       return line;
+}
+
+static int link_dump_stats(struct nl_object *obj, struct nl_dump_params *p)
+{
+       struct rtnl_link *link = (struct rtnl_link *) obj;
+       char *unit, fmt[64];
+       float res;
+       int line;
+       
+       line = link_dump_full(obj, p);
+
+       dp_dump_line(p, line++, "    Stats:    bytes    packets     errors "
+                               "   dropped   fifo-err compressed\n");
+
+       res = nl_cancel_down_bytes(link->l_stats[RTNL_LINK_RX_BYTES], &unit);
+
+       strcpy(fmt, "    RX  %X.2f %s %10llu %10llu %10llu %10llu %10llu\n");
+       fmt[9] = *unit == 'B' ? '9' : '7';
+       
+       dp_dump_line(p, line++, fmt,
+               res, unit,
+               link->l_stats[RTNL_LINK_RX_PACKETS],
+               link->l_stats[RTNL_LINK_RX_ERRORS],
+               link->l_stats[RTNL_LINK_RX_DROPPED],
+               link->l_stats[RTNL_LINK_RX_FIFO_ERR],
+               link->l_stats[RTNL_LINK_RX_COMPRESSED]);
+
+       res = nl_cancel_down_bytes(link->l_stats[RTNL_LINK_TX_BYTES], &unit);
+
+       strcpy(fmt, "    TX  %X.2f %s %10llu %10llu %10llu %10llu %10llu\n");
+       fmt[9] = *unit == 'B' ? '9' : '7';
+       
+       dp_dump_line(p, line++, fmt,
+               res, unit,
+               link->l_stats[RTNL_LINK_TX_PACKETS],
+               link->l_stats[RTNL_LINK_TX_ERRORS],
+               link->l_stats[RTNL_LINK_TX_DROPPED],
+               link->l_stats[RTNL_LINK_TX_FIFO_ERR],
+               link->l_stats[RTNL_LINK_TX_COMPRESSED]);
+
+       dp_dump_line(p, line++, "    Errors:  length       over        crc "
+                               "     frame     missed  multicast\n");
+
+       dp_dump_line(p, line++, "    RX   %10" PRIu64 " %10" PRIu64 " %10"
+                               PRIu64 " %10" PRIu64 " %10" PRIu64 " %10"
+                               PRIu64 "\n",
+               link->l_stats[RTNL_LINK_RX_LEN_ERR],
+               link->l_stats[RTNL_LINK_RX_OVER_ERR],
+               link->l_stats[RTNL_LINK_RX_CRC_ERR],
+               link->l_stats[RTNL_LINK_RX_FRAME_ERR],
+               link->l_stats[RTNL_LINK_RX_MISSED_ERR],
+               link->l_stats[RTNL_LINK_MULTICAST]);
+
+       dp_dump_line(p, line++, "    Errors: aborted    carrier  heartbeat "
+                               "    window  collision\n");
+       
+       dp_dump_line(p, line++, "    TX   %10" PRIu64 " %10" PRIu64 " %10"
+                               PRIu64 " %10" PRIu64 " %10" PRIu64 "\n",
+               link->l_stats[RTNL_LINK_TX_ABORT_ERR],
+               link->l_stats[RTNL_LINK_TX_CARRIER_ERR],
+               link->l_stats[RTNL_LINK_TX_HBEAT_ERR],
+               link->l_stats[RTNL_LINK_TX_WIN_ERR],
+               link->l_stats[RTNL_LINK_TX_COLLISIONS]);
+
+       return line;
+}
+
+static int link_dump_xml(struct nl_object *obj, struct nl_dump_params *p)
+{
+       struct rtnl_link *link = (struct rtnl_link *) obj;
+       struct nl_cache *cache = dp_cache(obj);
+       char buf[128];
+       int i, line = 0;
+
+       dp_dump_line(p, line++, "<link name=\"%s\" index=\"%u\">\n",
+                    link->l_name, link->l_index);
+       dp_dump_line(p, line++, "  <family>%s</family>\n",
+                    nl_af2str(link->l_family, buf, sizeof(buf)));
+       dp_dump_line(p, line++, "  <arptype>%s</arptype>\n",
+                    nl_llproto2str(link->l_arptype, buf, sizeof(buf)));
+       dp_dump_line(p, line++, "  <address>%s</address>\n",
+                    nl_addr2str(link->l_addr, buf, sizeof(buf)));
+       dp_dump_line(p, line++, "  <mtu>%u</mtu>\n", link->l_mtu);
+       dp_dump_line(p, line++, "  <txqlen>%u</txqlen>\n", link->l_txqlen);
+       dp_dump_line(p, line++, "  <weight>%u</weight>\n", link->l_weight);
+
+       rtnl_link_flags2str(link->l_flags, buf, sizeof(buf));
+       if (buf[0])
+               dp_dump_line(p, line++, "  <flags>%s</flags>\n", buf);
+
+       if (link->l_mask & LINK_ATTR_QDISC)
+               dp_dump_line(p, line++, "  <qdisc>%s</qdisc>\n", link->l_qdisc);
+
+       if (link->l_mask & LINK_ATTR_LINK) {
+               struct rtnl_link *ll = rtnl_link_get(cache, link->l_link);
+               dp_dump_line(p, line++, "  <link>%s</link>\n",
+                            ll ? ll->l_name : "none");
+               if (ll)
+                       rtnl_link_put(ll);
+       }
+
+       if (link->l_mask & LINK_ATTR_MASTER) {
+               struct rtnl_link *master = rtnl_link_get(cache, link->l_master);
+               dp_dump_line(p, line++, "  <master>%s</master>\n",
+                            master ? master->l_name : "none");
+               if (master)
+                       rtnl_link_put(master);
+       }
+
+       if (link->l_mask & LINK_ATTR_BRD)
+               dp_dump_line(p, line++, "  <broadcast>%s</broadcast>\n",
+                            nl_addr2str(link->l_bcast, buf, sizeof(buf)));
+
+       if (link->l_mask & LINK_ATTR_STATS) {
+               dp_dump_line(p, line++, "  <stats>\n");
+               for (i = 0; i <= RTNL_LINK_STATS_MAX; i++) {
+                       rtnl_link_stat2str(i, buf, sizeof(buf));
+                       dp_dump_line(p, line++,
+                                    "    <%s>%" PRIu64 "</%s>\n",
+                                    buf, link->l_stats[i], buf);
+               }
+               dp_dump_line(p, line++, "  </stats>\n");
+       }
+
+       dp_dump_line(p, line++, "</link>\n");
+
+#if 0
+       uint32_t        l_change;       /**< Change mask */
+       struct rtnl_lifmap l_map;       /**< Interface device mapping */
+#endif
+
+       return line;
+}
+
+static int link_dump_env(struct nl_object *obj, struct nl_dump_params *p)
+{
+       struct rtnl_link *link = (struct rtnl_link *) obj;
+       struct nl_cache *cache = dp_cache(obj);
+       char buf[128];
+       int i, line = 0;
+
+       dp_dump_line(p, line++, "LINK_NAME=%s\n", link->l_name);
+       dp_dump_line(p, line++, "LINK_IFINDEX=%u\n", link->l_index);
+       dp_dump_line(p, line++, "LINK_FAMILY=%s\n",
+                    nl_af2str(link->l_family, buf, sizeof(buf)));
+       dp_dump_line(p, line++, "LINK_TYPE=%s\n",
+                    nl_llproto2str(link->l_arptype, buf, sizeof(buf)));
+       if (link->l_mask & LINK_ATTR_ADDR)
+               dp_dump_line(p, line++, "LINK_ADDRESS=%s\n",
+                            nl_addr2str(link->l_addr, buf, sizeof(buf)));
+       dp_dump_line(p, line++, "LINK_MTU=%u\n", link->l_mtu);
+       dp_dump_line(p, line++, "LINK_TXQUEUELEN=%u\n", link->l_txqlen);
+       dp_dump_line(p, line++, "LINK_WEIGHT=%u\n", link->l_weight);
+
+       rtnl_link_flags2str(link->l_flags & ~IFF_RUNNING, buf, sizeof(buf));
+       if (buf[0])
+               dp_dump_line(p, line++, "LINK_FLAGS=%s\n", buf);
+
+       if (link->l_mask & LINK_ATTR_QDISC)
+               dp_dump_line(p, line++, "LINK_QDISC=%s\n", link->l_qdisc);
+
+       if (link->l_mask & LINK_ATTR_LINK) {
+               struct rtnl_link *ll = rtnl_link_get(cache, link->l_link);
+
+               dp_dump_line(p, line++, "LINK_LINK_IFINDEX=%d\n", link->l_link);
+               if (ll) {
+                       dp_dump_line(p, line++, "LINK_LINK_IFNAME=%s\n",
+                                    ll->l_name);
+                       rtnl_link_put(ll);
+               }
+       }
+
+       if (link->l_mask & LINK_ATTR_MASTER) {
+               struct rtnl_link *master = rtnl_link_get(cache, link->l_master);
+               dp_dump_line(p, line++, "LINK_MASTER=%s\n",
+                            master ? master->l_name : "none");
+               if (master)
+                       rtnl_link_put(master);
+       }
+
+       if (link->l_mask & LINK_ATTR_BRD)
+               dp_dump_line(p, line++, "LINK_BROADCAST=%s\n",
+                            nl_addr2str(link->l_bcast, buf, sizeof(buf)));
+
+       if (link->l_mask & LINK_ATTR_STATS) {
+               for (i = 0; i <= RTNL_LINK_STATS_MAX; i++) {
+                       char *c = buf;
+
+                       sprintf(buf, "LINK_");
+                       rtnl_link_stat2str(i, buf + 5, sizeof(buf) - 5);
+                       while (*c) {
+                               *c = toupper(*c);
+                               c++;
+                       }
+                       dp_dump_line(p, line++,
+                                    "%s=%" PRIu64 "\n", buf, link->l_stats[i]);
+               }
+       }
+
+       return line;
+}
+
+#if 0
+static int link_handle_event(struct nl_object *a, struct rtnl_link_event_cb *cb)
+{
+       struct rtnl_link *l = (struct rtnl_link *) a;
+       struct nl_cache *c = dp_cache(a);
+       int nevents = 0;
+
+       if (l->l_change == ~0U) {
+               if (l->ce_msgtype == RTM_NEWLINK)
+                       cb->le_register(l);
+               else
+                       cb->le_unregister(l);
+
+               return 1;
+       }
+
+       if (l->l_change & IFF_SLAVE) {
+               if (l->l_flags & IFF_SLAVE) {
+                       struct rtnl_link *m = rtnl_link_get(c, l->l_master);
+                       cb->le_new_bonding(l, m);
+                       if (m)
+                               rtnl_link_put(m);
+               } else
+                       cb->le_cancel_bonding(l);
+       }
+
+#if 0
+       if (l->l_change & IFF_UP && l->l_change & IFF_RUNNING)
+               dp_dump_line(p, line++, "link %s changed state to %s.\n",
+                       l->l_name, l->l_flags & IFF_UP ? "up" : "down");
+
+       if (l->l_change & IFF_PROMISC) {
+               dp_new_line(p, line++);
+               dp_dump(p, "link %s %s promiscuous mode.\n",
+                   l->l_name, l->l_flags & IFF_PROMISC ? "entered" : "left");
+       }
+
+       if (line == 0)
+               dp_dump_line(p, line++, "link %s sent unknown event.\n",
+                            l->l_name);
+#endif
+
+       return nevents;
+}
+#endif
+
+static int link_filter(struct nl_object *obj, struct nl_object *filter)
+{
+       struct rtnl_link *o = (struct rtnl_link *) obj;
+       struct rtnl_link *f = (struct rtnl_link *) filter;
+
+#define REQ(F) (f->l_mask & LINK_ATTR_##F)
+#define AVAIL(F) (o->l_mask & LINK_ATTR_##F)
+#define F_CUS(F, EXPR) (REQ(F) && (!AVAIL(F) || (EXPR)))
+#define F_INT(F, N) (REQ(F) && (!AVAIL(F) || (o->N != f->N)))
+       if (F_INT(MTU,  l_mtu)                                          ||
+           F_INT(LINK, l_link)                                         ||
+           F_INT(TXQLEN,       l_txqlen)                               ||
+           F_INT(WEIGHT,       l_weight)                               ||
+           F_INT(MASTER,       l_master)                               ||
+           F_INT(IFINDEX,      l_index)                                ||
+           F_INT(FAMILY,       l_family)                               ||
+           F_CUS(QDISC,        strcmp(o->l_qdisc, f->l_qdisc))         ||
+           F_CUS(IFNAME,       strcmp(o->l_name, f->l_name))           ||
+           F_CUS(ADDR, nl_addr_cmp(o->l_addr, f->l_addr))              ||
+           F_CUS(BRD,  nl_addr_cmp(o->l_bcast, f->l_bcast))            ||
+           F_CUS(FLAGS,        f->l_flags ^ (o->l_flags & f->l_flag_mask)))
+               return 0;
+#undef REQ
+#undef AVAIL
+#undef F_CUS
+#undef F_INT
+
+       return 1;
+}
+
+/**
+ * @name Link Object Allocation/Freeage
+ * @{
+ */
+
+/**
+ * Allocate and initialize new link object.
+ * @note Free the memory after usage using rtnl_link_put() or rtnl_link_free().
+ * @return Newly allocated link object or NULL if an error occured.
+ */
+struct rtnl_link *rtnl_link_alloc(void)
+{
+       return (struct rtnl_link *) nl_object_alloc_from_ops(&rtnl_link_ops);
+}
+
+/**
+ * Give back reference on link object.
+ * @arg link           Link object to be given back.
+ *
+ * Decrements the reference counter and frees the object if the
+ * last reference has been released.
+ */
+void rtnl_link_put(struct rtnl_link *link)
+{
+       nl_object_put((struct nl_object *) link);
+}
+/**
+ * Free link object.
+ * @arg link           Link object to be freed.
+ *
+ * @note Always use rtnl_link_put() unless you're absolutely sure
+ *       that no other user may have a reference on this object.
+ */
+void rtnl_link_free(struct rtnl_link *link)
+{
+       nl_object_free((struct nl_object *) link);
+}
+
+/** @} */
+
+/**
+ * @name Link Cache Management
+ * @{
+ */
+
+
+/**
+ * Allocate link cache and fill in all configured links.
+ * @arg handle         Netlink handle.
+ *
+ * Allocates a new link cache, initializes it properly and updates it
+ * to include all links currently configured in the kernel.
+ *
+ * @note Free the memory after usage.
+ * @return Newly allocated cache or NULL if an error occured.
+ */
+struct nl_cache *rtnl_link_alloc_cache(struct nl_handle *handle)
+{
+       struct nl_cache * cache;
+       
+       cache = nl_cache_alloc_from_ops(&rtnl_link_ops);
+       if (cache == NULL)
+               return NULL;
+       
+       if (nl_cache_update(handle, cache) < 0) {
+               nl_cache_free(cache);
+               return NULL;
+       }
+
+       return cache;
+}
+
+/**
+ * Look up link by interface index in the provided cache
+ * @arg cache          link cache
+ * @arg ifindex                link interface index
+ *
+ * The caller owns a reference on the returned object and
+ * must give the object back via rtnl_link_put().
+ *
+ * @return pointer to link inside the cache or NULL if no match was found.
+ */
+struct rtnl_link *rtnl_link_get(struct nl_cache *cache, int ifindex)
+{
+       struct rtnl_link *link;
+
+       if (cache->c_ops != &rtnl_link_ops)
+               return NULL;
+
+       nl_list_for_each_entry(link, &cache->c_items, ce_list) {
+               if (link->l_index == ifindex) {
+                       nl_object_get((struct nl_object *) link);
+                       return link;
+               }
+       }
+
+       return NULL;
+}
+
+/**
+ * Look up link by link name in the provided cache
+ * @arg cache          link cache
+ * @arg name           link name
+ *
+ * The caller owns a reference on the returned object and
+ * must give the object back via rtnl_link_put().
+ *
+ * @return pointer to link inside the cache or NULL if no match was found.
+ */
+struct rtnl_link *rtnl_link_get_by_name(struct nl_cache *cache,
+                                        const char *name)
+{
+       struct rtnl_link *link;
+
+       if (cache->c_ops != &rtnl_link_ops)
+               return NULL;
+
+       nl_list_for_each_entry(link, &cache->c_items, ce_list) {
+               if (!strcmp(name, link->l_name)) {
+                       nl_object_get((struct nl_object *) link);
+                       return link;
+               }
+       }
+
+       return NULL;
+}
+
+/** @} */
+
+/**
+ * @name Link Modifications
+ * @{
+ */
+
+/**
+ * Builds a netlink change request message to change link attributes
+ * @arg old            link to be changed
+ * @arg tmpl           template with requested changes
+ * @arg flags          additional netlink message flags
+ *
+ * Builds a new netlink message requesting a change of link attributes.
+ * The netlink message header isn't fully equipped with all relevant
+ * fields and must be sent out via nl_send_auto_complete() or
+ * supplemented as needed.
+ * \a old must point to a link currently configured in the kernel
+ * and \a tmpl must contain the attributes to be changed set via
+ * \c rtnl_link_set_* functions.
+ *
+ * @return New netlink message
+ * @note Not all attributes can be changed, see
+ *       \ref link_changeable "Changeable Attributes" for more details.
+ */
+struct nl_msg * rtnl_link_build_change_request(struct rtnl_link *old,
+                                              struct rtnl_link *tmpl,
+                                              int flags)
+{
+       struct nl_msg *msg;
+       struct ifinfomsg ifi = {
+               .ifi_family = old->l_family,
+               .ifi_index = old->l_index,
+       };
+
+       if (tmpl->l_mask & LINK_ATTR_FLAGS) {
+               ifi.ifi_flags = old->l_flags & ~tmpl->l_flag_mask;
+               ifi.ifi_flags |= tmpl->l_flags;
+       }
+
+       msg = nlmsg_build_simple(RTM_SETLINK, flags);
+       if (!msg)
+               goto nla_put_failure;
+
+       if (nlmsg_append(msg, &ifi, sizeof(ifi), 1) < 0)
+               goto nla_put_failure;
+
+       if (tmpl->l_mask & LINK_ATTR_ADDR)
+               NLA_PUT_ADDR(msg, IFLA_ADDRESS, tmpl->l_addr);
+
+       if (tmpl->l_mask & LINK_ATTR_BRD)
+               NLA_PUT_ADDR(msg, IFLA_BROADCAST, tmpl->l_bcast);
+
+       if (tmpl->l_mask & LINK_ATTR_MTU)
+               NLA_PUT_U32(msg, IFLA_MTU, tmpl->l_mtu);
+
+       if (tmpl->l_mask & LINK_ATTR_TXQLEN)
+               NLA_PUT_U32(msg, IFLA_TXQLEN, tmpl->l_txqlen);
+
+       if (tmpl->l_mask & LINK_ATTR_WEIGHT)
+               NLA_PUT_U32(msg, IFLA_WEIGHT, tmpl->l_weight);
+
+       if (tmpl->l_mask & LINK_ATTR_IFNAME)
+               NLA_PUT_STRING(msg, IFLA_IFNAME, tmpl->l_name);
+
+       return msg;
+
+nla_put_failure:
+       nlmsg_free(msg);
+       return NULL;
+}
+
+/**
+ * Change link attributes
+ * @arg handle         netlink handle
+ * @arg old            link to be changed
+ * @arg tmpl           template with requested changes
+ * @arg flags          additional netlink message flags
+ *
+ * Builds a new netlink message by calling rtnl_link_build_change_request(),
+ * sends the request to the kernel and waits for the next ACK to be
+ * received, i.e. blocks until the request has been processed.
+ *
+ * @return 0 on success or a negative error code
+ * @note Not all attributes can be changed, see
+ *       \ref link_changeable "Changeable Attributes" for more details.
+ */
+int rtnl_link_change(struct nl_handle *handle, struct rtnl_link *old,
+                    struct rtnl_link *tmpl, int flags)
+{
+       int err;
+       struct nl_msg *msg;
+       
+       msg = rtnl_link_build_change_request(old, tmpl, flags);
+       if (!msg)
+               return nl_errno(ENOMEM);
+       
+       err = nl_send_auto_complete(handle, msg);
+       if (err < 0)
+               return err;
+
+       nlmsg_free(msg);
+       return nl_wait_for_ack(handle);
+}
+
+/** @} */
+
+/**
+ * @name Name <-> Index Translations
+ * @{
+ */
+
+/**
+ * Translate an interface index to the corresponding link name
+ * @arg cache          link cache
+ * @arg ifindex                link interface index
+ * @arg dst            destination buffer
+ * @arg len            length of destination buffer
+ *
+ * Translates the specified interface index to the corresponding
+ * link name and stores the name in the destination buffer.
+ *
+ * @return link name or NULL if no match was found.
+ */
+char * rtnl_link_i2name(struct nl_cache *cache, int ifindex, char *dst,
+                       size_t len)
+{
+       struct rtnl_link *link = rtnl_link_get(cache, ifindex);
+
+       if (link) {
+               strncpy(dst, link->l_name, len - 1);
+               rtnl_link_put(link);
+               return dst;
+       }
+
+       return NULL;
+}
+
+/**
+ * Translate a link name to the corresponding interface index
+ * @arg cache          link cache
+ * @arg name           link name
+ *
+ * @return interface index or RTNL_LINK_NOT_FOUND if no match was found.
+ */
+int rtnl_link_name2i(struct nl_cache *cache, const char *name)
+{
+       int ifindex = RTNL_LINK_NOT_FOUND;
+       struct rtnl_link *link;
+       
+       link = rtnl_link_get_by_name(cache, name);
+       if (link) {
+               ifindex = link->l_index;
+               rtnl_link_put(link);
+       }
+
+       return ifindex;
+}
+
+/** @} */
+
+/**
+ * @name Link Flags Translations
+ * @{
+ */
+
+static struct trans_tbl link_flags[] = {
+       __ADD(IFF_LOOPBACK, loopback)
+       __ADD(IFF_BROADCAST, broadcast)
+       __ADD(IFF_POINTOPOINT, pointopoint)
+       __ADD(IFF_MULTICAST, multicast)
+       __ADD(IFF_NOARP, noarp)
+       __ADD(IFF_ALLMULTI, allmulti)
+       __ADD(IFF_PROMISC, promisc)
+       __ADD(IFF_MASTER, master)
+       __ADD(IFF_SLAVE, slave)
+       __ADD(IFF_DEBUG, debug)
+       __ADD(IFF_DYNAMIC, dynamic)
+       __ADD(IFF_AUTOMEDIA, automedia)
+       __ADD(IFF_PORTSEL, portsel)
+       __ADD(IFF_NOTRAILERS, notrailers)
+       __ADD(IFF_UP, up)
+       __ADD(IFF_RUNNING, running)
+       __ADD(IFF_LOWER_UP, lowerup)
+       __ADD(IFF_DORMANT, dormant)
+};
+
+/**
+ * Convert link flags to a character string (Reentrant).
+ * @arg flags          link flags
+ * @arg buf            destination buffer
+ * @arg len            buffer length
+ *
+ * Converts link flags to a character string separated by
+ * commands and stores it in the specified destination buffer.
+ *
+ * \return The destination buffer
+ */
+char * rtnl_link_flags2str(int flags, char *buf, size_t len)
+{
+       return __flags2str(flags, buf, len, link_flags,
+                          ARRAY_SIZE(link_flags));
+}
+
+/**
+ * Convert a character string to a link flag
+ * @arg name           Name of link flag
+ *
+ * Converts the provided character string specifying a link
+ * link the corresponding numeric value.
+ *
+ * \return Link flag or a negative value if none was found.
+ */
+int rtnl_link_str2flags(const char *name)
+{
+       return __str2flags(name, link_flags, ARRAY_SIZE(link_flags));
+}
+
+/** @} */
+
+/**
+ * @name Link Statistics Translations
+ * @{
+ */
+
+static struct trans_tbl link_stats[] = {
+       __ADD(RTNL_LINK_RX_PACKETS, rx_packets)
+       __ADD(RTNL_LINK_TX_PACKETS, tx_packets)
+       __ADD(RTNL_LINK_RX_BYTES, rx_bytes)
+       __ADD(RTNL_LINK_TX_BYTES, tx_bytes)
+       __ADD(RTNL_LINK_RX_ERRORS, rx_errors)
+       __ADD(RTNL_LINK_TX_ERRORS, tx_errors)
+       __ADD(RTNL_LINK_RX_DROPPED, rx_dropped)
+       __ADD(RTNL_LINK_TX_DROPPED, tx_dropped)
+       __ADD(RTNL_LINK_RX_COMPRESSED, rx_compressed)
+       __ADD(RTNL_LINK_TX_COMPRESSED, tx_compressed)
+       __ADD(RTNL_LINK_RX_FIFO_ERR, rx_fifo_err)
+       __ADD(RTNL_LINK_TX_FIFO_ERR, tx_fifo_err)
+       __ADD(RTNL_LINK_RX_LEN_ERR, rx_len_err)
+       __ADD(RTNL_LINK_RX_OVER_ERR, rx_over_err)
+       __ADD(RTNL_LINK_RX_CRC_ERR, rx_crc_err)
+       __ADD(RTNL_LINK_RX_FRAME_ERR, rx_frame_err)
+       __ADD(RTNL_LINK_RX_MISSED_ERR, rx_missed_err)
+       __ADD(RTNL_LINK_TX_ABORT_ERR, tx_abort_err)
+       __ADD(RTNL_LINK_TX_CARRIER_ERR, tx_carrier_err)
+       __ADD(RTNL_LINK_TX_HBEAT_ERR, tx_hbeat_err)
+       __ADD(RTNL_LINK_TX_WIN_ERR, tx_win_err)
+       __ADD(RTNL_LINK_TX_COLLISIONS, tx_collision)
+       __ADD(RTNL_LINK_MULTICAST, multicast)
+};
+
+/**
+ * Convert a link statistic to a character string (Reentrant).
+ * @arg st             link statistic
+ * @arg buf            destination buffer
+ * @arg len            buffer length
+ *
+ * Converts a link statistic to a character string and stores it in
+ * the specified destination buffer.
+ *
+ * @return The destination buffer or the statistic encoded in
+ *         hexidecimal form if no match was found.
+ */
+char *rtnl_link_stat2str(int st, char *buf, size_t len)
+{
+       return __type2str(st, buf, len, link_stats, ARRAY_SIZE(link_stats));
+}
+
+/**
+ * Convert a character string to a link statistic
+ * @arg name           name of link statistic
+ *
+ * Converts the provided character string specifying a link
+ * statistic to the corresponding numeric value.
+ *
+ * @return Link statistic or a negative value if none was found.
+ */
+int rtnl_link_str2stat(const char *name)
+{
+       return __str2type(name, link_stats, ARRAY_SIZE(link_stats));
+}
+
+/** @} */
+
+/**
+ * @name Attribute Modification
+ * @{
+ */
+
+/**
+ * Set QDisc name
+ * @arg link           link to change
+ * @arg qdisc          qdisc name
+ */
+void rtnl_link_set_qdisc(struct rtnl_link *link, const char *qdisc)
+{
+       strncpy(link->l_qdisc, qdisc, sizeof(link->l_qdisc) - 1);
+       link->l_mask |= LINK_ATTR_QDISC;
+}
+
+/**
+ * Get QDisc name
+ * @arg link           link handle
+ * @return Name of the qdisc or NULL if not set.
+ */
+char *rtnl_link_get_qdisc(struct rtnl_link *link)
+{
+       if (link->l_mask & LINK_ATTR_QDISC)
+               return link->l_qdisc;
+       else
+               return NULL;
+}
+
+/**
+ * Set new link name
+ * @arg link           link to change
+ * @arg name           new link name
+ */
+void rtnl_link_set_name(struct rtnl_link *link, const char *name)
+{
+       strncpy(link->l_name, name, sizeof(link->l_name) - 1);
+       link->l_mask |= LINK_ATTR_IFNAME;
+}
+
+/**
+ * Get link name
+ * @arg link           link handle
+ * @return Name of the link or NULL if not set.
+ */
+char *rtnl_link_get_name(struct rtnl_link *link)
+{
+       if (link->l_mask & LINK_ATTR_IFNAME)
+               return link->l_name;
+       else
+               return NULL;
+}
+
+static inline void __assign_addr(struct rtnl_link *link, struct nl_addr **pos,
+                                struct nl_addr *new, int flag)
+{
+       if (*pos)
+               nl_addr_put(*pos);
+
+       nl_addr_get(new);
+       *pos = new;
+
+       link->l_mask |= flag;
+}
+
+/**
+ * Set link layer address
+ * @arg link           link to change
+ * @arg addr           new link layer address
+ */
+void rtnl_link_set_addr(struct rtnl_link *link, struct nl_addr *addr)
+{
+       __assign_addr(link, &link->l_addr, addr, LINK_ATTR_ADDR);
+}
+
+/**
+ * Get link layer address
+ * @arg link           link handle
+ * @return link layer address or NULL if not set
+ */
+struct nl_addr *rtnl_link_get_addr(struct rtnl_link *link)
+{
+       if (link->l_mask & LINK_ATTR_ADDR)
+               return link->l_addr;
+       else
+               return NULL;
+}
+
+/**
+ * Set link layer broadcast address
+ * @arg link           link to change
+ * @arg brd            new link layer broadcast address
+ *
+ * Assigns the new broadcast address to the specified link handle.
+ *
+ * @note The prefix length of the address will be ignored.
+ */
+void rtnl_link_set_broadcast(struct rtnl_link *link, struct nl_addr *brd)
+{
+       __assign_addr(link, &link->l_bcast, brd, LINK_ATTR_BRD);
+}
+
+/**
+ * Get link layer broadcast address
+ * @arg link           link handle
+ * @return Link layer broadcast address or NULL if not set
+ */
+struct nl_addr *rtnl_link_get_broadcast(struct rtnl_link *link)
+{
+       if (link->l_mask & LINK_ATTR_BRD)
+               return link->l_bcast;
+       else
+               return NULL;
+}
+
+/**
+ * Set flags
+ * @arg link           link to change
+ * @arg flags          flags to set (see \ref link_flags "Link Flags")
+ */
+void rtnl_link_set_flags(struct rtnl_link *link, unsigned int flags)
+{
+       link->l_flag_mask |= flags;
+       link->l_flags |= flags;
+       link->l_mask |= LINK_ATTR_FLAGS;
+}
+
+/**
+ * Unset flags
+ * @arg link           link to change
+ * @arg flags          flags to unset (see \ref link_flags "Link Flags")
+ */
+void rtnl_link_unset_flags(struct rtnl_link *link, unsigned int flags)
+{
+       link->l_flag_mask |= flags;
+       link->l_flags &= ~flags;
+       link->l_mask |= LINK_ATTR_FLAGS;
+}
+
+/**
+ * Get flags
+ * @arg link           link handle
+ * @return Link flags
+ */
+unsigned int rtnl_link_get_flags(struct rtnl_link *link)
+{
+       return link->l_flags;
+}
+
+/**
+ * Set link layer address family
+ * @arg link           link to change
+ * @arg family         new address family
+ */
+void rtnl_link_set_family(struct rtnl_link *link, int family)
+{
+       link->l_family = family;
+       link->l_mask |= LINK_ATTR_FAMILY;
+}
+
+/**
+ * Get link layer address family
+ * @arg link           link handle
+ * @return Link layer address family or AF_UNSPEC if not set.
+ */
+int rtnl_link_get_family(struct rtnl_link *link)
+{
+       if (link->l_family & LINK_ATTR_FAMILY)
+               return link->l_family;
+       else
+               return AF_UNSPEC;
+}
+
+/**
+ * Set link layer type
+ * @arg link           link handle
+ * @arg arptype                Link layer type.
+ */
+void rtnl_link_set_type(struct rtnl_link *link, unsigned int arptype)
+{
+       link->l_arptype = arptype;
+}
+
+/**
+ * Get link layer type
+ * @arg link           link handle
+ * @return Link layer type.
+ */
+unsigned int rtnl_link_get_arptype(struct rtnl_link *link)
+{
+       return link->l_arptype;
+}
+
+/**
+ * Set interface index
+ * @arg link           link to change
+ * @arg ifindex                new interface index
+ */
+void rtnl_link_set_ifindex(struct rtnl_link *link, int ifindex)
+{
+       link->l_index = ifindex;
+       link->l_mask |= LINK_ATTR_IFINDEX;
+}
+
+/**
+ * Get interface index
+ * @arg link           link handle
+ * @return interface index or RTNL_LINK_NOT_FOUND if not set.
+ */
+int rtnl_link_get_ifindex(struct rtnl_link *link)
+{
+       if (link->l_mask & LINK_ATTR_IFINDEX)
+               return link->l_index;
+       else
+               return RTNL_LINK_NOT_FOUND;
+}
+
+/**
+ * Set Maximum Transmission Unit
+ * @arg link           link to change
+ * @arg mtu            new MTU
+ */
+void rtnl_link_set_mtu(struct rtnl_link *link, unsigned int mtu)
+{
+       link->l_mtu = mtu;
+       link->l_mask |= LINK_ATTR_MTU;
+}
+
+/**
+ * Get Maximum Transmission Unit
+ * @arg link           link handle
+ * @return Link MTU or 0 if MTU is not set.
+ */
+unsigned int rtnl_link_get_mtu(struct rtnl_link *link)
+{
+       if (link->l_mask & LINK_ATTR_MTU)
+               return link->l_mtu;
+       else
+               return 0;
+}
+
+/**
+ * Set Transmission Queue Length
+ * @arg link           link to change
+ * @arg txqlen         new TX queue length
+ * @note The unit of the transmission queue length depends on the
+ *       link type, a common unit is \a packets.
+ */
+void rtnl_link_set_txqlen(struct rtnl_link *link, unsigned int txqlen)
+{
+       link->l_txqlen = txqlen;
+       link->l_mask |= LINK_ATTR_TXQLEN;
+}
+
+/**
+ * Get Transmission Queue Length
+ * @arg link           link handle
+ * @return Transmission Queue Length or UINT_MAX if not set.
+ */
+unsigned int rtnl_link_get_txqlen(struct rtnl_link *link)
+{
+       if (link->l_mask & LINK_ATTR_TXQLEN)
+               return link->l_txqlen;
+       else
+               return UINT_MAX;
+}
+
+/**
+ * Set Weight
+ * @arg link           link to change
+ * @arg weight         new weight
+ */
+void rtnl_link_set_weight(struct rtnl_link *link, unsigned int weight)
+{
+       link->l_weight = weight;
+       link->l_mask |= LINK_ATTR_WEIGHT;
+}
+
+/**
+ * Get Weight
+ * @arg link           link handle
+ * @return Link weight or UINT_MAX if not set.
+ */
+unsigned int rtnl_link_get_weight(struct rtnl_link *link)
+{
+       if (link->l_mask & LINK_ATTR_WEIGHT)
+               return link->l_weight;
+       else
+               return UINT_MAX;
+}
+
+/**
+ * Set parent interface index
+ * @arg link           link to change
+ * @arg ifindex                new parent's interface index
+ */
+void rtnl_link_set_link(struct rtnl_link *link, int ifindex)
+{
+       link->l_link = ifindex;
+       link->l_mask |= LINK_ATTR_LINK;
+}
+
+/**
+ * Get parent interface index
+ * @arg link           link handle
+ * @return Parent interface index or RTNL_LINK_NOT_FOUND if not set
+ */
+int rtnl_link_get_link(struct rtnl_link *link)
+{
+       if (link->l_mask & LINK_ATTR_LINK)
+               return link->l_link;
+       else
+               return RTNL_LINK_NOT_FOUND;
+}
+
+/**
+ * Set master interface index
+ * @arg link           link to change
+ * @arg ifindex                new master's interface index
+ */
+void rtnl_link_set_master(struct rtnl_link *link, int ifindex)
+{
+       link->l_master = ifindex;
+       link->l_mask |= LINK_ATTR_MASTER;
+}
+
+/**
+ * Get master interface index
+ * @arg link           link handle
+ * @return Interface index of master or RTNL_LINK_NOT_FOUND if not set
+ */
+int rtnl_link_get_master(struct rtnl_link *link)
+{
+       if (link->l_mask & LINK_ATTR_MASTER)
+               return link->l_master;
+       else
+               return RTNL_LINK_NOT_FOUND;
+}
+
+/**
+ * Get the statistic specified by the id
+ * @arg link           link handle
+ * @arg id             statistic id
+ * @return The current counter of the specified statistic
+ */
+uint64_t rtnl_link_get_stat(struct rtnl_link *link, int id)
+{
+       if (id < 0 || id > RTNL_LINK_STATS_MAX)
+               return 0;
+
+       return link->l_stats[id];
+}
+
+/** @} */
+
+static struct nl_cache_ops rtnl_link_ops = {
+       .co_name                = "route/link",
+       .co_size                = sizeof(struct rtnl_link),
+       .co_hdrsize             = sizeof(struct ifinfomsg),
+       .co_msgtypes            = {
+                                       { RTM_NEWLINK, "new" },
+                                       { RTM_DELLINK, "delete" },
+                                       { RTM_GETLINK, "get" },
+                                       { -1, NULL },
+                                 },
+       .co_protocol            = NETLINK_ROUTE,
+       .co_request_update      = link_request_update,
+       .co_msg_parser          = link_msg_parser,
+       .co_free_data           = link_free_data,
+       .co_dump[NL_DUMP_BRIEF] = link_dump_brief,
+       .co_dump[NL_DUMP_FULL]  = link_dump_full,
+       .co_dump[NL_DUMP_STATS] = link_dump_stats,
+       .co_dump[NL_DUMP_XML]   = link_dump_xml,
+       .co_dump[NL_DUMP_ENV]   = link_dump_env,
+       .co_filter              = link_filter,
+};
+
+static void __init link_init(void)
+{
+       nl_cache_mngt_register(&rtnl_link_ops);
+}
+
+static void __exit link_exit(void)
+{
+       nl_cache_mngt_unregister(&rtnl_link_ops);
+}
+
+/** @} */
diff --git a/lib/route/neigh.c b/lib/route/neigh.c
new file mode 100644 (file)
index 0000000..e8e0e40
--- /dev/null
@@ -0,0 +1,1115 @@
+/*
+ * lib/route/neigh.c   Neighbours
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup rtnl
+ * @defgroup neigh Neighbours
+ * @brief
+ *
+ * The neighbour table establishes bindings between protocol addresses and
+ * link layer addresses for hosts sharing the same physical link. This
+ * module allows you to access and manipulate the content of these tables.
+ *
+ * @par Neighbour States
+ * @code
+ * NUD_INCOMPLETE
+ * NUD_REACHABLE
+ * NUD_STALE
+ * NUD_DELAY
+ * NUD_PROBE
+ * NUD_FAILED
+ * NUD_NOARP
+ * NUD_PERMANENT
+ * @endcode
+ *
+ * @par Neighbour Flags
+ * @code
+ * NTF_PROXY
+ * NTF_ROUTER
+ * @endcode
+ *
+ * @par Neighbour Identification
+ * A neighbour is uniquely identified by the attributes listed below, whenever
+ * you refer to an existing neighbour all of the attributes must be set.
+ * Neighbours from caches automatically have all required attributes set.
+ *   - interface index (rtnl_neigh_set_ifindex())
+ *   - destination address (rtnl_neigh_set_dst())
+ *
+ * @par Changeable Attributes
+ * \anchor neigh_changeable
+ *  - state (rtnl_neigh_set_state())
+ *  - link layer address (rtnl_neigh_set_lladdr())
+ *
+ * @par Required Caches for Dumping
+ * In order to dump neighbour attributes you must provide the following
+ * caches via nl_cache_provide()
+ *  - link cache holding all links
+ *
+ * @par TODO
+ *   - Document proxy settings
+ *   - Document states and their influence
+ *
+ * @par 1) Retrieving information about configured neighbours
+ * @code
+ * // The first step is to retrieve a list of all available neighbour within
+ * // the kernel and put them into a cache.
+ * struct nl_cache *cache = rtnl_neigh_alloc_cache(handle);
+ *
+ * // Neighbours can then be looked up by the interface and destination
+ * // address:
+ * struct rtnl_neigh *neigh = rtnl_neigh_get(cache, ifindex, dst_addr);
+ * 
+ * // After successful usage, the object must be given back to the cache
+ * rtnl_neigh_put(neigh);
+ * @endcode
+ *
+ * @par 2) Adding new neighbours
+ * @code
+ * // Allocate an empty neighbour handle to be filled out with the attributes
+ * // of the new neighbour.
+ * struct rtnl_neigh *neigh = rtnl_neigh_alloc();
+ *
+ * // Fill out the attributes of the new neighbour
+ * rtnl_neigh_set_ifindex(neigh, ifindex);
+ * rtnl_neigh_set_dst(neigh, dst_addr);
+ * rtnl_neigh_set_state(neigh, rtnl_neigh_str2state("permanent"));
+ *
+ * // Build the netlink message and send it to the kernel, the operation will
+ * // block until the operation has been completed. Alternatively the required
+ * // netlink message can be built using rtnl_neigh_build_add_request()
+ * // to be sent out using nl_send_auto_complete().
+ * rtnl_neigh_add(nl_handle, neigh, NLM_F_REPLACE);
+ *
+ * // Free the memory
+ * rtnl_neigh_put(neigh);
+ * @endcode
+ *
+ * @par 3) Deleting an existing neighbour
+ * @code
+ * // Allocate an empty neighbour object to be filled out with the attributes
+ * // matching the neighbour to be deleted. Alternatively a fully equipped
+ * // neighbour object out of a cache can be used instead.
+ * struct rtnl_neigh *neigh = rtnl_neigh_alloc();
+ *
+ * // Neighbours are uniquely identified by their interface index and
+ * // destination address, you may fill out other attributes but they
+ * // will have no influence.
+ * rtnl_neigh_set_ifindex(neigh, ifindex);
+ * rtnl_neigh_set_dst(neigh, dst_addr);
+ *
+ * // Build the netlink message and send it to the kernel, the operation will
+ * // block until the operation has been completed. Alternatively the required
+ * // netlink message can be built using rtnl_neigh_build_delete_request()
+ * // to be sent out using nl_send_auto_complete().
+ * rtnl_neigh_delete(handle, neigh, 0);
+ *
+ * // Free the memory
+ * rtnl_neigh_put(neigh);
+ * @endcode
+ *
+ * @par 4) Changing neighbour attributes
+ * @code
+ * // Allocate an empty neighbour object to be filled out with the attributes
+ * // matching the neighbour to be changed and the new parameters. Alternatively
+ * // a fully equipped modified neighbour object out of a cache can be used.
+ * struct rtnl_neigh *neigh = rtnl_neigh_alloc();
+ *
+ * // Identify the neighbour to be changed by its interface index and
+ * // destination address
+ * rtnl_neigh_set_ifindex(neigh, ifindex);
+ * rtnl_neigh_set_dst(neigh, dst_addr);
+ *
+ * // The link layer address may be modified, if so it is wise to change
+ * // its state to "permanent" in order to avoid having it overwritten.
+ * rtnl_neigh_set_lladdr(neigh, lladdr);
+ *
+ * // Secondly the state can be modified allowing normal neighbours to be
+ * // converted into permanent entries or to manually confirm a neighbour.
+ * rtnl_neigh_set_state(neigh, state);
+ *
+ * // Build the netlink message and send it to the kernel, the operation will
+ * // block until the operation has been completed. Alternatively the required
+ * // netlink message can be built using rtnl_neigh_build_change_request()
+ * // to be sent out using nl_send_auto_complete().
+ * rtnl_neigh_change(handle, neigh, 0);
+ *
+ * // Free the memory
+ * rtnl_neigh_put(neigh);
+ * @endcode
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink/netlink.h>
+#include <netlink/utils.h>
+#include <netlink/route/rtnl.h>
+#include <netlink/route/neighbour.h>
+#include <netlink/route/link.h>
+
+/** @cond SKIP */
+#define NEIGH_ATTR_FLAGS        0x01
+#define NEIGH_ATTR_STATE        0x02
+#define NEIGH_ATTR_LLADDR       0x04
+#define NEIGH_ATTR_DST          0x08
+#define NEIGH_ATTR_CACHEINFO    0x10
+#define NEIGH_ATTR_IFINDEX      0x20
+#define NEIGH_ATTR_FAMILY       0x40
+#define NEIGH_ATTR_TYPE         0x80
+#define NEIGH_ATTR_PROBES       0x100
+
+static struct nl_cache_ops rtnl_neigh_ops;
+/** @endcond */
+
+static void neigh_free_data(struct nl_object *c)
+{
+       struct rtnl_neigh *neigh = nl_object_priv(c);
+
+       if (!neigh)
+               return;
+
+       nl_addr_put(neigh->n_lladdr);
+       nl_addr_put(neigh->n_dst);
+}
+
+static int neigh_filter(struct nl_object *obj, struct nl_object *filter)
+{
+       struct rtnl_neigh *o = (struct rtnl_neigh *) obj;
+       struct rtnl_neigh *f = (struct rtnl_neigh *) filter;
+
+#define REQ(F) (f->n_mask & NEIGH_ATTR_##F)
+#define AVAIL(F) (o->n_mask & NEIGH_ATTR_##F)
+#define _O(F, EXPR) (REQ(F) && (!AVAIL(F) || (EXPR)))
+#define _C(F, N) (REQ(F) && (!AVAIL(F) || (o->N != f->N)))
+       if (_C(IFINDEX, n_ifindex)                                      ||
+           _C(FAMILY,  n_family)                                       ||
+           _C(TYPE,    n_type)                                         ||
+           _O(LLADDR,  nl_addr_cmp(o->n_lladdr, f->n_lladdr))          ||
+           _O(DST,     nl_addr_cmp(o->n_dst, f->n_dst))                ||
+           _O(STATE,   f->n_state ^ (o->n_state & f->n_state_mask))    ||
+           _O(FLAGS,   f->n_flags ^ (o->n_flags & f->n_flag_mask)))
+               return 0;
+#undef REQ
+#undef AVAIL
+#undef _O
+#undef _C
+
+       return 1;
+}
+
+static struct nla_policy neigh_policy[NDA_MAX+1] = {
+       [NDA_CACHEINFO] = { .minlen = sizeof(struct nda_cacheinfo) },
+       [NDA_PROBES]    = { .type = NLA_U32 },
+};
+
+static int neigh_msg_parser(struct sockaddr_nl *who, struct nlmsghdr *n,
+                           void *arg)
+{
+       struct rtnl_neigh *neigh;
+       struct nlattr *tb[NDA_MAX + 1];
+       struct nl_parser_param *pp = arg;
+       struct ndmsg *nm;
+       int err;
+
+       neigh = rtnl_neigh_alloc();
+       if (!neigh) {
+               err = nl_errno(ENOMEM);
+               goto errout;
+       }
+
+       neigh->ce_msgtype = n->nlmsg_type;
+       nm = nlmsg_data(n);
+
+       err = nlmsg_parse(n, sizeof(*nm), tb, NDA_MAX, neigh_policy);
+       if (err < 0)
+               goto errout;
+
+       neigh->n_family  = nm->ndm_family;
+       neigh->n_ifindex = nm->ndm_ifindex;
+       neigh->n_state   = nm->ndm_state;
+       neigh->n_flags   = nm->ndm_flags;
+       neigh->n_type    = nm->ndm_type;
+
+       neigh->n_mask |= (NEIGH_ATTR_FAMILY | NEIGH_ATTR_IFINDEX |
+                         NEIGH_ATTR_STATE | NEIGH_ATTR_FLAGS |
+                         NEIGH_ATTR_TYPE);
+
+       if (tb[NDA_LLADDR]) {
+               neigh->n_lladdr = nla_get_addr(tb[NDA_LLADDR], AF_UNSPEC);
+               if (!neigh->n_lladdr)
+                       goto errout;
+               nl_addr_set_family(neigh->n_lladdr,
+                                  nl_addr_guess_family(neigh->n_lladdr));
+               neigh->n_mask |= NEIGH_ATTR_LLADDR;
+       }
+
+       if (tb[NDA_DST]) {
+               neigh->n_dst = nla_get_addr(tb[NDA_DST], neigh->n_family);
+               if (!neigh->n_dst)
+                       goto errout;
+               neigh->n_mask |= NEIGH_ATTR_DST;
+       }
+
+       if (tb[NDA_CACHEINFO]) {
+               struct nda_cacheinfo *ci = nla_data(tb[NDA_CACHEINFO]);
+
+               neigh->n_cacheinfo.nci_confirmed = ci->ndm_confirmed;
+               neigh->n_cacheinfo.nci_used = ci->ndm_used;
+               neigh->n_cacheinfo.nci_updated = ci->ndm_updated;
+               neigh->n_cacheinfo.nci_refcnt = ci->ndm_refcnt;
+               
+               neigh->n_mask |= NEIGH_ATTR_CACHEINFO;
+       }
+
+       if (tb[NDA_PROBES]) {
+               neigh->n_probes = nla_get_u32(tb[NDA_PROBES]);
+               neigh->n_mask |= NEIGH_ATTR_PROBES;
+       }
+
+       err = pp->pp_cb((struct nl_object *) neigh, pp);
+       if (err < 0)
+               goto errout;
+
+       return P_ACCEPT;
+
+errout:
+       rtnl_neigh_put(neigh);
+       return err;
+}
+
+static int neigh_request_update(struct nl_cache *c, struct nl_handle *h)
+{
+       return nl_rtgen_request(h, RTM_GETNEIGH, AF_UNSPEC, NLM_F_DUMP);
+}
+
+
+static int neigh_dump_brief(struct nl_object *a, struct nl_dump_params *p)
+{
+       char dst[INET6_ADDRSTRLEN+5], lladdr[INET6_ADDRSTRLEN+5];
+       struct rtnl_neigh *n = (struct rtnl_neigh *) a;
+       struct nl_cache *link_cache;
+       char state[128], flags[64];
+
+       link_cache = nl_cache_mngt_require("route/link");
+
+       dp_dump(p, "%s ", nl_addr2str(n->n_dst, dst, sizeof(dst)));
+
+       if (link_cache)
+               dp_dump(p, "dev %s ",
+                       rtnl_link_i2name(link_cache, n->n_ifindex,
+                                        state, sizeof(state)));
+       else
+               dp_dump(p, "dev %d ", n->n_ifindex);
+
+       if (n->n_mask & NEIGH_ATTR_LLADDR)
+               dp_dump(p, "lladdr %s ",
+                       nl_addr2str(n->n_lladdr, lladdr, sizeof(lladdr)));
+
+       rtnl_neigh_state2str(n->n_state, state, sizeof(state));
+       rtnl_neigh_flags2str(n->n_flags, flags, sizeof(flags));
+
+       if (state[0])
+               dp_dump(p, "<%s", state);
+       if (flags[0])
+               dp_dump(p, "%s%s", state[0] ? "," : "<", flags);
+       if (state[0] || flags[0])
+               dp_dump(p, ">");
+       dp_dump(p, "\n");
+
+       return 1;
+}
+
+static int neigh_dump_full(struct nl_object *a, struct nl_dump_params *p)
+{
+       char rtn_type[32];
+       struct rtnl_neigh *n = (struct rtnl_neigh *) a;
+       int hz = nl_get_hz();
+
+       int line = neigh_dump_brief(a, p);
+
+       dp_dump_line(p, line++, "    refcnt %u type %s confirmed %u used "
+                               "%u updated %u\n",
+               n->n_cacheinfo.nci_refcnt,
+               nl_rtntype2str(n->n_type, rtn_type, sizeof(rtn_type)),
+               n->n_cacheinfo.nci_confirmed/hz,
+               n->n_cacheinfo.nci_used/hz, n->n_cacheinfo.nci_updated/hz);
+
+       return line;
+}
+
+static int neigh_dump_stats(struct nl_object *a, struct nl_dump_params *p)
+{
+       return neigh_dump_full(a, p);
+}
+
+static int neigh_dump_xml(struct nl_object *obj, struct nl_dump_params *p)
+{
+       struct rtnl_neigh *neigh = (struct rtnl_neigh *) obj;
+       char buf[128];
+       int line = 0;
+
+       dp_dump_line(p, line++, "<neighbour>\n");
+       dp_dump_line(p, line++, "  <family>%s</family>\n",
+                    nl_af2str(neigh->n_family, buf, sizeof(buf)));
+
+       if (neigh->n_mask & NEIGH_ATTR_LLADDR)
+               dp_dump_line(p, line++, "  <lladdr>%s</lladdr>\n",
+                            nl_addr2str(neigh->n_lladdr, buf, sizeof(buf)));
+
+       if (neigh->n_mask & NEIGH_ATTR_DST)
+               dp_dump_line(p, line++, "  <dst>%s</dst>\n",
+                            nl_addr2str(neigh->n_dst, buf, sizeof(buf)));
+
+       if (neigh->n_mask & NEIGH_ATTR_IFINDEX) {
+               struct nl_cache *link_cache;
+       
+               link_cache = nl_cache_mngt_require("route/link");
+
+               if (link_cache)
+                       dp_dump_line(p, line++, "  <device>%s</device>\n",
+                                    rtnl_link_i2name(link_cache,
+                                                     neigh->n_ifindex,
+                                                     buf, sizeof(buf)));
+               else
+                       dp_dump_line(p, line++, "  <device>%u</device>\n",
+                                    neigh->n_ifindex);
+       }
+
+       if (neigh->n_mask & NEIGH_ATTR_PROBES)
+               dp_dump_line(p, line++, "  <probes>%u</probes>\n",
+                            neigh->n_probes);
+
+       if (neigh->n_mask & NEIGH_ATTR_TYPE)
+               dp_dump_line(p, line++, "  <type>%s</type>\n",
+                            nl_rtntype2str(neigh->n_type, buf, sizeof(buf)));
+
+       rtnl_neigh_flags2str(neigh->n_flags, buf, sizeof(buf));
+       if (buf[0])
+               dp_dump_line(p, line++, "  <flags>%s</flags>\n", buf);
+
+       rtnl_neigh_state2str(neigh->n_state, buf, sizeof(buf));
+       if (buf[0])
+               dp_dump_line(p, line++, "  <state>%s</state>\n", buf);
+
+       dp_dump_line(p, line++, "</neighbour>\n");
+
+#if 0
+       struct rtnl_ncacheinfo n_cacheinfo;
+#endif
+
+       return line;
+}
+
+static int neigh_dump_env(struct nl_object *obj, struct nl_dump_params *p)
+{
+       struct rtnl_neigh *neigh = (struct rtnl_neigh *) obj;
+       char buf[128];
+       int line = 0;
+
+       dp_dump_line(p, line++, "NEIGH_FAMILY=%s\n",
+                    nl_af2str(neigh->n_family, buf, sizeof(buf)));
+
+       if (neigh->n_mask & NEIGH_ATTR_LLADDR)
+               dp_dump_line(p, line++, "NEIGHT_LLADDR=%s\n",
+                            nl_addr2str(neigh->n_lladdr, buf, sizeof(buf)));
+
+       if (neigh->n_mask & NEIGH_ATTR_DST)
+               dp_dump_line(p, line++, "NEIGH_DST=%s\n",
+                            nl_addr2str(neigh->n_dst, buf, sizeof(buf)));
+
+       if (neigh->n_mask & NEIGH_ATTR_IFINDEX) {
+               struct nl_cache *link_cache;
+
+               dp_dump_line(p, line++, "NEIGH_IFINDEX=%u\n",
+                            neigh->n_ifindex);
+
+               link_cache = nl_cache_mngt_require("route/link");
+               if (link_cache)
+                       dp_dump_line(p, line++, "NEIGH_IFNAME=%s\n",
+                                    rtnl_link_i2name(link_cache,
+                                                     neigh->n_ifindex,
+                                                     buf, sizeof(buf)));
+       }
+
+       if (neigh->n_mask & NEIGH_ATTR_PROBES)
+               dp_dump_line(p, line++, "NEIGH_PROBES=%u\n",
+                            neigh->n_probes);
+
+       if (neigh->n_mask & NEIGH_ATTR_TYPE)
+               dp_dump_line(p, line++, "NEIGH_TYPE=%s\n",
+                            nl_rtntype2str(neigh->n_type, buf, sizeof(buf)));
+
+       rtnl_neigh_flags2str(neigh->n_flags, buf, sizeof(buf));
+       if (buf[0])
+               dp_dump_line(p, line++, "NEIGH_FLAGS=%s\n", buf);
+
+       rtnl_neigh_state2str(neigh->n_state, buf, sizeof(buf));
+       if (buf[0])
+               dp_dump_line(p, line++, "NEIGH_STATE=%s\n", buf);
+
+       return line;
+}
+
+/**
+ * @name Neighbour Object Allocation/Freeage
+ * @{
+ */
+
+/**
+ * Allocate a new neighbour object
+ * @return New neighbour object
+ */
+struct rtnl_neigh *rtnl_neigh_alloc(void)
+{
+       return (struct rtnl_neigh *) nl_object_alloc_from_ops(&rtnl_neigh_ops);
+}
+
+/**
+ * Give back reference on neighbour object.
+ * @arg neigh          Neighbour  object to be given back.
+ *
+ * Decrements the reference counter and frees the object if the
+ * last reference has been released.
+ */
+void rtnl_neigh_put(struct rtnl_neigh *neigh)
+{
+       nl_object_put((struct nl_object *) neigh);
+}
+/**
+ * Free neighbour object.
+ * @arg neigh          Neighbour object to be freed.
+ *
+ * @note Always use rtnl_neigh_put() unless you're absolutely sure
+ *       that no other user may have a reference on this object.
+ */
+void rtnl_neigh_free(struct rtnl_neigh *neigh)
+{
+       nl_object_free((struct nl_object *) neigh);
+}
+
+/** @} */
+
+/**
+ * @name Neighbour Cache Managament
+ * @{
+ */
+
+/**
+ * Build a neighbour cache including all neighbours currently configured in the kernel.
+ * @arg handle         netlink handle
+ *
+ * Allocates a new neighbour cache, initializes it properly and updates it
+ * to include all neighbours currently configured in the kernel.
+ *
+ * @note The caller is responsible for destroying and freeing the
+ *       cache after using it.
+ * @return The new cache or NULL if an error occured.
+ */
+struct nl_cache *rtnl_neigh_alloc_cache(struct nl_handle *handle)
+{
+       struct nl_cache *cache = nl_cache_alloc_from_ops(&rtnl_neigh_ops);
+
+       if (cache == NULL)
+               return NULL;
+
+       if (nl_cache_update(handle, cache) < 0) {
+               nl_cache_free(cache);
+               return NULL;
+       }
+
+       NL_DBG(2, "Returning new cache %p\n", cache);
+
+       return cache;
+}
+
+/**
+ * Look up a neighbour by interface index and destination address
+ * @arg cache          neighbour cache
+ * @arg ifindex                interface index the neighbour is on
+ * @arg dst            destination address of the neighbour
+ * @return neighbour handle or NULL if no match was found.
+ */
+struct rtnl_neigh * rtnl_neigh_get(struct nl_cache *cache, int ifindex,
+                                  struct nl_addr *dst)
+{
+       struct rtnl_neigh *neigh;
+
+       nl_list_for_each_entry(neigh, &cache->c_items, ce_list) {
+               if (neigh->n_ifindex == ifindex &&
+                   !nl_addr_cmp(neigh->n_dst, dst)) {
+                       nl_object_get((struct nl_object *) neigh);
+                       return neigh;
+               }
+       }
+
+       return NULL;
+}
+
+/** @} */
+
+/**
+ * @name Neighbour Addition
+ * @{
+ */
+
+static struct nl_msg * build_neigh_msg(struct rtnl_neigh *tmpl, int cmd,
+                                      int flags)
+{
+       struct nl_msg *msg;
+       struct ndmsg nhdr = {
+               .ndm_ifindex = tmpl->n_ifindex,
+               .ndm_family = nl_addr_get_family(tmpl->n_dst),
+               .ndm_state = NUD_PERMANENT,
+       };
+
+       if (tmpl->n_mask & NEIGH_ATTR_STATE)
+               nhdr.ndm_state = tmpl->n_state;
+
+       msg = nlmsg_build_simple(cmd, flags);
+       if (!msg)
+               return NULL;
+
+       if (nlmsg_append(msg, &nhdr, sizeof(nhdr), 1) < 0)
+               goto nla_put_failure;
+
+       NLA_PUT_ADDR(msg, NDA_DST, tmpl->n_dst);
+
+       if (tmpl->n_mask & NEIGH_ATTR_LLADDR)
+               NLA_PUT_ADDR(msg, NDA_LLADDR, tmpl->n_lladdr);
+
+       return msg;
+
+nla_put_failure:
+       nlmsg_free(msg);
+       return NULL;
+}
+
+/**
+ * Build netlink request message to add a new neighbour
+ * @arg tmpl           template with data of new neighbour
+ * @arg flags          additional netlink message flags
+ *
+ * Builds a new netlink message requesting a addition of a new
+ * neighbour. The netlink message header isn't fully equipped with
+ * all relevant fields and must thus be sent out via nl_send_auto_complete()
+ * or supplemented as needed. \a tmpl must contain the attributes of the new
+ * neighbour set via \c rtnl_neigh_set_* functions.
+ * 
+ * The following attributes must be set in the template:
+ *  - Interface index (rtnl_neigh_set_ifindex())
+ *  - State (rtnl_neigh_set_state())
+ *  - Destination address (rtnl_neigh_set_dst())
+ *  - Link layer address (rtnl_neigh_set_lladdr())
+ *
+ * @return The netlink message
+ */
+struct nl_msg * rtnl_neigh_build_add_request(struct rtnl_neigh *tmpl, int flags)
+{
+       return build_neigh_msg(tmpl, RTM_NEWNEIGH, NLM_F_CREATE | flags);
+}
+
+/**
+ * Add a new neighbour
+ * @arg handle         netlink handle
+ * @arg tmpl           template with requested changes
+ * @arg flags          additional netlink message flags
+ *
+ * Builds a netlink message by calling rtnl_neigh_build_add_request(),
+ * sends the request to the kernel and waits for the next ACK to be
+ * received and thus blocks until the request has been fullfilled.
+ *
+ * The following attributes must be set in the template:
+ *  - Interface index (rtnl_neigh_set_ifindex())
+ *  - State (rtnl_neigh_set_state())
+ *  - Destination address (rtnl_neigh_set_dst())
+ *  - Link layer address (rtnl_neigh_set_lladdr())
+ *
+ * @return 0 on sucess or a negative error if an error occured.
+ */
+int rtnl_neigh_add(struct nl_handle *handle, struct rtnl_neigh *tmpl, int flags)
+{
+       int err;
+       struct nl_msg *msg;
+       
+       msg = rtnl_neigh_build_add_request(tmpl, flags);
+       if (!msg)
+               return nl_errno(ENOMEM);
+
+       err = nl_send_auto_complete(handle, msg);
+       if (err < 0)
+               return err;
+
+       nlmsg_free(msg);
+       return nl_wait_for_ack(handle);
+}
+
+/** @} */
+
+/**
+ * @name Neighbour Deletion
+ * @{
+ */
+
+/**
+ * Build a netlink request message to delete a neighbour
+ * @arg neigh          neighbour to delete
+ * @arg flags          additional netlink message flags
+ *
+ * Builds a new netlink message requesting a deletion of a neighbour.
+ * The netlink message header isn't fully equipped with all relevant
+ * fields and must thus be sent out via nl_send_auto_complete()
+ * or supplemented as needed. \a neigh must point to an existing
+ * neighbour.
+ *
+ * @return The netlink message
+ */
+struct nl_msg *rtnl_neigh_build_delete_request(struct rtnl_neigh *neigh,
+                                              int flags)
+{
+       return build_neigh_msg(neigh, RTM_DELNEIGH, flags);
+}
+
+/**
+ * Delete a neighbour
+ * @arg handle         netlink handle
+ * @arg neigh          neighbour to delete
+ * @arg flags          additional netlink message flags
+ *
+ * Builds a netlink message by calling rtnl_neigh_build_delete_request(),
+ * sends the request to the kernel and waits for the next ACK to be
+ * received and thus blocks until the request has been fullfilled.
+ *
+ * @return 0 on sucess or a negative error if an error occured.
+ */
+int rtnl_neigh_delete(struct nl_handle *handle, struct rtnl_neigh *neigh,
+                     int flags)
+{
+       int err;
+       struct nl_msg *msg;
+       
+       msg = rtnl_neigh_build_delete_request(neigh, flags);
+       if (!msg)
+               return nl_errno(ENOMEM);
+
+       err = nl_send_auto_complete(handle, msg);
+       if (err < 0)
+               return err;
+
+       nlmsg_free(msg);
+       return nl_wait_for_ack(handle);
+}
+
+/** @} */
+
+/**
+ * @name Neighbour Modification
+ * @{
+ */
+
+/**
+ * Build a netlink request message to change neighbour attributes
+ * @arg neigh          the neighbour to change
+ * @arg flags          additional netlink message flags
+ *
+ * Builds a new netlink message requesting a change of a neigh
+ * attributes. The netlink message header isn't fully equipped with
+ * all relevant fields and must thus be sent out via nl_send_auto_complete()
+ * or supplemented as needed.
+ *
+ * @return The netlink message
+ * @note Not all attributes can be changed, see
+ *       \ref neigh_changeable "Changeable Attributes" for a list.
+ */
+struct nl_msg *rtnl_neigh_build_change_request(struct rtnl_neigh *neigh,
+                                              int flags)
+{
+       return build_neigh_msg(neigh, RTM_NEWNEIGH, NLM_F_REPLACE | flags);
+}
+
+/**
+ * Change neighbour attributes
+ * @arg handle         netlink handle
+ * @arg neigh          neighbour to be changed
+ * @arg flags          additional netlink message flags
+ *
+ * Builds a netlink message by calling rtnl_neigh_build_change_request(),
+ * sends the request to the kernel and waits for the next ACK to be
+ * received and thus blocks until the request has been fullfilled.
+ *
+ * @return 0 on sucess or a negative error if an error occured.
+ * @note Not all attributes can be changed, see
+ *       \ref neigh_changeable "Changeable Attributes" for a list.
+ */
+int rtnl_neigh_change(struct nl_handle *handle, struct rtnl_neigh *neigh,
+                     int flags)
+{
+       int err;
+       struct nl_msg *msg;
+       
+       msg = rtnl_neigh_build_change_request(neigh, flags);
+       if (!msg)
+               return nl_errno(ENOMEM);
+
+       err = nl_send_auto_complete(handle, msg);
+       if (err < 0)
+               return err;
+
+       nlmsg_free(msg);
+       return nl_wait_for_ack(handle);
+}
+
+/** @} */
+
+/**
+ * @name Neighbour States Translations
+ * @{
+ */
+
+static struct trans_tbl neigh_states[] = {
+       __ADD(NUD_INCOMPLETE, incomplete)
+       __ADD(NUD_REACHABLE, reachable)
+       __ADD(NUD_STALE, stale)
+       __ADD(NUD_DELAY, delay)
+       __ADD(NUD_PROBE, probe)
+       __ADD(NUD_FAILED, failed)
+       __ADD(NUD_NOARP, norarp)
+       __ADD(NUD_PERMANENT, permanent)
+};
+
+/**
+ * Convert neighbour states to a character string (Reentrant).
+ * @arg state          neighbour state
+ * @arg buf            destination buffer
+ * @arg len            buffer length
+ *
+ * Converts a neighbour state to a character string separated by
+ * commands and stores it in the specified destination buffer.
+ *
+ * @return The destination buffer
+ */
+char * rtnl_neigh_state2str(int state, char *buf, size_t len)
+{
+       return __flags2str(state, buf, len, neigh_states,
+           ARRAY_SIZE(neigh_states));
+}
+
+/**
+ * Convert a character string to a neighbour state
+ * @arg name           Name of cscope
+ *
+ * Converts the provided character string specifying a neighbour
+ * state the corresponding numeric value.
+ *
+ * @return Neighbour state or a negative value if none was found.
+ */
+int rtnl_neigh_str2state(const char *name)
+{
+       return __str2type(name, neigh_states, ARRAY_SIZE(neigh_states));
+}
+
+/** @} */
+
+/**
+ * @name Neighbour Flags Translations
+ * @{
+ */
+
+static struct trans_tbl neigh_flags[] = {
+       __ADD(NTF_PROXY, proxy)
+       __ADD(NTF_ROUTER, router)
+};
+
+/**
+ * Convert neighbour flags to a character string (Reentrant).
+ * @arg flags          neighbour flags
+ * @arg buf            destination buffer
+ * @arg len            buffer length
+ *
+ * Converts neighbour flags to a character string separated by
+ * commands and stores it in the specified destination buffer.
+ *
+ * @return The destination buffer or a empty string if no flags are set.
+ */
+char * rtnl_neigh_flags2str(int flags, char *buf, size_t len)
+{
+       return __flags2str(flags, buf, len, neigh_flags,
+           ARRAY_SIZE(neigh_flags));
+}
+
+/**
+ * Convert a character string to a neighbour flag
+ * @arg name           name of the flag
+ *
+ * Converts the provided character string specifying a neighbour
+ * flag the corresponding numeric value.
+ *
+ * @return Neighbour flag or a negative value if none was found.
+ */
+int rtnl_neigh_str2flag(const char *name)
+{
+       return __str2type(name, neigh_flags, ARRAY_SIZE(neigh_flags));
+}
+
+/** @} */
+
+/**
+ * @name Attribute Modification
+ * @{
+ */
+
+/**
+ * Set a neighbour state
+ * @arg neigh          neighbour to change
+ * @arg state          state to set
+ */
+void rtnl_neigh_set_state(struct rtnl_neigh *neigh, int state)
+{
+       neigh->n_state_mask |= state;
+       neigh->n_state |= state;
+       neigh->n_mask |= NEIGH_ATTR_STATE;
+}
+
+/**
+ * Get neighbour states
+ * @arg neigh          neighbour handle
+ * @return Neighbour state or -1 if not set
+ */
+int rtnl_neigh_get_state(struct rtnl_neigh *neigh)
+{
+       if (neigh->n_mask & NEIGH_ATTR_STATE)
+               return neigh->n_state;
+       else
+               return -1;
+}
+
+/**
+ * Unset a neigbour state
+ * @arg neigh          neighbour to change
+ * @arg state          state to unset
+ */
+void rtnl_neigh_unset_state(struct rtnl_neigh *neigh, int state)
+{
+       neigh->n_state_mask |= state;
+       neigh->n_state &= ~state;
+       neigh->n_mask |= NEIGH_ATTR_STATE;
+}
+
+/**
+ * Set neighbour flags
+ * @arg neigh          neighbour to change
+ * @arg flags          flag to set
+ */
+void rtnl_neigh_set_flags(struct rtnl_neigh *neigh, unsigned int flags)
+{
+       neigh->n_flag_mask |= flags;
+       neigh->n_flags |= flags;
+       neigh->n_mask |= NEIGH_ATTR_FLAGS;
+}
+
+/**
+ * Get neighbour flags
+ * @arg neigh          neighbour handle
+ * @return Neighbour flags
+ */
+unsigned int rtnl_neigh_get_flags(struct rtnl_neigh *neigh)
+{
+       return neigh->n_flags;
+}
+
+/**
+ * Unset neighbour flags
+ * @arg neigh          neighbour to change
+ * @arg flags          flag to unset
+ */
+void rtnl_neigh_unset_flags(struct rtnl_neigh *neigh, unsigned int flags)
+{
+       neigh->n_flag_mask |= flags;
+       neigh->n_flags &= ~flags;
+       neigh->n_mask |= NEIGH_ATTR_FLAGS;
+}
+
+/**
+ * Set the interface index of device this neighbour is on
+ * @arg neigh          neighbour to change
+ * @arg ifindex                new interface index
+ */
+void rtnl_neigh_set_ifindex(struct rtnl_neigh *neigh, int ifindex)
+{
+       neigh->n_ifindex = ifindex;
+       neigh->n_mask |= NEIGH_ATTR_IFINDEX;
+}
+
+/**
+ * Get the interface index of the device this neighbour is on
+ * @arg neigh          neighbour handle
+ * @return Interface index or RTNL_LINK_NOT_FOUND if not set
+ */
+int rtnl_neigh_get_ifindex(struct rtnl_neigh *neigh)
+{
+       if (neigh->n_mask & NEIGH_ATTR_IFINDEX)
+               return neigh->n_ifindex;
+       else
+               return RTNL_LINK_NOT_FOUND;
+}
+
+static inline int __assign_addr(struct rtnl_neigh *neigh, struct nl_addr **pos,
+                               struct nl_addr *new, int flag, int nocheck)
+{
+       if (!nocheck) {
+               if (neigh->n_mask & NEIGH_ATTR_FAMILY) {
+                       if (new->a_family != neigh->n_family)
+                               return nl_error(EINVAL,
+                                               "Address family mismatch");
+               } else {
+                       neigh->n_family = new->a_family;
+                       neigh->n_mask |= NEIGH_ATTR_FAMILY;
+               }
+       }
+
+       if (*pos)
+               nl_addr_put(*pos);
+
+       nl_addr_get(new);
+       *pos = new;
+
+       neigh->n_mask |= flag;
+
+       return 0;
+}
+
+/**
+ * Set link layer address of a neighbour
+ * @arg neigh          neighbour to change
+ * @arg addr           new link layer address
+ *
+ * Assigns the new link layer address to the specified neighbour handle.
+ *
+ * @note The prefix length of the address will be ignored.
+ */
+void rtnl_neigh_set_lladdr(struct rtnl_neigh *neigh, struct nl_addr *addr)
+{
+       __assign_addr(neigh, &neigh->n_lladdr, addr, NEIGH_ATTR_LLADDR, 1);
+}
+
+/**
+ * Get link layer address of a neighbour
+ * @arg neigh          neighbour handle
+ * @return Link layer address or NULL if not set
+ */
+struct nl_addr *rtnl_neigh_get_lladdr(struct rtnl_neigh *neigh)
+{
+       if (neigh->n_mask & NEIGH_ATTR_LLADDR)
+               return neigh->n_lladdr;
+       else
+               return NULL;
+}
+
+/**
+ * Set destination address of a neighbour
+ * @arg neigh          neighbour to change
+ * @arg addr           new destination address
+ *
+ * Assigns the new destination address to the specified neighbour handle.
+ * The address is validated against the address family if set already via
+ * rtnl_neigh_set_family(). The assignment fails if the address families
+ * mismatch. In case the address family has not been specified yet, the
+ * address family of this new address is elected to be the requirement.
+ * 
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_neigh_set_dst(struct rtnl_neigh *neigh, struct nl_addr *addr)
+{
+       return __assign_addr(neigh, &neigh->n_dst, addr,
+                            NEIGH_ATTR_DST, 0);
+}
+
+/**
+ * Get the destination address of a neighbour
+ * @arg neigh          neighbour handle
+ * @return Destination address or NULL if not set
+ */
+struct nl_addr *rtnl_neigh_get_dst(struct rtnl_neigh *neigh)
+{
+       if (neigh->n_mask & NEIGH_ATTR_DST)
+               return neigh->n_dst;
+       else
+               return NULL;
+}
+
+/**
+ * Set destination address family
+ * @arg neigh          neighbour to change
+ * @arg family         new destination address family
+ */
+void rtnl_neigh_set_family(struct rtnl_neigh *neigh, int family)
+{
+       neigh->n_family = family;
+       neigh->n_mask |= NEIGH_ATTR_FAMILY;
+}
+
+/**
+ * Set RTN type of a neighbour
+ * @arg neigh          neighbour to change
+ * @arg type           new rtn type
+ */
+void rtnl_neigh_set_type(struct rtnl_neigh *neigh, int type)
+{
+       neigh->n_type = type;
+       neigh->n_mask = NEIGH_ATTR_TYPE;
+}
+
+/**
+ * Get RTN type of a neighbour
+ * @arg neigh          neighbour handle
+ * @return Type or -1 if not set
+ */
+int rtnl_neigh_get_type(struct rtnl_neigh *neigh)
+{
+       if (neigh->n_mask & NEIGH_ATTR_TYPE)
+               return neigh->n_type;
+       else
+               return -1;
+}
+
+/** @} */
+
+static struct nl_cache_ops rtnl_neigh_ops = {
+       .co_name                = "route/neigh",
+       .co_size                = sizeof(struct rtnl_neigh),
+       .co_hdrsize             = sizeof(struct ndmsg),
+       .co_msgtypes            = {
+                                       { RTM_NEWNEIGH, "new" },
+                                       { RTM_DELNEIGH, "delete" },
+                                       { RTM_GETNEIGH, "get" },
+                                       { -1, NULL },
+                                 },
+       .co_protocol            = NETLINK_ROUTE,
+       .co_request_update      = neigh_request_update,
+       .co_msg_parser          = neigh_msg_parser,
+       .co_free_data           = neigh_free_data,
+       .co_dump[NL_DUMP_BRIEF] = neigh_dump_brief,
+       .co_dump[NL_DUMP_FULL]  = neigh_dump_full,
+       .co_dump[NL_DUMP_STATS] = neigh_dump_stats,
+       .co_dump[NL_DUMP_XML]   = neigh_dump_xml,
+       .co_dump[NL_DUMP_ENV]   = neigh_dump_env,
+       .co_filter              = neigh_filter,
+};
+
+static void __init neigh_init(void)
+{
+       nl_cache_mngt_register(&rtnl_neigh_ops);
+}
+
+static void __exit neigh_exit(void)
+{
+       nl_cache_mngt_unregister(&rtnl_neigh_ops);
+}
+
+/** @} */
diff --git a/lib/route/neightbl.c b/lib/route/neightbl.c
new file mode 100644 (file)
index 0000000..4d7b914
--- /dev/null
@@ -0,0 +1,876 @@
+/*
+ * lib/route/neightbl.c         neighbour tables
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup rtnl
+ * @defgroup neightbl Neighbour Tables
+ * @brief
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink/netlink.h>
+#include <netlink/utils.h>
+#include <netlink/route/rtnl.h>
+#include <netlink/route/neightbl.h>
+#include <netlink/route/link.h>
+
+/** @cond SKIP */
+#define NEIGHTBL_ATTR_FAMILY       0x001
+#define NEIGHTBL_ATTR_STATS        0x002
+#define NEIGHTBL_ATTR_NAME       0x004
+#define NEIGHTBL_ATTR_THRESH1    0x008
+#define NEIGHTBL_ATTR_THRESH2    0x010
+#define NEIGHTBL_ATTR_THRESH3    0x020
+#define NEIGHTBL_ATTR_CONFIG     0x040
+#define NEIGHTBL_ATTR_PARMS      0x080
+#define NEIGHTBL_ATTR_GC_INTERVAL  0x100
+
+#define NEIGHTBLPARM_ATTR_IFINDEX      0x0001
+#define NEIGHTBLPARM_ATTR_REFCNT               0x0002
+#define NEIGHTBLPARM_ATTR_QUEUE_LEN    0x0004
+#define NEIGHTBLPARM_ATTR_APP_PROBES   0x0008
+#define NEIGHTBLPARM_ATTR_UCAST_PROBES 0x0010
+#define NEIGHTBLPARM_ATTR_MCAST_PROBES 0x0020
+#define NEIGHTBLPARM_ATTR_PROXY_QLEN   0x0040
+#define NEIGHTBLPARM_ATTR_REACHABLE_TIME       0x0080
+#define NEIGHTBLPARM_ATTR_BASE_REACHABLE_TIME 0x0100
+#define NEIGHTBLPARM_ATTR_RETRANS_TIME 0x0200
+#define NEIGHTBLPARM_ATTR_GC_STALETIME 0x0400
+#define NEIGHTBLPARM_ATTR_DELAY_PROBE_TIME 0x0800
+#define NEIGHTBLPARM_ATTR_ANYCAST_DELAY        0x1000
+#define NEIGHTBLPARM_ATTR_PROXY_DELAY  0x2000
+#define NEIGHTBLPARM_ATTR_LOCKTIME     0x4000
+
+static struct nl_cache_ops rtnl_neightbl_ops;
+/** @endcond */
+
+static int neightbl_filter(struct nl_object *obj, struct nl_object *filter)
+{
+       struct rtnl_neightbl *o = (struct rtnl_neightbl *) obj;
+       struct rtnl_neightbl *f = (struct rtnl_neightbl *) filter;
+       struct rtnl_neightbl_parms *op = &o->nt_parms;
+       struct rtnl_neightbl_parms *fp = &f->nt_parms;
+
+#define REQ(F) (f->nt_mask & NEIGHTBL_ATTR_##F)
+#define AVAIL(F) (o->nt_mask & NEIGHTBL_ATTR_##F)
+#define _O(F, EXPR) (REQ(F) && (!AVAIL(F) || (EXPR)))
+#define _C(F, N) (REQ(F) && (!AVAIL(F) || (o->N != f->N)))
+       
+       if (_C(FAMILY,          nt_family)                              ||
+           _O(NAME,            strcmp(o->nt_name, f->nt_name))         ||
+           _C(THRESH1,         nt_gc_thresh1)                          ||
+           _C(THRESH2,         nt_gc_thresh2)                          ||
+           _C(THRESH3,         nt_gc_thresh3)                          ||
+           _C(GC_INTERVAL,     nt_gc_interval))
+               return 0;
+#undef REQ
+#undef AVAIL
+#undef _O
+#undef _C
+
+       if (!(f->nt_mask & NEIGHTBL_ATTR_PARMS) &&
+           !(o->nt_mask & NEIGHTBL_ATTR_PARMS))
+               return 1;
+
+#define REQ(F) (fp->ntp_mask & NEIGHTBLPARM_ATTR_##F)
+#define AVAIL(F) (op->ntp_mask & NEIGHTBLPARM_ATTR_##F)
+#define _C(F, N) (REQ(F) && (!AVAIL(F) || (op->N != fp->N)))
+       if (_C(IFINDEX,                 ntp_ifindex)                    ||
+           _C(QUEUE_LEN,               ntp_queue_len)                  ||
+           _C(APP_PROBES,              ntp_app_probes)                 ||
+           _C(UCAST_PROBES,            ntp_ucast_probes)               ||
+           _C(MCAST_PROBES,            ntp_mcast_probes)               ||
+           _C(PROXY_QLEN,              ntp_proxy_qlen)                 ||
+           _C(LOCKTIME,                ntp_locktime)                   ||
+           _C(RETRANS_TIME,            ntp_retrans_time)               ||
+           _C(BASE_REACHABLE_TIME,     ntp_base_reachable_time)        ||
+           _C(GC_STALETIME,            ntp_gc_stale_time)              ||
+           _C(DELAY_PROBE_TIME,        ntp_probe_delay)                ||
+           _C(ANYCAST_DELAY,           ntp_anycast_delay)              ||
+           _C(PROXY_DELAY,             ntp_proxy_delay))
+               return 0;
+#undef REQ
+#undef AVAIL
+#undef _C
+
+       return 1;
+}
+
+static struct nla_policy neightbl_policy[NDTA_MAX+1] = {
+       [NDTA_NAME]             = { .type = NLA_STRING,
+                                   .maxlen = NTBLNAMSIZ },
+       [NDTA_THRESH1]          = { .type = NLA_U32 },
+       [NDTA_THRESH2]          = { .type = NLA_U32 },
+       [NDTA_THRESH3]          = { .type = NLA_U32 },
+       [NDTA_GC_INTERVAL]      = { .type = NLA_U32 },
+       [NDTA_CONFIG]           = { .minlen = sizeof(struct ndt_config) },
+       [NDTA_STATS]            = { .minlen = sizeof(struct ndt_stats) },
+       [NDTA_PARMS]            = { .type = NLA_NESTED },
+};
+
+static int neightbl_msg_parser(struct sockaddr_nl *who, struct nlmsghdr *n,
+                           void *arg)
+{
+       struct rtnl_neightbl *ntbl;
+       struct nlattr *tb[NDTA_MAX + 1];
+       struct nl_parser_param *pp = arg;
+       struct rtgenmsg *rtmsg;
+       int err;
+
+       ntbl = rtnl_neightbl_alloc();
+       if (!ntbl) {
+               err = nl_errno(ENOMEM);
+               goto errout;
+       }
+
+       ntbl->ce_msgtype = n->nlmsg_type;
+       rtmsg = nlmsg_data(n);
+       
+       err = nlmsg_parse(n, sizeof(*rtmsg), tb, NDTA_MAX, neightbl_policy);
+       if (err < 0)
+               goto errout;
+
+       ntbl->nt_family = rtmsg->rtgen_family;
+
+       if (tb[NDTA_NAME] == NULL) {
+               err = nl_error(EINVAL, "NDTA_NAME is missing");
+               goto errout;
+       }
+
+       nla_strlcpy(ntbl->nt_name, tb[NDTA_NAME], NTBLNAMSIZ);
+       ntbl->nt_mask |= NEIGHTBL_ATTR_NAME;
+
+       if (tb[NDTA_THRESH1]) {
+               ntbl->nt_gc_thresh1 = nla_get_u32(tb[NDTA_THRESH1]);
+               ntbl->nt_mask |= NEIGHTBL_ATTR_THRESH1;
+       }
+
+       if (tb[NDTA_THRESH2]) {
+               ntbl->nt_gc_thresh2 = nla_get_u32(tb[NDTA_THRESH2]);
+               ntbl->nt_mask |= NEIGHTBL_ATTR_THRESH2;
+       }
+
+       if (tb[NDTA_THRESH3]) {
+               ntbl->nt_gc_thresh3 = nla_get_u32(tb[NDTA_THRESH3]);
+               ntbl->nt_mask |= NEIGHTBL_ATTR_THRESH3;
+       }
+
+       if (tb[NDTA_GC_INTERVAL]) {
+               ntbl->nt_gc_interval = nla_get_u32(tb[NDTA_GC_INTERVAL]);
+               ntbl->nt_mask |= NEIGHTBL_ATTR_GC_INTERVAL;
+       }
+
+       if (tb[NDTA_CONFIG]) {
+               nla_memcpy(&ntbl->nt_config, tb[NDTA_CONFIG],
+                          sizeof(ntbl->nt_config));
+               ntbl->nt_mask |= NEIGHTBL_ATTR_CONFIG;
+       }
+
+       if (tb[NDTA_STATS]) {
+               nla_memcpy(&ntbl->nt_stats, tb[NDTA_STATS],
+                          sizeof(ntbl->nt_stats));
+               ntbl->nt_mask |= NEIGHTBL_ATTR_STATS;
+       }
+
+       if (tb[NDTA_PARMS]) {
+               struct nlattr *tbp[NDTPA_MAX + 1];
+               struct rtnl_neightbl_parms *p = &ntbl->nt_parms;
+
+               err = nla_parse_nested(tbp, NDTPA_MAX, tb[NDTA_PARMS], NULL);
+               if (err < 0)
+                       goto errout;
+
+#define COPY_ENTRY(name, var) \
+               if (tbp[NDTPA_ ##name]) { \
+                       p->ntp_ ##var = nla_get_u32(tbp[NDTPA_ ##name]); \
+                       p->ntp_mask |= NEIGHTBLPARM_ATTR_ ##name; \
+               }
+
+               COPY_ENTRY(IFINDEX, ifindex);
+               COPY_ENTRY(REFCNT, refcnt);
+               COPY_ENTRY(QUEUE_LEN, queue_len);
+               COPY_ENTRY(APP_PROBES, app_probes);
+               COPY_ENTRY(UCAST_PROBES, ucast_probes);
+               COPY_ENTRY(MCAST_PROBES, mcast_probes);
+               COPY_ENTRY(PROXY_QLEN, proxy_qlen);
+               COPY_ENTRY(PROXY_DELAY, proxy_delay);
+               COPY_ENTRY(ANYCAST_DELAY, anycast_delay);
+               COPY_ENTRY(LOCKTIME, locktime);
+               COPY_ENTRY(REACHABLE_TIME, reachable_time);
+               COPY_ENTRY(BASE_REACHABLE_TIME, base_reachable_time);
+               COPY_ENTRY(RETRANS_TIME, retrans_time);
+               COPY_ENTRY(GC_STALETIME, gc_stale_time);
+               COPY_ENTRY(DELAY_PROBE_TIME, probe_delay);
+#undef COPY_ENTRY
+
+               ntbl->nt_mask |= NEIGHTBL_ATTR_PARMS;
+       }
+
+       err = pp->pp_cb((struct nl_object *) ntbl, pp);
+       if (err < 0)
+               goto errout;
+
+       return P_ACCEPT;
+errout:
+       rtnl_neightbl_put(ntbl);
+       return err;
+}
+
+static int neightbl_request_update(struct nl_cache *c, struct nl_handle *h)
+{
+       return nl_rtgen_request(h, RTM_GETNEIGHTBL, AF_UNSPEC, NLM_F_DUMP);
+}
+
+
+static int neightbl_dump_brief(struct nl_object *arg, struct nl_dump_params *p)
+{
+       int line = 1;
+       struct rtnl_neightbl *ntbl = (struct rtnl_neightbl *) arg;
+
+       dp_dump(p, "%s", ntbl->nt_name);
+
+       if (ntbl->nt_parms.ntp_mask & NEIGHTBLPARM_ATTR_IFINDEX) {
+               struct nl_cache *link_cache;
+               
+               link_cache = nl_cache_mngt_require("route/link");
+
+               if (link_cache) {
+                       char buf[32];
+                       dp_dump(p, "<%s> ",
+                               rtnl_link_i2name(link_cache,
+                                                ntbl->nt_parms.ntp_ifindex,
+                                                buf, sizeof(buf)));
+               } else
+                       dp_dump(p, "<%u> ", ntbl->nt_parms.ntp_ifindex);
+       } else
+               dp_dump(p, " ");
+
+       if (ntbl->nt_mask & NEIGHTBL_ATTR_CONFIG)
+               dp_dump(p, "entries %u ", ntbl->nt_config.ndtc_entries);
+
+       if (ntbl->nt_mask & NEIGHTBL_ATTR_PARMS) {
+               char rt[32], rt2[32];
+               struct rtnl_neightbl_parms *pa = &ntbl->nt_parms;
+
+               dp_dump(p, "reachable-time %s retransmit-time %s",
+                       nl_msec2str(pa->ntp_reachable_time, rt, sizeof(rt)),
+                       nl_msec2str(pa->ntp_retrans_time, rt2, sizeof(rt2)));
+       }
+
+       dp_dump(p, "\n");
+
+       return line;
+}
+
+static int neightbl_dump_full(struct nl_object *arg, struct nl_dump_params *p)
+{
+       char x[32], y[32], z[32];
+       struct rtnl_neightbl *ntbl = (struct rtnl_neightbl *) arg;
+
+       int line = neightbl_dump_brief(arg, p);
+
+       if (ntbl->nt_mask & NEIGHTBL_ATTR_CONFIG) {
+               dp_new_line(p, line++);
+               dp_dump(p, "    key-len %u entry-size %u last-flush %s\n",
+                       ntbl->nt_config.ndtc_key_len,
+                       ntbl->nt_config.ndtc_entry_size,
+                       nl_msec2str(ntbl->nt_config.ndtc_last_flush,
+                                     x, sizeof(x)));
+
+               dp_new_line(p, line++);
+               dp_dump(p, "    gc threshold %u/%u/%u interval %s " \
+                           "chain-position %u\n",
+                       ntbl->nt_gc_thresh1, ntbl->nt_gc_thresh2,
+                       ntbl->nt_gc_thresh3,
+                       nl_msec2str(ntbl->nt_gc_interval, x, sizeof(x)),
+                       ntbl->nt_config.ndtc_hash_chain_gc);
+
+               dp_new_line(p, line++);
+               dp_dump(p, "    hash-rand 0x%08X/0x%08X last-rand %s\n",
+                       ntbl->nt_config.ndtc_hash_rnd,
+                       ntbl->nt_config.ndtc_hash_mask,
+                       nl_msec2str(ntbl->nt_config.ndtc_last_rand,
+                                     x, sizeof(x)));
+       }
+
+       if (ntbl->nt_mask & NEIGHTBL_ATTR_PARMS) {
+               struct rtnl_neightbl_parms *pa = &ntbl->nt_parms;
+
+               dp_new_line(p, line++);
+               dp_dump(p, "    refcnt %u pending-queue-limit %u " \
+                           "proxy-delayed-queue-limit %u\n",
+                       pa->ntp_refcnt,
+                       pa->ntp_queue_len,
+                       pa->ntp_proxy_qlen);
+
+               dp_new_line(p, line++);
+               dp_dump(p, "    num-userspace-probes %u num-unicast-probes " \
+                           "%u num-multicast-probes %u\n",
+                       pa->ntp_app_probes,
+                       pa->ntp_ucast_probes,
+                       pa->ntp_mcast_probes);
+
+               dp_new_line(p, line++);
+               dp_dump(p, "    min-age %s base-reachable-time %s " \
+                           "stale-check-interval %s\n",
+                       nl_msec2str(pa->ntp_locktime, x, sizeof(x)),
+                       nl_msec2str(pa->ntp_base_reachable_time,
+                                     y, sizeof(y)),
+                       nl_msec2str(pa->ntp_gc_stale_time, z, sizeof(z)));
+
+               dp_new_line(p, line++);
+               dp_dump(p, "    initial-probe-delay %s answer-delay %s " \
+                           "proxy-answer-delay %s\n",
+                       nl_msec2str(pa->ntp_probe_delay, x, sizeof(x)),
+                       nl_msec2str(pa->ntp_anycast_delay, y, sizeof(y)),
+                       nl_msec2str(pa->ntp_proxy_delay, z, sizeof(z)));
+       }
+
+       return line;
+}
+
+static int neightbl_dump_stats(struct nl_object *arg, struct nl_dump_params *p)
+{
+       struct rtnl_neightbl *ntbl = (struct rtnl_neightbl *) arg;
+       int line = neightbl_dump_full(arg, p);
+
+       if (!(ntbl->nt_mask & NEIGHTBL_ATTR_STATS))
+               return line;
+
+       dp_new_line(p, line++);
+       dp_dump(p, "    lookups %lld hits %lld failed %lld " \
+                   "allocations %lld destroys %lld\n",
+               ntbl->nt_stats.ndts_lookups,
+               ntbl->nt_stats.ndts_hits,
+               ntbl->nt_stats.ndts_res_failed,
+               ntbl->nt_stats.ndts_allocs,
+               ntbl->nt_stats.ndts_destroys);
+
+       dp_new_line(p, line++);
+       dp_dump(p, "    hash-grows %lld forced-gc-runs %lld " \
+                   "periodic-gc-runs %lld\n",
+               ntbl->nt_stats.ndts_hash_grows,
+               ntbl->nt_stats.ndts_forced_gc_runs,
+               ntbl->nt_stats.ndts_periodic_gc_runs);
+
+       dp_dump(p, "    rcv-unicast-probes %lld rcv-multicast-probes %lld\n",
+               ntbl->nt_stats.ndts_rcv_probes_ucast,
+               ntbl->nt_stats.ndts_rcv_probes_mcast);
+
+       return line;
+}
+
+/**
+ * @name Neighbour Table Object Allocation/Freeage
+ * @{
+ */
+
+/**
+ * Allocate a new neighbour table object
+ * @return New neighbour table object
+ */
+struct rtnl_neightbl *rtnl_neightbl_alloc(void)
+{
+       return (struct rtnl_neightbl *)
+                       nl_object_alloc_from_ops(&rtnl_neightbl_ops);
+}
+
+/**
+ * Give back reference on neighbour table object.
+ * @arg neightbl       Neighbour  table object to be given back.
+ *
+ * Decrements the reference counter and frees the object if the
+ * last reference has been released.
+ */
+void rtnl_neightbl_put(struct rtnl_neightbl *neightbl)
+{
+       nl_object_put((struct nl_object *) neightbl);
+}
+/**
+ * Free neighbour table object.
+ * @arg neightbl       Neighbour table object to be freed.
+ *
+ * @note Always use rtnl_neightbl_put() unless you're absolutely sure
+ *       that no other user may have a reference on this object.
+ */
+void rtnl_neightbl_free(struct rtnl_neightbl *neightbl)
+{
+       nl_object_free((struct nl_object *) neightbl);
+}
+
+/** @} */
+
+/**
+ * @name Neighbour Table Cache Management
+ * @{
+ */
+
+/**
+ * Build a neighbour table cache including all neighbour tables currently configured in the kernel.
+ * @arg handle         netlink handle
+ *
+ * Allocates a new neighbour table cache, initializes it properly and
+ * updates it to include all neighbour tables currently configured in
+ * the kernel.
+ *
+ * @note The caller is responsible for destroying and freeing the
+ *       cache after using it.
+ * @return The new cache or NULL if an error occured.
+ */
+struct nl_cache * rtnl_neightbl_alloc_cache(struct nl_handle *handle)
+{
+       struct nl_cache * cache;
+       
+       cache = nl_cache_alloc_from_ops(&rtnl_neightbl_ops);
+       if (cache == NULL)
+               return NULL;
+       
+       if (nl_cache_update(handle, cache) < 0) {
+               nl_cache_free(cache);
+               return NULL;
+       }
+
+       return cache;
+}
+
+/**
+ * Lookup neighbour table by name and optional interface index
+ * @arg cache          neighbour table cache
+ * @arg name           name of table
+ * @arg ifindex                optional interface index
+ *
+ * Looks up the neighbour table matching the specified name and
+ * optionally the specified ifindex to retrieve device specific
+ * parameter sets.
+ *
+ * @return ptr to neighbour table inside the cache or NULL if no
+ *         match was found.
+ */
+struct rtnl_neightbl *rtnl_neightbl_get(struct nl_cache *cache,
+                                       const char *name, int ifindex)
+{
+       struct rtnl_neightbl *nt;
+
+       if (cache->c_ops != &rtnl_neightbl_ops)
+               return NULL;
+
+       nl_list_for_each_entry(nt, &cache->c_items, ce_list) {
+               if (!strcasecmp(nt->nt_name, name) &&
+                   ((!ifindex && !nt->nt_parms.ntp_ifindex) ||
+                    (ifindex && ifindex == nt->nt_parms.ntp_ifindex))) {
+                       nl_object_get((struct nl_object *) nt);
+                       return nt;
+               }
+       }
+
+       return NULL;
+}
+
+/** @} */
+
+/**
+ * @name Neighbour Table Modifications
+ * @{
+ */
+
+/**
+ * Builds a netlink change request message to change neighbour table attributes
+ * @arg old            neighbour table to change
+ * @arg tmpl           template with requested changes
+ *
+ * Builds a new netlink message requesting a change of neighbour table
+ * attributes. The netlink message header isn't fully equipped with all
+ * relevant fields and must be sent out via nl_send_auto_complete() or
+ * supplemented as needed.
+ * \a old must point to a neighbour table currently configured in the
+ * kernel and \a tmpl must contain the attributes to be changed set via
+ * \c rtnl_neightbl_set_* functions.
+ *
+ * @return New netlink message
+ */
+struct nl_msg * rtnl_neightbl_build_change_request(struct rtnl_neightbl *old,
+                                                  struct rtnl_neightbl *tmpl)
+{
+       struct nl_msg *m;
+       struct ndtmsg ndt = {
+               .ndtm_family = old->nt_family,
+       };
+
+       m = nlmsg_build_simple(RTM_SETNEIGHTBL, 0);
+       nlmsg_append(m, &ndt, sizeof(ndt), 1);
+
+       nla_put_string(m, NDTA_NAME, old->nt_name);
+
+       if (tmpl->nt_mask & NEIGHTBL_ATTR_THRESH1)
+               nla_put_u32(m, NDTA_THRESH1, tmpl->nt_gc_thresh1);
+
+       if (tmpl->nt_mask & NEIGHTBL_ATTR_THRESH2)
+               nla_put_u32(m, NDTA_THRESH2, tmpl->nt_gc_thresh2);
+
+       if (tmpl->nt_mask & NEIGHTBL_ATTR_THRESH2)
+               nla_put_u32(m, NDTA_THRESH2, tmpl->nt_gc_thresh2);
+
+       if (tmpl->nt_mask & NEIGHTBL_ATTR_GC_INTERVAL)
+               nla_put_u64(m, NDTA_GC_INTERVAL,
+                                     tmpl->nt_gc_interval);
+
+       if (tmpl->nt_mask & NEIGHTBL_ATTR_PARMS) {
+               struct rtnl_neightbl_parms *p = &tmpl->nt_parms;
+               struct nl_msg *parms = nlmsg_build(NULL);
+
+               if (old->nt_parms.ntp_mask & NEIGHTBLPARM_ATTR_IFINDEX)
+                       nla_put_u32(parms, NDTPA_IFINDEX,
+                                             old->nt_parms.ntp_ifindex);
+
+
+               if (p->ntp_mask & NEIGHTBLPARM_ATTR_QUEUE_LEN)
+                       nla_put_u32(parms, NDTPA_QUEUE_LEN, p->ntp_queue_len);
+
+               if (p->ntp_mask & NEIGHTBLPARM_ATTR_APP_PROBES)
+                       nla_put_u32(parms, NDTPA_APP_PROBES, p->ntp_app_probes);
+
+               if (p->ntp_mask & NEIGHTBLPARM_ATTR_UCAST_PROBES)
+                       nla_put_u32(parms, NDTPA_UCAST_PROBES,
+                                   p->ntp_ucast_probes);
+
+               if (p->ntp_mask & NEIGHTBLPARM_ATTR_MCAST_PROBES)
+                       nla_put_u32(parms, NDTPA_MCAST_PROBES,
+                                   p->ntp_mcast_probes);
+
+               if (p->ntp_mask & NEIGHTBLPARM_ATTR_PROXY_QLEN)
+                       nla_put_u32(parms, NDTPA_PROXY_QLEN,
+                                   p->ntp_proxy_qlen);
+
+               if (p->ntp_mask & NEIGHTBLPARM_ATTR_BASE_REACHABLE_TIME)
+                       nla_put_u64(parms, NDTPA_BASE_REACHABLE_TIME,
+                                   p->ntp_base_reachable_time);
+
+               if (p->ntp_mask & NEIGHTBLPARM_ATTR_RETRANS_TIME)
+                       nla_put_u64(parms, NDTPA_RETRANS_TIME,
+                                   p->ntp_retrans_time);
+
+               if (p->ntp_mask & NEIGHTBLPARM_ATTR_GC_STALETIME)
+                       nla_put_u64(parms, NDTPA_GC_STALETIME,
+                                   p->ntp_gc_stale_time);
+
+               if (p->ntp_mask & NEIGHTBLPARM_ATTR_DELAY_PROBE_TIME)
+                       nla_put_u64(parms, NDTPA_DELAY_PROBE_TIME,
+                                   p->ntp_proxy_delay);
+
+               if (p->ntp_mask & NEIGHTBLPARM_ATTR_ANYCAST_DELAY)
+                       nla_put_u64(parms, NDTPA_ANYCAST_DELAY,
+                                   p->ntp_anycast_delay);
+
+               if (p->ntp_mask & NEIGHTBLPARM_ATTR_PROXY_DELAY)
+                       nla_put_u64(parms, NDTPA_PROXY_DELAY,
+                                             p->ntp_proxy_delay);
+
+               if (p->ntp_mask & NEIGHTBLPARM_ATTR_LOCKTIME)
+                       nla_put_u64(parms, NDTPA_LOCKTIME, p->ntp_locktime);
+
+               nla_put_nested(m, NDTA_PARMS, parms);
+               nlmsg_free(parms);
+       }
+       
+       return m;
+}
+
+/**
+ * Change neighbour table attributes
+ * @arg handle         netlink handle
+ * @arg old            neighbour table to be changed
+ * @arg tmpl           template with requested changes
+ *
+ * Builds a new netlink message by calling
+ * rtnl_neightbl_build_change_request(), sends the request to the
+ * kernel and waits for the next ACK to be received, i.e. blocks
+ * until the request has been processed.
+ *
+ * @return 0 on success or a negative error code
+ */
+int rtnl_neightbl_change(struct nl_handle *handle, struct rtnl_neightbl *old,
+                        struct rtnl_neightbl *tmpl)
+{
+       int err;
+       struct nl_msg *msg;
+       
+       msg = rtnl_neightbl_build_change_request(old, tmpl);
+       err = nl_send_auto_complete(handle, msg);
+       if (err < 0)
+               return err;
+
+       nlmsg_free(msg);
+       return nl_wait_for_ack(handle);
+}
+
+/** @} */
+
+/**
+ * @name Attribute Modification
+ * @{
+ */
+
+/**
+ * Set the address family of a neighbour table to the specified value
+ * @arg ntbl           neighbour table to change
+ * @arg family         new address family
+ */
+void rtnl_neightbl_set_family(struct rtnl_neightbl *ntbl, int family)
+{
+       ntbl->nt_family = family;
+       ntbl->nt_mask |= NEIGHTBL_ATTR_FAMILY;
+}
+
+/**
+ * Set the gc interval of a neighbour table to the specified value
+ * @arg ntbl           neighbour table to change
+ * @arg ms             new interval in milliseconds
+ */
+void rtnl_neightbl_set_gc_interval(struct rtnl_neightbl *ntbl, uint64_t ms)
+{
+       ntbl->nt_gc_interval = ms;
+       ntbl->nt_mask |= NEIGHTBL_ATTR_GC_INTERVAL;
+}
+
+/**
+ * Set the gc threshold 1 of a neighbour table to the specified value
+ * @arg ntbl           neighbour table to change
+ * @arg thresh         new gc threshold
+ */
+void rtnl_neightbl_set_gc_tresh1(struct rtnl_neightbl *ntbl, int thresh)
+{
+       ntbl->nt_gc_thresh1 = thresh;
+       ntbl->nt_mask |= NEIGHTBL_ATTR_THRESH1;
+}
+
+/**
+ * Set the gc threshold 2 of a neighbour table to the specified value
+ * @arg ntbl           neighbour table to change
+ * @arg thresh         new gc threshold
+ */
+void rtnl_neightbl_set_gc_tresh2(struct rtnl_neightbl *ntbl, int thresh)
+{
+       ntbl->nt_gc_thresh2 = thresh;
+       ntbl->nt_mask |= NEIGHTBL_ATTR_THRESH2;
+}
+
+/**
+ * Set the gc threshold 3 of a neighbour table to the specified value
+ * @arg ntbl           neighbour table to change
+ * @arg thresh         new gc threshold
+ */
+void rtnl_neightbl_set_gc_tresh3(struct rtnl_neightbl *ntbl, int thresh)
+{
+       ntbl->nt_gc_thresh3 = thresh;
+       ntbl->nt_mask |= NEIGHTBL_ATTR_THRESH3;
+}
+
+/**
+ * Set the table name of a neighbour table to the specified value
+ * @arg ntbl           neighbour table to change
+ * @arg name           new table name
+ */
+void rtnl_neightbl_set_name(struct rtnl_neightbl *ntbl, const char *name)
+{
+       strncpy(ntbl->nt_name, name, sizeof(ntbl->nt_name) - 1);
+       ntbl->nt_mask |= NEIGHTBL_ATTR_NAME;
+}
+
+/**
+ * Set the device of a neighbour table to the specified value
+ * @arg ntbl           neighbour table to change
+ * @arg ifindex                interface index of the device
+ */
+void rtnl_neightbl_set_dev(struct rtnl_neightbl *ntbl, int ifindex)
+{
+       ntbl->nt_parms.ntp_ifindex = ifindex;
+       ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_IFINDEX;
+       ntbl->nt_mask |= NEIGHTBL_ATTR_PARMS;
+}
+
+/**
+ * Set the queue length for pending requests of a neighbour table to the specified value
+ * @arg ntbl           neighbour table to change
+ * @arg len            new queue len
+ */
+void rtnl_neightbl_set_queue_len(struct rtnl_neightbl *ntbl, int len)
+{
+       ntbl->nt_parms.ntp_queue_len = len;
+       ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_QUEUE_LEN;
+       ntbl->nt_mask |= NEIGHTBL_ATTR_PARMS;
+}
+
+/**
+ * Set the queue length for delay proxy arp requests of a neighbour table to the specified value
+ * @arg ntbl           neighbour table to change
+ * @arg len            new queue len
+ */
+void rtnl_neightbl_set_proxy_queue_len(struct rtnl_neightbl *ntbl, int len)
+{
+       ntbl->nt_parms.ntp_proxy_qlen = len;
+       ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_PROXY_QLEN;
+       ntbl->nt_mask |= NEIGHTBL_ATTR_PARMS;
+}
+
+/**
+ * Set the number of application probes of a neighbour table to the specified value
+ * @arg ntbl           neighbour table to change
+ * @arg probes         new probes value
+ */
+void rtnl_neightbl_set_app_probes(struct rtnl_neightbl *ntbl, int probes)
+{
+       ntbl->nt_parms.ntp_app_probes = probes;
+       ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_APP_PROBES;
+       ntbl->nt_mask |= NEIGHTBL_ATTR_PARMS;
+}
+
+/**
+ * Set the number of unicast probes of a neighbour table to the specified value
+ * @arg ntbl           neighbour table to change
+ * @arg probes         new probes value
+ */
+void rtnl_neightbl_set_ucast_probes(struct rtnl_neightbl *ntbl, int probes)
+{
+       ntbl->nt_parms.ntp_ucast_probes = probes;
+       ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_UCAST_PROBES;
+       ntbl->nt_mask |= NEIGHTBL_ATTR_PARMS;
+}
+
+/**
+ * Set the number of multicast probes of a neighbour table to the specified value
+ * @arg ntbl           neighbour table to change
+ * @arg probes         new probes value
+ */
+void rtnl_neightbl_set_mcast_probes(struct rtnl_neightbl *ntbl, int probes)
+{
+       ntbl->nt_parms.ntp_mcast_probes = probes;
+       ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_MCAST_PROBES;
+       ntbl->nt_mask |= NEIGHTBL_ATTR_PARMS;
+}
+
+/**
+ * Set the base reachable time of a neighbour table to the specified value
+ * @arg ntbl           neighbour table to change
+ * @arg ms             new base reachable time in milliseconds
+ */
+void rtnl_neightbl_set_base_reachable_time(struct rtnl_neightbl *ntbl,
+                                          uint64_t ms)
+{
+       ntbl->nt_parms.ntp_base_reachable_time = ms;
+       ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_BASE_REACHABLE_TIME;
+       ntbl->nt_mask |= NEIGHTBL_ATTR_PARMS;
+}
+
+/**
+ * Set the retransmit time of a neighbour table to the specified value
+ * @arg ntbl           neighbour table to change
+ * @arg ms             new retransmit time
+ */
+void rtnl_neightbl_set_retrans_time(struct rtnl_neightbl *ntbl, uint64_t ms)
+{
+       ntbl->nt_parms.ntp_retrans_time = ms;
+       ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_RETRANS_TIME;
+       ntbl->nt_mask |= NEIGHTBL_ATTR_PARMS;
+}
+
+/**
+ * Set the gc stale time of a neighbour table to the specified value
+ * @arg ntbl           neighbour table to change
+ * @arg ms             new gc stale time in milliseconds
+ */
+void rtnl_neightbl_set_gc_stale_time(struct rtnl_neightbl *ntbl, uint64_t ms)
+{
+       ntbl->nt_parms.ntp_gc_stale_time = ms;
+       ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_GC_STALETIME;
+       ntbl->nt_mask |= NEIGHTBL_ATTR_PARMS;
+}
+
+/**
+ * Set the first probe delay time of a neighbour table to the specified value
+ * @arg ntbl           neighbour table to change
+ * @arg ms             new first probe delay time in milliseconds
+ */
+void rtnl_neightbl_set_delay_probe_time(struct rtnl_neightbl *ntbl, uint64_t ms)
+{
+       ntbl->nt_parms.ntp_probe_delay = ms;
+       ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_DELAY_PROBE_TIME;
+       ntbl->nt_mask |= NEIGHTBL_ATTR_PARMS;
+}
+
+/**
+ * Set the anycast delay of a neighbour table to the specified value
+ * @arg ntbl           neighbour table to change
+ * @arg ms             new anycast delay in milliseconds
+ */
+void rtnl_neightbl_set_anycast_delay(struct rtnl_neightbl *ntbl, uint64_t ms)
+{
+       ntbl->nt_parms.ntp_anycast_delay = ms;
+       ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_ANYCAST_DELAY;
+       ntbl->nt_mask |= NEIGHTBL_ATTR_PARMS;
+}
+
+/**
+ * Set the proxy delay of a neighbour table to the specified value
+ * @arg ntbl           neighbour table to change
+ * @arg ms             new proxy delay in milliseconds
+ */
+void rtnl_neightbl_set_proxy_delay(struct rtnl_neightbl *ntbl, uint64_t ms)
+{
+       ntbl->nt_parms.ntp_proxy_delay = ms;
+       ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_PROXY_DELAY;
+       ntbl->nt_mask |= NEIGHTBL_ATTR_PARMS;
+}
+
+/**
+ * Set the locktime of a neighbour table to the specified value
+ * @arg ntbl           neighbour table to change
+ * @arg ms             new locktime in milliseconds
+ */
+void rtnl_neightbl_set_locktime(struct rtnl_neightbl *ntbl, uint64_t ms)
+{
+       ntbl->nt_parms.ntp_locktime = ms;
+       ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_LOCKTIME;
+       ntbl->nt_mask |= NEIGHTBL_ATTR_PARMS;
+}
+
+/** @} */
+
+static struct nl_cache_ops rtnl_neightbl_ops = {
+       .co_name                = "route/neightbl",
+       .co_size                = sizeof(struct rtnl_neightbl),
+       .co_hdrsize             = sizeof(struct rtgenmsg),
+       .co_msgtypes            = {
+                                       { RTM_NEWNEIGHTBL, "new" },
+                                       { RTM_SETNEIGHTBL, "set" },
+                                       { RTM_GETNEIGHTBL, "get" },
+                                       { -1, NULL },
+                                 },
+       .co_protocol            = NETLINK_ROUTE,
+       .co_request_update      = &neightbl_request_update,
+       .co_msg_parser          = &neightbl_msg_parser,
+       .co_dump[NL_DUMP_BRIEF] = &neightbl_dump_brief,
+       .co_dump[NL_DUMP_FULL]  = &neightbl_dump_full,
+       .co_dump[NL_DUMP_STATS] = &neightbl_dump_stats,
+       .co_filter              = &neightbl_filter,
+};
+
+static void __init neightbl_init(void)
+{
+       nl_cache_mngt_register(&rtnl_neightbl_ops);
+}
+
+static void __exit neightbl_exit(void)
+{
+       nl_cache_mngt_unregister(&rtnl_neightbl_ops);
+}
+
+/** @} */
diff --git a/lib/route/nexthop.c b/lib/route/nexthop.c
new file mode 100644 (file)
index 0000000..720ceb5
--- /dev/null
@@ -0,0 +1,207 @@
+/*
+ * lib/route/nexthop.c Routing Nexthop
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup route
+ * @defgroup nexthop Nexthop
+ * @brief
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink/netlink.h>
+#include <netlink/utils.h>
+#include <netlink/route/rtnl.h>
+#include <netlink/route/route.h>
+
+/** @cond SKIP */
+#define NEXTHOP_HAS_FLAGS   0x000001
+#define NEXTHOP_HAS_WEIGHT  0x000002
+#define NEXTHOP_HAS_IFINDEX 0x000004
+#define NEXTHOP_HAS_GATEWAY 0x000008
+/** @endcond */
+
+/**
+ * @name Nexthop Allocation/Freeage
+ * @{
+ */
+
+/**
+ * Allocate a routing nexthop.
+ * @return Newly allocated routing nexthop object.
+ */
+struct rtnl_nexthop *rtnl_route_nh_alloc(void)
+{
+       struct rtnl_nexthop *nh;
+
+       nh = calloc(1, sizeof(*nh));
+       if (!nh) {
+               nl_errno(ENOMEM);
+               return NULL;
+       }
+
+       nl_init_list_head(&nh->rtnh_list);
+
+       return nh;
+}
+
+/**
+ * Free a routing nexthop.
+ * @arg nh             Routing nexthop to be freed.
+ */
+void rtnl_route_nh_free(struct rtnl_nexthop *nh)
+{
+       nl_addr_put(nh->rtnh_gateway);
+       free(nh);
+}
+
+/** @} */
+/**
+ * @name Attribute: Weight
+ */
+
+/**
+ * Set weight of routing nexthop.
+ * @arg nh             Routing nexthop.
+ * @arg weight         New weight value.
+ */
+void rtnl_route_nh_set_weight(struct rtnl_nexthop *nh, int weight)
+{
+       nh->rtnh_weight = weight;
+       nh->rtnh_mask |= NEXTHOP_HAS_WEIGHT;
+}
+
+/**
+ * Get weight of routing nexthop.
+ * @arg nh             Routing nexthop.
+ * @return Weight value or 0 if not available.
+ */
+int rtnl_route_nh_get_weight(struct rtnl_nexthop *nh)
+{
+       if (nh->rtnh_mask & NEXTHOP_HAS_WEIGHT)
+               return nh->rtnh_weight;
+       else
+               return 0;
+}
+
+/** @} */
+/**
+ * @name Attribute: Interface Index
+ * @{
+ */
+
+/**
+ * Set interface index for outgoing interface of routing nexthop.
+ * @arg nh             Routing nexthop.
+ * @arg ifindex                New interface index.
+ */
+void rtnl_route_nh_set_ifindex(struct rtnl_nexthop *nh, int ifindex)
+{
+       nh->rtnh_ifindex = ifindex;
+       nh->rtnh_mask |= NEXTHOP_HAS_IFINDEX;
+}
+
+/**
+ * Get interface index of outgoing index of routing nexthop.
+ * @arg nh             Routing nexthop.
+ * @return Interface index or -1 if not available.
+ */
+int rtnl_route_nh_get_ifindex(struct rtnl_nexthop *nh)
+{
+       if (nh->rtnh_mask & NEXTHOP_HAS_IFINDEX)
+               return nh->rtnh_ifindex;
+       else
+               return -1;
+}      
+
+/** @} */
+/**
+ * @name Attribute: Gateway Address
+ * @{
+ */
+
+/**
+ * Set gateway address of routing nexthop.
+ * @arg nh             Routing nexthop.
+ * @arg addr           New gateway address.
+ *
+ * An eventual existing gateway address will be freed and a
+ * reference is acquried of the new address.
+ */
+void rtnl_route_nh_set_gateway(struct rtnl_nexthop *nh, struct nl_addr *addr)
+{
+       struct nl_addr *old = nh->rtnh_gateway;
+
+       nh->rtnh_gateway = nl_addr_get(addr);
+       if (old)
+               nl_addr_put(old);
+
+       nh->rtnh_mask |= NEXTHOP_HAS_GATEWAY;
+}
+
+/**
+ * Get gateway address of routing nexthop.
+ * @arg nh             Routing nexthop.
+ * @return Gateway address or NULL if not available.
+ */
+struct nl_addr *rtnl_route_nh_get_gateway(struct rtnl_nexthop *nh)
+{
+       if (nh->rtnh_mask & NEXTHOP_HAS_GATEWAY)
+               return nh->rtnh_gateway;
+       else
+               return NULL;
+}
+
+/** @} */
+/**
+ * @name Attribute: Flags
+ * @{
+ */
+
+/**
+ * Set flags of routing nexthop.
+ * @arg nh             Routing nexthop.
+ * @arg flags          Flags to be set.
+ */
+void rtnl_route_nh_set_flags(struct rtnl_nexthop *nh, unsigned int flags)
+{
+       nh->rtnh_flag_mask |= flags;
+       nh->rtnh_flags |= flags;
+       nh->rtnh_mask |= NEXTHOP_HAS_FLAGS;
+}
+
+/**
+ * Unset flags of routing nexthop.
+ * @arg nh             Routing nexthop.
+ * @arg flags          Flags to be unset.
+ */
+void rtnl_route_nh_unset_flags(struct rtnl_nexthop *nh, unsigned int flags)
+{
+       nh->rtnh_flag_mask |= flags;
+       nh->rtnh_flags &= ~flags;
+       nh->rtnh_mask |= NEXTHOP_HAS_FLAGS;
+}
+
+/**
+ * Get flags of routing nexthop.
+ * @arg nh             Routing nexthop.
+ * @return Flags or 0 if not available.
+ */
+unsigned int rtnl_route_nh_get_flags(struct rtnl_nexthop *nh)
+{
+       if (nh->rtnh_mask & NEXTHOP_HAS_FLAGS)
+               return nh->rtnh_flags;
+       else
+               return 0;
+}
+
+/** @} */
+/** @} */
diff --git a/lib/route/qdisc.c b/lib/route/qdisc.c
new file mode 100644 (file)
index 0000000..c2c70fa
--- /dev/null
@@ -0,0 +1,841 @@
+/*
+ * lib/route/qdisc.c            Queueing Disciplines
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup tc
+ * @defgroup qdisc Queueing Disciplines
+ *
+ * @par Qdisc Handles
+ * In general, qdiscs are identified by the major part of a traffic control
+ * handle (the upper 16 bits). A few special values exist though:
+ *  - \c TC_H_ROOT: root qdisc (directly attached to the device)
+ *  - \c TC_H_INGRESS: ingress qdisc (directly attached to the device)
+ *  - \c TC_H_UNSPEC: unspecified qdisc (no reference)
+ *
+ * @par 1) Adding a Qdisc
+ * @code
+ * // Allocate a new empty qdisc to be filled out
+ * struct rtnl_qdisc *qdisc = rtnl_qdisc_alloc();
+ *
+ * // ... specify the kind of the Qdisc
+ * rtnl_qdisc_set_kind(qdisc, "pfifo");
+ *
+ * // Specify the device the qdisc should be attached to
+ * rtnl_qdisc_set_ifindex(qdisc, ifindex);
+ *
+ * // ... specify the parent qdisc
+ * rtnl_qdisc_set_parent(qdisc, TC_H_ROOT);
+ *
+ * // Specifying the handle is not required but makes reidentifying easier
+ * // and may help to avoid adding a qdisc twice.
+ * rtnl_qdisc_set_handle(qdisc, 0x000A0000);
+ *
+ * // Now on to specify the qdisc specific options, see the relevant qdisc
+ * // modules for documentation, in this example we set the upper limit of
+ * // the packet fifo qdisc to 64
+ * rtnl_qdisc_fifo_set_limit(qdisc, 64);
+ *
+ * rtnl_qdisc_add(handle, qdisc, NLM_R_REPLACE);
+ *
+ * // Free up the memory
+ * rtnl_qdisc_put(qdisc);
+ * @endcode
+ *
+ * @par 2) Deleting a Qdisc
+ * @code
+ * // Allocate a new empty qdisc to be filled out with the parameters
+ * // specifying the qdisc to be deleted. Alternatively a fully equiped
+ * // Qdisc object from a cache can be used.
+ * struct rtnl_qdisc *qdisc = rtnl_qdisc_alloc();
+ *
+ * // The interface index of the device the qdisc is on and the parent handle
+ * // are the least required fields to be filled out.
+ * // Note: Specify TC_H_ROOT or TC_H_INGRESS as parent handle to delete the
+ * //       root respectively root ingress qdisc.
+ * rtnl_qdisc_set_ifindex(qdisc, ifindex);
+ * rtnl_qdisc_set_parent(qdisc, parent_handle);
+ *
+ * // If required for identification, the handle can be specified as well.
+ * rtnl_qdisc_set_handle(qdisc, qdisc_handle);
+ *
+ * // Not required but maybe helpful as sanity check, the kind of the qdisc
+ * // can be specified to avoid mistakes.
+ * rtnl_qdisc_set_kind(qdisc, "pfifo");
+ *
+ * // Finally delete the qdisc with rtnl_qdisc_delete(), alternatively
+ * // rtnl_qdisc_build_delete_request() can be invoked to generate an
+ * // appropritate netlink message to send out.
+ * rtnl_qdisc_delete(handle, qdisc);
+ *
+ * // Free up the memory
+ * rtnl_qdisc_put(qdisc);
+ * @endcode
+ *
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink-tc.h>
+#include <netlink/netlink.h>
+#include <netlink/utils.h>
+#include <netlink/route/link.h>
+#include <netlink/route/tc.h>
+#include <netlink/route/qdisc.h>
+#include <netlink/route/class.h>
+#include <netlink/route/classifier.h>
+#include <netlink/route/qdisc-modules.h>
+
+/** @cond SKIP */
+static struct nl_cache_ops rtnl_qdisc_ops;
+/** @endcond */
+
+static struct rtnl_qdisc_ops *qdisc_ops_list;
+
+static struct rtnl_qdisc_ops *qdisc_lookup_ops(const char *kind)
+{
+       struct rtnl_qdisc_ops *ops;
+
+       for (ops = qdisc_ops_list; ops; ops = ops->qo_next)
+               if (!strcmp(kind, ops->qo_kind))
+                       return ops;
+
+       return NULL;
+}
+
+static inline struct rtnl_qdisc_ops *qdisc_ops(struct rtnl_qdisc *qdisc)
+{
+       if (!qdisc->q_ops)
+               qdisc->q_ops = qdisc_lookup_ops(qdisc->q_kind);
+
+       return qdisc->q_ops;
+}
+
+/**
+ * @name QDisc Module API
+ * @{
+ */
+
+/**
+ * Register a qdisc module
+ * @arg ops            qdisc module operations
+ */
+int rtnl_qdisc_register(struct rtnl_qdisc_ops *ops)
+{
+       struct rtnl_qdisc_ops *o, **op;
+
+       if (!ops->qo_kind[0])
+               BUG();
+
+       for (op = &qdisc_ops_list; (o = *op) != NULL; op = &o->qo_next)
+               if (!strcasecmp(ops->qo_kind, o->qo_kind))
+                       return nl_errno(EEXIST);
+
+       ops->qo_next = NULL;
+       *op = ops;
+
+       return 0;
+}
+
+/**
+ * Unregister a qdisc module
+ * @arg ops            qdisc module operations
+ */
+int rtnl_qdisc_unregister(struct rtnl_qdisc_ops *ops)
+{
+       struct rtnl_qdisc_ops *o, **op;
+
+       for (op = &qdisc_ops_list; (o = *op) != NULL; op = &o->qo_next)
+               if (!strcasecmp(ops->qo_kind, o->qo_kind))
+                       break;
+
+       if (!o)
+               return nl_errno(ENOENT);
+
+       *op = ops->qo_next;
+
+       return 0;
+}
+
+/** @} */
+
+static int qdisc_msg_parser(struct sockaddr_nl *who, struct nlmsghdr *n,
+                           void *arg)
+{
+       int err = -ENOMEM;
+       struct nl_parser_param *pp = arg;
+       struct rtnl_qdisc *qdisc;
+       struct rtnl_qdisc_ops *ops;
+
+       qdisc = rtnl_qdisc_alloc();
+       if (!qdisc) {
+               err = nl_errno(ENOMEM);
+               goto errout;
+       }
+
+       qdisc->ce_msgtype = n->nlmsg_type;
+
+       err = tca_msg_parser(n, (struct rtnl_tca *) qdisc);
+       if (err < 0)
+               goto errout_free;
+
+       ops = qdisc_ops(qdisc);
+       if (ops && ops->qo_msg_parser) {
+               err = ops->qo_msg_parser(qdisc);
+               if (err < 0)
+                       goto errout_free;
+       }
+
+       err = pp->pp_cb((struct nl_object *) qdisc, pp);
+       if (err < 0)
+               goto errout_free;
+
+       return P_ACCEPT;
+
+errout_free:
+       rtnl_qdisc_put(qdisc);
+errout:
+       return err;
+}
+
+static int qdisc_request_update(struct nl_cache *c, struct nl_handle *h)
+{
+       struct tcmsg tchdr = {
+               .tcm_family = AF_UNSPEC,
+               .tcm_ifindex = c->c_iarg1,
+       };
+
+       return nl_send_simple(h, RTM_GETQDISC, NLM_F_DUMP, &tchdr,
+                             sizeof(tchdr));
+}
+
+static void qdisc_free_data(struct nl_object *obj)
+{
+       struct rtnl_qdisc *qdisc = (struct rtnl_qdisc *) obj;
+       struct rtnl_qdisc_ops *ops;
+
+       tca_free_data((struct rtnl_tca *) qdisc);
+
+       ops = qdisc_ops(qdisc);
+       if (ops && ops->qo_free_data)
+               ops->qo_free_data(qdisc);
+}
+
+static int qdisc_dump_brief(struct nl_object *obj, struct nl_dump_params *p)
+{
+       struct rtnl_qdisc *qdisc = (struct rtnl_qdisc *) obj;
+       struct rtnl_qdisc_ops *ops;
+
+       int line = tca_dump_brief((struct rtnl_tca *) qdisc, "qdisc", p, 0);
+
+       ops = qdisc_ops(qdisc);
+       if (ops && ops->qo_dump[NL_DUMP_BRIEF])
+               line = ops->qo_dump[NL_DUMP_BRIEF](qdisc, p, line);
+
+       dp_dump(p, "\n");
+
+       return line;
+}
+
+static int qdisc_dump_full(struct nl_object *arg, struct nl_dump_params *p)
+{
+       struct rtnl_qdisc *qdisc = (struct rtnl_qdisc *) arg;
+       struct rtnl_qdisc_ops *ops;
+
+       int line = qdisc_dump_brief(arg, p);
+
+       line = tca_dump_full((struct rtnl_tca *) qdisc, p, line);
+       dp_dump(p, "refcnt %u ", qdisc->q_info);
+
+       ops = qdisc_ops(qdisc);
+       if (ops && ops->qo_dump[NL_DUMP_FULL])
+               line = ops->qo_dump[NL_DUMP_FULL](qdisc, p, line);
+
+       dp_dump(p, "\n");
+       return line;
+}
+
+static int qdisc_dump_stats(struct nl_object *arg, struct nl_dump_params *p)
+{
+       struct rtnl_qdisc *qdisc = (struct rtnl_qdisc *) arg;
+       struct rtnl_qdisc_ops *ops;
+
+       int line = qdisc_dump_full(arg, p);
+       line = tca_dump_stats((struct rtnl_tca *) qdisc, p, line );
+       dp_dump(p, "\n");
+
+       ops = qdisc_ops(qdisc);
+       if (ops && ops->qo_dump[NL_DUMP_STATS])
+               line = ops->qo_dump[NL_DUMP_STATS](qdisc, p, line);
+
+       return line;
+}
+
+static int qdisc_filter(struct nl_object *obj, struct nl_object *filter)
+{
+       return tca_filter((struct rtnl_tca *) obj, (struct rtnl_tca *) filter);
+}
+
+/**
+ * @name QDisc Addition
+ * @{
+ */
+
+static struct nl_msg *qdisc_build(struct rtnl_qdisc *qdisc, int type, int flags)
+{
+       struct rtnl_qdisc_ops *ops;
+       struct nl_msg *msg;
+       int err;
+
+       msg = tca_build_msg((struct rtnl_tca *) qdisc, type, flags);
+       if (!msg)
+               goto errout;
+
+       ops = qdisc_ops(qdisc);
+       if (ops && ops->qo_get_opts) {
+               struct nl_msg *opts;
+               
+               opts = ops->qo_get_opts(qdisc);
+               if (opts) {
+                       err = nla_put_nested(msg, TCA_OPTIONS, opts);
+                       nlmsg_free(opts);
+                       if (err < 0)
+                               goto errout;
+               }
+       }
+
+       return msg;
+errout:
+       nlmsg_free(msg);
+
+       return NULL;
+}
+
+/**
+ * Build a netlink message to add a new qdisc
+ * @arg qdisc          qdisc to add 
+ * @arg flags          additional netlink message flags
+ *
+ * Builds a new netlink message requesting an addition of a qdisc.
+ * The netlink message header isn't fully equipped with all relevant
+ * fields and must be sent out via nl_send_auto_complete() or
+ * supplemented as needed. 
+ *
+ * Common message flags used:
+ *  - NLM_F_REPLACE - replace a potential existing qdisc
+ *
+ * @return New netlink message
+ */
+struct nl_msg *rtnl_qdisc_build_add_request(struct rtnl_qdisc *qdisc,
+                                           int flags)
+{
+       struct nl_msg *msg;
+
+       msg = qdisc_build(qdisc, RTM_NEWQDISC, NLM_F_CREATE | flags);
+       if (!msg)
+               nl_errno(ENOMEM);
+
+       return msg;
+}
+
+/**
+ * Add a new qdisc
+ * @arg handle         netlink handle
+ * @arg qdisc          qdisc to delete
+ * @arg flags          additional netlink message flags
+ *
+ * Builds a netlink message by calling rtnl_qdisc_build_add_request(),
+ * sends the request to the kernel and waits for the ACK to be
+ * received and thus blocks until the request has been processed.
+ *
+ * Common message flags used:
+ *  - NLM_F_REPLACE - replace a potential existing qdisc
+ *
+ * @return 0 on success or a negative error code
+ */
+int rtnl_qdisc_add(struct nl_handle *handle, struct rtnl_qdisc *qdisc,
+                  int flags)
+{
+       struct nl_msg *msg;
+       int err;
+
+       msg = rtnl_qdisc_build_add_request(qdisc, flags);
+       if (!msg)
+               return nl_errno(ENOMEM);
+
+       err = nl_send_auto_complete(handle, msg);
+       if (err < 0)
+               return err;
+
+       nlmsg_free(msg);
+       return nl_wait_for_ack(handle);
+}
+
+/** @} */
+
+/**
+ * @name QDisc Modification
+ * @{
+ */
+
+/**
+ * Build a netlink message to change attributes of a existing qdisc
+ * @arg qdisc          qdisc to change
+ * @arg new            new qdisc attributes
+ *
+ * Builds a new netlink message requesting an change of qdisc
+ * attributes. The netlink message header isn't fully equipped
+ * with all relevant fields and must be sent out via
+ * nl_send_auto_complete() or supplemented as needed. 
+ *
+ * @return New netlink message
+ */
+struct nl_msg *rtnl_qdisc_build_change_request(struct rtnl_qdisc *qdisc,
+                                              struct rtnl_qdisc *new)
+{
+       return qdisc_build(qdisc, RTM_NEWQDISC, NLM_F_REPLACE);
+}
+
+/**
+ * Change attributes of a qdisc
+ * @arg handle         netlink handle
+ * @arg qdisc          qdisc to change
+ * @arg new            new qdisc attributes
+ *
+ * Builds a netlink message by calling rtnl_qdisc_build_change_request(),
+ * sends the request to the kernel and waits for the ACK to be
+ * received and thus blocks until the request has been processed.
+ *
+ * @return 0 on success or a negative error code
+ */
+int rtnl_qdisc_change(struct nl_handle *handle, struct rtnl_qdisc *qdisc,
+                     struct rtnl_qdisc *new)
+{
+       struct nl_msg *msg;
+       int err;
+
+       msg = rtnl_qdisc_build_change_request(qdisc, new);
+       if (!msg)
+               return nl_errno(ENOMEM);
+
+       err = nl_send_auto_complete(handle, msg);
+       if (err < 0)
+               return err;
+
+       nlmsg_free(msg);
+       return nl_wait_for_ack(handle);
+}
+
+/** @} */
+
+/**
+ * @name QDisc Deletion
+ * @{
+ */
+
+/**
+ * Build a netlink request message to delete a qdisc
+ * @arg qdisc          qdisc to delete
+ *
+ * Builds a new netlink message requesting a deletion of a qdisc.
+ * The netlink message header isn't fully equipped with all relevant
+ * fields and must thus be sent out via nl_send_auto_complete()
+ * or supplemented as needed.
+ *
+ * @return New netlink message
+ */
+struct nl_msg *rtnl_qdisc_build_delete_request(struct rtnl_qdisc *qdisc)
+{
+       struct nl_msg *msg;
+       struct tcmsg tchdr;
+       int required = TCA_ATTR_IFINDEX | TCA_ATTR_PARENT;
+
+       if ((qdisc->q_mask & required) != required)
+               BUG();
+
+       msg = nlmsg_build_simple(RTM_DELQDISC, 0);
+       if (!msg)
+               return NULL;
+
+       tchdr.tcm_family = AF_UNSPEC,
+       tchdr.tcm_handle = qdisc->q_handle,
+       tchdr.tcm_parent = qdisc->q_parent,
+       tchdr.tcm_ifindex = qdisc->q_ifindex,
+       nlmsg_append(msg, &tchdr, sizeof(tchdr), 1);
+
+       return msg;
+}
+
+/**
+ * Delete a qdisc
+ * @arg handle         netlink handle
+ * @arg qdisc          qdisc to delete
+ *
+ * Builds a netlink message by calling rtnl_qdisc_build_delete_request(),
+ * sends the request to the kernel and waits for the ACK to be
+ * received and thus blocks until the request has been processed.
+ *
+ * @return 0 on success or a negative error code
+ */
+int rtnl_qdisc_delete(struct nl_handle *handle, struct rtnl_qdisc *qdisc)
+{
+       struct nl_msg *msg;
+       int err;
+
+       msg = rtnl_qdisc_build_delete_request(qdisc);
+       if (!msg)
+               return nl_errno(ENOMEM);
+
+       err = nl_send_auto_complete(handle, msg);
+       if (err < 0)
+               return err;
+
+       nlmsg_free(msg);
+       return nl_wait_for_ack(handle);
+}
+
+/** @} */
+
+/**
+ * @name General
+ * @{
+ */
+
+/**
+ * Allocate a new qdisc object
+ * @return New qdisc object
+ */
+struct rtnl_qdisc *rtnl_qdisc_alloc(void)
+{
+       return (struct rtnl_qdisc *) nl_object_alloc_from_ops(&rtnl_qdisc_ops);
+}
+
+/**
+ * Give back reference on rqdisc object.
+ * @arg qdisc          Qdisc object to be given back.
+ *
+ * Decrements the reference counter and frees the object if the
+ * last reference has been released.
+ */
+void rtnl_qdisc_put(struct rtnl_qdisc *qdisc)
+{
+       nl_object_put((struct nl_object *) qdisc);
+}
+/**
+ * Free qdisc object.
+ * @arg qdisc          Qdisc object to be freed.
+ *
+ * @note Always use rtnl_qdisc_put() unless you're absolutely sure
+ *       that no other user may have a reference on this object.
+ */
+void rtnl_qdisc_free(struct rtnl_qdisc *qdisc)
+{
+       nl_object_free((struct nl_object *) qdisc);
+}
+
+/** @} */
+
+/**
+ * @name Qdisc Cache Management
+ * @{
+ */
+
+/**
+ * Build a qdisc cache including all qdiscs currently configured in
+ * the kernel
+ * @arg handle         netlink handle
+ *
+ * Allocates a new cache, initializes it properly and updates it to
+ * include all qdiscs currently configured in the kernel.
+ *
+ * @note The caller is responsible for destroying and freeing the
+ *       cache after using it.
+ * @return The cache or NULL if an error has occured.
+ */
+struct nl_cache * rtnl_qdisc_alloc_cache(struct nl_handle *handle)
+{
+       struct nl_cache * cache;
+       
+       cache = nl_cache_alloc_from_ops(&rtnl_qdisc_ops);
+       if (cache == NULL)
+               return NULL;
+
+       if (nl_cache_update(handle, cache) < 0) {
+               nl_cache_free(cache);
+               return NULL;
+       }
+
+       return cache;
+}
+
+/**
+ * Look up qdisc by its parent in the provided cache
+ * @arg cache          qdisc cache
+ * @arg ifindex                interface the qdisc is attached to
+ * @arg parent         parent handle
+ * @return pointer to qdisc inside the cache or NULL if no match was found.
+ */
+struct rtnl_qdisc * rtnl_qdisc_get_by_parent(struct nl_cache *cache,
+                                            int ifindex, uint32_t parent)
+{
+       struct rtnl_qdisc *q;
+
+       if (cache->c_ops != &rtnl_qdisc_ops)
+               return NULL;
+
+       nl_list_for_each_entry(q, &cache->c_items, ce_list) {
+               if (q->q_parent == parent && q->q_ifindex == ifindex) {
+                       nl_object_get((struct nl_object *) q);
+                       return q;
+               }
+       }
+
+       return NULL;
+}
+
+/**
+ * Look up qdisc by its handle in the provided cache
+ * @arg cache          qdisc cache
+ * @arg ifindex                interface the qdisc is attached to
+ * @arg handle         qdisc handle
+ * @return pointer to qdisc inside the cache or NULL if no match was found.
+ */
+struct rtnl_qdisc * rtnl_qdisc_get(struct nl_cache *cache,
+                                  int ifindex, uint32_t handle)
+{
+       struct rtnl_qdisc *q;
+
+       if (cache->c_ops != &rtnl_qdisc_ops)
+               return NULL;
+
+       nl_list_for_each_entry(q, &cache->c_items, ce_list) {
+               if (q->q_handle == handle && q->q_ifindex == ifindex) {
+                       nl_object_get((struct nl_object *) q);
+                       return q;
+               }
+       }
+
+       return NULL;
+}
+
+/** @} */
+
+/**
+ * @name Qdisc Specific Options
+ * @{
+ */
+
+/**
+ * Return qdisc specific options for use in TCA_OPTIONS
+ * @arg qdisc          qdisc carrying the optiosn
+ * 
+ * @return new headerless netlink message carrying the options as payload
+ */
+struct nl_msg *rtnl_qdisc_get_opts(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_qdisc_ops *ops;
+
+       ops = qdisc_ops(qdisc);
+       if (ops && ops->qo_get_opts)
+               return ops->qo_get_opts(qdisc);
+
+       return NULL;
+}
+
+/** @} */
+
+/**
+ * @name Iterators
+ * @{
+ */
+
+/**
+ * Call a callback for each child class of a qdisc
+ * @arg qdisc          the parent qdisc
+ * @arg cache          a class cache including all classes of the interface
+ *                      the specified qdisc is attached to
+ * @arg cb              callback function
+ * @arg arg             argument to be passed to callback function
+ */
+void rtnl_qdisc_foreach_child(struct rtnl_qdisc *qdisc, struct nl_cache *cache,
+                             void (*cb)(struct nl_object *, void *), void *arg)
+{
+       struct rtnl_class *filter;
+       
+       filter = rtnl_class_alloc();
+       if (!filter)
+               return;
+
+       rtnl_class_set_parent(filter, qdisc->q_handle);
+       rtnl_class_set_ifindex(filter, qdisc->q_ifindex);
+       rtnl_class_set_kind(filter, qdisc->q_kind);
+
+       nl_cache_foreach_filter(cache, (struct nl_object *) filter, cb, arg);
+
+       rtnl_class_put(filter);
+}
+
+/**
+ * Call a callback for each filter attached to the qdisc
+ * @arg qdisc          the parent qdisc
+ * @arg cache          a filter cache including at least all the filters
+ *                      attached to the specified qdisc
+ * @arg cb              callback function
+ * @arg arg             argument to be passed to callback function
+ */
+void rtnl_qdisc_foreach_cls(struct rtnl_qdisc *qdisc, struct nl_cache *cache,
+                           void (*cb)(struct nl_object *, void *), void *arg)
+{
+       struct rtnl_cls *filter;
+
+       filter = rtnl_cls_alloc();
+       if (!filter)
+               return;
+
+       rtnl_cls_set_ifindex(filter, qdisc->q_ifindex);
+       rtnl_cls_set_parent(filter, qdisc->q_parent);
+
+       nl_cache_foreach_filter(cache, (struct nl_object *) filter, cb, arg);
+       rtnl_cls_put(filter);
+}
+
+/** @} */
+
+/**
+ * @name Attribute Modification
+ * @{
+ */
+
+/**
+ * Set the interface index of a qdisc to the specified value
+ * @arg qdisc          qdisc to be changed
+ * @arg ifindex                new interface index
+ */
+void rtnl_qdisc_set_ifindex(struct rtnl_qdisc *qdisc, int ifindex)
+{
+       tca_set_ifindex((struct rtnl_tca *) qdisc, ifindex);
+}
+
+/**
+ * Get the interface index of a qdisc
+ * @arg qdisc          qdisc handle
+ * @return Interface index or RTNL_LINK_NOT_FOUND if not set
+ */
+int rtnl_qdisc_get_ifindex(struct rtnl_qdisc *qdisc)
+{
+       return tca_get_ifindex((struct rtnl_tca *) qdisc);
+}
+
+/**
+ * Set the handle of a qdisc to the specified value
+ * @arg qdisc          qdisc to be changed
+ * @arg handle         new handle
+ */
+void rtnl_qdisc_set_handle(struct rtnl_qdisc *qdisc, uint32_t handle)
+{
+       tca_set_handle((struct rtnl_tca *) qdisc, handle);
+}
+
+/**
+ * Get the handle of a qdisc
+ * @arg qdisc          qdisc handle
+ * @return Handle or 0 if not set
+ */
+uint32_t rtnl_qdisc_get_handle(struct rtnl_qdisc *qdisc)
+{
+       return tca_get_handle((struct rtnl_tca *) qdisc);
+}
+
+/**
+ * Set the parent handle of a qdisc to the specified value
+ * @arg qdisc          qdisc to be changed
+ * @arg parent         new parent handle
+ */
+void rtnl_qdisc_set_parent(struct rtnl_qdisc *qdisc, uint32_t parent)
+{
+       tca_set_parent((struct rtnl_tca *) qdisc, parent);
+}
+
+/**
+ * Get the parent handle of a qdisc
+ * @arg qdisc          qdisc handle
+ * @return Parent handle or 0 if not set
+ */
+uint32_t rtnl_qdisc_get_parent(struct rtnl_qdisc *qdisc)
+{
+       return tca_get_parent((struct rtnl_tca *) qdisc);
+}
+
+/**
+ * Set the kind of a qdisc to the specified value
+ * @arg qdisc          qdisc to be changed
+ * @arg name           new kind name
+ */
+void rtnl_qdisc_set_kind(struct rtnl_qdisc *qdisc, const char *name)
+{
+       tca_set_kind((struct rtnl_tca *) qdisc, name);
+       qdisc->q_ops = qdisc_lookup_ops(name);
+}
+
+/**
+ * Get the kind of a qdisc
+ * @arg qdisc          qdisc handle
+ * @return Kind or NULL if not set
+ */
+char *rtnl_qdisc_get_kind(struct rtnl_qdisc *qdisc)
+{
+       return tca_get_kind((struct rtnl_tca *) qdisc);
+}
+
+/**
+ * Get the statistic specified by the id
+ * @arg qdisc          qdisc handle
+ * @arg id             statistic id
+ * @return The current counter of the specified statistic
+ */
+uint64_t rtnl_qdisc_get_stat(struct rtnl_qdisc *qdisc,
+                            enum rtnl_tc_stats_id id)
+{
+       return tca_get_stat((struct rtnl_tca *) qdisc, id);
+}
+
+/** @} */
+
+static struct nl_cache_ops rtnl_qdisc_ops = {
+       .co_name                = "route/qdisc",
+       .co_size                = sizeof(struct rtnl_qdisc),
+       .co_hdrsize             = sizeof(struct tcmsg),
+       .co_msgtypes            = {
+                                       { RTM_NEWQDISC, "new" },
+                                       { RTM_DELQDISC, "delete" },
+                                       { RTM_GETQDISC, "get" },
+                                       { -1, NULL },
+                                 },
+       .co_protocol            = NETLINK_ROUTE,
+       .co_request_update      = qdisc_request_update,
+       .co_msg_parser          = qdisc_msg_parser,
+       .co_free_data           = qdisc_free_data,
+       .co_dump[NL_DUMP_BRIEF] = qdisc_dump_brief,
+       .co_dump[NL_DUMP_FULL]  = qdisc_dump_full,
+       .co_dump[NL_DUMP_STATS] = qdisc_dump_stats,
+       .co_filter              = qdisc_filter,
+};
+
+static void __init qdisc_init(void)
+{
+       nl_cache_mngt_register(&rtnl_qdisc_ops);
+}
+
+static void __exit qdisc_exit(void)
+{
+       nl_cache_mngt_unregister(&rtnl_qdisc_ops);
+}
+
+/** @} */
diff --git a/lib/route/route.c b/lib/route/route.c
new file mode 100644 (file)
index 0000000..7602853
--- /dev/null
@@ -0,0 +1,1772 @@
+/*
+ * lib/route/route.c   Routes
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup rtnl
+ * @defgroup route Routing
+ * @brief
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink/netlink.h>
+#include <netlink/cache.h>
+#include <netlink/utils.h>
+#include <netlink/data.h>
+#include <netlink/route/rtnl.h>
+#include <netlink/route/route.h>
+#include <netlink/route/link.h>
+
+/** @cond SKIP */
+#define ROUTE_ATTR_FAMILY    0x000001
+#define ROUTE_ATTR_DST_LEN   0x000002
+#define ROUTE_ATTR_SRC_LEN   0x000004
+#define ROUTE_ATTR_TOS       0x000008
+#define ROUTE_ATTR_TABLE     0x000010
+#define ROUTE_ATTR_PROTOCOL  0x000020
+#define ROUTE_ATTR_SCOPE     0x000040
+#define ROUTE_ATTR_TYPE      0x000080
+#define ROUTE_ATTR_FLAGS     0x000100
+#define ROUTE_ATTR_DST       0x000200
+#define ROUTE_ATTR_SRC       0x000400
+#define ROUTE_ATTR_IIF       0x000800
+#define ROUTE_ATTR_OIF       0x001000
+#define ROUTE_ATTR_GATEWAY   0x002000
+#define ROUTE_ATTR_PRIO      0x004000
+#define ROUTE_ATTR_PREF_SRC  0x008000
+#define ROUTE_ATTR_METRICS   0x010000
+#define ROUTE_ATTR_MULTIPATH 0x020000
+#define ROUTE_ATTR_REALMS    0x040000
+#define ROUTE_ATTR_CACHEINFO 0x080000
+#define ROUTE_ATTR_MP_ALGO   0x100000
+
+#define NEXTHOP_HAS_FLAGS   0x000001
+#define NEXTHOP_HAS_WEIGHT  0x000002
+#define NEXTHOP_HAS_IFINDEX 0x000004
+#define NEXTHOP_HAS_GATEWAY 0x000008
+
+static struct nl_cache_ops rtnl_route_ops;
+/** @endcond */
+
+static void route_constructor(struct nl_object *c)
+{
+       struct rtnl_route *r = (struct rtnl_route *) c;
+
+       nl_init_list_head(&r->rt_nexthops);
+}
+
+static void route_free_data(struct nl_object *c)
+{
+       struct rtnl_route *r = (struct rtnl_route *) c;
+       struct rtnl_nexthop *nh, *tmp;
+
+       if (r == NULL)
+               return;
+
+       nl_addr_put(r->rt_dst);
+       nl_addr_put(r->rt_src);
+       nl_addr_put(r->rt_gateway);
+       nl_addr_put(r->rt_pref_src);
+
+       nl_list_for_each_entry_safe(nh, tmp, &r->rt_nexthops, rtnh_list) {
+               rtnl_route_remove_nexthop(nh);
+               rtnl_route_nh_free(nh);
+       }
+}
+
+static struct nla_policy route_policy[RTA_MAX+1] = {
+       [RTA_IIF]       = { .type = NLA_STRING,
+                           .maxlen = IFNAMSIZ, },
+       [RTA_OIF]       = { .type = NLA_U32 },
+       [RTA_PRIORITY]  = { .type = NLA_U32 },
+       [RTA_FLOW]      = { .type = NLA_U32 },
+       [RTA_MP_ALGO]   = { .type = NLA_U32 },
+       [RTA_CACHEINFO] = { .minlen = sizeof(struct rta_cacheinfo) },
+       [RTA_METRICS]   = { .type = NLA_NESTED },
+       [RTA_MULTIPATH] = { .type = NLA_NESTED },
+};
+
+static void copy_rtmsg_into_route(struct rtmsg *rtmsg, struct rtnl_route *route)
+{
+       route->rt_family        = rtmsg->rtm_family;
+       route->rt_dst_len       = rtmsg->rtm_dst_len;
+       route->rt_src_len       = rtmsg->rtm_src_len;
+       route->rt_tos           = rtmsg->rtm_tos;
+       route->rt_table         = rtmsg->rtm_table;
+       route->rt_type          = rtmsg->rtm_type;
+       route->rt_scope         = rtmsg->rtm_scope;
+       route->rt_protocol      = rtmsg->rtm_protocol;
+       route->rt_flags         = rtmsg->rtm_flags;
+
+       route->rt_mask = (ROUTE_ATTR_FAMILY | ROUTE_ATTR_DST_LEN | 
+                         ROUTE_ATTR_SRC_LEN | ROUTE_ATTR_TABLE |
+                         ROUTE_ATTR_PROTOCOL| ROUTE_ATTR_SCOPE |
+                         ROUTE_ATTR_TYPE | ROUTE_ATTR_FLAGS);
+
+       if (route->rt_tos)
+               route->rt_mask |= ROUTE_ATTR_TOS;
+}
+
+static void copy_cacheinfo_into_route(struct rta_cacheinfo *ci,
+                                     struct rtnl_route *route)
+{
+       route->rt_cacheinfo.rtci_clntref  = ci->rta_clntref;
+       route->rt_cacheinfo.rtci_last_use = ci->rta_lastuse;
+       route->rt_cacheinfo.rtci_expires  = ci->rta_expires;
+       route->rt_cacheinfo.rtci_error    = ci->rta_error;
+       route->rt_cacheinfo.rtci_used     = ci->rta_used;
+       route->rt_cacheinfo.rtci_id       = ci->rta_id;
+       route->rt_cacheinfo.rtci_ts       = ci->rta_ts;
+       route->rt_cacheinfo.rtci_tsage    = ci->rta_tsage;
+
+       route->rt_mask |= ROUTE_ATTR_CACHEINFO;
+}
+
+static int route_msg_parser(struct sockaddr_nl *who, struct nlmsghdr *nlh,
+                           void *arg)
+{
+       struct nl_parser_param *pp = arg;
+       struct rtnl_route *route;
+       struct nlattr *tb[RTA_MAX + 1];
+       int err;
+
+       route = rtnl_route_alloc();
+       if (!route) {
+               err = nl_errno(ENOMEM);
+               goto errout;
+       }
+
+       route->ce_msgtype = nlh->nlmsg_type;
+
+       err = nlmsg_parse(nlh, sizeof(struct rtmsg), tb, RTA_MAX,
+                         route_policy);
+       if (err < 0)
+               goto errout;
+
+       copy_rtmsg_into_route((struct rtmsg *) nlmsg_data(nlh), route);
+
+       if (tb[RTA_DST]) {
+               route->rt_dst = nla_get_addr(tb[RTA_DST], route->rt_family);
+               if (!route->rt_dst)
+                       goto errout_errno;
+               nl_addr_set_prefixlen(route->rt_dst, route->rt_dst_len);
+               route->rt_mask |= ROUTE_ATTR_DST;
+       }
+
+       if (tb[RTA_SRC]) {
+               route->rt_src = nla_get_addr(tb[RTA_SRC], route->rt_family);
+               if (!route->rt_src)
+                       goto errout_errno;
+               nl_addr_set_prefixlen(route->rt_src, route->rt_src_len);
+               route->rt_mask |= ROUTE_ATTR_SRC;
+       }
+
+       if (tb[RTA_IIF]) {
+               nla_strlcpy(route->rt_iif, tb[RTA_IIF], IFNAMSIZ);
+               route->rt_mask |= ROUTE_ATTR_IIF;
+       }
+
+       if (tb[RTA_OIF]) {
+               route->rt_oif = nla_get_u32(tb[RTA_OIF]);
+               route->rt_mask |= ROUTE_ATTR_OIF;
+       }
+
+       if (tb[RTA_GATEWAY]) {
+               route->rt_gateway = nla_get_addr(tb[RTA_GATEWAY],
+                                                route->rt_family);
+               if (!route->rt_gateway)
+                       goto errout_errno;
+               route->rt_mask |= ROUTE_ATTR_GATEWAY;
+       }
+
+       if (tb[RTA_PRIORITY]) {
+               route->rt_prio = nla_get_u32(tb[RTA_PRIORITY]);
+               route->rt_mask |= ROUTE_ATTR_PRIO;
+       }
+
+       if (tb[RTA_PREFSRC]) {
+               route->rt_pref_src = nla_get_addr(tb[RTA_PREFSRC],
+                                                 route->rt_family);
+               if (!route->rt_pref_src)
+                       goto errout_errno;
+               route->rt_mask |= ROUTE_ATTR_PREF_SRC;
+       }
+
+       if (tb[RTA_METRICS]) {
+               struct nlattr *mtb[RTAX_MAX + 1];
+               int i;
+
+               err = nla_parse_nested(mtb, RTAX_MAX, tb[RTA_METRICS], NULL);
+               if (err < 0)
+                       goto errout;
+
+               for (i = 1; i <= RTAX_MAX; i++) {
+                       if (mtb[i] && nla_len(mtb[i]) >= sizeof(uint32_t)) {
+                               uint32_t m = nla_get_u32(mtb[i]);
+                               route->rt_metrics[i-1] = m;
+                               route->rt_metrics_mask |= (1 << (i - 1));
+                       }
+               }
+               
+               route->rt_mask |= ROUTE_ATTR_METRICS;
+       }
+
+       if (tb[RTA_MULTIPATH]) {
+               struct rtnl_nexthop *nh;
+               struct rtnexthop *rtnh = nla_data(tb[RTA_MULTIPATH]);
+               size_t tlen = nla_len(tb[RTA_MULTIPATH]);
+
+               while (tlen >= sizeof(*rtnh) && tlen >= rtnh->rtnh_len) {
+                       nh = rtnl_route_nh_alloc();
+                       if (!nh)
+                               goto errout;
+
+                       rtnl_route_nh_set_weight(nh, rtnh->rtnh_hops);
+                       rtnl_route_nh_set_ifindex(nh, rtnh->rtnh_ifindex);
+                       rtnl_route_nh_set_flags(nh, rtnh->rtnh_flags);
+
+                       if (rtnh->rtnh_len > sizeof(*rtnh)) {
+                               struct nlattr *ntb[RTA_MAX + 1];
+                               nla_parse(ntb, RTA_MAX, (struct nlattr *)
+                                         RTNH_DATA(rtnh),
+                                         rtnh->rtnh_len - sizeof(*rtnh),
+                                         route_policy);
+
+                               if (ntb[RTA_GATEWAY]) {
+                                       nh->rtnh_gateway = nla_get_addr(
+                                                       ntb[RTA_GATEWAY],
+                                                       route->rt_family);
+                                       nh->rtnh_mask = NEXTHOP_HAS_GATEWAY;
+                               }
+                       }
+
+                       rtnl_route_add_nexthop(route, nh);
+                       tlen -= RTNH_ALIGN(rtnh->rtnh_len);
+                       rtnh = RTNH_NEXT(rtnh);
+               }
+       }
+
+       if (tb[RTA_FLOW]) {
+               route->rt_realms = nla_get_u32(tb[RTA_FLOW]);
+               route->rt_mask |= ROUTE_ATTR_REALMS;
+       }
+
+       if (tb[RTA_CACHEINFO])
+               copy_cacheinfo_into_route(nla_data(tb[RTA_CACHEINFO]), route);
+
+       if (tb[RTA_MP_ALGO]) {
+               route->rt_mp_algo = nla_get_u32(tb[RTA_MP_ALGO]);
+               route->rt_mask |= ROUTE_ATTR_MP_ALGO;
+       }
+
+       err = pp->pp_cb((struct nl_object *) route, pp);
+       if (err < 0)
+               goto errout;
+
+       return P_ACCEPT;
+
+errout_errno:
+       err = nl_get_errno();
+errout:
+       rtnl_route_put(route);
+       return err;
+
+}
+
+static int route_request_update(struct nl_cache *c, struct nl_handle *h)
+{
+       return nl_rtgen_request(h, RTM_GETROUTE, AF_UNSPEC, NLM_F_DUMP);
+}
+
+static int route_dump_brief(struct nl_object *a, struct nl_dump_params *p)
+{
+       struct rtnl_route *r = (struct rtnl_route *) a;
+       struct nl_cache *link_cache;
+       char buf[64];
+
+       link_cache = nl_cache_mngt_require("route/link");
+
+       if (r->rt_mask & ROUTE_ATTR_DST)
+               dp_dump(p, "%s ", nl_addr2str(r->rt_dst, buf, sizeof(buf)));
+       else if (r->rt_dst_len)
+               dp_dump(p, "0/%u ", r->rt_dst_len);
+       else
+               dp_dump(p, "default ");
+
+       if (r->rt_mask & ROUTE_ATTR_OIF) {
+               if (link_cache)
+                       dp_dump(p, "dev %s ",
+                               rtnl_link_i2name(link_cache, r->rt_oif,
+                                                buf, sizeof(buf)));
+               else
+                       dp_dump(p, "dev %d ", r->rt_oif);
+       }
+
+       if (r->rt_mask & ROUTE_ATTR_GATEWAY)
+               dp_dump(p, "via %s ", nl_addr2str(r->rt_gateway, buf,
+                                                 sizeof(buf)));
+       else if (r->rt_mask & ROUTE_ATTR_MULTIPATH)
+               dp_dump(p, "via nexthops ");
+
+       if (r->rt_mask & ROUTE_ATTR_TABLE)
+               dp_dump(p, "table %s ",
+                       rtnl_route_table2str(r->rt_table, buf, sizeof(buf)));
+
+       if (r->rt_mask & ROUTE_ATTR_SCOPE)
+               dp_dump(p, "scope %s ",
+                       rtnl_scope2str(r->rt_scope, buf, sizeof(buf)));
+
+       if (r->rt_mask & ROUTE_ATTR_FLAGS && r->rt_flags) {
+               int flags = r->rt_flags;
+
+               dp_dump(p, "<");
+               
+#define PRINT_FLAG(f) if (flags & RTNH_F_##f) { \
+               flags &= ~RTNH_F_##f; dp_dump(p, #f "%s", flags ? "," : ""); }
+               PRINT_FLAG(DEAD);
+               PRINT_FLAG(ONLINK);
+               PRINT_FLAG(PERVASIVE);
+#undef PRINT_FLAG
+
+#define PRINT_FLAG(f) if (flags & RTM_F_##f) { \
+               flags &= ~RTM_F_##f; dp_dump(p, #f "%s", flags ? "," : ""); }
+               PRINT_FLAG(NOTIFY);
+               PRINT_FLAG(CLONED);
+               PRINT_FLAG(EQUALIZE);
+               PRINT_FLAG(PREFIX);
+#undef PRINT_FLAG
+
+               dp_dump(p, ">");
+       }
+
+       dp_dump(p, "\n");
+
+       return 1;
+}
+
+static int route_dump_full(struct nl_object *a, struct nl_dump_params *p)
+{
+       struct rtnl_route *r = (struct rtnl_route *) a;
+       struct nl_cache *link_cache;
+       char buf[128];
+       int i, line;
+
+       link_cache = nl_cache_mngt_require("route/link");
+       line = route_dump_brief(a, p);
+
+       if (r->rt_mask & ROUTE_ATTR_MULTIPATH) {
+               struct rtnl_nexthop *nh;
+
+               dp_dump_line(p, line++, "  ");
+
+               nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
+                       dp_dump(p, "nh ");
+
+                       if (nh->rtnh_mask & NEXTHOP_HAS_GATEWAY)
+                               dp_dump(p, "via %s ",
+                                       nl_addr2str(nh->rtnh_gateway,
+                                                   buf, sizeof(buf)));
+                       if (link_cache)
+                               dp_dump(p, "dev %s ",
+                                       rtnl_link_i2name(link_cache,
+                                                        nh->rtnh_ifindex,
+                                                        buf, sizeof(buf)));
+                       else
+                               dp_dump(p, "dev %d ", nh->rtnh_ifindex);
+
+                       dp_dump(p, "weight %u <%s> ", nh->rtnh_weight,
+                               rtnl_route_nh_flags2str(nh->rtnh_flags,
+                                                       buf, sizeof(buf)));
+               }
+
+               dp_dump(p, "\n");
+       }
+
+       dp_dump_line(p, line++, "  ");
+
+       if (r->rt_mask & ROUTE_ATTR_PREF_SRC)
+               dp_dump(p, "preferred-src %s ",
+                       nl_addr2str(r->rt_pref_src, buf, sizeof(buf)));
+
+       if (r->rt_mask & ROUTE_ATTR_TYPE)
+               dp_dump(p, "type %s ",
+                       nl_rtntype2str(r->rt_type, buf, sizeof(buf)));
+
+       if (r->rt_mask & ROUTE_ATTR_PRIO)
+               dp_dump(p, "metric %#x ", r->rt_prio);
+
+       if (r->rt_mask & ROUTE_ATTR_FAMILY)
+               dp_dump(p, "family %s ",
+                       nl_af2str(r->rt_family, buf, sizeof(buf)));
+
+       if (r->rt_mask & ROUTE_ATTR_PROTOCOL)
+               dp_dump(p, "protocol %s ",
+                       rtnl_route_proto2str(r->rt_protocol, buf, sizeof(buf)));
+
+       dp_dump(p, "\n");
+
+       if ((r->rt_mask & (ROUTE_ATTR_IIF | ROUTE_ATTR_SRC | ROUTE_ATTR_TOS |
+                          ROUTE_ATTR_REALMS)) || r->rt_src_len ||
+           ((r->rt_mask & ROUTE_ATTR_CACHEINFO) &&
+            r->rt_cacheinfo.rtci_error)) {
+               dp_dump_line(p, line++, "  ");
+
+               if (r->rt_mask & ROUTE_ATTR_IIF)
+                       dp_dump(p, "iif %s ", r->rt_iif);
+
+               if (r->rt_mask & ROUTE_ATTR_SRC)
+                       dp_dump(p, "src %s ",
+                               nl_addr2str(r->rt_src, buf, sizeof(buf)));
+               else if (r->rt_src_len)
+                       dp_dump(p, "src 0/%u ", r->rt_src_len);
+
+               if (r->rt_mask & ROUTE_ATTR_TOS)
+                       dp_dump(p, "tos %#x ", r->rt_tos);
+
+               if (r->rt_mask & ROUTE_ATTR_REALMS)
+                       dp_dump(p, "realm %04x:%04x ",
+                               RTNL_REALM_FROM(r->rt_realms),
+                               RTNL_REALM_TO(r->rt_realms));
+
+               if ((r->rt_mask & ROUTE_ATTR_CACHEINFO) &&
+                   r->rt_cacheinfo.rtci_error)
+                       dp_dump(p, "error %d (%s) ", r->rt_cacheinfo.rtci_error,
+                               strerror(-r->rt_cacheinfo.rtci_error));
+
+               dp_dump(p, "\n");
+       }
+
+       if (r->rt_mask & ROUTE_ATTR_METRICS) {
+               dp_dump_line(p, line++, "  ");
+               for (i = 0; i < RTAX_MAX; i++)
+                       if (r->rt_metrics_mask & (1 << i))
+                               dp_dump(p, "%s %u ",
+                                       rtnl_route_metric2str(i+1,
+                                                             buf, sizeof(buf)),
+                                       r->rt_metrics[i]);
+               dp_dump(p, "\n");
+       }
+
+       return line;
+}
+
+static int route_dump_stats(struct nl_object *obj, struct nl_dump_params *p)
+{
+       struct rtnl_route *route = (struct rtnl_route *) obj;
+       int line;
+
+       line = route_dump_full(obj, p);
+
+       if (route->rt_mask & ROUTE_ATTR_CACHEINFO) {
+               struct rtnl_rtcacheinfo *ci = &route->rt_cacheinfo;
+               dp_dump_line(p, line++, "  used %u refcnt %u ",
+                            ci->rtci_used, ci->rtci_clntref);
+               dp_dump_line(p, line++, "last-use %us expires %us\n",
+                            ci->rtci_last_use / nl_get_hz(),
+                            ci->rtci_expires / nl_get_hz());
+       }
+
+       return line;
+}
+
+static int route_dump_xml(struct nl_object *obj, struct nl_dump_params *p)
+{
+       struct rtnl_route *route = (struct rtnl_route *) obj;
+       char buf[128];
+       int line = 0;
+       
+       dp_dump_line(p, line++, "<route>\n");
+       dp_dump_line(p, line++, "  <family>%s</family>\n",
+                    nl_af2str(route->rt_family, buf, sizeof(buf)));
+
+       if (route->rt_mask & ROUTE_ATTR_DST)
+               dp_dump_line(p, line++, "  <dst>%s</dst>\n",
+                            nl_addr2str(route->rt_dst, buf, sizeof(buf)));
+
+       if (route->rt_mask & ROUTE_ATTR_DST_LEN)
+               dp_dump_line(p, line++, "  <dstlen>%u</dstlen>\n",
+                            route->rt_dst_len);
+
+       if (route->rt_mask & ROUTE_ATTR_SRC)
+               dp_dump_line(p, line++, "  <src>%s</src>\n",
+                            nl_addr2str(route->rt_src, buf, sizeof(buf)));
+
+       if (route->rt_mask & ROUTE_ATTR_SRC_LEN)
+               dp_dump_line(p, line++, "  <srclen>%u</srclen>\n",
+                            route->rt_src_len);
+
+       if (route->rt_mask & ROUTE_ATTR_GATEWAY)
+               dp_dump_line(p, line++, "  <gateway>%s</gateway>\n",
+                            nl_addr2str(route->rt_gateway, buf, sizeof(buf)));
+
+       if (route->rt_mask & ROUTE_ATTR_PREF_SRC)
+               dp_dump_line(p, line++, "  <prefsrc>%s</prefsrc>\n",
+                            nl_addr2str(route->rt_pref_src, buf, sizeof(buf)));
+
+       if (route->rt_mask & ROUTE_ATTR_IIF)
+               dp_dump_line(p, line++, "  <iif>%s</iif>\n", route->rt_iif);
+
+       if (route->rt_mask & ROUTE_ATTR_REALMS)
+               dp_dump_line(p, line++, "  <realms>%u</realms>\n",
+                            route->rt_realms);
+
+       if (route->rt_mask & ROUTE_ATTR_TOS)
+               dp_dump_line(p, line++, "  <tos>%u</tos>\n", route->rt_tos);
+
+       if (route->rt_mask & ROUTE_ATTR_TABLE)
+               dp_dump_line(p, line++, "  <table>%u</table>\n",
+                            route->rt_table);
+
+       if (route->rt_mask & ROUTE_ATTR_SCOPE)
+               dp_dump_line(p, line++, "  <scope>%s</scope>\n",
+                            rtnl_scope2str(route->rt_scope, buf, sizeof(buf)));
+
+       if (route->rt_mask & ROUTE_ATTR_PRIO)
+               dp_dump_line(p, line++, "  <metric>%u</metric>\n",
+                            route->rt_prio);
+
+       if (route->rt_mask & ROUTE_ATTR_OIF) {
+               struct nl_cache *link_cache;
+       
+               link_cache = nl_cache_mngt_require("route/link");
+               if (link_cache)
+                       dp_dump_line(p, line++, "  <oif>%s</oif>\n",
+                                    rtnl_link_i2name(link_cache,
+                                                     route->rt_oif,
+                                                     buf, sizeof(buf)));
+               else
+                       dp_dump_line(p, line++, "  <oif>%u</oif>\n",
+                                    route->rt_oif);
+       }
+
+       if (route->rt_mask & ROUTE_ATTR_TYPE)
+               dp_dump_line(p, line++, "  <type>%s</type>\n",
+                            nl_rtntype2str(route->rt_type, buf, sizeof(buf)));
+
+       dp_dump_line(p, line++, "</route>\n");
+
+#if 0
+       uint8_t                 rt_protocol;
+       uint32_t                rt_flags;
+       uint32_t                rt_metrics[RTAX_MAX];
+       uint32_t                rt_metrics_mask;
+       struct rtnl_nexthop *   rt_nexthops;
+       struct rtnl_rtcacheinfo rt_cacheinfo;
+       uint32_t                rt_mp_algo;
+
+#endif
+
+       return line;
+}
+
+static int route_dump_env(struct nl_object *obj, struct nl_dump_params *p)
+{
+       struct rtnl_route *route = (struct rtnl_route *) obj;
+       char buf[128];
+       int line = 0;
+
+       dp_dump_line(p, line++, "ROUTE_FAMILY=%s\n",
+                    nl_af2str(route->rt_family, buf, sizeof(buf)));
+
+       if (route->rt_mask & ROUTE_ATTR_DST)
+               dp_dump_line(p, line++, "ROUTE_DST=%s\n",
+                            nl_addr2str(route->rt_dst, buf, sizeof(buf)));
+
+       if (route->rt_mask & ROUTE_ATTR_DST_LEN)
+               dp_dump_line(p, line++, "ROUTE_DSTLEN=%u\n",
+                            route->rt_dst_len);
+
+       if (route->rt_mask & ROUTE_ATTR_SRC)
+               dp_dump_line(p, line++, "ROUTE_SRC=%s\n",
+                            nl_addr2str(route->rt_src, buf, sizeof(buf)));
+
+       if (route->rt_mask & ROUTE_ATTR_SRC_LEN)
+               dp_dump_line(p, line++, "ROUTE_SRCLEN=%u\n",
+                            route->rt_src_len);
+
+       if (route->rt_mask & ROUTE_ATTR_GATEWAY)
+               dp_dump_line(p, line++, "ROUTE_GATEWAY=%s\n",
+                            nl_addr2str(route->rt_gateway, buf, sizeof(buf)));
+
+       if (route->rt_mask & ROUTE_ATTR_PREF_SRC)
+               dp_dump_line(p, line++, "ROUTE_PREFSRC=%s\n",
+                            nl_addr2str(route->rt_pref_src, buf, sizeof(buf)));
+
+       if (route->rt_mask & ROUTE_ATTR_IIF)
+               dp_dump_line(p, line++, "ROUTE_IIF=%s\n", route->rt_iif);
+
+       if (route->rt_mask & ROUTE_ATTR_REALMS)
+               dp_dump_line(p, line++, "ROUTE_REALM=%u\n",
+                            route->rt_realms);
+
+       if (route->rt_mask & ROUTE_ATTR_TOS)
+               dp_dump_line(p, line++, "ROUTE_TOS=%u\n", route->rt_tos);
+
+       if (route->rt_mask & ROUTE_ATTR_TABLE)
+               dp_dump_line(p, line++, "ROUTE_TABLE=%u\n",
+                            route->rt_table);
+
+       if (route->rt_mask & ROUTE_ATTR_SCOPE)
+               dp_dump_line(p, line++, "ROUTE_SCOPE=%s\n",
+                            rtnl_scope2str(route->rt_scope, buf, sizeof(buf)));
+
+       if (route->rt_mask & ROUTE_ATTR_PRIO)
+               dp_dump_line(p, line++, "ROUTE_METRIC=%u\n",
+                            route->rt_prio);
+
+       if (route->rt_mask & ROUTE_ATTR_OIF) {
+               struct nl_cache *link_cache;
+
+               dp_dump_line(p, line++, "ROUTE_OIF_IFINDEX=%u\n",
+                            route->rt_oif);
+
+               link_cache = nl_cache_mngt_require("route/link");
+               if (link_cache)
+                       dp_dump_line(p, line++, "ROUTE_OIF_IFNAME=%s\n",
+                                    rtnl_link_i2name(link_cache,
+                                                     route->rt_oif,
+                                                     buf, sizeof(buf)));
+       }
+
+       if (route->rt_mask & ROUTE_ATTR_TYPE)
+               dp_dump_line(p, line++, "ROUTE_TYPE=%s\n",
+                            nl_rtntype2str(route->rt_type, buf, sizeof(buf)));
+
+       return line;
+}
+
+static int route_filter(struct nl_object *obj, struct nl_object *filter)
+{
+       struct rtnl_route *o = (struct rtnl_route *) obj;
+       struct rtnl_route *f = (struct rtnl_route *) filter;
+
+#define REQ(F) (f->rt_mask & ROUTE_ATTR_##F)
+#define AVAIL(F) (o->rt_mask & ROUTE_ATTR_##F)
+#define _O(F, EXPR) (REQ(F) && (!AVAIL(F) || (EXPR)))
+#define _C(F, N) (REQ(F) && (!AVAIL(F) || (o->N != f->N)))
+       if (_C(FAMILY,    rt_family)                                    ||
+           _C(DST_LEN,   rt_dst_len)                                   ||
+           _C(SRC_LEN,   rt_src_len)                                   ||
+           _C(TOS,       rt_tos)                                       ||
+           _C(TABLE,     rt_table)                                     ||
+           _C(PROTOCOL,  rt_protocol)                                  ||
+           _C(SCOPE,     rt_scope)                                     ||
+           _C(TYPE,      rt_type)                                      ||
+           _C(OIF,       rt_oif)                                       ||
+           _C(PRIO,      rt_prio)                                      ||
+           _C(REALMS,    rt_realms)                                    ||
+           _C(MP_ALGO,   rt_mp_algo)                                   ||
+           _O(DST,       nl_addr_cmp(o->rt_dst, f->rt_dst))            ||
+           _O(SRC,       nl_addr_cmp(o->rt_src, f->rt_src))            ||
+           _O(IIF,       strcmp(o->rt_iif, f->rt_iif))                 ||
+           _O(PREF_SRC,  nl_addr_cmp(o->rt_pref_src, f->rt_pref_src))  ||
+           _O(GATEWAY,   nl_addr_cmp(o->rt_gateway, f->rt_gateway))    ||
+           _O(FLAGS,     f->rt_flags ^ (o->rt_flags & f->rt_flag_mask)))
+               return 0;
+
+       if (REQ(METRICS)) {
+               int i;
+
+               if (!AVAIL(METRICS))
+                       return 0;
+
+               for (i = 0; i < RTAX_MAX; i++) {
+                       if (f->rt_metrics_mask & (1 << i)) {
+                               if (!(o->rt_metrics_mask & (1 << i)) ||
+                                   f->rt_metrics[i+1] != o->rt_metrics[i+1])
+                                       return 0;
+                       }
+               }
+       }
+
+       if (REQ(MULTIPATH)) {
+               /* FIXME */
+       }
+       
+#undef REQ
+#undef AVAIL
+#undef _O
+#undef _C
+
+       return 1;
+
+}
+
+/**
+ * @name Route Object Allocation/Freeage
+ * @{
+ */
+
+/**
+ * Allocate a new route object
+ * @return New route object
+ */
+struct rtnl_route *rtnl_route_alloc(void)
+{
+       return (struct rtnl_route *) nl_object_alloc_from_ops(&rtnl_route_ops);
+}
+
+/**
+ * Free route object.
+ * @arg route          Route object to be freed.
+ *
+ * @note Always use rtnl_route_put() unless you're absolutely sure
+ *       that no other user may have a reference on this object.
+ */
+void rtnl_route_free(struct rtnl_route *route)
+{
+       nl_object_free((struct nl_object *) route);
+}
+
+/** @} */
+
+/**
+ * @name Route Object Reference Counting
+ * @{
+ */
+
+void rtnl_route_get(struct rtnl_route *route)
+{
+       nl_object_get((struct nl_object *) route);
+}
+
+void rtnl_route_put(struct rtnl_route *route)
+{
+       nl_object_put((struct nl_object *) route);
+}
+
+/** @} */
+
+/**
+ * @name Route Cache Management
+ * @{
+ */
+
+/**
+ * Build a route cache holding all routes currently configured in the kernel
+ * @arg handle         netlink handle
+ *
+ * Allocates a new cache, initializes it properly and updates it to
+ * contain all routes currently configured in the kernel.
+ *
+ * @note The caller is responsible for destroying and freeing the
+ *       cache after using it.
+ * @return The cache or NULL if an error has occured.
+ */
+struct nl_cache *rtnl_route_alloc_cache(struct nl_handle *handle)
+{
+       struct nl_cache *cache = nl_cache_alloc_from_ops(&rtnl_route_ops);
+
+       if (!cache)
+               return NULL;
+
+       if (nl_cache_update(handle, cache) < 0) {
+               free(cache);
+               return NULL;
+       }
+
+       return cache;
+}
+
+/** @} */
+
+/**
+ * @name Route Addition
+ * @{
+ */
+
+static struct nl_msg *build_route_msg(struct rtnl_route *tmpl, int cmd,
+                                     int flags)
+{
+#if 0
+       struct nl_msg *msg;
+       struct rtmsg rtmsg = {
+               .rtm_family = tmpl->rt_family,
+       };
+       route->rt_dst_len       = rtmsg->rtm_dst_len;
+       route->rt_src_len       = rtmsg->rtm_src_len;
+       route->rt_tos           = rtmsg->rtm_tos;
+       route->rt_table         = rtmsg->rtm_table;
+       route->rt_type          = rtmsg->rtm_type;
+       route->rt_scope         = rtmsg->rtm_scope;
+       route->rt_protocol      = rtmsg->rtm_protocol;
+       route->rt_flags         = rtmsg->rtm_flags;
+
+       route->rt_mask = (ROUTE_ATTR_FAMILY | ROUTE_ATTR_DST_LEN | 
+                         ROUTE_ATTR_SRC_LEN | ROUTE_ATTR_TABLE |
+                         ROUTE_ATTR_PROTOCOL| ROUTE_ATTR_SCOPE |
+                         ROUTE_ATTR_TYPE | ROUTE_ATTR_FLAGS);
+
+       msg = nlmsg_build_simple(cmd, flags);
+       if (!msg)
+               return NULL;
+
+       if (nlmsg_append(msg, &rtmsg, sizeof(rtmsg), 1) < 0)
+               goto nla_put_failure;
+
+       NLA_PUT_ADDR(msg, NDA_DST, tmpl->n_dst);
+
+       if (tmpl->n_mask & NEIGH_ATTR_LLADDR)
+               NLA_PUT_ADDR(msg, NDA_LLADDR, tmpl->n_lladdr);
+
+       return msg;
+
+nla_put_failure:
+       nlmsg_free(msg);
+       return NULL;
+#endif
+
+       return NULL;
+}
+
+/**
+ * Build netlink request message to add a new route
+ * @arg tmpl           template with data of new route
+ * @arg flags          additional netlink message flags
+ *
+ * Builds a new netlink message requesting a addition of a new route.
+ * The netlink message header isn't fully equipped with all relevant
+ * fields and must thus be sent out via nl_send_auto_complete() or
+ * supplemented as needed. \a tmpl must contain the attributes of the
+ * new route set via \c rtnl_route_set_* functions.
+ * 
+ * The following attributes must be set in the template:
+ *  - Interface index (rtnl_neigh_set_ifindex())
+ *  - State (rtnl_neigh_set_state())
+ *  - Destination address (rtnl_neigh_set_dst())
+ *  - Link layer address (rtnl_neigh_set_lladdr())
+ *
+ * @return The netlink message
+ */
+struct nl_msg *rtnl_route_build_add_request(struct rtnl_route *tmpl, int flags)
+{
+       return build_route_msg(tmpl, RTM_NEWROUTE, NLM_F_CREATE | flags);
+}
+
+/** @} */
+/**
+ * @name Attribute: Routing Table
+ * @{
+ */
+
+/**
+ * Set the table of a route to the specified value
+ * @arg route          route to be changed
+ * @arg table          new table value
+ */
+void rtnl_route_set_table(struct rtnl_route *route, int table)
+{
+       route->rt_table = table;
+       route->rt_mask |= ROUTE_ATTR_TABLE;
+}
+
+/**
+ * Get the table of a route
+ * @arg route          route handle
+ * @return Table id or -1 if not set
+ */
+int rtnl_route_get_table(struct rtnl_route *route)
+{
+       if (route->rt_mask & ROUTE_ATTR_TABLE)
+               return route->rt_table;
+       else
+               return -1;
+}
+
+/** @} */
+/**
+ * @name Attribute: Scope
+ * @{
+ */
+
+/**
+ * Set the scope of a route to the specified value
+ * @arg route          route to be changed
+ * @arg scope          new scope
+ */
+void rtnl_route_set_scope(struct rtnl_route *route, int scope)
+{
+       route->rt_scope = scope;
+       route->rt_mask |= ROUTE_ATTR_SCOPE;
+}
+
+/**
+ * Get the scope of a route
+ * @arg route          route handle
+ * @return Scope or -1 if not set
+ */
+int rtnl_route_get_scope(struct rtnl_route *route)
+{
+       if (route->rt_mask & ROUTE_ATTR_SCOPE)
+               return route->rt_scope;
+       else
+               return -1;
+}
+
+/** @} */
+/**
+ * @name Attribute: Type Of Service
+ * @{
+ */
+
+/**
+ * Set the TOS of a route to the specified value
+ * @arg route          route to be changed
+ * @arg tos            new TOS value
+ */
+void rtnl_route_set_tos(struct rtnl_route *route, int tos)
+{
+       route->rt_tos = tos;
+       route->rt_mask |= ROUTE_ATTR_TOS;
+}
+
+/**
+ * Get the TOS of a route
+ * @arg route          route handle
+ * @return TOS value or -1 if not set
+ */
+int rtnl_route_get_tos(struct rtnl_route *route)
+{
+       if (route->rt_mask & ROUTE_ATTR_TOS)
+               return route->rt_tos;
+       else
+               return -1;
+}
+
+/** @} */
+/**
+ * @name Attribute: Realm
+ * @{
+ */
+
+/**
+ * Set the realms of a route to the specified value
+ * @arg route          route to be changed
+ * @arg realms         New realms value.
+ */
+void rtnl_route_set_realms(struct rtnl_route *route, realm_t realms)
+{
+       route->rt_realms = realms;
+       route->rt_mask |= ROUTE_ATTR_REALMS;
+}
+
+/**
+ * Get realms of route object.
+ * @arg route          Route object.
+ * @return Realms value or 0 if not set.
+ */
+realm_t rtnl_route_get_realms(struct rtnl_route *route)
+{
+       if (route->rt_mask & ROUTE_ATTR_REALMS)
+               return route->rt_realms;
+       else
+               return 0;
+}
+
+/** @} */
+/**
+ * @name Attribute: Routing Protocol
+ * @{
+ */
+
+/**
+ * Set the protocol of a route to the specified value
+ * @arg route          route to be changed
+ * @arg proto          new protocol
+ */
+void rtnl_route_set_protocol(struct rtnl_route *route, int proto)
+{
+       route->rt_protocol = proto;
+       route->rt_mask |= ROUTE_ATTR_PROTOCOL;
+}
+
+/**
+ * Get the protocol of a route
+ * @arg route          route handle
+ * @return Protocol number or -1 if not set
+ */
+int rtnl_route_get_protocol(struct rtnl_route *route)
+{
+       if (route->rt_mask & ROUTE_ATTR_PROTOCOL)
+               return route->rt_protocol;
+       else
+               return -1;
+}
+
+/** @} */
+/**
+ * @name Attribute: Priority/Metric
+ * @{
+ */
+
+/**
+ * Set the priority of a route to the specified value
+ * @arg route          route to be changed
+ * @arg prio           new priority
+ */
+void rtnl_route_set_prio(struct rtnl_route *route, int prio)
+{
+       route->rt_prio = prio;
+       route->rt_mask |= ROUTE_ATTR_PRIO;
+}
+
+/**
+ * Get the priority of a route
+ * @arg route          route handle
+ * @return Priority or -1 if not set
+ */
+int rtnl_route_get_prio(struct rtnl_route *route)
+{
+       if (route->rt_mask & ROUTE_ATTR_PRIO)
+               return route->rt_prio;
+       else
+               return -1;
+}
+
+/** @} */
+/**
+ * @name Attribute: Address Family
+ * @{
+ */
+
+/**
+ * Set the address family of a route to the specified value
+ * @arg route          route to be changed
+ * @arg family         new address family
+ */
+void rtnl_route_set_family(struct rtnl_route *route, int family)
+{
+       route->rt_family = family;
+       route->rt_mask |= ROUTE_ATTR_FAMILY;
+}
+
+/**
+ * Get the address family of a route
+ * @arg route          route handle
+ * @return Address family or AF_UNSPEC if not set
+ */
+int rtnl_route_get_family(struct rtnl_route *route)
+{
+       if (route->rt_mask & ROUTE_ATTR_FAMILY)
+               return route->rt_family;
+       else
+               return AF_UNSPEC;
+}
+
+/** @} */
+/**
+ * @name Attribute: Destination Address
+ * @{
+ */
+
+/**
+ * Set the destination address of a route to the specified address
+ * @arg route          route to be changed
+ * @arg addr           new destination address
+ *
+ * Assigns the new destination address to the specified \a route,
+ * overwrites the destination address length (rtnl_route::rt_dst_len),
+ * and sets the route's address family to the new address's family if
+ * it is not set already.
+ *
+ * If a address family has been specified already via either calling
+ * rtnl_route_set_family() or by setting one of the other addresses,
+ * the specified \a addr is automatically validated against this family
+ * and the assignment fails in case of a mismatch.
+ * 
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_route_set_dst(struct rtnl_route *route, struct nl_addr *addr)
+{
+       if (route->rt_mask & ROUTE_ATTR_FAMILY) {
+               if (addr->a_family != route->rt_family)
+                       return nl_error(EINVAL, "Address family mismatch");
+       } else
+               route->rt_family = addr->a_family;
+
+       if (route->rt_dst)
+               nl_addr_put(route->rt_dst);
+
+       nl_addr_get(addr);
+       route->rt_dst = addr;
+       
+       route->rt_mask |= (ROUTE_ATTR_DST|ROUTE_ATTR_FAMILY|ROUTE_ATTR_DST_LEN);
+
+       return 0;
+}
+
+/**
+ * Get the destination address of a route
+ * @arg route          route handle
+ * @return Destination address or NULL if not set
+ */
+struct nl_addr *rtnl_route_get_dst(struct rtnl_route *route)
+{
+       if (route->rt_mask & ROUTE_ATTR_DST)
+               return route->rt_dst;
+       else
+               return NULL;
+}
+
+/**
+ * Set the destination address prefix length of a route to the specified value
+ * @arg route          route to be changed
+ * @arg prefix         new destination address prefix
+ * @attention The destination address prefix gets overwritten by calls
+ *            to rtnl_route_set_dst() rtnl_route_set_dst_str().
+ */
+void rtnl_route_set_dst_len(struct rtnl_route *route, int prefix)
+{
+       route->rt_dst_len = prefix;
+       route->rt_mask |= ROUTE_ATTR_DST_LEN;
+}
+
+/**
+ * Get the destination address prefix length of a route
+ * @arg route          route handle
+ * @return Prefix length or -1 if not set
+ */
+int rtnl_route_get_dst_len(struct rtnl_route *route)
+{
+       if (route->rt_mask & ROUTE_ATTR_DST_LEN)
+               return route->rt_dst_len;
+       else
+               return -1;
+}
+
+/** @} */
+/**
+ * @name Attribute: Source Address
+ * @{
+ */
+
+/**
+ * Set the source address of a route to the specified address
+ * @arg route          route to be changed
+ * @arg addr           new source address
+ *
+ * Assigns the new source address to the specified \a route,
+ * overwrites the source address length (rtnl_route::rt_src_len),
+ * and sets the route's address family to the new address's family if
+ * it is not set already.
+ *
+ * If a address family has been specified already via either calling
+ * rtnl_route_set_family() or by setting one of the other addresses,
+ * the specified \a addr is automatically validated against this family
+ * and the assignment fails in case of a mismatch.
+ * 
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_route_set_src(struct rtnl_route *route, struct nl_addr *addr)
+{
+       if (route->rt_mask & ROUTE_ATTR_FAMILY) {
+               if (addr->a_family != route->rt_family)
+                       return nl_error(EINVAL, "Address family mismatch");
+       } else
+               route->rt_family = addr->a_family;
+
+       if (route->rt_src)
+               nl_addr_put(route->rt_src);
+
+       nl_addr_get(addr);
+       route->rt_src = addr;
+       route->rt_mask |= (ROUTE_ATTR_SRC|ROUTE_ATTR_FAMILY|ROUTE_ATTR_SRC_LEN);
+
+       return 0;
+}
+
+/**
+ * Get the source address of a route
+ * @arg route          route handle
+ * @return Source address or NULL if not set
+ */
+struct nl_addr *rtnl_route_get_src(struct rtnl_route *route)
+{
+       if (route->rt_mask & ROUTE_ATTR_SRC)
+               return route->rt_src;
+       else
+               return NULL;
+}
+
+/**
+ * Set the source address prefix length of a route to the specified value
+ * @arg route          route to be changed
+ * @arg prefix         new source address prefix
+ * @attention The source address prefix gets overwritten by calls
+ *            to rtnl_route_src_dst() rtnl_route_set_src_str().
+ */
+void rtnl_route_set_src_len(struct rtnl_route *route, int prefix)
+{
+       route->rt_dst_len = prefix;
+       route->rt_mask |= ROUTE_ATTR_SRC_LEN;
+}
+
+/**
+ * Get the source address prefix length of a route
+ * @arg route          route handle
+ * @return Prefix length or -1 if not set
+ */
+int rtnl_route_get_src_len(struct rtnl_route *route)
+{
+       if (route->rt_mask & ROUTE_ATTR_SRC_LEN)
+               return route->rt_src_len;
+       else
+               return -1;
+}
+
+/** @} */
+/**
+ * @name Attribute: Gateway Address
+ * @{
+ */
+
+/**
+ * Set the gateway address of a route to the specified address
+ * @arg route          route to be changed
+ * @arg addr           new gateway address
+ *
+ * Assigns the new gateway address to the specified \a route,
+ * and sets the route's address family to the new address's family if
+ * it is not set already.
+ *
+ * If a address family has been specified already via either calling
+ * rtnl_route_set_family() or by setting one of the other addresses,
+ * the specified \a addr is automatically validated against this family
+ * and the assignment fails in case of a mismatch.
+ * 
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_route_set_gateway(struct rtnl_route *route, struct nl_addr *addr)
+{
+       if (route->rt_mask & ROUTE_ATTR_FAMILY) {
+               if (addr->a_family != route->rt_family)
+                       return nl_error(EINVAL, "Address family mismatch");
+       } else
+               route->rt_family = addr->a_family;
+
+       if (route->rt_gateway)
+               nl_addr_put(route->rt_gateway);
+
+       nl_addr_get(addr);
+       route->rt_gateway = addr;
+       route->rt_mask |= (ROUTE_ATTR_GATEWAY | ROUTE_ATTR_FAMILY);
+
+       return 0;
+}
+
+/**
+ * Get the gateway address of a route
+ * @arg route          route handle
+ * @return Gateway address or NULL if not set
+ */
+struct nl_addr *rtnl_route_get_gateway(struct rtnl_route *route)
+{
+       if (route->rt_mask & ROUTE_ATTR_GATEWAY)
+               return route->rt_gateway;
+       else
+               return NULL;
+}
+
+/** @} */
+/**
+ * @name Attribute: Type
+ * @{
+ */
+
+/**
+ * Set the type of a route to the specified value
+ * @arg route          route to be changed
+ * @arg type           new route type
+ */
+void rtnl_route_set_type(struct rtnl_route *route, int type)
+{
+       route->rt_type = type;
+       route->rt_mask |= ROUTE_ATTR_TYPE;
+}
+
+/**
+ * Get the type of a route
+ * @arg route          route handle
+ * @return Type of route or -1 if not set
+ */
+int rtnl_route_get_type(struct rtnl_route *route)
+{
+       if (route->rt_mask & ROUTE_ATTR_TYPE)
+               return route->rt_type;
+       else
+               return -1;
+}
+
+/** @} */
+/**
+ * @name Attribute: Flags
+ * @{
+ */
+
+/**
+ * Add flags to a route 
+ * @arg route          route to be changed
+ * @arg flags          flags to set
+ */
+void rtnl_route_set_flags(struct rtnl_route *route, unsigned int flags)
+{
+       route->rt_flag_mask |= flags;
+       route->rt_flags |= flags;
+       route->rt_mask |= ROUTE_ATTR_FLAGS;
+}
+
+/**
+ * Remove flags from a route 
+ * @arg route          route to be changed
+ * @arg flags          flags to unset
+ */
+void rtnl_route_unset_flags(struct rtnl_route *route, unsigned int flags)
+{
+       route->rt_flag_mask |= flags;
+       route->rt_flags &= ~flags;
+       route->rt_mask |= ROUTE_ATTR_FLAGS;
+}
+
+/**
+ * Get flags of a route
+ * @arg route          route handle
+ */
+unsigned int rtnl_route_get_flags(struct rtnl_route *route)
+{
+       return route->rt_flags;
+}
+
+/** @} */
+/**
+ * @name Attribute: Routing Metrics
+ * @{
+ */
+
+/**
+ * Set a metric of a route to the specified value
+ * @arg route          route to be changed
+ * @arg metric         metric to be changed (see XXX)
+ * @arg value          new metric value
+ * @return 0 on sucess or a negative error code
+ */
+int rtnl_route_set_metric(struct rtnl_route *route, int metric, uint32_t value)
+{
+       if (metric <= RTAX_MAX || metric < 1)
+               return nl_error(EINVAL, "Metric out of range (1..%d)",
+                   RTAX_MAX);
+
+       route->rt_metrics[metric - 1] = value;
+       route->rt_metrics_mask |= (1 << (metric - 1));
+
+       return 0;
+}
+
+/**
+ * Unset a metric of a route
+ * @arg route          route to be changed
+ * @arg metric         metric to be unset (see XXX)
+ * @return 0 on sucess or a negative error code
+ */
+int rtnl_route_unset_metric(struct rtnl_route *route, int metric)
+{
+       if (metric <= RTAX_MAX || metric < 1)
+               return nl_error(EINVAL, "Metric out of range (1..%d)",
+                   RTAX_MAX);
+
+       route->rt_metrics_mask &= ~(1 << (metric - 1));
+
+       return 0;
+}
+
+/**
+ * Get a metric for a route
+ * @arg route          route handle
+ * @arg metric         metric to get
+ * @return The value for the specified metric or UINT_MAX if not set
+ */
+unsigned int rtnl_route_get_metric(struct rtnl_route *route, int metric)
+{
+       if (metric <= RTAX_MAX || metric < 1)
+               return UINT_MAX;
+
+       if (!(route->rt_metrics_mask & (1 << (metric - 1))))
+               return UINT_MAX;
+
+       return route->rt_metrics[metric - 1];
+}
+
+/** @} */
+/**
+ * @name Attribute: Preferred Source Address
+ * @{
+ */
+
+/**
+ * Set the preferred source address of a route to the specified address
+ * @arg route          route to be changed
+ * @arg addr           new preferred source address
+ *
+ * Assigns the new preferred source address to the specified \a route,
+ * and sets the route's address family to the new address's family if
+ * it is not set already.
+ *
+ * If a address family has been specified already via either calling
+ * rtnl_route_set_family() or by setting one of the other addresses,
+ * the specified \a addr is automatically validated against this family
+ * and the assignment fails in case of a mismatch.
+ * 
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_route_set_pref_src(struct rtnl_route *route, struct nl_addr *addr)
+{
+       if (route->rt_mask & ROUTE_ATTR_FAMILY) {
+               if (addr->a_family != route->rt_family)
+                       return nl_error(EINVAL, "Address family mismatch");
+       } else
+               route->rt_family = addr->a_family;
+
+       if (route->rt_pref_src)
+               nl_addr_put(route->rt_pref_src);
+
+       nl_addr_get(addr);
+       route->rt_pref_src = addr;
+       route->rt_mask |= (ROUTE_ATTR_PREF_SRC | ROUTE_ATTR_FAMILY);
+
+       return 0;
+}
+
+/**
+ * Get the preferred source address of a route
+ * @arg route          route handle
+ * @return Preferred source address or NULL if not set
+ */
+struct nl_addr *rtnl_route_get_pref_src(struct rtnl_route *route)
+{
+       if (route->rt_mask & ROUTE_ATTR_PREF_SRC)
+               return route->rt_pref_src;
+       else
+               return NULL;
+}
+
+/** @} */
+/**
+ * @name Attribute: Outgoing Interface Index
+ * @{
+ */
+
+/**
+ * Set the outgoing interface of a route to the specified value
+ * @arg route          route to be changed
+ * @arg ifindex                interface index of new outoing interface
+ */
+void rtnl_route_set_oif(struct rtnl_route *route, int ifindex)
+{
+       route->rt_oif = ifindex;
+       route->rt_mask |= ROUTE_ATTR_OIF;
+}
+
+/**
+ * Get the outgoing interface index of a route
+ * @arg route          route handle
+ * @return interface index or RTNL_LINK_NOT_FOUND if not set
+ */
+int rtnl_route_get_oif(struct rtnl_route *route)
+{
+       if (route->rt_mask & ROUTE_ATTR_OIF)
+               return route->rt_oif;
+       else
+               return RTNL_LINK_NOT_FOUND;
+}
+
+/** @} */
+/**
+ * @name Attribute: Incoming Interface
+ * @{
+ */
+
+/**
+ * Set the incoming interface of a route to the specified value
+ * @arg route          route to be changed
+ * @arg name           interface name of the new incoming interface
+ */
+void rtnl_route_set_iif(struct rtnl_route *route, const char *name)
+{
+       strncpy(route->rt_iif, name, sizeof(route->rt_iif) - 1);
+       route->rt_mask |= ROUTE_ATTR_IIF;
+}
+
+/**
+ * Get the incomming interface name of a route
+ * @arg route          route handle
+ * @return interface name or NULL if not set
+ */
+char *rtnl_route_get_iif(struct rtnl_route *route)
+{
+       if (route->rt_mask & ROUTE_ATTR_IIF)
+               return route->rt_iif;
+       else
+               return NULL;
+}
+
+#if 0
+       struct rtnl_rtcacheinfo rt_cacheinfo;
+#endif
+
+/** @} */
+/**
+ * @name Attribute: Nexthop
+ * @{
+ */
+
+void rtnl_route_add_nexthop(struct rtnl_route *route, struct rtnl_nexthop *nh)
+{
+       nl_list_add_tail(&nh->rtnh_list, &route->rt_nexthops);
+       route->rt_mask |= ROUTE_ATTR_MULTIPATH;
+}
+
+void rtnl_route_remove_nexthop(struct rtnl_nexthop *nh)
+{
+       nl_list_del(&nh->rtnh_list);
+}
+
+struct nl_list_head *rtnl_route_get_nexthops(struct rtnl_route *route)
+{
+       return &route->rt_nexthops;
+}
+
+/** @} */
+       
+/**
+ * @name Routing Table Identifier Translations
+ * @{
+ */
+
+static struct trans_tbl route_tables[] = {
+       __ADD(RT_TABLE_UNSPEC, unspec)
+       __ADD(RT_TABLE_DEFAULT, default)
+       __ADD(RT_TABLE_MAIN, main)
+       __ADD(RT_TABLE_LOCAL, local)
+};
+
+/**
+ * Convert routing table identifier to character string.
+ * @arg table          Routing table identifier.
+ * @arg buf            Destination buffer
+ * @arg size           Size of destination buffer.
+ *
+ * Converts a routing table identifier to a character string and stores
+ * it in the specified destination buffer.
+ *
+ * @return The destination buffer or the type encoded in hexidecimal
+ *         form if the routing table identifier is unknown.
+ */
+char *rtnl_route_table2str(int table, char *buf, size_t size)
+{
+       return __type2str(table, buf, size, route_tables,
+                         ARRAY_SIZE(route_tables));
+}
+
+/**
+ * Convert character string to routing table identifier.
+ * @arg name           Name of routing table.
+ *
+ * Converts the provided character string specifying a routing table
+ * identifier to the corresponding numeric value.
+ *
+ * @return Routing table identifier or a negative value if no match was found.
+ */
+int rtnl_route_str2table(const char *name)
+{
+       return __str2type(name, route_tables, ARRAY_SIZE(route_tables));
+}
+
+
+/** @} */
+
+/**
+ * @name Routing Protocol Translations
+ * @{
+ */
+
+static struct trans_tbl route_protos[] = {
+       __ADD(RTPROT_UNSPEC, unspec)
+       __ADD(RTPROT_REDIRECT, redirect)
+       __ADD(RTPROT_KERNEL, kernel)
+       __ADD(RTPROT_BOOT, boot)
+       __ADD(RTPROT_STATIC, static)
+};
+
+/**
+ * Convert routing protocol identifier to character string.
+ * @arg proto          Routing protocol identifier.
+ * @arg buf            Destination buffer
+ * @arg size           Size of destination buffer.
+ *
+ * Converts a routing protocol identifier to a character string and stores
+ * it in the specified destination buffer.
+ *
+ * @return The destination buffer or the protocol encoded in hexidecimal
+ *         form if the routing protocol is unknown.
+ */
+char *rtnl_route_proto2str(int proto, char *buf, size_t size)
+{
+       return __type2str(proto, buf, size, route_protos,
+                         ARRAY_SIZE(route_protos));
+}
+
+/**
+ * Convert character string to routing protocol identifier.
+ * @arg name           Name of routing protocol.
+ *
+ * Converts the provided character string specifying a routing protocl
+ * identifier to the corresponding numeric value.
+ *
+ * @return Routing protocol dentifier or a negative value if no match was found.
+ */
+int rtnl_route_str2proto(const char *name)
+{
+       return __str2type(name, route_protos, ARRAY_SIZE(route_protos));
+}
+
+/** @} */
+
+/**
+ * @name Routing Metrices Translations
+ * @{
+ */
+
+static struct trans_tbl route_metrices[] = {
+       __ADD(RTAX_UNSPEC, unspec)
+       __ADD(RTAX_LOCK, lock)
+       __ADD(RTAX_MTU, mtu)
+       __ADD(RTAX_WINDOW, window)
+       __ADD(RTAX_RTT, rtt)
+       __ADD(RTAX_RTTVAR, rttvar)
+       __ADD(RTAX_SSTHRESH, ssthresh)
+       __ADD(RTAX_CWND, cwnd)
+       __ADD(RTAX_ADVMSS, advmss)
+       __ADD(RTAX_REORDERING, reordering)
+       __ADD(RTAX_HOPLIMIT, hoplimit)
+       __ADD(RTAX_INITCWND, initcwnd)
+       __ADD(RTAX_FEATURES, features)
+};
+
+/**
+ * Convert routing metric identifier to character string.
+ * @arg metric         Routing metric identifier.
+ * @arg buf            Destination buffer
+ * @arg size           Size of destination buffer.
+ *
+ * Converts a routing metric identifier to a character string and stores
+ * it in the specified destination buffer.
+ *
+ * @return The destination buffer or the metric encoded in hexidecimal
+ *         form if the routing metric identifier is unknown.
+ */
+char *rtnl_route_metric2str(int metric, char *buf, size_t size)
+{
+       return __type2str(metric, buf, size, route_metrices,
+                         ARRAY_SIZE(route_metrices));
+}
+
+/**
+ * Convert character string to routing metric identifier.
+ * @arg name           Name of routing metric.
+ *
+ * Converts the provided character string specifying a routing metric
+ * identifier to the corresponding numeric value.
+ *
+ * @return Routing metric dentifier or a negative value if no match was found.
+ */
+int rtnl_route_str2metric(const char *name)
+{
+       return __str2type(name, route_metrices, ARRAY_SIZE(route_metrices));
+}
+
+/** @} */
+
+/**
+ * @name Nexthop Flags Translations
+ * @{
+ */
+
+static struct trans_tbl nh_flags[] = {
+       __ADD(RTNH_F_DEAD, dead)
+       __ADD(RTNH_F_PERVASIVE, pervasive)
+       __ADD(RTNH_F_ONLINK, onlink)
+};
+
+/**
+ * Convert nexthop flags to a character string.
+ * @arg flags          Nexthop flags.
+ * @arg buf            Destination buffer.
+ * @arg len            Length of destination buffer.
+ *
+ * Converts nexthop flags to a character string separated by
+ * commas and stores it in the specified destination buffer.
+ *
+ * \return The destination buffer
+ */
+char * rtnl_route_nh_flags2str(int flags, char *buf, size_t len)
+{
+       return __flags2str(flags, buf, len, nh_flags, ARRAY_SIZE(nh_flags));
+}
+
+/**
+ * Convert a character string to a nexthop flag
+ * @arg name           Name of nexthop flag.
+ *
+ * Converts the provided character string specifying a nexthop
+ * flag to the corresponding numeric value.
+ *
+ * \return Nexthop flag or a negative value if none was found.
+ */
+int rtnl_route_nh_str2flags(const char *name)
+{
+       return __str2flags(name, nh_flags, ARRAY_SIZE(nh_flags));
+}
+
+/** @} */
+
+static struct nl_cache_ops rtnl_route_ops = {
+       .co_name                = "route/route",
+       .co_size                = sizeof(struct rtnl_route),
+       .co_hdrsize             = sizeof(struct rtmsg),
+       .co_msgtypes            = {
+                                       { RTM_NEWROUTE, "new" },
+                                       { RTM_DELROUTE, "delete" },
+                                       { RTM_GETROUTE, "get" },
+                                       { -1, NULL },
+                                 },
+       .co_protocol            = NETLINK_ROUTE,
+       .co_request_update      = route_request_update,
+       .co_msg_parser          = route_msg_parser,
+       .co_constructor         = route_constructor,
+       .co_free_data           = route_free_data,
+       .co_dump[NL_DUMP_BRIEF] = route_dump_brief,
+       .co_dump[NL_DUMP_FULL]  = route_dump_full,
+       .co_dump[NL_DUMP_STATS] = route_dump_stats,
+       .co_dump[NL_DUMP_XML]   = route_dump_xml,
+       .co_dump[NL_DUMP_ENV]   = route_dump_env,
+       .co_filter              = route_filter,
+};
+
+static void __init route_init(void)
+{
+       nl_cache_mngt_register(&rtnl_route_ops);
+}
+
+static void __exit route_exit(void)
+{
+       nl_cache_mngt_unregister(&rtnl_route_ops);
+}
+
+/** @} */
diff --git a/lib/route/rtnl.c b/lib/route/rtnl.c
new file mode 100644 (file)
index 0000000..4fd2660
--- /dev/null
@@ -0,0 +1,168 @@
+/*
+ * lib/route/rtnl.c            Routing Netlink
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @defgroup rtnl Routing Netlink
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink/netlink.h>
+#include <netlink/utils.h>
+#include <netlink/route/rtnl.h>
+
+/**
+ * @name Sending
+ * @{
+ */
+
+/**
+ * Send routing netlink request message
+ * @arg handle         Netlink handle.
+ * @arg type           Netlink message type.
+ * @arg family         Address family.
+ * @arg flags          Additional netlink message flags.
+ *
+ * Fills out a routing netlink request message and sends it out
+ * using nl_send_simple().
+ *
+ * @return 0 on success or a negative error code.
+ */
+int nl_rtgen_request(struct nl_handle *handle, int type, int family, int flags)
+{
+       struct rtgenmsg gmsg = {
+               .rtgen_family = family,
+       };
+
+       return nl_send_simple(handle, type, flags, &gmsg, sizeof(gmsg));
+}
+
+/** @} */
+
+/**
+ * @name Routing Type Translations
+ * @{
+ */
+
+static struct trans_tbl rtntypes[] = {
+       __ADD(RTN_UNSPEC,unspec)
+       __ADD(RTN_UNICAST,unicast)
+       __ADD(RTN_LOCAL,local)
+       __ADD(RTN_BROADCAST,broadcast)
+       __ADD(RTN_ANYCAST,anycast)
+       __ADD(RTN_MULTICAST,multicast)
+       __ADD(RTN_BLACKHOLE,blackhole)
+       __ADD(RTN_UNREACHABLE,unreachable)
+       __ADD(RTN_PROHIBIT,prohibit)
+       __ADD(RTN_THROW,throw)
+       __ADD(RTN_NAT,nat)
+       __ADD(RTN_XRESOLVE,xresolve)
+};
+
+/**
+ * Convert routing type to character string.
+ * @arg type           Routing type.
+ * @arg buf            Destination buffer.
+ * @arg size           Size of destination buffer.
+ *
+ * Converts a routing type to a character string and stores it in
+ * the specified destination buffer.
+ *
+ * @return The destination buffer or the type encoded in hexidecimal
+ *         form if the routing type is unknown.
+ */
+char *nl_rtntype2str(int type, char *buf, size_t size)
+{
+       return __type2str(type, buf, size, rtntypes, ARRAY_SIZE(rtntypes));
+}
+
+/**
+ * Convert character string to routing type.
+ * @arg name           Name of routing type.
+ *
+ * Converts the provided character string specifying a routing
+ * type to the corresponding numeric value.
+ *
+ * @return Routing type or a negative value if no match was found.
+ */
+int nl_str2rtntype(const char *name)
+{
+       return __str2type(name, rtntypes, ARRAY_SIZE(rtntypes));
+}
+
+/** @} */
+
+/**
+ * @name Scope Translations
+ * @{
+ */
+
+static struct trans_tbl scopes[] = {
+       __ADD(255,nowhere)
+       __ADD(254,host)
+       __ADD(253,link)
+       __ADD(200,site)
+       __ADD(0,global)
+};
+
+/**
+ * Convert scope identifier to character string.
+ * @arg scope          Scope identifier.
+ * @arg buf            Destination buffer
+ * @arg size           Size of destination buffer.
+ *
+ * Converts a scope identifier to a character string and stores it in
+ * the specified destination buffer.
+ *
+ * @return The destination buffer or the type encoded in hexidecimal
+ *         form if the scope identifier is unknown.
+ */
+char *rtnl_scope2str(int scope, char *buf, size_t size)
+{
+       return __type2str(scope, buf, size, scopes, ARRAY_SIZE(scopes));
+}
+
+/**
+ * Convert character string to scope identifier.
+ * @arg name           Name of scope.
+ *
+ * Converts the provided character string specifying a scope identifier
+ * to the corresponding numeric value.
+ *
+ * @return Scope identifier or a negative value if no match was found.
+ */
+int rtnl_str2scope(const char *name)
+{
+       return __str2type(name, scopes, ARRAY_SIZE(scopes));
+}
+
+/** @} */
+
+/**
+ * @name Realms Translations
+ * @{
+ */
+
+char * rtnl_realms2str(uint32_t realms, char *buf, size_t len)
+{
+       int from = RTNL_REALM_FROM(realms);
+       int to = RTNL_REALM_TO(realms);
+
+       snprintf(buf, len, "%d/%d", from, to);
+
+       return buf;
+}
+
+/** @} */
+
+
+
+/** @} */
diff --git a/lib/route/rule.c b/lib/route/rule.c
new file mode 100644 (file)
index 0000000..3e04f3f
--- /dev/null
@@ -0,0 +1,1015 @@
+/*
+ * lib/route/rule.c          Routing Rules
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup rtnl
+ * @defgroup rule Routing Rules
+ * @brief
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink/netlink.h>
+#include <netlink/utils.h>
+#include <netlink/route/rtnl.h>
+#include <netlink/route/rule.h>
+#include <inttypes.h>
+
+/** @cond SKIP */
+#define RULE_ATTR_FAMILY  0x0001
+#define RULE_ATTR_PRIO    0x0002
+#define RULE_ATTR_FWMARK  0x0004
+#define RULE_ATTR_IIF     0x0008
+#define RULE_ATTR_REALMS  0x0010
+#define RULE_ATTR_SRC     0x0020
+#define RULE_ATTR_DST     0x0040
+#define RULE_ATTR_DSFIELD 0x0080
+#define RULE_ATTR_TABLE   0x0100
+#define RULE_ATTR_TYPE    0x0200
+#define RULE_ATTR_SRC_LEN 0x0400
+#define RULE_ATTR_DST_LEN 0x0800
+#define RULE_ATTR_SRCMAP  0x1000
+
+static struct nl_cache_ops rtnl_rule_ops;
+/** @endcond */
+
+static void rule_free_data(struct nl_object *c)
+{
+       struct rtnl_rule *rule = nl_object_priv(c);
+
+       if (!rule)
+               return;
+
+       nl_addr_put(rule->r_src);
+       nl_addr_put(rule->r_dst);
+}
+
+static struct nla_policy rule_policy[RTA_MAX+1] = {
+       [RTA_PRIORITY]  = { .type = NLA_U32 },
+       [RTA_FLOW]      = { .type = NLA_U32 },
+       [RTA_PROTOINFO] = { .type = NLA_U32 },
+       [RTA_IIF]       = { .type = NLA_STRING,
+                           .maxlen = IFNAMSIZ, },
+};
+
+static int rule_msg_parser(struct sockaddr_nl *who, struct nlmsghdr *n,
+                          void *arg)
+{
+       struct rtnl_rule *rule;
+       struct rtmsg *r;
+       struct nlattr *tb[RTA_MAX+1];
+       struct nl_parser_param *pp = arg;
+       int err = 1;
+
+       rule = rtnl_rule_alloc();
+       if (!rule) {
+               err = nl_errno(ENOMEM);
+               goto errout;
+       }
+
+       rule->ce_msgtype = n->nlmsg_type;
+       r = nlmsg_data(n);
+
+       err = nlmsg_parse(n, sizeof(*r), tb, RTA_MAX, rule_policy);
+       if (err < 0)
+               goto errout;
+
+       rule->r_family = r->rtm_family;
+       rule->r_type = r->rtm_type;
+       rule->r_dsfield = r->rtm_tos;
+       rule->r_src_len = r->rtm_src_len;
+       rule->r_dst_len = r->rtm_dst_len;
+       rule->r_table = r->rtm_table;
+       rule->r_mask = (RULE_ATTR_FAMILY | RULE_ATTR_TYPE | RULE_ATTR_DSFIELD |
+                       RULE_ATTR_SRC_LEN | RULE_ATTR_DST_LEN | RULE_ATTR_TYPE);
+
+       if (tb[RTA_PRIORITY]) {
+               rule->r_prio = nla_get_u32(tb[RTA_PRIORITY]);
+               rule->r_mask |= RULE_ATTR_PRIO;
+       }
+
+       if (tb[RTA_SRC]) {
+               rule->r_src = nla_get_addr(tb[RTA_SRC], r->rtm_family);
+               if (!rule->r_src) {
+                       err = nl_errno(ENOMEM);
+                       goto errout;
+               }
+               nl_addr_set_prefixlen(rule->r_src, r->rtm_src_len);
+               rule->r_mask |= RULE_ATTR_SRC;
+       }
+
+       if (tb[RTA_DST]) {
+               rule->r_dst = nla_get_addr(tb[RTA_DST], r->rtm_family);
+               if (!rule->r_dst) {
+                       err = nl_errno(ENOMEM);
+                       goto errout;
+               }
+               nl_addr_set_prefixlen(rule->r_dst, r->rtm_dst_len);
+               rule->r_mask |= RULE_ATTR_DST;
+       }
+
+       if (tb[RTA_PROTOINFO]) {
+               rule->r_fwmark = nla_get_u32(tb[RTA_PROTOINFO]);
+               rule->r_mask |= RULE_ATTR_FWMARK;
+       }
+
+       if (tb[RTA_IIF]) {
+               nla_strlcpy(rule->r_iif, tb[RTA_IIF], IFNAMSIZ);
+               rule->r_mask |= RULE_ATTR_IIF;
+       }
+
+       if (tb[RTA_FLOW]) {
+               rule->r_realms = nla_get_u32(tb[RTA_FLOW]);
+               rule->r_mask |= RULE_ATTR_REALMS;
+       }
+
+       if (tb[RTA_GATEWAY]) {
+               rule->r_srcmap = nla_get_addr(tb[RTA_GATEWAY], r->rtm_family);
+               if (!rule->r_srcmap) {
+                       err = nl_errno(ENOMEM);
+                       goto errout;
+               }
+               rule->r_mask |= RULE_ATTR_SRCMAP;
+       }
+
+       err = pp->pp_cb((struct nl_object *) rule, pp);
+       if (err < 0)
+               goto errout;
+
+       return P_ACCEPT;
+
+errout:
+       rtnl_rule_put(rule);
+       return err;
+}
+
+static int rule_request_update(struct nl_cache *c, struct nl_handle *h)
+{
+       return nl_rtgen_request(h, RTM_GETRULE, AF_UNSPEC, NLM_F_DUMP);
+}
+
+static int rule_dump_brief(struct nl_object *o, struct nl_dump_params *p)
+{
+       struct rtnl_rule *r = (struct rtnl_rule *) o;
+       char buf[128];
+
+       if (r->r_mask & RULE_ATTR_PRIO)
+               dp_dump(p, "%d:\t", r->r_prio);
+       else
+               dp_dump(p, "0:\t");
+
+       if (r->r_mask & RULE_ATTR_SRC)
+               dp_dump(p, "from %s ",
+                       nl_addr2str(r->r_src, buf, sizeof(buf)));
+       else if (r->r_mask & RULE_ATTR_SRC_LEN && r->r_src_len)
+               dp_dump(p, "from 0/%d ", r->r_src_len);
+
+       if (r->r_mask & RULE_ATTR_DST)
+               dp_dump(p, "to %s ",
+                       nl_addr2str(r->r_dst, buf, sizeof(buf)));
+       else if (r->r_mask & RULE_ATTR_DST_LEN && r->r_dst_len)
+               dp_dump(p, "to 0/%d ", r->r_dst_len);
+
+       if (r->r_mask & RULE_ATTR_DSFIELD && r->r_dsfield)
+               dp_dump(p, "tos %d ", r->r_dsfield);
+
+       if (r->r_mask & RULE_ATTR_FWMARK)
+               dp_dump(p, "fwmark %" PRIx64 , r->r_fwmark);
+
+       if (r->r_mask & RULE_ATTR_IIF)
+               dp_dump(p, "iif %s ", r->r_iif);
+
+       if (r->r_mask & RULE_ATTR_TABLE)
+               dp_dump(p, "lookup %s ",
+                       rtnl_route_table2str(r->r_table, buf, sizeof(buf)));
+
+       if (r->r_mask & RULE_ATTR_REALMS)
+               dp_dump(p, "realms %s ",
+                       rtnl_realms2str(r->r_realms, buf, sizeof(buf)));
+
+       dp_dump(p, "action %s\n",
+               nl_rtntype2str(r->r_type, buf, sizeof(buf)));
+
+       return 1;
+}
+
+static int rule_dump_full(struct nl_object *obj, struct nl_dump_params *p)
+{
+       struct rtnl_rule *rule = (struct rtnl_rule *) obj;
+       char buf[128];
+       int line;
+
+       line = rule_dump_brief(obj, p);
+
+       dp_dump_line(p, line++, "  family %s",
+                    nl_af2str(rule->r_family, buf, sizeof(buf)));
+
+       if (rule->r_mask & RULE_ATTR_SRCMAP)
+               dp_dump(p, " srcmap %s",
+                       nl_addr2str(rule->r_srcmap, buf, sizeof(buf)));
+
+       dp_dump(p, "\n");
+
+       return line;
+}
+
+static int rule_dump_stats(struct nl_object *obj, struct nl_dump_params *p)
+{
+       return rule_dump_full(obj, p);
+}
+
+static int rule_dump_xml(struct nl_object *obj, struct nl_dump_params *p)
+{
+       struct rtnl_rule *rule = (struct rtnl_rule *) obj;
+       char buf[128];
+       int line = 0;
+       
+       dp_dump_line(p, line++, "<rule>\n");
+
+       dp_dump_line(p, line++, "  <priority>%u</priority>\n",
+                    rule->r_prio);
+       dp_dump_line(p, line++, "  <family>%s</family>\n",
+                    nl_af2str(rule->r_family, buf, sizeof(buf)));
+
+       if (rule->r_mask & RULE_ATTR_DST)
+               dp_dump_line(p, line++, "  <dst>%s</dst>\n",
+                            nl_addr2str(rule->r_dst, buf, sizeof(buf)));
+
+       if (rule->r_mask & RULE_ATTR_DST_LEN)
+               dp_dump_line(p, line++, "  <dstlen>%u</dstlen>\n",
+                            rule->r_dst_len);
+
+       if (rule->r_mask & RULE_ATTR_SRC)
+               dp_dump_line(p, line++, "  <src>%s</src>\n",
+                            nl_addr2str(rule->r_src, buf, sizeof(buf)));
+
+       if (rule->r_mask & RULE_ATTR_SRC_LEN)
+               dp_dump_line(p, line++, "  <srclen>%u</srclen>\n",
+                            rule->r_src_len);
+
+       if (rule->r_mask & RULE_ATTR_IIF)
+               dp_dump_line(p, line++, "  <iif>%s</iif>\n", rule->r_iif);
+
+       if (rule->r_mask & RULE_ATTR_TABLE)
+               dp_dump_line(p, line++, "  <table>%u</table>\n",
+                            rule->r_table);
+
+       if (rule->r_mask & RULE_ATTR_REALMS)
+               dp_dump_line(p, line++, "  <realms>%u</realms>\n",
+                            rule->r_realms);
+
+       if (rule->r_mask & RULE_ATTR_FWMARK)
+               dp_dump_line(p, line++, "  <fwmark>%" PRIx64 "</fwmark>\n",
+                            rule->r_fwmark);
+
+       if (rule->r_mask & RULE_ATTR_DSFIELD)
+               dp_dump_line(p, line++, "  <dsfield>%u</dsfield>\n",
+                            rule->r_dsfield);
+
+       if (rule->r_mask & RULE_ATTR_TYPE)
+               dp_dump_line(p, line++, "<type>%s</type>\n",
+                            nl_rtntype2str(rule->r_type, buf, sizeof(buf)));
+
+       if (rule->r_mask & RULE_ATTR_SRCMAP)
+               dp_dump_line(p, line++, "<srcmap>%s</srcmap>\n",
+                            nl_addr2str(rule->r_srcmap, buf, sizeof(buf)));
+
+       dp_dump_line(p, line++, "</rule>\n");
+
+       return line;
+}
+
+static int rule_dump_env(struct nl_object *obj, struct nl_dump_params *p)
+{
+       struct rtnl_rule *rule = (struct rtnl_rule *) obj;
+       char buf[128];
+       int line = 0;
+
+       dp_dump_line(p, line++, "RULE_PRIORITY=%u\n",
+                    rule->r_prio);
+       dp_dump_line(p, line++, "RULE_FAMILY=%s\n",
+                    nl_af2str(rule->r_family, buf, sizeof(buf)));
+
+       if (rule->r_mask & RULE_ATTR_DST)
+               dp_dump_line(p, line++, "RULE_DST=%s\n",
+                            nl_addr2str(rule->r_dst, buf, sizeof(buf)));
+
+       if (rule->r_mask & RULE_ATTR_DST_LEN)
+               dp_dump_line(p, line++, "RULE_DSTLEN=%u\n",
+                            rule->r_dst_len);
+
+       if (rule->r_mask & RULE_ATTR_SRC)
+               dp_dump_line(p, line++, "RULE_SRC=%s\n",
+                            nl_addr2str(rule->r_src, buf, sizeof(buf)));
+
+       if (rule->r_mask & RULE_ATTR_SRC_LEN)
+               dp_dump_line(p, line++, "RULE_SRCLEN=%u\n",
+                            rule->r_src_len);
+
+       if (rule->r_mask & RULE_ATTR_IIF)
+               dp_dump_line(p, line++, "RULE_IIF=%s\n", rule->r_iif);
+
+       if (rule->r_mask & RULE_ATTR_TABLE)
+               dp_dump_line(p, line++, "RULE_TABLE=%u\n",
+                            rule->r_table);
+
+       if (rule->r_mask & RULE_ATTR_REALMS)
+               dp_dump_line(p, line++, "RULE_REALM=%u\n",
+                            rule->r_realms);
+
+       if (rule->r_mask & RULE_ATTR_FWMARK)
+               dp_dump_line(p, line++, "RULE_FWMARK=0x%" PRIx64 "\n",
+                            rule->r_fwmark);
+
+       if (rule->r_mask & RULE_ATTR_DSFIELD)
+               dp_dump_line(p, line++, "RULE_DSFIELD=%u\n",
+                            rule->r_dsfield);
+
+       if (rule->r_mask & RULE_ATTR_TYPE)
+               dp_dump_line(p, line++, "RULE_TYPE=%s\n",
+                            nl_rtntype2str(rule->r_type, buf, sizeof(buf)));
+
+       if (rule->r_mask & RULE_ATTR_SRCMAP)
+               dp_dump_line(p, line++, "RULE_SRCMAP=%s\n",
+                            nl_addr2str(rule->r_srcmap, buf, sizeof(buf)));
+
+       return line;
+}
+
+
+static int rule_filter(struct nl_object *obj, struct nl_object *filter)
+{
+       struct rtnl_rule *o = (struct rtnl_rule *) obj;
+       struct rtnl_rule *f = (struct rtnl_rule *) filter;
+
+#define REQ(F) (f->r_mask & RULE_ATTR_##F)
+#define AVAIL(F) (o->r_mask & RULE_ATTR_##F)
+#define _O(F, EXPR) (REQ(F) && (!AVAIL(F) || (EXPR)))
+#define _C(F, N) (REQ(F) && (!AVAIL(F) || (o->N != f->N)))
+       if (_C(FAMILY,  r_family)                                       ||
+           _C(TABLE,   r_table)                                        ||
+           _C(REALMS,  r_realms)                                       ||
+           _C(DSFIELD, r_dsfield)                                      ||
+           _C(TYPE,    r_type)                                         ||
+           _C(PRIO,    r_prio)                                         ||
+           _C(FWMARK,  r_fwmark)                                       ||
+           _C(SRC_LEN, r_src_len)                                      ||
+           _C(DST_LEN, r_dst_len)                                      ||
+           _O(SRC,     nl_addr_cmp(o->r_src, f->r_src))                ||
+           _O(DST,     nl_addr_cmp(o->r_dst, f->r_dst))                ||
+           _O(IIF,     strcmp(o->r_iif, f->r_iif)))
+               return 0;
+#undef REQ
+#undef AVAIL
+#undef _O
+#undef _C
+
+       return 1;
+}
+
+/**
+ * @name Routing Rule Object Allocation/Freeage
+ * @{
+ */
+
+/**
+ * Allocate a new rule object
+ * @return New rule object
+ */
+struct rtnl_rule *rtnl_rule_alloc(void)
+{
+       return (struct rtnl_rule *) nl_object_alloc_from_ops(&rtnl_rule_ops);
+}
+
+/**
+ * Give back reference on routing rule object.
+ * @arg rule           Routing rule object to be given back.
+ *
+ * Decrements the reference counter and frees the object if the
+ * last reference has been released.
+ */
+void rtnl_rule_put(struct rtnl_rule *rule)
+{
+       nl_object_put((struct nl_object *) rule);
+}
+/**
+ * Free routing rule object.
+ * @arg rule           Routing rule object to be freed.
+ *
+ * @note Always use rtnl_rule_put() unless you're absolutely sure
+ *       that no other user may have a reference on this object.
+ */
+void rtnl_rule_free(struct rtnl_rule *rule)
+{
+       nl_object_free((struct nl_object *) rule);
+}
+
+/** @} */
+
+/**
+ * @name Routing Rule Cache Management
+ * @{
+ */
+
+/**
+ * Build a rule cache including all rules of the specified family currently configured in the kernel.
+ * @arg handle         netlink handle
+ * @arg family         address family
+ *
+ * Allocates a new rule cache, initializes it properly and updates it
+ * to include all rules of the specified address family currently
+ * configured in the kernel.
+ *
+ * @note The caller is responsible for destroying and freeing the
+ *       cache after using it. (nl_cache_destroy_and_free())
+ * @return The new cache or NULL if an error occured.
+ */
+struct nl_cache * rtnl_rule_alloc_cache_by_family(struct nl_handle *handle,
+                                                 int family)
+{
+       struct nl_cache * cache = nl_cache_alloc_from_ops(&rtnl_rule_ops);
+
+       if (cache == NULL)
+               return NULL;
+
+       /* XXX RULE_CACHE_FAMILY(cache) = family; */
+
+       if (nl_cache_update(handle, cache) < 0) {
+               free(cache);
+               return NULL;
+       }
+
+       return cache;
+}
+
+/**
+ * Build a rule cache including all rules currently configured in the kernel.
+ * @arg handle         netlink handle
+ *
+ * Allocates a new rule cache, initializes it properly and updates it
+ * to include all rules currently configured in the kernel.
+ *
+ * @note The caller is responsible for destroying and freeing the
+ *       cache after using it. (nl_cache_destroy_and_free())
+ * @return The new cache or NULL if an error occured.
+ */
+struct nl_cache * rtnl_rule_alloc_cache(struct nl_handle *handle)
+{
+       return rtnl_rule_alloc_cache_by_family(handle, AF_UNSPEC);
+}
+
+/** @} */
+
+/**
+ * @name Rule Addition
+ * @{
+ */
+
+static struct nl_msg *build_rule_msg(struct rtnl_rule *tmpl, int cmd, int flags)
+{
+       struct nl_msg *msg;
+       struct rtmsg rtm = {
+               .rtm_type = RTN_UNSPEC
+       };
+
+       if (cmd == RTM_NEWRULE)
+               rtm.rtm_type = RTN_UNICAST;
+               
+       if (tmpl->r_mask & RULE_ATTR_FAMILY)
+               rtm.rtm_family = tmpl->r_family;
+
+       if (tmpl->r_mask & RULE_ATTR_TABLE)
+               rtm.rtm_table = tmpl->r_table;
+
+       if (tmpl->r_mask & RULE_ATTR_DSFIELD)
+               rtm.rtm_tos = tmpl->r_dsfield;
+
+       if (tmpl->r_mask & RULE_ATTR_TYPE)
+               rtm.rtm_type = tmpl->r_type;
+
+       if (tmpl->r_mask & RULE_ATTR_SRC_LEN)
+               rtm.rtm_src_len = tmpl->r_src_len;
+
+       if (tmpl->r_mask & RULE_ATTR_DST_LEN)
+               rtm.rtm_dst_len = tmpl->r_dst_len;
+
+       msg = nlmsg_build_simple(cmd, flags);
+       if (!msg)
+               goto nla_put_failure;
+
+       if (nlmsg_append(msg, &rtm, sizeof(rtm), 1) < 0)
+               goto nla_put_failure;
+
+       if (tmpl->r_mask & RULE_ATTR_SRC)
+               NLA_PUT_ADDR(msg, RTA_SRC, tmpl->r_src);
+
+       if (tmpl->r_mask & RULE_ATTR_DST)
+               NLA_PUT_ADDR(msg, RTA_DST, tmpl->r_dst);
+
+       if (tmpl->r_mask & RULE_ATTR_PRIO)
+               NLA_PUT_U32(msg, RTA_PRIORITY, tmpl->r_prio);
+
+       if (tmpl->r_mask & RULE_ATTR_FWMARK)
+               NLA_PUT_U32(msg, RTA_PROTOINFO, tmpl->r_fwmark);
+
+       if (tmpl->r_mask & RULE_ATTR_REALMS)
+               NLA_PUT_U32(msg, RTA_FLOW, tmpl->r_realms);
+
+       if (tmpl->r_mask & RULE_ATTR_IIF)
+               NLA_PUT_STRING(msg, RTA_IIF, tmpl->r_iif);
+
+       return msg;
+
+nla_put_failure:
+       nlmsg_free(msg);
+       return NULL;
+}
+
+/**
+ * Build netlink request message to add a new rule
+ * @arg tmpl           template with data of new rule
+ * @arg flags          additional netlink message flags
+ *
+ * Builds a new netlink message requesting a addition of a new
+ * rule. The netlink message header isn't fully equipped with
+ * all relevant fields and must thus be sent out via nl_send_auto_complete()
+ * or supplemented as needed. \a tmpl must contain the attributes of the new
+ * address set via \c rtnl_rule_set_* functions.
+ * 
+ * @return The netlink message
+ */
+struct nl_msg *rtnl_rule_build_add_request(struct rtnl_rule *tmpl, int flags)
+{
+       return build_rule_msg(tmpl, RTM_NEWRULE, NLM_F_CREATE | flags);
+}
+
+/**
+ * Add a new rule
+ * @arg handle         netlink handle
+ * @arg tmpl           template with requested changes
+ * @arg flags          additional netlink message flags
+ *
+ * Builds a netlink message by calling rtnl_rule_build_add_request(),
+ * sends the request to the kernel and waits for the next ACK to be
+ * received and thus blocks until the request has been fullfilled.
+ *
+ * @return 0 on sucess or a negative error if an error occured.
+ */
+int rtnl_rule_add(struct nl_handle *handle, struct rtnl_rule *tmpl, int flags)
+{
+       int err;
+       struct nl_msg *msg;
+       
+       msg = rtnl_rule_build_add_request(tmpl, flags);
+       if (!msg)
+               return nl_errno(ENOMEM);
+
+       err = nl_send_auto_complete(handle, msg);
+       if (err < 0)
+               return err;
+
+       nlmsg_free(msg);
+       return nl_wait_for_ack(handle);
+}
+
+/** @} */
+
+/**
+ * @name Rule Deletion
+ * @{
+ */
+
+/**
+ * Build a netlink request message to delete a rule
+ * @arg rule           rule to delete
+ * @arg flags          additional netlink message flags
+ *
+ * Builds a new netlink message requesting a deletion of a rule.
+ * The netlink message header isn't fully equipped with all relevant
+ * fields and must thus be sent out via nl_send_auto_complete()
+ * or supplemented as needed. \a rule must point to an existing
+ * address.
+ *
+ * @return The netlink message
+ */
+struct nl_msg *rtnl_rule_build_delete_request(struct rtnl_rule *rule, int flags)
+{
+       return build_rule_msg(rule, RTM_DELRULE, flags);
+}
+
+/**
+ * Delete a rule
+ * @arg handle         netlink handle
+ * @arg rule           rule to delete
+ * @arg flags          additional netlink message flags
+ *
+ * Builds a netlink message by calling rtnl_rule_build_delete_request(),
+ * sends the request to the kernel and waits for the next ACK to be
+ * received and thus blocks until the request has been fullfilled.
+ *
+ * @return 0 on sucess or a negative error if an error occured.
+ */
+int rtnl_rule_delete(struct nl_handle *handle, struct rtnl_rule *rule,
+                    int flags)
+{
+       int err;
+       struct nl_msg *msg;
+       
+       msg = rtnl_rule_build_delete_request(rule, flags);
+       if (!msg)
+               return nl_errno(ENOMEM);
+
+       err = nl_send_auto_complete(handle, msg);
+       if (err < 0)
+               return err;
+
+       nlmsg_free(msg);
+       return nl_wait_for_ack(handle);
+}
+
+/** @} */
+
+/**
+ * @name Attribute Modification
+ * @{
+ */
+
+/**
+ * Set the address family of a rule to the specified value
+ * @arg rule           rule to change
+ * @arg family         new address family
+ */
+void rtnl_rule_set_family(struct rtnl_rule *rule, int family)
+{
+       rule->r_family = family;
+       rule->r_mask |= RULE_ATTR_FAMILY;
+}
+
+/**
+ * Get the address family of a rule
+ * @arg rule           rule handle
+ * @return Address family or AF_UNSPEC if not set
+ */
+int rtnl_rule_get_family(struct rtnl_rule *rule)
+{
+       if (rule->r_mask & RULE_ATTR_FAMILY)
+               return rule->r_family;
+       else
+               return AF_UNSPEC;
+}
+
+/**
+ * Set the priority of a rule to the specified value
+ * @arg rule           rule to change
+ * @arg prio           new priority
+ */
+void rtnl_rule_set_prio(struct rtnl_rule *rule, int prio)
+{
+       rule->r_prio = prio;
+       rule->r_mask |= RULE_ATTR_PRIO;
+}
+
+/**
+ * Get the priority of a rule
+ * @arg rule           rule handle
+ * @return Priority or -1 if not set
+ */
+int rtnl_rule_get_prio(struct rtnl_rule *rule)
+{
+       if (rule->r_mask & RULE_ATTR_PRIO)
+               return rule->r_prio;
+       else
+               return -1;
+}
+
+/**
+ * Set the firewall mark of a rule to the specified value
+ * @arg rule           rule to change
+ * @arg fwmark         new firewall mark
+ */
+void rtnl_rule_set_fwmark(struct rtnl_rule *rule, uint64_t fwmark)
+{
+       rule->r_fwmark = fwmark;
+       rule->r_mask |= RULE_ATTR_FWMARK;
+}
+
+/**
+ * Get the firewall mark of a rule
+ * @arg rule           rule handle
+ * @return Firewall mark or UINT_LEAST64_MAX if not set
+ */
+uint64_t rtnl_rule_get_fwmark(struct rtnl_rule *rule)
+{
+       if (rule->r_mask & RULE_ATTR_FWMARK)
+               return rule->r_fwmark;
+       else
+               return UINT_LEAST64_MAX;
+}
+
+/**
+ * Set the table index of a rule to the specified value
+ * @arg rule           rule to change
+ * @arg table          new table
+ */
+void rtnl_rule_set_table(struct rtnl_rule *rule, int table)
+{
+       rule->r_table = table;
+       rule->r_mask |= RULE_ATTR_TABLE;
+}
+
+/**
+ * Get the table index of a rule
+ * @arg rule           rule handle
+ * @return Table index or -1 if not set
+ */
+int rtnl_rule_get_table(struct rtnl_rule *rule)
+{
+       if (rule->r_mask & RULE_ATTR_TABLE)
+               return rule->r_table;
+       else
+               return -1;
+}
+
+/**
+ * Set the dsfield of a rule to the specified value
+ * @arg rule           rule to change
+ * @arg dsfield                new dsfield value
+ */
+void rtnl_rule_set_dsfield(struct rtnl_rule *rule, int dsfield)
+{
+       rule->r_dsfield = dsfield;
+       rule->r_mask |= RULE_ATTR_DSFIELD;
+}
+
+/**
+ * Get the dsfield of a rule
+ * @arg rule           rule handle
+ * @return dsfield or -1 if not set
+ */
+int rtnl_rule_get_dsfield(struct rtnl_rule *rule)
+{
+       if (rule->r_mask & RULE_ATTR_DSFIELD)
+               return rule->r_dsfield;
+       else
+               return -1;
+}
+
+/**
+ * Set the source address prefix length of a rule to the specified value
+ * @arg rule           rule to change
+ * @arg len            new source address length
+ */
+void rtnl_rule_set_src_len(struct rtnl_rule *rule, int len)
+{
+       rule->r_src_len = len;
+       if (rule->r_mask & RULE_ATTR_SRC)
+               nl_addr_set_prefixlen(rule->r_src, len);
+       rule->r_mask |= RULE_ATTR_SRC_LEN;
+}
+
+/**
+ * Get the source address prefix length of a rule
+ * @arg rule           rule handle
+ * @return Prefix length of source address or -1 if not set
+ */
+int rtnl_rule_get_src_len(struct rtnl_rule *rule)
+{
+       if (rule->r_mask & RULE_ATTR_SRC_LEN)
+               return rule->r_src_len;
+       else
+               return -1;
+}
+
+/**
+ * Set the destination address prefix length of a rule to the specified value
+ * @arg rule           rule to change
+ * @arg len            new destination address length
+ */
+void rtnl_rule_set_dst_len(struct rtnl_rule *rule, int len)
+{
+       rule->r_dst_len = len;
+       if (rule->r_mask & RULE_ATTR_DST)
+               nl_addr_set_prefixlen(rule->r_dst, len);
+       rule->r_mask |= RULE_ATTR_DST_LEN;
+}
+
+/**
+ * Get the destination address prefix length of a rule
+ * @arg rule           rule handle
+ * @return Prefix length of destination address or -1 if not set
+ */
+int rtnl_rule_get_dst_len(struct rtnl_rule *rule)
+{
+       if (rule->r_mask & RULE_ATTR_DST_LEN)
+               return rule->r_dst_len;
+       else
+               return -1;
+}
+
+static inline int __assign_addr(struct rtnl_rule *rule, struct nl_addr **pos,
+                               struct nl_addr *new, uint8_t *len, int flag)
+{
+       if (rule->r_mask & RULE_ATTR_FAMILY) {
+               if (new->a_family != rule->r_family)
+                       return nl_error(EINVAL, "Address family mismatch");
+       } else
+               rule->r_family = new->a_family;
+
+       if (*pos)
+               nl_addr_put(*pos);
+
+       nl_addr_get(new);
+       *pos = new;
+       *len = nl_addr_get_prefixlen(new);
+
+       rule->r_mask |= (flag | RULE_ATTR_FAMILY);
+
+       return 0;
+}
+/**
+ * Set the source address of a rule
+ * @arg rule           rule to change
+ * @arg src            new source address
+ *
+ * Assigns the new source address to the specified rule handle. The
+ * address is validated against the address family if set already via
+ * either rtnl_rule_set_family() or by setting one of the other addresses.
+ * The assignment fails if the address families mismatch. In case the
+ * address family has not been specified yet, the address family of this
+ * new address is elected to be the requirement.
+ * 
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_rule_set_src(struct rtnl_rule *rule, struct nl_addr *src)
+{
+       return __assign_addr(rule, &rule->r_src, src, &rule->r_src_len,
+                            RULE_ATTR_SRC | RULE_ATTR_SRC_LEN);
+}
+
+/**
+ * Get the source address of a rule
+ * @arg rule           rule handle
+ * @return Source address or NULL if not set
+ */
+struct nl_addr *rtnl_rule_get_src(struct rtnl_rule *rule)
+{
+       if (rule->r_mask & RULE_ATTR_SRC)
+               return rule->r_src;
+       else
+               return NULL;
+}
+
+/**
+ * Set the destination address of a rule
+ * @arg rule           rule to change
+ * @arg dst            new destination address
+ *
+ * Assigns the new destination address to the specified rule handle. The
+ * address is validated against the address family if set already via
+ * either rtnl_rule_set_family() or by setting one of the other addresses.
+ * The assignment fails if the address families mismatch. In case the
+ * address family has not been specified yet, the address family of this
+ * new address is elected to be the requirement.
+ * 
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_rule_set_dst(struct rtnl_rule *rule, struct nl_addr *dst)
+{
+       return __assign_addr(rule, &rule->r_dst, dst, &rule->r_dst_len,
+                            RULE_ATTR_DST | RULE_ATTR_DST_LEN);
+}
+
+/**
+ * Get the destination address of a rule
+ * @arg rule           rule handle
+ * @return Destination address or NULL if not set
+ */
+struct nl_addr *rtnl_rule_get_dst(struct rtnl_rule *rule)
+{
+       if (rule->r_mask & RULE_ATTR_DST)
+               return rule->r_dst;
+       else
+               return NULL;
+}
+
+/**
+ * Set incoming interface of routing rule object.
+ * @arg rule           Routing rule object to be modified.
+ * @arg dev            Name of incoming interface.
+ *
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_rule_set_iif(struct rtnl_rule *rule, const char *dev)
+{
+       if (strlen(dev) > IFNAMSIZ-1)
+               return nl_errno(ERANGE);
+
+       strcpy(rule->r_iif, dev);
+       rule->r_mask |= RULE_ATTR_IIF;
+       return 0;
+}
+
+/**
+ * Get incoming interface of routing rule object.
+ * @arg rule           Routing rule object.
+ * @return Name of incoming interface or NULL if not available.
+ */
+char *rtnl_rule_get_iif(struct rtnl_rule *rule)
+{
+       if (rule->r_mask & RULE_ATTR_IIF)
+               return rule->r_iif;
+       else
+               return NULL;
+}
+
+/**
+ * Set action of routing rule object.
+ * @arg rule           Routing rule object to be modified.
+ * @arg type           New routing type specifying an action.
+ */
+void rtnl_rule_set_action(struct rtnl_rule *rule, int type)
+{
+       rule->r_type = type;
+       rule->r_mask |= RULE_ATTR_TYPE;
+}
+
+/**
+ * Get action of routing rule object.
+ * @arg rule           Routing rule object.
+ * @return Routing type or a negative error code.
+ */
+int rtnl_rule_get_action(struct rtnl_rule *rule)
+{
+       if (rule->r_mask & RULE_ATTR_TYPE)
+               return rule->r_type;
+       else
+               return nl_errno(ENOENT);
+}
+
+/**
+ * Set realms of routing rule object.
+ * @arg rule           Routing rule object to be modified.
+ * @arg realms         New realms value.
+ */
+void rtnl_rule_set_realms(struct rtnl_rule *rule, realm_t realms)
+{
+       rule->r_realms = realms;
+       rule->r_mask |= RULE_ATTR_REALMS;
+}
+
+/**
+ * Get realms of routing rule object.
+ * @arg rule           Routing rule object.
+ * @return Realms value or 0 if not set.
+ */
+realm_t rtnl_rule_get_realms(struct rtnl_rule *rule)
+{
+       if (rule->r_mask & RULE_ATTR_REALMS)
+               return rule->r_realms;
+       else
+               return 0;
+}
+
+/** @} */
+
+static struct nl_cache_ops rtnl_rule_ops = {
+       .co_name                = "route/rule",
+       .co_size                = sizeof(struct rtnl_rule),
+       .co_hdrsize             = sizeof(struct rtmsg),
+       .co_msgtypes            = {
+                                       { RTM_NEWRULE, "new" },
+                                       { RTM_DELRULE, "delete" },
+                                       { RTM_GETRULE, "get" },
+                                       { -1, NULL },
+                                 },
+       .co_protocol            = NETLINK_ROUTE,
+       .co_request_update      = rule_request_update,
+       .co_msg_parser          = rule_msg_parser,
+       .co_free_data           = rule_free_data,
+       .co_dump[NL_DUMP_BRIEF] = rule_dump_brief,
+       .co_dump[NL_DUMP_FULL]  = rule_dump_full,
+       .co_dump[NL_DUMP_STATS] = rule_dump_stats,
+       .co_dump[NL_DUMP_XML]   = rule_dump_xml,
+       .co_dump[NL_DUMP_ENV]   = rule_dump_env,
+       .co_filter              = rule_filter,
+};
+
+static void __init rule_init(void)
+{
+       nl_cache_mngt_register(&rtnl_rule_ops);
+}
+
+static void __exit rule_exit(void)
+{
+       nl_cache_mngt_unregister(&rtnl_rule_ops);
+}
+
+/** @} */
diff --git a/lib/route/sch/blackhole.c b/lib/route/sch/blackhole.c
new file mode 100644 (file)
index 0000000..207f0bd
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * lib/route/sch/blackhole.c   Blackhole Qdisc
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup qdisc
+ * @defgroup blackhole Blackhole
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink-tc.h>
+#include <netlink/netlink.h>
+#include <netlink/route/qdisc.h>
+#include <netlink/route/qdisc-modules.h>
+
+static struct rtnl_qdisc_ops blackhole_ops = {
+       .qo_kind                = "blackhole",
+};
+
+static void __init blackhole_init(void)
+{
+       rtnl_qdisc_register(&blackhole_ops);
+}
+
+static void __exit blackhole_exit(void)
+{
+       rtnl_qdisc_unregister(&blackhole_ops);
+}
+
+/** @} */
diff --git a/lib/route/sch/cbq.c b/lib/route/sch/cbq.c
new file mode 100644 (file)
index 0000000..59f6951
--- /dev/null
@@ -0,0 +1,284 @@
+/*
+ * lib/route/sch/cbq.c Class Based Queueing
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include <netlink-local.h>
+#include <netlink-tc.h>
+#include <netlink/netlink.h>
+#include <netlink/utils.h>
+#include <netlink/route/qdisc.h>
+#include <netlink/route/qdisc-modules.h>
+#include <netlink/route/class.h>
+#include <netlink/route/class-modules.h>
+#include <netlink/route/link.h>
+#include <netlink/route/sch/cbq.h>
+#include <netlink/route/cls/police.h>
+
+/**
+ * @ingroup qdisc
+ * @ingroup class
+ * @defgroup cbq Class Based Queueing (CBQ)
+ * @{
+ */
+
+static struct trans_tbl ovl_strategies[] = {
+       __ADD(TC_CBQ_OVL_CLASSIC,classic)
+       __ADD(TC_CBQ_OVL_DELAY,delay)
+       __ADD(TC_CBQ_OVL_LOWPRIO,lowprio)
+       __ADD(TC_CBQ_OVL_DROP,drop)
+       __ADD(TC_CBQ_OVL_RCLASSIC,rclassic)
+};
+
+/**
+ * Convert a CBQ OVL strategy to a character string
+ * @arg type           CBQ OVL strategy
+ * @arg buf            destination buffer
+ * @arg len            length of destination buffer
+ *
+ * Converts a CBQ OVL strategy to a character string and stores in the
+ * provided buffer. Returns the destination buffer or the type
+ * encoded in hex if no match was found.
+ */
+char *nl_ovl_strategy2str(int type, char *buf, size_t len)
+{
+       return __type2str(type, buf, len, ovl_strategies,
+                           ARRAY_SIZE(ovl_strategies));
+}
+
+/**
+ * Convert a string to a CBQ OVL strategy
+ * @arg name           CBQ OVL stragegy name
+ *
+ * Converts a CBQ OVL stragegy name to it's corresponding CBQ OVL strategy
+ * type. Returns the type or -1 if none was found.
+ */
+int nl_str2ovl_strategy(const char *name)
+{
+       return __str2type(name, ovl_strategies, ARRAY_SIZE(ovl_strategies));
+}
+
+static struct nla_policy cbq_policy[TCA_CBQ_MAX+1] = {
+       [TCA_CBQ_LSSOPT]        = { .minlen = sizeof(struct tc_cbq_lssopt) },
+       [TCA_CBQ_RATE]          = { .minlen = sizeof(struct tc_ratespec) },
+       [TCA_CBQ_WRROPT]        = { .minlen = sizeof(struct tc_cbq_wrropt) },
+       [TCA_CBQ_OVL_STRATEGY]  = { .minlen = sizeof(struct tc_cbq_ovl) },
+       [TCA_CBQ_FOPT]          = { .minlen = sizeof(struct tc_cbq_fopt) },
+       [TCA_CBQ_POLICE]        = { .minlen = sizeof(struct tc_cbq_police) },
+};
+
+static inline struct rtnl_cbq *cbq_qdisc(struct rtnl_tca *tca)
+{
+       return (struct rtnl_cbq *) tca->tc_subdata;
+}
+
+static inline struct rtnl_cbq *cbq_alloc(struct rtnl_tca *tca)
+{
+       if (!tca->tc_subdata)
+               tca->tc_subdata = calloc(1, sizeof(struct rtnl_qdisc));
+
+       return cbq_qdisc(tca);
+}
+
+
+static int cbq_msg_parser(struct rtnl_tca *tca)
+{
+       struct nlattr *tb[TCA_CBQ_MAX + 1];
+       struct rtnl_cbq *cbq;
+       int err;
+
+       err = tca_parse(tb, TCA_CBQ_MAX, tca, cbq_policy);
+       if (err < 0)
+               return err;
+
+       cbq = cbq_alloc(tca);
+       if (!cbq)
+               return nl_errno(ENOMEM);
+
+       nla_memcpy(&cbq->cbq_lss, tb[TCA_CBQ_LSSOPT], sizeof(cbq->cbq_lss));
+       nla_memcpy(&cbq->cbq_rate, tb[TCA_CBQ_RATE], sizeof(cbq->cbq_rate));
+       nla_memcpy(&cbq->cbq_wrr, tb[TCA_CBQ_WRROPT], sizeof(cbq->cbq_wrr));
+       nla_memcpy(&cbq->cbq_fopt, tb[TCA_CBQ_FOPT], sizeof(cbq->cbq_fopt));
+       nla_memcpy(&cbq->cbq_ovl, tb[TCA_CBQ_OVL_STRATEGY],
+                  sizeof(cbq->cbq_ovl));
+       nla_memcpy(&cbq->cbq_police, tb[TCA_CBQ_POLICE],
+                   sizeof(cbq->cbq_police));
+       
+       return 0;
+}
+
+static int cbq_qdisc_msg_parser(struct rtnl_qdisc *qdisc)
+{
+       return cbq_msg_parser((struct rtnl_tca *) qdisc);
+}
+
+static int cbq_class_msg_parser(struct rtnl_class *class)
+{
+       return cbq_msg_parser((struct rtnl_tca *) class);
+}
+
+static void cbq_qdisc_free_data(struct rtnl_qdisc *qdisc)
+{
+       free(qdisc->q_subdata);
+}
+
+static void cbq_class_free_data(struct rtnl_class *class)
+{
+       free(class->c_subdata);
+}
+
+static int cbq_dump_brief(struct rtnl_tca *tca, struct nl_dump_params *p,
+                         int line)
+{
+       struct rtnl_cbq *cbq;
+       double r, rbit;
+       char *ru, *rubit;
+
+       cbq = cbq_qdisc(tca);
+       if (!cbq)
+               goto ignore;
+
+       r = nl_cancel_down_bytes(cbq->cbq_rate.rate, &ru);
+       rbit = nl_cancel_down_bits(cbq->cbq_rate.rate * 8, &rubit);
+
+       dp_dump(p, " rate %.2f%s/s (%.0f%s) prio %u",
+               r, ru, rbit, rubit, cbq->cbq_wrr.priority);
+
+ignore:
+       return line;
+}
+
+static int cbq_qdisc_dump_brief(struct rtnl_qdisc *qdisc,
+                               struct nl_dump_params *p, int line)
+{
+       return cbq_dump_brief((struct rtnl_tca *) qdisc, p, line);
+}
+
+static int cbq_class_dump_brief(struct rtnl_class *class,
+                               struct nl_dump_params *p, int line)
+{
+       return cbq_dump_brief((struct rtnl_tca *) class, p, line);
+}
+
+static int cbq_dump_full(struct rtnl_tca *tca, struct nl_dump_params *p,
+                        int line)
+{
+       struct rtnl_cbq *cbq;
+       char *unit, buf[32];
+       double w;
+       uint32_t el;
+
+       cbq = cbq_qdisc(tca);
+       if (!cbq)
+               goto ignore;
+
+       w = nl_cancel_down_bits(cbq->cbq_wrr.weight * 8, &unit);
+
+       dp_dump(p, "avgpkt %u mpu %u cell %u allot %u weight %.0f%s\n",
+               cbq->cbq_lss.avpkt,
+               cbq->cbq_rate.mpu,
+               1 << cbq->cbq_rate.cell_log,
+               cbq->cbq_wrr.allot, w, unit);
+
+       el = cbq->cbq_lss.ewma_log;
+       dp_dump_line(p, line++, "  minidle %uus maxidle %uus offtime "
+                               "%uus level %u ewma_log %u\n",
+               nl_ticks2us(cbq->cbq_lss.minidle >> el),
+               nl_ticks2us(cbq->cbq_lss.maxidle >> el),
+               nl_ticks2us(cbq->cbq_lss.offtime >> el),
+               cbq->cbq_lss.level,
+               cbq->cbq_lss.ewma_log);
+
+       dp_dump_line(p, line++, "  penalty %uus strategy %s ",
+               nl_ticks2us(cbq->cbq_ovl.penalty),
+               nl_ovl_strategy2str(cbq->cbq_ovl.strategy, buf, sizeof(buf)));
+
+       dp_dump(p, "split %s defmap 0x%08x ",
+               rtnl_tc_handle2str(cbq->cbq_fopt.split, buf, sizeof(buf)),
+               cbq->cbq_fopt.defmap);
+       
+       dp_dump(p, "police %s",
+               nl_police2str(cbq->cbq_police.police, buf, sizeof(buf)));
+
+ignore:
+       return line;
+}
+
+static int cbq_qdisc_dump_full(struct rtnl_qdisc *qdisc,
+                              struct nl_dump_params *p, int line)
+{
+       return cbq_dump_full((struct rtnl_tca *) qdisc, p, line);
+}
+
+static int cbq_class_dump_full(struct rtnl_class *class,
+                              struct nl_dump_params *p, int line)
+{
+       return cbq_dump_full((struct rtnl_tca *) class, p, line);
+}
+
+static int cbq_dump_with_stats(struct rtnl_tca *tca, struct nl_dump_params *p,
+                              int line)
+{
+       struct tc_cbq_xstats *x = tca_xstats(tca);
+
+       if (!x)
+               goto ignore;
+
+       dp_dump_line(p, line++, "            borrows    overact  "
+                               "  avgidle  undertime\n");
+       dp_dump_line(p, line++, "         %10u %10u %10u %10u\n",
+                    x->borrows, x->overactions, x->avgidle, x->undertime);
+
+ignore:
+       return line;
+}
+
+static int cbq_qdisc_dump_with_stats(struct rtnl_qdisc *qdisc,
+                                    struct nl_dump_params *p, int line)
+{
+       return cbq_dump_with_stats((struct rtnl_tca *) qdisc, p, line);
+}
+
+static int cbq_class_dump_with_stats(struct rtnl_class *class,
+                                    struct nl_dump_params *p, int line)
+{
+       return cbq_dump_with_stats((struct rtnl_tca *) class, p, line);
+}
+
+static struct rtnl_qdisc_ops cbq_qdisc_ops = {
+       .qo_kind                = "cbq",
+       .qo_msg_parser          = cbq_qdisc_msg_parser,
+       .qo_free_data           = cbq_qdisc_free_data,
+       .qo_dump[NL_DUMP_BRIEF] = cbq_qdisc_dump_brief,
+       .qo_dump[NL_DUMP_FULL]  = cbq_qdisc_dump_full,
+       .qo_dump[NL_DUMP_STATS] = cbq_qdisc_dump_with_stats,
+};
+
+static struct rtnl_class_ops cbq_class_ops = {
+       .co_kind                = "cbq",
+       .co_msg_parser          = cbq_class_msg_parser,
+       .co_free_data           = cbq_class_free_data,
+       .co_dump[NL_DUMP_BRIEF] = cbq_class_dump_brief,
+       .co_dump[NL_DUMP_FULL]  = cbq_class_dump_full,
+       .co_dump[NL_DUMP_STATS] = cbq_class_dump_with_stats,
+};
+
+static void __init cbq_init(void)
+{
+       rtnl_qdisc_register(&cbq_qdisc_ops);
+       rtnl_class_register(&cbq_class_ops);
+}
+
+static void __exit cbq_exit(void)
+{
+       rtnl_qdisc_unregister(&cbq_qdisc_ops);
+       rtnl_class_unregister(&cbq_class_ops);
+}
+
+/** @} */
diff --git a/lib/route/sch/dsmark.c b/lib/route/sch/dsmark.c
new file mode 100644 (file)
index 0000000..6ffa039
--- /dev/null
@@ -0,0 +1,459 @@
+/*
+ * lib/route/sch/dsmark.c      DSMARK
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup qdisc
+ * @ingroup class
+ * @defgroup dsmark Differentiated Services Marker (DSMARK)
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink-tc.h>
+#include <netlink/netlink.h>
+#include <netlink/utils.h>
+#include <netlink/route/qdisc.h>
+#include <netlink/route/qdisc-modules.h>
+#include <netlink/route/class.h>
+#include <netlink/route/class-modules.h>
+#include <netlink/route/sch/dsmark.h>
+
+/** @cond SKIP */
+#define SCH_DSMARK_ATTR_INDICES                0x1
+#define SCH_DSMARK_ATTR_DEFAULT_INDEX  0x2
+#define SCH_DSMARK_ATTR_SET_TC_INDEX   0x4
+
+#define SCH_DSMARK_ATTR_MASK           0x1
+#define SCH_DSMARK_ATTR_VALUE          0x2
+/** @endcond */
+
+static inline struct rtnl_dsmark_qdisc *dsmark_qdisc(struct rtnl_qdisc *qdisc)
+{
+       return (struct rtnl_dsmark_qdisc *) qdisc->q_subdata;
+}
+
+static inline struct rtnl_dsmark_qdisc *
+dsmark_qdisc_alloc(struct rtnl_qdisc *qdisc)
+{
+       if (!qdisc->q_subdata)
+               qdisc->q_subdata = calloc(1, sizeof(struct rtnl_dsmark_qdisc));
+
+       return dsmark_qdisc(qdisc);
+}
+
+static struct nla_policy dsmark_policy[TCA_DSMARK_MAX+1] = {
+       [TCA_DSMARK_INDICES]            = { .type = NLA_U16 },
+       [TCA_DSMARK_DEFAULT_INDEX]      = { .type = NLA_U16 },
+       [TCA_DSMARK_SET_TC_INDEX]       = { .type = NLA_FLAG },
+       [TCA_DSMARK_VALUE]              = { .type = NLA_U8 },
+       [TCA_DSMARK_MASK]               = { .type = NLA_U8 },
+};
+
+static int dsmark_qdisc_msg_parser(struct rtnl_qdisc *qdisc)
+{
+       int err;
+       struct nlattr *tb[TCA_DSMARK_MAX + 1];
+       struct rtnl_dsmark_qdisc *dsmark;
+
+       err = tca_parse(tb, TCA_DSMARK_MAX, (struct rtnl_tca *) qdisc,
+                       dsmark_policy);
+       if (err < 0)
+               return err;
+
+       dsmark = dsmark_qdisc_alloc(qdisc);
+       if (!dsmark)
+               return nl_errno(ENOMEM);
+
+       if (tb[TCA_DSMARK_INDICES]) {
+               dsmark->qdm_indices = nla_get_u16(tb[TCA_DSMARK_INDICES]);
+               dsmark->qdm_mask |= SCH_DSMARK_ATTR_INDICES;
+       }
+
+       if (tb[TCA_DSMARK_DEFAULT_INDEX]) {
+               dsmark->qdm_default_index =
+                               nla_get_u16(tb[TCA_DSMARK_DEFAULT_INDEX]);
+               dsmark->qdm_mask |= SCH_DSMARK_ATTR_DEFAULT_INDEX;
+       }
+
+       if (tb[TCA_DSMARK_SET_TC_INDEX]) {
+               dsmark->qdm_set_tc_index = 1;
+               dsmark->qdm_mask |= SCH_DSMARK_ATTR_SET_TC_INDEX;
+       }
+
+       return 0;
+}
+
+static inline struct rtnl_dsmark_class *dsmark_class(struct rtnl_class *class)
+{
+       return (struct rtnl_dsmark_class *) class->c_subdata;
+}
+
+static inline struct rtnl_dsmark_class *
+dsmark_class_alloc(struct rtnl_class *class)
+{
+       if (!class->c_subdata)
+               class->c_subdata = calloc(1, sizeof(struct rtnl_dsmark_class));
+
+       return dsmark_class(class);
+}
+
+static int dsmark_class_msg_parser(struct rtnl_class *class)
+{
+       int err;
+       struct nlattr *tb[TCA_DSMARK_MAX + 1];
+       struct rtnl_dsmark_class *dsmark;
+
+       err = tca_parse(tb, TCA_DSMARK_MAX, (struct rtnl_tca *) class,
+                       dsmark_policy);
+       if (err < 0)
+               return err;
+
+       dsmark = dsmark_class_alloc(class);
+       if (!dsmark)
+               return nl_errno(ENOMEM);
+
+       if (tb[TCA_DSMARK_MASK]) {
+               dsmark->cdm_bmask = nla_get_u8(tb[TCA_DSMARK_MASK]);
+               dsmark->cdm_mask |= SCH_DSMARK_ATTR_MASK;
+       }
+
+       if (tb[TCA_DSMARK_VALUE]) {
+               dsmark->cdm_value = nla_get_u8(tb[TCA_DSMARK_VALUE]);
+               dsmark->cdm_mask |= SCH_DSMARK_ATTR_VALUE;
+       }
+
+       return 0;
+}
+
+static int dsmark_qdisc_dump_brief(struct rtnl_qdisc *qdisc,
+                                  struct nl_dump_params *p, int line)
+{
+       struct rtnl_dsmark_qdisc *dsmark = dsmark_qdisc(qdisc);
+
+       if (dsmark && (dsmark->qdm_mask & SCH_DSMARK_ATTR_INDICES))
+               dp_dump(p, " indices 0x%04x", dsmark->qdm_indices);
+
+       return line;
+}
+
+static int dsmark_qdisc_dump_full(struct rtnl_qdisc *qdisc,
+                                 struct nl_dump_params *p, int line)
+{
+       struct rtnl_dsmark_qdisc *dsmark = dsmark_qdisc(qdisc);
+
+       if (!dsmark)
+               goto ignore;
+
+       if (dsmark->qdm_mask & SCH_DSMARK_ATTR_DEFAULT_INDEX)
+               dp_dump(p, " default index 0x%04x", dsmark->qdm_default_index);
+
+       if (dsmark->qdm_mask & SCH_DSMARK_ATTR_SET_TC_INDEX)
+               dp_dump(p, " set-tc-index");
+
+ignore:
+       return line;
+}
+
+static int dsmark_class_dump_brief(struct rtnl_class *class,
+                                  struct nl_dump_params *p, int line)
+{
+       struct rtnl_dsmark_class *dsmark = dsmark_class(class);
+
+       if (!dsmark)
+               goto ignore;
+
+       if (dsmark->cdm_mask & SCH_DSMARK_ATTR_VALUE)
+               dp_dump(p, " value 0x%02x", dsmark->cdm_value);
+
+       if (dsmark->cdm_mask & SCH_DSMARK_ATTR_MASK)
+               dp_dump(p, " mask 0x%02x", dsmark->cdm_bmask);
+
+ignore:
+       return line;
+}
+
+static struct nl_msg *dsmark_qdisc_get_opts(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_dsmark_qdisc *dsmark = dsmark_qdisc(qdisc);
+       struct nl_msg *msg;
+
+       if (!dsmark)
+               return NULL;
+
+       msg = nlmsg_build_no_hdr();
+       if (!msg)
+               goto nla_put_failure;
+
+       if (dsmark->qdm_mask & SCH_DSMARK_ATTR_INDICES)
+               NLA_PUT_U16(msg, TCA_DSMARK_INDICES, dsmark->qdm_indices);
+
+       if (dsmark->qdm_mask & SCH_DSMARK_ATTR_DEFAULT_INDEX)
+               NLA_PUT_U16(msg, TCA_DSMARK_DEFAULT_INDEX,
+                           dsmark->qdm_default_index);
+
+       if (dsmark->qdm_mask & SCH_DSMARK_ATTR_SET_TC_INDEX)
+               NLA_PUT_FLAG(msg, TCA_DSMARK_SET_TC_INDEX);
+
+       return msg;
+
+nla_put_failure:
+       nlmsg_free(msg);
+       return NULL;
+}
+
+static struct nl_msg *dsmark_class_get_opts(struct rtnl_class *class)
+{
+       struct rtnl_dsmark_class *dsmark = dsmark_class(class);
+       struct nl_msg *msg;
+
+       if (!dsmark)
+               return NULL;
+
+       msg = nlmsg_build_no_hdr();
+       if (!msg)
+               goto nla_put_failure;
+
+       if (dsmark->cdm_mask & SCH_DSMARK_ATTR_MASK)
+               NLA_PUT_U8(msg, TCA_DSMARK_MASK, dsmark->cdm_bmask);
+
+       if (dsmark->cdm_mask & SCH_DSMARK_ATTR_VALUE)
+               NLA_PUT_U8(msg, TCA_DSMARK_VALUE, dsmark->cdm_value);
+
+       return msg;
+
+nla_put_failure:
+       nlmsg_free(msg);
+       return NULL;
+}
+
+/**
+ * @name Class Attribute Access
+ * @{
+ */
+
+/**
+ * Set bitmask of DSMARK class.
+ * @arg class          DSMARK class to be modified.
+ * @arg mask           New bitmask.
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_class_dsmark_set_bitmask(struct rtnl_class *class, uint8_t mask)
+{
+       struct rtnl_dsmark_class *dsmark;
+       
+       dsmark = dsmark_class(class);
+       if (!dsmark)
+               return nl_errno(ENOMEM);
+
+       dsmark->cdm_bmask = mask;
+       dsmark->cdm_mask |= SCH_DSMARK_ATTR_MASK;
+
+       return 0;
+}
+
+/**
+ * Get bitmask of DSMARK class.
+ * @arg class          DSMARK class.
+ * @return Bitmask or a negative error code.
+ */
+int rtnl_class_dsmark_get_bitmask(struct rtnl_class *class)
+{
+       struct rtnl_dsmark_class *dsmark;
+
+       dsmark = dsmark_class(class);
+       if (dsmark && dsmark->cdm_mask & SCH_DSMARK_ATTR_MASK)
+               return dsmark->cdm_bmask;
+       else
+               return nl_errno(ENOENT);
+}
+
+/**
+ * Set value of DSMARK class.
+ * @arg class          DSMARK class to be modified.
+ * @arg value          New value.
+ * @return 0 on success or a negative errror code.
+ */
+int rtnl_class_dsmark_set_value(struct rtnl_class *class, uint8_t value)
+{
+       struct rtnl_dsmark_class *dsmark;
+
+       dsmark = dsmark_class(class);
+       if (!dsmark)
+               return nl_errno(ENOMEM);
+
+       dsmark->cdm_value = value;
+       dsmark->cdm_mask |= SCH_DSMARK_ATTR_VALUE;
+
+       return 0;
+}
+
+/**
+ * Get value of DSMARK class.
+ * @arg class          DSMARK class.
+ * @return Value or a negative error code.
+ */
+int rtnl_class_dsmark_get_value(struct rtnl_class *class)
+{
+       struct rtnl_dsmark_class *dsmark;
+
+       dsmark = dsmark_class(class);
+       if (dsmark && dsmark->cdm_mask & SCH_DSMARK_ATTR_VALUE)
+               return dsmark->cdm_value;
+       else
+               return nl_errno(ENOENT);
+}
+
+/** @} */
+
+/**
+ * @name Qdisc Attribute Access
+ * @{
+ */
+
+/**
+ * Set indices of DSMARK qdisc.
+ * @arg qdisc          DSMARK qdisc to be modified.
+ * @arg indices                New indices.
+ */
+int rtnl_qdisc_dsmark_set_indices(struct rtnl_qdisc *qdisc, uint16_t indices)
+{
+       struct rtnl_dsmark_qdisc *dsmark;
+
+       dsmark = dsmark_qdisc(qdisc);
+       if (!dsmark)
+               return nl_errno(ENOMEM);
+
+       dsmark->qdm_indices = indices;
+       dsmark->qdm_mask |= SCH_DSMARK_ATTR_INDICES;
+
+       return 0;
+}
+
+/**
+ * Get indices of DSMARK qdisc.
+ * @arg qdisc          DSMARK qdisc.
+ * @return Indices or a negative error code.
+ */
+int rtnl_qdisc_dsmark_get_indices(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_dsmark_qdisc *dsmark;
+
+       dsmark = dsmark_qdisc(qdisc);
+       if (dsmark && dsmark->qdm_mask & SCH_DSMARK_ATTR_INDICES)
+               return dsmark->qdm_indices;
+       else
+               return nl_errno(ENOENT);
+}
+
+/**
+ * Set default index of DSMARK qdisc.
+ * @arg qdisc          DSMARK qdisc to be modified.
+ * @arg default_index  New default index.
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_qdisc_dsmark_set_default_index(struct rtnl_qdisc *qdisc,
+                                       uint16_t default_index)
+{
+       struct rtnl_dsmark_qdisc *dsmark;
+
+       dsmark = dsmark_qdisc(qdisc);
+       if (!dsmark)
+               return nl_errno(ENOMEM);
+
+       dsmark->qdm_default_index = default_index;
+       dsmark->qdm_mask |= SCH_DSMARK_ATTR_DEFAULT_INDEX;
+
+       return 0;
+}
+
+/**
+ * Get default index of DSMARK qdisc.
+ * @arg qdisc          DSMARK qdisc.
+ * @return Default index or a negative error code.
+ */
+int rtnl_qdisc_dsmark_get_default_index(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_dsmark_qdisc *dsmark;
+
+       dsmark = dsmark_qdisc(qdisc);
+       if (dsmark && dsmark->qdm_mask & SCH_DSMARK_ATTR_DEFAULT_INDEX)
+               return dsmark->qdm_default_index;
+       else
+               return nl_errno(ENOENT);
+}
+
+/**
+ * Set set-tc-index flag of DSMARK qdisc.
+ * @arg qdisc          DSMARK qdisc to be modified.
+ * @arg flag           Flag indicating whether to enable or disable.
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_qdisc_dsmark_set_set_tc_index(struct rtnl_qdisc *qdisc, int flag)
+{
+       struct rtnl_dsmark_qdisc *dsmark;
+
+       dsmark = dsmark_qdisc(qdisc);
+       if (!dsmark)
+               return nl_errno(ENOMEM);
+
+       dsmark->qdm_set_tc_index = !!flag;
+       dsmark->qdm_mask |= SCH_DSMARK_ATTR_SET_TC_INDEX;
+
+       return 0;
+}
+
+/**
+ * Get set-tc-index flag of DSMARK qdisc.
+ * @arg qdisc          DSMARK qdisc to be modified.
+ * @return 1 or 0 to indicate wehther the flag is enabled or a negative
+ *         error code.
+ */
+int rtnl_qdisc_dsmark_get_set_tc_index(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_dsmark_qdisc *dsmark;
+
+       dsmark = dsmark_qdisc(qdisc);
+       if (dsmark && dsmark->qdm_mask & SCH_DSMARK_ATTR_SET_TC_INDEX)
+               return dsmark->qdm_set_tc_index;
+       else
+               return nl_errno(ENOENT);
+}
+
+/** @} */
+
+static struct rtnl_qdisc_ops dsmark_qdisc_ops = {
+       .qo_kind                = "dsmark",
+       .qo_msg_parser          = dsmark_qdisc_msg_parser,
+       .qo_dump[NL_DUMP_BRIEF] = dsmark_qdisc_dump_brief,
+       .qo_dump[NL_DUMP_FULL]  = dsmark_qdisc_dump_full,
+       .qo_get_opts            = dsmark_qdisc_get_opts,
+};
+
+static struct rtnl_class_ops dsmark_class_ops = {
+       .co_kind                = "dsmark",
+       .co_msg_parser          = dsmark_class_msg_parser,
+       .co_dump[NL_DUMP_BRIEF] = dsmark_class_dump_brief,
+       .co_get_opts            = dsmark_class_get_opts,
+};
+
+static void __init dsmark_init(void)
+{
+       rtnl_qdisc_register(&dsmark_qdisc_ops);
+       rtnl_class_register(&dsmark_class_ops);
+}
+
+static void __exit dsmark_exit(void)
+{
+       rtnl_qdisc_unregister(&dsmark_qdisc_ops);
+       rtnl_class_unregister(&dsmark_class_ops);
+}
+
+/** @} */
diff --git a/lib/route/sch/fifo.c b/lib/route/sch/fifo.c
new file mode 100644 (file)
index 0000000..1d36fca
--- /dev/null
@@ -0,0 +1,205 @@
+/*
+ * lib/route/sch/fifo.c                (p|b)fifo
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup qdisc
+ * @defgroup fifo Packet/Bytes FIFO (pfifo/bfifo)
+ * @brief
+ *
+ * The FIFO qdisc comes in two flavours:
+ * @par bfifo (Byte FIFO)
+ * Allows enqueuing until the currently queued volume in bytes exceeds
+ * the configured limit.backlog contains currently enqueued volume in bytes.
+ *
+ * @par pfifo (Packet FIFO)
+ * Allows enquueing until the currently queued number of packets
+ * exceeds the configured limit.
+ *
+ * The configuration is exactly the same, the decision which of
+ * the two variations is going to be used is made based on the
+ * kind of the qdisc (rtnl_qdisc_set_kind()).
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink-tc.h>
+#include <netlink/netlink.h>
+#include <netlink/route/qdisc.h>
+#include <netlink/route/qdisc-modules.h>
+#include <netlink/route/sch/fifo.h>
+#include <netlink/utils.h>
+
+/** @cond SKIP */
+#define SCH_FIFO_ATTR_LIMIT 1
+/** @endcond */
+
+static inline struct rtnl_fifo *fifo_qdisc(struct rtnl_qdisc *qdisc)
+{
+       return (struct rtnl_fifo *) qdisc->q_subdata;
+}
+
+static inline struct rtnl_fifo *fifo_alloc(struct rtnl_qdisc *qdisc)
+{
+       if (!qdisc->q_subdata)
+               qdisc->q_subdata = calloc(1, sizeof(struct rtnl_fifo));
+
+       return fifo_qdisc(qdisc);
+}
+
+static int fifo_msg_parser(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_fifo *fifo;
+       struct tc_fifo_qopt *opt;
+
+       if (qdisc->q_opts->d_size < sizeof(struct tc_fifo_qopt))
+               return nl_error(EINVAL, "FIFO options size mismatch");
+
+       fifo = fifo_alloc(qdisc);
+       if (!fifo)
+               return nl_errno(ENOMEM);
+
+       opt = (struct tc_fifo_qopt *) qdisc->q_opts->d_data;
+       fifo->qf_limit = opt->limit;
+       fifo->qf_mask = SCH_FIFO_ATTR_LIMIT;
+
+       return 0;
+}
+
+static void fifo_free_data(struct rtnl_qdisc *qdisc)
+{
+       free(qdisc->q_subdata);
+}
+
+static int pfifo_dump_brief(struct rtnl_qdisc *qdisc,
+                           struct nl_dump_params *p, int line)
+{
+       struct rtnl_fifo *fifo = fifo_qdisc(qdisc);
+
+       if (fifo)
+               dp_dump(p, " limit %u packets", fifo->qf_limit);
+
+       return line;
+}
+
+static int bfifo_dump_brief(struct rtnl_qdisc *qdisc,
+                           struct nl_dump_params *p, int line)
+{
+       struct rtnl_fifo *fifo = fifo_qdisc(qdisc);
+
+       if (fifo) {
+               char *unit;
+               double r;
+
+               r = nl_cancel_down_bytes(fifo->qf_limit, &unit);
+               dp_dump(p, " limit %.1f%s", r, unit);
+       }
+
+       return line;
+}
+
+static struct nl_msg *fifo_get_opts(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_fifo *fifo;
+       struct tc_fifo_qopt opts;
+       struct nl_msg *msg;
+
+       fifo = fifo_qdisc(qdisc);
+       if (!fifo || !(fifo->qf_mask & SCH_FIFO_ATTR_LIMIT))
+               return NULL;
+
+       msg = nlmsg_build_no_hdr();
+       if (!msg)
+               goto errout;
+
+       memset(&opts, 0, sizeof(opts));
+       opts.limit = fifo->qf_limit;
+
+       if (nlmsg_append(msg, &opts, sizeof(opts), 0) < 0)
+               goto errout;
+
+       return msg;
+errout:
+       nlmsg_free(msg);
+       return NULL;
+}
+
+/**
+ * @name Attribute Modification
+ * @{
+ */
+
+/**
+ * Set limit of FIFO qdisc.
+ * @arg qdisc          FIFO qdisc to be modified.
+ * @arg limit          New limit.
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_qdisc_fifo_set_limit(struct rtnl_qdisc *qdisc, int limit)
+{
+       struct rtnl_fifo *fifo;
+       
+       fifo = fifo_alloc(qdisc);
+       if (!fifo)
+               return nl_errno(ENOMEM);
+               
+       fifo->qf_limit = limit;
+       fifo->qf_mask |= SCH_FIFO_ATTR_LIMIT;
+
+       return 0;
+}
+
+/**
+ * Get limit of a FIFO qdisc.
+ * @arg qdisc          FIFO qdisc.
+ * @return Numeric limit or a negative error code.
+ */
+int rtnl_qdisc_fifo_get_limit(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_fifo *fifo;
+       
+       fifo = fifo_qdisc(qdisc);
+       if (fifo && fifo->qf_mask & SCH_FIFO_ATTR_LIMIT)
+               return fifo->qf_limit;
+       else
+               return nl_errno(ENOMEM);
+}
+
+/** @} */
+
+static struct rtnl_qdisc_ops pfifo_ops = {
+       .qo_kind                = "pfifo",
+       .qo_msg_parser          = fifo_msg_parser,
+       .qo_free_data           = fifo_free_data,
+       .qo_dump[NL_DUMP_BRIEF] = pfifo_dump_brief,
+       .qo_get_opts            = fifo_get_opts,
+};
+
+static struct rtnl_qdisc_ops bfifo_ops = {
+       .qo_kind                = "bfifo",
+       .qo_msg_parser          = fifo_msg_parser,
+       .qo_free_data           = fifo_free_data,
+       .qo_dump[NL_DUMP_BRIEF] = bfifo_dump_brief,
+       .qo_get_opts            = fifo_get_opts,
+};
+
+static void __init fifo_init(void)
+{
+       rtnl_qdisc_register(&pfifo_ops);
+       rtnl_qdisc_register(&bfifo_ops);
+}
+
+static void __exit fifo_exit(void)
+{
+       rtnl_qdisc_unregister(&pfifo_ops);
+       rtnl_qdisc_unregister(&bfifo_ops);
+}
+
+/** @} */
diff --git a/lib/route/sch/htb.c b/lib/route/sch/htb.c
new file mode 100644 (file)
index 0000000..df641bc
--- /dev/null
@@ -0,0 +1,426 @@
+/*
+ * lib/route/sch/htb.c HTB Qdisc
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ * Copyright (c) 2005-2006 Petr Gotthard <petr.gotthard@siemens.com>
+ * Copyright (c) 2005-2006 Siemens AG Oesterreich
+ */
+
+/**
+ * @ingroup qdisc
+ * @ingroup class
+ * @defgroup htb Hierachical Token Bucket (HTB)
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink-tc.h>
+#include <netlink/netlink.h>
+#include <netlink/cache.h>
+#include <netlink/utils.h>
+#include <netlink/route/tc.h>
+#include <netlink/route/qdisc.h>
+#include <netlink/route/qdisc-modules.h>
+#include <netlink/route/class.h>
+#include <netlink/route/class-modules.h>
+#include <netlink/route/link.h>
+#include <netlink/route/sch/htb.h>
+
+/** @cond SKIP */
+#define SCH_HTB_HAS_RATE2QUANTUM       0x01
+#define SCH_HTB_HAS_DEFCLS             0x02
+
+#define SCH_HTB_HAS_PRIO               0x01
+#define SCH_HTB_HAS_MTU                        0x02
+#define SCH_HTB_HAS_RATE               0x04
+#define SCH_HTB_HAS_CEIL               0x08
+#define SCH_HTB_HAS_RBUFFER            0x10
+#define SCH_HTB_HAS_CBUFFER            0x20
+/** @endcond */
+
+static inline struct rtnl_htb_qdisc *htb_qdisc(struct rtnl_qdisc *qdisc)
+{
+       if (qdisc->q_subdata == NULL)
+               qdisc->q_subdata = calloc(1, sizeof(struct rtnl_htb_qdisc));
+
+       return (struct rtnl_htb_qdisc *) qdisc->q_subdata;
+}
+
+static struct nla_policy htb_policy[TCA_HTB_MAX+1] = {
+       [TCA_HTB_INIT]  = { .minlen = sizeof(struct tc_htb_glob) },
+       [TCA_HTB_PARMS] = { .minlen = sizeof(struct tc_htb_opt) },
+};
+
+static int htb_qdisc_msg_parser(struct rtnl_qdisc *qdisc)
+{
+       int err;
+       struct nlattr *tb[TCA_HTB_MAX + 1];
+       struct rtnl_htb_qdisc *d;
+
+       err = tca_parse(tb, TCA_HTB_MAX, (struct rtnl_tca *) qdisc, htb_policy);
+       if (err < 0)
+               return err;
+       
+       d = htb_qdisc(qdisc);
+
+       if (tb[TCA_HTB_INIT]) {
+               struct tc_htb_glob opts;
+
+               nla_memcpy(&opts, tb[TCA_HTB_INIT], sizeof(opts));
+               d->qh_rate2quantum = opts.rate2quantum;
+               d->qh_defcls = opts.defcls;
+
+               d->qh_mask = (SCH_HTB_HAS_RATE2QUANTUM | SCH_HTB_HAS_DEFCLS);
+       }
+
+       return 0;
+}
+
+static void htb_qdisc_free_data(struct rtnl_qdisc *qdisc)
+{
+       free(qdisc->q_subdata);
+}
+
+static inline struct rtnl_htb_class *htb_class(struct rtnl_class *class)
+{
+       if (class->c_subdata == NULL)
+               class->c_subdata = calloc(1, sizeof(struct rtnl_htb_class));
+
+       return (struct rtnl_htb_class *) class->c_subdata;
+}
+
+static int htb_class_msg_parser(struct rtnl_class *class)
+{
+       int err;
+       struct nlattr *tb[TCA_HTB_MAX + 1];
+       struct rtnl_htb_class *d;
+
+       err = tca_parse(tb, TCA_HTB_MAX, (struct rtnl_tca *) class, htb_policy);
+       if (err < 0)
+               return err;
+       
+       d = htb_class(class);
+
+       if (tb[TCA_HTB_PARMS]) {
+               struct tc_htb_opt opts;
+
+               nla_memcpy(&opts, tb[TCA_HTB_PARMS], sizeof(opts));
+               d->ch_prio = opts.prio;
+               rtnl_copy_ratespec(&d->ch_rate, &opts.rate);
+               rtnl_copy_ratespec(&d->ch_ceil, &opts.ceil);
+               d->ch_rbuffer = opts.buffer;
+               d->ch_cbuffer = opts.cbuffer;
+
+               d->ch_mask = (SCH_HTB_HAS_PRIO | SCH_HTB_HAS_RATE |
+                       SCH_HTB_HAS_CEIL | SCH_HTB_HAS_RBUFFER |
+                       SCH_HTB_HAS_CBUFFER);
+       }
+
+       return 0;
+}
+
+static void htb_class_free_data(struct rtnl_class *class)
+{
+       free(class->c_subdata);
+}
+
+static int htb_qdisc_dump_brief(struct rtnl_qdisc *qdisc,
+                               struct nl_dump_params *p, int line)
+{
+       struct rtnl_htb_qdisc *d = (struct rtnl_htb_qdisc *) qdisc->q_subdata;
+
+       if (d == NULL)
+               goto ignore;
+
+       if (d->qh_mask & SCH_HTB_HAS_RATE2QUANTUM)
+               dp_dump(p, " r2q %u", d->qh_rate2quantum);
+
+       if (d->qh_mask & SCH_HTB_HAS_DEFCLS) {
+               char buf[32];
+               dp_dump(p, " default %s",
+                       rtnl_tc_handle2str(d->qh_defcls, buf, sizeof(buf)));
+       }
+
+ignore:
+       return line;
+}
+
+static int htb_class_dump_brief(struct rtnl_class *class,
+                               struct nl_dump_params *p, int line)
+{
+       struct rtnl_htb_class *d = (struct rtnl_htb_class *) class->c_subdata;
+
+       if (d == NULL)
+               goto ignore;
+
+       if (d->ch_mask & SCH_HTB_HAS_RATE) {
+               double r, rbit;
+               char *ru, *rubit;
+
+               r = nl_cancel_down_bytes(d->ch_rate.rs_rate, &ru);
+               rbit = nl_cancel_down_bits(d->ch_rate.rs_rate*8, &rubit);
+
+               dp_dump(p, " rate %.2f%s/s (%.0f%s) log %u",
+                       r, ru, rbit, rubit, 1<<d->ch_rate.rs_cell_log);
+       }
+
+ignore:
+       return line;
+}
+
+static int htb_class_dump_full(struct rtnl_class *class,
+                              struct nl_dump_params *p, int line)
+{
+       struct rtnl_htb_class *d = (struct rtnl_htb_class *) class->c_subdata;
+
+       if (d == NULL)
+               goto ignore;
+
+       /* line 1 */
+       if (d->ch_mask & SCH_HTB_HAS_CEIL) {
+               double r, rbit;
+               char *ru, *rubit;
+
+               r = nl_cancel_down_bytes(d->ch_ceil.rs_rate, &ru);
+               rbit = nl_cancel_down_bits(d->ch_ceil.rs_rate*8, &rubit);
+
+               dp_dump(p, "    ceil %.2f%s/s (%.0f%s) log %u",
+                       r, ru, rbit, rubit, 1<<d->ch_ceil.rs_cell_log);
+       }
+
+       if (d->ch_mask & SCH_HTB_HAS_PRIO)
+               dp_dump(p, " prio %u", d->ch_prio);
+       if (d->ch_mask & SCH_HTB_HAS_RBUFFER)
+               dp_dump(p, " rbuffer %u", d->ch_rbuffer);
+       if (d->ch_mask & SCH_HTB_HAS_CBUFFER)
+               dp_dump(p, " cbuffer %u", d->ch_cbuffer);
+
+ignore:
+       return line;
+}
+
+static struct nl_msg *htb_qdisc_get_opts(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_htb_qdisc *d = (struct rtnl_htb_qdisc *) qdisc->q_subdata;
+       struct tc_htb_glob opts;
+       struct nl_msg *msg;
+
+       if (d == NULL)
+               return NULL;
+
+       msg = nlmsg_build(NULL);
+       if (msg == NULL)
+               return NULL;
+
+       memset(&opts, 0, sizeof(opts));
+       opts.version = TC_HTB_PROTOVER;
+
+       if (d->qh_mask & SCH_HTB_HAS_RATE2QUANTUM)
+               opts.rate2quantum = d->qh_rate2quantum;
+       if (d->qh_mask & SCH_HTB_HAS_DEFCLS)
+               opts.defcls = d->qh_defcls;
+
+       nla_put(msg, TCA_HTB_INIT, sizeof(opts), &opts);
+
+       return msg;
+}
+
+static inline uint32_t compute_burst(uint32_t rate, uint32_t mtu)
+{
+       return rtnl_tc_calc_txtime(rate / nl_get_hz() + mtu, rate);
+}
+
+static uint8_t compute_cell(uint32_t rate, uint32_t mtu)
+{
+       uint8_t cell_log = 0;
+       while (mtu > 255) {
+               mtu >>= 1;
+               cell_log++;
+       }
+
+       return cell_log;
+}
+
+static struct nl_msg *htb_class_get_opts(struct rtnl_class *class)
+{
+       struct rtnl_htb_class *d = (struct rtnl_htb_class *) class->c_subdata;
+       uint32_t rtable[256], ctable[256];
+       struct tc_htb_opt opts;
+       struct nl_msg *msg;
+       
+
+       if (d == NULL)
+               return NULL;
+
+       msg = nlmsg_build(NULL);
+       memset(&opts, 0, sizeof(opts));
+
+       /* if not set, zero (0) is used as priority */
+       if (d->ch_mask & SCH_HTB_HAS_PRIO)
+               opts.prio = d->ch_prio;
+
+       if (!(d->ch_mask & SCH_HTB_HAS_RATE))
+               BUG();
+
+       rtnl_rcopy_ratespec(&opts.rate, &d->ch_rate);
+       /* if cell_log not set, compute default value */
+       if (opts.rate.cell_log == UINT8_MAX)
+       {
+               if(!(d->ch_mask & SCH_HTB_HAS_MTU))
+                       BUG();
+               opts.rate.cell_log = compute_cell(opts.rate.rate, d->ch_mtu);
+       }
+
+       /* if not set, configured rate is used as ceil, which implies no borrowing */
+       if (d->ch_mask & SCH_HTB_HAS_CEIL)
+               rtnl_rcopy_ratespec(&opts.ceil, &d->ch_ceil);
+       else
+               memcpy(&opts.ceil, &opts.rate, sizeof(struct tc_ratespec));
+       /* if cell_log not set, compute default value */
+       if (opts.ceil.cell_log == UINT8_MAX)
+       {
+               if(!(d->ch_mask & SCH_HTB_HAS_MTU))
+                       BUG();
+               opts.ceil.cell_log = compute_cell(opts.ceil.rate, d->ch_mtu);
+       }
+
+       if (d->ch_mask & SCH_HTB_HAS_RBUFFER)
+               opts.buffer = d->ch_rbuffer;
+       else
+       {
+               if(!(d->ch_mask & SCH_HTB_HAS_MTU))
+                       BUG();
+               opts.buffer = compute_burst(opts.rate.rate, d->ch_mtu);
+       }
+
+       if (d->ch_mask & SCH_HTB_HAS_CBUFFER)
+               opts.cbuffer = d->ch_cbuffer;
+       else
+       {
+               if(!(d->ch_mask & SCH_HTB_HAS_MTU))
+                       BUG();
+               opts.cbuffer = compute_burst(opts.ceil.rate, d->ch_mtu);
+       }
+
+       nla_put(msg, TCA_HTB_PARMS, sizeof(opts), &opts);
+       rtnl_tc_build_rate_table(rtable, opts.rate.mpu & 0xff,
+                                opts.rate.mpu >> 8, 1 << opts.rate.cell_log,
+                                opts.rate.rate);
+       nla_put(msg, TCA_HTB_RTAB, sizeof(rtable), &rtable);
+       rtnl_tc_build_rate_table(ctable, opts.ceil.mpu & 0xff,
+                                opts.ceil.mpu >> 8, 1 << opts.ceil.cell_log,
+                                opts.ceil.rate);
+       nla_put(msg, TCA_HTB_CTAB, sizeof(ctable), &ctable);
+
+       return msg;
+}
+
+/**
+ * @name Attribute Modifications
+ * @{
+ */
+
+void rtnl_htb_set_rate2quantum(struct rtnl_qdisc *qdisc, uint32_t rate2quantum)
+{
+       struct rtnl_htb_qdisc *d = htb_qdisc(qdisc);
+       if (d == NULL)
+               return;
+
+       d->qh_rate2quantum = rate2quantum;
+       d->qh_mask |= SCH_HTB_HAS_RATE2QUANTUM;
+}
+
+/**
+ * Set default class of the htb qdisc to the specified value
+ * @arg qdisc          qdisc to change
+ * @arg defcls         new default class
+ */
+void rtnl_htb_set_defcls(struct rtnl_qdisc *qdisc, uint32_t defcls)
+{
+       struct rtnl_htb_qdisc *d = htb_qdisc(qdisc);
+       if (d == NULL)
+               return;
+
+       d->qh_defcls = defcls;
+       d->qh_mask |= SCH_HTB_HAS_DEFCLS;
+}
+
+void rtnl_htb_set_prio(struct rtnl_class *class, uint32_t prio)
+{
+       struct rtnl_htb_class *d = htb_class(class);
+       if (d == NULL)
+               return;
+
+       d->ch_prio = prio;
+       d->ch_mask |= SCH_HTB_HAS_PRIO;
+}
+
+void rtnl_htb_set_mtu(struct rtnl_class *class, uint32_t mtu)
+{
+       struct rtnl_htb_class *d = htb_class(class);
+       if (d == NULL)
+               return;
+
+       d->ch_mtu = mtu;
+       d->ch_mask |= SCH_HTB_HAS_MTU;
+}
+
+void rtnl_htb_set_rate(struct rtnl_class *class, uint32_t rate)
+{
+       struct rtnl_htb_class *d = htb_class(class);
+       if (d == NULL)
+               return;
+
+       d->ch_rate.rs_cell_log = UINT8_MAX; /* use default value */
+       d->ch_rate.rs_rate = rate;
+       d->ch_mask |= SCH_HTB_HAS_RATE;
+}
+
+void rtnl_htb_set_ceil(struct rtnl_class *class, uint32_t ceil)
+{
+       struct rtnl_htb_class *d = htb_class(class);
+       if (d == NULL)
+               return;
+
+       d->ch_ceil.rs_cell_log = UINT8_MAX; /* use default value */
+       d->ch_ceil.rs_rate = ceil;
+       d->ch_mask |= SCH_HTB_HAS_CEIL;
+}
+
+/** @} */
+
+static struct rtnl_qdisc_ops htb_qdisc_ops = {
+       .qo_kind                = "htb",
+       .qo_msg_parser          = htb_qdisc_msg_parser,
+       .qo_free_data           = htb_qdisc_free_data,
+       .qo_dump[NL_DUMP_BRIEF] = htb_qdisc_dump_brief,
+       .qo_get_opts            = htb_qdisc_get_opts,
+};
+
+static struct rtnl_class_ops htb_class_ops = {
+       .co_kind                = "htb",
+       .co_msg_parser          = htb_class_msg_parser,
+       .co_free_data           = htb_class_free_data,
+       .co_dump[NL_DUMP_BRIEF] = htb_class_dump_brief,
+       .co_dump[NL_DUMP_FULL]  = htb_class_dump_full,
+       .co_get_opts            = htb_class_get_opts,
+};
+
+static void __init htb_init(void)
+{
+       rtnl_qdisc_register(&htb_qdisc_ops);
+       rtnl_class_register(&htb_class_ops);
+}
+
+static void __exit htb_exit(void)
+{
+       rtnl_qdisc_unregister(&htb_qdisc_ops);
+       rtnl_class_unregister(&htb_class_ops);
+}
+
+/** @} */
diff --git a/lib/route/sch/netem.c b/lib/route/sch/netem.c
new file mode 100644 (file)
index 0000000..75e33c5
--- /dev/null
@@ -0,0 +1,603 @@
+/*
+ * lib/route/sch/netem.c               Network Emulator Qdisc
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup qdisc
+ * @defgroup netem Network Emulator
+ * @brief
+ *
+ * For further documentation see http://linux-net.osdl.org/index.php/Netem
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink-tc.h>
+#include <netlink/netlink.h>
+#include <netlink/utils.h>
+#include <netlink/route/qdisc.h>
+#include <netlink/route/qdisc-modules.h>
+#include <netlink/route/sch/netem.h>
+
+/** @cond SKIP */
+#define SCH_NETEM_ATTR_LATENCY         0x001
+#define SCH_NETEM_ATTR_LIMIT           0x002
+#define SCH_NETEM_ATTR_LOSS            0x004
+#define SCH_NETEM_ATTR_GAP             0x008
+#define SCH_NETEM_ATTR_DUPLICATE       0x010
+#define SCH_NETEM_ATTR_JITTER          0x020
+#define SCH_NETEM_ATTR_DELAY_CORR      0x040
+#define SCH_NETEM_ATTR_LOSS_CORR       0x080
+#define SCH_NETEM_ATTR_DUP_CORR                0x100
+#define SCH_NETEM_ATTR_RO_PROB         0x200
+#define SCH_NETEM_ATTR_RO_CORR         0x400
+/** @endcond */
+
+static inline struct rtnl_netem *netem_qdisc(struct rtnl_qdisc *qdisc)
+{
+       return (struct rtnl_netem *) qdisc->q_subdata;
+}
+
+static inline struct rtnl_netem *netem_alloc(struct rtnl_qdisc *qdisc)
+{
+       if (!qdisc->q_subdata)
+               qdisc->q_subdata = calloc(1, sizeof(struct rtnl_netem));
+
+       return netem_qdisc(qdisc);
+}
+
+static struct nla_policy netem_policy[TCA_NETEM_MAX+1] = {
+       [TCA_NETEM_CORR]        = { .minlen = sizeof(struct tc_netem_corr) },
+       [TCA_NETEM_REORDER]     = { .minlen = sizeof(struct tc_netem_reorder) },
+};
+
+static int netem_msg_parser(struct rtnl_qdisc *qdisc)
+{
+       int len, err = 0;
+       struct rtnl_netem *netem;
+       struct tc_netem_qopt *opts;
+
+       if (qdisc->q_opts->d_size < sizeof(*opts))
+               return nl_error(EINVAL, "Netem specific options size mismatch");
+
+       netem = netem_alloc(qdisc);
+       if (!netem)
+               return nl_errno(ENOMEM);
+
+       opts = (struct tc_netem_qopt *) qdisc->q_opts->d_data;
+       netem->qnm_latency = opts->latency;
+       netem->qnm_limit = opts->limit;
+       netem->qnm_loss = opts->loss;
+       netem->qnm_gap = opts->gap;
+       netem->qnm_duplicate = opts->duplicate;
+       netem->qnm_jitter = opts->jitter;
+
+       netem->qnm_mask = (SCH_NETEM_ATTR_LATENCY | SCH_NETEM_ATTR_LIMIT |
+                          SCH_NETEM_ATTR_LOSS | SCH_NETEM_ATTR_GAP |
+                          SCH_NETEM_ATTR_DUPLICATE | SCH_NETEM_ATTR_JITTER);
+
+       len = qdisc->q_opts->d_size - sizeof(*opts);
+
+       if (len > 0) {
+               struct nlattr *tb[TCA_NETEM_MAX+1];
+
+               err = nla_parse(tb, TCA_NETEM_MAX, (struct nlattr *)
+                               qdisc->q_opts->d_data + sizeof(*opts),
+                               len, netem_policy);
+               if (err < 0) {
+                       free(netem);
+                       return err;
+               }
+
+               if (tb[TCA_NETEM_CORR]) {
+                       struct tc_netem_corr cor;
+
+                       nla_memcpy(&cor, tb[TCA_NETEM_CORR], sizeof(cor));
+                       netem->qnm_corr.nmc_delay = cor.delay_corr;
+                       netem->qnm_corr.nmc_loss = cor.loss_corr;
+                       netem->qnm_corr.nmc_duplicate = cor.dup_corr;
+
+                       netem->qnm_mask |= (SCH_NETEM_ATTR_DELAY_CORR |
+                                           SCH_NETEM_ATTR_LOSS_CORR |
+                                           SCH_NETEM_ATTR_DELAY_CORR);
+               }
+
+               if (tb[TCA_NETEM_REORDER]) {
+                       struct tc_netem_reorder ro;
+
+                       nla_memcpy(&ro, tb[TCA_NETEM_REORDER], sizeof(ro));
+                       netem->qnm_ro.nmro_probability = ro.probability;
+                       netem->qnm_ro.nmro_correlation = ro.correlation;
+
+                       netem->qnm_mask |= (SCH_NETEM_ATTR_RO_PROB |
+                                           SCH_NETEM_ATTR_RO_CORR);
+               }
+       }
+
+       return 0;
+}
+
+static void netem_free_data(struct rtnl_qdisc *qdisc)
+{
+       free(qdisc->q_subdata);
+}
+
+static int netem_dump_brief(struct rtnl_qdisc *qdisc, struct nl_dump_params *p,
+                           int line)
+{
+       struct rtnl_netem *netem = netem_qdisc(qdisc);
+
+       if (netem)
+               dp_dump(p, "limit %d", netem->qnm_limit);
+
+       return line;
+}
+
+static int netem_dump_full(struct rtnl_qdisc *qdisc, struct nl_dump_params *p,
+                          int line)
+{
+       return line;
+}
+
+static struct nl_msg *netem_get_opts(struct rtnl_qdisc *qdisc)
+{
+       return NULL;
+}
+
+/**
+ * @name Queue Limit
+ * @{
+ */
+
+/**
+ * Set limit of netem qdisc.
+ * @arg qdisc          Netem qdisc to be modified.
+ * @arg limit          New limit in bytes.
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_netem_set_limit(struct rtnl_qdisc *qdisc, int limit)
+{
+       struct rtnl_netem *netem;
+
+       netem = netem_alloc(qdisc);
+       if (!netem)
+               return nl_errno(ENOMEM);
+       
+       netem->qnm_limit = limit;
+       netem->qnm_mask |= SCH_NETEM_ATTR_LIMIT;
+
+       return 0;
+}
+
+/**
+ * Get limit of netem qdisc.
+ * @arg qdisc          Netem qdisc.
+ * @return Limit in bytes or a negative error code.
+ */
+int rtnl_netem_get_limit(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_netem *netem;
+
+       netem = netem_qdisc(qdisc);
+       if (netem && (netem->qnm_mask & SCH_NETEM_ATTR_LIMIT))
+               return netem->qnm_limit;
+       else
+               return nl_errno(ENOENT);
+}
+
+/** @} */
+
+/**
+ * @name Packet Re-ordering
+ * @{
+ */
+
+/**
+ * Set re-ordering gap of netem qdisc.
+ * @arg qdisc          Netem qdisc to be modified.
+ * @arg gap            New gap in number of packets.
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_netem_set_gap(struct rtnl_qdisc *qdisc, int gap)
+{
+       struct rtnl_netem *netem;
+
+       netem = netem_alloc(qdisc);
+       if (!netem)
+               return nl_errno(ENOMEM);
+
+       netem->qnm_gap = gap;
+       netem->qnm_mask |= SCH_NETEM_ATTR_GAP;
+
+       return 0;
+}
+
+/**
+ * Get re-ordering gap of netem qdisc.
+ * @arg qdisc          Netem qdisc.
+ * @return Re-ordering gap in packets or a negative error code.
+ */
+int rtnl_netem_get_gap(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_netem *netem;
+
+       netem = netem_qdisc(qdisc);
+       if (netem && (netem->qnm_mask & SCH_NETEM_ATTR_GAP))
+               return netem->qnm_gap;
+       else
+               return nl_errno(ENOENT);
+}
+
+/**
+ * Set re-ordering probability of netem qdisc.
+ * @arg qdisc          Netem qdisc to be modified.
+ * @arg prob           New re-ordering probability.
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_netem_set_reorder_probability(struct rtnl_qdisc *qdisc, int prob)
+{
+       struct rtnl_netem *netem;
+
+       netem = netem_alloc(qdisc);
+       if (!netem)
+               return nl_errno(ENOMEM);
+
+       netem->qnm_ro.nmro_probability = prob;
+       netem->qnm_mask |= SCH_NETEM_ATTR_RO_PROB;
+
+       return 0;
+}
+
+/**
+ * Get re-ordering probability of netem qdisc.
+ * @arg qdisc          Netem qdisc.
+ * @return Re-ordering probability or a negative error code.
+ */
+int rtnl_netem_get_reorder_probability(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_netem *netem;
+
+       netem = netem_qdisc(qdisc);
+       if (netem && (netem->qnm_mask & SCH_NETEM_ATTR_RO_PROB))
+               return netem->qnm_ro.nmro_probability;
+       else
+               return nl_errno(ENOENT);
+}
+
+/**
+ * Set re-order correlation probability of netem qdisc.
+ * @arg qdisc          Netem qdisc to be modified.
+ * @arg prob           New re-ordering correlation probability.
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_netem_set_reorder_correlation(struct rtnl_qdisc *qdisc, int prob)
+{
+       struct rtnl_netem *netem;
+
+       netem = netem_alloc(qdisc);
+       if (!netem)
+               return nl_errno(ENOMEM);
+
+       netem->qnm_ro.nmro_correlation = prob;
+       netem->qnm_mask |= SCH_NETEM_ATTR_RO_CORR;
+
+       return 0;
+}
+
+/**
+ * Get re-ordering correlation probability of netem qdisc.
+ * @arg qdisc          Netem qdisc.
+ * @return Re-ordering correlation probability or a negative error code.
+ */
+int rtnl_netem_get_reorder_correlation(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_netem *netem;
+
+       netem = netem_qdisc(qdisc);
+       if (netem && (netem->qnm_mask & SCH_NETEM_ATTR_RO_CORR))
+               return netem->qnm_ro.nmro_correlation;
+       else
+               return nl_errno(ENOENT);
+}
+
+/** @} */
+
+/**
+ * @name Packet Loss
+ * @{
+ */
+
+/**
+ * Set packet loss probability of netem qdisc.
+ * @arg qdisc          Netem qdisc to be modified.
+ * @arg prob           New packet loss probability.
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_netem_set_loss(struct rtnl_qdisc *qdisc, int prob)
+{
+       struct rtnl_netem *netem;
+
+       netem = netem_alloc(qdisc);
+       if (!netem)
+               return nl_errno(ENOMEM);
+
+       netem->qnm_loss = prob;
+       netem->qnm_mask |= SCH_NETEM_ATTR_LOSS;
+
+       return 0;
+}
+
+/**
+ * Get packet loss probability of netem qdisc.
+ * @arg qdisc          Netem qdisc.
+ * @return Packet loss probability or a negative error code.
+ */
+int rtnl_netem_get_loss(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_netem *netem;
+
+       netem = netem_qdisc(qdisc);
+       if (netem && (netem->qnm_mask & SCH_NETEM_ATTR_LOSS))
+               return netem->qnm_loss;
+       else
+               return nl_errno(ENOENT);
+}
+
+/**
+ * Set packet loss correlation probability of netem qdisc.
+ * @arg qdisc          Netem qdisc to be modified.
+ * @arg prob   New packet loss correlation.
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_netem_set_loss_correlation(struct rtnl_qdisc *qdisc, int prob)
+{
+       struct rtnl_netem *netem;
+
+       netem = netem_alloc(qdisc);
+       if (!netem)
+               return nl_errno(ENOMEM);
+
+       netem->qnm_corr.nmc_loss = prob;
+       netem->qnm_mask |= SCH_NETEM_ATTR_LOSS_CORR;
+
+       return 0;
+}
+
+/**
+ * Get packet loss correlation probability of netem qdisc.
+ * @arg qdisc          Netem qdisc.
+ * @return Packet loss correlation probability or a negative error code.
+ */
+int rtnl_netem_get_loss_correlation(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_netem *netem;
+
+       netem = netem_qdisc(qdisc);
+       if (netem && (netem->qnm_mask & SCH_NETEM_ATTR_LOSS_CORR))
+               return netem->qnm_corr.nmc_loss;
+       else
+               return nl_errno(ENOENT);
+}
+
+/** @} */
+
+/**
+ * @name Packet Duplication
+ * @{
+ */
+
+/**
+ * Set packet duplication probability of netem qdisc.
+ * @arg qdisc          Netem qdisc to be modified.
+ * @arg prob   New packet duplication probability.
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_netem_set_duplicate(struct rtnl_qdisc *qdisc, int prob)
+{
+       struct rtnl_netem *netem;
+
+       netem = netem_alloc(qdisc);
+       if (!netem)
+               return nl_errno(ENOMEM);
+
+       netem->qnm_duplicate = prob;
+       netem->qnm_mask |= SCH_NETEM_ATTR_DUPLICATE;
+
+       return 0;
+}
+
+/**
+ * Get packet duplication probability of netem qdisc.
+ * @arg qdisc          Netem qdisc.
+ * @return Packet duplication probability or a negative error code.
+ */
+int rtnl_netem_get_duplicate(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_netem *netem;
+
+       netem = netem_qdisc(qdisc);
+       if (netem && (netem->qnm_mask & SCH_NETEM_ATTR_DUPLICATE))
+               return netem->qnm_duplicate;
+       else
+               return nl_errno(ENOENT);
+}
+
+/**
+ * Set packet duplication correlation probability of netem qdisc.
+ * @arg qdisc          Netem qdisc to be modified.
+ * @arg prob           New packet duplication correlation probability.
+ * @return 0 on sucess or a negative error code.
+ */
+int rtnl_netem_set_duplicate_correlation(struct rtnl_qdisc *qdisc, int prob)
+{
+       struct rtnl_netem *netem;
+
+       netem = netem_alloc(qdisc);
+       if (!netem)
+               return nl_errno(ENOMEM);
+
+       netem->qnm_corr.nmc_duplicate = prob;
+       netem->qnm_mask |= SCH_NETEM_ATTR_DUP_CORR;
+
+       return 0;
+}
+
+/**
+ * Get packet duplication correlation probability of netem qdisc.
+ * @arg qdisc          Netem qdisc.
+ * @return Packet duplication correlation probability or a negative error code.
+ */
+int rtnl_netem_get_duplicate_correlation(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_netem *netem;
+
+       netem = netem_qdisc(qdisc);
+       if (netem && (netem->qnm_mask & SCH_NETEM_ATTR_DUP_CORR))
+               return netem->qnm_corr.nmc_duplicate;
+       else
+               return nl_errno(ENOENT);
+}
+
+/** @} */
+
+/**
+ * @name Packet Delay
+ * @{
+ */
+
+/**
+ * Set packet delay of netem qdisc.
+ * @arg qdisc          Netem qdisc to be modified.
+ * @arg delay          New packet delay in micro seconds.
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_netem_set_delay(struct rtnl_qdisc *qdisc, int delay)
+{
+       struct rtnl_netem *netem;
+
+       netem = netem_alloc(qdisc);
+       if (!netem)
+               return nl_errno(ENOMEM);
+
+       netem->qnm_latency = nl_us2ticks(delay);
+       netem->qnm_mask |= SCH_NETEM_ATTR_LATENCY;
+
+       return 0;
+}
+
+/**
+ * Get packet delay of netem qdisc.
+ * @arg qdisc          Netem qdisc.
+ * @return Packet delay in micro seconds or a negative error code.
+ */
+int rtnl_netem_get_delay(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_netem *netem;
+
+       netem = netem_qdisc(qdisc);
+       if (netem && (netem->qnm_mask & SCH_NETEM_ATTR_LATENCY))
+               return nl_ticks2us(netem->qnm_latency);
+       else
+               return nl_errno(ENOENT);
+}
+
+/**
+ * Set packet delay jitter of netem qdisc.
+ * @arg qdisc          Netem qdisc to be modified.
+ * @arg jitter         New packet delay jitter in micro seconds.
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_netem_set_jitter(struct rtnl_qdisc *qdisc, int jitter)
+{
+       struct rtnl_netem *netem;
+
+       netem = netem_alloc(qdisc);
+       if (!netem)
+               return nl_errno(ENOMEM);
+
+       netem->qnm_jitter = nl_us2ticks(jitter);
+       netem->qnm_mask |= SCH_NETEM_ATTR_JITTER;
+
+       return 0;
+}
+
+/**
+ * Get packet delay jitter of netem qdisc.
+ * @arg qdisc          Netem qdisc.
+ * @return Packet delay jitter in micro seconds or a negative error code.
+ */
+int rtnl_netem_get_jitter(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_netem *netem;
+
+       netem = netem_qdisc(qdisc);
+       if (netem && (netem->qnm_mask & SCH_NETEM_ATTR_JITTER))
+               return nl_ticks2us(netem->qnm_jitter);
+       else
+               return nl_errno(ENOENT);
+}
+
+/**
+ * Set packet delay correlation probability of netem qdisc.
+ * @arg qdisc          Netem qdisc to be modified.
+ * @arg prob           New packet delay correlation probability.
+ */
+int rtnl_netem_set_delay_correlation(struct rtnl_qdisc *qdisc, int prob)
+{
+       struct rtnl_netem *netem;
+
+       netem = netem_alloc(qdisc);
+       if (!netem)
+               return nl_errno(ENOMEM);
+
+       netem->qnm_corr.nmc_delay = prob;
+       netem->qnm_mask |= SCH_NETEM_ATTR_DELAY_CORR;
+
+       return 0;
+}
+
+/**
+ * Get packet delay correlation probability of netem qdisc.
+ * @arg qdisc          Netem qdisc.
+ * @return Packet delay correlation probability or a negative error code.
+ */
+int rtnl_netem_get_delay_corellation(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_netem *netem;
+
+       netem = netem_qdisc(qdisc);
+       if (netem && (netem->qnm_mask & SCH_NETEM_ATTR_DELAY_CORR))
+               return netem->qnm_corr.nmc_delay;
+       else
+               return nl_errno(ENOENT);
+}
+
+/** @} */
+
+static struct rtnl_qdisc_ops netem_ops = {
+       .qo_kind                = "netem",
+       .qo_msg_parser          = netem_msg_parser,
+       .qo_free_data           = netem_free_data,
+       .qo_dump[NL_DUMP_BRIEF] = netem_dump_brief,
+       .qo_dump[NL_DUMP_FULL]  = netem_dump_full,
+       .qo_get_opts            = netem_get_opts,
+};
+
+static void __init netem_init(void)
+{
+       rtnl_qdisc_register(&netem_ops);
+}
+
+static void __exit netem_exit(void)
+{
+       rtnl_qdisc_unregister(&netem_ops);
+}
+
+/** @} */
diff --git a/lib/route/sch/prio.c b/lib/route/sch/prio.c
new file mode 100644 (file)
index 0000000..aa06f9c
--- /dev/null
@@ -0,0 +1,332 @@
+/*
+ * lib/route/sch/prio.c                PRIO Qdisc/Class
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup qdisc
+ * @defgroup prio (Fast) Prio
+ * @brief
+ *
+ * @par 1) Typical PRIO configuration
+ * @code
+ * // Specify the maximal number of bands to be used for this PRIO qdisc.
+ * rtnl_qdisc_prio_set_bands(qdisc, QDISC_PRIO_DEFAULT_BANDS);
+ *
+ * // Provide a map assigning each priority to a band number.
+ * uint8_t map[] = QDISC_PRIO_DEFAULT_PRIOMAP;
+ * rtnl_qdisc_prio_set_priomap(qdisc, map, sizeof(map));
+ * @endcode
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink-tc.h>
+#include <netlink/netlink.h>
+#include <netlink/utils.h>
+#include <netlink/route/qdisc.h>
+#include <netlink/route/qdisc-modules.h>
+#include <netlink/route/sch/prio.h>
+
+/** @cond SKIP */
+#define SCH_PRIO_ATTR_BANDS    1
+#define SCH_PRIO_ATTR_PRIOMAP  2
+/** @endcond */
+
+static inline struct rtnl_prio *prio_qdisc(struct rtnl_qdisc *qdisc)
+{
+       return (struct rtnl_prio *) qdisc->q_subdata;
+}
+
+static inline struct rtnl_prio *prio_alloc(struct rtnl_qdisc *qdisc)
+{
+       if (!qdisc->q_subdata)
+               qdisc->q_subdata = calloc(1, sizeof(struct rtnl_prio));
+
+       return prio_qdisc(qdisc);
+}
+
+static int prio_msg_parser(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_prio *prio;
+       struct tc_prio_qopt *opt;
+
+       if (qdisc->q_opts->d_size < sizeof(*opt))
+               return nl_error(EINVAL, "prio specific option size mismatch");
+
+       prio = prio_alloc(qdisc);
+       if (!prio)
+               return nl_errno(ENOMEM);
+
+       opt = (struct tc_prio_qopt *) qdisc->q_opts->d_data;
+       prio->qp_bands = opt->bands;
+       memcpy(prio->qp_priomap, opt->priomap, sizeof(prio->qp_priomap));
+       prio->qp_mask = (SCH_PRIO_ATTR_BANDS | SCH_PRIO_ATTR_PRIOMAP);
+       
+       return 0;
+}
+
+static void prio_free_data(struct rtnl_qdisc *qdisc)
+{
+       free(qdisc->q_subdata);
+}
+
+static int prio_dump_brief(struct rtnl_qdisc *qdisc,
+                          struct nl_dump_params *p, int line)
+{
+       struct rtnl_prio *prio = prio_qdisc(qdisc);
+
+       if (prio)
+               dp_dump(p, " bands %u", prio->qp_bands);
+
+       return line;
+}
+
+static int prio_dump_full(struct rtnl_qdisc *qdisc,
+                         struct nl_dump_params *p, int line)
+{
+       struct rtnl_prio *prio = prio_qdisc(qdisc);
+       int i, hp;
+
+       if (!prio)
+               goto ignore;
+
+       dp_dump(p, "priomap [");
+       
+       for (i = 0; i <= TC_PRIO_MAX; i++)
+               dp_dump(p, "%u%s", prio->qp_priomap[i],
+                       i < TC_PRIO_MAX ? " " : "");
+
+       dp_dump(p, "]\n");
+       dp_new_line(p, line++);
+
+       hp = (((TC_PRIO_MAX/2) + 1) & ~1);
+
+       for (i = 0; i < hp; i++) {
+               char a[32];
+               dp_dump(p, "    %18s => %u",
+                       rtnl_prio2str(i, a, sizeof(a)),
+                       prio->qp_priomap[i]);
+               if (hp+i <= TC_PRIO_MAX) {
+                       dp_dump(p, " %18s => %u",
+                               rtnl_prio2str(hp+i, a, sizeof(a)),
+                               prio->qp_priomap[hp+i]);
+                       if (i < (hp - 1)) {
+                               dp_dump(p, "\n");
+                               dp_new_line(p, line++);
+                       }
+               }
+       }
+
+ignore:
+       return line;
+}
+
+static struct nl_msg *prio_get_opts(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_prio *prio;
+       struct tc_prio_qopt opts;
+       struct nl_msg *msg;
+
+       prio = prio_qdisc(qdisc);
+       if (!prio ||
+           !(prio->qp_mask & SCH_PRIO_ATTR_PRIOMAP))
+               goto errout;
+
+       opts.bands = prio->qp_bands;
+       memcpy(opts.priomap, prio->qp_priomap, sizeof(opts.priomap));
+
+       msg = nlmsg_build_no_hdr();
+       if (!msg)
+               goto errout;
+
+       if (nlmsg_append(msg, &opts, sizeof(opts), 0) < 0) {
+               nlmsg_free(msg);
+               goto errout;
+       }
+
+       return msg;
+errout:
+       return NULL;
+}
+
+/**
+ * @name Attribute Modification
+ * @{
+ */
+
+/**
+ * Set number of bands of PRIO qdisc.
+ * @arg qdisc          PRIO qdisc to be modified.
+ * @arg bands          New number of bands.
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_qdisc_prio_set_bands(struct rtnl_qdisc *qdisc, int bands)
+{
+       struct rtnl_prio *prio;
+       
+       prio = prio_alloc(qdisc);
+       if (!prio)
+               return nl_errno(ENOMEM);
+
+       prio->qp_bands = bands;
+       prio->qp_mask |= SCH_PRIO_ATTR_BANDS;
+
+       return 0;
+}
+
+/**
+ * Get number of bands of PRIO qdisc.
+ * @arg qdisc          PRIO qdisc.
+ * @return Number of bands or a negative error code.
+ */
+int rtnl_qdisc_prio_get_bands(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_prio *prio;
+
+       prio = prio_qdisc(qdisc);
+       if (prio && prio->qp_mask & SCH_PRIO_ATTR_BANDS)
+               return prio->qp_bands;
+       else
+               return nl_errno(ENOMEM);
+}
+
+/**
+ * Set priomap of the PRIO qdisc.
+ * @arg qdisc          PRIO qdisc to be modified.
+ * @arg priomap                New priority mapping.
+ * @arg len            Length of priomap (# of elements).
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_qdisc_prio_set_priomap(struct rtnl_qdisc *qdisc, uint8_t priomap[],
+                               int len)
+{
+       struct rtnl_prio *prio;
+       int i;
+
+       prio = prio_alloc(qdisc);
+       if (!prio)
+               return nl_errno(ENOMEM);
+
+       if (!(prio->qp_mask & SCH_PRIO_ATTR_BANDS))
+               return nl_error(EINVAL, "Set number of bands first");
+
+       if ((len / sizeof(uint8_t)) > (TC_PRIO_MAX+1))
+               return nl_error(ERANGE, "priomap length out of bounds");
+
+       for (i = 0; i <= TC_PRIO_MAX; i++) {
+               if (priomap[i] > prio->qp_bands)
+                       return nl_error(ERANGE, "priomap element %d " \
+                           "out of bounds, increase bands number");
+       }
+
+       memcpy(prio->qp_priomap, priomap, len);
+       prio->qp_mask |= SCH_PRIO_ATTR_PRIOMAP;
+
+       return 0;
+}
+
+/**
+ * Get priomap of a PRIO qdisc.
+ * @arg qdisc          PRIO qdisc.
+ * @return Priority mapping as array of size TC_PRIO_MAX+1
+ *         or NULL if an error occured.
+ */
+uint8_t *rtnl_qdisc_prio_get_priomap(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_prio *prio;
+
+       prio = prio_qdisc(qdisc);
+       if (prio && prio->qp_mask & SCH_PRIO_ATTR_PRIOMAP)
+               return prio->qp_priomap;
+       else {
+               nl_errno(ENOENT);
+               return NULL;
+       }
+}
+
+/** @} */
+
+/**
+ * @name Priority Band Translations
+ * @{
+ */
+
+static struct trans_tbl prios[] = {
+       __ADD(TC_PRIO_BESTEFFORT,besteffort)
+       __ADD(TC_PRIO_FILLER,filler)
+       __ADD(TC_PRIO_BULK,bulk)
+       __ADD(TC_PRIO_INTERACTIVE_BULK,interactive_bulk)
+       __ADD(TC_PRIO_INTERACTIVE,interactive)
+       __ADD(TC_PRIO_CONTROL,control)
+};
+
+/**
+ * Convert priority to character string.
+ * @arg prio           Priority.
+ * @arg buf            Destination buffer
+ * @arg size           Size of destination buffer.
+ *
+ * Converts a priority to a character string and stores the result in
+ * the specified destination buffer.
+ *
+ * @return Name of priority as character string.
+ */
+char * rtnl_prio2str(int prio, char *buf, size_t size)
+{
+       return __type2str(prio, buf, size, prios, ARRAY_SIZE(prios));
+}
+
+/**
+ * Convert character string to priority.
+ * @arg name           Name of priority.
+ *
+ * Converts the provided character string specifying a priority
+ * to the corresponding numeric value.
+ *
+ * @return Numeric priority or a negative value if no match was found.
+ */
+int rtnl_str2prio(const char *name)
+{
+       return __str2type(name, prios, ARRAY_SIZE(prios));
+}
+
+/** @} */
+
+static struct rtnl_qdisc_ops prio_ops = {
+       .qo_kind                = "prio",
+       .qo_msg_parser          = prio_msg_parser,
+       .qo_free_data           = prio_free_data,
+       .qo_dump[NL_DUMP_BRIEF] = prio_dump_brief,
+       .qo_dump[NL_DUMP_FULL]  = prio_dump_full,
+       .qo_get_opts            = prio_get_opts,
+};
+
+static struct rtnl_qdisc_ops pfifo_fast_ops = {
+       .qo_kind                = "pfifo_fast",
+       .qo_msg_parser          = prio_msg_parser,
+       .qo_free_data           = prio_free_data,
+       .qo_dump[NL_DUMP_BRIEF] = prio_dump_brief,
+       .qo_dump[NL_DUMP_FULL]  = prio_dump_full,
+       .qo_get_opts            = prio_get_opts,
+};
+
+static void __init prio_init(void)
+{
+       rtnl_qdisc_register(&prio_ops);
+       rtnl_qdisc_register(&pfifo_fast_ops);
+}
+
+static void __exit prio_exit(void)
+{
+       rtnl_qdisc_unregister(&prio_ops);
+       rtnl_qdisc_unregister(&pfifo_fast_ops);
+}
+
+/** @} */
diff --git a/lib/route/sch/red.c b/lib/route/sch/red.c
new file mode 100644 (file)
index 0000000..80a44af
--- /dev/null
@@ -0,0 +1,219 @@
+/*
+ * lib/route/sch/red.c         RED Qdisc
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup qdisc
+ * @defgroup red Random Early Detection (RED)
+ * @brief
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink-tc.h>
+#include <netlink/netlink.h>
+#include <netlink/utils.h>
+#include <netlink/route/qdisc.h>
+#include <netlink/route/qdisc-modules.h>
+#include <netlink/route/sch/red.h>
+
+/** @cond SKIP */
+#define RED_ATTR_LIMIT         0x01
+#define RED_ATTR_QTH_MIN       0x02
+#define RED_ATTR_QTH_MAX       0x04
+#define RED_ATTR_FLAGS         0x08
+#define RED_ATTR_WLOG          0x10
+#define RED_ATTR_PLOG          0x20
+#define RED_ATTR_SCELL_LOG     0x40
+/** @endcond */
+
+static inline struct rtnl_red *red_qdisc(struct rtnl_qdisc *qdisc)
+{
+       return (struct rtnl_red *) qdisc->q_subdata;
+}
+
+static inline struct rtnl_red *red_alloc(struct rtnl_qdisc *qdisc)
+{
+       if (!qdisc->q_subdata)
+               qdisc->q_subdata = calloc(1, sizeof(struct rtnl_red));
+
+       return red_qdisc(qdisc);
+}
+
+static struct nla_policy red_policy[TCA_RED_MAX+1] = {
+       [TCA_RED_PARMS]         = { .minlen = sizeof(struct tc_red_qopt) },
+};
+
+static int red_msg_parser(struct rtnl_qdisc *qdisc)
+{
+       struct nlattr *tb[TCA_RED_MAX+1];
+       struct rtnl_red *red;
+       struct tc_red_qopt *opts;
+       int err;
+
+       if (!(qdisc->q_mask & TCA_ATTR_OPTS))
+               return 0;
+
+       err = tca_parse(tb, TCA_RED_MAX, (struct rtnl_tca *) qdisc, red_policy);
+       if (err < 0)
+               return err;
+
+       if (!tb[TCA_RED_PARMS])
+               return nl_error(EINVAL, "Missing TCA_RED_PARMS");
+
+       red = red_alloc(qdisc);
+       if (!red)
+               return nl_errno(ENOMEM);
+
+       opts = nla_data(tb[TCA_RED_PARMS]);
+
+       red->qr_limit = opts->limit;
+       red->qr_qth_min = opts->qth_min;
+       red->qr_qth_max = opts->qth_max;
+       red->qr_flags = opts->flags;
+       red->qr_wlog = opts->Wlog;
+       red->qr_plog = opts->Plog;
+       red->qr_scell_log = opts->Scell_log;
+
+       red->qr_mask = (RED_ATTR_LIMIT | RED_ATTR_QTH_MIN | RED_ATTR_QTH_MAX |
+                       RED_ATTR_FLAGS | RED_ATTR_WLOG | RED_ATTR_PLOG |
+                       RED_ATTR_SCELL_LOG);
+
+       return 0;
+}
+
+static int red_dump_brief(struct rtnl_qdisc *qdisc, struct nl_dump_params *p,
+                         int line)
+{
+       struct rtnl_red *red = red_qdisc(qdisc);
+
+       if (red) {
+               /* XXX: limit, min, max, flags */
+       }
+
+       return line;
+}
+
+static int red_dump_full(struct rtnl_qdisc *qdisc, struct nl_dump_params *p,
+                        int line)
+{
+       struct rtnl_red *red = red_qdisc(qdisc);
+
+       if (red) {
+               /* XXX: wlog, plog, scell_log */
+       }
+
+       return line;
+}
+
+static int red_dump_stats(struct rtnl_qdisc *qdisc, struct nl_dump_params *p,
+                         int line)
+{
+       struct rtnl_red *red = red_qdisc(qdisc);
+
+       if (red) {
+               /* XXX: xstats */
+       }
+
+       return line;
+}
+
+static struct nl_msg *red_get_opts(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_red *red;
+       struct nl_msg *msg;
+
+       red = red_qdisc(qdisc);
+       if (!red)
+               return NULL;
+
+       msg = nlmsg_build_no_hdr();
+       if (!msg)
+               goto errout;
+
+#if 0
+       memset(&opts, 0, sizeof(opts));
+       opts.quantum = sfq->qs_quantum;
+       opts.perturb_period = sfq->qs_perturb;
+       opts.limit = sfq->qs_limit;
+
+       if (nlmsg_append(msg, &opts, sizeof(opts), 0) < 0)
+               goto errout;
+#endif
+
+       return msg;
+errout:
+       nlmsg_free(msg);
+       return NULL;
+}
+
+/**
+ * @name Attribute Access
+ * @{
+ */
+
+/**
+ * Set limit of RED qdisc.
+ * @arg qdisc          RED qdisc to be modified.
+ * @arg limit          New limit in number of packets.
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_red_set_limit(struct rtnl_qdisc *qdisc, int limit)
+{
+       struct rtnl_red *red;
+
+       red = red_alloc(qdisc);
+       if (!red)
+               return nl_errno(ENOMEM);
+
+       red->qr_limit = limit;
+       red->qr_mask |= RED_ATTR_LIMIT;
+
+       return 0;
+}
+
+/**
+ * Get limit of RED qdisc.
+ * @arg qdisc          RED qdisc.
+ * @return Limit or a negative error code.
+ */
+int rtnl_red_get_limit(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_red *red;
+
+       red = red_qdisc(qdisc);
+       if (red && (red->qr_mask & RED_ATTR_LIMIT))
+               return red->qr_limit;
+       else
+               return nl_errno(ENOENT);
+}
+
+/** @} */
+
+static struct rtnl_qdisc_ops red_ops = {
+       .qo_kind                = "red",
+       .qo_msg_parser          = red_msg_parser,
+       .qo_dump[NL_DUMP_BRIEF] = red_dump_brief,
+       .qo_dump[NL_DUMP_FULL]  = red_dump_full,
+       .qo_dump[NL_DUMP_STATS] = red_dump_stats,
+       .qo_get_opts            = red_get_opts,
+};
+
+static void __init red_init(void)
+{
+       rtnl_qdisc_register(&red_ops);
+}
+
+static void __exit red_exit(void)
+{
+       rtnl_qdisc_unregister(&red_ops);
+}
+
+/** @} */
diff --git a/lib/route/sch/sfq.c b/lib/route/sch/sfq.c
new file mode 100644 (file)
index 0000000..2e7d3ce
--- /dev/null
@@ -0,0 +1,294 @@
+/*
+ * lib/route/sch/sfq.c         SFQ Qdisc
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup qdisc
+ * @defgroup sfq Stochastic Fairness Queueing (SFQ)
+ * @brief
+ *
+ * @par Parameter Description
+ * - \b Quantum: Number of bytes to send out per slot and round.
+ * - \b Perturbation: Timer period between changing the hash function.
+ * - \b Limit:  Upper limit of queue in number of packets before SFQ starts
+ *             dropping packets.
+ * - \b Divisor: Hash table divisor, i.e. size of hash table.
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink-tc.h>
+#include <netlink/netlink.h>
+#include <netlink/utils.h>
+#include <netlink/route/qdisc.h>
+#include <netlink/route/qdisc-modules.h>
+#include <netlink/route/sch/sfq.h>
+
+/** @cond SKIP */
+#define SCH_SFQ_ATTR_QUANTUM   0x01
+#define SCH_SFQ_ATTR_PERTURB   0x02
+#define SCH_SFQ_ATTR_LIMIT     0x04
+#define SCH_SFQ_ATTR_DIVISOR   0x08
+#define SCH_SFQ_ATTR_FLOWS     0x10
+/** @endcond */
+
+static inline struct rtnl_sfq *sfq_qdisc(struct rtnl_qdisc *qdisc)
+{
+       return (struct rtnl_sfq *) qdisc->q_subdata;
+}
+
+static inline struct rtnl_sfq *sfq_alloc(struct rtnl_qdisc *qdisc)
+{
+       if (!qdisc->q_subdata)
+               qdisc->q_subdata = calloc(1, sizeof(struct rtnl_sfq));
+
+       return sfq_qdisc(qdisc);
+}
+
+static int sfq_msg_parser(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_sfq *sfq;
+       struct tc_sfq_qopt *opts;
+
+       if (!(qdisc->q_mask & TCA_ATTR_OPTS))
+               return 0;
+
+       if (qdisc->q_opts->d_size < sizeof(*opts))
+               return nl_error(EINVAL, "SFQ specific options size mismatch");
+
+       sfq = sfq_alloc(qdisc);
+       if (!sfq)
+               return nl_errno(ENOMEM);
+
+       opts = (struct tc_sfq_qopt *) qdisc->q_opts->d_data;
+
+       sfq->qs_quantum = opts->quantum;
+       sfq->qs_perturb = opts->perturb_period;
+       sfq->qs_limit = opts->limit;
+       sfq->qs_divisor = opts->divisor;
+       sfq->qs_flows = opts->flows;
+
+       sfq->qs_mask = (SCH_SFQ_ATTR_QUANTUM | SCH_SFQ_ATTR_PERTURB |
+                       SCH_SFQ_ATTR_LIMIT | SCH_SFQ_ATTR_DIVISOR |
+                       SCH_SFQ_ATTR_FLOWS);
+
+       return 0;
+}
+
+static void sfq_free_data(struct rtnl_qdisc *qdisc)
+{
+       free(qdisc->q_subdata);
+}
+
+static int sfq_dump_brief(struct rtnl_qdisc *qdisc, struct nl_dump_params *p,
+                         int line)
+{
+       struct rtnl_sfq *sfq = sfq_qdisc(qdisc);
+
+       if (sfq)
+               dp_dump(p, " quantum %u perturb %us",
+                       sfq->qs_quantum,
+                       nl_ticks2us(sfq->qs_perturb * nl_get_hz()));
+
+       return line;
+}
+
+static int sfq_dump_full(struct rtnl_qdisc *qdisc, struct nl_dump_params *p,
+                        int line)
+{
+       struct rtnl_sfq *sfq = sfq_qdisc(qdisc);
+
+       if (sfq)
+               dp_dump(p, "limit %u divisor %u",
+                       sfq->qs_limit, sfq->qs_divisor);
+
+       return line;
+}
+
+static struct nl_msg *sfq_get_opts(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_sfq *sfq;
+       struct tc_sfq_qopt opts;
+       struct nl_msg *msg;
+
+       sfq = sfq_qdisc(qdisc);
+       if (!sfq)
+               return NULL;
+
+       msg = nlmsg_build_no_hdr();
+       if (!msg)
+               goto errout;
+
+       memset(&opts, 0, sizeof(opts));
+       opts.quantum = sfq->qs_quantum;
+       opts.perturb_period = sfq->qs_perturb;
+       opts.limit = sfq->qs_limit;
+
+       if (nlmsg_append(msg, &opts, sizeof(opts), 0) < 0)
+               goto errout;
+
+       return msg;
+errout:
+       nlmsg_free(msg);
+       return NULL;
+}
+
+/**
+ * @name Attribute Access
+ * @{
+ */
+
+/**
+ * Set quantum of SFQ qdisc.
+ * @arg qdisc          SFQ qdisc to be modified.
+ * @arg quantum                New quantum in bytes.
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_sfq_set_quantum(struct rtnl_qdisc *qdisc, int quantum)
+{
+       struct rtnl_sfq *sfq;
+       
+       sfq = sfq_alloc(qdisc);
+       if (!sfq)
+               return nl_errno(ENOMEM);
+
+       sfq->qs_quantum = quantum;
+       sfq->qs_mask |= SCH_SFQ_ATTR_QUANTUM;
+
+       return 0;
+}
+
+/**
+ * Get quantum of SFQ qdisc.
+ * @arg qdisc          SFQ qdisc.
+ * @return Quantum in bytes or a negative error code.
+ */
+int rtnl_sfq_get_quantum(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_sfq *sfq;
+
+       sfq = sfq_qdisc(qdisc);
+       if (sfq && sfq->qs_mask & SCH_SFQ_ATTR_QUANTUM)
+               return sfq->qs_quantum;
+       else
+               return nl_errno(ENOENT);
+}
+
+/**
+ * Set limit of SFQ qdisc.
+ * @arg qdisc          SFQ qdisc to be modified.
+ * @arg limit          New limit in number of packets.
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_sfq_set_limit(struct rtnl_qdisc *qdisc, int limit)
+{
+       struct rtnl_sfq *sfq;
+
+       sfq = sfq_alloc(qdisc);
+       if (!sfq)
+               return nl_errno(ENOMEM);
+
+       sfq->qs_limit = limit;
+       sfq->qs_mask |= SCH_SFQ_ATTR_LIMIT;
+
+       return 0;
+}
+
+/**
+ * Get limit of SFQ qdisc.
+ * @arg qdisc          SFQ qdisc.
+ * @return Limit or a negative error code.
+ */
+int rtnl_sfq_get_limit(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_sfq *sfq;
+
+       sfq = sfq_qdisc(qdisc);
+       if (sfq && sfq->qs_mask & SCH_SFQ_ATTR_LIMIT)
+               return sfq->qs_limit;
+       else
+               return nl_errno(ENOENT);
+}
+
+/**
+ * Set perturbation interval of SFQ qdisc.
+ * @arg qdisc          SFQ qdisc to be modified.
+ * @arg perturb                New perturbation interval in seconds.
+ * @note A value of 0 disables perturbation altogether.
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_sfq_set_perturb(struct rtnl_qdisc *qdisc, int perturb)
+{
+       struct rtnl_sfq *sfq;
+
+       sfq = sfq_alloc(qdisc);
+       if (!sfq)
+               return nl_errno(ENOMEM);
+
+       sfq->qs_perturb = perturb;
+       sfq->qs_mask |= SCH_SFQ_ATTR_PERTURB;
+
+       return 0;
+}
+
+/**
+ * Get perturbation interval of SFQ qdisc.
+ * @arg qdisc          SFQ qdisc.
+ * @return Perturbation interval in seconds or a negative error code.
+ */
+int rtnl_sfq_get_perturb(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_sfq *sfq;
+
+       sfq = sfq_qdisc(qdisc);
+       if (sfq && sfq->qs_mask & SCH_SFQ_ATTR_PERTURB)
+               return sfq->qs_perturb;
+       else
+               return nl_errno(ENOENT);
+}
+
+/**
+ * Get divisor of SFQ qdisc.
+ * @arg qdisc          SFQ qdisc.
+ * @return Divisor in number of entries or a negative error code.
+ */
+int rtnl_sfq_get_divisor(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_sfq *sfq;
+
+       sfq = sfq_qdisc(qdisc);
+       if (sfq && sfq->qs_mask & SCH_SFQ_ATTR_DIVISOR)
+               return sfq->qs_divisor;
+       else
+               return nl_errno(ENOENT);
+}
+
+/** @} */
+
+static struct rtnl_qdisc_ops sfq_ops = {
+       .qo_kind                = "sfq",
+       .qo_msg_parser          = sfq_msg_parser,
+       .qo_free_data           = sfq_free_data,
+       .qo_dump[NL_DUMP_BRIEF] = sfq_dump_brief,
+       .qo_dump[NL_DUMP_FULL]  = sfq_dump_full,
+       .qo_get_opts            = sfq_get_opts,
+};
+
+static void __init sfq_init(void)
+{
+       rtnl_qdisc_register(&sfq_ops);
+}
+
+static void __exit sfq_exit(void)
+{
+       rtnl_qdisc_unregister(&sfq_ops);
+}
+
+/** @} */
diff --git a/lib/route/sch/tbf.c b/lib/route/sch/tbf.c
new file mode 100644 (file)
index 0000000..f3d50a5
--- /dev/null
@@ -0,0 +1,540 @@
+/*
+ * lib/route/sch/tbf.c         TBF Qdisc
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup qdisc
+ * @defgroup tbf Token Bucket Filter (TBF)
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink-tc.h>
+#include <netlink/netlink.h>
+#include <netlink/cache.h>
+#include <netlink/utils.h>
+#include <netlink/route/tc.h>
+#include <netlink/route/qdisc.h>
+#include <netlink/route/qdisc-modules.h>
+#include <netlink/route/class.h>
+#include <netlink/route/class-modules.h>
+#include <netlink/route/link.h>
+#include <netlink/route/sch/tbf.h>
+
+/** @cond SKIP */
+#define TBF_ATTR_LIMIT                 0x01
+#define TBF_ATTR_RATE                  0x02
+#define TBF_ATTR_PEAKRATE              0x10
+#define TBF_ATTR_MPU                   0x80
+/** @endcond */
+
+static inline struct rtnl_tbf *tbf_qdisc(struct rtnl_qdisc *qdisc)
+{
+       return (struct rtnl_tbf *) qdisc->q_subdata;
+}
+
+static inline struct rtnl_tbf *tbf_alloc(struct rtnl_qdisc *qdisc)
+{
+       if (!qdisc->q_subdata)
+               qdisc->q_subdata = calloc(1, sizeof(struct rtnl_tbf));
+
+       return tbf_qdisc(qdisc);
+}
+
+static struct nla_policy tbf_policy[TCA_TBF_MAX+1] = {
+       [TCA_TBF_PARMS] = { .minlen = sizeof(struct tc_tbf_qopt) },
+};
+
+static int tbf_msg_parser(struct rtnl_qdisc *q)
+{
+       int err;
+       struct nlattr *tb[TCA_TBF_MAX + 1];
+       struct rtnl_tbf *tbf;
+
+       err = tca_parse(tb, TCA_TBF_MAX, (struct rtnl_tca *) q, tbf_policy);
+       if (err < 0)
+               return err;
+       
+       tbf = tbf_qdisc(q);
+       if (!tbf)
+               return nl_errno(ENOMEM);
+
+       if (tb[TCA_TBF_PARMS]) {
+               struct tc_tbf_qopt opts;
+               int bufsize;
+
+               nla_memcpy(&opts, tb[TCA_TBF_PARMS], sizeof(opts));
+               tbf->qt_limit = opts.limit;
+               tbf->qt_mpu = opts.rate.mpu;
+       
+               rtnl_copy_ratespec(&tbf->qt_rate, &opts.rate);
+               tbf->qt_rate_txtime = opts.buffer;
+               bufsize = rtnl_tc_calc_bufsize(nl_ticks2us(opts.buffer),
+                                              opts.rate.rate);
+               tbf->qt_rate_bucket = bufsize;
+
+               rtnl_copy_ratespec(&tbf->qt_peakrate, &opts.peakrate);
+               tbf->qt_peakrate_txtime = opts.mtu;
+               bufsize = rtnl_tc_calc_bufsize(nl_ticks2us(opts.mtu),
+                                              opts.peakrate.rate);
+               tbf->qt_peakrate_bucket = bufsize;
+
+               tbf->qt_mask = (TBF_ATTR_LIMIT | TBF_ATTR_MPU | TBF_ATTR_RATE |
+                               TBF_ATTR_PEAKRATE);
+       }
+
+       return 0;
+}
+
+static int tbf_dump_brief(struct rtnl_qdisc *qdisc, struct nl_dump_params *p,
+                         int line)
+{
+       double r, rbit, lim;
+       char *ru, *rubit, *limu;
+       struct rtnl_tbf *tbf = tbf_qdisc(qdisc);
+
+       if (!tbf)
+               goto ignore;
+
+       r = nl_cancel_down_bytes(tbf->qt_rate.rs_rate, &ru);
+       rbit = nl_cancel_down_bits(tbf->qt_rate.rs_rate*8, &rubit);
+       lim = nl_cancel_down_bytes(tbf->qt_limit, &limu);
+
+       dp_dump(p, " rate %.2f%s/s (%.0f%s) limit %.2f%s",
+               r, ru, rbit, rubit, lim, limu);
+
+ignore:
+       return line;
+}
+
+static int tbf_dump_full(struct rtnl_qdisc *qdisc, struct nl_dump_params *p,
+                        int line)
+{
+       struct rtnl_tbf *tbf = tbf_qdisc(qdisc);
+
+       if (!tbf)
+               goto ignore;
+
+       if (1) {
+               char *bu, *cu;
+               double bs = nl_cancel_down_bytes(tbf->qt_rate_bucket, &bu);
+               double cl = nl_cancel_down_bytes(1 << tbf->qt_rate.rs_cell_log,
+                                                &cu);
+
+               dp_dump(p, "mpu %u rate-bucket-size %1.f%s "
+                          "rate-cell-size %.1f%s\n",
+                       tbf->qt_mpu, bs, bu, cl, cu);
+
+       }
+
+       if (tbf->qt_mask & TBF_ATTR_PEAKRATE) {
+               char *pru, *prbu, *bsu, *clu;
+               double pr, prb, bs, cl;
+               
+               pr = nl_cancel_down_bytes(tbf->qt_peakrate.rs_rate, &pru);
+               prb = nl_cancel_down_bits(tbf->qt_peakrate.rs_rate * 8, &prbu);
+               bs = nl_cancel_down_bits(tbf->qt_peakrate_bucket, &bsu);
+               cl = nl_cancel_down_bits(1 << tbf->qt_peakrate.rs_cell_log,
+                                        &clu);
+
+               dp_dump_line(p, line++, "    peak-rate %.2f%s/s (%.0f%s) "
+                                       "bucket-size %.1f%s cell-size %.1f%s",
+                                       "latency %.1f%s",
+                            pr, pru, prb, prbu, bs, bsu, cl, clu);
+       }
+
+ignore:
+       return line;
+}
+
+static struct nl_msg *tbf_get_opts(struct rtnl_qdisc *qdisc)
+{
+       struct tc_tbf_qopt opts;
+       struct rtnl_tbf *tbf;
+       struct nl_msg *msg;
+       uint32_t rtab[RTNL_TC_RTABLE_SIZE];
+       uint32_t ptab[RTNL_TC_RTABLE_SIZE];
+       int required = TBF_ATTR_RATE | TBF_ATTR_LIMIT;
+
+       memset(&opts, 0, sizeof(opts));
+
+       tbf = tbf_qdisc(qdisc);
+       if (!tbf)
+               return NULL;
+
+       if (!(tbf->qt_mask & required) != required)
+               return NULL;
+
+       opts.limit = tbf->qt_limit;
+       opts.buffer = tbf->qt_rate_txtime;
+       tbf->qt_rate.rs_mpu = tbf->qt_mpu;
+       rtnl_rcopy_ratespec(&opts.rate, &tbf->qt_rate);
+
+       rtnl_tc_build_rate_table(rtab, tbf->qt_mpu & 0xff, tbf->qt_mpu >> 8,
+                                1 << tbf->qt_rate.rs_cell_log,
+                                tbf->qt_rate.rs_rate);
+
+       if (tbf->qt_mask & TBF_ATTR_PEAKRATE) {
+               opts.mtu = tbf->qt_peakrate_txtime;
+               tbf->qt_peakrate.rs_mpu = tbf->qt_mpu;
+               rtnl_rcopy_ratespec(&opts.peakrate, &tbf->qt_peakrate);
+
+               rtnl_tc_build_rate_table(ptab, tbf->qt_mpu & 0xff,
+                                        tbf->qt_mpu >> 8,
+                                        1 << tbf->qt_peakrate.rs_cell_log,
+                                        tbf->qt_peakrate.rs_rate);
+       }
+
+       msg = nlmsg_build_no_hdr();
+       if (!msg)
+               goto nla_put_failure;
+
+       NLA_PUT(msg, TCA_TBF_PARMS, sizeof(opts), &opts);
+       NLA_PUT(msg, TCA_TBF_RTAB, sizeof(rtab), rtab);
+
+       if (tbf->qt_mask & TBF_ATTR_PEAKRATE)
+               NLA_PUT(msg, TCA_TBF_PTAB, sizeof(ptab), ptab);
+
+       return msg;
+
+nla_put_failure:
+       nlmsg_free(msg);
+       return NULL;
+}
+
+/**
+ * @name Attribute Access
+ * @{
+ */
+
+/**
+ * Set limit of TBF qdisc.
+ * @arg qdisc          TBF qdisc to be modified.
+ * @arg limit          New limit in bytes.
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_qdisc_tbf_set_limit(struct rtnl_qdisc *qdisc, int limit)
+{
+       struct rtnl_tbf *tbf;
+       
+       tbf = tbf_alloc(qdisc);
+       if (!tbf)
+               return nl_errno(ENOMEM);
+
+       tbf->qt_limit = limit;
+       tbf->qt_mask |= TBF_ATTR_LIMIT;
+
+       return 0;
+}
+
+static inline double calc_limit(struct rtnl_ratespec *spec, int latency,
+                               int bucket)
+{
+       double limit;
+
+       limit = (double) spec->rs_rate * ((double) latency / 1000000.);
+       limit += bucket;
+
+       return limit;
+}
+
+/**
+ * Set limit of TBF qdisc by latency.
+ * @arg qdisc          TBF qdisc to be modified.
+ * @arg latency                Latency in micro seconds.
+ *
+ * Calculates and sets the limit based on the desired latency and the
+ * configured rate and peak rate. In order for this operation to succeed,
+ * the rate and if required the peak rate must have been set in advance.
+ *
+ * @f[
+ *   limit_n = \frac{{rate_n} \times {latency}}{10^6}+{bucketsize}_n
+ * @f]
+ * @f[
+ *   limit = min(limit_{rate},limit_{peak})
+ * @f]
+ * 
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_qdisc_tbf_set_limit_by_latency(struct rtnl_qdisc *qdisc, int latency)
+{
+       struct rtnl_tbf *tbf;
+       double limit, limit2;
+
+       tbf = tbf_alloc(qdisc);
+       if (!tbf)
+               return nl_errno(ENOMEM);
+
+       if (!(tbf->qt_mask & TBF_ATTR_RATE))
+               return nl_error(EINVAL, "The rate must be specified before "
+                               "limit can be calculated based on latency.");
+
+       limit = calc_limit(&tbf->qt_rate, latency, tbf->qt_rate_bucket);
+
+       if (tbf->qt_mask & TBF_ATTR_PEAKRATE) {
+               limit2 = calc_limit(&tbf->qt_peakrate, latency,
+                                   tbf->qt_peakrate_bucket);
+
+               if (limit2 < limit)
+                       limit = limit2;
+       }
+
+       return rtnl_qdisc_tbf_set_limit(qdisc, (int) limit);
+}
+
+/**
+ * Get limit of TBF qdisc.
+ * @arg qdisc          TBF qdisc.
+ * @return Limit in bytes or a negative error code.
+ */
+int rtnl_qdisc_tbf_get_limit(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_tbf *tbf;
+       
+       tbf = tbf_qdisc(qdisc);
+       if (tbf && (tbf->qt_mask & TBF_ATTR_LIMIT))
+               return tbf->qt_limit;
+       return
+               nl_errno(ENOENT);
+}
+
+/**
+ * Set MPU of TBF qdisc.
+ * @arg qdisc          TBF qdisc to be modified.
+ * @arg mpu            New MPU in bytes.
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_qdisc_tbf_set_mpu(struct rtnl_qdisc *qdisc, int mpu)
+{
+       struct rtnl_tbf *tbf;
+       
+       tbf = tbf_alloc(qdisc);
+       if (!tbf)
+               return nl_errno(ENOMEM);
+
+       tbf->qt_mpu = mpu;
+       tbf->qt_mask |= TBF_ATTR_MPU;
+
+       return 0;
+}
+
+/**
+ * Get MPU of TBF qdisc.
+ * @arg qdisc          TBF qdisc.
+ * @return MPU in bytes or a negative error code.
+ */
+int rtnl_qdisc_tbf_get_mpu(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_tbf *tbf;
+       
+       tbf = tbf_qdisc(qdisc);
+       if (tbf && (tbf->qt_mask & TBF_ATTR_MPU))
+               return tbf->qt_mpu;
+       return
+               nl_errno(ENOENT);
+}
+
+static inline int calc_cell_log(int cell, int bucket)
+{
+       if (cell > 0)
+               cell = rtnl_tc_calc_cell_log(cell);
+       else {
+               cell = 0;
+
+               if (!bucket)
+                       bucket = 2047; /* defaults to cell_log=3 */
+
+               while ((bucket >> cell) > 255)
+                       cell++;
+       }
+
+       return cell;
+}
+
+/**
+ * Set rate of TBF qdisc.
+ * @arg qdisc          TBF qdisc to be modified.
+ * @arg rate           New rate in bytes per second.
+ * @arg bucket         Size of bucket in bytes.
+ * @arg cell           Size of a rate cell or 0 to get default value.
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_qdisc_tbf_set_rate(struct rtnl_qdisc *qdisc, int rate, int bucket,
+                           int cell)
+{
+       struct rtnl_tbf *tbf;
+       int cell_log;
+       
+       tbf = tbf_alloc(qdisc);
+       if (!tbf)
+               return nl_errno(ENOMEM);
+
+       cell_log = calc_cell_log(cell, bucket);
+       if (cell_log < 0)
+               return cell_log;
+
+       tbf->qt_rate.rs_rate = rate;
+       tbf->qt_rate_bucket = bucket;
+       tbf->qt_rate.rs_cell_log = cell_log;
+       tbf->qt_rate_txtime = rtnl_tc_calc_txtime(bucket, rate);
+       tbf->qt_mask |= TBF_ATTR_RATE;
+
+       return 0;
+}
+
+/**
+ * Get rate of TBF qdisc.
+ * @arg qdisc          TBF qdisc.
+ * @return Rate in bytes per seconds or a negative error code.
+ */
+int rtnl_qdisc_tbf_get_rate(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_tbf *tbf;
+
+       tbf = tbf_qdisc(qdisc);
+       if (tbf && (tbf->qt_mask & TBF_ATTR_RATE))
+               return tbf->qt_rate.rs_rate;
+       else
+               return -1;
+}
+
+/**
+ * Get rate bucket size of TBF qdisc.
+ * @arg qdisc          TBF qdisc.
+ * @return Size of rate bucket or a negative error code.
+ */
+int rtnl_qdisc_tbf_get_rate_bucket(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_tbf *tbf;
+
+       tbf = tbf_qdisc(qdisc);
+       if (tbf && (tbf->qt_mask & TBF_ATTR_RATE))
+               return tbf->qt_rate_bucket;
+       else
+               return -1;
+}
+
+/**
+ * Get rate cell size of TBF qdisc.
+ * @arg qdisc          TBF qdisc.
+ * @return Size of rate cell in bytes or a negative error code.
+ */
+int rtnl_qdisc_tbf_get_rate_cell(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_tbf *tbf;
+
+       tbf = tbf_qdisc(qdisc);
+       if (tbf && (tbf->qt_mask & TBF_ATTR_RATE))
+               return (1 << tbf->qt_rate.rs_cell_log);
+       else
+               return -1;
+}
+
+/**
+ * Set peak rate of TBF qdisc.
+ * @arg qdisc          TBF qdisc to be modified.
+ * @arg rate           New peak rate in bytes per second.
+ * @arg bucket         Size of peakrate bucket.
+ * @arg cell           Size of a peakrate cell or 0 to get default value.
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_qdisc_tbf_set_peakrate(struct rtnl_qdisc *qdisc, int rate, int bucket,
+                               int cell)
+{
+       struct rtnl_tbf *tbf;
+       int cell_log;
+       
+       tbf = tbf_alloc(qdisc);
+       if (!tbf)
+               return nl_errno(ENOMEM);
+
+       cell_log = calc_cell_log(cell, bucket);
+       if (cell_log < 0)
+               return cell_log;
+
+       tbf->qt_peakrate.rs_rate = rate;
+       tbf->qt_peakrate_bucket = bucket;
+       tbf->qt_peakrate.rs_cell_log = cell_log;
+       tbf->qt_peakrate_txtime = rtnl_tc_calc_txtime(bucket, rate);
+       
+       tbf->qt_mask |= TBF_ATTR_PEAKRATE;
+
+       return 0;
+}
+
+/**
+ * Get peak rate of TBF qdisc.
+ * @arg qdisc          TBF qdisc.
+ * @return Peak rate in bytes per seconds or a negative error code.
+ */
+int rtnl_qdisc_tbf_get_peakrate(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_tbf *tbf;
+
+       tbf = tbf_qdisc(qdisc);
+       if (tbf && (tbf->qt_mask & TBF_ATTR_PEAKRATE))
+               return tbf->qt_peakrate.rs_rate;
+       else
+               return -1;
+}
+
+/**
+ * Get peak rate bucket size of TBF qdisc.
+ * @arg qdisc          TBF qdisc.
+ * @return Size of peak rate bucket or a negative error code.
+ */
+int rtnl_qdisc_tbf_get_peakrate_bucket(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_tbf *tbf;
+
+       tbf = tbf_qdisc(qdisc);
+       if (tbf && (tbf->qt_mask & TBF_ATTR_PEAKRATE))
+               return tbf->qt_peakrate_bucket;
+       else
+               return -1;
+}
+
+/**
+ * Get peak rate cell size of TBF qdisc.
+ * @arg qdisc          TBF qdisc.
+ * @return Size of peak rate cell in bytes or a negative error code.
+ */
+int rtnl_qdisc_tbf_get_peakrate_cell(struct rtnl_qdisc *qdisc)
+{
+       struct rtnl_tbf *tbf;
+
+       tbf = tbf_qdisc(qdisc);
+       if (tbf && (tbf->qt_mask & TBF_ATTR_PEAKRATE))
+               return (1 << tbf->qt_peakrate.rs_cell_log);
+       else
+               return -1;
+}
+
+/** @} */
+
+static struct rtnl_qdisc_ops tbf_qdisc_ops = {
+       .qo_kind                = "tbf",
+       .qo_msg_parser          = tbf_msg_parser,
+       .qo_dump[NL_DUMP_BRIEF] = tbf_dump_brief,
+       .qo_dump[NL_DUMP_FULL]  = tbf_dump_full,
+       .qo_get_opts            = tbf_get_opts,
+};
+
+static void __init tbf_init(void)
+{
+       rtnl_qdisc_register(&tbf_qdisc_ops);
+}
+
+static void __exit tbf_exit(void)
+{
+       rtnl_qdisc_unregister(&tbf_qdisc_ops);
+}
+
+/** @} */
diff --git a/lib/route/tc.c b/lib/route/tc.c
new file mode 100644 (file)
index 0000000..fc2069d
--- /dev/null
@@ -0,0 +1,566 @@
+/*
+ * lib/route/tc.c              Traffic Control
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup rtnl
+ * @defgroup tc Traffic Control
+ * @brief
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink-tc.h>
+#include <netlink/netlink.h>
+#include <netlink/utils.h>
+#include <netlink/route/rtnl.h>
+#include <netlink/route/link.h>
+#include <netlink/route/tc.h>
+
+/** @cond SKIP */
+
+static struct nla_policy tc_policy[TCA_MAX+1] = {
+       [TCA_KIND]      = { .type = NLA_STRING,
+                           .maxlen = TCKINDSIZ },
+       [TCA_STATS]     = { .minlen = sizeof(struct tc_stats) },
+       [TCA_STATS2]    = { .type = NLA_NESTED },
+};
+
+int tca_parse(struct nlattr **tb, int maxattr, struct rtnl_tca *g,
+             struct nla_policy *policy)
+{
+       
+       if (g->tc_mask & TCA_ATTR_OPTS)
+               return nla_parse(tb, maxattr,
+                                (struct nlattr *) g->tc_opts->d_data,
+                                g->tc_opts->d_size, policy);
+       else {
+               /* Ugly but tb[] must be in a defined state even if no
+                * attributes can be found. */
+               memset(tb, 0, sizeof(struct nlattr *) * (maxattr + 1));
+               return 0;
+       }
+}
+
+static struct nla_policy tc_stats2_policy[TCA_STATS_MAX+1] = {
+       [TCA_STATS_BASIC]    = { .minlen = sizeof(struct gnet_stats_basic) },
+       [TCA_STATS_RATE_EST] = { .minlen = sizeof(struct gnet_stats_rate_est) },
+       [TCA_STATS_QUEUE]    = { .minlen = sizeof(struct gnet_stats_queue) },
+};
+
+int tca_msg_parser(struct nlmsghdr *n, struct rtnl_tca *g)
+{
+       struct nlattr *tb[TCA_MAX + 1];
+       struct tcmsg *tm;
+       int err;
+
+       err = nlmsg_parse(n, sizeof(*tm), tb, TCA_MAX, tc_policy);
+       if (err < 0)
+               return err;
+
+       if (tb[TCA_KIND] == NULL)
+               return nl_error(EINVAL, "Missing tca kind TLV");
+
+       nla_strlcpy(g->tc_kind, tb[TCA_KIND], TCKINDSIZ);
+
+       tm = nlmsg_data(n);
+       g->tc_family  = tm->tcm_family;
+       g->tc_ifindex = tm->tcm_ifindex;
+       g->tc_handle  = tm->tcm_handle;
+       g->tc_parent  = tm->tcm_parent;
+       g->tc_info    = tm->tcm_info;
+
+       g->tc_mask = (TCA_ATTR_FAMILY|TCA_ATTR_IFINDEX|TCA_ATTR_HANDLE|
+               TCA_ATTR_PARENT|TCA_ATTR_INFO|TCA_ATTR_KIND);
+
+       if (tb[TCA_OPTIONS]) {
+               g->tc_opts = nla_get_data(tb[TCA_OPTIONS]);
+               if (!g->tc_opts)
+                       return nl_errno(ENOMEM);
+               g->tc_mask |= TCA_ATTR_OPTS;
+       }
+       
+
+       if (tb[TCA_STATS2]) {
+               struct nlattr *tbs[TCA_STATS_MAX + 1];
+
+               err = nla_parse_nested(tbs, TCA_STATS_MAX, tb[TCA_STATS2],
+                                      tc_stats2_policy);
+               if (err < 0)
+                       return err;
+
+               if (tbs[TCA_STATS_BASIC]) {
+                       struct gnet_stats_basic *bs;
+                       
+                       bs = nla_data(tbs[TCA_STATS_BASIC]);
+                       g->tc_stats[RTNL_TC_BYTES]      = bs->bytes;
+                       g->tc_stats[RTNL_TC_PACKETS]    = bs->packets;
+               }
+
+               if (tbs[TCA_STATS_RATE_EST]) {
+                       struct gnet_stats_rate_est *re;
+
+                       re = nla_data(tbs[TCA_STATS_RATE_EST]);
+                       g->tc_stats[RTNL_TC_RATE_BPS]   = re->bps;
+                       g->tc_stats[RTNL_TC_RATE_PPS]   = re->pps;
+               }
+               
+               if (tbs[TCA_STATS_QUEUE]) {
+                       struct gnet_stats_queue *q;
+
+                       q = nla_data(tbs[TCA_STATS_QUEUE]);
+                       g->tc_stats[RTNL_TC_QLEN]       = q->qlen;
+                       g->tc_stats[RTNL_TC_BACKLOG]    = q->backlog;
+                       g->tc_stats[RTNL_TC_DROPS]      = q->drops;
+                       g->tc_stats[RTNL_TC_REQUEUES]   = q->requeues;
+                       g->tc_stats[RTNL_TC_OVERLIMITS] = q->overlimits;
+               }
+
+               g->tc_mask |= TCA_ATTR_STATS;
+               
+               if (tbs[TCA_STATS_APP]) {
+                       g->tc_xstats = nla_get_data(tbs[TCA_STATS_APP]);
+                       if (g->tc_xstats == NULL)
+                               return -ENOMEM;
+               } else
+                       goto compat_xstats;
+       } else {
+               if (tb[TCA_STATS]) {
+                       struct tc_stats *st = nla_data(tb[TCA_STATS]);
+
+                       g->tc_stats[RTNL_TC_BYTES]      = st->bytes;
+                       g->tc_stats[RTNL_TC_PACKETS]    = st->packets;
+                       g->tc_stats[RTNL_TC_RATE_BPS]   = st->bps;
+                       g->tc_stats[RTNL_TC_RATE_PPS]   = st->pps;
+                       g->tc_stats[RTNL_TC_QLEN]       = st->qlen;
+                       g->tc_stats[RTNL_TC_BACKLOG]    = st->backlog;
+                       g->tc_stats[RTNL_TC_DROPS]      = st->drops;
+                       g->tc_stats[RTNL_TC_OVERLIMITS] = st->overlimits;
+
+                       g->tc_mask |= TCA_ATTR_STATS;
+               }
+
+compat_xstats:
+               if (tb[TCA_XSTATS]) {
+                       g->tc_xstats = nla_get_data(tb[TCA_XSTATS]);
+                       if (g->tc_xstats == NULL)
+                               return -ENOMEM;
+                       g->tc_mask |= TCA_ATTR_XSTATS;
+               }
+       }
+
+
+       return 0;
+}
+
+void tca_free_data(struct rtnl_tca *tca)
+{
+       nl_data_free(tca->tc_opts);
+       nl_data_free(tca->tc_xstats);
+}
+
+int tca_dump_brief(struct rtnl_tca *g, const char *type,
+                  struct nl_dump_params *p, int line)
+{
+       char handle[32], parent[32];
+       struct nl_cache *link_cache;
+       
+       link_cache = nl_cache_mngt_require("route/link");
+
+       dp_dump(p, "%s %s ", g->tc_kind, type);
+
+       if (link_cache) {
+               char buf[32];
+               dp_dump(p, "dev %s ",
+                       rtnl_link_i2name(link_cache, g->tc_ifindex,
+                                        buf, sizeof(buf)));
+       } else
+               dp_dump(p, "dev %u ", g->tc_ifindex);
+       
+       dp_dump(p, "handle %s parent %s",
+               rtnl_tc_handle2str(g->tc_handle, handle, sizeof(handle)),
+               rtnl_tc_handle2str(g->tc_parent, parent, sizeof(parent)));
+
+       return 1;
+}
+
+int tca_dump_full(struct rtnl_tca *g, struct nl_dump_params *p, int line)
+{
+       dp_dump_line(p, line++, "  ");
+       return line;
+}
+
+int tca_dump_stats(struct rtnl_tca *g, struct nl_dump_params *p, int line)
+{
+       char *unit, fmt[64];
+       float res;
+       strcpy(fmt, "        %7.2f %s %10u %10u %10u %10u %10u\n");
+
+       dp_dump_line(p, line++,
+               "    Stats:    bytes    packets      drops overlimits" \
+               "       qlen    backlog\n");
+
+       res = nl_cancel_down_bytes(g->tc_stats[RTNL_TC_BYTES], &unit);
+       if (*unit == 'B')
+               fmt[11] = '9';
+
+       dp_dump_line(p, line++, fmt, res, unit,
+               g->tc_stats[RTNL_TC_PACKETS],
+               g->tc_stats[RTNL_TC_DROPS],
+               g->tc_stats[RTNL_TC_OVERLIMITS],
+               g->tc_stats[RTNL_TC_QLEN],
+               g->tc_stats[RTNL_TC_BACKLOG]);
+
+       res = nl_cancel_down_bytes(g->tc_stats[RTNL_TC_RATE_BPS], &unit);
+
+       strcpy(fmt, "        %7.2f %s/s%9u pps");
+
+       if (*unit == 'B')
+               fmt[11] = '9';
+
+       dp_dump_line(p, line++, fmt, res, unit, g->tc_stats[RTNL_TC_RATE_PPS]);
+
+       return line;
+}
+
+int tca_filter(struct rtnl_tca *o, struct rtnl_tca *f)
+{
+#define REQ(F) (f->tc_mask & TCA_ATTR_##F)
+#define AVAIL(F) (o->tc_mask & TCA_ATTR_##F)
+#define _O(F, EXPR) (REQ(F) && (!AVAIL(F) || (EXPR)))
+#define _C(F, N) (REQ(F) && (!AVAIL(F) || (o->N != f->N)))
+       if (_C(HANDLE,  tc_handle)                      ||
+           _C(PARENT,  tc_parent)                      ||
+           _C(IFINDEX, tc_ifindex)                     ||
+           _O(KIND,    strcmp(o->tc_kind, f->tc_kind)))
+               return 0;
+#undef REQ
+#undef AVAIL
+#undef _O
+#undef _C
+
+       return 1;
+}
+
+void tca_set_ifindex(struct rtnl_tca *t, int ifindex)
+{
+       t->tc_ifindex = ifindex;
+       t->tc_mask |= TCA_ATTR_IFINDEX;
+}
+
+int tca_get_ifindex(struct rtnl_tca *t)
+{
+       if (t->tc_mask & TCA_ATTR_IFINDEX)
+               return t->tc_ifindex;
+       else
+               return RTNL_LINK_NOT_FOUND;
+}
+
+void tca_set_handle(struct rtnl_tca *t, uint32_t handle)
+{
+       t->tc_handle = handle;
+       t->tc_mask |= TCA_ATTR_HANDLE;
+}
+
+uint32_t tca_get_handle(struct rtnl_tca *t)
+{
+       if (t->tc_mask & TCA_ATTR_HANDLE)
+               return t->tc_handle;
+       else
+               return 0;
+}
+
+void tca_set_parent(struct rtnl_tca *t, uint32_t parent)
+{
+       t->tc_parent = parent;
+       t->tc_mask |= TCA_ATTR_PARENT;
+}
+
+uint32_t tca_get_parent(struct rtnl_tca *t)
+{
+       if (t->tc_mask & TCA_ATTR_PARENT)
+               return t->tc_parent;
+       else
+               return 0;
+}
+
+void tca_set_kind(struct rtnl_tca *t, const char *kind)
+{
+       strncpy(t->tc_kind, kind, sizeof(t->tc_kind) - 1);
+       t->tc_mask |= TCA_ATTR_KIND;
+}
+
+char *tca_get_kind(struct rtnl_tca *t)
+{
+       if (t->tc_mask & TCA_ATTR_KIND)
+               return t->tc_kind;
+       else
+               return NULL;
+}
+
+uint64_t tca_get_stat(struct rtnl_tca *t, int id)
+{
+       if (id < 0 || id > RTNL_TC_STATS_MAX)
+               return 0;
+
+       return t->tc_stats[id];
+}
+
+struct nl_msg *tca_build_msg(struct rtnl_tca *tca, int type, int flags)
+{
+       struct nl_msg *msg;
+       struct tcmsg tchdr = {
+               .tcm_family = AF_UNSPEC,
+               .tcm_ifindex = tca->tc_ifindex,
+               .tcm_handle = tca->tc_handle,
+               .tcm_parent = tca->tc_parent,
+       };
+
+       msg = nlmsg_build_simple(type, flags);
+       if (!msg)
+               goto nla_put_failure;
+
+       if (nlmsg_append(msg, &tchdr, sizeof(tchdr), 1) < 0)
+               goto nla_put_failure;
+
+       if (tca->tc_mask & TCA_ATTR_KIND)
+           NLA_PUT_STRING(msg, TCA_KIND, tca->tc_kind);
+
+       return msg;
+
+nla_put_failure:
+       nlmsg_free(msg);
+       return NULL;
+}
+
+/** @endcond */
+
+/**
+ * @name Utilities
+ * @{
+ */
+
+/**
+ * Calculate time required to transmit buffer at a specific rate
+ * @arg bufsize                Size of buffer to be transmited in bytes.
+ * @arg rate           Transmit rate in bytes per second.
+ *
+ * Calculates the number of micro seconds required to transmit a
+ * specific buffer at a specific transmit rate.
+ *
+ * @f[
+ *   txtime=\frac{bufsize}{rate}10^6
+ * @f]
+ * 
+ * @return Required transmit time in micro seconds.
+ */
+int rtnl_tc_calc_txtime(int bufsize, int rate)
+{
+       double tx_time_secs;
+       
+       tx_time_secs = (double) bufsize / (double) rate;
+
+       return tx_time_secs * 1000000.;
+}
+
+/**
+ * Calculate buffer size able to transmit in a specific time and rate.
+ * @arg txtime         Available transmit time in micro seconds.
+ * @arg rate           Transmit rate in bytes per second.
+ *
+ * Calculates the size of the buffer that can be transmitted in a
+ * specific time period at a specific transmit rate.
+ *
+ * @f[
+ *   bufsize=\frac{{txtime} \times {rate}}{10^6}
+ * @f]
+ *
+ * @return Size of buffer in bytes.
+ */
+int rtnl_tc_calc_bufsize(int txtime, int rate)
+{
+       double bufsize;
+
+       bufsize = (double) txtime * (double) rate;
+
+       return bufsize / 1000000.;
+}
+
+/**
+ * Calculate the binary logarithm for a specific cell size
+ * @arg cell_size      Size of cell, must be a power of two.
+ * @return Binary logirhtm of cell size or a negative error code.
+ */
+int rtnl_tc_calc_cell_log(int cell_size)
+{
+       int i;
+
+       for (i = 0; i < 32; i++)
+               if ((1 << i) == cell_size)
+                       return i;
+
+       return nl_errno(EINVAL);
+}
+
+
+/** @} */
+
+/**
+ * @name Rate Tables
+ * @{
+ */
+
+/**
+ * Compute a transmission time lookup table
+ * @arg dst     Destination buffer of RTNL_TC_RTABLE_SIZE uint32_t[].
+ * @arg mpu     Minimal size of a packet at all times.
+ * @arg overhead Overhead to be added to each packet.
+ * @arg cell    Size of cell, i.e. size of step between entries in bytes.
+ * @arg rate    Rate in bytes per second.
+ *
+ * Computes a table of RTNL_TC_RTABLE_SIZE entries specyfing the
+ * transmission times for various packet sizes, e.g. the transmission
+ * time for a packet of size \c pktsize could be looked up:
+ * @code
+ * txtime = table[pktsize >> log2(cell)];
+ * @endcode
+ */
+int rtnl_tc_build_rate_table(uint32_t *dst, uint8_t mpu, uint8_t overhead,
+                            int cell, int rate)
+{
+       int i, size, cell_log;
+
+       cell_log = rtnl_tc_calc_cell_log(cell);
+       if (cell_log < 0)
+               return cell_log;
+
+       for (i = 0; i < RTNL_TC_RTABLE_SIZE; i++) {
+               size = (i << cell_log) + overhead;
+               if (size < mpu)
+                       size = mpu;
+
+               dst[i] = rtnl_tc_calc_txtime(size, rate);
+       }
+
+       return 0;
+}
+
+/** @} */
+
+/**
+ * @name Traffic Control Handle Translations
+ * @{
+ */
+
+/**
+ * Convert a traffic control handle to a character string (Reentrant).
+ * @arg handle         traffic control handle
+ * @arg buf            destination buffer
+ * @arg len            buffer length
+ *
+ * Converts a tarffic control handle to a character string in the
+ * form of \c MAJ:MIN and stores it in the specified destination buffer.
+ *
+ * @return The destination buffer or the type encoded in hexidecimal
+ *         form if no match was found.
+ */
+char * rtnl_tc_handle2str(uint32_t handle, char *buf, size_t len)
+{
+       if (TC_H_ROOT == handle)
+               snprintf(buf, len, "root");
+       else if (TC_H_UNSPEC == handle)
+               snprintf(buf, len, "none");
+       else if (0 == TC_H_MAJ(handle))
+               snprintf(buf, len, ":%02x", TC_H_MIN(handle));
+       else if (0 == TC_H_MIN(handle))
+               snprintf(buf, len, "%02x:", TC_H_MAJ(handle) >> 16);
+       else
+               snprintf(buf, len, "%02x:%02x",
+                       TC_H_MAJ(handle) >> 16, TC_H_MIN(handle));
+
+       return buf;
+}
+
+/**
+ * Convert a charactering strint to a traffic control handle
+ * @arg name           traffic control handle as character string
+ * @arg res            destination buffer
+ *
+ * Converts the provided character string specifying a traffic
+ * control handle to the corresponding numeric value.
+ *
+ * The handle must be provided in one of the following formats:
+ *  - root
+ *  - none
+ *  - XXXX:
+ *  - :YYYY
+ *  - XXXX:YYYY
+ *  - XXXXYYYY
+ *
+ * @return 0 on success or a negative error code
+ */
+int rtnl_tc_str2handle(const char *name, uint32_t *res)
+{
+       char *colon, *end;
+       uint32_t h;
+
+       if (!strcasecmp(name, "root")) {
+               *res = TC_H_ROOT;
+               return 0;
+       }
+
+       if (!strcasecmp(name, "none")) {
+               *res = TC_H_UNSPEC;
+               return 0;
+       }
+
+       h = strtoul(name, &colon, 16);
+
+       if (colon == name) {
+               /* :YYYY */
+               h = 0;
+               if (':' != *colon)
+                       return -EINVAL;
+       }
+
+       if (':' == *colon) {
+               /* check if we would lose bits */
+               if (TC_H_MAJ(h))
+                       return -ERANGE;
+               h <<= 16;
+
+               if ('\0' == colon[1]) {
+                       /* XXXX: */
+                       *res = h;
+               } else {
+                       /* XXXX:YYYY */
+                       uint32_t l = strtoul(colon+1, &end, 16);
+
+                       /* check if we overlap with major part */
+                       if (TC_H_MAJ(l))
+                               return -ERANGE;
+
+                       if ('\0' != *end)
+                               return -EINVAL;
+
+                       *res = (h | l);
+               }
+       } else if ('\0' == *colon) {
+               /* XXXXYYYY */
+               *res = h;
+       } else
+               return -EINVAL;
+
+       return 0;
+}
+
+/** @} */
+
+/** @} */
diff --git a/lib/utils.c b/lib/utils.c
new file mode 100644 (file)
index 0000000..9055828
--- /dev/null
@@ -0,0 +1,635 @@
+/*
+ * lib/utils.c         Utility Functions
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @defgroup utils Utilities
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink/netlink.h>
+#include <netlink/utils.h>
+#include <linux/socket.h>
+
+/**
+ * Debug level
+ */
+int nl_debug = 0;
+
+/**
+ * @name Error Code Helpers
+ * @{
+ */
+
+static char *errbuf;
+static int nlerrno;
+
+/** @cond SKIP */
+int __nl_error(int err, const char *file, unsigned int line, const char *func,
+              const char *fmt, ...)
+{
+       char *user_err;
+       va_list args;
+
+       if (errbuf) {
+               free(errbuf);
+               errbuf = NULL;
+       }
+
+       nlerrno = err;
+
+       if (fmt) {
+               va_start(args, fmt);
+               vasprintf(&user_err, fmt, args);
+               va_end(args);
+       }
+
+#ifdef VERBOSE_ERRORS
+       asprintf(&errbuf, "%s:%u:%s: %s (errno = %s)",
+                file, line, func, fmt ? user_err : "", strerror(err));
+#else
+       asprintf(&errbuf, "%s (errno = %s)",
+                fmt ? user_err : "", strerror(err));
+#endif
+
+       if (fmt)
+               free(user_err);
+
+       return -err;
+}
+
+int nl_get_errno(void)
+{
+       return nlerrno;
+}
+
+/** @endcond */
+
+/**
+ * Return error message for an error code
+ * @return error message
+ */
+char *nl_geterror(void)
+{
+       if (errbuf)
+               return errbuf;
+
+       if (nlerrno)
+               return strerror(nlerrno);
+
+       return "Sucess\n";
+}
+
+/** @} */
+
+/**
+ * @name Unit Pretty-Printing
+ * @{
+ */
+
+/**
+ * Cancel down a byte counter
+ * @arg        l               byte counter
+ * @arg        unit            destination unit pointer
+ *
+ * Cancels down a byte counter until it reaches a reasonable
+ * unit. The chosen unit is assigned to \a unit.
+ * 
+ * @return The cancelled down byte counter in the new unit.
+ */
+double nl_cancel_down_bytes(unsigned long long l, char **unit)
+{
+       if (l >= 1099511627776LL) {
+               *unit = "TiB";
+               return ((double) l) / 1099511627776LL;
+       } else if (l >= 1073741824) {
+               *unit = "GiB";
+               return ((double) l) / 1073741824;
+       } else if (l >= 1048576) {
+               *unit = "MiB";
+               return ((double) l) / 1048576;
+       } else if (l >= 1024) {
+               *unit = "KiB";
+               return ((double) l) / 1024;
+       } else {
+               *unit = "B";
+               return (double) l;
+       }
+}
+
+/**
+ * Cancel down a bit counter
+ * @arg        l               bit counter
+ * @arg unit           destination unit pointer
+ *
+ * Cancels downa bit counter until it reaches a reasonable
+ * unit. The chosen unit is assigned to \a unit.
+ *
+ * @return The cancelled down bit counter in the new unit.
+ */
+double nl_cancel_down_bits(unsigned long long l, char **unit)
+{
+       if (l >= 1099511627776ULL) {
+               *unit = "Tbit";
+               return ((double) l) / 1099511627776ULL;
+       } else if (l >= 1073741824) {
+               *unit = "Gbit";
+               return ((double) l) / 1073741824;
+       } else if (l >= 1048576) {
+               *unit = "Mbit";
+               return ((double) l) / 1048576;
+       } else if (l >= 1024) {
+               *unit = "Kbit";
+               return ((double) l) / 1024;
+       } else {
+               *unit = "bit";
+               return (double) l;
+       }
+               
+}
+
+/**
+ * Cancel down a micro second value
+ * @arg        l               micro seconds
+ * @arg unit           destination unit pointer
+ *
+ * Cancels down a microsecond counter until it reaches a
+ * reasonable unit. The chosen unit is assigned to \a unit.
+ *
+ * @return The cancelled down microsecond in the new unit
+ */
+double nl_cancel_down_us(uint32_t l, char **unit)
+{
+       if (l >= 1000000) {
+               *unit = "s";
+               return ((double) l) / 1000000;
+       } else if (l >= 1000) {
+               *unit = "ms";
+               return ((double) l) / 1000;
+       } else {
+               *unit = "us";
+               return (double) l;
+       }
+}
+
+/** @} */
+
+/**
+ * @name Generic Unit Translations
+ * @{
+ */
+
+/**
+ * Convert a character string to a size
+ * @arg str            size encoded as character string
+ *
+ * Converts the specified size as character to the corresponding
+ * number of bytes.
+ *
+ * Supported formats are:
+ *  - b,kb/k,m/mb,gb/g for bytes
+ *  - bit,kbit/mbit/gbit
+ *
+ * @return The number of bytes or -1 if the string is unparseable
+ */
+long nl_size2int(const char *str)
+{
+       char *p;
+       long l = strtol(str, &p, 0);
+       if (p == str)
+               return -1;
+
+       if (*p) {
+               if (!strcasecmp(p, "kb") || !strcasecmp(p, "k"))
+                       l *= 1024;
+               else if (!strcasecmp(p, "gb") || !strcasecmp(p, "g"))
+                       l *= 1024*1024*1024;
+               else if (!strcasecmp(p, "gbit"))
+                       l *= 1024*1024*1024/8;
+               else if (!strcasecmp(p, "mb") || !strcasecmp(p, "m"))
+                       l *= 1024*1024;
+               else if (!strcasecmp(p, "mbit"))
+                       l *= 1024*1024/8;
+               else if (!strcasecmp(p, "kbit"))
+                       l *= 1024/8;
+               else if (!strcasecmp(p, "bit"))
+                       l /= 8;
+               else if (strcasecmp(p, "b") != 0)
+                       return -1;
+       }
+
+       return l;
+}
+
+/**
+ * Convert a character string to a probability
+ * @arg str            probability encoded as character string
+ *
+ * Converts the specified probability as character to the
+ * corresponding probability number.
+ *
+ * Supported formats are:
+ *  - 0.0-1.0
+ *  - 0%-100%
+ *
+ * @return The probability relative to NL_PROB_MIN and NL_PROB_MAX
+ */
+long nl_prob2int(const char *str)
+{
+       char *p;
+       double d = strtod(str, &p);
+
+       if (p == str)
+               return -1;
+
+       if (d > 1.0)
+               d /= 100.0f;
+
+       if (d > 1.0f || d < 0.0f)
+               return -1;
+
+       if (*p && strcmp(p, "%") != 0)
+               return -1;
+
+       return rint(d * NL_PROB_MAX);
+}
+
+/** @} */
+
+/**
+ * @name Time Translations
+ * @{
+ */
+
+#ifdef USER_HZ
+static uint32_t user_hz = USER_HZ;
+#else
+static uint32_t user_hz = 100;
+#endif
+
+static double ticks_per_usec = 1.0f;
+
+/* Retrieves the configured HZ and ticks/us value in the kernel.
+ * The value is cached. Supported ways of getting it:
+ *
+ * 1) environment variable
+ * 2) /proc/net/psched and sysconf
+ *
+ * Supports the environment variables:
+ *   PROC_NET_PSCHED  - may point to psched file in /proc
+ *   PROC_ROOT        - may point to /proc fs */ 
+static void __init get_psched_settings(void)
+{
+       char name[FILENAME_MAX];
+       FILE *fd;
+       int got_hz = 0, got_tick = 0;
+
+       if (getenv("HZ")) {
+               long hz = strtol(getenv("HZ"), NULL, 0);
+
+               if (LONG_MIN != hz && LONG_MAX != hz) {
+                       user_hz = hz;
+                       got_hz = 1;
+               }
+       }
+
+       if (!got_hz)
+               user_hz = sysconf(_SC_CLK_TCK);
+
+       if (getenv("TICKS_PER_USEC")) {
+               double t = strtod(getenv("TICKS_PER_USEC"), NULL);
+
+               ticks_per_usec = t;
+               got_tick = 1;
+       }
+               
+
+       if (getenv("PROC_NET_PSCHED"))
+               snprintf(name, sizeof(name), "%s", getenv("PROC_NET_PSCHED"));
+       else if (getenv("PROC_ROOT"))
+               snprintf(name, sizeof(name), "%s/net/psched",
+                        getenv("PROC_ROOT"));
+       else
+               strncpy(name, "/proc/net/psched", sizeof(name) - 1);
+
+       if ((fd = fopen(name, "r"))) {
+               uint32_t tick, us, nom;
+               int r = fscanf(fd, "%08x%08x%08x%*08x", &tick, &us, &nom);
+
+               if (4 == r && nom == 1000000 && !got_tick)
+                       ticks_per_usec = (double)tick/(double)us;
+                       
+               fclose(fd);
+       }
+}
+
+
+/**
+ * Return the value of HZ
+ */
+int nl_get_hz(void)
+{
+       return user_hz;
+}
+
+
+/**
+ * Convert micro seconds to ticks
+ * @arg us             micro seconds
+ * @return number of ticks
+ */
+uint32_t nl_us2ticks(uint32_t us)
+{
+       return us * ticks_per_usec;
+}
+
+
+/**
+ * Convert ticks to micro seconds
+ * @arg ticks          number of ticks
+ * @return microseconds
+ */
+uint32_t nl_ticks2us(uint32_t ticks)
+{
+       return ticks / ticks_per_usec;
+}
+
+long nl_time2int(const char *str)
+{
+       char *p;
+       long l = strtol(str, &p, 0);
+       if (p == str)
+               return -1;
+
+       if (*p) {
+               if (!strcasecmp(p, "min") == 0 || !strcasecmp(p, "m"))
+                       l *= 60;
+               else if (!strcasecmp(p, "hour") || !strcasecmp(p, "h"))
+                       l *= 60*60;
+               else if (!strcasecmp(p, "day") || !strcasecmp(p, "d"))
+                       l *= 60*60*24;
+               else if (strcasecmp(p, "s") != 0)
+                       return -1;
+       }
+
+       return l;
+}
+
+/**
+ * Convert milliseconds to a character string
+ * @arg msec           number of milliseconds
+ * @arg buf            destination buffer
+ * @arg len            buffer length
+ *
+ * Converts milliseconds to a character string split up in days, hours,
+ * minutes, seconds, and milliseconds and stores it in the specified
+ * destination buffer.
+ *
+ * @return The destination buffer.
+ */
+char * nl_msec2str(uint64_t msec, char *buf, size_t len)
+{
+       int i, split[5];
+       char *units[] = {"d", "h", "m", "s", "msec"};
+
+#define _SPLIT(idx, unit) if ((split[idx] = msec / unit) > 0) msec %= unit
+       _SPLIT(0, 86400000);    /* days */
+       _SPLIT(1, 3600000);     /* hours */
+       _SPLIT(2, 60000);       /* minutes */
+       _SPLIT(3, 1000);        /* seconds */
+#undef  _SPLIT
+       split[4] = msec;
+
+       memset(buf, 0, len);
+
+       for (i = 0; i < ARRAY_SIZE(split); i++) {
+               if (split[i] > 0) {
+                       char t[64];
+                       snprintf(t, sizeof(t), "%s%d%s",
+                                strlen(buf) ? " " : "", split[i], units[i]);
+                       strncat(buf, t, len - strlen(buf) - 1);
+               }
+       }
+
+       return buf;
+}
+
+/** @} */
+
+/**
+ * @name Link Layer Protocol Translations
+ * @{
+ */
+
+static struct trans_tbl llprotos[] = {
+       {0, "generic"},
+       __ADD(ARPHRD_ETHER,ether)
+       __ADD(ARPHRD_EETHER,eether)
+       __ADD(ARPHRD_AX25,ax25)
+       __ADD(ARPHRD_PRONET,pronet)
+       __ADD(ARPHRD_CHAOS,chaos)
+       __ADD(ARPHRD_IEEE802,ieee802)
+       __ADD(ARPHRD_ARCNET,arcnet)
+       __ADD(ARPHRD_APPLETLK,atalk)
+       __ADD(ARPHRD_DLCI,dlci)
+       __ADD(ARPHRD_ATM,atm)
+       __ADD(ARPHRD_METRICOM,metricom)
+       __ADD(ARPHRD_IEEE1394,ieee1394)
+#ifdef ARPHRD_EUI64
+       __ADD(ARPHRD_EUI64,eui64)
+#endif
+       __ADD(ARPHRD_INFINIBAND,infiniband)
+       __ADD(ARPHRD_SLIP,slip)
+       __ADD(ARPHRD_CSLIP,cslip)
+       __ADD(ARPHRD_SLIP6,slip6)
+       __ADD(ARPHRD_CSLIP6,cslip6)
+       __ADD(ARPHRD_RSRVD,rsrvd)
+       __ADD(ARPHRD_ADAPT,adapt)
+       __ADD(ARPHRD_ROSE,rose)
+       __ADD(ARPHRD_X25,x25)
+#ifdef ARPHRD_HWX25
+       __ADD(ARPHRD_HWX25,hwx25)
+#endif
+       __ADD(ARPHRD_PPP,ppp)
+       __ADD(ARPHRD_HDLC,hdlc)
+       __ADD(ARPHRD_LAPB,lapb)
+       __ADD(ARPHRD_DDCMP,ddcmp)
+       __ADD(ARPHRD_RAWHDLC,rawhdlc)
+       __ADD(ARPHRD_TUNNEL,ipip)
+       __ADD(ARPHRD_TUNNEL6,tunnel6)
+       __ADD(ARPHRD_FRAD,frad)
+       __ADD(ARPHRD_SKIP,skip)
+       __ADD(ARPHRD_LOOPBACK,loopback)
+       __ADD(ARPHRD_LOCALTLK,localtlk)
+       __ADD(ARPHRD_FDDI,fddi)
+       __ADD(ARPHRD_BIF,bif)
+       __ADD(ARPHRD_SIT,sit)
+       __ADD(ARPHRD_IPDDP,ip/ddp)
+       __ADD(ARPHRD_IPGRE,gre)
+       __ADD(ARPHRD_PIMREG,pimreg)
+       __ADD(ARPHRD_HIPPI,hippi)
+       __ADD(ARPHRD_ASH,ash)
+       __ADD(ARPHRD_ECONET,econet)
+       __ADD(ARPHRD_IRDA,irda)
+       __ADD(ARPHRD_FCPP,fcpp)
+       __ADD(ARPHRD_FCAL,fcal)
+       __ADD(ARPHRD_FCPL,fcpl)
+       __ADD(ARPHRD_FCFABRIC,fcfb_0)
+       __ADD(ARPHRD_FCFABRIC+1,fcfb_1)
+       __ADD(ARPHRD_FCFABRIC+2,fcfb_2)
+       __ADD(ARPHRD_FCFABRIC+3,fcfb_3)
+       __ADD(ARPHRD_FCFABRIC+4,fcfb_4)
+       __ADD(ARPHRD_FCFABRIC+5,fcfb_5)
+       __ADD(ARPHRD_FCFABRIC+6,fcfb_6)
+       __ADD(ARPHRD_FCFABRIC+7,fcfb_7)
+       __ADD(ARPHRD_FCFABRIC+8,fcfb_8)
+       __ADD(ARPHRD_FCFABRIC+9,fcfb_9)
+       __ADD(ARPHRD_FCFABRIC+10,fcfb_10)
+       __ADD(ARPHRD_FCFABRIC+11,fcfb_11)
+       __ADD(ARPHRD_FCFABRIC+12,fcfb_12)
+       __ADD(ARPHRD_IEEE802_TR,tr)
+       __ADD(ARPHRD_IEEE80211,ieee802.11)
+#ifdef ARPHRD_IEEE80211_PRISM
+       __ADD(ARPHRD_IEEE80211_PRISM, ieee802.11_prism)
+#endif
+#ifdef ARPHRD_VOID
+       __ADD(ARPHRD_VOID,void)
+#endif
+};
+
+/**
+ * Convert a link layer protocol to a character string (Reentrant).
+ * @arg llproto                link layer protocol
+ * @arg buf            destination buffer
+ * @arg len            buffer length
+ *
+ * Converts a link layer protocol to a character string and stores
+ * it in the specified destination buffer.
+ *
+ * @return The destination buffer or the type encoded in hexidecimal
+ *         form if no match was found.
+ */
+char * nl_llproto2str(int llproto, char *buf, size_t len)
+{
+       return __type2str(llproto, buf, len, llprotos, ARRAY_SIZE(llprotos));
+}
+
+/**
+ * Convert a character string to a link layer protocol
+ * @arg name           name of link layer protocol
+ *
+ * Converts the provided character string specifying a link layer
+ * protocl to the corresponding numeric value.
+ *
+ * @return link layer protocol or a negative value if none was found.
+ */
+int nl_str2llproto(const char *name)
+{
+       return __str2type(name, llprotos, ARRAY_SIZE(llprotos));
+}
+
+/** @} */
+
+
+/**
+ * @name Ethernet Protocol Translations
+ * @{
+ */
+
+static struct trans_tbl ether_protos[] = {
+       __ADD(ETH_P_LOOP,loop)
+       __ADD(ETH_P_PUP,pup)
+       __ADD(ETH_P_PUPAT,pupat)
+       __ADD(ETH_P_IP,ip)
+       __ADD(ETH_P_X25,x25)
+       __ADD(ETH_P_ARP,arp)
+       __ADD(ETH_P_BPQ,bpq)
+       __ADD(ETH_P_IEEEPUP,ieeepup)
+       __ADD(ETH_P_IEEEPUPAT,ieeepupat)
+       __ADD(ETH_P_DEC,dec)
+       __ADD(ETH_P_DNA_DL,dna_dl)
+       __ADD(ETH_P_DNA_RC,dna_rc)
+       __ADD(ETH_P_DNA_RT,dna_rt)
+       __ADD(ETH_P_LAT,lat)
+       __ADD(ETH_P_DIAG,diag)
+       __ADD(ETH_P_CUST,cust)
+       __ADD(ETH_P_SCA,sca)
+       __ADD(ETH_P_RARP,rarp)
+       __ADD(ETH_P_ATALK,atalk)
+       __ADD(ETH_P_AARP,aarp)
+#ifdef ETH_P_8021Q
+       __ADD(ETH_P_8021Q,802.1q)
+#endif
+       __ADD(ETH_P_IPX,ipx)
+       __ADD(ETH_P_IPV6,ipv6)
+#ifdef ETH_P_WCCP
+       __ADD(ETH_P_WCCP,wccp)
+#endif
+       __ADD(ETH_P_PPP_DISC,ppp_disc)
+       __ADD(ETH_P_PPP_SES,ppp_ses)
+       __ADD(ETH_P_MPLS_UC,mpls_uc)
+       __ADD(ETH_P_MPLS_MC,mpls_mc)
+       __ADD(ETH_P_ATMMPOA,atmmpoa)
+       __ADD(ETH_P_ATMFATE,atmfate)
+       __ADD(ETH_P_EDP2,edp2)
+       __ADD(ETH_P_802_3,802.3)
+       __ADD(ETH_P_AX25,ax25)
+       __ADD(ETH_P_ALL,all)
+       __ADD(ETH_P_802_2,802.2)
+       __ADD(ETH_P_SNAP,snap)
+       __ADD(ETH_P_DDCMP,ddcmp)
+       __ADD(ETH_P_WAN_PPP,wan_ppp)
+       __ADD(ETH_P_PPP_MP,ppp_mp)
+       __ADD(ETH_P_LOCALTALK,localtalk)
+       __ADD(ETH_P_PPPTALK,ppptalk)
+       __ADD(ETH_P_TR_802_2,tr_802.2)
+       __ADD(ETH_P_MOBITEX,mobitex)
+       __ADD(ETH_P_CONTROL,control)
+       __ADD(ETH_P_IRDA,irda)
+       __ADD(ETH_P_ECONET,econet)
+       __ADD(ETH_P_HDLC,hdlc)
+};
+
+/**
+ * Convert a ethernet protocol to a character string (Reentrant).
+ * @arg eproto         ethernet protocol
+ * @arg buf            destination buffer
+ * @arg len            buffer length
+ *
+ * Converts a ethernet protocol to a character string and stores
+ * it in the specified destination buffer.
+ *
+ * @return The destination buffer or the type encoded in hexidecimal
+ *         form if no match was found.
+ */
+char *nl_ether_proto2str(int eproto, char *buf, size_t len)
+{
+       return __type2str(eproto, buf, len, ether_protos,
+                           ARRAY_SIZE(ether_protos));
+}
+
+/**
+ * Convert a character string to a ethernet protocol
+ * @arg name           name of ethernet protocol
+ *
+ * Converts the provided character string specifying a ethernet
+ * protocl to the corresponding numeric value.
+ *
+ * @return ethernet protocol or a negative value if none was found.
+ */
+int nl_str2ether_proto(const char *name)
+{
+       return __str2type(name, ether_protos, ARRAY_SIZE(ether_protos));
+}
+
+/** @} */
+
+/** @} */
diff --git a/libnl-1.pc b/libnl-1.pc
new file mode 100644 (file)
index 0000000..edd2abe
--- /dev/null
@@ -0,0 +1,10 @@
+prefix=/usr/local
+exec_prefix=/usr/local
+libdir=${exec_prefix}/lib
+includedir=/usr/local/include
+
+Name: libnl
+Description: Convenience library for netlink sockets
+Version: 1.0-pre6
+Libs: -L${libdir} -lnl
+Cflags:
diff --git a/libnl-1.pc.in b/libnl-1.pc.in
new file mode 100644 (file)
index 0000000..7ac8413
--- /dev/null
@@ -0,0 +1,10 @@
+prefix=@prefix@
+exec_prefix=@prefix@
+libdir=@libdir@
+includedir=@prefix@/include
+
+Name: libnl
+Description: Convenience library for netlink sockets
+Version: @PACKAGE_VERSION@
+Libs: -L${libdir} -lnl
+Cflags:
diff --git a/libnl.spec b/libnl.spec
new file mode 100644 (file)
index 0000000..c4632c2
--- /dev/null
@@ -0,0 +1,138 @@
+%define preversion pre6
+
+Summary: Convenience library for kernel netlink sockets
+Group: Development/Libraries
+License: LGPL
+Name: libnl
+Version: 1.0
+Release: 0.11.%{preversion}
+URL: http://people.suug.ch/~tgr/libnl/
+Source: http://people.suug.ch/~tgr/libnl/files/libnl-%{version}-%{preversion}.tar.gz
+BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root
+BuildRequires: doxygen
+#Patch3: libnl-1.0-pre6-static.patch
+#Patch4: libnl-1.0-pre5-__u64_x86_64.patch
+#Patch5: libnl-1.0-pre6-debuginfo.patch
+
+%description
+This package contains a convenience library to simplify
+using the Linux kernel's netlink sockets interface for
+network manipulation
+
+%package devel
+Summary: Libraries and headers for using libnl
+Group: Development/Libraries
+Requires: %{name} = %{version}-%{release}
+
+%description devel
+This package contains various headers for using libnl
+
+
+%prep
+%setup -q -n libnl-%{version}-%{preversion}
+#%patch3 -p1
+#%patch4 -p1
+#%patch5 -p1
+
+%build
+%configure
+make
+
+%install
+%{__rm} -rf $RPM_BUILD_ROOT
+make install DESTDIR=$RPM_BUILD_ROOT
+
+%{__mkdir_p} $RPM_BUILD_ROOT%{_libdir}/pkgconfig/
+cat >libnl-1.pc <<EOF
+prefix=%{_prefix}
+exec_prefix=%{_prefix}
+libdir=%{_libdir}
+includedir=%{_includedir}
+
+Name: libnl
+Description: Convenience library for netlink sockets
+Version: %{version}-%{release}
+Libs: -L%{_libdir} -lnl
+Cflags:
+EOF
+
+%{__install} -m 644 libnl-1.pc $RPM_BUILD_ROOT%{_libdir}/pkgconfig/
+
+%clean
+%{__rm} -rf $RPM_BUILD_ROOT
+
+%post -p /sbin/ldconfig
+%postun -p /sbin/ldconfig
+
+%files
+%defattr(-,root,root)
+%{_libdir}/%{name}.so.*
+%doc COPYING
+
+%files devel
+%defattr(-,root,root,0755)
+%{_includedir}/netlink/
+%doc doc/*
+%{_libdir}/%{name}.so
+%{_libdir}/%{name}.a
+%{_libdir}/pkgconfig/%{name}-1.pc
+
+%changelog
+* Thu Jul 26 2007 Daniel Hokka Zakrisson <daniel@hozac.com> - 1.0-0.11.pre6
+- Upgrade to pre6
+
+* Mon Aug 14 2006 Peter Jones <pjones@redhat.com> - 1.0-0.10.pre5.4
+- Fix nl_recv() for ppc64
+
+* Mon Jul 31 2006 Jeremy Katz <katzj@redhat.com> - 1.0-0.10.pre5.3
+- unbreak the pkgconfig file
+
+* Wed Jul 26 2006 Matthias Clasen <mclasen@redhat.com> - 1.0-0.10.pre5.2
+- Fix the pkgconfig file on 64-bit systems (#197176)
+
+* Wed Jul 12 2006 Jesse Keating <jkeating@redhat.com> - 1.0-0.10.pre5.1
+- rebuild
+
+* Thu Jun 15 2006 Peter Jones <pjones@redhat.com> 1.0-0.10.pre5
+- Fix debuginfo generation.
+
+* Fri May 26 2006 Jason Vas Dias <jvdias@redhat.com> 1.0-0.9.pre5
+- Allow build to succeed with new gcc / glibc-kernheaders
+  (compile failed on __u64 redefinition on x86_64).
+- Add a static %{_libdir}/libnl.a library to libnl-devel for
+  programs that might need to do a static link to libnl.
+  Added after consultation with Christopher Aillon.
+
+* Tue Feb 12 2006 Christopher Aillon <caillon@redhat.com> 1.0-0.8.pre5
+- Rebuild
+
+* Tue Feb 07 2006 Jesse Keating <jkeating@redhat.com> 1.0-0.7.pre5.1
+- rebuilt for new gcc4.1 snapshot and glibc changes
+
+* Mon Jan 16 2006 Christopher Aillon <caillon@redhat.com> 1.0-0.7.pre5
+- Add patch to not chown files to root.root during make install; it
+  happens normally.
+
+* Mon Jan  9 2006 Christopher Aillon <caillon@redhat.com> 1.0-0.6.pre5
+- Correctly install the pkgconfig file
+
+* Fri Dec 09 2005 Jesse Keating <jkeating@redhat.com>
+- rebuilt
+
+* Wed Nov  2 2005 Christopher Aillon <caillon@redhat.com> 1.0-0.5.pre5
+- Fix 64bit LIBDIR issue; use $LIBDIR instead of $PREFIX/lib
+
+* Wed Nov  2 2005 Christopher Aillon <caillon@redhat.com> 1.0-0.4.pre5
+- Update to 1.0-pre5
+
+* Tue Nov  1 2005 Christopher Aillon <caillon@redhat.com> 1.0-0.3.pre4
+- Update to 1.0-pre4
+
+* Tue Nov  1 2005 Christopher Aillon <caillon@redhat.com> 1.0-0.2.pre3
+- Minor specfile cleanup
+
+* Thu Oct 27 2005 Dan Williams <dcbw@redhat.com> 1.0-0.1.pre3
+- Split into main and devel packages
+
+* Thu Oct 27 2005 Dan Williams <dcbw@redhat.com> 1.0-0.0.pre3
+- initial build
diff --git a/src/Makefile b/src/Makefile
new file mode 100644 (file)
index 0000000..795c3ce
--- /dev/null
@@ -0,0 +1,33 @@
+#
+# src/Makefile
+#
+#      This library is free software; you can redistribute it and/or
+#      modify it under the terms of the GNU Lesser General Public
+#      License as published by the Free Software Foundation version 2.1
+#      of the License.
+#
+# Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+#
+
+ifeq ($(shell [ ! -r ../Makefile.opts ] && echo 1),)
+    include ../Makefile.opts
+endif
+
+CFLAGS += -L../lib
+CIN    := $(wildcard nl-*.c)
+TOOLS  := $(CIN:%.c=%)
+
+all: $(TOOLS)
+
+$(TOOLS): utils.o
+
+nl-%: nl-%.c
+       $(CC) $(CFLAGS) -o $@ $< -lnl utils.o
+
+clean:
+       rm -f $(TOOLS) utils.o
+
+distclean: clean
+
+install:
+       @true
diff --git a/src/f_addr.c b/src/f_addr.c
new file mode 100644 (file)
index 0000000..491d88f
--- /dev/null
@@ -0,0 +1,107 @@
+/*
+ * src/f_addr.c        Address Filter
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+static void get_filter(struct rtnl_addr *addr, int argc, char **argv, int idx,
+                      struct nl_cache *link_cache)
+{
+       struct nl_addr *a;
+
+       while (argc > idx) {
+               if (arg_match("dev")) {
+                       if (argc > ++idx) {
+                               int ifindex = rtnl_link_name2i(link_cache, argv[idx++]);
+                               if (ifindex == RTNL_LINK_NOT_FOUND)
+                                       goto err_notfound;
+                               rtnl_addr_set_ifindex(addr, ifindex);
+                       }
+               } else if (arg_match("family")) {
+                       if (argc > ++idx) {
+                               int family = nl_str2af(argv[idx++]);
+                               if (family == AF_UNSPEC)
+                                       goto err_invaf;
+                               rtnl_addr_set_family(addr, family);
+                       }
+               } else if (arg_match("label")) {
+                       if (argc > ++idx)
+                               rtnl_addr_set_label(addr, argv[idx++]);
+               } else if (arg_match("scope")) {
+                       if (argc > ++idx) {
+                               int scope = rtnl_str2scope(argv[idx++]);
+                               if (scope < 0)
+                                       goto err_invscope;
+                               rtnl_addr_set_scope(addr, scope);
+                       }
+               } else if (arg_match("local")) {
+                       if (argc > ++idx) {
+                               a = nl_addr_parse(argv[idx++],
+                                                 rtnl_addr_get_family(addr));
+                               if (!a)
+                                       goto err_invaddr;
+                               rtnl_addr_set_local(addr, a);
+                               nl_addr_put(a);
+                       }
+               } else if (arg_match("peer")) {
+                       if (argc > ++idx) {
+                               a = nl_addr_parse(argv[idx++],
+                                                 rtnl_addr_get_family(addr));
+                               if (!a)
+                                       goto err_invaddr;
+                               rtnl_addr_set_peer(addr, a);
+                               nl_addr_put(a);
+                       }
+               } else if (arg_match("broadcast")) {
+                       if (argc > ++idx) {
+                               a = nl_addr_parse(argv[idx++],
+                                                 rtnl_addr_get_family(addr));
+                               if (!a)
+                                       goto err_invaddr;
+                               rtnl_addr_set_broadcast(addr, a);
+                               nl_addr_put(a);
+                       }
+               } else if (arg_match("multicast")) {
+                       if (argc > ++idx) {
+                               a = nl_addr_parse(argv[idx++],
+                                                 rtnl_addr_get_family(addr));
+                               if (!a)
+                                       goto err_invaddr;
+                               rtnl_addr_set_multicast(addr, a);
+                               nl_addr_put(a);
+                       }
+               } else if (arg_match("anycast")) {
+                       if (argc > ++idx) {
+                               a = nl_addr_parse(argv[idx++],
+                                                 rtnl_addr_get_family(addr));
+                               if (!a)
+                                       goto err_invaddr;
+                               rtnl_addr_set_anycast(addr, a);
+                               nl_addr_put(a);
+                       }
+               } else {
+                       fprintf(stderr, "What is '%s'?\n", argv[idx]);
+                       exit(1);
+               }
+       }
+
+       return;
+
+err_notfound:
+       fprintf(stderr, "Unknown link %s\n", argv[idx-1]);
+       exit(1);
+err_invscope:
+       fprintf(stderr, "Invalid scope name \"%s\".\n", argv[idx-1]);
+       exit(1);
+err_invaf:
+       fprintf(stderr, "Invalid address family \"%s\"\n", argv[idx-1]);
+       exit(1);
+err_invaddr:
+       fprintf(stderr, "Invalid address \"%s\": %s\n", argv[idx-1], nl_geterror());
+       exit(1);
+}
diff --git a/src/f_link.c b/src/f_link.c
new file mode 100644 (file)
index 0000000..3c1cb93
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+ * src/f_link.c                Link Filter
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include <linux/if.h>
+
+static void get_filter(struct rtnl_link *l, int ac, char **av, int idx,
+                      struct nl_cache *cache)
+{
+       while (ac > idx) {
+               if (!strcasecmp(av[idx], "dev")) {
+                       if (ac > ++idx) {
+                               int ifindex = rtnl_link_name2i(cache, av[idx++]);
+                               if (ifindex == RTNL_LINK_NOT_FOUND)
+                                       goto err_notfound;
+                               rtnl_link_set_ifindex(l, ifindex);
+                       }
+               } else if (!strcasecmp(av[idx], "mtu")) {
+                       if (ac > ++idx)
+                               rtnl_link_set_mtu(l, strtoul(av[idx++], NULL, 0));
+               } else if (!strcasecmp(av[idx], "txqlen")) {
+                       if (ac > ++idx)
+                               rtnl_link_set_txqlen(l, strtoul(av[idx++], NULL, 0));
+               } else if (!strcasecmp(av[idx], "weight")) {
+                       if (ac > ++idx)
+                               rtnl_link_set_weight(l, strtoul(av[idx++], NULL, 0));
+               } else if (!strcasecmp(av[idx], "link")) {
+                       if (ac > ++idx) {
+                               int ifindex = rtnl_link_name2i(cache, av[idx++]);
+                               if (ifindex == RTNL_LINK_NOT_FOUND)
+                                       goto err_notfound;
+                               rtnl_link_set_link(l, ifindex);
+                       }
+               } else if (!strcasecmp(av[idx], "master")) {
+                       if (ac > ++idx) {
+                               int ifindex = rtnl_link_name2i(cache, av[idx++]);
+                               if (ifindex == RTNL_LINK_NOT_FOUND)
+                                       goto err_notfound;
+                               rtnl_link_set_master(l, ifindex);
+                       }
+               } else if (!strcasecmp(av[idx], "qdisc")) {
+                       if (ac > ++idx)
+                               rtnl_link_set_qdisc(l, av[idx++]);
+               } else if (!strcasecmp(av[idx], "name")) {
+                       if (ac > ++idx)
+                               rtnl_link_set_name(l, av[idx++]);
+               } else if (!strcasecmp(av[idx], "addr")) {
+                       if (ac > ++idx) {
+                               struct nl_addr *a = nl_addr_parse(av[idx++], AF_UNSPEC);
+                               if (a == NULL)
+                                       goto err;
+                               rtnl_link_set_addr(l, a);
+                               nl_addr_put(a);
+                       }
+               } else if (!strcasecmp(av[idx], "broadcast")) {
+                       if (ac > ++idx) {
+                               struct nl_addr *a = nl_addr_parse(av[idx++], AF_UNSPEC);
+                               if (a == NULL)
+                                       goto err;
+                               rtnl_link_set_broadcast(l, a);
+                               nl_addr_put(a);
+                       }
+               }
+#define MFLAG(STR, FLAG) \
+       else if (!strcasecmp(av[idx], STR)) { \
+               rtnl_link_set_flags(l, FLAG); idx++; }
+#define MNOFLAG(STR, FLAG) \
+       else if (!strcasecmp(av[idx], STR)) { \
+               rtnl_link_unset_flags(l, FLAG); idx++; }
+
+               MFLAG("up", IFF_UP)
+               MNOFLAG("down", IFF_UP)
+               MFLAG("noarp", IFF_NOARP)
+               MNOFLAG("arp", IFF_NOARP)
+               MFLAG("promisc", IFF_PROMISC)
+               MNOFLAG("nopromisc", IFF_PROMISC)
+               MFLAG("dynamic", IFF_DYNAMIC)
+               MNOFLAG("nodynamic", IFF_DYNAMIC)
+               MFLAG("multicast", IFF_MULTICAST)
+               MNOFLAG("nomulticast", IFF_MULTICAST)
+               MFLAG("allmulticast", IFF_ALLMULTI)
+               MNOFLAG("noallmulticast", IFF_ALLMULTI)
+#undef MFLAG
+#undef MNOFLAG
+               else {
+                       fprintf(stderr, "What is '%s'?\n", av[idx]);
+                       exit(1);
+               }
+       }
+
+       return;
+
+err_notfound:
+       fprintf(stderr, "Unknown link %s\n", av[idx-1]);
+       exit(1);
+err:
+       fprintf(stderr, "%s\n", nl_geterror());
+       exit(1);
+}
diff --git a/src/f_neigh.c b/src/f_neigh.c
new file mode 100644 (file)
index 0000000..ac9355c
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * src/f_neigh.c       Neighbour Filter
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+static void get_filter(struct rtnl_neigh *n, int ac, char **av, int idx,
+                      struct nl_cache *cache)
+{
+       struct nl_cache *lc = nl_cache_mngt_require("route/link");
+       
+       while (ac > idx) {
+               if (!strcasecmp(av[idx], "dev")) {
+                       if (ac > ++idx) {
+                               int ifindex = rtnl_link_name2i(lc, av[idx++]);
+                               if (ifindex == RTNL_LINK_NOT_FOUND)
+                                       goto err_notfound;
+                               rtnl_neigh_set_ifindex(n, ifindex);
+                       }
+               } else if (!strcasecmp(av[idx], "dst")) {
+                       if (ac > ++idx) {
+                               struct nl_addr *a = nl_addr_parse(av[idx++], AF_UNSPEC);
+                               if (a == NULL)
+                                       goto err;
+                               rtnl_neigh_set_dst(n, a);
+                               nl_addr_put(a);
+                       }
+               } else if (!strcasecmp(av[idx], "lladdr")) {
+                       if (ac > ++idx) {
+                               struct nl_addr *a = nl_addr_parse(av[idx++], AF_UNSPEC);
+                               if (a == NULL)
+                                       goto err;
+                               rtnl_neigh_set_lladdr(n, a);
+                               nl_addr_put(a);
+                       }
+               }
+       }
+
+       return;
+err_notfound:
+       fprintf(stderr, "Unable to find interface %s\n", av[idx-1]);
+       exit(1);
+err:
+       fprintf(stderr, "%s\n", nl_geterror());
+       exit(1);
+}
diff --git a/src/f_route.c b/src/f_route.c
new file mode 100644 (file)
index 0000000..5e6e619
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * src/f_route.c       Routes Filter
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+static void get_filter(struct rtnl_route *r, int ac, char **av, int idx,
+                      struct nl_cache *cache, struct nl_cache *link_cache)
+{
+       while (ac > idx) {
+               if (!strcasecmp(av[idx], "src")) {
+                       if (ac > ++idx) {
+                               struct nl_addr *a = nl_addr_parse(av[idx++], AF_UNSPEC);
+                               if (!a)
+                                       goto err;
+                               rtnl_route_set_pref_src(r, a);
+                               nl_addr_put(a);
+                       }
+               } else if (!strcasecmp(av[idx], "via")) {
+                       if (ac > ++idx) {
+                               struct nl_addr *a = nl_addr_parse(av[idx++], AF_UNSPEC);
+                               if (!a)
+                                       goto err;
+                               rtnl_route_set_gateway(r, a);
+                               nl_addr_put(a);
+                       }
+               } else if (!strcasecmp(av[idx], "from")) {
+                       if (ac > ++idx) {
+                               struct nl_addr *a = nl_addr_parse(av[idx++], AF_UNSPEC);
+                               if (!a)
+                                       goto err;
+                               rtnl_route_set_src(r, a);
+                               nl_addr_put(a);
+                       }
+               } else if (!strcasecmp(av[idx], "tos")) {
+                       if (ac > ++idx)
+                               rtnl_route_set_tos(r, strtoul(av[idx++], NULL, 0));
+               } else if (!strcasecmp(av[idx], "prio")) {
+                       if (ac > ++idx)
+                               rtnl_route_set_prio(r, strtoul(av[idx++], NULL, 0));
+               } else if (!strcasecmp(av[idx], "scope")) {
+                       if (ac > ++idx)
+                               rtnl_route_set_prio(r, rtnl_str2scope(av[idx++]));
+               } else if (!strcasecmp(av[idx], "dev")) {
+                       if (ac > ++idx) {
+                               int ifindex = rtnl_link_name2i(link_cache, av[idx++]);
+                               if (ifindex == RTNL_LINK_NOT_FOUND)
+                                       goto err_notfound;
+                               rtnl_route_set_oif(r, ifindex);
+                       }
+               } else if (!strcasecmp(av[idx], "table")) {
+                       if (ac > ++idx)
+                               rtnl_route_set_table(r, strtoul(av[idx++], NULL, 0));
+               } else {
+                       fprintf(stderr, "What is '%s'?\n", av[idx]);
+                       exit(1);
+               }
+       }
+
+       return;
+
+err_notfound:
+       fprintf(stderr, "Unable to find device \"%s\"\n", av[idx-1]);
+       exit(1);
+err:
+       fprintf(stderr, "%s\n", nl_geterror());
+       exit(1);
+}
diff --git a/src/nl-addr-add.c b/src/nl-addr-add.c
new file mode 100644 (file)
index 0000000..fc07611
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * src/nl-addr-add.c     Add addresses
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include "utils.h"
+
+int main(int argc, char *argv[])
+{
+       struct nl_handle *nlh;
+       struct rtnl_addr *addr;
+       struct nl_addr *local;
+       int err = 1;
+
+       if (argc < 3 || !strcmp(argv[1], "-h")) {
+               printf("Usage: nl-addr-add <addr> <ifindex>\n");
+               goto errout;
+       }
+
+       if (nltool_init(argc, argv) < 0)
+               goto errout;
+
+       nlh = nl_handle_alloc_nondefault(nltool_cbset);
+       if (!nlh)
+               goto errout;
+
+       addr = rtnl_addr_alloc();
+       if (!addr)
+               goto errout_free_handle;
+
+       if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
+               goto errout_free_addr;
+
+       local = nltool_addr_parse(argv[1]);
+       if (!local)
+               goto errout_close;
+
+       if (rtnl_addr_set_local(addr, local) < 0) {
+               fprintf(stderr, "Unable to set local address: %s\n",
+                       nl_geterror());
+               goto errout_put_addr;
+       }
+
+       rtnl_addr_set_ifindex(addr, strtoul(argv[2], NULL, 0));
+
+       if (rtnl_addr_add(nlh, addr, 0) < 0) {
+               fprintf(stderr, "Unable to add address: %s\n", nl_geterror());
+               goto errout_close;
+       }
+
+       err = 0;
+errout_put_addr:
+       nl_addr_put(local);
+errout_close:
+       nl_close(nlh);
+errout_free_addr:
+       rtnl_addr_put(addr);
+errout_free_handle:
+       nl_handle_destroy(nlh);
+errout:
+       return err;
+}
diff --git a/src/nl-addr-delete.c b/src/nl-addr-delete.c
new file mode 100644 (file)
index 0000000..8518268
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * src/nl-addr-delete.c     Delete addresses
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include "utils.h"
+
+int main(int argc, char *argv[])
+{
+       struct nl_handle *nlh;
+       struct rtnl_addr *addr;
+       struct nl_addr *local;
+       int err = 1;
+
+       if (argc < 3 || !strcmp(argv[1], "-h")) {
+               printf("Usage: nl-addr-delete <addr> <ifindex>\n");
+               goto errout;
+       }
+
+       if (nltool_init(argc, argv) < 0)
+               goto errout;
+
+       nlh = nl_handle_alloc_nondefault(nltool_cbset);
+       if (!nlh)
+               goto errout;
+
+       addr = rtnl_addr_alloc();
+       if (!addr)
+               goto errout_free_handle;
+
+       if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
+               goto errout_free_addr;
+
+       local = nltool_addr_parse(argv[1]);
+       if (!local)
+               goto errout_close;
+
+       if (rtnl_addr_set_local(addr, local) < 0) {
+               fprintf(stderr, "Unable to set local address: %s\n",
+                       nl_geterror());
+               goto errout_addr_put;
+       }
+
+       rtnl_addr_set_ifindex(addr, strtoul(argv[2], NULL, 0));
+
+       if (rtnl_addr_delete(nlh, addr, 0) < 0) {
+               fprintf(stderr, "Unable to delete address: %s\n",
+                       nl_geterror());
+               goto errout_addr_put;
+       }
+
+       err = 0;
+
+errout_addr_put:
+       nl_addr_put(local);
+errout_close:
+       nl_close(nlh);
+errout_free_addr:
+       rtnl_addr_put(addr);
+errout_free_handle:
+       nl_handle_destroy(nlh);
+errout:
+       return err;
+}
diff --git a/src/nl-addr-dump.c b/src/nl-addr-dump.c
new file mode 100644 (file)
index 0000000..45a2ab8
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * src/nl-addr-dump.c     Dump address attributes
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include "utils.h"
+
+static void print_usage(void)
+{
+       printf(
+       "Usage: nl-addr-dump <mode> [<filter>]\n"
+       "  mode := { brief | detailed | stats | xml }\n"
+       "  filter := [dev DEV] [label LABEL] [family FAMILY] [scope SCOPE]\n"
+       "            [local ADDR] [peer ADDR] [broadcast ADDR] [anycast ADDR]\n"
+       "            [multicast ADDR]\n");
+       exit(1);
+}
+
+#include "f_addr.c"
+
+int main(int argc, char *argv[])
+{
+       struct nl_handle *nlh;
+       struct nl_cache *link_cache, *addr_cache;
+       struct rtnl_addr *addr;
+       struct nl_dump_params params = {
+               .dp_fd = stdout,
+               .dp_type = NL_DUMP_BRIEF
+       };
+       int err = 1;
+
+       if (nltool_init(argc, argv) < 0)
+               return -1;
+
+       nlh = nl_handle_alloc_nondefault(nltool_cbset);
+       if (!nlh)
+               return -1;
+
+       addr = rtnl_addr_alloc();
+       if (!addr)
+               goto errout;
+
+       if (argc < 2 || !strcmp(argv[1], "-h"))
+               print_usage();
+
+       if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
+               goto errout_free;
+
+       link_cache = nltool_alloc_link_cache(nlh);
+       if (!link_cache)
+               goto errout_close;
+
+       addr_cache = nltool_alloc_addr_cache(nlh);
+       if (!addr_cache)
+               goto errout_link_cache;
+
+       params.dp_type = nltool_parse_dumptype(argv[1]);
+       if (params.dp_type < 0)
+               goto errout_addr_cache;
+       get_filter(addr, argc, argv, 2, link_cache);
+
+       nl_cache_dump_filter(addr_cache, &params, (struct nl_object *) addr);
+
+       err = 0;
+
+errout_addr_cache:
+       nl_cache_free(addr_cache);
+errout_link_cache:
+       nl_cache_free(link_cache);
+errout_close:
+       nl_close(nlh);
+errout_free:
+       rtnl_addr_put(addr);
+errout:
+       return err;
+}
diff --git a/src/nl-fib-lookup.c b/src/nl-fib-lookup.c
new file mode 100644 (file)
index 0000000..69e9923
--- /dev/null
@@ -0,0 +1,130 @@
+/*
+ * src/nl-fib-lookup.c         FIB Route Lookup
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include "utils.h"
+
+static void print_usage(void)
+{
+       printf(
+       "Usage: nl-fib-lookup [options] <addr>\n"
+       "Options:\n"
+       "   -t, --table <table>         Table id\n"
+       "   -f, --fwmark <int>          Firewall mark\n"
+       "   -s, --scope <scope>         Routing scope\n"
+       "   -T, --tos <int>             Type of Service\n");
+       exit(1);
+}
+
+int main(int argc, char *argv[])
+{
+       struct nl_handle *nlh;
+       struct nl_cache *result;
+       struct flnl_request *request;
+       struct nl_addr *addr;
+       struct nl_dump_params params = {
+               .dp_fd = stdout,
+               .dp_type = NL_DUMP_FULL,
+       };
+       int table = RT_TABLE_UNSPEC, scope = RT_SCOPE_UNIVERSE;
+       int tos = 0, err = 1;
+       uint64_t fwmark = 0;
+
+       if (nltool_init(argc, argv) < 0)
+               return -1;
+
+       while (1) {
+               static struct option long_opts[] = {
+                       {"table", 1, 0, 't'},
+                       {"fwmark", 1, 0, 'f'},
+                       {"scope", 1, 0, 's'},
+                       {"tos", 1, 0, 'T'},
+                       {"help", 0, 0, 'h'},
+                       {0, 0, 0, 0},
+               };
+               int c, idx = 0;
+
+               c = getopt_long(argc, argv, "t:f:s:T:h", long_opts, &idx);
+               if (c == -1)
+                       break;
+
+               switch (c) {
+               case 't':
+                       table = strtoul(optarg, NULL, 0);
+                       break;
+               case 'f':
+                       fwmark = strtoul(optarg, NULL, 0);
+                       break;
+               case 's':
+                       scope = strtoul(optarg, NULL, 0);
+                       break;
+               case 'T':
+                       tos = strtoul(optarg, NULL, 0);
+                       break;
+               default:
+                       print_usage();
+               }
+       }
+
+       if (optind >= argc)
+               print_usage();
+
+       nlh = nl_handle_alloc_nondefault(nltool_cbset);
+       if (!nlh)
+               return -1;
+
+       addr = nl_addr_parse(argv[optind], AF_INET);
+       if (!addr) {
+               fprintf(stderr, "Unable to parse address \"%s\": %s\n",
+                       argv[optind], nl_geterror());
+               goto errout;
+       }
+
+       result = flnl_result_alloc_cache();
+       if (!result)
+               goto errout_addr;
+
+       request = flnl_request_alloc();
+       if (!request)
+               goto errout_result;
+
+       flnl_request_set_table(request, table);
+       flnl_request_set_fwmark(request, fwmark);
+       flnl_request_set_scope(request, scope);
+       flnl_request_set_tos(request, tos);
+
+       err = flnl_request_set_addr(request, addr);
+       nl_addr_put(addr);
+       if (err < 0)
+               goto errout_put;
+
+       if (nltool_connect(nlh, NETLINK_FIB_LOOKUP) < 0)
+               goto errout_put;
+
+       err = flnl_lookup(nlh, request, result);
+       if (err < 0) {
+               fprintf(stderr, "Unable to lookup: %s\n", nl_geterror());
+               goto errout_put;
+       }
+
+       nl_cache_dump(result, &params);
+
+       err = 0;
+errout_put:
+       flnl_request_put(request);
+errout_result:
+       nl_cache_free(result);
+errout_addr:
+       nl_addr_put(addr);
+errout:
+       nl_close(nlh);
+       nl_handle_destroy(nlh);
+       return err;
+}
diff --git a/src/nl-link-dump.c b/src/nl-link-dump.c
new file mode 100644 (file)
index 0000000..51be032
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * src/nl-link-dump.c  Dump link attributes
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include "utils.h"
+
+static void print_usage(void)
+{
+       printf(
+       "Usage: nl-link-dump <mode> [<filter>]\n"
+       "  mode := { brief | detailed | stats | xml }\n"
+       "  filter := [dev DEV] [mtu MTU] [txqlen TXQLEN] [weight WEIGHT] [link LINK]\n"
+       "            [master MASTER] [qdisc QDISC] [addr ADDR] [broadcast BRD]\n"
+       "            [{ up | down }] [{ arp | noarp }] [{ promisc | nopromisc }]\n"
+       "            [{ dynamic | nodynamic }] [{ multicast | nomulticast }]\n"
+       "            [{ trailers | notrailers }] [{ allmulticast | noallmulticast }]\n");
+       exit(1);
+}
+
+#include "f_link.c"
+
+int main(int argc, char *argv[])
+{
+       struct nl_handle *nlh;
+       struct nl_cache *link_cache;
+       struct rtnl_link *link;
+       struct nl_dump_params params = {
+               .dp_fd = stdout,
+               .dp_type = NL_DUMP_BRIEF
+       };
+       int err = 1;
+
+       if (nltool_init(argc, argv) < 0)
+               return -1;
+
+       if (argc < 2 || !strcmp(argv[1], "-h"))
+               print_usage();
+
+       nlh = nl_handle_alloc_nondefault(nltool_cbset);
+       if (!nlh)
+               return -1;
+
+       link = rtnl_link_alloc();
+       if (!link)
+               goto errout;
+
+       if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
+               goto errout_put;
+
+       link_cache = nltool_alloc_link_cache(nlh);
+       if (!link_cache)
+               goto errout_put;
+
+       params.dp_type = nltool_parse_dumptype(argv[1]);
+       if (params.dp_type < 0)
+               goto errout_put;
+
+       get_filter(link, argc, argv, 2, link_cache);
+       nl_cache_dump_filter(link_cache, &params, (struct nl_object *) link);
+       nl_cache_free(link_cache);
+       err = 0;
+errout_put:
+       rtnl_link_put(link);
+errout:
+       nl_close(nlh);
+       nl_handle_destroy(nlh);
+       return err;
+}
diff --git a/src/nl-link-ifindex2name.c b/src/nl-link-ifindex2name.c
new file mode 100644 (file)
index 0000000..1f28d66
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * src/nl-link-ifindex2name.c     Transform a interface index to its name
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include "utils.h"
+
+int main(int argc, char **argv)
+{
+       struct nl_handle *nlh;
+       struct nl_cache *link_cache;
+       int err = -1, ifindex;
+       char dst[32] = {0};
+       const char *name;
+
+       if (nltool_init(argc, argv) < 0)
+               return -1;
+
+       if (argc < 2 || !strcmp(argv[1], "-h")) {
+               fprintf(stderr, "Usage: nl-link-ifindex2name <ifindex>\n");
+               return -1;
+       }
+
+       nlh = nl_handle_alloc_nondefault(nltool_cbset);
+       if (!nlh)
+               return -1;
+
+       if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
+               goto errout;
+
+       link_cache = nltool_alloc_link_cache(nlh);
+       if (!link_cache)
+               goto errout;
+
+       ifindex = strtoul(argv[1], NULL, 0);
+
+       if (!(name = rtnl_link_i2name(link_cache, ifindex, dst, sizeof(dst))))
+               fprintf(stderr, "Interface index %d does not exist\n", ifindex);
+       else
+               printf("%s\n", name);
+
+       nl_cache_free(link_cache);
+       err = 0;
+errout:
+       nl_close(nlh);
+       nl_handle_destroy(nlh);
+
+       return err;
+}
diff --git a/src/nl-link-name2ifindex.c b/src/nl-link-name2ifindex.c
new file mode 100644 (file)
index 0000000..6ed146f
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * src/nl-link-name2ifindex.c     Transform a interface name to its index
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include "utils.h"
+
+int main(int argc, char *argv[])
+{
+       struct nl_handle *nlh;
+       struct nl_cache *link_cache;
+       int err = -1, ifindex;
+
+       if (nltool_init(argc, argv) < 0)
+               return -1;
+
+       if (argc < 2 || !strcmp(argv[1], "-h")) {
+               printf("Usage: nl-link-name2ifindex <name>\n");
+               return -1;
+       }
+
+       nlh = nl_handle_alloc_nondefault(nltool_cbset);
+       if (!nlh)
+               return -1;
+
+       if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
+               goto errout;
+
+       link_cache = nltool_alloc_link_cache(nlh);
+       if (!link_cache)
+               goto errout;
+
+       if ((ifindex = rtnl_link_name2i(link_cache, argv[1])) == RTNL_LINK_NOT_FOUND)
+               fprintf(stderr, "Interface %s does not exist\n", argv[1]);
+       else
+               printf("%d\n", ifindex);
+
+       nl_cache_free(link_cache);
+       err = 0;
+errout:
+       nl_close(nlh);
+       nl_handle_destroy(nlh);
+
+       return err;
+}
diff --git a/src/nl-link-set.c b/src/nl-link-set.c
new file mode 100644 (file)
index 0000000..5bd4cde
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ * src/nl-link-set.c     Set link attributes
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include "utils.h"
+
+static void print_usage(void)
+{
+       printf(
+       "Usage: nl-link-set <ifindex> <changes>\n"
+       "  changes := [dev DEV] [mtu MTU] [txqlen TXQLEN] [weight WEIGHT] [link LINK]\n"
+       "             [master MASTER] [qdisc QDISC] [addr ADDR] [broadcast BRD]\n"
+       "             [{ up | down }] [{ arp | noarp }] [{ promisc | nopromisc }]\n"
+       "             [{ dynamic | nodynamic }] [{ multicast | nomulticast }]\n"
+       "             [{ trailers | notrailers }] [{ allmulticast | noallmulticast }]\n");
+       exit(1);
+}
+
+#include "f_link.c"
+
+int main(int argc, char *argv[])
+{
+       struct nl_handle *nlh;
+       struct nl_cache *link_cache;
+       struct rtnl_link *link, *orig;
+       int err = 1, ifindex;
+
+       if (nltool_init(argc, argv) < 0)
+               return -1;
+
+       if (argc < 2 || !strcmp(argv[1], "-h"))
+               print_usage();
+
+       nlh = nl_handle_alloc_nondefault(nltool_cbset);
+       if (!nlh)
+               return -1;
+
+       link = rtnl_link_alloc();
+       if (!link)
+               goto errout;
+
+       if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
+               goto errout_free;
+
+       link_cache = nltool_alloc_link_cache(nlh);
+       if (!link_cache)
+               goto errout_close;
+
+       ifindex = strtoul(argv[1], NULL, 0);
+
+       if (!(orig = rtnl_link_get(link_cache, ifindex))) {
+               fprintf(stderr, "Interface index %d does not exist\n", ifindex);
+               goto errout_cache;
+       }
+
+       get_filter(link, argc, argv, 2, link_cache);
+
+       if (rtnl_link_change(nlh, orig, link, 0) < 0) {
+               fprintf(stderr, "Unable to change link: %s\n", nl_geterror());
+               goto errout_put;
+       }
+
+       err = 0;
+
+errout_put:
+       rtnl_link_put(orig);
+errout_cache:
+       nl_cache_free(link_cache);
+errout_close:
+       nl_close(nlh);
+errout_free:
+       rtnl_link_put(link);
+errout:
+       nl_handle_destroy(nlh);
+       return err;
+}
diff --git a/src/nl-link-stats.c b/src/nl-link-stats.c
new file mode 100644 (file)
index 0000000..9e20e21
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ * src/nl-link-stats.c     Retrieve link statistics
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include "utils.h"
+
+static void print_usage(void)
+{
+       printf(
+"Usage: nl-link-stats <ifindex> [<statistic> ...]\n"
+"  ifindex   := { all | number }\n"
+"  statistic := { (rx|tx)_packets | (rx|tx)_bytes | (rx|tx)_errors |\n"
+"                 (rx|tx)_dropped | (rx|tx)_compressed | (rx|tx)_fifo_err |\n" \
+"                 rx_len_err | rx_over_err | rx_crc_err | rx_frame_err |\n"
+"                 rx_missed_err | tx_abort_err | tx_carrier_err |\n"
+"                 tx_hbeat_err | tx_win_err | tx_collision | multicast }\n");
+       exit(1);
+}
+
+static char **gargv;
+static int gargc;
+
+static void dump_stat(struct rtnl_link *link, int id)
+{
+       uint64_t st = rtnl_link_get_stat(link, id);
+       char buf[62];
+
+       printf("%s.%s %" PRIu64 "\n", rtnl_link_get_name(link),
+              rtnl_link_stat2str(id, buf, sizeof(buf)), st);
+}
+
+static void dump_stats(struct nl_object *obj, void *arg)
+{
+       int i;
+       struct rtnl_link *link = (struct rtnl_link *) obj;
+
+       if (!strcasecmp(gargv[0], "all")) {
+               for (i = 0; i < RTNL_LINK_STATS_MAX; i++)
+                       dump_stat(link, i);
+       } else {
+               for (i = 0; i < gargc; i++) {
+                       int id = rtnl_link_str2stat(gargv[i]);
+
+                       if (id < 0)
+                               fprintf(stderr, "Warning: Unknown statistic "
+                                       "\"%s\"\n", gargv[i]);
+                       else
+                               dump_stat(link, id);
+               }
+       }
+}
+
+int main(int argc, char *argv[])
+{
+       struct nl_handle *nlh;
+       struct nl_cache *link_cache;
+       int err = 1;
+
+       if (nltool_init(argc, argv) < 0)
+               return -1;
+
+       if (argc < 3 || !strcmp(argv[1], "-h"))
+               print_usage();
+
+       nlh = nl_handle_alloc_nondefault(nltool_cbset);
+       if (!nlh)
+               return -1;
+
+       if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
+               goto errout;
+
+       link_cache = nltool_alloc_link_cache(nlh);
+       if (!link_cache)
+               goto errout_close;
+
+       gargv = &argv[2];
+       gargc = argc - 2;
+
+       if (!strcasecmp(argv[1], "all"))
+               nl_cache_foreach(link_cache, dump_stats, NULL);
+       else {
+               int ifindex = strtoul(argv[1], NULL, 0);
+               struct rtnl_link *link = rtnl_link_get(link_cache, ifindex);
+
+               if (!link) {
+                       fprintf(stderr, "Could not find ifindex %d\n", ifindex);
+                       goto errout_link_cache;
+               }
+
+               dump_stats((struct nl_object *) link, NULL);
+               rtnl_link_put(link);
+       }
+
+       err = 0;
+errout_link_cache:
+       nl_cache_free(link_cache);
+errout_close:
+       nl_close(nlh);
+errout:
+       nl_handle_destroy(nlh);
+       return err;
+}
diff --git a/src/nl-list-sockets.c b/src/nl-list-sockets.c
new file mode 100644 (file)
index 0000000..9a1333c
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * nl-list-sockets.c   Pretty-print /proc/net/netlink
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include "utils.h"
+
+#define PROC_NETLINK "/proc/net/netlink"
+
+static void print_usage(void)
+{
+       fprintf(stderr, "Usage: nl-list-sockets [<file>]\n");
+       exit(1);
+}
+
+int main(int argc, char *argv[])
+{
+       FILE *fd;
+       char buf[2048], p[64];
+
+       if (argc > 1 && !strcasecmp(argv[1], "-h"))
+               print_usage();
+
+       fd = fopen(PROC_NETLINK, "r");
+       if (fd == NULL) {
+               perror("fopen");
+               return -1;
+       }
+
+       printf("Address    Family           PID    Groups   rmem   wmem   " \
+              "CB         refcnt\n");
+
+       while (fgets(buf, sizeof(buf), fd)) {
+               unsigned long sk, cb;
+               int ret, proto, pid, rmem, wmem, refcnt;
+               uint32_t groups;
+               
+               ret = sscanf(buf, "%lx %d %d %08x %d %d %lx %d\n",
+                            &sk, &proto, &pid, &groups, &rmem, &wmem,
+                            &cb, &refcnt);
+               if (ret != 8)
+                       continue;
+               
+               printf("0x%08lx %-16s %-6d %08x %-6d %-6d 0x%08lx %d\n",
+                       sk, nl_nlfamily2str(proto, p, sizeof(p)), pid,
+                       groups, rmem, wmem, cb, refcnt);
+       }
+
+       fclose(fd);
+
+       return 0;
+}
diff --git a/src/nl-monitor.c b/src/nl-monitor.c
new file mode 100644 (file)
index 0000000..c08ed18
--- /dev/null
@@ -0,0 +1,139 @@
+/*
+ * src/nl-monitor.c     Monitor events
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include "utils.h"
+#include <netlink/route/link.h>
+#include <netlink/route/addr.h>
+
+static void obj_input(struct nl_object *obj, void *arg)
+{
+       struct nl_dump_params dp = {
+               .dp_type = NL_DUMP_STATS,
+               .dp_fd = stdout,
+               .dp_dump_msgtype = 1,
+       };
+
+       nl_object_dump(obj, &dp);
+}
+
+static int event_input(struct nl_msg *msg, void *arg)
+{
+       if (nl_msg_parse(msg, &obj_input, NULL) < 0)
+               fprintf(stderr, "<<EVENT>> Unknown message type\n");
+
+       /* Exit nl_recvmsgs_def() and return to the main select() */
+       return NL_EXIT;
+}
+
+int main(int argc, char *argv[])
+{
+       struct nl_handle *nlh;
+       struct nl_cache *link_cache;
+       int err = 1;
+       int i, idx;
+
+       static const struct {
+               enum rtnetlink_groups gr_id;
+               const char* gr_name;
+       } known_groups[] = {
+               { RTNLGRP_LINK, "link" },
+               { RTNLGRP_NOTIFY, "notify" },
+               { RTNLGRP_NEIGH, "neigh" },
+               { RTNLGRP_TC, "tc" },
+               { RTNLGRP_IPV4_IFADDR, "ipv4-ifaddr" },
+               { RTNLGRP_IPV4_MROUTE, "ipv4-mroute" },
+               { RTNLGRP_IPV4_ROUTE, "ipv4-route" },
+               { RTNLGRP_IPV6_IFADDR, "ipv6-ifaddr" },
+               { RTNLGRP_IPV6_MROUTE, "ipv6-mroute" },
+               { RTNLGRP_IPV6_ROUTE, "ipv6-route" },
+               { RTNLGRP_IPV6_IFINFO, "ipv6-ifinfo" },
+               { RTNLGRP_DECnet_IFADDR, "decnet-ifaddr" },
+               { RTNLGRP_DECnet_ROUTE, "decnet-route" },
+               { RTNLGRP_IPV6_PREFIX, "ipv6-prefix" },
+               { RTNLGRP_FIB_MAGIC, "fib-magic" },
+               { RTNLGRP_NONE, NULL }
+       };
+
+       if (nltool_init(argc, argv) < 0)
+               return -1;
+
+       nlh = nl_handle_alloc_nondefault(nltool_cbset);
+       if (nlh == NULL)
+               return -1;
+
+       nl_disable_sequence_check(nlh);
+
+       nl_cb_set(nl_handle_get_cb(nlh), NL_CB_VALID, NL_CB_CUSTOM,
+                 event_input, NULL);
+
+       if (argc > 1 && !strcasecmp(argv[1], "-h")) {
+               printf("Usage: nl-monitor [<groups>]\n");
+
+               printf("Known groups:");
+               for (i = 0; known_groups[i].gr_id != RTNLGRP_NONE; i++)
+                       printf(" %s", known_groups[i].gr_name);
+               printf("\n");
+               return 2;
+       }
+
+       nl_join_groups(nlh, RTMGRP_LINK);
+
+       if (nl_connect(nlh, NETLINK_ROUTE) < 0) {
+               fprintf(stderr, "%s\n", nl_geterror());
+               goto errout;
+       }
+
+       for (idx = 1; argc > idx; idx++) {
+               for (i = 0; known_groups[i].gr_id != RTNLGRP_NONE; i++) {
+                       if (!strcmp(argv[idx], known_groups[i].gr_name)) {
+
+                               if (nl_join_group(nlh, known_groups[i].gr_id) < 0) {
+                                       fprintf(stderr, "%s: %s\n", argv[idx], nl_geterror());
+                                       goto errout;
+                               }
+
+                               break;
+                       }
+               }
+               if (known_groups[i].gr_id == RTNLGRP_NONE)
+                       fprintf(stderr, "Warning: Unknown group: %s\n", argv[idx]);
+       }
+
+       if ((link_cache = rtnl_link_alloc_cache(nlh)) == NULL) {
+               fprintf(stderr, "%s\n", nl_geterror());
+               goto errout_close;
+       }
+
+       nl_cache_mngt_provide(link_cache);
+
+       while (1) {
+               fd_set rfds;
+               int fd, retval;
+
+               fd = nl_handle_get_fd(nlh);
+
+               FD_ZERO(&rfds);
+               FD_SET(fd, &rfds);
+               /* wait for an incoming message on the netlink socket */
+               retval = select(fd+1, &rfds, NULL, NULL, NULL);
+
+               if (retval) {
+                       /* FD_ISSET(fd, &rfds) will be true */
+                       nl_recvmsgs_def(nlh);
+               }
+       }
+
+       nl_cache_free(link_cache);
+errout_close:
+       nl_close(nlh);
+errout:
+       return err;
+}
diff --git a/src/nl-neigh-add.c b/src/nl-neigh-add.c
new file mode 100644 (file)
index 0000000..7bc854e
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * src/ nl-neigh-add.c     Add a neighbour
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include "utils.h"
+
+int main(int argc, char *argv[])
+{
+       struct nl_handle *nlh;
+       struct rtnl_neigh *neigh;
+       struct nl_addr *addr;
+       int err = 1;
+
+       if (nltool_init(argc, argv) < 0)
+               return -1;
+
+       if (argc < 4 || !strcmp(argv[1], "-h")) {
+               printf("Usage: nl-neigh-add <addr> <lladdr> "
+                      "<ifindex> [<state>]\n");
+               return 1;
+       }
+
+       nlh = nl_handle_alloc_nondefault(nltool_cbset);
+       if (!nlh)
+               return -1;
+
+       neigh = rtnl_neigh_alloc();
+       if (!neigh)
+               goto errout;
+
+       if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
+               goto errout_free;
+
+       addr = nltool_addr_parse(argv[1]);
+       if (!addr)
+               goto errout_close;
+       rtnl_neigh_set_dst(neigh, addr);
+       nl_addr_put(addr);
+
+       addr = nltool_addr_parse(argv[2]);
+       if (!addr)
+               goto errout_close;
+       rtnl_neigh_set_lladdr(neigh, addr);
+       nl_addr_put(addr);
+
+       rtnl_neigh_set_ifindex(neigh, strtoul(argv[3], NULL, 0));
+
+       if (argc > 4) {
+               int state = rtnl_neigh_str2state(argv[4]);
+               if (state < 0) {
+                       fprintf(stderr, "Unknown state \"%s\"\n", argv[4]);
+                       goto errout_close;
+               }
+               rtnl_neigh_set_state(neigh, state);
+       } else
+               rtnl_neigh_set_state(neigh, NUD_PERMANENT);
+
+       if (rtnl_neigh_add(nlh, neigh, 0) < 0) {
+               fprintf(stderr, "Unable to add address: %s\n", nl_geterror());
+               goto errout_close;
+       }
+
+       err = 0;
+
+errout_close:
+       nl_close(nlh);
+errout_free:
+       rtnl_neigh_put(neigh);
+errout:
+       nl_handle_destroy(nlh);
+       return err;
+}
diff --git a/src/nl-neigh-delete.c b/src/nl-neigh-delete.c
new file mode 100644 (file)
index 0000000..54a0324
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * src/nl-neigh-delete.c     Delete a neighbour
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include "utils.h"
+
+int main(int argc, char *argv[])
+{
+       struct nl_handle *nlh;
+       struct rtnl_neigh *neigh;
+       struct nl_addr *addr;
+       int err = 1;
+
+       if (nltool_init(argc, argv) < 0)
+               return -1;
+
+       if (argc < 3 || !strcmp(argv[1], "-h")) {
+               printf("Usage: nl-neigh-delete <addr> <ifindex>\n");
+               return 2;
+       }
+
+       nlh = nl_handle_alloc_nondefault(nltool_cbset);
+
+       neigh = rtnl_neigh_alloc();
+       if (neigh == NULL)
+               goto errout;
+
+       if (nl_connect(nlh, NETLINK_ROUTE) < 0) {
+               fprintf(stderr, "%s\n", nl_geterror());
+               goto errout_free;
+       }
+
+       addr = nl_addr_parse(argv[1], AF_UNSPEC);
+       if (addr == NULL) {
+               fprintf(stderr, "Invalid address \"%s\"\n", argv[1]);
+               goto errout_close;
+       }
+       rtnl_neigh_set_dst(neigh, addr);
+       nl_addr_put(addr);
+
+       rtnl_neigh_set_ifindex(neigh, strtoul(argv[2], NULL, 0));
+
+       if (rtnl_neigh_delete(nlh, neigh, 0) < 0) {
+               fprintf(stderr, "%s\n", nl_geterror());
+               goto errout_close;
+       }
+
+       err = 0;
+
+errout_close:
+       nl_close(nlh);
+errout_free:
+       rtnl_neigh_put(neigh);
+errout:
+       nl_handle_destroy(nlh);
+       return err;
+}
diff --git a/src/nl-neigh-dump.c b/src/nl-neigh-dump.c
new file mode 100644 (file)
index 0000000..8976a4f
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * src/nl-neigh-dump.c     Dump neighbour attributes
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include "utils.h"
+
+static void print_usage(void)
+{
+       printf(
+       "Usage: nl-neigh-dump <mode> [<filter>]\n"
+       "  mode := { brief | detailed | stats | xml }\n"
+       "  filter := [dev DEV] [dst ADDR] [lladdr ADDR]\n");
+       exit(1);
+}
+
+#include "f_neigh.c"
+
+int main(int argc, char *argv[])
+{
+       struct nl_handle *nlh;
+       struct nl_cache *link_cache, *neigh_cache;
+       struct rtnl_neigh *neigh;
+       struct nl_dump_params params = {
+               .dp_fd = stdout,
+               .dp_type = NL_DUMP_BRIEF
+       };
+       int err = 1;
+
+       if (nltool_init(argc, argv) < 0)
+               return -1;
+
+       if (argc < 2 || !strcmp(argv[1], "-h"))
+               print_usage();
+       
+       nlh = nl_handle_alloc_nondefault(nltool_cbset);
+       if (!nlh)
+               return -1;
+
+       neigh = rtnl_neigh_alloc();
+       if (neigh == NULL)
+               goto errout;
+
+       if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
+               goto errout_free;
+
+       link_cache = nltool_alloc_link_cache(nlh);
+       if (!link_cache)
+               goto errout_close;
+
+       neigh_cache = nltool_alloc_neigh_cache(nlh);
+       if (!neigh_cache)
+               goto errout_link_cache;
+
+       params.dp_type = nltool_parse_dumptype(argv[1]);
+       if (params.dp_type < 0)
+               goto errout_neigh_cache;
+
+       get_filter(neigh, argc, argv, 2, neigh_cache);
+
+       nl_cache_dump_filter(neigh_cache, &params, (struct nl_object *) neigh);
+
+       err = 0;
+
+errout_neigh_cache:
+       nl_cache_free(neigh_cache);
+errout_link_cache:
+       nl_cache_free(link_cache);
+errout_close:
+       nl_close(nlh);
+errout_free:
+       rtnl_neigh_put(neigh);
+errout:
+       nl_handle_destroy(nlh);
+       return err;
+}
diff --git a/src/nl-neightbl-dump.c b/src/nl-neightbl-dump.c
new file mode 100644 (file)
index 0000000..230e059
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * src/nl-neightbl-dump.c     Dump neighbour tables
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include "utils.h"
+
+static void print_usage(void)
+{
+       printf(
+       "Usage: nl-neightbl-dump <mode> [<filter>]\n"
+       "  mode := { brief | detailed | stats | xml }\n"
+       "  filter :=\n");
+       exit(1);
+}
+
+int main(int argc, char *argv[])
+{
+       int err = -1;
+       struct nl_handle *nlh;
+       struct nl_cache *ntc, *lc;
+       struct nl_dump_params params = {
+               .dp_fd = stdout,
+               .dp_type = NL_DUMP_BRIEF,
+       };
+
+       if (argc < 2)
+               print_usage();
+
+       if (nltool_init(argc, argv) < 0)
+               return -1;
+
+       nlh = nl_handle_alloc_nondefault(nltool_cbset);
+       if (!nlh)
+               return -1;
+
+       if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
+               goto errout;
+
+       ntc = nltool_alloc_neightbl_cache(nlh);
+       if (!ntc)
+               goto errout_close;
+
+       lc = nltool_alloc_link_cache(nlh);
+       if (!lc)
+               goto errout_ntbl_cache;
+
+       params.dp_type = nltool_parse_dumptype(argv[1]);
+       if (params.dp_type < 0)
+               goto errout_link_cache;
+
+       nl_cache_dump(ntc, &params);
+       err = 0;
+
+errout_link_cache:
+       nl_cache_free(lc);
+errout_ntbl_cache:
+       nl_cache_free(ntc);
+errout_close:
+       nl_close(nlh);
+errout:
+       nl_handle_destroy(nlh);
+       return err;
+}
diff --git a/src/nl-qdisc-add.c b/src/nl-qdisc-add.c
new file mode 100644 (file)
index 0000000..0bf2939
--- /dev/null
@@ -0,0 +1,196 @@
+/*
+ * src/nl-qdisc-dump.c     Dump qdisc attributes
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include "utils.h"
+#include <netlink/route/sch/fifo.h>
+#include <netlink/route/sch/prio.h>
+
+static void print_usage(void)
+{
+       printf(
+"Usage: nl-qdisc-add <ifindex> <handle> <parent> <kind>\n");
+       exit(1);
+}
+
+static int parse_blackhole_opts(struct rtnl_qdisc *qdisc, char *argv[],
+                               int argc)
+{
+       return 0;
+}
+
+static int parse_pfifo_opts(struct rtnl_qdisc *qdisc, char *argv[], int argc)
+{
+       int err, limit;
+
+       if (argc > 0) {
+               if (argc != 2 || strcasecmp(argv[0], "limit")) {
+                       fprintf(stderr, "Usage: ... pfifo limit <limit>\n");
+                       return -1;
+               }
+
+               limit = strtoul(argv[1], NULL, 0);
+               err = rtnl_qdisc_fifo_set_limit(qdisc, limit);
+               if (err < 0) {
+                       fprintf(stderr, "%s\n", nl_geterror());
+                       return -1;
+               }
+       }
+
+       return 0;
+}
+
+static int parse_bfifo_opts(struct rtnl_qdisc *qdisc, char *argv[], int argc)
+{
+       int err, limit;
+
+       if (argc > 0) {
+               if (argc != 2 || strcasecmp(argv[0], "limit")) {
+                       fprintf(stderr, "Usage: ... bfifo limit <limit>\n");
+                       return -1;
+               }
+
+               limit = nl_size2int(argv[1]);
+               if (limit < 0) {
+                       fprintf(stderr, "Invalid value for limit.\n");
+                       return -1;
+               }
+
+               err = rtnl_qdisc_fifo_set_limit(qdisc, limit);
+               if (err < 0) {
+                       fprintf(stderr, "%s\n", nl_geterror());
+                       return -1;
+               }
+       }
+
+       return 0;
+}
+
+static int parse_prio_opts(struct rtnl_qdisc *qdisc, char *argv[], int argc)
+{
+       int i, err, bands;
+       uint8_t map[] = QDISC_PRIO_DEFAULT_PRIOMAP;
+
+       if (argc > 0) {
+               if (argc < 2 || strcasecmp(argv[0], "bands"))
+                       goto usage;
+
+               bands = strtoul(argv[1], NULL, 0);
+               err = rtnl_qdisc_prio_set_bands(qdisc, bands);
+               if (err < 0) {
+                       fprintf(stderr, "%s\n", nl_geterror());
+                       return -1;
+               }
+       }
+
+       if (argc > 2) {
+               if (argc < 5 || strcasecmp(argv[2], "map"))
+                       goto usage;
+
+               for (i = 3; i < (argc & ~1U); i += 2) {
+                       int prio, band;
+
+                       prio = rtnl_str2prio(argv[i]);
+                       if (prio < 0 || prio > sizeof(map)/sizeof(map[0])) {
+                               fprintf(stderr, "Invalid priority \"%s\"\n",
+                                       argv[i]);
+                               return -1;
+                       }
+
+                       band = strtoul(argv[i+1], NULL, 0);
+
+                       map[prio] = band;
+               }
+       }
+
+       err = rtnl_qdisc_prio_set_priomap(qdisc, map, sizeof(map));
+       if (err < 0) {
+               fprintf(stderr, "%s\n", nl_geterror());
+               return -1;
+       }
+
+       return 0;
+usage:
+       fprintf(stderr, "Usage: ... prio bands <nbands> map MAP\n"
+                       "MAP := <prio> <band>\n");
+       return -1;
+}
+
+int main(int argc, char *argv[])
+{
+       struct nl_handle *nlh;
+       struct rtnl_qdisc *qdisc;
+       uint32_t handle, parent;
+       int err = 1;
+
+       if (nltool_init(argc, argv) < 0)
+               return -1;
+
+       if (argc < 5 || !strcmp(argv[1], "-h"))
+               print_usage();
+
+       nlh = nl_handle_alloc_nondefault(nltool_cbset);
+       if (!nlh)
+               goto errout;
+
+       qdisc = rtnl_qdisc_alloc();
+       if (!qdisc)
+               goto errout_free_handle;
+
+       rtnl_qdisc_set_ifindex(qdisc, strtoul(argv[1], NULL, 0));
+
+       if (rtnl_tc_str2handle(argv[2], &handle) < 0) {
+               fprintf(stderr, "%s\n", nl_geterror());
+               goto errout_free_qdisc;
+       }
+
+       if (rtnl_tc_str2handle(argv[3], &parent) < 0) {
+               fprintf(stderr, "%s\n", nl_geterror());
+               goto errout_free_qdisc;
+       }
+
+       rtnl_qdisc_set_handle(qdisc, handle);
+       rtnl_qdisc_set_parent(qdisc, parent);
+       rtnl_qdisc_set_kind(qdisc, argv[4]);
+
+       if (!strcasecmp(argv[4], "blackhole"))
+               err = parse_blackhole_opts(qdisc, &argv[5], argc-5);
+       else if (!strcasecmp(argv[4], "pfifo"))
+               err = parse_pfifo_opts(qdisc, &argv[5], argc-5);
+       else if (!strcasecmp(argv[4], "bfifo"))
+               err = parse_bfifo_opts(qdisc, &argv[5], argc-5);
+       else if (!strcasecmp(argv[4], "prio"))
+               err = parse_prio_opts(qdisc, &argv[5], argc-5);
+       else {
+               fprintf(stderr, "Unknown qdisc \"%s\"\n", argv[4]);
+               goto errout_free_qdisc;
+       }
+
+       if (err < 0)
+               goto errout_free_qdisc;
+
+       if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
+               goto errout_free_qdisc;
+
+       if (rtnl_qdisc_add(nlh, qdisc, NLM_F_REPLACE) < 0) {
+               fprintf(stderr, "Unable to add Qdisc: %s\n", nl_geterror());
+               goto errout_close;
+       }
+
+       err = 0;
+errout_close:
+       nl_close(nlh);
+errout_free_qdisc:
+       rtnl_qdisc_put(qdisc);
+errout_free_handle:
+       nl_handle_destroy(nlh);
+errout:
+       return err;
+}
diff --git a/src/nl-qdisc-delete.c b/src/nl-qdisc-delete.c
new file mode 100644 (file)
index 0000000..865d1e4
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * src/nl-qdisc-delete.c     Delete Qdiscs
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include "utils.h"
+
+static void print_usage(void)
+{
+       printf("Usage: nl-qdisc-delete <ifindex> <parent> <handle>\n");
+       exit(1);
+}
+
+int main(int argc, char *argv[])
+{
+       struct nl_handle *nlh;
+       struct rtnl_qdisc *qdisc;
+       uint32_t handle, parent;
+       int err = 1;
+
+       if (nltool_init(argc, argv) < 0)
+               return -1;
+
+       if (argc < 3 || !strcmp(argv[1], "-h"))
+               print_usage();
+
+       nlh = nl_handle_alloc_nondefault(nltool_cbset);
+       if (!nlh)
+               goto errout;
+
+       qdisc = rtnl_qdisc_alloc();
+       if (!qdisc)
+               goto errout_free_handle;
+
+       rtnl_qdisc_set_ifindex(qdisc, strtoul(argv[1], NULL, 0));
+
+       if (rtnl_tc_str2handle(argv[2], &parent) < 0) {
+               fprintf(stderr, "%s\n", nl_geterror());
+               goto errout_free_qdisc;
+       }
+
+       if (argc > 3) {
+               if (rtnl_tc_str2handle(argv[3], &handle) < 0) {
+                       fprintf(stderr, "%s\n", nl_geterror());
+                       goto errout_free_qdisc;
+               }
+
+               rtnl_qdisc_set_handle(qdisc, handle);
+       }
+
+       rtnl_qdisc_set_parent(qdisc, parent);
+
+       if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
+               goto errout_free_qdisc;
+
+       if (rtnl_qdisc_delete(nlh, qdisc) < 0) {
+               fprintf(stderr, "Unable to delete Qdisc: %s\n", nl_geterror());
+               goto errout_close;
+       }
+
+       err = 0;
+errout_close:
+       nl_close(nlh);
+errout_free_qdisc:
+       rtnl_qdisc_put(qdisc);
+errout_free_handle:
+       nl_handle_destroy(nlh);
+errout:
+       return err;
+}
diff --git a/src/nl-qdisc-dump.c b/src/nl-qdisc-dump.c
new file mode 100644 (file)
index 0000000..e677325
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * src/nl-qdisc-dump.c     Dump qdisc attributes
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include "utils.h"
+
+static void print_usage(void)
+{
+       printf(
+"Usage: nl-qdisc-dump <mode>\n"
+"  mode := { brief | detailed | stats | xml }\n");
+       exit(1);
+}
+
+int main(int argc, char *argv[])
+{
+       struct nl_handle *nlh;
+       struct nl_cache *link_cache, *qdisc_cache;
+       struct rtnl_qdisc *qdisc;
+       struct nl_dump_params params = {
+               .dp_fd = stdout,
+               .dp_type = NL_DUMP_BRIEF
+       };
+       int err = 1;
+
+       if (nltool_init(argc, argv) < 0)
+               return -1;
+
+       if (argc < 2 || !strcmp(argv[1], "-h"))
+               print_usage();
+
+       params.dp_type = nltool_parse_dumptype(argv[1]);
+       if (params.dp_type < 0)
+               return -1;
+
+       nlh = nl_handle_alloc_nondefault(nltool_cbset);
+       if (!nlh)
+               return -1;
+
+       qdisc = rtnl_qdisc_alloc();
+       if (!qdisc)
+               goto errout_no_put;
+
+       if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
+               goto errout;
+
+       link_cache = nltool_alloc_link_cache(nlh);
+       if (!link_cache)
+               goto errout;
+
+       qdisc_cache = nltool_alloc_qdisc_cache(nlh);
+       if (!qdisc_cache)
+               goto errout_link_cache;
+
+       nl_cache_dump_filter(qdisc_cache, &params, (struct nl_object *) qdisc);
+       nl_cache_free(qdisc_cache);
+       err = 0;
+
+errout_link_cache:
+       nl_cache_free(link_cache);
+errout:
+       rtnl_qdisc_put(qdisc);
+errout_no_put:
+       nl_close(nlh);
+       nl_handle_destroy(nlh);
+       return err;
+}
diff --git a/src/nl-route-dump.c b/src/nl-route-dump.c
new file mode 100644 (file)
index 0000000..18e6f4a
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * src/nl-route-dump.c     Dump route attributes
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include "utils.h"
+
+static void print_usage(void)
+{
+       printf(
+       "Usage: nl-route-dump <mode> [<filter>]\n"
+       "  mode := { brief | detailed | stats | xml }\n");
+       exit(1);
+}
+
+#include "f_route.c"
+
+int main(int argc, char *argv[])
+{
+       struct nl_handle *nlh;
+       struct nl_cache *link_cache, *route_cache;
+       struct rtnl_route *route;
+       struct nl_dump_params params = {
+               .dp_fd = stdout,
+               .dp_type = NL_DUMP_BRIEF
+       };
+       int err = 1;
+
+       if (nltool_init(argc, argv) < 0)
+               return -1;
+
+       if (argc < 2 || !strcmp(argv[1], "-h"))
+               print_usage();
+
+       nlh = nl_handle_alloc_nondefault(nltool_cbset);
+       if (!nlh)
+               goto errout;
+
+       route = rtnl_route_alloc();
+       if (!route)
+               goto errout;
+
+       if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
+               goto errout_free;
+
+       link_cache = nltool_alloc_link_cache(nlh);
+       if (!link_cache)
+               goto errout_close;
+
+       route_cache = nltool_alloc_route_cache(nlh);
+       if (!route_cache)
+               goto errout_link_cache;
+
+       params.dp_type = nltool_parse_dumptype(argv[1]);
+       if (params.dp_type < 0)
+               goto errout_route_cache;
+
+       get_filter(route, argc, argv, 2, route_cache, link_cache);
+
+       nl_cache_dump_filter(route_cache, &params, (struct nl_object *) route);
+
+       err = 0;
+
+errout_route_cache:
+       nl_cache_free(route_cache);
+errout_link_cache:
+       nl_cache_free(link_cache);
+errout_close:
+       nl_close(nlh);
+errout_free:
+       rtnl_route_put(route);
+errout:
+       nl_handle_destroy(nlh);
+       return err;
+}
diff --git a/src/nl-route-get.c b/src/nl-route-get.c
new file mode 100644 (file)
index 0000000..b5425bd
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+ * src/nl-route-get.c     Get Route Attributes
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include "utils.h"
+
+static void print_usage(void)
+{
+       printf("Usage: nl-route-get <addr>\n");
+       exit(1);
+}
+
+static int cb(struct nl_msg *msg, void *arg)
+{
+       nl_cache_parse_and_add(arg, msg);
+
+       return 0;
+}
+
+int main(int argc, char *argv[])
+{
+       struct nl_handle *nlh;
+       struct nl_cache *link_cache, *route_cache;
+       struct nl_addr *dst;
+       struct nl_dump_params params = {
+               .dp_fd = stdout,
+               .dp_type = NL_DUMP_BRIEF
+       };
+       int err = 1;
+
+       if (argc < 2 || !strcmp(argv[1], "-h"))
+               print_usage();
+
+       if (nltool_init(argc, argv) < 0)
+               goto errout;
+
+       nlh = nl_handle_alloc_nondefault(nltool_cbset);
+       if (!nlh)
+               goto errout;
+
+       if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
+               goto errout_free_handle;
+
+       link_cache = nltool_alloc_link_cache(nlh);
+       if (!link_cache)
+               goto errout_close;
+
+       dst = nltool_addr_parse(argv[1]);
+       if (!dst)
+               goto errout_link_cache;
+
+       route_cache = nltool_alloc_route_cache(nlh);
+       if (!route_cache)
+               goto errout_addr_put;
+
+       {
+               struct nl_msg *m;
+               struct rtmsg rmsg = {
+                       .rtm_family = nl_addr_get_family(dst),
+                       .rtm_dst_len = nl_addr_get_prefixlen(dst),
+               };
+
+               m = nlmsg_build_simple(RTM_GETROUTE, 0);
+               nlmsg_append(m, &rmsg, sizeof(rmsg), 1);
+               nla_put_addr(m, RTA_DST, dst);
+
+               if ((err = nl_send_auto_complete(nlh, m)) < 0) {
+                       nlmsg_free(m);
+                       fprintf(stderr, "%s\n", nl_geterror());
+                       goto errout_route_cache;
+               }
+
+               nlmsg_free(m);
+
+               nl_cb_set(nl_handle_get_cb(nlh), NL_CB_VALID, NL_CB_CUSTOM,
+                         cb, route_cache);
+
+               if (nl_recvmsgs_def(nlh) < 0) {
+                       fprintf(stderr, "%s\n", nl_geterror());
+                       goto errout_route_cache;
+               }
+       }
+
+       nl_cache_dump(route_cache, &params);
+
+       err = 0;
+errout_route_cache:
+       nl_cache_free(route_cache);
+errout_addr_put:
+       nl_addr_put(dst);
+errout_link_cache:
+       nl_cache_free(link_cache);
+errout_close:
+       nl_close(nlh);
+errout_free_handle:
+       nl_handle_destroy(nlh);
+errout:
+       return err;
+}
diff --git a/src/nl-rule-dump.c b/src/nl-rule-dump.c
new file mode 100644 (file)
index 0000000..a6a2dd9
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * src/nl-rule-dump.c     Dump rule attributes
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include "utils.h"
+
+static void print_usage(void)
+{
+       printf(
+       "Usage: nl-rule-dump <mode> [<filter>]\n"
+       "  mode := { brief | detailed | stats | xml }\n");
+       exit(1);
+}
+
+int main(int argc, char *argv[])
+{
+       struct nl_handle *nlh;
+       struct nl_cache *link_cache, *rule_cache;
+       struct rtnl_rule *rule;
+       struct nl_dump_params params = {
+               .dp_fd = stdout,
+               .dp_type = NL_DUMP_BRIEF
+       };
+       int err = 1;
+
+       if (nltool_init(argc, argv) < 0)
+               return -1;
+
+       if (argc < 2 || !strcmp(argv[1], "-h"))
+               print_usage();
+
+       nlh = nl_handle_alloc_nondefault(nltool_cbset);
+       if (!nlh)
+               return -1;
+
+       rule = rtnl_rule_alloc();
+       if (!rule)
+               goto errout;
+
+       if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
+               goto errout_free;
+
+       link_cache = nltool_alloc_link_cache(nlh);
+       if (!link_cache)
+               goto errout_close;
+
+       rule_cache = nltool_alloc_rule_cache(nlh);
+       if (!rule_cache)
+               goto errout_link_cache;
+
+       params.dp_type = nltool_parse_dumptype(argv[1]);
+       if (params.dp_type < 0)
+               goto errout_rule_cache;
+
+       //get_filter(route, argc, argv, 2, route_cache);
+
+       nl_cache_dump_filter(rule_cache, &params, (struct nl_object *) rule);
+
+       err = 0;
+
+errout_rule_cache:
+       nl_cache_free(rule_cache);
+errout_link_cache:
+       nl_cache_free(link_cache);
+errout_close:
+       nl_close(nlh);
+errout_free:
+       nl_object_put((struct nl_object *) rule);
+errout:
+       return err;
+}
diff --git a/src/nl-tctree-dump.c b/src/nl-tctree-dump.c
new file mode 100644 (file)
index 0000000..e2f11a2
--- /dev/null
@@ -0,0 +1,144 @@
+/*
+ * src/nl-tctree-dump.c                Dump Traffic Control Tree
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include "utils.h"
+#include <linux/pkt_sched.h>
+
+static struct nl_handle *nl_handle;
+static struct nl_cache *qdisc_cache, *class_cache;
+static struct nl_dump_params dump_params = {
+       .dp_type = NL_DUMP_FULL,
+};
+
+static int ifindex;
+static void print_qdisc(struct nl_object *, void *);
+
+static void print_class(struct nl_object *obj, void *arg)
+{
+       struct rtnl_qdisc *leaf;
+       struct rtnl_class *class = (struct rtnl_class *) obj;
+       struct nl_cache *cls_cache;
+       uint32_t parent = rtnl_class_get_handle(class);
+
+       dump_params.dp_prefix = (int) arg;
+       nl_object_dump(obj, &dump_params);
+
+       leaf = rtnl_class_leaf_qdisc(class, qdisc_cache);
+       if (leaf)
+               print_qdisc((struct nl_object *) leaf, arg + 2);
+
+       rtnl_class_foreach_child(class, class_cache, &print_class, arg + 2);
+
+       cls_cache = rtnl_cls_alloc_cache(nl_handle, ifindex, parent);
+       if (!cls_cache)
+               return;
+
+       dump_params.dp_prefix = (int) arg + 2;
+       nl_cache_dump(cls_cache, &dump_params);
+       nl_cache_free(cls_cache);
+}
+
+static void print_qdisc(struct nl_object *obj, void *arg)
+{
+       struct rtnl_qdisc *qdisc = (struct rtnl_qdisc *) obj;
+       struct nl_cache *cls_cache;
+       uint32_t parent = rtnl_qdisc_get_handle(qdisc);
+
+       dump_params.dp_prefix = (int) arg;
+       nl_object_dump(obj, &dump_params);
+
+       rtnl_qdisc_foreach_child(qdisc, class_cache, &print_class, arg + 2);
+
+       cls_cache = rtnl_cls_alloc_cache(nl_handle, ifindex, parent);
+       if (!cls_cache)
+               return;
+
+       dump_params.dp_prefix = (int) arg + 2;
+       nl_cache_dump(cls_cache, &dump_params);
+       nl_cache_free(cls_cache);
+}
+
+static void print_link(struct nl_object *obj, void *arg)
+{
+       struct rtnl_link *link = (struct rtnl_link *) obj;
+       struct rtnl_qdisc *qdisc;
+
+       ifindex = rtnl_link_get_ifindex(link);
+       dump_params.dp_prefix = 0;
+       nl_object_dump(obj, &dump_params);
+
+       class_cache = rtnl_class_alloc_cache(nl_handle, ifindex);
+       if (!class_cache)
+               return;
+
+       qdisc = rtnl_qdisc_get_by_parent(qdisc_cache, ifindex, TC_H_ROOT);
+       if (qdisc) {
+               print_qdisc((struct nl_object *) qdisc, (void *) 2);
+               rtnl_qdisc_put(qdisc);
+       }
+
+       qdisc = rtnl_qdisc_get_by_parent(qdisc_cache, ifindex, 0);
+       if (qdisc) {
+               print_qdisc((struct nl_object *) qdisc, (void *) 2);
+               rtnl_qdisc_put(qdisc);
+       }
+
+       qdisc = rtnl_qdisc_get_by_parent(qdisc_cache, ifindex, TC_H_INGRESS);
+       if (qdisc) {
+               print_qdisc((struct nl_object *) qdisc, (void *) 2);
+               rtnl_qdisc_put(qdisc);
+       }
+
+       nl_cache_free(class_cache);
+}
+
+int main(int argc, char *argv[])
+{
+       struct nl_cache *link_cache;
+
+       if (nltool_init(argc, argv) < 0)
+               return -1;
+
+       dump_params.dp_fd = stdout;
+
+       if (argc > 1) {
+               if (!strcasecmp(argv[1], "brief"))
+                       dump_params.dp_type = NL_DUMP_BRIEF;
+               else if (!strcasecmp(argv[1], "full"))
+                       dump_params.dp_type = NL_DUMP_FULL;
+               else if (!strcasecmp(argv[1], "stats"))
+                       dump_params.dp_type = NL_DUMP_STATS;
+       }
+
+       nl_handle = nl_handle_alloc_nondefault(nltool_cbset);
+       if (!nl_handle)
+               return 1;
+
+       if (nltool_connect(nl_handle, NETLINK_ROUTE) < 0)
+               return 1;
+
+       link_cache = nltool_alloc_link_cache(nl_handle);
+       if (!link_cache)
+               return 1;
+
+       qdisc_cache = nltool_alloc_qdisc_cache(nl_handle);
+       if (!qdisc_cache)
+               return 1;
+
+       nl_cache_foreach(link_cache, &print_link, NULL);
+
+       nl_cache_free(qdisc_cache);
+       nl_cache_free(link_cache);
+
+       nl_close(nl_handle);
+       nl_handle_destroy(nl_handle);
+       return 0;
+}
diff --git a/src/nl-util-addr.c b/src/nl-util-addr.c
new file mode 100644 (file)
index 0000000..9f12795
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * src/nl-util-addr.c     Address Helper
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include "utils.h"
+
+int main(int argc, char *argv[])
+{
+       int err;
+       char host[256];
+       struct nl_addr *a;
+
+       if (argc < 2) {
+               fprintf(stderr, "Usage: nl-util-addr <address>\n");
+               return -1;
+       }
+       
+       a = nl_addr_parse(argv[1], AF_UNSPEC);
+       if (a == NULL) {
+               fprintf(stderr, "Cannot parse address \"%s\"\n", argv[1]);
+               return -1;
+       }
+
+       err = nl_addr_resolve(a, host, sizeof(host));
+       if (err != 0) {
+               fprintf(stderr, "Cannot resolve address \"%s\": %d\n",
+                       argv[1], err);
+               return -1;
+       }
+
+       printf("%s\n", host);
+
+       return 0;
+}
diff --git a/src/utils.c b/src/utils.c
new file mode 100644 (file)
index 0000000..c00a300
--- /dev/null
@@ -0,0 +1,192 @@
+/*
+ * src/utils.c         Utilities
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include "utils.h"
+
+#include <stdlib.h>
+
+int nltool_cbset = NL_CB_VERBOSE;
+
+int nltool_init(int argc, char *argv[])
+{
+       char *nlcb = getenv("NLCB");
+       char *nldbg = getenv("NLDBG");
+       
+       if (nlcb) {
+               if (!strcasecmp(nlcb, "default"))
+                       nltool_cbset = NL_CB_DEFAULT;
+               else if (!strcasecmp(nlcb, "verbose"))
+                       nltool_cbset = NL_CB_VERBOSE;
+               else if (!strcasecmp(nlcb, "debug"))
+                       nltool_cbset = NL_CB_DEBUG;
+               else {
+                       fprintf(stderr, "Unknown value for NLCB, valid values: "
+                               "{default | verbose | debug}\n");
+                       goto errout;
+               }
+       }
+
+       if (nldbg) {
+               long dbg = strtol(nldbg, NULL, 0);
+
+               if (dbg == LONG_MIN || dbg == LONG_MAX) {
+                       fprintf(stderr, "Invalid value for NLDBG.\n");
+                       goto errout;
+               }
+
+               nl_debug = dbg;
+       }
+       
+       return 0;
+
+errout:
+       return -1;
+}
+
+int nltool_connect(struct nl_handle *nlh, int protocol)
+{
+       int err;
+
+       err = nl_connect(nlh, protocol);
+       if (err < 0)
+               fprintf(stderr, "Unable to connect netlink socket%s\n",
+                       nl_geterror());
+
+       return err;
+}
+
+struct nl_addr *nltool_addr_parse(const char *str)
+{
+       struct nl_addr *addr;
+
+       addr = nl_addr_parse(str, AF_UNSPEC);
+       if (!addr)
+               fprintf(stderr, "Unable to parse address \"%s\": %s\n",
+                       str, nl_geterror());
+
+       return addr;
+}
+
+int nltool_parse_dumptype(const char *str)
+{
+       if (!strcasecmp(str, "brief"))
+               return NL_DUMP_BRIEF;
+       else if (!strcasecmp(str, "detailed"))
+               return NL_DUMP_FULL;
+       else if (!strcasecmp(str, "stats"))
+               return NL_DUMP_STATS;
+       else if (!strcasecmp(str, "xml"))
+               return NL_DUMP_XML;
+       else if (!strcasecmp(str, "env"))
+               return NL_DUMP_ENV;
+       else {
+               fprintf(stderr, "Invalid dump type \"%s\".\n", str);
+               return -1;
+       }
+}
+
+struct nl_cache *nltool_alloc_link_cache(struct nl_handle *nlh)
+{
+       struct nl_cache *cache;
+
+       cache = rtnl_link_alloc_cache(nlh);
+       if (!cache)
+               fprintf(stderr, "Unable to retrieve link cache: %s\n",
+                       nl_geterror());
+       else
+               nl_cache_mngt_provide(cache);
+
+       return cache;
+}
+
+struct nl_cache *nltool_alloc_addr_cache(struct nl_handle *nlh)
+{
+       struct nl_cache *cache;
+
+       cache = rtnl_addr_alloc_cache(nlh);
+       if (!cache)
+               fprintf(stderr, "Unable to retrieve address cache: %s\n",
+                       nl_geterror());
+       else
+               nl_cache_mngt_provide(cache);
+
+       return cache;
+}
+
+struct nl_cache *nltool_alloc_neigh_cache(struct nl_handle *nlh)
+{
+       struct nl_cache *cache;
+
+       cache = rtnl_neigh_alloc_cache(nlh);
+       if (!cache)
+               fprintf(stderr, "Unable to retrieve neighbour cache: %s\n",
+                       nl_geterror());
+       else
+               nl_cache_mngt_provide(cache);
+
+       return cache;
+}
+
+struct nl_cache *nltool_alloc_neightbl_cache(struct nl_handle *nlh)
+{
+       struct nl_cache *cache;
+
+       cache = rtnl_neightbl_alloc_cache(nlh);
+       if (!cache)
+               fprintf(stderr, "Unable to retrieve neighbour table "
+                               "cache: %s\n", nl_geterror());
+       else
+               nl_cache_mngt_provide(cache);
+
+       return cache;
+}
+
+struct nl_cache *nltool_alloc_route_cache(struct nl_handle *nlh)
+{
+       struct nl_cache *cache;
+
+       cache = rtnl_route_alloc_cache(nlh);
+       if (!cache)
+               fprintf(stderr, "Unable to retrieve route cache: %s\n",
+                       nl_geterror());
+       else
+               nl_cache_mngt_provide(cache);
+
+       return cache;
+}
+
+struct nl_cache *nltool_alloc_rule_cache(struct nl_handle *nlh)
+{
+       struct nl_cache *cache;
+
+       cache = rtnl_rule_alloc_cache(nlh);
+       if (!cache)
+               fprintf(stderr, "Unable to retrieve rule cache: %s\n",
+                       nl_geterror());
+       else
+               nl_cache_mngt_provide(cache);
+
+       return cache;
+}
+
+struct nl_cache *nltool_alloc_qdisc_cache(struct nl_handle *nlh)
+{
+       struct nl_cache *cache;
+
+       cache = rtnl_qdisc_alloc_cache(nlh);
+       if (!cache)
+               fprintf(stderr, "Unable to retrieve qdisc cache: %s\n",
+                       nl_geterror());
+       else
+               nl_cache_mngt_provide(cache);
+
+       return cache;
+}
diff --git a/src/utils.h b/src/utils.h
new file mode 100644 (file)
index 0000000..c12e463
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * src/utils.h         Utilities
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation version 2.1
+ *     of the License.
+ *
+ * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
+ */
+
+#ifndef __SRC_UTILS_H_
+#define __SRC_UTILS_H_
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <inttypes.h>
+#include <errno.h>
+#include <stdint.h>
+#include <getopt.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+
+#include <netlink-local.h>
+#include <netlink/netlink.h>
+#include <netlink/utils.h>
+#include <netlink/addr.h>
+#include <netlink/route/rtnl.h>
+#include <netlink/route/link.h>
+#include <netlink/route/addr.h>
+#include <netlink/route/neighbour.h>
+#include <netlink/route/neightbl.h>
+#include <netlink/route/route.h>
+#include <netlink/route/rule.h>
+#include <netlink/route/qdisc.h>
+#include <netlink/route/class.h>
+#include <netlink/route/classifier.h>
+#include <netlink/fib_lookup/lookup.h>
+#include <netlink/fib_lookup/request.h>
+
+extern int nltool_init(int argc, char *argv[]);
+extern int nltool_connect(struct nl_handle *nlh, int protocol);
+extern struct nl_addr *nltool_addr_parse(const char *str);
+extern int nltool_parse_dumptype(const char *str);
+
+extern struct nl_cache *nltool_alloc_link_cache(struct nl_handle *nlh);
+extern struct nl_cache *nltool_alloc_addr_cache(struct nl_handle *nlh);
+extern struct nl_cache *nltool_alloc_neigh_cache(struct nl_handle *nlh);
+extern struct nl_cache *nltool_alloc_neightbl_cache(struct nl_handle *nlh);
+extern struct nl_cache *nltool_alloc_route_cache(struct nl_handle *nlh);
+extern struct nl_cache *nltool_alloc_rule_cache(struct nl_handle *nlh);
+extern struct nl_cache *nltool_alloc_qdisc_cache(struct nl_handle *nlh);
+
+extern int nltool_cbset;
+
+#define arg_match(str) !strcasecmp(argv[idx], str)
+
+#endif