1*de1e4e89SAndroid Build Coastguard Worker 2*de1e4e89SAndroid Build Coastguard WorkerVery funky action. I do plan to add to a few more things to it 3*de1e4e89SAndroid Build Coastguard WorkerThis is the basic stuff. Idea borrowed from the way ethernet switches 4*de1e4e89SAndroid Build Coastguard Workermirror and redirect packets. The main difference with say a vannila 5*de1e4e89SAndroid Build Coastguard Workerethernet switch is that you can use u32 classifier to select a 6*de1e4e89SAndroid Build Coastguard Workerflow to be mirrored. High end switches typically can select based 7*de1e4e89SAndroid Build Coastguard Workeron more than just a port (eg a 5 tuple classifier). They may also be 8*de1e4e89SAndroid Build Coastguard Workercapable of redirecting. 9*de1e4e89SAndroid Build Coastguard Worker 10*de1e4e89SAndroid Build Coastguard WorkerUsage: 11*de1e4e89SAndroid Build Coastguard Worker 12*de1e4e89SAndroid Build Coastguard Workermirred <DIRECTION> <ACTION> [index INDEX] <dev DEVICENAME> 13*de1e4e89SAndroid Build Coastguard Workerwhere: 14*de1e4e89SAndroid Build Coastguard WorkerDIRECTION := <ingress | egress> 15*de1e4e89SAndroid Build Coastguard WorkerACTION := <mirror | redirect> 16*de1e4e89SAndroid Build Coastguard WorkerINDEX is the specific policy instance id 17*de1e4e89SAndroid Build Coastguard WorkerDEVICENAME is the devicename 18*de1e4e89SAndroid Build Coastguard Worker 19*de1e4e89SAndroid Build Coastguard WorkerDirection: 20*de1e4e89SAndroid Build Coastguard Worker- Ingress is not supported at the moment. It will be in the 21*de1e4e89SAndroid Build Coastguard Workerfuture as well as mirror/redirecting to a socket. 22*de1e4e89SAndroid Build Coastguard Worker 23*de1e4e89SAndroid Build Coastguard WorkerAction: 24*de1e4e89SAndroid Build Coastguard Worker- Mirror takes a copy of the packet and sends it to specified 25*de1e4e89SAndroid Build Coastguard Workerdev ("port" in ethernet switch/bridging terminology) 26*de1e4e89SAndroid Build Coastguard Worker- redirect 27*de1e4e89SAndroid Build Coastguard Workersteals the packet and redirects to specified destination dev. 28*de1e4e89SAndroid Build Coastguard Worker 29*de1e4e89SAndroid Build Coastguard WorkerWhat NOT to do if you dont want your machine to crash: 30*de1e4e89SAndroid Build Coastguard Worker------------------------------------------------------ 31*de1e4e89SAndroid Build Coastguard Worker 32*de1e4e89SAndroid Build Coastguard WorkerDo not create loops! 33*de1e4e89SAndroid Build Coastguard WorkerLoops are not hard to create in the egress qdiscs. 34*de1e4e89SAndroid Build Coastguard Worker 35*de1e4e89SAndroid Build Coastguard WorkerHere are simple rules to follow if you dont want to get 36*de1e4e89SAndroid Build Coastguard Workerhurt: 37*de1e4e89SAndroid Build Coastguard WorkerA) Do not have the same packet go to same netdevice twice 38*de1e4e89SAndroid Build Coastguard Workerin a single graph of policies. Your machine will just hang! 39*de1e4e89SAndroid Build Coastguard WorkerThis is design intent _not a bug_ to teach you some lessons. 40*de1e4e89SAndroid Build Coastguard Worker 41*de1e4e89SAndroid Build Coastguard WorkerIn the future if there are easy ways to do this in the kernel 42*de1e4e89SAndroid Build Coastguard Workerwithout affecting other packets not interested in this feature 43*de1e4e89SAndroid Build Coastguard WorkerI will add them. At the moment that is not clear. 44*de1e4e89SAndroid Build Coastguard Worker 45*de1e4e89SAndroid Build Coastguard WorkerSome examples of bad things NOT to do: 46*de1e4e89SAndroid Build Coastguard Worker1) redirecting eth0 to eth0 47*de1e4e89SAndroid Build Coastguard Worker2) eth0->eth1-> eth0 48*de1e4e89SAndroid Build Coastguard Worker3) eth0->lo-> eth1-> eth0 49*de1e4e89SAndroid Build Coastguard Worker 50*de1e4e89SAndroid Build Coastguard WorkerB) Do not redirect from one IFB device to another. 51*de1e4e89SAndroid Build Coastguard WorkerRemember that IFB is a very specialized case of packet redirecting 52*de1e4e89SAndroid Build Coastguard Workerdevice. Instead of redirecting it puts packets at the exact spot 53*de1e4e89SAndroid Build Coastguard Workeron the stack it found them from. 54*de1e4e89SAndroid Build Coastguard WorkerRedirecting from ifbX->ifbY will actually not crash your machine but your 55*de1e4e89SAndroid Build Coastguard Workerpackets will all be dropped (this is much simpler to detect 56*de1e4e89SAndroid Build Coastguard Workerand resolve and is only affecting users of ifb as opposed to the 57*de1e4e89SAndroid Build Coastguard Workerwhole stack). 58*de1e4e89SAndroid Build Coastguard Worker 59*de1e4e89SAndroid Build Coastguard WorkerIn the case of A) the problem has to do with a recursive contention 60*de1e4e89SAndroid Build Coastguard Workerfor the devices queue lock and in the second case for the transmit lock. 61*de1e4e89SAndroid Build Coastguard Worker 62*de1e4e89SAndroid Build Coastguard WorkerSome examples: 63*de1e4e89SAndroid Build Coastguard Worker------------- 64*de1e4e89SAndroid Build Coastguard Worker 65*de1e4e89SAndroid Build Coastguard Worker1) Mirror all packets arriving on eth0 to be sent out on eth1. 66*de1e4e89SAndroid Build Coastguard WorkerYou may have a sniffer or some accounting box hooked up on eth1. 67*de1e4e89SAndroid Build Coastguard Worker 68*de1e4e89SAndroid Build Coastguard Worker--- 69*de1e4e89SAndroid Build Coastguard Workertc qdisc add dev eth0 ingress 70*de1e4e89SAndroid Build Coastguard Workertc filter add dev eth0 parent ffff: protocol ip prio 10 u32 \ 71*de1e4e89SAndroid Build Coastguard Workermatch u32 0 0 flowid 1:2 action mirred egress mirror dev eth1 72*de1e4e89SAndroid Build Coastguard Worker--- 73*de1e4e89SAndroid Build Coastguard Worker 74*de1e4e89SAndroid Build Coastguard WorkerIf you replace "mirror" with "redirect" then not a copy but rather 75*de1e4e89SAndroid Build Coastguard Workerthe original packet is sent to eth1. 76*de1e4e89SAndroid Build Coastguard Worker 77*de1e4e89SAndroid Build Coastguard Worker2) Host A is hooked up to us on eth0 78*de1e4e89SAndroid Build Coastguard Worker 79*de1e4e89SAndroid Build Coastguard Worker# redirect all packets arriving on ingress of lo to eth0 80*de1e4e89SAndroid Build Coastguard Worker--- 81*de1e4e89SAndroid Build Coastguard Workertc qdisc add dev lo ingress 82*de1e4e89SAndroid Build Coastguard Workertc filter add dev lo parent ffff: protocol ip prio 10 u32 \ 83*de1e4e89SAndroid Build Coastguard Workermatch u32 0 0 flowid 1:2 action mirred egress redirect dev eth0 84*de1e4e89SAndroid Build Coastguard Worker--- 85*de1e4e89SAndroid Build Coastguard Worker 86*de1e4e89SAndroid Build Coastguard WorkerOn host A start a tcpdump on interface connecting to us. 87*de1e4e89SAndroid Build Coastguard Worker 88*de1e4e89SAndroid Build Coastguard Workeron our host ping -c 2 127.0.0.1 89*de1e4e89SAndroid Build Coastguard Worker 90*de1e4e89SAndroid Build Coastguard WorkerPing would fail since all packets are heading out eth0 91*de1e4e89SAndroid Build Coastguard Workertcpudmp on host A would show them 92*de1e4e89SAndroid Build Coastguard Worker 93*de1e4e89SAndroid Build Coastguard Workerif you substitute the redirect with mirror above as in: 94*de1e4e89SAndroid Build Coastguard Workertc filter add dev lo parent ffff: protocol ip prio 10 u32 \ 95*de1e4e89SAndroid Build Coastguard Workermatch u32 0 0 flowid 1:2 action mirred egress mirror dev eth0 96*de1e4e89SAndroid Build Coastguard Worker 97*de1e4e89SAndroid Build Coastguard WorkerThen you should see the packets on both host A and the local 98*de1e4e89SAndroid Build Coastguard Workerstack (i.e ping would work). 99*de1e4e89SAndroid Build Coastguard Worker 100*de1e4e89SAndroid Build Coastguard Worker3) Even more funky example: 101*de1e4e89SAndroid Build Coastguard Worker 102*de1e4e89SAndroid Build Coastguard Worker# 103*de1e4e89SAndroid Build Coastguard Worker#allow 1 out 10 packets on ingress of lo to randomly make it to the 104*de1e4e89SAndroid Build Coastguard Worker# host A (Randomness uses the netrand generator) 105*de1e4e89SAndroid Build Coastguard Worker# 106*de1e4e89SAndroid Build Coastguard Worker--- 107*de1e4e89SAndroid Build Coastguard Workertc filter add dev lo parent ffff: protocol ip prio 10 u32 \ 108*de1e4e89SAndroid Build Coastguard Workermatch u32 0 0 flowid 1:2 \ 109*de1e4e89SAndroid Build Coastguard Workeraction drop random determ ok 10\ 110*de1e4e89SAndroid Build Coastguard Workeraction mirred egress mirror dev eth0 111*de1e4e89SAndroid Build Coastguard Worker--- 112*de1e4e89SAndroid Build Coastguard Worker 113*de1e4e89SAndroid Build Coastguard Worker4) 114*de1e4e89SAndroid Build Coastguard Worker# for packets from 10.0.0.9 going out on eth0 (could be local 115*de1e4e89SAndroid Build Coastguard Worker# IP or something # we are forwarding) - 116*de1e4e89SAndroid Build Coastguard Worker# if exceeding a 100Kbps rate, then redirect to eth1 117*de1e4e89SAndroid Build Coastguard Worker# 118*de1e4e89SAndroid Build Coastguard Worker 119*de1e4e89SAndroid Build Coastguard Worker--- 120*de1e4e89SAndroid Build Coastguard Workertc qdisc add dev eth0 handle 1:0 root prio 121*de1e4e89SAndroid Build Coastguard Workertc filter add dev eth0 parent 1:0 protocol ip prio 6 u32 \ 122*de1e4e89SAndroid Build Coastguard Workermatch ip src 10.0.0.9/32 flowid 1:16 \ 123*de1e4e89SAndroid Build Coastguard Workeraction police rate 100kbit burst 90k ok \ 124*de1e4e89SAndroid Build Coastguard Workeraction mirred egress mirror dev eth1 125*de1e4e89SAndroid Build Coastguard Worker--- 126*de1e4e89SAndroid Build Coastguard Worker 127*de1e4e89SAndroid Build Coastguard WorkerA more interesting example is when you mirror flows to a dummy device 128*de1e4e89SAndroid Build Coastguard Workerso you could tcpdump them (dummy by defaults drops all packets it sees). 129*de1e4e89SAndroid Build Coastguard WorkerThis is a very useful debug feature. 130*de1e4e89SAndroid Build Coastguard Worker 131*de1e4e89SAndroid Build Coastguard WorkerLets say you are policing packets from alias 192.168.200.200/32 132*de1e4e89SAndroid Build Coastguard Workeryou dont want those to exceed 100kbps going out. 133*de1e4e89SAndroid Build Coastguard Worker 134*de1e4e89SAndroid Build Coastguard Worker--- 135*de1e4e89SAndroid Build Coastguard Workertc qdisc add dev eth0 handle 1:0 root prio 136*de1e4e89SAndroid Build Coastguard Workertc filter add dev eth0 parent 1: protocol ip prio 10 u32 \ 137*de1e4e89SAndroid Build Coastguard Workermatch ip src 192.168.200.200/32 flowid 1:2 \ 138*de1e4e89SAndroid Build Coastguard Workeraction police rate 100kbit burst 90k drop 139*de1e4e89SAndroid Build Coastguard Worker--- 140*de1e4e89SAndroid Build Coastguard Worker 141*de1e4e89SAndroid Build Coastguard WorkerIf you run tcpdump on eth0 you will see all packets going out 142*de1e4e89SAndroid Build Coastguard Workerwith src 192.168.200.200/32 dropped or not (since tcpdump shows 143*de1e4e89SAndroid Build Coastguard Workerall packets being egressed). 144*de1e4e89SAndroid Build Coastguard WorkerExtend the rule a little to see only the packets making it out. 145*de1e4e89SAndroid Build Coastguard Worker 146*de1e4e89SAndroid Build Coastguard Worker--- 147*de1e4e89SAndroid Build Coastguard Workertc qdisc add dev eth0 handle 1:0 root prio 148*de1e4e89SAndroid Build Coastguard Workertc filter add dev eth0 parent 1: protocol ip prio 10 u32 \ 149*de1e4e89SAndroid Build Coastguard Workermatch ip src 192.168.200.200/32 flowid 1:2 \ 150*de1e4e89SAndroid Build Coastguard Workeraction police rate 10kbit burst 90k drop \ 151*de1e4e89SAndroid Build Coastguard Workeraction mirred egress mirror dev dummy0 152*de1e4e89SAndroid Build Coastguard Worker--- 153*de1e4e89SAndroid Build Coastguard Worker 154*de1e4e89SAndroid Build Coastguard WorkerNow fire tcpdump on dummy0 to see only those packets .. 155*de1e4e89SAndroid Build Coastguard Workertcpdump -n -i dummy0 -x -e -t 156*de1e4e89SAndroid Build Coastguard Worker 157*de1e4e89SAndroid Build Coastguard WorkerEssentially a good debugging/logging interface (sort of like 158*de1e4e89SAndroid Build Coastguard WorkerBSDs speacialized log device does without needing one). 159*de1e4e89SAndroid Build Coastguard Worker 160*de1e4e89SAndroid Build Coastguard WorkerIf you replace mirror with redirect, those packets will be 161*de1e4e89SAndroid Build Coastguard Workerblackholed and will never make it out. 162*de1e4e89SAndroid Build Coastguard Worker 163*de1e4e89SAndroid Build Coastguard Workercheers, 164*de1e4e89SAndroid Build Coastguard Workerjamal 165