4 namespace eval excentis {
14 namespace eval basic {
23 proc ParseFilter { filter } {
37 set filter [
string trim [
string tolower $filter]]
43 while { [scan $filter "%s %\[^\n\]" newPart rest] > 1} {
44 set newFilter [
append newFilter $newPart " "]
47 set filter [
append newFilter $newPart]
53 proc ConvertSubFilter { inFilter } {
57 if { $endPosition != {}} {
58 set startPosition [
lindex [
lindex [regexp -indices -inline {[^\s\t]} $inFilter] 0] 0]
59 if {[string index $inFilter $startPosition] == "("} {
62 set firstFilter [
string range $inFilter $startPosition [
expr $endPosition-1]]
65 set endPosition1 [
lindex [
lindex [regexp -indices -inline {[^\s\t]} [
string range $inFilter [
expr $endPosition + 1] end] ] 0] 0]
66 if {$endPosition1 == {}} {
70 set endPosition [
expr $endPosition + $endPosition1 +1]
71 set startPosition $endPosition
72 set endPosition1 [
lindex [
lindex [regexp -indices -inline {[\s\t]} [
string range $inFilter $endPosition end] ] 0] 0]
73 set endPosition [
expr $endPosition + $endPosition1]
75 append result " " [
string range $inFilter $startPosition $endPosition]
76 set endPosition1 [
lindex [
lindex [regexp -indices -inline {[^\s\t]} [
string range $inFilter [
expr $endPosition + 1] end] ] 0] 0]
77 set endPosition [
expr $endPosition + $endPosition1 ]
78 set secondFilter [
string range $inFilter $endPosition end]
88 proc GetSubFilter { inFilter } {
93 set position [
lindex [
lindex [regexp -indices -inline {[^\s\t]} $inFilter] 0] 0]
94 if {$position == {}} {
99 if {[string index $inFilter $position] == ")"} {
101 error "ParseFilter error: Unbalanced brace found. Please make sure each opening brace finds a closing one."
102 }
elseif {[string index $inFilter $position] == "("} {
104 set numberOfBracesToFind 1
107 set position1 [
lindex [
lindex [regexp -indices -inline {[()]} [
string range $inFilter [
expr $position + 1] end] ] 0] 0]
108 if {$position1 == {}} {
109 error "ParseFilter error: Unbalanced brace found. Please make sure each opening brace finds a closing one."
112 set position [
expr $position + $position1 + 1]
114 if {[string index $inFilter $position] == "("} {
115 incr numberOfBracesToFind
117 incr numberOfBracesToFind -1
119 if {$numberOfBracesToFind == 0} {
126 set position1 [
lindex [
lindex [regexp -indices -inline {[\s\t]} [
string range $inFilter [
expr $position + 1] end]] 0] 0]
127 if {$position1 == {}} {
129 error "ParseFilter error: Error during parsing of filter string. Expected a logical operation."
131 set position [
expr $position + $position1 + 1]
132 set position1 [
lindex [
lindex [regexp -indices -inline {[\s\t]} [
string range $inFilter [
expr $position + 1] end]] 0] 0]
133 if {$position1 == {}} {
135 error "ParseFilter error: Error during parsing of filter string. Expected a value."
137 set position [
expr $position + $position1 + 1]
139 if {[string index $inFilter [expr $position + 1]] == "("} {
140 set position1 [
lindex [
lindex [regexp -indices -inline {[)]} [
string range $inFilter [
expr $position + 1] end]] 0] 0]
141 set position [
expr $position + $position1 + 1]
143 set position1 [
lindex [
lindex [regexp -indices -inline {[\s\t]} [
string range $inFilter [
expr $position + 1] end]] 0] 0]
144 if {$position1 == {}} {
147 set position [
expr $position + $position1 + 1]
152 proc ConvertToBpf { filter } {
160 set filterParts [
split $filter]
161 if {[llength $filterParts] > 3} {
162 error "ConvertToBpf error: Invalid filter format: \"$filter\""
165 set field [
lindex $filterParts 0]
167 set operation [
lindex $filterParts 1 ]
168 switch -- $operation {
169 "eq" - "==" { set operation "=" }
170 "gt" - ">" { set operation ">" }
171 "lt" - "<" { set operation "<" }
172 "ge" - ">=" { set operation ">=" }
173 "le" - "<=" { set operation "<=" }
175 "!=" - "ne" { set operation "!=" }
176 default { error "ConvertToBpf error: Invalid filter operation: $operation" }
179 set value [
lindex $filterParts 2 ]
182 switch -regexp $field {
189 default { error "ConvertToBpf error: Unknown or unsupported filter protocol: $field\n Contact byteblower@exentis.com if you require support for this." }
195 proc ConvertToBpfEth { field operation value } {
204 if { [string equal $operation "="]} {
206 }
elseif { [string equal $operation "!="]} {
210 if { ! [
string equal $field "eth.len"]} {
211 error "ConvertToBpfEth error: Operation '$operation' not allowed on Ethernet protocol fields (except eth.len)\n Contact byteblower@exentis.com if you require support for this."
215 switch -exact $field {
217 # Convert the value from 000102030405 to 00:01:02:03:04:05
218 set result "ether dst $value"
221 set result "ether src $value"
224 set result "ether proto $value"
227 set result "len $operation $value"
230 error "ConvertToBpfEth error: Unknown or unsupported Ethernet field: $field\n Contact byteblower@exentis.com if you require support for this."
234 set result "!($result)"
240 proc ConvertToBpfVlan { field operation value } {
249 if { [string equal $operation "="]} {
251 }
elseif { [string equal $operation "!="]} {
255 error "ConvertToBpfVlan error: Operation '$operation' not allowed on VLAN protocol fields\n Contact byteblower@exentis.com if you require support for this."
258 switch -exact $field {
260 set result "vlan $value"
263 error "ConvertToBpfVlan error: Unknown or unsupported VLAN field: $field\n Contact byteblower@exentis.com if you require support for this."
267 set result "!($result)"
273 proc ConvertToBpfIp { field operation value } {
282 if { [string equal $operation "="]} {
284 }
elseif { [string equal $operation "!="]} {
288 error "ConvertToBpfIp error: Operation '$operation' not allowed on IP protocol fields\n Contact byteblower@exentis.com if you require support for this."
291 switch -exact $field {
293 set result "ip dst $value"
296 set result "ip src $value"
299 set result "ip\[0\] & 0xF0 = $value"
302 # Ip header length ( in words )
303 set result "ip\[0\] & 0x0F = $value"
307 set result "ip\[1\] = $value"
311 set result "ip\[2:2\] $operation $value"
315 set result "ip\[4:2\] = $value"
318 # Ip don't fragment flag
319 set result "ip\[6\] & 0x40 = [
expr $value * 0x40]"
322 # Ip more fragments flag
323 set result "ip\[7\] & 0x20 = [
expr $value * 0x20]"
327 set result "ip\[7:2\] & 0x1FFF = $value"
331 set result "ip\[8\] = $value"
334 # Higher layer protocol
335 set result "ip\[9\] = $value"
339 set result "ip\[10:2\] = $value"
342 error "ConvertToBpfIp error: Unknown or unsupported IP field: $field\n Contact byteblower@exentis.com if you require support for this."
346 set result "!($result)"
352 proc ConvertToBpfUdp {field operation value} {
361 if { [string equal $operation "="]} {
363 }
elseif { [string equal $operation "!="]} {
367 error "ConvertToBpfUdp error: Operation '$operation' not allowed on UDP protocol fields\n Contact byteblower@exentis.com if you require support for this."
369 switch -exact $field {
371 set result "udp src port $value"
374 set result "udp dst port $value"
377 set result "udp\[4:2\] = $value"
380 set result "udp\[6:2\] = $value"
383 error "ConvertToBpfUdp error: Unknown or unsupported UDP field: $field\n Contact byteblower@exentis.com if you require support for this."
387 set result "!($result)"
393 proc ConvertToBpfTcp {field operation value} {
402 if { [string equal $operation "="]} {
404 }
elseif { [string equal $operation "!="]} {
408 error "ConvertToBpfTcp error: Operation '$operation' not allowed on TCP protocol fields\n Contact byteblower@exentis.com if you require support for this."
411 switch -exact $field {
413 set result "tcp src port $value"
416 set result "tcp dst port $value"
419 error "ConvertToBpfTcp error: Unknown or unsupported TCP field: $field\n Contact byteblower@exentis.com if you require support for this."
423 set result "!($result)"
429 proc ConvertToBpfIpv6 { field operation value } {
438 if { [string equal $operation "="]} {
440 }
elseif { [string equal $operation "!="]} {
444 error "ConvertToBpfIpv6 error: Operation '$operation' not allowed on IPv6 protocol fields\n Contact byteblower@exentis.com if you require support for this."
447 switch -exact $field {
449 set result "ip6 dst $value"
452 set result "ip6 src $value"
455 set result "ip6\[0\] & 0xF0 = $value"
458 # Ipv6 traffic class ( in words )
459 set result "ip6\[0:2\] & 0x0FF0 = $value"
463 set result "ip6\[1:3\] & 0x0FFFFF = $value"
466 # Ipv6 payload Length
467 set result "ip6\[4:2\] $operation $value"
471 set result "ip6\[6\] = $value"
475 set result "ip6\[7\] = $value"
478 error "ConvertToBpfIpv6 error: Unknown or unsupported IPv6 field: $field\n Contact byteblower@exentis.com if you require support for this."
482 set result "!($result)"