1 '\" te 2 .\" To view license terms, attribution, and copyright for IP Filter, the 3 .\" default path is /usr/lib/ipf/IPFILTER.LICENCE. If the Illumos operating 4 .\" environment has been installed anywhere other than the default, modify the 5 .\" given path to access the file at the installed location. 6 .\" Portions Copyright (c) 2015, Joyent, Inc. 7 .TH IPF 4 "Mar 18, 2015" 8 .SH NAME 9 ipf, ipf.conf, ipf6.conf \- IP packet filter rule syntax 10 .SH DESCRIPTION 11 .PP 12 A rule file for \fBipf\fP may have any name or even be stdin. As 13 \fBipfstat\fP produces parsable rules as output when displaying the internal 14 kernel filter lists, it is quite plausible to use its output to feed back 15 into \fBipf\fP. Thus, to remove all filters on input packets, the following 16 could be done: 17 .nf 18 19 \fC# ipfstat \-i | ipf \-rf \-\fP 20 .fi 21 .SH GRAMMAR 22 .PP 23 The format used by \fBipf\fP for construction of filtering rules can be 24 described using the following grammar in BNF: 25 \fC 26 .nf 27 filter-rule = [ insert ] action in-out [ options ] [ tos ] [ ttl ] 28 [ proto ] ip [ group ]. 29 30 insert = "@" decnumber . 31 action = block | "pass" | log | "count" | skip | auth | call . 32 in-out = "in" | "out" . 33 options = [ log ] [ tag ] [ "quick" ] [ "on" interface-name [ dup ] 34 [ froute ] [ replyto ] ] . 35 tos = "tos" decnumber | "tos" hexnumber . 36 ttl = "ttl" decnumber . 37 proto = "proto" protocol . 38 ip = srcdst [ flags ] [ with withopt ] [ icmp ] [ keep ] . 39 group = [ "head" decnumber ] [ "group" decnumber ] . 40 41 block = "block" [ return-icmp[return-code] | "return-rst" ] . 42 log = "log" [ "body" ] [ "first" ] [ "or-block" ] [ "level" loglevel ] . 43 tag = "tag" tagid . 44 skip = "skip" decnumber . 45 auth = "auth" | "preauth" . 46 call = "call" [ "now" ] function-name . 47 dup = "dup-to" interface-name [ ":" ipaddr ] . 48 froute = "fastroute" | "to" interface-name [ ":" ipaddr ] . 49 replyto = "reply-to" interface-name [ ":" ipaddr ] . 50 protocol = "tcp/udp" | "udp" | "tcp" | "icmp" | decnumber . 51 srcdst = "all" | fromto . 52 fromto = "from" [ "!" ] object "to" [ "!" ] object . 53 54 return-icmp = "return-icmp" | "return-icmp-as-dest" . 55 return-code = "(" icmp-code ")" . 56 object = addr [ port-comp | port-range ] . 57 addr = "any" | nummask | host-name [ "mask" ipaddr | "mask" hexnumber ] . 58 addr = "any" | "<thishost>" | nummask | 59 host-name [ "mask" ipaddr | "mask" hexnumber ] . 60 port-comp = "port" compare port-num . 61 port-range = "port" port-num range port-num . 62 flags = "flags" flag { flag } [ "/" flag { flag } ] . 63 with = "with" | "and" . 64 icmp = "icmp-type" icmp-type [ "code" decnumber ] . 65 return-code = "(" icmp-code ")" . 66 keep = "keep" "state" [ "(" state-options ")" ] | "keep" "frags" . 67 loglevel = facility"."priority | priority . 68 69 nummask = host-name [ "/" decnumber ] . 70 host-name = ipaddr | hostname | "any" . 71 ipaddr = host-num "." host-num "." host-num "." host-num . 72 host-num = digit [ digit [ digit ] ] . 73 port-num = service-name | decnumber . 74 state-options = state-opts [ "," state-options ] . 75 76 state-opts = "age" decnumber [ "/" decnumber ] | "strict" | 77 "no-icmp-err" | "limit" decnumber | "newisn" | "sync" . 78 withopt = [ "not" | "no" ] opttype [ withopt ] . 79 opttype = "ipopts" | "short" | "frag" | "opt" optname . 80 optname = ipopts [ "," optname ] . 81 ipopts = optlist | "sec-class" [ secname ] . 82 secname = seclvl [ "," secname ] . 83 seclvl = "unclass" | "confid" | "reserv-1" | "reserv-2" | "reserv-3" | 84 "reserv-4" | "secret" | "topsecret" . 85 icmp-type = "unreach" | "echo" | "echorep" | "squench" | "redir" | 86 "timex" | "paramprob" | "timest" | "timestrep" | "inforeq" | 87 "inforep" | "maskreq" | "maskrep" | decnumber . 88 icmp-code = decumber | "net-unr" | "host-unr" | "proto-unr" | "port-unr" | 89 "needfrag" | "srcfail" | "net-unk" | "host-unk" | "isolate" | 90 "net-prohib" | "host-prohib" | "net-tos" | "host-tos" | 91 "filter-prohib" | "host-preced" | "cutoff-preced" . 92 optlist = "nop" | "rr" | "zsu" | "mtup" | "mtur" | "encode" | "ts" | 93 "tr" | "sec" | "lsrr" | "e-sec" | "cipso" | "satid" | "ssrr" | 94 "addext" | "visa" | "imitd" | "eip" | "finn" . 95 facility = "kern" | "user" | "mail" | "daemon" | "auth" | "syslog" | 96 "lpr" | "news" | "uucp" | "cron" | "ftp" | "authpriv" | 97 "audit" | "logalert" | "local0" | "local1" | "local2" | 98 "local3" | "local4" | "local5" | "local6" | "local7" . 99 priority = "emerg" | "alert" | "crit" | "err" | "warn" | "notice" | 100 "info" | "debug" . 101 102 hexnumber = "0" "x" hexstring . 103 hexstring = hexdigit [ hexstring ] . 104 decnumber = digit [ decnumber ] . 105 106 compare = "=" | "!=" | "<" | ">" | "<=" | ">=" | "eq" | "ne" | "lt" | 107 "gt" | "le" | "ge" . 108 range = "<>" | "><" . 109 hexdigit = digit | "a" | "b" | "c" | "d" | "e" | "f" . 110 digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" . 111 flag = "F" | "S" | "R" | "P" | "A" | "U" . 112 .fi 113 .PP 114 This syntax is somewhat simplified for readability, some combinations 115 that match this grammar are disallowed by the software because they do 116 not make sense (such as tcp \fBflags\fP for non-TCP packets). 117 .SH FILTER RULES 118 .PP 119 The "briefest" valid rules are (currently) no-ops and are of the form: 120 .nf 121 block in all 122 pass in all 123 log out all 124 count in all 125 .fi 126 .PP 127 Filter rules are checked in order, with the last matching rule 128 determining the fate of the packet (but see the \fBquick\fP option, 129 below). 130 .PP 131 Filters are installed by default at the end of the kernel's filter 132 lists, prepending the rule with \fB@n\fP will cause it to be inserted 133 as the n'th entry in the current list. This is especially useful when 134 modifying and testing active filter rulesets. See \fBipf\fP(1M) for more 135 information. 136 .SH ACTIONS 137 .PP 138 The action indicates what to do with the packet if it matches the rest 139 of the filter rule. Each rule MUST have an action. The following 140 actions are recognised: 141 .TP 142 .B block 143 indicates that the packet should be flagged to be dropped. In response 144 to blocking a packet, the filter may be instructed to send a reply 145 packet, either an ICMP packet (\fBreturn-icmp\fP), an ICMP packet 146 masquerading as being from the original packet's destination 147 (\fBreturn-icmp-as-dest\fP), or a TCP "reset" (\fBreturn-rst\fP). An 148 ICMP packet may be generated in response to any IP packet, and its 149 type may optionally be specified, but a TCP reset may only be used 150 with a rule which is being applied to TCP packets. When using 151 \fBreturn-icmp\fP or \fBreturn-icmp-as-dest\fP, it is possible to specify 152 the actual unreachable `type'. That is, whether it is a network 153 unreachable, port unreachable or even administratively 154 prohibited. This is done by enclosing the ICMP code associated with 155 it in parenthesis directly following \fBreturn-icmp\fP or 156 \fBreturn-icmp-as-dest\fP as follows: 157 .nf 158 block return-icmp(11) ... 159 .fi 160 .PP 161 Would return a Type-Of-Service (TOS) ICMP unreachable error. 162 .TP 163 .B pass 164 will flag the packet to be let through the filter. 165 .TP 166 .B log 167 causes the packet to be logged (as described in the LOGGING section 168 below) and has no effect on whether the packet will be allowed through 169 the filter. 170 .TP 171 .B count 172 causes the packet to be included in the accounting statistics kept by 173 the filter, and has no effect on whether the packet will be allowed through 174 the filter. These statistics are viewable with ipfstat(1M). 175 .TP 176 .B call 177 this action is used to invoke the named function in the kernel, which 178 must conform to a specific calling interface. Customised actions and 179 semantics can thus be implemented to supplement those available. This 180 feature is for use by knowledgeable hackers, and is not currently 181 documented. 182 .TP 183 .B "skip <n>" 184 causes the filter to skip over the next \fIn\fP filter rules. If a rule is 185 inserted or deleted inside the region being skipped over, then the value of 186 \fIn\fP is adjusted appropriately. 187 .TP 188 .B auth 189 this allows authentication to be performed by a user-space program running 190 and waiting for packet information to validate. The packet is held for a 191 period of time in an internal buffer whilst it waits for the program to return 192 to the kernel the \fIreal\fP flags for whether it should be allowed through 193 or not. Such a program might look at the source address and request some sort 194 of authentication from the user (such as a password) before allowing the 195 packet through or telling the kernel to drop it if from an unrecognised source. 196 .TP 197 .B preauth 198 tells the filter that for packets of this class, it should look in the 199 pre-authenticated list for further clarification. If no further matching 200 rule is found, the packet will be dropped (the FR_PREAUTH is not the same 201 as FR_PASS). If a further matching rule is found, the result from that is 202 used in its instead. This might be used in a situation where a person 203 \fIlogs in\fP to the firewall and it sets up some temporary rules defining 204 the access for that person. 205 .PP 206 The next word must be either \fBin\fP or \fBout\fP. Each packet 207 moving through the kernel is either inbound (just been received on an 208 interface, and moving towards the kernel's protocol processing) or 209 outbound (transmitted or forwarded by the stack, and on its way to an 210 interface). There is a requirement that each filter rule explicitly 211 state which side of the I/O it is to be used on. 212 .SH OPTIONS 213 .PP 214 The list of options is brief, and all are indeed optional. Where 215 options are used, they must be present in the order shown here. These 216 are the currently supported options: 217 .TP 218 .B log 219 indicates that, should this be the last matching rule, the packet 220 header will be written to the \fBipl\fP log (as described in the 221 LOGGING section below). 222 .TP 223 .B tag tagid 224 indicates that, if this rule causes the packet to be logged or entered 225 in the state table, the tagid will be logged as part of the log entry. 226 This can be used to quickly match "similar" rules in scripts that post 227 process the log files for e.g. generation of security reports or accounting 228 purposes. The tagid is a 32 bit unsigned integer. 229 .TP 230 .B quick 231 allows "short-cut" rules in order to speed up the filter or override 232 later rules. If a packet matches a filter rule which is marked as 233 \fBquick\fP, this rule will be the last rule checked, allowing a 234 "short-circuit" path to avoid processing later rules for this 235 packet. The current status of the packet (after any effects of the 236 current rule) will determine whether it is passed or blocked. 237 .IP 238 If this option is missing, the rule is taken to be a "fall-through" 239 rule, meaning that the result of the match (block/pass) is saved and 240 that processing will continue to see if there are any more matches. 241 .TP 242 .B on 243 allows an interface name to be incorporated into the matching 244 procedure. Interface names are as printed by "netstat \-i". If this 245 option is used, the rule will only match if the packet is going 246 through that interface in the specified direction (in/out). If this 247 option is absent, the rule is taken to be applied to a packet 248 regardless of the interface it is present on (i.e. on all interfaces). 249 Filter rulesets are common to all interfaces, rather than having a 250 filter list for each interface. 251 .IP 252 This option is especially useful for simple IP-spoofing protection: 253 packets should only be allowed to pass inbound on the interface from 254 which the specified source address would be expected, others may be 255 logged and/or dropped. 256 .TP 257 .B dup-to 258 causes the packet to be copied, and the duplicate packet to be sent 259 outbound on the specified interface, optionally with the destination 260 IP address changed to that specified. This is useful for off-host 261 logging, using a network sniffer. 262 .TP 263 .B to 264 causes the packet to be moved to the outbound queue on the 265 specified interface. This can be used to circumvent kernel routing 266 decisions, and even to bypass the rest of the kernel processing of the 267 packet (if applied to an inbound rule). It is thus possible to 268 construct a firewall that behaves transparently, like a filtering hub 269 or switch, rather than a router. The \fBfastroute\fP keyword is a 270 synonym for this option. 271 .SH MATCHING PARAMETERS 272 .PP 273 The keywords described in this section are used to describe attributes 274 of the packet to be used when determining whether rules match or don't 275 match. The following general-purpose attributes are provided for 276 matching, and must be used in this order: 277 .TP 278 .B tos 279 packets with different Type-Of-Service values can be filtered. 280 Individual service levels or combinations can be filtered upon. The 281 value for the TOS mask can either be represented as a hex number or a 282 decimal integer value. 283 .TP 284 .B ttl 285 packets may also be selected by their Time-To-Live value. The value given in 286 the filter rule must exactly match that in the packet for a match to occur. 287 This value can only be given as a decimal integer value. 288 .TP 289 .B proto 290 allows a specific protocol to be matched against. All protocol names 291 found in \fB/etc/protocols\fP are recognised and may be used. 292 However, the protocol may also be given as a DECIMAL number, allowing 293 for rules to match your own protocols, or new ones which would 294 out-date any attempted listing. 295 .IP 296 The special protocol keyword \fBtcp/udp\fP may be used to match either 297 a TCP or a UDP packet, and has been added as a convenience to save 298 duplication of otherwise-identical rules. 299 .\" XXX grammar should reflect this (/etc/protocols) 300 .PP 301 The \fBfrom\fP and \fBto\fP keywords are used to match against IP 302 addresses (and optionally port numbers). Rules must specify BOTH 303 source and destination parameters. 304 .PP 305 IP addresses may be specified in one of two ways: as a numerical 306 address\fB/\fPmask, or as a hostname \fBmask\fP netmask. The hostname 307 may either be a valid hostname, from either the hosts file or DNS 308 (depending on your configuration and library) or of the dotted numeric 309 form. There is no special designation for networks but network names 310 are recognised. Note that having your filter rules depend on DNS 311 results can introduce an avenue of attack, and is discouraged. 312 .PP 313 There is a special case for the hostname \fBany\fP which is taken to 314 be 0.0.0.0/0 (see below for mask syntax) and matches all IP addresses. 315 Only the presence of "any" has an implied mask, in all other 316 situations, a hostname MUST be accompanied by a mask. It is possible 317 to give "any" a hostmask, but in the context of this language, it is 318 non-sensical. 319 .PP 320 The numerical format "x\fB/\fPy" indicates that a mask of y 321 consecutive 1 bits set is generated, starting with the MSB, so a y value 322 of 16 would give 0xffff0000. The symbolic "x \fBmask\fP y" indicates 323 that the mask y is in dotted IP notation or a hexadecimal number of 324 the form 0x12345678. Note that all the bits of the IP address 325 indicated by the bitmask must match the address on the packet exactly; 326 there isn't currently a way to invert the sense of the match, or to 327 match ranges of IP addresses which do not express themselves easily as 328 bitmasks (anthropomorphization; it's not just for breakfast anymore). 329 .PP 330 If a \fBport\fP match is included, for either or both of source and 331 destination, then it is only applied to 332 .\" XXX - "may only be" ? how does this apply to other protocols? will it not match, or will it be ignored? 333 TCP and UDP packets. If there is no \fBproto\fP match parameter, 334 packets from both protocols are compared. This is equivalent to "proto 335 tcp/udp". When composing \fBport\fP comparisons, either the service 336 name or an integer port number may be used. Port comparisons may be 337 done in a number of forms, with a number of comparison operators, or 338 port ranges may be specified. When the port appears as part of the 339 \fBfrom\fP object, it matches the source port number, when it appears 340 as part of the \fBto\fP object, it matches the destination port number. 341 See the examples for more information. 342 .PP 343 The \fBall\fP keyword is essentially a synonym for "from any to any" 344 with no other match parameters. 345 .PP 346 Following the source and destination matching parameters, the 347 following additional parameters may be used: 348 .TP 349 .B with 350 is used to match irregular attributes that some packets may have 351 associated with them. To match the presence of IP options in general, 352 use \fBwith ipopts\fP. To match packets that are too short to contain 353 a complete header, use \fBwith short\fP. To match fragmented packets, 354 use \fBwith frag\fP. For more specific filtering on IP options, 355 individual options can be listed. 356 .IP 357 Before any parameter used after the \fBwith\fP keyword, the word 358 \fBnot\fP or \fBno\fP may be inserted to cause the filter rule to only 359 match if the option(s) is not present. 360 .IP 361 Multiple consecutive \fBwith\fP clauses are allowed. Alternatively, 362 the keyword \fBand\fP may be used in place of \fBwith\fP, this is 363 provided purely to make the rules more readable ("with ... and ..."). 364 When multiple clauses are listed, all those must match to cause a 365 match of the rule. 366 .\" XXX describe the options more specifically in a separate section 367 .TP 368 .B flags 369 is only effective for TCP filtering. Each of the letters possible 370 represents one of the possible flags that can be set in the TCP 371 header. The association is as follows: 372 .LP 373 .nf 374 F - FIN 375 S - SYN 376 R - RST 377 P - PUSH 378 A - ACK 379 U - URG 380 .fi 381 .IP 382 The various flag symbols may be used in combination, so that "SA" 383 would represent a SYN-ACK combination present in a packet. There is 384 nothing preventing the specification of combinations, such as "SFR", 385 that would not normally be generated by law-abiding TCP 386 implementations. However, to guard against weird aberrations, it is 387 necessary to state which flags you are filtering against. To allow 388 this, it is possible to set a mask indicating which TCP flags you wish 389 to compare (i.e., those you deem significant). This is done by 390 appending "/<flags>" to the set of TCP flags you wish to match 391 against, e.g.: 392 .LP 393 .nf 394 ... flags S 395 # becomes "flags S/AUPRFS" and will match 396 # packets with ONLY the SYN flag set. 397 398 ... flags SA 399 # becomes "flags SA/AUPRFS" and will match any 400 # packet with only the SYN and ACK flags set. 401 402 ... flags S/SA 403 # will match any packet with just the SYN flag set 404 # out of the SYN-ACK pair; the common "establish" 405 # keyword action. "S/SA" will NOT match a packet 406 # with BOTH SYN and ACK set, but WILL match "SFP". 407 .fi 408 .TP 409 .B icmp-type 410 is only effective when used with \fBproto icmp\fP and must NOT be used 411 in conjunction with \fBflags\fP. There are a number of types, which can be 412 referred to by an abbreviation recognised by this language, or the numbers 413 with which they are associated can be used. The most important from 414 a security point of view is the ICMP redirect. 415 .SH KEEP HISTORY 416 .PP 417 The second last parameter which can be set for a filter rule is whether or not 418 to record historical information for that packet, and what sort to keep. The 419 following information can be kept: 420 .TP 421 .B state 422 keeps information about the flow of a communication session. State can 423 be kept for TCP, UDP, and ICMP packets. 424 .TP 425 .B frags 426 keeps information on fragmented packets, to be applied to later 427 fragments. 428 .PP 429 allowing packets which match these to flow straight through, rather 430 than going through the access control list. 431 .SH GROUPS 432 The last pair of parameters control filter rule "grouping". By default, all 433 filter rules are placed in group 0 if no other group is specified. To add a 434 rule to a non-default group, the group must first be started by creating a 435 group \fIhead\fP. If a packet matches a rule which is the \fIhead\fP of a 436 group, the filter processing then switches to the group, using that rule as 437 the default for the group. If \fBquick\fP is used with a \fBhead\fP rule, rule 438 processing isn't stopped until it has returned from processing the group. 439 .PP 440 A rule may be both the head for a new group and a member of a non-default 441 group (\fBhead\fP and \fBgroup\fP may be used together in a rule). 442 .TP 443 .B "head <n>" 444 indicates that a new group (number n) should be created. 445 .TP 446 .B "group <n>" 447 indicates that the rule should be put in group (number n) rather than group 0. 448 .SH LOGGING 449 .PP 450 When a packet is logged, with either the \fBlog\fP action or option, 451 the headers of the packet are written to the \fBipl\fP packet logging 452 pseudo-device. Immediately following the \fBlog\fP keyword, the 453 following qualifiers may be used (in order): 454 .TP 455 .B body 456 indicates that the first 128 bytes of the packet contents will be 457 logged after the headers. 458 .TP 459 .B first 460 If log is being used in conjunction with a "keep" option, it is recommended 461 that this option is also applied so that only the triggering packet is logged 462 and not every packet which thereafter matches state information. 463 .TP 464 .B or-block 465 indicates that, if for some reason the filter is unable to log the 466 packet (such as the log reader being too slow) then the rule should be 467 interpreted as if the action was \fBblock\fP for this packet. 468 .TP 469 .B "level <loglevel>" 470 indicates what logging facility and priority, or just priority with 471 the default facility being used, will be used to log information about 472 this packet using ipmon's -s option. 473 .PP 474 See ipl(4) for the format of records written 475 to this device. The ipmon(1M) program can be used to read and format 476 this log. 477 .SH EXAMPLES 478 .PP 479 The \fBquick\fP option is good for rules such as: 480 \fC 481 .nf 482 block in quick from any to any with ipopts 483 .fi 484 .PP 485 which will match any packet with a non-standard header length (IP 486 options present) and abort further processing of later rules, 487 recording a match and also that the packet should be blocked. 488 .PP 489 The "fall-through" rule parsing allows for effects such as this: 490 .LP 491 .nf 492 block in from any to any port < 6000 493 pass in from any to any port >= 6000 494 block in from any to any port > 6003 495 .fi 496 .PP 497 which sets up the range 6000-6003 as being permitted and all others being 498 denied. Note that the effect of the first rule is overridden by subsequent 499 rules. Another (easier) way to do the same is: 500 .LP 501 .nf 502 block in from any to any port 6000 <> 6003 503 pass in from any to any port 5999 >< 6004 504 .fi 505 .PP 506 Note that both the "block" and "pass" are needed here to effect a 507 result as a failed match on the "block" action does not imply a pass, 508 only that the rule hasn't taken effect. To then allow ports < 1024, a 509 rule such as: 510 .LP 511 .nf 512 pass in quick from any to any port < 1024 513 .fi 514 .PP 515 would be needed before the first block. To create a new group for 516 processing all inbound packets on le0/le1/lo0, with the default being to block 517 all inbound packets, we would do something like: 518 .LP 519 .nf 520 block in all 521 block in quick on le0 all head 100 522 block in quick on le1 all head 200 523 block in quick on lo0 all head 300 524 .fi 525 .PP 526 527 and to then allow ICMP packets in on le0, only, we would do: 528 .LP 529 .nf 530 pass in proto icmp all group 100 531 .fi 532 .PP 533 Note that because only inbound packets on le0 are used processed by group 100, 534 there is no need to respecify the interface name. Likewise, we could further 535 breakup processing of TCP, etc, as follows: 536 .LP 537 .nf 538 block in proto tcp all head 110 group 100 539 pass in from any to any port = 23 group 110 540 .fi 541 .PP 542 and so on. The last line, if written without the groups would be: 543 .LP 544 .nf 545 pass in on le0 proto tcp from any to any port = telnet 546 .fi 547 .PP 548 Note, that if we wanted to say "port = telnet", "proto tcp" would 549 need to be specified as the parser interprets each rule on its own and 550 qualifies all service/port names with the protocol specified. 551 .SH FILES 552 /dev/ipauth 553 .br 554 /dev/ipl 555 .br 556 /dev/ipstate 557 .br 558 /etc/hosts 559 .br 560 /etc/services 561 .SH SEE ALSO 562 \fBipnat\fR(4), \fBipf\fR(1M), \fBipfstat\fR(1M), \fBipfilter\fR(5)