├── AUTHORS.txt ├── CMakeLists.txt ├── DNS ├── dns1.py ├── dns2.py └── dns3.py ├── LICENSE ├── Makefile ├── README.md ├── _config.yml ├── click ├── Makefile ├── ids.click ├── lb.click ├── lb1.click ├── lb2.click └── n9.click ├── firewall ├── Makefile ├── firewall_parent_proactive.py ├── l2_firewall.py ├── l2_firewall_example.py └── l2_learning.py ├── ik2220-assign-phase2-team5.tar.gz ├── shrinish.txt ├── src ├── dns_server.py ├── dnsrequest.py └── icmp_match_struct.py ├── sublime.gitignore ├── topology ├── MakeFile ├── phase2.py ├── topology_log.py └── topology_new.py └── zones.tar.gz /AUTHORS.txt: -------------------------------------------------------------------------------- 1 | *Vinayak Tejankar tejankar@kth.se 2 | Abhishek Dandekar dandekar@kth.se 3 | Shrinish Donde shrinish@kth.se 4 | Faizan Ahmed faizana@kth.se 5 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #application 2 | 3 | export POX_PATH := /opt/ik2220/pox 4 | export APP_PATH := ik2220 5 | app: 6 | #mkdir $$POX_PATH/ext/$$APP_PATH 7 | #python -m compileall ./sdn/task1/ 8 | cp sdn/task1/* $$POX_PATH/ext/ 9 | python -m compileall $$POX_PATH/ext/ 10 | sudo python $$POX_PATH/pox.py l2_firewall & 11 | #sudo python $$POX_PATH/pox.py $$APP_PATH.l2_firewall 12 | 13 | clean: 14 | sudo pkill -9 -f pox.py 15 | 16 | #topology 17 | 18 | topo: 19 | sudo python phase1.py 20 | 21 | clean: 22 | sudo mn -c 23 | -------------------------------------------------------------------------------- /DNS/dns1.py: -------------------------------------------------------------------------------- 1 | import socket, glob, json 2 | 3 | port = 53 4 | ip = '100.0.0.20' 5 | 6 | sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 7 | sock.bind((ip, port)) 8 | 9 | 10 | def load_zones(): 11 | 12 | jsonzone = {} 13 | zonefiles = glob.glob('zones/*.zone') 14 | 15 | for zone in zonefiles: 16 | with open(zone) as zonedata: 17 | data = json.load(zonedata) 18 | zonename = data["$origin"] 19 | jsonzone[zonename] = data 20 | return jsonzone 21 | 22 | zonedata = load_zones() 23 | 24 | def getflags(flags): 25 | 26 | byte1 = bytes(flags[:1]) 27 | byte2 = bytes(flags[1:2]) 28 | 29 | rflags = '' 30 | 31 | QR = '1' 32 | 33 | OPCODE = '' 34 | for bit in range(1,5): 35 | OPCODE += str(ord(byte1)&(1< 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 Affero General Public License is a free, copyleft license for 11 | software and other kinds of works, specifically designed to ensure 12 | cooperation with the community in the case of network server software. 13 | 14 | The licenses for most software and other practical works are designed 15 | to take away your freedom to share and change the works. By contrast, 16 | our General Public Licenses are intended to guarantee your freedom to 17 | share and change all versions of a program--to make sure it remains free 18 | software for all its users. 19 | 20 | When we speak of free software, we are referring to freedom, not 21 | price. Our General Public Licenses are designed to make sure that you 22 | have the freedom to distribute copies of free software (and charge for 23 | them if you wish), that you receive source code or can get it if you 24 | want it, that you can change the software or use pieces of it in new 25 | free programs, and that you know you can do these things. 26 | 27 | Developers that use our General Public Licenses protect your rights 28 | with two steps: (1) assert copyright on the software, and (2) offer 29 | you this License which gives you legal permission to copy, distribute 30 | and/or modify the software. 31 | 32 | A secondary benefit of defending all users' freedom is that 33 | improvements made in alternate versions of the program, if they 34 | receive widespread use, become available for other developers to 35 | incorporate. Many developers of free software are heartened and 36 | encouraged by the resulting cooperation. However, in the case of 37 | software used on network servers, this result may fail to come about. 38 | The GNU General Public License permits making a modified version and 39 | letting the public access it on a server without ever releasing its 40 | source code to the public. 41 | 42 | The GNU Affero General Public License is designed specifically to 43 | ensure that, in such cases, the modified source code becomes available 44 | to the community. It requires the operator of a network server to 45 | provide the source code of the modified version running there to the 46 | users of that server. Therefore, public use of a modified version, on 47 | a publicly accessible server, gives the public access to the source 48 | code of the modified version. 49 | 50 | An older license, called the Affero General Public License and 51 | published by Affero, was designed to accomplish similar goals. This is 52 | a different license, not a version of the Affero GPL, but Affero has 53 | released a new version of the Affero GPL which permits relicensing under 54 | this license. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | TERMS AND CONDITIONS 60 | 61 | 0. Definitions. 62 | 63 | "This License" refers to version 3 of the GNU Affero General Public License. 64 | 65 | "Copyright" also means copyright-like laws that apply to other kinds of 66 | works, such as semiconductor masks. 67 | 68 | "The Program" refers to any copyrightable work licensed under this 69 | License. Each licensee is addressed as "you". "Licensees" and 70 | "recipients" may be individuals or organizations. 71 | 72 | To "modify" a work means to copy from or adapt all or part of the work 73 | in a fashion requiring copyright permission, other than the making of an 74 | exact copy. The resulting work is called a "modified version" of the 75 | earlier work or a work "based on" the earlier work. 76 | 77 | A "covered work" means either the unmodified Program or a work based 78 | on the Program. 79 | 80 | To "propagate" a work means to do anything with it that, without 81 | permission, would make you directly or secondarily liable for 82 | infringement under applicable copyright law, except executing it on a 83 | computer or modifying a private copy. Propagation includes copying, 84 | distribution (with or without modification), making available to the 85 | public, and in some countries other activities as well. 86 | 87 | To "convey" a work means any kind of propagation that enables other 88 | parties to make or receive copies. Mere interaction with a user through 89 | a computer network, with no transfer of a copy, is not conveying. 90 | 91 | An interactive user interface displays "Appropriate Legal Notices" 92 | to the extent that it includes a convenient and prominently visible 93 | feature that (1) displays an appropriate copyright notice, and (2) 94 | tells the user that there is no warranty for the work (except to the 95 | extent that warranties are provided), that licensees may convey the 96 | work under this License, and how to view a copy of this License. If 97 | the interface presents a list of user commands or options, such as a 98 | menu, a prominent item in the list meets this criterion. 99 | 100 | 1. Source Code. 101 | 102 | The "source code" for a work means the preferred form of the work 103 | for making modifications to it. "Object code" means any non-source 104 | form of a work. 105 | 106 | A "Standard Interface" means an interface that either is an official 107 | standard defined by a recognized standards body, or, in the case of 108 | interfaces specified for a particular programming language, one that 109 | is widely used among developers working in that language. 110 | 111 | The "System Libraries" of an executable work include anything, other 112 | than the work as a whole, that (a) is included in the normal form of 113 | packaging a Major Component, but which is not part of that Major 114 | Component, and (b) serves only to enable use of the work with that 115 | Major Component, or to implement a Standard Interface for which an 116 | implementation is available to the public in source code form. A 117 | "Major Component", in this context, means a major essential component 118 | (kernel, window system, and so on) of the specific operating system 119 | (if any) on which the executable work runs, or a compiler used to 120 | produce the work, or an object code interpreter used to run it. 121 | 122 | The "Corresponding Source" for a work in object code form means all 123 | the source code needed to generate, install, and (for an executable 124 | work) run the object code and to modify the work, including scripts to 125 | control those activities. However, it does not include the work's 126 | System Libraries, or general-purpose tools or generally available free 127 | programs which are used unmodified in performing those activities but 128 | which are not part of the work. For example, Corresponding Source 129 | includes interface definition files associated with source files for 130 | the work, and the source code for shared libraries and dynamically 131 | linked subprograms that the work is specifically designed to require, 132 | such as by intimate data communication or control flow between those 133 | subprograms and other parts of the work. 134 | 135 | The Corresponding Source need not include anything that users 136 | can regenerate automatically from other parts of the Corresponding 137 | Source. 138 | 139 | The Corresponding Source for a work in source code form is that 140 | same work. 141 | 142 | 2. Basic Permissions. 143 | 144 | All rights granted under this License are granted for the term of 145 | copyright on the Program, and are irrevocable provided the stated 146 | conditions are met. This License explicitly affirms your unlimited 147 | permission to run the unmodified Program. The output from running a 148 | covered work is covered by this License only if the output, given its 149 | content, constitutes a covered work. This License acknowledges your 150 | rights of fair use or other equivalent, as provided by copyright law. 151 | 152 | You may make, run and propagate covered works that you do not 153 | convey, without conditions so long as your license otherwise remains 154 | in force. You may convey covered works to others for the sole purpose 155 | of having them make modifications exclusively for you, or provide you 156 | with facilities for running those works, provided that you comply with 157 | the terms of this License in conveying all material for which you do 158 | not control copyright. Those thus making or running the covered works 159 | for you must do so exclusively on your behalf, under your direction 160 | and control, on terms that prohibit them from making any copies of 161 | your copyrighted material outside their relationship with you. 162 | 163 | Conveying under any other circumstances is permitted solely under 164 | the conditions stated below. Sublicensing is not allowed; section 10 165 | makes it unnecessary. 166 | 167 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 168 | 169 | No covered work shall be deemed part of an effective technological 170 | measure under any applicable law fulfilling obligations under article 171 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 172 | similar laws prohibiting or restricting circumvention of such 173 | measures. 174 | 175 | When you convey a covered work, you waive any legal power to forbid 176 | circumvention of technological measures to the extent such circumvention 177 | is effected by exercising rights under this License with respect to 178 | the covered work, and you disclaim any intention to limit operation or 179 | modification of the work as a means of enforcing, against the work's 180 | users, your or third parties' legal rights to forbid circumvention of 181 | technological measures. 182 | 183 | 4. Conveying Verbatim Copies. 184 | 185 | You may convey verbatim copies of the Program's source code as you 186 | receive it, in any medium, provided that you conspicuously and 187 | appropriately publish on each copy an appropriate copyright notice; 188 | keep intact all notices stating that this License and any 189 | non-permissive terms added in accord with section 7 apply to the code; 190 | keep intact all notices of the absence of any warranty; and give all 191 | recipients a copy of this License along with the Program. 192 | 193 | You may charge any price or no price for each copy that you convey, 194 | and you may offer support or warranty protection for a fee. 195 | 196 | 5. Conveying Modified Source Versions. 197 | 198 | You may convey a work based on the Program, or the modifications to 199 | produce it from the Program, in the form of source code under the 200 | terms of section 4, provided that you also meet all of these conditions: 201 | 202 | a) The work must carry prominent notices stating that you modified 203 | it, and giving a relevant date. 204 | 205 | b) The work must carry prominent notices stating that it is 206 | released under this License and any conditions added under section 207 | 7. This requirement modifies the requirement in section 4 to 208 | "keep intact all notices". 209 | 210 | c) You must license the entire work, as a whole, under this 211 | License to anyone who comes into possession of a copy. This 212 | License will therefore apply, along with any applicable section 7 213 | additional terms, to the whole of the work, and all its parts, 214 | regardless of how they are packaged. This License gives no 215 | permission to license the work in any other way, but it does not 216 | invalidate such permission if you have separately received it. 217 | 218 | d) If the work has interactive user interfaces, each must display 219 | Appropriate Legal Notices; however, if the Program has interactive 220 | interfaces that do not display Appropriate Legal Notices, your 221 | work need not make them do so. 222 | 223 | A compilation of a covered work with other separate and independent 224 | works, which are not by their nature extensions of the covered work, 225 | and which are not combined with it such as to form a larger program, 226 | in or on a volume of a storage or distribution medium, is called an 227 | "aggregate" if the compilation and its resulting copyright are not 228 | used to limit the access or legal rights of the compilation's users 229 | beyond what the individual works permit. Inclusion of a covered work 230 | in an aggregate does not cause this License to apply to the other 231 | parts of the aggregate. 232 | 233 | 6. Conveying Non-Source Forms. 234 | 235 | You may convey a covered work in object code form under the terms 236 | of sections 4 and 5, provided that you also convey the 237 | machine-readable Corresponding Source under the terms of this License, 238 | in one of these ways: 239 | 240 | a) Convey the object code in, or embodied in, a physical product 241 | (including a physical distribution medium), accompanied by the 242 | Corresponding Source fixed on a durable physical medium 243 | customarily used for software interchange. 244 | 245 | b) Convey the object code in, or embodied in, a physical product 246 | (including a physical distribution medium), accompanied by a 247 | written offer, valid for at least three years and valid for as 248 | long as you offer spare parts or customer support for that product 249 | model, to give anyone who possesses the object code either (1) a 250 | copy of the Corresponding Source for all the software in the 251 | product that is covered by this License, on a durable physical 252 | medium customarily used for software interchange, for a price no 253 | more than your reasonable cost of physically performing this 254 | conveying of source, or (2) access to copy the 255 | Corresponding Source from a network server at no charge. 256 | 257 | c) Convey individual copies of the object code with a copy of the 258 | written offer to provide the Corresponding Source. This 259 | alternative is allowed only occasionally and noncommercially, and 260 | only if you received the object code with such an offer, in accord 261 | with subsection 6b. 262 | 263 | d) Convey the object code by offering access from a designated 264 | place (gratis or for a charge), and offer equivalent access to the 265 | Corresponding Source in the same way through the same place at no 266 | further charge. You need not require recipients to copy the 267 | Corresponding Source along with the object code. If the place to 268 | copy the object code is a network server, the Corresponding Source 269 | may be on a different server (operated by you or a third party) 270 | that supports equivalent copying facilities, provided you maintain 271 | clear directions next to the object code saying where to find the 272 | Corresponding Source. Regardless of what server hosts the 273 | Corresponding Source, you remain obligated to ensure that it is 274 | available for as long as needed to satisfy these requirements. 275 | 276 | e) Convey the object code using peer-to-peer transmission, provided 277 | you inform other peers where the object code and Corresponding 278 | Source of the work are being offered to the general public at no 279 | charge under subsection 6d. 280 | 281 | A separable portion of the object code, whose source code is excluded 282 | from the Corresponding Source as a System Library, need not be 283 | included in conveying the object code work. 284 | 285 | A "User Product" is either (1) a "consumer product", which means any 286 | tangible personal property which is normally used for personal, family, 287 | or household purposes, or (2) anything designed or sold for incorporation 288 | into a dwelling. In determining whether a product is a consumer product, 289 | doubtful cases shall be resolved in favor of coverage. For a particular 290 | product received by a particular user, "normally used" refers to a 291 | typical or common use of that class of product, regardless of the status 292 | of the particular user or of the way in which the particular user 293 | actually uses, or expects or is expected to use, the product. A product 294 | is a consumer product regardless of whether the product has substantial 295 | commercial, industrial or non-consumer uses, unless such uses represent 296 | the only significant mode of use of the product. 297 | 298 | "Installation Information" for a User Product means any methods, 299 | procedures, authorization keys, or other information required to install 300 | and execute modified versions of a covered work in that User Product from 301 | a modified version of its Corresponding Source. The information must 302 | suffice to ensure that the continued functioning of the modified object 303 | code is in no case prevented or interfered with solely because 304 | modification has been made. 305 | 306 | If you convey an object code work under this section in, or with, or 307 | specifically for use in, a User Product, and the conveying occurs as 308 | part of a transaction in which the right of possession and use of the 309 | User Product is transferred to the recipient in perpetuity or for a 310 | fixed term (regardless of how the transaction is characterized), the 311 | Corresponding Source conveyed under this section must be accompanied 312 | by the Installation Information. But this requirement does not apply 313 | if neither you nor any third party retains the ability to install 314 | modified object code on the User Product (for example, the work has 315 | been installed in ROM). 316 | 317 | The requirement to provide Installation Information does not include a 318 | requirement to continue to provide support service, warranty, or updates 319 | for a work that has been modified or installed by the recipient, or for 320 | the User Product in which it has been modified or installed. Access to a 321 | network may be denied when the modification itself materially and 322 | adversely affects the operation of the network or violates the rules and 323 | protocols for communication across the network. 324 | 325 | Corresponding Source conveyed, and Installation Information provided, 326 | in accord with this section must be in a format that is publicly 327 | documented (and with an implementation available to the public in 328 | source code form), and must require no special password or key for 329 | unpacking, reading or copying. 330 | 331 | 7. Additional Terms. 332 | 333 | "Additional permissions" are terms that supplement the terms of this 334 | License by making exceptions from one or more of its conditions. 335 | Additional permissions that are applicable to the entire Program shall 336 | be treated as though they were included in this License, to the extent 337 | that they are valid under applicable law. If additional permissions 338 | apply only to part of the Program, that part may be used separately 339 | under those permissions, but the entire Program remains governed by 340 | this License without regard to the additional permissions. 341 | 342 | When you convey a copy of a covered work, you may at your option 343 | remove any additional permissions from that copy, or from any part of 344 | it. (Additional permissions may be written to require their own 345 | removal in certain cases when you modify the work.) You may place 346 | additional permissions on material, added by you to a covered work, 347 | for which you have or can give appropriate copyright permission. 348 | 349 | Notwithstanding any other provision of this License, for material you 350 | add to a covered work, you may (if authorized by the copyright holders of 351 | that material) supplement the terms of this License with terms: 352 | 353 | a) Disclaiming warranty or limiting liability differently from the 354 | terms of sections 15 and 16 of this License; or 355 | 356 | b) Requiring preservation of specified reasonable legal notices or 357 | author attributions in that material or in the Appropriate Legal 358 | Notices displayed by works containing it; or 359 | 360 | c) Prohibiting misrepresentation of the origin of that material, or 361 | requiring that modified versions of such material be marked in 362 | reasonable ways as different from the original version; or 363 | 364 | d) Limiting the use for publicity purposes of names of licensors or 365 | authors of the material; or 366 | 367 | e) Declining to grant rights under trademark law for use of some 368 | trade names, trademarks, or service marks; or 369 | 370 | f) Requiring indemnification of licensors and authors of that 371 | material by anyone who conveys the material (or modified versions of 372 | it) with contractual assumptions of liability to the recipient, for 373 | any liability that these contractual assumptions directly impose on 374 | those licensors and authors. 375 | 376 | All other non-permissive additional terms are considered "further 377 | restrictions" within the meaning of section 10. If the Program as you 378 | received it, or any part of it, contains a notice stating that it is 379 | governed by this License along with a term that is a further 380 | restriction, you may remove that term. If a license document contains 381 | a further restriction but permits relicensing or conveying under this 382 | License, you may add to a covered work material governed by the terms 383 | of that license document, provided that the further restriction does 384 | not survive such relicensing or conveying. 385 | 386 | If you add terms to a covered work in accord with this section, you 387 | must place, in the relevant source files, a statement of the 388 | additional terms that apply to those files, or a notice indicating 389 | where to find the applicable terms. 390 | 391 | Additional terms, permissive or non-permissive, may be stated in the 392 | form of a separately written license, or stated as exceptions; 393 | the above requirements apply either way. 394 | 395 | 8. Termination. 396 | 397 | You may not propagate or modify a covered work except as expressly 398 | provided under this License. Any attempt otherwise to propagate or 399 | modify it is void, and will automatically terminate your rights under 400 | this License (including any patent licenses granted under the third 401 | paragraph of section 11). 402 | 403 | However, if you cease all violation of this License, then your 404 | license from a particular copyright holder is reinstated (a) 405 | provisionally, unless and until the copyright holder explicitly and 406 | finally terminates your license, and (b) permanently, if the copyright 407 | holder fails to notify you of the violation by some reasonable means 408 | prior to 60 days after the cessation. 409 | 410 | Moreover, your license from a particular copyright holder is 411 | reinstated permanently if the copyright holder notifies you of the 412 | violation by some reasonable means, this is the first time you have 413 | received notice of violation of this License (for any work) from that 414 | copyright holder, and you cure the violation prior to 30 days after 415 | your receipt of the notice. 416 | 417 | Termination of your rights under this section does not terminate the 418 | licenses of parties who have received copies or rights from you under 419 | this License. If your rights have been terminated and not permanently 420 | reinstated, you do not qualify to receive new licenses for the same 421 | material under section 10. 422 | 423 | 9. Acceptance Not Required for Having Copies. 424 | 425 | You are not required to accept this License in order to receive or 426 | run a copy of the Program. Ancillary propagation of a covered work 427 | occurring solely as a consequence of using peer-to-peer transmission 428 | to receive a copy likewise does not require acceptance. However, 429 | nothing other than this License grants you permission to propagate or 430 | modify any covered work. These actions infringe copyright if you do 431 | not accept this License. Therefore, by modifying or propagating a 432 | covered work, you indicate your acceptance of this License to do so. 433 | 434 | 10. Automatic Licensing of Downstream Recipients. 435 | 436 | Each time you convey a covered work, the recipient automatically 437 | receives a license from the original licensors, to run, modify and 438 | propagate that work, subject to this License. You are not responsible 439 | for enforcing compliance by third parties with this License. 440 | 441 | An "entity transaction" is a transaction transferring control of an 442 | organization, or substantially all assets of one, or subdividing an 443 | organization, or merging organizations. If propagation of a covered 444 | work results from an entity transaction, each party to that 445 | transaction who receives a copy of the work also receives whatever 446 | licenses to the work the party's predecessor in interest had or could 447 | give under the previous paragraph, plus a right to possession of the 448 | Corresponding Source of the work from the predecessor in interest, if 449 | the predecessor has it or can get it with reasonable efforts. 450 | 451 | You may not impose any further restrictions on the exercise of the 452 | rights granted or affirmed under this License. For example, you may 453 | not impose a license fee, royalty, or other charge for exercise of 454 | rights granted under this License, and you may not initiate litigation 455 | (including a cross-claim or counterclaim in a lawsuit) alleging that 456 | any patent claim is infringed by making, using, selling, offering for 457 | sale, or importing the Program or any portion of it. 458 | 459 | 11. Patents. 460 | 461 | A "contributor" is a copyright holder who authorizes use under this 462 | License of the Program or a work on which the Program is based. The 463 | work thus licensed is called the contributor's "contributor version". 464 | 465 | A contributor's "essential patent claims" are all patent claims 466 | owned or controlled by the contributor, whether already acquired or 467 | hereafter acquired, that would be infringed by some manner, permitted 468 | by this License, of making, using, or selling its contributor version, 469 | but do not include claims that would be infringed only as a 470 | consequence of further modification of the contributor version. For 471 | purposes of this definition, "control" includes the right to grant 472 | patent sublicenses in a manner consistent with the requirements of 473 | this License. 474 | 475 | Each contributor grants you a non-exclusive, worldwide, royalty-free 476 | patent license under the contributor's essential patent claims, to 477 | make, use, sell, offer for sale, import and otherwise run, modify and 478 | propagate the contents of its contributor version. 479 | 480 | In the following three paragraphs, a "patent license" is any express 481 | agreement or commitment, however denominated, not to enforce a patent 482 | (such as an express permission to practice a patent or covenant not to 483 | sue for patent infringement). To "grant" such a patent license to a 484 | party means to make such an agreement or commitment not to enforce a 485 | patent against the party. 486 | 487 | If you convey a covered work, knowingly relying on a patent license, 488 | and the Corresponding Source of the work is not available for anyone 489 | to copy, free of charge and under the terms of this License, through a 490 | publicly available network server or other readily accessible means, 491 | then you must either (1) cause the Corresponding Source to be so 492 | available, or (2) arrange to deprive yourself of the benefit of the 493 | patent license for this particular work, or (3) arrange, in a manner 494 | consistent with the requirements of this License, to extend the patent 495 | license to downstream recipients. "Knowingly relying" means you have 496 | actual knowledge that, but for the patent license, your conveying the 497 | covered work in a country, or your recipient's use of the covered work 498 | in a country, would infringe one or more identifiable patents in that 499 | country that you have reason to believe are valid. 500 | 501 | If, pursuant to or in connection with a single transaction or 502 | arrangement, you convey, or propagate by procuring conveyance of, a 503 | covered work, and grant a patent license to some of the parties 504 | receiving the covered work authorizing them to use, propagate, modify 505 | or convey a specific copy of the covered work, then the patent license 506 | you grant is automatically extended to all recipients of the covered 507 | work and works based on it. 508 | 509 | A patent license is "discriminatory" if it does not include within 510 | the scope of its coverage, prohibits the exercise of, or is 511 | conditioned on the non-exercise of one or more of the rights that are 512 | specifically granted under this License. You may not convey a covered 513 | work if you are a party to an arrangement with a third party that is 514 | in the business of distributing software, under which you make payment 515 | to the third party based on the extent of your activity of conveying 516 | the work, and under which the third party grants, to any of the 517 | parties who would receive the covered work from you, a discriminatory 518 | patent license (a) in connection with copies of the covered work 519 | conveyed by you (or copies made from those copies), or (b) primarily 520 | for and in connection with specific products or compilations that 521 | contain the covered work, unless you entered into that arrangement, 522 | or that patent license was granted, prior to 28 March 2007. 523 | 524 | Nothing in this License shall be construed as excluding or limiting 525 | any implied license or other defenses to infringement that may 526 | otherwise be available to you under applicable patent law. 527 | 528 | 12. No Surrender of Others' Freedom. 529 | 530 | If conditions are imposed on you (whether by court order, agreement or 531 | otherwise) that contradict the conditions of this License, they do not 532 | excuse you from the conditions of this License. If you cannot convey a 533 | covered work so as to satisfy simultaneously your obligations under this 534 | License and any other pertinent obligations, then as a consequence you may 535 | not convey it at all. For example, if you agree to terms that obligate you 536 | to collect a royalty for further conveying from those to whom you convey 537 | the Program, the only way you could satisfy both those terms and this 538 | License would be to refrain entirely from conveying the Program. 539 | 540 | 13. Remote Network Interaction; Use with the GNU General Public License. 541 | 542 | Notwithstanding any other provision of this License, if you modify the 543 | Program, your modified version must prominently offer all users 544 | interacting with it remotely through a computer network (if your version 545 | supports such interaction) an opportunity to receive the Corresponding 546 | Source of your version by providing access to the Corresponding Source 547 | from a network server at no charge, through some standard or customary 548 | means of facilitating copying of software. This Corresponding Source 549 | shall include the Corresponding Source for any work covered by version 3 550 | of the GNU General Public License that is incorporated pursuant to the 551 | following paragraph. 552 | 553 | Notwithstanding any other provision of this License, you have 554 | permission to link or combine any covered work with a work licensed 555 | under version 3 of the GNU General Public License into a single 556 | combined work, and to convey the resulting work. The terms of this 557 | License will continue to apply to the part which is the covered work, 558 | but the work with which it is combined will remain governed by version 559 | 3 of the GNU General Public License. 560 | 561 | 14. Revised Versions of this License. 562 | 563 | The Free Software Foundation may publish revised and/or new versions of 564 | the GNU Affero General Public License from time to time. Such new versions 565 | will be similar in spirit to the present version, but may differ in detail to 566 | address new problems or concerns. 567 | 568 | Each version is given a distinguishing version number. If the 569 | Program specifies that a certain numbered version of the GNU Affero General 570 | Public License "or any later version" applies to it, you have the 571 | option of following the terms and conditions either of that numbered 572 | version or of any later version published by the Free Software 573 | Foundation. If the Program does not specify a version number of the 574 | GNU Affero General Public License, you may choose any version ever published 575 | by the Free Software Foundation. 576 | 577 | If the Program specifies that a proxy can decide which future 578 | versions of the GNU Affero General Public License can be used, that proxy's 579 | public statement of acceptance of a version permanently authorizes you 580 | to choose that version for the Program. 581 | 582 | Later license versions may give you additional or different 583 | permissions. However, no additional obligations are imposed on any 584 | author or copyright holder as a result of your choosing to follow a 585 | later version. 586 | 587 | 15. Disclaimer of Warranty. 588 | 589 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 590 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 591 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 592 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 593 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 594 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 595 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 596 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 597 | 598 | 16. Limitation of Liability. 599 | 600 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 601 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 602 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 603 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 604 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 605 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 606 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 607 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 608 | SUCH DAMAGES. 609 | 610 | 17. Interpretation of Sections 15 and 16. 611 | 612 | If the disclaimer of warranty and limitation of liability provided 613 | above cannot be given local legal effect according to their terms, 614 | reviewing courts shall apply local law that most closely approximates 615 | an absolute waiver of all civil liability in connection with the 616 | Program, unless a warranty or assumption of liability accompanies a 617 | copy of the Program in return for a fee. 618 | 619 | END OF TERMS AND CONDITIONS 620 | 621 | How to Apply These Terms to Your New Programs 622 | 623 | If you develop a new program, and you want it to be of the greatest 624 | possible use to the public, the best way to achieve this is to make it 625 | free software which everyone can redistribute and change under these terms. 626 | 627 | To do so, attach the following notices to the program. It is safest 628 | to attach them to the start of each source file to most effectively 629 | state the exclusion of warranty; and each file should have at least 630 | the "copyright" line and a pointer to where the full notice is found. 631 | 632 | 633 | Copyright (C) 634 | 635 | This program is free software: you can redistribute it and/or modify 636 | it under the terms of the GNU Affero General Public License as published 637 | by the Free Software Foundation, either version 3 of the License, or 638 | (at your option) any later version. 639 | 640 | This program is distributed in the hope that it will be useful, 641 | but WITHOUT ANY WARRANTY; without even the implied warranty of 642 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 643 | GNU Affero General Public License for more details. 644 | 645 | You should have received a copy of the GNU Affero General Public License 646 | along with this program. If not, see . 647 | 648 | Also add information on how to contact you by electronic and paper mail. 649 | 650 | If your software can interact with users remotely through a computer 651 | network, you should also make sure that it provides a way for users to 652 | get its source. For example, if your program is a web application, its 653 | interface could display a "Source" link that leads users to an archive 654 | of the code. There are many ways you could offer source, and different 655 | solutions will be better for different programs; see section 13 for the 656 | specific requirements. 657 | 658 | You should also get your employer (if you work as a programmer) or school, 659 | if any, to sign a "copyright disclaimer" for the program, if necessary. 660 | For more information on this, and how to apply and follow the GNU AGPL, see 661 | . 662 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | #application 2 | 3 | export POX_PATH := /opt/ik2220/pox 4 | export APP_PATH := ik2220 5 | app: 6 | #mkdir $$POX_PATH/ext/$$APP_PATH 7 | #python -m compileall ./sdn/task1/ 8 | cp sdn/task1/* $$POX_PATH/ext/ 9 | python -m compileall $$POX_PATH/ext/ 10 | sudo python $$POX_PATH/pox.py l2_firewall & 11 | #sudo python $$POX_PATH/pox.py $$APP_PATH.l2_firewall 12 | 13 | clean: 14 | sudo pkill -9 -f pox.py 15 | 16 | #topology 17 | 18 | topo: 19 | sudo python phase1.py 20 | 21 | clean: 22 | sudo mn -c 23 | 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/shrin18/SDN_Project.svg?branch=master)](https://travis-ci.org/shrin18/SDN_Project) 2 | 3 | ## SDN_Project 4 | 5 | topo 6 | 7 |

This is a project based on the above topology which incorporates the use of Load Balanacer, Firewalls, Intrusion detecion system and the NAPT. The topology of this project is divided into three zones namely: Private, Public and DmZ. Stress Tests for the two firewalls are run by sending packets from one zone to the other. There are two sets of servers that are running a simpleHTTP and DNS server as configured in the dns.py scripts. Tests are also run for testing these servers using dig and curl commands respectively.

8 | 9 |

Mininet Tutorial

10 | https://kth.instructure.com/courses/7689/pages/ik2220-tutorial-video-mininet+pox?module_item_id=134779e 11 | 12 |

Visualiser

http://mininet.spear.narmox.com 13 | *Just paste your answer for links and dump in the given link and render graph for the topology for mininet* 14 | 15 |

Repo for VND (Visual Network Descriptor)

https://github.com/ramonfontes/vnd 16 | 17 | 18 |

Commands for Starting topology, firewall, click scripts independently

19 |
    1. Topology
20 |
sudo python filename.py
21 |
    2. Firewall
22 |
./pox.py log.level --DEBUG forwarding.l2_firewall
23 |
    3. Click
24 |
sudo /usr/local/bin/click filename.click
25 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman -------------------------------------------------------------------------------- /click/Makefile: -------------------------------------------------------------------------------- 1 | export POX_PATH := /opt/ik2220/pox 2 | export APP_PATH := ik2220 3 | export PATHRR := $(CURDIR) 4 | app: 5 | sudo cp sdn/* $$POX_PATH/ext/ 6 | sudo cp nfv/* $$POX_PATH/ext/ 7 | sudo python -m compileall $$POX_PATH/ext/ 8 | sudo python $$POX_PATH/pox.py l2_firewall --pathExec=$$PATHRR & 9 | 10 | clean: 11 | -sudo pkill -2 -f click 12 | -sudo pkill -9 -f pox 13 | 14 | -------------------------------------------------------------------------------- /click/ids.click: -------------------------------------------------------------------------------- 1 | //Intrusion detection system 2 | // Benign -> HTTP POST, PUT 3 | // Malicious -> SQL injection (cat /etc/passwd, cat /var/log/, INSERT, UPDATE, DELETE), 4 | // HTTP GET, HEAD, OPTIONS, TRACE, DELETE, CONNECT 5 | /* 6 | +------ v ------+ +---------------+ +---------------+ 7 | | | | | | | 8 | | SW2 |------------> IDS +----Benign--> LB2 | 9 | | | fromSwitch | | | | 10 | +---------------+ +-------+-------+ +---------------+ 11 | | 12 | Malicious 13 | | 14 | +------ v ------+ 15 | | | 16 | | INSP | 17 | | | 18 | +---------------+ 19 | */ 20 | 21 | ctr_pkt_in, ctr_pkt_malicious, ctr_pkt_benign :: AverageCounter; 22 | 23 | fromSwitch :: FromDevice(id8-eth1, METHOD LINUX, SNIFFER false); 24 | fromLoadbal :: FromDevice(id8-eth2, METHOD LINUX, SNIFFER false); 25 | 26 | toSwitch :: Queue -> ToDevice(id8-eth1); // connect sw2 27 | toInsp :: Queue -> ctr_pkt_malicious -> ToDevice(id8-eth3); // connect insp and count number of malicious packets 28 | toLoadbal :: Queue -> ctr_pkt_benign -> ToDevice(id8-eth2); // connect lb2 and count number of benign packets 29 | 30 | //Define patterns to look out for 31 | filter :: Classifier( 32 | 66/474554, // HTTP GET 33 | 66/48454144, // HTTP head 34 | 66/5452414345, // HTTP TRACE 35 | 66/4f5054494f4e53, //HTTP OPTIONS 36 | 66/44454c455445, //HTTP DELETE 37 | 66/434f4e4e454354, //HTTP CONNECT 38 | 209/636174202f6574632f706173737764, // "cat /etc/passwd" 39 | 209/636174202f7661722f6c6f672f, // "cat /var/log/" 40 | 208/494E53455254, //"INSERT" 41 | 208/555044415445, //"UPDATE" 42 | 208/44454C455445, //"DELETE" 43 | -); 44 | 45 | 46 | fromSwitch -> ctr_pkt_in -> filter; // Count incoming pkts before sending to filter 47 | 48 | filter[0],filter[1],filter[2],filter[3],filter[4],filter[5],filter[6],filter[7],filter[8],filter[9],filter[10] -> toInsp; // Send malicious pkts to Inspector 49 | 50 | filter[11] -> toLoadbal; //send all rest of the pkts(-) to loadbalancer2 51 | 52 | fromLoadbal -> toSwitch; // On return path ids should be transparant to all packets. Send all pkts from loadbalancer2 to sw2 53 | 54 | 55 | // report 56 | DriverManager(wait , print > $rPath/../results/ids.counter " #../results/ids.report 57 | =================== IDS Report =================== 58 | Input Packet Rate (pps): $(ctr_pkt_in.rate) 59 | Output Packet Rate(pps): $(ctr_pkt_benign.rate) 60 | Total # of input packets: $(ctr_pkt_in.count) 61 | Total # of output packets: $(ctr_pkt_benign.count) 62 | Total # of dropped packets: $(ctr_pkt_malicious.count) 63 | ================================================== 64 | " , stop); 65 | -------------------------------------------------------------------------------- /click/lb.click: -------------------------------------------------------------------------------- 1 | // define counter 2 | counter_from_DmZ, counter_from_PrZ, counter_to_DmZ, counter_to_PrZ :: AverageCounter; 3 | arp_req1, arp_res1, icmp1, ip1 :: Counter; 4 | arp_req2, arp_res2, icmp2, ip2 :: Counter; 5 | drop1, drop2, drop3, drop4 :: Counter; 6 | 7 | AddressInfo( 8 | DmZ 100.0.0.1 00:00:00:01:00:01, 9 | PrZ 10.0.0.1 00:00:00:00:10:01, 10 | ); 11 | 12 | 13 | // DmZ to PrZ 14 | from_DmZ :: FromDevice(n9-eth1, METHOD LINUX, SNIFFER false); 15 | to_PrZ :: Queue -> counter_to_PrZ -> ToDevice(n9-eth2); 16 | 17 | 18 | // PrZ to DmZ 19 | from_PrZ :: FromDevice(n9-eth2, METHOD LINUX, SNIFFER false); 20 | to_DmZ :: Queue -> counter_to_DmZ -> ToDevice(n9-eth1); 21 | 22 | 23 | // Packet classifier 24 | PrZ_classifier, DmZ_classifier :: Classifier( 25 | 12/0806 20/0001, //[0]ARP request 26 | 12/0806 20/0002, //[1]ARP reply 27 | 12/0800, //[2]IP 28 | -); //[3]rest 29 | 30 | 31 | // ARP querier 32 | DmZ_arpq :: ARPQuerier(DmZ); 33 | PrZ_arpq :: ARPQuerier(PrZ); 34 | 35 | 36 | // IP packet 37 | ip_to_DmZ :: GetIPAddress(16) -> CheckIPHeader -> [0]DmZ_arpq -> IPPrint("Ip packet to DmZ") -> to_DmZ; 38 | ip_to_PrZ :: GetIPAddress(16) -> CheckIPHeader -> [0]PrZ_arpq -> IPPrint("Ip packet to PrZ") -> to_PrZ; 39 | 40 | 41 | // TCP & UDP rewrite 42 | ip_rewrite :: IPRewriter(pattern 100.0.0.1 20000-65535 - - 0 1,drop); 43 | ip_rewrite[0] -> ip_to_DmZ; 44 | ip_rewrite[1] -> ip_to_PrZ; 45 | 46 | 47 | // ICMP echo rewrite 48 | icmp_rewrite :: ICMPPingRewriter(pattern 100.0.0.1 20000-65535 - - 0 1,drop); 49 | icmp_rewrite[0] -> ip_to_DmZ; 50 | icmp_rewrite[1] -> ip_to_PrZ; 51 | 52 | 53 | // packet from DmZ 54 | from_DmZ -> counter_from_DmZ -> DmZ_classifier; 55 | DmZ_classifier[0] -> arp_req1 -> ARPResponder(DmZ) -> to_DmZ; //ARP request 56 | DmZ_classifier[1] -> arp_res1 -> [1]DmZ_arpq; //ARP reply 57 | DmZ_classifier[2] -> ip1 -> Strip(14) -> CheckIPHeader -> IPPrint("IP packet from DmZ") -> DmZ_IP_classifier :: IPClassifier(icmp type echo-reply, udp or tcp, -); //IP packet 58 | DmZ_IP_classifier[0] -> icmp2 -> [1]icmp_rewrite; 59 | DmZ_IP_classifier[1] -> [1]ip_rewrite; 60 | DmZ_IP_classifier[2] -> drop3 -> Discard; 61 | DmZ_classifier[3] -> drop1 -> Discard; //Drop rest packet 62 | 63 | 64 | // packet from PrZ 65 | from_PrZ -> counter_from_PrZ -> PrZ_classifier; 66 | PrZ_classifier[0] -> arp_req2 -> ARPResponder(PrZ)-> to_PrZ; //ARP request 67 | PrZ_classifier[1] -> arp_res2 -> [1]PrZ_arpq; //ARP reply 68 | PrZ_classifier[2] -> ip2 -> Strip(14)-> CheckIPHeader -> IPPrint("IP packet from PrZ") -> PrZ_IP_classifier :: IPClassifier(icmp type echo, udp or tcp, -); //IP packet 69 | PrZ_IP_classifier[0] -> icmp1 -> [0]icmp_rewrite; 70 | PrZ_IP_classifier[1] -> [0]ip_rewrite; 71 | PrZ_IP_classifier[2] -> drop4 -> Discard; 72 | PrZ_classifier[3] -> drop2 -> Discard; //Drop rest packet 73 | 74 | 75 | // report 76 | DriverManager(wait , print > $rPath/../results/napt.counter " 77 | =================== NAPT Report =================== 78 | Input Packet Rate (pps): $(add $(counter_from_DmZ.rate) $(counter_from_PrZ.rate)) 79 | Output Packet Rate(pps): $(add $(counter_to_DmZ.rate) $(counter_to_PrZ.rate)) 80 | 81 | Total # of input packets: $(add $(counter_from_DmZ.count) $(counter_from_PrZ.count)) 82 | Total # of output packets: $(add $(counter_to_DmZ.count) $(counter_to_PrZ.count)) 83 | 84 | Total # of ARP requests packets: $(add $(arp_req1.count) $(arp_req2.count)) 85 | Total # of ARP respondes packets: $(add $(arp_res1.count) $(arp_res2.count)) 86 | 87 | Total # of service requests packets: $(add $(ip1.count) $(ip2.count)) 88 | Total # of ICMP packets: $(add $(icmp1.count) $(icmp2.count)) 89 | Total # of dropped packets: $(add $(drop1.count) $(drop2.count) $(drop3.count) $(drop4.count)) 90 | ================================================== 91 | " , stop); 92 | 93 | 94 | shrinish@click:/home/tejankar/ik2220-assign-phase2-team5/application/nfv$ 95 | shrinish@click:/home/tejankar/ik2220-assign-phase2-team5/application/nfv$ 96 | shrinish@click:/home/tejankar/ik2220-assign-phase2-team5/application/nfv$ 97 | shrinish@click:/home/tejankar/ik2220-assign-phase2-team5/application/nfv$ 98 | shrinish@click:/home/tejankar/ik2220-assign-phase2-team5/application/nfv$ 99 | shrinish@click:/home/tejankar/ik2220-assign-phase2-team5/application/nfv$ cat lb_update.click 100 | //LOAD BALANCER 101 | 102 | //define average counters 103 | IF1_in, IF1_out, IF2_in, IF2_out :: AverageCounter; 104 | 105 | //define counters for different packets 106 | arp_req1, arp_rep1, ip1 :: Counter; 107 | arp_req2, arp_rep2, ip2 :: Counter; 108 | icmp, drop_IF1, drop_IF2, drop_IP :: Counter; 109 | 110 | // Traffic from server to client 111 | init_serv :: FromDevice($LB-$port1, METHOD LINUX, SNIFFER false); 112 | end_cli :: Queue -> IF1_out -> ToDevice($LB-$port2, METHOD LINUX); 113 | 114 | // Traffic from client to server 115 | init_cli :: FromDevice($LB-$port2, METHOD LINUX, SNIFFER false); 116 | end_serv :: Queue -> IF2_out -> ToDevice($LB-$port1, METHOD LINUX); 117 | 118 | 119 | // Packet classification 120 | cli_pkt, serv_pkt :: Classifier( 121 | 12/0806 20/0001, //[0]ARP request 122 | 12/0806 20/0002, //[1]ARP reply 123 | 12/0800, //[2]IP 124 | -); //[3]others 125 | 126 | 127 | // ARP query definition 128 | serv_arpq :: ARPQuerier($VIP/32, $LB-$port1); 129 | cli_arpq :: ARPQuerier($VIP/32, $LB-$port2); 130 | 131 | 132 | // IP packet 133 | // GetIPAddress(16) OFFSET is usually 16, to fetch the destination address from an IP packet. 134 | 135 | ip_to_cli :: GetIPAddress(16) -> CheckIPHeader -> [0]cli_arpq -> IPPrint("IP packet destined to client") -> end_cli; 136 | ip_to_serv :: GetIPAddress(16) -> CheckIPHeader -> [0]serv_arpq -> IPPrint("IP packet destined to server") -> end_serv; 137 | 138 | 139 | // Load balancing through Round Rrobin and IP Rewrite elements 140 | ip_map :: RoundRobinIPMapper( 141 | $VIP - $DIP_1 $prt_n 0 1, 142 | $VIP - $DIP_2 $prt_n 0 1, 143 | $VIP - $DIP_3 $prt_n 0 1); 144 | ip_assign :: IPRewriter(ip_map, pattern $VIP 20000-65535 - - 1 0); 145 | ip_assign[0] -> ip_to_serv; 146 | ip_assign[1] -> ip_to_cli; 147 | 148 | 149 | // packet coming from server 150 | init_serv -> IF2_in -> serv_pkt; 151 | serv_pkt[0] -> arp_req2 -> ARPResponder($VIP $LB-$port1) -> end_serv; 152 | serv_pkt[1] -> arp_rep2 -> [1]serv_arpq; 153 | serv_pkt[2] -> ip2 -> Strip(14) -> CheckIPHeader -> IPPrint("IP packet coming from server") -> [1]ip_assign; //IP packet and Strip(14) to get rid of the Ethernet header 154 | serv_pkt[3] -> drop_IF2 -> Discard; //Drop other packets 155 | 156 | // packet coming from client 157 | init_cli -> IF1_in -> cli_pkt; 158 | cli_pkt[0] -> arp_req1 -> ARPResponder($VIP $LB-$port2) -> end_cli; 159 | cli_pkt[1] -> arp_rep1 -> [1]cli_arpq; 160 | cli_pkt[2] -> ip1 -> Strip(14) -> CheckIPHeader -> IPPrint("IP packet coming from client") -> cli_IP_pkt :: IPClassifier(icmp, dst $prt port $prt_n, -); //IP packet 161 | cli_IP_pkt[0] -> icmp -> icmppr :: ICMPPingResponder() -> ip_to_cli; //ICMP 162 | cli_IP_pkt[1] -> [0]ip_assign; 163 | cli_IP_pkt[2] -> drop_IP -> Discard; //drop other IP packets 164 | cli_pkt[3] -> drop_IF1 -> Discard; //Drop other packet 165 | 166 | 167 | 168 | //Report generation 169 | 170 | DriverManager(wait , print > $rPath/../results/$LB.counter " 171 | =================== LB Report =================================== 172 | Input Packet Rate (pps): $(add $(IF2_in.rate) $(IF1_in.rate)) 173 | Output Packet Rate(pps): $(add $(IF2_out.rate) $(IF1_out.rate)) 174 | Total # of input packets: $(add $(IF2_in.count) $(IF1_in.count)) 175 | Total # of output packets: $(add $(IF2_out.count) $(IF2_out.count)) 176 | Total # of ARP requests packets: $(add $(arp_req1.count) $(arp_req2.count)) 177 | Total # of ARP respondes packets: $(add $(arp_rep1.count) $(arp_rep2.count)) 178 | Total # of service requests packets: $(add $(ip1.count) $(ip2.count)) 179 | Total # of ICMP packets: $(icmp.count) 180 | Total # of dropped packets: $(add $(drop_IF1.count) $(drop_IF2.count) $(drop_IP.count)) 181 | ==================================================================== 182 | " , stop); 183 | -------------------------------------------------------------------------------- /click/lb1.click: -------------------------------------------------------------------------------- 1 | //LOAD BALANCER 2 | 3 | //define average counters 4 | IF1_in, IF1_out, IF2_in, IF2_out :: AverageCounter; 5 | 6 | //define counters for different packets 7 | arp_req1, arp_rep1, ip1 :: Counter; 8 | arp_req2, arp_rep2, ip2 :: Counter; 9 | icmp, drop_IF1, drop_IF2, drop_IP :: Counter; 10 | 11 | // Traffic from server to client 12 | init_serv :: FromDevice(lb7-eth1, METHOD LINUX, SNIFFER false); 13 | end_cli :: Queue -> IF1_out -> ToDevice(lb7-eth2, METHOD LINUX); 14 | 15 | // Traffic from client to server 16 | init_cli :: FromDevice(lb7-eth2, METHOD LINUX, SNIFFER false); 17 | end_serv :: Queue -> IF2_out -> ToDevice(lb7-eth1, METHOD LINUX); 18 | 19 | 20 | // Packet classification 21 | cli_pkt, serv_pkt :: Classifier( 22 | 12/0806 20/0001, //[0]ARP request 23 | 12/0806 20/0002, //[1]ARP reply 24 | 12/0800, //[2]IP 25 | -); //[3]others 26 | 27 | 28 | // ARP query definition 29 | serv_arpq :: ARPQuerier(100.0.0.45, lb7-eth1); 30 | cli_arpq :: ARPQuerier(100.0.0.45, lb7-eth2); 31 | 32 | 33 | // IP packet 34 | // GetIPAddress(16) OFFSET is usually 16, to fetch the destination address from an IP packet. 35 | 36 | ip_to_cli :: GetIPAddress(16) -> CheckIPHeader -> [0]cli_arpq -> IPPrint("IP packet destined to client") -> end_cli; 37 | ip_to_serv :: GetIPAddress(16) -> CheckIPHeader -> [0]serv_arpq -> IPPrint("IP packet destined to server") -> end_serv; 38 | 39 | 40 | // Load balancing through Round Rrobin and IP Rewrite elements 41 | ip_map :: RoundRobinIPMapper( 42 | 100.0.0.45 - 100.0.0.40 80 0 1, 43 | 100.0.0.45 - 100.0.0.41 80 0 1, 44 | 100.0.0.45 - 100.0.0.42 80 0 1); 45 | ip_assign :: IPRewriter(ip_map, pattern 100.0.0.45 20000-65535 - - 1 0); 46 | ip_assign[0] -> ip_to_serv; 47 | ip_assign[1] -> ip_to_cli; 48 | 49 | 50 | // packet coming from server 51 | init_serv -> IF2_in -> serv_pkt; 52 | serv_pkt[0] -> arp_req2 -> ARPResponder(100.0.0.45 lb7-eth1) -> end_serv; 53 | serv_pkt[1] -> arp_rep2 -> [1]serv_arpq; 54 | serv_pkt[2] -> ip2 -> Strip(14) -> CheckIPHeader -> IPPrint("IP packet coming from server") -> [1]ip_assign; //IP packet and Strip(14) to get rid of the Ethernet header 55 | serv_pkt[3] -> drop_IF2 -> Discard; //Drop other packets 56 | 57 | // packet coming from client 58 | init_cli -> IF1_in -> cli_pkt; 59 | cli_pkt[0] -> arp_req1 -> ARPResponder(100.0.0.45 lb7-eth2) -> end_cli; 60 | cli_pkt[1] -> arp_rep1 -> [1]cli_arpq; 61 | cli_pkt[2] -> ip1 -> Strip(14) -> CheckIPHeader -> IPPrint("IP packet coming from client") -> cli_IP_pkt :: IPClassifier(icmp, dst tcp port 80, -); //IP packet 62 | cli_IP_pkt[0] -> icmp -> icmppr :: ICMPPingResponder() -> ip_to_cli; //ICMP 63 | cli_IP_pkt[1] -> [0]ip_assign; //UDP 64 | cli_IP_pkt[2] -> drop_IP -> Discard; //drop other IP packets 65 | cli_pkt[3] -> drop_IF1 -> Discard; //Drop other packet 66 | 67 | 68 | 69 | //Report generation 70 | 71 | DriverManager(wait , print > lb2.report " 72 | =================== LB2 Report =================================== 73 | Input Packet Rate (pps): $(add $(IF2_in.rate) $(IF1_in.rate)) 74 | Output Packet Rate(pps): $(add $(IF2_out.rate) $(IF1_out.rate)) 75 | Total # of input packets: $(add $(IF2_in.count) $(IF1_in.count)) 76 | Total # of output packets: $(add $(IF2_out.count) $(IF2_out.count)) 77 | Total # of ARP requests packets: $(add $(arp_req1.count) $(arp_req2.count)) 78 | Total # of ARP respondes packets: $(add $(arp_rep1.count) $(arp_rep2.count)) 79 | Total # of service requests packets: $(add $(ip1.count) $(ip2.count)) 80 | Total # of ICMP packets: $(icmp.count) 81 | Total # of dropped packets: $(add $(drop_IF1.count) $(drop_IF2.count) $(drop_IP.count)) 82 | ==================================================================== 83 | " , stop); 84 | 85 | shrinish@click:/opt/ik2220/click$ 86 | shrinish@click:/opt/ik2220/click$ 87 | shrinish@click:/opt/ik2220/click$ 88 | shrinish@click:/opt/ik2220/click$ 89 | shrinish@click:/opt/ik2220/click$ cat LB1.click 90 | IF1_in, IF1_out, IF2_in, IF2_out :: AverageCounter; 91 | 92 | //define counters for different packets 93 | arp_req1, arp_rep1, ip1 :: Counter; 94 | arp_req2, arp_rep2, ip2 :: Counter; 95 | icmp, drop_IF1, drop_IF2, drop_IP :: Counter; 96 | 97 | // Traffic from server to client 98 | init_serv :: FromDevice(lb6-eth2, METHOD LINUX, SNIFFER false); 99 | end_cli :: Queue -> IF2_out -> ToDevice(lb6-eth1, METHOD LINUX); 100 | 101 | // Traffic from client to server 102 | init_cli :: FromDevice(lb6-eth1, METHOD LINUX, SNIFFER false); 103 | end_serv :: Queue -> IF1_out -> ToDevice(lb6-eth2, METHOD LINUX); 104 | 105 | 106 | // Packet classification 107 | cli_pkt, serv_pkt :: Classifier( 108 | 12/0806 20/0001, //[0]ARP request 109 | 12/0806 20/0002, //[1]ARP reply 110 | 12/0800, //[2]IP 111 | -); //[3]others 112 | 113 | 114 | // ARP query definition 115 | serv_arpq :: ARPQuerier(100.0.0.25, lb6-eth2); 116 | cli_arpq :: ARPQuerier(100.0.0.25, lb6-eth1); 117 | 118 | 119 | // IP packet 120 | // GetIPAddress(16) OFFSET is usually 16, to fetch the destination address from an IP packet. 121 | 122 | ip_to_cli :: GetIPAddress(16) -> CheckIPHeader -> [0]cli_arpq -> IPPrint("IP packet destined to client") -> end_cli; 123 | ip_to_serv :: GetIPAddress(16) -> CheckIPHeader -> [0]serv_arpq -> IPPrint("IP packet destined to server") -> end_serv; 124 | 125 | 126 | // Load balancing through Round Rrobin and IP Rewrite elements 127 | ip_map :: RoundRobinIPMapper( 128 | 100.0.0.25 - 100.0.0.20 53 0 1, 129 | 100.0.0.25 - 100.0.0.21 53 0 1, 130 | 100.0.0.25 - 100.0.0.22 53 0 1); 131 | ip_assign :: IPRewriter(ip_map, pattern 100.0.0.25 20000-65535 - - 1 0); 132 | ip_assign[0] -> ip_to_serv; 133 | ip_assign[1] -> ip_to_cli; 134 | 135 | 136 | // packet coming from server 137 | init_serv -> IF1_in -> serv_pkt; 138 | serv_pkt[0] -> arp_req2 -> ARPResponder(100.0.0.25 lb6-eth2) -> end_serv; 139 | serv_pkt[1] -> arp_rep2 -> [1]serv_arpq; 140 | serv_pkt[2] -> ip2 -> Strip(14) -> CheckIPHeader -> IPPrint("IP packet coming from server") -> [1]ip_assign; //IP packet and Strip(14) to get rid of the Ethernet header 141 | serv_pkt[3] -> drop_IF1 -> Discard; //Drop other packets 142 | 143 | // packet coming from client 144 | init_cli -> IF2_in -> cli_pkt; 145 | cli_pkt[0] -> arp_req1 -> ARPResponder(100.0.0.25 lb6-eth1) -> end_cli; 146 | cli_pkt[1] -> arp_rep1 -> [1]cli_arpq; 147 | cli_pkt[2] -> ip1 -> Strip(14) -> CheckIPHeader -> IPPrint("IP packet coming from client") -> cli_IP_pkt :: IPClassifier(icmp, dst udp port 53, -); //IP packet 148 | cli_IP_pkt[0] -> icmp -> icmppr :: ICMPPingResponder() -> ip_to_cli; //ICMP 149 | cli_IP_pkt[1] -> [0]ip_assign; //UDP 150 | cli_IP_pkt[2] -> drop_IP -> Discard; //drop other IP packets 151 | cli_pkt[3] -> drop_IF2 -> Discard; //Drop other packet 152 | 153 | 154 | 155 | //Report generation 156 | 157 | DriverManager(wait , print > ../results/lb1.report " 158 | =================== LB1 Report =================================== 159 | Input Packet Rate (pps): $(add $(IF2_in.rate) $(IF1_in.rate)) 160 | Output Packet Rate(pps): $(add $(IF2_out.rate) $(IF1_out.rate)) 161 | 162 | Total # of input packets: $(add $(IF2_in.count) $(IF1_in.count)) 163 | Total # of output packets: $(add $(IF2_out.count) $(IF2_out.count)) 164 | 165 | Total # of ARP requests packets: $(add $(arp_req1.count) $(arp_req2.count)) 166 | Total # of ARP respondes packets: $(add $(arp_rep1.count) $(arp_rep2.count)) 167 | 168 | Total # of service requests packets: $(add $(ip1.count) $(ip2.count)) 169 | Total # of ICMP packets: $(icmp.count) 170 | Total # of dropped packets: $(add $(drop_IF1.count) $(drop_IF2.count) $(drop_IP.count)) 171 | ==================================================================== 172 | " , stop); 173 | 174 | -------------------------------------------------------------------------------- /click/lb2.click: -------------------------------------------------------------------------------- 1 | //LOAD BALANCER 2 | 3 | //define average counters 4 | IF1_in, IF1_out, IF2_in, IF2_out :: AverageCounter; 5 | 6 | //define counters for different packets 7 | arp_req1, arp_rep1, ip1 :: Counter; 8 | arp_req2, arp_rep2, ip2 :: Counter; 9 | icmp, drop_IF1, drop_IF2, drop_IP :: Counter; 10 | 11 | // Traffic from server to client 12 | init_serv :: FromDevice(lb7-eth1, METHOD LINUX, SNIFFER false); 13 | end_cli :: Queue -> IF1_out -> ToDevice(lb7-eth2, METHOD LINUX); 14 | 15 | // Traffic from client to server 16 | init_cli :: FromDevice(lb7-eth2, METHOD LINUX, SNIFFER false); 17 | end_serv :: Queue -> IF2_out -> ToDevice(lb7-eth1, METHOD LINUX); 18 | 19 | 20 | // Packet classification 21 | cli_pkt, serv_pkt :: Classifier( 22 | 12/0806 20/0001, //[0]ARP request 23 | 12/0806 20/0002, //[1]ARP reply 24 | 12/0800, //[2]IP 25 | -); //[3]others 26 | 27 | 28 | // ARP query definition 29 | serv_arpq :: ARPQuerier(100.0.0.45, lb7-eth1); 30 | cli_arpq :: ARPQuerier(100.0.0.45, lb7-eth2); 31 | 32 | 33 | // IP packet 34 | // GetIPAddress(16) OFFSET is usually 16, to fetch the destination address from an IP packet. 35 | 36 | ip_to_cli :: GetIPAddress(16) -> CheckIPHeader -> [0]cli_arpq -> IPPrint("IP packet destined to client") -> end_cli; 37 | ip_to_serv :: GetIPAddress(16) -> CheckIPHeader -> [0]serv_arpq -> IPPrint("IP packet destined to server") -> end_serv; 38 | 39 | 40 | // Load balancing through Round Rrobin and IP Rewrite elements 41 | ip_map :: RoundRobinIPMapper( 42 | 100.0.0.45 - 100.0.0.40 80 0 1, 43 | 100.0.0.45 - 100.0.0.41 80 0 1, 44 | 100.0.0.45 - 100.0.0.42 80 0 1); 45 | ip_assign :: IPRewriter(ip_map, pattern 100.0.0.45 20000-65535 - - 1 0); 46 | ip_assign[0] -> ip_to_serv; 47 | ip_assign[1] -> ip_to_cli; 48 | 49 | 50 | // packet coming from server 51 | init_serv -> IF2_in -> serv_pkt; 52 | serv_pkt[0] -> arp_req2 -> ARPResponder(100.0.0.45 lb7-eth1) -> end_serv; 53 | serv_pkt[1] -> arp_rep2 -> [1]serv_arpq; 54 | serv_pkt[2] -> ip2 -> Strip(14) -> CheckIPHeader -> IPPrint("IP packet coming from server") -> [1]ip_assign; //IP packet and Strip(14) to get rid of the Ethernet header 55 | serv_pkt[3] -> drop_IF2 -> Discard; //Drop other packets 56 | 57 | // packet coming from client 58 | init_cli -> IF1_in -> cli_pkt; 59 | cli_pkt[0] -> arp_req1 -> ARPResponder(100.0.0.45 lb7-eth2) -> end_cli; 60 | cli_pkt[1] -> arp_rep1 -> [1]cli_arpq; 61 | cli_pkt[2] -> ip1 -> Strip(14) -> CheckIPHeader -> IPPrint("IP packet coming from client") -> cli_IP_pkt :: IPClassifier(icmp, dst tcp port 80, -); //IP packet 62 | cli_IP_pkt[0] -> icmp -> icmppr :: ICMPPingResponder() -> ip_to_cli; //ICMP 63 | cli_IP_pkt[1] -> [0]ip_assign; //UDP 64 | cli_IP_pkt[2] -> drop_IP -> Discard; //drop other IP packets 65 | cli_pkt[3] -> drop_IF1 -> Discard; //Drop other packet 66 | 67 | 68 | 69 | //Report generation 70 | 71 | DriverManager(wait , print > lb2.report " 72 | =================== LB2 Report =================================== 73 | Input Packet Rate (pps): $(add $(IF2_in.rate) $(IF1_in.rate)) 74 | Output Packet Rate(pps): $(add $(IF2_out.rate) $(IF1_out.rate)) 75 | Total # of input packets: $(add $(IF2_in.count) $(IF1_in.count)) 76 | Total # of output packets: $(add $(IF2_out.count) $(IF2_out.count)) 77 | Total # of ARP requests packets: $(add $(arp_req1.count) $(arp_req2.count)) 78 | Total # of ARP respondes packets: $(add $(arp_rep1.count) $(arp_rep2.count)) 79 | Total # of service requests packets: $(add $(ip1.count) $(ip2.count)) 80 | Total # of ICMP packets: $(icmp.count) 81 | Total # of dropped packets: $(add $(drop_IF1.count) $(drop_IF2.count) $(drop_IP.count)) 82 | ==================================================================== 83 | " , stop); 84 | 85 | -------------------------------------------------------------------------------- /click/n9.click: -------------------------------------------------------------------------------- 1 | // define counter 2 | counter_from_DmZ, counter_from_PrZ, counter_to_DmZ, counter_to_PrZ :: AverageCounter; 3 | arp_req1, arp_res1, icmp1, ip1 :: Counter; 4 | arp_req2, arp_res2, icmp2, ip2 :: Counter; 5 | drop1, drop2, drop3, drop4 :: Counter; 6 | 7 | AddressInfo( 8 | DmZ 100.0.0.1 00:00:00:01:00:01, 9 | PrZ 10.0.0.1 00:00:00:00:10:01, 10 | ); 11 | 12 | 13 | // DmZ to PrZ 14 | from_DmZ :: FromDevice(n9-eth1, METHOD LINUX, SNIFFER false); 15 | to_PrZ :: Queue -> counter_to_PrZ -> ToDevice(n9-eth2); 16 | 17 | 18 | // PrZ to DmZ 19 | from_PrZ :: FromDevice(n9-eth2, METHOD LINUX, SNIFFER false); 20 | to_DmZ :: Queue -> counter_to_DmZ -> ToDevice(n9-eth1); 21 | 22 | 23 | // Packet classifier 24 | PrZ_classifier, DmZ_classifier :: Classifier( 25 | 12/0806 20/0001, //[0]ARP request 26 | 12/0806 20/0002, //[1]ARP reply 27 | 12/0800, //[2]IP 28 | -); //[3]rest 29 | 30 | 31 | // ARP querier 32 | DmZ_arpq :: ARPQuerier(DmZ); 33 | PrZ_arpq :: ARPQuerier(PrZ); 34 | 35 | 36 | // IP packet 37 | ip_to_DmZ :: GetIPAddress(16) -> CheckIPHeader -> [0]DmZ_arpq -> IPPrint("Ip packet to DmZ") -> to_DmZ; 38 | ip_to_PrZ :: GetIPAddress(16) -> CheckIPHeader -> [0]PrZ_arpq -> IPPrint("Ip packet to PrZ") -> to_PrZ; 39 | 40 | 41 | // TCP & UDP rewrite 42 | ip_rewrite :: IPRewriter(pattern 100.0.0.1 20000-65535 - - 0 1,drop); 43 | ip_rewrite[0] -> ip_to_DmZ; 44 | ip_rewrite[1] -> ip_to_PrZ; 45 | 46 | 47 | // ICMP echo rewrite 48 | icmp_rewrite :: ICMPPingRewriter(pattern 100.0.0.1 20000-65535 - - 0 1,drop); 49 | icmp_rewrite[0] -> ip_to_DmZ; 50 | icmp_rewrite[1] -> ip_to_PrZ; 51 | 52 | 53 | // packet from DmZ 54 | from_DmZ -> counter_from_DmZ -> DmZ_classifier; 55 | DmZ_classifier[0] -> arp_req1 -> ARPResponder(DmZ) -> to_DmZ; //ARP request 56 | DmZ_classifier[1] -> arp_res1 -> [1]DmZ_arpq; //ARP reply 57 | DmZ_classifier[2] -> ip1 -> Strip(14) -> CheckIPHeader -> IPPrint("IP packet from DmZ") -> DmZ_IP_classifier :: IPClassifier(icmp type echo-reply, udp or tcp, -); //IP packet 58 | DmZ_IP_classifier[0] -> icmp2 -> [1]icmp_rewrite; 59 | DmZ_IP_classifier[1] -> [1]ip_rewrite; 60 | DmZ_IP_classifier[2] -> drop3 -> Discard; 61 | DmZ_classifier[3] -> drop1 -> Discard; //Drop rest packet 62 | 63 | 64 | // packet from PrZ 65 | from_PrZ -> counter_from_PrZ -> PrZ_classifier; 66 | PrZ_classifier[0] -> arp_req2 -> ARPResponder(PrZ)-> to_PrZ; //ARP request 67 | PrZ_classifier[1] -> arp_res2 -> [1]PrZ_arpq; //ARP reply 68 | PrZ_classifier[2] -> ip2 -> Strip(14)-> CheckIPHeader -> IPPrint("IP packet from PrZ") -> PrZ_IP_classifier :: IPClassifier(icmp type echo, udp or tcp, -); //IP packet 69 | PrZ_IP_classifier[0] -> icmp1 -> [0]icmp_rewrite; 70 | PrZ_IP_classifier[1] -> [0]ip_rewrite; 71 | PrZ_IP_classifier[2] -> drop4 -> Discard; 72 | PrZ_classifier[3] -> drop2 -> Discard; //Drop rest packet 73 | 74 | 75 | // report 76 | DriverManager(wait , print > n9.report " 77 | =================== NAPT Report =================== 78 | Input Packet Rate (pps): $(add $(counter_from_DmZ.rate) $(counter_from_PrZ.rate)) 79 | Output Packet Rate(pps): $(add $(counter_to_DmZ.rate) $(counter_to_PrZ.rate)) 80 | 81 | Total # of input packets: $(add $(counter_from_DmZ.count) $(counter_from_PrZ.count)) 82 | Total # of output packets: $(add $(counter_to_DmZ.count) $(counter_to_PrZ.count)) 83 | 84 | Total # of ARP requests packets: $(add $(arp_req1.count) $(arp_req2.count)) 85 | Total # of ARP respondes packets: $(add $(arp_res1.count) $(arp_res2.count)) 86 | 87 | Total # of service requests packets: $(add $(ip1.count) $(ip2.count)) 88 | Total # of ICMP packets: $(add $(icmp1.count) $(icmp2.count)) 89 | Total # of dropped packets: $(add $(drop1.count) $(drop2.count) $(drop3.count) $(drop4.count)) 90 | ================================================== 91 | " , stop); 92 | 93 | -------------------------------------------------------------------------------- /firewall/Makefile: -------------------------------------------------------------------------------- 1 | export POX_PATH := /opt/ik2220/pox 2 | export APP_PATH := ik2220 3 | export PATHRR := $(CURDIR) 4 | app: 5 | sudo cp sdn/* $$POX_PATH/ext/ 6 | sudo cp nfv/* $$POX_PATH/ext/ 7 | sudo python -m compileall $$POX_PATH/ext/ 8 | sudo python $$POX_PATH/pox.py l2_firewall --pathExec=$$PATHRR & 9 | 10 | clean: 11 | -sudo pkill -2 -f click 12 | -sudo pkill -9 -f pox 13 | -------------------------------------------------------------------------------- /firewall/firewall_parent_proactive.py: -------------------------------------------------------------------------------- 1 | # Copyright 2011-2012 James McCauley 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at: 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | """ 16 | An L2 learning switch. 17 | It is derived from one written live for an SDN crash course. 18 | It is somwhat similar to NOX's pyswitch in that it installs 19 | exact-match rules for each flow. 20 | """ 21 | 22 | from pox.core import core 23 | import pox.openflow.libopenflow_01 as of 24 | from pox.lib.util import dpid_to_str 25 | from pox.lib.util import str_to_bool 26 | from pox.lib.addresses import IPAddr, EthAddr 27 | import time 28 | import pox.lib.packet as pkt 29 | from pox.lib.addresses import EthAddr 30 | 31 | log = core.getLogger() 32 | 33 | # We don't want to flood immediately when a switch connects. 34 | # Can be overriden on commandline. 35 | _flood_delay = 0 36 | 37 | class FirewallSwitch (object): 38 | def __init__ (self, connection, transparent): 39 | # Switch we'll be adding L2 learning switch capabilities to 40 | self.connection = connection 41 | self.transparent = transparent 42 | 43 | # Our table 44 | self.macToPort = {} 45 | 46 | # Our firewall table 47 | self.firewall = {} 48 | 49 | # Add a Couple of Rules 50 | #self.AddRule('00-00-00-00-00-01',EthAddr('00:00:00:00:00:01')) 51 | #self.AddRule('00-00-00-00-00-01',EthAddr('00:00:00:00:00:02')) 52 | self.BasicRule(connection) 53 | connection.addListeners(self) 54 | 55 | # We just use this to know when to log a helpful message 56 | self.hold_down_expired = _flood_delay == 0 57 | 58 | #log.debug("Initializing LearningSwitch, transparent=%s", 59 | # str(self.transparent)) 60 | 61 | 62 | # function that allows adding firewall rules into the firewall table 63 | def AddRule (self, dpidstr, src=0,value=True): 64 | self.firewall[(dpidstr,src)]=value 65 | log.debug("Adding firewall rule in %s: %s", dpidstr, src) 66 | 67 | @classmethod 68 | def getConnection(self): 69 | return self.connection 70 | def BasicRule (self, connection): 71 | fm = of.ofp_flow_mod() 72 | fm.match.in_port = 1 73 | fm.priority = 18001 74 | fm.match.dl_type = 0x0806 75 | fm.actions.append(of.ofp_action_output( port = 2 ) ) 76 | connection.send( fm ) 77 | # Allow arp based on dl_type for in_port 2 to output 1 78 | fm = of.ofp_flow_mod() 79 | fm.match.in_port = 2 80 | fm.priority = 18001 81 | fm.match.dl_type = 0x0806 82 | fm.actions.append(of.ofp_action_output( port = 1 ) ) 83 | connection.send( fm ) 84 | # Default drop 85 | fm = of.ofp_flow_mod() 86 | fm.priority = 1001 87 | connection.send( fm ) 88 | 89 | # function that allows deleting firewall rules from the firewall table 90 | # Not used DeleteRule 91 | def DeleteRule (self, dpidstr, src=0): 92 | try: 93 | del self.firewall[(dpidstr,src)] 94 | log.debug("Deleting firewall rule in %s: %s", 95 | dpidstr, src) 96 | except KeyError: 97 | log.error("Cannot find in %s: %s", 98 | dpidstr, src) 99 | 100 | 101 | # check if packet is compliant to rules before proceeding 102 | def CheckRule (self, dpidstr, src=0): 103 | try: 104 | entry = self.firewall[(dpidstr, src)] 105 | if (entry == True): 106 | log.debug("Rule (%s) found in %s: FORWARD", 107 | src, dpidstr) 108 | else: 109 | log.debug("Rule (%s) found in %s: DROP", 110 | src, dpidstr) 111 | return entry 112 | except KeyError: 113 | log.debug("Rule (%s) NOT found in %s: DROP", 114 | src, dpidstr) 115 | return False 116 | 117 | def _handle_PacketIn (self, event): 118 | """ 119 | Handle packet in messages from the switch to implement above algorithm. 120 | """ 121 | 122 | packet = event.parsed 123 | 124 | def flood (message = None): 125 | """ Floods the packet """ 126 | msg = of.ofp_packet_out() 127 | if time.time() - self.connection.connect_time >= _flood_delay: 128 | # Only flood if we've been connected for a little while... 129 | 130 | if self.hold_down_expired is False: 131 | # Oh yes it is! 132 | self.hold_down_expired = True 133 | log.info("%s: Flood hold-down expired -- flooding", 134 | dpid_to_str(event.dpid)) 135 | 136 | if message is not None: log.debug(message) 137 | #log.debug("%i: flood %s -> %s", event.dpid,packet.src,packet.dst) 138 | # OFPP_FLOOD is optional; on some switches you may need to change 139 | # this to OFPP_ALL. 140 | msg.actions.append(of.ofp_action_output(port = of.OFPP_FLOOD)) 141 | else: 142 | pass 143 | #log.info("Holding down flood for %s", dpid_to_str(event.dpid)) 144 | msg.data = event.ofp 145 | msg.in_port = event.port 146 | self.connection.send(msg) 147 | 148 | def drop (duration = None): 149 | """ 150 | Drops this packet and optionally installs a flow to continue 151 | dropping similar ones for a while 152 | """ 153 | if duration is not None: 154 | if not isinstance(duration, tuple): 155 | duration = (duration,duration) 156 | msg = of.ofp_flow_mod() 157 | msg.match = of.ofp_match.from_packet(packet) 158 | msg.idle_timeout = duration[0] 159 | msg.hard_timeout = duration[1] 160 | msg.buffer_id = event.ofp.buffer_id 161 | self.connection.send(msg) 162 | elif event.ofp.buffer_id is not None: 163 | msg = of.ofp_packet_out() 164 | msg.buffer_id = event.ofp.buffer_id 165 | msg.in_port = event.port 166 | self.connection.send(msg) 167 | 168 | self.macToPort[packet.src] = event.port # 1 169 | 170 | # Get the DPID of the Switch Connection 171 | dpidstr = dpid_to_str(event.connection.dpid) 172 | 173 | packet = event.parsed 174 | if packet.type == packet.ARP_TYPE: 175 | log.info("Caught Arp packet") 176 | 177 | # Check the Firewall Rules 178 | if self.CheckRule(dpidstr, packet.src) == False: 179 | drop() 180 | return 181 | 182 | if not self.transparent: # 2 183 | if packet.type == packet.LLDP_TYPE or packet.dst.isBridgeFiltered(): 184 | drop() # 2a 185 | return 186 | 187 | if packet.dst.is_multicast: 188 | flood() # 3a 189 | else: 190 | if packet.dst not in self.macToPort: # 4 191 | flood("Port for %s unknown -- flooding" % (packet.dst,)) # 4a 192 | else: 193 | port = self.macToPort[packet.dst] 194 | if port == event.port: # 5 195 | # 5a 196 | log.warning("Same port for packet from %s -> %s on %s.%s. Drop." 197 | % (packet.src, packet.dst, dpid_to_str(event.dpid), port)) 198 | drop(10) 199 | return 200 | # 6 201 | log.debug("installing flow for %s.%i -> %s.%i" % 202 | (packet.src, event.port, packet.dst, port)) 203 | msg = of.ofp_flow_mod() 204 | msg.match = of.ofp_match.from_packet(packet, event.port) 205 | msg.idle_timeout = 10 206 | msg.hard_timeout = 30 207 | msg.actions.append(of.ofp_action_output(port = port)) 208 | msg.data = event.ofp # 6a 209 | self.connection.send(msg) 210 | 211 | 212 | class firewall (object): 213 | """ 214 | Waits for OpenFlow switches to connect and makes them learning switches. 215 | """ 216 | def __init__ (self, transparent): 217 | core.openflow.addListeners(self) 218 | self.transparent = transparent 219 | 220 | def _handle_ConnectionUp (self, event): 221 | log.debug("Connection %s" % (event.connection,)) 222 | #FirewallSwitch(event.connection, self.transparent) 223 | log.debug("###################DPID#############:%s ", dpid_to_str(event.connection.dpid)) 224 | # Allow arp based on dl_type for in_port 1 to output 2 225 | #if dpid_to_str(event.connection.dpid) == "00-00-00-00-00-01": 226 | # return 227 | if dpid_to_str(event.connection.dpid) == "00-00-00-00-00-0a": 228 | fw1 = FW1(event.connection, self.transparent) 229 | fw1.AddRule(event.connection) 230 | elif dpid_to_str(event.connection.dpid) == "00-00-00-00-00-0b": 231 | fw2 = FW2(event.connection, self.transparent) 232 | fw2.AddRule(event.connection) 233 | else: 234 | return 235 | 236 | class FW1 (FirewallSwitch): 237 | @staticmethod 238 | def AddRule(connection): 239 | #connection = connection() 240 | # ICMP Echo Request in_port 2 out_port 1 241 | fm = of.ofp_flow_mod() 242 | fm.match.in_port = 2 243 | fm.priority = 33001 244 | fm.match.dl_type = 0x0800 245 | fm.match.nw_proto=pkt.ipv4.ICMP_PROTOCOL 246 | fm.match.tp_src = pkt.ICMP.TYPE_ECHO_REQUEST 247 | fm.actions.append(of.ofp_action_output( port = 1 ) ) 248 | connection.send( fm ) 249 | # ICMP Echo Reply in_port 1 out_port 2 250 | fm = of.ofp_flow_mod() 251 | fm.match.in_port = 1 252 | fm.priority = 33001 253 | fm.match.dl_type = 0x0800 254 | fm.match.nw_proto=pkt.ipv4.ICMP_PROTOCOL 255 | fm.match.tp_src = pkt.ICMP.TYPE_ECHO_REPLY 256 | fm.actions.append(of.ofp_action_output( port = 2 ) ) 257 | connection.send( fm ) 258 | 259 | class FW2 (FirewallSwitch): 260 | @staticmethod 261 | def AddRule(connection): 262 | # ALLOW default in_port 2 out_port 1 263 | fm = of.ofp_flow_mod() 264 | fm.match.in_port = 2 265 | fm.priority = 33001 266 | #fm.match.dl_type = 0x0800 267 | #fm.match.nw_proto=pkt.ipv4.ICMP_PROTOCOL 268 | #fm.match.tp_src = pkt.ICMP.TYPE_ECHO_REQUEST 269 | fm.actions.append(of.ofp_action_output( port = 1 ) ) 270 | connection.send( fm ) 271 | # Allow Echo Reply in_port 1 out_port 2 272 | fm = of.ofp_flow_mod() 273 | fm.match.in_port = 1 274 | fm.priority = 33001 275 | fm.match.dl_type = 0x0800 276 | fm.match.nw_proto=pkt.ipv4.ICMP_PROTOCOL 277 | fm.match.tp_src = pkt.ICMP.TYPE_ECHO_REPLY 278 | fm.actions.append(of.ofp_action_output( port = 2 ) ) 279 | connection.send( fm ) 280 | 281 | def launch (transparent=False, hold_down=_flood_delay): 282 | """ 283 | Starts an firewall switch. 284 | """ 285 | try: 286 | global _flood_delay 287 | _flood_delay = int(str(hold_down), 10) 288 | assert _flood_delay >= 0 289 | except: 290 | raise RuntimeError("Expected hold-down to be a number") 291 | 292 | core.registerNew(firewall, str_to_bool(transparent)) 293 | -------------------------------------------------------------------------------- /firewall/l2_firewall.py: -------------------------------------------------------------------------------- 1 | # Copyright 2011-2012 James McCauley 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at: 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | """ 16 | An L2 learning switch. 17 | It is derived from one written live for an SDN crash course. 18 | It is somwhat similar to NOX's pyswitch in that it installs 19 | exact-match rules for each flow. 20 | """ 21 | 22 | from pox.core import core 23 | import pox.openflow.libopenflow_01 as of 24 | from pox.lib.util import dpid_to_str 25 | from pox.lib.util import str_to_bool 26 | from pox.lib.addresses import IPAddr, EthAddr 27 | import time 28 | import pox.lib.packet as pkt 29 | from pox.lib.addresses import EthAddr 30 | from pox.forwarding.l2_learning import LearningSwitch 31 | import os 32 | 33 | log = core.getLogger() 34 | rPath = "" 35 | # We don't want to flood immediately when a switch connects. 36 | # Can be overriden on commandline. 37 | _flood_delay = 0 38 | 39 | class FirewallSwitch (object): 40 | def __init__ (self, connection, transparent): 41 | # Switch we'll be adding L2 learning switch capabilities to 42 | self.connection = connection 43 | self.transparent = transparent 44 | 45 | # Our table 46 | self.macToPort = {} 47 | 48 | # Our firewall table 49 | self.firewall = {} 50 | 51 | # Add a Couple of Rules 52 | #self.AddRule('00-00-00-00-00-01',EthAddr('00:00:00:00:00:01')) 53 | #self.AddRule('00-00-00-00-00-01',EthAddr('00:00:00:00:00:02')) 54 | self.BasicRule(connection) 55 | connection.addListeners(self) 56 | 57 | # We just use this to know when to log a helpful message 58 | self.hold_down_expired = _flood_delay == 0 59 | 60 | #log.debug("Initializing LearningSwitch, transparent=%s", 61 | # str(self.transparent)) 62 | 63 | 64 | # function that allows adding firewall rules into the firewall table 65 | def AddRule (self, dpidstr, src=0,value=True): 66 | self.firewall[(dpidstr,src)]=value 67 | log.debug("Adding firewall rule in %s: %s", dpidstr, src) 68 | 69 | @classmethod 70 | def getConnection(self): 71 | return self.connection 72 | def BasicRule (self, connection): 73 | fm = of.ofp_flow_mod() 74 | fm.match.in_port = 1 75 | fm.priority = 18001 76 | fm.match.dl_type = 0x0806 77 | fm.actions.append(of.ofp_action_output( port = 2 ) ) 78 | connection.send( fm ) 79 | # Allow arp based on dl_type for in_port 2 to output 1 80 | fm = of.ofp_flow_mod() 81 | fm.match.in_port = 2 82 | fm.priority = 18001 83 | fm.match.dl_type = 0x0806 84 | fm.actions.append(of.ofp_action_output( port = 1 ) ) 85 | connection.send( fm ) 86 | # Default drop 87 | fm = of.ofp_flow_mod() 88 | fm.priority = 1001 89 | connection.send( fm ) 90 | 91 | # function that allows deleting firewall rules from the firewall table 92 | # Not used DeleteRule 93 | def DeleteRule (self, dpidstr, src=0): 94 | try: 95 | del self.firewall[(dpidstr,src)] 96 | log.debug("Deleting firewall rule in %s: %s", 97 | dpidstr, src) 98 | except KeyError: 99 | log.error("Cannot find in %s: %s", 100 | dpidstr, src) 101 | 102 | 103 | # check if packet is compliant to rules before proceeding 104 | def CheckRule (self, dpidstr, src=0): 105 | try: 106 | entry = self.firewall[(dpidstr, src)] 107 | if (entry == True): 108 | log.debug("Rule (%s) found in %s: FORWARD", 109 | src, dpidstr) 110 | else: 111 | log.debug("Rule (%s) found in %s: DROP", 112 | src, dpidstr) 113 | return entry 114 | except KeyError: 115 | log.debug("Rule (%s) NOT found in %s: DROP", 116 | src, dpidstr) 117 | return False 118 | 119 | def _handle_PacketIn (self, event): 120 | """ 121 | Handle packet in messages from the switch to implement above algorithm. 122 | """ 123 | 124 | packet = event.parsed 125 | 126 | def flood (message = None): 127 | """ Floods the packet """ 128 | msg = of.ofp_packet_out() 129 | if time.time() - self.connection.connect_time >= _flood_delay: 130 | # Only flood if we've been connected for a little while... 131 | 132 | if self.hold_down_expired is False: 133 | # Oh yes it is! 134 | self.hold_down_expired = True 135 | log.info("%s: Flood hold-down expired -- flooding", 136 | dpid_to_str(event.dpid)) 137 | 138 | if message is not None: log.debug(message) 139 | #log.debug("%i: flood %s -> %s", event.dpid,packet.src,packet.dst) 140 | # OFPP_FLOOD is optional; on some switches you may need to change 141 | # this to OFPP_ALL. 142 | msg.actions.append(of.ofp_action_output(port = of.OFPP_FLOOD)) 143 | else: 144 | pass 145 | #log.info("Holding down flood for %s", dpid_to_str(event.dpid)) 146 | msg.data = event.ofp 147 | msg.in_port = event.port 148 | self.connection.send(msg) 149 | 150 | def drop (duration = None): 151 | """ 152 | Drops this packet and optionally installs a flow to continue 153 | dropping similar ones for a while 154 | """ 155 | if duration is not None: 156 | if not isinstance(duration, tuple): 157 | duration = (duration,duration) 158 | msg = of.ofp_flow_mod() 159 | msg.match = of.ofp_match.from_packet(packet) 160 | msg.idle_timeout = duration[0] 161 | msg.hard_timeout = duration[1] 162 | msg.buffer_id = event.ofp.buffer_id 163 | self.connection.send(msg) 164 | elif event.ofp.buffer_id is not None: 165 | msg = of.ofp_packet_out() 166 | msg.buffer_id = event.ofp.buffer_id 167 | msg.in_port = event.port 168 | self.connection.send(msg) 169 | 170 | self.macToPort[packet.src] = event.port # 1 171 | 172 | # Get the DPID of the Switch Connection 173 | dpidstr = dpid_to_str(event.connection.dpid) 174 | 175 | packet = event.parsed 176 | if packet.type == packet.ARP_TYPE: 177 | log.info("Caught Arp packet") 178 | 179 | # Check the Firewall Rules 180 | if self.CheckRule(dpidstr, packet.src) == False: 181 | drop() 182 | return 183 | 184 | if not self.transparent: # 2 185 | if packet.type == packet.LLDP_TYPE or packet.dst.isBridgeFiltered(): 186 | drop() # 2a 187 | return 188 | 189 | if packet.dst.is_multicast: 190 | flood() # 3a 191 | else: 192 | if packet.dst not in self.macToPort: # 4 193 | flood("Port for %s unknown -- flooding" % (packet.dst,)) # 4a 194 | else: 195 | port = self.macToPort[packet.dst] 196 | if port == event.port: # 5 197 | # 5a 198 | log.warning("Same port for packet from %s -> %s on %s.%s. Drop." 199 | % (packet.src, packet.dst, dpid_to_str(event.dpid), port)) 200 | drop(10) 201 | return 202 | # 6 203 | log.debug("installing flow for %s.%i -> %s.%i" % 204 | (packet.src, event.port, packet.dst, port)) 205 | msg = of.ofp_flow_mod() 206 | msg.match = of.ofp_match.from_packet(packet, event.port) 207 | msg.idle_timeout = 10 208 | msg.hard_timeout = 30 209 | msg.actions.append(of.ofp_action_output(port = port)) 210 | msg.data = event.ofp # 6a 211 | self.connection.send(msg) 212 | 213 | 214 | class firewall (object): 215 | """ 216 | Waits for OpenFlow switches to connect and makes them learning switches. 217 | """ 218 | def __init__ (self, transparent): 219 | core.openflow.addListeners(self) 220 | self.transparent = transparent 221 | 222 | def _handle_ConnectionUp (self, event): 223 | global rPath 224 | log.debug("Connection %s" % (event.connection,)) 225 | log.debug("report rPath %s ", rPath) 226 | #FirewallSwitch(event.connection, self.transparent) 227 | log.debug("###################DPID#############:%s ", dpid_to_str(event.connection.dpid)) 228 | # Allow arp based on dl_type for in_port 1 to output 2 229 | #if dpid_to_str(event.connection.dpid) == "00-00-00-00-00-01": 230 | # return 231 | if dpid_to_str(event.connection.dpid) == "00-00-00-00-00-0a": 232 | fw1 = FW1(event.connection, self.transparent) 233 | fw1.AddRule(event.connection) 234 | elif dpid_to_str(event.connection.dpid) == "00-00-00-00-00-0b": 235 | fw2 = FW2(event.connection, self.transparent) 236 | fw2.AddRule(event.connection) 237 | if dpid_to_str(event.connection.dpid) == "00-00-00-00-00-08": 238 | os.system('sudo /usr/local/bin/click /opt/ik2220/pox/ext/ids.click rPath='+rPath+' &') 239 | elif dpid_to_str(event.connection.dpid) == "00-00-00-00-00-06": 240 | os.system('sudo /usr/local/bin/click /opt/ik2220/pox/ext/lb_update.click LB=lb6 VIP=100.0.0.25 prt_n=53 prt=udp port1=eth2 port2=eth1 DIP_1=100.0.0.20 DIP_2=100.0.0.21 DIP_3=100.0.0.22 rPath='+rPath+' &') 241 | elif dpid_to_str(event.connection.dpid) == "00-00-00-00-00-07": 242 | os.system('sudo /usr/local/bin/click /opt/ik2220/pox/ext/lb_update.click LB=lb7 VIP=100.0.0.45 prt_n=80 prt=tcp port1=eth1 port2=eth2 DIP_1=100.0.0.40 DIP_2=100.0.0.41 DIP_3=100.0.0.42 rPath='+rPath+' &') 243 | elif dpid_to_str(event.connection.dpid) == "00-00-00-00-00-09": 244 | #os.system('sudo /usr/local/bin/click /opt/ik2220/click/n9.click &') 245 | os.system('sudo /usr/local/bin/click /opt/ik2220/pox/ext/napt.click rPath='+rPath+' &') 246 | else: 247 | LearningSwitch(event.connection, False) 248 | 249 | class FW1 (FirewallSwitch): 250 | @staticmethod 251 | def AddRule(connection): 252 | #connection = connection() 253 | # ICMP Echo Request in_port 2 out_port 1 254 | fm = of.ofp_flow_mod() 255 | fm.match.in_port = 2 256 | fm.priority = 33001 257 | fm.match.dl_type = 0x0800 258 | fm.match.nw_proto=pkt.ipv4.ICMP_PROTOCOL 259 | fm.match.tp_src = pkt.ICMP.TYPE_ECHO_REQUEST 260 | fm.actions.append(of.ofp_action_output( port = 1 ) ) 261 | connection.send( fm ) 262 | # ICMP Echo Reply in_port 1 out_port 2 263 | fm = of.ofp_flow_mod() 264 | fm.match.in_port = 1 265 | fm.priority = 33001 266 | fm.match.dl_type = 0x0800 267 | fm.match.nw_proto=pkt.ipv4.ICMP_PROTOCOL 268 | fm.match.tp_src = pkt.ICMP.TYPE_ECHO_REPLY 269 | fm.actions.append(of.ofp_action_output( port = 2 ) ) 270 | connection.send( fm ) 271 | #Allow icmp echo request for SLBs in_port 1 272 | fm = init_fm( 1, 2) 273 | fm.match.tp_src = pkt.ICMP.TYPE_ECHO_REQUEST 274 | fm.match.nw_dst='100.0.0.25' 275 | connection.send(fm) 276 | fm = init_fm( 1, 2) 277 | fm.match.tp_src = pkt.ICMP.TYPE_ECHO_REQUEST 278 | fm.match.nw_dst='100.0.0.45' 279 | connection.send(fm) 280 | #Allow ICMP reply from SLBs in_port 2 out_port 1 281 | fm = init_fm( 2, 1) 282 | fm.match.tp_src = pkt.ICMP.TYPE_ECHO_REPLY 283 | connection.send(fm) 284 | #Allow DNS Request 285 | fm = of.ofp_flow_mod() 286 | fm.match.in_port = 1 287 | fm.priority = 33001 288 | fm.match.dl_type = 0x0800 289 | fm.match.nw_proto=pkt.ipv4.UDP_PROTOCOL 290 | fm.match.tp_dst = 53 291 | fm.actions.append(of.ofp_action_output( port = 2 ) ) 292 | connection.send( fm ) 293 | #Allow DNS reply 294 | fm = of.ofp_flow_mod() 295 | fm.match.in_port = 2 296 | fm.priority = 33001 297 | fm.match.dl_type = 0x0800 298 | fm.match.nw_proto=pkt.ipv4.UDP_PROTOCOL 299 | fm.match.tp_src = 53 300 | fm.actions.append(of.ofp_action_output( port = 1 ) ) 301 | connection.send( fm ) 302 | #Allow HTTP Request 303 | fm = of.ofp_flow_mod() 304 | fm.match.in_port = 1 305 | fm.priority = 33001 306 | fm.match.dl_type = 0x0800 307 | fm.match.nw_proto=pkt.ipv4.TCP_PROTOCOL 308 | fm.match.tp_dst = 80 309 | fm.actions.append(of.ofp_action_output( port = 2 ) ) 310 | connection.send( fm ) 311 | #Allow HTTP response 312 | fm = of.ofp_flow_mod() 313 | fm.match.in_port = 2 314 | fm.priority = 33001 315 | fm.match.dl_type = 0x0800 316 | fm.match.nw_proto=pkt.ipv4.TCP_PROTOCOL 317 | fm.match.tp_src = 80 318 | fm.actions.append(of.ofp_action_output( port = 1 ) ) 319 | connection.send( fm ) 320 | 321 | class FW2 (FirewallSwitch): 322 | @staticmethod 323 | def AddRule(connection): 324 | # ALLOW default in_port 2 out_port 1 325 | fm = of.ofp_flow_mod() 326 | fm.match.in_port = 2 327 | fm.priority = 33001 328 | #fm.match.dl_type = 0x0800 329 | #fm.match.nw_proto=pkt.ipv4.ICMP_PROTOCOL 330 | #fm.match.tp_src = pkt.ICMP.TYPE_ECHO_REQUEST 331 | fm.actions.append(of.ofp_action_output( port = 1 ) ) 332 | connection.send( fm ) 333 | # Allow Echo Reply in_port 1 out_port 2 334 | fm = of.ofp_flow_mod() 335 | fm.match.in_port = 1 336 | fm.priority = 33001 337 | fm.match.dl_type = 0x0800 338 | fm.match.nw_proto=pkt.ipv4.ICMP_PROTOCOL 339 | fm.match.tp_src = pkt.ICMP.TYPE_ECHO_REPLY 340 | fm.actions.append(of.ofp_action_output( port = 2 ) ) 341 | connection.send( fm ) 342 | #Allow incoming DNS Response 343 | fm = of.ofp_flow_mod() 344 | fm.match.in_port = 1 345 | fm.priority = 55001 346 | fm.match.dl_type = 0x0800 347 | fm.match.nw_proto=pkt.ipv4.UDP_PROTOCOL 348 | fm.match.tp_src = 53 349 | fm.actions.append(of.ofp_action_output( port = 2 ) ) 350 | connection.send( fm ) 351 | #Allow HTTP incoming response 352 | fm = of.ofp_flow_mod() 353 | fm.match.in_port = 1 354 | fm.priority = 55001 355 | fm.match.dl_type = 0x0800 356 | fm.match.nw_proto=pkt.ipv4.TCP_PROTOCOL 357 | fm.match.tp_src = 80 358 | fm.actions.append(of.ofp_action_output( port = 2 ) ) 359 | connection.send( fm ) 360 | #Block ICMP request for dmz 361 | #fm = of.ofp_flow_mod() 362 | #fm.match.in_port = 2 363 | #fm.priority = 55001 364 | #fm.match.dl_type = 0x0800 365 | #fm.match.nw_proto=pkt.ipv4.ICMP_PROTOCOL 366 | #fm.match.tp_src = pkt.ICMP.TYPE_ECHO_REQUEST 367 | #fm.match.nw_dst='100.0.0.20' 368 | #connection.send( fm ) 369 | #fm = of.ofp_flow_mod() 370 | #fm.match.in_port = 2 371 | #fm.priority = 55001 372 | #fm.match.dl_type = 0x0800 373 | #fm.match.nw_proto=pkt.ipv4.ICMP_PROTOCOL 374 | #fm.match.tp_src = pkt.ICMP.TYPE_ECHO_REQUEST 375 | #fm.match.nw_dst='100.0.0.40' 376 | #connection.send( fm ) 377 | connection.send( block_icmp_ip('100.0.0.20') ) 378 | connection.send( block_icmp_ip('100.0.0.21') ) 379 | connection.send( block_icmp_ip('100.0.0.22') ) 380 | connection.send( block_icmp_ip('100.0.0.40') ) 381 | connection.send( block_icmp_ip('100.0.0.41') ) 382 | connection.send( block_icmp_ip('100.0.0.42') ) 383 | 384 | def block_icmp_ip(ip): 385 | fm = of.ofp_flow_mod() 386 | fm.match.in_port = 2 387 | fm.priority = 55001 388 | fm.match.dl_type = 0x0800 389 | fm.match.nw_proto=pkt.ipv4.ICMP_PROTOCOL 390 | fm.match.tp_src = pkt.ICMP.TYPE_ECHO_REQUEST 391 | fm.match.nw_dst=ip 392 | return fm 393 | 394 | def init_fm(in_port, out_port): 395 | fm = of.ofp_flow_mod() 396 | fm.match.in_port = in_port 397 | fm.priority = 33001 398 | fm.match.dl_type = 0x0800 399 | fm.match.nw_proto=pkt.ipv4.ICMP_PROTOCOL 400 | #fm.match.tp_src = tp_src 401 | fm.actions.append(of.ofp_action_output( port = out_port ) ) 402 | return fm 403 | 404 | def launch (pathExec, transparent=False, hold_down=_flood_delay): 405 | """ 406 | Starts an firewall switch. 407 | """ 408 | try: 409 | global _flood_delay 410 | global rPath 411 | print("Pathexec:"+pathExec) 412 | rPath = pathExec 413 | _flood_delay = int(str(hold_down), 10) 414 | assert _flood_delay >= 0 415 | except: 416 | raise RuntimeError("Expected hold-down to be a number") 417 | 418 | core.registerNew(firewall, str_to_bool(transparent)) 419 | -------------------------------------------------------------------------------- /firewall/l2_firewall_example.py: -------------------------------------------------------------------------------- 1 | # Copyright 2011-2012 James McCauley 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at: 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | """ 16 | An L2 learning switch. 17 | 18 | It is derived from one written live for an SDN crash course. 19 | It is somwhat similar to NOX's pyswitch in that it installs 20 | exact-match rules for each flow. 21 | """ 22 | 23 | from pox.core import core 24 | import pox.openflow.libopenflow_01 as of 25 | from pox.lib.util import dpid_to_str 26 | from pox.lib.util import str_to_bool 27 | import time 28 | 29 | from pox.lib.addresses import EthAddr 30 | 31 | log = core.getLogger() 32 | 33 | # We don't want to flood immediately when a switch connects. 34 | # Can be overriden on commandline. 35 | _flood_delay = 0 36 | 37 | class LearningSwitch (object): 38 | """ 39 | The learning switch "brain" associated with a single OpenFlow switch. 40 | 41 | When we see a packet, we'd like to output it on a port which will 42 | eventually lead to the destination. To accomplish this, we build a 43 | table that maps addresses to ports. 44 | 45 | We populate the table by observing traffic. When we see a packet 46 | from some source coming from some port, we know that source is out 47 | that port. 48 | 49 | When we want to forward traffic, we look up the desintation in our 50 | table. If we don't know the port, we simply send the message out 51 | all ports except the one it came in on. (In the presence of loops, 52 | this is bad!). 53 | 54 | In short, our algorithm looks like this: 55 | 56 | For each packet from the switch: 57 | 1) Use source address and switch port to update address/port table 58 | 2) Is transparent = False and either Ethertype is LLDP or the packet's 59 | destination address is a Bridge Filtered address? 60 | Yes: 61 | 2a) Drop packet -- don't forward link-local traffic (LLDP, 802.1x) 62 | DONE 63 | 3) Is destination multicast? 64 | Yes: 65 | 3a) Flood the packet 66 | DONE 67 | 4) Port for destination address in our address/port table? 68 | No: 69 | 4a) Flood the packet 70 | DONE 71 | 5) Is output port the same as input port? 72 | Yes: 73 | 5a) Drop packet and similar ones for a while 74 | 6) Install flow table entry in the switch so that this 75 | flow goes out the appopriate port 76 | 6a) Send the packet out appropriate port 77 | """ 78 | def __init__ (self, connection, transparent): 79 | # Switch we'll be adding L2 learning switch capabilities to 80 | self.connection = connection 81 | self.transparent = transparent 82 | 83 | # Our table 84 | self.macToPort = {} 85 | 86 | # Our firewall table 87 | self.firewall = {} 88 | 89 | # Add a Couple of Rules 90 | self.AddRule('00-00-00-00-00-01',EthAddr('00:00:00:00:00:01')) 91 | self.AddRule('00-00-00-00-00-01',EthAddr('00:00:00:00:00:02')) 92 | 93 | # We want to hear PacketIn messages, so we listen 94 | # to the connection 95 | connection.addListeners(self) 96 | 97 | # We just use this to know when to log a helpful message 98 | self.hold_down_expired = _flood_delay == 0 99 | 100 | #log.debug("Initializing LearningSwitch, transparent=%s", 101 | # str(self.transparent)) 102 | 103 | 104 | # function that allows adding firewall rules into the firewall table 105 | def AddRule (self, dpidstr, src=0,value=True): 106 | self.firewall[(dpidstr,src)]=value 107 | log.debug("Adding firewall rule in %s: %s", dpidstr, src) 108 | 109 | # function that allows deleting firewall rules from the firewall table 110 | def DeleteRule (self, dpidstr, src=0): 111 | try: 112 | del self.firewall[(dpidstr,src)] 113 | log.debug("Deleting firewall rule in %s: %s", 114 | dpidstr, src) 115 | except KeyError: 116 | log.error("Cannot find in %s: %s", 117 | dpidstr, src) 118 | 119 | 120 | # check if packet is compliant to rules before proceeding 121 | def CheckRule (self, dpidstr, src=0): 122 | try: 123 | entry = self.firewall[(dpidstr, src)] 124 | if (entry == True): 125 | log.debug("Rule (%s) found in %s: FORWARD", 126 | src, dpidstr) 127 | else: 128 | log.debug("Rule (%s) found in %s: DROP", 129 | src, dpidstr) 130 | return entry 131 | except KeyError: 132 | log.debug("Rule (%s) NOT found in %s: DROP", 133 | src, dpidstr) 134 | return False 135 | 136 | def _handle_PacketIn (self, event): 137 | """ 138 | Handle packet in messages from the switch to implement above algorithm. 139 | """ 140 | 141 | packet = event.parsed 142 | 143 | def flood (message = None): 144 | """ Floods the packet """ 145 | msg = of.ofp_packet_out() 146 | if time.time() - self.connection.connect_time >= _flood_delay: 147 | # Only flood if we've been connected for a little while... 148 | 149 | if self.hold_down_expired is False: 150 | # Oh yes it is! 151 | self.hold_down_expired = True 152 | log.info("%s: Flood hold-down expired -- flooding", 153 | dpid_to_str(event.dpid)) 154 | 155 | if message is not None: log.debug(message) 156 | #log.debug("%i: flood %s -> %s", event.dpid,packet.src,packet.dst) 157 | # OFPP_FLOOD is optional; on some switches you may need to change 158 | # this to OFPP_ALL. 159 | msg.actions.append(of.ofp_action_output(port = of.OFPP_FLOOD)) 160 | else: 161 | pass 162 | #log.info("Holding down flood for %s", dpid_to_str(event.dpid)) 163 | msg.data = event.ofp 164 | msg.in_port = event.port 165 | self.connection.send(msg) 166 | 167 | def drop (duration = None): 168 | """ 169 | Drops this packet and optionally installs a flow to continue 170 | dropping similar ones for a while 171 | """ 172 | if duration is not None: 173 | if not isinstance(duration, tuple): 174 | duration = (duration,duration) 175 | msg = of.ofp_flow_mod() 176 | msg.match = of.ofp_match.from_packet(packet) 177 | msg.idle_timeout = duration[0] 178 | msg.hard_timeout = duration[1] 179 | msg.buffer_id = event.ofp.buffer_id 180 | self.connection.send(msg) 181 | elif event.ofp.buffer_id is not None: 182 | msg = of.ofp_packet_out() 183 | msg.buffer_id = event.ofp.buffer_id 184 | msg.in_port = event.port 185 | self.connection.send(msg) 186 | 187 | self.macToPort[packet.src] = event.port # 1 188 | 189 | # Get the DPID of the Switch Connection 190 | dpidstr = dpid_to_str(event.connection.dpid) 191 | 192 | # Check the Firewall Rules 193 | if self.CheckRule(dpidstr, packet.src) == False: 194 | drop() 195 | return 196 | 197 | if not self.transparent: # 2 198 | if packet.type == packet.LLDP_TYPE or packet.dst.isBridgeFiltered(): 199 | drop() # 2a 200 | return 201 | 202 | if packet.dst.is_multicast: 203 | flood() # 3a 204 | else: 205 | if packet.dst not in self.macToPort: # 4 206 | flood("Port for %s unknown -- flooding" % (packet.dst,)) # 4a 207 | else: 208 | port = self.macToPort[packet.dst] 209 | if port == event.port: # 5 210 | # 5a 211 | log.warning("Same port for packet from %s -> %s on %s.%s. Drop." 212 | % (packet.src, packet.dst, dpid_to_str(event.dpid), port)) 213 | drop(10) 214 | return 215 | # 6 216 | log.debug("installing flow for %s.%i -> %s.%i" % 217 | (packet.src, event.port, packet.dst, port)) 218 | msg = of.ofp_flow_mod() 219 | msg.match = of.ofp_match.from_packet(packet, event.port) 220 | msg.idle_timeout = 10 221 | msg.hard_timeout = 30 222 | msg.actions.append(of.ofp_action_output(port = port)) 223 | msg.data = event.ofp # 6a 224 | self.connection.send(msg) 225 | 226 | 227 | class l2_learning (object): 228 | """ 229 | Waits for OpenFlow switches to connect and makes them learning switches. 230 | """ 231 | def __init__ (self, transparent): 232 | core.openflow.addListeners(self) 233 | self.transparent = transparent 234 | 235 | def _handle_ConnectionUp (self, event): 236 | log.debug("Connection %s" % (event.connection,)) 237 | LearningSwitch(event.connection, self.transparent) 238 | 239 | 240 | def launch (transparent=False, hold_down=_flood_delay): 241 | """ 242 | Starts an L2 learning switch. 243 | """ 244 | try: 245 | global _flood_delay 246 | _flood_delay = int(str(hold_down), 10) 247 | assert _flood_delay >= 0 248 | except: 249 | raise RuntimeError("Expected hold-down to be a number") 250 | 251 | core.registerNew(l2_learning, str_to_bool(transparent)) 252 | -------------------------------------------------------------------------------- /firewall/l2_learning.py: -------------------------------------------------------------------------------- 1 | # Copyright 2011-2012 James McCauley 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at: 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | """ 16 | An L2 learning switch. 17 | 18 | It is derived from one written live for an SDN crash course. 19 | It is somwhat similar to NOX's pyswitch in that it installs 20 | exact-match rules for each flow. 21 | """ 22 | 23 | from pox.core import core 24 | import pox.openflow.libopenflow_01 as of 25 | from pox.lib.util import dpid_to_str 26 | from pox.lib.util import str_to_bool 27 | import time 28 | 29 | log = core.getLogger() 30 | 31 | # We don't want to flood immediately when a switch connects. 32 | # Can be overriden on commandline. 33 | _flood_delay = 0 34 | 35 | class LearningSwitch (object): 36 | """ 37 | The learning switch "brain" associated with a single OpenFlow switch. 38 | 39 | When we see a packet, we'd like to output it on a port which will 40 | eventually lead to the destination. To accomplish this, we build a 41 | table that maps addresses to ports. 42 | 43 | We populate the table by observing traffic. When we see a packet 44 | from some source coming from some port, we know that source is out 45 | that port. 46 | 47 | When we want to forward traffic, we look up the desintation in our 48 | table. If we don't know the port, we simply send the message out 49 | all ports except the one it came in on. (In the presence of loops, 50 | this is bad!). 51 | 52 | In short, our algorithm looks like this: 53 | 54 | For each packet from the switch: 55 | 1) Use source address and switch port to update address/port table 56 | 2) Is transparent = False and either Ethertype is LLDP or the packet's 57 | destination address is a Bridge Filtered address? 58 | Yes: 59 | 2a) Drop packet -- don't forward link-local traffic (LLDP, 802.1x) 60 | DONE 61 | 3) Is destination multicast? 62 | Yes: 63 | 3a) Flood the packet 64 | DONE 65 | 4) Port for destination address in our address/port table? 66 | No: 67 | 4a) Flood the packet 68 | DONE 69 | 5) Is output port the same as input port? 70 | Yes: 71 | 5a) Drop packet and similar ones for a while 72 | 6) Install flow table entry in the switch so that this 73 | flow goes out the appopriate port 74 | 6a) Send the packet out appropriate port 75 | """ 76 | def __init__ (self, connection, transparent): 77 | # Switch we'll be adding L2 learning switch capabilities to 78 | self.connection = connection 79 | self.transparent = transparent 80 | 81 | # Our table 82 | self.macToPort = {} 83 | 84 | # We want to hear PacketIn messages, so we listen 85 | # to the connection 86 | connection.addListeners(self) 87 | 88 | # We just use this to know when to log a helpful message 89 | self.hold_down_expired = _flood_delay == 0 90 | 91 | #log.debug("Initializing LearningSwitch, transparent=%s", 92 | # str(self.transparent)) 93 | 94 | def _handle_PacketIn (self, event): 95 | """ 96 | Handle packet in messages from the switch to implement above algorithm. 97 | """ 98 | 99 | packet = event.parsed 100 | 101 | def flood (message = None): 102 | """ Floods the packet """ 103 | msg = of.ofp_packet_out() 104 | if time.time() - self.connection.connect_time >= _flood_delay: 105 | # Only flood if we've been connected for a little while... 106 | 107 | if self.hold_down_expired is False: 108 | # Oh yes it is! 109 | self.hold_down_expired = True 110 | log.info("%s: Flood hold-down expired -- flooding", 111 | dpid_to_str(event.dpid)) 112 | 113 | if message is not None: log.debug(message) 114 | #log.debug("%i: flood %s -> %s", event.dpid,packet.src,packet.dst) 115 | # OFPP_FLOOD is optional; on some switches you may need to change 116 | # this to OFPP_ALL. 117 | msg.actions.append(of.ofp_action_output(port = of.OFPP_FLOOD)) 118 | else: 119 | pass 120 | #log.info("Holding down flood for %s", dpid_to_str(event.dpid)) 121 | msg.data = event.ofp 122 | msg.in_port = event.port 123 | self.connection.send(msg) 124 | 125 | def drop (duration = None): 126 | """ 127 | Drops this packet and optionally installs a flow to continue 128 | dropping similar ones for a while 129 | """ 130 | if duration is not None: 131 | if not isinstance(duration, tuple): 132 | duration = (duration,duration) 133 | msg = of.ofp_flow_mod() 134 | msg.match = of.ofp_match.from_packet(packet) 135 | msg.idle_timeout = duration[0] 136 | msg.hard_timeout = duration[1] 137 | msg.buffer_id = event.ofp.buffer_id 138 | self.connection.send(msg) 139 | elif event.ofp.buffer_id is not None: 140 | msg = of.ofp_packet_out() 141 | msg.buffer_id = event.ofp.buffer_id 142 | msg.in_port = event.port 143 | self.connection.send(msg) 144 | 145 | self.macToPort[packet.src] = event.port # 1 146 | 147 | if not self.transparent: # 2 148 | if packet.type == packet.LLDP_TYPE or packet.dst.isBridgeFiltered(): 149 | drop() # 2a 150 | return 151 | 152 | if packet.dst.is_multicast: 153 | flood() # 3a 154 | else: 155 | if packet.dst not in self.macToPort: # 4 156 | flood("Port for %s unknown -- flooding" % (packet.dst,)) # 4a 157 | else: 158 | port = self.macToPort[packet.dst] 159 | if port == event.port: # 5 160 | # 5a 161 | log.warning("Same port for packet from %s -> %s on %s.%s. Drop." 162 | % (packet.src, packet.dst, dpid_to_str(event.dpid), port)) 163 | drop(10) 164 | return 165 | # 6 166 | log.debug("installing flow for %s.%i -> %s.%i" % 167 | (packet.src, event.port, packet.dst, port)) 168 | msg = of.ofp_flow_mod() 169 | msg.match = of.ofp_match.from_packet(packet, event.port) 170 | msg.idle_timeout = 10 171 | msg.hard_timeout = 30 172 | msg.actions.append(of.ofp_action_output(port = port)) 173 | msg.data = event.ofp # 6a 174 | self.connection.send(msg) 175 | 176 | 177 | class l2_learning (object): 178 | """ 179 | Waits for OpenFlow switches to connect and makes them learning switches. 180 | """ 181 | def __init__ (self, transparent): 182 | core.openflow.addListeners(self) 183 | self.transparent = transparent 184 | 185 | def _handle_ConnectionUp (self, event): 186 | log.debug("Connection %s" % (event.connection,)) 187 | LearningSwitch(event.connection, self.transparent) 188 | if dpid_to_str(event.connection.dpid) == "00-00-00-00-00-01": 189 | return 190 | 191 | 192 | def launch (transparent=False, hold_down=_flood_delay): 193 | """ 194 | Starts an L2 learning switch. 195 | """ 196 | try: 197 | global _flood_delay 198 | _flood_delay = int(str(hold_down), 10) 199 | assert _flood_delay >= 0 200 | except: 201 | raise RuntimeError("Expected hold-down to be a number") 202 | 203 | core.registerNew(l2_learning, str_to_bool(transparent)) 204 | -------------------------------------------------------------------------------- /ik2220-assign-phase2-team5.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shrin18/SDNProject/824ac53287c27f18df2e4022693fd0ca8dd5fab7/ik2220-assign-phase2-team5.tar.gz -------------------------------------------------------------------------------- /shrinish.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /src/dns_server.py: -------------------------------------------------------------------------------- 1 | #DNS Server 2 | 3 | import socket, glob, json 4 | 5 | port = 53 6 | ip = '127.0.0.1' 7 | 8 | sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 9 | sock.bind((ip, port)) 10 | 11 | 12 | def load_zones(): 13 | 14 | jsonzone = {} 15 | zonefiles = glob.glob('zones/*.zone') 16 | 17 | for zone in zonefiles: 18 | with open(zone) as zonedata: 19 | data = json.load(zonedata) 20 | zonename = data["$origin"] 21 | jsonzone[zonename] = data 22 | return jsonzone 23 | 24 | zonedata = load_zones() 25 | 26 | def getflags(flags): 27 | 28 | byte1 = bytes(flags[:1]) 29 | byte2 = bytes(flags[1:2]) 30 | 31 | rflags = '' 32 | 33 | QR = '1' 34 | 35 | OPCODE = '' 36 | for bit in range(1,5): 37 | OPCODE += str(ord(byte1)&(1<