Merge quagga mainline into the google ISIS code.

The steps were:

  $ git checkout google-is-is
  $ git merge quagga
  $ git checkout google-is-is -- isisd

  # Resolve conflicts in the following:
  lib/md5.h
  zebra/rt_netlink.c
  zebra/zebra_rib.c
  zebra/zserv.c

Note that the content in the isisd directory is left unchanged in the
merge. As a result, changes made to isisd as part of the following
commits on the quagga mainline are dropped.

  # 8ced4e82 is the merge base, e96b3121 is the current quagga master
  $ git log --oneline --reverse 8ced4e82..e96b3121 -- isisd
  5574999 isisd: fix crash on "no router isis" (BZ#536)
  8998075 isisd: raise hello rate for DIS (BZ#539)
  306ca83 isisd: include hash.h, not hash.c
  b82cdeb delete CVS keywords
  2f65867 isisd: indent longopts array
  b511468 quagga: option "-z" ("--socket <path>") added
  05e54ee build: delete .cvsignore files
  b4e45f6 fix zebra protocol after MP-BGP changes
  7fd6cd8 isisd: fix circuit state machine
  907fd95 isisd: send proper LSP after DIS election
  d034aa0 isisd: fix wrong next-hops from SPF
  c25eaff isisd: unexpected kernel routing table (BZ#544)
  e6b03b7 isisd: implement MD5 circuit authentication
diff --git a/.cvsignore b/.cvsignore
deleted file mode 100644
index a67bba0..0000000
--- a/.cvsignore
+++ /dev/null
@@ -1,30 +0,0 @@
-config.log
-config.h
-config.cache
-config.status
-config.guess
-config.sub
-ltmain.sh
-stamp-h
-stamp-h[0-9]*
-*-stamp
-Makefile
-INSTALL
-.deps
-depcomp
-missing
-install-sh
-autom4te*.cache
-configure.lineno
-configure
-config.h.in
-aclocal.m4
-Makefile.in
-zebra-[0-9.][0-9.][0-9.]*.tar.gz
-quagga-[0-9.][0-9.][0-9.]*.tar.gz
-quagga-[0-9.][0-9.][0-9.]*.tar.gz.asc
-.nfs*
-libtool
-.arch-inventory
-.arch-ids
-{arch}
diff --git a/HACKING b/HACKING
deleted file mode 100644
index 0358fed..0000000
--- a/HACKING
+++ /dev/null
@@ -1,357 +0,0 @@
--*- mode: text; -*-
-$QuaggaId: Format:%an, %ai, %h$ $
-
-Contents:
-
-* GUIDELINES FOR HACKING ON QUAGGA
-* COMPILE-TIME CONDITIONAL CODE
-* COMMIT MESSAGE
-* HACKING THE BUILD SYSTEM
-* RELEASE PROCEDURE
-* SHARED LIBRARY VERSIONING
-* RELEASE PROCEDURE
-* TOOL VERSIONS
-* SHARED LIBRARY VERSIONING
-* PATCH SUBMISSION
-* PATCH APPLICATION
-* STABLE PLATFORMS AND DAEMONS
-* IMPORT OR UPDATE VENDOR SPECIFIC ROUTING PROTOCOLS
-
-
-GUIDELINES FOR HACKING ON QUAGGA
-
-[this is a draft in progress]
-
-GNU coding standards apply.  Indentation follows the result of
-invoking GNU indent (as of 2.2.8a) with no arguments.  Note that this
-uses tabs instead of spaces where possible for leading whitespace, and
-assumes that tabs are every 8 columns.  Do not attempt to redefine the
-location of tab stops.  Note also that some indentation does not
-follow GNU style.  This is a historical accident, and we generally
-only clean up whitespace when code is unmaintainable due to whitespace
-issues, as fewer changes from zebra lead to easier merges.
-
-For GNU emacs, use indentation style "gnu".
-
-For Vim, use the following lines (note that tabs are at 8, and that
-softtabstop sets the indentation level):
-
-set tabstop=8
-set softtabstop=2
-set shiftwidth=2
-set noexpandtab
-
-Be particularly careful not to break platforms/protocols that you
-cannot test.
-
-New code should have good comments, and changes to existing code
-should in many cases upgrade the comments when necessary for a
-reviewer to conclude that the change has no unintended consequences.
-
-Each file in the Git repository should have a git format-placeholder (like
-an RCS Id keyword), somewhere very near the top, commented out appropriately
-for the file type. The placeholder used for Quagga (replacing <dollar> with
-$) is:
-
-	$QuaggaId: <dollar>Format:%an, %ai, %h<dollar> $
-
-See line 2 of HACKING for an example;
-
-This placeholder string will be expanded out by the 'git archive' commands,
-wihch is used to generate the tar archives for snapshots and releases.
-
-Please document fully the proper use of a new function in the header file
-in which it is declared.  And please consult existing headers for
-documentation on how to use existing functions.  In particular, please consult
-these header files:
-
-  lib/log.h	logging levels and usage guidance
-  [more to be added]
-
-If changing an exported interface, please try to deprecate the interface in
-an orderly manner. If at all possible, try to retain the old deprecated
-interface as is, or functionally equivalent. Make a note of when the
-interface was deprecated and guard the deprecated interface definitions in
-the header file, ie:
-
-/* Deprecated: 20050406 */
-#if !defined(QUAGGA_NO_DEPRECATED_INTERFACES)
-#warning "Using deprecated <libname> (interface(s)|function(s))"
-...
-#endif /* QUAGGA_NO_DEPRECATED_INTERFACES */
-
-To ensure that the core Quagga sources do not use the deprecated interfaces
-(you should update Quagga sources to use new interfaces, if applicable)
-while allowing external sources to continue to build. Deprecated interfaces
-should be excised in the next unstable cycle.
-
-Note: If you wish, you can test for GCC and use a function
-marked with the 'deprecated' attribute. However, you must provide the
-#warning for other compilers.
-
-If changing or removing a command definition, *ensure* that you properly
-deprecate it - use the _DEPRECATED form of the appropriate DEFUN macro. This
-is *critical*. Even if the command can no longer function, you *must* still
-implement it as a do-nothing stub. Failure to follow this causes grief for
-systems administrators. Deprecated commands should be excised in the next
-unstable cycle. A list of deprecated commands should be collated for each
-release.
-
-See also below regarding SHARED LIBRARY VERSIONING.
-
-
-COMPILE-TIME CONDITIONAL CODE
-
-Please think very carefully before making code conditional at compile time,
-as it increases maintenance burdens and user confusion. In particular,
-please avoid gratuitious --enable-.... switches to the configure script -
-typically code should be good enough to be in Quagga, or it shouldn't be
-there at all.
-
-When code must be compile-time conditional, try have the compiler make it
-conditional rather than the C pre-processor. I.e. this:
-
-    if (SOME_SYMBOL)
-      frobnicate();
-
-rather than:
-
-  #ifdef SOME_SYMBOL
-  frobnicate ();
-  #endif /* SOME_SYMBOL */
-
-Note that the former approach requires ensuring that SOME_SYMBOL will be
-defined (watch your AC_DEFINEs).
-
-
-COMMIT MESSAGES
-
-The commit message should provide:
-
-* A suitable one-line summary followed by a blank line as the very
-  first line of the message, in the form:
-
-  topic: high-level, one line summary
-
-  Where topic would tend to be name of a subdirectory, and/or daemon, unless
-  there's a more suitable topic (e.g. 'build').  This topic is used to
-  organise change summaries in release announcements.
-
-* An optional introduction, discussing the general intent of the change.
-* A short description of each change made, preferably:
-  * file by file
-    * function by function (use of "ditto", or globs is allowed)
-
-to provide a short description of the general intent of the patch, in terms
-of the problem it solves and how it achieves it, to help reviewers
-understand.
-
-The one-line summary must be limited to 54 characters, and all other
-lines to 72 characters.
-
-The reason for such itemised commit messages is to encourage the author to
-self-review every line of the patch, as well as provide reviewers an index
-of which changes are intended, along with a short description for each.
-Some discretion is obviously required.  A C-to-english description is not
-desireable.  For short patches, a per-function/file break-down may be
-redundant.  For longer patches, such a break-down may be essential.
-
-An example (where the general discussion is obviously somewhat redundant,
-given the one-line summary):
-
-zebra: Enhance frob FSM to detect loss of frob
-
-* (general) Add a new DOWN state to the frob state machine
-   to allow the barinator to detect loss of frob.
-* frob.h: (struct frob) Add DOWN state flag.
-* frob.c: (frob_change) set/clear DOWN appropriately on state change.
-* bar.c: (barinate) Check frob for DOWN state.
-
-Note that the commit message format follows git norms, so that "git
-log --oneline" will have useful output.
-
-HACKING THE BUILD SYSTEM
-
-If you change or add to the build system (configure.ac, any Makefile.am,
-etc.), try to check that the following things still work:
-
-	- make dist
-	- resulting dist tarball builds
-	- out-of-tree builds
-
-The quagga.net site relies on make dist to work to generate snapshots. It
-must work. Common problems are to forget to have some additional file
-included in the dist, or to have a make rule refer to a source file without
-using the srcdir variable.
-
-
-RELEASE PROCEDURE
-
-* Tag the apppropriate commit with a release tag (follow existing
-  conventions).
-  [This enables recreating the release, and is just good CM practice.]
-
-* Create a fresh tar archive of the quagga.net repository, and do a test
-  build:
-
-    git-clone git:///code.quagga.net/quagga.git quagga
-    git-archive --remote=git://code.quagga.net/quagga.git \
-        --prefix=quagga-release/ master | tar -xf -
-    cd quagga-release
-
-    autoreconf -i
-    ./configure
-    make
-    make dist
-
-The tarball which 'make dist' creates is the tarball to be released! The
-git-archive step ensures you're working with code corresponding to that in
-the official repository, and also carries out keyword expansion. If any
-errors occur, move tags as needed and start over from the fresh checkouts.
-Do not append to tarballs, as this has produced non-standards-conforming
-tarballs in the past.
-
-See also: http://wiki.quagga.net/index.php/Main/Processes
-
-[TODO: collation of a list of deprecated commands. Possibly can be scripted
-to extract from vtysh/vtysh_cmd.c]
-
-
-TOOL VERSIONS
-
-Require versions of support tools are listed in INSTALL.quagga.txt.
-Required versions should only be done with due deliberation, as it can
-cause environments to no longer be able to compile quagga.
-
-
-SHARED LIBRARY VERSIONING
-
-[this section is at the moment just gdt's opinion]
-
-Quagga builds several shared libaries (lib/libzebra, ospfd/libospf,
-ospfclient/libsopfapiclient).  These may be used by external programs,
-e.g. a new routing protocol that works with the zebra daemon, or
-ospfapi clients.  The libtool info pages (node Versioning) explain
-when major and minor version numbers should be changed.  These values
-are set in Makefile.am near the definition of the library.  If you
-make a change that requires changing the shared library version,
-please update Makefile.am.
-
-libospf exports far more than it should, and is needed by ospfapi
-clients.  Only bump libospf for changes to functions for which it is
-reasonable for a user of ospfapi to call, and please err on the side
-of not bumping.
-
-There is no support intended for installing part of zebra.  The core
-library libzebra and the included daemons should always be built and
-installed together.
-
-
-GIT COMMIT SUBSMISSION
-
-The preferred method for changes is to provide git commits via a
-publically-accessible git repository.
-
-All content guidelines in PATCH SUBMISSION apply.
-
-
-PATCH SUBMISSION
-
-* Send a clean diff against the 'master' branch of the quagga.git
-  repository, in unified diff format, preferably with the '-p' argument to
-  show C function affected by any chunk, and with the -w and -b arguments to
-  minimise changes. E.g:
-
-    git diff -up mybranch..remotes/quagga.net/master
-
-  It is preferable to use git format-patch, and even more preferred to
-  publish a git repostory.
-
-  If not using git format-patch, Include the commit message in the email.
-
-* After a commit, code should have comments explaining to the reviewer
-  why it is correct, without reference to history.  The commit message
-  should explain why the change is correct.
-
-* Include NEWS entries as appropriate.
-
-* Include only one semantic change or group of changes per patch.
-
-* Do not make gratuitous changes to whitespace. See the w and b arguments
-  to diff.
-
-* State on which platforms and with what daemons the patch has been
-  tested.  Understand that if the set of testing locations is small,
-  and the patch might have unforeseen or hard to fix consequences that
-  there may be a call for testers on quagga-dev, and that the patch
-  may be blocked until test results appear.
-
-  If there are no users for a platform on quagga-dev who are able and
-  willing to verify -current occasionally, that platform may be
-  dropped from the "should be checked" list.
-
-
-PATCH APPLICATION
-
-* Only apply patches that meet the submission guidelines.
-
-* If the patch might break something, issue a call for testing on the
-  mailinglist.
-
-* Give an appropriate commit message (see above), and use the --author
-  argument to git-commit, if required, to ensure proper attribution (you
-  should still be listed as committer)
-
-* Immediately after commiting, double-check (with git-log and/or gitk). If
-  there's a small mistake you can easily fix it with 'git commit --amend ..'
-
-* By committing a patch, you are responsible for fixing problems
-  resulting from it (or backing it out).
-
-
-STABLE PLATFORMS AND DAEMONS
-
-The list of platforms that should be tested follow.  This is a list
-derived from what quagga is thought to run on and for which
-maintainers can test or there are people on quagga-dev who are able
-and willing to verify that -current does or does not work correctly.
-
-  BSD (Free, Net or Open, any platform) # without capabilities
-  GNU/Linux (any distribution, i386)
-  Solaris (strict alignment, any platform)
-  [future: NetBSD/sparc64]
-
-The list of daemons that are thought to be stable and that should be
-tested are:
-
-  zebra
-  bgpd
-  ripd
-  ospfd
-  ripngd
-
-Daemons which are in a testing phase are
-
-  ospf6d
-  isisd
-  watchquagga
-
-
-IMPORT OR UPDATE VENDOR SPECIFIC ROUTING PROTOCOLS
-
-The source code of Quagga is based on two vendors:
-
-   zebra_org (http://www.zebra.org/)
-   isisd_sf (http://isisd.sf.net/)
-
-To import code from further sources, e.g. for archival purposes without
-necessarily having to review and/or fix some changeset, create a branch from
-'master':
-
-	git checkout -b archive/foo master
-	<apply changes>
-	git commit -a "Joe Bar <joe@example.com>"
-	git push quagga archive/foo
-
-presuming 'quagga' corresponds to a file in your .git/remotes with
-configuration for the appropriate Quagga.net repository.
diff --git a/HACKING.pending b/HACKING.pending
index 5e0defd..dfce5ce 100644
--- a/HACKING.pending
+++ b/HACKING.pending
@@ -14,6 +14,11 @@
 
 * public git repositories
 
+** git remote add quagga-re git://github.com/Quagga-RE/quagga-RE.git
+
+Maintained by Denis Ovsienko, and geared towards producing a
+production-ready branch of Quagga, in the Quagga-RE-stable branch.
+
 ** git remote add equinox git://git.spaceboyz.net/equinox/quagga.git/
 
 This repository has topic branches for patches intended for inclusion
diff --git a/HACKING.tex b/HACKING.tex
new file mode 100644
index 0000000..a49113f
--- /dev/null
+++ b/HACKING.tex
@@ -0,0 +1,462 @@
+%% -*- mode: text; -*-
+%% $QuaggaId: Format:%an, %ai, %h$ $
+
+\documentclass[oneside]{article}
+\usepackage{parskip}
+\usepackage[bookmarks,colorlinks=true]{hyperref}
+
+\title{Conventions for working on Quagga}
+
+\begin{document}
+\maketitle
+
+This is a living document. Suggestions for updates, via the
+\href{http://lists.quagga.net/mailman/listinfo/quagga-dev}{quagga-dev list},
+are welcome.
+
+\tableofcontents
+
+\section{GUIDELINES FOR HACKING ON QUAGGA}
+\label{sec:guidelines}
+
+
+GNU coding standards apply.  Indentation follows the result of
+invoking GNU indent (as of 2.2.8a) with no arguments.  Note that this
+uses tabs instead of spaces where possible for leading whitespace, and
+assumes that tabs are every 8 columns.  Do not attempt to redefine the
+location of tab stops.  Note also that some indentation does not
+follow GNU style.  This is a historical accident, and we generally
+only clean up whitespace when code is unmaintainable due to whitespace
+issues, to minimise merging conflicts.
+
+For GNU emacs, use indentation style ``gnu''.
+
+For Vim, use the following lines (note that tabs are at 8, and that
+softtabstop sets the indentation level):
+
+set tabstop=8
+set softtabstop=2
+set shiftwidth=2
+set noexpandtab
+
+Be particularly careful not to break platforms/protocols that you
+cannot test.
+
+New code should have good comments, which explain why the code is correct.
+Changes to existing code should in many cases upgrade the comments when
+necessary for a reviewer to conclude that the change has no unintended
+consequences.
+
+Each file in the Git repository should have a git format-placeholder (like
+an RCS Id keyword), somewhere very near the top, commented out appropriately
+for the file type. The placeholder used for Quagga (replacing <dollar> with
+\$) is:
+
+	\verb|$QuaggaId: <dollar>Format:%an, %ai, %h<dollar> $|
+
+See line 2 of HACKING.tex, the source for this document, for an example.
+
+This placeholder string will be expanded out by the `git archive' commands,
+wihch is used to generate the tar archives for snapshots and releases.
+
+Please document fully the proper use of a new function in the header file
+in which it is declared.  And please consult existing headers for
+documentation on how to use existing functions.  In particular, please consult
+these header files:
+
+\begin{description}
+  \item{lib/log.h}	logging levels and usage guidance
+  \item{[more to be added]}
+\end{description}
+
+If changing an exported interface, please try to deprecate the interface in
+an orderly manner. If at all possible, try to retain the old deprecated
+interface as is, or functionally equivalent. Make a note of when the
+interface was deprecated and guard the deprecated interface definitions in
+the header file, ie:
+
+\begin{verbatim}
+/* Deprecated: 20050406 */
+#if !defined(QUAGGA_NO_DEPRECATED_INTERFACES)
+#warning "Using deprecated <libname> (interface(s)|function(s))"
+...
+#endif /* QUAGGA_NO_DEPRECATED_INTERFACES */
+\end{verbatim}
+
+This is to ensure that the core Quagga sources do not use the deprecated
+interfaces (you should update Quagga sources to use new interfaces, if
+applicable), while allowing external sources to continue to build. 
+Deprecated interfaces should be excised in the next unstable cycle.
+
+Note: If you wish, you can test for GCC and use a function
+marked with the 'deprecated' attribute. However, you must provide the
+warning for other compilers.
+
+If changing or removing a command definition, \emph{ensure} that you
+properly deprecate it - use the \_DEPRECATED form of the appropriate DEFUN
+macro.  This is \emph{critical}.  Even if the command can no longer
+function, you \emph{MUST} still implement it as a do-nothing stub. 
+
+Failure to follow this causes grief for systems administrators, as an
+upgrade may cause daemons to fail to start because of unrecognised commands. 
+Deprecated commands should be excised in the next unstable cycle.  A list of
+deprecated commands should be collated for each release.
+
+See also section~\ref{sec:dll-versioning} below regarding SHARED LIBRARY
+VERSIONING.
+
+
+\section{COMPILE-TIME CONDITIONAL CODE}
+
+Please think very carefully before making code conditional at compile time,
+as it increases maintenance burdens and user confusion. In particular,
+please avoid gratuitious --enable-\ldots switches to the configure script -
+typically code should be good enough to be in Quagga, or it shouldn't be
+there at all.
+
+When code must be compile-time conditional, try have the compiler make it
+conditional rather than the C pre-processor - so that it will still be
+checked by the compiler, even if disabled. I.e.  this:
+
+\begin{verbatim}
+    if (SOME_SYMBOL)
+      frobnicate();
+\end{verbatim}
+
+rather than:
+
+\begin{verbatim}
+  #ifdef SOME_SYMBOL
+  frobnicate ();
+  #endif /* SOME_SYMBOL */
+\end{verbatim}
+
+Note that the former approach requires ensuring that SOME\_SYMBOL will be
+defined (watch your AC\_DEFINEs).
+
+
+\section{COMMIT MESSAGES}
+
+The commit message requirements are:
+
+\begin{itemize}
+
+\item The message \emph{MUST} provide a suitable one-line summary followed
+      by a blank line as the very first line of the message, in the form:
+
+  \verb|topic: high-level, one line summary|
+
+  Where topic would tend to be name of a subdirectory, and/or daemon, unless
+  there's a more suitable topic (e.g.  'build').  This topic is used to
+  organise change summaries in release announcements.
+
+\item It should have a suitable "body", which tries  to address the
+      following areas, so as to help reviewers and future browsers of the
+      code-base understand why the change is correct (note also the code
+      comment requirements):
+
+  \begin{itemize}
+  
+  \item The motivation for the change (does it fix a bug, if so which? 
+        add a feature?)
+  
+  \item The general approach taken, and trade-offs versus any other
+        approaches.
+  
+  \item Any testing undertaken or other information affecting the confidence
+        that can be had in the change.
+  
+  \item Information to allow reviewers to be able to tell which specific
+        changes to the code are intended (and hence be able to spot any accidental
+        unintended changes).
+
+  \end{itemize}
+\end{itemize}
+
+The one-line summary must be limited to 54 characters, and all other
+lines to 72 characters.
+
+Commit message bodies in the Quagga project have typically taken the
+following form:
+
+\begin{itemize}
+\item An optional introduction, describing the change generally.
+\item A short description of each specific change made, preferably:
+  \begin{itemize} \item file by file
+    \begin{itemize} \item function by function (use of "ditto", or globs is
+                          allowed)
+    \end{itemize}
+  \end{itemize}
+\end{itemize}
+
+Contributors are strongly encouraged to follow this form.
+
+This itemised commit messages allows reviewers to have confidence that the
+author has self-reviewed every line of the patch, as well as providing
+reviewers a clear index of which changes are intended, and descriptions for
+them (C-to-english descriptions are not desireable - some discretion is
+useful).  For short patches, a per-function/file break-down may be
+redundant.  For longer patches, such a break-down may be essential.  A
+contrived example (where the general discussion is obviously somewhat
+redundant, given the one-line summary):
+
+\begin{quote}\begin{verbatim}
+zebra: Enhance frob FSM to detect loss of frob
+
+Add a new DOWN state to the frob state machine to allow the barinator to
+detect loss of frob.
+
+* frob.h: (struct frob) Add DOWN state flag.
+* frob.c: (frob\_change) set/clear DOWN appropriately on state change.
+* bar.c: (barinate) Check frob for DOWN state.
+\end{verbatim}\end{quote}
+
+Please have a look at the git commit logs to get a feel for what the norms
+are.
+
+Note that the commit message format follows git norms, so that ``git
+log --oneline'' will have useful output.
+
+\section{HACKING THE BUILD SYSTEM}
+
+If you change or add to the build system (configure.ac, any Makefile.am,
+etc.), try to check that the following things still work:
+
+\begin{itemize}
+\item make dist
+\item resulting dist tarball builds
+\item out-of-tree builds
+\end{itemize}
+
+The quagga.net site relies on make dist to work to generate snapshots. It
+must work. Common problems are to forget to have some additional file
+included in the dist, or to have a make rule refer to a source file without
+using the srcdir variable.
+
+
+\section{RELEASE PROCEDURE}
+
+\begin{itemize}
+\item Tag the apppropriate commit with a release tag (follow existing
+  conventions).
+  
+  [This enables recreating the release, and is just good CM practice.]
+
+\item Create a fresh tar archive of the quagga.net repository, and do a test
+  build:
+
+  \begin{verbatim}
+    git-clone git:///code.quagga.net/quagga.git quagga
+    git-archive --remote=git://code.quagga.net/quagga.git \
+        --prefix=quagga-release/ master | tar -xf -
+    cd quagga-release
+
+    autoreconf -i
+    ./configure
+    make
+    make dist
+  \end{verbatim}
+\end{itemize}
+
+The tarball which `make dist' creates is the tarball to be released! The
+git-archive step ensures you're working with code corresponding to that in
+the official repository, and also carries out keyword expansion. If any
+errors occur, move tags as needed and start over from the fresh checkouts.
+Do not append to tarballs, as this has produced non-standards-conforming
+tarballs in the past.
+
+See also: \url{http://wiki.quagga.net/index.php/Main/Processes}
+
+[TODO: collation of a list of deprecated commands. Possibly can be scripted
+to extract from vtysh/vtysh\_cmd.c]
+
+
+\section{TOOL VERSIONS}
+
+Require versions of support tools are listed in INSTALL.quagga.txt.
+Required versions should only be done with due deliberation, as it can
+cause environments to no longer be able to compile quagga.
+
+
+\section{SHARED LIBRARY VERSIONING}
+\label{sec:dll-versioning}
+
+[this section is at the moment just gdt's opinion]
+
+Quagga builds several shared libaries (lib/libzebra, ospfd/libospf, 
+ospfclient/libsopfapiclient).  These may be used by external programs,
+e.g. a new routing protocol that works with the zebra daemon, or
+ospfapi clients.  The libtool info pages (node Versioning) explain
+when major and minor version numbers should be changed.  These values
+are set in Makefile.am near the definition of the library.  If you
+make a change that requires changing the shared library version,
+please update Makefile.am.
+
+libospf exports far more than it should, and is needed by ospfapi
+clients.  Only bump libospf for changes to functions for which it is
+reasonable for a user of ospfapi to call, and please err on the side
+of not bumping.
+
+There is no support intended for installing part of zebra.  The core
+library libzebra and the included daemons should always be built and
+installed together.
+
+
+\section{GIT COMMIT SUBMISSION}
+\label{sec:git-submission}
+
+The preferred method for submitting changes is to provide git commits via a
+publically-accessible git repository, which the maintainers can easily pull.
+
+The commits should be in a branch based off the Quagga.net master - a
+"feature branch".  Ideally there should be no commits to this branch other
+than those in master, and those intended to be submitted.  However, merge
+commits to this branch from the Quagga master are permitted, though strongly
+discouraged - use another (potentially local and throw-away) branch to test
+merge with the latest Quagga master.
+
+Recommended practice is to keep different logical sets of changes on
+separate branches - "topic" or "feature" branches.  This allows you to still
+merge them together to one branch (potentially local and/or "throw-away")
+for testing or use, while retaining smaller, independent branches that are
+easier to merge.
+
+All content guidelines in section \ref{sec:patch-submission}, PATCH
+SUBMISSION apply.
+
+
+\section{PATCH SUBMISSION}
+\label{sec:patch-submission}
+
+\begin{itemize}
+
+\item For complex changes, contributors are strongly encouraged to first
+      start a design discussion on the quagga-dev list \emph{before}
+      starting any coding.
+
+\item Send a clean diff against the 'master' branch of the quagga.git
+      repository, in unified diff format, preferably with the '-p' argument to
+      show C function affected by any chunk, and with the -w and -b arguments to
+      minimise changes. E.g:
+
+     git diff -up mybranch..remotes/quagga.net/master
+
+     It is preferable to use git format-patch, and even more preferred to
+     publish a git repository (see GIT COMMIT SUBMISSION, section
+     \ref{sec:git-submission}).
+
+     If not using git format-patch, Include the commit message in the email.
+
+\item After a commit, code should have comments explaining to the reviewer
+      why it is correct, without reference to history.  The commit message
+      should explain why the change is correct.
+
+\item Include NEWS entries as appropriate.
+
+\item Include only one semantic change or group of changes per patch.
+
+\item Do not make gratuitous changes to whitespace. See the w and b arguments
+      to diff.
+
+\item Changes should be arranged so that the least contraversial and most
+      trivial are first, and the most complex or more contraversial are
+      last.  This will maximise how many the Quagga maintainers can merge,
+      even if some other commits need further work.
+
+\item Providing a unit-test is strongly encouraged. Doing so will make it
+      much easier for maintainers to have confidence that they will be able
+      to support your change.
+
+\item New code should be arranged so that it easy to verify and test. E.g. 
+      stateful logic should be separated out from functional logic as much as
+      possible: wherever possible, move complex logic out to smaller helper
+      functions which access no state other than their arguments.
+
+\item State on which platforms and with what daemons the patch has been
+      tested.  Understand that if the set of testing locations is small,
+      and the patch might have unforeseen or hard to fix consequences that
+      there may be a call for testers on quagga-dev, and that the patch
+      may be blocked until test results appear.
+
+      If there are no users for a platform on quagga-dev who are able and
+      willing to verify -current occasionally, that platform may be
+      dropped from the "should be checked" list.
+
+\end{itemize}
+
+\section{PATCH APPLICATION}
+
+\begin{itemize}
+
+\item Only apply patches that meet the submission guidelines.
+
+\item If the patch might break something, issue a call for testing on the
+      mailinglist.
+
+\item Give an appropriate commit message (see above), and use the --author
+      argument to git-commit, if required, to ensure proper attribution (you
+      should still be listed as committer)
+
+\item Immediately after commiting, double-check (with git-log and/or gitk).
+      If there's a small mistake you can easily fix it with `git commit
+      --amend ..'
+
+\item When merging a branch, always use an explicit merge commit. Giving
+      --no-ff ensures a merge commit is created which documents ``this human
+      decided to merge this branch at this time''.
+\end{itemize}
+
+\section{STABLE PLATFORMS AND DAEMONS}
+
+The list of platforms that should be tested follow.  This is a list
+derived from what quagga is thought to run on and for which
+maintainers can test or there are people on quagga-dev who are able
+and willing to verify that -current does or does not work correctly.
+
+\begin{itemize}
+  \item BSD (Free, Net or Open, any platform)
+  \item GNU/Linux (any distribution, i386)
+  \item Solaris (strict alignment, any platform)
+  \item future: NetBSD/sparc64
+\end{itemize}
+
+The list of daemons that are thought to be stable and that should be
+tested are:
+
+\begin{itemize}
+  \item zebra
+  \item bgpd
+  \item ripd
+  \item ospfd
+  \item ripngd
+\end{itemize}
+Daemons which are in a testing phase are
+
+\begin{itemize}
+  \item ospf6d
+  \item isisd
+  \item watchquagga
+\end{itemize}
+
+\section{IMPORT OR UPDATE VENDOR SPECIFIC ROUTING PROTOCOLS}
+
+The source code of Quagga is based on two vendors:
+
+   \verb|zebra_org| (\url{http://www.zebra.org/})
+   \verb|isisd_sf| (\url{http://isisd.sf.net/})
+
+To import code from further sources, e.g. for archival purposes without
+necessarily having to review and/or fix some changeset, create a branch from
+`master':
+
+\begin{verbatim}
+	git checkout -b archive/foo master
+	<apply changes>
+	git commit -a "Joe Bar <joe@example.com>"
+	git push quagga archive/foo
+\end{verbatim}
+
+presuming `quagga' corresponds to a file in your .git/remotes with
+configuration for the appropriate Quagga.net repository.
+
+\end{document}
diff --git a/INSTALL.quagga.txt b/INSTALL.quagga.txt
index f627c92..ec4e799 100644
--- a/INSTALL.quagga.txt
+++ b/INSTALL.quagga.txt
@@ -1,5 +1,3 @@
-# $Id$
-
 --------------------------------------------------------------------------
 Building and Installing Quagga from releases or snapshots:
 
diff --git a/Makefile.am b/Makefile.am
index 007758f..19a9022 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,10 +1,10 @@
 ## Process this file with automake to produce Makefile.in.
 
-SUBDIRS = lib @ZEBRA@ @BGPD@ @RIPD@ @RIPNGD@ @OSPFD@ @OSPF6D@ \
+SUBDIRS = lib @ZEBRA@ @BGPD@ @RIPD@ @RIPNGD@ @OSPFD@ @OSPF6D@ @BABELD@ \
          @ISISD@ @WATCHQUAGGA@ @VTYSH@ @OSPFCLIENT@ @DOC@ m4 @pkgsrcdir@ \
          redhat @SOLARIS@
 
-DIST_SUBDIRS = lib zebra bgpd ripd ripngd ospfd ospf6d \
+DIST_SUBDIRS = lib zebra bgpd ripd ripngd ospfd ospf6d babeld \
 	  isisd watchquagga vtysh ospfclient doc m4 pkgsrc redhat tests \
 	  solaris
 
@@ -14,4 +14,14 @@
 	tools/mrlg.cgi tools/rrcheck.pl tools/rrlookup.pl tools/zc.pl \
 	tools/zebra.el tools/multiple-bgpd.sh
 
+if HAVE_LATEX
+
+HACKING.pdf: HACKING.tex
+	$(LATEXMK) -pdf $<
+
+clean-local:
+	-$(LATEXMK) -C HACKING.tex
+
+endif
+
 ACLOCAL_AMFLAGS = -I m4
diff --git a/babeld/.gitignore b/babeld/.gitignore
new file mode 100644
index 0000000..8384763
--- /dev/null
+++ b/babeld/.gitignore
@@ -0,0 +1,7 @@
+*
+!*.c
+!*.h
+!LICENCE
+!Makefile.am
+!babeld.conf.sample
+!.gitignore
\ No newline at end of file
diff --git a/babeld/LICENCE b/babeld/LICENCE
new file mode 100644
index 0000000..9da569d
--- /dev/null
+++ b/babeld/LICENCE
@@ -0,0 +1,36 @@
+Code in this directory is made available under the following licence:
+
+  ---------------------------------------------------------------------------
+  Copyright (c) 2007, 2008 by Juliusz Chroboczek
+  
+  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
+  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+  THE SOFTWARE.
+  ---------------------------------------------------------------------------
+
+The code also makes calls to and links with the "libzebra" code of Quagga,
+in the lib/ directory of this project, which is subject to the GPL licence
+as given in the top-level COPYING file included with Quagga.
+
+Contributors to the code in babeld/ are asked to make their work available
+under the same MIT/X11 licence as given immediately above.  Please indicate
+your assent to this by updating this file and appending the appropriate
+
+  Copyright <year> <Author name>, <author contact details>
+
+line to the existing copyright assertion lines in the MIT/X11 licence text
+above in this file.
diff --git a/babeld/Makefile.am b/babeld/Makefile.am
new file mode 100644
index 0000000..468b5a5
--- /dev/null
+++ b/babeld/Makefile.am
@@ -0,0 +1,29 @@
+## Process this file with automake to produce Makefile.in.
+
+INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib @SNMP_INCLUDES@
+DEFS = @DEFS@ -DSYSCONFDIR=\"$(sysconfdir)/\"
+INSTALL_SDATA=@INSTALL@ -m 600
+
+AM_CFLAGS = $(PICFLAGS)
+AM_LDFLAGS = $(PILDFLAGS)
+
+noinst_LIBRARIES = libbabel.a
+sbin_PROGRAMS = babeld
+
+libbabel_a_SOURCES = \
+	babel_zebra.c net.c kernel.c util.c source.c neighbour.c	\
+	route.c xroute.c message.c resend.c babel_interface.c babeld.c	\
+	babel_filter.c
+
+noinst_HEADERS = \
+	babel_zebra.h net.h kernel.h util.h source.h neighbour.h	\
+	route.h xroute.h message.h resend.h babel_interface.h babeld.h	\
+	babel_filter.h
+
+babeld_SOURCES = \
+	babel_main.c $(libbabel_a_SOURCES)
+
+babeld_LDADD = ../lib/libzebra.la @LIBCAP@
+
+examplesdir = $(exampledir)
+dist_examples_DATA = babeld.conf.sample
diff --git a/babeld/babel_filter.c b/babeld/babel_filter.c
new file mode 100644
index 0000000..191a9f7
--- /dev/null
+++ b/babeld/babel_filter.c
@@ -0,0 +1,124 @@
+/*  
+ *  This file is free software: you may copy, redistribute 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.  
+ *  
+ *  This file is distributed in the hope that it will be useful, but  
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+ *  General Public License for more details.  
+ *  
+ *  You should have received a copy of the GNU General Public License  
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ *  
+ * This file incorporates work covered by the following copyright and  
+ * permission notice:  
+ *  
+
+Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
+
+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
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#include "babel_filter.h"
+#include "vty.h"
+#include "filter.h"
+#include "log.h"
+#include "plist.h"
+#include "distribute.h"
+#include "util.h"
+
+int
+babel_filter(int output, const unsigned char *prefix, unsigned short plen,
+             unsigned int ifindex)
+{
+    struct interface *ifp = if_lookup_by_index(ifindex);
+    babel_interface_nfo *babel_ifp = ifp ? babel_get_if_nfo(ifp) : NULL;
+    struct prefix p;
+    struct distribute *dist;
+    struct access_list *alist;
+    struct prefix_list *plist;
+    int filter = output ? BABEL_FILTER_OUT : BABEL_FILTER_IN;
+    int distribute = output ? DISTRIBUTE_OUT : DISTRIBUTE_IN;
+
+    p.family = v4mapped(prefix) ? AF_INET : AF_INET6;
+    p.prefixlen = v4mapped(prefix) ? plen - 96 : plen;
+    if (p.family == AF_INET)
+        uchar_to_inaddr(&p.u.prefix4, prefix);
+    else
+        uchar_to_in6addr(&p.u.prefix6, prefix);
+
+    if (babel_ifp != NULL && babel_ifp->list[filter]) {
+        if (access_list_apply (babel_ifp->list[filter], &p)
+            == FILTER_DENY) {
+            debugf(BABEL_DEBUG_FILTER,
+                   "%s/%d filtered by distribute in",
+                   p.family == AF_INET ?
+                   inet_ntoa(p.u.prefix4) :
+                   inet6_ntoa (p.u.prefix6),
+                   p.prefixlen);
+            return INFINITY;
+	}
+    }
+    if (babel_ifp != NULL && babel_ifp->prefix[filter]) {
+        if (prefix_list_apply (babel_ifp->prefix[filter], &p)
+            == PREFIX_DENY) {
+            debugf(BABEL_DEBUG_FILTER, "%s/%d filtered by distribute in",
+                        p.family == AF_INET ?
+                        inet_ntoa(p.u.prefix4) :
+                        inet6_ntoa (p.u.prefix6),
+                        p.prefixlen);
+            return INFINITY;
+	}
+    }
+
+    /* All interface filter check. */
+    dist = distribute_lookup (NULL);
+    if (dist) {
+        if (dist->list[distribute]) {
+            alist = access_list_lookup (AFI_IP6, dist->list[distribute]);
+
+            if (alist) {
+                if (access_list_apply (alist, &p) == FILTER_DENY) {
+                    debugf(BABEL_DEBUG_FILTER, "%s/%d filtered by distribute in",
+                                p.family == AF_INET ?
+                                inet_ntoa(p.u.prefix4) :
+                                inet6_ntoa (p.u.prefix6),
+                                p.prefixlen);
+                    return INFINITY;
+		}
+	    }
+	}
+        if (dist->prefix[distribute]) {
+            plist = prefix_list_lookup (AFI_IP6, dist->prefix[distribute]);
+            if (plist) {
+                if (prefix_list_apply (plist, &p) == PREFIX_DENY) {
+                    debugf(BABEL_DEBUG_FILTER, "%s/%d filtered by distribute in",
+                                p.family == AF_INET ?
+                                inet_ntoa(p.u.prefix4) :
+                                inet6_ntoa (p.u.prefix6),
+                                p.prefixlen);
+                    return INFINITY;
+		}
+	    }
+	}
+    }
+    return 0;
+}
diff --git a/babeld/babel_filter.h b/babeld/babel_filter.h
new file mode 100644
index 0000000..73722e0
--- /dev/null
+++ b/babeld/babel_filter.h
@@ -0,0 +1,49 @@
+/*  
+ *  This file is free software: you may copy, redistribute 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.  
+ *  
+ *  This file is distributed in the hope that it will be useful, but  
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+ *  General Public License for more details.  
+ *  
+ *  You should have received a copy of the GNU General Public License  
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ *  
+ * This file incorporates work covered by the following copyright and  
+ * permission notice:  
+ *  
+Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
+
+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
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#ifndef BABELD_BABEL_FILTER_H
+#define BABELD_BABEL_FILTER_H
+
+#include <zebra.h>
+#include "prefix.h"
+#include "babel_interface.h"
+
+int babel_filter(int output, const unsigned char *prefix, unsigned short plen,
+                 unsigned int index);
+
+#endif /* BABELD_BABEL_FILTER_H */
diff --git a/babeld/babel_interface.c b/babeld/babel_interface.c
new file mode 100644
index 0000000..ace2812
--- /dev/null
+++ b/babeld/babel_interface.c
@@ -0,0 +1,1022 @@
+/*  
+ *  This file is free software: you may copy, redistribute 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.  
+ *  
+ *  This file is distributed in the hope that it will be useful, but  
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+ *  General Public License for more details.  
+ *  
+ *  You should have received a copy of the GNU General Public License  
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ *  
+ * This file incorporates work covered by the following copyright and  
+ * permission notice:  
+ *  
+
+Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
+
+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
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#include <zebra.h>
+#include "memory.h"
+#include "log.h"
+#include "command.h"
+#include "prefix.h"
+#include "vector.h"
+#include "distribute.h"
+
+#include "babel_main.h"
+#include "util.h"
+#include "kernel.h"
+#include "babel_interface.h"
+#include "message.h"
+#include "route.h"
+#include "babel_zebra.h"
+#include "neighbour.h"
+#include "route.h"
+#include "xroute.h"
+
+
+#define IS_ENABLE(ifp) (babel_enable_if_lookup(ifp->name) >= 0)
+
+static int babel_enable_if_lookup (const char *ifname);
+static int babel_enable_if_add (const char *ifname);
+static int babel_enable_if_delete (const char *ifname);
+static int interface_recalculate(struct interface *ifp);
+static int interface_reset(struct interface *ifp);
+static int babel_if_new_hook    (struct interface *ifp);
+static int babel_if_delete_hook (struct interface *ifp);
+static int interface_config_write (struct vty *vty);
+static babel_interface_nfo * babel_interface_allocate (void);
+static void babel_interface_free (babel_interface_nfo *bi);
+
+
+static vector babel_enable_if;                 /* enable interfaces (by cmd). */
+static struct cmd_node babel_interface_node =  /* babeld's interface node.    */
+{
+    INTERFACE_NODE,
+    "%s(config-if)# ",
+    1 /* VTYSH */
+};
+
+
+int
+babel_interface_up (int cmd, struct zclient *client, zebra_size_t length)
+{
+    struct stream *s = NULL;
+    struct interface *ifp = NULL;
+
+    debugf(BABEL_DEBUG_IF, "receive a 'interface up'");
+
+    s = zclient->ibuf;
+    ifp = zebra_interface_state_read(s); /* it updates iflist */
+
+    if (ifp == NULL) {
+        return 0;
+    }
+
+    interface_recalculate(ifp);
+    return 0;
+}
+
+int
+babel_interface_down (int cmd, struct zclient *client, zebra_size_t length)
+{
+    struct stream *s = NULL;
+    struct interface *ifp = NULL;
+
+    debugf(BABEL_DEBUG_IF, "receive a 'interface down'");
+
+    s = zclient->ibuf;
+    ifp = zebra_interface_state_read(s); /* it updates iflist */
+
+    if (ifp == NULL) {
+        return 0;
+    }
+
+    interface_reset(ifp);
+    return 0;
+}
+
+int
+babel_interface_add (int cmd, struct zclient *client, zebra_size_t length)
+{
+    struct interface *ifp = NULL;
+
+    debugf(BABEL_DEBUG_IF, "receive a 'interface add'");
+
+    /* read and add the interface in the iflist. */
+    ifp = zebra_interface_add_read (zclient->ibuf);
+
+    if (ifp == NULL) {
+        return 0;
+    }
+
+    interface_recalculate(ifp);
+    return 0;
+}
+
+int
+babel_interface_delete (int cmd, struct zclient *client, zebra_size_t length)
+{
+    struct interface *ifp;
+    struct stream *s;
+
+    debugf(BABEL_DEBUG_IF, "receive a 'interface delete'");
+
+    s = zclient->ibuf;
+    ifp = zebra_interface_state_read(s); /* it updates iflist */
+
+    if (ifp == NULL)
+        return 0;
+
+    if (IS_ENABLE(ifp))
+        interface_reset(ifp);
+
+    /* To support pseudo interface do not free interface structure.  */
+    /* if_delete(ifp); */
+    ifp->ifindex = IFINDEX_INTERNAL;
+
+    return 0;
+}
+
+int
+babel_interface_address_add (int cmd, struct zclient *client,
+                             zebra_size_t length)
+{
+    babel_interface_nfo *babel_ifp;
+    struct connected *ifc;
+    struct prefix *prefix;
+
+    debugf(BABEL_DEBUG_IF, "receive a 'interface address add'");
+
+    ifc = zebra_interface_address_read (ZEBRA_INTERFACE_ADDRESS_ADD,
+                                        zclient->ibuf);
+
+    if (ifc == NULL)
+        return 0;
+
+    prefix = ifc->address;
+
+    if (prefix->family == AF_INET) {
+        flush_interface_routes(ifc->ifp, 0);
+        babel_ifp = babel_get_if_nfo(ifc->ifp);
+        if (babel_ifp->ipv4 == NULL) {
+            babel_ifp->ipv4 = malloc(4);
+            if (babel_ifp->ipv4 == NULL) {
+                zlog_err("not einough memory");
+            } else {
+                memcpy(babel_ifp->ipv4, &prefix->u.prefix4, 4);
+            }
+        }
+    }
+
+    send_request(ifc->ifp, NULL, 0);
+    send_update(ifc->ifp, 0, NULL, 0);
+
+    return 0;
+}
+
+int
+babel_interface_address_delete (int cmd, struct zclient *client,
+                                zebra_size_t length)
+{
+    babel_interface_nfo *babel_ifp;
+    struct connected *ifc;
+    struct prefix *prefix;
+
+    debugf(BABEL_DEBUG_IF, "receive a 'interface address add'");
+
+    ifc = zebra_interface_address_read (ZEBRA_INTERFACE_ADDRESS_ADD,
+                                        zclient->ibuf);
+
+    if (ifc == NULL)
+        return 0;
+
+    prefix = ifc->address;
+
+    if (prefix->family == AF_INET) {
+        flush_interface_routes(ifc->ifp, 0);
+        babel_ifp = babel_get_if_nfo(ifc->ifp);
+        if (babel_ifp->ipv4 != NULL
+            && memcmp(babel_ifp->ipv4, &prefix->u.prefix4, 4) == 0) {
+            free(babel_ifp->ipv4);
+            babel_ifp->ipv4 = NULL;
+        }
+    }
+
+    send_request(ifc->ifp, NULL, 0);
+    send_update(ifc->ifp, 0, NULL, 0);
+
+    return 0;
+}
+
+/* Lookup function. */
+static int
+babel_enable_if_lookup (const char *ifname)
+{
+    unsigned int i;
+    char *str;
+
+    for (i = 0; i < vector_active (babel_enable_if); i++)
+        if ((str = vector_slot (babel_enable_if, i)) != NULL)
+            if (strcmp (str, ifname) == 0)
+                return i;
+    return -1;
+}
+
+/* Add interface to babel_enable_if. */
+static int
+babel_enable_if_add (const char *ifname)
+{
+    int ret;
+    struct interface *ifp = NULL;
+
+    ret = babel_enable_if_lookup (ifname);
+    if (ret >= 0)
+        return -1;
+
+    vector_set (babel_enable_if, strdup (ifname));
+
+    ifp = if_lookup_by_name(ifname);
+    if (ifp != NULL)
+        interface_recalculate(ifp);
+
+    return 1;
+}
+
+/* Delete interface from babel_enable_if. */
+static int
+babel_enable_if_delete (const char *ifname)
+{
+    int babel_enable_if_index;
+    char *str;
+    struct interface *ifp = NULL;
+
+    babel_enable_if_index = babel_enable_if_lookup (ifname);
+    if (babel_enable_if_index < 0)
+        return -1;
+
+    str = vector_slot (babel_enable_if, babel_enable_if_index);
+    free (str);
+    vector_unset (babel_enable_if, babel_enable_if_index);
+
+    ifp = if_lookup_by_name(ifname);
+    if (ifp != NULL)
+        interface_reset(ifp);
+
+    return 1;
+}
+
+/* [Babel Command] Babel enable on specified interface or matched network. */
+DEFUN (babel_network,
+       babel_network_cmd,
+       "network IF_OR_ADDR",
+       "Enable Babel protocol on specified interface or network.\n"
+       "Interface or address")
+{
+    int ret;
+    struct prefix p;
+
+    ret = str2prefix (argv[0], &p);
+
+    /* Given string is:               */
+    if (ret) /* an IPv4 or v6 network */
+        return CMD_ERR_NO_MATCH; /* not implemented yet */
+    else     /* an interface name     */
+        ret = babel_enable_if_add (argv[0]);
+
+    if (ret < 0) {
+        vty_out (vty, "There is same network configuration %s%s", argv[0],
+                 VTY_NEWLINE);
+        return CMD_WARNING;
+    }
+
+    return CMD_SUCCESS;
+}
+
+/* [Babel Command] Babel enable on specified interface or matched network. */
+DEFUN (no_babel_network,
+       no_babel_network_cmd,
+       "no network IF_OR_ADDR",
+       NO_STR
+       "Disable Babel protocol on specified interface or network.\n"
+       "Interface or address")
+{
+    int ret;
+    struct prefix p;
+
+    ret = str2prefix (argv[0], &p);
+
+    /* Given string is:               */
+    if (ret) /* an IPv4 or v6 network */
+        return CMD_ERR_NO_MATCH; /* not implemented yet */
+    else     /* an interface name     */
+        ret = babel_enable_if_delete (argv[0]);
+
+    if (ret < 0) {
+        vty_out (vty, "can't find network %s%s", argv[0],
+                 VTY_NEWLINE);
+        return CMD_WARNING;
+    }
+
+    return CMD_SUCCESS;
+}
+
+/* There are a number of interface parameters that must be changed when
+   an interface becomes wired/wireless.  In Quagga, they cannot be
+   configured separately. */
+
+static void
+babel_set_wired_internal(babel_interface_nfo *babel_ifp, int wired)
+{
+    if(wired) {
+        babel_ifp->flags |= BABEL_IF_WIRED;
+        babel_ifp->cost = 96;
+        babel_ifp->flags &= ~BABEL_IF_LQ;
+    } else {
+        babel_ifp->flags &= ~BABEL_IF_WIRED;
+        babel_ifp->cost = 256;
+        babel_ifp->flags |= BABEL_IF_LQ;
+    }
+
+}
+
+/* [Interface Command] Tell the interface is wire. */
+DEFUN (babel_set_wired,
+       babel_set_wired_cmd,
+       "babel wired",
+       "Babel interface commands\n"
+       "Enable wired optimisations")
+{
+    struct interface *ifp;
+    babel_interface_nfo *babel_ifp;
+
+    ifp = vty->index;
+    babel_ifp = babel_get_if_nfo(ifp);
+
+    assert (babel_ifp != NULL);
+    babel_set_wired_internal(babel_ifp, 1);
+    return CMD_SUCCESS;
+}
+
+/* [Interface Command] Tell the interface is wireless (default). */
+DEFUN (babel_set_wireless,
+       babel_set_wireless_cmd,
+       "babel wireless",
+       "Babel interface commands\n"
+       "Disable wired optimiations (assume wireless)")
+{
+    struct interface *ifp;
+    babel_interface_nfo *babel_ifp;
+
+    ifp = vty->index;
+    babel_ifp = babel_get_if_nfo(ifp);
+
+    assert (babel_ifp != NULL);
+    babel_set_wired_internal(babel_ifp, 0);
+    return CMD_SUCCESS;
+}
+
+/* [Interface Command] Enable split horizon. */
+DEFUN (babel_split_horizon,
+       babel_split_horizon_cmd,
+       "babel split-horizon",
+       "Babel interface commands\n"
+       "Enable split horizon processing")
+{
+    struct interface *ifp;
+    babel_interface_nfo *babel_ifp;
+
+    ifp = vty->index;
+    babel_ifp = babel_get_if_nfo(ifp);
+
+    assert (babel_ifp != NULL);
+    babel_ifp->flags |= BABEL_IF_SPLIT_HORIZON;
+    return CMD_SUCCESS;
+}
+
+/* [Interface Command] Disable split horizon (default). */
+DEFUN (no_babel_split_horizon,
+       no_babel_split_horizon_cmd,
+       "no babel split-horizon",
+       NO_STR
+       "Babel interface commands\n"
+       "Disable split horizon processing")
+{
+    struct interface *ifp;
+    babel_interface_nfo *babel_ifp;
+
+    ifp = vty->index;
+    babel_ifp = babel_get_if_nfo(ifp);
+
+    assert (babel_ifp != NULL);
+    babel_ifp->flags &= ~BABEL_IF_SPLIT_HORIZON;
+    return CMD_SUCCESS;
+}
+
+/* [Interface Command]. */
+DEFUN (babel_set_hello_interval,
+       babel_set_hello_interval_cmd,
+       "babel hello-interval <20-655340>",
+       "Babel interface commands\n"
+       "Time between scheduled hellos\n"
+       "Milliseconds\n")
+{
+    struct interface *ifp;
+    babel_interface_nfo *babel_ifp;
+    int interval;
+
+    VTY_GET_INTEGER_RANGE("milliseconds", interval, argv[0], 20, 10 * 0xFFFE);
+
+    ifp = vty->index;
+    babel_ifp = babel_get_if_nfo(ifp);
+    assert (babel_ifp != NULL);
+
+    babel_ifp->hello_interval = interval;
+    return CMD_SUCCESS;
+}
+
+/* [Interface Command]. */
+DEFUN (babel_set_update_interval,
+       babel_set_update_interval_cmd,
+       "babel update-interval <20-655340>",
+       "Babel interface commands\n"
+       "Time between scheduled updates\n"
+       "Milliseconds\n")
+{
+    struct interface *ifp;
+    babel_interface_nfo *babel_ifp;
+    int interval;
+
+    VTY_GET_INTEGER_RANGE("milliseconds", interval, argv[0], 20, 10 * 0xFFFE);
+
+    ifp = vty->index;
+    babel_ifp = babel_get_if_nfo(ifp);
+    assert (babel_ifp != NULL);
+
+    babel_ifp->update_interval = interval;
+    return CMD_SUCCESS;
+}
+
+/* This should be no more than half the hello interval, so that hellos
+   aren't sent late.  The result is in milliseconds. */
+unsigned
+jitter(babel_interface_nfo *babel_ifp, int urgent)
+{
+    unsigned interval = babel_ifp->hello_interval;
+    if(urgent)
+        interval = MIN(interval, 100);
+    else
+        interval = MIN(interval, 4000);
+    return roughly(interval) / 4;
+}
+
+unsigned
+update_jitter(babel_interface_nfo *babel_ifp, int urgent)
+{
+    unsigned interval = babel_ifp->hello_interval;
+    if(urgent)
+        interval = MIN(interval, 100);
+    else
+        interval = MIN(interval, 4000);
+    return roughly(interval);
+}
+
+/* calculate babeld's specific datas of an interface (change when the interface
+ change) */
+static int
+interface_recalculate(struct interface *ifp)
+{
+    babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp);
+    unsigned char *tmp = NULL;
+    int mtu, rc;
+    struct ipv6_mreq mreq;
+
+    if (!IS_ENABLE(ifp))
+        return -1;
+
+    if (!if_is_operative(ifp) || !CHECK_FLAG(ifp->flags, IFF_RUNNING)) {
+        interface_reset(ifp);
+        return -1;
+    }
+
+    babel_ifp->flags |= BABEL_IF_IS_UP;
+
+    mtu = MIN(ifp->mtu, ifp->mtu6);
+
+    /* We need to be able to fit at least two messages into a packet,
+     so MTUs below 116 require lower layer fragmentation. */
+    /* In IPv6, the minimum MTU is 1280, and every host must be able
+     to reassemble up to 1500 bytes, but I'd rather not rely on this. */
+    if(mtu < 128) {
+        debugf(BABEL_DEBUG_IF, "Suspiciously low MTU %d on interface %s (%d).",
+               mtu, ifp->name, ifp->ifindex);
+        mtu = 128;
+    }
+
+    /* 40 for IPv6 header, 8 for UDP header, 12 for good luck. */
+    babel_ifp->bufsize = mtu - sizeof(packet_header) - 60;
+    tmp = babel_ifp->sendbuf;
+    babel_ifp->sendbuf = realloc(babel_ifp->sendbuf, babel_ifp->bufsize);
+    if(babel_ifp->sendbuf == NULL) {
+        zlog_err("Couldn't reallocate sendbuf.");
+        free(tmp);
+        babel_ifp->bufsize = 0;
+        return -1;
+    }
+    tmp = NULL;
+
+    resize_receive_buffer(mtu);
+
+    memset(&mreq, 0, sizeof(mreq));
+    memcpy(&mreq.ipv6mr_multiaddr, protocol_group, 16);
+    mreq.ipv6mr_interface = ifp->ifindex;
+
+    rc = setsockopt(protocol_socket, IPPROTO_IPV6, IPV6_JOIN_GROUP,
+                    (char*)&mreq, sizeof(mreq));
+    if(rc < 0) {
+        zlog_err("setsockopt(IPV6_JOIN_GROUP) on interface '%s': %s",
+                 ifp->name, safe_strerror(errno));
+        /* This is probably due to a missing link-local address,
+         so down this interface, and wait until the main loop
+         tries to up it again. */
+        interface_reset(ifp);
+        return -1;
+    }
+
+    set_timeout(&babel_ifp->hello_timeout, babel_ifp->hello_interval);
+    set_timeout(&babel_ifp->update_timeout, babel_ifp->update_interval);
+    send_hello(ifp);
+    send_request(ifp, NULL, 0);
+
+    update_interface_metric(ifp);
+
+    debugf(BABEL_DEBUG_COMMON,
+           "Upped interface %s (%s, cost=%d, channel=%d%s).",
+           ifp->name,
+           (babel_ifp->flags & BABEL_IF_WIRED) ? "wired" : "wireless",
+           babel_ifp->cost,
+           babel_ifp->channel,
+           babel_ifp->ipv4 ? ", IPv4" : "");
+
+    if(rc > 0)
+        send_update(ifp, 0, NULL, 0);
+
+    return 1;
+}
+
+/* Reset the interface as it was new: it's not removed from the interface list,
+ and may be considered as a upped interface. */
+static int
+interface_reset(struct interface *ifp)
+{
+    int rc;
+    struct ipv6_mreq mreq;
+    babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp);
+
+    if (!(babel_ifp->flags & BABEL_IF_IS_UP))
+        return 0;
+
+    debugf(BABEL_DEBUG_IF, "interface reset: %s", ifp->name);
+    babel_ifp->flags &= ~BABEL_IF_IS_UP;
+
+    flush_interface_routes(ifp, 0);
+    babel_ifp->buffered = 0;
+    babel_ifp->bufsize = 0;
+    free(babel_ifp->sendbuf);
+    babel_ifp->num_buffered_updates = 0;
+    babel_ifp->update_bufsize = 0;
+    if(babel_ifp->buffered_updates)
+        free(babel_ifp->buffered_updates);
+    babel_ifp->buffered_updates = NULL;
+    babel_ifp->sendbuf = NULL;
+
+    if(ifp->ifindex > 0) {
+        memset(&mreq, 0, sizeof(mreq));
+        memcpy(&mreq.ipv6mr_multiaddr, protocol_group, 16);
+        mreq.ipv6mr_interface = ifp->ifindex;
+        rc = setsockopt(protocol_socket, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
+                        (char*)&mreq, sizeof(mreq));
+        if(rc < 0)
+            zlog_err("setsockopt(IPV6_LEAVE_GROUP) on interface '%s': %s",
+                     ifp->name, safe_strerror(errno));
+    }
+
+    update_interface_metric(ifp);
+
+    debugf(BABEL_DEBUG_COMMON,"Upped network %s (%s, cost=%d%s).",
+           ifp->name,
+           (babel_ifp->flags & BABEL_IF_WIRED) ? "wired" : "wireless",
+           babel_ifp->cost,
+           babel_ifp->ipv4 ? ", IPv4" : "");
+
+    return 1;
+}
+
+/* Send retraction to all, and reset all interfaces statistics. */
+void
+babel_interface_close_all(void)
+{
+    struct interface *ifp = NULL;
+    struct listnode *linklist_node = NULL;
+
+    FOR_ALL_INTERFACES(ifp, linklist_node) {
+        if(!if_up(ifp))
+            continue;
+        send_wildcard_retraction(ifp);
+        /* Make sure that we expire quickly from our neighbours'
+         association caches. */
+        send_hello_noupdate(ifp, 10);
+        flushbuf(ifp);
+        usleep(roughly(1000));
+        gettime(&babel_now);
+    }
+    FOR_ALL_INTERFACES(ifp, linklist_node) {
+        if(!if_up(ifp))
+            continue;
+        /* Make sure they got it. */
+        send_wildcard_retraction(ifp);
+        send_hello_noupdate(ifp, 1);
+        flushbuf(ifp);
+        usleep(roughly(10000));
+        gettime(&babel_now);
+        interface_reset(ifp);
+    }
+}
+
+/* return "true" if address is one of our ipv6 addresses */
+int
+is_interface_ll_address(struct interface *ifp, const unsigned char *address)
+{
+    struct connected *connected;
+    struct listnode *node;
+
+    if(!if_up(ifp))
+        return 0;
+
+    FOR_ALL_INTERFACES_ADDRESSES(ifp, connected, node) {
+        if(connected->address->family == AF_INET6 &&
+           memcmp(&connected->address->u.prefix6, address, 16) == 0)
+            return 1;
+    }
+
+    return 0;
+}
+
+static void
+show_babel_interface_sub (struct vty *vty, struct interface *ifp)
+{
+  int is_up;
+  babel_interface_nfo *babel_ifp;
+
+  vty_out (vty, "%s is %s%s", ifp->name,
+    ((is_up = if_is_operative(ifp)) ? "up" : "down"), VTY_NEWLINE);
+  vty_out (vty, "  ifindex %u, MTU %u bytes %s%s",
+    ifp->ifindex, ifp->mtu, if_flag_dump(ifp->flags), VTY_NEWLINE);
+
+  if (babel_enable_if_lookup (ifp->name) < 0)
+  {
+    vty_out (vty, "  Babel protocol is not enabled on this interface%s", VTY_NEWLINE);
+    return;
+  }
+  if (!is_up)
+  {
+    vty_out (vty, "  Babel protocol is enabled, but not running on this interface%s", VTY_NEWLINE);
+    return;
+  }
+  babel_ifp = babel_get_if_nfo (ifp);
+  vty_out (vty, "  Babel protocol is running on this interface%s", VTY_NEWLINE);
+  vty_out (vty, "  Operating mode is \"%s\"%s",
+           CHECK_FLAG (babel_ifp->flags, BABEL_IF_WIRED) ? "wired" : "wireless", VTY_NEWLINE);
+  vty_out (vty, "  Split horizon mode is %s%s",
+           CHECK_FLAG (babel_ifp->flags, BABEL_IF_SPLIT_HORIZON) ? "On" : "Off", VTY_NEWLINE);
+  vty_out (vty, "  Hello interval is %u ms%s", babel_ifp->hello_interval, VTY_NEWLINE);
+  vty_out (vty, "  Update interval is %u ms%s", babel_ifp->update_interval, VTY_NEWLINE);
+}
+
+DEFUN (show_babel_interface,
+       show_babel_interface_cmd,
+       "show babel interface [INTERFACE]",
+       SHOW_STR
+       IP_STR
+       "Babel information\n"
+       "Interface information\n"
+       "Interface name\n")
+{
+  struct interface *ifp;
+  struct listnode *node;
+
+  if (argc == 0)
+  {
+    for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
+      show_babel_interface_sub (vty, ifp);
+    return CMD_SUCCESS;
+  }
+  if ((ifp = if_lookup_by_name (argv[0])) == NULL)
+  {
+    vty_out (vty, "No such interface name%s", VTY_NEWLINE);
+    return CMD_WARNING;
+  }
+  show_babel_interface_sub (vty, ifp);
+  return CMD_SUCCESS;
+}
+
+static void
+show_babel_neighbour_sub (struct vty *vty, struct neighbour *neigh)
+{
+    vty_out (vty,
+             "Neighbour %s dev %s reach %04x rxcost %d txcost %d %s.%s",
+             format_address(neigh->address),
+             neigh->ifp->name,
+             neigh->reach,
+             neighbour_rxcost(neigh),
+             neigh->txcost,
+             if_up(neigh->ifp) ? "" : " (down)",
+             VTY_NEWLINE);
+}
+
+DEFUN (show_babel_neighbour,
+       show_babel_neighbour_cmd,
+       "show babel neighbour [INTERFACE]",
+       SHOW_STR
+       IP_STR
+       "Babel information\n"
+       "Print neighbours\n"
+       "Interface name\n")
+{
+    struct neighbour *neigh;
+    struct interface *ifp;
+
+    if (argc == 0) {
+        FOR_ALL_NEIGHBOURS(neigh) {
+            show_babel_neighbour_sub(vty, neigh);
+        }
+        return CMD_SUCCESS;
+    }
+    if ((ifp = if_lookup_by_name (argv[0])) == NULL)
+    {
+        vty_out (vty, "No such interface name%s", VTY_NEWLINE);
+        return CMD_WARNING;
+    }
+    FOR_ALL_NEIGHBOURS(neigh) {
+        if(ifp->ifindex == neigh->ifp->ifindex) {
+            show_babel_neighbour_sub(vty, neigh);
+        }
+    }
+    return CMD_SUCCESS;
+}
+
+static void
+show_babel_routes_sub (struct babel_route *route, void *closure)
+{
+    struct vty *vty = (struct vty*) closure;
+    const unsigned char *nexthop =
+        memcmp(route->nexthop, route->neigh->address, 16) == 0 ?
+        NULL : route->nexthop;
+    char channels[100];
+
+    if(route->channels[0] == 0)
+        channels[0] = '\0';
+    else {
+        int k, j = 0;
+        snprintf(channels, 100, " chan (");
+        j = strlen(channels);
+        for(k = 0; k < DIVERSITY_HOPS; k++) {
+            if(route->channels[k] == 0)
+                break;
+            if(k > 0)
+                channels[j++] = ',';
+            snprintf(channels + j, 100 - j, "%d", route->channels[k]);
+            j = strlen(channels);
+        }
+        snprintf(channels + j, 100 - j, ")");
+        if(k == 0)
+            channels[0] = '\0';
+    }
+
+    vty_out(vty,
+            "%s metric %d refmetric %d id %s seqno %d%s age %d "
+            "via %s neigh %s%s%s%s%s",
+            format_prefix(route->src->prefix, route->src->plen),
+            route_metric(route), route->refmetric,
+            format_eui64(route->src->id),
+            (int)route->seqno,
+            channels,
+            (int)(babel_now.tv_sec - route->time),
+            route->neigh->ifp->name,
+            format_address(route->neigh->address),
+            nexthop ? " nexthop " : "",
+            nexthop ? format_address(nexthop) : "",
+            route->installed ? " (installed)" :
+            route_feasible(route) ? " (feasible)" : "",
+            VTY_NEWLINE);
+}
+
+static void
+show_babel_xroutes_sub (struct xroute *xroute, void *closure)
+{
+    struct vty *vty = (struct vty *) closure;
+    vty_out(vty, "%s metric %d (exported)%s",
+            format_prefix(xroute->prefix, xroute->plen),
+            xroute->metric,
+            VTY_NEWLINE);
+}
+
+DEFUN (show_babel_database,
+       show_babel_database_cmd,
+       "show babel database",
+       SHOW_STR
+       IP_STR
+       "Babel information\n"
+       "Database information\n"
+       "No attributes\n")
+{
+    for_all_routes(show_babel_routes_sub, vty);
+    for_all_xroutes(show_babel_xroutes_sub, vty);
+    return CMD_SUCCESS;
+}
+
+DEFUN (show_babel_parameters,
+       show_babel_parameters_cmd,
+       "show babel parameters",
+       SHOW_STR
+       IP_STR
+       "Babel information\n"
+       "Configuration information\n"
+       "No attributes\n")
+{
+    vty_out(vty, "    -- Babel running configuration --%s", VTY_NEWLINE);
+    show_babel_main_configuration(vty);
+    vty_out(vty, "    -- distribution lists --%s", VTY_NEWLINE);
+    config_show_distribute(vty);
+
+    return CMD_SUCCESS;
+}
+
+void
+babel_if_init ()
+{
+    /* initialize interface list */
+    if_init();
+    if_add_hook (IF_NEW_HOOK,    babel_if_new_hook);
+    if_add_hook (IF_DELETE_HOOK, babel_if_delete_hook);
+
+    babel_enable_if = vector_init (1);
+
+    /* install interface node and commands */
+    install_element (CONFIG_NODE, &interface_cmd);
+    install_element (CONFIG_NODE, &no_interface_cmd);
+    install_node (&babel_interface_node, interface_config_write);
+    install_default(INTERFACE_NODE);
+    install_element(INTERFACE_NODE, &interface_cmd);
+    install_element(INTERFACE_NODE, &no_interface_cmd);
+
+    install_element(BABEL_NODE, &babel_network_cmd);
+    install_element(BABEL_NODE, &no_babel_network_cmd);
+    install_element(INTERFACE_NODE, &babel_split_horizon_cmd);
+    install_element(INTERFACE_NODE, &no_babel_split_horizon_cmd);
+    install_element(INTERFACE_NODE, &babel_set_wired_cmd);
+    install_element(INTERFACE_NODE, &babel_set_wireless_cmd);
+    install_element(INTERFACE_NODE, &babel_set_hello_interval_cmd);
+    install_element(INTERFACE_NODE, &babel_set_update_interval_cmd);
+
+    /* "show babel ..." commands */
+    install_element(VIEW_NODE, &show_babel_interface_cmd);
+    install_element(ENABLE_NODE, &show_babel_interface_cmd);
+    install_element(VIEW_NODE, &show_babel_neighbour_cmd);
+    install_element(ENABLE_NODE, &show_babel_neighbour_cmd);
+    install_element(VIEW_NODE, &show_babel_database_cmd);
+    install_element(ENABLE_NODE, &show_babel_database_cmd);
+    install_element(VIEW_NODE, &show_babel_parameters_cmd);
+    install_element(ENABLE_NODE, &show_babel_parameters_cmd);
+}
+
+/* hooks: functions called respectively when struct interface is
+ created or deleted. */
+static int
+babel_if_new_hook (struct interface *ifp)
+{
+    ifp->info = babel_interface_allocate();
+    return 0;
+}
+
+static int
+babel_if_delete_hook (struct interface *ifp)
+{
+    babel_interface_free(ifp->info);
+    ifp->info = NULL;
+    return 0;
+}
+
+/* Output an "interface" section for each of the known interfaces with
+babeld-specific statement lines where appropriate. */
+static int
+interface_config_write (struct vty *vty)
+{
+    struct listnode *node;
+    struct interface *ifp;
+    int write = 0;
+
+    for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp)) {
+        vty_out (vty, "interface %s%s", ifp->name,
+                 VTY_NEWLINE);
+        if (ifp->desc)
+            vty_out (vty, " description %s%s", ifp->desc,
+                     VTY_NEWLINE);
+        if (IS_ENABLE (ifp))
+        {
+            babel_interface_nfo *babel_ifp = babel_get_if_nfo (ifp);
+            /* wireless/no split-horizon is the default */
+            if (CHECK_FLAG (babel_ifp->flags, BABEL_IF_WIRED))
+            {
+                vty_out (vty, " babel wired%s", VTY_NEWLINE);
+                write++;
+            }
+            if (CHECK_FLAG (babel_ifp->flags, BABEL_IF_SPLIT_HORIZON))
+            {
+                vty_out (vty, " babel split-horizon%s", VTY_NEWLINE);
+                write++;
+            }
+            if (babel_ifp->hello_interval != BABEL_DEFAULT_HELLO_INTERVAL)
+            {
+                vty_out (vty, " babel hello-interval %u%s", babel_ifp->hello_interval, VTY_NEWLINE);
+                write++;
+            }
+            if (babel_ifp->update_interval != BABEL_DEFAULT_UPDATE_INTERVAL)
+            {
+                vty_out (vty, " babel update-interval %u%s", babel_ifp->update_interval, VTY_NEWLINE);
+                write++;
+            }
+        }
+        vty_out (vty, "!%s", VTY_NEWLINE);
+        write++;
+    }
+    return write;
+}
+
+/* Output a "network" statement line for each of the enabled interfaces. */
+int
+babel_enable_if_config_write (struct vty * vty)
+{
+    unsigned int i, lines = 0;
+    char *str;
+
+    for (i = 0; i < vector_active (babel_enable_if); i++)
+        if ((str = vector_slot (babel_enable_if, i)) != NULL)
+        {
+            vty_out (vty, " network %s%s", str, VTY_NEWLINE);
+            lines++;
+        }
+    return lines;
+}
+
+/* functions to allocate or free memory for a babel_interface_nfo, filling
+ needed fields */
+static babel_interface_nfo *
+babel_interface_allocate (void)
+{
+    babel_interface_nfo *babel_ifp;
+    babel_ifp = XMALLOC(MTYPE_BABEL_IF, sizeof(babel_interface_nfo));
+    if(babel_ifp == NULL)
+        return NULL;
+
+    /* Here are set the default values for an interface. */
+    memset(babel_ifp, 0, sizeof(babel_interface_nfo));
+    /* All flags are unset */
+    babel_ifp->bucket_time = babel_now.tv_sec;
+    babel_ifp->bucket = BUCKET_TOKENS_MAX;
+    babel_ifp->hello_seqno = (random() & 0xFFFF);
+    babel_ifp->hello_interval = BABEL_DEFAULT_HELLO_INTERVAL;
+    babel_ifp->update_interval = BABEL_DEFAULT_UPDATE_INTERVAL;
+    babel_ifp->channel = BABEL_IF_CHANNEL_INTERFERING;
+    babel_set_wired_internal(babel_ifp, 0);
+
+    return babel_ifp;
+}
+
+static void
+babel_interface_free (babel_interface_nfo *babel_ifp)
+{
+    XFREE(MTYPE_BABEL_IF, babel_ifp);
+}
diff --git a/babeld/babel_interface.h b/babeld/babel_interface.h
new file mode 100644
index 0000000..94fd8e5
--- /dev/null
+++ b/babeld/babel_interface.h
@@ -0,0 +1,152 @@
+/*  
+ *  This file is free software: you may copy, redistribute 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.  
+ *  
+ *  This file is distributed in the hope that it will be useful, but  
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+ *  General Public License for more details.  
+ *  
+ *  You should have received a copy of the GNU General Public License  
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ *  
+ * This file incorporates work covered by the following copyright and  
+ * permission notice:  
+ *  
+Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
+
+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
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#ifndef BABEL_INTERFACE_H
+#define BABEL_INTERFACE_H
+
+#include <zebra.h>
+#include "zclient.h"
+#include "vty.h"
+
+#define CONFIG_DEFAULT 0
+#define CONFIG_NO 1
+#define CONFIG_YES 2
+
+/* babeld interface informations */
+struct babel_interface {
+    unsigned short flags;                     /* see below */
+    unsigned short cost;
+    int channel;
+    struct timeval hello_timeout;
+    struct timeval update_timeout;
+    struct timeval flush_timeout;
+    struct timeval update_flush_timeout;
+    unsigned char *ipv4;
+    int buffered;
+    int bufsize;
+    char have_buffered_hello;
+    char have_buffered_id;
+    char have_buffered_nh;
+    char have_buffered_prefix;
+    unsigned char buffered_id[16];
+    unsigned char buffered_nh[4];
+    unsigned char buffered_prefix[16];
+    unsigned char *sendbuf;
+    struct buffered_update *buffered_updates;
+    int num_buffered_updates;
+    int update_bufsize;
+    time_t bucket_time;
+    unsigned int bucket;
+    time_t last_update_time;
+    unsigned short hello_seqno;
+    unsigned hello_interval;
+    unsigned update_interval;
+
+    /* For filter type slot. */
+#define BABEL_FILTER_IN  0
+#define BABEL_FILTER_OUT 1
+#define BABEL_FILTER_MAX 2
+    struct access_list *list[BABEL_FILTER_MAX];               /* Access-list. */
+    struct prefix_list *prefix[BABEL_FILTER_MAX];             /* Prefix-list. */
+};
+
+typedef struct babel_interface babel_interface_nfo;
+static inline babel_interface_nfo* babel_get_if_nfo(struct interface *ifp)
+{
+    return ((babel_interface_nfo*) ifp->info);
+}
+
+/* babel_interface_nfo flags */
+#define BABEL_IF_IS_UP         (1 << 0)
+#define BABEL_IF_WIRED         (1 << 1)
+#define BABEL_IF_SPLIT_HORIZON (1 << 2)
+#define BABEL_IF_LQ            (1 << 3)
+#define BABEL_IF_FARAWAY       (1 << 4)
+
+/* Only INTERFERING can appear on the wire. */
+#define BABEL_IF_CHANNEL_UNKNOWN 0
+#define BABEL_IF_CHANNEL_INTERFERING 255
+#define BABEL_IF_CHANNEL_NONINTERFERING -2
+
+static inline int
+if_up(struct interface *ifp)
+{
+    return (if_is_operative(ifp) &&
+            ifp->connected != NULL &&
+            (babel_get_if_nfo(ifp)->flags & BABEL_IF_IS_UP));
+}
+
+/* types:
+ struct interface _ifp, struct listnode node */
+#define FOR_ALL_INTERFACES(_ifp, _node)                                              \
+    for(ALL_LIST_ELEMENTS_RO(iflist, _node, _ifp))
+
+/* types:
+ struct interface *ifp, struct connected *_connected, struct listnode *node */
+#define FOR_ALL_INTERFACES_ADDRESSES(ifp, _connected, _node)                   \
+    for(ALL_LIST_ELEMENTS_RO(ifp->connected, _node, _connected))
+
+struct buffered_update {
+    unsigned char id[8];
+    unsigned char prefix[16];
+    unsigned char plen;
+    unsigned char pad[3];
+};
+
+
+/* init function */
+void babel_if_init(void);
+
+/* Callback functions for zebra client */
+int babel_interface_up (int, struct zclient *, zebra_size_t);
+int babel_interface_down (int, struct zclient *, zebra_size_t);
+int babel_interface_add (int, struct zclient *, zebra_size_t);
+int babel_interface_delete (int, struct zclient *, zebra_size_t);
+int babel_interface_address_add (int, struct zclient *, zebra_size_t);
+int babel_interface_address_delete (int, struct zclient *, zebra_size_t);
+
+unsigned jitter(babel_interface_nfo *, int);
+unsigned update_jitter(babel_interface_nfo *babel_ifp, int urgent);
+/* return "true" if "address" is one of our ipv6 addresses */
+int is_interface_ll_address(struct interface *ifp, const unsigned char *address);
+/* Send retraction to all, and reset all interfaces statistics. */
+void babel_interface_close_all(void);
+extern int babel_enable_if_config_write (struct vty *);
+
+
+#endif
diff --git a/babeld/babel_main.c b/babeld/babel_main.c
new file mode 100644
index 0000000..2f3b555
--- /dev/null
+++ b/babeld/babel_main.c
@@ -0,0 +1,532 @@
+/*  
+ *  This file is free software: you may copy, redistribute 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.  
+ *  
+ *  This file is distributed in the hope that it will be useful, but  
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+ *  General Public License for more details.  
+ *  
+ *  You should have received a copy of the GNU General Public License  
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ *  
+ * This file incorporates work covered by the following copyright and  
+ * permission notice:  
+ *  
+
+Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
+
+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
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+/* include zebra library */
+#include <zebra.h>
+#include "getopt.h"
+#include "if.h"
+#include "log.h"
+#include "thread.h"
+#include "privs.h"
+#include "sigevent.h"
+#include "version.h"
+#include "command.h"
+#include "vty.h"
+#include "memory.h"
+
+#include "babel_main.h"
+#include "babeld.h"
+#include "util.h"
+#include "kernel.h"
+#include "babel_interface.h"
+#include "neighbour.h"
+#include "route.h"
+#include "xroute.h"
+#include "message.h"
+#include "resend.h"
+#include "babel_zebra.h"
+
+
+static void babel_init (int argc, char **argv);
+static char *babel_get_progname(char *argv_0);
+static void babel_fail(void);
+static void babel_init_random(void);
+static void babel_replace_by_null(int fd);
+static void babel_init_signals(void);
+static void babel_exit_properly(void);
+static void babel_save_state_file(void);
+
+
+struct thread_master *master;     /* quagga's threads handler */
+struct timeval babel_now;         /* current time             */
+
+unsigned char myid[8];            /* unique id (mac address of an interface) */
+int debug = 0;
+
+int resend_delay = -1;
+static const char *pidfile = PATH_BABELD_PID;
+
+const unsigned char zeroes[16] = {0};
+const unsigned char ones[16] =
+    {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+     0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
+
+static const char *state_file = DAEMON_VTY_DIR "/babel-state";
+
+unsigned char protocol_group[16]; /* babel's link-local multicast address */
+int protocol_port;                /* babel's port */
+int protocol_socket = -1;         /* socket: communicate with others babeld */
+
+static char babel_config_default[] = SYSCONFDIR BABEL_DEFAULT_CONFIG;
+static char *babel_config_file = NULL;
+static char *babel_vty_addr = NULL;
+static int babel_vty_port = BABEL_VTY_PORT;
+
+/* Babeld options. */
+struct option longopts[] =
+{
+    { "daemon",      no_argument,       NULL, 'd'},
+    { "config_file", required_argument, NULL, 'f'},
+    { "pid_file",    required_argument, NULL, 'i'},
+    { "socket",      required_argument, NULL, 'z'},
+    { "help",        no_argument,       NULL, 'h'},
+    { "vty_addr",    required_argument, NULL, 'A'},
+    { "vty_port",    required_argument, NULL, 'P'},
+    { "user",        required_argument, NULL, 'u'},
+    { "group",       required_argument, NULL, 'g'},
+    { "version",     no_argument,       NULL, 'v'},
+    { 0 }
+};
+
+/* babeld privileges */
+static zebra_capabilities_t _caps_p [] =
+{
+    ZCAP_NET_RAW,
+    ZCAP_BIND
+};
+static struct zebra_privs_t babeld_privs =
+{
+#if defined(QUAGGA_USER)
+    .user = QUAGGA_USER,
+#endif
+#if defined QUAGGA_GROUP
+    .group = QUAGGA_GROUP,
+#endif
+#ifdef VTY_GROUP
+    .vty_group = VTY_GROUP,
+#endif
+    .caps_p = _caps_p,
+    .cap_num_p = 2,
+    .cap_num_i = 0
+};
+
+
+int
+main(int argc, char **argv)
+{
+    struct thread thread;
+    /* and print banner too */
+    babel_init(argc, argv);
+    while (thread_fetch (master, &thread)) {
+        thread_call (&thread);
+    }
+    return 0;
+}
+
+static void
+babel_usage (char *progname, int status)
+{
+  if (status != 0)
+    fprintf (stderr, "Try `%s --help' for more information.\n", progname);
+  else
+    {
+      printf ("Usage : %s [OPTION...]\n\
+Daemon which manages Babel routing protocol.\n\n\
+-d, --daemon       Runs in daemon mode\n\
+-f, --config_file  Set configuration file name\n\
+-i, --pid_file     Set process identifier file name\n\
+-z, --socket       Set path of zebra socket\n\
+-A, --vty_addr     Set vty's bind address\n\
+-P, --vty_port     Set vty's port number\n\
+-u, --user         User to run as\n\
+-g, --group        Group to run as\n\
+-v, --version      Print program version\n\
+-h, --help         Display this help and exit\n\
+\n\
+Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
+    }
+  exit (status);
+}
+
+/* make initialisations witch don't need infos about kernel(interfaces, etc.) */
+static void
+babel_init(int argc, char **argv)
+{
+    int rc, opt;
+    int do_daemonise = 0;
+    char *progname = NULL;
+
+    /* Set umask before anything for security */
+    umask (0027);
+    progname = babel_get_progname(argv[0]);
+
+    /* set default log (lib/log.h) */
+    zlog_default = openzlog(progname, ZLOG_BABEL,
+                            LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
+    /* set log destination as stdout until the config file is read */
+    zlog_set_level(NULL, ZLOG_DEST_STDOUT, LOG_WARNING);
+
+    babel_init_random();
+
+    /* set the Babel's default link-local multicast address and Babel's port */
+    parse_address("ff02:0:0:0:0:0:1:6", protocol_group, NULL);
+    protocol_port = 6696;
+
+    /* get options */
+    while(1) {
+        opt = getopt_long(argc, argv, "df:i:z:hA:P:u:g:v", longopts, 0);
+        if(opt < 0)
+            break;
+
+        switch(opt) {
+            case 0:
+                break;
+            case 'd':
+                do_daemonise = -1;
+                break;
+            case 'f':
+                babel_config_file = optarg;
+                break;
+            case 'i':
+                pidfile = optarg;
+                break;
+            case 'z':
+                zclient_serv_path_set (optarg);
+                break;
+            case 'A':
+                babel_vty_addr = optarg;
+                break;
+            case 'P':
+                babel_vty_port = atoi (optarg);
+                if (babel_vty_port <= 0 || babel_vty_port > 0xffff)
+                    babel_vty_port = BABEL_VTY_PORT;
+                break;
+            case 'u':
+                babeld_privs.user = optarg;
+                break;
+            case 'g':
+                babeld_privs.group = optarg;
+                break;
+            case 'v':
+                print_version (progname);
+                exit (0);
+                break;
+            case 'h':
+                babel_usage (progname, 0);
+                break;
+            default:
+                babel_usage (progname, 1);
+                break;
+        }
+    }
+
+    /* create the threads handler */
+    master = thread_master_create ();
+
+    /* Library inits. */
+    zprivs_init (&babeld_privs);
+    babel_init_signals();
+    cmd_init (1);
+    vty_init (master);
+    memory_init ();
+
+    resend_delay = BABEL_DEFAULT_RESEND_DELAY;
+
+    babel_replace_by_null(STDIN_FILENO);
+
+    if (do_daemonise && daemonise() < 0) {
+        zlog_err("daemonise: %s", safe_strerror(errno));
+        exit (1);
+    }
+
+    /* write pid file */
+    if (pid_output(pidfile) < 0) {
+        zlog_err("error while writing pidfile");
+        exit (1);
+    };
+
+    /* init some quagga's dependencies, and babeld's commands */
+    babeld_quagga_init();
+    /* init zebra client's structure and it's commands */
+    /* this replace kernel_setup && kernel_setup_socket */
+    babelz_zebra_init ();
+
+    /* Sort all installed commands. */
+    sort_node ();
+
+    /* Get zebra configuration file. */
+    zlog_set_level (NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
+    vty_read_config (babel_config_file, babel_config_default);
+
+    /* Create VTY socket */
+    vty_serv_sock (babel_vty_addr, babel_vty_port, BABEL_VTYSH_PATH);
+
+    /* init buffer */
+    rc = resize_receive_buffer(1500);
+    if(rc < 0)
+        babel_fail();
+
+    schedule_neighbours_check(5000, 1);
+
+    zlog_notice ("BABELd %s starting: vty@%d", BABEL_VERSION, babel_vty_port);
+}
+
+/* return the progname (without path, example: "./x/progname" --> "progname") */
+static char *
+babel_get_progname(char *argv_0) {
+    char *p = strrchr (argv_0, '/');
+    return (p ? ++p : argv_0);
+}
+
+static void
+babel_fail(void)
+{
+    exit(1);
+}
+
+/* initialize random value, and set 'babel_now' by the way. */
+static void
+babel_init_random(void)
+{
+    gettime(&babel_now);
+    int rc;
+    unsigned int seed;
+
+    rc = read_random_bytes(&seed, sizeof(seed));
+    if(rc < 0) {
+        zlog_err("read(random): %s", safe_strerror(errno));
+        seed = 42;
+    }
+
+    seed ^= (babel_now.tv_sec ^ babel_now.tv_usec);
+    srandom(seed);
+}
+
+/*
+ close fd, and replace it by "/dev/null"
+ exit if error
+ */
+static void
+babel_replace_by_null(int fd)
+{
+    int fd_null;
+    int rc;
+
+    fd_null = open("/dev/null", O_RDONLY);
+    if(fd_null < 0) {
+        zlog_err("open(null): %s", safe_strerror(errno));
+        exit(1);
+    }
+
+    rc = dup2(fd_null, fd);
+    if(rc < 0) {
+        zlog_err("dup2(null, 0): %s", safe_strerror(errno));
+        exit(1);
+    }
+
+    close(fd_null);
+}
+
+/*
+ Load the state file: check last babeld's running state, usefull in case of
+ "/etc/init.d/babeld restart"
+ */
+void
+babel_load_state_file(void)
+{
+    int fd;
+    int rc;
+
+    fd = open(state_file, O_RDONLY);
+    if(fd < 0 && errno != ENOENT)
+        zlog_err("open(babel-state: %s)", safe_strerror(errno));
+    rc = unlink(state_file);
+    if(fd >= 0 && rc < 0) {
+        zlog_err("unlink(babel-state): %s", safe_strerror(errno));
+        /* If we couldn't unlink it, it's probably stale. */
+        close(fd);
+        fd = -1;
+    }
+    if(fd >= 0) {
+        char buf[100];
+        char buf2[100];
+        int s;
+        long t;
+        rc = read(fd, buf, 99);
+        if(rc < 0) {
+            zlog_err("read(babel-state): %s", safe_strerror(errno));
+        } else {
+            buf[rc] = '\0';
+            rc = sscanf(buf, "%99s %d %ld\n", buf2, &s, &t);
+            if(rc == 3 && s >= 0 && s <= 0xFFFF) {
+                unsigned char sid[8];
+                rc = parse_eui64(buf2, sid);
+                if(rc < 0) {
+                    zlog_err("Couldn't parse babel-state.");
+                } else {
+                    struct timeval realnow;
+                    debugf(BABEL_DEBUG_COMMON,
+                           "Got %s %d %ld from babel-state.",
+                           format_eui64(sid), s, t);
+                    gettimeofday(&realnow, NULL);
+                    if(memcmp(sid, myid, 8) == 0)
+                        myseqno = seqno_plus(s, 1);
+                    else
+                        zlog_err("ID mismatch in babel-state. id=%s; old=%s",
+                                 format_eui64(myid),
+                                 format_eui64(sid));
+                }
+            } else {
+                zlog_err("Couldn't parse babel-state.");
+            }
+        }
+        close(fd);
+        fd = -1;
+    }
+}
+
+static void
+babel_sigexit(void)
+{
+    zlog_notice("Terminating on signal");
+
+    babel_exit_properly();
+}
+
+static void
+babel_sigusr1 (void)
+{
+    zlog_rotate (NULL);
+}
+
+static void
+babel_init_signals(void)
+{
+    static struct quagga_signal_t babel_signals[] =
+    {
+        {
+            .signal = SIGUSR1,
+            .handler = &babel_sigusr1,
+        },
+        {
+            .signal = SIGINT,
+            .handler = &babel_sigexit,
+        },
+        {
+            .signal = SIGTERM,
+            .handler = &babel_sigexit,
+        },
+    };
+
+    signal_init (master, Q_SIGC(babel_signals), babel_signals);
+}
+
+static void
+babel_exit_properly(void)
+{
+    debugf(BABEL_DEBUG_COMMON, "Exiting...");
+    usleep(roughly(10000));
+    gettime(&babel_now);
+
+    /* Uninstall and flush all routes. */
+    debugf(BABEL_DEBUG_COMMON, "Uninstall routes.");
+    flush_all_routes();
+    babel_interface_close_all();
+    babel_zebra_close_connexion();
+    babel_save_state_file();
+    debugf(BABEL_DEBUG_COMMON, "Remove pid file.");
+    if(pidfile)
+        unlink(pidfile);
+    debugf(BABEL_DEBUG_COMMON, "Done.");
+
+    exit(0);
+}
+
+static void
+babel_save_state_file(void)
+{
+    int fd;
+    int rc;
+
+    debugf(BABEL_DEBUG_COMMON, "Save state file.");
+    fd = open(state_file, O_WRONLY | O_TRUNC | O_CREAT, 0644);
+    if(fd < 0) {
+        zlog_err("creat(babel-state): %s", safe_strerror(errno));
+        unlink(state_file);
+    } else {
+        struct timeval realnow;
+        char buf[100];
+        gettimeofday(&realnow, NULL);
+        rc = snprintf(buf, 100, "%s %d %ld\n",
+                      format_eui64(myid), (int)myseqno,
+                      (long)realnow.tv_sec);
+        if(rc < 0 || rc >= 100) {
+            zlog_err("write(babel-state): overflow.");
+            unlink(state_file);
+        } else {
+            rc = write(fd, buf, rc);
+            if(rc < 0) {
+                zlog_err("write(babel-state): %s", safe_strerror(errno));
+                unlink(state_file);
+            }
+            fsync(fd);
+        }
+        close(fd);
+    }
+}
+
+void
+show_babel_main_configuration (struct vty *vty)
+{
+    vty_out(vty,
+            "pid file                = %s%s"
+            "state file              = %s%s"
+            "configuration file      = %s%s"
+            "protocol informations:%s"
+            "  multicast address     = %s%s"
+            "  port                  = %d%s"
+            "vty address             = %s%s"
+            "vty port                = %d%s"
+            "id                      = %s%s"
+            "allow_duplicates        = %s%s"
+            "kernel_metric           = %d%s",
+            pidfile, VTY_NEWLINE,
+            state_file, VTY_NEWLINE,
+            babel_config_file ? babel_config_file : babel_config_default,
+            VTY_NEWLINE,
+            VTY_NEWLINE,
+            format_address(protocol_group), VTY_NEWLINE,
+            protocol_port, VTY_NEWLINE,
+            babel_vty_addr ? babel_vty_addr : "None",
+            VTY_NEWLINE,
+            babel_vty_port, VTY_NEWLINE,
+            format_eui64(myid), VTY_NEWLINE,
+            format_bool(allow_duplicates), VTY_NEWLINE,
+            kernel_metric, VTY_NEWLINE);
+}
diff --git a/babeld/babel_main.h b/babeld/babel_main.h
new file mode 100644
index 0000000..a4038de
--- /dev/null
+++ b/babeld/babel_main.h
@@ -0,0 +1,57 @@
+/*  
+ *  This file is free software: you may copy, redistribute 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.  
+ *  
+ *  This file is distributed in the hope that it will be useful, but  
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+ *  General Public License for more details.  
+ *  
+ *  You should have received a copy of the GNU General Public License  
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ *  
+ * This file incorporates work covered by the following copyright and  
+ * permission notice:  
+ *  
+Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
+
+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
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#include "vty.h"
+
+extern struct timeval babel_now;         /* current time             */
+extern struct thread_master *master;     /* quagga's threads handler */
+extern int debug;
+extern int resend_delay;
+
+extern unsigned char myid[8];
+
+extern const unsigned char zeroes[16], ones[16];
+
+extern int protocol_port;
+extern unsigned char protocol_group[16];
+extern int protocol_socket;
+extern int kernel_socket;
+extern int max_request_hopcount;
+
+void babel_load_state_file(void);
+void show_babel_main_configuration (struct vty *vty);
diff --git a/babeld/babel_zebra.c b/babeld/babel_zebra.c
new file mode 100644
index 0000000..75a1e6a
--- /dev/null
+++ b/babeld/babel_zebra.c
@@ -0,0 +1,378 @@
+/*  
+ *  This file is free software: you may copy, redistribute 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.  
+ *  
+ *  This file is distributed in the hope that it will be useful, but  
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+ *  General Public License for more details.  
+ *  
+ *  You should have received a copy of the GNU General Public License  
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ *  
+ * This file incorporates work covered by the following copyright and  
+ * permission notice:  
+ *
+Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
+
+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
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+/* quagga's includes */
+#include <zebra.h>
+#include "command.h"
+#include "zclient.h"
+#include "stream.h"
+
+/* babel's includes*/
+#include "babel_zebra.h"
+#include "babel_interface.h"
+#include "xroute.h"
+#include "util.h"
+
+void babelz_zebra_init(void);
+
+
+/* we must use a pointer because of zclient.c's functions (new, free). */
+struct zclient *zclient;
+static int zebra_config_write (struct vty *vty);
+
+/* Debug types */
+static struct {
+    int type;
+    int str_min_len;
+    const char *str;
+} debug_type[] = {
+    {BABEL_DEBUG_COMMON,  1, "common"},
+    {BABEL_DEBUG_KERNEL,  1, "kernel"},
+    {BABEL_DEBUG_FILTER,  1, "filter"},
+    {BABEL_DEBUG_TIMEOUT, 1, "timeout"},
+    {BABEL_DEBUG_IF,      1, "interface"},
+    {BABEL_DEBUG_ROUTE,   1, "route"},
+    {BABEL_DEBUG_ALL,     1, "all"},
+    {0, 0, NULL}
+};
+
+/* Zebra node structure. */
+struct cmd_node zebra_node =
+{
+    ZEBRA_NODE,
+    "%s(config-router)# ",
+    1 /* vtysh? yes */
+};
+
+
+/* Zebra route add and delete treatment (ipv6). */
+static int
+babel_zebra_read_ipv6 (int command, struct zclient *zclient,
+		       zebra_size_t length)
+{
+    struct stream *s;
+    struct zapi_ipv6 api;
+    unsigned long ifindex = -1;
+    struct in6_addr nexthop;
+    struct prefix_ipv6 prefix;
+
+    s = zclient->ibuf;
+    ifindex = 0;
+    memset (&nexthop, 0, sizeof (struct in6_addr));
+    memset (&api, 0, sizeof(struct zapi_ipv6));
+    memset (&prefix, 0, sizeof (struct prefix_ipv6));
+
+    /* Type, flags, message. */
+    api.type = stream_getc (s);
+    api.flags = stream_getc (s);
+    api.message = stream_getc (s);
+
+    /* IPv6 prefix. */
+    prefix.family = AF_INET6;
+    prefix.prefixlen = stream_getc (s);
+    stream_get (&prefix.prefix, s, PSIZE (prefix.prefixlen));
+
+    /* Nexthop, ifindex, distance, metric. */
+    if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) {
+        api.nexthop_num = stream_getc (s);
+        stream_get (&nexthop, s, sizeof(nexthop));
+    }
+    if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX)) {
+        api.ifindex_num = stream_getc (s);
+        ifindex = stream_getl (s);
+    }
+    if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
+        api.distance = stream_getc (s);
+    else
+        api.distance = 0;
+    if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
+        api.metric = stream_getl (s);
+    else
+        api.metric = 0;
+
+    if (command == ZEBRA_IPV6_ROUTE_ADD)
+        babel_ipv6_route_add(&api, &prefix, ifindex, &nexthop);
+    else
+        babel_ipv6_route_delete(&api, &prefix, ifindex);
+
+    return 0;
+}
+
+static int
+babel_zebra_read_ipv4 (int command, struct zclient *zclient,
+		       zebra_size_t length)
+{
+    struct stream *s;
+    struct zapi_ipv4 api;
+    unsigned long ifindex = -1;
+    struct in_addr nexthop;
+    struct prefix_ipv4 prefix;
+
+    s = zclient->ibuf;
+    ifindex = 0;
+    memset (&nexthop, 0, sizeof (struct in_addr));
+    memset (&api, 0, sizeof(struct zapi_ipv4));
+    memset (&prefix, 0, sizeof (struct prefix_ipv4));
+
+    /* Type, flags, message. */
+    api.type = stream_getc (s);
+    api.flags = stream_getc (s);
+    api.message = stream_getc (s);
+
+    /* IPv6 prefix. */
+    prefix.family = AF_INET;
+    prefix.prefixlen = stream_getc (s);
+    stream_get (&prefix.prefix, s, PSIZE (prefix.prefixlen));
+
+    /* Nexthop, ifindex, distance, metric. */
+    if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) {
+        api.nexthop_num = stream_getc (s);
+        stream_get (&nexthop, s, sizeof(nexthop));
+    }
+    if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX)) {
+        api.ifindex_num = stream_getc (s);
+        ifindex = stream_getl (s);
+    }
+    if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
+        api.distance = stream_getc (s);
+    else
+        api.distance = 0;
+    if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
+        api.metric = stream_getl (s);
+    else
+        api.metric = 0;
+
+    if (command == ZEBRA_IPV6_ROUTE_ADD) {
+        babel_ipv4_route_add(&api, &prefix, ifindex, &nexthop);
+    } else {
+        babel_ipv4_route_delete(&api, &prefix, ifindex);
+    }
+
+    return 0;
+}
+
+/* [Babel Command] */
+DEFUN (babel_redistribute_type,
+       babel_redistribute_type_cmd,
+       "redistribute " QUAGGA_REDIST_STR_BABELD,
+       "Redistribute\n"
+       QUAGGA_REDIST_HELP_STR_BABELD)
+{
+    int type;
+
+    type = proto_redistnum(AFI_IP6, argv[0]);
+
+    if (type < 0)
+        type = proto_redistnum(AFI_IP, argv[0]);
+
+    if (type < 0) {
+        vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE);
+        return CMD_WARNING;
+    }
+
+    zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, type);
+    return CMD_SUCCESS;
+}
+
+/* [Babel Command] */
+DEFUN (no_babel_redistribute_type,
+       no_babel_redistribute_type_cmd,
+       "no redistribute " QUAGGA_REDIST_STR_BABELD,
+       NO_STR
+       "Redistribute\n"
+       QUAGGA_REDIST_HELP_STR_BABELD)
+{
+    int type;
+
+    type = proto_redistnum(AFI_IP6, argv[0]);
+
+    if (type < 0)
+        type = proto_redistnum(AFI_IP, argv[0]);
+
+    if (type < 0) {
+        vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE);
+        return CMD_WARNING;
+    }
+
+    zclient_redistribute (ZEBRA_REDISTRIBUTE_DELETE, zclient, type);
+    /* perhaps should we remove xroutes having the same type... */
+    return CMD_SUCCESS;
+}
+
+#ifndef NO_DEBUG
+/* [Babel Command] */
+DEFUN (debug_babel,
+       debug_babel_cmd,
+       "debug babel (common|kernel|filter|timeout|interface|route|all)",
+       "Enable debug messages for specific or all part.\n"
+       "Babel information\n"
+       "Common messages (default)\n"
+       "Kernel messages\n"
+       "Filter messages\n"
+       "Timeout messages\n"
+       "Interface messages\n"
+       "Route messages\n"
+       "All messages\n")
+{
+    int i;
+
+    for(i = 0; debug_type[i].str != NULL; i++) {
+        if (strncmp (debug_type[i].str, argv[0],
+                     debug_type[i].str_min_len) == 0) {
+            debug |= debug_type[i].type;
+            return CMD_SUCCESS;
+        }
+    }
+
+    vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE);
+
+    return CMD_WARNING;
+}
+
+/* [Babel Command] */
+DEFUN (no_debug_babel,
+       no_debug_babel_cmd,
+       "no debug babel (common|kernel|filter|timeout|interface|route|all)",
+       NO_STR
+       "Disable debug messages for specific or all part.\n"
+       "Babel information\n"
+       "Common messages (default)\n"
+       "Kernel messages\n"
+       "Filter messages\n"
+       "Timeout messages\n"
+       "Interface messages\n"
+       "Route messages\n"
+       "All messages\n")
+{
+    int i;
+
+    for (i = 0; debug_type[i].str; i++) {
+        if (strncmp(debug_type[i].str, argv[0],
+                    debug_type[i].str_min_len) == 0) {
+            debug &= ~debug_type[i].type;
+            return CMD_SUCCESS;
+        }
+    }
+
+    vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE);
+
+    return CMD_WARNING;
+}
+#endif /* NO_DEBUG */
+
+/* Output "debug" statement lines, if necessary. */
+int
+debug_babel_config_write (struct vty * vty)
+{
+#ifdef NO_DEBUG
+    return 0;
+#else
+    int i, lines = 0;
+
+    if (debug == BABEL_DEBUG_ALL)
+    {
+        vty_out (vty, "debug babel all%s", VTY_NEWLINE);
+        lines++;
+    }
+    else
+        for (i = 0; debug_type[i].str != NULL; i++)
+            if
+            (
+                debug_type[i].type != BABEL_DEBUG_ALL
+                && CHECK_FLAG (debug, debug_type[i].type)
+            )
+            {
+                vty_out (vty, "debug babel %s%s", debug_type[i].str, VTY_NEWLINE);
+                lines++;
+            }
+    if (lines)
+    {
+        vty_out (vty, "!%s", VTY_NEWLINE);
+        lines++;
+    }
+    return lines;
+#endif /* NO_DEBUG */
+}
+
+void babelz_zebra_init(void)
+{
+    zclient = zclient_new();
+    zclient_init(zclient, ZEBRA_ROUTE_BABEL);
+
+    zclient->interface_add = babel_interface_add;
+    zclient->interface_delete = babel_interface_delete;
+    zclient->interface_up = babel_interface_up;
+    zclient->interface_down = babel_interface_down;
+    zclient->interface_address_add = babel_interface_address_add;
+    zclient->interface_address_delete = babel_interface_address_delete;
+    zclient->ipv4_route_add = babel_zebra_read_ipv4;
+    zclient->ipv4_route_delete = babel_zebra_read_ipv4;
+    zclient->ipv6_route_add = babel_zebra_read_ipv6;
+    zclient->ipv6_route_delete = babel_zebra_read_ipv6;
+
+    install_node (&zebra_node, zebra_config_write);
+    install_element(BABEL_NODE, &babel_redistribute_type_cmd);
+    install_element(BABEL_NODE, &no_babel_redistribute_type_cmd);
+    install_element(ENABLE_NODE, &debug_babel_cmd);
+    install_element(ENABLE_NODE, &no_debug_babel_cmd);
+    install_element(CONFIG_NODE, &debug_babel_cmd);
+    install_element(CONFIG_NODE, &no_debug_babel_cmd);
+}
+
+static int
+zebra_config_write (struct vty *vty)
+{
+    if (! zclient->enable)
+    {
+        vty_out (vty, "no router zebra%s", VTY_NEWLINE);
+        return 1;
+    }
+    else if (! zclient->redist[ZEBRA_ROUTE_BABEL])
+    {
+        vty_out (vty, "router zebra%s", VTY_NEWLINE);
+        vty_out (vty, " no redistribute babel%s", VTY_NEWLINE);
+        return 1;
+    }
+    return 0;
+}
+
+void
+babel_zebra_close_connexion(void)
+{
+    zclient_stop(zclient);
+}
diff --git a/babeld/babel_zebra.h b/babeld/babel_zebra.h
new file mode 100644
index 0000000..99601aa
--- /dev/null
+++ b/babeld/babel_zebra.h
@@ -0,0 +1,50 @@
+/*  
+ *  This file is free software: you may copy, redistribute 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.  
+ *  
+ *  This file is distributed in the hope that it will be useful, but  
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+ *  General Public License for more details.  
+ *  
+ *  You should have received a copy of the GNU General Public License  
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ *  
+ * This file incorporates work covered by the following copyright and  
+ * permission notice:  
+ *  
+Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
+
+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
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#ifndef BABEL_ZEBRA_H
+#define BABEL_ZEBRA_H
+
+#include "vty.h"
+
+extern struct zclient *zclient;
+
+void babelz_zebra_init(void);
+void babel_zebra_close_connexion(void);
+extern int debug_babel_config_write (struct vty *);
+
+#endif
diff --git a/babeld/babeld.c b/babeld/babeld.c
new file mode 100644
index 0000000..1ae3f04
--- /dev/null
+++ b/babeld/babeld.c
@@ -0,0 +1,728 @@
+/*  
+ *  This file is free software: you may copy, redistribute 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.  
+ *  
+ *  This file is distributed in the hope that it will be useful, but  
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+ *  General Public License for more details.  
+ *  
+ *  You should have received a copy of the GNU General Public License  
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ *  
+ * This file incorporates work covered by the following copyright and  
+ * permission notice:  
+ *  
+
+Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
+
+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
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#include <zebra.h>
+#include "command.h"
+#include "prefix.h"
+#include "memory.h"
+#include "memtypes.h"
+#include "table.h"
+#include "distribute.h"
+#include "prefix.h"
+#include "filter.h"
+#include "plist.h"
+
+#include "babel_main.h"
+#include "babeld.h"
+#include "util.h"
+#include "net.h"
+#include "kernel.h"
+#include "babel_interface.h"
+#include "neighbour.h"
+#include "route.h"
+#include "message.h"
+#include "resend.h"
+#include "babel_filter.h"
+#include "babel_zebra.h"
+
+
+static int babel_init_routing_process(struct thread *thread);
+static void babel_get_myid(void);
+static void babel_initial_noise(void);
+static int babel_read_protocol (struct thread *thread);
+static int babel_main_loop(struct thread *thread);
+static void babel_set_timer(struct timeval *timeout);
+static void babel_fill_with_next_timeout(struct timeval *tv);
+
+
+/* Informations relative to the babel running daemon. */
+static struct babel *babel_routing_process = NULL;
+static unsigned char *receive_buffer = NULL;
+static int receive_buffer_size = 0;
+
+/* timeouts */
+struct timeval check_neighbours_timeout;
+static time_t expiry_time;
+static time_t source_expiry_time;
+
+/* Babel node structure. */
+static struct cmd_node cmd_babel_node =
+{
+    .node   = BABEL_NODE,
+    .prompt = "%s(config-router)# ",
+    .vtysh  = 1,
+};
+
+/* print current babel configuration on vty */
+static int
+babel_config_write (struct vty *vty)
+{
+    int lines = 0;
+    int i;
+
+    /* list enabled debug modes */
+    lines += debug_babel_config_write (vty);
+
+    if (!babel_routing_process)
+        return lines;
+    vty_out (vty, "router babel%s", VTY_NEWLINE);
+    if (resend_delay != BABEL_DEFAULT_RESEND_DELAY)
+    {
+        vty_out (vty, " babel resend-delay %u%s", resend_delay, VTY_NEWLINE);
+        lines++;
+    }
+    /* list enabled interfaces */
+    lines = 1 + babel_enable_if_config_write (vty);
+    /* list redistributed protocols */
+    for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
+        if (i != zclient->redist_default && zclient->redist[i])
+        {
+            vty_out (vty, " redistribute %s%s", zebra_route_string (i), VTY_NEWLINE);
+            lines++;
+        }
+
+    return lines;
+}
+
+
+static int
+babel_create_routing_process (void)
+{
+    assert (babel_routing_process == NULL);
+
+    /* Allocaste Babel instance. */
+    babel_routing_process = XCALLOC (MTYPE_BABEL, sizeof (struct babel));
+
+    /* Initialize timeouts */
+    gettime(&babel_now);
+    expiry_time = babel_now.tv_sec + roughly(30);
+    source_expiry_time = babel_now.tv_sec + roughly(300);
+
+    /* Make socket for Babel protocol. */
+    protocol_socket = babel_socket(protocol_port);
+    if (protocol_socket < 0) {
+        zlog_err("Couldn't create link local socket: %s", safe_strerror(errno));
+        goto fail;
+    }
+
+    /* Threads. */
+    babel_routing_process->t_read =
+    thread_add_read(master, &babel_read_protocol, NULL, protocol_socket);
+    /* wait a little: zebra will announce interfaces, addresses, routes... */
+    babel_routing_process->t_update =
+    thread_add_timer_msec(master, &babel_init_routing_process, NULL, 200L);
+    return 0;
+
+fail:
+    XFREE(MTYPE_BABEL, babel_routing_process);
+    babel_routing_process = NULL;
+    return -1;
+}
+
+/* thread reading entries form others babel daemons */
+static int
+babel_read_protocol (struct thread *thread)
+{
+    int rc;
+    struct interface *ifp = NULL;
+    struct sockaddr_in6 sin6;
+    struct listnode *linklist_node = NULL;
+
+    assert(babel_routing_process != NULL);
+    assert(protocol_socket >= 0);
+
+    rc = babel_recv(protocol_socket,
+                    receive_buffer, receive_buffer_size,
+                    (struct sockaddr*)&sin6, sizeof(sin6));
+    if(rc < 0) {
+        if(errno != EAGAIN && errno != EINTR) {
+            zlog_err("recv: %s", safe_strerror(errno));
+        }
+    } else {
+        FOR_ALL_INTERFACES(ifp, linklist_node) {
+            if(!if_up(ifp))
+                continue;
+            if(ifp->ifindex == sin6.sin6_scope_id) {
+                parse_packet((unsigned char*)&sin6.sin6_addr, ifp,
+                             receive_buffer, rc);
+                break;
+            }
+        }
+    }
+
+    /* re-add thread */
+    babel_routing_process->t_read =
+    thread_add_read(master, &babel_read_protocol, NULL, protocol_socket);
+    return 0;
+}
+
+/* Zebra will give some information, especially about interfaces. This function
+ must be call with a litte timeout wich may give zebra the time to do his job,
+ making these inits have sense. */
+static int
+babel_init_routing_process(struct thread *thread)
+{
+    myseqno = (random() & 0xFFFF);
+    babel_get_myid();
+    babel_load_state_file();
+    debugf(BABEL_DEBUG_COMMON, "My ID is : %s.", format_eui64(myid));
+    babel_initial_noise();
+    babel_main_loop(thread);/* this function self-add to the t_update thread */
+    return 0;
+}
+
+/* fill "myid" with an unique id (only if myid != {0}). */
+static void
+babel_get_myid(void)
+{
+    struct interface *ifp = NULL;
+    struct listnode *linklist_node = NULL;
+    int rc;
+    int i;
+
+    /* if we already have an id (from state file), we return. */
+    if (memcmp(myid, zeroes, 8) != 0) {
+        return;
+    }
+
+    FOR_ALL_INTERFACES(ifp, linklist_node) {
+        /* ifp->ifindex is not necessarily valid at this point */
+        int ifindex = if_nametoindex(ifp->name);
+        if(ifindex > 0) {
+            unsigned char eui[8];
+            rc = if_eui64(ifp->name, ifindex, eui);
+            if(rc < 0)
+                continue;
+            memcpy(myid, eui, 8);
+            return;
+        }
+    }
+
+    /* We failed to get a global EUI64 from the interfaces we were given.
+     Let's try to find an interface with a MAC address. */
+    for(i = 1; i < 256; i++) {
+        char buf[IF_NAMESIZE], *ifname;
+        unsigned char eui[8];
+        ifname = if_indextoname(i, buf);
+        if(ifname == NULL)
+            continue;
+        rc = if_eui64(ifname, i, eui);
+        if(rc < 0)
+            continue;
+        memcpy(myid, eui, 8);
+        return;
+    }
+
+    zlog_err("Warning: couldn't find router id -- using random value.");
+
+    rc = read_random_bytes(myid, 8);
+    if(rc < 0) {
+        zlog_err("read(random): %s (cannot assign an ID)",safe_strerror(errno));
+        exit(1);
+    }
+    /* Clear group and global bits */
+    myid[0] &= ~3;
+}
+
+/* Make some noise so that others notice us, and send retractions in
+ case we were restarted recently */
+static void
+babel_initial_noise(void)
+{
+    struct interface *ifp = NULL;
+    struct listnode *linklist_node = NULL;
+
+    FOR_ALL_INTERFACES(ifp, linklist_node) {
+        if(!if_up(ifp))
+            continue;
+        /* Apply jitter before we send the first message. */
+        usleep(roughly(10000));
+        gettime(&babel_now);
+        send_hello(ifp);
+        send_wildcard_retraction(ifp);
+    }
+
+    FOR_ALL_INTERFACES(ifp, linklist_node) {
+        if(!if_up(ifp))
+            continue;
+        usleep(roughly(10000));
+        gettime(&babel_now);
+        send_hello(ifp);
+        send_wildcard_retraction(ifp);
+        send_self_update(ifp);
+        send_request(ifp, NULL, 0);
+        flushupdates(ifp);
+        flushbuf(ifp);
+    }
+}
+
+/* Delete all the added babel routes, make babeld only speak to zebra. */
+static void
+babel_clean_routing_process()
+{
+    flush_all_routes();
+    babel_interface_close_all();
+
+    /* cancel threads */
+    if (babel_routing_process->t_read != NULL) {
+        thread_cancel(babel_routing_process->t_read);
+    }
+    if (babel_routing_process->t_update != NULL) {
+        thread_cancel(babel_routing_process->t_update);
+    }
+
+    XFREE(MTYPE_BABEL, babel_routing_process);
+    babel_routing_process = NULL;
+}
+
+/* Function used with timeout. */
+static int
+babel_main_loop(struct thread *thread)
+{
+    struct timeval tv;
+    struct interface *ifp = NULL;
+    struct listnode *linklist_node = NULL;
+
+    while(1) {
+        gettime(&babel_now);
+
+        /* timeouts --------------------------------------------------------- */
+        /* get the next timeout */
+        babel_fill_with_next_timeout(&tv);
+        /* if there is no timeout, we must wait. */
+        if(timeval_compare(&tv, &babel_now) > 0) {
+            timeval_minus(&tv, &tv, &babel_now);
+            debugf(BABEL_DEBUG_TIMEOUT, "babel main loop : timeout: %ld msecs",
+                   tv.tv_sec * 1000 + tv.tv_usec / 1000);
+            /* it happens often to have less than 1 ms, it's bad. */
+            timeval_add_msec(&tv, &tv, 300);
+            babel_set_timer(&tv);
+            return 0;
+        }
+
+        gettime(&babel_now);
+
+        /* update database -------------------------------------------------- */
+        if(timeval_compare(&check_neighbours_timeout, &babel_now) < 0) {
+            int msecs;
+            msecs = check_neighbours();
+            msecs = MAX(msecs, 10);
+            schedule_neighbours_check(msecs, 1);
+        }
+
+        if(babel_now.tv_sec >= expiry_time) {
+            expire_routes();
+            expire_resend();
+            expiry_time = babel_now.tv_sec + roughly(30);
+        }
+
+        if(babel_now.tv_sec >= source_expiry_time) {
+            expire_sources();
+            source_expiry_time = babel_now.tv_sec + roughly(300);
+        }
+
+        FOR_ALL_INTERFACES(ifp, linklist_node) {
+            babel_interface_nfo *babel_ifp = NULL;
+            if(!if_up(ifp))
+                continue;
+            babel_ifp = babel_get_if_nfo(ifp);
+            if(timeval_compare(&babel_now, &babel_ifp->hello_timeout) >= 0)
+                send_hello(ifp);
+            if(timeval_compare(&babel_now, &babel_ifp->update_timeout) >= 0)
+                send_update(ifp, 0, NULL, 0);
+            if(timeval_compare(&babel_now,
+                               &babel_ifp->update_flush_timeout) >= 0)
+                flushupdates(ifp);
+        }
+
+        if(resend_time.tv_sec != 0) {
+            if(timeval_compare(&babel_now, &resend_time) >= 0)
+                do_resend();
+        }
+
+        if(unicast_flush_timeout.tv_sec != 0) {
+            if(timeval_compare(&babel_now, &unicast_flush_timeout) >= 0)
+                flush_unicast(1);
+        }
+
+        FOR_ALL_INTERFACES(ifp, linklist_node) {
+            babel_interface_nfo *babel_ifp = NULL;
+            if(!if_up(ifp))
+                continue;
+            babel_ifp = babel_get_if_nfo(ifp);
+            if(babel_ifp->flush_timeout.tv_sec != 0) {
+                if(timeval_compare(&babel_now, &babel_ifp->flush_timeout) >= 0)
+                    flushbuf(ifp);
+            }
+        }
+    }
+
+    assert(0); /* this line should never be reach */
+}
+
+static void
+printIfMin(struct timeval *tv, int cmd, const char *tag, const char *ifname)
+{
+    static struct timeval curr_tv;
+    static char buffer[200];
+    static const char *curr_tag = NULL;
+
+    switch (cmd) {
+        case 0: /* reset timeval */
+            curr_tv = *tv;
+            if(ifname != NULL) {
+                snprintf(buffer, 200L, "interface: %s; %s", ifname, tag);
+                curr_tag = buffer;
+            } else {
+                curr_tag = tag;
+            }
+            break;
+        case 1: /* take the min */
+            if (tv->tv_sec == 0 && tv->tv_usec == 0) { /* if (tv == ∞) */
+                break;
+            }
+            if (tv->tv_sec < curr_tv.tv_sec ||(tv->tv_sec == curr_tv.tv_sec &&
+                                               tv->tv_usec < curr_tv.tv_usec)) {
+                curr_tv = *tv;
+                if(ifname != NULL) {
+                    snprintf(buffer, 200L, "interface: %s; %s", ifname, tag);
+                    curr_tag = buffer;
+                } else {
+                    curr_tag = tag;
+                }
+            }
+            break;
+        case 2: /* print message */
+            debugf(BABEL_DEBUG_TIMEOUT, "next timeout due to: %s", curr_tag);
+            break;
+        default:
+            break;
+    }
+}
+
+static void
+babel_fill_with_next_timeout(struct timeval *tv)
+{
+#if (defined NO_DEBUG)
+#define printIfMin(a,b,c,d)
+#else
+#define printIfMin(a,b,c,d) \
+  if (UNLIKELY(debug & BABEL_DEBUG_TIMEOUT)) {printIfMin(a,b,c,d);}
+
+    struct interface *ifp = NULL;
+    struct listnode *linklist_node = NULL;
+
+    *tv = check_neighbours_timeout;
+    printIfMin(tv, 0, "check_neighbours_timeout", NULL);
+    timeval_min_sec(tv, expiry_time);
+    printIfMin(tv, 1, "expiry_time", NULL);
+    timeval_min_sec(tv, source_expiry_time);
+    printIfMin(tv, 1, "source_expiry_time", NULL);
+    timeval_min(tv, &resend_time);
+    printIfMin(tv, 1, "resend_time", NULL);
+    FOR_ALL_INTERFACES(ifp, linklist_node) {
+        babel_interface_nfo *babel_ifp = NULL;
+        if(!if_up(ifp))
+            continue;
+        babel_ifp = babel_get_if_nfo(ifp);
+        timeval_min(tv, &babel_ifp->flush_timeout);
+        printIfMin(tv, 1, "flush_timeout", ifp->name);
+        timeval_min(tv, &babel_ifp->hello_timeout);
+        printIfMin(tv, 1, "hello_timeout", ifp->name);
+        timeval_min(tv, &babel_ifp->update_timeout);
+        printIfMin(tv, 1, "update_timeout", ifp->name);
+        timeval_min(tv, &babel_ifp->update_flush_timeout);
+        printIfMin(tv, 1, "update_flush_timeout",ifp->name);
+    }
+    timeval_min(tv, &unicast_flush_timeout);
+    printIfMin(tv, 1, "unicast_flush_timeout", NULL);
+    printIfMin(tv, 2, NULL, NULL);
+#undef printIfMin
+#endif
+}
+
+/* set the t_update thread of the babel routing process to be launch in
+ 'timeout' (approximate at the milisecond) */
+static void
+babel_set_timer(struct timeval *timeout)
+{
+    long msecs = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
+    if (babel_routing_process->t_update != NULL) {
+        thread_cancel(babel_routing_process->t_update);
+    }
+    babel_routing_process->t_update =
+    thread_add_timer_msec(master, &babel_main_loop, NULL, msecs);
+}
+
+/* Schedule a neighbours check after roughly 3/2 times msecs have elapsed. */
+void
+schedule_neighbours_check(int msecs, int override)
+{
+    struct timeval timeout;
+
+    timeval_add_msec(&timeout, &babel_now, roughly(msecs * 3 / 2));
+    if(override)
+        check_neighbours_timeout = timeout;
+    else
+        timeval_min(&check_neighbours_timeout, &timeout);
+}
+
+int
+resize_receive_buffer(int size)
+{
+    if(size <= receive_buffer_size)
+        return 0;
+
+    if(receive_buffer == NULL) {
+        receive_buffer = malloc(size);
+        if(receive_buffer == NULL) {
+            zlog_err("malloc(receive_buffer): %s", safe_strerror(errno));
+            return -1;
+        }
+        receive_buffer_size = size;
+    } else {
+        unsigned char *new;
+        new = realloc(receive_buffer, size);
+        if(new == NULL) {
+            zlog_err("realloc(receive_buffer): %s", safe_strerror(errno));
+            return -1;
+        }
+        receive_buffer = new;
+        receive_buffer_size = size;
+    }
+    return 1;
+}
+
+static void
+babel_distribute_update (struct distribute *dist)
+{
+    struct interface *ifp;
+    babel_interface_nfo *babel_ifp;
+    struct access_list *alist;
+    struct prefix_list *plist;
+
+    if (! dist->ifname)
+        return;
+
+    ifp = if_lookup_by_name (dist->ifname);
+    if (ifp == NULL)
+        return;
+
+    babel_ifp = babel_get_if_nfo(ifp);
+
+    if (dist->list[DISTRIBUTE_IN]) {
+        alist = access_list_lookup (AFI_IP6, dist->list[DISTRIBUTE_IN]);
+        if (alist)
+            babel_ifp->list[BABEL_FILTER_IN] = alist;
+        else
+            babel_ifp->list[BABEL_FILTER_IN] = NULL;
+    } else {
+        babel_ifp->list[BABEL_FILTER_IN] = NULL;
+    }
+
+    if (dist->list[DISTRIBUTE_OUT]) {
+        alist = access_list_lookup (AFI_IP6, dist->list[DISTRIBUTE_OUT]);
+        if (alist)
+            babel_ifp->list[BABEL_FILTER_OUT] = alist;
+        else
+            babel_ifp->list[BABEL_FILTER_OUT] = NULL;
+    } else {
+        babel_ifp->list[BABEL_FILTER_OUT] = NULL;
+    }
+
+    if (dist->prefix[DISTRIBUTE_IN]) {
+        plist = prefix_list_lookup (AFI_IP6, dist->prefix[DISTRIBUTE_IN]);
+        if (plist)
+            babel_ifp->prefix[BABEL_FILTER_IN] = plist;
+        else
+            babel_ifp->prefix[BABEL_FILTER_IN] = NULL;
+    } else {
+        babel_ifp->prefix[BABEL_FILTER_IN] = NULL;
+    }
+
+    if (dist->prefix[DISTRIBUTE_OUT]) {
+        plist = prefix_list_lookup (AFI_IP6, dist->prefix[DISTRIBUTE_OUT]);
+        if (plist)
+            babel_ifp->prefix[BABEL_FILTER_OUT] = plist;
+        else
+            babel_ifp->prefix[BABEL_FILTER_OUT] = NULL;
+    } else {
+        babel_ifp->prefix[BABEL_FILTER_OUT] = NULL;
+    }
+}
+
+static void
+babel_distribute_update_interface (struct interface *ifp)
+{
+    struct distribute *dist;
+
+    dist = distribute_lookup (ifp->name);
+    if (dist)
+        babel_distribute_update (dist);
+}
+
+/* Update all interface's distribute list. */
+static void
+babel_distribute_update_all (struct prefix_list *notused)
+{
+    struct interface *ifp;
+    struct listnode *node;
+
+    for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
+        babel_distribute_update_interface (ifp);
+}
+
+static void
+babel_distribute_update_all_wrapper (struct access_list *notused)
+{
+    babel_distribute_update_all(NULL);
+}
+
+
+/* [Command] */
+DEFUN (router_babel,
+       router_babel_cmd,
+       "router babel",
+       "Enable a routing process\n"
+       "Make Babel instance command\n"
+       "No attributes\n")
+{
+    int ret;
+
+    vty->node = BABEL_NODE;
+
+    if (!babel_routing_process) {
+        ret = babel_create_routing_process ();
+
+        /* Notice to user we couldn't create Babel. */
+        if (ret < 0) {
+            zlog_warn ("can't create Babel");
+            return CMD_WARNING;
+        }
+    }
+
+    return CMD_SUCCESS;
+}
+
+/* [Command] */
+DEFUN (no_router_babel,
+       no_router_babel_cmd,
+       "no router babel",
+       NO_STR
+       "Disable a routing process\n"
+       "Remove Babel instance command\n"
+       "No attributes\n")
+{
+    if(babel_routing_process)
+        babel_clean_routing_process();
+    return CMD_SUCCESS;
+}
+
+/* [Babel Command] */
+DEFUN (babel_set_resend_delay,
+       babel_set_resend_delay_cmd,
+       "babel resend-delay <20-655340>",
+       "Babel commands\n"
+       "Time before resending a message\n"
+       "Milliseconds\n")
+{
+    int interval;
+
+    VTY_GET_INTEGER_RANGE("milliseconds", interval, argv[0], 20, 10 * 0xFFFE);
+
+    resend_delay = interval;
+    return CMD_SUCCESS;
+}
+
+void
+babeld_quagga_init(void)
+{
+
+    install_node(&cmd_babel_node, &babel_config_write);
+
+    install_element(CONFIG_NODE, &router_babel_cmd);
+    install_element(CONFIG_NODE, &no_router_babel_cmd);
+
+    install_default(BABEL_NODE);
+    install_element(BABEL_NODE, &babel_set_resend_delay_cmd);
+
+    babel_if_init();
+
+    /* Access list install. */
+    access_list_init ();
+    access_list_add_hook (babel_distribute_update_all_wrapper);
+    access_list_delete_hook (babel_distribute_update_all_wrapper);
+
+    /* Prefix list initialize.*/
+    prefix_list_init ();
+    prefix_list_add_hook (babel_distribute_update_all);
+    prefix_list_delete_hook (babel_distribute_update_all);
+
+    /* Distribute list install. */
+    distribute_list_init (BABEL_NODE);
+    distribute_list_add_hook (babel_distribute_update);
+    distribute_list_delete_hook (babel_distribute_update);
+}
+
+/* Stubs to adapt Babel's filtering calls to Quagga's infrastructure. */
+
+int
+input_filter(const unsigned char *id,
+             const unsigned char *prefix, unsigned short plen,
+             const unsigned char *neigh, unsigned int ifindex)
+{
+    return babel_filter(0, prefix, plen, ifindex);
+}
+
+int
+output_filter(const unsigned char *id, const unsigned char *prefix,
+              unsigned short plen, unsigned int ifindex)
+{
+    return babel_filter(1, prefix, plen, ifindex);
+}
+
+/* There's no redistribute filter in Quagga -- the zebra daemon does its
+   own filtering. */
+int
+redistribute_filter(const unsigned char *prefix, unsigned short plen,
+                    unsigned int ifindex, int proto)
+{
+    return 0;
+}
+
diff --git a/babeld/babeld.conf.sample b/babeld/babeld.conf.sample
new file mode 100644
index 0000000..a4924ec
--- /dev/null
+++ b/babeld/babeld.conf.sample
@@ -0,0 +1,30 @@
+debug babel common
+!debug babel kernel
+!debug babel filter
+!debug babel timeout
+!debug babel interface
+!debug babel route
+!debug babel all
+
+router babel
+! network wlan0
+! network eth0
+! redistribute kernel
+! no redistribute static
+
+! The defaults are fine for a wireless interface
+
+!interface wlan0
+
+! A few optimisation tweaks are optional but recommended on a wired interface
+! Disable link quality estimation, enable split horizon processing, and
+! increase the hello and update intervals.
+
+!interface eth0
+! babel wired
+! babel split-horizon
+! babel hello-interval 12000
+! babel update-interval 36000
+
+! log file /var/log/quagga/babeld.log
+log stdout
diff --git a/babeld/babeld.h b/babeld/babeld.h
new file mode 100644
index 0000000..b19ae0f
--- /dev/null
+++ b/babeld/babeld.h
@@ -0,0 +1,141 @@
+/*  
+ *  This file is free software: you may copy, redistribute 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.  
+ *  
+ *  This file is distributed in the hope that it will be useful, but  
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+ *  General Public License for more details.  
+ *  
+ *  You should have received a copy of the GNU General Public License  
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ *  
+ * This file incorporates work covered by the following copyright and  
+ * permission notice:  
+ *  
+Copyright (c) 2007, 2008 by Juliusz Chroboczek
+Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
+
+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
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#ifndef BABEL_BABELD_H
+#define BABEL_BABELD_H
+
+#include <zebra.h>
+#include "vty.h"
+
+#define INFINITY ((unsigned short)(~0))
+
+#ifndef RTPROT_BABEL
+#define RTPROT_BABEL 42
+#endif
+
+#define RTPROT_BABEL_LOCAL -2
+
+#undef MAX
+#undef MIN
+
+#define MAX(x,y) ((x)<=(y)?(y):(x))
+#define MIN(x,y) ((x)<=(y)?(x):(y))
+
+#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
+/* nothing */
+#elif defined(__GNUC__)
+#define inline __inline
+#if  (__GNUC__ >= 3)
+#define restrict __restrict
+#else
+#define restrict /**/
+#endif
+#else
+#define inline /**/
+#define restrict /**/
+#endif
+
+#if defined(__GNUC__) && (__GNUC__ >= 3)
+#define ATTRIBUTE(x) __attribute__ (x)
+#define LIKELY(_x) __builtin_expect(!!(_x), 1)
+#define UNLIKELY(_x) __builtin_expect(!!(_x), 0)
+#else
+#define ATTRIBUTE(x) /**/
+#define LIKELY(_x) !!(_x)
+#define UNLIKELY(_x) !!(_x)
+#endif
+
+#if defined(__GNUC__) && (__GNUC__ >= 4) && (__GNUC_MINOR__ >= 3)
+#define COLD __attribute__ ((cold))
+#else
+#define COLD /**/
+#endif
+
+#ifndef IF_NAMESIZE
+#include <sys/socket.h>
+#include <net/if.h>
+#endif
+
+#ifdef HAVE_VALGRIND
+#include <valgrind/memcheck.h>
+#else
+#ifndef VALGRIND_MAKE_MEM_UNDEFINED
+#define VALGRIND_MAKE_MEM_UNDEFINED(a, b) do {} while(0)
+#endif
+#ifndef VALGRIND_CHECK_MEM_IS_DEFINED
+#define VALGRIND_CHECK_MEM_IS_DEFINED(a, b) do {} while(0)
+#endif
+#endif
+
+
+#define BABEL_VTY_PORT 2609
+#define BABEL_DEFAULT_CONFIG "babeld.conf"
+#define BABEL_VERSION "0.1 for quagga"
+
+/* Values in milliseconds */
+#define BABEL_DEFAULT_HELLO_INTERVAL 4000
+#define BABEL_DEFAULT_UPDATE_INTERVAL 16000
+#define BABEL_DEFAULT_RESEND_DELAY 2000
+
+
+/* Babel socket. */
+extern int protocol_socket;
+
+/* Babel structure. */
+struct babel
+{
+    /* Babel threads. */
+    struct thread *t_read;    /* on Babel protocol's socket */
+    struct thread *t_update;  /* timers */
+};
+
+
+extern void babeld_quagga_init(void);
+extern int input_filter(const unsigned char *id,
+                        const unsigned char *prefix, unsigned short plen,
+                        const unsigned char *neigh, unsigned int ifindex);
+extern int output_filter(const unsigned char *id, const unsigned char *prefix,
+                         unsigned short plen, unsigned int ifindex);
+extern int redistribute_filter(const unsigned char *prefix, unsigned short plen,
+                               unsigned int ifindex, int proto);
+extern int resize_receive_buffer(int size);
+extern void schedule_neighbours_check(int msecs, int override);
+
+
+#endif /* BABEL_BABELD_H */
diff --git a/babeld/kernel.c b/babeld/kernel.c
new file mode 100644
index 0000000..c31f617
--- /dev/null
+++ b/babeld/kernel.c
@@ -0,0 +1,76 @@
+/*  
+ *  This file is free software: you may copy, redistribute 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.  
+ *  
+ *  This file is distributed in the hope that it will be useful, but  
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+ *  General Public License for more details.  
+ *  
+ *  You should have received a copy of the GNU General Public License  
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ *  
+ * This file incorporates work covered by the following copyright and  
+ * permission notice:  
+ *  
+
+Copyright 2007, 2008 by Grégoire Henry, Julien Cristau and Juliusz Chroboczek
+
+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
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#include <sys/time.h>
+#include <sys/param.h>
+#include <time.h>
+
+#include "babeld.h"
+
+#include "kernel_zebra.c"
+
+/* Like gettimeofday, but returns monotonic time.  If POSIX clocks are not
+   available, falls back to gettimeofday but enforces monotonicity. */
+int
+gettime(struct timeval *tv)
+{
+    return quagga_gettime(QUAGGA_CLK_MONOTONIC, tv);
+}
+
+/* If /dev/urandom doesn't exist, this will fail with ENOENT, which the
+   caller will deal with gracefully. */
+
+int
+read_random_bytes(void *buf, size_t len)
+{
+    int fd;
+    int rc;
+
+    fd = open("/dev/urandom", O_RDONLY);
+    if(fd < 0) {
+        rc = -1;
+    } else {
+        rc = read(fd, buf, len);
+        if(rc < 0 || (unsigned) rc < len)
+            rc = -1;
+        close(fd);
+    }
+    return rc;
+}
+
diff --git a/babeld/kernel.h b/babeld/kernel.h
new file mode 100644
index 0000000..e8c8f9b
--- /dev/null
+++ b/babeld/kernel.h
@@ -0,0 +1,69 @@
+/*  
+ *  This file is free software: you may copy, redistribute 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.  
+ *  
+ *  This file is distributed in the hope that it will be useful, but  
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+ *  General Public License for more details.  
+ *  
+ *  You should have received a copy of the GNU General Public License  
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ *  
+ * This file incorporates work covered by the following copyright and  
+ * permission notice:  
+ *  
+Copyright (c) 2007, 2008 by Juliusz Chroboczek
+
+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
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#include <netinet/in.h>
+#include "babel_main.h"
+#include "if.h"
+
+#define KERNEL_INFINITY 0xFFFF
+
+struct kernel_route {
+    unsigned char prefix[16];
+    int plen;
+    int metric;
+    unsigned int ifindex;
+    int proto;
+    unsigned char gw[16];
+};
+
+#define ROUTE_FLUSH 0
+#define ROUTE_ADD 1
+#define ROUTE_MODIFY 2
+
+extern int export_table, import_table;
+
+int kernel_interface_operational(struct interface *interface);
+int kernel_interface_mtu(struct interface *interface);
+int kernel_interface_wireless(struct interface *interface);
+int kernel_route(int operation, const unsigned char *dest, unsigned short plen,
+                 const unsigned char *gate, int ifindex, unsigned int metric,
+                 const unsigned char *newgate, int newifindex,
+                 unsigned int newmetric);
+int if_eui64(char *ifname, int ifindex, unsigned char *eui);
+int gettime(struct timeval *tv);
+int read_random_bytes(void *buf, size_t len);
diff --git a/babeld/kernel_zebra.c b/babeld/kernel_zebra.c
new file mode 100644
index 0000000..db7d0b3
--- /dev/null
+++ b/babeld/kernel_zebra.c
@@ -0,0 +1,275 @@
+/*  
+ *  This file is free software: you may copy, redistribute 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.  
+ *  
+ *  This file is distributed in the hope that it will be useful, but  
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+ *  General Public License for more details.  
+ *  
+ *  You should have received a copy of the GNU General Public License  
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ *  
+ * This file incorporates work covered by the following copyright and  
+ * permission notice:  
+ *  
+Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
+
+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
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <arpa/inet.h>
+
+#include <zebra.h>
+#include "prefix.h"
+#include "zclient.h"
+#include "kernel.h"
+#include "privs.h"
+#include "command.h"
+#include "vty.h"
+#include "memory.h"
+#include "thread.h"
+
+#include "util.h"
+#include "babel_interface.h"
+#include "babel_zebra.h"
+
+
+static int
+kernel_route_v4(int add, const unsigned char *pref, unsigned short plen,
+                const unsigned char *gate, int ifindex,
+                unsigned int metric);
+static int
+kernel_route_v6(int add, const unsigned char *pref, unsigned short plen,
+                const unsigned char *gate, int ifindex,
+                unsigned int metric);
+
+int
+kernel_interface_operational(struct interface *interface)
+{
+    return if_is_operative(interface);
+}
+
+int
+kernel_interface_mtu(struct interface *interface)
+{
+    return MIN(interface->mtu, interface->mtu6);
+}
+
+int
+kernel_interface_wireless(struct interface *interface)
+{
+    return 0;
+}
+
+int
+kernel_route(int operation, const unsigned char *pref, unsigned short plen,
+             const unsigned char *gate, int ifindex, unsigned int metric,
+             const unsigned char *newgate, int newifindex,
+             unsigned int newmetric)
+{
+    int rc;
+    int ipv4;
+
+    /* Check that the protocol family is consistent. */
+    if(plen >= 96 && v4mapped(pref)) {
+        if(!v4mapped(gate)) {
+            errno = EINVAL;
+            return -1;
+        }
+        ipv4 = 1;
+    } else {
+        if(v4mapped(gate)) {
+            errno = EINVAL;
+            return -1;
+        }
+        ipv4 = 0;
+    }
+
+    switch (operation) {
+        case ROUTE_ADD:
+            return ipv4 ?
+                   kernel_route_v4(1, pref, plen, gate, ifindex, metric):
+                   kernel_route_v6(1, pref, plen, gate, ifindex, metric);
+            break;
+        case ROUTE_FLUSH:
+            return ipv4 ?
+                   kernel_route_v4(0, pref, plen, gate, ifindex, metric):
+                   kernel_route_v6(0, pref, plen, gate, ifindex, metric);
+            break;
+        case ROUTE_MODIFY:
+            if(newmetric == metric && memcmp(newgate, gate, 16) == 0 &&
+               newifindex == ifindex)
+                return 0;
+            debugf(BABEL_DEBUG_ROUTE, "Modify route: delete old; add new.");
+            rc = ipv4 ?
+                kernel_route_v4(0, pref, plen, gate, ifindex, metric):
+                kernel_route_v6(0, pref, plen, gate, ifindex, metric);
+
+            if (rc < 0)
+                return -1;
+
+            rc = ipv4 ?
+                kernel_route_v4(1, pref, plen, newgate, newifindex, newmetric):
+                kernel_route_v6(1, pref, plen, newgate, newifindex, newmetric);
+
+            return rc;
+            break;
+        default:
+            zlog_err("this should never appens (false value - kernel_route)");
+            assert(0);
+            exit(1);
+            break;
+    }
+}
+
+static int
+kernel_route_v4(int add,
+                const unsigned char *pref, unsigned short plen,
+                const unsigned char *gate, int ifindex, unsigned int metric)
+{
+    struct zapi_ipv4 api;               /* quagga's communication system */
+    struct prefix_ipv4 quagga_prefix;   /* quagga's prefix */
+    struct in_addr babel_prefix_addr;   /* babeld's prefix addr */
+    struct in_addr nexthop;             /* next router to go */
+    struct in_addr *nexthop_pointer = &nexthop; /* it's an array! */
+
+    /* convert to be understandable by quagga */
+    /* convert given addresses */
+    uchar_to_inaddr(&babel_prefix_addr, pref);
+    uchar_to_inaddr(&nexthop, gate);
+
+    /* make prefix structure */
+    memset (&quagga_prefix, 0, sizeof(quagga_prefix));
+    quagga_prefix.family = AF_INET;
+    IPV4_ADDR_COPY (&quagga_prefix.prefix, &babel_prefix_addr);
+    quagga_prefix.prefixlen = plen - 96; /* our plen is for v4mapped's addr */
+    apply_mask_ipv4(&quagga_prefix);
+
+    api.type  = ZEBRA_ROUTE_BABEL;
+    api.flags = 0;
+    api.message = 0;
+    api.safi = SAFI_UNICAST;
+
+    /* Unlike the native Linux and BSD interfaces, Quagga doesn't like
+       there to be both and IPv4 nexthop and an ifindex.  Omit the
+       ifindex, and assume that the connected prefixes be set up
+       correctly. */
+
+    SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
+    api.ifindex_num = 0;
+    if(metric >= KERNEL_INFINITY) {
+        api.flags = ZEBRA_FLAG_BLACKHOLE;
+        api.nexthop_num = 0;
+    } else {
+        api.nexthop_num = 1;
+        api.nexthop = &nexthop_pointer;
+        SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
+        api.metric = metric;
+    }
+
+    debugf(BABEL_DEBUG_ROUTE, "%s route (ipv4) to zebra",
+           add ? "adding" : "removing" );
+    return zapi_ipv4_route (add ? ZEBRA_IPV4_ROUTE_ADD :
+                                  ZEBRA_IPV4_ROUTE_DELETE,
+                            zclient, &quagga_prefix, &api);
+}
+
+static int
+kernel_route_v6(int add, const unsigned char *pref, unsigned short plen,
+                const unsigned char *gate, int ifindex, unsigned int metric)
+{
+    unsigned int tmp_ifindex = ifindex; /* (for typing) */
+    struct zapi_ipv6 api;               /* quagga's communication system */
+    struct prefix_ipv6 quagga_prefix;   /* quagga's prefix */
+    struct in6_addr babel_prefix_addr;  /* babeld's prefix addr */
+    struct in6_addr nexthop;            /* next router to go */
+    struct in6_addr *nexthop_pointer = &nexthop;
+
+    /* convert to be understandable by quagga */
+    /* convert given addresses */
+    uchar_to_in6addr(&babel_prefix_addr, pref);
+    uchar_to_in6addr(&nexthop, gate);
+
+    /* make prefix structure */
+    memset (&quagga_prefix, 0, sizeof(quagga_prefix));
+    quagga_prefix.family = AF_INET6;
+    IPV6_ADDR_COPY (&quagga_prefix.prefix, &babel_prefix_addr);
+    quagga_prefix.prefixlen = plen;
+    apply_mask_ipv6(&quagga_prefix);
+
+    api.type  = ZEBRA_ROUTE_BABEL;
+    api.flags = 0;
+    api.message = 0;
+    api.safi = SAFI_UNICAST;
+    SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
+    if(metric >= KERNEL_INFINITY) {
+        api.flags = ZEBRA_FLAG_BLACKHOLE;
+        api.nexthop_num = 0;
+        api.ifindex_num = 0;
+    } else {
+        api.nexthop_num = 1;
+        api.nexthop = &nexthop_pointer;
+        SET_FLAG(api.message, ZAPI_MESSAGE_IFINDEX);
+        api.ifindex_num = 1;
+        api.ifindex = &tmp_ifindex;
+        SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
+        api.metric = metric;
+    }
+
+    debugf(BABEL_DEBUG_ROUTE, "%s route (ipv6) to zebra",
+           add ? "adding" : "removing" );
+    return zapi_ipv6_route (add ? ZEBRA_IPV6_ROUTE_ADD :
+                                  ZEBRA_IPV6_ROUTE_DELETE,
+                            zclient, &quagga_prefix, &api);
+}
+
+int
+if_eui64(char *ifname, int ifindex, unsigned char *eui)
+{
+    struct interface *ifp = if_lookup_by_index(ifindex);
+    if (ifp == NULL) {
+        return -1;
+    }
+#ifdef HAVE_STRUCT_SOCKADDR_DL
+    u_char len = ifp->sdl.sdl_alen;
+    char *tmp = ifp->sdl.sdl_data + ifp->sdl.sdl_nlen;
+#else
+    u_char len = (u_char) ifp->hw_addr_len;
+    char *tmp = (void*) ifp->hw_addr;
+#endif
+    if (len == 8) {
+        memcpy(eui, tmp, 8);
+        eui[0] ^= 2;
+    } else if (len == 6) {
+        memcpy(eui,   tmp,   3);
+        eui[3] = 0xFF;
+        eui[4] = 0xFE;
+        memcpy(eui+5, tmp+3, 3);
+    } else {
+        return -1;
+    }
+    return 0;
+}
diff --git a/babeld/message.c b/babeld/message.c
new file mode 100644
index 0000000..9dcfc67
--- /dev/null
+++ b/babeld/message.c
@@ -0,0 +1,1561 @@
+/*  
+ *  This file is free software: you may copy, redistribute 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.  
+ *  
+ *  This file is distributed in the hope that it will be useful, but  
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+ *  General Public License for more details.  
+ *  
+ *  You should have received a copy of the GNU General Public License  
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ *  
+ * This file incorporates work covered by the following copyright and  
+ * permission notice:  
+ *  
+
+Copyright (c) 2007, 2008 by Juliusz Chroboczek
+
+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
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#include <zebra.h>
+#include "if.h"
+
+#include "babeld.h"
+#include "util.h"
+#include "net.h"
+#include "babel_interface.h"
+#include "source.h"
+#include "neighbour.h"
+#include "route.h"
+#include "xroute.h"
+#include "resend.h"
+#include "message.h"
+#include "kernel.h"
+
+unsigned char packet_header[4] = {42, 2};
+
+int split_horizon = 1;
+
+unsigned short myseqno = 0;
+struct timeval seqno_time = {0, 0};
+
+#define UNICAST_BUFSIZE 1024
+int unicast_buffered = 0;
+unsigned char *unicast_buffer = NULL;
+struct neighbour *unicast_neighbour = NULL;
+struct timeval unicast_flush_timeout = {0, 0};
+
+static const unsigned char v4prefix[16] =
+    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };
+
+/* Parse a network prefix, encoded in the somewhat baroque compressed
+   representation used by Babel.  Return the number of bytes parsed. */
+static int
+network_prefix(int ae, int plen, unsigned int omitted,
+               const unsigned char *p, const unsigned char *dp,
+               unsigned int len, unsigned char *p_r)
+{
+    unsigned pb;
+    unsigned char prefix[16];
+    int ret = -1;
+
+    if(plen >= 0)
+        pb = (plen + 7) / 8;
+    else if(ae == 1)
+        pb = 4;
+    else
+        pb = 16;
+
+    if(pb > 16)
+        return -1;
+
+    memset(prefix, 0, 16);
+
+    switch(ae) {
+    case 0:
+        ret = 0;
+        break;
+    case 1:
+        if(omitted > 4 || pb > 4 || (pb > omitted && len < pb - omitted))
+            return -1;
+        memcpy(prefix, v4prefix, 12);
+        if(omitted) {
+            if (dp == NULL || !v4mapped(dp)) return -1;
+            memcpy(prefix, dp, 12 + omitted);
+        }
+        if(pb > omitted) memcpy(prefix + 12 + omitted, p, pb - omitted);
+        ret = pb - omitted;
+        break;
+    case 2:
+        if(omitted > 16 || (pb > omitted && len < pb - omitted)) return -1;
+        if(omitted) {
+            if (dp == NULL || v4mapped(dp)) return -1;
+            memcpy(prefix, dp, omitted);
+        }
+        if(pb > omitted) memcpy(prefix + omitted, p, pb - omitted);
+        ret = pb - omitted;
+        break;
+    case 3:
+        if(pb > 8 && len < pb - 8) return -1;
+        prefix[0] = 0xfe;
+        prefix[1] = 0x80;
+        if(pb > 8) memcpy(prefix + 8, p, pb - 8);
+        ret = pb - 8;
+        break;
+    default:
+        return -1;
+    }
+
+    mask_prefix(p_r, prefix, plen < 0 ? 128 : ae == 1 ? plen + 96 : plen);
+    return ret;
+}
+
+static void
+parse_route_attributes(const unsigned char *a, int alen,
+                       unsigned char *channels)
+{
+    int type, len, i = 0;
+
+    while(i < alen) {
+        type = a[i];
+        if(type == 0) {
+            i++;
+            continue;
+        }
+
+        if(i + 1 > alen) {
+            fprintf(stderr, "Received truncated attributes.\n");
+            return;
+        }
+        len = a[i + 1];
+        if(i + len > alen) {
+            fprintf(stderr, "Received truncated attributes.\n");
+            return;
+        }
+
+        if(type == 1) {
+            /* Nothing. */
+        } else if(type == 2) {
+            if(len > DIVERSITY_HOPS) {
+                fprintf(stderr,
+                        "Received overlong channel information (%d > %d).\n",
+                        len, DIVERSITY_HOPS);
+                len = DIVERSITY_HOPS;
+            }
+            if(memchr(a + i + 2, 0, len) != NULL) {
+                /* 0 is reserved. */
+                fprintf(stderr, "Channel information contains 0!");
+                return;
+            }
+            memset(channels, 0, DIVERSITY_HOPS);
+            memcpy(channels, a + i + 2, len);
+        } else {
+            fprintf(stderr, "Received unknown route attribute %d.\n", type);
+        }
+
+        i += len + 2;
+    }
+}
+
+static int
+network_address(int ae, const unsigned char *a, unsigned int len,
+                unsigned char *a_r)
+{
+    return network_prefix(ae, -1, 0, a, NULL, len, a_r);
+}
+
+static int
+channels_len(unsigned char *channels)
+{
+    unsigned char *p = memchr(channels, 0, DIVERSITY_HOPS);
+    return p ? (p - channels) : DIVERSITY_HOPS;
+}
+
+void
+parse_packet(const unsigned char *from, struct interface *ifp,
+             const unsigned char *packet, int packetlen)
+{
+    int i;
+    const unsigned char *message;
+    unsigned char type, len;
+    int bodylen;
+    struct neighbour *neigh;
+    int have_router_id = 0, have_v4_prefix = 0, have_v6_prefix = 0,
+        have_v4_nh = 0, have_v6_nh = 0;
+    unsigned char router_id[8], v4_prefix[16], v6_prefix[16],
+        v4_nh[16], v6_nh[16];
+
+    if(!linklocal(from)) {
+        zlog_err("Received packet from non-local address %s.",
+                 format_address(from));
+        return;
+    }
+
+    if(packet[0] != 42) {
+        zlog_err("Received malformed packet on %s from %s.",
+                 ifp->name, format_address(from));
+        return;
+    }
+
+    if(packet[1] != 2) {
+        zlog_err("Received packet with unknown version %d on %s from %s.",
+                 packet[1], ifp->name, format_address(from));
+        return;
+    }
+
+    neigh = find_neighbour(from, ifp);
+    if(neigh == NULL) {
+        zlog_err("Couldn't allocate neighbour.");
+        return;
+    }
+
+    DO_NTOHS(bodylen, packet + 2);
+
+    if(bodylen + 4 > packetlen) {
+        zlog_err("Received truncated packet (%d + 4 > %d).",
+                 bodylen, packetlen);
+        bodylen = packetlen - 4;
+    }
+
+    i = 0;
+    while(i < bodylen) {
+        message = packet + 4 + i;
+        type = message[0];
+        if(type == MESSAGE_PAD1) {
+            debugf(BABEL_DEBUG_COMMON,"Received pad1 from %s on %s.",
+                   format_address(from), ifp->name);
+            i++;
+            continue;
+        }
+        if(i + 1 > bodylen) {
+            zlog_err("Received truncated message.");
+            break;
+        }
+        len = message[1];
+        if(i + len > bodylen) {
+            zlog_err("Received truncated message.");
+            break;
+        }
+
+        if(type == MESSAGE_PADN) {
+            debugf(BABEL_DEBUG_COMMON,"Received pad%d from %s on %s.",
+                   len, format_address(from), ifp->name);
+        } else if(type == MESSAGE_ACK_REQ) {
+            unsigned short nonce, interval;
+            if(len < 6) goto fail;
+            DO_NTOHS(nonce, message + 4);
+            DO_NTOHS(interval, message + 6);
+            debugf(BABEL_DEBUG_COMMON,"Received ack-req (%04X %d) from %s on %s.",
+                   nonce, interval, format_address(from), ifp->name);
+            send_ack(neigh, nonce, interval);
+        } else if(type == MESSAGE_ACK) {
+            debugf(BABEL_DEBUG_COMMON,"Received ack from %s on %s.",
+                   format_address(from), ifp->name);
+            /* Nothing right now */
+        } else if(type == MESSAGE_HELLO) {
+            unsigned short seqno, interval;
+            int changed;
+            if(len < 6) goto fail;
+            DO_NTOHS(seqno, message + 4);
+            DO_NTOHS(interval, message + 6);
+            debugf(BABEL_DEBUG_COMMON,"Received hello %d (%d) from %s on %s.",
+                   seqno, interval,
+                   format_address(from), ifp->name);
+            changed = update_neighbour(neigh, seqno, interval);
+            update_neighbour_metric(neigh, changed);
+            if(interval > 0)
+                schedule_neighbours_check(interval * 10, 0);
+        } else if(type == MESSAGE_IHU) {
+            unsigned short txcost, interval;
+            unsigned char address[16];
+            int rc;
+            if(len < 6) goto fail;
+            DO_NTOHS(txcost, message + 4);
+            DO_NTOHS(interval, message + 6);
+            rc = network_address(message[2], message + 8, len - 6, address);
+            if(rc < 0) goto fail;
+            debugf(BABEL_DEBUG_COMMON,"Received ihu %d (%d) from %s on %s for %s.",
+                   txcost, interval,
+                   format_address(from), ifp->name,
+                   format_address(address));
+            if(message[2] == 0 || is_interface_ll_address(ifp, address)) {
+                int changed = txcost != neigh->txcost;
+                neigh->txcost = txcost;
+                neigh->ihu_time = babel_now;
+                neigh->ihu_interval = interval;
+                update_neighbour_metric(neigh, changed);
+                if(interval > 0)
+                    schedule_neighbours_check(interval * 10 * 3, 0);
+            }
+        } else if(type == MESSAGE_ROUTER_ID) {
+            if(len < 10) {
+                have_router_id = 0;
+                goto fail;
+            }
+            memcpy(router_id, message + 4, 8);
+            have_router_id = 1;
+            debugf(BABEL_DEBUG_COMMON,"Received router-id %s from %s on %s.",
+                   format_eui64(router_id), format_address(from), ifp->name);
+        } else if(type == MESSAGE_NH) {
+            unsigned char nh[16];
+            int rc;
+            if(len < 2) {
+                have_v4_nh = 0;
+                have_v6_nh = 0;
+                goto fail;
+            }
+            rc = network_address(message[2], message + 4, len - 2,
+                                 nh);
+            if(rc < 0) {
+                have_v4_nh = 0;
+                have_v6_nh = 0;
+                goto fail;
+            }
+            debugf(BABEL_DEBUG_COMMON,"Received nh %s (%d) from %s on %s.",
+                   format_address(nh), message[2],
+                   format_address(from), ifp->name);
+            if(message[2] == 1) {
+                memcpy(v4_nh, nh, 16);
+                have_v4_nh = 1;
+            } else {
+                memcpy(v6_nh, nh, 16);
+                have_v6_nh = 1;
+            }
+        } else if(type == MESSAGE_UPDATE) {
+            unsigned char prefix[16], *nh;
+            unsigned char plen;
+            unsigned char channels[DIVERSITY_HOPS];
+            unsigned short interval, seqno, metric;
+            int rc, parsed_len;
+            if(len < 10) {
+                if(len < 2 || message[3] & 0x80)
+                    have_v4_prefix = have_v6_prefix = 0;
+                goto fail;
+            }
+            DO_NTOHS(interval, message + 6);
+            DO_NTOHS(seqno, message + 8);
+            DO_NTOHS(metric, message + 10);
+            if(message[5] == 0 ||
+               (message[3] == 1 ? have_v4_prefix : have_v6_prefix))
+                rc = network_prefix(message[2], message[4], message[5],
+                                    message + 12,
+                                    message[2] == 1 ? v4_prefix : v6_prefix,
+                                    len - 10, prefix);
+            else
+                rc = -1;
+            if(rc < 0) {
+                if(message[3] & 0x80)
+                    have_v4_prefix = have_v6_prefix = 0;
+                goto fail;
+            }
+            parsed_len = 10 + rc;
+
+            plen = message[4] + (message[2] == 1 ? 96 : 0);
+
+            if(message[3] & 0x80) {
+                if(message[2] == 1) {
+                    memcpy(v4_prefix, prefix, 16);
+                    have_v4_prefix = 1;
+                } else {
+                    memcpy(v6_prefix, prefix, 16);
+                    have_v6_prefix = 1;
+                }
+            }
+            if(message[3] & 0x40) {
+                if(message[2] == 1) {
+                    memset(router_id, 0, 4);
+                    memcpy(router_id + 4, prefix + 12, 4);
+                } else {
+                    memcpy(router_id, prefix + 8, 8);
+                }
+                have_router_id = 1;
+            }
+            if(!have_router_id && message[2] != 0) {
+                zlog_err("Received prefix with no router id.");
+                goto fail;
+            }
+            debugf(BABEL_DEBUG_COMMON,"Received update%s%s for %s from %s on %s.",
+                   (message[3] & 0x80) ? "/prefix" : "",
+                   (message[3] & 0x40) ? "/id" : "",
+                   format_prefix(prefix, plen),
+                   format_address(from), ifp->name);
+
+            if(message[2] == 0) {
+                if(metric < 0xFFFF) {
+                    zlog_err("Received wildcard update with finite metric.");
+                    goto done;
+                }
+                retract_neighbour_routes(neigh);
+                goto done;
+            } else if(message[2] == 1) {
+                if(!have_v4_nh)
+                    goto fail;
+                nh = v4_nh;
+            } else if(have_v6_nh) {
+                nh = v6_nh;
+            } else {
+                nh = neigh->address;
+            }
+
+            if(message[2] == 1) {
+                if(!babel_get_if_nfo(ifp)->ipv4)
+                    goto done;
+            }
+
+            if((ifp->flags & BABEL_IF_FARAWAY)) {
+                channels[0] = 0;
+            } else {
+                /* This will be overwritten by parse_route_attributes below. */
+                if(metric < 256) {
+                    /* Assume non-interfering (wired) link. */
+                    channels[0] = 0;
+                } else {
+                    /* Assume interfering. */
+                    channels[0] = BABEL_IF_CHANNEL_INTERFERING;
+                    channels[1] = 0;
+                }
+
+                if(parsed_len < len)
+                    parse_route_attributes(message + 2 + parsed_len,
+                                           len - parsed_len, channels);
+            }
+
+            update_route(router_id, prefix, plen, seqno, metric, interval,
+                         neigh, nh,
+                         channels, channels_len(channels));
+        } else if(type == MESSAGE_REQUEST) {
+            unsigned char prefix[16], plen;
+            int rc;
+            if(len < 2) goto fail;
+            rc = network_prefix(message[2], message[3], 0,
+                                message + 4, NULL, len - 2, prefix);
+            if(rc < 0) goto fail;
+            plen = message[3] + (message[2] == 1 ? 96 : 0);
+            debugf(BABEL_DEBUG_COMMON,"Received request for %s from %s on %s.",
+                   message[2] == 0 ? "any" : format_prefix(prefix, plen),
+                   format_address(from), ifp->name);
+            if(message[2] == 0) {
+                struct babel_interface *babel_ifp =babel_get_if_nfo(neigh->ifp);
+                /* If a neighbour is requesting a full route dump from us,
+                   we might as well send it an IHU. */
+                send_ihu(neigh, NULL);
+                /* Since nodes send wildcard requests on boot, booting
+                   a large number of nodes at the same time may cause an
+                   update storm.  Ignore a wildcard request that happens
+                   shortly after we sent a full update. */
+                if(babel_ifp->last_update_time <
+                   (time_t)(babel_now.tv_sec -
+                            MAX(babel_ifp->hello_interval / 100, 1)))
+                    send_update(neigh->ifp, 0, NULL, 0);
+            } else {
+                send_update(neigh->ifp, 0, prefix, plen);
+            }
+        } else if(type == MESSAGE_MH_REQUEST) {
+            unsigned char prefix[16], plen;
+            unsigned short seqno;
+            int rc;
+            if(len < 14) goto fail;
+            DO_NTOHS(seqno, message + 4);
+            rc = network_prefix(message[2], message[3], 0,
+                                message + 16, NULL, len - 14, prefix);
+            if(rc < 0) goto fail;
+            plen = message[3] + (message[2] == 1 ? 96 : 0);
+            debugf(BABEL_DEBUG_COMMON,"Received request (%d) for %s from %s on %s (%s, %d).",
+                   message[6],
+                   format_prefix(prefix, plen),
+                   format_address(from), ifp->name,
+                   format_eui64(message + 8), seqno);
+            handle_request(neigh, prefix, plen, message[6],
+                           seqno, message + 8);
+        } else {
+            debugf(BABEL_DEBUG_COMMON,"Received unknown packet type %d from %s on %s.",
+                   type, format_address(from), ifp->name);
+        }
+    done:
+        i += len + 2;
+        continue;
+
+    fail:
+        zlog_err("Couldn't parse packet (%d, %d) from %s on %s.",
+                 message[0], message[1], format_address(from), ifp->name);
+        goto done;
+    }
+    return;
+}
+
+/* Under normal circumstances, there are enough moderation mechanisms
+   elsewhere in the protocol to make sure that this last-ditch check
+   should never trigger.  But I'm superstitious. */
+
+static int
+check_bucket(struct interface *ifp)
+{
+    babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp);
+    if(babel_ifp->bucket <= 0) {
+        int seconds = babel_now.tv_sec - babel_ifp->bucket_time;
+        if(seconds > 0) {
+            babel_ifp->bucket = MIN(BUCKET_TOKENS_MAX,
+                              seconds * BUCKET_TOKENS_PER_SEC);
+        }
+        /* Reset bucket time unconditionally, in case clock is stepped. */
+        babel_ifp->bucket_time = babel_now.tv_sec;
+    }
+
+    if(babel_ifp->bucket > 0) {
+        babel_ifp->bucket--;
+        return 1;
+    } else {
+        return 0;
+    }
+}
+
+void
+flushbuf(struct interface *ifp)
+{
+    int rc;
+    struct sockaddr_in6 sin6;
+    babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp);
+
+    assert(babel_ifp->buffered <= babel_ifp->bufsize);
+
+    flushupdates(ifp);
+
+    if(babel_ifp->buffered > 0) {
+        debugf(BABEL_DEBUG_COMMON,"  (flushing %d buffered bytes on %s)",
+               babel_ifp->buffered, ifp->name);
+        if(check_bucket(ifp)) {
+            memset(&sin6, 0, sizeof(sin6));
+            sin6.sin6_family = AF_INET6;
+            memcpy(&sin6.sin6_addr, protocol_group, 16);
+            sin6.sin6_port = htons(protocol_port);
+            sin6.sin6_scope_id = ifp->ifindex;
+            DO_HTONS(packet_header + 2, babel_ifp->buffered);
+            rc = babel_send(protocol_socket,
+                            packet_header, sizeof(packet_header),
+                            babel_ifp->sendbuf, babel_ifp->buffered,
+                            (struct sockaddr*)&sin6, sizeof(sin6));
+            if(rc < 0)
+                zlog_err("send: %s", safe_strerror(errno));
+        } else {
+            zlog_err("Warning: bucket full, dropping packet to %s.",
+                     ifp->name);
+        }
+    }
+    VALGRIND_MAKE_MEM_UNDEFINED(babel_ifp->sendbuf, babel_ifp->bufsize);
+    babel_ifp->buffered = 0;
+    babel_ifp->have_buffered_hello = 0;
+    babel_ifp->have_buffered_id = 0;
+    babel_ifp->have_buffered_nh = 0;
+    babel_ifp->have_buffered_prefix = 0;
+    babel_ifp->flush_timeout.tv_sec = 0;
+    babel_ifp->flush_timeout.tv_usec = 0;
+}
+
+static void
+schedule_flush(struct interface *ifp)
+{
+    babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp);
+    unsigned msecs = jitter(babel_ifp, 0);
+    if(babel_ifp->flush_timeout.tv_sec != 0 &&
+       timeval_minus_msec(&babel_ifp->flush_timeout, &babel_now) < msecs)
+        return;
+    set_timeout(&babel_ifp->flush_timeout, msecs);
+}
+
+static void
+schedule_flush_now(struct interface *ifp)
+{
+    babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp);
+    /* Almost now */
+    unsigned msecs = roughly(10);
+    if(babel_ifp->flush_timeout.tv_sec != 0 &&
+       timeval_minus_msec(&babel_ifp->flush_timeout, &babel_now) < msecs)
+        return;
+    set_timeout(&babel_ifp->flush_timeout, msecs);
+}
+
+static void
+schedule_unicast_flush(unsigned msecs)
+{
+    if(!unicast_neighbour)
+        return;
+    if(unicast_flush_timeout.tv_sec != 0 &&
+       timeval_minus_msec(&unicast_flush_timeout, &babel_now) < msecs)
+        return;
+    unicast_flush_timeout.tv_usec = (babel_now.tv_usec + msecs * 1000) %1000000;
+    unicast_flush_timeout.tv_sec =
+        babel_now.tv_sec + (babel_now.tv_usec / 1000 + msecs) / 1000;
+}
+
+static void
+ensure_space(struct interface *ifp, int space)
+{
+    babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp);
+    if(babel_ifp->bufsize - babel_ifp->buffered < space)
+        flushbuf(ifp);
+}
+
+static void
+start_message(struct interface *ifp, int type, int len)
+{
+  babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp);
+    if(babel_ifp->bufsize - babel_ifp->buffered < len + 2)
+        flushbuf(ifp);
+    babel_ifp->sendbuf[babel_ifp->buffered++] = type;
+    babel_ifp->sendbuf[babel_ifp->buffered++] = len;
+}
+
+static void
+end_message(struct interface *ifp, int type, int bytes)
+{
+    babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp);
+    assert(babel_ifp->buffered >= bytes + 2 &&
+           babel_ifp->sendbuf[babel_ifp->buffered - bytes - 2] == type &&
+           babel_ifp->sendbuf[babel_ifp->buffered - bytes - 1] == bytes);
+    schedule_flush(ifp);
+}
+
+static void
+accumulate_byte(struct interface *ifp, unsigned char value)
+{
+    babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp);
+    babel_ifp->sendbuf[babel_ifp->buffered++] = value;
+}
+
+static void
+accumulate_short(struct interface *ifp, unsigned short value)
+{
+    babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp);
+    DO_HTONS(babel_ifp->sendbuf + babel_ifp->buffered, value);
+    babel_ifp->buffered += 2;
+}
+
+static void
+accumulate_bytes(struct interface *ifp,
+                 const unsigned char *value, unsigned len)
+{
+    babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp);
+    memcpy(babel_ifp->sendbuf + babel_ifp->buffered, value, len);
+    babel_ifp->buffered += len;
+}
+
+static int
+start_unicast_message(struct neighbour *neigh, int type, int len)
+{
+    if(unicast_neighbour) {
+        if(neigh != unicast_neighbour ||
+           unicast_buffered + len + 2 >=
+           MIN(UNICAST_BUFSIZE, babel_get_if_nfo(neigh->ifp)->bufsize))
+            flush_unicast(0);
+    }
+    if(!unicast_buffer)
+        unicast_buffer = malloc(UNICAST_BUFSIZE);
+    if(!unicast_buffer) {
+        zlog_err("malloc(unicast_buffer): %s", safe_strerror(errno));
+        return -1;
+    }
+
+    unicast_neighbour = neigh;
+
+    unicast_buffer[unicast_buffered++] = type;
+    unicast_buffer[unicast_buffered++] = len;
+    return 1;
+}
+
+static void
+end_unicast_message(struct neighbour *neigh, int type, int bytes)
+{
+    assert(unicast_neighbour == neigh && unicast_buffered >= bytes + 2 &&
+           unicast_buffer[unicast_buffered - bytes - 2] == type &&
+           unicast_buffer[unicast_buffered - bytes - 1] == bytes);
+    schedule_unicast_flush(jitter(babel_get_if_nfo(neigh->ifp), 0));
+}
+
+static void
+accumulate_unicast_byte(struct neighbour *neigh, unsigned char value)
+{
+    unicast_buffer[unicast_buffered++] = value;
+}
+
+static void
+accumulate_unicast_short(struct neighbour *neigh, unsigned short value)
+{
+    DO_HTONS(unicast_buffer + unicast_buffered, value);
+    unicast_buffered += 2;
+}
+
+static void
+accumulate_unicast_bytes(struct neighbour *neigh,
+                         const unsigned char *value, unsigned len)
+{
+    memcpy(unicast_buffer + unicast_buffered, value, len);
+    unicast_buffered += len;
+}
+
+void
+send_ack(struct neighbour *neigh, unsigned short nonce, unsigned short interval)
+{
+    int rc;
+    debugf(BABEL_DEBUG_COMMON,"Sending ack (%04x) to %s on %s.",
+           nonce, format_address(neigh->address), neigh->ifp->name);
+    rc = start_unicast_message(neigh, MESSAGE_ACK, 2); if(rc < 0) return;
+    accumulate_unicast_short(neigh, nonce);
+    end_unicast_message(neigh, MESSAGE_ACK, 2);
+    /* Roughly yields a value no larger than 3/2, so this meets the deadline */
+    schedule_unicast_flush(roughly(interval * 6));
+}
+
+void
+send_hello_noupdate(struct interface *ifp, unsigned interval)
+{
+    babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp);
+    /* This avoids sending multiple hellos in a single packet, which breaks
+       link quality estimation. */
+    if(babel_ifp->have_buffered_hello)
+        flushbuf(ifp);
+
+    babel_ifp->hello_seqno = seqno_plus(babel_ifp->hello_seqno, 1);
+    set_timeout(&babel_ifp->hello_timeout, babel_ifp->hello_interval);
+
+    if(!if_up(ifp))
+        return;
+
+    debugf(BABEL_DEBUG_COMMON,"Sending hello %d (%d) to %s.",
+           babel_ifp->hello_seqno, interval, ifp->name);
+
+    start_message(ifp, MESSAGE_HELLO, 6);
+    accumulate_short(ifp, 0);
+    accumulate_short(ifp, babel_ifp->hello_seqno);
+    accumulate_short(ifp, interval > 0xFFFF ? 0xFFFF : interval);
+    end_message(ifp, MESSAGE_HELLO, 6);
+    babel_ifp->have_buffered_hello = 1;
+}
+
+void
+send_hello(struct interface *ifp)
+{
+    babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp);
+    send_hello_noupdate(ifp, (babel_ifp->hello_interval + 9) / 10);
+    /* Send full IHU every 3 hellos, and marginal IHU each time */
+    if(babel_ifp->hello_seqno % 3 == 0)
+        send_ihu(NULL, ifp);
+    else
+        send_marginal_ihu(ifp);
+}
+
+void
+flush_unicast(int dofree)
+{
+    struct sockaddr_in6 sin6;
+    int rc;
+
+    if(unicast_buffered == 0)
+        goto done;
+
+    if(!if_up(unicast_neighbour->ifp))
+        goto done;
+
+    /* Preserve ordering of messages */
+    flushbuf(unicast_neighbour->ifp);
+
+    if(check_bucket(unicast_neighbour->ifp)) {
+        memset(&sin6, 0, sizeof(sin6));
+        sin6.sin6_family = AF_INET6;
+        memcpy(&sin6.sin6_addr, unicast_neighbour->address, 16);
+        sin6.sin6_port = htons(protocol_port);
+        sin6.sin6_scope_id = unicast_neighbour->ifp->ifindex;
+        DO_HTONS(packet_header + 2, unicast_buffered);
+        rc = babel_send(protocol_socket,
+                        packet_header, sizeof(packet_header),
+                        unicast_buffer, unicast_buffered,
+                        (struct sockaddr*)&sin6, sizeof(sin6));
+        if(rc < 0)
+            zlog_err("send(unicast): %s", safe_strerror(errno));
+    } else {
+        zlog_err("Warning: bucket full, dropping unicast packet to %s if %s.",
+                 format_address(unicast_neighbour->address),
+                 unicast_neighbour->ifp->name);
+    }
+
+ done:
+    VALGRIND_MAKE_MEM_UNDEFINED(unicast_buffer, UNICAST_BUFSIZE);
+    unicast_buffered = 0;
+    if(dofree && unicast_buffer) {
+        free(unicast_buffer);
+        unicast_buffer = NULL;
+    }
+    unicast_neighbour = NULL;
+    unicast_flush_timeout.tv_sec = 0;
+    unicast_flush_timeout.tv_usec = 0;
+}
+
+static void
+really_send_update(struct interface *ifp,
+                   const unsigned char *id,
+                   const unsigned char *prefix, unsigned char plen,
+                   unsigned short seqno, unsigned short metric,
+                   unsigned char *channels, int channels_len)
+{
+    babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp);
+    int add_metric, v4, real_plen, omit = 0;
+    const unsigned char *real_prefix;
+    unsigned short flags = 0;
+    int channels_size;
+
+    if(diversity_kind != DIVERSITY_CHANNEL)
+        channels_len = -1;
+
+    channels_size = channels_len >= 0 ? channels_len + 2 : 0;
+
+    if(!if_up(ifp))
+        return;
+
+    add_metric = output_filter(id, prefix, plen, ifp->ifindex);
+    if(add_metric >= INFINITY)
+        return;
+
+    metric = MIN(metric + add_metric, INFINITY);
+    /* Worst case */
+    ensure_space(ifp, 20 + 12 + 28);
+
+    v4 = plen >= 96 && v4mapped(prefix);
+
+    if(v4) {
+        if(!babel_ifp->ipv4)
+            return;
+        if(!babel_ifp->have_buffered_nh ||
+           memcmp(babel_ifp->buffered_nh, babel_ifp->ipv4, 4) != 0) {
+            start_message(ifp, MESSAGE_NH, 6);
+            accumulate_byte(ifp, 1);
+            accumulate_byte(ifp, 0);
+            accumulate_bytes(ifp, babel_ifp->ipv4, 4);
+            end_message(ifp, MESSAGE_NH, 6);
+            memcpy(babel_ifp->buffered_nh, babel_ifp->ipv4, 4);
+            babel_ifp->have_buffered_nh = 1;
+        }
+
+        real_prefix = prefix + 12;
+        real_plen = plen - 96;
+    } else {
+        if(babel_ifp->have_buffered_prefix) {
+            while(omit < plen / 8 &&
+                  babel_ifp->buffered_prefix[omit] == prefix[omit])
+                omit++;
+        }
+        if(!babel_ifp->have_buffered_prefix || plen >= 48)
+            flags |= 0x80;
+        real_prefix = prefix;
+        real_plen = plen;
+    }
+
+    if(!babel_ifp->have_buffered_id
+       || memcmp(id, babel_ifp->buffered_id, 8) != 0) {
+        if(real_plen == 128 && memcmp(real_prefix + 8, id, 8) == 0) {
+            flags |= 0x40;
+        } else {
+            start_message(ifp, MESSAGE_ROUTER_ID, 10);
+            accumulate_short(ifp, 0);
+            accumulate_bytes(ifp, id, 8);
+            end_message(ifp, MESSAGE_ROUTER_ID, 10);
+        }
+        memcpy(babel_ifp->buffered_id, id, 16);
+        babel_ifp->have_buffered_id = 1;
+    }
+
+    start_message(ifp, MESSAGE_UPDATE, 10 + (real_plen + 7) / 8 - omit +
+                  channels_size);
+    accumulate_byte(ifp, v4 ? 1 : 2);
+    accumulate_byte(ifp, flags);
+    accumulate_byte(ifp, real_plen);
+    accumulate_byte(ifp, omit);
+    accumulate_short(ifp, (babel_ifp->update_interval + 5) / 10);
+    accumulate_short(ifp, seqno);
+    accumulate_short(ifp, metric);
+    accumulate_bytes(ifp, real_prefix + omit, (real_plen + 7) / 8 - omit);
+    /* Note that an empty channels TLV is different from no such TLV. */
+    if(channels_len >= 0) {
+        accumulate_byte(ifp, 2);
+        accumulate_byte(ifp, channels_len);
+        accumulate_bytes(ifp, channels, channels_len);
+    }
+    end_message(ifp, MESSAGE_UPDATE, 10 + (real_plen + 7) / 8 - omit +
+                channels_size);
+
+    if(flags & 0x80) {
+        memcpy(babel_ifp->buffered_prefix, prefix, 16);
+        babel_ifp->have_buffered_prefix = 1;
+    }
+}
+
+static int
+compare_buffered_updates(const void *av, const void *bv)
+{
+    const struct buffered_update *a = av, *b = bv;
+    int rc, v4a, v4b, ma, mb;
+
+    rc = memcmp(a->id, b->id, 8);
+    if(rc != 0)
+        return rc;
+
+    v4a = (a->plen >= 96 && v4mapped(a->prefix));
+    v4b = (b->plen >= 96 && v4mapped(b->prefix));
+
+    if(v4a > v4b)
+        return 1;
+    else if(v4a < v4b)
+        return -1;
+
+    ma = (!v4a && a->plen == 128 && memcmp(a->prefix + 8, a->id, 8) == 0);
+    mb = (!v4b && b->plen == 128 && memcmp(b->prefix + 8, b->id, 8) == 0);
+
+    if(ma > mb)
+        return -1;
+    else if(mb > ma)
+        return 1;
+
+    if(a->plen < b->plen)
+        return 1;
+    else if(a->plen > b->plen)
+        return -1;
+
+    return memcmp(a->prefix, b->prefix, 16);
+}
+
+void
+flushupdates(struct interface *ifp)
+{
+    babel_interface_nfo *babel_ifp = NULL;
+    struct xroute *xroute;
+    struct babel_route *route;
+    const unsigned char *last_prefix = NULL;
+    unsigned char last_plen = 0xFF;
+    int i;
+
+    if(ifp == NULL) {
+      struct interface *ifp_aux;
+      struct listnode *linklist_node = NULL;
+        FOR_ALL_INTERFACES(ifp_aux, linklist_node)
+            flushupdates(ifp_aux);
+        return;
+    }
+
+    babel_ifp = babel_get_if_nfo(ifp);
+    if(babel_ifp->num_buffered_updates > 0) {
+        struct buffered_update *b = babel_ifp->buffered_updates;
+        int n = babel_ifp->num_buffered_updates;
+
+        babel_ifp->buffered_updates = NULL;
+        babel_ifp->update_bufsize = 0;
+        babel_ifp->num_buffered_updates = 0;
+
+        if(!if_up(ifp))
+            goto done;
+
+        debugf(BABEL_DEBUG_COMMON,"  (flushing %d buffered updates on %s (%d))",
+               n, ifp->name, ifp->ifindex);
+
+        /* In order to send fewer update messages, we want to send updates
+           with the same router-id together, with IPv6 going out before IPv4. */
+
+        for(i = 0; i < n; i++) {
+            route = find_installed_route(b[i].prefix, b[i].plen);
+            if(route)
+                memcpy(b[i].id, route->src->id, 8);
+            else
+                memcpy(b[i].id, myid, 8);
+        }
+
+        qsort(b, n, sizeof(struct buffered_update), compare_buffered_updates);
+
+        for(i = 0; i < n; i++) {
+            /* The same update may be scheduled multiple times before it is
+               sent out.  Since our buffer is now sorted, it is enough to
+               compare with the previous update. */
+
+            if(last_prefix) {
+                if(b[i].plen == last_plen &&
+                   memcmp(b[i].prefix, last_prefix, 16) == 0)
+                    continue;
+            }
+
+            xroute = find_xroute(b[i].prefix, b[i].plen);
+            route = find_installed_route(b[i].prefix, b[i].plen);
+
+            if(xroute && (!route || xroute->metric <= kernel_metric)) {
+                really_send_update(ifp, myid,
+                                   xroute->prefix, xroute->plen,
+                                   myseqno, xroute->metric,
+                                   NULL, 0);
+                last_prefix = xroute->prefix;
+                last_plen = xroute->plen;
+            } else if(route) {
+                unsigned char channels[DIVERSITY_HOPS];
+                int chlen;
+                struct interface *route_ifp = route->neigh->ifp;
+                struct babel_interface *babel_route_ifp = NULL;
+                unsigned short metric;
+                unsigned short seqno;
+
+                seqno = route->seqno;
+                metric =
+                    route_interferes(route, ifp) ?
+                    route_metric(route) :
+                    route_metric_noninterfering(route);
+
+                if(metric < INFINITY)
+                    satisfy_request(route->src->prefix, route->src->plen,
+                                    seqno, route->src->id, ifp);
+                if((babel_ifp->flags & BABEL_IF_SPLIT_HORIZON) &&
+                   route->neigh->ifp == ifp)
+                    continue;
+
+                babel_route_ifp = babel_get_if_nfo(route_ifp);
+                if(babel_route_ifp->channel ==BABEL_IF_CHANNEL_NONINTERFERING) {
+                    memcpy(channels, route->channels, DIVERSITY_HOPS);
+                } else {
+                    if(babel_route_ifp->channel == BABEL_IF_CHANNEL_UNKNOWN)
+                        channels[0] = BABEL_IF_CHANNEL_INTERFERING;
+                    else {
+                        assert(babel_route_ifp->channel > 0 &&
+                               babel_route_ifp->channel <= 255);
+                        channels[0] = babel_route_ifp->channel;
+                    }
+                    memcpy(channels + 1, route->channels, DIVERSITY_HOPS - 1);
+                }
+
+                chlen = channels_len(channels);
+                really_send_update(ifp, route->src->id,
+                                   route->src->prefix,
+                                   route->src->plen,
+                                   seqno, metric,
+                                   channels, chlen);
+                update_source(route->src, seqno, metric);
+                last_prefix = route->src->prefix;
+                last_plen = route->src->plen;
+            } else {
+            /* There's no route for this prefix.  This can happen shortly
+               after an xroute has been retracted, so send a retraction. */
+                really_send_update(ifp, myid, b[i].prefix, b[i].plen,
+                                   myseqno, INFINITY, NULL, -1);
+            }
+        }
+        schedule_flush_now(ifp);
+    done:
+        free(b);
+    }
+    babel_ifp->update_flush_timeout.tv_sec = 0;
+    babel_ifp->update_flush_timeout.tv_usec = 0;
+}
+
+static void
+schedule_update_flush(struct interface *ifp, int urgent)
+{
+    babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp);
+    unsigned msecs;
+    msecs = update_jitter(babel_ifp, urgent);
+    if(babel_ifp->update_flush_timeout.tv_sec != 0 &&
+       timeval_minus_msec(&babel_ifp->update_flush_timeout, &babel_now) < msecs)
+        return;
+    set_timeout(&babel_ifp->update_flush_timeout, msecs);
+}
+
+static void
+buffer_update(struct interface *ifp,
+              const unsigned char *prefix, unsigned char plen)
+{
+    babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp);
+    if(babel_ifp->num_buffered_updates > 0 &&
+       babel_ifp->num_buffered_updates >= babel_ifp->update_bufsize)
+        flushupdates(ifp);
+
+    if(babel_ifp->update_bufsize == 0) {
+        int n;
+        assert(babel_ifp->buffered_updates == NULL);
+        /* Allocate enough space to hold a full update.  Since the
+           number of installed routes will grow over time, make sure we
+           have enough space to send a full-ish frame. */
+        n = installed_routes_estimate() + xroutes_estimate() + 4;
+        n = MAX(n, babel_ifp->bufsize / 16);
+    again:
+        babel_ifp->buffered_updates = malloc(n *sizeof(struct buffered_update));
+        if(babel_ifp->buffered_updates == NULL) {
+            zlog_err("malloc(buffered_updates): %s", safe_strerror(errno));
+            if(n > 4) {
+                /* Try again with a tiny buffer. */
+                n = 4;
+                goto again;
+            }
+            return;
+        }
+        babel_ifp->update_bufsize = n;
+        babel_ifp->num_buffered_updates = 0;
+    }
+
+    memcpy(babel_ifp->buffered_updates[babel_ifp->num_buffered_updates].prefix,
+           prefix, 16);
+    babel_ifp->buffered_updates[babel_ifp->num_buffered_updates].plen = plen;
+    babel_ifp->num_buffered_updates++;
+}
+
+static void
+buffer_update_callback(struct babel_route *route, void *closure)
+{
+    buffer_update((struct interface*)closure,
+                  route->src->prefix, route->src->plen);
+}
+
+void
+send_update(struct interface *ifp, int urgent,
+            const unsigned char *prefix, unsigned char plen)
+{
+    babel_interface_nfo *babel_ifp = NULL;
+
+    if(ifp == NULL) {
+      struct interface *ifp_aux;
+      struct listnode *linklist_node = NULL;
+        struct babel_route *route;
+        FOR_ALL_INTERFACES(ifp_aux, linklist_node)
+            send_update(ifp_aux, urgent, prefix, plen);
+        if(prefix) {
+            /* Since flushupdates only deals with non-wildcard interfaces, we
+               need to do this now. */
+            route = find_installed_route(prefix, plen);
+            if(route && route_metric(route) < INFINITY)
+                satisfy_request(prefix, plen, route->src->seqno, route->src->id,
+                                NULL);
+        }
+        return;
+    }
+
+    if(!if_up(ifp))
+        return;
+
+    babel_ifp = babel_get_if_nfo(ifp);
+    if(prefix) {
+        debugf(BABEL_DEBUG_COMMON,"Sending update to %s for %s.",
+               ifp->name, format_prefix(prefix, plen));
+        buffer_update(ifp, prefix, plen);
+    } else {
+        send_self_update(ifp);
+        debugf(BABEL_DEBUG_COMMON,"Sending update to %s for any.", ifp->name);
+        for_all_installed_routes(buffer_update_callback, ifp);
+        set_timeout(&babel_ifp->update_timeout, babel_ifp->update_interval);
+        babel_ifp->last_update_time = babel_now.tv_sec;
+    }
+    schedule_update_flush(ifp, urgent);
+}
+
+void
+send_update_resend(struct interface *ifp,
+                   const unsigned char *prefix, unsigned char plen)
+{
+    assert(prefix != NULL);
+
+    send_update(ifp, 1, prefix, plen);
+    record_resend(RESEND_UPDATE, prefix, plen, 0, 0, NULL, resend_delay);
+}
+
+void
+send_wildcard_retraction(struct interface *ifp)
+{
+    babel_interface_nfo *babel_ifp = NULL;
+    if(ifp == NULL) {
+      struct interface *ifp_aux;
+      struct listnode *linklist_node = NULL;
+        FOR_ALL_INTERFACES(ifp_aux, linklist_node)
+            send_wildcard_retraction(ifp_aux);
+        return;
+    }
+
+    if(!if_up(ifp))
+        return;
+
+    babel_ifp = babel_get_if_nfo(ifp);
+    start_message(ifp, MESSAGE_UPDATE, 10);
+    accumulate_byte(ifp, 0);
+    accumulate_byte(ifp, 0x40);
+    accumulate_byte(ifp, 0);
+    accumulate_byte(ifp, 0);
+    accumulate_short(ifp, 0xFFFF);
+    accumulate_short(ifp, myseqno);
+    accumulate_short(ifp, 0xFFFF);
+    end_message(ifp, MESSAGE_UPDATE, 10);
+
+    babel_ifp->have_buffered_id = 0;
+}
+
+void
+update_myseqno()
+{
+    myseqno = seqno_plus(myseqno, 1);
+    seqno_time = babel_now;
+}
+
+static void
+send_xroute_update_callback(struct xroute *xroute, void *closure)
+{
+    struct interface *ifp = (struct interface*)closure;
+    send_update(ifp, 0, xroute->prefix, xroute->plen);
+}
+
+void
+send_self_update(struct interface *ifp)
+{
+    if(ifp == NULL) {
+      struct interface *ifp_aux;
+      struct listnode *linklist_node = NULL;
+        FOR_ALL_INTERFACES(ifp_aux, linklist_node) {
+            if(!if_up(ifp_aux))
+                continue;
+            send_self_update(ifp_aux);
+        }
+        return;
+    }
+
+    debugf(BABEL_DEBUG_COMMON,"Sending self update to %s.", ifp->name);
+    for_all_xroutes(send_xroute_update_callback, ifp);
+}
+
+void
+send_ihu(struct neighbour *neigh, struct interface *ifp)
+{
+    babel_interface_nfo *babel_ifp = NULL;
+    int rxcost, interval;
+    int ll;
+
+    if(neigh == NULL && ifp == NULL) {
+      struct interface *ifp_aux;
+      struct listnode *linklist_node = NULL;
+        FOR_ALL_INTERFACES(ifp_aux, linklist_node) {
+            if(if_up(ifp_aux))
+                continue;
+            send_ihu(NULL, ifp_aux);
+        }
+        return;
+    }
+
+    if(neigh == NULL) {
+        struct neighbour *ngh;
+        FOR_ALL_NEIGHBOURS(ngh) {
+            if(ngh->ifp == ifp)
+                send_ihu(ngh, ifp);
+        }
+        return;
+    }
+
+
+    if(ifp && neigh->ifp != ifp)
+        return;
+
+    ifp = neigh->ifp;
+    babel_ifp = babel_get_if_nfo(ifp);
+    if(!if_up(ifp))
+        return;
+
+    rxcost = neighbour_rxcost(neigh);
+    interval = (babel_ifp->hello_interval * 3 + 9) / 10;
+
+    /* Conceptually, an IHU is a unicast message.  We usually send them as
+       multicast, since this allows aggregation into a single packet and
+       avoids an ARP exchange.  If we already have a unicast message queued
+       for this neighbour, however, we might as well piggyback the IHU. */
+    debugf(BABEL_DEBUG_COMMON,"Sending %sihu %d on %s to %s.",
+           unicast_neighbour == neigh ? "unicast " : "",
+           rxcost,
+           neigh->ifp->name,
+           format_address(neigh->address));
+
+    ll = linklocal(neigh->address);
+
+    if(unicast_neighbour != neigh) {
+        start_message(ifp, MESSAGE_IHU, ll ? 14 : 22);
+        accumulate_byte(ifp, ll ? 3 : 2);
+        accumulate_byte(ifp, 0);
+        accumulate_short(ifp, rxcost);
+        accumulate_short(ifp, interval);
+        if(ll)
+            accumulate_bytes(ifp, neigh->address + 8, 8);
+        else
+            accumulate_bytes(ifp, neigh->address, 16);
+        end_message(ifp, MESSAGE_IHU, ll ? 14 : 22);
+    } else {
+        int rc;
+        rc = start_unicast_message(neigh, MESSAGE_IHU, ll ? 14 : 22);
+        if(rc < 0) return;
+        accumulate_unicast_byte(neigh, ll ? 3 : 2);
+        accumulate_unicast_byte(neigh, 0);
+        accumulate_unicast_short(neigh, rxcost);
+        accumulate_unicast_short(neigh, interval);
+        if(ll)
+            accumulate_unicast_bytes(neigh, neigh->address + 8, 8);
+        else
+            accumulate_unicast_bytes(neigh, neigh->address, 16);
+        end_unicast_message(neigh, MESSAGE_IHU, ll ? 14 : 22);
+    }
+}
+
+/* Send IHUs to all marginal neighbours */
+void
+send_marginal_ihu(struct interface *ifp)
+{
+    struct neighbour *neigh;
+    FOR_ALL_NEIGHBOURS(neigh) {
+        if(ifp && neigh->ifp != ifp)
+            continue;
+        if(neigh->txcost >= 384 || (neigh->reach & 0xF000) != 0xF000)
+            send_ihu(neigh, ifp);
+    }
+}
+
+void
+send_request(struct interface *ifp,
+             const unsigned char *prefix, unsigned char plen)
+{
+    int v4, len;
+
+    if(ifp == NULL) {
+      struct interface *ifp_aux;
+      struct listnode *linklist_node = NULL;
+        FOR_ALL_INTERFACES(ifp_aux, linklist_node) {
+            if(if_up(ifp_aux))
+                continue;
+            send_request(ifp_aux, prefix, plen);
+        }
+        return;
+    }
+
+    /* make sure any buffered updates go out before this request. */
+    flushupdates(ifp);
+
+    if(!if_up(ifp))
+        return;
+
+    debugf(BABEL_DEBUG_COMMON,"sending request to %s for %s.",
+           ifp->name, prefix ? format_prefix(prefix, plen) : "any");
+    v4 = plen >= 96 && v4mapped(prefix);
+    len = !prefix ? 2 : v4 ? 6 : 18;
+
+    start_message(ifp, MESSAGE_REQUEST, len);
+    accumulate_byte(ifp, !prefix ? 0 : v4 ? 1 : 2);
+    accumulate_byte(ifp, !prefix ? 0 : v4 ? plen - 96 : plen);
+    if(prefix) {
+        if(v4)
+            accumulate_bytes(ifp, prefix + 12, 4);
+        else
+            accumulate_bytes(ifp, prefix, 16);
+    }
+    end_message(ifp, MESSAGE_REQUEST, len);
+}
+
+void
+send_unicast_request(struct neighbour *neigh,
+                     const unsigned char *prefix, unsigned char plen)
+{
+    int rc, v4, len;
+
+    /* make sure any buffered updates go out before this request. */
+    flushupdates(neigh->ifp);
+
+    debugf(BABEL_DEBUG_COMMON,"sending unicast request to %s for %s.",
+           format_address(neigh->address),
+           prefix ? format_prefix(prefix, plen) : "any");
+    v4 = plen >= 96 && v4mapped(prefix);
+    len = !prefix ? 2 : v4 ? 6 : 18;
+
+    rc = start_unicast_message(neigh, MESSAGE_REQUEST, len);
+    if(rc < 0) return;
+    accumulate_unicast_byte(neigh, !prefix ? 0 : v4 ? 1 : 2);
+    accumulate_unicast_byte(neigh, !prefix ? 0 : v4 ? plen - 96 : plen);
+    if(prefix) {
+        if(v4)
+            accumulate_unicast_bytes(neigh, prefix + 12, 4);
+        else
+            accumulate_unicast_bytes(neigh, prefix, 16);
+    }
+    end_unicast_message(neigh, MESSAGE_REQUEST, len);
+}
+
+void
+send_multihop_request(struct interface *ifp,
+                      const unsigned char *prefix, unsigned char plen,
+                      unsigned short seqno, const unsigned char *id,
+                      unsigned short hop_count)
+{
+    int v4, pb, len;
+
+    /* Make sure any buffered updates go out before this request. */
+    flushupdates(ifp);
+
+    if(ifp == NULL) {
+      struct interface *ifp_aux;
+      struct listnode *linklist_node = NULL;
+        FOR_ALL_INTERFACES(ifp_aux, linklist_node) {
+            if(!if_up(ifp_aux))
+                continue;
+            send_multihop_request(ifp_aux, prefix, plen, seqno, id, hop_count);
+        }
+        return;
+    }
+
+    if(!if_up(ifp))
+        return;
+
+    debugf(BABEL_DEBUG_COMMON,"Sending request (%d) on %s for %s.",
+           hop_count, ifp->name, format_prefix(prefix, plen));
+    v4 = plen >= 96 && v4mapped(prefix);
+    pb = v4 ? ((plen - 96) + 7) / 8 : (plen + 7) / 8;
+    len = 6 + 8 + pb;
+
+    start_message(ifp, MESSAGE_MH_REQUEST, len);
+    accumulate_byte(ifp, v4 ? 1 : 2);
+    accumulate_byte(ifp, v4 ? plen - 96 : plen);
+    accumulate_short(ifp, seqno);
+    accumulate_byte(ifp, hop_count);
+    accumulate_byte(ifp, 0);
+    accumulate_bytes(ifp, id, 8);
+    if(prefix) {
+        if(v4)
+            accumulate_bytes(ifp, prefix + 12, pb);
+        else
+            accumulate_bytes(ifp, prefix, pb);
+    }
+    end_message(ifp, MESSAGE_MH_REQUEST, len);
+}
+
+void
+send_unicast_multihop_request(struct neighbour *neigh,
+                              const unsigned char *prefix, unsigned char plen,
+                              unsigned short seqno, const unsigned char *id,
+                              unsigned short hop_count)
+{
+    int rc, v4, pb, len;
+
+    /* Make sure any buffered updates go out before this request. */
+    flushupdates(neigh->ifp);
+
+    debugf(BABEL_DEBUG_COMMON,"Sending multi-hop request to %s for %s (%d hops).",
+           format_address(neigh->address),
+           format_prefix(prefix, plen), hop_count);
+    v4 = plen >= 96 && v4mapped(prefix);
+    pb = v4 ? ((plen - 96) + 7) / 8 : (plen + 7) / 8;
+    len = 6 + 8 + pb;
+
+    rc = start_unicast_message(neigh, MESSAGE_MH_REQUEST, len);
+    if(rc < 0) return;
+    accumulate_unicast_byte(neigh, v4 ? 1 : 2);
+    accumulate_unicast_byte(neigh, v4 ? plen - 96 : plen);
+    accumulate_unicast_short(neigh, seqno);
+    accumulate_unicast_byte(neigh, hop_count);
+    accumulate_unicast_byte(neigh, 0);
+    accumulate_unicast_bytes(neigh, id, 8);
+    if(prefix) {
+        if(v4)
+            accumulate_unicast_bytes(neigh, prefix + 12, pb);
+        else
+            accumulate_unicast_bytes(neigh, prefix, pb);
+    }
+    end_unicast_message(neigh, MESSAGE_MH_REQUEST, len);
+}
+
+void
+send_request_resend(struct neighbour *neigh,
+                    const unsigned char *prefix, unsigned char plen,
+                    unsigned short seqno, unsigned char *id)
+{
+    if(neigh)
+        send_unicast_multihop_request(neigh, prefix, plen, seqno, id, 127);
+    else
+        send_multihop_request(NULL, prefix, plen, seqno, id, 127);
+
+    record_resend(RESEND_REQUEST, prefix, plen, seqno, id,
+                  neigh ? neigh->ifp : NULL, resend_delay);
+}
+
+void
+handle_request(struct neighbour *neigh, const unsigned char *prefix,
+               unsigned char plen, unsigned char hop_count,
+               unsigned short seqno, const unsigned char *id)
+{
+    struct xroute *xroute;
+    struct babel_route *route;
+    struct neighbour *successor = NULL;
+
+    xroute = find_xroute(prefix, plen);
+    route = find_installed_route(prefix, plen);
+
+    if(xroute && (!route || xroute->metric <= kernel_metric)) {
+        if(hop_count > 0 && memcmp(id, myid, 8) == 0) {
+            if(seqno_compare(seqno, myseqno) > 0) {
+                if(seqno_minus(seqno, myseqno) > 100) {
+                    /* Hopelessly out-of-date request */
+                    return;
+                }
+                update_myseqno();
+            }
+        }
+        send_update(neigh->ifp, 1, prefix, plen);
+        return;
+    }
+
+    if(route &&
+       (memcmp(id, route->src->id, 8) != 0 ||
+        seqno_compare(seqno, route->seqno) <= 0)) {
+        send_update(neigh->ifp, 1, prefix, plen);
+        return;
+    }
+
+    if(hop_count <= 1)
+        return;
+
+    if(route && memcmp(id, route->src->id, 8) == 0 &&
+       seqno_minus(seqno, route->seqno) > 100) {
+        /* Hopelessly out-of-date */
+        return;
+    }
+
+    if(request_redundant(neigh->ifp, prefix, plen, seqno, id))
+        return;
+
+    /* Let's try to forward this request. */
+    if(route && route_metric(route) < INFINITY)
+        successor = route->neigh;
+
+    if(!successor || successor == neigh) {
+        /* We were about to forward a request to its requestor.  Try to
+           find a different neighbour to forward the request to. */
+        struct babel_route *other_route;
+
+        other_route = find_best_route(prefix, plen, 0, neigh);
+        if(other_route && route_metric(other_route) < INFINITY)
+            successor = other_route->neigh;
+    }
+
+    if(!successor || successor == neigh)
+        /* Give up */
+        return;
+
+    send_unicast_multihop_request(successor, prefix, plen, seqno, id,
+                                  hop_count - 1);
+    record_resend(RESEND_REQUEST, prefix, plen, seqno, id,
+                  neigh->ifp, 0);
+}
diff --git a/babeld/message.h b/babeld/message.h
new file mode 100644
index 0000000..6a9aa10
--- /dev/null
+++ b/babeld/message.h
@@ -0,0 +1,111 @@
+/*  
+ *  This file is free software: you may copy, redistribute 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.  
+ *  
+ *  This file is distributed in the hope that it will be useful, but  
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+ *  General Public License for more details.  
+ *  
+ *  You should have received a copy of the GNU General Public License  
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ *  
+ * This file incorporates work covered by the following copyright and  
+ * permission notice:  
+ *  
+Copyright (c) 2007, 2008 by Juliusz Chroboczek
+
+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
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#ifndef BABEL_MESSAGE_H
+#define BABEL_MESSAGE_H
+
+#include "babel_interface.h"
+
+#define MAX_BUFFERED_UPDATES 200
+
+#define BUCKET_TOKENS_MAX 200
+#define BUCKET_TOKENS_PER_SEC 40
+
+#define MESSAGE_PAD1 0
+#define MESSAGE_PADN 1
+#define MESSAGE_ACK_REQ 2
+#define MESSAGE_ACK 3
+#define MESSAGE_HELLO 4
+#define MESSAGE_IHU 5
+#define MESSAGE_ROUTER_ID 6
+#define MESSAGE_NH 7
+#define MESSAGE_UPDATE 8
+#define MESSAGE_REQUEST 9
+#define MESSAGE_MH_REQUEST 10
+
+
+extern unsigned short myseqno;
+extern struct timeval seqno_time;
+
+extern int broadcast_ihu;
+extern int split_horizon;
+
+extern unsigned char packet_header[4];
+
+extern struct neighbour *unicast_neighbour;
+extern struct timeval unicast_flush_timeout;
+
+void parse_packet(const unsigned char *from, struct interface *ifp,
+                  const unsigned char *packet, int packetlen);
+void flushbuf(struct interface *ifp);
+void flushupdates(struct interface *ifp);
+void send_ack(struct neighbour *neigh, unsigned short nonce,
+              unsigned short interval);
+void send_hello_noupdate(struct interface *ifp, unsigned interval);
+void send_hello(struct interface *ifp);
+void flush_unicast(int dofree);
+void send_update(struct interface *ifp, int urgent,
+                 const unsigned char *prefix, unsigned char plen);
+void send_update_resend(struct interface *ifp,
+                        const unsigned char *prefix, unsigned char plen);
+void send_wildcard_retraction(struct interface *ifp);
+void update_myseqno(void);
+void send_self_update(struct interface *ifp);
+void send_ihu(struct neighbour *neigh, struct interface *ifp);
+void send_marginal_ihu(struct interface *ifp);
+void send_request(struct interface *ifp,
+                  const unsigned char *prefix, unsigned char plen);
+void send_unicast_request(struct neighbour *neigh,
+                          const unsigned char *prefix, unsigned char plen);
+void send_multihop_request(struct interface *ifp,
+                           const unsigned char *prefix, unsigned char plen,
+                           unsigned short seqno, const unsigned char *id,
+                           unsigned short hop_count);
+void
+send_unicast_multihop_request(struct neighbour *neigh,
+                              const unsigned char *prefix, unsigned char plen,
+                              unsigned short seqno, const unsigned char *id,
+                              unsigned short hop_count);
+void send_request_resend(struct neighbour *neigh,
+                         const unsigned char *prefix, unsigned char plen,
+                         unsigned short seqno, unsigned char *id);
+void handle_request(struct neighbour *neigh, const unsigned char *prefix,
+                    unsigned char plen, unsigned char hop_count,
+                    unsigned short seqno, const unsigned char *id);
+
+#endif
diff --git a/babeld/neighbour.c b/babeld/neighbour.c
new file mode 100644
index 0000000..5a327df
--- /dev/null
+++ b/babeld/neighbour.c
@@ -0,0 +1,343 @@
+/*  
+ *  This file is free software: you may copy, redistribute 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.  
+ *  
+ *  This file is distributed in the hope that it will be useful, but  
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+ *  General Public License for more details.  
+ *  
+ *  You should have received a copy of the GNU General Public License  
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ *  
+ * This file incorporates work covered by the following copyright and  
+ * permission notice:  
+ *
+Copyright (c) 2007, 2008 by Juliusz Chroboczek
+
+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
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <sys/time.h>
+#include <time.h>
+
+#include <zebra.h>
+#include "if.h"
+
+#include "babel_main.h"
+#include "babeld.h"
+#include "util.h"
+#include "babel_interface.h"
+#include "neighbour.h"
+#include "source.h"
+#include "route.h"
+#include "message.h"
+#include "resend.h"
+
+struct neighbour *neighs = NULL;
+
+static struct neighbour *
+find_neighbour_nocreate(const unsigned char *address, struct interface *ifp)
+{
+    struct neighbour *neigh;
+    FOR_ALL_NEIGHBOURS(neigh) {
+        if(memcmp(address, neigh->address, 16) == 0 &&
+           neigh->ifp == ifp)
+            return neigh;
+    }
+    return NULL;
+}
+
+void
+flush_neighbour(struct neighbour *neigh)
+{
+    flush_neighbour_routes(neigh);
+    if(unicast_neighbour == neigh)
+        flush_unicast(1);
+    flush_resends(neigh);
+
+    if(neighs == neigh) {
+        neighs = neigh->next;
+    } else {
+        struct neighbour *previous = neighs;
+        while(previous->next != neigh)
+            previous = previous->next;
+        previous->next = neigh->next;
+    }
+    free(neigh);
+}
+
+struct neighbour *
+find_neighbour(const unsigned char *address, struct interface *ifp)
+{
+    struct neighbour *neigh;
+    const struct timeval zero = {0, 0};
+
+    neigh = find_neighbour_nocreate(address, ifp);
+    if(neigh)
+        return neigh;
+
+    debugf(BABEL_DEBUG_COMMON,"Creating neighbour %s on %s.",
+           format_address(address), ifp->name);
+
+    neigh = malloc(sizeof(struct neighbour));
+    if(neigh == NULL) {
+        zlog_err("malloc(neighbour): %s", safe_strerror(errno));
+        return NULL;
+    }
+
+    neigh->hello_seqno = -1;
+    memcpy(neigh->address, address, 16);
+    neigh->reach = 0;
+    neigh->txcost = INFINITY;
+    neigh->ihu_time = babel_now;
+    neigh->hello_time = zero;
+    neigh->hello_interval = 0;
+    neigh->ihu_interval = 0;
+    neigh->ifp = ifp;
+    neigh->next = neighs;
+    neighs = neigh;
+    send_hello(ifp);
+    return neigh;
+}
+
+/* Recompute a neighbour's rxcost.  Return true if anything changed.
+   This does not call local_notify_neighbour, see update_neighbour_metric. */
+int
+update_neighbour(struct neighbour *neigh, int hello, int hello_interval)
+{
+    int missed_hellos;
+    int rc = 0;
+
+    if(hello < 0) {
+        if(neigh->hello_interval <= 0)
+            return rc;
+        missed_hellos =
+            ((int)timeval_minus_msec(&babel_now, &neigh->hello_time) -
+             neigh->hello_interval * 7) /
+            (neigh->hello_interval * 10);
+        if(missed_hellos <= 0)
+            return rc;
+        timeval_add_msec(&neigh->hello_time, &neigh->hello_time,
+                          missed_hellos * neigh->hello_interval * 10);
+    } else {
+        if(neigh->hello_seqno >= 0 && neigh->reach > 0) {
+            missed_hellos = seqno_minus(hello, neigh->hello_seqno) - 1;
+            if(missed_hellos < -8) {
+                /* Probably a neighbour that rebooted and lost its seqno.
+                   Reboot the universe. */
+                neigh->reach = 0;
+                missed_hellos = 0;
+                rc = 1;
+            } else if(missed_hellos < 0) {
+                if(hello_interval > neigh->hello_interval) {
+                    /* This neighbour has increased its hello interval,
+                       and we didn't notice. */
+                    neigh->reach <<= -missed_hellos;
+                    missed_hellos = 0;
+                } else {
+                    /* Late hello.  Probably due to the link layer buffering
+                       packets during a link outage.  Ignore it, but reset
+                       the expected seqno. */
+                    neigh->hello_seqno = hello;
+                    hello = -1;
+                    missed_hellos = 0;
+                }
+                rc = 1;
+            }
+        } else {
+            missed_hellos = 0;
+        }
+        neigh->hello_time = babel_now;
+        neigh->hello_interval = hello_interval;
+    }
+
+    if(missed_hellos > 0) {
+        neigh->reach >>= missed_hellos;
+        neigh->hello_seqno = seqno_plus(neigh->hello_seqno, missed_hellos);
+        missed_hellos = 0;
+        rc = 1;
+    }
+
+    if(hello >= 0) {
+        neigh->hello_seqno = hello;
+        neigh->reach >>= 1;
+        neigh->reach |= 0x8000;
+        if((neigh->reach & 0xFC00) != 0xFC00)
+            rc = 1;
+    }
+
+    /* Make sure to give neighbours some feedback early after association */
+    if((neigh->reach & 0xBF00) == 0x8000) {
+        /* A new neighbour */
+        send_hello(neigh->ifp);
+    } else {
+        /* Don't send hellos, in order to avoid a positive feedback loop. */
+        int a = (neigh->reach & 0xC000);
+        int b = (neigh->reach & 0x3000);
+        if((a == 0xC000 && b == 0) || (a == 0 && b == 0x3000)) {
+            /* Reachability is either 1100 or 0011 */
+            send_self_update(neigh->ifp);
+        }
+    }
+
+    if((neigh->reach & 0xFC00) == 0xC000) {
+        /* This is a newish neighbour, let's request a full route dump.
+           We ought to avoid this when the network is dense */
+        send_unicast_request(neigh, NULL, 0);
+        send_ihu(neigh, NULL);
+    }
+    return rc;
+}
+
+static int
+reset_txcost(struct neighbour *neigh)
+{
+    unsigned delay;
+
+    delay = timeval_minus_msec(&babel_now, &neigh->ihu_time);
+
+    if(neigh->ihu_interval > 0 && delay < neigh->ihu_interval * 10U * 3U)
+        return 0;
+
+    /* If we're losing a lot of packets, we probably lost an IHU too */
+    if(delay >= 180000 || (neigh->reach & 0xFFF0) == 0 ||
+       (neigh->ihu_interval > 0 &&
+        delay >= neigh->ihu_interval * 10U * 10U)) {
+        neigh->txcost = INFINITY;
+        neigh->ihu_time = babel_now;
+        return 1;
+    }
+
+    return 0;
+}
+
+unsigned
+neighbour_txcost(struct neighbour *neigh)
+{
+    return neigh->txcost;
+}
+
+unsigned
+check_neighbours()
+{
+    struct neighbour *neigh;
+    int changed, rc;
+    unsigned msecs = 50000;
+
+    debugf(BABEL_DEBUG_COMMON,"Checking neighbours.");
+
+    neigh = neighs;
+    while(neigh) {
+        changed = update_neighbour(neigh, -1, 0);
+
+        if(neigh->reach == 0 ||
+           neigh->hello_time.tv_sec > babel_now.tv_sec || /* clock stepped */
+           timeval_minus_msec(&babel_now, &neigh->hello_time) > 300000) {
+            struct neighbour *old = neigh;
+            neigh = neigh->next;
+            flush_neighbour(old);
+            continue;
+        }
+
+        rc = reset_txcost(neigh);
+        changed = changed || rc;
+
+        update_neighbour_metric(neigh, changed);
+
+        if(neigh->hello_interval > 0)
+            msecs = MIN(msecs, neigh->hello_interval * 10U);
+        if(neigh->ihu_interval > 0)
+            msecs = MIN(msecs, neigh->ihu_interval * 10U);
+        neigh = neigh->next;
+    }
+
+    return msecs;
+}
+
+unsigned
+neighbour_rxcost(struct neighbour *neigh)
+{
+    unsigned delay;
+    unsigned short reach = neigh->reach;
+
+    delay = timeval_minus_msec(&babel_now, &neigh->hello_time);
+
+    if((reach & 0xFFF0) == 0 || delay >= 180000) {
+        return INFINITY;
+    } else if(babel_get_if_nfo(neigh->ifp)->flags & BABEL_IF_LQ) {
+        int sreach =
+            ((reach & 0x8000) >> 2) +
+            ((reach & 0x4000) >> 1) +
+            (reach & 0x3FFF);
+        /* 0 <= sreach <= 0x7FFF */
+        int cost = (0x8000 * babel_get_if_nfo(neigh->ifp)->cost) / (sreach + 1);
+        /* cost >= interface->cost */
+        if(delay >= 40000)
+            cost = (cost * (delay - 20000) + 10000) / 20000;
+        return MIN(cost, INFINITY);
+    } else {
+        /* To lose one hello is a misfortune, to lose two is carelessness. */
+        if((reach & 0xC000) == 0xC000)
+            return babel_get_if_nfo(neigh->ifp)->cost;
+        else if((reach & 0xC000) == 0)
+            return INFINITY;
+        else if((reach & 0x2000))
+            return babel_get_if_nfo(neigh->ifp)->cost;
+        else
+            return INFINITY;
+    }
+}
+
+unsigned
+neighbour_cost(struct neighbour *neigh)
+{
+    unsigned a, b;
+
+    if(!if_up(neigh->ifp))
+        return INFINITY;
+
+    a = neighbour_txcost(neigh);
+
+    if(a >= INFINITY)
+        return INFINITY;
+
+    b = neighbour_rxcost(neigh);
+    if(b >= INFINITY)
+        return INFINITY;
+
+    if(!(babel_get_if_nfo(neigh->ifp)->flags & BABEL_IF_LQ)
+       || (a < 256 && b < 256)) {
+        return a;
+    } else {
+        /* a = 256/alpha, b = 256/beta, where alpha and beta are the expected
+           probabilities of a packet getting through in the direct and reverse
+           directions. */
+        a = MAX(a, 256);
+        b = MAX(b, 256);
+        /* 1/(alpha * beta), which is just plain ETX. */
+        /* Since a and b are capped to 16 bits, overflow is impossible. */
+        return (a * b + 128) >> 8;
+    }
+}
diff --git a/babeld/neighbour.h b/babeld/neighbour.h
new file mode 100644
index 0000000..cf8c0f0
--- /dev/null
+++ b/babeld/neighbour.h
@@ -0,0 +1,66 @@
+/*  
+ *  This file is free software: you may copy, redistribute 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.  
+ *  
+ *  This file is distributed in the hope that it will be useful, but  
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+ *  General Public License for more details.  
+ *  
+ *  You should have received a copy of the GNU General Public License  
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ *  
+ * This file incorporates work covered by the following copyright and  
+ * permission notice:  
+ *  
+Copyright (c) 2007, 2008 by Juliusz Chroboczek
+
+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
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+struct neighbour {
+    struct neighbour *next;
+    /* This is -1 when unknown, so don't make it unsigned */
+    int hello_seqno;
+    unsigned char address[16];
+    unsigned short reach;
+    unsigned short txcost;
+    struct timeval hello_time;
+    struct timeval ihu_time;
+    unsigned short hello_interval; /* in centiseconds */
+    unsigned short ihu_interval;   /* in centiseconds */
+    struct interface *ifp;
+};
+
+extern struct neighbour *neighs;
+
+#define FOR_ALL_NEIGHBOURS(_neigh) \
+    for(_neigh = neighs; _neigh; _neigh = _neigh->next)
+
+int neighbour_valid(struct neighbour *neigh);
+void flush_neighbour(struct neighbour *neigh);
+struct neighbour *find_neighbour(const unsigned char *address,
+                                 struct interface *ifp);
+int update_neighbour(struct neighbour *neigh, int hello, int hello_interval);
+unsigned check_neighbours(void);
+unsigned neighbour_txcost(struct neighbour *neigh);
+unsigned neighbour_rxcost(struct neighbour *neigh);
+unsigned neighbour_cost(struct neighbour *neigh);
diff --git a/babeld/net.c b/babeld/net.c
new file mode 100644
index 0000000..5e0200b
--- /dev/null
+++ b/babeld/net.c
@@ -0,0 +1,239 @@
+/*  
+ *  This file is free software: you may copy, redistribute 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.  
+ *  
+ *  This file is distributed in the hope that it will be useful, but  
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+ *  General Public License for more details.  
+ *  
+ *  You should have received a copy of the GNU General Public License  
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ *  
+ * This file incorporates work covered by the following copyright and  
+ * permission notice:  
+ *  
+Copyright (c) 2007, 2008 by Juliusz Chroboczek
+
+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
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/uio.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <errno.h>
+
+#include "babeld.h"
+#include "util.h"
+#include "net.h"
+
+int
+babel_socket(int port)
+{
+    struct sockaddr_in6 sin6;
+    int s, rc;
+    int saved_errno;
+    int one = 1, zero = 0;
+    const int ds = 0xc0;        /* CS6 - Network Control */
+
+    s = socket(PF_INET6, SOCK_DGRAM, 0);
+    if(s < 0)
+        return -1;
+
+    rc = setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
+    if(rc < 0)
+        goto fail;
+
+    rc = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
+    if(rc < 0)
+        goto fail;
+
+    rc = setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
+                    &zero, sizeof(zero));
+    if(rc < 0)
+        goto fail;
+
+    rc = setsockopt(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
+                    &one, sizeof(one));
+    if(rc < 0)
+        goto fail;
+
+    rc = setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
+                    &one, sizeof(one));
+    if(rc < 0)
+        goto fail;
+
+#ifdef IPV6_TCLASS
+    rc = setsockopt(s, IPPROTO_IPV6, IPV6_TCLASS, &ds, sizeof(ds));
+#else
+    rc = -1;
+    errno = ENOSYS;
+#endif
+    if(rc < 0)
+        perror("Couldn't set traffic class");
+
+    rc = fcntl(s, F_GETFL, 0);
+    if(rc < 0)
+        goto fail;
+
+    rc = fcntl(s, F_SETFL, (rc | O_NONBLOCK));
+    if(rc < 0)
+        goto fail;
+
+    rc = fcntl(s, F_GETFD, 0);
+    if(rc < 0)
+        goto fail;
+
+    rc = fcntl(s, F_SETFD, rc | FD_CLOEXEC);
+    if(rc < 0)
+        goto fail;
+
+    memset(&sin6, 0, sizeof(sin6));
+    sin6.sin6_family = AF_INET6;
+    sin6.sin6_port = htons(port);
+    rc = bind(s, (struct sockaddr*)&sin6, sizeof(sin6));
+    if(rc < 0)
+        goto fail;
+
+    return s;
+
+ fail:
+    saved_errno = errno;
+    close(s);
+    errno = saved_errno;
+    return -1;
+}
+
+int
+babel_recv(int s, void *buf, int buflen, struct sockaddr *sin, int slen)
+{
+    struct iovec iovec;
+    struct msghdr msg;
+    int rc;
+
+    memset(&msg, 0, sizeof(msg));
+    iovec.iov_base = buf;
+    iovec.iov_len = buflen;
+    msg.msg_name = sin;
+    msg.msg_namelen = slen;
+    msg.msg_iov = &iovec;
+    msg.msg_iovlen = 1;
+
+    rc = recvmsg(s, &msg, 0);
+    return rc;
+}
+
+int
+babel_send(int s,
+           void *buf1, int buflen1, void *buf2, int buflen2,
+           struct sockaddr *sin, int slen)
+{
+    struct iovec iovec[2];
+    struct msghdr msg;
+    int rc;
+
+    iovec[0].iov_base = buf1;
+    iovec[0].iov_len = buflen1;
+    iovec[1].iov_base = buf2;
+    iovec[1].iov_len = buflen2;
+    memset(&msg, 0, sizeof(msg));
+    msg.msg_name = (struct sockaddr*)sin;
+    msg.msg_namelen = slen;
+    msg.msg_iov = iovec;
+    msg.msg_iovlen = 2;
+
+ again:
+    rc = sendmsg(s, &msg, 0);
+    if(rc < 0) {
+        if(errno == EINTR)
+            goto again;
+        else if(errno == EAGAIN) {
+            int rc2;
+            rc2 = wait_for_fd(1, s, 5);
+            if(rc2 > 0)
+                goto again;
+            errno = EAGAIN;
+        }
+    }
+    return rc;
+}
+
+int
+tcp_server_socket(int port, int local)
+{
+    struct sockaddr_in6 sin6;
+    int s, rc, saved_errno;
+    int one = 1;
+
+    s = socket(PF_INET6, SOCK_STREAM, 0);
+    if(s < 0)
+        return -1;
+
+    rc = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
+    if(rc < 0)
+        goto fail;
+
+    rc = fcntl(s, F_GETFL, 0);
+    if(rc < 0)
+        goto fail;
+
+    rc = fcntl(s, F_SETFL, (rc | O_NONBLOCK));
+    if(rc < 0)
+        goto fail;
+
+    rc = fcntl(s, F_GETFD, 0);
+    if(rc < 0)
+        goto fail;
+
+    rc = fcntl(s, F_SETFD, rc | FD_CLOEXEC);
+    if(rc < 0)
+        goto fail;
+
+    memset(&sin6, 0, sizeof(sin6));
+    sin6.sin6_family = AF_INET6;
+    sin6.sin6_port = htons(port);
+    if(local) {
+        rc = inet_pton(AF_INET6, "::1", &sin6.sin6_addr);
+        if(rc < 0)
+            goto fail;
+    }
+    rc = bind(s, (struct sockaddr*)&sin6, sizeof(sin6));
+    if(rc < 0)
+        goto fail;
+
+    rc = listen(s, 2);
+    if(rc < 0)
+        goto fail;
+
+    return s;
+
+ fail:
+    saved_errno = errno;
+    close(s);
+    errno = saved_errno;
+    return -1;
+}
diff --git a/babeld/net.h b/babeld/net.h
new file mode 100644
index 0000000..4bd0f04
--- /dev/null
+++ b/babeld/net.h
@@ -0,0 +1,44 @@
+/*  
+ *  This file is free software: you may copy, redistribute 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.  
+ *  
+ *  This file is distributed in the hope that it will be useful, but  
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+ *  General Public License for more details.  
+ *  
+ *  You should have received a copy of the GNU General Public License  
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ *  
+ * This file incorporates work covered by the following copyright and  
+ * permission notice:  
+ *  
+Copyright (c) 2007, 2008 by Juliusz Chroboczek
+
+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
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+int babel_socket(int port);
+int babel_recv(int s, void *buf, int buflen, struct sockaddr *sin, int slen);
+int babel_send(int s,
+               void *buf1, int buflen1, void *buf2, int buflen2,
+               struct sockaddr *sin, int slen);
+int tcp_server_socket(int port, int local);
diff --git a/babeld/resend.c b/babeld/resend.c
new file mode 100644
index 0000000..1cc6290
--- /dev/null
+++ b/babeld/resend.c
@@ -0,0 +1,330 @@
+/*  
+ *  This file is free software: you may copy, redistribute 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.  
+ *  
+ *  This file is distributed in the hope that it will be useful, but  
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+ *  General Public License for more details.  
+ *  
+ *  You should have received a copy of the GNU General Public License  
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ *  
+ * This file incorporates work covered by the following copyright and  
+ * permission notice:  
+ *  
+Copyright (c) 2007, 2008 by Juliusz Chroboczek
+
+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
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#include <sys/time.h>
+#include <time.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include <zebra.h>
+#include "if.h"
+
+#include "babel_main.h"
+#include "babeld.h"
+#include "util.h"
+#include "neighbour.h"
+#include "resend.h"
+#include "message.h"
+#include "babel_interface.h"
+
+struct timeval resend_time = {0, 0};
+struct resend *to_resend = NULL;
+
+static int
+resend_match(struct resend *resend,
+             int kind, const unsigned char *prefix, unsigned char plen)
+{
+    return (resend->kind == kind &&
+            resend->plen == plen && memcmp(resend->prefix, prefix, 16) == 0);
+}
+
+/* This is called by neigh.c when a neighbour is flushed */
+
+void
+flush_resends(struct neighbour *neigh)
+{
+    /* Nothing for now */
+}
+
+static struct resend *
+find_resend(int kind, const unsigned char *prefix, unsigned char plen,
+             struct resend **previous_return)
+{
+    struct resend *current, *previous;
+
+    previous = NULL;
+    current = to_resend;
+    while(current) {
+        if(resend_match(current, kind, prefix, plen)) {
+            if(previous_return)
+                *previous_return = previous;
+            return current;
+        }
+        previous = current;
+        current = current->next;
+    }
+
+    return NULL;
+}
+
+struct resend *
+find_request(const unsigned char *prefix, unsigned char plen,
+             struct resend **previous_return)
+{
+    return find_resend(RESEND_REQUEST, prefix, plen, previous_return);
+}
+
+int
+record_resend(int kind, const unsigned char *prefix, unsigned char plen,
+              unsigned short seqno, const unsigned char *id,
+              struct interface *ifp, int delay)
+{
+    struct resend *resend;
+    unsigned int ifindex = ifp ? ifp->ifindex : 0;
+
+    if((kind == RESEND_REQUEST &&
+        input_filter(NULL, prefix, plen, NULL, ifindex) >= INFINITY) ||
+       (kind == RESEND_UPDATE &&
+        output_filter(NULL, prefix, plen, ifindex) >= INFINITY))
+        return 0;
+
+    if(delay >= 0xFFFF)
+        delay = 0xFFFF;
+
+    resend = find_resend(kind, prefix, plen, NULL);
+    if(resend) {
+        if(resend->delay && delay)
+            resend->delay = MIN(resend->delay, delay);
+        else if(delay)
+            resend->delay = delay;
+        resend->time = babel_now;
+        resend->max = RESEND_MAX;
+        if(id && memcmp(resend->id, id, 8) == 0 &&
+           seqno_compare(resend->seqno, seqno) > 0) {
+            return 0;
+        }
+        if(id)
+            memcpy(resend->id, id, 8);
+        else
+            memset(resend->id, 0, 8);
+        resend->seqno = seqno;
+        if(resend->ifp != ifp)
+            resend->ifp = NULL;
+    } else {
+        resend = malloc(sizeof(struct resend));
+        if(resend == NULL)
+            return -1;
+        resend->kind = kind;
+        resend->max = RESEND_MAX;
+        resend->delay = delay;
+        memcpy(resend->prefix, prefix, 16);
+        resend->plen = plen;
+        resend->seqno = seqno;
+        if(id)
+            memcpy(resend->id, id, 8);
+        else
+            memset(resend->id, 0, 8);
+        resend->ifp = ifp;
+        resend->time = babel_now;
+        resend->next = to_resend;
+        to_resend = resend;
+    }
+
+    if(resend->delay) {
+        struct timeval timeout;
+        timeval_add_msec(&timeout, &resend->time, resend->delay);
+        timeval_min(&resend_time, &timeout);
+    }
+    return 1;
+}
+
+static int
+resend_expired(struct resend *resend)
+{
+    switch(resend->kind) {
+    case RESEND_REQUEST:
+        return timeval_minus_msec(&babel_now, &resend->time) >= REQUEST_TIMEOUT;
+    default:
+        return resend->max <= 0;
+    }
+}
+
+int
+unsatisfied_request(const unsigned char *prefix, unsigned char plen,
+                    unsigned short seqno, const unsigned char *id)
+{
+    struct resend *request;
+
+    request = find_request(prefix, plen, NULL);
+    if(request == NULL || resend_expired(request))
+        return 0;
+
+    if(memcmp(request->id, id, 8) != 0 ||
+       seqno_compare(request->seqno, seqno) <= 0)
+        return 1;
+
+    return 0;
+}
+
+/* Determine whether a given request should be forwarded. */
+int
+request_redundant(struct interface *ifp,
+                  const unsigned char *prefix, unsigned char plen,
+                  unsigned short seqno, const unsigned char *id)
+{
+    struct resend *request;
+
+    request = find_request(prefix, plen, NULL);
+    if(request == NULL || resend_expired(request))
+        return 0;
+
+    if(memcmp(request->id, id, 8) == 0 &&
+       seqno_compare(request->seqno, seqno) > 0)
+        return 0;
+
+    if(request->ifp != NULL && request->ifp != ifp)
+        return 0;
+
+    if(request->max > 0)
+        /* Will be resent. */
+        return 1;
+
+    if(timeval_minus_msec(&babel_now, &request->time) <
+       (ifp ? MIN(babel_get_if_nfo(ifp)->hello_interval, 1000) : 1000))
+        /* Fairly recent. */
+        return 1;
+
+    return 0;
+}
+
+int
+satisfy_request(const unsigned char *prefix, unsigned char plen,
+                unsigned short seqno, const unsigned char *id,
+                struct interface *ifp)
+{
+    struct resend *request, *previous;
+
+    request = find_request(prefix, plen, &previous);
+    if(request == NULL)
+        return 0;
+
+    if(ifp != NULL && request->ifp != ifp)
+        return 0;
+
+    if(memcmp(request->id, id, 8) != 0 ||
+       seqno_compare(request->seqno, seqno) <= 0) {
+        /* We cannot remove the request, as we may be walking the list right
+           now.  Mark it as expired, so that expire_resend will remove it. */
+        request->max = 0;
+        request->time.tv_sec = 0;
+        recompute_resend_time();
+        return 1;
+    }
+
+    return 0;
+}
+
+void
+expire_resend()
+{
+    struct resend *current, *previous;
+    int recompute = 0;
+
+    previous = NULL;
+    current = to_resend;
+    while(current) {
+        if(resend_expired(current)) {
+            if(previous == NULL) {
+                to_resend = current->next;
+                free(current);
+                current = to_resend;
+            } else {
+                previous->next = current->next;
+                free(current);
+                current = previous->next;
+            }
+            recompute = 1;
+        } else {
+            previous = current;
+            current = current->next;
+        }
+    }
+    if(recompute)
+        recompute_resend_time();
+}
+
+void
+recompute_resend_time()
+{
+    struct resend *request;
+    struct timeval resend = {0, 0};
+
+    request = to_resend;
+    while(request) {
+        if(!resend_expired(request) && request->delay > 0 && request->max > 0) {
+            struct timeval timeout;
+            timeval_add_msec(&timeout, &request->time, request->delay);
+            timeval_min(&resend, &timeout);
+        }
+        request = request->next;
+    }
+
+    resend_time = resend;
+}
+
+void
+do_resend()
+{
+    struct resend *resend;
+
+    resend = to_resend;
+    while(resend) {
+        if(!resend_expired(resend) && resend->delay > 0 && resend->max > 0) {
+            struct timeval timeout;
+            timeval_add_msec(&timeout, &resend->time, resend->delay);
+            if(timeval_compare(&babel_now, &timeout) >= 0) {
+                switch(resend->kind) {
+                case RESEND_REQUEST:
+                    send_multihop_request(resend->ifp,
+                                          resend->prefix, resend->plen,
+                                          resend->seqno, resend->id, 127);
+                    break;
+                case RESEND_UPDATE:
+                    send_update(resend->ifp, 1,
+                                resend->prefix, resend->plen);
+                    break;
+                default: abort();
+                }
+                resend->delay = MIN(0xFFFF, resend->delay * 2);
+                resend->max--;
+            }
+        }
+        resend = resend->next;
+    }
+    recompute_resend_time();
+}
diff --git a/babeld/resend.h b/babeld/resend.h
new file mode 100644
index 0000000..a6755c0
--- /dev/null
+++ b/babeld/resend.h
@@ -0,0 +1,77 @@
+/*  
+ *  This file is free software: you may copy, redistribute 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.  
+ *  
+ *  This file is distributed in the hope that it will be useful, but  
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+ *  General Public License for more details.  
+ *  
+ *  You should have received a copy of the GNU General Public License  
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ *  
+ * This file incorporates work covered by the following copyright and  
+ * permission notice:  
+ *  
+Copyright (c) 2007, 2008 by Juliusz Chroboczek
+
+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
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#define REQUEST_TIMEOUT 65000
+#define RESEND_MAX 3
+
+#define RESEND_REQUEST 1
+#define RESEND_UPDATE 2
+
+struct resend {
+    unsigned char kind;
+    unsigned char max;
+    unsigned short delay;
+    struct timeval time;
+    unsigned char prefix[16];
+    unsigned char plen;
+    unsigned short seqno;
+    unsigned char id[8];
+    struct interface *ifp;
+    struct resend *next;
+};
+
+extern struct timeval resend_time;
+
+struct resend *find_request(const unsigned char *prefix, unsigned char plen,
+                            struct resend **previous_return);
+void flush_resends(struct neighbour *neigh);
+int record_resend(int kind, const unsigned char *prefix, unsigned char plen,
+                   unsigned short seqno, const unsigned char *id,
+                   struct interface *ifp, int delay);
+int unsatisfied_request(const unsigned char *prefix, unsigned char plen,
+                        unsigned short seqno, const unsigned char *id);
+int request_redundant(struct interface *ifp,
+                      const unsigned char *prefix, unsigned char plen,
+                      unsigned short seqno, const unsigned char *id);
+int satisfy_request(const unsigned char *prefix, unsigned char plen,
+                    unsigned short seqno, const unsigned char *id,
+                    struct interface *ifp);
+
+void expire_resend(void);
+void recompute_resend_time(void);
+void do_resend(void);
diff --git a/babeld/route.c b/babeld/route.c
new file mode 100644
index 0000000..fe2b9ce
--- /dev/null
+++ b/babeld/route.c
@@ -0,0 +1,1019 @@
+/*  
+ *  This file is free software: you may copy, redistribute 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.  
+ *  
+ *  This file is distributed in the hope that it will be useful, but  
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+ *  General Public License for more details.  
+ *  
+ *  You should have received a copy of the GNU General Public License  
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ *  
+ * This file incorporates work covered by the following copyright and  
+ * permission notice:  
+ *  
+Copyright (c) 2007, 2008 by Juliusz Chroboczek
+Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
+
+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
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#include <zebra.h>
+#include "if.h"
+
+#include "babeld.h"
+#include "util.h"
+#include "kernel.h"
+#include "babel_interface.h"
+#include "source.h"
+#include "neighbour.h"
+#include "route.h"
+#include "xroute.h"
+#include "message.h"
+#include "resend.h"
+
+static void consider_route(struct babel_route *route);
+
+struct babel_route **routes = NULL;
+static int route_slots = 0, max_route_slots = 0;
+int kernel_metric = 0;
+int allow_duplicates = -1;
+int diversity_kind = DIVERSITY_NONE;
+int diversity_factor = 256;     /* in units of 1/256 */
+int keep_unfeasible = 0;
+
+/* We maintain a list of "slots", ordered by prefix.  Every slot
+   contains a linked list of the routes to this prefix, with the
+   installed route, if any, at the head of the list. */
+
+static int
+route_compare(const unsigned char *prefix, unsigned char plen,
+               struct babel_route *route)
+{
+    int i = memcmp(prefix, route->src->prefix, 16);
+    if(i != 0)
+        return i;
+
+    if(plen < route->src->plen)
+        return -1;
+    else if(plen > route->src->plen)
+        return 1;
+    else
+        return 0;
+}
+
+/* Performs binary search, returns -1 in case of failure.  In the latter
+   case, new_return is the place where to insert the new element. */
+
+static int
+find_route_slot(const unsigned char *prefix, unsigned char plen,
+                int *new_return)
+{
+    int p, m, g, c;
+
+    if(route_slots < 1) {
+        if(new_return)
+            *new_return = 0;
+        return -1;
+    }
+
+    p = 0; g = route_slots - 1;
+
+    do {
+        m = (p + g) / 2;
+        c = route_compare(prefix, plen, routes[m]);
+        if(c == 0)
+            return m;
+        else if(c < 0)
+            g = m - 1;
+        else
+            p = m + 1;
+    } while(p <= g);
+
+    if(new_return)
+        *new_return = p;
+
+    return -1;
+}
+
+struct babel_route *
+find_route(const unsigned char *prefix, unsigned char plen,
+           struct neighbour *neigh, const unsigned char *nexthop)
+{
+    struct babel_route *route;
+    int i = find_route_slot(prefix, plen, NULL);
+
+    if(i < 0)
+        return NULL;
+
+    route = routes[i];
+
+    while(route) {
+        if(route->neigh == neigh && memcmp(route->nexthop, nexthop, 16) == 0)
+            return route;
+        route = route->next;
+    }
+
+    return NULL;
+}
+
+struct babel_route *
+find_installed_route(const unsigned char *prefix, unsigned char plen)
+{
+    int i = find_route_slot(prefix, plen, NULL);
+
+    if(i >= 0 && routes[i]->installed)
+        return routes[i];
+
+    return NULL;
+}
+
+/* Returns an overestimate of the number of installed routes. */
+int
+installed_routes_estimate(void)
+{
+    return route_slots;
+}
+
+static int
+resize_route_table(int new_slots)
+{
+    struct babel_route **new_routes;
+    assert(new_slots >= route_slots);
+
+    if(new_slots == 0) {
+        new_routes = NULL;
+        free(routes);
+    } else {
+        new_routes = realloc(routes, new_slots * sizeof(struct babel_route*));
+        if(new_routes == NULL)
+            return -1;
+    }
+
+    max_route_slots = new_slots;
+    routes = new_routes;
+    return 1;
+}
+
+/* Insert a route into the table.  If successful, retains the route.
+   On failure, caller must free the route. */
+static struct babel_route *
+insert_route(struct babel_route *route)
+{
+    int i, n;
+
+    assert(!route->installed);
+
+    i = find_route_slot(route->src->prefix, route->src->plen, &n);
+
+    if(i < 0) {
+        if(route_slots >= max_route_slots)
+            resize_route_table(max_route_slots < 1 ? 8 : 2 * max_route_slots);
+        if(route_slots >= max_route_slots)
+            return NULL;
+        route->next = NULL;
+        if(n < route_slots)
+            memmove(routes + n + 1, routes + n,
+                    (route_slots - n) * sizeof(struct babel_route*));
+        route_slots++;
+        routes[n] = route;
+    } else {
+        struct babel_route *r;
+        r = routes[i];
+        while(r->next)
+            r = r->next;
+        r->next = route;
+        route->next = NULL;
+    }
+
+    return route;
+}
+
+void
+flush_route(struct babel_route *route)
+{
+    int i;
+    struct source *src;
+    unsigned oldmetric;
+    int lost = 0;
+
+    oldmetric = route_metric(route);
+    src = route->src;
+
+    if(route->installed) {
+        uninstall_route(route);
+        lost = 1;
+    }
+
+    i = find_route_slot(route->src->prefix, route->src->plen, NULL);
+    assert(i >= 0 && i < route_slots);
+
+    if(route == routes[i]) {
+        routes[i] = route->next;
+        route->next = NULL;
+        free(route);
+
+        if(routes[i] == NULL) {
+            if(i < route_slots - 1)
+                memmove(routes + i, routes + i + 1,
+                        (route_slots - i - 1) * sizeof(struct babel_route*));
+            routes[route_slots - 1] = NULL;
+            route_slots--;
+        }
+
+        if(route_slots == 0)
+            resize_route_table(0);
+        else if(max_route_slots > 8 && route_slots < max_route_slots / 4)
+            resize_route_table(max_route_slots / 2);
+    } else {
+        struct babel_route *r = routes[i];
+        while(r->next != route)
+            r = r->next;
+        r->next = route->next;
+        route->next = NULL;
+        free(route);
+    }
+
+    if(lost)
+        route_lost(src, oldmetric);
+
+    release_source(src);
+}
+
+void
+flush_all_routes()
+{
+    int i;
+
+    /* Start from the end, to avoid shifting the table. */
+    i = route_slots - 1;
+    while(i >= 0) {
+        while(i < route_slots) {
+        /* Uninstall first, to avoid calling route_lost. */
+            if(routes[i]->installed)
+                uninstall_route(routes[0]);
+            flush_route(routes[i]);
+        }
+        i--;
+    }
+
+    check_sources_released();
+}
+
+void
+flush_neighbour_routes(struct neighbour *neigh)
+{
+    int i;
+
+    i = 0;
+    while(i < route_slots) {
+        struct babel_route *r;
+        r = routes[i];
+        while(r) {
+            if(r->neigh == neigh) {
+                flush_route(r);
+                goto again;
+            }
+            r = r->next;
+        }
+        i++;
+    again:
+        ;
+    }
+}
+
+void
+flush_interface_routes(struct interface *ifp, int v4only)
+{
+    int i;
+
+    i = 0;
+    while(i < route_slots) {
+        struct babel_route *r;
+        r = routes[i];
+        while(r) {
+            if(r->neigh->ifp == ifp &&
+               (!v4only || v4mapped(r->nexthop))) {
+                flush_route(r);
+                goto again;
+            }
+            r = r->next;
+        }
+        i++;
+    again:
+        ;
+    }
+}
+
+/* Iterate a function over all routes. */
+void
+for_all_routes(void (*f)(struct babel_route*, void*), void *closure)
+{
+    int i;
+
+    for(i = 0; i < route_slots; i++) {
+        struct babel_route *r = routes[i];
+        while(r) {
+            (*f)(r, closure);
+            r = r->next;
+        }
+    }
+}
+
+void
+for_all_installed_routes(void (*f)(struct babel_route*, void*), void *closure)
+{
+    int i;
+
+    for(i = 0; i < route_slots; i++) {
+        if(routes[i]->installed)
+            (*f)(routes[i], closure);
+    }
+}
+
+static int
+metric_to_kernel(int metric)
+{
+    return metric < INFINITY ? kernel_metric : KERNEL_INFINITY;
+}
+
+/* This is used to maintain the invariant that the installed route is at
+   the head of the list. */
+static void
+move_installed_route(struct babel_route *route, int i)
+{
+    assert(i >= 0 && i < route_slots);
+    assert(route->installed);
+
+    if(route != routes[i]) {
+        struct babel_route *r = routes[i];
+        while(r->next != route)
+            r = r->next;
+        r->next = route->next;
+        route->next = routes[i];
+        routes[i] = route;
+    }
+}
+
+void
+install_route(struct babel_route *route)
+{
+    int i, rc;
+
+    if(route->installed)
+        return;
+
+    if(!route_feasible(route))
+        zlog_err("WARNING: installing unfeasible route "
+                 "(this shouldn't happen).");
+
+    i = find_route_slot(route->src->prefix, route->src->plen, NULL);
+    assert(i >= 0 && i < route_slots);
+
+    if(routes[i] != route && routes[i]->installed) {
+        fprintf(stderr, "WARNING: attempting to install duplicate route "
+                "(this shouldn't happen).");
+        return;
+    }
+
+    rc = kernel_route(ROUTE_ADD, route->src->prefix, route->src->plen,
+                      route->nexthop,
+                      route->neigh->ifp->ifindex,
+                      metric_to_kernel(route_metric(route)), NULL, 0, 0);
+    if(rc < 0) {
+        int save = errno;
+        zlog_err("kernel_route(ADD): %s", safe_strerror(errno));
+        if(save != EEXIST)
+            return;
+    }
+    route->installed = 1;
+    move_installed_route(route, i);
+
+}
+
+void
+uninstall_route(struct babel_route *route)
+{
+    int rc;
+
+    if(!route->installed)
+        return;
+
+    rc = kernel_route(ROUTE_FLUSH, route->src->prefix, route->src->plen,
+                      route->nexthop,
+                      route->neigh->ifp->ifindex,
+                      metric_to_kernel(route_metric(route)), NULL, 0, 0);
+    if(rc < 0)
+        zlog_err("kernel_route(FLUSH): %s", safe_strerror(errno));
+
+    route->installed = 0;
+}
+
+/* This is equivalent to uninstall_route followed with install_route,
+   but without the race condition.  The destination of both routes
+   must be the same. */
+
+static void
+switch_routes(struct babel_route *old, struct babel_route *new)
+{
+    int rc;
+
+    if(!old) {
+        install_route(new);
+        return;
+    }
+
+    if(!old->installed)
+        return;
+
+    if(!route_feasible(new))
+        zlog_err("WARNING: switching to unfeasible route "
+                 "(this shouldn't happen).");
+
+    rc = kernel_route(ROUTE_MODIFY, old->src->prefix, old->src->plen,
+                      old->nexthop, old->neigh->ifp->ifindex,
+                      metric_to_kernel(route_metric(old)),
+                      new->nexthop, new->neigh->ifp->ifindex,
+                      metric_to_kernel(route_metric(new)));
+    if(rc < 0) {
+        zlog_err("kernel_route(MODIFY): %s", safe_strerror(errno));
+        return;
+    }
+
+    old->installed = 0;
+    new->installed = 1;
+    move_installed_route(new, find_route_slot(new->src->prefix, new->src->plen,
+                                              NULL));
+}
+
+static void
+change_route_metric(struct babel_route *route,
+                    unsigned refmetric, unsigned cost, unsigned add)
+{
+    int old, new;
+    int newmetric = MIN(refmetric + cost + add, INFINITY);
+
+    old = metric_to_kernel(route_metric(route));
+    new = metric_to_kernel(newmetric);
+
+    if(route->installed && old != new) {
+        int rc;
+        rc = kernel_route(ROUTE_MODIFY, route->src->prefix, route->src->plen,
+                          route->nexthop, route->neigh->ifp->ifindex,
+                          old,
+                          route->nexthop, route->neigh->ifp->ifindex,
+                          new);
+        if(rc < 0) {
+            zlog_err("kernel_route(MODIFY metric): %s", safe_strerror(errno));
+            return;
+        }
+    }
+
+    route->refmetric = refmetric;
+    route->cost = cost;
+    route->add_metric = add;
+}
+
+static void
+retract_route(struct babel_route *route)
+{
+    change_route_metric(route, INFINITY, INFINITY, 0);
+}
+
+int
+route_feasible(struct babel_route *route)
+{
+    return update_feasible(route->src, route->seqno, route->refmetric);
+}
+
+int
+route_old(struct babel_route *route)
+{
+    return route->time < babel_now.tv_sec - route->hold_time * 7 / 8;
+}
+
+int
+route_expired(struct babel_route *route)
+{
+    return route->time < babel_now.tv_sec - route->hold_time;
+}
+
+static int
+channels_interfere(int ch1, int ch2)
+{
+    if(ch1 == BABEL_IF_CHANNEL_NONINTERFERING
+       || ch2 == BABEL_IF_CHANNEL_NONINTERFERING)
+        return 0;
+    if(ch1 == BABEL_IF_CHANNEL_INTERFERING
+       || ch2 == BABEL_IF_CHANNEL_INTERFERING)
+        return 1;
+    return ch1 == ch2;
+}
+
+int
+route_interferes(struct babel_route *route, struct interface *ifp)
+{
+    struct babel_interface *babel_ifp = NULL;
+    switch(diversity_kind) {
+    case DIVERSITY_NONE:
+        return 1;
+    case DIVERSITY_INTERFACE_1:
+        return route->neigh->ifp == ifp;
+    case DIVERSITY_CHANNEL_1:
+    case DIVERSITY_CHANNEL:
+        if(route->neigh->ifp == ifp)
+            return 1;
+        babel_ifp = babel_get_if_nfo(ifp);
+        if(channels_interfere(babel_ifp->channel,
+                              babel_get_if_nfo(route->neigh->ifp)->channel))
+            return 1;
+        if(diversity_kind == DIVERSITY_CHANNEL) {
+            int i;
+            for(i = 0; i < DIVERSITY_HOPS; i++) {
+                if(route->channels[i] == 0)
+                    break;
+                if(channels_interfere(babel_ifp->channel, route->channels[i]))
+                    return 1;
+            }
+        }
+        return 0;
+    default:
+        fprintf(stderr, "Unknown kind of diversity.\n");
+        return 1;
+    }
+}
+
+int
+update_feasible(struct source *src,
+                unsigned short seqno, unsigned short refmetric)
+{
+    if(src == NULL)
+        return 1;
+
+    if(src->time < babel_now.tv_sec - SOURCE_GC_TIME)
+        /* Never mind what is probably stale data */
+        return 1;
+
+    if(refmetric >= INFINITY)
+        /* Retractions are always feasible */
+        return 1;
+
+    return (seqno_compare(seqno, src->seqno) > 0 ||
+            (src->seqno == seqno && refmetric < src->metric));
+}
+
+/* This returns the feasible route with the smallest metric. */
+struct babel_route *
+find_best_route(const unsigned char *prefix, unsigned char plen, int feasible,
+                struct neighbour *exclude)
+{
+    struct babel_route *route = NULL, *r = NULL;
+    int i = find_route_slot(prefix, plen, NULL);
+
+    if(i < 0)
+        return NULL;
+
+    route = routes[i];
+
+    r = route->next;
+    while(r) {
+        if(!route_expired(r) &&
+           (!feasible || route_feasible(r)) &&
+           (!exclude || r->neigh != exclude) &&
+           (route_metric(r) < route_metric(route)))
+            route = r;
+        r = r->next;
+    }
+    return route;
+}
+
+void
+update_route_metric(struct babel_route *route)
+{
+    int oldmetric = route_metric(route);
+
+    if(route_expired(route)) {
+        if(route->refmetric < INFINITY) {
+            route->seqno = seqno_plus(route->src->seqno, 1);
+            retract_route(route);
+            if(oldmetric < INFINITY)
+                route_changed(route, route->src, oldmetric);
+        }
+    } else {
+        struct neighbour *neigh = route->neigh;
+        int add_metric = input_filter(route->src->id,
+                                      route->src->prefix, route->src->plen,
+                                      neigh->address,
+                                      neigh->ifp->ifindex);
+        change_route_metric(route, route->refmetric,
+                            neighbour_cost(route->neigh), add_metric);
+        if(route_metric(route) != oldmetric)
+            route_changed(route, route->src, oldmetric);
+    }
+}
+
+/* Called whenever a neighbour's cost changes, to update the metric of
+   all routes through that neighbour.  Calls local_notify_neighbour. */
+void
+update_neighbour_metric(struct neighbour *neigh, int changed)
+{
+
+    if(changed) {
+        int i;
+
+        for(i = 0; i < route_slots; i++) {
+            struct babel_route *r = routes[i];
+            while(r) {
+                if(r->neigh == neigh)
+                    update_route_metric(r);
+                r = r->next;
+            }
+        }
+    }
+}
+
+void
+update_interface_metric(struct interface *ifp)
+{
+    int i;
+
+    for(i = 0; i < route_slots; i++) {
+        struct babel_route *r = routes[i];
+        while(r) {
+            if(r->neigh->ifp == ifp)
+                update_route_metric(r);
+            r = r->next;
+        }
+    }
+}
+
+/* This is called whenever we receive an update. */
+struct babel_route *
+update_route(const unsigned char *router_id,
+             const unsigned char *prefix, unsigned char plen,
+             unsigned short seqno, unsigned short refmetric,
+             unsigned short interval,
+             struct neighbour *neigh, const unsigned char *nexthop,
+             const unsigned char *channels, int channels_len)
+{
+    struct babel_route *route;
+    struct source *src;
+    int metric, feasible;
+    int add_metric;
+    int hold_time = MAX((4 * interval) / 100 + interval / 50, 15);
+
+    if(memcmp(router_id, myid, 8) == 0)
+        return NULL;
+
+    if(martian_prefix(prefix, plen)) {
+        zlog_err("Rejecting martian route to %s through %s.",
+                 format_prefix(prefix, plen), format_address(router_id));
+        return NULL;
+    }
+
+    add_metric = input_filter(router_id, prefix, plen,
+                              neigh->address, neigh->ifp->ifindex);
+    if(add_metric >= INFINITY)
+        return NULL;
+
+    route = find_route(prefix, plen, neigh, nexthop);
+
+    if(route && memcmp(route->src->id, router_id, 8) == 0)
+        /* Avoid scanning the source table. */
+        src = route->src;
+    else
+        src = find_source(router_id, prefix, plen, 1, seqno);
+
+    if(src == NULL)
+        return NULL;
+
+    feasible = update_feasible(src, seqno, refmetric);
+    metric = MIN((int)refmetric + neighbour_cost(neigh) + add_metric, INFINITY);
+
+    if(route) {
+        struct source *oldsrc;
+        unsigned short oldmetric;
+        int lost = 0;
+
+        oldsrc = route->src;
+        oldmetric = route_metric(route);
+
+        /* If a successor switches sources, we must accept his update even
+           if it makes a route unfeasible in order to break any routing loops
+           in a timely manner.  If the source remains the same, we ignore
+           the update. */
+        if(!feasible && route->installed) {
+            debugf(BABEL_DEBUG_COMMON,"Unfeasible update for installed route to %s "
+                   "(%s %d %d -> %s %d %d).",
+                   format_prefix(src->prefix, src->plen),
+                   format_address(route->src->id),
+                   route->seqno, route->refmetric,
+                   format_address(src->id), seqno, refmetric);
+            if(src != route->src) {
+                uninstall_route(route);
+                lost = 1;
+            }
+        }
+
+        route->src = retain_source(src);
+        if((feasible || keep_unfeasible) && refmetric < INFINITY)
+            route->time = babel_now.tv_sec;
+        route->seqno = seqno;
+        change_route_metric(route,
+                            refmetric, neighbour_cost(neigh), add_metric);
+        route->hold_time = hold_time;
+
+        route_changed(route, oldsrc, oldmetric);
+        if(lost)
+            route_lost(oldsrc, oldmetric);
+
+        if(!feasible)
+            send_unfeasible_request(neigh, route->installed && route_old(route),
+                                    seqno, metric, src);
+        release_source(oldsrc);
+    } else {
+        struct babel_route *new_route;
+
+        if(refmetric >= INFINITY)
+            /* Somebody's retracting a route we never saw. */
+            return NULL;
+        if(!feasible) {
+            send_unfeasible_request(neigh, 0, seqno, metric, src);
+            if(!keep_unfeasible)
+                return NULL;
+        }
+
+        route = malloc(sizeof(struct babel_route));
+        if(route == NULL) {
+            perror("malloc(route)");
+            return NULL;
+        }
+
+        route->src = retain_source(src);
+        route->refmetric = refmetric;
+        route->cost = neighbour_cost(neigh);
+        route->add_metric = add_metric;
+        route->seqno = seqno;
+        route->neigh = neigh;
+        memcpy(route->nexthop, nexthop, 16);
+        route->time = babel_now.tv_sec;
+        route->hold_time = hold_time;
+        route->installed = 0;
+        memset(&route->channels, 0, sizeof(route->channels));
+        if(channels_len > 0)
+            memcpy(&route->channels, channels,
+                   MIN(channels_len, DIVERSITY_HOPS));
+        route->next = NULL;
+        new_route = insert_route(route);
+        if(new_route == NULL) {
+            fprintf(stderr, "Couldn't insert route.\n");
+            free(route);
+            return NULL;
+        }
+        consider_route(route);
+    }
+    return route;
+}
+
+/* We just received an unfeasible update.  If it's any good, send
+   a request for a new seqno. */
+void
+send_unfeasible_request(struct neighbour *neigh, int force,
+                        unsigned short seqno, unsigned short metric,
+                        struct source *src)
+{
+    struct babel_route *route = find_installed_route(src->prefix, src->plen);
+
+    if(seqno_minus(src->seqno, seqno) > 100) {
+        /* Probably a source that lost its seqno.  Let it time-out. */
+        return;
+    }
+
+    if(force || !route || route_metric(route) >= metric + 512) {
+        send_unicast_multihop_request(neigh, src->prefix, src->plen,
+                                      src->metric >= INFINITY ?
+                                      src->seqno :
+                                      seqno_plus(src->seqno, 1),
+                                      src->id, 127);
+    }
+}
+
+/* This takes a feasible route and decides whether to install it. */
+static void
+consider_route(struct babel_route *route)
+{
+    struct babel_route *installed;
+    struct xroute *xroute;
+
+    if(route->installed)
+        return;
+
+    if(!route_feasible(route))
+        return;
+
+    xroute = find_xroute(route->src->prefix, route->src->plen);
+    if(xroute && (allow_duplicates < 0 || xroute->metric >= allow_duplicates))
+        return;
+
+    installed = find_installed_route(route->src->prefix, route->src->plen);
+
+    if(installed == NULL)
+        goto install;
+
+    if(route_metric(route) >= INFINITY)
+        return;
+
+    if(route_metric(installed) >= INFINITY)
+        goto install;
+
+    if(route_metric(installed) >= route_metric(route) + 64)
+        goto install;
+
+    return;
+
+ install:
+    switch_routes(installed, route);
+    if(installed && route->installed)
+        send_triggered_update(route, installed->src, route_metric(installed));
+    else
+        send_update(NULL, 1, route->src->prefix, route->src->plen);
+    return;
+}
+
+void
+retract_neighbour_routes(struct neighbour *neigh)
+{
+    int i;
+
+    for(i = 0; i < route_slots; i++) {
+        struct babel_route *r = routes[i];
+        while(r) {
+            if(r->neigh == neigh) {
+                if(r->refmetric != INFINITY) {
+                    unsigned short oldmetric = route_metric(r);
+                    retract_route(r);
+                    if(oldmetric != INFINITY)
+                        route_changed(r, r->src, oldmetric);
+                }
+            }
+            r = r->next;
+        }
+        i++;
+    }
+}
+
+void
+send_triggered_update(struct babel_route *route, struct source *oldsrc,
+                      unsigned oldmetric)
+{
+    unsigned newmetric, diff;
+    /* 1 means send speedily, 2 means resend */
+    int urgent;
+
+    if(!route->installed)
+        return;
+
+    newmetric = route_metric(route);
+    diff =
+        newmetric >= oldmetric ? newmetric - oldmetric : oldmetric - newmetric;
+
+    if(route->src != oldsrc || (oldmetric < INFINITY && newmetric >= INFINITY))
+        /* Switching sources can cause transient routing loops.
+           Retractions can cause blackholes. */
+        urgent = 2;
+    else if(newmetric > oldmetric && oldmetric < 6 * 256 && diff >= 512)
+        /* Route getting significantly worse */
+        urgent = 1;
+    else if(unsatisfied_request(route->src->prefix, route->src->plen,
+                                route->seqno, route->src->id))
+        /* Make sure that requests are satisfied speedily */
+        urgent = 1;
+    else if(oldmetric >= INFINITY && newmetric < INFINITY)
+        /* New route */
+        urgent = 0;
+    else if(newmetric < oldmetric && diff < 1024)
+        /* Route getting better.  This may be a transient fluctuation, so
+           don't advertise it to avoid making routes unfeasible later on. */
+        return;
+    else if(diff < 384)
+        /* Don't fret about trivialities */
+        return;
+    else
+        urgent = 0;
+
+    if(urgent >= 2)
+        send_update_resend(NULL, route->src->prefix, route->src->plen);
+    else
+        send_update(NULL, urgent, route->src->prefix, route->src->plen);
+
+    if(oldmetric < INFINITY) {
+        if(newmetric >= oldmetric + 512) {
+            send_request_resend(NULL, route->src->prefix, route->src->plen,
+                                route->src->metric >= INFINITY ?
+                                route->src->seqno :
+                                seqno_plus(route->src->seqno, 1),
+                                route->src->id);
+        } else if(newmetric >= oldmetric + 288) {
+            send_request(NULL, route->src->prefix, route->src->plen);
+        }
+    }
+}
+
+/* A route has just changed.  Decide whether to switch to a different route or
+   send an update. */
+void
+route_changed(struct babel_route *route,
+              struct source *oldsrc, unsigned short oldmetric)
+{
+    if(route->installed) {
+        if(route_metric(route) > oldmetric) {
+            struct babel_route *better_route;
+            better_route =
+                find_best_route(route->src->prefix, route->src->plen, 1, NULL);
+            if(better_route &&
+               route_metric(better_route) <= route_metric(route) - 96)
+                consider_route(better_route);
+        }
+
+        if(route->installed)
+            /* We didn't change routes after all. */
+            send_triggered_update(route, oldsrc, oldmetric);
+    } else {
+        /* Reconsider routes even when their metric didn't decrease,
+           they may not have been feasible before. */
+        consider_route(route);
+    }
+}
+
+/* We just lost the installed route to a given destination. */
+void
+route_lost(struct source *src, unsigned oldmetric)
+{
+    struct babel_route *new_route;
+    new_route = find_best_route(src->prefix, src->plen, 1, NULL);
+    if(new_route) {
+        consider_route(new_route);
+    } else if(oldmetric < INFINITY) {
+        /* Complain loudly. */
+        send_update_resend(NULL, src->prefix, src->plen);
+        send_request_resend(NULL, src->prefix, src->plen,
+                            src->metric >= INFINITY ?
+                            src->seqno : seqno_plus(src->seqno, 1),
+                            src->id);
+    }
+}
+
+/* This is called periodically to flush old routes.  It will also send
+   requests for routes that are about to expire. */
+void
+expire_routes(void)
+{
+    struct babel_route *r;
+    int i;
+
+    debugf(BABEL_DEBUG_COMMON,"Expiring old routes.");
+
+    i = 0;
+    while(i < route_slots) {
+        r = routes[i];
+        while(r) {
+            /* Protect against clock being stepped. */
+            if(r->time > babel_now.tv_sec || route_old(r)) {
+                flush_route(r);
+                goto again;
+            }
+
+            update_route_metric(r);
+
+            if(r->installed && r->refmetric < INFINITY) {
+                if(route_old(r))
+                    /* Route about to expire, send a request. */
+                    send_unicast_request(r->neigh,
+                                         r->src->prefix, r->src->plen);
+            }
+            r = r->next;
+        }
+        i++;
+    again:
+        ;
+    }
+}
diff --git a/babeld/route.h b/babeld/route.h
new file mode 100644
index 0000000..b6d2d29
--- /dev/null
+++ b/babeld/route.h
@@ -0,0 +1,135 @@
+/*  
+ *  This file is free software: you may copy, redistribute 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.  
+ *  
+ *  This file is distributed in the hope that it will be useful, but  
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+ *  General Public License for more details.  
+ *  
+ *  You should have received a copy of the GNU General Public License  
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ *  
+ * This file incorporates work covered by the following copyright and  
+ * permission notice:  
+ *  
+Copyright (c) 2007, 2008 by Juliusz Chroboczek
+Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
+
+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
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#ifndef BABEL_ROUTE_H
+#define BABEL_ROUTE_H
+
+#include "babel_interface.h"
+#include "source.h"
+
+#define DIVERSITY_NONE 0
+#define DIVERSITY_INTERFACE_1 1
+#define DIVERSITY_CHANNEL_1 2
+#define DIVERSITY_CHANNEL 3
+
+#define DIVERSITY_HOPS 8
+
+struct babel_route {
+    struct source *src;
+    unsigned short refmetric;
+    unsigned short cost;
+    unsigned short add_metric;
+    unsigned short seqno;
+    struct neighbour *neigh;
+    unsigned char nexthop[16];
+    time_t time;
+    unsigned short hold_time;    /* in seconds */
+    short installed;
+    unsigned char channels[DIVERSITY_HOPS];
+    struct babel_route *next;
+};
+
+extern struct babel_route **routes;
+extern int kernel_metric, allow_duplicates;
+extern int diversity_kind, diversity_factor;
+extern int keep_unfeasible;
+
+static inline int
+route_metric(const struct babel_route *route)
+{
+    int m = (int)route->refmetric + route->cost + route->add_metric;
+    return MIN(m, INFINITY);
+}
+
+static inline int
+route_metric_noninterfering(const struct babel_route *route)
+{
+    int m =
+        (int)route->refmetric +
+        (diversity_factor * route->cost + 128) / 256 +
+        route->add_metric;
+    m = MAX(m, route->refmetric + 1);
+    return MIN(m, INFINITY);
+}
+
+struct babel_route *find_route(const unsigned char *prefix, unsigned char plen,
+                         struct neighbour *neigh, const unsigned char *nexthop);
+struct babel_route *find_installed_route(const unsigned char *prefix,
+                                   unsigned char plen);
+int installed_routes_estimate(void);
+void flush_route(struct babel_route *route);
+void flush_all_routes(void);
+void flush_neighbour_routes(struct neighbour *neigh);
+void flush_interface_routes(struct interface *ifp, int v4only);
+void for_all_routes(void (*f)(struct babel_route*, void*), void *closure);
+void for_all_installed_routes(void (*f)(struct babel_route*, void*), void *closure);
+void install_route(struct babel_route *route);
+void uninstall_route(struct babel_route *route);
+void switch_route(struct babel_route *old, struct babel_route *new);
+int route_feasible(struct babel_route *route);
+int route_old(struct babel_route *route);
+int route_expired(struct babel_route *route);
+int route_interferes(struct babel_route *route, struct interface *ifp);
+int update_feasible(struct source *src,
+                    unsigned short seqno, unsigned short refmetric);
+struct babel_route *find_best_route(const unsigned char *prefix, unsigned char plen,
+                              int feasible, struct neighbour *exclude);
+struct babel_route *install_best_route(const unsigned char prefix[16],
+                                 unsigned char plen);
+void update_neighbour_metric(struct neighbour *neigh, int change);
+void update_interface_metric(struct interface *ifp);
+void update_route_metric(struct babel_route *route);
+struct babel_route *update_route(const unsigned char *id,
+                           const unsigned char *prefix, unsigned char plen,
+                           unsigned short seqno, unsigned short refmetric,
+                           unsigned short interval, struct neighbour *neigh,
+                           const unsigned char *nexthop,
+                           const unsigned char *channels, int channels_len);
+void retract_neighbour_routes(struct neighbour *neigh);
+void send_unfeasible_request(struct neighbour *neigh, int force,
+                             unsigned short seqno, unsigned short metric,
+                             struct source *src);
+void send_triggered_update(struct babel_route *route,
+                           struct source *oldsrc, unsigned oldmetric);
+void route_changed(struct babel_route *route,
+                   struct source *oldsrc, unsigned short oldmetric);
+void route_lost(struct source *src, unsigned oldmetric);
+void expire_routes(void);
+
+#endif
diff --git a/babeld/source.c b/babeld/source.c
new file mode 100644
index 0000000..772112d
--- /dev/null
+++ b/babeld/source.c
@@ -0,0 +1,180 @@
+/*  
+ *  This file is free software: you may copy, redistribute 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.  
+ *  
+ *  This file is distributed in the hope that it will be useful, but  
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+ *  General Public License for more details.  
+ *  
+ *  You should have received a copy of the GNU General Public License  
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ *  
+ * This file incorporates work covered by the following copyright and  
+ * permission notice:  
+ *  
+Copyright (c) 2007, 2008 by Juliusz Chroboczek
+
+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
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/time.h>
+
+#include "babel_main.h"
+#include "babeld.h"
+#include "util.h"
+#include "source.h"
+#include "babel_interface.h"
+#include "route.h"
+
+struct source *srcs = NULL;
+
+struct source*
+find_source(const unsigned char *id, const unsigned char *p, unsigned char plen,
+            int create, unsigned short seqno)
+{
+    struct source *src;
+
+    for(src = srcs; src; src = src->next) {
+        /* This should really be a hash table.  For now, check the
+           last byte first. */
+        if(src->id[7] != id[7])
+            continue;
+        if(memcmp(src->id, id, 8) != 0)
+            continue;
+        if(src->plen != plen)
+            continue;
+        if(memcmp(src->prefix, p, 16) == 0)
+            return src;
+    }
+
+    if(!create)
+        return NULL;
+
+    src = malloc(sizeof(struct source));
+    if(src == NULL) {
+        zlog_err("malloc(source): %s", safe_strerror(errno));
+        return NULL;
+    }
+
+    memcpy(src->id, id, 8);
+    memcpy(src->prefix, p, 16);
+    src->plen = plen;
+    src->seqno = seqno;
+    src->metric = INFINITY;
+    src->time = babel_now.tv_sec;
+    src->route_count = 0;
+    src->next = srcs;
+    srcs = src;
+    return src;
+}
+
+struct source *
+retain_source(struct source *src)
+{
+    assert(src->route_count < 0xffff);
+    src->route_count++;
+    return src;
+}
+
+void
+release_source(struct source *src)
+{
+    assert(src->route_count > 0);
+    src->route_count--;
+}
+
+int
+flush_source(struct source *src)
+{
+    if(src->route_count > 0)
+        /* The source is in use by a route. */
+        return 0;
+
+    if(srcs == src) {
+        srcs = src->next;
+    } else {
+        struct source *previous = srcs;
+        while(previous->next != src)
+            previous = previous->next;
+        previous->next = src->next;
+    }
+
+    free(src);
+    return 1;
+}
+
+void
+update_source(struct source *src,
+              unsigned short seqno, unsigned short metric)
+{
+    if(metric >= INFINITY)
+        return;
+
+    /* If a source is expired, pretend that it doesn't exist and update
+       it unconditionally.  This makes ensures that old data will
+       eventually be overridden, and prevents us from getting stuck if
+       a router loses its sequence number. */
+    if(src->time < babel_now.tv_sec - SOURCE_GC_TIME ||
+       seqno_compare(src->seqno, seqno) < 0 ||
+       (src->seqno == seqno && src->metric > metric)) {
+        src->seqno = seqno;
+        src->metric = metric;
+    }
+    src->time = babel_now.tv_sec;
+}
+
+void
+expire_sources()
+{
+    struct source *src;
+
+    src = srcs;
+    while(src) {
+        if(src->time > babel_now.tv_sec)
+            /* clock stepped */
+            src->time = babel_now.tv_sec;
+        if(src->time < babel_now.tv_sec - SOURCE_GC_TIME) {
+            struct source *old = src;
+            src = src->next;
+            flush_source(old);
+            continue;
+        }
+        src = src->next;
+    }
+}
+
+void
+check_sources_released(void)
+{
+    struct source *src;
+
+    for(src = srcs; src; src = src->next) {
+        if(src->route_count != 0)
+            fprintf(stderr, "Warning: source %s %s has refcount %d.\n",
+                    format_eui64(src->id),
+                    format_prefix(src->prefix, src->plen),
+                    (int)src->route_count);
+    }
+}
diff --git a/babeld/source.h b/babeld/source.h
new file mode 100644
index 0000000..62a7e1e
--- /dev/null
+++ b/babeld/source.h
@@ -0,0 +1,67 @@
+/*  
+ *  This file is free software: you may copy, redistribute 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.  
+ *  
+ *  This file is distributed in the hope that it will be useful, but  
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+ *  General Public License for more details.  
+ *  
+ *  You should have received a copy of the GNU General Public License  
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ *  
+ * This file incorporates work covered by the following copyright and  
+ * permission notice:  
+ *  
+Copyright (c) 2007, 2008 by Juliusz Chroboczek
+
+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
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#ifndef BABEL_SOURCE_H
+#define BABEL_SOURCE_H
+
+#define SOURCE_GC_TIME 200
+
+struct source {
+    struct source *next;
+    unsigned char id[8];
+    unsigned char prefix[16];
+    unsigned char plen;
+    unsigned short seqno;
+    unsigned short metric;
+    unsigned short route_count;
+    time_t time;
+};
+
+struct source *find_source(const unsigned char *id,
+                           const unsigned char *p,
+                           unsigned char plen,
+                           int create, unsigned short seqno);
+struct source *retain_source(struct source *src);
+void release_source(struct source *src);
+int flush_source(struct source *src);
+void update_source(struct source *src,
+                   unsigned short seqno, unsigned short metric);
+void expire_sources(void);
+void check_sources_released(void);
+
+#endif
diff --git a/babeld/util.c b/babeld/util.c
new file mode 100644
index 0000000..011f382
--- /dev/null
+++ b/babeld/util.c
@@ -0,0 +1,445 @@
+/*  
+ *  This file is free software: you may copy, redistribute 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.  
+ *  
+ *  This file is distributed in the hope that it will be useful, but  
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+ *  General Public License for more details.  
+ *  
+ *  You should have received a copy of the GNU General Public License  
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ *  
+ * This file incorporates work covered by the following copyright and  
+ * permission notice:  
+ *  
+Copyright (c) 2007, 2008 by Juliusz Chroboczek
+Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
+
+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
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <sys/time.h>
+#include <time.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "babel_main.h"
+#include "babeld.h"
+#include "util.h"
+
+unsigned
+roughly(unsigned value)
+{
+    return value * 3 / 4 + random() % (value / 2);
+}
+
+/* d = s1 - s2 */
+void
+timeval_minus(struct timeval *d,
+              const struct timeval *s1, const struct timeval *s2)
+{
+    if(s1->tv_usec >= s2->tv_usec) {
+        d->tv_usec = s1->tv_usec - s2->tv_usec;
+        d->tv_sec = s1->tv_sec - s2->tv_sec;
+    } else {
+        d->tv_usec = s1->tv_usec + 1000000 - s2->tv_usec;
+        d->tv_sec = s1->tv_sec - s2->tv_sec - 1;
+    }
+}
+
+unsigned
+timeval_minus_msec(const struct timeval *s1, const struct timeval *s2)
+{
+    if(s1->tv_sec < s2->tv_sec)
+        return 0;
+
+    /* Avoid overflow. */
+    if(s1->tv_sec - s2->tv_sec > 2000000)
+        return 2000000000;
+
+    if(s1->tv_sec > s2->tv_sec)
+        return
+            (unsigned)((unsigned)(s1->tv_sec - s2->tv_sec) * 1000 +
+                       ((int)s1->tv_usec - s2->tv_usec) / 1000);
+
+    if(s1->tv_usec <= s2->tv_usec)
+        return 0;
+
+    return (unsigned)(s1->tv_usec - s2->tv_usec) / 1000u;
+}
+
+/* d = s + msecs */
+void
+timeval_add_msec(struct timeval *d, const struct timeval *s, const int msecs)
+{
+    int usecs;
+    d->tv_sec = s->tv_sec + msecs / 1000;
+    usecs = s->tv_usec + (msecs % 1000) * 1000;
+    if(usecs < 1000000) {
+        d->tv_usec = usecs;
+    } else {
+        d->tv_usec = usecs - 1000000;
+        d->tv_sec++;
+    }
+}
+
+void
+set_timeout(struct timeval *timeout, int msecs)
+{
+    timeval_add_msec(timeout, &babel_now, roughly(msecs));
+}
+
+/* returns <0 if "s1" < "s2", etc. */
+int
+timeval_compare(const struct timeval *s1, const struct timeval *s2)
+{
+    if(s1->tv_sec < s2->tv_sec)
+        return -1;
+    else if(s1->tv_sec > s2->tv_sec)
+        return 1;
+    else if(s1->tv_usec < s2->tv_usec)
+        return -1;
+    else if(s1->tv_usec > s2->tv_usec)
+        return 1;
+    else
+        return 0;
+}
+
+/* set d at min(d, s) */
+/* {0, 0} represents infinity */
+void
+timeval_min(struct timeval *d, const struct timeval *s)
+{
+    if(s->tv_sec == 0)
+        return;
+
+    if(d->tv_sec == 0 || timeval_compare(d, s) > 0) {
+        *d = *s;
+    }
+}
+
+/* set d to min(d, x) with x in [secs, secs+1] */
+void
+timeval_min_sec(struct timeval *d, time_t secs)
+{
+    if(d->tv_sec == 0 || d->tv_sec > secs) {
+        d->tv_sec = secs;
+        d->tv_usec = random() % 1000000;
+    }
+}
+
+/* parse a float value in second and return the corresponding mili-seconds.
+ For example:
+ parse_msec("12.342345") returns 12342 */
+int
+parse_msec(const char *string)
+{
+    unsigned int in, fl;
+    int i, j;
+
+    in = fl = 0;
+    i = 0;
+    while(string[i] == ' ' || string[i] == '\t')
+        i++;
+    while(string[i] >= '0' && string[i] <= '9') {
+        in = in * 10 + string[i] - '0';
+        i++;
+    }
+    if(string[i] == '.') {
+        i++;
+        j = 0;
+        while(string[i] >= '0' && string[i] <= '9') {
+            fl = fl * 10 + string[i] - '0';
+            i++;
+            j++;
+        }
+
+        while(j > 3) {
+            fl /= 10;
+            j--;
+        }
+        while(j < 3) {
+            fl *= 10;
+            j++;
+        }
+    }
+
+    while(string[i] == ' ' || string[i] == '\t')
+        i++;
+
+    if(string[i] == '\0')
+        return in * 1000 + fl;
+
+    return -1;
+}
+
+int
+in_prefix(const unsigned char *restrict address,
+          const unsigned char *restrict prefix, unsigned char plen)
+{
+    unsigned char m;
+
+    if(plen > 128)
+        plen = 128;
+
+    if(memcmp(address, prefix, plen / 8) != 0)
+        return 0;
+
+    if(plen % 8 == 0)
+        return 1;
+
+    m = 0xFF << (8 - (plen % 8));
+
+    return ((address[plen / 8] & m) == (prefix[plen / 8] & m));
+}
+
+unsigned char *
+mask_prefix(unsigned char *restrict ret,
+            const unsigned char *restrict prefix, unsigned char plen)
+{
+    if(plen >= 128) {
+        memcpy(ret, prefix, 16);
+        return ret;
+    }
+
+    memset(ret, 0, 16);
+    memcpy(ret, prefix, plen / 8);
+    if(plen % 8 != 0)
+        ret[plen / 8] =
+            (prefix[plen / 8] & ((0xFF << (8 - (plen % 8))) & 0xFF));
+    return ret;
+}
+
+static const unsigned char v4prefix[16] =
+    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };
+
+static const unsigned char llprefix[16] =
+    {0xFE, 0x80};
+
+const char *
+format_address(const unsigned char *address)
+{
+    static char buf[4][INET6_ADDRSTRLEN];
+    static int i = 0;
+    i = (i + 1) % 4;
+    if(v4mapped(address))
+       inet_ntop(AF_INET, address + 12, buf[i], INET6_ADDRSTRLEN);
+    else
+       inet_ntop(AF_INET6, address, buf[i], INET6_ADDRSTRLEN);
+    return buf[i];
+}
+
+const char *
+format_prefix(const unsigned char *prefix, unsigned char plen)
+{
+    static char buf[4][INET6_ADDRSTRLEN + 4];
+    static int i = 0;
+    int n;
+    i = (i + 1) % 4;
+    if(plen >= 96 && v4mapped(prefix)) {
+        inet_ntop(AF_INET, prefix + 12, buf[i], INET6_ADDRSTRLEN);
+        n = strlen(buf[i]);
+        snprintf(buf[i] + n, INET6_ADDRSTRLEN + 4 - n, "/%d", plen - 96);
+    } else {
+        inet_ntop(AF_INET6, prefix, buf[i], INET6_ADDRSTRLEN);
+        n = strlen(buf[i]);
+        snprintf(buf[i] + n, INET6_ADDRSTRLEN + 4 - n, "/%d", plen);
+    }
+    return buf[i];
+}
+
+const char *
+format_eui64(const unsigned char *eui)
+{
+    static char buf[4][28];
+    static int i = 0;
+    i = (i + 1) % 4;
+    snprintf(buf[i], 28, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
+             eui[0], eui[1], eui[2], eui[3],
+             eui[4], eui[5], eui[6], eui[7]);
+    return buf[i];
+}
+
+const char *format_bool(const int b) {
+    return b ? "true" : "false";
+}
+
+int
+parse_address(const char *address, unsigned char *addr_r, int *af_r)
+{
+    struct in_addr ina;
+    struct in6_addr ina6;
+    int rc;
+
+    rc = inet_pton(AF_INET, address, &ina);
+    if(rc > 0) {
+        memcpy(addr_r, v4prefix, 12);
+        memcpy(addr_r + 12, &ina, 4);
+        if(af_r) *af_r = AF_INET;
+        return 0;
+    }
+
+    rc = inet_pton(AF_INET6, address, &ina6);
+    if(rc > 0) {
+        memcpy(addr_r, &ina6, 16);
+        if(af_r) *af_r = AF_INET6;
+        return 0;
+    }
+
+    return -1;
+}
+
+int
+parse_eui64(const char *eui, unsigned char *eui_r)
+{
+    int n;
+    n = sscanf(eui, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
+               &eui_r[0], &eui_r[1], &eui_r[2], &eui_r[3],
+               &eui_r[4], &eui_r[5], &eui_r[6], &eui_r[7]);
+    if(n == 8)
+        return 0;
+
+    n = sscanf(eui, "%02hhx-%02hhx-%02hhx-%02hhx-%02hhx-%02hhx-%02hhx-%02hhx",
+               &eui_r[0], &eui_r[1], &eui_r[2], &eui_r[3],
+               &eui_r[4], &eui_r[5], &eui_r[6], &eui_r[7]);
+    if(n == 8)
+        return 0;
+
+    n = sscanf(eui, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
+               &eui_r[0], &eui_r[1], &eui_r[2],
+               &eui_r[5], &eui_r[6], &eui_r[7]);
+    if(n == 6) {
+        eui_r[3] = 0xFF;
+        eui_r[4] = 0xFE;
+        return 0;
+    }
+    return -1;
+}
+
+int
+wait_for_fd(int direction, int fd, int msecs)
+{
+    fd_set fds;
+    int rc;
+    struct timeval tv;
+
+    tv.tv_sec = msecs / 1000;
+    tv.tv_usec = (msecs % 1000) * 1000;
+
+    FD_ZERO(&fds);
+    FD_SET(fd, &fds);
+    if(direction)
+        rc = select(fd + 1, NULL, &fds, NULL, &tv);
+    else
+        rc = select(fd + 1, &fds, NULL, NULL, &tv);
+
+    return rc;
+}
+
+int
+martian_prefix(const unsigned char *prefix, int plen)
+{
+    return
+        (plen >= 8 && prefix[0] == 0xFF) ||
+        (plen >= 10 && prefix[0] == 0xFE && (prefix[1] & 0xC0) == 0x80) ||
+        (plen >= 128 && memcmp(prefix, zeroes, 15) == 0 &&
+         (prefix[15] == 0 || prefix[15] == 1)) ||
+        (plen >= 96 && v4mapped(prefix) &&
+         ((plen >= 104 && (prefix[12] == 127 || prefix[12] == 0)) ||
+          (plen >= 100 && (prefix[12] & 0xE0) == 0xE0)));
+}
+
+int
+linklocal(const unsigned char *address)
+{
+    return memcmp(address, llprefix, 8) == 0;
+}
+
+int
+v4mapped(const unsigned char *address)
+{
+    return memcmp(address, v4prefix, 12) == 0;
+}
+
+void
+v4tov6(unsigned char *dst, const unsigned char *src)
+{
+    memcpy(dst, v4prefix, 12);
+    memcpy(dst + 12, src, 4);
+}
+
+void
+inaddr_to_uchar(unsigned char *dest, const struct in_addr *src)
+{
+    memcpy(dest, v4prefix, 12);
+    memcpy(dest + 12, src, 4);
+    assert(v4mapped(dest));
+}
+
+void
+uchar_to_inaddr(struct in_addr *dest, const unsigned char *src)
+{
+    assert(v4mapped(src));
+    memcpy(dest, src + 12, 4);
+}
+
+void
+in6addr_to_uchar(unsigned char *dest, const struct in6_addr *src)
+{
+    memcpy(dest, src, 16);
+}
+
+void
+uchar_to_in6addr(struct in6_addr *dest, const unsigned char *src)
+{
+    memcpy(dest, src, 16);
+}
+
+int
+daemonise()
+{
+    int rc;
+
+    fflush(stdout);
+    fflush(stderr);
+
+    rc = fork();
+    if(rc < 0)
+        return -1;
+
+    if(rc > 0)
+        exit(0);
+
+    rc = setsid();
+    if(rc < 0)
+        return -1;
+
+    return 1;
+}
diff --git a/babeld/util.h b/babeld/util.h
new file mode 100644
index 0000000..5d9d2f5
--- /dev/null
+++ b/babeld/util.h
@@ -0,0 +1,165 @@
+/*  
+ *  This file is free software: you may copy, redistribute 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.  
+ *  
+ *  This file is distributed in the hope that it will be useful, but  
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+ *  General Public License for more details.  
+ *  
+ *  You should have received a copy of the GNU General Public License  
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ *  
+ * This file incorporates work covered by the following copyright and  
+ * permission notice:  
+ *  
+Copyright (c) 2007, 2008 by Juliusz Chroboczek
+Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
+
+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
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#include "babeld.h"
+#include "babel_main.h"
+#include "log.h"
+
+#if defined(i386) || defined(__mc68020__) || defined(__x86_64__)
+#define DO_NTOHS(_d, _s) do{ _d = ntohs(*(const unsigned short*)(_s)); }while(0)
+#define DO_NTOHL(_d, _s) do{ _d = ntohl(*(const unsigned*)(_s)); } while(0)
+#define DO_HTONS(_d, _s) do{ *(unsigned short*)(_d) = htons(_s); } while(0)
+#define DO_HTONL(_d, _s) do{ *(unsigned*)(_d) = htonl(_s); } while(0)
+/* Some versions of gcc seem to be buggy, and ignore the packed attribute.
+   Disable this code until the issue is clarified. */
+/* #elif defined __GNUC__*/
+#elif 0
+struct __us { unsigned short x __attribute__((packed)); };
+#define DO_NTOHS(_d, _s) \
+    do { _d = ntohs(((const struct __us*)(_s))->x); } while(0)
+#define DO_HTONS(_d, _s) \
+    do { ((struct __us*)(_d))->x = htons(_s); } while(0)
+#else
+#define DO_NTOHS(_d, _s) \
+    do { short _dd; \
+         memcpy(&(_dd), (_s), 2); \
+         _d = ntohs(_dd); } while(0)
+#define DO_HTONS(_d, _s) \
+    do { unsigned short _dd; \
+         _dd = htons(_s); \
+         memcpy((_d), &(_dd), 2); } while(0)
+#endif
+
+static inline int
+seqno_compare(unsigned short s1, unsigned short s2)
+{
+    if(s1 == s2)
+        return 0;
+    else
+        return ((s2 - s1) & 0x8000) ? 1 : -1;
+}
+
+static inline short
+seqno_minus(unsigned short s1, unsigned short s2)
+{
+    return (short)((s1 - s2) & 0xFFFF);
+}
+
+static inline unsigned short
+seqno_plus(unsigned short s, int plus)
+{
+    return ((s + plus) & 0xFFFF);
+}
+
+unsigned roughly(unsigned value);
+void timeval_minus(struct timeval *d,
+                   const struct timeval *s1, const struct timeval *s2);
+unsigned timeval_minus_msec(const struct timeval *s1, const struct timeval *s2)
+    ATTRIBUTE ((pure));
+void timeval_add_msec(struct timeval *d,
+                      const struct timeval *s, const int msecs);
+void set_timeout (struct timeval *timeout, int msecs);
+int timeval_compare(const struct timeval *s1, const struct timeval *s2)
+    ATTRIBUTE ((pure));
+void timeval_min(struct timeval *d, const struct timeval *s);
+void timeval_min_sec(struct timeval *d, time_t secs);
+int parse_msec(const char *string) ATTRIBUTE ((pure));
+int in_prefix(const unsigned char *restrict address,
+              const unsigned char *restrict prefix, unsigned char plen)
+    ATTRIBUTE ((pure));
+unsigned char *mask_prefix(unsigned char *restrict ret,
+                           const unsigned char *restrict prefix,
+                           unsigned char plen);
+const char *format_address(const unsigned char *address);
+const char *format_prefix(const unsigned char *address, unsigned char prefix);
+const char *format_eui64(const unsigned char *eui);
+const char *format_bool(const int b);
+int parse_address(const char *address, unsigned char *addr_r, int *af_r);
+int parse_eui64(const char *eui, unsigned char *eui_r);
+int wait_for_fd(int direction, int fd, int msecs);
+int martian_prefix(const unsigned char *prefix, int plen) ATTRIBUTE ((pure));
+int linklocal(const unsigned char *address) ATTRIBUTE ((pure));
+int v4mapped(const unsigned char *address) ATTRIBUTE ((pure));
+void v4tov6(unsigned char *dst, const unsigned char *src);
+void inaddr_to_uchar(unsigned char *dest, const struct in_addr *src);
+void uchar_to_inaddr(struct in_addr *dest, const unsigned char *src);
+void in6addr_to_uchar(unsigned char *dest, const struct in6_addr *src);
+void uchar_to_in6addr(struct in6_addr *dest, const unsigned char *src);
+int daemonise(void);
+
+/* If debugging is disabled, we want to avoid calling format_address
+   for every omitted debugging message.  So debug is a macro.  But
+   vararg macros are not portable. */
+#if defined NO_DEBUG
+
+#if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
+#define debugf(...) do {} while(0)
+#elif defined __GNUC__
+#define debugf(_args...) do {} while(0)
+#else
+static inline void debugf(int level, const char *format, ...) { return; }
+#endif
+
+#else /* NO_DEBUG */
+
+/* some levels */
+#define BABEL_DEBUG_COMMON      (1 << 0)
+#define BABEL_DEBUG_KERNEL      (1 << 1)
+#define BABEL_DEBUG_FILTER      (1 << 2)
+#define BABEL_DEBUG_TIMEOUT     (1 << 3)
+#define BABEL_DEBUG_IF          (1 << 4)
+#define BABEL_DEBUG_ROUTE       (1 << 5)
+#define BABEL_DEBUG_ALL         (0xFFFF)
+
+#if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
+#define debugf(level, ...) \
+do { \
+if(UNLIKELY(debug & level)) zlog_debug(__VA_ARGS__);     \
+} while(0)
+#elif defined __GNUC__
+#define debugf(level, _args...) \
+do { \
+if(UNLIKELY(debug & level)) zlog_debug(_args);   \
+} while(0)
+#else
+static inline void debugf(int level, const char *format, ...) { return; }
+#endif
+
+#endif /* NO_DEBUG */
+
diff --git a/babeld/xroute.c b/babeld/xroute.c
new file mode 100644
index 0000000..8065167
--- /dev/null
+++ b/babeld/xroute.c
@@ -0,0 +1,237 @@
+/*  
+ *  This file is free software: you may copy, redistribute 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.  
+ *  
+ *  This file is distributed in the hope that it will be useful, but  
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+ *  General Public License for more details.  
+ *  
+ *  You should have received a copy of the GNU General Public License  
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ *  
+ * This file incorporates work covered by the following copyright and  
+ * permission notice:  
+ *  
+Copyright (c) 2007, 2008 by Juliusz Chroboczek
+Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
+
+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
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#include <zebra.h>
+#include "if.h"
+#include "log.h"
+
+#include "babeld.h"
+#include "kernel.h"
+#include "neighbour.h"
+#include "message.h"
+#include "route.h"
+#include "xroute.h"
+#include "util.h"
+#include "babel_interface.h"
+
+static int xroute_add_new_route(unsigned char prefix[16], unsigned char plen,
+                                unsigned short metric, unsigned int ifindex,
+                                int proto, int send_updates);
+
+static struct xroute *xroutes;
+static int numxroutes = 0, maxxroutes = 0;
+
+/* Add redistributed route to Babel table. */
+int
+babel_ipv4_route_add (struct zapi_ipv4 *api, struct prefix_ipv4 *prefix,
+                      unsigned int ifindex, struct in_addr *nexthop)
+{
+    unsigned char uchar_prefix[16];
+
+    inaddr_to_uchar(uchar_prefix, &prefix->prefix);
+    debugf(BABEL_DEBUG_ROUTE, "Adding new ipv4 route comming from Zebra.");
+    xroute_add_new_route(uchar_prefix, prefix->prefixlen + 96,
+                         api->metric, ifindex, 0, 1);
+    return 0;
+}
+
+/* Remove redistributed route from Babel table. */
+int
+babel_ipv4_route_delete (struct zapi_ipv4 *api, struct prefix_ipv4 *prefix,
+                         unsigned int ifindex)
+{
+    unsigned char uchar_prefix[16];
+    struct xroute *xroute = NULL;
+
+    inaddr_to_uchar(uchar_prefix, &prefix->prefix);
+    xroute = find_xroute(uchar_prefix, prefix->prefixlen + 96);
+    if (xroute != NULL) {
+        debugf(BABEL_DEBUG_ROUTE, "Removing ipv4 route (from zebra).");
+        flush_xroute(xroute);
+    }
+    return 0;
+}
+
+/* Add redistributed route to Babel table. */
+int
+babel_ipv6_route_add (struct zapi_ipv6 *api, struct prefix_ipv6 *prefix,
+                      unsigned int ifindex, struct in6_addr *nexthop)
+{
+    unsigned char uchar_prefix[16];
+
+    in6addr_to_uchar(uchar_prefix, &prefix->prefix);
+    debugf(BABEL_DEBUG_ROUTE, "Adding new route comming from Zebra.");
+    xroute_add_new_route(uchar_prefix, prefix->prefixlen, api->metric, ifindex,
+                         0, 1);
+    return 0;
+}
+
+/* Remove redistributed route from Babel table. */
+int
+babel_ipv6_route_delete (struct zapi_ipv6 *api, struct prefix_ipv6 *prefix,
+                         unsigned int ifindex)
+{
+    unsigned char uchar_prefix[16];
+    struct xroute *xroute = NULL;
+
+    in6addr_to_uchar(uchar_prefix, &prefix->prefix);
+    xroute = find_xroute(uchar_prefix, prefix->prefixlen);
+    if (xroute != NULL) {
+        debugf(BABEL_DEBUG_ROUTE, "Removing route (from zebra).");
+        flush_xroute(xroute);
+    }
+    return 0;
+}
+
+struct xroute *
+find_xroute(const unsigned char *prefix, unsigned char plen)
+{
+    int i;
+    for(i = 0; i < numxroutes; i++) {
+        if(xroutes[i].plen == plen &&
+           memcmp(xroutes[i].prefix, prefix, 16) == 0)
+            return &xroutes[i];
+    }
+    return NULL;
+}
+
+void
+flush_xroute(struct xroute *xroute)
+{
+    int i;
+
+    i = xroute - xroutes;
+    assert(i >= 0 && i < numxroutes);
+
+    if(i != numxroutes - 1)
+        memcpy(xroutes + i, xroutes + numxroutes - 1, sizeof(struct xroute));
+    numxroutes--;
+    VALGRIND_MAKE_MEM_UNDEFINED(xroutes + numxroutes, sizeof(struct xroute));
+
+    if(numxroutes == 0) {
+        free(xroutes);
+        xroutes = NULL;
+        maxxroutes = 0;
+    } else if(maxxroutes > 8 && numxroutes < maxxroutes / 4) {
+        struct xroute *new_xroutes;
+        int n = maxxroutes / 2;
+        new_xroutes = realloc(xroutes, n * sizeof(struct xroute));
+        if(new_xroutes == NULL)
+            return;
+        xroutes = new_xroutes;
+        maxxroutes = n;
+    }
+}
+
+static int
+add_xroute(unsigned char prefix[16], unsigned char plen,
+           unsigned short metric, unsigned int ifindex, int proto)
+{
+    struct xroute *xroute = find_xroute(prefix, plen);
+    if(xroute) {
+        if(xroute->metric <= metric)
+            return 0;
+        xroute->metric = metric;
+        return 1;
+    }
+
+    if(numxroutes >= maxxroutes) {
+        struct xroute *new_xroutes;
+        int n = maxxroutes < 1 ? 8 : 2 * maxxroutes;
+        new_xroutes = xroutes == NULL ?
+            malloc(n * sizeof(struct xroute)) :
+            realloc(xroutes, n * sizeof(struct xroute));
+        if(new_xroutes == NULL)
+            return -1;
+        maxxroutes = n;
+        xroutes = new_xroutes;
+    }
+
+    memcpy(xroutes[numxroutes].prefix, prefix, 16);
+    xroutes[numxroutes].plen = plen;
+    xroutes[numxroutes].metric = metric;
+    xroutes[numxroutes].ifindex = ifindex;
+    xroutes[numxroutes].proto = proto;
+    numxroutes++;
+    return 1;
+}
+
+/* Returns an overestimate of the number of xroutes. */
+int
+xroutes_estimate()
+{
+    return numxroutes;
+}
+
+void
+for_all_xroutes(void (*f)(struct xroute*, void*), void *closure)
+{
+    int i;
+
+    for(i = 0; i < numxroutes; i++)
+        (*f)(&xroutes[i], closure);
+}
+
+/* add an xroute, verifying some conditions; return 0 if there is no changes */
+static int
+xroute_add_new_route(unsigned char prefix[16], unsigned char plen,
+                     unsigned short metric, unsigned int ifindex,
+                     int proto, int send_updates)
+{
+    int rc;
+    if(martian_prefix(prefix, plen))
+        return 0;
+    metric = redistribute_filter(prefix, plen, ifindex, proto);
+    if(metric < INFINITY) {
+        rc = add_xroute(prefix, plen, metric, ifindex, proto);
+        if(rc > 0) {
+            struct babel_route *route;
+            route = find_installed_route(prefix, plen);
+            if(route) {
+                if(allow_duplicates < 0 ||
+                   metric < allow_duplicates)
+                    uninstall_route(route);
+            }
+            if(send_updates)
+                send_update(NULL, 0, prefix, plen);
+            return 1;
+        }
+    }
+    return 0;
+}
diff --git a/babeld/xroute.h b/babeld/xroute.h
new file mode 100644
index 0000000..4d4ab99
--- /dev/null
+++ b/babeld/xroute.h
@@ -0,0 +1,59 @@
+/*  
+ *  This file is free software: you may copy, redistribute 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.  
+ *  
+ *  This file is distributed in the hope that it will be useful, but  
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+ *  General Public License for more details.  
+ *  
+ *  You should have received a copy of the GNU General Public License  
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ *  
+ * This file incorporates work covered by the following copyright and  
+ * permission notice:  
+ *  
+Copyright (c) 2007, 2008 by Juliusz Chroboczek
+Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
+
+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
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+struct xroute {
+    unsigned char prefix[16];
+    unsigned char plen;
+    unsigned short metric;
+    unsigned int ifindex;
+    int proto;
+};
+
+struct xroute *find_xroute(const unsigned char *prefix, unsigned char plen);
+void flush_xroute(struct xroute *xroute);
+int babel_ipv4_route_add (struct zapi_ipv4 *api, struct prefix_ipv4 *prefix,
+                          unsigned int ifindex, struct in_addr *nexthop);
+int babel_ipv4_route_delete (struct zapi_ipv4 *api, struct prefix_ipv4 *prefix,
+                             unsigned int ifindex);
+int babel_ipv6_route_add (struct zapi_ipv6 *api, struct prefix_ipv6 *prefix,
+                          unsigned int ifindex, struct in6_addr *nexthop);
+int babel_ipv6_route_delete (struct zapi_ipv6 *api, struct prefix_ipv6 *prefix,
+                             unsigned int ifindex);
+int xroutes_estimate(void);
+void for_all_xroutes(void (*f)(struct xroute*, void*), void *closure);
diff --git a/bgpd/.cvsignore b/bgpd/.cvsignore
deleted file mode 100644
index f4504f6..0000000
--- a/bgpd/.cvsignore
+++ /dev/null
@@ -1,15 +0,0 @@
-Makefile
-Makefile.in
-*.o
-bgpd
-bgp_btoa
-bgpd.conf
-tags
-TAGS
-.deps
-.nfs*
-*.lo
-*.la
-*.libs
-.arch-inventory
-.arch-ids
diff --git a/bgpd/bgp_advertise.c b/bgpd/bgp_advertise.c
index 87eb7ac..666218f 100644
--- a/bgpd/bgp_advertise.c
+++ b/bgpd/bgp_advertise.c
@@ -140,13 +140,13 @@
     baa->refcnt--;
 
   if (baa->refcnt && baa->attr)
-    bgp_attr_unintern (baa->attr);
+    bgp_attr_unintern (&baa->attr);
   else
     {
       if (baa->attr)
 	{
 	  hash_release (hash, baa);
-	  bgp_attr_unintern (baa->attr);
+	  bgp_attr_unintern (&baa->attr);
 	}
       baa_free (baa);
     }
@@ -319,7 +319,7 @@
 		    struct peer *peer, afi_t afi, safi_t safi)
 {
   if (adj->attr)
-    bgp_attr_unintern (adj->attr);
+    bgp_attr_unintern (&adj->attr);
 
   if (adj->adv)
     bgp_advertise_clean (peer, adj, afi, safi);
@@ -339,7 +339,7 @@
 	{
 	  if (adj->attr != attr)
 	    {
-	      bgp_attr_unintern (adj->attr);
+	      bgp_attr_unintern (&adj->attr);
 	      adj->attr = bgp_attr_intern (attr);
 	    }
 	  return;
@@ -355,7 +355,7 @@
 void
 bgp_adj_in_remove (struct bgp_node *rn, struct bgp_adj_in *bai)
 {
-  bgp_attr_unintern (bai->attr);
+  bgp_attr_unintern (&bai->attr);
   BGP_ADJ_IN_DEL (rn, bai);
   peer_unlock (bai->peer); /* adj_in peer reference */
   XFREE (MTYPE_BGP_ADJ_IN, bai);
diff --git a/bgpd/bgp_aspath.c b/bgpd/bgp_aspath.c
index cf93042..776c712 100644
--- a/bgpd/bgp_aspath.c
+++ b/bgpd/bgp_aspath.c
@@ -91,13 +91,13 @@
 /* Stream for SNMP. See aspath_snmp_pathseg */
 static struct stream *snmp_stream;
 
-static inline as_t *
+static as_t *
 assegment_data_new (int num)
 {
   return (XCALLOC (MTYPE_AS_SEG_DATA, ASSEGMENT_DATA_SIZE (num, 1)));
 }
 
-static inline void
+static void
 assegment_data_free (as_t *asdata)
 {
   XFREE (MTYPE_AS_SEG_DATA,asdata);
@@ -340,19 +340,21 @@
 
 /* Unintern aspath from AS path bucket. */
 void
-aspath_unintern (struct aspath *aspath)
+aspath_unintern (struct aspath **aspath)
 {
   struct aspath *ret;
+  struct aspath *asp = *aspath;
+  
+  if (asp->refcnt)
+    asp->refcnt--;
 
-  if (aspath->refcnt)
-    aspath->refcnt--;
-
-  if (aspath->refcnt == 0)
+  if (asp->refcnt == 0)
     {
       /* This aspath must exist in aspath hash table. */
-      ret = hash_release (ashash, aspath);
+      ret = hash_release (ashash, asp);
       assert (ret != NULL);
-      aspath_free (aspath);
+      aspath_free (asp);
+      *aspath = NULL;
     }
 }
 
@@ -671,79 +673,78 @@
   return aspath;
 }
 
-/* parse as-segment byte stream in struct assegment
- *
- * Returns NULL if the AS_PATH or AS4_PATH is not valid.
- */
-static struct assegment *
-assegments_parse (struct stream *s, size_t length, int use32bit, int as4_path)
+/* parse as-segment byte stream in struct assegment */
+static int
+assegments_parse (struct stream *s, size_t length, 
+                  struct assegment **result, int use32bit)
 {
   struct assegment_header segh;
   struct assegment *seg, *prev = NULL, *head = NULL;
+  size_t bytes = 0;
   
-  assert (length > 0);  /* does not expect empty AS_PATH or AS4_PATH    */
+  /* empty aspath (ie iBGP or somesuch) */
+  if (length == 0)
+    return 0;
   
   if (BGP_DEBUG (as4, AS4_SEGMENT))
     zlog_debug ("[AS4SEG] Parse aspath segment: got total byte length %lu",
 		(unsigned long) length);
-
-  /* double check that length does not exceed stream    */
-  if (STREAM_READABLE(s) < length)
-    return NULL;
+  /* basic checks */
+  if ((STREAM_READABLE(s) < length)
+      || (STREAM_READABLE(s) < AS_HEADER_SIZE) 
+      || (length % AS16_VALUE_SIZE ))
+    return -1;
   
-  /* deal with each segment in turn                             */
-  while (length > 0)
+  while (bytes < length)
     {
       int i;
       size_t seg_size;
       
-      /* softly softly, get the header first on its own */
-      if (length < AS_HEADER_SIZE)
+      if ((length - bytes) <= AS_HEADER_SIZE)
         {
-          assegment_free_all (head);
-          return NULL;
+          if (head)
+            assegment_free_all (head);
+          return -1;
         }
       
+      /* softly softly, get the header first on its own */
       segh.type = stream_getc (s);
       segh.length = stream_getc (s);
       
       seg_size = ASSEGMENT_SIZE(segh.length, use32bit);
-                                      /* includes the header bytes */
 
       if (BGP_DEBUG (as4, AS4_SEGMENT))
 	zlog_debug ("[AS4SEG] Parse aspath segment: got type %d, length %d",
                     segh.type, segh.length);
       
+      /* check it.. */
+      if ( ((bytes + seg_size) > length)
+          /* 1771bis 4.3b: seg length contains one or more */
+          || (segh.length == 0) 
+          /* Paranoia in case someone changes type of segment length.
+           * Shift both values by 0x10 to make the comparison operate
+           * on more, than 8 bits (otherwise it's a warning, bug #564).
+           */
+          || ((sizeof segh.length > 1) 
+              && (0x10 + segh.length > 0x10 + AS_SEGMENT_MAX)))
+        {
+          if (head)
+            assegment_free_all (head);
+          return -1;
+        }
+      
       switch (segh.type)
         {
           case AS_SEQUENCE:
           case AS_SET:
-            break ;
-
           case AS_CONFED_SEQUENCE:
           case AS_CONFED_SET:
-            if (!as4_path)
-              break ;
-              /* RFC4893 3: "invalid for the AS4_PATH attribute"            */
-              /* fall through */
-
-          default:    /* reject unknown or invalid AS_PATH segment types  */
-            seg_size = 0 ;
+            break;
+          default:
+            if (head)
+              assegment_free_all (head);
+            return -1;
         }
-     
-     /* Stop now if segment is not valid (discarding anything collected to date)
-      *
-      * RFC4271 4.3, Path Attributes, b) AS_PATH:
-      *
-      *   "path segment value field contains one or more AS numbers"
-           */
-      if ((seg_size == 0) || (seg_size > length) || (segh.length == 0))
-        {
-          assegment_free_all (head);
-          return NULL;
-        }
-      
-      length -= seg_size ;
       
       /* now its safe to trust lengths */
       seg = assegment_new (segh.type, segh.length);
@@ -756,52 +757,47 @@
       for (i = 0; i < segh.length; i++)
 	seg->as[i] = (use32bit) ? stream_getl (s) : stream_getw (s);
 
+      bytes += seg_size;
+      
       if (BGP_DEBUG (as4, AS4_SEGMENT))
-	zlog_debug ("[AS4SEG] Parse aspath segment: length left: %lu",
-	            (unsigned long) length);
+	zlog_debug ("[AS4SEG] Parse aspath segment: Bytes now: %lu",
+	            (unsigned long) bytes);
       
       prev = seg;
     }
  
-  return assegment_normalise (head);
+  *result = assegment_normalise (head);
+  return 0;
 }
 
-/* AS path parse function -- parses AS_PATH and AS4_PATH attributes
- *
- * Requires: s        -- stream, currently positioned before first segment
- *                       of AS_PATH or AS4_PATH (ie after attribute header)
- *           length   -- length of the value of the AS_PATH or AS4_PATH
- *           use32bit -- true <=> 4Byte ASN, otherwise 2Byte ASN
- *           as4_path -- true <=> AS4_PATH, otherwise AS_PATH
- *
- * Returns: if valid: address of struct aspath in the hash of known aspaths,
- *                    with reference count incremented.
- *              else: NULL
- *
- * NB: empty AS path (length == 0) is valid.  The returned struct aspath will
- *     have segments == NULL and str == zero length string (unique).
+/* AS path parse function.  pnt is a pointer to byte stream and length
+   is length of byte stream.  If there is same AS path in the the AS
+   path hash then return it else make new AS path structure. 
+   
+   On error NULL is returned.
  */
 struct aspath *
-aspath_parse (struct stream *s, size_t length, int use32bit, int as4_path)
+aspath_parse (struct stream *s, size_t length, int use32bit)
 {
   struct aspath as;
   struct aspath *find;
 
-  /* Parse each segment and construct normalised list of struct assegment */
-  memset (&as, 0, sizeof (struct aspath));
-  if (length != 0)
-    {
-      as.segments = assegments_parse (s, length, use32bit, as4_path);
+  /* If length is odd it's malformed AS path. */
+  /* Nit-picking: if (use32bit == 0) it is malformed if odd,
+   * otherwise its malformed when length is larger than 2 and (length-2) 
+   * is not dividable by 4.
+   * But... this time we're lazy
+   */
+  if (length % AS16_VALUE_SIZE )
+    return NULL;
 
-      if (as.segments == NULL)
-        return NULL ;   /* Invalid AS_PATH or AS4_PATH  */
-    } ;
+  memset (&as, 0, sizeof (struct aspath));
+  if (assegments_parse (s, length, &as.segments, use32bit) < 0)
+    return NULL;
   
   /* If already same aspath exist then return it. */
   find = hash_get (ashash, &as, aspath_hash_alloc);
   
-  assert(find) ;        /* valid aspath, so must find or create */
-  
   /* aspath_hash_alloc dupes segments too. that probably could be
    * optimised out.
    */
@@ -809,12 +805,14 @@
   if (as.str)
     XFREE (MTYPE_AS_STR, as.str);
   
+  if (! find)
+    return NULL;
   find->refcnt++;
 
   return find;
 }
 
-static inline void
+static void
 assegment_data_put (struct stream *s, as_t *as, int num, int use32bit)
 {
   int i;
@@ -832,7 +830,7 @@
       }
 }
 
-static inline size_t
+static size_t
 assegment_header_put (struct stream *s, u_char type, int length)
 {
   size_t lenp;
@@ -1632,7 +1630,7 @@
 struct aspath *
 aspath_empty (void)
 {
-  return aspath_parse (NULL, 0, 1, 0); /* 32Bit ;-) not AS4_PATH */
+  return aspath_parse (NULL, 0, 1); /* 32Bit ;-) */
 }
 
 struct aspath *
diff --git a/bgpd/bgp_aspath.h b/bgpd/bgp_aspath.h
index d63b914..d55f9ce 100644
--- a/bgpd/bgp_aspath.h
+++ b/bgpd/bgp_aspath.h
@@ -65,7 +65,7 @@
 /* Prototypes. */
 extern void aspath_init (void);
 extern void aspath_finish (void);
-extern struct aspath *aspath_parse (struct stream *, size_t, int, int);
+extern struct aspath *aspath_parse (struct stream *, size_t, int);
 extern struct aspath *aspath_dup (struct aspath *);
 extern struct aspath *aspath_aggregate (struct aspath *, struct aspath *);
 extern struct aspath *aspath_prepend (struct aspath *, struct aspath *);
@@ -80,7 +80,7 @@
 extern struct aspath *aspath_str2aspath (const char *);
 extern void aspath_free (struct aspath *);
 extern struct aspath *aspath_intern (struct aspath *);
-extern void aspath_unintern (struct aspath *);
+extern void aspath_unintern (struct aspath **);
 extern const char *aspath_print (struct aspath *);
 extern void aspath_print_vty (struct vty *, const char *, struct aspath *, const char *);
 extern void aspath_print_all_vty (struct vty *);
diff --git a/bgpd/bgp_attr.c b/bgpd/bgp_attr.c
index 01598c8..0d82aba 100644
--- a/bgpd/bgp_attr.c
+++ b/bgpd/bgp_attr.c
@@ -51,7 +51,7 @@
   { BGP_ATTR_AGGREGATOR,       "AGGREGATOR" }, 
   { BGP_ATTR_COMMUNITIES,      "COMMUNITY" }, 
   { BGP_ATTR_ORIGINATOR_ID,    "ORIGINATOR_ID" },
-  { BGP_ATTR_CLUSTER_LIST,     "CLUSTERLIST" }, 
+  { BGP_ATTR_CLUSTER_LIST,     "CLUSTER_LIST" }, 
   { BGP_ATTR_DPA,              "DPA" },
   { BGP_ATTR_ADVERTISER,       "ADVERTISER"} ,
   { BGP_ATTR_RCID_PATH,        "RCID_PATH" },
@@ -63,6 +63,17 @@
   { BGP_ATTR_AS_PATHLIMIT,     "AS_PATHLIMIT" },
 };
 static const int attr_str_max = sizeof(attr_str)/sizeof(attr_str[0]);
+
+static const struct message attr_flag_str[] =
+{
+  { BGP_ATTR_FLAG_OPTIONAL, "Optional" },
+  { BGP_ATTR_FLAG_TRANS,    "Transitive" },
+  { BGP_ATTR_FLAG_PARTIAL,  "Partial" },
+  /* bgp_attr_flags_diagnose() relies on this bit being last in this list */
+  { BGP_ATTR_FLAG_EXTLEN,   "Extended Length" },
+};
+static const size_t attr_flag_str_max =
+  sizeof (attr_flag_str) / sizeof (attr_flag_str[0]);
 
 static struct hash *cluster_hash;
 
@@ -175,14 +186,12 @@
 void
 cluster_unintern (struct cluster_list *cluster)
 {
-  struct cluster_list *ret;
-
   if (cluster->refcnt)
     cluster->refcnt--;
 
   if (cluster->refcnt == 0)
     {
-      ret = hash_release (cluster_hash, cluster);
+      hash_release (cluster_hash, cluster);
       cluster_free (cluster);
     }
 }
@@ -235,14 +244,12 @@
 void
 transit_unintern (struct transit *transit)
 {
-  struct transit *ret;
-
   if (transit->refcnt)
     transit->refcnt--;
 
   if (transit->refcnt == 0)
     {
-      ret = hash_release (transit_hash, transit);
+      hash_release (transit_hash, transit);
       transit_free (transit);
     }
 }
@@ -500,6 +507,7 @@
             attre->ecommunity = ecommunity_intern (attre->ecommunity);
           else
             attre->ecommunity->refcnt++;
+          
         }
       if (attre->cluster)
         {
@@ -516,10 +524,10 @@
             attre->transit->refcnt++;
         }
     }
-
+  
   find = (struct attr *) hash_get (attrhash, attr, bgp_attr_hash_alloc);
   find->refcnt++;
-
+  
   return find;
 }
 
@@ -551,17 +559,16 @@
 {
   struct attr attr;
   struct attr *new;
-  struct attr_extra *attre;
   
   memset (&attr, 0, sizeof (struct attr));
-  attre = bgp_attr_extra_get (&attr);
+  bgp_attr_extra_get (&attr);
   
   bgp_attr_default_set(&attr, origin);
 
   new = bgp_attr_intern (&attr);
   bgp_attr_extra_free (&attr);
   
-  aspath_unintern (new->aspath);
+  aspath_unintern (&new->aspath);
   return new;
 }
 
@@ -613,52 +620,68 @@
   new = bgp_attr_intern (&attr);
   bgp_attr_extra_free (&attr);
   
-  aspath_unintern (new->aspath);
+  aspath_unintern (&new->aspath);
   return new;
 }
 
+/* Unintern just the sub-components of the attr, but not the attr */
+void
+bgp_attr_unintern_sub (struct attr *attr)
+{
+  /* aspath refcount shoud be decrement. */
+  if (attr->aspath)
+    aspath_unintern (&attr->aspath);
+  UNSET_FLAG(attr->flag, BGP_ATTR_AS_PATH);
+  
+  if (attr->community)
+    community_unintern (&attr->community);
+  UNSET_FLAG(attr->flag, BGP_ATTR_COMMUNITIES);
+  
+  if (attr->extra)
+    {
+      if (attr->extra->ecommunity)
+        ecommunity_unintern (&attr->extra->ecommunity);
+      UNSET_FLAG(attr->flag, BGP_ATTR_EXT_COMMUNITIES);
+      
+      if (attr->extra->cluster)
+        cluster_unintern (attr->extra->cluster);
+      UNSET_FLAG(attr->flag, BGP_ATTR_CLUSTER_LIST);
+      
+      if (attr->extra->transit)
+        transit_unintern (attr->extra->transit);
+    }
+}
+
 /* Free bgp attribute and aspath. */
 void
-bgp_attr_unintern (struct attr *attr)
+bgp_attr_unintern (struct attr **attr)
 {
   struct attr *ret;
-  struct aspath *aspath;
-  struct community *community;
-  struct ecommunity *ecommunity = NULL;
-  struct cluster_list *cluster = NULL;
-  struct transit *transit = NULL;
-
+  struct attr tmp;
+  
   /* Decrement attribute reference. */
-  attr->refcnt--;
-  aspath = attr->aspath;
-  community = attr->community;
-  if (attr->extra)
+  (*attr)->refcnt--;
+  
+  tmp = *(*attr);
+  
+  if ((*attr)->extra)
     {
-      ecommunity = attr->extra->ecommunity;
-      cluster = attr->extra->cluster;
-      transit = attr->extra->transit;
+      tmp.extra = bgp_attr_extra_new ();
+      memcpy (tmp.extra, (*attr)->extra, sizeof (struct attr_extra));
     }
-
+  
   /* If reference becomes zero then free attribute object. */
-  if (attr->refcnt == 0)
+  if ((*attr)->refcnt == 0)
     {    
-      ret = hash_release (attrhash, attr);
+      ret = hash_release (attrhash, *attr);
       assert (ret != NULL);
-      bgp_attr_extra_free (attr);
-      XFREE (MTYPE_ATTR, attr);
+      bgp_attr_extra_free (*attr);
+      XFREE (MTYPE_ATTR, *attr);
+      *attr = NULL;
     }
 
-  /* aspath refcount shoud be decrement. */
-  if (aspath)
-    aspath_unintern (aspath);
-  if (community)
-    community_unintern (community);
-  if (ecommunity)
-    ecommunity_unintern (ecommunity);
-  if (cluster)
-    cluster_unintern (cluster);
-  if (transit)
-    transit_unintern (transit);
+  bgp_attr_unintern_sub (&tmp);
+  bgp_attr_extra_free (&tmp);
 }
 
 void
@@ -671,8 +694,9 @@
   if (attr->extra)
     {
       struct attr_extra *attre = attr->extra;
+
       if (attre->ecommunity && ! attre->ecommunity->refcnt)
-        ecommunity_free (attre->ecommunity);
+        ecommunity_free (&attre->ecommunity);
       if (attre->cluster && ! attre->cluster->refcnt)
         cluster_free (attre->cluster);
       if (attre->transit && ! attre->transit->refcnt)
@@ -680,32 +704,217 @@
     }
 }
 
-/* Get origin attribute of the update message. */
-static int
-bgp_attr_origin (struct peer *peer, bgp_size_t length, 
-		 struct attr *attr, u_char flag, u_char *startp)
+/* Implement draft-scudder-idr-optional-transitive behaviour and
+ * avoid resetting sessions for malformed attributes which are
+ * are partial/optional and hence where the error likely was not
+ * introduced by the sending neighbour.
+ */
+static bgp_attr_parse_ret_t
+bgp_attr_malformed (struct bgp_attr_parser_args *args, u_char subcode,
+                    bgp_size_t length)
 {
-  bgp_size_t total;
-
-  /* total is entire attribute length include Attribute Flags (1),
-     Attribute Type code (1) and Attribute length (1 or 2).  */
-  total = length + (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN) ? 4 : 3);
-
-  /* If any recognized attribute has Attribute Flags that conflict
-     with the Attribute Type Code, then the Error Subcode is set to
-     Attribute Flags Error.  The Data field contains the erroneous
-     attribute (type, length and value). */
-  if (flag != BGP_ATTR_FLAG_TRANS)
+  struct peer *const peer = args->peer; 
+  const u_int8_t flags = args->flags;
+  /* startp and length must be special-cased, as whether or not to
+   * send the attribute data with the NOTIFY depends on the error,
+   * the caller therefore signals this with the seperate length argument
+   */
+  u_char *notify_datap = (length > 0 ? args->startp : NULL);
+  
+  /* Only relax error handling for eBGP peers */
+  if (peer_sort (peer) != BGP_PEER_EBGP)
     {
-      zlog (peer->log, LOG_ERR, 
-	    "Origin attribute flag isn't transitive %d", flag);
-      bgp_notify_send_with_data (peer, 
-				 BGP_NOTIFY_UPDATE_ERR, 
-				 BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR,
-				 startp, total);
-      return -1;
-    }
+      bgp_notify_send_with_data (peer, BGP_NOTIFY_UPDATE_ERR, subcode,
+                                 notify_datap, length);
+      return BGP_ATTR_PARSE_ERROR;
 
+    }
+  
+  /* Adjust the stream getp to the end of the attribute, in case we can
+   * still proceed but the caller hasn't read all the attribute.
+   */
+  stream_set_getp (BGP_INPUT (peer),
+                   (args->startp - STREAM_DATA (BGP_INPUT (peer)))
+                    + args->total);
+  
+  switch (args->type) {
+    /* where an attribute is relatively inconsequential, e.g. it does not
+     * affect route selection, and can be safely ignored, then any such
+     * attributes which are malformed should just be ignored and the route
+     * processed as normal.
+     */
+    case BGP_ATTR_AS4_AGGREGATOR:
+    case BGP_ATTR_AGGREGATOR:
+    case BGP_ATTR_ATOMIC_AGGREGATE:
+      return BGP_ATTR_PARSE_PROCEED;
+    
+    /* Core attributes, particularly ones which may influence route
+     * selection, should always cause session resets
+     */
+    case BGP_ATTR_ORIGIN:
+    case BGP_ATTR_AS_PATH:
+    case BGP_ATTR_NEXT_HOP:
+    case BGP_ATTR_MULTI_EXIT_DISC:
+    case BGP_ATTR_LOCAL_PREF:
+    case BGP_ATTR_COMMUNITIES:
+    case BGP_ATTR_ORIGINATOR_ID:
+    case BGP_ATTR_CLUSTER_LIST:
+    case BGP_ATTR_MP_REACH_NLRI:
+    case BGP_ATTR_MP_UNREACH_NLRI:
+    case BGP_ATTR_EXT_COMMUNITIES:
+      bgp_notify_send_with_data (peer, BGP_NOTIFY_UPDATE_ERR, subcode,
+                                 notify_datap, length);
+      return BGP_ATTR_PARSE_ERROR;
+  }
+  
+  /* Partial optional attributes that are malformed should not cause
+   * the whole session to be reset. Instead treat it as a withdrawal
+   * of the routes, if possible.
+   */
+  if (CHECK_FLAG (flags, BGP_ATTR_FLAG_TRANS)
+      && CHECK_FLAG (flags, BGP_ATTR_FLAG_OPTIONAL)
+      && CHECK_FLAG (flags, BGP_ATTR_FLAG_PARTIAL))
+    return BGP_ATTR_PARSE_WITHDRAW;
+  
+  /* default to reset */
+  return BGP_ATTR_PARSE_ERROR;
+}
+
+/* Find out what is wrong with the path attribute flag bits and log the error.
+   "Flag bits" here stand for Optional, Transitive and Partial, but not for
+   Extended Length. Checking O/T/P bits at once implies, that the attribute
+   being diagnosed is defined by RFC as either a "well-known" or an "optional,
+   non-transitive" attribute. */
+static void
+bgp_attr_flags_diagnose (struct bgp_attr_parser_args *args,
+                         u_int8_t desired_flags /* how RFC says it must be */
+)
+{
+  u_char seen = 0, i;
+  u_char real_flags = args->flags;
+  const u_int8_t attr_code = args->type;
+  
+  desired_flags &= ~BGP_ATTR_FLAG_EXTLEN;
+  real_flags &= ~BGP_ATTR_FLAG_EXTLEN;
+  for (i = 0; i <= 2; i++) /* O,T,P, but not E */
+    if
+    (
+      CHECK_FLAG (desired_flags, attr_flag_str[i].key) !=
+      CHECK_FLAG (real_flags,    attr_flag_str[i].key)
+    )
+    {
+      zlog (args->peer->log, LOG_ERR, "%s attribute must%s be flagged as \"%s\"",
+            LOOKUP (attr_str, attr_code),
+            CHECK_FLAG (desired_flags, attr_flag_str[i].key) ? "" : " not",
+            attr_flag_str[i].str);
+      seen = 1;
+    }
+  if (!seen)
+    {
+      zlog (args->peer->log, LOG_DEBUG,
+            "Strange, %s called for attr %s, but no problem found with flags"
+            " (real flags 0x%x, desired 0x%x)",
+            __func__, LOOKUP (attr_str, attr_code),
+            real_flags, desired_flags);
+    }
+}
+
+/* Required flags for attributes. EXTLEN will be masked off when testing,
+ * as will PARTIAL for optional+transitive attributes.
+ */
+const u_int8_t attr_flags_values [] = {
+  [BGP_ATTR_ORIGIN] =           BGP_ATTR_FLAG_TRANS,
+  [BGP_ATTR_AS_PATH] =          BGP_ATTR_FLAG_TRANS,
+  [BGP_ATTR_NEXT_HOP] =         BGP_ATTR_FLAG_TRANS,
+  [BGP_ATTR_MULTI_EXIT_DISC] =  BGP_ATTR_FLAG_OPTIONAL,
+  [BGP_ATTR_LOCAL_PREF] =       BGP_ATTR_FLAG_TRANS,
+  [BGP_ATTR_ATOMIC_AGGREGATE] = BGP_ATTR_FLAG_TRANS,
+  [BGP_ATTR_AGGREGATOR] =       BGP_ATTR_FLAG_TRANS | BGP_ATTR_FLAG_OPTIONAL,
+  [BGP_ATTR_COMMUNITIES] =      BGP_ATTR_FLAG_TRANS | BGP_ATTR_FLAG_OPTIONAL,
+  [BGP_ATTR_ORIGINATOR_ID] =    BGP_ATTR_FLAG_OPTIONAL,
+  [BGP_ATTR_CLUSTER_LIST] =     BGP_ATTR_FLAG_OPTIONAL,
+  [BGP_ATTR_MP_REACH_NLRI] =    BGP_ATTR_FLAG_OPTIONAL,
+  [BGP_ATTR_MP_UNREACH_NLRI] =  BGP_ATTR_FLAG_OPTIONAL,
+  [BGP_ATTR_EXT_COMMUNITIES] =  BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANS,
+  [BGP_ATTR_AS4_PATH] =         BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANS,
+  [BGP_ATTR_AS4_AGGREGATOR] =   BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANS,
+};
+static const size_t attr_flags_values_max =
+  sizeof (attr_flags_values) / sizeof (attr_flags_values[0]);
+
+static int
+bgp_attr_flag_invalid (struct bgp_attr_parser_args *args)
+{
+  u_int8_t mask = BGP_ATTR_FLAG_EXTLEN;
+  const u_int8_t flags = args->flags;
+  const u_int8_t attr_code = args->type;
+  struct peer *const peer = args->peer; 
+  
+  /* there may be attributes we don't know about */
+  if (attr_code > attr_flags_values_max)
+    return 0;
+  if (attr_flags_values[attr_code] == 0)
+    return 0;
+  
+  /* RFC4271, "For well-known attributes, the Transitive bit MUST be set to
+   * 1."
+   */
+  if (!CHECK_FLAG (BGP_ATTR_FLAG_OPTIONAL, flags)
+      && !CHECK_FLAG (BGP_ATTR_FLAG_TRANS, flags))
+    {
+      zlog (peer->log, LOG_ERR,
+            "%s well-known attributes must have transitive flag set (%x)",
+            LOOKUP (attr_str, attr_code), flags);
+      return 1;
+    }
+  
+  /* "For well-known attributes and for optional non-transitive attributes,
+   *  the Partial bit MUST be set to 0." 
+   */
+  if (CHECK_FLAG (flags, BGP_ATTR_FLAG_PARTIAL))
+    {
+      if (!CHECK_FLAG (flags, BGP_ATTR_FLAG_OPTIONAL))
+        {
+          zlog (peer->log, LOG_ERR,
+                "%s well-known attribute "
+                "must NOT have the partial flag set (%x)",
+                 LOOKUP (attr_str, attr_code), flags);
+          return 1;
+        }
+      if (CHECK_FLAG (flags, BGP_ATTR_FLAG_OPTIONAL)
+          && !CHECK_FLAG (flags, BGP_ATTR_FLAG_TRANS))
+        {
+          zlog (peer->log, LOG_ERR,
+                "%s optional + transitive attribute "
+                "must NOT have the partial flag set (%x)",
+                 LOOKUP (attr_str, attr_code), flags);
+          return 1;
+        }
+    }
+  
+  /* Optional transitive attributes may go through speakers that don't
+   * reocgnise them and set the Partial bit.
+   */
+  if (CHECK_FLAG (flags, BGP_ATTR_FLAG_OPTIONAL)
+      && CHECK_FLAG (flags, BGP_ATTR_FLAG_TRANS))
+    SET_FLAG (mask, BGP_ATTR_FLAG_PARTIAL);
+  
+  if ((flags & ~mask)
+      == attr_flags_values[attr_code])
+    return 0;
+  
+  bgp_attr_flags_diagnose (args, attr_flags_values[attr_code]);
+  return 1;
+}
+
+/* Get origin attribute of the update message. */
+static bgp_attr_parse_ret_t
+bgp_attr_origin (struct bgp_attr_parser_args *args)
+{
+  struct peer *const peer = args->peer;
+  struct attr *const attr = args->attr;
+  const bgp_size_t length = args->length;
+  
   /* If any recognized attribute has Attribute Length that conflicts
      with the expected length (based on the attribute type code), then
      the Error Subcode is set to Attribute Length Error.  The Data
@@ -715,10 +924,9 @@
     {
       zlog (peer->log, LOG_ERR, "Origin attribute length is not one %d",
 	    length);
-      bgp_notify_send_with_data (peer, BGP_NOTIFY_UPDATE_ERR, 
-				 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
-				 startp, total);
-      return -1;
+      return bgp_attr_malformed (args,
+                                 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
+                                 args->total);
     }
 
   /* Fetch origin attribute. */
@@ -733,12 +941,9 @@
     {
       zlog (peer->log, LOG_ERR, "Origin attribute value is invalid %d",
 	      attr->origin);
-
-      bgp_notify_send_with_data (peer, 
-				 BGP_NOTIFY_UPDATE_ERR, 
-				 BGP_NOTIFY_UPDATE_INVAL_ORIGIN,
-				 startp, total);
-      return -1;
+      return bgp_attr_malformed (args,
+                                 BGP_NOTIFY_UPDATE_INVAL_ORIGIN,
+                                 args->total);
     }
 
   /* Set oring attribute flag. */
@@ -746,82 +951,40 @@
 
   return 0;
 }
-/* Parse AS path information.  This function is wrapper of aspath_parse.
- *
- * Parses AS_PATH or AS4_PATH.
- *
- * Returns: if valid: address of struct aspath in the hash of known aspaths,
- *                    with reference count incremented.
- *              else: NULL
- *
- * NB: empty AS path (length == 0) is valid.  The returned struct aspath will
- *     have segments == NULL and str == zero length string (unique).
- */
-static struct aspath *
-bgp_attr_aspath (struct peer *peer, bgp_size_t length, 
-		 struct attr *attr, u_char flag, u_char *startp, int as4_path)
+
+/* Parse AS path information.  This function is wrapper of
+   aspath_parse. */
+static int
+bgp_attr_aspath (struct bgp_attr_parser_args *args)
 {
-  u_char require ;
-  struct aspath *asp ;
-
-  /* Check the attribute flags                                          */
-  require = as4_path ? BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANS
-                     :                          BGP_ATTR_FLAG_TRANS ;
-
-  if ((flag & (BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANS)) != require)
-    {
-      const char* path_type ;
-      bgp_size_t total;
-
-      path_type = as4_path ? "AS4_PATH" : "AS_PATH" ;
-
-      if (!CHECK_FLAG(flag, BGP_ATTR_FLAG_TRANS))
-      zlog (peer->log, LOG_ERR, 
-            "%s attribute flag isn't transitive %d", path_type, flag) ;
-
-      if ((flag & BGP_ATTR_FLAG_OPTIONAL) != (require & BGP_ATTR_FLAG_OPTIONAL))
-        zlog (peer->log, LOG_ERR,
-            "%s attribute flag must %sbe optional %d", path_type,
-            (flag & BGP_ATTR_FLAG_OPTIONAL) ? "not " : "", flag) ;
-
-      total = length + (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN) ? 4 : 3);
-
-      bgp_notify_send_with_data (peer, 
-				 BGP_NOTIFY_UPDATE_ERR, 
-				 BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR,
-				 startp, total);
-
-      return NULL ;
-    } ;
-
-  /* Parse the AS_PATH/AS4_PATH body.
-   *
-   * For AS_PATH  peer with AS4 => 4Byte ASN otherwise 2Byte ASN
-   *     AS4_PATH 4Byte ASN
+  struct attr *const attr = args->attr;
+  struct peer *const peer = args->peer; 
+  const bgp_size_t length = args->length;
+  
+  /*
+   * peer with AS4 => will get 4Byte ASnums
+   * otherwise, will get 16 Bit
    */
-  asp = aspath_parse (peer->ibuf, length,
-               as4_path || CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV), as4_path) ;
+  attr->aspath = aspath_parse (peer->ibuf, length, 
+                               CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV));
 
-  if (asp != NULL)
+  /* In case of IBGP, length will be zero. */
+  if (! attr->aspath)
     {
-      attr->flag |= ATTR_FLAG_BIT (as4_path ? BGP_ATTR_AS4_PATH
-                                            : BGP_ATTR_AS_PATH) ;
+      zlog (peer->log, LOG_ERR,
+            "Malformed AS path from %s, length is %d",
+            peer->host, length);
+      return bgp_attr_malformed (args, BGP_NOTIFY_UPDATE_MAL_AS_PATH, 0);
     }
-  else
-    {
-      zlog (peer->log, LOG_ERR, "Malformed AS path length is %d", length);
 
-      /* TODO: should BGP_NOTIFY_UPDATE_MAL_AS_PATH be sent for AS4_PATH ??  */
-      bgp_notify_send (peer, 
-		       BGP_NOTIFY_UPDATE_ERR, 
-		       BGP_NOTIFY_UPDATE_MAL_AS_PATH);
-    } ;
+  /* Set aspath attribute flag. */
+  attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS_PATH);
 
-  return asp ;
+  return BGP_ATTR_PARSE_PROCEED;
 }
 
-static int bgp_attr_aspath_check( struct peer *peer, 
-		struct attr *attr)
+static bgp_attr_parse_ret_t
+bgp_attr_aspath_check (struct peer *const peer, struct attr *const attr)
 {
   /* These checks were part of bgp_attr_aspath, but with
    * as4 we should to check aspath things when
@@ -840,10 +1003,9 @@
      (peer_sort (peer) == BGP_PEER_EBGP && aspath_confed_check (attr->aspath)))
     {
       zlog (peer->log, LOG_ERR, "Malformed AS path from %s", peer->host);
-      bgp_notify_send (peer, 
-		       BGP_NOTIFY_UPDATE_ERR, 
-		       BGP_NOTIFY_UPDATE_MAL_AS_PATH);
-      return -1;
+      bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
+                       BGP_NOTIFY_UPDATE_MAL_AS_PATH);
+      return BGP_ATTR_PARSE_ERROR;
     }
 
   /* First AS check for EBGP. */
@@ -854,10 +1016,9 @@
  	{
  	  zlog (peer->log, LOG_ERR,
  		"%s incorrect first AS (must be %u)", peer->host, peer->as);
- 	  bgp_notify_send (peer,
- 			   BGP_NOTIFY_UPDATE_ERR,
- 			   BGP_NOTIFY_UPDATE_MAL_AS_PATH);
-	  return -1;
+          bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
+                           BGP_NOTIFY_UPDATE_MAL_AS_PATH);
+          return BGP_ATTR_PARSE_ERROR;
  	}
     }
 
@@ -867,35 +1028,51 @@
     {
       aspath = aspath_dup (attr->aspath);
       aspath = aspath_add_seq (aspath, peer->change_local_as);
-      aspath_unintern (attr->aspath);
+      aspath_unintern (&attr->aspath);
       attr->aspath = aspath_intern (aspath);
     }
 
-  return 0;
+  return BGP_ATTR_PARSE_PROCEED;
+}
 
+/* Parse AS4 path information.  This function is another wrapper of
+   aspath_parse. */
+static int
+bgp_attr_as4_path (struct bgp_attr_parser_args *args, struct aspath **as4_path)
+{
+  struct peer *const peer = args->peer; 
+  struct attr *const attr = args->attr;
+  const bgp_size_t length = args->length;
+  
+  *as4_path = aspath_parse (peer->ibuf, length, 1);
+
+  /* In case of IBGP, length will be zero. */
+  if (!*as4_path)
+    {
+      zlog (peer->log, LOG_ERR,
+            "Malformed AS4 path from %s, length is %d",
+            peer->host, length);
+      return bgp_attr_malformed (args,
+                                 BGP_NOTIFY_UPDATE_MAL_AS_PATH,
+                                 0);
+    }
+
+  /* Set aspath attribute flag. */
+  if (as4_path)
+    attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS4_PATH);
+
+  return BGP_ATTR_PARSE_PROCEED;
 }
 
 /* Nexthop attribute. */
-static int
-bgp_attr_nexthop (struct peer *peer, bgp_size_t length, 
-		  struct attr *attr, u_char flag, u_char *startp)
+static bgp_attr_parse_ret_t
+bgp_attr_nexthop (struct bgp_attr_parser_args *args)
 {
-  bgp_size_t total;
-
-  total = length + (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN) ? 4 : 3);
-
-  /* Flag check. */
-  if (CHECK_FLAG (flag, BGP_ATTR_FLAG_OPTIONAL)
-      || ! CHECK_FLAG (flag, BGP_ATTR_FLAG_TRANS))
-    {
-      zlog (peer->log, LOG_ERR, 
-	    "Origin attribute flag isn't transitive %d", flag);
-      bgp_notify_send_with_data (peer, 
-				 BGP_NOTIFY_UPDATE_ERR, 
-				 BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR,
-				 startp, total);
-      return -1;
-    }
+  struct peer *const peer = args->peer; 
+  struct attr *const attr = args->attr;
+  const bgp_size_t length = args->length;
+  
+  in_addr_t nexthop_h, nexthop_n;
 
   /* Check nexthop attribute length. */
   if (length != 4)
@@ -903,114 +1080,141 @@
       zlog (peer->log, LOG_ERR, "Nexthop attribute length isn't four [%d]",
 	      length);
 
-      bgp_notify_send_with_data (peer, 
-				 BGP_NOTIFY_UPDATE_ERR, 
-				 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
-				 startp, total);
-      return -1;
+      return bgp_attr_malformed (args,
+                                 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
+                                 args->total);
     }
 
-  attr->nexthop.s_addr = stream_get_ipv4 (peer->ibuf);
+  /* According to section 6.3 of RFC4271, syntactically incorrect NEXT_HOP
+     attribute must result in a NOTIFICATION message (this is implemented below).
+     At the same time, semantically incorrect NEXT_HOP is more likely to be just
+     logged locally (this is implemented somewhere else). The UPDATE message
+     gets ignored in any of these cases. */
+  nexthop_n = stream_get_ipv4 (peer->ibuf);
+  nexthop_h = ntohl (nexthop_n);
+  if (IPV4_NET0 (nexthop_h) || IPV4_NET127 (nexthop_h) || IPV4_CLASS_DE (nexthop_h))
+    {
+      char buf[INET_ADDRSTRLEN];
+      inet_ntop (AF_INET, &nexthop_h, buf, INET_ADDRSTRLEN);
+      zlog (peer->log, LOG_ERR, "Martian nexthop %s", buf);
+      return bgp_attr_malformed (args,
+                                 BGP_NOTIFY_UPDATE_INVAL_NEXT_HOP,
+                                 args->total);
+    }
+
+  attr->nexthop.s_addr = nexthop_n;
   attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP);
 
-  return 0;
+  return BGP_ATTR_PARSE_PROCEED;
 }
 
 /* MED atrribute. */
-static int
-bgp_attr_med (struct peer *peer, bgp_size_t length, 
-	      struct attr *attr, u_char flag, u_char *startp)
+static bgp_attr_parse_ret_t
+bgp_attr_med (struct bgp_attr_parser_args *args)
 {
-  bgp_size_t total;
-
-  total = length + (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN) ? 4 : 3);
-
+  struct peer *const peer = args->peer; 
+  struct attr *const attr = args->attr;
+  const bgp_size_t length = args->length;
+  
   /* Length check. */
   if (length != 4)
     {
       zlog (peer->log, LOG_ERR, 
 	    "MED attribute length isn't four [%d]", length);
-      
-      bgp_notify_send_with_data (peer, 
-				 BGP_NOTIFY_UPDATE_ERR, 
-				 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
-				 startp, total);
-      return -1;
+
+      return bgp_attr_malformed (args,
+                                 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
+                                 args->total);
     }
 
   attr->med = stream_getl (peer->ibuf);
 
   attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC);
 
-  return 0;
+  return BGP_ATTR_PARSE_PROCEED;
 }
 
 /* Local preference attribute. */
-static int
-bgp_attr_local_pref (struct peer *peer, bgp_size_t length, 
-		     struct attr *attr, u_char flag)
+static bgp_attr_parse_ret_t
+bgp_attr_local_pref (struct bgp_attr_parser_args *args)
 {
+  struct peer *const peer = args->peer; 
+  struct attr *const attr = args->attr;
+  const bgp_size_t length = args->length;
+  
+  /* Length check. */
+  if (length != 4)
+  {
+    zlog (peer->log, LOG_ERR, "LOCAL_PREF attribute length isn't 4 [%u]",
+          length);
+    return bgp_attr_malformed (args,
+                               BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
+                               args->total);
+  }
+
   /* If it is contained in an UPDATE message that is received from an
      external peer, then this attribute MUST be ignored by the
      receiving speaker. */
   if (peer_sort (peer) == BGP_PEER_EBGP)
     {
       stream_forward_getp (peer->ibuf, length);
-      return 0;
+      return BGP_ATTR_PARSE_PROCEED;
     }
 
-  if (length == 4) 
-    attr->local_pref = stream_getl (peer->ibuf);
-  else 
-    attr->local_pref = 0;
+  attr->local_pref = stream_getl (peer->ibuf);
 
   /* Set atomic aggregate flag. */
   attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF);
 
-  return 0;
+  return BGP_ATTR_PARSE_PROCEED;
 }
 
 /* Atomic aggregate. */
 static int
-bgp_attr_atomic (struct peer *peer, bgp_size_t length, 
-		 struct attr *attr, u_char flag)
+bgp_attr_atomic (struct bgp_attr_parser_args *args)
 {
+  struct peer *const peer = args->peer; 
+  struct attr *const attr = args->attr;
+  const bgp_size_t length = args->length;
+  
+  /* Length check. */
   if (length != 0)
     {
-      zlog (peer->log, LOG_ERR, "Bad atomic aggregate length %d", length);
-
-      bgp_notify_send (peer, 
-		       BGP_NOTIFY_UPDATE_ERR, 
-		       BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
-      return -1;
+      zlog (peer->log, LOG_ERR, "ATOMIC_AGGREGATE attribute length isn't 0 [%u]",
+            length);
+      return bgp_attr_malformed (args,
+                                 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
+                                 args->total);
     }
 
   /* Set atomic aggregate flag. */
   attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE);
 
-  return 0;
+  return BGP_ATTR_PARSE_PROCEED;
 }
 
 /* Aggregator attribute */
 static int
-bgp_attr_aggregator (struct peer *peer, bgp_size_t length,
-		     struct attr *attr, u_char flag)
+bgp_attr_aggregator (struct bgp_attr_parser_args *args)
 {
+  struct peer *const peer = args->peer; 
+  struct attr *const attr = args->attr;
+  const bgp_size_t length = args->length;
+  
   int wantedlen = 6;
   struct attr_extra *attre = bgp_attr_extra_get (attr);
   
   /* peer with AS4 will send 4 Byte AS, peer without will send 2 Byte */
-  if ( CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV ) )
+  if (CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV))
     wantedlen = 8;
   
   if (length != wantedlen)
     {
-      zlog (peer->log, LOG_ERR, "Aggregator length is not %d [%d]", wantedlen, length);
-
-      bgp_notify_send (peer,
-		       BGP_NOTIFY_UPDATE_ERR,
-		       BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
-      return -1;
+      zlog (peer->log, LOG_ERR, "AGGREGATOR attribute length isn't %u [%u]",
+            wantedlen, length);
+      return bgp_attr_malformed (args,
+                                 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
+                                 args->total);
     }
   
   if ( CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV ) )
@@ -1022,36 +1226,41 @@
   /* Set atomic aggregate flag. */
   attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR);
 
-  return 0;
+  return BGP_ATTR_PARSE_PROCEED;
 }
 
 /* New Aggregator attribute */
-static int
-bgp_attr_as4_aggregator (struct peer *peer, bgp_size_t length,
-		     struct attr *attr, as_t *as4_aggregator_as,
-		     struct in_addr *as4_aggregator_addr)
+static bgp_attr_parse_ret_t
+bgp_attr_as4_aggregator (struct bgp_attr_parser_args *args,
+		         as_t *as4_aggregator_as,
+		         struct in_addr *as4_aggregator_addr)
 {
+  struct peer *const peer = args->peer; 
+  struct attr *const attr = args->attr;
+  const bgp_size_t length = args->length;
+      
   if (length != 8)
     {
-      zlog (peer->log, LOG_ERR, "New Aggregator length is not 8 [%d]", length);
-
-      bgp_notify_send (peer,
-		       BGP_NOTIFY_UPDATE_ERR,
-		       BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
-      return -1;
+      zlog (peer->log, LOG_ERR, "New Aggregator length is not 8 [%d]",
+            length);
+      return bgp_attr_malformed (args,
+                                 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
+                                 0);
     }
+  
   *as4_aggregator_as = stream_getl (peer->ibuf);
   as4_aggregator_addr->s_addr = stream_get_ipv4 (peer->ibuf);
 
   attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS4_AGGREGATOR);
 
-  return 0;
+  return BGP_ATTR_PARSE_PROCEED;
 }
 
 /* Munge Aggregator and New-Aggregator, AS_PATH and NEW_AS_PATH.
  */
-static int
-bgp_attr_munge_as4_attrs (struct peer *peer, struct attr *attr,
+static bgp_attr_parse_ret_t
+bgp_attr_munge_as4_attrs (struct peer *const peer,
+                          struct attr *const attr,
                           struct aspath *as4_path, as_t as4_aggregator,
                           struct in_addr *as4_aggregator_addr)
 {
@@ -1059,7 +1268,7 @@
   struct aspath *newpath;
   struct attr_extra *attre = attr->extra;
     
-  if ( CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV) )
+  if (CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV))
     {
       /* peer can do AS4, so we ignore AS4_PATH and AS4_AGGREGATOR
        * if given.
@@ -1077,34 +1286,15 @@
                         peer->host, "AS4 capable peer, yet it sent");
         }
       
-      return 0;
+      return BGP_ATTR_PARSE_PROCEED;
     }
   
-  if (attr->flag & ( ATTR_FLAG_BIT( BGP_ATTR_AS4_PATH))
-      && !(attr->flag & ( ATTR_FLAG_BIT( BGP_ATTR_AS_PATH))))
-    {
-      /* Hu? This is not supposed to happen at all!
-       * got as4_path and no aspath,
-       *   This should already
-       *   have been handled by 'well known attributes missing'
-       *   But... yeah, paranoia
-       * Take this as a "malformed attribute"
-       */
-      zlog (peer->log, LOG_ERR, 
-            "%s BGP not AS4 capable peer sent AS4_PATH but"
-            " no AS_PATH, cant do anything here", peer->host);
-      bgp_notify_send (peer, 
-                       BGP_NOTIFY_UPDATE_ERR, 
-                       BGP_NOTIFY_UPDATE_MAL_ATTR);
-      return -1;
-    }
-
   /* We have a asn16 peer.  First, look for AS4_AGGREGATOR
    * because that may override AS4_PATH
    */
   if (attr->flag & (ATTR_FLAG_BIT (BGP_ATTR_AS4_AGGREGATOR) ) )
     {
-      if ( attr->flag & (ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR) ) )
+      if (attr->flag & (ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR) ) )
         {
           assert (attre);
           
@@ -1120,7 +1310,7 @@
            *        Aggregating node and the AS_PATH is to be
            *        constructed "as in all other cases"
            */
-          if ( attre->aggregator_as != BGP_AS_TRANS )
+          if (attre->aggregator_as != BGP_AS_TRANS)
             {
               /* ignore */
               if ( BGP_DEBUG(as4, AS4))
@@ -1155,24 +1345,27 @@
     }
 
   /* need to reconcile NEW_AS_PATH and AS_PATH */
-  if ( !ignore_as4_path && (attr->flag & ( ATTR_FLAG_BIT( BGP_ATTR_AS4_PATH))) )
+  if (!ignore_as4_path && (attr->flag & (ATTR_FLAG_BIT( BGP_ATTR_AS4_PATH))))
     {
        newpath = aspath_reconcile_as4 (attr->aspath, as4_path);
-       aspath_unintern (attr->aspath);
+       aspath_unintern (&attr->aspath);
        attr->aspath = aspath_intern (newpath);
     }
-  return 0;
+  return BGP_ATTR_PARSE_PROCEED;
 }
 
 /* Community attribute. */
-static int
-bgp_attr_community (struct peer *peer, bgp_size_t length, 
-		    struct attr *attr, u_char flag)
+static bgp_attr_parse_ret_t
+bgp_attr_community (struct bgp_attr_parser_args *args)
 {
+  struct peer *const peer = args->peer; 
+  struct attr *const attr = args->attr;  
+  const bgp_size_t length = args->length;
+  
   if (length == 0)
     {
       attr->community = NULL;
-      return 0;
+      return BGP_ATTR_PARSE_PROCEED;
     }
   
   attr->community =
@@ -1182,26 +1375,31 @@
   stream_forward_getp (peer->ibuf, length);
 
   if (!attr->community)
-    return -1;
+    return bgp_attr_malformed (args,
+                               BGP_NOTIFY_UPDATE_OPT_ATTR_ERR,
+                               args->total);
   
   attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES);
 
-  return 0;
+  return BGP_ATTR_PARSE_PROCEED;
 }
 
 /* Originator ID attribute. */
-static int
-bgp_attr_originator_id (struct peer *peer, bgp_size_t length, 
-			struct attr *attr, u_char flag)
+static bgp_attr_parse_ret_t
+bgp_attr_originator_id (struct bgp_attr_parser_args *args)
 {
+  struct peer *const peer = args->peer; 
+  struct attr *const attr = args->attr;
+  const bgp_size_t length = args->length;
+  
+  /* Length check. */
   if (length != 4)
     {
       zlog (peer->log, LOG_ERR, "Bad originator ID length %d", length);
 
-      bgp_notify_send (peer, 
-		       BGP_NOTIFY_UPDATE_ERR, 
-		       BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
-      return -1;
+      return bgp_attr_malformed (args,
+                                 BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
+                                 args->total);
     }
 
   (bgp_attr_extra_get (attr))->originator_id.s_addr 
@@ -1209,39 +1407,41 @@
 
   attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGINATOR_ID);
 
-  return 0;
+  return BGP_ATTR_PARSE_PROCEED;
 }
 
 /* Cluster list attribute. */
-static int
-bgp_attr_cluster_list (struct peer *peer, bgp_size_t length, 
-		       struct attr *attr, u_char flag)
+static bgp_attr_parse_ret_t
+bgp_attr_cluster_list (struct bgp_attr_parser_args *args)
 {
+  struct peer *const peer = args->peer; 
+  struct attr *const attr = args->attr;
+  const bgp_size_t length = args->length;
+  
   /* Check length. */
   if (length % 4)
     {
       zlog (peer->log, LOG_ERR, "Bad cluster list length %d", length);
 
-      bgp_notify_send (peer, 
-		       BGP_NOTIFY_UPDATE_ERR, 
-		       BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
-      return -1;
+      return bgp_attr_malformed (args, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
+                                 args->total);
     }
 
   (bgp_attr_extra_get (attr))->cluster 
     = cluster_parse ((struct in_addr *)stream_pnt (peer->ibuf), length);
-
-  stream_forward_getp (peer->ibuf, length);;
+  
+  /* XXX: Fix cluster_parse to use stream API and then remove this */
+  stream_forward_getp (peer->ibuf, length);
 
   attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_CLUSTER_LIST);
 
-  return 0;
+  return BGP_ATTR_PARSE_PROCEED;
 }
 
 /* Multiprotocol reachability information parse. */
 int
-bgp_mp_reach_parse (struct peer *peer, bgp_size_t length, struct attr *attr,
-		    struct bgp_nlri *mp_update)
+bgp_mp_reach_parse (struct bgp_attr_parser_args *args,
+                    struct bgp_nlri *mp_update)
 {
   afi_t afi;
   safi_t safi;
@@ -1249,6 +1449,9 @@
   size_t start;
   int ret;
   struct stream *s;
+  struct peer *const peer = args->peer;  
+  struct attr *const attr = args->attr;
+  const bgp_size_t length = args->length;
   struct attr_extra *attre = bgp_attr_extra_get(attr);
   
   /* Set end of packet. */
@@ -1262,7 +1465,7 @@
     {
       zlog_info ("%s: %s sent invalid length, %lu", 
 		 __func__, peer->host, (unsigned long)length);
-      return -1;
+      return BGP_ATTR_PARSE_ERROR;
     }
   
   /* Load AFI, SAFI. */
@@ -1276,7 +1479,7 @@
     {
       zlog_info ("%s: %s, MP nexthop length, %u, goes past end of attribute", 
 		 __func__, peer->host, attre->mp_nexthop_len);
-      return -1;
+      return BGP_ATTR_PARSE_ERROR;
     }
   
   /* Nexthop length check. */
@@ -1289,14 +1492,9 @@
         memcpy(&attr->nexthop.s_addr, &attre->mp_nexthop_global_in, 4);
       break;
     case 12:
-      {
-	u_int32_t rd_high;
-	u_int32_t rd_low;
-
-	rd_high = stream_getl (s);
-	rd_low = stream_getl (s);
-	stream_get (&attre->mp_nexthop_global_in, s, 4);
-      }
+      stream_getl (s); /* RD high */
+      stream_getl (s); /* RD low */
+      stream_get (&attre->mp_nexthop_global_in, s, 4);
       break;
 #ifdef HAVE_IPV6
     case 16:
@@ -1324,14 +1522,14 @@
     default:
       zlog_info ("%s: (%s) Wrong multiprotocol next hop length: %d", 
 		 __func__, peer->host, attre->mp_nexthop_len);
-      return -1;
+      return BGP_ATTR_PARSE_ERROR;
     }
 
   if (!LEN_LEFT)
     {
       zlog_info ("%s: (%s) Failed to read SNPA and NLRI(s)",
                  __func__, peer->host);
-      return -1;
+      return BGP_ATTR_PARSE_ERROR;
     }
   
   {
@@ -1347,17 +1545,17 @@
     {
       zlog_info ("%s: (%s) Failed to read NLRI",
                  __func__, peer->host);
-      return -1;
+      return BGP_ATTR_PARSE_ERROR;
     }
  
-  if (safi != BGP_SAFI_VPNV4)
+  if (safi != SAFI_MPLS_LABELED_VPN)
     {
       ret = bgp_nlri_sanity_check (peer, afi, stream_pnt (s), nlri_len);
       if (ret < 0) 
         {
           zlog_info ("%s: (%s) NLRI doesn't pass sanity check",
                      __func__, peer->host);
-	  return -1;
+	  return BGP_ATTR_PARSE_ERROR;
 	}
     }
 
@@ -1368,13 +1566,13 @@
 
   stream_forward_getp (s, nlri_len);
 
-  return 0;
+  return BGP_ATTR_PARSE_PROCEED;
 #undef LEN_LEFT
 }
 
 /* Multiprotocol unreachable parse */
 int
-bgp_mp_unreach_parse (struct peer *peer, bgp_size_t length, 
+bgp_mp_unreach_parse (struct bgp_attr_parser_args *args,
 		      struct bgp_nlri *mp_withdraw)
 {
   struct stream *s;
@@ -1382,23 +1580,25 @@
   safi_t safi;
   u_int16_t withdraw_len;
   int ret;
+  struct peer *const peer = args->peer;  
+  const bgp_size_t length = args->length;
 
   s = peer->ibuf;
   
 #define BGP_MP_UNREACH_MIN_SIZE 3
   if ((length > STREAM_READABLE(s)) || (length <  BGP_MP_UNREACH_MIN_SIZE))
-    return -1;
+    return BGP_ATTR_PARSE_ERROR;
   
   afi = stream_getw (s);
   safi = stream_getc (s);
   
   withdraw_len = length - BGP_MP_UNREACH_MIN_SIZE;
 
-  if (safi != BGP_SAFI_VPNV4)
+  if (safi != SAFI_MPLS_LABELED_VPN)
     {
       ret = bgp_nlri_sanity_check (peer, afi, stream_pnt (s), withdraw_len);
       if (ret < 0)
-	return -1;
+	return BGP_ATTR_PARSE_ERROR;
     }
 
   mp_withdraw->afi = afi;
@@ -1408,20 +1608,23 @@
 
   stream_forward_getp (s, withdraw_len);
 
-  return 0;
+  return BGP_ATTR_PARSE_PROCEED;
 }
 
 /* Extended Community attribute. */
-static int
-bgp_attr_ext_communities (struct peer *peer, bgp_size_t length, 
-			  struct attr *attr, u_char flag)
+static bgp_attr_parse_ret_t
+bgp_attr_ext_communities (struct bgp_attr_parser_args *args)
 {
+  struct peer *const peer = args->peer;  
+  struct attr *const attr = args->attr;  
+  const bgp_size_t length = args->length;
+  
   if (length == 0)
     {
       if (attr->extra)
         attr->extra->ecommunity = NULL;
       /* Empty extcomm doesn't seem to be invalid per se */
-      return 0;
+      return BGP_ATTR_PARSE_PROCEED;
     }
 
   (bgp_attr_extra_get (attr))->ecommunity =
@@ -1430,21 +1633,29 @@
   stream_forward_getp (peer->ibuf, length);
   
   if (!attr->extra->ecommunity)
-    return -1;
+    return bgp_attr_malformed (args,
+                               BGP_NOTIFY_UPDATE_OPT_ATTR_ERR,
+                               args->total);
   
   attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_EXT_COMMUNITIES);
 
-  return 0;
+  return BGP_ATTR_PARSE_PROCEED;
 }
 
 /* BGP unknown attribute treatment. */
-static int
-bgp_attr_unknown (struct peer *peer, struct attr *attr, u_char flag,
-		  u_char type, bgp_size_t length, u_char *startp)
+static bgp_attr_parse_ret_t
+bgp_attr_unknown (struct bgp_attr_parser_args *args)
 {
   bgp_size_t total;
   struct transit *transit;
   struct attr_extra *attre;
+  struct peer *const peer = args->peer; 
+  struct attr *const attr = args->attr;
+  u_char *const startp = args->startp;
+  const u_char type = args->type;
+  const u_char flag = args->flags;  
+  const bgp_size_t length = args->length;
+  
 
   if (BGP_DEBUG (normal, NORMAL))
   zlog_debug ("%s Unknown attribute is received (type %d, length %d)",
@@ -1457,27 +1668,21 @@
   /* Forward read pointer of input stream. */
   stream_forward_getp (peer->ibuf, length);
 
-  /* Adjest total length to include type and length. */
-  total = length + (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN) ? 4 : 3);
-
   /* If any of the mandatory well-known attributes are not recognized,
      then the Error Subcode is set to Unrecognized Well-known
      Attribute.  The Data field contains the unrecognized attribute
      (type, length and value). */
-  if (! CHECK_FLAG (flag, BGP_ATTR_FLAG_OPTIONAL))
+  if (!CHECK_FLAG (flag, BGP_ATTR_FLAG_OPTIONAL))
     {
-      /* Adjust startp to do not include flag value. */
-      bgp_notify_send_with_data (peer, 
-				 BGP_NOTIFY_UPDATE_ERR, 
-				 BGP_NOTIFY_UPDATE_UNREC_ATTR,
-				 startp, total);
-      return -1;
+      return bgp_attr_malformed (args,
+                                 BGP_NOTIFY_UPDATE_UNREC_ATTR,
+                                 args->total);
     }
 
   /* Unrecognized non-transitive optional attributes must be quietly
      ignored and not passed along to other BGP peers. */
   if (! CHECK_FLAG (flag, BGP_ATTR_FLAG_TRANS))
-    return 0;
+    return BGP_ATTR_PARSE_PROCEED;
 
   /* If a path with recognized transitive optional attribute is
      accepted and passed along to other BGP peers and the Partial bit
@@ -1500,17 +1705,17 @@
   memcpy (transit->val + transit->length, startp, total);
   transit->length += total;
 
-  return 0;
+  return BGP_ATTR_PARSE_PROCEED;
 }
 
 /* Read attribute of update packet.  This function is called from
    bgp_update() in bgpd.c.  */
-int
+bgp_attr_parse_ret_t
 bgp_attr_parse (struct peer *peer, struct attr *attr, bgp_size_t size,
 		struct bgp_nlri *mp_update, struct bgp_nlri *mp_withdraw)
 {
   int ret;
-  u_char flag;
+  u_char flag = 0;
   u_char type = 0;
   bgp_size_t length;
   u_char *startp, *endp;
@@ -1527,7 +1732,7 @@
 
   /* End pointer of BGP attribute. */
   endp = BGP_INPUT_PNT (peer) + size;
-
+  
   /* Get attributes to the end of attribute length. */
   while (BGP_INPUT_PNT (peer) < endp)
     {
@@ -1536,19 +1741,22 @@
 	{
 	  /* XXX warning: long int format, int arg (arg 5) */
 	  zlog (peer->log, LOG_WARNING, 
-		"%s error BGP attribute length %lu is smaller than min len",
+		"%s: error BGP attribute length %lu is smaller than min len",
 		peer->host,
 		(unsigned long) (endp - STREAM_PNT (BGP_INPUT (peer))));
 
 	  bgp_notify_send (peer, 
 			   BGP_NOTIFY_UPDATE_ERR, 
 			   BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
-	  return -1;
+	  return BGP_ATTR_PARSE_ERROR;
 	}
 
       /* Fetch attribute flag and type. */
       startp = BGP_INPUT_PNT (peer);
-      flag = stream_getc (BGP_INPUT (peer));
+      /* "The lower-order four bits of the Attribute Flags octet are
+         unused.  They MUST be zero when sent and MUST be ignored when
+         received." */
+      flag = 0xF0 & stream_getc (BGP_INPUT (peer));
       type = stream_getc (BGP_INPUT (peer));
 
       /* Check whether Extended-Length applies and is in bounds */
@@ -1556,16 +1764,16 @@
           && ((endp - startp) < (BGP_ATTR_MIN_LEN + 1)))
 	{
 	  zlog (peer->log, LOG_WARNING, 
-		"%s Extended length set, but just %lu bytes of attr header",
+		"%s: Extended length set, but just %lu bytes of attr header",
 		peer->host,
 		(unsigned long) (endp - STREAM_PNT (BGP_INPUT (peer))));
 
 	  bgp_notify_send (peer, 
 			   BGP_NOTIFY_UPDATE_ERR, 
 			   BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
-	  return -1;
+	  return BGP_ATTR_PARSE_ERROR;
 	}
-
+      
       /* Check extended attribue length bit. */
       if (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN))
 	length = stream_getw (BGP_INPUT (peer));
@@ -1579,13 +1787,13 @@
       if (CHECK_BITMAP (seen, type))
 	{
 	  zlog (peer->log, LOG_WARNING,
-		"%s error BGP attribute type %d appears twice in a message",
+		"%s: error BGP attribute type %d appears twice in a message",
 		peer->host, type);
 
 	  bgp_notify_send (peer, 
 			   BGP_NOTIFY_UPDATE_ERR, 
 			   BGP_NOTIFY_UPDATE_MAL_ATTR);
-	  return -1;
+	  return BGP_ATTR_PARSE_ERROR;
 	}
 
       /* Set type to bitmap to check duplicate attribute.  `type' is
@@ -1599,81 +1807,120 @@
       if (attr_endp > endp)
 	{
 	  zlog (peer->log, LOG_WARNING, 
-		"%s BGP type %d length %d is too large, attribute total length is %d.  attr_endp is %p.  endp is %p", peer->host, type, length, size, attr_endp, endp);
+		"%s: BGP type %d length %d is too large, attribute total length is %d.  attr_endp is %p.  endp is %p", peer->host, type, length, size, attr_endp, endp);
 	  bgp_notify_send (peer, 
 			   BGP_NOTIFY_UPDATE_ERR, 
 			   BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
-	  return -1;
+	  return BGP_ATTR_PARSE_ERROR;
 	}
+	
+        struct bgp_attr_parser_args attr_args = {
+          .peer = peer,
+          .length = length,
+          .attr = attr,
+          .type = type,
+          .flags = flag,
+          .startp = startp,
+          .total = attr_endp - startp,
+        };
+      
+	
+      /* If any recognized attribute has Attribute Flags that conflict
+         with the Attribute Type Code, then the Error Subcode is set to
+         Attribute Flags Error.  The Data field contains the erroneous
+         attribute (type, length and value). */
+      if (bgp_attr_flag_invalid (&attr_args))
+        {
+          bgp_attr_parse_ret_t ret;
+          ret = bgp_attr_malformed (&attr_args,
+                                    BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR,
+                                    attr_args.total);
+          if (ret == BGP_ATTR_PARSE_PROCEED)
+            continue;
+          return ret;
+        }
 
       /* OK check attribute and store it's value. */
       switch (type)
 	{
 	case BGP_ATTR_ORIGIN:
-	  ret = bgp_attr_origin (peer, length, attr, flag, startp);
+	  ret = bgp_attr_origin (&attr_args);
 	  break;
 	case BGP_ATTR_AS_PATH:
-          attr->aspath = bgp_attr_aspath (peer, length, attr, flag, startp, 0);
-          ret = attr->aspath ? 0 : -1 ;
+	  ret = bgp_attr_aspath (&attr_args);
 	  break;
 	case BGP_ATTR_AS4_PATH:
-          as4_path = bgp_attr_aspath (peer, length, attr, flag, startp, 1);
-          ret = as4_path  ? 0 : -1 ;
+	  ret = bgp_attr_as4_path (&attr_args, &as4_path);
 	  break;
 	case BGP_ATTR_NEXT_HOP:	
-	  ret = bgp_attr_nexthop (peer, length, attr, flag, startp);
+	  ret = bgp_attr_nexthop (&attr_args);
 	  break;
 	case BGP_ATTR_MULTI_EXIT_DISC:
-	  ret = bgp_attr_med (peer, length, attr, flag, startp);
+	  ret = bgp_attr_med (&attr_args);
 	  break;
 	case BGP_ATTR_LOCAL_PREF:
-	  ret = bgp_attr_local_pref (peer, length, attr, flag);
+	  ret = bgp_attr_local_pref (&attr_args);
 	  break;
 	case BGP_ATTR_ATOMIC_AGGREGATE:
-	  ret = bgp_attr_atomic (peer, length, attr, flag);
+	  ret = bgp_attr_atomic (&attr_args);
 	  break;
 	case BGP_ATTR_AGGREGATOR:
-	  ret = bgp_attr_aggregator (peer, length, attr, flag);
+	  ret = bgp_attr_aggregator (&attr_args);
 	  break;
 	case BGP_ATTR_AS4_AGGREGATOR:
-	  ret = bgp_attr_as4_aggregator (peer, length, attr, &as4_aggregator, &as4_aggregator_addr);
+	  ret = bgp_attr_as4_aggregator (&attr_args,
+	                                 &as4_aggregator,
+	                                 &as4_aggregator_addr);
 	  break;
 	case BGP_ATTR_COMMUNITIES:
-	  ret = bgp_attr_community (peer, length, attr, flag);
+	  ret = bgp_attr_community (&attr_args);
 	  break;
 	case BGP_ATTR_ORIGINATOR_ID:
-	  ret = bgp_attr_originator_id (peer, length, attr, flag);
+	  ret = bgp_attr_originator_id (&attr_args);
 	  break;
 	case BGP_ATTR_CLUSTER_LIST:
-	  ret = bgp_attr_cluster_list (peer, length, attr, flag);
+	  ret = bgp_attr_cluster_list (&attr_args);
 	  break;
 	case BGP_ATTR_MP_REACH_NLRI:
-	  ret = bgp_mp_reach_parse (peer, length, attr, mp_update);
+	  ret = bgp_mp_reach_parse (&attr_args, mp_update);
 	  break;
 	case BGP_ATTR_MP_UNREACH_NLRI:
-	  ret = bgp_mp_unreach_parse (peer, length, mp_withdraw);
+	  ret = bgp_mp_unreach_parse (&attr_args, mp_withdraw);
 	  break;
 	case BGP_ATTR_EXT_COMMUNITIES:
-	  ret = bgp_attr_ext_communities (peer, length, attr, flag);
+	  ret = bgp_attr_ext_communities (&attr_args);
 	  break;
 	default:
-	  ret = bgp_attr_unknown (peer, attr, flag, type, length, startp);
+	  ret = bgp_attr_unknown (&attr_args);
 	  break;
 	}
-
-      /* If error occured immediately return to the caller. */
-      if (ret < 0)
+      
+      /* If hard error occured immediately return to the caller. */
+      if (ret == BGP_ATTR_PARSE_ERROR)
         {
           zlog (peer->log, LOG_WARNING,
                 "%s: Attribute %s, parse error", 
                 peer->host, 
                 LOOKUP (attr_str, type));
-           bgp_notify_send (peer, 
-                            BGP_NOTIFY_UPDATE_ERR,
-                            BGP_NOTIFY_UPDATE_MAL_ATTR);
-           return ret;
+          bgp_notify_send (peer, 
+                           BGP_NOTIFY_UPDATE_ERR,
+                           BGP_NOTIFY_UPDATE_MAL_ATTR);
+          if (as4_path)
+            aspath_unintern (&as4_path);
+          return ret;
         }
-
+      if (ret == BGP_ATTR_PARSE_WITHDRAW)
+        {
+          
+          zlog (peer->log, LOG_WARNING,
+                "%s: Attribute %s, parse error - treating as withdrawal",
+                peer->host,
+                LOOKUP (attr_str, type));
+          if (as4_path)
+            aspath_unintern (&as4_path);
+          return ret;
+        }
+      
       /* Check the fetched length. */
       if (BGP_INPUT_PNT (peer) != attr_endp)
 	{
@@ -1683,7 +1930,9 @@
 	  bgp_notify_send (peer, 
 			   BGP_NOTIFY_UPDATE_ERR, 
 			   BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
-	  return -1;
+          if (as4_path)
+            aspath_unintern (&as4_path);
+	  return BGP_ATTR_PARSE_ERROR;
 	}
     }
 
@@ -1691,12 +1940,14 @@
   if (BGP_INPUT_PNT (peer) != endp)
     {
       zlog (peer->log, LOG_WARNING, 
-	    "%s BGP attribute %s, length mismatch",
+	    "%s: BGP attribute %s, length mismatch",
 	    peer->host, LOOKUP (attr_str, type));
       bgp_notify_send (peer, 
 		       BGP_NOTIFY_UPDATE_ERR, 
 		       BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
-      return -1;
+      if (as4_path)
+        aspath_unintern (&as4_path);
+      return BGP_ATTR_PARSE_ERROR;
     }
 
   /* 
@@ -1712,17 +1963,20 @@
    */
   if (bgp_attr_munge_as4_attrs (peer, attr, as4_path,
                                 as4_aggregator, &as4_aggregator_addr))
-    return -1;
+    {
+      if (as4_path)
+        aspath_unintern (&as4_path);
+      return BGP_ATTR_PARSE_ERROR;
+    }
 
   /* At this stage, we have done all fiddling with as4, and the
    * resulting info is in attr->aggregator resp. attr->aspath
    * so we can chuck as4_aggregator and as4_path alltogether in
    * order to save memory
    */
-  if ( as4_path )
+  if (as4_path)
     {
-      aspath_unintern( as4_path ); /* unintern - it is in the hash */
-      as4_path = NULL;
+      aspath_unintern (&as4_path); /* unintern - it is in the hash */
       /* The flag that we got this is still there, but that does not
        * do any trouble
        */
@@ -1737,10 +1991,10 @@
    * Finally do the checks on the aspath we did not do yet
    * because we waited for a potentially synthesized aspath.
    */
-  if ( attr->flag & ( ATTR_FLAG_BIT( BGP_ATTR_AS_PATH)))
+  if (attr->flag & (ATTR_FLAG_BIT(BGP_ATTR_AS_PATH)))
     {
-      ret = bgp_attr_aspath_check( peer, attr );
-      if ( ret < 0 )
+      ret = bgp_attr_aspath_check (peer, attr);
+      if (ret != BGP_ATTR_PARSE_PROCEED)
 	return ret;
     }
 
@@ -1748,7 +2002,7 @@
   if (attr->extra && attr->extra->transit)
     attr->extra->transit = transit_intern (attr->extra->transit);
 
-  return 0;
+  return BGP_ATTR_PARSE_PROCEED;
 }
 
 /* Well-known attribute check. */
@@ -1779,9 +2033,9 @@
 				 BGP_NOTIFY_UPDATE_ERR, 
 				 BGP_NOTIFY_UPDATE_MISS_ATTR,
 				 &type, 1);
-      return -1;
+      return BGP_ATTR_PARSE_ERROR;
     }
-  return 0;
+  return BGP_ATTR_PARSE_PROCEED;
 }
 
 int stream_put_prefix (struct stream *, struct prefix *);
@@ -2077,7 +2331,7 @@
       sizep = stream_get_endp (s);
       stream_putc (s, 0);	/* Length of this attribute. */
       stream_putw (s, AFI_IP);	/* AFI */
-      stream_putc (s, BGP_SAFI_VPNV4);	/* SAFI */
+      stream_putc (s, SAFI_MPLS_LABELED_VPN);	/* SAFI */
 
       stream_putc (s, 12);
       stream_putl (s, 0);
@@ -2240,7 +2494,7 @@
   if (safi == SAFI_MPLS_VPN)
     {
       /* SAFI */
-      stream_putc (s, BGP_SAFI_VPNV4);
+      stream_putc (s, SAFI_MPLS_LABELED_VPN);
 
       /* prefix. */
       stream_putc (s, p->prefixlen + 88);
diff --git a/bgpd/bgp_attr.h b/bgpd/bgp_attr.h
index af9dcf5..df87c86 100644
--- a/bgpd/bgp_attr.h
+++ b/bgpd/bgp_attr.h
@@ -132,17 +132,25 @@
 
 #define ATTR_FLAG_BIT(X)  (1 << ((X) - 1))
 
+typedef enum {
+ BGP_ATTR_PARSE_PROCEED = 0,
+ BGP_ATTR_PARSE_ERROR = -1,
+ BGP_ATTR_PARSE_WITHDRAW = -2,
+} bgp_attr_parse_ret_t;
+
 /* Prototypes. */
 extern void bgp_attr_init (void);
 extern void bgp_attr_finish (void);
-extern int bgp_attr_parse (struct peer *, struct attr *, bgp_size_t,
-		    struct bgp_nlri *, struct bgp_nlri *);
+extern bgp_attr_parse_ret_t bgp_attr_parse (struct peer *, struct attr *,
+                                           bgp_size_t, struct bgp_nlri *,
+                                           struct bgp_nlri *);
 extern int bgp_attr_check (struct peer *, struct attr *);
 extern struct attr_extra *bgp_attr_extra_get (struct attr *);
 extern void bgp_attr_extra_free (struct attr *);
 extern void bgp_attr_dup (struct attr *, struct attr *);
 extern struct attr *bgp_attr_intern (struct attr *attr);
-extern void bgp_attr_unintern (struct attr *);
+extern void bgp_attr_unintern_sub (struct attr *);
+extern void bgp_attr_unintern (struct attr **);
 extern void bgp_attr_flush (struct attr *);
 extern struct attr *bgp_attr_default_set (struct attr *attr, u_char);
 extern struct attr *bgp_attr_default_intern (u_char);
@@ -171,9 +179,19 @@
 /* Transit attribute prototypes. */
 void transit_unintern (struct transit *);
 
-/* Exported for unit-test purposes only */
-extern int bgp_mp_reach_parse (struct peer *, bgp_size_t, struct attr *,
+/* Below exported for unit-test purposes only */
+struct bgp_attr_parser_args {
+  struct peer *peer;
+  bgp_size_t length; /* attribute data length; */
+  bgp_size_t total; /* total length, inc header */
+  struct attr *attr;
+  u_int8_t type;
+  u_int8_t flags;
+  u_char *startp;   
+};
+extern int bgp_mp_reach_parse (struct bgp_attr_parser_args *args, 
 			       struct bgp_nlri *);
-extern int bgp_mp_unreach_parse (struct peer *, bgp_size_t, struct bgp_nlri *);
+extern int bgp_mp_unreach_parse (struct bgp_attr_parser_args *args,
+                                 struct bgp_nlri *);
 
 #endif /* _QUAGGA_BGP_ATTR_H */
diff --git a/bgpd/bgp_clist.c b/bgpd/bgp_clist.c
index d660167..6c9976e 100644
--- a/bgpd/bgp_clist.c
+++ b/bgpd/bgp_clist.c
@@ -70,7 +70,7 @@
       if (entry->config)
         XFREE (MTYPE_ECOMMUNITY_STR, entry->config);
       if (entry->u.ecom)
-        ecommunity_free (entry->u.ecom);
+        ecommunity_free (&entry->u.ecom);
       break;
     case COMMUNITY_LIST_EXPANDED:
     case EXTCOMMUNITY_LIST_EXPANDED:
@@ -806,7 +806,7 @@
     entry = community_list_entry_lookup (list, str, direct);
 
   if (ecom)
-    ecommunity_free (ecom);
+    ecommunity_free (&ecom);
   if (regex)
     bgp_regex_free (regex);
 
diff --git a/bgpd/bgp_community.c b/bgpd/bgp_community.c
index ae1d7a1..2ba45f6 100644
--- a/bgpd/bgp_community.c
+++ b/bgpd/bgp_community.c
@@ -321,21 +321,22 @@
 
 /* Free community attribute. */
 void
-community_unintern (struct community *com)
+community_unintern (struct community **com)
 {
   struct community *ret;
 
-  if (com->refcnt)
-    com->refcnt--;
+  if ((*com)->refcnt)
+    (*com)->refcnt--;
 
   /* Pull off from hash.  */
-  if (com->refcnt == 0)
+  if ((*com)->refcnt == 0)
     {
       /* Community value com must exist in hash. */
-      ret = (struct community *) hash_release (comhash, com);
+      ret = (struct community *) hash_release (comhash, *com);
       assert (ret != NULL);
 
-      community_free (com);
+      community_free (*com);
+      *com = NULL;
     }
 }
 
diff --git a/bgpd/bgp_community.h b/bgpd/bgp_community.h
index bc1e56e..9e48377 100644
--- a/bgpd/bgp_community.h
+++ b/bgpd/bgp_community.h
@@ -57,7 +57,7 @@
 extern struct community *community_uniq_sort (struct community *);
 extern struct community *community_parse (u_int32_t *, u_short);
 extern struct community *community_intern (struct community *);
-extern void community_unintern (struct community *);
+extern void community_unintern (struct community **);
 extern char *community_str (struct community *);
 extern unsigned int community_hash_make (struct community *);
 extern struct community *community_str2com (const char *);
diff --git a/bgpd/bgp_debug.c b/bgpd/bgp_debug.c
index 26b35df..8e16186 100644
--- a/bgpd/bgp_debug.c
+++ b/bgpd/bgp_debug.c
@@ -105,6 +105,7 @@
 
 static const struct message bgp_notify_open_msg[] = 
 {
+  { BGP_NOTIFY_SUBCODE_UNSPECIFIC, "/Unspecific"},
   { BGP_NOTIFY_OPEN_UNSUP_VERSION, "/Unsupported Version Number" },
   { BGP_NOTIFY_OPEN_BAD_PEER_AS, "/Bad Peer AS"},
   { BGP_NOTIFY_OPEN_BAD_BGP_IDENT, "/Bad BGP Identifier"},
@@ -117,6 +118,7 @@
 
 static const struct message bgp_notify_update_msg[] = 
 {
+  { BGP_NOTIFY_SUBCODE_UNSPECIFIC, "/Unspecific"},
   { BGP_NOTIFY_UPDATE_MAL_ATTR, "/Malformed Attribute List"},
   { BGP_NOTIFY_UPDATE_UNREC_ATTR, "/Unrecognized Well-known Attribute"},
   { BGP_NOTIFY_UPDATE_MISS_ATTR, "/Missing Well-known Attribute"},
@@ -133,6 +135,7 @@
 
 static const struct message bgp_notify_cease_msg[] =
 {
+  { BGP_NOTIFY_SUBCODE_UNSPECIFIC, "/Unspecific"},
   { BGP_NOTIFY_CEASE_MAX_PREFIX, "/Maximum Number of Prefixes Reached"},
   { BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN, "/Administratively Shutdown"},
   { BGP_NOTIFY_CEASE_PEER_UNCONFIG, "/Peer Unconfigured"},
@@ -146,6 +149,7 @@
 
 static const struct message bgp_notify_capability_msg[] = 
 {
+  { BGP_NOTIFY_SUBCODE_UNSPECIFIC, "/Unspecific"},
   { BGP_NOTIFY_CAPABILITY_INVALID_ACTION, "/Invalid Action Value" },
   { BGP_NOTIFY_CAPABILITY_INVALID_LENGTH, "/Invalid Capability Length"},
   { BGP_NOTIFY_CAPABILITY_MALFORMED_CODE, "/Malformed Capability Value"},
diff --git a/bgpd/bgp_ecommunity.c b/bgpd/bgp_ecommunity.c
index 8d5fa74..440c15a 100644
--- a/bgpd/bgp_ecommunity.c
+++ b/bgpd/bgp_ecommunity.c
@@ -42,13 +42,14 @@
 
 /* Allocate ecommunities.  */
 void
-ecommunity_free (struct ecommunity *ecom)
+ecommunity_free (struct ecommunity **ecom)
 {
-  if (ecom->val)
-    XFREE (MTYPE_ECOMMUNITY_VAL, ecom->val);
-  if (ecom->str)
-    XFREE (MTYPE_ECOMMUNITY_STR, ecom->str);
-  XFREE (MTYPE_ECOMMUNITY, ecom);
+  if ((*ecom)->val)
+    XFREE (MTYPE_ECOMMUNITY_VAL, (*ecom)->val);
+  if ((*ecom)->str)
+    XFREE (MTYPE_ECOMMUNITY_STR, (*ecom)->str);
+  XFREE (MTYPE_ECOMMUNITY, *ecom);
+  ecom = NULL;
 }
 
 /* Add a new Extended Communities value to Extended Communities
@@ -197,7 +198,7 @@
   find = (struct ecommunity *) hash_get (ecomhash, ecom, hash_alloc_intern);
 
   if (find != ecom)
-    ecommunity_free (ecom);
+    ecommunity_free (&ecom);
 
   find->refcnt++;
 
@@ -209,18 +210,18 @@
 
 /* Unintern Extended Communities Attribute.  */
 void
-ecommunity_unintern (struct ecommunity *ecom)
+ecommunity_unintern (struct ecommunity **ecom)
 {
   struct ecommunity *ret;
 
-  if (ecom->refcnt)
-    ecom->refcnt--;
-
+  if ((*ecom)->refcnt)
+    (*ecom)->refcnt--;
+    
   /* Pull off from hash.  */
-  if (ecom->refcnt == 0)
+  if ((*ecom)->refcnt == 0)
     {
       /* Extended community must be in the hash.  */
-      ret = (struct ecommunity *) hash_release (ecomhash, ecom);
+      ret = (struct ecommunity *) hash_release (ecomhash, *ecom);
       assert (ret != NULL);
 
       ecommunity_free (ecom);
@@ -516,7 +517,7 @@
 	  if (! keyword_included || keyword)
 	    {
 	      if (ecom)
-		ecommunity_free (ecom);
+		ecommunity_free (&ecom);
 	      return NULL;
 	    }
 	  keyword = 1;
@@ -536,7 +537,7 @@
 	      if (! keyword)
 		{
 		  if (ecom)
-		    ecommunity_free (ecom);
+		    ecommunity_free (&ecom);
 		  return NULL;
 		}
 	      keyword = 0;
@@ -549,7 +550,7 @@
 	case ecommunity_token_unknown:
 	default:
 	  if (ecom)
-	    ecommunity_free (ecom);
+	    ecommunity_free (&ecom);
 	  return NULL;
 	}
     }
@@ -619,6 +620,13 @@
 
   for (i = 0; i < ecom->size; i++)
     {
+      /* Make it sure size is enough.  */
+      while (str_pnt + ECOMMUNITY_STR_DEFAULT_LEN >= str_size)
+	{
+	  str_size *= 2;
+	  str_buf = XREALLOC (MTYPE_ECOMMUNITY_STR, str_buf, str_size);
+	}
+
       /* Space between each value.  */
       if (! first)
 	str_buf[str_pnt++] = ' ';
@@ -662,13 +670,6 @@
 	  break;
 	}
 
-      /* Make it sure size is enough.  */
-      while (str_pnt + ECOMMUNITY_STR_DEFAULT_LEN >= str_size)
-	{
-	  str_size *= 2;
-	  str_buf = XREALLOC (MTYPE_ECOMMUNITY_STR, str_buf, str_size);
-	}
-
       /* Put string into buffer.  */
       if (encode == ECOMMUNITY_ENCODE_AS4)
 	{
diff --git a/bgpd/bgp_ecommunity.h b/bgpd/bgp_ecommunity.h
index 942fdc7..2f59dc4 100644
--- a/bgpd/bgp_ecommunity.h
+++ b/bgpd/bgp_ecommunity.h
@@ -67,13 +67,13 @@
 
 extern void ecommunity_init (void);
 extern void ecommunity_finish (void);
-extern void ecommunity_free (struct ecommunity *);
+extern void ecommunity_free (struct ecommunity **);
 extern struct ecommunity *ecommunity_parse (u_int8_t *, u_short);
 extern struct ecommunity *ecommunity_dup (struct ecommunity *);
 extern struct ecommunity *ecommunity_merge (struct ecommunity *, struct ecommunity *);
 extern struct ecommunity *ecommunity_intern (struct ecommunity *);
 extern int ecommunity_cmp (const void *, const void *);
-extern void ecommunity_unintern (struct ecommunity *);
+extern void ecommunity_unintern (struct ecommunity **);
 extern unsigned int ecommunity_hash_make (void *);
 extern struct ecommunity *ecommunity_str2com (const char *, int, int);
 extern char *ecommunity_ecom2str (struct ecommunity *, int);
diff --git a/bgpd/bgp_fsm.c b/bgpd/bgp_fsm.c
index 487ebdd..813e59e 100644
--- a/bgpd/bgp_fsm.c
+++ b/bgpd/bgp_fsm.c
@@ -355,7 +355,7 @@
 
   /* NSF delete stale route */
   for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
-    for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++)
+    for (safi = SAFI_UNICAST ; safi < SAFI_RESERVED_3 ; safi++)
       if (peer->nsf[afi][safi])
 	bgp_clear_stale_route (peer, afi, safi);
 
@@ -388,7 +388,7 @@
 
   /* NSF delete stale route */
   for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
-    for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++)
+    for (safi = SAFI_UNICAST ; safi < SAFI_RESERVED_3 ; safi++)
       if (peer->nsf[afi][safi])
 	bgp_clear_stale_route (peer, afi, safi);
 
@@ -481,7 +481,7 @@
 	  UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
 
 	  for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
-	    for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++)
+	    for (safi = SAFI_UNICAST ; safi < SAFI_RESERVED_3 ; safi++)
 	      peer->nsf[afi][safi] = 0;
 	}
 
@@ -799,7 +799,7 @@
   /* graceful restart */
   UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT);
   for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
-    for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++)
+    for (safi = SAFI_UNICAST ; safi < SAFI_RESERVED_3 ; safi++)
       {
 	if (peer->afc_nego[afi][safi]
 	    && CHECK_FLAG (peer->cap, PEER_CAP_RESTART_ADV)
diff --git a/bgpd/bgp_main.c b/bgpd/bgp_main.c
index 1a460c6..8dede58 100644
--- a/bgpd/bgp_main.c
+++ b/bgpd/bgp_main.c
@@ -54,6 +54,7 @@
   { "daemon",      no_argument,       NULL, 'd'},
   { "config_file", required_argument, NULL, 'f'},
   { "pid_file",    required_argument, NULL, 'i'},
+  { "socket",      required_argument, NULL, 'z'},
   { "bgp_port",    required_argument, NULL, 'p'},
   { "listenon",    required_argument, NULL, 'l'},
   { "vty_addr",    required_argument, NULL, 'A'},
@@ -119,6 +120,7 @@
 {
     ZCAP_BIND, 
     ZCAP_NET_RAW,
+    ZCAP_NET_ADMIN,
 };
 
 struct zebra_privs_t bgpd_privs =
@@ -149,6 +151,7 @@
 -d, --daemon       Runs in daemon mode\n\
 -f, --config_file  Set configuration file name\n\
 -i, --pid_file     Set process identifier file name\n\
+-z, --socket       Set path of zebra socket\n\
 -p, --bgp_port     Set bgp protocol's port number\n\
 -l, --listenon     Listen on specified address (implies -n)\n\
 -A, --vty_addr     Set vty's bind address\n\
@@ -196,6 +199,7 @@
   if (! retain_mode)
     bgp_terminate ();
 
+  zprivs_terminate (&bgpd_privs);
   bgp_exit (0);
 }
 
@@ -335,7 +339,7 @@
   /* Command line argument treatment. */
   while (1) 
     {
-      opt = getopt_long (argc, argv, "df:i:hp:l:A:P:rnu:g:vC", longopts, 0);
+      opt = getopt_long (argc, argv, "df:i:z:hp:l:A:P:rnu:g:vC", longopts, 0);
     
       if (opt == EOF)
 	break;
@@ -353,6 +357,9 @@
         case 'i':
           pid_file = optarg;
           break;
+	case 'z':
+	  zclient_serv_path_set (optarg);
+	  break;
 	case 'p':
 	  tmp_port = atoi (optarg);
 	  if (tmp_port <= 0 || tmp_port > 0xffff)
diff --git a/bgpd/bgp_mplsvpn.c b/bgpd/bgp_mplsvpn.c
index 72ad089..c1f1fbb 100644
--- a/bgpd/bgp_mplsvpn.c
+++ b/bgpd/bgp_mplsvpn.c
@@ -233,9 +233,13 @@
   char *endptr;
   u_int32_t t;
 
-  l = strtoul (str, &endptr, 10);
+  if (*str == '-')
+    return 0;
   
-  if (*endptr == '\0' || l == ULONG_MAX || l > UINT32_MAX)
+  errno = 0;
+  l = strtoul (str, &endptr, 10);
+
+  if (*endptr != '\0' || errno || l > UINT32_MAX)
     return 0;
 
   t = (u_int32_t) l;
diff --git a/bgpd/bgp_network.c b/bgpd/bgp_network.c
index 570cc3b..a7dca53 100644
--- a/bgpd/bgp_network.c
+++ b/bgpd/bgp_network.c
@@ -238,46 +238,36 @@
 }
 
 static int
-bgp_bind_address (int sock, struct in_addr *addr)
+bgp_update_address (struct interface *ifp, const union sockunion *dst,
+		    union sockunion *addr)
 {
-  int ret;
-  struct sockaddr_in local;
-
-  memset (&local, 0, sizeof (struct sockaddr_in));
-  local.sin_family = AF_INET;
-#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
-  local.sin_len = sizeof(struct sockaddr_in);
-#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
-  memcpy (&local.sin_addr, addr, sizeof (struct in_addr));
-
-  if ( bgpd_privs.change (ZPRIVS_RAISE) )
-    zlog_err ("bgp_bind_address: could not raise privs");
-    
-  ret = bind (sock, (struct sockaddr *)&local, sizeof (struct sockaddr_in));
-  if (ret < 0)
-    ;
-    
-  if (bgpd_privs.change (ZPRIVS_LOWER) )
-    zlog_err ("bgp_bind_address: could not lower privs");
-    
-  return 0;
-}
-
-static struct in_addr *
-bgp_update_address (struct interface *ifp)
-{
-  struct prefix_ipv4 *p;
+  struct prefix *p, *sel, *d;
   struct connected *connected;
   struct listnode *node;
+  int common;
+
+  d = sockunion2hostprefix (dst);
+  sel = NULL;
+  common = -1;
 
   for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, connected))
     {
-      p = (struct prefix_ipv4 *) connected->address;
-
-      if (p->family == AF_INET)
-	return &p->prefix;
+      p = connected->address;
+      if (p->family != d->family)
+	continue;
+      if (prefix_common_bits (p, d) > common)
+	{
+	  sel = p;
+	  common = prefix_common_bits (sel, d);
+	}
     }
-  return NULL;
+
+  prefix_free (d);
+  if (!sel)
+    return 1;
+
+  prefix2sockunion (sel, addr);
+  return 0;
 }
 
 /* Update source selection.  */
@@ -285,7 +275,7 @@
 bgp_update_source (struct peer *peer)
 {
   struct interface *ifp;
-  struct in_addr *addr;
+  union sockunion addr;
 
   /* Source is specified with interface name.  */
   if (peer->update_if)
@@ -294,11 +284,10 @@
       if (! ifp)
 	return;
 
-      addr = bgp_update_address (ifp);
-      if (! addr)
+      if (bgp_update_address (ifp, &peer->su, &addr))
 	return;
 
-      bgp_bind_address (peer->fd, addr);
+      sockunion_bind (peer->fd, &addr, 0, &addr);
     }
 
   /* Source is specified with IP address.  */
@@ -328,8 +317,16 @@
   sockopt_reuseport (peer->fd);
   
 #ifdef IPTOS_PREC_INTERNETCONTROL
+  if (bgpd_privs.change (ZPRIVS_RAISE))
+    zlog_err ("%s: could not raise privs", __func__);
   if (sockunion_family (&peer->su) == AF_INET)
     setsockopt_ipv4_tos (peer->fd, IPTOS_PREC_INTERNETCONTROL);
+# ifdef HAVE_IPV6
+  else if (sockunion_family (&peer->su) == AF_INET6)
+    setsockopt_ipv6_tclass (peer->fd, IPTOS_PREC_INTERNETCONTROL);
+# endif
+  if (bgpd_privs.change (ZPRIVS_LOWER))
+    zlog_err ("%s: could not lower privs", __func__);
 #endif
 
   if (peer->password)
@@ -386,27 +383,24 @@
   sockopt_reuseaddr (sock);
   sockopt_reuseport (sock);
 
+  if (bgpd_privs.change (ZPRIVS_RAISE))
+    zlog_err ("%s: could not raise privs", __func__);
+
 #ifdef IPTOS_PREC_INTERNETCONTROL
   if (sa->sa_family == AF_INET)
     setsockopt_ipv4_tos (sock, IPTOS_PREC_INTERNETCONTROL);
+#  ifdef HAVE_IPV6
+  else if (sa->sa_family == AF_INET6)
+    setsockopt_ipv6_tclass (sock, IPTOS_PREC_INTERNETCONTROL);
+#  endif
 #endif
 
-#ifdef IPV6_V6ONLY
-  /* Want only IPV6 on ipv6 socket (not mapped addresses) */
-  if (sa->sa_family == AF_INET6) {
-    int on = 1;
-    setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY,
-		(void *) &on, sizeof (on));
-  }
-#endif
-
-  if (bgpd_privs.change (ZPRIVS_RAISE) )
-    zlog_err ("bgp_socket: could not raise privs");
+  sockopt_v6only (sa->sa_family, sock);
 
   ret = bind (sock, sa, salen);
   en = errno;
-  if (bgpd_privs.change (ZPRIVS_LOWER) )
-    zlog_err ("bgp_bind_address: could not lower privs");
+  if (bgpd_privs.change (ZPRIVS_LOWER))
+    zlog_err ("%s: could not lower privs", __func__);
 
   if (ret < 0)
     {
diff --git a/bgpd/bgp_nexthop.c b/bgpd/bgp_nexthop.c
index 719cb96..fdf251b 100644
--- a/bgpd/bgp_nexthop.c
+++ b/bgpd/bgp_nexthop.c
@@ -146,7 +146,7 @@
 }
 
 static int
-bgp_nexthop_cache_changed (struct bgp_nexthop_cache *bnc1,
+bgp_nexthop_cache_different (struct bgp_nexthop_cache *bnc1,
 			   struct bgp_nexthop_cache *bnc2)
 {
   int i;
@@ -171,14 +171,14 @@
 
 /* If nexthop exists on connected network return 1. */
 int
-bgp_nexthop_check_ebgp (afi_t afi, struct attr *attr)
+bgp_nexthop_onlink (afi_t afi, struct attr *attr)
 {
   struct bgp_node *rn;
-
+  
   /* If zebra is not enabled return */
   if (zlookup->sock < 0)
     return 1;
-
+  
   /* Lookup the address is onlink or not. */
   if (afi == AFI_IP)
     {
@@ -222,7 +222,7 @@
   struct prefix p;
   struct bgp_nexthop_cache *bnc;
   struct attr *attr;
-
+  
   /* If lookup is not enabled, return valid. */
   if (zlookup->sock < 0)
     {
@@ -230,7 +230,7 @@
         ri->extra->igpmetric = 0;
       return 1;
     }
-
+  
   /* Only check IPv6 global address only nexthop. */
   attr = ri->attr;
 
@@ -253,15 +253,15 @@
     }
   else
     {
-      bnc = zlookup_query_ipv6 (&attr->extra->mp_nexthop_global);
-      if (bnc)
+      if (NULL == (bnc = zlookup_query_ipv6 (&attr->extra->mp_nexthop_global)))
+	bnc = bnc_new ();
+      else
 	{
-	  struct bgp_table *old;
-	  struct bgp_node *oldrn;
-	  struct bgp_nexthop_cache *oldbnc;
-
 	  if (changed)
 	    {
+	      struct bgp_table *old;
+	      struct bgp_node *oldrn;
+
 	      if (bgp_nexthop_cache_table[AFI_IP6] == cache1_table[AFI_IP6])
 		old = cache2_table[AFI_IP6];
 	      else
@@ -270,9 +270,9 @@
 	      oldrn = bgp_node_lookup (old, &p);
 	      if (oldrn)
 		{
-		  oldbnc = oldrn->info;
+		  struct bgp_nexthop_cache *oldbnc = oldrn->info;
 
-		  bnc->changed = bgp_nexthop_cache_changed (bnc, oldbnc);
+		  bnc->changed = bgp_nexthop_cache_different (bnc, oldbnc);
 
 		  if (bnc->metric != oldbnc->metric)
 		    bnc->metricchanged = 1;
@@ -281,11 +281,6 @@
 		}
 	    }
 	}
-      else
-	{
-	  bnc = bnc_new ();
-	  bnc->valid = 0;
-	}
       rn->info = bnc;
     }
 
@@ -313,7 +308,7 @@
   struct prefix p;
   struct bgp_nexthop_cache *bnc;
   struct in_addr addr;
-
+  
   /* If lookup is not enabled, return valid. */
   if (zlookup->sock < 0)
     {
@@ -321,7 +316,7 @@
         ri->extra->igpmetric = 0;
       return 1;
     }
-
+  
 #ifdef HAVE_IPV6
   if (afi == AFI_IP6)
     return bgp_nexthop_lookup_ipv6 (peer, ri, changed, metricchanged);
@@ -344,15 +339,15 @@
     }
   else
     {
-      bnc = zlookup_query (addr);
-      if (bnc)
+      if (NULL == (bnc = zlookup_query (addr)))
+	bnc = bnc_new ();
+      else
 	{
-	  struct bgp_table *old;
-	  struct bgp_node *oldrn;
-	  struct bgp_nexthop_cache *oldbnc;
-
 	  if (changed)
 	    {
+	      struct bgp_table *old;
+	      struct bgp_node *oldrn;
+
 	      if (bgp_nexthop_cache_table[AFI_IP] == cache1_table[AFI_IP])
 		old = cache2_table[AFI_IP];
 	      else
@@ -361,9 +356,9 @@
 	      oldrn = bgp_node_lookup (old, &p);
 	      if (oldrn)
 		{
-		  oldbnc = oldrn->info;
+		  struct bgp_nexthop_cache *oldbnc = oldrn->info;
 
-		  bnc->changed = bgp_nexthop_cache_changed (bnc, oldbnc);
+		  bnc->changed = bgp_nexthop_cache_different (bnc, oldbnc);
 
 		  if (bnc->metric != oldbnc->metric)
 		    bnc->metricchanged = 1;
@@ -372,11 +367,6 @@
 		}
 	    }
 	}
-      else
-	{
-	  bnc = bnc_new ();
-	  bnc->valid = 0;
-	}
       rn->info = bnc;
     }
 
@@ -462,7 +452,7 @@
 	      metricchanged = 0;
 
 	      if (peer_sort (bi->peer) == BGP_PEER_EBGP && bi->peer->ttl == 1)
-		valid = bgp_nexthop_check_ebgp (afi, bi->attr);
+		valid = bgp_nexthop_onlink (afi, bi->attr);
 	      else
 		valid = bgp_nexthop_lookup (afi, bi->peer, bi,
 					    &changed, &metricchanged);
@@ -1098,12 +1088,7 @@
   if (zlookup->sock != -1)
     return 0;
 
-#ifdef HAVE_TCP_ZEBRA
-  zlookup->sock = zclient_socket ();
-#else
-  zlookup->sock = zclient_socket_un (ZEBRA_SERV_PATH);
-#endif /* HAVE_TCP_ZEBRA */
-  if (zlookup->sock < 0)
+  if (zclient_socket_connect (zlookup) < 0)
     return -1;
 
   return 0;
@@ -1201,16 +1186,13 @@
        "Configure background scanner interval\n"
        "Scanner interval (seconds)\n")
 
-DEFUN (show_ip_bgp_scan,
-       show_ip_bgp_scan_cmd,
-       "show ip bgp scan",
-       SHOW_STR
-       IP_STR
-       BGP_STR
-       "BGP scan status\n")
+static int
+show_ip_bgp_scan_tables (struct vty *vty, const char detail)
 {
   struct bgp_node *rn;
   struct bgp_nexthop_cache *bnc;
+  char buf[INET6_ADDRSTRLEN];
+  u_char i;
 
   if (bgp_scan_thread)
     vty_out (vty, "BGP scan is running%s", VTY_NEWLINE);
@@ -1223,28 +1205,57 @@
     if ((bnc = rn->info) != NULL)
       {
 	if (bnc->valid)
+	{
 	  vty_out (vty, " %s valid [IGP metric %d]%s",
-		   inet_ntoa (rn->p.u.prefix4), bnc->metric, VTY_NEWLINE);
+		   inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN), bnc->metric, VTY_NEWLINE);
+	  if (detail)
+	    for (i = 0; i < bnc->nexthop_num; i++)
+	      switch (bnc->nexthop[i].type)
+	      {
+	      case NEXTHOP_TYPE_IPV4:
+		vty_out (vty, "  gate %s%s", inet_ntop (AF_INET, &bnc->nexthop[i].gate.ipv4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
+		break;
+	      case NEXTHOP_TYPE_IFINDEX:
+		vty_out (vty, "  ifidx %u%s", bnc->nexthop[i].ifindex, VTY_NEWLINE);
+		break;
+	      default:
+		vty_out (vty, "  invalid nexthop type %u%s", bnc->nexthop[i].type, VTY_NEWLINE);
+	      }
+	}
 	else
 	  vty_out (vty, " %s invalid%s",
-		   inet_ntoa (rn->p.u.prefix4), VTY_NEWLINE);
+		   inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
       }
 
 #ifdef HAVE_IPV6
   {
-    char buf[BUFSIZ];
     for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP6]); 
          rn; 
          rn = bgp_route_next (rn))
       if ((bnc = rn->info) != NULL)
 	{
 	  if (bnc->valid)
+	  {
 	    vty_out (vty, " %s valid [IGP metric %d]%s",
-		     inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ),
+		     inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
 		     bnc->metric, VTY_NEWLINE);
+	    if (detail)
+	      for (i = 0; i < bnc->nexthop_num; i++)
+		switch (bnc->nexthop[i].type)
+		{
+		case NEXTHOP_TYPE_IPV6:
+		  vty_out (vty, "  gate %s%s", inet_ntop (AF_INET6, &bnc->nexthop[i].gate.ipv6, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
+		  break;
+		case NEXTHOP_TYPE_IFINDEX:
+		  vty_out (vty, "  ifidx %u%s", bnc->nexthop[i].ifindex, VTY_NEWLINE);
+		  break;
+		default:
+		  vty_out (vty, "  invalid nexthop type %u%s", bnc->nexthop[i].type, VTY_NEWLINE);
+		}
+	  }
 	  else
 	    vty_out (vty, " %s invalid%s",
-		     inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ),
+		     inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
 		     VTY_NEWLINE);
 	}
   }
@@ -1260,14 +1271,12 @@
 
 #ifdef HAVE_IPV6
   {
-    char buf[BUFSIZ];
-
     for (rn = bgp_table_top (bgp_connected_table[AFI_IP6]); 
          rn; 
          rn = bgp_route_next (rn))
       if (rn->info != NULL)
 	vty_out (vty, " %s/%d%s",
-		 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ),
+		 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
 		 rn->p.prefixlen,
 		 VTY_NEWLINE);
   }
@@ -1276,6 +1285,29 @@
   return CMD_SUCCESS;
 }
 
+DEFUN (show_ip_bgp_scan,
+       show_ip_bgp_scan_cmd,
+       "show ip bgp scan",
+       SHOW_STR
+       IP_STR
+       BGP_STR
+       "BGP scan status\n")
+{
+  return show_ip_bgp_scan_tables (vty, 0);
+}
+
+DEFUN (show_ip_bgp_scan_detail,
+       show_ip_bgp_scan_detail_cmd,
+       "show ip bgp scan detail",
+       SHOW_STR
+       IP_STR
+       BGP_STR
+       "BGP scan status\n"
+       "More detailed output\n")
+{
+  return show_ip_bgp_scan_tables (vty, 1);
+}
+
 int
 bgp_config_write_scan_time (struct vty *vty)
 {
@@ -1317,8 +1349,10 @@
   install_element (BGP_NODE, &no_bgp_scan_time_cmd);
   install_element (BGP_NODE, &no_bgp_scan_time_val_cmd);
   install_element (VIEW_NODE, &show_ip_bgp_scan_cmd);
+  install_element (VIEW_NODE, &show_ip_bgp_scan_detail_cmd);
   install_element (RESTRICTED_NODE, &show_ip_bgp_scan_cmd);
   install_element (ENABLE_NODE, &show_ip_bgp_scan_cmd);
+  install_element (ENABLE_NODE, &show_ip_bgp_scan_detail_cmd);
 }
 
 void
diff --git a/bgpd/bgp_nexthop.h b/bgpd/bgp_nexthop.h
index 2dad742..874f3bb 100644
--- a/bgpd/bgp_nexthop.h
+++ b/bgpd/bgp_nexthop.h
@@ -54,7 +54,7 @@
 extern void bgp_connected_delete (struct connected *c);
 extern int bgp_multiaccess_check_v4 (struct in_addr, char *);
 extern int bgp_config_write_scan_time (struct vty *);
-extern int bgp_nexthop_check_ebgp (afi_t, struct attr *);
+extern int bgp_nexthop_onlink (afi_t, struct attr *);
 extern int bgp_nexthop_self (afi_t, struct attr *);
 
 #endif /* _QUAGGA_BGP_NEXTHOP_H */
diff --git a/bgpd/bgp_open.c b/bgpd/bgp_open.c
index 3759581..b5b50bb 100644
--- a/bgpd/bgp_open.c
+++ b/bgpd/bgp_open.c
@@ -93,11 +93,8 @@
 	    case SAFI_MULTICAST:
 	      vty_out (vty, "SAFI Multicast");
 	      break;
-	    case SAFI_UNICAST_MULTICAST:
-	      vty_out (vty, "SAFI Unicast Multicast");
-	      break;
-	    case BGP_SAFI_VPNV4:
-	      vty_out (vty, "SAFI MPLS-VPN");
+	    case SAFI_MPLS_LABELED_VPN:
+	      vty_out (vty, "SAFI MPLS-labeled VPN");
 	      break;
 	    default:
 	      vty_out (vty, "SAFI Unknown %d ", mpc.safi);
@@ -127,14 +124,6 @@
 int
 bgp_afi_safi_valid_indices (afi_t afi, safi_t *safi)
 {
-  /* VPNvX are AFI specific */
-  if ((afi == AFI_IP6 && *safi == BGP_SAFI_VPNV4)
-      || (afi == AFI_IP && *safi == BGP_SAFI_VPNV6))
-    {
-      zlog_warn ("Invalid afi/safi combination (%u/%u)", afi, *safi);
-      return 0;
-    }
-  
   switch (afi)
     {
       case AFI_IP:
@@ -143,9 +132,8 @@
 #endif
         switch (*safi)
           {
-            /* BGP VPNvX SAFI isn't contigious with others, remap */
-            case BGP_SAFI_VPNV4:
-            case BGP_SAFI_VPNV6:
+            /* BGP MPLS-labeled VPN SAFI isn't contigious with others, remap */
+            case SAFI_MPLS_LABELED_VPN:
               *safi = SAFI_MPLS_VPN;
             case SAFI_UNICAST:
             case SAFI_MULTICAST:
@@ -392,7 +380,7 @@
                   peer->v_gr_restart);
     }
 
-  while (stream_get_getp (s) + 4 < end)
+  while (stream_get_getp (s) + 4 <= end)
     {
       afi_t afi = stream_getw (s);
       safi_t safi = stream_getc (s);
@@ -433,13 +421,20 @@
 static as_t
 bgp_capability_as4 (struct peer *peer, struct capability_header *hdr)
 {
+  SET_FLAG (peer->cap, PEER_CAP_AS4_RCV);
+  
+  if (hdr->length != CAPABILITY_CODE_AS4_LEN)
+    {
+      zlog_err ("%s AS4 capability has incorrect data length %d",
+                peer->host, hdr->length);
+      return 0;
+    }
+  
   as_t as4 = stream_getl (BGP_INPUT(peer));
   
   if (BGP_DEBUG (as4, AS4))
     zlog_debug ("%s [AS4] about to set cap PEER_CAP_AS4_RCV, got as4 %u",
                 peer->host, as4);
-  SET_FLAG (peer->cap, PEER_CAP_AS4_RCV);
-  
   return as4;
 }
 
@@ -701,9 +696,6 @@
 
 	      if (hdr.code == CAPABILITY_CODE_AS4)
 	        {
-	          if (hdr.length != CAPABILITY_CODE_AS4_LEN)
-	            goto end;
-                  
 	          if (BGP_DEBUG (as4, AS4))
 	            zlog_info ("[AS4] found AS4 capability, about to parse");
 	          as4 = bgp_capability_as4 (peer, &hdr);
@@ -859,7 +851,7 @@
   int number_of_orfs = 0;
 
   if (safi == SAFI_MPLS_VPN)
-    safi = BGP_SAFI_VPNV4;
+    safi = SAFI_MPLS_LABELED_VPN;
 
   stream_putc (s, BGP_OPEN_OPT_CAP);
   capp = stream_get_endp (s);           /* Set Capability Len Pointer */
@@ -967,7 +959,7 @@
       stream_putc (s, CAPABILITY_CODE_MP_LEN);
       stream_putw (s, AFI_IP);
       stream_putc (s, 0);
-      stream_putc (s, BGP_SAFI_VPNV4);
+      stream_putc (s, SAFI_MPLS_LABELED_VPN);
     }
 #ifdef HAVE_IPV6
   /* IPv6 unicast. */
diff --git a/bgpd/bgp_packet.c b/bgpd/bgp_packet.c
index ed2cb73..5d8087a 100644
--- a/bgpd/bgp_packet.c
+++ b/bgpd/bgp_packet.c
@@ -206,7 +206,7 @@
 
       /* Synchnorize attribute.  */
       if (adj->attr)
-	bgp_attr_unintern (adj->attr);
+	bgp_attr_unintern (&adj->attr);
       else
 	peer->scount[afi][safi]++;
 
@@ -895,14 +895,27 @@
   if (sub_code != BGP_NOTIFY_CEASE_CONFIG_CHANGE)
     {
       if (sub_code == BGP_NOTIFY_CEASE_ADMIN_RESET)
-      peer->last_reset = PEER_DOWN_USER_RESET;
+      {
+        peer->last_reset = PEER_DOWN_USER_RESET;
+        zlog_info ("Notification sent to neighbor %s: User reset", peer->host);
+      }
       else if (sub_code == BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN)
-      peer->last_reset = PEER_DOWN_USER_SHUTDOWN;
+      {
+        peer->last_reset = PEER_DOWN_USER_SHUTDOWN;
+        zlog_info ("Notification sent to neighbor %s: shutdown", peer->host);
+      }
       else
-      peer->last_reset = PEER_DOWN_NOTIFY_SEND;
+      {
+        peer->last_reset = PEER_DOWN_NOTIFY_SEND;
+        zlog_info ("Notification sent to neighbor %s: type %u/%u",
+                   peer->host, code, sub_code);
+      }
     }
+  else
+     zlog_info ("Notification sent to neighbor %s: configuration change",
+                peer->host);
 
-  /* Call imidiately. */
+  /* Call immediately. */
   BGP_WRITE_OFF (peer->t_write);
 
   bgp_write_notify (peer);
@@ -933,7 +946,7 @@
 
   /* Adjust safi code. */
   if (safi == SAFI_MPLS_VPN)
-    safi = BGP_SAFI_VPNV4;
+    safi = SAFI_MPLS_LABELED_VPN;
   
   s = stream_new (BGP_MAX_PACKET_SIZE);
 
@@ -1023,7 +1036,7 @@
 
   /* Adjust safi code. */
   if (safi == SAFI_MPLS_VPN)
-    safi = BGP_SAFI_VPNV4;
+    safi = SAFI_MPLS_LABELED_VPN;
 
   s = stream_new (BGP_MAX_PACKET_SIZE);
 
@@ -1368,7 +1381,7 @@
 
   /* remote router-id check. */
   if (remote_id.s_addr == 0
-      || ntohl (remote_id.s_addr) >= 0xe0000000
+      || IPV4_CLASS_DE (ntohl (remote_id.s_addr))
       || ntohl (peer->local_id.s_addr) == ntohl (remote_id.s_addr))
     {
       if (BGP_DEBUG (normal, NORMAL))
@@ -1446,9 +1459,13 @@
   /* Open option part parse. */
   if (optlen != 0) 
     {
-      ret = bgp_open_option_parse (peer, optlen, &capability);
-      if (ret < 0)
-	return ret;
+      if ((ret = bgp_open_option_parse (peer, optlen, &capability)) < 0)
+        {
+          bgp_notify_send (peer,
+                 BGP_NOTIFY_OPEN_ERR,
+                 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME);
+	  return ret;
+        }
     }
   else
     {
@@ -1583,26 +1600,47 @@
 		       BGP_NOTIFY_UPDATE_MAL_ATTR);
       return -1;
     }
+  
+  /* Certain attribute parsing errors should not be considered bad enough
+   * to reset the session for, most particularly any partial/optional
+   * attributes that have 'tunneled' over speakers that don't understand
+   * them. Instead we withdraw only the prefix concerned.
+   * 
+   * Complicates the flow a little though..
+   */
+  bgp_attr_parse_ret_t attr_parse_ret = BGP_ATTR_PARSE_PROCEED;
+  /* This define morphs the update case into a withdraw when lower levels
+   * have signalled an error condition where this is best.
+   */
+#define NLRI_ATTR_ARG (attr_parse_ret != BGP_ATTR_PARSE_WITHDRAW ? &attr : NULL)
 
   /* Parse attribute when it exists. */
   if (attribute_len)
     {
-      ret = bgp_attr_parse (peer, &attr, attribute_len, 
+      attr_parse_ret = bgp_attr_parse (peer, &attr, attribute_len, 
 			    &mp_update, &mp_withdraw);
-      if (ret < 0)
+      if (attr_parse_ret == BGP_ATTR_PARSE_ERROR)
 	return -1;
     }
-
+  
   /* Logging the attribute. */
-  if (BGP_DEBUG (update, UPDATE_IN))
+  if (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW
+      || BGP_DEBUG (update, UPDATE_IN))
     {
       ret= bgp_dump_attr (peer, &attr, attrstr, BUFSIZ);
+      int lvl = (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW)
+                 ? LOG_ERR : LOG_DEBUG;
+      
+      if (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW)
+        zlog (peer->log, LOG_ERR,
+              "%s rcvd UPDATE with errors in attr(s)!! Withdrawing route.",
+              peer->host);
 
       if (ret)
-	zlog (peer->log, LOG_DEBUG, "%s rcvd UPDATE w/ attr: %s",
+	zlog (peer->log, lvl, "%s rcvd UPDATE w/ attr: %s",
 	      peer->host, attrstr);
     }
-
+  
   /* Network Layer Reachability Information. */
   update_len = end - stream_pnt (s);
 
@@ -1611,7 +1649,12 @@
       /* Check NLRI packet format and prefix length. */
       ret = bgp_nlri_sanity_check (peer, AFI_IP, stream_pnt (s), update_len);
       if (ret < 0)
-	return -1;
+        {
+          bgp_attr_unintern_sub (&attr);
+          if (attr.extra)
+            bgp_attr_extra_free (&attr);
+	  return -1;
+	}
 
       /* Set NLRI portion to structure. */
       update.afi = AFI_IP;
@@ -1634,15 +1677,20 @@
 	     update. */
 	  ret = bgp_attr_check (peer, &attr);
 	  if (ret < 0)
-	    return -1;
+	    {
+	      bgp_attr_unintern_sub (&attr);
+              if (attr.extra)
+                bgp_attr_extra_free (&attr);
+	      return -1;
+            }
 
-	  bgp_nlri_parse (peer, &attr, &update);
+	  bgp_nlri_parse (peer, NLRI_ATTR_ARG, &update);
 	}
 
       if (mp_update.length
 	  && mp_update.afi == AFI_IP 
 	  && mp_update.safi == SAFI_UNICAST)
-	bgp_nlri_parse (peer, &attr, &mp_update);
+	bgp_nlri_parse (peer, NLRI_ATTR_ARG, &mp_update);
 
       if (mp_withdraw.length
 	  && mp_withdraw.afi == AFI_IP 
@@ -1669,7 +1717,7 @@
       if (mp_update.length
 	  && mp_update.afi == AFI_IP 
 	  && mp_update.safi == SAFI_MULTICAST)
-	bgp_nlri_parse (peer, &attr, &mp_update);
+	bgp_nlri_parse (peer, NLRI_ATTR_ARG, &mp_update);
 
       if (mp_withdraw.length
 	  && mp_withdraw.afi == AFI_IP 
@@ -1699,7 +1747,7 @@
       if (mp_update.length 
 	  && mp_update.afi == AFI_IP6 
 	  && mp_update.safi == SAFI_UNICAST)
-	bgp_nlri_parse (peer, &attr, &mp_update);
+	bgp_nlri_parse (peer, NLRI_ATTR_ARG, &mp_update);
 
       if (mp_withdraw.length 
 	  && mp_withdraw.afi == AFI_IP6 
@@ -1728,7 +1776,7 @@
       if (mp_update.length 
 	  && mp_update.afi == AFI_IP6 
 	  && mp_update.safi == SAFI_MULTICAST)
-	bgp_nlri_parse (peer, &attr, &mp_update);
+	bgp_nlri_parse (peer, NLRI_ATTR_ARG, &mp_update);
 
       if (mp_withdraw.length 
 	  && mp_withdraw.afi == AFI_IP6 
@@ -1755,17 +1803,17 @@
     {
       if (mp_update.length 
 	  && mp_update.afi == AFI_IP 
-	  && mp_update.safi == BGP_SAFI_VPNV4)
-	bgp_nlri_parse_vpnv4 (peer, &attr, &mp_update);
+	  && mp_update.safi == SAFI_MPLS_LABELED_VPN)
+	bgp_nlri_parse_vpnv4 (peer, NLRI_ATTR_ARG, &mp_update);
 
       if (mp_withdraw.length 
 	  && mp_withdraw.afi == AFI_IP 
-	  && mp_withdraw.safi == BGP_SAFI_VPNV4)
+	  && mp_withdraw.safi == SAFI_MPLS_LABELED_VPN)
 	bgp_nlri_parse_vpnv4 (peer, NULL, &mp_withdraw);
 
       if (! withdraw_len
 	  && mp_withdraw.afi == AFI_IP
-	  && mp_withdraw.safi == BGP_SAFI_VPNV4
+	  && mp_withdraw.safi == SAFI_MPLS_LABELED_VPN
 	  && mp_withdraw.length == 0)
 	{
 	  /* End-of-RIB received */
@@ -1778,21 +1826,10 @@
 
   /* Everything is done.  We unintern temporary structures which
      interned in bgp_attr_parse(). */
-  if (attr.aspath)
-    aspath_unintern (attr.aspath);
-  if (attr.community)
-    community_unintern (attr.community);
+  bgp_attr_unintern_sub (&attr);
   if (attr.extra)
-    {
-      if (attr.extra->ecommunity)
-        ecommunity_unintern (attr.extra->ecommunity);
-      if (attr.extra->cluster)
-        cluster_unintern (attr.extra->cluster);
-      if (attr.extra->transit)
-        transit_unintern (attr.extra->transit);
-      bgp_attr_extra_free (&attr);
-    }
-
+    bgp_attr_extra_free (&attr);
+  
   /* If peering is stopped due to some reason, do not generate BGP
      event.  */
   if (peer->status != Established)
@@ -1936,7 +1973,7 @@
   /* Check AFI and SAFI. */
   if ((afi != AFI_IP && afi != AFI_IP6)
       || (safi != SAFI_UNICAST && safi != SAFI_MULTICAST
-	  && safi != BGP_SAFI_VPNV4))
+	  && safi != SAFI_MPLS_LABELED_VPN))
     {
       if (BGP_DEBUG (normal, NORMAL))
 	{
@@ -1947,7 +1984,7 @@
     }
 
   /* Adjust safi code. */
-  if (safi == BGP_SAFI_VPNV4)
+  if (safi == SAFI_MPLS_LABELED_VPN)
     safi = SAFI_MPLS_VPN;
 
   if (size != BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE)
@@ -2021,7 +2058,7 @@
 		      break;
 		    }
 		  ok = ((p_end - p_pnt) >= sizeof(u_int32_t)) ;
-		  if (!ok)
+		  if (ok)
 		    {
 		      memcpy (&seq, p_pnt, sizeof (u_int32_t));
                       p_pnt += sizeof (u_int32_t);
diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c
index 5c516f0..ba53032 100644
--- a/bgpd/bgp_route.c
+++ b/bgpd/bgp_route.c
@@ -137,7 +137,7 @@
 bgp_info_free (struct bgp_info *binfo)
 {
   if (binfo->attr)
-    bgp_attr_unintern (binfo->attr);
+    bgp_attr_unintern (&binfo->attr);
   
   bgp_info_extra_free (&binfo->extra);
 
@@ -1069,11 +1069,9 @@
   struct bgp_filter *filter;
   struct bgp_info info;
   struct peer *from;
-  struct bgp *bgp;
 
   from = ri->peer;
   filter = &rsclient->filter[afi][safi];
-  bgp = rsclient->bgp;
 
   if (DISABLE_BGP_ANNOUNCE)
     return 0;
@@ -1493,7 +1491,7 @@
       if (! CHECK_FLAG (old_select->flags, BGP_INFO_ATTR_CHANGED))
         {
           if (CHECK_FLAG (old_select->flags, BGP_INFO_IGP_CHANGED))
-            bgp_zebra_announce (p, old_select, bgp);
+            bgp_zebra_announce (p, old_select, bgp, safi);
           
           UNSET_FLAG (rn->flags, BGP_NODE_PROCESS_SCHEDULED);
           return WQ_SUCCESS;
@@ -1516,20 +1514,20 @@
     }
 
   /* FIB update. */
-  if (safi == SAFI_UNICAST && ! bgp->name &&
-      ! bgp_option_check (BGP_OPT_NO_FIB))
+  if ((safi == SAFI_UNICAST || safi == SAFI_MULTICAST) && (! bgp->name &&
+      ! bgp_option_check (BGP_OPT_NO_FIB)))
     {
       if (new_select 
 	  && new_select->type == ZEBRA_ROUTE_BGP 
 	  && new_select->sub_type == BGP_ROUTE_NORMAL)
-	bgp_zebra_announce (p, new_select, bgp);
+	bgp_zebra_announce (p, new_select, bgp, safi);
       else
 	{
 	  /* Withdraw the route from the kernel. */
 	  if (old_select 
 	      && old_select->type == ZEBRA_ROUTE_BGP
 	      && old_select->sub_type == BGP_ROUTE_NORMAL)
-	    bgp_zebra_withdraw (p, old_select);
+	    bgp_zebra_withdraw (p, old_select, safi);
 	}
     }
     
@@ -1659,7 +1657,7 @@
        u_int8_t ndata[7];
 
        if (safi == SAFI_MPLS_VPN)
-         safi = BGP_SAFI_VPNV4;
+         safi = SAFI_MPLS_LABELED_VPN;
          
        ndata[0] = (afi >>  8);
        ndata[1] = afi;
@@ -1802,23 +1800,23 @@
   /* Apply import policy. */
   if (bgp_import_modifier (rsclient, peer, p, &new_attr, afi, safi) == RMAP_DENY)
     {
-      bgp_attr_unintern (attr_new2);
+      bgp_attr_unintern (&attr_new2);
 
       reason = "import-policy;";
       goto filtered;
     }
 
   attr_new = bgp_attr_intern (&new_attr);
-  bgp_attr_unintern (attr_new2);
+  bgp_attr_unintern (&attr_new2);
 
   /* IPv4 unicast next hop check.  */
-  if (afi == AFI_IP && safi == SAFI_UNICAST)
+  if ((afi == AFI_IP) && ((safi == SAFI_UNICAST) || safi == SAFI_MULTICAST))
     {
-     /* Next hop must not be 0.0.0.0 nor Class E address. */
+     /* Next hop must not be 0.0.0.0 nor Class D/E address. */
       if (new_attr.nexthop.s_addr == 0
-         || ntohl (new_attr.nexthop.s_addr) >= 0xe0000000)
+         || IPV4_CLASS_DE (ntohl (new_attr.nexthop.s_addr)))
        {
-         bgp_attr_unintern (attr_new);
+         bgp_attr_unintern (&attr_new);
 
          reason = "martian next-hop;";
          goto filtered;
@@ -1848,7 +1846,7 @@
                     p->prefixlen, rsclient->host);
 
           bgp_unlock_node (rn);
-          bgp_attr_unintern (attr_new);
+          bgp_attr_unintern (&attr_new);
 
           return;
         }
@@ -1868,7 +1866,7 @@
       bgp_info_set_flag (rn, ri, BGP_INFO_ATTR_CHANGED);
 
       /* Update to new attribute.  */
-      bgp_attr_unintern (ri->attr);
+      bgp_attr_unintern (&ri->attr);
       ri->attr = attr_new;
 
       /* Update MPLS tag.  */
@@ -2063,18 +2061,18 @@
       /* If the peer is EBGP and nexthop is not on connected route,
 	 discard it.  */
       if (peer_sort (peer) == BGP_PEER_EBGP && peer->ttl == 1
-	  && ! bgp_nexthop_check_ebgp (afi, &new_attr)
+	  && ! bgp_nexthop_onlink (afi, &new_attr)
 	  && ! CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK))
 	{
 	  reason = "non-connected next-hop;";
 	  goto filtered;
 	}
 
-      /* Next hop must not be 0.0.0.0 nor Class E address.  Next hop
+      /* Next hop must not be 0.0.0.0 nor Class D/E address. Next hop
 	 must not be my own address.  */
       if (bgp_nexthop_self (afi, &new_attr)
 	  || new_attr.nexthop.s_addr == 0
-	  || ntohl (new_attr.nexthop.s_addr) >= 0xe0000000)
+	  || IPV4_CLASS_DE (ntohl (new_attr.nexthop.s_addr)))
 	{
 	  reason = "martian next-hop;";
 	  goto filtered;
@@ -2128,7 +2126,7 @@
 	    }
 
 	  bgp_unlock_node (rn);
-	  bgp_attr_unintern (attr_new);
+	  bgp_attr_unintern (&attr_new);
 	  bgp_attr_extra_free (&new_attr);
 	  
 	  return 0;
@@ -2175,7 +2173,7 @@
 	}
 	
       /* Update to new attribute.  */
-      bgp_attr_unintern (ri->attr);
+      bgp_attr_unintern (&ri->attr);
       ri->attr = attr_new;
 
       /* Update MPLS tag.  */
@@ -2467,7 +2465,7 @@
     }
   
   bgp_attr_extra_free (&attr);
-  aspath_unintern (aspath);
+  aspath_unintern (&aspath);
 }
 
 static void
@@ -2936,7 +2934,7 @@
 	  if (CHECK_FLAG (ri->flags, BGP_INFO_SELECTED)
 	      && ri->type == ZEBRA_ROUTE_BGP 
 	      && ri->sub_type == BGP_ROUTE_NORMAL)
-	    bgp_zebra_withdraw (&rn->p, ri);
+	    bgp_zebra_withdraw (&rn->p, ri,SAFI_UNICAST);
 
       table = bgp->rib[AFI_IP6][SAFI_UNICAST];
 
@@ -2945,7 +2943,7 @@
 	  if (CHECK_FLAG (ri->flags, BGP_INFO_SELECTED)
 	      && ri->type == ZEBRA_ROUTE_BGP 
 	      && ri->sub_type == BGP_ROUTE_NORMAL)
-	    bgp_zebra_withdraw (&rn->p, ri);
+	    bgp_zebra_withdraw (&rn->p, ri,SAFI_UNICAST);
     }
 }
 
@@ -3215,7 +3213,7 @@
           bgp_attr_flush (&attr_tmp);
 
           /* Unintern original. */
-          aspath_unintern (attr.aspath);
+          aspath_unintern (&attr.aspath);
           bgp_static_withdraw_rsclient (bgp, rsclient, p, afi, safi);
           bgp_attr_extra_free (&attr);
           
@@ -3242,8 +3240,8 @@
 
       bgp->peer_self->rmap_type = 0;
 
-      bgp_attr_unintern (attr_new);
-      aspath_unintern (attr.aspath);
+      bgp_attr_unintern (&attr_new);
+      aspath_unintern (&attr.aspath);
       bgp_attr_extra_free (&attr);
 
       bgp_static_withdraw_rsclient (bgp, rsclient, p, afi, safi);
@@ -3253,7 +3251,7 @@
 
   bgp->peer_self->rmap_type = 0;
 
-  bgp_attr_unintern (attr_new);
+  bgp_attr_unintern (&attr_new);
   attr_new = bgp_attr_intern (&new_attr);
   bgp_attr_extra_free (&new_attr);
 
@@ -3268,8 +3266,8 @@
 	  !CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
         {
           bgp_unlock_node (rn);
-          bgp_attr_unintern (attr_new);
-          aspath_unintern (attr.aspath);
+          bgp_attr_unintern (&attr_new);
+          aspath_unintern (&attr.aspath);
           bgp_attr_extra_free (&attr);
           return;
        }
@@ -3281,14 +3279,14 @@
           /* Rewrite BGP route information. */
 	  if (CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
 	    bgp_info_restore(rn, ri);
-          bgp_attr_unintern (ri->attr);
+          bgp_attr_unintern (&ri->attr);
           ri->attr = attr_new;
           ri->uptime = bgp_clock ();
 
           /* Process change. */
           bgp_process (bgp, rn, afi, safi);
           bgp_unlock_node (rn);
-          aspath_unintern (attr.aspath);
+          aspath_unintern (&attr.aspath);
           bgp_attr_extra_free (&attr);
           return;
         }
@@ -3313,7 +3311,7 @@
   bgp_process (bgp, rn, afi, safi);
 
   /* Unintern original. */
-  aspath_unintern (attr.aspath);
+  aspath_unintern (&attr.aspath);
   bgp_attr_extra_free (&attr);
 }
 
@@ -3363,7 +3361,7 @@
 	  bgp_attr_flush (&attr_tmp);
 
 	  /* Unintern original. */
-	  aspath_unintern (attr.aspath);
+	  aspath_unintern (&attr.aspath);
 	  bgp_attr_extra_free (&attr);
 	  bgp_static_withdraw (bgp, p, afi, safi);
 	  return;
@@ -3384,8 +3382,8 @@
 	  !CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
 	{
 	  bgp_unlock_node (rn);
-	  bgp_attr_unintern (attr_new);
-	  aspath_unintern (attr.aspath);
+	  bgp_attr_unintern (&attr_new);
+	  aspath_unintern (&attr.aspath);
 	  bgp_attr_extra_free (&attr);
 	  return;
 	}
@@ -3399,7 +3397,7 @@
 	    bgp_info_restore(rn, ri);
 	  else
 	    bgp_aggregate_decrement (bgp, p, ri, afi, safi);
-	  bgp_attr_unintern (ri->attr);
+	  bgp_attr_unintern (&ri->attr);
 	  ri->attr = attr_new;
 	  ri->uptime = bgp_clock ();
 
@@ -3407,7 +3405,7 @@
 	  bgp_aggregate_increment (bgp, p, ri, afi, safi);
 	  bgp_process (bgp, rn, afi, safi);
 	  bgp_unlock_node (rn);
-	  aspath_unintern (attr.aspath);
+	  aspath_unintern (&attr.aspath);
 	  bgp_attr_extra_free (&attr);
 	  return;
 	}
@@ -3435,7 +3433,7 @@
   bgp_process (bgp, rn, afi, safi);
 
   /* Unintern original. */
-  aspath_unintern (attr.aspath);
+  aspath_unintern (&attr.aspath);
   bgp_attr_extra_free (&attr);
 }
 
@@ -4169,7 +4167,7 @@
        "Specify a network to announce via BGP\n"
        "IPv6 prefix <network>/<length>\n")
 {
-  return bgp_static_set (vty, vty->index, argv[0], AFI_IP6, SAFI_UNICAST,
+  return bgp_static_set (vty, vty->index, argv[0], AFI_IP6, bgp_node_safi(vty),
                          NULL, 0);
 }
 
@@ -4192,7 +4190,7 @@
        "Specify a network to announce via BGP\n"
        "IPv6 prefix <network>/<length>\n")
 {
-  return bgp_static_unset (vty, vty->index, argv[0], AFI_IP6, SAFI_UNICAST);
+  return bgp_static_unset (vty, vty->index, argv[0], AFI_IP6, bgp_node_safi(vty));
 }
 
 ALIAS (no_ipv6_bgp_network,
@@ -5244,7 +5242,8 @@
 
 /* Redistribute route treatment. */
 void
-bgp_redistribute_add (struct prefix *p, struct in_addr *nexthop,
+bgp_redistribute_add (struct prefix *p, const struct in_addr *nexthop,
+		      const struct in6_addr *nexthop6,
 		      u_int32_t metric, u_char type)
 {
   struct bgp *bgp;
@@ -5264,6 +5263,15 @@
   if (nexthop)
     attr.nexthop = *nexthop;
 
+#ifdef HAVE_IPV6
+  if (nexthop6)
+    {
+      struct attr_extra *extra = bgp_attr_extra_get(&attr);
+      extra->mp_nexthop_global = *nexthop6;
+      extra->mp_nexthop_len = 16;
+    }
+#endif
+
   attr.med = metric;
   attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC);
 
@@ -5299,7 +5307,7 @@
 		  bgp_attr_extra_free (&attr_new);
 		  
 		  /* Unintern original. */
-		  aspath_unintern (attr.aspath);
+		  aspath_unintern (&attr.aspath);
 		  bgp_attr_extra_free (&attr);
 		  bgp_redistribute_delete (p, type);
 		  return;
@@ -5322,8 +5330,8 @@
  	      if (attrhash_cmp (bi->attr, new_attr) &&
 		  !CHECK_FLAG(bi->flags, BGP_INFO_REMOVED))
  		{
- 		  bgp_attr_unintern (new_attr);
- 		  aspath_unintern (attr.aspath);
+ 		  bgp_attr_unintern (&new_attr);
+ 		  aspath_unintern (&attr.aspath);
  		  bgp_attr_extra_free (&attr);
  		  bgp_unlock_node (bn);
  		  return;
@@ -5338,7 +5346,7 @@
 		    bgp_info_restore(bn, bi);
 		  else
 		    bgp_aggregate_decrement (bgp, p, bi, afi, SAFI_UNICAST);
- 		  bgp_attr_unintern (bi->attr);
+ 		  bgp_attr_unintern (&bi->attr);
  		  bi->attr = new_attr;
  		  bi->uptime = bgp_clock ();
  
@@ -5346,7 +5354,7 @@
  		  bgp_aggregate_increment (bgp, p, bi, afi, SAFI_UNICAST);
  		  bgp_process (bgp, bn, afi, SAFI_UNICAST);
  		  bgp_unlock_node (bn);
- 		  aspath_unintern (attr.aspath);
+ 		  aspath_unintern (&attr.aspath);
  		  bgp_attr_extra_free (&attr);
  		  return;
  		} 
@@ -5368,7 +5376,7 @@
     }
 
   /* Unintern original. */
-  aspath_unintern (attr.aspath);
+  aspath_unintern (&attr.aspath);
   bgp_attr_extra_free (&attr);
 }
 
@@ -9379,10 +9387,8 @@
         safi = SAFI_MULTICAST;
       else if (strncmp (safi_str, "u", 1) == 0)
         safi = SAFI_UNICAST;
-      else if (strncmp (safi_str, "vpnv4", 5) == 0)
-        safi = BGP_SAFI_VPNV4;
-      else if (strncmp (safi_str, "vpnv6", 6) == 0)
-        safi = BGP_SAFI_VPNV6;
+      else if (strncmp (safi_str, "vpnv4", 5) == 0 || strncmp (safi_str, "vpnv6", 5) == 0)
+        safi = SAFI_MPLS_LABELED_VPN;
       else
         {
           vty_out (vty, "%% Invalid subsequent address family %s%s",
@@ -9397,13 +9403,6 @@
       return CMD_WARNING;
     }
 
-  if ((afi == AFI_IP && safi ==  BGP_SAFI_VPNV6)
-      || (afi == AFI_IP6 && safi == BGP_SAFI_VPNV4))
-    {
-      vty_out (vty, "%% Invalid subsequent address family %s for %s%s",
-               afi_str, safi_str, VTY_NEWLINE);
-      return CMD_WARNING;
-    }
   return bgp_table_stats (vty, bgp, afi, safi);
 }
 
@@ -12581,6 +12580,9 @@
   install_element (BGP_IPV6_NODE, &no_ipv6_aggregate_address_cmd);
   install_element (BGP_IPV6_NODE, &no_ipv6_aggregate_address_summary_only_cmd);
 
+  install_element (BGP_IPV6M_NODE, &ipv6_bgp_network_cmd);
+  install_element (BGP_IPV6M_NODE, &no_ipv6_bgp_network_cmd);
+
   /* Old config IPv6 BGP commands.  */
   install_element (BGP_NODE, &old_ipv6_bgp_network_cmd);
   install_element (BGP_NODE, &old_no_ipv6_bgp_network_cmd);
diff --git a/bgpd/bgp_route.h b/bgpd/bgp_route.h
index 3e52859..7adc573 100644
--- a/bgpd/bgp_route.h
+++ b/bgpd/bgp_route.h
@@ -192,7 +192,9 @@
 
 extern int bgp_maximum_prefix_overflow (struct peer *, afi_t, safi_t, int);
 
-extern void bgp_redistribute_add (struct prefix *, struct in_addr *, u_int32_t, u_char);
+extern void bgp_redistribute_add (struct prefix *, const struct in_addr *,
+				  const struct in6_addr *,
+				  u_int32_t, u_char);
 extern void bgp_redistribute_delete (struct prefix *, u_char);
 extern void bgp_redistribute_withdraw (struct bgp *, afi_t, int);
 
diff --git a/bgpd/bgp_routemap.c b/bgpd/bgp_routemap.c
index 255b25a..abb85fd 100644
--- a/bgpd/bgp_routemap.c
+++ b/bgpd/bgp_routemap.c
@@ -525,8 +525,13 @@
   char *endptr = NULL;
   unsigned long tmpval;
 
+  /* Metric value shoud be integer. */
+  if (! all_digit (arg))
+    return NULL;
+
+  errno = 0;
   tmpval = strtoul (arg, &endptr, 10);
-  if (*endptr != '\0' || tmpval == ULONG_MAX || tmpval > UINT32_MAX)
+  if (*endptr != '\0' || errno || tmpval > UINT32_MAX)
     return NULL;
     
   med = XMALLOC (MTYPE_ROUTE_MAP_COMPILED, sizeof (u_int32_t));
@@ -790,6 +795,75 @@
   route_match_origin_compile,
   route_match_origin_free
 };
+
+/* match probability  { */
+
+static route_map_result_t
+route_match_probability (void *rule, struct prefix *prefix,
+		    route_map_object_t type, void *object)
+{
+  long r;
+#if _SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE >= 500
+  r = random();
+#else
+  r = (long) rand();
+#endif
+
+  switch (*(unsigned *) rule)
+  {
+    case 0: break;
+    case RAND_MAX: return RMAP_MATCH;
+    default:
+      if (r < *(unsigned *) rule)
+        {
+          return RMAP_MATCH;
+        }
+  }
+
+  return RMAP_NOMATCH;
+}
+
+static void *
+route_match_probability_compile (const char *arg)
+{
+  unsigned *lobule;
+  unsigned  perc;
+
+#if _SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE >= 500
+  srandom (time (NULL));
+#else
+  srand (time (NULL));
+#endif
+
+  perc    = atoi (arg);
+  lobule  = XMALLOC (MTYPE_ROUTE_MAP_COMPILED, sizeof (unsigned));
+
+  switch (perc)
+    {
+      case 0:   *lobule = 0; break;
+      case 100: *lobule = RAND_MAX; break;
+      default:  *lobule = RAND_MAX / 100 * perc;
+    }
+
+  return lobule;
+}
+
+static void
+route_match_probability_free (void *rule)
+{
+  XFREE (MTYPE_ROUTE_MAP_COMPILED, rule);
+}
+
+struct route_map_rule_cmd route_match_probability_cmd =
+{
+  "probability",
+  route_match_probability,
+  route_match_probability_compile,
+  route_match_probability_free
+};
+
+/* } */
+
 /* `set ip next-hop IP_ADDRESS' */
 
 /* Set nexthop to object.  ojbect must be pointer to struct attr. */
@@ -933,8 +1007,9 @@
   if (! all_digit (arg))
     return NULL;
   
+  errno = 0;
   tmp = strtoul (arg, &endptr, 10);
-  if (*endptr != '\0' || tmp == ULONG_MAX || tmp > UINT32_MAX)
+  if (*endptr != '\0' || errno || tmp > UINT32_MAX)
     return NULL;
    
   local_pref = XMALLOC (MTYPE_ROUTE_MAP_COMPILED, sizeof (u_int32_t)); 
@@ -1001,9 +1076,9 @@
   if (! all_digit (arg))
     return NULL;
 
-
+  errno = 0;
   tmp = strtoul (arg, &endptr, 10);
-  if (*endptr != '\0' || tmp == ULONG_MAX || tmp > UINT32_MAX)
+  if (*endptr != '\0' || errno || tmp > UINT32_MAX)
     return NULL;
   
   weight = XMALLOC (MTYPE_ROUTE_MAP_COMPILED, sizeof (u_int32_t));
@@ -1092,8 +1167,9 @@
   if (all_digit (arg))
     {
       /* set metric value check*/
+      errno = 0;
       larg = strtoul (arg, &endptr, 10);
-      if (*endptr != '\0' || larg == ULONG_MAX || larg > UINT32_MAX)
+      if (*endptr != '\0' || errno || larg > UINT32_MAX)
         return NULL;
       metric = larg;
     }
@@ -1105,8 +1181,9 @@
 	   || (! all_digit (arg+1)))
 	return NULL;
 
+      errno = 0;
       larg = strtoul (arg+1, &endptr, 10);
-      if (*endptr != '\0' || larg == ULONG_MAX || larg > UINT32_MAX)
+      if (*endptr != '\0' || errno || larg > UINT32_MAX)
 	return NULL;
       metric = larg;
     }
@@ -1482,10 +1559,10 @@
       else
 	new_ecom = ecommunity_dup (ecom);
 
-      bgp_info->attr->extra->ecommunity = new_ecom;
+      bgp_info->attr->extra->ecommunity = ecommunity_intern (new_ecom);
 
       if (old_ecom)
-	ecommunity_free (old_ecom);
+	ecommunity_unintern (&old_ecom);
 
       bgp_info->attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_EXT_COMMUNITIES);
     }
@@ -1501,7 +1578,7 @@
   ecom = ecommunity_str2com (arg, ECOMMUNITY_ROUTE_TARGET, 0);
   if (! ecom)
     return NULL;
-  return ecom;
+  return ecommunity_intern (ecom);
 }
 
 /* Free function for set community. */
@@ -1509,7 +1586,7 @@
 route_set_ecommunity_rt_free (void *rule)
 {
   struct ecommunity *ecom = rule;
-  ecommunity_free (ecom);
+  ecommunity_unintern (&ecom);
 }
 
 /* Set community rule structure. */
@@ -1528,7 +1605,7 @@
 route_set_ecommunity_soo (void *rule, struct prefix *prefix, 
 			 route_map_object_t type, void *object)
 {
-  struct ecommunity *ecom;
+  struct ecommunity *ecom, *old_ecom, *new_ecom;
   struct bgp_info *bgp_info;
 
   if (type == RMAP_BGP)
@@ -1539,8 +1616,19 @@
       if (! ecom)
 	return RMAP_OKAY;
     
+      old_ecom = (bgp_attr_extra_get (bgp_info->attr))->ecommunity;
+      
+      if (old_ecom)
+	new_ecom = ecommunity_merge (ecommunity_dup (old_ecom), ecom);
+      else
+	new_ecom = ecommunity_dup (ecom);
+
+      bgp_info->attr->extra->ecommunity = ecommunity_intern (new_ecom);
+
+      if (old_ecom)
+	ecommunity_unintern (&old_ecom);
+
       bgp_info->attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_EXT_COMMUNITIES);
-      (bgp_attr_extra_get (bgp_info->attr))->ecommunity = ecommunity_dup (ecom);
     }
   return RMAP_OKAY;
 }
@@ -1555,7 +1643,7 @@
   if (! ecom)
     return NULL;
   
-  return ecom;
+  return ecommunity_intern (ecom);
 }
 
 /* Free function for set community. */
@@ -1563,7 +1651,7 @@
 route_set_ecommunity_soo_free (void *rule)
 {
   struct ecommunity *ecom = rule;
-  ecommunity_free (ecom);
+  ecommunity_unintern (&ecom);
 }
 
 /* Set community rule structure. */
@@ -2451,6 +2539,38 @@
        "IP access-list number (expanded range)\n"
        "IP Access-list name\n")
 
+/* match probability { */
+
+DEFUN (match_probability,
+       match_probability_cmd,
+       "match probability <0-100>",
+       MATCH_STR
+       "Match portion of routes defined by percentage value\n"
+       "Percentage of routes\n")
+{
+  return bgp_route_match_add (vty, vty->index, "probability", argv[0]);
+}
+
+DEFUN (no_match_probability,
+       no_match_probability_cmd,
+       "no match probability",
+       NO_STR
+       MATCH_STR
+       "Match portion of routes defined by percentage value\n")
+{
+  return bgp_route_match_delete (vty, vty->index, "probability", argc ? argv[0] : NULL);
+}
+
+ALIAS (no_match_probability,
+       no_match_probability_val_cmd,
+       "no match probability <1-99>",
+       NO_STR
+       MATCH_STR
+       "Match portion of routes defined by percentage value\n"
+       "Percentage of routes\n")
+
+/* } */
+
 DEFUN (match_ip_route_source, 
        match_ip_route_source_cmd,
        "match ip route-source (<1-199>|<1300-2699>|WORD)",
@@ -3738,6 +3858,7 @@
   route_map_install_match (&route_match_ecommunity_cmd);
   route_map_install_match (&route_match_metric_cmd);
   route_map_install_match (&route_match_origin_cmd);
+  route_map_install_match (&route_match_probability_cmd);
 
   route_map_install_set (&route_set_ip_nexthop_cmd);
   route_map_install_set (&route_set_local_pref_cmd);
@@ -3769,7 +3890,6 @@
   install_element (RMAP_NODE, &match_ip_route_source_cmd);
   install_element (RMAP_NODE, &no_match_ip_route_source_cmd);
   install_element (RMAP_NODE, &no_match_ip_route_source_val_cmd);
-
   install_element (RMAP_NODE, &match_ip_address_prefix_list_cmd);
   install_element (RMAP_NODE, &no_match_ip_address_prefix_list_cmd);
   install_element (RMAP_NODE, &no_match_ip_address_prefix_list_val_cmd);
@@ -3797,6 +3917,9 @@
   install_element (RMAP_NODE, &match_origin_cmd);
   install_element (RMAP_NODE, &no_match_origin_cmd);
   install_element (RMAP_NODE, &no_match_origin_val_cmd);
+  install_element (RMAP_NODE, &match_probability_cmd);
+  install_element (RMAP_NODE, &no_match_probability_cmd);
+  install_element (RMAP_NODE, &no_match_probability_val_cmd);
 
   install_element (RMAP_NODE, &set_ip_nexthop_cmd);
   install_element (RMAP_NODE, &set_ip_nexthop_peer_cmd);
diff --git a/bgpd/bgp_vty.c b/bgpd/bgp_vty.c
index e7e7dba..f65bb15 100644
--- a/bgpd/bgp_vty.c
+++ b/bgpd/bgp_vty.c
@@ -3707,7 +3707,7 @@
   if (! peer)
     return CMD_WARNING;
 
-  VTY_GET_INTEGER ("maxmum number", max, num_str);
+  VTY_GET_INTEGER ("maximum number", max, num_str);
   if (threshold_str)
     threshold = atoi (threshold_str);
   else
@@ -4215,18 +4215,10 @@
   if (sort == clear_as)
     {
       as_t as;
-      unsigned long as_ul;
       int find = 0;
 
-      VTY_GET_LONG ("AS", as_ul, arg);
+      VTY_GET_INTEGER_RANGE ("AS", as, arg, 1, BGP_AS4_MAX);
       
-      if (!as_ul)
-	{
-	  vty_out (vty, "Invalid AS number%s", VTY_NEWLINE); 
-	  return CMD_WARNING;
-	}
-      as = (as_t) as_ul;
-
       for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
 	{
 	  if (peer->as != as) 
@@ -8381,57 +8373,16 @@
 
 /* Redistribute VTY commands.  */
 
-/* Utility function to convert user input route type string to route
-   type.  */
-static int
-bgp_str2route_type (int afi, const char *str)
-{
-  if (! str)
-    return 0;
-
-  if (afi == AFI_IP)
-    {
-      if (strncmp (str, "k", 1) == 0)
-	return ZEBRA_ROUTE_KERNEL;
-      else if (strncmp (str, "c", 1) == 0)
-	return ZEBRA_ROUTE_CONNECT;
-      else if (strncmp (str, "s", 1) == 0)
-	return ZEBRA_ROUTE_STATIC;
-      else if (strncmp (str, "r", 1) == 0)
-	return ZEBRA_ROUTE_RIP;
-      else if (strncmp (str, "o", 1) == 0)
-	return ZEBRA_ROUTE_OSPF;
-    }
-  if (afi == AFI_IP6)
-    {
-      if (strncmp (str, "k", 1) == 0)
-	return ZEBRA_ROUTE_KERNEL;
-      else if (strncmp (str, "c", 1) == 0)
-	return ZEBRA_ROUTE_CONNECT;
-      else if (strncmp (str, "s", 1) == 0)
-	return ZEBRA_ROUTE_STATIC;
-      else if (strncmp (str, "r", 1) == 0)
-	return ZEBRA_ROUTE_RIPNG;
-      else if (strncmp (str, "o", 1) == 0)
-	return ZEBRA_ROUTE_OSPF6;
-    }
-  return 0;
-}
-
 DEFUN (bgp_redistribute_ipv4,
        bgp_redistribute_ipv4_cmd,
-       "redistribute (connected|kernel|ospf|rip|static)",
+       "redistribute " QUAGGA_IP_REDIST_STR_BGPD,
        "Redistribute information from another routing protocol\n"
-       "Connected\n"
-       "Kernel routes\n"
-       "Open Shurtest Path First (OSPF)\n"
-       "Routing Information Protocol (RIP)\n"
-       "Static routes\n")
+       QUAGGA_IP_REDIST_HELP_STR_BGPD)
 {
   int type;
 
-  type = bgp_str2route_type (AFI_IP, argv[0]);
-  if (! type)
+  type = proto_redistnum (AFI_IP, argv[0]);
+  if (type < 0 || type == ZEBRA_ROUTE_BGP)
     {
       vty_out (vty, "%% Invalid route type%s", VTY_NEWLINE);
       return CMD_WARNING;
@@ -8441,20 +8392,16 @@
 
 DEFUN (bgp_redistribute_ipv4_rmap,
        bgp_redistribute_ipv4_rmap_cmd,
-       "redistribute (connected|kernel|ospf|rip|static) route-map WORD",
+       "redistribute " QUAGGA_IP_REDIST_STR_BGPD " route-map WORD",
        "Redistribute information from another routing protocol\n"
-       "Connected\n"
-       "Kernel routes\n"
-       "Open Shurtest Path First (OSPF)\n"
-       "Routing Information Protocol (RIP)\n"
-       "Static routes\n"
+       QUAGGA_IP_REDIST_HELP_STR_BGPD
        "Route map reference\n"
        "Pointer to route-map entries\n")
 {
   int type;
 
-  type = bgp_str2route_type (AFI_IP, argv[0]);
-  if (! type)
+  type = proto_redistnum (AFI_IP, argv[0]);
+  if (type < 0 || type == ZEBRA_ROUTE_BGP)
     {
       vty_out (vty, "%% Invalid route type%s", VTY_NEWLINE);
       return CMD_WARNING;
@@ -8466,21 +8413,17 @@
 
 DEFUN (bgp_redistribute_ipv4_metric,
        bgp_redistribute_ipv4_metric_cmd,
-       "redistribute (connected|kernel|ospf|rip|static) metric <0-4294967295>",
+       "redistribute " QUAGGA_IP_REDIST_STR_BGPD " metric <0-4294967295>",
        "Redistribute information from another routing protocol\n"
-       "Connected\n"
-       "Kernel routes\n"
-       "Open Shurtest Path First (OSPF)\n"
-       "Routing Information Protocol (RIP)\n"
-       "Static routes\n"
+       QUAGGA_IP_REDIST_HELP_STR_BGPD
        "Metric for redistributed routes\n"
        "Default metric\n")
 {
   int type;
   u_int32_t metric;
 
-  type = bgp_str2route_type (AFI_IP, argv[0]);
-  if (! type)
+  type = proto_redistnum (AFI_IP, argv[0]);
+  if (type < 0 || type == ZEBRA_ROUTE_BGP)
     {
       vty_out (vty, "%% Invalid route type%s", VTY_NEWLINE);
       return CMD_WARNING;
@@ -8493,13 +8436,9 @@
 
 DEFUN (bgp_redistribute_ipv4_rmap_metric,
        bgp_redistribute_ipv4_rmap_metric_cmd,
-       "redistribute (connected|kernel|ospf|rip|static) route-map WORD metric <0-4294967295>",
+       "redistribute " QUAGGA_IP_REDIST_STR_BGPD " route-map WORD metric <0-4294967295>",
        "Redistribute information from another routing protocol\n"
-       "Connected\n"
-       "Kernel routes\n"
-       "Open Shurtest Path First (OSPF)\n"
-       "Routing Information Protocol (RIP)\n"
-       "Static routes\n"
+       QUAGGA_IP_REDIST_HELP_STR_BGPD
        "Route map reference\n"
        "Pointer to route-map entries\n"
        "Metric for redistributed routes\n"
@@ -8508,8 +8447,8 @@
   int type;
   u_int32_t metric;
 
-  type = bgp_str2route_type (AFI_IP, argv[0]);
-  if (! type)
+  type = proto_redistnum (AFI_IP, argv[0]);
+  if (type < 0 || type == ZEBRA_ROUTE_BGP)
     {
       vty_out (vty, "%% Invalid route type%s", VTY_NEWLINE);
       return CMD_WARNING;
@@ -8523,13 +8462,9 @@
 
 DEFUN (bgp_redistribute_ipv4_metric_rmap,
        bgp_redistribute_ipv4_metric_rmap_cmd,
-       "redistribute (connected|kernel|ospf|rip|static) metric <0-4294967295> route-map WORD",
+       "redistribute " QUAGGA_IP_REDIST_STR_BGPD " metric <0-4294967295> route-map WORD",
        "Redistribute information from another routing protocol\n"
-       "Connected\n"
-       "Kernel routes\n"
-       "Open Shurtest Path First (OSPF)\n"
-       "Routing Information Protocol (RIP)\n"
-       "Static routes\n"
+       QUAGGA_IP_REDIST_HELP_STR_BGPD
        "Metric for redistributed routes\n"
        "Default metric\n"
        "Route map reference\n"
@@ -8538,8 +8473,8 @@
   int type;
   u_int32_t metric;
 
-  type = bgp_str2route_type (AFI_IP, argv[0]);
-  if (! type)
+  type = proto_redistnum (AFI_IP, argv[0]);
+  if (type < 0 || type == ZEBRA_ROUTE_BGP)
     {
       vty_out (vty, "%% Invalid route type%s", VTY_NEWLINE);
       return CMD_WARNING;
@@ -8553,19 +8488,15 @@
 
 DEFUN (no_bgp_redistribute_ipv4,
        no_bgp_redistribute_ipv4_cmd,
-       "no redistribute (connected|kernel|ospf|rip|static)",
+       "no redistribute " QUAGGA_IP_REDIST_STR_BGPD,
        NO_STR
        "Redistribute information from another routing protocol\n"
-       "Connected\n"
-       "Kernel routes\n"
-       "Open Shurtest Path First (OSPF)\n"
-       "Routing Information Protocol (RIP)\n"
-       "Static routes\n")
+       QUAGGA_IP_REDIST_HELP_STR_BGPD)
 {
   int type;
 
-  type = bgp_str2route_type (AFI_IP, argv[0]);
-  if (! type)
+  type = proto_redistnum (AFI_IP, argv[0]);
+  if (type < 0 || type == ZEBRA_ROUTE_BGP)
     {
       vty_out (vty, "%% Invalid route type%s", VTY_NEWLINE);
       return CMD_WARNING;
@@ -8576,21 +8507,17 @@
 
 DEFUN (no_bgp_redistribute_ipv4_rmap,
        no_bgp_redistribute_ipv4_rmap_cmd,
-       "no redistribute (connected|kernel|ospf|rip|static) route-map WORD",
+       "no redistribute " QUAGGA_IP_REDIST_STR_BGPD " route-map WORD",
        NO_STR
        "Redistribute information from another routing protocol\n"
-       "Connected\n"
-       "Kernel routes\n"
-       "Open Shurtest Path First (OSPF)\n"
-       "Routing Information Protocol (RIP)\n"
-       "Static routes\n"
+       QUAGGA_IP_REDIST_HELP_STR_BGPD
        "Route map reference\n"
        "Pointer to route-map entries\n")
 {
   int type;
 
-  type = bgp_str2route_type (AFI_IP, argv[0]);
-  if (! type)
+  type = proto_redistnum (AFI_IP, argv[0]);
+  if (type < 0 || type == ZEBRA_ROUTE_BGP)
     {
       vty_out (vty, "%% Invalid route type%s", VTY_NEWLINE);
       return CMD_WARNING;
@@ -8602,21 +8529,17 @@
 
 DEFUN (no_bgp_redistribute_ipv4_metric,
        no_bgp_redistribute_ipv4_metric_cmd,
-       "no redistribute (connected|kernel|ospf|rip|static) metric <0-4294967295>",
+       "no redistribute " QUAGGA_IP_REDIST_STR_BGPD " metric <0-4294967295>",
        NO_STR
        "Redistribute information from another routing protocol\n"
-       "Connected\n"
-       "Kernel routes\n"
-       "Open Shurtest Path First (OSPF)\n"
-       "Routing Information Protocol (RIP)\n"
-       "Static routes\n"
+       QUAGGA_IP_REDIST_HELP_STR_BGPD
        "Metric for redistributed routes\n"
        "Default metric\n")
 {
   int type;
 
-  type = bgp_str2route_type (AFI_IP, argv[0]);
-  if (! type)
+  type = proto_redistnum (AFI_IP, argv[0]);
+  if (type < 0 || type == ZEBRA_ROUTE_BGP)
     {
       vty_out (vty, "%% Invalid route type%s", VTY_NEWLINE);
       return CMD_WARNING;
@@ -8628,14 +8551,10 @@
 
 DEFUN (no_bgp_redistribute_ipv4_rmap_metric,
        no_bgp_redistribute_ipv4_rmap_metric_cmd,
-       "no redistribute (connected|kernel|ospf|rip|static) route-map WORD metric <0-4294967295>",
+       "no redistribute " QUAGGA_IP_REDIST_STR_BGPD " route-map WORD metric <0-4294967295>",
        NO_STR
        "Redistribute information from another routing protocol\n"
-       "Connected\n"
-       "Kernel routes\n"
-       "Open Shurtest Path First (OSPF)\n"
-       "Routing Information Protocol (RIP)\n"
-       "Static routes\n"
+       QUAGGA_IP_REDIST_HELP_STR_BGPD
        "Route map reference\n"
        "Pointer to route-map entries\n"
        "Metric for redistributed routes\n"
@@ -8643,8 +8562,8 @@
 {
   int type;
 
-  type = bgp_str2route_type (AFI_IP, argv[0]);
-  if (! type)
+  type = proto_redistnum (AFI_IP, argv[0]);
+  if (type < 0 || type == ZEBRA_ROUTE_BGP)
     {
       vty_out (vty, "%% Invalid route type%s", VTY_NEWLINE);
       return CMD_WARNING;
@@ -8657,14 +8576,10 @@
 
 ALIAS (no_bgp_redistribute_ipv4_rmap_metric,
        no_bgp_redistribute_ipv4_metric_rmap_cmd,
-       "no redistribute (connected|kernel|ospf|rip|static) metric <0-4294967295> route-map WORD",
+       "no redistribute " QUAGGA_IP_REDIST_STR_BGPD " metric <0-4294967295> route-map WORD",
        NO_STR
        "Redistribute information from another routing protocol\n"
-       "Connected\n"
-       "Kernel routes\n"
-       "Open Shurtest Path First (OSPF)\n"
-       "Routing Information Protocol (RIP)\n"
-       "Static routes\n"
+       QUAGGA_IP_REDIST_HELP_STR_BGPD
        "Metric for redistributed routes\n"
        "Default metric\n"
        "Route map reference\n"
@@ -8673,18 +8588,14 @@
 #ifdef HAVE_IPV6
 DEFUN (bgp_redistribute_ipv6,
        bgp_redistribute_ipv6_cmd,
-       "redistribute (connected|kernel|ospf6|ripng|static)",
+       "redistribute " QUAGGA_IP6_REDIST_STR_BGPD,
        "Redistribute information from another routing protocol\n"
-       "Connected\n"
-       "Kernel routes\n"
-       "Open Shurtest Path First (OSPFv3)\n"
-       "Routing Information Protocol (RIPng)\n"
-       "Static routes\n")
+       QUAGGA_IP6_REDIST_HELP_STR_BGPD)
 {
   int type;
 
-  type = bgp_str2route_type (AFI_IP6, argv[0]);
-  if (! type)
+  type = proto_redistnum (AFI_IP6, argv[0]);
+  if (type < 0 || type == ZEBRA_ROUTE_BGP)
     {
       vty_out (vty, "%% Invalid route type%s", VTY_NEWLINE);
       return CMD_WARNING;
@@ -8695,20 +8606,16 @@
 
 DEFUN (bgp_redistribute_ipv6_rmap,
        bgp_redistribute_ipv6_rmap_cmd,
-       "redistribute (connected|kernel|ospf6|ripng|static) route-map WORD",
+       "redistribute " QUAGGA_IP6_REDIST_STR_BGPD " route-map WORD",
        "Redistribute information from another routing protocol\n"
-       "Connected\n"
-       "Kernel routes\n"
-       "Open Shurtest Path First (OSPFv3)\n"
-       "Routing Information Protocol (RIPng)\n"
-       "Static routes\n"
+       QUAGGA_IP6_REDIST_HELP_STR_BGPD
        "Route map reference\n"
        "Pointer to route-map entries\n")
 {
   int type;
 
-  type = bgp_str2route_type (AFI_IP6, argv[0]);
-  if (! type)
+  type = proto_redistnum (AFI_IP6, argv[0]);
+  if (type < 0 || type == ZEBRA_ROUTE_BGP)
     {
       vty_out (vty, "%% Invalid route type%s", VTY_NEWLINE);
       return CMD_WARNING;
@@ -8720,21 +8627,17 @@
 
 DEFUN (bgp_redistribute_ipv6_metric,
        bgp_redistribute_ipv6_metric_cmd,
-       "redistribute (connected|kernel|ospf6|ripng|static) metric <0-4294967295>",
+       "redistribute " QUAGGA_IP6_REDIST_STR_BGPD " metric <0-4294967295>",
        "Redistribute information from another routing protocol\n"
-       "Connected\n"
-       "Kernel routes\n"
-       "Open Shurtest Path First (OSPFv3)\n"
-       "Routing Information Protocol (RIPng)\n"
-       "Static routes\n"
+       QUAGGA_IP6_REDIST_HELP_STR_BGPD
        "Metric for redistributed routes\n"
        "Default metric\n")
 {
   int type;
   u_int32_t metric;
 
-  type = bgp_str2route_type (AFI_IP6, argv[0]);
-  if (! type)
+  type = proto_redistnum (AFI_IP6, argv[0]);
+  if (type < 0 || type == ZEBRA_ROUTE_BGP)
     {
       vty_out (vty, "%% Invalid route type%s", VTY_NEWLINE);
       return CMD_WARNING;
@@ -8747,13 +8650,9 @@
 
 DEFUN (bgp_redistribute_ipv6_rmap_metric,
        bgp_redistribute_ipv6_rmap_metric_cmd,
-       "redistribute (connected|kernel|ospf6|ripng|static) route-map WORD metric <0-4294967295>",
+       "redistribute " QUAGGA_IP6_REDIST_STR_BGPD " route-map WORD metric <0-4294967295>",
        "Redistribute information from another routing protocol\n"
-       "Connected\n"
-       "Kernel routes\n"
-       "Open Shurtest Path First (OSPFv3)\n"
-       "Routing Information Protocol (RIPng)\n"
-       "Static routes\n"
+       QUAGGA_IP6_REDIST_HELP_STR_BGPD
        "Route map reference\n"
        "Pointer to route-map entries\n"
        "Metric for redistributed routes\n"
@@ -8762,8 +8661,8 @@
   int type;
   u_int32_t metric;
 
-  type = bgp_str2route_type (AFI_IP6, argv[0]);
-  if (! type)
+  type = proto_redistnum (AFI_IP6, argv[0]);
+  if (type < 0 || type == ZEBRA_ROUTE_BGP)
     {
       vty_out (vty, "%% Invalid route type%s", VTY_NEWLINE);
       return CMD_WARNING;
@@ -8777,13 +8676,9 @@
 
 DEFUN (bgp_redistribute_ipv6_metric_rmap,
        bgp_redistribute_ipv6_metric_rmap_cmd,
-       "redistribute (connected|kernel|ospf6|ripng|static) metric <0-4294967295> route-map WORD",
+       "redistribute " QUAGGA_IP6_REDIST_STR_BGPD " metric <0-4294967295> route-map WORD",
        "Redistribute information from another routing protocol\n"
-       "Connected\n"
-       "Kernel routes\n"
-       "Open Shurtest Path First (OSPFv3)\n"
-       "Routing Information Protocol (RIPng)\n"
-       "Static routes\n"
+       QUAGGA_IP6_REDIST_HELP_STR_BGPD
        "Metric for redistributed routes\n"
        "Default metric\n"
        "Route map reference\n"
@@ -8792,8 +8687,8 @@
   int type;
   u_int32_t metric;
 
-  type = bgp_str2route_type (AFI_IP6, argv[0]);
-  if (! type)
+  type = proto_redistnum (AFI_IP6, argv[0]);
+  if (type < 0 || type == ZEBRA_ROUTE_BGP)
     {
       vty_out (vty, "%% Invalid route type%s", VTY_NEWLINE);
       return CMD_WARNING;
@@ -8807,19 +8702,15 @@
 
 DEFUN (no_bgp_redistribute_ipv6,
        no_bgp_redistribute_ipv6_cmd,
-       "no redistribute (connected|kernel|ospf6|ripng|static)",
+       "no redistribute " QUAGGA_IP6_REDIST_STR_BGPD,
        NO_STR
        "Redistribute information from another routing protocol\n"
-       "Connected\n"
-       "Kernel routes\n"
-       "Open Shurtest Path First (OSPFv3)\n"
-       "Routing Information Protocol (RIPng)\n"
-       "Static routes\n")
+       QUAGGA_IP6_REDIST_HELP_STR_BGPD)
 {
   int type;
 
-  type = bgp_str2route_type (AFI_IP6, argv[0]);
-  if (! type)
+  type = proto_redistnum (AFI_IP6, argv[0]);
+  if (type < 0 || type == ZEBRA_ROUTE_BGP)
     {
       vty_out (vty, "%% Invalid route type%s", VTY_NEWLINE);
       return CMD_WARNING;
@@ -8830,21 +8721,17 @@
 
 DEFUN (no_bgp_redistribute_ipv6_rmap,
        no_bgp_redistribute_ipv6_rmap_cmd,
-       "no redistribute (connected|kernel|ospf6|ripng|static) route-map WORD",
+       "no redistribute " QUAGGA_IP6_REDIST_STR_BGPD " route-map WORD",
        NO_STR
        "Redistribute information from another routing protocol\n"
-       "Connected\n"
-       "Kernel routes\n"
-       "Open Shurtest Path First (OSPFv3)\n"
-       "Routing Information Protocol (RIPng)\n"
-       "Static routes\n"
+       QUAGGA_IP6_REDIST_HELP_STR_BGPD
        "Route map reference\n"
        "Pointer to route-map entries\n")
 {
   int type;
 
-  type = bgp_str2route_type (AFI_IP6, argv[0]);
-  if (! type)
+  type = proto_redistnum (AFI_IP6, argv[0]);
+  if (type < 0 || type == ZEBRA_ROUTE_BGP)
     {
       vty_out (vty, "%% Invalid route type%s", VTY_NEWLINE);
       return CMD_WARNING;
@@ -8856,21 +8743,17 @@
 
 DEFUN (no_bgp_redistribute_ipv6_metric,
        no_bgp_redistribute_ipv6_metric_cmd,
-       "no redistribute (connected|kernel|ospf6|ripng|static) metric <0-4294967295>",
+       "no redistribute " QUAGGA_IP6_REDIST_STR_BGPD " metric <0-4294967295>",
        NO_STR
        "Redistribute information from another routing protocol\n"
-       "Connected\n"
-       "Kernel routes\n"
-       "Open Shurtest Path First (OSPFv3)\n"
-       "Routing Information Protocol (RIPng)\n"
-       "Static routes\n"
+       QUAGGA_IP6_REDIST_HELP_STR_BGPD
        "Metric for redistributed routes\n"
        "Default metric\n")
 {
   int type;
 
-  type = bgp_str2route_type (AFI_IP6, argv[0]);
-  if (! type)
+  type = proto_redistnum (AFI_IP6, argv[0]);
+  if (type < 0 || type == ZEBRA_ROUTE_BGP)
     {
       vty_out (vty, "%% Invalid route type%s", VTY_NEWLINE);
       return CMD_WARNING;
@@ -8882,14 +8765,10 @@
 
 DEFUN (no_bgp_redistribute_ipv6_rmap_metric,
        no_bgp_redistribute_ipv6_rmap_metric_cmd,
-       "no redistribute (connected|kernel|ospf6|ripng|static) route-map WORD metric <0-4294967295>",
+       "no redistribute " QUAGGA_IP6_REDIST_STR_BGPD " route-map WORD metric <0-4294967295>",
        NO_STR
        "Redistribute information from another routing protocol\n"
-       "Connected\n"
-       "Kernel routes\n"
-       "Open Shurtest Path First (OSPFv3)\n"
-       "Routing Information Protocol (RIPng)\n"
-       "Static routes\n"
+       QUAGGA_IP6_REDIST_HELP_STR_BGPD
        "Route map reference\n"
        "Pointer to route-map entries\n"
        "Metric for redistributed routes\n"
@@ -8897,8 +8776,8 @@
 {
   int type;
 
-  type = bgp_str2route_type (AFI_IP6, argv[0]);
-  if (! type)
+  type = proto_redistnum (AFI_IP6, argv[0]);
+  if (type < 0 || type == ZEBRA_ROUTE_BGP)
     {
       vty_out (vty, "%% Invalid route type%s", VTY_NEWLINE);
       return CMD_WARNING;
@@ -8911,14 +8790,10 @@
 
 ALIAS (no_bgp_redistribute_ipv6_rmap_metric,
        no_bgp_redistribute_ipv6_metric_rmap_cmd,
-       "no redistribute (connected|kernel|ospf6|ripng|static) metric <0-4294967295> route-map WORD",
+       "no redistribute " QUAGGA_IP6_REDIST_STR_BGPD " metric <0-4294967295> route-map WORD",
        NO_STR
        "Redistribute information from another routing protocol\n"
-       "Connected\n"
-       "Kernel routes\n"
-       "Open Shurtest Path First (OSPFv3)\n"
-       "Routing Information Protocol (RIPng)\n"
-       "Static routes\n"
+       QUAGGA_IP6_REDIST_HELP_STR_BGPD
        "Metric for redistributed routes\n"
        "Default metric\n"
        "Route map reference\n"
diff --git a/bgpd/bgp_zebra.c b/bgpd/bgp_zebra.c
index f3baeee..20feba0 100644
--- a/bgpd/bgp_zebra.c
+++ b/bgpd/bgp_zebra.c
@@ -232,12 +232,10 @@
 {
   struct stream *s;
   struct zapi_ipv4 api;
-  unsigned long ifindex;
   struct in_addr nexthop;
   struct prefix_ipv4 p;
 
   s = zclient->ibuf;
-  ifindex = 0;
   nexthop.s_addr = 0;
 
   /* Type, flags, message. */
@@ -260,7 +258,7 @@
   if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
     {
       api.ifindex_num = stream_getc (s);
-      ifindex = stream_getl (s);
+      stream_getl (s); /* ifindex, unused */
     }
   if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
     api.distance = stream_getc (s);
@@ -281,7 +279,8 @@
 		     inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])),
 		     api.metric);
 	}
-      bgp_redistribute_add((struct prefix *)&p, &nexthop, api.metric, api.type);
+      bgp_redistribute_add((struct prefix *)&p, &nexthop, NULL,
+			   api.metric, api.type);
     }
   else
     {
@@ -309,12 +308,10 @@
 {
   struct stream *s;
   struct zapi_ipv6 api;
-  unsigned long ifindex;
   struct in6_addr nexthop;
   struct prefix_ipv6 p;
 
   s = zclient->ibuf;
-  ifindex = 0;
   memset (&nexthop, 0, sizeof (struct in6_addr));
 
   /* Type, flags, message. */
@@ -337,7 +334,7 @@
   if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
     {
       api.ifindex_num = stream_getc (s);
-      ifindex = stream_getl (s);
+      stream_getl (s); /* ifindex, unused */
     }
   if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
     api.distance = stream_getc (s);
@@ -356,23 +353,29 @@
     {
       if (BGP_DEBUG(zebra, ZEBRA))
 	{
-	  char buf[INET6_ADDRSTRLEN];
-	  zlog_debug("Zebra rcvd: IPv6 route add %s %s/%d metric %u",
+	  char buf[2][INET6_ADDRSTRLEN];
+	  zlog_debug("Zebra rcvd: IPv6 route add %s %s/%d nexthop %s metric %u",
 		     zebra_route_string(api.type),
-		     inet_ntop(AF_INET6, &p.prefix, buf, sizeof(buf)),
-		     p.prefixlen, api.metric);
+		     inet_ntop(AF_INET6, &p.prefix, buf[0], sizeof(buf[0])),
+		     p.prefixlen,
+		     inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])),
+		     api.metric);
 	}
-      bgp_redistribute_add ((struct prefix *)&p, NULL, api.metric, api.type);
+      bgp_redistribute_add ((struct prefix *)&p, NULL, &nexthop,
+			    api.metric, api.type);
     }
   else
     {
       if (BGP_DEBUG(zebra, ZEBRA))
 	{
-	  char buf[INET6_ADDRSTRLEN];
-	  zlog_debug("Zebra rcvd: IPv6 route delete %s %s/%d metric %u",
+	  char buf[2][INET6_ADDRSTRLEN];
+	  zlog_debug("Zebra rcvd: IPv6 route delete %s %s/%d "
+		     "nexthop %s metric %u",
 		     zebra_route_string(api.type),
-		     inet_ntop(AF_INET6, &p.prefix, buf, sizeof(buf)),
-		     p.prefixlen, api.metric);
+		     inet_ntop(AF_INET6, &p.prefix, buf[0], sizeof(buf[0])),
+		     p.prefixlen,
+		     inet_ntop(AF_INET6, &nexthop, buf[1], sizeof(buf[1])),
+		     api.metric);
 	}
       bgp_redistribute_delete ((struct prefix *) &p, api.type);
     }
@@ -640,7 +643,7 @@
 }
 
 void
-bgp_zebra_announce (struct prefix *p, struct bgp_info *info, struct bgp *bgp)
+bgp_zebra_announce (struct prefix *p, struct bgp_info *info, struct bgp *bgp, safi_t safi)
 {
   int flags;
   u_char distance;
@@ -675,6 +678,7 @@
 
       api.type = ZEBRA_ROUTE_BGP;
       api.message = 0;
+      api.safi = safi;
       SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
       api.nexthop_num = 1;
       api.nexthop = &nexthop;
@@ -749,6 +753,7 @@
       api.flags = flags;
       api.type = ZEBRA_ROUTE_BGP;
       api.message = 0;
+      api.safi = safi;
       SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
       api.nexthop_num = 1;
       api.nexthop = &nexthop;
@@ -775,7 +780,7 @@
 }
 
 void
-bgp_zebra_withdraw (struct prefix *p, struct bgp_info *info)
+bgp_zebra_withdraw (struct prefix *p, struct bgp_info *info, safi_t safi)
 {
   int flags;
   struct peer *peer;
@@ -809,6 +814,7 @@
 
       api.type = ZEBRA_ROUTE_BGP;
       api.message = 0;
+      api.safi = safi;
       SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
       api.nexthop_num = 1;
       api.nexthop = &nexthop;
@@ -864,6 +870,7 @@
       api.flags = flags;
       api.type = ZEBRA_ROUTE_BGP;
       api.message = 0;
+      api.safi = safi;
       SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
       api.nexthop_num = 1;
       api.nexthop = &nexthop;
diff --git a/bgpd/bgp_zebra.h b/bgpd/bgp_zebra.h
index bd95386..1351333 100644
--- a/bgpd/bgp_zebra.h
+++ b/bgpd/bgp_zebra.h
@@ -25,8 +25,8 @@
 extern int bgp_if_update_all (void);
 extern int bgp_config_write_redistribute (struct vty *, struct bgp *, afi_t, safi_t,
 				   int *);
-extern void bgp_zebra_announce (struct prefix *, struct bgp_info *, struct bgp *);
-extern void bgp_zebra_withdraw (struct prefix *, struct bgp_info *);
+extern void bgp_zebra_announce (struct prefix *, struct bgp_info *, struct bgp *, safi_t);
+extern void bgp_zebra_withdraw (struct prefix *, struct bgp_info *, safi_t);
 
 extern int bgp_redistribute_set (struct bgp *, afi_t, int);
 extern int bgp_redistribute_rmap_set (struct bgp *, afi_t, int, const char *);
diff --git a/bgpd/bgpd.c b/bgpd/bgpd.c
index ee0cc5d..5dc014b 100644
--- a/bgpd/bgpd.c
+++ b/bgpd/bgpd.c
@@ -695,7 +695,7 @@
     }
 }
 
-static inline void
+static void
 peer_free (struct peer *peer)
 {
   assert (peer->status == Deleted);
@@ -1147,7 +1147,7 @@
   UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
 
   for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
-    for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++)
+    for (safi = SAFI_UNICAST ; safi < SAFI_RESERVED_3 ; safi++)
       peer->nsf[afi][safi] = 0;
 
   if (peer->t_gr_restart)
@@ -2048,6 +2048,10 @@
 	}
     }
 
+  bgp = bgp_create (as, name);
+  bgp_router_id_set(bgp, &router_id_zebra);
+  *bgp_val = bgp;
+
   /* Create BGP server socket, if first instance.  */
   if (list_isempty(bm->bgp))
     {
@@ -2055,10 +2059,7 @@
 	return BGP_ERR_INVALID_VALUE;
     }
 
-  bgp = bgp_create (as, name);
   listnode_add (bm->bgp, bgp);
-  bgp_router_id_set(bgp, &router_id_zebra);
-  *bgp_val = bgp;
 
   return 0;
 }
@@ -4720,12 +4721,10 @@
 bgp_config_write_peer (struct vty *vty, struct bgp *bgp,
 		       struct peer *peer, afi_t afi, safi_t safi)
 {
-  struct bgp_filter *filter;
   struct peer *g_peer = NULL;
   char buf[SU_ADDRSTRLEN];
   char *addr;
 
-  filter = &peer->filter[afi][safi];
   addr = peer->host;
   if (peer_group_active (peer))
     g_peer = peer->group->conf;
@@ -5014,6 +5013,11 @@
       && ! peer->af_group[afi][safi])
     vty_out (vty, " neighbor %s route-server-client%s", addr, VTY_NEWLINE);
 
+  /* Nexthop-local unchanged. */
+  if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED)
+      && ! peer->af_group[afi][safi])
+    vty_out (vty, " neighbor %s nexthop-local unchanged%s", addr, VTY_NEWLINE);
+
   /* Allow AS in.  */
   if (peer_af_flag_check (peer, afi, safi, PEER_FLAG_ALLOWAS_IN))
     if (! peer_group_active (peer)
diff --git a/bgpd/bgpd.h b/bgpd/bgpd.h
index 4da19e7..892e7de 100644
--- a/bgpd/bgpd.h
+++ b/bgpd/bgpd.h
@@ -632,6 +632,8 @@
 #define BGP_NOTIFY_CAPABILITY_ERR                7
 #define BGP_NOTIFY_MAX	                         8
 
+#define BGP_NOTIFY_SUBCODE_UNSPECIFIC            0
+
 /* BGP_NOTIFY_HEADER_ERR sub codes.  */
 #define BGP_NOTIFY_HEADER_NOT_SYNC               1
 #define BGP_NOTIFY_HEADER_BAD_MESLEN             2
@@ -662,7 +664,7 @@
 #define BGP_NOTIFY_UPDATE_MAL_AS_PATH           11
 #define BGP_NOTIFY_UPDATE_MAX                   12
 
-/* BGP_NOTIFY_CEASE sub codes (draft-ietf-idr-cease-subcode-05).  */
+/* BGP_NOTIFY_CEASE sub codes (RFC 4486).  */
 #define BGP_NOTIFY_CEASE_MAX_PREFIX              1
 #define BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN          2
 #define BGP_NOTIFY_CEASE_PEER_UNCONFIG           3
@@ -725,9 +727,8 @@
 #define BGP_DEFAULT_RESTART_TIME               120
 #define BGP_DEFAULT_STALEPATH_TIME             360
 
-/* SAFI which used in open capability negotiation.  */
-#define BGP_SAFI_VPNV4                         128
-#define BGP_SAFI_VPNV6                         129
+/* RFC4364 */
+#define SAFI_MPLS_LABELED_VPN                  128
 
 /* Max TTL value.  */
 #define TTL_MAX                                255
diff --git a/configure.ac b/configure.ac
index b981d5b..4fe70e1 100755
--- a/configure.ac
+++ b/configure.ac
@@ -5,10 +5,9 @@
 ##  Copyright (c) 1996, 97, 98, 99, 2000 Kunihiro Ishiguro <kunihiro@zebra.org>
 ##  Portions Copyright (c) 2003 Paul Jakma <paul@dishone.st>
 ##
-## $Id$
 AC_PREREQ(2.53)
 
-AC_INIT(Quagga, 0.99.18, [https://bugzilla.quagga.net])
+AC_INIT(Quagga, 0.99.20, [https://bugzilla.quagga.net])
 AC_CONFIG_SRCDIR(lib/zebra.h)
 AC_CONFIG_MACRO_DIR([m4])
 
@@ -73,6 +72,16 @@
 dnl AC_PROG_SED
 AC_CHECK_PROG([SED],[sed],[sed],[/bin/false])
 
+dnl pdflatex and latexmk are needed to build HACKING.pdf
+AC_CHECK_PROG([PDFLATEX],[pdflatex],[pdflatex],[/bin/false])
+AC_CHECK_PROG([LATEXMK],[latexmk],[latexmk],[/bin/false])
+if test "x$PDFLATEX" = "x/bin/false" -o "x$LATEXMK" = "x/bin/false"; then
+	AC_MSG_WARN([Will not be able to make PDF versions of TeX documents])
+else
+	HAVE_LATEX=true
+fi
+AM_CONDITIONAL([HAVE_LATEX], [test "x$HAVE_LATEX" = "xtrue"])
+
 dnl ------------------------------------------------------------------
 dnl Intel compiler check. Although Intel tries really hard to make icc
 dnl look like gcc, there are some differences. It's very verbose with
@@ -198,6 +207,8 @@
 [  --disable-ospfd         do not build ospfd])
 AC_ARG_ENABLE(ospf6d,
 [  --disable-ospf6d        do not build ospf6d])
+AC_ARG_ENABLE(babeld,
+[  --disable-babeld        do not build babeld])
 AC_ARG_ENABLE(watchquagga,
 [  --disable-watchquagga   do not build watchquagga])
 AC_ARG_ENABLE(isisd,
@@ -395,7 +406,7 @@
 dnl -------------------------
 AC_CHECK_HEADERS([stropts.h sys/ksym.h sys/times.h sys/select.h \
 	sys/types.h linux/version.h netdb.h asm/types.h \
-	sys/param.h limits.h signal.h libutil.h \
+	sys/param.h limits.h signal.h \
 	sys/socket.h netinet/in.h time.h sys/time.h])
 
 dnl Utility macro to avoid retyping includes all the time
@@ -442,8 +453,35 @@
 #endif /* TIME_WITH_SYS_TIME */
 ])dnl
 
-AC_CHECK_HEADERS([sys/un.h net/if.h netinet/in_systm.h netinet/in_var.h \
-	net/if_dl.h net/if_var.h net/netopt.h net/route.h \
+dnl HAVE_NET_IF_H must be discovered by the time the longer AC_CHECK_HEADERS
+dnl round below execution begins, otherwise it doesn't properly detect
+dnl HAVE_NETINET6_IN6_VAR_H, HAVE_NET_IF_VAR_H and HAVE_STRUCT_IN6_ALIASREQ
+dnl on FreeBSD (BZ#408).
+
+AC_CHECK_HEADERS([net/if.h], [], [], QUAGGA_INCLUDES)
+
+m4_define([QUAGGA_INCLUDES],
+QUAGGA_INCLUDES
+[#if HAVE_NET_IF_H
+# include <net/if.h>
+#endif
+])dnl
+
+dnl Same applies for HAVE_NET_IF_VAR_H, which HAVE_NETINET6_ND6_H and
+dnl HAVE_NETINET_IN_VAR_H depend upon. But if_var.h depends on if.h, hence
+dnl an additional round for it.
+
+AC_CHECK_HEADERS([net/if_var.h], [], [], QUAGGA_INCLUDES)
+
+m4_define([QUAGGA_INCLUDES],
+QUAGGA_INCLUDES
+[#if HAVE_NET_IF_VAR_H
+# include <net/if_var.h>
+#endif
+])dnl
+
+AC_CHECK_HEADERS([sys/un.h netinet/in_systm.h netinet/in_var.h \
+	net/if_dl.h net/netopt.h net/route.h \
 	inet/nd.h arpa/inet.h netinet/ip_icmp.h \
 	fcntl.h stddef.h sys/ioctl.h syslog.h wchar.h wctype.h \
 	sys/sysctl.h sys/sockio.h kvm.h sys/conf.h],
@@ -458,10 +496,7 @@
 
 m4_define([QUAGGA_INCLUDES],
 QUAGGA_INCLUDES
-[#if HAVE_NET_IF_H
-# include <net/if.h>
-#endif
-#if HAVE_SYS_UN_H
+[#if HAVE_SYS_UN_H
 # include <sys/un.h>
 #endif
 #if HAVE_NETINET_IN_SYSTM_H
@@ -473,9 +508,6 @@
 #if HAVE_NET_IF_DL_H
 # include <net/if_dl.h>
 #endif
-#if HAVE_NET_IF_VAR_H
-# include <net/if_var.h>
-#endif
 #if HAVE_NET_NETOPT_H
 # include <net/netopt.h>
 #endif
@@ -595,6 +627,13 @@
 
 dnl ----------
 dnl PAM module
+dnl
+dnl Quagga detects the PAM library it is built against by checking for a
+dnl functional pam_misc.h (Linux-PAM) or openpam.h (OpenPAM) header. pam_misc.h
+dnl is known to #include pam_appl.h, the standard header of a PAM library, and
+dnl openpam.h doesn't do that, although depends on the header too. Hence a
+dnl little assistance to AC_CHECK_HEADER is necessary for the proper detection
+dnl of OpenPAM.
 dnl ----------
 if test "$with_libpam" = "yes"; then
   AC_CHECK_HEADER([security/pam_misc.h],
@@ -608,7 +647,7 @@
      AC_DEFINE(PAM_CONV_FUNC,openpam_ttyconv,Have openpam_ttyconv)
      pam_conv_func="openpam_ttyconv"
     ],
-    [], QUAGGA_INCLUDES)
+    [], QUAGGA_INCLUDES[#include <security/pam_appl.h>])
   if test -z "$ac_cv_header_security_pam_misc_h$ac_cv_header_security_openpam_h" ; then
     AC_MSG_WARN([*** pam support will not be built ***])
     with_libpam="no"
@@ -665,11 +704,9 @@
 AC_FUNC_CHOWN
 AC_FUNC_FNMATCH
 AC_FUNC_FORK
-AC_FUNC_MALLOC
 AC_FUNC_MEMCMP
 AC_FUNC_MKTIME
 AC_FUNC_STRFTIME
-AC_FUNC_REALLOC
 AC_FUNC_STAT
 AC_FUNC_SELECT_ARGTYPES
 AC_FUNC_STRFTIME
@@ -916,6 +953,15 @@
 AC_DEFINE(HAVE_BSD_STRUCT_IP_MREQ_HACK,,[Can pass ifindex in struct ip_mreq])],
 AC_MSG_RESULT(no))
 
+AC_MSG_CHECKING([for RFC3678 protocol-independed API])
+AC_TRY_COMPILE([
+#include <sys/types.h>
+#include <netinet/in.h>
+], [struct group_req gr; int sock; setsockopt(sock, IPPROTO_IP, MCAST_JOIN_GROUP, (void*)&gr, sizeof(gr));
+], [AC_MSG_RESULT(yes)
+AC_DEFINE(HAVE_RFC3678,1,[Have RFC3678 protocol-independed API])],
+AC_MSG_RESULT(no))
+
 dnl ---------------------------------------------------------------
 dnl figure out how to check link-state
 dnl ---------------------------------------------------------------
@@ -972,10 +1018,18 @@
 dnl check ipforward detect method
 dnl -----------------------------
 AC_CACHE_CHECK([ipforward method], [quagga_cv_ipforward_method],
-[for quagga_cv_ipforward_method in /proc/net/snmp /dev/ip /dev/null;
-do
-  test x`ls $quagga_cv_ipforward_method 2>/dev/null` = x"$quagga_cv_ipforward_method" && break
-done
+[if test x$cross_compiling = xyes; then
+  if test x"$opsys" = x"gnu-linux"; then
+    quagga_cv_ipforward_method=/proc/net/snmp
+  else
+    quagga_cv_ipforward_method=/dev/ip
+  fi
+else
+  for quagga_cv_ipforward_method in /proc/net/snmp /dev/ip /dev/null;
+  do
+    test x`ls $quagga_cv_ipforward_method 2>/dev/null` = x"$quagga_cv_ipforward_method" && break
+  done
+fi
 case $quagga_cv_ipforward_method in
   "/proc/net/snmp")  quagga_cv_ipforward_method="proc";;
   "/dev/ip")         
@@ -1193,6 +1247,12 @@
   OSPFD="ospfd"
 fi
 
+if test "${enable_babeld}" = "no";then
+  BABELD=""
+else
+  BABELD="babeld"
+fi
+
 if test "${enable_watchquagga}" = "no";then
   WATCHQUAGGA=""
 else
@@ -1249,6 +1309,7 @@
 AC_SUBST(RIPNGD)
 AC_SUBST(OSPFD)
 AC_SUBST(OSPF6D)
+AC_SUBST(BABELD)
 AC_SUBST(WATCHQUAGGA)
 AC_SUBST(ISISD)
 AC_SUBST(SOLARIS)
@@ -1403,14 +1464,12 @@
 dnl ---------------------------
 dnl check for glibc 'backtrace'
 dnl --------------------------- 
-if test "${glibc}" = "yes"; then
-   AC_CHECK_HEADER([execinfo.h],
-    [AC_CHECK_FUNC([backtrace],
-    	[AC_DEFINE(HAVE_GLIBC_BACKTRACE,,[Glibc backtrace])
-    	 AC_DEFINE(HAVE_STACK_TRACE,,[Stack symbol decoding])
-    	])
-    ])
-fi
+AC_CHECK_HEADER([execinfo.h],
+  [AC_CHECK_FUNC([backtrace],
+    [AC_DEFINE(HAVE_GLIBC_BACKTRACE,,[Glibc backtrace])
+     AC_DEFINE(HAVE_STACK_TRACE,,[Stack symbol decoding])
+  ])
+])
 
 dnl -----------------------------------------
 dnl check for malloc mallinfo struct and call
@@ -1494,6 +1553,7 @@
 AC_DEFINE_UNQUOTED(PATH_BGPD_PID, "$quagga_statedir/bgpd.pid",bgpd PID)
 AC_DEFINE_UNQUOTED(PATH_OSPFD_PID, "$quagga_statedir/ospfd.pid",ospfd PID)
 AC_DEFINE_UNQUOTED(PATH_OSPF6D_PID, "$quagga_statedir/ospf6d.pid",ospf6d PID)
+AC_DEFINE_UNQUOTED(PATH_BABELD_PID, "$quagga_statedir/babeld.pid",babeld PID)
 AC_DEFINE_UNQUOTED(PATH_ISISD_PID, "$quagga_statedir/isisd.pid",isisd PID)
 AC_DEFINE_UNQUOTED(PATH_WATCHQUAGGA_PID, "$quagga_statedir/watchquagga.pid",watchquagga PID)
 AC_DEFINE_UNQUOTED(ZEBRA_SERV_PATH, "$quagga_statedir/zserv.api",zebra api socket)
@@ -1503,6 +1563,7 @@
 AC_DEFINE_UNQUOTED(BGP_VTYSH_PATH, "$quagga_statedir/bgpd.vty",bgpd vty socket)
 AC_DEFINE_UNQUOTED(OSPF_VTYSH_PATH, "$quagga_statedir/ospfd.vty",ospfd vty socket)
 AC_DEFINE_UNQUOTED(OSPF6_VTYSH_PATH, "$quagga_statedir/ospf6d.vty",ospf6d vty socket)
+AC_DEFINE_UNQUOTED(BABEL_VTYSH_PATH, "$quagga_statedir/babeld.vty",babeld vty socket)
 AC_DEFINE_UNQUOTED(ISIS_VTYSH_PATH, "$quagga_statedir/isisd.vty",isisd vty socket)
 AC_DEFINE_UNQUOTED(DAEMON_VTY_DIR, "$quagga_statedir",daemon vty directory)
 
@@ -1527,8 +1588,9 @@
 
 AC_CONFIG_FILES([Makefile lib/Makefile zebra/Makefile ripd/Makefile 
 	  ripngd/Makefile bgpd/Makefile ospfd/Makefile watchquagga/Makefile
-	  ospf6d/Makefile isisd/Makefile vtysh/Makefile doc/Makefile 
-	  ospfclient/Makefile tests/Makefile m4/Makefile redhat/Makefile
+	  ospf6d/Makefile isisd/Makefile babeld/Makefile vtysh/Makefile
+          doc/Makefile ospfclient/Makefile tests/Makefile m4/Makefile
+          redhat/Makefile
 	  pkgsrc/Makefile
 	  redhat/quagga.spec 
 	  lib/version.h
diff --git a/doc/.cvsignore b/doc/.cvsignore
deleted file mode 100644
index 43987b2..0000000
--- a/doc/.cvsignore
+++ /dev/null
@@ -1,31 +0,0 @@
-Makefile
-Makefile.in
-mdate-sh
-draft-zebra-00.txt
-zebra.info-*
-zebra.html
-defines.texi
-version.texi
-quagga.html
-quagga.info
-*.pdf
-*.eps
-quagga.ps
-quagga.dvi
-stamp-vti
-.nfs*
-*.aux
-*.cp
-*.cps
-*.fn
-*.fns
-*.ky
-*.kys
-*.log
-*.op
-*.pg
-*.toc
-*.tp
-*.vr
-.arch-inventory
-.arch-ids
diff --git a/doc/BGP-TypeCode b/doc/BGP-TypeCode
index 5663c4d..b321807 100644
--- a/doc/BGP-TypeCode
+++ b/doc/BGP-TypeCode
@@ -3,24 +3,22 @@
 
   Value  Attribute            References
 =========================================================================
-    1    ORIGIN               [RFC 1771]
-    2    AS_PATH              [RFC 1771]
-    3    NEXT_HOP             [RFC 1771]
-    4    MULTI_EXIT_DISC      [RFC 1771]
-    5    LOCAL_PREF           [RFC 1771]
-    6    ATOMIC_AGGREGATE     [RFC 1771]
-    7    AGGREGATOR           [RFC 1771]
+    1    ORIGIN               [RFC 4271]
+    2    AS_PATH              [RFC 4271]
+    3    NEXT_HOP             [RFC 4271]
+    4    MULTI_EXIT_DISC      [RFC 4271]
+    5    LOCAL_PREF           [RFC 4271]
+    6    ATOMIC_AGGREGATE     [RFC 4271]
+    7    AGGREGATOR           [RFC 4271]
     8    COMMUNITIES          [RFC 1997]
-    9    ORIGINATOR_ID        [RFC 1966]
-   10    CLUSTER_LIST         [RFC 1966]
+    9    ORIGINATOR_ID        [RFC 4456]
+   10    CLUSTER_LIST         [RFC 4456]
    11    DPA                  [draft-ietf-idr-bgp-dpa-05.txt(expired)]
-   12    ADVERTISER           [Changed from RFC 1863 bgp@ans.net ML?]
-   13    RCID_PATH            [Changed from RFC 1863 bgp@ans.net ML?]
-   14    MP_REACH_NLRI        [RFC 2283]
-   15    MP_UNREACH_NLRI      [RFC 2283]
-   16    EXT_COMMUNITIES      [draft-ramachandra-bgp-ext-communities-09.txt]
+   12    ADVERTISER           [RFC 1863]
+   13    RCID_PATH            [RFC 1863]
+   14    MP_REACH_NLRI        [RFC 4760]
+   15    MP_UNREACH_NLRI      [RFC 4760]
+   16    EXT_COMMUNITIES      [RFC 4360]
    17    AS4_PATH             [RFC 4893]
    18    AS4_AGGREGATOR       [RFC 4893]
-  254    RCID_PATH            [RFC 1863]
-  255    ADVERTISER           [RFC 1863]
 =========================================================================
diff --git a/doc/Makefile.am b/doc/Makefile.am
index cd4d71b..dde95ab 100644
--- a/doc/Makefile.am
+++ b/doc/Makefile.am
@@ -46,11 +46,11 @@
 quagga.pdf: $(info_TEXINFOS) $(figures_pdf) $(quagga_TEXINFOS)
 	$(TEXI2PDF) -o "$@" $<
 
-quagga_TEXINFOS = appendix.texi basic.texi bgpd.texi filter.texi install.texi \
-	ipv6.texi kernel.texi main.texi ospf6d.texi ospfd.texi overview.texi \
-	protocol.texi ripd.texi ripngd.texi routemap.texi snmp.texi \
-	vtysh.texi routeserver.texi defines.texi $(figures_png) snmptrap.texi \
-	$(figures_txt)
+quagga_TEXINFOS = appendix.texi babeld.texi basic.texi bgpd.texi filter.texi \
+	install.texi ipv6.texi kernel.texi main.texi ospf6d.texi ospfd.texi \
+	overview.texi protocol.texi ripd.texi ripngd.texi routemap.texi \
+	snmp.texi vtysh.texi routeserver.texi defines.texi $(figures_png) \
+	snmptrap.texi $(figures_txt)
 
 .png.eps:
 	$(PNGTOEPS) $< "$@"
diff --git a/doc/babeld.texi b/doc/babeld.texi
new file mode 100644
index 0000000..1f24edd
--- /dev/null
+++ b/doc/babeld.texi
@@ -0,0 +1,118 @@
+@c -*-texinfo-*-
+@c This is part of the Quagga Manual.
+@c @value{COPYRIGHT_STR}
+@c See file quagga.texi for copying conditions.
+@node Babel
+@chapter Babel
+
+Babel is an interior gateway protocol that is suitable both for wired
+networks and for wireless mesh networks.  Babel has been described as
+``RIP on speed'' --- it is based on the same principles as RIP, but
+includes a number of refinements that make it react much faster to
+topology changes without ever counting to infinity, and allow it to
+perform reliable link quality estimation on wireless links.  Babel is
+a double-stack routing protocol, meaning that a single Babel instance
+is able to perform routing for both IPv4 and IPv6.
+
+Quagga implements Babel as described in RFC6126.
+
+@menu
+* Configuring babeld::          
+* Babel configuration::         
+* Babel redistribution::        
+* Show Babel information::      
+* Babel debugging commands::    
+@end menu
+
+@node Configuring babeld, Babel configuration, Babel, Babel
+@section Configuring babeld
+
+The @command{babeld} daemon can be invoked with any of the common
+options (@pxref{Common Invocation Options}).
+
+The @command{zebra} daemon must be running before @command{babeld} is
+invoked. Also, if @command{zebra} is restarted then @command{babeld}
+must be too.
+
+Configuration of @command{babeld} is done in its configuration file
+@file{babeld.conf}.
+
+@node Babel configuration, Babel redistribution, Configuring babeld, Babel
+@section Babel configuration
+
+@deffn Command {router babel} {}
+@deffnx Command {no router babel} {}
+Enable or disable Babel routing.
+@end deffn
+
+@deffn {Babel Command} {network @var{ifname}} {}
+@deffnx {Babel Command} {no network @var{ifname}} {}
+Enable or disable Babel on the given interface.
+@end deffn
+
+@deffn {Interface Command} {babel wired} {}
+@deffnx {Interface Command} {babel wireless} {}
+Specifies whether this interface is wireless, which disables a number
+of optimisations that are only correct on wired interfaces.
+Specifying @code{wireless} (the default) is always correct, but may
+cause slower convergence and extra routing traffic.
+@end deffn
+
+@deffn {Interface Command} {babel split-horizon}
+@deffnx {Interface Command} {no babel split-horizon}
+Specifies whether to perform split-horizon on the interface.
+Specifying @code{no babel split-horizon} (the default) is always
+correct, while @code{babel split-horizon} is an optimisation that
+should only be used on symmetric and transitive (wired) networks.
+@end deffn
+
+@deffn {Interface Command} {babel hello-interval <20-655340>}
+Specifies the time in milliseconds between two scheduled hellos.  On
+wired links, Babel notices a link failure within two hello intervals;
+on wireless links, the link quality value is reestimated at every
+hello interval.  The default is 4000@dmn{ms}.
+@end deffn
+
+@deffn {Interface Command} {babel update-interval <20-655340>}
+Specifies the time in milliseconds between two scheduled updates.
+Since Babel makes extensive use of triggered updates, this can be set
+to fairly high values on links with little packet loss.  The default
+is 20000@dmn{ms}.
+@end deffn
+
+@deffn {Babel Command} {babel resend-delay <20-655340>}
+Specifies the time in milliseconds after which an ``important''
+request or update will be resent.  The default is 2000@dmn{ms}.  You
+probably don't want to tweak this value.
+@end deffn
+
+@node Babel redistribution, Show Babel information, Babel configuration, Babel
+@section Babel redistribution
+
+@deffn {Babel command} {redistribute @var{kind}}
+@deffnx {Babel command} {no redistribute @var{kind}}
+Specify which kind of routes should be redistributed into Babel.
+@end deffn
+
+@node Show Babel information, Babel debugging commands, Babel redistribution, Babel
+@section Show Babel information
+
+@deffn {Command} {show babel database} {}
+@deffnx {Command} {show babel interface} {}
+@deffnx {Command} {show babel neighbour} {}
+@deffnx {Command} {show babel parameters} {}
+These commands dump various parts of @command{babeld}'s internal
+state.  They are mostly useful for troubleshooting.
+@end deffn
+
+@node Babel debugging commands,  , Show Babel information, Babel
+@section Babel debugging commands
+
+@deffn {Babel Command} {debug babel @var{kind}} {}
+@deffnx {Babel Command} {no debug babel @var{kind}} {}
+Enable or disable debugging messages of a given kind.  @var{kind} can
+be one of @samp{common}, @samp{kernel}, @samp{filter}, @samp{timeout},
+@samp{interface}, @samp{route} or @samp{all}.  Note that if you have
+compiled with the NO_DEBUG flag, then these commands aren't available.
+@end deffn
+
diff --git a/doc/basic.texi b/doc/basic.texi
index 77fceee..b3b23ca 100644
--- a/doc/basic.texi
+++ b/doc/basic.texi
@@ -46,14 +46,14 @@
 
 Config files are generally found in:
 
-@itemize @asis
+@itemize @w{}
 @item @file{@value{INSTALL_PREFIX_ETC}/*.conf}
 @end itemize
 
 Each of the daemons has its own
 config file.  For example, zebra's default config file name is:
 
-@itemize @asis
+@itemize @w{}
 @item @file{@value{INSTALL_PREFIX_ETC}/zebra.conf}
 @end itemize
 
diff --git a/doc/bgpd.texi b/doc/bgpd.texi
index e746330..6383460 100644
--- a/doc/bgpd.texi
+++ b/doc/bgpd.texi
@@ -85,6 +85,7 @@
 @menu
 * BGP distance::                
 * BGP decision process::        
+* BGP route flap dampening::      
 @end menu
 
 @node BGP distance
@@ -123,6 +124,27 @@
 decision process.
 @end deffn
 
+@node BGP route flap dampening
+@subsection BGP route flap dampening
+
+@deffn {BGP} {bgp dampening @var{<1-45>} @var{<1-20000>} @var{<1-20000>} @var{<1-255>}} {}
+This command enables BGP route-flap dampening and specifies dampening parameters.
+
+@table @asis
+@item @asis{half-life}
+Half-life time for the penalty
+@item @asis{reuse-threshold}
+Value to start reusing a route
+@item @asis{suppress-threshold}
+Value to start suppressing a route
+@item @asis{max-suppress}
+Maximum duration to suppress a stable route
+@end table
+
+The route-flap damping algorithm is compatible with @cite{RFC2439}. The use of this command
+is not recommended nowadays, see @uref{http://www.ripe.net/ripe/docs/ripe-378,,RIPE-378}.
+@end deffn
+
 @node BGP network
 @section BGP network
 
@@ -930,6 +952,14 @@
 Clear peer using soft reconfiguration.
 @end deffn
 
+@deffn {Command} {show ip bgp dampened-paths} {}
+Display paths suppressed due to dampening
+@end deffn
+
+@deffn {Command} {show ip bgp flap-statistics} {}
+Display flap statistics of routes
+@end deffn
+
 @deffn {Command} {show debug} {}
 @end deffn
 
diff --git a/doc/ipv6.texi b/doc/ipv6.texi
index a78a92f..b6cc437 100644
--- a/doc/ipv6.texi
+++ b/doc/ipv6.texi
@@ -2,7 +2,7 @@
 @chapter IPv6 Support
 
 Quagga fully supports IPv6 routing.  As described so far, Quagga supports
-RIPng, OSPFv3 and BGP-4+.  You can give IPv6 addresses to an interface
+RIPng, OSPFv3, Babel and BGP-4+.  You can give IPv6 addresses to an interface
 and configure static IPv6 routing information.  Quagga IPv6 also provides
 automatic address configuration via a feature called @code{address
 auto configuration}.  To do it, the router must send router advertisement
@@ -62,23 +62,24 @@
 @end itemize
 @end deffn
 
-@deffn {Interface Command} {ipv6 nd ra-interval SECONDS} {}
-@deffnx {Interface Command} {no ipv6 nd ra-interval} {}
+@deffn {Interface Command} {ipv6 nd ra-interval <1-1800>} {}
+@deffnx {Interface Command} {no ipv6 nd ra-interval [<1-1800>]} {}
 The  maximum  time allowed between sending unsolicited multicast router
-advertisements from the interface, in seconds. Must be no less than 3 seconds.
+advertisements from the interface, in seconds.
 
 Default: @code{600}
 @end deffn
 
-@deffn {Interface Command} {ipv6 nd ra-interval msec MILLISECONDS} {}
-@deffnx {Interface Command} {no ipv6 nd ra-interval msec} {}
+@deffn {Interface Command} {ipv6 nd ra-interval msec <70-1800000>} {}
+@deffnx {Interface Command} {no ipv6 nd ra-interval [msec <70-1800000>]} {}
 The  maximum  time allowed between sending unsolicited multicast router
-advertisements from the interface, in milliseconds. Must be no less than 30 milliseconds.
+advertisements from the interface, in milliseconds.
 
 Default: @code{600000}
 @end deffn
-@deffn {Interface Command} {ipv6 nd ra-lifetime SECONDS} {}
-@deffnx {Interface Command} {no ipv6 nd ra-lifetime} {}
+
+@deffn {Interface Command} {ipv6 nd ra-lifetime <0-9000>} {}
+@deffnx {Interface Command} {no ipv6 nd ra-lifetime [<0-9000>]} {}
 The value to be placed in the Router Lifetime field of router advertisements
 sent from the interface, in seconds. Indicates the usefulness of the router
 as a default router on this interface. Setting the value to zero indicates
@@ -89,12 +90,12 @@
 Default: @code{1800}
 @end deffn
 
-@deffn {Interface Command} {ipv6 nd reachable-time MILLISECONDS} {}
-@deffnx {Interface Command} {no ipv6 nd reachable-time} {}
+@deffn {Interface Command} {ipv6 nd reachable-time <1-3600000>} {}
+@deffnx {Interface Command} {no ipv6 nd reachable-time [<1-3600000>]} {}
 The value to be placed in the Reachable Time field in the Router Advertisement
 messages sent by the router, in milliseconds. The configured time enables the
 router to detect unavailable neighbors. The value zero means unspecified (by
-this router). Must be no greater than @code{3,600,000} milliseconds (1 hour).
+this router).
 
 Default: @code{0}
 @end deffn
@@ -126,18 +127,20 @@
 Default: not set
 @end deffn
 
-@deffn {Interface Command} {ipv6 nd home-agent-preference} {}
-@deffnx {Interface Command} {no ipv6 nd home-agent-preference} {}
+@deffn {Interface Command} {ipv6 nd home-agent-preference <0-65535>} {}
+@deffnx {Interface Command} {no ipv6 nd home-agent-preference [<0-65535>]} {}
 The value to be placed in Home Agent Option, when Home Agent config flag is set, 
-which indicates to hosts Home Agent preference.
+which indicates to hosts Home Agent preference. The default value of 0 stands
+for the lowest preference possible.
 
 Default: 0
 @end deffn
 
-@deffn {Interface Command} {ipv6 nd home-agent-lifetime} {}
-@deffnx {Interface Command} {no ipv6 nd home-agent-lifetime} {}
++@deffn {Interface Command} {ipv6 nd home-agent-lifetime <0-65520>} {}
++@deffnx {Interface Command} {no ipv6 nd home-agent-lifetime [<0-65520>]} {}
 The value to be placed in Home Agent Option, when Home Agent config flag is set, 
-which indicates to hosts Home Agent Lifetime. A value of 0 means to place Router Lifetime value.
+which indicates to hosts Home Agent Lifetime. The default value of 0 means to
+place the current Router Lifetime value.
 
 Default: 0
 @end deffn
@@ -151,12 +154,21 @@
 @end deffn
 
 @deffn {Interface Command} {ipv6 nd router-preference (high|medium|low)} {}
-@deffnx {Interface Command} {no ipv6 nd router-preference} {}
+@deffnx {Interface Command} {no ipv6 nd router-preference [(high|medium|low)]} {}
 Set default router preference in IPv6 router advertisements per RFC4191.
 
 Default: medium
 @end deffn
 
+@deffn {Interface Command} {ipv6 nd mtu <1-65535>} {}
+@deffnx {Interface Command} {no ipv6 nd mtu [<1-65535>]} {}
+Include an MTU (type 5) option in each RA packet to assist the attached hosts
+in proper interface configuration. The announced value is not verified to be
+consistent with router interface MTU.
+
+Default: don't advertise any MTU option
+@end deffn
+
 @example
 @group
 interface eth0
@@ -166,6 +178,6 @@
 @end example
 
 For more information see @cite{RFC2462 (IPv6 Stateless Address Autoconfiguration)}
-, @cite{RFC2461 (Neighbor Discovery for IP Version 6 (IPv6))}
-, @cite{RFC3775 (Mobility Support in IPv6 (Mobile IPv6))}
+, @cite{RFC4861 (Neighbor Discovery for IP Version 6 (IPv6))}
+, @cite{RFC6275 (Mobility Support in IPv6)}
 and @cite{RFC4191 (Default Router Preferences and More-Specific Routes)}.
diff --git a/doc/mpls/.cvsignore b/doc/mpls/.cvsignore
deleted file mode 100644
index 1218df9..0000000
--- a/doc/mpls/.cvsignore
+++ /dev/null
@@ -1,2 +0,0 @@
-.arch-ids
-.arch-inventory
diff --git a/doc/ospfd.texi b/doc/ospfd.texi
index f879a98..856a2ba 100644
--- a/doc/ospfd.texi
+++ b/doc/ospfd.texi
@@ -99,7 +99,7 @@
 to section G.2 (changes) in section 16.4 a change to the path
 preference algorithm that prevents possible routing loops that were
 possible in the old version of OSPFv2. More specifically it demands
-that inter-area paths and intra-area path are now of equal preference
+that inter-area paths and intra-area backbone path are now of equal preference
 but still both preferred to external paths.
 
 This command should NOT be set normally.
@@ -568,10 +568,6 @@
 @deffnx {OSPF Command} {no distance ospf} {}
 @end deffn
 
-@deffn {Command} {router zebra} {}
-@deffnx {Command} {no router zebra} {}
-@end deffn
-
 @node Showing OSPF information
 @section Showing OSPF information
 
diff --git a/doc/quagga.texi b/doc/quagga.texi
index ff8002c..ff913aa 100644
--- a/doc/quagga.texi
+++ b/doc/quagga.texi
@@ -86,6 +86,7 @@
 * RIPng::
 * OSPFv2::
 * OSPFv3::
+* Babel::
 * BGP::
 * Configuring Quagga as a Route Server::
 * VTY shell::
@@ -110,6 +111,7 @@
 @include ripngd.texi
 @include ospfd.texi
 @include ospf6d.texi
+@include babeld.texi
 @include bgpd.texi
 @include routeserver.texi
 @include vtysh.texi
diff --git a/doc/ripd.texi b/doc/ripd.texi
index 197bc5a..c6f804a 100644
--- a/doc/ripd.texi
+++ b/doc/ripd.texi
@@ -424,10 +424,10 @@
 Match if route destination is permitted by access-list.
 @end deffn
 
-@deffn {Route Map} {match ip next-hop A.B.C.D} {}
-Cisco uses here <access-list>, @command{ripd} IPv4 address. Match if
-route has this next-hop (meaning next-hop listed in the rip route
-table - "show ip rip")
+@deffn {Route Map} {match ip next-hop @var{word}} {}
+@deffnx {Route Map} {match ip next-hop prefix-list @var{word}} {}
+Match if route next-hop (meaning next-hop listed in the rip route-table
+as displayed by "show ip rip") is permitted by access-list.
 @end deffn
 
 @deffn {Route Map} {match metric <0-4294967295>} {}
diff --git a/guile/.cvsignore b/guile/.cvsignore
deleted file mode 100644
index d345fc3..0000000
--- a/guile/.cvsignore
+++ /dev/null
@@ -1,7 +0,0 @@
-Makefile
-*.o
-zebra-guile
-Makefile.in
-.nfs*
-.arch-inventory
-.arch-ids
diff --git a/init/.cvsignore b/init/.cvsignore
deleted file mode 100644
index 3c447f5..0000000
--- a/init/.cvsignore
+++ /dev/null
@@ -1,3 +0,0 @@
-Makefile
-Makefile.in
-.nfs*
diff --git a/lib/.cvsignore b/lib/.cvsignore
deleted file mode 100644
index 806696d..0000000
--- a/lib/.cvsignore
+++ /dev/null
@@ -1,14 +0,0 @@
-Makefile
-Makefile.in
-*.o
-*.lo
-*.la
-version.c
-version.h
-.deps
-.nfs*
-.libs
-.arch-inventory
-.arch-ids
-memtypes.h
-route_types.h
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 315e919..890cc5c 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -29,10 +29,10 @@
 	privs.h sigevent.h pqueue.h jhash.h zassert.h memtypes.h \
 	workqueue.h route_types.h
 
-EXTRA_DIST = regex.c regex-gnu.h memtypes.awk route_types.awk route_types.txt
+EXTRA_DIST = regex.c regex-gnu.h memtypes.awk route_types.pl route_types.txt
 
 memtypes.h: $(srcdir)/memtypes.c $(srcdir)/memtypes.awk
 	($(GAWK) -f $(srcdir)/memtypes.awk $(srcdir)/memtypes.c > $@)
 
-route_types.h: $(srcdir)/route_types.txt $(srcdir)/route_types.awk
-	($(GAWK) -f $(srcdir)/route_types.awk $(srcdir)/route_types.txt > $@)
+route_types.h: $(srcdir)/route_types.txt $(srcdir)/route_types.pl
+	@PERL@ $(srcdir)/route_types.pl < $(srcdir)/route_types.txt > $@
diff --git a/lib/command.c b/lib/command.c
index 264e0f7..e62a7a7 100644
--- a/lib/command.c
+++ b/lib/command.c
@@ -1,6 +1,4 @@
 /*
-   $Id$
- 
    Command interpreter routine for virtual terminal [aka TeletYpe]
    Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
 
@@ -2402,6 +2400,7 @@
     case BGP_NODE:
     case RIP_NODE:
     case RIPNG_NODE:
+    case BABEL_NODE:
     case OSPF_NODE:
     case OSPF6_NODE:
     case ISIS_NODE:
@@ -2451,6 +2450,7 @@
     case ZEBRA_NODE:
     case RIP_NODE:
     case RIPNG_NODE:
+    case BABEL_NODE:
     case BGP_NODE:
     case BGP_VPNV4_NODE:
     case BGP_IPV4_NODE:
diff --git a/lib/command.h b/lib/command.h
index 1275efe..2d708d8 100644
--- a/lib/command.h
+++ b/lib/command.h
@@ -78,6 +78,7 @@
   TABLE_NODE,			/* rtm_table selection node. */
   RIP_NODE,			/* RIP protocol mode node. */ 
   RIPNG_NODE,			/* RIPng protocol mode node. */
+  BABEL_NODE,			/* Babel protocol mode node. */
   BGP_NODE,			/* BGP protocol mode which includes BGP4+ */
   BGP_VPNV4_NODE,		/* BGP MPLS-VPN PE exchange. */
   BGP_IPV4_NODE,		/* BGP IPv4 unicast address family.  */
diff --git a/lib/distribute.c b/lib/distribute.c
index 420849d..8d6f637 100644
--- a/lib/distribute.c
+++ b/lib/distribute.c
@@ -299,7 +299,6 @@
        "Filter outgoing routing updates\n")
 {
   enum distribute_type type;
-  struct distribute *dist;
 
   /* Check of distribute list type. */
   if (strncmp (argv[1], "i", 1) == 0)
@@ -314,7 +313,7 @@
     }
 
   /* Get interface name corresponding distribute list. */
-  dist = distribute_list_set (NULL, type, argv[0]);
+  distribute_list_set (NULL, type, argv[0]);
 
   return CMD_SUCCESS;
 }
@@ -379,7 +378,6 @@
        "Interface name\n")
 {
   enum distribute_type type;
-  struct distribute *dist;
 
   /* Check of distribute list type. */
   if (strncmp (argv[1], "i", 1) == 0)
@@ -393,7 +391,7 @@
     }
 
   /* Get interface name corresponding distribute list. */
-  dist = distribute_list_set (argv[2], type, argv[0]);
+  distribute_list_set (argv[2], type, argv[0]);
 
   return CMD_SUCCESS;
 }       
@@ -407,7 +405,7 @@
        "Filter outgoing routing updates\n"
        "Interface name\n")
 
-DEFUN (no_districute_list, no_distribute_list_cmd,
+DEFUN (no_distribute_list, no_distribute_list_cmd,
        "no distribute-list WORD (in|out) WORD",
        NO_STR
        "Filter networks in routing updates\n"
@@ -439,7 +437,7 @@
   return CMD_SUCCESS;
 }       
 
-ALIAS (no_districute_list, no_ipv6_distribute_list_cmd,
+ALIAS (no_distribute_list, no_ipv6_distribute_list_cmd,
        "no distribute-list WORD (in|out) WORD",
        NO_STR
        "Filter networks in routing updates\n"
@@ -448,7 +446,7 @@
        "Filter outgoing routing updates\n"
        "Interface name\n")
 
-DEFUN (districute_list_prefix_all,
+DEFUN (distribute_list_prefix_all,
        distribute_list_prefix_all_cmd,
        "distribute-list prefix WORD (in|out)",
        "Filter networks in routing updates\n"
@@ -458,7 +456,6 @@
        "Filter outgoing routing updates\n")
 {
   enum distribute_type type;
-  struct distribute *dist;
 
   /* Check of distribute list type. */
   if (strncmp (argv[1], "i", 1) == 0)
@@ -473,12 +470,12 @@
     }
 
   /* Get interface name corresponding distribute list. */
-  dist = distribute_list_prefix_set (NULL, type, argv[0]);
+  distribute_list_prefix_set (NULL, type, argv[0]);
 
   return CMD_SUCCESS;
 }       
 
-ALIAS (districute_list_prefix_all,
+ALIAS (distribute_list_prefix_all,
        ipv6_distribute_list_prefix_all_cmd,
        "distribute-list prefix WORD (in|out)",
        "Filter networks in routing updates\n"
@@ -487,7 +484,7 @@
        "Filter incoming routing updates\n"
        "Filter outgoing routing updates\n")
 
-DEFUN (no_districute_list_prefix_all,
+DEFUN (no_distribute_list_prefix_all,
        no_distribute_list_prefix_all_cmd,
        "no distribute-list prefix WORD (in|out)",
        NO_STR
@@ -521,7 +518,7 @@
   return CMD_SUCCESS;
 }       
 
-ALIAS (no_districute_list_prefix_all,
+ALIAS (no_distribute_list_prefix_all,
        no_ipv6_distribute_list_prefix_all_cmd,
        "no distribute-list prefix WORD (in|out)",
        NO_STR
@@ -531,7 +528,7 @@
        "Filter incoming routing updates\n"
        "Filter outgoing routing updates\n")
 
-DEFUN (districute_list_prefix, distribute_list_prefix_cmd,
+DEFUN (distribute_list_prefix, distribute_list_prefix_cmd,
        "distribute-list prefix WORD (in|out) WORD",
        "Filter networks in routing updates\n"
        "Filter prefixes in routing updates\n"
@@ -541,7 +538,6 @@
        "Interface name\n")
 {
   enum distribute_type type;
-  struct distribute *dist;
 
   /* Check of distribute list type. */
   if (strncmp (argv[1], "i", 1) == 0)
@@ -556,12 +552,12 @@
     }
 
   /* Get interface name corresponding distribute list. */
-  dist = distribute_list_prefix_set (argv[2], type, argv[0]);
+  distribute_list_prefix_set (argv[2], type, argv[0]);
 
   return CMD_SUCCESS;
 }       
 
-ALIAS (districute_list_prefix, ipv6_distribute_list_prefix_cmd,
+ALIAS (distribute_list_prefix, ipv6_distribute_list_prefix_cmd,
        "distribute-list prefix WORD (in|out) WORD",
        "Filter networks in routing updates\n"
        "Filter prefixes in routing updates\n"
@@ -570,7 +566,7 @@
        "Filter outgoing routing updates\n"
        "Interface name\n")
 
-DEFUN (no_districute_list_prefix, no_distribute_list_prefix_cmd,
+DEFUN (no_distribute_list_prefix, no_distribute_list_prefix_cmd,
        "no distribute-list prefix WORD (in|out) WORD",
        NO_STR
        "Filter networks in routing updates\n"
@@ -604,7 +600,7 @@
   return CMD_SUCCESS;
 }       
 
-ALIAS (no_districute_list_prefix, no_ipv6_distribute_list_prefix_cmd,
+ALIAS (no_distribute_list_prefix, no_ipv6_distribute_list_prefix_cmd,
        "no distribute-list prefix WORD (in|out) WORD",
        NO_STR
        "Filter networks in routing updates\n"
@@ -762,22 +758,25 @@
                           (int (*) (const void *, const void *)) distribute_cmp);
 
   if(node==RIP_NODE) {
-    install_element (RIP_NODE, &distribute_list_all_cmd);
-    install_element (RIP_NODE, &no_distribute_list_all_cmd);
-    install_element (RIP_NODE, &distribute_list_cmd);
-    install_element (RIP_NODE, &no_distribute_list_cmd);
-    install_element (RIP_NODE, &distribute_list_prefix_all_cmd);
-    install_element (RIP_NODE, &no_distribute_list_prefix_all_cmd);
-    install_element (RIP_NODE, &distribute_list_prefix_cmd);
-    install_element (RIP_NODE, &no_distribute_list_prefix_cmd);
-  } else {
-    install_element (RIPNG_NODE, &ipv6_distribute_list_all_cmd);
-    install_element (RIPNG_NODE, &no_ipv6_distribute_list_all_cmd);
-    install_element (RIPNG_NODE, &ipv6_distribute_list_cmd);
-    install_element (RIPNG_NODE, &no_ipv6_distribute_list_cmd);
-    install_element (RIPNG_NODE, &ipv6_distribute_list_prefix_all_cmd);
-    install_element (RIPNG_NODE, &no_ipv6_distribute_list_prefix_all_cmd);
-    install_element (RIPNG_NODE, &ipv6_distribute_list_prefix_cmd);
-    install_element (RIPNG_NODE, &no_ipv6_distribute_list_prefix_cmd);
+    install_element (node, &distribute_list_all_cmd);
+    install_element (node, &no_distribute_list_all_cmd);
+    install_element (node, &distribute_list_cmd);
+    install_element (node, &no_distribute_list_cmd);
+    install_element (node, &distribute_list_prefix_all_cmd);
+    install_element (node, &no_distribute_list_prefix_all_cmd);
+    install_element (node, &distribute_list_prefix_cmd);
+    install_element (node, &no_distribute_list_prefix_cmd);
+  } else if (node == RIPNG_NODE || node == BABEL_NODE) {
+    /* WARNING: two identical commands installed do a crash, so be worry with
+     aliases. For this reason, and because all these commands are aliases, Babel
+     is not set with RIP. */
+    install_element (node, &ipv6_distribute_list_all_cmd);
+    install_element (node, &no_ipv6_distribute_list_all_cmd);
+    install_element (node, &ipv6_distribute_list_cmd);
+    install_element (node, &no_ipv6_distribute_list_cmd);
+    install_element (node, &ipv6_distribute_list_prefix_all_cmd);
+    install_element (node, &no_ipv6_distribute_list_prefix_all_cmd);
+    install_element (node, &ipv6_distribute_list_prefix_cmd);
+    install_element (node, &no_ipv6_distribute_list_prefix_cmd);
   }
 }
diff --git a/lib/distribute.h b/lib/distribute.h
index a1bec03..5072016 100644
--- a/lib/distribute.h
+++ b/lib/distribute.h
@@ -22,6 +22,9 @@
 #ifndef _ZEBRA_DISTRIBUTE_H
 #define _ZEBRA_DISTRIBUTE_H
 
+#include <zebra.h>
+#include "if.h"
+
 /* Disctirubte list types. */
 enum distribute_type
 {
diff --git a/lib/filter.c b/lib/filter.c
index af8d587..6934182 100644
--- a/lib/filter.c
+++ b/lib/filter.c
@@ -1337,13 +1337,13 @@
 
   master = access->master;
 
-  /* Delete all filter from access-list. */
-  access_list_delete (access);
-
   /* Run hook function. */
   if (master->delete_hook)
     (*master->delete_hook) (access);
  
+  /* Delete all filter from access-list. */
+  access_list_delete (access);
+
   return CMD_SUCCESS;
 }
 
@@ -1508,13 +1508,13 @@
 
   master = access->master;
 
-  /* Delete all filter from access-list. */
-  access_list_delete (access);
-
   /* Run hook function. */
   if (master->delete_hook)
     (*master->delete_hook) (access);
 
+  /* Delete all filter from access-list. */
+  access_list_delete (access);
+
   return CMD_SUCCESS;
 }
 
diff --git a/lib/if.c b/lib/if.c
index e9ef50b..e26aa04 100644
--- a/lib/if.c
+++ b/lib/if.c
@@ -431,19 +431,17 @@
   struct listnode *node;
   struct connected *c;
 
-  zlog_info ("Interface %s index %d metric %d mtu %d "
-#ifdef HAVE_IPV6
-             "mtu6 %d "
-#endif /* HAVE_IPV6 */
-             "%s",
-	     ifp->name, ifp->ifindex, ifp->metric, ifp->mtu, 
-#ifdef HAVE_IPV6
-	     ifp->mtu6,
-#endif /* HAVE_IPV6 */
-	     if_flag_dump (ifp->flags));
-  
   for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, c))
-    ;
+    zlog_info ("Interface %s index %d metric %d mtu %d "
+#ifdef HAVE_IPV6
+               "mtu6 %d "
+#endif /* HAVE_IPV6 */
+               "%s",
+               ifp->name, ifp->ifindex, ifp->metric, ifp->mtu, 
+#ifdef HAVE_IPV6
+               ifp->mtu6,
+#endif /* HAVE_IPV6 */
+               if_flag_dump (ifp->flags));
 }
 
 /* Interface printing for all interface. */
diff --git a/lib/if_rmap.c b/lib/if_rmap.c
index 9774be4..7d049b8 100644
--- a/lib/if_rmap.c
+++ b/lib/if_rmap.c
@@ -207,7 +207,6 @@
        "Route map interface name\n")
 {
   enum if_rmap_type type;
-  struct if_rmap *if_rmap;
 
   if (strncmp (argv[1], "i", 1) == 0)
     type = IF_RMAP_IN;
@@ -219,7 +218,7 @@
       return CMD_WARNING;
     }
 
-  if_rmap = if_rmap_set (argv[2], type, argv[0]);
+  if_rmap_set (argv[2], type, argv[0]);
 
   return CMD_SUCCESS;
 }      
diff --git a/lib/log.c b/lib/log.c
index df6e13d..cbc935b 100644
--- a/lib/log.c
+++ b/lib/log.c
@@ -1,6 +1,4 @@
 /*
- * $Id$
- *
  * Logging of zebra
  * Copyright (C) 1997, 1998, 1999 Kunihiro Ishiguro
  *
@@ -22,6 +20,8 @@
  * 02111-1307, USA.  
  */
 
+#define QUAGGA_DEFINE_DESC_TABLE
+
 #include <zebra.h>
 
 #include "log.h"
@@ -48,6 +48,7 @@
   "BGP",
   "OSPF",
   "RIPNG",
+  "BABEL",
   "OSPF6",
   "ISIS",
   "MASC",
@@ -776,7 +777,8 @@
  * provided otherwise.
  */
 const char *
-mes_lookup (const struct message *meslist, int max, int index, const char *none)
+mes_lookup (const struct message *meslist, int max, int index,
+  const char *none, const char *mesname)
 {
   int pos = index - meslist[0].key;
   
@@ -799,13 +801,13 @@
 	  {
 	    const char *str = (meslist->str ? meslist->str : none);
 	    
-	    zlog_debug ("message index %d [%s] found in position %d (max is %d)",
-		      index, str, i, max);
+	    zlog_debug ("message index %d [%s] found in %s at position %d (max is %d)",
+		      index, str, mesname, i, max);
 	    return str;
 	  }
       }
   }
-  zlog_err("message index %d not found (max is %d)", index, max);
+  zlog_err("message index %d not found in %s (max is %d)", index, mesname, max);
   assert (none);
   return none;
 }
@@ -818,29 +820,6 @@
   return (s != NULL) ? s : "Unknown error";
 }
 
-struct zebra_desc_table
-{
-  unsigned int type;
-  const char *string;
-  char chr;
-};
-
-#define DESC_ENTRY(T,S,C) [(T)] = { (T), (S), (C) }
-static const struct zebra_desc_table route_types[] = {
-  DESC_ENTRY	(ZEBRA_ROUTE_SYSTEM,	"system",	'X' ),
-  DESC_ENTRY	(ZEBRA_ROUTE_KERNEL,	"kernel",	'K' ),
-  DESC_ENTRY	(ZEBRA_ROUTE_CONNECT,	"connected",	'C' ),
-  DESC_ENTRY	(ZEBRA_ROUTE_STATIC,	"static",	'S' ),
-  DESC_ENTRY	(ZEBRA_ROUTE_RIP,	"rip",		'R' ),
-  DESC_ENTRY	(ZEBRA_ROUTE_RIPNG,	"ripng",	'R' ),
-  DESC_ENTRY	(ZEBRA_ROUTE_OSPF,	"ospf",		'O' ),
-  DESC_ENTRY	(ZEBRA_ROUTE_OSPF6,	"ospf6",	'O' ),
-  DESC_ENTRY	(ZEBRA_ROUTE_ISIS,	"isis",		'I' ),
-  DESC_ENTRY	(ZEBRA_ROUTE_BGP,	"bgp",		'B' ),
-  DESC_ENTRY	(ZEBRA_ROUTE_HSLS,	"hsls",		'H' ),
-};
-#undef DESC_ENTRY
-
 #define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' }
 static const struct zebra_desc_table command_types[] = {
   DESC_ENTRY	(ZEBRA_INTERFACE_ADD),
@@ -865,6 +844,7 @@
   DESC_ENTRY	(ZEBRA_ROUTER_ID_ADD),
   DESC_ENTRY	(ZEBRA_ROUTER_ID_DELETE),
   DESC_ENTRY	(ZEBRA_ROUTER_ID_UPDATE),
+  DESC_ENTRY	(ZEBRA_HELLO),
 };
 #undef DESC_ENTRY
 
@@ -930,4 +910,52 @@
        return route_types[i].type;
    return -1;
 }
+
 #undef RTSIZE
+
+int
+proto_redistnum(int afi, const char *s)
+{
+  if (! s)
+    return -1;
+
+  if (afi == AFI_IP)
+    {
+      if (strncmp (s, "k", 1) == 0)
+	return ZEBRA_ROUTE_KERNEL;
+      else if (strncmp (s, "c", 1) == 0)
+	return ZEBRA_ROUTE_CONNECT;
+      else if (strncmp (s, "s", 1) == 0)
+	return ZEBRA_ROUTE_STATIC;
+      else if (strncmp (s, "r", 1) == 0)
+	return ZEBRA_ROUTE_RIP;
+      else if (strncmp (s, "o", 1) == 0)
+	return ZEBRA_ROUTE_OSPF;
+      else if (strncmp (s, "i", 1) == 0)
+	return ZEBRA_ROUTE_ISIS;
+      else if (strncmp (s, "bg", 2) == 0)
+	return ZEBRA_ROUTE_BGP;
+      else if (strncmp (s, "ba", 2) == 0)
+	return ZEBRA_ROUTE_BABEL;
+    }
+  if (afi == AFI_IP6)
+    {
+      if (strncmp (s, "k", 1) == 0)
+	return ZEBRA_ROUTE_KERNEL;
+      else if (strncmp (s, "c", 1) == 0)
+	return ZEBRA_ROUTE_CONNECT;
+      else if (strncmp (s, "s", 1) == 0)
+	return ZEBRA_ROUTE_STATIC;
+      else if (strncmp (s, "r", 1) == 0)
+	return ZEBRA_ROUTE_RIPNG;
+      else if (strncmp (s, "o", 1) == 0)
+	return ZEBRA_ROUTE_OSPF6;
+      else if (strncmp (s, "i", 1) == 0)
+	return ZEBRA_ROUTE_ISIS;
+      else if (strncmp (s, "bg", 2) == 0)
+	return ZEBRA_ROUTE_BGP;
+      else if (strncmp (s, "ba", 2) == 0)
+	return ZEBRA_ROUTE_BABEL;
+    }
+  return -1;
+}
diff --git a/lib/log.h b/lib/log.h
index 2dd1d31..27f21b3 100644
--- a/lib/log.h
+++ b/lib/log.h
@@ -1,6 +1,4 @@
 /*
- * $Id$
- *
  * Zebra logging funcions.
  * Copyright (C) 1997, 1998, 1999 Kunihiro Ishiguro
  *
@@ -51,7 +49,8 @@
   ZLOG_RIP,
   ZLOG_BGP,
   ZLOG_OSPF,
-  ZLOG_RIPNG,  
+  ZLOG_RIPNG,
+  ZLOG_BABEL,
   ZLOG_OSPF6,
   ZLOG_ISIS,
   ZLOG_MASC
@@ -148,12 +147,12 @@
 extern int zlog_rotate (struct zlog *);
 
 /* For hackey massage lookup and check */
-#define LOOKUP(x, y) mes_lookup(x, x ## _max, y, "(no item found)")
+#define LOOKUP(x, y) mes_lookup(x, x ## _max, y, "(no item found)", #x)
 
 extern const char *lookup (const struct message *, int);
 extern const char *mes_lookup (const struct message *meslist, 
                                int max, int index,
-                               const char *no_item);
+                               const char *no_item, const char *mesname);
 
 extern const char *zlog_priority[];
 extern const char *zlog_proto_names[];
diff --git a/lib/memory.c b/lib/memory.c
index 4090fd9..684ebcf 100644
--- a/lib/memory.c
+++ b/lib/memory.c
@@ -466,6 +466,17 @@
   return CMD_SUCCESS;
 }
 
+DEFUN (show_memory_babel,
+       show_memory_babel_cmd,
+       "show memory babel",
+       SHOW_STR
+       "Memory statistics\n"
+       "Babel memory\n")
+{
+  show_memory_vty (vty, memory_list_babel);
+  return CMD_SUCCESS;
+}
+
 DEFUN (show_memory_bgp,
        show_memory_bgp_cmd,
        "show memory bgp",
@@ -518,6 +529,7 @@
   install_element (RESTRICTED_NODE, &show_memory_lib_cmd);
   install_element (RESTRICTED_NODE, &show_memory_rip_cmd);
   install_element (RESTRICTED_NODE, &show_memory_ripng_cmd);
+  install_element (RESTRICTED_NODE, &show_memory_babel_cmd);
   install_element (RESTRICTED_NODE, &show_memory_bgp_cmd);
   install_element (RESTRICTED_NODE, &show_memory_ospf_cmd);
   install_element (RESTRICTED_NODE, &show_memory_ospf6_cmd);
@@ -528,6 +540,7 @@
   install_element (VIEW_NODE, &show_memory_lib_cmd);
   install_element (VIEW_NODE, &show_memory_rip_cmd);
   install_element (VIEW_NODE, &show_memory_ripng_cmd);
+  install_element (VIEW_NODE, &show_memory_babel_cmd);
   install_element (VIEW_NODE, &show_memory_bgp_cmd);
   install_element (VIEW_NODE, &show_memory_ospf_cmd);
   install_element (VIEW_NODE, &show_memory_ospf6_cmd);
@@ -539,6 +552,7 @@
   install_element (ENABLE_NODE, &show_memory_zebra_cmd);
   install_element (ENABLE_NODE, &show_memory_rip_cmd);
   install_element (ENABLE_NODE, &show_memory_ripng_cmd);
+  install_element (ENABLE_NODE, &show_memory_babel_cmd);
   install_element (ENABLE_NODE, &show_memory_bgp_cmd);
   install_element (ENABLE_NODE, &show_memory_ospf_cmd);
   install_element (ENABLE_NODE, &show_memory_ospf6_cmd);
diff --git a/lib/memtypes.c b/lib/memtypes.c
index 69beb1c..2ded1d6 100644
--- a/lib/memtypes.c
+++ b/lib/memtypes.c
@@ -5,8 +5,6 @@
  *
  * The script is sensitive to the format (though not whitespace), see
  * the top of memtypes.awk for more details.
- *
- * $Id$
  */
 
 #include "zebra.h"
@@ -176,6 +174,13 @@
   { -1, NULL }
 };
 
+struct memory_list memory_list_babel[] =
+{
+  { MTYPE_BABEL,              "Babel structure"			},
+  { MTYPE_BABEL_IF,           "Babel interface"			},
+  { -1, NULL }
+};
+
 struct memory_list memory_list_ospf[] =
 {
   { MTYPE_OSPF_TOP,           "OSPF top"			},
diff --git a/lib/prefix.c b/lib/prefix.c
index 61a278c..a3b1adf 100644
--- a/lib/prefix.c
+++ b/lib/prefix.c
@@ -32,6 +32,139 @@
 static const u_char maskbit[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
 			         0xf8, 0xfc, 0xfe, 0xff};
 
+static const struct in6_addr maskbytes6[] =
+{
+  /* /0   */ { { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /1   */ { { { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /2   */ { { { 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /3   */ { { { 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /4   */ { { { 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /5   */ { { { 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /6   */ { { { 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /7   */ { { { 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /8   */ { { { 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /9   */ { { { 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /10  */ { { { 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /11  */ { { { 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /12  */ { { { 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /13  */ { { { 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /14  */ { { { 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /15  */ { { { 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /16  */ { { { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /17  */ { { { 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /18  */ { { { 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /19  */ { { { 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /20  */ { { { 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /21  */ { { { 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /22  */ { { { 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /23  */ { { { 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /24  */ { { { 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /25  */ { { { 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /26  */ { { { 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /27  */ { { { 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /28  */ { { { 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /29  */ { { { 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /30  */ { { { 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /31  */ { { { 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /32  */ { { { 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /33  */ { { { 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /34  */ { { { 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /35  */ { { { 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /36  */ { { { 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /37  */ { { { 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /38  */ { { { 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /39  */ { { { 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /40  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /41  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /42  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /43  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /44  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /45  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /46  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /47  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /48  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /49  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /50  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /51  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /52  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /53  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /54  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /55  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /56  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /57  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /58  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /59  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /60  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /61  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /62  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /63  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /64  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /65  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /66  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /67  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /68  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /69  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /70  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /71  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /72  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /73  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /74  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /75  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /76  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /77  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /78  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /79  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /80  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /81  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /82  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /83  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /84  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /85  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /86  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /87  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /88  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /89  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /90  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /91  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /92  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /93  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /94  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /95  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /96  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 } } },
+  /* /97  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00 } } },
+  /* /98  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00 } } },
+  /* /99  */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00 } } },
+  /* /100 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00 } } },
+  /* /101 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00 } } },
+  /* /102 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00 } } },
+  /* /103 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00 } } },
+  /* /104 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00 } } },
+  /* /105 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00 } } },
+  /* /106 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00 } } },
+  /* /107 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00 } } },
+  /* /108 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00 } } },
+  /* /109 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00 } } },
+  /* /110 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00 } } },
+  /* /111 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00 } } },
+  /* /112 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00 } } },
+  /* /113 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00 } } },
+  /* /114 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00 } } },
+  /* /115 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00 } } },
+  /* /116 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00 } } },
+  /* /117 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00 } } },
+  /* /118 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00 } } },
+  /* /119 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00 } } },
+  /* /120 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 } } },
+  /* /121 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 } } },
+  /* /122 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0 } } },
+  /* /123 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0 } } },
+  /* /124 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0 } } },
+  /* /125 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8 } } },
+  /* /126 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc } } },
+  /* /127 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe } } },
+  /* /128 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } }
+};
+
 /* Number of bits in prefix type. */
 #ifndef PNBBY
 #define PNBBY 8
@@ -39,6 +172,21 @@
 
 #define MASKBIT(offset)  ((0xff << (PNBBY - (offset))) & 0xff)
 
+unsigned int
+prefix_bit (const u_char *prefix, const u_char prefixlen)
+{
+  unsigned int offset = prefixlen / 8;
+  unsigned int shift  = 7 - (prefixlen % 8);
+  
+  return (prefix[offset] >> shift) & 1;
+}
+
+unsigned int
+prefix6_bit (const struct in6_addr *prefix, const u_char prefixlen)
+{
+  return prefix_bit((const u_char *) &prefix->s6_addr, prefixlen);
+}
+
 /* Address Famiy Identifier to Address Family converter. */
 int
 afi2family (afi_t afi)
@@ -133,11 +281,11 @@
   if (p1->family == p2->family && p1->prefixlen == p2->prefixlen)
     {
       if (p1->family == AF_INET)
-	if (IPV4_ADDR_SAME (&p1->u.prefix, &p2->u.prefix))
+	if (IPV4_ADDR_SAME (&p1->u.prefix4.s_addr, &p2->u.prefix4.s_addr))
 	  return 1;
 #ifdef HAVE_IPV6
       if (p1->family == AF_INET6 )
-	if (IPV6_ADDR_SAME (&p1->u.prefix, &p2->u.prefix))
+	if (IPV6_ADDR_SAME (&p1->u.prefix6.s6_addr, &p2->u.prefix6.s6_addr))
 	  return 1;
 #endif /* HAVE_IPV6 */
     }
@@ -167,8 +315,8 @@
   if (p1->family != p2->family || p1->prefixlen != p2->prefixlen)
     return 1;
 
-  offset = p1->prefixlen / 8;
-  shift = p1->prefixlen % 8;
+  offset = p1->prefixlen / PNBBY;
+  shift = p1->prefixlen % PNBBY;
 
   if (shift)
     if (maskbit[shift] & (pp1[offset] ^ pp2[offset]))
@@ -181,6 +329,46 @@
   return 0;
 }
 
+/*
+ * Count the number of common bits in 2 prefixes. The prefix length is
+ * ignored for this function; the whole prefix is compared. If the prefix
+ * address families don't match, return -1; otherwise the return value is
+ * in range 0 ... maximum prefix length for the address family.
+ */
+int
+prefix_common_bits (const struct prefix *p1, const struct prefix *p2)
+{
+  int pos, bit;
+  int length = 0;
+  u_char xor;
+
+  /* Set both prefix's head pointer. */
+  const u_char *pp1 = (const u_char *)&p1->u.prefix;
+  const u_char *pp2 = (const u_char *)&p2->u.prefix;
+
+  if (p1->family == AF_INET)
+    length = IPV4_MAX_BYTELEN;
+#ifdef HAVE_IPV6
+  if (p1->family == AF_INET6)
+    length = IPV6_MAX_BYTELEN;
+#endif
+  if (p1->family != p2->family || !length)
+    return -1;
+
+  for (pos = 0; pos < length; pos++)
+    if (pp1[pos] != pp2[pos])
+      break;
+  if (pos == length)
+    return pos * 8;
+
+  xor = pp1[pos] ^ pp2[pos];
+  for (bit = 0; bit < 8; bit++)
+    if (xor & (1 << (7 - bit)))
+      break;
+
+  return pos * 8 + bit;
+}
+
 /* Return prefix family type string. */
 const char *
 prefix_family_str (const struct prefix *p)
@@ -261,25 +449,20 @@
   return ret;
 }
 
-/* Convert masklen into IP address's netmask. */
+/* Convert masklen into IP address's netmask (network byte order). */
 void
-masklen2ip (int masklen, struct in_addr *netmask)
+masklen2ip (const int masklen, struct in_addr *netmask)
 {
-  u_char *pnt;
-  int bit;
-  int offset;
+  assert (masklen >= 0 && masklen <= IPV4_MAX_BITLEN);
 
-  memset (netmask, 0, sizeof (struct in_addr));
-  pnt = (unsigned char *) netmask;
+  /* left shift is only defined for less than the size of the type.
+   * we unconditionally use long long in case the target platform
+   * has defined behaviour for << 32 (or has a 64-bit left shift) */
 
-  offset = masklen / 8;
-  bit = masklen % 8;
-  
-  while (offset--)
-    *pnt++ = 0xff;
-
-  if (bit)
-    *pnt = maskbit[bit];
+  if (sizeof(unsigned long long) > 4)
+    netmask->s_addr = htonl(0xffffffffULL << (32 - masklen));
+  else
+    netmask->s_addr = htonl(masklen ? 0xffffffffU << (32 - masklen) : 0);
 }
 
 /* Convert IP address's netmask into integer. We assume netmask is
@@ -287,54 +470,22 @@
 u_char
 ip_masklen (struct in_addr netmask)
 {
-  u_char len;
-  u_char *pnt;
-  u_char *end;
-  u_char val;
-
-  len = 0;
-  pnt = (u_char *) &netmask;
-  end = pnt + 4;
-
-  while ((pnt < end) && (*pnt == 0xff))
-    {
-      len+= 8;
-      pnt++;
-    } 
-
-  if (pnt < end)
-    {
-      val = *pnt;
-      while (val)
-	{
-	  len++;
-	  val <<= 1;
-	}
-    }
-  return len;
+  uint32_t tmp = ~ntohl(netmask.s_addr);
+  if (tmp)
+    /* clz: count leading zeroes. sadly, the behaviour of this builtin
+     * is undefined for a 0 argument, even though most CPUs give 32 */
+    return __builtin_clz(tmp);
+  else
+    return 32;
 }
 
-/* Apply mask to IPv4 prefix. */
+/* Apply mask to IPv4 prefix (network byte order). */
 void
 apply_mask_ipv4 (struct prefix_ipv4 *p)
 {
-  u_char *pnt;
-  int index;
-  int offset;
-
-  index = p->prefixlen / 8;
-
-  if (index < 4)
-    {
-      pnt = (u_char *) &p->prefix;
-      offset = p->prefixlen % 8;
-
-      pnt[index] &= maskbit[offset];
-      index++;
-
-      while (index < 4)
-	pnt[index++] = 0;
-    }
+  struct in_addr mask;
+  masklen2ip(p->prefixlen, &mask);
+  p->prefix.s_addr &= mask.s_addr;
 }
 
 /* If prefix is 0.0.0.0/0 then return 1 else return 0. */
@@ -396,7 +547,7 @@
       if (ret == 0)
 	return 0;
       plen = (u_char) atoi (++pnt);
-      if (plen > 128)
+      if (plen > IPV6_MAX_BITLEN)
 	return 0;
       p->prefixlen = plen;
     }
@@ -416,13 +567,13 @@
   
   pnt = (unsigned char *) & netmask;
 
-  while ((*pnt == 0xff) && len < 128) 
+  while ((*pnt == 0xff) && len < IPV6_MAX_BITLEN)
     {
       len += 8;
       pnt++;
     } 
   
-  if (len < 128) 
+  if (len < IPV6_MAX_BITLEN)
     {
       val = *pnt;
       while (val) 
@@ -435,23 +586,10 @@
 }
 
 void
-masklen2ip6 (int masklen, struct in6_addr *netmask)
+masklen2ip6 (const int masklen, struct in6_addr *netmask)
 {
-  unsigned char *pnt;
-  int bit;
-  int offset;
-
-  memset (netmask, 0, sizeof (struct in6_addr));
-  pnt = (unsigned char *) netmask;
-
-  offset = masklen / 8;
-  bit = masklen % 8;
-
-  while (offset--)
-    *pnt++ = 0xff;
-
-  if (bit)
-    *pnt = maskbit[bit];
+  assert (masklen >= 0 && masklen <= IPV6_MAX_BITLEN);
+  memcpy (netmask, maskbytes6 + masklen, sizeof (struct in6_addr));
 }
 
 void
@@ -570,6 +708,20 @@
   return NULL;
 }
 
+void
+prefix2sockunion (const struct prefix *p, union sockunion *su)
+{
+  memset (su, 0, sizeof (*su));
+
+  su->sa.sa_family = p->family;
+  if (p->family == AF_INET)
+    su->sin.sin_addr = p->u.prefix4;
+#ifdef HAVE_IPV6
+  if (p->family == AF_INET6)
+    memcpy (&su->sin6.sin6_addr, &p->u.prefix6, sizeof (struct in6_addr));
+#endif /* HAVE_IPV6 */
+}
+
 int
 prefix_blen (const struct prefix *p)
 {
diff --git a/lib/prefix.h b/lib/prefix.h
index 5f1ff05..7f0d360 100644
--- a/lib/prefix.h
+++ b/lib/prefix.h
@@ -112,6 +112,7 @@
 #define IPV4_NET0(a)    ((((u_int32_t) (a)) & 0xff000000) == 0x00000000)
 #define IPV4_NET127(a)  ((((u_int32_t) (a)) & 0xff000000) == 0x7f000000)
 #define IPV4_LINKLOCAL(a) ((((u_int32_t) (a)) & 0xffff0000) == 0xa9fe0000)
+#define IPV4_CLASS_DE(a)  ((((u_int32_t) (a)) & 0xe0000000) == 0xe0000000)
 
 /* Max bit/byte length of IPv6 address. */
 #define IPV6_MAX_BYTELEN    16
@@ -127,26 +128,14 @@
 /* Prefix's family member. */
 #define PREFIX_FAMILY(p)  ((p)->family)
 
-/* Check bit of the prefix. */
-static inline unsigned int
-prefix_bit (const u_char *prefix, const u_char prefixlen)
-{
-  unsigned int offset = prefixlen / 8;
-  unsigned int shift  = 7 - (prefixlen % 8);
-
-  return (prefix[offset] >> shift) & 1;
-}
-
-static inline unsigned int
-prefix6_bit (const struct in6_addr *prefix, const u_char prefixlen)
-{
-  return prefix_bit((const u_char *) &prefix->s6_addr, prefixlen);
-}
-
 /* Prototypes. */
 extern int afi2family (afi_t);
 extern afi_t family2afi (int);
 
+/* Check bit of the prefix. */
+extern unsigned int prefix_bit (const u_char *prefix, const u_char prefixlen);
+extern unsigned int prefix6_bit (const struct in6_addr *prefix, const u_char prefixlen);
+
 extern struct prefix *prefix_new (void);
 extern void prefix_free (struct prefix *);
 extern const char *prefix_family_str (const struct prefix *);
@@ -156,12 +145,14 @@
 extern int prefix_match (const struct prefix *, const struct prefix *);
 extern int prefix_same (const struct prefix *, const struct prefix *);
 extern int prefix_cmp (const struct prefix *, const struct prefix *);
+extern int prefix_common_bits (const struct prefix *, const struct prefix *);
 extern void prefix_copy (struct prefix *dest, const struct prefix *src);
 extern void apply_mask (struct prefix *);
 
 extern struct prefix *sockunion2prefix (const union sockunion *dest,
                                         const union sockunion *mask);
 extern struct prefix *sockunion2hostprefix (const union sockunion *);
+extern void prefix2sockunion (const struct prefix *, union sockunion *);
 
 extern struct prefix_ipv4 *prefix_ipv4_new (void);
 extern void prefix_ipv4_free (struct prefix_ipv4 *);
@@ -175,7 +166,7 @@
 extern void apply_classful_mask_ipv4 (struct prefix_ipv4 *);
 
 extern u_char ip_masklen (struct in_addr);
-extern void masklen2ip (int, struct in_addr *);
+extern void masklen2ip (const int, struct in_addr *);
 /* returns the network portion of the host address */
 extern in_addr_t ipv4_network_addr (in_addr_t hostaddr, int masklen);
 /* given the address of a host on a network and the network mask length,
@@ -196,7 +187,7 @@
 	*((struct prefix_ipv6 *)(DST)) = *((const struct prefix_ipv6 *)(SRC));
 
 extern int ip6_masklen (struct in6_addr);
-extern void masklen2ip6 (int, struct in6_addr *);
+extern void masklen2ip6 (const int, struct in6_addr *);
 
 extern void str2in6_addr (const char *, struct in6_addr *);
 extern const char *inet6_ntoa (struct in6_addr);
diff --git a/lib/route_types.awk b/lib/route_types.awk
deleted file mode 100644
index eb3d382..0000000
--- a/lib/route_types.awk
+++ /dev/null
@@ -1,187 +0,0 @@
-# $Id$
-#
-# Scan a file of route-type definitions (see eg route_types.txt) and
-# generate a corresponding header file with:
-#
-# - enum of Zserv route-types
-# - redistribute strings for the various Quagga daemons
-#
-# See route_types.txt for the format.
-#
-#
-
-BEGIN {
-	FS="[,]";
-	
-	# globals
-	exitret = 0;
-	tcount = 0;
-	
-	# formats for output
-	## the define format
-	redist_def_fmt = "#define QUAGGA_REDIST_STR_%s \\\n";
-	## DEFUN/vty route-type argument
-	redist_str_fmt = "\"(%s)\"\n";
-	redist_help_def_fmt = "#define QUAGGA_REDIST_HELP_STR_%s";
-	redist_help_str_fmt = " \\\n  \"%s\\n\"";
-	
-	# header
-	header = "/* Auto-generated from route_types.txt by " ARGV[0] ". */\n";
-	header = header "/* Do not edit! */\n";
-	header = header "\n#ifndef _QUAGGA_ROUTE_TYPES_H\n";
-	header = header "#define _QUAGGA_ROUTE_TYPES_H\n";
-	footer = "#endif /* _QUAGGA_ROUTE_TYPES_H */\n";
-	printf ("%s\n", header);
-}
-
-# Chomp comment lines
-($0 ~ /^#/) { 
-	next;
-}
-
-# get rid of the commas, leading/trailling whitespace and
-# quotes
-{
-	for (i = 1; i <= NF; i++) {
-		#print "before:" $i;
-		$i = gensub(/^[[:blank:]]*(.*)[,]*.*/, "\\1", "g",$i);
-		$i = gensub(/^["](.*)["]$/, "\\1", "g", $i);
-		#print "after :" $i;
-	}
-}
-
-# 7 field format:
-#  type                 cname      daemon  C    4  6  short help
-(NF >= 7) {
-	#print "7", $1, $0;
-	
-	if ($1 in types) {
-		print "error: attempt to redefine", $1;
-		exitret = 1;
-		exit exitret;
-	}
-	
-	typesbynum[tcount] = $1;
-	types[$1,"num"] = tcount++; 
-	types[$1,"cname"] = $2;
-	types[$1,"daemon"] = $3;
-	types[$1,"C"] = $4;
-	types[$1,"4"] = strtonum($5);
-	types[$1,"6"] = strtonum($6);
-	types[$1,"shelp"] = $7;
-	
-	#print "num   :", types[$1,"num"]
-	#print "cname :", types[$1,"cname"]
-	#print "daemon:", types[$1,"daemon"];
-	#print "char  :", types[$1,"C"];
-};
-
-# 2 field: type "long description"
-(NF == 2) {
-	#print "2", $1, $2;
-	
-	if (!(($1 SUBSEP "num") in types)) {
-		print "error: type", $1, "must be defined before help str";
-		exitret = 2;
-		exit exitret;
-	}
-	
-	types[$1,"lhelp"] = $2;
-}
-
-END {
-	if (exitret)
-		exit exitret;
-	
-	# The enums
-	# not yet...
-	#printf("enum\n{\n");
-	#for (i = 0; i < tcount; i++) {
-	#	type = typesbynum[i];
-	#	if (type != "" && types[type,"num"] == i)
-	#		printf ("  %s,\n", type);
-	#}
-	#printf ("  ZEBRA_ROUTE_MAX,\n};\n\n");
-	
-	# the redistribute defines
-	for (i = 0; i < tcount; i++) {
-		type = typesbynum[i];
-		
-		# must be a type, and must cross-check against recorded type
-		if (type == "" || types[type,"num"] != i)
-			continue;
-		
-		# ignore route types that can't be redistributed
-		if (!(types[type,"4"] || types[type,"6"]))
-			continue;
-		
-		# must have a daemon name
-		if (!((type SUBSEP "daemon") in types))
-			continue;
-		if (!(daemon = types[type,"daemon"]))
-			continue;
-		
-		# might have done this daemon already?
-		if (daemon in seen_daemons)
-			continue;
-		
-		cname = types[type,"cname"];
-		all = all "|" cname;
-		rstr = "";
-		hstr = "";
-		
-		# add it to the others
-		for (j = 0; j < tcount; j++) {
-			# ignore self
-			if (i == j)
-				continue;
-			
-			type2 = typesbynum[j];
-			
-			# type2 must be valid, and self-check.
-			if (type2 == "" || types[type2,"num"] != j)
-				continue;
-			
-			# ignore different route types for the same daemon
-			# (eg system/kernel/connected)
-			if (types[type2,"daemon"] == daemon)
-				continue;
-			
-			if ((types[type2,"4"] && types[type,"4"]) \
-			    || (types[type2,"6"] && types[type,"6"])) {
-			    	
-			    	if (rstr == "")
-			    		rstr = types[type2,"cname"];
-				else
-					rstr = rstr "|" types[type2,"cname"];
-				
-				if ((type2 SUBSEP "lhelp") in types)
-				  hstr2 = types[type2,"lhelp"];
-				else if ((type2 SUBSEP "shelp") in types)
-				  hstr2 = types[type2,"shelp"];
-				else
-				  hstr2 = types[type2,"cname"];
-				
-				hstr = hstr sprintf(redist_help_str_fmt, hstr2);
-			}
-		}
-		
-		# dont double-process daemons.
-		seen_daemons[daemon] = 1;
-		
-		printf("/* %s */\n", daemon);
-		printf(redist_def_fmt, toupper(daemon));
-		printf(redist_str_fmt, rstr);
-		printf(redist_help_def_fmt, toupper(daemon));
-		printf("%s", hstr);
-		printf("\n\n");
-	}
-	
-	#printf("#define QUAGGA_REDIST_STR_ALL %s\n",all);
-			
-#	for (i = 0; i < lcount; i++) {
-#		if (mlists[i] != "")
-#			printf (mlistformat "\n", mlists[i]);
-#	}
-	printf (footer);
-}
diff --git a/lib/route_types.pl b/lib/route_types.pl
new file mode 100755
index 0000000..e1595af
--- /dev/null
+++ b/lib/route_types.pl
@@ -0,0 +1,199 @@
+#!/usr/bin/perl
+##
+## Scan a file of route-type definitions (see eg route_types.txt) and
+## generate a corresponding header file with:
+##
+## - enum of Zserv route-types
+## - redistribute strings for the various Quagga daemons
+##
+## See route_types.txt for the format.
+##
+##
+## Copyright (C) 2009 David Lamparter.
+## This file is part of GNU Zebra.
+##
+## GNU Zebra 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, or (at your option) any
+## later version.
+##
+## GNU Zebra is distributed in the hope that it will be useful, but
+## WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+## General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with GNU Zebra; see the file COPYING.  If not, write to the Free
+## Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+## 02111-1307, USA.
+##
+
+use strict;
+
+# input processing
+#
+my @protos;
+my %protodetail;
+
+my %daemons;
+
+while (<STDIN>) {
+	# skip comments and empty lines
+	next if (/^\s*(#|$)/);
+
+	# strip whitespace
+	chomp;
+	$_ =~ s/^\s*//;
+	$_ =~ s/\s*$//;
+
+	# match help strings
+	if (/^(ZEBRA_ROUTE_[^\s]+)\s*,\s*"(.*)"$/) {
+		$protodetail{$1}->{'longhelp'} = $2;
+		next;
+	}
+
+	$_ =~ s/\s*,\s*/,/g;
+
+	# else: 7-field line
+	my @f = split(/,/, $_);
+	unless (@f == 7) {
+		die "invalid input on route_types line $.\n";
+	}
+
+	my $proto = $f[0];
+	$f[3] = $1 if ($f[3] =~ /^'(.*)'$/);
+	$f[6] = $1 if ($f[6] =~ /^"(.*)"$/);
+
+	$protodetail{$proto} = {
+		"number" => scalar @protos,
+		"type" => $f[0],
+		"cname" => $f[1],
+		"daemon" => $f[2],
+		"char" => $f[3],
+		"ipv4" => int($f[4]),
+		"ipv6" => int($f[5]),
+		"shorthelp" => $f[6],
+	};
+	push @protos, $proto;
+	$daemons{$f[2]} = {
+		"ipv4" => int($f[4]),
+		"ipv6" => int($f[5])
+	} unless ($f[2] eq "NULL");
+}
+
+# output
+printf <<EOF, $ARGV[0];
+/* Auto-generated from route_types.txt by %s. */
+/* Do not edit! */
+
+#ifndef _QUAGGA_ROUTE_TYPES_H
+#define _QUAGGA_ROUTE_TYPES_H
+
+/* Zebra route's types. */
+EOF
+
+push @protos, "ZEBRA_ROUTE_MAX";
+my (@protosv4, @protosv6) = ((), ());
+for (my $c = 0; $c < @protos; $c++) {
+	my $p = $protos[$c];
+	printf "#define %-32s %d\n", $p, $c;
+	push @protosv4, $p if ($protodetail{$p}->{"ipv4"});
+	push @protosv6, $p if ($protodetail{$p}->{"ipv6"});
+}
+pop @protos;
+
+sub codelist {
+	my (@protos) = @_;
+	my (@lines) = ();
+	my $str = "  \"Codes: ";
+	for my $p (@protos) {
+		my $s = sprintf("%s - %s, ",
+			$protodetail{$p}->{"char"},
+			$protodetail{$p}->{"shorthelp"});
+		if (length($str . $s) > 70) {
+			$str =~ s/ $//;
+			push @lines, $str . "%s\" \\\n";
+			$str = "  \"       ";
+		}
+		$str .= $s;
+	}
+	$str =~ s/ $//;
+	push @lines, $str . "%s\" \\\n";
+	push @lines, "  \"       > - selected route, * - FIB route%s%s\", \\\n";
+	my @nl = ();
+	for (my $c = 0; $c < @lines + 1; $c++) {
+		push @nl, "VTY_NEWLINE"
+	}
+	return join("", @lines) ."  ". join(", ", @nl);
+}
+
+print "\n";
+printf "#define SHOW_ROUTE_V4_HEADER \\\n%s\n", codelist(@protosv4);
+printf "#define SHOW_ROUTE_V6_HEADER \\\n%s\n", codelist(@protosv6);
+print "\n";
+
+sub collect {
+	my ($daemon, $ipv4, $ipv6) = @_;
+	my (@names, @help) = ((), ());
+	for my $p (@protos) {
+		next if ($protodetail{$p}->{"daemon"} eq $daemon && $daemon ne "zebra");
+		next unless (($ipv4 && $protodetail{$p}->{"ipv4"})
+				|| ($ipv6 && $protodetail{$p}->{"ipv6"}));
+		push @names, $protodetail{$p}->{"cname"};
+		push @help, "  \"".$protodetail{$p}->{"longhelp"}."\\n\"";
+	}
+	return ("\"(" . join("|", @names) . ")\"", join(" \\\n", @help));
+}
+
+for my $daemon (sort keys %daemons) {
+	next unless ($daemons{$daemon}->{"ipv4"} || $daemons{$daemon}->{"ipv6"});
+	printf "/* %s */\n", $daemon;
+	if ($daemons{$daemon}->{"ipv4"} && $daemons{$daemon}->{"ipv6"}) {
+		my ($names, $help) = collect($daemon, 1, 1);
+		printf "#define QUAGGA_REDIST_STR_%s \\\n  %s\n", uc $daemon, $names;
+		printf "#define QUAGGA_REDIST_HELP_STR_%s \\\n%s\n", uc $daemon, $help;
+		($names, $help) = collect($daemon, 1, 0);
+		printf "#define QUAGGA_IP_REDIST_STR_%s \\\n  %s\n", uc $daemon, $names;
+		printf "#define QUAGGA_IP_REDIST_HELP_STR_%s \\\n%s\n", uc $daemon, $help;
+		($names, $help) = collect($daemon, 0, 1);
+		printf "#define QUAGGA_IP6_REDIST_STR_%s \\\n  %s\n", uc $daemon, $names;
+		printf "#define QUAGGA_IP6_REDIST_HELP_STR_%s \\\n%s\n", uc $daemon, $help;
+	} else {
+		my ($names, $help) = collect($daemon,
+			$daemons{$daemon}->{"ipv4"}, $daemons{$daemon}->{"ipv6"});
+		printf "#define QUAGGA_REDIST_STR_%s \\\n  %s\n", uc $daemon, $names;
+		printf "#define QUAGGA_REDIST_HELP_STR_%s \\\n%s\n", uc $daemon, $help;
+	}
+	print "\n";
+}
+
+print <<EOF;
+
+#ifdef QUAGGA_DEFINE_DESC_TABLE
+
+struct zebra_desc_table
+{
+  unsigned int type;
+  const char *string;
+  char chr;
+};
+
+#define DESC_ENTRY(T,S,C) [(T)] = { (T), (S), (C) }
+static const struct zebra_desc_table route_types[] = {
+EOF
+
+for (my $c = 0; $c < @protos; $c++) {
+	my $p = $protos[$c];
+	printf "  DESC_ENTRY\t(%s\t \"%s\",\t'%s' ),\n",
+	       $p.",", $protodetail{$p}->{"cname"}, $protodetail{$p}->{"char"};
+}
+
+print <<EOF;
+};
+#undef DESC_ENTRY
+
+#endif /* QUAGGA_DEFINE_DESC_TABLE */
+
+#endif /* _QUAGGA_ROUTE_TYPES_H */
+EOF
+
diff --git a/lib/route_types.txt b/lib/route_types.txt
index e99cacd..cebf01f 100644
--- a/lib/route_types.txt
+++ b/lib/route_types.txt
@@ -42,13 +42,13 @@
 
 ##  type                cname      daemon  C    4  6  short help
 ZEBRA_ROUTE_SYSTEM,     system,    NULL,   'X', 0, 0, "Reserved"
-ZEBRA_ROUTE_KERNEL,     kernel,    zebra,  'K', 1, 1, NULL
-ZEBRA_ROUTE_CONNECT,    connected, zebra,  'C', 1, 1, NULL
-ZEBRA_ROUTE_STATIC,     static,    zebra,  'S', 1, 1, NULL
+ZEBRA_ROUTE_KERNEL,     kernel,    zebra,  'K', 1, 1, "kernel route"
+ZEBRA_ROUTE_CONNECT,    connected, zebra,  'C', 1, 1, "connected"
+ZEBRA_ROUTE_STATIC,     static,    zebra,  'S', 1, 1, "static"
 ZEBRA_ROUTE_RIP,        rip,       ripd,   'R', 1, 0, "RIP"
 ZEBRA_ROUTE_RIPNG,      ripng,     ripngd, 'R', 0, 1, "RIPng"
 ZEBRA_ROUTE_OSPF,       ospf,      ospfd,  'O', 1, 0, "OSPF"
-ZEBRA_ROUTE_OSPF6,      ospf6,     ospf6d, 'O', 0, 1, "OSPF"
+ZEBRA_ROUTE_OSPF6,      ospf6,     ospf6d, 'O', 0, 1, "OSPFv6"
 ZEBRA_ROUTE_ISIS,       isis,      isisd,  'I', 1, 1, "IS-IS"
 ZEBRA_ROUTE_BGP,        bgp,       bgpd,   'B', 1, 1, "BGP"
 # HSLS and OLSR both are AFI independent (so: 1, 1), however
@@ -57,7 +57,8 @@
 # to 'switch on' redist support (direct numeric entry remaining
 # possible).
 ZEBRA_ROUTE_HSLS,       hsls,      hslsd,  'H', 0, 0, "HSLS"
-ZEBRA_ROUTE_OLSR,       olsr,      oslrd,  'o', 0, 0, "OLSR"
+ZEBRA_ROUTE_OLSR,       olsr,      olsrd,  'o', 0, 0, "OLSR"
+ZEBRA_ROUTE_BABEL,      babel,     babeld, 'A', 1, 1, "Babel"
 
 ## help strings
 ZEBRA_ROUTE_SYSTEM, "Reserved route type, for internal use only"
@@ -72,3 +73,4 @@
 ZEBRA_ROUTE_BGP,    "Border Gateway Protocol (BGP)"
 ZEBRA_ROUTE_HSLS,   "Hazy-Sighted Link State Protocol (HSLS)"
 ZEBRA_ROUTE_OLSR,   "Optimised Link State Routing (OLSR)"
+ZEBRA_ROUTE_BABEL,  "Babel routing protocol (Babel)"
diff --git a/lib/routemap.h b/lib/routemap.h
index 1402f5c..ba64553 100644
--- a/lib/routemap.h
+++ b/lib/routemap.h
@@ -43,6 +43,7 @@
 {
   RMAP_RIP,
   RMAP_RIPNG,
+  RMAP_BABEL,
   RMAP_OSPF,
   RMAP_OSPF6,
   RMAP_BGP,
diff --git a/lib/smux.c b/lib/smux.c
index 1941cf8..b7cd18d 100644
--- a/lib/smux.c
+++ b/lib/smux.c
@@ -270,7 +270,6 @@
 smux_getresp_send (oid objid[], size_t objid_len, long reqid, long errstat,
 		   long errindex, u_char val_type, void *arg, size_t arg_len)
 {
-  int ret;
   u_char buf[BUFSIZ];
   u_char *ptr, *h1, *h1e, *h2, *h2e;
   size_t len, length;
@@ -326,7 +325,7 @@
   if (debug_smux)
     zlog_debug ("SMUX getresp send: %td", (ptr - buf));
   
-  ret = send (smux_sock, buf, (ptr - buf), 0);
+  send (smux_sock, buf, (ptr - buf), 0);
 }
 
 static u_char *
@@ -946,7 +945,7 @@
   u_char *ptr;
   size_t len;
   long version;
-  u_char progname[] = QUAGGA_PROGNAME "-" QUAGGA_VERSION;
+  const char progname[] = QUAGGA_PROGNAME "-" QUAGGA_VERSION;
 
   if (debug_smux)
     {
@@ -977,7 +976,7 @@
   ptr = asn_build_string (ptr, &len, 
 			  (u_char)
 			  (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OCTET_STR),
-			  progname, strlen (progname));
+			  (const u_char *) progname, strlen (progname));
 
   /* SMUX connection password. */
   ptr = asn_build_string (ptr, &len, 
diff --git a/lib/sockopt.c b/lib/sockopt.c
index 55c6226..be22827 100644
--- a/lib/sockopt.c
+++ b/lib/sockopt.c
@@ -179,12 +179,25 @@
   
   return pktinfo->ipi6_ifindex;
 }
-#endif /* HAVE_IPV6 */
 
+int
+setsockopt_ipv6_tclass(int sock, int tclass)
+{
+  int ret = 0;
+
+#ifdef IPV6_TCLASS /* RFC3542 */
+  ret = setsockopt (sock, IPPROTO_IPV6, IPV6_TCLASS, &tclass, sizeof (tclass));
+  if (ret < 0)
+    zlog_warn ("Can't set IPV6_TCLASS option for fd %d to %#x: %s",
+	       sock, tclass, safe_strerror(errno));
+#endif
+  return ret;
+}
+#endif /* HAVE_IPV6 */
 
 /*
  * Process multicast socket options for IPv4 in an OS-dependent manner.
- * Supported options are IP_MULTICAST_IF and IP_{ADD,DROP}_MEMBERSHIP.
+ * Supported options are IP_{ADD,DROP}_MEMBERSHIP.
  *
  * Many operating systems have a limit on the number of groups that
  * can be joined per socket (where each group and local address
@@ -204,121 +217,133 @@
  * allow leaves, or implicitly leave all groups joined to down interfaces.
  */
 int
-setsockopt_multicast_ipv4(int sock, 
+setsockopt_ipv4_multicast(int sock,
 			int optname, 
-			struct in_addr if_addr /* required */,
 			unsigned int mcast_addr,
-			unsigned int ifindex /* optional: if non-zero, may be
-						  used instead of if_addr */)
+			unsigned int ifindex)
 {
+#ifdef HAVE_RFC3678
+  struct group_req gr;
+  struct sockaddr_in *si;
+  int ret;
+  memset (&gr, 0, sizeof(gr));
+  si = (struct sockaddr_in *)&gr.gr_group;
+  gr.gr_interface = ifindex;
+  si->sin_family = AF_INET;
+#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
+  si->sin_len = sizeof(struct sockaddr_in);
+#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
+  si->sin_addr.s_addr = mcast_addr;
+  ret = setsockopt(sock, IPPROTO_IP, (optname == IP_ADD_MEMBERSHIP) ? 
+    MCAST_JOIN_GROUP : MCAST_LEAVE_GROUP, (void *)&gr, sizeof(gr));
+  if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP) && (errno == EADDRINUSE))
+    {
+      setsockopt(sock, IPPROTO_IP, MCAST_LEAVE_GROUP, (void *)&gr, sizeof(gr));
+      ret = setsockopt(sock, IPPROTO_IP, MCAST_JOIN_GROUP, (void *)&gr, sizeof(gr));
+    }
+  return ret;
 
-#ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
-  /* This is better because it uses ifindex directly */
+#elif defined(HAVE_STRUCT_IP_MREQN_IMR_IFINDEX) && !defined(__FreeBSD__)
   struct ip_mreqn mreqn;
   int ret;
   
-  switch (optname)
+  assert(optname == IP_ADD_MEMBERSHIP || optname == IP_DROP_MEMBERSHIP);
+  memset (&mreqn, 0, sizeof(mreqn));
+
+  mreqn.imr_multiaddr.s_addr = mcast_addr;
+  mreqn.imr_ifindex = ifindex;
+  
+  ret = setsockopt(sock, IPPROTO_IP, optname,
+                   (void *)&mreqn, sizeof(mreqn));
+  if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP) && (errno == EADDRINUSE))
     {
-    case IP_MULTICAST_IF:
-    case IP_ADD_MEMBERSHIP:
-    case IP_DROP_MEMBERSHIP:
-      memset (&mreqn, 0, sizeof(mreqn));
-
-      if (mcast_addr)
-	mreqn.imr_multiaddr.s_addr = mcast_addr;
-      
-      if (ifindex)
-	mreqn.imr_ifindex = ifindex;
-      else
-	mreqn.imr_address = if_addr;
-      
-      ret = setsockopt(sock, IPPROTO_IP, optname,
-		       (void *)&mreqn, sizeof(mreqn));
-      if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP) && (errno == EADDRINUSE))
-        {
-	  /* see above: handle possible problem when interface comes back up */
-	  char buf[2][INET_ADDRSTRLEN];
-	  zlog_info("setsockopt_multicast_ipv4 attempting to drop and "
-		    "re-add (fd %d, ifaddr %s, mcast %s, ifindex %u)",
-		    sock,
-		    inet_ntop(AF_INET, &if_addr, buf[0], sizeof(buf[0])),
-		    inet_ntop(AF_INET, &mreqn.imr_multiaddr,
-			      buf[1], sizeof(buf[1])), ifindex);
-	  setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP,
-		     (void *)&mreqn, sizeof(mreqn));
-	  ret = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
-			   (void *)&mreqn, sizeof(mreqn));
-        }
-      return ret;
-      break;
-
-    default:
-      /* Can out and give an understandable error */
-      errno = EINVAL;
-      return -1;
-      break;
+      /* see above: handle possible problem when interface comes back up */
+      char buf[1][INET_ADDRSTRLEN];
+      zlog_info("setsockopt_ipv4_multicast attempting to drop and "
+                "re-add (fd %d, mcast %s, ifindex %u)",
+                sock,
+                inet_ntop(AF_INET, &mreqn.imr_multiaddr,
+                          buf[0], sizeof(buf[0])), ifindex);
+      setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP,
+                 (void *)&mreqn, sizeof(mreqn));
+      ret = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
+                       (void *)&mreqn, sizeof(mreqn));
     }
+  return ret;
 
   /* Example defines for another OS, boilerplate off other code in this
      function, AND handle optname as per other sections for consistency !! */
   /* #elif  defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */
   /* Add your favourite OS here! */
 
-#else /* #if OS_TYPE */ 
+#elif defined(HAVE_BSD_STRUCT_IP_MREQ_HACK) /* #if OS_TYPE */ 
   /* standard BSD API */
 
   struct in_addr m;
   struct ip_mreq mreq;
   int ret;
 
-#ifdef HAVE_BSD_STRUCT_IP_MREQ_HACK
-  if (ifindex)
-    m.s_addr = htonl(ifindex);
-  else
-#endif
-    m = if_addr;
+  assert(optname == IP_ADD_MEMBERSHIP || optname == IP_DROP_MEMBERSHIP);
 
-  switch (optname)
+  m.s_addr = htonl(ifindex);
+
+  memset (&mreq, 0, sizeof(mreq));
+  mreq.imr_multiaddr.s_addr = mcast_addr;
+  mreq.imr_interface = m;
+  
+  ret = setsockopt (sock, IPPROTO_IP, optname, (void *)&mreq, sizeof(mreq));
+  if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP) && (errno == EADDRINUSE))
     {
-    case IP_MULTICAST_IF:
-      return setsockopt (sock, IPPROTO_IP, optname, (void *)&m, sizeof(m)); 
-      break;
-
-    case IP_ADD_MEMBERSHIP:
-    case IP_DROP_MEMBERSHIP:
-      memset (&mreq, 0, sizeof(mreq));
-      mreq.imr_multiaddr.s_addr = mcast_addr;
-      mreq.imr_interface = m;
-      
-      ret = setsockopt (sock, IPPROTO_IP, optname, (void *)&mreq, sizeof(mreq));
-      if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP) && (errno == EADDRINUSE))
-        {
-	  /* see above: handle possible problem when interface comes back up */
-	  char buf[2][INET_ADDRSTRLEN];
-	  zlog_info("setsockopt_multicast_ipv4 attempting to drop and "
-		    "re-add (fd %d, ifaddr %s, mcast %s, ifindex %u)",
-		    sock,
-		    inet_ntop(AF_INET, &if_addr, buf[0], sizeof(buf[0])),
-		    inet_ntop(AF_INET, &mreq.imr_multiaddr,
-			      buf[1], sizeof(buf[1])), ifindex);
-	  setsockopt (sock, IPPROTO_IP, IP_DROP_MEMBERSHIP,
-		      (void *)&mreq, sizeof(mreq));
-	  ret = setsockopt (sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
-	  		    (void *)&mreq, sizeof(mreq));
-        }
-      return ret;
-      break;
-      
-    default:
-      /* Can out and give an understandable error */
-      errno = EINVAL;
-      return -1;
-      break;
+      /* see above: handle possible problem when interface comes back up */
+      char buf[1][INET_ADDRSTRLEN];
+      zlog_info("setsockopt_ipv4_multicast attempting to drop and "
+                "re-add (fd %d, mcast %s, ifindex %u)",
+                sock,
+                inet_ntop(AF_INET, &mreq.imr_multiaddr,
+                          buf[0], sizeof(buf[0])), ifindex);
+      setsockopt (sock, IPPROTO_IP, IP_DROP_MEMBERSHIP,
+                  (void *)&mreq, sizeof(mreq));
+      ret = setsockopt (sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
+                        (void *)&mreq, sizeof(mreq));
     }
+  return ret;
+
+#else
+  #error "Unsupported multicast API"
 #endif /* #if OS_TYPE */
 
 }
 
+/*
+ * Set IP_MULTICAST_IF socket option in an OS-dependent manner.
+ */
+int
+setsockopt_ipv4_multicast_if(int sock,
+			unsigned int ifindex)
+{
+
+#ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
+  struct ip_mreqn mreqn;
+  memset (&mreqn, 0, sizeof(mreqn));
+
+  mreqn.imr_ifindex = ifindex;
+  return setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, (void *)&mreqn, sizeof(mreqn));
+
+  /* Example defines for another OS, boilerplate off other code in this
+     function */
+  /* #elif  defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */
+  /* Add your favourite OS here! */
+#elif defined(HAVE_BSD_STRUCT_IP_MREQ_HACK)
+  struct in_addr m;
+
+  m.s_addr = htonl(ifindex);
+
+  return setsockopt (sock, IPPROTO_IP, IP_MULTICAST_IF, (void *)&m, sizeof(m));
+#else
+  #error "Unsupported multicast API"
+#endif
+}
+  
 static int
 setsockopt_ipv4_ifindex (int sock, int val)
 {
@@ -451,8 +476,6 @@
 int
 getsockopt_ifindex (int af, struct msghdr *msgh)
 {
-  int ifindex = 0;
-  
   switch (af)
     {
       case AF_INET:
@@ -465,7 +488,7 @@
 #endif
       default:
         zlog_warn ("getsockopt_ifindex: unknown address family %d", af);
-        return (ifindex = 0);
+        return 0;
     }
 }
 
diff --git a/lib/sockopt.h b/lib/sockopt.h
index cb05c6f..aced6d4 100644
--- a/lib/sockopt.h
+++ b/lib/sockopt.h
@@ -35,6 +35,7 @@
 extern int setsockopt_ipv6_unicast_hops (int, int);
 extern int setsockopt_ipv6_hoplimit (int, int);
 extern int setsockopt_ipv6_multicast_loop (int, int);
+extern int setsockopt_ipv6_tclass (int, int);
 #endif /* HAVE_IPV6 */
 
 /*
@@ -82,13 +83,11 @@
   (((af) == AF_INET) : SOPT_SIZE_CMSG_IFINDEX_IPV4() \
                     ? SOPT_SIZE_CMSG_PKTINFO_IPV6())
 
-extern int setsockopt_multicast_ipv4(int sock, int optname, 
-			             struct in_addr if_addr
-					 /* required: interface to join on */,
+extern int setsockopt_ipv4_multicast_if(int sock,
+			             unsigned int ifindex);
+extern int setsockopt_ipv4_multicast(int sock, int optname,
                                      unsigned int mcast_addr,
-			             unsigned int ifindex
-					 /* optional: if non-zero, may be used
-					 	instead of if_addr */);
+			             unsigned int ifindex);
 extern int setsockopt_ipv4_tos(int sock, int tos);
 
 /* Ask for, and get, ifindex, by whatever method is supported. */
diff --git a/lib/sockunion.c b/lib/sockunion.c
index a5382a7..5977052 100644
--- a/lib/sockunion.c
+++ b/lib/sockunion.c
@@ -297,27 +297,24 @@
 }
 
 /* return sockunion structure : this function should be revised. */
-static char *
-sockunion_log (union sockunion *su)
+static const char *
+sockunion_log (union sockunion *su, char *buf, size_t len)
 {
-  static char buf[SU_ADDRSTRLEN];
-
   switch (su->sa.sa_family) 
     {
     case AF_INET:
-      snprintf (buf, SU_ADDRSTRLEN, "%s", inet_ntoa (su->sin.sin_addr));
-      break;
+      return inet_ntop(AF_INET, &su->sin.sin_addr, buf, len);
+
 #ifdef HAVE_IPV6
     case AF_INET6:
-      snprintf (buf, SU_ADDRSTRLEN, "%s",
-		inet_ntop (AF_INET6, &(su->sin6.sin6_addr), buf, SU_ADDRSTRLEN));
+      return inet_ntop(AF_INET6, &(su->sin6.sin6_addr), buf, len);
       break;
 #endif /* HAVE_IPV6 */
+
     default:
-      snprintf (buf, SU_ADDRSTRLEN, "af_unknown %d ", su->sa.sa_family);
-      break;
+      snprintf (buf, len, "af_unknown %d ", su->sa.sa_family);
+      return buf;
     }
-  return (XSTRDUP (MTYPE_TMP, buf));
 }
 
 /* sockunion_connect returns
@@ -379,8 +376,10 @@
     {
       if (errno != EINPROGRESS)
 	{
+	  char str[SU_ADDRSTRLEN];
 	  zlog_info ("can't connect to %s fd %d : %s",
-		     sockunion_log (&su), fd, safe_strerror (errno));
+		     sockunion_log (&su, str, sizeof str),
+		     fd, safe_strerror (errno));
 	  return connect_error;
 	}
     }
@@ -567,6 +566,30 @@
   return -1;
 }
 
+int
+sockopt_v6only (int family, int sock)
+{
+  int ret, on = 1;
+
+#ifdef HAVE_IPV6
+#ifdef IPV6_V6ONLY
+  if (family == AF_INET6)
+    {
+      ret = setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY,
+			(void *) &on, sizeof (int));
+      if (ret < 0)
+	{
+	  zlog (NULL, LOG_WARNING, "can't set sockopt IPV6_V6ONLY "
+		    "to socket %d", sock);
+	  return -1;
+	}
+      return 0;
+    }
+#endif /* IPV6_V6ONLY */
+#endif /* HAVE_IPV6 */
+  return 0;
+}
+
 /* If same family and same prefix return 1. */
 int
 sockunion_same (union sockunion *su1, union sockunion *su2)
diff --git a/lib/sockunion.h b/lib/sockunion.h
index 0ee2d63..4531f62 100644
--- a/lib/sockunion.h
+++ b/lib/sockunion.h
@@ -99,6 +99,7 @@
 extern int sockunion_stream_socket (union sockunion *);
 extern int sockopt_reuseaddr (int);
 extern int sockopt_reuseport (int);
+extern int sockopt_v6only (int family, int sock);
 extern int sockunion_bind (int sock, union sockunion *, 
                            unsigned short, union sockunion *);
 extern int sockopt_ttl (int family, int sock, int ttl);
diff --git a/lib/thread.c b/lib/thread.c
index fd841c2..b36c43a 100644
--- a/lib/thread.c
+++ b/lib/thread.c
@@ -248,7 +248,7 @@
   XFREE (MTYPE_THREAD_STATS, hist);
 }
 
-static inline void 
+static void 
 vty_out_cpu_thread_history(struct vty* vty,
 			   struct cpu_thread_history *a)
 {
@@ -608,7 +608,7 @@
 }
 
 /* Thread list is empty or not.  */
-static inline int
+static int
 thread_empty (struct thread_list *list)
 {
   return  list->head ? 0 : 1;
@@ -972,10 +972,12 @@
 thread_timer_process (struct thread_list *list, struct timeval *timenow)
 {
   struct thread *thread;
+  struct thread *next;
   unsigned int ready = 0;
   
-  for (thread = list->head; thread; thread = thread->next)
+  for (thread = list->head; thread; thread = next)
     {
+      next = thread->next;
       if (timeval_cmp (*timenow, thread->u.sands) < 0)
         return ready;
       thread_list_delete (list, thread);
@@ -991,10 +993,12 @@
 thread_process (struct thread_list *list)
 {
   struct thread *thread;
+  struct thread *next;
   unsigned int ready = 0;
   
-  for (thread = list->head; thread; thread = thread->next)
+  for (thread = list->head; thread; thread = next)
     {
+      next = thread->next;
       thread_list_delete (list, thread);
       thread->type = THREAD_READY;
       thread_list_add (&thread->master->ready, thread);
diff --git a/lib/thread.h b/lib/thread.h
index 978aa6b..dfc51e2 100644
--- a/lib/thread.h
+++ b/lib/thread.h
@@ -1,5 +1,6 @@
 /* Thread management routine header.
  * Copyright (C) 1998 Kunihiro Ishiguro
+ * Portions Copyright (c) 2008 Everton da Silva Marques <everton.marques@gmail.com>
  *
  * This file is part of GNU Zebra.
  *
@@ -22,6 +23,8 @@
 #ifndef _ZEBRA_THREAD_H
 #define _ZEBRA_THREAD_H
 
+#include <zebra.h>
+
 struct rusage_t
 {
 #ifdef HAVE_RUSAGE
@@ -137,6 +140,12 @@
       thread = thread_add_timer (master, func, arg, time); \
   } while (0)
 
+#define THREAD_TIMER_MSEC_ON(master,thread,func,arg,time) \
+  do { \
+    if (! thread) \
+      thread = thread_add_timer_msec (master, func, arg, time); \
+  } while (0)
+
 #define THREAD_OFF(thread) \
   do { \
     if (thread) \
diff --git a/lib/vty.c b/lib/vty.c
index bd1dbac..9a4efe6 100644
--- a/lib/vty.c
+++ b/lib/vty.c
@@ -699,6 +699,7 @@
     case ZEBRA_NODE:
     case RIP_NODE:
     case RIPNG_NODE:
+    case BABEL_NODE:
     case BGP_NODE:
     case BGP_VPNV4_NODE:
     case BGP_IPV4_NODE:
@@ -1107,6 +1108,7 @@
     case ZEBRA_NODE:
     case RIP_NODE:
     case RIPNG_NODE:
+    case BABEL_NODE:
     case BGP_NODE:
     case RMAP_NODE:
     case OSPF_NODE:
@@ -1685,7 +1687,6 @@
 vty_accept (struct thread *thread)
 {
   int vty_sock;
-  struct vty *vty;
   union sockunion su;
   int ret;
   unsigned int on;
@@ -1770,7 +1771,7 @@
   if (bufp)
     XFREE (MTYPE_TMP, bufp);
 
-  vty = vty_create (vty_sock, &su);
+  vty_create (vty_sock, &su);
 
   return 0;
 }
@@ -1816,6 +1817,7 @@
       if (sock < 0)
 	continue;
 
+      sockopt_v6only (ainfo->ai_family, sock);
       sockopt_reuseaddr (sock);
       sockopt_reuseport (sock);
 
@@ -1839,7 +1841,7 @@
 
   freeaddrinfo (ainfo_save);
 }
-#endif /* HAVE_IPV6 && ! NRL */
+#else /* HAVE_IPV6 && ! NRL */
 
 /* Make vty server socket. */
 static void
@@ -1905,6 +1907,7 @@
   /* Add vty server event. */
   vty_event (VTY_SERV, accept_sock, NULL);
 }
+#endif /* HAVE_IPV6 && ! NRL */
 
 #ifdef VTYSH
 /* For sockaddr_un. */
diff --git a/lib/vty.h b/lib/vty.h
index 7df04b5..639d741 100644
--- a/lib/vty.h
+++ b/lib/vty.h
@@ -152,8 +152,9 @@
 #define VTY_GET_LONG(NAME,V,STR) \
 do { \
   char *endptr = NULL; \
+  errno = 0; \
   (V) = strtoul ((STR), &endptr, 10); \
-  if (*endptr != '\0' || (V) == ULONG_MAX) \
+  if (*(STR) == '-' || *endptr != '\0' || errno) \
     { \
       vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
       return CMD_WARNING; \
diff --git a/lib/workqueue.c b/lib/workqueue.c
index 52b5f41..61643bf 100644
--- a/lib/workqueue.c
+++ b/lib/workqueue.c
@@ -103,7 +103,7 @@
   return;
 }
 
-static inline int
+static int
 work_queue_schedule (struct work_queue *wq, unsigned int delay)
 {
   /* if appropriate, schedule work queue thread */
diff --git a/lib/zclient.c b/lib/zclient.c
index 85aa737..61c6f73 100644
--- a/lib/zclient.c
+++ b/lib/zclient.c
@@ -41,6 +41,8 @@
 
 extern struct thread_master *master;
 
+char *zclient_serv_path = NULL;
+
 /* This file local debug flag. */
 int zclient_debug = 0;
 
@@ -143,8 +145,10 @@
   zclient_init (zclient, zclient->redist_default);
 }
 
+#ifdef HAVE_TCP_ZEBRA
+
 /* Make socket to zebra daemon. Return zebra socket. */
-int
+static int
 zclient_socket(void)
 {
   int sock;
@@ -175,10 +179,12 @@
   return sock;
 }
 
+#else
+
 /* For sockaddr_un. */
 #include <sys/un.h>
 
-int
+static int
 zclient_socket_un (const char *path)
 {
   int ret;
@@ -208,6 +214,26 @@
   return sock;
 }
 
+#endif /* HAVE_TCP_ZEBRA */
+
+/**
+ * Connect to zebra daemon.
+ * @param zclient a pointer to zclient structure
+ * @return socket fd just to make sure that connection established
+ * @see zclient_init
+ * @see zclient_new
+ */
+int
+zclient_socket_connect (struct zclient *zclient)
+{
+#ifdef HAVE_TCP_ZEBRA
+  zclient->sock = zclient_socket ();
+#else
+  zclient->sock = zclient_socket_un (zclient_serv_path ? zclient_serv_path : ZEBRA_SERV_PATH);
+#endif
+  return zclient->sock;
+}
+
 static int
 zclient_failed(struct zclient *zclient)
 {
@@ -292,6 +318,25 @@
   return zclient_send_message(zclient);
 }
 
+static int
+zebra_hello_send (struct zclient *zclient)
+{
+  struct stream *s;
+
+  if (zclient->redist_default)
+    {
+      s = zclient->obuf;
+      stream_reset (s);
+
+      zclient_create_header (s, ZEBRA_HELLO);
+      stream_putc (s, zclient->redist_default);
+      stream_putw_at (s, 0, stream_get_endp (s));
+      return zclient_send_message(zclient);
+    }
+
+  return 0;
+}
+
 /* Make connection to zebra daemon. */
 int
 zclient_start (struct zclient *zclient)
@@ -313,13 +358,7 @@
   if (zclient->t_connect)
     return 0;
 
-  /* Make socket. */
-#ifdef HAVE_TCP_ZEBRA
-  zclient->sock = zclient_socket ();
-#else
-  zclient->sock = zclient_socket_un (ZEBRA_SERV_PATH);
-#endif /* HAVE_TCP_ZEBRA */
-  if (zclient->sock < 0)
+  if (zclient_socket_connect(zclient) < 0)
     {
       if (zclient_debug)
 	zlog_debug ("zclient connection fail");
@@ -339,6 +378,8 @@
   /* Create read thread. */
   zclient_event (ZCLIENT_READ, zclient);
 
+  zebra_hello_send (zclient);
+
   /* We need router-id information. */
   zebra_message_send (zclient, ZEBRA_ROUTER_ID_ADD);
 
@@ -435,6 +476,7 @@
   stream_putc (s, api->type);
   stream_putc (s, api->flags);
   stream_putc (s, api->message);
+  stream_putw (s, api->safi);
 
   /* Put prefix information. */
   psize = PSIZE (p->prefixlen);
@@ -496,6 +538,7 @@
   stream_putc (s, api->type);
   stream_putc (s, api->flags);
   stream_putc (s, api->message);
+  stream_putw (s, api->safi);
   
   /* Put prefix information. */
   psize = PSIZE (p->prefixlen);
@@ -778,7 +821,6 @@
 static int
 zclient_read (struct thread *thread)
 {
-  int ret;
   size_t already;
   uint16_t length, command;
   uint8_t marker, version;
@@ -873,47 +915,47 @@
     {
     case ZEBRA_ROUTER_ID_UPDATE:
       if (zclient->router_id_update)
-	ret = (*zclient->router_id_update) (command, zclient, length);
+	(*zclient->router_id_update) (command, zclient, length);
       break;
     case ZEBRA_INTERFACE_ADD:
       if (zclient->interface_add)
-	ret = (*zclient->interface_add) (command, zclient, length);
+	(*zclient->interface_add) (command, zclient, length);
       break;
     case ZEBRA_INTERFACE_DELETE:
       if (zclient->interface_delete)
-	ret = (*zclient->interface_delete) (command, zclient, length);
+	(*zclient->interface_delete) (command, zclient, length);
       break;
     case ZEBRA_INTERFACE_ADDRESS_ADD:
       if (zclient->interface_address_add)
-	ret = (*zclient->interface_address_add) (command, zclient, length);
+	(*zclient->interface_address_add) (command, zclient, length);
       break;
     case ZEBRA_INTERFACE_ADDRESS_DELETE:
       if (zclient->interface_address_delete)
-	ret = (*zclient->interface_address_delete) (command, zclient, length);
+	(*zclient->interface_address_delete) (command, zclient, length);
       break;
     case ZEBRA_INTERFACE_UP:
       if (zclient->interface_up)
-	ret = (*zclient->interface_up) (command, zclient, length);
+	(*zclient->interface_up) (command, zclient, length);
       break;
     case ZEBRA_INTERFACE_DOWN:
       if (zclient->interface_down)
-	ret = (*zclient->interface_down) (command, zclient, length);
+	(*zclient->interface_down) (command, zclient, length);
       break;
     case ZEBRA_IPV4_ROUTE_ADD:
       if (zclient->ipv4_route_add)
-	ret = (*zclient->ipv4_route_add) (command, zclient, length);
+	(*zclient->ipv4_route_add) (command, zclient, length);
       break;
     case ZEBRA_IPV4_ROUTE_DELETE:
       if (zclient->ipv4_route_delete)
-	ret = (*zclient->ipv4_route_delete) (command, zclient, length);
+	(*zclient->ipv4_route_delete) (command, zclient, length);
       break;
     case ZEBRA_IPV6_ROUTE_ADD:
       if (zclient->ipv6_route_add)
-	ret = (*zclient->ipv6_route_add) (command, zclient, length);
+	(*zclient->ipv6_route_add) (command, zclient, length);
       break;
     case ZEBRA_IPV6_ROUTE_DELETE:
       if (zclient->ipv6_route_delete)
-	ret = (*zclient->ipv6_route_delete) (command, zclient, length);
+	(*zclient->ipv6_route_delete) (command, zclient, length);
       break;
     default:
       break;
@@ -1000,3 +1042,29 @@
       break;
     }
 }
+
+void
+zclient_serv_path_set (char *path)
+{
+  struct stat sb;
+
+  /* reset */
+  zclient_serv_path = NULL;
+
+  /* test if `path' is socket. don't set it otherwise. */
+  if (stat(path, &sb) == -1)
+    {
+      zlog_warn ("%s: zebra socket `%s' does not exist", __func__, path);
+      return;
+    }
+
+  if ((sb.st_mode & S_IFMT) != S_IFSOCK)
+    {
+      zlog_warn ("%s: `%s' is not unix socket, sir", __func__, path);
+      return;
+    }
+
+  /* it seems that path is unix socket */
+  zclient_serv_path = path;
+}
+
diff --git a/lib/zclient.h b/lib/zclient.h
index 21786ab..a7d7b54 100644
--- a/lib/zclient.h
+++ b/lib/zclient.h
@@ -22,6 +22,9 @@
 #ifndef _ZEBRA_ZCLIENT_H
 #define _ZEBRA_ZCLIENT_H
 
+/* For struct zapi_ipv{4,6}. */
+#include "prefix.h"
+
 /* For struct interface and struct connected. */
 #include "if.h"
 
@@ -108,6 +111,8 @@
 
   u_char message;
 
+  safi_t safi;
+
   u_char nexthop_num;
   struct in_addr **nexthop;
 
@@ -127,11 +132,8 @@
 extern void zclient_reset (struct zclient *);
 extern void zclient_free (struct zclient *);
 
-/* Get TCP socket connection to zebra daemon at loopback address. */
-extern int zclient_socket (void);
-
-/* Get unix stream socket connection to zebra daemon at given path. */
-extern int zclient_socket_un (const char *);
+extern int  zclient_socket_connect (struct zclient *);
+extern void zclient_serv_path_set  (char *path);
 
 /* Send redistribute command to zebra daemon. Do not update zclient state. */
 extern int zebra_redistribute_send (int command, struct zclient *, int type);
@@ -168,6 +170,8 @@
 
   u_char message;
 
+  safi_t safi;
+
   u_char nexthop_num;
   struct in6_addr **nexthop;
 
diff --git a/lib/zebra.h b/lib/zebra.h
index 2dc8451..ab072d6 100644
--- a/lib/zebra.h
+++ b/lib/zebra.h
@@ -237,10 +237,6 @@
 #define UINT32_MAX	(4294967295U)
 #endif
 
-#ifdef HAVE_LIBUTIL_H
-#include <libutil.h>
-#endif /* HAVE_LIBUTIL_H */
-
 #ifdef HAVE_GLIBC_BACKTRACE
 #include <execinfo.h>
 #endif /* HAVE_GLIBC_BACKTRACE */
@@ -421,7 +417,8 @@
 #define ZEBRA_ROUTER_ID_ADD               20
 #define ZEBRA_ROUTER_ID_DELETE            21
 #define ZEBRA_ROUTER_ID_UPDATE            22
-#define ZEBRA_MESSAGE_MAX                 23
+#define ZEBRA_HELLO                       23
+#define ZEBRA_MESSAGE_MAX                 24
 
 /* Marker value used in new Zserv, in the byte location corresponding
  * the command value in the old zserv header. To allow old and new
@@ -429,19 +426,8 @@
  */
 #define ZEBRA_HEADER_MARKER              255
 
-/* Zebra route's types. */
-#define ZEBRA_ROUTE_SYSTEM               0
-#define ZEBRA_ROUTE_KERNEL               1
-#define ZEBRA_ROUTE_CONNECT              2
-#define ZEBRA_ROUTE_STATIC               3
-#define ZEBRA_ROUTE_RIP                  4
-#define ZEBRA_ROUTE_RIPNG                5
-#define ZEBRA_ROUTE_OSPF                 6
-#define ZEBRA_ROUTE_OSPF6                7
-#define ZEBRA_ROUTE_ISIS                 8
-#define ZEBRA_ROUTE_BGP                  9
-#define ZEBRA_ROUTE_HSLS		 10
-#define ZEBRA_ROUTE_MAX                  11
+/* Zebra route's types are defined in route_types.h */
+#include "route_types.h"
 
 /* Note: whenever a new route-type or zserv-command is added the
  * corresponding {command,route}_types[] table in lib/log.c MUST be
@@ -455,6 +441,10 @@
  * e.g. ZEBRA_INTERFACE_ADD -> "ZEBRA_INTERFACE_ADD" */
 /* Map a protocol name to its number. e.g. ZEBRA_ROUTE_BGP->9*/
 extern int proto_name2num(const char *s);
+/* Map redistribute X argument to protocol number.
+ * unlike proto_name2num, this accepts shorthands and takes
+ * an AFI value to restrict input */
+extern int proto_redistnum(int afi, const char *s);
 
 extern const char *zserv_command_string (unsigned int command);
 
@@ -504,7 +494,7 @@
 /* Subsequent Address Family Identifier. */
 #define SAFI_UNICAST              1
 #define SAFI_MULTICAST            2
-#define SAFI_UNICAST_MULTICAST    3
+#define SAFI_RESERVED_3           3
 #define SAFI_MPLS_VPN             4
 #define SAFI_MAX                  5
 
diff --git a/m4/.cvsignore b/m4/.cvsignore
deleted file mode 100644
index 2f02fdb..0000000
--- a/m4/.cvsignore
+++ /dev/null
@@ -1,4 +0,0 @@
-Makefile
-Makefile.in
-.arch-inventory
-.arch-ids
diff --git a/ospf6d/.cvsignore b/ospf6d/.cvsignore
deleted file mode 100644
index 522397a..0000000
--- a/ospf6d/.cvsignore
+++ /dev/null
@@ -1,15 +0,0 @@
-Makefile.in
-Makefile
-*.o
-*.patch
-ospf6d
-ospf6d.conf
-tags
-TAGS
-.deps
-.nfs*
-*.lo
-*.la
-*.libs
-.arch-inventory
-.arch-ids
diff --git a/ospf6d/ospf6_abr.c b/ospf6d/ospf6_abr.c
index d38ef21..c3a63fe 100644
--- a/ospf6d/ospf6_abr.c
+++ b/ospf6d/ospf6_abr.c
@@ -555,7 +555,8 @@
       prefix.family = AF_INET6;
       prefix.prefixlen = prefix_lsa->prefix.prefix_length;
       ospf6_prefix_in6_addr (&prefix.u.prefix6, &prefix_lsa->prefix);
-      prefix2str (&prefix, buf, sizeof (buf));
+      if (is_debug)
+        prefix2str (&prefix, buf, sizeof (buf));
       table = oa->ospf6->route_table;
       type = OSPF6_DEST_TYPE_NETWORK;
       prefix_options = prefix_lsa->prefix.prefix_options;
@@ -574,7 +575,8 @@
       router_lsa = (struct ospf6_inter_router_lsa *)
         OSPF6_LSA_HEADER_END (lsa->header);
       ospf6_linkstate_prefix (router_lsa->router_id, htonl (0), &prefix);
-      inet_ntop (AF_INET, &router_lsa->router_id, buf, sizeof (buf));
+      if (is_debug)
+        inet_ntop (AF_INET, &router_lsa->router_id, buf, sizeof (buf));
       table = oa->ospf6->brouter_table;
       type = OSPF6_DEST_TYPE_ROUTER;
       options[0] = router_lsa->options[0];
diff --git a/ospf6d/ospf6_abr.h b/ospf6d/ospf6_abr.h
index 5d00c47..816f596 100644
--- a/ospf6d/ospf6_abr.h
+++ b/ospf6d/ospf6_abr.h
@@ -22,6 +22,9 @@
 #ifndef OSPF6_ABR_H
 #define OSPF6_ABR_H
 
+/* for struct ospf6_route */
+#include "ospf6_route.h"
+
 /* Debug option */
 extern unsigned char conf_debug_ospf6_abr;
 #define OSPF6_DEBUG_ABR_ON() \
@@ -32,6 +35,7 @@
   (conf_debug_ospf6_abr)
 
 /* Inter-Area-Prefix-LSA */
+#define OSPF6_INTER_PREFIX_LSA_MIN_SIZE        4U /* w/o IPv6 prefix */
 struct ospf6_inter_prefix_lsa
 {
   u_int32_t metric;
@@ -39,6 +43,7 @@
 };
 
 /* Inter-Area-Router-LSA */
+#define OSPF6_INTER_ROUTER_LSA_FIX_SIZE       12U
 struct ospf6_inter_router_lsa
 {
   u_char mbz;
diff --git a/ospf6d/ospf6_area.c b/ospf6d/ospf6_area.c
index f4b327e..9934e6b 100644
--- a/ospf6d/ospf6_area.c
+++ b/ospf6d/ospf6_area.c
@@ -420,6 +420,18 @@
           prefix2str (&range->prefix, buf, sizeof (buf));
           vty_out (vty, " area %s range %s%s", oa->name, buf, VNL);
         }
+      if (PREFIX_NAME_IN (oa))
+        vty_out (vty, " area %s filter-list prefix %s in%s",
+                 oa->name, PREFIX_NAME_IN (oa), VNL);
+      if (PREFIX_NAME_OUT (oa))
+        vty_out (vty, " area %s filter-list prefix %s out%s",
+                 oa->name, PREFIX_NAME_OUT (oa), VNL);
+      if (IMPORT_NAME (oa))
+        vty_out (vty, " area %s import-list %s%s",
+                 oa->name, IMPORT_NAME (oa), VNL);
+      if (EXPORT_NAME (oa))
+        vty_out (vty, " area %s export-list %s%s",
+                 oa->name, EXPORT_NAME (oa), VNL);
     }
 }
 
@@ -441,14 +453,14 @@
   argc--;
   argv++;
 
-  plist = prefix_list_lookup (AFI_IP6, argv[1]);
-  if (strncmp (argv[2], "in", 2) == 0)
+  plist = prefix_list_lookup (AFI_IP6, argv[0]);
+  if (strncmp (argv[1], "in", 2) == 0)
     {
       PREFIX_LIST_IN (area) = plist;
       if (PREFIX_NAME_IN (area))
 	free (PREFIX_NAME_IN (area));
 
-      PREFIX_NAME_IN (area) = strdup (argv[1]);
+      PREFIX_NAME_IN (area) = strdup (argv[0]);
       ospf6_abr_reimport (area);
     }
   else
@@ -457,7 +469,7 @@
       if (PREFIX_NAME_OUT (area))
 	free (PREFIX_NAME_OUT (area));
 
-      PREFIX_NAME_OUT (area) = strdup (argv[1]);
+      PREFIX_NAME_OUT (area) = strdup (argv[0]);
       ospf6_abr_enable_area (area);
     }
 
@@ -483,11 +495,11 @@
   argc--;
   argv++;
 
-  plist = prefix_list_lookup (AFI_IP6, argv[1]);
-  if (strncmp (argv[2], "in", 2) == 0)
+  plist = prefix_list_lookup (AFI_IP6, argv[0]);
+  if (strncmp (argv[1], "in", 2) == 0)
     {
       if (PREFIX_NAME_IN (area))
-	if (strcmp (PREFIX_NAME_IN (area), argv[1]) != 0)
+	if (strcmp (PREFIX_NAME_IN (area), argv[0]) != 0)
 	  return CMD_SUCCESS;
 
       PREFIX_LIST_IN (area) = NULL;
@@ -500,7 +512,7 @@
   else
     {
       if (PREFIX_NAME_OUT (area))
-	if (strcmp (PREFIX_NAME_OUT (area), argv[1]) != 0)
+	if (strcmp (PREFIX_NAME_OUT (area), argv[0]) != 0)
 	  return CMD_SUCCESS;
 
       PREFIX_LIST_OUT (area) = NULL;
diff --git a/ospf6d/ospf6_asbr.c b/ospf6d/ospf6_asbr.c
index 3efaab4..ae0a286 100644
--- a/ospf6d/ospf6_asbr.c
+++ b/ospf6d/ospf6_asbr.c
@@ -447,6 +447,7 @@
       memset (&troute, 0, sizeof (troute));
       memset (&tinfo, 0, sizeof (tinfo));
       troute.route_option = &tinfo;
+      tinfo.ifindex = ifindex;
 
       ret = route_map_apply (ospf6->rmap[type].map, prefix,
                              RMAP_OSPF6, &troute);
@@ -616,27 +617,16 @@
 
 DEFUN (ospf6_redistribute,
        ospf6_redistribute_cmd,
-       "redistribute (static|kernel|connected|ripng|bgp)",
+       "redistribute " QUAGGA_REDIST_STR_OSPF6D,
        "Redistribute\n"
-       "Static route\n"
-       "Kernel route\n"
-       "Connected route\n"
-       "RIPng route\n"
-       "BGP route\n"
+       QUAGGA_REDIST_HELP_STR_OSPF6D
       )
 {
-  int type = 0;
+  int type;
 
-  if (strncmp (argv[0], "sta", 3) == 0)
-    type = ZEBRA_ROUTE_STATIC;
-  else if (strncmp (argv[0], "ker", 3) == 0)
-    type = ZEBRA_ROUTE_KERNEL;
-  else if (strncmp (argv[0], "con", 3) == 0)
-    type = ZEBRA_ROUTE_CONNECT;
-  else if (strncmp (argv[0], "rip", 3) == 0)
-    type = ZEBRA_ROUTE_RIPNG;
-  else if (strncmp (argv[0], "bgp", 3) == 0)
-    type = ZEBRA_ROUTE_BGP;
+  type = proto_redistnum(AFI_IP6, argv[0]);
+  if (type < 0 || type == ZEBRA_ROUTE_OSPF6)
+    return CMD_WARNING;
 
   ospf6_asbr_redistribute_unset (type);
   ospf6_asbr_routemap_unset (type);
@@ -646,29 +636,18 @@
 
 DEFUN (ospf6_redistribute_routemap,
        ospf6_redistribute_routemap_cmd,
-       "redistribute (static|kernel|connected|ripng|bgp) route-map WORD",
+       "redistribute " QUAGGA_REDIST_STR_OSPF6D " route-map WORD",
        "Redistribute\n"
-       "Static routes\n"
-       "Kernel route\n"
-       "Connected route\n"
-       "RIPng route\n"
-       "BGP route\n"
+       QUAGGA_REDIST_HELP_STR_OSPF6D
        "Route map reference\n"
        "Route map name\n"
       )
 {
-  int type = 0;
+  int type;
 
-  if (strncmp (argv[0], "sta", 3) == 0)
-    type = ZEBRA_ROUTE_STATIC;
-  else if (strncmp (argv[0], "ker", 3) == 0)
-    type = ZEBRA_ROUTE_KERNEL;
-  else if (strncmp (argv[0], "con", 3) == 0)
-    type = ZEBRA_ROUTE_CONNECT;
-  else if (strncmp (argv[0], "rip", 3) == 0)
-    type = ZEBRA_ROUTE_RIPNG;
-  else if (strncmp (argv[0], "bgp", 3) == 0)
-    type = ZEBRA_ROUTE_BGP;
+  type = proto_redistnum(AFI_IP6, argv[0]);
+  if (type < 0 || type == ZEBRA_ROUTE_OSPF6)
+    return CMD_WARNING;
 
   ospf6_asbr_redistribute_unset (type);
   ospf6_asbr_routemap_set (type, argv[1]);
@@ -678,28 +657,17 @@
 
 DEFUN (no_ospf6_redistribute,
        no_ospf6_redistribute_cmd,
-       "no redistribute (static|kernel|connected|ripng|bgp)",
+       "no redistribute " QUAGGA_REDIST_STR_OSPF6D,
        NO_STR
        "Redistribute\n"
-       "Static route\n"
-       "Kernel route\n"
-       "Connected route\n"
-       "RIPng route\n"
-       "BGP route\n"
+       QUAGGA_REDIST_HELP_STR_OSPF6D
       )
 {
-  int type = 0;
+  int type;
 
-  if (strncmp (argv[0], "sta", 3) == 0)
-    type = ZEBRA_ROUTE_STATIC;
-  else if (strncmp (argv[0], "ker", 3) == 0)
-    type = ZEBRA_ROUTE_KERNEL;
-  else if (strncmp (argv[0], "con", 3) == 0)
-    type = ZEBRA_ROUTE_CONNECT;
-  else if (strncmp (argv[0], "rip", 3) == 0)
-    type = ZEBRA_ROUTE_RIPNG;
-  else if (strncmp (argv[0], "bgp", 3) == 0)
-    type = ZEBRA_ROUTE_BGP;
+  type = proto_redistnum(AFI_IP6, argv[0]);
+  if (type < 0 || type == ZEBRA_ROUTE_OSPF6)
+    return CMD_WARNING;
 
   ospf6_asbr_redistribute_unset (type);
   ospf6_asbr_routemap_unset (type);
@@ -813,6 +781,54 @@
   ospf6_routemap_rule_match_address_prefixlist_free,
 };
 
+/* `match interface IFNAME' */
+/* Match function should return 1 if match is success else return
+   zero. */
+static route_map_result_t
+ospf6_routemap_rule_match_interface (void *rule, struct prefix *prefix,
+		       route_map_object_t type, void *object)
+{
+  struct interface   *ifp;
+  struct ospf6_external_info *ei;
+
+  if (type == RMAP_OSPF6)
+    {
+      ei = ((struct ospf6_route *) object)->route_option;
+      ifp = if_lookup_by_name ((char *)rule);
+
+      if (ifp != NULL
+      &&  ei->ifindex == ifp->ifindex)
+          return RMAP_MATCH;
+    }
+
+  return RMAP_NOMATCH;
+}
+
+/* Route map `interface' match statement.  `arg' should be
+   interface name. */
+static void *
+ospf6_routemap_rule_match_interface_compile (const char *arg)
+{
+  return XSTRDUP (MTYPE_ROUTE_MAP_COMPILED, arg);
+}
+
+/* Free route map's compiled `interface' value. */
+static void
+ospf6_routemap_rule_match_interface_free (void *rule)
+{
+  XFREE (MTYPE_ROUTE_MAP_COMPILED, rule);
+}
+
+/* Route map commands for interface matching. */
+struct route_map_rule_cmd
+ospf6_routemap_rule_match_interface_cmd =
+{
+  "interface",
+  ospf6_routemap_rule_match_interface,
+  ospf6_routemap_rule_match_interface_compile,
+  ospf6_routemap_rule_match_interface_free
+};
+
 static route_map_result_t
 ospf6_routemap_rule_set_metric_type (void *rule, struct prefix *prefix,
                                      route_map_object_t type, void *object)
@@ -990,6 +1006,39 @@
   return route_map_command_status (vty, ret);
 }
 
+/* "match interface" */
+DEFUN (ospf6_routemap_match_interface,
+       ospf6_routemap_match_interface_cmd,
+       "match interface WORD",
+       MATCH_STR
+       "Match first hop interface of route\n"
+       "Interface name\n")
+{
+  return route_map_add_match ((struct route_map_index *) vty->index,
+                              "interface", argv[0]);
+}
+
+/* "no match interface WORD" */
+DEFUN (ospf6_routemap_no_match_interface,
+       ospf6_routemap_no_match_interface_cmd,
+       "no match interface",
+       MATCH_STR
+       NO_STR
+       "Match first hop interface of route\n")
+{
+  int ret = route_map_delete_match ((struct route_map_index *) vty->index,
+                                    "interface", (argc == 0) ? NULL : argv[0]);
+  return route_map_command_status (vty, ret);
+}
+
+ALIAS (ospf6_routemap_no_match_interface,
+       ospf6_routemap_no_match_interface_val_cmd,
+       "no match interface WORD",
+       MATCH_STR
+       NO_STR
+       "Match first hop interface of route\n"
+       "Interface name\n")
+
 /* add "set metric-type" */
 DEFUN (ospf6_routemap_set_metric_type,
        ospf6_routemap_set_metric_type_cmd,
@@ -1082,6 +1131,8 @@
   route_map_delete_hook (ospf6_asbr_routemap_update);
 
   route_map_install_match (&ospf6_routemap_rule_match_address_prefixlist_cmd);
+  route_map_install_match (&ospf6_routemap_rule_match_interface_cmd);
+
   route_map_install_set (&ospf6_routemap_rule_set_metric_type_cmd);
   route_map_install_set (&ospf6_routemap_rule_set_metric_cmd);
   route_map_install_set (&ospf6_routemap_rule_set_forwarding_cmd);
@@ -1090,6 +1141,11 @@
   install_element (RMAP_NODE, &ospf6_routemap_match_address_prefixlist_cmd);
   install_element (RMAP_NODE, &ospf6_routemap_no_match_address_prefixlist_cmd);
 
+  /* Match interface */
+  install_element (RMAP_NODE, &ospf6_routemap_match_interface_cmd);
+  install_element (RMAP_NODE, &ospf6_routemap_no_match_interface_cmd);
+  install_element (RMAP_NODE, &ospf6_routemap_no_match_interface_val_cmd);
+
   /* ASE Metric Type (e.g. Type-1/Type-2) */
   install_element (RMAP_NODE, &ospf6_routemap_set_metric_type_cmd);
   install_element (RMAP_NODE, &ospf6_routemap_no_set_metric_type_cmd);
diff --git a/ospf6d/ospf6_asbr.h b/ospf6d/ospf6_asbr.h
index 6deb93e..72e4914 100644
--- a/ospf6d/ospf6_asbr.h
+++ b/ospf6d/ospf6_asbr.h
@@ -22,6 +22,13 @@
 #ifndef OSPF6_ASBR_H
 #define OSPF6_ASBR_H
 
+/* for struct ospf6_prefix */
+#include "ospf6_proto.h"
+/* for struct ospf6_lsa */
+#include "ospf6_lsa.h"
+/* for struct ospf6_route */
+#include "ospf6_route.h"
+
 /* Debug option */
 extern unsigned char conf_debug_ospf6_asbr;
 #define OSPF6_DEBUG_ASBR_ON() \
@@ -41,9 +48,12 @@
 
   struct in6_addr forwarding;
   /* u_int32_t tag; */
+
+  unsigned int ifindex;
 };
 
 /* AS-External-LSA */
+#define OSPF6_AS_EXTERNAL_LSA_MIN_SIZE         4U /* w/o IPv6 prefix */
 struct ospf6_as_external_lsa
 {
   u_int32_t bits_metric;
diff --git a/ospf6d/ospf6_interface.c b/ospf6d/ospf6_interface.c
index 236baf1..71aa685 100644
--- a/ospf6d/ospf6_interface.c
+++ b/ospf6d/ospf6_interface.c
@@ -108,14 +108,14 @@
   oi->neighbor_list = list_new ();
   oi->neighbor_list->cmp = ospf6_neighbor_cmp;
   oi->linklocal_addr = (struct in6_addr *) NULL;
-  oi->instance_id = 0;
-  oi->transdelay = 1;
-  oi->priority = 1;
+  oi->instance_id = OSPF6_INTERFACE_INSTANCE_ID;
+  oi->transdelay = OSPF6_INTERFACE_TRANSDELAY;
+  oi->priority = OSPF6_INTERFACE_PRIORITY;
 
-  oi->hello_interval = 10;
-  oi->dead_interval = 40;
-  oi->rxmt_interval = 5;
-  oi->cost = 1;
+  oi->hello_interval = OSPF6_INTERFACE_HELLO_INTERVAL;
+  oi->dead_interval = OSPF6_INTERFACE_DEAD_INTERVAL;
+  oi->rxmt_interval = OSPF6_INTERFACE_RXMT_INTERVAL;
+  oi->cost = OSPF6_INTERFACE_COST;
   oi->state = OSPF6_INTERFACE_DOWN;
   oi->flag = 0;
   oi->mtu_ignore = 0;
@@ -399,12 +399,12 @@
        prev_state == OSPF6_INTERFACE_BDR) &&
       (next_state != OSPF6_INTERFACE_DR &&
        next_state != OSPF6_INTERFACE_BDR))
-    ospf6_leave_alldrouters (oi->interface->ifindex);
+    ospf6_sso (oi->interface->ifindex, &alldrouters6, IPV6_LEAVE_GROUP);
   if ((prev_state != OSPF6_INTERFACE_DR &&
        prev_state != OSPF6_INTERFACE_BDR) &&
       (next_state == OSPF6_INTERFACE_DR ||
        next_state == OSPF6_INTERFACE_BDR))
-    ospf6_join_alldrouters (oi->interface->ifindex);
+    ospf6_sso (oi->interface->ifindex, &alldrouters6, IPV6_JOIN_GROUP);
 
   OSPF6_ROUTER_LSA_SCHEDULE (oi->area);
   if (next_state == OSPF6_INTERFACE_DOWN)
@@ -612,7 +612,7 @@
     }
 
   /* Join AllSPFRouters */
-  ospf6_join_allspfrouters (oi->interface->ifindex);
+  ospf6_sso (oi->interface->ifindex, &allspfrouters6, IPV6_JOIN_GROUP);
 
   /* Update interface route */
   ospf6_interface_connected_route_update (oi->interface);
@@ -707,7 +707,7 @@
 
   /* Leave AllSPFRouters */
   if (oi->state > OSPF6_INTERFACE_DOWN)
-    ospf6_leave_allspfrouters (oi->interface->ifindex);
+    ospf6_sso (oi->interface->ifindex, &allspfrouters6, IPV6_LEAVE_GROUP);
 
   ospf6_interface_state_change (OSPF6_INTERFACE_DOWN, oi);
 
@@ -929,7 +929,7 @@
        "Display connected prefixes to advertise\n"
        OSPF6_ROUTE_ADDRESS_STR
        OSPF6_ROUTE_PREFIX_STR
-       "Dispaly details of the prefixes\n"
+       "Display details of the prefixes\n"
        )
 
 ALIAS (show_ipv6_ospf6_interface_ifname_prefix,
@@ -943,7 +943,7 @@
        "Display connected prefixes to advertise\n"
        OSPF6_ROUTE_PREFIX_STR
        OSPF6_ROUTE_MATCH_STR
-       "Dispaly details of the prefixes\n"
+       "Display details of the prefixes\n"
        )
 
 DEFUN (show_ipv6_ospf6_interface_prefix,
@@ -982,7 +982,7 @@
        "Display connected prefixes to advertise\n"
        OSPF6_ROUTE_ADDRESS_STR
        OSPF6_ROUTE_PREFIX_STR
-       "Dispaly details of the prefixes\n"
+       "Display details of the prefixes\n"
        )
 
 ALIAS (show_ipv6_ospf6_interface_prefix,
@@ -995,7 +995,7 @@
        "Display connected prefixes to advertise\n"
        OSPF6_ROUTE_PREFIX_STR
        OSPF6_ROUTE_MATCH_STR
-       "Dispaly details of the prefixes\n"
+       "Display details of the prefixes\n"
        )
 
 
@@ -1522,23 +1522,36 @@
 
       if (ifp->desc)
         vty_out (vty, " description %s%s", ifp->desc, VNL);
-
       if (ifp->mtu6 != oi->ifmtu)
         vty_out (vty, " ipv6 ospf6 ifmtu %d%s", oi->ifmtu, VNL);
-      vty_out (vty, " ipv6 ospf6 cost %d%s",
-               oi->cost, VNL);
-      vty_out (vty, " ipv6 ospf6 hello-interval %d%s",
-               oi->hello_interval, VNL);
-      vty_out (vty, " ipv6 ospf6 dead-interval %d%s",
-               oi->dead_interval, VNL);
-      vty_out (vty, " ipv6 ospf6 retransmit-interval %d%s",
-               oi->rxmt_interval, VNL);
-      vty_out (vty, " ipv6 ospf6 priority %d%s",
-               oi->priority, VNL);
-      vty_out (vty, " ipv6 ospf6 transmit-delay %d%s",
-               oi->transdelay, VNL);
-      vty_out (vty, " ipv6 ospf6 instance-id %d%s",
-               oi->instance_id, VNL);
+
+      if (oi->cost != OSPF6_INTERFACE_COST)
+        vty_out (vty, " ipv6 ospf6 cost %d%s",
+                 oi->cost, VNL);
+
+      if (oi->hello_interval != OSPF6_INTERFACE_HELLO_INTERVAL)
+        vty_out (vty, " ipv6 ospf6 hello-interval %d%s",
+                 oi->hello_interval, VNL);
+
+      if (oi->dead_interval != OSPF6_INTERFACE_DEAD_INTERVAL)
+        vty_out (vty, " ipv6 ospf6 dead-interval %d%s",
+                 oi->dead_interval, VNL);
+
+      if (oi->rxmt_interval != OSPF6_INTERFACE_RXMT_INTERVAL)
+        vty_out (vty, " ipv6 ospf6 retransmit-interval %d%s",
+                 oi->rxmt_interval, VNL);
+
+      if (oi->priority != OSPF6_INTERFACE_PRIORITY)
+        vty_out (vty, " ipv6 ospf6 priority %d%s",
+                 oi->priority, VNL);
+
+      if (oi->transdelay != OSPF6_INTERFACE_TRANSDELAY)
+        vty_out (vty, " ipv6 ospf6 transmit-delay %d%s",
+                 oi->transdelay, VNL);
+
+      if (oi->instance_id != OSPF6_INTERFACE_INSTANCE_ID)
+        vty_out (vty, " ipv6 ospf6 instance-id %d%s",
+                 oi->instance_id, VNL);
 
       if (oi->plist_name)
         vty_out (vty, " ipv6 ospf6 advertise prefix-list %s%s",
diff --git a/ospf6d/ospf6_interface.h b/ospf6d/ospf6_interface.h
index cf758c0..2d1ff34 100644
--- a/ospf6d/ospf6_interface.h
+++ b/ospf6d/ospf6_interface.h
@@ -124,6 +124,15 @@
 #define OSPF6_INTERFACE_DISABLE      0x01
 #define OSPF6_INTERFACE_PASSIVE      0x02
 
+/* default values */
+#define OSPF6_INTERFACE_HELLO_INTERVAL 10
+#define OSPF6_INTERFACE_DEAD_INTERVAL  40
+#define OSPF6_INTERFACE_RXMT_INTERVAL  5
+#define OSPF6_INTERFACE_COST           1
+#define OSPF6_INTERFACE_PRIORITY       1
+#define OSPF6_INTERFACE_TRANSDELAY     1
+#define OSPF6_INTERFACE_INSTANCE_ID    0
+
 
 /* Function Prototypes */
 
diff --git a/ospf6d/ospf6_intra.h b/ospf6d/ospf6_intra.h
index 31643fd..3810174 100644
--- a/ospf6d/ospf6_intra.h
+++ b/ospf6d/ospf6_intra.h
@@ -69,6 +69,7 @@
    conf_debug_ospf6_brouter_specific_area_id == (area_id))
 
 /* Router-LSA */
+#define OSPF6_ROUTER_LSA_MIN_SIZE              4U
 struct ospf6_router_lsa
 {
   u_char bits;
@@ -77,6 +78,7 @@
 };
 
 /* Link State Description in Router-LSA */
+#define OSPF6_ROUTER_LSDESC_FIX_SIZE          16U
 struct ospf6_router_lsdesc
 {
   u_char    type;
@@ -105,6 +107,7 @@
   (((struct ospf6_router_lsdesc *)(x))->neighbor_router_id)
 
 /* Network-LSA */
+#define OSPF6_NETWORK_LSA_MIN_SIZE             4U
 struct ospf6_network_lsa
 {
   u_char reserved;
@@ -113,6 +116,7 @@
 };
 
 /* Link State Description in Router-LSA */
+#define OSPF6_NETWORK_LSDESC_FIX_SIZE          4U
 struct ospf6_network_lsdesc
 {
   u_int32_t router_id;
@@ -121,6 +125,7 @@
   (((struct ospf6_network_lsdesc *)(x))->router_id)
 
 /* Link-LSA */
+#define OSPF6_LINK_LSA_MIN_SIZE               24U /* w/o 1st IPv6 prefix */
 struct ospf6_link_lsa
 {
   u_char          priority;
@@ -131,6 +136,7 @@
 };
 
 /* Intra-Area-Prefix-LSA */
+#define OSPF6_INTRA_PREFIX_LSA_MIN_SIZE       12U /* w/o 1st IPv6 prefix */
 struct ospf6_intra_prefix_lsa
 {
   u_int16_t prefix_num;
diff --git a/ospf6d/ospf6_lsa.c b/ospf6d/ospf6_lsa.c
index 588b946..e65752d 100644
--- a/ospf6d/ospf6_lsa.c
+++ b/ospf6d/ospf6_lsa.c
@@ -163,9 +163,19 @@
     return 1;
   if (ntohs (lsa1->header->length) != ntohs (lsa2->header->length))
     return 1;
+  /* Going beyond LSA headers to compare the payload only makes sense, when both LSAs aren't header-only. */
+  if (CHECK_FLAG (lsa1->flag, OSPF6_LSA_HEADERONLY) != CHECK_FLAG (lsa2->flag, OSPF6_LSA_HEADERONLY))
+  {
+    zlog_warn ("%s: only one of two (%s, %s) LSAs compared is header-only", __func__, lsa1->name, lsa2->name);
+    return 1;
+  }
+  if (CHECK_FLAG (lsa1->flag, OSPF6_LSA_HEADERONLY))
+    return 0;
 
   length = OSPF6_LSA_SIZE (lsa1->header) - sizeof (struct ospf6_lsa_header);
-  assert (length > 0);
+  /* Once upper layer verifies LSAs received, length underrun should become a warning. */
+  if (length <= 0)
+    return 0;
 
   return memcmp (OSPF6_LSA_HEADER_END (lsa1->header),
                  OSPF6_LSA_HEADER_END (lsa2->header), length);
diff --git a/ospf6d/ospf6_lsa.h b/ospf6d/ospf6_lsa.h
index c1093ca..7d93f5c 100644
--- a/ospf6d/ospf6_lsa.h
+++ b/ospf6d/ospf6_lsa.h
@@ -79,6 +79,7 @@
   (ntohs (type) & OSPF6_LSTYPE_SCOPE_MASK)
 
 /* LSA Header */
+#define OSPF6_LSA_HEADER_SIZE                 20U
 struct ospf6_lsa_header
 {
   u_int16_t age;        /* LS age */
@@ -244,7 +245,6 @@
 
 extern void ospf6_lsa_init (void);
 extern void ospf6_lsa_terminate (void);
-extern void ospf6_lsa_cmd_init (void);
 
 extern int config_write_ospf6_debug_lsa (struct vty *vty);
 extern void install_element_ospf6_debug_lsa (void);
diff --git a/ospf6d/ospf6_main.c b/ospf6d/ospf6_main.c
index 800fae4..d3ef0a6 100644
--- a/ospf6d/ospf6_main.c
+++ b/ospf6d/ospf6_main.c
@@ -34,8 +34,13 @@
 #include "plist.h"
 #include "privs.h"
 #include "sigevent.h"
+#include "zclient.h"
 
 #include "ospf6d.h"
+#include "ospf6_top.h"
+#include "ospf6_message.h"
+#include "ospf6_asbr.h"
+#include "ospf6_lsa.h"
 
 /* Default configuration file name for ospf6d. */
 #define OSPF6_DEFAULT_CONFIG       "ospf6d.conf"
@@ -72,6 +77,7 @@
   { "daemon",      no_argument,       NULL, 'd'},
   { "config_file", required_argument, NULL, 'f'},
   { "pid_file",    required_argument, NULL, 'i'},
+  { "socket",      required_argument, NULL, 'z'},
   { "vty_addr",    required_argument, NULL, 'A'},
   { "vty_port",    required_argument, NULL, 'P'},
   { "user",        required_argument, NULL, 'u'},
@@ -110,6 +116,7 @@
 -d, --daemon       Runs in daemon mode\n\
 -f, --config_file  Set configuration file name\n\
 -i, --pid_file     Set process identifier file name\n\
+-z, --socket       Set path of zebra socket\n\
 -A, --vty_addr     Set vty's bind address\n\
 -P, --vty_port     Set vty's port number\n\
 -u, --user         User to run as\n\
@@ -124,7 +131,7 @@
   exit (status);
 }
 
-static void
+static void __attribute__ ((noreturn))
 ospf6_exit (int status)
 {
   extern struct ospf6 *ospf6;
@@ -173,6 +180,7 @@
 sigterm (void)
 {
   zlog_notice ("Terminating on signal SIGTERM");
+  ospf6_clean();
   ospf6_exit (0);
 }
 
@@ -226,7 +234,7 @@
   /* Command line argument treatment. */
   while (1) 
     {
-      opt = getopt_long (argc, argv, "df:i:hp:A:P:u:g:vC", longopts, 0);
+      opt = getopt_long (argc, argv, "df:i:z:hp:A:P:u:g:vC", longopts, 0);
     
       if (opt == EOF)
         break;
@@ -247,6 +255,9 @@
         case 'i':
           pid_file = optarg;
           break;
+        case 'z':
+          zclient_serv_path_set (optarg);
+          break;
         case 'P':
          /* Deal with atoi() returning 0 on failure, and ospf6d not
              listening on ospf6d port... */
@@ -281,6 +292,13 @@
         }
     }
 
+  if (geteuid () != 0)
+    {
+      errno = EPERM;
+      perror (progname);
+      exit (1);
+    }
+
   /* thread master */
   master = thread_master_create ();
 
diff --git a/ospf6d/ospf6_message.c b/ospf6d/ospf6_message.c
index 51933b7..01d6126 100644
--- a/ospf6d/ospf6_message.c
+++ b/ospf6d/ospf6_message.c
@@ -39,12 +39,55 @@
 #include "ospf6_neighbor.h"
 #include "ospf6_interface.h"
 
+/* for structures and macros ospf6_lsa_examin() needs */
+#include "ospf6_abr.h"
+#include "ospf6_asbr.h"
+#include "ospf6_intra.h"
+
 #include "ospf6_flood.h"
 #include "ospf6d.h"
 
+#include <netinet/ip6.h>
+
 unsigned char conf_debug_ospf6_message[6] = {0x03, 0, 0, 0, 0, 0};
-const char *ospf6_message_type_str[] =
-  { "Unknown", "Hello", "DbDesc", "LSReq", "LSUpdate", "LSAck" };
+static const struct message ospf6_message_type_str [] =
+{
+  { OSPF6_MESSAGE_TYPE_HELLO,    "Hello"    },
+  { OSPF6_MESSAGE_TYPE_DBDESC,   "DbDesc"   },
+  { OSPF6_MESSAGE_TYPE_LSREQ,    "LSReq"    },
+  { OSPF6_MESSAGE_TYPE_LSUPDATE, "LSUpdate" },
+  { OSPF6_MESSAGE_TYPE_LSACK,    "LSAck"    },
+};
+static const size_t ospf6_message_type_str_max =
+  sizeof (ospf6_message_type_str) / sizeof (ospf6_message_type_str[0]);
+
+/* Minimum (besides the standard OSPF packet header) lengths for OSPF
+   packets of particular types, offset is the "type" field. */
+const u_int16_t ospf6_packet_minlen[OSPF6_MESSAGE_TYPE_ALL] =
+{
+  0,
+  OSPF6_HELLO_MIN_SIZE,
+  OSPF6_DB_DESC_MIN_SIZE,
+  OSPF6_LS_REQ_MIN_SIZE,
+  OSPF6_LS_UPD_MIN_SIZE,
+  OSPF6_LS_ACK_MIN_SIZE
+};
+
+/* Minimum (besides the standard LSA header) lengths for LSAs of particular
+   types, offset is the "LSA function code" portion of "LSA type" field. */
+const u_int16_t ospf6_lsa_minlen[OSPF6_LSTYPE_SIZE] =
+{
+  0,
+  /* 0x2001 */ OSPF6_ROUTER_LSA_MIN_SIZE,
+  /* 0x2002 */ OSPF6_NETWORK_LSA_MIN_SIZE,
+  /* 0x2003 */ OSPF6_INTER_PREFIX_LSA_MIN_SIZE,
+  /* 0x2004 */ OSPF6_INTER_ROUTER_LSA_FIX_SIZE,
+  /* 0x4005 */ OSPF6_AS_EXTERNAL_LSA_MIN_SIZE,
+  /* 0x2006 */ 0,
+  /* 0x2007 */ OSPF6_AS_EXTERNAL_LSA_MIN_SIZE,
+  /* 0x0008 */ OSPF6_LINK_LSA_MIN_SIZE,
+  /* 0x2009 */ OSPF6_INTRA_PREFIX_LSA_MIN_SIZE
+};
 
 /* print functions */
 
@@ -93,8 +136,7 @@
       zlog_debug ("    Neighbor: %s", neighbor);
     }
 
-  if (p != OSPF6_MESSAGE_END (oh))
-    zlog_debug ("Trailing garbage exists");
+  assert (p == OSPF6_MESSAGE_END (oh));
 }
 
 void
@@ -126,8 +168,7 @@
        p += sizeof (struct ospf6_lsa_header))
     ospf6_lsa_header_print_raw ((struct ospf6_lsa_header *) p);
 
-  if (p != OSPF6_MESSAGE_END (oh))
-    zlog_debug ("Trailing garbage exists");
+  assert (p == OSPF6_MESSAGE_END (oh));
 }
 
 void
@@ -150,8 +191,7 @@
                  ospf6_lstype_name (e->type), id, adv_router);
     }
 
-  if (p != OSPF6_MESSAGE_END (oh))
-    zlog_debug ("Trailing garbage exists");
+  assert (p == OSPF6_MESSAGE_END (oh));
 }
 
 void
@@ -176,35 +216,9 @@
        p += OSPF6_LSA_SIZE (p))
     {
       ospf6_lsa_header_print_raw ((struct ospf6_lsa_header *) p);
-      if (OSPF6_LSA_SIZE (p) < sizeof (struct ospf6_lsa_header))
-        {
-          zlog_debug ("    Malformed LSA length, quit printing");
-          break;
-        }
     }
 
-  if (p != OSPF6_MESSAGE_END (oh))
-    {
-      char buf[32];
-
-      int num = 0;
-      memset (buf, 0, sizeof (buf));
-
-      zlog_debug ("    Trailing garbage exists");
-      while (p < OSPF6_MESSAGE_END (oh))
-        {
-          snprintf (buf, sizeof (buf), "%s %2x", buf, *p++);
-          num++;
-          if (num == 8)
-            {
-              zlog_debug ("    %s", buf);
-              memset (buf, 0, sizeof (buf));
-              num = 0;
-            }
-        }
-      if (num)
-        zlog_debug ("    %s", buf);
-    }
+  assert (p == OSPF6_MESSAGE_END (oh));
 }
 
 void
@@ -220,56 +234,7 @@
        p += sizeof (struct ospf6_lsa_header))
     ospf6_lsa_header_print_raw ((struct ospf6_lsa_header *) p);
 
-  if (p != OSPF6_MESSAGE_END (oh))
-    zlog_debug ("Trailing garbage exists");
-}
-
-/* Receive function */
-#define MSG_OK    0
-#define MSG_NG    1
-static int
-ospf6_header_examin (struct in6_addr *src, struct in6_addr *dst,
-                     struct ospf6_interface *oi, struct ospf6_header *oh)
-{
-  u_char type;
-  type = OSPF6_MESSAGE_TYPE_CANONICAL (oh->type);
-
-  /* version check */
-  if (oh->version != OSPFV3_VERSION)
-    {
-      if (IS_OSPF6_DEBUG_MESSAGE (type, RECV))
-        zlog_debug ("Message with unknown version");
-      return MSG_NG;
-    }
-
-  /* Area-ID check */
-  if (oh->area_id != oi->area->area_id)
-    {
-      if (oh->area_id == BACKBONE_AREA_ID)
-        {
-          if (IS_OSPF6_DEBUG_MESSAGE (type, RECV))
-            zlog_debug ("Message may be via Virtual Link: not supported");
-          return MSG_NG;
-        }
-
-      if (IS_OSPF6_DEBUG_MESSAGE (type, RECV))
-        zlog_debug ("Area-ID mismatch");
-      return MSG_NG;
-    }
-
-  /* Instance-ID check */
-  if (oh->instance_id != oi->instance_id)
-    {
-      if (IS_OSPF6_DEBUG_MESSAGE (type, RECV))
-        zlog_debug ("Instance-ID mismatch");
-      return MSG_NG;
-    }
-
-  /* Router-ID check */
-  if (oh->router_id == oi->area->ospf6->router_id)
-    zlog_warn ("Detect duplicate Router-ID");
-
-  return MSG_OK;
+  assert (p == OSPF6_MESSAGE_END (oh));
 }
 
 static void
@@ -283,9 +248,6 @@
   int neighborchange = 0;
   int backupseen = 0;
 
-  if (ospf6_header_examin (src, dst, oi, oh) != MSG_OK)
-    return;
-
   hello = (struct ospf6_hello *)
     ((caddr_t) oh + sizeof (struct ospf6_header));
 
@@ -339,11 +301,7 @@
         twoway++;
     }
 
-  if (p != OSPF6_MESSAGE_END (oh))
-    {
-      if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
-        zlog_debug ("Trailing garbage ignored");
-    }
+  assert (p == OSPF6_MESSAGE_END (oh));
 
   /* RouterPriority check */
   if (on->priority != hello->priority)
@@ -576,11 +534,7 @@
         }
     }
 
-  if (p != OSPF6_MESSAGE_END (oh))
-    {
-      if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
-        zlog_debug ("Trailing garbage ignored");
-    }
+  assert (p == OSPF6_MESSAGE_END (oh));
 
   /* Increment sequence number */
   on->dbdesc_seqnum ++;
@@ -788,11 +742,7 @@
         ospf6_lsa_delete (his);
     }
 
-  if (p != OSPF6_MESSAGE_END (oh))
-    {
-      if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
-        zlog_debug ("Trailing garbage ignored");
-    }
+  assert (p == OSPF6_MESSAGE_END (oh));
 
   /* Set sequence number to Master's */
   on->dbdesc_seqnum = ntohl (dbdesc->seqnum);
@@ -817,9 +767,6 @@
   struct ospf6_neighbor *on;
   struct ospf6_dbdesc *dbdesc;
 
-  if (ospf6_header_examin (src, dst, oi, oh) != MSG_OK)
-    return;
-
   on = ospf6_neighbor_lookup (oh->router_id, oi);
   if (on == NULL)
     {
@@ -869,9 +816,6 @@
   struct ospf6_lsdb *lsdb = NULL;
   struct ospf6_lsa *lsa;
 
-  if (ospf6_header_examin (src, dst, oi, oh) != MSG_OK)
-    return;
-
   on = ospf6_neighbor_lookup (oh->router_id, oi);
   if (on == NULL)
     {
@@ -934,11 +878,7 @@
       ospf6_lsdb_add (ospf6_lsa_copy (lsa), on->lsupdate_list);
     }
 
-  if (p != OSPF6_MESSAGE_END (oh))
-    {
-      if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
-        zlog_debug ("Trailing garbage ignored");
-    }
+  assert (p == OSPF6_MESSAGE_END (oh));
 
   /* schedule send lsupdate */
   THREAD_OFF (on->thread_send_lsupdate);
@@ -946,18 +886,441 @@
     thread_add_event (master, ospf6_lsupdate_send_neighbor, on, 0);
 }
 
+/* Verify, that the specified memory area contains exactly N valid IPv6
+   prefixes as specified by RFC5340, A.4.1. */
+static unsigned
+ospf6_prefixes_examin
+(
+  struct ospf6_prefix *current, /* start of buffer    */
+  unsigned length,
+  const u_int32_t req_num_pfxs  /* always compared with the actual number of prefixes */
+)
+{
+  u_char requested_pfx_bytes;
+  u_int32_t real_num_pfxs = 0;
+
+  while (length)
+  {
+    if (length < OSPF6_PREFIX_MIN_SIZE)
+    {
+      if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
+        zlog_debug ("%s: undersized IPv6 prefix header", __func__);
+      return MSG_NG;
+    }
+    /* safe to look deeper */
+    if (current->prefix_length > IPV6_MAX_BITLEN)
+    {
+      if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
+        zlog_debug ("%s: invalid PrefixLength (%u bits)", __func__, current->prefix_length);
+      return MSG_NG;
+    }
+    /* covers both fixed- and variable-sized fields */
+    requested_pfx_bytes = OSPF6_PREFIX_MIN_SIZE + OSPF6_PREFIX_SPACE (current->prefix_length);
+    if (requested_pfx_bytes > length)
+    {
+      if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
+        zlog_debug ("%s: undersized IPv6 prefix", __func__);
+      return MSG_NG;
+    }
+    /* next prefix */
+    length -= requested_pfx_bytes;
+    current = (struct ospf6_prefix *) ((caddr_t) current + requested_pfx_bytes);
+    real_num_pfxs++;
+  }
+  if (real_num_pfxs != req_num_pfxs)
+  {
+    if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
+      zlog_debug ("%s: IPv6 prefix number mismatch (%u required, %u real)",
+                  __func__, req_num_pfxs, real_num_pfxs);
+    return MSG_NG;
+  }
+  return MSG_OK;
+}
+
+/* Verify an LSA to have a valid length and dispatch further (where
+   appropriate) to check if the contents, including nested IPv6 prefixes,
+   is properly sized/aligned within the LSA. Note that this function gets
+   LSA type in network byte order, uses in host byte order and passes to
+   ospf6_lstype_name() in network byte order again. */
+static unsigned
+ospf6_lsa_examin (struct ospf6_lsa_header *lsah, const u_int16_t lsalen, const u_char headeronly)
+{
+  struct ospf6_intra_prefix_lsa *intra_prefix_lsa;
+  struct ospf6_as_external_lsa *as_external_lsa;
+  struct ospf6_link_lsa *link_lsa;
+  unsigned exp_length;
+  u_int8_t ltindex;
+  u_int16_t lsatype;
+
+  /* In case an additional minimum length constraint is defined for current
+     LSA type, make sure that this constraint is met. */
+  lsatype = ntohs (lsah->type);
+  ltindex = lsatype & OSPF6_LSTYPE_FCODE_MASK;
+  if
+  (
+    ltindex < OSPF6_LSTYPE_SIZE &&
+    ospf6_lsa_minlen[ltindex] &&
+    lsalen < ospf6_lsa_minlen[ltindex] + OSPF6_LSA_HEADER_SIZE
+  )
+  {
+    if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
+      zlog_debug ("%s: undersized (%u B) LSA", __func__, lsalen);
+    return MSG_NG;
+  }
+  switch (lsatype)
+  {
+  case OSPF6_LSTYPE_ROUTER:
+    /* RFC5340 A.4.3, LSA header + OSPF6_ROUTER_LSA_MIN_SIZE bytes followed
+       by N>=0 interface descriptions. */
+    if ((lsalen - OSPF6_LSA_HEADER_SIZE - OSPF6_ROUTER_LSA_MIN_SIZE) % OSPF6_ROUTER_LSDESC_FIX_SIZE)
+    {
+      if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
+        zlog_debug ("%s: interface description alignment error", __func__);
+      return MSG_NG;
+    }
+    break;
+  case OSPF6_LSTYPE_NETWORK:
+    /* RFC5340 A.4.4, LSA header + OSPF6_NETWORK_LSA_MIN_SIZE bytes
+       followed by N>=0 attached router descriptions. */
+    if ((lsalen - OSPF6_LSA_HEADER_SIZE - OSPF6_NETWORK_LSA_MIN_SIZE) % OSPF6_NETWORK_LSDESC_FIX_SIZE)
+    {
+      if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
+        zlog_debug ("%s: router description alignment error", __func__);
+      return MSG_NG;
+    }
+    break;
+  case OSPF6_LSTYPE_INTER_PREFIX:
+    /* RFC5340 A.4.5, LSA header + OSPF6_INTER_PREFIX_LSA_MIN_SIZE bytes
+       followed by 3-4 fields of a single IPv6 prefix. */
+    if (headeronly)
+      break;
+    return ospf6_prefixes_examin
+    (
+      (struct ospf6_prefix *) ((caddr_t) lsah + OSPF6_LSA_HEADER_SIZE + OSPF6_INTER_PREFIX_LSA_MIN_SIZE),
+      lsalen - OSPF6_LSA_HEADER_SIZE - OSPF6_INTER_PREFIX_LSA_MIN_SIZE,
+      1
+    );
+  case OSPF6_LSTYPE_INTER_ROUTER:
+    /* RFC5340 A.4.6, fixed-size LSA. */
+    if (lsalen > OSPF6_LSA_HEADER_SIZE + OSPF6_INTER_ROUTER_LSA_FIX_SIZE)
+    {
+      if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
+        zlog_debug ("%s: oversized (%u B) LSA", __func__, lsalen);
+      return MSG_NG;
+    }
+    break;
+  case OSPF6_LSTYPE_AS_EXTERNAL: /* RFC5340 A.4.7, same as A.4.8. */
+  case OSPF6_LSTYPE_TYPE_7:
+    /* RFC5340 A.4.8, LSA header + OSPF6_AS_EXTERNAL_LSA_MIN_SIZE bytes
+       followed by 3-4 fields of IPv6 prefix and 3 conditional LSA fields:
+       16 bytes of forwarding address, 4 bytes of external route tag,
+       4 bytes of referenced link state ID. */
+    if (headeronly)
+      break;
+    as_external_lsa = (struct ospf6_as_external_lsa *) ((caddr_t) lsah + OSPF6_LSA_HEADER_SIZE);
+    exp_length = OSPF6_LSA_HEADER_SIZE + OSPF6_AS_EXTERNAL_LSA_MIN_SIZE;
+    /* To find out if the last optional field (Referenced Link State ID) is
+       assumed in this LSA, we need to access fixed fields of the IPv6
+       prefix before ospf6_prefix_examin() confirms its sizing. */
+    if (exp_length + OSPF6_PREFIX_MIN_SIZE > lsalen)
+    {
+      if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
+        zlog_debug ("%s: undersized (%u B) LSA header", __func__, lsalen);
+      return MSG_NG;
+    }
+    /* forwarding address */
+    if (CHECK_FLAG (as_external_lsa->bits_metric, OSPF6_ASBR_BIT_F))
+      exp_length += 16;
+    /* external route tag */
+    if (CHECK_FLAG (as_external_lsa->bits_metric, OSPF6_ASBR_BIT_T))
+      exp_length += 4;
+    /* referenced link state ID */
+    if (as_external_lsa->prefix.u._prefix_referenced_lstype)
+      exp_length += 4;
+    /* All the fixed-size fields (mandatory and optional) must fit. I.e.,
+       this check does not include any IPv6 prefix fields. */
+    if (exp_length > lsalen)
+    {
+      if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
+        zlog_debug ("%s: undersized (%u B) LSA header", __func__, lsalen);
+      return MSG_NG;
+    }
+    /* The last call completely covers the remainder (IPv6 prefix). */
+    return ospf6_prefixes_examin
+    (
+      (struct ospf6_prefix *) ((caddr_t) as_external_lsa + OSPF6_AS_EXTERNAL_LSA_MIN_SIZE),
+      lsalen - exp_length,
+      1
+    );
+  case OSPF6_LSTYPE_LINK:
+    /* RFC5340 A.4.9, LSA header + OSPF6_LINK_LSA_MIN_SIZE bytes followed
+       by N>=0 IPv6 prefix blocks (with N declared beforehand). */
+    if (headeronly)
+      break;
+    link_lsa = (struct ospf6_link_lsa *) ((caddr_t) lsah + OSPF6_LSA_HEADER_SIZE);
+    return ospf6_prefixes_examin
+    (
+      (struct ospf6_prefix *) ((caddr_t) link_lsa + OSPF6_LINK_LSA_MIN_SIZE),
+      lsalen - OSPF6_LSA_HEADER_SIZE - OSPF6_LINK_LSA_MIN_SIZE,
+      ntohl (link_lsa->prefix_num) /* 32 bits */
+    );
+  case OSPF6_LSTYPE_INTRA_PREFIX:
+  /* RFC5340 A.4.10, LSA header + OSPF6_INTRA_PREFIX_LSA_MIN_SIZE bytes
+     followed by N>=0 IPv6 prefixes (with N declared beforehand). */
+    if (headeronly)
+      break;
+    intra_prefix_lsa = (struct ospf6_intra_prefix_lsa *) ((caddr_t) lsah + OSPF6_LSA_HEADER_SIZE);
+    return ospf6_prefixes_examin
+    (
+      (struct ospf6_prefix *) ((caddr_t) intra_prefix_lsa + OSPF6_INTRA_PREFIX_LSA_MIN_SIZE),
+      lsalen - OSPF6_LSA_HEADER_SIZE - OSPF6_INTRA_PREFIX_LSA_MIN_SIZE,
+      ntohs (intra_prefix_lsa->prefix_num) /* 16 bits */
+    );
+  }
+  /* No additional validation is possible for unknown LSA types, which are
+     themselves valid in OPSFv3, hence the default decision is to accept. */
+  return MSG_OK;
+}
+
+/* Verify if the provided input buffer is a valid sequence of LSAs. This
+   includes verification of LSA blocks length/alignment and dispatching
+   of deeper-level checks. */
+static unsigned
+ospf6_lsaseq_examin
+(
+  struct ospf6_lsa_header *lsah, /* start of buffered data */
+  size_t length,
+  const u_char headeronly,
+  /* When declared_num_lsas is not 0, compare it to the real number of LSAs
+     and treat the difference as an error. */
+  const u_int32_t declared_num_lsas
+)
+{
+  u_int32_t counted_lsas = 0;
+
+  while (length)
+  {
+    u_int16_t lsalen;
+    if (length < OSPF6_LSA_HEADER_SIZE)
+    {
+      if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
+        zlog_debug ("%s: undersized (%zu B) trailing (#%u) LSA header",
+                    __func__, length, counted_lsas);
+      return MSG_NG;
+    }
+    /* save on ntohs() calls here and in the LSA validator */
+    lsalen = OSPF6_LSA_SIZE (lsah);
+    if (lsalen < OSPF6_LSA_HEADER_SIZE)
+    {
+      if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
+        zlog_debug ("%s: malformed LSA header #%u, declared length is %u B",
+                    __func__, counted_lsas, lsalen);
+      return MSG_NG;
+    }
+    if (headeronly)
+    {
+      /* less checks here and in ospf6_lsa_examin() */
+      if (MSG_OK != ospf6_lsa_examin (lsah, lsalen, 1))
+      {
+        if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
+          zlog_debug ("%s: anomaly in header-only %s LSA #%u", __func__,
+                      ospf6_lstype_name (lsah->type), counted_lsas);
+        return MSG_NG;
+      }
+      lsah = (struct ospf6_lsa_header *) ((caddr_t) lsah + OSPF6_LSA_HEADER_SIZE);
+      length -= OSPF6_LSA_HEADER_SIZE;
+    }
+    else
+    {
+      /* make sure the input buffer is deep enough before further checks */
+      if (lsalen > length)
+      {
+        if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
+          zlog_debug ("%s: anomaly in %s LSA #%u: declared length is %u B, buffered length is %zu B",
+                      __func__, ospf6_lstype_name (lsah->type), counted_lsas, lsalen, length);
+        return MSG_NG;
+      }
+      if (MSG_OK != ospf6_lsa_examin (lsah, lsalen, 0))
+      {
+        if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
+          zlog_debug ("%s: anomaly in %s LSA #%u", __func__,
+                      ospf6_lstype_name (lsah->type), counted_lsas);
+        return MSG_NG;
+      }
+      lsah = (struct ospf6_lsa_header *) ((caddr_t) lsah + lsalen);
+      length -= lsalen;
+    }
+    counted_lsas++;
+  }
+
+  if (declared_num_lsas && counted_lsas != declared_num_lsas)
+  {
+    if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
+      zlog_debug ("%s: #LSAs declared (%u) does not match actual (%u)",
+                  __func__, declared_num_lsas, counted_lsas);
+    return MSG_NG;
+  }
+  return MSG_OK;
+}
+
+/* Verify a complete OSPF packet for proper sizing/alignment. */
+static unsigned
+ospf6_packet_examin (struct ospf6_header *oh, const unsigned bytesonwire)
+{
+  struct ospf6_lsupdate *lsupd;
+  unsigned test;
+
+  /* length, 1st approximation */
+  if (bytesonwire < OSPF6_HEADER_SIZE)
+  {
+    if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
+      zlog_debug ("%s: undersized (%u B) packet", __func__, bytesonwire);
+    return MSG_NG;
+  }
+  /* Now it is safe to access header fields. */
+  if (bytesonwire != ntohs (oh->length))
+  {
+    if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
+      zlog_debug ("%s: packet length error (%u real, %u declared)",
+                  __func__, bytesonwire, ntohs (oh->length));
+    return MSG_NG;
+  }
+  /* version check */
+  if (oh->version != OSPFV3_VERSION)
+  {
+    if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
+      zlog_debug ("%s: invalid (%u) protocol version", __func__, oh->version);
+    return MSG_NG;
+  }
+  /* length, 2nd approximation */
+  if
+  (
+    oh->type < OSPF6_MESSAGE_TYPE_ALL &&
+    ospf6_packet_minlen[oh->type] &&
+    bytesonwire < OSPF6_HEADER_SIZE + ospf6_packet_minlen[oh->type]
+  )
+  {
+    if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
+      zlog_debug ("%s: undersized (%u B) %s packet", __func__,
+                  bytesonwire, LOOKUP (ospf6_message_type_str, oh->type));
+    return MSG_NG;
+  }
+  /* type-specific deeper validation */
+  switch (oh->type)
+  {
+  case OSPF6_MESSAGE_TYPE_HELLO:
+    /* RFC5340 A.3.2, packet header + OSPF6_HELLO_MIN_SIZE bytes followed
+       by N>=0 router-IDs. */
+    if (0 == (bytesonwire - OSPF6_HEADER_SIZE - OSPF6_HELLO_MIN_SIZE) % 4)
+      return MSG_OK;
+    if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
+      zlog_debug ("%s: alignment error in %s packet",
+                  __func__, LOOKUP (ospf6_message_type_str, oh->type));
+    return MSG_NG;
+  case OSPF6_MESSAGE_TYPE_DBDESC:
+    /* RFC5340 A.3.3, packet header + OSPF6_DB_DESC_MIN_SIZE bytes followed
+       by N>=0 header-only LSAs. */
+    test = ospf6_lsaseq_examin
+    (
+      (struct ospf6_lsa_header *) ((caddr_t) oh + OSPF6_HEADER_SIZE + OSPF6_DB_DESC_MIN_SIZE),
+      bytesonwire - OSPF6_HEADER_SIZE - OSPF6_DB_DESC_MIN_SIZE,
+      1,
+      0
+    );
+    break;
+  case OSPF6_MESSAGE_TYPE_LSREQ:
+    /* RFC5340 A.3.4, packet header + N>=0 LS description blocks. */
+    if (0 == (bytesonwire - OSPF6_HEADER_SIZE - OSPF6_LS_REQ_MIN_SIZE) % OSPF6_LSREQ_LSDESC_FIX_SIZE)
+      return MSG_OK;
+    if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
+      zlog_debug ("%s: alignment error in %s packet",
+                  __func__, LOOKUP (ospf6_message_type_str, oh->type));
+    return MSG_NG;
+  case OSPF6_MESSAGE_TYPE_LSUPDATE:
+    /* RFC5340 A.3.5, packet header + OSPF6_LS_UPD_MIN_SIZE bytes followed
+       by N>=0 full LSAs (with N declared beforehand). */
+    lsupd = (struct ospf6_lsupdate *) ((caddr_t) oh + OSPF6_HEADER_SIZE);
+    test = ospf6_lsaseq_examin
+    (
+      (struct ospf6_lsa_header *) ((caddr_t) lsupd + OSPF6_LS_UPD_MIN_SIZE),
+      bytesonwire - OSPF6_HEADER_SIZE - OSPF6_LS_UPD_MIN_SIZE,
+      0,
+      ntohl (lsupd->lsa_number) /* 32 bits */
+    );
+    break;
+  case OSPF6_MESSAGE_TYPE_LSACK:
+    /* RFC5340 A.3.6, packet header + N>=0 header-only LSAs. */
+    test = ospf6_lsaseq_examin
+    (
+      (struct ospf6_lsa_header *) ((caddr_t) oh + OSPF6_HEADER_SIZE + OSPF6_LS_ACK_MIN_SIZE),
+      bytesonwire - OSPF6_HEADER_SIZE - OSPF6_LS_ACK_MIN_SIZE,
+      1,
+      0
+    );
+    break;
+  default:
+    if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
+      zlog_debug ("%s: invalid (%u) message type", __func__, oh->type);
+    return MSG_NG;
+  }
+  if (test != MSG_OK && IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
+    zlog_debug ("%s: anomaly in %s packet", __func__, LOOKUP (ospf6_message_type_str, oh->type));
+  return test;
+}
+
+/* Verify particular fields of otherwise correct received OSPF packet to
+   meet the requirements of RFC. */
+static int
+ospf6_rxpacket_examin (struct ospf6_interface *oi, struct ospf6_header *oh, const unsigned bytesonwire)
+{
+  char buf[2][INET_ADDRSTRLEN];
+
+  if (MSG_OK != ospf6_packet_examin (oh, bytesonwire))
+    return MSG_NG;
+
+  /* Area-ID check */
+  if (oh->area_id != oi->area->area_id)
+  {
+    if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
+    {
+      if (oh->area_id == BACKBONE_AREA_ID)
+        zlog_debug ("%s: Message may be via Virtual Link: not supported", __func__);
+      else
+        zlog_debug
+        (
+          "%s: Area-ID mismatch (my %s, rcvd %s)", __func__,
+          inet_ntop (AF_INET, &oi->area->area_id, buf[0], INET_ADDRSTRLEN),
+          inet_ntop (AF_INET, &oh->area_id, buf[1], INET_ADDRSTRLEN)
+         );
+    }
+    return MSG_NG;
+  }
+
+  /* Instance-ID check */
+  if (oh->instance_id != oi->instance_id)
+  {
+    if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
+      zlog_debug ("%s: Instance-ID mismatch (my %u, rcvd %u)", __func__, oi->instance_id, oh->instance_id);
+    return MSG_NG;
+  }
+
+  /* Router-ID check */
+  if (oh->router_id == oi->area->ospf6->router_id)
+  {
+    zlog_warn ("%s: Duplicate Router-ID (%s)", __func__, inet_ntop (AF_INET, &oh->router_id, buf[0], INET_ADDRSTRLEN));
+    return MSG_NG;
+  }
+  return MSG_OK;
+}
+
 static void
 ospf6_lsupdate_recv (struct in6_addr *src, struct in6_addr *dst,
                      struct ospf6_interface *oi, struct ospf6_header *oh)
 {
   struct ospf6_neighbor *on;
   struct ospf6_lsupdate *lsupdate;
-  unsigned long num;
   char *p;
 
-  if (ospf6_header_examin (src, dst, oi, oh) != MSG_OK)
-    return;
-
   on = ospf6_neighbor_lookup (oh->router_id, oi);
   if (on == NULL)
     {
@@ -978,37 +1341,16 @@
   lsupdate = (struct ospf6_lsupdate *)
     ((caddr_t) oh + sizeof (struct ospf6_header));
 
-  num = ntohl (lsupdate->lsa_number);
-
   /* Process LSAs */
   for (p = (char *) ((caddr_t) lsupdate + sizeof (struct ospf6_lsupdate));
        p < OSPF6_MESSAGE_END (oh) &&
        p + OSPF6_LSA_SIZE (p) <= OSPF6_MESSAGE_END (oh);
        p += OSPF6_LSA_SIZE (p))
     {
-      if (num == 0)
-        break;
-      if (OSPF6_LSA_SIZE (p) < sizeof (struct ospf6_lsa_header))
-        {
-          if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
-            zlog_debug ("Malformed LSA length, quit processing");
-          break;
-        }
-
       ospf6_receive_lsa (on, (struct ospf6_lsa_header *) p);
-      num--;
     }
 
-  if (num != 0)
-    {
-      if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
-        zlog_debug ("Malformed LSA number or LSA length");
-    }
-  if (p != OSPF6_MESSAGE_END (oh))
-    {
-      if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
-        zlog_debug ("Trailing garbage ignored");
-    }
+  assert (p == OSPF6_MESSAGE_END (oh));
 
   /* RFC2328 Section 10.9: When the neighbor responds to these requests
      with the proper Link State Update packet(s), the Link state request
@@ -1035,8 +1377,6 @@
   struct ospf6_lsdb *lsdb = NULL;
 
   assert (oh->type == OSPF6_MESSAGE_TYPE_LSACK);
-  if (ospf6_header_examin (src, dst, oi, oh) != MSG_OK)
-    return;
 
   on = ospf6_neighbor_lookup (oh->router_id, oi);
   if (on == NULL)
@@ -1126,11 +1466,7 @@
       ospf6_lsa_delete (his);
     }
 
-  if (p != OSPF6_MESSAGE_END (oh))
-    {
-      if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
-        zlog_debug ("Trailing garbage ignored");
-    }
+  assert (p == OSPF6_MESSAGE_END (oh));
 }
 
 static u_char *recvbuf = NULL;
@@ -1219,11 +1555,6 @@
       zlog_err ("Excess message read");
       return 0;
     }
-  else if (len < sizeof (struct ospf6_header))
-    {
-      zlog_err ("Deficient message read");
-      return 0;
-    }
 
   oi = ospf6_interface_lookup_by_ifindex (ifindex);
   if (oi == NULL || oi->area == NULL)
@@ -1231,8 +1562,22 @@
       zlog_debug ("Message received on disabled interface");
       return 0;
     }
+  if (CHECK_FLAG (oi->flag, OSPF6_INTERFACE_PASSIVE))
+    {
+      if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
+        zlog_debug ("%s: Ignore message on passive interface %s",
+                    __func__, oi->interface->name);
+      return 0;
+    }
 
   oh = (struct ospf6_header *) recvbuf;
+  if (ospf6_rxpacket_examin (oi, oh, len) != MSG_OK)
+    return 0;
+
+  /* Being here means, that no sizing/alignment issues were detected in
+     the input packet. This renders the additional checks performed below
+     and also in the type-specific dispatching functions a dead code,
+     which can be dismissed in a cleanup-focused review round later. */
 
   /* Log */
   if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
@@ -1240,11 +1585,9 @@
       inet_ntop (AF_INET6, &src, srcname, sizeof (srcname));
       inet_ntop (AF_INET6, &dst, dstname, sizeof (dstname));
       zlog_debug ("%s received on %s",
-                 OSPF6_MESSAGE_TYPE_NAME (oh->type), oi->interface->name);
+                 LOOKUP (ospf6_message_type_str, oh->type), oi->interface->name);
       zlog_debug ("    src: %s", srcname);
       zlog_debug ("    dst: %s", dstname);
-      if (len != ntohs (oh->length))
-        zlog_debug ("Message length does not match actually received: %d", len);
 
       switch (oh->type)
         {
@@ -1264,19 +1607,10 @@
             ospf6_lsack_print (oh);
             break;
           default:
-            zlog_debug ("Unknown message");
-            break;
+            assert (0);
         }
     }
 
-  if (CHECK_FLAG (oi->flag, OSPF6_INTERFACE_PASSIVE))
-    {
-      if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
-        zlog_debug ("Ignore message on passive interface %s",
-                   oi->interface->name);
-      return 0;
-    }
-
   switch (oh->type)
     {
       case OSPF6_MESSAGE_TYPE_HELLO:
@@ -1300,9 +1634,7 @@
         break;
 
       default:
-        if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
-          zlog_debug ("Unknown message");
-        break;
+        assert (0);
     }
 
   return 0;
@@ -1341,7 +1673,7 @@
       else
         memset (srcname, 0, sizeof (srcname));
       zlog_debug ("%s send on %s",
-                 OSPF6_MESSAGE_TYPE_NAME (oh->type), oi->interface->name);
+                 LOOKUP (ospf6_message_type_str, oh->type), oi->interface->name);
       zlog_debug ("    src: %s", srcname);
       zlog_debug ("    dst: %s", dstname);
 
@@ -1375,6 +1707,13 @@
     zlog_err ("Could not send entire message");
 }
 
+static uint32_t
+ospf6_packet_max(struct ospf6_interface *oi)
+{
+  assert (oi->ifmtu > sizeof (struct ip6_hdr));
+  return oi->ifmtu - (sizeof (struct ip6_hdr));
+}
+
 int
 ospf6_hello_send (struct thread *thread)
 {
@@ -1421,7 +1760,7 @@
       if (on->state < OSPF6_NEIGHBOR_INIT)
         continue;
 
-      if (p - sendbuf + sizeof (u_int32_t) > oi->ifmtu)
+      if (p - sendbuf + sizeof (u_int32_t) > ospf6_packet_max(oi))
         {
           if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_HELLO, SEND))
             zlog_debug ("sending Hello message: exceeds I/F MTU");
@@ -1497,7 +1836,7 @@
 
           /* MTU check */
           if (p - sendbuf + sizeof (struct ospf6_lsa_header) >
-              on->ospf6_if->ifmtu)
+              ospf6_packet_max(on->ospf6_if))
             {
               ospf6_lsa_unlock (lsa);
               break;
@@ -1531,7 +1870,7 @@
   for (lsa = ospf6_lsdb_head (on->summary_list); lsa;
        lsa = ospf6_lsdb_next (lsa))
     {
-      if (size + sizeof (struct ospf6_lsa_header) > on->ospf6_if->ifmtu)
+      if (size + sizeof (struct ospf6_lsa_header) > ospf6_packet_max(on->ospf6_if))
         {
           ospf6_lsa_unlock (lsa);
           break;
@@ -1598,7 +1937,7 @@
        lsa = ospf6_lsdb_next (lsa))
     {
       /* MTU check */
-      if (p - sendbuf + sizeof (struct ospf6_lsreq_entry) > on->ospf6_if->ifmtu)
+      if (p - sendbuf + sizeof (struct ospf6_lsreq_entry) > ospf6_packet_max(on->ospf6_if))
         {
           ospf6_lsa_unlock (lsa);
           break;
@@ -1667,7 +2006,7 @@
     {
       /* MTU check */
       if ( (p - sendbuf + (unsigned int)OSPF6_LSA_SIZE (lsa->header))
-          > on->ospf6_if->ifmtu)
+          > ospf6_packet_max(on->ospf6_if))
         {
           ospf6_lsa_unlock (lsa);
           break;
@@ -1687,7 +2026,7 @@
     {
       /* MTU check */
       if ( (p - sendbuf + (unsigned int)OSPF6_LSA_SIZE (lsa->header))
-          > on->ospf6_if->ifmtu)
+          > ospf6_packet_max(on->ospf6_if))
         {
           ospf6_lsa_unlock (lsa);
           break;
@@ -1760,7 +2099,7 @@
     {
       /* MTU check */
       if ( (p - sendbuf + ((unsigned int)OSPF6_LSA_SIZE (lsa->header)))
-          > oi->ifmtu)
+          > ospf6_packet_max(oi))
         {
           ospf6_lsa_unlock (lsa);
           break;
@@ -1827,7 +2166,7 @@
        lsa = ospf6_lsdb_next (lsa))
     {
       /* MTU check */
-      if (p - sendbuf + sizeof (struct ospf6_lsa_header) > on->ospf6_if->ifmtu)
+      if (p - sendbuf + sizeof (struct ospf6_lsa_header) > ospf6_packet_max(on->ospf6_if))
         {
           /* if we run out of packet size/space here,
              better to try again soon. */
@@ -1887,7 +2226,7 @@
        lsa = ospf6_lsdb_next (lsa))
     {
       /* MTU check */
-      if (p - sendbuf + sizeof (struct ospf6_lsa_header) > oi->ifmtu)
+      if (p - sendbuf + sizeof (struct ospf6_lsa_header) > ospf6_packet_max(oi))
         {
           /* if we run out of packet size/space here,
              better to try again soon. */
diff --git a/ospf6d/ospf6_message.h b/ospf6d/ospf6_message.h
index c72f0af..b085a96 100644
--- a/ospf6d/ospf6_message.h
+++ b/ospf6d/ospf6_message.h
@@ -44,14 +44,8 @@
 #define OSPF6_MESSAGE_TYPE_LSACK    0x5  /* Flooding acknowledgment */
 #define OSPF6_MESSAGE_TYPE_ALL      0x6  /* For debug option */
 
-#define OSPF6_MESSAGE_TYPE_CANONICAL(T) \
-  ((T) > OSPF6_MESSAGE_TYPE_LSACK ? OSPF6_MESSAGE_TYPE_UNKNOWN : (T))
-
-extern const char *ospf6_message_type_str[];
-#define OSPF6_MESSAGE_TYPE_NAME(T) \
-  (ospf6_message_type_str[ OSPF6_MESSAGE_TYPE_CANONICAL (T) ])
-
 /* OSPFv3 packet header */
+#define OSPF6_HEADER_SIZE                     16U
 struct ospf6_header
 {
   u_char    version;
@@ -67,6 +61,7 @@
 #define OSPF6_MESSAGE_END(H) ((caddr_t) (H) + ntohs ((H)->length))
 
 /* Hello */
+#define OSPF6_HELLO_MIN_SIZE                  20U
 struct ospf6_hello
 {
   u_int32_t interface_id;
@@ -80,6 +75,7 @@
 };
 
 /* Database Description */
+#define OSPF6_DB_DESC_MIN_SIZE                12U
 struct ospf6_dbdesc
 {
   u_char    reserved1;
@@ -96,7 +92,9 @@
 #define OSPF6_DBDESC_IBIT  (0x04) /* initial bit */
 
 /* Link State Request */
+#define OSPF6_LS_REQ_MIN_SIZE                  0U
 /* It is just a sequence of entries below */
+#define OSPF6_LSREQ_LSDESC_FIX_SIZE           12U
 struct ospf6_lsreq_entry
 {
   u_int16_t reserved;     /* Must Be Zero */
@@ -106,6 +104,7 @@
 };
 
 /* Link State Update */
+#define OSPF6_LS_UPD_MIN_SIZE                  4U
 struct ospf6_lsupdate
 {
   u_int32_t lsa_number;
@@ -113,6 +112,7 @@
 };
 
 /* Link State Acknowledgement */
+#define OSPF6_LS_ACK_MIN_SIZE                  0U
 /* It is just a sequence of LSA Headers */
 
 /* Function definition */
diff --git a/ospf6d/ospf6_network.c b/ospf6d/ospf6_network.c
index 96b82af..e5a1436 100644
--- a/ospf6d/ospf6_network.c
+++ b/ospf6d/ospf6_network.c
@@ -64,6 +64,14 @@
 }
 
 void
+ospf6_set_transport_class (void)
+{
+#ifdef IPTOS_PREC_INTERNETCONTROL
+  setsockopt_ipv6_tclass (ospf6_sock, IPTOS_PREC_INTERNETCONTROL);
+#endif
+}
+
+void
 ospf6_set_checksum (void)
 {
   int offset = 12;
@@ -102,6 +110,7 @@
 #endif /*1*/
   ospf6_reset_mcastloop ();
   ospf6_set_pktinfo ();
+  ospf6_set_transport_class ();
   ospf6_set_checksum ();
 
   /* setup global in6_addr, allspf6 and alldr6 for later use */
@@ -111,86 +120,22 @@
   return 0;
 }
 
+/* ospf6 set socket option */
 void
-ospf6_join_allspfrouters (u_int ifindex)
+ospf6_sso (u_int ifindex, struct in6_addr *group, int option)
 {
   struct ipv6_mreq mreq6;
-  int retval;
+  int ret;
 
   assert (ifindex);
   mreq6.ipv6mr_interface = ifindex;
-  memcpy (&mreq6.ipv6mr_multiaddr, &allspfrouters6,
-          sizeof (struct in6_addr));
+  memcpy (&mreq6.ipv6mr_multiaddr, group, sizeof (struct in6_addr));
 
-  retval = setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
-                       &mreq6, sizeof (mreq6));
-
-  if (retval < 0)
-    zlog_err ("Network: Join AllSPFRouters on ifindex %d failed: %s",
-              ifindex, safe_strerror (errno));
-#if 0
-  else
-    zlog_debug ("Network: Join AllSPFRouters on ifindex %d", ifindex);
-#endif
-}
-
-void
-ospf6_leave_allspfrouters (u_int ifindex)
-{
-  struct ipv6_mreq mreq6;
-
-  assert (ifindex);
-  mreq6.ipv6mr_interface = ifindex;
-  memcpy (&mreq6.ipv6mr_multiaddr, &allspfrouters6,
-          sizeof (struct in6_addr));
-
-  if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
-                  &mreq6, sizeof (mreq6)) < 0)
-    zlog_warn ("Network: Leave AllSPFRouters on ifindex %d Failed: %s",
-               ifindex, safe_strerror (errno));
-#if 0
-  else
-    zlog_debug ("Network: Leave AllSPFRouters on ifindex %d", ifindex);
-#endif
-}
-
-void
-ospf6_join_alldrouters (u_int ifindex)
-{
-  struct ipv6_mreq mreq6;
-
-  assert (ifindex);
-  mreq6.ipv6mr_interface = ifindex;
-  memcpy (&mreq6.ipv6mr_multiaddr, &alldrouters6,
-          sizeof (struct in6_addr));
-
-  if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
-                  &mreq6, sizeof (mreq6)) < 0)
-    zlog_warn ("Network: Join AllDRouters on ifindex %d Failed: %s",
-               ifindex, safe_strerror (errno));
-#if 0
-  else
-    zlog_debug ("Network: Join AllDRouters on ifindex %d", ifindex);
-#endif
-}
-
-void
-ospf6_leave_alldrouters (u_int ifindex)
-{
-  struct ipv6_mreq mreq6;
-
-  assert (ifindex);
-  mreq6.ipv6mr_interface = ifindex;
-  memcpy (&mreq6.ipv6mr_multiaddr, &alldrouters6,
-          sizeof (struct in6_addr));
-
-  if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
-                  &mreq6, sizeof (mreq6)) < 0)
-    zlog_warn ("Network: Leave AllDRouters on ifindex %d Failed", ifindex);
-#if 0
-  else
-    zlog_debug ("Network: Leave AllDRouters on ifindex %d", ifindex);
-#endif
+  ret = setsockopt (ospf6_sock, IPPROTO_IPV6, option,
+                    &mreq6, sizeof (mreq6));
+  if (ret < 0)
+    zlog_err ("Network: setsockopt (%d) on ifindex %d failed: %s",
+              option, ifindex, safe_strerror (errno));
 }
 
 static int
diff --git a/ospf6d/ospf6_network.h b/ospf6d/ospf6_network.h
index fd8758e..0526b3e 100644
--- a/ospf6d/ospf6_network.h
+++ b/ospf6d/ospf6_network.h
@@ -35,11 +35,7 @@
 extern void ospf6_set_checksum (void);
 
 extern int ospf6_serv_sock (void);
-
-extern void ospf6_join_allspfrouters (u_int);
-extern void ospf6_leave_allspfrouters (u_int);
-extern void ospf6_join_alldrouters (u_int);
-extern void ospf6_leave_alldrouters (u_int);
+extern void ospf6_sso (u_int ifindex, struct in6_addr *group, int option);
 
 extern int ospf6_sendmsg (struct in6_addr *, struct in6_addr *,
                           unsigned int *, struct iovec *);
diff --git a/ospf6d/ospf6_proto.c b/ospf6d/ospf6_proto.c
index c792aa4..d011601 100644
--- a/ospf6d/ospf6_proto.c
+++ b/ospf6d/ospf6_proto.c
@@ -42,11 +42,10 @@
       return;
     }
 
-  if (index == 16)
-    return;
-
-  pnt[index] &= mask;
-  index ++;
+  /* nonzero mask means no check for this byte because if it contains
+   * prefix bits it must be there for us to write */
+  if (mask)
+    pnt[index++] &= mask;
 
   while (index < OSPF6_PREFIX_SPACE (op->prefix_length))
     pnt[index++] = 0;
diff --git a/ospf6d/ospf6_proto.h b/ospf6d/ospf6_proto.h
index a8c1b1a..6462500 100644
--- a/ospf6d/ospf6_proto.h
+++ b/ospf6d/ospf6_proto.h
@@ -73,6 +73,7 @@
 #define OSPF6_OPT_V6 (1 << 0)   /* IPv6 forwarding Capability */
 
 /* OSPF6 Prefix */
+#define OSPF6_PREFIX_MIN_SIZE                  4U /* .length == 0 */
 struct ospf6_prefix
 {
   u_int8_t prefix_length;
diff --git a/ospf6d/ospf6_route.h b/ospf6d/ospf6_route.h
index 8dcc877..b384824 100644
--- a/ospf6d/ospf6_route.h
+++ b/ospf6d/ospf6_route.h
@@ -300,6 +300,7 @@
 extern int config_write_ospf6_debug_route (struct vty *vty);
 extern void install_element_ospf6_debug_route (void);
 extern void ospf6_route_init (void);
+extern void ospf6_clean (void);
 
 #endif /* OSPF6_ROUTE_H */
 
diff --git a/ospf6d/ospf6_spf.c b/ospf6d/ospf6_spf.c
index cb54961..a4a5b72 100644
--- a/ospf6d/ospf6_spf.c
+++ b/ospf6d/ospf6_spf.c
@@ -282,8 +282,7 @@
 {
   struct ospf6_route *route;
   int i, j;
-  struct ospf6_vertex *prev, *w;
-  struct listnode *node, *nnode;
+  struct ospf6_vertex *prev;
 
   if (IS_OSPF6_DEBUG_SPF (PROCESS))
     zlog_debug ("SPF install %s hops %d cost %d",
@@ -392,6 +391,8 @@
   caddr_t lsdesc;
   struct ospf6_lsa *lsa;
 
+  ospf6_spf_table_finish (result_table);
+
   /* Install the calculating router itself as the root of the SPF tree */
   /* construct root vertex */
   lsa = ospf6_lsdb_lookup (htons (OSPF6_LSTYPE_ROUTER), htonl (0),
@@ -403,8 +404,6 @@
   candidate_list = pqueue_create ();
   candidate_list->cmp = ospf6_vertex_cmp;
 
-  ospf6_spf_table_finish (result_table);
-
   root = ospf6_vertex_create (lsa);
   root->area = oa;
   root->cost = 0;
diff --git a/ospf6d/ospf6_top.c b/ospf6d/ospf6_top.c
index 2b65be8..df856b4 100644
--- a/ospf6d/ospf6_top.c
+++ b/ospf6d/ospf6_top.c
@@ -594,10 +594,10 @@
        IP6_STR
        OSPF6_STR
        ROUTE_STR
-       "Dispaly Intra-Area routes\n"
-       "Dispaly Inter-Area routes\n"
-       "Dispaly Type-1 External routes\n"
-       "Dispaly Type-2 External routes\n"
+       "Display Intra-Area routes\n"
+       "Display Inter-Area routes\n"
+       "Display Type-1 External routes\n"
+       "Display Type-2 External routes\n"
        )
 
 DEFUN (show_ipv6_ospf6_route_type_detail,
@@ -607,10 +607,10 @@
        IP6_STR
        OSPF6_STR
        ROUTE_STR
-       "Dispaly Intra-Area routes\n"
-       "Dispaly Inter-Area routes\n"
-       "Dispaly Type-1 External routes\n"
-       "Dispaly Type-2 External routes\n"
+       "Display Intra-Area routes\n"
+       "Display Inter-Area routes\n"
+       "Display Type-1 External routes\n"
+       "Display Type-2 External routes\n"
        "Detailed information\n"
        )
 {
diff --git a/ospf6d/ospf6_zebra.c b/ospf6d/ospf6_zebra.c
index 881771a..f09e9d2 100644
--- a/ospf6d/ospf6_zebra.c
+++ b/ospf6d/ospf6_zebra.c
@@ -451,6 +451,7 @@
   api.type = ZEBRA_ROUTE_OSPF6;
   api.flags = 0;
   api.message = 0;
+  api.safi = SAFI_UNICAST;
   SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
   api.nexthop_num = nhcount;
   api.nexthop = nexthops;
diff --git a/ospf6d/ospf6d.c b/ospf6d/ospf6d.c
index bb091d4..3fdbda1 100644
--- a/ospf6d/ospf6d.c
+++ b/ospf6d/ospf6d.c
@@ -1889,4 +1889,11 @@
   thread_add_read (master, ospf6_receive, NULL, ospf6_sock);
 }
 
-
+void
+ospf6_clean (void)
+{
+  if (ospf6->route_table)
+    ospf6_route_remove_all (ospf6->route_table);
+  if (ospf6->brouter_table)
+    ospf6_route_remove_all (ospf6->brouter_table);
+}
diff --git a/ospf6d/ospf6d.h b/ospf6d/ospf6d.h
index b6f1b73..2ac6300 100644
--- a/ospf6d/ospf6d.h
+++ b/ospf6d/ospf6d.h
@@ -49,6 +49,9 @@
 #endif /* IPV6_DROP_MEMBERSHIP */
 #endif /* ! IPV6_LEAVE_GROUP */
 
+#define MSG_OK    0
+#define MSG_NG    1
+
 /* cast macro: XXX - these *must* die, ick ick. */
 #define OSPF6_PROCESS(x) ((struct ospf6 *) (x))
 #define OSPF6_AREA(x) ((struct ospf6_area *) (x))
diff --git a/ospfclient/.cvsignore b/ospfclient/.cvsignore
deleted file mode 100644
index d6de29a..0000000
--- a/ospfclient/.cvsignore
+++ /dev/null
@@ -1,13 +0,0 @@
-Makefile
-Makefile.in
-*.o
-ospfclient
-tags
-TAGS
-.deps
-.nfs*
-*.lo
-*.la
-*.libs
-.arch-inventory
-.arch-ids
diff --git a/ospfd/.cvsignore b/ospfd/.cvsignore
deleted file mode 100644
index 6fce629..0000000
--- a/ospfd/.cvsignore
+++ /dev/null
@@ -1,14 +0,0 @@
-Makefile
-Makefile.in
-*.o
-ospfd
-ospfd.conf
-tags
-TAGS
-.deps
-.nfs*
-*.lo
-*.la
-*.libs
-.arch-inventory
-.arch-ids
diff --git a/ospfd/ospf_abr.c b/ospfd/ospf_abr.c
index 7a75194..b7cc20d 100644
--- a/ospfd/ospf_abr.c
+++ b/ospfd/ospf_abr.c
@@ -384,7 +384,7 @@
       if (best == NULL)
       	best = &lsa->data->id;
       else
-        if ( IPV4_ADDR_CMP (&best, &lsa->data->id) < 0)
+        if (IPV4_ADDR_CMP (&best->s_addr, &lsa->data->id.s_addr) < 0)
           best = &lsa->data->id;
     }
     
@@ -395,7 +395,7 @@
     if (best == NULL)
       return 1;
     
-    if ( IPV4_ADDR_CMP (&best, &area->ospf->router_id) < 0)
+    if (IPV4_ADDR_CMP (&best->s_addr, &area->ospf->router_id.s_addr) < 0)
       return 1;
     else
       return 0;
@@ -613,15 +613,6 @@
   memcpy(header->metric, mp, 3);
 }
 
-static int
-ospf_abr_check_nssa_range (struct prefix_ipv4 *p, u_int32_t cost,
-				   struct ospf_area *area)
-{
-  /* The Type-7 is tested against the aggregated prefix and forwarded
-       for lsa installation and flooding */
-  return 0;
-}
-
 /* ospf_abr_translate_nssa */
 static int
 ospf_abr_translate_nssa (struct ospf_area *area, struct ospf_lsa *lsa)
@@ -1577,42 +1568,6 @@
 }
 
 static void
-ospf_abr_announce_nssa_defaults (struct ospf *ospf) /* By ABR-Translator */
-{
-  struct listnode *node;
-  struct ospf_area *area;
-
-  if (! IS_OSPF_ABR (ospf))
-    return;
-
-  if (IS_DEBUG_OSPF_NSSA)
-    zlog_debug ("ospf_abr_announce_stub_defaults(): Start");
-
-  for (ALL_LIST_ELEMENTS_RO (ospf->areas, node, area))
-    {
-      if (IS_DEBUG_OSPF_NSSA)
-        zlog_debug ("ospf_abr_announce_nssa_defaults(): looking at area %s",
-                   inet_ntoa (area->area_id));
-
-      if (area->external_routing != OSPF_AREA_NSSA)
-        continue;
-
-      if (OSPF_IS_AREA_BACKBONE (area))
-        continue; /* Sanity Check */
-
-      /* if (!TranslatorRole continue V 1.0 look for "always" conf */
-      if (area->NSSATranslatorState)
-        {
-          if (IS_DEBUG_OSPF_NSSA)
-            zlog_debug ("ospf_abr_announce_nssa_defaults(): "
-                       "announcing 0.0.0.0/0 to this nssa");
-          /* ospf_abr_announce_nssa_asbr_to_as (&p, area->default_cost, area); */
-          /*ospf_abr_announce_network_to_area (&p, area->default_cost, area);*/
-        }
-    }
-}
-
-static void
 ospf_abr_announce_stub_defaults (struct ospf *ospf)
 {
   struct listnode *node;
diff --git a/ospfd/ospf_asbr.c b/ospfd/ospf_asbr.c
index c39efee..a23b4f2 100644
--- a/ospfd/ospf_asbr.c
+++ b/ospfd/ospf_asbr.c
@@ -284,6 +284,9 @@
 	      continue;
 	    ospf_external_lsa_flush (ospf, type, &ei->p,
 				     ei->ifindex /*, ei->nexthop */);
-	    ospf_external_info_delete (type, ei->p);
+
+	    ospf_external_info_free (ei);
+	    route_unlock_node (rn);
+	    rn->info = NULL;
 	  }
 }
diff --git a/ospfd/ospf_ase.c b/ospfd/ospf_ase.c
index 3c19931..6a72e31 100644
--- a/ospfd/ospf_ase.c
+++ b/ospfd/ospf_ase.c
@@ -135,7 +135,6 @@
 {
   struct listnode *node;
   struct ospf_path *op;
-  struct interface *ifp;
 
   for (ALL_LIST_ELEMENTS_RO (ro->paths, node, op))
     if (op->nexthop.s_addr == 0)
diff --git a/ospfd/ospf_dump.c b/ospfd/ospf_dump.c
index e65b2e3..7e11e25 100644
--- a/ospfd/ospf_dump.c
+++ b/ospfd/ospf_dump.c
@@ -115,6 +115,16 @@
 };
 const int ospf_network_type_msg_max = OSPF_IFTYPE_MAX;
 
+/* AuType */
+const struct message ospf_auth_type_str[] =
+{
+  { OSPF_AUTH_NULL,          "Null"          },
+  { OSPF_AUTH_SIMPLE,        "Simple"        },
+  { OSPF_AUTH_CRYPTOGRAPHIC, "Cryptographic" },
+};
+const size_t ospf_auth_type_str_max = sizeof (ospf_auth_type_str) /
+  sizeof (ospf_auth_type_str[0]);
+
 /* Configuration debug option variables. */
 unsigned long conf_debug_ospf_packet[5] = {0, 0, 0, 0, 0};
 unsigned long conf_debug_ospf_event = 0;
@@ -657,18 +667,19 @@
 ospf_header_dump (struct ospf_header *ospfh)
 {
   char buf[9];
+  u_int16_t auth_type = ntohs (ospfh->auth_type);
 
   zlog_debug ("Header");
   zlog_debug ("  Version %d", ospfh->version);
   zlog_debug ("  Type %d (%s)", ospfh->type,
-	     ospf_packet_type_str[ospfh->type]);
+	     LOOKUP (ospf_packet_type_str, ospfh->type));
   zlog_debug ("  Packet Len %d", ntohs (ospfh->length));
   zlog_debug ("  Router ID %s", inet_ntoa (ospfh->router_id));
   zlog_debug ("  Area ID %s", inet_ntoa (ospfh->area_id));
   zlog_debug ("  Checksum 0x%x", ntohs (ospfh->checksum));
-  zlog_debug ("  AuType %d", ntohs (ospfh->auth_type));
+  zlog_debug ("  AuType %s", LOOKUP (ospf_auth_type_str, auth_type));
 
-  switch (ntohs (ospfh->auth_type))
+  switch (auth_type)
     {
     case OSPF_AUTH_NULL:
       break;
@@ -1457,7 +1468,7 @@
     if (IS_DEBUG_OSPF_PACKET (i, SEND) && IS_DEBUG_OSPF_PACKET (i, RECV))
       {
 	vty_out (vty, "  OSPF packet %s%s debugging is on%s",
-		 ospf_packet_type_str[i + 1],
+		 LOOKUP (ospf_packet_type_str, i + 1),
 		 IS_DEBUG_OSPF_PACKET (i, DETAIL) ? " detail" : "",
 		 VTY_NEWLINE);
       }
@@ -1465,12 +1476,12 @@
       {
 	if (IS_DEBUG_OSPF_PACKET (i, SEND))
 	  vty_out (vty, "  OSPF packet %s send%s debugging is on%s",
-		   ospf_packet_type_str[i + 1],
+		   LOOKUP (ospf_packet_type_str, i + 1),
 		   IS_DEBUG_OSPF_PACKET (i, DETAIL) ? " detail" : "",
 		   VTY_NEWLINE);
 	if (IS_DEBUG_OSPF_PACKET (i, RECV))
 	  vty_out (vty, "  OSPF packet %s receive%s debugging is on%s",
-		   ospf_packet_type_str[i + 1],
+		   LOOKUP (ospf_packet_type_str, i + 1),
 		   IS_DEBUG_OSPF_PACKET (i, DETAIL) ? " detail" : "",
 		   VTY_NEWLINE);
       }
diff --git a/ospfd/ospf_dump.h b/ospfd/ospf_dump.h
index fb81371..a2d5e8b 100644
--- a/ospfd/ospf_dump.h
+++ b/ospfd/ospf_dump.h
@@ -121,8 +121,9 @@
 extern unsigned long term_debug_ospf_nssa;
 
 /* Message Strings. */
-extern const char *ospf_packet_type_str[];
 extern char *ospf_lsa_type_str[];
+extern const struct message ospf_auth_type_str[];
+extern const size_t ospf_auth_type_str_max;
 
 /* Prototypes. */
 extern const char *ospf_area_name_string (struct ospf_area *);
diff --git a/ospfd/ospf_flood.c b/ospfd/ospf_flood.c
index 77f2e16..2ebae89 100644
--- a/ospfd/ospf_flood.c
+++ b/ospfd/ospf_flood.c
@@ -319,7 +319,8 @@
      procedure cannot overwrite the newly installed LSA until
      MinLSArrival seconds have elapsed. */  
 
-  new = ospf_lsa_install (ospf, nbr->oi, new);
+  if (! (new = ospf_lsa_install (ospf, nbr->oi, new)))
+    return -1; /* unknown LSA type or any other error condition */
 
   /* Acknowledge the receipt of the LSA by sending a Link State
      Acknowledgment packet back out the receiving interface. */
diff --git a/ospfd/ospf_lsa.c b/ospfd/ospf_lsa.c
index 842df49..d5959eb 100644
--- a/ospfd/ospf_lsa.c
+++ b/ospfd/ospf_lsa.c
@@ -746,7 +746,7 @@
   return 0;
 }
 
-inline static void
+static void
 ospf_stub_router_check (struct ospf_area *area)
 {
   /* area must either be administratively configured to be stub
@@ -1637,7 +1637,7 @@
   if (ei == NULL)
     {
       if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
-	zlog_debug ("LSA[Type5]: External info is NULL, could not originated");
+	zlog_debug ("LSA[Type5]: External info is NULL, can't originate");
       return NULL;
     }
 
@@ -2838,9 +2838,9 @@
           OSPF_TIMER_ON (ospf->t_maxage, ospf_maxage_lsa_remover, 0);
           
         /* Remove LSA from the LSDB */
-        if (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF))
+        if (IS_LSA_SELF (lsa))
           if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
-            zlog_debug ("LSA[Type%d:%s]: LSA 0x%lx is self-oririnated: ",
+            zlog_debug ("LSA[Type%d:%s]: LSA 0x%lx is self-originated: ",
                        lsa->data->type, inet_ntoa (lsa->data->id), (u_long)lsa);
 
         if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
@@ -3389,7 +3389,7 @@
 
   /* This LSA is already checked. */
   if (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF_CHECKED))
-    return CHECK_FLAG (lsa->flags, OSPF_LSA_SELF);
+    return IS_LSA_SELF (lsa);
 
   /* Make sure LSA is self-checked. */
   SET_FLAG (lsa->flags, OSPF_LSA_SELF_CHECKED);
@@ -3414,11 +3414,11 @@
 	      {
 		/* to make it easier later */
 		SET_FLAG (lsa->flags, OSPF_LSA_SELF);
-		return CHECK_FLAG (lsa->flags, OSPF_LSA_SELF);
+		return IS_LSA_SELF (lsa);
 	      }
       }
 
-  return CHECK_FLAG (lsa->flags, OSPF_LSA_SELF);
+  return IS_LSA_SELF (lsa);
 }
 
 /* Get unique Link State ID. */
@@ -3541,6 +3541,7 @@
   struct external_info *ei;
   struct ospf_lsa *new = NULL;
   assert (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF));
+  assert (IS_LSA_SELF (lsa));
   assert (lsa->lock > 0);
 
   switch (lsa->data->type)
@@ -3589,7 +3590,7 @@
   u_int16_t index, current_index;
   
   assert (lsa->lock > 0);
-  assert (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF));
+  assert (IS_LSA_SELF (lsa));
 
   if (lsa->refresh_list < 0)
     {
@@ -3632,7 +3633,7 @@
 ospf_refresher_unregister_lsa (struct ospf *ospf, struct ospf_lsa *lsa)
 {
   assert (lsa->lock > 0);
-  assert (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF));
+  assert (IS_LSA_SELF (lsa));
   if (lsa->refresh_list >= 0)
     {
       struct list *refresh_list = ospf->lsa_refresh_queue.qs[lsa->refresh_list];
diff --git a/ospfd/ospf_lsa.h b/ospfd/ospf_lsa.h
index 72e2f8a..297cd98 100644
--- a/ospfd/ospf_lsa.h
+++ b/ospfd/ospf_lsa.h
@@ -49,6 +49,7 @@
 
 #define OSPF_LSA_HEADER_SIZE	     20U
 #define OSPF_ROUTER_LSA_LINK_SIZE    12U
+#define OSPF_ROUTER_LSA_TOS_SIZE      4U
 #define OSPF_MAX_LSA_SIZE	   1500U
 
 /* AS-external-LSA refresh method. */
@@ -152,6 +153,7 @@
 };
 
 /* OSPF Router-LSAs structure. */
+#define OSPF_ROUTER_LSA_MIN_SIZE                  16U /* w/1 link descriptor */
 struct router_lsa
 {
   struct lsa_header header;
@@ -169,6 +171,7 @@
 };
 
 /* OSPF Network-LSAs structure. */
+#define OSPF_NETWORK_LSA_MIN_SIZE                  8U /* w/1 router-ID */
 struct network_lsa
 {
   struct lsa_header header;
@@ -177,6 +180,7 @@
 };
 
 /* OSPF Summary-LSAs structure. */
+#define OSPF_SUMMARY_LSA_MIN_SIZE                  8U /* w/1 TOS metric block */
 struct summary_lsa
 {
   struct lsa_header header;
@@ -186,6 +190,7 @@
 };
 
 /* OSPF AS-external-LSAs structure. */
+#define OSPF_AS_EXTERNAL_LSA_MIN_SIZE             16U /* w/1 TOS forwarding block */
 struct as_external_lsa
 {
   struct lsa_header header;
@@ -274,6 +279,7 @@
 
 extern struct ospf_lsa *ospf_external_lsa_originate (struct ospf *, struct external_info *);
 extern int ospf_external_lsa_originate_timer (struct thread *);
+extern int ospf_default_originate_timer (struct thread *);
 extern struct ospf_lsa *ospf_lsa_lookup (struct ospf_area *, u_int32_t,
 				  struct in_addr, struct in_addr);
 extern struct ospf_lsa *ospf_lsa_lookup_by_id (struct ospf_area *,
diff --git a/ospfd/ospf_main.c b/ospfd/ospf_main.c
index 8b9a345..bad674b 100644
--- a/ospfd/ospf_main.c
+++ b/ospfd/ospf_main.c
@@ -38,6 +38,7 @@
 #include "memory.h"
 #include "privs.h"
 #include "sigevent.h"
+#include "zclient.h"
 
 #include "ospfd/ospfd.h"
 #include "ospfd/ospf_interface.h"
@@ -80,6 +81,7 @@
   { "daemon",      no_argument,       NULL, 'd'},
   { "config_file", required_argument, NULL, 'f'},
   { "pid_file",    required_argument, NULL, 'i'},
+  { "socket",      required_argument, NULL, 'z'},
   { "dryrun",      no_argument,       NULL, 'C'},
   { "help",        no_argument,       NULL, 'h'},
   { "vty_addr",    required_argument, NULL, 'A'},
@@ -116,6 +118,7 @@
 -d, --daemon       Runs in daemon mode\n\
 -f, --config_file  Set configuration file name\n\
 -i, --pid_file     Set process identifier file name\n\
+-z, --socket       Set path of zebra socket\n\
 -A, --vty_addr     Set vty's bind address\n\
 -P, --vty_port     Set vty's port number\n\
 -u, --user         User to run as\n\
@@ -191,30 +194,11 @@
   /* get program name */
   progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
 
-  /* Invoked by a priviledged user? -- endo. */
-  if (geteuid () != 0)
-    {
-      errno = EPERM;
-      perror (progname);
-      exit (1);
-    }
-
-  zlog_default = openzlog (progname, ZLOG_OSPF,
-			   LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
-
-  /* OSPF master init. */
-  ospf_master_init ();
-
-#ifdef SUPPORT_OSPF_API
-  /* OSPF apiserver is disabled by default. */
-  ospf_apiserver_enable = 0;
-#endif /* SUPPORT_OSPF_API */
-
   while (1) 
     {
       int opt;
 
-      opt = getopt_long (argc, argv, "df:i:hA:P:u:g:avC", longopts, 0);
+      opt = getopt_long (argc, argv, "df:i:z:hA:P:u:g:avC", longopts, 0);
     
       if (opt == EOF)
 	break;
@@ -235,6 +219,9 @@
         case 'i':
           pid_file = optarg;
           break;
+	case 'z':
+	  zclient_serv_path_set (optarg);
+	  break;
 	case 'P':
           /* Deal with atoi() returning 0 on failure, and ospfd not
              listening on ospfd port... */
@@ -274,6 +261,25 @@
 	}
     }
 
+  /* Invoked by a priviledged user? -- endo. */
+  if (geteuid () != 0)
+    {
+      errno = EPERM;
+      perror (progname);
+      exit (1);
+    }
+
+  zlog_default = openzlog (progname, ZLOG_OSPF,
+			   LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
+
+  /* OSPF master init. */
+  ospf_master_init ();
+
+#ifdef SUPPORT_OSPF_API
+  /* OSPF apiserver is disabled by default. */
+  ospf_apiserver_enable = 0;
+#endif /* SUPPORT_OSPF_API */
+
   /* Initializations. */
   master = om->master;
 
diff --git a/ospfd/ospf_network.c b/ospfd/ospf_network.c
index 1e2d44e..3e326a8 100644
--- a/ospfd/ospf_network.c
+++ b/ospfd/ospf_network.c
@@ -52,8 +52,8 @@
 {
   int ret;
   
-  ret = setsockopt_multicast_ipv4 (top->fd, IP_ADD_MEMBERSHIP,
-                                   p->u.prefix4, htonl (OSPF_ALLSPFROUTERS),
+  ret = setsockopt_ipv4_multicast (top->fd, IP_ADD_MEMBERSHIP,
+                                   htonl (OSPF_ALLSPFROUTERS),
                                    ifindex);
   if (ret < 0)
     zlog_warn ("can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %s, "
@@ -73,8 +73,8 @@
 {
   int ret;
 
-  ret = setsockopt_multicast_ipv4 (top->fd, IP_DROP_MEMBERSHIP,
-                                   p->u.prefix4, htonl (OSPF_ALLSPFROUTERS),
+  ret = setsockopt_ipv4_multicast (top->fd, IP_DROP_MEMBERSHIP,
+                                   htonl (OSPF_ALLSPFROUTERS),
                                    ifindex);
   if (ret < 0)
     zlog_warn ("can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, "
@@ -94,8 +94,8 @@
 {
   int ret;
 
-  ret = setsockopt_multicast_ipv4 (top->fd, IP_ADD_MEMBERSHIP,
-                                   p->u.prefix4, htonl (OSPF_ALLDROUTERS),
+  ret = setsockopt_ipv4_multicast (top->fd, IP_ADD_MEMBERSHIP,
+                                   htonl (OSPF_ALLDROUTERS),
                                    ifindex);
   if (ret < 0)
     zlog_warn ("can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %s, "
@@ -115,8 +115,8 @@
 {
   int ret;
 
-  ret = setsockopt_multicast_ipv4 (top->fd, IP_DROP_MEMBERSHIP,
-                                   p->u.prefix4, htonl (OSPF_ALLDROUTERS),
+  ret = setsockopt_ipv4_multicast (top->fd, IP_DROP_MEMBERSHIP,
+                                   htonl (OSPF_ALLDROUTERS),
                                    ifindex);
   if (ret < 0)
     zlog_warn ("can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, "
@@ -151,8 +151,7 @@
     zlog_warn ("can't setsockopt IP_MULTICAST_TTL(1) for fd %d: %s",
 	       top->fd, safe_strerror (errno));
 
-  ret = setsockopt_multicast_ipv4 (top->fd, IP_MULTICAST_IF,
-                                   p->u.prefix4, 0, ifindex);
+  ret = setsockopt_ipv4_multicast_if (top->fd, ifindex);
   if (ret < 0)
     zlog_warn("can't setsockopt IP_MULTICAST_IF(fd %d, addr %s, "
 	      "ifindex %u): %s",
diff --git a/ospfd/ospf_packet.c b/ospfd/ospf_packet.c
index be137d9..351fb21 100644
--- a/ospfd/ospf_packet.c
+++ b/ospfd/ospf_packet.c
@@ -50,15 +50,49 @@
 #include "ospfd/ospf_dump.h"
 
 /* Packet Type String. */
-const char *ospf_packet_type_str[] =
+const struct message ospf_packet_type_str[] =
 {
-  "unknown",
-  "Hello",
-  "Database Description",
-  "Link State Request",
-  "Link State Update",
-  "Link State Acknowledgment",
+  { OSPF_MSG_HELLO,   "Hello"                     },
+  { OSPF_MSG_DB_DESC, "Database Description"      },
+  { OSPF_MSG_LS_REQ,  "Link State Request"        },
+  { OSPF_MSG_LS_UPD,  "Link State Update"         },
+  { OSPF_MSG_LS_ACK,  "Link State Acknowledgment" },
 };
+const size_t ospf_packet_type_str_max = sizeof (ospf_packet_type_str) /
+  sizeof (ospf_packet_type_str[0]);
+
+/* Minimum (besides OSPF_HEADER_SIZE) lengths for OSPF packets of
+   particular types, offset is the "type" field of a packet. */
+static const u_int16_t ospf_packet_minlen[] =
+{
+  0,
+  OSPF_HELLO_MIN_SIZE,
+  OSPF_DB_DESC_MIN_SIZE,
+  OSPF_LS_REQ_MIN_SIZE,
+  OSPF_LS_UPD_MIN_SIZE,
+  OSPF_LS_ACK_MIN_SIZE,
+};
+
+/* Minimum (besides OSPF_LSA_HEADER_SIZE) lengths for LSAs of particular
+   types, offset is the "LSA type" field. */
+static const u_int16_t ospf_lsa_minlen[] =
+{
+  0,
+  OSPF_ROUTER_LSA_MIN_SIZE,
+  OSPF_NETWORK_LSA_MIN_SIZE,
+  OSPF_SUMMARY_LSA_MIN_SIZE,
+  OSPF_SUMMARY_LSA_MIN_SIZE,
+  OSPF_AS_EXTERNAL_LSA_MIN_SIZE,
+  0,
+  OSPF_AS_EXTERNAL_LSA_MIN_SIZE,
+  0,
+  0,
+  0,
+  0,
+};
+
+/* for ospf_check_auth() */
+static int ospf_check_sum (struct ospf_header *);
 
 /* OSPF authentication checking function */
 static int
@@ -201,7 +235,7 @@
 	       "destination %s) called with NULL obuf, ignoring "
 	       "(please report this bug)!\n",
 	       IF_NAME(oi), oi->state, LOOKUP (ospf_ism_state_msg, oi->state),
-	       ospf_packet_type_str[stream_getc_from(op->s, 1)],
+	       LOOKUP (ospf_packet_type_str, stream_getc_from(op->s, 1)),
 	       inet_ntoa (op->dst));
       return;
     }
@@ -222,7 +256,7 @@
 	       "destination %s) called with NULL obuf, ignoring "
 	       "(please report this bug)!\n",
 	       IF_NAME(oi), oi->state, LOOKUP (ospf_ism_state_msg, oi->state),
-	       ospf_packet_type_str[stream_getc_from(op->s, 1)],
+	       LOOKUP (ospf_packet_type_str, stream_getc_from(op->s, 1)),
 	       inet_ntoa (op->dst));
       return;
     }
@@ -266,7 +300,7 @@
 }
 
 /* XXX inline */
-static inline unsigned int
+static unsigned int
 ospf_packet_authspace (struct ospf_interface *oi)
 {
   int auth = 0;
@@ -291,24 +325,14 @@
 
 
 static int
-ospf_check_md5_digest (struct ospf_interface *oi, struct stream *s,
-                       u_int16_t length)
+ospf_check_md5_digest (struct ospf_interface *oi, struct ospf_header *ospfh)
 {
-  unsigned char *ibuf;
   MD5_CTX ctx;
   unsigned char digest[OSPF_AUTH_MD5_SIZE];
-  unsigned char *pdigest;
   struct crypt_key *ck;
-  struct ospf_header *ospfh;
   struct ospf_neighbor *nbr;
+  u_int16_t length = ntohs (ospfh->length);
   
-
-  ibuf = STREAM_PNT (s);
-  ospfh = (struct ospf_header *) ibuf;
-
-  /* Get pointer to the end of the packet. */
-  pdigest = ibuf + length;
-
   /* Get secret key. */
   ck = ospf_crypt_key_lookup (OSPF_IF_PARAM (oi, auth_crypt),
 			      ospfh->u.crypt.key_id);
@@ -334,12 +358,12 @@
   /* Generate a digest for the ospf packet - their digest + our digest. */
   memset(&ctx, 0, sizeof(ctx));
   MD5Init(&ctx);
-  MD5Update(&ctx, ibuf, length);
+  MD5Update(&ctx, ospfh, length);
   MD5Update(&ctx, ck->auth_key, OSPF_AUTH_MD5_SIZE);
   MD5Final(digest, &ctx);
 
   /* compare the two */
-  if (memcmp (pdigest, digest, OSPF_AUTH_MD5_SIZE))
+  if (memcmp ((caddr_t)ospfh + length, digest, OSPF_AUTH_MD5_SIZE))
     {
       zlog_warn ("interface %s: ospf_check_md5 checksum mismatch",
 		 IF_NAME (oi));
@@ -755,7 +779,7 @@
 	}
 
       zlog_debug ("%s sent to [%s] via [%s].",
-		 ospf_packet_type_str[type], inet_ntoa (op->dst),
+		 LOOKUP (ospf_packet_type_str, type), inet_ntoa (op->dst),
 		 IF_NAME (oi));
 
       if (IS_DEBUG_OSPF_PACKET (type - 1, DETAIL))
@@ -801,7 +825,7 @@
         {
           zlog_debug ("ospf_header[%s/%s]: selforiginated, "
                      "dropping.",
-                     ospf_packet_type_str[ospfh->type],
+                     LOOKUP (ospf_packet_type_str, ospfh->type),
                      inet_ntoa (iph->ip_src));
         }
       return;
@@ -1568,8 +1592,13 @@
       sum = lsah->checksum;
       if (sum != ospf_lsa_checksum (lsah))
 	{
-	  zlog_warn ("Link State Update: LSA checksum error %x, %x.",
-		     sum, lsah->checksum);
+	  /* (bug #685) more details in a one-line message make it possible
+	   * to identify problem source on the one hand and to have a better
+	   * chance to compress repeated messages in syslog on the other */
+	  zlog_warn ("Link State Update: LSA checksum error %x/%x, ID=%s from: nbr %s, router ID %s, adv router %s",
+		     sum, lsah->checksum, inet_ntoa (lsah->id),
+		     inet_ntoa (nbr->src), inet_ntoa (nbr->router_id),
+		     inet_ntoa (lsah->adv_router));
 	  continue;
 	}
 
@@ -2116,7 +2145,7 @@
   
   ip_len = iph->ip_len;
   
-#if !defined(GNU_LINUX) && (OpenBSD < 200311)
+#if !defined(GNU_LINUX) && (OpenBSD < 200311) && (__FreeBSD_version < 1000000)
   /*
    * Kernel network code touches incoming IP header parameters,
    * before protocol specific processing.
@@ -2208,7 +2237,7 @@
   return NULL;
 }
 
-static inline int
+static int
 ospf_check_area_id (struct ospf_interface *oi, struct ospf_header *ospfh)
 {
   /* Check match the Area ID of the receiving interface. */
@@ -2241,45 +2270,91 @@
  return 0;
 }
 
+/* Return 1, if the packet is properly authenticated and checksummed,
+   0 otherwise. In particular, check that AuType header field is valid and
+   matches the locally configured AuType, and that D.5 requirements are met. */
 static int
-ospf_check_auth (struct ospf_interface *oi, struct stream *ibuf,
-		 struct ospf_header *ospfh)
+ospf_check_auth (struct ospf_interface *oi, struct ospf_header *ospfh)
 {
-  int ret = 0;
   struct crypt_key *ck;
+  u_int16_t iface_auth_type;
+  u_int16_t pkt_auth_type = ntohs (ospfh->auth_type);
 
-  switch (ntohs (ospfh->auth_type))
+  switch (pkt_auth_type)
+  {
+  case OSPF_AUTH_NULL: /* RFC2328 D.5.1 */
+    if (OSPF_AUTH_NULL != (iface_auth_type = ospf_auth_type (oi)))
     {
-    case OSPF_AUTH_NULL:
-      ret = 1;
-      break;
-    case OSPF_AUTH_SIMPLE:
-      if (!memcmp (OSPF_IF_PARAM (oi, auth_simple), ospfh->u.auth_data, OSPF_AUTH_SIMPLE_SIZE))
-	ret = 1;
-      else
-	ret = 0;
-      break;
-    case OSPF_AUTH_CRYPTOGRAPHIC:
-      if ((ck = listgetdata (listtail(OSPF_IF_PARAM (oi,auth_crypt)))) == NULL)
-	{
-	  ret = 0;
-	  break;
-	}
-      
-      /* This is very basic, the digest processing is elsewhere */
-      if (ospfh->u.crypt.auth_data_len == OSPF_AUTH_MD5_SIZE && 
-          ospfh->u.crypt.key_id == ck->key_id &&
-          ntohs (ospfh->length) + OSPF_AUTH_SIMPLE_SIZE <= stream_get_size (ibuf))
-        ret = 1;
-      else
-        ret = 0;
-      break;
-    default:
-      ret = 0;
-      break;
+      if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
+        zlog_warn ("interface %s: auth-type mismatch, local %s, rcvd Null",
+                   IF_NAME (oi), LOOKUP (ospf_auth_type_str, iface_auth_type));
+      return 0;
     }
-
-  return ret;
+    if (! ospf_check_sum (ospfh))
+    {
+      if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
+        zlog_warn ("interface %s: Null auth OK, but checksum error, Router-ID %s",
+                   IF_NAME (oi), inet_ntoa (ospfh->router_id));
+      return 0;
+    }
+    return 1;
+  case OSPF_AUTH_SIMPLE: /* RFC2328 D.5.2 */
+    if (OSPF_AUTH_SIMPLE != (iface_auth_type = ospf_auth_type (oi)))
+    {
+      if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
+        zlog_warn ("interface %s: auth-type mismatch, local %s, rcvd Simple",
+                   IF_NAME (oi), LOOKUP (ospf_auth_type_str, iface_auth_type));
+      return 0;
+    }
+    if (memcmp (OSPF_IF_PARAM (oi, auth_simple), ospfh->u.auth_data, OSPF_AUTH_SIMPLE_SIZE))
+    {
+      if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
+        zlog_warn ("interface %s: Simple auth failed", IF_NAME (oi));
+      return 0;
+    }
+    if (! ospf_check_sum (ospfh))
+    {
+      if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
+        zlog_warn ("interface %s: Simple auth OK, checksum error, Router-ID %s",
+                   IF_NAME (oi), inet_ntoa (ospfh->router_id));
+      return 0;
+    }
+    return 1;
+  case OSPF_AUTH_CRYPTOGRAPHIC: /* RFC2328 D.5.3 */
+    if (OSPF_AUTH_CRYPTOGRAPHIC != (iface_auth_type = ospf_auth_type (oi)))
+    {
+      if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
+        zlog_warn ("interface %s: auth-type mismatch, local %s, rcvd Cryptographic",
+                   IF_NAME (oi), LOOKUP (ospf_auth_type_str, iface_auth_type));
+      return 0;
+    }
+    if (ospfh->checksum)
+    {
+      if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
+        zlog_warn ("interface %s: OSPF header checksum is not 0", IF_NAME (oi));
+      return 0;
+    }
+    /* only MD5 crypto method can pass ospf_packet_examin() */
+    if
+    (
+      NULL == (ck = listgetdata (listtail(OSPF_IF_PARAM (oi,auth_crypt)))) ||
+      ospfh->u.crypt.key_id != ck->key_id ||
+      /* Condition above uses the last key ID on the list, which is
+         different from what ospf_crypt_key_lookup() does. A bug? */
+      ! ospf_check_md5_digest (oi, ospfh)
+    )
+    {
+      if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
+        zlog_warn ("interface %s: MD5 auth failed", IF_NAME (oi));
+      return 0;
+    }
+    return 1;
+  default:
+    if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
+      zlog_warn ("interface %s: invalid packet auth-type (%02x)",
+                 IF_NAME (oi), pkt_auth_type);
+    return 0;
+  }
 }
 
 static int
@@ -2308,19 +2383,311 @@
   return 1;
 }
 
+/* Verify, that given link/TOS records are properly sized/aligned and match
+   Router-LSA "# links" and "# TOS" fields as specified in RFC2328 A.4.2. */
+static unsigned
+ospf_router_lsa_links_examin
+(
+  struct router_lsa_link * link,
+  u_int16_t linkbytes,
+  const u_int16_t num_links
+)
+{
+  unsigned counted_links = 0, thislinklen;
+
+  while (linkbytes)
+  {
+    thislinklen = OSPF_ROUTER_LSA_LINK_SIZE + 4 * link->m[0].tos_count;
+    if (thislinklen > linkbytes)
+    {
+      if (IS_DEBUG_OSPF_PACKET (0, RECV))
+        zlog_debug ("%s: length error in link block #%u", __func__, counted_links);
+      return MSG_NG;
+    }
+    link = (struct router_lsa_link *)((caddr_t) link + thislinklen);
+    linkbytes -= thislinklen;
+    counted_links++;
+  }
+  if (counted_links != num_links)
+  {
+    if (IS_DEBUG_OSPF_PACKET (0, RECV))
+      zlog_debug ("%s: %u link blocks declared, %u present",
+                  __func__, num_links, counted_links);
+    return MSG_NG;
+  }
+  return MSG_OK;
+}
+
+/* Verify, that the given LSA is properly sized/aligned (including type-specific
+   minimum length constraint). */
+static unsigned
+ospf_lsa_examin (struct lsa_header * lsah, const u_int16_t lsalen, const u_char headeronly)
+{
+  unsigned ret;
+  struct router_lsa * rlsa;
+  if
+  (
+    lsah->type < OSPF_MAX_LSA &&
+    ospf_lsa_minlen[lsah->type] &&
+    lsalen < OSPF_LSA_HEADER_SIZE + ospf_lsa_minlen[lsah->type]
+  )
+  {
+    if (IS_DEBUG_OSPF_PACKET (0, RECV))
+      zlog_debug ("%s: undersized (%u B) %s",
+                  __func__, lsalen, LOOKUP (ospf_lsa_type_msg, lsah->type));
+    return MSG_NG;
+  }
+  switch (lsah->type)
+  {
+  case OSPF_ROUTER_LSA:
+    /* RFC2328 A.4.2, LSA header + 4 bytes followed by N>=1 (12+)-byte link blocks */
+    if (headeronly)
+    {
+      ret = (lsalen - OSPF_LSA_HEADER_SIZE - OSPF_ROUTER_LSA_MIN_SIZE) % 4 ? MSG_NG : MSG_OK;
+      break;
+    }
+    rlsa = (struct router_lsa *) lsah;
+    ret = ospf_router_lsa_links_examin
+    (
+      (struct router_lsa_link *) rlsa->link,
+      lsalen - OSPF_LSA_HEADER_SIZE - 4, /* skip: basic header, "flags", 0, "# links" */
+      ntohs (rlsa->links) /* 16 bits */
+    );
+    break;
+  case OSPF_AS_EXTERNAL_LSA:
+    /* RFC2328 A.4.5, LSA header + 4 bytes followed by N>=1 12-bytes long blocks */
+  case OSPF_AS_NSSA_LSA:
+    /* RFC3101 C, idem */
+    ret = (lsalen - OSPF_LSA_HEADER_SIZE - OSPF_AS_EXTERNAL_LSA_MIN_SIZE) % 12 ? MSG_NG : MSG_OK;
+    break;
+  /* Following LSA types are considered OK length-wise as soon as their minimum
+   * length constraint is met and length of the whole LSA is a multiple of 4
+   * (basic LSA header size is already a multiple of 4). */
+  case OSPF_NETWORK_LSA:
+    /* RFC2328 A.4.3, LSA header + 4 bytes followed by N>=1 router-IDs */
+  case OSPF_SUMMARY_LSA:
+  case OSPF_ASBR_SUMMARY_LSA:
+    /* RFC2328 A.4.4, LSA header + 4 bytes followed by N>=1 4-bytes TOS blocks */
+#ifdef HAVE_OPAQUE_LSA
+  case OSPF_OPAQUE_LINK_LSA:
+  case OSPF_OPAQUE_AREA_LSA:
+  case OSPF_OPAQUE_AS_LSA:
+    /* RFC5250 A.2, "some number of octets (of application-specific
+     * data) padded to 32-bit alignment." This is considered equivalent
+     * to 4-byte alignment of all other LSA types, see OSPF-ALIGNMENT.txt
+     * file for the detailed analysis of this passage. */
+#endif
+    ret = lsalen % 4 ? MSG_NG : MSG_OK;
+    break;
+  default:
+    if (IS_DEBUG_OSPF_PACKET (0, RECV))
+      zlog_debug ("%s: unsupported LSA type 0x%02x", __func__, lsah->type);
+    return MSG_NG;
+  }
+  if (ret != MSG_OK && IS_DEBUG_OSPF_PACKET (0, RECV))
+    zlog_debug ("%s: alignment error in %s",
+                __func__, LOOKUP (ospf_lsa_type_msg, lsah->type));
+  return ret;
+}
+
+/* Verify if the provided input buffer is a valid sequence of LSAs. This
+   includes verification of LSA blocks length/alignment and dispatching
+   of deeper-level checks. */
+static unsigned
+ospf_lsaseq_examin
+(
+  struct lsa_header *lsah, /* start of buffered data */
+  size_t length,
+  const u_char headeronly,
+  /* When declared_num_lsas is not 0, compare it to the real number of LSAs
+     and treat the difference as an error. */
+  const u_int32_t declared_num_lsas
+)
+{
+  u_int32_t counted_lsas = 0;
+
+  while (length)
+  {
+    u_int16_t lsalen;
+    if (length < OSPF_LSA_HEADER_SIZE)
+    {
+      if (IS_DEBUG_OSPF_PACKET (0, RECV))
+        zlog_debug ("%s: undersized (%zu B) trailing (#%u) LSA header",
+                    __func__, length, counted_lsas);
+      return MSG_NG;
+    }
+    /* save on ntohs() calls here and in the LSA validator */
+    lsalen = ntohs (lsah->length);
+    if (lsalen < OSPF_LSA_HEADER_SIZE)
+    {
+      if (IS_DEBUG_OSPF_PACKET (0, RECV))
+        zlog_debug ("%s: malformed LSA header #%u, declared length is %u B",
+                    __func__, counted_lsas, lsalen);
+      return MSG_NG;
+    }
+    if (headeronly)
+    {
+      /* less checks here and in ospf_lsa_examin() */
+      if (MSG_OK != ospf_lsa_examin (lsah, lsalen, 1))
+      {
+        if (IS_DEBUG_OSPF_PACKET (0, RECV))
+          zlog_debug ("%s: malformed header-only LSA #%u", __func__, counted_lsas);
+        return MSG_NG;
+      }
+      lsah = (struct lsa_header *) ((caddr_t) lsah + OSPF_LSA_HEADER_SIZE);
+      length -= OSPF_LSA_HEADER_SIZE;
+    }
+    else
+    {
+      /* make sure the input buffer is deep enough before further checks */
+      if (lsalen > length)
+      {
+        if (IS_DEBUG_OSPF_PACKET (0, RECV))
+          zlog_debug ("%s: anomaly in LSA #%u: declared length is %u B, buffered length is %zu B",
+                      __func__, counted_lsas, lsalen, length);
+        return MSG_NG;
+      }
+      if (MSG_OK != ospf_lsa_examin (lsah, lsalen, 0))
+      {
+        if (IS_DEBUG_OSPF_PACKET (0, RECV))
+          zlog_debug ("%s: malformed LSA #%u", __func__, counted_lsas);
+        return MSG_NG;
+      }
+      lsah = (struct lsa_header *) ((caddr_t) lsah + lsalen);
+      length -= lsalen;
+    }
+    counted_lsas++;
+  }
+
+  if (declared_num_lsas && counted_lsas != declared_num_lsas)
+  {
+    if (IS_DEBUG_OSPF_PACKET (0, RECV))
+      zlog_debug ("%s: #LSAs declared (%u) does not match actual (%u)",
+                  __func__, declared_num_lsas, counted_lsas);
+    return MSG_NG;
+  }
+  return MSG_OK;
+}
+
+/* Verify a complete OSPF packet for proper sizing/alignment. */
+static unsigned
+ospf_packet_examin (struct ospf_header * oh, const unsigned bytesonwire)
+{
+  u_int16_t bytesdeclared, bytesauth;
+  unsigned ret;
+  struct ospf_ls_update * lsupd;
+
+  /* Length, 1st approximation. */
+  if (bytesonwire < OSPF_HEADER_SIZE)
+  {
+    if (IS_DEBUG_OSPF_PACKET (0, RECV))
+      zlog_debug ("%s: undersized (%u B) packet", __func__, bytesonwire);
+    return MSG_NG;
+  }
+  /* Now it is safe to access header fields. Performing length check, allow
+   * for possible extra bytes of crypto auth/padding, which are not counted
+   * in the OSPF header "length" field. */
+  if (oh->version != OSPF_VERSION)
+  {
+    if (IS_DEBUG_OSPF_PACKET (0, RECV))
+      zlog_debug ("%s: invalid (%u) protocol version", __func__, oh->version);
+    return MSG_NG;
+  }
+  bytesdeclared = ntohs (oh->length);
+  if (ntohs (oh->auth_type) != OSPF_AUTH_CRYPTOGRAPHIC)
+    bytesauth = 0;
+  else
+  {
+    if (oh->u.crypt.auth_data_len != OSPF_AUTH_MD5_SIZE)
+    {
+      if (IS_DEBUG_OSPF_PACKET (0, RECV))
+        zlog_debug ("%s: unsupported crypto auth length (%u B)",
+                    __func__, oh->u.crypt.auth_data_len);
+      return MSG_NG;
+    }
+    bytesauth = OSPF_AUTH_MD5_SIZE;
+  }
+  if (bytesdeclared + bytesauth > bytesonwire)
+  {
+    if (IS_DEBUG_OSPF_PACKET (0, RECV))
+      zlog_debug ("%s: packet length error (%u real, %u+%u declared)",
+                  __func__, bytesonwire, bytesdeclared, bytesauth);
+    return MSG_NG;
+  }
+  /* Length, 2nd approximation. The type-specific constraint is checked
+     against declared length, not amount of bytes on wire. */
+  if
+  (
+    oh->type >= OSPF_MSG_HELLO &&
+    oh->type <= OSPF_MSG_LS_ACK &&
+    bytesdeclared < OSPF_HEADER_SIZE + ospf_packet_minlen[oh->type]
+  )
+  {
+    if (IS_DEBUG_OSPF_PACKET (0, RECV))
+      zlog_debug ("%s: undersized (%u B) %s packet", __func__,
+                  bytesdeclared, LOOKUP (ospf_packet_type_str, oh->type));
+    return MSG_NG;
+  }
+  switch (oh->type)
+  {
+  case OSPF_MSG_HELLO:
+    /* RFC2328 A.3.2, packet header + OSPF_HELLO_MIN_SIZE bytes followed
+       by N>=0 router-IDs. */
+    ret = (bytesdeclared - OSPF_HEADER_SIZE - OSPF_HELLO_MIN_SIZE) % 4 ? MSG_NG : MSG_OK;
+    break;
+  case OSPF_MSG_DB_DESC:
+    /* RFC2328 A.3.3, packet header + OSPF_DB_DESC_MIN_SIZE bytes followed
+       by N>=0 header-only LSAs. */
+    ret = ospf_lsaseq_examin
+    (
+      (struct lsa_header *) ((caddr_t) oh + OSPF_HEADER_SIZE + OSPF_DB_DESC_MIN_SIZE),
+      bytesdeclared - OSPF_HEADER_SIZE - OSPF_DB_DESC_MIN_SIZE,
+      1, /* header-only LSAs */
+      0
+    );
+    break;
+  case OSPF_MSG_LS_REQ:
+    /* RFC2328 A.3.4, packet header followed by N>=0 12-bytes request blocks. */
+    ret = (bytesdeclared - OSPF_HEADER_SIZE - OSPF_LS_REQ_MIN_SIZE) %
+      OSPF_LSA_KEY_SIZE ? MSG_NG : MSG_OK;
+    break;
+  case OSPF_MSG_LS_UPD:
+    /* RFC2328 A.3.5, packet header + OSPF_LS_UPD_MIN_SIZE bytes followed
+       by N>=0 full LSAs (with N declared beforehand). */
+    lsupd = (struct ospf_ls_update *) ((caddr_t) oh + OSPF_HEADER_SIZE);
+    ret = ospf_lsaseq_examin
+    (
+      (struct lsa_header *) ((caddr_t) lsupd + OSPF_LS_UPD_MIN_SIZE),
+      bytesdeclared - OSPF_HEADER_SIZE - OSPF_LS_UPD_MIN_SIZE,
+      0, /* full LSAs */
+      ntohl (lsupd->num_lsas) /* 32 bits */
+    );
+    break;
+  case OSPF_MSG_LS_ACK:
+    /* RFC2328 A.3.6, packet header followed by N>=0 header-only LSAs. */
+    ret = ospf_lsaseq_examin
+    (
+      (struct lsa_header *) ((caddr_t) oh + OSPF_HEADER_SIZE + OSPF_LS_ACK_MIN_SIZE),
+      bytesdeclared - OSPF_HEADER_SIZE - OSPF_LS_ACK_MIN_SIZE,
+      1, /* header-only LSAs */
+      0
+    );
+    break;
+  default:
+    if (IS_DEBUG_OSPF_PACKET (0, RECV))
+      zlog_debug ("%s: invalid packet type 0x%02x", __func__, oh->type);
+    return MSG_NG;
+  }
+  if (ret != MSG_OK && IS_DEBUG_OSPF_PACKET (0, RECV))
+    zlog_debug ("%s: malformed %s packet", __func__, LOOKUP (ospf_packet_type_str, oh->type));
+  return ret;
+}
+
 /* OSPF Header verification. */
 static int
 ospf_verify_header (struct stream *ibuf, struct ospf_interface *oi,
 		    struct ip *iph, struct ospf_header *ospfh)
 {
-  /* check version. */
-  if (ospfh->version != OSPF_VERSION)
-    {
-      zlog_warn ("interface %s: ospf_read version number mismatch.",
-		 IF_NAME (oi));
-      return -1;
-    }
-
   /* Check Area ID. */
   if (!ospf_check_area_id (oi, ospfh))
     {
@@ -2337,42 +2704,9 @@
       return -1;
     }
 
-  /* Check authentication. */
-  if (ospf_auth_type (oi) != ntohs (ospfh->auth_type))
-    {
-      zlog_warn ("interface %s: auth-type mismatch, local %d, rcvd %d",
-		 IF_NAME (oi), ospf_auth_type (oi), ntohs (ospfh->auth_type));
-      return -1;
-    }
-
-  if (! ospf_check_auth (oi, ibuf, ospfh))
-    {
-      zlog_warn ("interface %s: ospf_read authentication failed.",
-		 IF_NAME (oi));
-      return -1;
-    }
-
-  /* if check sum is invalid, packet is discarded. */
-  if (ntohs (ospfh->auth_type) != OSPF_AUTH_CRYPTOGRAPHIC)
-    {
-      if (! ospf_check_sum (ospfh))
-	{
-	  zlog_warn ("interface %s: ospf_read packet checksum error %s",
-		     IF_NAME (oi), inet_ntoa (ospfh->router_id));
-	  return -1;
-	}
-    }
-  else
-    {
-      if (ospfh->checksum != 0)
-	return -1;
-      if (ospf_check_md5_digest (oi, ibuf, ntohs (ospfh->length)) == 0)
-	{
-	  zlog_warn ("interface %s: ospf_read md5 authentication failed.",
-		     IF_NAME (oi));
-	  return -1;
-	}
-    }
+  /* Check authentication. The function handles logging actions, where required. */
+  if (! ospf_check_auth (oi, ospfh))
+    return -1;
 
   return 0;
 }
@@ -2396,10 +2730,10 @@
   /* prepare for next packet. */
   ospf->t_read = thread_add_read (master, ospf_read, ospf, ospf->fd);
 
-  /* read OSPF packet. */
   stream_reset(ospf->ibuf);
   if (!(ibuf = ospf_recv_packet (ospf->fd, &ifp, ospf->ibuf)))
     return -1;
+  /* This raw packet is known to be at least as big as its IP header. */
   
   /* Note that there should not be alignment problems with this assignment
      because this is at the beginning of the stream data buffer. */
@@ -2430,15 +2764,23 @@
       return 0;
     }
 
-  /* Adjust size to message length. */
+  /* Advance from IP header to OSPF header (iph->ip_hl has been verified
+     by ospf_recv_packet() to be correct). */
   stream_forward_getp (ibuf, iph->ip_hl * 4);
-  
-  /* Get ospf packet header. */
+
   ospfh = (struct ospf_header *) STREAM_PNT (ibuf);
+  if (MSG_OK != ospf_packet_examin (ospfh, stream_get_endp (ibuf) - stream_get_getp (ibuf)))
+    return -1;
+  /* Now it is safe to access all fields of OSPF packet header. */
 
   /* associate packet with ospf interface */
   oi = ospf_if_lookup_recv_if (ospf, iph->ip_src, ifp);
 
+  /* ospf_verify_header() relies on a valid "oi" and thus can be called only
+     after the passive/backbone/other checks below are passed. These checks
+     in turn access the fields of unverified "ospfh" structure for their own
+     purposes and must remain very accurate in doing this. */
+
   /* If incoming interface is passive one, ignore it. */
   if (oi && OSPF_IF_PASSIVE_STATUS (oi) == OSPF_IF_PASSIVE)
     {
@@ -2529,6 +2871,17 @@
       return 0;
     }
 
+  /* Verify more OSPF header fields. */
+  ret = ospf_verify_header (ibuf, oi, iph, ospfh);
+  if (ret < 0)
+  {
+    if (IS_DEBUG_OSPF_PACKET (0, RECV))
+      zlog_debug ("ospf_read[%s]: Header check failed, "
+                  "dropping.",
+                  inet_ntoa (iph->ip_src));
+    return ret;
+  }
+
   /* Show debug receiving packet. */
   if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
     {
@@ -2539,7 +2892,7 @@
         }
 
       zlog_debug ("%s received from [%s] via [%s]",
-                 ospf_packet_type_str[ospfh->type],
+                 LOOKUP (ospf_packet_type_str, ospfh->type),
                  inet_ntoa (ospfh->router_id), IF_NAME (oi));
       zlog_debug (" src [%s],", inet_ntoa (iph->ip_src));
       zlog_debug (" dst [%s]", inet_ntoa (iph->ip_dst));
@@ -2548,20 +2901,6 @@
 	zlog_debug ("-----------------------------------------------------");
   }
 
-  /* Some header verification. */
-  ret = ospf_verify_header (ibuf, oi, iph, ospfh);
-  if (ret < 0)
-    {
-      if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
-        {
-          zlog_debug ("ospf_read[%s/%s]: Header check failed, "
-                     "dropping.",
-                     ospf_packet_type_str[ospfh->type],
-                     inet_ntoa (iph->ip_src));
-        }
-      return ret;
-    }
-
   stream_forward_getp (ibuf, OSPF_HEADER_SIZE);
 
   /* Adjust size to message length. */
diff --git a/ospfd/ospf_packet.h b/ospfd/ospf_packet.h
index 9a47208..337686a 100644
--- a/ospfd/ospf_packet.h
+++ b/ospfd/ospf_packet.h
@@ -46,6 +46,10 @@
 
 #define OSPF_HELLO_REPLY_DELAY          1
 
+/* Return values of functions involved in packet verification, see ospf6d. */
+#define MSG_OK    0
+#define MSG_NG    1
+
 struct ospf_packet
 {
   struct ospf_packet *next;
@@ -117,6 +121,10 @@
   u_int32_t dd_seqnum;
 };
 
+struct ospf_ls_update
+{
+  u_int32_t num_lsas;
+};
 
 /* Macros. */
 /* XXX Perhaps obsolete; function in ospf_packet.c */
@@ -163,4 +171,7 @@
 extern int ospf_poll_timer (struct thread *);
 extern int ospf_hello_reply_timer (struct thread *);
 
+extern const struct message ospf_packet_type_str[];
+extern const size_t ospf_packet_type_str_max;
+
 #endif /* _ZEBRA_OSPF_PACKET_H */
diff --git a/ospfd/ospf_route.c b/ospfd/ospf_route.c
index 267237b..d2e5e1e 100644
--- a/ospfd/ospf_route.c
+++ b/ospfd/ospf_route.c
@@ -272,61 +272,6 @@
       }
 }
 
-static void
-ospf_intra_route_add (struct route_table *rt, struct vertex *v,
-		      struct ospf_area *area)
-{
-  struct route_node *rn;
-  struct ospf_route *or;
-  struct prefix_ipv4 p;
-  struct ospf_path *path;
-  struct vertex_parent *parent;
-  struct listnode *node, *nnode;
-
-  p.family = AF_INET;
-  p.prefix = v->id;
-  if (v->type == OSPF_VERTEX_ROUTER)
-    p.prefixlen = IPV4_MAX_BITLEN;
-  else
-    {
-      struct network_lsa *lsa = (struct network_lsa *) v->lsa;
-      p.prefixlen = ip_masklen (lsa->mask);
-    }
-  apply_mask_ipv4 (&p);
-
-  rn = route_node_get (rt, (struct prefix *) &p);
-  if (rn->info)
-    {
-      zlog_warn ("Same routing information exists for %s", inet_ntoa (v->id));
-      route_unlock_node (rn);
-      return;
-    }
-
-  or = ospf_route_new ();
-
-  if (v->type == OSPF_VERTEX_NETWORK)
-    {
-      or->type = OSPF_DESTINATION_NETWORK;
-
-      for (ALL_LIST_ELEMENTS (v->parents, node, nnode, parent))
-        {
-          path = ospf_path_new ();
-          path->nexthop = parent->nexthop->router;
-          listnode_add (or->paths, path);
-        }
-    }
-  else
-    or->type = OSPF_DESTINATION_ROUTER;
-
-  or->id = v->id;
-  or->u.std.area_id = area->area_id;
-  or->u.std.external_routing= area->external_routing;
-  or->path_type = OSPF_PATH_INTRA_AREA;
-  or->cost = v->distance;
-
-  rn->info = or;
-}
-
 /* RFC2328 16.1. (4). For "router". */
 void
 ospf_intra_add_router (struct route_table *rt, struct vertex *v,
@@ -720,10 +665,6 @@
   r1_type = r1->path_type;
   r2_type = r2->path_type;
 
-  /* If RFC1583Compat flag is on -- all paths are equal. */
-  if (CHECK_FLAG (ospf->config, OSPF_RFC1583_COMPATIBLE))
-    return 0;
-
   /* r1/r2 itself is backbone, and it's Inter-area path. */
   if (OSPF_IS_AREA_ID_BACKBONE (r1->u.std.area_id))
     r1_type = OSPF_PATH_INTER_AREA;
diff --git a/ospfd/ospf_spf.c b/ospfd/ospf_spf.c
index ca20022..d7f9ba2 100644
--- a/ospfd/ospf_spf.c
+++ b/ospfd/ospf_spf.c
@@ -356,9 +356,6 @@
   return -1;
 }
 
-#define ROUTER_LSA_MIN_SIZE 12
-#define ROUTER_LSA_TOS_SIZE 4
-
 /* Find the next link after prev_link from v to w.  If prev_link is
  * NULL, return the first link from v to w.  Ignore stub and virtual links;
  * these link types will never be returned.
@@ -380,8 +377,8 @@
   else
     {
       p = (u_char *) prev_link;
-      p += (ROUTER_LSA_MIN_SIZE +
-            (prev_link->m[0].tos_count * ROUTER_LSA_TOS_SIZE));
+      p += (OSPF_ROUTER_LSA_LINK_SIZE +
+            (prev_link->m[0].tos_count * OSPF_ROUTER_LSA_TOS_SIZE));
     }
 
   lim = ((u_char *) v->lsa) + ntohs (v->lsa->length);
@@ -390,7 +387,7 @@
     {
       l = (struct router_lsa_link *) p;
 
-      p += (ROUTER_LSA_MIN_SIZE + (l->m[0].tos_count * ROUTER_LSA_TOS_SIZE));
+      p += (OSPF_ROUTER_LSA_LINK_SIZE + (l->m[0].tos_count * OSPF_ROUTER_LSA_TOS_SIZE));
 
       if (l->m[0].type != lsa_type)
         continue;
@@ -755,8 +752,8 @@
         {
           l = (struct router_lsa_link *) p;
 
-          p += (ROUTER_LSA_MIN_SIZE +
-                (l->m[0].tos_count * ROUTER_LSA_TOS_SIZE));
+          p += (OSPF_ROUTER_LSA_LINK_SIZE +
+                (l->m[0].tos_count * OSPF_ROUTER_LSA_TOS_SIZE));
 
           /* (a) If this is a link to a stub network, examine the next
              link in V's LSA.  Links to stub networks will be
@@ -989,8 +986,8 @@
         {
           l = (struct router_lsa_link *) p;
 
-          p += (ROUTER_LSA_MIN_SIZE +
-                (l->m[0].tos_count * ROUTER_LSA_TOS_SIZE));
+          p += (OSPF_ROUTER_LSA_LINK_SIZE +
+                (l->m[0].tos_count * OSPF_ROUTER_LSA_TOS_SIZE));
 
           if (l->m[0].type == LSA_LINK_TYPE_STUB)
             ospf_intra_add_stub (rt, l, v, area, parent_is_root);
@@ -1045,6 +1042,7 @@
   route_table_finish (rtrs);
 }
 
+#if 0
 static void
 ospf_rtrs_print (struct route_table *rtrs)
 {
@@ -1104,6 +1102,7 @@
 
   zlog_debug ("ospf_rtrs_print() end");
 }
+#endif
 
 /* Calculating the shortest-path tree for an area. */
 static void
diff --git a/ospfd/ospf_vty.c b/ospfd/ospf_vty.c
index 46e7ffa..97c8e8d 100644
--- a/ospfd/ospf_vty.c
+++ b/ospfd/ospf_vty.c
@@ -80,8 +80,11 @@
   /* match "<0-4294967295>". */
   else
     {
+      if (*str == '-')
+        return -1;
+      errno = 0;
       ret = strtoul (str, &endptr, 10);
-      if (*endptr != '\0' || (ret == ULONG_MAX && errno == ERANGE))
+      if (*endptr != '\0' || errno || ret > UINT32_MAX)
         return -1;
 
       area_id->s_addr = htonl (ret);
@@ -93,29 +96,6 @@
 
 
 static int
-str2distribute_source (const char *str, int *source)
-{
-  /* Sanity check. */
-  if (str == NULL)
-    return 0;
-
-  if (strncmp (str, "k", 1) == 0)
-    *source = ZEBRA_ROUTE_KERNEL;
-  else if (strncmp (str, "c", 1) == 0)
-    *source = ZEBRA_ROUTE_CONNECT;
-  else if (strncmp (str, "s", 1) == 0)
-    *source = ZEBRA_ROUTE_STATIC;
-  else if (strncmp (str, "r", 1) == 0)
-    *source = ZEBRA_ROUTE_RIP;
-  else if (strncmp (str, "b", 1) == 0)
-    *source = ZEBRA_ROUTE_BGP;
-  else
-    return 0;
-
-  return 1;
-}
-
-static int
 str2metric (const char *str, int *metric)
 {
   /* Sanity check. */
@@ -3761,7 +3741,7 @@
   return 0;
 }
 
-/* N.B. This function currently seems to be unused. */
+#if 0
 static int
 show_as_external_lsa_stdvty (struct ospf_lsa *lsa)
 {
@@ -3785,6 +3765,7 @@
 
   return 0;
 }
+#endif
 
 /* Show AS-NSSA-LSA detail information. */
 static int
@@ -5824,7 +5805,8 @@
   int metric = -1;
 
   /* Get distribute source. */
-  if (!str2distribute_source (argv[0], &source))
+  source = proto_redistnum(AFI_IP, argv[0]);
+  if (source < 0 || source == ZEBRA_ROUTE_OSPF)
     return CMD_WARNING;
 
   /* Get metric value. */
@@ -5885,7 +5867,8 @@
   int metric = -1;
 
   /* Get distribute source. */
-  if (!str2distribute_source (argv[0], &source))
+  source = proto_redistnum(AFI_IP, argv[0]);
+  if (source < 0 || source == ZEBRA_ROUTE_OSPF)
     return CMD_WARNING;
 
   /* Get metric value. */
@@ -5949,7 +5932,8 @@
   int metric = -1;
 
   /* Get distribute source. */
-  if (!str2distribute_source (argv[0], &source))
+  source = proto_redistnum(AFI_IP, argv[0]);
+  if (source < 0 || source == ZEBRA_ROUTE_OSPF)
     return CMD_WARNING;
 
   /* Get metric value. */
@@ -5982,7 +5966,8 @@
   int type = -1;
 
   /* Get distribute source. */
-  if (!str2distribute_source (argv[0], &source))
+  source = proto_redistnum(AFI_IP, argv[0]);
+  if (source < 0 || source == ZEBRA_ROUTE_OSPF)
     return CMD_WARNING;
 
   /* Get metric value. */
@@ -6010,7 +5995,8 @@
   int source;
 
   /* Get distribute source. */
-  if (!str2distribute_source (argv[0], &source))
+  source = proto_redistnum(AFI_IP, argv[0]);
+  if (source < 0 || source == ZEBRA_ROUTE_OSPF)
     return CMD_WARNING;
 
   if (argc == 2)
@@ -6031,7 +6017,8 @@
   struct ospf *ospf = vty->index;
   int source;
 
-  if (!str2distribute_source (argv[0], &source))
+  source = proto_redistnum(AFI_IP, argv[0]);
+  if (source < 0 || source == ZEBRA_ROUTE_OSPF)
     return CMD_WARNING;
 
   ospf_routemap_unset (ospf, source);
@@ -6050,7 +6037,8 @@
   int source;
 
   /* Get distribute source. */
-  if (!str2distribute_source (argv[1], &source))
+  source = proto_redistnum(AFI_IP, argv[0]);
+  if (source < 0 || source == ZEBRA_ROUTE_OSPF)
     return CMD_WARNING;
 
   return ospf_distribute_list_out_set (ospf, source, argv[0]);
@@ -6068,7 +6056,8 @@
   struct ospf *ospf = vty->index;
   int source;
 
-  if (!str2distribute_source (argv[1], &source))
+  source = proto_redistnum(AFI_IP, argv[0]);
+  if (source < 0 || source == ZEBRA_ROUTE_OSPF)
     return CMD_WARNING;
 
   return ospf_distribute_list_out_unset (ospf, source, argv[0]);
@@ -7902,9 +7891,9 @@
     {
       /* distribute-list print. */
       for (type = 0; type < ZEBRA_ROUTE_MAX; type++)
-	if (ospf->dlist[type].name)
+	if (DISTRIBUTE_NAME (ospf, type))
 	  vty_out (vty, " distribute-list %s out %s%s", 
-		   ospf->dlist[type].name,
+		   DISTRIBUTE_NAME (ospf, type),
 		   zebra_route_string(type), VTY_NEWLINE);
 
       /* default-information print. */
diff --git a/ospfd/ospf_zebra.c b/ospfd/ospf_zebra.c
index 50ca85e..f8d1cb7 100644
--- a/ospfd/ospf_zebra.c
+++ b/ospfd/ospf_zebra.c
@@ -358,6 +358,7 @@
       stream_putc (s, ZEBRA_ROUTE_OSPF);
       stream_putc (s, flags);
       stream_putc (s, message);
+      stream_putw (s, SAFI_UNICAST);
 
       /* Put prefix information. */
       psize = PSIZE (p->prefixlen);
@@ -427,6 +428,7 @@
       api.type = ZEBRA_ROUTE_OSPF;
       api.flags = 0;
       api.message = 0;
+      api.safi = SAFI_UNICAST;
       api.ifindex_num = 0;
       api.nexthop_num = 0;
 
@@ -483,6 +485,7 @@
       api.type = ZEBRA_ROUTE_OSPF;
       api.flags = ZEBRA_FLAG_BLACKHOLE;
       api.message = 0;
+      api.safi = SAFI_UNICAST;
       SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
       api.nexthop_num = 0;
       api.ifindex_num = 0;
@@ -505,6 +508,7 @@
       api.type = ZEBRA_ROUTE_OSPF;
       api.flags = ZEBRA_FLAG_BLACKHOLE;
       api.message = 0;
+      api.safi = SAFI_UNICAST;
       SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
       api.nexthop_num = 0;
       api.ifindex_num = 0;
@@ -670,7 +674,7 @@
   if (is_prefix_default (&ei->p))
     if (ospf->default_originate == DEFAULT_ORIGINATE_NONE)
       {
-        zlog_info ("LSA[Type5:0.0.0.0]: Not originate AS-exntenal-LSA "
+        zlog_info ("LSA[Type5:0.0.0.0]: Not originate AS-external-LSA "
                    "for default");
         return 0;
       }
diff --git a/ospfd/ospf_zebra.h b/ospfd/ospf_zebra.h
index 3efd895..fbb3444 100644
--- a/ospfd/ospf_zebra.h
+++ b/ospfd/ospf_zebra.h
@@ -50,8 +50,6 @@
 extern void ospf_zebra_add_discard (struct prefix_ipv4 *);
 extern void ospf_zebra_delete_discard (struct prefix_ipv4 *);
 
-extern int ospf_default_originate_timer (struct thread *);
-
 extern int ospf_redistribute_check (struct ospf *, struct external_info *,
 				    int *);
 extern int ospf_distribute_check_connected (struct ospf *,
diff --git a/ospfd/ospfd.c b/ospfd/ospfd.c
index 0188ffd..e840513 100644
--- a/ospfd/ospfd.c
+++ b/ospfd/ospfd.c
@@ -1204,6 +1204,7 @@
   return 1;
 }
 
+#if 0
 /* XXX: unused? Leave for symmetry? */
 static int
 ospf_area_nssa_translator_role_unset (struct ospf *ospf,
@@ -1221,6 +1222,7 @@
 
   return 1;
 }
+#endif
 
 int
 ospf_area_export_list_set (struct ospf *ospf,
diff --git a/ospfd/ospfd.h b/ospfd/ospfd.h
index 0e57c45..7a56d16 100644
--- a/ospfd/ospfd.h
+++ b/ospfd/ospfd.h
@@ -72,10 +72,6 @@
 #define OSPF_ALLSPFROUTERS              0xe0000005      /* 224.0.0.5 */
 #define OSPF_ALLDROUTERS                0xe0000006      /* 224.0.0.6 */
 
-/* XXX Where is this used? And why it was used only if compiled with
- * NSSA support. */
-#define OSPF_LOOPer                     0x7f000000      /* 127.0.0.0 */
-
 #define OSPF_AREA_BACKBONE              0x00000000      /* 0.0.0.0 */
 
 /* OSPF Authentication Type. */
diff --git a/pkgsrc/.cvsignore b/pkgsrc/.cvsignore
deleted file mode 100644
index 818bd17..0000000
--- a/pkgsrc/.cvsignore
+++ /dev/null
@@ -1,5 +0,0 @@
-Makefile
-Makefile.in
-*.sh
-.arch-inventory
-.arch-ids
diff --git a/ports/.cvsignore b/ports/.cvsignore
deleted file mode 100644
index 73bcf19..0000000
--- a/ports/.cvsignore
+++ /dev/null
@@ -1,3 +0,0 @@
-.arch-inventory
-.arch-ids
-
diff --git a/ports/files/.cvsignore b/ports/files/.cvsignore
deleted file mode 100644
index 73bcf19..0000000
--- a/ports/files/.cvsignore
+++ /dev/null
@@ -1,3 +0,0 @@
-.arch-inventory
-.arch-ids
-
diff --git a/ports/pkg/.cvsignore b/ports/pkg/.cvsignore
deleted file mode 100644
index 73bcf19..0000000
--- a/ports/pkg/.cvsignore
+++ /dev/null
@@ -1,3 +0,0 @@
-.arch-inventory
-.arch-ids
-
diff --git a/redhat/.cvsignore b/redhat/.cvsignore
deleted file mode 100644
index 98ca65a..0000000
--- a/redhat/.cvsignore
+++ /dev/null
@@ -1,7 +0,0 @@
-zebra.spec
-quagga.spec
-Makefile
-Makefile.in
-.nfs*
-.arch-inventory
-.arch-ids
diff --git a/ripd/.cvsignore b/ripd/.cvsignore
deleted file mode 100644
index 41fd8c8..0000000
--- a/ripd/.cvsignore
+++ /dev/null
@@ -1,14 +0,0 @@
-Makefile
-Makefile.in
-*.o
-ripd
-ripd.conf
-tags
-TAGS
-.deps
-.nfs*
-*.lo
-*.la
-*.libs
-.arch-inventory
-.arch-ids
diff --git a/ripd/rip_interface.c b/ripd/rip_interface.c
index d3b55fc..810b71c 100644
--- a/ripd/rip_interface.c
+++ b/ripd/rip_interface.c
@@ -78,9 +78,8 @@
 {
   int ret;
 
-  ret = setsockopt_multicast_ipv4 (sock, 
+  ret = setsockopt_ipv4_multicast (sock,
 				   IP_ADD_MEMBERSHIP, 
-				   ifa, 
 				   group.s_addr, 
 				   ifindex); 
 
@@ -100,9 +99,8 @@
 {
   int ret;
 
-  ret = setsockopt_multicast_ipv4 (sock, 
+  ret = setsockopt_ipv4_multicast (sock,
 				   IP_DROP_MEMBERSHIP, 
-				   ifa, 
 				   group.s_addr, 
 				   ifindex);
 
@@ -138,18 +136,13 @@
 void
 rip_interface_multicast_set (int sock, struct connected *connected)
 {
-  struct in_addr addr;
-  
   assert (connected != NULL);
   
-  addr = CONNECTED_ID(connected)->u.prefix4;
-
-  if (setsockopt_multicast_ipv4 (sock, IP_MULTICAST_IF, addr, 0, 
-                                 connected->ifp->ifindex) < 0) 
+  if (setsockopt_ipv4_multicast_if (sock, connected->ifp->ifindex) < 0)
     {
       zlog_warn ("Can't setsockopt IP_MULTICAST_IF on fd %d to "
-		 "source address %s for interface %s",
-		 sock, inet_ntoa(addr),
+		 "ifindex %d for interface %s",
+		 sock, connected->ifp->ifindex,
 		 connected->ifp->name);
     }
   
diff --git a/ripd/rip_main.c b/ripd/rip_main.c
index 57b5f3a..ccb5fa0 100644
--- a/ripd/rip_main.c
+++ b/ripd/rip_main.c
@@ -32,6 +32,7 @@
 #include "log.h"
 #include "privs.h"
 #include "sigevent.h"
+#include "zclient.h"
 
 #include "ripd/ripd.h"
 
@@ -41,6 +42,7 @@
   { "daemon",      no_argument,       NULL, 'd'},
   { "config_file", required_argument, NULL, 'f'},
   { "pid_file",    required_argument, NULL, 'i'},
+  { "socket",      required_argument, NULL, 'z'},
   { "help",        no_argument,       NULL, 'h'},
   { "dryrun",      no_argument,       NULL, 'C'},
   { "vty_addr",    required_argument, NULL, 'A'},
@@ -109,6 +111,7 @@
 -d, --daemon       Runs in daemon mode\n\
 -f, --config_file  Set configuration file name\n\
 -i, --pid_file     Set process identifier file name\n\
+-z, --socket       Set path of zebra socket\n\
 -A, --vty_addr     Set vty's bind address\n\
 -P, --vty_port     Set vty's port number\n\
 -C, --dryrun       Check configuration for validity and exit\n\
@@ -206,7 +209,7 @@
     {
       int opt;
 
-      opt = getopt_long (argc, argv, "df:i:hA:P:u:g:rvC", longopts, 0);
+      opt = getopt_long (argc, argv, "df:i:z:hA:P:u:g:rvC", longopts, 0);
     
       if (opt == EOF)
 	break;
@@ -227,6 +230,9 @@
         case 'i':
           pid_file = optarg;
           break;
+	case 'z':
+	  zclient_serv_path_set (optarg);
+	  break;
 	case 'P':
           /* Deal with atoi() returning 0 on failure, and ripd not
              listening on rip port... */
diff --git a/ripd/rip_zebra.c b/ripd/rip_zebra.c
index c476d8f..199e85e 100644
--- a/ripd/rip_zebra.c
+++ b/ripd/rip_zebra.c
@@ -46,6 +46,7 @@
       api.type = ZEBRA_ROUTE_RIP;
       api.flags = 0;
       api.message = 0;
+      api.safi = SAFI_UNICAST;
       SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
       api.nexthop_num = 1;
       api.nexthop = &nexthop;
@@ -76,6 +77,7 @@
       api.type = ZEBRA_ROUTE_RIP;
       api.flags = 0;
       api.message = 0;
+      api.safi = SAFI_UNICAST;
       SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
       api.nexthop_num = 1;
       api.nexthop = &nexthop;
@@ -205,7 +207,8 @@
   {ZEBRA_ROUTE_CONNECT, 1, "connected"},
   {ZEBRA_ROUTE_STATIC,  1, "static"},
   {ZEBRA_ROUTE_OSPF,    1, "ospf"},
-  {ZEBRA_ROUTE_BGP,     1, "bgp"},
+  {ZEBRA_ROUTE_BGP,     2, "bgp"},
+  {ZEBRA_ROUTE_BABEL,   2, "babel"},
   {0, 0, NULL}
 };
 
diff --git a/ripngd/.cvsignore b/ripngd/.cvsignore
deleted file mode 100644
index 48f45ce..0000000
--- a/ripngd/.cvsignore
+++ /dev/null
@@ -1,14 +0,0 @@
-Makefile
-Makefile.in
-*.o
-ripngd
-ripngd.conf
-tags
-TAGS
-.deps
-.nfs*
-*.lo
-*.la
-*.libs
-.arch-inventory
-.arch-ids
diff --git a/ripngd/ripng_main.c b/ripngd/ripng_main.c
index 85209a1..f4a6244 100644
--- a/ripngd/ripng_main.c
+++ b/ripngd/ripng_main.c
@@ -47,6 +47,7 @@
   { "daemon",      no_argument,       NULL, 'd'},
   { "config_file", required_argument, NULL, 'f'},
   { "pid_file",    required_argument, NULL, 'i'},
+  { "socket",      required_argument, NULL, 'z'},
   { "dryrun",      no_argument,       NULL, 'C'},
   { "help",        no_argument,       NULL, 'h'},
   { "vty_addr",    required_argument, NULL, 'A'},
@@ -112,6 +113,7 @@
 -d, --daemon       Runs in daemon mode\n\
 -f, --config_file  Set configuration file name\n\
 -i, --pid_file     Set process identifier file name\n\
+-z, --socket       Set path of zebra socket\n\
 -A, --vty_addr     Set vty's bind address\n\
 -P, --vty_port     Set vty's port number\n\
 -r, --retain       When program terminates, retain added route by ripngd.\n\
@@ -205,7 +207,7 @@
     {
       int opt;
 
-      opt = getopt_long (argc, argv, "df:i:hA:P:u:g:vC", longopts, 0);
+      opt = getopt_long (argc, argv, "df:i:z:hA:P:u:g:vC", longopts, 0);
     
       if (opt == EOF)
 	break;
@@ -225,7 +227,10 @@
 	  break;
         case 'i':
           pid_file = optarg;
-          break; 
+          break;
+	case 'z':
+	  zclient_serv_path_set (optarg);
+	  break;
 	case 'P':
           /* Deal with atoi() returning 0 on failure, and ripngd not
              listening on ripngd port... */
diff --git a/ripngd/ripng_zebra.c b/ripngd/ripng_zebra.c
index 4c44455..8e76606 100644
--- a/ripngd/ripng_zebra.c
+++ b/ripngd/ripng_zebra.c
@@ -53,6 +53,7 @@
       api.type = ZEBRA_ROUTE_RIPNG;
       api.flags = 0;
       api.message = 0;
+      api.safi = SAFI_UNICAST;
       SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
       api.nexthop_num = 1;
       api.nexthop = &nexthop;
@@ -77,6 +78,7 @@
       api.type = ZEBRA_ROUTE_RIPNG;
       api.flags = 0;
       api.message = 0;
+      api.safi = SAFI_UNICAST;
       SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
       api.nexthop_num = 1;
       api.nexthop = &nexthop;
@@ -215,7 +217,8 @@
   {ZEBRA_ROUTE_CONNECT, 1, "connected"},
   {ZEBRA_ROUTE_STATIC,  1, "static"},
   {ZEBRA_ROUTE_OSPF6,   1, "ospf6"},
-  {ZEBRA_ROUTE_BGP,     1, "bgp"},
+  {ZEBRA_ROUTE_BGP,     2, "bgp"},
+  {ZEBRA_ROUTE_BABEL,   2, "babel"},
   {0, 0, NULL}
 };
 
@@ -287,198 +290,147 @@
 
 DEFUN (ripng_redistribute_type,
        ripng_redistribute_type_cmd,
-       "redistribute (kernel|connected|static|ospf6|bgp)",
-       "Redistribute information from another routing protocol\n"
-       "Kernel routes\n"
-       "Connected\n"
-       "Static routes\n"
-       "Open Shortest Path First (OSPFv3)\n"
-       "Border Gateway Protocol (BGP)\n")
+       "redistribute " QUAGGA_REDIST_STR_RIPNGD,
+       "Redistribute\n"
+       QUAGGA_REDIST_HELP_STR_RIPNGD)
 {
-  int i;
+  int type;
 
-  for(i = 0; redist_type[i].str; i++) 
+  type = proto_redistnum(AFI_IP6, argv[0]);
+
+  if (type < 0)
     {
-      if (strncmp (redist_type[i].str, argv[0], 
-		   redist_type[i].str_min_len) == 0) 
-	{
-	  zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, redist_type[i].type);
-	  return CMD_SUCCESS;
-	}
+      vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE);
+      return CMD_WARNING;
     }
 
-  vty_out(vty, "Invalid type %s%s", argv[0],
-	  VTY_NEWLINE);
-
-  return CMD_WARNING;
+  zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, type);
+  return CMD_SUCCESS;
 }
 
 DEFUN (no_ripng_redistribute_type,
        no_ripng_redistribute_type_cmd,
-       "no redistribute (kernel|connected|static|ospf6|bgp)",
+       "no redistribute " QUAGGA_REDIST_STR_RIPNGD,
        NO_STR
-       "Redistribute information from another routing protocol\n"
-       "Kernel routes\n"
-       "Connected\n"
-       "Static routes\n"
-       "Open Shortest Path First (OSPFv3)\n"
-       "Border Gateway Protocol (BGP)\n")
+       "Redistribute\n"
+       QUAGGA_REDIST_HELP_STR_RIPNGD)
 {
-  int i;
+  int type;
 
-  for (i = 0; redist_type[i].str; i++) 
+  type = proto_redistnum(AFI_IP6, argv[0]);
+
+  if (type < 0)
     {
-      if (strncmp(redist_type[i].str, argv[0], 
-		  redist_type[i].str_min_len) == 0) 
-	{
-	  ripng_redistribute_metric_unset (redist_type[i].type);
-	  ripng_redistribute_routemap_unset (redist_type[i].type);
-	  return ripng_redistribute_unset (redist_type[i].type);
-        }
+      vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE);
+      return CMD_WARNING;
     }
 
-  vty_out(vty, "Invalid type %s%s", argv[0],
-	  VTY_NEWLINE);
-
-  return CMD_WARNING;
+  ripng_redistribute_metric_unset (type);
+  ripng_redistribute_routemap_unset (type);
+  return ripng_redistribute_unset (type);
 }
 
 
 DEFUN (ripng_redistribute_type_metric,
        ripng_redistribute_type_metric_cmd,
-       "redistribute (kernel|connected|static|ospf6|bgp) metric <0-16>",
-       "Redistribute information from another routing protocol\n"
-       "Kernel routes\n"
-       "Connected\n"
-       "Static routes\n"
-       "Open Shortest Path First (OSPFv3)\n"
-       "Border Gateway Protocol (BGP)\n"
+       "redistribute " QUAGGA_REDIST_STR_RIPNGD " metric <0-16>",
+       "Redistribute\n"
+       QUAGGA_REDIST_HELP_STR_RIPNGD
        "Metric\n"
        "Metric value\n")
 {
-  int i;
+  int type;
   int metric;
 
   metric = atoi (argv[1]);
+  type = proto_redistnum(AFI_IP6, argv[0]);
 
-  for (i = 0; redist_type[i].str; i++) {
-    if (strncmp(redist_type[i].str, argv[0],
-		redist_type[i].str_min_len) == 0) 
-      {
-	ripng_redistribute_metric_set (redist_type[i].type, metric);
-	zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, redist_type[i].type);
-	return CMD_SUCCESS;
-      }
-  }
+  if (type < 0)
+    {
+      vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE);
+      return CMD_WARNING;
+    }
 
-  vty_out(vty, "Invalid type %s%s", argv[0],
-	  VTY_NEWLINE);
-
-  return CMD_WARNING;
+  ripng_redistribute_metric_set (type, metric);
+  zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, type);
+  return CMD_SUCCESS;
 }
 
 ALIAS (no_ripng_redistribute_type,
        no_ripng_redistribute_type_metric_cmd,
-       "no redistribute (kernel|connected|static|ospf6|bgp) metric <0-16>",
+       "no redistribute " QUAGGA_REDIST_STR_RIPNGD " metric <0-16>",
        NO_STR
-       "Redistribute information from another routing protocol\n"
-       "Kernel routes\n"
-       "Connected\n"
-       "Static routes\n"
-       "Open Shortest Path First (OSPFv3)\n"
-       "Border Gateway Protocol (BGP)\n"
+       "Redistribute\n"
+       QUAGGA_REDIST_HELP_STR_RIPNGD
        "Metric\n"
        "Metric value\n")
 
 DEFUN (ripng_redistribute_type_routemap,
        ripng_redistribute_type_routemap_cmd,
-       "redistribute (kernel|connected|static|ospf6|bgp) route-map WORD",
-       "Redistribute information from another routing protocol\n"
-       "Kernel routes\n"
-       "Connected\n"
-       "Static routes\n"
-       "Open Shortest Path First (OSPFv3)\n"
-       "Border Gateway Protocol (BGP)\n"
+       "redistribute " QUAGGA_REDIST_STR_RIPNGD " route-map WORD",
+       "Redistribute\n"
+       QUAGGA_REDIST_HELP_STR_RIPNGD
        "Route map reference\n"
        "Pointer to route-map entries\n")
 {
-  int i;
+  int type;
 
-  for (i = 0; redist_type[i].str; i++) {
-    if (strncmp(redist_type[i].str, argv[0],
-		redist_type[i].str_min_len) == 0) 
-      {
-	ripng_redistribute_routemap_set (redist_type[i].type, argv[1]);
-	zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, redist_type[i].type);
-	return CMD_SUCCESS;
-      }
-  }
+  type = proto_redistnum(AFI_IP6, argv[0]);
 
-  vty_out(vty, "Invalid type %s%s", argv[0],
-	  VTY_NEWLINE);
+  if (type < 0)
+    {
+      vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE);
+      return CMD_WARNING;
+    }
 
-  return CMD_WARNING;
+  ripng_redistribute_routemap_set (type, argv[1]);
+  zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, type);
+ return CMD_SUCCESS;
 }
 
 ALIAS (no_ripng_redistribute_type,
        no_ripng_redistribute_type_routemap_cmd,
-       "no redistribute (kernel|connected|static|ospf6|bgp) route-map WORD",
+       "no redistribute " QUAGGA_REDIST_STR_RIPNGD " route-map WORD",
        NO_STR
-       "Redistribute information from another routing protocol\n"
-       "Kernel routes\n"
-       "Connected\n"
-       "Static routes\n"
-       "Open Shortest Path First (OSPFv3)\n"
-       "Border Gateway Protocol (BGP)\n"
+       "Redistribute\n"
+       QUAGGA_REDIST_HELP_STR_RIPNGD
        "Route map reference\n"
        "Pointer to route-map entries\n")
 
 DEFUN (ripng_redistribute_type_metric_routemap,
        ripng_redistribute_type_metric_routemap_cmd,
-       "redistribute (kernel|connected|static|ospf6|bgp) metric <0-16> route-map WORD",
-       "Redistribute information from another routing protocol\n"
-       "Kernel routes\n"
-       "Connected\n"
-       "Static routes\n"
-       "Open Shortest Path First (OSPFv3)\n"
-       "Border Gateway Protocol (BGP)\n"
+       "redistribute " QUAGGA_REDIST_STR_RIPNGD " metric <0-16> route-map WORD",
+       "Redistribute\n"
+       QUAGGA_REDIST_HELP_STR_RIPNGD
        "Metric\n"
        "Metric value\n"
        "Route map reference\n"
        "Pointer to route-map entries\n")
 {
-  int i;
+  int type;
   int metric;
 
+  type = proto_redistnum(AFI_IP6, argv[0]);
   metric = atoi (argv[1]);
 
-  for (i = 0; redist_type[i].str; i++) {
-    if (strncmp(redist_type[i].str, argv[0],
-		redist_type[i].str_min_len) == 0) 
-      {
-	ripng_redistribute_metric_set (redist_type[i].type, metric);
-	ripng_redistribute_routemap_set (redist_type[i].type, argv[2]);
-	zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, redist_type[i].type);
-	return CMD_SUCCESS;
-      }
-  }
+  if (type < 0)
+    {
+      vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE);
+      return CMD_WARNING;
+    }
 
-  vty_out(vty, "Invalid type %s%s", argv[0],
-	  VTY_NEWLINE);
-
-  return CMD_WARNING;
+  ripng_redistribute_metric_set (type, metric);
+  ripng_redistribute_routemap_set (type, argv[2]);
+  zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, type);
+  return CMD_SUCCESS;
 }
 
 ALIAS (no_ripng_redistribute_type,
        no_ripng_redistribute_type_metric_routemap_cmd,
-       "no redistribute (kernel|connected|static|ospf6|bgp) metric <0-16> route-map WORD",
+       "no redistribute " QUAGGA_REDIST_STR_RIPNGD " metric <0-16> route-map WORD",
        NO_STR
-       "Redistribute information from another routing protocol\n"
-       "Kernel routes\n"
-       "Connected\n"
-       "Static routes\n"
-       "Open Shortest Path First (OSPFv3)\n"
-       "Border Gateway Protocol (BGP)\n"
+       "Redistribute\n"
+       QUAGGA_REDIST_HELP_STR_RIPNGD
        "Route map reference\n"
        "Pointer to route-map entries\n")
 
diff --git a/ripngd/ripngd.c b/ripngd/ripngd.c
index 6e32d83..8e97c2f 100644
--- a/ripngd/ripngd.c
+++ b/ripngd/ripngd.c
@@ -117,6 +117,11 @@
   ret = setsockopt_ipv6_pktinfo (sock, 1);
   if (ret < 0)
     return ret;
+#ifdef IPTOS_PREC_INTERNETCONTROL
+  ret = setsockopt_ipv6_tclass (sock, IPTOS_PREC_INTERNETCONTROL);
+  if (ret < 0)
+    return ret;
+#endif
   ret = setsockopt_ipv6_multicast_hops (sock, 255);
   if (ret < 0)
     return ret;
@@ -2480,28 +2485,10 @@
   unsigned long update;
   unsigned long timeout;
   unsigned long garbage;
-  char *endptr = NULL;
 
-  update = strtoul (argv[0], &endptr, 10);
-  if (update == ULONG_MAX || *endptr != '\0')
-    {
-      vty_out (vty, "update timer value error%s", VTY_NEWLINE);
-      return CMD_WARNING;
-    }
-  
-  timeout = strtoul (argv[1], &endptr, 10);
-  if (timeout == ULONG_MAX || *endptr != '\0')
-    {
-      vty_out (vty, "timeout timer value error%s", VTY_NEWLINE);
-      return CMD_WARNING;
-    }
-  
-  garbage = strtoul (argv[2], &endptr, 10);
-  if (garbage == ULONG_MAX || *endptr != '\0')
-    {
-      vty_out (vty, "garbage timer value error%s", VTY_NEWLINE);
-      return CMD_WARNING;
-    }
+  VTY_GET_INTEGER_RANGE("update timer", update, argv[0], 0, 65535);
+  VTY_GET_INTEGER_RANGE("timeout timer", timeout, argv[1], 0, 65535);
+  VTY_GET_INTEGER_RANGE("garbage timer", garbage, argv[2], 0, 65535);
 
   /* Set each timer value. */
   ripng->update_time = update;
diff --git a/solaris/.cvsignore b/solaris/.cvsignore
deleted file mode 100644
index 810874c..0000000
--- a/solaris/.cvsignore
+++ /dev/null
@@ -1,14 +0,0 @@
-Makefile
-Makefile.in
-?.manifest
-*.xml
-pkginfo.*.full
-pkginfo.tmpl
-prototype.daemons
-prototype.dev
-prototype.doc
-prototype.libs
-prototype.smf
-quagga.init
-*.pkg
-*.pkg.gz
diff --git a/solaris/Makefile.am b/solaris/Makefile.am
index ecaf9b9..dcee240 100644
--- a/solaris/Makefile.am
+++ b/solaris/Makefile.am
@@ -1,5 +1,4 @@
 # Solaris packages automake file
-# $Id$
 
 # XXX This file uses GNU make extensions.
 
diff --git a/solaris/quagga.init.in b/solaris/quagga.init.in
index 8457e99..0042624 100755
--- a/solaris/quagga.init.in
+++ b/solaris/quagga.init.in
@@ -20,8 +20,6 @@
 # Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 # 02111-1307, USA.
 #
-# $Id$
-#
 # Starts/stops the given daemon
 
 SMFINCLUDE=/lib/svc/share/smf_include.sh
diff --git a/tests/.cvsignore b/tests/.cvsignore
deleted file mode 100644
index 4c3e50d..0000000
--- a/tests/.cvsignore
+++ /dev/null
@@ -1,22 +0,0 @@
-Makefile
-Makefile.in
-*.o
-tags
-TAGS
-.deps
-.nfs*
-*.lo
-*.la
-*.libs
-testsig
-.arch-inventory
-.arch-ids
-testbuffer
-testmemory
-testsig
-aspathtest
-heavy
-heavythread
-heavywq
-testprivs
-teststream
diff --git a/tests/aspath_test.c b/tests/aspath_test.c
index 9e51e8d..4a2ce9a 100644
--- a/tests/aspath_test.c
+++ b/tests/aspath_test.c
@@ -6,6 +6,7 @@
 
 #include "bgpd/bgpd.h"
 #include "bgpd/bgp_aspath.h"
+#include "bgpd/bgp_attr.h"
 
 #define VT100_RESET "\x1b[0m"
 #define VT100_RED "\x1b[31m"
@@ -407,7 +408,7 @@
     "#ASNs = 0, data = seq(8466 3 52737 4096 3456)",
     { 0x2,0x0, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80 },
     12,
-    { "", "",
+    { NULL, NULL,
       0, 0, 0, 0, 0, 0 },
   },
   { /* 26  */ 
@@ -417,10 +418,190 @@
       0x2,0x2, 0x10,0x00, 0x0d,0x80 },
     14
     ,
-    { "", "",
+    { NULL, NULL,
       0, 0, 0, 0, 0, 0 },
   },
-  { NULL, NULL, {0}, 0, { NULL, 0, 0 } }
+  { /* 27  */ 
+    "invalid segment type",
+    "type=8(4096 3456)",
+    { 0x8,0x2, 0x10,0x00, 0x0d,0x80 },
+    14
+    ,
+    { NULL, NULL,
+      0, 0, 0, 0, 0, 0 },
+  },  { NULL, NULL, {0}, 0, { NULL, 0, 0 } }
+};
+
+/* */
+static struct aspath_tests {
+  const char *desc;
+  const struct test_segment *segment;
+  const char *shouldbe;  /* String it should evaluate to */
+  const enum as4 { AS4_DATA, AS2_DATA }
+          as4; 	/* whether data should be as4 or not (ie as2) */
+  const int result;	/* expected result for bgp_attr_parse */
+  const int cap;	/* capabilities to set for peer */
+  const char attrheader [1024];
+  size_t len;
+} aspath_tests [] =
+{
+  /* 0 */
+  {
+    "basic test",
+    &test_segments[0],
+    "8466 3 52737 4096",
+    AS2_DATA, 0,
+    0,
+    { BGP_ATTR_FLAG_TRANS,
+      BGP_ATTR_AS_PATH, 
+      10,
+    },
+    3,
+  },
+  /* 1 */
+  {
+    "length too short",
+    &test_segments[0],
+    "8466 3 52737 4096",
+    AS2_DATA, -1,
+    0,
+    { BGP_ATTR_FLAG_TRANS,
+      BGP_ATTR_AS_PATH, 
+      8,
+    },
+    3,
+  },
+  /* 2 */
+  {
+    "length too long",
+    &test_segments[0],
+    "8466 3 52737 4096",
+    AS2_DATA, -1,
+    0,
+    { BGP_ATTR_FLAG_TRANS,
+      BGP_ATTR_AS_PATH, 
+      12,
+    },
+    3,
+  },
+  /* 3 */
+  {
+    "incorrect flag",
+    &test_segments[0],
+    "8466 3 52737 4096",
+    AS2_DATA, -1,
+    0,
+    { BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
+      BGP_ATTR_AS_PATH, 
+      10,
+    },
+    3,
+  },
+  /* 4 */
+  {
+    "as4_path, with as2 format data",
+    &test_segments[0],
+    "8466 3 52737 4096",
+    AS2_DATA, -1,
+    0,
+    { BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
+      BGP_ATTR_AS4_PATH, 
+      10,
+    },
+    3,
+  },
+  /* 5 */
+  {
+    "as4, with incorrect attr length",
+    &test_segments[0],
+    "8466 3 52737 4096",
+    AS4_DATA, -1,
+    PEER_CAP_AS4_RCV,
+    { BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
+      BGP_ATTR_AS4_PATH, 
+      10,
+    },
+    3,
+  },
+  /* 6 */
+  {
+    "basic 4-byte as-path",
+    &test_segments[0],
+    "8466 3 52737 4096",
+    AS4_DATA, 0,
+    PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
+    { BGP_ATTR_FLAG_TRANS,
+      BGP_ATTR_AS_PATH, 
+      18,
+    },
+    3,
+  },
+  /* 7 */
+  {
+    "4b AS_PATH: too short",
+    &test_segments[0],
+    "8466 3 52737 4096",
+    AS4_DATA, -1,
+    PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
+    { BGP_ATTR_FLAG_TRANS,
+      BGP_ATTR_AS_PATH, 
+      16,
+    },
+    3,
+  },
+  /* 8 */
+  {
+    "4b AS_PATH: too long",
+    &test_segments[0],
+    "8466 3 52737 4096",
+    AS4_DATA, -1,
+    PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
+    { BGP_ATTR_FLAG_TRANS,
+      BGP_ATTR_AS_PATH, 
+      20,
+    },
+    3,
+  },
+  /* 9 */
+  {
+    "4b AS_PATH: too long2",
+    &test_segments[0],
+    "8466 3 52737 4096",
+    AS4_DATA, -1,
+    PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
+    { BGP_ATTR_FLAG_TRANS,
+      BGP_ATTR_AS_PATH, 
+      22,
+    },
+    3,
+  },
+  /* 10 */
+  {
+    "4b AS_PATH: bad flags",
+    &test_segments[0],
+    "8466 3 52737 4096",
+    AS4_DATA, -1,
+    PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
+    { BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
+      BGP_ATTR_AS_PATH, 
+      18,
+    },
+    3,
+  },
+  /* 11 */
+  {
+    "4b AS_PATH: confed",
+    &test_segments[6],
+    "8466 3 52737 4096",
+    AS4_DATA, -1,
+    PEER_CAP_AS4_ADV,
+    { BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
+      BGP_ATTR_AS4_PATH, 
+      14,
+    },
+    3,
+  },
+  { NULL, NULL, NULL, 0, 0, 0, { 0 }, 0 },
 };
 
 /* prepending tests */
@@ -430,21 +611,25 @@
   struct test_spec sp;
 } prepend_tests[] = 
 {
+  /* 0 */
   { &test_segments[0], &test_segments[1],
     { "8466 3 52737 4096 8722 4",
       "8466 3 52737 4096 8722 4",
       6, 0, NOT_ALL_PRIVATE, 4096, 1, 8466 },
   },
+  /* 1 */
   { &test_segments[1], &test_segments[3],
     { "8722 4 8482 51457 {5204}",
       "8722 4 8482 51457 {5204}",
       5, 0, NOT_ALL_PRIVATE, 5204, 1, 8722 }
   },
+  /* 2 */
   { &test_segments[3], &test_segments[4],
     { "8482 51457 {5204} 8467 59649 {4196,48658} {17322,30745}",
       "8482 51457 {5204} 8467 59649 {4196,48658} {17322,30745}",
       7, 0, NOT_ALL_PRIVATE, 5204, 1, 8482 },
   },
+  /* 3 */
   { &test_segments[4], &test_segments[5],
     { "8467 59649 {4196,48658} {17322,30745} 6435 59408 21665"
       " {2457,4369,61697} 1842 41590 51793",
@@ -452,11 +637,13 @@
       " {2457,4369,61697} 1842 41590 51793",
       11, 0, NOT_ALL_PRIVATE, 61697, 1, 8467 }
   },
+  /* 4 */
   { &test_segments[5], &test_segments[6],
-    { "6435 59408 21665 {2457,4369,61697} 1842 41590 51793 (123 456 789)",
-      "6435 59408 21665 {2457,4369,61697} 1842 41590 51793 (123 456 789)",
-      7, 3, NOT_ALL_PRIVATE, 123, 1, 6435 },
+    { "6435 59408 21665 {2457,4369,61697} 1842 41590 51793",
+      "6435 59408 21665 {2457,4369,61697} 1842 41590 51793",
+      7, 0, NOT_ALL_PRIVATE, 1842, 1, 6435 },
   },
+  /* 5 */
   { &test_segments[6], &test_segments[7],
     { "(123 456 789) (123 456 789) (111 222)",
       "",
@@ -649,7 +836,7 @@
       s = stream_new (len);
       stream_put (s, data, len);
     }
-  as = aspath_parse (s, len, use32bit, 0);
+  as = aspath_parse (s, len, use32bit);
   
   if (s)
     stream_free (s);
@@ -682,6 +869,12 @@
   static struct stream *s;
   struct aspath *asinout, *asconfeddel, *asstr, *as4;
   
+  if (as == NULL && sp->shouldbe == NULL)
+    {
+      printf ("Correctly failed to parse\n");
+      return fails;
+    }
+  
   out = aspath_snmp_pathseg (as, &bytes);
   asinout = make_aspath (out, bytes, 0);
   
@@ -826,7 +1019,7 @@
   printf ("%s: %s\n", t->name, t->desc);
 
   asp = make_aspath (t->asdata, t->len, 0);
-
+  
   printf ("aspath: %s\nvalidating...:\n", aspath_print (asp));
 
   if (!validate (asp, &t->sp))
@@ -835,7 +1028,9 @@
     printf (FAILED "\n");
   
   printf ("\n");
-  aspath_unintern (asp);
+  
+  if (asp)
+    aspath_unintern (asp);
 }
 
 /* prepend testing */
@@ -891,7 +1086,8 @@
     printf (FAILED "!\n");
   
   printf ("\n");
-  aspath_unintern (asp1);
+  if (asp1)
+    aspath_unintern (asp1);
   aspath_free (asp2);
 }
 
@@ -993,30 +1189,106 @@
       aspath_unintern (asp2);
     }
 }
-     
+
+static int
+handle_attr_test (struct aspath_tests *t)
+{
+  struct bgp bgp = { 0 }; 
+  struct peer peer = { 0 };
+  struct attr attr = { 0 };  
+  int ret;
+  int initfail = failed;
+  struct aspath *asp;
+  size_t datalen;
+  
+  asp = make_aspath (t->segment->asdata, t->segment->len, 0);
+    
+  peer.ibuf = stream_new (BGP_MAX_PACKET_SIZE);
+  peer.obuf = stream_fifo_new ();
+  peer.bgp = &bgp;
+  peer.host = (char *)"none";
+  peer.fd = -1;
+  peer.cap = t->cap;
+  
+  stream_write (peer.ibuf, t->attrheader, t->len);
+  datalen = aspath_put (peer.ibuf, asp, t->as4 == AS4_DATA);
+  
+  ret = bgp_attr_parse (&peer, &attr, t->len + datalen, NULL, NULL);
+  
+  if (ret != t->result)
+    {
+      printf ("bgp_attr_parse returned %d, expected %d\n", ret, t->result);
+      printf ("datalen %d\n", datalen);
+      failed++;
+    }
+  if (ret != 0)
+    goto out;
+  
+  if (attr.aspath == NULL)
+    {
+      printf ("aspath is NULL!\n");
+      failed++;
+    }
+  if (attr.aspath && strcmp (attr.aspath->str, t->shouldbe))
+    {
+      printf ("attr str and 'shouldbe' mismatched!\n"
+              "attr str:  %s\n"
+              "shouldbe:  %s\n",
+              attr.aspath->str, t->shouldbe);
+      failed++;
+    }
+
+out:
+  if (attr.aspath)
+    aspath_unintern (attr.aspath);
+  if (asp)
+    aspath_unintern (asp);
+  return failed - initfail;
+}
+
+static void
+attr_test (struct aspath_tests *t)
+{
+    printf ("%s\n", t->desc);
+    printf ("%s\n\n", handle_attr_test (t) ? FAILED : OK);  
+}
+
 int
 main (void)
 {
   int i = 0;
-  aspath_init();
+  bgp_master_init ();
+  master = bm->master;
+  bgp_attr_init ();
+  
   while (test_segments[i].name)
     {
+      printf ("test %u\n", i);
       parse_test (&test_segments[i]);
       empty_prepend_test (&test_segments[i++]);
     }
   
   i = 0;
   while (prepend_tests[i].test1)
-    prepend_test (&prepend_tests[i++]);
+    {
+      printf ("prepend test %u\n", i);
+      prepend_test (&prepend_tests[i++]);
+    }
   
   i = 0;
   while (aggregate_tests[i].test1)
-    aggregate_test (&aggregate_tests[i++]);
+    {
+      printf ("aggregate test %u\n", i);
+      aggregate_test (&aggregate_tests[i++]);
+    }
   
   i = 0;
   
   while (reconcile_tests[i].test1)
-    as4_reconcile_test (&reconcile_tests[i++]);
+    {
+      printf ("reconcile test %u\n", i);
+      as4_reconcile_test (&reconcile_tests[i++]);
+    }
   
   i = 0;
   
@@ -1026,6 +1298,14 @@
   
   empty_get_test();
   
+  i = 0;
+  
+  while (aspath_tests[i].desc)
+    {
+      printf ("aspath_attr test %d\n", i);
+      attr_test (&aspath_tests[i++]);
+    }
+  
   printf ("failures: %d\n", failed);
   printf ("aspath count: %ld\n", aspath_count());
   
diff --git a/tests/bgp_capability_test.c b/tests/bgp_capability_test.c
index 0dbf4fb..9b43159 100644
--- a/tests/bgp_capability_test.c
+++ b/tests/bgp_capability_test.c
@@ -96,10 +96,10 @@
   },
   /* 6 */
   { "MP3",
-    "MP IP6/VPNv4",
+    "MP IP6/MPLS-labeled VPN",
     { CAPABILITY_CODE_MP, 0x4, 0x0, 0x2, 0x0, 0x80 },
-    6, SHOULD_PARSE, 0, /* parses, but invalid afi,safi */
-    1, AFI_IP6, BGP_SAFI_VPNV4, INVALID_AFI,
+    6, SHOULD_PARSE, 0,
+    1, AFI_IP6, SAFI_MPLS_LABELED_VPN, VALID_AFI,
   },
   /* 7 */
   { "MP5",
@@ -110,21 +110,14 @@
   },
   /* 8 */
   { "MP6",
-    "MP IP4/VPNv4",
+    "MP IP4/MPLS-laveled VPN",
     { CAPABILITY_CODE_MP, 0x4, 0x0, 0x1, 0x0, 0x80 },
     6, SHOULD_PARSE, 0,
-    1, AFI_IP, BGP_SAFI_VPNV4, VALID_AFI,
+    1, AFI_IP, SAFI_MPLS_LABELED_VPN, VALID_AFI,
   },  
-  /* 9 */
-  { "MP7",
-    "MP IP4/VPNv6",
-    { CAPABILITY_CODE_MP, 0x4, 0x0, 0x1, 0x0, 0x81 },
-    6, SHOULD_PARSE, 0, /* parses, but invalid afi,safi tuple */
-    1, AFI_IP, BGP_SAFI_VPNV6, INVALID_AFI,
-  },
   /* 10 */
   { "MP8",
-    "MP unknown AFI",
+    "MP unknown AFI/SAFI",
     { CAPABILITY_CODE_MP, 0x4, 0x0, 0xa, 0x0, 0x81 },
     6, SHOULD_PARSE, 0,
     1, 0xa, 0x81, INVALID_AFI, /* parses, but unknown */
diff --git a/tests/bgp_mp_attr_test.c b/tests/bgp_mp_attr_test.c
index dde0df2..9cbe9f2 100644
--- a/tests/bgp_mp_attr_test.c
+++ b/tests/bgp_mp_attr_test.c
@@ -286,10 +286,10 @@
     SHOULD_ERR,
     AFI_IP, SAFI_UNICAST, VALID_AFI,
   },
-  { "IPv4-vpnv4",
-    "IPv4/VPNv4 MP Reach, RD, Nexthop, 3 NLRIs", 
+  { "IPv4-MLVPN",
+    "IPv4/MPLS-labeled VPN MP Reach, RD, Nexthop, 3 NLRIs", 
     {
-      /* AFI / SAFI */		0x0, AFI_IP, BGP_SAFI_VPNV4,
+      /* AFI / SAFI */		0x0, AFI_IP, SAFI_MPLS_LABELED_VPN,
       /* nexthop bytes */	12,
       /* RD */			0, 0, 1, 2,
                                 0, 0xff, 3, 4,
@@ -409,10 +409,10 @@
     SHOULD_ERR,
     AFI_IP, SAFI_UNICAST, VALID_AFI,
   },
-  { "IPv4-unreach-vpnv4",
-    "IPv4/VPNv4 MP Unreach, RD, 3 NLRIs", 
+  { "IPv4-unreach-MLVPN",
+    "IPv4/MPLS-labeled VPN MP Unreach, RD, 3 NLRIs", 
     {
-      /* AFI / SAFI */		0x0, AFI_IP, BGP_SAFI_VPNV4,
+      /* AFI / SAFI */		0x0, AFI_IP, SAFI_MPLS_LABELED_VPN,
       /* nexthop bytes */	12,
       /* RD */			0, 0, 1, 2,
                                 0, 0xff, 3, 4,
@@ -449,9 +449,9 @@
   printf ("%s: %s\n", t->name, t->desc);
 
   if (type == BGP_ATTR_MP_REACH_NLRI)
-    ret = bgp_mp_reach_parse (peer, t->len, &attr, &nlri);
+    ret = bgp_mp_reach_parse (peer, t->len, &attr, BGP_ATTR_FLAG_OPTIONAL, BGP_INPUT_PNT (peer), &nlri);
   else
-    ret = bgp_mp_unreach_parse (peer, t->len, &nlri);
+    ret = bgp_mp_unreach_parse (peer, t->len, BGP_ATTR_FLAG_OPTIONAL, BGP_INPUT_PNT (peer), &nlri);
 
   if (!ret)
     {
diff --git a/tests/heavy-wq.c b/tests/heavy-wq.c
index a2c609d..e5f688c 100644
--- a/tests/heavy-wq.c
+++ b/tests/heavy-wq.c
@@ -1,6 +1,4 @@
 /*
- * $Id$
- *
  * This file is part of Quagga.
  *
  * Quagga is free software; you can redistribute it and/or modify it
diff --git a/tools/.cvsignore b/tools/.cvsignore
deleted file mode 100644
index 73bcf19..0000000
--- a/tools/.cvsignore
+++ /dev/null
@@ -1,3 +0,0 @@
-.arch-inventory
-.arch-ids
-
diff --git a/tools/multiple-bgpd.sh b/tools/multiple-bgpd.sh
index 028ad69..20a92a9 100644
--- a/tools/multiple-bgpd.sh
+++ b/tools/multiple-bgpd.sh
@@ -20,18 +20,19 @@
 	if [ ! -e "$CONF" ] ; then
 		# This sets up a ring of bgpd peerings
 		NEXT=$(( ($H % ${NUM}) + 1 ))
-		PREV=$(( (($H + 3) % ${NUM}) + 1 ))
+		PREV=$(( (($H + $NUM - 2) % ${NUM}) + 1 ))
 		NEXTADDR="${PREFIX}${NEXT}"
 		NEXTAS=$((${ASBASE} + $NEXT))
 		PREVADDR="${PREFIX}${PREV}"
 		PREVAS=$((${ASBASE} + $PREV))
+		ASN=$((64560+${H}))
 		
 		# Edit config to suit.
 		cat > "$CONF" <<- EOF
 			password whatever
 			service advanced-vty
 			!
-			router bgp $((64560+${H}))
+			router bgp ${ASN}
 			 bgp router-id ${ADDR}
 			 network 10.${H}.1.0/24 pathlimit 1
 			 network 10.${H}.2.0/24 pathlimit 2
@@ -40,6 +41,7 @@
 			 neighbor default update-source ${ADDR}
 			 neighbor default capability orf prefix-list both
 			 neighbor default soft-reconfiguration inbound
+			 neighbor default route-map test out
 			 neighbor ${NEXTADDR} remote-as ${NEXTAS}
 			 neighbor ${NEXTADDR} peer-group default
 			 neighbor ${PREVADDR} remote-as ${PREVAS}
@@ -53,10 +55,16 @@
 			 neighbor default activate
 			 neighbor default capability orf prefix-list both
 			 neighbor default default-originate
+			 neighbor default route-map test out
 			 neighbor ${NEXTADDR} peer-group default
 			 neighbor ${PREVADDR} peer-group default
 			 exit-address-family
 			!
+			! bgpd still has problems with extcommunity rt/soo
+			route-map test permit 10
+			 set extcommunity rt ${ASN}:1
+			 set extcommunity soo ${ASN}:2
+			 set community ${ASN}:1
 			line vty
 			!
 			end
diff --git a/vtysh/.cvsignore b/vtysh/.cvsignore
deleted file mode 100644
index ed3f1f3..0000000
--- a/vtysh/.cvsignore
+++ /dev/null
@@ -1,13 +0,0 @@
-Makefile
-Makefile.in
-*.o
-vtysh
-tags
-TAGS
-.deps
-vtysh_cmd.c
-.nfs*
-extract.pl
-.libs
-.arch-inventory
-.arch-ids
diff --git a/vtysh/Makefile.am b/vtysh/Makefile.am
index 791d95d..0fd2f14 100644
--- a/vtysh/Makefile.am
+++ b/vtysh/Makefile.am
@@ -24,6 +24,7 @@
 vtysh_cmd_FILES = $(top_srcdir)/bgpd/*.c $(top_srcdir)/isisd/*.c \
 		  $(top_srcdir)/ospfd/*.c $(top_srcdir)/ospf6d/*.c \
 		  $(top_srcdir)/ripd/*.c $(top_srcdir)/ripngd/*.c \
+		  $(top_srcdir)/babeld/*.c \
 		  $(top_srcdir)/lib/keychain.c $(top_srcdir)/lib/routemap.c \
 		  $(top_srcdir)/lib/filter.c $(top_srcdir)/lib/plist.c \
 		  $(top_srcdir)/lib/distribute.c $(top_srcdir)/lib/if_rmap.c \
diff --git a/vtysh/extract.pl.in b/vtysh/extract.pl.in
index 0b9ee51..61b2d2a 100755
--- a/vtysh/extract.pl.in
+++ b/vtysh/extract.pl.in
@@ -37,6 +37,7 @@
 $ignore{'"router ospf"'} = "ignore";
 $ignore{'"router ospf <0-65535>"'} = "ignore";
 $ignore{'"router ospf6"'} = "ignore";
+$ignore{'"router babel"'} = "ignore";
 $ignore{'"router bgp " "<1-4294967295>"'} = "ignore";
 $ignore{'"router bgp " "<1-4294967295>" " view WORD"'} = "ignore";
 $ignore{'"router isis WORD"'} = "ignore";
@@ -62,13 +63,13 @@
 foreach (@ARGV) {
     $file = $_;
 
-    open (FH, "cpp -DHAVE_CONFIG_H -DVTYSH_EXTRACT_PL -DHAVE_IPV6 -I@top_builddir@ -I@srcdir@/ -I@srcdir@/.. -I@top_srcdir@/lib -I@top_srcdir@/isisd/topology @SNMP_INCLUDES@ $file |");
+    open (FH, "cpp -DHAVE_CONFIG_H -DVTYSH_EXTRACT_PL -DHAVE_IPV6 -I@top_builddir@ -I@srcdir@/ -I@srcdir@/.. -I@top_srcdir@/lib -I@top_srcdir@/isisd/topology @SNMP_INCLUDES@ @CPPFLAGS@ $file |");
     local $/; undef $/;
     $line = <FH>;
     close (FH);
 
     @defun = ($line =~ /(?:DEFUN|ALIAS)\s*\((.+?)\);?\s?\s?\n/sg);
-    @install = ($line =~ /install_element \(\s*[0-9A-Z_]+,\s*&[^;]*;\s*\n/sg);
+    @install = ($line =~ /install_element\s*\(\s*[0-9A-Z_]+,\s*&[^;]*;\s*\n/sg);
 
     # DEFUN process
     foreach (@defun) {
diff --git a/vtysh/vtysh.c b/vtysh/vtysh.c
index 3f189ad..431c08e 100644
--- a/vtysh/vtysh.c
+++ b/vtysh/vtysh.c
@@ -58,6 +58,7 @@
   { .fd = -1, .name = "ospf6d", .flag = VTYSH_OSPF6D, .path = OSPF6_VTYSH_PATH},
   { .fd = -1, .name = "bgpd", .flag = VTYSH_BGPD, .path = BGP_VTYSH_PATH},
   { .fd = -1, .name = "isisd", .flag = VTYSH_ISISD, .path = ISIS_VTYSH_PATH},
+  { .fd = -1, .name = "babeld", .flag = VTYSH_BABELD, .path = BABEL_VTYSH_PATH},
 };
 
 #define VTYSH_INDEX_MAX (sizeof(vtysh_client)/sizeof(vtysh_client[0]))
@@ -797,6 +798,12 @@
   "%s(config-ospf6)# "
 };
 
+static struct cmd_node babel_node =
+{
+  BABEL_NODE,
+  "%s(config-babel)# "
+};
+
 static struct cmd_node keychain_node =
 {
   KEYCHAIN_NODE,
@@ -1009,6 +1016,17 @@
   return CMD_SUCCESS;
 }
 
+DEFUNSH (VTYSH_BABELD,
+	 router_babel,
+	 router_babel_cmd,
+	 "router babel",
+	 ROUTER_STR
+	 "Babel")
+{
+  vty->node = BABEL_NODE;
+  return CMD_SUCCESS;
+}
+
 DEFUNSH (VTYSH_ISISD,
 	 router_isis,
 	 router_isis_cmd,
@@ -1097,6 +1115,7 @@
     case RIPNG_NODE:
     case OSPF_NODE:
     case OSPF6_NODE:
+    case BABEL_NODE:
     case ISIS_NODE:
     case MASC_NODE:
     case RMAP_NODE:
@@ -2252,6 +2271,7 @@
   install_node (&ripng_node, NULL);
   install_node (&ospf6_node, NULL);
 /* #endif */
+  install_node (&babel_node, NULL);
   install_node (&keychain_node, NULL);
   install_node (&keychain_key_node, NULL);
   install_node (&isis_node, NULL);
@@ -2273,6 +2293,7 @@
   vtysh_install_default (OSPF_NODE);
   vtysh_install_default (RIPNG_NODE);
   vtysh_install_default (OSPF6_NODE);
+  vtysh_install_default (BABEL_NODE);
   vtysh_install_default (ISIS_NODE);
   vtysh_install_default (KEYCHAIN_NODE);
   vtysh_install_default (KEYCHAIN_KEY_NODE);
@@ -2327,6 +2348,7 @@
   install_element (RIPNG_NODE, &vtysh_end_all_cmd);
   install_element (OSPF_NODE, &vtysh_end_all_cmd);
   install_element (OSPF6_NODE, &vtysh_end_all_cmd);
+  install_element (BABEL_NODE, &vtysh_end_all_cmd);
   install_element (BGP_NODE, &vtysh_end_all_cmd);
   install_element (BGP_IPV4_NODE, &vtysh_end_all_cmd);
   install_element (BGP_IPV4M_NODE, &vtysh_end_all_cmd);
@@ -2352,6 +2374,7 @@
 #ifdef HAVE_IPV6
   install_element (CONFIG_NODE, &router_ospf6_cmd);
 #endif
+  install_element (CONFIG_NODE, &router_babel_cmd);
   install_element (CONFIG_NODE, &router_isis_cmd);
   install_element (CONFIG_NODE, &router_bgp_cmd);
   install_element (CONFIG_NODE, &router_bgp_view_cmd);
diff --git a/vtysh/vtysh.h b/vtysh/vtysh.h
index e711d59..3cc7baf 100644
--- a/vtysh/vtysh.h
+++ b/vtysh/vtysh.h
@@ -29,9 +29,10 @@
 #define VTYSH_OSPF6D 0x10
 #define VTYSH_BGPD   0x20
 #define VTYSH_ISISD  0x40
-#define VTYSH_ALL	  VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_RIPNGD|VTYSH_OSPFD|VTYSH_OSPF6D|VTYSH_BGPD|VTYSH_ISISD
-#define VTYSH_RMAP	  VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_RIPNGD|VTYSH_OSPFD|VTYSH_OSPF6D|VTYSH_BGPD
-#define VTYSH_INTERFACE	  VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_RIPNGD|VTYSH_OSPFD|VTYSH_OSPF6D|VTYSH_ISISD
+#define VTYSH_BABELD  0x80
+#define VTYSH_ALL	  VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_RIPNGD|VTYSH_OSPFD|VTYSH_OSPF6D|VTYSH_BGPD|VTYSH_ISISD|VTYSH_BABELD
+#define VTYSH_RMAP	  VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_RIPNGD|VTYSH_OSPFD|VTYSH_OSPF6D|VTYSH_BGPD|VTYSH_BABELD
+#define VTYSH_INTERFACE	  VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_RIPNGD|VTYSH_OSPFD|VTYSH_OSPF6D|VTYSH_ISISD|VTYSH_BABELD
 
 /* vtysh local configuration file. */
 #define VTYSH_DEFAULT_CONFIG "vtysh.conf"
diff --git a/vtysh/vtysh_config.c b/vtysh/vtysh_config.c
index fb8a126..f9cb6a7 100644
--- a/vtysh/vtysh_config.c
+++ b/vtysh/vtysh_config.c
@@ -198,6 +198,8 @@
 	config = config_get (OSPF_NODE, line);
       else if (strncmp (line, "router ospf6", strlen ("router ospf6")) == 0)
 	config = config_get (OSPF6_NODE, line);
+      else if (strncmp (line, "router babel", strlen ("router babel")) == 0)
+	config = config_get (BABEL_NODE, line);
       else if (strncmp (line, "router bgp", strlen ("router bgp")) == 0)
 	config = config_get (BGP_NODE, line);
       else if (strncmp (line, "router isis", strlen ("router isis")) == 0)
diff --git a/watchquagga/.cvsignore b/watchquagga/.cvsignore
deleted file mode 100644
index c661510..0000000
--- a/watchquagga/.cvsignore
+++ /dev/null
@@ -1,13 +0,0 @@
-Makefile
-Makefile.in
-*.o
-watchquagga
-tags
-TAGS
-.deps
-.nfs*
-*.lo
-*.la
-*.libs
-.arch-inventory
-.arch-ids
diff --git a/watchquagga/watchquagga.c b/watchquagga/watchquagga.c
index fb628ac..b57fd3a 100644
--- a/watchquagga/watchquagga.c
+++ b/watchquagga/watchquagga.c
@@ -1,6 +1,4 @@
 /*
-    $Id$
-
     Monitor status of quagga daemons and restart if necessary.
 
     Copyright (C) 2004  Andrew J. Schorr
diff --git a/zebra/.cvsignore b/zebra/.cvsignore
deleted file mode 100644
index 59c5889..0000000
--- a/zebra/.cvsignore
+++ /dev/null
@@ -1,13 +0,0 @@
-Makefile
-Makefile.in
-*.o
-zebra
-zebra.conf
-client
-tags
-TAGS
-.deps
-.nfs*
-.libs
-.arch-inventory
-.arch-ids
diff --git a/zebra/client_main.c b/zebra/client_main.c
index ce01231..8b95907 100644
--- a/zebra/client_main.c
+++ b/zebra/client_main.c
@@ -115,6 +115,7 @@
   { "static", ZEBRA_ROUTE_STATIC },
   { "rip",    ZEBRA_ROUTE_RIP },
   { "ripng",  ZEBRA_ROUTE_RIPNG },
+  { "babel",  ZEBRA_ROUTE_BABEL },
   { "ospf",   ZEBRA_ROUTE_OSPF },
   { "ospf6",  ZEBRA_ROUTE_OSPF6 },
   { "bgp",    ZEBRA_ROUTE_BGP },
diff --git a/zebra/connected.c b/zebra/connected.c
index 95399fa..f699b14 100644
--- a/zebra/connected.c
+++ b/zebra/connected.c
@@ -189,7 +189,7 @@
     return;
 
   rib_add_ipv4 (ZEBRA_ROUTE_CONNECT, 0, &p, NULL, NULL, ifp->ifindex,
-	RT_TABLE_MAIN, ifp->metric, 0);
+	RT_TABLE_MAIN, ifp->metric, 0, SAFI_UNICAST);
 
   rib_update ();
 }
@@ -295,7 +295,7 @@
     return;
 
   /* Same logic as for connected_up_ipv4(): push the changes into the head. */
-  rib_delete_ipv4 (ZEBRA_ROUTE_CONNECT, 0, &p, NULL, ifp->ifindex, 0);
+  rib_delete_ipv4 (ZEBRA_ROUTE_CONNECT, 0, &p, NULL, ifp->ifindex, 0, SAFI_UNICAST);
 
   rib_update ();
 }
@@ -343,7 +343,7 @@
 #endif
 
   rib_add_ipv6 (ZEBRA_ROUTE_CONNECT, 0, &p, NULL, ifp->ifindex, RT_TABLE_MAIN,
-                ifp->metric, 0);
+                ifp->metric, 0, SAFI_UNICAST);
 
   rib_update ();
 }
@@ -417,7 +417,7 @@
   if (IN6_IS_ADDR_UNSPECIFIED (&p.prefix))
     return;
 
-  rib_delete_ipv6 (ZEBRA_ROUTE_CONNECT, 0, &p, NULL, ifp->ifindex, 0);
+  rib_delete_ipv6 (ZEBRA_ROUTE_CONNECT, 0, &p, NULL, ifp->ifindex, 0, SAFI_UNICAST);
 
   rib_update ();
 }
diff --git a/zebra/if_netlink.c b/zebra/if_netlink.c
index 701c81b..86bd8ff 100644
--- a/zebra/if_netlink.c
+++ b/zebra/if_netlink.c
@@ -22,6 +22,8 @@
 
 #include <zebra.h>
 
+#include "zebra/zserv.h"
+
 extern int interface_lookup_netlink (void);
 
 /* Interface information read by netlink. */
diff --git a/zebra/interface.c b/zebra/interface.c
index 03e7ff7..2242259 100644
--- a/zebra/interface.c
+++ b/zebra/interface.c
@@ -76,9 +76,9 @@
     rtadv->AdvReachableTime = 0;
     rtadv->AdvRetransTimer = 0;
     rtadv->AdvCurHopLimit = 0;
-    rtadv->AdvDefaultLifetime = RTADV_ADV_DEFAULT_LIFETIME;
+    rtadv->AdvDefaultLifetime = -1; /* derive from MaxRtrAdvInterval */
     rtadv->HomeAgentPreference = 0;
-    rtadv->HomeAgentLifetime = RTADV_ADV_DEFAULT_LIFETIME;
+    rtadv->HomeAgentLifetime = -1; /* derive from AdvDefaultLifetime */
     rtadv->AdvIntervalOption = 0;
     rtadv->DefaultPreference = RTADV_PREF_MEDIUM;
 
@@ -216,7 +216,7 @@
  * interface will affect only the primary interface/address on Solaris.
  ************************End Solaris flags hacks ***********************
  */
-static inline void
+static void
 if_flags_mangle (struct interface *ifp, uint64_t *newflags)
 {
 #ifdef SUNOS_5
@@ -630,8 +630,12 @@
         vty_out (vty, "  ND router advertisements are sent every "
 			"%d seconds%s", interval / 1000,
 		 VTY_NEWLINE);
-      vty_out (vty, "  ND router advertisements live for %d seconds%s",
-	       rtadv->AdvDefaultLifetime, VTY_NEWLINE);
+      if (rtadv->AdvDefaultLifetime != -1)
+	vty_out (vty, "  ND router advertisements live for %d seconds%s",
+		 rtadv->AdvDefaultLifetime, VTY_NEWLINE);
+      else
+	vty_out (vty, "  ND router advertisements lifetime tracks ra-interval%s",
+		 VTY_NEWLINE);
       vty_out (vty, "  ND router advertisement default router preference is "
 			"%s%s", rtadv_pref_strs[rtadv->DefaultPreference],
 		 VTY_NEWLINE);
@@ -642,9 +646,19 @@
 	vty_out (vty, "  Hosts use stateless autoconfig for addresses.%s",
 		 VTY_NEWLINE);
       if (rtadv->AdvHomeAgentFlag)
+      {
       	vty_out (vty, "  ND router advertisements with "
 				"Home Agent flag bit set.%s",
 		 VTY_NEWLINE);
+	if (rtadv->HomeAgentLifetime != -1)
+	  vty_out (vty, "  Home Agent lifetime is %u seconds%s",
+	           rtadv->HomeAgentLifetime, VTY_NEWLINE);
+	else
+	  vty_out (vty, "  Home Agent lifetime tracks ra-lifetime%s",
+	           VTY_NEWLINE);
+	vty_out (vty, "  Home Agent preference is %u%s",
+	         rtadv->HomeAgentPreference, VTY_NEWLINE);
+      }
       if (rtadv->AdvIntervalOption)
       	vty_out (vty, "  ND router advertisements with Adv. Interval option.%s",
 		 VTY_NEWLINE);
diff --git a/zebra/interface.h b/zebra/interface.h
index 0cf6640..0777a2f 100644
--- a/zebra/interface.h
+++ b/zebra/interface.h
@@ -46,7 +46,7 @@
 #endif
 
 #ifdef RTADV
-/* Router advertisement parameter.  From RFC2461, RFC3775 and RFC4191. */
+/* Router advertisement parameter.  From RFC4861, RFC6275 and RFC4191. */
 struct rtadvconf
 {
   /* A flag indicating whether or not the router sends periodic Router
@@ -56,8 +56,8 @@
 
   /* The maximum time allowed between sending unsolicited multicast
      Router Advertisements from the interface, in milliseconds.
-     MUST be no less than 70 ms (RFC3775, section 7.4) and no greater 
-     than 1800000 ms (See RFC2461).
+     MUST be no less than 70 ms [RFC6275 7.5] and no greater
+     than 1800000 ms [RFC4861 6.2.1].
 
      Default: 600000 milliseconds */
   int MaxRtrAdvInterval;
@@ -65,11 +65,11 @@
 
   /* The minimum time allowed between sending unsolicited multicast
      Router Advertisements from the interface, in milliseconds.
-     MUST be no less than 30 ms (See RFC3775, section 7.4). 
+     MUST be no less than 30 ms [RFC6275 7.5].
      MUST be no greater than .75 * MaxRtrAdvInterval.
 
      Default: 0.33 * MaxRtrAdvInterval */
-  int MinRtrAdvInterval;
+  int MinRtrAdvInterval; /* This field is currently unused. */
 #define RTADV_MIN_RTR_ADV_INTERVAL (0.33 * RTADV_MAX_RTR_ADV_INTERVAL)
 
   /* Unsolicited Router Advertisements' interval timer. */
@@ -131,8 +131,7 @@
 
      Default: 3 * MaxRtrAdvInterval */
   int AdvDefaultLifetime;
-#define RTADV_ADV_DEFAULT_LIFETIME (3 * RTADV_MAX_RTR_ADV_INTERVAL)
-
+#define RTADV_MAX_RTRLIFETIME 9000 /* 2.5 hours */
 
   /* A list of prefixes to be placed in Prefix Information options in
      Router Advertisement messages sent from the interface.
@@ -144,7 +143,7 @@
   struct list *AdvPrefixList;
 
   /* The TRUE/FALSE value to be placed in the "Home agent"
-     flag field in the Router Advertisement.  See [RFC3775 7.1].
+     flag field in the Router Advertisement.  See [RFC6275 7.1].
 
      Default: FALSE */
   int AdvHomeAgentFlag;
@@ -167,7 +166,7 @@
 #define RTADV_MAX_HALIFETIME 65520 /* 18.2 hours */
 
   /* The TRUE/FALSE value to insert or not an Advertisement Interval
-     option. See [RFC 3775 7.3]
+     option. See [RFC 6275 7.3]
 
      Default: FALSE */
   int AdvIntervalOption;
diff --git a/zebra/ipforward_proc.c b/zebra/ipforward_proc.c
index 7384613..2876ede 100644
--- a/zebra/ipforward_proc.c
+++ b/zebra/ipforward_proc.c
@@ -45,7 +45,6 @@
 {
   FILE *fp;
   int ipforwarding = 0;
-  char *pnt;
   char buf[10];
 
   fp = fopen (proc_net_snmp, "r");
@@ -59,8 +58,8 @@
   /* Get ip_statistics.IpForwarding : 
      1 => ip forwarding enabled 
      2 => ip forwarding off. */
-  pnt = fgets (buf, 6, fp);
-  sscanf (buf, "Ip: %d", &ipforwarding);
+  if (fgets (buf, 6, fp))
+    sscanf (buf, "Ip: %d", &ipforwarding);
 
   fclose(fp);
   
@@ -140,8 +139,8 @@
   if (fp == NULL)
     return -1;
 
-  fgets (buf, 2, fp);
-  sscanf (buf, "%d", &ipforwarding);
+  if (fgets (buf, 2, fp))
+    sscanf (buf, "%d", &ipforwarding);
 
   fclose (fp);
   return ipforwarding;
diff --git a/zebra/irdp_interface.c b/zebra/irdp_interface.c
index 8742b62..6403830 100644
--- a/zebra/irdp_interface.c
+++ b/zebra/irdp_interface.c
@@ -99,14 +99,11 @@
 	  u_int32_t group, 
 	  int add_leave)
 {
-  struct zebra_if *zi;
   struct ip_mreq m;
   struct prefix *p;
   int ret;
   char b1[INET_ADDRSTRLEN];
 
-  zi = ifp->info;
-
   memset (&m, 0, sizeof (m));
   m.imr_multiaddr.s_addr = htonl (group);
   p = irdp_get_prefix(ifp);
diff --git a/zebra/kernel_socket.c b/zebra/kernel_socket.c
index feeaf5d..b7061e7 100644
--- a/zebra/kernel_socket.c
+++ b/zebra/kernel_socket.c
@@ -319,6 +319,7 @@
 ifm_read (struct if_msghdr *ifm)
 {
   struct interface *ifp = NULL;
+  struct sockaddr_dl *sdl;
   char ifname[IFNAMSIZ];
   short ifnlen = 0;
   caddr_t *cp;
@@ -356,6 +357,7 @@
   RTA_ADDR_GET (NULL, RTA_GATEWAY, ifm->ifm_addrs, cp);
   RTA_ATTR_GET (NULL, RTA_NETMASK, ifm->ifm_addrs, cp);
   RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifm_addrs, cp);
+  sdl = (struct sockaddr_dl *)cp;
   RTA_NAME_GET (ifname, RTA_IFP, ifm->ifm_addrs, cp, ifnlen);
   RTA_ADDR_GET (NULL, RTA_IFA, ifm->ifm_addrs, cp);
   RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifm_addrs, cp);
@@ -454,6 +456,16 @@
 #endif /* __bsdi__ */
       if_get_metric (ifp);
 
+      /*
+       * XXX sockaddr_dl contents can be larger than the structure
+       * definition, so the user of the stored structure must be
+       * careful not to read off the end.
+       *
+       * a nonzero ifnlen from RTA_NAME_GET() means sdl is valid
+       */
+      if (ifnlen)
+	memcpy (&ifp->sdl, sdl, sizeof (struct sockaddr_dl));
+
       if_add_update (ifp);
     }
   else
@@ -606,7 +618,7 @@
 
   /* Assert read up end point matches to end point */
   if (pnt != end)
-    zlog_warn ("ifam_read() does't read all socket data");
+    zlog_warn ("ifam_read() doesn't read all socket data");
 }
 
 /* Interface's address information get. */
@@ -754,7 +766,7 @@
 
   /* Assert read up to the end of pointer. */
   if (pnt != end) 
-      zlog (NULL, LOG_WARNING, "rtm_read() does't read all socket data.");
+      zlog (NULL, LOG_WARNING, "rtm_read() doesn't read all socket data.");
 
   return rtm->rtm_flags;
 }
@@ -894,16 +906,16 @@
        */
       if (rtm->rtm_type == RTM_CHANGE)
         rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
-                         NULL, 0, 0);
+                         NULL, 0, 0, SAFI_UNICAST);
       
       if (rtm->rtm_type == RTM_GET 
           || rtm->rtm_type == RTM_ADD
           || rtm->rtm_type == RTM_CHANGE)
 	rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, 
-		      &p, &gate.sin.sin_addr, NULL, 0, 0, 0, 0);
+		      &p, &gate.sin.sin_addr, NULL, 0, 0, 0, 0, SAFI_UNICAST);
       else
 	rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, 
-		      &p, &gate.sin.sin_addr, 0, 0);
+		      &p, &gate.sin.sin_addr, 0, 0, SAFI_UNICAST);
     }
 #ifdef HAVE_IPV6
   if (dest.sa.sa_family == AF_INET6)
@@ -936,16 +948,16 @@
        */
       if (rtm->rtm_type == RTM_CHANGE)
         rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
-                         NULL, 0, 0);
+                         NULL, 0, 0, SAFI_UNICAST);
       
       if (rtm->rtm_type == RTM_GET 
           || rtm->rtm_type == RTM_ADD
           || rtm->rtm_type == RTM_CHANGE)
 	rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
-		      &p, &gate.sin6.sin6_addr, ifindex, 0, 0, 0);
+		      &p, &gate.sin6.sin6_addr, ifindex, 0, 0, 0, SAFI_UNICAST);
       else
 	rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
-			 &p, &gate.sin6.sin6_addr, ifindex, 0);
+			 &p, &gate.sin6.sin6_addr, ifindex, 0, SAFI_UNICAST);
     }
 #endif /* HAVE_IPV6 */
 }
diff --git a/zebra/main.c b/zebra/main.c
index d829c04..5f26ce2 100644
--- a/zebra/main.c
+++ b/zebra/main.c
@@ -71,6 +71,7 @@
   { "keep_kernel", no_argument,       NULL, 'k'},
   { "config_file", required_argument, NULL, 'f'},
   { "pid_file",    required_argument, NULL, 'i'},
+  { "socket",      required_argument, NULL, 'z'},
   { "help",        no_argument,       NULL, 'h'},
   { "vty_addr",    required_argument, NULL, 'A'},
   { "vty_port",    required_argument, NULL, 'P'},
@@ -128,6 +129,7 @@
 	      "-d, --daemon       Runs in daemon mode\n"\
 	      "-f, --config_file  Set configuration file name\n"\
 	      "-i, --pid_file     Set process identifier file name\n"\
+	      "-z, --socket       Set path of zebra socket\n"\
 	      "-k, --keep_kernel  Don't delete old routes which installed by "\
 				  "zebra.\n"\
 	      "-C, --dryrun       Check configuration for validity and exit\n"\
@@ -214,6 +216,7 @@
   char *config_file = NULL;
   char *progname;
   struct thread thread;
+  char *zserv_path = NULL;
 
   /* Set umask before anything for security */
   umask (0027);
@@ -229,9 +232,9 @@
       int opt;
   
 #ifdef HAVE_NETLINK  
-      opt = getopt_long (argc, argv, "bdkf:i:hA:P:ru:g:vs:C", longopts, 0);
+      opt = getopt_long (argc, argv, "bdkf:i:z:hA:P:ru:g:vs:C", longopts, 0);
 #else
-      opt = getopt_long (argc, argv, "bdkf:i:hA:P:ru:g:vC", longopts, 0);
+      opt = getopt_long (argc, argv, "bdkf:i:z:hA:P:ru:g:vC", longopts, 0);
 #endif /* HAVE_NETLINK */
 
       if (opt == EOF)
@@ -261,6 +264,9 @@
         case 'i':
           pid_file = optarg;
           break;
+	case 'z':
+	  zserv_path = optarg;
+	  break;
 	case 'P':
 	  /* Deal with atoi() returning 0 on failure, and zebra not
 	     listening on zebra port... */
@@ -386,7 +392,7 @@
   pid = getpid ();
 
   /* This must be done only after locking pidfile (bug #403). */
-  zebra_zserv_socket_init ();
+  zebra_zserv_socket_init (zserv_path);
 
   /* Make vty server socket. */
   vty_serv_sock (vty_addr, vty_port, ZEBRA_VTYSH_PATH);
diff --git a/zebra/redistribute.c b/zebra/redistribute.c
index a8107ae..4276f1d 100644
--- a/zebra/redistribute.c
+++ b/zebra/redistribute.c
@@ -245,26 +245,15 @@
 
   type = stream_getc (client->ibuf);
 
-  switch (type)
+  if (type == 0 || type >= ZEBRA_ROUTE_MAX)
+    return;
+
+  if (! client->redist[type])
     {
-    case ZEBRA_ROUTE_KERNEL:
-    case ZEBRA_ROUTE_CONNECT:
-    case ZEBRA_ROUTE_STATIC:
-    case ZEBRA_ROUTE_RIP:
-    case ZEBRA_ROUTE_RIPNG:
-    case ZEBRA_ROUTE_OSPF:
-    case ZEBRA_ROUTE_OSPF6:
-    case ZEBRA_ROUTE_BGP:
-      if (! client->redist[type])
-	{
-	  client->redist[type] = 1;
-	  zebra_redistribute (client, type);
-	}
-      break;
-    default:
-      break;
+      client->redist[type] = 1;
+      zebra_redistribute (client, type);
     }
-}     
+}
 
 void
 zebra_redistribute_delete (int command, struct zserv *client, int length)
@@ -273,22 +262,11 @@
 
   type = stream_getc (client->ibuf);
 
-  switch (type)
-    {
-    case ZEBRA_ROUTE_KERNEL:
-    case ZEBRA_ROUTE_CONNECT:
-    case ZEBRA_ROUTE_STATIC:
-    case ZEBRA_ROUTE_RIP:
-    case ZEBRA_ROUTE_RIPNG:
-    case ZEBRA_ROUTE_OSPF:
-    case ZEBRA_ROUTE_OSPF6:
-    case ZEBRA_ROUTE_BGP:
-      client->redist[type] = 0;
-      break;
-    default:
-      break;
-    }
-}     
+  if (type == 0 || type >= ZEBRA_ROUTE_MAX)
+    return;
+
+  client->redist[type] = 0;
+}
 
 void
 zebra_redistribute_default_add (int command, struct zserv *client, int length)
diff --git a/zebra/rib.h b/zebra/rib.h
index b5c9e05..1b85c81 100644
--- a/zebra/rib.h
+++ b/zebra/rib.h
@@ -253,13 +253,13 @@
 extern int rib_add_ipv4 (int type, int flags, struct prefix_ipv4 *p, 
 			 struct in_addr *gate, struct in_addr *src,
 			 unsigned int ifindex, u_int32_t vrf_id,
-			 u_int32_t, u_char);
+			 u_int32_t, u_char, safi_t);
 
-extern int rib_add_ipv4_multipath (struct prefix_ipv4 *, struct rib *);
+extern int rib_add_ipv4_multipath (struct prefix_ipv4 *, struct rib *, safi_t);
 
 extern int rib_delete_ipv4 (int type, int flags, struct prefix_ipv4 *p,
 		            struct in_addr *gate, unsigned int ifindex, 
-		            u_int32_t);
+		            u_int32_t, safi_t safi);
 
 extern struct rib *rib_match_ipv4 (struct in_addr);
 
@@ -270,6 +270,7 @@
 extern void rib_sweep_route (void);
 extern void rib_close (void);
 extern void rib_init (void);
+extern unsigned long rib_score_proto (u_char proto);
 
 extern int
 static_add_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
@@ -283,11 +284,11 @@
 extern int
 rib_add_ipv6 (int type, int flags, struct prefix_ipv6 *p,
 	      struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id,
-	      u_int32_t metric, u_char distance);
+	      u_int32_t metric, u_char distance, safi_t safi);
 
 extern int
 rib_delete_ipv6 (int type, int flags, struct prefix_ipv6 *p,
-		 struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id);
+		 struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id, safi_t safi);
 
 extern struct rib *rib_lookup_ipv6 (struct in6_addr *);
 
diff --git a/zebra/rt_netlink.c b/zebra/rt_netlink.c
index 2cde50a..73097bf 100644
--- a/zebra/rt_netlink.c
+++ b/zebra/rt_netlink.c
@@ -732,7 +732,7 @@
 
       if (!tb[RTA_MULTIPATH])
           rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, flags, &p, gate, src, index,
-                        table, metric, 0);
+                        table, metric, 0, SAFI_UNICAST);
       else
         {
           /* This is a multipath route */
@@ -786,7 +786,7 @@
           if (rib->nexthop_num == 0)
             XFREE (MTYPE_RIB, rib);
           else
-            rib_add_ipv4_multipath (&p, rib);
+            rib_add_ipv4_multipath (&p, rib, SAFI_UNICAST);
         }
     }
 #ifdef HAVE_IPV6
@@ -798,7 +798,7 @@
       p.prefixlen = rtm->rtm_dst_len;
 
       rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, flags, &p, gate, index, table,
-		    metric, 0);
+		    metric, 0, SAFI_UNICAST);
     }
 #endif /* HAVE_IPV6 */
 
@@ -833,6 +833,7 @@
   int index;
   int table;
   int metric;
+
   void *dest;
   void *gate;
   void *src;
@@ -909,7 +910,7 @@
   if (tb[RTA_PREFSRC])
     src = RTA_DATA (tb[RTA_PREFSRC]);
 
-  if (tb[RTA_PRIORITY])
+  if (h->nlmsg_type == RTM_NEWROUTE && tb[RTA_PRIORITY])
     metric = *(int *) RTA_DATA(tb[RTA_PRIORITY]);
 
   if (rtm->rtm_family == AF_INET)
@@ -933,7 +934,7 @@
         {
           if (!tb[RTA_MULTIPATH])
             rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, src, index, table,
-                          metric, 0);
+                          metric, 0, SAFI_UNICAST);
           else
             {
               /* This is a multipath route */
@@ -987,11 +988,11 @@
               if (rib->nexthop_num == 0)
                 XFREE (MTYPE_RIB, rib);
               else
-                rib_add_ipv4_multipath (&p, rib);
+                rib_add_ipv4_multipath (&p, rib, SAFI_UNICAST);
             }
         }
       else
-        rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, table);
+        rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, table, SAFI_UNICAST);
     }
 
 #ifdef HAVE_IPV6
@@ -1017,9 +1018,9 @@
         }
 
       if (h->nlmsg_type == RTM_NEWROUTE)
-        rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, table, 0, 0);
+        rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, table, metric, 0, SAFI_UNICAST);
       else
-        rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, table);
+        rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, table, SAFI_UNICAST);
     }
 #endif /* HAVE_IPV6 */
 
@@ -1953,11 +1954,7 @@
 static int
 kernel_read (struct thread *thread)
 {
-  int ret;
-  int sock;
-
-  sock = THREAD_FD (thread);
-  ret = netlink_parse_info (netlink_information_fetch, &netlink);
+  netlink_parse_info (netlink_information_fetch, &netlink);
   thread_add_read (zebrad.master, kernel_read, NULL, netlink.sock);
 
   return 0;
diff --git a/zebra/rtadv.c b/zebra/rtadv.c
index 8cc3c4c..ae5c5a1 100644
--- a/zebra/rtadv.c
+++ b/zebra/rtadv.c
@@ -163,6 +163,7 @@
   struct rtadv_prefix *rprefix;
   u_char all_nodes_addr[] = {0xff,0x02,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
   struct listnode *node;
+  u_int16_t pkt_RouterLifetime;
 
   /*
    * Allocate control message bufffer.  This is dynamic because
@@ -190,7 +191,7 @@
   addr.sin6_len = sizeof (struct sockaddr_in6);
 #endif /* SIN6_LEN */
   addr.sin6_port = htons (IPPROTO_ICMPV6);
-  memcpy (&addr.sin6_addr, all_nodes_addr, sizeof (struct in6_addr));
+  IPV6_ADDR_COPY (&addr.sin6_addr, all_nodes_addr);
 
   /* Fetch interface information. */
   zif = ifp->info;
@@ -215,13 +216,32 @@
     rtadv->nd_ra_flags_reserved |= ND_RA_FLAG_OTHER;
   if (zif->rtadv.AdvHomeAgentFlag)
     rtadv->nd_ra_flags_reserved |= ND_RA_FLAG_HOME_AGENT;
-  rtadv->nd_ra_router_lifetime = htons (zif->rtadv.AdvDefaultLifetime);
+  /* Note that according to Neighbor Discovery (RFC 4861 [18]),
+   * AdvDefaultLifetime is by default based on the value of
+   * MaxRtrAdvInterval.  AdvDefaultLifetime is used in the Router Lifetime
+   * field of Router Advertisements.  Given that this field is expressed
+   * in seconds, a small MaxRtrAdvInterval value can result in a zero
+   * value for this field.  To prevent this, routers SHOULD keep
+   * AdvDefaultLifetime in at least one second, even if the use of
+   * MaxRtrAdvInterval would result in a smaller value. -- RFC6275, 7.5 */
+  pkt_RouterLifetime = zif->rtadv.AdvDefaultLifetime != -1 ?
+    zif->rtadv.AdvDefaultLifetime :
+    MAX (1, 0.003 * zif->rtadv.MaxRtrAdvInterval);
+  rtadv->nd_ra_router_lifetime = htons (pkt_RouterLifetime);
   rtadv->nd_ra_reachable = htonl (zif->rtadv.AdvReachableTime);
   rtadv->nd_ra_retransmit = htonl (0);
 
   len = sizeof (struct nd_router_advert);
 
-  if (zif->rtadv.AdvHomeAgentFlag)
+  /* If both the Home Agent Preference and Home Agent Lifetime are set to
+   * their default values specified above, this option SHOULD NOT be
+   * included in the Router Advertisement messages sent by this home
+   * agent. -- RFC6275, 7.4 */
+  if
+  (
+    zif->rtadv.AdvHomeAgentFlag &&
+    (zif->rtadv.HomeAgentPreference || zif->rtadv.HomeAgentLifetime != -1)
+  )
     {
       struct nd_opt_homeagent_info *ndopt_hai = 
 	(struct nd_opt_homeagent_info *)(buf + len);
@@ -229,7 +249,17 @@
       ndopt_hai->nd_opt_hai_len = 1;
       ndopt_hai->nd_opt_hai_reserved = 0;
       ndopt_hai->nd_opt_hai_preference = htons(zif->rtadv.HomeAgentPreference);
-      ndopt_hai->nd_opt_hai_lifetime = htons(zif->rtadv.HomeAgentLifetime);
+      /* 16-bit unsigned integer.  The lifetime associated with the home
+       * agent in units of seconds.  The default value is the same as the
+       * Router Lifetime, as specified in the main body of the Router
+       * Advertisement.  The maximum value corresponds to 18.2 hours.  A
+       * value of 0 MUST NOT be used. -- RFC6275, 7.5 */
+      ndopt_hai->nd_opt_hai_lifetime = htons
+      (
+        zif->rtadv.HomeAgentLifetime != -1 ?
+        zif->rtadv.HomeAgentLifetime :
+        MAX (1, pkt_RouterLifetime) /* 0 is OK for RL, but not for HAL*/
+      );
       len += sizeof(struct nd_opt_homeagent_info);
     }
 
@@ -267,8 +297,7 @@
       pinfo->nd_opt_pi_preferred_time = htonl (rprefix->AdvPreferredLifetime);
       pinfo->nd_opt_pi_reserved2 = 0;
 
-      memcpy (&pinfo->nd_opt_pi_prefix, &rprefix->prefix.u.prefix6,
-	      sizeof (struct in6_addr));
+      IPV6_ADDR_COPY (&pinfo->nd_opt_pi_prefix, &rprefix->prefix.prefix);
 
 #ifdef DEBUG
       {
@@ -319,6 +348,17 @@
     }
 #endif /* HAVE_STRUCT_SOCKADDR_DL */
 
+  /* MTU */
+  if (zif->rtadv.AdvLinkMTU)
+    {
+      struct nd_opt_mtu * opt = (struct nd_opt_mtu *) (buf + len);
+      opt->nd_opt_mtu_type = ND_OPT_MTU;
+      opt->nd_opt_mtu_len = 1;
+      opt->nd_opt_mtu_reserved = 0;
+      opt->nd_opt_mtu_mtu = htonl (zif->rtadv.AdvLinkMTU);
+      len += sizeof (struct nd_opt_mtu);
+    }
+
   msg.msg_name = (void *) &addr;
   msg.msg_namelen = sizeof (struct sockaddr_in6);
   msg.msg_iov = &iov;
@@ -368,7 +408,7 @@
 
   for (ALL_LIST_ELEMENTS (iflist, node, nnode, ifp))
     {
-      if (if_is_loopback (ifp))
+      if (if_is_loopback (ifp) || ! if_is_operative (ifp))
 	continue;
 
       zif = ifp->info;
@@ -378,6 +418,8 @@
 	  zif->rtadv.AdvIntervalTimer -= period;
 	  if (zif->rtadv.AdvIntervalTimer <= 0)
 	    {
+	      /* FIXME: using MaxRtrAdvInterval each time isn't what section
+	         6.2.4 of RFC4861 tells to do. */
 	      zif->rtadv.AdvIntervalTimer = zif->rtadv.MaxRtrAdvInterval;
 	      rtadv_send_packet (rtadv->sock, ifp);
 	    }
@@ -552,19 +594,19 @@
 }
 
 static struct rtadv_prefix *
-rtadv_prefix_lookup (struct list *rplist, struct prefix *p)
+rtadv_prefix_lookup (struct list *rplist, struct prefix_ipv6 *p)
 {
   struct listnode *node;
   struct rtadv_prefix *rprefix;
 
   for (ALL_LIST_ELEMENTS_RO (rplist, node, rprefix))
-    if (prefix_same (&rprefix->prefix, p))
+    if (prefix_same ((struct prefix *) &rprefix->prefix, (struct prefix *) p))
       return rprefix;
   return NULL;
 }
 
 static struct rtadv_prefix *
-rtadv_prefix_get (struct list *rplist, struct prefix *p)
+rtadv_prefix_get (struct list *rplist, struct prefix_ipv6 *p)
 {
   struct rtadv_prefix *rprefix;
   
@@ -573,7 +615,7 @@
     return rprefix;
 
   rprefix = rtadv_prefix_new ();
-  memcpy (&rprefix->prefix, p, sizeof (struct prefix));
+  memcpy (&rprefix->prefix, p, sizeof (struct prefix_ipv6));
   listnode_add (rplist, rprefix);
 
   return rprefix;
@@ -681,26 +723,22 @@
 
 DEFUN (ipv6_nd_ra_interval_msec,
        ipv6_nd_ra_interval_msec_cmd,
-       "ipv6 nd ra-interval msec MILLISECONDS",
+       "ipv6 nd ra-interval msec <70-1800000>",
        "Interface IPv6 config commands\n"
        "Neighbor discovery\n"
        "Router Advertisement interval\n"
        "Router Advertisement interval in milliseconds\n")
 {
-  int interval;
-  struct interface *ifp;
-  struct zebra_if *zif;
+  unsigned interval;
+  struct interface *ifp = (struct interface *) vty->index;
+  struct zebra_if *zif = ifp->info;
 
-  ifp = (struct interface *) vty->index;
-  zif = ifp->info;
-
-  interval = atoi (argv[0]);
-
-  if (interval <= 0)
-    {
-      vty_out (vty, "Invalid Router Advertisement Interval%s", VTY_NEWLINE);
-      return CMD_WARNING;
-    }
+  VTY_GET_INTEGER_RANGE ("router advertisement interval", interval, argv[0], 70, 1800000);
+  if ((zif->rtadv.AdvDefaultLifetime != -1 && interval > (unsigned)zif->rtadv.AdvDefaultLifetime * 1000))
+  {
+    vty_out (vty, "This ra-interval would conflict with configured ra-lifetime!%s", VTY_NEWLINE);
+    return CMD_WARNING;
+  }
 
   if (zif->rtadv.MaxRtrAdvInterval % 1000)
     rtadv->adv_msec_if_count--;
@@ -717,26 +755,22 @@
 
 DEFUN (ipv6_nd_ra_interval,
        ipv6_nd_ra_interval_cmd,
-       "ipv6 nd ra-interval SECONDS",
+       "ipv6 nd ra-interval <1-1800>",
        "Interface IPv6 config commands\n"
        "Neighbor discovery\n"
        "Router Advertisement interval\n"
        "Router Advertisement interval in seconds\n")
 {
-  int interval;
-  struct interface *ifp;
-  struct zebra_if *zif;
+  unsigned interval;
+  struct interface *ifp = (struct interface *) vty->index;
+  struct zebra_if *zif = ifp->info;
 
-  ifp = (struct interface *) vty->index;
-  zif = ifp->info;
-
-  interval = atoi (argv[0]);
-
-  if (interval <= 0)
-    {
-      vty_out (vty, "Invalid Router Advertisement Interval%s", VTY_NEWLINE);
-      return CMD_WARNING;
-    }
+  VTY_GET_INTEGER_RANGE ("router advertisement interval", interval, argv[0], 1, 1800);
+  if ((zif->rtadv.AdvDefaultLifetime != -1 && interval > (unsigned)zif->rtadv.AdvDefaultLifetime))
+  {
+    vty_out (vty, "This ra-interval would conflict with configured ra-lifetime!%s", VTY_NEWLINE);
+    return CMD_WARNING;
+  }
 
   if (zif->rtadv.MaxRtrAdvInterval % 1000)
     rtadv->adv_msec_if_count--;
@@ -775,13 +809,30 @@
   return CMD_SUCCESS;
 }
 
+ALIAS (no_ipv6_nd_ra_interval,
+       no_ipv6_nd_ra_interval_val_cmd,
+       "no ipv6 nd ra-interval <1-1800>",
+       NO_STR
+       "Interface IPv6 config commands\n"
+       "Neighbor discovery\n"
+       "Router Advertisement interval\n")
+
+ALIAS (no_ipv6_nd_ra_interval,
+       no_ipv6_nd_ra_interval_msec_val_cmd,
+       "no ipv6 nd ra-interval msec <1-1800000>",
+       NO_STR
+       "Interface IPv6 config commands\n"
+       "Neighbor discovery\n"
+       "Router Advertisement interval\n"
+       "Router Advertisement interval in milliseconds\n")
+
 DEFUN (ipv6_nd_ra_lifetime,
        ipv6_nd_ra_lifetime_cmd,
-       "ipv6 nd ra-lifetime SECONDS",
+       "ipv6 nd ra-lifetime <0-9000>",
        "Interface IPv6 config commands\n"
        "Neighbor discovery\n"
        "Router lifetime\n"
-       "Router lifetime in seconds\n")
+       "Router lifetime in seconds (0 stands for a non-default gw)\n")
 {
   int lifetime;
   struct interface *ifp;
@@ -790,11 +841,15 @@
   ifp = (struct interface *) vty->index;
   zif = ifp->info;
 
-  lifetime = atoi (argv[0]);
+  VTY_GET_INTEGER_RANGE ("router lifetime", lifetime, argv[0], 0, 9000);
 
-  if (lifetime < 0 || lifetime > 0xffff)
+  /* The value to be placed in the Router Lifetime field
+   * of Router Advertisements sent from the interface,
+   * in seconds.  MUST be either zero or between
+   * MaxRtrAdvInterval and 9000 seconds. -- RFC4861, 6.2.1 */
+  if ((lifetime != 0 && lifetime * 1000 < zif->rtadv.MaxRtrAdvInterval))
     {
-      vty_out (vty, "Invalid Router Lifetime%s", VTY_NEWLINE);
+      vty_out (vty, "This ra-lifetime would conflict with configured ra-interval%s", VTY_NEWLINE);
       return CMD_WARNING;
     }
 
@@ -817,36 +872,31 @@
   ifp = (struct interface *) vty->index;
   zif = ifp->info;
 
-  zif->rtadv.AdvDefaultLifetime = RTADV_ADV_DEFAULT_LIFETIME;
+  zif->rtadv.AdvDefaultLifetime = -1;
 
   return CMD_SUCCESS;
 }
 
+ALIAS (no_ipv6_nd_ra_lifetime,
+       no_ipv6_nd_ra_lifetime_val_cmd,
+       "no ipv6 nd ra-lifetime <0-9000>",
+       NO_STR
+       "Interface IPv6 config commands\n"
+       "Neighbor discovery\n"
+       "Router lifetime\n"
+       "Router lifetime in seconds (0 stands for a non-default gw)\n")
+
 DEFUN (ipv6_nd_reachable_time,
        ipv6_nd_reachable_time_cmd,
-       "ipv6 nd reachable-time MILLISECONDS",
+       "ipv6 nd reachable-time <1-3600000>",
        "Interface IPv6 config commands\n"
        "Neighbor discovery\n"
        "Reachable time\n"
        "Reachable time in milliseconds\n")
 {
-  u_int32_t rtime;
-  struct interface *ifp;
-  struct zebra_if *zif;
-
-  ifp = (struct interface *) vty->index;
-  zif = ifp->info;
-
-  rtime = (u_int32_t) atol (argv[0]);
-
-  if (rtime > RTADV_MAX_REACHABLE_TIME)
-    {
-      vty_out (vty, "Invalid Reachable time%s", VTY_NEWLINE);
-      return CMD_WARNING;
-    }
-
-  zif->rtadv.AdvReachableTime = rtime;
-
+  struct interface *ifp = (struct interface *) vty->index;
+  struct zebra_if *zif = ifp->info;
+  VTY_GET_INTEGER_RANGE ("reachable time", zif->rtadv.AdvReachableTime, argv[0], 1, RTADV_MAX_REACHABLE_TIME);
   return CMD_SUCCESS;
 }
 
@@ -869,31 +919,26 @@
   return CMD_SUCCESS;
 }
 
+ALIAS (no_ipv6_nd_reachable_time,
+       no_ipv6_nd_reachable_time_val_cmd,
+       "no ipv6 nd reachable-time <1-3600000>",
+       NO_STR
+       "Interface IPv6 config commands\n"
+       "Neighbor discovery\n"
+       "Reachable time\n"
+       "Reachable time in milliseconds\n")
+
 DEFUN (ipv6_nd_homeagent_preference,
        ipv6_nd_homeagent_preference_cmd,
-       "ipv6 nd home-agent-preference PREFERENCE",
+       "ipv6 nd home-agent-preference <0-65535>",
        "Interface IPv6 config commands\n"
        "Neighbor discovery\n"
        "Home Agent preference\n"
-       "Home Agent preference value 0..65535\n")
+       "preference value (default is 0, least preferred)\n")
 {
-  u_int32_t hapref;
-  struct interface *ifp;
-  struct zebra_if *zif;
-
-  ifp = (struct interface *) vty->index;
-  zif = ifp->info;
-
-  hapref = (u_int32_t) atol (argv[0]);
-
-  if (hapref > 65535)
-    {
-      vty_out (vty, "Invalid Home Agent preference%s", VTY_NEWLINE);
-      return CMD_WARNING;
-    }
-
-  zif->rtadv.HomeAgentPreference = hapref;
-
+  struct interface *ifp = (struct interface *) vty->index;
+  struct zebra_if *zif = ifp->info;
+  VTY_GET_INTEGER_RANGE ("home agent preference", zif->rtadv.HomeAgentPreference, argv[0], 0, 65535);
   return CMD_SUCCESS;
 }
 
@@ -916,31 +961,26 @@
   return CMD_SUCCESS;
 }
 
+ALIAS (no_ipv6_nd_homeagent_preference,
+       no_ipv6_nd_homeagent_preference_val_cmd,
+       "no ipv6 nd home-agent-preference <0-65535>",
+       NO_STR
+       "Interface IPv6 config commands\n"
+       "Neighbor discovery\n"
+       "Home Agent preference\n"
+       "preference value (default is 0, least preferred)\n")
+
 DEFUN (ipv6_nd_homeagent_lifetime,
        ipv6_nd_homeagent_lifetime_cmd,
-       "ipv6 nd home-agent-lifetime SECONDS",
+       "ipv6 nd home-agent-lifetime <0-65520>",
        "Interface IPv6 config commands\n"
        "Neighbor discovery\n"
        "Home Agent lifetime\n"
-       "Home Agent lifetime in seconds\n")
+       "Home Agent lifetime in seconds (0 to track ra-lifetime)\n")
 {
-  u_int32_t ha_ltime;
-  struct interface *ifp;
-  struct zebra_if *zif;
-
-  ifp = (struct interface *) vty->index;
-  zif = ifp->info;
-
-  ha_ltime = (u_int32_t) atol (argv[0]);
-
-  if (ha_ltime > RTADV_MAX_HALIFETIME)
-    {
-      vty_out (vty, "Invalid Home Agent Lifetime time%s", VTY_NEWLINE);
-      return CMD_WARNING;
-    }
-
-  zif->rtadv.HomeAgentLifetime = ha_ltime;
-
+  struct interface *ifp = (struct interface *) vty->index;
+  struct zebra_if *zif = ifp->info;
+  VTY_GET_INTEGER_RANGE ("home agent lifetime", zif->rtadv.HomeAgentLifetime, argv[0], 0, RTADV_MAX_HALIFETIME);
   return CMD_SUCCESS;
 }
 
@@ -958,11 +998,20 @@
   ifp = (struct interface *) vty->index;
   zif = ifp->info;
 
-  zif->rtadv.HomeAgentLifetime = 0;
+  zif->rtadv.HomeAgentLifetime = -1;
 
   return CMD_SUCCESS;
 }
 
+ALIAS (no_ipv6_nd_homeagent_lifetime,
+       no_ipv6_nd_homeagent_lifetime_val_cmd,
+       "no ipv6 nd home-agent-lifetime <0-65520>",
+       NO_STR
+       "Interface IPv6 config commands\n"
+       "Neighbor discovery\n"
+       "Home Agent lifetime\n"
+       "Home Agent lifetime in seconds (0 to track ra-lifetime)\n")
+
 DEFUN (ipv6_nd_managed_config_flag,
        ipv6_nd_managed_config_flag_cmd,
        "ipv6 nd managed-config-flag",
@@ -1137,12 +1186,13 @@
   ifp = (struct interface *) vty->index;
   zebra_if = ifp->info;
 
-  ret = str2prefix_ipv6 (argv[0], (struct prefix_ipv6 *) &rp.prefix);
+  ret = str2prefix_ipv6 (argv[0], &rp.prefix);
   if (!ret)
     {
       vty_out (vty, "Malformed IPv6 prefix%s", VTY_NEWLINE);
       return CMD_WARNING;
     }
+  apply_mask_ipv6 (&rp.prefix); /* RFC4861 4.6.2 */
   rp.AdvOnLinkFlag = 1;
   rp.AdvAutonomousFlag = 1;
   rp.AdvRouterAddressFlag = 0;
@@ -1364,12 +1414,13 @@
   ifp = (struct interface *) vty->index;
   zebra_if = ifp->info;
 
-  ret = str2prefix_ipv6 (argv[0], (struct prefix_ipv6 *) &rp.prefix);
+  ret = str2prefix_ipv6 (argv[0], &rp.prefix);
   if (!ret)
     {
       vty_out (vty, "Malformed IPv6 prefix%s", VTY_NEWLINE);
       return CMD_WARNING;
     }
+  apply_mask_ipv6 (&rp.prefix); /* RFC4861 4.6.2 */
 
   ret = rtadv_prefix_reset (zebra_if, &rp);
   if (!ret)
@@ -1430,6 +1481,54 @@
   return CMD_SUCCESS;
 }
 
+ALIAS (no_ipv6_nd_router_preference,
+       no_ipv6_nd_router_preference_val_cmd,
+       "no ipv6 nd router-preference (high|medium|low",
+       NO_STR
+       "Interface IPv6 config commands\n"
+       "Neighbor discovery\n"
+       "Default router preference\n"
+       "High default router preference\n"
+       "Low default router preference\n"
+       "Medium default router preference (default)\n")
+
+DEFUN (ipv6_nd_mtu,
+       ipv6_nd_mtu_cmd,
+       "ipv6 nd mtu <1-65535>",
+       "Interface IPv6 config commands\n"
+       "Neighbor discovery\n"
+       "Advertised MTU\n"
+       "MTU in bytes\n")
+{
+  struct interface *ifp = (struct interface *) vty->index;
+  struct zebra_if *zif = ifp->info;
+  VTY_GET_INTEGER_RANGE ("MTU", zif->rtadv.AdvLinkMTU, argv[0], 1, 65535);
+  return CMD_SUCCESS;
+}
+
+DEFUN (no_ipv6_nd_mtu,
+       no_ipv6_nd_mtu_cmd,
+       "no ipv6 nd mtu",
+       NO_STR
+       "Interface IPv6 config commands\n"
+       "Neighbor discovery\n"
+       "Advertised MTU\n")
+{
+  struct interface *ifp = (struct interface *) vty->index;
+  struct zebra_if *zif = ifp->info;
+  zif->rtadv.AdvLinkMTU = 0;
+  return CMD_SUCCESS;
+}
+
+ALIAS (no_ipv6_nd_mtu,
+       no_ipv6_nd_mtu_val_cmd,
+       "no ipv6 nd mtu <1-65535>",
+       NO_STR
+       "Interface IPv6 config commands\n"
+       "Neighbor discovery\n"
+       "Advertised MTU\n"
+       "MTU in bytes\n")
+
 /* Write configuration about router advertisement. */
 void
 rtadv_config_write (struct vty *vty, struct interface *ifp)
@@ -1463,10 +1562,24 @@
       vty_out (vty, " ipv6 nd ra-interval %d%s", interval / 1000,
 	     VTY_NEWLINE);
 
-  if (zif->rtadv.AdvDefaultLifetime != RTADV_ADV_DEFAULT_LIFETIME)
+  if (zif->rtadv.AdvIntervalOption)
+    vty_out (vty, " ipv6 nd adv-interval-option%s", VTY_NEWLINE);
+
+  if (zif->rtadv.AdvDefaultLifetime != -1)
     vty_out (vty, " ipv6 nd ra-lifetime %d%s", zif->rtadv.AdvDefaultLifetime,
 	     VTY_NEWLINE);
 
+  if (zif->rtadv.HomeAgentPreference)
+    vty_out (vty, " ipv6 nd home-agent-preference %u%s",
+	     zif->rtadv.HomeAgentPreference, VTY_NEWLINE);
+
+  if (zif->rtadv.HomeAgentLifetime != -1)
+    vty_out (vty, " ipv6 nd home-agent-lifetime %u%s",
+	     zif->rtadv.HomeAgentLifetime, VTY_NEWLINE);
+
+  if (zif->rtadv.AdvHomeAgentFlag)
+    vty_out (vty, " ipv6 nd home-agent-config-flag%s", VTY_NEWLINE);
+
   if (zif->rtadv.AdvReachableTime)
     vty_out (vty, " ipv6 nd reachable-time %d%s", zif->rtadv.AdvReachableTime,
 	     VTY_NEWLINE);
@@ -1482,10 +1595,13 @@
 	     rtadv_pref_strs[zif->rtadv.DefaultPreference],
 	     VTY_NEWLINE);
 
+  if (zif->rtadv.AdvLinkMTU)
+    vty_out (vty, " ipv6 nd mtu %d%s", zif->rtadv.AdvLinkMTU, VTY_NEWLINE);
+
   for (ALL_LIST_ELEMENTS_RO (zif->rtadv.AdvPrefixList, node, rprefix))
     {
       vty_out (vty, " ipv6 nd prefix %s/%d",
-	       inet_ntop (AF_INET6, &rprefix->prefix.u.prefix6, 
+	       inet_ntop (AF_INET6, &rprefix->prefix.prefix,
 			  (char *) buf, INET6_ADDRSTRLEN),
 	       rprefix->prefix.prefixlen);
       if ((rprefix->AdvValidLifetime != RTADV_VALID_LIFETIME) || 
@@ -1572,10 +1688,14 @@
   install_element (INTERFACE_NODE, &ipv6_nd_ra_interval_cmd);
   install_element (INTERFACE_NODE, &ipv6_nd_ra_interval_msec_cmd);
   install_element (INTERFACE_NODE, &no_ipv6_nd_ra_interval_cmd);
+  install_element (INTERFACE_NODE, &no_ipv6_nd_ra_interval_val_cmd);
+  install_element (INTERFACE_NODE, &no_ipv6_nd_ra_interval_msec_val_cmd);
   install_element (INTERFACE_NODE, &ipv6_nd_ra_lifetime_cmd);
   install_element (INTERFACE_NODE, &no_ipv6_nd_ra_lifetime_cmd);
+  install_element (INTERFACE_NODE, &no_ipv6_nd_ra_lifetime_val_cmd);
   install_element (INTERFACE_NODE, &ipv6_nd_reachable_time_cmd);
   install_element (INTERFACE_NODE, &no_ipv6_nd_reachable_time_cmd);
+  install_element (INTERFACE_NODE, &no_ipv6_nd_reachable_time_val_cmd);
   install_element (INTERFACE_NODE, &ipv6_nd_managed_config_flag_cmd);
   install_element (INTERFACE_NODE, &no_ipv6_nd_managed_config_flag_cmd);
   install_element (INTERFACE_NODE, &ipv6_nd_other_config_flag_cmd);
@@ -1584,8 +1704,10 @@
   install_element (INTERFACE_NODE, &no_ipv6_nd_homeagent_config_flag_cmd);
   install_element (INTERFACE_NODE, &ipv6_nd_homeagent_preference_cmd);
   install_element (INTERFACE_NODE, &no_ipv6_nd_homeagent_preference_cmd);
+  install_element (INTERFACE_NODE, &no_ipv6_nd_homeagent_preference_val_cmd);
   install_element (INTERFACE_NODE, &ipv6_nd_homeagent_lifetime_cmd);
   install_element (INTERFACE_NODE, &no_ipv6_nd_homeagent_lifetime_cmd);
+  install_element (INTERFACE_NODE, &no_ipv6_nd_homeagent_lifetime_val_cmd);
   install_element (INTERFACE_NODE, &ipv6_nd_adv_interval_config_option_cmd);
   install_element (INTERFACE_NODE, &no_ipv6_nd_adv_interval_config_option_cmd);
   install_element (INTERFACE_NODE, &ipv6_nd_prefix_cmd);
@@ -1605,6 +1727,10 @@
   install_element (INTERFACE_NODE, &no_ipv6_nd_prefix_cmd);
   install_element (INTERFACE_NODE, &ipv6_nd_router_preference_cmd);
   install_element (INTERFACE_NODE, &no_ipv6_nd_router_preference_cmd);
+  install_element (INTERFACE_NODE, &no_ipv6_nd_router_preference_val_cmd);
+  install_element (INTERFACE_NODE, &ipv6_nd_mtu_cmd);
+  install_element (INTERFACE_NODE, &no_ipv6_nd_mtu_cmd);
+  install_element (INTERFACE_NODE, &no_ipv6_nd_mtu_val_cmd);
 }
 
 static int
diff --git a/zebra/rtadv.h b/zebra/rtadv.h
index d8d263d..564a4c6 100644
--- a/zebra/rtadv.h
+++ b/zebra/rtadv.h
@@ -30,7 +30,7 @@
 struct rtadv_prefix
 {
   /* Prefix to be advertised. */
-  struct prefix prefix;
+  struct prefix_ipv6 prefix;
   
   /* The value to be placed in the Valid Lifetime in the Prefix */
   u_int32_t AdvValidLifetime;
@@ -47,7 +47,7 @@
   /* The value to be placed in the Autonomous Flag. */
   int AdvAutonomousFlag;
 
-  /* The value to be placed in the Router Address Flag (RFC3775 7.2). */
+  /* The value to be placed in the Router Address Flag [RFC6275 7.2]. */
   int AdvRouterAddressFlag;
 #ifndef ND_OPT_PI_FLAG_RADDR
 #define ND_OPT_PI_FLAG_RADDR         0x20
@@ -58,7 +58,7 @@
 extern void rtadv_config_write (struct vty *, struct interface *);
 extern void rtadv_init (void);
 
-/* draft-ietf-mip6-mipext-advapi-03 */
+/* RFC4584 Extension to Sockets API for Mobile IPv6 */
 
 #ifndef ND_OPT_ADV_INTERVAL
 #define ND_OPT_ADV_INTERVAL	7   /* Adv Interval Option */
diff --git a/zebra/rtread_getmsg.c b/zebra/rtread_getmsg.c
index 3e065c6..81bf0de 100644
--- a/zebra/rtread_getmsg.c
+++ b/zebra/rtread_getmsg.c
@@ -90,7 +90,7 @@
 	gateway.s_addr = routeEntry->ipRouteNextHop;
 
 	rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &prefix,
-		      &gateway, NULL, 0, 0, 0, 0);
+		      &gateway, NULL, 0, 0, 0, 0, SAFI_UNICAST);
 }
 
 void
diff --git a/zebra/rtread_netlink.c b/zebra/rtread_netlink.c
index 44715d9..066e844 100644
--- a/zebra/rtread_netlink.c
+++ b/zebra/rtread_netlink.c
@@ -21,6 +21,9 @@
  */
 
 #include <zebra.h>
+
+#include "zebra/zserv.h"
+
 extern void netlink_route_read (void);
 
 void route_read (void)
diff --git a/zebra/rtread_proc.c b/zebra/rtread_proc.c
index 1de435a..07e8491 100644
--- a/zebra/rtread_proc.c
+++ b/zebra/rtread_proc.c
@@ -96,7 +96,7 @@
       p.prefixlen = ip_masklen (tmpmask);
       sscanf (gate, "%lX", (unsigned long *)&gateway);
 
-      rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p, &gateway, NULL, 0, 0, 0, 0);
+      rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p, &gateway, NULL, 0, 0, 0, 0, SAFI_UNICAST);
     }
 
   fclose (fp);
diff --git a/zebra/zebra_rib.c b/zebra/zebra_rib.c
index 8da6c84..2fa439c 100644
--- a/zebra/zebra_rib.c
+++ b/zebra/zebra_rib.c
@@ -55,18 +55,20 @@
 {  
   int key;
   int distance;
-} route_info[] =
+} route_info[ZEBRA_ROUTE_MAX] =
 {
-  {ZEBRA_ROUTE_SYSTEM,    0},
-  {ZEBRA_ROUTE_KERNEL,    0},
-  {ZEBRA_ROUTE_CONNECT,   0},
-  {ZEBRA_ROUTE_STATIC,    1},
-  {ZEBRA_ROUTE_RIP,     120},
-  {ZEBRA_ROUTE_RIPNG,   120},
-  {ZEBRA_ROUTE_OSPF,    110},
-  {ZEBRA_ROUTE_OSPF6,   110},
-  {ZEBRA_ROUTE_ISIS,    115},
-  {ZEBRA_ROUTE_BGP,      20  /* IBGP is 200. */}
+  [ZEBRA_ROUTE_SYSTEM]  = {ZEBRA_ROUTE_SYSTEM,    0},
+  [ZEBRA_ROUTE_KERNEL]  = {ZEBRA_ROUTE_KERNEL,    0},
+  [ZEBRA_ROUTE_CONNECT] = {ZEBRA_ROUTE_CONNECT,   0},
+  [ZEBRA_ROUTE_STATIC]  = {ZEBRA_ROUTE_STATIC,    1},
+  [ZEBRA_ROUTE_RIP]     = {ZEBRA_ROUTE_RIP,     120},
+  [ZEBRA_ROUTE_RIPNG]   = {ZEBRA_ROUTE_RIPNG,   120},
+  [ZEBRA_ROUTE_OSPF]    = {ZEBRA_ROUTE_OSPF,    110},
+  [ZEBRA_ROUTE_OSPF6]   = {ZEBRA_ROUTE_OSPF6,   110},
+  [ZEBRA_ROUTE_ISIS]    = {ZEBRA_ROUTE_ISIS,    115},
+  [ZEBRA_ROUTE_BGP]     = {ZEBRA_ROUTE_BGP,      20  /* IBGP is 200. */},
+  [ZEBRA_ROUTE_BABEL]   = {ZEBRA_ROUTE_BABEL,    95},
+  /* no entry/default: 150 */
 };
 
 /* Vector for routing table.  */
@@ -89,6 +91,11 @@
   vrf->table[AFI_IP6][SAFI_UNICAST] = route_table_init ();
   vrf->stable[AFI_IP][SAFI_UNICAST] = route_table_init ();
   vrf->stable[AFI_IP6][SAFI_UNICAST] = route_table_init ();
+  vrf->table[AFI_IP][SAFI_MULTICAST] = route_table_init ();
+  vrf->table[AFI_IP6][SAFI_MULTICAST] = route_table_init ();
+  vrf->stable[AFI_IP][SAFI_MULTICAST] = route_table_init ();
+  vrf->stable[AFI_IP6][SAFI_MULTICAST] = route_table_init ();
+
 
   return vrf;
 }
@@ -1229,6 +1236,7 @@
   [ZEBRA_ROUTE_ISIS]    = 2,
   [ZEBRA_ROUTE_BGP]     = 3,
   [ZEBRA_ROUTE_HSLS]    = 4,
+  [ZEBRA_ROUTE_BABEL]   = 2,
 };
 
 /* Look into the RN and queue it into one or more priority queues,
@@ -1519,7 +1527,7 @@
 rib_add_ipv4 (int type, int flags, struct prefix_ipv4 *p, 
 	      struct in_addr *gate, struct in_addr *src,
 	      unsigned int ifindex, u_int32_t vrf_id,
-	      u_int32_t metric, u_char distance)
+	      u_int32_t metric, u_char distance, safi_t safi)
 {
   struct rib *rib;
   struct rib *same = NULL;
@@ -1528,7 +1536,7 @@
   struct nexthop *nexthop;
 
   /* Lookup table.  */
-  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
+  table = vrf_table (AFI_IP, safi, 0);
   if (! table)
     return 0;
 
@@ -1538,7 +1546,10 @@
   /* Set default distance by route type. */
   if (distance == 0)
     {
-      distance = route_info[type].distance;
+      if ((unsigned)type >= sizeof(route_info) / sizeof(route_info[0]))
+	distance = 150;
+      else
+        distance = route_info[type].distance;
 
       /* iBGP distance is 200. */
       if (type == ZEBRA_ROUTE_BGP && CHECK_FLAG (flags, ZEBRA_FLAG_IBGP))
@@ -1774,7 +1785,7 @@
 }
 
 int
-rib_add_ipv4_multipath (struct prefix_ipv4 *p, struct rib *rib)
+rib_add_ipv4_multipath (struct prefix_ipv4 *p, struct rib *rib, safi_t safi)
 {
   struct route_table *table;
   struct route_node *rn;
@@ -1782,9 +1793,10 @@
   struct nexthop *nexthop;
   
   /* Lookup table.  */
-  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
+  table = vrf_table (AFI_IP, safi, 0);
   if (! table)
     return 0;
+
   /* Make it sure prefixlen is applied to the prefix. */
   apply_mask_ipv4 (p);
 
@@ -1847,7 +1859,7 @@
 /* XXX factor with rib_delete_ipv6 */
 int
 rib_delete_ipv4 (int type, int flags, struct prefix_ipv4 *p,
-		 struct in_addr *gate, unsigned int ifindex, u_int32_t vrf_id)
+		 struct in_addr *gate, unsigned int ifindex, u_int32_t vrf_id, safi_t safi)
 {
   struct route_table *table;
   struct route_node *rn;
@@ -1859,7 +1871,7 @@
   char buf2[INET_ADDRSTRLEN];
 
   /* Lookup table.  */
-  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
+  table = vrf_table (AFI_IP, safi, 0);
   if (! table)
     return 0;
 
@@ -1906,8 +1918,10 @@
       if (rib->type != type)
 	continue;
       if (rib->type == ZEBRA_ROUTE_CONNECT && (nexthop = rib->nexthop) &&
-	  nexthop->type == NEXTHOP_TYPE_IFINDEX && nexthop->ifindex == ifindex)
+	  nexthop->type == NEXTHOP_TYPE_IFINDEX)
 	{
+	  if (nexthop->ifindex != ifindex)
+	    continue;
 	  if (rib->refcnt)
 	    {
 	      rib->refcnt--;
@@ -2308,7 +2322,7 @@
 int
 rib_add_ipv6 (int type, int flags, struct prefix_ipv6 *p,
 	      struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id,
-	      u_int32_t metric, u_char distance)
+	      u_int32_t metric, u_char distance, safi_t safi)
 {
   struct rib *rib;
   struct rib *same = NULL;
@@ -2317,7 +2331,7 @@
   struct nexthop *nexthop;
 
   /* Lookup table.  */
-  table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
+  table = vrf_table (AFI_IP6, safi, 0);
   if (! table)
     return 0;
 
@@ -2402,7 +2416,7 @@
 /* XXX factor with rib_delete_ipv6 */
 int
 rib_delete_ipv6 (int type, int flags, struct prefix_ipv6 *p,
-		 struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id)
+		 struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id, safi_t safi)
 {
   struct route_table *table;
   struct route_node *rn;
@@ -2417,7 +2431,7 @@
   apply_mask_ipv6 (p);
 
   /* Lookup table.  */
-  table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
+  table = vrf_table (AFI_IP6, safi, 0);
   if (! table)
     return 0;
   
@@ -2454,8 +2468,10 @@
       if (rib->type != type)
         continue;
       if (rib->type == ZEBRA_ROUTE_CONNECT && (nexthop = rib->nexthop) &&
-	  nexthop->type == NEXTHOP_TYPE_IFINDEX && nexthop->ifindex == ifindex)
+	  nexthop->type == NEXTHOP_TYPE_IFINDEX)
 	{
+	  if (nexthop->ifindex != ifindex)
+	    continue;
 	  if (rib->refcnt)
 	    {
 	      rib->refcnt--;
@@ -2960,6 +2976,41 @@
     }
 }
 
+
+/* Remove specific by protocol routes from 'table'. */
+static unsigned long
+rib_score_proto_table (u_char proto, struct route_table *table)
+{
+  struct route_node *rn;
+  struct rib *rib;
+  struct rib *next;
+  unsigned long n = 0;
+
+  if (table)
+    for (rn = route_top (table); rn; rn = route_next (rn))
+      for (rib = rn->info; rib; rib = next)
+        {
+          next = rib->next;
+          if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
+            continue;
+          if (rib->type == proto)
+            {
+              rib_delnode (rn, rib);
+              n++;
+            }
+        }
+
+  return n;
+}
+
+/* Remove specific by protocol routes. */
+unsigned long
+rib_score_proto (u_char proto)
+{
+  return  rib_score_proto_table (proto, vrf_table (AFI_IP,  SAFI_UNICAST, 0))
+         +rib_score_proto_table (proto, vrf_table (AFI_IP6, SAFI_UNICAST, 0));
+}
+
 /* Close RIB and clean up kernel routes. */
 static void
 rib_close_table (struct route_table *table)
diff --git a/zebra/zebra_vty.c b/zebra/zebra_vty.c
index ecb5d10..dafcf75 100644
--- a/zebra/zebra_vty.c
+++ b/zebra/zebra_vty.c
@@ -556,6 +556,7 @@
 #define ONE_WEEK_SECOND 60*60*24*7
       if (rib->type == ZEBRA_ROUTE_RIP
 	  || rib->type == ZEBRA_ROUTE_OSPF
+	  || rib->type == ZEBRA_ROUTE_BABEL
 	  || rib->type == ZEBRA_ROUTE_ISIS
 	  || rib->type == ZEBRA_ROUTE_BGP)
 	{
@@ -774,6 +775,7 @@
 
       if (rib->type == ZEBRA_ROUTE_RIP
 	  || rib->type == ZEBRA_ROUTE_OSPF
+	  || rib->type == ZEBRA_ROUTE_BABEL
 	  || rib->type == ZEBRA_ROUTE_ISIS
 	  || rib->type == ZEBRA_ROUTE_BGP)
 	{
@@ -802,10 +804,6 @@
     }
 }
 
-#define SHOW_ROUTE_V4_HEADER "Codes: K - kernel route, C - connected, " \
-  "S - static, R - RIP, O - OSPF,%s       I - ISIS, B - BGP, " \
-  "> - selected route, * - FIB route%s%s"
-
 DEFUN (show_ip_route,
        show_ip_route_cmd,
        "show ip route",
@@ -828,8 +826,7 @@
       {
 	if (first)
 	  {
-	    vty_out (vty, SHOW_ROUTE_V4_HEADER, VTY_NEWLINE, VTY_NEWLINE,
-		     VTY_NEWLINE);
+	    vty_out (vty, SHOW_ROUTE_V4_HEADER);
 	    first = 0;
 	  }
 	vty_show_ip_route (vty, rn, rib);
@@ -871,8 +868,7 @@
 	{
 	  if (first)
 	    {
-	      vty_out (vty, SHOW_ROUTE_V4_HEADER, VTY_NEWLINE,
-		       VTY_NEWLINE, VTY_NEWLINE);
+	      vty_out (vty, SHOW_ROUTE_V4_HEADER);
 	      first = 0;
 	    }
 	  vty_show_ip_route (vty, rn, rib);
@@ -910,8 +906,7 @@
 	  {
 	    if (first)
 	      {
-		vty_out (vty, SHOW_ROUTE_V4_HEADER, VTY_NEWLINE,
-			 VTY_NEWLINE, VTY_NEWLINE);
+		vty_out (vty, SHOW_ROUTE_V4_HEADER);
 		first = 0;
 	      }
 	    vty_show_ip_route (vty, rn, rib);
@@ -922,17 +917,11 @@
 
 DEFUN (show_ip_route_protocol,
        show_ip_route_protocol_cmd,
-       "show ip route (bgp|connected|isis|kernel|ospf|rip|static)",
+       "show ip route " QUAGGA_IP_REDIST_STR_ZEBRA,
        SHOW_STR
        IP_STR
        "IP routing table\n"
-       "Border Gateway Protocol (BGP)\n"
-       "Connected\n"
-       "ISO IS-IS (ISIS)\n"
-       "Kernel\n"
-       "Open Shortest Path First (OSPF)\n"
-       "Routing Information Protocol (RIP)\n"
-       "Static routes\n")
+       QUAGGA_IP_REDIST_HELP_STR_ZEBRA)
 {
   int type;
   struct route_table *table;
@@ -940,21 +929,8 @@
   struct rib *rib;
   int first = 1;
 
-  if (strncmp (argv[0], "b", 1) == 0)
-    type = ZEBRA_ROUTE_BGP;
-  else if (strncmp (argv[0], "c", 1) == 0)
-    type = ZEBRA_ROUTE_CONNECT;
-  else if (strncmp (argv[0], "k", 1) ==0)
-    type = ZEBRA_ROUTE_KERNEL;
-  else if (strncmp (argv[0], "o", 1) == 0)
-    type = ZEBRA_ROUTE_OSPF;
-  else if (strncmp (argv[0], "i", 1) == 0)
-    type = ZEBRA_ROUTE_ISIS;
-  else if (strncmp (argv[0], "r", 1) == 0)
-    type = ZEBRA_ROUTE_RIP;
-  else if (strncmp (argv[0], "s", 1) == 0)
-    type = ZEBRA_ROUTE_STATIC;
-  else 
+  type = proto_redistnum (AFI_IP, argv[0]);
+  if (type < 0)
     {
       vty_out (vty, "Unknown route type%s", VTY_NEWLINE);
       return CMD_WARNING;
@@ -971,8 +947,7 @@
 	{
 	  if (first)
 	    {
-	      vty_out (vty, SHOW_ROUTE_V4_HEADER,
-		       VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
+	      vty_out (vty, SHOW_ROUTE_V4_HEADER);
 	      first = 0;
 	    }
 	  vty_show_ip_route (vty, rn, rib);
@@ -1559,6 +1534,7 @@
 #define ONE_WEEK_SECOND 60*60*24*7
       if (rib->type == ZEBRA_ROUTE_RIPNG
 	  || rib->type == ZEBRA_ROUTE_OSPF6
+	  || rib->type == ZEBRA_ROUTE_BABEL
 	  || rib->type == ZEBRA_ROUTE_ISIS
 	  || rib->type == ZEBRA_ROUTE_BGP)
 	{
@@ -1738,6 +1714,7 @@
       
       if (rib->type == ZEBRA_ROUTE_RIPNG
 	  || rib->type == ZEBRA_ROUTE_OSPF6
+	  || rib->type == ZEBRA_ROUTE_BABEL
 	  || rib->type == ZEBRA_ROUTE_ISIS
 	  || rib->type == ZEBRA_ROUTE_BGP)
 	{
@@ -1766,8 +1743,6 @@
     }
 }
 
-#define SHOW_ROUTE_V6_HEADER "Codes: K - kernel route, C - connected, S - static, R - RIPng, O - OSPFv3,%s       I - ISIS, B - BGP, * - FIB route.%s%s"
-
 DEFUN (show_ipv6_route,
        show_ipv6_route_cmd,
        "show ipv6 route",
@@ -1790,7 +1765,7 @@
       {
 	if (first)
 	  {
-	    vty_out (vty, SHOW_ROUTE_V6_HEADER, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
+	    vty_out (vty, SHOW_ROUTE_V6_HEADER);
 	    first = 0;
 	  }
 	vty_show_ipv6_route (vty, rn, rib);
@@ -1832,7 +1807,7 @@
 	{
 	  if (first)
 	    {
-	      vty_out (vty, SHOW_ROUTE_V6_HEADER, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
+	      vty_out (vty, SHOW_ROUTE_V6_HEADER);
 	      first = 0;
 	    }
 	  vty_show_ipv6_route (vty, rn, rib);
@@ -1842,17 +1817,11 @@
 
 DEFUN (show_ipv6_route_protocol,
        show_ipv6_route_protocol_cmd,
-       "show ipv6 route (bgp|connected|isis|kernel|ospf6|ripng|static)",
+       "show ipv6 route " QUAGGA_IP6_REDIST_STR_ZEBRA,
        SHOW_STR
        IP_STR
        "IP routing table\n"
-       "Border Gateway Protocol (BGP)\n"
-       "Connected\n"
-       "ISO IS-IS (ISIS)\n"
-       "Kernel\n"
-       "Open Shortest Path First (OSPFv3)\n"
-       "Routing Information Protocol (RIPng)\n"
-       "Static routes\n")
+	QUAGGA_IP6_REDIST_HELP_STR_ZEBRA)
 {
   int type;
   struct route_table *table;
@@ -1860,21 +1829,8 @@
   struct rib *rib;
   int first = 1;
 
-  if (strncmp (argv[0], "b", 1) == 0)
-    type = ZEBRA_ROUTE_BGP;
-  else if (strncmp (argv[0], "c", 1) == 0)
-    type = ZEBRA_ROUTE_CONNECT;
-  else if (strncmp (argv[0], "k", 1) ==0)
-    type = ZEBRA_ROUTE_KERNEL;
-  else if (strncmp (argv[0], "o", 1) == 0)
-    type = ZEBRA_ROUTE_OSPF6;
-  else if (strncmp (argv[0], "i", 1) == 0)
-    type = ZEBRA_ROUTE_ISIS;
-  else if (strncmp (argv[0], "r", 1) == 0)
-    type = ZEBRA_ROUTE_RIPNG;
-  else if (strncmp (argv[0], "s", 1) == 0)
-    type = ZEBRA_ROUTE_STATIC;
-  else 
+  type = proto_redistnum (AFI_IP6, argv[0]);
+  if (type < 0)
     {
       vty_out (vty, "Unknown route type%s", VTY_NEWLINE);
       return CMD_WARNING;
@@ -1891,7 +1847,7 @@
 	{
 	  if (first)
 	    {
-	      vty_out (vty, SHOW_ROUTE_V6_HEADER, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
+	      vty_out (vty, SHOW_ROUTE_V6_HEADER);
 	      first = 0;
 	    }
 	  vty_show_ipv6_route (vty, rn, rib);
@@ -1995,6 +1951,80 @@
   return CMD_SUCCESS;
 }
 
+/*
+ * Show IP mroute command to dump the BGP Multicast 
+ * routing table
+ */
+DEFUN (show_ip_mroute,
+       show_ip_mroute_cmd,
+       "show ip mroute",
+       SHOW_STR
+       IP_STR
+       "IP Multicast routing table\n")
+{
+  struct route_table *table;
+  struct route_node *rn;
+  struct rib *rib;
+  int first = 1;
+
+  table = vrf_table (AFI_IP, SAFI_MULTICAST, 0);
+  if (! table)
+    return CMD_SUCCESS;
+
+  /* Show all IPv4 routes. */
+  for (rn = route_top (table); rn; rn = route_next (rn))
+    for (rib = rn->info; rib; rib = rib->next)
+      {
+       if (first)
+         {
+	   vty_out (vty, SHOW_ROUTE_V4_HEADER);
+           first = 0;
+         }
+       vty_show_ip_route (vty, rn, rib);
+      }
+  return CMD_SUCCESS;
+}
+
+/*
+ * Show IPv6 mroute command.Used to dump
+ * the Multicast routing table.
+ */
+
+DEFUN (show_ipv6_mroute,
+       show_ipv6_mroute_cmd,
+       "show ipv6 mroute",
+       SHOW_STR
+       IP_STR
+       "IPv6 Multicast routing table\n")
+{
+  struct route_table *table;
+  struct route_node *rn;
+  struct rib *rib;
+  int first = 1;
+
+  table = vrf_table (AFI_IP6, SAFI_MULTICAST, 0);
+  if (! table)
+    return CMD_SUCCESS;
+
+  /* Show all IPv6 route. */
+  for (rn = route_top (table); rn; rn = route_next (rn))
+    for (rib = rn->info; rib; rib = rib->next)
+      {
+       if (first)
+         {
+	   vty_out (vty, SHOW_ROUTE_V6_HEADER);
+           first = 0;
+         }
+       vty_show_ipv6_route (vty, rn, rib);
+      }
+  return CMD_SUCCESS;
+}
+
+
+
+
+
+
 /* Write IPv6 static route configuration. */
 static int
 static_config_ipv6 (struct vty *vty)
@@ -2137,6 +2167,10 @@
   install_element (ENABLE_NODE, &show_ip_route_supernets_cmd);
   install_element (ENABLE_NODE, &show_ip_route_summary_cmd);
 
+  install_element (VIEW_NODE, &show_ip_mroute_cmd);
+  install_element (ENABLE_NODE, &show_ip_mroute_cmd);
+
+
 #ifdef HAVE_IPV6
   install_element (CONFIG_NODE, &ipv6_route_cmd);
   install_element (CONFIG_NODE, &ipv6_route_flags_cmd);
@@ -2166,5 +2200,8 @@
   install_element (ENABLE_NODE, &show_ipv6_route_prefix_cmd);
   install_element (ENABLE_NODE, &show_ipv6_route_prefix_longer_cmd);
   install_element (ENABLE_NODE, &show_ipv6_route_summary_cmd);
+
+  install_element (VIEW_NODE, &show_ipv6_mroute_cmd);
+  install_element (ENABLE_NODE, &show_ipv6_mroute_cmd);
 #endif /* HAVE_IPV6 */
 }
diff --git a/zebra/zserv.c b/zebra/zserv.c
index 23a5c4e..b1f539d 100644
--- a/zebra/zserv.c
+++ b/zebra/zserv.c
@@ -64,6 +64,15 @@
   return 0;
 }
 
+/* When client connects, it sends hello message
+ * with promise to send zebra routes of specific type.
+ * Zebra stores a socket fd of the client into
+ * this array. And use it to clean up routes that
+ * client didn't remove for some reasons after closing
+ * connection.
+ */
+static int route_type_oaths[ZEBRA_ROUTE_MAX];
+
 static int
 zserv_flush_data(struct thread *thread)
 {
@@ -721,6 +730,8 @@
   struct stream *s;
   unsigned int ifindex;
   u_char ifname_len;
+  safi_t safi;	
+
 
   /* Get input stream.  */
   s = client->ibuf;
@@ -739,6 +750,7 @@
 
   rib->flags = stream_getc (s);
   message = stream_getc (s); 
+  safi = stream_getw (s);
   rib->uptime = time (NULL);
 
   /* IPv4 prefix. */
@@ -790,7 +802,7 @@
     
   /* Table */
   rib->table=zebrad.rtm_table_default;
-  rib_add_ipv4_multipath (&p, rib);
+  rib_add_ipv4_multipath (&p, rib, safi);
   return 0;
 }
 
@@ -817,6 +829,7 @@
   api.type = stream_getc (s);
   api.flags = stream_getc (s);
   api.message = stream_getc (s);
+  api.safi = stream_getw (s);
 
   /* IPv4 prefix. */
   memset (&p, 0, sizeof (struct prefix_ipv4));
@@ -866,7 +879,7 @@
     api.metric = 0;
     
   rib_delete_ipv4 (api.type, api.flags, &p, nexthop_p, ifindex,
-		   client->rtm_table);
+		   client->rtm_table, api.safi);
   return 0;
 }
 
@@ -918,6 +931,7 @@
 
   api.flags = stream_getc (s);
   api.message = stream_getc (s);
+  api.safi = stream_getw (s);
 
   /* IPv4 prefix. */
   memset (&p, 0, sizeof (struct prefix_ipv6));
@@ -959,10 +973,10 @@
     
   if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
     rib_add_ipv6 (api.type, api.flags, &p, NULL, ifindex, zebrad.rtm_table_default, api.metric,
-		  api.distance);
+		  api.distance, api.safi);
   else
     rib_add_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, zebrad.rtm_table_default, api.metric,
-		  api.distance);
+		  api.distance, api.safi);
   return 0;
 }
 
@@ -985,6 +999,7 @@
   api.type = stream_getc (s);
   api.flags = stream_getc (s);
   api.message = stream_getc (s);
+  api.safi = stream_getw (s);
 
   /* IPv4 prefix. */
   memset (&p, 0, sizeof (struct prefix_ipv6));
@@ -1024,9 +1039,9 @@
     api.metric = 0;
     
   if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
-    rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, client->rtm_table);
+    rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, client->rtm_table, api.safi);
   else
-    rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, client->rtm_table);
+    rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, client->rtm_table, api.safi);
   return 0;
 }
 
@@ -1065,6 +1080,49 @@
   return 0;
 }
 
+/* Tie up route-type and client->sock */
+static void
+zread_hello (struct zserv *client)
+{
+  /* type of protocol (lib/zebra.h) */
+  u_char proto;
+  proto = stream_getc (client->ibuf);
+
+  /* accept only dynamic routing protocols */
+  if ((proto < ZEBRA_ROUTE_MAX)
+  &&  (proto > ZEBRA_ROUTE_STATIC))
+    {
+      zlog_notice ("client %d says hello and bids fair to announce only %s routes",
+                    client->sock, zebra_route_string(proto));
+
+      /* if route-type was binded by other client */
+      if (route_type_oaths[proto])
+        zlog_warn ("sender of %s routes changed %c->%c",
+                    zebra_route_string(proto), route_type_oaths[proto],
+                    client->sock);
+
+      route_type_oaths[proto] = client->sock;
+    }
+}
+
+/* If client sent routes of specific type, zebra removes it
+ * and returns number of deleted routes.
+ */
+static void
+zebra_score_rib (int client_sock)
+{
+  int i;
+
+  for (i = ZEBRA_ROUTE_RIP; i < ZEBRA_ROUTE_MAX; i++)
+    if (client_sock == route_type_oaths[i])
+      {
+        zlog_notice ("client %d disconnected. %lu %s routes removed from the rib",
+                      client_sock, rib_score_proto (i), zebra_route_string (i));
+        route_type_oaths[i] = 0;
+        break;
+      }
+}
+
 /* Close zebra client. */
 static void
 zebra_client_close (struct zserv *client)
@@ -1081,6 +1139,7 @@
   if (client->sock)
     {
       close (client->sock);
+      zebra_score_rib (client->sock);
       client->sock = -1;
     }
 
@@ -1288,6 +1347,9 @@
     case ZEBRA_IPV4_IMPORT_LOOKUP:
       zread_ipv4_import_lookup (client, length);
       break;
+    case ZEBRA_HELLO:
+      zread_hello (client);
+      break;
     default:
       zlog_info ("Zebra received unknown command %d", command);
       break;
@@ -1357,6 +1419,7 @@
       return;
     }
 
+  memset (&route_type_oaths, 0, sizeof (route_type_oaths));
   memset (&addr, 0, sizeof (struct sockaddr_in));
   addr.sin_family = AF_INET;
   addr.sin_port = htons (ZEBRA_PORT);
@@ -1427,6 +1490,8 @@
       return;
     }
 
+  memset (&route_type_oaths, 0, sizeof (route_type_oaths));
+
   /* Make server socket. */
   memset (&serv, 0, sizeof (struct sockaddr_un));
   serv.sun_family = AF_UNIX;
@@ -1738,11 +1803,11 @@
 
 /* Make zebra server socket, wiping any existing one (see bug #403). */
 void
-zebra_zserv_socket_init (void)
+zebra_zserv_socket_init (char *path)
 {
 #ifdef HAVE_TCP_ZEBRA
   zebra_serv ();
 #else
-  zebra_serv_un (ZEBRA_SERV_PATH);
+  zebra_serv_un (path ? path : ZEBRA_SERV_PATH);
 #endif /* HAVE_TCP_ZEBRA */
 }
diff --git a/zebra/zserv.h b/zebra/zserv.h
index e37041f..3d7ebbc 100644
--- a/zebra/zserv.h
+++ b/zebra/zserv.h
@@ -93,7 +93,7 @@
 /* Prototypes. */
 extern void zebra_init (void);
 extern void zebra_if_init (void);
-extern void zebra_zserv_socket_init (void);
+extern void zebra_zserv_socket_init (char *path);
 extern void hostinfo_get (void);
 extern void rib_init (void);
 extern void interface_list (void);