├── AUTHORS ├── COPYING ├── INSTALL ├── Makefile ├── README ├── ipt ├── Makefile └── libxt_opendpi.c └── src ├── Makefile ├── main.c └── xt_opendpi.h /AUTHORS: -------------------------------------------------------------------------------- 1 | G. Elian Gidoni 2 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /INSTALL: -------------------------------------------------------------------------------- 1 | OpenDPI module INSTALL 2 | ---------------------- 3 | 4 | The install the modules follow these steps, always from the modules package 5 | root (i.e. where this file is located) 6 | 7 | - To compile the modules first type: 8 | 9 | OPENDPI_PATH=/home/foo/opendpi-1.3.0 make 10 | 11 | Where, OPENDPI_PATH is the path to the original OpenDPI source code package 12 | (note that you have to extract it first). 13 | 14 | - Then, as root, type: 15 | 16 | make modules_install 17 | 18 | That would install the OpenDPI module for the given Linux kernel. 19 | 20 | - Finally, also as root, you should copy the iptables module into the 21 | corresponding directory. Usually this is "/lib/xtables" or 22 | "/usr/lib/xtables". 23 | 24 | cp ipt/libxt_opendpi.so /lib/xtables 25 | 26 | Now you can read the following section to learn how to use it. 27 | 28 | 29 | Module usage 30 | ------------ 31 | 32 | Once you have installed both modules ("libxt_opendpi.so" and "xt_opendpi.ko") 33 | you should type (as root): 34 | 35 | modprobe xt_opendpi 36 | 37 | If the module has been successfully loaded you shouldn't see any message. 38 | After loading the kernel module you can use iptables to add a rule. To see 39 | available protocols you can match for in every packet, type (as root): 40 | 41 | iptables -m opendpi --help 42 | 43 | Note that the list is long, you best try: "iptables -m opendpi --help | more" 44 | 45 | An example rule would be (as root): 46 | 47 | iptables -A OUTPUT -m opendpi --http -j REJECT 48 | 49 | This wouldn't allow any HTTP traffic that originates from the machine where the 50 | rule was added. 51 | 52 | In case you want to stop using the OpenDPI kernel module, first remove every 53 | iptables rule for OpenDPI and then type (as root): 54 | 55 | modprobe -r xt_opendpi 56 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | $(MAKE) -C ipt 3 | $(MAKE) -C src 4 | modules_install: 5 | $(MAKE) -C src modules_install 6 | clean: 7 | $(MAKE) -C src clean 8 | $(MAKE) -C ipt clean -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | This package is a GPL implementation of an iptables and netfilter module for 2 | OpenDPI integration into the Linux kernel. 3 | 4 | The prerequisites are: 5 | 6 | - Tested on Debian 6.0 (Kernel 2.6.32-5) 7 | - Following packages to compile kernel-modules: 8 | linux-headers 9 | iptables-dev >= version 1.4.8-3 10 | OpenDPI source package 11 | 12 | 13 | Compiled kernel features 14 | ------------------------ 15 | 16 | You do not need to do the below steps for Debian 6 17 | 18 | In order to use OpenDPI as a kernel module notice that: 19 | 20 | - You should ENABLE Netfilter conntrack events (and also enable Advanced 21 | netfilter features to see it). 22 | 23 | In kernel 2.6.34 its defined as: 24 | 25 | Connection tracking events 26 | Symbol: NF_CONNTRACK_EVENTS 27 | Location: 28 | -> Networking support 29 | -> Networking options 30 | -> Network packet filtering framework (Netfilter) 31 | -> Core Netfilter Configuration 32 | -> Netfilter connection tracking support 33 | 34 | - If your kernel version is 2.6.31 or greater, then you should DISABLE 35 | Netfilter conntrack for the Netlink protocol. 36 | 37 | In kernel 2.6.34 its defined as: 38 | 39 | Connection tracking netlink interface 40 | Symbol: NF_CT_NETLINK 41 | Location: 42 | -> Networking support 43 | -> Networking options 44 | -> Network packet filtering framework (Netfilter) 45 | -> Core Netfilter Configuration 46 | -> Netfilter connection tracking support 47 | 48 | 49 | Once you have downloaded/installed each package and checked for the above 50 | kernel features you can read the INSTALL file. 51 | -------------------------------------------------------------------------------- /ipt/Makefile: -------------------------------------------------------------------------------- 1 | OPENDPI_PATH2 := ${OPENDPI_PATH}/src 2 | OPENDPI_SRC := opendpi_cpy 3 | OPENDPI_PRO := ${OPENDPI_SRC}/lib/protocols 4 | 5 | CFLAGS = -fPIC -I${OPENDPI_SRC}/include -I${OPENDPI_SRC}/lib -I../src -DOPENDPI_NETFILTER_MODULE -O2 -Wall 6 | 7 | all: 8 | if test -d ${OPENDPI_SRC}; then \ 9 | cp ${OPENDPI_PATH2}/* ${OPENDPI_SRC} -R; \ 10 | else \ 11 | mkdir ${OPENDPI_SRC}; \ 12 | cp ${OPENDPI_PATH2}/* ${OPENDPI_SRC} -R; \ 13 | fi 14 | make libxt_opendpi.so 15 | rm -r ${OPENDPI_SRC} 16 | lib%.so: lib%.o 17 | $(CC) -shared -o $@ $^; 18 | lib%.o: lib%.c 19 | $(CC) ${CFLAGS} -D_INIT=lib$*_init -c -o $@ $<; 20 | clean: 21 | rm -rf libxt_opendpi.so ${OPENDPI_SRC} 22 | -------------------------------------------------------------------------------- /ipt/libxt_opendpi.c: -------------------------------------------------------------------------------- 1 | /* 2 | * libxt_opendpi.c 3 | * Copyright (C) 2010-2012 G. Elian Gidoni 4 | * 5 | * This file is part of OpenDPI, an open source deep packet inspection 6 | * library based on the PACE technology by ipoque GmbH 7 | * 8 | * This program is free software; you can redistribute it and/or 9 | * modify it under the terms of the GNU General Public License 10 | * as published by the Free Software Foundation; version 2 of the License. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 20 | */ 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #include 29 | 30 | #include "xt_opendpi.h" 31 | 32 | static char *prot_long_str[] = { IPOQUE_PROTOCOL_LONG_STRING }; 33 | static char *prot_short_str[] = { IPOQUE_PROTOCOL_SHORT_STRING }; 34 | 35 | static void 36 | opendpi_mt4_save(const void *entry, const struct xt_entry_match *match) 37 | { 38 | const struct xt_opendpi_mtinfo *info = (const void *)match->data; 39 | int i; 40 | 41 | for (i = 1; i <= IPOQUE_MAX_SUPPORTED_PROTOCOLS; i++){ 42 | if (IPOQUE_COMPARE_PROTOCOL_TO_BITMASK(info->flags, i) != 0){ 43 | printf("--%s ", prot_short_str[i]); 44 | } 45 | } 46 | } 47 | 48 | 49 | static void 50 | opendpi_mt4_print(const void *entry, const struct xt_entry_match *match, 51 | int numeric) 52 | { 53 | const struct xt_opendpi_mtinfo *info = (const void *)match->data; 54 | int i; 55 | 56 | for (i = 1; i <= IPOQUE_MAX_SUPPORTED_PROTOCOLS; i++){ 57 | if (IPOQUE_COMPARE_PROTOCOL_TO_BITMASK(info->flags, i) != 0){ 58 | printf("protocol %s ", prot_long_str[i]); 59 | } 60 | } 61 | } 62 | 63 | 64 | static int 65 | opendpi_mt4_parse(int c, char **argv, int invert, unsigned int *flags, 66 | const void *entry, struct xt_entry_match **match) 67 | { 68 | struct xt_opendpi_mtinfo *info = (void *)(*match)->data; 69 | int i; 70 | 71 | *flags = 0; 72 | for (i = 1; i <= IPOQUE_MAX_SUPPORTED_PROTOCOLS; i++){ 73 | if (c == i){ 74 | IPOQUE_ADD_PROTOCOL_TO_BITMASK(info->flags, i); 75 | /*printf("Parameter detected as protocol %s.\n", 76 | prot_long_str[i]);*/ 77 | *flags = 1; 78 | return true; 79 | } 80 | } 81 | 82 | return false; 83 | } 84 | 85 | #ifndef xtables_error 86 | #define xtables_error exit_error 87 | #endif 88 | 89 | static void 90 | opendpi_mt_check (unsigned int flags) 91 | { 92 | if (flags == 0){ 93 | xtables_error(PARAMETER_PROBLEM, "xt_opendpi: You need to " 94 | "specify at least one protocol"); 95 | } 96 | } 97 | 98 | 99 | static void 100 | opendpi_mt_help(void) 101 | { 102 | int i; 103 | 104 | printf("opendpi match options:\n"); 105 | for (i = 1; i <= IPOQUE_MAX_SUPPORTED_PROTOCOLS; i++){ 106 | printf("--%s Match for %s protocol packets.\n", 107 | prot_short_str[i], prot_long_str[i]); 108 | } 109 | } 110 | 111 | 112 | static void 113 | opendpi_mt_init (struct xt_entry_match *match) 114 | { 115 | struct xt_opendpi_mtinfo *info = (void *)match->data; 116 | /* inet_pton(PF_INET, "192.0.2.137", &info->dst.in); */ 117 | } 118 | 119 | 120 | static struct option opendpi_mt_opts[IPOQUE_MAX_SUPPORTED_PROTOCOLS+1]; 121 | 122 | static struct xtables_match 123 | opendpi_mt4_reg = { 124 | .version = XTABLES_VERSION, 125 | .name = "opendpi", 126 | .revision = 0, 127 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28) 128 | .family = AF_INET, 129 | #else 130 | .family = NFPROTO_IPV4, 131 | #endif 132 | .size = XT_ALIGN(sizeof(struct xt_opendpi_mtinfo)), 133 | .userspacesize = XT_ALIGN(sizeof(struct xt_opendpi_mtinfo)), 134 | .help = opendpi_mt_help, 135 | .init = opendpi_mt_init, 136 | .parse = opendpi_mt4_parse, 137 | .final_check = opendpi_mt_check, 138 | .print = opendpi_mt4_print, 139 | .save = opendpi_mt4_save, 140 | .extra_opts = opendpi_mt_opts, 141 | }; 142 | 143 | void _init(void) 144 | { 145 | int i; 146 | 147 | for (i = 0; i < IPOQUE_MAX_SUPPORTED_PROTOCOLS; i++){ 148 | opendpi_mt_opts[i].name = prot_short_str[i+1]; 149 | opendpi_mt_opts[i].has_arg = false; 150 | opendpi_mt_opts[i].val = i+1; 151 | } 152 | opendpi_mt_opts[i].name = NULL; 153 | opendpi_mt_opts[i].flag = NULL; 154 | opendpi_mt_opts[i].has_arg = 0; 155 | opendpi_mt_opts[i].val = 0; 156 | 157 | xtables_register_match(&opendpi_mt4_reg); 158 | } 159 | -------------------------------------------------------------------------------- /src/Makefile: -------------------------------------------------------------------------------- 1 | OPENDPI_PATH2 := ${OPENDPI_PATH}/src 2 | OPENDPI_SRC := opendpi_cpy 3 | OPENDPI_PRO := ${OPENDPI_SRC}/lib/protocols 4 | 5 | ccflags-y += -I${src}/${OPENDPI_SRC}/include -I${src}/${OPENDPI_SRC}/lib -DOPENDPI_NETFILTER_MODULE 6 | 7 | #MODULES_DIR := /lib/modules/$(shell uname -r) 8 | #KERNEL_DIR := ${MODULES_DIR}/build 9 | 10 | obj-m := xt_opendpi.o 11 | xt_opendpi-y := main.o ${OPENDPI_SRC}/lib/ipq_main.o \ 12 | ${OPENDPI_PRO}/afp.o ${OPENDPI_PRO}/dns.o ${OPENDPI_PRO}/halflife2_and_mods.o \ 13 | ${OPENDPI_PRO}/kontiki.o ${OPENDPI_PRO}/msn.o ${OPENDPI_PRO}/pcanywhere.o \ 14 | ${OPENDPI_PRO}/rtsp.o ${OPENDPI_PRO}/ssdp.o ${OPENDPI_PRO}/tftp.o \ 15 | ${OPENDPI_PRO}/xbox.o ${OPENDPI_PRO}/aimini.o ${OPENDPI_PRO}/edonkey.o \ 16 | ${OPENDPI_PRO}/http.o ${OPENDPI_PRO}/mail_imap.o ${OPENDPI_PRO}/mysql.o \ 17 | ${OPENDPI_PRO}/popo.o ${OPENDPI_PRO}/secondlife.o ${OPENDPI_PRO}/ssh.o \ 18 | ${OPENDPI_PRO}/thunder.o ${OPENDPI_PRO}/xdmcp.o ${OPENDPI_PRO}/applejuice.o \ 19 | ${OPENDPI_PRO}/fasttrack.o ${OPENDPI_PRO}/i23v5.o ${OPENDPI_PRO}/mail_pop.o \ 20 | ${OPENDPI_PRO}/netbios.o ${OPENDPI_PRO}/postgres.o ${OPENDPI_PRO}/shoutcast.o \ 21 | ${OPENDPI_PRO}/ssl.o ${OPENDPI_PRO}/tvants.o ${OPENDPI_PRO}/yahoo.o \ 22 | ${OPENDPI_PRO}/battlefield.o ${OPENDPI_PRO}/feidian.o ${OPENDPI_PRO}/iax.o \ 23 | ${OPENDPI_PRO}/mail_smtp.o ${OPENDPI_PRO}/nfs.o ${OPENDPI_PRO}/pplive.o \ 24 | ${OPENDPI_PRO}/sip.o ${OPENDPI_PRO}/stealthnet.o ${OPENDPI_PRO}/tvuplayer.o \ 25 | ${OPENDPI_PRO}/zattoo.o ${OPENDPI_PRO}/bgp.o ${OPENDPI_PRO}/filetopia.o \ 26 | ${OPENDPI_PRO}/icecast.o ${OPENDPI_PRO}/manolito.o ${OPENDPI_PRO}/non_tcp_udp.o \ 27 | ${OPENDPI_PRO}/ppstream.o ${OPENDPI_PRO}/smb.o ${OPENDPI_PRO}/steam.o \ 28 | ${OPENDPI_PRO}/usenet.o ${OPENDPI_PRO}/bittorrent.o ${OPENDPI_PRO}/flash.o \ 29 | ${OPENDPI_PRO}/imesh.o ${OPENDPI_PRO}/mdns.o ${OPENDPI_PRO}/ntp.o \ 30 | ${OPENDPI_PRO}/qq.o ${OPENDPI_PRO}/snmp.o ${OPENDPI_PRO}/stun.o \ 31 | ${OPENDPI_PRO}/veohtv.o ${OPENDPI_PRO}/dhcp.o ${OPENDPI_PRO}/ftp.o \ 32 | ${OPENDPI_PRO}/ipp.o ${OPENDPI_PRO}/meebo.o ${OPENDPI_PRO}/openft.o \ 33 | ${OPENDPI_PRO}/quake.o ${OPENDPI_PRO}/socrates.o ${OPENDPI_PRO}/syslog.o \ 34 | ${OPENDPI_PRO}/vnc.o ${OPENDPI_PRO}/directconnect.o ${OPENDPI_PRO}/gadu_gadu.o \ 35 | ${OPENDPI_PRO}/irc.o ${OPENDPI_PRO}/mgcp.o ${OPENDPI_PRO}/oscar.o \ 36 | ${OPENDPI_PRO}/rdp.o ${OPENDPI_PRO}/sopcast.o ${OPENDPI_PRO}/tds.o \ 37 | ${OPENDPI_PRO}/winmx.o ${OPENDPI_PRO}/directdownloadlink.o ${OPENDPI_PRO}/gnutella.o \ 38 | ${OPENDPI_PRO}/jabber.o ${OPENDPI_PRO}/mms.o ${OPENDPI_PRO}/pando.o \ 39 | ${OPENDPI_PRO}/rtp.o ${OPENDPI_PRO}/soulseek.o ${OPENDPI_PRO}/telnet.o \ 40 | ${OPENDPI_PRO}/world_of_warcraft.o ${OPENDPI_PRO}/maplestory.o \ 41 | ${OPENDPI_PRO}/ldap.o ${OPENDPI_PRO}/dhcpv6.o \ 42 | ${OPENDPI_PRO}/dofus.o ${OPENDPI_PRO}/kerberos.o \ 43 | ${OPENDPI_PRO}/pptp.o ${OPENDPI_PRO}/warcraft3.o \ 44 | ${OPENDPI_PRO}/http_activesync.o ${OPENDPI_PRO}/armagetron.o \ 45 | ${OPENDPI_PRO}/florensia.o ${OPENDPI_PRO}/world_of_kung_fu.o \ 46 | ${OPENDPI_PRO}/mssql.o ${OPENDPI_PRO}/guildwars.o \ 47 | ${OPENDPI_PRO}/crossfire.o ${OPENDPI_PRO}/fiesta.o ${OPENDPI_PRO}/hf.o \ 48 | ${OPENDPI_PRO}/pt11.o ${OPENDPI_PRO}/funshion.o ${OPENDPI_PRO}/pptv.o \ 49 | ${OPENDPI_PRO}/counterstrike.o ${OPENDPI_PRO}/aliwangwang.o 50 | 51 | all: 52 | if test -d ${OPENDPI_SRC}; then \ 53 | cp ${OPENDPI_PATH2}/* ${OPENDPI_SRC} -R; \ 54 | else \ 55 | mkdir ${OPENDPI_SRC}; \ 56 | cp ${OPENDPI_PATH2}/* ${OPENDPI_SRC} -R; \ 57 | fi 58 | make -C ${KERNEL_DIR} M=$$PWD; 59 | rm -r ${OPENDPI_SRC} 60 | modules: 61 | make -C ${KERNEL_DIR} M=$$PWD $@; 62 | modules_install: 63 | make -C ${KERNEL_DIR} M=$$PWD $@; 64 | depmod -a; 65 | clean: 66 | make -C ${KERNEL_DIR} M=$$PWD $@; 67 | rm -rf modules.order ${OPENDPI_SRC} 68 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * main.c 3 | * Copyright (C) 2010-2012 G. Elian Gidoni 4 | * 5 | * This file is part of OpenDPI, an open source deep packet inspection 6 | * library based on the PACE technology by ipoque GmbH 7 | * 8 | * This program is free software; you can redistribute it and/or 9 | * modify it under the terms of the GNU General Public License 10 | * as published by the Free Software Foundation; version 2 of the License. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 20 | */ 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | #include 37 | #include 38 | 39 | #include "ipq_api.h" 40 | #include "xt_opendpi.h" 41 | 42 | MODULE_LICENSE("GPL"); 43 | MODULE_AUTHOR("G. Elian Gidoni "); 44 | MODULE_DESCRIPTION("OpenDPI wrapper"); 45 | MODULE_ALIAS("ipt_opendpi"); 46 | 47 | /* flow tracking */ 48 | struct osdpi_flow_node { 49 | struct rb_node node; 50 | struct nf_conn * ct; 51 | /* result only, not used for flow identification */ 52 | u32 detected_protocol; 53 | /* last pointer assigned at run time */ 54 | struct ipoque_flow_struct *ipoque_flow; 55 | }; 56 | 57 | /* id tracking */ 58 | struct osdpi_id_node { 59 | struct rb_node node; 60 | struct kref refcnt; 61 | union nf_inet_addr ip; 62 | /* last pointer assigned at run time */ 63 | struct ipoque_id_struct *ipoque_id; 64 | }; 65 | 66 | static u32 size_id_struct = 0; 67 | static u32 size_flow_struct = 0; 68 | 69 | static struct rb_root osdpi_flow_root = RB_ROOT; 70 | static struct rb_root osdpi_id_root = RB_ROOT; 71 | 72 | static struct kmem_cache *osdpi_flow_cache __read_mostly; 73 | static struct kmem_cache *osdpi_id_cache __read_mostly; 74 | 75 | static IPOQUE_PROTOCOL_BITMASK protocols_bitmask; 76 | static atomic_t protocols_cnt[IPOQUE_MAX_SUPPORTED_PROTOCOLS]; 77 | 78 | DEFINE_SPINLOCK(flow_lock); 79 | DEFINE_SPINLOCK(id_lock); 80 | DEFINE_SPINLOCK(ipq_lock); 81 | 82 | 83 | /* detection */ 84 | static struct ipoque_detection_module_struct *ipoque_struct = NULL; 85 | static u32 detection_tick_resolution = 1000; 86 | 87 | 88 | static void debug_printf(u32 protocol, void *id_struct, 89 | ipq_log_level_t log_level, const char *format, ...) 90 | { 91 | /* do nothing */ 92 | } 93 | 94 | 95 | static void *malloc_wrapper(unsigned long size) 96 | { 97 | return kmalloc(size, GFP_KERNEL); 98 | } 99 | 100 | 101 | static void free_wrapper(void *freeable) 102 | { 103 | kfree(freeable); 104 | } 105 | 106 | static struct osdpi_flow_node * 107 | opendpi_flow_search(struct rb_root *root, struct nf_conn *ct) 108 | { 109 | struct osdpi_flow_node *data; 110 | struct rb_node *node = root->rb_node; 111 | 112 | while (node) { 113 | data = rb_entry(node, struct osdpi_flow_node, node); 114 | 115 | if (ct < data->ct) 116 | node = node->rb_left; 117 | else if (ct > data->ct) 118 | node = node->rb_right; 119 | else 120 | return data; 121 | } 122 | 123 | return NULL; 124 | } 125 | 126 | 127 | static int 128 | opendpi_flow_insert(struct rb_root *root, struct osdpi_flow_node *data) 129 | { 130 | struct osdpi_flow_node *this; 131 | struct rb_node **new = &(root->rb_node), *parent = NULL; 132 | 133 | while (*new) { 134 | this = rb_entry(*new, struct osdpi_flow_node, node); 135 | 136 | parent = *new; 137 | if (data->ct < this->ct) 138 | new = &((*new)->rb_left); 139 | else if (data->ct > this->ct) 140 | new = &((*new)->rb_right); 141 | else 142 | return 0; 143 | } 144 | rb_link_node(&data->node, parent, new); 145 | rb_insert_color(&data->node, root); 146 | 147 | return 1; 148 | } 149 | 150 | 151 | static struct osdpi_id_node * 152 | opendpi_id_search(struct rb_root *root, union nf_inet_addr *ip) 153 | { 154 | int res; 155 | struct osdpi_id_node *data; 156 | struct rb_node *node = root->rb_node; 157 | 158 | while (node) { 159 | data = rb_entry(node, struct osdpi_id_node, node); 160 | res = memcmp(ip, &data->ip, sizeof(union nf_inet_addr)); 161 | 162 | if (res < 0) 163 | node = node->rb_left; 164 | else if (res > 0) 165 | node = node->rb_right; 166 | else 167 | return data; 168 | } 169 | 170 | return NULL; 171 | } 172 | 173 | 174 | static int 175 | opendpi_id_insert(struct rb_root *root, struct osdpi_id_node *data) 176 | { 177 | int res; 178 | struct osdpi_id_node *this; 179 | struct rb_node **new = &(root->rb_node), *parent = NULL; 180 | 181 | while (*new) { 182 | this = rb_entry(*new, struct osdpi_id_node, node); 183 | res = memcmp(&data->ip, &this->ip, sizeof(union nf_inet_addr)); 184 | 185 | parent = *new; 186 | if (res < 0) 187 | new = &((*new)->rb_left); 188 | else if (res > 0) 189 | new = &((*new)->rb_right); 190 | else 191 | return 0; 192 | } 193 | rb_link_node(&data->node, parent, new); 194 | rb_insert_color(&data->node, root); 195 | 196 | return 1; 197 | } 198 | 199 | 200 | static void 201 | opendpi_id_release(struct kref *kref) 202 | { 203 | struct osdpi_id_node * id; 204 | 205 | id = container_of (kref, struct osdpi_id_node, refcnt); 206 | rb_erase(&id->node, &osdpi_id_root); 207 | kmem_cache_free (osdpi_id_cache, id); 208 | } 209 | 210 | 211 | static struct osdpi_flow_node * 212 | opendpi_alloc_flow (struct nf_conn * ct) 213 | { 214 | struct osdpi_flow_node *flow; 215 | 216 | spin_lock_bh (&flow_lock); 217 | flow = opendpi_flow_search (&osdpi_flow_root, ct); 218 | if (flow != NULL){ 219 | spin_unlock_bh (&flow_lock); 220 | return flow; 221 | } 222 | flow = kmem_cache_zalloc (osdpi_flow_cache, GFP_ATOMIC); 223 | if (flow == NULL){ 224 | pr_err("xt_opendpi: couldn't allocate new flow.\n"); 225 | spin_unlock_bh (&flow_lock); 226 | return NULL; 227 | } 228 | flow->ct = ct; 229 | flow->ipoque_flow = (struct ipoque_flow_struct *) 230 | ((char*)&flow->ipoque_flow+sizeof(flow->ipoque_flow)); 231 | opendpi_flow_insert (&osdpi_flow_root, flow); 232 | spin_unlock_bh (&flow_lock); 233 | 234 | return flow; 235 | } 236 | 237 | 238 | static void 239 | opendpi_free_flow (struct nf_conn * ct) 240 | { 241 | struct osdpi_flow_node * flow; 242 | 243 | spin_lock_bh (&flow_lock); 244 | flow = opendpi_flow_search (&osdpi_flow_root, ct); 245 | if (flow != NULL){ 246 | rb_erase (&flow->node, &osdpi_flow_root); 247 | kmem_cache_free (osdpi_flow_cache, flow); 248 | } 249 | spin_unlock_bh (&flow_lock); 250 | } 251 | 252 | 253 | static struct osdpi_id_node * 254 | opendpi_alloc_id (union nf_inet_addr * ip) 255 | { 256 | struct osdpi_id_node *id; 257 | 258 | spin_lock_bh (&id_lock); 259 | id = opendpi_id_search (&osdpi_id_root, ip); 260 | if (id != NULL){ 261 | kref_get (&id->refcnt); 262 | }else{ 263 | id = kmem_cache_zalloc (osdpi_id_cache, GFP_ATOMIC); 264 | 265 | if (id == NULL){ 266 | pr_err("xt_opendpi: couldn't allocate new id.\n"); 267 | spin_unlock_bh (&id_lock); 268 | return NULL; 269 | } 270 | memcpy(&id->ip, ip, sizeof(union nf_inet_addr)); 271 | id->ipoque_id = (struct ipoque_id_struct *) 272 | ((char*)&id->ipoque_id+sizeof(id->ipoque_id)); 273 | kref_init (&id->refcnt); 274 | opendpi_id_insert (&osdpi_id_root, id); 275 | } 276 | spin_unlock_bh (&id_lock); 277 | 278 | return id; 279 | } 280 | 281 | 282 | static void 283 | opendpi_free_id (union nf_inet_addr * ip) 284 | { 285 | struct osdpi_id_node *id; 286 | 287 | spin_lock_bh (&id_lock); 288 | id = opendpi_id_search (&osdpi_id_root, ip); 289 | if (id != NULL) 290 | kref_put (&id->refcnt, opendpi_id_release); 291 | spin_unlock_bh (&id_lock); 292 | } 293 | 294 | 295 | static void 296 | opendpi_enable_protocols (const struct xt_opendpi_mtinfo*info) 297 | { 298 | int i; 299 | 300 | for (i = 1; i <= IPOQUE_MAX_SUPPORTED_PROTOCOLS; i++){ 301 | if (IPOQUE_COMPARE_PROTOCOL_TO_BITMASK(info->flags, i) != 0){ 302 | spin_lock_bh (&ipq_lock); 303 | atomic_inc(&protocols_cnt[i-1]); 304 | IPOQUE_ADD_PROTOCOL_TO_BITMASK(protocols_bitmask, i); 305 | ipoque_set_protocol_detection_bitmask2 306 | (ipoque_struct,&protocols_bitmask); 307 | spin_unlock_bh (&ipq_lock); 308 | } 309 | } 310 | } 311 | 312 | 313 | static void 314 | opendpi_disable_protocols (const struct xt_opendpi_mtinfo*info) 315 | { 316 | int i; 317 | 318 | for (i = 1; i <= IPOQUE_MAX_SUPPORTED_PROTOCOLS; i++){ 319 | if (IPOQUE_COMPARE_PROTOCOL_TO_BITMASK(info->flags, i) != 0){ 320 | spin_lock_bh (&ipq_lock); 321 | if (atomic_dec_and_test(&protocols_cnt[i-1])){ 322 | IPOQUE_DEL_PROTOCOL_FROM_BITMASK(protocols_bitmask, i); 323 | ipoque_set_protocol_detection_bitmask2 324 | (ipoque_struct, &protocols_bitmask); 325 | } 326 | spin_unlock_bh (&ipq_lock); 327 | } 328 | } 329 | } 330 | 331 | 332 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28) 333 | static int 334 | opendpi_conntrack_event (struct notifier_block *this, unsigned long ev, 335 | void * data) 336 | { 337 | struct nf_conn * ct = (struct nf_conn *) data; 338 | union nf_inet_addr *src, *dst; 339 | 340 | if (ct == &nf_conntrack_untracked) 341 | return NOTIFY_DONE; 342 | 343 | if (ev & IPCT_DESTROY){ 344 | src = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3; 345 | dst = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3; 346 | 347 | opendpi_free_id (src); 348 | opendpi_free_id (dst); 349 | opendpi_free_flow (ct); 350 | } 351 | 352 | return NOTIFY_DONE; 353 | } 354 | 355 | static struct notifier_block 356 | osdpi_notifier = { 357 | .notifier_call = opendpi_conntrack_event, 358 | }; 359 | 360 | #else 361 | static int 362 | opendpi_conntrack_event(unsigned int events, struct nf_ct_event *item) 363 | { 364 | struct nf_conn * ct = item->ct; 365 | union nf_inet_addr *src, *dst; 366 | 367 | if (ct == &nf_conntrack_untracked) 368 | return 0; 369 | 370 | if (events & (1 << IPCT_DESTROY)){ 371 | src = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3; 372 | dst = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3; 373 | 374 | opendpi_free_id (src); 375 | opendpi_free_id (dst); 376 | opendpi_free_flow (ct); 377 | } 378 | 379 | return 0; 380 | } 381 | 382 | static struct nf_ct_event_notifier 383 | osdpi_notifier = { 384 | .fcn = opendpi_conntrack_event, 385 | }; 386 | 387 | #endif 388 | 389 | 390 | 391 | static u32 392 | opendpi_process_packet(struct nf_conn * ct, const uint64_t time, 393 | const struct iphdr *iph, uint16_t ipsize) 394 | { 395 | u32 proto = IPOQUE_PROTOCOL_UNKNOWN; 396 | union nf_inet_addr *ipsrc, *ipdst; 397 | struct osdpi_id_node *src, *dst; 398 | struct osdpi_flow_node * flow; 399 | 400 | spin_lock_bh (&flow_lock); 401 | flow = opendpi_flow_search (&osdpi_flow_root, ct); 402 | spin_unlock_bh (&flow_lock); 403 | if (flow == NULL){ 404 | flow = opendpi_alloc_flow(ct); 405 | if (flow == NULL) 406 | return proto; 407 | } 408 | 409 | ipsrc = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3; 410 | 411 | spin_lock_bh (&id_lock); 412 | src = opendpi_id_search (&osdpi_id_root, ipsrc); 413 | spin_unlock_bh (&id_lock); 414 | if (src == NULL) { 415 | src = opendpi_alloc_id(ipsrc); 416 | if (src == NULL) 417 | return proto; 418 | } 419 | 420 | ipdst = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3; 421 | 422 | spin_lock_bh (&id_lock); 423 | dst = opendpi_id_search (&osdpi_id_root, ipdst); 424 | spin_unlock_bh (&id_lock); 425 | if (dst == NULL) { 426 | dst = opendpi_alloc_id(ipdst); 427 | if (dst == NULL) 428 | return proto; 429 | } 430 | 431 | /* here the actual detection is performed */ 432 | spin_lock_bh (&ipq_lock); 433 | proto = ipoque_detection_process_packet(ipoque_struct,flow->ipoque_flow, 434 | (uint8_t *) iph, ipsize, time, 435 | src->ipoque_id, dst->ipoque_id); 436 | flow->detected_protocol = proto; 437 | spin_unlock_bh (&ipq_lock); 438 | 439 | return proto; 440 | } 441 | 442 | 443 | 444 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28) 445 | static bool 446 | opendpi_mt (const struct sk_buff *skb, 447 | const struct net_device *in, 448 | const struct net_device *out, 449 | const struct xt_match *match, 450 | const void *matchinfo, 451 | int offset, 452 | unsigned int protoff, 453 | bool *hotdrop) 454 | 455 | #elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35) 456 | static bool 457 | opendpi_mt(const struct sk_buff *skb, const struct xt_match_param *par) 458 | #else 459 | static bool 460 | opendpi_mt(const struct sk_buff *skb, struct xt_action_param *par) 461 | #endif 462 | { 463 | u32 proto; 464 | u64 time; 465 | 466 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28) 467 | const struct xt_opendpi_mtinfo *info = matchinfo; 468 | #else 469 | const struct xt_opendpi_mtinfo *info = par->matchinfo; 470 | #endif 471 | 472 | enum ip_conntrack_info ctinfo; 473 | struct nf_conn * ct; 474 | struct timeval tv; 475 | struct sk_buff *linearized_skb = NULL; 476 | const struct sk_buff *skb_use = NULL; 477 | 478 | if (nf_ct_is_untracked(skb)){ 479 | pr_info ("xt_opendpi: ignoring untracked sk_buff.\n"); 480 | return false; 481 | } 482 | 483 | if (skb_is_nonlinear(skb)){ 484 | linearized_skb = skb_copy(skb, GFP_ATOMIC); 485 | if (linearized_skb == NULL) { 486 | pr_info ("xt_opendpi: linearization failed.\n"); 487 | return false; 488 | } 489 | skb_use = linearized_skb; 490 | } else { 491 | skb_use = skb; 492 | } 493 | 494 | ct = nf_ct_get (skb_use, &ctinfo); 495 | if (ct == NULL){ 496 | 497 | if(linearized_skb != NULL){ 498 | kfree_skb(linearized_skb); 499 | } 500 | 501 | return false; 502 | } 503 | do_gettimeofday(&tv); 504 | 505 | time = ((uint64_t) tv.tv_sec) * detection_tick_resolution + 506 | tv.tv_usec / (1000000 / detection_tick_resolution); 507 | 508 | /* process the packet */ 509 | proto = opendpi_process_packet(ct, time, ip_hdr(skb_use), skb_use->len); 510 | 511 | if(linearized_skb != NULL){ 512 | kfree_skb(linearized_skb); 513 | } 514 | 515 | 516 | if (IPOQUE_COMPARE_PROTOCOL_TO_BITMASK(info->flags,proto) != 0) 517 | return true; 518 | 519 | return false; 520 | } 521 | 522 | 523 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28) 524 | static bool 525 | opendpi_mt_check(const char *tablename, 526 | const void *ip, 527 | const struct xt_match *match, 528 | void *matchinfo, 529 | unsigned int hook_mask) 530 | 531 | { 532 | const struct xt_opendpi_mtinfo *info = matchinfo; 533 | 534 | if (IPOQUE_BITMASK_IS_ZERO(info->flags)){ 535 | pr_info("None selected protocol.\n"); 536 | return false; 537 | } 538 | 539 | opendpi_enable_protocols (info); 540 | 541 | return nf_ct_l3proto_try_module_get (match->family) == 0; 542 | } 543 | 544 | #elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35) 545 | static bool 546 | opendpi_mt_check(const struct xt_mtchk_param *par) 547 | { 548 | const struct xt_opendpi_mtinfo *info = par->matchinfo; 549 | 550 | if (IPOQUE_BITMASK_IS_ZERO(info->flags)){ 551 | pr_info("None selected protocol.\n"); 552 | return false; 553 | } 554 | 555 | opendpi_enable_protocols (info); 556 | 557 | return nf_ct_l3proto_try_module_get (par->family) == 0; 558 | } 559 | #else 560 | static int 561 | opendpi_mt_check(const struct xt_mtchk_param *par) 562 | { 563 | const struct xt_opendpi_mtinfo *info = par->matchinfo; 564 | 565 | if (IPOQUE_BITMASK_IS_ZERO(info->flags)){ 566 | pr_info("None selected protocol.\n"); 567 | return -EINVAL; 568 | } 569 | 570 | opendpi_enable_protocols (info); 571 | 572 | return nf_ct_l3proto_try_module_get (par->family); 573 | } 574 | #endif 575 | 576 | 577 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28) 578 | static void 579 | opendpi_mt_destroy (const struct xt_match *match, void *matchinfo) 580 | { 581 | const struct xt_opendpi_mtinfo *info = matchinfo; 582 | 583 | opendpi_disable_protocols (info); 584 | nf_ct_l3proto_module_put (match->family); 585 | } 586 | 587 | #else 588 | static void 589 | opendpi_mt_destroy (const struct xt_mtdtor_param *par) 590 | { 591 | const struct xt_opendpi_mtinfo *info = par->matchinfo; 592 | 593 | opendpi_disable_protocols (info); 594 | nf_ct_l3proto_module_put (par->family); 595 | } 596 | 597 | #endif 598 | 599 | 600 | 601 | static void opendpi_cleanup(void) 602 | { 603 | struct rb_node * next; 604 | struct osdpi_id_node *id; 605 | struct osdpi_flow_node *flow; 606 | 607 | ipoque_exit_detection_module(ipoque_struct, free_wrapper); 608 | 609 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3,2,0) 610 | nf_conntrack_unregister_notifier (&osdpi_notifier); 611 | #else 612 | nf_conntrack_unregister_notifier (&init_net,&osdpi_notifier); 613 | #endif 614 | 615 | /* free all objects before destroying caches */ 616 | next = rb_first(&osdpi_flow_root); 617 | while (next){ 618 | flow = rb_entry(next, struct osdpi_flow_node, node); 619 | next = rb_next(&flow->node); 620 | rb_erase(&flow->node, &osdpi_flow_root); 621 | kmem_cache_free (osdpi_flow_cache, flow); 622 | } 623 | kmem_cache_destroy (osdpi_flow_cache); 624 | 625 | next = rb_first(&osdpi_id_root); 626 | while (next){ 627 | id = rb_entry(next, struct osdpi_id_node, node); 628 | next = rb_next(&id->node); 629 | rb_erase(&id->node, &osdpi_id_root); 630 | kmem_cache_free (osdpi_id_cache, id); 631 | } 632 | kmem_cache_destroy (osdpi_id_cache); 633 | } 634 | 635 | 636 | static struct xt_match 637 | opendpi_mt_reg __read_mostly = { 638 | .name = "opendpi", 639 | .revision = 0, 640 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28) 641 | .family = AF_INET, 642 | #else 643 | .family = NFPROTO_IPV4, 644 | #endif 645 | .match = opendpi_mt, 646 | .checkentry = opendpi_mt_check, 647 | .destroy = opendpi_mt_destroy, 648 | .matchsize = sizeof(struct xt_opendpi_mtinfo), 649 | .me = THIS_MODULE, 650 | }; 651 | 652 | 653 | static int __init opendpi_mt_init(void) 654 | { 655 | int ret, i; 656 | 657 | pr_info("xt_opendpi 0.1 (OpenDPI wrapper module).\n"); 658 | /* init global detection structure */ 659 | ipoque_struct = ipoque_init_detection_module(detection_tick_resolution, 660 | malloc_wrapper, debug_printf); 661 | if (ipoque_struct == NULL) { 662 | pr_err("xt_opendpi: global structure initialization failed.\n"); 663 | ret = -ENOMEM; 664 | goto err_out; 665 | } 666 | 667 | for (i = 0; i < IPOQUE_MAX_SUPPORTED_PROTOCOLS; i++){ 668 | atomic_set (&protocols_cnt[i], 0); 669 | } 670 | 671 | /* disable all protocols */ 672 | IPOQUE_BITMASK_RESET(protocols_bitmask); 673 | ipoque_set_protocol_detection_bitmask2(ipoque_struct, &protocols_bitmask); 674 | 675 | /* allocate memory for id and flow tracking */ 676 | size_id_struct = ipoque_detection_get_sizeof_ipoque_id_struct(); 677 | size_flow_struct = ipoque_detection_get_sizeof_ipoque_flow_struct(); 678 | 679 | osdpi_flow_cache = kmem_cache_create("xt_opendpi_flows", 680 | sizeof(struct osdpi_flow_node) + 681 | size_flow_struct, 682 | 0, 0, NULL); 683 | if (!osdpi_flow_cache){ 684 | pr_err("xt_opendpi: error creating flow cache.\n"); 685 | ret = -ENOMEM; 686 | goto err_ipq; 687 | } 688 | 689 | osdpi_id_cache = kmem_cache_create("xt_opendpi_ids", 690 | sizeof(struct osdpi_id_node) + 691 | size_id_struct, 692 | 0, 0, NULL); 693 | if (!osdpi_id_cache){ 694 | pr_err("xt_opendpi: error creating ids cache.\n"); 695 | ret = -ENOMEM; 696 | goto err_flow; 697 | } 698 | 699 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3,2,0) 700 | ret = nf_conntrack_register_notifier(&osdpi_notifier); 701 | #else 702 | ret = nf_conntrack_register_notifier(&init_net,&osdpi_notifier); 703 | #endif 704 | if (ret < 0){ 705 | pr_err("xt_opendpi: error registering notifier.\n"); 706 | goto err_id; 707 | } 708 | 709 | ret = xt_register_match(&opendpi_mt_reg); 710 | if (ret != 0){ 711 | pr_err("xt_opendpi: error registering opendpi match.\n"); 712 | opendpi_cleanup(); 713 | } 714 | 715 | return ret; 716 | 717 | err_id: 718 | kmem_cache_destroy (osdpi_id_cache); 719 | err_flow: 720 | kmem_cache_destroy (osdpi_flow_cache); 721 | err_ipq: 722 | ipoque_exit_detection_module(ipoque_struct, free_wrapper); 723 | err_out: 724 | return ret; 725 | } 726 | 727 | 728 | static void __exit opendpi_mt_exit(void) 729 | { 730 | pr_info("xt_opendpi 1.2 unload.\n"); 731 | 732 | xt_unregister_match(&opendpi_mt_reg); 733 | 734 | opendpi_cleanup(); 735 | } 736 | 737 | 738 | module_init(opendpi_mt_init); 739 | module_exit(opendpi_mt_exit); 740 | -------------------------------------------------------------------------------- /src/xt_opendpi.h: -------------------------------------------------------------------------------- 1 | /* 2 | * xt_opendpi.h 3 | * Copyright (C) 2010-2012 G. Elian Gidoni 4 | * 5 | * This file is part of OpenDPI, an open source deep packet inspection 6 | * library based on the PACE technology by ipoque GmbH 7 | * 8 | * This program is free software; you can redistribute it and/or 9 | * modify it under the terms of the GNU General Public License 10 | * as published by the Free Software Foundation; version 2 of the License. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 20 | */ 21 | 22 | #ifndef _LINUX_NETFILTER_XT_OPENDPI_H 23 | #define _LINUX_NETFILTER_XT_OPENDPI_H 1 24 | 25 | #include 26 | #include "ipq_api.h" 27 | 28 | struct xt_opendpi_mtinfo { 29 | IPOQUE_PROTOCOL_BITMASK flags; 30 | }; 31 | 32 | #endif /* _LINUX_NETFILTER_XT_OPENDPI_H */ 33 | --------------------------------------------------------------------------------