pax_global_header00006660000000000000000000000064137725644650014535gustar00rootroot0000000000000052 comment=5506dc23541b5392a9590189c80eac32a44bc9f4 nlinline-0.2.2/000077500000000000000000000000001377256446500133465ustar00rootroot00000000000000nlinline-0.2.2/CMakeLists.txt000066400000000000000000000005411377256446500161060ustar00rootroot00000000000000cmake_minimum_required(VERSION 3.12) project(nlinline VERSION 0.2.2 DESCRIPTION "A quick and clean API for networking configuration via NETLINK" HOMEPAGE_URL "https://github.com/virtualsquare/nlinline" LANGUAGES C) include(GNUInstallDirs) install(FILES nlinline.h nlinline+.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) add_subdirectory(man) nlinline-0.2.2/COPYING000066400000000000000000000431231377256446500144040ustar00rootroot00000000000000 GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) 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 this service 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 make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. 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. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute 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 and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the 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 a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, 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. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE 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. END OF TERMS AND CONDITIONS Appendix: 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 convey 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) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. 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, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19yy name of author Gnomovision 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, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This 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 Library General Public License instead of this License. nlinline-0.2.2/README.md000066400000000000000000000160461377256446500146340ustar00rootroot00000000000000# nlinline A quick and clean API for NetLink networking configuring. NLINLINE (netlink inline) is a *library* of inline functions providing C programmers with very handy functions to configure network stacks. NLINLINE is entirely implemented in a header file, nlinline.h. NLinline provides the following functions: * `nlinline_if_nametoindex(const char *ifname);` return the index of the network interface whose name is `ifname` * `int nlinline_iplink_add(const char *ifname, unsigned int ifindex, const char *type, const char *data);` add a (virtual) link * `int nlinline_iplink_del(const char *ifname, unsigned int ifindex);` remove a link * `int nlinline_linksetupdown(unsigned int ifindex, int updown);` turn the interface `ifindex` up (`updown == 1`) or down (`updown == 0`). * `int nlinline_ipaddr_add(int family, void *addr, int prefixlen, int ifindex)` add an IP address to the interface `ifindex`. It supports IPv4 (`family == AF_INET`) and IPv6 `(family == AF_INET6)`. * `int nlinline_ipaddr_del(int family, void *addr, int prefixlen, int ifindex)` remove the IP address from the interface `ifindex`. It supports IPv4 (`family == AF_INET`) and IPv6 `(family == AF_INET6)`. * `int nlinline_iproute_add(int family, void *dst_addr, int dst_prefixlen, void *gw_addr, int ifindex);` add a static route to `dst_addr`/`dst_prefixlen` network through the gateway `gw_addr`. If `dst_addr == NULL` it adds a default route. `ifindex` must be specified when `gw_addr` is an IPv6 link local address. * `int nlinline_iproute_del(int family, void *dst_addr, int dst_prefixlen, void *gw_addr, int ifindex);` remove the static route to `dst_addr`/`dst_prefixlen` network through the gateway `gw_addr`. * `int nlinline_linksetaddr(unsigned int ifindex, void *macaddr);` set the MAC address of the interface `ifindex`. * `int nlinline_linkgetaddr(unsigned int ifindex, void *macaddr);` get the MAC address of the interface `ifindex`. * `int nlinline_linksetmtu(unsigned int ifindex, unsigned int mtu);` set the MTU of the interface `ifindex`. IP addresses are `void *` arguments, any sequence of 4 or 16 bytes (in network byte order) is a legal IPv4 or IPv6 address respectively. ### Netlink + inline * NLINLINE is a simple way to configure networking for network namespaces and *Internet of Threads* programs. * NLINLINE do not add dependencies at run-time. It is useful for security critical applications (like PAM modules) * NLINLINE uses netlink only, it does not depends on the obsolete netdevice (ioctl) API. * Only the code of referenced inline functions enters in the object and executable code. ### Example ```C #include #include #include #include int main(int argc, char *argv[]) { uint8_t ipv4addr[] = {192,168,2,2}; uint8_t ipv4gw[] = {192,168,2,1}; uint8_t ipv6addr[16] = {0x20, 0x01, 0x07, 0x60, [15] = 0x02}; uint8_t ipv6gw[16] = {0x20, 0x01, 0x07, 0x60, [15] = 0x01}; int ifindex = nlinline_if_nametoindex(argv[1]); if (ifindex > 0) printf("%d\n", ifindex); else { perror("nametoindex"); return 1; } if (nlinline_linksetupdown(ifindex, 1) < 0) perror("link up"); if (nlinline_ipaddr_add(AF_INET, ipv4addr, 24, ifindex) < 0) perror("addr ipv4"); if (nlinline_iproute_add(AF_INET, NULL, 0, ipv4gw) < 0) perror("addr ipv6"); if (nlinline_ipaddr_add(AF_INET6, ipv6addr, 64, ifindex) < 0) perror("route ipv4"); if (nlinline_iproute_add(AF_INET6, NULL, 0, ipv6gw, 0) < 0) perror("route ipv6"); return 0; } ``` This program takes the name of an interface from the command line. It turns that interface up and sets the interface IPv4 and IPv6 addresses and default routes. ## nlinline extended to user-mode stacks: nlinline+ The header file `nlinline+.h` implements an extended version of nlinline providing the support for user-mode networking stacks available as libraries. `nlinline+.h` basically provides the same function as `nlinline.h` in a more flexible implementation. `nlinline+.h` includes a set of macro which define the same inline functions as nlinkine for library-based networking stacks. ``` NLINLINE_LIB(mystack) ``` defines the functions `mystack_if_nametoindex`, `mystack_linksetupdown`... using `mystack_socket` instead of `socket`, `mystack_bind` instead of `bind` etc. ``` NLINLINE_LIBCOMP(yourstack) ``` is similar to `NLINLINE_LIB`: it defines `yourstack_if_nametoindex`, `yourstack_linksetupdown`... but it uses only `yourstack_socket` instead of `socket`, while `bind`, `close`, `recv`, `send` are the standard system calls. ``` NLINLINE_LIBMULTI(hisstack) ``` is for librariesproviding multi-stack support. The functions `hisstack_if_nametoindex`, `hisstack_linksetupdown` have one more (leading) argument: a generic pointer used as stack identifier. These functions use `hisstack_bind`, `hisstack_recv`... Instead of `socket`, `hisstack_msocket` is used: it has one more (leading) argument which is the stack identifier. ``` NLINLINE_LIBMULTICOMP(herstack) ``` combines the characteristics of `NLINLINE_LIBCOMP` and those of `NLINLINE_LIBMULTI`, it is for libraries providing multi-stack support whose file descriptors are compatible with standard system calls. All the function generated by `NLINLINE_LIBMULTICOMP` have one more argument as in `NLINLINE_LIBMULTI`, `herstack_msocket` is used instead of `socket`, while `bind`, `close`, `recv`, `send` are the standard system calls. ### Example As an example of usage, programs using the [libvdestack](https://github.com/rd235/libvdestack) library can have nlinline support: ``` #include #include #include #include #include #include #include NLINLINE_LIBMULTICOMP(vde_) int main(int argc, char *argv[]) { int rv; uint8_t ipv4addr[] = {192,168,2,2}; uint8_t ipv4gw[] = {192,168,2,1}; uint8_t ipv6addr[16] = {0x20, 0x01, 0x07, 0x60, [15] = 0x02}; uint8_t ipv6gw[16] = {0x20, 0x01, 0x07, 0x60, [15] = 0x01}; struct vdestack *stack = vde_addstack(argv[1], NULL); int ifindex = vde_if_nametoindex(stack, "vde0"); if (ifindex > 0) printf("%d\n", ifindex); else { perror("nametoindex"); return 1; } if (vde_linksetupdown(stack, ifindex, 1) < 0) perror("link up"); if (vde_ipaddr_add(stack, AF_INET, ipv4addr, 24, ifindex) < 0) perror("addr ipv4"); if (vde_iproute_add(stack, AF_INET, NULL, 0, ipv4gw) < 0) perror("addr ipv6"); if (vde_ipaddr_add(stack, AF_INET6, ipv6addr, 64, ifindex) < 0) perror("route ipv4"); if (vde_iproute_add(stack, AF_INET6, NULL, 0, ipv6gw, 0) < 0) perror("route ipv6"); /* use the stack */ vde_delstack(stack); } ``` Functions like `vde_linksetupdown`, `vde_if_nametoindex`,... have been defined by `NLINLINE_LIBMULTICOMP`. ## how to install nlinline Just put `nlinline.h` (and `nlinline+.h`) where you need it and that's all. ``` cp nlinline.h nlinline+.h /usr/local/include ``` or ``` cp nlinline.h /usr/include ``` In case you are lazy and you like standard install methods and tools, there is a trivial CMake support. The standard cmake install sequence applies: ``` $ cmake . $ sudo make install ``` nlinline-0.2.2/man/000077500000000000000000000000001377256446500141215ustar00rootroot00000000000000nlinline-0.2.2/man/CMakeLists.txt000066400000000000000000000020101377256446500166520ustar00rootroot00000000000000cmake_minimum_required(VERSION 3.7) set(RONN_ORGANIZATION "VirtualSquare") set(RONN_ARGS --organization=${RONN_ORGANIZATION}) # ### ronn pages file(GLOB VU_RONN_PAGES ${CMAKE_CURRENT_SOURCE_DIR}/*.[1-8].ronn) set(VU_MAN_FILES) foreach(VU_RONN_PATH IN LISTS VU_RONN_PAGES) # VU_RONNPAGE: basename of VU_RONN_PATH get_filename_component(VU_RONNPAGE ${VU_RONN_PATH} NAME) # VU_MANPAGE: VU_RONNPAGE without the suffix string(REGEX REPLACE "\.ronn$" "" VU_MANPAGE ${VU_RONNPAGE}) list(APPEND VU_MAN_FILES ${VU_MANPAGE}) endforeach(VU_RONN_PATH) add_custom_target(${PROJECT_NAME}_manpages ALL make RONN_ARGS="${RONN_ARGS}" ${VU_MAN_FILES} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) ### man pages file(GLOB VU_MAN_PAGES ${CMAKE_CURRENT_SOURCE_DIR}/*.[1-8]) foreach(VU_MAN_PATH IN LISTS VU_MAN_PAGES) get_filename_component(VU_MANPAGE ${VU_MAN_PATH} NAME) string(REGEX REPLACE ".*\\." "" MAN_CHAPTER ${VU_MANPAGE}) install(FILES ${VU_MAN_PATH} DESTINATION ${CMAKE_INSTALL_MANDIR}/man${MAN_CHAPTER}) endforeach(VU_MAN_PATH) nlinline-0.2.2/man/Makefile000066400000000000000000000005551377256446500155660ustar00rootroot00000000000000RONN=ronn RONNOK := $(shell command -v ${RONN} 2> /dev/null) none: % : %.ronn ifdef RONNOK # copy copyright notice grep "^\.\\\\\"" $< > $@ || true # run ronn $(RONN) -r ${RONN_ARGS} --pipe $< >> $@ # delete useless trailing "" in .TH sed -i '/^\.TH /s/ ""$$//' $@ else echo "${RONN} is not available. Manpage $@ cannot be updated" >/dev/stderr >&2 endif nlinline-0.2.2/man/nlinline.3000066400000000000000000000160571377256446500160260ustar00rootroot00000000000000.\" Copyright (C) 2019 VirtualSquare. Project Leader: Renzo Davoli .\" .\" This is free documentation; you can redistribute it and/or .\" modify it under the terms of the GNU General Public License, .\" as published by the Free Software Foundation, either version 2 .\" of the License, or (at your option) any later version. .\" .\" The GNU General Public License's references to "object code" .\" and "executables" are to be interpreted as the output of any .\" document formatting or typesetting system, including .\" intermediate and printed output. .\" .\" This manual 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 manual; if not, write to the Free .\" Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, .\" MA 02110-1301 USA. .\" .\" generated with Ronn-NG/v0.10.0 .\" http://github.com/apjanke/ronn-ng/tree/0.10.0-SNAPSHOT .TH "NLINLINE" "3" "December 2020" "VirtualSquare" .SH "NAME" nlinline_if_nametoindex, nlinline_linksetupdown, nlinline_ipaddr_add, nlinline_ipaddr_del, nlinline_iproute_add, nlinline_iproute_del, nlinline_iplink_add, nlinline_iplink_del, nlinline_linksetaddr, nlinline_linkgetaddr \- configure network interfaces .SH "SYNOPSIS" \fB#include \fR .P \fBint nlinline_if_nametoindex(const char *\fR\fIifname\fR\fB);\fR .P \fBint nlinline_linksetupdown(unsigned int\fR \fIifindex\fR\fB, int\fR \fIupdown\fR\fB);\fR .P \fBint nlinline_ipaddr_add(int\fR \fIfamily\fR\fB, void *\fR\fIaddr\fR\fB, int\fR \fIprefixlen\fR\fB, unsigned int\fR \fIifindex\fR\fB);\fR .P \fBint nlinline_ipaddr_del(int\fR \fIfamily\fR\fB, void *\fR\fIaddr\fR\fB, int\fR \fIprefixlen\fR\fB, unsigned int\fR \fIifindex\fR\fB);\fR .P \fBint nlinline_iproute_add(int\fR \fIfamily\fR\fB, void *\fR\fIdst_addr\fR\fB, int\fR \fIdst_prefixlen\fR\fB, void *\fR\fIgw_addr\fR\fB, unsigned int\fR \fIifindex\fR\fB);\fR .P \fBint nlinline_iproute_del(int\fR \fIfamily\fR\fB, void *\fR\fIdst_addr\fR\fB, int\fR \fIdst_prefixlen\fR\fB, void *\fR\fIgw_addr\fR\fB, unsigned int\fR \fIifindex\fR\fB);\fR .P \fBint nlinline_iplink_add(const char *\fR\fIifname\fR\fB, unsigned int\fR \fIifindex\fR\fB, const char *\fR\fItype\fR\fB, const char *\fR\fIdata\fR\fB);\fR .P \fBint nlinline_iplink_del(const char *\fR\fIifname\fR\fB, unsigned int\fR \fIifindex\fR\fB);\fR .P \fBint nlinline_linksetaddr(unsigned int\fR \fIifindex\fR\fB, void *\fR\fImacaddr\fR\fB);\fR .P \fBint nlinline_linkgetaddr(unsigned int\fR \fIifindex\fR\fB, void *\fR\fImacaddr\fR\fB);\fR .P \fBint nlinline_linksetmtu(unsigned int\fR \fIifindex\fR\fB, unsigned int\fR \fImtu\fR\fB);\fR .SH "DESCRIPTION" NLINLINE (netlink inline) is a \fIlibrary\fR of inline functions providing C programmers with very handy functions to configure network stacks\. NLINLINE is entirely implemented in a header file, nlinline\.h\. .TP \fBnlinline_if_nametoindex\fR This function returns the index of the network interface whose name is \fIifname\fR\. .TP \fBnlinline_linksetupdown\fR This function turns the interface \fIifindex\fR up (\fIupdown\fR == \fB1\fR) or down (\fIupdown\fR == \fB0\fR)\. .TP \fBnlinline_ipaddr_add\fR This function adds an IP address to the interface \fIifindex\fR\. It supports IPv4 (\fIfamily\fR == \fBAF_INET\fR) and IPv6 (\fIfamily\fR == \fBAF_INET6\fR)\. .TP \fBnlinline_ipaddr_del\fR This function removes the IP address from the interface \fIifindex\fR\. It supports IPv4 (\fIfamily\fR == \fBAF_INET\fR) and IPv6 (\fIfamily\fR == \fBAF_INET6\fR)\. .TP \fBnlinline_iproute_add\fR This function adds a static route to \fIdst_addr\fR/\fIdst_prefixlen\fR network through the gateway \fIgw_addr\fR\. If \fIdst_addr\fR == \fBNULL\fR it adds a default route\. \fIifindex\fR must be specified when \fIgw_addr\fR is an IPv6 link local address\. .TP \fBnlinline_iproute_del\fR This function removes the static route to \fIdst_addr\fR/\fIdst_prefixlen\fR network through the gateway \fIgw_addr\fR\. .TP \fBnlinline_iplink_add\fR This function adds a new link of type \fItype\fR, named \fIifname\fR\. The value of \fIdata\fR depends on the type of link and can be NULL\. A default interface name is assigned if \fIname\fR == \fBNULL\fR\. The link is created with a given index when \fIifindex\fR is positive\. .TP \fBnlinline_iplink_del\fR This function removes a link\. The link to be deleted can be identified by its name (\fIifname\fR) or by its index (\fIifindex\fR)\. Either \fIifindex\fR can be zero or \fIifname\fR can be \fBNULL\fR\. It is possible to use both \fIifindex\fR and \fIifname\fR to identify the link\. An error may occur if the parameters are inconsistent\. .TP \fBnlinline_linksetaddr\fR This functions sets the mac address of the interface \fIifindex\fR\. .TP \fBnlinline_linkgetaddr\fR This functions gets the mac address of the interface \fIifindex\fR\. .TP \fBnlinline_linksetmtu\fR This functions sets the MTU (Maximum Transfer Unit) of the interface \fIifindex\fR\. .P IP addresses are \fBvoid *\fR arguments, any sequence of 4 or 16 bytes (in network byte order) is a legal IPv4 or IPv6 address respectively\. .P \fBnlinline\fR functions do not add dependencies at run\-time\. This is useful for security critical applications (like PAM modules) These inline functions use netlink only, they do not depend on the obsolete netdevice (ioctl) API\. Only the code of referenced inline functions enters in the object and executable code\. .SH "RETURN VALUE" \fBnlinline_if_nametoindex\fR returns the interface index or \-1 if an error occurred (in which case, errno is set appropriately) .P All the other functions return zero in case of success\. On error, \-1 is returned, and errno is set appropriately\. .P (\fBnlinline_iplink_add\fR can return the (positive) ifindex of the newly created link when the argument \fIifindex\fR is \-1 and the stack supports this feature\.) .SH "EXAMPLE" .nf #include #include #include #include int main(int argc, char *argv[]) { uint8_t ipv4addr[] = {192,168,2,2}; uint8_t ipv4gw[] = {192,168,2,1}; uint8_t ipv6addr[16] = {0x20, 0x01, 0x07, 0x60, [15] = 0x02}; uint8_t ipv6gw[16] = {0x20, 0x01, 0x07, 0x60, [15] = 0x01}; int ifindex = nlinline_if_nametoindex(argv[1]); if (ifindex > 0) printf("%d\en", ifindex); else { perror("nametoindex"); return 1; } if (nlinline_linksetupdown(ifindex, 1) < 0) perror("link up"); if (nlinline_ipaddr_add(AF_INET, ipv4addr, 24, ifindex) < 0) perror("addr ipv4"); if (nlinline_iproute_add(AF_INET, NULL, 0, ipv4gw, 0) < 0) perror("addr ipv6"); if (nlinline_ipaddr_add(AF_INET6, ipv6addr, 64, ifindex) < 0) perror("route ipv4"); if (nlinline_iproute_add(AF_INET6, NULL, 0, ipv6gw, 0) < 0) perror("route ipv6"); return 0; } .fi .P This program takes the name of an interface from the command line\. It turns that interface up and sets the interface IPv4 and IPv6 addresses and default routes\. .SH "AUTHOR" VirtualSquare\. Project leader: Renzo Davoli nlinline-0.2.2/man/nlinline.3.ronn000066400000000000000000000146231377256446500167760ustar00rootroot00000000000000# ## NAME nlinline_if_nametoindex, nlinline_linksetupdown, nlinline_ipaddr_add, nlinline_ipaddr_del, nlinline_iproute_add, nlinline_iproute_del, nlinline_iplink_add, nlinline_iplink_del, nlinline_linksetaddr, nlinline_linkgetaddr - configure network interfaces ## SYNOPSIS `#include ` `int nlinline_if_nametoindex(const char *`_ifname_`);` `int nlinline_linksetupdown(unsigned int ` _ifindex_`, int ` _updown_`);` `int nlinline_ipaddr_add(int ` _family_`, void *`_addr_`, int ` _prefixlen_`, unsigned int ` _ifindex_`);` `int nlinline_ipaddr_del(int ` _family_`, void *`_addr_`, int ` _prefixlen_`, unsigned int ` _ifindex_`);` `int nlinline_iproute_add(int ` _family_`, void *`_dst_addr_`, int ` _dst_prefixlen_`, void *`_gw_addr_`, unsigned int ` _ifindex_`);` `int nlinline_iproute_del(int ` _family_`, void *`_dst_addr_`, int ` _dst_prefixlen_`, void *`_gw_addr_`, unsigned int ` _ifindex_`);` `int nlinline_iplink_add(const char *`_ifname_`, unsigned int ` _ifindex_`, const char *`_type_`, const char *`_data_`);` `int nlinline_iplink_del(const char *`_ifname_`, unsigned int ` _ifindex_`);` `int nlinline_linksetaddr(unsigned int ` _ifindex_`, void *`_macaddr_`);` `int nlinline_linkgetaddr(unsigned int ` _ifindex_`, void *`_macaddr_`);` `int nlinline_linksetmtu(unsigned int ` _ifindex_`, unsigned int ` _mtu_`);` ## DESCRIPTION NLINLINE (netlink inline) is a *library* of inline functions providing C programmers with very handy functions to configure network stacks. NLINLINE is entirely implemented in a header file, nlinline.h. * `nlinline_if_nametoindex`: This function returns the index of the network interface whose name is _ifname_. * `nlinline_linksetupdown`: This function turns the interface _ifindex_ up (_updown_ == `1`) or down (_updown_ == `0`). * `nlinline_ipaddr_add`: This function adds an IP address to the interface _ifindex_. It supports IPv4 (_family_ == `AF_INET`) and IPv6 (_family_ == `AF_INET6`). * `nlinline_ipaddr_del`: This function removes the IP address from the interface _ifindex_. It supports IPv4 (_family_ == `AF_INET`) and IPv6 (_family_ == `AF_INET6`). * `nlinline_iproute_add`: This function adds a static route to _dst_addr_/_dst_prefixlen_ network through the gateway _gw_addr_. If _dst_addr_ == `NULL` it adds a default route. _ifindex_ must be specified when _gw_addr_ is an IPv6 link local address. * `nlinline_iproute_del`: This function removes the static route to _dst_addr_/_dst_prefixlen_ network through the gateway _gw_addr_. * `nlinline_iplink_add`: This function adds a new link of type _type_, named _ifname_. The value of _data_ depends on the type of link and can be NULL. A default interface name is assigned if _name_ == `NULL`. The link is created with a given index when _ifindex_ is positive. * `nlinline_iplink_del`: This function removes a link. The link to be deleted can be identified by its name (_ifname_) or by its index (_ifindex_). Either _ifindex_ can be zero or _ifname_ can be `NULL`. It is possible to use both _ifindex_ and _ifname_ to identify the link. An error may occur if the parameters are inconsistent. * `nlinline_linksetaddr`: This functions sets the mac address of the interface _ifindex_. * `nlinline_linkgetaddr`: This functions gets the mac address of the interface _ifindex_. * `nlinline_linksetmtu`: This functions sets the MTU (Maximum Transfer Unit) of the interface _ifindex_. IP addresses are `void *` arguments, any sequence of 4 or 16 bytes (in network byte order) is a legal IPv4 or IPv6 address respectively. `nlinline` functions do not add dependencies at run-time. This is useful for security critical applications (like PAM modules) These inline functions use netlink only, they do not depend on the obsolete netdevice (ioctl) API. Only the code of referenced inline functions enters in the object and executable code. ## RETURN VALUE `nlinline_if_nametoindex` returns the interface index or -1 if an error occurred (in which case, errno is set appropriately) All the other functions return zero in case of success. On error, -1 is returned, and errno is set appropriately. (`nlinline_iplink_add` can return the (positive) ifindex of the newly created link when the argument _ifindex_ is -1 and the stack supports this feature.) ## EXAMPLE ```C #include #include #include #include int main(int argc, char *argv[]) { uint8_t ipv4addr[] = {192,168,2,2}; uint8_t ipv4gw[] = {192,168,2,1}; uint8_t ipv6addr[16] = {0x20, 0x01, 0x07, 0x60, [15] = 0x02}; uint8_t ipv6gw[16] = {0x20, 0x01, 0x07, 0x60, [15] = 0x01}; int ifindex = nlinline_if_nametoindex(argv[1]); if (ifindex > 0) printf("%d\n", ifindex); else { perror("nametoindex"); return 1; } if (nlinline_linksetupdown(ifindex, 1) < 0) perror("link up"); if (nlinline_ipaddr_add(AF_INET, ipv4addr, 24, ifindex) < 0) perror("addr ipv4"); if (nlinline_iproute_add(AF_INET, NULL, 0, ipv4gw, 0) < 0) perror("addr ipv6"); if (nlinline_ipaddr_add(AF_INET6, ipv6addr, 64, ifindex) < 0) perror("route ipv4"); if (nlinline_iproute_add(AF_INET6, NULL, 0, ipv6gw, 0) < 0) perror("route ipv6"); return 0; } ``` This program takes the name of an interface from the command line. It turns that interface up and sets the interface IPv4 and IPv6 addresses and default routes. ## AUTHOR VirtualSquare. Project leader: Renzo Davoli nlinline-0.2.2/man/nlinline_if_nametoindex.3000077700000000000000000000000001377256446500227552nlinline.3ustar00rootroot00000000000000nlinline-0.2.2/man/nlinline_ipaddr_add.3000077700000000000000000000000001377256446500220372nlinline.3ustar00rootroot00000000000000nlinline-0.2.2/man/nlinline_ipaddr_del.3000077700000000000000000000000001377256446500220532nlinline.3ustar00rootroot00000000000000nlinline-0.2.2/man/nlinline_iplink_add.3000077700000000000000000000000001377256446500220622nlinline.3ustar00rootroot00000000000000nlinline-0.2.2/man/nlinline_iplink_del.3000077700000000000000000000000001377256446500220762nlinline.3ustar00rootroot00000000000000nlinline-0.2.2/man/nlinline_iproute_add.3000077700000000000000000000000001377256446500222632nlinline.3ustar00rootroot00000000000000nlinline-0.2.2/man/nlinline_iproute_del.3000077700000000000000000000000001377256446500222772nlinline.3ustar00rootroot00000000000000nlinline-0.2.2/man/nlinline_linkgetaddr.3000077700000000000000000000000001377256446500222542nlinline.3ustar00rootroot00000000000000nlinline-0.2.2/man/nlinline_linksetaddr.3000077700000000000000000000000001377256446500222702nlinline.3ustar00rootroot00000000000000nlinline-0.2.2/man/nlinline_linksetmtu.3000077700000000000000000000000001377256446500221632nlinline.3ustar00rootroot00000000000000nlinline-0.2.2/man/nlinline_linksetupdown.3000077700000000000000000000000001377256446500226722nlinline.3ustar00rootroot00000000000000nlinline-0.2.2/nlinline+.h000066400000000000000000000157361377256446500154160ustar00rootroot00000000000000/* * nlinline plus: compact library of inline function providing the most common * network configuration operations. * * Copyright (C) 2019 Renzo Davoli VirtualSquare team. * * nlinline is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * 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 . * */ #ifndef NLINLINEPLUS_H #define NLINLINEPLUS_H #ifdef NLINLINE_H #error nlinline+.h must be included instead of nlinline.h #else #include #include #include /************************** * Implementation **************************/ typedef int msocket_t (void *, int, int, int); struct _stackinfo { msocket_t *msocket; void *mstack; typeof(socket) *socket; typeof(bind) *bind; typeof(send) *send; typeof(recv) *recv; typeof(close) *close; }; #define __NLINLINE_PLUSTYPE struct _stackinfo #include #define __LIB_STACKINFO(X, Y) \ struct _stackinfo stackinfo = {\ NULL, NULL, \ X ## socket, \ Y ## bind, \ Y ## send, \ Y ## recv, \ Y ## close \ } #define __LIB_NLINLINE(NAME, X, Y) \ static inline int NAME ## if_nametoindex(const char *ifname) {\ __LIB_STACKINFO(X, Y); \ return __nlinline_if_nametoindex(&stackinfo, ifname); \ } \ static inline int NAME ## linksetupdown(unsigned int ifindex, int updown) {\ __LIB_STACKINFO(X, Y); \ return __nlinline_linksetupdown(&stackinfo, ifindex, updown); \ } \ static inline int NAME ## linksetaddr(unsigned int ifindex, void *macaddr) {\ __LIB_STACKINFO(X, Y); \ return __nlinline_linksetaddr(&stackinfo, ifindex, macaddr); \ } \ static inline int NAME ## linkgetaddr(unsigned int ifindex, void *macaddr) {\ __LIB_STACKINFO(X, Y); \ return __nlinline_linkgetaddr(&stackinfo, ifindex, macaddr); \ } \ static inline int NAME ## linksetmtu(unsigned int ifindex, unsigned int mtu) {\ __LIB_STACKINFO(X, Y); \ return __nlinline_linksetmtu(&stackinfo, ifindex, mtu); \ } \ static inline int NAME ## ipaddr_add(int family, void *addr, int prefixlen, unsigned int ifindex) {\ __LIB_STACKINFO(X, Y); \ return __nlinline_ipaddr_add(&stackinfo, family, addr, prefixlen, ifindex); \ } \ static inline int NAME ## ipaddr_del(int family, void *addr, int prefixlen, unsigned int ifindex) {\ __LIB_STACKINFO(X, Y); \ return __nlinline_ipaddr_del(&stackinfo, family, addr, prefixlen, ifindex); \ } \ static inline int NAME ## iproute_add(int family, void *dst_addr, int dst_prefixlen, void *gw_addr, unsigned int ifindex) {\ __LIB_STACKINFO(X, Y); \ return __nlinline_iproute_add(&stackinfo, family, dst_addr, dst_prefixlen, gw_addr, ifindex); \ } \ static inline int NAME ## iproute_del(int family, void *dst_addr, int dst_prefixlen, void *gw_addr, unsigned int ifindex) {\ __LIB_STACKINFO(X, Y); \ return __nlinline_iproute_del(&stackinfo, family, dst_addr, dst_prefixlen, gw_addr, ifindex); \ } \ static inline int NAME ## iplink_add(const char *ifname, unsigned int ifindex, const char *type, const char *data) {\ __LIB_STACKINFO(X, Y); \ return __nlinline_iplink_add(&stackinfo, ifname, ifindex, type, data); \ } \ static inline int NAME ## iplink_del(const char *ifname, unsigned int ifindex) {\ __LIB_STACKINFO(X, Y); \ return __nlinline_iplink_del(&stackinfo, ifname, ifindex); \ } \ static inline int NAME ## nldialog(const char *ifname, void *msg) {\ __LIB_STACKINFO(X, Y); \ return __nlinline_nldialog(&stackinfo, msg); \ } #define NLINLINE_LIB(X) __LIB_NLINLINE(X, X, X) #define NLINLINE_LIBCOMP(X) __LIB_NLINLINE(X, X, ) #define __LIBMULTI_STACKINFO(X, Y, MSTACK) \ struct _stackinfo stackinfo = {\ (msocket_t *) X ## msocket, \ MSTACK, \ NULL, \ Y ## bind, \ Y ## send, \ Y ## recv, \ Y ## close \ } #define __LIBMULTI_NLINLINE(X, Y) \ static inline int X ## if_nametoindex(void *mstack, const char *ifname) {\ __LIBMULTI_STACKINFO(X, Y, mstack); \ return __nlinline_if_nametoindex(&stackinfo, ifname); \ } \ static inline int X ## linksetupdown(void *mstack, unsigned int ifindex, int updown) {\ __LIBMULTI_STACKINFO(X, Y, mstack); \ return __nlinline_linksetupdown(&stackinfo, ifindex, updown); \ } \ static inline int X ## linksetaddr(void *mstack, unsigned int ifindex, void *macaddr) {\ __LIBMULTI_STACKINFO(X, Y, mstack); \ return __nlinline_linksetaddr(&stackinfo, ifindex, macaddr); \ } \ static inline int X ## linkgetaddr(void *mstack, unsigned int ifindex, void *macaddr) {\ __LIBMULTI_STACKINFO(X, Y, mstack); \ return __nlinline_linkgetaddr(&stackinfo, ifindex, macaddr); \ } \ static inline int X ## linksetmtu(void *mstack, unsigned int ifindex, unsigned int mtu) {\ __LIBMULTI_STACKINFO(X, Y, mstack); \ return __nlinline_linksetmtu(&stackinfo, ifindex, mtu); \ } \ static inline int X ## ipaddr_add(void *mstack, int family, void *addr, int prefixlen, int ifindex) {\ __LIBMULTI_STACKINFO(X, Y, mstack); \ return __nlinline_ipaddr_add(&stackinfo, family, addr, prefixlen, ifindex); \ } \ static inline int X ## ipaddr_del(void *mstack, int family, void *addr, int prefixlen, int ifindex) {\ __LIBMULTI_STACKINFO(X, Y, mstack); \ return __nlinline_ipaddr_del(&stackinfo, family, addr, prefixlen, ifindex); \ } \ static inline int X ## iproute_add(void *mstack, int family, void *dst_addr, int dst_prefixlen, void *gw_addr, unsigned int ifindex) {\ __LIBMULTI_STACKINFO(X, Y, mstack); \ return __nlinline_iproute_add(&stackinfo, family, dst_addr, dst_prefixlen, gw_addr, ifindex); \ } \ static inline int X ## iproute_del(void *mstack, int family, void *dst_addr, int dst_prefixlen, void *gw_addr, unsigned int ifindex) {\ __LIBMULTI_STACKINFO(X, Y, mstack); \ return __nlinline_iproute_del(&stackinfo, family, dst_addr, dst_prefixlen, gw_addr, ifindex); \ } \ static inline int X ## iplink_add(void *mstack, const char *ifname, unsigned int ifindex, const char *type, const char *data) {\ __LIBMULTI_STACKINFO(X, Y, mstack); \ return __nlinline_iplink_add(&stackinfo, ifname, ifindex, type, data); \ } \ static inline int X ## iplink_del(void *mstack, const char *ifname, unsigned int ifindex) {\ __LIBMULTI_STACKINFO(X, Y, mstack); \ return __nlinline_iplink_del(&stackinfo, ifname, ifindex); \ } \ static inline int X ## nldialog(void *mstack, void *msg) {\ __LIBMULTI_STACKINFO(X, Y, mstack); \ return __nlinline_nldialog(&stackinfo, msg); \ } #define NLINLINE_LIBMULTI(X) __LIBMULTI_NLINLINE(X, X) #define NLINLINE_LIBMULTICOMP(X) __LIBMULTI_NLINLINE(X, ) /* define the standard inline functions nlinline_... */ __LIB_NLINLINE(nlinline_,,) #endif #endif nlinline-0.2.2/nlinline.h000066400000000000000000000354411377256446500153360ustar00rootroot00000000000000/* * nlinline: compact library of inline function providing the most common * network configuration operations. * * Copyright (C) 2019 Renzo Davoli VirtualSquare team. * * nlinline is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * 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 . * */ #ifndef NLINLINE_H #define NLINLINE_H #include #include #include #include #include #include #include #include #include #include #include static inline int nlinline_if_nametoindex(const char *ifname); static inline int nlinline_linksetupdown(unsigned int ifindex, int updown); static inline int nlinline_linksetaddr(unsigned int ifindex, void *macaddr); static inline int nlinline_linkgetaddr(unsigned int ifindex, void *macaddr); static inline int nlinline_linksetmtu(unsigned int ifindex, unsigned int mtu); static inline int nlinline_ipaddr_add(int family, void *addr, int prefixlen, unsigned int ifindex); static inline int nlinline_ipaddr_del(int family, void *addr, int prefixlen, unsigned int ifindex); static inline int nlinline_iproute_add(int family, void *dst_addr, int dst_prefixlen, void *gw_addr, unsigned int ifindex); static inline int nlinline_iproute_del(int family, void *dst_addr, int dst_prefixlen, void *gw_addr, unsigned int ifindex); static inline int nlinline_iplink_add(const char *ifname, unsigned int ifindex, const char *type, const char *data); static inline int nlinline_iplink_del(const char *ifname, unsigned int ifindex); #ifndef __NLINLINE_PLUSTYPE #define __PLUSARG #define __PLUSF #define __PLUS #define __nlinline_if_nametoindex nlinline_if_nametoindex #define __nlinline_linksetupdown nlinline_linksetupdown #define __nlinline_linksetaddr nlinline_linksetaddr #define __nlinline_linkgetaddr nlinline_linkgetaddr #define __nlinline_linksetmtu nlinline_linksetmtu #define __nlinline_ipaddr_add nlinline_ipaddr_add #define __nlinline_ipaddr_del nlinline_ipaddr_del #define __nlinline_iproute_add nlinline_iproute_add #define __nlinline_iproute_del nlinline_iproute_del #define __nlinline_iplink_add nlinline_iplink_add #define __nlinline_iplink_del nlinline_iplink_del #define __nlinline_nldialog nlinline_nldialog #else #define __PLUSARG __NLINLINE_PLUSTYPE *__stack, #define __PLUSF __stack-> #define __PLUS __stack, #endif /************************** * Implementation **************************/ static inline int nlinline_family2addrlen(int family) { switch (family) { case AF_INET: return 4; case AF_INET6: return 16; default: return 0; __default: return 0; } } static inline int __nlinline_geterror(__PLUSARG int fd) { struct { struct nlmsghdr h; union { struct nlmsgerr e; struct ifinfomsg i; }; } msg; int replylen = __PLUSF recv(fd, &msg, sizeof(msg), 0); if (replylen < 0) return -1; if (replylen <= sizeof(msg.h)) return errno = EFAULT, -1; switch (msg.h.nlmsg_type) { case NLMSG_ERROR: if (msg.e.error >= 0) return msg.e.error; else return errno = -msg.e.error, -1; case RTM_NEWLINK: return msg.i.ifi_index; default: return errno = EFAULT, -1; } } static inline int __nlinline_open_send(__PLUSARG void *msg) { struct nlmsghdr *nlmsg = msg; struct sockaddr_nl sanl = {AF_NETLINK, 0, 0, 0}; int fd; #ifdef __NLINLINE_PLUSTYPE if (__PLUSF msocket) fd = __PLUSF msocket(__PLUSF mstack, AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE); else #endif fd = __PLUSF socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE); if (fd < 0) return fd; if (__PLUSF bind(fd, (struct sockaddr *) &sanl, sizeof(struct sockaddr_nl)) < 0) return __PLUSF close(fd), -1; if (__PLUSF send(fd, msg, nlmsg->nlmsg_len, 0) < 0) return __PLUSF close(fd), -1; return fd; } static inline int __nlinline_nldialog(__PLUSARG void *msg) { int ret_value; int fd = __nlinline_open_send(__PLUS msg); if (fd < 0) return fd; ret_value = __nlinline_geterror(__PLUS fd); __PLUSF close(fd); return ret_value; } static inline int __nlinline_if_nametoindex(__PLUSARG const char *ifname) { struct { struct nlmsghdr h; struct ifinfomsg i; struct nlattr a; char ifname[IFNAMSIZ]; } msg = { .h.nlmsg_len = sizeof(msg.h) + sizeof(msg.i), .h.nlmsg_type = RTM_GETLINK, .h.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK, .h.nlmsg_seq = 1, .a.nla_type = IFLA_IFNAME, }; int namelen = snprintf(msg.ifname, IFNAMSIZ, "%s", ifname); msg.a.nla_len = sizeof(msg.a) + namelen + 1; msg.h.nlmsg_len += (msg.a.nla_len + 3) & ~3; return __nlinline_nldialog(__PLUS &msg); } static inline int __nlinline_linksetupdown(__PLUSARG unsigned int ifindex, int updown) { struct { struct nlmsghdr h; struct ifinfomsg i; } msg = { .h.nlmsg_len = sizeof(msg), .h.nlmsg_type = RTM_SETLINK, .h.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK, .h.nlmsg_seq = 1, .i.ifi_index = ifindex, .i.ifi_flags = (updown) ? IFF_UP : 0, .i.ifi_change=IFF_UP }; return __nlinline_nldialog(__PLUS &msg); } struct __nlinline_macaddr { unsigned char byte[6]; }; struct __nlinline_macattr { struct nlattr h; struct __nlinline_macaddr addr; }; static inline int __nlinline_linksetaddr(__PLUSARG unsigned int ifindex, void *macaddr) { struct { struct nlmsghdr h; struct ifinfomsg i; struct __nlinline_macattr mac; } msg = { .h.nlmsg_len = sizeof(msg), .h.nlmsg_type = RTM_NEWLINK, .h.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK, .h.nlmsg_seq = 1, .i.ifi_index = ifindex, .mac.h.nla_len = sizeof(struct __nlinline_macattr), .mac.h.nla_type = IFLA_ADDRESS, .mac.addr = *((struct __nlinline_macaddr *) macaddr) }; return __nlinline_nldialog(__PLUS &msg); } static inline int __nlinline_linkgetaddr(__PLUSARG unsigned int ifindex, void *macaddr) { struct { struct nlmsghdr h; struct ifinfomsg i; unsigned char attrs[]; } *reply, msg = { .h.nlmsg_len = sizeof(msg), .h.nlmsg_type = RTM_GETLINK, .h.nlmsg_flags = NLM_F_REQUEST, .h.nlmsg_seq = 1, .i.ifi_index = ifindex, }; struct sockaddr_nl sanl = {AF_NETLINK, 0, 0, 0}; int ret_value; int fd = __nlinline_open_send(__PLUS &msg); if (fd < 0) return fd; if ((ret_value = __PLUSF recv(fd, NULL, 0, MSG_PEEK|MSG_TRUNC)) < 0) return close(fd), -1; unsigned char buf[ret_value]; if ((ret_value = __PLUSF recv(fd, buf, ret_value, 0)) < 0) return __PLUSF close(fd), -1; reply = (void *) buf; if (ret_value <= sizeof(msg.h) || ret_value < reply->h.nlmsg_len) return errno = EFAULT, __PLUSF close(fd), -1; if (reply->h.nlmsg_type != RTM_NEWLINK) return errno = ENODEV, __PLUSF close(fd), -1; unsigned char *limit = buf + reply->h.nlmsg_len; unsigned char *scan = reply->attrs; while (scan < limit) { struct nlattr *attr = (void *) scan; if (attr->nla_type == IFLA_ADDRESS && attr->nla_len >= 6) { memcpy(macaddr, attr + 1, 6); return __PLUSF close(fd), 0; } scan += NLMSG_ALIGN(attr->nla_len); } __PLUSF close(fd); return errno = ENOENT, -1; } struct __nlinline_u32 { struct nlattr h; __u32 value; }; static inline int __nlinline_linksetmtu(__PLUSARG unsigned int ifindex, unsigned int mtu) { struct { struct nlmsghdr h; struct ifinfomsg i; struct __nlinline_u32 mtu; } msg = { .h.nlmsg_len = sizeof(msg), .h.nlmsg_type = RTM_NEWLINK, .h.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK, .h.nlmsg_seq = 1, .i.ifi_index = ifindex, .mtu.h.nla_len = sizeof(struct __nlinline_u32), .mtu.h.nla_type = IFLA_MTU, .mtu.value = mtu, }; return __nlinline_nldialog(__PLUS &msg); } struct __nlinline_ipv4addr { unsigned char byte[4]; }; struct __nlinline_ipv6addr { unsigned char byte[16]; }; struct __nlinline_ipv4attr { struct nlattr h; struct __nlinline_ipv4addr addr; }; struct __nlinline_ipv6attr { struct nlattr h; struct __nlinline_ipv6addr addr; }; static inline int __nlinline_ipaddr(__PLUSARG int request, int xflags, int family, void *addr, int prefixlen, unsigned int ifindex) { int addrlen = nlinline_family2addrlen(family); if (addrlen == 0) return errno = EINVAL, -1; else { struct { struct nlmsghdr h; struct ifaddrmsg i; union { struct __nlinline_ipv4attr a4[2]; struct __nlinline_ipv6attr a6[2]; }; } msg = { .h.nlmsg_len = sizeof(msg.h) + sizeof(msg.i), .h.nlmsg_type = request, .h.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | xflags, .h.nlmsg_seq = 1, .i.ifa_family = family, .i.ifa_prefixlen = prefixlen, .i.ifa_scope = RT_SCOPE_UNIVERSE, .i.ifa_index = ifindex}; if (addrlen == 4) { msg.a4[0].h.nla_len = msg.a4[1].h.nla_len = sizeof(struct nlattr) + addrlen; msg.a4[0].h.nla_type = IFA_LOCAL; msg.a4[1].h.nla_type = IFA_ADDRESS; msg.a4[0].addr = msg.a4[1].addr = *((struct __nlinline_ipv4addr *) addr); msg.h.nlmsg_len += 2 * sizeof(msg.a4[0]); } else { msg.a6[0].h.nla_len = msg.a6[1].h.nla_len = sizeof(struct nlattr) + addrlen; msg.a6[0].h.nla_type = IFA_LOCAL; msg.a6[1].h.nla_type = IFA_ADDRESS; msg.a6[0].addr = msg.a6[1].addr = *((struct __nlinline_ipv6addr *) addr); msg.h.nlmsg_len += 2 * sizeof(msg.a6[0]); } return __nlinline_nldialog(__PLUS &msg); } } static inline int __nlinline_ipaddr_add(__PLUSARG int family, void *addr, int prefixlen, unsigned int ifindex) { return __nlinline_ipaddr(__PLUS RTM_NEWADDR, NLM_F_EXCL | NLM_F_CREATE, family, addr, prefixlen, ifindex); } static inline int __nlinline_ipaddr_del(__PLUSARG int family, void *addr, int prefixlen, unsigned int ifindex) { return __nlinline_ipaddr(__PLUS RTM_DELADDR, 0, family, addr, prefixlen, ifindex); } static inline int __nlinline_iproute(__PLUSARG int request, int xflags, int family, void *dst_addr, int dst_prefixlen, void *gw_addr, unsigned int ifindex) { int addrlen = nlinline_family2addrlen(family); if (addrlen == 0) return errno = EINVAL, -1; else { struct { struct nlmsghdr h; struct rtmsg r; struct __nlinline_u32 oif; union { struct __nlinline_ipv4attr a4[2]; struct __nlinline_ipv6attr a6[2]; }; } msg = { .h.nlmsg_len = sizeof(msg.h) + sizeof(msg.r) + sizeof(msg.oif), .h.nlmsg_type = request, .h.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | xflags, .h.nlmsg_seq = 1, .r.rtm_family = family, .r.rtm_dst_len = dst_prefixlen, .r.rtm_table = RT_TABLE_MAIN, .r.rtm_protocol = RTPROT_BOOT, .r.rtm_scope = RT_SCOPE_UNIVERSE, .r.rtm_type = RTN_UNICAST, .oif.h.nla_type = (ifindex == 0) ? RTA_UNSPEC : RTA_OIF, .oif.h.nla_len = sizeof(msg.oif), .oif.value = ifindex, }; int nattr = 0; if (addrlen == 4) { if (dst_prefixlen > 0) { msg.a4[nattr].h.nla_len = sizeof(msg.a4[0]); msg.a4[nattr].h.nla_type = RTA_DST; msg.a4[nattr].addr = *((struct __nlinline_ipv4addr *)dst_addr); nattr++; } msg.a4[nattr].h.nla_len = sizeof(msg.a4[0]); msg.a4[nattr].h.nla_type = RTA_GATEWAY; msg.a4[nattr].addr = *((struct __nlinline_ipv4addr *)gw_addr); nattr++; msg.h.nlmsg_len += nattr * sizeof(msg.a4[0]); } else { if (dst_prefixlen > 0) { msg.a6[nattr].h.nla_len = sizeof(msg.a6[0]); msg.a6[nattr].h.nla_type = RTA_DST; msg.a6[nattr].addr = *((struct __nlinline_ipv6addr *)dst_addr); nattr++; } msg.a6[nattr].h.nla_len = sizeof(msg.a6[0]); msg.a6[nattr].h.nla_type = RTA_GATEWAY; msg.a6[nattr].addr = *((struct __nlinline_ipv6addr *)gw_addr); nattr++; msg.h.nlmsg_len += nattr * sizeof(msg.a6[0]); } return __nlinline_nldialog(__PLUS &msg); } } static inline int __nlinline_iproute_add(__PLUSARG int family, void *dst_addr, int dst_prefixlen, void *gw_addr, unsigned int ifindex) { return __nlinline_iproute(__PLUS RTM_NEWROUTE, NLM_F_EXCL | NLM_F_CREATE, family, dst_addr, dst_prefixlen, gw_addr, ifindex); } static inline int __nlinline_iproute_del(__PLUSARG int family, void *dst_addr, int dst_prefixlen, void *gw_addr, unsigned int ifindex) { return __nlinline_iproute(__PLUS RTM_DELROUTE, 0, family, dst_addr, dst_prefixlen, gw_addr, ifindex); } static inline int __nlinline_add_attr(void *buf, unsigned int type, const char *s) { if (s) { int payloadlen = (strlen(s) + 4) & ~3; int attrlen = sizeof(struct nlattr) + payloadlen; if (buf) { struct nlattr *attr = buf; attr->nla_len = attrlen; attr->nla_type = type; snprintf((char *)(attr + 1), payloadlen, "%s", s); } return attrlen; } else return 0; } static inline int __nlinline_iplink_add(__PLUSARG const char *ifname, unsigned int ifindex, const char *type, const char *data) { int msglen = sizeof(struct nlmsghdr) + sizeof(struct ifinfomsg) + sizeof(struct nlattr) + __nlinline_add_attr(NULL, IFLA_IFNAME, ifname) + __nlinline_add_attr(NULL, IFLA_INFO_KIND, type) + ((ifindex == -1) ? __nlinline_add_attr(NULL, IFLA_NEW_IFINDEX, "") : 0) + __nlinline_add_attr(NULL, IFLA_INFO_DATA, data); unsigned char msgbuf[msglen]; unsigned char *rawmsg = msgbuf; struct { struct nlmsghdr h; struct ifinfomsg i; } *msg = (void *) rawmsg; struct nlattr *info; memset(msgbuf, 0, msglen); msg->h.nlmsg_len = msglen; msg->h.nlmsg_type = RTM_NEWLINK; msg->h.nlmsg_flags = NLM_F_EXCL | NLM_F_CREATE | NLM_F_REQUEST | NLM_F_ACK; msg->h.nlmsg_seq = 1; msg->i.ifi_index = ifindex == -1 ? 0 : ifindex; rawmsg += sizeof(*msg); rawmsg += __nlinline_add_attr(rawmsg, IFLA_IFNAME, ifname); if (ifindex == -1) rawmsg += __nlinline_add_attr(rawmsg, IFLA_NEW_IFINDEX, ""); info = (void *) rawmsg; rawmsg += sizeof(*info); info->nla_type = IFLA_LINKINFO; rawmsg += __nlinline_add_attr(rawmsg, IFLA_INFO_KIND, type); rawmsg += __nlinline_add_attr(rawmsg, IFLA_INFO_DATA, data); info->nla_len = rawmsg - (unsigned char *)info; return __nlinline_nldialog(__PLUS &msgbuf); } static inline int __nlinline_iplink_del(__PLUSARG const char *ifname, unsigned int ifindex) { struct { struct nlmsghdr h; struct ifinfomsg i; struct nlattr a; char ifname[IFNAMSIZ]; } msg = { .h.nlmsg_len = sizeof(msg.h) + sizeof(msg.i), .h.nlmsg_type = RTM_DELLINK, .h.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK, .h.nlmsg_seq = 1, .i.ifi_index = ifindex, .a.nla_type = IFLA_IFNAME, }; if (ifname) { int namelen = snprintf(msg.ifname, IFNAMSIZ, "%s", ifname); msg.a.nla_len = sizeof(msg.a) + namelen + 1; msg.h.nlmsg_len += (msg.a.nla_len + 3) & ~3; } return __nlinline_nldialog(__PLUS &msg); } #endif