pax_global_header00006660000000000000000000000064127052127630014517gustar00rootroot0000000000000052 comment=97097592702e88226039a4106f22e87d5bef6d95 ndppd-0.2.5/000077500000000000000000000000001270521276300126305ustar00rootroot00000000000000ndppd-0.2.5/.gitignore000066400000000000000000000000611270521276300146150ustar00rootroot00000000000000ndppd.conf ndppd *.o ndppd.conf.5.gz ndppd.1.gz ndppd-0.2.5/ChangeLog000066400000000000000000000022251270521276300144030ustar00rootroot000000000000002016-04-18 Daniel Adolfsson * Version 0.2.5 * Defer configuration of interfaces until after daemonized; fixes an issue where ndppd would fail to set ALLMULTI on the interface properly. * Fix a cast so ndppd can be compiled on GCC 6. * Fix so ndppd changes working directory to / and umask to 0 once daemonized. 2015-10-13 Daniel Adolfsson * Version 0.2.4 * Fix an issue where ndppd daemonizes too early. * Fix to make sure the right pid is written to the pidfile. 2012-09-21 Daniel Adolfsson * Version 0.2.3 2012-02-06 Daniel Adolfsson * Version 0.2.2 * Removed "libconfuse" dependency. * New "auto" configuration to detect outgoing interface, for forwarding Neighbor Solicitation Messages. * Improved logging. * Bug fixes related to memory management. 2012-01-26 Daniel Adolfsson * Author changed e-mail address; updated copyright info. 2011-10-11 Daniel Adolfsson * Initial Release; 0.2.1 ndppd-0.2.5/LICENSE000066400000000000000000001045131270521276300136410ustar00rootroot00000000000000 GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The GNU General Public License is a free, copyleft license for software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modification follow. TERMS AND CONDITIONS 0. Definitions. "This License" refers to version 3 of the GNU General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. A "covered work" means either the unmodified Program or a work based on the Program. To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 1. Source Code. The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 3. Protecting Users' Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 4. Conveying Verbatim Copies. You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 5. Conveying Modified Source Versions. You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: a) The work must carry prominent notices stating that you modified it, and giving a relevant date. b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 7. Additional Terms. "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or d) Limiting the use for publicity purposes of names of licensors or authors of the material; or e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 8. Termination. You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 9. Acceptance Not Required for Having Copies. You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 10. Automatic Licensing of Downstream Recipients. Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 11. Patents. A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 12. No Surrender of Others' Freedom. If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 15. Disclaimer of Warranty. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. Limitation of Liability. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 17. Interpretation of Sections 15 and 16. If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program 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 . Also add information on how to contact you by electronic and paper mail. If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: Copyright (C) This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an "about box". You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see . The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read . ndppd-0.2.5/Makefile000066400000000000000000000015161270521276300142730ustar00rootroot00000000000000ifdef DEBUG CXXFLAGS ?= -g -DDEBUG else CXXFLAGS ?= -O3 endif PREFIX ?= /usr/local CXX ?= g++ GZIP ?= /bin/gzip MANDIR ?= ${DESTDIR}${PREFIX}/share/man SBINDIR ?= ${DESTDIR}${PREFIX}/sbin LIBS = OBJS = src/logger.o src/ndppd.o src/iface.o src/proxy.o src/address.o \ src/rule.o src/session.o src/conf.o src/route.o all: ndppd ndppd.1.gz ndppd.conf.5.gz install: all mkdir -p ${SBINDIR} ${MANDIR} ${MANDIR}/man1 ${MANDIR}/man5 cp ndppd ${SBINDIR} chmod +x ${SBINDIR}/ndppd cp ndppd.1.gz ${MANDIR}/man1 cp ndppd.conf.5.gz ${MANDIR}/man5 ndppd.1.gz: ${GZIP} < ndppd.1 > ndppd.1.gz ndppd.conf.5.gz: ${GZIP} < ndppd.conf.5 > ndppd.conf.5.gz ndppd: ${OBJS} ${CXX} -o ndppd ${LDFLAGS} ${LIBS} ${OBJS} .cc.o: ${CXX} -c ${CPPFLAGS} $(CXXFLAGS) -o $@ $< clean: rm -f ndppd ndppd.conf.5.gz ndppd.1.gz ${OBJS} ndppd-0.2.5/README000066400000000000000000000124371270521276300135170ustar00rootroot00000000000000 ndppd - NDP Proxy Daemon Version 0.2.5 ------------------------------------------------------------------------ 1. Legal ------------------------------------------------------------------------ ndppd - NDP Proxy Daemon Copyright (C) 2011-2016 Daniel Adolfsson This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program 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 . ------------------------------------------------------------------------ 2. About 'ndppd' ------------------------------------------------------------------------ 'ndppd', or NDP Proxy Daemon, is a daemon that proxies NDP (Neighbor Discovery Protocol) messages between interfaces. The Neighbor Discovery Protocol (NDP) is a protocol in the Internet Protocol Suite used with Internet Protocol Version 6 (IPv6). It operates in the Link Layer of the Internet model (RFC 1122) and is responsible for address autoconfiguration of nodes, discovery of other nodes on the link, determining the Link Layer addresses of other nodes, duplicate address detection, finding available routers and Domain Name System (DNS) servers, address prefix discovery, and maintaining reachability information about the paths to other active neighbor nodes (RFC 4861). (Wikipedia) 'ndppd' currently only supports Neighbor Solicitation Messages and Neighbor Advertisement Messages. Before an IPv6 packet can be sent to a host, that host's link-layer address must first be discovered. This is done by sending a Neighbor Solicitation message containing the requested target IPv6 address to a specific multicast address. If a host have configured a matching IP, that host will then respond with a Neighbor Advertisement message, and provide it's link-layer address. Let's say you want to route some IPs to another interface, and your ISP isn't truly routing your subnet to your host. It means that your host will have respond to Neighbor Solicitation messages for IPs it haven't configured in order to be able to route them. Linux have a limited support for proxying Neighbor Solicitation messages by simply answering to any messages where the target IP can be found in the host's neighbor proxy table. To make this work you need to enable "proxy_ndp", and then add each single host to the neighbor proxy table by typing something like: ip -6 neigh add proxy dev Unfortunately, it doesn't support listing proxies, and as I said, only individual IPs are supported. No subnets. 'ndppd' solves this by listening for Neighbor Solicitation messages on an interface, then query the internal interfaces for that target IP before finally sending a Neighbor Advertisement message. You can create rules to query one interface for one subnet, and another interface for another. 'ndppd' can even respond directly to Neighbor Solicitation messages without querying anything, should you need that. ------------------------------------------------------------------------ 3. Dependencies ------------------------------------------------------------------------ As of version 0.2.2, libconfuse is no longer needed. ------------------------------------------------------------------------ 4. Compiling ------------------------------------------------------------------------ First, make sure you have g++ and make installed. It should be as easy as: make all && make install If you want to enable debugging, you can type: make DEBUG=1 all Note that this version of the binary is much bigger, and the daemon produces a lot of messages. ------------------------------------------------------------------------ 5. Usage ------------------------------------------------------------------------ Read through 'ndppd.conf-dist' for guidelines and examples how to configure the daemon. Usage: ndppd [-d] [-c ] [-p ] -p Create a pidfile at the specified location. -c Read configuration from the specified location, instead of the default which is /etc/ndppd.conf. -d Daemonize the process, putting it in the background. Also enables syslogging. -v Increase logging verbosity. Can be used several times in order to increase even further. ------------------------------------------------------------------------ 5. Website and contact ------------------------------------------------------------------------ Contact : Daniel Adolfsson Website : https://github.com/DanielAdolfsson/ndppd Git : git://github.com/DanielAdolfsson/ndppd.git If you want to report a bug, you can either send me a mail directly, or submit an issue on github.com. ndppd-0.2.5/ndppd.1000066400000000000000000000032431270521276300140210ustar00rootroot00000000000000.\" Process this file with .\" groff -man -Tascii ndppd.1 .\" .TH NDPPD 1 "9/18/2011" "NDP Proxy Daemon Manual" "NDP Proxy Daemon Manual" .SH NAME ndppd \- NDP Proxy Daemon .SH SYNOPSIS .B ndppd [-d] [-vvv] [-c ] [-p ] .SH DESCRIPTION .BR ndppd, or .BR "NDP Proxy Daemon" , is a daemon that proxies NDP (Neighbor Discovery Protocol) messages between interfaces. .SH OPTIONS .IP "-c " Use the alternate .I config-file instead of .IR /etc/ndppd.conf . .IP -d Daemonizes .B ndppd and puts it into the background. It also enables syslogging. .IP "-p " Creates a .I pidfile at the specified location. .IP -v Increases logging verbosity. Can be specified several times to increase verbosity even further. .SH FILES .I /etc/ndppd.conf .RS Default configuration file. See .BR ndppd.conf(5) for further details about configuration options. .RE .SH BUGS No known bugs at the time of this writing. .SH LICENSE .EX Copyright (C) 2011 Daniel Adolfsson This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program 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 . .EE .SH AUTHOR Daniel Adolfsson .SH "SEE ALSO" .BR ndppd.conf(5) ndppd-0.2.5/ndppd.conf-dist000066400000000000000000000041551270521276300155520ustar00rootroot00000000000000# route-ttl (NEW) # This tells 'ndppd' how often to reload the route file /proc/net/ipv6_route. # Default value is '30000' (30 seconds). route-ttl 30000 # proxy # This sets up a listener, that will listen for any Neighbor Solicitation # messages, and respond to them according to a set of rules (see below). # is required. You may have several 'proxy' sections. proxy eth0 { # router # This option turns on or off the router flag for Neighbor Advertisement # messages. Default value is 'true'. router yes # timeout # Controls how long to wait for a Neighbor Advertisment message before # invalidating the entry, in milliseconds. Default value is '500'. timeout 500 # ttl # Controls how long a valid or invalid entry remains in the cache, in # milliseconds. Default value is '30000' (30 seconds). ttl 30000 # rule [/] # This is a rule that the target address is to match against. If no netmask # is provided, /128 is assumed. You may have several rule sections, and the # addresses may or may not overlap. rule 1111:: { # Only one of 'static', 'auto' and 'interface' may be specified. Please # read 'ndppd.conf' manpage for details about the methods below. # 'auto' should work in most cases. # static (NEW) # 'ndppd' will immediately answer any Neighbor Solicitation Messages # (if they match the IP rule). # iface # 'ndppd' will forward the Neighbor Solicitation Message through the # specified interface - and only respond if a matching Neighbor # Advertisement Message is received. # auto (NEW) # Same as above, but instead of manually specifying the outgoing # interface, 'ndppd' will check for a matching route in /proc/net/ipv6_route. auto # Note that before version 0.2.2 of 'ndppd', if you didn't choose a # method, it defaulted to 'static'. For compatibility reasons we choose # to keep this behavior - for now (it may be removed in a future version). } } ndppd-0.2.5/ndppd.conf.5000066400000000000000000000052741270521276300147570ustar00rootroot00000000000000.\" Process this file with .\" groff -man -Tascii ndppd.conf.5 .\" .TH NDPPD\&.CONF 5 "9/18/2011" "NDP Proxy Daemon Manual" "NDP Proxy Daemon Manual" .SH NAME ndppd.conf \- configuration file for ndppd .SH DESCRIPTION The syntax is as follows: .PP .EX proxy eth0 { rule 1234:5678::/96 { } } .EE .PP The configuration file must contain one or more .B proxy sections, and each of these section must contain one or more .B rule sections. .PP The .B ndppd daemon listens on the interface specified as an argument to the .B proxy section. Once a .B Neighbor Solicitation message arrives, it will try to match the target address against the address specified as the argument of the .B rule section. .SH OPTIONS .IP "proxy " Adds a proxy and binds it to the specified .IR interface . See below for information about .BR "proxy options" . .SH PROXY OPTIONS .IP "rule
" Adds a rule with the specified .I address to the proxy. It may be a an IP such as 1234::1 or a subnet such as 1111::/96. See below for information about .BR "rule options" . .IP "ttl " Controls how long .B ndppd will cache an entry. This is in milliseconds, and the default value is 30000 (30 seconds). .IP "timeout " Controls how long .B ndppd will wait for a Neighbor Advertisement message after forwarding a Neighbor Solicitation message according to the rule. This is in milliseconds, and the default value is 500 (.5 second). .IP "router " Controls if .B ndppd should send the .I router bit when sending Neighbor Advertisement messages. The default value here is .BR yes . .SH RULE OPTIONS Specify a method here. See below. .SH METHOD One of the following options must be specified in the .B rule section. All of these are mutually exclusive options, and cannot be combined. .IP "iface " Specify which .I interface the Neighbor Solicitation message will be sent out through. .IP "auto" .B (NEW) If this option is specified .B ndppd will attempt to detect which interface to use in order to forward Neighbor Solicitation Messages, by reading the routing table .BR /proc/net/ipv6_route . .IP "static" .B (NEW) This option tells .B ndppd that it should immediately respond to a Neighbor Solicitation Message without querying an internal interface. Note that it's recommended that you use this option sparingly, and with as high prefix length as possible. This is to make sure upstream routers are not polluted with spurious neighbor entries. If no rule option has been specified, it will default to .B static in order to be compatible with .BR 0.2.1 . This will, however, produce a warning, and most likely not work in future versions of .BR ndppd . .SH AUTHOR Daniel Adolfsson .SH "SEE ALSO" .BR ndppd(1) ndppd-0.2.5/src/000077500000000000000000000000001270521276300134175ustar00rootroot00000000000000ndppd-0.2.5/src/address.cc000066400000000000000000000150021270521276300153510ustar00rootroot00000000000000// ndppd - NDP Proxy Daemon // Copyright (C) 2011 Daniel Adolfsson // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program 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 . #include #include #include #include #include #include #include #include #include #include "ndppd.h" #include "address.h" NDPPD_NS_BEGIN address::address() { reset(); } address::address(const address& addr) { _addr.s6_addr32[0] = addr._addr.s6_addr32[0]; _addr.s6_addr32[1] = addr._addr.s6_addr32[1]; _addr.s6_addr32[2] = addr._addr.s6_addr32[2]; _addr.s6_addr32[3] = addr._addr.s6_addr32[3]; _mask.s6_addr32[0] = addr._mask.s6_addr32[0]; _mask.s6_addr32[1] = addr._mask.s6_addr32[1]; _mask.s6_addr32[2] = addr._mask.s6_addr32[2]; _mask.s6_addr32[3] = addr._mask.s6_addr32[3]; } address::address(const std::string& str) { parse_string(str); } address::address(const char* str) { parse_string(str); } address::address(const in6_addr& addr) { _addr.s6_addr32[0] = addr.s6_addr32[0]; _addr.s6_addr32[1] = addr.s6_addr32[1]; _addr.s6_addr32[2] = addr.s6_addr32[2]; _addr.s6_addr32[3] = addr.s6_addr32[3]; _mask.s6_addr32[0] = 0xffffffff; _mask.s6_addr32[1] = 0xffffffff; _mask.s6_addr32[2] = 0xffffffff; _mask.s6_addr32[3] = 0xffffffff; } address::address(const in6_addr& addr, const in6_addr& mask) { _addr.s6_addr32[0] = addr.s6_addr32[0]; _addr.s6_addr32[1] = addr.s6_addr32[1]; _addr.s6_addr32[2] = addr.s6_addr32[2]; _addr.s6_addr32[3] = addr.s6_addr32[3]; _mask.s6_addr32[0] = mask.s6_addr32[0]; _mask.s6_addr32[1] = mask.s6_addr32[1]; _mask.s6_addr32[2] = mask.s6_addr32[2]; _mask.s6_addr32[3] = mask.s6_addr32[3]; } address::address(const in6_addr& addr, int pf) { _addr.s6_addr32[0] = addr.s6_addr32[0]; _addr.s6_addr32[1] = addr.s6_addr32[1]; _addr.s6_addr32[2] = addr.s6_addr32[2]; _addr.s6_addr32[3] = addr.s6_addr32[3]; prefix(pf); } bool address::operator==(const address& addr) const { return !(((_addr.s6_addr32[0] ^ addr._addr.s6_addr32[0]) & _mask.s6_addr32[0]) | ((_addr.s6_addr32[1] ^ addr._addr.s6_addr32[1]) & _mask.s6_addr32[1]) | ((_addr.s6_addr32[2] ^ addr._addr.s6_addr32[2]) & _mask.s6_addr32[2]) | ((_addr.s6_addr32[3] ^ addr._addr.s6_addr32[3]) & _mask.s6_addr32[3])); } bool address::operator!=(const address& addr) const { return !!(((_addr.s6_addr32[0] ^ addr._addr.s6_addr32[0]) & _mask.s6_addr32[0]) | ((_addr.s6_addr32[1] ^ addr._addr.s6_addr32[1]) & _mask.s6_addr32[1]) | ((_addr.s6_addr32[2] ^ addr._addr.s6_addr32[2]) & _mask.s6_addr32[2]) | ((_addr.s6_addr32[3] ^ addr._addr.s6_addr32[3]) & _mask.s6_addr32[3])); } void address::reset() { _addr.s6_addr32[0] = 0; _addr.s6_addr32[1] = 0; _addr.s6_addr32[2] = 0; _addr.s6_addr32[3] = 0; _mask.s6_addr32[0] = 0xffffffff; _mask.s6_addr32[1] = 0xffffffff; _mask.s6_addr32[2] = 0xffffffff; _mask.s6_addr32[3] = 0xffffffff; } int address::prefix() const { if (!_mask.s6_addr[0]) { return 0; } for (int p = 0; p < 128; p++) { int byi = p / 8, bii = 7 - (p % 8); if (!(_mask.s6_addr[byi]& (1 << bii))) { return p; } } return 128; } void address::prefix(int pf) { const unsigned char maskbit[] = { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff }; if (pf >= 128) { _mask.s6_addr32[0] = 0xffffffff; _mask.s6_addr32[1] = 0xffffffff; _mask.s6_addr32[2] = 0xffffffff; _mask.s6_addr32[3] = 0xffffffff; return; } else { _mask.s6_addr32[0] = 0; _mask.s6_addr32[1] = 0; _mask.s6_addr32[2] = 0; _mask.s6_addr32[3] = 0; if (pf <= 0) { return; } } int offset = pf / 8, n; for (n = 0; n < offset; n++) { _mask.s6_addr[n] = 0xff; } _mask.s6_addr[offset] = maskbit[pf % 8]; } const std::string address::to_string() const { char buf[INET6_ADDRSTRLEN + 8]; if (!inet_ntop(AF_INET6,& _addr, buf, INET6_ADDRSTRLEN)) return "::1"; // TODO: What to do about invalid ip? int p; if ((p = prefix()) < 128) { sprintf(buf + strlen(buf), "/%d", p); } return buf; } bool address::parse_string(const std::string& str) { char buf[INET6_ADDRSTRLEN],* b; int sz; sz = 0; b = buf; reset(); const char* p = str.c_str(); while (*p && isspace(*p)) p++; while (*p) { if ((*p == '/') || isspace(*p)) { break; } if ((*p != ':') && !isxdigit(*p)) { return false; } if (sz >= (INET6_ADDRSTRLEN - 1)) { return false; } *b++ =* p++; sz++; } *b = '\0'; if (inet_pton(AF_INET6, buf,& _addr) <= 0) { return false; } while (*p && isspace(*p)) { p++; } if (*p == '\0') { _mask.s6_addr32[0] = 0xffffffff; _mask.s6_addr32[1] = 0xffffffff; _mask.s6_addr32[2] = 0xffffffff; _mask.s6_addr32[3] = 0xffffffff; return true; } if (*p++ != '/') return false; while (*p && isspace(*p)) { p++; } sz = 0; b = buf; while (*p) { if (!isdigit(*p)) { return false; } if (sz > 3) { return false; } *b++ =* p++; sz++; } *b = '\0'; prefix(atoi(buf)); return true; } address::operator std::string() const { return to_string(); } struct in6_addr& address::addr() { return _addr; } const struct in6_addr& address::const_addr() const { return _addr; } struct in6_addr& address::mask() { return _mask; } bool address::is_multicast() const { return _addr.s6_addr[0] == 0xff; } bool address::is_unicast() const { return _addr.s6_addr[0] != 0xff; } NDPPD_NS_END ndppd-0.2.5/src/address.h000066400000000000000000000032171270521276300152200ustar00rootroot00000000000000// ndppd - NDP Proxy Daemon // Copyright (C) 2011 Daniel Adolfsson // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program 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 . #pragma once #include #include #include "ndppd.h" NDPPD_NS_BEGIN class iface; class address { public: address(); address(const address& addr); address(const std::string& str); address(const char* str); address(const in6_addr& addr); address(const in6_addr& addr, const in6_addr& mask); address(const in6_addr& addr, int prefix); struct in6_addr& addr(); const struct in6_addr& const_addr() const; struct in6_addr& mask(); // Compare _a/_m against a._a. bool operator==(const address& addr) const; bool operator!=(const address& addr) const; void reset(); const std::string to_string() const; bool parse_string(const std::string& str); int prefix() const; void prefix(int n); bool is_unicast() const; bool is_multicast() const; operator std::string() const; private: struct in6_addr _addr, _mask; }; NDPPD_NS_END ndppd-0.2.5/src/conf.cc000066400000000000000000000135251270521276300146610ustar00rootroot00000000000000// ndppd - NDP Proxy Daemon // Copyright (C) 2011 Daniel Adolfsson // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program 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 . #include #include #include #include #include #include #include #include #include #include #include "ndppd.h" NDPPD_NS_BEGIN conf::conf() : _is_block(false) { } conf::operator int() const { return as_int(); } conf::operator const std::string&() const { return as_str(); } conf::operator bool() const { return as_bool(); } bool conf::as_bool() const { if (!strcasecmp(_value.c_str(), "true") || !strcasecmp(_value.c_str(), "yes")) return true; else return false; } const std::string& conf::as_str() const { return _value; } int conf::as_int() const { return atoi(_value.c_str()); } bool conf::empty() const { return _value == ""; } ptr conf::load(const std::string& path) { try { std::ifstream ifs; ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit); ifs.open(path.c_str(), std::ios::in); ifs.exceptions(std::ifstream::badbit); std::string buf((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); const char* c_buf = buf.c_str(); ptr cf(new conf); if (cf->parse_block(&c_buf)) { cf->dump(LOG_DEBUG); return cf; } logger::error() << "Could not parse configuration file"; } catch (std::ifstream::failure e) { logger::error() << "Failed to load configuration file '" << path << "'"; } return ptr(); } bool conf::is_block() const { return _is_block; } const char* conf::skip(const char* str, bool newlines) { while (*str) { while (*str && isspace(*str) && ((*str != '\n') || newlines)) str++; if ((*str == '#') || ((*str == '/') && (*(str + 1) == '/'))) { while (*str && (*str != '\n')) { str++; } } else if ((*str == '/') && (*(str + 1) == '*')) { while (*str) { if ((*str == '*') && (*(str + 1) == '/')) { str += 2; break; } str++; } } else { break; } } return str; } bool conf::parse_block(const char** str) { const char* p = *str; _is_block = true; while (*p) { std::stringstream ss; p = skip(p, true); if ((*p == '}') || !*p) { *str = p; return true; } while (isalnum(*p) || (*p == '_') || (*p == '-')) { ss << *p++; } p = skip(p, false); if (*p == '=') { p++; p = skip(p, false); } ptr cf(new conf); if (cf->parse(&p)) { _map.insert(std::pair >(ss.str(), cf)); } else { return false; } } *str = p; return true; } bool conf::parse(const char** str) { const char* p = *str; std::stringstream ss; p = skip(p, false); if ((*p == '\'') || (*p == '"')) { for (char e = *p++; *p && (*p != e) && (*p != '\n'); p++) ss << *p; p = skip(p, false); } else { while (*p && isgraph(*p) && (*p != '{') && (*p != '}')) { ss << *p++; } } _value = ss.str(); p = skip(p, false); if (*p == '{') { p++; if (!parse_block(&p)) { return false; } if (*p != '}') { return false; } p++; } *str = p; return true; } void conf::dump(int pri) const { logger l(pri); dump(l, 0); } void conf::dump(logger& l, int level) const { std::string pfx; for (int i = 0; i < level; i++) { pfx += " "; } if (_value != "") { l << _value << " "; } if (_is_block) { l << "{" << logger::endl; std::multimap >::const_iterator it; for (it = _map.begin(); it != _map.end(); it++) { l << pfx << " " << it->first << " "; it->second->dump(l, level + 1); } l << pfx << "}" << logger::endl; } l << logger::endl; } ptr conf::operator()(const std::string& name, int index) const { return find(name, index); } ptr conf::find(const std::string& name, int index) const { std::multimap >::const_iterator it; for (it = _map.find(name); it != _map.end(); it++) { if (index-- <= 0) return it->second; } return ptr(); } ptr conf::operator[](const std::string& name) const { return find(name, 0); } std::vector > conf::find_all(const std::string& name) const { std::vector > vec; std::multimap >::const_iterator it; std::pair >::const_iterator, std::multimap >::const_iterator> ret; ret = _map.equal_range(name); for (it = ret.first; it != ret.second; it++) { vec.push_back(it->second); } return vec; } conf::operator const std::string&() { return _value; } NDPPD_NS_END ndppd-0.2.5/src/conf.h000066400000000000000000000035021270521276300145150ustar00rootroot00000000000000// ndppd - NDP Proxy Daemon // Copyright (C) 2011 Daniel Adolfsson // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program 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 . #pragma once #include #include #include #include #include #include "ndppd.h" NDPPD_NS_BEGIN class conf { public: private: std::string _value; bool _is_block; std::multimap > _map; void dump(logger& l, int level) const; static const char* skip(const char* str, bool all = true); bool parse_block(const char* *str); bool parse(const char* *str); public: conf(); static ptr load(const std::string& path); bool is_block() const; ptr operator[](const std::string& name) const; ptr operator()(const std::string& name, int index = 0) const; operator const std::string&() const; operator int() const; operator bool() const; bool as_bool() const; const std::string& as_str() const; int as_int() const; bool empty() const; std::vector > find_all(const std::string& name) const; ptr find(const std::string& name, int index = 0) const; void dump(int pri = LOG_INFO) const; operator const std::string&(); }; NDPPD_NS_END ndppd-0.2.5/src/iface.cc000066400000000000000000000401001270521276300147700ustar00rootroot00000000000000// ndppd - NDP Proxy Daemon // Copyright (C) 2011 Daniel Adolfsson // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program 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 . #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "ndppd.h" NDPPD_NS_BEGIN std::map > iface::_map; bool iface::_map_dirty = false; std::vector iface::_pollfds; iface::iface() : _ifd(-1), _pfd(-1), _name("") { } iface::~iface() { logger::debug() << "iface::~iface()"; if (_ifd >= 0) close(_ifd); if (_pfd >= 0) { if (_prev_allmulti >= 0) { allmulti(_prev_allmulti); } close(_pfd); } _map_dirty = true; } ptr iface::open_pfd(const std::string& name) { int fd = 0; std::map >::iterator it = _map.find(name); ptr ifa; if (it != _map.end()) { if (it->second->_pfd >= 0) return it->second; ifa = it->second; } else { // We need an _ifs, so let's set one up. ifa = open_ifd(name); } if (!ifa) return ptr(); // Create a socket. if ((fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IPV6))) < 0) { logger::error() << "Unable to create socket"; return ptr(); } // Bind to the specified interface. struct sockaddr_ll lladdr; memset(&lladdr, 0, sizeof(struct sockaddr_ll)); lladdr.sll_family = AF_PACKET; lladdr.sll_protocol = htons(ETH_P_IPV6); if (!(lladdr.sll_ifindex = if_nametoindex(name.c_str()))) { close(fd); logger::error() << "Failed to bind to interface '" << name << "'"; return ptr(); } if (bind(fd, (struct sockaddr* )&lladdr, sizeof(struct sockaddr_ll)) < 0) { close(fd); logger::error() << "Failed to bind to interface '" << name << "'"; return ptr(); } // Switch to non-blocking mode. int on = 1; if (ioctl(fd, FIONBIO, (char* )&on) < 0) { close(fd); logger::error() << "Failed to switch to non-blocking on interface '" << name << "'"; return ptr(); } // Set up filter. static struct sock_filter filter[] = { // Load the ether_type. BPF_STMT(BPF_LD | BPF_H | BPF_ABS, offsetof(struct ether_header, ether_type)), // Bail if it's* not* ETHERTYPE_IPV6. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, ETHERTYPE_IPV6, 0, 5), // Load the next header type. BPF_STMT(BPF_LD | BPF_B | BPF_ABS, sizeof(struct ether_header) + offsetof(struct ip6_hdr, ip6_nxt)), // Bail if it's* not* IPPROTO_ICMPV6. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, IPPROTO_ICMPV6, 0, 3), // Load the ICMPv6 type. BPF_STMT(BPF_LD | BPF_B | BPF_ABS, sizeof(struct ether_header) + sizeof(ip6_hdr) + offsetof(struct icmp6_hdr, icmp6_type)), // Bail if it's* not* ND_NEIGHBOR_SOLICIT. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, ND_NEIGHBOR_SOLICIT, 0, 1), // Keep packet. BPF_STMT(BPF_RET | BPF_K, (u_int32_t)-1), // Drop packet. BPF_STMT(BPF_RET | BPF_K, 0) }; static struct sock_fprog fprog = { 8, filter }; if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog)) < 0) { logger::error() << "Failed to set filter"; return ptr(); } // Set up an instance of 'iface'. ifa->_pfd = fd; // Eh. Allmulti. ifa->_prev_allmulti = ifa->allmulti(1); _map_dirty = true; return ifa; } ptr iface::open_ifd(const std::string& name) { int fd; std::map >::iterator it = _map.find(name); if ((it != _map.end()) && it->second->_ifd) return it->second; // Create a socket. if ((fd = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) { logger::error() << "Unable to create socket"; return ptr(); } // Bind to the specified interface. struct ifreq ifr; memset(&ifr, 0, sizeof(ifr)); strncpy(ifr.ifr_name, name.c_str(), IFNAMSIZ - 1); ifr.ifr_name[IFNAMSIZ - 1] = '\0'; if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,& ifr, sizeof(ifr)) < 0) { close(fd); logger::error() << "Failed to bind to interface '" << name << "'"; return ptr(); } // Detect the link-layer address. memset(&ifr, 0, sizeof(ifr)); strncpy(ifr.ifr_name, name.c_str(), IFNAMSIZ - 1); ifr.ifr_name[IFNAMSIZ - 1] = '\0'; if (ioctl(fd, SIOCGIFHWADDR,& ifr) < 0) { close(fd); logger::error() << "Failed to detect link-layer address for interface '" << name << "'"; return ptr(); } logger::debug() << "fd=" << fd << ", hwaddr=" << ether_ntoa((const struct ether_addr* )&ifr.ifr_hwaddr.sa_data); // Set max hops. int hops = 255; if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hops, sizeof(hops)) < 0) { close(fd); logger::error() << "iface::open_ifd() failed IPV6_MULTICAST_HOPS"; return ptr(); } if (setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &hops, sizeof(hops)) < 0) { close(fd); logger::error() << "iface::open_ifd() failed IPV6_UNICAST_HOPS"; return ptr(); } // Switch to non-blocking mode. int on = 1; if (ioctl(fd, FIONBIO, (char*)&on) < 0) { close(fd); logger::error() << "Failed to switch to non-blocking on interface '" << name << "'"; return ptr(); } // Set up filter. struct icmp6_filter filter; ICMP6_FILTER_SETBLOCKALL(&filter); ICMP6_FILTER_SETPASS(ND_NEIGHBOR_ADVERT, &filter); if (setsockopt(fd, IPPROTO_ICMPV6, ICMP6_FILTER,& filter, sizeof(filter)) < 0) { logger::error() << "Failed to set filter"; return ptr(); } // Set up an instance of 'iface'. ptr ifa; if (it == _map.end()) { ifa = new iface(); ifa->_name = name; ifa->_ptr = ifa; _map[name] = ifa; } else { ifa = it->second; } ifa->_ifd = fd; memcpy(&ifa->hwaddr, ifr.ifr_hwaddr.sa_data, sizeof(struct ether_addr)); _map_dirty = true; return ifa; } ssize_t iface::read(int fd, struct sockaddr* saddr, uint8_t* msg, size_t size) { struct msghdr mhdr; struct iovec iov; int len; if (!msg || (size < 0)) return -1; iov.iov_len = size; iov.iov_base = (caddr_t)msg; memset(&mhdr, 0, sizeof(mhdr)); mhdr.msg_name = (caddr_t)saddr; mhdr.msg_namelen = sizeof(struct sockaddr); mhdr.msg_iov =& iov; mhdr.msg_iovlen = 1; if ((len = recvmsg(fd,& mhdr, 0)) < 0) return -1; if (len < sizeof(struct icmp6_hdr)) return -1; logger::debug() << "iface::read() len=" << len; return len; } ssize_t iface::write(int fd, const address& daddr, const uint8_t* msg, size_t size) { struct sockaddr_in6 daddr_tmp; struct msghdr mhdr; struct iovec iov; memset(&daddr_tmp, 0, sizeof(struct sockaddr_in6)); daddr_tmp.sin6_family = AF_INET6; daddr_tmp.sin6_port = htons(IPPROTO_ICMPV6); // Needed? memcpy(&daddr_tmp.sin6_addr,& daddr.const_addr(), sizeof(struct in6_addr)); iov.iov_len = size; iov.iov_base = (caddr_t)msg; memset(&mhdr, 0, sizeof(mhdr)); mhdr.msg_name = (caddr_t)&daddr_tmp; mhdr.msg_namelen = sizeof(sockaddr_in6); mhdr.msg_iov =& iov; mhdr.msg_iovlen = 1; logger::debug() << "iface::write() daddr=" << daddr.to_string() << ", len=" << size; int len; if ((len = sendmsg(fd,& mhdr, 0)) < 0) return -1; return len; } ssize_t iface::read_solicit(address& saddr, address& daddr, address& taddr) { struct sockaddr_ll t_saddr; uint8_t msg[256]; ssize_t len; if ((len = read(_pfd, (struct sockaddr*)&t_saddr, msg, sizeof(msg))) < 0) return -1; struct ip6_hdr* ip6h = (struct ip6_hdr* )(msg + ETH_HLEN); struct nd_neighbor_solicit* ns = (struct nd_neighbor_solicit*)(msg + ETH_HLEN + sizeof(struct ip6_hdr)); taddr = ns->nd_ns_target; daddr = ip6h->ip6_dst; saddr = ip6h->ip6_src; logger::debug() << "iface::read_solicit() saddr=" << saddr.to_string() << ", daddr=" << daddr.to_string() << ", len=" << len; return len; } ssize_t iface::write_solicit(const address& taddr) { char buf[128]; memset(buf, 0, sizeof(buf)); struct nd_neighbor_solicit* ns = (struct nd_neighbor_solicit* )&buf[0]; struct nd_opt_hdr* opt = (struct nd_opt_hdr* )&buf[sizeof(struct nd_neighbor_solicit)]; opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; opt->nd_opt_len = 1; ns->nd_ns_type = ND_NEIGHBOR_SOLICIT; memcpy(&ns->nd_ns_target,& taddr.const_addr(), sizeof(struct in6_addr)); memcpy(buf + sizeof(struct nd_neighbor_solicit) + sizeof(struct nd_opt_hdr), &hwaddr, 6); // FIXME: Alright, I'm lazy. static address multicast("ff02::1:ff00:0000"); address daddr; daddr = multicast; daddr.addr().s6_addr[13] = taddr.const_addr().s6_addr[13]; daddr.addr().s6_addr[14] = taddr.const_addr().s6_addr[14]; daddr.addr().s6_addr[15] = taddr.const_addr().s6_addr[15]; logger::debug() << "iface::write_solicit() taddr=" << taddr.to_string() << ", daddr=" << daddr.to_string(); return write(_ifd, daddr, (uint8_t* )buf, sizeof(struct nd_neighbor_solicit) + sizeof(struct nd_opt_hdr) + 6); } ssize_t iface::write_advert(const address& daddr, const address& taddr, bool router) { char buf[128]; memset(buf, 0, sizeof(buf)); struct nd_neighbor_advert* na = (struct nd_neighbor_advert* )&buf[0]; struct nd_opt_hdr* opt = (struct nd_opt_hdr* )&buf[sizeof(struct nd_neighbor_advert)]; opt->nd_opt_type = ND_OPT_TARGET_LINKADDR; opt->nd_opt_len = 1; na->nd_na_type = ND_NEIGHBOR_ADVERT; na->nd_na_flags_reserved = ND_NA_FLAG_SOLICITED | (router ? ND_NA_FLAG_ROUTER : 0); memcpy(&na->nd_na_target,& taddr.const_addr(), sizeof(struct in6_addr)); memcpy(buf + sizeof(struct nd_neighbor_advert) + sizeof(struct nd_opt_hdr), &hwaddr, 6); logger::debug() << "iface::write_advert() daddr=" << daddr.to_string() << ", taddr=" << taddr.to_string(); return write(_ifd, daddr, (uint8_t* )buf, sizeof(struct nd_neighbor_advert) + sizeof(struct nd_opt_hdr) + 6); } ssize_t iface::read_advert(address& saddr, address& taddr) { struct sockaddr_in6 t_saddr; uint8_t msg[256]; ssize_t len; if ((len = read(_ifd, (struct sockaddr* )&t_saddr, msg, sizeof(msg))) < 0) return -1; saddr = t_saddr.sin6_addr; if (((struct icmp6_hdr* )msg)->icmp6_type != ND_NEIGHBOR_ADVERT) return -1; taddr = ((struct nd_neighbor_solicit* )msg)->nd_ns_target; logger::debug() << "iface::read_advert() saddr=" << saddr.to_string() << ", taddr=" << taddr.to_string() << ", len=" << len; return len; } void iface::fixup_pollfds() { _pollfds.resize(_map.size()* 2); int i = 0; logger::debug() << "iface::fixup_pollfds() _map.size()=" << _map.size(); for (std::map >::iterator it = _map.begin(); it != _map.end(); it++) { _pollfds[i].fd = it->second->_ifd; _pollfds[i].events = POLLIN; _pollfds[i].revents = 0; i++; _pollfds[i].fd = it->second->_pfd; _pollfds[i].events = POLLIN; _pollfds[i].revents = 0; i++; } } void iface::remove_session(const ptr& se) { for (std::list >::iterator it = _sessions.begin(); it != _sessions.end(); it++) { if (*it == se) { _sessions.erase(it); break; } } } void iface::add_session(const ptr& se) { _sessions.push_back(se); } void iface::cleanup() { for (std::map >::iterator it = _map.begin(); it != _map.end(); ) { std::map >::iterator c_it = it++; if (!c_it->second) { _map.erase(c_it); } } } int iface::poll_all() { if (_map_dirty) { cleanup(); fixup_pollfds(); _map_dirty = false; } if (_pollfds.size() == 0) { ::sleep(1); return 0; } assert(_pollfds.size() == _map.size()* 2); int len; if ((len = ::poll(&_pollfds[0], _pollfds.size(), 50)) < 0) { return -1; } if (len == 0) { return 0; } std::map >::iterator i_it = _map.begin(); int i = 0; for (std::vector::iterator f_it = _pollfds.begin(); f_it != _pollfds.end(); f_it++) { assert(i_it != _map.end()); if (i && !(i % 2)) { i_it++; } bool is_pfd = i++ % 2; if (!(f_it->revents & POLLIN)) { continue; } ptr ifa = i_it->second; address saddr, daddr, taddr; if (is_pfd) { if (ifa->read_solicit(saddr, daddr, taddr) < 0) { logger::error() << "Failed to read from interface '%s'", ifa->_name.c_str(); continue; } if (!saddr.is_unicast() || !daddr.is_multicast()) { continue; } if (ifa->_pr) { ifa->_pr->handle_solicit(saddr, daddr, taddr); } } else { if (ifa->read_advert(saddr, taddr) < 0) { logger::error() << "Failed to read from interface '%s'", ifa->_name.c_str(); continue; } for (std::list >::iterator s_it = ifa->_sessions.begin(); s_it != ifa->_sessions.end(); s_it++) { assert(!s_it->is_null()); const ptr sess = *s_it; if ((sess->taddr() == taddr) && (sess->status() == session::WAITING)) { sess->handle_advert(); break; } } } } return 0; } int iface::allmulti(int state) { struct ifreq ifr; logger::debug() << "iface::allmulti() state=" << state << ", _name=\"" << _name << "\""; state = !!state; memset(&ifr, 0, sizeof(ifr)); strncpy(ifr.ifr_name, _name.c_str(), IFNAMSIZ); if (ioctl(_pfd, SIOCGIFFLAGS, &ifr) < 0) { logger::error() << "Failed to get allmulti: " << logger::err(); return -1; } int old_state = !!(ifr.ifr_flags &IFF_ALLMULTI); if (state == old_state) { return old_state; } if (state) { ifr.ifr_flags |= IFF_ALLMULTI; } else { ifr.ifr_flags &= ~IFF_ALLMULTI; } if (ioctl(_pfd, SIOCSIFFLAGS, &ifr) < 0) { logger::error() << "Failed to set allmulti: " << logger::err(); return -1; } return old_state; } const std::string& iface::name() const { return _name; } void iface::pr(const ptr& pr) { _pr = pr; } const ptr& iface::pr() const { return _pr; } NDPPD_NS_END ndppd-0.2.5/src/iface.h000066400000000000000000000064431270521276300146460ustar00rootroot00000000000000// ndppd - NDP Proxy Daemon // Copyright (C) 2011 Daniel Adolfsson // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program 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 . #pragma once #include #include #include #include #include #include #include "ndppd.h" NDPPD_NS_BEGIN class session; class proxy; class iface { public: // Destructor. ~iface(); static ptr open_ifd(const std::string& name); static ptr open_pfd(const std::string& name); static int poll_all(); static ssize_t read(int fd, struct sockaddr* saddr, uint8_t* msg, size_t size); static ssize_t write(int fd, const address& daddr, const uint8_t* msg, size_t size); // Writes a NB_NEIGHBOR_SOLICIT message to the _ifd socket. ssize_t write_solicit(const address& taddr); // Writes a NB_NEIGHBOR_ADVERT message to the _ifd socket; ssize_t write_advert(const address& daddr, const address& taddr, bool router); // Reads a NB_NEIGHBOR_SOLICIT message from the _pfd socket. ssize_t read_solicit(address& saddr, address& daddr, address& taddr); // Reads a NB_NEIGHBOR_ADVERT message from the _ifd socket; ssize_t read_advert(address& saddr, address& taddr); // Returns the name of the interface. const std::string& name() const; // Adds a session to be monitored for ND_NEIGHBOR_ADVERT messages. void add_session(const ptr& se); void remove_session(const ptr& se); void pr(const ptr& pr); const ptr& pr() const; private: static std::map > _map; static bool _map_dirty; // An array of objects used with ::poll. static std::vector _pollfds; // Updates the array above. static void fixup_pollfds(); static void cleanup(); // Weak pointer so this object can reference itself. weak_ptr _ptr; // The "generic" ICMPv6 socket for reading/writing NB_NEIGHBOR_ADVERT // messages as well as writing NB_NEIGHBOR_SOLICIT messages. int _ifd; // This is the PF_PACKET socket we use in order to read // NB_NEIGHBOR_SOLICIT messages. int _pfd; // Previous state of ALLMULTI for the interface. int _prev_allmulti; // Name of this interface. std::string _name; // An array of sessions that are monitoring this interface for // ND_NEIGHBOR_ADVERT messages. std::list > _sessions; weak_ptr _pr; // The link-layer address of this interface. struct ether_addr hwaddr; // Turns on/off ALLMULTI for this interface - returns the previous state // or -1 if there was an error. int allmulti(int state); // Constructor. iface(); }; NDPPD_NS_END ndppd-0.2.5/src/logger.cc000066400000000000000000000100211270521276300151770ustar00rootroot00000000000000 // ndppd - NDP Proxy Daemon // Copyright (C) 2011 Daniel Adolfsson // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program 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 . #include #include #include #include #include #include #include #include #include "ndppd.h" #include "logger.h" NDPPD_NS_BEGIN /*const char* log::_level_str[] = { "fatal", "alert", "critical", "error", "warning", "notice", "info", "debug" };*/ int logger::_max_pri = LOG_NOTICE; bool logger::_syslog = false; const logger::pri_name logger::_pri_names[] = { { "emergency", LOG_EMERG }, { "alert", LOG_ALERT }, { "critical", LOG_CRIT }, { "error", LOG_ERR }, { "warning", LOG_WARNING }, { "notice", LOG_NOTICE }, { "info", LOG_INFO }, { "debug", LOG_DEBUG }, { NULL, 0 } }; logger::logger(int pri) : _pri(pri), _force_log(false) { } logger::logger(const logger& l) : _pri(l._pri), _ss(l._ss.str()), _force_log(false) { } logger::~logger() { flush(); } std::string logger::format(const std::string& fmt, ...) { char buf[2048]; va_list va; va_start(va, fmt); vsnprintf(buf, sizeof(buf), fmt.c_str(), va); va_end(va); return buf; } std::string logger::err() { char buf[2048]; return strerror_r(errno, buf, sizeof(buf)); } logger logger::error() { return logger(LOG_ERR); } logger logger::info() { return logger(LOG_INFO); } logger logger::warning() { return logger(LOG_WARNING); } logger logger::debug() { return logger(LOG_DEBUG); } logger logger::notice() { return logger(LOG_NOTICE); } logger& logger::operator<<(const std::string& str) { _ss << str; return *this; } logger& logger::operator<<(int n) { _ss << n; return *this; } logger& logger::operator<<(logger& (*pf)(logger& )) { pf(*this); return *this; } logger& logger::endl(logger& __l) { __l.flush(); return __l; } logger& logger::force_log(bool b) { _force_log = b; return *this; } void logger::flush() { if (!_ss.rdbuf()->in_avail()) return; if (!_force_log && (_pri > _max_pri)) return; #ifndef DISABLE_SYSLOG if (_syslog) { ::syslog(_pri, "(%s) %s", _pri_names[_pri].name, _ss.str().c_str()); return; } #endif std::cout << "(" << _pri_names[_pri].name << ") " << _ss.str() << std::endl; _ss.str(""); } #ifndef DISABLE_SYSLOG void logger::syslog(bool sl) { if (sl == _syslog) return; if (_syslog = sl) { setlogmask(LOG_UPTO(LOG_DEBUG)); openlog("ndppd", LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER); } else { closelog(); } } bool logger::syslog() { return _syslog; } #endif void logger::max_pri(int pri) { _max_pri = pri; } int logger::verbosity() { return _max_pri; } void logger::verbosity(int pri) { if ((pri >= 0) && (pri <= 7)) { _max_pri = pri; } } bool logger::verbosity(const std::string& name) { const char* c_name = name.c_str(); if (!*c_name) { return false; } if (isdigit(*c_name)) { _max_pri = atoi(c_name); return true; } for (int i = 0; _pri_names[i].name; i++) { if (!strncmp(c_name, _pri_names[i].name, strlen(_pri_names[i].name))) { _max_pri = _pri_names[i].pri; return true; } } return false; } NDPPD_NS_END ndppd-0.2.5/src/logger.h000066400000000000000000000044431270521276300150540ustar00rootroot00000000000000// ndppd - NDP Proxy Daemon // Copyright (C) 2011 Daniel Adolfsson // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program 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 . #pragma once #include #ifndef DISABLE_SYSLOG # include #else # define LOG_EMERG 0 /* system is unusable */ # define LOG_ALERT 1 /* action must be taken immediately */ # define LOG_CRIT 2 /* critical conditions */ # define LOG_ERR 3 /* error conditions */ # define LOG_WARNING 4 /* warning conditions */ # define LOG_NOTICE 5 /* normal but significant condition */ # define LOG_INFO 6 /* informational */ # define LOG_DEBUG 7 /* debug-level messages */ #endif NDPPD_NS_BEGIN class logger { public: logger(int pri = LOG_NOTICE); logger(const logger& l); ~logger(); static std::string format(const std::string& fmt, ...); static void syslog(bool enable); static bool syslog(); static void max_pri(int pri); void flush(); static bool verbosity(const std::string& name); static int verbosity(); static void verbosity(int pri); logger& operator<<(const std::string& str); logger& operator<<(logger& (*pf)(logger& )); logger& operator<<(int n); logger& force_log(bool b = true); static logger& endl(logger& __l); // Shortcuts. static logger error(); static logger info(); static logger warning(); static logger debug(); static logger notice(); static std::string err(); private: int _pri; std::stringstream _ss; bool _force_log; struct pri_name { const char* name; int pri; }; static const pri_name _pri_names[]; static bool _syslog; static int _max_pri; }; NDPPD_NS_END ndppd-0.2.5/src/ndppd.cc000066400000000000000000000170471270521276300150440ustar00rootroot00000000000000// ndppd - NDP Proxy Daemon // Copyright (C) 2011 Daniel Adolfsson // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program 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 . #include #include #include #include #include #include #include #include #include #include #include #include "ndppd.h" #include "route.h" using namespace ndppd; static int daemonize() { pid_t pid = fork(); if (pid < 0) return -1; if (pid > 0) exit(0); umask(0); pid_t sid = setsid(); if (sid < 0) return -1; if (chdir("/") < 0) return -1; close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); return 0; } static ptr load_config(const std::string& path) { ptr cf, x_cf; if (!(cf = conf::load(path))) return (conf*)NULL; std::vector >::const_iterator p_it; std::vector > proxies(cf->find_all("proxy")); for (p_it = proxies.begin(); p_it != proxies.end(); p_it++) { ptr pr_cf = *p_it; if (pr_cf->empty()) { logger::error() << "'proxy' section is missing interface name"; return (conf*)NULL; } std::vector >::const_iterator r_it; std::vector > rules(pr_cf->find_all("rule")); for (r_it = rules.begin(); r_it != rules.end(); r_it++) { ptr ru_cf =* r_it; if (ru_cf->empty()) { logger::error() << "'rule' is missing an IPv6 address/net"; return (conf*)NULL; } address addr(*ru_cf); if (x_cf = ru_cf->find("iface")) { if (ru_cf->find("static") || ru_cf->find("auto")) { logger::error() << "Only one of 'iface', 'auto' and 'static' may " << "be specified."; return (conf*)NULL; } if ((const std::string&)*x_cf == "") { logger::error() << "'iface' expected an interface name"; return (conf*)NULL; } } else if (ru_cf->find("static")) { if (ru_cf->find("auto")) { logger::error() << "Only one of 'iface', 'auto' and 'static' may " << "be specified."; return (conf*)NULL; } if (addr.prefix() <= 120) { logger::warning() << "Low prefix length (" << addr.prefix() << " <= 120) when using 'static' method"; } } else if (!ru_cf->find("auto")) { logger::error() << "You must specify either 'iface', 'auto' or " << "'static'"; return (conf*)NULL; } } } return cf; } static bool configure(ptr& cf) { ptr x_cf; if (!(x_cf = cf->find("route-ttl"))) route::ttl(30000); else route::ttl(*x_cf); std::vector >::const_iterator p_it; std::vector > proxies(cf->find_all("proxy")); for (p_it = proxies.begin(); p_it != proxies.end(); p_it++) { ptr pr_cf = *p_it; if (pr_cf->empty()) { return false; } ptr pr = proxy::open(*pr_cf); if (!pr) { return false; } if (!(x_cf = pr_cf->find("router"))) pr->router(true); else pr->router(*x_cf); if (!(x_cf = pr_cf->find("ttl"))) pr->ttl(30000); else pr->ttl(*x_cf); if (!(x_cf = pr_cf->find("timeout"))) pr->timeout(500); else pr->timeout(*x_cf); std::vector >::const_iterator r_it; std::vector > rules(pr_cf->find_all("rule")); for (r_it = rules.begin(); r_it != rules.end(); r_it++) { ptr ru_cf =* r_it; address addr(*ru_cf); if (x_cf = ru_cf->find("iface")) { pr->add_rule(addr, iface::open_ifd(*x_cf)); } else if (ru_cf->find("auto")) { pr->add_rule(addr, true); } else { pr->add_rule(addr, false); } } } return true; } static bool running = true; static void exit_ndppd(int sig) { logger::error() << "Shutting down..."; running = 0; } int main(int argc, char* argv[], char* env[]) { signal(SIGINT, exit_ndppd); signal(SIGTERM, exit_ndppd); std::string config_path("/etc/ndppd.conf"); std::string pidfile; std::string verbosity; bool daemon = false; while (1) { int c, opt; static struct option long_options[] = { { "config", 1, 0, 'c' }, { "daemon", 0, 0, 'd' }, { "verbose", 1, 0, 'v' }, { 0, 0, 0, 0} }; c = getopt_long(argc, argv, "c:dp:v", long_options,& opt); if (c == -1) break; switch (c) { case 'c': config_path = optarg; break; case 'd': daemon = true; break; case 'p': pidfile = optarg; break; case 'v': logger::verbosity(logger::verbosity() + 1); /*if (optarg) { if (!logger::verbosity(optarg)) logger::error() << "Unknown verbosity level '" << optarg << "'"; } else { logger::max_pri(LOG_INFO); }*/ break; } } logger::notice() << "ndppd (NDP Proxy Daemon) version " NDPPD_VERSION << logger::endl << "Using configuration file '" << config_path << "'"; // Load configuration. ptr cf = load_config(config_path); if (cf.is_null()) return -1; if (daemon) { logger::syslog(true); if (daemonize() < 0) { logger::error() << "Failed to daemonize process"; return 1; } } if (!configure(cf)) return -1; if (!pidfile.empty()) { std::ofstream pf; pf.open(pidfile.c_str(), std::ios::out | std::ios::trunc); pf << getpid() << std::endl; pf.close(); } // Time stuff. struct timeval t1, t2; gettimeofday(&t1, 0); while (running) { if (iface::poll_all() < 0) { if (running) { logger::error() << "iface::poll_all() failed"; } break; } int elapsed_time; gettimeofday(&t2, 0); elapsed_time = ((t2.tv_sec - t1.tv_sec)* 1000) + ((t2.tv_usec - t1.tv_usec) / 1000); t1.tv_sec = t2.tv_sec; t1.tv_usec = t2.tv_usec; route::update(elapsed_time); session::update_all(elapsed_time); } logger::notice() << "Bye"; return 0; } ndppd-0.2.5/src/ndppd.h000066400000000000000000000020601270521276300146730ustar00rootroot00000000000000// ndppd - NDP Proxy Daemon // Copyright (C) 2011 Daniel Adolfsson // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program 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 . #pragma once #include #include #define NDPPD_NS_BEGIN namespace ndppd { #define NDPPD_NS_END } #define NDPPD_VERSION "0.2.4" #include #include "ptr.h" #include "logger.h" #include "conf.h" #include "address.h" #include "iface.h" #include "proxy.h" #include "session.h" #include "rule.h" ndppd-0.2.5/src/proxy.cc000066400000000000000000000104701270521276300151110ustar00rootroot00000000000000// ndppd - NDP Proxy Daemon // Copyright (C) 2011 Daniel Adolfsson // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program 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 . #include #include #include #include "ndppd.h" #include "proxy.h" #include "route.h" #include "iface.h" #include "rule.h" #include "session.h" NDPPD_NS_BEGIN std::list > proxy::_list; proxy::proxy() : _router(true), _ttl(30000), _timeout(500) { } ptr proxy::create(const ptr& ifa) { ptr pr(new proxy()); pr->_ptr = pr; pr->_ifa = ifa; _list.push_back(pr); ifa->pr(pr); logger::debug() << "proxy::create() if=" << ifa->name(); return pr; } ptr proxy::open(const std::string& ifname) { ptr ifa = iface::open_pfd(ifname); if (!ifa) { return ptr(); } return create(ifa); } void proxy::handle_solicit(const address& saddr, const address& daddr, const address& taddr) { logger::debug() << "proxy::handle_solicit() saddr=" << saddr.to_string() << ", taddr=" << taddr.to_string(); // Let's check this proxy's list of sessions to see if we can // find one with the same target address. for (std::list >::iterator sit = _sessions.begin(); sit != _sessions.end(); sit++) { if ((*sit)->taddr() == taddr) { switch ((*sit)->status()) { case session::WAITING: case session::INVALID: break; case session::VALID: (*sit)->send_advert(); } return; } } // Since we couldn't find a session that matched, we'll try to find // a matching rule instead, and then set up a new session. ptr se; for (std::list >::iterator it = _rules.begin(); it != _rules.end(); it++) { ptr ru = *it; logger::debug() << "checking " << ru->addr() << " against " << taddr; if (ru->addr() == taddr) { if (!se) { se = session::create(_ptr, saddr, daddr, taddr); } if (ru->is_auto()) { ptr rt = route::find(taddr); if (rt->ifname() == _ifa->name()) { logger::debug() << "skipping route since it's using interface " << rt->ifname(); } else { ptr ifa = rt->ifa(); if (ifa && (ifa != ru->ifa())) { se->add_iface(ifa); } } } else if (!ru->ifa()) { // This rule doesn't have an interface, and thus we'll consider // it "static" and immediately send the response. se->handle_advert(); return; } else { se->add_iface((*it)->ifa()); } } } if (se) { _sessions.push_back(se); se->send_solicit(); } } ptr proxy::add_rule(const address& addr, const ptr& ifa) { ptr ru(rule::create(_ptr, addr, ifa)); _rules.push_back(ru); return ru; } ptr proxy::add_rule(const address& addr, bool aut) { ptr ru(rule::create(_ptr, addr, aut)); _rules.push_back(ru); return ru; } void proxy::remove_session(const ptr& se) { _sessions.remove(se); } const ptr& proxy::ifa() const { return _ifa; } bool proxy::router() const { return _router; } void proxy::router(bool val) { _router = val; } int proxy::ttl() const { return _ttl; } void proxy::ttl(int val) { _ttl = (val >= 0) ? val : 30000; } int proxy::timeout() const { return _timeout; } void proxy::timeout(int val) { _timeout = (val >= 0) ? val : 500; } NDPPD_NS_END ndppd-0.2.5/src/proxy.h000066400000000000000000000033111270521276300147470ustar00rootroot00000000000000// ndppd - NDP Proxy Daemon // Copyright (C) 2011 Daniel Adolfsson // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program 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 . #pragma once #include #include #include #include #include "ndppd.h" NDPPD_NS_BEGIN class iface; class rule; class proxy { public: static ptr create(const ptr& ifa); static ptr open(const std::string& ifn); void handle_solicit(const address& saddr, const address& daddr, const address& taddr); void remove_session(const ptr& se); ptr add_rule(const address& addr, const ptr& ifa); ptr add_rule(const address& addr, bool aut = false); const ptr& ifa() const; bool router() const; void router(bool val); int timeout() const; void timeout(int val); int ttl() const; void ttl(int val); private: static std::list > _list; weak_ptr _ptr; ptr _ifa; std::list > _rules; std::list > _sessions; bool _router; int _ttl, _timeout; proxy(); }; NDPPD_NS_END ndppd-0.2.5/src/ptr.h000066400000000000000000000116641270521276300144050ustar00rootroot00000000000000// ndppd - NDP Proxy Daemon // Copyright (C) 2011 Daniel Adolfsson // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program 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 . #pragma once #include #include "ndppd.h" #include "logger.h" NDPPD_NS_BEGIN class invalid_pointer : public std::exception { public: invalid_pointer() throw() {}; }; template class weak_ptr; // This template class simplifies the usage of pointers. It's basically // a reference-counting smart pointer that supports both weak and // strong references. template class ptr { template friend class ptr; struct ptr_ref { T* ptr; int wc, sc; }; protected: bool _weak; ptr_ref* _ref; void acquire(ptr_ref* ref) { if (_ref) { release(); } if (ref && !ref->sc) { throw new invalid_pointer; } if (_ref = ref) { if (_weak) { _ref->wc++; } else { _ref->sc++; } } } void acquire(void* ptr) { if (!ptr) return; _ref = new ptr_ref(); _ref->ptr = (T*)ptr; _ref->wc = !!_weak; _ref->sc = !_weak; } void release() { if (!_ref) { return; } //logger::debug() // << "ptr::release() _ref=" << logger::format("%x", _ref) // << ", _ref->wc=" << _ref->wc << ", _ref->sc=" << _ref->sc // << ", _weak=" << (_weak ? "yes" : "no"); if (_weak) { assert(_ref->wc > 0); _ref->wc--; } else { assert(_ref->sc > 0); if (!--_ref->sc && _ref->ptr) { T* ptr = _ref->ptr; _ref->ptr = 0; _ref->wc++; delete ptr; _ref->wc--; } } /*if (!_weak && !_ref->sc && _ref->ptr) { T* ptr = (T*)(_ref->ptr); _ref->ptr = 0; delete ptr; }*/ if (!_ref->sc && !_ref->wc) { delete _ref; } _ref = 0; } template void acquire(const ptr& ptr) { acquire(ptr._ref); } public: ptr(bool weak = false) : _weak(weak), _ref(0) { } ptr(T* p, bool weak = false) : _weak(weak), _ref(0) { acquire(p); } ptr(const ptr& p, bool weak = false) : _weak(weak), _ref(0) { acquire(p._ref); } ptr(const weak_ptr& p, bool weak = false) : _weak(weak), _ref(0) { acquire(p._ref); } template ptr(const ptr& p, bool weak = false) : _weak(weak), _ref(0) { T* x = (U*)0; acquire(p._ref); } template ptr(const weak_ptr& p, bool weak = false) : _weak(weak), _ref(0) { T* x = (U*)0; acquire(p._ref); } ~ptr() { release(); } void operator=(T* p) { acquire(p); } ptr& operator=(const ptr& p) { acquire(p); return* this; } bool operator==(const ptr& other) const { return other._ref == _ref; } bool operator!=(const ptr& other) const { return other._ref != _ref; } bool is_null() const { return !_ref || !_ref->ptr; } T& operator*() const { return* get_pointer(); } T* operator->() const { return get_pointer(); } operator T*() const { return get_pointer(); } operator bool() const { return !is_null(); } void reset(T* p = 0) { acquire(p); } T* get_pointer() const { if (!_ref || !_ref->ptr) { throw new invalid_pointer; } return static_cast(_ref->ptr); } }; template class weak_ptr : public ptr { public: weak_ptr() : ptr(true) { } weak_ptr(T* p) : ptr(p, true) { } weak_ptr(const ptr& p) : ptr(p, true) { } weak_ptr(const weak_ptr& p) : ptr(p, true) { } template weak_ptr(const ptr& p) : ptr(p, true) { } template weak_ptr(const weak_ptr& p) : ptr(p, true) { } }; NDPPD_NS_END ndppd-0.2.5/src/route.cc000066400000000000000000000102111270521276300150570ustar00rootroot00000000000000// ndppd - NDP Proxy Daemon // Copyright (C) 2011 Daniel Adolfsson // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program 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 . #include #include #include #include "ndppd.h" #include "route.h" NDPPD_NS_BEGIN std::list > route::_routes; int route::_ttl; int route::_c_ttl; route::route(const address& addr, const std::string& ifname) : _addr(addr), _ifname(ifname) { } size_t route::hexdec(const char* str, unsigned char* buf, size_t size) { for (size_t i = 0; ; i++) { if (i >= size) { return i; } char c1 = tolower(str[i* 2]), c2 = tolower(str[i* 2 + 1]); if (!isxdigit(c1) || !isxdigit(c2)) { return i; } if ((c1 >= '0') && (c1 <= '9')) { buf[i] = (c1 - '0') << 4; } else { buf[i] = ((c1 - 'a') + 10) << 4; } if ((c2 >= '0') && (c2 <= '9')) { buf[i] |= c2 - '0'; } else { buf[i] |= (c2 - 'a') + 10; } } } std::string route::token(const char* str) { while (*str && isspace(*str)) { str++; } if (!*str) { return ""; } std::stringstream ss; while (*str && !isspace(*str)) { ss <<* str++; } return ss.str(); } void route::load(const std::string& path) { _routes.clear(); logger::debug() << "reading routes"; try { std::ifstream ifs; ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); ifs.open(path.c_str(), std::ios::in); ifs.exceptions(std::ifstream::badbit); while (!ifs.eof()) { char buf[1024]; ifs.getline(buf, sizeof(buf)); if (ifs.gcount() < 149) { continue; } address addr; unsigned char pfx; if (hexdec(buf, (unsigned char* )&addr.addr(), 16) != 16) { // TODO: Warn here? continue; } if (hexdec(buf + 33,& pfx, 1) != 1) { // TODO: Warn here? continue; } addr.prefix((int)pfx); route::create(addr, token(buf + 141)); } } catch (std::ifstream::failure e) { logger::warning() << "Failed to parse IPv6 routing data from '" << path << "'"; logger::error() << e.what(); } } void route::update(int elapsed_time) { if ((_c_ttl -= elapsed_time) <= 0) { load("/proc/net/ipv6_route"); _c_ttl = _ttl; } } ptr route::create(const address& addr, const std::string& ifname) { ptr rt(new route(addr, ifname)); // logger::debug() << "route::create() addr=" << addr << ", ifname=" << ifname; _routes.push_back(rt); return rt; } ptr route::find(const address& addr) { for (std::list >::iterator it = _routes.begin(); it != _routes.end(); it++) { if ((*it)->addr() == addr) return *it; } return ptr(); } ptr route::find_and_open(const address& addr) { ptr rt; if (rt = find(addr)) { return rt->ifa(); } return ptr(); } const std::string& route::ifname() const { return _ifname; } ptr route::ifa() { if (!_ifa) { logger::debug() << "router::ifa() opening interface '" << _ifname << "'"; return _ifa = iface::open_ifd(_ifname); } return ptr(); } const address& route::addr() const { return _addr; } int route::ttl() { return _ttl; } void route::ttl(int ttl) { _ttl = ttl; } NDPPD_NS_END ndppd-0.2.5/src/route.h000066400000000000000000000031721270521276300147310ustar00rootroot00000000000000// ndppd - NDP Proxy Daemon // Copyright (C) 2011 Daniel Adolfsson // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program 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 . #pragma once #include #include #include #include "ndppd.h" NDPPD_NS_BEGIN class route { public: static ptr create(const address& addr, const std::string& ifname); static ptr find(const address& addr); static ptr find_and_open(const address& addr); static void load(const std::string& path); static void update(int elapsed_time); static int ttl(); static void ttl(int ttl); const std::string& ifname() const; const address& addr() const; ptr ifa(); private: static int _ttl; static int _c_ttl; address _addr; std::string _ifname; ptr _ifa; static size_t hexdec(const char* str, unsigned char* buf, size_t size); static std::string token(const char* str); static std::list > _routes; route(const address& addr, const std::string& ifname); }; NDPPD_NS_END ndppd-0.2.5/src/rule.cc000066400000000000000000000034661270521276300147060ustar00rootroot00000000000000// ndppd - NDP Proxy Daemon // Copyright (C) 2011 Daniel Adolfsson // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program 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 . #include #include #include #include "ndppd.h" #include "rule.h" #include "proxy.h" #include "iface.h" NDPPD_NS_BEGIN rule::rule() { } ptr rule::create(const ptr& pr, const address& addr, const ptr& ifa) { ptr ru(new rule()); ru->_ptr = ru; ru->_pr = pr; ru->_ifa = ifa; ru->_addr = addr; ru->_aut = false; logger::debug() << "rule::create() if=" << pr->ifa()->name() << ", addr=" << addr; return ru; } ptr rule::create(const ptr& pr, const address& addr, bool aut) { ptr ru(new rule()); ru->_ptr = ru; ru->_pr = pr; ru->_addr = addr; ru->_aut = aut; logger::debug() << "rule::create() if=" << pr->ifa()->name().c_str() << ", addr=" << addr << ", auto=" << (aut ? "yes" : "no"); return ru; } const address& rule::addr() const { return _addr; } ptr rule::ifa() const { return _ifa; } bool rule::is_auto() const { return _aut; } bool rule::check(const address& addr) const { return _addr == addr; } NDPPD_NS_END ndppd-0.2.5/src/rule.h000066400000000000000000000025371270521276300145460ustar00rootroot00000000000000// ndppd - NDP Proxy Daemon // Copyright (C) 2011 Daniel Adolfsson // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program 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 . #pragma once #include #include #include #include #include "ndppd.h" NDPPD_NS_BEGIN class iface; class proxy; class rule { public: static ptr create(const ptr& pr, const address& addr, const ptr& ifa); static ptr create(const ptr& pr, const address& addr, bool stc = true); const address& addr() const; ptr ifa() const; bool is_auto() const; bool check(const address& addr) const; private: weak_ptr _ptr; weak_ptr _pr; ptr _ifa; address _addr; bool _aut; rule(); }; NDPPD_NS_END ndppd-0.2.5/src/session.cc000066400000000000000000000065351270521276300154220ustar00rootroot00000000000000// ndppd - NDP Proxy Daemon // Copyright (C) 2011 Daniel Adolfsson // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program 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 . #include #include "ndppd.h" #include "proxy.h" #include "iface.h" #include "session.h" NDPPD_NS_BEGIN std::list > session::_sessions; void session::update_all(int elapsed_time) { for (std::list >::iterator it = _sessions.begin(); it != _sessions.end(); ) { if (!*it) { _sessions.erase(it++); continue; } ptr se = *it++; if ((se->_ttl -= elapsed_time) >= 0) { continue; } switch (se->_status) { case session::WAITING: logger::debug() << "session is now invalid"; se->_status = session::INVALID; se->_ttl = se->_pr->ttl(); break; default: se->_pr->remove_session(se); } } } session::~session() { logger::debug() << "session::~session() this=" << logger::format("%x", this); for (std::list >::iterator it = _ifaces.begin(); it != _ifaces.end(); it++) { (*it)->remove_session(_ptr); } } ptr session::create(const ptr& pr, const address& saddr, const address& daddr, const address& taddr) { ptr se(new session()); se->_ptr = se; se->_pr = pr; se->_saddr = saddr; se->_taddr = taddr; se->_daddr = daddr; se->_ttl = pr->timeout(); _sessions.push_back(se); logger::debug() << "session::create() pr=" << logger::format("%x", (proxy* )pr) << ", saddr=" << saddr << ", daddr=" << daddr << ", taddr=" << taddr << " =" << logger::format("%x", (session* )se); return se; } void session::add_iface(const ptr& ifa) { if (std::find(_ifaces.begin(), _ifaces.end(), ifa) != _ifaces.end()) return; ifa->add_session(_ptr); _ifaces.push_back(ifa); } void session::send_solicit() { logger::debug() << "session::send_solicit() (_ifaces.size() = " << _ifaces.size() << ")"; for (std::list >::iterator it = _ifaces.begin(); it != _ifaces.end(); it++) { logger::debug() << " - " << (*it)->name(); (*it)->write_solicit(_taddr); } } void session::send_advert() { _pr->ifa()->write_advert(_saddr, _taddr, _pr->router()); } void session::handle_advert() { _status = VALID; _ttl = _pr->ttl(); send_advert(); } const address& session::taddr() const { return _taddr; } const address& session::saddr() const { return _saddr; } const address& session::daddr() const { return _daddr; } int session::status() const { return _status; } void session::status(int val) { _status = val; } NDPPD_NS_END ndppd-0.2.5/src/session.h000066400000000000000000000036231270521276300152570ustar00rootroot00000000000000// ndppd - NDP Proxy Daemon // Copyright (C) 2011 Daniel Adolfsson // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program 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 . #pragma once #include #include "ndppd.h" NDPPD_NS_BEGIN class proxy; class iface; class session { private: weak_ptr _ptr; weak_ptr _pr; address _saddr, _daddr, _taddr; // An array of interfaces this session is monitoring for // ND_NEIGHBOR_ADVERT on. std::list > _ifaces; // The remaining time in miliseconds the object will stay in the // interface's session array or cache. int _ttl; int _status; static std::list > _sessions; public: enum { WAITING, // Waiting for an advert response. VALID, // Valid; INVALID // Invalid; }; static void update_all(int elapsed_time); // Destructor. ~session(); static ptr create(const ptr& pr, const address& saddr, const address& daddr, const address& taddr); void add_iface(const ptr& ifa); const address& taddr() const; const address& daddr() const; const address& saddr() const; int status() const; void status(int val); void handle_advert(); void send_advert(); void send_solicit(); void refesh(); }; NDPPD_NS_END