├── .gitignore ├── LICENSE ├── Makefile ├── Makefile.openwrt ├── README.md ├── TODO ├── VERSION ├── hijackfilter.spec └── src ├── .gitignore ├── Makefile ├── common └── list.h ├── libxt_vfree_dns ├── .gitignore ├── Makefile └── libxt_vfree_dns.c ├── libxt_vfree_http ├── .gitignore ├── Makefile └── libxt_vfree_http.c ├── xt_vfree_dns ├── .gitignore ├── Makefile ├── dns.h ├── xt_vfree_dns.c └── xt_vfree_dns.h └── xt_vfree_http ├── .gitignore ├── Makefile ├── xt_vfree_http.c └── xt_vfree_http.h /.gitignore: -------------------------------------------------------------------------------- 1 | .* 2 | !.gitignore 3 | *~ 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | 676 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # VFREE HijackFilter 2 | # Copyright (C) 2016 Rayson Zhu 3 | # 4 | # This program is free software: you can redistribute it and/or modify 5 | # it under the terms of the GNU General Public License as published by 6 | # the Free Software Foundation, either version 3 of the License, or 7 | # (at your option) any later version. 8 | # 9 | # This program is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU General Public License 15 | # along with this program. If not, see . 16 | 17 | SUBDIRS := src 18 | 19 | BUILD_TARGETS := $(SUBDIRS) 20 | INSTALL_TARGETS := $(SUBDIRS:%=%/install) 21 | UNINSTALL_TARGETS := $(SUBDIRS:%=%/uninstall) 22 | CLEAN_TARGETS := $(SUBDIRS:%=%/clean) 23 | 24 | export DEBUG ?= 0 25 | export DESTDIR ?= test2 26 | export PREFIX ?= /usr/local 27 | 28 | PACKAGE_NAME := hijackfilter 29 | PACKAGE_VERSION := $(shell cut -d - -f1 VERSION) 30 | PACKAGE_RELEASE := $(shell cut -d - -f2 VERSION) 31 | DIST_DIR := dist 32 | RPMBUILD_TOPDIR = $(DIST_DIR)/rpmbuild 33 | 34 | all: $(BUILD_TARGETS) 35 | 36 | $(BUILD_TARGETS): 37 | @$(MAKE) -C $@ 38 | 39 | $(CLEAN_TARGETS): 40 | @$(MAKE) -C $(@:%/clean=%) clean 41 | 42 | $(INSTALL_TARGETS): 43 | @$(MAKE) -C $(@:%/install=%) install 44 | 45 | $(UNINSTALL_TARGETS): 46 | @$(MAKE) -C $(@:%/uninstall=%) uninstall 47 | 48 | install: $(INSTALL_TARGETS) 49 | 50 | uninstall: $(UNINSTALL_TARGETS) 51 | 52 | clean: $(CLEAN_TARGETS) 53 | @rm -rf -- "$(DIST_DIR)" 54 | 55 | dist: clean 56 | @mkdir -p "$(DIST_DIR)/$(PACKAGE_NAME)-$(PACKAGE_VERSION)" \ 57 | && cp -a {LICENSE,VERSION,Makefile,README.md,*.spec,src} "$(DIST_DIR)/$(PACKAGE_NAME)-$(PACKAGE_VERSION)" 58 | @cd "$(DIST_DIR)" && tar -czvf "$(PACKAGE_NAME)-$(PACKAGE_VERSION)".tar.gz "$(PACKAGE_NAME)-$(PACKAGE_VERSION)" 59 | 60 | rpm: dist 61 | @mkdir -p $(RPMBUILD_TOPDIR) 62 | @rpmbuild --define "_topdir `pwd`/$(RPMBUILD_TOPDIR)" -ta "$(DIST_DIR)/$(PACKAGE_NAME)-$(PACKAGE_VERSION)".tar.gz 63 | 64 | .PHONY: $(BUILD_TARGETS) $(INSTALL_TARGETS) $(UNINSTALL_TARGETS) $(CLEAN_TARGETS) 65 | .PHONY: all clean install uninstall dist rpm 66 | -------------------------------------------------------------------------------- /Makefile.openwrt: -------------------------------------------------------------------------------- 1 | # VFREE HijackFilter 2 | # Copyright (C) 2016 Rayson Zhu 3 | # 4 | # This program is free software: you can redistribute it and/or modify 5 | # it under the terms of the GNU General Public License as published by 6 | # the Free Software Foundation, either version 3 of the License, or 7 | # (at your option) any later version. 8 | # 9 | # This program is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU General Public License 15 | # along with this program. If not, see . 16 | 17 | include $(TOPDIR)/rules.mk 18 | include $(INCLUDE_DIR)/kernel.mk 19 | 20 | PKG_NAME:=hijackfilter 21 | PKG_VERSION:=0.0.1 22 | PKG_RELEASE:=1 23 | PKG_MAINTAINER:=Rayson Zhu 24 | PKG_LICENSE:=GPL-3.0 25 | 26 | PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.bz2 27 | PKG_SOURCE_SUBDIR:=$(PKG_NAME)-$(PKG_VERSION) 28 | PKG_SOURCE_URL:=git://github.com/hijackfilter/hijackfilter.git 29 | PKG_SOURCE_PROTO:=git 30 | PKG_SOURCE_VERSION:=697e33ab138ed9061bf3b9c6e6fa6b7175d9ebfa 31 | 32 | PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION) 33 | 34 | INSTALL_KMOD_DIR := /lib/modules/$(LINUX_VERSION) 35 | 36 | include $(INCLUDE_DIR)/package.mk 37 | 38 | define Package/hijackfilter/Default 39 | SECTION:=net 40 | CATEGORY:=Network 41 | URL:=https://github.com/hijackfilter/hijackfilter 42 | endef 43 | 44 | define Package/hijackfilter/description/Default 45 | VFREE HijackFilter is a set of free software to protect you from Internet hijacking 46 | endef 47 | 48 | define Package/hijackfilter 49 | $(call Package/hijackfilter/Default) 50 | TITLE:=hijackfilter 51 | DEPENDS:=+iptables-mod-vfree-dns kmod-vfree-dns 52 | endef 53 | 54 | define Package/iptables-mod-vfree-dns 55 | $(call Package/hijackfilter/Default) 56 | TITLE:=iptables-mod-vfree-dns 57 | FILES:=$(PKG_BUILD_DIR)/src/libxt_vfree_dns/libxt_vfree_dns.so 58 | DEPENDS:=+kmod-vfree-dns +libxtables 59 | endef 60 | 61 | define KernelPackage/vfree-dns 62 | $(call Package/hijackfilter/Default) 63 | TITLE:=kmod-vfree-dns 64 | FILES:=$(PKG_BUILD_DIR)/src/xt_vfree_dns/xt_vfree_dns.ko 65 | AUTOLOAD:=$(call AutoLoad,50,$(MADWIFI_AUTOLOAD)) 66 | DEPENDS:=+kmod-ipt-core 67 | endef 68 | 69 | define Build/Compile 70 | $(call Build/Compile/Default, KERNEL_DIR=$(LINUX_DIR)) 71 | endef 72 | 73 | define Package/hijackfilter/install 74 | true 75 | endef 76 | 77 | define Package/iptables-mod-vfree-dns/install 78 | $(INSTALL_DIR) $(1)/usr/lib/iptables/ 79 | $(INSTALL_BIN) $(PKG_BUILD_DIR)/src/libxt_vfree_dns/libxt_vfree_dns.so $(1)/usr/lib/iptables/ 80 | endef 81 | 82 | define KernelPackage/vfree-dns/install 83 | $(INSTALL_DIR) $(1)/$(INSTALL_KMOD_DIR) 84 | $(INSTALL_BIN) $(PKG_BUILD_DIR)/src/xt_vfree_dns/xt_vfree_dns.ko $(1)/$(INSTALL_KMOD_DIR) 85 | endef 86 | 87 | $(eval $(call BuildPackage,hijackfilter)) 88 | $(eval $(call BuildPackage,iptables-mod-vfree-dns)) 89 | $(eval $(call KernelPackage,vfree-dns)) 90 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # VFREE HijackFilter 2 | 3 | VFREE HijackFilter is a free software to protect you from unwanted Internet hijacking. 4 | 5 | Copyright (C) 2016 Rayson Zhu 6 | 7 | If you encountered Internet hijacking, call your ISP to stop it! 8 | This software should be your last choice for preventing Internet hijacking. 9 | 10 | ## 1. Installation 11 | 12 | ### 1.1 Build 13 | 14 | - Ubuntu / Debian 15 | 16 | ``` bash 17 | apt install make gcc iptables-dev linux-headers-`uname -r` pkg-config 18 | make 19 | ``` 20 | 21 | - Fedora 22 | 23 | ``` bash 24 | dnf install make gcc iptables-devel kernel-devel-`uname -r` pkgconfig 25 | make 26 | ``` 27 | 28 | - RHEL / CentOS 29 | 30 | ``` bash 31 | yum install make gcc iptables-devel kernel-devel-`uname -r` pkgconfig 32 | make 33 | ``` 34 | 35 | ## 1.2 Install 36 | 37 | ``` bash 38 | make install 39 | depmod 40 | ``` 41 | 42 | ## 1.3. Uninstall 43 | 44 | ``` bash 45 | make uninstall 46 | depmod 47 | ``` 48 | 49 | ## 2. Usage 50 | 51 | ### 2.1 DNSFilter 52 | 53 | DNSFilter is a [Netfilter][1] extension to help you match and filter proofed DNS responses. 54 | 55 | Suppose your ISP redirects nonexistent domain names to `192.0.2.1` and `198.51.100.1`. 56 | To match and drop those spoofed DNS responses, we can use following commands: 57 | 58 | ``` bash 59 | # for IPv4 network 60 | iptables -t mangle -A PREROUTING -p udp --sport 53 -m vfree_dns \ 61 | --a 192.0.2.1,198.51.100.1\ 62 | -j DROP 63 | # for IPv6 network 64 | ip6tables -t mangle -A PREROUTING -p udp --sport 53 -m vfree_dns \ 65 | --a 192.0.2.1,198.51.100.1\ 66 | -j DROP 67 | ``` 68 | 69 | Use `-h` option for help. 70 | ``` bash 71 | iptables -m vfree_dns -h 72 | ``` 73 | 74 | ## 3. TO-DO 75 | 76 | - HTTPFilter: a netfilter module to match and filter proofed HTTP messages 77 | 78 | ## 4. LICENSE 79 | 80 | This program is distributed under GNU GENERAL PUBLIC LICENSE Version 3. 81 | 82 | [1]: http://www.netfilter.org 83 | -------------------------------------------------------------------------------- /TODO: -------------------------------------------------------------------------------- 1 | ## In Progress 2 | 3 | 1. HTTPFilter 4 | 5 | This will take a long time. I'm trying to make use of the connection tracking feature of Netfilter to support for more complicated filtering. 6 | Please tell me if you have any suggestion. 7 | 8 | 2. Packaging for Debian 9 | 10 | ## Proposed 11 | 12 | -------------------------------------------------------------------------------- /VERSION: -------------------------------------------------------------------------------- 1 | 0.0.1-1 2 | -------------------------------------------------------------------------------- /hijackfilter.spec: -------------------------------------------------------------------------------- 1 | %define KERNEL_VERSION %(uname -r) 2 | %define XTABLES_LIBDIR %(pkg-config --variable=xtlibdir xtables) 3 | %define MODULES_DIR /lib/modules/%{KERNEL_VERSION} 4 | %define debug_package %{nil} 5 | %define PACKAGE_VERSION %(cut -d - -f1 VERSION) 6 | %define PACKAGE_RELEASE %(cut -d - -f2 VERSION) 7 | 8 | Name: hijackfilter 9 | Version: %{PACKAGE_VERSION} 10 | Release: %{PACKAGE_RELEASE}%{?dist} 11 | Summary: VFREE HijackFilter is a free software to protect you from unwanted Internet hijacking. 12 | 13 | License: GPLv3 14 | URL: https://github.com/hijackfilter/hijackfilter 15 | Source: %{name}-%{version}.tar.gz 16 | Requires: hijackfilter-dnsfilter 17 | 18 | %description 19 | VFREE HijackFilter is a set of free software to protect you from Internet hijacking 20 | 21 | %package dnsfilter 22 | Version: %{PACKAGE_VERSION} 23 | Release: %{PACKAGE_RELEASE}%{?dist} 24 | Summary: DNSFilter is a Netfilter extension to match and filter proofed DNS responses. 25 | BuildRequires: iptables-devel pkgconfig 26 | Requires: iptables hijackfilter-dnsfilter-kmod = %{version} 27 | 28 | %description dnsfilter 29 | DNSFilter is a Netfilter extension to match and filter proofed DNS responses. 30 | 31 | %package dnsfilter-kmod 32 | Version: %{PACKAGE_VERSION} 33 | Release: %{PACKAGE_RELEASE}.%(echo %{KERNEL_VERSION} | cut -d . -f1-4 - | tr - _) 34 | Summary: DNSFilter is a Netfilter extension to match and filter proofed DNS responses. 35 | BuildRequires: kernel-devel 36 | Provides: hijackfilter-dnsfilter-kmod-common = %{version} 37 | 38 | %description dnsfilter-kmod 39 | DNSFilter is a Netfilter extension to match and filter proofed DNS responses. 40 | 41 | %prep 42 | %setup -q -n %{name}-%{version} 43 | 44 | %build 45 | make %{?_smp_mflags} 46 | 47 | 48 | %install 49 | rm -rf $RPM_BUILD_ROOT 50 | %make_install AUTO_DEPMOD=0 51 | 52 | 53 | %files 54 | %doc README.md 55 | %license LICENSE 56 | 57 | %files dnsfilter 58 | %doc README.md 59 | %license LICENSE 60 | %{XTABLES_LIBDIR}/libxt_vfree_dns.so 61 | 62 | %files dnsfilter-kmod 63 | %doc README.md 64 | %license LICENSE 65 | %{MODULES_DIR}/extra/vfree/xt_vfree_dns.ko 66 | 67 | %post dnsfilter-kmod 68 | depmod 69 | 70 | %postun dnsfilter-kmod 71 | depmod 72 | 73 | %changelog 74 | * Sun May 15 2016 Rayson Zhu 75 | - First version 76 | -------------------------------------------------------------------------------- /src/.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.so 3 | *.ko 4 | -------------------------------------------------------------------------------- /src/Makefile: -------------------------------------------------------------------------------- 1 | # VFREE HijackFilter 2 | # Copyright (C) 2016 Rayson Zhu 3 | # 4 | # This program is free software: you can redistribute it and/or modify 5 | # it under the terms of the GNU General Public License as published by 6 | # the Free Software Foundation, either version 3 of the License, or 7 | # (at your option) any later version. 8 | # 9 | # This program is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU General Public License 15 | # along with this program. If not, see . 16 | 17 | SUBDIRS := libxt_vfree_dns xt_vfree_dns 18 | 19 | BUILD_TARGETS := $(SUBDIRS) 20 | INSTALL_TARGETS := $(SUBDIRS:%=%/install) 21 | UNINSTALL_TARGETS := $(SUBDIRS:%=%/uninstall) 22 | CLEAN_TARGETS := $(SUBDIRS:%=%/clean) 23 | 24 | export DEBUG ?= 0 25 | export DESTDIR ?= 26 | export PREFIX ?= /usr/local 27 | 28 | all: $(BUILD_TARGETS) 29 | 30 | $(BUILD_TARGETS): 31 | @$(MAKE) -C $@ 32 | 33 | $(CLEAN_TARGETS): 34 | @$(MAKE) -C $(@:%/clean=%) clean 35 | 36 | $(INSTALL_TARGETS): 37 | @$(MAKE) -C $(@:%/install=%) install 38 | 39 | $(UNINSTALL_TARGETS): 40 | @$(MAKE) -C $(@:%/uninstall=%) uninstall 41 | 42 | install: $(INSTALL_TARGETS) 43 | 44 | uninstall: $(UNINSTALL_TARGETS) 45 | 46 | clean: $(CLEAN_TARGETS) 47 | 48 | .PHONY: $(BUILD_TARGETS) $(INSTALL_TARGETS) $(UNINSTALL_TARGETS) $(CLEAN_TARGETS) 49 | .PHONY: all clean install uninstall 50 | -------------------------------------------------------------------------------- /src/common/list.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Linux linked list implementation for user space 3 | * https://gist.github.com/evanslai/3607695 4 | */ 5 | 6 | #ifndef _LINUX_LIST_H 7 | #define _LINUX_LIST_H 8 | 9 | #include 10 | 11 | #ifndef offsetof 12 | #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) 13 | #endif /* offsetof */ 14 | 15 | #ifndef container_of 16 | /** 17 | * container_of - cast a member of a structure out to the containing structure 18 | * @ptr: the pointer to the member. 19 | * @type: the type of the container struct this is embedded in. 20 | * @member: the name of the member within the struct. 21 | * 22 | */ 23 | #define container_of(ptr, type, member) ({ \ 24 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ 25 | (type *)( (char *)__mptr - offsetof(type,member) );}) 26 | #endif /* container_of */ 27 | /* 28 | * These are non-NULL pointers that will result in page faults 29 | * under normal circumstances, used to verify that nobody uses 30 | * non-initialized list entries. 31 | */ 32 | #define LIST_POISON1 ((void *) 0x00100100) 33 | #define LIST_POISON2 ((void *) 0x00200200) 34 | 35 | struct list_head { 36 | struct list_head *next, *prev; 37 | }; 38 | 39 | struct hlist_head { 40 | struct hlist_node *first; 41 | }; 42 | 43 | struct hlist_node { 44 | struct hlist_node *next, **pprev; 45 | }; 46 | 47 | /* 48 | * Simple doubly linked list implementation. 49 | * 50 | * Some of the internal functions ("__xxx") are useful when 51 | * manipulating whole lists rather than single entries, as 52 | * sometimes we already know the next/prev entries and we can 53 | * generate better code by using them directly rather than 54 | * using the generic single-entry routines. 55 | */ 56 | 57 | #define LIST_HEAD_INIT(name) { &(name), &(name) } 58 | 59 | #define LIST_HEAD(name) \ 60 | struct list_head name = LIST_HEAD_INIT(name) 61 | 62 | static inline void INIT_LIST_HEAD(struct list_head *list) 63 | { 64 | list->next = list; 65 | list->prev = list; 66 | } 67 | 68 | /* 69 | * Insert a new entry between two known consecutive entries. 70 | * 71 | * This is only for internal list manipulation where we know 72 | * the prev/next entries already! 73 | */ 74 | #ifndef CONFIG_DEBUG_LIST 75 | static inline void __list_add(struct list_head *new, 76 | struct list_head *prev, 77 | struct list_head *next) 78 | { 79 | next->prev = new; 80 | new->next = next; 81 | new->prev = prev; 82 | prev->next = new; 83 | } 84 | #else 85 | extern void __list_add(struct list_head *new, 86 | struct list_head *prev, 87 | struct list_head *next); 88 | #endif 89 | 90 | /** 91 | * list_add - add a new entry 92 | * @new: new entry to be added 93 | * @head: list head to add it after 94 | * 95 | * Insert a new entry after the specified head. 96 | * This is good for implementing stacks. 97 | */ 98 | static inline void list_add(struct list_head *new, struct list_head *head) 99 | { 100 | __list_add(new, head, head->next); 101 | } 102 | 103 | 104 | /** 105 | * list_add_tail - add a new entry 106 | * @new: new entry to be added 107 | * @head: list head to add it before 108 | * 109 | * Insert a new entry before the specified head. 110 | * This is useful for implementing queues. 111 | */ 112 | static inline void list_add_tail(struct list_head *new, struct list_head *head) 113 | { 114 | __list_add(new, head->prev, head); 115 | } 116 | 117 | /* 118 | * Delete a list entry by making the prev/next entries 119 | * point to each other. 120 | * 121 | * This is only for internal list manipulation where we know 122 | * the prev/next entries already! 123 | */ 124 | static inline void __list_del(struct list_head * prev, struct list_head * next) 125 | { 126 | next->prev = prev; 127 | prev->next = next; 128 | } 129 | 130 | /** 131 | * list_del - deletes entry from list. 132 | * @entry: the element to delete from the list. 133 | * Note: list_empty() on entry does not return true after this, the entry is 134 | * in an undefined state. 135 | */ 136 | #ifndef CONFIG_DEBUG_LIST 137 | static inline void __list_del_entry(struct list_head *entry) 138 | { 139 | __list_del(entry->prev, entry->next); 140 | } 141 | 142 | static inline void list_del(struct list_head *entry) 143 | { 144 | __list_del(entry->prev, entry->next); 145 | entry->next = LIST_POISON1; 146 | entry->prev = LIST_POISON2; 147 | } 148 | #else 149 | extern void __list_del_entry(struct list_head *entry); 150 | extern void list_del(struct list_head *entry); 151 | #endif 152 | 153 | /** 154 | * list_replace - replace old entry by new one 155 | * @old : the element to be replaced 156 | * @new : the new element to insert 157 | * 158 | * If @old was empty, it will be overwritten. 159 | */ 160 | static inline void list_replace(struct list_head *old, 161 | struct list_head *new) 162 | { 163 | new->next = old->next; 164 | new->next->prev = new; 165 | new->prev = old->prev; 166 | new->prev->next = new; 167 | } 168 | 169 | static inline void list_replace_init(struct list_head *old, 170 | struct list_head *new) 171 | { 172 | list_replace(old, new); 173 | INIT_LIST_HEAD(old); 174 | } 175 | 176 | /** 177 | * list_del_init - deletes entry from list and reinitialize it. 178 | * @entry: the element to delete from the list. 179 | */ 180 | static inline void list_del_init(struct list_head *entry) 181 | { 182 | __list_del_entry(entry); 183 | INIT_LIST_HEAD(entry); 184 | } 185 | 186 | /** 187 | * list_move - delete from one list and add as another's head 188 | * @list: the entry to move 189 | * @head: the head that will precede our entry 190 | */ 191 | static inline void list_move(struct list_head *list, struct list_head *head) 192 | { 193 | __list_del_entry(list); 194 | list_add(list, head); 195 | } 196 | 197 | /** 198 | * list_move_tail - delete from one list and add as another's tail 199 | * @list: the entry to move 200 | * @head: the head that will follow our entry 201 | */ 202 | static inline void list_move_tail(struct list_head *list, 203 | struct list_head *head) 204 | { 205 | __list_del_entry(list); 206 | list_add_tail(list, head); 207 | } 208 | 209 | /** 210 | * list_is_last - tests whether @list is the last entry in list @head 211 | * @list: the entry to test 212 | * @head: the head of the list 213 | */ 214 | static inline int list_is_last(const struct list_head *list, 215 | const struct list_head *head) 216 | { 217 | return list->next == head; 218 | } 219 | 220 | /** 221 | * list_empty - tests whether a list is empty 222 | * @head: the list to test. 223 | */ 224 | static inline int list_empty(const struct list_head *head) 225 | { 226 | return head->next == head; 227 | } 228 | 229 | /** 230 | * list_empty_careful - tests whether a list is empty and not being modified 231 | * @head: the list to test 232 | * 233 | * Description: 234 | * tests whether a list is empty _and_ checks that no other CPU might be 235 | * in the process of modifying either member (next or prev) 236 | * 237 | * NOTE: using list_empty_careful() without synchronization 238 | * can only be safe if the only activity that can happen 239 | * to the list entry is list_del_init(). Eg. it cannot be used 240 | * if another CPU could re-list_add() it. 241 | */ 242 | static inline int list_empty_careful(const struct list_head *head) 243 | { 244 | struct list_head *next = head->next; 245 | return (next == head) && (next == head->prev); 246 | } 247 | 248 | /** 249 | * list_rotate_left - rotate the list to the left 250 | * @head: the head of the list 251 | */ 252 | static inline void list_rotate_left(struct list_head *head) 253 | { 254 | struct list_head *first; 255 | 256 | if (!list_empty(head)) { 257 | first = head->next; 258 | list_move_tail(first, head); 259 | } 260 | } 261 | 262 | /** 263 | * list_is_singular - tests whether a list has just one entry. 264 | * @head: the list to test. 265 | */ 266 | static inline int list_is_singular(const struct list_head *head) 267 | { 268 | return !list_empty(head) && (head->next == head->prev); 269 | } 270 | 271 | static inline void __list_cut_position(struct list_head *list, 272 | struct list_head *head, struct list_head *entry) 273 | { 274 | struct list_head *new_first = entry->next; 275 | list->next = head->next; 276 | list->next->prev = list; 277 | list->prev = entry; 278 | entry->next = list; 279 | head->next = new_first; 280 | new_first->prev = head; 281 | } 282 | 283 | /** 284 | * list_cut_position - cut a list into two 285 | * @list: a new list to add all removed entries 286 | * @head: a list with entries 287 | * @entry: an entry within head, could be the head itself 288 | * and if so we won't cut the list 289 | * 290 | * This helper moves the initial part of @head, up to and 291 | * including @entry, from @head to @list. You should 292 | * pass on @entry an element you know is on @head. @list 293 | * should be an empty list or a list you do not care about 294 | * losing its data. 295 | * 296 | */ 297 | static inline void list_cut_position(struct list_head *list, 298 | struct list_head *head, struct list_head *entry) 299 | { 300 | if (list_empty(head)) 301 | return; 302 | if (list_is_singular(head) && 303 | (head->next != entry && head != entry)) 304 | return; 305 | if (entry == head) 306 | INIT_LIST_HEAD(list); 307 | else 308 | __list_cut_position(list, head, entry); 309 | } 310 | 311 | static inline void __list_splice(const struct list_head *list, 312 | struct list_head *prev, 313 | struct list_head *next) 314 | { 315 | struct list_head *first = list->next; 316 | struct list_head *last = list->prev; 317 | 318 | first->prev = prev; 319 | prev->next = first; 320 | 321 | last->next = next; 322 | next->prev = last; 323 | } 324 | 325 | /** 326 | * list_splice - join two lists, this is designed for stacks 327 | * @list: the new list to add. 328 | * @head: the place to add it in the first list. 329 | */ 330 | static inline void list_splice(const struct list_head *list, 331 | struct list_head *head) 332 | { 333 | if (!list_empty(list)) 334 | __list_splice(list, head, head->next); 335 | } 336 | 337 | /** 338 | * list_splice_tail - join two lists, each list being a queue 339 | * @list: the new list to add. 340 | * @head: the place to add it in the first list. 341 | */ 342 | static inline void list_splice_tail(struct list_head *list, 343 | struct list_head *head) 344 | { 345 | if (!list_empty(list)) 346 | __list_splice(list, head->prev, head); 347 | } 348 | 349 | /** 350 | * list_splice_init - join two lists and reinitialise the emptied list. 351 | * @list: the new list to add. 352 | * @head: the place to add it in the first list. 353 | * 354 | * The list at @list is reinitialised 355 | */ 356 | static inline void list_splice_init(struct list_head *list, 357 | struct list_head *head) 358 | { 359 | if (!list_empty(list)) { 360 | __list_splice(list, head, head->next); 361 | INIT_LIST_HEAD(list); 362 | } 363 | } 364 | 365 | /** 366 | * list_splice_tail_init - join two lists and reinitialise the emptied list 367 | * @list: the new list to add. 368 | * @head: the place to add it in the first list. 369 | * 370 | * Each of the lists is a queue. 371 | * The list at @list is reinitialised 372 | */ 373 | static inline void list_splice_tail_init(struct list_head *list, 374 | struct list_head *head) 375 | { 376 | if (!list_empty(list)) { 377 | __list_splice(list, head->prev, head); 378 | INIT_LIST_HEAD(list); 379 | } 380 | } 381 | 382 | /** 383 | * list_entry - get the struct for this entry 384 | * @ptr: the &struct list_head pointer. 385 | * @type: the type of the struct this is embedded in. 386 | * @member: the name of the list_struct within the struct. 387 | */ 388 | #define list_entry(ptr, type, member) \ 389 | container_of(ptr, type, member) 390 | 391 | /** 392 | * list_first_entry - get the first element from a list 393 | * @ptr: the list head to take the element from. 394 | * @type: the type of the struct this is embedded in. 395 | * @member: the name of the list_struct within the struct. 396 | * 397 | * Note, that list is expected to be not empty. 398 | */ 399 | #define list_first_entry(ptr, type, member) \ 400 | list_entry((ptr)->next, type, member) 401 | 402 | /** 403 | * list_for_each - iterate over a list 404 | * @pos: the &struct list_head to use as a loop cursor. 405 | * @head: the head for your list. 406 | */ 407 | #define list_for_each(pos, head) \ 408 | for (pos = (head)->next; pos != (head); pos = pos->next) 409 | 410 | /** 411 | * __list_for_each - iterate over a list 412 | * @pos: the &struct list_head to use as a loop cursor. 413 | * @head: the head for your list. 414 | * 415 | * This variant doesn't differ from list_for_each() any more. 416 | * We don't do prefetching in either case. 417 | */ 418 | #define __list_for_each(pos, head) \ 419 | for (pos = (head)->next; pos != (head); pos = pos->next) 420 | 421 | /** 422 | * list_for_each_prev - iterate over a list backwards 423 | * @pos: the &struct list_head to use as a loop cursor. 424 | * @head: the head for your list. 425 | */ 426 | #define list_for_each_prev(pos, head) \ 427 | for (pos = (head)->prev; pos != (head); pos = pos->prev) 428 | 429 | /** 430 | * list_for_each_safe - iterate over a list safe against removal of list entry 431 | * @pos: the &struct list_head to use as a loop cursor. 432 | * @n: another &struct list_head to use as temporary storage 433 | * @head: the head for your list. 434 | */ 435 | #define list_for_each_safe(pos, n, head) \ 436 | for (pos = (head)->next, n = pos->next; pos != (head); \ 437 | pos = n, n = pos->next) 438 | 439 | /** 440 | * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry 441 | * @pos: the &struct list_head to use as a loop cursor. 442 | * @n: another &struct list_head to use as temporary storage 443 | * @head: the head for your list. 444 | */ 445 | #define list_for_each_prev_safe(pos, n, head) \ 446 | for (pos = (head)->prev, n = pos->prev; \ 447 | pos != (head); \ 448 | pos = n, n = pos->prev) 449 | 450 | /** 451 | * list_for_each_entry - iterate over list of given type 452 | * @pos: the type * to use as a loop cursor. 453 | * @head: the head for your list. 454 | * @member: the name of the list_struct within the struct. 455 | */ 456 | #define list_for_each_entry(pos, head, member) \ 457 | for (pos = list_entry((head)->next, typeof(*pos), member); \ 458 | &pos->member != (head); \ 459 | pos = list_entry(pos->member.next, typeof(*pos), member)) 460 | 461 | /** 462 | * list_for_each_entry_reverse - iterate backwards over list of given type. 463 | * @pos: the type * to use as a loop cursor. 464 | * @head: the head for your list. 465 | * @member: the name of the list_struct within the struct. 466 | */ 467 | #define list_for_each_entry_reverse(pos, head, member) \ 468 | for (pos = list_entry((head)->prev, typeof(*pos), member); \ 469 | &pos->member != (head); \ 470 | pos = list_entry(pos->member.prev, typeof(*pos), member)) 471 | 472 | /** 473 | * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue() 474 | * @pos: the type * to use as a start point 475 | * @head: the head of the list 476 | * @member: the name of the list_struct within the struct. 477 | * 478 | * Prepares a pos entry for use as a start point in list_for_each_entry_continue(). 479 | */ 480 | #define list_prepare_entry(pos, head, member) \ 481 | ((pos) ? : list_entry(head, typeof(*pos), member)) 482 | 483 | /** 484 | * list_for_each_entry_continue - continue iteration over list of given type 485 | * @pos: the type * to use as a loop cursor. 486 | * @head: the head for your list. 487 | * @member: the name of the list_struct within the struct. 488 | * 489 | * Continue to iterate over list of given type, continuing after 490 | * the current position. 491 | */ 492 | #define list_for_each_entry_continue(pos, head, member) \ 493 | for (pos = list_entry(pos->member.next, typeof(*pos), member); \ 494 | &pos->member != (head); \ 495 | pos = list_entry(pos->member.next, typeof(*pos), member)) 496 | 497 | /** 498 | * list_for_each_entry_continue_reverse - iterate backwards from the given point 499 | * @pos: the type * to use as a loop cursor. 500 | * @head: the head for your list. 501 | * @member: the name of the list_struct within the struct. 502 | * 503 | * Start to iterate over list of given type backwards, continuing after 504 | * the current position. 505 | */ 506 | #define list_for_each_entry_continue_reverse(pos, head, member) \ 507 | for (pos = list_entry(pos->member.prev, typeof(*pos), member); \ 508 | &pos->member != (head); \ 509 | pos = list_entry(pos->member.prev, typeof(*pos), member)) 510 | 511 | /** 512 | * list_for_each_entry_from - iterate over list of given type from the current point 513 | * @pos: the type * to use as a loop cursor. 514 | * @head: the head for your list. 515 | * @member: the name of the list_struct within the struct. 516 | * 517 | * Iterate over list of given type, continuing from current position. 518 | */ 519 | #define list_for_each_entry_from(pos, head, member) \ 520 | for (; &pos->member != (head); \ 521 | pos = list_entry(pos->member.next, typeof(*pos), member)) 522 | 523 | /** 524 | * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry 525 | * @pos: the type * to use as a loop cursor. 526 | * @n: another type * to use as temporary storage 527 | * @head: the head for your list. 528 | * @member: the name of the list_struct within the struct. 529 | */ 530 | #define list_for_each_entry_safe(pos, n, head, member) \ 531 | for (pos = list_entry((head)->next, typeof(*pos), member), \ 532 | n = list_entry(pos->member.next, typeof(*pos), member); \ 533 | &pos->member != (head); \ 534 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) 535 | 536 | /** 537 | * list_for_each_entry_safe_continue - continue list iteration safe against removal 538 | * @pos: the type * to use as a loop cursor. 539 | * @n: another type * to use as temporary storage 540 | * @head: the head for your list. 541 | * @member: the name of the list_struct within the struct. 542 | * 543 | * Iterate over list of given type, continuing after current point, 544 | * safe against removal of list entry. 545 | */ 546 | #define list_for_each_entry_safe_continue(pos, n, head, member) \ 547 | for (pos = list_entry(pos->member.next, typeof(*pos), member), \ 548 | n = list_entry(pos->member.next, typeof(*pos), member); \ 549 | &pos->member != (head); \ 550 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) 551 | 552 | /** 553 | * list_for_each_entry_safe_from - iterate over list from current point safe against removal 554 | * @pos: the type * to use as a loop cursor. 555 | * @n: another type * to use as temporary storage 556 | * @head: the head for your list. 557 | * @member: the name of the list_struct within the struct. 558 | * 559 | * Iterate over list of given type from current point, safe against 560 | * removal of list entry. 561 | */ 562 | #define list_for_each_entry_safe_from(pos, n, head, member) \ 563 | for (n = list_entry(pos->member.next, typeof(*pos), member); \ 564 | &pos->member != (head); \ 565 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) 566 | 567 | /** 568 | * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal 569 | * @pos: the type * to use as a loop cursor. 570 | * @n: another type * to use as temporary storage 571 | * @head: the head for your list. 572 | * @member: the name of the list_struct within the struct. 573 | * 574 | * Iterate backwards over list of given type, safe against removal 575 | * of list entry. 576 | */ 577 | #define list_for_each_entry_safe_reverse(pos, n, head, member) \ 578 | for (pos = list_entry((head)->prev, typeof(*pos), member), \ 579 | n = list_entry(pos->member.prev, typeof(*pos), member); \ 580 | &pos->member != (head); \ 581 | pos = n, n = list_entry(n->member.prev, typeof(*n), member)) 582 | 583 | /** 584 | * list_safe_reset_next - reset a stale list_for_each_entry_safe loop 585 | * @pos: the loop cursor used in the list_for_each_entry_safe loop 586 | * @n: temporary storage used in list_for_each_entry_safe 587 | * @member: the name of the list_struct within the struct. 588 | * 589 | * list_safe_reset_next is not safe to use in general if the list may be 590 | * modified concurrently (eg. the lock is dropped in the loop body). An 591 | * exception to this is if the cursor element (pos) is pinned in the list, 592 | * and list_safe_reset_next is called after re-taking the lock and before 593 | * completing the current iteration of the loop body. 594 | */ 595 | #define list_safe_reset_next(pos, n, member) \ 596 | n = list_entry(pos->member.next, typeof(*pos), member) 597 | 598 | /* 599 | * Double linked lists with a single pointer list head. 600 | * Mostly useful for hash tables where the two pointer list head is 601 | * too wasteful. 602 | * You lose the ability to access the tail in O(1). 603 | */ 604 | 605 | #define HLIST_HEAD_INIT { .first = NULL } 606 | #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL } 607 | #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL) 608 | static inline void INIT_HLIST_NODE(struct hlist_node *h) 609 | { 610 | h->next = NULL; 611 | h->pprev = NULL; 612 | } 613 | 614 | static inline int hlist_unhashed(const struct hlist_node *h) 615 | { 616 | return !h->pprev; 617 | } 618 | 619 | static inline int hlist_empty(const struct hlist_head *h) 620 | { 621 | return !h->first; 622 | } 623 | 624 | static inline void __hlist_del(struct hlist_node *n) 625 | { 626 | struct hlist_node *next = n->next; 627 | struct hlist_node **pprev = n->pprev; 628 | *pprev = next; 629 | if (next) 630 | next->pprev = pprev; 631 | } 632 | 633 | static inline void hlist_del(struct hlist_node *n) 634 | { 635 | __hlist_del(n); 636 | n->next = LIST_POISON1; 637 | n->pprev = LIST_POISON2; 638 | } 639 | 640 | static inline void hlist_del_init(struct hlist_node *n) 641 | { 642 | if (!hlist_unhashed(n)) { 643 | __hlist_del(n); 644 | INIT_HLIST_NODE(n); 645 | } 646 | } 647 | 648 | static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) 649 | { 650 | struct hlist_node *first = h->first; 651 | n->next = first; 652 | if (first) 653 | first->pprev = &n->next; 654 | h->first = n; 655 | n->pprev = &h->first; 656 | } 657 | 658 | /* next must be != NULL */ 659 | static inline void hlist_add_before(struct hlist_node *n, 660 | struct hlist_node *next) 661 | { 662 | n->pprev = next->pprev; 663 | n->next = next; 664 | next->pprev = &n->next; 665 | *(n->pprev) = n; 666 | } 667 | 668 | static inline void hlist_add_after(struct hlist_node *n, 669 | struct hlist_node *next) 670 | { 671 | next->next = n->next; 672 | n->next = next; 673 | next->pprev = &n->next; 674 | 675 | if(next->next) 676 | next->next->pprev = &next->next; 677 | } 678 | 679 | /* after that we'll appear to be on some hlist and hlist_del will work */ 680 | static inline void hlist_add_fake(struct hlist_node *n) 681 | { 682 | n->pprev = &n->next; 683 | } 684 | 685 | /* 686 | * Move a list from one list head to another. Fixup the pprev 687 | * reference of the first entry if it exists. 688 | */ 689 | static inline void hlist_move_list(struct hlist_head *old, 690 | struct hlist_head *new) 691 | { 692 | new->first = old->first; 693 | if (new->first) 694 | new->first->pprev = &new->first; 695 | old->first = NULL; 696 | } 697 | 698 | #define hlist_entry(ptr, type, member) container_of(ptr,type,member) 699 | 700 | #define hlist_for_each(pos, head) \ 701 | for (pos = (head)->first; pos ; pos = pos->next) 702 | 703 | #define hlist_for_each_safe(pos, n, head) \ 704 | for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \ 705 | pos = n) 706 | 707 | /** 708 | * hlist_for_each_entry - iterate over list of given type 709 | * @tpos: the type * to use as a loop cursor. 710 | * @pos: the &struct hlist_node to use as a loop cursor. 711 | * @head: the head for your list. 712 | * @member: the name of the hlist_node within the struct. 713 | */ 714 | #define hlist_for_each_entry(tpos, pos, head, member) \ 715 | for (pos = (head)->first; \ 716 | pos && \ 717 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ 718 | pos = pos->next) 719 | 720 | /** 721 | * hlist_for_each_entry_continue - iterate over a hlist continuing after current point 722 | * @tpos: the type * to use as a loop cursor. 723 | * @pos: the &struct hlist_node to use as a loop cursor. 724 | * @member: the name of the hlist_node within the struct. 725 | */ 726 | #define hlist_for_each_entry_continue(tpos, pos, member) \ 727 | for (pos = (pos)->next; \ 728 | pos && \ 729 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ 730 | pos = pos->next) 731 | 732 | /** 733 | * hlist_for_each_entry_from - iterate over a hlist continuing from current point 734 | * @tpos: the type * to use as a loop cursor. 735 | * @pos: the &struct hlist_node to use as a loop cursor. 736 | * @member: the name of the hlist_node within the struct. 737 | */ 738 | #define hlist_for_each_entry_from(tpos, pos, member) \ 739 | for (; pos && \ 740 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ 741 | pos = pos->next) 742 | 743 | /** 744 | * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry 745 | * @tpos: the type * to use as a loop cursor. 746 | * @pos: the &struct hlist_node to use as a loop cursor. 747 | * @n: another &struct hlist_node to use as temporary storage 748 | * @head: the head for your list. 749 | * @member: the name of the hlist_node within the struct. 750 | */ 751 | #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \ 752 | for (pos = (head)->first; \ 753 | pos && ({ n = pos->next; 1; }) && \ 754 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ 755 | pos = n) 756 | 757 | #endif 758 | -------------------------------------------------------------------------------- /src/libxt_vfree_dns/.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.so 3 | -------------------------------------------------------------------------------- /src/libxt_vfree_dns/Makefile: -------------------------------------------------------------------------------- 1 | # VFREE HijackFilter 2 | # Copyright (C) 2016 Rayson Zhu 3 | # 4 | # This program is free software: you can redistribute it and/or modify 5 | # it under the terms of the GNU General Public License as published by 6 | # the Free Software Foundation, either version 3 of the License, or 7 | # (at your option) any later version. 8 | # 9 | # This program is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU General Public License 15 | # along with this program. If not, see . 16 | 17 | XTABLES_LIBS := libxt_vfree_dns.so 18 | INCLUDES := -I../xt_vfree_dns 19 | 20 | DESTDIR ?= 21 | PREFIX ?= /usr/local 22 | INSTALL := install -c 23 | INSTALLDATA := install -m 644 24 | XTABLES_LIBDIR := $(shell pkg-config --variable=xtlibdir xtables) 25 | 26 | INSTALL_XTABLES_LIBDIR = $(DESTDIR)/$(XTABLES_LIBDIR) 27 | DEBUG ?= 0 28 | ifeq ($(DEBUG), 1) 29 | CFLAGS :=-Wall -g3 -DDEBUG 30 | else 31 | CFLAGS :=-O2 -Wall -DNDEBUG 32 | endif 33 | 34 | all: build 35 | 36 | check: 37 | ifeq ($(XTABLES_LIBDIR),) 38 | $(error 'Failed to locate your Xtables extension path. Please try defining XTABLES_LIBDIR.') 39 | endif 40 | 41 | build: check $(XTABLES_LIBS) 42 | 43 | lib%.so: lib%.o 44 | @$(CC) -shared -fPIC -o $@ $^ 45 | 46 | lib%.o: lib%.c 47 | @$(CC) $(CFLAGS) $(INCLUDES) -D_INIT=lib$*_init -fPIC -c -o $@ $< 48 | 49 | clean: 50 | @rm -f *.o $(XTABLES_LIBS) 51 | 52 | install: build 53 | mkdir -p $(INSTALL_XTABLES_LIBDIR) 54 | $(INSTALL) -t $(INSTALL_XTABLES_LIBDIR) $(XTABLES_LIBS) 55 | 56 | uninstall: 57 | rm -f $(XTABLES_LIBS:%=$(INSTALL_XTABLES_LIBDIR)/%) 58 | 59 | .PHONY: all check build clean install uninstall 60 | .PHONY: modules modules/clean modules/install modules/uninstall 61 | -------------------------------------------------------------------------------- /src/libxt_vfree_dns/libxt_vfree_dns.c: -------------------------------------------------------------------------------- 1 | /* 2 | * VFREE HijackFilter 3 | * Copyright (C) 2016 Rayson Zhu 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | */ 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include "xt_vfree_dns.h" 28 | 29 | #if DEBUG 30 | #define D(x) x 31 | #else 32 | #define D(x) 33 | #endif 34 | 35 | static const struct option vfree_dns_mt_opts[] = { 36 | {.name = "a", .has_arg = true, .val = '1'}, 37 | {.name = "aaaa", .has_arg = true, .val = '2'}, 38 | {NULL}, 39 | }; 40 | 41 | static void vfree_dns_mt_help(void) 42 | { 43 | printf( 44 | "vfree_dns match " 45 | "\noptions:\n" 46 | "\t--a\ta comma seperated list of A records (IPv4 addresses)\n" 47 | "\t--aaaa\ta comma seperated list of AAAA records (IPv6 addresses)\n" 48 | ); 49 | } 50 | 51 | static void vfree_dns_mt_init(struct xt_entry_match *match) 52 | { 53 | } 54 | 55 | static int parse_address_list6(const char *in, struct vfree_dns_aaaa_item *out, size_t max_count, size_t *count) 56 | { 57 | int err = 0; 58 | size_t i; 59 | char *copy, *current, *next, *prefix, *prefix_end; 60 | struct vfree_dns_aaaa_item *record_item; 61 | struct in6_addr *addr; 62 | unsigned long prefix_val; 63 | assert(in); 64 | assert(out); 65 | if (!in[0]) 66 | return -EINVAL; 67 | copy = strdup(in); 68 | if (!copy) 69 | return -ENOMEM; 70 | for (i = 0, current = copy; i < max_count && current; i++, current = next) { 71 | next = strchr(current, ','); 72 | if (next) 73 | *next++ = 0; 74 | record_item = out + i; 75 | if (!record_item) { 76 | err = -ENOMEM; 77 | goto cleanup_copy; 78 | } 79 | prefix = strchr(current, '/'); 80 | if (prefix) { 81 | *prefix++ = 0; 82 | if (!*prefix) { 83 | D(printf("Prefix value is missing. at %td\n", prefix - copy)); 84 | err = -EINVAL; 85 | goto cleanup_copy; 86 | } 87 | prefix_val = strtoul(prefix, &prefix_end, 10); 88 | if (*prefix_end || prefix_val > 128) { 89 | D(printf("Prefix value is invalid. at %td\n", prefix_end - copy)); 90 | err = -EINVAL; 91 | goto cleanup_copy; 92 | } 93 | record_item->prefix = (uint8_t)prefix_val; 94 | } else { 95 | record_item->prefix = 128; 96 | } 97 | if (record_item->prefix < 128) { 98 | D(printf("Specifying a subnet is not supported in current version. at %td\n", prefix_end - copy)); 99 | err = -EINVAL; 100 | goto cleanup_copy; 101 | } 102 | addr = xtables_numeric_to_ip6addr(current); 103 | if (!addr) { 104 | D(printf("IPv6 address (%s) is invalid. at %td\n", current, current - copy)); 105 | err = -EINVAL; 106 | goto cleanup_copy; 107 | } 108 | memcpy(&record_item->ip, addr, sizeof(struct in6_addr)); 109 | D(printf("Parsed IPv6 address: %s/%d. at %td\n", current, 110 | record_item->prefix, current - copy)); 111 | } 112 | if (count) 113 | *count = i; 114 | if (current) { 115 | D(printf("Too many IPv6 addresses. limit: %zu\n", max_count)); 116 | err = E2BIG; 117 | goto cleanup_copy; 118 | } 119 | cleanup_copy: 120 | free(copy); 121 | return err; 122 | } 123 | 124 | static int parse_address_list4(const char *in, struct vfree_dns_a_item *out, size_t max_count, size_t *count) 125 | { 126 | int err = 0; 127 | size_t i; 128 | char *copy, *current, *next, *prefix, *prefix_end; 129 | struct vfree_dns_a_item *record_item; 130 | struct in_addr *addr; 131 | unsigned long prefix_val; 132 | assert(in); 133 | assert(out); 134 | if (!in[0]) 135 | return -EINVAL; 136 | copy = strdup(in); 137 | if (!copy) 138 | return -ENOMEM; 139 | for (i = 0, current = copy; i < max_count && current; i++, current = next) { 140 | next = strchr(current, ','); 141 | if (next) 142 | *next++ = 0; 143 | record_item = out + i; 144 | if (!record_item) { 145 | err = -ENOMEM; 146 | goto cleanup_copy; 147 | } 148 | prefix = strchr(current, '/'); 149 | if (prefix) { 150 | *prefix++ = 0; 151 | if (!*prefix) { 152 | D(printf("Prefix value is missing. at %td\n", prefix - copy)); 153 | err = -EINVAL; 154 | goto cleanup_copy; 155 | } 156 | prefix_val = strtoul(prefix, &prefix_end, 10); 157 | if (*prefix_end || prefix_val > 32) { 158 | D(printf("Prefix value is invalid. at %td\n", prefix_end - copy)); 159 | err = -EINVAL; 160 | goto cleanup_copy; 161 | } 162 | record_item->prefix = (uint8_t)prefix_val; 163 | } else { 164 | record_item->prefix = 32; 165 | } 166 | if (record_item->prefix < 32) { 167 | D(printf("Specifying a subnet is not supported in current version. at %td\n", prefix_end - copy)); 168 | err = -EINVAL; 169 | goto cleanup_copy; 170 | } 171 | addr = xtables_numeric_to_ipaddr(current); 172 | if (!addr) { 173 | D(printf("IPv4 address (%s) is invalid. at %td\n", current, current - copy)); 174 | err = -EINVAL; 175 | goto cleanup_copy; 176 | } 177 | memcpy(&record_item->ip, addr, sizeof(struct in_addr)); 178 | D(printf("Parsed IPv4 address: %s/%d. at %td\n", current, 179 | record_item->prefix, current - copy)); 180 | } 181 | if (count) 182 | *count = i; 183 | if (current) { 184 | D(printf("Too many IPv4 addresses. limit: %zu\n", max_count)); 185 | err = E2BIG; 186 | goto cleanup_copy; 187 | } 188 | cleanup_copy: 189 | free(copy); 190 | return err; 191 | } 192 | 193 | static int vfree_dns_mt_parse(int c, char **argv, int invert, 194 | unsigned int *flags, const void *entry, struct xt_entry_match **match) 195 | { 196 | struct xt_vfree_dns_mtinfo *info = (void *)(*match)->data; 197 | int err; 198 | size_t records_count; 199 | switch (c) { 200 | case '1': // --a 201 | xtables_param_act(XTF_ONLY_ONCE, "xt_vfree_dns", "--a", *flags & XT_VFREE_DNS_A); 202 | xtables_param_act(XTF_NO_INVERT, "xt_vfree_dns", "--a", invert); 203 | if (*flags & XT_VFREE_DNS_AAAA) { 204 | xtables_error(PARAMETER_PROBLEM, "xt_vfree_dns: " 205 | "You can specify either \"--a\" or \"--aaaa\", not both.\n"); 206 | } 207 | *flags |= XT_VFREE_DNS_A; 208 | info->flags = *flags; 209 | err = parse_address_list4(optarg, (struct vfree_dns_a_item *)info->argument, MAX_A_RECORDS, &records_count); 210 | if (err) { 211 | xtables_error(PARAMETER_PROBLEM, "xt_vfree_dns: " 212 | "Failed to parse IPv4 address list, error code %d\n", err); 213 | } 214 | info->records_count = (uint16_t)records_count; // records_count <= MAX_RECORDS 215 | D(printf("parsed record count = %d\n", info->records_count)); 216 | return true; 217 | case '2': // --aaaa 218 | xtables_param_act(XTF_ONLY_ONCE, "xt_vfree_dns", "--aaaa", *flags & XT_VFREE_DNS_AAAA); 219 | xtables_param_act(XTF_NO_INVERT, "xt_vfree_dns", "--aaaa", invert); 220 | if (*flags & XT_VFREE_DNS_A) { 221 | xtables_error(PARAMETER_PROBLEM, "xt_vfree_dns: " 222 | "You can specify either \"--a\" or \"--aaaa\", not both.\n"); 223 | } 224 | *flags |= XT_VFREE_DNS_AAAA; 225 | info->flags = *flags; 226 | err = parse_address_list6(optarg, (struct vfree_dns_aaaa_item *)info->argument, MAX_AAAA_RECORDS, &records_count); 227 | if (err) { 228 | xtables_error(PARAMETER_PROBLEM, "xt_vfree_dns: " 229 | "Failed to parse IPv6 address list, error code %d\n", err); 230 | } 231 | info->records_count = (uint16_t)records_count; // records_count <= MAX_RECORDS 232 | D(printf("parsed record count = %d\n", info->records_count)); 233 | return true; 234 | } 235 | return false; 236 | } 237 | 238 | static void vfree_dns_mt_check(unsigned int flags) 239 | { 240 | if (!(flags & XT_VFREE_DNS_A) && !(flags & XT_VFREE_DNS_AAAA)) 241 | xtables_error(PARAMETER_PROBLEM, "xt_vfree_dns: Either \"--a\" or \"--aaaa\" parameter is required."); 242 | } 243 | 244 | static void vfree_dns_mt_print(const void *entry, 245 | const struct xt_entry_match *match, int numeric) 246 | { 247 | const struct xt_vfree_dns_mtinfo *info = (const void *)match->data; 248 | struct vfree_dns_a_item *a_item; 249 | struct vfree_dns_aaaa_item *aaaa_item; 250 | int i; 251 | printf(" vfree_dns"); 252 | if (info->flags & XT_VFREE_DNS_A) { 253 | printf(" IN A"); 254 | for (i = 0; i < info->records_count; ++i) { 255 | a_item = (struct vfree_dns_a_item *)info->argument + i; 256 | printf(" %s", xtables_ipaddr_to_numeric((const struct in_addr *)&a_item->ip)); 257 | if (a_item->prefix < 32) 258 | printf("/%u", a_item->prefix); 259 | } 260 | } 261 | else if (info->flags & XT_VFREE_DNS_AAAA) { 262 | printf(" IN AAAA"); 263 | for (i = 0; i < info->records_count; ++i) { 264 | aaaa_item = (struct vfree_dns_aaaa_item *)info->argument + i; 265 | printf(" %s", xtables_ip6addr_to_numeric((const struct in6_addr *)&aaaa_item->ip)); 266 | if (aaaa_item->prefix < 128) 267 | printf("/%u", aaaa_item->prefix); 268 | } 269 | } 270 | } 271 | 272 | static void vfree_dns_mt_save(const void *entry, const struct xt_entry_match *match) 273 | { 274 | const struct xt_vfree_dns_mtinfo *info = (const void *)match->data; 275 | struct vfree_dns_a_item *a_item; 276 | struct vfree_dns_aaaa_item *aaaa_item; 277 | int i; 278 | if (info->flags & XT_VFREE_DNS_A) { 279 | printf(" --a"); 280 | for (i = 0; i < info->records_count; ++i) { 281 | a_item = (struct vfree_dns_a_item *)info->argument + i; 282 | printf(i > 0 ? ",%s" : " %s", xtables_ipaddr_to_numeric((const struct in_addr *)&a_item->ip)); 283 | if (a_item->prefix < 32) 284 | printf("/%d", a_item->prefix); 285 | } 286 | } 287 | else if (info->flags & XT_VFREE_DNS_AAAA) { 288 | printf(" --aaaa"); 289 | for (i = 0; i < info->records_count; ++i) { 290 | aaaa_item = (struct vfree_dns_aaaa_item *)info->argument + i; 291 | printf(i > 0 ? ",%s" : " %s", xtables_ip6addr_to_numeric((const struct in6_addr *)&aaaa_item->ip)); 292 | if (aaaa_item->prefix < 128) 293 | printf("/%u", aaaa_item->prefix); 294 | } 295 | } 296 | } 297 | 298 | static struct xtables_match vfree_dns_mt_reg = { 299 | .version = XTABLES_VERSION, 300 | .name = "vfree_dns", 301 | .revision = 0, 302 | .family = PF_INET, 303 | .size = XT_ALIGN(sizeof(struct xt_vfree_dns_mtinfo)), 304 | .userspacesize = offsetof(struct xt_vfree_dns_mtinfo, priv), 305 | .help = vfree_dns_mt_help, 306 | .init = vfree_dns_mt_init, 307 | .parse = vfree_dns_mt_parse, 308 | .final_check = vfree_dns_mt_check, 309 | .print = vfree_dns_mt_print, 310 | .save = vfree_dns_mt_save, 311 | .extra_opts = vfree_dns_mt_opts, 312 | }; 313 | 314 | static struct xtables_match vfree_dns_mt6_reg = { 315 | .version = XTABLES_VERSION, 316 | .name = "vfree_dns", 317 | .revision = 0, 318 | .family = PF_INET6, 319 | .size = XT_ALIGN(sizeof(struct xt_vfree_dns_mtinfo)), 320 | .userspacesize = offsetof(struct xt_vfree_dns_mtinfo, priv), 321 | .help = vfree_dns_mt_help, 322 | .init = vfree_dns_mt_init, 323 | .parse = vfree_dns_mt_parse, 324 | .final_check = vfree_dns_mt_check, 325 | .print = vfree_dns_mt_print, 326 | .save = vfree_dns_mt_save, 327 | .extra_opts = vfree_dns_mt_opts, 328 | }; 329 | 330 | static void _init(void) 331 | { 332 | xtables_register_match(&vfree_dns_mt_reg); 333 | xtables_register_match(&vfree_dns_mt6_reg); 334 | } 335 | -------------------------------------------------------------------------------- /src/libxt_vfree_http/.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.so 3 | -------------------------------------------------------------------------------- /src/libxt_vfree_http/Makefile: -------------------------------------------------------------------------------- 1 | # VFREE HijackFilter 2 | # Copyright (C) 2016 Rayson Zhu 3 | # 4 | # This program is free software: you can redistribute it and/or modify 5 | # it under the terms of the GNU General Public License as published by 6 | # the Free Software Foundation, either version 3 of the License, or 7 | # (at your option) any later version. 8 | # 9 | # This program is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU General Public License 15 | # along with this program. If not, see . 16 | 17 | XTABLES_LIBS := libxt_vfree_http.so 18 | INCLUDES := -I../xt_vfree_http 19 | 20 | DESTDIR ?= 21 | PREFIX ?= /usr/local 22 | INSTALL := install -c 23 | INSTALLDATA := install -m 644 24 | XTABLES_LIBDIR := $(shell pkg-config --variable=xtlibdir xtables) 25 | 26 | INSTALL_XTABLES_LIBDIR = $(DESTDIR)/$(XTABLES_LIBDIR) 27 | DEBUG ?= 0 28 | ifeq ($(DEBUG), 1) 29 | CFLAGS :=-Wall -g3 -DDEBUG 30 | else 31 | CFLAGS :=-O2 -Wall -DNDEBUG 32 | endif 33 | 34 | all: build 35 | 36 | check: 37 | ifeq ($(XTABLES_LIBDIR),) 38 | $(error 'Failed to locate your Xtables extension path. Please try defining XTABLES_LIBDIR.') 39 | endif 40 | 41 | build: check $(XTABLES_LIBS) 42 | 43 | lib%.so: lib%.o 44 | @$(CC) -shared -fPIC -o $@ $^ 45 | 46 | lib%.o: lib%.c 47 | @$(CC) $(CFLAGS) $(INCLUDES) -D_INIT=lib$*_init -fPIC -c -o $@ $< 48 | 49 | clean: 50 | @rm -f *.o $(XTABLES_LIBS) 51 | 52 | install: build 53 | mkdir -p $(INSTALL_XTABLES_LIBDIR) 54 | $(INSTALL) -t $(INSTALL_XTABLES_LIBDIR) $(XTABLES_LIBS) 55 | 56 | uninstall: 57 | rm -f $(XTABLES_LIBS:%=$(INSTALL_XTABLES_LIBDIR)/%) 58 | 59 | .PHONY: all check build clean install uninstall 60 | .PHONY: modules modules/clean modules/install modules/uninstall 61 | -------------------------------------------------------------------------------- /src/libxt_vfree_http/libxt_vfree_http.c: -------------------------------------------------------------------------------- 1 | /* 2 | * VFREE HijackFilter 3 | * Copyright (C) 2016 Rayson Zhu 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include "xt_vfree_http.h" 29 | 30 | #if DEBUG 31 | #define D(x) x 32 | #else 33 | #define D(x) 34 | #endif 35 | 36 | static const struct option vfree_http_mt_opts[] = { 37 | {.name = "version", .has_arg = true, .val = '1'}, 38 | {.name = "status", .has_arg = true, .val = '2'}, 39 | {NULL}, 40 | }; 41 | 42 | static void vfree_http_mt_help(void) 43 | { 44 | printf( 45 | "vfree_http match " 46 | "\noptions:\n" 47 | "\t--version\tHTTP version (1.0, 1.1, 1), default 1\n" 48 | "\t--status\tHTTP status code\n" 49 | ); 50 | } 51 | 52 | static void vfree_http_mt_init(struct xt_entry_match *match) 53 | { 54 | } 55 | 56 | static int vfree_http_mt_parse(int c, char **argv, int invert, 57 | unsigned int *flags, const void *entry, struct xt_entry_match **match) 58 | { 59 | return false; 60 | } 61 | 62 | static void vfree_http_mt_check(unsigned int flags) 63 | { 64 | } 65 | 66 | static void vfree_http_mt_print(const void *entry, 67 | const struct xt_entry_match *match, int numeric) 68 | { 69 | } 70 | 71 | static void vfree_http_mt_save(const void *entry, const struct xt_entry_match *match) 72 | { 73 | } 74 | 75 | static struct xtables_match vfree_http_mt_reg = { 76 | .version = XTABLES_VERSstatic void vfree_http_mt_save(const void *entry, const struct xt_entry_match *match) 77 | { 78 | .revision = 0, 79 | .family = PF_INET, 80 | .size = XT_ALIGN(sizeof(struct xt_vfree_http_mtinfo)), 81 | .userspacesize = offsetof(struct xt_vfree_http_mtinfo, priv), 82 | .help = vfree_http_mt_help, 83 | .init = vfree_http_mt_init, 84 | .parse = vfree_http_mt_parse, 85 | .final_check = vfree_http_mt_check, 86 | .print = vfree_http_mt_print, 87 | .save = vfree_http_mt_save, 88 | .extra_opts = vfree_http_mt_opts, 89 | }; 90 | 91 | static struct xtables_match vfree_http_mt6_reg = { 92 | .version = XTABLES_VERSION, 93 | .name = "vfree_http", 94 | .revision = 0, 95 | .family = PF_INET6, 96 | .size = XT_ALIGN(sizeof(struct xt_vfree_http_mtinfo)), 97 | .userspacesize = offsetof(struct xt_vfree_http_mtinfo, priv), 98 | .help = vfree_http_mt_help, 99 | .init = vfree_http_mt_init, 100 | .parse = vfree_http_mt_parse, 101 | .final_check = vfree_http_mt_check, 102 | .print = vfree_http_mt_print, 103 | .save = vfree_http_mt_save, 104 | .extra_opts = vfree_http_mt_opts, 105 | }; 106 | 107 | static void _init(void) 108 | { 109 | xtables_register_match(&vfree_http_mt_reg); 110 | xtables_register_match(&vfree_http_mt6_reg); 111 | } 112 | -------------------------------------------------------------------------------- /src/xt_vfree_dns/.gitignore: -------------------------------------------------------------------------------- 1 | *.mod.c 2 | Module.symvers 3 | modules.order 4 | -------------------------------------------------------------------------------- /src/xt_vfree_dns/Makefile: -------------------------------------------------------------------------------- 1 | # VFREE HijackFilter 2 | # Copyright (C) 2016 Rayson Zhu 3 | # 4 | # This program is free software: you can redistribute it and/or modify 5 | # it under the terms of the GNU General Public License as published by 6 | # the Free Software Foundation, either version 3 of the License, or 7 | # (at your option) any later version. 8 | # 9 | # This program is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU General Public License 15 | # along with this program. If not, see . 16 | 17 | obj-m += xt_vfree_dns.o 18 | 19 | DESTDIR ?= 20 | PREFIX ?= /usr/local 21 | MODULES_DIR ?= /lib/modules/$(shell uname -r) 22 | INSTALL := install -c 23 | INSTALLDATA := install -m 644 24 | 25 | MODULES = $(obj-m:.o=.ko) 26 | KERNEL_DIR = ${MODULES_DIR}/build 27 | INSTALL_MOD_PATH = $(DESTDIR) 28 | INSTALL_MOD_FULLPATH = $(DESTDIR)/$(MODULES_DIR)/extra/vfree 29 | 30 | DEBUG ?= 0 31 | ifeq ($(DEBUG), 1) 32 | ccflags-y :=-DDEBUG 33 | else 34 | ccflags-y :=-DNDEBUG 35 | endif 36 | 37 | AUTO_DEPMOD ?= 1 38 | 39 | all: modules 40 | 41 | check: 42 | ifeq ($(MODULES_DIR),/lib/modules/) 43 | $(error 'Failed to locate your kernel module path. Please try defining MODULES_DIR.') 44 | endif 45 | 46 | modules: check 47 | @echo "Building kernel modules: $(obj-m)" 48 | @make -C ${KERNEL_DIR} M=$$PWD $@ 49 | 50 | install: modules 51 | mkdir -p $(INSTALL_MOD_FULLPATH) 52 | #make -C ${KERNEL_DIR} M=$$PWD $@ 53 | @$(INSTALL) -t $(INSTALL_MOD_FULLPATH) $(MODULES) 54 | ifeq ($(AUTO_DEPMOD), 1) 55 | @depmod 56 | endif 57 | 58 | uninstall: 59 | @rm -f $(MODULES:%=$(INSTALL_MOD_FULLPATH)/%) 60 | ifeq ($(AUTO_DEPMOD), 1) 61 | @depmod 62 | endif 63 | 64 | clean: 65 | @make -C ${KERNEL_DIR} M=$$PWD $@ 66 | 67 | .PHONY: all check install uninstall clean modules 68 | -------------------------------------------------------------------------------- /src/xt_vfree_dns/dns.h: -------------------------------------------------------------------------------- 1 | /* 2 | * VFREE HijackFilter 3 | * Copyright (C) 2016 Rayson Zhu 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | */ 19 | #ifndef _HIJACK_FILTER_DNS_H_ 20 | #define _HIJACK_FILTER_DNS_H_ 21 | 22 | /* 23 | * DNS header and constants 24 | * see https://www.ietf.org/rfc/rfc1035.txt 25 | */ 26 | 27 | /* DNS header */ 28 | struct dns_hdr 29 | { 30 | __u16 id; 31 | #if defined(__LITTLE_ENDIAN_BITFIELD) 32 | __u16 rd :1, 33 | tc :1, 34 | aa :1, 35 | opcode :4, 36 | qr :1, 37 | 38 | rcode :4, 39 | cd :1, 40 | ad :1, 41 | z :1, 42 | ra :1; 43 | #elif defined(__BIG_ENDIAN_BITFIELD) 44 | __u16 qr :1, 45 | opcode :4, 46 | aa :1, 47 | tc :1, 48 | rd :1, 49 | 50 | ra :1, 51 | z :1, 52 | ad :1, 53 | cd :1, 54 | rcode :4; 55 | #else 56 | #error "Adjust your defines" 57 | #endif 58 | __u16 qdcount; // number of question entries 59 | __u16 ancount; // number of answer entries 60 | __u16 nscount; // number of authority entries 61 | __u16 arcount; // number of additional entries 62 | }; 63 | 64 | /* CLASS & QCLASS values*/ 65 | enum { 66 | DNS_CLASS_IN = 0x0001, 67 | DNS_CLASS_CS = 0x0002, 68 | DNS_CLASS_CH = 0x0003, 69 | DNS_CLASS_HS = 0x0004, 70 | DNS_QCLASS_IN = DNS_CLASS_IN, 71 | DNS_QCLASS_CS = DNS_CLASS_CS, 72 | DNS_QCLASS_CH = DNS_CLASS_CH, 73 | DNS_QCLASS_HS = DNS_CLASS_HS, 74 | DNS_QCLASS_ANY = 0x00ff, 75 | }; 76 | 77 | /* TYPE & QTYPE values*/ 78 | enum { 79 | DNS_TYPE_A = 0x0001, 80 | DNS_TYPE_NS = 0x0002, 81 | DNS_TYPE_CNAME = 0x0005, 82 | DNS_TYPE_PTR = 0x000c, 83 | DNS_TYPE_MX = 0x000f, 84 | DNS_TYPE_TXT = 0x0010, 85 | DNS_TYPE_AAAA = 0x001c, 86 | DNS_QTYPE_A = DNS_TYPE_A, 87 | DNS_QTYPE_NS = DNS_TYPE_NS, 88 | DNS_QTYPE_CNAME = DNS_TYPE_CNAME, 89 | DNS_QTYPE_PTR = DNS_TYPE_PTR, 90 | DNS_QTYPE_MX = DNS_TYPE_MX, 91 | DNS_QTYPE_TXT = DNS_TYPE_TXT, 92 | DNS_QTYPE_AAAA = DNS_TYPE_AAAA, 93 | DNS_QTYPE_ANY = 0x00ff, 94 | }; 95 | 96 | #endif /* end of include guard: _HIJACK_FILTER_DNS_H_ */ 97 | -------------------------------------------------------------------------------- /src/xt_vfree_dns/xt_vfree_dns.c: -------------------------------------------------------------------------------- 1 | /* 2 | * VFREE HijackFilter 3 | * Copyright (C) 2016 Rayson Zhu 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | */ 19 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include "xt_vfree_dns.h" 33 | #include "dns.h" 34 | 35 | #define get_u8(X, O) (*(const __u8 *)((X) + O)) 36 | #define get_u16(X, O) get_unaligned((const __u16 *)((X) + O)) 37 | #define get_u32(X, O) get_unaligned((const __u32 *)((X) + O)) 38 | 39 | union vfree_dns_record_group { 40 | void *data; 41 | struct vfree_dns_a_item *a; 42 | struct vfree_dns_aaaa_item *aaaa; 43 | }; 44 | 45 | struct vfree_dns_record_group_kernel { 46 | uint16_t flags; 47 | uint16_t count; 48 | union vfree_dns_record_group records; 49 | }; 50 | 51 | static const struct vfree_dns_a_item *binarry_search4(const struct vfree_dns_a_item *addresses, size_t count, const uint32_t *target) 52 | { 53 | size_t left = 0, right = count, medium; 54 | while (left < right) { 55 | medium = left + (right - left) / 2; 56 | if (addresses[medium].ip < *target) 57 | left = medium + 1; 58 | else if (addresses[medium].ip > *target) 59 | right = medium; 60 | else 61 | return addresses + medium; 62 | } 63 | return false; 64 | } 65 | 66 | static const struct vfree_dns_aaaa_item *binarry_search6(const struct vfree_dns_aaaa_item *addresses, size_t count, const struct in6_addr *target) 67 | { 68 | size_t left = 0, right = count, medium; 69 | int cmp; 70 | while (left < right) { 71 | medium = left + (right - left) / 2; 72 | cmp = ipv6_addr_cmp((const struct in6_addr *)addresses[medium].ip, target); 73 | if (cmp < 0) 74 | left = medium + 1; 75 | else if (cmp > 0) 76 | right = medium; 77 | else 78 | return addresses + medium; 79 | } 80 | return false; 81 | } 82 | 83 | /* 84 | * @dns_message: pointer to DNS header 85 | * @size: size of DNS message, including DNS header 86 | */ 87 | static bool record_match(const struct vfree_dns_record_group_kernel *record_group, const unsigned char *dns_message, unsigned int size) 88 | { 89 | const struct dns_hdr *dns_header = (const struct dns_hdr *)dns_message; 90 | int i; 91 | uint16_t qd, an; 92 | const unsigned char *p, *tail; 93 | 94 | if (size < sizeof(struct dns_hdr)) { 95 | pr_devel("message shorter than DNS header\n"); 96 | return false; 97 | } 98 | 99 | pr_devel("DNS ID: %X\n", ntohs(dns_header->id)); 100 | qd = ntohs(dns_header->qdcount); 101 | pr_devel(" QDCOUNT: %d\n", qd); 102 | an = ntohs(dns_header->ancount); 103 | pr_devel(" ANCOUNT: %d\n", an); 104 | pr_devel(" NSCOUNT: %d\n", ntohs(dns_header->nscount)); 105 | pr_devel(" ARCOUNT: %d\n", ntohs(dns_header->arcount)); 106 | 107 | if (!dns_header->qr) { 108 | pr_devel("will not match a DNS request\n"); 109 | return false; 110 | } 111 | 112 | p = dns_message + sizeof(struct dns_hdr); 113 | tail = dns_message + size; 114 | 115 | // skip Question section 116 | for (i = 0; i < qd; ++i) { 117 | uint16_t qtype, qclass; 118 | if (p >= tail) { 119 | goto error_reached_end; 120 | } 121 | // skip a DNS question 122 | pr_devel("DNS question:\n"); 123 | // skip QNAME 124 | while (*p && !(*p & 0xC0)) { // is a label 125 | if (p + 1 + *p > tail) { 126 | goto error_reached_end; 127 | } 128 | pr_devel("%.*s.\n", *p, p + 1); // print this label 129 | p += 1 + *p; // skip this lable 130 | } 131 | if (*p) { // is a label pointer 132 | if (p + 2 > tail) { 133 | goto error_reached_end; 134 | } 135 | pr_devel("ptr=%d\n", ntohs(get_u16(p, 0)) & 0x3FFF); 136 | p += 2; // skip the pointer 137 | } else { // *p == 0, mark of DNS root in a QNAME 138 | if (p + 1 > tail) { 139 | goto error_reached_end; 140 | } 141 | p++; // skip the mark 142 | } 143 | if (p + 4 > tail) { 144 | goto error_reached_end; 145 | } 146 | qtype = ntohs(get_u16(p, 0)); 147 | qclass = ntohs(get_u16(p, 2)); 148 | pr_devel("qtype=%d, qclass=%d\n", qtype, qclass); 149 | p += 4; // move to next question 150 | } 151 | 152 | // visit DNS answer section 153 | for (i = 0; i < an; ++i) { 154 | // visit a DNS answer 155 | uint16_t type, class, rdlength; 156 | uint32_t ttl; 157 | if (p >= tail) { 158 | goto error_reached_end; 159 | } 160 | pr_devel("DNS answer:\n"); 161 | // visit NAME 162 | while (*p && !(*p & 0xC0)) { // is a label 163 | if (p + 1 + *p > tail) { 164 | goto error_reached_end; 165 | } 166 | pr_devel("%.*s.", *p, p + 1); // print this label 167 | p += 1 + *p; // skip this lable 168 | } 169 | if (*p) { // is a label pointer 170 | if (p + 2 > tail) { 171 | goto error_reached_end; 172 | } 173 | pr_devel("ptr=%d\n", ntohs(get_u16(p, 0)) & 0x3FFF); 174 | p += 2; // skip the pointer 175 | } else { // *p == 0, mark of DNS root in a QNAME 176 | if (p + 1 > tail) { 177 | goto error_reached_end; 178 | } 179 | p++; // skip the mark 180 | } 181 | if (p + 10 > tail) { 182 | goto error_reached_end; 183 | } 184 | type = ntohs(get_u16(p, 0)); 185 | class = ntohs(get_u16(p, 2)); 186 | ttl = ntohl(get_u32(p, 4)); 187 | rdlength = ntohs(get_u16(p, 8)); 188 | pr_devel("type=%d, qclass=%d, ttl=%d, rdlength=%d\n", type, class, ttl, rdlength); 189 | p += 10; // move to rdata 190 | if (p + rdlength > tail) { 191 | goto error_reached_end; 192 | } 193 | if (class != DNS_CLASS_IN) { 194 | pr_devel("will not match answer not for Internet\n"); 195 | continue; 196 | } 197 | if ((record_group->flags & XT_VFREE_DNS_A) && type == DNS_TYPE_A) { 198 | uint32_t address; 199 | if (rdlength != 4) { 200 | pr_devel("invalid A record length\n"); 201 | return false; 202 | } 203 | address = be32_to_cpu(get_u32(p, 0)); 204 | pr_devel("A %pI4\n", &address); 205 | if (binarry_search4(record_group->records.a, record_group->count, &address)) { 206 | pr_devel("address matches (answered address: %pI4)\n", &address); 207 | return true; 208 | } 209 | } 210 | else if ((record_group->flags & XT_VFREE_DNS_AAAA) && type == DNS_TYPE_AAAA) { 211 | struct in6_addr address; 212 | if (rdlength != 16) { 213 | pr_devel("invalid AAAA record length\n"); 214 | return false; 215 | } 216 | memcpy(&address, p, 16); 217 | pr_devel("A %pI6\n", &address); 218 | if (binarry_search6(record_group->records.aaaa, record_group->count, &address)) { 219 | pr_devel("address matches (answered address: %pI6)\n", &address); 220 | return true; 221 | } 222 | } 223 | else { 224 | print_hex_dump_bytes("RDATA: ", DUMP_PREFIX_NONE, p, rdlength); 225 | } 226 | p += rdlength; // move to next answer 227 | } 228 | return false; 229 | error_reached_end: 230 | pr_devel("unexpectly reached the end of a DNS message\n"); 231 | return false; 232 | } 233 | 234 | static bool vfree_dns_mt4(const struct sk_buff *skb, struct xt_action_param *par) 235 | { 236 | const struct xt_vfree_dns_mtinfo *info = par->matchinfo; 237 | 238 | const struct iphdr *network_header; // ip header struct 239 | unsigned int hlen; 240 | //const struct tcphdr *tcp_header; // tcp header struct 241 | const struct udphdr *udp_header; // udp header struct 242 | //int i; 243 | unsigned char *dns_message; 244 | 245 | if (!info->priv) 246 | return false; 247 | 248 | /* must not be a fragment */ 249 | if (par->fragoff != 0) { 250 | pr_devel("offset found %d\n", par->fragoff); 251 | return false; 252 | } 253 | 254 | /* make sure that skb is linear */ 255 | if (skb_is_nonlinear(skb)) { 256 | pr_devel("nonlinear skb found\n"); 257 | return false; 258 | } 259 | 260 | network_header = (const struct iphdr *)skb_network_header(skb); 261 | hlen = ntohs(network_header->tot_len) - ntohs(network_header->ihl * 4); /* hlen = packet-data length */ 262 | 263 | if (network_header->protocol == IPPROTO_UDP){ 264 | udp_header = (const struct udphdr *)skb_transport_header(skb); 265 | pr_devel("---------- matching packet ----------\n"); 266 | //pr_devel(" udp_header - network_header = %d\n", (void*)udp_header - (void*)network_header); 267 | pr_devel(" skb->head = %p, skb->data = %p, skb->tail = %d, skb->end = %d, skb->len = %d, skb->data_len= %d\n", 268 | skb->head, skb->data, skb->tail, skb->end, skb->len, skb->data_len); 269 | pr_devel(" network_header = %p, skb->mac_header = %d, skb->network_header = %d, skb->transport_header = %d\n", 270 | network_header, skb->mac_header, skb->network_header, skb->transport_header); 271 | pr_devel(" IN: %s\n",skb->dev->name); 272 | pr_devel(" Protocol: UDP\n"); 273 | pr_devel(" Length: %d\n",skb->len); 274 | pr_devel(" TTL: %d\n",network_header->ttl); 275 | pr_devel(" ID: %d\n",network_header->id); 276 | pr_devel(" S_PORT: %d\n",ntohs((unsigned short int) udp_header->source)); 277 | pr_devel(" D_PORT: %d\n",ntohs((unsigned short int) udp_header->dest)); 278 | //printk(" @_SRC: %d.%d.%d.%d\n",NIPQUAD(network_header->saddr)); 279 | //printk(" @_DST: %d.%d.%d.%d\n",NIPQUAD(network_header->daddr)); 280 | pr_devel(" SRC: %pI4\n", &network_header->saddr); 281 | pr_devel(" DST: %pI4\n", &network_header->daddr); 282 | 283 | if (hlen < sizeof(struct udphdr)) { 284 | pr_devel("UDP header indicated packet larger than it is\n"); 285 | return false; 286 | } 287 | hlen -= sizeof(struct udphdr); /* hlen = UDP payload length */ 288 | dns_message = ((unsigned char *)udp_header) + sizeof(struct udphdr); 289 | //return false; 290 | return record_match(info->priv, dns_message, hlen); 291 | } 292 | return false; 293 | } 294 | 295 | static bool vfree_dns_mt6(const struct sk_buff *skb, struct xt_action_param *par) 296 | { 297 | const struct xt_vfree_dns_mtinfo *info = par->matchinfo; 298 | 299 | const struct ipv6hdr *network_header; // ip header struct 300 | unsigned int hlen; 301 | //const struct tcphdr *tcp_header; // tcp header struct 302 | const struct udphdr *udp_header; // udp header struct 303 | //int i; 304 | unsigned char *dns_message; 305 | 306 | if (!info->priv) 307 | return false; 308 | 309 | /* must not be a fragment */ 310 | if (par->fragoff != 0) { 311 | pr_devel("offset found %d\n", par->fragoff); 312 | return false; 313 | } 314 | 315 | /* make sure that skb is linear */ 316 | if (skb_is_nonlinear(skb)) { 317 | pr_devel("nonlinear skb found\n"); 318 | return false; 319 | } 320 | 321 | network_header = (const struct ipv6hdr *)skb_network_header(skb); 322 | hlen = ntohs(network_header->payload_len); /* hlen = packet-data length */ 323 | 324 | if (network_header->nexthdr == IPPROTO_UDP){ 325 | udp_header = (const struct udphdr *)skb_transport_header(skb); 326 | pr_devel("---------- matching packet ----------\n"); 327 | //pr_devel(" udp_header - network_header = %d\n", (void*)udp_header - (void*)network_header); 328 | pr_devel(" skb->head = %p, skb->mac_header = %d, skb->network_header = %d, skb->transport_header = %d\n", 329 | skb->head, skb->mac_header, skb->network_header, skb->transport_header); 330 | pr_devel(" IN: %s\n",skb->dev->name); 331 | pr_devel(" Protocol: UDP\n"); 332 | pr_devel(" Length: %d\n",skb->len); 333 | pr_devel(" TTL: %d\n",network_header->hop_limit); 334 | //pr_devel(" ID: %d\n",network_header->id); 335 | pr_devel(" S_PORT: %d\n",ntohs((unsigned short int) udp_header->source)); 336 | pr_devel(" D_PORT: %d\n",ntohs((unsigned short int) udp_header->dest)); 337 | //printk(" @_SRC: %d.%d.%d.%d\n",NIPQUAD(network_header->saddr)); 338 | //printk(" @_DST: %d.%d.%d.%d\n",NIPQUAD(network_header->daddr)); 339 | pr_devel(" SRC: %pI6\n", &network_header->saddr); 340 | pr_devel(" DST: %pI6\n", &network_header->daddr); 341 | 342 | if (hlen < sizeof(struct udphdr)) { 343 | pr_devel("UDP header indicated packet larger than it is\n"); 344 | return false; 345 | } 346 | hlen -= sizeof(struct udphdr); /* hlen = UDP payload length */ 347 | dns_message = ((unsigned char *)udp_header) + sizeof(struct udphdr); 348 | //return false; 349 | return record_match(info->priv, dns_message, hlen); 350 | } 351 | return false; 352 | } 353 | 354 | static inline int vfree_dns_a_item_compare(const void *lhs, const void *rhs) 355 | { 356 | struct vfree_dns_a_item *l = (struct vfree_dns_a_item *)lhs, 357 | *r = (struct vfree_dns_a_item *)rhs; 358 | return l->ip < r->ip ? -1 : (l->ip > r->ip ? 1 : 0); 359 | } 360 | 361 | static inline int vfree_dns_aaaa_item_compare(const void *lhs, const void *rhs) 362 | { 363 | struct vfree_dns_aaaa_item *l = (struct vfree_dns_aaaa_item *)lhs, 364 | *r = (struct vfree_dns_aaaa_item *)rhs; 365 | return ipv6_addr_cmp((const struct in6_addr *)l->ip, (const struct in6_addr *)r->ip); 366 | } 367 | 368 | static int vfree_dns_mt_check(const struct xt_mtchk_param *par) 369 | { 370 | int ret = 0, i; 371 | struct xt_vfree_dns_mtinfo *info = par->matchinfo; 372 | struct vfree_dns_a_item *a_item; 373 | struct vfree_dns_aaaa_item *aaaa_item; 374 | size_t records_size; 375 | 376 | pr_devel("vfree_dns_mt_check() was called.\n"); 377 | 378 | if (info->flags & XT_VFREE_DNS_A) { 379 | if (info->flags & XT_VFREE_DNS_AAAA) { 380 | pr_devel("You can specify either XT_VFREE_DNS_A or XT_VFREE_DNS_AAAA, not both.\n"); 381 | return -EINVAL; 382 | } 383 | if (info->records_count == 0) { 384 | pr_devel("No A records to match.\n"); 385 | return -EINVAL; 386 | } 387 | pr_devel("There are %zu A records to match.\n", (size_t)info->records_count); 388 | if (info->records_count > MAX_A_RECORDS) { 389 | pr_devel("A records limit exceeded: %u\n", MAX_A_RECORDS); 390 | return -EINVAL; 391 | } 392 | info->priv = kmalloc(sizeof(struct vfree_dns_record_group_kernel), GFP_KERNEL); 393 | pr_devel("kmalloc(): info->priv = %p\n", info->priv); 394 | if (!info->priv) 395 | return -ENOMEM; 396 | records_size = sizeof(struct vfree_dns_a_item) * info->records_count; 397 | info->priv->records.a = vmalloc(records_size); 398 | pr_devel("vmalloc(): info->priv->records.a = %p, records_size = %zu\n", info->priv->records.a, records_size); 399 | if (!info->priv->records.a) { 400 | ret = ENOMEM; 401 | goto cleanup_priv; 402 | } 403 | info->priv->flags = info->flags; 404 | info->priv->count = info->records_count; 405 | for (i = 0; i < info->records_count; ++i) { 406 | a_item = (struct vfree_dns_a_item *)info->argument + i; 407 | pr_devel("A record: %pI4/%d\n", &a_item->ip, a_item->prefix); 408 | info->priv->records.a[i].ip = be32_to_cpu(a_item->ip); 409 | info->priv->records.a[i].prefix = a_item->prefix; 410 | } 411 | sort(info->priv->records.a, info->priv->count, sizeof(struct vfree_dns_a_item), vfree_dns_a_item_compare, NULL); 412 | } 413 | else if (info->flags & XT_VFREE_DNS_AAAA) { 414 | if (info->records_count == 0) { 415 | pr_devel("No AAAA records to match.\n"); 416 | return -EINVAL; 417 | } 418 | pr_devel("There are %zu AAAA records to match.\n", (size_t)info->records_count); 419 | if (info->records_count > MAX_A_RECORDS) { 420 | pr_devel("AAAA records limit exceeded: %u\n", MAX_AAAA_RECORDS); 421 | return -EINVAL; 422 | } 423 | info->priv = kmalloc(sizeof(struct vfree_dns_record_group_kernel), GFP_KERNEL); 424 | pr_devel("kmalloc(): info->priv = %p\n", info->priv); 425 | if (!info->priv) 426 | return -ENOMEM; 427 | records_size = sizeof(struct vfree_dns_aaaa_item) * info->records_count; 428 | info->priv->records.aaaa = vmalloc(records_size); 429 | pr_devel("vmalloc(): info->priv->records.aaaa = %p, records_size = %zu\n", info->priv->records.a, records_size); 430 | if (!info->priv->records.aaaa) { 431 | ret = ENOMEM; 432 | goto cleanup_priv; 433 | } 434 | info->priv->flags = info->flags; 435 | info->priv->count = info->records_count; 436 | for (i = 0; i < info->records_count; ++i) { 437 | aaaa_item = (struct vfree_dns_aaaa_item *)info->argument + i; 438 | pr_devel("A record: %pI6/%d\n", &aaaa_item->ip, aaaa_item->prefix); 439 | memcpy(info->priv->records.aaaa[i].ip, aaaa_item->ip, 128); 440 | info->priv->records.aaaa[i].prefix = aaaa_item->prefix; 441 | } 442 | sort(info->priv->records.aaaa, info->priv->count, sizeof(struct vfree_dns_aaaa_item), vfree_dns_aaaa_item_compare, NULL); 443 | } 444 | else { 445 | pr_devel("Nothing to match.\n"); 446 | return -EINVAL; 447 | } 448 | 449 | return ret; 450 | cleanup_priv: 451 | kfree(info->priv); 452 | return ret; 453 | } 454 | 455 | static void vfree_dns_mt_destroy(const struct xt_mtdtor_param *par) 456 | { 457 | struct xt_vfree_dns_mtinfo *info = par->matchinfo; 458 | pr_devel("vfree_dns_mt_destroy() was called.\n"); 459 | if (info->priv) { 460 | if (info->priv->records.data) { 461 | pr_devel("vfree(): info->priv->records.data = %p\n", info->priv->records.data); 462 | vfree(info->priv->records.data); 463 | } 464 | pr_devel("kfree(): info->priv =%p\n", info->priv); 465 | kfree(info->priv); 466 | } 467 | } 468 | 469 | static struct xt_match vfree_dns_mt_reg[] __read_mostly = { 470 | { 471 | .name = "vfree_dns", 472 | .revision = 0, 473 | .family = NFPROTO_IPV4, 474 | .match = vfree_dns_mt4, 475 | .checkentry = vfree_dns_mt_check, 476 | .destroy = vfree_dns_mt_destroy, 477 | .matchsize = XT_ALIGN(sizeof(struct xt_vfree_dns_mtinfo)), 478 | .me = THIS_MODULE, 479 | }, 480 | { 481 | .name = "vfree_dns", 482 | .revision = 0, 483 | .family = NFPROTO_IPV6, 484 | .match = vfree_dns_mt6, 485 | .checkentry = vfree_dns_mt_check, 486 | .destroy = vfree_dns_mt_destroy, 487 | .matchsize = XT_ALIGN(sizeof(struct xt_vfree_dns_mtinfo)), 488 | .me = THIS_MODULE, 489 | }, 490 | 491 | }; 492 | 493 | static int __init xt_vfree_dns_init(void) 494 | { 495 | pr_devel("load xt_vfree_dns\n"); 496 | return xt_register_matches(vfree_dns_mt_reg, ARRAY_SIZE(vfree_dns_mt_reg)); 497 | } 498 | 499 | static void __exit xt_vfree_dns_exit(void) 500 | { 501 | xt_unregister_matches(vfree_dns_mt_reg, ARRAY_SIZE(vfree_dns_mt_reg)); 502 | pr_devel("unload xt_vfree_dns\n"); 503 | } 504 | 505 | module_init(xt_vfree_dns_init); 506 | module_exit(xt_vfree_dns_exit); 507 | 508 | MODULE_AUTHOR("Rayson Zhu "); 509 | MODULE_DESCRIPTION("vfree_dns match extension for Xtables"); 510 | MODULE_LICENSE("GPL"); 511 | MODULE_ALIAS("ipt_vfree_dns"); 512 | MODULE_ALIAS("ip6t_vfree_dns"); 513 | -------------------------------------------------------------------------------- /src/xt_vfree_dns/xt_vfree_dns.h: -------------------------------------------------------------------------------- 1 | /* 2 | * VFREE HijackFilter 3 | * Copyright (C) 2016 Rayson Zhu 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | */ 19 | #ifndef _XT_VFREE_DNS_H_ 20 | #define _XT_VFREE_DNS_H_ 21 | 22 | struct vfree_dns_a_item { 23 | uint32_t ip; // network byte order in userspace, host byte order in kernelspace 24 | uint8_t prefix; 25 | }; 26 | 27 | struct vfree_dns_aaaa_item { 28 | uint16_t ip[8]; // network byte order 29 | uint8_t prefix; 30 | }; 31 | 32 | enum { 33 | MAX_ARGUMENT_SIZE = 4096, 34 | MAX_A_RECORDS = MAX_ARGUMENT_SIZE / sizeof(struct vfree_dns_a_item), 35 | MAX_AAAA_RECORDS = MAX_ARGUMENT_SIZE / sizeof(struct vfree_dns_aaaa_item), 36 | }; 37 | 38 | enum { 39 | XT_VFREE_DNS_INV = 1 << 0, 40 | XT_VFREE_DNS_A = 1 << 1, 41 | XT_VFREE_DNS_AAAA = 1 << 2, 42 | }; 43 | 44 | struct xt_vfree_dns_mtinfo { 45 | uint16_t flags; 46 | uint16_t records_count; 47 | uint8_t argument[MAX_ARGUMENT_SIZE]; 48 | /* Used internally by the kernel */ 49 | struct vfree_dns_record_group_kernel *priv; 50 | }; 51 | 52 | #endif /* end of include guard: _XT_VFREE_DNS_H_ */ 53 | -------------------------------------------------------------------------------- /src/xt_vfree_http/.gitignore: -------------------------------------------------------------------------------- 1 | *.mod.c 2 | Module.symvers 3 | modules.order 4 | -------------------------------------------------------------------------------- /src/xt_vfree_http/Makefile: -------------------------------------------------------------------------------- 1 | # VFREE HijackFilter 2 | # Copyright (C) 2016 Rayson Zhu 3 | # 4 | # This program is free software: you can redistribute it and/or modify 5 | # it under the terms of the GNU General Public License as published by 6 | # the Free Software Foundation, either version 3 of the License, or 7 | # (at your option) any later version. 8 | # 9 | # This program is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU General Public License 15 | # along with this program. If not, see . 16 | 17 | obj-m += xt_vfree_http.o 18 | 19 | DESTDIR ?= 20 | PREFIX ?= /usr/local 21 | MODULES_DIR ?= /lib/modules/$(shell uname -r) 22 | INSTALL := install -c 23 | INSTALLDATA := install -m 644 24 | 25 | MODULES = $(obj-m:.o=.ko) 26 | KERNEL_DIR = ${MODULES_DIR}/build 27 | INSTALL_MOD_PATH = $(DESTDIR) 28 | INSTALL_MOD_FULLPATH = $(DESTDIR)/$(MODULES_DIR)/extra/vfree 29 | 30 | DEBUG ?= 0 31 | ifeq ($(DEBUG), 1) 32 | ccflags-y :=-DDEBUG 33 | else 34 | ccflags-y :=-DNDEBUG 35 | endif 36 | 37 | AUTO_DEPMOD ?= 1 38 | 39 | all: modules 40 | 41 | check: 42 | ifeq ($(MODULES_DIR),/lib/modules/) 43 | $(error 'Failed to locate your kernel module path. Please try defining MODULES_DIR.') 44 | endif 45 | 46 | modules: check 47 | @echo "Building kernel modules: $(obj-m)" 48 | @make -C ${KERNEL_DIR} M=$$PWD $@ 49 | 50 | install: modules 51 | mkdir -p $(INSTALL_MOD_FULLPATH) 52 | #make -C ${KERNEL_DIR} M=$$PWD $@ 53 | @$(INSTALL) -t $(INSTALL_MOD_FULLPATH) $(MODULES) 54 | ifeq ($(AUTO_DEPMOD), 1) 55 | @depmod 56 | endif 57 | 58 | uninstall: 59 | @rm -f $(MODULES:%=$(INSTALL_MOD_FULLPATH)/%) 60 | ifeq ($(AUTO_DEPMOD), 1) 61 | @depmod 62 | endif 63 | 64 | clean: 65 | @make -C ${KERNEL_DIR} M=$$PWD $@ 66 | 67 | .PHONY: all check install uninstall clean modules 68 | -------------------------------------------------------------------------------- /src/xt_vfree_http/xt_vfree_http.c: -------------------------------------------------------------------------------- 1 | /* 2 | * VFREE HijackFilter 3 | * Copyright (C) 2016 Rayson Zhu 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | */ 19 | 20 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | #include "xt_vfree_http.h" 30 | 31 | static bool vfree_http_mt4(const struct sk_buff *skb, struct xt_action_param *par) 32 | { 33 | return false; 34 | } 35 | 36 | static bool vfree_http_mt6(const struct sk_buff *skb, struct xt_action_param *par) 37 | { 38 | return false; 39 | } 40 | 41 | static ibxt_vfree_http xt_vfree_httpnt vfree_http_mt_check(const struct xt_mtchk_param *par) 42 | { 43 | return 0; 44 | } 45 | 46 | static void vfree_http_mt_destroy(const struct xt_mtdtor_param *par) 47 | { 48 | } 49 | 50 | static struct xt_match vfree_http_mt_reg[] __read_mostly = { 51 | { 52 | .name = "vfree_http", 53 | .revision = 0, 54 | .family = NFPROTO_IPV4, 55 | .match = vfree_http_mt4, 56 | .checkentry = vfree_http_mt_check, 57 | .destroy = vfree_http_mt_destroy, 58 | .matchsize = XT_ALIGN(sizeof(struct xt_vfree_http_mtinfo)), 59 | .me = THIS_MODULE, 60 | }, 61 | { 62 | .name = "vfree_http", 63 | .revision = 0, 64 | .family = NFPROTO_IPV6, 65 | .match = vfree_http_mt6, 66 | .checkentry = vfree_http_mt_check, 67 | .destroy = vfree_http_mt_destroy, 68 | .matchsize = XT_ALIGN(sizeof(struct xt_vfree_http_mtinfo)), 69 | .me = THIS_MODULE, 70 | }, 71 | 72 | }; 73 | 74 | static int __init xt_vfree_http_init(void) 75 | { 76 | pr_devel("load xt_vfree_http\n"); 77 | return xt_register_matches(vfree_http_mt_reg, ARRAY_SIZE(vfree_http_mt_reg)); 78 | } 79 | 80 | static void __exit xt_vfree_http_exit(void) 81 | { 82 | xt_unregister_matches(vfree_http_mt_reg, ARRAY_SIZE(vfree_http_mt_reg)); 83 | pr_devel("unload xt_vfree_http\n"); 84 | } 85 | 86 | module_init(xt_vfree_http_init); 87 | module_exit(xt_vfree_http_exit); 88 | 89 | MODULE_AUTHOR("Rayson Zhu "); 90 | MODULE_DESCRIPTION("vfree_http match extension for Xtables"); 91 | MODULE_LICENSE("GPL"); 92 | MODULE_ALIAS("ipt_vfree_http"); 93 | MODULE_ALIAS("ip6t_vfree_http"); 94 | -------------------------------------------------------------------------------- /src/xt_vfree_http/xt_vfree_http.h: -------------------------------------------------------------------------------- 1 | /* 2 | * VFREE HijackFilter 3 | * Copyright (C) 2016 Rayson Zhu 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | */ 19 | 20 | #ifndef _XT_FREE_HTTP_H_ 21 | #define _XT_FREE_HTTP_H_ 22 | 23 | struct xt_vfree_http_mtinfo { 24 | uint16_t flags; 25 | uint16_t http_version; 26 | uint16_t http_status; 27 | }; 28 | 29 | 30 | #endif /* end of include guard: _XT_FREE_HTTP_H_ */ 31 | --------------------------------------------------------------------------------