Lab 2-Ryu Firewall簡易實作
本實作是利用 REST API (傳送點) 的方式來實現防火牆。
實驗拓樸
首先在Controller上面去執行rest_firewall的應用程式
$ryu-manager ryu.app.rest_firewall
loading app ryu.app.rest_firewall
loading app ryu.controller.ofp_handler
instantiating app None of DPSet
creating context dpset
creating context wsgi
instantiating app ryu.app.rest_firewall of RestFirewallAPI
instantiating app ryu.controller.ofp_handler of OFPHandler
(2210) wsgi starting up on http://0.0.0.0:8080/
接著Ryu和交換器的連線完成後,會出現下列訊息。
[INFO] switch_id={switch_id}: Join as firewall
啟動狀態
防火牆啟動後,在起始狀態下全部的封包都會處於無法傳送的狀態。
接下來我們要下指令使其生效。
$curl -X PUT http://127.0.0.1:8080/firewall/module/enable/{switch_id}
鍵入指令後,會顯示防火牆啟動訊息(如圖所示)。
可以透過輸入
$curl http://127.0.0.1:8080/firewall/module/status
來確認啟用的狀況。
此時,可以先從Host1向Host2執行Ping指令,但是因為防火牆存取規則權限還沒有被設定,所以還是處於無法連通的狀態。
在Controller上也可以而看到封包被阻擋的過程被寫進Log檔中。
所以接下來要新增規則,來使Host1和Host2可以順利地互相使用Ping指令。
新增規則
※新增規則,規則的編號會自動編碼。
Host1→Host2
$curl -X POST -d '{"nw_src": "192.168.1.101", "nw_dst": "192.168.1.102", "nw_proto": "ICMP"}' http://127.0.0.1:8080/firewall/rules/{switch_id}
Host2→Host1
$curl -X POST -d '{"nw_src": "192.168.1.102", "nw_dst": "192.168.1.101", "nw_proto": "ICMP"}' http://127.0.0.1:8080/firewall/rules/{switch_id}
新增加的規則做為 Flow Entry 會被送達到Switch中。
可以連接Switch(10.0.0.1)中,去查看裡面的Flow Table。
可以發現多增加了兩條Flow Table的規則。
接著在Host2 和 Host3 之間,新增加規則允許所有 ipv4 封包通過
Host2→Host3
$curl -X POST -d '{"nw_src": "192.168.1.102", "nw_dst": "192.168.1.103"}' http://127.0.0.1:8080/firewall/rules/{switch_id}
Host3→Host2
$curl -X POST -d '{"nw_src": "192.168.1.103", "nw_dst": "192.168.1.102"}' http://127.0.0.1:8080/firewall/rules/{switch_id}
新增阻斷 Host2 和 Host3 之間的 ping( ICMP )封包規則。
優先度的值設定為大於 1 的值。
Host2→Host3
$curl -X POST -d '{"nw_src": "192.168.1.102", "nw_dst": "192.168.1.103", "nw_proto": "ICMP", "actions": "DENY", "priority": "10"}' http://127.0.0.1:8080/firewall/rules/{switch_id}
Host3→Host2
$curl -X POST -d '{"nw_src": "192.168.1.103", "nw_dst": "192.168.1.102", "nw_proto": "ICMP", "actions": "DENY", "priority": "10"}' http://127.0.0.1:8080/firewall/rules/{switch_id}
確認規則
確認已經設定完成的規則,可以輸入下列指令確認。
$curl http://127.0.0.1:8080/firewall/rules/{switch_id}
設定完成規則後,目前運作的方式,如圖所示。
這樣就完成了一個簡易的防火牆。
刪除規則
刪除 rule id 為 5 的規則
$curl -X DELETE -d'{"rule_id": "5"}' http://localhost:8080/firewall/rules/{switch_id}
刪除 rule id 為 6 的規則
$curl -X DELETE -d'{"rule_id": "6"}' http://localhost:8080/firewall/rules/{switch_id}
即可使 Host2 <-> Host3 的封包傳送正常運作。
延伸學習
在官網上有rest_firewall.py檔案可以學習使用
github:https://github.com/osrg/ryu/blob/master/ryu/app/rest_firewall.py